1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-07 00:36:50 +03:00

Add pg_partition_root to display top-most parent of a partition tree

This is useful when looking at partition trees with multiple layers, and
combined with pg_partition_tree, it provides the possibility to show up
an entire tree by just knowing one member at any level.

Author: Michael Paquier
Reviewed-by: Álvaro Herrera, Amit Langote
Discussion: https://postgr.es/m/20181207014015.GP2407@paquier.xyz
This commit is contained in:
Michael Paquier
2019-02-08 08:56:14 +09:00
parent 34ea1ab7fd
commit 3677a0b26b
6 changed files with 153 additions and 11 deletions

View File

@ -12,6 +12,18 @@ SELECT * FROM pg_partition_tree(0);
| | |
(1 row)
SELECT pg_partition_root(NULL);
pg_partition_root
-------------------
(1 row)
SELECT pg_partition_root(0);
pg_partition_root
-------------------
(1 row)
-- Test table partition trees
CREATE TABLE ptif_test (a int, b int) PARTITION BY range (a);
CREATE TABLE ptif_test0 PARTITION OF ptif_test
@ -66,6 +78,20 @@ SELECT relid, parentrelid, level, isleaf
ptif_test01 | ptif_test0 | 0 | t
(1 row)
-- List all members using pg_partition_root with leaf table reference
SELECT relid, parentrelid, level, isleaf
FROM pg_partition_tree(pg_partition_root('ptif_test01')) p
JOIN pg_class c ON (p.relid = c.oid);
relid | parentrelid | level | isleaf
-------------+-------------+-------+--------
ptif_test | | 0 | f
ptif_test0 | ptif_test | 1 | f
ptif_test1 | ptif_test | 1 | f
ptif_test2 | ptif_test | 1 | t
ptif_test01 | ptif_test0 | 2 | t
ptif_test11 | ptif_test1 | 2 | t
(6 rows)
-- List all indexes members of the tree
SELECT relid, parentrelid, level, isleaf
FROM pg_partition_tree('ptif_test_index');
@ -98,6 +124,20 @@ SELECT relid, parentrelid, level, isleaf
ptif_test01_index | ptif_test0_index | 0 | t
(1 row)
-- List all members using pg_partition_root with leaf index reference
SELECT relid, parentrelid, level, isleaf
FROM pg_partition_tree(pg_partition_root('ptif_test01_index')) p
JOIN pg_class c ON (p.relid = c.oid);
relid | parentrelid | level | isleaf
-------------------+------------------+-------+--------
ptif_test_index | | 0 | f
ptif_test0_index | ptif_test_index | 1 | f
ptif_test1_index | ptif_test_index | 1 | f
ptif_test2_index | ptif_test_index | 1 | t
ptif_test01_index | ptif_test0_index | 2 | t
ptif_test11_index | ptif_test1_index | 2 | t
(6 rows)
DROP TABLE ptif_test;
-- Table that is not part of any partition tree is the only member listed.
CREATE TABLE ptif_normal_table(a int);
@ -108,6 +148,12 @@ SELECT relid, parentrelid, level, isleaf
ptif_normal_table | | 0 | t
(1 row)
SELECT pg_partition_root('ptif_normal_table');
pg_partition_root
-------------------
ptif_normal_table
(1 row)
DROP TABLE ptif_normal_table;
-- Various partitioning-related functions return NULL if passed relations
-- of types that cannot be part of a partition tree; for example, views,
@ -126,5 +172,17 @@ SELECT * FROM pg_partition_tree('ptif_test_matview');
| | |
(1 row)
SELECT pg_partition_root('ptif_test_view');
pg_partition_root
-------------------
(1 row)
SELECT pg_partition_root('ptif_test_matview');
pg_partition_root
-------------------
(1 row)
DROP VIEW ptif_test_view;
DROP MATERIALIZED VIEW ptif_test_matview;

View File

@ -3,6 +3,8 @@
--
SELECT * FROM pg_partition_tree(NULL);
SELECT * FROM pg_partition_tree(0);
SELECT pg_partition_root(NULL);
SELECT pg_partition_root(0);
-- Test table partition trees
CREATE TABLE ptif_test (a int, b int) PARTITION BY range (a);
@ -39,6 +41,10 @@ SELECT relid, parentrelid, level, isleaf
SELECT relid, parentrelid, level, isleaf
FROM pg_partition_tree('ptif_test01') p
JOIN pg_class c ON (p.relid = c.oid);
-- List all members using pg_partition_root with leaf table reference
SELECT relid, parentrelid, level, isleaf
FROM pg_partition_tree(pg_partition_root('ptif_test01')) p
JOIN pg_class c ON (p.relid = c.oid);
-- List all indexes members of the tree
SELECT relid, parentrelid, level, isleaf
@ -51,6 +57,10 @@ SELECT relid, parentrelid, level, isleaf
SELECT relid, parentrelid, level, isleaf
FROM pg_partition_tree('ptif_test01_index') p
JOIN pg_class c ON (p.relid = c.oid);
-- List all members using pg_partition_root with leaf index reference
SELECT relid, parentrelid, level, isleaf
FROM pg_partition_tree(pg_partition_root('ptif_test01_index')) p
JOIN pg_class c ON (p.relid = c.oid);
DROP TABLE ptif_test;
@ -58,6 +68,7 @@ DROP TABLE ptif_test;
CREATE TABLE ptif_normal_table(a int);
SELECT relid, parentrelid, level, isleaf
FROM pg_partition_tree('ptif_normal_table');
SELECT pg_partition_root('ptif_normal_table');
DROP TABLE ptif_normal_table;
-- Various partitioning-related functions return NULL if passed relations
@ -67,5 +78,7 @@ CREATE VIEW ptif_test_view AS SELECT 1;
CREATE MATERIALIZED VIEW ptif_test_matview AS SELECT 1;
SELECT * FROM pg_partition_tree('ptif_test_view');
SELECT * FROM pg_partition_tree('ptif_test_matview');
SELECT pg_partition_root('ptif_test_view');
SELECT pg_partition_root('ptif_test_matview');
DROP VIEW ptif_test_view;
DROP MATERIALIZED VIEW ptif_test_matview;