PostgreSQL select result size

社会主义新天地 提交于 2019-12-12 01:33:25

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/13476649/postgresql-select-result-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!