1 /*-------------------------------------------------------------------------
4 * Planning routines for subselects and parameters.
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.146 2009/02/25 03:30:37 tgl Exp $
12 *-------------------------------------------------------------------------
16 #include "catalog/pg_operator.h"
17 #include "catalog/pg_type.h"
18 #include "miscadmin.h"
19 #include "nodes/makefuncs.h"
20 #include "nodes/nodeFuncs.h"
21 #include "optimizer/clauses.h"
22 #include "optimizer/cost.h"
23 #include "optimizer/planmain.h"
24 #include "optimizer/planner.h"
25 #include "optimizer/prep.h"
26 #include "optimizer/subselect.h"
27 #include "optimizer/var.h"
28 #include "parser/parse_relation.h"
29 #include "parser/parsetree.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, List *rtable,
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 static bool finalize_primnode(Node *node, finalize_primnode_context *context);
83 * Generate a Param node to replace the given Var,
84 * which is expected to have varlevelsup > 0 (ie, it is not local).
87 replace_outer_var(PlannerInfo *root, Var *var)
91 PlannerParamItem *pitem;
95 Assert(var->varlevelsup > 0 && var->varlevelsup < root->query_level);
96 abslevel = root->query_level - var->varlevelsup;
99 * If there's already a paramlist entry for this same Var, just use it.
100 * NOTE: in sufficiently complex querytrees, it is possible for the same
101 * varno/abslevel to refer to different RTEs in different parts of the
102 * parsetree, so that different fields might end up sharing the same Param
103 * number. As long as we check the vartype/typmod as well, I believe that
104 * this sort of aliasing will cause no trouble. The correct field should
105 * get stored into the Param slot at execution in each part of the tree.
108 foreach(ppl, root->glob->paramlist)
110 pitem = (PlannerParamItem *) lfirst(ppl);
111 if (pitem->abslevel == abslevel && IsA(pitem->item, Var))
113 Var *pvar = (Var *) pitem->item;
115 if (pvar->varno == var->varno &&
116 pvar->varattno == var->varattno &&
117 pvar->vartype == var->vartype &&
118 pvar->vartypmod == var->vartypmod)
126 /* Nope, so make a new one */
127 var = (Var *) copyObject(var);
128 var->varlevelsup = 0;
130 pitem = makeNode(PlannerParamItem);
131 pitem->item = (Node *) var;
132 pitem->abslevel = abslevel;
134 root->glob->paramlist = lappend(root->glob->paramlist, pitem);
135 /* i is already the correct index for the new item */
138 retval = makeNode(Param);
139 retval->paramkind = PARAM_EXEC;
141 retval->paramtype = var->vartype;
142 retval->paramtypmod = var->vartypmod;
143 retval->location = -1;
149 * Generate a Param node to replace the given Aggref
150 * which is expected to have agglevelsup > 0 (ie, it is not local).
153 replace_outer_agg(PlannerInfo *root, Aggref *agg)
156 PlannerParamItem *pitem;
160 Assert(agg->agglevelsup > 0 && agg->agglevelsup < root->query_level);
161 abslevel = root->query_level - agg->agglevelsup;
164 * It does not seem worthwhile to try to match duplicate outer aggs. Just
165 * make a new slot every time.
167 agg = (Aggref *) copyObject(agg);
168 IncrementVarSublevelsUp((Node *) agg, -((int) agg->agglevelsup), 0);
169 Assert(agg->agglevelsup == 0);
171 pitem = makeNode(PlannerParamItem);
172 pitem->item = (Node *) agg;
173 pitem->abslevel = abslevel;
175 root->glob->paramlist = lappend(root->glob->paramlist, pitem);
176 i = list_length(root->glob->paramlist) - 1;
178 retval = makeNode(Param);
179 retval->paramkind = PARAM_EXEC;
181 retval->paramtype = agg->aggtype;
182 retval->paramtypmod = -1;
183 retval->location = -1;
189 * Generate a new Param node that will not conflict with any other.
191 * This is used to allocate PARAM_EXEC slots for subplan outputs.
194 generate_new_param(PlannerInfo *root, Oid paramtype, int32 paramtypmod)
197 PlannerParamItem *pitem;
199 retval = makeNode(Param);
200 retval->paramkind = PARAM_EXEC;
201 retval->paramid = list_length(root->glob->paramlist);
202 retval->paramtype = paramtype;
203 retval->paramtypmod = paramtypmod;
204 retval->location = -1;
206 pitem = makeNode(PlannerParamItem);
207 pitem->item = (Node *) retval;
208 pitem->abslevel = root->query_level;
210 root->glob->paramlist = lappend(root->glob->paramlist, pitem);
216 * Assign a (nonnegative) PARAM_EXEC ID for a recursive query's worktable.
219 SS_assign_worktable_param(PlannerInfo *root)
223 /* We generate a Param of datatype INTERNAL */
224 param = generate_new_param(root, INTERNALOID, -1);
225 /* ... but the caller only cares about its ID */
226 return param->paramid;
230 * Get the datatype of the first column of the plan's output.
232 * This is stored for ARRAY_SUBLINK and for exprType(), which doesn't have any
233 * way to get at the plan associated with a SubPlan node. We really only need
234 * the value for EXPR_SUBLINK and ARRAY_SUBLINK subplans, but for consistency
238 get_first_col_type(Plan *plan)
240 /* In cases such as EXISTS, tlist might be empty; arbitrarily use VOID */
241 if (plan->targetlist)
243 TargetEntry *tent = (TargetEntry *) linitial(plan->targetlist);
245 Assert(IsA(tent, TargetEntry));
247 return exprType((Node *) tent->expr);
253 * Convert a SubLink (as created by the parser) into a SubPlan.
255 * We are given the SubLink's contained query, type, and testexpr. We are
256 * also told if this expression appears at top level of a WHERE/HAVING qual.
258 * Note: we assume that the testexpr has been AND/OR flattened (actually,
259 * it's been through eval_const_expressions), but not converted to
260 * implicit-AND form; and any SubLinks in it should already have been
261 * converted to SubPlans. The subquery is as yet untouched, however.
263 * The result is whatever we need to substitute in place of the SubLink
264 * node in the executable expression. This will be either the SubPlan
265 * node (if we have to do the subplan as a subplan), or a Param node
266 * representing the result of an InitPlan, or a row comparison expression
267 * tree containing InitPlan Param nodes.
270 make_subplan(PlannerInfo *root, Query *orig_subquery, SubLinkType subLinkType,
271 Node *testexpr, bool isTopQual)
274 bool simple_exists = false;
275 double tuple_fraction;
277 PlannerInfo *subroot;
281 * Copy the source Query node. This is a quick and dirty kluge to resolve
282 * the fact that the parser can generate trees with multiple links to the
283 * same sub-Query node, but the planner wants to scribble on the Query.
284 * Try to clean this up when we do querytree redesign...
286 subquery = (Query *) copyObject(orig_subquery);
289 * If it's an EXISTS subplan, we might be able to simplify it.
291 if (subLinkType == EXISTS_SUBLINK)
292 simple_exists = simplify_EXISTS_query(subquery);
295 * For an EXISTS subplan, tell lower-level planner to expect that only the
296 * first tuple will be retrieved. For ALL and ANY subplans, we will be
297 * able to stop evaluating if the test condition fails or matches, so very
298 * often not all the tuples will be retrieved; for lack of a better idea,
299 * specify 50% retrieval. For EXPR and ROWCOMPARE subplans, use default
300 * behavior (we're only expecting one row out, anyway).
302 * NOTE: if you change these numbers, also change cost_subplan() in
305 * XXX If an ANY subplan is uncorrelated, build_subplan may decide to hash
306 * its output. In that case it would've been better to specify full
307 * retrieval. At present, however, we can only check hashability after
308 * we've made the subplan :-(. (Determining whether it'll fit in work_mem
309 * is the really hard part.) Therefore, we don't want to be too
310 * optimistic about the percentage of tuples retrieved, for fear of
311 * selecting a plan that's bad for the materialization case.
313 if (subLinkType == EXISTS_SUBLINK)
314 tuple_fraction = 1.0; /* just like a LIMIT 1 */
315 else if (subLinkType == ALL_SUBLINK ||
316 subLinkType == ANY_SUBLINK)
317 tuple_fraction = 0.5; /* 50% */
319 tuple_fraction = 0.0; /* default behavior */
322 * Generate the plan for the subquery.
324 plan = subquery_planner(root->glob, subquery,
326 false, tuple_fraction,
329 /* And convert to SubPlan or InitPlan format. */
330 result = build_subplan(root, plan, subroot->parse->rtable,
331 subLinkType, testexpr, true, isTopQual);
334 * If it's a correlated EXISTS with an unimportant targetlist, we might be
335 * able to transform it to the equivalent of an IN and then implement it
336 * by hashing. We don't have enough information yet to tell which way
337 * is likely to be better (it depends on the expected number of executions
338 * of the EXISTS qual, and we are much too early in planning the outer
339 * query to be able to guess that). So we generate both plans, if
340 * possible, and leave it to the executor to decide which to use.
342 if (simple_exists && IsA(result, SubPlan))
347 /* Make a second copy of the original subquery */
348 subquery = (Query *) copyObject(orig_subquery);
349 /* and re-simplify */
350 simple_exists = simplify_EXISTS_query(subquery);
351 Assert(simple_exists);
352 /* See if it can be converted to an ANY query */
353 subquery = convert_EXISTS_to_ANY(root, subquery,
354 &newtestexpr, ¶mIds);
357 /* Generate the plan for the ANY subquery; we'll need all rows */
358 plan = subquery_planner(root->glob, subquery,
363 /* Now we can check if it'll fit in work_mem */
364 if (subplan_is_hashable(plan))
367 AlternativeSubPlan *asplan;
369 /* OK, convert to SubPlan format. */
370 hashplan = (SubPlan *) build_subplan(root, plan,
371 subroot->parse->rtable,
372 ANY_SUBLINK, newtestexpr,
374 /* Check we got what we expected */
375 Assert(IsA(hashplan, SubPlan));
376 Assert(hashplan->parParam == NIL);
377 Assert(hashplan->useHashTable);
378 /* build_subplan won't have filled in paramIds */
379 hashplan->paramIds = paramIds;
381 /* Leave it to the executor to decide which plan to use */
382 asplan = makeNode(AlternativeSubPlan);
383 asplan->subplans = list_make2(result, hashplan);
384 result = (Node *) asplan;
393 * Build a SubPlan node given the raw inputs --- subroutine for make_subplan
395 * Returns either the SubPlan, or an expression using initplan output Params,
396 * as explained in the comments for make_subplan.
399 build_subplan(PlannerInfo *root, Plan *plan, List *rtable,
400 SubLinkType subLinkType, Node *testexpr,
401 bool adjust_testexpr, bool unknownEqFalse)
410 * Initialize the SubPlan node. Note plan_id isn't set till further down,
411 * likewise the cost fields.
413 splan = makeNode(SubPlan);
414 splan->subLinkType = subLinkType;
415 splan->testexpr = NULL;
416 splan->paramIds = NIL;
417 splan->firstColType = get_first_col_type(plan);
418 splan->useHashTable = false;
419 splan->unknownEqFalse = unknownEqFalse;
420 splan->setParam = NIL;
421 splan->parParam = NIL;
425 * Make parParam and args lists of param IDs and expressions that current
426 * query level will pass to this child plan.
428 tmpset = bms_copy(plan->extParam);
429 while ((paramid = bms_first_member(tmpset)) >= 0)
431 PlannerParamItem *pitem = list_nth(root->glob->paramlist, paramid);
433 if (pitem->abslevel == root->query_level)
435 splan->parParam = lappend_int(splan->parParam, paramid);
437 * The Var or Aggref has already been adjusted to have the correct
438 * varlevelsup or agglevelsup. We probably don't even need to
439 * copy it again, but be safe.
441 splan->args = lappend(splan->args, copyObject(pitem->item));
447 * Un-correlated or undirect correlated plans of EXISTS, EXPR, ARRAY, or
448 * ROWCOMPARE types can be used as initPlans. For EXISTS, EXPR, or ARRAY,
449 * we just produce a Param referring to the result of evaluating the
450 * initPlan. For ROWCOMPARE, we must modify the testexpr tree to contain
451 * PARAM_EXEC Params instead of the PARAM_SUBLINK Params emitted by the
454 if (splan->parParam == NIL && subLinkType == EXISTS_SUBLINK)
458 Assert(testexpr == NULL);
459 prm = generate_new_param(root, BOOLOID, -1);
460 splan->setParam = list_make1_int(prm->paramid);
462 result = (Node *) prm;
464 else if (splan->parParam == NIL && subLinkType == EXPR_SUBLINK)
466 TargetEntry *te = linitial(plan->targetlist);
469 Assert(!te->resjunk);
470 Assert(testexpr == NULL);
471 prm = generate_new_param(root,
472 exprType((Node *) te->expr),
473 exprTypmod((Node *) te->expr));
474 splan->setParam = list_make1_int(prm->paramid);
476 result = (Node *) prm;
478 else if (splan->parParam == NIL && subLinkType == ARRAY_SUBLINK)
480 TargetEntry *te = linitial(plan->targetlist);
484 Assert(!te->resjunk);
485 Assert(testexpr == NULL);
486 arraytype = get_array_type(exprType((Node *) te->expr));
487 if (!OidIsValid(arraytype))
488 elog(ERROR, "could not find array type for datatype %s",
489 format_type_be(exprType((Node *) te->expr)));
490 prm = generate_new_param(root,
492 exprTypmod((Node *) te->expr));
493 splan->setParam = list_make1_int(prm->paramid);
495 result = (Node *) prm;
497 else if (splan->parParam == NIL && subLinkType == ROWCOMPARE_SUBLINK)
499 /* Adjust the Params */
502 Assert(testexpr != NULL);
503 params = generate_subquery_params(root,
506 result = convert_testexpr(root,
509 splan->setParam = list_copy(splan->paramIds);
513 * The executable expression is returned to become part of the outer
514 * plan's expression tree; it is not kept in the initplan node.
520 * Adjust the Params in the testexpr, unless caller said it's not
523 if (testexpr && adjust_testexpr)
527 params = generate_subquery_params(root,
530 splan->testexpr = convert_testexpr(root,
535 splan->testexpr = testexpr;
538 * We can't convert subplans of ALL_SUBLINK or ANY_SUBLINK types to
539 * initPlans, even when they are uncorrelated or undirect correlated,
540 * because we need to scan the output of the subplan for each outer
541 * tuple. But if it's a not-direct-correlated IN (= ANY) test, we
542 * might be able to use a hashtable to avoid comparing all the tuples.
544 if (subLinkType == ANY_SUBLINK &&
545 splan->parParam == NIL &&
546 subplan_is_hashable(plan) &&
547 testexpr_is_hashable(splan->testexpr))
548 splan->useHashTable = true;
551 * Otherwise, we have the option to tack a MATERIAL node onto the top
552 * of the subplan, to reduce the cost of reading it repeatedly. This
553 * is pointless for a direct-correlated subplan, since we'd have to
554 * recompute its results each time anyway. For uncorrelated/undirect
555 * correlated subplans, we add MATERIAL unless the subplan's top plan
556 * node would materialize its output anyway.
558 else if (splan->parParam == NIL)
562 switch (nodeTag(plan))
567 case T_WorkTableScan:
569 use_material = false;
576 plan = materialize_finished_plan(plan);
579 result = (Node *) splan;
584 * Add the subplan and its rtable to the global lists.
586 root->glob->subplans = lappend(root->glob->subplans, plan);
587 root->glob->subrtables = lappend(root->glob->subrtables, rtable);
588 splan->plan_id = list_length(root->glob->subplans);
591 root->init_plans = lappend(root->init_plans, splan);
594 * A parameterless subplan (not initplan) should be prepared to handle
595 * REWIND efficiently. If it has direct parameters then there's no point
596 * since it'll be reset on each scan anyway; and if it's an initplan then
597 * there's no point since it won't get re-run without parameter changes
598 * anyway. The input of a hashed subplan doesn't need REWIND either.
600 if (splan->parParam == NIL && !isInitPlan && !splan->useHashTable)
601 root->glob->rewindPlanIDs = bms_add_member(root->glob->rewindPlanIDs,
604 /* Lastly, fill in the cost estimates for use later */
605 cost_subplan(root, splan, plan);
611 * generate_subquery_params: build a list of Params representing the output
612 * columns of a sublink's sub-select, given the sub-select's targetlist.
614 * We also return an integer list of the paramids of the Params.
617 generate_subquery_params(PlannerInfo *root, List *tlist, List **paramIds)
626 TargetEntry *tent = (TargetEntry *) lfirst(lc);
632 param = generate_new_param(root,
633 exprType((Node *) tent->expr),
634 exprTypmod((Node *) tent->expr));
635 result = lappend(result, param);
636 ids = lappend_int(ids, param->paramid);
644 * generate_subquery_vars: build a list of Vars representing the output
645 * columns of a sublink's sub-select, given the sub-select's targetlist.
646 * The Vars have the specified varno (RTE index).
649 generate_subquery_vars(PlannerInfo *root, List *tlist, Index varno)
657 TargetEntry *tent = (TargetEntry *) lfirst(lc);
665 exprType((Node *) tent->expr),
666 exprTypmod((Node *) tent->expr),
668 result = lappend(result, var);
675 * convert_testexpr: convert the testexpr given by the parser into
676 * actually executable form. This entails replacing PARAM_SUBLINK Params
677 * with Params or Vars representing the results of the sub-select. The
678 * nodes to be substituted are passed in as the List result from
679 * generate_subquery_params or generate_subquery_vars.
681 * The given testexpr has already been recursively processed by
682 * process_sublinks_mutator. Hence it can no longer contain any
683 * PARAM_SUBLINK Params for lower SubLink nodes; we can safely assume that
684 * any we find are for our own level of SubLink.
687 convert_testexpr(PlannerInfo *root,
691 convert_testexpr_context context;
694 context.subst_nodes = subst_nodes;
695 return convert_testexpr_mutator(testexpr, &context);
699 convert_testexpr_mutator(Node *node,
700 convert_testexpr_context *context)
704 if (IsA(node, Param))
706 Param *param = (Param *) node;
708 if (param->paramkind == PARAM_SUBLINK)
710 if (param->paramid <= 0 ||
711 param->paramid > list_length(context->subst_nodes))
712 elog(ERROR, "unexpected PARAM_SUBLINK ID: %d", param->paramid);
715 * We copy the list item to avoid having doubly-linked
716 * substructure in the modified parse tree. This is probably
717 * unnecessary when it's a Param, but be safe.
719 return (Node *) copyObject(list_nth(context->subst_nodes,
720 param->paramid - 1));
723 return expression_tree_mutator(node,
724 convert_testexpr_mutator,
729 * subplan_is_hashable: can we implement an ANY subplan by hashing?
732 subplan_is_hashable(Plan *plan)
734 double subquery_size;
737 * The estimated size of the subquery result must fit in work_mem. (Note:
738 * we use sizeof(HeapTupleHeaderData) here even though the tuples will
739 * actually be stored as MinimalTuples; this provides some fudge factor
740 * for hashtable overhead.)
742 subquery_size = plan->plan_rows *
743 (MAXALIGN(plan->plan_width) + MAXALIGN(sizeof(HeapTupleHeaderData)));
744 if (subquery_size > work_mem * 1024L)
751 * testexpr_is_hashable: is an ANY SubLink's test expression hashable?
754 testexpr_is_hashable(Node *testexpr)
757 * The testexpr must be a single OpExpr, or an AND-clause containing
760 * The combining operators must be hashable and strict. The need for
761 * hashability is obvious, since we want to use hashing. Without
762 * strictness, behavior in the presence of nulls is too unpredictable. We
763 * actually must assume even more than plain strictness: they can't yield
764 * NULL for non-null inputs, either (see nodeSubplan.c). However, hash
765 * indexes and hash joins assume that too.
767 if (testexpr && IsA(testexpr, OpExpr))
769 if (hash_ok_operator((OpExpr *) testexpr))
772 else if (and_clause(testexpr))
776 foreach(l, ((BoolExpr *) testexpr)->args)
778 Node *andarg = (Node *) lfirst(l);
780 if (!IsA(andarg, OpExpr))
782 if (!hash_ok_operator((OpExpr *) andarg))
792 hash_ok_operator(OpExpr *expr)
794 Oid opid = expr->opno;
796 Form_pg_operator optup;
798 /* quick out if not a binary operator */
799 if (list_length(expr->args) != 2)
801 /* else must look up the operator properties */
802 tup = SearchSysCache(OPEROID,
803 ObjectIdGetDatum(opid),
805 if (!HeapTupleIsValid(tup))
806 elog(ERROR, "cache lookup failed for operator %u", opid);
807 optup = (Form_pg_operator) GETSTRUCT(tup);
808 if (!optup->oprcanhash || !func_strict(optup->oprcode))
810 ReleaseSysCache(tup);
813 ReleaseSysCache(tup);
819 * SS_process_ctes: process a query's WITH list
821 * We plan each interesting WITH item and convert it to an initplan.
822 * A side effect is to fill in root->cte_plan_ids with a list that
823 * parallels root->parse->cteList and provides the subplan ID for
824 * each CTE's initplan.
827 SS_process_ctes(PlannerInfo *root)
831 Assert(root->cte_plan_ids == NIL);
833 foreach(lc, root->parse->cteList)
835 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
838 PlannerInfo *subroot;
845 * Ignore CTEs that are not actually referenced anywhere.
847 if (cte->cterefcount == 0)
849 /* Make a dummy entry in cte_plan_ids */
850 root->cte_plan_ids = lappend_int(root->cte_plan_ids, -1);
855 * Copy the source Query node. Probably not necessary, but let's
856 * keep this similar to make_subplan.
858 subquery = (Query *) copyObject(cte->ctequery);
861 * Generate the plan for the CTE query. Always plan for full
862 * retrieval --- we don't have enough info to predict otherwise.
864 plan = subquery_planner(root->glob, subquery,
866 cte->cterecursive, 0.0,
870 * Make a SubPlan node for it. This is just enough unlike
871 * build_subplan that we can't share code.
873 * Note plan_id isn't set till further down, likewise the cost fields.
875 splan = makeNode(SubPlan);
876 splan->subLinkType = CTE_SUBLINK;
877 splan->testexpr = NULL;
878 splan->paramIds = NIL;
879 splan->firstColType = get_first_col_type(plan);
880 splan->useHashTable = false;
881 splan->unknownEqFalse = false;
882 splan->setParam = NIL;
883 splan->parParam = NIL;
887 * Make parParam and args lists of param IDs and expressions that
888 * current query level will pass to this child plan. Even though
889 * this is an initplan, there could be side-references to earlier
890 * initplan's outputs, specifically their CTE output parameters.
892 tmpset = bms_copy(plan->extParam);
893 while ((paramid = bms_first_member(tmpset)) >= 0)
895 PlannerParamItem *pitem = list_nth(root->glob->paramlist, paramid);
897 if (pitem->abslevel == root->query_level)
899 prm = (Param *) pitem->item;
900 if (!IsA(prm, Param) ||
901 prm->paramtype != INTERNALOID)
902 elog(ERROR, "bogus local parameter passed to WITH query");
904 splan->parParam = lappend_int(splan->parParam, paramid);
905 splan->args = lappend(splan->args, copyObject(prm));
911 * Assign a param to represent the query output. We only really
912 * care about reserving a parameter ID number.
914 prm = generate_new_param(root, INTERNALOID, -1);
915 splan->setParam = list_make1_int(prm->paramid);
918 * Add the subplan and its rtable to the global lists.
920 root->glob->subplans = lappend(root->glob->subplans, plan);
921 root->glob->subrtables = lappend(root->glob->subrtables,
922 subroot->parse->rtable);
923 splan->plan_id = list_length(root->glob->subplans);
925 root->init_plans = lappend(root->init_plans, splan);
927 root->cte_plan_ids = lappend_int(root->cte_plan_ids, splan->plan_id);
929 /* Lastly, fill in the cost estimates for use later */
930 cost_subplan(root, splan, plan);
935 * convert_ANY_sublink_to_join: try to convert an ANY SubLink to a join
937 * The caller has found an ANY SubLink at the top level of one of the query's
938 * qual clauses, but has not checked the properties of the SubLink further.
939 * Decide whether it is appropriate to process this SubLink in join style.
940 * If so, form a JoinExpr and return it. Return NULL if the SubLink cannot
941 * be converted to a join.
943 * The only non-obvious input parameter is available_rels: this is the set
944 * of query rels that can safely be referenced in the sublink expression.
945 * (We must restrict this to avoid changing the semantics when a sublink
946 * is present in an outer join's ON qual.) The conversion must fail if
947 * the converted qual would reference any but these parent-query relids.
949 * On success, the returned JoinExpr has larg = NULL and rarg = the jointree
950 * item representing the pulled-up subquery. The caller must set larg to
951 * represent the relation(s) on the lefthand side of the new join, and insert
952 * the JoinExpr into the upper query's jointree at an appropriate place
953 * (typically, where the lefthand relation(s) had been). Note that the
954 * passed-in SubLink must also be removed from its original position in the
955 * query quals, since the quals of the returned JoinExpr replace it.
956 * (Notionally, we replace the SubLink with a constant TRUE, then elide the
957 * redundant constant from the qual.)
959 * Side effects of a successful conversion include adding the SubLink's
960 * subselect to the query's rangetable, so that it can be referenced in
961 * the JoinExpr's rarg.
964 convert_ANY_sublink_to_join(PlannerInfo *root, SubLink *sublink,
965 Relids available_rels)
968 Query *parse = root->parse;
969 Query *subselect = (Query *) sublink->subselect;
977 Assert(sublink->subLinkType == ANY_SUBLINK);
980 * The sub-select must not refer to any Vars of the parent query. (Vars of
981 * higher levels should be okay, though.)
983 if (contain_vars_of_level((Node *) subselect, 1))
987 * The test expression must contain some Vars of the parent query,
988 * else it's not gonna be a join. (Note that it won't have Vars
989 * referring to the subquery, rather Params.)
991 upper_varnos = pull_varnos(sublink->testexpr);
992 if (bms_is_empty(upper_varnos))
996 * However, it can't refer to anything outside available_rels.
998 if (!bms_is_subset(upper_varnos, available_rels))
1002 * The combining operators and left-hand expressions mustn't be volatile.
1004 if (contain_volatile_functions(sublink->testexpr))
1008 * Okay, pull up the sub-select into upper range table.
1010 * We rely here on the assumption that the outer query has no references
1011 * to the inner (necessarily true, other than the Vars that we build
1012 * below). Therefore this is a lot easier than what pull_up_subqueries has
1015 rte = addRangeTableEntryForSubquery(NULL,
1017 makeAlias("ANY_subquery", NIL),
1019 parse->rtable = lappend(parse->rtable, rte);
1020 rtindex = list_length(parse->rtable);
1023 * Form a RangeTblRef for the pulled-up sub-select.
1025 rtr = makeNode(RangeTblRef);
1026 rtr->rtindex = rtindex;
1029 * Build a list of Vars representing the subselect outputs.
1031 subquery_vars = generate_subquery_vars(root,
1032 subselect->targetList,
1036 * Build the new join's qual expression, replacing Params with these Vars.
1038 quals = convert_testexpr(root, sublink->testexpr, subquery_vars);
1041 * And finally, build the JoinExpr node.
1043 result = makeNode(JoinExpr);
1044 result->jointype = JOIN_SEMI;
1045 result->isNatural = false;
1046 result->larg = NULL; /* caller must fill this in */
1047 result->rarg = (Node *) rtr;
1048 result->using = NIL;
1049 result->quals = quals;
1050 result->alias = NULL;
1051 result->rtindex = 0; /* we don't need an RTE for it */
1057 * convert_EXISTS_sublink_to_join: try to convert an EXISTS SubLink to a join
1059 * The API of this function is identical to convert_ANY_sublink_to_join's,
1060 * except that we also support the case where the caller has found NOT EXISTS,
1061 * so we need an additional input parameter "under_not".
1064 convert_EXISTS_sublink_to_join(PlannerInfo *root, SubLink *sublink,
1065 bool under_not, Relids available_rels)
1068 Query *parse = root->parse;
1069 Query *subselect = (Query *) sublink->subselect;
1073 Relids clause_varnos;
1074 Relids upper_varnos;
1076 Assert(sublink->subLinkType == EXISTS_SUBLINK);
1079 * Copy the subquery so we can modify it safely (see comments in
1082 subselect = (Query *) copyObject(subselect);
1085 * See if the subquery can be simplified based on the knowledge that
1086 * it's being used in EXISTS(). If we aren't able to get rid of its
1087 * targetlist, we have to fail, because the pullup operation leaves
1088 * us with noplace to evaluate the targetlist.
1090 if (!simplify_EXISTS_query(subselect))
1094 * The subquery must have a nonempty jointree, else we won't have a join.
1096 if (subselect->jointree->fromlist == NIL)
1100 * Separate out the WHERE clause. (We could theoretically also remove
1101 * top-level plain JOIN/ON clauses, but it's probably not worth the
1104 whereClause = subselect->jointree->quals;
1105 subselect->jointree->quals = NULL;
1108 * The rest of the sub-select must not refer to any Vars of the parent
1109 * query. (Vars of higher levels should be okay, though.)
1111 if (contain_vars_of_level((Node *) subselect, 1))
1115 * On the other hand, the WHERE clause must contain some Vars of the
1116 * parent query, else it's not gonna be a join.
1118 if (!contain_vars_of_level(whereClause, 1))
1122 * We don't risk optimizing if the WHERE clause is volatile, either.
1124 if (contain_volatile_functions(whereClause))
1128 * Prepare to pull up the sub-select into top range table.
1130 * We rely here on the assumption that the outer query has no references
1131 * to the inner (necessarily true). Therefore this is a lot easier than
1132 * what pull_up_subqueries has to go through.
1134 * In fact, it's even easier than what convert_ANY_sublink_to_join has
1135 * to do. The machinations of simplify_EXISTS_query ensured that there
1136 * is nothing interesting in the subquery except an rtable and jointree,
1137 * and even the jointree FromExpr no longer has quals. So we can just
1138 * append the rtable to our own and use the FromExpr in our jointree.
1139 * But first, adjust all level-zero varnos in the subquery to account
1140 * for the rtable merger.
1142 rtoffset = list_length(parse->rtable);
1143 OffsetVarNodes((Node *) subselect, rtoffset, 0);
1144 OffsetVarNodes(whereClause, rtoffset, 0);
1147 * Upper-level vars in subquery will now be one level closer to their
1148 * parent than before; in particular, anything that had been level 1
1149 * becomes level zero.
1151 IncrementVarSublevelsUp((Node *) subselect, -1, 1);
1152 IncrementVarSublevelsUp(whereClause, -1, 1);
1155 * Now that the WHERE clause is adjusted to match the parent query
1156 * environment, we can easily identify all the level-zero rels it uses.
1157 * The ones <= rtoffset belong to the upper query; the ones > rtoffset
1160 clause_varnos = pull_varnos(whereClause);
1161 upper_varnos = NULL;
1162 while ((varno = bms_first_member(clause_varnos)) >= 0)
1164 if (varno <= rtoffset)
1165 upper_varnos = bms_add_member(upper_varnos, varno);
1167 bms_free(clause_varnos);
1168 Assert(!bms_is_empty(upper_varnos));
1171 * Now that we've got the set of upper-level varnos, we can make the
1172 * last check: only available_rels can be referenced.
1174 if (!bms_is_subset(upper_varnos, available_rels))
1177 /* Now we can attach the modified subquery rtable to the parent */
1178 parse->rtable = list_concat(parse->rtable, subselect->rtable);
1181 * And finally, build the JoinExpr node.
1183 result = makeNode(JoinExpr);
1184 result->jointype = under_not ? JOIN_ANTI : JOIN_SEMI;
1185 result->isNatural = false;
1186 result->larg = NULL; /* caller must fill this in */
1187 /* flatten out the FromExpr node if it's useless */
1188 if (list_length(subselect->jointree->fromlist) == 1)
1189 result->rarg = (Node *) linitial(subselect->jointree->fromlist);
1191 result->rarg = (Node *) subselect->jointree;
1192 result->using = NIL;
1193 result->quals = whereClause;
1194 result->alias = NULL;
1195 result->rtindex = 0; /* we don't need an RTE for it */
1201 * simplify_EXISTS_query: remove any useless stuff in an EXISTS's subquery
1203 * The only thing that matters about an EXISTS query is whether it returns
1204 * zero or more than zero rows. Therefore, we can remove certain SQL features
1205 * that won't affect that. The only part that is really likely to matter in
1206 * typical usage is simplifying the targetlist: it's a common habit to write
1207 * "SELECT * FROM" even though there is no need to evaluate any columns.
1209 * Note: by suppressing the targetlist we could cause an observable behavioral
1210 * change, namely that any errors that might occur in evaluating the tlist
1211 * won't occur, nor will other side-effects of volatile functions. This seems
1212 * unlikely to bother anyone in practice.
1214 * Returns TRUE if was able to discard the targetlist, else FALSE.
1217 simplify_EXISTS_query(Query *query)
1220 * We don't try to simplify at all if the query uses set operations,
1221 * aggregates, HAVING, LIMIT/OFFSET, or FOR UPDATE/SHARE; none of these
1222 * seem likely in normal usage and their possible effects are complex.
1224 if (query->commandType != CMD_SELECT ||
1225 query->intoClause ||
1226 query->setOperations ||
1228 query->hasWindowFuncs ||
1229 query->havingQual ||
1230 query->limitOffset ||
1231 query->limitCount ||
1236 * Mustn't throw away the targetlist if it contains set-returning
1237 * functions; those could affect whether zero rows are returned!
1239 if (expression_returns_set((Node *) query->targetList))
1243 * Otherwise, we can throw away the targetlist, as well as any GROUP,
1244 * WINDOW, DISTINCT, and ORDER BY clauses; none of those clauses will
1245 * change a nonzero-rows result to zero rows or vice versa. (Furthermore,
1246 * since our parsetree representation of these clauses depends on the
1247 * targetlist, we'd better throw them away if we drop the targetlist.)
1249 query->targetList = NIL;
1250 query->groupClause = NIL;
1251 query->windowClause = NIL;
1252 query->distinctClause = NIL;
1253 query->sortClause = NIL;
1254 query->hasDistinctOn = false;
1260 * convert_EXISTS_to_ANY: try to convert EXISTS to a hashable ANY sublink
1262 * The subselect is expected to be a fresh copy that we can munge up,
1263 * and to have been successfully passed through simplify_EXISTS_query.
1265 * On success, the modified subselect is returned, and we store a suitable
1266 * upper-level test expression at *testexpr, plus a list of the subselect's
1267 * output Params at *paramIds. (The test expression is already Param-ified
1268 * and hence need not go through convert_testexpr, which is why we have to
1269 * deal with the Param IDs specially.)
1271 * On failure, returns NULL.
1274 convert_EXISTS_to_ANY(PlannerInfo *root, Query *subselect,
1275 Node **testexpr, List **paramIds)
1291 * Query must not require a targetlist, since we have to insert a new one.
1292 * Caller should have dealt with the case already.
1294 Assert(subselect->targetList == NIL);
1297 * Separate out the WHERE clause. (We could theoretically also remove
1298 * top-level plain JOIN/ON clauses, but it's probably not worth the
1301 whereClause = subselect->jointree->quals;
1302 subselect->jointree->quals = NULL;
1305 * The rest of the sub-select must not refer to any Vars of the parent
1306 * query. (Vars of higher levels should be okay, though.)
1308 * Note: we need not check for Aggrefs separately because we know the
1309 * sub-select is as yet unoptimized; any uplevel Aggref must therefore
1310 * contain an uplevel Var reference. This is not the case below ...
1312 if (contain_vars_of_level((Node *) subselect, 1))
1316 * We don't risk optimizing if the WHERE clause is volatile, either.
1318 if (contain_volatile_functions(whereClause))
1322 * Clean up the WHERE clause by doing const-simplification etc on it.
1323 * Aside from simplifying the processing we're about to do, this is
1324 * important for being able to pull chunks of the WHERE clause up into
1325 * the parent query. Since we are invoked partway through the parent's
1326 * preprocess_expression() work, earlier steps of preprocess_expression()
1327 * wouldn't get applied to the pulled-up stuff unless we do them here.
1328 * For the parts of the WHERE clause that get put back into the child
1329 * query, this work is partially duplicative, but it shouldn't hurt.
1331 * Note: we do not run flatten_join_alias_vars. This is OK because
1332 * any parent aliases were flattened already, and we're not going to
1333 * pull any child Vars (of any description) into the parent.
1335 * Note: passing the parent's root to eval_const_expressions is technically
1336 * wrong, but we can get away with it since only the boundParams (if any)
1337 * are used, and those would be the same in a subroot.
1339 whereClause = eval_const_expressions(root, whereClause);
1340 whereClause = (Node *) canonicalize_qual((Expr *) whereClause);
1341 whereClause = (Node *) make_ands_implicit((Expr *) whereClause);
1344 * We now have a flattened implicit-AND list of clauses, which we
1345 * try to break apart into "outervar = innervar" hash clauses.
1346 * Anything that can't be broken apart just goes back into the
1347 * newWhere list. Note that we aren't trying hard yet to ensure
1348 * that we have only outer or only inner on each side; we'll check
1349 * that if we get to the end.
1351 leftargs = rightargs = opids = newWhere = NIL;
1352 foreach(lc, (List *) whereClause)
1354 OpExpr *expr = (OpExpr *) lfirst(lc);
1356 if (IsA(expr, OpExpr) &&
1357 hash_ok_operator(expr))
1359 Node *leftarg = (Node *) linitial(expr->args);
1360 Node *rightarg = (Node *) lsecond(expr->args);
1362 if (contain_vars_of_level(leftarg, 1))
1364 leftargs = lappend(leftargs, leftarg);
1365 rightargs = lappend(rightargs, rightarg);
1366 opids = lappend_oid(opids, expr->opno);
1369 if (contain_vars_of_level(rightarg, 1))
1372 * We must commute the clause to put the outer var on the
1373 * left, because the hashing code in nodeSubplan.c expects
1374 * that. This probably shouldn't ever fail, since hashable
1375 * operators ought to have commutators, but be paranoid.
1377 expr->opno = get_commutator(expr->opno);
1378 if (OidIsValid(expr->opno) && hash_ok_operator(expr))
1380 leftargs = lappend(leftargs, rightarg);
1381 rightargs = lappend(rightargs, leftarg);
1382 opids = lappend_oid(opids, expr->opno);
1385 /* If no commutator, no chance to optimize the WHERE clause */
1389 /* Couldn't handle it as a hash clause */
1390 newWhere = lappend(newWhere, expr);
1394 * If we didn't find anything we could convert, fail.
1396 if (leftargs == NIL)
1400 * There mustn't be any parent Vars or Aggs in the stuff that we intend to
1401 * put back into the child query. Note: you might think we don't need to
1402 * check for Aggs separately, because an uplevel Agg must contain an
1403 * uplevel Var in its argument. But it is possible that the uplevel Var
1404 * got optimized away by eval_const_expressions. Consider
1406 * SUM(CASE WHEN false THEN uplevelvar ELSE 0 END)
1408 if (contain_vars_of_level((Node *) newWhere, 1) ||
1409 contain_vars_of_level((Node *) rightargs, 1))
1411 if (root->parse->hasAggs &&
1412 (contain_aggs_of_level((Node *) newWhere, 1) ||
1413 contain_aggs_of_level((Node *) rightargs, 1)))
1417 * And there can't be any child Vars in the stuff we intend to pull up.
1418 * (Note: we'd need to check for child Aggs too, except we know the
1419 * child has no aggs at all because of simplify_EXISTS_query's check.
1420 * The same goes for window functions.)
1422 if (contain_vars_of_level((Node *) leftargs, 0))
1426 * Also reject sublinks in the stuff we intend to pull up. (It might be
1427 * possible to support this, but doesn't seem worth the complication.)
1429 if (contain_subplans((Node *) leftargs))
1433 * Okay, adjust the sublevelsup in the stuff we're pulling up.
1435 IncrementVarSublevelsUp((Node *) leftargs, -1, 1);
1438 * Put back any child-level-only WHERE clauses.
1441 subselect->jointree->quals = (Node *) make_ands_explicit(newWhere);
1444 * Build a new targetlist for the child that emits the expressions
1445 * we need. Concurrently, build a testexpr for the parent using
1446 * Params to reference the child outputs. (Since we generate Params
1447 * directly here, there will be no need to convert the testexpr in
1450 tlist = testlist = paramids = NIL;
1452 /* there's no "for3" so we have to chase one of the lists manually */
1453 oc = list_head(opids);
1454 forboth(lc, leftargs, rc, rightargs)
1456 Node *leftarg = (Node *) lfirst(lc);
1457 Node *rightarg = (Node *) lfirst(rc);
1458 Oid opid = lfirst_oid(oc);
1462 param = generate_new_param(root,
1464 exprTypmod(rightarg));
1465 tlist = lappend(tlist,
1466 makeTargetEntry((Expr *) rightarg,
1470 testlist = lappend(testlist,
1471 make_opclause(opid, BOOLOID, false,
1472 (Expr *) leftarg, (Expr *) param));
1473 paramids = lappend_int(paramids, param->paramid);
1476 /* Put everything where it should go, and we're done */
1477 subselect->targetList = tlist;
1478 *testexpr = (Node *) make_ands_explicit(testlist);
1479 *paramIds = paramids;
1486 * Replace correlation vars (uplevel vars) with Params.
1488 * Uplevel aggregates are replaced, too.
1490 * Note: it is critical that this runs immediately after SS_process_sublinks.
1491 * Since we do not recurse into the arguments of uplevel aggregates, they will
1492 * get copied to the appropriate subplan args list in the parent query with
1493 * uplevel vars not replaced by Params, but only adjusted in level (see
1494 * replace_outer_agg). That's exactly what we want for the vars of the parent
1495 * level --- but if an aggregate's argument contains any further-up variables,
1496 * they have to be replaced with Params in their turn. That will happen when
1497 * the parent level runs SS_replace_correlation_vars. Therefore it must do
1498 * so after expanding its sublinks to subplans. And we don't want any steps
1499 * in between, else those steps would never get applied to the aggregate
1500 * argument expressions, either in the parent or the child level.
1503 SS_replace_correlation_vars(PlannerInfo *root, Node *expr)
1505 /* No setup needed for tree walk, so away we go */
1506 return replace_correlation_vars_mutator(expr, root);
1510 replace_correlation_vars_mutator(Node *node, PlannerInfo *root)
1516 if (((Var *) node)->varlevelsup > 0)
1517 return (Node *) replace_outer_var(root, (Var *) node);
1519 if (IsA(node, Aggref))
1521 if (((Aggref *) node)->agglevelsup > 0)
1522 return (Node *) replace_outer_agg(root, (Aggref *) node);
1524 return expression_tree_mutator(node,
1525 replace_correlation_vars_mutator,
1530 * Expand SubLinks to SubPlans in the given expression.
1532 * The isQual argument tells whether or not this expression is a WHERE/HAVING
1533 * qualifier expression. If it is, any sublinks appearing at top level need
1534 * not distinguish FALSE from UNKNOWN return values.
1537 SS_process_sublinks(PlannerInfo *root, Node *expr, bool isQual)
1539 process_sublinks_context context;
1541 context.root = root;
1542 context.isTopQual = isQual;
1543 return process_sublinks_mutator(expr, &context);
1547 process_sublinks_mutator(Node *node, process_sublinks_context *context)
1549 process_sublinks_context locContext;
1551 locContext.root = context->root;
1555 if (IsA(node, SubLink))
1557 SubLink *sublink = (SubLink *) node;
1561 * First, recursively process the lefthand-side expressions, if any.
1562 * They're not top-level anymore.
1564 locContext.isTopQual = false;
1565 testexpr = process_sublinks_mutator(sublink->testexpr, &locContext);
1568 * Now build the SubPlan node and make the expr to return.
1570 return make_subplan(context->root,
1571 (Query *) sublink->subselect,
1572 sublink->subLinkType,
1574 context->isTopQual);
1578 * We should never see a SubPlan expression in the input (since this is
1579 * the very routine that creates 'em to begin with). We shouldn't find
1580 * ourselves invoked directly on a Query, either.
1582 Assert(!IsA(node, SubPlan));
1583 Assert(!IsA(node, AlternativeSubPlan));
1584 Assert(!IsA(node, Query));
1587 * Because make_subplan() could return an AND or OR clause, we have to
1588 * take steps to preserve AND/OR flatness of a qual. We assume the input
1589 * has been AND/OR flattened and so we need no recursion here.
1591 * (Due to the coding here, we will not get called on the List subnodes of
1592 * an AND; and the input is *not* yet in implicit-AND format. So no check
1593 * is needed for a bare List.)
1595 * Anywhere within the top-level AND/OR clause structure, we can tell
1596 * make_subplan() that NULL and FALSE are interchangeable. So isTopQual
1597 * propagates down in both cases. (Note that this is unlike the meaning
1598 * of "top level qual" used in most other places in Postgres.)
1600 if (and_clause(node))
1602 List *newargs = NIL;
1605 /* Still at qual top-level */
1606 locContext.isTopQual = context->isTopQual;
1608 foreach(l, ((BoolExpr *) node)->args)
1612 newarg = process_sublinks_mutator(lfirst(l), &locContext);
1613 if (and_clause(newarg))
1614 newargs = list_concat(newargs, ((BoolExpr *) newarg)->args);
1616 newargs = lappend(newargs, newarg);
1618 return (Node *) make_andclause(newargs);
1621 if (or_clause(node))
1623 List *newargs = NIL;
1626 /* Still at qual top-level */
1627 locContext.isTopQual = context->isTopQual;
1629 foreach(l, ((BoolExpr *) node)->args)
1633 newarg = process_sublinks_mutator(lfirst(l), &locContext);
1634 if (or_clause(newarg))
1635 newargs = list_concat(newargs, ((BoolExpr *) newarg)->args);
1637 newargs = lappend(newargs, newarg);
1639 return (Node *) make_orclause(newargs);
1643 * If we recurse down through anything other than an AND or OR node,
1644 * we are definitely not at top qual level anymore.
1646 locContext.isTopQual = false;
1648 return expression_tree_mutator(node,
1649 process_sublinks_mutator,
1650 (void *) &locContext);
1654 * SS_finalize_plan - do final sublink processing for a completed Plan.
1656 * This recursively computes the extParam and allParam sets for every Plan
1657 * node in the given plan tree. It also optionally attaches any previously
1658 * generated InitPlans to the top plan node. (Any InitPlans should already
1659 * have been put through SS_finalize_plan.)
1662 SS_finalize_plan(PlannerInfo *root, Plan *plan, bool attach_initplans)
1664 Bitmapset *valid_params,
1672 * Examine any initPlans to determine the set of external params they
1673 * reference, the set of output params they supply, and their total cost.
1674 * We'll use at least some of this info below. (Note we are assuming that
1675 * finalize_plan doesn't touch the initPlans.)
1677 * In the case where attach_initplans is false, we are assuming that the
1678 * existing initPlans are siblings that might supply params needed by the
1681 initExtParam = initSetParam = NULL;
1683 foreach(l, root->init_plans)
1685 SubPlan *initsubplan = (SubPlan *) lfirst(l);
1686 Plan *initplan = planner_subplan_get_plan(root, initsubplan);
1689 initExtParam = bms_add_members(initExtParam, initplan->extParam);
1690 foreach(l2, initsubplan->setParam)
1692 initSetParam = bms_add_member(initSetParam, lfirst_int(l2));
1694 initplan_cost += initsubplan->startup_cost + initsubplan->per_call_cost;
1698 * Now determine the set of params that are validly referenceable in this
1699 * query level; to wit, those available from outer query levels plus the
1700 * output parameters of any initPlans. (We do not include output
1701 * parameters of regular subplans. Those should only appear within the
1702 * testexpr of SubPlan nodes, and are taken care of locally within
1703 * finalize_primnode.)
1705 * Note: this is a bit overly generous since some parameters of upper
1706 * query levels might belong to query subtrees that don't include this
1707 * query. However, valid_params is only a debugging crosscheck, so it
1708 * doesn't seem worth expending lots of cycles to try to be exact.
1710 valid_params = bms_copy(initSetParam);
1712 foreach(l, root->glob->paramlist)
1714 PlannerParamItem *pitem = (PlannerParamItem *) lfirst(l);
1716 if (pitem->abslevel < root->query_level)
1718 /* valid outer-level parameter */
1719 valid_params = bms_add_member(valid_params, paramid);
1724 /* Also include the recursion working table, if any */
1725 if (root->wt_param_id >= 0)
1726 valid_params = bms_add_member(valid_params, root->wt_param_id);
1729 * Now recurse through plan tree.
1731 (void) finalize_plan(root, plan, valid_params);
1733 bms_free(valid_params);
1736 * Finally, attach any initPlans to the topmost plan node, and add their
1737 * extParams to the topmost node's, too. However, any setParams of the
1738 * initPlans should not be present in the topmost node's extParams, only
1739 * in its allParams. (As of PG 8.1, it's possible that some initPlans
1740 * have extParams that are setParams of other initPlans, so we have to
1741 * take care of this situation explicitly.)
1743 * We also add the eval cost of each initPlan to the startup cost of the
1744 * top node. This is a conservative overestimate, since in fact each
1745 * initPlan might be executed later than plan startup, or even not at all.
1747 if (attach_initplans)
1749 plan->initPlan = root->init_plans;
1750 root->init_plans = NIL; /* make sure they're not attached twice */
1752 /* allParam must include all these params */
1753 plan->allParam = bms_add_members(plan->allParam, initExtParam);
1754 plan->allParam = bms_add_members(plan->allParam, initSetParam);
1755 /* extParam must include any child extParam */
1756 plan->extParam = bms_add_members(plan->extParam, initExtParam);
1757 /* but extParam shouldn't include any setParams */
1758 plan->extParam = bms_del_members(plan->extParam, initSetParam);
1759 /* ensure extParam is exactly NULL if it's empty */
1760 if (bms_is_empty(plan->extParam))
1761 plan->extParam = NULL;
1763 plan->startup_cost += initplan_cost;
1764 plan->total_cost += initplan_cost;
1769 * Recursive processing of all nodes in the plan tree
1771 * The return value is the computed allParam set for the given Plan node.
1772 * This is just an internal notational convenience.
1775 finalize_plan(PlannerInfo *root, Plan *plan, Bitmapset *valid_params)
1777 finalize_primnode_context context;
1782 context.root = root;
1783 context.paramids = NULL; /* initialize set to empty */
1786 * When we call finalize_primnode, context.paramids sets are automatically
1787 * merged together. But when recursing to self, we have to do it the hard
1788 * way. We want the paramids set to include params in subplans as well as
1792 /* Find params in targetlist and qual */
1793 finalize_primnode((Node *) plan->targetlist, &context);
1794 finalize_primnode((Node *) plan->qual, &context);
1796 /* Check additional node-type-specific fields */
1797 switch (nodeTag(plan))
1800 finalize_primnode(((Result *) plan)->resconstantqual,
1805 finalize_primnode((Node *) ((IndexScan *) plan)->indexqual,
1809 * we need not look at indexqualorig, since it will have the same
1810 * param references as indexqual.
1814 case T_BitmapIndexScan:
1815 finalize_primnode((Node *) ((BitmapIndexScan *) plan)->indexqual,
1819 * we need not look at indexqualorig, since it will have the same
1820 * param references as indexqual.
1824 case T_BitmapHeapScan:
1825 finalize_primnode((Node *) ((BitmapHeapScan *) plan)->bitmapqualorig,
1830 finalize_primnode((Node *) ((TidScan *) plan)->tidquals,
1834 case T_SubqueryScan:
1837 * In a SubqueryScan, SS_finalize_plan has already been run on the
1838 * subplan by the inner invocation of subquery_planner, so there's
1839 * no need to do it again. Instead, just pull out the subplan's
1840 * extParams list, which represents the params it needs from my
1841 * level and higher levels.
1843 context.paramids = bms_add_members(context.paramids,
1844 ((SubqueryScan *) plan)->subplan->extParam);
1847 case T_FunctionScan:
1848 finalize_primnode(((FunctionScan *) plan)->funcexpr,
1853 finalize_primnode((Node *) ((ValuesScan *) plan)->values_lists,
1859 bms_add_member(context.paramids,
1860 ((CteScan *) plan)->cteParam);
1863 case T_WorkTableScan:
1865 bms_add_member(context.paramids,
1866 ((WorkTableScan *) plan)->wtParam);
1873 foreach(l, ((Append *) plan)->appendplans)
1876 bms_add_members(context.paramids,
1888 foreach(l, ((BitmapAnd *) plan)->bitmapplans)
1891 bms_add_members(context.paramids,
1903 foreach(l, ((BitmapOr *) plan)->bitmapplans)
1906 bms_add_members(context.paramids,
1915 finalize_primnode((Node *) ((Join *) plan)->joinqual,
1920 finalize_primnode((Node *) ((Join *) plan)->joinqual,
1922 finalize_primnode((Node *) ((MergeJoin *) plan)->mergeclauses,
1927 finalize_primnode((Node *) ((Join *) plan)->joinqual,
1929 finalize_primnode((Node *) ((HashJoin *) plan)->hashclauses,
1934 finalize_primnode(((Limit *) plan)->limitOffset,
1936 finalize_primnode(((Limit *) plan)->limitCount,
1940 case T_RecursiveUnion:
1953 elog(ERROR, "unrecognized node type: %d",
1954 (int) nodeTag(plan));
1957 /* Process left and right child plans, if any */
1958 context.paramids = bms_add_members(context.paramids,
1963 context.paramids = bms_add_members(context.paramids,
1969 * RecursiveUnion *generates* its worktable param, so don't bubble that up
1971 if (IsA(plan, RecursiveUnion))
1973 context.paramids = bms_del_member(context.paramids,
1974 ((RecursiveUnion *) plan)->wtParam);
1977 /* Now we have all the paramids */
1979 if (!bms_is_subset(context.paramids, valid_params))
1980 elog(ERROR, "plan should not reference subplan's variable");
1983 * Note: by definition, extParam and allParam should have the same value
1984 * in any plan node that doesn't have child initPlans. We set them
1985 * equal here, and later SS_finalize_plan will update them properly
1986 * in node(s) that it attaches initPlans to.
1988 * For speed at execution time, make sure extParam/allParam are actually
1989 * NULL if they are empty sets.
1991 if (bms_is_empty(context.paramids))
1993 plan->extParam = NULL;
1994 plan->allParam = NULL;
1998 plan->extParam = context.paramids;
1999 plan->allParam = bms_copy(context.paramids);
2002 return plan->allParam;
2006 * finalize_primnode: add IDs of all PARAM_EXEC params appearing in the given
2007 * expression tree to the result set.
2010 finalize_primnode(Node *node, finalize_primnode_context *context)
2014 if (IsA(node, Param))
2016 if (((Param *) node)->paramkind == PARAM_EXEC)
2018 int paramid = ((Param *) node)->paramid;
2020 context->paramids = bms_add_member(context->paramids, paramid);
2022 return false; /* no more to do here */
2024 if (IsA(node, SubPlan))
2026 SubPlan *subplan = (SubPlan *) node;
2027 Plan *plan = planner_subplan_get_plan(context->root, subplan);
2029 Bitmapset *subparamids;
2031 /* Recurse into the testexpr, but not into the Plan */
2032 finalize_primnode(subplan->testexpr, context);
2035 * Remove any param IDs of output parameters of the subplan that were
2036 * referenced in the testexpr. These are not interesting for
2037 * parameter change signaling since we always re-evaluate the subplan.
2038 * Note that this wouldn't work too well if there might be uses of the
2039 * same param IDs elsewhere in the plan, but that can't happen because
2040 * generate_new_param never tries to merge params.
2042 foreach(lc, subplan->paramIds)
2044 context->paramids = bms_del_member(context->paramids,
2048 /* Also examine args list */
2049 finalize_primnode((Node *) subplan->args, context);
2052 * Add params needed by the subplan to paramids, but excluding those
2053 * we will pass down to it.
2055 subparamids = bms_copy(plan->extParam);
2056 foreach(lc, subplan->parParam)
2058 subparamids = bms_del_member(subparamids, lfirst_int(lc));
2060 context->paramids = bms_join(context->paramids, subparamids);
2062 return false; /* no more to do here */
2064 return expression_tree_walker(node, finalize_primnode,
2069 * SS_make_initplan_from_plan - given a plan tree, make it an InitPlan
2071 * The plan is expected to return a scalar value of the indicated type.
2072 * We build an EXPR_SUBLINK SubPlan node and put it into the initplan
2073 * list for the current query level. A Param that represents the initplan's
2074 * output is returned.
2076 * We assume the plan hasn't been put through SS_finalize_plan.
2079 SS_make_initplan_from_plan(PlannerInfo *root, Plan *plan,
2080 Oid resulttype, int32 resulttypmod)
2086 * We must run SS_finalize_plan(), since that's normally done before a
2087 * subplan gets put into the initplan list. Tell it not to attach any
2088 * pre-existing initplans to this one, since they are siblings not
2089 * children of this initplan. (This is something else that could perhaps
2090 * be cleaner if we did extParam/allParam processing in setrefs.c instead
2091 * of here? See notes for materialize_finished_plan.)
2095 * Build extParam/allParam sets for plan nodes.
2097 SS_finalize_plan(root, plan, false);
2100 * Add the subplan and its rtable to the global lists.
2102 root->glob->subplans = lappend(root->glob->subplans,
2104 root->glob->subrtables = lappend(root->glob->subrtables,
2105 root->parse->rtable);
2108 * Create a SubPlan node and add it to the outer list of InitPlans.
2109 * Note it has to appear after any other InitPlans it might depend on
2110 * (see comments in ExecReScan).
2112 node = makeNode(SubPlan);
2113 node->subLinkType = EXPR_SUBLINK;
2114 node->firstColType = get_first_col_type(plan);
2115 node->plan_id = list_length(root->glob->subplans);
2117 root->init_plans = lappend(root->init_plans, node);
2120 * The node can't have any inputs (since it's an initplan), so the
2121 * parParam and args lists remain empty.
2124 cost_subplan(root, node, plan);
2127 * Make a Param that will be the subplan's output.
2129 prm = generate_new_param(root, resulttype, resulttypmod);
2130 node->setParam = list_make1_int(prm->paramid);