From 6adb696b45bd2bc6814adf52851508706b27f0ad Mon Sep 17 00:00:00 2001
From: Robert Haas <rhaas@postgresql.org>
Date: Wed, 30 Aug 2017 12:53:43 -0400
Subject: [PATCH 1/2] expand_single_inheritance_child

---
 src/backend/optimizer/prep/prepunion.c | 225 +++++++++++++++++++--------------
 1 file changed, 127 insertions(+), 98 deletions(-)

diff --git a/src/backend/optimizer/prep/prepunion.c b/src/backend/optimizer/prep/prepunion.c
index e73c819901..870a4a6bfd 100644
--- a/src/backend/optimizer/prep/prepunion.c
+++ b/src/backend/optimizer/prep/prepunion.c
@@ -100,6 +100,12 @@ 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_single_inheritance_child(PlannerInfo *root,
+								RangeTblEntry *rte,
+								Index rti, Relation oldrelation,
+								PlanRowMark *oldrc, Relation newrelation,
+								bool *has_child, List **appinfos,
+								List **partitioned_child_rels);
 static void make_inh_translation_list(Relation oldrelation,
 						  Relation newrelation,
 						  Index newvarno,
@@ -1459,9 +1465,6 @@ expand_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte, Index rti)
 	{
 		Oid			childOID = lfirst_oid(l);
 		Relation	newrelation;
-		RangeTblEntry *childrte;
-		Index		childRTindex;
-		AppendRelInfo *appinfo;
 
 		/* Open rel if needed; we already have required locks */
 		if (childOID != parentOID)
@@ -1481,101 +1484,10 @@ expand_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte, Index rti)
 			continue;
 		}
 
