28

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.

3 Answers 3

49

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'

2
  • 2
    Nice one. Note that 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
    – user330315
    Feb 22, 2018 at 14:26
  • 3
    In case it saves someone a quick lookup, the value return here is the size in bytes.
    – rocksteady
    Dec 22, 2022 at 18:09
10

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.

4
  • Thanks, but how can i run this query for all rows in a table.can you help me in providing the dyanamic query.? Dec 11, 2015 at 17:37
  • @DeepeshGoel: that will run for all rows in the table because it has no where clause
    – user330315
    Dec 11, 2015 at 22:52
  • @a_horse_with_no_name can you also share what is the unit of the output? is it bytes?
    – aquaman
    Jun 15, 2021 at 13:49
  • 1
    @aquaman Quote from the manual "Shows the number of bytes used to store any individual data value"
    – user330315
    Jun 15, 2021 at 13:50
0

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';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.