In previous versions of PG 12, to obtain information such as the partition and sub-partition in the partition table you need to use a recursive CTE query to get the script, unintuitive and cumbersome, add the PG 12 in the pg_partition_tree and pg_partition_root system functions are used to get a partition tree and the root partition of relation.
Here a simple example will be described.
Test Script
-- Hash Partitiondrop table if exists t_hash1;create table t_hash1 (c1 int not null,c2 varchar(40),c3 varchar(40)) partition by hash(c1);-- Level 1create table t_hash1_1 partition of t_hash1 for values with (modulus 6,remainder 0) partition by hash(c1);create table t_hash1_2 partition of t_hash1 for values with (modulus 6,remainder 1) partition by hash(c1);create table t_hash1_3 partition of t_hash1 for values with (modulus 6,remainder 2);create table t_hash1_4 partition of t_hash1 for values with (modulus 6,remainder 3);create table t_hash1_5 partition of t_hash1 for values with (modulus 6,remainder 4);create table t_hash1_6 partition of t_hash1 for values with (modulus 6,remainder 5);-- Level 2create table t_hash1_1_1 partition of t_hash1_1 for values with (modulus 2,remainder 0);create table t_hash1_1_2 partition of t_hash1_1 for values with (modulus 2,remainder 1);create table t_hash1_2_1 partition of t_hash1_2 for values with (modulus 2,remainder 0);create table t_hash1_2_2 partition of t_hash1_2 for values with (modulus 2,remainder 1);
t_hash1 Hash is a partition table, there are sub-partition 6, wherein the sub-partition is the partition table t_hash1_2 t_hash1_1 and, respectively, two partitions.
Zhengzhou infertility hospital: http: //byby.zztjyy.com/yiyuanzaixian/zztjyy//
In PG 11, you need to use a recursive CTE query for information about the partition:
-- PG11WITH RECURSIVE partition_info (relid, -- oid relname, -- 名称 relsize, -- 大小 relispartition, -- 是否分区表 relkind) AS ( SELECT oid AS relid, relname, pg_relation_size(oid) AS relsize, relispartition, relkind FROM pg_catalog.pg_classWHERE relname = 't_hash1' AND -- 最顶层的分区表 relkind = 'p' UNION ALL SELECT c.oid AS relid, c.relname AS relname, pg_relation_size(c.oid) AS relsize, c.relispartition AS relispartition, c.relkind AS relkind FROM partition_info AS p, pg_catalog.pg_inherits AS i, pg_catalog.pg_class AS c WHERE p.relid = i.inhparent AND -- 从最顶层的分区表(即t_hash1)开始递归 c.oid = i.inhrelid AND -- 寻找子分区 c.relispartition -- 分区表标记 )SELECT * FROM partition_info; relid | relname | relsize | relispartition | relkind -------+-------------+---------+----------------+--------- 57457 | t_hash1 | 0 | f | p 57466 | t_hash1_3 | 0 | t | r 57469 | t_hash1_4 | 0 | t | r 57472 | t_hash1_5 | 0 | t | r 57475 | t_hash1_6 | 0 | t | r 57460 | t_hash1_1 | 0 | t | p 57463 | t_hash1_2 | 0 | t | p 57487 | t_hash1_2_2 | 0 | t | r 57478 | t_hash1_1_1 | 0 | t | r 57481 | t_hash1_1_2 | 0 | t | r 57484 | t_hash1_2_1 | 0 | t | r(11 rows)
In the PG 12, the system functions can be used directly to obtain information:
testdb=# \sf pg_partition_treeCREATE OR REPLACE FUNCTION pg_catalog.pg_partition_tree(rootrelid regclass, OUT relid regclass, OUT parentrelid regclass, OUT isleaf boolean, OUT level integer) RETURNS SETOF record LANGUAGE internal PARALLEL SAFE STRICTAS $function$pg_partition_tree$function$testdb=# select pg_partition_tree('t_hash1'); pg_partition_tree ----------------------------- (t_hash1,,f,0) (t_hash1_1,t_hash1,f,1) (t_hash1_2,t_hash1,f,1) (t_hash1_3,t_hash1,t,1) (t_hash1_4,t_hash1,t,1) (t_hash1_5,t_hash1,t,1) (t_hash1_6,t_hash1,t,1) (t_hash1_1_1,t_hash1_1,t,2) (t_hash1_1_2,t_hash1_1,t,2) (t_hash1_2_1,t_hash1_2,t,2) (t_hash1_2_2,t_hash1_2,t,2)(11 rows)
Information returned includes:
RELID -> the partition RELID
parentrelid -> parent partition
isleaf -> if the leaf node
level -> level
通过pg_partition_root可以获取分区表的root节点
testdb=# \sf pg_partition_rootCREATE OR REPLACE FUNCTION pg_catalog.pg_partition_root(regclass) RETURNS regclass LANGUAGE internal IMMUTABLE PARALLEL SAFE STRICTAS $function$pg_partition_root$function$testdb=# select pg_partition_root('t_hash1_2_2'); pg_partition_root ------------------- t_hash1(1 row)
参考资料
Postgres 12 highlight - Functions for partitions