X-Git-Url: https://granicus.if.org/sourcecode?a=blobdiff_plain;f=src%2Fbackend%2Foptimizer%2Fplan%2Fsetrefs.c;h=aa4fd4e1ebe71b0eabf0eb826c50fe81119de8ea;hb=08f8d478ebc37e42f3ced07d17dae83d6a9a3810;hp=bda3f38136d579b2743ac5c828d9679ab8591c40;hpb=29dccf5fe0e98af9ce023299c4fe9776a52fd23d;p=postgresql diff --git a/src/backend/optimizer/plan/setrefs.c b/src/backend/optimizer/plan/setrefs.c index bda3f38136..aa4fd4e1eb 100644 --- a/src/backend/optimizer/plan/setrefs.c +++ b/src/backend/optimizer/plan/setrefs.c @@ -2,26 +2,29 @@ * * setrefs.c * Post-processing of a completed plan tree: fix references to subplan - * vars, and compute regproc values for operators + * vars, compute regproc values for operators, etc * - * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group + * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/optimizer/plan/setrefs.c,v 1.127 2007/01/05 22:19:32 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/optimizer/plan/setrefs.c,v 1.157 2010/01/15 22:36:32 tgl Exp $ * *------------------------------------------------------------------------- */ #include "postgres.h" +#include "access/transam.h" +#include "catalog/pg_type.h" #include "nodes/makefuncs.h" +#include "nodes/nodeFuncs.h" #include "optimizer/clauses.h" #include "optimizer/planmain.h" #include "optimizer/tlist.h" -#include "parser/parse_expr.h" #include "parser/parsetree.h" #include "utils/lsyscache.h" +#include "utils/syscache.h" typedef struct @@ -35,55 +38,89 @@ typedef struct { List *tlist; /* underlying target list */ int num_vars; /* number of plain Var tlist entries */ - bool has_non_vars; /* are there non-plain-Var entries? */ + bool has_ph_vars; /* are there PlaceHolderVar entries? */ + bool has_non_vars; /* are there other entries? */ /* array of num_vars entries: */ tlist_vinfo vars[1]; /* VARIABLE LENGTH ARRAY */ } indexed_tlist; /* VARIABLE LENGTH STRUCT */ typedef struct { + PlannerGlobal *glob; + int rtoffset; +} fix_scan_expr_context; + +typedef struct +{ + PlannerGlobal *glob; indexed_tlist *outer_itlist; indexed_tlist *inner_itlist; Index acceptable_rel; -} join_references_context; + int rtoffset; +} fix_join_expr_context; typedef struct { + PlannerGlobal *glob; indexed_tlist *subplan_itlist; - Index subvarno; -} replace_vars_with_subplan_refs_context; + int rtoffset; +} fix_upper_expr_context; + +/* + * Check if a Const node is a regclass value. We accept plain OID too, + * since a regclass Const will get folded to that type if it's an argument + * to oideq or similar operators. (This might result in some extraneous + * values in a plan's list of relation dependencies, but the worst result + * would be occasional useless replans.) + */ +#define ISREGCLASSCONST(con) \ + (((con)->consttype == REGCLASSOID || (con)->consttype == OIDOID) && \ + !(con)->constisnull) + +#define fix_scan_list(glob, lst, rtoffset) \ + ((List *) fix_scan_expr(glob, (Node *) (lst), rtoffset)) -static Plan *set_subqueryscan_references(SubqueryScan *plan, List *rtable); +static Plan *set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset); +static Plan *set_subqueryscan_references(PlannerGlobal *glob, + SubqueryScan *plan, + int rtoffset); static bool trivial_subqueryscan(SubqueryScan *plan); -static void adjust_plan_varnos(Plan *plan, int rtoffset); -static void adjust_expr_varnos(Node *node, int rtoffset); -static bool adjust_expr_varnos_walker(Node *node, int *context); -static void fix_expr_references(Plan *plan, Node *node); -static bool fix_expr_references_walker(Node *node, void *context); -static void set_join_references(Join *join); -static void set_inner_join_references(Plan *inner_plan, +static Node *fix_scan_expr(PlannerGlobal *glob, 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_inner_join_references(PlannerGlobal *glob, Plan *inner_plan, indexed_tlist *outer_itlist); -static void set_uppernode_references(Plan *plan, Index subvarno); +static void set_upper_references(PlannerGlobal *glob, 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, indexed_tlist *itlist, - Index newvarno); + Index newvarno, + int rtoffset); static Var *search_indexed_tlist_for_non_var(Node *node, indexed_tlist *itlist, Index newvarno); -static List *join_references(List *clauses, - indexed_tlist *outer_itlist, - indexed_tlist *inner_itlist, - Index acceptable_rel); -static Node *join_references_mutator(Node *node, - join_references_context *context); -static Node *replace_vars_with_subplan_refs(Node *node, - indexed_tlist *subplan_itlist, - Index subvarno); -static Node *replace_vars_with_subplan_refs_mutator(Node *node, - replace_vars_with_subplan_refs_context *context); +static Var *search_indexed_tlist_for_sortgroupref(Node *node, + Index sortgroupref, + indexed_tlist *itlist, + Index newvarno); +static List *fix_join_expr(PlannerGlobal *glob, + 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, + Node *node, + indexed_tlist *subplan_itlist, + int rtoffset); +static Node *fix_upper_expr_mutator(Node *node, + fix_upper_expr_context *context); static bool fix_opfuncids_walker(Node *node, void *context); -static void set_sa_opfuncid(ScalarArrayOpExpr *opexpr); +static bool extract_query_dependencies_walker(Node *node, + PlannerGlobal *context); /***************************************************************************** @@ -97,34 +134,140 @@ static void set_sa_opfuncid(ScalarArrayOpExpr *opexpr); * * This is the final processing pass of the planner/optimizer. The plan * tree is complete; we just have to adjust some representational details - * for the convenience of the executor. We update Vars in upper plan nodes - * to refer to the outputs of their subplans, and we compute regproc OIDs - * for operators (ie, we look up the function that implements each op). + * for the convenience of the executor: + * + * 1. We flatten the various subquery rangetables into a single list, and + * zero out RangeTblEntry fields that are not useful to the executor. + * + * 2. We adjust Vars in scan nodes to be consistent with the flat rangetable. + * + * 3. We adjust Vars in upper plan nodes to refer to the outputs of their + * subplans. + * + * 4. We compute regproc OIDs for operators (ie, we look up the function + * that implements each op). + * + * 5. We create lists of specific objects that the plan depends on. + * This will be used by plancache.c to drive invalidation of cached plans. + * Relation dependencies are represented by OIDs, and everything else by + * PlanInvalItems (this distinction is motivated by the shared-inval APIs). + * Currently, relations and user-defined functions are the only types of + * objects that are explicitly tracked this way. * * We also perform one final optimization step, which is to delete * SubqueryScan plan nodes that aren't doing anything useful (ie, have * no qual and a no-op targetlist). The reason for doing this last is that * it can't readily be done before set_plan_references, because it would - * break set_uppernode_references: the Vars in the subquery's top tlist - * won't match up with the Vars in the outer plan tree. The SubqueryScan + * break set_upper_references: the Vars in the subquery's top tlist + * wouldn't match up with the Vars in the outer plan tree. The SubqueryScan * serves a necessary function as a buffer between outer query and subquery - * variable numbering ... but the executor doesn't care about that, only the - * planner. + * variable numbering ... but after we've flattened the rangetable this is + * no longer a problem, since then there's only one rtindex namespace. * * 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. * - * Note: to delete a SubqueryScan, we have to renumber Vars in its child nodes - * and append the modified subquery rangetable to the outer rangetable. - * Therefore "rtable" is an in/out argument and really should be declared - * "List **". But in the interest of notational simplicity we don't do that. - * (Since rtable can't be NIL if there's a SubqueryScan, the list header - * address won't change when we append a subquery rangetable.) + * The flattened rangetable entries are appended to glob->finalrtable, + * and we also append rowmarks entries to glob->finalrowmarks. + * Plan dependencies are appended to glob->relationOids (for relations) + * and 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 + * nodes were just built by the planner and are not multiply referenced, but + * it's not so safe to assume that for expression tree nodes. */ Plan * -set_plan_references(Plan *plan, List *rtable) +set_plan_references(PlannerGlobal *glob, Plan *plan, + List *rtable, List *rowmarks) +{ + int rtoffset = list_length(glob->finalrtable); + ListCell *lc; + + /* + * In the flat rangetable, we zero out substructure pointers that are not + * needed by the executor; this reduces the storage space and copying cost + * for cached plans. We keep only the alias and eref Alias fields, which + * are needed by EXPLAIN, and the selectedCols and modifiedCols bitmaps, + * which are needed for executor-startup permissions checking and for + * trigger event checking. + */ + foreach(lc, rtable) + { + RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc); + RangeTblEntry *newrte; + + /* flat copy to duplicate all the scalar fields */ + newrte = (RangeTblEntry *) palloc(sizeof(RangeTblEntry)); + memcpy(newrte, rte, sizeof(RangeTblEntry)); + + /* zap unneeded sub-structure */ + newrte->subquery = NULL; + newrte->joinaliasvars = NIL; + newrte->funcexpr = NULL; + newrte->funccoltypes = NIL; + newrte->funccoltypmods = NIL; + newrte->values_lists = NIL; + newrte->ctecoltypes = NIL; + newrte->ctecoltypmods = NIL; + + glob->finalrtable = lappend(glob->finalrtable, newrte); + + /* + * If it's a plain relation RTE, add the table to relationOids. + * + * We do this even though the RTE might be unreferenced in the plan + * tree; this would correspond to cases such as views that were + * expanded, child tables that were eliminated by constraint + * exclusion, etc. Schema invalidation on such a rel must still force + * rebuilding of the plan. + * + * Note we don't bother to avoid duplicate list entries. We could, + * but it would probably cost more cycles than it would save. + */ + if (newrte->rtekind == RTE_RELATION) + glob->relationOids = lappend_oid(glob->relationOids, + newrte->relid); + } + + /* + * Adjust RT indexes of PlanRowMarks and add to final rowmarks list + */ + foreach(lc, rowmarks) + { + PlanRowMark *rc = (PlanRowMark *) lfirst(lc); + PlanRowMark *newrc; + + Assert(IsA(rc, PlanRowMark)); + + /* flat copy is enough since all fields are scalars */ + newrc = (PlanRowMark *) palloc(sizeof(PlanRowMark)); + memcpy(newrc, rc, sizeof(PlanRowMark)); + + /* adjust indexes */ + newrc->rti += rtoffset; + newrc->prti += rtoffset; + + glob->finalrowmarks = lappend(glob->finalrowmarks, newrc); + } + + /* Now fix the Plan tree */ + return set_plan_refs(glob, 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) { ListCell *l; @@ -137,86 +280,130 @@ set_plan_references(Plan *plan, List *rtable) switch (nodeTag(plan)) { case T_SeqScan: - fix_expr_references(plan, (Node *) plan->targetlist); - fix_expr_references(plan, (Node *) plan->qual); + { + SeqScan *splan = (SeqScan *) plan; + + splan->scanrelid += rtoffset; + splan->plan.targetlist = + fix_scan_list(glob, splan->plan.targetlist, rtoffset); + splan->plan.qual = + fix_scan_list(glob, splan->plan.qual, rtoffset); + } break; case T_IndexScan: - fix_expr_references(plan, (Node *) plan->targetlist); - fix_expr_references(plan, (Node *) plan->qual); - fix_expr_references(plan, - (Node *) ((IndexScan *) plan)->indexqual); - fix_expr_references(plan, - (Node *) ((IndexScan *) plan)->indexqualorig); + { + IndexScan *splan = (IndexScan *) plan; + + splan->scan.scanrelid += rtoffset; + splan->scan.plan.targetlist = + fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset); + splan->scan.plan.qual = + fix_scan_list(glob, splan->scan.plan.qual, rtoffset); + splan->indexqual = + fix_scan_list(glob, splan->indexqual, rtoffset); + splan->indexqualorig = + fix_scan_list(glob, splan->indexqualorig, rtoffset); + } break; case T_BitmapIndexScan: - /* no need to fix targetlist and qual */ - Assert(plan->targetlist == NIL); - Assert(plan->qual == NIL); - fix_expr_references(plan, - (Node *) ((BitmapIndexScan *) plan)->indexqual); - fix_expr_references(plan, - (Node *) ((BitmapIndexScan *) plan)->indexqualorig); + { + BitmapIndexScan *splan = (BitmapIndexScan *) plan; + + splan->scan.scanrelid += rtoffset; + /* no need to fix targetlist and qual */ + Assert(splan->scan.plan.targetlist == NIL); + Assert(splan->scan.plan.qual == NIL); + splan->indexqual = + fix_scan_list(glob, splan->indexqual, rtoffset); + splan->indexqualorig = + fix_scan_list(glob, splan->indexqualorig, rtoffset); + } break; case T_BitmapHeapScan: - fix_expr_references(plan, (Node *) plan->targetlist); - fix_expr_references(plan, (Node *) plan->qual); - fix_expr_references(plan, - (Node *) ((BitmapHeapScan *) plan)->bitmapqualorig); + { + BitmapHeapScan *splan = (BitmapHeapScan *) plan; + + splan->scan.scanrelid += rtoffset; + splan->scan.plan.targetlist = + fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset); + splan->scan.plan.qual = + fix_scan_list(glob, splan->scan.plan.qual, rtoffset); + splan->bitmapqualorig = + fix_scan_list(glob, splan->bitmapqualorig, rtoffset); + } break; case T_TidScan: - fix_expr_references(plan, (Node *) plan->targetlist); - fix_expr_references(plan, (Node *) plan->qual); - fix_expr_references(plan, (Node *) ((TidScan *) plan)->tidquals); + { + TidScan *splan = (TidScan *) plan; + + splan->scan.scanrelid += rtoffset; + splan->scan.plan.targetlist = + fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset); + splan->scan.plan.qual = + fix_scan_list(glob, splan->scan.plan.qual, rtoffset); + splan->tidquals = + fix_scan_list(glob, splan->tidquals, rtoffset); + } break; case T_SubqueryScan: /* Needs special treatment, see comments below */ - return set_subqueryscan_references((SubqueryScan *) plan, rtable); + return set_subqueryscan_references(glob, + (SubqueryScan *) plan, + rtoffset); case T_FunctionScan: { - RangeTblEntry *rte; - - fix_expr_references(plan, (Node *) plan->targetlist); - fix_expr_references(plan, (Node *) plan->qual); - rte = rt_fetch(((FunctionScan *) plan)->scan.scanrelid, - rtable); - Assert(rte->rtekind == RTE_FUNCTION); - fix_expr_references(plan, rte->funcexpr); + FunctionScan *splan = (FunctionScan *) plan; + + splan->scan.scanrelid += rtoffset; + splan->scan.plan.targetlist = + fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset); + splan->scan.plan.qual = + fix_scan_list(glob, splan->scan.plan.qual, rtoffset); + splan->funcexpr = + fix_scan_expr(glob, splan->funcexpr, rtoffset); } break; case T_ValuesScan: { - RangeTblEntry *rte; - - fix_expr_references(plan, (Node *) plan->targetlist); - fix_expr_references(plan, (Node *) plan->qual); - rte = rt_fetch(((ValuesScan *) plan)->scan.scanrelid, - rtable); - Assert(rte->rtekind == RTE_VALUES); - fix_expr_references(plan, (Node *) rte->values_lists); + ValuesScan *splan = (ValuesScan *) plan; + + splan->scan.scanrelid += rtoffset; + splan->scan.plan.targetlist = + fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset); + splan->scan.plan.qual = + fix_scan_list(glob, splan->scan.plan.qual, rtoffset); + splan->values_lists = + fix_scan_list(glob, splan->values_lists, rtoffset); } break; - case T_NestLoop: - set_join_references((Join *) plan); - fix_expr_references(plan, (Node *) plan->targetlist); - fix_expr_references(plan, (Node *) plan->qual); - fix_expr_references(plan, (Node *) ((Join *) plan)->joinqual); + case T_CteScan: + { + CteScan *splan = (CteScan *) plan; + + splan->scan.scanrelid += rtoffset; + splan->scan.plan.targetlist = + fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset); + splan->scan.plan.qual = + fix_scan_list(glob, splan->scan.plan.qual, rtoffset); + } break; - case T_MergeJoin: - set_join_references((Join *) plan); - fix_expr_references(plan, (Node *) plan->targetlist); - fix_expr_references(plan, (Node *) plan->qual); - fix_expr_references(plan, (Node *) ((Join *) plan)->joinqual); - fix_expr_references(plan, - (Node *) ((MergeJoin *) plan)->mergeclauses); + case T_WorkTableScan: + { + WorkTableScan *splan = (WorkTableScan *) plan; + + splan->scan.scanrelid += rtoffset; + splan->scan.plan.targetlist = + fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset); + splan->scan.plan.qual = + fix_scan_list(glob, splan->scan.plan.qual, rtoffset); + } break; + case T_NestLoop: + case T_MergeJoin: case T_HashJoin: - set_join_references((Join *) plan); - fix_expr_references(plan, (Node *) plan->targetlist); - fix_expr_references(plan, (Node *) plan->qual); - fix_expr_references(plan, (Node *) ((Join *) plan)->joinqual); - fix_expr_references(plan, - (Node *) ((HashJoin *) plan)->hashclauses); + set_join_references(glob, (Join *) plan, rtoffset); break; + case T_Hash: case T_Material: case T_Sort: @@ -225,75 +412,168 @@ set_plan_references(Plan *plan, List *rtable) /* * These plan types don't actually bother to evaluate their - * targetlists (because they just return their unmodified input - * tuples). The optimizer is lazy about creating really valid - * targetlists for them --- it tends to just put in a pointer to - * the child plan node's tlist. Hence, we leave the tlist alone. - * In particular, we do not want to process subplans in the tlist, - * since we will likely end up reprocessing subplans that also - * appear in lower levels of the plan tree! - * + * targetlists, because they just return their unmodified input + * tuples. Even though the targetlist won't be used by the + * executor, we fix it up for possible use by EXPLAIN (not to + * mention ease of debugging --- wrong varnos are very confusing). + */ + set_dummy_tlist_references(plan, rtoffset); + + /* * Since these plan types don't check quals either, we should not * find any qual expression attached to them. */ Assert(plan->qual == NIL); break; + case T_LockRows: + { + LockRows *splan = (LockRows *) plan; + + /* + * Like the plan types above, LockRows doesn't evaluate its + * tlist or quals. But we have to fix up the RT indexes + * in its rowmarks. + */ + set_dummy_tlist_references(plan, rtoffset); + Assert(splan->plan.qual == NIL); + + foreach(l, splan->rowMarks) + { + PlanRowMark *rc = (PlanRowMark *) lfirst(l); + + rc->rti += rtoffset; + rc->prti += rtoffset; + } + } + break; case T_Limit: - - /* - * Like the plan types above, Limit doesn't evaluate its tlist or - * quals. It does have live expressions for limit/offset, - * however. - */ - Assert(plan->qual == NIL); - fix_expr_references(plan, ((Limit *) plan)->limitOffset); - fix_expr_references(plan, ((Limit *) plan)->limitCount); + { + Limit *splan = (Limit *) plan; + + /* + * Like the plan types above, Limit doesn't evaluate its tlist + * or quals. It does have live expressions for limit/offset, + * however; and those cannot contain subplan variable refs, so + * fix_scan_expr works for them. + */ + set_dummy_tlist_references(plan, rtoffset); + Assert(splan->plan.qual == NIL); + + splan->limitOffset = + fix_scan_expr(glob, splan->limitOffset, rtoffset); + splan->limitCount = + fix_scan_expr(glob, splan->limitCount, rtoffset); + } break; case T_Agg: + case T_WindowAgg: case T_Group: - set_uppernode_references(plan, (Index) 0); - fix_expr_references(plan, (Node *) plan->targetlist); - fix_expr_references(plan, (Node *) plan->qual); + set_upper_references(glob, plan, rtoffset); break; case T_Result: - - /* - * Result may or may not have a subplan; no need to fix up subplan - * references if it hasn't got one... - * - * XXX why does Result use a different subvarno from Agg/Group? - */ - if (plan->lefttree != NULL) - set_uppernode_references(plan, (Index) OUTER); - fix_expr_references(plan, (Node *) plan->targetlist); - fix_expr_references(plan, (Node *) plan->qual); - fix_expr_references(plan, ((Result *) plan)->resconstantqual); + { + Result *splan = (Result *) plan; + + /* + * Result may or may not have a subplan; if not, it's more + * like a scan node than an upper node. + */ + if (splan->plan.lefttree != NULL) + set_upper_references(glob, plan, rtoffset); + else + { + splan->plan.targetlist = + fix_scan_list(glob, splan->plan.targetlist, rtoffset); + splan->plan.qual = + fix_scan_list(glob, splan->plan.qual, rtoffset); + } + /* resconstantqual can't contain any subplan variable refs */ + splan->resconstantqual = + fix_scan_expr(glob, splan->resconstantqual, rtoffset); + } + break; + case T_ModifyTable: + { + ModifyTable *splan = (ModifyTable *) plan; + + /* + * planner.c already called set_returning_clause_references, + * so we should not process either the targetlist or the + * returningLists. + */ + Assert(splan->plan.qual == NIL); + + foreach(l, splan->resultRelations) + { + lfirst_int(l) += rtoffset; + } + foreach(l, splan->rowMarks) + { + PlanRowMark *rc = (PlanRowMark *) lfirst(l); + + rc->rti += rtoffset; + rc->prti += rtoffset; + } + foreach(l, splan->plans) + { + lfirst(l) = set_plan_refs(glob, + (Plan *) lfirst(l), + rtoffset); + } + } break; case T_Append: - - /* - * Append, like Sort et al, doesn't actually evaluate its - * targetlist or check quals, and we haven't bothered to give it - * its own tlist copy. So, don't fix targetlist/qual. But do - * recurse into child plans. - */ + { + Append *splan = (Append *) plan; + + /* + * Append, like Sort et al, doesn't actually evaluate its + * targetlist or check quals. + */ + set_dummy_tlist_references(plan, rtoffset); + Assert(splan->plan.qual == NIL); + foreach(l, splan->appendplans) + { + lfirst(l) = set_plan_refs(glob, + (Plan *) lfirst(l), + rtoffset); + } + } + break; + case T_RecursiveUnion: + /* This doesn't evaluate targetlist or check quals either */ + set_dummy_tlist_references(plan, rtoffset); Assert(plan->qual == NIL); - foreach(l, ((Append *) plan)->appendplans) - lfirst(l) = set_plan_references((Plan *) lfirst(l), rtable); break; case T_BitmapAnd: - /* BitmapAnd works like Append, but has no tlist */ - Assert(plan->targetlist == NIL); - Assert(plan->qual == NIL); - foreach(l, ((BitmapAnd *) plan)->bitmapplans) - lfirst(l) = set_plan_references((Plan *) lfirst(l), rtable); + { + BitmapAnd *splan = (BitmapAnd *) plan; + + /* BitmapAnd works like Append, but has no tlist */ + Assert(splan->plan.targetlist == NIL); + Assert(splan->plan.qual == NIL); + foreach(l, splan->bitmapplans) + { + lfirst(l) = set_plan_refs(glob, + (Plan *) lfirst(l), + rtoffset); + } + } break; case T_BitmapOr: - /* BitmapOr works like Append, but has no tlist */ - Assert(plan->targetlist == NIL); - Assert(plan->qual == NIL); - foreach(l, ((BitmapOr *) plan)->bitmapplans) - lfirst(l) = set_plan_references((Plan *) lfirst(l), rtable); + { + BitmapOr *splan = (BitmapOr *) plan; + + /* BitmapOr works like Append, but has no tlist */ + Assert(splan->plan.targetlist == NIL); + Assert(splan->plan.qual == NIL); + foreach(l, splan->bitmapplans) + { + lfirst(l) = set_plan_refs(glob, + (Plan *) lfirst(l), + rtoffset); + } + } break; default: elog(ERROR, "unrecognized node type: %d", @@ -302,25 +582,15 @@ set_plan_references(Plan *plan, List *rtable) } /* - * Now recurse into child plans and initplans, if any + * Now recurse into child plans, if any * * NOTE: it is essential that we recurse into child plans AFTER we set * subplan references in this plan's tlist and quals. If we did the * reference-adjustments bottom-up, then we would fail to match this * plan's var nodes against the already-modified nodes of the children. - * Fortunately, that consideration doesn't apply to SubPlan nodes; else - * we'd need two passes over the expression trees. */ - plan->lefttree = set_plan_references(plan->lefttree, rtable); - plan->righttree = set_plan_references(plan->righttree, rtable); - - foreach(l, plan->initPlan) - { - SubPlan *sp = (SubPlan *) lfirst(l); - - Assert(IsA(sp, SubPlan)); - sp->plan = set_plan_references(sp->plan, sp->rtable); - } + plan->lefttree = set_plan_refs(glob, plan->lefttree, rtoffset); + plan->righttree = set_plan_refs(glob, plan->righttree, rtoffset); return plan; } @@ -333,58 +603,31 @@ set_plan_references(Plan *plan, List *rtable) * to do the normal processing on it. */ static Plan * -set_subqueryscan_references(SubqueryScan *plan, List *rtable) +set_subqueryscan_references(PlannerGlobal *glob, + SubqueryScan *plan, + int rtoffset) { Plan *result; - RangeTblEntry *rte; - ListCell *l; /* First, recursively process the subplan */ - rte = rt_fetch(plan->scan.scanrelid, rtable); - Assert(rte->rtekind == RTE_SUBQUERY); - plan->subplan = set_plan_references(plan->subplan, - rte->subquery->rtable); + plan->subplan = set_plan_references(glob, plan->subplan, + plan->subrtable, plan->subrowmark); - /* - * We have to process any initplans too; set_plan_references can't do it - * for us because of the possibility of double-processing. - */ - foreach(l, plan->scan.plan.initPlan) - { - SubPlan *sp = (SubPlan *) lfirst(l); - - Assert(IsA(sp, SubPlan)); - sp->plan = set_plan_references(sp->plan, sp->rtable); - } + /* subrtable/subrowmark are no longer needed in the plan tree */ + plan->subrtable = NIL; + plan->subrowmark = NIL; if (trivial_subqueryscan(plan)) { /* - * We can omit the SubqueryScan node and just pull up the subplan. We - * have to merge its rtable into the outer rtable, which means - * adjusting varnos throughout the subtree. + * We can omit the SubqueryScan node and just pull up the subplan. */ - int rtoffset = list_length(rtable); - List *sub_rtable; ListCell *lp, *lc; - sub_rtable = copyObject(rte->subquery->rtable); - range_table_walker(sub_rtable, - adjust_expr_varnos_walker, - (void *) &rtoffset, - QTW_IGNORE_RT_SUBQUERIES); - rtable = list_concat(rtable, sub_rtable); - - /* - * we have to copy the subplan to make sure there are no duplicately - * linked nodes in it, else adjust_plan_varnos might increment some - * varnos twice - */ - result = copyObject(plan->subplan); - - adjust_plan_varnos(result, rtoffset); + result = plan->subplan; + /* We have to be sure we don't lose any initplans */ result->initPlan = list_concat(plan->scan.plan.initPlan, result->initPlan); @@ -409,14 +652,17 @@ set_subqueryscan_references(SubqueryScan *plan, List *rtable) /* * Keep the SubqueryScan node. We have to do the processing that * set_plan_references would otherwise have done on it. Notice we do - * not do set_uppernode_references() here, because a SubqueryScan will + * not do set_upper_references() here, because a SubqueryScan will * always have been created with correct references to its subplan's * outputs to begin with. */ - result = (Plan *) plan; + plan->scan.scanrelid += rtoffset; + plan->scan.plan.targetlist = + fix_scan_list(glob, plan->scan.plan.targetlist, rtoffset); + plan->scan.plan.qual = + fix_scan_list(glob, plan->scan.plan.qual, rtoffset); - fix_expr_references(result, (Node *) result->targetlist); - fix_expr_references(result, (Node *) result->qual); + result = (Plan *) plan; } return result; @@ -481,259 +727,197 @@ trivial_subqueryscan(SubqueryScan *plan) } /* - * adjust_plan_varnos - * Offset varnos and other rangetable indexes in a plan tree by rtoffset. + * copyVar + * Copy a Var node. + * + * fix_scan_expr and friends do this enough times that it's worth having + * a bespoke routine instead of using the generic copyObject() function. */ -static void -adjust_plan_varnos(Plan *plan, int rtoffset) +static inline Var * +copyVar(Var *var) { - ListCell *l; + Var *newvar = (Var *) palloc(sizeof(Var)); - if (plan == NULL) - return; + *newvar = *var; + return newvar; +} - /* - * Plan-type-specific fixes - */ - switch (nodeTag(plan)) +/* + * fix_expr_common + * Do generic set_plan_references processing on an expression node + * + * 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. + * + * 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) +{ + /* We assume callers won't call us on a NULL pointer */ + if (IsA(node, Aggref)) { - case T_SeqScan: - ((SeqScan *) plan)->scanrelid += rtoffset; - adjust_expr_varnos((Node *) plan->targetlist, rtoffset); - adjust_expr_varnos((Node *) plan->qual, rtoffset); - break; - case T_IndexScan: - ((IndexScan *) plan)->scan.scanrelid += rtoffset; - adjust_expr_varnos((Node *) plan->targetlist, rtoffset); - adjust_expr_varnos((Node *) plan->qual, rtoffset); - adjust_expr_varnos((Node *) ((IndexScan *) plan)->indexqual, - rtoffset); - adjust_expr_varnos((Node *) ((IndexScan *) plan)->indexqualorig, - rtoffset); - break; - case T_BitmapIndexScan: - ((BitmapIndexScan *) plan)->scan.scanrelid += rtoffset; - /* no need to fix targetlist and qual */ - Assert(plan->targetlist == NIL); - Assert(plan->qual == NIL); - adjust_expr_varnos((Node *) ((BitmapIndexScan *) plan)->indexqual, - rtoffset); - adjust_expr_varnos((Node *) ((BitmapIndexScan *) plan)->indexqualorig, - rtoffset); - break; - case T_BitmapHeapScan: - ((BitmapHeapScan *) plan)->scan.scanrelid += rtoffset; - adjust_expr_varnos((Node *) plan->targetlist, rtoffset); - adjust_expr_varnos((Node *) plan->qual, rtoffset); - adjust_expr_varnos((Node *) ((BitmapHeapScan *) plan)->bitmapqualorig, - rtoffset); - break; - case T_TidScan: - ((TidScan *) plan)->scan.scanrelid += rtoffset; - adjust_expr_varnos((Node *) plan->targetlist, rtoffset); - adjust_expr_varnos((Node *) plan->qual, rtoffset); - adjust_expr_varnos((Node *) ((TidScan *) plan)->tidquals, - rtoffset); - break; - case T_SubqueryScan: - ((SubqueryScan *) plan)->scan.scanrelid += rtoffset; - adjust_expr_varnos((Node *) plan->targetlist, rtoffset); - adjust_expr_varnos((Node *) plan->qual, rtoffset); - /* we should not recurse into the subquery! */ - break; - case T_FunctionScan: - ((FunctionScan *) plan)->scan.scanrelid += rtoffset; - adjust_expr_varnos((Node *) plan->targetlist, rtoffset); - adjust_expr_varnos((Node *) plan->qual, rtoffset); - /* rte was already fixed by set_subqueryscan_references */ - break; - case T_ValuesScan: - ((ValuesScan *) plan)->scan.scanrelid += rtoffset; - adjust_expr_varnos((Node *) plan->targetlist, rtoffset); - adjust_expr_varnos((Node *) plan->qual, rtoffset); - /* rte was already fixed by set_subqueryscan_references */ - break; - case T_NestLoop: - adjust_expr_varnos((Node *) plan->targetlist, rtoffset); - adjust_expr_varnos((Node *) plan->qual, rtoffset); - adjust_expr_varnos((Node *) ((Join *) plan)->joinqual, rtoffset); - break; - case T_MergeJoin: - adjust_expr_varnos((Node *) plan->targetlist, rtoffset); - adjust_expr_varnos((Node *) plan->qual, rtoffset); - adjust_expr_varnos((Node *) ((Join *) plan)->joinqual, rtoffset); - adjust_expr_varnos((Node *) ((MergeJoin *) plan)->mergeclauses, - rtoffset); - break; - case T_HashJoin: - adjust_expr_varnos((Node *) plan->targetlist, rtoffset); - adjust_expr_varnos((Node *) plan->qual, rtoffset); - adjust_expr_varnos((Node *) ((Join *) plan)->joinqual, rtoffset); - adjust_expr_varnos((Node *) ((HashJoin *) plan)->hashclauses, - rtoffset); - break; - case T_Hash: - case T_Material: - case T_Sort: - case T_Unique: - case T_SetOp: - - /* - * Even though the targetlist won't be used by the executor, we - * fix it up for possible use by EXPLAIN (not to mention ease of - * debugging --- wrong varnos are very confusing). - */ - adjust_expr_varnos((Node *) plan->targetlist, rtoffset); - Assert(plan->qual == NIL); - break; - case T_Limit: - - /* - * Like the plan types above, Limit doesn't evaluate its tlist or - * quals. It does have live expressions for limit/offset, - * however. - */ - adjust_expr_varnos((Node *) plan->targetlist, rtoffset); - Assert(plan->qual == NIL); - adjust_expr_varnos(((Limit *) plan)->limitOffset, rtoffset); - adjust_expr_varnos(((Limit *) plan)->limitCount, rtoffset); - break; - case T_Agg: - case T_Group: - adjust_expr_varnos((Node *) plan->targetlist, rtoffset); - adjust_expr_varnos((Node *) plan->qual, rtoffset); - break; - case T_Result: - adjust_expr_varnos((Node *) plan->targetlist, rtoffset); - adjust_expr_varnos((Node *) plan->qual, rtoffset); - adjust_expr_varnos(((Result *) plan)->resconstantqual, rtoffset); - break; - case T_Append: - adjust_expr_varnos((Node *) plan->targetlist, rtoffset); - Assert(plan->qual == NIL); - foreach(l, ((Append *) plan)->appendplans) - adjust_plan_varnos((Plan *) lfirst(l), rtoffset); - break; - case T_BitmapAnd: - /* BitmapAnd works like Append, but has no tlist */ - Assert(plan->targetlist == NIL); - Assert(plan->qual == NIL); - foreach(l, ((BitmapAnd *) plan)->bitmapplans) - adjust_plan_varnos((Plan *) lfirst(l), rtoffset); - break; - case T_BitmapOr: - /* BitmapOr works like Append, but has no tlist */ - Assert(plan->targetlist == NIL); - Assert(plan->qual == NIL); - foreach(l, ((BitmapOr *) plan)->bitmapplans) - adjust_plan_varnos((Plan *) lfirst(l), rtoffset); - break; - default: - elog(ERROR, "unrecognized node type: %d", - (int) nodeTag(plan)); - break; + record_plan_function_dependency(glob, + ((Aggref *) node)->aggfnoid); + } + else if (IsA(node, WindowFunc)) + { + record_plan_function_dependency(glob, + ((WindowFunc *) node)->winfnoid); + } + else if (IsA(node, FuncExpr)) + { + record_plan_function_dependency(glob, + ((FuncExpr *) node)->funcid); + } + else if (IsA(node, OpExpr)) + { + set_opfuncid((OpExpr *) node); + record_plan_function_dependency(glob, + ((OpExpr *) node)->opfuncid); + } + else if (IsA(node, DistinctExpr)) + { + set_opfuncid((OpExpr *) node); /* rely on struct equivalence */ + record_plan_function_dependency(glob, + ((DistinctExpr *) node)->opfuncid); + } + else if (IsA(node, NullIfExpr)) + { + set_opfuncid((OpExpr *) node); /* rely on struct equivalence */ + record_plan_function_dependency(glob, + ((NullIfExpr *) node)->opfuncid); + } + else if (IsA(node, ScalarArrayOpExpr)) + { + set_sa_opfuncid((ScalarArrayOpExpr *) node); + record_plan_function_dependency(glob, + ((ScalarArrayOpExpr *) node)->opfuncid); + } + else if (IsA(node, ArrayCoerceExpr)) + { + if (OidIsValid(((ArrayCoerceExpr *) node)->elemfuncid)) + record_plan_function_dependency(glob, + ((ArrayCoerceExpr *) node)->elemfuncid); } + else if (IsA(node, Const)) + { + Const *con = (Const *) node; - /* - * Now recurse into child plans. - * - * We don't need to (and in fact mustn't) recurse into subqueries, so no - * need to examine initPlan list. - */ - adjust_plan_varnos(plan->lefttree, rtoffset); - adjust_plan_varnos(plan->righttree, rtoffset); + /* Check for regclass reference */ + if (ISREGCLASSCONST(con)) + glob->relationOids = + lappend_oid(glob->relationOids, + DatumGetObjectId(con->constvalue)); + } } /* - * adjust_expr_varnos - * Offset varnos of Vars in an expression by rtoffset. + * fix_scan_expr + * Do set_plan_references processing on a scan-level expression * - * This is different from the rewriter's OffsetVarNodes in that it has to - * work on an already-planned expression tree; in particular, we should not - * disturb INNER and OUTER references. On the other hand, we don't have to - * recurse into subqueries nor deal with outer-level Vars, so it's pretty - * simple. + * 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. */ -static void -adjust_expr_varnos(Node *node, int rtoffset) +static Node * +fix_scan_expr(PlannerGlobal *glob, Node *node, int rtoffset) { - /* This tree walk requires no special setup, so away we go... */ - adjust_expr_varnos_walker(node, &rtoffset); + fix_scan_expr_context context; + + context.glob = glob; + context.rtoffset = rtoffset; + + if (rtoffset != 0 || glob->lastPHId != 0) + { + return fix_scan_expr_mutator(node, &context); + } + else + { + /* + * If rtoffset == 0, we don't need to change any Vars, and if there + * are no placeholders anywhere we won't need to remove them. Then + * it's OK to just scribble on the input node tree instead of copying + * (since the only change, filling in any unset opfuncid fields, is + * harmless). This saves just enough cycles to be noticeable on + * trivial queries. + */ + (void) fix_scan_expr_walker(node, &context); + return node; + } } -static bool -adjust_expr_varnos_walker(Node *node, int *context) +static Node * +fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context) { if (node == NULL) - return false; + return NULL; if (IsA(node, Var)) { - Var *var = (Var *) node; + Var *var = copyVar((Var *) node); Assert(var->varlevelsup == 0); - if (var->varno > 0 && var->varno != INNER && var->varno != OUTER) - var->varno += *context; + + /* + * We should not see any Vars marked INNER, but in a nestloop inner + * scan there could be OUTER Vars. Leave them alone. + */ + Assert(var->varno != INNER); + if (var->varno > 0 && var->varno != OUTER) + var->varno += context->rtoffset; if (var->varnoold > 0) - var->varnoold += *context; - return false; + var->varnoold += context->rtoffset; + return (Node *) var; } - return expression_tree_walker(node, adjust_expr_varnos_walker, - (void *) context); -} + if (IsA(node, CurrentOfExpr)) + { + CurrentOfExpr *cexpr = (CurrentOfExpr *) copyObject(node); -/* - * fix_expr_references - * Do final cleanup on expressions (targetlists or quals). - * - * This consists of looking up operator opcode info for OpExpr nodes - * and recursively performing set_plan_references on subplans. - * - * The Plan argument is currently unused, but might be needed again someday. - */ -static void -fix_expr_references(Plan *plan, Node *node) -{ - /* This tree walk requires no special setup, so away we go... */ - fix_expr_references_walker(node, NULL); + Assert(cexpr->cvarno != INNER); + Assert(cexpr->cvarno != OUTER); + cexpr->cvarno += context->rtoffset; + return (Node *) cexpr; + } + if (IsA(node, PlaceHolderVar)) + { + /* At scan level, we should always just evaluate the contained expr */ + PlaceHolderVar *phv = (PlaceHolderVar *) node; + + return fix_scan_expr_mutator((Node *) phv->phexpr, context); + } + fix_expr_common(context->glob, node); + return expression_tree_mutator(node, fix_scan_expr_mutator, + (void *) context); } static bool -fix_expr_references_walker(Node *node, void *context) +fix_scan_expr_walker(Node *node, fix_scan_expr_context *context) { if (node == NULL) return false; - if (IsA(node, OpExpr)) - set_opfuncid((OpExpr *) node); - else if (IsA(node, DistinctExpr)) - set_opfuncid((OpExpr *) node); /* rely on struct equivalence */ - else if (IsA(node, ScalarArrayOpExpr)) - set_sa_opfuncid((ScalarArrayOpExpr *) node); - else if (IsA(node, NullIfExpr)) - set_opfuncid((OpExpr *) node); /* rely on struct equivalence */ - else if (IsA(node, SubPlan)) - { - SubPlan *sp = (SubPlan *) node; - - sp->plan = set_plan_references(sp->plan, sp->rtable); - } - return expression_tree_walker(node, fix_expr_references_walker, context); + Assert(!IsA(node, PlaceHolderVar)); + fix_expr_common(context->glob, node); + return expression_tree_walker(node, fix_scan_expr_walker, + (void *) context); } /* * set_join_references - * Modifies the target list and quals of a join node to reference its + * Modify the target list and quals of a join node to reference its * 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. + * or inner join tuple item. Also perform opcode lookup for these + * expressions. and add regclass OIDs to glob->relationOids. * * In the case of a nestloop with inner indexscan, we will also need to * apply the same transformation to any outer vars appearing in the * quals of the child indexscan. set_inner_join_references does that. - * - * 'join' is a join plan node */ static void -set_join_references(Join *join) +set_join_references(PlannerGlobal *glob, Join *join, int rtoffset) { Plan *outer_plan = join->plan.lefttree; Plan *inner_plan = join->plan.righttree; @@ -744,43 +928,52 @@ set_join_references(Join *join) inner_itlist = build_tlist_index(inner_plan->targetlist); /* All join plans have tlist, qual, and joinqual */ - join->plan.targetlist = join_references(join->plan.targetlist, - outer_itlist, - inner_itlist, - (Index) 0); - join->plan.qual = join_references(join->plan.qual, - outer_itlist, - inner_itlist, - (Index) 0); - join->joinqual = join_references(join->joinqual, - outer_itlist, - inner_itlist, - (Index) 0); + join->plan.targetlist = fix_join_expr(glob, + join->plan.targetlist, + outer_itlist, + inner_itlist, + (Index) 0, + rtoffset); + join->plan.qual = fix_join_expr(glob, + join->plan.qual, + outer_itlist, + inner_itlist, + (Index) 0, + rtoffset); + join->joinqual = fix_join_expr(glob, + join->joinqual, + outer_itlist, + inner_itlist, + (Index) 0, + rtoffset); /* Now do join-type-specific stuff */ if (IsA(join, NestLoop)) { /* This processing is split out to handle possible recursion */ - set_inner_join_references(inner_plan, - outer_itlist); + set_inner_join_references(glob, inner_plan, outer_itlist); } else if (IsA(join, MergeJoin)) { MergeJoin *mj = (MergeJoin *) join; - mj->mergeclauses = join_references(mj->mergeclauses, - outer_itlist, - inner_itlist, - (Index) 0); + mj->mergeclauses = fix_join_expr(glob, + mj->mergeclauses, + outer_itlist, + inner_itlist, + (Index) 0, + rtoffset); } else if (IsA(join, HashJoin)) { HashJoin *hj = (HashJoin *) join; - hj->hashclauses = join_references(hj->hashclauses, - outer_itlist, - inner_itlist, - (Index) 0); + hj->hashclauses = fix_join_expr(glob, + hj->hashclauses, + outer_itlist, + inner_itlist, + (Index) 0, + rtoffset); } pfree(outer_itlist); @@ -795,9 +988,14 @@ set_join_references(Join *join) * to the bottom BitmapIndexScan nodes; likewise, appendrel indexscans * require recursing through Append nodes. This is split out as a separate * function so that it can recurse. + * + * Note we do *not* apply any rtoffset for non-join Vars; this is because + * the quals will be processed again by fix_scan_expr when the set_plan_refs + * recursion reaches the inner indexscan, and so we'd have done it twice. */ static void -set_inner_join_references(Plan *inner_plan, indexed_tlist *outer_itlist) +set_inner_join_references(PlannerGlobal *glob, Plan *inner_plan, + indexed_tlist *outer_itlist) { if (IsA(inner_plan, IndexScan)) { @@ -816,14 +1014,18 @@ set_inner_join_references(Plan *inner_plan, indexed_tlist *outer_itlist) Index innerrel = innerscan->scan.scanrelid; /* only refs to outer vars get changed in the inner qual */ - innerscan->indexqualorig = join_references(indexqualorig, - outer_itlist, - NULL, - innerrel); - innerscan->indexqual = join_references(innerscan->indexqual, - outer_itlist, - NULL, - innerrel); + innerscan->indexqualorig = fix_join_expr(glob, + indexqualorig, + outer_itlist, + NULL, + innerrel, + 0); + innerscan->indexqual = fix_join_expr(glob, + innerscan->indexqual, + outer_itlist, + NULL, + innerrel, + 0); /* * We must fix the inner qpqual too, if it has join clauses (this @@ -831,10 +1033,12 @@ set_inner_join_references(Plan *inner_plan, indexed_tlist *outer_itlist) * may get rechecked as qpquals). */ if (NumRelids((Node *) inner_plan->qual) > 1) - inner_plan->qual = join_references(inner_plan->qual, - outer_itlist, - NULL, - innerrel); + inner_plan->qual = fix_join_expr(glob, + inner_plan->qual, + outer_itlist, + NULL, + innerrel, + 0); } } else if (IsA(inner_plan, BitmapIndexScan)) @@ -851,14 +1055,18 @@ set_inner_join_references(Plan *inner_plan, indexed_tlist *outer_itlist) Index innerrel = innerscan->scan.scanrelid; /* only refs to outer vars get changed in the inner qual */ - innerscan->indexqualorig = join_references(indexqualorig, - outer_itlist, - NULL, - innerrel); - innerscan->indexqual = join_references(innerscan->indexqual, - outer_itlist, - NULL, - innerrel); + innerscan->indexqualorig = fix_join_expr(glob, + indexqualorig, + outer_itlist, + NULL, + innerrel, + 0); + innerscan->indexqual = fix_join_expr(glob, + innerscan->indexqual, + outer_itlist, + NULL, + innerrel, + 0); /* no need to fix inner qpqual */ Assert(inner_plan->qual == NIL); } @@ -878,10 +1086,12 @@ set_inner_join_references(Plan *inner_plan, indexed_tlist *outer_itlist) /* only refs to outer vars get changed in the inner qual */ if (NumRelids((Node *) bitmapqualorig) > 1) - innerscan->bitmapqualorig = join_references(bitmapqualorig, - outer_itlist, - NULL, - innerrel); + innerscan->bitmapqualorig = fix_join_expr(glob, + bitmapqualorig, + outer_itlist, + NULL, + innerrel, + 0); /* * We must fix the inner qpqual too, if it has join clauses (this @@ -889,14 +1099,15 @@ set_inner_join_references(Plan *inner_plan, indexed_tlist *outer_itlist) * get rechecked as qpquals). */ if (NumRelids((Node *) inner_plan->qual) > 1) - inner_plan->qual = join_references(inner_plan->qual, - outer_itlist, - NULL, - innerrel); + inner_plan->qual = fix_join_expr(glob, + inner_plan->qual, + outer_itlist, + NULL, + innerrel, + 0); /* Now recurse */ - set_inner_join_references(inner_plan->lefttree, - outer_itlist); + set_inner_join_references(glob, inner_plan->lefttree, outer_itlist); } else if (IsA(inner_plan, BitmapAnd)) { @@ -906,8 +1117,7 @@ set_inner_join_references(Plan *inner_plan, indexed_tlist *outer_itlist) foreach(l, innerscan->bitmapplans) { - set_inner_join_references((Plan *) lfirst(l), - outer_itlist); + set_inner_join_references(glob, (Plan *) lfirst(l), outer_itlist); } } else if (IsA(inner_plan, BitmapOr)) @@ -918,10 +1128,21 @@ set_inner_join_references(Plan *inner_plan, indexed_tlist *outer_itlist) foreach(l, innerscan->bitmapplans) { - set_inner_join_references((Plan *) lfirst(l), - outer_itlist); + set_inner_join_references(glob, (Plan *) lfirst(l), outer_itlist); } } + else if (IsA(inner_plan, TidScan)) + { + TidScan *innerscan = (TidScan *) inner_plan; + Index innerrel = innerscan->scan.scanrelid; + + innerscan->tidquals = fix_join_expr(glob, + innerscan->tidquals, + outer_itlist, + NULL, + innerrel, + 0); + } else if (IsA(inner_plan, Append)) { /* @@ -933,26 +1154,25 @@ set_inner_join_references(Plan *inner_plan, indexed_tlist *outer_itlist) foreach(l, appendplan->appendplans) { - set_inner_join_references((Plan *) lfirst(l), - outer_itlist); + set_inner_join_references(glob, (Plan *) lfirst(l), outer_itlist); } } - else if (IsA(inner_plan, TidScan)) + else if (IsA(inner_plan, Result)) { - TidScan *innerscan = (TidScan *) inner_plan; - Index innerrel = innerscan->scan.scanrelid; + /* Recurse through a gating Result node (similar to Append case) */ + Result *result = (Result *) inner_plan; - innerscan->tidquals = join_references(innerscan->tidquals, - outer_itlist, - NULL, - innerrel); + if (result->plan.lefttree) + set_inner_join_references(glob, result->plan.lefttree, outer_itlist); } } /* - * set_uppernode_references + * set_upper_references * 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. * * This is used for single-input plan types like Agg, Group, Result. * @@ -966,17 +1186,14 @@ set_inner_join_references(Plan *inner_plan, indexed_tlist *outer_itlist) * the expression. */ static void -set_uppernode_references(Plan *plan, Index subvarno) +set_upper_references(PlannerGlobal *glob, Plan *plan, int rtoffset) { Plan *subplan = plan->lefttree; indexed_tlist *subplan_itlist; List *output_targetlist; ListCell *l; - if (subplan != NULL) - subplan_itlist = build_tlist_index(subplan->targetlist); - else - subplan_itlist = build_tlist_index(NIL); + subplan_itlist = build_tlist_index(subplan->targetlist); output_targetlist = NIL; foreach(l, plan->targetlist) @@ -984,9 +1201,25 @@ set_uppernode_references(Plan *plan, Index subvarno) TargetEntry *tle = (TargetEntry *) lfirst(l); Node *newexpr; - newexpr = replace_vars_with_subplan_refs((Node *) tle->expr, - subplan_itlist, - subvarno); + /* If it's a non-Var sort/group item, first try to match by sortref */ + if (tle->ressortgroupref != 0 && !IsA(tle->expr, Var)) + { + newexpr = (Node *) + search_indexed_tlist_for_sortgroupref((Node *) tle->expr, + tle->ressortgroupref, + subplan_itlist, + OUTER); + if (!newexpr) + newexpr = fix_upper_expr(glob, + (Node *) tle->expr, + subplan_itlist, + rtoffset); + } + else + newexpr = fix_upper_expr(glob, + (Node *) tle->expr, + subplan_itlist, + rtoffset); tle = flatCopyTargetEntry(tle); tle->expr = (Expr *) newexpr; output_targetlist = lappend(output_targetlist, tle); @@ -994,13 +1227,66 @@ set_uppernode_references(Plan *plan, Index subvarno) plan->targetlist = output_targetlist; plan->qual = (List *) - replace_vars_with_subplan_refs((Node *) plan->qual, - subplan_itlist, - subvarno); + fix_upper_expr(glob, + (Node *) plan->qual, + subplan_itlist, + rtoffset); pfree(subplan_itlist); } +/* + * set_dummy_tlist_references + * Replace the targetlist of an upper-level plan node with a simple + * list of OUTER references to its child. + * + * This is used for plan types like Sort and Append that don't evaluate + * their targetlists. Although the executor doesn't care at all what's in + * the tlist, EXPLAIN needs it to be realistic. + * + * Note: we could almost use set_upper_references() here, but it fails for + * Append for lack of a lefttree subplan. Single-purpose code is faster + * anyway. + */ +static void +set_dummy_tlist_references(Plan *plan, int rtoffset) +{ + List *output_targetlist; + ListCell *l; + + output_targetlist = NIL; + foreach(l, plan->targetlist) + { + TargetEntry *tle = (TargetEntry *) lfirst(l); + Var *oldvar = (Var *) tle->expr; + Var *newvar; + + newvar = makeVar(OUTER, + tle->resno, + exprType((Node *) oldvar), + exprTypmod((Node *) oldvar), + 0); + if (IsA(oldvar, Var)) + { + newvar->varnoold = oldvar->varno + rtoffset; + newvar->varoattno = oldvar->varattno; + } + else + { + newvar->varnoold = 0; /* wasn't ever a plain Var */ + newvar->varoattno = 0; + } + + tle = flatCopyTargetEntry(tle); + tle->expr = (Expr *) newvar; + output_targetlist = lappend(output_targetlist, tle); + } + plan->targetlist = output_targetlist; + + /* We don't touch plan->qual here */ +} + + /* * build_tlist_index --- build an index data structure for a child tlist * @@ -1027,6 +1313,7 @@ build_tlist_index(List *tlist) list_length(tlist) * sizeof(tlist_vinfo)); itlist->tlist = tlist; + itlist->has_ph_vars = false; itlist->has_non_vars = false; /* Find the Vars and fill in the index array */ @@ -1044,6 +1331,8 @@ build_tlist_index(List *tlist) vinfo->resno = tle->resno; vinfo++; } + else if (tle->expr && IsA(tle->expr, PlaceHolderVar)) + itlist->has_ph_vars = true; else itlist->has_non_vars = true; } @@ -1057,7 +1346,9 @@ build_tlist_index(List *tlist) * build_tlist_index_other_vars --- build a restricted tlist index * * This is like build_tlist_index, but we only index tlist entries that - * are Vars and belong to some rel other than the one specified. + * are Vars belonging to some rel other than the one specified. We will set + * has_ph_vars (allowing PlaceHolderVars to be matched), but not has_non_vars + * (so nothing other than Vars and PlaceHolderVars can be matched). */ static indexed_tlist * build_tlist_index_other_vars(List *tlist, Index ignore_rel) @@ -1072,6 +1363,7 @@ build_tlist_index_other_vars(List *tlist, Index ignore_rel) list_length(tlist) * sizeof(tlist_vinfo)); itlist->tlist = tlist; + itlist->has_ph_vars = false; itlist->has_non_vars = false; /* Find the desired Vars and fill in the index array */ @@ -1092,6 +1384,8 @@ build_tlist_index_other_vars(List *tlist, Index ignore_rel) vinfo++; } } + else if (tle->expr && IsA(tle->expr, PlaceHolderVar)) + itlist->has_ph_vars = true; } itlist->num_vars = (vinfo - itlist->vars); @@ -1104,10 +1398,12 @@ build_tlist_index_other_vars(List *tlist, Index ignore_rel) * * If a match is found, return a copy of the given Var with suitably * modified varno/varattno (to wit, newvarno and the resno of the TLE entry). + * Also ensure that varnoold is incremented by rtoffset. * If no match, return NULL. */ static Var * -search_indexed_tlist_for_var(Var *var, indexed_tlist *itlist, Index newvarno) +search_indexed_tlist_for_var(Var *var, indexed_tlist *itlist, + Index newvarno, int rtoffset) { Index varno = var->varno; AttrNumber varattno = var->varattno; @@ -1121,10 +1417,12 @@ search_indexed_tlist_for_var(Var *var, indexed_tlist *itlist, Index newvarno) if (vinfo->varno == varno && vinfo->varattno == varattno) { /* Found a match */ - Var *newvar = (Var *) copyObject(var); + Var *newvar = copyVar(var); newvar->varno = newvarno; newvar->varattno = vinfo->resno; + if (newvar->varnoold > 0) + newvar->varnoold += rtoffset; return newvar; } vinfo++; @@ -1138,7 +1436,8 @@ search_indexed_tlist_for_var(Var *var, indexed_tlist *itlist, Index newvarno) * If a match is found, return a Var constructed to reference the tlist item. * If no match, return NULL. * - * NOTE: it is a waste of time to call this if !itlist->has_non_vars + * NOTE: it is a waste of time to call this unless itlist->has_ph_vars or + * itlist->has_non_vars */ static Var * search_indexed_tlist_for_non_var(Node *node, @@ -1165,22 +1464,67 @@ search_indexed_tlist_for_non_var(Node *node, } /* - * join_references - * Creates a new set of targetlist entries or join qual clauses by + * search_indexed_tlist_for_sortgroupref --- find a sort/group expression + * (which is assumed not to be just a Var) + * + * If a match is found, return a Var constructed to reference the tlist item. + * If no match, return NULL. + * + * This is needed to ensure that we select the right subplan TLE in cases + * where there are multiple textually-equal()-but-volatile sort expressions. + * And it's also faster than search_indexed_tlist_for_non_var. + */ +static Var * +search_indexed_tlist_for_sortgroupref(Node *node, + Index sortgroupref, + indexed_tlist *itlist, + Index newvarno) +{ + ListCell *lc; + + foreach(lc, itlist->tlist) + { + TargetEntry *tle = (TargetEntry *) lfirst(lc); + + /* The equal() check should be redundant, but let's be paranoid */ + if (tle->ressortgroupref == sortgroupref && + equal(node, tle->expr)) + { + /* Found a matching subplan output expression */ + Var *newvar; + + newvar = makeVar(newvarno, + tle->resno, + exprType((Node *) tle->expr), + exprTypmod((Node *) tle->expr), + 0); + newvar->varnoold = 0; /* wasn't ever a plain Var */ + newvar->varoattno = 0; + return newvar; + } + } + return NULL; /* no match */ +} + +/* + * fix_join_expr + * Create a new set of targetlist entries or join qual clauses by * changing the varno/varattno values of variables in the clauses * to reference target list values from the outer and inner join - * relation target lists. + * relation target lists. Also perform opcode lookup and add + * regclass OIDs to 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; * and an indexscan being used on the inner side of a nestloop join. * In the latter case we want to replace the outer-relation Vars by OUTER - * references, but not touch the Vars of the inner relation. (We also - * implement RETURNING clause fixup using this second scenario.) + * references, while Vars of the inner relation should be adjusted by rtoffset. + * (We also implement RETURNING clause fixup using this second scenario.) * * For a normal join, acceptable_rel should be zero so that any failure to * match a Var will be reported as an error. For the indexscan case, - * pass inner_itlist = NULL and acceptable_rel = the ID of the inner relation. + * pass inner_itlist = NULL and acceptable_rel = the (not-offseted-yet) ID + * of the inner relation. * * 'clauses' is the targetlist or list of join clauses * 'outer_itlist' is the indexed target list of the outer join relation @@ -1188,27 +1532,31 @@ search_indexed_tlist_for_non_var(Node *node, * or NULL * 'acceptable_rel' is either zero or the rangetable index of a relation * whose Vars may appear in the clause without provoking an error. + * 'rtoffset' is what to add to varno for Vars of acceptable_rel. * * Returns the new expression tree. The original clause structure is * not modified. */ static List * -join_references(List *clauses, - indexed_tlist *outer_itlist, - indexed_tlist *inner_itlist, - Index acceptable_rel) +fix_join_expr(PlannerGlobal *glob, + List *clauses, + indexed_tlist *outer_itlist, + indexed_tlist *inner_itlist, + Index acceptable_rel, + int rtoffset) { - join_references_context context; + fix_join_expr_context context; + context.glob = glob; context.outer_itlist = outer_itlist; context.inner_itlist = inner_itlist; context.acceptable_rel = acceptable_rel; - return (List *) join_references_mutator((Node *) clauses, &context); + context.rtoffset = rtoffset; + return (List *) fix_join_expr_mutator((Node *) clauses, &context); } static Node * -join_references_mutator(Node *node, - join_references_context *context) +fix_join_expr_mutator(Node *node, fix_join_expr_context *context) { Var *newvar; @@ -1221,25 +1569,57 @@ join_references_mutator(Node *node, /* First look for the var in the input tlists */ newvar = search_indexed_tlist_for_var(var, context->outer_itlist, - OUTER); + OUTER, + context->rtoffset); if (newvar) return (Node *) newvar; if (context->inner_itlist) { newvar = search_indexed_tlist_for_var(var, context->inner_itlist, - INNER); + INNER, + context->rtoffset); if (newvar) return (Node *) newvar; } - /* Return the Var unmodified, if it's for acceptable_rel */ + /* If it's for acceptable_rel, adjust and return it */ if (var->varno == context->acceptable_rel) - return (Node *) copyObject(var); + { + var = copyVar(var); + var->varno += context->rtoffset; + var->varnoold += context->rtoffset; + return (Node *) var; + } /* No referent found for Var */ elog(ERROR, "variable not found in subplan target lists"); } + if (IsA(node, PlaceHolderVar)) + { + PlaceHolderVar *phv = (PlaceHolderVar *) node; + + /* See if the PlaceHolderVar has bubbled up from a lower plan node */ + if (context->outer_itlist->has_ph_vars) + { + newvar = search_indexed_tlist_for_non_var((Node *) phv, + context->outer_itlist, + OUTER); + if (newvar) + return (Node *) newvar; + } + if (context->inner_itlist && context->inner_itlist->has_ph_vars) + { + newvar = search_indexed_tlist_for_non_var((Node *) phv, + context->inner_itlist, + INNER); + if (newvar) + return (Node *) newvar; + } + + /* If not supplied by input plans, evaluate the contained expr */ + return fix_join_expr_mutator((Node *) phv->phexpr, context); + } /* Try matching more complex expressions too, if tlists have any */ if (context->outer_itlist->has_non_vars) { @@ -1257,16 +1637,20 @@ join_references_mutator(Node *node, if (newvar) return (Node *) newvar; } + fix_expr_common(context->glob, node); return expression_tree_mutator(node, - join_references_mutator, + fix_join_expr_mutator, (void *) context); } /* - * replace_vars_with_subplan_refs - * This routine modifies an expression tree so that all Var nodes - * reference target nodes of a subplan. It is used to fix up - * target and qual expressions of non-join upper-level plan nodes. + * 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. + * + * This is used to fix up target and qual expressions of non-join upper-level + * plan nodes. * * An error is raised if no matching var can be found in the subplan tlist * --- so this routine should only be applied to nodes whose subplans' @@ -1280,27 +1664,28 @@ join_references_mutator(Node *node, * * 'node': the tree to be fixed (a target item or qual) * 'subplan_itlist': indexed target list for subplan - * 'subvarno': varno to be assigned to all Vars + * 'rtoffset': how much to increment varnoold by * * The resulting tree is a copy of the original in which all Var nodes have - * varno = subvarno, varattno = resno of corresponding subplan target. + * varno = OUTER, varattno = resno of corresponding subplan target. * The original tree is not modified. */ static Node * -replace_vars_with_subplan_refs(Node *node, - indexed_tlist *subplan_itlist, - Index subvarno) +fix_upper_expr(PlannerGlobal *glob, + Node *node, + indexed_tlist *subplan_itlist, + int rtoffset) { - replace_vars_with_subplan_refs_context context; + fix_upper_expr_context context; + context.glob = glob; context.subplan_itlist = subplan_itlist; - context.subvarno = subvarno; - return replace_vars_with_subplan_refs_mutator(node, &context); + context.rtoffset = rtoffset; + return fix_upper_expr_mutator(node, &context); } static Node * -replace_vars_with_subplan_refs_mutator(Node *node, - replace_vars_with_subplan_refs_context *context) +fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context) { Var *newvar; @@ -1312,22 +1697,40 @@ replace_vars_with_subplan_refs_mutator(Node *node, newvar = search_indexed_tlist_for_var(var, context->subplan_itlist, - context->subvarno); + OUTER, + context->rtoffset); if (!newvar) elog(ERROR, "variable not found in subplan target list"); return (Node *) newvar; } + if (IsA(node, PlaceHolderVar)) + { + PlaceHolderVar *phv = (PlaceHolderVar *) node; + + /* See if the PlaceHolderVar has bubbled up from a lower plan node */ + if (context->subplan_itlist->has_ph_vars) + { + newvar = search_indexed_tlist_for_non_var((Node *) phv, + context->subplan_itlist, + OUTER); + if (newvar) + return (Node *) newvar; + } + /* If not supplied by input plan, evaluate the contained expr */ + return fix_upper_expr_mutator((Node *) phv->phexpr, context); + } /* Try matching more complex expressions too, if tlist has any */ if (context->subplan_itlist->has_non_vars) { newvar = search_indexed_tlist_for_non_var(node, context->subplan_itlist, - context->subvarno); + OUTER); if (newvar) return (Node *) newvar; } + fix_expr_common(context->glob, node); return expression_tree_mutator(node, - replace_vars_with_subplan_refs_mutator, + fix_upper_expr_mutator, (void *) context); } @@ -1337,41 +1740,53 @@ replace_vars_with_subplan_refs_mutator(Node *node, * * If the query involves more than just the result table, we have to * adjust any Vars that refer to other tables to reference junk tlist - * entries in the top plan's targetlist. Vars referencing the result + * entries in the top subplan's targetlist. Vars referencing the result * table should be left alone, however (the executor will evaluate them * using the actual heap tuple, after firing triggers if any). In the * adjusted RETURNING list, result-table Vars will still have their * original varno, but Vars for other rels will have varno OUTER. * - * We also must apply fix_expr_references to the list. + * We also must perform opcode lookup and add regclass OIDs to + * glob->relationOids. * * 'rlist': the RETURNING targetlist to be fixed - * 'topplan': the top Plan node for the query (not yet passed through - * set_plan_references) - * 'resultRelation': RT index of the query's result relation + * 'topplan': the top subplan node that will be just below the ModifyTable + * node (note it's not yet passed through set_plan_references) + * 'resultRelation': RT index of the associated result relation + * + * Note: we assume that result relations will have rtoffset zero, that is, + * they are not coming from a subplan. */ List * -set_returning_clause_references(List *rlist, +set_returning_clause_references(PlannerGlobal *glob, + List *rlist, Plan *topplan, Index resultRelation) { indexed_tlist *itlist; /* - * We can perform the desired Var fixup by abusing the join_references + * We can perform the desired Var fixup by abusing the fix_join_expr * machinery that normally handles inner indexscan fixup. We search the * top plan's targetlist for Vars of non-result relations, and use - * join_references to convert RETURNING Vars into references to those - * tlist entries, while leaving result-rel Vars as-is. + * fix_join_expr to convert RETURNING Vars into references to those tlist + * entries, while leaving result-rel Vars as-is. + * + * PlaceHolderVars will also be sought in the targetlist, but no + * more-complex expressions will be. Note that it is not possible for a + * PlaceHolderVar to refer to the result relation, since the result is + * never below an outer join. If that case could happen, we'd have to be + * prepared to pick apart the PlaceHolderVar and evaluate its contained + * expression instead. */ itlist = build_tlist_index_other_vars(topplan->targetlist, resultRelation); - rlist = join_references(rlist, - itlist, - NULL, - resultRelation); - - fix_expr_references(topplan, (Node *) rlist); + rlist = fix_join_expr(glob, + rlist, + itlist, + NULL, + resultRelation, + 0); pfree(itlist); @@ -1407,10 +1822,10 @@ fix_opfuncids_walker(Node *node, void *context) set_opfuncid((OpExpr *) node); else if (IsA(node, DistinctExpr)) set_opfuncid((OpExpr *) node); /* rely on struct equivalence */ - else if (IsA(node, ScalarArrayOpExpr)) - set_sa_opfuncid((ScalarArrayOpExpr *) node); else if (IsA(node, NullIfExpr)) set_opfuncid((OpExpr *) node); /* rely on struct equivalence */ + else if (IsA(node, ScalarArrayOpExpr)) + set_sa_opfuncid((ScalarArrayOpExpr *) node); return expression_tree_walker(node, fix_opfuncids_walker, context); } @@ -1433,9 +1848,129 @@ set_opfuncid(OpExpr *opexpr) * set_sa_opfuncid * As above, for ScalarArrayOpExpr nodes. */ -static void +void set_sa_opfuncid(ScalarArrayOpExpr *opexpr) { if (opexpr->opfuncid == InvalidOid) opexpr->opfuncid = get_opcode(opexpr->opno); } + +/***************************************************************************** + * QUERY DEPENDENCY MANAGEMENT + *****************************************************************************/ + +/* + * record_plan_function_dependency + * Mark the current plan as depending on a particular function. + * + * This is exported so that the function-inlining code can record a + * dependency on a function that it's removed from the plan tree. + */ +void +record_plan_function_dependency(PlannerGlobal *glob, Oid funcid) +{ + /* + * For performance reasons, we don't bother to track built-in functions; + * we just assume they'll never change (or at least not in ways that'd + * invalidate plans using them). For this purpose we can consider a + * built-in function to be one with OID less than FirstBootstrapObjectId. + * Note that the OID generator guarantees never to generate such an OID + * after startup, even at OID wraparound. + */ + if (funcid >= (Oid) FirstBootstrapObjectId) + { + HeapTuple func_tuple; + PlanInvalItem *inval_item; + + func_tuple = SearchSysCache(PROCOID, + ObjectIdGetDatum(funcid), + 0, 0, 0); + if (!HeapTupleIsValid(func_tuple)) + elog(ERROR, "cache lookup failed for function %u", funcid); + + inval_item = makeNode(PlanInvalItem); + + /* + * It would work to use any syscache on pg_proc, but plancache.c + * expects us to use PROCOID. + */ + inval_item->cacheId = PROCOID; + inval_item->tupleId = func_tuple->t_self; + + glob->invalItems = lappend(glob->invalItems, inval_item); + + ReleaseSysCache(func_tuple); + } +} + +/* + * extract_query_dependencies + * Given a not-yet-planned query or queries (i.e. a Query node or list + * of Query nodes), extract dependencies just as set_plan_references + * would do. + * + * This is needed by plancache.c to handle invalidation of cached unplanned + * queries. + */ +void +extract_query_dependencies(Node *query, + List **relationOids, + List **invalItems) +{ + PlannerGlobal glob; + + /* Make up a dummy PlannerGlobal 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); + + *relationOids = glob.relationOids; + *invalItems = glob.invalItems; +} + +static bool +extract_query_dependencies_walker(Node *node, PlannerGlobal *context) +{ + if (node == NULL) + return false; + Assert(!IsA(node, PlaceHolderVar)); + /* Extract function dependencies and check for regclass Consts */ + fix_expr_common(context, node); + if (IsA(node, Query)) + { + Query *query = (Query *) node; + ListCell *lc; + + if (query->commandType == CMD_UTILITY) + { + /* Ignore utility statements, except EXPLAIN */ + if (IsA(query->utilityStmt, ExplainStmt)) + { + query = (Query *) ((ExplainStmt *) query->utilityStmt)->query; + Assert(IsA(query, Query)); + Assert(query->commandType != CMD_UTILITY); + } + else + return false; + } + + /* Collect relation OIDs in this Query's rtable */ + foreach(lc, query->rtable) + { + RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc); + + if (rte->rtekind == RTE_RELATION) + context->relationOids = lappend_oid(context->relationOids, + rte->relid); + } + + /* And recurse into the query's subexpressions */ + return query_tree_walker(query, extract_query_dependencies_walker, + (void *) context, 0); + } + return expression_tree_walker(node, extract_query_dependencies_walker, + (void *) context); +}