这篇文章主要介绍“PostgreSQL12的pg_partition_tree和pg_partition_root系统函数有什么作用”,在日常操作中,相信很多人在PostgreSQL12的pg_partition_tree和pg_partition_root系统函数有什么作用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”PostgreSQL12的pg_partition_tree和pg_partition_root系统函数有什么作用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

在PG 12以前的版本,获取分区表中的分区以及子分区等信息需要使用递归CTE查询脚本来获取,不直观而且麻烦,在PG 12中新增了pg_partition_tree和pg_partition_root系统函数分别用于获取分区树和分区的root relation.

下面以一个简单的例子进行说明.

测试脚本

--HashPartitiondroptableifexistst_hash2;createtablet_hash2(c1intnotnull,c2varchar(40),c3varchar(40))partitionbyhash(c1);--Level1createtablet_hash2_1partitionoft_hash2forvalueswith(modulus6,remainder0)partitionbyhash(c1);createtablet_hash2_2partitionoft_hash2forvalueswith(modulus6,remainder1)partitionbyhash(c1);createtablet_hash2_3partitionoft_hash2forvalueswith(modulus6,remainder2);createtablet_hash2_4partitionoft_hash2forvalueswith(modulus6,remainder3);createtablet_hash2_5partitionoft_hash2forvalueswith(modulus6,remainder4);createtablet_hash2_6partitionoft_hash2forvalueswith(modulus6,remainder5);--Level2createtablet_hash2_1_1partitionoft_hash2_1forvalueswith(modulus2,remainder0);createtablet_hash2_1_2partitionoft_hash2_1forvalueswith(modulus2,remainder1);createtablet_hash2_2_1partitionoft_hash2_2forvalueswith(modulus2,remainder0);createtablet_hash2_2_2partitionoft_hash2_2forvalueswith(modulus2,remainder1);

t_hash2是一张Hash分区表,有6个子分区,其中子分区中的t_hash2_1和t_hash2_2也是分区表,分别有2个分区.

在PG 11中,需要使用CTE递归查询来查询该分区的相关信息:

--PG11WITHRECURSIVEpartition_info(relid,--oidrelname,--名称relsize,--大小relispartition,--是否分区表relkind)AS(SELECToidASrelid,relname,pg_relation_size(oid)ASrelsize,relispartition,relkindFROMpg_catalog.pg_classWHERErelname='t_hash2'AND--最顶层的分区表relkind='p'UNIONALLSELECTc.oidASrelid,c.relnameASrelname,pg_relation_size(c.oid)ASrelsize,c.relispartitionASrelispartition,c.relkindASrelkindFROMpartition_infoASp,pg_catalog.pg_inheritsASi,pg_catalog.pg_classAScWHEREp.relid=i.inhparentAND--从最顶层的分区表(即t_hash2)开始递归c.oid=i.inhrelidAND--寻找子分区c.relispartition--分区表标记)SELECT*FROMpartition_info;relid|relname|relsize|relispartition|relkind-------+-------------+---------+----------------+---------57457|t_hash2|0|f|p57466|t_hash2_3|0|t|r57469|t_hash2_4|0|t|r57472|t_hash2_5|0|t|r57475|t_hash2_6|0|t|r57460|t_hash2_1|0|t|p57463|t_hash2_2|0|t|p57487|t_hash2_2_2|0|t|r57478|t_hash2_1_1|0|t|r57481|t_hash2_1_2|0|t|r57484|t_hash2_2_1|0|t|r(11rows)

而在PG 12中,则可以直接使用系统函数获取相关信息:

testdb=#\sfpg_partition_treeCREATEORREPLACEFUNCTIONpg_catalog.pg_partition_tree(rootrelidregclass,OUTrelidregclass,OUTparentrelidregclass,OUTisleafboolean,OUTlevelinteger)RETURNSSETOFrecordLANGUAGEinternalPARALLELSAFESTRICTAS$function$pg_partition_tree$function$testdb=#selectpg_partition_tree('t_hash2');pg_partition_tree-----------------------------(t_hash2,,f,0)(t_hash2_1,t_hash2,f,1)(t_hash2_2,t_hash2,f,1)(t_hash2_3,t_hash2,t,1)(t_hash2_4,t_hash2,t,1)(t_hash2_5,t_hash2,t,1)(t_hash2_6,t_hash2,t,1)(t_hash2_1_1,t_hash2_1,t,2)(t_hash2_1_2,t_hash2_1,t,2)(t_hash2_2_1,t_hash2_2,t,2)(t_hash2_2_2,t_hash2_2,t,2)(11rows)

返回的信息包括:
relid -> 该分区的relid
parentrelid -> 父分区
isleaf —> 是否叶子节点
level —> 层次

通过pg_partition_root可以获取分区表的root节点

testdb=#\sfpg_partition_rootCREATEORREPLACEFUNCTIONpg_catalog.pg_partition_root(regclass)RETURNSregclassLANGUAGEinternalIMMUTABLEPARALLELSAFESTRICTAS$function$pg_partition_root$function$testdb=#selectpg_partition_root('t_hash2_2_2');pg_partition_root-------------------t_hash2(1row)

到此,关于“PostgreSQL12的pg_partition_tree和pg_partition_root系统函数有什么作用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!