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-2018, 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 bool has_conv_whole_rows; /* are there ConvertRowtypeExpr
45 * entries encapsulating a whole-row
47 tlist_vinfo vars[FLEXIBLE_ARRAY_MEMBER]; /* has num_vars entries */
54 } fix_scan_expr_context;
59 indexed_tlist *outer_itlist;
60 indexed_tlist *inner_itlist;
63 } fix_join_expr_context;
68 indexed_tlist *subplan_itlist;
71 } fix_upper_expr_context;
74 * Check if a Const node is a regclass value. We accept plain OID too,
75 * since a regclass Const will get folded to that type if it's an argument
76 * to oideq or similar operators. (This might result in some extraneous
77 * values in a plan's list of relation dependencies, but the worst result
78 * would be occasional useless replans.)
80 #define ISREGCLASSCONST(con) \
81 (((con)->consttype == REGCLASSOID || (con)->consttype == OIDOID) && \
84 #define fix_scan_list(root, lst, rtoffset) \
85 ((List *) fix_scan_expr(root, (Node *) (lst), rtoffset))
87 static void add_rtes_to_flat_rtable(PlannerInfo *root, bool recursing);
88 static void flatten_unplanned_rtes(PlannerGlobal *glob, RangeTblEntry *rte);
89 static bool flatten_rtes_walker(Node *node, PlannerGlobal *glob);
90 static void add_rte_to_flat_rtable(PlannerGlobal *glob, RangeTblEntry *rte);
91 static Plan *set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset);
92 static Plan *set_indexonlyscan_references(PlannerInfo *root,
95 static Plan *set_subqueryscan_references(PlannerInfo *root,
98 static bool trivial_subqueryscan(SubqueryScan *plan);
99 static void set_foreignscan_references(PlannerInfo *root,
102 static void set_customscan_references(PlannerInfo *root,
105 static Node *fix_scan_expr(PlannerInfo *root, Node *node, int rtoffset);
106 static Node *fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context);
107 static bool fix_scan_expr_walker(Node *node, fix_scan_expr_context *context);
108 static void set_join_references(PlannerInfo *root, Join *join, int rtoffset);
109 static void set_upper_references(PlannerInfo *root, Plan *plan, int rtoffset);
110 static void set_param_references(PlannerInfo *root, Plan *plan);
111 static Node *convert_combining_aggrefs(Node *node, void *context);
112 static void set_dummy_tlist_references(Plan *plan, int rtoffset);
113 static indexed_tlist *build_tlist_index(List *tlist);
114 static Var *search_indexed_tlist_for_var(Var *var,
115 indexed_tlist *itlist,
118 static Var *search_indexed_tlist_for_non_var(Expr *node,
119 indexed_tlist *itlist,
121 static Var *search_indexed_tlist_for_sortgroupref(Expr *node,
123 indexed_tlist *itlist,
125 static List *fix_join_expr(PlannerInfo *root,
127 indexed_tlist *outer_itlist,
128 indexed_tlist *inner_itlist,
129 Index acceptable_rel, int rtoffset);
130 static Node *fix_join_expr_mutator(Node *node,
131 fix_join_expr_context *context);
132 static Node *fix_upper_expr(PlannerInfo *root,
134 indexed_tlist *subplan_itlist,
137 static Node *fix_upper_expr_mutator(Node *node,
138 fix_upper_expr_context *context);
139 static List *set_returning_clause_references(PlannerInfo *root,
142 Index resultRelation,
144 static bool extract_query_dependencies_walker(Node *node,
145 PlannerInfo *context);
146 static bool is_converted_whole_row_reference(Node *node);
148 /*****************************************************************************
152 *****************************************************************************/
155 * set_plan_references
157 * This is the final processing pass of the planner/optimizer. The plan
158 * tree is complete; we just have to adjust some representational details
159 * for the convenience of the executor:
161 * 1. We flatten the various subquery rangetables into a single list, and
162 * zero out RangeTblEntry fields that are not useful to the executor.
164 * 2. We adjust Vars in scan nodes to be consistent with the flat rangetable.
166 * 3. We adjust Vars in upper plan nodes to refer to the outputs of their
169 * 4. Aggrefs in Agg plan nodes need to be adjusted in some cases involving
170 * partial aggregation or minmax aggregate optimization.
172 * 5. PARAM_MULTIEXPR Params are replaced by regular PARAM_EXEC Params,
173 * now that we have finished planning all MULTIEXPR subplans.
175 * 6. We compute regproc OIDs for operators (ie, we look up the function
176 * that implements each op).
178 * 7. We create lists of specific objects that the plan depends on.
179 * This will be used by plancache.c to drive invalidation of cached plans.
180 * Relation dependencies are represented by OIDs, and everything else by
181 * PlanInvalItems (this distinction is motivated by the shared-inval APIs).
182 * Currently, relations and user-defined functions are the only types of
183 * objects that are explicitly tracked this way.
185 * 8. We assign every plan node in the tree a unique ID.
187 * We also perform one final optimization step, which is to delete
188 * SubqueryScan plan nodes that aren't doing anything useful (ie, have
189 * no qual and a no-op targetlist). The reason for doing this last is that
190 * it can't readily be done before set_plan_references, because it would
191 * break set_upper_references: the Vars in the subquery's top tlist
192 * wouldn't match up with the Vars in the outer plan tree. The SubqueryScan
193 * serves a necessary function as a buffer between outer query and subquery
194 * variable numbering ... but after we've flattened the rangetable this is
195 * no longer a problem, since then there's only one rtindex namespace.
197 * set_plan_references recursively traverses the whole plan tree.
199 * The return value is normally the same Plan node passed in, but can be
200 * different when the passed-in Plan is a SubqueryScan we decide isn't needed.
202 * The flattened rangetable entries are appended to root->glob->finalrtable.
203 * Also, rowmarks entries are appended to root->glob->finalrowmarks, and the
204 * RT indexes of ModifyTable result relations to root->glob->resultRelations.
205 * Plan dependencies are appended to root->glob->relationOids (for relations)
206 * and root->glob->invalItems (for everything else).
208 * Notice that we modify Plan nodes in-place, but use expression_tree_mutator
209 * to process targetlist and qual expressions. We can assume that the Plan
210 * nodes were just built by the planner and are not multiply referenced, but
211 * it's not so safe to assume that for expression tree nodes.
214 set_plan_references(PlannerInfo *root, Plan *plan)
216 PlannerGlobal *glob = root->glob;
217 int rtoffset = list_length(glob->finalrtable);
221 * Add all the query's RTEs to the flattened rangetable. The live ones
222 * will have their rangetable indexes increased by rtoffset. (Additional
223 * RTEs, not referenced by the Plan tree, might get added after those.)
225 add_rtes_to_flat_rtable(root, false);
228 * Adjust RT indexes of PlanRowMarks and add to final rowmarks list
230 foreach(lc, root->rowMarks)
232 PlanRowMark *rc = lfirst_node(PlanRowMark, lc);
235 /* flat copy is enough since all fields are scalars */
236 newrc = (PlanRowMark *) palloc(sizeof(PlanRowMark));
237 memcpy(newrc, rc, sizeof(PlanRowMark));
239 /* adjust indexes ... but *not* the rowmarkId */
240 newrc->rti += rtoffset;
241 newrc->prti += rtoffset;
243 glob->finalrowmarks = lappend(glob->finalrowmarks, newrc);
246 /* Now fix the Plan tree */
247 return set_plan_refs(root, plan, rtoffset);
251 * Extract RangeTblEntries from the plan's rangetable, and add to flat rtable
253 * This can recurse into subquery plans; "recursing" is true if so.
256 add_rtes_to_flat_rtable(PlannerInfo *root, bool recursing)
258 PlannerGlobal *glob = root->glob;
263 * Add the query's own RTEs to the flattened rangetable.
265 * At top level, we must add all RTEs so that their indexes in the
266 * flattened rangetable match up with their original indexes. When
267 * recursing, we only care about extracting relation RTEs.
269 foreach(lc, root->parse->rtable)
271 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
273 if (!recursing || rte->rtekind == RTE_RELATION)
274 add_rte_to_flat_rtable(glob, rte);
278 * If there are any dead subqueries, they are not referenced in the Plan
279 * tree, so we must add RTEs contained in them to the flattened rtable
280 * separately. (If we failed to do this, the executor would not perform
281 * expected permission checks for tables mentioned in such subqueries.)
283 * Note: this pass over the rangetable can't be combined with the previous
284 * one, because that would mess up the numbering of the live RTEs in the
285 * flattened rangetable.
288 foreach(lc, root->parse->rtable)
290 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
293 * We should ignore inheritance-parent RTEs: their contents have been
294 * pulled up into our rangetable already. Also ignore any subquery
295 * RTEs without matching RelOptInfos, as they likewise have been
298 if (rte->rtekind == RTE_SUBQUERY && !rte->inh &&
299 rti < root->simple_rel_array_size)
301 RelOptInfo *rel = root->simple_rel_array[rti];
305 Assert(rel->relid == rti); /* sanity check on array */
308 * The subquery might never have been planned at all, if it
309 * was excluded on the basis of self-contradictory constraints
310 * in our query level. In this case apply
311 * flatten_unplanned_rtes.
313 * If it was planned but the result rel is dummy, we assume
314 * that it has been omitted from our plan tree (see
315 * set_subquery_pathlist), and recurse to pull up its RTEs.
317 * Otherwise, it should be represented by a SubqueryScan node
318 * somewhere in our plan tree, and we'll pull up its RTEs when
319 * we process that plan node.
321 * However, if we're recursing, then we should pull up RTEs
322 * whether the subquery is dummy or not, because we've found
323 * that some upper query level is treating this one as dummy,
324 * and so we won't scan this level's plan tree at all.
326 if (rel->subroot == NULL)
327 flatten_unplanned_rtes(glob, rte);
328 else if (recursing ||
329 IS_DUMMY_REL(fetch_upper_rel(rel->subroot,
330 UPPERREL_FINAL, NULL)))
331 add_rtes_to_flat_rtable(rel->subroot, true);
339 * Extract RangeTblEntries from a subquery that was never planned at all
342 flatten_unplanned_rtes(PlannerGlobal *glob, RangeTblEntry *rte)
344 /* Use query_tree_walker to find all RTEs in the parse tree */
345 (void) query_tree_walker(rte->subquery,
352 flatten_rtes_walker(Node *node, PlannerGlobal *glob)
356 if (IsA(node, RangeTblEntry))
358 RangeTblEntry *rte = (RangeTblEntry *) node;
360 /* As above, we need only save relation RTEs */
361 if (rte->rtekind == RTE_RELATION)
362 add_rte_to_flat_rtable(glob, rte);
365 if (IsA(node, Query))
367 /* Recurse into subselects */
368 return query_tree_walker((Query *) node,
373 return expression_tree_walker(node, flatten_rtes_walker,
378 * Add (a copy of) the given RTE to the final rangetable
380 * In the flat rangetable, we zero out substructure pointers that are not
381 * needed by the executor; this reduces the storage space and copying cost
382 * for cached plans. We keep only the ctename, alias and eref Alias fields,
383 * which are needed by EXPLAIN, and the selectedCols, insertedCols and
384 * updatedCols bitmaps, which are needed for executor-startup permissions
385 * checking and for trigger event checking.
388 add_rte_to_flat_rtable(PlannerGlobal *glob, RangeTblEntry *rte)
390 RangeTblEntry *newrte;
392 /* flat copy to duplicate all the scalar fields */
393 newrte = (RangeTblEntry *) palloc(sizeof(RangeTblEntry));
394 memcpy(newrte, rte, sizeof(RangeTblEntry));
396 /* zap unneeded sub-structure */
397 newrte->tablesample = NULL;
398 newrte->subquery = NULL;
399 newrte->joinaliasvars = NIL;
400 newrte->functions = NIL;
401 newrte->tablefunc = NULL;
402 newrte->values_lists = NIL;
403 newrte->coltypes = NIL;
404 newrte->coltypmods = NIL;
405 newrte->colcollations = NIL;
406 newrte->securityQuals = NIL;
408 glob->finalrtable = lappend(glob->finalrtable, newrte);
411 * Check for RT index overflow; it's very unlikely, but if it did happen,
412 * the executor would get confused by varnos that match the special varno
415 if (IS_SPECIAL_VARNO(list_length(glob->finalrtable)))
417 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
418 errmsg("too many range table entries")));
421 * If it's a plain relation RTE, add the table to relationOids.
423 * We do this even though the RTE might be unreferenced in the plan tree;
424 * this would correspond to cases such as views that were expanded, child
425 * tables that were eliminated by constraint exclusion, etc. Schema
426 * invalidation on such a rel must still force rebuilding of the plan.
428 * Note we don't bother to avoid making duplicate list entries. We could,
429 * but it would probably cost more cycles than it would save.
431 if (newrte->rtekind == RTE_RELATION)
432 glob->relationOids = lappend_oid(glob->relationOids, newrte->relid);
436 * set_plan_refs: recurse through the Plan nodes of a single subquery level
439 set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
446 /* Assign this node a unique ID. */
447 plan->plan_node_id = root->glob->lastPlanNodeId++;
450 * Plan-type-specific fixes
452 switch (nodeTag(plan))
456 SeqScan *splan = (SeqScan *) plan;
458 splan->scanrelid += rtoffset;
459 splan->plan.targetlist =
460 fix_scan_list(root, splan->plan.targetlist, rtoffset);
462 fix_scan_list(root, splan->plan.qual, rtoffset);
467 SampleScan *splan = (SampleScan *) plan;
469 splan->scan.scanrelid += rtoffset;
470 splan->scan.plan.targetlist =
471 fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
472 splan->scan.plan.qual =
473 fix_scan_list(root, splan->scan.plan.qual, rtoffset);
474 splan->tablesample = (TableSampleClause *)
475 fix_scan_expr(root, (Node *) splan->tablesample, rtoffset);
480 IndexScan *splan = (IndexScan *) plan;
482 splan->scan.scanrelid += rtoffset;
483 splan->scan.plan.targetlist =
484 fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
485 splan->scan.plan.qual =
486 fix_scan_list(root, splan->scan.plan.qual, rtoffset);
488 fix_scan_list(root, splan->indexqual, rtoffset);
489 splan->indexqualorig =
490 fix_scan_list(root, splan->indexqualorig, rtoffset);
491 splan->indexorderby =
492 fix_scan_list(root, splan->indexorderby, rtoffset);
493 splan->indexorderbyorig =
494 fix_scan_list(root, splan->indexorderbyorig, rtoffset);
497 case T_IndexOnlyScan:
499 IndexOnlyScan *splan = (IndexOnlyScan *) plan;
501 return set_indexonlyscan_references(root, splan, rtoffset);
504 case T_BitmapIndexScan:
506 BitmapIndexScan *splan = (BitmapIndexScan *) plan;
508 splan->scan.scanrelid += rtoffset;
509 /* no need to fix targetlist and qual */
510 Assert(splan->scan.plan.targetlist == NIL);
511 Assert(splan->scan.plan.qual == NIL);
513 fix_scan_list(root, splan->indexqual, rtoffset);
514 splan->indexqualorig =
515 fix_scan_list(root, splan->indexqualorig, rtoffset);
518 case T_BitmapHeapScan:
520 BitmapHeapScan *splan = (BitmapHeapScan *) plan;
522 splan->scan.scanrelid += rtoffset;
523 splan->scan.plan.targetlist =
524 fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
525 splan->scan.plan.qual =
526 fix_scan_list(root, splan->scan.plan.qual, rtoffset);
527 splan->bitmapqualorig =
528 fix_scan_list(root, splan->bitmapqualorig, rtoffset);
533 TidScan *splan = (TidScan *) plan;
535 splan->scan.scanrelid += rtoffset;
536 splan->scan.plan.targetlist =
537 fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
538 splan->scan.plan.qual =
539 fix_scan_list(root, splan->scan.plan.qual, rtoffset);
541 fix_scan_list(root, splan->tidquals, rtoffset);
545 /* Needs special treatment, see comments below */
546 return set_subqueryscan_references(root,
547 (SubqueryScan *) plan,
551 FunctionScan *splan = (FunctionScan *) plan;
553 splan->scan.scanrelid += rtoffset;
554 splan->scan.plan.targetlist =
555 fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
556 splan->scan.plan.qual =
557 fix_scan_list(root, splan->scan.plan.qual, rtoffset);
559 fix_scan_list(root, splan->functions, rtoffset);
562 case T_TableFuncScan:
564 TableFuncScan *splan = (TableFuncScan *) plan;
566 splan->scan.scanrelid += rtoffset;
567 splan->scan.plan.targetlist =
568 fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
569 splan->scan.plan.qual =
570 fix_scan_list(root, splan->scan.plan.qual, rtoffset);
571 splan->tablefunc = (TableFunc *)
572 fix_scan_expr(root, (Node *) splan->tablefunc, rtoffset);
577 ValuesScan *splan = (ValuesScan *) plan;
579 splan->scan.scanrelid += rtoffset;
580 splan->scan.plan.targetlist =
581 fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
582 splan->scan.plan.qual =
583 fix_scan_list(root, splan->scan.plan.qual, rtoffset);
584 splan->values_lists =
585 fix_scan_list(root, splan->values_lists, rtoffset);
590 CteScan *splan = (CteScan *) plan;
592 splan->scan.scanrelid += rtoffset;
593 splan->scan.plan.targetlist =
594 fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
595 splan->scan.plan.qual =
596 fix_scan_list(root, splan->scan.plan.qual, rtoffset);
599 case T_NamedTuplestoreScan:
601 NamedTuplestoreScan *splan = (NamedTuplestoreScan *) plan;
603 splan->scan.scanrelid += rtoffset;
604 splan->scan.plan.targetlist =
605 fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
606 splan->scan.plan.qual =
607 fix_scan_list(root, splan->scan.plan.qual, rtoffset);
610 case T_WorkTableScan:
612 WorkTableScan *splan = (WorkTableScan *) plan;
614 splan->scan.scanrelid += rtoffset;
615 splan->scan.plan.targetlist =
616 fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
617 splan->scan.plan.qual =
618 fix_scan_list(root, splan->scan.plan.qual, rtoffset);
622 set_foreignscan_references(root, (ForeignScan *) plan, rtoffset);
625 set_customscan_references(root, (CustomScan *) plan, rtoffset);
631 set_join_references(root, (Join *) plan, rtoffset);
637 set_upper_references(root, plan, rtoffset);
638 set_param_references(root, plan);
649 * These plan types don't actually bother to evaluate their
650 * targetlists, because they just return their unmodified input
651 * tuples. Even though the targetlist won't be used by the
652 * executor, we fix it up for possible use by EXPLAIN (not to
653 * mention ease of debugging --- wrong varnos are very confusing).
655 set_dummy_tlist_references(plan, rtoffset);
658 * Since these plan types don't check quals either, we should not
659 * find any qual expression attached to them.
661 Assert(plan->qual == NIL);
665 LockRows *splan = (LockRows *) plan;
668 * Like the plan types above, LockRows doesn't evaluate its
669 * tlist or quals. But we have to fix up the RT indexes in
672 set_dummy_tlist_references(plan, rtoffset);
673 Assert(splan->plan.qual == NIL);
675 foreach(l, splan->rowMarks)
677 PlanRowMark *rc = (PlanRowMark *) lfirst(l);
680 rc->prti += rtoffset;
686 Limit *splan = (Limit *) plan;
689 * Like the plan types above, Limit doesn't evaluate its tlist
690 * or quals. It does have live expressions for limit/offset,
691 * however; and those cannot contain subplan variable refs, so
692 * fix_scan_expr works for them.
694 set_dummy_tlist_references(plan, rtoffset);
695 Assert(splan->plan.qual == NIL);
698 fix_scan_expr(root, splan->limitOffset, rtoffset);
700 fix_scan_expr(root, splan->limitCount, rtoffset);
705 Agg *agg = (Agg *) plan;
708 * If this node is combining partial-aggregation results, we
709 * must convert its Aggrefs to contain references to the
710 * partial-aggregate subexpressions that will be available
711 * from the child plan node.
713 if (DO_AGGSPLIT_COMBINE(agg->aggsplit))
715 plan->targetlist = (List *)
716 convert_combining_aggrefs((Node *) plan->targetlist,
718 plan->qual = (List *)
719 convert_combining_aggrefs((Node *) plan->qual,
723 set_upper_references(root, plan, rtoffset);
727 set_upper_references(root, plan, rtoffset);
731 WindowAgg *wplan = (WindowAgg *) plan;
733 set_upper_references(root, plan, rtoffset);
736 * Like Limit node limit/offset expressions, WindowAgg has
737 * frame offset expressions, which cannot contain subplan
738 * variable refs, so fix_scan_expr works for them.
741 fix_scan_expr(root, wplan->startOffset, rtoffset);
743 fix_scan_expr(root, wplan->endOffset, rtoffset);
748 Result *splan = (Result *) plan;
751 * Result may or may not have a subplan; if not, it's more
752 * like a scan node than an upper node.
754 if (splan->plan.lefttree != NULL)
755 set_upper_references(root, plan, rtoffset);
758 splan->plan.targetlist =
759 fix_scan_list(root, splan->plan.targetlist, rtoffset);
761 fix_scan_list(root, splan->plan.qual, rtoffset);
763 /* resconstantqual can't contain any subplan variable refs */
764 splan->resconstantqual =
765 fix_scan_expr(root, splan->resconstantqual, rtoffset);
769 set_upper_references(root, plan, rtoffset);
773 ModifyTable *splan = (ModifyTable *) plan;
775 Assert(splan->plan.targetlist == NIL);
776 Assert(splan->plan.qual == NIL);
778 splan->withCheckOptionLists =
779 fix_scan_list(root, splan->withCheckOptionLists, rtoffset);
781 if (splan->returningLists)
789 * Pass each per-subplan returningList through
790 * set_returning_clause_references().
792 Assert(list_length(splan->returningLists) == list_length(splan->resultRelations));
793 Assert(list_length(splan->returningLists) == list_length(splan->plans));
794 forthree(lcrl, splan->returningLists,
795 lcrr, splan->resultRelations,
798 List *rlist = (List *) lfirst(lcrl);
799 Index resultrel = lfirst_int(lcrr);
800 Plan *subplan = (Plan *) lfirst(lcp);
802 rlist = set_returning_clause_references(root,
807 newRL = lappend(newRL, rlist);
809 splan->returningLists = newRL;
812 * Set up the visible plan targetlist as being the same as
813 * the first RETURNING list. This is for the use of
814 * EXPLAIN; the executor won't pay any attention to the
815 * targetlist. We postpone this step until here so that
816 * we don't have to do set_returning_clause_references()
817 * twice on identical targetlists.
819 splan->plan.targetlist = copyObject(linitial(newRL));
823 * We treat ModifyTable with ON CONFLICT as a form of 'pseudo
824 * join', where the inner side is the EXCLUDED tuple.
825 * Therefore use fix_join_expr to setup the relevant variables
826 * to INNER_VAR. We explicitly don't create any OUTER_VARs as
827 * those are already used by RETURNING and it seems better to
828 * be non-conflicting.
830 if (splan->onConflictSet)
832 indexed_tlist *itlist;
834 itlist = build_tlist_index(splan->exclRelTlist);
836 splan->onConflictSet =
837 fix_join_expr(root, splan->onConflictSet,
839 linitial_int(splan->resultRelations),
842 splan->onConflictWhere = (Node *)
843 fix_join_expr(root, (List *) splan->onConflictWhere,
845 linitial_int(splan->resultRelations),
850 splan->exclRelTlist =
851 fix_scan_list(root, splan->exclRelTlist, rtoffset);
854 splan->nominalRelation += rtoffset;
855 splan->exclRelRTI += rtoffset;
857 foreach(l, splan->partitioned_rels)
859 lfirst_int(l) += rtoffset;
861 foreach(l, splan->resultRelations)
863 lfirst_int(l) += rtoffset;
865 foreach(l, splan->rowMarks)
867 PlanRowMark *rc = (PlanRowMark *) lfirst(l);
870 rc->prti += rtoffset;
872 foreach(l, splan->plans)
874 lfirst(l) = set_plan_refs(root,
880 * Append this ModifyTable node's final result relation RT
881 * index(es) to the global list for the plan, and set its
882 * resultRelIndex to reflect their starting position in the
885 splan->resultRelIndex = list_length(root->glob->resultRelations);
886 root->glob->resultRelations =
887 list_concat(root->glob->resultRelations,
888 list_copy(splan->resultRelations));
891 * If the main target relation is a partitioned table, the
892 * following list contains the RT indexes of partitioned child
893 * relations including the root, which are not included in the
894 * above list. We also keep RT indexes of the roots
895 * separately to be identitied as such during the executor
898 if (splan->partitioned_rels != NIL)
900 root->glob->nonleafResultRelations =
901 list_concat(root->glob->nonleafResultRelations,
902 list_copy(splan->partitioned_rels));
903 /* Remember where this root will be in the global list. */
904 splan->rootResultRelIndex =
905 list_length(root->glob->rootResultRelations);
906 root->glob->rootResultRelations =
907 lappend_int(root->glob->rootResultRelations,
908 linitial_int(splan->partitioned_rels));
914 Append *splan = (Append *) plan;
917 * Append, like Sort et al, doesn't actually evaluate its
918 * targetlist or check quals.
920 set_dummy_tlist_references(plan, rtoffset);
921 Assert(splan->plan.qual == NIL);
922 foreach(l, splan->partitioned_rels)
924 lfirst_int(l) += rtoffset;
926 foreach(l, splan->appendplans)
928 lfirst(l) = set_plan_refs(root,
936 MergeAppend *splan = (MergeAppend *) plan;
939 * MergeAppend, like Sort et al, doesn't actually evaluate its
940 * targetlist or check quals.
942 set_dummy_tlist_references(plan, rtoffset);
943 Assert(splan->plan.qual == NIL);
944 foreach(l, splan->partitioned_rels)
946 lfirst_int(l) += rtoffset;
948 foreach(l, splan->mergeplans)
950 lfirst(l) = set_plan_refs(root,
956 case T_RecursiveUnion:
957 /* This doesn't evaluate targetlist or check quals either */
958 set_dummy_tlist_references(plan, rtoffset);
959 Assert(plan->qual == NIL);
963 BitmapAnd *splan = (BitmapAnd *) plan;
965 /* BitmapAnd works like Append, but has no tlist */
966 Assert(splan->plan.targetlist == NIL);
967 Assert(splan->plan.qual == NIL);
968 foreach(l, splan->bitmapplans)
970 lfirst(l) = set_plan_refs(root,
978 BitmapOr *splan = (BitmapOr *) plan;
980 /* BitmapOr works like Append, but has no tlist */
981 Assert(splan->plan.targetlist == NIL);
982 Assert(splan->plan.qual == NIL);
983 foreach(l, splan->bitmapplans)
985 lfirst(l) = set_plan_refs(root,
992 elog(ERROR, "unrecognized node type: %d",
993 (int) nodeTag(plan));
998 * Now recurse into child plans, if any
1000 * NOTE: it is essential that we recurse into child plans AFTER we set
1001 * subplan references in this plan's tlist and quals. If we did the
1002 * reference-adjustments bottom-up, then we would fail to match this
1003 * plan's var nodes against the already-modified nodes of the children.
1005 plan->lefttree = set_plan_refs(root, plan->lefttree, rtoffset);
1006 plan->righttree = set_plan_refs(root, plan->righttree, rtoffset);
1012 * set_indexonlyscan_references
1013 * Do set_plan_references processing on an IndexOnlyScan
1015 * This is unlike the handling of a plain IndexScan because we have to
1016 * convert Vars referencing the heap into Vars referencing the index.
1017 * We can use the fix_upper_expr machinery for that, by working from a
1018 * targetlist describing the index columns.
1021 set_indexonlyscan_references(PlannerInfo *root,
1022 IndexOnlyScan *plan,
1025 indexed_tlist *index_itlist;
1027 index_itlist = build_tlist_index(plan->indextlist);
1029 plan->scan.scanrelid += rtoffset;
1030 plan->scan.plan.targetlist = (List *)
1031 fix_upper_expr(root,
1032 (Node *) plan->scan.plan.targetlist,
1036 plan->scan.plan.qual = (List *)
1037 fix_upper_expr(root,
1038 (Node *) plan->scan.plan.qual,
1042 /* indexqual is already transformed to reference index columns */
1043 plan->indexqual = fix_scan_list(root, plan->indexqual, rtoffset);
1044 /* indexorderby is already transformed to reference index columns */
1045 plan->indexorderby = fix_scan_list(root, plan->indexorderby, rtoffset);
1046 /* indextlist must NOT be transformed to reference index columns */
1047 plan->indextlist = fix_scan_list(root, plan->indextlist, rtoffset);
1049 pfree(index_itlist);
1051 return (Plan *) plan;
1055 * set_subqueryscan_references
1056 * Do set_plan_references processing on a SubqueryScan
1058 * We try to strip out the SubqueryScan entirely; if we can't, we have
1059 * to do the normal processing on it.
1062 set_subqueryscan_references(PlannerInfo *root,
1069 /* Need to look up the subquery's RelOptInfo, since we need its subroot */
1070 rel = find_base_rel(root, plan->scan.scanrelid);
1072 /* Recursively process the subplan */
1073 plan->subplan = set_plan_references(rel->subroot, plan->subplan);
1075 if (trivial_subqueryscan(plan))
1078 * We can omit the SubqueryScan node and just pull up the subplan.
1083 result = plan->subplan;
1085 /* We have to be sure we don't lose any initplans */
1086 result->initPlan = list_concat(plan->scan.plan.initPlan,
1090 * We also have to transfer the SubqueryScan's result-column names
1091 * into the subplan, else columns sent to client will be improperly
1092 * labeled if this is the topmost plan level. Copy the "source
1093 * column" information too.
1095 forboth(lp, plan->scan.plan.targetlist, lc, result->targetlist)
1097 TargetEntry *ptle = (TargetEntry *) lfirst(lp);
1098 TargetEntry *ctle = (TargetEntry *) lfirst(lc);
1100 ctle->resname = ptle->resname;
1101 ctle->resorigtbl = ptle->resorigtbl;
1102 ctle->resorigcol = ptle->resorigcol;
1108 * Keep the SubqueryScan node. We have to do the processing that
1109 * set_plan_references would otherwise have done on it. Notice we do
1110 * not do set_upper_references() here, because a SubqueryScan will
1111 * always have been created with correct references to its subplan's
1112 * outputs to begin with.
1114 plan->scan.scanrelid += rtoffset;
1115 plan->scan.plan.targetlist =
1116 fix_scan_list(root, plan->scan.plan.targetlist, rtoffset);
1117 plan->scan.plan.qual =
1118 fix_scan_list(root, plan->scan.plan.qual, rtoffset);
1120 result = (Plan *) plan;
1127 * trivial_subqueryscan
1128 * Detect whether a SubqueryScan can be deleted from the plan tree.
1130 * We can delete it if it has no qual to check and the targetlist just
1131 * regurgitates the output of the child plan.
1134 trivial_subqueryscan(SubqueryScan *plan)
1140 if (plan->scan.plan.qual != NIL)
1143 if (list_length(plan->scan.plan.targetlist) !=
1144 list_length(plan->subplan->targetlist))
1145 return false; /* tlists not same length */
1148 forboth(lp, plan->scan.plan.targetlist, lc, plan->subplan->targetlist)
1150 TargetEntry *ptle = (TargetEntry *) lfirst(lp);
1151 TargetEntry *ctle = (TargetEntry *) lfirst(lc);
1153 if (ptle->resjunk != ctle->resjunk)
1154 return false; /* tlist doesn't match junk status */
1157 * We accept either a Var referencing the corresponding element of the
1158 * subplan tlist, or a Const equaling the subplan element. See
1159 * generate_setop_tlist() for motivation.
1161 if (ptle->expr && IsA(ptle->expr, Var))
1163 Var *var = (Var *) ptle->expr;
1165 Assert(var->varno == plan->scan.scanrelid);
1166 Assert(var->varlevelsup == 0);
1167 if (var->varattno != attrno)
1168 return false; /* out of order */
1170 else if (ptle->expr && IsA(ptle->expr, Const))
1172 if (!equal(ptle->expr, ctle->expr))
1185 * set_foreignscan_references
1186 * Do set_plan_references processing on a ForeignScan
1189 set_foreignscan_references(PlannerInfo *root,
1193 /* Adjust scanrelid if it's valid */
1194 if (fscan->scan.scanrelid > 0)
1195 fscan->scan.scanrelid += rtoffset;
1197 if (fscan->fdw_scan_tlist != NIL || fscan->scan.scanrelid == 0)
1200 * Adjust tlist, qual, fdw_exprs, fdw_recheck_quals to reference
1201 * foreign scan tuple
1203 indexed_tlist *itlist = build_tlist_index(fscan->fdw_scan_tlist);
1205 fscan->scan.plan.targetlist = (List *)
1206 fix_upper_expr(root,
1207 (Node *) fscan->scan.plan.targetlist,
1211 fscan->scan.plan.qual = (List *)
1212 fix_upper_expr(root,
1213 (Node *) fscan->scan.plan.qual,
1217 fscan->fdw_exprs = (List *)
1218 fix_upper_expr(root,
1219 (Node *) fscan->fdw_exprs,
1223 fscan->fdw_recheck_quals = (List *)
1224 fix_upper_expr(root,
1225 (Node *) fscan->fdw_recheck_quals,
1230 /* fdw_scan_tlist itself just needs fix_scan_list() adjustments */
1231 fscan->fdw_scan_tlist =
1232 fix_scan_list(root, fscan->fdw_scan_tlist, rtoffset);
1237 * Adjust tlist, qual, fdw_exprs, fdw_recheck_quals in the standard
1240 fscan->scan.plan.targetlist =
1241 fix_scan_list(root, fscan->scan.plan.targetlist, rtoffset);
1242 fscan->scan.plan.qual =
1243 fix_scan_list(root, fscan->scan.plan.qual, rtoffset);
1245 fix_scan_list(root, fscan->fdw_exprs, rtoffset);
1246 fscan->fdw_recheck_quals =
1247 fix_scan_list(root, fscan->fdw_recheck_quals, rtoffset);
1250 /* Adjust fs_relids if needed */
1253 Bitmapset *tempset = NULL;
1256 while ((x = bms_next_member(fscan->fs_relids, x)) >= 0)
1257 tempset = bms_add_member(tempset, x + rtoffset);
1258 fscan->fs_relids = tempset;
1263 * set_customscan_references
1264 * Do set_plan_references processing on a CustomScan
1267 set_customscan_references(PlannerInfo *root,
1273 /* Adjust scanrelid if it's valid */
1274 if (cscan->scan.scanrelid > 0)
1275 cscan->scan.scanrelid += rtoffset;
1277 if (cscan->custom_scan_tlist != NIL || cscan->scan.scanrelid == 0)
1279 /* Adjust tlist, qual, custom_exprs to reference custom scan tuple */
1280 indexed_tlist *itlist = build_tlist_index(cscan->custom_scan_tlist);
1282 cscan->scan.plan.targetlist = (List *)
1283 fix_upper_expr(root,
1284 (Node *) cscan->scan.plan.targetlist,
1288 cscan->scan.plan.qual = (List *)
1289 fix_upper_expr(root,
1290 (Node *) cscan->scan.plan.qual,
1294 cscan->custom_exprs = (List *)
1295 fix_upper_expr(root,
1296 (Node *) cscan->custom_exprs,
1301 /* custom_scan_tlist itself just needs fix_scan_list() adjustments */
1302 cscan->custom_scan_tlist =
1303 fix_scan_list(root, cscan->custom_scan_tlist, rtoffset);
1307 /* Adjust tlist, qual, custom_exprs in the standard way */
1308 cscan->scan.plan.targetlist =
1309 fix_scan_list(root, cscan->scan.plan.targetlist, rtoffset);
1310 cscan->scan.plan.qual =
1311 fix_scan_list(root, cscan->scan.plan.qual, rtoffset);
1312 cscan->custom_exprs =
1313 fix_scan_list(root, cscan->custom_exprs, rtoffset);
1316 /* Adjust child plan-nodes recursively, if needed */
1317 foreach(lc, cscan->custom_plans)
1319 lfirst(lc) = set_plan_refs(root, (Plan *) lfirst(lc), rtoffset);
1322 /* Adjust custom_relids if needed */
1325 Bitmapset *tempset = NULL;
1328 while ((x = bms_next_member(cscan->custom_relids, x)) >= 0)
1329 tempset = bms_add_member(tempset, x + rtoffset);
1330 cscan->custom_relids = tempset;
1338 * fix_scan_expr and friends do this enough times that it's worth having
1339 * a bespoke routine instead of using the generic copyObject() function.
1344 Var *newvar = (Var *) palloc(sizeof(Var));
1352 * Do generic set_plan_references processing on an expression node
1354 * This is code that is common to all variants of expression-fixing.
1355 * We must look up operator opcode info for OpExpr and related nodes,
1356 * add OIDs from regclass Const nodes into root->glob->relationOids, and
1357 * add PlanInvalItems for user-defined functions into root->glob->invalItems.
1358 * We also fill in column index lists for GROUPING() expressions.
1360 * We assume it's okay to update opcode info in-place. So this could possibly
1361 * scribble on the planner's input data structures, but it's OK.
1364 fix_expr_common(PlannerInfo *root, Node *node)
1366 /* We assume callers won't call us on a NULL pointer */
1367 if (IsA(node, Aggref))
1369 record_plan_function_dependency(root,
1370 ((Aggref *) node)->aggfnoid);
1372 else if (IsA(node, WindowFunc))
1374 record_plan_function_dependency(root,
1375 ((WindowFunc *) node)->winfnoid);
1377 else if (IsA(node, FuncExpr))
1379 record_plan_function_dependency(root,
1380 ((FuncExpr *) node)->funcid);
1382 else if (IsA(node, OpExpr))
1384 set_opfuncid((OpExpr *) node);
1385 record_plan_function_dependency(root,
1386 ((OpExpr *) node)->opfuncid);
1388 else if (IsA(node, DistinctExpr))
1390 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1391 record_plan_function_dependency(root,
1392 ((DistinctExpr *) node)->opfuncid);
1394 else if (IsA(node, NullIfExpr))
1396 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1397 record_plan_function_dependency(root,
1398 ((NullIfExpr *) node)->opfuncid);
1400 else if (IsA(node, ScalarArrayOpExpr))
1402 set_sa_opfuncid((ScalarArrayOpExpr *) node);
1403 record_plan_function_dependency(root,
1404 ((ScalarArrayOpExpr *) node)->opfuncid);
1406 else if (IsA(node, Const))
1408 Const *con = (Const *) node;
1410 /* Check for regclass reference */
1411 if (ISREGCLASSCONST(con))
1412 root->glob->relationOids =
1413 lappend_oid(root->glob->relationOids,
1414 DatumGetObjectId(con->constvalue));
1416 else if (IsA(node, GroupingFunc))
1418 GroupingFunc *g = (GroupingFunc *) node;
1419 AttrNumber *grouping_map = root->grouping_map;
1421 /* If there are no grouping sets, we don't need this. */
1423 Assert(grouping_map || g->cols == NIL);
1430 foreach(lc, g->refs)
1432 cols = lappend_int(cols, grouping_map[lfirst_int(lc)]);
1435 Assert(!g->cols || equal(cols, g->cols));
1445 * Do set_plan_references processing on a Param
1447 * If it's a PARAM_MULTIEXPR, replace it with the appropriate Param from
1448 * root->multiexpr_params; otherwise no change is needed.
1449 * Just for paranoia's sake, we make a copy of the node in either case.
1452 fix_param_node(PlannerInfo *root, Param *p)
1454 if (p->paramkind == PARAM_MULTIEXPR)
1456 int subqueryid = p->paramid >> 16;
1457 int colno = p->paramid & 0xFFFF;
1460 if (subqueryid <= 0 ||
1461 subqueryid > list_length(root->multiexpr_params))
1462 elog(ERROR, "unexpected PARAM_MULTIEXPR ID: %d", p->paramid);
1463 params = (List *) list_nth(root->multiexpr_params, subqueryid - 1);
1464 if (colno <= 0 || colno > list_length(params))
1465 elog(ERROR, "unexpected PARAM_MULTIEXPR ID: %d", p->paramid);
1466 return copyObject(list_nth(params, colno - 1));
1468 return (Node *) copyObject(p);
1473 * Do set_plan_references processing on a scan-level expression
1475 * This consists of incrementing all Vars' varnos by rtoffset,
1476 * replacing PARAM_MULTIEXPR Params, expanding PlaceHolderVars,
1477 * replacing Aggref nodes that should be replaced by initplan output Params,
1478 * looking up operator opcode info for OpExpr and related nodes,
1479 * and adding OIDs from regclass Const nodes into root->glob->relationOids.
1482 fix_scan_expr(PlannerInfo *root, Node *node, int rtoffset)
1484 fix_scan_expr_context context;
1486 context.root = root;
1487 context.rtoffset = rtoffset;
1489 if (rtoffset != 0 ||
1490 root->multiexpr_params != NIL ||
1491 root->glob->lastPHId != 0 ||
1492 root->minmax_aggs != NIL)
1494 return fix_scan_expr_mutator(node, &context);
1499 * If rtoffset == 0, we don't need to change any Vars, and if there
1500 * are no MULTIEXPR subqueries then we don't need to replace
1501 * PARAM_MULTIEXPR Params, and if there are no placeholders anywhere
1502 * we won't need to remove them, and if there are no minmax Aggrefs we
1503 * won't need to replace them. Then it's OK to just scribble on the
1504 * input node tree instead of copying (since the only change, filling
1505 * in any unset opfuncid fields, is harmless). This saves just enough
1506 * cycles to be noticeable on trivial queries.
1508 (void) fix_scan_expr_walker(node, &context);
1514 fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context)
1520 Var *var = copyVar((Var *) node);
1522 Assert(var->varlevelsup == 0);
1525 * We should not see any Vars marked INNER_VAR or OUTER_VAR. But an
1526 * indexqual expression could contain INDEX_VAR Vars.
1528 Assert(var->varno != INNER_VAR);
1529 Assert(var->varno != OUTER_VAR);
1530 if (!IS_SPECIAL_VARNO(var->varno))
1531 var->varno += context->rtoffset;
1532 if (var->varnoold > 0)
1533 var->varnoold += context->rtoffset;
1534 return (Node *) var;
1536 if (IsA(node, Param))
1537 return fix_param_node(context->root, (Param *) node);
1538 if (IsA(node, Aggref))
1540 Aggref *aggref = (Aggref *) node;
1542 /* See if the Aggref should be replaced by a Param */
1543 if (context->root->minmax_aggs != NIL &&
1544 list_length(aggref->args) == 1)
1546 TargetEntry *curTarget = (TargetEntry *) linitial(aggref->args);
1549 foreach(lc, context->root->minmax_aggs)
1551 MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
1553 if (mminfo->aggfnoid == aggref->aggfnoid &&
1554 equal(mminfo->target, curTarget->expr))
1555 return (Node *) copyObject(mminfo->param);
1558 /* If no match, just fall through to process it normally */
1560 if (IsA(node, CurrentOfExpr))
1562 CurrentOfExpr *cexpr = (CurrentOfExpr *) copyObject(node);
1564 Assert(cexpr->cvarno != INNER_VAR);
1565 Assert(cexpr->cvarno != OUTER_VAR);
1566 if (!IS_SPECIAL_VARNO(cexpr->cvarno))
1567 cexpr->cvarno += context->rtoffset;
1568 return (Node *) cexpr;
1570 if (IsA(node, PlaceHolderVar))
1572 /* At scan level, we should always just evaluate the contained expr */
1573 PlaceHolderVar *phv = (PlaceHolderVar *) node;
1575 return fix_scan_expr_mutator((Node *) phv->phexpr, context);
1577 fix_expr_common(context->root, node);
1578 return expression_tree_mutator(node, fix_scan_expr_mutator,
1583 fix_scan_expr_walker(Node *node, fix_scan_expr_context *context)
1587 Assert(!IsA(node, PlaceHolderVar));
1588 fix_expr_common(context->root, node);
1589 return expression_tree_walker(node, fix_scan_expr_walker,
1594 * set_join_references
1595 * Modify the target list and quals of a join node to reference its
1596 * subplans, by setting the varnos to OUTER_VAR or INNER_VAR and setting
1597 * attno values to the result domain number of either the corresponding
1598 * outer or inner join tuple item. Also perform opcode lookup for these
1599 * expressions, and add regclass OIDs to root->glob->relationOids.
1602 set_join_references(PlannerInfo *root, Join *join, int rtoffset)
1604 Plan *outer_plan = join->plan.lefttree;
1605 Plan *inner_plan = join->plan.righttree;
1606 indexed_tlist *outer_itlist;
1607 indexed_tlist *inner_itlist;
1609 outer_itlist = build_tlist_index(outer_plan->targetlist);
1610 inner_itlist = build_tlist_index(inner_plan->targetlist);
1613 * First process the joinquals (including merge or hash clauses). These
1614 * are logically below the join so they can always use all values
1615 * available from the input tlists. It's okay to also handle
1616 * NestLoopParams now, because those couldn't refer to nullable
1619 join->joinqual = fix_join_expr(root,
1626 /* Now do join-type-specific stuff */
1627 if (IsA(join, NestLoop))
1629 NestLoop *nl = (NestLoop *) join;
1632 foreach(lc, nl->nestParams)
1634 NestLoopParam *nlp = (NestLoopParam *) lfirst(lc);
1636 nlp->paramval = (Var *) fix_upper_expr(root,
1637 (Node *) nlp->paramval,
1641 /* Check we replaced any PlaceHolderVar with simple Var */
1642 if (!(IsA(nlp->paramval, Var) &&
1643 nlp->paramval->varno == OUTER_VAR))
1644 elog(ERROR, "NestLoopParam was not reduced to a simple Var");
1647 else if (IsA(join, MergeJoin))
1649 MergeJoin *mj = (MergeJoin *) join;
1651 mj->mergeclauses = fix_join_expr(root,
1658 else if (IsA(join, HashJoin))
1660 HashJoin *hj = (HashJoin *) join;
1662 hj->hashclauses = fix_join_expr(root,
1671 * Now we need to fix up the targetlist and qpqual, which are logically
1672 * above the join. This means they should not re-use any input expression
1673 * that was computed in the nullable side of an outer join. Vars and
1674 * PlaceHolderVars are fine, so we can implement this restriction just by
1675 * clearing has_non_vars in the indexed_tlist structs.
1677 * XXX This is a grotty workaround for the fact that we don't clearly
1678 * distinguish between a Var appearing below an outer join and the "same"
1679 * Var appearing above it. If we did, we'd not need to hack the matching
1682 switch (join->jointype)
1687 inner_itlist->has_non_vars = false;
1690 outer_itlist->has_non_vars = false;
1693 outer_itlist->has_non_vars = false;
1694 inner_itlist->has_non_vars = false;
1700 join->plan.targetlist = fix_join_expr(root,
1701 join->plan.targetlist,
1706 join->plan.qual = fix_join_expr(root,
1713 pfree(outer_itlist);
1714 pfree(inner_itlist);
1718 * set_upper_references
1719 * Update the targetlist and quals of an upper-level plan node
1720 * to refer to the tuples returned by its lefttree subplan.
1721 * Also perform opcode lookup for these expressions, and
1722 * add regclass OIDs to root->glob->relationOids.
1724 * This is used for single-input plan types like Agg, Group, Result.
1726 * In most cases, we have to match up individual Vars in the tlist and
1727 * qual expressions with elements of the subplan's tlist (which was
1728 * generated by flattening these selfsame expressions, so it should have all
1729 * the required variables). There is an important exception, however:
1730 * depending on where we are in the plan tree, sort/group columns may have
1731 * been pushed into the subplan tlist unflattened. If these values are also
1732 * needed in the output then we want to reference the subplan tlist element
1733 * rather than recomputing the expression.
1736 set_upper_references(PlannerInfo *root, Plan *plan, int rtoffset)
1738 Plan *subplan = plan->lefttree;
1739 indexed_tlist *subplan_itlist;
1740 List *output_targetlist;
1743 subplan_itlist = build_tlist_index(subplan->targetlist);
1745 output_targetlist = NIL;
1746 foreach(l, plan->targetlist)
1748 TargetEntry *tle = (TargetEntry *) lfirst(l);
1751 /* If it's a sort/group item, first try to match by sortref */
1752 if (tle->ressortgroupref != 0)
1755 search_indexed_tlist_for_sortgroupref(tle->expr,
1756 tle->ressortgroupref,
1760 newexpr = fix_upper_expr(root,
1767 newexpr = fix_upper_expr(root,
1772 tle = flatCopyTargetEntry(tle);
1773 tle->expr = (Expr *) newexpr;
1774 output_targetlist = lappend(output_targetlist, tle);
1776 plan->targetlist = output_targetlist;
1778 plan->qual = (List *)
1779 fix_upper_expr(root,
1780 (Node *) plan->qual,
1785 pfree(subplan_itlist);
1789 * set_param_references
1790 * Initialize the initParam list in Gather or Gather merge node such that
1791 * it contains reference of all the params that needs to be evaluated
1792 * before execution of the node. It contains the initplan params that are
1793 * being passed to the plan nodes below it.
1796 set_param_references(PlannerInfo *root, Plan *plan)
1798 Assert(IsA(plan, Gather) ||IsA(plan, GatherMerge));
1800 if (plan->lefttree->extParam)
1803 Bitmapset *initSetParam = NULL;
1806 for (proot = root; proot != NULL; proot = proot->parent_root)
1808 foreach(l, proot->init_plans)
1810 SubPlan *initsubplan = (SubPlan *) lfirst(l);
1813 foreach(l2, initsubplan->setParam)
1815 initSetParam = bms_add_member(initSetParam, lfirst_int(l2));
1821 * Remember the list of all external initplan params that are used by
1822 * the children of Gather or Gather merge node.
1824 if (IsA(plan, Gather))
1825 ((Gather *) plan)->initParam =
1826 bms_intersect(plan->lefttree->extParam, initSetParam);
1828 ((GatherMerge *) plan)->initParam =
1829 bms_intersect(plan->lefttree->extParam, initSetParam);
1834 * Recursively scan an expression tree and convert Aggrefs to the proper
1835 * intermediate form for combining aggregates. This means (1) replacing each
1836 * one's argument list with a single argument that is the original Aggref
1837 * modified to show partial aggregation and (2) changing the upper Aggref to
1838 * show combining aggregation.
1840 * After this step, set_upper_references will replace the partial Aggrefs
1841 * with Vars referencing the lower Agg plan node's outputs, so that the final
1842 * form seen by the executor is a combining Aggref with a Var as input.
1844 * It's rather messy to postpone this step until setrefs.c; ideally it'd be
1845 * done in createplan.c. The difficulty is that once we modify the Aggref
1846 * expressions, they will no longer be equal() to their original form and
1847 * so cross-plan-node-level matches will fail. So this has to happen after
1848 * the plan node above the Agg has resolved its subplan references.
1851 convert_combining_aggrefs(Node *node, void *context)
1855 if (IsA(node, Aggref))
1857 Aggref *orig_agg = (Aggref *) node;
1861 /* Assert we've not chosen to partial-ize any unsupported cases */
1862 Assert(orig_agg->aggorder == NIL);
1863 Assert(orig_agg->aggdistinct == NIL);
1866 * Since aggregate calls can't be nested, we needn't recurse into the
1867 * arguments. But for safety, flat-copy the Aggref node itself rather
1868 * than modifying it in-place.
1870 child_agg = makeNode(Aggref);
1871 memcpy(child_agg, orig_agg, sizeof(Aggref));
1874 * For the parent Aggref, we want to copy all the fields of the
1875 * original aggregate *except* the args list, which we'll replace
1876 * below, and the aggfilter expression, which should be applied only
1877 * by the child not the parent. Rather than explicitly knowing about
1878 * all the other fields here, we can momentarily modify child_agg to
1879 * provide a suitable source for copyObject.
1881 child_agg->args = NIL;
1882 child_agg->aggfilter = NULL;
1883 parent_agg = copyObject(child_agg);
1884 child_agg->args = orig_agg->args;
1885 child_agg->aggfilter = orig_agg->aggfilter;
1888 * Now, set up child_agg to represent the first phase of partial
1889 * aggregation. For now, assume serialization is required.
1891 mark_partial_aggref(child_agg, AGGSPLIT_INITIAL_SERIAL);
1894 * And set up parent_agg to represent the second phase.
1896 parent_agg->args = list_make1(makeTargetEntry((Expr *) child_agg,
1898 mark_partial_aggref(parent_agg, AGGSPLIT_FINAL_DESERIAL);
1900 return (Node *) parent_agg;
1902 return expression_tree_mutator(node, convert_combining_aggrefs,
1907 * set_dummy_tlist_references
1908 * Replace the targetlist of an upper-level plan node with a simple
1909 * list of OUTER_VAR references to its child.
1911 * This is used for plan types like Sort and Append that don't evaluate
1912 * their targetlists. Although the executor doesn't care at all what's in
1913 * the tlist, EXPLAIN needs it to be realistic.
1915 * Note: we could almost use set_upper_references() here, but it fails for
1916 * Append for lack of a lefttree subplan. Single-purpose code is faster
1920 set_dummy_tlist_references(Plan *plan, int rtoffset)
1922 List *output_targetlist;
1925 output_targetlist = NIL;
1926 foreach(l, plan->targetlist)
1928 TargetEntry *tle = (TargetEntry *) lfirst(l);
1929 Var *oldvar = (Var *) tle->expr;
1933 * As in search_indexed_tlist_for_non_var(), we prefer to keep Consts
1934 * as Consts, not Vars referencing Consts. Here, there's no speed
1935 * advantage to be had, but it makes EXPLAIN output look cleaner, and
1936 * again it avoids confusing the executor.
1938 if (IsA(oldvar, Const))
1940 /* just reuse the existing TLE node */
1941 output_targetlist = lappend(output_targetlist, tle);
1945 newvar = makeVar(OUTER_VAR,
1947 exprType((Node *) oldvar),
1948 exprTypmod((Node *) oldvar),
1949 exprCollation((Node *) oldvar),
1951 if (IsA(oldvar, Var))
1953 newvar->varnoold = oldvar->varno + rtoffset;
1954 newvar->varoattno = oldvar->varattno;
1958 newvar->varnoold = 0; /* wasn't ever a plain Var */
1959 newvar->varoattno = 0;
1962 tle = flatCopyTargetEntry(tle);
1963 tle->expr = (Expr *) newvar;
1964 output_targetlist = lappend(output_targetlist, tle);
1966 plan->targetlist = output_targetlist;
1968 /* We don't touch plan->qual here */
1973 * build_tlist_index --- build an index data structure for a child tlist
1975 * In most cases, subplan tlists will be "flat" tlists with only Vars,
1976 * so we try to optimize that case by extracting information about Vars
1977 * in advance. Matching a parent tlist to a child is still an O(N^2)
1978 * operation, but at least with a much smaller constant factor than plain
1979 * tlist_member() searches.
1981 * The result of this function is an indexed_tlist struct to pass to
1982 * search_indexed_tlist_for_var() or search_indexed_tlist_for_non_var().
1983 * When done, the indexed_tlist may be freed with a single pfree().
1985 static indexed_tlist *
1986 build_tlist_index(List *tlist)
1988 indexed_tlist *itlist;
1992 /* Create data structure with enough slots for all tlist entries */
1993 itlist = (indexed_tlist *)
1994 palloc(offsetof(indexed_tlist, vars) +
1995 list_length(tlist) * sizeof(tlist_vinfo));
1997 itlist->tlist = tlist;
1998 itlist->has_ph_vars = false;
1999 itlist->has_non_vars = false;
2000 itlist->has_conv_whole_rows = false;
2002 /* Find the Vars and fill in the index array */
2003 vinfo = itlist->vars;
2006 TargetEntry *tle = (TargetEntry *) lfirst(l);
2008 if (tle->expr && IsA(tle->expr, Var))
2010 Var *var = (Var *) tle->expr;
2012 vinfo->varno = var->varno;
2013 vinfo->varattno = var->varattno;
2014 vinfo->resno = tle->resno;
2017 else if (tle->expr && IsA(tle->expr, PlaceHolderVar))
2018 itlist->has_ph_vars = true;
2019 else if (is_converted_whole_row_reference((Node *) tle->expr))
2020 itlist->has_conv_whole_rows = true;
2022 itlist->has_non_vars = true;
2025 itlist->num_vars = (vinfo - itlist->vars);
2031 * build_tlist_index_other_vars --- build a restricted tlist index
2033 * This is like build_tlist_index, but we only index tlist entries that
2034 * are Vars belonging to some rel other than the one specified. We will set
2035 * has_ph_vars (allowing PlaceHolderVars to be matched), but not has_non_vars
2036 * (so nothing other than Vars and PlaceHolderVars can be matched). In case of
2037 * DML, where this function will be used, returning lists from child relations
2038 * will be appended similar to a simple append relation. That does not require
2039 * fixing ConvertRowtypeExpr references. So, those are not considered here.
2041 static indexed_tlist *
2042 build_tlist_index_other_vars(List *tlist, Index ignore_rel)
2044 indexed_tlist *itlist;
2048 /* Create data structure with enough slots for all tlist entries */
2049 itlist = (indexed_tlist *)
2050 palloc(offsetof(indexed_tlist, vars) +
2051 list_length(tlist) * sizeof(tlist_vinfo));
2053 itlist->tlist = tlist;
2054 itlist->has_ph_vars = false;
2055 itlist->has_non_vars = false;
2056 itlist->has_conv_whole_rows = false;
2058 /* Find the desired Vars and fill in the index array */
2059 vinfo = itlist->vars;
2062 TargetEntry *tle = (TargetEntry *) lfirst(l);
2064 if (tle->expr && IsA(tle->expr, Var))
2066 Var *var = (Var *) tle->expr;
2068 if (var->varno != ignore_rel)
2070 vinfo->varno = var->varno;
2071 vinfo->varattno = var->varattno;
2072 vinfo->resno = tle->resno;
2076 else if (tle->expr && IsA(tle->expr, PlaceHolderVar))
2077 itlist->has_ph_vars = true;
2080 itlist->num_vars = (vinfo - itlist->vars);
2086 * search_indexed_tlist_for_var --- find a Var in an indexed tlist
2088 * If a match is found, return a copy of the given Var with suitably
2089 * modified varno/varattno (to wit, newvarno and the resno of the TLE entry).
2090 * Also ensure that varnoold is incremented by rtoffset.
2091 * If no match, return NULL.
2094 search_indexed_tlist_for_var(Var *var, indexed_tlist *itlist,
2095 Index newvarno, int rtoffset)
2097 Index varno = var->varno;
2098 AttrNumber varattno = var->varattno;
2102 vinfo = itlist->vars;
2103 i = itlist->num_vars;
2106 if (vinfo->varno == varno && vinfo->varattno == varattno)
2109 Var *newvar = copyVar(var);
2111 newvar->varno = newvarno;
2112 newvar->varattno = vinfo->resno;
2113 if (newvar->varnoold > 0)
2114 newvar->varnoold += rtoffset;
2119 return NULL; /* no match */
2123 * search_indexed_tlist_for_non_var --- find a non-Var in an indexed tlist
2125 * If a match is found, return a Var constructed to reference the tlist item.
2126 * If no match, return NULL.
2128 * NOTE: it is a waste of time to call this unless itlist->has_ph_vars or
2129 * itlist->has_non_vars. Furthermore, set_join_references() relies on being
2130 * able to prevent matching of non-Vars by clearing itlist->has_non_vars,
2131 * so there's a correctness reason not to call it unless that's set.
2134 search_indexed_tlist_for_non_var(Expr *node,
2135 indexed_tlist *itlist, Index newvarno)
2140 * If it's a simple Const, replacing it with a Var is silly, even if there
2141 * happens to be an identical Const below; a Var is more expensive to
2142 * execute than a Const. What's more, replacing it could confuse some
2143 * places in the executor that expect to see simple Consts for, eg,
2146 if (IsA(node, Const))
2149 tle = tlist_member(node, itlist->tlist);
2152 /* Found a matching subplan output expression */
2155 newvar = makeVarFromTargetEntry(newvarno, tle);
2156 newvar->varnoold = 0; /* wasn't ever a plain Var */
2157 newvar->varoattno = 0;
2160 return NULL; /* no match */
2164 * search_indexed_tlist_for_sortgroupref --- find a sort/group expression
2166 * If a match is found, return a Var constructed to reference the tlist item.
2167 * If no match, return NULL.
2169 * This is needed to ensure that we select the right subplan TLE in cases
2170 * where there are multiple textually-equal()-but-volatile sort expressions.
2171 * And it's also faster than search_indexed_tlist_for_non_var.
2174 search_indexed_tlist_for_sortgroupref(Expr *node,
2176 indexed_tlist *itlist,
2181 foreach(lc, itlist->tlist)
2183 TargetEntry *tle = (TargetEntry *) lfirst(lc);
2185 /* The equal() check should be redundant, but let's be paranoid */
2186 if (tle->ressortgroupref == sortgroupref &&
2187 equal(node, tle->expr))
2189 /* Found a matching subplan output expression */
2192 newvar = makeVarFromTargetEntry(newvarno, tle);
2193 newvar->varnoold = 0; /* wasn't ever a plain Var */
2194 newvar->varoattno = 0;
2198 return NULL; /* no match */
2203 * Create a new set of targetlist entries or join qual clauses by
2204 * changing the varno/varattno values of variables in the clauses
2205 * to reference target list values from the outer and inner join
2206 * relation target lists. Also perform opcode lookup and add
2207 * regclass OIDs to root->glob->relationOids.
2209 * This is used in three different scenarios:
2210 * 1) a normal join clause, where all the Vars in the clause *must* be
2211 * replaced by OUTER_VAR or INNER_VAR references. In this case
2212 * acceptable_rel should be zero so that any failure to match a Var will be
2213 * reported as an error.
2214 * 2) RETURNING clauses, which may contain both Vars of the target relation
2215 * and Vars of other relations. In this case we want to replace the
2216 * other-relation Vars by OUTER_VAR references, while leaving target Vars
2217 * alone. Thus inner_itlist = NULL and acceptable_rel = the ID of the
2218 * target relation should be passed.
2219 * 3) ON CONFLICT UPDATE SET/WHERE clauses. Here references to EXCLUDED are
2220 * to be replaced with INNER_VAR references, while leaving target Vars (the
2221 * to-be-updated relation) alone. Correspondingly inner_itlist is to be
2222 * EXCLUDED elements, outer_itlist = NULL and acceptable_rel the target
2225 * 'clauses' is the targetlist or list of join clauses
2226 * 'outer_itlist' is the indexed target list of the outer join relation,
2228 * 'inner_itlist' is the indexed target list of the inner join relation,
2230 * 'acceptable_rel' is either zero or the rangetable index of a relation
2231 * whose Vars may appear in the clause without provoking an error
2232 * 'rtoffset': how much to increment varnoold by
2234 * Returns the new expression tree. The original clause structure is
2238 fix_join_expr(PlannerInfo *root,
2240 indexed_tlist *outer_itlist,
2241 indexed_tlist *inner_itlist,
2242 Index acceptable_rel,
2245 fix_join_expr_context context;
2247 context.root = root;
2248 context.outer_itlist = outer_itlist;
2249 context.inner_itlist = inner_itlist;
2250 context.acceptable_rel = acceptable_rel;
2251 context.rtoffset = rtoffset;
2252 return (List *) fix_join_expr_mutator((Node *) clauses, &context);
2256 fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
2259 bool converted_whole_row;
2265 Var *var = (Var *) node;
2267 /* Look for the var in the input tlists, first in the outer */
2268 if (context->outer_itlist)
2270 newvar = search_indexed_tlist_for_var(var,
2271 context->outer_itlist,
2275 return (Node *) newvar;
2278 /* then in the inner. */
2279 if (context->inner_itlist)
2281 newvar = search_indexed_tlist_for_var(var,
2282 context->inner_itlist,
2286 return (Node *) newvar;
2289 /* If it's for acceptable_rel, adjust and return it */
2290 if (var->varno == context->acceptable_rel)
2293 var->varno += context->rtoffset;
2294 if (var->varnoold > 0)
2295 var->varnoold += context->rtoffset;
2296 return (Node *) var;
2299 /* No referent found for Var */
2300 elog(ERROR, "variable not found in subplan target lists");
2302 if (IsA(node, PlaceHolderVar))
2304 PlaceHolderVar *phv = (PlaceHolderVar *) node;
2306 /* See if the PlaceHolderVar has bubbled up from a lower plan node */
2307 if (context->outer_itlist && context->outer_itlist->has_ph_vars)
2309 newvar = search_indexed_tlist_for_non_var((Expr *) phv,
2310 context->outer_itlist,
2313 return (Node *) newvar;
2315 if (context->inner_itlist && context->inner_itlist->has_ph_vars)
2317 newvar = search_indexed_tlist_for_non_var((Expr *) phv,
2318 context->inner_itlist,
2321 return (Node *) newvar;
2324 /* If not supplied by input plans, evaluate the contained expr */
2325 return fix_join_expr_mutator((Node *) phv->phexpr, context);
2327 if (IsA(node, Param))
2328 return fix_param_node(context->root, (Param *) node);
2330 /* Try matching more complex expressions too, if tlists have any */
2331 converted_whole_row = is_converted_whole_row_reference(node);
2332 if (context->outer_itlist &&
2333 (context->outer_itlist->has_non_vars ||
2334 (context->outer_itlist->has_conv_whole_rows && converted_whole_row)))
2336 newvar = search_indexed_tlist_for_non_var((Expr *) node,
2337 context->outer_itlist,
2340 return (Node *) newvar;
2342 if (context->inner_itlist &&
2343 (context->inner_itlist->has_non_vars ||
2344 (context->inner_itlist->has_conv_whole_rows && converted_whole_row)))
2346 newvar = search_indexed_tlist_for_non_var((Expr *) node,
2347 context->inner_itlist,
2350 return (Node *) newvar;
2352 fix_expr_common(context->root, node);
2353 return expression_tree_mutator(node,
2354 fix_join_expr_mutator,
2360 * Modifies an expression tree so that all Var nodes reference outputs
2361 * of a subplan. Also looks for Aggref nodes that should be replaced
2362 * by initplan output Params. Also performs opcode lookup, and adds
2363 * regclass OIDs to root->glob->relationOids.
2365 * This is used to fix up target and qual expressions of non-join upper-level
2366 * plan nodes, as well as index-only scan nodes.
2368 * An error is raised if no matching var can be found in the subplan tlist
2369 * --- so this routine should only be applied to nodes whose subplans'
2370 * targetlists were generated by flattening the expressions used in the
2373 * If itlist->has_non_vars is true, then we try to match whole subexpressions
2374 * against elements of the subplan tlist, so that we can avoid recomputing
2375 * expressions that were already computed by the subplan. (This is relatively
2376 * expensive, so we don't want to try it in the common case where the
2377 * subplan tlist is just a flattened list of Vars.)
2379 * 'node': the tree to be fixed (a target item or qual)
2380 * 'subplan_itlist': indexed target list for subplan (or index)
2381 * 'newvarno': varno to use for Vars referencing tlist elements
2382 * 'rtoffset': how much to increment varnoold by
2384 * The resulting tree is a copy of the original in which all Var nodes have
2385 * varno = newvarno, varattno = resno of corresponding targetlist element.
2386 * The original tree is not modified.
2389 fix_upper_expr(PlannerInfo *root,
2391 indexed_tlist *subplan_itlist,
2395 fix_upper_expr_context context;
2397 context.root = root;
2398 context.subplan_itlist = subplan_itlist;
2399 context.newvarno = newvarno;
2400 context.rtoffset = rtoffset;
2401 return fix_upper_expr_mutator(node, &context);
2405 fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context)
2413 Var *var = (Var *) node;
2415 newvar = search_indexed_tlist_for_var(var,
2416 context->subplan_itlist,
2420 elog(ERROR, "variable not found in subplan target list");
2421 return (Node *) newvar;
2423 if (IsA(node, PlaceHolderVar))
2425 PlaceHolderVar *phv = (PlaceHolderVar *) node;
2427 /* See if the PlaceHolderVar has bubbled up from a lower plan node */
2428 if (context->subplan_itlist->has_ph_vars)
2430 newvar = search_indexed_tlist_for_non_var((Expr *) phv,
2431 context->subplan_itlist,
2434 return (Node *) newvar;
2436 /* If not supplied by input plan, evaluate the contained expr */
2437 return fix_upper_expr_mutator((Node *) phv->phexpr, context);
2439 if (IsA(node, Param))
2440 return fix_param_node(context->root, (Param *) node);
2441 if (IsA(node, Aggref))
2443 Aggref *aggref = (Aggref *) node;
2445 /* See if the Aggref should be replaced by a Param */
2446 if (context->root->minmax_aggs != NIL &&
2447 list_length(aggref->args) == 1)
2449 TargetEntry *curTarget = (TargetEntry *) linitial(aggref->args);
2452 foreach(lc, context->root->minmax_aggs)
2454 MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
2456 if (mminfo->aggfnoid == aggref->aggfnoid &&
2457 equal(mminfo->target, curTarget->expr))
2458 return (Node *) copyObject(mminfo->param);
2461 /* If no match, just fall through to process it normally */
2463 /* Try matching more complex expressions too, if tlist has any */
2464 if (context->subplan_itlist->has_non_vars ||
2465 (context->subplan_itlist->has_conv_whole_rows &&
2466 is_converted_whole_row_reference(node)))
2468 newvar = search_indexed_tlist_for_non_var((Expr *) node,
2469 context->subplan_itlist,
2472 return (Node *) newvar;
2474 fix_expr_common(context->root, node);
2475 return expression_tree_mutator(node,
2476 fix_upper_expr_mutator,
2481 * set_returning_clause_references
2482 * Perform setrefs.c's work on a RETURNING targetlist
2484 * If the query involves more than just the result table, we have to
2485 * adjust any Vars that refer to other tables to reference junk tlist
2486 * entries in the top subplan's targetlist. Vars referencing the result
2487 * table should be left alone, however (the executor will evaluate them
2488 * using the actual heap tuple, after firing triggers if any). In the
2489 * adjusted RETURNING list, result-table Vars will have their original
2490 * varno (plus rtoffset), but Vars for other rels will have varno OUTER_VAR.
2492 * We also must perform opcode lookup and add regclass OIDs to
2493 * root->glob->relationOids.
2495 * 'rlist': the RETURNING targetlist to be fixed
2496 * 'topplan': the top subplan node that will be just below the ModifyTable
2497 * node (note it's not yet passed through set_plan_refs)
2498 * 'resultRelation': RT index of the associated result relation
2499 * 'rtoffset': how much to increment varnos by
2501 * Note: the given 'root' is for the parent query level, not the 'topplan'.
2502 * This does not matter currently since we only access the dependency-item
2503 * lists in root->glob, but it would need some hacking if we wanted a root
2504 * that actually matches the subplan.
2506 * Note: resultRelation is not yet adjusted by rtoffset.
2509 set_returning_clause_references(PlannerInfo *root,
2512 Index resultRelation,
2515 indexed_tlist *itlist;
2518 * We can perform the desired Var fixup by abusing the fix_join_expr
2519 * machinery that formerly handled inner indexscan fixup. We search the
2520 * top plan's targetlist for Vars of non-result relations, and use
2521 * fix_join_expr to convert RETURNING Vars into references to those tlist
2522 * entries, while leaving result-rel Vars as-is.
2524 * PlaceHolderVars will also be sought in the targetlist, but no
2525 * more-complex expressions will be. Note that it is not possible for a
2526 * PlaceHolderVar to refer to the result relation, since the result is
2527 * never below an outer join. If that case could happen, we'd have to be
2528 * prepared to pick apart the PlaceHolderVar and evaluate its contained
2529 * expression instead.
2531 itlist = build_tlist_index_other_vars(topplan->targetlist, resultRelation);
2533 rlist = fix_join_expr(root,
2546 /*****************************************************************************
2547 * QUERY DEPENDENCY MANAGEMENT
2548 *****************************************************************************/
2551 * record_plan_function_dependency
2552 * Mark the current plan as depending on a particular function.
2554 * This is exported so that the function-inlining code can record a
2555 * dependency on a function that it's removed from the plan tree.
2558 record_plan_function_dependency(PlannerInfo *root, Oid funcid)
2561 * For performance reasons, we don't bother to track built-in functions;
2562 * we just assume they'll never change (or at least not in ways that'd
2563 * invalidate plans using them). For this purpose we can consider a
2564 * built-in function to be one with OID less than FirstBootstrapObjectId.
2565 * Note that the OID generator guarantees never to generate such an OID
2566 * after startup, even at OID wraparound.
2568 if (funcid >= (Oid) FirstBootstrapObjectId)
2570 PlanInvalItem *inval_item = makeNode(PlanInvalItem);
2573 * It would work to use any syscache on pg_proc, but the easiest is
2574 * PROCOID since we already have the function's OID at hand. Note
2575 * that plancache.c knows we use PROCOID.
2577 inval_item->cacheId = PROCOID;
2578 inval_item->hashValue = GetSysCacheHashValue1(PROCOID,
2579 ObjectIdGetDatum(funcid));
2581 root->glob->invalItems = lappend(root->glob->invalItems, inval_item);
2586 * extract_query_dependencies
2587 * Given a rewritten, but not yet planned, query or queries
2588 * (i.e. a Query node or list of Query nodes), extract dependencies
2589 * just as set_plan_references would do. Also detect whether any
2590 * rewrite steps were affected by RLS.
2592 * This is needed by plancache.c to handle invalidation of cached unplanned
2596 extract_query_dependencies(Node *query,
2597 List **relationOids,
2599 bool *hasRowSecurity)
2604 /* Make up dummy planner state so we can use this module's machinery */
2605 MemSet(&glob, 0, sizeof(glob));
2606 glob.type = T_PlannerGlobal;
2607 glob.relationOids = NIL;
2608 glob.invalItems = NIL;
2609 /* Hack: we use glob.dependsOnRole to collect hasRowSecurity flags */
2610 glob.dependsOnRole = false;
2612 MemSet(&root, 0, sizeof(root));
2613 root.type = T_PlannerInfo;
2616 (void) extract_query_dependencies_walker(query, &root);
2618 *relationOids = glob.relationOids;
2619 *invalItems = glob.invalItems;
2620 *hasRowSecurity = glob.dependsOnRole;
2624 extract_query_dependencies_walker(Node *node, PlannerInfo *context)
2628 Assert(!IsA(node, PlaceHolderVar));
2629 /* Extract function dependencies and check for regclass Consts */
2630 fix_expr_common(context, node);
2631 if (IsA(node, Query))
2633 Query *query = (Query *) node;
2636 if (query->commandType == CMD_UTILITY)
2639 * Ignore utility statements, except those (such as EXPLAIN) that
2640 * contain a parsed-but-not-planned query.
2642 query = UtilityContainsQuery(query->utilityStmt);
2647 /* Remember if any Query has RLS quals applied by rewriter */
2648 if (query->hasRowSecurity)
2649 context->glob->dependsOnRole = true;
2651 /* Collect relation OIDs in this Query's rtable */
2652 foreach(lc, query->rtable)
2654 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
2656 if (rte->rtekind == RTE_RELATION)
2657 context->glob->relationOids =
2658 lappend_oid(context->glob->relationOids, rte->relid);
2659 else if (rte->rtekind == RTE_NAMEDTUPLESTORE &&
2660 OidIsValid(rte->relid))
2661 context->glob->relationOids =
2662 lappend_oid(context->glob->relationOids,
2666 /* And recurse into the query's subexpressions */
2667 return query_tree_walker(query, extract_query_dependencies_walker,
2668 (void *) context, 0);
2670 return expression_tree_walker(node, extract_query_dependencies_walker,
2675 * is_converted_whole_row_reference
2676 * If the given node is a ConvertRowtypeExpr encapsulating a whole-row
2677 * reference as implicit cast, return true. Otherwise return false.
2680 is_converted_whole_row_reference(Node *node)
2682 ConvertRowtypeExpr *convexpr;
2684 if (!node || !IsA(node, ConvertRowtypeExpr))
2687 /* Traverse nested ConvertRowtypeExpr's. */
2688 convexpr = castNode(ConvertRowtypeExpr, node);
2689 while (convexpr->convertformat == COERCE_IMPLICIT_CAST &&
2690 IsA(convexpr->arg, ConvertRowtypeExpr))
2691 convexpr = castNode(ConvertRowtypeExpr, convexpr->arg);
2693 if (IsA(convexpr->arg, Var))
2695 Var *var = castNode(Var, convexpr->arg);
2697 if (var->varattno == 0)