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 * $Header: /cvsroot/pgsql/src/backend/optimizer/plan/setrefs.c,v 1.96 2003/08/04 02:40:01 momjian 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);
172 * Hash does not evaluate its targetlist or quals, so don't
173 * touch those (see comments below). But we do need to fix
174 * its hashkeys. The hashkeys are a little bizarre because
175 * they need to match the hashclauses of the parent HashJoin
176 * node, so we use join_references to fix them.
178 ((Hash *) plan)->hashkeys =
179 join_references(((Hash *) plan)->hashkeys,
182 plan->lefttree->targetlist,
184 targetlist_has_non_vars(plan->lefttree->targetlist));
185 fix_expr_references(plan,
186 (Node *) ((Hash *) plan)->hashkeys);
195 * These plan types don't actually bother to evaluate their
196 * targetlists or quals (because they just return their
197 * unmodified input tuples). The optimizer is lazy about
198 * creating really valid targetlists for them. Best to just
199 * leave the targetlist alone. In particular, we do not want
200 * to process subplans for them, since we will likely end up
201 * reprocessing subplans that also appear in lower levels of
207 set_uppernode_references(plan, (Index) 0);
208 fix_expr_references(plan, (Node *) plan->targetlist);
209 fix_expr_references(plan, (Node *) plan->qual);
214 * Result may or may not have a subplan; no need to fix up
215 * subplan references if it hasn't got one...
217 * XXX why does Result use a different subvarno from Agg/Group?
219 if (plan->lefttree != NULL)
220 set_uppernode_references(plan, (Index) OUTER);
221 fix_expr_references(plan, (Node *) plan->targetlist);
222 fix_expr_references(plan, (Node *) plan->qual);
223 fix_expr_references(plan, ((Result *) plan)->resconstantqual);
228 * Append, like Sort et al, doesn't actually evaluate its
229 * targetlist or quals, and we haven't bothered to give it its
230 * own tlist copy. So, don't fix targetlist/qual. But do
231 * recurse into child plans.
233 foreach(pl, ((Append *) plan)->appendplans)
234 set_plan_references((Plan *) lfirst(pl), rtable);
237 elog(ERROR, "unrecognized node type: %d",
238 (int) nodeTag(plan));
243 * Now recurse into child plans and initplans, if any
245 * NOTE: it is essential that we recurse into child plans AFTER we set
246 * subplan references in this plan's tlist and quals. If we did the
247 * reference-adjustments bottom-up, then we would fail to match this
248 * plan's var nodes against the already-modified nodes of the
249 * children. Fortunately, that consideration doesn't apply to SubPlan
250 * nodes; else we'd need two passes over the expression trees.
252 set_plan_references(plan->lefttree, rtable);
253 set_plan_references(plan->righttree, rtable);
255 foreach(pl, plan->initPlan)
257 SubPlan *sp = (SubPlan *) lfirst(pl);
259 Assert(IsA(sp, SubPlan));
260 set_plan_references(sp->plan, sp->rtable);
265 * fix_expr_references
266 * Do final cleanup on expressions (targetlists or quals).
268 * This consists of looking up operator opcode info for OpExpr nodes
269 * and recursively performing set_plan_references on subplans.
271 * The Plan argument is currently unused, but might be needed again someday.
274 fix_expr_references(Plan *plan, Node *node)
276 /* This tree walk requires no special setup, so away we go... */
277 fix_expr_references_walker(node, NULL);
281 fix_expr_references_walker(Node *node, void *context)
285 if (IsA(node, OpExpr))
286 set_opfuncid((OpExpr *) node);
287 else if (IsA(node, DistinctExpr))
288 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
289 else if (IsA(node, ScalarArrayOpExpr))
290 set_sa_opfuncid((ScalarArrayOpExpr *) node);
291 else if (IsA(node, NullIfExpr))
292 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
293 else if (IsA(node, SubPlan))
295 SubPlan *sp = (SubPlan *) node;
297 set_plan_references(sp->plan, sp->rtable);
299 return expression_tree_walker(node, fix_expr_references_walker, context);
303 * set_join_references
304 * Modifies the target list and quals of a join node to reference its
305 * subplans, by setting the varnos to OUTER or INNER and setting attno
306 * values to the result domain number of either the corresponding outer
307 * or inner join tuple item.
309 * In the case of a nestloop with inner indexscan, we will also need to
310 * apply the same transformation to any outer vars appearing in the
311 * quals of the child indexscan.
313 * 'join' is a join plan node
314 * 'rtable' is the associated range table
317 set_join_references(Join *join, List *rtable)
319 Plan *outer_plan = join->plan.lefttree;
320 Plan *inner_plan = join->plan.righttree;
321 List *outer_tlist = outer_plan->targetlist;
322 List *inner_tlist = inner_plan->targetlist;
323 bool tlists_have_non_vars;
325 tlists_have_non_vars = targetlist_has_non_vars(outer_tlist) ||
326 targetlist_has_non_vars(inner_tlist);
328 /* All join plans have tlist, qual, and joinqual */
329 join->plan.targetlist = join_references(join->plan.targetlist,
334 tlists_have_non_vars);
335 join->plan.qual = join_references(join->plan.qual,
340 tlists_have_non_vars);
341 join->joinqual = join_references(join->joinqual,
346 tlists_have_non_vars);
348 /* Now do join-type-specific stuff */
349 if (IsA(join, NestLoop))
351 if (IsA(inner_plan, IndexScan))
354 * An index is being used to reduce the number of tuples
355 * scanned in the inner relation. If there are join clauses
356 * being used with the index, we must update their outer-rel
357 * var nodes to refer to the outer side of the join.
359 IndexScan *innerscan = (IndexScan *) inner_plan;
360 List *indxqualorig = innerscan->indxqualorig;
362 /* No work needed if indxqual refers only to its own rel... */
363 if (NumRelids((Node *) indxqualorig) > 1)
365 Index innerrel = innerscan->scan.scanrelid;
367 /* only refs to outer vars get changed in the inner qual */
368 innerscan->indxqualorig = join_references(indxqualorig,
373 tlists_have_non_vars);
374 innerscan->indxqual = join_references(innerscan->indxqual,
379 tlists_have_non_vars);
382 * We must fix the inner qpqual too, if it has join
383 * clauses (this could happen if the index is lossy: some
384 * indxquals may get rechecked as qpquals).
386 if (NumRelids((Node *) inner_plan->qual) > 1)
387 inner_plan->qual = join_references(inner_plan->qual,
392 tlists_have_non_vars);
395 else if (IsA(inner_plan, TidScan))
397 TidScan *innerscan = (TidScan *) inner_plan;
398 Index innerrel = innerscan->scan.scanrelid;
400 innerscan->tideval = join_references(innerscan->tideval,
405 tlists_have_non_vars);
408 else if (IsA(join, MergeJoin))
410 MergeJoin *mj = (MergeJoin *) join;
412 mj->mergeclauses = join_references(mj->mergeclauses,
417 tlists_have_non_vars);
419 else if (IsA(join, HashJoin))
421 HashJoin *hj = (HashJoin *) join;
423 hj->hashclauses = join_references(hj->hashclauses,
428 tlists_have_non_vars);
433 * set_uppernode_references
434 * Update the targetlist and quals of an upper-level plan node
435 * to refer to the tuples returned by its lefttree subplan.
437 * This is used for single-input plan types like Agg, Group, Result.
439 * In most cases, we have to match up individual Vars in the tlist and
440 * qual expressions with elements of the subplan's tlist (which was
441 * generated by flatten_tlist() from these selfsame expressions, so it
442 * should have all the required variables). There is an important exception,
443 * however: GROUP BY and ORDER BY expressions will have been pushed into the
444 * subplan tlist unflattened. If these values are also needed in the output
445 * then we want to reference the subplan tlist element rather than recomputing
449 set_uppernode_references(Plan *plan, Index subvarno)
451 Plan *subplan = plan->lefttree;
452 List *subplan_targetlist,
455 bool tlist_has_non_vars;
458 subplan_targetlist = subplan->targetlist;
460 subplan_targetlist = NIL;
462 tlist_has_non_vars = targetlist_has_non_vars(subplan_targetlist);
464 output_targetlist = NIL;
465 foreach(l, plan->targetlist)
467 TargetEntry *tle = (TargetEntry *) lfirst(l);
470 newexpr = replace_vars_with_subplan_refs((Node *) tle->expr,
474 output_targetlist = lappend(output_targetlist,
475 makeTargetEntry(tle->resdom,
478 plan->targetlist = output_targetlist;
480 plan->qual = (List *)
481 replace_vars_with_subplan_refs((Node *) plan->qual,
488 * targetlist_has_non_vars --- are there any non-Var entries in tlist?
490 * In most cases, subplan tlists will be "flat" tlists with only Vars.
491 * Checking for this allows us to save comparisons in common cases.
494 targetlist_has_non_vars(List *tlist)
500 TargetEntry *tle = (TargetEntry *) lfirst(l);
502 if (tle->expr && !IsA(tle->expr, Var))
510 * Creates a new set of targetlist entries or join qual clauses by
511 * changing the varno/varattno values of variables in the clauses
512 * to reference target list values from the outer and inner join
513 * relation target lists.
515 * This is used in two different scenarios: a normal join clause, where
516 * all the Vars in the clause *must* be replaced by OUTER or INNER references;
517 * and an indexscan being used on the inner side of a nestloop join.
518 * In the latter case we want to replace the outer-relation Vars by OUTER
519 * references, but not touch the Vars of the inner relation.
521 * For a normal join, acceptable_rel should be zero so that any failure to
522 * match a Var will be reported as an error. For the indexscan case,
523 * pass inner_tlist = NIL and acceptable_rel = the ID of the inner relation.
525 * 'clauses' is the targetlist or list of join clauses
526 * 'rtable' is the current range table
527 * 'outer_tlist' is the target list of the outer join relation
528 * 'inner_tlist' is the target list of the inner join relation, or NIL
529 * 'acceptable_rel' is either zero or the rangetable index of a relation
530 * whose Vars may appear in the clause without provoking an error.
532 * Returns the new expression tree. The original clause structure is
536 join_references(List *clauses,
540 Index acceptable_rel,
541 bool tlists_have_non_vars)
543 join_references_context context;
545 context.rtable = rtable;
546 context.outer_tlist = outer_tlist;
547 context.inner_tlist = inner_tlist;
548 context.acceptable_rel = acceptable_rel;
549 context.tlists_have_non_vars = tlists_have_non_vars;
550 return (List *) join_references_mutator((Node *) clauses, &context);
554 join_references_mutator(Node *node,
555 join_references_context *context)
561 Var *var = (Var *) node;
564 /* First look for the var in the input tlists */
565 resdom = tlist_member((Node *) var, context->outer_tlist);
568 Var *newvar = (Var *) copyObject(var);
570 newvar->varno = OUTER;
571 newvar->varattno = resdom->resno;
572 return (Node *) newvar;
574 resdom = tlist_member((Node *) var, context->inner_tlist);
577 Var *newvar = (Var *) copyObject(var);
579 newvar->varno = INNER;
580 newvar->varattno = resdom->resno;
581 return (Node *) newvar;
584 /* Return the Var unmodified, if it's for acceptable_rel */
585 if (var->varno == context->acceptable_rel)
586 return (Node *) copyObject(var);
588 /* No referent found for Var */
589 elog(ERROR, "variable not found in subplan target lists");
591 /* Try matching more complex expressions too, if tlists have any */
592 if (context->tlists_have_non_vars)
596 resdom = tlist_member(node, context->outer_tlist);
599 /* Found a matching subplan output expression */
602 newvar = makeVar(OUTER,
607 newvar->varnoold = 0; /* wasn't ever a plain Var */
608 newvar->varoattno = 0;
609 return (Node *) newvar;
611 resdom = tlist_member(node, context->inner_tlist);
614 /* Found a matching subplan output expression */
617 newvar = makeVar(INNER,
622 newvar->varnoold = 0; /* wasn't ever a plain Var */
623 newvar->varoattno = 0;
624 return (Node *) newvar;
627 return expression_tree_mutator(node,
628 join_references_mutator,
633 * replace_vars_with_subplan_refs
634 * This routine modifies an expression tree so that all Var nodes
635 * reference target nodes of a subplan. It is used to fix up
636 * target and qual expressions of non-join upper-level plan nodes.
638 * An error is raised if no matching var can be found in the subplan tlist
639 * --- so this routine should only be applied to nodes whose subplans'
640 * targetlists were generated via flatten_tlist() or some such method.
642 * If tlist_has_non_vars is true, then we try to match whole subexpressions
643 * against elements of the subplan tlist, so that we can avoid recomputing
644 * expressions that were already computed by the subplan. (This is relatively
645 * expensive, so we don't want to try it in the common case where the
646 * subplan tlist is just a flattened list of Vars.)
648 * 'node': the tree to be fixed (a target item or qual)
649 * 'subvarno': varno to be assigned to all Vars
650 * 'subplan_targetlist': target list for subplan
651 * 'tlist_has_non_vars': true if subplan_targetlist contains non-Var exprs
653 * The resulting tree is a copy of the original in which all Var nodes have
654 * varno = subvarno, varattno = resno of corresponding subplan target.
655 * The original tree is not modified.
658 replace_vars_with_subplan_refs(Node *node,
660 List *subplan_targetlist,
661 bool tlist_has_non_vars)
663 replace_vars_with_subplan_refs_context context;
665 context.subvarno = subvarno;
666 context.subplan_targetlist = subplan_targetlist;
667 context.tlist_has_non_vars = tlist_has_non_vars;
668 return replace_vars_with_subplan_refs_mutator(node, &context);
672 replace_vars_with_subplan_refs_mutator(Node *node,
673 replace_vars_with_subplan_refs_context *context)
679 Var *var = (Var *) node;
683 resdom = tlist_member((Node *) var, context->subplan_targetlist);
685 elog(ERROR, "variable not found in subplan target list");
686 newvar = (Var *) copyObject(var);
687 newvar->varno = context->subvarno;
688 newvar->varattno = resdom->resno;
689 return (Node *) newvar;
691 /* Try matching more complex expressions too, if tlist has any */
692 if (context->tlist_has_non_vars)
696 resdom = tlist_member(node, context->subplan_targetlist);
699 /* Found a matching subplan output expression */
702 newvar = makeVar(context->subvarno,
707 newvar->varnoold = 0; /* wasn't ever a plain Var */
708 newvar->varoattno = 0;
709 return (Node *) newvar;
712 return expression_tree_mutator(node,
713 replace_vars_with_subplan_refs_mutator,
717 /*****************************************************************************
718 * OPERATOR REGPROC LOOKUP
719 *****************************************************************************/
723 * Calculate opfuncid field from opno for each OpExpr node in given tree.
724 * The given tree can be anything expression_tree_walker handles.
726 * The argument is modified in-place. (This is OK since we'd want the
727 * same change for any node, even if it gets visited more than once due to
731 fix_opfuncids(Node *node)
733 /* This tree walk requires no special setup, so away we go... */
734 fix_opfuncids_walker(node, NULL);
738 fix_opfuncids_walker(Node *node, void *context)
742 if (IsA(node, OpExpr))
743 set_opfuncid((OpExpr *) node);
744 else if (IsA(node, DistinctExpr))
745 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
746 else if (IsA(node, ScalarArrayOpExpr))
747 set_sa_opfuncid((ScalarArrayOpExpr *) node);
748 else if (IsA(node, NullIfExpr))
749 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
750 return expression_tree_walker(node, fix_opfuncids_walker, context);
755 * Set the opfuncid (procedure OID) in an OpExpr node,
756 * if it hasn't been set already.
758 * Because of struct equivalence, this can also be used for
759 * DistinctExpr and NullIfExpr nodes.
762 set_opfuncid(OpExpr * opexpr)
764 if (opexpr->opfuncid == InvalidOid)
765 opexpr->opfuncid = get_opcode(opexpr->opno);
770 * As above, for ScalarArrayOpExpr nodes.
773 set_sa_opfuncid(ScalarArrayOpExpr * opexpr)
775 if (opexpr->opfuncid == InvalidOid)
776 opexpr->opfuncid = get_opcode(opexpr->opno);