Reputation: 347
I have a table in PostgreSQL DB and make a select from this table with some constraints, and than I want to know how much disk space does this select has. I know that there is a postgres function pg_total_relation_size
that gives me the size of some table in DB, but how can I find the 'subtable' size?
Any Ideas?
I use PostgreSQL 9.1
Upvotes: 6
Views: 25973
Reputation: 324511
To get the data size, allowing for TOAST
compression, etc:
regress=> SELECT sum(pg_column_size(devices)) FROM devices WHERE country = 'US';
sum
-----
105
(1 row)
To get the disk storage required including block allocation overhead, headers, etc etc:
regress=> CREATE TEMPORARY TABLE query_out AS SELECT * FROM devices WHERE country = 'US';
SELECT 3
regress=> SELECT pg_total_relation_size('query_out');
pg_total_relation_size
------------------------
16384
(1 row)
Why are the results so different? Because the latter query is reporting the size of the 8k block for the main table, and the 8k block for the TOAST table. It doesn't care that these blocks are mostly empty.
Upvotes: 19