Skip to content

Commit 2ab6d28

Browse files
committed
Fix crash with pg_partition_root
Trying to call the function with the top-most parent of a partition tree was leading to a crash. In this case the correct result is to return the top-most parent itself. Reported-by: Álvaro Herrera Author: Michael Paquier Reviewed-by: Amit Langote Discussion: https://postgr.es/m/20190322032612.GA323@alvherre.pgsql
1 parent fff518d commit 2ab6d28

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

src/backend/utils/adt/partitionfuncs.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,16 @@ pg_partition_root(PG_FUNCTION_ARGS)
189189
if (!check_rel_can_be_partition(relid))
190190
PG_RETURN_NULL();
191191

192-
/* Fetch the top-most parent */
192+
/* fetch the list of ancestors */
193193
ancestors = get_partition_ancestors(relid);
194+
195+
/*
196+
* If the input relation is already the top-most parent, just return
197+
* itself.
198+
*/
199+
if (ancestors == NIL)
200+
PG_RETURN_OID(relid);
201+
194202
rootrelid = llast_oid(ancestors);
195203
list_free(ancestors);
196204

src/test/regress/expected/partition_info.out

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,31 @@ CREATE TABLE ptif_test2 PARTITION OF ptif_test
4646
-- This partitioned table should remain with no partitions.
4747
CREATE TABLE ptif_test3 PARTITION OF ptif_test
4848
FOR VALUES FROM (200) TO (maxvalue) PARTITION BY list (b);
49+
-- Test pg_partition_root for tables
50+
SELECT pg_partition_root('ptif_test');
51+
pg_partition_root
52+
-------------------
53+
ptif_test
54+
(1 row)
55+
56+
SELECT pg_partition_root('ptif_test0');
57+
pg_partition_root
58+
-------------------
59+
ptif_test
60+
(1 row)
61+
62+
SELECT pg_partition_root('ptif_test01');
63+
pg_partition_root
64+
-------------------
65+
ptif_test
66+
(1 row)
67+
68+
SELECT pg_partition_root('ptif_test3');
69+
pg_partition_root
70+
-------------------
71+
ptif_test
72+
(1 row)
73+
4974
-- Test index partition tree
5075
CREATE INDEX ptif_test_index ON ONLY ptif_test (a);
5176
CREATE INDEX ptif_test0_index ON ONLY ptif_test0 (a);
@@ -60,6 +85,31 @@ CREATE INDEX ptif_test2_index ON ptif_test2 (a);
6085
ALTER INDEX ptif_test_index ATTACH PARTITION ptif_test2_index;
6186
CREATE INDEX ptif_test3_index ON ptif_test3 (a);
6287
ALTER INDEX ptif_test_index ATTACH PARTITION ptif_test3_index;
88+
-- Test pg_partition_root for indexes
89+
SELECT pg_partition_root('ptif_test_index');
90+
pg_partition_root
91+
-------------------
92+
ptif_test_index
93+
(1 row)
94+
95+
SELECT pg_partition_root('ptif_test0_index');
96+
pg_partition_root
97+
-------------------
98+
ptif_test_index
99+
(1 row)
100+
101+
SELECT pg_partition_root('ptif_test01_index');
102+
pg_partition_root
103+
-------------------
104+
ptif_test_index
105+
(1 row)
106+
107+
SELECT pg_partition_root('ptif_test3_index');
108+
pg_partition_root
109+
-------------------
110+
ptif_test_index
111+
(1 row)
112+
63113
-- List all tables members of the tree
64114
SELECT relid, parentrelid, level, isleaf
65115
FROM pg_partition_tree('ptif_test');

src/test/regress/sql/partition_info.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ CREATE TABLE ptif_test2 PARTITION OF ptif_test
2222
CREATE TABLE ptif_test3 PARTITION OF ptif_test
2323
FOR VALUES FROM (200) TO (maxvalue) PARTITION BY list (b);
2424

25+
-- Test pg_partition_root for tables
26+
SELECT pg_partition_root('ptif_test');
27+
SELECT pg_partition_root('ptif_test0');
28+
SELECT pg_partition_root('ptif_test01');
29+
SELECT pg_partition_root('ptif_test3');
30+
2531
-- Test index partition tree
2632
CREATE INDEX ptif_test_index ON ONLY ptif_test (a);
2733
CREATE INDEX ptif_test0_index ON ONLY ptif_test0 (a);
@@ -37,6 +43,12 @@ ALTER INDEX ptif_test_index ATTACH PARTITION ptif_test2_index;
3743
CREATE INDEX ptif_test3_index ON ptif_test3 (a);
3844
ALTER INDEX ptif_test_index ATTACH PARTITION ptif_test3_index;
3945

46+
-- Test pg_partition_root for indexes
47+
SELECT pg_partition_root('ptif_test_index');
48+
SELECT pg_partition_root('ptif_test0_index');
49+
SELECT pg_partition_root('ptif_test01_index');
50+
SELECT pg_partition_root('ptif_test3_index');
51+
4052
-- List all tables members of the tree
4153
SELECT relid, parentrelid, level, isleaf
4254
FROM pg_partition_tree('ptif_test');

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy