#include "executor/nodeAgg.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
+#ifdef OPTIMIZER_DEBUG
+#include "nodes/print.h"
+#endif
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
#include "optimizer/pathnode.h"
#include "optimizer/prep.h"
#include "optimizer/subselect.h"
#include "optimizer/tlist.h"
-#ifdef OPTIMIZER_DEBUG
-#include "nodes/print.h"
-#endif
#include "parser/analyze.h"
#include "parser/parsetree.h"
+#include "rewrite/rewriteManip.h"
#include "utils/rel.h"
PlannerInfo *root;
Plan *top_plan;
ListCell *lp,
- *lrt,
- *lrm;
+ *lr;
/* Cursor options may come from caller or from DECLARE CURSOR stmt */
if (parse->utilityStmt &&
glob->boundParams = boundParams;
glob->paramlist = NIL;
glob->subplans = NIL;
- glob->subrtables = NIL;
- glob->subrowmarks = NIL;
+ glob->subroots = NIL;
glob->rewindPlanIDs = NULL;
glob->finalrtable = NIL;
glob->finalrowmarks = NIL;
Assert(glob->finalrtable == NIL);
Assert(glob->finalrowmarks == NIL);
Assert(glob->resultRelations == NIL);
- top_plan = set_plan_references(glob, top_plan,
- root->parse->rtable,
- root->rowMarks);
+ top_plan = set_plan_references(root, top_plan);
/* ... and the subplans (both regular subplans and initplans) */
- Assert(list_length(glob->subplans) == list_length(glob->subrtables));
- Assert(list_length(glob->subplans) == list_length(glob->subrowmarks));
- lrt = list_head(glob->subrtables);
- lrm = list_head(glob->subrowmarks);
- foreach(lp, glob->subplans)
+ Assert(list_length(glob->subplans) == list_length(glob->subroots));
+ forboth(lp, glob->subplans, lr, glob->subroots)
{
Plan *subplan = (Plan *) lfirst(lp);
- List *subrtable = (List *) lfirst(lrt);
- List *subrowmark = (List *) lfirst(lrm);
+ PlannerInfo *subroot = (PlannerInfo *) lfirst(lr);
- lfirst(lp) = set_plan_references(glob, subplan,
- subrtable, subrowmark);
- lrt = lnext(lrt);
- lrm = lnext(lrm);
+ lfirst(lp) = set_plan_references(subroot, subplan);
}
/* build the PlannedStmt result */
List *rlist;
Assert(parse->resultRelation);
- rlist = set_returning_clause_references(root->glob,
+ rlist = set_returning_clause_references(root,
parse->returningList,
plan,
parse->resultRelation);
{
Query *parse = root->parse;
int parentRTindex = parse->resultRelation;
+ List *final_rtable = NIL;
+ int save_rel_array_size = 0;
+ RelOptInfo **save_rel_array = NULL;
List *subplans = NIL;
List *resultRelations = NIL;
List *returningLists = NIL;
- List *rtable = NIL;
List *rowMarks;
- List *tlist;
- PlannerInfo subroot;
- ListCell *l;
+ ListCell *lc;
- foreach(l, root->append_rel_list)
+ /*
+ * We generate a modified instance of the original Query for each target
+ * relation, plan that, and put all the plans into a list that will be
+ * controlled by a single ModifyTable node. All the instances share the
+ * same rangetable, but each instance must have its own set of subquery
+ * RTEs within the finished rangetable because (1) they are likely to get
+ * scribbled on during planning, and (2) it's not inconceivable that
+ * subqueries could get planned differently in different cases. We need
+ * not create duplicate copies of other RTE kinds, in particular not the
+ * target relations, because they don't have either of those issues. Not
+ * having to duplicate the target relations is important because doing so
+ * (1) would result in a rangetable of length O(N^2) for N targets, with
+ * at least O(N^3) work expended here; and (2) would greatly complicate
+ * management of the rowMarks list.
+ */
+ foreach(lc, root->append_rel_list)
{
- AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
+ AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(lc);
+ PlannerInfo subroot;
Plan *subplan;
+ Index rti;
/* append_rel_list contains all append rels; ignore others */
if (appinfo->parent_relid != parentRTindex)
continue;
/*
- * Generate modified query with this rel as target.
+ * We need a working copy of the PlannerInfo so that we can control
+ * propagation of information back to the main copy.
*/
memcpy(&subroot, root, sizeof(PlannerInfo));
+
+ /*
+ * Generate modified query with this rel as target. We first apply
+ * adjust_appendrel_attrs, which copies the Query and changes
+ * references to the parent RTE to refer to the current child RTE,
+ * then fool around with subquery RTEs.
+ */
subroot.parse = (Query *)
adjust_appendrel_attrs((Node *) parse,
appinfo);
- subroot.init_plans = NIL;
- subroot.hasInheritedTarget = true;
+
+ /*
+ * The rowMarks list might contain references to subquery RTEs, so
+ * make a copy that we can apply ChangeVarNodes to. (Fortunately,
+ * the executor doesn't need to see the modified copies --- we can
+ * just pass it the original rowMarks list.)
+ */
+ subroot.rowMarks = (List *) copyObject(root->rowMarks);
+
+ /*
+ * Add placeholders to the child Query's rangetable list to fill the
+ * RT indexes already reserved for subqueries in previous children.
+ * These won't be referenced, so there's no need to make them very
+ * valid-looking.
+ */
+ while (list_length(subroot.parse->rtable) < list_length(final_rtable))
+ subroot.parse->rtable = lappend(subroot.parse->rtable,
+ makeNode(RangeTblEntry));
+
+ /*
+ * If this isn't the first child Query, generate duplicates of all
+ * subquery RTEs, and adjust Var numbering to reference the duplicates.
+ * To simplify the loop logic, we scan the original rtable not the
+ * copy just made by adjust_appendrel_attrs; that should be OK since
+ * subquery RTEs couldn't contain any references to the target rel.
+ */
+ if (final_rtable != NIL)
+ {
+ ListCell *lr;
+
+ rti = 1;
+ foreach(lr, parse->rtable)
+ {
+ RangeTblEntry *rte = (RangeTblEntry *) lfirst(lr);
+
+ if (rte->rtekind == RTE_SUBQUERY)
+ {
+ Index newrti;
+
+ /*
+ * The RTE can't contain any references to its own RT
+ * index, so we can save a few cycles by applying
+ * ChangeVarNodes before we append the RTE to the
+ * rangetable.
+ */
+ newrti = list_length(subroot.parse->rtable) + 1;
+ ChangeVarNodes((Node *) subroot.parse, rti, newrti, 0);
+ ChangeVarNodes((Node *) subroot.rowMarks, rti, newrti, 0);
+ rte = copyObject(rte);
+ subroot.parse->rtable = lappend(subroot.parse->rtable,
+ rte);
+ }
+ rti++;
+ }
+ }
+
/* We needn't modify the child's append_rel_list */
/* There shouldn't be any OJ info to translate, as yet */
Assert(subroot.join_info_list == NIL);
/* and we haven't created PlaceHolderInfos, either */
Assert(subroot.placeholder_list == NIL);
+ /* build a separate list of initplans for each child */
+ subroot.init_plans = NIL;
+ /* hack to mark target relation as an inheritance partition */
+ subroot.hasInheritedTarget = true;
/* Generate plan */
subplan = grouping_planner(&subroot, 0.0 /* retrieve all tuples */ );
/*
* If this child rel was excluded by constraint exclusion, exclude it
- * from the plan.
+ * from the result plan.
*/
if (is_dummy_plan(subplan))
continue;
- /* Save rtable from first rel for use below */
- if (subplans == NIL)
- rtable = subroot.parse->rtable;
-
subplans = lappend(subplans, subplan);
+ /*
+ * If this is the first non-excluded child, its post-planning rtable
+ * becomes the initial contents of final_rtable; otherwise, append
+ * just its modified subquery RTEs to final_rtable.
+ */
+ if (final_rtable == NIL)
+ final_rtable = subroot.parse->rtable;
+ else
+ final_rtable = list_concat(final_rtable,
+ list_copy_tail(subroot.parse->rtable,
+ list_length(final_rtable)));
+
+ /*
+ * We need to collect all the RelOptInfos from all child plans into
+ * the main PlannerInfo, since setrefs.c will need them. We use the
+ * last child's simple_rel_array (previous ones are too short), so we
+ * have to propagate forward the RelOptInfos that were already built
+ * in previous children.
+ */
+ Assert(subroot.simple_rel_array_size >= save_rel_array_size);
+ for (rti = 1; rti < save_rel_array_size; rti++)
+ {
+ RelOptInfo *brel = save_rel_array[rti];
+
+ if (brel)
+ subroot.simple_rel_array[rti] = brel;
+ }
+ save_rel_array_size = subroot.simple_rel_array_size;
+ save_rel_array = subroot.simple_rel_array;
+
/* Make sure any initplans from this rel get into the outer list */
root->init_plans = list_concat(root->init_plans, subroot.init_plans);
{
List *rlist;
- rlist = set_returning_clause_references(root->glob,
+ rlist = set_returning_clause_references(&subroot,
subroot.parse->returningList,
subplan,
appinfo->child_relid);
if (subplans == NIL)
{
/* although dummy, it must have a valid tlist for executor */
+ List *tlist;
+
tlist = preprocess_targetlist(root, parse->targetList);
return (Plan *) make_result(root,
tlist,
}
/*
- * Planning might have modified the rangetable, due to changes of the
- * Query structures inside subquery RTEs. We have to ensure that this
- * gets propagated back to the master copy. But can't do this until we
- * are done planning, because all the calls to grouping_planner need
- * virgin sub-Queries to work from. (We are effectively assuming that
- * sub-Queries will get planned identically each time, or at least that
- * the impacts on their rangetables will be the same each time.)
- *
- * XXX should clean this up someday
+ * Put back the final adjusted rtable into the master copy of the Query.
*/
- parse->rtable = rtable;
+ parse->rtable = final_rtable;
+ root->simple_rel_array_size = save_rel_array_size;
+ root->simple_rel_array = save_rel_array;
/*
* If there was a FOR UPDATE/SHARE clause, the LockRows node will have
rte->inFromCl = true;
query->rtable = list_make1(rte);
- /* ... and insert it into PlannerInfo */
- root->simple_rel_array_size = 2;
- root->simple_rel_array = (RelOptInfo **)
- palloc0(root->simple_rel_array_size * sizeof(RelOptInfo *));
- root->simple_rte_array = (RangeTblEntry **)
- palloc0(root->simple_rel_array_size * sizeof(RangeTblEntry *));
- root->simple_rte_array[1] = rte;
+ /* Set up RTE/RelOptInfo arrays */
+ setup_simple_rel_arrays(root);
/* Build RelOptInfo */
rel = build_simple_rel(root, 1, RELOPT_BASEREL);
#include "catalog/pg_type.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
+#include "optimizer/pathnode.h"
#include "optimizer/planmain.h"
#include "optimizer/tlist.h"
#include "utils/lsyscache.h"
typedef struct
{
- PlannerGlobal *glob;
+ PlannerInfo *root;
int rtoffset;
} fix_scan_expr_context;
typedef struct
{
- PlannerGlobal *glob;
+ PlannerInfo *root;
indexed_tlist *outer_itlist;
indexed_tlist *inner_itlist;
Index acceptable_rel;
typedef struct
{
- PlannerGlobal *glob;
+ PlannerInfo *root;
indexed_tlist *subplan_itlist;
int rtoffset;
} fix_upper_expr_context;
(((con)->consttype == REGCLASSOID || (con)->consttype == OIDOID) && \
!(con)->constisnull)
-#define fix_scan_list(glob, lst, rtoffset) \
- ((List *) fix_scan_expr(glob, (Node *) (lst), rtoffset))
+#define fix_scan_list(root, lst, rtoffset) \
+ ((List *) fix_scan_expr(root, (Node *) (lst), rtoffset))
-static Plan *set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset);
-static Plan *set_subqueryscan_references(PlannerGlobal *glob,
+static Plan *set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset);
+static Plan *set_subqueryscan_references(PlannerInfo *root,
SubqueryScan *plan,
int rtoffset);
static bool trivial_subqueryscan(SubqueryScan *plan);
-static Node *fix_scan_expr(PlannerGlobal *glob, Node *node, int rtoffset);
+static Node *fix_scan_expr(PlannerInfo *root, Node *node, int rtoffset);
static Node *fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context);
static bool fix_scan_expr_walker(Node *node, fix_scan_expr_context *context);
-static void set_join_references(PlannerGlobal *glob, Join *join, int rtoffset);
-static void set_upper_references(PlannerGlobal *glob, Plan *plan, int rtoffset);
+static void set_join_references(PlannerInfo *root, Join *join, int rtoffset);
+static void set_upper_references(PlannerInfo *root, Plan *plan, int rtoffset);
static void set_dummy_tlist_references(Plan *plan, int rtoffset);
static indexed_tlist *build_tlist_index(List *tlist);
static Var *search_indexed_tlist_for_var(Var *var,
Index sortgroupref,
indexed_tlist *itlist,
Index newvarno);
-static List *fix_join_expr(PlannerGlobal *glob,
+static List *fix_join_expr(PlannerInfo *root,
List *clauses,
indexed_tlist *outer_itlist,
indexed_tlist *inner_itlist,
Index acceptable_rel, int rtoffset);
static Node *fix_join_expr_mutator(Node *node,
fix_join_expr_context *context);
-static Node *fix_upper_expr(PlannerGlobal *glob,
+static Node *fix_upper_expr(PlannerInfo *root,
Node *node,
indexed_tlist *subplan_itlist,
int rtoffset);
fix_upper_expr_context *context);
static bool fix_opfuncids_walker(Node *node, void *context);
static bool extract_query_dependencies_walker(Node *node,
- PlannerGlobal *context);
+ PlannerInfo *context);
/*****************************************************************************
*
* set_plan_references recursively traverses the whole plan tree.
*
- * Inputs:
- * glob: global data for planner run
- * plan: the topmost node of the plan
- * rtable: the rangetable for the current subquery
- * rowmarks: the PlanRowMark list for the current subquery
- *
* The return value is normally the same Plan node passed in, but can be
* different when the passed-in Plan is a SubqueryScan we decide isn't needed.
*
- * The flattened rangetable entries are appended to glob->finalrtable.
- * Also, rowmarks entries are appended to glob->finalrowmarks, and the
- * RT indexes of ModifyTable result relations to glob->resultRelations.
- * Plan dependencies are appended to glob->relationOids (for relations)
- * and glob->invalItems (for everything else).
+ * The flattened rangetable entries are appended to root->glob->finalrtable.
+ * Also, rowmarks entries are appended to root->glob->finalrowmarks, and the
+ * RT indexes of ModifyTable result relations to root->glob->resultRelations.
+ * Plan dependencies are appended to root->glob->relationOids (for relations)
+ * and root->glob->invalItems (for everything else).
*
* Notice that we modify Plan nodes in-place, but use expression_tree_mutator
* to process targetlist and qual expressions. We can assume that the Plan
* it's not so safe to assume that for expression tree nodes.
*/
Plan *
-set_plan_references(PlannerGlobal *glob, Plan *plan,
- List *rtable, List *rowmarks)
+set_plan_references(PlannerInfo *root, Plan *plan)
{
+ PlannerGlobal *glob = root->glob;
int rtoffset = list_length(glob->finalrtable);
ListCell *lc;
* which are needed for executor-startup permissions checking and for
* trigger event checking.
*/
- foreach(lc, rtable)
+ foreach(lc, root->parse->rtable)
{
RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
RangeTblEntry *newrte;
/*
* Adjust RT indexes of PlanRowMarks and add to final rowmarks list
*/
- foreach(lc, rowmarks)
+ foreach(lc, root->rowMarks)
{
PlanRowMark *rc = (PlanRowMark *) lfirst(lc);
PlanRowMark *newrc;
}
/* Now fix the Plan tree */
- return set_plan_refs(glob, plan, rtoffset);
+ return set_plan_refs(root, plan, rtoffset);
}
/*
* set_plan_refs: recurse through the Plan nodes of a single subquery level
*/
static Plan *
-set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
+set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
{
ListCell *l;
splan->scanrelid += rtoffset;
splan->plan.targetlist =
- fix_scan_list(glob, splan->plan.targetlist, rtoffset);
+ fix_scan_list(root, splan->plan.targetlist, rtoffset);
splan->plan.qual =
- fix_scan_list(glob, splan->plan.qual, rtoffset);
+ fix_scan_list(root, splan->plan.qual, rtoffset);
}
break;
case T_IndexScan:
splan->scan.scanrelid += rtoffset;
splan->scan.plan.targetlist =
- fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
+ fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
splan->scan.plan.qual =
- fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
+ fix_scan_list(root, splan->scan.plan.qual, rtoffset);
splan->indexqual =
- fix_scan_list(glob, splan->indexqual, rtoffset);
+ fix_scan_list(root, splan->indexqual, rtoffset);
splan->indexqualorig =
- fix_scan_list(glob, splan->indexqualorig, rtoffset);
+ fix_scan_list(root, splan->indexqualorig, rtoffset);
splan->indexorderby =
- fix_scan_list(glob, splan->indexorderby, rtoffset);
+ fix_scan_list(root, splan->indexorderby, rtoffset);
splan->indexorderbyorig =
- fix_scan_list(glob, splan->indexorderbyorig, rtoffset);
+ fix_scan_list(root, splan->indexorderbyorig, rtoffset);
}
break;
case T_BitmapIndexScan:
Assert(splan->scan.plan.targetlist == NIL);
Assert(splan->scan.plan.qual == NIL);
splan->indexqual =
- fix_scan_list(glob, splan->indexqual, rtoffset);
+ fix_scan_list(root, splan->indexqual, rtoffset);
splan->indexqualorig =
- fix_scan_list(glob, splan->indexqualorig, rtoffset);
+ fix_scan_list(root, splan->indexqualorig, rtoffset);
}
break;
case T_BitmapHeapScan:
splan->scan.scanrelid += rtoffset;
splan->scan.plan.targetlist =
- fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
+ fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
splan->scan.plan.qual =
- fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
+ fix_scan_list(root, splan->scan.plan.qual, rtoffset);
splan->bitmapqualorig =
- fix_scan_list(glob, splan->bitmapqualorig, rtoffset);
+ fix_scan_list(root, splan->bitmapqualorig, rtoffset);
}
break;
case T_TidScan:
splan->scan.scanrelid += rtoffset;
splan->scan.plan.targetlist =
- fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
+ fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
splan->scan.plan.qual =
- fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
+ fix_scan_list(root, splan->scan.plan.qual, rtoffset);
splan->tidquals =
- fix_scan_list(glob, splan->tidquals, rtoffset);
+ fix_scan_list(root, splan->tidquals, rtoffset);
}
break;
case T_SubqueryScan:
/* Needs special treatment, see comments below */
- return set_subqueryscan_references(glob,
+ return set_subqueryscan_references(root,
(SubqueryScan *) plan,
rtoffset);
case T_FunctionScan:
splan->scan.scanrelid += rtoffset;
splan->scan.plan.targetlist =
- fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
+ fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
splan->scan.plan.qual =
- fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
+ fix_scan_list(root, splan->scan.plan.qual, rtoffset);
splan->funcexpr =
- fix_scan_expr(glob, splan->funcexpr, rtoffset);
+ fix_scan_expr(root, splan->funcexpr, rtoffset);
}
break;
case T_ValuesScan:
splan->scan.scanrelid += rtoffset;
splan->scan.plan.targetlist =
- fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
+ fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
splan->scan.plan.qual =
- fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
+ fix_scan_list(root, splan->scan.plan.qual, rtoffset);
splan->values_lists =
- fix_scan_list(glob, splan->values_lists, rtoffset);
+ fix_scan_list(root, splan->values_lists, rtoffset);
}
break;
case T_CteScan:
splan->scan.scanrelid += rtoffset;
splan->scan.plan.targetlist =
- fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
+ fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
splan->scan.plan.qual =
- fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
+ fix_scan_list(root, splan->scan.plan.qual, rtoffset);
}
break;
case T_WorkTableScan:
splan->scan.scanrelid += rtoffset;
splan->scan.plan.targetlist =
- fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
+ fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
splan->scan.plan.qual =
- fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
+ fix_scan_list(root, splan->scan.plan.qual, rtoffset);
}
break;
case T_ForeignScan:
splan->scan.scanrelid += rtoffset;
splan->scan.plan.targetlist =
- fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
+ fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
splan->scan.plan.qual =
- fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
+ fix_scan_list(root, splan->scan.plan.qual, rtoffset);
}
break;
case T_NestLoop:
case T_MergeJoin:
case T_HashJoin:
- set_join_references(glob, (Join *) plan, rtoffset);
+ set_join_references(root, (Join *) plan, rtoffset);
break;
case T_Hash:
Assert(splan->plan.qual == NIL);
splan->limitOffset =
- fix_scan_expr(glob, splan->limitOffset, rtoffset);
+ fix_scan_expr(root, splan->limitOffset, rtoffset);
splan->limitCount =
- fix_scan_expr(glob, splan->limitCount, rtoffset);
+ fix_scan_expr(root, splan->limitCount, rtoffset);
}
break;
case T_Agg:
case T_Group:
- set_upper_references(glob, plan, rtoffset);
+ set_upper_references(root, plan, rtoffset);
break;
case T_WindowAgg:
{
WindowAgg *wplan = (WindowAgg *) plan;
- set_upper_references(glob, plan, rtoffset);
+ set_upper_references(root, plan, rtoffset);
/*
* Like Limit node limit/offset expressions, WindowAgg has
* variable refs, so fix_scan_expr works for them.
*/
wplan->startOffset =
- fix_scan_expr(glob, wplan->startOffset, rtoffset);
+ fix_scan_expr(root, wplan->startOffset, rtoffset);
wplan->endOffset =
- fix_scan_expr(glob, wplan->endOffset, rtoffset);
+ fix_scan_expr(root, wplan->endOffset, rtoffset);
}
break;
case T_Result:
* like a scan node than an upper node.
*/
if (splan->plan.lefttree != NULL)
- set_upper_references(glob, plan, rtoffset);
+ set_upper_references(root, plan, rtoffset);
else
{
splan->plan.targetlist =
- fix_scan_list(glob, splan->plan.targetlist, rtoffset);
+ fix_scan_list(root, splan->plan.targetlist, rtoffset);
splan->plan.qual =
- fix_scan_list(glob, splan->plan.qual, rtoffset);
+ fix_scan_list(root, splan->plan.qual, rtoffset);
}
/* resconstantqual can't contain any subplan variable refs */
splan->resconstantqual =
- fix_scan_expr(glob, splan->resconstantqual, rtoffset);
+ fix_scan_expr(root, splan->resconstantqual, rtoffset);
}
break;
case T_ModifyTable:
}
foreach(l, splan->plans)
{
- lfirst(l) = set_plan_refs(glob,
+ lfirst(l) = set_plan_refs(root,
(Plan *) lfirst(l),
rtoffset);
}
* resultRelIndex to reflect their starting position in the
* global list.
*/
- splan->resultRelIndex = list_length(glob->resultRelations);
- glob->resultRelations =
- list_concat(glob->resultRelations,
+ splan->resultRelIndex = list_length(root->glob->resultRelations);
+ root->glob->resultRelations =
+ list_concat(root->glob->resultRelations,
list_copy(splan->resultRelations));
}
break;
Assert(splan->plan.qual == NIL);
foreach(l, splan->appendplans)
{
- lfirst(l) = set_plan_refs(glob,
+ lfirst(l) = set_plan_refs(root,
(Plan *) lfirst(l),
rtoffset);
}
Assert(splan->plan.qual == NIL);
foreach(l, splan->mergeplans)
{
- lfirst(l) = set_plan_refs(glob,
+ lfirst(l) = set_plan_refs(root,
(Plan *) lfirst(l),
rtoffset);
}
Assert(splan->plan.qual == NIL);
foreach(l, splan->bitmapplans)
{
- lfirst(l) = set_plan_refs(glob,
+ lfirst(l) = set_plan_refs(root,
(Plan *) lfirst(l),
rtoffset);
}
Assert(splan->plan.qual == NIL);
foreach(l, splan->bitmapplans)
{
- lfirst(l) = set_plan_refs(glob,
+ lfirst(l) = set_plan_refs(root,
(Plan *) lfirst(l),
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(glob, plan->lefttree, rtoffset);
- plan->righttree = set_plan_refs(glob, plan->righttree, rtoffset);
+ plan->lefttree = set_plan_refs(root, plan->lefttree, rtoffset);
+ plan->righttree = set_plan_refs(root, plan->righttree, rtoffset);
return plan;
}
* to do the normal processing on it.
*/
static Plan *
-set_subqueryscan_references(PlannerGlobal *glob,
+set_subqueryscan_references(PlannerInfo *root,
SubqueryScan *plan,
int rtoffset)
{
+ RelOptInfo *rel;
Plan *result;
- /* First, recursively process the subplan */
- plan->subplan = set_plan_references(glob, plan->subplan,
- plan->subrtable, plan->subrowmark);
+ /* Need to look up the subquery's RelOptInfo, since we need its subroot */
+ rel = find_base_rel(root, plan->scan.scanrelid);
+ Assert(rel->subplan == plan->subplan);
- /* subrtable/subrowmark are no longer needed in the plan tree */
- plan->subrtable = NIL;
- plan->subrowmark = NIL;
+ /* Recursively process the subplan */
+ plan->subplan = set_plan_references(rel->subroot, plan->subplan);
if (trivial_subqueryscan(plan))
{
*/
plan->scan.scanrelid += rtoffset;
plan->scan.plan.targetlist =
- fix_scan_list(glob, plan->scan.plan.targetlist, rtoffset);
+ fix_scan_list(root, plan->scan.plan.targetlist, rtoffset);
plan->scan.plan.qual =
- fix_scan_list(glob, plan->scan.plan.qual, rtoffset);
+ fix_scan_list(root, plan->scan.plan.qual, rtoffset);
result = (Plan *) plan;
}
*
* This is code that is common to all variants of expression-fixing.
* We must look up operator opcode info for OpExpr and related nodes,
- * add OIDs from regclass Const nodes into glob->relationOids,
- * and add catalog TIDs for user-defined functions into glob->invalItems.
+ * add OIDs from regclass Const nodes into root->glob->relationOids, and
+ * add catalog TIDs for user-defined functions into root->glob->invalItems.
*
* We assume it's okay to update opcode info in-place. So this could possibly
* scribble on the planner's input data structures, but it's OK.
*/
static void
-fix_expr_common(PlannerGlobal *glob, Node *node)
+fix_expr_common(PlannerInfo *root, Node *node)
{
/* We assume callers won't call us on a NULL pointer */
if (IsA(node, Aggref))
{
- record_plan_function_dependency(glob,
+ record_plan_function_dependency(root,
((Aggref *) node)->aggfnoid);
}
else if (IsA(node, WindowFunc))
{
- record_plan_function_dependency(glob,
+ record_plan_function_dependency(root,
((WindowFunc *) node)->winfnoid);
}
else if (IsA(node, FuncExpr))
{
- record_plan_function_dependency(glob,
+ record_plan_function_dependency(root,
((FuncExpr *) node)->funcid);
}
else if (IsA(node, OpExpr))
{
set_opfuncid((OpExpr *) node);
- record_plan_function_dependency(glob,
+ record_plan_function_dependency(root,
((OpExpr *) node)->opfuncid);
}
else if (IsA(node, DistinctExpr))
{
set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
- record_plan_function_dependency(glob,
+ record_plan_function_dependency(root,
((DistinctExpr *) node)->opfuncid);
}
else if (IsA(node, NullIfExpr))
{
set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
- record_plan_function_dependency(glob,
+ record_plan_function_dependency(root,
((NullIfExpr *) node)->opfuncid);
}
else if (IsA(node, ScalarArrayOpExpr))
{
set_sa_opfuncid((ScalarArrayOpExpr *) node);
- record_plan_function_dependency(glob,
+ record_plan_function_dependency(root,
((ScalarArrayOpExpr *) node)->opfuncid);
}
else if (IsA(node, ArrayCoerceExpr))
{
if (OidIsValid(((ArrayCoerceExpr *) node)->elemfuncid))
- record_plan_function_dependency(glob,
+ record_plan_function_dependency(root,
((ArrayCoerceExpr *) node)->elemfuncid);
}
else if (IsA(node, Const))
/* Check for regclass reference */
if (ISREGCLASSCONST(con))
- glob->relationOids =
- lappend_oid(glob->relationOids,
+ root->glob->relationOids =
+ lappend_oid(root->glob->relationOids,
DatumGetObjectId(con->constvalue));
}
}
*
* This consists of incrementing all Vars' varnos by rtoffset,
* looking up operator opcode info for OpExpr and related nodes,
- * and adding OIDs from regclass Const nodes into glob->relationOids.
+ * and adding OIDs from regclass Const nodes into root->glob->relationOids.
*/
static Node *
-fix_scan_expr(PlannerGlobal *glob, Node *node, int rtoffset)
+fix_scan_expr(PlannerInfo *root, Node *node, int rtoffset)
{
fix_scan_expr_context context;
- context.glob = glob;
+ context.root = root;
context.rtoffset = rtoffset;
- if (rtoffset != 0 || glob->lastPHId != 0)
+ if (rtoffset != 0 || root->glob->lastPHId != 0)
{
return fix_scan_expr_mutator(node, &context);
}
return fix_scan_expr_mutator((Node *) phv->phexpr, context);
}
- fix_expr_common(context->glob, node);
+ fix_expr_common(context->root, node);
return expression_tree_mutator(node, fix_scan_expr_mutator,
(void *) context);
}
if (node == NULL)
return false;
Assert(!IsA(node, PlaceHolderVar));
- fix_expr_common(context->glob, node);
+ fix_expr_common(context->root, node);
return expression_tree_walker(node, fix_scan_expr_walker,
(void *) context);
}
* subplans, by setting the varnos to OUTER or INNER and setting attno
* values to the result domain number of either the corresponding outer
* or inner join tuple item. Also perform opcode lookup for these
- * expressions. and add regclass OIDs to glob->relationOids.
+ * expressions. and add regclass OIDs to root->glob->relationOids.
*/
static void
-set_join_references(PlannerGlobal *glob, Join *join, int rtoffset)
+set_join_references(PlannerInfo *root, Join *join, int rtoffset)
{
Plan *outer_plan = join->plan.lefttree;
Plan *inner_plan = join->plan.righttree;
inner_itlist = build_tlist_index(inner_plan->targetlist);
/* All join plans have tlist, qual, and joinqual */
- join->plan.targetlist = fix_join_expr(glob,
+ join->plan.targetlist = fix_join_expr(root,
join->plan.targetlist,
outer_itlist,
inner_itlist,
(Index) 0,
rtoffset);
- join->plan.qual = fix_join_expr(glob,
+ join->plan.qual = fix_join_expr(root,
join->plan.qual,
outer_itlist,
inner_itlist,
(Index) 0,
rtoffset);
- join->joinqual = fix_join_expr(glob,
+ join->joinqual = fix_join_expr(root,
join->joinqual,
outer_itlist,
inner_itlist,
{
NestLoopParam *nlp = (NestLoopParam *) lfirst(lc);
- nlp->paramval = (Var *) fix_upper_expr(glob,
+ nlp->paramval = (Var *) fix_upper_expr(root,
(Node *) nlp->paramval,
outer_itlist,
rtoffset);
{
MergeJoin *mj = (MergeJoin *) join;
- mj->mergeclauses = fix_join_expr(glob,
+ mj->mergeclauses = fix_join_expr(root,
mj->mergeclauses,
outer_itlist,
inner_itlist,
{
HashJoin *hj = (HashJoin *) join;
- hj->hashclauses = fix_join_expr(glob,
+ hj->hashclauses = fix_join_expr(root,
hj->hashclauses,
outer_itlist,
inner_itlist,
* Update the targetlist and quals of an upper-level plan node
* to refer to the tuples returned by its lefttree subplan.
* Also perform opcode lookup for these expressions, and
- * add regclass OIDs to glob->relationOids.
+ * add regclass OIDs to root->glob->relationOids.
*
* This is used for single-input plan types like Agg, Group, Result.
*
* the expression.
*/
static void
-set_upper_references(PlannerGlobal *glob, Plan *plan, int rtoffset)
+set_upper_references(PlannerInfo *root, Plan *plan, int rtoffset)
{
Plan *subplan = plan->lefttree;
indexed_tlist *subplan_itlist;
subplan_itlist,
OUTER);
if (!newexpr)
- newexpr = fix_upper_expr(glob,
+ newexpr = fix_upper_expr(root,
(Node *) tle->expr,
subplan_itlist,
rtoffset);
}
else
- newexpr = fix_upper_expr(glob,
+ newexpr = fix_upper_expr(root,
(Node *) tle->expr,
subplan_itlist,
rtoffset);
plan->targetlist = output_targetlist;
plan->qual = (List *)
- fix_upper_expr(glob,
+ fix_upper_expr(root,
(Node *) plan->qual,
subplan_itlist,
rtoffset);
* changing the varno/varattno values of variables in the clauses
* to reference target list values from the outer and inner join
* relation target lists. Also perform opcode lookup and add
- * regclass OIDs to glob->relationOids.
+ * regclass OIDs to root->glob->relationOids.
*
* This is used in two different scenarios: a normal join clause, where
* all the Vars in the clause *must* be replaced by OUTER or INNER references;
* not modified.
*/
static List *
-fix_join_expr(PlannerGlobal *glob,
+fix_join_expr(PlannerInfo *root,
List *clauses,
indexed_tlist *outer_itlist,
indexed_tlist *inner_itlist,
{
fix_join_expr_context context;
- context.glob = glob;
+ context.root = root;
context.outer_itlist = outer_itlist;
context.inner_itlist = inner_itlist;
context.acceptable_rel = acceptable_rel;
if (newvar)
return (Node *) newvar;
}
- fix_expr_common(context->glob, node);
+ fix_expr_common(context->root, node);
return expression_tree_mutator(node,
fix_join_expr_mutator,
(void *) context);
* fix_upper_expr
* Modifies an expression tree so that all Var nodes reference outputs
* of a subplan. Also performs opcode lookup, and adds regclass OIDs to
- * glob->relationOids.
+ * root->glob->relationOids.
*
* This is used to fix up target and qual expressions of non-join upper-level
* plan nodes.
* The original tree is not modified.
*/
static Node *
-fix_upper_expr(PlannerGlobal *glob,
+fix_upper_expr(PlannerInfo *root,
Node *node,
indexed_tlist *subplan_itlist,
int rtoffset)
{
fix_upper_expr_context context;
- context.glob = glob;
+ context.root = root;
context.subplan_itlist = subplan_itlist;
context.rtoffset = rtoffset;
return fix_upper_expr_mutator(node, &context);
if (newvar)
return (Node *) newvar;
}
- fix_expr_common(context->glob, node);
+ fix_expr_common(context->root, node);
return expression_tree_mutator(node,
fix_upper_expr_mutator,
(void *) context);
* original varno, but Vars for other rels will have varno OUTER.
*
* We also must perform opcode lookup and add regclass OIDs to
- * glob->relationOids.
+ * root->glob->relationOids.
*
* 'rlist': the RETURNING targetlist to be fixed
* 'topplan': the top subplan node that will be just below the ModifyTable
* they are not coming from a subplan.
*/
List *
-set_returning_clause_references(PlannerGlobal *glob,
+set_returning_clause_references(PlannerInfo *root,
List *rlist,
Plan *topplan,
Index resultRelation)
*/
itlist = build_tlist_index_other_vars(topplan->targetlist, resultRelation);
- rlist = fix_join_expr(glob,
+ rlist = fix_join_expr(root,
rlist,
itlist,
NULL,
* dependency on a function that it's removed from the plan tree.
*/
void
-record_plan_function_dependency(PlannerGlobal *glob, Oid funcid)
+record_plan_function_dependency(PlannerInfo *root, Oid funcid)
{
/*
* For performance reasons, we don't bother to track built-in functions;
DatumGetUInt32(DirectFunctionCall1(hashoid,
ObjectIdGetDatum(funcid)));
- glob->invalItems = lappend(glob->invalItems, inval_item);
+ root->glob->invalItems = lappend(root->glob->invalItems, inval_item);
}
}
List **invalItems)
{
PlannerGlobal glob;
+ PlannerInfo root;
- /* Make up a dummy PlannerGlobal so we can use this module's machinery */
+ /* Make up dummy planner state so we can use this module's machinery */
MemSet(&glob, 0, sizeof(glob));
glob.type = T_PlannerGlobal;
glob.relationOids = NIL;
glob.invalItems = NIL;
- (void) extract_query_dependencies_walker(query, &glob);
+ MemSet(&root, 0, sizeof(root));
+ root.type = T_PlannerInfo;
+ root.glob = &glob;
+
+ (void) extract_query_dependencies_walker(query, &root);
*relationOids = glob.relationOids;
*invalItems = glob.invalItems;
}
static bool
-extract_query_dependencies_walker(Node *node, PlannerGlobal *context)
+extract_query_dependencies_walker(Node *node, PlannerInfo *context)
{
if (node == NULL)
return false;
RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
if (rte->rtekind == RTE_RELATION)
- context->relationOids = lappend_oid(context->relationOids,
- rte->relid);
+ context->glob->relationOids =
+ lappend_oid(context->glob->relationOids, rte->relid);
}
/* And recurse into the query's subexpressions */