大家知道 PostgreSQL 的分区是通过继承来实现的,按分区方式,可以实现表的列表分区,范围分区,以及复合分区等, 这里不打算详细介绍分区表的使用,本篇 blog 仅介绍关于分区表的几个查询,方便维护和管理分区表。
查询指定分区表信息
1 2 3 4 5 6 7 8 9 10 11 12 13
SELECT nmsp_parent.nspname AS parent_schema , parent.relname ASparent , nmsp_child.nspname ASchild , child.relname AS child_schema FROM pg_inherits JOIN pg_class parent ON pg_inherits.inhparent = parent.oid JOIN pg_class child ON pg_inherits.inhrelid = child.oid JOIN pg_namespace nmsp_parent ON nmsp_parent.oid = parent.relnamespace JOIN pg_namespace nmsp_child ON nmsp_child.oid = child.relnamespace WHERE parent.relname = 'table_name';
查询库中所有分区表子表个数
1 2 3 4 5 6 7 8 9 10 11 12 13 14
SELECT nspname , relname , COUNT(*) AS partition_num FROM pg_class c , pg_namespace n , pg_inherits i WHERE c.oid = i.inhparent AND c.relnamespace = n.oid AND c.relhassubclass AND c.relkind = 'r' GROUPBY1,2ORDERBY partition_num DESC;