1 /*-------------------------------------------------------------------------
4 * Post-processing of a completed plan tree: fix references to subplan
5 * vars, compute regproc values for operators, etc
7 * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * src/backend/optimizer/plan/setrefs.c
14 *-------------------------------------------------------------------------
18 #include "access/transam.h"
19 #include "catalog/pg_type.h"
20 #include "nodes/makefuncs.h"
21 #include "nodes/nodeFuncs.h"
22 #include "optimizer/pathnode.h"
23 #include "optimizer/planmain.h"
24 #include "optimizer/planner.h"
25 #include "optimizer/tlist.h"
26 #include "tcop/utility.h"
27 #include "utils/lsyscache.h"
28 #include "utils/syscache.h"
33 Index varno; /* RT index of Var */
34 AttrNumber varattno; /* attr number of Var */
35 AttrNumber resno; /* TLE position of Var */
40 List *tlist; /* underlying target list */
41 int num_vars; /* number of plain Var tlist entries */
42 bool has_ph_vars; /* are there PlaceHolderVar entries? */
43 bool has_non_vars; /* are there other entries? */
44 tlist_vinfo vars[FLEXIBLE_ARRAY_MEMBER]; /* has num_vars entries */
51 } fix_scan_expr_context;
56 indexed_tlist *outer_itlist;
57 indexed_tlist *inner_itlist;
60 } fix_join_expr_context;
65 indexed_tlist *subplan_itlist;
68 } fix_upper_expr_context;
71 * Check if a Const node is a regclass value. We accept plain OID too,
72 * since a regclass Const will get folded to that type if it's an argument
73 * to oideq or similar operators. (This might result in some extraneous
74 * values in a plan's list of relation dependencies, but the worst result
75 * would be occasional useless replans.)
77 #define ISREGCLASSCONST(con) \
78 (((con)->consttype == REGCLASSOID || (con)->consttype == OIDOID) && \
81 #define fix_scan_list(root, lst, rtoffset) \
82 ((List *) fix_scan_expr(root, (Node *) (lst), rtoffset))
84 static void add_rtes_to_flat_rtable(PlannerInfo *root, bool recursing);
85 static void flatten_unplanned_rtes(PlannerGlobal *glob, RangeTblEntry *rte);
86 static bool flatten_rtes_walker(Node *node, PlannerGlobal *glob);
87 static void add_rte_to_flat_rtable(PlannerGlobal *glob, RangeTblEntry *rte);
88 static Plan *set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset);
89 static Plan *set_indexonlyscan_references(PlannerInfo *root,
92 static Plan *set_subqueryscan_references(PlannerInfo *root,
95 static bool trivial_subqueryscan(SubqueryScan *plan);
96 static Node *fix_scan_expr(PlannerInfo *root, Node *node, int rtoffset);
97 static Node *fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context);
98 static bool fix_scan_expr_walker(Node *node, fix_scan_expr_context *context);
99 static void set_join_references(PlannerInfo *root, Join *join, int rtoffset);
100 static void set_upper_references(PlannerInfo *root, Plan *plan, int rtoffset);
101 static void set_dummy_tlist_references(Plan *plan, int rtoffset);
102 static indexed_tlist *build_tlist_index(List *tlist);
103 static Var *search_indexed_tlist_for_var(Var *var,
104 indexed_tlist *itlist,
107 static Var *search_indexed_tlist_for_non_var(Node *node,
108 indexed_tlist *itlist,
110 static Var *search_indexed_tlist_for_sortgroupref(Node *node,
112 indexed_tlist *itlist,
114 static List *fix_join_expr(PlannerInfo *root,
116 indexed_tlist *outer_itlist,
117 indexed_tlist *inner_itlist,
118 Index acceptable_rel, int rtoffset);
119 static Node *fix_join_expr_mutator(Node *node,
120 fix_join_expr_context *context);
121 static Node *fix_upper_expr(PlannerInfo *root,
123 indexed_tlist *subplan_itlist,
126 static Node *fix_upper_expr_mutator(Node *node,
127 fix_upper_expr_context *context);
128 static List *set_returning_clause_references(PlannerInfo *root,
131 Index resultRelation,
133 static bool fix_opfuncids_walker(Node *node, void *context);
134 static bool extract_query_dependencies_walker(Node *node,
135 PlannerInfo *context);
138 /*****************************************************************************
142 *****************************************************************************/
145 * set_plan_references
147 * This is the final processing pass of the planner/optimizer. The plan
148 * tree is complete; we just have to adjust some representational details
149 * for the convenience of the executor:
151 * 1. We flatten the various subquery rangetables into a single list, and
152 * zero out RangeTblEntry fields that are not useful to the executor.
154 * 2. We adjust Vars in scan nodes to be consistent with the flat rangetable.
156 * 3. We adjust Vars in upper plan nodes to refer to the outputs of their
159 * 4. PARAM_MULTIEXPR Params are replaced by regular PARAM_EXEC Params,
160 * now that we have finished planning all MULTIEXPR subplans.
162 * 5. We compute regproc OIDs for operators (ie, we look up the function
163 * that implements each op).
165 * 6. We create lists of specific objects that the plan depends on.
166 * This will be used by plancache.c to drive invalidation of cached plans.
167 * Relation dependencies are represented by OIDs, and everything else by
168 * PlanInvalItems (this distinction is motivated by the shared-inval APIs).
169 * Currently, relations and user-defined functions are the only types of
170 * objects that are explicitly tracked this way.
172 * We also perform one final optimization step, which is to delete
173 * SubqueryScan plan nodes that aren't doing anything useful (ie, have
174 * no qual and a no-op targetlist). The reason for doing this last is that
175 * it can't readily be done before set_plan_references, because it would
176 * break set_upper_references: the Vars in the subquery's top tlist
177 * wouldn't match up with the Vars in the outer plan tree. The SubqueryScan
178 * serves a necessary function as a buffer between outer query and subquery
179 * variable numbering ... but after we've flattened the rangetable this is
180 * no longer a problem, since then there's only one rtindex namespace.
182 * set_plan_references recursively traverses the whole plan tree.
184 * The return value is normally the same Plan node passed in, but can be
185 * different when the passed-in Plan is a SubqueryScan we decide isn't needed.
187 * The flattened rangetable entries are appended to root->glob->finalrtable.
188 * Also, rowmarks entries are appended to root->glob->finalrowmarks, and the
189 * RT indexes of ModifyTable result relations to root->glob->resultRelations.
190 * Plan dependencies are appended to root->glob->relationOids (for relations)
191 * and root->glob->invalItems (for everything else).
193 * Notice that we modify Plan nodes in-place, but use expression_tree_mutator
194 * to process targetlist and qual expressions. We can assume that the Plan
195 * nodes were just built by the planner and are not multiply referenced, but
196 * it's not so safe to assume that for expression tree nodes.
199 set_plan_references(PlannerInfo *root, Plan *plan)
201 PlannerGlobal *glob = root->glob;
202 int rtoffset = list_length(glob->finalrtable);
206 * Add all the query's RTEs to the flattened rangetable. The live ones
207 * will have their rangetable indexes increased by rtoffset. (Additional
208 * RTEs, not referenced by the Plan tree, might get added after those.)
210 add_rtes_to_flat_rtable(root, false);
213 * Adjust RT indexes of PlanRowMarks and add to final rowmarks list
215 foreach(lc, root->rowMarks)
217 PlanRowMark *rc = (PlanRowMark *) lfirst(lc);
220 Assert(IsA(rc, PlanRowMark));
222 /* flat copy is enough since all fields are scalars */
223 newrc = (PlanRowMark *) palloc(sizeof(PlanRowMark));
224 memcpy(newrc, rc, sizeof(PlanRowMark));
226 /* adjust indexes ... but *not* the rowmarkId */
227 newrc->rti += rtoffset;
228 newrc->prti += rtoffset;
230 glob->finalrowmarks = lappend(glob->finalrowmarks, newrc);
233 /* Now fix the Plan tree */
234 return set_plan_refs(root, plan, rtoffset);
238 * Extract RangeTblEntries from the plan's rangetable, and add to flat rtable
240 * This can recurse into subquery plans; "recursing" is true if so.
243 add_rtes_to_flat_rtable(PlannerInfo *root, bool recursing)
245 PlannerGlobal *glob = root->glob;
250 * Add the query's own RTEs to the flattened rangetable.
252 * At top level, we must add all RTEs so that their indexes in the
253 * flattened rangetable match up with their original indexes. When
254 * recursing, we only care about extracting relation RTEs.
256 foreach(lc, root->parse->rtable)
258 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
260 if (!recursing || rte->rtekind == RTE_RELATION)
261 add_rte_to_flat_rtable(glob, rte);
265 * If there are any dead subqueries, they are not referenced in the Plan
266 * tree, so we must add RTEs contained in them to the flattened rtable
267 * separately. (If we failed to do this, the executor would not perform
268 * expected permission checks for tables mentioned in such subqueries.)
270 * Note: this pass over the rangetable can't be combined with the previous
271 * one, because that would mess up the numbering of the live RTEs in the
272 * flattened rangetable.
275 foreach(lc, root->parse->rtable)
277 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
280 * We should ignore inheritance-parent RTEs: their contents have been
281 * pulled up into our rangetable already. Also ignore any subquery
282 * RTEs without matching RelOptInfos, as they likewise have been
285 if (rte->rtekind == RTE_SUBQUERY && !rte->inh &&
286 rti < root->simple_rel_array_size)
288 RelOptInfo *rel = root->simple_rel_array[rti];
292 Assert(rel->relid == rti); /* sanity check on array */
295 * The subquery might never have been planned at all, if it
296 * was excluded on the basis of self-contradictory constraints
297 * in our query level. In this case apply
298 * flatten_unplanned_rtes.
300 * If it was planned but the plan is dummy, we assume that it
301 * has been omitted from our plan tree (see
302 * set_subquery_pathlist), and recurse to pull up its RTEs.
304 * Otherwise, it should be represented by a SubqueryScan node
305 * somewhere in our plan tree, and we'll pull up its RTEs when
306 * we process that plan node.
308 * However, if we're recursing, then we should pull up RTEs
309 * whether the subplan is dummy or not, because we've found
310 * that some upper query level is treating this one as dummy,
311 * and so we won't scan this level's plan tree at all.
313 if (rel->subplan == NULL)
314 flatten_unplanned_rtes(glob, rte);
315 else if (recursing || is_dummy_plan(rel->subplan))
317 Assert(rel->subroot != NULL);
318 add_rtes_to_flat_rtable(rel->subroot, true);
327 * Extract RangeTblEntries from a subquery that was never planned at all
330 flatten_unplanned_rtes(PlannerGlobal *glob, RangeTblEntry *rte)
332 /* Use query_tree_walker to find all RTEs in the parse tree */
333 (void) query_tree_walker(rte->subquery,
340 flatten_rtes_walker(Node *node, PlannerGlobal *glob)
344 if (IsA(node, RangeTblEntry))
346 RangeTblEntry *rte = (RangeTblEntry *) node;
348 /* As above, we need only save relation RTEs */
349 if (rte->rtekind == RTE_RELATION)
350 add_rte_to_flat_rtable(glob, rte);
353 if (IsA(node, Query))
355 /* Recurse into subselects */
356 return query_tree_walker((Query *) node,
361 return expression_tree_walker(node, flatten_rtes_walker,
366 * Add (a copy of) the given RTE to the final rangetable
368 * In the flat rangetable, we zero out substructure pointers that are not
369 * needed by the executor; this reduces the storage space and copying cost
370 * for cached plans. We keep only the alias and eref Alias fields, which
371 * are needed by EXPLAIN, and the selectedCols and modifiedCols bitmaps,
372 * which are needed for executor-startup permissions checking and for
373 * trigger event checking.
376 add_rte_to_flat_rtable(PlannerGlobal *glob, RangeTblEntry *rte)
378 RangeTblEntry *newrte;
380 /* flat copy to duplicate all the scalar fields */
381 newrte = (RangeTblEntry *) palloc(sizeof(RangeTblEntry));
382 memcpy(newrte, rte, sizeof(RangeTblEntry));
384 /* zap unneeded sub-structure */
385 newrte->subquery = NULL;
386 newrte->joinaliasvars = NIL;
387 newrte->functions = NIL;
388 newrte->values_lists = NIL;
389 newrte->values_collations = NIL;
390 newrte->ctecoltypes = NIL;
391 newrte->ctecoltypmods = NIL;
392 newrte->ctecolcollations = NIL;
394 glob->finalrtable = lappend(glob->finalrtable, newrte);
397 * Check for RT index overflow; it's very unlikely, but if it did happen,
398 * the executor would get confused by varnos that match the special varno
401 if (IS_SPECIAL_VARNO(list_length(glob->finalrtable)))
403 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
404 errmsg("too many range table entries")));
407 * If it's a plain relation RTE, add the table to relationOids.
409 * We do this even though the RTE might be unreferenced in the plan tree;
410 * this would correspond to cases such as views that were expanded, child
411 * tables that were eliminated by constraint exclusion, etc. Schema
412 * invalidation on such a rel must still force rebuilding of the plan.
414 * Note we don't bother to avoid making duplicate list entries. We could,
415 * but it would probably cost more cycles than it would save.
417 if (newrte->rtekind == RTE_RELATION)
418 glob->relationOids = lappend_oid(glob->relationOids, newrte->relid);
422 * set_plan_refs: recurse through the Plan nodes of a single subquery level
425 set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
433 * Plan-type-specific fixes
435 switch (nodeTag(plan))
439 SeqScan *splan = (SeqScan *) plan;
441 splan->scanrelid += rtoffset;
442 splan->plan.targetlist =
443 fix_scan_list(root, splan->plan.targetlist, rtoffset);
445 fix_scan_list(root, splan->plan.qual, rtoffset);
450 IndexScan *splan = (IndexScan *) plan;
452 splan->scan.scanrelid += rtoffset;
453 splan->scan.plan.targetlist =
454 fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
455 splan->scan.plan.qual =
456 fix_scan_list(root, splan->scan.plan.qual, rtoffset);
458 fix_scan_list(root, splan->indexqual, rtoffset);
459 splan->indexqualorig =
460 fix_scan_list(root, splan->indexqualorig, rtoffset);
461 splan->indexorderby =
462 fix_scan_list(root, splan->indexorderby, rtoffset);
463 splan->indexorderbyorig =
464 fix_scan_list(root, splan->indexorderbyorig, rtoffset);
467 case T_IndexOnlyScan:
469 IndexOnlyScan *splan = (IndexOnlyScan *) plan;
471 return set_indexonlyscan_references(root, splan, rtoffset);
474 case T_BitmapIndexScan:
476 BitmapIndexScan *splan = (BitmapIndexScan *) plan;
478 splan->scan.scanrelid += rtoffset;
479 /* no need to fix targetlist and qual */
480 Assert(splan->scan.plan.targetlist == NIL);
481 Assert(splan->scan.plan.qual == NIL);
483 fix_scan_list(root, splan->indexqual, rtoffset);
484 splan->indexqualorig =
485 fix_scan_list(root, splan->indexqualorig, rtoffset);
488 case T_BitmapHeapScan:
490 BitmapHeapScan *splan = (BitmapHeapScan *) plan;
492 splan->scan.scanrelid += rtoffset;
493 splan->scan.plan.targetlist =
494 fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
495 splan->scan.plan.qual =
496 fix_scan_list(root, splan->scan.plan.qual, rtoffset);
497 splan->bitmapqualorig =
498 fix_scan_list(root, splan->bitmapqualorig, rtoffset);
503 TidScan *splan = (TidScan *) plan;
505 splan->scan.scanrelid += rtoffset;
506 splan->scan.plan.targetlist =
507 fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
508 splan->scan.plan.qual =
509 fix_scan_list(root, splan->scan.plan.qual, rtoffset);
511 fix_scan_list(root, splan->tidquals, rtoffset);
515 /* Needs special treatment, see comments below */
516 return set_subqueryscan_references(root,
517 (SubqueryScan *) plan,
521 FunctionScan *splan = (FunctionScan *) plan;
523 splan->scan.scanrelid += rtoffset;
524 splan->scan.plan.targetlist =
525 fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
526 splan->scan.plan.qual =
527 fix_scan_list(root, splan->scan.plan.qual, rtoffset);
529 fix_scan_list(root, splan->functions, rtoffset);
534 ValuesScan *splan = (ValuesScan *) plan;
536 splan->scan.scanrelid += rtoffset;
537 splan->scan.plan.targetlist =
538 fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
539 splan->scan.plan.qual =
540 fix_scan_list(root, splan->scan.plan.qual, rtoffset);
541 splan->values_lists =
542 fix_scan_list(root, splan->values_lists, rtoffset);
547 CteScan *splan = (CteScan *) plan;
549 splan->scan.scanrelid += rtoffset;
550 splan->scan.plan.targetlist =
551 fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
552 splan->scan.plan.qual =
553 fix_scan_list(root, splan->scan.plan.qual, rtoffset);
556 case T_WorkTableScan:
558 WorkTableScan *splan = (WorkTableScan *) plan;
560 splan->scan.scanrelid += rtoffset;
561 splan->scan.plan.targetlist =
562 fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
563 splan->scan.plan.qual =
564 fix_scan_list(root, splan->scan.plan.qual, rtoffset);
569 ForeignScan *splan = (ForeignScan *) plan;
571 splan->scan.scanrelid += rtoffset;
572 splan->scan.plan.targetlist =
573 fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
574 splan->scan.plan.qual =
575 fix_scan_list(root, splan->scan.plan.qual, rtoffset);
577 fix_scan_list(root, splan->fdw_exprs, rtoffset);
583 CustomScan *splan = (CustomScan *) plan;
585 splan->scan.scanrelid += rtoffset;
586 splan->scan.plan.targetlist =
587 fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
588 splan->scan.plan.qual =
589 fix_scan_list(root, splan->scan.plan.qual, rtoffset);
590 splan->custom_exprs =
591 fix_scan_list(root, splan->custom_exprs, rtoffset);
598 set_join_references(root, (Join *) plan, rtoffset);
608 * These plan types don't actually bother to evaluate their
609 * targetlists, because they just return their unmodified input
610 * tuples. Even though the targetlist won't be used by the
611 * executor, we fix it up for possible use by EXPLAIN (not to
612 * mention ease of debugging --- wrong varnos are very confusing).
614 set_dummy_tlist_references(plan, rtoffset);
617 * Since these plan types don't check quals either, we should not
618 * find any qual expression attached to them.
620 Assert(plan->qual == NIL);
624 LockRows *splan = (LockRows *) plan;
627 * Like the plan types above, LockRows doesn't evaluate its
628 * tlist or quals. But we have to fix up the RT indexes in
631 set_dummy_tlist_references(plan, rtoffset);
632 Assert(splan->plan.qual == NIL);
634 foreach(l, splan->rowMarks)
636 PlanRowMark *rc = (PlanRowMark *) lfirst(l);
639 rc->prti += rtoffset;
645 Limit *splan = (Limit *) plan;
648 * Like the plan types above, Limit doesn't evaluate its tlist
649 * or quals. It does have live expressions for limit/offset,
650 * however; and those cannot contain subplan variable refs, so
651 * fix_scan_expr works for them.
653 set_dummy_tlist_references(plan, rtoffset);
654 Assert(splan->plan.qual == NIL);
657 fix_scan_expr(root, splan->limitOffset, rtoffset);
659 fix_scan_expr(root, splan->limitCount, rtoffset);
664 set_upper_references(root, plan, rtoffset);
668 WindowAgg *wplan = (WindowAgg *) plan;
670 set_upper_references(root, plan, rtoffset);
673 * Like Limit node limit/offset expressions, WindowAgg has
674 * frame offset expressions, which cannot contain subplan
675 * variable refs, so fix_scan_expr works for them.
678 fix_scan_expr(root, wplan->startOffset, rtoffset);
680 fix_scan_expr(root, wplan->endOffset, rtoffset);
685 Result *splan = (Result *) plan;
688 * Result may or may not have a subplan; if not, it's more
689 * like a scan node than an upper node.
691 if (splan->plan.lefttree != NULL)
692 set_upper_references(root, plan, rtoffset);
695 splan->plan.targetlist =
696 fix_scan_list(root, splan->plan.targetlist, rtoffset);
698 fix_scan_list(root, splan->plan.qual, rtoffset);
700 /* resconstantqual can't contain any subplan variable refs */
701 splan->resconstantqual =
702 fix_scan_expr(root, splan->resconstantqual, rtoffset);
707 ModifyTable *splan = (ModifyTable *) plan;
709 Assert(splan->plan.targetlist == NIL);
710 Assert(splan->plan.qual == NIL);
712 splan->withCheckOptionLists =
713 fix_scan_list(root, splan->withCheckOptionLists, rtoffset);
715 if (splan->returningLists)
723 * Pass each per-subplan returningList through
724 * set_returning_clause_references().
726 Assert(list_length(splan->returningLists) == list_length(splan->resultRelations));
727 Assert(list_length(splan->returningLists) == list_length(splan->plans));
728 forthree(lcrl, splan->returningLists,
729 lcrr, splan->resultRelations,
732 List *rlist = (List *) lfirst(lcrl);
733 Index resultrel = lfirst_int(lcrr);
734 Plan *subplan = (Plan *) lfirst(lcp);
736 rlist = set_returning_clause_references(root,
741 newRL = lappend(newRL, rlist);
743 splan->returningLists = newRL;
746 * Set up the visible plan targetlist as being the same as
747 * the first RETURNING list. This is for the use of
748 * EXPLAIN; the executor won't pay any attention to the
749 * targetlist. We postpone this step until here so that
750 * we don't have to do set_returning_clause_references()
751 * twice on identical targetlists.
753 splan->plan.targetlist = copyObject(linitial(newRL));
756 splan->nominalRelation += rtoffset;
758 foreach(l, splan->resultRelations)
760 lfirst_int(l) += rtoffset;
762 foreach(l, splan->rowMarks)
764 PlanRowMark *rc = (PlanRowMark *) lfirst(l);
767 rc->prti += rtoffset;
769 foreach(l, splan->plans)
771 lfirst(l) = set_plan_refs(root,
777 * Append this ModifyTable node's final result relation RT
778 * index(es) to the global list for the plan, and set its
779 * resultRelIndex to reflect their starting position in the
782 splan->resultRelIndex = list_length(root->glob->resultRelations);
783 root->glob->resultRelations =
784 list_concat(root->glob->resultRelations,
785 list_copy(splan->resultRelations));
790 Append *splan = (Append *) plan;
793 * Append, like Sort et al, doesn't actually evaluate its
794 * targetlist or check quals.
796 set_dummy_tlist_references(plan, rtoffset);
797 Assert(splan->plan.qual == NIL);
798 foreach(l, splan->appendplans)
800 lfirst(l) = set_plan_refs(root,
808 MergeAppend *splan = (MergeAppend *) plan;
811 * MergeAppend, like Sort et al, doesn't actually evaluate its
812 * targetlist or check quals.
814 set_dummy_tlist_references(plan, rtoffset);
815 Assert(splan->plan.qual == NIL);
816 foreach(l, splan->mergeplans)
818 lfirst(l) = set_plan_refs(root,
824 case T_RecursiveUnion:
825 /* This doesn't evaluate targetlist or check quals either */
826 set_dummy_tlist_references(plan, rtoffset);
827 Assert(plan->qual == NIL);
831 BitmapAnd *splan = (BitmapAnd *) plan;
833 /* BitmapAnd works like Append, but has no tlist */
834 Assert(splan->plan.targetlist == NIL);
835 Assert(splan->plan.qual == NIL);
836 foreach(l, splan->bitmapplans)
838 lfirst(l) = set_plan_refs(root,
846 BitmapOr *splan = (BitmapOr *) plan;
848 /* BitmapOr works like Append, but has no tlist */
849 Assert(splan->plan.targetlist == NIL);
850 Assert(splan->plan.qual == NIL);
851 foreach(l, splan->bitmapplans)
853 lfirst(l) = set_plan_refs(root,
860 elog(ERROR, "unrecognized node type: %d",
861 (int) nodeTag(plan));
866 * Now recurse into child plans, if any
868 * NOTE: it is essential that we recurse into child plans AFTER we set
869 * subplan references in this plan's tlist and quals. If we did the
870 * reference-adjustments bottom-up, then we would fail to match this
871 * plan's var nodes against the already-modified nodes of the children.
873 plan->lefttree = set_plan_refs(root, plan->lefttree, rtoffset);
874 plan->righttree = set_plan_refs(root, plan->righttree, rtoffset);
880 * set_indexonlyscan_references
881 * Do set_plan_references processing on an IndexOnlyScan
883 * This is unlike the handling of a plain IndexScan because we have to
884 * convert Vars referencing the heap into Vars referencing the index.
885 * We can use the fix_upper_expr machinery for that, by working from a
886 * targetlist describing the index columns.
889 set_indexonlyscan_references(PlannerInfo *root,
893 indexed_tlist *index_itlist;
895 index_itlist = build_tlist_index(plan->indextlist);
897 plan->scan.scanrelid += rtoffset;
898 plan->scan.plan.targetlist = (List *)
900 (Node *) plan->scan.plan.targetlist,
904 plan->scan.plan.qual = (List *)
906 (Node *) plan->scan.plan.qual,
910 /* indexqual is already transformed to reference index columns */
911 plan->indexqual = fix_scan_list(root, plan->indexqual, rtoffset);
912 /* indexorderby is already transformed to reference index columns */
913 plan->indexorderby = fix_scan_list(root, plan->indexorderby, rtoffset);
914 /* indextlist must NOT be transformed to reference index columns */
915 plan->indextlist = fix_scan_list(root, plan->indextlist, rtoffset);
919 return (Plan *) plan;
923 * set_subqueryscan_references
924 * Do set_plan_references processing on a SubqueryScan
926 * We try to strip out the SubqueryScan entirely; if we can't, we have
927 * to do the normal processing on it.
930 set_subqueryscan_references(PlannerInfo *root,
937 /* Need to look up the subquery's RelOptInfo, since we need its subroot */
938 rel = find_base_rel(root, plan->scan.scanrelid);
939 Assert(rel->subplan == plan->subplan);
941 /* Recursively process the subplan */
942 plan->subplan = set_plan_references(rel->subroot, plan->subplan);
944 if (trivial_subqueryscan(plan))
947 * We can omit the SubqueryScan node and just pull up the subplan.
952 result = plan->subplan;
954 /* We have to be sure we don't lose any initplans */
955 result->initPlan = list_concat(plan->scan.plan.initPlan,
959 * We also have to transfer the SubqueryScan's result-column names
960 * into the subplan, else columns sent to client will be improperly
961 * labeled if this is the topmost plan level. Copy the "source
962 * column" information too.
964 forboth(lp, plan->scan.plan.targetlist, lc, result->targetlist)
966 TargetEntry *ptle = (TargetEntry *) lfirst(lp);
967 TargetEntry *ctle = (TargetEntry *) lfirst(lc);
969 ctle->resname = ptle->resname;
970 ctle->resorigtbl = ptle->resorigtbl;
971 ctle->resorigcol = ptle->resorigcol;
977 * Keep the SubqueryScan node. We have to do the processing that
978 * set_plan_references would otherwise have done on it. Notice we do
979 * not do set_upper_references() here, because a SubqueryScan will
980 * always have been created with correct references to its subplan's
981 * outputs to begin with.
983 plan->scan.scanrelid += rtoffset;
984 plan->scan.plan.targetlist =
985 fix_scan_list(root, plan->scan.plan.targetlist, rtoffset);
986 plan->scan.plan.qual =
987 fix_scan_list(root, plan->scan.plan.qual, rtoffset);
989 result = (Plan *) plan;
996 * trivial_subqueryscan
997 * Detect whether a SubqueryScan can be deleted from the plan tree.
999 * We can delete it if it has no qual to check and the targetlist just
1000 * regurgitates the output of the child plan.
1003 trivial_subqueryscan(SubqueryScan *plan)
1009 if (plan->scan.plan.qual != NIL)
1012 if (list_length(plan->scan.plan.targetlist) !=
1013 list_length(plan->subplan->targetlist))
1014 return false; /* tlists not same length */
1017 forboth(lp, plan->scan.plan.targetlist, lc, plan->subplan->targetlist)
1019 TargetEntry *ptle = (TargetEntry *) lfirst(lp);
1020 TargetEntry *ctle = (TargetEntry *) lfirst(lc);
1022 if (ptle->resjunk != ctle->resjunk)
1023 return false; /* tlist doesn't match junk status */
1026 * We accept either a Var referencing the corresponding element of the
1027 * subplan tlist, or a Const equaling the subplan element. See
1028 * generate_setop_tlist() for motivation.
1030 if (ptle->expr && IsA(ptle->expr, Var))
1032 Var *var = (Var *) ptle->expr;
1034 Assert(var->varno == plan->scan.scanrelid);
1035 Assert(var->varlevelsup == 0);
1036 if (var->varattno != attrno)
1037 return false; /* out of order */
1039 else if (ptle->expr && IsA(ptle->expr, Const))
1041 if (!equal(ptle->expr, ctle->expr))
1057 * fix_scan_expr and friends do this enough times that it's worth having
1058 * a bespoke routine instead of using the generic copyObject() function.
1063 Var *newvar = (Var *) palloc(sizeof(Var));
1071 * Do generic set_plan_references processing on an expression node
1073 * This is code that is common to all variants of expression-fixing.
1074 * We must look up operator opcode info for OpExpr and related nodes,
1075 * add OIDs from regclass Const nodes into root->glob->relationOids, and
1076 * add catalog TIDs for user-defined functions into root->glob->invalItems.
1078 * We assume it's okay to update opcode info in-place. So this could possibly
1079 * scribble on the planner's input data structures, but it's OK.
1082 fix_expr_common(PlannerInfo *root, Node *node)
1084 /* We assume callers won't call us on a NULL pointer */
1085 if (IsA(node, Aggref))
1087 record_plan_function_dependency(root,
1088 ((Aggref *) node)->aggfnoid);
1090 else if (IsA(node, WindowFunc))
1092 record_plan_function_dependency(root,
1093 ((WindowFunc *) node)->winfnoid);
1095 else if (IsA(node, FuncExpr))
1097 record_plan_function_dependency(root,
1098 ((FuncExpr *) node)->funcid);
1100 else if (IsA(node, OpExpr))
1102 set_opfuncid((OpExpr *) node);
1103 record_plan_function_dependency(root,
1104 ((OpExpr *) node)->opfuncid);
1106 else if (IsA(node, DistinctExpr))
1108 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1109 record_plan_function_dependency(root,
1110 ((DistinctExpr *) node)->opfuncid);
1112 else if (IsA(node, NullIfExpr))
1114 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1115 record_plan_function_dependency(root,
1116 ((NullIfExpr *) node)->opfuncid);
1118 else if (IsA(node, ScalarArrayOpExpr))
1120 set_sa_opfuncid((ScalarArrayOpExpr *) node);
1121 record_plan_function_dependency(root,
1122 ((ScalarArrayOpExpr *) node)->opfuncid);
1124 else if (IsA(node, ArrayCoerceExpr))
1126 if (OidIsValid(((ArrayCoerceExpr *) node)->elemfuncid))
1127 record_plan_function_dependency(root,
1128 ((ArrayCoerceExpr *) node)->elemfuncid);
1130 else if (IsA(node, Const))
1132 Const *con = (Const *) node;
1134 /* Check for regclass reference */
1135 if (ISREGCLASSCONST(con))
1136 root->glob->relationOids =
1137 lappend_oid(root->glob->relationOids,
1138 DatumGetObjectId(con->constvalue));
1144 * Do set_plan_references processing on a Param
1146 * If it's a PARAM_MULTIEXPR, replace it with the appropriate Param from
1147 * root->multiexpr_params; otherwise no change is needed.
1148 * Just for paranoia's sake, we make a copy of the node in either case.
1151 fix_param_node(PlannerInfo *root, Param *p)
1153 if (p->paramkind == PARAM_MULTIEXPR)
1155 int subqueryid = p->paramid >> 16;
1156 int colno = p->paramid & 0xFFFF;
1159 if (subqueryid <= 0 ||
1160 subqueryid > list_length(root->multiexpr_params))
1161 elog(ERROR, "unexpected PARAM_MULTIEXPR ID: %d", p->paramid);
1162 params = (List *) list_nth(root->multiexpr_params, subqueryid - 1);
1163 if (colno <= 0 || colno > list_length(params))
1164 elog(ERROR, "unexpected PARAM_MULTIEXPR ID: %d", p->paramid);
1165 return copyObject(list_nth(params, colno - 1));
1167 return copyObject(p);
1172 * Do set_plan_references processing on a scan-level expression
1174 * This consists of incrementing all Vars' varnos by rtoffset,
1175 * replacing PARAM_MULTIEXPR Params, expanding PlaceHolderVars,
1176 * looking up operator opcode info for OpExpr and related nodes,
1177 * and adding OIDs from regclass Const nodes into root->glob->relationOids.
1180 fix_scan_expr(PlannerInfo *root, Node *node, int rtoffset)
1182 fix_scan_expr_context context;
1184 context.root = root;
1185 context.rtoffset = rtoffset;
1187 if (rtoffset != 0 ||
1188 root->multiexpr_params != NIL ||
1189 root->glob->lastPHId != 0)
1191 return fix_scan_expr_mutator(node, &context);
1196 * If rtoffset == 0, we don't need to change any Vars, and if there
1197 * are no MULTIEXPR subqueries then we don't need to replace
1198 * PARAM_MULTIEXPR Params, and if there are no placeholders anywhere
1199 * we won't need to remove them. Then it's OK to just scribble on the
1200 * input node tree instead of copying (since the only change, filling
1201 * in any unset opfuncid fields, is harmless). This saves just enough
1202 * cycles to be noticeable on trivial queries.
1204 (void) fix_scan_expr_walker(node, &context);
1210 fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context)
1216 Var *var = copyVar((Var *) node);
1218 Assert(var->varlevelsup == 0);
1221 * We should not see any Vars marked INNER_VAR or OUTER_VAR. But an
1222 * indexqual expression could contain INDEX_VAR Vars.
1224 Assert(var->varno != INNER_VAR);
1225 Assert(var->varno != OUTER_VAR);
1226 if (!IS_SPECIAL_VARNO(var->varno))
1227 var->varno += context->rtoffset;
1228 if (var->varnoold > 0)
1229 var->varnoold += context->rtoffset;
1230 return (Node *) var;
1232 if (IsA(node, Param))
1233 return fix_param_node(context->root, (Param *) node);
1234 if (IsA(node, CurrentOfExpr))
1236 CurrentOfExpr *cexpr = (CurrentOfExpr *) copyObject(node);
1238 Assert(cexpr->cvarno != INNER_VAR);
1239 Assert(cexpr->cvarno != OUTER_VAR);
1240 if (!IS_SPECIAL_VARNO(cexpr->cvarno))
1241 cexpr->cvarno += context->rtoffset;
1242 return (Node *) cexpr;
1244 if (IsA(node, PlaceHolderVar))
1246 /* At scan level, we should always just evaluate the contained expr */
1247 PlaceHolderVar *phv = (PlaceHolderVar *) node;
1249 return fix_scan_expr_mutator((Node *) phv->phexpr, context);
1251 fix_expr_common(context->root, node);
1252 return expression_tree_mutator(node, fix_scan_expr_mutator,
1257 fix_scan_expr_walker(Node *node, fix_scan_expr_context *context)
1261 Assert(!IsA(node, PlaceHolderVar));
1262 fix_expr_common(context->root, node);
1263 return expression_tree_walker(node, fix_scan_expr_walker,
1268 * set_join_references
1269 * Modify the target list and quals of a join node to reference its
1270 * subplans, by setting the varnos to OUTER_VAR or INNER_VAR and setting
1271 * attno values to the result domain number of either the corresponding
1272 * outer or inner join tuple item. Also perform opcode lookup for these
1273 * expressions. and add regclass OIDs to root->glob->relationOids.
1276 set_join_references(PlannerInfo *root, Join *join, int rtoffset)
1278 Plan *outer_plan = join->plan.lefttree;
1279 Plan *inner_plan = join->plan.righttree;
1280 indexed_tlist *outer_itlist;
1281 indexed_tlist *inner_itlist;
1283 outer_itlist = build_tlist_index(outer_plan->targetlist);
1284 inner_itlist = build_tlist_index(inner_plan->targetlist);
1287 * First process the joinquals (including merge or hash clauses). These
1288 * are logically below the join so they can always use all values
1289 * available from the input tlists. It's okay to also handle
1290 * NestLoopParams now, because those couldn't refer to nullable
1293 join->joinqual = fix_join_expr(root,
1300 /* Now do join-type-specific stuff */
1301 if (IsA(join, NestLoop))
1303 NestLoop *nl = (NestLoop *) join;
1306 foreach(lc, nl->nestParams)
1308 NestLoopParam *nlp = (NestLoopParam *) lfirst(lc);
1310 nlp->paramval = (Var *) fix_upper_expr(root,
1311 (Node *) nlp->paramval,
1315 /* Check we replaced any PlaceHolderVar with simple Var */
1316 if (!(IsA(nlp->paramval, Var) &&
1317 nlp->paramval->varno == OUTER_VAR))
1318 elog(ERROR, "NestLoopParam was not reduced to a simple Var");
1321 else if (IsA(join, MergeJoin))
1323 MergeJoin *mj = (MergeJoin *) join;
1325 mj->mergeclauses = fix_join_expr(root,
1332 else if (IsA(join, HashJoin))
1334 HashJoin *hj = (HashJoin *) join;
1336 hj->hashclauses = fix_join_expr(root,
1345 * Now we need to fix up the targetlist and qpqual, which are logically
1346 * above the join. This means they should not re-use any input expression
1347 * that was computed in the nullable side of an outer join. Vars and
1348 * PlaceHolderVars are fine, so we can implement this restriction just by
1349 * clearing has_non_vars in the indexed_tlist structs.
1351 * XXX This is a grotty workaround for the fact that we don't clearly
1352 * distinguish between a Var appearing below an outer join and the "same"
1353 * Var appearing above it. If we did, we'd not need to hack the matching
1356 switch (join->jointype)
1361 inner_itlist->has_non_vars = false;
1364 outer_itlist->has_non_vars = false;
1367 outer_itlist->has_non_vars = false;
1368 inner_itlist->has_non_vars = false;
1374 join->plan.targetlist = fix_join_expr(root,
1375 join->plan.targetlist,
1380 join->plan.qual = fix_join_expr(root,
1387 pfree(outer_itlist);
1388 pfree(inner_itlist);
1392 * set_upper_references
1393 * Update the targetlist and quals of an upper-level plan node
1394 * to refer to the tuples returned by its lefttree subplan.
1395 * Also perform opcode lookup for these expressions, and
1396 * add regclass OIDs to root->glob->relationOids.
1398 * This is used for single-input plan types like Agg, Group, Result.
1400 * In most cases, we have to match up individual Vars in the tlist and
1401 * qual expressions with elements of the subplan's tlist (which was
1402 * generated by flatten_tlist() from these selfsame expressions, so it
1403 * should have all the required variables). There is an important exception,
1404 * however: GROUP BY and ORDER BY expressions will have been pushed into the
1405 * subplan tlist unflattened. If these values are also needed in the output
1406 * then we want to reference the subplan tlist element rather than recomputing
1410 set_upper_references(PlannerInfo *root, Plan *plan, int rtoffset)
1412 Plan *subplan = plan->lefttree;
1413 indexed_tlist *subplan_itlist;
1414 List *output_targetlist;
1417 subplan_itlist = build_tlist_index(subplan->targetlist);
1419 output_targetlist = NIL;
1420 foreach(l, plan->targetlist)
1422 TargetEntry *tle = (TargetEntry *) lfirst(l);
1425 /* If it's a non-Var sort/group item, first try to match by sortref */
1426 if (tle->ressortgroupref != 0 && !IsA(tle->expr, Var))
1429 search_indexed_tlist_for_sortgroupref((Node *) tle->expr,
1430 tle->ressortgroupref,
1434 newexpr = fix_upper_expr(root,
1441 newexpr = fix_upper_expr(root,
1446 tle = flatCopyTargetEntry(tle);
1447 tle->expr = (Expr *) newexpr;
1448 output_targetlist = lappend(output_targetlist, tle);
1450 plan->targetlist = output_targetlist;
1452 plan->qual = (List *)
1453 fix_upper_expr(root,
1454 (Node *) plan->qual,
1459 pfree(subplan_itlist);
1463 * set_dummy_tlist_references
1464 * Replace the targetlist of an upper-level plan node with a simple
1465 * list of OUTER_VAR references to its child.
1467 * This is used for plan types like Sort and Append that don't evaluate
1468 * their targetlists. Although the executor doesn't care at all what's in
1469 * the tlist, EXPLAIN needs it to be realistic.
1471 * Note: we could almost use set_upper_references() here, but it fails for
1472 * Append for lack of a lefttree subplan. Single-purpose code is faster
1476 set_dummy_tlist_references(Plan *plan, int rtoffset)
1478 List *output_targetlist;
1481 output_targetlist = NIL;
1482 foreach(l, plan->targetlist)
1484 TargetEntry *tle = (TargetEntry *) lfirst(l);
1485 Var *oldvar = (Var *) tle->expr;
1488 newvar = makeVar(OUTER_VAR,
1490 exprType((Node *) oldvar),
1491 exprTypmod((Node *) oldvar),
1492 exprCollation((Node *) oldvar),
1494 if (IsA(oldvar, Var))
1496 newvar->varnoold = oldvar->varno + rtoffset;
1497 newvar->varoattno = oldvar->varattno;
1501 newvar->varnoold = 0; /* wasn't ever a plain Var */
1502 newvar->varoattno = 0;
1505 tle = flatCopyTargetEntry(tle);
1506 tle->expr = (Expr *) newvar;
1507 output_targetlist = lappend(output_targetlist, tle);
1509 plan->targetlist = output_targetlist;
1511 /* We don't touch plan->qual here */
1516 * build_tlist_index --- build an index data structure for a child tlist
1518 * In most cases, subplan tlists will be "flat" tlists with only Vars,
1519 * so we try to optimize that case by extracting information about Vars
1520 * in advance. Matching a parent tlist to a child is still an O(N^2)
1521 * operation, but at least with a much smaller constant factor than plain
1522 * tlist_member() searches.
1524 * The result of this function is an indexed_tlist struct to pass to
1525 * search_indexed_tlist_for_var() or search_indexed_tlist_for_non_var().
1526 * When done, the indexed_tlist may be freed with a single pfree().
1528 static indexed_tlist *
1529 build_tlist_index(List *tlist)
1531 indexed_tlist *itlist;
1535 /* Create data structure with enough slots for all tlist entries */
1536 itlist = (indexed_tlist *)
1537 palloc(offsetof(indexed_tlist, vars) +
1538 list_length(tlist) * sizeof(tlist_vinfo));
1540 itlist->tlist = tlist;
1541 itlist->has_ph_vars = false;
1542 itlist->has_non_vars = false;
1544 /* Find the Vars and fill in the index array */
1545 vinfo = itlist->vars;
1548 TargetEntry *tle = (TargetEntry *) lfirst(l);
1550 if (tle->expr && IsA(tle->expr, Var))
1552 Var *var = (Var *) tle->expr;
1554 vinfo->varno = var->varno;
1555 vinfo->varattno = var->varattno;
1556 vinfo->resno = tle->resno;
1559 else if (tle->expr && IsA(tle->expr, PlaceHolderVar))
1560 itlist->has_ph_vars = true;
1562 itlist->has_non_vars = true;
1565 itlist->num_vars = (vinfo - itlist->vars);
1571 * build_tlist_index_other_vars --- build a restricted tlist index
1573 * This is like build_tlist_index, but we only index tlist entries that
1574 * are Vars belonging to some rel other than the one specified. We will set
1575 * has_ph_vars (allowing PlaceHolderVars to be matched), but not has_non_vars
1576 * (so nothing other than Vars and PlaceHolderVars can be matched).
1578 static indexed_tlist *
1579 build_tlist_index_other_vars(List *tlist, Index ignore_rel)
1581 indexed_tlist *itlist;
1585 /* Create data structure with enough slots for all tlist entries */
1586 itlist = (indexed_tlist *)
1587 palloc(offsetof(indexed_tlist, vars) +
1588 list_length(tlist) * sizeof(tlist_vinfo));
1590 itlist->tlist = tlist;
1591 itlist->has_ph_vars = false;
1592 itlist->has_non_vars = false;
1594 /* Find the desired Vars and fill in the index array */
1595 vinfo = itlist->vars;
1598 TargetEntry *tle = (TargetEntry *) lfirst(l);
1600 if (tle->expr && IsA(tle->expr, Var))
1602 Var *var = (Var *) tle->expr;
1604 if (var->varno != ignore_rel)
1606 vinfo->varno = var->varno;
1607 vinfo->varattno = var->varattno;
1608 vinfo->resno = tle->resno;
1612 else if (tle->expr && IsA(tle->expr, PlaceHolderVar))
1613 itlist->has_ph_vars = true;
1616 itlist->num_vars = (vinfo - itlist->vars);
1622 * search_indexed_tlist_for_var --- find a Var in an indexed tlist
1624 * If a match is found, return a copy of the given Var with suitably
1625 * modified varno/varattno (to wit, newvarno and the resno of the TLE entry).
1626 * Also ensure that varnoold is incremented by rtoffset.
1627 * If no match, return NULL.
1630 search_indexed_tlist_for_var(Var *var, indexed_tlist *itlist,
1631 Index newvarno, int rtoffset)
1633 Index varno = var->varno;
1634 AttrNumber varattno = var->varattno;
1638 vinfo = itlist->vars;
1639 i = itlist->num_vars;
1642 if (vinfo->varno == varno && vinfo->varattno == varattno)
1645 Var *newvar = copyVar(var);
1647 newvar->varno = newvarno;
1648 newvar->varattno = vinfo->resno;
1649 if (newvar->varnoold > 0)
1650 newvar->varnoold += rtoffset;
1655 return NULL; /* no match */
1659 * search_indexed_tlist_for_non_var --- find a non-Var in an indexed tlist
1661 * If a match is found, return a Var constructed to reference the tlist item.
1662 * If no match, return NULL.
1664 * NOTE: it is a waste of time to call this unless itlist->has_ph_vars or
1665 * itlist->has_non_vars. Furthermore, set_join_references() relies on being
1666 * able to prevent matching of non-Vars by clearing itlist->has_non_vars,
1667 * so there's a correctness reason not to call it unless that's set.
1670 search_indexed_tlist_for_non_var(Node *node,
1671 indexed_tlist *itlist, Index newvarno)
1675 tle = tlist_member(node, itlist->tlist);
1678 /* Found a matching subplan output expression */
1681 newvar = makeVarFromTargetEntry(newvarno, tle);
1682 newvar->varnoold = 0; /* wasn't ever a plain Var */
1683 newvar->varoattno = 0;
1686 return NULL; /* no match */
1690 * search_indexed_tlist_for_sortgroupref --- find a sort/group expression
1691 * (which is assumed not to be just a Var)
1693 * If a match is found, return a Var constructed to reference the tlist item.
1694 * If no match, return NULL.
1696 * This is needed to ensure that we select the right subplan TLE in cases
1697 * where there are multiple textually-equal()-but-volatile sort expressions.
1698 * And it's also faster than search_indexed_tlist_for_non_var.
1701 search_indexed_tlist_for_sortgroupref(Node *node,
1703 indexed_tlist *itlist,
1708 foreach(lc, itlist->tlist)
1710 TargetEntry *tle = (TargetEntry *) lfirst(lc);
1712 /* The equal() check should be redundant, but let's be paranoid */
1713 if (tle->ressortgroupref == sortgroupref &&
1714 equal(node, tle->expr))
1716 /* Found a matching subplan output expression */
1719 newvar = makeVarFromTargetEntry(newvarno, tle);
1720 newvar->varnoold = 0; /* wasn't ever a plain Var */
1721 newvar->varoattno = 0;
1725 return NULL; /* no match */
1730 * Create a new set of targetlist entries or join qual clauses by
1731 * changing the varno/varattno values of variables in the clauses
1732 * to reference target list values from the outer and inner join
1733 * relation target lists. Also perform opcode lookup and add
1734 * regclass OIDs to root->glob->relationOids.
1736 * This is used in two different scenarios: a normal join clause, where all
1737 * the Vars in the clause *must* be replaced by OUTER_VAR or INNER_VAR
1738 * references; and a RETURNING clause, which may contain both Vars of the
1739 * target relation and Vars of other relations. In the latter case we want
1740 * to replace the other-relation Vars by OUTER_VAR references, while leaving
1741 * target Vars alone.
1743 * For a normal join, acceptable_rel should be zero so that any failure to
1744 * match a Var will be reported as an error. For the RETURNING case, pass
1745 * inner_itlist = NULL and acceptable_rel = the ID of the target relation.
1747 * 'clauses' is the targetlist or list of join clauses
1748 * 'outer_itlist' is the indexed target list of the outer join relation
1749 * 'inner_itlist' is the indexed target list of the inner join relation,
1751 * 'acceptable_rel' is either zero or the rangetable index of a relation
1752 * whose Vars may appear in the clause without provoking an error
1753 * 'rtoffset': how much to increment varnoold by
1755 * Returns the new expression tree. The original clause structure is
1759 fix_join_expr(PlannerInfo *root,
1761 indexed_tlist *outer_itlist,
1762 indexed_tlist *inner_itlist,
1763 Index acceptable_rel,
1766 fix_join_expr_context context;
1768 context.root = root;
1769 context.outer_itlist = outer_itlist;
1770 context.inner_itlist = inner_itlist;
1771 context.acceptable_rel = acceptable_rel;
1772 context.rtoffset = rtoffset;
1773 return (List *) fix_join_expr_mutator((Node *) clauses, &context);
1777 fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
1785 Var *var = (Var *) node;
1787 /* First look for the var in the input tlists */
1788 newvar = search_indexed_tlist_for_var(var,
1789 context->outer_itlist,
1793 return (Node *) newvar;
1794 if (context->inner_itlist)
1796 newvar = search_indexed_tlist_for_var(var,
1797 context->inner_itlist,
1801 return (Node *) newvar;
1804 /* If it's for acceptable_rel, adjust and return it */
1805 if (var->varno == context->acceptable_rel)
1808 var->varno += context->rtoffset;
1809 if (var->varnoold > 0)
1810 var->varnoold += context->rtoffset;
1811 return (Node *) var;
1814 /* No referent found for Var */
1815 elog(ERROR, "variable not found in subplan target lists");
1817 if (IsA(node, PlaceHolderVar))
1819 PlaceHolderVar *phv = (PlaceHolderVar *) node;
1821 /* See if the PlaceHolderVar has bubbled up from a lower plan node */
1822 if (context->outer_itlist->has_ph_vars)
1824 newvar = search_indexed_tlist_for_non_var((Node *) phv,
1825 context->outer_itlist,
1828 return (Node *) newvar;
1830 if (context->inner_itlist && context->inner_itlist->has_ph_vars)
1832 newvar = search_indexed_tlist_for_non_var((Node *) phv,
1833 context->inner_itlist,
1836 return (Node *) newvar;
1839 /* If not supplied by input plans, evaluate the contained expr */
1840 return fix_join_expr_mutator((Node *) phv->phexpr, context);
1842 if (IsA(node, Param))
1843 return fix_param_node(context->root, (Param *) node);
1844 /* Try matching more complex expressions too, if tlists have any */
1845 if (context->outer_itlist->has_non_vars)
1847 newvar = search_indexed_tlist_for_non_var(node,
1848 context->outer_itlist,
1851 return (Node *) newvar;
1853 if (context->inner_itlist && context->inner_itlist->has_non_vars)
1855 newvar = search_indexed_tlist_for_non_var(node,
1856 context->inner_itlist,
1859 return (Node *) newvar;
1861 fix_expr_common(context->root, node);
1862 return expression_tree_mutator(node,
1863 fix_join_expr_mutator,
1869 * Modifies an expression tree so that all Var nodes reference outputs
1870 * of a subplan. Also performs opcode lookup, and adds regclass OIDs to
1871 * root->glob->relationOids.
1873 * This is used to fix up target and qual expressions of non-join upper-level
1874 * plan nodes, as well as index-only scan nodes.
1876 * An error is raised if no matching var can be found in the subplan tlist
1877 * --- so this routine should only be applied to nodes whose subplans'
1878 * targetlists were generated via flatten_tlist() or some such method.
1880 * If itlist->has_non_vars is true, then we try to match whole subexpressions
1881 * against elements of the subplan tlist, so that we can avoid recomputing
1882 * expressions that were already computed by the subplan. (This is relatively
1883 * expensive, so we don't want to try it in the common case where the
1884 * subplan tlist is just a flattened list of Vars.)
1886 * 'node': the tree to be fixed (a target item or qual)
1887 * 'subplan_itlist': indexed target list for subplan (or index)
1888 * 'newvarno': varno to use for Vars referencing tlist elements
1889 * 'rtoffset': how much to increment varnoold by
1891 * The resulting tree is a copy of the original in which all Var nodes have
1892 * varno = newvarno, varattno = resno of corresponding targetlist element.
1893 * The original tree is not modified.
1896 fix_upper_expr(PlannerInfo *root,
1898 indexed_tlist *subplan_itlist,
1902 fix_upper_expr_context context;
1904 context.root = root;
1905 context.subplan_itlist = subplan_itlist;
1906 context.newvarno = newvarno;
1907 context.rtoffset = rtoffset;
1908 return fix_upper_expr_mutator(node, &context);
1912 fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context)
1920 Var *var = (Var *) node;
1922 newvar = search_indexed_tlist_for_var(var,
1923 context->subplan_itlist,
1927 elog(ERROR, "variable not found in subplan target list");
1928 return (Node *) newvar;
1930 if (IsA(node, PlaceHolderVar))
1932 PlaceHolderVar *phv = (PlaceHolderVar *) node;
1934 /* See if the PlaceHolderVar has bubbled up from a lower plan node */
1935 if (context->subplan_itlist->has_ph_vars)
1937 newvar = search_indexed_tlist_for_non_var((Node *) phv,
1938 context->subplan_itlist,
1941 return (Node *) newvar;
1943 /* If not supplied by input plan, evaluate the contained expr */
1944 return fix_upper_expr_mutator((Node *) phv->phexpr, context);
1946 if (IsA(node, Param))
1947 return fix_param_node(context->root, (Param *) node);
1948 /* Try matching more complex expressions too, if tlist has any */
1949 if (context->subplan_itlist->has_non_vars)
1951 newvar = search_indexed_tlist_for_non_var(node,
1952 context->subplan_itlist,
1955 return (Node *) newvar;
1957 fix_expr_common(context->root, node);
1958 return expression_tree_mutator(node,
1959 fix_upper_expr_mutator,
1964 * set_returning_clause_references
1965 * Perform setrefs.c's work on a RETURNING targetlist
1967 * If the query involves more than just the result table, we have to
1968 * adjust any Vars that refer to other tables to reference junk tlist
1969 * entries in the top subplan's targetlist. Vars referencing the result
1970 * table should be left alone, however (the executor will evaluate them
1971 * using the actual heap tuple, after firing triggers if any). In the
1972 * adjusted RETURNING list, result-table Vars will have their original
1973 * varno (plus rtoffset), but Vars for other rels will have varno OUTER_VAR.
1975 * We also must perform opcode lookup and add regclass OIDs to
1976 * root->glob->relationOids.
1978 * 'rlist': the RETURNING targetlist to be fixed
1979 * 'topplan': the top subplan node that will be just below the ModifyTable
1980 * node (note it's not yet passed through set_plan_refs)
1981 * 'resultRelation': RT index of the associated result relation
1982 * 'rtoffset': how much to increment varnos by
1984 * Note: the given 'root' is for the parent query level, not the 'topplan'.
1985 * This does not matter currently since we only access the dependency-item
1986 * lists in root->glob, but it would need some hacking if we wanted a root
1987 * that actually matches the subplan.
1989 * Note: resultRelation is not yet adjusted by rtoffset.
1992 set_returning_clause_references(PlannerInfo *root,
1995 Index resultRelation,
1998 indexed_tlist *itlist;
2001 * We can perform the desired Var fixup by abusing the fix_join_expr
2002 * machinery that formerly handled inner indexscan fixup. We search the
2003 * top plan's targetlist for Vars of non-result relations, and use
2004 * fix_join_expr to convert RETURNING Vars into references to those tlist
2005 * entries, while leaving result-rel Vars as-is.
2007 * PlaceHolderVars will also be sought in the targetlist, but no
2008 * more-complex expressions will be. Note that it is not possible for a
2009 * PlaceHolderVar to refer to the result relation, since the result is
2010 * never below an outer join. If that case could happen, we'd have to be
2011 * prepared to pick apart the PlaceHolderVar and evaluate its contained
2012 * expression instead.
2014 itlist = build_tlist_index_other_vars(topplan->targetlist, resultRelation);
2016 rlist = fix_join_expr(root,
2028 /*****************************************************************************
2029 * OPERATOR REGPROC LOOKUP
2030 *****************************************************************************/
2034 * Calculate opfuncid field from opno for each OpExpr node in given tree.
2035 * The given tree can be anything expression_tree_walker handles.
2037 * The argument is modified in-place. (This is OK since we'd want the
2038 * same change for any node, even if it gets visited more than once due to
2039 * shared structure.)
2042 fix_opfuncids(Node *node)
2044 /* This tree walk requires no special setup, so away we go... */
2045 fix_opfuncids_walker(node, NULL);
2049 fix_opfuncids_walker(Node *node, void *context)
2053 if (IsA(node, OpExpr))
2054 set_opfuncid((OpExpr *) node);
2055 else if (IsA(node, DistinctExpr))
2056 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
2057 else if (IsA(node, NullIfExpr))
2058 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
2059 else if (IsA(node, ScalarArrayOpExpr))
2060 set_sa_opfuncid((ScalarArrayOpExpr *) node);
2061 return expression_tree_walker(node, fix_opfuncids_walker, context);
2066 * Set the opfuncid (procedure OID) in an OpExpr node,
2067 * if it hasn't been set already.
2069 * Because of struct equivalence, this can also be used for
2070 * DistinctExpr and NullIfExpr nodes.
2073 set_opfuncid(OpExpr *opexpr)
2075 if (opexpr->opfuncid == InvalidOid)
2076 opexpr->opfuncid = get_opcode(opexpr->opno);
2081 * As above, for ScalarArrayOpExpr nodes.
2084 set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
2086 if (opexpr->opfuncid == InvalidOid)
2087 opexpr->opfuncid = get_opcode(opexpr->opno);
2090 /*****************************************************************************
2091 * QUERY DEPENDENCY MANAGEMENT
2092 *****************************************************************************/
2095 * record_plan_function_dependency
2096 * Mark the current plan as depending on a particular function.
2098 * This is exported so that the function-inlining code can record a
2099 * dependency on a function that it's removed from the plan tree.
2102 record_plan_function_dependency(PlannerInfo *root, Oid funcid)
2105 * For performance reasons, we don't bother to track built-in functions;
2106 * we just assume they'll never change (or at least not in ways that'd
2107 * invalidate plans using them). For this purpose we can consider a
2108 * built-in function to be one with OID less than FirstBootstrapObjectId.
2109 * Note that the OID generator guarantees never to generate such an OID
2110 * after startup, even at OID wraparound.
2112 if (funcid >= (Oid) FirstBootstrapObjectId)
2114 PlanInvalItem *inval_item = makeNode(PlanInvalItem);
2117 * It would work to use any syscache on pg_proc, but the easiest is
2118 * PROCOID since we already have the function's OID at hand. Note
2119 * that plancache.c knows we use PROCOID.
2121 inval_item->cacheId = PROCOID;
2122 inval_item->hashValue = GetSysCacheHashValue1(PROCOID,
2123 ObjectIdGetDatum(funcid));
2125 root->glob->invalItems = lappend(root->glob->invalItems, inval_item);
2130 * extract_query_dependencies
2131 * Given a not-yet-planned query or queries (i.e. a Query node or list
2132 * of Query nodes), extract dependencies just as set_plan_references
2135 * This is needed by plancache.c to handle invalidation of cached unplanned
2139 extract_query_dependencies(Node *query,
2140 List **relationOids,
2142 bool *hasRowSecurity)
2147 /* Make up dummy planner state so we can use this module's machinery */
2148 MemSet(&glob, 0, sizeof(glob));
2149 glob.type = T_PlannerGlobal;
2150 glob.relationOids = NIL;
2151 glob.invalItems = NIL;
2152 glob.hasRowSecurity = false;
2154 MemSet(&root, 0, sizeof(root));
2155 root.type = T_PlannerInfo;
2158 (void) extract_query_dependencies_walker(query, &root);
2160 *relationOids = glob.relationOids;
2161 *invalItems = glob.invalItems;
2162 *hasRowSecurity = glob.hasRowSecurity;
2166 extract_query_dependencies_walker(Node *node, PlannerInfo *context)
2170 Assert(!IsA(node, PlaceHolderVar));
2171 /* Extract function dependencies and check for regclass Consts */
2172 fix_expr_common(context, node);
2173 if (IsA(node, Query))
2175 Query *query = (Query *) node;
2178 /* Collect row security information */
2179 context->glob->hasRowSecurity = query->hasRowSecurity;
2181 if (query->commandType == CMD_UTILITY)
2184 * Ignore utility statements, except those (such as EXPLAIN) that
2185 * contain a parsed-but-not-planned query.
2187 query = UtilityContainsQuery(query->utilityStmt);
2192 /* Collect relation OIDs in this Query's rtable */
2193 foreach(lc, query->rtable)
2195 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
2197 if (rte->rtekind == RTE_RELATION)
2198 context->glob->relationOids =
2199 lappend_oid(context->glob->relationOids, rte->relid);
2202 /* And recurse into the query's subexpressions */
2203 return query_tree_walker(query, extract_query_dependencies_walker,
2204 (void *) context, 0);
2206 return expression_tree_walker(node, extract_query_dependencies_walker,