久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

PostgreSQL12的pg

共計(jì) 3991 個(gè)字符,預(yù)計(jì)需要花費(fèi) 10 分鐘才能閱讀完成。

行業(yè)資訊    
數(shù)據(jù)庫(kù)    
關(guān)系型數(shù)據(jù)庫(kù)    
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ù)有什么作用問題上存在疑惑,丸趣 TV 小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”PostgreSQL12 的 pg_partition_tree 和 pg_partition_root 系統(tǒng)函數(shù)有什么作用”的疑惑有所幫助!接下來(lái),請(qǐng)跟著丸趣 TV 小編一起來(lái)學(xué)習(xí)吧!

在 PG 12 以前的版本, 獲取分區(qū)表中的分區(qū)以及子分區(qū)等信息需要使用遞歸 CTE 查詢腳本來(lái)獲取, 不直觀而且麻煩, 在 PG 12 中新增了 pg_partition_tree 和 pg_partition_root 系統(tǒng)函數(shù)分別用于獲取分區(qū)樹和分區(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) 開始遞歸
 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)注丸趣 TV 網(wǎng)站,丸趣 TV 小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

正文完
 
丸趣
版權(quán)聲明:本站原創(chuàng)文章,由 丸趣 2023-07-24發(fā)表,共計(jì)3991字。
轉(zhuǎn)載說(shuō)明:除特殊說(shuō)明外本站除技術(shù)相關(guān)以外文章皆由網(wǎng)絡(luò)搜集發(fā)布,轉(zhuǎn)載請(qǐng)注明出處。
評(píng)論(沒有評(píng)論)
主站蜘蛛池模板: 龙海市| 观塘区| 嵩明县| 张家界市| 宁海县| 宣威市| 都江堰市| 宜丰县| 武威市| 专栏| 石阡县| 叙永县| 环江| 固始县| 西乡县| 新沂市| 天柱县| 平顺县| 江门市| 温宿县| 介休市| 泉州市| 宁都县| 襄樊市| 惠州市| 都安| 丁青县| 河池市| 南部县| 象州县| 北碚区| 成都市| 福鼎市| 专栏| 贵阳市| 慈溪市| 措美县| 安泽县| 临夏市| 当涂县| 花莲县|