PSQL - 选择分区和普通表的大小

时间:2015-09-28 10:03:00

标签: psql database-partitioning greenplum

提前感谢您对此的任何帮助,我们非常感谢。

所以,基本上,我有一个Greenplum数据库,我想选择前10大表的表大小。使用以下内容不是问题:

select 
sotaidschemaname schema_name
,sotaidtablename table_name
,pg_size_pretty(sotaidtablesize) table_size
from gp_toolkit.gp_size_of_table_and_indexes_disk
order by 3 desc
limit 10
;

但是我在我的数据库中有几个分区表,这些表显示上面的sql作为他们所有的子表'分成小片段(虽然我知道他们准备制作最大的2张桌子)。有没有办法制作一个脚本来选择表(分区或其他)及其总大小?

注意:我很乐意在我指定partitoned table-name时特别包含某种连接,因为只有2个分区表。但是,我仍然需要进入前10名(我不能假设分区表在那里)并且我不能指定任何其他表名,因为它们有近千个。

再次感谢, 温尼。

2 个答案:

答案 0 :(得分:0)

您的朋友将获得pg_relation_size()函数来获取关系大小,您可以选择pg_class,pg_namespace和pg_partition将它们连接在一起,如下所示:

select  schemaname,
        tablename,
        sum(size_mb) as size_mb,
        sum(num_partitions) as num_partitions
    from (
        select  coalesce(p.schemaname, n.nspname) as schemaname,
                coalesce(p.tablename, c.relname) as tablename,
                1 as num_partitions,
                pg_relation_size(n.nspname || '.' || c.relname)/1000000. as size_mb
            from pg_class as c
                inner join pg_namespace as n on c.relnamespace = n.oid
                left join pg_partitions as p on c.relname = p.partitiontablename and n.nspname = p.partitionschemaname    
        ) as q
    group by 1, 2
    order by 3 desc
    limit 10;

答案 1 :(得分:0)

select * from 
(       
select schemaname,tablename, 
pg_relation_size(schemaname||'.'||tablename) as Size_In_Bytes 
from pg_tables 
where schemaname||'.'||tablename not in (select schemaname||'.'||partitiontablename from pg_partitions) 
and schemaname||'.'||tablename not in (select distinct schemaname||'.'||tablename from pg_partitions ) 

union all 

select schemaname,tablename, 
sum(pg_relation_size(schemaname||'.'||partitiontablename)) as Size_In_Bytes 
from pg_partitions
group by 1,2) as foo 

where Size_In_Bytes >= '0' order by 3 desc;