PostgreSQL12的pg_partition_tree和pg_partition_root系統(tǒng)函數(shù)有什么作用

這篇文章主要介紹“PostgreSQL12的pg_partition_tree和pg_partition_root系統(tǒng)函數(shù)有什么作用”,在日常操作中,相信很多人在PostgreSQL12的pg_partition_tree和pg_partition_root系統(tǒng)函數(shù)有什么作用問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”PostgreSQL12的pg_partition_tree和pg_partition_root系統(tǒng)函數(shù)有什么作用”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

創(chuàng)新互聯(lián)公司專注于企業(yè)營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站重做改版、交城網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5建站、成都商城網(wǎng)站開(kāi)發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為交城等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。

在PG 12以前的版本,獲取分區(qū)表中的分區(qū)以及子分區(qū)等信息需要使用遞歸CTE查詢腳本來(lái)獲取,不直觀而且麻煩,在PG 12中新增了pg_partition_tree和pg_partition_root系統(tǒng)函數(shù)分別用于獲取分區(qū)樹(shù)和分區(qū)的root relation.

下面以一個(gè)簡(jiǎn)單的例子進(jìn)行說(shuō)明.

測(cè)試腳本

-- Hash Partition
drop table if exists t_hash2;
create table t_hash2 (c1 int not null,c2  varchar(40),c3 varchar(40)) partition by hash(c1);
-- Level 1
create table t_hash2_1 partition of t_hash2 for values with (modulus 6,remainder 0) partition by hash(c1);
create table t_hash2_2 partition of t_hash2 for values with (modulus 6,remainder 1) partition by hash(c1);
create table t_hash2_3 partition of t_hash2 for values with (modulus 6,remainder 2);
create table t_hash2_4 partition of t_hash2 for values with (modulus 6,remainder 3);
create table t_hash2_5 partition of t_hash2 for values with (modulus 6,remainder 4);
create table t_hash2_6 partition of t_hash2 for values with (modulus 6,remainder 5);
-- Level 2
create table t_hash2_1_1 partition of t_hash2_1 for values with (modulus 2,remainder 0);
create table t_hash2_1_2 partition of t_hash2_1 for values with (modulus 2,remainder 1);
create table t_hash2_2_1 partition of t_hash2_2 for values with (modulus 2,remainder 0);
create table t_hash2_2_2 partition of t_hash2_2 for values with (modulus 2,remainder 1);

t_hash2是一張Hash分區(qū)表,有6個(gè)子分區(qū),其中子分區(qū)中的t_hash2_1和t_hash2_2也是分區(qū)表,分別有2個(gè)分區(qū).

在PG 11中,需要使用CTE遞歸查詢來(lái)查詢?cè)摲謪^(qū)的相關(guān)信息:

-- PG11
WITH RECURSIVE partition_info
      (relid,             -- oid
       relname,            -- 名稱
       relsize,            -- 大小
       relispartition,     -- 是否分區(qū)表
       relkind) AS (
    SELECT oid AS relid,
           relname,
           pg_relation_size(oid) AS relsize,
           relispartition,
           relkind
    FROM pg_catalog.pg_class
WHERE relname = 't_hash2' AND -- 最頂層的分區(qū)表
      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 -- 從最頂層的分區(qū)表(即t_hash2)開(kāi)始遞歸
         c.oid = i.inhrelid AND -- 尋找子分區(qū)
         c.relispartition -- 分區(qū)表標(biāo)記
  )
SELECT * FROM partition_info;
 relid |   relname   | relsize | relispartition | relkind 
-------+-------------+---------+----------------+---------
 57457 | t_hash2     |       0 | f              | p
 57466 | t_hash2_3   |       0 | t              | r
 57469 | t_hash2_4   |       0 | t              | r
 57472 | t_hash2_5   |       0 | t              | r
 57475 | t_hash2_6   |       0 | t              | r
 57460 | t_hash2_1   |       0 | t              | p
 57463 | t_hash2_2   |       0 | t              | p
 57487 | t_hash2_2_2 |       0 | t              | r
 57478 | t_hash2_1_1 |       0 | t              | r
 57481 | t_hash2_1_2 |       0 | t              | r
 57484 | t_hash2_2_1 |       0 | t              | r
(11 rows)

而在PG 12中,則可以直接使用系統(tǒng)函數(shù)獲取相關(guān)信息:

testdb=# \sf pg_partition_tree
CREATE 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 STRICT
AS $function$pg_partition_tree$function$
testdb=# select pg_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)
(11 rows)

返回的信息包括:
relid -> 該分區(qū)的relid
parentrelid -> 父分區(qū)
isleaf —> 是否葉子節(jié)點(diǎn)
level —> 層次

通過(guò)pg_partition_root可以獲取分區(qū)表的root節(jié)點(diǎn)

testdb=# \sf pg_partition_root
CREATE OR REPLACE FUNCTION pg_catalog.pg_partition_root(regclass)
 RETURNS regclass
 LANGUAGE internal
 IMMUTABLE PARALLEL SAFE STRICT
AS $function$pg_partition_root$function$
testdb=# select pg_partition_root('t_hash2_2_2');
 pg_partition_root 
-------------------
 t_hash2
(1 row)

到此,關(guān)于“PostgreSQL12的pg_partition_tree和pg_partition_root系統(tǒng)函數(shù)有什么作用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

本文名稱:PostgreSQL12的pg_partition_tree和pg_partition_root系統(tǒng)函數(shù)有什么作用
轉(zhuǎn)載來(lái)源:http://muchs.cn/article0/ghspio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、網(wǎng)站營(yíng)銷、網(wǎng)站策劃、面包屑導(dǎo)航、網(wǎng)站改版、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名