1 /*-------------------------------------------------------------------------
4 * Planning routines for subselects and parameters.
6 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/backend/optimizer/plan/subselect.c
12 *-------------------------------------------------------------------------
16 #include "catalog/pg_operator.h"
17 #include "catalog/pg_type.h"
18 #include "executor/executor.h"
19 #include "miscadmin.h"
20 #include "nodes/makefuncs.h"
21 #include "nodes/nodeFuncs.h"
22 #include "optimizer/clauses.h"
23 #include "optimizer/cost.h"
24 #include "optimizer/planmain.h"
25 #include "optimizer/planner.h"
26 #include "optimizer/prep.h"
27 #include "optimizer/subselect.h"
28 #include "optimizer/var.h"
29 #include "parser/parse_relation.h"
30 #include "rewrite/rewriteManip.h"
31 #include "utils/builtins.h"
32 #include "utils/lsyscache.h"
33 #include "utils/syscache.h"
36 typedef struct convert_testexpr_context
39 List *subst_nodes; /* Nodes to substitute for Params */
40 } convert_testexpr_context;
42 typedef struct process_sublinks_context
46 } process_sublinks_context;
48 typedef struct finalize_primnode_context
51 Bitmapset *paramids; /* Non-local PARAM_EXEC paramids found */
52 } finalize_primnode_context;
55 static Node *build_subplan(PlannerInfo *root, Plan *plan, PlannerInfo *subroot,
56 SubLinkType subLinkType, Node *testexpr,
57 bool adjust_testexpr, bool unknownEqFalse);
58 static List *generate_subquery_params(PlannerInfo *root, List *tlist,
60 static List *generate_subquery_vars(PlannerInfo *root, List *tlist,
62 static Node *convert_testexpr(PlannerInfo *root,
65 static Node *convert_testexpr_mutator(Node *node,
66 convert_testexpr_context *context);
67 static bool subplan_is_hashable(Plan *plan);
68 static bool testexpr_is_hashable(Node *testexpr);
69 static bool hash_ok_operator(OpExpr *expr);
70 static bool simplify_EXISTS_query(Query *query);
71 static Query *convert_EXISTS_to_ANY(PlannerInfo *root, Query *subselect,
72 Node **testexpr, List **paramIds);
73 static Node *replace_correlation_vars_mutator(Node *node, PlannerInfo *root);
74 static Node *process_sublinks_mutator(Node *node,
75 process_sublinks_context *context);
76 static Bitmapset *finalize_plan(PlannerInfo *root,
78 Bitmapset *valid_params,
79 Bitmapset *scan_params);
80 static bool finalize_primnode(Node *node, finalize_primnode_context *context);
84 * Select a PARAM_EXEC number to identify the given Var.
85 * If the Var already has a param slot, return that one.
88 assign_param_for_var(PlannerInfo *root, Var *var)
91 PlannerParamItem *pitem;
95 abslevel = root->query_level - var->varlevelsup;
97 /* If there's already a paramlist entry for this same Var, just use it */
99 foreach(ppl, root->glob->paramlist)
101 pitem = (PlannerParamItem *) lfirst(ppl);
102 if (pitem->abslevel == abslevel && IsA(pitem->item, Var))
104 Var *pvar = (Var *) pitem->item;
106 if (pvar->varno == var->varno &&
107 pvar->varattno == var->varattno &&
108 pvar->vartype == var->vartype &&
109 pvar->vartypmod == var->vartypmod)
115 /* Nope, so make a new one */
116 var = (Var *) copyObject(var);
117 var->varlevelsup = 0;
119 pitem = makeNode(PlannerParamItem);
120 pitem->item = (Node *) var;
121 pitem->abslevel = abslevel;
123 root->glob->paramlist = lappend(root->glob->paramlist, pitem);
125 /* i is already the correct list index for the new item */
130 * Generate a Param node to replace the given Var,
131 * which is expected to have varlevelsup > 0 (ie, it is not local).
134 replace_outer_var(PlannerInfo *root, Var *var)
139 Assert(var->varlevelsup > 0 && var->varlevelsup < root->query_level);
142 * Find the Var in root->glob->paramlist, or add it if not present.
144 * NOTE: in sufficiently complex querytrees, it is possible for the same
145 * varno/abslevel to refer to different RTEs in different parts of the
146 * parsetree, so that different fields might end up sharing the same Param
147 * number. As long as we check the vartype/typmod as well, I believe that
148 * this sort of aliasing will cause no trouble. The correct field should
149 * get stored into the Param slot at execution in each part of the tree.
151 i = assign_param_for_var(root, var);
153 retval = makeNode(Param);
154 retval->paramkind = PARAM_EXEC;
156 retval->paramtype = var->vartype;
157 retval->paramtypmod = var->vartypmod;
158 retval->paramcollid = var->varcollid;
159 retval->location = -1;
165 * Generate a Param node to replace the given Var, which will be supplied
166 * from an upper NestLoop join node.
168 * Because we allow nestloop and subquery Params to alias each other,
169 * this is effectively the same as replace_outer_var, except that we expect
170 * the Var to be local to the current query level.
173 assign_nestloop_param(PlannerInfo *root, Var *var)
178 Assert(var->varlevelsup == 0);
180 i = assign_param_for_var(root, var);
182 retval = makeNode(Param);
183 retval->paramkind = PARAM_EXEC;
185 retval->paramtype = var->vartype;
186 retval->paramtypmod = var->vartypmod;
187 retval->paramcollid = var->varcollid;
188 retval->location = -1;
194 * Generate a Param node to replace the given Aggref
195 * which is expected to have agglevelsup > 0 (ie, it is not local).
198 replace_outer_agg(PlannerInfo *root, Aggref *agg)
201 PlannerParamItem *pitem;
205 Assert(agg->agglevelsup > 0 && agg->agglevelsup < root->query_level);
206 abslevel = root->query_level - agg->agglevelsup;
209 * It does not seem worthwhile to try to match duplicate outer aggs. Just
210 * make a new slot every time.
212 agg = (Aggref *) copyObject(agg);
213 IncrementVarSublevelsUp((Node *) agg, -((int) agg->agglevelsup), 0);
214 Assert(agg->agglevelsup == 0);
216 pitem = makeNode(PlannerParamItem);
217 pitem->item = (Node *) agg;
218 pitem->abslevel = abslevel;
220 root->glob->paramlist = lappend(root->glob->paramlist, pitem);
221 i = list_length(root->glob->paramlist) - 1;
223 retval = makeNode(Param);
224 retval->paramkind = PARAM_EXEC;
226 retval->paramtype = agg->aggtype;
227 retval->paramtypmod = -1;
228 retval->paramcollid = agg->aggcollid;
229 retval->location = -1;
235 * Generate a new Param node that will not conflict with any other.
237 * This is used to allocate PARAM_EXEC slots for subplan outputs.
240 generate_new_param(PlannerInfo *root, Oid paramtype, int32 paramtypmod,
244 PlannerParamItem *pitem;
246 retval = makeNode(Param);
247 retval->paramkind = PARAM_EXEC;
248 retval->paramid = list_length(root->glob->paramlist);
249 retval->paramtype = paramtype;
250 retval->paramtypmod = paramtypmod;
251 retval->paramcollid = paramcollation;
252 retval->location = -1;
254 pitem = makeNode(PlannerParamItem);
255 pitem->item = (Node *) retval;
256 pitem->abslevel = root->query_level;
258 root->glob->paramlist = lappend(root->glob->paramlist, pitem);
264 * Assign a (nonnegative) PARAM_EXEC ID for a special parameter (one that
265 * is not actually used to carry a value at runtime). Such parameters are
266 * used for special runtime signaling purposes, such as connecting a
267 * recursive union node to its worktable scan node or forcing plan
268 * re-evaluation within the EvalPlanQual mechanism.
271 SS_assign_special_param(PlannerInfo *root)
275 /* We generate a Param of datatype INTERNAL */
276 param = generate_new_param(root, INTERNALOID, -1, InvalidOid);
277 /* ... but the caller only cares about its ID */
278 return param->paramid;
282 * Get the datatype/typmod/collation of the first column of the plan's output.
284 * This information is stored for ARRAY_SUBLINK execution and for
285 * exprType()/exprTypmod()/exprCollation(), which have no way to get at the
286 * plan associated with a SubPlan node. We really only need the info for
287 * EXPR_SUBLINK and ARRAY_SUBLINK subplans, but for consistency we save it
291 get_first_col_type(Plan *plan, Oid *coltype, int32 *coltypmod,
294 /* In cases such as EXISTS, tlist might be empty; arbitrarily use VOID */
295 if (plan->targetlist)
297 TargetEntry *tent = (TargetEntry *) linitial(plan->targetlist);
299 Assert(IsA(tent, TargetEntry));
302 *coltype = exprType((Node *) tent->expr);
303 *coltypmod = exprTypmod((Node *) tent->expr);
304 *colcollation = exprCollation((Node *) tent->expr);
310 *colcollation = InvalidOid;
314 * Convert a SubLink (as created by the parser) into a SubPlan.
316 * We are given the SubLink's contained query, type, and testexpr. We are
317 * also told if this expression appears at top level of a WHERE/HAVING qual.
319 * Note: we assume that the testexpr has been AND/OR flattened (actually,
320 * it's been through eval_const_expressions), but not converted to
321 * implicit-AND form; and any SubLinks in it should already have been
322 * converted to SubPlans. The subquery is as yet untouched, however.
324 * The result is whatever we need to substitute in place of the SubLink
325 * node in the executable expression. This will be either the SubPlan
326 * node (if we have to do the subplan as a subplan), or a Param node
327 * representing the result of an InitPlan, or a row comparison expression
328 * tree containing InitPlan Param nodes.
331 make_subplan(PlannerInfo *root, Query *orig_subquery, SubLinkType subLinkType,
332 Node *testexpr, bool isTopQual)
335 bool simple_exists = false;
336 double tuple_fraction;
338 PlannerInfo *subroot;
342 * Copy the source Query node. This is a quick and dirty kluge to resolve
343 * the fact that the parser can generate trees with multiple links to the
344 * same sub-Query node, but the planner wants to scribble on the Query.
345 * Try to clean this up when we do querytree redesign...
347 subquery = (Query *) copyObject(orig_subquery);
350 * If it's an EXISTS subplan, we might be able to simplify it.
352 if (subLinkType == EXISTS_SUBLINK)
353 simple_exists = simplify_EXISTS_query(subquery);
356 * For an EXISTS subplan, tell lower-level planner to expect that only the
357 * first tuple will be retrieved. For ALL and ANY subplans, we will be
358 * able to stop evaluating if the test condition fails or matches, so very
359 * often not all the tuples will be retrieved; for lack of a better idea,
360 * specify 50% retrieval. For EXPR and ROWCOMPARE subplans, use default
361 * behavior (we're only expecting one row out, anyway).
363 * NOTE: if you change these numbers, also change cost_subplan() in
366 * XXX If an ANY subplan is uncorrelated, build_subplan may decide to hash
367 * its output. In that case it would've been better to specify full
368 * retrieval. At present, however, we can only check hashability after
369 * we've made the subplan :-(. (Determining whether it'll fit in work_mem
370 * is the really hard part.) Therefore, we don't want to be too
371 * optimistic about the percentage of tuples retrieved, for fear of
372 * selecting a plan that's bad for the materialization case.
374 if (subLinkType == EXISTS_SUBLINK)
375 tuple_fraction = 1.0; /* just like a LIMIT 1 */
376 else if (subLinkType == ALL_SUBLINK ||
377 subLinkType == ANY_SUBLINK)
378 tuple_fraction = 0.5; /* 50% */
380 tuple_fraction = 0.0; /* default behavior */
383 * Generate the plan for the subquery.
385 plan = subquery_planner(root->glob, subquery,
387 false, tuple_fraction,
390 /* And convert to SubPlan or InitPlan format. */
391 result = build_subplan(root, plan, subroot,
392 subLinkType, testexpr, true, isTopQual);
395 * If it's a correlated EXISTS with an unimportant targetlist, we might be
396 * able to transform it to the equivalent of an IN and then implement it
397 * by hashing. We don't have enough information yet to tell which way is
398 * likely to be better (it depends on the expected number of executions of
399 * the EXISTS qual, and we are much too early in planning the outer query
400 * to be able to guess that). So we generate both plans, if possible, and
401 * leave it to the executor to decide which to use.
403 if (simple_exists && IsA(result, SubPlan))
408 /* Make a second copy of the original subquery */
409 subquery = (Query *) copyObject(orig_subquery);
410 /* and re-simplify */
411 simple_exists = simplify_EXISTS_query(subquery);
412 Assert(simple_exists);
413 /* See if it can be converted to an ANY query */
414 subquery = convert_EXISTS_to_ANY(root, subquery,
415 &newtestexpr, ¶mIds);
418 /* Generate the plan for the ANY subquery; we'll need all rows */
419 plan = subquery_planner(root->glob, subquery,
424 /* Now we can check if it'll fit in work_mem */
425 if (subplan_is_hashable(plan))
428 AlternativeSubPlan *asplan;
430 /* OK, convert to SubPlan format. */
431 hashplan = (SubPlan *) build_subplan(root, plan, subroot,
432 ANY_SUBLINK, newtestexpr,
434 /* Check we got what we expected */
435 Assert(IsA(hashplan, SubPlan));
436 Assert(hashplan->parParam == NIL);
437 Assert(hashplan->useHashTable);
438 /* build_subplan won't have filled in paramIds */
439 hashplan->paramIds = paramIds;
441 /* Leave it to the executor to decide which plan to use */
442 asplan = makeNode(AlternativeSubPlan);
443 asplan->subplans = list_make2(result, hashplan);
444 result = (Node *) asplan;
453 * Build a SubPlan node given the raw inputs --- subroutine for make_subplan
455 * Returns either the SubPlan, or an expression using initplan output Params,
456 * as explained in the comments for make_subplan.
459 build_subplan(PlannerInfo *root, Plan *plan, PlannerInfo *subroot,
460 SubLinkType subLinkType, Node *testexpr,
461 bool adjust_testexpr, bool unknownEqFalse)
470 * Initialize the SubPlan node. Note plan_id, plan_name, and cost fields
471 * are set further down.
473 splan = makeNode(SubPlan);
474 splan->subLinkType = subLinkType;
475 splan->testexpr = NULL;
476 splan->paramIds = NIL;
477 get_first_col_type(plan, &splan->firstColType, &splan->firstColTypmod,
478 &splan->firstColCollation);
479 splan->useHashTable = false;
480 splan->unknownEqFalse = unknownEqFalse;
481 splan->setParam = NIL;
482 splan->parParam = NIL;
486 * Make parParam and args lists of param IDs and expressions that current
487 * query level will pass to this child plan.
489 tmpset = bms_copy(plan->extParam);
490 while ((paramid = bms_first_member(tmpset)) >= 0)
492 PlannerParamItem *pitem = list_nth(root->glob->paramlist, paramid);
494 if (pitem->abslevel == root->query_level)
499 * The Var or Aggref has already been adjusted to have the correct
500 * varlevelsup or agglevelsup. We probably don't even need to
501 * copy it again, but be safe.
503 arg = copyObject(pitem->item);
506 * If it's an Aggref, its arguments might contain SubLinks, which
507 * have not yet been processed. Do that now.
509 if (IsA(arg, Aggref))
510 arg = SS_process_sublinks(root, arg, false);
512 splan->parParam = lappend_int(splan->parParam, paramid);
513 splan->args = lappend(splan->args, arg);
519 * Un-correlated or undirect correlated plans of EXISTS, EXPR, ARRAY, or
520 * ROWCOMPARE types can be used as initPlans. For EXISTS, EXPR, or ARRAY,
521 * we just produce a Param referring to the result of evaluating the
522 * initPlan. For ROWCOMPARE, we must modify the testexpr tree to contain
523 * PARAM_EXEC Params instead of the PARAM_SUBLINK Params emitted by the
526 if (splan->parParam == NIL && subLinkType == EXISTS_SUBLINK)
530 Assert(testexpr == NULL);
531 prm = generate_new_param(root, BOOLOID, -1, InvalidOid);
532 splan->setParam = list_make1_int(prm->paramid);
534 result = (Node *) prm;
536 else if (splan->parParam == NIL && subLinkType == EXPR_SUBLINK)
538 TargetEntry *te = linitial(plan->targetlist);
541 Assert(!te->resjunk);
542 Assert(testexpr == NULL);
543 prm = generate_new_param(root,
544 exprType((Node *) te->expr),
545 exprTypmod((Node *) te->expr),
546 exprCollation((Node *) te->expr));
547 splan->setParam = list_make1_int(prm->paramid);
549 result = (Node *) prm;
551 else if (splan->parParam == NIL && subLinkType == ARRAY_SUBLINK)
553 TargetEntry *te = linitial(plan->targetlist);
557 Assert(!te->resjunk);
558 Assert(testexpr == NULL);
559 arraytype = get_array_type(exprType((Node *) te->expr));
560 if (!OidIsValid(arraytype))
561 elog(ERROR, "could not find array type for datatype %s",
562 format_type_be(exprType((Node *) te->expr)));
563 prm = generate_new_param(root,
565 exprTypmod((Node *) te->expr),
566 exprCollation((Node *) te->expr));
567 splan->setParam = list_make1_int(prm->paramid);
569 result = (Node *) prm;
571 else if (splan->parParam == NIL && subLinkType == ROWCOMPARE_SUBLINK)
573 /* Adjust the Params */
576 Assert(testexpr != NULL);
577 params = generate_subquery_params(root,
580 result = convert_testexpr(root,
583 splan->setParam = list_copy(splan->paramIds);
587 * The executable expression is returned to become part of the outer
588 * plan's expression tree; it is not kept in the initplan node.
594 * Adjust the Params in the testexpr, unless caller said it's not
597 if (testexpr && adjust_testexpr)
601 params = generate_subquery_params(root,
604 splan->testexpr = convert_testexpr(root,
609 splan->testexpr = testexpr;
612 * We can't convert subplans of ALL_SUBLINK or ANY_SUBLINK types to
613 * initPlans, even when they are uncorrelated or undirect correlated,
614 * because we need to scan the output of the subplan for each outer
615 * tuple. But if it's a not-direct-correlated IN (= ANY) test, we
616 * might be able to use a hashtable to avoid comparing all the tuples.
618 if (subLinkType == ANY_SUBLINK &&
619 splan->parParam == NIL &&
620 subplan_is_hashable(plan) &&
621 testexpr_is_hashable(splan->testexpr))
622 splan->useHashTable = true;
625 * Otherwise, we have the option to tack a Material node onto the top
626 * of the subplan, to reduce the cost of reading it repeatedly. This
627 * is pointless for a direct-correlated subplan, since we'd have to
628 * recompute its results each time anyway. For uncorrelated/undirect
629 * correlated subplans, we add Material unless the subplan's top plan
630 * node would materialize its output anyway. Also, if enable_material
631 * is false, then the user does not want us to materialize anything
632 * unnecessarily, so we don't.
634 else if (splan->parParam == NIL && enable_material &&
635 !ExecMaterializesOutput(nodeTag(plan)))
636 plan = materialize_finished_plan(plan);
638 result = (Node *) splan;
643 * Add the subplan and its PlannerInfo to the global lists.
645 root->glob->subplans = lappend(root->glob->subplans, plan);
646 root->glob->subroots = lappend(root->glob->subroots, subroot);
647 splan->plan_id = list_length(root->glob->subplans);
650 root->init_plans = lappend(root->init_plans, splan);
653 * A parameterless subplan (not initplan) should be prepared to handle
654 * REWIND efficiently. If it has direct parameters then there's no point
655 * since it'll be reset on each scan anyway; and if it's an initplan then
656 * there's no point since it won't get re-run without parameter changes
657 * anyway. The input of a hashed subplan doesn't need REWIND either.
659 if (splan->parParam == NIL && !isInitPlan && !splan->useHashTable)
660 root->glob->rewindPlanIDs = bms_add_member(root->glob->rewindPlanIDs,
663 /* Label the subplan for EXPLAIN purposes */
669 splan->plan_name = palloc(32 + 12 * list_length(splan->setParam));
670 sprintf(splan->plan_name, "InitPlan %d (returns ", splan->plan_id);
671 offset = strlen(splan->plan_name);
672 foreach(lc, splan->setParam)
674 sprintf(splan->plan_name + offset, "$%d%s",
676 lnext(lc) ? "," : "");
677 offset += strlen(splan->plan_name + offset);
679 sprintf(splan->plan_name + offset, ")");
683 splan->plan_name = palloc(32);
684 sprintf(splan->plan_name, "SubPlan %d", splan->plan_id);
687 /* Lastly, fill in the cost estimates for use later */
688 cost_subplan(root, splan, plan);
694 * generate_subquery_params: build a list of Params representing the output
695 * columns of a sublink's sub-select, given the sub-select's targetlist.
697 * We also return an integer list of the paramids of the Params.
700 generate_subquery_params(PlannerInfo *root, List *tlist, List **paramIds)
709 TargetEntry *tent = (TargetEntry *) lfirst(lc);
715 param = generate_new_param(root,
716 exprType((Node *) tent->expr),
717 exprTypmod((Node *) tent->expr),
718 exprCollation((Node *) tent->expr));
719 result = lappend(result, param);
720 ids = lappend_int(ids, param->paramid);
728 * generate_subquery_vars: build a list of Vars representing the output
729 * columns of a sublink's sub-select, given the sub-select's targetlist.
730 * The Vars have the specified varno (RTE index).
733 generate_subquery_vars(PlannerInfo *root, List *tlist, Index varno)
741 TargetEntry *tent = (TargetEntry *) lfirst(lc);
747 var = makeVarFromTargetEntry(varno, tent);
748 result = lappend(result, var);
755 * convert_testexpr: convert the testexpr given by the parser into
756 * actually executable form. This entails replacing PARAM_SUBLINK Params
757 * with Params or Vars representing the results of the sub-select. The
758 * nodes to be substituted are passed in as the List result from
759 * generate_subquery_params or generate_subquery_vars.
761 * The given testexpr has already been recursively processed by
762 * process_sublinks_mutator. Hence it can no longer contain any
763 * PARAM_SUBLINK Params for lower SubLink nodes; we can safely assume that
764 * any we find are for our own level of SubLink.
767 convert_testexpr(PlannerInfo *root,
771 convert_testexpr_context context;
774 context.subst_nodes = subst_nodes;
775 return convert_testexpr_mutator(testexpr, &context);
779 convert_testexpr_mutator(Node *node,
780 convert_testexpr_context *context)
784 if (IsA(node, Param))
786 Param *param = (Param *) node;
788 if (param->paramkind == PARAM_SUBLINK)
790 if (param->paramid <= 0 ||
791 param->paramid > list_length(context->subst_nodes))
792 elog(ERROR, "unexpected PARAM_SUBLINK ID: %d", param->paramid);
795 * We copy the list item to avoid having doubly-linked
796 * substructure in the modified parse tree. This is probably
797 * unnecessary when it's a Param, but be safe.
799 return (Node *) copyObject(list_nth(context->subst_nodes,
800 param->paramid - 1));
803 return expression_tree_mutator(node,
804 convert_testexpr_mutator,
809 * subplan_is_hashable: can we implement an ANY subplan by hashing?
812 subplan_is_hashable(Plan *plan)
814 double subquery_size;
817 * The estimated size of the subquery result must fit in work_mem. (Note:
818 * we use sizeof(HeapTupleHeaderData) here even though the tuples will
819 * actually be stored as MinimalTuples; this provides some fudge factor
820 * for hashtable overhead.)
822 subquery_size = plan->plan_rows *
823 (MAXALIGN(plan->plan_width) + MAXALIGN(sizeof(HeapTupleHeaderData)));
824 if (subquery_size > work_mem * 1024L)
831 * testexpr_is_hashable: is an ANY SubLink's test expression hashable?
834 testexpr_is_hashable(Node *testexpr)
837 * The testexpr must be a single OpExpr, or an AND-clause containing only
840 * The combining operators must be hashable and strict. The need for
841 * hashability is obvious, since we want to use hashing. Without
842 * strictness, behavior in the presence of nulls is too unpredictable. We
843 * actually must assume even more than plain strictness: they can't yield
844 * NULL for non-null inputs, either (see nodeSubplan.c). However, hash
845 * indexes and hash joins assume that too.
847 if (testexpr && IsA(testexpr, OpExpr))
849 if (hash_ok_operator((OpExpr *) testexpr))
852 else if (and_clause(testexpr))
856 foreach(l, ((BoolExpr *) testexpr)->args)
858 Node *andarg = (Node *) lfirst(l);
860 if (!IsA(andarg, OpExpr))
862 if (!hash_ok_operator((OpExpr *) andarg))
872 * Check expression is hashable + strict
874 * We could use op_hashjoinable() and op_strict(), but do it like this to
875 * avoid a redundant cache lookup.
878 hash_ok_operator(OpExpr *expr)
880 Oid opid = expr->opno;
882 /* quick out if not a binary operator */
883 if (list_length(expr->args) != 2)
885 if (opid == ARRAY_EQ_OP)
887 /* array_eq is strict, but must check input type to ensure hashable */
888 /* XXX record_eq will need same treatment when it becomes hashable */
889 Node *leftarg = linitial(expr->args);
891 return op_hashjoinable(opid, exprType(leftarg));
895 /* else must look up the operator properties */
897 Form_pg_operator optup;
899 tup = SearchSysCache1(OPEROID, ObjectIdGetDatum(opid));
900 if (!HeapTupleIsValid(tup))
901 elog(ERROR, "cache lookup failed for operator %u", opid);
902 optup = (Form_pg_operator) GETSTRUCT(tup);
903 if (!optup->oprcanhash || !func_strict(optup->oprcode))
905 ReleaseSysCache(tup);
908 ReleaseSysCache(tup);
915 * SS_process_ctes: process a query's WITH list
917 * We plan each interesting WITH item and convert it to an initplan.
918 * A side effect is to fill in root->cte_plan_ids with a list that
919 * parallels root->parse->cteList and provides the subplan ID for
920 * each CTE's initplan.
923 SS_process_ctes(PlannerInfo *root)
927 Assert(root->cte_plan_ids == NIL);
929 foreach(lc, root->parse->cteList)
931 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
932 CmdType cmdType = ((Query *) cte->ctequery)->commandType;
935 PlannerInfo *subroot;
942 * Ignore SELECT CTEs that are not actually referenced anywhere.
944 if (cte->cterefcount == 0 && cmdType == CMD_SELECT)
946 /* Make a dummy entry in cte_plan_ids */
947 root->cte_plan_ids = lappend_int(root->cte_plan_ids, -1);
952 * Copy the source Query node. Probably not necessary, but let's keep
953 * this similar to make_subplan.
955 subquery = (Query *) copyObject(cte->ctequery);
958 * Generate the plan for the CTE query. Always plan for full
959 * retrieval --- we don't have enough info to predict otherwise.
961 plan = subquery_planner(root->glob, subquery,
963 cte->cterecursive, 0.0,
967 * Make a SubPlan node for it. This is just enough unlike
968 * build_subplan that we can't share code.
970 * Note plan_id, plan_name, and cost fields are set further down.
972 splan = makeNode(SubPlan);
973 splan->subLinkType = CTE_SUBLINK;
974 splan->testexpr = NULL;
975 splan->paramIds = NIL;
976 get_first_col_type(plan, &splan->firstColType, &splan->firstColTypmod,
977 &splan->firstColCollation);
978 splan->useHashTable = false;
979 splan->unknownEqFalse = false;
980 splan->setParam = NIL;
981 splan->parParam = NIL;
985 * Make parParam and args lists of param IDs and expressions that
986 * current query level will pass to this child plan. Even though this
987 * is an initplan, there could be side-references to earlier
988 * initplan's outputs, specifically their CTE output parameters.
990 tmpset = bms_copy(plan->extParam);
991 while ((paramid = bms_first_member(tmpset)) >= 0)
993 PlannerParamItem *pitem = list_nth(root->glob->paramlist, paramid);
995 if (pitem->abslevel == root->query_level)
997 prm = (Param *) pitem->item;
998 if (!IsA(prm, Param) ||
999 prm->paramtype != INTERNALOID)
1000 elog(ERROR, "bogus local parameter passed to WITH query");
1002 splan->parParam = lappend_int(splan->parParam, paramid);
1003 splan->args = lappend(splan->args, copyObject(prm));
1009 * Assign a param to represent the query output. We only really care
1010 * about reserving a parameter ID number.
1012 prm = generate_new_param(root, INTERNALOID, -1, InvalidOid);
1013 splan->setParam = list_make1_int(prm->paramid);
1016 * Add the subplan and its PlannerInfo to the global lists.
1018 root->glob->subplans = lappend(root->glob->subplans, plan);
1019 root->glob->subroots = lappend(root->glob->subroots, subroot);
1020 splan->plan_id = list_length(root->glob->subplans);
1022 root->init_plans = lappend(root->init_plans, splan);
1024 root->cte_plan_ids = lappend_int(root->cte_plan_ids, splan->plan_id);
1026 /* Label the subplan for EXPLAIN purposes */
1027 splan->plan_name = palloc(4 + strlen(cte->ctename) + 1);
1028 sprintf(splan->plan_name, "CTE %s", cte->ctename);
1030 /* Lastly, fill in the cost estimates for use later */
1031 cost_subplan(root, splan, plan);
1036 * convert_ANY_sublink_to_join: try to convert an ANY SubLink to a join
1038 * The caller has found an ANY SubLink at the top level of one of the query's
1039 * qual clauses, but has not checked the properties of the SubLink further.
1040 * Decide whether it is appropriate to process this SubLink in join style.
1041 * If so, form a JoinExpr and return it. Return NULL if the SubLink cannot
1042 * be converted to a join.
1044 * The only non-obvious input parameter is available_rels: this is the set
1045 * of query rels that can safely be referenced in the sublink expression.
1046 * (We must restrict this to avoid changing the semantics when a sublink
1047 * is present in an outer join's ON qual.) The conversion must fail if
1048 * the converted qual would reference any but these parent-query relids.
1050 * On success, the returned JoinExpr has larg = NULL and rarg = the jointree
1051 * item representing the pulled-up subquery. The caller must set larg to
1052 * represent the relation(s) on the lefthand side of the new join, and insert
1053 * the JoinExpr into the upper query's jointree at an appropriate place
1054 * (typically, where the lefthand relation(s) had been). Note that the
1055 * passed-in SubLink must also be removed from its original position in the
1056 * query quals, since the quals of the returned JoinExpr replace it.
1057 * (Notionally, we replace the SubLink with a constant TRUE, then elide the
1058 * redundant constant from the qual.)
1060 * On success, the caller is also responsible for recursively applying
1061 * pull_up_sublinks processing to the rarg and quals of the returned JoinExpr.
1062 * (On failure, there is no need to do anything, since pull_up_sublinks will
1063 * be applied when we recursively plan the sub-select.)
1065 * Side effects of a successful conversion include adding the SubLink's
1066 * subselect to the query's rangetable, so that it can be referenced in
1067 * the JoinExpr's rarg.
1070 convert_ANY_sublink_to_join(PlannerInfo *root, SubLink *sublink,
1071 Relids available_rels)
1074 Query *parse = root->parse;
1075 Query *subselect = (Query *) sublink->subselect;
1076 Relids upper_varnos;
1080 List *subquery_vars;
1083 Assert(sublink->subLinkType == ANY_SUBLINK);
1086 * The sub-select must not refer to any Vars of the parent query. (Vars of
1087 * higher levels should be okay, though.)
1089 if (contain_vars_of_level((Node *) subselect, 1))
1093 * The test expression must contain some Vars of the parent query, else
1094 * it's not gonna be a join. (Note that it won't have Vars referring to
1095 * the subquery, rather Params.)
1097 upper_varnos = pull_varnos(sublink->testexpr);
1098 if (bms_is_empty(upper_varnos))
1102 * However, it can't refer to anything outside available_rels.
1104 if (!bms_is_subset(upper_varnos, available_rels))
1108 * The combining operators and left-hand expressions mustn't be volatile.
1110 if (contain_volatile_functions(sublink->testexpr))
1114 * Okay, pull up the sub-select into upper range table.
1116 * We rely here on the assumption that the outer query has no references
1117 * to the inner (necessarily true, other than the Vars that we build
1118 * below). Therefore this is a lot easier than what pull_up_subqueries has
1121 rte = addRangeTableEntryForSubquery(NULL,
1123 makeAlias("ANY_subquery", NIL),
1125 parse->rtable = lappend(parse->rtable, rte);
1126 rtindex = list_length(parse->rtable);
1129 * Form a RangeTblRef for the pulled-up sub-select.
1131 rtr = makeNode(RangeTblRef);
1132 rtr->rtindex = rtindex;
1135 * Build a list of Vars representing the subselect outputs.
1137 subquery_vars = generate_subquery_vars(root,
1138 subselect->targetList,
1142 * Build the new join's qual expression, replacing Params with these Vars.
1144 quals = convert_testexpr(root, sublink->testexpr, subquery_vars);
1147 * And finally, build the JoinExpr node.
1149 result = makeNode(JoinExpr);
1150 result->jointype = JOIN_SEMI;
1151 result->isNatural = false;
1152 result->larg = NULL; /* caller must fill this in */
1153 result->rarg = (Node *) rtr;
1154 result->usingClause = NIL;
1155 result->quals = quals;
1156 result->alias = NULL;
1157 result->rtindex = 0; /* we don't need an RTE for it */
1163 * convert_EXISTS_sublink_to_join: try to convert an EXISTS SubLink to a join
1165 * The API of this function is identical to convert_ANY_sublink_to_join's,
1166 * except that we also support the case where the caller has found NOT EXISTS,
1167 * so we need an additional input parameter "under_not".
1170 convert_EXISTS_sublink_to_join(PlannerInfo *root, SubLink *sublink,
1171 bool under_not, Relids available_rels)
1174 Query *parse = root->parse;
1175 Query *subselect = (Query *) sublink->subselect;
1179 Relids clause_varnos;
1180 Relids upper_varnos;
1182 Assert(sublink->subLinkType == EXISTS_SUBLINK);
1185 * Can't flatten if it contains WITH. (We could arrange to pull up the
1186 * WITH into the parent query's cteList, but that risks changing the
1187 * semantics, since a WITH ought to be executed once per associated query
1188 * call.) Note that convert_ANY_sublink_to_join doesn't have to reject
1189 * this case, since it just produces a subquery RTE that doesn't have to
1190 * get flattened into the parent query.
1192 if (subselect->cteList)
1196 * Copy the subquery so we can modify it safely (see comments in
1199 subselect = (Query *) copyObject(subselect);
1202 * See if the subquery can be simplified based on the knowledge that it's
1203 * being used in EXISTS(). If we aren't able to get rid of its
1204 * targetlist, we have to fail, because the pullup operation leaves us
1205 * with noplace to evaluate the targetlist.
1207 if (!simplify_EXISTS_query(subselect))
1211 * The subquery must have a nonempty jointree, else we won't have a join.
1213 if (subselect->jointree->fromlist == NIL)
1217 * Separate out the WHERE clause. (We could theoretically also remove
1218 * top-level plain JOIN/ON clauses, but it's probably not worth the
1221 whereClause = subselect->jointree->quals;
1222 subselect->jointree->quals = NULL;
1225 * The rest of the sub-select must not refer to any Vars of the parent
1226 * query. (Vars of higher levels should be okay, though.)
1228 if (contain_vars_of_level((Node *) subselect, 1))
1232 * On the other hand, the WHERE clause must contain some Vars of the
1233 * parent query, else it's not gonna be a join.
1235 if (!contain_vars_of_level(whereClause, 1))
1239 * We don't risk optimizing if the WHERE clause is volatile, either.
1241 if (contain_volatile_functions(whereClause))
1245 * Prepare to pull up the sub-select into top range table.
1247 * We rely here on the assumption that the outer query has no references
1248 * to the inner (necessarily true). Therefore this is a lot easier than
1249 * what pull_up_subqueries has to go through.
1251 * In fact, it's even easier than what convert_ANY_sublink_to_join has to
1252 * do. The machinations of simplify_EXISTS_query ensured that there is
1253 * nothing interesting in the subquery except an rtable and jointree, and
1254 * even the jointree FromExpr no longer has quals. So we can just append
1255 * the rtable to our own and use the FromExpr in our jointree. But first,
1256 * adjust all level-zero varnos in the subquery to account for the rtable
1259 rtoffset = list_length(parse->rtable);
1260 OffsetVarNodes((Node *) subselect, rtoffset, 0);
1261 OffsetVarNodes(whereClause, rtoffset, 0);
1264 * Upper-level vars in subquery will now be one level closer to their
1265 * parent than before; in particular, anything that had been level 1
1266 * becomes level zero.
1268 IncrementVarSublevelsUp((Node *) subselect, -1, 1);
1269 IncrementVarSublevelsUp(whereClause, -1, 1);
1272 * Now that the WHERE clause is adjusted to match the parent query
1273 * environment, we can easily identify all the level-zero rels it uses.
1274 * The ones <= rtoffset belong to the upper query; the ones > rtoffset do
1277 clause_varnos = pull_varnos(whereClause);
1278 upper_varnos = NULL;
1279 while ((varno = bms_first_member(clause_varnos)) >= 0)
1281 if (varno <= rtoffset)
1282 upper_varnos = bms_add_member(upper_varnos, varno);
1284 bms_free(clause_varnos);
1285 Assert(!bms_is_empty(upper_varnos));
1288 * Now that we've got the set of upper-level varnos, we can make the last
1289 * check: only available_rels can be referenced.
1291 if (!bms_is_subset(upper_varnos, available_rels))
1294 /* Now we can attach the modified subquery rtable to the parent */
1295 parse->rtable = list_concat(parse->rtable, subselect->rtable);
1298 * And finally, build the JoinExpr node.
1300 result = makeNode(JoinExpr);
1301 result->jointype = under_not ? JOIN_ANTI : JOIN_SEMI;
1302 result->isNatural = false;
1303 result->larg = NULL; /* caller must fill this in */
1304 /* flatten out the FromExpr node if it's useless */
1305 if (list_length(subselect->jointree->fromlist) == 1)
1306 result->rarg = (Node *) linitial(subselect->jointree->fromlist);
1308 result->rarg = (Node *) subselect->jointree;
1309 result->usingClause = NIL;
1310 result->quals = whereClause;
1311 result->alias = NULL;
1312 result->rtindex = 0; /* we don't need an RTE for it */
1318 * simplify_EXISTS_query: remove any useless stuff in an EXISTS's subquery
1320 * The only thing that matters about an EXISTS query is whether it returns
1321 * zero or more than zero rows. Therefore, we can remove certain SQL features
1322 * that won't affect that. The only part that is really likely to matter in
1323 * typical usage is simplifying the targetlist: it's a common habit to write
1324 * "SELECT * FROM" even though there is no need to evaluate any columns.
1326 * Note: by suppressing the targetlist we could cause an observable behavioral
1327 * change, namely that any errors that might occur in evaluating the tlist
1328 * won't occur, nor will other side-effects of volatile functions. This seems
1329 * unlikely to bother anyone in practice.
1331 * Returns TRUE if was able to discard the targetlist, else FALSE.
1334 simplify_EXISTS_query(Query *query)
1337 * We don't try to simplify at all if the query uses set operations,
1338 * aggregates, modifying CTEs, HAVING, LIMIT/OFFSET, or FOR UPDATE/SHARE;
1339 * none of these seem likely in normal usage and their possible effects
1342 if (query->commandType != CMD_SELECT ||
1343 query->intoClause ||
1344 query->setOperations ||
1346 query->hasWindowFuncs ||
1347 query->hasModifyingCTE ||
1348 query->havingQual ||
1349 query->limitOffset ||
1350 query->limitCount ||
1355 * Mustn't throw away the targetlist if it contains set-returning
1356 * functions; those could affect whether zero rows are returned!
1358 if (expression_returns_set((Node *) query->targetList))
1362 * Otherwise, we can throw away the targetlist, as well as any GROUP,
1363 * WINDOW, DISTINCT, and ORDER BY clauses; none of those clauses will
1364 * change a nonzero-rows result to zero rows or vice versa. (Furthermore,
1365 * since our parsetree representation of these clauses depends on the
1366 * targetlist, we'd better throw them away if we drop the targetlist.)
1368 query->targetList = NIL;
1369 query->groupClause = NIL;
1370 query->windowClause = NIL;
1371 query->distinctClause = NIL;
1372 query->sortClause = NIL;
1373 query->hasDistinctOn = false;
1379 * convert_EXISTS_to_ANY: try to convert EXISTS to a hashable ANY sublink
1381 * The subselect is expected to be a fresh copy that we can munge up,
1382 * and to have been successfully passed through simplify_EXISTS_query.
1384 * On success, the modified subselect is returned, and we store a suitable
1385 * upper-level test expression at *testexpr, plus a list of the subselect's
1386 * output Params at *paramIds. (The test expression is already Param-ified
1387 * and hence need not go through convert_testexpr, which is why we have to
1388 * deal with the Param IDs specially.)
1390 * On failure, returns NULL.
1393 convert_EXISTS_to_ANY(PlannerInfo *root, Query *subselect,
1394 Node **testexpr, List **paramIds)
1412 * Query must not require a targetlist, since we have to insert a new one.
1413 * Caller should have dealt with the case already.
1415 Assert(subselect->targetList == NIL);
1418 * Separate out the WHERE clause. (We could theoretically also remove
1419 * top-level plain JOIN/ON clauses, but it's probably not worth the
1422 whereClause = subselect->jointree->quals;
1423 subselect->jointree->quals = NULL;
1426 * The rest of the sub-select must not refer to any Vars of the parent
1427 * query. (Vars of higher levels should be okay, though.)
1429 * Note: we need not check for Aggrefs separately because we know the
1430 * sub-select is as yet unoptimized; any uplevel Aggref must therefore
1431 * contain an uplevel Var reference. This is not the case below ...
1433 if (contain_vars_of_level((Node *) subselect, 1))
1437 * We don't risk optimizing if the WHERE clause is volatile, either.
1439 if (contain_volatile_functions(whereClause))
1443 * Clean up the WHERE clause by doing const-simplification etc on it.
1444 * Aside from simplifying the processing we're about to do, this is
1445 * important for being able to pull chunks of the WHERE clause up into the
1446 * parent query. Since we are invoked partway through the parent's
1447 * preprocess_expression() work, earlier steps of preprocess_expression()
1448 * wouldn't get applied to the pulled-up stuff unless we do them here. For
1449 * the parts of the WHERE clause that get put back into the child query,
1450 * this work is partially duplicative, but it shouldn't hurt.
1452 * Note: we do not run flatten_join_alias_vars. This is OK because any
1453 * parent aliases were flattened already, and we're not going to pull any
1454 * child Vars (of any description) into the parent.
1456 * Note: passing the parent's root to eval_const_expressions is
1457 * technically wrong, but we can get away with it since only the
1458 * boundParams (if any) are used, and those would be the same in a
1461 whereClause = eval_const_expressions(root, whereClause);
1462 whereClause = (Node *) canonicalize_qual((Expr *) whereClause);
1463 whereClause = (Node *) make_ands_implicit((Expr *) whereClause);
1466 * We now have a flattened implicit-AND list of clauses, which we try to
1467 * break apart into "outervar = innervar" hash clauses. Anything that
1468 * can't be broken apart just goes back into the newWhere list. Note that
1469 * we aren't trying hard yet to ensure that we have only outer or only
1470 * inner on each side; we'll check that if we get to the end.
1472 leftargs = rightargs = opids = opcollations = newWhere = NIL;
1473 foreach(lc, (List *) whereClause)
1475 OpExpr *expr = (OpExpr *) lfirst(lc);
1477 if (IsA(expr, OpExpr) &&
1478 hash_ok_operator(expr))
1480 Node *leftarg = (Node *) linitial(expr->args);
1481 Node *rightarg = (Node *) lsecond(expr->args);
1483 if (contain_vars_of_level(leftarg, 1))
1485 leftargs = lappend(leftargs, leftarg);
1486 rightargs = lappend(rightargs, rightarg);
1487 opids = lappend_oid(opids, expr->opno);
1488 opcollations = lappend_oid(opcollations, expr->inputcollid);
1491 if (contain_vars_of_level(rightarg, 1))
1494 * We must commute the clause to put the outer var on the
1495 * left, because the hashing code in nodeSubplan.c expects
1496 * that. This probably shouldn't ever fail, since hashable
1497 * operators ought to have commutators, but be paranoid.
1499 expr->opno = get_commutator(expr->opno);
1500 if (OidIsValid(expr->opno) && hash_ok_operator(expr))
1502 leftargs = lappend(leftargs, rightarg);
1503 rightargs = lappend(rightargs, leftarg);
1504 opids = lappend_oid(opids, expr->opno);
1505 opcollations = lappend_oid(opcollations, expr->inputcollid);
1508 /* If no commutator, no chance to optimize the WHERE clause */
1512 /* Couldn't handle it as a hash clause */
1513 newWhere = lappend(newWhere, expr);
1517 * If we didn't find anything we could convert, fail.
1519 if (leftargs == NIL)
1523 * There mustn't be any parent Vars or Aggs in the stuff that we intend to
1524 * put back into the child query. Note: you might think we don't need to
1525 * check for Aggs separately, because an uplevel Agg must contain an
1526 * uplevel Var in its argument. But it is possible that the uplevel Var
1527 * got optimized away by eval_const_expressions. Consider
1529 * SUM(CASE WHEN false THEN uplevelvar ELSE 0 END)
1531 if (contain_vars_of_level((Node *) newWhere, 1) ||
1532 contain_vars_of_level((Node *) rightargs, 1))
1534 if (root->parse->hasAggs &&
1535 (contain_aggs_of_level((Node *) newWhere, 1) ||
1536 contain_aggs_of_level((Node *) rightargs, 1)))
1540 * And there can't be any child Vars in the stuff we intend to pull up.
1541 * (Note: we'd need to check for child Aggs too, except we know the child
1542 * has no aggs at all because of simplify_EXISTS_query's check. The same
1543 * goes for window functions.)
1545 if (contain_vars_of_level((Node *) leftargs, 0))
1549 * Also reject sublinks in the stuff we intend to pull up. (It might be
1550 * possible to support this, but doesn't seem worth the complication.)
1552 if (contain_subplans((Node *) leftargs))
1556 * Okay, adjust the sublevelsup in the stuff we're pulling up.
1558 IncrementVarSublevelsUp((Node *) leftargs, -1, 1);
1561 * Put back any child-level-only WHERE clauses.
1564 subselect->jointree->quals = (Node *) make_ands_explicit(newWhere);
1567 * Build a new targetlist for the child that emits the expressions we
1568 * need. Concurrently, build a testexpr for the parent using Params to
1569 * reference the child outputs. (Since we generate Params directly here,
1570 * there will be no need to convert the testexpr in build_subplan.)
1572 tlist = testlist = paramids = NIL;
1574 /* there's no "forfour" so we have to chase one of the lists manually */
1575 cc = list_head(opcollations);
1576 forthree(lc, leftargs, rc, rightargs, oc, opids)
1578 Node *leftarg = (Node *) lfirst(lc);
1579 Node *rightarg = (Node *) lfirst(rc);
1580 Oid opid = lfirst_oid(oc);
1581 Oid opcollation = lfirst_oid(cc);
1585 param = generate_new_param(root,
1587 exprTypmod(rightarg),
1588 exprCollation(rightarg));
1589 tlist = lappend(tlist,
1590 makeTargetEntry((Expr *) rightarg,
1594 testlist = lappend(testlist,
1595 make_opclause(opid, BOOLOID, false,
1596 (Expr *) leftarg, (Expr *) param,
1597 InvalidOid, opcollation));
1598 paramids = lappend_int(paramids, param->paramid);
1601 /* Put everything where it should go, and we're done */
1602 subselect->targetList = tlist;
1603 *testexpr = (Node *) make_ands_explicit(testlist);
1604 *paramIds = paramids;
1611 * Replace correlation vars (uplevel vars) with Params.
1613 * Uplevel aggregates are replaced, too.
1615 * Note: it is critical that this runs immediately after SS_process_sublinks.
1616 * Since we do not recurse into the arguments of uplevel aggregates, they will
1617 * get copied to the appropriate subplan args list in the parent query with
1618 * uplevel vars not replaced by Params, but only adjusted in level (see
1619 * replace_outer_agg). That's exactly what we want for the vars of the parent
1620 * level --- but if an aggregate's argument contains any further-up variables,
1621 * they have to be replaced with Params in their turn. That will happen when
1622 * the parent level runs SS_replace_correlation_vars. Therefore it must do
1623 * so after expanding its sublinks to subplans. And we don't want any steps
1624 * in between, else those steps would never get applied to the aggregate
1625 * argument expressions, either in the parent or the child level.
1627 * Another fairly tricky thing going on here is the handling of SubLinks in
1628 * the arguments of uplevel aggregates. Those are not touched inside the
1629 * intermediate query level, either. Instead, SS_process_sublinks recurses
1630 * on them after copying the Aggref expression into the parent plan level
1631 * (this is actually taken care of in build_subplan).
1634 SS_replace_correlation_vars(PlannerInfo *root, Node *expr)
1636 /* No setup needed for tree walk, so away we go */
1637 return replace_correlation_vars_mutator(expr, root);
1641 replace_correlation_vars_mutator(Node *node, PlannerInfo *root)
1647 if (((Var *) node)->varlevelsup > 0)
1648 return (Node *) replace_outer_var(root, (Var *) node);
1650 if (IsA(node, Aggref))
1652 if (((Aggref *) node)->agglevelsup > 0)
1653 return (Node *) replace_outer_agg(root, (Aggref *) node);
1655 return expression_tree_mutator(node,
1656 replace_correlation_vars_mutator,
1661 * Expand SubLinks to SubPlans in the given expression.
1663 * The isQual argument tells whether or not this expression is a WHERE/HAVING
1664 * qualifier expression. If it is, any sublinks appearing at top level need
1665 * not distinguish FALSE from UNKNOWN return values.
1668 SS_process_sublinks(PlannerInfo *root, Node *expr, bool isQual)
1670 process_sublinks_context context;
1672 context.root = root;
1673 context.isTopQual = isQual;
1674 return process_sublinks_mutator(expr, &context);
1678 process_sublinks_mutator(Node *node, process_sublinks_context *context)
1680 process_sublinks_context locContext;
1682 locContext.root = context->root;
1686 if (IsA(node, SubLink))
1688 SubLink *sublink = (SubLink *) node;
1692 * First, recursively process the lefthand-side expressions, if any.
1693 * They're not top-level anymore.
1695 locContext.isTopQual = false;
1696 testexpr = process_sublinks_mutator(sublink->testexpr, &locContext);
1699 * Now build the SubPlan node and make the expr to return.
1701 return make_subplan(context->root,
1702 (Query *) sublink->subselect,
1703 sublink->subLinkType,
1705 context->isTopQual);
1709 * Don't recurse into the arguments of an outer aggregate here. Any
1710 * SubLinks in the arguments have to be dealt with at the outer query
1711 * level; they'll be handled when build_subplan collects the Aggref into
1712 * the arguments to be passed down to the current subplan.
1714 if (IsA(node, Aggref))
1716 if (((Aggref *) node)->agglevelsup > 0)
1721 * We should never see a SubPlan expression in the input (since this is
1722 * the very routine that creates 'em to begin with). We shouldn't find
1723 * ourselves invoked directly on a Query, either.
1725 Assert(!IsA(node, SubPlan));
1726 Assert(!IsA(node, AlternativeSubPlan));
1727 Assert(!IsA(node, Query));
1730 * Because make_subplan() could return an AND or OR clause, we have to
1731 * take steps to preserve AND/OR flatness of a qual. We assume the input
1732 * has been AND/OR flattened and so we need no recursion here.
1734 * (Due to the coding here, we will not get called on the List subnodes of
1735 * an AND; and the input is *not* yet in implicit-AND format. So no check
1736 * is needed for a bare List.)
1738 * Anywhere within the top-level AND/OR clause structure, we can tell
1739 * make_subplan() that NULL and FALSE are interchangeable. So isTopQual
1740 * propagates down in both cases. (Note that this is unlike the meaning
1741 * of "top level qual" used in most other places in Postgres.)
1743 if (and_clause(node))
1745 List *newargs = NIL;
1748 /* Still at qual top-level */
1749 locContext.isTopQual = context->isTopQual;
1751 foreach(l, ((BoolExpr *) node)->args)
1755 newarg = process_sublinks_mutator(lfirst(l), &locContext);
1756 if (and_clause(newarg))
1757 newargs = list_concat(newargs, ((BoolExpr *) newarg)->args);
1759 newargs = lappend(newargs, newarg);
1761 return (Node *) make_andclause(newargs);
1764 if (or_clause(node))
1766 List *newargs = NIL;
1769 /* Still at qual top-level */
1770 locContext.isTopQual = context->isTopQual;
1772 foreach(l, ((BoolExpr *) node)->args)
1776 newarg = process_sublinks_mutator(lfirst(l), &locContext);
1777 if (or_clause(newarg))
1778 newargs = list_concat(newargs, ((BoolExpr *) newarg)->args);
1780 newargs = lappend(newargs, newarg);
1782 return (Node *) make_orclause(newargs);
1786 * If we recurse down through anything other than an AND or OR node, we
1787 * are definitely not at top qual level anymore.
1789 locContext.isTopQual = false;
1791 return expression_tree_mutator(node,
1792 process_sublinks_mutator,
1793 (void *) &locContext);
1797 * SS_finalize_plan - do final sublink and parameter processing for a
1800 * This recursively computes the extParam and allParam sets for every Plan
1801 * node in the given plan tree. It also optionally attaches any previously
1802 * generated InitPlans to the top plan node. (Any InitPlans should already
1803 * have been put through SS_finalize_plan.)
1806 SS_finalize_plan(PlannerInfo *root, Plan *plan, bool attach_initplans)
1808 Bitmapset *valid_params,
1816 * Examine any initPlans to determine the set of external params they
1817 * reference, the set of output params they supply, and their total cost.
1818 * We'll use at least some of this info below. (Note we are assuming that
1819 * finalize_plan doesn't touch the initPlans.)
1821 * In the case where attach_initplans is false, we are assuming that the
1822 * existing initPlans are siblings that might supply params needed by the
1825 initExtParam = initSetParam = NULL;
1827 foreach(l, root->init_plans)
1829 SubPlan *initsubplan = (SubPlan *) lfirst(l);
1830 Plan *initplan = planner_subplan_get_plan(root, initsubplan);
1833 initExtParam = bms_add_members(initExtParam, initplan->extParam);
1834 foreach(l2, initsubplan->setParam)
1836 initSetParam = bms_add_member(initSetParam, lfirst_int(l2));
1838 initplan_cost += initsubplan->startup_cost + initsubplan->per_call_cost;
1842 * Now determine the set of params that are validly referenceable in this
1843 * query level; to wit, those available from outer query levels plus the
1844 * output parameters of any initPlans. (We do not include output
1845 * parameters of regular subplans. Those should only appear within the
1846 * testexpr of SubPlan nodes, and are taken care of locally within
1847 * finalize_primnode. Likewise, special parameters that are generated by
1848 * nodes such as ModifyTable are handled within finalize_plan.)
1850 * Note: this is a bit overly generous since some parameters of upper
1851 * query levels might belong to query subtrees that don't include this
1852 * query, or might be nestloop params that won't be passed down at all.
1853 * However, valid_params is only a debugging crosscheck, so it doesn't
1854 * seem worth expending lots of cycles to try to be exact.
1856 valid_params = bms_copy(initSetParam);
1858 foreach(l, root->glob->paramlist)
1860 PlannerParamItem *pitem = (PlannerParamItem *) lfirst(l);
1862 if (pitem->abslevel < root->query_level)
1864 /* valid outer-level parameter */
1865 valid_params = bms_add_member(valid_params, paramid);
1872 * Now recurse through plan tree.
1874 (void) finalize_plan(root, plan, valid_params, NULL);
1876 bms_free(valid_params);
1879 * Finally, attach any initPlans to the topmost plan node, and add their
1880 * extParams to the topmost node's, too. However, any setParams of the
1881 * initPlans should not be present in the topmost node's extParams, only
1882 * in its allParams. (As of PG 8.1, it's possible that some initPlans
1883 * have extParams that are setParams of other initPlans, so we have to
1884 * take care of this situation explicitly.)
1886 * We also add the eval cost of each initPlan to the startup cost of the
1887 * top node. This is a conservative overestimate, since in fact each
1888 * initPlan might be executed later than plan startup, or even not at all.
1890 if (attach_initplans)
1892 plan->initPlan = root->init_plans;
1893 root->init_plans = NIL; /* make sure they're not attached twice */
1895 /* allParam must include all these params */
1896 plan->allParam = bms_add_members(plan->allParam, initExtParam);
1897 plan->allParam = bms_add_members(plan->allParam, initSetParam);
1898 /* extParam must include any child extParam */
1899 plan->extParam = bms_add_members(plan->extParam, initExtParam);
1900 /* but extParam shouldn't include any setParams */
1901 plan->extParam = bms_del_members(plan->extParam, initSetParam);
1902 /* ensure extParam is exactly NULL if it's empty */
1903 if (bms_is_empty(plan->extParam))
1904 plan->extParam = NULL;
1906 plan->startup_cost += initplan_cost;
1907 plan->total_cost += initplan_cost;
1912 * Recursive processing of all nodes in the plan tree
1914 * valid_params is the set of param IDs considered valid to reference in
1915 * this plan node or its children.
1916 * scan_params is a set of param IDs to force scan plan nodes to reference.
1917 * This is for EvalPlanQual support, and is always NULL at the top of the
1920 * The return value is the computed allParam set for the given Plan node.
1921 * This is just an internal notational convenience.
1924 finalize_plan(PlannerInfo *root, Plan *plan, Bitmapset *valid_params,
1925 Bitmapset *scan_params)
1927 finalize_primnode_context context;
1928 int locally_added_param;
1929 Bitmapset *nestloop_params;
1930 Bitmapset *child_params;
1935 context.root = root;
1936 context.paramids = NULL; /* initialize set to empty */
1937 locally_added_param = -1; /* there isn't one */
1938 nestloop_params = NULL; /* there aren't any */
1941 * When we call finalize_primnode, context.paramids sets are automatically
1942 * merged together. But when recursing to self, we have to do it the hard
1943 * way. We want the paramids set to include params in subplans as well as
1947 /* Find params in targetlist and qual */
1948 finalize_primnode((Node *) plan->targetlist, &context);
1949 finalize_primnode((Node *) plan->qual, &context);
1951 /* Check additional node-type-specific fields */
1952 switch (nodeTag(plan))
1955 finalize_primnode(((Result *) plan)->resconstantqual,
1960 context.paramids = bms_add_members(context.paramids, scan_params);
1964 finalize_primnode((Node *) ((IndexScan *) plan)->indexqual,
1966 finalize_primnode((Node *) ((IndexScan *) plan)->indexorderby,
1970 * we need not look at indexqualorig, since it will have the same
1971 * param references as indexqual. Likewise, we can ignore
1974 context.paramids = bms_add_members(context.paramids, scan_params);
1977 case T_BitmapIndexScan:
1978 finalize_primnode((Node *) ((BitmapIndexScan *) plan)->indexqual,
1982 * we need not look at indexqualorig, since it will have the same
1983 * param references as indexqual.
1987 case T_BitmapHeapScan:
1988 finalize_primnode((Node *) ((BitmapHeapScan *) plan)->bitmapqualorig,
1990 context.paramids = bms_add_members(context.paramids, scan_params);
1994 finalize_primnode((Node *) ((TidScan *) plan)->tidquals,
1996 context.paramids = bms_add_members(context.paramids, scan_params);
1999 case T_SubqueryScan:
2002 * In a SubqueryScan, SS_finalize_plan has already been run on the
2003 * subplan by the inner invocation of subquery_planner, so there's
2004 * no need to do it again. Instead, just pull out the subplan's
2005 * extParams list, which represents the params it needs from my
2006 * level and higher levels.
2008 context.paramids = bms_add_members(context.paramids,
2009 ((SubqueryScan *) plan)->subplan->extParam);
2010 /* We need scan_params too, though */
2011 context.paramids = bms_add_members(context.paramids, scan_params);
2014 case T_FunctionScan:
2015 finalize_primnode(((FunctionScan *) plan)->funcexpr,
2017 context.paramids = bms_add_members(context.paramids, scan_params);
2021 finalize_primnode((Node *) ((ValuesScan *) plan)->values_lists,
2023 context.paramids = bms_add_members(context.paramids, scan_params);
2029 * You might think we should add the node's cteParam to
2030 * paramids, but we shouldn't because that param is just a
2031 * linkage mechanism for multiple CteScan nodes for the same
2032 * CTE; it is never used for changed-param signaling. What we
2033 * have to do instead is to find the referenced CTE plan and
2034 * incorporate its external paramids, so that the correct
2035 * things will happen if the CTE references outer-level
2036 * variables. See test cases for bug #4902.
2038 int plan_id = ((CteScan *) plan)->ctePlanId;
2041 /* so, do this ... */
2042 if (plan_id < 1 || plan_id > list_length(root->glob->subplans))
2043 elog(ERROR, "could not find plan for CteScan referencing plan ID %d",
2045 cteplan = (Plan *) list_nth(root->glob->subplans, plan_id - 1);
2047 bms_add_members(context.paramids, cteplan->extParam);
2050 /* ... but not this */
2052 bms_add_member(context.paramids,
2053 ((CteScan *) plan)->cteParam);
2056 context.paramids = bms_add_members(context.paramids,
2061 case T_WorkTableScan:
2063 bms_add_member(context.paramids,
2064 ((WorkTableScan *) plan)->wtParam);
2065 context.paramids = bms_add_members(context.paramids, scan_params);
2069 context.paramids = bms_add_members(context.paramids, scan_params);
2074 ModifyTable *mtplan = (ModifyTable *) plan;
2077 /* Force descendant scan nodes to reference epqParam */
2078 locally_added_param = mtplan->epqParam;
2079 valid_params = bms_add_member(bms_copy(valid_params),
2080 locally_added_param);
2081 scan_params = bms_add_member(bms_copy(scan_params),
2082 locally_added_param);
2083 finalize_primnode((Node *) mtplan->returningLists,
2085 foreach(l, mtplan->plans)
2088 bms_add_members(context.paramids,
2101 foreach(l, ((Append *) plan)->appendplans)
2104 bms_add_members(context.paramids,
2117 foreach(l, ((MergeAppend *) plan)->mergeplans)
2120 bms_add_members(context.paramids,
2133 foreach(l, ((BitmapAnd *) plan)->bitmapplans)
2136 bms_add_members(context.paramids,
2149 foreach(l, ((BitmapOr *) plan)->bitmapplans)
2152 bms_add_members(context.paramids,
2165 finalize_primnode((Node *) ((Join *) plan)->joinqual,
2167 /* collect set of params that will be passed to right child */
2168 foreach(l, ((NestLoop *) plan)->nestParams)
2170 NestLoopParam *nlp = (NestLoopParam *) lfirst(l);
2172 nestloop_params = bms_add_member(nestloop_params,
2179 finalize_primnode((Node *) ((Join *) plan)->joinqual,
2181 finalize_primnode((Node *) ((MergeJoin *) plan)->mergeclauses,
2186 finalize_primnode((Node *) ((Join *) plan)->joinqual,
2188 finalize_primnode((Node *) ((HashJoin *) plan)->hashclauses,
2193 finalize_primnode(((Limit *) plan)->limitOffset,
2195 finalize_primnode(((Limit *) plan)->limitCount,
2199 case T_RecursiveUnion:
2200 /* child nodes are allowed to reference wtParam */
2201 locally_added_param = ((RecursiveUnion *) plan)->wtParam;
2202 valid_params = bms_add_member(bms_copy(valid_params),
2203 locally_added_param);
2204 /* wtParam does *not* get added to scan_params */
2208 /* Force descendant scan nodes to reference epqParam */
2209 locally_added_param = ((LockRows *) plan)->epqParam;
2210 valid_params = bms_add_member(bms_copy(valid_params),
2211 locally_added_param);
2212 scan_params = bms_add_member(bms_copy(scan_params),
2213 locally_added_param);
2217 finalize_primnode(((WindowAgg *) plan)->startOffset,
2219 finalize_primnode(((WindowAgg *) plan)->endOffset,
2233 elog(ERROR, "unrecognized node type: %d",
2234 (int) nodeTag(plan));
2237 /* Process left and right child plans, if any */
2238 child_params = finalize_plan(root,
2242 context.paramids = bms_add_members(context.paramids, child_params);
2244 if (nestloop_params)
2246 /* right child can reference nestloop_params as well as valid_params */
2247 child_params = finalize_plan(root,
2249 bms_union(nestloop_params, valid_params),
2251 /* ... and they don't count as parameters used at my level */
2252 child_params = bms_difference(child_params, nestloop_params);
2253 bms_free(nestloop_params);
2258 child_params = finalize_plan(root,
2263 context.paramids = bms_add_members(context.paramids, child_params);
2266 * Any locally generated parameter doesn't count towards its generating
2267 * plan node's external dependencies. (Note: if we changed valid_params
2268 * and/or scan_params, we leak those bitmapsets; not worth the notational
2269 * trouble to clean them up.)
2271 if (locally_added_param >= 0)
2273 context.paramids = bms_del_member(context.paramids,
2274 locally_added_param);
2277 /* Now we have all the paramids */
2279 if (!bms_is_subset(context.paramids, valid_params))
2280 elog(ERROR, "plan should not reference subplan's variable");
2283 * Note: by definition, extParam and allParam should have the same value
2284 * in any plan node that doesn't have child initPlans. We set them equal
2285 * here, and later SS_finalize_plan will update them properly in node(s)
2286 * that it attaches initPlans to.
2288 * For speed at execution time, make sure extParam/allParam are actually
2289 * NULL if they are empty sets.
2291 if (bms_is_empty(context.paramids))
2293 plan->extParam = NULL;
2294 plan->allParam = NULL;
2298 plan->extParam = context.paramids;
2299 plan->allParam = bms_copy(context.paramids);
2302 return plan->allParam;
2306 * finalize_primnode: add IDs of all PARAM_EXEC params appearing in the given
2307 * expression tree to the result set.
2310 finalize_primnode(Node *node, finalize_primnode_context *context)
2314 if (IsA(node, Param))
2316 if (((Param *) node)->paramkind == PARAM_EXEC)
2318 int paramid = ((Param *) node)->paramid;
2320 context->paramids = bms_add_member(context->paramids, paramid);
2322 return false; /* no more to do here */
2324 if (IsA(node, SubPlan))
2326 SubPlan *subplan = (SubPlan *) node;
2327 Plan *plan = planner_subplan_get_plan(context->root, subplan);
2329 Bitmapset *subparamids;
2331 /* Recurse into the testexpr, but not into the Plan */
2332 finalize_primnode(subplan->testexpr, context);
2335 * Remove any param IDs of output parameters of the subplan that were
2336 * referenced in the testexpr. These are not interesting for
2337 * parameter change signaling since we always re-evaluate the subplan.
2338 * Note that this wouldn't work too well if there might be uses of the
2339 * same param IDs elsewhere in the plan, but that can't happen because
2340 * generate_new_param never tries to merge params.
2342 foreach(lc, subplan->paramIds)
2344 context->paramids = bms_del_member(context->paramids,
2348 /* Also examine args list */
2349 finalize_primnode((Node *) subplan->args, context);
2352 * Add params needed by the subplan to paramids, but excluding those
2353 * we will pass down to it.
2355 subparamids = bms_copy(plan->extParam);
2356 foreach(lc, subplan->parParam)
2358 subparamids = bms_del_member(subparamids, lfirst_int(lc));
2360 context->paramids = bms_join(context->paramids, subparamids);
2362 return false; /* no more to do here */
2364 return expression_tree_walker(node, finalize_primnode,
2369 * SS_make_initplan_from_plan - given a plan tree, make it an InitPlan
2371 * The plan is expected to return a scalar value of the given type/collation.
2372 * We build an EXPR_SUBLINK SubPlan node and put it into the initplan
2373 * list for the current query level. A Param that represents the initplan's
2374 * output is returned.
2376 * We assume the plan hasn't been put through SS_finalize_plan.
2379 SS_make_initplan_from_plan(PlannerInfo *root, Plan *plan,
2380 Oid resulttype, int32 resulttypmod,
2381 Oid resultcollation)
2387 * We must run SS_finalize_plan(), since that's normally done before a
2388 * subplan gets put into the initplan list. Tell it not to attach any
2389 * pre-existing initplans to this one, since they are siblings not
2390 * children of this initplan. (This is something else that could perhaps
2391 * be cleaner if we did extParam/allParam processing in setrefs.c instead
2392 * of here? See notes for materialize_finished_plan.)
2396 * Build extParam/allParam sets for plan nodes.
2398 SS_finalize_plan(root, plan, false);
2401 * Add the subplan and its PlannerInfo to the global lists.
2403 root->glob->subplans = lappend(root->glob->subplans, plan);
2404 root->glob->subroots = lappend(root->glob->subroots, root);
2407 * Create a SubPlan node and add it to the outer list of InitPlans. Note
2408 * it has to appear after any other InitPlans it might depend on (see
2409 * comments in ExecReScan).
2411 node = makeNode(SubPlan);
2412 node->subLinkType = EXPR_SUBLINK;
2413 get_first_col_type(plan, &node->firstColType, &node->firstColTypmod,
2414 &node->firstColCollation);
2415 node->plan_id = list_length(root->glob->subplans);
2417 root->init_plans = lappend(root->init_plans, node);
2420 * The node can't have any inputs (since it's an initplan), so the
2421 * parParam and args lists remain empty.
2424 cost_subplan(root, node, plan);
2427 * Make a Param that will be the subplan's output.
2429 prm = generate_new_param(root, resulttype, resulttypmod, resultcollation);
2430 node->setParam = list_make1_int(prm->paramid);
2432 /* Label the subplan for EXPLAIN purposes */
2433 node->plan_name = palloc(64);
2434 sprintf(node->plan_name, "InitPlan %d (returns $%d)",
2435 node->plan_id, prm->paramid);