Commit 2ab6d28d authored by Michael Paquier's avatar Michael Paquier

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
parent fff518d0
...@@ -189,8 +189,16 @@ pg_partition_root(PG_FUNCTION_ARGS) ...@@ -189,8 +189,16 @@ pg_partition_root(PG_FUNCTION_ARGS)
if (!check_rel_can_be_partition(relid)) if (!check_rel_can_be_partition(relid))
PG_RETURN_NULL(); PG_RETURN_NULL();
/* Fetch the top-most parent */ /* fetch the list of ancestors */
ancestors = get_partition_ancestors(relid); ancestors = get_partition_ancestors(relid);
/*
* If the input relation is already the top-most parent, just return
* itself.
*/
if (ancestors == NIL)
PG_RETURN_OID(relid);
rootrelid = llast_oid(ancestors); rootrelid = llast_oid(ancestors);
list_free(ancestors); list_free(ancestors);
......
...@@ -46,6 +46,31 @@ CREATE TABLE ptif_test2 PARTITION OF ptif_test ...@@ -46,6 +46,31 @@ CREATE TABLE ptif_test2 PARTITION OF ptif_test
-- This partitioned table should remain with no partitions. -- This partitioned table should remain with no partitions.
CREATE TABLE ptif_test3 PARTITION OF ptif_test CREATE TABLE ptif_test3 PARTITION OF ptif_test
FOR VALUES FROM (200) TO (maxvalue) PARTITION BY list (b); FOR VALUES FROM (200) TO (maxvalue) PARTITION BY list (b);
-- Test pg_partition_root for tables
SELECT pg_partition_root('ptif_test');
pg_partition_root
-------------------
ptif_test
(1 row)
SELECT pg_partition_root('ptif_test0');
pg_partition_root
-------------------
ptif_test
(1 row)
SELECT pg_partition_root('ptif_test01');
pg_partition_root
-------------------
ptif_test
(1 row)
SELECT pg_partition_root('ptif_test3');
pg_partition_root
-------------------
ptif_test
(1 row)
-- Test index partition tree -- Test index partition tree
CREATE INDEX ptif_test_index ON ONLY ptif_test (a); CREATE INDEX ptif_test_index ON ONLY ptif_test (a);
CREATE INDEX ptif_test0_index ON ONLY ptif_test0 (a); CREATE INDEX ptif_test0_index ON ONLY ptif_test0 (a);
...@@ -60,6 +85,31 @@ CREATE INDEX ptif_test2_index ON ptif_test2 (a); ...@@ -60,6 +85,31 @@ CREATE INDEX ptif_test2_index ON ptif_test2 (a);
ALTER INDEX ptif_test_index ATTACH PARTITION ptif_test2_index; ALTER INDEX ptif_test_index ATTACH PARTITION ptif_test2_index;
CREATE INDEX ptif_test3_index ON ptif_test3 (a); CREATE INDEX ptif_test3_index ON ptif_test3 (a);
ALTER INDEX ptif_test_index ATTACH PARTITION ptif_test3_index; ALTER INDEX ptif_test_index ATTACH PARTITION ptif_test3_index;
-- Test pg_partition_root for indexes
SELECT pg_partition_root('ptif_test_index');
pg_partition_root
-------------------
ptif_test_index
(1 row)
SELECT pg_partition_root('ptif_test0_index');
pg_partition_root
-------------------
ptif_test_index
(1 row)
SELECT pg_partition_root('ptif_test01_index');
pg_partition_root
-------------------
ptif_test_index
(1 row)
SELECT pg_partition_root('ptif_test3_index');
pg_partition_root
-------------------
ptif_test_index
(1 row)
-- List all tables members of the tree -- List all tables members of the tree
SELECT relid, parentrelid, level, isleaf SELECT relid, parentrelid, level, isleaf
FROM pg_partition_tree('ptif_test'); FROM pg_partition_tree('ptif_test');
......
...@@ -22,6 +22,12 @@ CREATE TABLE ptif_test2 PARTITION OF ptif_test ...@@ -22,6 +22,12 @@ CREATE TABLE ptif_test2 PARTITION OF ptif_test
CREATE TABLE ptif_test3 PARTITION OF ptif_test CREATE TABLE ptif_test3 PARTITION OF ptif_test
FOR VALUES FROM (200) TO (maxvalue) PARTITION BY list (b); FOR VALUES FROM (200) TO (maxvalue) PARTITION BY list (b);
-- Test pg_partition_root for tables
SELECT pg_partition_root('ptif_test');
SELECT pg_partition_root('ptif_test0');
SELECT pg_partition_root('ptif_test01');
SELECT pg_partition_root('ptif_test3');
-- Test index partition tree -- Test index partition tree
CREATE INDEX ptif_test_index ON ONLY ptif_test (a); CREATE INDEX ptif_test_index ON ONLY ptif_test (a);
CREATE INDEX ptif_test0_index ON ONLY ptif_test0 (a); CREATE INDEX ptif_test0_index ON ONLY ptif_test0 (a);
...@@ -37,6 +43,12 @@ ALTER INDEX ptif_test_index ATTACH PARTITION ptif_test2_index; ...@@ -37,6 +43,12 @@ ALTER INDEX ptif_test_index ATTACH PARTITION ptif_test2_index;
CREATE INDEX ptif_test3_index ON ptif_test3 (a); CREATE INDEX ptif_test3_index ON ptif_test3 (a);
ALTER INDEX ptif_test_index ATTACH PARTITION ptif_test3_index; ALTER INDEX ptif_test_index ATTACH PARTITION ptif_test3_index;
-- Test pg_partition_root for indexes
SELECT pg_partition_root('ptif_test_index');
SELECT pg_partition_root('ptif_test0_index');
SELECT pg_partition_root('ptif_test01_index');
SELECT pg_partition_root('ptif_test3_index');
-- List all tables members of the tree -- List all tables members of the tree
SELECT relid, parentrelid, level, isleaf SELECT relid, parentrelid, level, isleaf
FROM pg_partition_tree('ptif_test'); FROM pg_partition_tree('ptif_test');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment