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-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.137 2007/10/11 18:05:27 tgl Exp $
14 *-------------------------------------------------------------------------
18 #include "catalog/pg_type.h"
19 #include "nodes/makefuncs.h"
20 #include "optimizer/clauses.h"
21 #include "optimizer/planmain.h"
22 #include "optimizer/tlist.h"
23 #include "parser/parse_expr.h"
24 #include "parser/parsetree.h"
25 #include "utils/lsyscache.h"
30 Index varno; /* RT index of Var */
31 AttrNumber varattno; /* attr number of Var */
32 AttrNumber resno; /* TLE position of Var */
37 List *tlist; /* underlying target list */
38 int num_vars; /* number of plain Var tlist entries */
39 bool has_non_vars; /* are there non-plain-Var entries? */
40 /* array of num_vars entries: */
41 tlist_vinfo vars[1]; /* VARIABLE LENGTH ARRAY */
42 } indexed_tlist; /* VARIABLE LENGTH STRUCT */
48 } fix_scan_expr_context;
53 indexed_tlist *outer_itlist;
54 indexed_tlist *inner_itlist;
57 } fix_join_expr_context;
62 indexed_tlist *subplan_itlist;
64 } fix_upper_expr_context;
66 #define fix_scan_list(glob, lst, rtoffset) \
67 ((List *) fix_scan_expr(glob, (Node *) (lst), rtoffset))
69 static Plan *set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset);
70 static Plan *set_subqueryscan_references(PlannerGlobal *glob,
73 static bool trivial_subqueryscan(SubqueryScan *plan);
74 static Node *fix_scan_expr(PlannerGlobal *glob, Node *node, int rtoffset);
75 static Node *fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context);
76 static void set_join_references(PlannerGlobal *glob, Join *join, int rtoffset);
77 static void set_inner_join_references(PlannerGlobal *glob, Plan *inner_plan,
78 indexed_tlist *outer_itlist);
79 static void set_upper_references(PlannerGlobal *glob, Plan *plan, int rtoffset);
80 static void set_dummy_tlist_references(Plan *plan, int rtoffset);
81 static indexed_tlist *build_tlist_index(List *tlist);
82 static Var *search_indexed_tlist_for_var(Var *var,
83 indexed_tlist *itlist,
86 static Var *search_indexed_tlist_for_non_var(Node *node,
87 indexed_tlist *itlist,
89 static List *fix_join_expr(PlannerGlobal *glob,
91 indexed_tlist *outer_itlist,
92 indexed_tlist *inner_itlist,
93 Index acceptable_rel, int rtoffset);
94 static Node *fix_join_expr_mutator(Node *node,
95 fix_join_expr_context *context);
96 static Node *fix_upper_expr(PlannerGlobal *glob,
98 indexed_tlist *subplan_itlist,
100 static Node *fix_upper_expr_mutator(Node *node,
101 fix_upper_expr_context *context);
102 static bool fix_opfuncids_walker(Node *node, void *context);
105 /*****************************************************************************
109 *****************************************************************************/
112 * set_plan_references
114 * This is the final processing pass of the planner/optimizer. The plan
115 * tree is complete; we just have to adjust some representational details
116 * for the convenience of the executor:
118 * 1. We flatten the various subquery rangetables into a single list, and
119 * zero out RangeTblEntry fields that are not useful to the executor.
121 * 2. We adjust Vars in scan nodes to be consistent with the flat rangetable.
123 * 3. We adjust Vars in upper plan nodes to refer to the outputs of their
126 * 4. We compute regproc OIDs for operators (ie, we look up the function
127 * that implements each op).
129 * 5. We create a list of OIDs of relations that the plan depends on.
130 * This will be used by plancache.c to drive invalidation of cached plans.
131 * (Someday we might want to generalize this to include other types of
132 * objects, but for now tracking relations seems to solve most problems.)
134 * We also perform one final optimization step, which is to delete
135 * SubqueryScan plan nodes that aren't doing anything useful (ie, have
136 * no qual and a no-op targetlist). The reason for doing this last is that
137 * it can't readily be done before set_plan_references, because it would
138 * break set_upper_references: the Vars in the subquery's top tlist
139 * wouldn't match up with the Vars in the outer plan tree. The SubqueryScan
140 * serves a necessary function as a buffer between outer query and subquery
141 * variable numbering ... but after we've flattened the rangetable this is
142 * no longer a problem, since then there's only one rtindex namespace.
144 * set_plan_references recursively traverses the whole plan tree.
147 * glob: global data for planner run
148 * plan: the topmost node of the plan
149 * rtable: the rangetable for the current subquery
151 * The return value is normally the same Plan node passed in, but can be
152 * different when the passed-in Plan is a SubqueryScan we decide isn't needed.
154 * The flattened rangetable entries are appended to glob->finalrtable, and
155 * the list of relation OIDs is appended to glob->relationOids.
157 * Notice that we modify Plan nodes in-place, but use expression_tree_mutator
158 * to process targetlist and qual expressions. We can assume that the Plan
159 * nodes were just built by the planner and are not multiply referenced, but
160 * it's not so safe to assume that for expression tree nodes.
163 set_plan_references(PlannerGlobal *glob, Plan *plan, List *rtable)
165 int rtoffset = list_length(glob->finalrtable);
169 * In the flat rangetable, we zero out substructure pointers that are
170 * not needed by the executor; this reduces the storage space and
171 * copying cost for cached plans. We keep only the alias and eref
172 * Alias fields, which are needed by EXPLAIN.
176 RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
177 RangeTblEntry *newrte;
179 /* flat copy to duplicate all the scalar fields */
180 newrte = (RangeTblEntry *) palloc(sizeof(RangeTblEntry));
181 memcpy(newrte, rte, sizeof(RangeTblEntry));
183 /* zap unneeded sub-structure */
184 newrte->subquery = NULL;
185 newrte->funcexpr = NULL;
186 newrte->funccoltypes = NIL;
187 newrte->funccoltypmods = NIL;
188 newrte->values_lists = NIL;
189 newrte->joinaliasvars = NIL;
191 glob->finalrtable = lappend(glob->finalrtable, newrte);
194 * If it's a plain relation RTE, add the table to relationOids.
196 * We do this even though the RTE might be unreferenced in the
197 * plan tree; this would correspond to cases such as views that
198 * were expanded, child tables that were eliminated by constraint
199 * exclusion, etc. Schema invalidation on such a rel must still
200 * force rebuilding of the plan.
202 * Note we don't bother to avoid duplicate list entries. We could,
203 * but it would probably cost more cycles than it would save.
205 if (newrte->rtekind == RTE_RELATION)
206 glob->relationOids = lappend_oid(glob->relationOids,
210 /* Now fix the Plan tree */
211 return set_plan_refs(glob, plan, rtoffset);
215 * set_plan_refs: recurse through the Plan nodes of a single subquery level
218 set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
226 * Plan-type-specific fixes
228 switch (nodeTag(plan))
232 SeqScan *splan = (SeqScan *) plan;
234 splan->scanrelid += rtoffset;
235 splan->plan.targetlist =
236 fix_scan_list(glob, splan->plan.targetlist, rtoffset);
238 fix_scan_list(glob, splan->plan.qual, rtoffset);
243 IndexScan *splan = (IndexScan *) plan;
245 splan->scan.scanrelid += rtoffset;
246 splan->scan.plan.targetlist =
247 fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
248 splan->scan.plan.qual =
249 fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
251 fix_scan_list(glob, splan->indexqual, rtoffset);
252 splan->indexqualorig =
253 fix_scan_list(glob, splan->indexqualorig, rtoffset);
256 case T_BitmapIndexScan:
258 BitmapIndexScan *splan = (BitmapIndexScan *) plan;
260 splan->scan.scanrelid += rtoffset;
261 /* no need to fix targetlist and qual */
262 Assert(splan->scan.plan.targetlist == NIL);
263 Assert(splan->scan.plan.qual == NIL);
265 fix_scan_list(glob, splan->indexqual, rtoffset);
266 splan->indexqualorig =
267 fix_scan_list(glob, splan->indexqualorig, rtoffset);
270 case T_BitmapHeapScan:
272 BitmapHeapScan *splan = (BitmapHeapScan *) plan;
274 splan->scan.scanrelid += rtoffset;
275 splan->scan.plan.targetlist =
276 fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
277 splan->scan.plan.qual =
278 fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
279 splan->bitmapqualorig =
280 fix_scan_list(glob, splan->bitmapqualorig, rtoffset);
285 TidScan *splan = (TidScan *) plan;
287 splan->scan.scanrelid += rtoffset;
288 splan->scan.plan.targetlist =
289 fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
290 splan->scan.plan.qual =
291 fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
293 fix_scan_list(glob, splan->tidquals, rtoffset);
297 /* Needs special treatment, see comments below */
298 return set_subqueryscan_references(glob,
299 (SubqueryScan *) plan,
303 FunctionScan *splan = (FunctionScan *) plan;
305 splan->scan.scanrelid += rtoffset;
306 splan->scan.plan.targetlist =
307 fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
308 splan->scan.plan.qual =
309 fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
311 fix_scan_expr(glob, splan->funcexpr, rtoffset);
316 ValuesScan *splan = (ValuesScan *) plan;
318 splan->scan.scanrelid += rtoffset;
319 splan->scan.plan.targetlist =
320 fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
321 splan->scan.plan.qual =
322 fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
323 splan->values_lists =
324 fix_scan_list(glob, splan->values_lists, rtoffset);
331 set_join_references(glob, (Join *) plan, rtoffset);
341 * These plan types don't actually bother to evaluate their
342 * targetlists, because they just return their unmodified input
343 * tuples. Even though the targetlist won't be used by the
344 * executor, we fix it up for possible use by EXPLAIN (not to
345 * mention ease of debugging --- wrong varnos are very confusing).
347 set_dummy_tlist_references(plan, rtoffset);
349 * Since these plan types don't check quals either, we should not
350 * find any qual expression attached to them.
352 Assert(plan->qual == NIL);
356 Limit *splan = (Limit *) plan;
359 * Like the plan types above, Limit doesn't evaluate its tlist
360 * or quals. It does have live expressions for limit/offset,
361 * however; and those cannot contain subplan variable refs,
362 * so fix_scan_expr works for them.
364 set_dummy_tlist_references(plan, rtoffset);
365 Assert(splan->plan.qual == NIL);
368 fix_scan_expr(glob, splan->limitOffset, rtoffset);
370 fix_scan_expr(glob, splan->limitCount, rtoffset);
375 set_upper_references(glob, plan, rtoffset);
379 Result *splan = (Result *) plan;
382 * Result may or may not have a subplan; if not, it's more
383 * like a scan node than an upper node.
385 if (splan->plan.lefttree != NULL)
386 set_upper_references(glob, plan, rtoffset);
389 splan->plan.targetlist =
390 fix_scan_list(glob, splan->plan.targetlist, rtoffset);
392 fix_scan_list(glob, splan->plan.qual, rtoffset);
394 /* resconstantqual can't contain any subplan variable refs */
395 splan->resconstantqual =
396 fix_scan_expr(glob, splan->resconstantqual, rtoffset);
401 Append *splan = (Append *) plan;
404 * Append, like Sort et al, doesn't actually evaluate its
405 * targetlist or check quals.
407 set_dummy_tlist_references(plan, rtoffset);
408 Assert(splan->plan.qual == NIL);
409 foreach(l, splan->appendplans)
411 lfirst(l) = set_plan_refs(glob,
419 BitmapAnd *splan = (BitmapAnd *) plan;
421 /* BitmapAnd works like Append, but has no tlist */
422 Assert(splan->plan.targetlist == NIL);
423 Assert(splan->plan.qual == NIL);
424 foreach(l, splan->bitmapplans)
426 lfirst(l) = set_plan_refs(glob,
434 BitmapOr *splan = (BitmapOr *) plan;
436 /* BitmapOr works like Append, but has no tlist */
437 Assert(splan->plan.targetlist == NIL);
438 Assert(splan->plan.qual == NIL);
439 foreach(l, splan->bitmapplans)
441 lfirst(l) = set_plan_refs(glob,
448 elog(ERROR, "unrecognized node type: %d",
449 (int) nodeTag(plan));
454 * Now recurse into child plans, if any
456 * NOTE: it is essential that we recurse into child plans AFTER we set
457 * subplan references in this plan's tlist and quals. If we did the
458 * reference-adjustments bottom-up, then we would fail to match this
459 * plan's var nodes against the already-modified nodes of the children.
461 plan->lefttree = set_plan_refs(glob, plan->lefttree, rtoffset);
462 plan->righttree = set_plan_refs(glob, plan->righttree, rtoffset);
468 * set_subqueryscan_references
469 * Do set_plan_references processing on a SubqueryScan
471 * We try to strip out the SubqueryScan entirely; if we can't, we have
472 * to do the normal processing on it.
475 set_subqueryscan_references(PlannerGlobal *glob,
481 /* First, recursively process the subplan */
482 plan->subplan = set_plan_references(glob, plan->subplan, plan->subrtable);
484 /* subrtable is no longer needed in the plan tree */
485 plan->subrtable = NIL;
487 if (trivial_subqueryscan(plan))
490 * We can omit the SubqueryScan node and just pull up the subplan.
495 result = plan->subplan;
497 /* We have to be sure we don't lose any initplans */
498 result->initPlan = list_concat(plan->scan.plan.initPlan,
502 * We also have to transfer the SubqueryScan's result-column names
503 * into the subplan, else columns sent to client will be improperly
504 * labeled if this is the topmost plan level. Copy the "source
505 * column" information too.
507 forboth(lp, plan->scan.plan.targetlist, lc, result->targetlist)
509 TargetEntry *ptle = (TargetEntry *) lfirst(lp);
510 TargetEntry *ctle = (TargetEntry *) lfirst(lc);
512 ctle->resname = ptle->resname;
513 ctle->resorigtbl = ptle->resorigtbl;
514 ctle->resorigcol = ptle->resorigcol;
520 * Keep the SubqueryScan node. We have to do the processing that
521 * set_plan_references would otherwise have done on it. Notice we do
522 * not do set_upper_references() here, because a SubqueryScan will
523 * always have been created with correct references to its subplan's
524 * outputs to begin with.
526 plan->scan.scanrelid += rtoffset;
527 plan->scan.plan.targetlist =
528 fix_scan_list(glob, plan->scan.plan.targetlist, rtoffset);
529 plan->scan.plan.qual =
530 fix_scan_list(glob, plan->scan.plan.qual, rtoffset);
532 result = (Plan *) plan;
539 * trivial_subqueryscan
540 * Detect whether a SubqueryScan can be deleted from the plan tree.
542 * We can delete it if it has no qual to check and the targetlist just
543 * regurgitates the output of the child plan.
546 trivial_subqueryscan(SubqueryScan *plan)
552 if (plan->scan.plan.qual != NIL)
555 if (list_length(plan->scan.plan.targetlist) !=
556 list_length(plan->subplan->targetlist))
557 return false; /* tlists not same length */
560 forboth(lp, plan->scan.plan.targetlist, lc, plan->subplan->targetlist)
562 TargetEntry *ptle = (TargetEntry *) lfirst(lp);
563 TargetEntry *ctle = (TargetEntry *) lfirst(lc);
565 if (ptle->resjunk != ctle->resjunk)
566 return false; /* tlist doesn't match junk status */
569 * We accept either a Var referencing the corresponding element of the
570 * subplan tlist, or a Const equaling the subplan element. See
571 * generate_setop_tlist() for motivation.
573 if (ptle->expr && IsA(ptle->expr, Var))
575 Var *var = (Var *) ptle->expr;
577 Assert(var->varno == plan->scan.scanrelid);
578 Assert(var->varlevelsup == 0);
579 if (var->varattno != attrno)
580 return false; /* out of order */
582 else if (ptle->expr && IsA(ptle->expr, Const))
584 if (!equal(ptle->expr, ctle->expr))
600 * fix_scan_expr and friends do this enough times that it's worth having
601 * a bespoke routine instead of using the generic copyObject() function.
606 Var *newvar = (Var *) palloc(sizeof(Var));
614 * Do set_plan_references processing on a scan-level expression
616 * This consists of incrementing all Vars' varnos by rtoffset,
617 * looking up operator opcode info for OpExpr and related nodes,
618 * and adding OIDs from regclass Const nodes into glob->relationOids.
621 fix_scan_expr(PlannerGlobal *glob, Node *node, int rtoffset)
623 fix_scan_expr_context context;
626 context.rtoffset = rtoffset;
627 return fix_scan_expr_mutator(node, &context);
631 fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context)
637 Var *var = copyVar((Var *) node);
639 Assert(var->varlevelsup == 0);
641 * We should not see any Vars marked INNER, but in a nestloop inner
642 * scan there could be OUTER Vars. Leave them alone.
644 Assert(var->varno != INNER);
645 if (var->varno > 0 && var->varno != OUTER)
646 var->varno += context->rtoffset;
647 if (var->varnoold > 0)
648 var->varnoold += context->rtoffset;
651 if (IsA(node, CurrentOfExpr))
653 CurrentOfExpr *cexpr = (CurrentOfExpr *) copyObject(node);
655 Assert(cexpr->cvarno != INNER);
656 Assert(cexpr->cvarno != OUTER);
657 cexpr->cvarno += context->rtoffset;
658 return (Node *) cexpr;
661 * Since we update opcode info in-place, this part could possibly
662 * scribble on the planner's input data structures, but it's OK.
664 if (IsA(node, OpExpr))
665 set_opfuncid((OpExpr *) node);
666 else if (IsA(node, DistinctExpr))
667 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
668 else if (IsA(node, NullIfExpr))
669 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
670 else if (IsA(node, ScalarArrayOpExpr))
671 set_sa_opfuncid((ScalarArrayOpExpr *) node);
672 else if (IsA(node, Const))
674 Const *con = (Const *) node;
676 /* Check for regclass reference */
677 if (con->consttype == REGCLASSOID && !con->constisnull)
678 context->glob->relationOids =
679 lappend_oid(context->glob->relationOids,
680 DatumGetObjectId(con->constvalue));
681 /* Fall through to let expression_tree_mutator copy it */
683 return expression_tree_mutator(node, fix_scan_expr_mutator,
688 * set_join_references
689 * Modify the target list and quals of a join node to reference its
690 * subplans, by setting the varnos to OUTER or INNER and setting attno
691 * values to the result domain number of either the corresponding outer
692 * or inner join tuple item. Also perform opcode lookup for these
693 * expressions. and add regclass OIDs to glob->relationOids.
695 * In the case of a nestloop with inner indexscan, we will also need to
696 * apply the same transformation to any outer vars appearing in the
697 * quals of the child indexscan. set_inner_join_references does that.
700 set_join_references(PlannerGlobal *glob, Join *join, int rtoffset)
702 Plan *outer_plan = join->plan.lefttree;
703 Plan *inner_plan = join->plan.righttree;
704 indexed_tlist *outer_itlist;
705 indexed_tlist *inner_itlist;
707 outer_itlist = build_tlist_index(outer_plan->targetlist);
708 inner_itlist = build_tlist_index(inner_plan->targetlist);
710 /* All join plans have tlist, qual, and joinqual */
711 join->plan.targetlist = fix_join_expr(glob,
712 join->plan.targetlist,
717 join->plan.qual = fix_join_expr(glob,
723 join->joinqual = fix_join_expr(glob,
730 /* Now do join-type-specific stuff */
731 if (IsA(join, NestLoop))
733 /* This processing is split out to handle possible recursion */
734 set_inner_join_references(glob, inner_plan, outer_itlist);
736 else if (IsA(join, MergeJoin))
738 MergeJoin *mj = (MergeJoin *) join;
740 mj->mergeclauses = fix_join_expr(glob,
747 else if (IsA(join, HashJoin))
749 HashJoin *hj = (HashJoin *) join;
751 hj->hashclauses = fix_join_expr(glob,
764 * set_inner_join_references
765 * Handle join references appearing in an inner indexscan's quals
767 * To handle bitmap-scan plan trees, we have to be able to recurse down
768 * to the bottom BitmapIndexScan nodes; likewise, appendrel indexscans
769 * require recursing through Append nodes. This is split out as a separate
770 * function so that it can recurse.
772 * Note we do *not* apply any rtoffset for non-join Vars; this is because
773 * the quals will be processed again by fix_scan_expr when the set_plan_refs
774 * recursion reaches the inner indexscan, and so we'd have done it twice.
777 set_inner_join_references(PlannerGlobal *glob, Plan *inner_plan,
778 indexed_tlist *outer_itlist)
780 if (IsA(inner_plan, IndexScan))
783 * An index is being used to reduce the number of tuples scanned in
784 * the inner relation. If there are join clauses being used with the
785 * index, we must update their outer-rel var nodes to refer to the
786 * outer side of the join.
788 IndexScan *innerscan = (IndexScan *) inner_plan;
789 List *indexqualorig = innerscan->indexqualorig;
791 /* No work needed if indexqual refers only to its own rel... */
792 if (NumRelids((Node *) indexqualorig) > 1)
794 Index innerrel = innerscan->scan.scanrelid;
796 /* only refs to outer vars get changed in the inner qual */
797 innerscan->indexqualorig = fix_join_expr(glob,
803 innerscan->indexqual = fix_join_expr(glob,
804 innerscan->indexqual,
811 * We must fix the inner qpqual too, if it has join clauses (this
812 * could happen if special operators are involved: some indexquals
813 * may get rechecked as qpquals).
815 if (NumRelids((Node *) inner_plan->qual) > 1)
816 inner_plan->qual = fix_join_expr(glob,
824 else if (IsA(inner_plan, BitmapIndexScan))
827 * Same, but index is being used within a bitmap plan.
829 BitmapIndexScan *innerscan = (BitmapIndexScan *) inner_plan;
830 List *indexqualorig = innerscan->indexqualorig;
832 /* No work needed if indexqual refers only to its own rel... */
833 if (NumRelids((Node *) indexqualorig) > 1)
835 Index innerrel = innerscan->scan.scanrelid;
837 /* only refs to outer vars get changed in the inner qual */
838 innerscan->indexqualorig = fix_join_expr(glob,
844 innerscan->indexqual = fix_join_expr(glob,
845 innerscan->indexqual,
850 /* no need to fix inner qpqual */
851 Assert(inner_plan->qual == NIL);
854 else if (IsA(inner_plan, BitmapHeapScan))
857 * The inner side is a bitmap scan plan. Fix the top node, and
858 * recurse to get the lower nodes.
860 * Note: create_bitmap_scan_plan removes clauses from bitmapqualorig
861 * if they are duplicated in qpqual, so must test these independently.
863 BitmapHeapScan *innerscan = (BitmapHeapScan *) inner_plan;
864 Index innerrel = innerscan->scan.scanrelid;
865 List *bitmapqualorig = innerscan->bitmapqualorig;
867 /* only refs to outer vars get changed in the inner qual */
868 if (NumRelids((Node *) bitmapqualorig) > 1)
869 innerscan->bitmapqualorig = fix_join_expr(glob,
877 * We must fix the inner qpqual too, if it has join clauses (this
878 * could happen if special operators are involved: some indexquals may
879 * get rechecked as qpquals).
881 if (NumRelids((Node *) inner_plan->qual) > 1)
882 inner_plan->qual = fix_join_expr(glob,
890 set_inner_join_references(glob, inner_plan->lefttree, outer_itlist);
892 else if (IsA(inner_plan, BitmapAnd))
894 /* All we need do here is recurse */
895 BitmapAnd *innerscan = (BitmapAnd *) inner_plan;
898 foreach(l, innerscan->bitmapplans)
900 set_inner_join_references(glob, (Plan *) lfirst(l), outer_itlist);
903 else if (IsA(inner_plan, BitmapOr))
905 /* All we need do here is recurse */
906 BitmapOr *innerscan = (BitmapOr *) inner_plan;
909 foreach(l, innerscan->bitmapplans)
911 set_inner_join_references(glob, (Plan *) lfirst(l), outer_itlist);
914 else if (IsA(inner_plan, TidScan))
916 TidScan *innerscan = (TidScan *) inner_plan;
917 Index innerrel = innerscan->scan.scanrelid;
919 innerscan->tidquals = fix_join_expr(glob,
926 else if (IsA(inner_plan, Append))
929 * The inner side is an append plan. Recurse to see if it contains
930 * indexscans that need to be fixed.
932 Append *appendplan = (Append *) inner_plan;
935 foreach(l, appendplan->appendplans)
937 set_inner_join_references(glob, (Plan *) lfirst(l), outer_itlist);
940 else if (IsA(inner_plan, Result))
942 /* Recurse through a gating Result node (similar to Append case) */
943 Result *result = (Result *) inner_plan;
945 if (result->plan.lefttree)
946 set_inner_join_references(glob, result->plan.lefttree, outer_itlist);
951 * set_upper_references
952 * Update the targetlist and quals of an upper-level plan node
953 * to refer to the tuples returned by its lefttree subplan.
954 * Also perform opcode lookup for these expressions, and
955 * add regclass OIDs to glob->relationOids.
957 * This is used for single-input plan types like Agg, Group, Result.
959 * In most cases, we have to match up individual Vars in the tlist and
960 * qual expressions with elements of the subplan's tlist (which was
961 * generated by flatten_tlist() from these selfsame expressions, so it
962 * should have all the required variables). There is an important exception,
963 * however: GROUP BY and ORDER BY expressions will have been pushed into the
964 * subplan tlist unflattened. If these values are also needed in the output
965 * then we want to reference the subplan tlist element rather than recomputing
969 set_upper_references(PlannerGlobal *glob, Plan *plan, int rtoffset)
971 Plan *subplan = plan->lefttree;
972 indexed_tlist *subplan_itlist;
973 List *output_targetlist;
976 subplan_itlist = build_tlist_index(subplan->targetlist);
978 output_targetlist = NIL;
979 foreach(l, plan->targetlist)
981 TargetEntry *tle = (TargetEntry *) lfirst(l);
984 newexpr = fix_upper_expr(glob,
988 tle = flatCopyTargetEntry(tle);
989 tle->expr = (Expr *) newexpr;
990 output_targetlist = lappend(output_targetlist, tle);
992 plan->targetlist = output_targetlist;
994 plan->qual = (List *)
1000 pfree(subplan_itlist);
1004 * set_dummy_tlist_references
1005 * Replace the targetlist of an upper-level plan node with a simple
1006 * list of OUTER references to its child.
1008 * This is used for plan types like Sort and Append that don't evaluate
1009 * their targetlists. Although the executor doesn't care at all what's in
1010 * the tlist, EXPLAIN needs it to be realistic.
1012 * Note: we could almost use set_upper_references() here, but it fails for
1013 * Append for lack of a lefttree subplan. Single-purpose code is faster
1017 set_dummy_tlist_references(Plan *plan, int rtoffset)
1019 List *output_targetlist;
1022 output_targetlist = NIL;
1023 foreach(l, plan->targetlist)
1025 TargetEntry *tle = (TargetEntry *) lfirst(l);
1026 Var *oldvar = (Var *) tle->expr;
1029 newvar = makeVar(OUTER,
1031 exprType((Node *) oldvar),
1032 exprTypmod((Node *) oldvar),
1034 if (IsA(oldvar, Var))
1036 newvar->varnoold = oldvar->varno + rtoffset;
1037 newvar->varoattno = oldvar->varattno;
1041 newvar->varnoold = 0; /* wasn't ever a plain Var */
1042 newvar->varoattno = 0;
1045 tle = flatCopyTargetEntry(tle);
1046 tle->expr = (Expr *) newvar;
1047 output_targetlist = lappend(output_targetlist, tle);
1049 plan->targetlist = output_targetlist;
1051 /* We don't touch plan->qual here */
1056 * build_tlist_index --- build an index data structure for a child tlist
1058 * In most cases, subplan tlists will be "flat" tlists with only Vars,
1059 * so we try to optimize that case by extracting information about Vars
1060 * in advance. Matching a parent tlist to a child is still an O(N^2)
1061 * operation, but at least with a much smaller constant factor than plain
1062 * tlist_member() searches.
1064 * The result of this function is an indexed_tlist struct to pass to
1065 * search_indexed_tlist_for_var() or search_indexed_tlist_for_non_var().
1066 * When done, the indexed_tlist may be freed with a single pfree().
1068 static indexed_tlist *
1069 build_tlist_index(List *tlist)
1071 indexed_tlist *itlist;
1075 /* Create data structure with enough slots for all tlist entries */
1076 itlist = (indexed_tlist *)
1077 palloc(offsetof(indexed_tlist, vars) +
1078 list_length(tlist) * sizeof(tlist_vinfo));
1080 itlist->tlist = tlist;
1081 itlist->has_non_vars = false;
1083 /* Find the Vars and fill in the index array */
1084 vinfo = itlist->vars;
1087 TargetEntry *tle = (TargetEntry *) lfirst(l);
1089 if (tle->expr && IsA(tle->expr, Var))
1091 Var *var = (Var *) tle->expr;
1093 vinfo->varno = var->varno;
1094 vinfo->varattno = var->varattno;
1095 vinfo->resno = tle->resno;
1099 itlist->has_non_vars = true;
1102 itlist->num_vars = (vinfo - itlist->vars);
1108 * build_tlist_index_other_vars --- build a restricted tlist index
1110 * This is like build_tlist_index, but we only index tlist entries that
1111 * are Vars and belong to some rel other than the one specified.
1113 static indexed_tlist *
1114 build_tlist_index_other_vars(List *tlist, Index ignore_rel)
1116 indexed_tlist *itlist;
1120 /* Create data structure with enough slots for all tlist entries */
1121 itlist = (indexed_tlist *)
1122 palloc(offsetof(indexed_tlist, vars) +
1123 list_length(tlist) * sizeof(tlist_vinfo));
1125 itlist->tlist = tlist;
1126 itlist->has_non_vars = false;
1128 /* Find the desired Vars and fill in the index array */
1129 vinfo = itlist->vars;
1132 TargetEntry *tle = (TargetEntry *) lfirst(l);
1134 if (tle->expr && IsA(tle->expr, Var))
1136 Var *var = (Var *) tle->expr;
1138 if (var->varno != ignore_rel)
1140 vinfo->varno = var->varno;
1141 vinfo->varattno = var->varattno;
1142 vinfo->resno = tle->resno;
1148 itlist->num_vars = (vinfo - itlist->vars);
1154 * search_indexed_tlist_for_var --- find a Var in an indexed tlist
1156 * If a match is found, return a copy of the given Var with suitably
1157 * modified varno/varattno (to wit, newvarno and the resno of the TLE entry).
1158 * Also ensure that varnoold is incremented by rtoffset.
1159 * If no match, return NULL.
1162 search_indexed_tlist_for_var(Var *var, indexed_tlist *itlist,
1163 Index newvarno, int rtoffset)
1165 Index varno = var->varno;
1166 AttrNumber varattno = var->varattno;
1170 vinfo = itlist->vars;
1171 i = itlist->num_vars;
1174 if (vinfo->varno == varno && vinfo->varattno == varattno)
1177 Var *newvar = copyVar(var);
1179 newvar->varno = newvarno;
1180 newvar->varattno = vinfo->resno;
1181 if (newvar->varnoold > 0)
1182 newvar->varnoold += rtoffset;
1187 return NULL; /* no match */
1191 * search_indexed_tlist_for_non_var --- find a non-Var in an indexed tlist
1193 * If a match is found, return a Var constructed to reference the tlist item.
1194 * If no match, return NULL.
1196 * NOTE: it is a waste of time to call this if !itlist->has_non_vars
1199 search_indexed_tlist_for_non_var(Node *node,
1200 indexed_tlist *itlist, Index newvarno)
1204 tle = tlist_member(node, itlist->tlist);
1207 /* Found a matching subplan output expression */
1210 newvar = makeVar(newvarno,
1212 exprType((Node *) tle->expr),
1213 exprTypmod((Node *) tle->expr),
1215 newvar->varnoold = 0; /* wasn't ever a plain Var */
1216 newvar->varoattno = 0;
1219 return NULL; /* no match */
1224 * Create a new set of targetlist entries or join qual clauses by
1225 * changing the varno/varattno values of variables in the clauses
1226 * to reference target list values from the outer and inner join
1227 * relation target lists. Also perform opcode lookup and add
1228 * regclass OIDs to glob->relationOids.
1230 * This is used in two different scenarios: a normal join clause, where
1231 * all the Vars in the clause *must* be replaced by OUTER or INNER references;
1232 * and an indexscan being used on the inner side of a nestloop join.
1233 * In the latter case we want to replace the outer-relation Vars by OUTER
1234 * references, while Vars of the inner relation should be adjusted by rtoffset.
1235 * (We also implement RETURNING clause fixup using this second scenario.)
1237 * For a normal join, acceptable_rel should be zero so that any failure to
1238 * match a Var will be reported as an error. For the indexscan case,
1239 * pass inner_itlist = NULL and acceptable_rel = the (not-offseted-yet) ID
1240 * of the inner relation.
1242 * 'clauses' is the targetlist or list of join clauses
1243 * 'outer_itlist' is the indexed target list of the outer join relation
1244 * 'inner_itlist' is the indexed target list of the inner join relation,
1246 * 'acceptable_rel' is either zero or the rangetable index of a relation
1247 * whose Vars may appear in the clause without provoking an error.
1248 * 'rtoffset' is what to add to varno for Vars of acceptable_rel.
1250 * Returns the new expression tree. The original clause structure is
1254 fix_join_expr(PlannerGlobal *glob,
1256 indexed_tlist *outer_itlist,
1257 indexed_tlist *inner_itlist,
1258 Index acceptable_rel,
1261 fix_join_expr_context context;
1263 context.glob = glob;
1264 context.outer_itlist = outer_itlist;
1265 context.inner_itlist = inner_itlist;
1266 context.acceptable_rel = acceptable_rel;
1267 context.rtoffset = rtoffset;
1268 return (List *) fix_join_expr_mutator((Node *) clauses, &context);
1272 fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
1280 Var *var = (Var *) node;
1282 /* First look for the var in the input tlists */
1283 newvar = search_indexed_tlist_for_var(var,
1284 context->outer_itlist,
1288 return (Node *) newvar;
1289 if (context->inner_itlist)
1291 newvar = search_indexed_tlist_for_var(var,
1292 context->inner_itlist,
1296 return (Node *) newvar;
1299 /* If it's for acceptable_rel, adjust and return it */
1300 if (var->varno == context->acceptable_rel)
1303 var->varno += context->rtoffset;
1304 var->varnoold += context->rtoffset;
1305 return (Node *) var;
1308 /* No referent found for Var */
1309 elog(ERROR, "variable not found in subplan target lists");
1311 /* Try matching more complex expressions too, if tlists have any */
1312 if (context->outer_itlist->has_non_vars)
1314 newvar = search_indexed_tlist_for_non_var(node,
1315 context->outer_itlist,
1318 return (Node *) newvar;
1320 if (context->inner_itlist && context->inner_itlist->has_non_vars)
1322 newvar = search_indexed_tlist_for_non_var(node,
1323 context->inner_itlist,
1326 return (Node *) newvar;
1329 * Since we update opcode info in-place, this part could possibly
1330 * scribble on the planner's input data structures, but it's OK.
1332 if (IsA(node, OpExpr))
1333 set_opfuncid((OpExpr *) node);
1334 else if (IsA(node, DistinctExpr))
1335 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1336 else if (IsA(node, NullIfExpr))
1337 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1338 else if (IsA(node, ScalarArrayOpExpr))
1339 set_sa_opfuncid((ScalarArrayOpExpr *) node);
1340 else if (IsA(node, Const))
1342 Const *con = (Const *) node;
1344 /* Check for regclass reference */
1345 if (con->consttype == REGCLASSOID && !con->constisnull)
1346 context->glob->relationOids =
1347 lappend_oid(context->glob->relationOids,
1348 DatumGetObjectId(con->constvalue));
1349 /* Fall through to let expression_tree_mutator copy it */
1351 return expression_tree_mutator(node,
1352 fix_join_expr_mutator,
1358 * Modifies an expression tree so that all Var nodes reference outputs
1359 * of a subplan. Also performs opcode lookup, and adds regclass OIDs to
1360 * glob->relationOids.
1362 * This is used to fix up target and qual expressions of non-join upper-level
1365 * An error is raised if no matching var can be found in the subplan tlist
1366 * --- so this routine should only be applied to nodes whose subplans'
1367 * targetlists were generated via flatten_tlist() or some such method.
1369 * If itlist->has_non_vars is true, then we try to match whole subexpressions
1370 * against elements of the subplan tlist, so that we can avoid recomputing
1371 * expressions that were already computed by the subplan. (This is relatively
1372 * expensive, so we don't want to try it in the common case where the
1373 * subplan tlist is just a flattened list of Vars.)
1375 * 'node': the tree to be fixed (a target item or qual)
1376 * 'subplan_itlist': indexed target list for subplan
1377 * 'rtoffset': how much to increment varnoold by
1379 * The resulting tree is a copy of the original in which all Var nodes have
1380 * varno = OUTER, varattno = resno of corresponding subplan target.
1381 * The original tree is not modified.
1384 fix_upper_expr(PlannerGlobal *glob,
1386 indexed_tlist *subplan_itlist,
1389 fix_upper_expr_context context;
1391 context.glob = glob;
1392 context.subplan_itlist = subplan_itlist;
1393 context.rtoffset = rtoffset;
1394 return fix_upper_expr_mutator(node, &context);
1398 fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context)
1406 Var *var = (Var *) node;
1408 newvar = search_indexed_tlist_for_var(var,
1409 context->subplan_itlist,
1413 elog(ERROR, "variable not found in subplan target list");
1414 return (Node *) newvar;
1416 /* Try matching more complex expressions too, if tlist has any */
1417 if (context->subplan_itlist->has_non_vars)
1419 newvar = search_indexed_tlist_for_non_var(node,
1420 context->subplan_itlist,
1423 return (Node *) newvar;
1426 * Since we update opcode info in-place, this part could possibly
1427 * scribble on the planner's input data structures, but it's OK.
1429 if (IsA(node, OpExpr))
1430 set_opfuncid((OpExpr *) node);
1431 else if (IsA(node, DistinctExpr))
1432 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1433 else if (IsA(node, NullIfExpr))
1434 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1435 else if (IsA(node, ScalarArrayOpExpr))
1436 set_sa_opfuncid((ScalarArrayOpExpr *) node);
1437 else if (IsA(node, Const))
1439 Const *con = (Const *) node;
1441 /* Check for regclass reference */
1442 if (con->consttype == REGCLASSOID && !con->constisnull)
1443 context->glob->relationOids =
1444 lappend_oid(context->glob->relationOids,
1445 DatumGetObjectId(con->constvalue));
1446 /* Fall through to let expression_tree_mutator copy it */
1448 return expression_tree_mutator(node,
1449 fix_upper_expr_mutator,
1454 * set_returning_clause_references
1455 * Perform setrefs.c's work on a RETURNING targetlist
1457 * If the query involves more than just the result table, we have to
1458 * adjust any Vars that refer to other tables to reference junk tlist
1459 * entries in the top plan's targetlist. Vars referencing the result
1460 * table should be left alone, however (the executor will evaluate them
1461 * using the actual heap tuple, after firing triggers if any). In the
1462 * adjusted RETURNING list, result-table Vars will still have their
1463 * original varno, but Vars for other rels will have varno OUTER.
1465 * We also must perform opcode lookup and add regclass OIDs to
1466 * glob->relationOids.
1468 * 'rlist': the RETURNING targetlist to be fixed
1469 * 'topplan': the top Plan node for the query (not yet passed through
1470 * set_plan_references)
1471 * 'resultRelation': RT index of the associated result relation
1473 * Note: we assume that result relations will have rtoffset zero, that is,
1474 * they are not coming from a subplan.
1477 set_returning_clause_references(PlannerGlobal *glob,
1480 Index resultRelation)
1482 indexed_tlist *itlist;
1485 * We can perform the desired Var fixup by abusing the fix_join_expr
1486 * machinery that normally handles inner indexscan fixup. We search the
1487 * top plan's targetlist for Vars of non-result relations, and use
1488 * fix_join_expr to convert RETURNING Vars into references to those
1489 * tlist entries, while leaving result-rel Vars as-is.
1491 itlist = build_tlist_index_other_vars(topplan->targetlist, resultRelation);
1493 rlist = fix_join_expr(glob,
1505 /*****************************************************************************
1506 * OPERATOR REGPROC LOOKUP
1507 *****************************************************************************/
1511 * Calculate opfuncid field from opno for each OpExpr node in given tree.
1512 * The given tree can be anything expression_tree_walker handles.
1514 * The argument is modified in-place. (This is OK since we'd want the
1515 * same change for any node, even if it gets visited more than once due to
1516 * shared structure.)
1519 fix_opfuncids(Node *node)
1521 /* This tree walk requires no special setup, so away we go... */
1522 fix_opfuncids_walker(node, NULL);
1526 fix_opfuncids_walker(Node *node, void *context)
1530 if (IsA(node, OpExpr))
1531 set_opfuncid((OpExpr *) node);
1532 else if (IsA(node, DistinctExpr))
1533 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1534 else if (IsA(node, NullIfExpr))
1535 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1536 else if (IsA(node, ScalarArrayOpExpr))
1537 set_sa_opfuncid((ScalarArrayOpExpr *) node);
1538 return expression_tree_walker(node, fix_opfuncids_walker, context);
1543 * Set the opfuncid (procedure OID) in an OpExpr node,
1544 * if it hasn't been set already.
1546 * Because of struct equivalence, this can also be used for
1547 * DistinctExpr and NullIfExpr nodes.
1550 set_opfuncid(OpExpr *opexpr)
1552 if (opexpr->opfuncid == InvalidOid)
1553 opexpr->opfuncid = get_opcode(opexpr->opno);
1558 * As above, for ScalarArrayOpExpr nodes.
1561 set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
1563 if (opexpr->opfuncid == InvalidOid)
1564 opexpr->opfuncid = get_opcode(opexpr->opno);