From ddccecf06b38ba950850c4528aa3d37fcf2b860f Mon Sep 17 00:00:00 2001
From: amitlan <amitlangote09@gmail.com>
Date: Wed, 7 Oct 2020 17:34:49 +0900
Subject: [PATCH v12 1/5] Refactor set_plan_refs()

Pass the information needed by set_plan_refs() in a "context" struct
instead of directly as arguments.  This will allow to expand the set
of information that can be passed without much code churn.
---
 src/backend/optimizer/plan/setrefs.c | 83 ++++++++++++++++++------------------
 1 file changed, 41 insertions(+), 42 deletions(-)

diff --git a/src/backend/optimizer/plan/setrefs.c b/src/backend/optimizer/plan/setrefs.c
index dd8e2e9..e647f2d 100644
--- a/src/backend/optimizer/plan/setrefs.c
+++ b/src/backend/optimizer/plan/setrefs.c
@@ -71,6 +71,12 @@ typedef struct
 	double		num_exec;
 } fix_upper_expr_context;
 
+typedef struct
+{
+	PlannerInfo	   *root;
+	int				rtoffset;	/* offset in root->glob->finalrtable */
+} set_plan_refs_context;
+
 /*
  * Selecting the best alternative in an AlternativeSubPlan expression requires
  * estimating how many times that expression will be evaluated.  For an
@@ -108,7 +114,7 @@ static void add_rtes_to_flat_rtable(PlannerInfo *root, bool recursing);
 static void flatten_unplanned_rtes(PlannerGlobal *glob, RangeTblEntry *rte);
 static bool flatten_rtes_walker(Node *node, PlannerGlobal *glob);
 static void add_rte_to_flat_rtable(PlannerGlobal *glob, RangeTblEntry *rte);
-static Plan *set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset);
+static Plan *set_plan_refs(Plan *plan, set_plan_refs_context *context);
 static Plan *set_indexonlyscan_references(PlannerInfo *root,
 										  IndexOnlyScan *plan,
 										  int rtoffset);
@@ -120,15 +126,12 @@ static Plan *clean_up_removed_plan_level(Plan *parent, Plan *child);
 static void set_foreignscan_references(PlannerInfo *root,
 									   ForeignScan *fscan,
 									   int rtoffset);
-static void set_customscan_references(PlannerInfo *root,
-									  CustomScan *cscan,
-									  int rtoffset);
-static Plan *set_append_references(PlannerInfo *root,
-								   Append *aplan,
-								   int rtoffset);
-static Plan *set_mergeappend_references(PlannerInfo *root,
-										MergeAppend *mplan,
-										int rtoffset);
+static void set_customscan_references(CustomScan *cscan,
+									  set_plan_refs_context *context);
+static Plan *set_append_references(Append *aplan,
+								   set_plan_refs_context *context);
+static Plan *set_mergeappend_references(MergeAppend *mplan,
+										set_plan_refs_context *context);
 static void set_hash_references(PlannerInfo *root, Plan *plan, int rtoffset);
 static Relids offset_relid_set(Relids relids, int rtoffset);
 static Node *fix_scan_expr(PlannerInfo *root, Node *node,
@@ -252,6 +255,7 @@ set_plan_references(PlannerInfo *root, Plan *plan)
 	PlannerGlobal *glob = root->glob;
 	int			rtoffset = list_length(glob->finalrtable);
 	ListCell   *lc;
+	set_plan_refs_context	context;
 
 	/*
 	 * Add all the query's RTEs to the flattened rangetable.  The live ones
@@ -302,7 +306,9 @@ set_plan_references(PlannerInfo *root, Plan *plan)
 	}
 
 	/* Now fix the Plan tree */
-	return set_plan_refs(root, plan, rtoffset);
+	context.root = root;
+	context.rtoffset = rtoffset;
+	return set_plan_refs(plan, &context);
 }
 
 /*
@@ -496,8 +502,10 @@ add_rte_to_flat_rtable(PlannerGlobal *glob, RangeTblEntry *rte)
  * set_plan_refs: recurse through the Plan nodes of a single subquery level
  */
 static Plan *
-set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
+set_plan_refs(Plan *plan, set_plan_refs_context *context)
 {
+	PlannerInfo *root = context->root;
+	int			rtoffset = context->rtoffset;
 	ListCell   *l;
 
 	if (plan == NULL)
@@ -714,7 +722,7 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
 			set_foreignscan_references(root, (ForeignScan *) plan, rtoffset);
 			break;
 		case T_CustomScan:
-			set_customscan_references(root, (CustomScan *) plan, rtoffset);
+			set_customscan_references((CustomScan *) plan, context);
 			break;
 
 		case T_NestLoop:
@@ -968,9 +976,7 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
 				}
 				foreach(l, splan->plans)
 				{
-					lfirst(l) = set_plan_refs(root,
-											  (Plan *) lfirst(l),
-											  rtoffset);
+					lfirst(l) = set_plan_refs((Plan *) lfirst(l), context);
 				}
 
 				/*
@@ -1001,14 +1007,10 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
 			break;
 		case T_Append:
 			/* Needs special treatment, see comments below */