-		/*
-		 * Build an RTE for the child, and attach to query's rangetable list.
-		 * We copy most fields of the parent's RTE, but replace relation OID
-		 * and relkind, and set inh = false.  Also, set requiredPerms to zero
-		 * since all required permissions checks are done on the original RTE.
-		 * Likewise, set the child's securityQuals to empty, because we only
-		 * want to apply the parent's RLS conditions regardless of what RLS
-		 * properties individual children may have.  (This is an intentional
-		 * choice to make inherited RLS work like regular permissions checks.)
-		 * The parent securityQuals will be propagated to children along with
-		 * other base restriction clauses, so we don't need to do it here.
-		 */
-		childrte = copyObject(rte);
-		childrte->relid = childOID;
-		childrte->relkind = newrelation->rd_rel->relkind;
-		childrte->inh = false;
-		childrte->requiredPerms = 0;
-		childrte->securityQuals = NIL;
-		parse->rtable = lappend(parse->rtable, childrte);
-		childRTindex = list_length(parse->rtable);
-
-		/*
-		 * Build an AppendRelInfo for this parent and child, unless the child
-		 * is a partitioned table.
-		 */
-		if (childrte->relkind != RELKIND_PARTITIONED_TABLE)
-		{
-			/* Remember if we saw a real child. */
-			if (childOID != parentOID)
-				has_child = true;
-
-			appinfo = makeNode(AppendRelInfo);
-			appinfo->parent_relid = rti;
-			appinfo->child_relid = childRTindex;
-			appinfo->parent_reltype = oldrelation->rd_rel->reltype;
-			appinfo->child_reltype = newrelation->rd_rel->reltype;
-			make_inh_translation_list(oldrelation, newrelation, childRTindex,
-									  &appinfo->translated_vars);
-			appinfo->parent_reloid = parentOID;
-			appinfos = lappend(appinfos, appinfo);
-
-			/*
-			 * Translate the column permissions bitmaps to the child's attnums
-			 * (we have to build the translated_vars list before we can do
-			 * this). But if this is the parent table, leave copyObject's
-			 * result alone.
-			 *
-			 * Note: we need to do this even though the executor won't run any
-			 * permissions checks on the child RTE.  The
-			 * insertedCols/updatedCols bitmaps may be examined for
-			 * trigger-firing purposes.
-			 */
-			if (childOID != parentOID)
-			{
-				childrte->selectedCols = translate_col_privs(rte->selectedCols,
-															 appinfo->translated_vars);
-				childrte->insertedCols = translate_col_privs(rte->insertedCols,
-															 appinfo->translated_vars);
-				childrte->updatedCols = translate_col_privs(rte->updatedCols,
-															appinfo->translated_vars);
-			}
-		}
-		else
-			partitioned_child_rels = lappend_int(partitioned_child_rels,
-												 childRTindex);
-
-		/*
-		 * Build a PlanRowMark if parent is marked FOR UPDATE/SHARE.
-		 */
-		if (oldrc)
-		{
-			PlanRowMark *newrc = makeNode(PlanRowMark);
-
-			newrc->rti = childRTindex;
-			newrc->prti = rti;
-			newrc->rowmarkId = oldrc->rowmarkId;
-			/* Reselect rowmark type, because relkind might not match parent */
-			newrc->markType = select_rowmark_type(childrte, oldrc->strength);
-			newrc->allMarkTypes = (1 << newrc->markType);
-			newrc->strength = oldrc->strength;
-			newrc->waitPolicy = oldrc->waitPolicy;
-
-			/*
-			 * We mark RowMarks for partitioned child tables as parent
-			 * RowMarks so that the executor ignores them (except their
-			 * existence means that the child tables be locked using
-			 * appropriate mode).
-			 */
-			newrc->isParent = (childrte->relkind == RELKIND_PARTITIONED_TABLE);
-
-			/* Include child's rowmark type in parent's allMarkTypes */
-			oldrc->allMarkTypes |= newrc->allMarkTypes;
-
-			root->rowMarks = lappend(root->rowMarks, newrc);
-		}
+		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)
@@ -1621,6 +1533,123 @@ expand_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte, Index rti)
 }
 
 /*
+ * expand_single_inheritance_child
+ *		Expand a single inheritance child, if needed.
+ *
+ * If this is a temp table of another backend, we'll return without doing
+ * anything at all.  Otherwise, we'll build a RangeTblEntry and either a
+ * PartitionedChildRelInfo or AppendRelInfo as appropriate, plus maybe a
+ * PlanRowMark.
+ */
+static void
+expand_single_inheritance_child(PlannerInfo *root, RangeTblEntry *rte,
+								Index rti, Relation oldrelation,
+								PlanRowMark *oldrc, Relation newrelation,
+								bool *has_child, List **appinfos,
+								List **partitioned_child_rels)
+{
+	Query	   *parse = root->parse;
+	Oid			parentOID = RelationGetRelid(oldrelation);
+	Oid			childOID = RelationGetRelid(newrelation);
+	RangeTblEntry *childrte;
+	Index		childRTindex;
+	AppendRelInfo *appinfo;
+
+	/*
+	 * Build an RTE for the child, and attach to query's rangetable list. We
+	 * copy most fields of the parent's RTE, but replace relation OID and
+	 * relkind, and set inh = false.  Also, set requiredPerms to zero since
+	 * all required permissions checks are done on the original RTE. Likewise,
+	 * set the child's securityQuals to empty, because we only want to apply
+	 * the parent's RLS conditions regardless of what RLS properties
+	 * individual children may have.  (This is an intentional choice to make
+	 * inherited RLS work like regular permissions checks.) The parent
+	 * securityQuals will be propagated to children along with other base
+	 * restriction clauses, so we don't need to do it here.
+	 */
+	childrte = copyObject(rte);
+	childrte->relid = childOID;
+	childrte->relkind = newrelation->rd_rel->relkind;
+	childrte->inh = false;
+	childrte->requiredPerms = 0;
+	childrte->securityQuals = NIL;
+	parse->rtable = lappend(parse->rtable, childrte);
+	childRTindex = list_length(parse->rtable);
+
+	/*
+	 * Build an AppendRelInfo for this parent and child, unless the child is a
+	 * partitioned table.
+	 */
+	if (childrte->relkind != RELKIND_PARTITIONED_TABLE)
+	{
+		/* Remember if we saw a real child. */
+		if (childOID != parentOID)
+			*has_child = true;
+
+		appinfo = makeNode(AppendRelInfo);
+		appinfo->parent_relid = rti;
+		appinfo->child_relid = childRTindex;
+		appinfo->parent_reltype = oldrelation->rd_rel->reltype;
+		appinfo->child_reltype = newrelation->rd_rel->reltype;
+		make_inh_translation_list(oldrelation, newrelation, childRTindex,
+								  &appinfo->translated_vars);
+		appinfo->parent_reloid = parentOID;
+		*appinfos = lappend(*appinfos, appinfo);
+
+		/*
+		 * Translate the column permissions bitmaps to the child's attnums (we
+		 * have to build the translated_vars list before we can do this). But
+		 * if this is the parent table, leave copyObject's result alone.
+		 *
+		 * Note: we need to do this even though the executor won't run any
+		 * permissions checks on the child RTE.  The insertedCols/updatedCols
+		 * bitmaps may be examined for trigger-firing purposes.
+		 */
+		if (childOID != parentOID)
+		{
+			childrte->selectedCols = translate_col_privs(rte->selectedCols,
+														 appinfo->translated_vars);
+			childrte->insertedCols = translate_col_privs(rte->insertedCols,
+														 appinfo->translated_vars);
+			childrte->updatedCols = translate_col_privs(rte->updatedCols,
+														appinfo->translated_vars);
+		}
+	}
+	else
+		*partitioned_child_rels = lappend_int(*partitioned_child_rels,
+											  childRTindex);
+
+	/*
+	 * Build a PlanRowMark if parent is marked FOR UPDATE/SHARE.
+	 */
+	if (oldrc)
+	{
+		PlanRowMark *newrc = makeNode(PlanRowMark);
+
+		newrc->rti = childRTindex;
+		newrc->prti = rti;
+		newrc->rowmarkId = oldrc->rowmarkId;
+		/* Reselect rowmark type, because relkind might not match parent */
+		newrc->markType = select_rowmark_type(childrte, oldrc->strength);
+		newrc->allMarkTypes = (1 << newrc->markType);
+		newrc->strength = oldrc->strength;
+		newrc->waitPolicy = oldrc->waitPolicy;
+
+		/*
+		 * We mark RowMarks for partitioned child tables as parent RowMarks so
+		 * that the executor ignores them (except their existence means that
+		 * the child tables be locked using appropriate mode).
+		 */
+		newrc->isParent = (childrte->relkind == RELKIND_PARTITIONED_TABLE);
+
+		/* Include child's rowmark type in parent's allMarkTypes */
+		oldrc->allMarkTypes |= newrc->allMarkTypes;
+
+		root->rowMarks = lappend(root->rowMarks, newrc);
+	}
+}
+
+/*
  * make_inh_translation_list
  *	  Build the list of translations from parent Vars to child Vars for
  *	  an inheritance child.
-- 
2.11.0 (Apple Git-81)

