pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Paquier <michael@paquier.xyz>
To: Amit Langote <Langote_Amit_f8@lab.ntt.co.jp>
Cc: Alvaro Herrera <alvherre@2ndquadrant.com>
Cc: pgsql-committers@lists.postgresql.org
Subject: Re: pgsql: Add pg_partition_root to display top-most parent of a partition
Date: Fri, 22 Mar 2019 14:13:49 +0900
Message-ID: <20190322051349.GU20192@paquier.xyz> (raw)
In-Reply-To: <27e4501a-ae56-94e4-1524-d509f048d61c@lab.ntt.co.jp>
References: <E1grthW-00078n-Az@gemulon.postgresql.org>
	<20190322032612.GA323@alvherre.pgsql>
	<20190322035538.GO20192@paquier.xyz>
	<06be11a6-fd8d-b68b-1b97-1bafb1d3185c@lab.ntt.co.jp>
	<20190322041225.GS20192@paquier.xyz>
	<27e4501a-ae56-94e4-1524-d509f048d61c@lab.ntt.co.jp>

On Fri, Mar 22, 2019 at 01:28:19PM +0900, Amit Langote wrote:
> It looked fine before, but the new lines added by patch makes it look
> wrong/misplaced somehow.

Okay, what do you think about the attached then?
--
Michael

Attachments:

  [text/x-diff] partition-root-fix-v2.patch (3.8K, ../20190322051349.GU20192@paquier.xyz/2-partition-root-fix-v2.patch)
  download | inline diff:
diff --git a/src/backend/utils/adt/partitionfuncs.c b/src/backend/utils/adt/partitionfuncs.c
index 98c8ef77f1..17211888dd 100644
--- a/src/backend/utils/adt/partitionfuncs.c
+++ b/src/backend/utils/adt/partitionfuncs.c
@@ -189,8 +189,16 @@ pg_partition_root(PG_FUNCTION_ARGS)
 	if (!check_rel_can_be_partition(relid))
 		PG_RETURN_NULL();
 
-	/* Fetch the top-most parent */
+	/* fetch the list of ancestors */
 	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);
 	list_free(ancestors);
 
diff --git a/src/test/regress/expected/partition_info.out b/src/test/regress/expected/partition_info.out
index 92aa6abe3f..42b6bc77ca 100644
--- a/src/test/regress/expected/partition_info.out
+++ b/src/test/regress/expected/partition_info.out
@@ -46,6 +46,31 @@ CREATE TABLE ptif_test2 PARTITION OF ptif_test
 -- This partitioned table should remain with no partitions.
 CREATE TABLE ptif_test3 PARTITION OF ptif_test
   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
 CREATE INDEX ptif_test_index ON ONLY ptif_test (a);
 CREATE INDEX ptif_test0_index ON ONLY ptif_test0 (a);
@@ -60,6 +85,31 @@ CREATE INDEX ptif_test2_index ON ptif_test2 (a);
 ALTER INDEX ptif_test_index ATTACH PARTITION ptif_test2_index;
 CREATE INDEX ptif_test3_index ON ptif_test3 (a);
 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
 SELECT relid, parentrelid, level, isleaf
   FROM pg_partition_tree('ptif_test');
diff --git a/src/test/regress/sql/partition_info.sql b/src/test/regress/sql/partition_info.sql
index adaca23232..b5060bec7f 100644
--- a/src/test/regress/sql/partition_info.sql
+++ b/src/test/regress/sql/partition_info.sql
@@ -22,6 +22,12 @@ CREATE TABLE ptif_test2 PARTITION OF ptif_test
 CREATE TABLE ptif_test3 PARTITION OF ptif_test
   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
 CREATE INDEX ptif_test_index ON ONLY ptif_test (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;
 CREATE INDEX ptif_test3_index ON ptif_test3 (a);
 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
 SELECT relid, parentrelid, level, isleaf
   FROM pg_partition_tree('ptif_test');

  [application/pgp-signature] signature.asc (832B, ../20190322051349.GU20192@paquier.xyz/3-signature.asc)
  download

view thread (10+ messages)  latest in thread

Message-ID: <20190322051349.GU20192@paquier.xyz>
Permalink:  ../20190322051349.GU20192@paquier.xyz/
Also on:    postgresql.org/message-id/20190322051349.GU20192@paquier.xyz

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: michael@paquier.xyz, Langote_Amit_f8@lab.ntt.co.jp, alvherre@2ndquadrant.com, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Add pg_partition_root to display top-most parent of a partition
  In-Reply-To: <20190322051349.GU20192@paquier.xyz>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox