PSQL - Select 分区表和普通表的大小

PSQL - Select size of tables for both partitioned and normal

在此先感谢您对此的任何帮助,非常感谢。

所以,基本上,我有一个 Greenplum 数据库,我想要 select 前 10 个最大 table 的 table 大小。使用以下不是问题:

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
;

但是我的数据库中有几个分区的 table,它们与上面的 sql 一起显示,因为它们的所有 'child tables' 都分裂成小片段(尽管我知道它们会累积使最大的 2 tables)。有没有办法制作 selects tables(分区或其他)及其总大小的脚本?

注意:我很乐意加入某种类型的连接,我在其中指定分区的 table-名称,因为只有 2 个分区的 table。但是,我仍然需要进入前 10 名(我不能假设分区的 table(s) 在那里)并且我不能指定任何其他 table 名称,因为它们有将近一千个.

再次感谢, 文尼.

您的朋友将使用 pg_relation_size() 函数来获取关系大小,您将 select 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;
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;