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