Debian Codegraph @

src/backend/optimizer/plan/subselect.c

raw
1 /*-------------------------------------------------------------------------
2 *
3 * subselect.c
4 * Planning routines for subselects.
5 *
6 * This module deals with SubLinks and CTEs, but not subquery RTEs (i.e.,
7 * not sub-SELECT-in-FROM cases).
8 *
9 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
11 *
12 * IDENTIFICATION
13 * src/backend/optimizer/plan/subselect.c
14 *
15 *-------------------------------------------------------------------------
16 */
17 #include "postgres.h"
18
19 #include "access/htup_details.h"
20 #include "catalog/pg_operator.h"
21 #include "catalog/pg_type.h"
22 #include "executor/executor.h"
23 #include "miscadmin.h"
24 #include "nodes/makefuncs.h"
25 #include "nodes/nodeFuncs.h"
26 #include "optimizer/clauses.h"
27 #include "optimizer/cost.h"
28 #include "optimizer/optimizer.h"
29 #include "optimizer/paramassign.h"
30 #include "optimizer/pathnode.h"
31 #include "optimizer/planmain.h"
32 #include "optimizer/planner.h"
33 #include "optimizer/prep.h"
34 #include "optimizer/subselect.h"
35 #include "parser/parse_relation.h"
36 #include "rewrite/rewriteManip.h"
37 #include "utils/builtins.h"
38 #include "utils/lsyscache.h"
39 #include "utils/syscache.h"
40
41
42 typedef struct convert_testexpr_context
43 {
44 PlannerInfo *root;
45 List *subst_nodes; /* Nodes to substitute for Params */
46 } convert_testexpr_context;
47
48 typedef struct process_sublinks_context
49 {
50 PlannerInfo *root;
51 bool isTopQual;
52 } process_sublinks_context;
53
54 typedef struct finalize_primnode_context
55 {
56 PlannerInfo *root;
57 Bitmapset *paramids; /* Non-local PARAM_EXEC paramids found */
58 } finalize_primnode_context;
59
60 typedef struct inline_cte_walker_context
61 {
62 const char *ctename; /* name and relative level of target CTE */
63 int levelsup;
64 Query *ctequery; /* query to substitute */
65 } inline_cte_walker_context;
66
67
68 static Node *build_subplan(PlannerInfo *root, Plan *plan, Path *path,
69 PlannerInfo *subroot, List *plan_params,
70 SubLinkType subLinkType, int subLinkId,
71 Node *testexpr, List *testexpr_paramids,
72 bool unknownEqFalse);
73 static List *generate_subquery_params(PlannerInfo *root, List *tlist,
74 List **paramIds);
75 static List *generate_subquery_vars(PlannerInfo *root, List *tlist,
76 Index varno);
77 static Node *convert_testexpr(PlannerInfo *root,
78 Node *testexpr,
79 List *subst_nodes);
80 static Node *convert_testexpr_mutator(Node *node,
81 convert_testexpr_context *context);
82 static bool subplan_is_hashable(Plan *plan);
83 static bool subpath_is_hashable(Path *path);
84 static bool testexpr_is_hashable(Node *testexpr, List *param_ids);
85 static bool test_opexpr_is_hashable(OpExpr *testexpr, List *param_ids);
86 static bool hash_ok_operator(OpExpr *expr);
87 static bool contain_dml(Node *node);
88 static bool contain_dml_walker(Node *node, void *context);
89 static bool contain_outer_selfref(Node *node);
90 static bool contain_outer_selfref_walker(Node *node, Index *depth);
91 static void inline_cte(PlannerInfo *root, CommonTableExpr *cte);
92 static bool inline_cte_walker(Node *node, inline_cte_walker_context *context);
93 static bool simplify_EXISTS_query(PlannerInfo *root, Query *query);
94 static Query *convert_EXISTS_to_ANY(PlannerInfo *root, Query *subselect,
95 Node **testexpr, List **paramIds);
96 static Node *replace_correlation_vars_mutator(Node *node, PlannerInfo *root);
97 static Node *process_sublinks_mutator(Node *node,
98 process_sublinks_context *context);
99 static Bitmapset *finalize_plan(PlannerInfo *root,
100 Plan *plan,
101 int gather_param,
102 Bitmapset *valid_params,
103 Bitmapset *scan_params);
104 static bool finalize_primnode(Node *node, finalize_primnode_context *context);
105 static bool finalize_agg_primnode(Node *node, finalize_primnode_context *context);
106
107
108 /*
109 * Get the datatype/typmod/collation of the first column of the plan's output.
110 *
111 * This information is stored for ARRAY_SUBLINK execution and for
112 * exprType()/exprTypmod()/exprCollation(), which have no way to get at the
113 * plan associated with a SubPlan node. We really only need the info for
114 * EXPR_SUBLINK and ARRAY_SUBLINK subplans, but for consistency we save it
115 * always.
116 */
117 static void
118 get_first_col_type(Plan *plan, Oid *coltype, int32 *coltypmod,
119 Oid *colcollation)
120 {
121 /* In cases such as EXISTS, tlist might be empty; arbitrarily use VOID */
122 if (plan->targetlist)
123 {
124 TargetEntry *tent = linitial_node(TargetEntry, plan->targetlist);
125
126 if (!tent->resjunk)
127 {
128 *coltype = exprType((Node *) tent->expr);
129 *coltypmod = exprTypmod((Node *) tent->expr);
130 *colcollation = exprCollation((Node *) tent->expr);
131 return;
132 }
133 }
134 *coltype = VOIDOID;
135 *coltypmod = -1;
136 *colcollation = InvalidOid;
137 }
138
139 /*
140 * Convert a SubLink (as created by the parser) into a SubPlan.
141 *
142 * We are given the SubLink's contained query, type, ID, and testexpr. We are
143 * also told if this expression appears at top level of a WHERE/HAVING qual.
144 *
145 * Note: we assume that the testexpr has been AND/OR flattened (actually,
146 * it's been through eval_const_expressions), but not converted to
147 * implicit-AND form; and any SubLinks in it should already have been
148 * converted to SubPlans. The subquery is as yet untouched, however.
149 *
150 * The result is whatever we need to substitute in place of the SubLink node
151 * in the executable expression. If we're going to do the subplan as a
152 * regular subplan, this will be the constructed SubPlan node. If we're going
153 * to do the subplan as an InitPlan, the SubPlan node instead goes into
154 * root->init_plans, and what we return here is an expression tree
155 * representing the InitPlan's result: usually just a Param node representing
156 * a single scalar result, but possibly a row comparison tree containing
157 * multiple Param nodes, or for a MULTIEXPR subquery a simple NULL constant
158 * (since the real output Params are elsewhere in the tree, and the MULTIEXPR
159 * subquery itself is in a resjunk tlist entry whose value is uninteresting).
160 */
161 static Node *
162 make_subplan(PlannerInfo *root, Query *orig_subquery,
163 SubLinkType subLinkType, int subLinkId,
164 Node *testexpr, bool isTopQual)
165 {
166 Query *subquery;
167 bool simple_exists = false;
168 double tuple_fraction;
169 PlannerInfo *subroot;
170 RelOptInfo *final_rel;
171 Path *best_path;
172 Plan *plan;
173 List *plan_params;
174 Node *result;
175
176 /*
177 * Copy the source Query node. This is a quick and dirty kluge to resolve
178 * the fact that the parser can generate trees with multiple links to the
179 * same sub-Query node, but the planner wants to scribble on the Query.
180 * Try to clean this up when we do querytree redesign...
181 */
182 subquery = copyObject(orig_subquery);
183
184 /*
185 * If it's an EXISTS subplan, we might be able to simplify it.
186 */
187 if (subLinkType == EXISTS_SUBLINK)
188 simple_exists = simplify_EXISTS_query(root, subquery);
189
190 /*
191 * For an EXISTS subplan, tell lower-level planner to expect that only the
192 * first tuple will be retrieved. For ALL and ANY subplans, we will be
193 * able to stop evaluating if the test condition fails or matches, so very
194 * often not all the tuples will be retrieved; for lack of a better idea,
195 * specify 50% retrieval. For EXPR, MULTIEXPR, and ROWCOMPARE subplans,
196 * use default behavior (we're only expecting one row out, anyway).
197 *
198 * NOTE: if you change these numbers, also change cost_subplan() in
199 * path/costsize.c.
200 *
201 * XXX If an ANY subplan is uncorrelated, build_subplan may decide to hash
202 * its output. In that case it would've been better to specify full
203 * retrieval. At present, however, we can only check hashability after
204 * we've made the subplan :-(. (Determining whether it'll fit in hash_mem
205 * is the really hard part.) Therefore, we don't want to be too
206 * optimistic about the percentage of tuples retrieved, for fear of
207 * selecting a plan that's bad for the materialization case.
208 */
209 if (subLinkType == EXISTS_SUBLINK)
210 tuple_fraction = 1.0; /* just like a LIMIT 1 */
211 else if (subLinkType == ALL_SUBLINK ||
212 subLinkType == ANY_SUBLINK)
213 tuple_fraction = 0.5; /* 50% */
214 else
215 tuple_fraction = 0.0; /* default behavior */
216
217 /* plan_params should not be in use in current query level */
218 Assert(root->plan_params == NIL);
219
220 /* Generate Paths for the subquery */
221 subroot = subquery_planner(root->glob, subquery, root, false,
222 tuple_fraction, NULL);
223
224 /* Isolate the params needed by this specific subplan */
225 plan_params = root->plan_params;
226 root->plan_params = NIL;
227
228 /*
229 * Select best Path and turn it into a Plan. At least for now, there
230 * seems no reason to postpone doing that.
231 */
232 final_rel = fetch_upper_rel(subroot, UPPERREL_FINAL, NULL);
233 best_path = get_cheapest_fractional_path(final_rel, tuple_fraction);
234
235 plan = create_plan(subroot, best_path);
236
237 /* And convert to SubPlan or InitPlan format. */
238 result = build_subplan(root, plan, best_path,
239 subroot, plan_params,
240 subLinkType, subLinkId,
241 testexpr, NIL, isTopQual);
242
243 /*
244 * If it's a correlated EXISTS with an unimportant targetlist, we might be
245 * able to transform it to the equivalent of an IN and then implement it
246 * by hashing. We don't have enough information yet to tell which way is
247 * likely to be better (it depends on the expected number of executions of
248 * the EXISTS qual, and we are much too early in planning the outer query
249 * to be able to guess that). So we generate both plans, if possible, and
250 * leave it to setrefs.c to decide which to use.
251 */
252 if (simple_exists && IsA(result, SubPlan))
253 {
254 Node *newtestexpr;
255 List *paramIds;
256
257 /* Make a second copy of the original subquery */
258 subquery = copyObject(orig_subquery);
259 /* and re-simplify */
260 simple_exists = simplify_EXISTS_query(root, subquery);
261 Assert(simple_exists);
262 /* See if it can be converted to an ANY query */
263 subquery = convert_EXISTS_to_ANY(root, subquery,
264 &newtestexpr, &paramIds);
265 if (subquery)
266 {
267 /* Generate Paths for the ANY subquery; we'll need all rows */
268 subroot = subquery_planner(root->glob, subquery, root, false, 0.0,
269 NULL);
270
271 /* Isolate the params needed by this specific subplan */
272 plan_params = root->plan_params;
273 root->plan_params = NIL;
274
275 /* Select best Path */
276 final_rel = fetch_upper_rel(subroot, UPPERREL_FINAL, NULL);
277 best_path = final_rel->cheapest_total_path;
278
279 /* Now we can check if it'll fit in hash_mem */
280 if (subpath_is_hashable(best_path))
281 {
282 SubPlan *hashplan;
283 AlternativeSubPlan *asplan;
284
285 /* OK, finish planning the ANY subquery */
286 plan = create_plan(subroot, best_path);
287
288 /* ... and convert to SubPlan format */
289 hashplan = castNode(SubPlan,
290 build_subplan(root, plan, best_path,
291 subroot, plan_params,
292 ANY_SUBLINK, 0,
293 newtestexpr,
294 paramIds,
295 true));
296 /* Check we got what we expected */
297 Assert(hashplan->parParam == NIL);
298 Assert(hashplan->useHashTable);
299
300 /* Leave it to setrefs.c to decide which plan to use */
301 asplan = makeNode(AlternativeSubPlan);
302 asplan->subplans = list_make2(result, hashplan);
303 result = (Node *) asplan;
304 root->hasAlternativeSubPlans = true;
305 }
306 }
307 }
308
309 return result;
310 }
311
312 /*
313 * Build a SubPlan node given the raw inputs --- subroutine for make_subplan
314 *
315 * Returns either the SubPlan, or a replacement expression if we decide to
316 * make it an InitPlan, as explained in the comments for make_subplan.
317 */
318 static Node *
319 build_subplan(PlannerInfo *root, Plan *plan, Path *path,
320 PlannerInfo *subroot, List *plan_params,
321 SubLinkType subLinkType, int subLinkId,
322 Node *testexpr, List *testexpr_paramids,
323 bool unknownEqFalse)
324 {
325 Node *result;
326 SubPlan *splan;
327 bool isInitPlan;
328 ListCell *lc;
329
330 /*
331 * Initialize the SubPlan node. Note plan_id, plan_name, and cost fields
332 * are set further down.
333 */
334 splan = makeNode(SubPlan);
335 splan->subLinkType = subLinkType;
336 splan->testexpr = NULL;
337 splan->paramIds = NIL;
338 get_first_col_type(plan, &splan->firstColType, &splan->firstColTypmod,
339 &splan->firstColCollation);
340 splan->useHashTable = false;
341 splan->unknownEqFalse = unknownEqFalse;
342 splan->parallel_safe = plan->parallel_safe;
343 splan->setParam = NIL;
344 splan->parParam = NIL;
345 splan->args = NIL;
346
347 /*
348 * Make parParam and args lists of param IDs and expressions that current
349 * query level will pass to this child plan.
350 */
351 foreach(lc, plan_params)
352 {
353 PlannerParamItem *pitem = (PlannerParamItem *) lfirst(lc);
354 Node *arg = pitem->item;
355
356 /*
357 * The Var, PlaceHolderVar, Aggref, GroupingFunc, or ReturningExpr has
358 * already been adjusted to have the correct varlevelsup, phlevelsup,
359 * agglevelsup, or retlevelsup.
360 *
361 * If it's a PlaceHolderVar, Aggref, GroupingFunc, or ReturningExpr,
362 * its arguments might contain SubLinks, which have not yet been
363 * processed (see the comments for SS_replace_correlation_vars). Do
364 * that now.
365 */
366 if (IsA(arg, PlaceHolderVar) ||
367 IsA(arg, Aggref) ||
368 IsA(arg, GroupingFunc) ||
369 IsA(arg, ReturningExpr))
370 arg = SS_process_sublinks(root, arg, false);
371
372 splan->parParam = lappend_int(splan->parParam, pitem->paramId);
373 splan->args = lappend(splan->args, arg);
374 }
375
376 /*
377 * Un-correlated or undirect correlated plans of EXISTS, EXPR, ARRAY,
378 * ROWCOMPARE, or MULTIEXPR types can be used as initPlans. For EXISTS,
379 * EXPR, or ARRAY, we return a Param referring to the result of evaluating
380 * the initPlan. For ROWCOMPARE, we must modify the testexpr tree to
381 * contain PARAM_EXEC Params instead of the PARAM_SUBLINK Params emitted
382 * by the parser, and then return that tree. For MULTIEXPR, we return a
383 * null constant: the resjunk targetlist item containing the SubLink does
384 * not need to return anything useful, since the referencing Params are
385 * elsewhere.
386 */
387 if (splan->parParam == NIL && subLinkType == EXISTS_SUBLINK)
388 {
389 Param *prm;
390
391 Assert(testexpr == NULL);
392 prm = generate_new_exec_param(root, BOOLOID, -1, InvalidOid);
393 splan->setParam = list_make1_int(prm->paramid);
394 isInitPlan = true;
395 result = (Node *) prm;
396 }
397 else if (splan->parParam == NIL && subLinkType == EXPR_SUBLINK)
398 {
399 TargetEntry *te = linitial(plan->targetlist);
400 Param *prm;
401
402 Assert(!te->resjunk);
403 Assert(testexpr == NULL);
404 prm = generate_new_exec_param(root,
405 exprType((Node *) te->expr),
406 exprTypmod((Node *) te->expr),
407 exprCollation((Node *) te->expr));
408 splan->setParam = list_make1_int(prm->paramid);
409 isInitPlan = true;
410 result = (Node *) prm;
411 }
412 else if (splan->parParam == NIL && subLinkType == ARRAY_SUBLINK)
413 {
414 TargetEntry *te = linitial(plan->targetlist);
415 Oid arraytype;
416 Param *prm;
417
418 Assert(!te->resjunk);
419 Assert(testexpr == NULL);
420 arraytype = get_promoted_array_type(exprType((Node *) te->expr));
421 if (!OidIsValid(arraytype))
422 elog(ERROR, "could not find array type for datatype %s",
423 format_type_be(exprType((Node *) te->expr)));
424 prm = generate_new_exec_param(root,
425 arraytype,
426 exprTypmod((Node *) te->expr),
427 exprCollation((Node *) te->expr));
428 splan->setParam = list_make1_int(prm->paramid);
429 isInitPlan = true;
430 result = (Node *) prm;
431 }
432 else if (splan->parParam == NIL && subLinkType == ROWCOMPARE_SUBLINK)
433 {
434 /* Adjust the Params */
435 List *params;
436
437 Assert(testexpr != NULL);
438 params = generate_subquery_params(root,
439 plan->targetlist,
440 &splan->paramIds);
441 result = convert_testexpr(root,
442 testexpr,
443 params);
444 splan->setParam = list_copy(splan->paramIds);
445 isInitPlan = true;
446
447 /*
448 * The executable expression is returned to become part of the outer
449 * plan's expression tree; it is not kept in the initplan node.
450 */
451 }
452 else if (subLinkType == MULTIEXPR_SUBLINK)
453 {
454 /*
455 * Whether it's an initplan or not, it needs to set a PARAM_EXEC Param
456 * for each output column.
457 */
458 List *params;
459
460 Assert(testexpr == NULL);
461 params = generate_subquery_params(root,
462 plan->targetlist,
463 &splan->setParam);
464
465 /*
466 * Save the list of replacement Params in the n'th cell of
467 * root->multiexpr_params; setrefs.c will use it to replace
468 * PARAM_MULTIEXPR Params.
469 */
470 while (list_length(root->multiexpr_params) < subLinkId)
471 root->multiexpr_params = lappend(root->multiexpr_params, NIL);
472 lc = list_nth_cell(root->multiexpr_params, subLinkId - 1);
473 Assert(lfirst(lc) == NIL);
474 lfirst(lc) = params;
475
476 /* It can be an initplan if there are no parParams. */
477 if (splan->parParam == NIL)
478 {
479 isInitPlan = true;
480 result = (Node *) makeNullConst(RECORDOID, -1, InvalidOid);
481 }
482 else
483 {
484 isInitPlan = false;
485 result = (Node *) splan;
486 }
487 }
488 else
489 {
490 /*
491 * Adjust the Params in the testexpr, unless caller already took care
492 * of it (as indicated by passing a list of Param IDs).
493 */
494 if (testexpr && testexpr_paramids == NIL)
495 {
496 List *params;
497
498 params = generate_subquery_params(root,
499 plan->targetlist,
500 &splan->paramIds);
501 splan->testexpr = convert_testexpr(root,
502 testexpr,
503 params);
504 }
505 else
506 {
507 splan->testexpr = testexpr;
508 splan->paramIds = testexpr_paramids;
509 }
510
511 /*
512 * We can't convert subplans of ALL_SUBLINK or ANY_SUBLINK types to
513 * initPlans, even when they are uncorrelated or undirect correlated,
514 * because we need to scan the output of the subplan for each outer
515 * tuple. But if it's a not-direct-correlated IN (= ANY) test, we
516 * might be able to use a hashtable to avoid comparing all the tuples.
517 */
518 if (subLinkType == ANY_SUBLINK &&
519 splan->parParam == NIL &&
520 subplan_is_hashable(plan) &&
521 testexpr_is_hashable(splan->testexpr, splan->paramIds))
522 splan->useHashTable = true;
523
524 /*
525 * Otherwise, we have the option to tack a Material node onto the top
526 * of the subplan, to reduce the cost of reading it repeatedly. This
527 * is pointless for a direct-correlated subplan, since we'd have to
528 * recompute its results each time anyway. For uncorrelated/undirect
529 * correlated subplans, we add Material unless the subplan's top plan
530 * node would materialize its output anyway. Also, if enable_material
531 * is false, then the user does not want us to materialize anything
532 * unnecessarily, so we don't.
533 */
534 else if (splan->parParam == NIL && enable_material &&
535 !ExecMaterializesOutput(nodeTag(plan)))
536 plan = materialize_finished_plan(plan);
537
538 result = (Node *) splan;
539 isInitPlan = false;
540 }
541
542 /*
543 * Add the subplan, its path, and its PlannerInfo to the global lists.
544 */
545 root->glob->subplans = lappend(root->glob->subplans, plan);
546 root->glob->subpaths = lappend(root->glob->subpaths, path);
547 root->glob->subroots = lappend(root->glob->subroots, subroot);
548 splan->plan_id = list_length(root->glob->subplans);
549
550 if (isInitPlan)
551 root->init_plans = lappend(root->init_plans, splan);
552
553 /*
554 * A parameterless subplan (not initplan) should be prepared to handle
555 * REWIND efficiently. If it has direct parameters then there's no point
556 * since it'll be reset on each scan anyway; and if it's an initplan then
557 * there's no point since it won't get re-run without parameter changes
558 * anyway. The input of a hashed subplan doesn't need REWIND either.
559 */
560 if (splan->parParam == NIL && !isInitPlan && !splan->useHashTable)
561 root->glob->rewindPlanIDs = bms_add_member(root->glob->rewindPlanIDs,
562 splan->plan_id);
563
564 /* Label the subplan for EXPLAIN purposes */
565 splan->plan_name = psprintf("%s %d",
566 isInitPlan ? "InitPlan" : "SubPlan",
567 splan->plan_id);
568
569 /* Lastly, fill in the cost estimates for use later */
570 cost_subplan(root, splan, plan);
571
572 return result;
573 }
574
575 /*
576 * generate_subquery_params: build a list of Params representing the output
577 * columns of a sublink's sub-select, given the sub-select's targetlist.
578 *
579 * We also return an integer list of the paramids of the Params.
580 */
581 static List *
582 generate_subquery_params(PlannerInfo *root, List *tlist, List **paramIds)
583 {
584 List *result;
585 List *ids;
586 ListCell *lc;
587
588 result = ids = NIL;
589 foreach(lc, tlist)
590 {
591 TargetEntry *tent = (TargetEntry *) lfirst(lc);
592 Param *param;
593
594 if (tent->resjunk)
595 continue;
596
597 param = generate_new_exec_param(root,
598 exprType((Node *) tent->expr),
599 exprTypmod((Node *) tent->expr),
600 exprCollation((Node *) tent->expr));
601 result = lappend(result, param);
602 ids = lappend_int(ids, param->paramid);
603 }
604
605 *paramIds = ids;
606 return result;
607 }
608
609 /*
610 * generate_subquery_vars: build a list of Vars representing the output
611 * columns of a sublink's sub-select, given the sub-select's targetlist.
612 * The Vars have the specified varno (RTE index).
613 */
614 static List *
615 generate_subquery_vars(PlannerInfo *root, List *tlist, Index varno)
616 {
617 List *result;
618 ListCell *lc;
619
620 result = NIL;
621 foreach(lc, tlist)
622 {
623 TargetEntry *tent = (TargetEntry *) lfirst(lc);
624 Var *var;
625
626 if (tent->resjunk)
627 continue;
628
629 var = makeVarFromTargetEntry(varno, tent);
630 result = lappend(result, var);
631 }
632
633 return result;
634 }
635
636 /*
637 * convert_testexpr: convert the testexpr given by the parser into
638 * actually executable form. This entails replacing PARAM_SUBLINK Params
639 * with Params or Vars representing the results of the sub-select. The
640 * nodes to be substituted are passed in as the List result from
641 * generate_subquery_params or generate_subquery_vars.
642 */
643 static Node *
644 convert_testexpr(PlannerInfo *root,
645 Node *testexpr,
646 List *subst_nodes)
647 {
648 convert_testexpr_context context;
649
650 context.root = root;
651 context.subst_nodes = subst_nodes;
652 return convert_testexpr_mutator(testexpr, &context);
653 }
654
655 static Node *
656 convert_testexpr_mutator(Node *node,
657 convert_testexpr_context *context)
658 {
659 if (node == NULL)
660 return NULL;
661 if (IsA(node, Param))
662 {
663 Param *param = (Param *) node;
664
665 if (param->paramkind == PARAM_SUBLINK)
666 {
667 if (param->paramid <= 0 ||
668 param->paramid > list_length(context->subst_nodes))
669 elog(ERROR, "unexpected PARAM_SUBLINK ID: %d", param->paramid);
670
671 /*
672 * We copy the list item to avoid having doubly-linked
673 * substructure in the modified parse tree. This is probably
674 * unnecessary when it's a Param, but be safe.
675 */
676 return (Node *) copyObject(list_nth(context->subst_nodes,
677 param->paramid - 1));
678 }
679 }
680 if (IsA(node, SubLink))
681 {
682 /*
683 * If we come across a nested SubLink, it is neither necessary nor
684 * correct to recurse into it: any PARAM_SUBLINKs we might find inside
685 * belong to the inner SubLink not the outer. So just return it as-is.
686 *
687 * This reasoning depends on the assumption that nothing will pull
688 * subexpressions into or out of the testexpr field of a SubLink, at
689 * least not without replacing PARAM_SUBLINKs first. If we did want
690 * to do that we'd need to rethink the parser-output representation
691 * altogether, since currently PARAM_SUBLINKs are only unique per
692 * SubLink not globally across the query. The whole point of
693 * replacing them with Vars or PARAM_EXEC nodes is to make them
694 * globally unique before they escape from the SubLink's testexpr.
695 *
696 * Note: this can't happen when called during SS_process_sublinks,
697 * because that recursively processes inner SubLinks first. It can
698 * happen when called from convert_ANY_sublink_to_join, though.
699 */
700 return node;
701 }
702 return expression_tree_mutator(node, convert_testexpr_mutator, context);
703 }
704
705 /*
706 * subplan_is_hashable: can we implement an ANY subplan by hashing?
707 *
708 * This is not responsible for checking whether the combining testexpr
709 * is suitable for hashing. We only look at the subquery itself.
710 */
711 static bool
712 subplan_is_hashable(Plan *plan)
713 {
714 double subquery_size;
715
716 /*
717 * The estimated size of the subquery result must fit in hash_mem. (Note:
718 * we use heap tuple overhead here even though the tuples will actually be
719 * stored as MinimalTuples; this provides some fudge factor for hashtable
720 * overhead.)
721 */
722 subquery_size = plan->plan_rows *
723 (MAXALIGN(plan->plan_width) + MAXALIGN(SizeofHeapTupleHeader));
724 if (subquery_size > get_hash_memory_limit())
725 return false;
726
727 return true;
728 }
729
730 /*
731 * subpath_is_hashable: can we implement an ANY subplan by hashing?
732 *
733 * Identical to subplan_is_hashable, but work from a Path for the subplan.
734 */
735 static bool
736 subpath_is_hashable(Path *path)
737 {
738 double subquery_size;
739
740 /*
741 * The estimated size of the subquery result must fit in hash_mem. (Note:
742 * we use heap tuple overhead here even though the tuples will actually be
743 * stored as MinimalTuples; this provides some fudge factor for hashtable
744 * overhead.)
745 */
746 subquery_size = path->rows *
747 (MAXALIGN(path->pathtarget->width) + MAXALIGN(SizeofHeapTupleHeader));
748 if (subquery_size > get_hash_memory_limit())
749 return false;
750
751 return true;
752 }
753
754 /*
755 * testexpr_is_hashable: is an ANY SubLink's test expression hashable?
756 *
757 * To identify LHS vs RHS of the hash expression, we must be given the
758 * list of output Param IDs of the SubLink's subquery.
759 */
760 static bool
761 testexpr_is_hashable(Node *testexpr, List *param_ids)
762 {
763 /*
764 * The testexpr must be a single OpExpr, or an AND-clause containing only
765 * OpExprs, each of which satisfy test_opexpr_is_hashable().
766 */
767 if (testexpr && IsA(testexpr, OpExpr))
768 {
769 if (test_opexpr_is_hashable((OpExpr *) testexpr, param_ids))
770 return true;
771 }
772 else if (is_andclause(testexpr))
773 {
774 ListCell *l;
775
776 foreach(l, ((BoolExpr *) testexpr)->args)
777 {
778 Node *andarg = (Node *) lfirst(l);
779
780 if (!IsA(andarg, OpExpr))
781 return false;
782 if (!test_opexpr_is_hashable((OpExpr *) andarg, param_ids))
783 return false;
784 }
785 return true;
786 }
787
788 return false;
789 }
790
791 static bool
792 test_opexpr_is_hashable(OpExpr *testexpr, List *param_ids)
793 {
794 /*
795 * The combining operator must be hashable and strict. The need for
796 * hashability is obvious, since we want to use hashing. Without
797 * strictness, behavior in the presence of nulls is too unpredictable. We
798 * actually must assume even more than plain strictness: it can't yield
799 * NULL for non-null inputs, either (see nodeSubplan.c). However, hash
800 * indexes and hash joins assume that too.
801 */
802 if (!hash_ok_operator(testexpr))
803 return false;
804
805 /*
806 * The left and right inputs must belong to the outer and inner queries
807 * respectively; hence Params that will be supplied by the subquery must
808 * not appear in the LHS, and Vars of the outer query must not appear in
809 * the RHS. (Ordinarily, this must be true because of the way that the
810 * parser builds an ANY SubLink's testexpr ... but inlining of functions
811 * could have changed the expression's structure, so we have to check.
812 * Such cases do not occur often enough to be worth trying to optimize, so
813 * we don't worry about trying to commute the clause or anything like
814 * that; we just need to be sure not to build an invalid plan.)
815 */
816 if (list_length(testexpr->args) != 2)
817 return false;
818 if (contain_exec_param((Node *) linitial(testexpr->args), param_ids))
819 return false;
820 if (contain_var_clause((Node *) lsecond(testexpr->args)))
821 return false;
822 return true;
823 }
824
825 /*
826 * Check expression is hashable + strict
827 *
828 * We could use op_hashjoinable() and op_strict(), but do it like this to
829 * avoid a redundant cache lookup.
830 */
831 static bool
832 hash_ok_operator(OpExpr *expr)
833 {
834 Oid opid = expr->opno;
835
836 /* quick out if not a binary operator */
837 if (list_length(expr->args) != 2)
838 return false;
839 if (opid == ARRAY_EQ_OP ||
840 opid == RECORD_EQ_OP ||
841 opid == RANGE_EQ_OP ||
842 opid == MULTIRANGE_EQ_OP)
843 {
844 /* these are strict, but must check input type to ensure hashable */
845 Node *leftarg = linitial(expr->args);
846
847 return op_hashjoinable(opid, exprType(leftarg));
848 }
849 else
850 {
851 /* else must look up the operator properties */
852 HeapTuple tup;
853 Form_pg_operator optup;
854
855 tup = SearchSysCache1(OPEROID, ObjectIdGetDatum(opid));
856 if (!HeapTupleIsValid(tup))
857 elog(ERROR, "cache lookup failed for operator %u", opid);
858 optup = (Form_pg_operator) GETSTRUCT(tup);
859 if (!optup->oprcanhash || !func_strict(optup->oprcode))
860 {
861 ReleaseSysCache(tup);
862 return false;
863 }
864 ReleaseSysCache(tup);
865 return true;
866 }
867 }
868
869
870 /*
871 * SS_process_ctes: process a query's WITH list
872 *
873 * Consider each CTE in the WITH list and either ignore it (if it's an
874 * unreferenced SELECT), "inline" it to create a regular sub-SELECT-in-FROM,
875 * or convert it to an initplan.
876 *
877 * A side effect is to fill in root->cte_plan_ids with a list that
878 * parallels root->parse->cteList and provides the subplan ID for
879 * each CTE's initplan, or a dummy ID (-1) if we didn't make an initplan.
880 */
881 void
882 SS_process_ctes(PlannerInfo *root)
883 {
884 ListCell *lc;
885
886 Assert(root->cte_plan_ids == NIL);
887
888 foreach(lc, root->parse->cteList)
889 {
890 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
891 CmdType cmdType = ((Query *) cte->ctequery)->commandType;
892 Query *subquery;
893 PlannerInfo *subroot;
894 RelOptInfo *final_rel;
895 Path *best_path;
896 Plan *plan;
897 SubPlan *splan;
898 int paramid;
899
900 /*
901 * Ignore SELECT CTEs that are not actually referenced anywhere.
902 */
903 if (cte->cterefcount == 0 && cmdType == CMD_SELECT)
904 {
905 /* Make a dummy entry in cte_plan_ids */
906 root->cte_plan_ids = lappend_int(root->cte_plan_ids, -1);
907 continue;
908 }
909
910 /*
911 * Consider inlining the CTE (creating RTE_SUBQUERY RTE(s)) instead of
912 * implementing it as a separately-planned CTE.
913 *
914 * We cannot inline if any of these conditions hold:
915 *
916 * 1. The user said not to (the CTEMaterializeAlways option).
917 *
918 * 2. The CTE is recursive.
919 *
920 * 3. The CTE has side-effects; this includes either not being a plain
921 * SELECT, or containing volatile functions. Inlining might change
922 * the side-effects, which would be bad.
923 *
924 * 4. The CTE is multiply-referenced and contains a self-reference to
925 * a recursive CTE outside itself. Inlining would result in multiple
926 * recursive self-references, which we don't support.
927 *
928 * Otherwise, we have an option whether to inline or not. That should
929 * always be a win if there's just a single reference, but if the CTE
930 * is multiply-referenced then it's unclear: inlining adds duplicate
931 * computations, but the ability to absorb restrictions from the outer
932 * query level could outweigh that. We do not have nearly enough
933 * information at this point to tell whether that's true, so we let
934 * the user express a preference. Our default behavior is to inline
935 * only singly-referenced CTEs, but a CTE marked CTEMaterializeNever
936 * will be inlined even if multiply referenced.
937 *
938 * Note: we check for volatile functions last, because that's more
939 * expensive than the other tests needed.
940 */
941 if ((cte->ctematerialized == CTEMaterializeNever ||
942 (cte->ctematerialized == CTEMaterializeDefault &&
943 cte->cterefcount == 1)) &&
944 !cte->cterecursive &&
945 cmdType == CMD_SELECT &&
946 !contain_dml(cte->ctequery) &&
947 (cte->cterefcount <= 1 ||
948 !contain_outer_selfref(cte->ctequery)) &&
949 !contain_volatile_functions(cte->ctequery))
950 {
951 inline_cte(root, cte);
952 /* Make a dummy entry in cte_plan_ids */
953 root->cte_plan_ids = lappend_int(root->cte_plan_ids, -1);
954 continue;
955 }
956
957 /*
958 * Copy the source Query node. Probably not necessary, but let's keep
959 * this similar to make_subplan.
960 */
961 subquery = (Query *) copyObject(cte->ctequery);
962
963 /* plan_params should not be in use in current query level */
964 Assert(root->plan_params == NIL);
965
966 /*
967 * Generate Paths for the CTE query. Always plan for full retrieval
968 * --- we don't have enough info to predict otherwise.
969 */
970 subroot = subquery_planner(root->glob, subquery, root,
971 cte->cterecursive, 0.0, NULL);
972
973 /*
974 * Since the current query level doesn't yet contain any RTEs, it
975 * should not be possible for the CTE to have requested parameters of
976 * this level.
977 */
978 if (root->plan_params)
979 elog(ERROR, "unexpected outer reference in CTE query");
980
981 /*
982 * Select best Path and turn it into a Plan. At least for now, there
983 * seems no reason to postpone doing that.
984 */
985 final_rel = fetch_upper_rel(subroot, UPPERREL_FINAL, NULL);
986 best_path = final_rel->cheapest_total_path;
987
988 plan = create_plan(subroot, best_path);
989
990 /*
991 * Make a SubPlan node for it. This is just enough unlike
992 * build_subplan that we can't share code.
993 *
994 * Note plan_id, plan_name, and cost fields are set further down.
995 */
996 splan = makeNode(SubPlan);
997 splan->subLinkType = CTE_SUBLINK;
998 splan->testexpr = NULL;
999 splan->paramIds = NIL;
1000 get_first_col_type(plan, &splan->firstColType, &splan->firstColTypmod,
1001 &splan->firstColCollation);
1002 splan->useHashTable = false;
1003 splan->unknownEqFalse = false;
1004
1005 /*
1006 * CTE scans are not considered for parallelism (cf
1007 * set_rel_consider_parallel).
1008 */
1009 splan->parallel_safe = false;
1010 splan->setParam = NIL;
1011 splan->parParam = NIL;
1012 splan->args = NIL;
1013
1014 /*
1015 * The node can't have any inputs (since it's an initplan), so the
1016 * parParam and args lists remain empty. (It could contain references
1017 * to earlier CTEs' output param IDs, but CTE outputs are not
1018 * propagated via the args list.)
1019 */
1020
1021 /*
1022 * Assign a param ID to represent the CTE's output. No ordinary
1023 * "evaluation" of this param slot ever happens, but we use the param
1024 * ID for setParam/chgParam signaling just as if the CTE plan were
1025 * returning a simple scalar output. (Also, the executor abuses the
1026 * ParamExecData slot for this param ID for communication among
1027 * multiple CteScan nodes that might be scanning this CTE.)
1028 */
1029 paramid = assign_special_exec_param(root);
1030 splan->setParam = list_make1_int(paramid);
1031
1032 /*
1033 * Add the subplan, its path, and its PlannerInfo to the global lists.
1034 */
1035 root->glob->subplans = lappend(root->glob->subplans, plan);
1036 root->glob->subpaths = lappend(root->glob->subpaths, best_path);
1037 root->glob->subroots = lappend(root->glob->subroots, subroot);
1038 splan->plan_id = list_length(root->glob->subplans);
1039
1040 root->init_plans = lappend(root->init_plans, splan);
1041
1042 root->cte_plan_ids = lappend_int(root->cte_plan_ids, splan->plan_id);
1043
1044 /* Label the subplan for EXPLAIN purposes */
1045 splan->plan_name = psprintf("CTE %s", cte->ctename);
1046
1047 /* Lastly, fill in the cost estimates for use later */
1048 cost_subplan(root, splan, plan);
1049 }
1050 }
1051
1052 /*
1053 * contain_dml: is any subquery not a plain SELECT?
1054 *
1055 * We reject SELECT FOR UPDATE/SHARE as well as INSERT etc.
1056 */
1057 static bool
1058 contain_dml(Node *node)
1059 {
1060 return contain_dml_walker(node, NULL);
1061 }
1062
1063 static bool
1064 contain_dml_walker(Node *node, void *context)
1065 {
1066 if (node == NULL)
1067 return false;
1068 if (IsA(node, Query))
1069 {
1070 Query *query = (Query *) node;
1071
1072 if (query->commandType != CMD_SELECT ||
1073 query->rowMarks != NIL)
1074 return true;
1075
1076 return query_tree_walker(query, contain_dml_walker, context, 0);
1077 }
1078 return expression_tree_walker(node, contain_dml_walker, context);
1079 }
1080
1081 /*
1082 * contain_outer_selfref: is there an external recursive self-reference?
1083 */
1084 static bool
1085 contain_outer_selfref(Node *node)
1086 {
1087 Index depth = 0;
1088
1089 /*
1090 * We should be starting with a Query, so that depth will be 1 while
1091 * examining its immediate contents.
1092 */
1093 Assert(IsA(node, Query));
1094
1095 return contain_outer_selfref_walker(node, &depth);
1096 }
1097
1098 static bool
1099 contain_outer_selfref_walker(Node *node, Index *depth)
1100 {
1101 if (node == NULL)
1102 return false;
1103 if (IsA(node, RangeTblEntry))
1104 {
1105 RangeTblEntry *rte = (RangeTblEntry *) node;
1106
1107 /*
1108 * Check for a self-reference to a CTE that's above the Query that our
1109 * search started at.
1110 */
1111 if (rte->rtekind == RTE_CTE &&
1112 rte->self_reference &&
1113 rte->ctelevelsup >= *depth)
1114 return true;
1115 return false; /* allow range_table_walker to continue */
1116 }
1117 if (IsA(node, Query))
1118 {
1119 /* Recurse into subquery, tracking nesting depth properly */
1120 Query *query = (Query *) node;
1121 bool result;
1122
1123 (*depth)++;
1124
1125 result = query_tree_walker(query, contain_outer_selfref_walker,
1126 depth, QTW_EXAMINE_RTES_BEFORE);
1127
1128 (*depth)--;
1129
1130 return result;
1131 }
1132 return expression_tree_walker(node, contain_outer_selfref_walker, depth);
1133 }
1134
1135 /*
1136 * inline_cte: convert RTE_CTE references to given CTE into RTE_SUBQUERYs
1137 */
1138 static void
1139 inline_cte(PlannerInfo *root, CommonTableExpr *cte)
1140 {
1141 struct inline_cte_walker_context context;
1142
1143 context.ctename = cte->ctename;
1144 /* Start at levelsup = -1 because we'll immediately increment it */
1145 context.levelsup = -1;
1146 context.ctequery = castNode(Query, cte->ctequery);
1147
1148 (void) inline_cte_walker((Node *) root->parse, &context);
1149 }
1150
1151 static bool
1152 inline_cte_walker(Node *node, inline_cte_walker_context *context)
1153 {
1154 if (node == NULL)
1155 return false;
1156 if (IsA(node, Query))
1157 {
1158 Query *query = (Query *) node;
1159
1160 context->levelsup++;
1161
1162 /*
1163 * Visit the query's RTE nodes after their contents; otherwise
1164 * query_tree_walker would descend into the newly inlined CTE query,
1165 * which we don't want.
1166 */
1167 (void) query_tree_walker(query, inline_cte_walker, context,
1168 QTW_EXAMINE_RTES_AFTER);
1169
1170 context->levelsup--;
1171
1172 return false;
1173 }
1174 else if (IsA(node, RangeTblEntry))
1175 {
1176 RangeTblEntry *rte = (RangeTblEntry *) node;
1177
1178 if (rte->rtekind == RTE_CTE &&
1179 strcmp(rte->ctename, context->ctename) == 0 &&
1180 rte->ctelevelsup == context->levelsup)
1181 {
1182 /*
1183 * Found a reference to replace. Generate a copy of the CTE query
1184 * with appropriate level adjustment for outer references (e.g.,
1185 * to other CTEs).
1186 */
1187 Query *newquery = copyObject(context->ctequery);
1188
1189 if (context->levelsup > 0)
1190 IncrementVarSublevelsUp((Node *) newquery, context->levelsup, 1);
1191
1192 /*
1193 * Convert the RTE_CTE RTE into a RTE_SUBQUERY.
1194 *
1195 * Historically, a FOR UPDATE clause has been treated as extending
1196 * into views and subqueries, but not into CTEs. We preserve this
1197 * distinction by not trying to push rowmarks into the new
1198 * subquery.
1199 */
1200 rte->rtekind = RTE_SUBQUERY;
1201 rte->subquery = newquery;
1202 rte->security_barrier = false;
1203
1204 /* Zero out CTE-specific fields */
1205 rte->ctename = NULL;
1206 rte->ctelevelsup = 0;
1207 rte->self_reference = false;
1208 rte->coltypes = NIL;
1209 rte->coltypmods = NIL;
1210 rte->colcollations = NIL;
1211 }
1212
1213 return false;
1214 }
1215
1216 return expression_tree_walker(node, inline_cte_walker, context);
1217 }
1218
1219 /*
1220 * Attempt to transform 'testexpr' over the VALUES subquery into
1221 * a ScalarArrayOpExpr. We currently support the transformation only when
1222 * it ends up with a constant array. Otherwise, the evaluation of non-hashed
1223 * SAOP might be slower than the corresponding Hash Join with VALUES.
1224 *
1225 * Return transformed ScalarArrayOpExpr or NULL if transformation isn't
1226 * allowed.
1227 */
1228 ScalarArrayOpExpr *
1229 convert_VALUES_to_ANY(PlannerInfo *root, Node *testexpr, Query *values)
1230 {
1231 RangeTblEntry *rte;
1232 Node *leftop;
1233 Node *rightop;
1234 Oid opno;
1235 ListCell *lc;
1236 Oid inputcollid;
1237 List *exprs = NIL;
1238
1239 /*
1240 * Check we have a binary operator over a single-column subquery with no
1241 * joins and no LIMIT/OFFSET/ORDER BY clauses.
1242 */
1243 if (!IsA(testexpr, OpExpr) ||
1244 list_length(((OpExpr *) testexpr)->args) != 2 ||
1245 list_length(values->targetList) > 1 ||
1246 values->limitCount != NULL ||
1247 values->limitOffset != NULL ||
1248 values->sortClause != NIL ||
1249 list_length(values->rtable) != 1)
1250 return NULL;
1251
1252 rte = linitial_node(RangeTblEntry, values->rtable);
1253 leftop = linitial(((OpExpr *) testexpr)->args);
1254 rightop = lsecond(((OpExpr *) testexpr)->args);
1255 opno = ((OpExpr *) testexpr)->opno;
1256 inputcollid = ((OpExpr *) testexpr)->inputcollid;
1257
1258 /*
1259 * Also, check that only RTE corresponds to VALUES; the list of values has
1260 * at least two items and no volatile functions.
1261 */
1262 if (rte->rtekind != RTE_VALUES ||
1263 list_length(rte->values_lists) < 2 ||
1264 contain_volatile_functions((Node *) rte->values_lists))
1265 return NULL;
1266
1267 foreach(lc, rte->values_lists)
1268 {
1269 List *elem = lfirst(lc);
1270 Node *value = linitial(elem);
1271
1272 /*
1273 * Prepare an evaluation of the right side of the operator with
1274 * substitution of the given value.
1275 */
1276 value = convert_testexpr(root, rightop, list_make1(value));
1277
1278 /*
1279 * Try to evaluate constant expressions. We could get Const as a
1280 * result.
1281 */
1282 value = eval_const_expressions(root, value);
1283
1284 /*
1285 * As we only support constant output arrays, all the items must also
1286 * be constant.
1287 */
1288 if (!IsA(value, Const))
1289 return NULL;
1290
1291 exprs = lappend(exprs, value);
1292 }
1293
1294 /* Finally, build ScalarArrayOpExpr at the top of the 'exprs' list. */
1295 return make_SAOP_expr(opno, leftop, exprType(rightop),
1296 linitial_oid(rte->colcollations), inputcollid,
1297 exprs, false);
1298 }
1299
1300 /*
1301 * convert_ANY_sublink_to_join: try to convert an ANY SubLink to a join
1302 *
1303 * The caller has found an ANY SubLink at the top level of one of the query's
1304 * qual clauses, but has not checked the properties of the SubLink further.
1305 * Decide whether it is appropriate to process this SubLink in join style.
1306 * If so, form a JoinExpr and return it. Return NULL if the SubLink cannot
1307 * be converted to a join.
1308 *
1309 * The only non-obvious input parameter is available_rels: this is the set
1310 * of query rels that can safely be referenced in the sublink expression.
1311 * (We must restrict this to avoid changing the semantics when a sublink
1312 * is present in an outer join's ON qual.) The conversion must fail if
1313 * the converted qual would reference any but these parent-query relids.
1314 *
1315 * On success, the returned JoinExpr has larg = NULL and rarg = the jointree
1316 * item representing the pulled-up subquery. The caller must set larg to
1317 * represent the relation(s) on the lefthand side of the new join, and insert
1318 * the JoinExpr into the upper query's jointree at an appropriate place
1319 * (typically, where the lefthand relation(s) had been). Note that the
1320 * passed-in SubLink must also be removed from its original position in the
1321 * query quals, since the quals of the returned JoinExpr replace it.
1322 * (Notionally, we replace the SubLink with a constant TRUE, then elide the
1323 * redundant constant from the qual.)
1324 *
1325 * On success, the caller is also responsible for recursively applying
1326 * pull_up_sublinks processing to the rarg and quals of the returned JoinExpr.
1327 * (On failure, there is no need to do anything, since pull_up_sublinks will
1328 * be applied when we recursively plan the sub-select.)
1329 *
1330 * Side effects of a successful conversion include adding the SubLink's
1331 * subselect to the query's rangetable, so that it can be referenced in
1332 * the JoinExpr's rarg.
1333 */
1334 JoinExpr *
1335 convert_ANY_sublink_to_join(PlannerInfo *root, SubLink *sublink,
1336 Relids available_rels)
1337 {
1338 JoinExpr *result;
1339 Query *parse = root->parse;
1340 Query *subselect = (Query *) sublink->subselect;
1341 Relids upper_varnos;
1342 int rtindex;
1343 ParseNamespaceItem *nsitem;
1344 RangeTblEntry *rte;
1345 RangeTblRef *rtr;
1346 List *subquery_vars;
1347 Node *quals;
1348 ParseState *pstate;
1349 Relids sub_ref_outer_relids;
1350 bool use_lateral;
1351
1352 Assert(sublink->subLinkType == ANY_SUBLINK);
1353
1354 /*
1355 * If the sub-select contains any Vars of the parent query, we treat it as
1356 * LATERAL. (Vars from higher levels don't matter here.)
1357 */
1358 sub_ref_outer_relids = pull_varnos_of_level(NULL, (Node *) subselect, 1);
1359 use_lateral = !bms_is_empty(sub_ref_outer_relids);
1360
1361 /*
1362 * Can't convert if the sub-select contains parent-level Vars of relations
1363 * not in available_rels.
1364 */
1365 if (!bms_is_subset(sub_ref_outer_relids, available_rels))
1366 return NULL;
1367
1368 /*
1369 * The test expression must contain some Vars of the parent query, else
1370 * it's not gonna be a join. (Note that it won't have Vars referring to
1371 * the subquery, rather Params.)
1372 */
1373 upper_varnos = pull_varnos(root, sublink->testexpr);
1374 if (bms_is_empty(upper_varnos))
1375 return NULL;
1376
1377 /*
1378 * However, it can't refer to anything outside available_rels.
1379 */
1380 if (!bms_is_subset(upper_varnos, available_rels))
1381 return NULL;
1382
1383 /*
1384 * The combining operators and left-hand expressions mustn't be volatile.
1385 */
1386 if (contain_volatile_functions(sublink->testexpr))
1387 return NULL;
1388
1389 /* Create a dummy ParseState for addRangeTableEntryForSubquery */
1390 pstate = make_parsestate(NULL);
1391
1392 /*
1393 * Okay, pull up the sub-select into upper range table.
1394 *
1395 * We rely here on the assumption that the outer query has no references
1396 * to the inner (necessarily true, other than the Vars that we build
1397 * below). Therefore this is a lot easier than what pull_up_subqueries has
1398 * to go through.
1399 */
1400 nsitem = addRangeTableEntryForSubquery(pstate,
1401 subselect,
1402 makeAlias("ANY_subquery", NIL),
1403 use_lateral,
1404 false);
1405 rte = nsitem->p_rte;
1406 parse->rtable = lappend(parse->rtable, rte);
1407 rtindex = list_length(parse->rtable);
1408
1409 /*
1410 * Form a RangeTblRef for the pulled-up sub-select.
1411 */
1412 rtr = makeNode(RangeTblRef);
1413 rtr->rtindex = rtindex;
1414
1415 /*
1416 * Build a list of Vars representing the subselect outputs.
1417 */
1418 subquery_vars = generate_subquery_vars(root,
1419 subselect->targetList,
1420 rtindex);
1421
1422 /*
1423 * Build the new join's qual expression, replacing Params with these Vars.
1424 */
1425 quals = convert_testexpr(root, sublink->testexpr, subquery_vars);
1426
1427 /*
1428 * And finally, build the JoinExpr node.
1429 */
1430 result = makeNode(JoinExpr);
1431 result->jointype = JOIN_SEMI;
1432 result->isNatural = false;
1433 result->larg = NULL; /* caller must fill this in */
1434 result->rarg = (Node *) rtr;
1435 result->usingClause = NIL;
1436 result->join_using_alias = NULL;
1437 result->quals = quals;
1438 result->alias = NULL;
1439 result->rtindex = 0; /* we don't need an RTE for it */
1440
1441 return result;
1442 }
1443
1444 /*
1445 * convert_EXISTS_sublink_to_join: try to convert an EXISTS SubLink to a join
1446 *
1447 * The API of this function is identical to convert_ANY_sublink_to_join's,
1448 * except that we also support the case where the caller has found NOT EXISTS,
1449 * so we need an additional input parameter "under_not".
1450 */
1451 JoinExpr *
1452 convert_EXISTS_sublink_to_join(PlannerInfo *root, SubLink *sublink,
1453 bool under_not, Relids available_rels)
1454 {
1455 JoinExpr *result;
1456 Query *parse = root->parse;
1457 Query *subselect = (Query *) sublink->subselect;
1458 Node *whereClause;
1459 int rtoffset;
1460 int varno;
1461 Relids clause_varnos;
1462 Relids upper_varnos;
1463
1464 Assert(sublink->subLinkType == EXISTS_SUBLINK);
1465
1466 /*
1467 * Can't flatten if it contains WITH. (We could arrange to pull up the
1468 * WITH into the parent query's cteList, but that risks changing the
1469 * semantics, since a WITH ought to be executed once per associated query
1470 * call.) Note that convert_ANY_sublink_to_join doesn't have to reject
1471 * this case, since it just produces a subquery RTE that doesn't have to
1472 * get flattened into the parent query.
1473 */
1474 if (subselect->cteList)
1475 return NULL;
1476
1477 /*
1478 * Copy the subquery so we can modify it safely (see comments in
1479 * make_subplan).
1480 */
1481 subselect = copyObject(subselect);
1482
1483 /*
1484 * See if the subquery can be simplified based on the knowledge that it's
1485 * being used in EXISTS(). If we aren't able to get rid of its
1486 * targetlist, we have to fail, because the pullup operation leaves us
1487 * with noplace to evaluate the targetlist.
1488 */
1489 if (!simplify_EXISTS_query(root, subselect))
1490 return NULL;
1491
1492 /*
1493 * Separate out the WHERE clause. (We could theoretically also remove
1494 * top-level plain JOIN/ON clauses, but it's probably not worth the
1495 * trouble.)
1496 */
1497 whereClause = subselect->jointree->quals;
1498 subselect->jointree->quals = NULL;
1499
1500 /*
1501 * The rest of the sub-select must not refer to any Vars of the parent
1502 * query. (Vars of higher levels should be okay, though.)
1503 */
1504 if (contain_vars_of_level((Node *) subselect, 1))
1505 return NULL;
1506
1507 /*
1508 * On the other hand, the WHERE clause must contain some Vars of the
1509 * parent query, else it's not gonna be a join.
1510 */
1511 if (!contain_vars_of_level(whereClause, 1))
1512 return NULL;
1513
1514 /*
1515 * We don't risk optimizing if the WHERE clause is volatile, either.
1516 */
1517 if (contain_volatile_functions(whereClause))
1518 return NULL;
1519
1520 /*
1521 * The subquery must have a nonempty jointree, but we can make it so.
1522 */
1523 replace_empty_jointree(subselect);
1524
1525 /*
1526 * Prepare to pull up the sub-select into top range table.
1527 *
1528 * We rely here on the assumption that the outer query has no references
1529 * to the inner (necessarily true). Therefore this is a lot easier than
1530 * what pull_up_subqueries has to go through.
1531 *
1532 * In fact, it's even easier than what convert_ANY_sublink_to_join has to
1533 * do. The machinations of simplify_EXISTS_query ensured that there is
1534 * nothing interesting in the subquery except an rtable and jointree, and
1535 * even the jointree FromExpr no longer has quals. So we can just append
1536 * the rtable to our own and use the FromExpr in our jointree. But first,
1537 * adjust all level-zero varnos in the subquery to account for the rtable
1538 * merger.
1539 */
1540 rtoffset = list_length(parse->rtable);
1541 OffsetVarNodes((Node *) subselect, rtoffset, 0);
1542 OffsetVarNodes(whereClause, rtoffset, 0);
1543
1544 /*
1545 * Upper-level vars in subquery will now be one level closer to their
1546 * parent than before; in particular, anything that had been level 1
1547 * becomes level zero.
1548 */
1549 IncrementVarSublevelsUp((Node *) subselect, -1, 1);
1550 IncrementVarSublevelsUp(whereClause, -1, 1);
1551
1552 /*
1553 * Now that the WHERE clause is adjusted to match the parent query
1554 * environment, we can easily identify all the level-zero rels it uses.
1555 * The ones <= rtoffset belong to the upper query; the ones > rtoffset do
1556 * not.
1557 */
1558 clause_varnos = pull_varnos(root, whereClause);
1559 upper_varnos = NULL;
1560 varno = -1;
1561 while ((varno = bms_next_member(clause_varnos, varno)) >= 0)
1562 {
1563 if (varno <= rtoffset)
1564 upper_varnos = bms_add_member(upper_varnos, varno);
1565 }
1566 bms_free(clause_varnos);
1567 Assert(!bms_is_empty(upper_varnos));
1568
1569 /*
1570 * Now that we've got the set of upper-level varnos, we can make the last
1571 * check: only available_rels can be referenced.
1572 */
1573 if (!bms_is_subset(upper_varnos, available_rels))
1574 return NULL;
1575
1576 /*
1577 * Now we can attach the modified subquery rtable to the parent. This also
1578 * adds subquery's RTEPermissionInfos into the upper query.
1579 */
1580 CombineRangeTables(&parse->rtable, &parse->rteperminfos,
1581 subselect->rtable, subselect->rteperminfos);
1582
1583 /*
1584 * And finally, build the JoinExpr node.
1585 */
1586 result = makeNode(JoinExpr);
1587 result->jointype = under_not ? JOIN_ANTI : JOIN_SEMI;
1588 result->isNatural = false;
1589 result->larg = NULL; /* caller must fill this in */
1590 /* flatten out the FromExpr node if it's useless */
1591 if (list_length(subselect->jointree->fromlist) == 1)
1592 result->rarg = (Node *) linitial(subselect->jointree->fromlist);
1593 else
1594 result->rarg = (Node *) subselect->jointree;
1595 result->usingClause = NIL;
1596 result->join_using_alias = NULL;
1597 result->quals = whereClause;
1598 result->alias = NULL;
1599 result->rtindex = 0; /* we don't need an RTE for it */
1600
1601 return result;
1602 }
1603
1604 /*
1605 * simplify_EXISTS_query: remove any useless stuff in an EXISTS's subquery
1606 *
1607 * The only thing that matters about an EXISTS query is whether it returns
1608 * zero or more than zero rows. Therefore, we can remove certain SQL features
1609 * that won't affect that. The only part that is really likely to matter in
1610 * typical usage is simplifying the targetlist: it's a common habit to write
1611 * "SELECT * FROM" even though there is no need to evaluate any columns.
1612 *
1613 * Note: by suppressing the targetlist we could cause an observable behavioral
1614 * change, namely that any errors that might occur in evaluating the tlist
1615 * won't occur, nor will other side-effects of volatile functions. This seems
1616 * unlikely to bother anyone in practice.
1617 *
1618 * Returns true if was able to discard the targetlist, else false.
1619 */
1620 static bool
1621 simplify_EXISTS_query(PlannerInfo *root, Query *query)
1622 {
1623 /*
1624 * We don't try to simplify at all if the query uses set operations,
1625 * aggregates, grouping sets, SRFs, modifying CTEs, HAVING, OFFSET, or FOR
1626 * UPDATE/SHARE; none of these seem likely in normal usage and their
1627 * possible effects are complex. (Note: we could ignore an "OFFSET 0"
1628 * clause, but that traditionally is used as an optimization fence, so we
1629 * don't.)
1630 */
1631 if (query->commandType != CMD_SELECT ||
1632 query->setOperations ||
1633 query->hasAggs ||
1634 query->groupingSets ||
1635 query->hasWindowFuncs ||
1636 query->hasTargetSRFs ||
1637 query->hasModifyingCTE ||
1638 query->havingQual ||
1639 query->limitOffset ||
1640 query->rowMarks)
1641 return false;
1642
1643 /*
1644 * LIMIT with a constant positive (or NULL) value doesn't affect the
1645 * semantics of EXISTS, so let's ignore such clauses. This is worth doing
1646 * because people accustomed to certain other DBMSes may be in the habit
1647 * of writing EXISTS(SELECT ... LIMIT 1) as an optimization. If there's a
1648 * LIMIT with anything else as argument, though, we can't simplify.
1649 */
1650 if (query->limitCount)
1651 {
1652 /*
1653 * The LIMIT clause has not yet been through eval_const_expressions,
1654 * so we have to apply that here. It might seem like this is a waste
1655 * of cycles, since the only case plausibly worth worrying about is
1656 * "LIMIT 1" ... but what we'll actually see is "LIMIT int8(1::int4)",
1657 * so we have to fold constants or we're not going to recognize it.
1658 */
1659 Node *node = eval_const_expressions(root, query->limitCount);
1660 Const *limit;
1661
1662 /* Might as well update the query if we simplified the clause. */
1663 query->limitCount = node;
1664
1665 if (!IsA(node, Const))
1666 return false;
1667
1668 limit = (Const *) node;
1669 Assert(limit->consttype == INT8OID);
1670 if (!limit->constisnull && DatumGetInt64(limit->constvalue) <= 0)
1671 return false;
1672
1673 /* Whether or not the targetlist is safe, we can drop the LIMIT. */
1674 query->limitCount = NULL;
1675 }
1676
1677 /*
1678 * Otherwise, we can throw away the targetlist, as well as any GROUP,
1679 * WINDOW, DISTINCT, and ORDER BY clauses; none of those clauses will
1680 * change a nonzero-rows result to zero rows or vice versa. (Furthermore,
1681 * since our parsetree representation of these clauses depends on the
1682 * targetlist, we'd better throw them away if we drop the targetlist.)
1683 */
1684 query->targetList = NIL;
1685 query->groupClause = NIL;
1686 query->windowClause = NIL;
1687 query->distinctClause = NIL;
1688 query->sortClause = NIL;
1689 query->hasDistinctOn = false;
1690
1691 /*
1692 * Since we have thrown away the GROUP BY clauses, we'd better get rid of
1693 * the RTE_GROUP RTE and clear the hasGroupRTE flag. To safely get rid of
1694 * the RTE_GROUP RTE without shifting the index of any subsequent RTE in
1695 * the rtable, we convert the RTE to be RTE_RESULT type in-place, and zero
1696 * out RTE_GROUP-specific fields.
1697 */
1698 if (query->hasGroupRTE)
1699 {
1700 foreach_node(RangeTblEntry, rte, query->rtable)
1701 {
1702 if (rte->rtekind == RTE_GROUP)
1703 {
1704 rte->rtekind = RTE_RESULT;
1705 rte->groupexprs = NIL;
1706
1707 /* A query should only have one RTE_GROUP, so we can stop. */
1708 break;
1709 }
1710 }
1711
1712 query->hasGroupRTE = false;
1713 }
1714
1715 return true;
1716 }
1717
1718 /*
1719 * convert_EXISTS_to_ANY: try to convert EXISTS to a hashable ANY sublink
1720 *
1721 * The subselect is expected to be a fresh copy that we can munge up,
1722 * and to have been successfully passed through simplify_EXISTS_query.
1723 *
1724 * On success, the modified subselect is returned, and we store a suitable
1725 * upper-level test expression at *testexpr, plus a list of the subselect's
1726 * output Params at *paramIds. (The test expression is already Param-ified
1727 * and hence need not go through convert_testexpr, which is why we have to
1728 * deal with the Param IDs specially.)
1729 *
1730 * On failure, returns NULL.
1731 */
1732 static Query *
1733 convert_EXISTS_to_ANY(PlannerInfo *root, Query *subselect,
1734 Node **testexpr, List **paramIds)
1735 {
1736 Node *whereClause;
1737 List *leftargs,
1738 *rightargs,
1739 *opids,
1740 *opcollations,
1741 *newWhere,
1742 *tlist,
1743 *testlist,
1744 *paramids;
1745 ListCell *lc,
1746 *rc,
1747 *oc,
1748 *cc;
1749 AttrNumber resno;
1750
1751 /*
1752 * Query must not require a targetlist, since we have to insert a new one.
1753 * Caller should have dealt with the case already.
1754 */
1755 Assert(subselect->targetList == NIL);
1756
1757 /*
1758 * Separate out the WHERE clause. (We could theoretically also remove
1759 * top-level plain JOIN/ON clauses, but it's probably not worth the
1760 * trouble.)
1761 */
1762 whereClause = subselect->jointree->quals;
1763 subselect->jointree->quals = NULL;
1764
1765 /*
1766 * The rest of the sub-select must not refer to any Vars of the parent
1767 * query. (Vars of higher levels should be okay, though.)
1768 *
1769 * Note: we need not check for Aggrefs separately because we know the
1770 * sub-select is as yet unoptimized; any uplevel Aggref must therefore
1771 * contain an uplevel Var reference. This is not the case below ...
1772 */
1773 if (contain_vars_of_level((Node *) subselect, 1))
1774 return NULL;
1775
1776 /*
1777 * We don't risk optimizing if the WHERE clause is volatile, either.
1778 */
1779 if (contain_volatile_functions(whereClause))
1780 return NULL;
1781
1782 /*
1783 * Clean up the WHERE clause by doing const-simplification etc on it.
1784 * Aside from simplifying the processing we're about to do, this is
1785 * important for being able to pull chunks of the WHERE clause up into the
1786 * parent query. Since we are invoked partway through the parent's
1787 * preprocess_expression() work, earlier steps of preprocess_expression()
1788 * wouldn't get applied to the pulled-up stuff unless we do them here. For
1789 * the parts of the WHERE clause that get put back into the child query,
1790 * this work is partially duplicative, but it shouldn't hurt.
1791 *
1792 * Note: we do not run flatten_join_alias_vars. This is OK because any
1793 * parent aliases were flattened already, and we're not going to pull any
1794 * child Vars (of any description) into the parent.
1795 *
1796 * Note: passing the parent's root to eval_const_expressions is
1797 * technically wrong, but we can get away with it since only the
1798 * boundParams (if any) are used, and those would be the same in a
1799 * subroot.
1800 */
1801 whereClause = eval_const_expressions(root, whereClause);
1802 whereClause = (Node *) canonicalize_qual((Expr *) whereClause, false);
1803 whereClause = (Node *) make_ands_implicit((Expr *) whereClause);
1804
1805 /*
1806 * We now have a flattened implicit-AND list of clauses, which we try to
1807 * break apart into "outervar = innervar" hash clauses. Anything that
1808 * can't be broken apart just goes back into the newWhere list. Note that
1809 * we aren't trying hard yet to ensure that we have only outer or only
1810 * inner on each side; we'll check that if we get to the end.
1811 */
1812 leftargs = rightargs = opids = opcollations = newWhere = NIL;
1813 foreach(lc, (List *) whereClause)
1814 {
1815 OpExpr *expr = (OpExpr *) lfirst(lc);
1816
1817 if (IsA(expr, OpExpr) &&
1818 hash_ok_operator(expr))
1819 {
1820 Node *leftarg = (Node *) linitial(expr->args);
1821 Node *rightarg = (Node *) lsecond(expr->args);
1822
1823 if (contain_vars_of_level(leftarg, 1))
1824 {
1825 leftargs = lappend(leftargs, leftarg);
1826 rightargs = lappend(rightargs, rightarg);
1827 opids = lappend_oid(opids, expr->opno);
1828 opcollations = lappend_oid(opcollations, expr->inputcollid);
1829 continue;
1830 }
1831 if (contain_vars_of_level(rightarg, 1))
1832 {
1833 /*
1834 * We must commute the clause to put the outer var on the
1835 * left, because the hashing code in nodeSubplan.c expects
1836 * that. This probably shouldn't ever fail, since hashable
1837 * operators ought to have commutators, but be paranoid.
1838 */
1839 expr->opno = get_commutator(expr->opno);
1840 if (OidIsValid(expr->opno) && hash_ok_operator(expr))
1841 {
1842 leftargs = lappend(leftargs, rightarg);
1843 rightargs = lappend(rightargs, leftarg);
1844 opids = lappend_oid(opids, expr->opno);
1845 opcollations = lappend_oid(opcollations, expr->inputcollid);
1846 continue;
1847 }
1848 /* If no commutator, no chance to optimize the WHERE clause */
1849 return NULL;
1850 }
1851 }
1852 /* Couldn't handle it as a hash clause */
1853 newWhere = lappend(newWhere, expr);
1854 }
1855
1856 /*
1857 * If we didn't find anything we could convert, fail.
1858 */
1859 if (leftargs == NIL)
1860 return NULL;
1861
1862 /*
1863 * There mustn't be any parent Vars or Aggs in the stuff that we intend to
1864 * put back into the child query. Note: you might think we don't need to
1865 * check for Aggs separately, because an uplevel Agg must contain an
1866 * uplevel Var in its argument. But it is possible that the uplevel Var
1867 * got optimized away by eval_const_expressions. Consider
1868 *
1869 * SUM(CASE WHEN false THEN uplevelvar ELSE 0 END)
1870 */
1871 if (contain_vars_of_level((Node *) newWhere, 1) ||
1872 contain_vars_of_level((Node *) rightargs, 1))
1873 return NULL;
1874 if (root->parse->hasAggs &&
1875 (contain_aggs_of_level((Node *) newWhere, 1) ||
1876 contain_aggs_of_level((Node *) rightargs, 1)))
1877 return NULL;
1878
1879 /*
1880 * And there can't be any child Vars in the stuff we intend to pull up.
1881 * (Note: we'd need to check for child Aggs too, except we know the child
1882 * has no aggs at all because of simplify_EXISTS_query's check. The same
1883 * goes for window functions.)
1884 */
1885 if (contain_vars_of_level((Node *) leftargs, 0))
1886 return NULL;
1887
1888 /*
1889 * Also reject sublinks in the stuff we intend to pull up. (It might be
1890 * possible to support this, but doesn't seem worth the complication.)
1891 */
1892 if (contain_subplans((Node *) leftargs))
1893 return NULL;
1894
1895 /*
1896 * Okay, adjust the sublevelsup in the stuff we're pulling up.
1897 */
1898 IncrementVarSublevelsUp((Node *) leftargs, -1, 1);
1899
1900 /*
1901 * Put back any child-level-only WHERE clauses.
1902 */
1903 if (newWhere)
1904 subselect->jointree->quals = (Node *) make_ands_explicit(newWhere);
1905
1906 /*
1907 * Build a new targetlist for the child that emits the expressions we
1908 * need. Concurrently, build a testexpr for the parent using Params to
1909 * reference the child outputs. (Since we generate Params directly here,
1910 * there will be no need to convert the testexpr in build_subplan.)
1911 */
1912 tlist = testlist = paramids = NIL;
1913 resno = 1;
1914 forfour(lc, leftargs, rc, rightargs, oc, opids, cc, opcollations)
1915 {
1916 Node *leftarg = (Node *) lfirst(lc);
1917 Node *rightarg = (Node *) lfirst(rc);
1918 Oid opid = lfirst_oid(oc);
1919 Oid opcollation = lfirst_oid(cc);
1920 Param *param;
1921
1922 param = generate_new_exec_param(root,
1923 exprType(rightarg),
1924 exprTypmod(rightarg),
1925 exprCollation(rightarg));
1926 tlist = lappend(tlist,
1927 makeTargetEntry((Expr *) rightarg,
1928 resno++,
1929 NULL,
1930 false));
1931 testlist = lappend(testlist,
1932 make_opclause(opid, BOOLOID, false,
1933 (Expr *) leftarg, (Expr *) param,
1934 InvalidOid, opcollation));
1935 paramids = lappend_int(paramids, param->paramid);
1936 }
1937
1938 /* Put everything where it should go, and we're done */
1939 subselect->targetList = tlist;
1940 *testexpr = (Node *) make_ands_explicit(testlist);
1941 *paramIds = paramids;
1942
1943 return subselect;
1944 }
1945
1946
1947 /*
1948 * Replace correlation vars (uplevel vars) with Params.
1949 *
1950 * Uplevel PlaceHolderVars, aggregates, GROUPING() expressions,
1951 * MergeSupportFuncs, and ReturningExprs are replaced, too.
1952 *
1953 * Note: it is critical that this runs immediately after SS_process_sublinks.
1954 * Since we do not recurse into the arguments of uplevel PHVs and aggregates,
1955 * they will get copied to the appropriate subplan args list in the parent
1956 * query with uplevel vars not replaced by Params, but only adjusted in level
1957 * (see replace_outer_placeholdervar and replace_outer_agg). That's exactly
1958 * what we want for the vars of the parent level --- but if a PHV's or
1959 * aggregate's argument contains any further-up variables, they have to be
1960 * replaced with Params in their turn. That will happen when the parent level
1961 * runs SS_replace_correlation_vars. Therefore it must do so after expanding
1962 * its sublinks to subplans. And we don't want any steps in between, else
1963 * those steps would never get applied to the argument expressions, either in
1964 * the parent or the child level.
1965 *
1966 * Another fairly tricky thing going on here is the handling of SubLinks in
1967 * the arguments of uplevel PHVs/aggregates. Those are not touched inside the
1968 * intermediate query level, either. Instead, SS_process_sublinks recurses on
1969 * them after copying the PHV or Aggref expression into the parent plan level
1970 * (this is actually taken care of in build_subplan).
1971 */
1972 Node *
1973 SS_replace_correlation_vars(PlannerInfo *root, Node *expr)
1974 {
1975 /* No setup needed for tree walk, so away we go */
1976 return replace_correlation_vars_mutator(expr, root);
1977 }
1978
1979 static Node *
1980 replace_correlation_vars_mutator(Node *node, PlannerInfo *root)
1981 {
1982 if (node == NULL)
1983 return NULL;
1984 if (IsA(node, Var))
1985 {
1986 if (((Var *) node)->varlevelsup > 0)
1987 return (Node *) replace_outer_var(root, (Var *) node);
1988 }
1989 if (IsA(node, PlaceHolderVar))
1990 {
1991 if (((PlaceHolderVar *) node)->phlevelsup > 0)
1992 return (Node *) replace_outer_placeholdervar(root,
1993 (PlaceHolderVar *) node);
1994 }
1995 if (IsA(node, Aggref))
1996 {
1997 if (((Aggref *) node)->agglevelsup > 0)
1998 return (Node *) replace_outer_agg(root, (Aggref *) node);
1999 }
2000 if (IsA(node, GroupingFunc))
2001 {
2002 if (((GroupingFunc *) node)->agglevelsup > 0)
2003 return (Node *) replace_outer_grouping(root, (GroupingFunc *) node);
2004 }
2005 if (IsA(node, MergeSupportFunc))
2006 {
2007 if (root->parse->commandType != CMD_MERGE)
2008 return (Node *) replace_outer_merge_support(root,
2009 (MergeSupportFunc *) node);
2010 }
2011 if (IsA(node, ReturningExpr))
2012 {
2013 if (((ReturningExpr *) node)->retlevelsup > 0)
2014 return (Node *) replace_outer_returning(root,
2015 (ReturningExpr *) node);
2016 }
2017 return expression_tree_mutator(node, replace_correlation_vars_mutator, root);
2018 }
2019
2020 /*
2021 * Expand SubLinks to SubPlans in the given expression.
2022 *
2023 * The isQual argument tells whether or not this expression is a WHERE/HAVING
2024 * qualifier expression. If it is, any sublinks appearing at top level need
2025 * not distinguish FALSE from UNKNOWN return values.
2026 */
2027 Node *
2028 SS_process_sublinks(PlannerInfo *root, Node *expr, bool isQual)
2029 {
2030 process_sublinks_context context;
2031
2032 context.root = root;
2033 context.isTopQual = isQual;
2034 return process_sublinks_mutator(expr, &context);
2035 }
2036
2037 static Node *
2038 process_sublinks_mutator(Node *node, process_sublinks_context *context)
2039 {
2040 process_sublinks_context locContext;
2041
2042 locContext.root = context->root;
2043
2044 if (node == NULL)
2045 return NULL;
2046 if (IsA(node, SubLink))
2047 {
2048 SubLink *sublink = (SubLink *) node;
2049 Node *testexpr;
2050
2051 /*
2052 * First, recursively process the lefthand-side expressions, if any.
2053 * They're not top-level anymore.
2054 */
2055 locContext.isTopQual = false;
2056 testexpr = process_sublinks_mutator(sublink->testexpr, &locContext);
2057
2058 /*
2059 * Now build the SubPlan node and make the expr to return.
2060 */
2061 return make_subplan(context->root,
2062 (Query *) sublink->subselect,
2063 sublink->subLinkType,
2064 sublink->subLinkId,
2065 testexpr,
2066 context->isTopQual);
2067 }
2068
2069 /*
2070 * Don't recurse into the arguments of an outer PHV, Aggref, GroupingFunc,
2071 * or ReturningExpr here. Any SubLinks in the arguments have to be dealt
2072 * with at the outer query level; they'll be handled when build_subplan
2073 * collects the PHV, Aggref, GroupingFunc, or ReturningExpr into the
2074 * arguments to be passed down to the current subplan.
2075 */
2076 if (IsA(node, PlaceHolderVar))
2077 {
2078 if (((PlaceHolderVar *) node)->phlevelsup > 0)
2079 return node;
2080 }
2081 else if (IsA(node, Aggref))
2082 {
2083 if (((Aggref *) node)->agglevelsup > 0)
2084 return node;
2085 }
2086 else if (IsA(node, GroupingFunc))
2087 {
2088 if (((GroupingFunc *) node)->agglevelsup > 0)
2089 return node;
2090 }
2091 else if (IsA(node, ReturningExpr))
2092 {
2093 if (((ReturningExpr *) node)->retlevelsup > 0)
2094 return node;
2095 }
2096
2097 /*
2098 * We should never see a SubPlan expression in the input (since this is
2099 * the very routine that creates 'em to begin with). We shouldn't find
2100 * ourselves invoked directly on a Query, either.
2101 */
2102 Assert(!IsA(node, SubPlan));
2103 Assert(!IsA(node, AlternativeSubPlan));
2104 Assert(!IsA(node, Query));
2105
2106 /*
2107 * Because make_subplan() could return an AND or OR clause, we have to
2108 * take steps to preserve AND/OR flatness of a qual. We assume the input
2109 * has been AND/OR flattened and so we need no recursion here.
2110 *
2111 * (Due to the coding here, we will not get called on the List subnodes of
2112 * an AND; and the input is *not* yet in implicit-AND format. So no check
2113 * is needed for a bare List.)
2114 *
2115 * Anywhere within the top-level AND/OR clause structure, we can tell
2116 * make_subplan() that NULL and FALSE are interchangeable. So isTopQual
2117 * propagates down in both cases. (Note that this is unlike the meaning
2118 * of "top level qual" used in most other places in Postgres.)
2119 */
2120 if (is_andclause(node))
2121 {
2122 List *newargs = NIL;
2123 ListCell *l;
2124
2125 /* Still at qual top-level */
2126 locContext.isTopQual = context->isTopQual;
2127
2128 foreach(l, ((BoolExpr *) node)->args)
2129 {
2130 Node *newarg;
2131
2132 newarg = process_sublinks_mutator(lfirst(l), &locContext);
2133 if (is_andclause(newarg))
2134 newargs = list_concat(newargs, ((BoolExpr *) newarg)->args);
2135 else
2136 newargs = lappend(newargs, newarg);
2137 }
2138 return (Node *) make_andclause(newargs);
2139 }
2140
2141 if (is_orclause(node))
2142 {
2143 List *newargs = NIL;
2144 ListCell *l;
2145
2146 /* Still at qual top-level */
2147 locContext.isTopQual = context->isTopQual;
2148
2149 foreach(l, ((BoolExpr *) node)->args)
2150 {
2151 Node *newarg;
2152
2153 newarg = process_sublinks_mutator(lfirst(l), &locContext);
2154 if (is_orclause(newarg))
2155 newargs = list_concat(newargs, ((BoolExpr *) newarg)->args);
2156 else
2157 newargs = lappend(newargs, newarg);
2158 }
2159 return (Node *) make_orclause(newargs);
2160 }
2161
2162 /*
2163 * If we recurse down through anything other than an AND or OR node, we
2164 * are definitely not at top qual level anymore.
2165 */
2166 locContext.isTopQual = false;
2167
2168 return expression_tree_mutator(node,
2169 process_sublinks_mutator,
2170 &locContext);
2171 }
2172
2173 /*
2174 * SS_identify_outer_params - identify the Params available from outer levels
2175 *
2176 * This must be run after SS_replace_correlation_vars and SS_process_sublinks
2177 * processing is complete in a given query level as well as all of its
2178 * descendant levels (which means it's most practical to do it at the end of
2179 * processing the query level). We compute the set of paramIds that outer
2180 * levels will make available to this level+descendants, and record it in
2181 * root->outer_params for use while computing extParam/allParam sets in final
2182 * plan cleanup. (We can't just compute it then, because the upper levels'
2183 * plan_params lists are transient and will be gone by then.)
2184 */
2185 void
2186 SS_identify_outer_params(PlannerInfo *root)
2187 {
2188 Bitmapset *outer_params;
2189 PlannerInfo *proot;
2190 ListCell *l;
2191
2192 /*
2193 * If no parameters have been assigned anywhere in the tree, we certainly
2194 * don't need to do anything here.
2195 */
2196 if (root->glob->paramExecTypes == NIL)
2197 return;
2198
2199 /*
2200 * Scan all query levels above this one to see which parameters are due to
2201 * be available from them, either because lower query levels have
2202 * requested them (via plan_params) or because they will be available from
2203 * initPlans of those levels.
2204 */
2205 outer_params = NULL;
2206 for (proot = root->parent_root; proot != NULL; proot = proot->parent_root)
2207 {
2208 /*
2209 * Include ordinary Var/PHV/Aggref/GroupingFunc/ReturningExpr params.
2210 */
2211 foreach(l, proot->plan_params)
2212 {
2213 PlannerParamItem *pitem = (PlannerParamItem *) lfirst(l);
2214
2215 outer_params = bms_add_member(outer_params, pitem->paramId);
2216 }
2217 /* Include any outputs of outer-level initPlans */
2218 foreach(l, proot->init_plans)
2219 {
2220 SubPlan *initsubplan = (SubPlan *) lfirst(l);
2221 ListCell *l2;
2222
2223 foreach(l2, initsubplan->setParam)
2224 {
2225 outer_params = bms_add_member(outer_params, lfirst_int(l2));
2226 }
2227 }
2228 /* Include worktable ID, if a recursive query is being planned */
2229 if (proot->wt_param_id >= 0)
2230 outer_params = bms_add_member(outer_params, proot->wt_param_id);
2231 }
2232 root->outer_params = outer_params;
2233 }
2234
2235 /*
2236 * SS_charge_for_initplans - account for initplans in Path costs & parallelism
2237 *
2238 * If any initPlans have been created in the current query level, they will
2239 * get attached to the Plan tree created from whichever Path we select from
2240 * the given rel. Increment all that rel's Paths' costs to account for them,
2241 * and if any of the initPlans are parallel-unsafe, mark all the rel's Paths
2242 * parallel-unsafe as well.
2243 *
2244 * This is separate from SS_attach_initplans because we might conditionally
2245 * create more initPlans during create_plan(), depending on which Path we
2246 * select. However, Paths that would generate such initPlans are expected
2247 * to have included their cost and parallel-safety effects already.
2248 */
2249 void
2250 SS_charge_for_initplans(PlannerInfo *root, RelOptInfo *final_rel)
2251 {
2252 Cost initplan_cost;
2253 bool unsafe_initplans;
2254 ListCell *lc;
2255
2256 /* Nothing to do if no initPlans */
2257 if (root->init_plans == NIL)
2258 return;
2259
2260 /*
2261 * Compute the cost increment just once, since it will be the same for all
2262 * Paths. Also check for parallel-unsafe initPlans.
2263 */
2264 SS_compute_initplan_cost(root->init_plans,
2265 &initplan_cost, &unsafe_initplans);
2266
2267 /*
2268 * Now adjust the costs and parallel_safe flags.
2269 */
2270 foreach(lc, final_rel->pathlist)
2271 {
2272 Path *path = (Path *) lfirst(lc);
2273
2274 path->startup_cost += initplan_cost;
2275 path->total_cost += initplan_cost;
2276 if (unsafe_initplans)
2277 path->parallel_safe = false;
2278 }
2279
2280 /*
2281 * Adjust partial paths' costs too, or forget them entirely if we must
2282 * consider the rel parallel-unsafe.
2283 */
2284 if (unsafe_initplans)
2285 {
2286 final_rel->partial_pathlist = NIL;
2287 final_rel->consider_parallel = false;
2288 }
2289 else
2290 {
2291 foreach(lc, final_rel->partial_pathlist)
2292 {
2293 Path *path = (Path *) lfirst(lc);
2294
2295 path->startup_cost += initplan_cost;
2296 path->total_cost += initplan_cost;
2297 }
2298 }
2299
2300 /* We needn't do set_cheapest() here, caller will do it */
2301 }
2302
2303 /*
2304 * SS_compute_initplan_cost - count up the cost delta for some initplans
2305 *
2306 * The total cost returned in *initplan_cost_p should be added to both the
2307 * startup and total costs of the plan node the initplans get attached to.
2308 * We also report whether any of the initplans are not parallel-safe.
2309 *
2310 * The primary user of this is SS_charge_for_initplans, but it's also
2311 * used in adjusting costs when we move initplans to another plan node.
2312 */
2313 void
2314 SS_compute_initplan_cost(List *init_plans,
2315 Cost *initplan_cost_p,
2316 bool *unsafe_initplans_p)
2317 {
2318 Cost initplan_cost;
2319 bool unsafe_initplans;
2320 ListCell *lc;
2321
2322 /*
2323 * We assume each initPlan gets run once during top plan startup. This is
2324 * a conservative overestimate, since in fact an initPlan might be
2325 * executed later than plan startup, or even not at all.
2326 */
2327 initplan_cost = 0;
2328 unsafe_initplans = false;
2329 foreach(lc, init_plans)
2330 {
2331 SubPlan *initsubplan = lfirst_node(SubPlan, lc);
2332
2333 initplan_cost += initsubplan->startup_cost + initsubplan->per_call_cost;
2334 if (!initsubplan->parallel_safe)
2335 unsafe_initplans = true;
2336 }
2337 *initplan_cost_p = initplan_cost;
2338 *unsafe_initplans_p = unsafe_initplans;
2339 }
2340
2341 /*
2342 * SS_attach_initplans - attach initplans to topmost plan node
2343 *
2344 * Attach any initplans created in the current query level to the specified
2345 * plan node, which should normally be the topmost node for the query level.
2346 * (In principle the initPlans could go in any node at or above where they're
2347 * referenced; but there seems no reason to put them any lower than the
2348 * topmost node, so we don't bother to track exactly where they came from.)
2349 *
2350 * We do not touch the plan node's cost or parallel_safe flag. The initplans
2351 * must have been accounted for in SS_charge_for_initplans, or by any later
2352 * code that adds initplans via SS_make_initplan_from_plan.
2353 */
2354 void
2355 SS_attach_initplans(PlannerInfo *root, Plan *plan)
2356 {
2357 plan->initPlan = root->init_plans;
2358 }
2359
2360 /*
2361 * SS_finalize_plan - do final parameter processing for a completed Plan.
2362 *
2363 * This recursively computes the extParam and allParam sets for every Plan
2364 * node in the given plan tree. (Oh, and RangeTblFunction.funcparams too.)
2365 *
2366 * We assume that SS_finalize_plan has already been run on any initplans or
2367 * subplans the plan tree could reference.
2368 */
2369 void
2370 SS_finalize_plan(PlannerInfo *root, Plan *plan)
2371 {
2372 /* No setup needed, just recurse through plan tree. */
2373 (void) finalize_plan(root, plan, -1, root->outer_params, NULL);
2374 }
2375
2376 /*
2377 * Recursive processing of all nodes in the plan tree
2378 *
2379 * gather_param is the rescan_param of an ancestral Gather/GatherMerge,
2380 * or -1 if there is none.
2381 *
2382 * valid_params is the set of param IDs supplied by outer plan levels
2383 * that are valid to reference in this plan node or its children.
2384 *
2385 * scan_params is a set of param IDs to force scan plan nodes to reference.
2386 * This is for EvalPlanQual support, and is always NULL at the top of the
2387 * recursion.
2388 *
2389 * The return value is the computed allParam set for the given Plan node.
2390 * This is just an internal notational convenience: we can add a child
2391 * plan's allParams to the set of param IDs of interest to this level
2392 * in the same statement that recurses to that child.
2393 *
2394 * Do not scribble on caller's values of valid_params or scan_params!
2395 *
2396 * Note: although we attempt to deal with initPlans anywhere in the tree, the
2397 * logic is not really right. The problem is that a plan node might return an
2398 * output Param of its initPlan as a targetlist item, in which case it's valid
2399 * for the parent plan level to reference that same Param; the parent's usage
2400 * will be converted into a Var referencing the child plan node by setrefs.c.
2401 * But this function would see the parent's reference as out of scope and
2402 * complain about it. For now, this does not matter because the planner only
2403 * attaches initPlans to the topmost plan node in a query level, so the case
2404 * doesn't arise. If we ever merge this processing into setrefs.c, maybe it
2405 * can be handled more cleanly.
2406 */
2407 static Bitmapset *
2408 finalize_plan(PlannerInfo *root, Plan *plan,
2409 int gather_param,
2410 Bitmapset *valid_params,
2411 Bitmapset *scan_params)
2412 {
2413 finalize_primnode_context context;
2414 int locally_added_param;
2415 Bitmapset *nestloop_params;
2416 Bitmapset *initExtParam;
2417 Bitmapset *initSetParam;
2418 Bitmapset *child_params;
2419 ListCell *l;
2420
2421 if (plan == NULL)
2422 return NULL;
2423
2424 context.root = root;
2425 context.paramids = NULL; /* initialize set to empty */
2426 locally_added_param = -1; /* there isn't one */
2427 nestloop_params = NULL; /* there aren't any */
2428
2429 /*
2430 * Examine any initPlans to determine the set of external params they
2431 * reference and the set of output params they supply. (We assume
2432 * SS_finalize_plan was run on them already.)
2433 */
2434 initExtParam = initSetParam = NULL;
2435 foreach(l, plan->initPlan)
2436 {
2437 SubPlan *initsubplan = (SubPlan *) lfirst(l);
2438 Plan *initplan = planner_subplan_get_plan(root, initsubplan);
2439 ListCell *l2;
2440
2441 initExtParam = bms_add_members(initExtParam, initplan->extParam);
2442 foreach(l2, initsubplan->setParam)
2443 {
2444 initSetParam = bms_add_member(initSetParam, lfirst_int(l2));
2445 }
2446 }
2447
2448 /* Any setParams are validly referenceable in this node and children */
2449 if (initSetParam)
2450 valid_params = bms_union(valid_params, initSetParam);
2451
2452 /*
2453 * When we call finalize_primnode, context.paramids sets are automatically
2454 * merged together. But when recursing to self, we have to do it the hard
2455 * way. We want the paramids set to include params in subplans as well as
2456 * at this level.
2457 */
2458
2459 /* Find params in targetlist and qual */
2460 finalize_primnode((Node *) plan->targetlist, &context);
2461 finalize_primnode((Node *) plan->qual, &context);
2462
2463 /*
2464 * If it's a parallel-aware scan node, mark it as dependent on the parent
2465 * Gather/GatherMerge's rescan Param.
2466 */
2467 if (plan->parallel_aware)
2468 {
2469 if (gather_param < 0)
2470 elog(ERROR, "parallel-aware plan node is not below a Gather");
2471 context.paramids =
2472 bms_add_member(context.paramids, gather_param);
2473 }
2474
2475 /* Check additional node-type-specific fields */
2476 switch (nodeTag(plan))
2477 {
2478 case T_Result:
2479 finalize_primnode(((Result *) plan)->resconstantqual,
2480 &context);
2481 break;
2482
2483 case T_SeqScan:
2484 context.paramids = bms_add_members(context.paramids, scan_params);
2485 break;
2486
2487 case T_SampleScan:
2488 finalize_primnode((Node *) ((SampleScan *) plan)->tablesample,
2489 &context);
2490 context.paramids = bms_add_members(context.paramids, scan_params);
2491 break;
2492
2493 case T_IndexScan:
2494 finalize_primnode((Node *) ((IndexScan *) plan)->indexqual,
2495 &context);
2496 finalize_primnode((Node *) ((IndexScan *) plan)->indexorderby,
2497 &context);
2498
2499 /*
2500 * we need not look at indexqualorig, since it will have the same
2501 * param references as indexqual. Likewise, we can ignore
2502 * indexorderbyorig.
2503 */
2504 context.paramids = bms_add_members(context.paramids, scan_params);
2505 break;
2506
2507 case T_IndexOnlyScan:
2508 finalize_primnode((Node *) ((IndexOnlyScan *) plan)->indexqual,
2509 &context);
2510 finalize_primnode((Node *) ((IndexOnlyScan *) plan)->recheckqual,
2511 &context);
2512 finalize_primnode((Node *) ((IndexOnlyScan *) plan)->indexorderby,
2513 &context);
2514
2515 /*
2516 * we need not look at indextlist, since it cannot contain Params.
2517 */
2518 context.paramids = bms_add_members(context.paramids, scan_params);
2519 break;
2520
2521 case T_BitmapIndexScan:
2522 finalize_primnode((Node *) ((BitmapIndexScan *) plan)->indexqual,
2523 &context);
2524
2525 /*
2526 * we need not look at indexqualorig, since it will have the same
2527 * param references as indexqual.
2528 */
2529 break;
2530
2531 case T_BitmapHeapScan:
2532 finalize_primnode((Node *) ((BitmapHeapScan *) plan)->bitmapqualorig,
2533 &context);
2534 context.paramids = bms_add_members(context.paramids, scan_params);
2535 break;
2536
2537 case T_TidScan:
2538 finalize_primnode((Node *) ((TidScan *) plan)->tidquals,
2539 &context);
2540 context.paramids = bms_add_members(context.paramids, scan_params);
2541 break;
2542
2543 case T_TidRangeScan:
2544 finalize_primnode((Node *) ((TidRangeScan *) plan)->tidrangequals,
2545 &context);
2546 context.paramids = bms_add_members(context.paramids, scan_params);
2547 break;
2548
2549 case T_SubqueryScan:
2550 {
2551 SubqueryScan *sscan = (SubqueryScan *) plan;
2552 RelOptInfo *rel;
2553 Bitmapset *subquery_params;
2554
2555 /* We must run finalize_plan on the subquery */
2556 rel = find_base_rel(root, sscan->scan.scanrelid);
2557 subquery_params = rel->subroot->outer_params;
2558 if (gather_param >= 0)
2559 subquery_params = bms_add_member(bms_copy(subquery_params),
2560 gather_param);
2561 finalize_plan(rel->subroot, sscan->subplan, gather_param,
2562 subquery_params, NULL);
2563
2564 /* Now we can add its extParams to the parent's params */
2565 context.paramids = bms_add_members(context.paramids,
2566 sscan->subplan->extParam);
2567 /* We need scan_params too, though */
2568 context.paramids = bms_add_members(context.paramids,
2569 scan_params);
2570 }
2571 break;
2572
2573 case T_FunctionScan:
2574 {
2575 FunctionScan *fscan = (FunctionScan *) plan;
2576 ListCell *lc;
2577
2578 /*
2579 * Call finalize_primnode independently on each function
2580 * expression, so that we can record which params are
2581 * referenced in each, in order to decide which need
2582 * re-evaluating during rescan.
2583 */
2584 foreach(lc, fscan->functions)
2585 {
2586 RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2587 finalize_primnode_context funccontext;
2588
2589 funccontext = context;
2590 funccontext.paramids = NULL;
2591
2592 finalize_primnode(rtfunc->funcexpr, &funccontext);
2593
2594 /* remember results for execution */
2595 rtfunc->funcparams = funccontext.paramids;
2596
2597 /* add the function's params to the overall set */
2598 context.paramids = bms_add_members(context.paramids,
2599 funccontext.paramids);
2600 }
2601
2602 context.paramids = bms_add_members(context.paramids,
2603 scan_params);
2604 }
2605 break;
2606
2607 case T_TableFuncScan:
2608 finalize_primnode((Node *) ((TableFuncScan *) plan)->tablefunc,
2609 &context);
2610 context.paramids = bms_add_members(context.paramids, scan_params);
2611 break;
2612
2613 case T_ValuesScan:
2614 finalize_primnode((Node *) ((ValuesScan *) plan)->values_lists,
2615 &context);
2616 context.paramids = bms_add_members(context.paramids, scan_params);
2617 break;
2618
2619 case T_CteScan:
2620 {
2621 /*
2622 * You might think we should add the node's cteParam to
2623 * paramids, but we shouldn't because that param is just a
2624 * linkage mechanism for multiple CteScan nodes for the same
2625 * CTE; it is never used for changed-param signaling. What we
2626 * have to do instead is to find the referenced CTE plan and
2627 * incorporate its external paramids, so that the correct
2628 * things will happen if the CTE references outer-level
2629 * variables. See test cases for bug #4902. (We assume
2630 * SS_finalize_plan was run on the CTE plan already.)
2631 */
2632 int plan_id = ((CteScan *) plan)->ctePlanId;
2633 Plan *cteplan;
2634
2635 /* so, do this ... */
2636 if (plan_id < 1 || plan_id > list_length(root->glob->subplans))
2637 elog(ERROR, "could not find plan for CteScan referencing plan ID %d",
2638 plan_id);
2639 cteplan = (Plan *) list_nth(root->glob->subplans, plan_id - 1);
2640 context.paramids =
2641 bms_add_members(context.paramids, cteplan->extParam);
2642
2643 #ifdef NOT_USED
2644 /* ... but not this */
2645 context.paramids =
2646 bms_add_member(context.paramids,
2647 ((CteScan *) plan)->cteParam);
2648 #endif
2649
2650 context.paramids = bms_add_members(context.paramids,
2651 scan_params);
2652 }
2653 break;
2654
2655 case T_WorkTableScan:
2656 context.paramids =
2657 bms_add_member(context.paramids,
2658 ((WorkTableScan *) plan)->wtParam);
2659 context.paramids = bms_add_members(context.paramids, scan_params);
2660 break;
2661
2662 case T_NamedTuplestoreScan:
2663 context.paramids = bms_add_members(context.paramids, scan_params);
2664 break;
2665
2666 case T_ForeignScan:
2667 {
2668 ForeignScan *fscan = (ForeignScan *) plan;
2669
2670 finalize_primnode((Node *) fscan->fdw_exprs,
2671 &context);
2672 finalize_primnode((Node *) fscan->fdw_recheck_quals,
2673 &context);
2674
2675 /* We assume fdw_scan_tlist cannot contain Params */
2676 context.paramids = bms_add_members(context.paramids,
2677 scan_params);
2678 }
2679 break;
2680
2681 case T_CustomScan:
2682 {
2683 CustomScan *cscan = (CustomScan *) plan;
2684 ListCell *lc;
2685
2686 finalize_primnode((Node *) cscan->custom_exprs,
2687 &context);
2688 /* We assume custom_scan_tlist cannot contain Params */
2689 context.paramids =
2690 bms_add_members(context.paramids, scan_params);
2691
2692 /* child nodes if any */
2693 foreach(lc, cscan->custom_plans)
2694 {
2695 context.paramids =
2696 bms_add_members(context.paramids,
2697 finalize_plan(root,
2698 (Plan *) lfirst(lc),
2699 gather_param,
2700 valid_params,
2701 scan_params));
2702 }
2703 }
2704 break;
2705
2706 case T_ModifyTable:
2707 {
2708 ModifyTable *mtplan = (ModifyTable *) plan;
2709
2710 /* Force descendant scan nodes to reference epqParam */
2711 locally_added_param = mtplan->epqParam;
2712 valid_params = bms_add_member(bms_copy(valid_params),
2713 locally_added_param);
2714 scan_params = bms_add_member(bms_copy(scan_params),
2715 locally_added_param);
2716 finalize_primnode((Node *) mtplan->returningLists,
2717 &context);
2718 finalize_primnode((Node *) mtplan->onConflictSet,
2719 &context);
2720 finalize_primnode((Node *) mtplan->onConflictWhere,
2721 &context);
2722 /* exclRelTlist contains only Vars, doesn't need examination */
2723 }
2724 break;
2725
2726 case T_Append:
2727 {
2728 foreach(l, ((Append *) plan)->appendplans)
2729 {
2730 context.paramids =
2731 bms_add_members(context.paramids,
2732 finalize_plan(root,
2733 (Plan *) lfirst(l),
2734 gather_param,
2735 valid_params,
2736 scan_params));
2737 }
2738 }
2739 break;
2740
2741 case T_MergeAppend:
2742 {
2743 foreach(l, ((MergeAppend *) plan)->mergeplans)
2744 {
2745 context.paramids =
2746 bms_add_members(context.paramids,
2747 finalize_plan(root,
2748 (Plan *) lfirst(l),
2749 gather_param,
2750 valid_params,
2751 scan_params));
2752 }
2753 }
2754 break;
2755
2756 case T_BitmapAnd:
2757 {
2758 foreach(l, ((BitmapAnd *) plan)->bitmapplans)
2759 {
2760 context.paramids =
2761 bms_add_members(context.paramids,
2762 finalize_plan(root,
2763 (Plan *) lfirst(l),
2764 gather_param,
2765 valid_params,
2766 scan_params));
2767 }
2768 }
2769 break;
2770
2771 case T_BitmapOr:
2772 {
2773 foreach(l, ((BitmapOr *) plan)->bitmapplans)
2774 {
2775 context.paramids =
2776 bms_add_members(context.paramids,
2777 finalize_plan(root,
2778 (Plan *) lfirst(l),
2779 gather_param,
2780 valid_params,
2781 scan_params));
2782 }
2783 }
2784 break;
2785
2786 case T_NestLoop:
2787 {
2788 finalize_primnode((Node *) ((Join *) plan)->joinqual,
2789 &context);
2790 /* collect set of params that will be passed to right child */
2791 foreach(l, ((NestLoop *) plan)->nestParams)
2792 {
2793 NestLoopParam *nlp = (NestLoopParam *) lfirst(l);
2794
2795 nestloop_params = bms_add_member(nestloop_params,
2796 nlp->paramno);
2797 }
2798 }
2799 break;
2800
2801 case T_MergeJoin:
2802 finalize_primnode((Node *) ((Join *) plan)->joinqual,
2803 &context);
2804 finalize_primnode((Node *) ((MergeJoin *) plan)->mergeclauses,
2805 &context);
2806 break;
2807
2808 case T_HashJoin:
2809 finalize_primnode((Node *) ((Join *) plan)->joinqual,
2810 &context);
2811 finalize_primnode((Node *) ((HashJoin *) plan)->hashclauses,
2812 &context);
2813 break;
2814
2815 case T_Hash:
2816 finalize_primnode((Node *) ((Hash *) plan)->hashkeys,
2817 &context);
2818 break;
2819
2820 case T_Limit:
2821 finalize_primnode(((Limit *) plan)->limitOffset,
2822 &context);
2823 finalize_primnode(((Limit *) plan)->limitCount,
2824 &context);
2825 break;
2826
2827 case T_RecursiveUnion:
2828 /* child nodes are allowed to reference wtParam */
2829 locally_added_param = ((RecursiveUnion *) plan)->wtParam;
2830 valid_params = bms_add_member(bms_copy(valid_params),
2831 locally_added_param);
2832 /* wtParam does *not* get added to scan_params */
2833 break;
2834
2835 case T_LockRows:
2836 /* Force descendant scan nodes to reference epqParam */
2837 locally_added_param = ((LockRows *) plan)->epqParam;
2838 valid_params = bms_add_member(bms_copy(valid_params),
2839 locally_added_param);
2840 scan_params = bms_add_member(bms_copy(scan_params),
2841 locally_added_param);
2842 break;
2843
2844 case T_Agg:
2845 {
2846 Agg *agg = (Agg *) plan;
2847
2848 /*
2849 * AGG_HASHED plans need to know which Params are referenced
2850 * in aggregate calls. Do a separate scan to identify them.
2851 */
2852 if (agg->aggstrategy == AGG_HASHED)
2853 {
2854 finalize_primnode_context aggcontext;
2855
2856 aggcontext.root = root;
2857 aggcontext.paramids = NULL;
2858 finalize_agg_primnode((Node *) agg->plan.targetlist,
2859 &aggcontext);
2860 finalize_agg_primnode((Node *) agg->plan.qual,
2861 &aggcontext);
2862 agg->aggParams = aggcontext.paramids;
2863 }
2864 }
2865 break;
2866
2867 case T_WindowAgg:
2868 finalize_primnode(((WindowAgg *) plan)->startOffset,
2869 &context);
2870 finalize_primnode(((WindowAgg *) plan)->endOffset,
2871 &context);
2872 break;
2873
2874 case T_Gather:
2875 /* child nodes are allowed to reference rescan_param, if any */
2876 locally_added_param = ((Gather *) plan)->rescan_param;
2877 if (locally_added_param >= 0)
2878 {
2879 valid_params = bms_add_member(bms_copy(valid_params),
2880 locally_added_param);
2881
2882 /*
2883 * We currently don't support nested Gathers. The issue so
2884 * far as this function is concerned would be how to identify
2885 * which child nodes depend on which Gather.
2886 */
2887 Assert(gather_param < 0);
2888 /* Pass down rescan_param to child parallel-aware nodes */
2889 gather_param = locally_added_param;
2890 }
2891 /* rescan_param does *not* get added to scan_params */
2892 break;
2893
2894 case T_GatherMerge:
2895 /* child nodes are allowed to reference rescan_param, if any */
2896 locally_added_param = ((GatherMerge *) plan)->rescan_param;
2897 if (locally_added_param >= 0)
2898 {
2899 valid_params = bms_add_member(bms_copy(valid_params),
2900 locally_added_param);
2901
2902 /*
2903 * We currently don't support nested Gathers. The issue so
2904 * far as this function is concerned would be how to identify
2905 * which child nodes depend on which Gather.
2906 */
2907 Assert(gather_param < 0);
2908 /* Pass down rescan_param to child parallel-aware nodes */
2909 gather_param = locally_added_param;
2910 }
2911 /* rescan_param does *not* get added to scan_params */
2912 break;
2913
2914 case T_Memoize:
2915 finalize_primnode((Node *) ((Memoize *) plan)->param_exprs,
2916 &context);
2917 break;
2918
2919 case T_ProjectSet:
2920 case T_Material:
2921 case T_Sort:
2922 case T_IncrementalSort:
2923 case T_Unique:
2924 case T_SetOp:
2925 case T_Group:
2926 /* no node-type-specific fields need fixing */
2927 break;
2928
2929 default:
2930 elog(ERROR, "unrecognized node type: %d",
2931 (int) nodeTag(plan));
2932 }
2933
2934 /* Process left and right child plans, if any */
2935 child_params = finalize_plan(root,
2936 plan->lefttree,
2937 gather_param,
2938 valid_params,
2939 scan_params);
2940 context.paramids = bms_add_members(context.paramids, child_params);
2941
2942 if (nestloop_params)
2943 {
2944 /* right child can reference nestloop_params as well as valid_params */
2945 child_params = finalize_plan(root,
2946 plan->righttree,
2947 gather_param,
2948 bms_union(nestloop_params, valid_params),
2949 scan_params);
2950 /* ... and they don't count as parameters used at my level */
2951 child_params = bms_difference(child_params, nestloop_params);
2952 bms_free(nestloop_params);
2953 }
2954 else
2955 {
2956 /* easy case */
2957 child_params = finalize_plan(root,
2958 plan->righttree,
2959 gather_param,
2960 valid_params,
2961 scan_params);
2962 }
2963 context.paramids = bms_add_members(context.paramids, child_params);
2964
2965 /*
2966 * Any locally generated parameter doesn't count towards its generating
2967 * plan node's external dependencies. (Note: if we changed valid_params
2968 * and/or scan_params, we leak those bitmapsets; not worth the notational
2969 * trouble to clean them up.)
2970 */
2971 if (locally_added_param >= 0)
2972 {
2973 context.paramids = bms_del_member(context.paramids,
2974 locally_added_param);
2975 }
2976
2977 /* Now we have all the paramids referenced in this node and children */
2978
2979 if (!bms_is_subset(context.paramids, valid_params))
2980 elog(ERROR, "plan should not reference subplan's variable");
2981
2982 /*
2983 * The plan node's allParam and extParam fields should include all its
2984 * referenced paramids, plus contributions from any child initPlans.
2985 * However, any setParams of the initPlans should not be present in the
2986 * parent node's extParams, only in its allParams. (It's possible that
2987 * some initPlans have extParams that are setParams of other initPlans.)
2988 */
2989
2990 /* allParam must include initplans' extParams and setParams */
2991 plan->allParam = bms_union(context.paramids, initExtParam);
2992 plan->allParam = bms_add_members(plan->allParam, initSetParam);
2993 /* extParam must include any initplan extParams */
2994 plan->extParam = bms_union(context.paramids, initExtParam);
2995 /* but not any initplan setParams */
2996 plan->extParam = bms_del_members(plan->extParam, initSetParam);
2997
2998 return plan->allParam;
2999 }
3000
3001 /*
3002 * finalize_primnode: add IDs of all PARAM_EXEC params that appear (or will
3003 * appear) in the given expression tree to the result set.
3004 */
3005 static bool
3006 finalize_primnode(Node *node, finalize_primnode_context *context)
3007 {
3008 if (node == NULL)
3009 return false;
3010 if (IsA(node, Param))
3011 {
3012 if (((Param *) node)->paramkind == PARAM_EXEC)
3013 {
3014 int paramid = ((Param *) node)->paramid;
3015
3016 context->paramids = bms_add_member(context->paramids, paramid);
3017 }
3018 return false; /* no more to do here */
3019 }
3020 else if (IsA(node, Aggref))
3021 {
3022 /*
3023 * Check to see if the aggregate will be replaced by a Param
3024 * referencing a subquery output during setrefs.c. If so, we must
3025 * account for that Param here. (For various reasons, it's not
3026 * convenient to perform that substitution earlier than setrefs.c, nor
3027 * to perform this processing after setrefs.c. Thus we need a wart
3028 * here.)
3029 */
3030 Aggref *aggref = (Aggref *) node;
3031 Param *aggparam;
3032
3033 aggparam = find_minmax_agg_replacement_param(context->root, aggref);
3034 if (aggparam != NULL)
3035 context->paramids = bms_add_member(context->paramids,
3036 aggparam->paramid);
3037 /* Fall through to examine the agg's arguments */
3038 }
3039 else if (IsA(node, SubPlan))
3040 {
3041 SubPlan *subplan = (SubPlan *) node;
3042 Plan *plan = planner_subplan_get_plan(context->root, subplan);
3043 ListCell *lc;
3044 Bitmapset *subparamids;
3045
3046 /* Recurse into the testexpr, but not into the Plan */
3047 finalize_primnode(subplan->testexpr, context);
3048
3049 /*
3050 * Remove any param IDs of output parameters of the subplan that were
3051 * referenced in the testexpr. These are not interesting for
3052 * parameter change signaling since we always re-evaluate the subplan.
3053 * Note that this wouldn't work too well if there might be uses of the
3054 * same param IDs elsewhere in the plan, but that can't happen because
3055 * generate_new_exec_param never tries to merge params.
3056 */
3057 foreach(lc, subplan->paramIds)
3058 {
3059 context->paramids = bms_del_member(context->paramids,
3060 lfirst_int(lc));
3061 }
3062
3063 /* Also examine args list */
3064 finalize_primnode((Node *) subplan->args, context);
3065
3066 /*
3067 * Add params needed by the subplan to paramids, but excluding those
3068 * we will pass down to it. (We assume SS_finalize_plan was run on
3069 * the subplan already.)
3070 */
3071 subparamids = bms_copy(plan->extParam);
3072 foreach(lc, subplan->parParam)
3073 {
3074 subparamids = bms_del_member(subparamids, lfirst_int(lc));
3075 }
3076 context->paramids = bms_join(context->paramids, subparamids);
3077
3078 return false; /* no more to do here */
3079 }
3080 return expression_tree_walker(node, finalize_primnode, context);
3081 }
3082
3083 /*
3084 * finalize_agg_primnode: find all Aggref nodes in the given expression tree,
3085 * and add IDs of all PARAM_EXEC params appearing within their aggregated
3086 * arguments to the result set.
3087 */
3088 static bool
3089 finalize_agg_primnode(Node *node, finalize_primnode_context *context)
3090 {
3091 if (node == NULL)
3092 return false;
3093 if (IsA(node, Aggref))
3094 {
3095 Aggref *agg = (Aggref *) node;
3096
3097 /* we should not consider the direct arguments, if any */
3098 finalize_primnode((Node *) agg->args, context);
3099 finalize_primnode((Node *) agg->aggfilter, context);
3100 return false; /* there can't be any Aggrefs below here */
3101 }
3102 return expression_tree_walker(node, finalize_agg_primnode, context);
3103 }
3104
3105 /*
3106 * SS_make_initplan_output_param - make a Param for an initPlan's output
3107 *
3108 * The plan is expected to return a scalar value of the given type/collation.
3109 *
3110 * Note that in some cases the initplan may not ever appear in the finished
3111 * plan tree. If that happens, we'll have wasted a PARAM_EXEC slot, which
3112 * is no big deal.
3113 */
3114 Param *
3115 SS_make_initplan_output_param(PlannerInfo *root,
3116 Oid resulttype, int32 resulttypmod,
3117 Oid resultcollation)
3118 {
3119 return generate_new_exec_param(root, resulttype,
3120 resulttypmod, resultcollation);
3121 }
3122
3123 /*
3124 * SS_make_initplan_from_plan - given a plan tree, make it an InitPlan
3125 *
3126 * We build an EXPR_SUBLINK SubPlan node and put it into the initplan
3127 * list for the outer query level. A Param that represents the initplan's
3128 * output has already been assigned using SS_make_initplan_output_param.
3129 */
3130 void
3131 SS_make_initplan_from_plan(PlannerInfo *root,
3132 PlannerInfo *subroot, Plan *plan,
3133 Param *prm)
3134 {
3135 SubPlan *node;
3136
3137 /*
3138 * Add the subplan and its PlannerInfo, as well as a dummy path entry, to
3139 * the global lists. Ideally we'd save a real path, but right now our
3140 * sole caller doesn't build a path that exactly matches the plan. Since
3141 * we're not currently going to need the path for an initplan, it's not
3142 * worth requiring construction of such a path.
3143 */
3144 root->glob->subplans = lappend(root->glob->subplans, plan);
3145 root->glob->subpaths = lappend(root->glob->subpaths, NULL);
3146 root->glob->subroots = lappend(root->glob->subroots, subroot);
3147
3148 /*
3149 * Create a SubPlan node and add it to the outer list of InitPlans. Note
3150 * it has to appear after any other InitPlans it might depend on (see
3151 * comments in ExecReScan).
3152 */
3153 node = makeNode(SubPlan);
3154 node->subLinkType = EXPR_SUBLINK;
3155 node->plan_id = list_length(root->glob->subplans);
3156 node->plan_name = psprintf("InitPlan %d", node->plan_id);
3157 get_first_col_type(plan, &node->firstColType, &node->firstColTypmod,
3158 &node->firstColCollation);
3159 node->parallel_safe = plan->parallel_safe;
3160 node->setParam = list_make1_int(prm->paramid);
3161
3162 root->init_plans = lappend(root->init_plans, node);
3163
3164 /*
3165 * The node can't have any inputs (since it's an initplan), so the
3166 * parParam and args lists remain empty.
3167 */
3168
3169 /* Set costs of SubPlan using info from the plan tree */
3170 cost_subplan(subroot, node, plan);
3171 }