Search code examples
postgresqlsizebytediskspace

Determine size of a table or selected records in terms of bytes/megabytes


I've been a bit lazy lately and have been generating gigabytes of PostGis database records much of which I don't need anymore. I'm beginning to think of cleaning it out a bit, but want to determine how many bytes these records are taking up.

I'm using rails, so if I could call a method or SQL snippet on an already selected group of records to determine the size of said records in bytes this would be very helpful.


Solution

  • For the table:

    SELECT  pg_size_pretty(pg_total_relation_size('name_of_tbl'));
    

    For a particular table row and/or column in it:

    SELECT pg_column_size(t) AS row_size_on_disk
           pg_column_size(t.column1) AS column1_size_on_disk
    FROM   tbl t
    WHERE  tbl_id = ?;
    

    See: