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-2003, 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.101 2004/05/11 13:15:15 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 "optimizer/var.h"
23 #include "parser/parsetree.h"
24 #include "utils/lsyscache.h"
33 bool tlists_have_non_vars;
34 } join_references_context;
39 List *subplan_targetlist;
40 bool tlist_has_non_vars;
41 } replace_vars_with_subplan_refs_context;
43 static void fix_expr_references(Plan *plan, Node *node);
44 static bool fix_expr_references_walker(Node *node, void *context);
45 static void set_join_references(Join *join, List *rtable);
46 static void set_uppernode_references(Plan *plan, Index subvarno);
47 static bool targetlist_has_non_vars(List *tlist);
48 static List *join_references(List *clauses,
53 bool tlists_have_non_vars);
54 static Node *join_references_mutator(Node *node,
55 join_references_context *context);
56 static Node *replace_vars_with_subplan_refs(Node *node,
58 List *subplan_targetlist,
59 bool tlist_has_non_vars);
60 static Node *replace_vars_with_subplan_refs_mutator(Node *node,
61 replace_vars_with_subplan_refs_context *context);
62 static bool fix_opfuncids_walker(Node *node, void *context);
63 static void set_sa_opfuncid(ScalarArrayOpExpr *opexpr);
66 /*****************************************************************************
70 *****************************************************************************/
74 * This is the final processing pass of the planner/optimizer. The plan
75 * tree is complete; we just have to adjust some representational details
76 * for the convenience of the executor. We update Vars in upper plan nodes
77 * to refer to the outputs of their subplans, and we compute regproc OIDs
78 * for operators (ie, we look up the function that implements each op).
80 * set_plan_references recursively traverses the whole plan tree.
82 * Returns nothing of interest, but modifies internal fields of nodes.
85 set_plan_references(Plan *plan, List *rtable)
93 * Plan-type-specific fixes
95 switch (nodeTag(plan))
98 fix_expr_references(plan, (Node *) plan->targetlist);
99 fix_expr_references(plan, (Node *) plan->qual);
102 fix_expr_references(plan, (Node *) plan->targetlist);
103 fix_expr_references(plan, (Node *) plan->qual);
104 fix_expr_references(plan,
105 (Node *) ((IndexScan *) plan)->indxqual);
106 fix_expr_references(plan,
107 (Node *) ((IndexScan *) plan)->indxqualorig);
110 fix_expr_references(plan, (Node *) plan->targetlist);
111 fix_expr_references(plan, (Node *) plan->qual);
112 fix_expr_references(plan,
113 (Node *) ((TidScan *) plan)->tideval);
120 * We do not do set_uppernode_references() here, because a
121 * SubqueryScan will always have been created with correct
122 * references to its subplan's outputs to begin with.
124 fix_expr_references(plan, (Node *) plan->targetlist);
125 fix_expr_references(plan, (Node *) plan->qual);
127 /* Recurse into subplan too */
128 rte = rt_fetch(((SubqueryScan *) plan)->scan.scanrelid,
130 Assert(rte->rtekind == RTE_SUBQUERY);
131 set_plan_references(((SubqueryScan *) plan)->subplan,
132 rte->subquery->rtable);
139 fix_expr_references(plan, (Node *) plan->targetlist);
140 fix_expr_references(plan, (Node *) plan->qual);
141 rte = rt_fetch(((FunctionScan *) plan)->scan.scanrelid,
143 Assert(rte->rtekind == RTE_FUNCTION);
144 fix_expr_references(plan, rte->funcexpr);
148 set_join_references((Join *) plan, rtable);
149 fix_expr_references(plan, (Node *) plan->targetlist);
150 fix_expr_references(plan, (Node *) plan->qual);
151 fix_expr_references(plan, (Node *) ((Join *) plan)->joinqual);
154 set_join_references((Join *) plan, rtable);
155 fix_expr_references(plan, (Node *) plan->targetlist);
156 fix_expr_references(plan, (Node *) plan->qual);
157 fix_expr_references(plan, (Node *) ((Join *) plan)->joinqual);
158 fix_expr_references(plan,
159 (Node *) ((MergeJoin *) plan)->mergeclauses);
162 set_join_references((Join *) plan, rtable);
163 fix_expr_references(plan, (Node *) plan->targetlist);
164 fix_expr_references(plan, (Node *) plan->qual);
165 fix_expr_references(plan, (Node *) ((Join *) plan)->joinqual);
166 fix_expr_references(plan,
167 (Node *) ((HashJoin *) plan)->hashclauses);
176 * These plan types don't actually bother to evaluate their
177 * targetlists or quals (because they just return their
178 * unmodified input tuples). The optimizer is lazy about
179 * creating really valid targetlists for them. Best to just
180 * leave the targetlist alone. In particular, we do not want
181 * to process subplans for them, since we will likely end up
182 * reprocessing subplans that also appear in lower levels of
188 * Like the plan types above, Limit doesn't evaluate its
189 * tlist or quals. It does have live expressions for
190 * limit/offset, however.
192 fix_expr_references(plan, ((Limit *) plan)->limitOffset);
193 fix_expr_references(plan, ((Limit *) plan)->limitCount);
197 set_uppernode_references(plan, (Index) 0);
198 fix_expr_references(plan, (Node *) plan->targetlist);
199 fix_expr_references(plan, (Node *) plan->qual);
204 * Result may or may not have a subplan; no need to fix up
205 * subplan references if it hasn't got one...
207 * XXX why does Result use a different subvarno from Agg/Group?
209 if (plan->lefttree != NULL)
210 set_uppernode_references(plan, (Index) OUTER);
211 fix_expr_references(plan, (Node *) plan->targetlist);
212 fix_expr_references(plan, (Node *) plan->qual);
213 fix_expr_references(plan, ((Result *) plan)->resconstantqual);
218 * Append, like Sort et al, doesn't actually evaluate its
219 * targetlist or quals, and we haven't bothered to give it its
220 * own tlist copy. So, don't fix targetlist/qual. But do
221 * recurse into child plans.
223 foreach(pl, ((Append *) plan)->appendplans)
224 set_plan_references((Plan *) lfirst(pl), rtable);
227 elog(ERROR, "unrecognized node type: %d",
228 (int) nodeTag(plan));
233 * Now recurse into child plans and initplans, if any
235 * NOTE: it is essential that we recurse into child plans AFTER we set
236 * subplan references in this plan's tlist and quals. If we did the
237 * reference-adjustments bottom-up, then we would fail to match this
238 * plan's var nodes against the already-modified nodes of the
239 * children. Fortunately, that consideration doesn't apply to SubPlan
240 * nodes; else we'd need two passes over the expression trees.
242 set_plan_references(plan->lefttree, rtable);
243 set_plan_references(plan->righttree, rtable);
245 foreach(pl, plan->initPlan)
247 SubPlan *sp = (SubPlan *) lfirst(pl);
249 Assert(IsA(sp, SubPlan));
250 set_plan_references(sp->plan, sp->rtable);
255 * fix_expr_references
256 * Do final cleanup on expressions (targetlists or quals).
258 * This consists of looking up operator opcode info for OpExpr nodes
259 * and recursively performing set_plan_references on subplans.
261 * The Plan argument is currently unused, but might be needed again someday.
264 fix_expr_references(Plan *plan, Node *node)
266 /* This tree walk requires no special setup, so away we go... */
267 fix_expr_references_walker(node, NULL);
271 fix_expr_references_walker(Node *node, void *context)
275 if (IsA(node, OpExpr))
276 set_opfuncid((OpExpr *) node);
277 else if (IsA(node, DistinctExpr))
278 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
279 else if (IsA(node, ScalarArrayOpExpr))
280 set_sa_opfuncid((ScalarArrayOpExpr *) node);
281 else if (IsA(node, NullIfExpr))
282 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
283 else if (IsA(node, SubPlan))
285 SubPlan *sp = (SubPlan *) node;
287 set_plan_references(sp->plan, sp->rtable);
289 return expression_tree_walker(node, fix_expr_references_walker, context);
293 * set_join_references
294 * Modifies the target list and quals of a join node to reference its
295 * subplans, by setting the varnos to OUTER or INNER and setting attno
296 * values to the result domain number of either the corresponding outer
297 * or inner join tuple item.
299 * In the case of a nestloop with inner indexscan, we will also need to
300 * apply the same transformation to any outer vars appearing in the
301 * quals of the child indexscan.
303 * 'join' is a join plan node
304 * 'rtable' is the associated range table
307 set_join_references(Join *join, List *rtable)
309 Plan *outer_plan = join->plan.lefttree;
310 Plan *inner_plan = join->plan.righttree;
311 List *outer_tlist = outer_plan->targetlist;
312 List *inner_tlist = inner_plan->targetlist;
313 bool tlists_have_non_vars;
315 tlists_have_non_vars = targetlist_has_non_vars(outer_tlist) ||
316 targetlist_has_non_vars(inner_tlist);
318 /* All join plans have tlist, qual, and joinqual */
319 join->plan.targetlist = join_references(join->plan.targetlist,
324 tlists_have_non_vars);
325 join->plan.qual = join_references(join->plan.qual,
330 tlists_have_non_vars);
331 join->joinqual = join_references(join->joinqual,
336 tlists_have_non_vars);
338 /* Now do join-type-specific stuff */
339 if (IsA(join, NestLoop))
341 if (IsA(inner_plan, IndexScan))
344 * An index is being used to reduce the number of tuples
345 * scanned in the inner relation. If there are join clauses
346 * being used with the index, we must update their outer-rel
347 * var nodes to refer to the outer side of the join.
349 IndexScan *innerscan = (IndexScan *) inner_plan;
350 List *indxqualorig = innerscan->indxqualorig;
352 /* No work needed if indxqual refers only to its own rel... */
353 if (NumRelids((Node *) indxqualorig) > 1)
355 Index innerrel = innerscan->scan.scanrelid;
357 /* only refs to outer vars get changed in the inner qual */
358 innerscan->indxqualorig = join_references(indxqualorig,
363 tlists_have_non_vars);
364 innerscan->indxqual = join_references(innerscan->indxqual,
369 tlists_have_non_vars);
372 * We must fix the inner qpqual too, if it has join
373 * clauses (this could happen if special operators are
374 * involved: some indxquals may get rechecked as qpquals).
376 if (NumRelids((Node *) inner_plan->qual) > 1)
377 inner_plan->qual = join_references(inner_plan->qual,
382 tlists_have_non_vars);
385 else if (IsA(inner_plan, TidScan))
387 TidScan *innerscan = (TidScan *) inner_plan;
388 Index innerrel = innerscan->scan.scanrelid;
390 innerscan->tideval = join_references(innerscan->tideval,
395 tlists_have_non_vars);
398 else if (IsA(join, MergeJoin))
400 MergeJoin *mj = (MergeJoin *) join;
402 mj->mergeclauses = join_references(mj->mergeclauses,
407 tlists_have_non_vars);
409 else if (IsA(join, HashJoin))
411 HashJoin *hj = (HashJoin *) join;
413 hj->hashclauses = join_references(hj->hashclauses,
418 tlists_have_non_vars);
423 * set_uppernode_references
424 * Update the targetlist and quals of an upper-level plan node
425 * to refer to the tuples returned by its lefttree subplan.
427 * This is used for single-input plan types like Agg, Group, Result.
429 * In most cases, we have to match up individual Vars in the tlist and
430 * qual expressions with elements of the subplan's tlist (which was
431 * generated by flatten_tlist() from these selfsame expressions, so it
432 * should have all the required variables). There is an important exception,
433 * however: GROUP BY and ORDER BY expressions will have been pushed into the
434 * subplan tlist unflattened. If these values are also needed in the output
435 * then we want to reference the subplan tlist element rather than recomputing
439 set_uppernode_references(Plan *plan, Index subvarno)
441 Plan *subplan = plan->lefttree;
442 List *subplan_targetlist,
445 bool tlist_has_non_vars;
448 subplan_targetlist = subplan->targetlist;
450 subplan_targetlist = NIL;
452 tlist_has_non_vars = targetlist_has_non_vars(subplan_targetlist);
454 output_targetlist = NIL;
455 foreach(l, plan->targetlist)
457 TargetEntry *tle = (TargetEntry *) lfirst(l);
460 newexpr = replace_vars_with_subplan_refs((Node *) tle->expr,
464 output_targetlist = lappend(output_targetlist,
465 makeTargetEntry(tle->resdom,
468 plan->targetlist = output_targetlist;
470 plan->qual = (List *)
471 replace_vars_with_subplan_refs((Node *) plan->qual,
478 * targetlist_has_non_vars --- are there any non-Var entries in tlist?
480 * In most cases, subplan tlists will be "flat" tlists with only Vars.
481 * Checking for this allows us to save comparisons in common cases.
484 targetlist_has_non_vars(List *tlist)
490 TargetEntry *tle = (TargetEntry *) lfirst(l);
492 if (tle->expr && !IsA(tle->expr, Var))
500 * Creates a new set of targetlist entries or join qual clauses by
501 * changing the varno/varattno values of variables in the clauses
502 * to reference target list values from the outer and inner join
503 * relation target lists.
505 * This is used in two different scenarios: a normal join clause, where
506 * all the Vars in the clause *must* be replaced by OUTER or INNER references;
507 * and an indexscan being used on the inner side of a nestloop join.
508 * In the latter case we want to replace the outer-relation Vars by OUTER
509 * references, but not touch the Vars of the inner relation.
511 * For a normal join, acceptable_rel should be zero so that any failure to
512 * match a Var will be reported as an error. For the indexscan case,
513 * pass inner_tlist = NIL and acceptable_rel = the ID of the inner relation.
515 * 'clauses' is the targetlist or list of join clauses
516 * 'rtable' is the current range table
517 * 'outer_tlist' is the target list of the outer join relation
518 * 'inner_tlist' is the target list of the inner join relation, or NIL
519 * 'acceptable_rel' is either zero or the rangetable index of a relation
520 * whose Vars may appear in the clause without provoking an error.
522 * Returns the new expression tree. The original clause structure is
526 join_references(List *clauses,
530 Index acceptable_rel,
531 bool tlists_have_non_vars)
533 join_references_context context;
535 context.rtable = rtable;
536 context.outer_tlist = outer_tlist;
537 context.inner_tlist = inner_tlist;
538 context.acceptable_rel = acceptable_rel;
539 context.tlists_have_non_vars = tlists_have_non_vars;
540 return (List *) join_references_mutator((Node *) clauses, &context);
544 join_references_mutator(Node *node,
545 join_references_context *context)
551 Var *var = (Var *) node;
554 /* First look for the var in the input tlists */
555 resdom = tlist_member((Node *) var, context->outer_tlist);
558 Var *newvar = (Var *) copyObject(var);
560 newvar->varno = OUTER;
561 newvar->varattno = resdom->resno;
562 return (Node *) newvar;
564 resdom = tlist_member((Node *) var, context->inner_tlist);
567 Var *newvar = (Var *) copyObject(var);
569 newvar->varno = INNER;
570 newvar->varattno = resdom->resno;
571 return (Node *) newvar;
574 /* Return the Var unmodified, if it's for acceptable_rel */
575 if (var->varno == context->acceptable_rel)
576 return (Node *) copyObject(var);
578 /* No referent found for Var */
579 elog(ERROR, "variable not found in subplan target lists");
581 /* Try matching more complex expressions too, if tlists have any */
582 if (context->tlists_have_non_vars)
586 resdom = tlist_member(node, context->outer_tlist);
589 /* Found a matching subplan output expression */
592 newvar = makeVar(OUTER,
597 newvar->varnoold = 0; /* wasn't ever a plain Var */
598 newvar->varoattno = 0;
599 return (Node *) newvar;
601 resdom = tlist_member(node, context->inner_tlist);
604 /* Found a matching subplan output expression */
607 newvar = makeVar(INNER,
612 newvar->varnoold = 0; /* wasn't ever a plain Var */
613 newvar->varoattno = 0;
614 return (Node *) newvar;
617 return expression_tree_mutator(node,
618 join_references_mutator,
623 * replace_vars_with_subplan_refs
624 * This routine modifies an expression tree so that all Var nodes
625 * reference target nodes of a subplan. It is used to fix up
626 * target and qual expressions of non-join upper-level plan nodes.
628 * An error is raised if no matching var can be found in the subplan tlist
629 * --- so this routine should only be applied to nodes whose subplans'
630 * targetlists were generated via flatten_tlist() or some such method.
632 * If tlist_has_non_vars is true, then we try to match whole subexpressions
633 * against elements of the subplan tlist, so that we can avoid recomputing
634 * expressions that were already computed by the subplan. (This is relatively
635 * expensive, so we don't want to try it in the common case where the
636 * subplan tlist is just a flattened list of Vars.)
638 * 'node': the tree to be fixed (a target item or qual)
639 * 'subvarno': varno to be assigned to all Vars
640 * 'subplan_targetlist': target list for subplan
641 * 'tlist_has_non_vars': true if subplan_targetlist contains non-Var exprs
643 * The resulting tree is a copy of the original in which all Var nodes have
644 * varno = subvarno, varattno = resno of corresponding subplan target.
645 * The original tree is not modified.
648 replace_vars_with_subplan_refs(Node *node,
650 List *subplan_targetlist,
651 bool tlist_has_non_vars)
653 replace_vars_with_subplan_refs_context context;
655 context.subvarno = subvarno;
656 context.subplan_targetlist = subplan_targetlist;
657 context.tlist_has_non_vars = tlist_has_non_vars;
658 return replace_vars_with_subplan_refs_mutator(node, &context);
662 replace_vars_with_subplan_refs_mutator(Node *node,
663 replace_vars_with_subplan_refs_context *context)
669 Var *var = (Var *) node;
673 resdom = tlist_member((Node *) var, context->subplan_targetlist);
675 elog(ERROR, "variable not found in subplan target list");
676 newvar = (Var *) copyObject(var);
677 newvar->varno = context->subvarno;
678 newvar->varattno = resdom->resno;
679 return (Node *) newvar;
681 /* Try matching more complex expressions too, if tlist has any */
682 if (context->tlist_has_non_vars)
686 resdom = tlist_member(node, context->subplan_targetlist);
689 /* Found a matching subplan output expression */
692 newvar = makeVar(context->subvarno,
697 newvar->varnoold = 0; /* wasn't ever a plain Var */
698 newvar->varoattno = 0;
699 return (Node *) newvar;
702 return expression_tree_mutator(node,
703 replace_vars_with_subplan_refs_mutator,
707 /*****************************************************************************
708 * OPERATOR REGPROC LOOKUP
709 *****************************************************************************/
713 * Calculate opfuncid field from opno for each OpExpr node in given tree.
714 * The given tree can be anything expression_tree_walker handles.
716 * The argument is modified in-place. (This is OK since we'd want the
717 * same change for any node, even if it gets visited more than once due to
721 fix_opfuncids(Node *node)
723 /* This tree walk requires no special setup, so away we go... */
724 fix_opfuncids_walker(node, NULL);
728 fix_opfuncids_walker(Node *node, void *context)
732 if (IsA(node, OpExpr))
733 set_opfuncid((OpExpr *) node);
734 else if (IsA(node, DistinctExpr))
735 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
736 else if (IsA(node, ScalarArrayOpExpr))
737 set_sa_opfuncid((ScalarArrayOpExpr *) node);
738 else if (IsA(node, NullIfExpr))
739 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
740 return expression_tree_walker(node, fix_opfuncids_walker, context);
745 * Set the opfuncid (procedure OID) in an OpExpr node,
746 * if it hasn't been set already.
748 * Because of struct equivalence, this can also be used for
749 * DistinctExpr and NullIfExpr nodes.
752 set_opfuncid(OpExpr *opexpr)
754 if (opexpr->opfuncid == InvalidOid)
755 opexpr->opfuncid = get_opcode(opexpr->opno);
760 * As above, for ScalarArrayOpExpr nodes.
763 set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
765 if (opexpr->opfuncid == InvalidOid)
766 opexpr->opfuncid = get_opcode(opexpr->opno);