Is it possible to get each row size of a particular table in postgres?
For example a table has 50 rows and total table size is 10gb but i want to know each row size rather than complete table size.
Function to calculate row size of postgress table row is something like this
SELECT sum(pg_column_size(t.*)) as filesize, count(*) as filerow FROM TABLE_NAME as t;
replace TABLE_NAME
with your table name;
to add condition t.COLUMN_NAME = 'YOUR_VALUE'
pg_column_size(t.*)
can be simplified to pg_column_size(t)
. That value will include the row header "overhead", so to get the actual data size, you need to subtract 24: pg_column_size(t) - 24
There is no function that calculates the size of a row, only a function that calculates the size of a column value. So you can do something like this:
select pg_column_size(column_1) +
pg_column_size(column_2) +
pg_column_size(column_3) as row_size
from the_table;
This could be automated using dynamic SQL and a PL/pgSQL function if you need this.
where
clause
If you want to calculate the size of a specific row or with the where clause returning only 1 row, try below -
select pg_column_size(table_name.*) as rowsize_in_bytes from table_name where id = '698119855102848';