From faac5b1261497c5cceb246bc7d52cdaff2ac5057 Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <ashutosh.bapat@enterprisedb.com>
Date: Thu, 31 Aug 2017 11:40:11 +0530
Subject: [PATCH 2/2] EIBO patch from Robert

with

my changes to rename arguements to and variables in
expand_partitions_recursively(). Also rename expand_partitions_recursively() to
expand_partitioned_rtentry() inline with expand_inherited_rtentry().
---
 src/backend/optimizer/prep/prepunion.c |  127 ++++++++++++++++++++++++++------
 src/test/regress/expected/insert.out   |    4 +-
 2 files changed, 105 insertions(+), 26 deletions(-)

diff --git a/src/backend/optimizer/prep/prepunion.c b/src/backend/optimizer/prep/prepunion.c
index bb8f1ce..ccf2145 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"
@@ -100,6 +101,13 @@ static List *generate_append_tlist(List *colTypes, List *colCollations,
 static List *generate_setop_grouplist(SetOperationStmt *op, List *targetlist);
 static void expand_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte,
 						 Index rti);
+static void expand_partitioned_rtentry(PlannerInfo *root,
+						   RangeTblEntry *parentrte,
+						   Index parentRTindex, Relation parentrel,
+						   PlanRowMark *parentrc, PartitionDesc partdesc,
+						   LOCKMODE lockmode,
+						   bool *has_child, List **appinfos,
+						   List **partitioned_child_rels);
 static void expand_single_inheritance_child(PlannerInfo *root,
 								RangeTblEntry *parentrte,
 								Index parentRTindex, Relation parentrel,
@@ -1461,37 +1469,62 @@ expand_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte, Index rti)
 	/* Scan the inheritance set and expand it */
 	appinfos = NIL;
 	has_child = false;
-	foreach(l, inhOIDs)
+	if (RelationGetPartitionDesc(oldrelation) != NULL)
 	{
-		Oid			childOID = lfirst_oid(l);
-		Relation	newrelation;
-
-		/* Open rel if needed; we already have required locks */
-		if (childOID != parentOID)
-			newrelation = heap_open(childOID, NoLock);
-		else
-			newrelation = oldrelation;
-
 		/*
-		 * It is possible that the parent table has children that are temp
-		 * tables of other backends.  We cannot safely access such tables
-		 * (because of buffering issues), and the best thing to do seems to be
-		 * to silently ignore them.
+		 * If this table has partitions, recursively expand them in the order
+		 * in which they appear in the PartitionDesc.  But first, expand the
+		 * parent itself.
 		 */
-		if (childOID != parentOID && RELATION_IS_OTHER_TEMP(newrelation))
-		{
-			heap_close(newrelation, lockmode);
-			continue;
-		}
-
 		expand_single_inheritance_child(root, rte, rti, oldrelation, oldrc,
-										newrelation,
+										oldrelation,
 										&has_child, &appinfos,
 										&partitioned_child_rels);
+		expand_partitioned_rtentry(root, rte, rti, oldrelation, oldrc,
+									  RelationGetPartitionDesc(oldrelation),
+									  lockmode,
+									  &has_child, &appinfos,
+									  &partitioned_child_rels);
+	}
+	else
+	{
+		/*
+		 * This table has no partitions.  Expand any plain inheritance
+		 * children in the order the OIDs were returned by
+		 * find_all_inheritors.
+		 */
+		foreach(l, inhOIDs)
+		{
+			Oid			childOID = lfirst_oid(l);
+			Relation	newrelation;
 
-		/* Close child relations, but keep locks */
-		if (childOID != parentOID)
-			heap_close(newrelation, NoLock);
+			/* Open rel if needed; we already have required locks */
+			if (childOID != parentOID)
+				newrelation = heap_open(childOID, NoLock);
+			else
+				newrelation = oldrelation;
+
+			/*
+			 * It is possible that the parent table has children that are temp
+			 * tables of other backends.  We cannot safely access such tables
+			 * (because of buffering issues), and the best thing to do seems
+			 * to be to silently ignore them.
+			 */
+			if (childOID != parentOID && RELATION_IS_OTHER_TEMP(newrelation))
+			{
+				heap_close(newrelation, lockmode);
+				continue;
+			}
+
+			expand_single_inheritance_child(root, rte, rti, oldrelation, oldrc,
+											newrelation,
+											&has_child, &appinfos,
+											&partitioned_child_rels);
+
+			/* Close child relations, but keep locks */
+			if (childOID != parentOID)
+				heap_close(newrelation, NoLock);
+		}
 	}
 
 	heap_close(oldrelation, NoLock);
@@ -1532,6 +1565,52 @@ expand_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte, Index rti)
 	root->append_rel_list = list_concat(root->append_rel_list, appinfos);
 }
 
+static void
+expand_partitioned_rtentry(PlannerInfo *root, RangeTblEntry *parentrte,
+						   Index parentRTindex, Relation parentrel,
+						   PlanRowMark *parentrc, PartitionDesc partdesc,
+						   LOCKMODE lockmode,
+						   bool *has_child, List **appinfos,
+						   List **partitioned_child_rels)
+{
+	int			i;
+
+	check_stack_depth();
+
+	for (i = 0; i < partdesc->nparts; i++)
+	{
+		Oid			childOID = partdesc->oids[i];
+		Relation	childrel;
+
+		/* Open rel; we already have required locks */
+		childrel = heap_open(childOID, NoLock);
+
+		/* As in expand_inherited_rtentry, skip non-local temp tables */
+		if (RELATION_IS_OTHER_TEMP(childrel))
+		{
+			heap_close(childrel, lockmode);
+			continue;
+		}
+
+		expand_single_inheritance_child(root, parentrte, parentRTindex,
+										parentrel, parentrc, childrel,
+										has_child, appinfos,
+										partitioned_child_rels);
+
+		/* If this child is itself partitioned, recurse */
+		if (childrel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
+			expand_partitioned_rtentry(root, parentrte, parentRTindex,
+										  parentrel, parentrc,
+										  RelationGetPartitionDesc(childrel),
+										  lockmode,
+										  has_child, appinfos,
+										  partitioned_child_rels);
+
+		/* Close child relation, but keep locks */
+		heap_close(childrel, NoLock);
+	}
+}
+
 /*
  * expand_single_inheritance_child
  *		Expand a single inheritance child, if needed.
diff --git a/src/test/regress/expected/insert.out b/src/test/regress/expected/insert.out
index a2d9469..e159d62 100644
--- a/src/test/regress/expected/insert.out
+++ b/src/test/regress/expected/insert.out
@@ -278,12 +278,12 @@ select tableoid::regclass, * from list_parted;
 -------------+----+----
  part_aa_bb  | aA |   
  part_cc_dd  | cC |  1
- part_null   |    |  0
- part_null   |    |  1
  part_ee_ff1 | ff |  1
  part_ee_ff1 | EE |  1
  part_ee_ff2 | ff | 11
  part_ee_ff2 | EE | 10
+ part_null   |    |  0
+ part_null   |    |  1
 (8 rows)
 
 -- some more tests to exercise tuple-routing with multi-level partitioning
-- 
1.7.9.5

