On Fri, 25 Mar 2022 at 20:59, Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:
> On 2022-Mar-25, Japin Li wrote:
>
>> Could we provide a function to get the total size of the partition table
>> though the partitioned table name? Maybe we can extend
>> the pg_relation_size() to get the total size of partition tables through
>> the partitioned table name.
>
> Does \dP+ do what you need?
Thanks for your quick response!
I find the \dP+ use the following SQL:
SELECT n.nspname as "Schema",
c.relname as "Name",
pg_catalog.pg_get_userbyid(c.relowner) as "Owner",
CASE c.relkind WHEN 'p' THEN 'partitioned table' WHEN 'I' THEN 'partitioned index' END as "Type",
inh.inhparent::pg_catalog.regclass as "Parent name",
c2.oid::pg_catalog.regclass as "Table",
s.tps as "Total size",
pg_catalog.obj_description(c.oid, 'pg_class') as "Description"
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
LEFT JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid
LEFT JOIN pg_catalog.pg_class c2 ON i.indrelid = c2.oid
LEFT JOIN pg_catalog.pg_inherits inh ON c.oid = inh.inhrelid,
LATERAL (SELECT pg_catalog.pg_size_pretty(sum(
CASE WHEN ppt.isleaf AND ppt.level = 1
THEN pg_catalog.pg_table_size(ppt.relid) ELSE 0 END)) AS dps,
pg_catalog.pg_size_pretty(sum(pg_catalog.pg_table_size(ppt.relid))) AS tps
FROM pg_catalog.pg_partition_tree(c.oid) ppt) s
WHERE c.relkind IN ('p','I','')
AND c.relname OPERATOR(pg_catalog.~) '^(parent)$' COLLATE pg_catalog.default
AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY "Schema", "Type" DESC, "Parent name" NULLS FIRST, "Name";
pg_table_size() includes "main", "vm", "fsm", "init" and "toast", however,
I only care about the "main" fork.
--
Regrads,
Japin Li.
ChengDu WenWu Information Technology Co.,Ltd.