-			return set_append_references(root,
-										 (Append *) plan,
-										 rtoffset);
+			return set_append_references((Append *) plan, context);
 		case T_MergeAppend:
 			/* Needs special treatment, see comments below */
-			return set_mergeappend_references(root,
-											  (MergeAppend *) plan,
-											  rtoffset);
+			return set_mergeappend_references((MergeAppend *) plan, context);
 		case T_RecursiveUnion:
 			/* This doesn't evaluate targetlist or check quals either */
 			set_dummy_tlist_references(plan, rtoffset);
@@ -1023,9 +1025,7 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
 				Assert(splan->plan.qual == NIL);
 				foreach(l, splan->bitmapplans)
 				{
-					lfirst(l) = set_plan_refs(root,
-											  (Plan *) lfirst(l),
-											  rtoffset);
+					lfirst(l) = set_plan_refs((Plan *) lfirst(l), context);
 				}
 			}
 			break;
@@ -1038,9 +1038,7 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
 				Assert(splan->plan.qual == NIL);
 				foreach(l, splan->bitmapplans)
 				{
-					lfirst(l) = set_plan_refs(root,
-											  (Plan *) lfirst(l),
-											  rtoffset);
+					lfirst(l) = set_plan_refs((Plan *) lfirst(l), context);
 				}
 			}
 			break;
@@ -1058,8 +1056,8 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
 	 * reference-adjustments bottom-up, then we would fail to match this
 	 * plan's var nodes against the already-modified nodes of the children.
 	 */
-	plan->lefttree = set_plan_refs(root, plan->lefttree, rtoffset);
-	plan->righttree = set_plan_refs(root, plan->righttree, rtoffset);
+	plan->lefttree = set_plan_refs(plan->lefttree, context);
+	plan->righttree = set_plan_refs(plan->righttree, context);
 
 	return plan;
 }
@@ -1328,10 +1326,11 @@ set_foreignscan_references(PlannerInfo *root,
  *	   Do set_plan_references processing on a CustomScan
  */
 static void
-set_customscan_references(PlannerInfo *root,
-						  CustomScan *cscan,
-						  int rtoffset)
+set_customscan_references(CustomScan *cscan,
+						  set_plan_refs_context *context)
 {
+	PlannerInfo *root = context->root;
+	int			rtoffset = context->rtoffset;
 	ListCell   *lc;
 
 	/* Adjust scanrelid if it's valid */
@@ -1387,7 +1386,7 @@ set_customscan_references(PlannerInfo *root,
 	/* Adjust child plan-nodes recursively, if needed */
 	foreach(lc, cscan->custom_plans)
 	{
-		lfirst(lc) = set_plan_refs(root, (Plan *) lfirst(lc), rtoffset);
+		lfirst(lc) = set_plan_refs((Plan *) lfirst(lc), context);
 	}
 
 	cscan->custom_relids = offset_relid_set(cscan->custom_relids, rtoffset);
@@ -1401,10 +1400,10 @@ set_customscan_references(PlannerInfo *root,
  * to do the normal processing on it.
  */
 static Plan *
-set_append_references(PlannerInfo *root,
-					  Append *aplan,
-					  int rtoffset)
+set_append_references(Append *aplan,
+					  set_plan_refs_context *context)
 {
+	int			rtoffset = context->rtoffset;
 	ListCell   *l;
 
 	/*
@@ -1417,7 +1416,7 @@ set_append_references(PlannerInfo *root,
 	/* First, we gotta recurse on the children */
 	foreach(l, aplan->appendplans)
 	{
-		lfirst(l) = set_plan_refs(root, (Plan *) lfirst(l), rtoffset);
+		lfirst(l) = set_plan_refs((Plan *) lfirst(l), context);
 	}
 
 	/* Now, if there's just one, forget the Append and return that child */
@@ -1465,10 +1464,10 @@ set_append_references(PlannerInfo *root,
  * to do the normal processing on it.
  */
 static Plan *
-set_mergeappend_references(PlannerInfo *root,
-						   MergeAppend *mplan,
-						   int rtoffset)
+set_mergeappend_references(MergeAppend *mplan,
+						   set_plan_refs_context *context)
 {
+	int			rtoffset = context->rtoffset;
 	ListCell   *l;
 
 	/*
@@ -1481,7 +1480,7 @@ set_mergeappend_references(PlannerInfo *root,
 	/* First, we gotta recurse on the children */
 	foreach(l, mplan->mergeplans)
 	{
-		lfirst(l) = set_plan_refs(root, (Plan *) lfirst(l), rtoffset);
+		lfirst(l) = set_plan_refs((Plan *) lfirst(l), context);
 	}
 
 	/* Now, if there's just one, forget the MergeAppend and return that child */
-- 
1.8.3.1

