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.154 2009/10/10 01:43:49 tgl Exp $
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 "parser/parsetree.h"
31 #include "rewrite/rewriteManip.h"
32 #include "utils/builtins.h"
33 #include "utils/lsyscache.h"
34 #include "utils/syscache.h"
37 typedef struct convert_testexpr_context
40 List *subst_nodes; /* Nodes to substitute for Params */
41 } convert_testexpr_context;
43 typedef struct process_sublinks_context
47 } process_sublinks_context;
49 typedef struct finalize_primnode_context
52 Bitmapset *paramids; /* Non-local PARAM_EXEC paramids found */
53 } finalize_primnode_context;
56 static Node *build_subplan(PlannerInfo *root, Plan *plan, List *rtable,
57 SubLinkType subLinkType, Node *testexpr,
58 bool adjust_testexpr, bool unknownEqFalse);
59 static List *generate_subquery_params(PlannerInfo *root, List *tlist,
61 static List *generate_subquery_vars(PlannerInfo *root, List *tlist,
63 static Node *convert_testexpr(PlannerInfo *root,
66 static Node *convert_testexpr_mutator(Node *node,
67 convert_testexpr_context *context);
68 static bool subplan_is_hashable(Plan *plan);
69 static bool testexpr_is_hashable(Node *testexpr);
70 static bool hash_ok_operator(OpExpr *expr);
71 static bool simplify_EXISTS_query(Query *query);
72 static Query *convert_EXISTS_to_ANY(PlannerInfo *root, Query *subselect,
73 Node **testexpr, List **paramIds);
74 static Node *replace_correlation_vars_mutator(Node *node, PlannerInfo *root);
75 static Node *process_sublinks_mutator(Node *node,
76 process_sublinks_context *context);
77 static Bitmapset *finalize_plan(PlannerInfo *root,
79 Bitmapset *valid_params);
80 static bool finalize_primnode(Node *node, finalize_primnode_context *context);
84 * Generate a Param node to replace the given Var,
85 * which is expected to have varlevelsup > 0 (ie, it is not local).
88 replace_outer_var(PlannerInfo *root, Var *var)
92 PlannerParamItem *pitem;
96 Assert(var->varlevelsup > 0 && var->varlevelsup < root->query_level);
97 abslevel = root->query_level - var->varlevelsup;
100 * If there's already a paramlist entry for this same Var, just use it.
101 * NOTE: in sufficiently complex querytrees, it is possible for the same
102 * varno/abslevel to refer to different RTEs in different parts of the
103 * parsetree, so that different fields might end up sharing the same Param
104 * number. As long as we check the vartype/typmod as well, I believe that
105 * this sort of aliasing will cause no trouble. The correct field should
106 * get stored into the Param slot at execution in each part of the tree.
109 foreach(ppl, root->glob->paramlist)
111 pitem = (PlannerParamItem *) lfirst(ppl);
112 if (pitem->abslevel == abslevel && IsA(pitem->item, Var))
114 Var *pvar = (Var *) pitem->item;
116 if (pvar->varno == var->varno &&
117 pvar->varattno == var->varattno &&
118 pvar->vartype == var->vartype &&
119 pvar->vartypmod == var->vartypmod)
127 /* Nope, so make a new one */
128 var = (Var *) copyObject(var);
129 var->varlevelsup = 0;
131 pitem = makeNode(PlannerParamItem);
132 pitem->item = (Node *) var;
133 pitem->abslevel = abslevel;
135 root->glob->paramlist = lappend(root->glob->paramlist, pitem);
136 /* i is already the correct index for the new item */
139 retval = makeNode(Param);
140 retval->paramkind = PARAM_EXEC;
142 retval->paramtype = var->vartype;
143 retval->paramtypmod = var->vartypmod;
144 retval->location = -1;
150 * Generate a Param node to replace the given Aggref
151 * which is expected to have agglevelsup > 0 (ie, it is not local).
154 replace_outer_agg(PlannerInfo *root, Aggref *agg)
157 PlannerParamItem *pitem;
161 Assert(agg->agglevelsup > 0 && agg->agglevelsup < root->query_level);
162 abslevel = root->query_level - agg->agglevelsup;
165 * It does not seem worthwhile to try to match duplicate outer aggs. Just
166 * make a new slot every time.
168 agg = (Aggref *) copyObject(agg);
169 IncrementVarSublevelsUp((Node *) agg, -((int) agg->agglevelsup), 0);
170 Assert(agg->agglevelsup == 0);
172 pitem = makeNode(PlannerParamItem);
173 pitem->item = (Node *) agg;
174 pitem->abslevel = abslevel;
176 root->glob->paramlist = lappend(root->glob->paramlist, pitem);
177 i = list_length(root->glob->paramlist) - 1;
179 retval = makeNode(Param);
180 retval->paramkind = PARAM_EXEC;
182 retval->paramtype = agg->aggtype;
183 retval->paramtypmod = -1;
184 retval->location = -1;
190 * Generate a new Param node that will not conflict with any other.
192 * This is used to allocate PARAM_EXEC slots for subplan outputs.
195 generate_new_param(PlannerInfo *root, Oid paramtype, int32 paramtypmod)
198 PlannerParamItem *pitem;
200 retval = makeNode(Param);
201 retval->paramkind = PARAM_EXEC;
202 retval->paramid = list_length(root->glob->paramlist);
203 retval->paramtype = paramtype;
204 retval->paramtypmod = paramtypmod;
205 retval->location = -1;
207 pitem = makeNode(PlannerParamItem);
208 pitem->item = (Node *) retval;
209 pitem->abslevel = root->query_level;
211 root->glob->paramlist = lappend(root->glob->paramlist, pitem);
217 * Assign a (nonnegative) PARAM_EXEC ID for a recursive query's worktable.
220 SS_assign_worktable_param(PlannerInfo *root)
224 /* We generate a Param of datatype INTERNAL */
225 param = generate_new_param(root, INTERNALOID, -1);
226 /* ... but the caller only cares about its ID */
227 return param->paramid;
231 * Get the datatype of the first column of the plan's output.
233 * This is stored for ARRAY_SUBLINK execution and for exprType()/exprTypmod(),
234 * which have no way to get at the plan associated with a SubPlan node.
235 * We really only need the info for EXPR_SUBLINK and ARRAY_SUBLINK subplans,
236 * but for consistency we save it always.
239 get_first_col_type(Plan *plan, Oid *coltype, int32 *coltypmod)
241 /* In cases such as EXISTS, tlist might be empty; arbitrarily use VOID */
242 if (plan->targetlist)
244 TargetEntry *tent = (TargetEntry *) linitial(plan->targetlist);
246 Assert(IsA(tent, TargetEntry));
249 *coltype = exprType((Node *) tent->expr);
250 *coltypmod = exprTypmod((Node *) tent->expr);
259 * Convert a SubLink (as created by the parser) into a SubPlan.
261 * We are given the SubLink's contained query, type, and testexpr. We are
262 * also told if this expression appears at top level of a WHERE/HAVING qual.
264 * Note: we assume that the testexpr has been AND/OR flattened (actually,
265 * it's been through eval_const_expressions), but not converted to
266 * implicit-AND form; and any SubLinks in it should already have been
267 * converted to SubPlans. The subquery is as yet untouched, however.
269 * The result is whatever we need to substitute in place of the SubLink
270 * node in the executable expression. This will be either the SubPlan
271 * node (if we have to do the subplan as a subplan), or a Param node
272 * representing the result of an InitPlan, or a row comparison expression
273 * tree containing InitPlan Param nodes.
276 make_subplan(PlannerInfo *root, Query *orig_subquery, SubLinkType subLinkType,
277 Node *testexpr, bool isTopQual)
280 bool simple_exists = false;
281 double tuple_fraction;
283 PlannerInfo *subroot;
287 * Copy the source Query node. This is a quick and dirty kluge to resolve
288 * the fact that the parser can generate trees with multiple links to the
289 * same sub-Query node, but the planner wants to scribble on the Query.
290 * Try to clean this up when we do querytree redesign...
292 subquery = (Query *) copyObject(orig_subquery);
295 * If it's an EXISTS subplan, we might be able to simplify it.
297 if (subLinkType == EXISTS_SUBLINK)
298 simple_exists = simplify_EXISTS_query(subquery);
301 * For an EXISTS subplan, tell lower-level planner to expect that only the
302 * first tuple will be retrieved. For ALL and ANY subplans, we will be
303 * able to stop evaluating if the test condition fails or matches, so very
304 * often not all the tuples will be retrieved; for lack of a better idea,
305 * specify 50% retrieval. For EXPR and ROWCOMPARE subplans, use default
306 * behavior (we're only expecting one row out, anyway).
308 * NOTE: if you change these numbers, also change cost_subplan() in
311 * XXX If an ANY subplan is uncorrelated, build_subplan may decide to hash
312 * its output. In that case it would've been better to specify full
313 * retrieval. At present, however, we can only check hashability after
314 * we've made the subplan :-(. (Determining whether it'll fit in work_mem
315 * is the really hard part.) Therefore, we don't want to be too
316 * optimistic about the percentage of tuples retrieved, for fear of
317 * selecting a plan that's bad for the materialization case.
319 if (subLinkType == EXISTS_SUBLINK)
320 tuple_fraction = 1.0; /* just like a LIMIT 1 */
321 else if (subLinkType == ALL_SUBLINK ||
322 subLinkType == ANY_SUBLINK)
323 tuple_fraction = 0.5; /* 50% */
325 tuple_fraction = 0.0; /* default behavior */
328 * Generate the plan for the subquery.
330 plan = subquery_planner(root->glob, subquery,
332 false, tuple_fraction,
335 /* And convert to SubPlan or InitPlan format. */
336 result = build_subplan(root, plan, subroot->parse->rtable,
337 subLinkType, testexpr, true, isTopQual);
340 * If it's a correlated EXISTS with an unimportant targetlist, we might be
341 * able to transform it to the equivalent of an IN and then implement it
342 * by hashing. We don't have enough information yet to tell which way is
343 * likely to be better (it depends on the expected number of executions of
344 * the EXISTS qual, and we are much too early in planning the outer query
345 * to be able to guess that). So we generate both plans, if possible, and
346 * leave it to the executor to decide which to use.
348 if (simple_exists && IsA(result, SubPlan))
353 /* Make a second copy of the original subquery */
354 subquery = (Query *) copyObject(orig_subquery);
355 /* and re-simplify */
356 simple_exists = simplify_EXISTS_query(subquery);
357 Assert(simple_exists);
358 /* See if it can be converted to an ANY query */
359 subquery = convert_EXISTS_to_ANY(root, subquery,
360 &newtestexpr, ¶mIds);
363 /* Generate the plan for the ANY subquery; we'll need all rows */
364 plan = subquery_planner(root->glob, subquery,
369 /* Now we can check if it'll fit in work_mem */
370 if (subplan_is_hashable(plan))
373 AlternativeSubPlan *asplan;
375 /* OK, convert to SubPlan format. */
376 hashplan = (SubPlan *) build_subplan(root, plan,
377 subroot->parse->rtable,
378 ANY_SUBLINK, newtestexpr,
380 /* Check we got what we expected */
381 Assert(IsA(hashplan, SubPlan));
382 Assert(hashplan->parParam == NIL);
383 Assert(hashplan->useHashTable);
384 /* build_subplan won't have filled in paramIds */
385 hashplan->paramIds = paramIds;
387 /* Leave it to the executor to decide which plan to use */
388 asplan = makeNode(AlternativeSubPlan);
389 asplan->subplans = list_make2(result, hashplan);
390 result = (Node *) asplan;
399 * Build a SubPlan node given the raw inputs --- subroutine for make_subplan
401 * Returns either the SubPlan, or an expression using initplan output Params,
402 * as explained in the comments for make_subplan.
405 build_subplan(PlannerInfo *root, Plan *plan, List *rtable,
406 SubLinkType subLinkType, Node *testexpr,
407 bool adjust_testexpr, bool unknownEqFalse)
416 * Initialize the SubPlan node. Note plan_id, plan_name, and cost fields
417 * are set further down.
419 splan = makeNode(SubPlan);
420 splan->subLinkType = subLinkType;
421 splan->testexpr = NULL;
422 splan->paramIds = NIL;
423 get_first_col_type(plan, &splan->firstColType, &splan->firstColTypmod);
424 splan->useHashTable = false;
425 splan->unknownEqFalse = unknownEqFalse;
426 splan->setParam = NIL;
427 splan->parParam = NIL;
431 * Make parParam and args lists of param IDs and expressions that current
432 * query level will pass to this child plan.
434 tmpset = bms_copy(plan->extParam);
435 while ((paramid = bms_first_member(tmpset)) >= 0)
437 PlannerParamItem *pitem = list_nth(root->glob->paramlist, paramid);
439 if (pitem->abslevel == root->query_level)
444 * The Var or Aggref has already been adjusted to have the correct
445 * varlevelsup or agglevelsup. We probably don't even need to
446 * copy it again, but be safe.
448 arg = copyObject(pitem->item);
451 * If it's an Aggref, its arguments might contain SubLinks, which
452 * have not yet been processed. Do that now.
454 if (IsA(arg, Aggref))
455 arg = SS_process_sublinks(root, arg, false);
457 splan->parParam = lappend_int(splan->parParam, paramid);
458 splan->args = lappend(splan->args, arg);
464 * Un-correlated or undirect correlated plans of EXISTS, EXPR, ARRAY, or
465 * ROWCOMPARE types can be used as initPlans. For EXISTS, EXPR, or ARRAY,
466 * we just produce a Param referring to the result of evaluating the
467 * initPlan. For ROWCOMPARE, we must modify the testexpr tree to contain
468 * PARAM_EXEC Params instead of the PARAM_SUBLINK Params emitted by the
471 if (splan->parParam == NIL && subLinkType == EXISTS_SUBLINK)
475 Assert(testexpr == NULL);
476 prm = generate_new_param(root, BOOLOID, -1);
477 splan->setParam = list_make1_int(prm->paramid);
479 result = (Node *) prm;
481 else if (splan->parParam == NIL && subLinkType == EXPR_SUBLINK)
483 TargetEntry *te = linitial(plan->targetlist);
486 Assert(!te->resjunk);
487 Assert(testexpr == NULL);
488 prm = generate_new_param(root,
489 exprType((Node *) te->expr),
490 exprTypmod((Node *) te->expr));
491 splan->setParam = list_make1_int(prm->paramid);
493 result = (Node *) prm;
495 else if (splan->parParam == NIL && subLinkType == ARRAY_SUBLINK)
497 TargetEntry *te = linitial(plan->targetlist);
501 Assert(!te->resjunk);
502 Assert(testexpr == NULL);
503 arraytype = get_array_type(exprType((Node *) te->expr));
504 if (!OidIsValid(arraytype))
505 elog(ERROR, "could not find array type for datatype %s",
506 format_type_be(exprType((Node *) te->expr)));
507 prm = generate_new_param(root,
509 exprTypmod((Node *) te->expr));
510 splan->setParam = list_make1_int(prm->paramid);
512 result = (Node *) prm;
514 else if (splan->parParam == NIL && subLinkType == ROWCOMPARE_SUBLINK)
516 /* Adjust the Params */
519 Assert(testexpr != NULL);
520 params = generate_subquery_params(root,
523 result = convert_testexpr(root,
526 splan->setParam = list_copy(splan->paramIds);
530 * The executable expression is returned to become part of the outer
531 * plan's expression tree; it is not kept in the initplan node.
537 * Adjust the Params in the testexpr, unless caller said it's not
540 if (testexpr && adjust_testexpr)
544 params = generate_subquery_params(root,
547 splan->testexpr = convert_testexpr(root,
552 splan->testexpr = testexpr;
555 * We can't convert subplans of ALL_SUBLINK or ANY_SUBLINK types to
556 * initPlans, even when they are uncorrelated or undirect correlated,
557 * because we need to scan the output of the subplan for each outer
558 * tuple. But if it's a not-direct-correlated IN (= ANY) test, we
559 * might be able to use a hashtable to avoid comparing all the tuples.
561 if (subLinkType == ANY_SUBLINK &&
562 splan->parParam == NIL &&
563 subplan_is_hashable(plan) &&
564 testexpr_is_hashable(splan->testexpr))
565 splan->useHashTable = true;
568 * Otherwise, we have the option to tack a Material node onto the top
569 * of the subplan, to reduce the cost of reading it repeatedly. This
570 * is pointless for a direct-correlated subplan, since we'd have to
571 * recompute its results each time anyway. For uncorrelated/undirect
572 * correlated subplans, we add Material unless the subplan's top plan
573 * node would materialize its output anyway.
575 else if (splan->parParam == NIL &&
576 !ExecMaterializesOutput(nodeTag(plan)))
577 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 /* Label the subplan for EXPLAIN purposes */
610 splan->plan_name = palloc(32 + 12 * list_length(splan->setParam));
611 sprintf(splan->plan_name, "InitPlan %d (returns ", splan->plan_id);
612 offset = strlen(splan->plan_name);
613 foreach(lc, splan->setParam)
615 sprintf(splan->plan_name + offset, "$%d%s",
617 lnext(lc) ? "," : "");
618 offset += strlen(splan->plan_name + offset);
620 sprintf(splan->plan_name + offset, ")");
624 splan->plan_name = palloc(32);
625 sprintf(splan->plan_name, "SubPlan %d", splan->plan_id);
628 /* Lastly, fill in the cost estimates for use later */
629 cost_subplan(root, splan, plan);
635 * generate_subquery_params: build a list of Params representing the output
636 * columns of a sublink's sub-select, given the sub-select's targetlist.
638 * We also return an integer list of the paramids of the Params.
641 generate_subquery_params(PlannerInfo *root, List *tlist, List **paramIds)
650 TargetEntry *tent = (TargetEntry *) lfirst(lc);
656 param = generate_new_param(root,
657 exprType((Node *) tent->expr),
658 exprTypmod((Node *) tent->expr));
659 result = lappend(result, param);
660 ids = lappend_int(ids, param->paramid);
668 * generate_subquery_vars: build a list of Vars representing the output
669 * columns of a sublink's sub-select, given the sub-select's targetlist.
670 * The Vars have the specified varno (RTE index).
673 generate_subquery_vars(PlannerInfo *root, List *tlist, Index varno)
681 TargetEntry *tent = (TargetEntry *) lfirst(lc);
689 exprType((Node *) tent->expr),
690 exprTypmod((Node *) tent->expr),
692 result = lappend(result, var);
699 * convert_testexpr: convert the testexpr given by the parser into
700 * actually executable form. This entails replacing PARAM_SUBLINK Params
701 * with Params or Vars representing the results of the sub-select. The
702 * nodes to be substituted are passed in as the List result from
703 * generate_subquery_params or generate_subquery_vars.
705 * The given testexpr has already been recursively processed by
706 * process_sublinks_mutator. Hence it can no longer contain any
707 * PARAM_SUBLINK Params for lower SubLink nodes; we can safely assume that
708 * any we find are for our own level of SubLink.
711 convert_testexpr(PlannerInfo *root,
715 convert_testexpr_context context;
718 context.subst_nodes = subst_nodes;
719 return convert_testexpr_mutator(testexpr, &context);
723 convert_testexpr_mutator(Node *node,
724 convert_testexpr_context *context)
728 if (IsA(node, Param))
730 Param *param = (Param *) node;
732 if (param->paramkind == PARAM_SUBLINK)
734 if (param->paramid <= 0 ||
735 param->paramid > list_length(context->subst_nodes))
736 elog(ERROR, "unexpected PARAM_SUBLINK ID: %d", param->paramid);
739 * We copy the list item to avoid having doubly-linked
740 * substructure in the modified parse tree. This is probably
741 * unnecessary when it's a Param, but be safe.
743 return (Node *) copyObject(list_nth(context->subst_nodes,
744 param->paramid - 1));
747 return expression_tree_mutator(node,
748 convert_testexpr_mutator,
753 * subplan_is_hashable: can we implement an ANY subplan by hashing?
756 subplan_is_hashable(Plan *plan)
758 double subquery_size;
761 * The estimated size of the subquery result must fit in work_mem. (Note:
762 * we use sizeof(HeapTupleHeaderData) here even though the tuples will
763 * actually be stored as MinimalTuples; this provides some fudge factor
764 * for hashtable overhead.)
766 subquery_size = plan->plan_rows *
767 (MAXALIGN(plan->plan_width) + MAXALIGN(sizeof(HeapTupleHeaderData)));
768 if (subquery_size > work_mem * 1024L)
775 * testexpr_is_hashable: is an ANY SubLink's test expression hashable?
778 testexpr_is_hashable(Node *testexpr)
781 * The testexpr must be a single OpExpr, or an AND-clause containing only
784 * The combining operators must be hashable and strict. The need for
785 * hashability is obvious, since we want to use hashing. Without
786 * strictness, behavior in the presence of nulls is too unpredictable. We
787 * actually must assume even more than plain strictness: they can't yield
788 * NULL for non-null inputs, either (see nodeSubplan.c). However, hash
789 * indexes and hash joins assume that too.
791 if (testexpr && IsA(testexpr, OpExpr))
793 if (hash_ok_operator((OpExpr *) testexpr))
796 else if (and_clause(testexpr))
800 foreach(l, ((BoolExpr *) testexpr)->args)
802 Node *andarg = (Node *) lfirst(l);
804 if (!IsA(andarg, OpExpr))
806 if (!hash_ok_operator((OpExpr *) andarg))
816 hash_ok_operator(OpExpr *expr)
818 Oid opid = expr->opno;
820 Form_pg_operator optup;
822 /* quick out if not a binary operator */
823 if (list_length(expr->args) != 2)
825 /* else must look up the operator properties */
826 tup = SearchSysCache(OPEROID,
827 ObjectIdGetDatum(opid),
829 if (!HeapTupleIsValid(tup))
830 elog(ERROR, "cache lookup failed for operator %u", opid);
831 optup = (Form_pg_operator) GETSTRUCT(tup);
832 if (!optup->oprcanhash || !func_strict(optup->oprcode))
834 ReleaseSysCache(tup);
837 ReleaseSysCache(tup);
843 * SS_process_ctes: process a query's WITH list
845 * We plan each interesting WITH item and convert it to an initplan.
846 * A side effect is to fill in root->cte_plan_ids with a list that
847 * parallels root->parse->cteList and provides the subplan ID for
848 * each CTE's initplan.
851 SS_process_ctes(PlannerInfo *root)
855 Assert(root->cte_plan_ids == NIL);
857 foreach(lc, root->parse->cteList)
859 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
862 PlannerInfo *subroot;
869 * Ignore CTEs that are not actually referenced anywhere.
871 if (cte->cterefcount == 0)
873 /* Make a dummy entry in cte_plan_ids */
874 root->cte_plan_ids = lappend_int(root->cte_plan_ids, -1);
879 * Copy the source Query node. Probably not necessary, but let's keep
880 * this similar to make_subplan.
882 subquery = (Query *) copyObject(cte->ctequery);
885 * Generate the plan for the CTE query. Always plan for full
886 * retrieval --- we don't have enough info to predict otherwise.
888 plan = subquery_planner(root->glob, subquery,
890 cte->cterecursive, 0.0,
894 * Make a SubPlan node for it. This is just enough unlike
895 * build_subplan that we can't share code.
897 * Note plan_id, plan_name, and cost fields are set further down.
899 splan = makeNode(SubPlan);
900 splan->subLinkType = CTE_SUBLINK;
901 splan->testexpr = NULL;
902 splan->paramIds = NIL;
903 get_first_col_type(plan, &splan->firstColType, &splan->firstColTypmod);
904 splan->useHashTable = false;
905 splan->unknownEqFalse = false;
906 splan->setParam = NIL;
907 splan->parParam = NIL;
911 * Make parParam and args lists of param IDs and expressions that
912 * current query level will pass to this child plan. Even though this
913 * is an initplan, there could be side-references to earlier
914 * initplan's outputs, specifically their CTE output parameters.
916 tmpset = bms_copy(plan->extParam);
917 while ((paramid = bms_first_member(tmpset)) >= 0)
919 PlannerParamItem *pitem = list_nth(root->glob->paramlist, paramid);
921 if (pitem->abslevel == root->query_level)
923 prm = (Param *) pitem->item;
924 if (!IsA(prm, Param) ||
925 prm->paramtype != INTERNALOID)
926 elog(ERROR, "bogus local parameter passed to WITH query");
928 splan->parParam = lappend_int(splan->parParam, paramid);
929 splan->args = lappend(splan->args, copyObject(prm));
935 * Assign a param to represent the query output. We only really care
936 * about reserving a parameter ID number.
938 prm = generate_new_param(root, INTERNALOID, -1);
939 splan->setParam = list_make1_int(prm->paramid);
942 * Add the subplan and its rtable to the global lists.
944 root->glob->subplans = lappend(root->glob->subplans, plan);
945 root->glob->subrtables = lappend(root->glob->subrtables,
946 subroot->parse->rtable);
947 splan->plan_id = list_length(root->glob->subplans);
949 root->init_plans = lappend(root->init_plans, splan);
951 root->cte_plan_ids = lappend_int(root->cte_plan_ids, splan->plan_id);
953 /* Label the subplan for EXPLAIN purposes */
954 splan->plan_name = palloc(4 + strlen(cte->ctename) + 1);
955 sprintf(splan->plan_name, "CTE %s", cte->ctename);
957 /* Lastly, fill in the cost estimates for use later */
958 cost_subplan(root, splan, plan);
963 * convert_ANY_sublink_to_join: try to convert an ANY SubLink to a join
965 * The caller has found an ANY SubLink at the top level of one of the query's
966 * qual clauses, but has not checked the properties of the SubLink further.
967 * Decide whether it is appropriate to process this SubLink in join style.
968 * If so, form a JoinExpr and return it. Return NULL if the SubLink cannot
969 * be converted to a join.
971 * The only non-obvious input parameter is available_rels: this is the set
972 * of query rels that can safely be referenced in the sublink expression.
973 * (We must restrict this to avoid changing the semantics when a sublink
974 * is present in an outer join's ON qual.) The conversion must fail if
975 * the converted qual would reference any but these parent-query relids.
977 * On success, the returned JoinExpr has larg = NULL and rarg = the jointree
978 * item representing the pulled-up subquery. The caller must set larg to
979 * represent the relation(s) on the lefthand side of the new join, and insert
980 * the JoinExpr into the upper query's jointree at an appropriate place
981 * (typically, where the lefthand relation(s) had been). Note that the
982 * passed-in SubLink must also be removed from its original position in the
983 * query quals, since the quals of the returned JoinExpr replace it.
984 * (Notionally, we replace the SubLink with a constant TRUE, then elide the
985 * redundant constant from the qual.)
987 * Side effects of a successful conversion include adding the SubLink's
988 * subselect to the query's rangetable, so that it can be referenced in
989 * the JoinExpr's rarg.
992 convert_ANY_sublink_to_join(PlannerInfo *root, SubLink *sublink,
993 Relids available_rels)
996 Query *parse = root->parse;
997 Query *subselect = (Query *) sublink->subselect;
1002 List *subquery_vars;
1005 Assert(sublink->subLinkType == ANY_SUBLINK);
1008 * The sub-select must not refer to any Vars of the parent query. (Vars of
1009 * higher levels should be okay, though.)
1011 if (contain_vars_of_level((Node *) subselect, 1))
1015 * The test expression must contain some Vars of the parent query, else
1016 * it's not gonna be a join. (Note that it won't have Vars referring to
1017 * the subquery, rather Params.)
1019 upper_varnos = pull_varnos(sublink->testexpr);
1020 if (bms_is_empty(upper_varnos))
1024 * However, it can't refer to anything outside available_rels.
1026 if (!bms_is_subset(upper_varnos, available_rels))
1030 * The combining operators and left-hand expressions mustn't be volatile.
1032 if (contain_volatile_functions(sublink->testexpr))
1036 * Okay, pull up the sub-select into upper range table.
1038 * We rely here on the assumption that the outer query has no references
1039 * to the inner (necessarily true, other than the Vars that we build
1040 * below). Therefore this is a lot easier than what pull_up_subqueries has
1043 rte = addRangeTableEntryForSubquery(NULL,
1045 makeAlias("ANY_subquery", NIL),
1047 parse->rtable = lappend(parse->rtable, rte);
1048 rtindex = list_length(parse->rtable);
1051 * Form a RangeTblRef for the pulled-up sub-select.
1053 rtr = makeNode(RangeTblRef);
1054 rtr->rtindex = rtindex;
1057 * Build a list of Vars representing the subselect outputs.
1059 subquery_vars = generate_subquery_vars(root,
1060 subselect->targetList,
1064 * Build the new join's qual expression, replacing Params with these Vars.
1066 quals = convert_testexpr(root, sublink->testexpr, subquery_vars);
1069 * And finally, build the JoinExpr node.
1071 result = makeNode(JoinExpr);
1072 result->jointype = JOIN_SEMI;
1073 result->isNatural = false;
1074 result->larg = NULL; /* caller must fill this in */
1075 result->rarg = (Node *) rtr;
1076 result->usingClause = NIL;
1077 result->quals = quals;
1078 result->alias = NULL;
1079 result->rtindex = 0; /* we don't need an RTE for it */
1085 * convert_EXISTS_sublink_to_join: try to convert an EXISTS SubLink to a join
1087 * The API of this function is identical to convert_ANY_sublink_to_join's,
1088 * except that we also support the case where the caller has found NOT EXISTS,
1089 * so we need an additional input parameter "under_not".
1092 convert_EXISTS_sublink_to_join(PlannerInfo *root, SubLink *sublink,
1093 bool under_not, Relids available_rels)
1096 Query *parse = root->parse;
1097 Query *subselect = (Query *) sublink->subselect;
1101 Relids clause_varnos;
1102 Relids upper_varnos;
1104 Assert(sublink->subLinkType == EXISTS_SUBLINK);
1107 * Copy the subquery so we can modify it safely (see comments in
1110 subselect = (Query *) copyObject(subselect);
1113 * See if the subquery can be simplified based on the knowledge that it's
1114 * being used in EXISTS(). If we aren't able to get rid of its
1115 * targetlist, we have to fail, because the pullup operation leaves us
1116 * with noplace to evaluate the targetlist.
1118 if (!simplify_EXISTS_query(subselect))
1122 * The subquery must have a nonempty jointree, else we won't have a join.
1124 if (subselect->jointree->fromlist == NIL)
1128 * Separate out the WHERE clause. (We could theoretically also remove
1129 * top-level plain JOIN/ON clauses, but it's probably not worth the
1132 whereClause = subselect->jointree->quals;
1133 subselect->jointree->quals = NULL;
1136 * The rest of the sub-select must not refer to any Vars of the parent
1137 * query. (Vars of higher levels should be okay, though.)
1139 if (contain_vars_of_level((Node *) subselect, 1))
1143 * On the other hand, the WHERE clause must contain some Vars of the
1144 * parent query, else it's not gonna be a join.
1146 if (!contain_vars_of_level(whereClause, 1))
1150 * We don't risk optimizing if the WHERE clause is volatile, either.
1152 if (contain_volatile_functions(whereClause))
1156 * Prepare to pull up the sub-select into top range table.
1158 * We rely here on the assumption that the outer query has no references
1159 * to the inner (necessarily true). Therefore this is a lot easier than
1160 * what pull_up_subqueries has to go through.
1162 * In fact, it's even easier than what convert_ANY_sublink_to_join has to
1163 * do. The machinations of simplify_EXISTS_query ensured that there is
1164 * nothing interesting in the subquery except an rtable and jointree, and
1165 * even the jointree FromExpr no longer has quals. So we can just append
1166 * the rtable to our own and use the FromExpr in our jointree. But first,
1167 * adjust all level-zero varnos in the subquery to account for the rtable
1170 rtoffset = list_length(parse->rtable);
1171 OffsetVarNodes((Node *) subselect, rtoffset, 0);
1172 OffsetVarNodes(whereClause, rtoffset, 0);
1175 * Upper-level vars in subquery will now be one level closer to their
1176 * parent than before; in particular, anything that had been level 1
1177 * becomes level zero.
1179 IncrementVarSublevelsUp((Node *) subselect, -1, 1);
1180 IncrementVarSublevelsUp(whereClause, -1, 1);
1183 * Now that the WHERE clause is adjusted to match the parent query
1184 * environment, we can easily identify all the level-zero rels it uses.
1185 * The ones <= rtoffset belong to the upper query; the ones > rtoffset do
1188 clause_varnos = pull_varnos(whereClause);
1189 upper_varnos = NULL;
1190 while ((varno = bms_first_member(clause_varnos)) >= 0)
1192 if (varno <= rtoffset)
1193 upper_varnos = bms_add_member(upper_varnos, varno);
1195 bms_free(clause_varnos);
1196 Assert(!bms_is_empty(upper_varnos));
1199 * Now that we've got the set of upper-level varnos, we can make the last
1200 * check: only available_rels can be referenced.
1202 if (!bms_is_subset(upper_varnos, available_rels))
1205 /* Now we can attach the modified subquery rtable to the parent */
1206 parse->rtable = list_concat(parse->rtable, subselect->rtable);
1209 * And finally, build the JoinExpr node.
1211 result = makeNode(JoinExpr);
1212 result->jointype = under_not ? JOIN_ANTI : JOIN_SEMI;
1213 result->isNatural = false;
1214 result->larg = NULL; /* caller must fill this in */
1215 /* flatten out the FromExpr node if it's useless */
1216 if (list_length(subselect->jointree->fromlist) == 1)
1217 result->rarg = (Node *) linitial(subselect->jointree->fromlist);
1219 result->rarg = (Node *) subselect->jointree;
1220 result->usingClause = NIL;
1221 result->quals = whereClause;
1222 result->alias = NULL;
1223 result->rtindex = 0; /* we don't need an RTE for it */
1229 * simplify_EXISTS_query: remove any useless stuff in an EXISTS's subquery
1231 * The only thing that matters about an EXISTS query is whether it returns
1232 * zero or more than zero rows. Therefore, we can remove certain SQL features
1233 * that won't affect that. The only part that is really likely to matter in
1234 * typical usage is simplifying the targetlist: it's a common habit to write
1235 * "SELECT * FROM" even though there is no need to evaluate any columns.
1237 * Note: by suppressing the targetlist we could cause an observable behavioral
1238 * change, namely that any errors that might occur in evaluating the tlist
1239 * won't occur, nor will other side-effects of volatile functions. This seems
1240 * unlikely to bother anyone in practice.
1242 * Returns TRUE if was able to discard the targetlist, else FALSE.
1245 simplify_EXISTS_query(Query *query)
1248 * We don't try to simplify at all if the query uses set operations,
1249 * aggregates, HAVING, LIMIT/OFFSET, or FOR UPDATE/SHARE; none of these
1250 * seem likely in normal usage and their possible effects are complex.
1252 if (query->commandType != CMD_SELECT ||
1253 query->intoClause ||
1254 query->setOperations ||
1256 query->hasWindowFuncs ||
1257 query->havingQual ||
1258 query->limitOffset ||
1259 query->limitCount ||
1264 * Mustn't throw away the targetlist if it contains set-returning
1265 * functions; those could affect whether zero rows are returned!
1267 if (expression_returns_set((Node *) query->targetList))
1271 * Otherwise, we can throw away the targetlist, as well as any GROUP,
1272 * WINDOW, DISTINCT, and ORDER BY clauses; none of those clauses will
1273 * change a nonzero-rows result to zero rows or vice versa. (Furthermore,
1274 * since our parsetree representation of these clauses depends on the
1275 * targetlist, we'd better throw them away if we drop the targetlist.)
1277 query->targetList = NIL;
1278 query->groupClause = NIL;
1279 query->windowClause = NIL;
1280 query->distinctClause = NIL;
1281 query->sortClause = NIL;
1282 query->hasDistinctOn = false;
1288 * convert_EXISTS_to_ANY: try to convert EXISTS to a hashable ANY sublink
1290 * The subselect is expected to be a fresh copy that we can munge up,
1291 * and to have been successfully passed through simplify_EXISTS_query.
1293 * On success, the modified subselect is returned, and we store a suitable
1294 * upper-level test expression at *testexpr, plus a list of the subselect's
1295 * output Params at *paramIds. (The test expression is already Param-ified
1296 * and hence need not go through convert_testexpr, which is why we have to
1297 * deal with the Param IDs specially.)
1299 * On failure, returns NULL.
1302 convert_EXISTS_to_ANY(PlannerInfo *root, Query *subselect,
1303 Node **testexpr, List **paramIds)
1319 * Query must not require a targetlist, since we have to insert a new one.
1320 * Caller should have dealt with the case already.
1322 Assert(subselect->targetList == NIL);
1325 * Separate out the WHERE clause. (We could theoretically also remove
1326 * top-level plain JOIN/ON clauses, but it's probably not worth the
1329 whereClause = subselect->jointree->quals;
1330 subselect->jointree->quals = NULL;
1333 * The rest of the sub-select must not refer to any Vars of the parent
1334 * query. (Vars of higher levels should be okay, though.)
1336 * Note: we need not check for Aggrefs separately because we know the
1337 * sub-select is as yet unoptimized; any uplevel Aggref must therefore
1338 * contain an uplevel Var reference. This is not the case below ...
1340 if (contain_vars_of_level((Node *) subselect, 1))
1344 * We don't risk optimizing if the WHERE clause is volatile, either.
1346 if (contain_volatile_functions(whereClause))
1350 * Clean up the WHERE clause by doing const-simplification etc on it.
1351 * Aside from simplifying the processing we're about to do, this is
1352 * important for being able to pull chunks of the WHERE clause up into the
1353 * parent query. Since we are invoked partway through the parent's
1354 * preprocess_expression() work, earlier steps of preprocess_expression()
1355 * wouldn't get applied to the pulled-up stuff unless we do them here. For
1356 * the parts of the WHERE clause that get put back into the child query,
1357 * this work is partially duplicative, but it shouldn't hurt.
1359 * Note: we do not run flatten_join_alias_vars. This is OK because any
1360 * parent aliases were flattened already, and we're not going to pull any
1361 * child Vars (of any description) into the parent.
1363 * Note: passing the parent's root to eval_const_expressions is
1364 * technically wrong, but we can get away with it since only the
1365 * boundParams (if any) are used, and those would be the same in a
1368 whereClause = eval_const_expressions(root, whereClause);
1369 whereClause = (Node *) canonicalize_qual((Expr *) whereClause);
1370 whereClause = (Node *) make_ands_implicit((Expr *) whereClause);
1373 * We now have a flattened implicit-AND list of clauses, which we try to
1374 * break apart into "outervar = innervar" hash clauses. Anything that
1375 * can't be broken apart just goes back into the newWhere list. Note that
1376 * we aren't trying hard yet to ensure that we have only outer or only
1377 * inner on each side; we'll check that if we get to the end.
1379 leftargs = rightargs = opids = newWhere = NIL;
1380 foreach(lc, (List *) whereClause)
1382 OpExpr *expr = (OpExpr *) lfirst(lc);
1384 if (IsA(expr, OpExpr) &&
1385 hash_ok_operator(expr))
1387 Node *leftarg = (Node *) linitial(expr->args);
1388 Node *rightarg = (Node *) lsecond(expr->args);
1390 if (contain_vars_of_level(leftarg, 1))
1392 leftargs = lappend(leftargs, leftarg);
1393 rightargs = lappend(rightargs, rightarg);
1394 opids = lappend_oid(opids, expr->opno);
1397 if (contain_vars_of_level(rightarg, 1))
1400 * We must commute the clause to put the outer var on the
1401 * left, because the hashing code in nodeSubplan.c expects
1402 * that. This probably shouldn't ever fail, since hashable
1403 * operators ought to have commutators, but be paranoid.
1405 expr->opno = get_commutator(expr->opno);
1406 if (OidIsValid(expr->opno) && hash_ok_operator(expr))
1408 leftargs = lappend(leftargs, rightarg);
1409 rightargs = lappend(rightargs, leftarg);
1410 opids = lappend_oid(opids, expr->opno);
1413 /* If no commutator, no chance to optimize the WHERE clause */
1417 /* Couldn't handle it as a hash clause */
1418 newWhere = lappend(newWhere, expr);
1422 * If we didn't find anything we could convert, fail.
1424 if (leftargs == NIL)
1428 * There mustn't be any parent Vars or Aggs in the stuff that we intend to
1429 * put back into the child query. Note: you might think we don't need to
1430 * check for Aggs separately, because an uplevel Agg must contain an
1431 * uplevel Var in its argument. But it is possible that the uplevel Var
1432 * got optimized away by eval_const_expressions. Consider
1434 * SUM(CASE WHEN false THEN uplevelvar ELSE 0 END)
1436 if (contain_vars_of_level((Node *) newWhere, 1) ||
1437 contain_vars_of_level((Node *) rightargs, 1))
1439 if (root->parse->hasAggs &&
1440 (contain_aggs_of_level((Node *) newWhere, 1) ||
1441 contain_aggs_of_level((Node *) rightargs, 1)))
1445 * And there can't be any child Vars in the stuff we intend to pull up.
1446 * (Note: we'd need to check for child Aggs too, except we know the child
1447 * has no aggs at all because of simplify_EXISTS_query's check. The same
1448 * goes for window functions.)
1450 if (contain_vars_of_level((Node *) leftargs, 0))
1454 * Also reject sublinks in the stuff we intend to pull up. (It might be
1455 * possible to support this, but doesn't seem worth the complication.)
1457 if (contain_subplans((Node *) leftargs))
1461 * Okay, adjust the sublevelsup in the stuff we're pulling up.
1463 IncrementVarSublevelsUp((Node *) leftargs, -1, 1);
1466 * Put back any child-level-only WHERE clauses.
1469 subselect->jointree->quals = (Node *) make_ands_explicit(newWhere);
1472 * Build a new targetlist for the child that emits the expressions we
1473 * need. Concurrently, build a testexpr for the parent using Params to
1474 * reference the child outputs. (Since we generate Params directly here,
1475 * there will be no need to convert the testexpr in build_subplan.)
1477 tlist = testlist = paramids = NIL;
1479 /* there's no "for3" so we have to chase one of the lists manually */
1480 oc = list_head(opids);
1481 forboth(lc, leftargs, rc, rightargs)
1483 Node *leftarg = (Node *) lfirst(lc);
1484 Node *rightarg = (Node *) lfirst(rc);
1485 Oid opid = lfirst_oid(oc);
1489 param = generate_new_param(root,
1491 exprTypmod(rightarg));
1492 tlist = lappend(tlist,
1493 makeTargetEntry((Expr *) rightarg,
1497 testlist = lappend(testlist,
1498 make_opclause(opid, BOOLOID, false,
1499 (Expr *) leftarg, (Expr *) param));
1500 paramids = lappend_int(paramids, param->paramid);
1503 /* Put everything where it should go, and we're done */
1504 subselect->targetList = tlist;
1505 *testexpr = (Node *) make_ands_explicit(testlist);
1506 *paramIds = paramids;
1513 * Replace correlation vars (uplevel vars) with Params.
1515 * Uplevel aggregates are replaced, too.
1517 * Note: it is critical that this runs immediately after SS_process_sublinks.
1518 * Since we do not recurse into the arguments of uplevel aggregates, they will
1519 * get copied to the appropriate subplan args list in the parent query with
1520 * uplevel vars not replaced by Params, but only adjusted in level (see
1521 * replace_outer_agg). That's exactly what we want for the vars of the parent
1522 * level --- but if an aggregate's argument contains any further-up variables,
1523 * they have to be replaced with Params in their turn. That will happen when
1524 * the parent level runs SS_replace_correlation_vars. Therefore it must do
1525 * so after expanding its sublinks to subplans. And we don't want any steps
1526 * in between, else those steps would never get applied to the aggregate
1527 * argument expressions, either in the parent or the child level.
1529 * Another fairly tricky thing going on here is the handling of SubLinks in
1530 * the arguments of uplevel aggregates. Those are not touched inside the
1531 * intermediate query level, either. Instead, SS_process_sublinks recurses
1532 * on them after copying the Aggref expression into the parent plan level
1533 * (this is actually taken care of in build_subplan).
1536 SS_replace_correlation_vars(PlannerInfo *root, Node *expr)
1538 /* No setup needed for tree walk, so away we go */
1539 return replace_correlation_vars_mutator(expr, root);
1543 replace_correlation_vars_mutator(Node *node, PlannerInfo *root)
1549 if (((Var *) node)->varlevelsup > 0)
1550 return (Node *) replace_outer_var(root, (Var *) node);
1552 if (IsA(node, Aggref))
1554 if (((Aggref *) node)->agglevelsup > 0)
1555 return (Node *) replace_outer_agg(root, (Aggref *) node);
1557 return expression_tree_mutator(node,
1558 replace_correlation_vars_mutator,
1563 * Expand SubLinks to SubPlans in the given expression.
1565 * The isQual argument tells whether or not this expression is a WHERE/HAVING
1566 * qualifier expression. If it is, any sublinks appearing at top level need
1567 * not distinguish FALSE from UNKNOWN return values.
1570 SS_process_sublinks(PlannerInfo *root, Node *expr, bool isQual)
1572 process_sublinks_context context;
1574 context.root = root;
1575 context.isTopQual = isQual;
1576 return process_sublinks_mutator(expr, &context);
1580 process_sublinks_mutator(Node *node, process_sublinks_context *context)
1582 process_sublinks_context locContext;
1584 locContext.root = context->root;
1588 if (IsA(node, SubLink))
1590 SubLink *sublink = (SubLink *) node;
1594 * First, recursively process the lefthand-side expressions, if any.
1595 * They're not top-level anymore.
1597 locContext.isTopQual = false;
1598 testexpr = process_sublinks_mutator(sublink->testexpr, &locContext);
1601 * Now build the SubPlan node and make the expr to return.
1603 return make_subplan(context->root,
1604 (Query *) sublink->subselect,
1605 sublink->subLinkType,
1607 context->isTopQual);
1611 * Don't recurse into the arguments of an outer aggregate here. Any
1612 * SubLinks in the arguments have to be dealt with at the outer query
1613 * level; they'll be handled when build_subplan collects the Aggref into
1614 * the arguments to be passed down to the current subplan.
1616 if (IsA(node, Aggref))
1618 if (((Aggref *) node)->agglevelsup > 0)
1623 * We should never see a SubPlan expression in the input (since this is
1624 * the very routine that creates 'em to begin with). We shouldn't find
1625 * ourselves invoked directly on a Query, either.
1627 Assert(!IsA(node, SubPlan));
1628 Assert(!IsA(node, AlternativeSubPlan));
1629 Assert(!IsA(node, Query));
1632 * Because make_subplan() could return an AND or OR clause, we have to
1633 * take steps to preserve AND/OR flatness of a qual. We assume the input
1634 * has been AND/OR flattened and so we need no recursion here.
1636 * (Due to the coding here, we will not get called on the List subnodes of
1637 * an AND; and the input is *not* yet in implicit-AND format. So no check
1638 * is needed for a bare List.)
1640 * Anywhere within the top-level AND/OR clause structure, we can tell
1641 * make_subplan() that NULL and FALSE are interchangeable. So isTopQual
1642 * propagates down in both cases. (Note that this is unlike the meaning
1643 * of "top level qual" used in most other places in Postgres.)
1645 if (and_clause(node))
1647 List *newargs = NIL;
1650 /* Still at qual top-level */
1651 locContext.isTopQual = context->isTopQual;
1653 foreach(l, ((BoolExpr *) node)->args)
1657 newarg = process_sublinks_mutator(lfirst(l), &locContext);
1658 if (and_clause(newarg))
1659 newargs = list_concat(newargs, ((BoolExpr *) newarg)->args);
1661 newargs = lappend(newargs, newarg);
1663 return (Node *) make_andclause(newargs);
1666 if (or_clause(node))
1668 List *newargs = NIL;
1671 /* Still at qual top-level */
1672 locContext.isTopQual = context->isTopQual;
1674 foreach(l, ((BoolExpr *) node)->args)
1678 newarg = process_sublinks_mutator(lfirst(l), &locContext);
1679 if (or_clause(newarg))
1680 newargs = list_concat(newargs, ((BoolExpr *) newarg)->args);
1682 newargs = lappend(newargs, newarg);
1684 return (Node *) make_orclause(newargs);
1688 * If we recurse down through anything other than an AND or OR node, we
1689 * are definitely not at top qual level anymore.
1691 locContext.isTopQual = false;
1693 return expression_tree_mutator(node,
1694 process_sublinks_mutator,
1695 (void *) &locContext);
1699 * SS_finalize_plan - do final sublink processing for a completed Plan.
1701 * This recursively computes the extParam and allParam sets for every Plan
1702 * node in the given plan tree. It also optionally attaches any previously
1703 * generated InitPlans to the top plan node. (Any InitPlans should already
1704 * have been put through SS_finalize_plan.)
1707 SS_finalize_plan(PlannerInfo *root, Plan *plan, bool attach_initplans)
1709 Bitmapset *valid_params,
1717 * Examine any initPlans to determine the set of external params they
1718 * reference, the set of output params they supply, and their total cost.
1719 * We'll use at least some of this info below. (Note we are assuming that
1720 * finalize_plan doesn't touch the initPlans.)
1722 * In the case where attach_initplans is false, we are assuming that the
1723 * existing initPlans are siblings that might supply params needed by the
1726 initExtParam = initSetParam = NULL;
1728 foreach(l, root->init_plans)
1730 SubPlan *initsubplan = (SubPlan *) lfirst(l);
1731 Plan *initplan = planner_subplan_get_plan(root, initsubplan);
1734 initExtParam = bms_add_members(initExtParam, initplan->extParam);
1735 foreach(l2, initsubplan->setParam)
1737 initSetParam = bms_add_member(initSetParam, lfirst_int(l2));
1739 initplan_cost += initsubplan->startup_cost + initsubplan->per_call_cost;
1743 * Now determine the set of params that are validly referenceable in this
1744 * query level; to wit, those available from outer query levels plus the
1745 * output parameters of any initPlans. (We do not include output
1746 * parameters of regular subplans. Those should only appear within the
1747 * testexpr of SubPlan nodes, and are taken care of locally within
1748 * finalize_primnode.)
1750 * Note: this is a bit overly generous since some parameters of upper
1751 * query levels might belong to query subtrees that don't include this
1752 * query. However, valid_params is only a debugging crosscheck, so it
1753 * doesn't seem worth expending lots of cycles to try to be exact.
1755 valid_params = bms_copy(initSetParam);
1757 foreach(l, root->glob->paramlist)
1759 PlannerParamItem *pitem = (PlannerParamItem *) lfirst(l);
1761 if (pitem->abslevel < root->query_level)
1763 /* valid outer-level parameter */
1764 valid_params = bms_add_member(valid_params, paramid);
1769 /* Also include the recursion working table, if any */
1770 if (root->wt_param_id >= 0)
1771 valid_params = bms_add_member(valid_params, root->wt_param_id);
1774 * Now recurse through plan tree.
1776 (void) finalize_plan(root, plan, valid_params);
1778 bms_free(valid_params);
1781 * Finally, attach any initPlans to the topmost plan node, and add their
1782 * extParams to the topmost node's, too. However, any setParams of the
1783 * initPlans should not be present in the topmost node's extParams, only
1784 * in its allParams. (As of PG 8.1, it's possible that some initPlans
1785 * have extParams that are setParams of other initPlans, so we have to
1786 * take care of this situation explicitly.)
1788 * We also add the eval cost of each initPlan to the startup cost of the
1789 * top node. This is a conservative overestimate, since in fact each
1790 * initPlan might be executed later than plan startup, or even not at all.
1792 if (attach_initplans)
1794 plan->initPlan = root->init_plans;
1795 root->init_plans = NIL; /* make sure they're not attached twice */
1797 /* allParam must include all these params */
1798 plan->allParam = bms_add_members(plan->allParam, initExtParam);
1799 plan->allParam = bms_add_members(plan->allParam, initSetParam);
1800 /* extParam must include any child extParam */
1801 plan->extParam = bms_add_members(plan->extParam, initExtParam);
1802 /* but extParam shouldn't include any setParams */
1803 plan->extParam = bms_del_members(plan->extParam, initSetParam);
1804 /* ensure extParam is exactly NULL if it's empty */
1805 if (bms_is_empty(plan->extParam))
1806 plan->extParam = NULL;
1808 plan->startup_cost += initplan_cost;
1809 plan->total_cost += initplan_cost;
1814 * Recursive processing of all nodes in the plan tree
1816 * The return value is the computed allParam set for the given Plan node.
1817 * This is just an internal notational convenience.
1820 finalize_plan(PlannerInfo *root, Plan *plan, Bitmapset *valid_params)
1822 finalize_primnode_context context;
1827 context.root = root;
1828 context.paramids = NULL; /* initialize set to empty */
1831 * When we call finalize_primnode, context.paramids sets are automatically
1832 * merged together. But when recursing to self, we have to do it the hard
1833 * way. We want the paramids set to include params in subplans as well as
1837 /* Find params in targetlist and qual */
1838 finalize_primnode((Node *) plan->targetlist, &context);
1839 finalize_primnode((Node *) plan->qual, &context);
1841 /* Check additional node-type-specific fields */
1842 switch (nodeTag(plan))
1845 finalize_primnode(((Result *) plan)->resconstantqual,
1850 finalize_primnode((Node *) ((IndexScan *) plan)->indexqual,
1854 * we need not look at indexqualorig, since it will have the same
1855 * param references as indexqual.
1859 case T_BitmapIndexScan:
1860 finalize_primnode((Node *) ((BitmapIndexScan *) plan)->indexqual,
1864 * we need not look at indexqualorig, since it will have the same
1865 * param references as indexqual.
1869 case T_BitmapHeapScan:
1870 finalize_primnode((Node *) ((BitmapHeapScan *) plan)->bitmapqualorig,
1875 finalize_primnode((Node *) ((TidScan *) plan)->tidquals,
1879 case T_SubqueryScan:
1882 * In a SubqueryScan, SS_finalize_plan has already been run on the
1883 * subplan by the inner invocation of subquery_planner, so there's
1884 * no need to do it again. Instead, just pull out the subplan's
1885 * extParams list, which represents the params it needs from my
1886 * level and higher levels.
1888 context.paramids = bms_add_members(context.paramids,
1889 ((SubqueryScan *) plan)->subplan->extParam);
1892 case T_FunctionScan:
1893 finalize_primnode(((FunctionScan *) plan)->funcexpr,
1898 finalize_primnode((Node *) ((ValuesScan *) plan)->values_lists,
1905 * You might think we should add the node's cteParam to
1906 * paramids, but we shouldn't because that param is just a
1907 * linkage mechanism for multiple CteScan nodes for the same
1908 * CTE; it is never used for changed-param signaling. What
1909 * we have to do instead is to find the referenced CTE plan
1910 * and incorporate its external paramids, so that the correct
1911 * things will happen if the CTE references outer-level
1912 * variables. See test cases for bug #4902.
1914 int plan_id = ((CteScan *) plan)->ctePlanId;
1917 /* so, do this ... */
1918 if (plan_id < 1 || plan_id > list_length(root->glob->subplans))
1919 elog(ERROR, "could not find plan for CteScan referencing plan ID %d",
1921 cteplan = (Plan *) list_nth(root->glob->subplans, plan_id - 1);
1923 bms_add_members(context.paramids, cteplan->extParam);
1926 /* ... but not this */
1928 bms_add_member(context.paramids,
1929 ((CteScan *) plan)->cteParam);
1934 case T_WorkTableScan:
1936 bms_add_member(context.paramids,
1937 ((WorkTableScan *) plan)->wtParam);
1944 finalize_primnode((Node *) ((ModifyTable *) plan)->returningLists,
1946 foreach(l, ((ModifyTable *) plan)->plans)
1949 bms_add_members(context.paramids,
1961 foreach(l, ((Append *) plan)->appendplans)
1964 bms_add_members(context.paramids,
1976 foreach(l, ((BitmapAnd *) plan)->bitmapplans)
1979 bms_add_members(context.paramids,
1991 foreach(l, ((BitmapOr *) plan)->bitmapplans)
1994 bms_add_members(context.paramids,
2003 finalize_primnode((Node *) ((Join *) plan)->joinqual,
2008 finalize_primnode((Node *) ((Join *) plan)->joinqual,
2010 finalize_primnode((Node *) ((MergeJoin *) plan)->mergeclauses,
2015 finalize_primnode((Node *) ((Join *) plan)->joinqual,
2017 finalize_primnode((Node *) ((HashJoin *) plan)->hashclauses,
2022 finalize_primnode(((Limit *) plan)->limitOffset,
2024 finalize_primnode(((Limit *) plan)->limitCount,
2028 case T_RecursiveUnion:
2041 elog(ERROR, "unrecognized node type: %d",
2042 (int) nodeTag(plan));
2045 /* Process left and right child plans, if any */
2046 context.paramids = bms_add_members(context.paramids,
2051 context.paramids = bms_add_members(context.paramids,
2057 * RecursiveUnion *generates* its worktable param, so don't bubble that up
2059 if (IsA(plan, RecursiveUnion))
2061 context.paramids = bms_del_member(context.paramids,
2062 ((RecursiveUnion *) plan)->wtParam);
2065 /* Now we have all the paramids */
2067 if (!bms_is_subset(context.paramids, valid_params))
2068 elog(ERROR, "plan should not reference subplan's variable");
2071 * Note: by definition, extParam and allParam should have the same value
2072 * in any plan node that doesn't have child initPlans. We set them equal
2073 * here, and later SS_finalize_plan will update them properly in node(s)
2074 * that it attaches initPlans to.
2076 * For speed at execution time, make sure extParam/allParam are actually
2077 * NULL if they are empty sets.
2079 if (bms_is_empty(context.paramids))
2081 plan->extParam = NULL;
2082 plan->allParam = NULL;
2086 plan->extParam = context.paramids;
2087 plan->allParam = bms_copy(context.paramids);
2090 return plan->allParam;
2094 * finalize_primnode: add IDs of all PARAM_EXEC params appearing in the given
2095 * expression tree to the result set.
2098 finalize_primnode(Node *node, finalize_primnode_context *context)
2102 if (IsA(node, Param))
2104 if (((Param *) node)->paramkind == PARAM_EXEC)
2106 int paramid = ((Param *) node)->paramid;
2108 context->paramids = bms_add_member(context->paramids, paramid);
2110 return false; /* no more to do here */
2112 if (IsA(node, SubPlan))
2114 SubPlan *subplan = (SubPlan *) node;
2115 Plan *plan = planner_subplan_get_plan(context->root, subplan);
2117 Bitmapset *subparamids;
2119 /* Recurse into the testexpr, but not into the Plan */
2120 finalize_primnode(subplan->testexpr, context);
2123 * Remove any param IDs of output parameters of the subplan that were
2124 * referenced in the testexpr. These are not interesting for
2125 * parameter change signaling since we always re-evaluate the subplan.
2126 * Note that this wouldn't work too well if there might be uses of the
2127 * same param IDs elsewhere in the plan, but that can't happen because
2128 * generate_new_param never tries to merge params.
2130 foreach(lc, subplan->paramIds)
2132 context->paramids = bms_del_member(context->paramids,
2136 /* Also examine args list */
2137 finalize_primnode((Node *) subplan->args, context);
2140 * Add params needed by the subplan to paramids, but excluding those
2141 * we will pass down to it.
2143 subparamids = bms_copy(plan->extParam);
2144 foreach(lc, subplan->parParam)
2146 subparamids = bms_del_member(subparamids, lfirst_int(lc));
2148 context->paramids = bms_join(context->paramids, subparamids);
2150 return false; /* no more to do here */
2152 return expression_tree_walker(node, finalize_primnode,
2157 * SS_make_initplan_from_plan - given a plan tree, make it an InitPlan
2159 * The plan is expected to return a scalar value of the indicated type.
2160 * We build an EXPR_SUBLINK SubPlan node and put it into the initplan
2161 * list for the current query level. A Param that represents the initplan's
2162 * output is returned.
2164 * We assume the plan hasn't been put through SS_finalize_plan.
2167 SS_make_initplan_from_plan(PlannerInfo *root, Plan *plan,
2168 Oid resulttype, int32 resulttypmod)
2174 * We must run SS_finalize_plan(), since that's normally done before a
2175 * subplan gets put into the initplan list. Tell it not to attach any
2176 * pre-existing initplans to this one, since they are siblings not
2177 * children of this initplan. (This is something else that could perhaps
2178 * be cleaner if we did extParam/allParam processing in setrefs.c instead
2179 * of here? See notes for materialize_finished_plan.)
2183 * Build extParam/allParam sets for plan nodes.
2185 SS_finalize_plan(root, plan, false);
2188 * Add the subplan and its rtable to the global lists.
2190 root->glob->subplans = lappend(root->glob->subplans,
2192 root->glob->subrtables = lappend(root->glob->subrtables,
2193 root->parse->rtable);
2196 * Create a SubPlan node and add it to the outer list of InitPlans. Note
2197 * it has to appear after any other InitPlans it might depend on (see
2198 * comments in ExecReScan).
2200 node = makeNode(SubPlan);
2201 node->subLinkType = EXPR_SUBLINK;
2202 get_first_col_type(plan, &node->firstColType, &node->firstColTypmod);
2203 node->plan_id = list_length(root->glob->subplans);
2205 root->init_plans = lappend(root->init_plans, node);
2208 * The node can't have any inputs (since it's an initplan), so the
2209 * parParam and args lists remain empty.
2212 cost_subplan(root, node, plan);
2215 * Make a Param that will be the subplan's output.
2217 prm = generate_new_param(root, resulttype, resulttypmod);
2218 node->setParam = list_make1_int(prm->paramid);
2220 /* Label the subplan for EXPLAIN purposes */
2221 node->plan_name = palloc(64);
2222 sprintf(node->plan_name, "InitPlan %d (returns $%d)",
2223 node->plan_id, prm->paramid);