|
| 1 | +-- |
| 2 | +-- Tests for pg_partition_tree |
| 3 | +-- |
| 4 | +SELECT * FROM pg_partition_tree(NULL); |
| 5 | + relid | parentrelid | isleaf | level |
| 6 | +-------+-------------+--------+------- |
| 7 | +(0 rows) |
| 8 | + |
| 9 | +-- Test table partition trees |
| 10 | +CREATE TABLE ptif_test (a int, b int) PARTITION BY range (a); |
| 11 | +CREATE TABLE ptif_test0 PARTITION OF ptif_test |
| 12 | + FOR VALUES FROM (minvalue) TO (0) PARTITION BY list (b); |
| 13 | +CREATE TABLE ptif_test01 PARTITION OF ptif_test0 FOR VALUES IN (1); |
| 14 | +CREATE TABLE ptif_test1 PARTITION OF ptif_test |
| 15 | + FOR VALUES FROM (0) TO (100) PARTITION BY list (b); |
| 16 | +CREATE TABLE ptif_test11 PARTITION OF ptif_test1 FOR VALUES IN (1); |
| 17 | +CREATE TABLE ptif_test2 PARTITION OF ptif_test |
| 18 | + FOR VALUES FROM (100) TO (maxvalue); |
| 19 | +-- Test index partition tree |
| 20 | +CREATE INDEX ptif_test_index ON ONLY ptif_test (a); |
| 21 | +CREATE INDEX ptif_test0_index ON ONLY ptif_test0 (a); |
| 22 | +ALTER INDEX ptif_test_index ATTACH PARTITION ptif_test0_index; |
| 23 | +CREATE INDEX ptif_test01_index ON ptif_test01 (a); |
| 24 | +ALTER INDEX ptif_test0_index ATTACH PARTITION ptif_test01_index; |
| 25 | +CREATE INDEX ptif_test1_index ON ONLY ptif_test1 (a); |
| 26 | +ALTER INDEX ptif_test_index ATTACH PARTITION ptif_test1_index; |
| 27 | +CREATE INDEX ptif_test11_index ON ptif_test11 (a); |
| 28 | +ALTER INDEX ptif_test1_index ATTACH PARTITION ptif_test11_index; |
| 29 | +CREATE INDEX ptif_test2_index ON ptif_test2 (a); |
| 30 | +ALTER INDEX ptif_test_index ATTACH PARTITION ptif_test2_index; |
| 31 | +-- List all tables members of the tree |
| 32 | +SELECT relid, parentrelid, level, isleaf |
| 33 | + FROM pg_partition_tree('ptif_test'); |
| 34 | + relid | parentrelid | level | isleaf |
| 35 | +-------------+-------------+-------+-------- |
| 36 | + ptif_test | | 0 | f |
| 37 | + ptif_test0 | ptif_test | 1 | f |
| 38 | + ptif_test1 | ptif_test | 1 | f |
| 39 | + ptif_test2 | ptif_test | 1 | t |
| 40 | + ptif_test01 | ptif_test0 | 2 | t |
| 41 | + ptif_test11 | ptif_test1 | 2 | t |
| 42 | +(6 rows) |
| 43 | + |
| 44 | +-- List tables from an intermediate level |
| 45 | +SELECT relid, parentrelid, level, isleaf |
| 46 | + FROM pg_partition_tree('ptif_test0') p |
| 47 | + JOIN pg_class c ON (p.relid = c.oid); |
| 48 | + relid | parentrelid | level | isleaf |
| 49 | +-------------+-------------+-------+-------- |
| 50 | + ptif_test0 | ptif_test | 0 | f |
| 51 | + ptif_test01 | ptif_test0 | 1 | t |
| 52 | +(2 rows) |
| 53 | + |
| 54 | +-- List from leaf table |
| 55 | +SELECT relid, parentrelid, level, isleaf |
| 56 | + FROM pg_partition_tree('ptif_test01') p |
| 57 | + JOIN pg_class c ON (p.relid = c.oid); |
| 58 | + relid | parentrelid | level | isleaf |
| 59 | +-------------+-------------+-------+-------- |
| 60 | + ptif_test01 | ptif_test0 | 0 | t |
| 61 | +(1 row) |
| 62 | + |
| 63 | +-- List all indexes members of the tree |
| 64 | +SELECT relid, parentrelid, level, isleaf |
| 65 | + FROM pg_partition_tree('ptif_test_index'); |
| 66 | + relid | parentrelid | level | isleaf |
| 67 | +-------------------+------------------+-------+-------- |
| 68 | + ptif_test_index | | 0 | f |
| 69 | + ptif_test0_index | ptif_test_index | 1 | f |
| 70 | + ptif_test1_index | ptif_test_index | 1 | f |
| 71 | + ptif_test2_index | ptif_test_index | 1 | t |
| 72 | + ptif_test01_index | ptif_test0_index | 2 | t |
| 73 | + ptif_test11_index | ptif_test1_index | 2 | t |
| 74 | +(6 rows) |
| 75 | + |
| 76 | +-- List indexes from an intermediate level |
| 77 | +SELECT relid, parentrelid, level, isleaf |
| 78 | + FROM pg_partition_tree('ptif_test0_index') p |
| 79 | + JOIN pg_class c ON (p.relid = c.oid); |
| 80 | + relid | parentrelid | level | isleaf |
| 81 | +-------------------+------------------+-------+-------- |
| 82 | + ptif_test0_index | ptif_test_index | 0 | f |
| 83 | + ptif_test01_index | ptif_test0_index | 1 | t |
| 84 | +(2 rows) |
| 85 | + |
| 86 | +-- List from leaf index |
| 87 | +SELECT relid, parentrelid, level, isleaf |
| 88 | + FROM pg_partition_tree('ptif_test01_index') p |
| 89 | + JOIN pg_class c ON (p.relid = c.oid); |
| 90 | + relid | parentrelid | level | isleaf |
| 91 | +-------------------+------------------+-------+-------- |
| 92 | + ptif_test01_index | ptif_test0_index | 0 | t |
| 93 | +(1 row) |
| 94 | + |
| 95 | +DROP TABLE ptif_test; |
| 96 | +-- A table not part of a partition tree works is the only member listed. |
| 97 | +CREATE TABLE ptif_normal_table(a int); |
| 98 | +SELECT relid, parentrelid, level, isleaf |
| 99 | + FROM pg_partition_tree('ptif_normal_table'); |
| 100 | + relid | parentrelid | level | isleaf |
| 101 | +-------------------+-------------+-------+-------- |
| 102 | + ptif_normal_table | | 0 | t |
| 103 | +(1 row) |
| 104 | + |
| 105 | +DROP TABLE ptif_normal_table; |
| 106 | +-- Views and materialized viewS cannot be part of a partition tree. |
| 107 | +CREATE VIEW ptif_test_view AS SELECT 1; |
| 108 | +CREATE MATERIALIZED VIEW ptif_test_matview AS SELECT 1; |
| 109 | +SELECT * FROM pg_partition_tree('ptif_test_view'); |
| 110 | +ERROR: "ptif_test_view" is not a table, a foreign table, or an index |
| 111 | +SELECT * FROM pg_partition_tree('ptif_test_matview'); |
| 112 | +ERROR: "ptif_test_matview" is not a table, a foreign table, or an index |
| 113 | +DROP VIEW ptif_test_view; |
| 114 | +DROP MATERIALIZED VIEW ptif_test_matview; |
0 commit comments