PostgreSQL Source Code  git master
prepjointree.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * prepjointree.c
4  * Planner preprocessing for subqueries and join tree manipulation.
5  *
6  * NOTE: the intended sequence for invoking these operations is
7  * replace_empty_jointree
8  * pull_up_sublinks
9  * preprocess_function_rtes
10  * pull_up_subqueries
11  * flatten_simple_union_all
12  * do expression preprocessing (including flattening JOIN alias vars)
13  * reduce_outer_joins
14  * remove_useless_result_rtes
15  *
16  *
17  * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
18  * Portions Copyright (c) 1994, Regents of the University of California
19  *
20  *
21  * IDENTIFICATION
22  * src/backend/optimizer/prep/prepjointree.c
23  *
24  *-------------------------------------------------------------------------
25  */
26 #include "postgres.h"
27 
28 #include "catalog/pg_type.h"
29 #include "funcapi.h"
30 #include "nodes/makefuncs.h"
31 #include "nodes/multibitmapset.h"
32 #include "nodes/nodeFuncs.h"
33 #include "optimizer/clauses.h"
34 #include "optimizer/optimizer.h"
35 #include "optimizer/placeholder.h"
36 #include "optimizer/prep.h"
37 #include "optimizer/subselect.h"
38 #include "optimizer/tlist.h"
39 #include "parser/parse_relation.h"
40 #include "parser/parsetree.h"
41 #include "rewrite/rewriteManip.h"
42 
43 
45 {
47  List *targetlist; /* tlist of subquery being pulled up */
48  RangeTblEntry *target_rte; /* RTE of subquery */
49  Relids relids; /* relids within subquery, as numbered after
50  * pullup (set only if target_rte->lateral) */
51  bool *outer_hasSubLinks; /* -> outer query's hasSubLinks */
52  int varno; /* varno of subquery */
53  bool need_phvs; /* do we need PlaceHolderVars? */
54  bool wrap_non_vars; /* do we need 'em on *all* non-Vars? */
55  Node **rv_cache; /* cache for results with PHVs */
57 
59 {
60  Relids relids; /* base relids within this subtree */
61  bool contains_outer; /* does subtree contain outer join(s)? */
62  List *sub_states; /* List of states for subtree components */
64 
66  Relids *relids);
68  Node **jtlink1, Relids available_rels1,
69  Node **jtlink2, Relids available_rels2);
70 static Node *pull_up_subqueries_recurse(PlannerInfo *root, Node *jtnode,
71  JoinExpr *lowest_outer_join,
72  JoinExpr *lowest_nulling_outer_join,
73  AppendRelInfo *containing_appendrel);
74 static Node *pull_up_simple_subquery(PlannerInfo *root, Node *jtnode,
75  RangeTblEntry *rte,
76  JoinExpr *lowest_outer_join,
77  JoinExpr *lowest_nulling_outer_join,
78  AppendRelInfo *containing_appendrel);
79 static Node *pull_up_simple_union_all(PlannerInfo *root, Node *jtnode,
80  RangeTblEntry *rte);
81 static void pull_up_union_leaf_queries(Node *setOp, PlannerInfo *root,
82  int parentRTindex, Query *setOpQuery,
83  int childRToffset);
84 static void make_setop_translation_list(Query *query, int newvarno,
85  AppendRelInfo *appinfo);
86 static bool is_simple_subquery(PlannerInfo *root, Query *subquery,
87  RangeTblEntry *rte,
88  JoinExpr *lowest_outer_join);
89 static Node *pull_up_simple_values(PlannerInfo *root, Node *jtnode,
90  RangeTblEntry *rte);
91 static bool is_simple_values(PlannerInfo *root, RangeTblEntry *rte);
92 static Node *pull_up_constant_function(PlannerInfo *root, Node *jtnode,
93  RangeTblEntry *rte,
94  JoinExpr *lowest_nulling_outer_join,
95  AppendRelInfo *containing_appendrel);
96 static bool is_simple_union_all(Query *subquery);
97 static bool is_simple_union_all_recurse(Node *setOp, Query *setOpQuery,
98  List *colTypes);
99 static bool is_safe_append_member(Query *subquery);
101  Node *jtnode, bool restricted,
102  Relids safe_upper_varnos);
103 static void perform_pullup_replace_vars(PlannerInfo *root,
104  pullup_replace_vars_context *rvcontext,
105  JoinExpr *lowest_nulling_outer_join,
106  AppendRelInfo *containing_appendrel);
107 static void replace_vars_in_jointree(Node *jtnode,
109  JoinExpr *lowest_nulling_outer_join);
110 static Node *pullup_replace_vars(Node *expr,
111  pullup_replace_vars_context *context);
115  pullup_replace_vars_context *context);
117 static void reduce_outer_joins_pass2(Node *jtnode,
119  PlannerInfo *root,
120  Relids nonnullable_rels,
121  List *forced_null_vars);
122 static Node *remove_useless_results_recurse(PlannerInfo *root, Node *jtnode);
123 static int get_result_relid(PlannerInfo *root, Node *jtnode);
124 static void remove_result_refs(PlannerInfo *root, int varno, Node *newjtloc);
125 static bool find_dependent_phvs(PlannerInfo *root, int varno);
127  Node *node, int varno);
128 static void substitute_phv_relids(Node *node,
129  int varno, Relids subrelids);
130 static void fix_append_rel_relids(List *append_rel_list, int varno,
131  Relids subrelids);
132 static Node *find_jointree_node_for_rel(Node *jtnode, int relid);
133 
134 
135 /*
136  * transform_MERGE_to_join
137  * Replace a MERGE's jointree to also include the target relation.
138  */
139 void
141 {
142  RangeTblEntry *joinrte;
143  JoinExpr *joinexpr;
144  JoinType jointype;
145  int joinrti;
146  List *vars;
147 
148  if (parse->commandType != CMD_MERGE)
149  return;
150 
151  /* XXX probably bogus */
152  vars = NIL;
153 
154  /*
155  * When any WHEN NOT MATCHED THEN INSERT clauses exist, we need to use an
156  * outer join so that we process all unmatched tuples from the source
157  * relation. If none exist, we can use an inner join.
158  */
159  if (parse->mergeUseOuterJoin)
160  jointype = JOIN_RIGHT;
161  else
162  jointype = JOIN_INNER;
163 
164  /* Manufacture a join RTE to use. */
165  joinrte = makeNode(RangeTblEntry);
166  joinrte->rtekind = RTE_JOIN;
167  joinrte->jointype = jointype;
168  joinrte->joinmergedcols = 0;
169  joinrte->joinaliasvars = vars;
170  joinrte->joinleftcols = NIL; /* MERGE does not allow JOIN USING */
171  joinrte->joinrightcols = NIL; /* ditto */
172  joinrte->join_using_alias = NULL;
173 
174  joinrte->alias = NULL;
175  joinrte->eref = makeAlias("*MERGE*", NIL);
176  joinrte->lateral = false;
177  joinrte->inh = false;
178  joinrte->inFromCl = true;
179  joinrte->requiredPerms = 0;
180  joinrte->checkAsUser = InvalidOid;
181  joinrte->selectedCols = NULL;
182  joinrte->insertedCols = NULL;
183  joinrte->updatedCols = NULL;
184  joinrte->extraUpdatedCols = NULL;
185  joinrte->securityQuals = NIL;
186 
187  /*
188  * Add completed RTE to pstate's range table list, so that we know its
189  * index.
190  */
191  parse->rtable = lappend(parse->rtable, joinrte);
192  joinrti = list_length(parse->rtable);
193 
194  /*
195  * Create a JOIN between the target and the source relation.
196  */
197  joinexpr = makeNode(JoinExpr);
198  joinexpr->jointype = jointype;
199  joinexpr->isNatural = false;
200  joinexpr->larg = (Node *) makeNode(RangeTblRef);
201  ((RangeTblRef *) joinexpr->larg)->rtindex = parse->resultRelation;
202  joinexpr->rarg = linitial(parse->jointree->fromlist); /* original join */
203  joinexpr->usingClause = NIL;
204  joinexpr->join_using_alias = NULL;
205  /* The quals are removed from the jointree and into this specific join */
206  joinexpr->quals = parse->jointree->quals;
207  joinexpr->alias = NULL;
208  joinexpr->rtindex = joinrti;
209 
210  /* Make the new join be the sole entry in the query's jointree */
211  parse->jointree->fromlist = list_make1(joinexpr);
212  parse->jointree->quals = NULL;
213 }
214 
215 /*
216  * replace_empty_jointree
217  * If the Query's jointree is empty, replace it with a dummy RTE_RESULT
218  * relation.
219  *
220  * By doing this, we can avoid a bunch of corner cases that formerly existed
221  * for SELECTs with omitted FROM clauses. An example is that a subquery
222  * with empty jointree previously could not be pulled up, because that would
223  * have resulted in an empty relid set, making the subquery not uniquely
224  * identifiable for join or PlaceHolderVar processing.
225  *
226  * Unlike most other functions in this file, this function doesn't recurse;
227  * we rely on other processing to invoke it on sub-queries at suitable times.
228  */
229 void
231 {
232  RangeTblEntry *rte;
233  Index rti;
234  RangeTblRef *rtr;
235 
236  /* Nothing to do if jointree is already nonempty */
237  if (parse->jointree->fromlist != NIL)
238  return;
239 
240  /* We mustn't change it in the top level of a setop tree, either */
241  if (parse->setOperations)
242  return;
243 
244  /* Create suitable RTE */
245  rte = makeNode(RangeTblEntry);
246  rte->rtekind = RTE_RESULT;
247  rte->eref = makeAlias("*RESULT*", NIL);
248 
249  /* Add it to rangetable */
250  parse->rtable = lappend(parse->rtable, rte);
251  rti = list_length(parse->rtable);
252 
253  /* And jam a reference into the jointree */
254  rtr = makeNode(RangeTblRef);
255  rtr->rtindex = rti;
256  parse->jointree->fromlist = list_make1(rtr);
257 }
258 
259 /*
260  * pull_up_sublinks
261  * Attempt to pull up ANY and EXISTS SubLinks to be treated as
262  * semijoins or anti-semijoins.
263  *
264  * A clause "foo op ANY (sub-SELECT)" can be processed by pulling the
265  * sub-SELECT up to become a rangetable entry and treating the implied
266  * comparisons as quals of a semijoin. However, this optimization *only*
267  * works at the top level of WHERE or a JOIN/ON clause, because we cannot
268  * distinguish whether the ANY ought to return FALSE or NULL in cases
269  * involving NULL inputs. Also, in an outer join's ON clause we can only
270  * do this if the sublink is degenerate (ie, references only the nullable
271  * side of the join). In that case it is legal to push the semijoin
272  * down into the nullable side of the join. If the sublink references any
273  * nonnullable-side variables then it would have to be evaluated as part
274  * of the outer join, which makes things way too complicated.
275  *
276  * Under similar conditions, EXISTS and NOT EXISTS clauses can be handled
277  * by pulling up the sub-SELECT and creating a semijoin or anti-semijoin.
278  *
279  * This routine searches for such clauses and does the necessary parsetree
280  * transformations if any are found.
281  *
282  * This routine has to run before preprocess_expression(), so the quals
283  * clauses are not yet reduced to implicit-AND format, and are not guaranteed
284  * to be AND/OR-flat either. That means we need to recursively search through
285  * explicit AND clauses. We stop as soon as we hit a non-AND item.
286  */
287 void
289 {
290  Node *jtnode;
291  Relids relids;
292 
293  /* Begin recursion through the jointree */
294  jtnode = pull_up_sublinks_jointree_recurse(root,
295  (Node *) root->parse->jointree,
296  &relids);
297 
298  /*
299  * root->parse->jointree must always be a FromExpr, so insert a dummy one
300  * if we got a bare RangeTblRef or JoinExpr out of the recursion.
301  */
302  if (IsA(jtnode, FromExpr))
303  root->parse->jointree = (FromExpr *) jtnode;
304  else
305  root->parse->jointree = makeFromExpr(list_make1(jtnode), NULL);
306 }
307 
308 /*
309  * Recurse through jointree nodes for pull_up_sublinks()
310  *
311  * In addition to returning the possibly-modified jointree node, we return
312  * a relids set of the contained rels into *relids.
313  */
314 static Node *
316  Relids *relids)
317 {
318  if (jtnode == NULL)
319  {
320  *relids = NULL;
321  }
322  else if (IsA(jtnode, RangeTblRef))
323  {
324  int varno = ((RangeTblRef *) jtnode)->rtindex;
325 
326  *relids = bms_make_singleton(varno);
327  /* jtnode is returned unmodified */
328  }
329  else if (IsA(jtnode, FromExpr))
330  {
331  FromExpr *f = (FromExpr *) jtnode;
332  List *newfromlist = NIL;
333  Relids frelids = NULL;
334  FromExpr *newf;
335  Node *jtlink;
336  ListCell *l;
337 
338  /* First, recurse to process children and collect their relids */
339  foreach(l, f->fromlist)
340  {
341  Node *newchild;
342  Relids childrelids;
343 
344  newchild = pull_up_sublinks_jointree_recurse(root,
345  lfirst(l),
346  &childrelids);
347  newfromlist = lappend(newfromlist, newchild);
348  frelids = bms_join(frelids, childrelids);
349  }
350  /* Build the replacement FromExpr; no quals yet */
351  newf = makeFromExpr(newfromlist, NULL);
352  /* Set up a link representing the rebuilt jointree */
353  jtlink = (Node *) newf;
354  /* Now process qual --- all children are available for use */
355  newf->quals = pull_up_sublinks_qual_recurse(root, f->quals,
356  &jtlink, frelids,
357  NULL, NULL);
358 
359  /*
360  * Note that the result will be either newf, or a stack of JoinExprs
361  * with newf at the base. We rely on subsequent optimization steps to
362  * flatten this and rearrange the joins as needed.
363  *
364  * Although we could include the pulled-up subqueries in the returned
365  * relids, there's no need since upper quals couldn't refer to their
366  * outputs anyway.
367  */
368  *relids = frelids;
369  jtnode = jtlink;
370  }
371  else if (IsA(jtnode, JoinExpr))
372  {
373  JoinExpr *j;
374  Relids leftrelids;
375  Relids rightrelids;
376  Node *jtlink;
377 
378  /*
379  * Make a modifiable copy of join node, but don't bother copying its
380  * subnodes (yet).
381  */
382  j = (JoinExpr *) palloc(sizeof(JoinExpr));
383  memcpy(j, jtnode, sizeof(JoinExpr));
384  jtlink = (Node *) j;
385 
386  /* Recurse to process children and collect their relids */
387  j->larg = pull_up_sublinks_jointree_recurse(root, j->larg,
388  &leftrelids);
389  j->rarg = pull_up_sublinks_jointree_recurse(root, j->rarg,
390  &rightrelids);
391 
392  /*
393  * Now process qual, showing appropriate child relids as available,
394  * and attach any pulled-up jointree items at the right place. In the
395  * inner-join case we put new JoinExprs above the existing one (much
396  * as for a FromExpr-style join). In outer-join cases the new
397  * JoinExprs must go into the nullable side of the outer join. The
398  * point of the available_rels machinations is to ensure that we only
399  * pull up quals for which that's okay.
400  *
401  * We don't expect to see any pre-existing JOIN_SEMI or JOIN_ANTI
402  * nodes here.
403  */
404  switch (j->jointype)
405  {
406  case JOIN_INNER:
407  j->quals = pull_up_sublinks_qual_recurse(root, j->quals,
408  &jtlink,
409  bms_union(leftrelids,
410  rightrelids),
411  NULL, NULL);
412  break;
413  case JOIN_LEFT:
414  j->quals = pull_up_sublinks_qual_recurse(root, j->quals,
415  &j->rarg,
416  rightrelids,
417  NULL, NULL);
418  break;
419  case JOIN_FULL:
420  /* can't do anything with full-join quals */
421  break;
422  case JOIN_RIGHT:
423  j->quals = pull_up_sublinks_qual_recurse(root, j->quals,
424  &j->larg,
425  leftrelids,
426  NULL, NULL);
427  break;
428  default:
429  elog(ERROR, "unrecognized join type: %d",
430  (int) j->jointype);
431  break;
432  }
433 
434  /*
435  * Although we could include the pulled-up subqueries in the returned
436  * relids, there's no need since upper quals couldn't refer to their
437  * outputs anyway. But we *do* need to include the join's own rtindex
438  * because we haven't yet collapsed join alias variables, so upper
439  * levels would mistakenly think they couldn't use references to this
440  * join.
441  */
442  *relids = bms_join(leftrelids, rightrelids);
443  if (j->rtindex)
444  *relids = bms_add_member(*relids, j->rtindex);
445  jtnode = jtlink;
446  }
447  else
448  elog(ERROR, "unrecognized node type: %d",
449  (int) nodeTag(jtnode));
450  return jtnode;
451 }
452 
453 /*
454  * Recurse through top-level qual nodes for pull_up_sublinks()
455  *
456  * jtlink1 points to the link in the jointree where any new JoinExprs should
457  * be inserted if they reference available_rels1 (i.e., available_rels1
458  * denotes the relations present underneath jtlink1). Optionally, jtlink2 can
459  * point to a second link where new JoinExprs should be inserted if they
460  * reference available_rels2 (pass NULL for both those arguments if not used).
461  * Note that SubLinks referencing both sets of variables cannot be optimized.
462  * If we find multiple pull-up-able SubLinks, they'll get stacked onto jtlink1
463  * and/or jtlink2 in the order we encounter them. We rely on subsequent
464  * optimization to rearrange the stack if appropriate.
465  *
466  * Returns the replacement qual node, or NULL if the qual should be removed.
467  */
468 static Node *
470  Node **jtlink1, Relids available_rels1,
471  Node **jtlink2, Relids available_rels2)
472 {
473  if (node == NULL)
474  return NULL;
475  if (IsA(node, SubLink))
476  {
477  SubLink *sublink = (SubLink *) node;
478  JoinExpr *j;
479  Relids child_rels;
480 
481  /* Is it a convertible ANY or EXISTS clause? */
482  if (sublink->subLinkType == ANY_SUBLINK)
483  {
484  if ((j = convert_ANY_sublink_to_join(root, sublink,
485  available_rels1)) != NULL)
486  {
487  /* Yes; insert the new join node into the join tree */
488  j->larg = *jtlink1;
489  *jtlink1 = (Node *) j;
490  /* Recursively process pulled-up jointree nodes */
492  j->rarg,
493  &child_rels);
494 
495  /*
496  * Now recursively process the pulled-up quals. Any inserted
497  * joins can get stacked onto either j->larg or j->rarg,
498  * depending on which rels they reference.
499  */
500  j->quals = pull_up_sublinks_qual_recurse(root,
501  j->quals,
502  &j->larg,
503  available_rels1,
504  &j->rarg,
505  child_rels);
506  /* Return NULL representing constant TRUE */
507  return NULL;
508  }
509  if (available_rels2 != NULL &&
510  (j = convert_ANY_sublink_to_join(root, sublink,
511  available_rels2)) != NULL)
512  {
513  /* Yes; insert the new join node into the join tree */
514  j->larg = *jtlink2;
515  *jtlink2 = (Node *) j;
516  /* Recursively process pulled-up jointree nodes */
518  j->rarg,
519  &child_rels);
520 
521  /*
522  * Now recursively process the pulled-up quals. Any inserted
523  * joins can get stacked onto either j->larg or j->rarg,
524  * depending on which rels they reference.
525  */
526  j->quals = pull_up_sublinks_qual_recurse(root,
527  j->quals,
528  &j->larg,
529  available_rels2,
530  &j->rarg,
531  child_rels);
532  /* Return NULL representing constant TRUE */
533  return NULL;
534  }
535  }
536  else if (sublink->subLinkType == EXISTS_SUBLINK)
537  {
538  if ((j = convert_EXISTS_sublink_to_join(root, sublink, false,
539  available_rels1)) != NULL)
540  {
541  /* Yes; insert the new join node into the join tree */
542  j->larg = *jtlink1;
543  *jtlink1 = (Node *) j;
544  /* Recursively process pulled-up jointree nodes */
546  j->rarg,
547  &child_rels);
548 
549  /*
550  * Now recursively process the pulled-up quals. Any inserted
551  * joins can get stacked onto either j->larg or j->rarg,
552  * depending on which rels they reference.
553  */
554  j->quals = pull_up_sublinks_qual_recurse(root,
555  j->quals,
556  &j->larg,
557  available_rels1,
558  &j->rarg,
559  child_rels);
560  /* Return NULL representing constant TRUE */
561  return NULL;
562  }
563  if (available_rels2 != NULL &&
564  (j = convert_EXISTS_sublink_to_join(root, sublink, false,
565  available_rels2)) != NULL)
566  {
567  /* Yes; insert the new join node into the join tree */
568  j->larg = *jtlink2;
569  *jtlink2 = (Node *) j;
570  /* Recursively process pulled-up jointree nodes */
572  j->rarg,
573  &child_rels);
574 
575  /*
576  * Now recursively process the pulled-up quals. Any inserted
577  * joins can get stacked onto either j->larg or j->rarg,
578  * depending on which rels they reference.
579  */
580  j->quals = pull_up_sublinks_qual_recurse(root,
581  j->quals,
582  &j->larg,
583  available_rels2,
584  &j->rarg,
585  child_rels);
586  /* Return NULL representing constant TRUE */
587  return NULL;
588  }
589  }
590  /* Else return it unmodified */
591  return node;
592  }
593  if (is_notclause(node))
594  {
595  /* If the immediate argument of NOT is EXISTS, try to convert */
596  SubLink *sublink = (SubLink *) get_notclausearg((Expr *) node);
597  JoinExpr *j;
598  Relids child_rels;
599 
600  if (sublink && IsA(sublink, SubLink))
601  {
602  if (sublink->subLinkType == EXISTS_SUBLINK)
603  {
604  if ((j = convert_EXISTS_sublink_to_join(root, sublink, true,
605  available_rels1)) != NULL)
606  {
607  /* Yes; insert the new join node into the join tree */
608  j->larg = *jtlink1;
609  *jtlink1 = (Node *) j;
610  /* Recursively process pulled-up jointree nodes */
612  j->rarg,
613  &child_rels);
614 
615  /*
616  * Now recursively process the pulled-up quals. Because
617  * we are underneath a NOT, we can't pull up sublinks that
618  * reference the left-hand stuff, but it's still okay to
619  * pull up sublinks referencing j->rarg.
620  */
621  j->quals = pull_up_sublinks_qual_recurse(root,
622  j->quals,
623  &j->rarg,
624  child_rels,
625  NULL, NULL);
626  /* Return NULL representing constant TRUE */
627  return NULL;
628  }
629  if (available_rels2 != NULL &&
630  (j = convert_EXISTS_sublink_to_join(root, sublink, true,
631  available_rels2)) != NULL)
632  {
633  /* Yes; insert the new join node into the join tree */
634  j->larg = *jtlink2;
635  *jtlink2 = (Node *) j;
636  /* Recursively process pulled-up jointree nodes */
638  j->rarg,
639  &child_rels);
640 
641  /*
642  * Now recursively process the pulled-up quals. Because
643  * we are underneath a NOT, we can't pull up sublinks that
644  * reference the left-hand stuff, but it's still okay to
645  * pull up sublinks referencing j->rarg.
646  */
647  j->quals = pull_up_sublinks_qual_recurse(root,
648  j->quals,
649  &j->rarg,
650  child_rels,
651  NULL, NULL);
652  /* Return NULL representing constant TRUE */
653  return NULL;
654  }
655  }
656  }
657  /* Else return it unmodified */
658  return node;
659  }
660  if (is_andclause(node))
661  {
662  /* Recurse into AND clause */
663  List *newclauses = NIL;
664  ListCell *l;
665 
666  foreach(l, ((BoolExpr *) node)->args)
667  {
668  Node *oldclause = (Node *) lfirst(l);
669  Node *newclause;
670 
671  newclause = pull_up_sublinks_qual_recurse(root,
672  oldclause,
673  jtlink1,
674  available_rels1,
675  jtlink2,
676  available_rels2);
677  if (newclause)
678  newclauses = lappend(newclauses, newclause);
679  }
680  /* We might have got back fewer clauses than we started with */
681  if (newclauses == NIL)
682  return NULL;
683  else if (list_length(newclauses) == 1)
684  return (Node *) linitial(newclauses);
685  else
686  return (Node *) make_andclause(newclauses);
687  }
688  /* Stop if not an AND */
689  return node;
690 }
691 
692 /*
693  * preprocess_function_rtes
694  * Constant-simplify any FUNCTION RTEs in the FROM clause, and then
695  * attempt to "inline" any that are set-returning functions.
696  *
697  * If an RTE_FUNCTION rtable entry invokes a set-returning function that
698  * contains just a simple SELECT, we can convert the rtable entry to an
699  * RTE_SUBQUERY entry exposing the SELECT directly. This is especially
700  * useful if the subquery can then be "pulled up" for further optimization,
701  * but we do it even if not, to reduce executor overhead.
702  *
703  * This has to be done before we have started to do any optimization of
704  * subqueries, else any such steps wouldn't get applied to subqueries
705  * obtained via inlining. However, we do it after pull_up_sublinks
706  * so that we can inline any functions used in SubLink subselects.
707  *
708  * The reason for applying const-simplification at this stage is that
709  * (a) we'd need to do it anyway to inline a SRF, and (b) by doing it now,
710  * we can be sure that pull_up_constant_function() will see constants
711  * if there are constants to be seen. This approach also guarantees
712  * that every FUNCTION RTE has been const-simplified, allowing planner.c's
713  * preprocess_expression() to skip doing it again.
714  *
715  * Like most of the planner, this feels free to scribble on its input data
716  * structure.
717  */
718 void
720 {
721  ListCell *rt;
722 
723  foreach(rt, root->parse->rtable)
724  {
725  RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
726 
727  if (rte->rtekind == RTE_FUNCTION)
728  {
729  Query *funcquery;
730 
731  /* Apply const-simplification */
732  rte->functions = (List *)
733  eval_const_expressions(root, (Node *) rte->functions);
734 
735  /* Check safety of expansion, and expand if possible */
736  funcquery = inline_set_returning_function(root, rte);
737  if (funcquery)
738  {
739  /* Successful expansion, convert the RTE to a subquery */
740  rte->rtekind = RTE_SUBQUERY;
741  rte->subquery = funcquery;
742  rte->security_barrier = false;
743  /* Clear fields that should not be set in a subquery RTE */
744  rte->functions = NIL;
745  rte->funcordinality = false;
746  }
747  }
748  }
749 }
750 
751 /*
752  * pull_up_subqueries
753  * Look for subqueries in the rangetable that can be pulled up into
754  * the parent query. If the subquery has no special features like
755  * grouping/aggregation then we can merge it into the parent's jointree.
756  * Also, subqueries that are simple UNION ALL structures can be
757  * converted into "append relations".
758  */
759 void
761 {
762  /* Top level of jointree must always be a FromExpr */
763  Assert(IsA(root->parse->jointree, FromExpr));
764  /* Recursion starts with no containing join nor appendrel */
765  root->parse->jointree = (FromExpr *)
767  NULL, NULL, NULL);
768  /* We should still have a FromExpr */
769  Assert(IsA(root->parse->jointree, FromExpr));
770 }
771 
772 /*
773  * pull_up_subqueries_recurse
774  * Recursive guts of pull_up_subqueries.
775  *
776  * This recursively processes the jointree and returns a modified jointree.
777  *
778  * If this jointree node is within either side of an outer join, then
779  * lowest_outer_join references the lowest such JoinExpr node; otherwise
780  * it is NULL. We use this to constrain the effects of LATERAL subqueries.
781  *
782  * If this jointree node is within the nullable side of an outer join, then
783  * lowest_nulling_outer_join references the lowest such JoinExpr node;
784  * otherwise it is NULL. This forces use of the PlaceHolderVar mechanism for
785  * references to non-nullable targetlist items, but only for references above
786  * that join.
787  *
788  * If we are looking at a member subquery of an append relation,
789  * containing_appendrel describes that relation; else it is NULL.
790  * This forces use of the PlaceHolderVar mechanism for all non-Var targetlist
791  * items, and puts some additional restrictions on what can be pulled up.
792  *
793  * A tricky aspect of this code is that if we pull up a subquery we have
794  * to replace Vars that reference the subquery's outputs throughout the
795  * parent query, including quals attached to jointree nodes above the one
796  * we are currently processing! We handle this by being careful to maintain
797  * validity of the jointree structure while recursing, in the following sense:
798  * whenever we recurse, all qual expressions in the tree must be reachable
799  * from the top level, in case the recursive call needs to modify them.
800  *
801  * Notice also that we can't turn pullup_replace_vars loose on the whole
802  * jointree, because it'd return a mutated copy of the tree; we have to
803  * invoke it just on the quals, instead. This behavior is what makes it
804  * reasonable to pass lowest_outer_join and lowest_nulling_outer_join as
805  * pointers rather than some more-indirect way of identifying the lowest
806  * OJs. Likewise, we don't replace append_rel_list members but only their
807  * substructure, so the containing_appendrel reference is safe to use.
808  */
809 static Node *
811  JoinExpr *lowest_outer_join,
812  JoinExpr *lowest_nulling_outer_join,
813  AppendRelInfo *containing_appendrel)
814 {
815  Assert(jtnode != NULL);
816  if (IsA(jtnode, RangeTblRef))
817  {
818  int varno = ((RangeTblRef *) jtnode)->rtindex;
819  RangeTblEntry *rte = rt_fetch(varno, root->parse->rtable);
820 
821  /*
822  * Is this a subquery RTE, and if so, is the subquery simple enough to
823  * pull up?
824  *
825  * If we are looking at an append-relation member, we can't pull it up
826  * unless is_safe_append_member says so.
827  */
828  if (rte->rtekind == RTE_SUBQUERY &&
829  is_simple_subquery(root, rte->subquery, rte, lowest_outer_join) &&
830  (containing_appendrel == NULL ||
832  return pull_up_simple_subquery(root, jtnode, rte,
833  lowest_outer_join,
834  lowest_nulling_outer_join,
835  containing_appendrel);
836 
837  /*
838  * Alternatively, is it a simple UNION ALL subquery? If so, flatten
839  * into an "append relation".
840  *
841  * It's safe to do this regardless of whether this query is itself an
842  * appendrel member. (If you're thinking we should try to flatten the
843  * two levels of appendrel together, you're right; but we handle that
844  * in set_append_rel_pathlist, not here.)
845  */
846  if (rte->rtekind == RTE_SUBQUERY &&
848  return pull_up_simple_union_all(root, jtnode, rte);
849 
850  /*
851  * Or perhaps it's a simple VALUES RTE?
852  *
853  * We don't allow VALUES pullup below an outer join nor into an
854  * appendrel (such cases are impossible anyway at the moment).
855  */
856  if (rte->rtekind == RTE_VALUES &&
857  lowest_outer_join == NULL &&
858  containing_appendrel == NULL &&
859  is_simple_values(root, rte))
860  return pull_up_simple_values(root, jtnode, rte);
861 
862  /*
863  * Or perhaps it's a FUNCTION RTE that we could inline?
864  */
865  if (rte->rtekind == RTE_FUNCTION)
866  return pull_up_constant_function(root, jtnode, rte,
867  lowest_nulling_outer_join,
868  containing_appendrel);
869 
870  /* Otherwise, do nothing at this node. */
871  }
872  else if (IsA(jtnode, FromExpr))
873  {
874  FromExpr *f = (FromExpr *) jtnode;
875  ListCell *l;
876 
877  Assert(containing_appendrel == NULL);
878  /* Recursively transform all the child nodes */
879  foreach(l, f->fromlist)
880  {
882  lowest_outer_join,
883  lowest_nulling_outer_join,
884  NULL);
885  }
886  }
887  else if (IsA(jtnode, JoinExpr))
888  {
889  JoinExpr *j = (JoinExpr *) jtnode;
890 
891  Assert(containing_appendrel == NULL);
892  /* Recurse, being careful to tell myself when inside outer join */
893  switch (j->jointype)
894  {
895  case JOIN_INNER:
896  j->larg = pull_up_subqueries_recurse(root, j->larg,
897  lowest_outer_join,
898  lowest_nulling_outer_join,
899  NULL);
900  j->rarg = pull_up_subqueries_recurse(root, j->rarg,
901  lowest_outer_join,
902  lowest_nulling_outer_join,
903  NULL);
904  break;
905  case JOIN_LEFT:
906  case JOIN_SEMI:
907  case JOIN_ANTI:
908  j->larg = pull_up_subqueries_recurse(root, j->larg,
909  j,
910  lowest_nulling_outer_join,
911  NULL);
912  j->rarg = pull_up_subqueries_recurse(root, j->rarg,
913  j,
914  j,
915  NULL);
916  break;
917  case JOIN_FULL:
918  j->larg = pull_up_subqueries_recurse(root, j->larg,
919  j,
920  j,
921  NULL);
922  j->rarg = pull_up_subqueries_recurse(root, j->rarg,
923  j,
924  j,
925  NULL);
926  break;
927  case JOIN_RIGHT:
928  j->larg = pull_up_subqueries_recurse(root, j->larg,
929  j,
930  j,
931  NULL);
932  j->rarg = pull_up_subqueries_recurse(root, j->rarg,
933  j,
934  lowest_nulling_outer_join,
935  NULL);
936  break;
937  default:
938  elog(ERROR, "unrecognized join type: %d",
939  (int) j->jointype);
940  break;
941  }
942  }
943  else
944  elog(ERROR, "unrecognized node type: %d",
945  (int) nodeTag(jtnode));
946  return jtnode;
947 }
948 
949 /*
950  * pull_up_simple_subquery
951  * Attempt to pull up a single simple subquery.
952  *
953  * jtnode is a RangeTblRef that has been tentatively identified as a simple
954  * subquery by pull_up_subqueries. We return the replacement jointree node,
955  * or jtnode itself if we determine that the subquery can't be pulled up
956  * after all.
957  *
958  * rte is the RangeTblEntry referenced by jtnode. Remaining parameters are
959  * as for pull_up_subqueries_recurse.
960  */
961 static Node *
963  JoinExpr *lowest_outer_join,
964  JoinExpr *lowest_nulling_outer_join,
965  AppendRelInfo *containing_appendrel)
966 {
967  Query *parse = root->parse;
968  int varno = ((RangeTblRef *) jtnode)->rtindex;
969  Query *subquery;
970  PlannerInfo *subroot;
971  int rtoffset;
972  pullup_replace_vars_context rvcontext;
973  ListCell *lc;
974 
975  /*
976  * Make a modifiable copy of the subquery to hack on, so that the RTE will
977  * be left unchanged in case we decide below that we can't pull it up
978  * after all.
979  */
980  subquery = copyObject(rte->subquery);
981 
982  /*
983  * Create a PlannerInfo data structure for this subquery.
984  *
985  * NOTE: the next few steps should match the first processing in
986  * subquery_planner(). Can we refactor to avoid code duplication, or
987  * would that just make things uglier?
988  */
989  subroot = makeNode(PlannerInfo);
990  subroot->parse = subquery;
991  subroot->glob = root->glob;
992  subroot->query_level = root->query_level;
993  subroot->parent_root = root->parent_root;
994  subroot->plan_params = NIL;
995  subroot->outer_params = NULL;
996  subroot->planner_cxt = CurrentMemoryContext;
997  subroot->init_plans = NIL;
998  subroot->cte_plan_ids = NIL;
999  subroot->multiexpr_params = NIL;
1000  subroot->eq_classes = NIL;
1001  subroot->ec_merging_done = false;
1002  subroot->all_result_relids = NULL;
1003  subroot->leaf_result_relids = NULL;
1004  subroot->append_rel_list = NIL;
1005  subroot->row_identity_vars = NIL;
1006  subroot->rowMarks = NIL;
1007  memset(subroot->upper_rels, 0, sizeof(subroot->upper_rels));
1008  memset(subroot->upper_targets, 0, sizeof(subroot->upper_targets));
1009  subroot->processed_tlist = NIL;
1010  subroot->update_colnos = NIL;
1011  subroot->grouping_map = NULL;
1012  subroot->minmax_aggs = NIL;
1013  subroot->qual_security_level = 0;
1014  subroot->placeholdersFrozen = false;
1015  subroot->hasRecursion = false;
1016  subroot->wt_param_id = -1;
1017  subroot->non_recursive_path = NULL;
1018 
1019  /* No CTEs to worry about */
1020  Assert(subquery->cteList == NIL);
1021 
1022  /*
1023  * If the FROM clause is empty, replace it with a dummy RTE_RESULT RTE, so
1024  * that we don't need so many special cases to deal with that situation.
1025  */
1026  replace_empty_jointree(subquery);
1027 
1028  /*
1029  * Pull up any SubLinks within the subquery's quals, so that we don't
1030  * leave unoptimized SubLinks behind.
1031  */
1032  if (subquery->hasSubLinks)
1033  pull_up_sublinks(subroot);
1034 
1035  /*
1036  * Similarly, preprocess its function RTEs to inline any set-returning
1037  * functions in its rangetable.
1038  */
1039  preprocess_function_rtes(subroot);
1040 
1041  /*
1042  * Recursively pull up the subquery's subqueries, so that
1043  * pull_up_subqueries' processing is complete for its jointree and
1044  * rangetable.
1045  *
1046  * Note: it's okay that the subquery's recursion starts with NULL for
1047  * containing-join info, even if we are within an outer join in the upper
1048  * query; the lower query starts with a clean slate for outer-join
1049  * semantics. Likewise, we needn't pass down appendrel state.
1050  */
1051  pull_up_subqueries(subroot);
1052 
1053  /*
1054  * Now we must recheck whether the subquery is still simple enough to pull
1055  * up. If not, abandon processing it.
1056  *
1057  * We don't really need to recheck all the conditions involved, but it's
1058  * easier just to keep this "if" looking the same as the one in
1059  * pull_up_subqueries_recurse.
1060  */
1061  if (is_simple_subquery(root, subquery, rte, lowest_outer_join) &&
1062  (containing_appendrel == NULL || is_safe_append_member(subquery)))
1063  {
1064  /* good to go */
1065  }
1066  else
1067  {
1068  /*
1069  * Give up, return unmodified RangeTblRef.
1070  *
1071  * Note: The work we just did will be redone when the subquery gets
1072  * planned on its own. Perhaps we could avoid that by storing the
1073  * modified subquery back into the rangetable, but I'm not gonna risk
1074  * it now.
1075  */
1076  return jtnode;
1077  }
1078 
1079  /*
1080  * We must flatten any join alias Vars in the subquery's targetlist,
1081  * because pulling up the subquery's subqueries might have changed their
1082  * expansions into arbitrary expressions, which could affect
1083  * pullup_replace_vars' decisions about whether PlaceHolderVar wrappers
1084  * are needed for tlist entries. (Likely it'd be better to do
1085  * flatten_join_alias_vars on the whole query tree at some earlier stage,
1086  * maybe even in the rewriter; but for now let's just fix this case here.)
1087  */
1088  subquery->targetList = (List *)
1089  flatten_join_alias_vars(subroot->parse, (Node *) subquery->targetList);
1090 
1091  /*
1092  * Adjust level-0 varnos in subquery so that we can append its rangetable
1093  * to upper query's. We have to fix the subquery's append_rel_list as
1094  * well.
1095  */
1096  rtoffset = list_length(parse->rtable);
1097  OffsetVarNodes((Node *) subquery, rtoffset, 0);
1098  OffsetVarNodes((Node *) subroot->append_rel_list, rtoffset, 0);
1099 
1100  /*
1101  * Upper-level vars in subquery are now one level closer to their parent
1102  * than before.
1103  */
1104  IncrementVarSublevelsUp((Node *) subquery, -1, 1);
1105  IncrementVarSublevelsUp((Node *) subroot->append_rel_list, -1, 1);
1106 
1107  /*
1108  * The subquery's targetlist items are now in the appropriate form to
1109  * insert into the top query, except that we may need to wrap them in
1110  * PlaceHolderVars. Set up required context data for pullup_replace_vars.
1111  */
1112  rvcontext.root = root;
1113  rvcontext.targetlist = subquery->targetList;
1114  rvcontext.target_rte = rte;
1115  if (rte->lateral)
1116  rvcontext.relids = get_relids_in_jointree((Node *) subquery->jointree,
1117  true);
1118  else /* won't need relids */
1119  rvcontext.relids = NULL;
1120  rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
1121  rvcontext.varno = varno;
1122  /* these flags will be set below, if needed */
1123  rvcontext.need_phvs = false;
1124  rvcontext.wrap_non_vars = false;
1125  /* initialize cache array with indexes 0 .. length(tlist) */
1126  rvcontext.rv_cache = palloc0((list_length(subquery->targetList) + 1) *
1127  sizeof(Node *));
1128 
1129  /*
1130  * If we are under an outer join then non-nullable items and lateral
1131  * references may have to be turned into PlaceHolderVars.
1132  */
1133  if (lowest_nulling_outer_join != NULL)
1134  rvcontext.need_phvs = true;
1135 
1136  /*
1137  * If we are dealing with an appendrel member then anything that's not a
1138  * simple Var has to be turned into a PlaceHolderVar. We force this to
1139  * ensure that what we pull up doesn't get merged into a surrounding
1140  * expression during later processing and then fail to match the
1141  * expression actually available from the appendrel.
1142  */
1143  if (containing_appendrel != NULL)
1144  {
1145  rvcontext.need_phvs = true;
1146  rvcontext.wrap_non_vars = true;
1147  }
1148 
1149  /*
1150  * If the parent query uses grouping sets, we need a PlaceHolderVar for
1151  * anything that's not a simple Var. Again, this ensures that expressions
1152  * retain their separate identity so that they will match grouping set
1153  * columns when appropriate. (It'd be sufficient to wrap values used in
1154  * grouping set columns, and do so only in non-aggregated portions of the
1155  * tlist and havingQual, but that would require a lot of infrastructure
1156  * that pullup_replace_vars hasn't currently got.)
1157  */
1158  if (parse->groupingSets)
1159  {
1160  rvcontext.need_phvs = true;
1161  rvcontext.wrap_non_vars = true;
1162  }
1163 
1164  /*
1165  * Replace all of the top query's references to the subquery's outputs
1166  * with copies of the adjusted subtlist items, being careful not to
1167  * replace any of the jointree structure.
1168  */
1169  perform_pullup_replace_vars(root, &rvcontext,
1170  lowest_nulling_outer_join,
1171  containing_appendrel);
1172 
1173  /*
1174  * If the subquery had a LATERAL marker, propagate that to any of its
1175  * child RTEs that could possibly now contain lateral cross-references.
1176  * The children might or might not contain any actual lateral
1177  * cross-references, but we have to mark the pulled-up child RTEs so that
1178  * later planner stages will check for such.
1179  */
1180  if (rte->lateral)
1181  {
1182  foreach(lc, subquery->rtable)
1183  {
1184  RangeTblEntry *child_rte = (RangeTblEntry *) lfirst(lc);
1185 
1186  switch (child_rte->rtekind)
1187  {
1188  case RTE_RELATION:
1189  if (child_rte->tablesample)
1190  child_rte->lateral = true;
1191  break;
1192  case RTE_SUBQUERY:
1193  case RTE_FUNCTION:
1194  case RTE_VALUES:
1195  case RTE_TABLEFUNC:
1196  child_rte->lateral = true;
1197  break;
1198  case RTE_JOIN:
1199  case RTE_CTE:
1200  case RTE_NAMEDTUPLESTORE:
1201  case RTE_RESULT:
1202  /* these can't contain any lateral references */
1203  break;
1204  }
1205  }
1206  }
1207 
1208  /*
1209  * Now append the adjusted rtable entries to upper query. (We hold off
1210  * until after fixing the upper rtable entries; no point in running that
1211  * code on the subquery ones too.)
1212  */
1213  parse->rtable = list_concat(parse->rtable, subquery->rtable);
1214 
1215  /*
1216  * Pull up any FOR UPDATE/SHARE markers, too. (OffsetVarNodes already
1217  * adjusted the marker rtindexes, so just concat the lists.)
1218  */
1219  parse->rowMarks = list_concat(parse->rowMarks, subquery->rowMarks);
1220 
1221  /*
1222  * We also have to fix the relid sets of any PlaceHolderVar nodes in the
1223  * parent query. (This could perhaps be done by pullup_replace_vars(),
1224  * but it seems cleaner to use two passes.) Note in particular that any
1225  * PlaceHolderVar nodes just created by pullup_replace_vars() will be
1226  * adjusted, so having created them with the subquery's varno is correct.
1227  *
1228  * Likewise, relids appearing in AppendRelInfo nodes have to be fixed. We
1229  * already checked that this won't require introducing multiple subrelids
1230  * into the single-slot AppendRelInfo structs.
1231  */
1232  if (parse->hasSubLinks || root->glob->lastPHId != 0 ||
1233  root->append_rel_list)
1234  {
1235  Relids subrelids;
1236 
1237  subrelids = get_relids_in_jointree((Node *) subquery->jointree, false);
1238  substitute_phv_relids((Node *) parse, varno, subrelids);
1239  fix_append_rel_relids(root->append_rel_list, varno, subrelids);
1240  }
1241 
1242  /*
1243  * And now add subquery's AppendRelInfos to our list.
1244  */
1246  subroot->append_rel_list);
1247 
1248  /*
1249  * We don't have to do the equivalent bookkeeping for outer-join info,
1250  * because that hasn't been set up yet. placeholder_list likewise.
1251  */
1252  Assert(root->join_info_list == NIL);
1253  Assert(subroot->join_info_list == NIL);
1254  Assert(root->placeholder_list == NIL);
1255  Assert(subroot->placeholder_list == NIL);
1256 
1257  /*
1258  * We no longer need the RTE's copy of the subquery's query tree. Getting
1259  * rid of it saves nothing in particular so far as this level of query is
1260  * concerned; but if this query level is in turn pulled up into a parent,
1261  * we'd waste cycles copying the now-unused query tree.
1262  */
1263  rte->subquery = NULL;
1264 
1265  /*
1266  * Miscellaneous housekeeping.
1267  *
1268  * Although replace_rte_variables() faithfully updated parse->hasSubLinks
1269  * if it copied any SubLinks out of the subquery's targetlist, we still
1270  * could have SubLinks added to the query in the expressions of FUNCTION
1271  * and VALUES RTEs copied up from the subquery. So it's necessary to copy
1272  * subquery->hasSubLinks anyway. Perhaps this can be improved someday.
1273  */
1274  parse->hasSubLinks |= subquery->hasSubLinks;
1275 
1276  /* If subquery had any RLS conditions, now main query does too */
1277  parse->hasRowSecurity |= subquery->hasRowSecurity;
1278 
1279  /*
1280  * subquery won't be pulled up if it hasAggs, hasWindowFuncs, or
1281  * hasTargetSRFs, so no work needed on those flags
1282  */
1283 
1284  /*
1285  * Return the adjusted subquery jointree to replace the RangeTblRef entry
1286  * in parent's jointree; or, if the FromExpr is degenerate, just return
1287  * its single member.
1288  */
1289  Assert(IsA(subquery->jointree, FromExpr));
1290  Assert(subquery->jointree->fromlist != NIL);
1291  if (subquery->jointree->quals == NULL &&
1292  list_length(subquery->jointree->fromlist) == 1)
1293  return (Node *) linitial(subquery->jointree->fromlist);
1294 
1295  return (Node *) subquery->jointree;
1296 }
1297 
1298 /*
1299  * pull_up_simple_union_all
1300  * Pull up a single simple UNION ALL subquery.
1301  *
1302  * jtnode is a RangeTblRef that has been identified as a simple UNION ALL
1303  * subquery by pull_up_subqueries. We pull up the leaf subqueries and
1304  * build an "append relation" for the union set. The result value is just
1305  * jtnode, since we don't actually need to change the query jointree.
1306  */
1307 static Node *
1309 {
1310  int varno = ((RangeTblRef *) jtnode)->rtindex;
1311  Query *subquery = rte->subquery;
1312  int rtoffset = list_length(root->parse->rtable);
1313  List *rtable;
1314 
1315  /*
1316  * Make a modifiable copy of the subquery's rtable, so we can adjust
1317  * upper-level Vars in it. There are no such Vars in the setOperations
1318  * tree proper, so fixing the rtable should be sufficient.
1319  */
1320  rtable = copyObject(subquery->rtable);
1321 
1322  /*
1323  * Upper-level vars in subquery are now one level closer to their parent
1324  * than before. We don't have to worry about offsetting varnos, though,
1325  * because the UNION leaf queries can't cross-reference each other.
1326  */
1327  IncrementVarSublevelsUp_rtable(rtable, -1, 1);
1328 
1329  /*
1330  * If the UNION ALL subquery had a LATERAL marker, propagate that to all
1331  * its children. The individual children might or might not contain any
1332  * actual lateral cross-references, but we have to mark the pulled-up
1333  * child RTEs so that later planner stages will check for such.
1334  */
1335  if (rte->lateral)
1336  {
1337  ListCell *rt;
1338 
1339  foreach(rt, rtable)
1340  {
1341  RangeTblEntry *child_rte = (RangeTblEntry *) lfirst(rt);
1342 
1343  Assert(child_rte->rtekind == RTE_SUBQUERY);
1344  child_rte->lateral = true;
1345  }
1346  }
1347 
1348  /*
1349  * Append child RTEs to parent rtable.
1350  */
1351  root->parse->rtable = list_concat(root->parse->rtable, rtable);
1352 
1353  /*
1354  * Recursively scan the subquery's setOperations tree and add
1355  * AppendRelInfo nodes for leaf subqueries to the parent's
1356  * append_rel_list. Also apply pull_up_subqueries to the leaf subqueries.
1357  */
1358  Assert(subquery->setOperations);
1359  pull_up_union_leaf_queries(subquery->setOperations, root, varno, subquery,
1360  rtoffset);
1361 
1362  /*
1363  * Mark the parent as an append relation.
1364  */
1365  rte->inh = true;
1366 
1367  return jtnode;
1368 }
1369 
1370 /*
1371  * pull_up_union_leaf_queries -- recursive guts of pull_up_simple_union_all
1372  *
1373  * Build an AppendRelInfo for each leaf query in the setop tree, and then
1374  * apply pull_up_subqueries to the leaf query.
1375  *
1376  * Note that setOpQuery is the Query containing the setOp node, whose tlist
1377  * contains references to all the setop output columns. When called from
1378  * pull_up_simple_union_all, this is *not* the same as root->parse, which is
1379  * the parent Query we are pulling up into.
1380  *
1381  * parentRTindex is the appendrel parent's index in root->parse->rtable.
1382  *
1383  * The child RTEs have already been copied to the parent. childRToffset
1384  * tells us where in the parent's range table they were copied. When called
1385  * from flatten_simple_union_all, childRToffset is 0 since the child RTEs
1386  * were already in root->parse->rtable and no RT index adjustment is needed.
1387  */
1388 static void
1389 pull_up_union_leaf_queries(Node *setOp, PlannerInfo *root, int parentRTindex,
1390  Query *setOpQuery, int childRToffset)
1391 {
1392  if (IsA(setOp, RangeTblRef))
1393  {
1394  RangeTblRef *rtr = (RangeTblRef *) setOp;
1395  int childRTindex;
1396  AppendRelInfo *appinfo;
1397 
1398  /*
1399  * Calculate the index in the parent's range table
1400  */
1401  childRTindex = childRToffset + rtr->rtindex;
1402 
1403  /*
1404  * Build a suitable AppendRelInfo, and attach to parent's list.
1405  */
1406  appinfo = makeNode(AppendRelInfo);
1407  appinfo->parent_relid = parentRTindex;
1408  appinfo->child_relid = childRTindex;
1409  appinfo->parent_reltype = InvalidOid;
1410  appinfo->child_reltype = InvalidOid;
1411  make_setop_translation_list(setOpQuery, childRTindex, appinfo);
1412  appinfo->parent_reloid = InvalidOid;
1413  root->append_rel_list = lappend(root->append_rel_list, appinfo);
1414 
1415  /*
1416  * Recursively apply pull_up_subqueries to the new child RTE. (We
1417  * must build the AppendRelInfo first, because this will modify it.)
1418  * Note that we can pass NULL for containing-join info even if we're
1419  * actually under an outer join, because the child's expressions
1420  * aren't going to propagate up to the join. Also, we ignore the
1421  * possibility that pull_up_subqueries_recurse() returns a different
1422  * jointree node than what we pass it; if it does, the important thing
1423  * is that it replaced the child relid in the AppendRelInfo node.
1424  */
1425  rtr = makeNode(RangeTblRef);
1426  rtr->rtindex = childRTindex;
1427  (void) pull_up_subqueries_recurse(root, (Node *) rtr,
1428  NULL, NULL, appinfo);
1429  }
1430  else if (IsA(setOp, SetOperationStmt))
1431  {
1432  SetOperationStmt *op = (SetOperationStmt *) setOp;
1433 
1434  /* Recurse to reach leaf queries */
1435  pull_up_union_leaf_queries(op->larg, root, parentRTindex, setOpQuery,
1436  childRToffset);
1437  pull_up_union_leaf_queries(op->rarg, root, parentRTindex, setOpQuery,
1438  childRToffset);
1439  }
1440  else
1441  {
1442  elog(ERROR, "unrecognized node type: %d",
1443  (int) nodeTag(setOp));
1444  }
1445 }
1446 
1447 /*
1448  * make_setop_translation_list
1449  * Build the list of translations from parent Vars to child Vars for
1450  * a UNION ALL member. (At this point it's just a simple list of
1451  * referencing Vars, but if we succeed in pulling up the member
1452  * subquery, the Vars will get replaced by pulled-up expressions.)
1453  * Also create the rather trivial reverse-translation array.
1454  */
1455 static void
1456 make_setop_translation_list(Query *query, int newvarno,
1457  AppendRelInfo *appinfo)
1458 {
1459  List *vars = NIL;
1460  AttrNumber *pcolnos;
1461  ListCell *l;
1462 
1463  /* Initialize reverse-translation array with all entries zero */
1464  /* (entries for resjunk columns will stay that way) */
1465  appinfo->num_child_cols = list_length(query->targetList);
1466  appinfo->parent_colnos = pcolnos =
1467  (AttrNumber *) palloc0(appinfo->num_child_cols * sizeof(AttrNumber));
1468 
1469  foreach(l, query->targetList)
1470  {
1471  TargetEntry *tle = (TargetEntry *) lfirst(l);
1472 
1473  if (tle->resjunk)
1474  continue;
1475 
1476  vars = lappend(vars, makeVarFromTargetEntry(newvarno, tle));
1477  pcolnos[tle->resno - 1] = tle->resno;
1478  }
1479 
1480  appinfo->translated_vars = vars;
1481 }
1482 
1483 /*
1484  * is_simple_subquery
1485  * Check a subquery in the range table to see if it's simple enough
1486  * to pull up into the parent query.
1487  *
1488  * rte is the RTE_SUBQUERY RangeTblEntry that contained the subquery.
1489  * (Note subquery is not necessarily equal to rte->subquery; it could be a
1490  * processed copy of that.)
1491  * lowest_outer_join is the lowest outer join above the subquery, or NULL.
1492  */
1493 static bool
1495  JoinExpr *lowest_outer_join)
1496 {
1497  /*
1498  * Let's just make sure it's a valid subselect ...
1499  */
1500  if (!IsA(subquery, Query) ||
1501  subquery->commandType != CMD_SELECT)
1502  elog(ERROR, "subquery is bogus");
1503 
1504  /*
1505  * Can't currently pull up a query with setops (unless it's simple UNION
1506  * ALL, which is handled by a different code path). Maybe after querytree
1507  * redesign...
1508  */
1509  if (subquery->setOperations)
1510  return false;
1511 
1512  /*
1513  * Can't pull up a subquery involving grouping, aggregation, SRFs,
1514  * sorting, limiting, or WITH. (XXX WITH could possibly be allowed later)
1515  *
1516  * We also don't pull up a subquery that has explicit FOR UPDATE/SHARE
1517  * clauses, because pullup would cause the locking to occur semantically
1518  * higher than it should. Implicit FOR UPDATE/SHARE is okay because in
1519  * that case the locking was originally declared in the upper query
1520  * anyway.
1521  */
1522  if (subquery->hasAggs ||
1523  subquery->hasWindowFuncs ||
1524  subquery->hasTargetSRFs ||
1525  subquery->groupClause ||
1526  subquery->groupingSets ||
1527  subquery->havingQual ||
1528  subquery->sortClause ||
1529  subquery->distinctClause ||
1530  subquery->limitOffset ||
1531  subquery->limitCount ||
1532  subquery->hasForUpdate ||
1533  subquery->cteList)
1534  return false;
1535 
1536  /*
1537  * Don't pull up if the RTE represents a security-barrier view; we
1538  * couldn't prevent information leakage once the RTE's Vars are scattered
1539  * about in the upper query.
1540  */
1541  if (rte->security_barrier)
1542  return false;
1543 
1544  /*
1545  * If the subquery is LATERAL, check for pullup restrictions from that.
1546  */
1547  if (rte->lateral)
1548  {
1549  bool restricted;
1550  Relids safe_upper_varnos;
1551 
1552  /*
1553  * The subquery's WHERE and JOIN/ON quals mustn't contain any lateral
1554  * references to rels outside a higher outer join (including the case
1555  * where the outer join is within the subquery itself). In such a
1556  * case, pulling up would result in a situation where we need to
1557  * postpone quals from below an outer join to above it, which is
1558  * probably completely wrong and in any case is a complication that
1559  * doesn't seem worth addressing at the moment.
1560  */
1561  if (lowest_outer_join != NULL)
1562  {
1563  restricted = true;
1564  safe_upper_varnos = get_relids_in_jointree((Node *) lowest_outer_join,
1565  true);
1566  }
1567  else
1568  {
1569  restricted = false;
1570  safe_upper_varnos = NULL; /* doesn't matter */
1571  }
1572 
1574  (Node *) subquery->jointree,
1575  restricted, safe_upper_varnos))
1576  return false;
1577 
1578  /*
1579  * If there's an outer join above the LATERAL subquery, also disallow
1580  * pullup if the subquery's targetlist has any references to rels
1581  * outside the outer join, since these might get pulled into quals
1582  * above the subquery (but in or below the outer join) and then lead
1583  * to qual-postponement issues similar to the case checked for above.
1584  * (We wouldn't need to prevent pullup if no such references appear in
1585  * outer-query quals, but we don't have enough info here to check
1586  * that. Also, maybe this restriction could be removed if we forced
1587  * such refs to be wrapped in PlaceHolderVars, even when they're below
1588  * the nearest outer join? But it's a pretty hokey usage, so not
1589  * clear this is worth sweating over.)
1590  */
1591  if (lowest_outer_join != NULL)
1592  {
1593  Relids lvarnos = pull_varnos_of_level(root,
1594  (Node *) subquery->targetList,
1595  1);
1596 
1597  if (!bms_is_subset(lvarnos, safe_upper_varnos))
1598  return false;
1599  }
1600  }
1601 
1602  /*
1603  * Don't pull up a subquery that has any volatile functions in its
1604  * targetlist. Otherwise we might introduce multiple evaluations of these
1605  * functions, if they get copied to multiple places in the upper query,
1606  * leading to surprising results. (Note: the PlaceHolderVar mechanism
1607  * doesn't quite guarantee single evaluation; else we could pull up anyway
1608  * and just wrap such items in PlaceHolderVars ...)
1609  */
1610  if (contain_volatile_functions((Node *) subquery->targetList))
1611  return false;
1612 
1613  return true;
1614 }
1615 
1616 /*
1617  * pull_up_simple_values
1618  * Pull up a single simple VALUES RTE.
1619  *
1620  * jtnode is a RangeTblRef that has been identified as a simple VALUES RTE
1621  * by pull_up_subqueries. We always return a RangeTblRef representing a
1622  * RESULT RTE to replace it (all failure cases should have been detected by
1623  * is_simple_values()). Actually, what we return is just jtnode, because
1624  * we replace the VALUES RTE in the rangetable with the RESULT RTE.
1625  *
1626  * rte is the RangeTblEntry referenced by jtnode. Because of the limited
1627  * possible usage of VALUES RTEs, we do not need the remaining parameters
1628  * of pull_up_subqueries_recurse.
1629  */
1630 static Node *
1632 {
1633  Query *parse = root->parse;
1634  int varno = ((RangeTblRef *) jtnode)->rtindex;
1635  List *values_list;
1636  List *tlist;
1637  AttrNumber attrno;
1638  pullup_replace_vars_context rvcontext;
1639  ListCell *lc;
1640 
1641  Assert(rte->rtekind == RTE_VALUES);
1642  Assert(list_length(rte->values_lists) == 1);
1643 
1644  /*
1645  * Need a modifiable copy of the VALUES list to hack on, just in case it's
1646  * multiply referenced.
1647  */
1648  values_list = copyObject(linitial(rte->values_lists));
1649 
1650  /*
1651  * The VALUES RTE can't contain any Vars of level zero, let alone any that
1652  * are join aliases, so no need to flatten join alias Vars.
1653  */
1654  Assert(!contain_vars_of_level((Node *) values_list, 0));
1655 
1656  /*
1657  * Set up required context data for pullup_replace_vars. In particular,
1658  * we have to make the VALUES list look like a subquery targetlist.
1659  */
1660  tlist = NIL;
1661  attrno = 1;
1662  foreach(lc, values_list)
1663  {
1664  tlist = lappend(tlist,
1665  makeTargetEntry((Expr *) lfirst(lc),
1666  attrno,
1667  NULL,
1668  false));
1669  attrno++;
1670  }
1671  rvcontext.root = root;
1672  rvcontext.targetlist = tlist;
1673  rvcontext.target_rte = rte;
1674  rvcontext.relids = NULL;
1675  rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
1676  rvcontext.varno = varno;
1677  rvcontext.need_phvs = false;
1678  rvcontext.wrap_non_vars = false;
1679  /* initialize cache array with indexes 0 .. length(tlist) */
1680  rvcontext.rv_cache = palloc0((list_length(tlist) + 1) *
1681  sizeof(Node *));
1682 
1683  /*
1684  * Replace all of the top query's references to the RTE's outputs with
1685  * copies of the adjusted VALUES expressions, being careful not to replace
1686  * any of the jointree structure. We can assume there's no outer joins or
1687  * appendrels in the dummy Query that surrounds a VALUES RTE.
1688  */
1689  perform_pullup_replace_vars(root, &rvcontext, NULL, NULL);
1690 
1691  /*
1692  * There should be no appendrels to fix, nor any outer joins and hence no
1693  * PlaceHolderVars.
1694  */
1695  Assert(root->append_rel_list == NIL);
1696  Assert(root->join_info_list == NIL);
1697  Assert(root->placeholder_list == NIL);
1698 
1699  /*
1700  * Replace the VALUES RTE with a RESULT RTE. The VALUES RTE is the only
1701  * rtable entry in the current query level, so this is easy.
1702  */
1703  Assert(list_length(parse->rtable) == 1);
1704 
1705  /* Create suitable RTE */
1706  rte = makeNode(RangeTblEntry);
1707  rte->rtekind = RTE_RESULT;
1708  rte->eref = makeAlias("*RESULT*", NIL);
1709 
1710  /* Replace rangetable */
1711  parse->rtable = list_make1(rte);
1712 
1713  /* We could manufacture a new RangeTblRef, but the one we have is fine */
1714  Assert(varno == 1);
1715 
1716  return jtnode;
1717 }
1718 
1719 /*
1720  * is_simple_values
1721  * Check a VALUES RTE in the range table to see if it's simple enough
1722  * to pull up into the parent query.
1723  *
1724  * rte is the RTE_VALUES RangeTblEntry to check.
1725  */
1726 static bool
1728 {
1729  Assert(rte->rtekind == RTE_VALUES);
1730 
1731  /*
1732  * There must be exactly one VALUES list, else it's not semantically
1733  * correct to replace the VALUES RTE with a RESULT RTE, nor would we have
1734  * a unique set of expressions to substitute into the parent query.
1735  */
1736  if (list_length(rte->values_lists) != 1)
1737  return false;
1738 
1739  /*
1740  * Because VALUES can't appear under an outer join (or at least, we won't
1741  * try to pull it up if it does), we need not worry about LATERAL, nor
1742  * about validity of PHVs for the VALUES' outputs.
1743  */
1744 
1745  /*
1746  * Don't pull up a VALUES that contains any set-returning or volatile
1747  * functions. The considerations here are basically identical to the
1748  * restrictions on a pull-able subquery's targetlist.
1749  */
1750  if (expression_returns_set((Node *) rte->values_lists) ||
1752  return false;
1753 
1754  /*
1755  * Do not pull up a VALUES that's not the only RTE in its parent query.
1756  * This is actually the only case that the parser will generate at the
1757  * moment, and assuming this is true greatly simplifies
1758  * pull_up_simple_values().
1759  */
1760  if (list_length(root->parse->rtable) != 1 ||
1761  rte != (RangeTblEntry *) linitial(root->parse->rtable))
1762  return false;
1763 
1764  return true;
1765 }
1766 
1767 /*
1768  * pull_up_constant_function
1769  * Pull up an RTE_FUNCTION expression that was simplified to a constant.
1770  *
1771  * jtnode is a RangeTblRef that has been identified as a FUNCTION RTE by
1772  * pull_up_subqueries. If its expression is just a Const, hoist that value
1773  * up into the parent query, and replace the RTE_FUNCTION with RTE_RESULT.
1774  *
1775  * In principle we could pull up any immutable expression, but we don't.
1776  * That might result in multiple evaluations of the expression, which could
1777  * be costly if it's not just a Const. Also, the main value of this is
1778  * to let the constant participate in further const-folding, and of course
1779  * that won't happen for a non-Const.
1780  *
1781  * The pulled-up value might need to be wrapped in a PlaceHolderVar if the
1782  * RTE is below an outer join or is part of an appendrel; the extra
1783  * parameters show whether that's needed.
1784  */
1785 static Node *
1787  RangeTblEntry *rte,
1788  JoinExpr *lowest_nulling_outer_join,
1789  AppendRelInfo *containing_appendrel)
1790 {
1791  Query *parse = root->parse;
1792  RangeTblFunction *rtf;
1793  TypeFuncClass functypclass;
1794  Oid funcrettype;
1795  TupleDesc tupdesc;
1796  pullup_replace_vars_context rvcontext;
1797 
1798  /* Fail if the RTE has ORDINALITY - we don't implement that here. */
1799  if (rte->funcordinality)
1800  return jtnode;
1801 
1802  /* Fail if RTE isn't a single, simple Const expr */
1803  if (list_length(rte->functions) != 1)
1804  return jtnode;
1806  if (!IsA(rtf->funcexpr, Const))
1807  return jtnode;
1808 
1809  /*
1810  * If the function's result is not a scalar, we punt. In principle we
1811  * could break the composite constant value apart into per-column
1812  * constants, but for now it seems not worth the work.
1813  */
1814  if (rtf->funccolcount != 1)
1815  return jtnode; /* definitely composite */
1816 
1817  functypclass = get_expr_result_type(rtf->funcexpr,
1818  &funcrettype,
1819  &tupdesc);
1820  if (functypclass != TYPEFUNC_SCALAR)
1821  return jtnode; /* must be a one-column composite type */
1822 
1823  /* Create context for applying pullup_replace_vars */
1824  rvcontext.root = root;
1825  rvcontext.targetlist = list_make1(makeTargetEntry((Expr *) rtf->funcexpr,
1826  1, /* resno */
1827  NULL, /* resname */
1828  false)); /* resjunk */
1829  rvcontext.target_rte = rte;
1830 
1831  /*
1832  * Since this function was reduced to a Const, it doesn't contain any
1833  * lateral references, even if it's marked as LATERAL. This means we
1834  * don't need to fill relids.
1835  */
1836  rvcontext.relids = NULL;
1837 
1838  rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
1839  rvcontext.varno = ((RangeTblRef *) jtnode)->rtindex;
1840  /* these flags will be set below, if needed */
1841  rvcontext.need_phvs = false;
1842  rvcontext.wrap_non_vars = false;
1843  /* initialize cache array with indexes 0 .. length(tlist) */
1844  rvcontext.rv_cache = palloc0((list_length(rvcontext.targetlist) + 1) *
1845  sizeof(Node *));
1846 
1847  /*
1848  * If we are under an outer join then non-nullable items and lateral
1849  * references may have to be turned into PlaceHolderVars.
1850  */
1851  if (lowest_nulling_outer_join != NULL)
1852  rvcontext.need_phvs = true;
1853 
1854  /*
1855  * If we are dealing with an appendrel member then anything that's not a
1856  * simple Var has to be turned into a PlaceHolderVar. (See comments in
1857  * pull_up_simple_subquery().)
1858  */
1859  if (containing_appendrel != NULL)
1860  {
1861  rvcontext.need_phvs = true;
1862  rvcontext.wrap_non_vars = true;
1863  }
1864 
1865  /*
1866  * If the parent query uses grouping sets, we need a PlaceHolderVar for
1867  * anything that's not a simple Var.
1868  */
1869  if (parse->groupingSets)
1870  {
1871  rvcontext.need_phvs = true;
1872  rvcontext.wrap_non_vars = true;
1873  }
1874 
1875  /*
1876  * Replace all of the top query's references to the RTE's output with
1877  * copies of the funcexpr, being careful not to replace any of the
1878  * jointree structure.
1879  */
1880  perform_pullup_replace_vars(root, &rvcontext,
1881  lowest_nulling_outer_join,
1882  containing_appendrel);
1883 
1884  /*
1885  * We don't need to bother with changing PlaceHolderVars in the parent
1886  * query. Their references to the RT index are still good for now, and
1887  * will get removed later if we're able to drop the RTE_RESULT.
1888  */
1889 
1890  /*
1891  * Convert the RTE to be RTE_RESULT type, signifying that we don't need to
1892  * scan it anymore, and zero out RTE_FUNCTION-specific fields. Also make
1893  * sure the RTE is not marked LATERAL, since elsewhere we don't expect
1894  * RTE_RESULTs to be LATERAL.
1895  */
1896  rte->rtekind = RTE_RESULT;
1897  rte->functions = NIL;
1898  rte->lateral = false;
1899 
1900  /*
1901  * We can reuse the RangeTblRef node.
1902  */
1903  return jtnode;
1904 }
1905 
1906 /*
1907  * is_simple_union_all
1908  * Check a subquery to see if it's a simple UNION ALL.
1909  *
1910  * We require all the setops to be UNION ALL (no mixing) and there can't be
1911  * any datatype coercions involved, ie, all the leaf queries must emit the
1912  * same datatypes.
1913  */
1914 static bool
1916 {
1917  SetOperationStmt *topop;
1918 
1919  /* Let's just make sure it's a valid subselect ... */
1920  if (!IsA(subquery, Query) ||
1921  subquery->commandType != CMD_SELECT)
1922  elog(ERROR, "subquery is bogus");
1923 
1924  /* Is it a set-operation query at all? */
1925  topop = castNode(SetOperationStmt, subquery->setOperations);
1926  if (!topop)
1927  return false;
1928 
1929  /* Can't handle ORDER BY, LIMIT/OFFSET, locking, or WITH */
1930  if (subquery->sortClause ||
1931  subquery->limitOffset ||
1932  subquery->limitCount ||
1933  subquery->rowMarks ||
1934  subquery->cteList)
1935  return false;
1936 
1937  /* Recursively check the tree of set operations */
1938  return is_simple_union_all_recurse((Node *) topop, subquery,
1939  topop->colTypes);
1940 }
1941 
1942 static bool
1943 is_simple_union_all_recurse(Node *setOp, Query *setOpQuery, List *colTypes)
1944 {
1945  if (IsA(setOp, RangeTblRef))
1946  {
1947  RangeTblRef *rtr = (RangeTblRef *) setOp;
1948  RangeTblEntry *rte = rt_fetch(rtr->rtindex, setOpQuery->rtable);
1949  Query *subquery = rte->subquery;
1950 
1951  Assert(subquery != NULL);
1952 
1953  /* Leaf nodes are OK if they match the toplevel column types */
1954  /* We don't have to compare typmods or collations here */
1955  return tlist_same_datatypes(subquery->targetList, colTypes, true);
1956  }
1957  else if (IsA(setOp, SetOperationStmt))
1958  {
1959  SetOperationStmt *op = (SetOperationStmt *) setOp;
1960 
1961  /* Must be UNION ALL */
1962  if (op->op != SETOP_UNION || !op->all)
1963  return false;
1964 
1965  /* Recurse to check inputs */
1966  return is_simple_union_all_recurse(op->larg, setOpQuery, colTypes) &&
1967  is_simple_union_all_recurse(op->rarg, setOpQuery, colTypes);
1968  }
1969  else
1970  {
1971  elog(ERROR, "unrecognized node type: %d",
1972  (int) nodeTag(setOp));
1973  return false; /* keep compiler quiet */
1974  }
1975 }
1976 
1977 /*
1978  * is_safe_append_member
1979  * Check a subquery that is a leaf of a UNION ALL appendrel to see if it's
1980  * safe to pull up.
1981  */
1982 static bool
1984 {
1985  FromExpr *jtnode;
1986 
1987  /*
1988  * It's only safe to pull up the child if its jointree contains exactly
1989  * one RTE, else the AppendRelInfo data structure breaks. The one base RTE
1990  * could be buried in several levels of FromExpr, however. Also, if the
1991  * child's jointree is completely empty, we can pull up because
1992  * pull_up_simple_subquery will insert a single RTE_RESULT RTE instead.
1993  *
1994  * Also, the child can't have any WHERE quals because there's no place to
1995  * put them in an appendrel. (This is a bit annoying...) If we didn't
1996  * need to check this, we'd just test whether get_relids_in_jointree()
1997  * yields a singleton set, to be more consistent with the coding of
1998  * fix_append_rel_relids().
1999  */
2000  jtnode = subquery->jointree;
2001  Assert(IsA(jtnode, FromExpr));
2002  /* Check the completely-empty case */
2003  if (jtnode->fromlist == NIL && jtnode->quals == NULL)
2004  return true;
2005  /* Check the more general case */
2006  while (IsA(jtnode, FromExpr))
2007  {
2008  if (jtnode->quals != NULL)
2009  return false;
2010  if (list_length(jtnode->fromlist) != 1)
2011  return false;
2012  jtnode = linitial(jtnode->fromlist);
2013  }
2014  if (!IsA(jtnode, RangeTblRef))
2015  return false;
2016 
2017  return true;
2018 }
2019 
2020 /*
2021  * jointree_contains_lateral_outer_refs
2022  * Check for disallowed lateral references in a jointree's quals
2023  *
2024  * If restricted is false, all level-1 Vars are allowed (but we still must
2025  * search the jointree, since it might contain outer joins below which there
2026  * will be restrictions). If restricted is true, return true when any qual
2027  * in the jointree contains level-1 Vars coming from outside the rels listed
2028  * in safe_upper_varnos.
2029  */
2030 static bool
2032  bool restricted,
2033  Relids safe_upper_varnos)
2034 {
2035  if (jtnode == NULL)
2036  return false;
2037  if (IsA(jtnode, RangeTblRef))
2038  return false;
2039  else if (IsA(jtnode, FromExpr))
2040  {
2041  FromExpr *f = (FromExpr *) jtnode;
2042  ListCell *l;
2043 
2044  /* First, recurse to check child joins */
2045  foreach(l, f->fromlist)
2046  {
2048  lfirst(l),
2049  restricted,
2050  safe_upper_varnos))
2051  return true;
2052  }
2053 
2054  /* Then check the top-level quals */
2055  if (restricted &&
2057  safe_upper_varnos))
2058  return true;
2059  }
2060  else if (IsA(jtnode, JoinExpr))
2061  {
2062  JoinExpr *j = (JoinExpr *) jtnode;
2063 
2064  /*
2065  * If this is an outer join, we mustn't allow any upper lateral
2066  * references in or below it.
2067  */
2068  if (j->jointype != JOIN_INNER)
2069  {
2070  restricted = true;
2071  safe_upper_varnos = NULL;
2072  }
2073 
2074  /* Check the child joins */
2076  j->larg,
2077  restricted,
2078  safe_upper_varnos))
2079  return true;
2081  j->rarg,
2082  restricted,
2083  safe_upper_varnos))
2084  return true;
2085 
2086  /* Check the JOIN's qual clauses */
2087  if (restricted &&
2088  !bms_is_subset(pull_varnos_of_level(root, j->quals, 1),
2089  safe_upper_varnos))
2090  return true;
2091  }
2092  else
2093  elog(ERROR, "unrecognized node type: %d",
2094  (int) nodeTag(jtnode));
2095  return false;
2096 }
2097 
2098 /*
2099  * Perform pullup_replace_vars everyplace it's needed in the query tree.
2100  *
2101  * Caller has already filled *rvcontext with data describing what to
2102  * substitute for Vars referencing the target subquery. In addition
2103  * we need the identity of the lowest outer join that can null the
2104  * target subquery, and its containing appendrel if any.
2105  */
2106 static void
2108  pullup_replace_vars_context *rvcontext,
2109  JoinExpr *lowest_nulling_outer_join,
2110  AppendRelInfo *containing_appendrel)
2111 {
2112  Query *parse = root->parse;
2113  ListCell *lc;
2114 
2115  /*
2116  * Replace all of the top query's references to the subquery's outputs
2117  * with copies of the adjusted subtlist items, being careful not to
2118  * replace any of the jointree structure. (This'd be a lot cleaner if we
2119  * could use query_tree_mutator.) We have to use PHVs in the targetList,
2120  * returningList, and havingQual, since those are certainly above any
2121  * outer join. replace_vars_in_jointree tracks its location in the
2122  * jointree and uses PHVs or not appropriately.
2123  */
2124  parse->targetList = (List *)
2125  pullup_replace_vars((Node *) parse->targetList, rvcontext);
2126  parse->returningList = (List *)
2127  pullup_replace_vars((Node *) parse->returningList, rvcontext);
2128  if (parse->onConflict)
2129  {
2130  parse->onConflict->onConflictSet = (List *)
2131  pullup_replace_vars((Node *) parse->onConflict->onConflictSet,
2132  rvcontext);
2133  parse->onConflict->onConflictWhere =
2134  pullup_replace_vars(parse->onConflict->onConflictWhere,
2135  rvcontext);
2136 
2137  /*
2138  * We assume ON CONFLICT's arbiterElems, arbiterWhere, exclRelTlist
2139  * can't contain any references to a subquery.
2140  */
2141  }
2142  if (parse->mergeActionList)
2143  {
2144  foreach(lc, parse->mergeActionList)
2145  {
2146  MergeAction *action = lfirst(lc);
2147 
2148  action->qual = pullup_replace_vars(action->qual, rvcontext);
2149  action->targetList = (List *)
2150  pullup_replace_vars((Node *) action->targetList, rvcontext);
2151  }
2152  }
2153  replace_vars_in_jointree((Node *) parse->jointree, rvcontext,
2154  lowest_nulling_outer_join);
2155  Assert(parse->setOperations == NULL);
2156  parse->havingQual = pullup_replace_vars(parse->havingQual, rvcontext);
2157 
2158  /*
2159  * Replace references in the translated_vars lists of appendrels. When
2160  * pulling up an appendrel member, we do not need PHVs in the list of the
2161  * parent appendrel --- there isn't any outer join between. Elsewhere,
2162  * use PHVs for safety. (This analysis could be made tighter but it seems
2163  * unlikely to be worth much trouble.)
2164  */
2165  foreach(lc, root->append_rel_list)
2166  {
2167  AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(lc);
2168  bool save_need_phvs = rvcontext->need_phvs;
2169 
2170  if (appinfo == containing_appendrel)
2171  rvcontext->need_phvs = false;
2172  appinfo->translated_vars = (List *)
2173  pullup_replace_vars((Node *) appinfo->translated_vars, rvcontext);
2174  rvcontext->need_phvs = save_need_phvs;
2175  }
2176 
2177  /*
2178  * Replace references in the joinaliasvars lists of join RTEs.
2179  *
2180  * You might think that we could avoid using PHVs for alias vars of joins
2181  * below lowest_nulling_outer_join, but that doesn't work because the
2182  * alias vars could be referenced above that join; we need the PHVs to be
2183  * present in such references after the alias vars get flattened. (It
2184  * might be worth trying to be smarter here, someday.)
2185  */
2186  foreach(lc, parse->rtable)
2187  {
2188  RangeTblEntry *otherrte = (RangeTblEntry *) lfirst(lc);
2189 
2190  if (otherrte->rtekind == RTE_JOIN)
2191  otherrte->joinaliasvars = (List *)
2192  pullup_replace_vars((Node *) otherrte->joinaliasvars,
2193  rvcontext);
2194  }
2195 }
2196 
2197 /*
2198  * Helper routine for perform_pullup_replace_vars: do pullup_replace_vars on
2199  * every expression in the jointree, without changing the jointree structure
2200  * itself. Ugly, but there's no other way...
2201  *
2202  * If we are at or below lowest_nulling_outer_join, we can suppress use of
2203  * PlaceHolderVars wrapped around the replacement expressions.
2204  */
2205 static void
2207  pullup_replace_vars_context *context,
2208  JoinExpr *lowest_nulling_outer_join)
2209 {
2210  if (jtnode == NULL)
2211  return;
2212  if (IsA(jtnode, RangeTblRef))
2213  {
2214  /*
2215  * If the RangeTblRef refers to a LATERAL subquery (that isn't the
2216  * same subquery we're pulling up), it might contain references to the
2217  * target subquery, which we must replace. We drive this from the
2218  * jointree scan, rather than a scan of the rtable, for a couple of
2219  * reasons: we can avoid processing no-longer-referenced RTEs, and we
2220  * can use the appropriate setting of need_phvs depending on whether
2221  * the RTE is above possibly-nulling outer joins or not.
2222  */
2223  int varno = ((RangeTblRef *) jtnode)->rtindex;
2224 
2225  if (varno != context->varno) /* ignore target subquery itself */
2226  {
2227  RangeTblEntry *rte = rt_fetch(varno, context->root->parse->rtable);
2228 
2229  Assert(rte != context->target_rte);
2230  if (rte->lateral)
2231  {
2232  switch (rte->rtekind)
2233  {
2234  case RTE_RELATION:
2235  /* shouldn't be marked LATERAL unless tablesample */
2236  Assert(rte->tablesample);
2237  rte->tablesample = (TableSampleClause *)
2239  context);
2240  break;
2241  case RTE_SUBQUERY:
2242  rte->subquery =
2244  context);
2245  break;
2246  case RTE_FUNCTION:
2247  rte->functions = (List *)
2249  context);
2250  break;
2251  case RTE_TABLEFUNC:
2252  rte->tablefunc = (TableFunc *)
2254  context);
2255  break;
2256  case RTE_VALUES:
2257  rte->values_lists = (List *)
2259  context);
2260  break;
2261  case RTE_JOIN:
2262  case RTE_CTE:
2263  case RTE_NAMEDTUPLESTORE:
2264  case RTE_RESULT:
2265  /* these shouldn't be marked LATERAL */
2266  Assert(false);
2267  break;
2268  }
2269  }
2270  }
2271  }
2272  else if (IsA(jtnode, FromExpr))
2273  {
2274  FromExpr *f = (FromExpr *) jtnode;
2275  ListCell *l;
2276 
2277  foreach(l, f->fromlist)
2278  replace_vars_in_jointree(lfirst(l), context,
2279  lowest_nulling_outer_join);
2280  f->quals = pullup_replace_vars(f->quals, context);
2281  }
2282  else if (IsA(jtnode, JoinExpr))
2283  {
2284  JoinExpr *j = (JoinExpr *) jtnode;
2285  bool save_need_phvs = context->need_phvs;
2286 
2287  if (j == lowest_nulling_outer_join)
2288  {
2289  /* no more PHVs in or below this join */
2290  context->need_phvs = false;
2291  lowest_nulling_outer_join = NULL;
2292  }
2293  replace_vars_in_jointree(j->larg, context, lowest_nulling_outer_join);
2294  replace_vars_in_jointree(j->rarg, context, lowest_nulling_outer_join);
2295 
2296  /*
2297  * Use PHVs within the join quals of a full join, even when it's the
2298  * lowest nulling outer join. Otherwise, we cannot identify which
2299  * side of the join a pulled-up var-free expression came from, which
2300  * can lead to failure to make a plan at all because none of the quals
2301  * appear to be mergeable or hashable conditions. For this purpose we
2302  * don't care about the state of wrap_non_vars, so leave it alone.
2303  */
2304  if (j->jointype == JOIN_FULL)
2305  context->need_phvs = true;
2306 
2307  j->quals = pullup_replace_vars(j->quals, context);
2308 
2309  /*
2310  * We don't bother to update the colvars list, since it won't be used
2311  * again ...
2312  */
2313  context->need_phvs = save_need_phvs;
2314  }
2315  else
2316  elog(ERROR, "unrecognized node type: %d",
2317  (int) nodeTag(jtnode));
2318 }
2319 
2320 /*
2321  * Apply pullup variable replacement throughout an expression tree
2322  *
2323  * Returns a modified copy of the tree, so this can't be used where we
2324  * need to do in-place replacement.
2325  */
2326 static Node *
2328 {
2329  return replace_rte_variables(expr,
2330  context->varno, 0,
2332  (void *) context,
2333  context->outer_hasSubLinks);
2334 }
2335 
2336 static Node *
2339 {
2341  int varattno = var->varattno;
2342  Node *newnode;
2343 
2344  /*
2345  * If PlaceHolderVars are needed, we cache the modified expressions in
2346  * rcon->rv_cache[]. This is not in hopes of any material speed gain
2347  * within this function, but to avoid generating identical PHVs with
2348  * different IDs. That would result in duplicate evaluations at runtime,
2349  * and possibly prevent optimizations that rely on recognizing different
2350  * references to the same subquery output as being equal(). So it's worth
2351  * a bit of extra effort to avoid it.
2352  */
2353  if (rcon->need_phvs &&
2354  varattno >= InvalidAttrNumber &&
2355  varattno <= list_length(rcon->targetlist) &&
2356  rcon->rv_cache[varattno] != NULL)
2357  {
2358  /* Just copy the entry and fall through to adjust its varlevelsup */
2359  newnode = copyObject(rcon->rv_cache[varattno]);
2360  }
2361  else if (varattno == InvalidAttrNumber)
2362  {
2363  /* Must expand whole-tuple reference into RowExpr */
2364  RowExpr *rowexpr;
2365  List *colnames;
2366  List *fields;
2367  bool save_need_phvs = rcon->need_phvs;
2368  int save_sublevelsup = context->sublevels_up;
2369 
2370  /*
2371  * If generating an expansion for a var of a named rowtype (ie, this
2372  * is a plain relation RTE), then we must include dummy items for
2373  * dropped columns. If the var is RECORD (ie, this is a JOIN), then
2374  * omit dropped columns. In the latter case, attach column names to
2375  * the RowExpr for use of the executor and ruleutils.c.
2376  *
2377  * In order to be able to cache the results, we always generate the
2378  * expansion with varlevelsup = 0, and then adjust if needed.
2379  */
2380  expandRTE(rcon->target_rte,
2381  var->varno, 0 /* not varlevelsup */ , var->location,
2382  (var->vartype != RECORDOID),
2383  &colnames, &fields);
2384  /* Adjust the generated per-field Vars, but don't insert PHVs */
2385  rcon->need_phvs = false;
2386  context->sublevels_up = 0; /* to match the expandRTE output */
2387  fields = (List *) replace_rte_variables_mutator((Node *) fields,
2388  context);
2389  rcon->need_phvs = save_need_phvs;
2390  context->sublevels_up = save_sublevelsup;
2391 
2392  rowexpr = makeNode(RowExpr);
2393  rowexpr->args = fields;
2394  rowexpr->row_typeid = var->vartype;
2395  rowexpr->row_format = COERCE_IMPLICIT_CAST;
2396  rowexpr->colnames = (var->vartype == RECORDOID) ? colnames : NIL;
2397  rowexpr->location = var->location;
2398  newnode = (Node *) rowexpr;
2399 
2400  /*
2401  * Insert PlaceHolderVar if needed. Notice that we are wrapping one
2402  * PlaceHolderVar around the whole RowExpr, rather than putting one
2403  * around each element of the row. This is because we need the
2404  * expression to yield NULL, not ROW(NULL,NULL,...) when it is forced
2405  * to null by an outer join.
2406  */
2407  if (rcon->need_phvs)
2408  {
2409  /* RowExpr is certainly not strict, so always need PHV */
2410  newnode = (Node *)
2412  (Expr *) newnode,
2413  bms_make_singleton(rcon->varno));
2414  /* cache it with the PHV, and with varlevelsup still zero */
2415  rcon->rv_cache[InvalidAttrNumber] = copyObject(newnode);
2416  }
2417  }
2418  else
2419  {
2420  /* Normal case referencing one targetlist element */
2421  TargetEntry *tle = get_tle_by_resno(rcon->targetlist, varattno);
2422 
2423  if (tle == NULL) /* shouldn't happen */
2424  elog(ERROR, "could not find attribute %d in subquery targetlist",
2425  varattno);
2426 
2427  /* Make a copy of the tlist item to return */
2428  newnode = (Node *) copyObject(tle->expr);
2429 
2430  /* Insert PlaceHolderVar if needed */
2431  if (rcon->need_phvs)
2432  {
2433  bool wrap;
2434 
2435  if (newnode && IsA(newnode, Var) &&
2436  ((Var *) newnode)->varlevelsup == 0)
2437  {
2438  /*
2439  * Simple Vars always escape being wrapped, unless they are
2440  * lateral references to something outside the subquery being
2441  * pulled up. (Even then, we could omit the PlaceHolderVar if
2442  * the referenced rel is under the same lowest outer join, but
2443  * it doesn't seem worth the trouble to check that.)
2444  */
2445  if (rcon->target_rte->lateral &&
2446  !bms_is_member(((Var *) newnode)->varno, rcon->relids))
2447  wrap = true;
2448  else
2449  wrap = false;
2450  }
2451  else if (newnode && IsA(newnode, PlaceHolderVar) &&
2452  ((PlaceHolderVar *) newnode)->phlevelsup == 0)
2453  {
2454  /* No need to wrap a PlaceHolderVar with another one, either */
2455  wrap = false;
2456  }
2457  else if (rcon->wrap_non_vars)
2458  {
2459  /* Wrap all non-Vars in a PlaceHolderVar */
2460  wrap = true;
2461  }
2462  else
2463  {
2464  /*
2465  * If it contains a Var of the subquery being pulled up, and
2466  * does not contain any non-strict constructs, then it's
2467  * certainly nullable so we don't need to insert a
2468  * PlaceHolderVar.
2469  *
2470  * This analysis could be tighter: in particular, a non-strict
2471  * construct hidden within a lower-level PlaceHolderVar is not
2472  * reason to add another PHV. But for now it doesn't seem
2473  * worth the code to be more exact.
2474  *
2475  * Note: in future maybe we should insert a PlaceHolderVar
2476  * anyway, if the tlist item is expensive to evaluate?
2477  *
2478  * For a LATERAL subquery, we have to check the actual var
2479  * membership of the node, but if it's non-lateral then any
2480  * level-zero var must belong to the subquery.
2481  */
2482  if ((rcon->target_rte->lateral ?
2483  bms_overlap(pull_varnos(rcon->root, (Node *) newnode),
2484  rcon->relids) :
2485  contain_vars_of_level((Node *) newnode, 0)) &&
2486  !contain_nonstrict_functions((Node *) newnode))
2487  {
2488  /* No wrap needed */
2489  wrap = false;
2490  }
2491  else
2492  {
2493  /* Else wrap it in a PlaceHolderVar */
2494  wrap = true;
2495  }
2496  }
2497 
2498  if (wrap)
2499  newnode = (Node *)
2501  (Expr *) newnode,
2502  bms_make_singleton(rcon->varno));
2503 
2504  /*
2505  * Cache it if possible (ie, if the attno is in range, which it
2506  * probably always should be). We can cache the value even if we
2507  * decided we didn't need a PHV, since this result will be
2508  * suitable for any request that has need_phvs.
2509  */
2510  if (varattno > InvalidAttrNumber &&
2511  varattno <= list_length(rcon->targetlist))
2512  rcon->rv_cache[varattno] = copyObject(newnode);
2513  }
2514  }
2515 
2516  /* Must adjust varlevelsup if tlist item is from higher query */
2517  if (var->varlevelsup > 0)
2518  IncrementVarSublevelsUp(newnode, var->varlevelsup, 0);
2519 
2520  return newnode;
2521 }
2522 
2523 /*
2524  * Apply pullup variable replacement to a subquery
2525  *
2526  * This needs to be different from pullup_replace_vars() because
2527  * replace_rte_variables will think that it shouldn't increment sublevels_up
2528  * before entering the Query; so we need to call it with sublevels_up == 1.
2529  */
2530 static Query *
2532  pullup_replace_vars_context *context)
2533 {
2534  Assert(IsA(query, Query));
2535  return (Query *) replace_rte_variables((Node *) query,
2536  context->varno, 1,
2538  (void *) context,
2539  NULL);
2540 }
2541 
2542 
2543 /*
2544  * flatten_simple_union_all
2545  * Try to optimize top-level UNION ALL structure into an appendrel
2546  *
2547  * If a query's setOperations tree consists entirely of simple UNION ALL
2548  * operations, flatten it into an append relation, which we can process more
2549  * intelligently than the general setops case. Otherwise, do nothing.
2550  *
2551  * In most cases, this can succeed only for a top-level query, because for a
2552  * subquery in FROM, the parent query's invocation of pull_up_subqueries would
2553  * already have flattened the UNION via pull_up_simple_union_all. But there
2554  * are a few cases we can support here but not in that code path, for example
2555  * when the subquery also contains ORDER BY.
2556  */
2557 void
2559 {
2560  Query *parse = root->parse;
2561  SetOperationStmt *topop;
2562  Node *leftmostjtnode;
2563  int leftmostRTI;
2564  RangeTblEntry *leftmostRTE;
2565  int childRTI;
2566  RangeTblEntry *childRTE;
2567  RangeTblRef *rtr;
2568 
2569  /* Shouldn't be called unless query has setops */
2570  topop = castNode(SetOperationStmt, parse->setOperations);
2571  Assert(topop);
2572 
2573  /* Can't optimize away a recursive UNION */
2574  if (root->hasRecursion)
2575  return;
2576 
2577  /*
2578  * Recursively check the tree of set operations. If not all UNION ALL
2579  * with identical column types, punt.
2580  */
2581  if (!is_simple_union_all_recurse((Node *) topop, parse, topop->colTypes))
2582  return;
2583 
2584  /*
2585  * Locate the leftmost leaf query in the setops tree. The upper query's
2586  * Vars all refer to this RTE (see transformSetOperationStmt).
2587  */
2588  leftmostjtnode = topop->larg;
2589  while (leftmostjtnode && IsA(leftmostjtnode, SetOperationStmt))
2590  leftmostjtnode = ((SetOperationStmt *) leftmostjtnode)->larg;
2591  Assert(leftmostjtnode && IsA(leftmostjtnode, RangeTblRef));
2592  leftmostRTI = ((RangeTblRef *) leftmostjtnode)->rtindex;
2593  leftmostRTE = rt_fetch(leftmostRTI, parse->rtable);
2594  Assert(leftmostRTE->rtekind == RTE_SUBQUERY);
2595 
2596  /*
2597  * Make a copy of the leftmost RTE and add it to the rtable. This copy
2598  * will represent the leftmost leaf query in its capacity as a member of
2599  * the appendrel. The original will represent the appendrel as a whole.
2600  * (We must do things this way because the upper query's Vars have to be
2601  * seen as referring to the whole appendrel.)
2602  */
2603  childRTE = copyObject(leftmostRTE);
2604  parse->rtable = lappend(parse->rtable, childRTE);
2605  childRTI = list_length(parse->rtable);
2606 
2607  /* Modify the setops tree to reference the child copy */
2608  ((RangeTblRef *) leftmostjtnode)->rtindex = childRTI;
2609 
2610  /* Modify the formerly-leftmost RTE to mark it as an appendrel parent */
2611  leftmostRTE->inh = true;
2612 
2613  /*
2614  * Form a RangeTblRef for the appendrel, and insert it into FROM. The top
2615  * Query of a setops tree should have had an empty FromClause initially.
2616  */
2617  rtr = makeNode(RangeTblRef);
2618  rtr->rtindex = leftmostRTI;
2619  Assert(parse->jointree->fromlist == NIL);
2620  parse->jointree->fromlist = list_make1(rtr);
2621 
2622  /*
2623  * Now pretend the query has no setops. We must do this before trying to
2624  * do subquery pullup, because of Assert in pull_up_simple_subquery.
2625  */
2626  parse->setOperations = NULL;
2627 
2628  /*
2629  * Build AppendRelInfo information, and apply pull_up_subqueries to the
2630  * leaf queries of the UNION ALL. (We must do that now because they
2631  * weren't previously referenced by the jointree, and so were missed by
2632  * the main invocation of pull_up_subqueries.)
2633  */
2634  pull_up_union_leaf_queries((Node *) topop, root, leftmostRTI, parse, 0);
2635 }
2636 
2637 
2638 /*
2639  * reduce_outer_joins
2640  * Attempt to reduce outer joins to plain inner joins.
2641  *
2642  * The idea here is that given a query like
2643  * SELECT ... FROM a LEFT JOIN b ON (...) WHERE b.y = 42;
2644  * we can reduce the LEFT JOIN to a plain JOIN if the "=" operator in WHERE
2645  * is strict. The strict operator will always return NULL, causing the outer
2646  * WHERE to fail, on any row where the LEFT JOIN filled in NULLs for b's
2647  * columns. Therefore, there's no need for the join to produce null-extended
2648  * rows in the first place --- which makes it a plain join not an outer join.
2649  * (This scenario may not be very likely in a query written out by hand, but
2650  * it's reasonably likely when pushing quals down into complex views.)
2651  *
2652  * More generally, an outer join can be reduced in strength if there is a
2653  * strict qual above it in the qual tree that constrains a Var from the
2654  * nullable side of the join to be non-null. (For FULL joins this applies
2655  * to each side separately.)
2656  *
2657  * Another transformation we apply here is to recognize cases like
2658  * SELECT ... FROM a LEFT JOIN b ON (a.x = b.y) WHERE b.y IS NULL;
2659  * If the join clause is strict for b.y, then only null-extended rows could
2660  * pass the upper WHERE, and we can conclude that what the query is really
2661  * specifying is an anti-semijoin. We change the join type from JOIN_LEFT
2662  * to JOIN_ANTI. The IS NULL clause then becomes redundant, and must be
2663  * removed to prevent bogus selectivity calculations, but we leave it to
2664  * distribute_qual_to_rels to get rid of such clauses.
2665  *
2666  * Also, we get rid of JOIN_RIGHT cases by flipping them around to become
2667  * JOIN_LEFT. This saves some code here and in some later planner routines,
2668  * but the main reason to do it is to not need to invent a JOIN_REVERSE_ANTI
2669  * join type.
2670  *
2671  * To ease recognition of strict qual clauses, we require this routine to be
2672  * run after expression preprocessing (i.e., qual canonicalization and JOIN
2673  * alias-var expansion).
2674  */
2675 void
2677 {
2679 
2680  /*
2681  * To avoid doing strictness checks on more quals than necessary, we want
2682  * to stop descending the jointree as soon as there are no outer joins
2683  * below our current point. This consideration forces a two-pass process.
2684  * The first pass gathers information about which base rels appear below
2685  * each side of each join clause, and about whether there are outer
2686  * join(s) below each side of each join clause. The second pass examines
2687  * qual clauses and changes join types as it descends the tree.
2688  */
2690 
2691  /* planner.c shouldn't have called me if no outer joins */
2692  if (state == NULL || !state->contains_outer)
2693  elog(ERROR, "so where are the outer joins?");
2694 
2696  state, root, NULL, NIL);
2697 }
2698 
2699 /*
2700  * reduce_outer_joins_pass1 - phase 1 data collection
2701  *
2702  * Returns a state node describing the given jointree node.
2703  */
2704 static reduce_outer_joins_state *
2706 {
2707  reduce_outer_joins_state *result;
2708 
2709  result = (reduce_outer_joins_state *)
2711  result->relids = NULL;
2712  result->contains_outer = false;
2713  result->sub_states = NIL;
2714 
2715  if (jtnode == NULL)
2716  return result;
2717  if (IsA(jtnode, RangeTblRef))
2718  {
2719  int varno = ((RangeTblRef *) jtnode)->rtindex;
2720 
2721  result->relids = bms_make_singleton(varno);
2722  }
2723  else if (IsA(jtnode, FromExpr))
2724  {
2725  FromExpr *f = (FromExpr *) jtnode;
2726  ListCell *l;
2727 
2728  foreach(l, f->fromlist)
2729  {
2730  reduce_outer_joins_state *sub_state;
2731 
2732  sub_state = reduce_outer_joins_pass1(lfirst(l));
2733  result->relids = bms_add_members(result->relids,
2734  sub_state->relids);
2735  result->contains_outer |= sub_state->contains_outer;
2736  result->sub_states = lappend(result->sub_states, sub_state);
2737  }
2738  }
2739  else if (IsA(jtnode, JoinExpr))
2740  {
2741  JoinExpr *j = (JoinExpr *) jtnode;
2742  reduce_outer_joins_state *sub_state;
2743 
2744  /* join's own RT index is not wanted in result->relids */
2745  if (IS_OUTER_JOIN(j->jointype))
2746  result->contains_outer = true;
2747 
2748  sub_state = reduce_outer_joins_pass1(j->larg);
2749  result->relids = bms_add_members(result->relids,
2750  sub_state->relids);
2751  result->contains_outer |= sub_state->contains_outer;
2752  result->sub_states = lappend(result->sub_states, sub_state);
2753 
2754  sub_state = reduce_outer_joins_pass1(j->rarg);
2755  result->relids = bms_add_members(result->relids,
2756  sub_state->relids);
2757  result->contains_outer |= sub_state->contains_outer;
2758  result->sub_states = lappend(result->sub_states, sub_state);
2759  }
2760  else
2761  elog(ERROR, "unrecognized node type: %d",
2762  (int) nodeTag(jtnode));
2763  return result;
2764 }
2765 
2766 /*
2767  * reduce_outer_joins_pass2 - phase 2 processing
2768  *
2769  * jtnode: current jointree node
2770  * state: state data collected by phase 1 for this node
2771  * root: toplevel planner state
2772  * nonnullable_rels: set of base relids forced non-null by upper quals
2773  * forced_null_vars: multibitmapset of Vars forced null by upper quals
2774  */
2775 static void
2778  PlannerInfo *root,
2779  Relids nonnullable_rels,
2780  List *forced_null_vars)
2781 {
2782  /*
2783  * pass 2 should never descend as far as an empty subnode or base rel,
2784  * because it's only called on subtrees marked as contains_outer.
2785  */
2786  if (jtnode == NULL)
2787  elog(ERROR, "reached empty jointree");
2788  if (IsA(jtnode, RangeTblRef))
2789  elog(ERROR, "reached base rel");
2790  else if (IsA(jtnode, FromExpr))
2791  {
2792  FromExpr *f = (FromExpr *) jtnode;
2793  ListCell *l;
2794  ListCell *s;
2795  Relids pass_nonnullable_rels;
2796  List *pass_forced_null_vars;
2797 
2798  /* Scan quals to see if we can add any constraints */
2799  pass_nonnullable_rels = find_nonnullable_rels(f->quals);
2800  pass_nonnullable_rels = bms_add_members(pass_nonnullable_rels,
2801  nonnullable_rels);
2802  pass_forced_null_vars = find_forced_null_vars(f->quals);
2803  pass_forced_null_vars = mbms_add_members(pass_forced_null_vars,
2804  forced_null_vars);
2805  /* And recurse --- but only into interesting subtrees */
2806  Assert(list_length(f->fromlist) == list_length(state->sub_states));
2807  forboth(l, f->fromlist, s, state->sub_states)
2808  {
2809  reduce_outer_joins_state *sub_state = lfirst(s);
2810 
2811  if (sub_state->contains_outer)
2812  reduce_outer_joins_pass2(lfirst(l), sub_state, root,
2813  pass_nonnullable_rels,
2814  pass_forced_null_vars);
2815  }
2816  bms_free(pass_nonnullable_rels);
2817  /* can't so easily clean up var lists, unfortunately */
2818  }
2819  else if (IsA(jtnode, JoinExpr))
2820  {
2821  JoinExpr *j = (JoinExpr *) jtnode;
2822  int rtindex = j->rtindex;
2823  JoinType jointype = j->jointype;
2824  reduce_outer_joins_state *left_state = linitial(state->sub_states);
2825  reduce_outer_joins_state *right_state = lsecond(state->sub_states);
2826 
2827  /* Can we simplify this join? */
2828  switch (jointype)
2829  {
2830  case JOIN_INNER:
2831  break;
2832  case JOIN_LEFT:
2833  if (bms_overlap(nonnullable_rels, right_state->relids))
2834  jointype = JOIN_INNER;
2835  break;
2836  case JOIN_RIGHT:
2837  if (bms_overlap(nonnullable_rels, left_state->relids))
2838  jointype = JOIN_INNER;
2839  break;
2840  case JOIN_FULL:
2841  if (bms_overlap(nonnullable_rels, left_state->relids))
2842  {
2843  if (bms_overlap(nonnullable_rels, right_state->relids))
2844  jointype = JOIN_INNER;
2845  else
2846  jointype = JOIN_LEFT;
2847  }
2848  else
2849  {
2850  if (bms_overlap(nonnullable_rels, right_state->relids))
2851  jointype = JOIN_RIGHT;
2852  }
2853  break;
2854  case JOIN_SEMI:
2855  case JOIN_ANTI:
2856 
2857  /*
2858  * These could only have been introduced by pull_up_sublinks,
2859  * so there's no way that upper quals could refer to their
2860  * righthand sides, and no point in checking.
2861  */
2862  break;
2863  default:
2864  elog(ERROR, "unrecognized join type: %d",
2865  (int) jointype);
2866  break;
2867  }
2868 
2869  /*
2870  * Convert JOIN_RIGHT to JOIN_LEFT. Note that in the case where we
2871  * reduced JOIN_FULL to JOIN_RIGHT, this will mean the JoinExpr no
2872  * longer matches the internal ordering of any CoalesceExpr's built to
2873  * represent merged join variables. We don't care about that at
2874  * present, but be wary of it ...
2875  */
2876  if (jointype == JOIN_RIGHT)
2877  {
2878  Node *tmparg;
2879 
2880  tmparg = j->larg;
2881  j->larg = j->rarg;
2882  j->rarg = tmparg;
2883  jointype = JOIN_LEFT;
2884  right_state = linitial(state->sub_states);
2885  left_state = lsecond(state->sub_states);
2886  }
2887 
2888  /*
2889  * See if we can reduce JOIN_LEFT to JOIN_ANTI. This is the case if
2890  * the join's own quals are strict for any var that was forced null by
2891  * higher qual levels. NOTE: there are other ways that we could
2892  * detect an anti-join, in particular if we were to check whether Vars
2893  * coming from the RHS must be non-null because of table constraints.
2894  * That seems complicated and expensive though (in particular, one
2895  * would have to be wary of lower outer joins). For the moment this
2896  * seems sufficient.
2897  */
2898  if (jointype == JOIN_LEFT)
2899  {
2900  List *nonnullable_vars;
2901  Bitmapset *overlap;
2902 
2903  /* Find Vars in j->quals that must be non-null in joined rows */
2904  nonnullable_vars = find_nonnullable_vars(j->quals);
2905 
2906  /*
2907  * It's not sufficient to check whether nonnullable_vars and
2908  * forced_null_vars overlap: we need to know if the overlap
2909  * includes any RHS variables.
2910  */
2911  overlap = mbms_overlap_sets(nonnullable_vars, forced_null_vars);
2912  if (bms_overlap(overlap, right_state->relids))
2913  jointype = JOIN_ANTI;
2914  }
2915 
2916  /* Apply the jointype change, if any, to both jointree node and RTE */
2917  if (rtindex && jointype != j->jointype)
2918  {
2919  RangeTblEntry *rte = rt_fetch(rtindex, root->parse->rtable);
2920 
2921  Assert(rte->rtekind == RTE_JOIN);
2922  Assert(rte->jointype == j->jointype);
2923  rte->jointype = jointype;
2924  }
2925  j->jointype = jointype;
2926 
2927  /* Only recurse if there's more to do below here */
2928  if (left_state->contains_outer || right_state->contains_outer)
2929  {
2930  Relids local_nonnullable_rels;
2931  List *local_forced_null_vars;
2932  Relids pass_nonnullable_rels;
2933  List *pass_forced_null_vars;
2934 
2935  /*
2936  * If this join is (now) inner, we can add any constraints its
2937  * quals provide to those we got from above. But if it is outer,
2938  * we can pass down the local constraints only into the nullable
2939  * side, because an outer join never eliminates any rows from its
2940  * non-nullable side. Also, there is no point in passing upper
2941  * constraints into the nullable side, since if there were any
2942  * we'd have been able to reduce the join. (In the case of upper
2943  * forced-null constraints, we *must not* pass them into the
2944  * nullable side --- they either applied here, or not.) The upshot
2945  * is that we pass either the local or the upper constraints,
2946  * never both, to the children of an outer join.
2947  *
2948  * Note that a SEMI join works like an inner join here: it's okay
2949  * to pass down both local and upper constraints. (There can't be
2950  * any upper constraints affecting its inner side, but it's not
2951  * worth having a separate code path to avoid passing them.)
2952  *
2953  * At a FULL join we just punt and pass nothing down --- is it
2954  * possible to be smarter?
2955  */
2956  if (jointype != JOIN_FULL)
2957  {
2958  local_nonnullable_rels = find_nonnullable_rels(j->quals);
2959  local_forced_null_vars = find_forced_null_vars(j->quals);
2960  if (jointype == JOIN_INNER || jointype == JOIN_SEMI)
2961  {
2962  /* OK to merge upper and local constraints */
2963  local_nonnullable_rels = bms_add_members(local_nonnullable_rels,
2964  nonnullable_rels);
2965  local_forced_null_vars = mbms_add_members(local_forced_null_vars,
2966  forced_null_vars);
2967  }
2968  }
2969  else
2970  {
2971  /* no use in calculating these */
2972  local_nonnullable_rels = NULL;
2973  local_forced_null_vars = NIL;
2974  }
2975 
2976  if (left_state->contains_outer)
2977  {
2978  if (jointype == JOIN_INNER || jointype == JOIN_SEMI)
2979  {
2980  /* pass union of local and upper constraints */
2981  pass_nonnullable_rels = local_nonnullable_rels;
2982  pass_forced_null_vars = local_forced_null_vars;
2983  }
2984  else if (jointype != JOIN_FULL) /* ie, LEFT or ANTI */
2985  {
2986  /* can't pass local constraints to non-nullable side */
2987  pass_nonnullable_rels = nonnullable_rels;
2988  pass_forced_null_vars = forced_null_vars;
2989  }
2990  else
2991  {
2992  /* no constraints pass through JOIN_FULL */
2993  pass_nonnullable_rels = NULL;
2994  pass_forced_null_vars = NIL;
2995  }
2996  reduce_outer_joins_pass2(j->larg, left_state, root,
2997  pass_nonnullable_rels,
2998  pass_forced_null_vars);
2999  }
3000 
3001  if (right_state->contains_outer)
3002  {
3003  if (jointype != JOIN_FULL) /* ie, INNER/LEFT/SEMI/ANTI */
3004  {
3005  /* pass appropriate constraints, per comment above */
3006  pass_nonnullable_rels = local_nonnullable_rels;
3007  pass_forced_null_vars = local_forced_null_vars;
3008  }
3009  else
3010  {
3011  /* no constraints pass through JOIN_FULL */
3012  pass_nonnullable_rels = NULL;
3013  pass_forced_null_vars = NIL;
3014  }
3015  reduce_outer_joins_pass2(j->rarg, right_state, root,
3016  pass_nonnullable_rels,
3017  pass_forced_null_vars);
3018  }
3019  bms_free(local_nonnullable_rels);
3020  }
3021  }
3022  else
3023  elog(ERROR, "unrecognized node type: %d",
3024  (int) nodeTag(jtnode));
3025 }
3026 
3027 
3028 /*
3029  * remove_useless_result_rtes
3030  * Attempt to remove RTE_RESULT RTEs from the join tree.
3031  *
3032  * We can remove RTE_RESULT entries from the join tree using the knowledge
3033  * that RTE_RESULT returns exactly one row and has no output columns. Hence,
3034  * if one is inner-joined to anything else, we can delete it. Optimizations
3035  * are also possible for some outer-join cases, as detailed below.
3036  *
3037  * Some of these optimizations depend on recognizing empty (constant-true)
3038  * quals for FromExprs and JoinExprs. That makes it useful to apply this
3039  * optimization pass after expression preprocessing, since that will have
3040  * eliminated constant-true quals, allowing more cases to be recognized as
3041  * optimizable. What's more, the usual reason for an RTE_RESULT to be present
3042  * is that we pulled up a subquery or VALUES clause, thus very possibly
3043  * replacing Vars with constants, making it more likely that a qual can be
3044  * reduced to constant true. Also, because some optimizations depend on
3045  * the outer-join type, it's best to have done reduce_outer_joins() first.
3046  *
3047  * A PlaceHolderVar referencing an RTE_RESULT RTE poses an obstacle to this
3048  * process: we must remove the RTE_RESULT's relid from the PHV's phrels, but
3049  * we must not reduce the phrels set to empty. If that would happen, and
3050  * the RTE_RESULT is an immediate child of an outer join, we have to give up
3051  * and not remove the RTE_RESULT: there is noplace else to evaluate the
3052  * PlaceHolderVar. (That is, in such cases the RTE_RESULT *does* have output
3053  * columns.) But if the RTE_RESULT is an immediate child of an inner join,
3054  * we can usually change the PlaceHolderVar's phrels so as to evaluate it at
3055  * the inner join instead. This is OK because we really only care that PHVs
3056  * are evaluated above or below the correct outer joins. We can't, however,
3057  * postpone the evaluation of a PHV to above where it is used; so there are
3058  * some checks below on whether output PHVs are laterally referenced in the
3059  * other join input rel(s).
3060  *
3061  * We used to try to do this work as part of pull_up_subqueries() where the
3062  * potentially-optimizable cases get introduced; but it's way simpler, and
3063  * more effective, to do it separately.
3064  */
3065 void
3067 {
3068  ListCell *cell;
3069 
3070  /* Top level of jointree must always be a FromExpr */
3071  Assert(IsA(root->parse->jointree, FromExpr));
3072  /* Recurse ... */
3073  root->parse->jointree = (FromExpr *)
3075  /* We should still have a FromExpr */
3076  Assert(IsA(root->parse->jointree, FromExpr));
3077 
3078  /*
3079  * Remove any PlanRowMark referencing an RTE_RESULT RTE. We obviously
3080  * must do that for any RTE_RESULT that we just removed. But one for a
3081  * RTE that we did not remove can be dropped anyway: since the RTE has
3082  * only one possible output row, there is no need for EPQ to mark and
3083  * restore that row.
3084  *
3085  * It's necessary, not optional, to remove the PlanRowMark for a surviving
3086  * RTE_RESULT RTE; otherwise we'll generate a whole-row Var for the
3087  * RTE_RESULT, which the executor has no support for.
3088  */
3089  foreach(cell, root->rowMarks)
3090  {
3091  PlanRowMark *rc = (PlanRowMark *) lfirst(cell);
3092 
3093  if (rt_fetch(rc->rti, root->parse->rtable)->rtekind == RTE_RESULT)
3094  root->rowMarks = foreach_delete_current(root->rowMarks, cell);
3095  }
3096 }
3097 
3098 /*
3099  * remove_useless_results_recurse
3100  * Recursive guts of remove_useless_result_rtes.
3101  *
3102  * This recursively processes the jointree and returns a modified jointree.
3103  */
3104 static Node *
3106 {
3107  Assert(jtnode != NULL);
3108  if (IsA(jtnode, RangeTblRef))
3109  {
3110  /* Can't immediately do anything with a RangeTblRef */
3111  }
3112  else if (IsA(jtnode, FromExpr))
3113  {
3114  FromExpr *f = (FromExpr *) jtnode;
3115  Relids result_relids = NULL;
3116  ListCell *cell;
3117 
3118  /*
3119  * We can drop RTE_RESULT rels from the fromlist so long as at least
3120  * one child remains, since joining to a one-row table changes
3121  * nothing. (But we can't drop a RTE_RESULT that computes PHV(s) that
3122  * are needed by some sibling. The cleanup transformation below would
3123  * reassign the PHVs to be computed at the join, which is too late for
3124  * the sibling's use.) The easiest way to mechanize this rule is to
3125  * modify the list in-place.
3126  */
3127  foreach(cell, f->fromlist)
3128  {
3129  Node *child = (Node *) lfirst(cell);
3130  int varno;
3131 
3132  /* Recursively transform child ... */
3133  child = remove_useless_results_recurse(root, child);
3134  /* ... and stick it back into the tree */
3135  lfirst(cell) = child;
3136 
3137  /*
3138  * If it's an RTE_RESULT with at least one sibling, and no sibling
3139  * references dependent PHVs, we can drop it. We don't yet know
3140  * what the inner join's final relid set will be, so postpone
3141  * cleanup of PHVs etc till after this loop.
3142  */
3143  if (list_length(f->fromlist) > 1 &&
3144  (varno = get_result_relid(root, child)) != 0 &&
3145  !find_dependent_phvs_in_jointree(root, (Node *) f, varno))
3146  {
3147  f->fromlist = foreach_delete_current(f->fromlist, cell);
3148  result_relids = bms_add_member(result_relids, varno);
3149  }
3150  }
3151 
3152  /*
3153  * Clean up if we dropped any RTE_RESULT RTEs. This is a bit
3154  * inefficient if there's more than one, but it seems better to
3155  * optimize the support code for the single-relid case.
3156  */
3157  if (result_relids)
3158  {
3159  int varno = -1;
3160 
3161  while ((varno = bms_next_member(result_relids, varno)) >= 0)
3162  remove_result_refs(root, varno, (Node *) f);
3163  }
3164 
3165  /*
3166  * If we're not at the top of the jointree, it's valid to simplify a
3167  * degenerate FromExpr into its single child. (At the top, we must
3168  * keep the FromExpr since Query.jointree is required to point to a
3169  * FromExpr.)
3170  */
3171  if (f != root->parse->jointree &&
3172  f->quals == NULL &&
3173  list_length(f->fromlist) == 1)
3174  return (Node *) linitial(f->fromlist);
3175  }
3176  else if (IsA(jtnode, JoinExpr))
3177  {
3178  JoinExpr *j = (JoinExpr *) jtnode;
3179  int varno;
3180 
3181  /* First, recurse */
3182  j->larg = remove_useless_results_recurse(root, j->larg);
3183  j->rarg = remove_useless_results_recurse(root, j->rarg);
3184 
3185  /* Apply join-type-specific optimization rules */
3186  switch (j->jointype)
3187  {
3188  case JOIN_INNER:
3189 
3190  /*
3191  * An inner join is equivalent to a FromExpr, so if either
3192  * side was simplified to an RTE_RESULT rel, we can replace
3193  * the join with a FromExpr with just the other side; and if
3194  * the qual is empty (JOIN ON TRUE) then we can omit the
3195  * FromExpr as well.
3196  *
3197  * Just as in the FromExpr case, we can't simplify if the
3198  * other input rel references any PHVs that are marked as to
3199  * be evaluated at the RTE_RESULT rel, because we can't
3200  * postpone their evaluation in that case. But we only have
3201  * to check this in cases where it's syntactically legal for
3202  * the other input to have a LATERAL reference to the
3203  * RTE_RESULT rel. Only RHSes of inner and left joins are
3204  * allowed to have such refs.
3205  */
3206  if ((varno = get_result_relid(root, j->larg)) != 0 &&
3207  !find_dependent_phvs_in_jointree(root, j->rarg, varno))
3208  {
3209  remove_result_refs(root, varno, j->rarg);
3210  if (j->quals)
3211  jtnode = (Node *)
3212  makeFromExpr(list_make1(j->rarg), j->quals);
3213  else
3214  jtnode = j->rarg;
3215  }
3216  else if ((varno = get_result_relid(root, j->rarg)) != 0)
3217  {
3218  remove_result_refs(root, varno, j->larg);
3219  if (j->quals)
3220  jtnode = (Node *)
3221  makeFromExpr(list_make1(j->larg), j->quals);
3222  else
3223  jtnode = j->larg;
3224  }
3225  break;
3226  case JOIN_LEFT:
3227 
3228  /*
3229  * We can simplify this case if the RHS is an RTE_RESULT, with
3230  * two different possibilities:
3231  *
3232  * If the qual is empty (JOIN ON TRUE), then the join can be
3233  * strength-reduced to a plain inner join, since each LHS row
3234  * necessarily has exactly one join partner. So we can always
3235  * discard the RHS, much as in the JOIN_INNER case above.
3236  * (Again, the LHS could not contain a lateral reference to
3237  * the RHS.)
3238  *
3239  * Otherwise, it's still true that each LHS row should be
3240  * returned exactly once, and since the RHS returns no columns
3241  * (unless there are PHVs that have to be evaluated there), we
3242  * don't much care if it's null-extended or not. So in this
3243  * case also, we can just ignore the qual and discard the left
3244  * join.
3245  */
3246  if ((varno = get_result_relid(root, j->rarg)) != 0 &&
3247  (j->quals == NULL ||
3248  !find_dependent_phvs(root, varno)))
3249  {
3250  remove_result_refs(root, varno, j->larg);
3251  jtnode = j->larg;
3252  }
3253  break;
3254  case JOIN_SEMI:
3255 
3256  /*
3257  * We may simplify this case if the RHS is an RTE_RESULT; the
3258  * join qual becomes effectively just a filter qual for the
3259  * LHS, since we should either return the LHS row or not. For
3260  * simplicity we inject the filter qual into a new FromExpr.
3261  *
3262  * There is a fine point about PHVs that are supposed to be
3263  * evaluated at the RHS. Such PHVs could only appear in the
3264  * semijoin's qual, since the rest of the query cannot
3265  * reference any outputs of the semijoin's RHS. Therefore,
3266  * they can't actually go to null before being examined, and
3267  * it'd be OK to just remove the PHV wrapping. We don't have
3268  * infrastructure for that, but remove_result_refs() will
3269  * relabel them as to be evaluated at the LHS, which is fine.
3270  */
3271  if ((varno = get_result_relid(root, j->rarg)) != 0)
3272  {
3273  remove_result_refs(root, varno, j->larg);
3274  if (j->quals)
3275  jtnode = (Node *)
3276  makeFromExpr(list_make1(j->larg), j->quals);
3277  else
3278  jtnode = j->larg;
3279  }
3280  break;
3281  case JOIN_FULL:
3282  case JOIN_ANTI:
3283  /* We have no special smarts for these cases */
3284  break;
3285  default:
3286  /* Note: JOIN_RIGHT should be gone at this point */
3287  elog(ERROR, "unrecognized join type: %d",
3288  (int) j->jointype);
3289  break;
3290  }
3291  }
3292  else
3293  elog(ERROR, "unrecognized node type: %d",
3294  (int) nodeTag(jtnode));
3295  return jtnode;
3296 }
3297 
3298 /*
3299  * get_result_relid
3300  * If jtnode is a RangeTblRef for an RTE_RESULT RTE, return its relid;
3301  * otherwise return 0.
3302  */
3303 static int
3305 {
3306  int varno;
3307 
3308  if (!IsA(jtnode, RangeTblRef))
3309  return 0;
3310  varno = ((RangeTblRef *) jtnode)->rtindex;
3311  if (rt_fetch(varno, root->parse->rtable)->rtekind != RTE_RESULT)
3312  return 0;
3313  return varno;
3314 }
3315 
3316 /*
3317  * remove_result_refs
3318  * Helper routine for dropping an unneeded RTE_RESULT RTE.
3319  *
3320  * This doesn't physically remove the RTE from the jointree, because that's
3321  * more easily handled in remove_useless_results_recurse. What it does do
3322  * is the necessary cleanup in the rest of the tree: we must adjust any PHVs
3323  * that may reference the RTE. Be sure to call this at a point where the
3324  * jointree is valid (no disconnected nodes).
3325  *
3326  * Note that we don't need to process the append_rel_list, since RTEs
3327  * referenced directly in the jointree won't be appendrel members.
3328  *
3329  * varno is the RTE_RESULT's relid.
3330  * newjtloc is the jointree location at which any PHVs referencing the
3331  * RTE_RESULT should be evaluated instead.
3332  */
3333 static void
3334 remove_result_refs(PlannerInfo *root, int varno, Node *newjtloc)
3335 {
3336  /* Fix up PlaceHolderVars as needed */
3337  /* If there are no PHVs anywhere, we can skip this bit */
3338  if (root->glob->lastPHId != 0)
3339  {
3340  Relids subrelids;
3341 
3342  subrelids = get_relids_in_jointree(newjtloc, false);
3343  Assert(!bms_is_empty(subrelids));
3344  substitute_phv_relids((Node *) root->parse, varno, subrelids);
3345  fix_append_rel_relids(root->append_rel_list, varno, subrelids);
3346  }
3347 
3348  /*
3349  * We also need to remove any PlanRowMark referencing the RTE, but we
3350  * postpone that work until we return to remove_useless_result_rtes.
3351  */
3352 }
3353 
3354 
3355 /*
3356  * find_dependent_phvs - are there any PlaceHolderVars whose relids are
3357  * exactly the given varno?
3358  *
3359  * find_dependent_phvs should be used when we want to see if there are
3360  * any such PHVs anywhere in the Query. Another use-case is to see if
3361  * a subtree of the join tree contains such PHVs; but for that, we have
3362  * to look not only at the join tree nodes themselves but at the
3363  * referenced RTEs. For that, use find_dependent_phvs_in_jointree.
3364  */
3365 
3366 typedef struct
3367 {
3371 
3372 static bool
3374  find_dependent_phvs_context *context)
3375 {
3376  if (node == NULL)
3377  return false;
3378  if (IsA(node, PlaceHolderVar))
3379  {
3380  PlaceHolderVar *phv = (PlaceHolderVar *) node;
3381 
3382  if (phv->phlevelsup == context->sublevels_up &&
3383  bms_equal(context->relids, phv->phrels))
3384  return true;
3385  /* fall through to examine children */
3386  }
3387  if (IsA(node, Query))
3388  {
3389  /* Recurse into subselects */
3390  bool result;
3391 
3392  context->sublevels_up++;
3393  result = query_tree_walker((Query *) node,
3395  (void *) context, 0);
3396  context->sublevels_up--;
3397  return result;
3398  }
3399  /* Shouldn't need to handle planner auxiliary nodes here */
3400  Assert(!IsA(node, SpecialJoinInfo));
3401  Assert(!IsA(node, AppendRelInfo));
3402  Assert(!IsA(node, PlaceHolderInfo));
3403  Assert(!IsA(node, MinMaxAggInfo));
3404 
3406  (void *) context);
3407 }
3408 
3409 static bool
3411 {
3413 
3414  /* If there are no PHVs anywhere, we needn't work hard */
3415  if (root->glob->lastPHId == 0)
3416  return false;
3417 
3418  context.relids = bms_make_singleton(varno);
3419  context.sublevels_up = 0;
3420 
3421  return query_tree_walker(root->parse,
3423  (void *) &context,
3424  0);
3425 }
3426 
3427 static bool
3429 {
3431  Relids subrelids;
3432  int relid;
3433 
3434  /* If there are no PHVs anywhere, we needn't work hard */
3435  if (root->glob->lastPHId == 0)
3436  return false;
3437 
3438  context.relids = bms_make_singleton(varno);
3439  context.sublevels_up = 0;
3440 
3441  /*
3442  * See if the jointree fragment itself contains references (in join quals)
3443  */
3444  if (find_dependent_phvs_walker(node, &context))
3445  return true;
3446 
3447  /*
3448  * Otherwise, identify the set of referenced RTEs (we can ignore joins,
3449  * since they should be flattened already, so their join alias lists no
3450  * longer matter), and tediously check each RTE. We can ignore RTEs that
3451  * are not marked LATERAL, though, since they couldn't possibly contain
3452  * any cross-references to other RTEs.
3453  */
3454  subrelids = get_relids_in_jointree(node, false);
3455  relid = -1;
3456  while ((relid = bms_next_member(subrelids, relid)) >= 0)
3457  {
3458  RangeTblEntry *rte = rt_fetch(relid, root->parse->rtable);
3459 
3460  if (rte->lateral &&
3463  (void *) &context,
3464  0))
3465  return true;
3466  }
3467 
3468  return false;
3469 }
3470 
3471 /*
3472  * substitute_phv_relids - adjust PlaceHolderVar relid sets after pulling up
3473  * a subquery or removing an RTE_RESULT jointree item
3474  *
3475  * Find any PlaceHolderVar nodes in the given tree that reference the
3476  * pulled-up relid, and change them to reference the replacement relid(s).
3477  *
3478  * NOTE: although this has the form of a walker, we cheat and modify the
3479  * nodes in-place. This should be OK since the tree was copied by
3480  * pullup_replace_vars earlier. Avoid scribbling on the original values of
3481  * the bitmapsets, though, because expression_tree_mutator doesn't copy those.
3482  */
3483 
3484 typedef struct
3485 {
3486  int varno;
3490 
3491 static bool
3494 {
3495  if (node == NULL)
3496  return false;
3497  if (IsA(node, PlaceHolderVar))
3498  {
3499  PlaceHolderVar *phv = (PlaceHolderVar *) node;
3500 
3501  if (phv->phlevelsup == context->sublevels_up &&
3502  bms_is_member(context->varno, phv->phrels))
3503  {
3504  phv->phrels = bms_union(phv->phrels,
3505  context->subrelids);
3506  phv->phrels = bms_del_member(phv->phrels,
3507  context->varno);
3508  /* Assert we haven't broken the PHV */
3509  Assert(!bms_is_empty(phv->phrels));
3510  }
3511  /* fall through to examine children */
3512  }
3513  if (IsA(node, Query))
3514  {
3515  /* Recurse into subselects */
3516  bool result;
3517 
3518  context->sublevels_up++;
3519  result = query_tree_walker((Query *) node,
3521  (void *) context, 0);
3522  context->sublevels_up--;
3523  return result;
3524  }
3525  /* Shouldn't need to handle planner auxiliary nodes here */
3526  Assert(!IsA(node, SpecialJoinInfo));
3527  Assert(!IsA(node, AppendRelInfo));
3528  Assert(!IsA(node, PlaceHolderInfo));
3529  Assert(!IsA(node, MinMaxAggInfo));
3530 
3532  (void *) context);
3533 }
3534 
3535 static void
3536 substitute_phv_relids(Node *node, int varno, Relids subrelids)
3537 {
3539 
3540  context.varno = varno;
3541  context.sublevels_up = 0;
3542  context.subrelids = subrelids;
3543 
3544  /*
3545  * Must be prepared to start with a Query or a bare expression tree.
3546  */
3549  (void *) &context,
3550  0);
3551 }
3552 
3553 /*
3554  * fix_append_rel_relids: update RT-index fields of AppendRelInfo nodes
3555  *
3556  * When we pull up a subquery, any AppendRelInfo references to the subquery's
3557  * RT index have to be replaced by the substituted relid (and there had better
3558  * be only one). We also need to apply substitute_phv_relids to their
3559  * translated_vars lists, since those might contain PlaceHolderVars.
3560  *
3561  * We assume we may modify the AppendRelInfo nodes in-place.
3562  */
3563 static void
3564 fix_append_rel_relids(List *append_rel_list, int varno, Relids subrelids)
3565 {
3566  ListCell *l;
3567  int subvarno = -1;
3568 
3569  /*
3570  * We only want to extract the member relid once, but we mustn't fail
3571  * immediately if there are multiple members; it could be that none of the
3572  * AppendRelInfo nodes refer to it. So compute it on first use. Note that
3573  * bms_singleton_member will complain if set is not singleton.
3574  */
3575  foreach(l, append_rel_list)
3576  {
3577  AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
3578 
3579  /* The parent_relid shouldn't ever be a pullup target */
3580  Assert(appinfo->parent_relid != varno);
3581 
3582  if (appinfo->child_relid == varno)
3583  {
3584  if (subvarno < 0)
3585  subvarno = bms_singleton_member(subrelids);
3586  appinfo->child_relid = subvarno;
3587  }
3588 
3589  /* Also fix up any PHVs in its translated vars */
3591  varno, subrelids);
3592  }
3593 }
3594 
3595 /*
3596  * get_relids_in_jointree: get set of RT indexes present in a jointree
3597  *
3598  * If include_joins is true, join RT indexes are included; if false,
3599  * only base rels are included.
3600  */
3601 Relids
3602 get_relids_in_jointree(Node *jtnode, bool include_joins)
3603 {
3604  Relids result = NULL;
3605 
3606  if (jtnode == NULL)
3607  return result;
3608  if (IsA(jtnode, RangeTblRef))
3609  {
3610  int varno = ((RangeTblRef *) jtnode)->rtindex;
3611 
3612  result = bms_make_singleton(varno);
3613  }
3614  else if (IsA(jtnode, FromExpr))
3615  {
3616  FromExpr *f = (FromExpr *) jtnode;
3617  ListCell *l;
3618 
3619  foreach(l, f->fromlist)
3620  {
3621  result = bms_join(result,
3623  include_joins));
3624  }
3625  }
3626  else if (IsA(jtnode, JoinExpr))
3627  {
3628  JoinExpr *j = (JoinExpr *) jtnode;
3629 
3630  result = get_relids_in_jointree(j->larg, include_joins);
3631  result = bms_join(result,
3632  get_relids_in_jointree(j->rarg, include_joins));
3633  if (include_joins && j->rtindex)
3634  result = bms_add_member(result, j->rtindex);
3635  }
3636  else
3637  elog(ERROR, "unrecognized node type: %d",
3638  (int) nodeTag(jtnode));
3639  return result;
3640 }
3641 
3642 /*
3643  * get_relids_for_join: get set of base RT indexes making up a join
3644  */
3645 Relids
3646 get_relids_for_join(Query *query, int joinrelid)
3647 {
3648  Node *jtnode;
3649 
3650  jtnode = find_jointree_node_for_rel((Node *) query->jointree,
3651  joinrelid);
3652  if (!jtnode)
3653  elog(ERROR, "could not find join node %d", joinrelid);
3654  return get_relids_in_jointree(jtnode, false);
3655 }
3656 
3657 /*
3658  * find_jointree_node_for_rel: locate jointree node for a base or join RT index
3659  *
3660  * Returns NULL if not found
3661  */
3662 static Node *
3664 {
3665  if (jtnode == NULL)
3666  return NULL;
3667  if (IsA(jtnode, RangeTblRef))
3668  {
3669  int varno = ((RangeTblRef *) jtnode)->rtindex;
3670 
3671  if (relid == varno)
3672  return jtnode;
3673  }
3674  else if (IsA(jtnode, FromExpr))
3675  {
3676  FromExpr *f = (FromExpr *) jtnode;
3677  ListCell *l;
3678 
3679  foreach(l, f->fromlist)
3680  {
3681  jtnode = find_jointree_node_for_rel(lfirst(l), relid);
3682  if (jtnode)
3683  return jtnode;
3684  }
3685  }
3686  else if (IsA(jtnode, JoinExpr))
3687  {
3688  JoinExpr *j = (JoinExpr *) jtnode;
3689 
3690  if (relid == j->rtindex)
3691  return jtnode;
3692  jtnode = find_jointree_node_for_rel(j->larg, relid);
3693  if (jtnode)
3694  return jtnode;
3695  jtnode = find_jointree_node_for_rel(j->rarg, relid);
3696  if (jtnode)
3697  return jtnode;
3698  }
3699  else
3700  elog(ERROR, "unrecognized node type: %d",
3701  (int) nodeTag(jtnode));
3702  return NULL;
3703 }
int16 AttrNumber
Definition: attnum.h:21
#define InvalidAttrNumber
Definition: attnum.h:23
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:953
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1047
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:316
int bms_singleton_member(const Bitmapset *a)
Definition: bitmapset.c:580
void bms_free(Bitmapset *a)
Definition: bitmapset.c:209
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:428
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:186
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:739
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:226
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:796
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:776
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:704
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:495
unsigned int Index
Definition: c.h:550
List * find_forced_null_vars(Node *node)
Definition: clauses.c:1796
Query * inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte)
Definition: clauses.c:4899
Relids find_nonnullable_rels(Node *clause)
Definition: clauses.c:1336
List * find_nonnullable_vars(Node *clause)
Definition: clauses.c:1587
bool contain_nonstrict_functions(Node *clause)
Definition: clauses.c:874
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition: clauses.c:2132
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:448
#define ERROR
Definition: elog.h:35
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:292
TypeFuncClass
Definition: funcapi.h:147
@ TYPEFUNC_SCALAR
Definition: funcapi.h:148
int j
Definition: isn.c:74
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
Definition: list.c:338
List * list_concat(List *list1, const List *list2)
Definition: list.c:560
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:387
Var * makeVarFromTargetEntry(int varno, TargetEntry *tle)
Definition: makefuncs.c:103
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:238
Expr * make_andclause(List *andclauses)
Definition: makefuncs.c:636
FromExpr * makeFromExpr(List *fromlist, Node *quals)
Definition: makefuncs.c:285
void * palloc0(Size size)
Definition: mcxt.c:1230
MemoryContext CurrentMemoryContext
Definition: mcxt.c:124
void * palloc(Size size)
Definition: mcxt.c:1199
Bitmapset * mbms_overlap_sets(const List *a, const List *b)
List * mbms_add_members(List *a, const List *b)
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:706
static bool is_andclause(const void *clause)
Definition: nodeFuncs.h:105
static Expr * get_notclausearg(const void *notclause)
Definition: nodeFuncs.h:132
#define query_tree_walker(q, w, c, f)
Definition: nodeFuncs.h:156
#define query_or_expression_tree_walker(n, w, c, f)
Definition: nodeFuncs.h:169
#define range_table_entry_walker(r, w, c, f)
Definition: nodeFuncs.h:166
#define expression_tree_walker(n, w, c)
Definition: nodeFuncs.h:151
static bool is_notclause(const void *clause)
Definition: nodeFuncs.h:123
#define IsA(nodeptr, _type_)
Definition: nodes.h:168
#define copyObject(obj)
Definition: nodes.h:233
#define nodeTag(nodeptr)
Definition: nodes.h:122
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:336
@ CMD_MERGE
Definition: nodes.h:269
@ CMD_SELECT
Definition: nodes.h:265
#define makeNode(_type_)
Definition: nodes.h:165
#define castNode(_type_, nodeptr)
Definition: nodes.h:186
JoinType
Definition: nodes.h:288
@ JOIN_SEMI
Definition: nodes.h:307
@ JOIN_FULL
Definition: nodes.h:295
@ JOIN_INNER
Definition: nodes.h:293
@ JOIN_RIGHT
Definition: nodes.h:296
@ JOIN_LEFT
Definition: nodes.h:294
@ JOIN_ANTI
Definition: nodes.h:308
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
@ SETOP_UNION
Definition: parsenodes.h:1719
@ RTE_JOIN
Definition: parsenodes.h:1013
@ RTE_CTE
Definition: parsenodes.h:1017
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1018
@ RTE_VALUES
Definition: parsenodes.h:1016
@ RTE_SUBQUERY
Definition: parsenodes.h:1012
@ RTE_RESULT
Definition: parsenodes.h:1019
@ RTE_FUNCTION
Definition: parsenodes.h:1014
@ RTE_TABLEFUNC
Definition: parsenodes.h:1015
@ RTE_RELATION
Definition: parsenodes.h:1011
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
#define lfirst(lc)
Definition: pg_list.h:170
static int list_length(const List *l)
Definition: pg_list.h:150
#define linitial_node(type, l)
Definition: pg_list.h:179
#define NIL
Definition: pg_list.h:66
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:465
#define list_make1(x1)
Definition: pg_list.h:210
#define linitial(l)
Definition: pg_list.h:176
#define lsecond(l)
Definition: pg_list.h:181
#define foreach_delete_current(lst, cell)
Definition: pg_list.h:388
PlaceHolderVar * make_placeholder_expr(PlannerInfo *root, Expr *expr, Relids phrels)
Definition: placeholder.c:39
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
static void remove_result_refs(PlannerInfo *root, int varno, Node *newjtloc)
void preprocess_function_rtes(PlannerInfo *root)
Definition: prepjointree.c:719
static bool find_dependent_phvs_walker(Node *node, find_dependent_phvs_context *context)
static Node * find_jointree_node_for_rel(Node *jtnode, int relid)
static Node * pull_up_simple_union_all(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte)
static Node * pull_up_subqueries_recurse(PlannerInfo *root, Node *jtnode, JoinExpr *lowest_outer_join, JoinExpr *lowest_nulling_outer_join, AppendRelInfo *containing_appendrel)
Definition: prepjointree.c:810
static void pull_up_union_leaf_queries(Node *setOp, PlannerInfo *root, int parentRTindex, Query *setOpQuery, int childRToffset)
static bool is_simple_values(PlannerInfo *root, RangeTblEntry *rte)
static void make_setop_translation_list(Query *query, int newvarno, AppendRelInfo *appinfo)
void flatten_simple_union_all(PlannerInfo *root)
void transform_MERGE_to_join(Query *parse)
Definition: prepjointree.c:140
static Node * remove_useless_results_recurse(PlannerInfo *root, Node *jtnode)
static void perform_pullup_replace_vars(PlannerInfo *root, pullup_replace_vars_context *rvcontext, JoinExpr *lowest_nulling_outer_join, AppendRelInfo *containing_appendrel)
void remove_useless_result_rtes(PlannerInfo *root)
static Node * pull_up_sublinks_jointree_recurse(PlannerInfo *root, Node *jtnode, Relids *relids)
Definition: prepjointree.c:315
static bool is_simple_subquery(PlannerInfo *root, Query *subquery, RangeTblEntry *rte, JoinExpr *lowest_outer_join)
static Node * pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte, JoinExpr *lowest_outer_join, JoinExpr *lowest_nulling_outer_join, AppendRelInfo *containing_appendrel)
Definition: prepjointree.c:962
static void substitute_phv_relids(Node *node, int varno, Relids subrelids)
void pull_up_sublinks(PlannerInfo *root)
Definition: prepjointree.c:288
static Node * pullup_replace_vars_callback(Var *var, replace_rte_variables_context *context)
void replace_empty_jointree(Query *parse)
Definition: prepjointree.c:230
static bool is_simple_union_all_recurse(Node *setOp, Query *setOpQuery, List *colTypes)
static Node * pull_up_constant_function(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte, JoinExpr *lowest_nulling_outer_join, AppendRelInfo *containing_appendrel)
static bool substitute_phv_relids_walker(Node *node, substitute_phv_relids_context *context)
static Query * pullup_replace_vars_subquery(Query *query, pullup_replace_vars_context *context)
Relids get_relids_for_join(Query *query, int joinrelid)
void pull_up_subqueries(PlannerInfo *root)
Definition: prepjointree.c:760
static int get_result_relid(PlannerInfo *root, Node *jtnode)
static void fix_append_rel_relids(List *append_rel_list, int varno, Relids subrelids)
static Node * pull_up_simple_values(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte)
static bool is_safe_append_member(Query *subquery)
struct pullup_replace_vars_context pullup_replace_vars_context
static void replace_vars_in_jointree(Node *jtnode, pullup_replace_vars_context *context, JoinExpr *lowest_nulling_outer_join)
static void reduce_outer_joins_pass2(Node *jtnode, reduce_outer_joins_state *state, PlannerInfo *root, Relids nonnullable_rels, List *forced_null_vars)
static Node * pull_up_sublinks_qual_recurse(PlannerInfo *root, Node *node, Node **jtlink1, Relids available_rels1, Node **jtlink2, Relids available_rels2)
Definition: prepjointree.c:469
void reduce_outer_joins(PlannerInfo *root)
static bool find_dependent_phvs(PlannerInfo *root, int varno)
static bool jointree_contains_lateral_outer_refs(PlannerInfo *root, Node *jtnode, bool restricted, Relids safe_upper_varnos)
Relids get_relids_in_jointree(Node *jtnode, bool include_joins)
struct reduce_outer_joins_state reduce_outer_joins_state
static Node * pullup_replace_vars(Node *expr, pullup_replace_vars_context *context)
static reduce_outer_joins_state * reduce_outer_joins_pass1(Node *jtnode)
static bool is_simple_union_all(Query *subquery)
static bool find_dependent_phvs_in_jointree(PlannerInfo *root, Node *node, int varno)
@ ANY_SUBLINK
Definition: primnodes.h:826
@ EXISTS_SUBLINK
Definition: primnodes.h:824
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:588
static struct subre * parse(struct vars *v, int stopper, int type, struct state *init, struct state *final)
Definition: regcomp.c:717
void IncrementVarSublevelsUp_rtable(List *rtable, int delta_sublevels_up, int min_sublevels_up)
Definition: rewriteManip.c:799
Node * replace_rte_variables_mutator(Node *node, replace_rte_variables_context *context)
void OffsetVarNodes(Node *node, int offset, int sublevels_up)
Definition: rewriteManip.c:425
Node * replace_rte_variables(Node *node, int target_varno, int sublevels_up, replace_rte_variables_callback callback, void *callback_arg, bool *outer_hasSubLinks)
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
Definition: rewriteManip.c:776
Index child_relid
Definition: pathnodes.h:2755
List * translated_vars
Definition: pathnodes.h:2782
Index parent_relid
Definition: pathnodes.h:2754
int num_child_cols
Definition: pathnodes.h:2790
Oid parent_reltype
Definition: pathnodes.h:2763
Node * quals
Definition: primnodes.h:1665
List * fromlist
Definition: primnodes.h:1664
Node * quals
Definition: primnodes.h:1647
JoinType jointype
Definition: primnodes.h:1641
Alias * alias
Definition: primnodes.h:1648
int rtindex
Definition: primnodes.h:1649
List * usingClause
Definition: primnodes.h:1645
Alias * join_using_alias
Definition: primnodes.h:1646
Node * larg
Definition: primnodes.h:1643
bool isNatural
Definition: primnodes.h:1642
Node * rarg
Definition: primnodes.h:1644
Definition: pg_list.h:52
Definition: nodes.h:118
Index phlevelsup
Definition: pathnodes.h:2630
Index lastPHId
Definition: pathnodes.h:138
List * minmax_aggs
Definition: pathnodes.h:431
List * processed_tlist
Definition: pathnodes.h:415
bool hasRecursion
Definition: pathnodes.h:463
List * cte_plan_ids
Definition: pathnodes.h:298
Index qual_security_level
Definition: pathnodes.h:448
List * init_plans
Definition: pathnodes.h:292
List * multiexpr_params
Definition: pathnodes.h:301
List * row_identity_vars
Definition: pathnodes.h:355
bool ec_merging_done
Definition: pathnodes.h:307
Bitmapset * outer_params
Definition: pathnodes.h:218
Index query_level
Definition: pathnodes.h:205
List * append_rel_list
Definition: pathnodes.h:352
struct Path * non_recursive_path
Definition: pathnodes.h:485
List * placeholder_list
Definition: pathnodes.h:361
PlannerGlobal * glob
Definition: pathnodes.h:202
List * eq_classes
Definition: pathnodes.h:304
int wt_param_id
Definition: pathnodes.h:483
List * plan_params
Definition: pathnodes.h:217
Query * parse
Definition: pathnodes.h:199
List * rowMarks
Definition: pathnodes.h:358
List * update_colnos
Definition: pathnodes.h:423
bool placeholdersFrozen
Definition: pathnodes.h:461
List * join_info_list
Definition: pathnodes.h:330
Relids all_result_relids
Definition: pathnodes.h:341
Relids leaf_result_relids
Definition: pathnodes.h:343
List * rowMarks
Definition: parsenodes.h:187
bool hasWindowFuncs
Definition: parsenodes.h:142
Node * limitCount
Definition: parsenodes.h:184
FromExpr * jointree
Definition: parsenodes.h:156
bool hasRowSecurity
Definition: parsenodes.h:149
Node * setOperations
Definition: parsenodes.h:189
List * cteList
Definition: parsenodes.h:153
bool hasTargetSRFs
Definition: parsenodes.h:143
List * groupClause
Definition: parsenodes.h:170
Node * havingQual
Definition: parsenodes.h:175
List * rtable
Definition: parsenodes.h:155
Node * limitOffset
Definition: parsenodes.h:183
bool hasAggs
Definition: parsenodes.h:141
CmdType commandType
Definition: parsenodes.h:124
List * targetList
Definition: parsenodes.h:162
bool hasForUpdate
Definition: parsenodes.h:148
bool hasSubLinks
Definition: parsenodes.h:144
List * groupingSets
Definition: parsenodes.h:173
List * distinctClause
Definition: parsenodes.h:179
List * sortClause
Definition: parsenodes.h:181
TableFunc * tablefunc
Definition: parsenodes.h:1130
Bitmapset * extraUpdatedCols
Definition: parsenodes.h:1185
bool security_barrier
Definition: parsenodes.h:1066
AclMode requiredPerms
Definition: parsenodes.h:1180
bool funcordinality
Definition: parsenodes.h:1125
Alias * join_using_alias
Definition: parsenodes.h:1114
struct TableSampleClause * tablesample
Definition: parsenodes.h:1060
List * securityQuals
Definition: parsenodes.h:1186
Bitmapset * updatedCols
Definition: parsenodes.h:1184
Query * subquery
Definition: parsenodes.h:1065
Bitmapset * selectedCols
Definition: parsenodes.h:1182
Alias * eref
Definition: parsenodes.h:1176
List * joinrightcols
Definition: parsenodes.h:1107
List * values_lists
Definition: parsenodes.h:1135
List * joinaliasvars
Definition: parsenodes.h:1105
JoinType jointype
Definition: parsenodes.h:1103
Alias * alias
Definition: parsenodes.h:1175
List * functions
Definition: parsenodes.h:1124
Bitmapset * insertedCols
Definition: parsenodes.h:1183
List * joinleftcols
Definition: parsenodes.h:1106
RTEKind rtekind
Definition: parsenodes.h:1030
Oid row_typeid
Definition: primnodes.h:1208
int location
Definition: primnodes.h:1224
List * args
Definition: primnodes.h:1207
CoercionForm row_format
Definition: primnodes.h:1222
List * colnames
Definition: primnodes.h:1223
SetOperation op
Definition: parsenodes.h:1796
Expr * expr
Definition: primnodes.h:1555
AttrNumber resno
Definition: primnodes.h:1556
bool resjunk
Definition: primnodes.h:1562
Definition: primnodes.h:205
Oid vartype
Definition: primnodes.h:220
AttrNumber varattno
Definition: primnodes.h:217
int varno
Definition: primnodes.h:212
Index varlevelsup
Definition: primnodes.h:230
int location
Definition: primnodes.h:243
RangeTblEntry * target_rte
Definition: prepjointree.c:48
Definition: regguts.h:318
Definition: regcomp.c:282
JoinExpr * convert_ANY_sublink_to_join(PlannerInfo *root, SubLink *sublink, Relids available_rels)
Definition: subselect.c:1268
JoinExpr * convert_EXISTS_sublink_to_join(PlannerInfo *root, SubLink *sublink, bool under_not, Relids available_rels)
Definition: subselect.c:1376
bool tlist_same_datatypes(List *tlist, List *colTypes, bool junkOK)
Definition: tlist.c:248
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:431
Relids pull_varnos_of_level(PlannerInfo *root, Node *node, int levelsup)
Definition: var.c:126
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition: var.c:100
Node * flatten_join_alias_vars(Query *query, Node *node)
Definition: var.c:726