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-2002, 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.90 2003/01/15 23:10:32 tgl Exp $
14 *-------------------------------------------------------------------------
19 #include "nodes/makefuncs.h"
20 #include "nodes/nodeFuncs.h"
21 #include "optimizer/clauses.h"
22 #include "optimizer/planmain.h"
23 #include "optimizer/tlist.h"
24 #include "optimizer/var.h"
25 #include "parser/parsetree.h"
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 List *join_references(List *clauses,
51 Index acceptable_rel);
52 static Node *join_references_mutator(Node *node,
53 join_references_context *context);
54 static Node *replace_vars_with_subplan_refs(Node *node,
56 List *subplan_targetlist,
57 bool tlist_has_non_vars);
58 static Node *replace_vars_with_subplan_refs_mutator(Node *node,
59 replace_vars_with_subplan_refs_context *context);
60 static bool fix_opfuncids_walker(Node *node, void *context);
62 /*****************************************************************************
66 *****************************************************************************/
70 * This is the final processing pass of the planner/optimizer. The plan
71 * tree is complete; we just have to adjust some representational details
72 * for the convenience of the executor. We update Vars in upper plan nodes
73 * to refer to the outputs of their subplans, and we compute regproc OIDs
74 * for operators (ie, we look up the function that implements each op).
76 * set_plan_references recursively traverses the whole plan tree.
78 * Returns nothing of interest, but modifies internal fields of nodes.
81 set_plan_references(Plan *plan, List *rtable)
89 * Plan-type-specific fixes
91 switch (nodeTag(plan))
94 fix_expr_references(plan, (Node *) plan->targetlist);
95 fix_expr_references(plan, (Node *) plan->qual);
98 fix_expr_references(plan, (Node *) plan->targetlist);
99 fix_expr_references(plan, (Node *) plan->qual);
100 fix_expr_references(plan,
101 (Node *) ((IndexScan *) plan)->indxqual);
102 fix_expr_references(plan,
103 (Node *) ((IndexScan *) plan)->indxqualorig);
106 fix_expr_references(plan, (Node *) plan->targetlist);
107 fix_expr_references(plan, (Node *) plan->qual);
108 fix_expr_references(plan,
109 (Node *) ((TidScan *) plan)->tideval);
116 * We do not do set_uppernode_references() here, because a
117 * SubqueryScan will always have been created with correct
118 * references to its subplan's outputs to begin with.
120 fix_expr_references(plan, (Node *) plan->targetlist);
121 fix_expr_references(plan, (Node *) plan->qual);
123 /* Recurse into subplan too */
124 rte = rt_fetch(((SubqueryScan *) plan)->scan.scanrelid,
126 Assert(rte->rtekind == RTE_SUBQUERY);
127 set_plan_references(((SubqueryScan *) plan)->subplan,
128 rte->subquery->rtable);
135 fix_expr_references(plan, (Node *) plan->targetlist);
136 fix_expr_references(plan, (Node *) plan->qual);
137 rte = rt_fetch(((FunctionScan *) plan)->scan.scanrelid,
139 Assert(rte->rtekind == RTE_FUNCTION);
140 fix_expr_references(plan, rte->funcexpr);
144 set_join_references((Join *) plan, rtable);
145 fix_expr_references(plan, (Node *) plan->targetlist);
146 fix_expr_references(plan, (Node *) plan->qual);
147 fix_expr_references(plan, (Node *) ((Join *) plan)->joinqual);
150 set_join_references((Join *) plan, rtable);
151 fix_expr_references(plan, (Node *) plan->targetlist);
152 fix_expr_references(plan, (Node *) plan->qual);
153 fix_expr_references(plan, (Node *) ((Join *) plan)->joinqual);
154 fix_expr_references(plan,
155 (Node *) ((MergeJoin *) plan)->mergeclauses);
158 set_join_references((Join *) plan, rtable);
159 fix_expr_references(plan, (Node *) plan->targetlist);
160 fix_expr_references(plan, (Node *) plan->qual);
161 fix_expr_references(plan, (Node *) ((Join *) plan)->joinqual);
162 fix_expr_references(plan,
163 (Node *) ((HashJoin *) plan)->hashclauses);
167 * Hash does not evaluate its targetlist or quals, so don't
168 * touch those (see comments below). But we do need to fix its
169 * hashkeys. The hashkeys are a little bizarre because they
170 * need to match the hashclauses of the parent HashJoin node,
171 * so we use join_references to fix them.
173 ((Hash *) plan)->hashkeys =
174 join_references(((Hash *) plan)->hashkeys,
177 plan->lefttree->targetlist,
187 * These plan types don't actually bother to evaluate their
188 * targetlists or quals (because they just return their
189 * unmodified input tuples). The optimizer is lazy about
190 * creating really valid targetlists for them. Best to just
191 * leave the targetlist alone. In particular, we do not want
192 * to process subplans for them, since we will likely end
193 * up reprocessing subplans that also appear in lower levels
199 set_uppernode_references(plan, (Index) 0);
200 fix_expr_references(plan, (Node *) plan->targetlist);
201 fix_expr_references(plan, (Node *) plan->qual);
206 * Result may or may not have a subplan; no need to fix up
207 * subplan references if it hasn't got one...
209 * XXX why does Result use a different subvarno from Agg/Group?
211 if (plan->lefttree != NULL)
212 set_uppernode_references(plan, (Index) OUTER);
213 fix_expr_references(plan, (Node *) plan->targetlist);
214 fix_expr_references(plan, (Node *) plan->qual);
215 fix_expr_references(plan, ((Result *) plan)->resconstantqual);
220 * Append, like Sort et al, doesn't actually evaluate its
221 * targetlist or quals, and we haven't bothered to give it its
222 * own tlist copy. So, don't fix targetlist/qual. But do
223 * recurse into child plans.
225 foreach(pl, ((Append *) plan)->appendplans)
226 set_plan_references((Plan *) lfirst(pl), rtable);
229 elog(ERROR, "set_plan_references: unknown plan type %d",
235 * Now recurse into child plans and initplans, if any
237 * NOTE: it is essential that we recurse into child plans AFTER we set
238 * subplan references in this plan's tlist and quals. If we did the
239 * reference-adjustments bottom-up, then we would fail to match this
240 * plan's var nodes against the already-modified nodes of the
241 * children. Fortunately, that consideration doesn't apply to SubPlan
242 * nodes; else we'd need two passes over the expression trees.
244 set_plan_references(plan->lefttree, rtable);
245 set_plan_references(plan->righttree, rtable);
247 foreach(pl, plan->initPlan)
249 SubPlan *sp = (SubPlan *) lfirst(pl);
251 Assert(IsA(sp, SubPlan));
252 set_plan_references(sp->plan, sp->rtable);
257 * fix_expr_references
258 * Do final cleanup on expressions (targetlists or quals).
260 * This consists of looking up operator opcode info for OpExpr nodes
261 * and recursively performing set_plan_references on subplans.
263 * The Plan argument is currently unused, but might be needed again someday.
266 fix_expr_references(Plan *plan, Node *node)
268 /* This tree walk requires no special setup, so away we go... */
269 fix_expr_references_walker(node, NULL);
273 fix_expr_references_walker(Node *node, void *context)
277 if (IsA(node, OpExpr))
278 set_opfuncid((OpExpr *) node);
279 else if (IsA(node, DistinctExpr))
280 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
281 else if (IsA(node, SubPlan))
283 SubPlan *sp = (SubPlan *) node;
285 set_plan_references(sp->plan, sp->rtable);
287 return expression_tree_walker(node, fix_expr_references_walker, context);
291 * set_join_references
292 * Modifies the target list and quals of a join node to reference its
293 * subplans, by setting the varnos to OUTER or INNER and setting attno
294 * values to the result domain number of either the corresponding outer
295 * or inner join tuple item.
297 * In the case of a nestloop with inner indexscan, we will also need to
298 * apply the same transformation to any outer vars appearing in the
299 * quals of the child indexscan.
301 * 'join' is a join plan node
302 * 'rtable' is the associated range table
305 set_join_references(Join *join, List *rtable)
307 Plan *outer_plan = join->plan.lefttree;
308 Plan *inner_plan = join->plan.righttree;
309 List *outer_tlist = outer_plan->targetlist;
310 List *inner_tlist = inner_plan->targetlist;
312 /* All join plans have tlist, qual, and joinqual */
313 join->plan.targetlist = join_references(join->plan.targetlist,
318 join->plan.qual = join_references(join->plan.qual,
323 join->joinqual = join_references(join->joinqual,
329 /* Now do join-type-specific stuff */
330 if (IsA(join, NestLoop))
332 if (IsA(inner_plan, IndexScan))
335 * An index is being used to reduce the number of tuples scanned
336 * in the inner relation. If there are join clauses being used
337 * with the index, we must update their outer-rel var nodes to
338 * refer to the outer side of the join.
340 IndexScan *innerscan = (IndexScan *) inner_plan;
341 List *indxqualorig = innerscan->indxqualorig;
343 /* No work needed if indxqual refers only to its own rel... */
344 if (NumRelids((Node *) indxqualorig) > 1)
346 Index innerrel = innerscan->scan.scanrelid;
348 /* only refs to outer vars get changed in the inner qual */
349 innerscan->indxqualorig = join_references(indxqualorig,
354 innerscan->indxqual = join_references(innerscan->indxqual,
360 * We must fix the inner qpqual too, if it has join clauses
361 * (this could happen if the index is lossy: some indxquals
362 * may get rechecked as qpquals).
364 if (NumRelids((Node *) inner_plan->qual) > 1)
365 inner_plan->qual = join_references(inner_plan->qual,
372 else if (IsA(inner_plan, TidScan))
374 TidScan *innerscan = (TidScan *) inner_plan;
375 Index innerrel = innerscan->scan.scanrelid;
377 innerscan->tideval = join_references(innerscan->tideval,
384 else if (IsA(join, MergeJoin))
386 MergeJoin *mj = (MergeJoin *) join;
388 mj->mergeclauses = join_references(mj->mergeclauses,
394 else if (IsA(join, HashJoin))
396 HashJoin *hj = (HashJoin *) join;
398 hj->hashclauses = join_references(hj->hashclauses,
407 * set_uppernode_references
408 * Update the targetlist and quals of an upper-level plan node
409 * to refer to the tuples returned by its lefttree subplan.
411 * This is used for single-input plan types like Agg, Group, Result.
413 * In most cases, we have to match up individual Vars in the tlist and
414 * qual expressions with elements of the subplan's tlist (which was
415 * generated by flatten_tlist() from these selfsame expressions, so it
416 * should have all the required variables). There is an important exception,
417 * however: GROUP BY and ORDER BY expressions will have been pushed into the
418 * subplan tlist unflattened. If these values are also needed in the output
419 * then we want to reference the subplan tlist element rather than recomputing
423 set_uppernode_references(Plan *plan, Index subvarno)
425 Plan *subplan = plan->lefttree;
426 List *subplan_targetlist,
429 bool tlist_has_non_vars;
432 subplan_targetlist = subplan->targetlist;
434 subplan_targetlist = NIL;
437 * Detect whether subplan tlist has any non-Vars (typically it won't
438 * because it's been flattened). This allows us to save comparisons
441 tlist_has_non_vars = false;
442 foreach(l, subplan_targetlist)
444 TargetEntry *tle = (TargetEntry *) lfirst(l);
446 if (tle->expr && !IsA(tle->expr, Var))
448 tlist_has_non_vars = true;
453 output_targetlist = NIL;
454 foreach(l, plan->targetlist)
456 TargetEntry *tle = (TargetEntry *) lfirst(l);
459 newexpr = replace_vars_with_subplan_refs((Node *) tle->expr,
463 output_targetlist = lappend(output_targetlist,
464 makeTargetEntry(tle->resdom,
467 plan->targetlist = output_targetlist;
469 plan->qual = (List *)
470 replace_vars_with_subplan_refs((Node *) plan->qual,
478 * Creates a new set of targetlist entries or join qual clauses by
479 * changing the varno/varattno values of variables in the clauses
480 * to reference target list values from the outer and inner join
481 * relation target lists.
483 * This is used in two different scenarios: a normal join clause, where
484 * all the Vars in the clause *must* be replaced by OUTER or INNER references;
485 * and an indexscan being used on the inner side of a nestloop join.
486 * In the latter case we want to replace the outer-relation Vars by OUTER
487 * references, but not touch the Vars of the inner relation.
489 * For a normal join, acceptable_rel should be zero so that any failure to
490 * match a Var will be reported as an error. For the indexscan case,
491 * pass inner_tlist = NIL and acceptable_rel = the ID of the inner relation.
493 * 'clauses' is the targetlist or list of join clauses
494 * 'rtable' is the current range table
495 * 'outer_tlist' is the target list of the outer join relation
496 * 'inner_tlist' is the target list of the inner join relation, or NIL
497 * 'acceptable_rel' is either zero or the rangetable index of a relation
498 * whose Vars may appear in the clause without provoking an error.
500 * Returns the new expression tree. The original clause structure is
504 join_references(List *clauses,
508 Index acceptable_rel)
510 join_references_context context;
512 context.rtable = rtable;
513 context.outer_tlist = outer_tlist;
514 context.inner_tlist = inner_tlist;
515 context.acceptable_rel = acceptable_rel;
516 return (List *) join_references_mutator((Node *) clauses, &context);
520 join_references_mutator(Node *node,
521 join_references_context *context)
527 Var *var = (Var *) node;
530 /* First look for the var in the input tlists */
531 resdom = tlist_member((Node *) var, context->outer_tlist);
534 Var *newvar = (Var *) copyObject(var);
536 newvar->varno = OUTER;
537 newvar->varattno = resdom->resno;
538 return (Node *) newvar;
540 resdom = tlist_member((Node *) var, context->inner_tlist);
543 Var *newvar = (Var *) copyObject(var);
545 newvar->varno = INNER;
546 newvar->varattno = resdom->resno;
547 return (Node *) newvar;
550 /* Return the Var unmodified, if it's for acceptable_rel */
551 if (var->varno == context->acceptable_rel)
552 return (Node *) copyObject(var);
554 /* No referent found for Var */
555 elog(ERROR, "join_references: variable not in subplan target lists");
557 return expression_tree_mutator(node,
558 join_references_mutator,
563 * replace_vars_with_subplan_refs
564 * This routine modifies an expression tree so that all Var nodes
565 * reference target nodes of a subplan. It is used to fix up
566 * target and qual expressions of non-join upper-level plan nodes.
568 * An error is raised if no matching var can be found in the subplan tlist
569 * --- so this routine should only be applied to nodes whose subplans'
570 * targetlists were generated via flatten_tlist() or some such method.
572 * If tlist_has_non_vars is true, then we try to match whole subexpressions
573 * against elements of the subplan tlist, so that we can avoid recomputing
574 * expressions that were already computed by the subplan. (This is relatively
575 * expensive, so we don't want to try it in the common case where the
576 * subplan tlist is just a flattened list of Vars.)
578 * 'node': the tree to be fixed (a target item or qual)
579 * 'subvarno': varno to be assigned to all Vars
580 * 'subplan_targetlist': target list for subplan
581 * 'tlist_has_non_vars': true if subplan_targetlist contains non-Var exprs
583 * The resulting tree is a copy of the original in which all Var nodes have
584 * varno = subvarno, varattno = resno of corresponding subplan target.
585 * The original tree is not modified.
588 replace_vars_with_subplan_refs(Node *node,
590 List *subplan_targetlist,
591 bool tlist_has_non_vars)
593 replace_vars_with_subplan_refs_context context;
595 context.subvarno = subvarno;
596 context.subplan_targetlist = subplan_targetlist;
597 context.tlist_has_non_vars = tlist_has_non_vars;
598 return replace_vars_with_subplan_refs_mutator(node, &context);
602 replace_vars_with_subplan_refs_mutator(Node *node,
603 replace_vars_with_subplan_refs_context *context)
609 Var *var = (Var *) node;
613 resdom = tlist_member((Node *) var, context->subplan_targetlist);
615 elog(ERROR, "replace_vars_with_subplan_refs: variable not in subplan target list");
616 newvar = (Var *) copyObject(var);
617 newvar->varno = context->subvarno;
618 newvar->varattno = resdom->resno;
619 return (Node *) newvar;
621 /* Try matching more complex expressions too, if tlist has any */
622 if (context->tlist_has_non_vars)
626 resdom = tlist_member(node, context->subplan_targetlist);
629 /* Found a matching subplan output expression */
632 newvar = makeVar(context->subvarno,
637 newvar->varnoold = 0; /* wasn't ever a plain Var */
638 newvar->varoattno = 0;
639 return (Node *) newvar;
642 return expression_tree_mutator(node,
643 replace_vars_with_subplan_refs_mutator,
647 /*****************************************************************************
648 * OPERATOR REGPROC LOOKUP
649 *****************************************************************************/
653 * Calculate opfuncid field from opno for each OpExpr node in given tree.
654 * The given tree can be anything expression_tree_walker handles.
656 * The argument is modified in-place. (This is OK since we'd want the
657 * same change for any node, even if it gets visited more than once due to
661 fix_opfuncids(Node *node)
663 /* This tree walk requires no special setup, so away we go... */
664 fix_opfuncids_walker(node, NULL);
668 fix_opfuncids_walker(Node *node, void *context)
672 if (IsA(node, OpExpr))
673 set_opfuncid((OpExpr *) node);
674 else if (IsA(node, DistinctExpr))
675 set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
676 return expression_tree_walker(node, fix_opfuncids_walker, context);