1 /*-------------------------------------------------------------------------
4 * Post-processing of a completed plan tree: fix references to subplan
5 * vars, and compute regproc values for operators
7 * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * $PostgreSQL: pgsql/src/backend/optimizer/plan/setrefs.c,v 1.136 2007/06/11 01:16:23 tgl Exp $
14 *-------------------------------------------------------------------------
18 #include "nodes/makefuncs.h"
19 #include "optimizer/clauses.h"
20 #include "optimizer/planmain.h"
21 #include "optimizer/tlist.h"
22 #include "parser/parse_expr.h"
23 #include "parser/parsetree.h"
24 #include "utils/lsyscache.h"
29 Index varno; /* RT index of Var */
30 AttrNumber varattno; /* attr number of Var */
31 AttrNumber resno; /* TLE position of Var */
36 List *tlist; /* underlying target list */
37 int num_vars; /* number of plain Var tlist entries */
38 bool has_non_vars; /* are there non-plain-Var entries? */
39 /* array of num_vars entries: */
40 tlist_vinfo vars[1]; /* VARIABLE LENGTH ARRAY */
41 } indexed_tlist; /* VARIABLE LENGTH STRUCT */
46 } fix_scan_expr_context;
50 indexed_tlist *outer_itlist;
51 indexed_tlist *inner_itlist;
54 } fix_join_expr_context;
58 indexed_tlist *subplan_itlist;
60 } fix_upper_expr_context;
62 #define fix_scan_list(lst, rtoffset) \
63 ((List *) fix_scan_expr((Node *) (lst), rtoffset))
65 static Plan *set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset);
66 static Plan *set_subqueryscan_references(PlannerGlobal *glob,
69 static bool trivial_subqueryscan(SubqueryScan *plan);
70 static Node *fix_scan_expr(Node *node, int rtoffset);
71 static Node *fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context);
72 static void set_join_references(Join *join, int rtoffset);
73 static void set_inner_join_references(Plan *inner_plan,
74 indexed_tlist *outer_itlist);
75 static void set_upper_references(Plan *plan, int rtoffset);
76 static void set_dummy_tlist_references(Plan *plan, int rtoffset);
77 static indexed_tlist *build_tlist_index(List *tlist);
78 static Var *search_indexed_tlist_for_var(Var *var,
79 indexed_tlist *itlist,
82 static Var *search_indexed_tlist_for_non_var(Node *node,
83 indexed_tlist *itlist,
85 static List *fix_join_expr(List *clauses,
86 indexed_tlist *outer_itlist,
87 indexed_tlist *inner_itlist,
88 Index acceptable_rel, int rtoffset);
89 static Node *fix_join_expr_mutator(Node *node,
90 fix_join_expr_context *context);
91 static Node *fix_upper_expr(Node *node,
92 indexed_tlist *subplan_itlist,
94 static Node *fix_upper_expr_mutator(Node *node,
95 fix_upper_expr_context *context);
96 static bool fix_opfuncids_walker(Node *node, void *context);
99 /*****************************************************************************
103 *****************************************************************************/
106 * set_plan_references
108 * This is the final processing pass of the planner/optimizer. The plan
109 * tree is complete; we just have to adjust some representational details
110 * for the convenience of the executor:
112 * 1. We flatten the various subquery rangetables into a single list, and
113 * zero out RangeTblEntry fields that are not useful to the executor.
115 * 2. We adjust Vars in scan nodes to be consistent with the flat rangetable.
117 * 3. We adjust Vars in upper plan nodes to refer to the outputs of their
120 * 4. We compute regproc OIDs for operators (ie, we look up the function
121 * that implements each op).
123 * We also perform one final optimization step, which is to delete
124 * SubqueryScan plan nodes that aren't doing anything useful (ie, have
125 * no qual and a no-op targetlist). The reason for doing this last is that
126 * it can't readily be done before set_plan_references, because it would
127 * break set_upper_references: the Vars in the subquery's top tlist
128 * wouldn't match up with the Vars in the outer plan tree. The SubqueryScan
129 * serves a necessary function as a buffer between outer query and subquery
130 * variable numbering ... but after we've flattened the rangetable this is
131 * no longer a problem, since there's only one rtindex namespace.
133 * set_plan_references recursively traverses the whole plan tree.
136 * glob: global data for planner run
137 * plan: the topmost node of the plan
138 * rtable: the rangetable for the current subquery
140 * The return value is normally the same Plan node passed in, but can be
141 * different when the passed-in Plan is a SubqueryScan we decide isn't needed.
143 * The flattened rangetable entries are appended to glob->finalrtable.
145 * Notice that we modify Plan nodes in-place, but use expression_tree_mutator
146 * to process targetlist and qual expressions. We can assume that the Plan
147 * nodes were just built by the planner and are not multiply referenced, but
148 * it's not so safe to assume that for expression tree nodes.
151 set_plan_references(PlannerGlobal *glob, Plan *plan, List *rtable)
153 int rtoffset = list_length(glob->finalrtable);
157 * In the flat rangetable, we zero out substructure pointers that are
158 * not needed by the executor; this reduces the storage space and
159 * copying cost for cached plans. We keep only the alias and eref
160 * Alias fields, which are needed by EXPLAIN.
164 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
165 RangeTblEntry *newrte;
167 /* flat copy to duplicate all the scalar fields */
168 newrte = (RangeTblEntry *) palloc(sizeof(RangeTblEntry));
169 memcpy(newrte, rte, sizeof(RangeTblEntry));
171 /* zap unneeded sub-structure */
172 newrte->subquery = NULL;
173 newrte->funcexpr = NULL;
174 newrte->funccoltypes = NIL;
175 newrte->funccoltypmods = NIL;
176 newrte->values_lists = NIL;
177 newrte->joinaliasvars = NIL;
179 glob->finalrtable = lappend(glob->finalrtable, newrte);
182 /* Now fix the Plan tree */
183 return set_plan_refs(glob, plan, rtoffset);
187 * set_plan_refs: recurse through the Plan nodes of a single subquery level
190 set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
198 * Plan-type-specific fixes
200 switch (nodeTag(plan))
204 SeqScan *splan = (SeqScan *) plan;
206 splan->scanrelid += rtoffset;
207 splan->plan.targetlist =
208 fix_scan_list(splan->plan.targetlist, rtoffset);
210 fix_scan_list(splan->plan.qual, rtoffset);
215 IndexScan *splan = (IndexScan *) plan;
217 splan->scan.scanrelid += rtoffset;
218 splan->scan.plan.targetlist =
219 fix_scan_list(splan->scan.plan.targetlist, rtoffset);
220 splan->scan.plan.qual =
221 fix_scan_list(splan->scan.plan.qual, rtoffset);
223 fix_scan_list(splan->indexqual, rtoffset);
224 splan->indexqualorig =
225 fix_scan_list(splan->indexqualorig, rtoffset);
228 case T_BitmapIndexScan:
230 BitmapIndexScan *splan = (BitmapIndexScan *) plan;
232 splan->scan.scanrelid += rtoffset;
233 /* no need to fix targetlist and qual */
234 Assert(splan->scan.plan.targetlist == NIL);
235 Assert(splan->scan.plan.qual == NIL);
237 fix_scan_list(splan->indexqual, rtoffset);
238 splan->indexqualorig =
239 fix_scan_list(splan->indexqualorig, rtoffset);
242 case T_BitmapHeapScan:
244 BitmapHeapScan *splan = (BitmapHeapScan *) plan;
246 splan->scan.scanrelid += rtoffset;
247 splan->scan.plan.targetlist =
248 fix_scan_list(splan->scan.plan.targetlist, rtoffset);
249 splan->scan.plan.qual =
250 fix_scan_list(splan->scan.plan.qual, rtoffset);
251 splan->bitmapqualorig =
252 fix_scan_list(splan->bitmapqualorig, rtoffset);
257 TidScan *splan = (TidScan *) plan;
259 splan->scan.scanrelid += rtoffset;
260 splan->scan.plan.targetlist =
261 fix_scan_list(splan->scan.plan.targetlist, rtoffset);
262 splan->scan.plan.qual =
263 fix_scan_list(splan->scan.plan.qual, rtoffset);
265 fix_scan_list(splan->tidquals, rtoffset);
269 /* Needs special treatment, see comments below */
270 return set_subqueryscan_references(glob,
271 (SubqueryScan *) plan,
275 FunctionScan *splan = (FunctionScan *) plan;
277 splan->scan.scanrelid += rtoffset;
278 splan->scan.plan.targetlist =
279 fix_scan_list(splan->scan.plan.targetlist, rtoffset);
280 splan->scan.plan.qual =
281 fix_scan_list(splan->scan.plan.qual, rtoffset);
283 fix_scan_expr(splan->funcexpr, rtoffset);
288 ValuesScan *splan = (ValuesScan *) plan;
290 splan->scan.scanrelid += rtoffset;
291 splan->scan.plan.targetlist =
292 fix_scan_list(splan->scan.plan.targetlist, rtoffset);
293 splan->scan.plan.qual =
294 fix_scan_list(splan->scan.plan.qual, rtoffset);
295 splan->values_lists =
296 fix_scan_list(splan->values_lists, rtoffset);
303 set_join_references((Join *) plan, rtoffset);
313 * These plan types don't actually bother to evaluate their
314 * targetlists, because they just return their unmodified input
315 * tuples. Even though the targetlist won't be used by the
316 * executor, we fix it up for possible use by EXPLAIN (not to
317 * mention ease of debugging --- wrong varnos are very confusing).
319 set_dummy_tlist_references(plan, rtoffset);
321 * Since these plan types don't check quals either, we should not
322 * find any qual expression attached to them.
324 Assert(plan->qual == NIL);
328 Limit *splan = (Limit *) plan;
331 * Like the plan types above, Limit doesn't evaluate its tlist
332 * or quals. It does have live expressions for limit/offset,
333 * however; and those cannot contain subplan variable refs,
334 * so fix_scan_expr works for them.
336 set_dummy_tlist_references(plan, rtoffset);
337 Assert(splan->plan.qual == NIL);
340 fix_scan_expr(splan->limitOffset, rtoffset);
342 fix_scan_expr(splan->limitCount, rtoffset);
347 set_upper_references(plan, rtoffset);
351 Result *splan = (Result *) plan;
354 * Result may or may not have a subplan; if not, it's more
355 * like a scan node than an upper node.
357 if (splan->plan.lefttree != NULL)
358 set_upper_references(plan, rtoffset);
361 splan->plan.targetlist =
362 fix_scan_list(splan->plan.targetlist, rtoffset);
364 fix_scan_list(splan->plan.qual, rtoffset);
366 /* resconstantqual can't contain any subplan variable refs */
367 splan->resconstantqual =
368 fix_scan_expr(splan->resconstantqual, rtoffset);
373 Append *splan = (Append *) plan;
376 * Append, like Sort et al, doesn't actually evaluate its
377 * targetlist or check quals.
379 set_dummy_tlist_references(plan, rtoffset);
380 Assert(splan->plan.qual == NIL);
381 foreach(l, splan->appendplans)
383 lfirst(l) = set_plan_refs(glob,
391 BitmapAnd *splan = (BitmapAnd *) plan;
393 /* BitmapAnd works like Append, but has no tlist */
394 Assert(splan->plan.targetlist == NIL);
395 Assert(splan->plan.qual == NIL);
396 foreach(l, splan->bitmapplans)
398 lfirst(l) = set_plan_refs(glob,
406 BitmapOr *splan = (BitmapOr *) plan;
408 /* BitmapOr works like Append, but has no tlist */
409 Assert(splan->plan.targetlist == NIL);
410 Assert(splan->plan.qual == NIL);
411 foreach(l, splan->bitmapplans)
413 lfirst(l) = set_plan_refs(glob,
420 elog(ERROR, "unrecognized node type: %d",
421 (int) nodeTag(plan));
426 * Now recurse into child plans, if any
428 * NOTE: it is essential that we recurse into child plans AFTER we set
429 * subplan references in this plan's tlist and quals. If we did the
430 * reference-adjustments bottom-up, then we would fail to match this
431 * plan's var nodes against the already-modified nodes of the children.
433 plan->lefttree = set_plan_refs(glob, plan->lefttree, rtoffset);
434 plan->righttree = set_plan_refs(glob, plan->righttree, rtoffset);
440 * set_subqueryscan_references
441 * Do set_plan_references processing on a SubqueryScan
443 * We try to strip out the SubqueryScan entirely; if we can't, we have
444 * to do the normal processing on it.
447 set_subqueryscan_references(PlannerGlobal *glob,
453 /* First, recursively process the subplan */
454 plan->subplan = set_plan_references(glob, plan->subplan, plan->subrtable);
456 /* subrtable is no longer needed in the plan tree */
457 plan->subrtable = NIL;
459 if (trivial_subqueryscan(plan))
462 * We can omit the SubqueryScan node and just pull up the subplan.
467 result = plan->subplan;
469 /* We have to be sure we don't lose any initplans */
470 result->initPlan = list_concat(plan->scan.plan.initPlan,
474 * We also have to transfer the SubqueryScan's result-column names
475 * into the subplan, else columns sent to client will be improperly
476 * labeled if this is the topmost plan level. Copy the "source
477 * column" information too.
479 forboth(lp, plan->scan.plan.targetlist, lc, result->targetlist)
481 TargetEntry *ptle = (TargetEntry *) lfirst(lp);
482 TargetEntry *ctle = (TargetEntry *) lfirst(lc);
484 ctle->resname = ptle->resname;
485 ctle->resorigtbl = ptle->resorigtbl;
486 ctle->resorigcol = ptle->resorigcol;
492 * Keep the SubqueryScan node. We have to do the processing that
493 * set_plan_references would otherwise have done on it. Notice we do
494 * not do set_upper_references() here, because a SubqueryScan will
495 * always have been created with correct references to its subplan's
496 * outputs to begin with.
498 plan->scan.scanrelid += rtoffset;
499 plan->scan.plan.targetlist =
500 fix_scan_list(plan->scan.plan.targetlist, rtoffset);
501 plan->scan.plan.qual =
502 fix_scan_list(plan->scan.plan.qual, rtoffset);
504 result = (Plan *) plan;
511 * trivial_subqueryscan
512 * Detect whether a SubqueryScan can be deleted from the plan tree.
514 * We can delete it if it has no qual to check and the targetlist just
515 * regurgitates the output of the child plan.
518 trivial_subqueryscan(SubqueryScan *plan)
524 if (plan->scan.plan.qual != NIL)
527 if (list_length(plan->scan.plan.targetlist) !=
528 list_length(plan->subplan->targetlist))
529 return false; /* tlists not same length */
532 forboth(lp, plan->scan.plan.targetlist, lc, plan->subplan->targetlist)
534 TargetEntry *ptle = (TargetEntry *) lfirst(lp);
535 TargetEntry *ctle = (TargetEntry *) lfirst(lc);
537 if (ptle->resjunk != ctle->resjunk)
538 return false; /* tlist doesn't match junk status */
541 * We accept either a Var referencing the corresponding element of the
542 * subplan tlist, or a Const equaling the subplan element. See
543 * generate_setop_tlist() for motivation.
545 if (ptle->expr && IsA(ptle->expr, Var))
547 Var *var = (Var *) ptle->expr;
549 Assert(var->varno == plan->scan.scanrelid);
550 Assert(var->varlevelsup == 0);
551 if (var->varattno != attrno)
552 return false; /* out of order */
554 else if (ptle->expr && IsA(ptle->expr, Const))
556 if (!equal(ptle->expr, ctle->expr))
572 * fix_scan_expr and friends do this enough times that it's worth having
573 * a bespoke routine instead of using the generic copyObject() function.
578 Var *newvar = (Var *) palloc(sizeof(Var));
586 * Do set_plan_references processing on a scan-level expression
588 * This consists of incrementing all Vars' varnos by rtoffset and
589 * looking up operator opcode info for OpExpr and related nodes.
592 fix_scan_expr(Node *node, int rtoffset)
594 fix_scan_expr_context context;
596 context.rtoffset = rtoffset;
597 return fix_scan_expr_mutator(node, &context);
601 fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context)
607 Var *var = copyVar((Var *) node);
609 Assert(var->varlevelsup == 0);
611 * We should not see any Vars marked INNER, but in a nestloop inner
612 * scan there could be OUTER Vars. Leave them alone.
614 Assert(var->varno != INNER);
615 if (var->varno > 0 && var->varno != OUTER)
616 var->varno += context->rtoffset;
617 if (var->varnoold > 0)
618 var->varnoold += context->rtoffset;
621 if (IsA(node, CurrentOfExpr))
623 CurrentOfExpr *cexpr = (CurrentOfExpr *) copyObject(node);
625 Assert(cexpr->cvarno != INNER);
626 Assert(cexpr->cvarno != OUTER);
627 cexpr->cvarno += context->rtoffset;
628 return (Node *) cexpr;
631 * Since we update opcode info in-place, this part could possibly
632 * scribble on the planner's input data structures, but it's OK.
634 if (IsA(node, OpExpr))
635 set_opfuncid((OpExpr *) node);
636 else if (IsA(node, DistinctExpr))
637 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
638 else if (IsA(node, NullIfExpr))
639 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
640 else if (IsA(node, ScalarArrayOpExpr))
641 set_sa_opfuncid((ScalarArrayOpExpr *) node);
642 return expression_tree_mutator(node, fix_scan_expr_mutator,
647 * set_join_references
648 * Modify the target list and quals of a join node to reference its
649 * subplans, by setting the varnos to OUTER or INNER and setting attno
650 * values to the result domain number of either the corresponding outer
651 * or inner join tuple item. Also perform opcode lookup for these
654 * In the case of a nestloop with inner indexscan, we will also need to
655 * apply the same transformation to any outer vars appearing in the
656 * quals of the child indexscan. set_inner_join_references does that.
659 set_join_references(Join *join, int rtoffset)
661 Plan *outer_plan = join->plan.lefttree;
662 Plan *inner_plan = join->plan.righttree;
663 indexed_tlist *outer_itlist;
664 indexed_tlist *inner_itlist;
666 outer_itlist = build_tlist_index(outer_plan->targetlist);
667 inner_itlist = build_tlist_index(inner_plan->targetlist);
669 /* All join plans have tlist, qual, and joinqual */
670 join->plan.targetlist = fix_join_expr(join->plan.targetlist,
675 join->plan.qual = fix_join_expr(join->plan.qual,
680 join->joinqual = fix_join_expr(join->joinqual,
686 /* Now do join-type-specific stuff */
687 if (IsA(join, NestLoop))
689 /* This processing is split out to handle possible recursion */
690 set_inner_join_references(inner_plan, outer_itlist);
692 else if (IsA(join, MergeJoin))
694 MergeJoin *mj = (MergeJoin *) join;
696 mj->mergeclauses = fix_join_expr(mj->mergeclauses,
702 else if (IsA(join, HashJoin))
704 HashJoin *hj = (HashJoin *) join;
706 hj->hashclauses = fix_join_expr(hj->hashclauses,
718 * set_inner_join_references
719 * Handle join references appearing in an inner indexscan's quals
721 * To handle bitmap-scan plan trees, we have to be able to recurse down
722 * to the bottom BitmapIndexScan nodes; likewise, appendrel indexscans
723 * require recursing through Append nodes. This is split out as a separate
724 * function so that it can recurse.
726 * Note we do *not* apply any rtoffset for non-join Vars; this is because
727 * the quals will be processed again by fix_scan_expr when the set_plan_refs
728 * recursion reaches the inner indexscan, and so we'd have done it twice.
731 set_inner_join_references(Plan *inner_plan, indexed_tlist *outer_itlist)
733 if (IsA(inner_plan, IndexScan))
736 * An index is being used to reduce the number of tuples scanned in
737 * the inner relation. If there are join clauses being used with the
738 * index, we must update their outer-rel var nodes to refer to the
739 * outer side of the join.
741 IndexScan *innerscan = (IndexScan *) inner_plan;
742 List *indexqualorig = innerscan->indexqualorig;
744 /* No work needed if indexqual refers only to its own rel... */
745 if (NumRelids((Node *) indexqualorig) > 1)
747 Index innerrel = innerscan->scan.scanrelid;
749 /* only refs to outer vars get changed in the inner qual */
750 innerscan->indexqualorig = fix_join_expr(indexqualorig,
755 innerscan->indexqual = fix_join_expr(innerscan->indexqual,
762 * We must fix the inner qpqual too, if it has join clauses (this
763 * could happen if special operators are involved: some indexquals
764 * may get rechecked as qpquals).
766 if (NumRelids((Node *) inner_plan->qual) > 1)
767 inner_plan->qual = fix_join_expr(inner_plan->qual,
774 else if (IsA(inner_plan, BitmapIndexScan))
777 * Same, but index is being used within a bitmap plan.
779 BitmapIndexScan *innerscan = (BitmapIndexScan *) inner_plan;
780 List *indexqualorig = innerscan->indexqualorig;
782 /* No work needed if indexqual refers only to its own rel... */
783 if (NumRelids((Node *) indexqualorig) > 1)
785 Index innerrel = innerscan->scan.scanrelid;
787 /* only refs to outer vars get changed in the inner qual */
788 innerscan->indexqualorig = fix_join_expr(indexqualorig,
793 innerscan->indexqual = fix_join_expr(innerscan->indexqual,
798 /* no need to fix inner qpqual */
799 Assert(inner_plan->qual == NIL);
802 else if (IsA(inner_plan, BitmapHeapScan))
805 * The inner side is a bitmap scan plan. Fix the top node, and
806 * recurse to get the lower nodes.
808 * Note: create_bitmap_scan_plan removes clauses from bitmapqualorig
809 * if they are duplicated in qpqual, so must test these independently.
811 BitmapHeapScan *innerscan = (BitmapHeapScan *) inner_plan;
812 Index innerrel = innerscan->scan.scanrelid;
813 List *bitmapqualorig = innerscan->bitmapqualorig;
815 /* only refs to outer vars get changed in the inner qual */
816 if (NumRelids((Node *) bitmapqualorig) > 1)
817 innerscan->bitmapqualorig = fix_join_expr(bitmapqualorig,
824 * We must fix the inner qpqual too, if it has join clauses (this
825 * could happen if special operators are involved: some indexquals may
826 * get rechecked as qpquals).
828 if (NumRelids((Node *) inner_plan->qual) > 1)
829 inner_plan->qual = fix_join_expr(inner_plan->qual,
836 set_inner_join_references(inner_plan->lefttree, outer_itlist);
838 else if (IsA(inner_plan, BitmapAnd))
840 /* All we need do here is recurse */
841 BitmapAnd *innerscan = (BitmapAnd *) inner_plan;
844 foreach(l, innerscan->bitmapplans)
846 set_inner_join_references((Plan *) lfirst(l), outer_itlist);
849 else if (IsA(inner_plan, BitmapOr))
851 /* All we need do here is recurse */
852 BitmapOr *innerscan = (BitmapOr *) inner_plan;
855 foreach(l, innerscan->bitmapplans)
857 set_inner_join_references((Plan *) lfirst(l), outer_itlist);
860 else if (IsA(inner_plan, TidScan))
862 TidScan *innerscan = (TidScan *) inner_plan;
863 Index innerrel = innerscan->scan.scanrelid;
865 innerscan->tidquals = fix_join_expr(innerscan->tidquals,
871 else if (IsA(inner_plan, Append))
874 * The inner side is an append plan. Recurse to see if it contains
875 * indexscans that need to be fixed.
877 Append *appendplan = (Append *) inner_plan;
880 foreach(l, appendplan->appendplans)
882 set_inner_join_references((Plan *) lfirst(l), outer_itlist);
885 else if (IsA(inner_plan, Result))
887 /* Recurse through a gating Result node (similar to Append case) */
888 Result *result = (Result *) inner_plan;
890 if (result->plan.lefttree)
891 set_inner_join_references(result->plan.lefttree, outer_itlist);
896 * set_upper_references
897 * Update the targetlist and quals of an upper-level plan node
898 * to refer to the tuples returned by its lefttree subplan.
899 * Also perform opcode lookup for these expressions.
901 * This is used for single-input plan types like Agg, Group, Result.
903 * In most cases, we have to match up individual Vars in the tlist and
904 * qual expressions with elements of the subplan's tlist (which was
905 * generated by flatten_tlist() from these selfsame expressions, so it
906 * should have all the required variables). There is an important exception,
907 * however: GROUP BY and ORDER BY expressions will have been pushed into the
908 * subplan tlist unflattened. If these values are also needed in the output
909 * then we want to reference the subplan tlist element rather than recomputing
913 set_upper_references(Plan *plan, int rtoffset)
915 Plan *subplan = plan->lefttree;
916 indexed_tlist *subplan_itlist;
917 List *output_targetlist;
920 subplan_itlist = build_tlist_index(subplan->targetlist);
922 output_targetlist = NIL;
923 foreach(l, plan->targetlist)
925 TargetEntry *tle = (TargetEntry *) lfirst(l);
928 newexpr = fix_upper_expr((Node *) tle->expr,
931 tle = flatCopyTargetEntry(tle);
932 tle->expr = (Expr *) newexpr;
933 output_targetlist = lappend(output_targetlist, tle);
935 plan->targetlist = output_targetlist;
937 plan->qual = (List *)
938 fix_upper_expr((Node *) plan->qual,
942 pfree(subplan_itlist);
946 * set_dummy_tlist_references
947 * Replace the targetlist of an upper-level plan node with a simple
948 * list of OUTER references to its child.
950 * This is used for plan types like Sort and Append that don't evaluate
951 * their targetlists. Although the executor doesn't care at all what's in
952 * the tlist, EXPLAIN needs it to be realistic.
954 * Note: we could almost use set_upper_references() here, but it fails for
955 * Append for lack of a lefttree subplan. Single-purpose code is faster
959 set_dummy_tlist_references(Plan *plan, int rtoffset)
961 List *output_targetlist;
964 output_targetlist = NIL;
965 foreach(l, plan->targetlist)
967 TargetEntry *tle = (TargetEntry *) lfirst(l);
968 Var *oldvar = (Var *) tle->expr;
971 newvar = makeVar(OUTER,
973 exprType((Node *) oldvar),
974 exprTypmod((Node *) oldvar),
976 if (IsA(oldvar, Var))
978 newvar->varnoold = oldvar->varno + rtoffset;
979 newvar->varoattno = oldvar->varattno;
983 newvar->varnoold = 0; /* wasn't ever a plain Var */
984 newvar->varoattno = 0;
987 tle = flatCopyTargetEntry(tle);
988 tle->expr = (Expr *) newvar;
989 output_targetlist = lappend(output_targetlist, tle);
991 plan->targetlist = output_targetlist;
993 /* We don't touch plan->qual here */
998 * build_tlist_index --- build an index data structure for a child tlist
1000 * In most cases, subplan tlists will be "flat" tlists with only Vars,
1001 * so we try to optimize that case by extracting information about Vars
1002 * in advance. Matching a parent tlist to a child is still an O(N^2)
1003 * operation, but at least with a much smaller constant factor than plain
1004 * tlist_member() searches.
1006 * The result of this function is an indexed_tlist struct to pass to
1007 * search_indexed_tlist_for_var() or search_indexed_tlist_for_non_var().
1008 * When done, the indexed_tlist may be freed with a single pfree().
1010 static indexed_tlist *
1011 build_tlist_index(List *tlist)
1013 indexed_tlist *itlist;
1017 /* Create data structure with enough slots for all tlist entries */
1018 itlist = (indexed_tlist *)
1019 palloc(offsetof(indexed_tlist, vars) +
1020 list_length(tlist) * sizeof(tlist_vinfo));
1022 itlist->tlist = tlist;
1023 itlist->has_non_vars = false;
1025 /* Find the Vars and fill in the index array */
1026 vinfo = itlist->vars;
1029 TargetEntry *tle = (TargetEntry *) lfirst(l);
1031 if (tle->expr && IsA(tle->expr, Var))
1033 Var *var = (Var *) tle->expr;
1035 vinfo->varno = var->varno;
1036 vinfo->varattno = var->varattno;
1037 vinfo->resno = tle->resno;
1041 itlist->has_non_vars = true;
1044 itlist->num_vars = (vinfo - itlist->vars);
1050 * build_tlist_index_other_vars --- build a restricted tlist index
1052 * This is like build_tlist_index, but we only index tlist entries that
1053 * are Vars and belong to some rel other than the one specified.
1055 static indexed_tlist *
1056 build_tlist_index_other_vars(List *tlist, Index ignore_rel)
1058 indexed_tlist *itlist;
1062 /* Create data structure with enough slots for all tlist entries */
1063 itlist = (indexed_tlist *)
1064 palloc(offsetof(indexed_tlist, vars) +
1065 list_length(tlist) * sizeof(tlist_vinfo));
1067 itlist->tlist = tlist;
1068 itlist->has_non_vars = false;
1070 /* Find the desired Vars and fill in the index array */
1071 vinfo = itlist->vars;
1074 TargetEntry *tle = (TargetEntry *) lfirst(l);
1076 if (tle->expr && IsA(tle->expr, Var))
1078 Var *var = (Var *) tle->expr;
1080 if (var->varno != ignore_rel)
1082 vinfo->varno = var->varno;
1083 vinfo->varattno = var->varattno;
1084 vinfo->resno = tle->resno;
1090 itlist->num_vars = (vinfo - itlist->vars);
1096 * search_indexed_tlist_for_var --- find a Var in an indexed tlist
1098 * If a match is found, return a copy of the given Var with suitably
1099 * modified varno/varattno (to wit, newvarno and the resno of the TLE entry).
1100 * Also ensure that varnoold is incremented by rtoffset.
1101 * If no match, return NULL.
1104 search_indexed_tlist_for_var(Var *var, indexed_tlist *itlist,
1105 Index newvarno, int rtoffset)
1107 Index varno = var->varno;
1108 AttrNumber varattno = var->varattno;
1112 vinfo = itlist->vars;
1113 i = itlist->num_vars;
1116 if (vinfo->varno == varno && vinfo->varattno == varattno)
1119 Var *newvar = copyVar(var);
1121 newvar->varno = newvarno;
1122 newvar->varattno = vinfo->resno;
1123 if (newvar->varnoold > 0)
1124 newvar->varnoold += rtoffset;
1129 return NULL; /* no match */
1133 * search_indexed_tlist_for_non_var --- find a non-Var in an indexed tlist
1135 * If a match is found, return a Var constructed to reference the tlist item.
1136 * If no match, return NULL.
1138 * NOTE: it is a waste of time to call this if !itlist->has_non_vars
1141 search_indexed_tlist_for_non_var(Node *node,
1142 indexed_tlist *itlist, Index newvarno)
1146 tle = tlist_member(node, itlist->tlist);
1149 /* Found a matching subplan output expression */
1152 newvar = makeVar(newvarno,
1154 exprType((Node *) tle->expr),
1155 exprTypmod((Node *) tle->expr),
1157 newvar->varnoold = 0; /* wasn't ever a plain Var */
1158 newvar->varoattno = 0;
1161 return NULL; /* no match */
1166 * Create a new set of targetlist entries or join qual clauses by
1167 * changing the varno/varattno values of variables in the clauses
1168 * to reference target list values from the outer and inner join
1169 * relation target lists. Also perform opcode lookup.
1171 * This is used in two different scenarios: a normal join clause, where
1172 * all the Vars in the clause *must* be replaced by OUTER or INNER references;
1173 * and an indexscan being used on the inner side of a nestloop join.
1174 * In the latter case we want to replace the outer-relation Vars by OUTER
1175 * references, while Vars of the inner relation should be adjusted by rtoffset.
1176 * (We also implement RETURNING clause fixup using this second scenario.)
1178 * For a normal join, acceptable_rel should be zero so that any failure to
1179 * match a Var will be reported as an error. For the indexscan case,
1180 * pass inner_itlist = NULL and acceptable_rel = the (not-offseted-yet) ID
1181 * of the inner relation.
1183 * 'clauses' is the targetlist or list of join clauses
1184 * 'outer_itlist' is the indexed target list of the outer join relation
1185 * 'inner_itlist' is the indexed target list of the inner join relation,
1187 * 'acceptable_rel' is either zero or the rangetable index of a relation
1188 * whose Vars may appear in the clause without provoking an error.
1189 * 'rtoffset' is what to add to varno for Vars of acceptable_rel.
1191 * Returns the new expression tree. The original clause structure is
1195 fix_join_expr(List *clauses,
1196 indexed_tlist *outer_itlist,
1197 indexed_tlist *inner_itlist,
1198 Index acceptable_rel,
1201 fix_join_expr_context context;
1203 context.outer_itlist = outer_itlist;
1204 context.inner_itlist = inner_itlist;
1205 context.acceptable_rel = acceptable_rel;
1206 context.rtoffset = rtoffset;
1207 return (List *) fix_join_expr_mutator((Node *) clauses, &context);
1211 fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
1219 Var *var = (Var *) node;
1221 /* First look for the var in the input tlists */
1222 newvar = search_indexed_tlist_for_var(var,
1223 context->outer_itlist,
1227 return (Node *) newvar;
1228 if (context->inner_itlist)
1230 newvar = search_indexed_tlist_for_var(var,
1231 context->inner_itlist,
1235 return (Node *) newvar;
1238 /* If it's for acceptable_rel, adjust and return it */
1239 if (var->varno == context->acceptable_rel)
1242 var->varno += context->rtoffset;
1243 var->varnoold += context->rtoffset;
1244 return (Node *) var;
1247 /* No referent found for Var */
1248 elog(ERROR, "variable not found in subplan target lists");
1250 /* Try matching more complex expressions too, if tlists have any */
1251 if (context->outer_itlist->has_non_vars)
1253 newvar = search_indexed_tlist_for_non_var(node,
1254 context->outer_itlist,
1257 return (Node *) newvar;
1259 if (context->inner_itlist && context->inner_itlist->has_non_vars)
1261 newvar = search_indexed_tlist_for_non_var(node,
1262 context->inner_itlist,
1265 return (Node *) newvar;
1268 * Since we update opcode info in-place, this part could possibly
1269 * scribble on the planner's input data structures, but it's OK.
1271 if (IsA(node, OpExpr))
1272 set_opfuncid((OpExpr *) node);
1273 else if (IsA(node, DistinctExpr))
1274 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1275 else if (IsA(node, NullIfExpr))
1276 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1277 else if (IsA(node, ScalarArrayOpExpr))
1278 set_sa_opfuncid((ScalarArrayOpExpr *) node);
1279 return expression_tree_mutator(node,
1280 fix_join_expr_mutator,
1286 * Modifies an expression tree so that all Var nodes reference outputs
1287 * of a subplan. Also performs opcode lookup.
1289 * This is used to fix up target and qual expressions of non-join upper-level
1292 * An error is raised if no matching var can be found in the subplan tlist
1293 * --- so this routine should only be applied to nodes whose subplans'
1294 * targetlists were generated via flatten_tlist() or some such method.
1296 * If itlist->has_non_vars is true, then we try to match whole subexpressions
1297 * against elements of the subplan tlist, so that we can avoid recomputing
1298 * expressions that were already computed by the subplan. (This is relatively
1299 * expensive, so we don't want to try it in the common case where the
1300 * subplan tlist is just a flattened list of Vars.)
1302 * 'node': the tree to be fixed (a target item or qual)
1303 * 'subplan_itlist': indexed target list for subplan
1304 * 'rtoffset': how much to increment varnoold by
1306 * The resulting tree is a copy of the original in which all Var nodes have
1307 * varno = OUTER, varattno = resno of corresponding subplan target.
1308 * The original tree is not modified.
1311 fix_upper_expr(Node *node,
1312 indexed_tlist *subplan_itlist,
1315 fix_upper_expr_context context;
1317 context.subplan_itlist = subplan_itlist;
1318 context.rtoffset = rtoffset;
1319 return fix_upper_expr_mutator(node, &context);
1323 fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context)
1331 Var *var = (Var *) node;
1333 newvar = search_indexed_tlist_for_var(var,
1334 context->subplan_itlist,
1338 elog(ERROR, "variable not found in subplan target list");
1339 return (Node *) newvar;
1341 /* Try matching more complex expressions too, if tlist has any */
1342 if (context->subplan_itlist->has_non_vars)
1344 newvar = search_indexed_tlist_for_non_var(node,
1345 context->subplan_itlist,
1348 return (Node *) newvar;
1351 * Since we update opcode info in-place, this part could possibly
1352 * scribble on the planner's input data structures, but it's OK.
1354 if (IsA(node, OpExpr))
1355 set_opfuncid((OpExpr *) node);
1356 else if (IsA(node, DistinctExpr))
1357 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1358 else if (IsA(node, NullIfExpr))
1359 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1360 else if (IsA(node, ScalarArrayOpExpr))
1361 set_sa_opfuncid((ScalarArrayOpExpr *) node);
1362 return expression_tree_mutator(node,
1363 fix_upper_expr_mutator,
1368 * set_returning_clause_references
1369 * Perform setrefs.c's work on a RETURNING targetlist
1371 * If the query involves more than just the result table, we have to
1372 * adjust any Vars that refer to other tables to reference junk tlist
1373 * entries in the top plan's targetlist. Vars referencing the result
1374 * table should be left alone, however (the executor will evaluate them
1375 * using the actual heap tuple, after firing triggers if any). In the
1376 * adjusted RETURNING list, result-table Vars will still have their
1377 * original varno, but Vars for other rels will have varno OUTER.
1379 * We also must perform opcode lookup.
1381 * 'rlist': the RETURNING targetlist to be fixed
1382 * 'topplan': the top Plan node for the query (not yet passed through
1383 * set_plan_references)
1384 * 'resultRelation': RT index of the associated result relation
1386 * Note: we assume that result relations will have rtoffset zero, that is,
1387 * they are not coming from a subplan.
1390 set_returning_clause_references(List *rlist,
1392 Index resultRelation)
1394 indexed_tlist *itlist;
1397 * We can perform the desired Var fixup by abusing the fix_join_expr
1398 * machinery that normally handles inner indexscan fixup. We search the
1399 * top plan's targetlist for Vars of non-result relations, and use
1400 * fix_join_expr to convert RETURNING Vars into references to those
1401 * tlist entries, while leaving result-rel Vars as-is.
1403 itlist = build_tlist_index_other_vars(topplan->targetlist, resultRelation);
1405 rlist = fix_join_expr(rlist,
1416 /*****************************************************************************
1417 * OPERATOR REGPROC LOOKUP
1418 *****************************************************************************/
1422 * Calculate opfuncid field from opno for each OpExpr node in given tree.
1423 * The given tree can be anything expression_tree_walker handles.
1425 * The argument is modified in-place. (This is OK since we'd want the
1426 * same change for any node, even if it gets visited more than once due to
1427 * shared structure.)
1430 fix_opfuncids(Node *node)
1432 /* This tree walk requires no special setup, so away we go... */
1433 fix_opfuncids_walker(node, NULL);
1437 fix_opfuncids_walker(Node *node, void *context)
1441 if (IsA(node, OpExpr))
1442 set_opfuncid((OpExpr *) node);
1443 else if (IsA(node, DistinctExpr))
1444 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1445 else if (IsA(node, NullIfExpr))
1446 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1447 else if (IsA(node, ScalarArrayOpExpr))
1448 set_sa_opfuncid((ScalarArrayOpExpr *) node);
1449 return expression_tree_walker(node, fix_opfuncids_walker, context);
1454 * Set the opfuncid (procedure OID) in an OpExpr node,
1455 * if it hasn't been set already.
1457 * Because of struct equivalence, this can also be used for
1458 * DistinctExpr and NullIfExpr nodes.
1461 set_opfuncid(OpExpr *opexpr)
1463 if (opexpr->opfuncid == InvalidOid)
1464 opexpr->opfuncid = get_opcode(opexpr->opno);
1469 * As above, for ScalarArrayOpExpr nodes.
1472 set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
1474 if (opexpr->opfuncid == InvalidOid)
1475 opexpr->opfuncid = get_opcode(opexpr->opno);