0002-Teach-expand_inherited_rtentry-to-use-partition-boun.patch

text/plain

Filename: 0002-Teach-expand_inherited_rtentry-to-use-partition-boun.patch
Type: text/plain
Part: 1
Message: Re: expanding inheritance in partition bound order

Patch

Format: format-patch
Series: patch 0002
Subject: Teach expand_inherited_rtentry to use partition bound order
File+
src/backend/optimizer/prep/prepunion.c 35 0
From b23ce2d7acbb89636c61b5e10c93211b052ef61a Mon Sep 17 00:00:00 2001
From: amit <amitlangote09@gmail.com>
Date: Wed, 9 Aug 2017 15:52:36 +0900
Subject: [PATCH 2/2] Teach expand_inherited_rtentry to use partition bound
 order

After locking the child tables using find_all_inheritors, we discard
the list of child table OIDs that it generates and rebuild the same
using the information returned by RelationGetPartitionDispatchInfo.
---
 src/backend/optimizer/prep/prepunion.c | 35 ++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/src/backend/optimizer/prep/prepunion.c b/src/backend/optimizer/prep/prepunion.c
index e73c819901..1ae1a851d4 100644
--- a/src/backend/optimizer/prep/prepunion.c
+++ b/src/backend/optimizer/prep/prepunion.c
@@ -33,6 +33,7 @@
 #include "access/heapam.h"
 #include "access/htup_details.h"
 #include "access/sysattr.h"
+#include "catalog/partition.h"
 #include "catalog/pg_inherits_fn.h"
 #include "catalog/pg_type.h"
 #include "miscadmin.h"
@@ -1452,6 +1453,40 @@ expand_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte, Index rti)
 	 */
 	oldrelation = heap_open(parentOID, NoLock);
 
+	/*
+	 * For partitioned tables, we arrange the child table OIDs such that they
+	 * appear in the partition bound order.
+	 */
+	if (rte->relkind == RELKIND_PARTITIONED_TABLE)
+	{
+		List   *leaf_part_oids,
+			   *pdlist;
+
+		/* Discard the original list. */
+		list_free(inhOIDs);
+		inhOIDs = NIL;
+
+		/* Request partitioning information. */
+		pdlist = RelationGetPartitionDispatchInfo(oldrelation,
+												  &leaf_part_oids);
+
+		/*
+		 * First collect the partitioned child table OIDs, which includes the
+		 * root parent at the head.
+		 */
+		foreach(l, pdlist)
+		{
+			PartitionDispatch	pd = lfirst(l);
+
+			inhOIDs = lappend_oid(inhOIDs, RelationGetRelid(pd->reldesc));
+			if (pd->reldesc != oldrelation)
+				heap_close(pd->reldesc, NoLock);
+		}
+
+		/* Concatenate the leaf partition OIDs. */
+		inhOIDs = list_concat(inhOIDs, leaf_part_oids);
+	}
+
 	/* Scan the inheritance set and expand it */
 	appinfos = NIL;
 	has_child = false;
-- 
2.11.0