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.134 2007/04/06 22:57:20 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))
570 * Do set_plan_references processing on a scan-level expression
572 * This consists of incrementing all Vars' varnos by rtoffset and
573 * looking up operator opcode info for OpExpr and related nodes.
576 fix_scan_expr(Node *node, int rtoffset)
578 fix_scan_expr_context context;
580 context.rtoffset = rtoffset;
581 return fix_scan_expr_mutator(node, &context);
585 fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context)
591 Var *var = (Var *) copyObject(node);
593 Assert(var->varlevelsup == 0);
595 * We should not see any Vars marked INNER, but in a nestloop inner
596 * scan there could be OUTER Vars. Leave them alone.
598 Assert(var->varno != INNER);
599 if (var->varno > 0 && var->varno != OUTER)
600 var->varno += context->rtoffset;
601 if (var->varnoold > 0)
602 var->varnoold += context->rtoffset;
606 * Since we update opcode info in-place, this part could possibly
607 * scribble on the planner's input data structures, but it's OK.
609 if (IsA(node, OpExpr))
610 set_opfuncid((OpExpr *) node);
611 else if (IsA(node, DistinctExpr))
612 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
613 else if (IsA(node, NullIfExpr))
614 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
615 else if (IsA(node, ScalarArrayOpExpr))
616 set_sa_opfuncid((ScalarArrayOpExpr *) node);
617 return expression_tree_mutator(node, fix_scan_expr_mutator,
622 * set_join_references
623 * Modify the target list and quals of a join node to reference its
624 * subplans, by setting the varnos to OUTER or INNER and setting attno
625 * values to the result domain number of either the corresponding outer
626 * or inner join tuple item. Also perform opcode lookup for these
629 * In the case of a nestloop with inner indexscan, we will also need to
630 * apply the same transformation to any outer vars appearing in the
631 * quals of the child indexscan. set_inner_join_references does that.
634 set_join_references(Join *join, int rtoffset)
636 Plan *outer_plan = join->plan.lefttree;
637 Plan *inner_plan = join->plan.righttree;
638 indexed_tlist *outer_itlist;
639 indexed_tlist *inner_itlist;
641 outer_itlist = build_tlist_index(outer_plan->targetlist);
642 inner_itlist = build_tlist_index(inner_plan->targetlist);
644 /* All join plans have tlist, qual, and joinqual */
645 join->plan.targetlist = fix_join_expr(join->plan.targetlist,
650 join->plan.qual = fix_join_expr(join->plan.qual,
655 join->joinqual = fix_join_expr(join->joinqual,
661 /* Now do join-type-specific stuff */
662 if (IsA(join, NestLoop))
664 /* This processing is split out to handle possible recursion */
665 set_inner_join_references(inner_plan, outer_itlist);
667 else if (IsA(join, MergeJoin))
669 MergeJoin *mj = (MergeJoin *) join;
671 mj->mergeclauses = fix_join_expr(mj->mergeclauses,
677 else if (IsA(join, HashJoin))
679 HashJoin *hj = (HashJoin *) join;
681 hj->hashclauses = fix_join_expr(hj->hashclauses,
693 * set_inner_join_references
694 * Handle join references appearing in an inner indexscan's quals
696 * To handle bitmap-scan plan trees, we have to be able to recurse down
697 * to the bottom BitmapIndexScan nodes; likewise, appendrel indexscans
698 * require recursing through Append nodes. This is split out as a separate
699 * function so that it can recurse.
701 * Note we do *not* apply any rtoffset for non-join Vars; this is because
702 * the quals will be processed again by fix_scan_expr when the set_plan_refs
703 * recursion reaches the inner indexscan, and so we'd have done it twice.
706 set_inner_join_references(Plan *inner_plan, indexed_tlist *outer_itlist)
708 if (IsA(inner_plan, IndexScan))
711 * An index is being used to reduce the number of tuples scanned in
712 * the inner relation. If there are join clauses being used with the
713 * index, we must update their outer-rel var nodes to refer to the
714 * outer side of the join.
716 IndexScan *innerscan = (IndexScan *) inner_plan;
717 List *indexqualorig = innerscan->indexqualorig;
719 /* No work needed if indexqual refers only to its own rel... */
720 if (NumRelids((Node *) indexqualorig) > 1)
722 Index innerrel = innerscan->scan.scanrelid;
724 /* only refs to outer vars get changed in the inner qual */
725 innerscan->indexqualorig = fix_join_expr(indexqualorig,
730 innerscan->indexqual = fix_join_expr(innerscan->indexqual,
737 * We must fix the inner qpqual too, if it has join clauses (this
738 * could happen if special operators are involved: some indexquals
739 * may get rechecked as qpquals).
741 if (NumRelids((Node *) inner_plan->qual) > 1)
742 inner_plan->qual = fix_join_expr(inner_plan->qual,
749 else if (IsA(inner_plan, BitmapIndexScan))
752 * Same, but index is being used within a bitmap plan.
754 BitmapIndexScan *innerscan = (BitmapIndexScan *) inner_plan;
755 List *indexqualorig = innerscan->indexqualorig;
757 /* No work needed if indexqual refers only to its own rel... */
758 if (NumRelids((Node *) indexqualorig) > 1)
760 Index innerrel = innerscan->scan.scanrelid;
762 /* only refs to outer vars get changed in the inner qual */
763 innerscan->indexqualorig = fix_join_expr(indexqualorig,
768 innerscan->indexqual = fix_join_expr(innerscan->indexqual,
773 /* no need to fix inner qpqual */
774 Assert(inner_plan->qual == NIL);
777 else if (IsA(inner_plan, BitmapHeapScan))
780 * The inner side is a bitmap scan plan. Fix the top node, and
781 * recurse to get the lower nodes.
783 * Note: create_bitmap_scan_plan removes clauses from bitmapqualorig
784 * if they are duplicated in qpqual, so must test these independently.
786 BitmapHeapScan *innerscan = (BitmapHeapScan *) inner_plan;
787 Index innerrel = innerscan->scan.scanrelid;
788 List *bitmapqualorig = innerscan->bitmapqualorig;
790 /* only refs to outer vars get changed in the inner qual */
791 if (NumRelids((Node *) bitmapqualorig) > 1)
792 innerscan->bitmapqualorig = fix_join_expr(bitmapqualorig,
799 * We must fix the inner qpqual too, if it has join clauses (this
800 * could happen if special operators are involved: some indexquals may
801 * get rechecked as qpquals).
803 if (NumRelids((Node *) inner_plan->qual) > 1)
804 inner_plan->qual = fix_join_expr(inner_plan->qual,
811 set_inner_join_references(inner_plan->lefttree, outer_itlist);
813 else if (IsA(inner_plan, BitmapAnd))
815 /* All we need do here is recurse */
816 BitmapAnd *innerscan = (BitmapAnd *) inner_plan;
819 foreach(l, innerscan->bitmapplans)
821 set_inner_join_references((Plan *) lfirst(l), outer_itlist);
824 else if (IsA(inner_plan, BitmapOr))
826 /* All we need do here is recurse */
827 BitmapOr *innerscan = (BitmapOr *) inner_plan;
830 foreach(l, innerscan->bitmapplans)
832 set_inner_join_references((Plan *) lfirst(l), outer_itlist);
835 else if (IsA(inner_plan, TidScan))
837 TidScan *innerscan = (TidScan *) inner_plan;
838 Index innerrel = innerscan->scan.scanrelid;
840 innerscan->tidquals = fix_join_expr(innerscan->tidquals,
846 else if (IsA(inner_plan, Append))
849 * The inner side is an append plan. Recurse to see if it contains
850 * indexscans that need to be fixed.
852 Append *appendplan = (Append *) inner_plan;
855 foreach(l, appendplan->appendplans)
857 set_inner_join_references((Plan *) lfirst(l), outer_itlist);
860 else if (IsA(inner_plan, Result))
862 /* Recurse through a gating Result node (similar to Append case) */
863 Result *result = (Result *) inner_plan;
865 if (result->plan.lefttree)
866 set_inner_join_references(result->plan.lefttree, outer_itlist);
871 * set_upper_references
872 * Update the targetlist and quals of an upper-level plan node
873 * to refer to the tuples returned by its lefttree subplan.
874 * Also perform opcode lookup for these expressions.
876 * This is used for single-input plan types like Agg, Group, Result.
878 * In most cases, we have to match up individual Vars in the tlist and
879 * qual expressions with elements of the subplan's tlist (which was
880 * generated by flatten_tlist() from these selfsame expressions, so it
881 * should have all the required variables). There is an important exception,
882 * however: GROUP BY and ORDER BY expressions will have been pushed into the
883 * subplan tlist unflattened. If these values are also needed in the output
884 * then we want to reference the subplan tlist element rather than recomputing
888 set_upper_references(Plan *plan, int rtoffset)
890 Plan *subplan = plan->lefttree;
891 indexed_tlist *subplan_itlist;
892 List *output_targetlist;
895 subplan_itlist = build_tlist_index(subplan->targetlist);
897 output_targetlist = NIL;
898 foreach(l, plan->targetlist)
900 TargetEntry *tle = (TargetEntry *) lfirst(l);
903 newexpr = fix_upper_expr((Node *) tle->expr,
906 tle = flatCopyTargetEntry(tle);
907 tle->expr = (Expr *) newexpr;
908 output_targetlist = lappend(output_targetlist, tle);
910 plan->targetlist = output_targetlist;
912 plan->qual = (List *)
913 fix_upper_expr((Node *) plan->qual,
917 pfree(subplan_itlist);
921 * set_dummy_tlist_references
922 * Replace the targetlist of an upper-level plan node with a simple
923 * list of OUTER references to its child.
925 * This is used for plan types like Sort and Append that don't evaluate
926 * their targetlists. Although the executor doesn't care at all what's in
927 * the tlist, EXPLAIN needs it to be realistic.
929 * Note: we could almost use set_upper_references() here, but it fails for
930 * Append for lack of a lefttree subplan. Single-purpose code is faster
934 set_dummy_tlist_references(Plan *plan, int rtoffset)
936 List *output_targetlist;
939 output_targetlist = NIL;
940 foreach(l, plan->targetlist)
942 TargetEntry *tle = (TargetEntry *) lfirst(l);
943 Var *oldvar = (Var *) tle->expr;
946 newvar = makeVar(OUTER,
948 exprType((Node *) oldvar),
949 exprTypmod((Node *) oldvar),
951 if (IsA(oldvar, Var))
953 newvar->varnoold = oldvar->varno + rtoffset;
954 newvar->varoattno = oldvar->varattno;
958 newvar->varnoold = 0; /* wasn't ever a plain Var */
959 newvar->varoattno = 0;
962 tle = flatCopyTargetEntry(tle);
963 tle->expr = (Expr *) newvar;
964 output_targetlist = lappend(output_targetlist, tle);
966 plan->targetlist = output_targetlist;
968 /* We don't touch plan->qual here */
973 * build_tlist_index --- build an index data structure for a child tlist
975 * In most cases, subplan tlists will be "flat" tlists with only Vars,
976 * so we try to optimize that case by extracting information about Vars
977 * in advance. Matching a parent tlist to a child is still an O(N^2)
978 * operation, but at least with a much smaller constant factor than plain
979 * tlist_member() searches.
981 * The result of this function is an indexed_tlist struct to pass to
982 * search_indexed_tlist_for_var() or search_indexed_tlist_for_non_var().
983 * When done, the indexed_tlist may be freed with a single pfree().
985 static indexed_tlist *
986 build_tlist_index(List *tlist)
988 indexed_tlist *itlist;
992 /* Create data structure with enough slots for all tlist entries */
993 itlist = (indexed_tlist *)
994 palloc(offsetof(indexed_tlist, vars) +
995 list_length(tlist) * sizeof(tlist_vinfo));
997 itlist->tlist = tlist;
998 itlist->has_non_vars = false;
1000 /* Find the Vars and fill in the index array */
1001 vinfo = itlist->vars;
1004 TargetEntry *tle = (TargetEntry *) lfirst(l);
1006 if (tle->expr && IsA(tle->expr, Var))
1008 Var *var = (Var *) tle->expr;
1010 vinfo->varno = var->varno;
1011 vinfo->varattno = var->varattno;
1012 vinfo->resno = tle->resno;
1016 itlist->has_non_vars = true;
1019 itlist->num_vars = (vinfo - itlist->vars);
1025 * build_tlist_index_other_vars --- build a restricted tlist index
1027 * This is like build_tlist_index, but we only index tlist entries that
1028 * are Vars and belong to some rel other than the one specified.
1030 static indexed_tlist *
1031 build_tlist_index_other_vars(List *tlist, Index ignore_rel)
1033 indexed_tlist *itlist;
1037 /* Create data structure with enough slots for all tlist entries */
1038 itlist = (indexed_tlist *)
1039 palloc(offsetof(indexed_tlist, vars) +
1040 list_length(tlist) * sizeof(tlist_vinfo));
1042 itlist->tlist = tlist;
1043 itlist->has_non_vars = false;
1045 /* Find the desired Vars and fill in the index array */
1046 vinfo = itlist->vars;
1049 TargetEntry *tle = (TargetEntry *) lfirst(l);
1051 if (tle->expr && IsA(tle->expr, Var))
1053 Var *var = (Var *) tle->expr;
1055 if (var->varno != ignore_rel)
1057 vinfo->varno = var->varno;
1058 vinfo->varattno = var->varattno;
1059 vinfo->resno = tle->resno;
1065 itlist->num_vars = (vinfo - itlist->vars);
1071 * search_indexed_tlist_for_var --- find a Var in an indexed tlist
1073 * If a match is found, return a copy of the given Var with suitably
1074 * modified varno/varattno (to wit, newvarno and the resno of the TLE entry).
1075 * Also ensure that varnoold is incremented by rtoffset.
1076 * If no match, return NULL.
1079 search_indexed_tlist_for_var(Var *var, indexed_tlist *itlist,
1080 Index newvarno, int rtoffset)
1082 Index varno = var->varno;
1083 AttrNumber varattno = var->varattno;
1087 vinfo = itlist->vars;
1088 i = itlist->num_vars;
1091 if (vinfo->varno == varno && vinfo->varattno == varattno)
1094 Var *newvar = (Var *) copyObject(var);
1096 newvar->varno = newvarno;
1097 newvar->varattno = vinfo->resno;
1098 if (newvar->varnoold > 0)
1099 newvar->varnoold += rtoffset;
1104 return NULL; /* no match */
1108 * search_indexed_tlist_for_non_var --- find a non-Var in an indexed tlist
1110 * If a match is found, return a Var constructed to reference the tlist item.
1111 * If no match, return NULL.
1113 * NOTE: it is a waste of time to call this if !itlist->has_non_vars
1116 search_indexed_tlist_for_non_var(Node *node,
1117 indexed_tlist *itlist, Index newvarno)
1121 tle = tlist_member(node, itlist->tlist);
1124 /* Found a matching subplan output expression */
1127 newvar = makeVar(newvarno,
1129 exprType((Node *) tle->expr),
1130 exprTypmod((Node *) tle->expr),
1132 newvar->varnoold = 0; /* wasn't ever a plain Var */
1133 newvar->varoattno = 0;
1136 return NULL; /* no match */
1141 * Create a new set of targetlist entries or join qual clauses by
1142 * changing the varno/varattno values of variables in the clauses
1143 * to reference target list values from the outer and inner join
1144 * relation target lists. Also perform opcode lookup.
1146 * This is used in two different scenarios: a normal join clause, where
1147 * all the Vars in the clause *must* be replaced by OUTER or INNER references;
1148 * and an indexscan being used on the inner side of a nestloop join.
1149 * In the latter case we want to replace the outer-relation Vars by OUTER
1150 * references, while Vars of the inner relation should be adjusted by rtoffset.
1151 * (We also implement RETURNING clause fixup using this second scenario.)
1153 * For a normal join, acceptable_rel should be zero so that any failure to
1154 * match a Var will be reported as an error. For the indexscan case,
1155 * pass inner_itlist = NULL and acceptable_rel = the (not-offseted-yet) ID
1156 * of the inner relation.
1158 * 'clauses' is the targetlist or list of join clauses
1159 * 'outer_itlist' is the indexed target list of the outer join relation
1160 * 'inner_itlist' is the indexed target list of the inner join relation,
1162 * 'acceptable_rel' is either zero or the rangetable index of a relation
1163 * whose Vars may appear in the clause without provoking an error.
1164 * 'rtoffset' is what to add to varno for Vars of acceptable_rel.
1166 * Returns the new expression tree. The original clause structure is
1170 fix_join_expr(List *clauses,
1171 indexed_tlist *outer_itlist,
1172 indexed_tlist *inner_itlist,
1173 Index acceptable_rel,
1176 fix_join_expr_context context;
1178 context.outer_itlist = outer_itlist;
1179 context.inner_itlist = inner_itlist;
1180 context.acceptable_rel = acceptable_rel;
1181 context.rtoffset = rtoffset;
1182 return (List *) fix_join_expr_mutator((Node *) clauses, &context);
1186 fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
1194 Var *var = (Var *) node;
1196 /* First look for the var in the input tlists */
1197 newvar = search_indexed_tlist_for_var(var,
1198 context->outer_itlist,
1202 return (Node *) newvar;
1203 if (context->inner_itlist)
1205 newvar = search_indexed_tlist_for_var(var,
1206 context->inner_itlist,
1210 return (Node *) newvar;
1213 /* If it's for acceptable_rel, adjust and return it */
1214 if (var->varno == context->acceptable_rel)
1216 var = (Var *) copyObject(var);
1217 var->varno += context->rtoffset;
1218 var->varnoold += context->rtoffset;
1219 return (Node *) var;
1222 /* No referent found for Var */
1223 elog(ERROR, "variable not found in subplan target lists");
1225 /* Try matching more complex expressions too, if tlists have any */
1226 if (context->outer_itlist->has_non_vars)
1228 newvar = search_indexed_tlist_for_non_var(node,
1229 context->outer_itlist,
1232 return (Node *) newvar;
1234 if (context->inner_itlist && context->inner_itlist->has_non_vars)
1236 newvar = search_indexed_tlist_for_non_var(node,
1237 context->inner_itlist,
1240 return (Node *) newvar;
1243 * Since we update opcode info in-place, this part could possibly
1244 * scribble on the planner's input data structures, but it's OK.
1246 if (IsA(node, OpExpr))
1247 set_opfuncid((OpExpr *) node);
1248 else if (IsA(node, DistinctExpr))
1249 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1250 else if (IsA(node, NullIfExpr))
1251 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1252 else if (IsA(node, ScalarArrayOpExpr))
1253 set_sa_opfuncid((ScalarArrayOpExpr *) node);
1254 return expression_tree_mutator(node,
1255 fix_join_expr_mutator,
1261 * Modifies an expression tree so that all Var nodes reference outputs
1262 * of a subplan. Also performs opcode lookup.
1264 * This is used to fix up target and qual expressions of non-join upper-level
1267 * An error is raised if no matching var can be found in the subplan tlist
1268 * --- so this routine should only be applied to nodes whose subplans'
1269 * targetlists were generated via flatten_tlist() or some such method.
1271 * If itlist->has_non_vars is true, then we try to match whole subexpressions
1272 * against elements of the subplan tlist, so that we can avoid recomputing
1273 * expressions that were already computed by the subplan. (This is relatively
1274 * expensive, so we don't want to try it in the common case where the
1275 * subplan tlist is just a flattened list of Vars.)
1277 * 'node': the tree to be fixed (a target item or qual)
1278 * 'subplan_itlist': indexed target list for subplan
1279 * 'rtoffset': how much to increment varnoold by
1281 * The resulting tree is a copy of the original in which all Var nodes have
1282 * varno = OUTER, varattno = resno of corresponding subplan target.
1283 * The original tree is not modified.
1286 fix_upper_expr(Node *node,
1287 indexed_tlist *subplan_itlist,
1290 fix_upper_expr_context context;
1292 context.subplan_itlist = subplan_itlist;
1293 context.rtoffset = rtoffset;
1294 return fix_upper_expr_mutator(node, &context);
1298 fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context)
1306 Var *var = (Var *) node;
1308 newvar = search_indexed_tlist_for_var(var,
1309 context->subplan_itlist,
1313 elog(ERROR, "variable not found in subplan target list");
1314 return (Node *) newvar;
1316 /* Try matching more complex expressions too, if tlist has any */
1317 if (context->subplan_itlist->has_non_vars)
1319 newvar = search_indexed_tlist_for_non_var(node,
1320 context->subplan_itlist,
1323 return (Node *) newvar;
1326 * Since we update opcode info in-place, this part could possibly
1327 * scribble on the planner's input data structures, but it's OK.
1329 if (IsA(node, OpExpr))
1330 set_opfuncid((OpExpr *) node);
1331 else if (IsA(node, DistinctExpr))
1332 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1333 else if (IsA(node, NullIfExpr))
1334 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1335 else if (IsA(node, ScalarArrayOpExpr))
1336 set_sa_opfuncid((ScalarArrayOpExpr *) node);
1337 return expression_tree_mutator(node,
1338 fix_upper_expr_mutator,
1343 * set_returning_clause_references
1344 * Perform setrefs.c's work on a RETURNING targetlist
1346 * If the query involves more than just the result table, we have to
1347 * adjust any Vars that refer to other tables to reference junk tlist
1348 * entries in the top plan's targetlist. Vars referencing the result
1349 * table should be left alone, however (the executor will evaluate them
1350 * using the actual heap tuple, after firing triggers if any). In the
1351 * adjusted RETURNING list, result-table Vars will still have their
1352 * original varno, but Vars for other rels will have varno OUTER.
1354 * We also must perform opcode lookup.
1356 * 'rlist': the RETURNING targetlist to be fixed
1357 * 'topplan': the top Plan node for the query (not yet passed through
1358 * set_plan_references)
1359 * 'resultRelation': RT index of the associated result relation
1361 * Note: we assume that result relations will have rtoffset zero, that is,
1362 * they are not coming from a subplan.
1365 set_returning_clause_references(List *rlist,
1367 Index resultRelation)
1369 indexed_tlist *itlist;
1372 * We can perform the desired Var fixup by abusing the fix_join_expr
1373 * machinery that normally handles inner indexscan fixup. We search the
1374 * top plan's targetlist for Vars of non-result relations, and use
1375 * fix_join_expr to convert RETURNING Vars into references to those
1376 * tlist entries, while leaving result-rel Vars as-is.
1378 itlist = build_tlist_index_other_vars(topplan->targetlist, resultRelation);
1380 rlist = fix_join_expr(rlist,
1391 /*****************************************************************************
1392 * OPERATOR REGPROC LOOKUP
1393 *****************************************************************************/
1397 * Calculate opfuncid field from opno for each OpExpr node in given tree.
1398 * The given tree can be anything expression_tree_walker handles.
1400 * The argument is modified in-place. (This is OK since we'd want the
1401 * same change for any node, even if it gets visited more than once due to
1402 * shared structure.)
1405 fix_opfuncids(Node *node)
1407 /* This tree walk requires no special setup, so away we go... */
1408 fix_opfuncids_walker(node, NULL);
1412 fix_opfuncids_walker(Node *node, void *context)
1416 if (IsA(node, OpExpr))
1417 set_opfuncid((OpExpr *) node);
1418 else if (IsA(node, DistinctExpr))
1419 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1420 else if (IsA(node, NullIfExpr))
1421 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1422 else if (IsA(node, ScalarArrayOpExpr))
1423 set_sa_opfuncid((ScalarArrayOpExpr *) node);
1424 return expression_tree_walker(node, fix_opfuncids_walker, context);
1429 * Set the opfuncid (procedure OID) in an OpExpr node,
1430 * if it hasn't been set already.
1432 * Because of struct equivalence, this can also be used for
1433 * DistinctExpr and NullIfExpr nodes.
1436 set_opfuncid(OpExpr *opexpr)
1438 if (opexpr->opfuncid == InvalidOid)
1439 opexpr->opfuncid = get_opcode(opexpr->opno);
1444 * As above, for ScalarArrayOpExpr nodes.
1447 set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
1449 if (opexpr->opfuncid == InvalidOid)
1450 opexpr->opfuncid = get_opcode(opexpr->opno);