How to determine disc usage of a subset of a table in PostgreSQL?

时间:2016-07-11 20:51:08

标签: database postgresql

I'm trying to generate a table that groups disc size on a feature based on a filter. For example, this is the kind of query I'd expect to use:

select trip,pg_size_pretty(pg_database_size('cav_wsu')) as size from wsu where time between 0 and 3600 group by trip;

Which would generate something like this:

        trip          |    size 
----------------------+-------------
 1000                 | 120 kB      
 1001                 | 256 kB      
 1002                 | 111 kB      
 1003                 | 342 kB  

Is this at all possible?

1 个答案:

答案 0 :(得分:0)

使用pg_column_size

select trip, pg_size_pretty(pg_column_size(wsu)::bigint) as size
from wsu
where time between 0 and 3600
group by trip;

请注意,此函数不包括索引和TOAST文件使用的空间。 PostgreSQL似乎没有提供(我知道)一个值,但是你可以从表大小与总大小,行大小与表大小的比率平均得出它:

select trip, pg_size_pretty(pg_column_size(wsu)::bigint) as size,
       pg_size_pretty((pg_column_size(wsu)::float / pg_relation_size('wsu') * pg_total_relation_size('wsu'))::bigint) as total_size
from wsu
where time between 0 and 3600
group by trip;