pg_sizeof function?

Viewed 33

I love the pg_typeof function to get a type of a value, such as:

SELECT pg_typeof(1), pg_typeof(1.4);
integer  numeric

Is there a way to get the byte size of a value as well, doing something like:

SELECT pg_sizeof(1.4);

I know that for each data type I can go back to the documentation page and try and figure out what is what, but is there a way to do that interactively with a query?

1 Answers

Using pg_column_size you can do something like:

select v, pg_typeof(v), pg_column_size(v) from (values (1)) _ (v) union ALL
select v, pg_typeof(v), pg_column_size(v) from (values (1.2)) _ (v) union ALL
select v, pg_typeof(v), pg_column_size(v) from (values (1e2)) _ (v)

enter image description here

Related