Line data Source code
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 : * expand_virtual_generated_columns
11 : * pull_up_subqueries
12 : * flatten_simple_union_all
13 : * do expression preprocessing (including flattening JOIN alias vars)
14 : * reduce_outer_joins
15 : * remove_useless_result_rtes
16 : *
17 : *
18 : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
19 : * Portions Copyright (c) 1994, Regents of the University of California
20 : *
21 : *
22 : * IDENTIFICATION
23 : * src/backend/optimizer/prep/prepjointree.c
24 : *
25 : *-------------------------------------------------------------------------
26 : */
27 : #include "postgres.h"
28 :
29 : #include "access/table.h"
30 : #include "catalog/pg_type.h"
31 : #include "funcapi.h"
32 : #include "miscadmin.h"
33 : #include "nodes/makefuncs.h"
34 : #include "nodes/multibitmapset.h"
35 : #include "nodes/nodeFuncs.h"
36 : #include "optimizer/clauses.h"
37 : #include "optimizer/optimizer.h"
38 : #include "optimizer/placeholder.h"
39 : #include "optimizer/prep.h"
40 : #include "optimizer/subselect.h"
41 : #include "optimizer/tlist.h"
42 : #include "parser/parse_relation.h"
43 : #include "parser/parsetree.h"
44 : #include "rewrite/rewriteHandler.h"
45 : #include "rewrite/rewriteManip.h"
46 : #include "utils/rel.h"
47 :
48 :
49 : typedef struct nullingrel_info
50 : {
51 : /*
52 : * For each leaf RTE, nullingrels[rti] is the set of relids of outer joins
53 : * that potentially null that RTE.
54 : */
55 : Relids *nullingrels;
56 : /* Length of range table (maximum index in nullingrels[]) */
57 : int rtlength; /* used only for assertion checks */
58 : } nullingrel_info;
59 :
60 : /* Options for wrapping an expression for identification purposes */
61 : typedef enum ReplaceWrapOption
62 : {
63 : REPLACE_WRAP_NONE, /* no expressions need to be wrapped */
64 : REPLACE_WRAP_ALL, /* all expressions need to be wrapped */
65 : REPLACE_WRAP_VARFREE, /* variable-free expressions need to be
66 : * wrapped */
67 : } ReplaceWrapOption;
68 :
69 : typedef struct pullup_replace_vars_context
70 : {
71 : PlannerInfo *root;
72 : List *targetlist; /* tlist of subquery being pulled up */
73 : RangeTblEntry *target_rte; /* RTE of subquery */
74 : int result_relation; /* the index of the result relation in the
75 : * rewritten query */
76 : Relids relids; /* relids within subquery, as numbered after
77 : * pullup (set only if target_rte->lateral) */
78 : nullingrel_info *nullinfo; /* per-RTE nullingrel info (set only if
79 : * target_rte->lateral) */
80 : bool *outer_hasSubLinks; /* -> outer query's hasSubLinks */
81 : int varno; /* varno of subquery */
82 : ReplaceWrapOption wrap_option; /* do we need certain outputs to be PHVs? */
83 : Node **rv_cache; /* cache for results with PHVs */
84 : } pullup_replace_vars_context;
85 :
86 : typedef struct reduce_outer_joins_pass1_state
87 : {
88 : Relids relids; /* base relids within this subtree */
89 : bool contains_outer; /* does subtree contain outer join(s)? */
90 : List *sub_states; /* List of states for subtree components */
91 : } reduce_outer_joins_pass1_state;
92 :
93 : typedef struct reduce_outer_joins_pass2_state
94 : {
95 : Relids inner_reduced; /* OJ relids reduced to plain inner joins */
96 : List *partial_reduced; /* List of partially reduced FULL joins */
97 : } reduce_outer_joins_pass2_state;
98 :
99 : typedef struct reduce_outer_joins_partial_state
100 : {
101 : int full_join_rti; /* RT index of a formerly-FULL join */
102 : Relids unreduced_side; /* relids in its still-nullable side */
103 : } reduce_outer_joins_partial_state;
104 :
105 : static Node *pull_up_sublinks_jointree_recurse(PlannerInfo *root, Node *jtnode,
106 : Relids *relids);
107 : static Node *pull_up_sublinks_qual_recurse(PlannerInfo *root, Node *node,
108 : Node **jtlink1, Relids available_rels1,
109 : Node **jtlink2, Relids available_rels2);
110 : static Node *pull_up_subqueries_recurse(PlannerInfo *root, Node *jtnode,
111 : JoinExpr *lowest_outer_join,
112 : AppendRelInfo *containing_appendrel);
113 : static Node *pull_up_simple_subquery(PlannerInfo *root, Node *jtnode,
114 : RangeTblEntry *rte,
115 : JoinExpr *lowest_outer_join,
116 : AppendRelInfo *containing_appendrel);
117 : static Node *pull_up_simple_union_all(PlannerInfo *root, Node *jtnode,
118 : RangeTblEntry *rte);
119 : static void pull_up_union_leaf_queries(Node *setOp, PlannerInfo *root,
120 : int parentRTindex, Query *setOpQuery,
121 : int childRToffset);
122 : static void make_setop_translation_list(Query *query, int newvarno,
123 : AppendRelInfo *appinfo);
124 : static bool is_simple_subquery(PlannerInfo *root, Query *subquery,
125 : RangeTblEntry *rte,
126 : JoinExpr *lowest_outer_join);
127 : static Node *pull_up_simple_values(PlannerInfo *root, Node *jtnode,
128 : RangeTblEntry *rte);
129 : static bool is_simple_values(PlannerInfo *root, RangeTblEntry *rte);
130 : static Node *pull_up_constant_function(PlannerInfo *root, Node *jtnode,
131 : RangeTblEntry *rte,
132 : AppendRelInfo *containing_appendrel);
133 : static bool is_simple_union_all(Query *subquery);
134 : static bool is_simple_union_all_recurse(Node *setOp, Query *setOpQuery,
135 : List *colTypes);
136 : static bool is_safe_append_member(Query *subquery);
137 : static bool jointree_contains_lateral_outer_refs(PlannerInfo *root,
138 : Node *jtnode, bool restricted,
139 : Relids safe_upper_varnos);
140 : static void perform_pullup_replace_vars(PlannerInfo *root,
141 : pullup_replace_vars_context *rvcontext,
142 : AppendRelInfo *containing_appendrel);
143 : static void replace_vars_in_jointree(Node *jtnode,
144 : pullup_replace_vars_context *context);
145 : static Node *pullup_replace_vars(Node *expr,
146 : pullup_replace_vars_context *context);
147 : static Node *pullup_replace_vars_callback(Var *var,
148 : replace_rte_variables_context *context);
149 : static Query *pullup_replace_vars_subquery(Query *query,
150 : pullup_replace_vars_context *context);
151 : static reduce_outer_joins_pass1_state *reduce_outer_joins_pass1(Node *jtnode);
152 : static void reduce_outer_joins_pass2(Node *jtnode,
153 : reduce_outer_joins_pass1_state *state1,
154 : reduce_outer_joins_pass2_state *state2,
155 : PlannerInfo *root,
156 : Relids nonnullable_rels,
157 : List *forced_null_vars);
158 : static void report_reduced_full_join(reduce_outer_joins_pass2_state *state2,
159 : int rtindex, Relids relids);
160 : static Node *remove_useless_results_recurse(PlannerInfo *root, Node *jtnode,
161 : Node **parent_quals,
162 : Relids *dropped_outer_joins);
163 : static int get_result_relid(PlannerInfo *root, Node *jtnode);
164 : static void remove_result_refs(PlannerInfo *root, int varno, Node *newjtloc);
165 : static bool find_dependent_phvs(PlannerInfo *root, int varno);
166 : static bool find_dependent_phvs_in_jointree(PlannerInfo *root,
167 : Node *node, int varno);
168 : static void substitute_phv_relids(Node *node,
169 : int varno, Relids subrelids);
170 : static void fix_append_rel_relids(PlannerInfo *root, int varno,
171 : Relids subrelids);
172 : static Node *find_jointree_node_for_rel(Node *jtnode, int relid);
173 : static nullingrel_info *get_nullingrels(Query *parse);
174 : static void get_nullingrels_recurse(Node *jtnode, Relids upper_nullingrels,
175 : nullingrel_info *info);
176 :
177 :
178 : /*
179 : * transform_MERGE_to_join
180 : * Replace a MERGE's jointree to also include the target relation.
181 : */
182 : void
183 545512 : transform_MERGE_to_join(Query *parse)
184 : {
185 : RangeTblEntry *joinrte;
186 : JoinExpr *joinexpr;
187 : bool have_action[NUM_MERGE_MATCH_KINDS];
188 : JoinType jointype;
189 : int joinrti;
190 : List *vars;
191 : RangeTblRef *rtr;
192 : FromExpr *target;
193 : Node *source;
194 : int sourcerti;
195 :
196 545512 : if (parse->commandType != CMD_MERGE)
197 543682 : return;
198 :
199 : /* XXX probably bogus */
200 1830 : vars = NIL;
201 :
202 : /*
203 : * Work out what kind of join is required. If there any WHEN NOT MATCHED
204 : * BY SOURCE/TARGET actions, an outer join is required so that we process
205 : * all unmatched tuples from the source and/or target relations.
206 : * Otherwise, we can use an inner join.
207 : */
208 1830 : have_action[MERGE_WHEN_MATCHED] = false;
209 1830 : have_action[MERGE_WHEN_NOT_MATCHED_BY_SOURCE] = false;
210 1830 : have_action[MERGE_WHEN_NOT_MATCHED_BY_TARGET] = false;
211 :
212 6438 : foreach_node(MergeAction, action, parse->mergeActionList)
213 : {
214 2778 : if (action->commandType != CMD_NOTHING)
215 2706 : have_action[action->matchKind] = true;
216 : }
217 :
218 1830 : if (have_action[MERGE_WHEN_NOT_MATCHED_BY_SOURCE] &&
219 126 : have_action[MERGE_WHEN_NOT_MATCHED_BY_TARGET])
220 96 : jointype = JOIN_FULL;
221 1734 : else if (have_action[MERGE_WHEN_NOT_MATCHED_BY_SOURCE])
222 30 : jointype = JOIN_LEFT;
223 1704 : else if (have_action[MERGE_WHEN_NOT_MATCHED_BY_TARGET])
224 782 : jointype = JOIN_RIGHT;
225 : else
226 922 : jointype = JOIN_INNER;
227 :
228 : /* Manufacture a join RTE to use. */
229 1830 : joinrte = makeNode(RangeTblEntry);
230 1830 : joinrte->rtekind = RTE_JOIN;
231 1830 : joinrte->jointype = jointype;
232 1830 : joinrte->joinmergedcols = 0;
233 1830 : joinrte->joinaliasvars = vars;
234 1830 : joinrte->joinleftcols = NIL; /* MERGE does not allow JOIN USING */
235 1830 : joinrte->joinrightcols = NIL; /* ditto */
236 1830 : joinrte->join_using_alias = NULL;
237 :
238 1830 : joinrte->alias = NULL;
239 1830 : joinrte->eref = makeAlias("*MERGE*", NIL);
240 1830 : joinrte->lateral = false;
241 1830 : joinrte->inh = false;
242 1830 : joinrte->inFromCl = true;
243 :
244 : /*
245 : * Add completed RTE to pstate's range table list, so that we know its
246 : * index.
247 : */
248 1830 : parse->rtable = lappend(parse->rtable, joinrte);
249 1830 : joinrti = list_length(parse->rtable);
250 :
251 : /*
252 : * Create a JOIN between the target and the source relation.
253 : *
254 : * Here the target is identified by parse->mergeTargetRelation. For a
255 : * regular table, this will equal parse->resultRelation, but for a
256 : * trigger-updatable view, it will be the expanded view subquery that we
257 : * need to pull data from.
258 : *
259 : * The source relation is in parse->jointree->fromlist, but any quals in
260 : * parse->jointree->quals are restrictions on the target relation (if the
261 : * target relation is an auto-updatable view).
262 : */
263 : /* target rel, with any quals */
264 1830 : rtr = makeNode(RangeTblRef);
265 1830 : rtr->rtindex = parse->mergeTargetRelation;
266 1830 : target = makeFromExpr(list_make1(rtr), parse->jointree->quals);
267 :
268 : /* source rel (expect exactly one -- see transformMergeStmt()) */
269 : Assert(list_length(parse->jointree->fromlist) == 1);
270 1830 : source = linitial(parse->jointree->fromlist);
271 :
272 : /*
273 : * index of source rel (expect either a RangeTblRef or a JoinExpr -- see
274 : * transformFromClauseItem()).
275 : */
276 1830 : if (IsA(source, RangeTblRef))
277 1746 : sourcerti = ((RangeTblRef *) source)->rtindex;
278 84 : else if (IsA(source, JoinExpr))
279 84 : sourcerti = ((JoinExpr *) source)->rtindex;
280 : else
281 : {
282 0 : elog(ERROR, "unrecognized source node type: %d",
283 : (int) nodeTag(source));
284 : sourcerti = 0; /* keep compiler quiet */
285 : }
286 :
287 : /* Join the source and target */
288 1830 : joinexpr = makeNode(JoinExpr);
289 1830 : joinexpr->jointype = jointype;
290 1830 : joinexpr->isNatural = false;
291 1830 : joinexpr->larg = (Node *) target;
292 1830 : joinexpr->rarg = source;
293 1830 : joinexpr->usingClause = NIL;
294 1830 : joinexpr->join_using_alias = NULL;
295 1830 : joinexpr->quals = parse->mergeJoinCondition;
296 1830 : joinexpr->alias = NULL;
297 1830 : joinexpr->rtindex = joinrti;
298 :
299 : /* Make the new join be the sole entry in the query's jointree */
300 1830 : parse->jointree->fromlist = list_make1(joinexpr);
301 1830 : parse->jointree->quals = NULL;
302 :
303 : /*
304 : * If necessary, mark parse->targetlist entries that refer to the target
305 : * as nullable by the join. Normally the targetlist will be empty for a
306 : * MERGE, but if the target is a trigger-updatable view, it will contain a
307 : * whole-row Var referring to the expanded view query.
308 : */
309 1830 : if (parse->targetList != NIL &&
310 42 : (jointype == JOIN_RIGHT || jointype == JOIN_FULL))
311 42 : parse->targetList = (List *)
312 42 : add_nulling_relids((Node *) parse->targetList,
313 42 : bms_make_singleton(parse->mergeTargetRelation),
314 42 : bms_make_singleton(joinrti));
315 :
316 : /*
317 : * If the source relation is on the outer side of the join, mark any
318 : * source relation Vars in the join condition, actions, and RETURNING list
319 : * as nullable by the join. These Vars will be added to the targetlist by
320 : * preprocess_targetlist(), so it's important to mark them correctly here.
321 : *
322 : * It might seem that this is not necessary for Vars in the join
323 : * condition, since it is inside the join, but it is also needed above the
324 : * join (in the ModifyTable node) to distinguish between the MATCHED and
325 : * NOT MATCHED BY SOURCE cases -- see ExecMergeMatched(). Note that this
326 : * creates a modified copy of the join condition, for use above the join,
327 : * without modifying the original join condition, inside the join.
328 : */
329 1830 : if (jointype == JOIN_LEFT || jointype == JOIN_FULL)
330 : {
331 126 : parse->mergeJoinCondition =
332 126 : add_nulling_relids(parse->mergeJoinCondition,
333 126 : bms_make_singleton(sourcerti),
334 126 : bms_make_singleton(joinrti));
335 :
336 600 : foreach_node(MergeAction, action, parse->mergeActionList)
337 : {
338 348 : action->qual =
339 348 : add_nulling_relids(action->qual,
340 348 : bms_make_singleton(sourcerti),
341 348 : bms_make_singleton(joinrti));
342 :
343 348 : action->targetList = (List *)
344 348 : add_nulling_relids((Node *) action->targetList,
345 348 : bms_make_singleton(sourcerti),
346 348 : bms_make_singleton(joinrti));
347 : }
348 :
349 126 : parse->returningList = (List *)
350 126 : add_nulling_relids((Node *) parse->returningList,
351 126 : bms_make_singleton(sourcerti),
352 126 : bms_make_singleton(joinrti));
353 : }
354 :
355 : /*
356 : * If there are any WHEN NOT MATCHED BY SOURCE actions, the executor will
357 : * use the join condition to distinguish between MATCHED and NOT MATCHED
358 : * BY SOURCE cases. Otherwise, it's no longer needed, and we set it to
359 : * NULL, saving cycles during planning and execution.
360 : *
361 : * We need to be careful though: the executor evaluates this condition
362 : * using the output of the join subplan node, which nulls the output from
363 : * the source relation when the join condition doesn't match. That risks
364 : * producing incorrect results when rechecking using a "non-strict" join
365 : * condition, such as "src.col IS NOT DISTINCT FROM tgt.col". To guard
366 : * against that, we add an additional "src IS NOT NULL" check to the join
367 : * condition, so that it does the right thing when performing a recheck
368 : * based on the output of the join subplan.
369 : */
370 1830 : if (have_action[MERGE_WHEN_NOT_MATCHED_BY_SOURCE])
371 : {
372 : Var *var;
373 : NullTest *ntest;
374 :
375 : /* source wholerow Var (nullable by the new join) */
376 126 : var = makeWholeRowVar(rt_fetch(sourcerti, parse->rtable),
377 : sourcerti, 0, false);
378 126 : var->varnullingrels = bms_make_singleton(joinrti);
379 :
380 : /* "src IS NOT NULL" check */
381 126 : ntest = makeNode(NullTest);
382 126 : ntest->arg = (Expr *) var;
383 126 : ntest->nulltesttype = IS_NOT_NULL;
384 126 : ntest->argisrow = false;
385 126 : ntest->location = -1;
386 :
387 : /* combine it with the original join condition */
388 126 : parse->mergeJoinCondition =
389 126 : (Node *) make_and_qual((Node *) ntest, parse->mergeJoinCondition);
390 : }
391 : else
392 1704 : parse->mergeJoinCondition = NULL; /* join condition not needed */
393 : }
394 :
395 : /*
396 : * replace_empty_jointree
397 : * If the Query's jointree is empty, replace it with a dummy RTE_RESULT
398 : * relation.
399 : *
400 : * By doing this, we can avoid a bunch of corner cases that formerly existed
401 : * for SELECTs with omitted FROM clauses. An example is that a subquery
402 : * with empty jointree previously could not be pulled up, because that would
403 : * have resulted in an empty relid set, making the subquery not uniquely
404 : * identifiable for join or PlaceHolderVar processing.
405 : *
406 : * Unlike most other functions in this file, this function doesn't recurse;
407 : * we rely on other processing to invoke it on sub-queries at suitable times.
408 : */
409 : void
410 582114 : replace_empty_jointree(Query *parse)
411 : {
412 : RangeTblEntry *rte;
413 : Index rti;
414 : RangeTblRef *rtr;
415 :
416 : /* Nothing to do if jointree is already nonempty */
417 582114 : if (parse->jointree->fromlist != NIL)
418 355158 : return;
419 :
420 : /* We mustn't change it in the top level of a setop tree, either */
421 233282 : if (parse->setOperations)
422 6326 : return;
423 :
424 : /* Create suitable RTE */
425 226956 : rte = makeNode(RangeTblEntry);
426 226956 : rte->rtekind = RTE_RESULT;
427 226956 : rte->eref = makeAlias("*RESULT*", NIL);
428 :
429 : /* Add it to rangetable */
430 226956 : parse->rtable = lappend(parse->rtable, rte);
431 226956 : rti = list_length(parse->rtable);
432 :
433 : /* And jam a reference into the jointree */
434 226956 : rtr = makeNode(RangeTblRef);
435 226956 : rtr->rtindex = rti;
436 226956 : parse->jointree->fromlist = list_make1(rtr);
437 : }
438 :
439 : /*
440 : * pull_up_sublinks
441 : * Attempt to pull up ANY and EXISTS SubLinks to be treated as
442 : * semijoins or anti-semijoins.
443 : *
444 : * A clause "foo op ANY (sub-SELECT)" can be processed by pulling the
445 : * sub-SELECT up to become a rangetable entry and treating the implied
446 : * comparisons as quals of a semijoin. However, this optimization *only*
447 : * works at the top level of WHERE or a JOIN/ON clause, because we cannot
448 : * distinguish whether the ANY ought to return FALSE or NULL in cases
449 : * involving NULL inputs. Also, in an outer join's ON clause we can only
450 : * do this if the sublink is degenerate (ie, references only the nullable
451 : * side of the join). In that case it is legal to push the semijoin
452 : * down into the nullable side of the join. If the sublink references any
453 : * nonnullable-side variables then it would have to be evaluated as part
454 : * of the outer join, which makes things way too complicated.
455 : *
456 : * Under similar conditions, EXISTS and NOT EXISTS clauses can be handled
457 : * by pulling up the sub-SELECT and creating a semijoin or anti-semijoin.
458 : *
459 : * This routine searches for such clauses and does the necessary parsetree
460 : * transformations if any are found.
461 : *
462 : * This routine has to run before preprocess_expression(), so the quals
463 : * clauses are not yet reduced to implicit-AND format, and are not guaranteed
464 : * to be AND/OR-flat either. That means we need to recursively search through
465 : * explicit AND clauses. We stop as soon as we hit a non-AND item.
466 : */
467 : void
468 35022 : pull_up_sublinks(PlannerInfo *root)
469 : {
470 : Node *jtnode;
471 : Relids relids;
472 :
473 : /* Begin recursion through the jointree */
474 35022 : jtnode = pull_up_sublinks_jointree_recurse(root,
475 35022 : (Node *) root->parse->jointree,
476 : &relids);
477 :
478 : /*
479 : * root->parse->jointree must always be a FromExpr, so insert a dummy one
480 : * if we got a bare RangeTblRef or JoinExpr out of the recursion.
481 : */
482 35022 : if (IsA(jtnode, FromExpr))
483 30106 : root->parse->jointree = (FromExpr *) jtnode;
484 : else
485 4916 : root->parse->jointree = makeFromExpr(list_make1(jtnode), NULL);
486 35022 : }
487 :
488 : /*
489 : * Recurse through jointree nodes for pull_up_sublinks()
490 : *
491 : * In addition to returning the possibly-modified jointree node, we return
492 : * a relids set of the contained rels into *relids.
493 : */
494 : static Node *
495 117826 : pull_up_sublinks_jointree_recurse(PlannerInfo *root, Node *jtnode,
496 : Relids *relids)
497 : {
498 : /* Since this function recurses, it could be driven to stack overflow. */
499 117826 : check_stack_depth();
500 :
501 117826 : if (jtnode == NULL)
502 : {
503 0 : *relids = NULL;
504 : }
505 117826 : else if (IsA(jtnode, RangeTblRef))
506 : {
507 63088 : int varno = ((RangeTblRef *) jtnode)->rtindex;
508 :
509 63088 : *relids = bms_make_singleton(varno);
510 : /* jtnode is returned unmodified */
511 : }
512 54738 : else if (IsA(jtnode, FromExpr))
513 : {
514 35250 : FromExpr *f = (FromExpr *) jtnode;
515 35250 : List *newfromlist = NIL;
516 35250 : Relids frelids = NULL;
517 : FromExpr *newf;
518 : Node *jtlink;
519 : ListCell *l;
520 :
521 : /* First, recurse to process children and collect their relids */
522 73944 : foreach(l, f->fromlist)
523 : {
524 : Node *newchild;
525 : Relids childrelids;
526 :
527 38694 : newchild = pull_up_sublinks_jointree_recurse(root,
528 38694 : lfirst(l),
529 : &childrelids);
530 38694 : newfromlist = lappend(newfromlist, newchild);
531 38694 : frelids = bms_join(frelids, childrelids);
532 : }
533 : /* Build the replacement FromExpr; no quals yet */
534 35250 : newf = makeFromExpr(newfromlist, NULL);
535 : /* Set up a link representing the rebuilt jointree */
536 35250 : jtlink = (Node *) newf;
537 : /* Now process qual --- all children are available for use */
538 35250 : newf->quals = pull_up_sublinks_qual_recurse(root, f->quals,
539 : &jtlink, frelids,
540 : NULL, NULL);
541 :
542 : /*
543 : * Note that the result will be either newf, or a stack of JoinExprs
544 : * with newf at the base. We rely on subsequent optimization steps to
545 : * flatten this and rearrange the joins as needed.
546 : *
547 : * Although we could include the pulled-up subqueries in the returned
548 : * relids, there's no need since upper quals couldn't refer to their
549 : * outputs anyway.
550 : */
551 35250 : *relids = frelids;
552 35250 : jtnode = jtlink;
553 : }
554 19488 : else if (IsA(jtnode, JoinExpr))
555 : {
556 : JoinExpr *j;
557 : Relids leftrelids;
558 : Relids rightrelids;
559 : Node *jtlink;
560 :
561 : /*
562 : * Make a modifiable copy of join node, but don't bother copying its
563 : * subnodes (yet).
564 : */
565 19488 : j = (JoinExpr *) palloc(sizeof(JoinExpr));
566 19488 : memcpy(j, jtnode, sizeof(JoinExpr));
567 19488 : jtlink = (Node *) j;
568 :
569 : /* Recurse to process children and collect their relids */
570 19488 : j->larg = pull_up_sublinks_jointree_recurse(root, j->larg,
571 : &leftrelids);
572 19488 : j->rarg = pull_up_sublinks_jointree_recurse(root, j->rarg,
573 : &rightrelids);
574 :
575 : /*
576 : * Now process qual, showing appropriate child relids as available,
577 : * and attach any pulled-up jointree items at the right place. In the
578 : * inner-join case we put new JoinExprs above the existing one (much
579 : * as for a FromExpr-style join). In outer-join cases the new
580 : * JoinExprs must go into the nullable side of the outer join. The
581 : * point of the available_rels machinations is to ensure that we only
582 : * pull up quals for which that's okay.
583 : *
584 : * We don't expect to see any pre-existing JOIN_SEMI, JOIN_ANTI,
585 : * JOIN_RIGHT_SEMI, or JOIN_RIGHT_ANTI jointypes here.
586 : */
587 19488 : switch (j->jointype)
588 : {
589 8924 : case JOIN_INNER:
590 8924 : j->quals = pull_up_sublinks_qual_recurse(root, j->quals,
591 : &jtlink,
592 : bms_union(leftrelids,
593 : rightrelids),
594 : NULL, NULL);
595 8924 : break;
596 10456 : case JOIN_LEFT:
597 10456 : j->quals = pull_up_sublinks_qual_recurse(root, j->quals,
598 : &j->rarg,
599 : rightrelids,
600 : NULL, NULL);
601 10456 : break;
602 12 : case JOIN_FULL:
603 : /* can't do anything with full-join quals */
604 12 : break;
605 96 : case JOIN_RIGHT:
606 96 : j->quals = pull_up_sublinks_qual_recurse(root, j->quals,
607 : &j->larg,
608 : leftrelids,
609 : NULL, NULL);
610 96 : break;
611 0 : default:
612 0 : elog(ERROR, "unrecognized join type: %d",
613 : (int) j->jointype);
614 : break;
615 : }
616 :
617 : /*
618 : * Although we could include the pulled-up subqueries in the returned
619 : * relids, there's no need since upper quals couldn't refer to their
620 : * outputs anyway. But we *do* need to include the join's own rtindex
621 : * because we haven't yet collapsed join alias variables, so upper
622 : * levels would mistakenly think they couldn't use references to this
623 : * join.
624 : */
625 19488 : *relids = bms_join(leftrelids, rightrelids);
626 19488 : if (j->rtindex)
627 19488 : *relids = bms_add_member(*relids, j->rtindex);
628 19488 : jtnode = jtlink;
629 : }
630 : else
631 0 : elog(ERROR, "unrecognized node type: %d",
632 : (int) nodeTag(jtnode));
633 117826 : return jtnode;
634 : }
635 :
636 : /*
637 : * Recurse through top-level qual nodes for pull_up_sublinks()
638 : *
639 : * jtlink1 points to the link in the jointree where any new JoinExprs should
640 : * be inserted if they reference available_rels1 (i.e., available_rels1
641 : * denotes the relations present underneath jtlink1). Optionally, jtlink2 can
642 : * point to a second link where new JoinExprs should be inserted if they
643 : * reference available_rels2 (pass NULL for both those arguments if not used).
644 : * Note that SubLinks referencing both sets of variables cannot be optimized.
645 : * If we find multiple pull-up-able SubLinks, they'll get stacked onto jtlink1
646 : * and/or jtlink2 in the order we encounter them. We rely on subsequent
647 : * optimization to rearrange the stack if appropriate.
648 : *
649 : * Returns the replacement qual node, or NULL if the qual should be removed.
650 : */
651 : static Node *
652 104368 : pull_up_sublinks_qual_recurse(PlannerInfo *root, Node *node,
653 : Node **jtlink1, Relids available_rels1,
654 : Node **jtlink2, Relids available_rels2)
655 : {
656 104368 : if (node == NULL)
657 7230 : return NULL;
658 97138 : if (IsA(node, SubLink))
659 : {
660 2716 : SubLink *sublink = (SubLink *) node;
661 : JoinExpr *j;
662 : Relids child_rels;
663 :
664 : /* Is it a convertible ANY or EXISTS clause? */
665 2716 : if (sublink->subLinkType == ANY_SUBLINK)
666 : {
667 1808 : if ((j = convert_ANY_sublink_to_join(root, sublink,
668 : available_rels1)) != NULL)
669 : {
670 : /* Yes; insert the new join node into the join tree */
671 1702 : j->larg = *jtlink1;
672 1702 : *jtlink1 = (Node *) j;
673 : /* Recursively process pulled-up jointree nodes */
674 1702 : j->rarg = pull_up_sublinks_jointree_recurse(root,
675 : j->rarg,
676 : &child_rels);
677 :
678 : /*
679 : * Now recursively process the pulled-up quals. Any inserted
680 : * joins can get stacked onto either j->larg or j->rarg,
681 : * depending on which rels they reference.
682 : */
683 1702 : j->quals = pull_up_sublinks_qual_recurse(root,
684 : j->quals,
685 : &j->larg,
686 : available_rels1,
687 : &j->rarg,
688 : child_rels);
689 : /* Return NULL representing constant TRUE */
690 1702 : return NULL;
691 : }
692 112 : if (available_rels2 != NULL &&
693 6 : (j = convert_ANY_sublink_to_join(root, sublink,
694 : available_rels2)) != NULL)
695 : {
696 : /* Yes; insert the new join node into the join tree */
697 0 : j->larg = *jtlink2;
698 0 : *jtlink2 = (Node *) j;
699 : /* Recursively process pulled-up jointree nodes */
700 0 : j->rarg = pull_up_sublinks_jointree_recurse(root,
701 : j->rarg,
702 : &child_rels);
703 :
704 : /*
705 : * Now recursively process the pulled-up quals. Any inserted
706 : * joins can get stacked onto either j->larg or j->rarg,
707 : * depending on which rels they reference.
708 : */
709 0 : j->quals = pull_up_sublinks_qual_recurse(root,
710 : j->quals,
711 : &j->larg,
712 : available_rels2,
713 : &j->rarg,
714 : child_rels);
715 : /* Return NULL representing constant TRUE */
716 0 : return NULL;
717 : }
718 : }
719 908 : else if (sublink->subLinkType == EXISTS_SUBLINK)
720 : {
721 848 : if ((j = convert_EXISTS_sublink_to_join(root, sublink, false,
722 : available_rels1)) != NULL)
723 : {
724 : /* Yes; insert the new join node into the join tree */
725 700 : j->larg = *jtlink1;
726 700 : *jtlink1 = (Node *) j;
727 : /* Recursively process pulled-up jointree nodes */
728 700 : j->rarg = pull_up_sublinks_jointree_recurse(root,
729 : j->rarg,
730 : &child_rels);
731 :
732 : /*
733 : * Now recursively process the pulled-up quals. Any inserted
734 : * joins can get stacked onto either j->larg or j->rarg,
735 : * depending on which rels they reference.
736 : */
737 700 : j->quals = pull_up_sublinks_qual_recurse(root,
738 : j->quals,
739 : &j->larg,
740 : available_rels1,
741 : &j->rarg,
742 : child_rels);
743 : /* Return NULL representing constant TRUE */
744 700 : return NULL;
745 : }
746 180 : if (available_rels2 != NULL &&
747 32 : (j = convert_EXISTS_sublink_to_join(root, sublink, false,
748 : available_rels2)) != NULL)
749 : {
750 : /* Yes; insert the new join node into the join tree */
751 32 : j->larg = *jtlink2;
752 32 : *jtlink2 = (Node *) j;
753 : /* Recursively process pulled-up jointree nodes */
754 32 : j->rarg = pull_up_sublinks_jointree_recurse(root,
755 : j->rarg,
756 : &child_rels);
757 :
758 : /*
759 : * Now recursively process the pulled-up quals. Any inserted
760 : * joins can get stacked onto either j->larg or j->rarg,
761 : * depending on which rels they reference.
762 : */
763 32 : j->quals = pull_up_sublinks_qual_recurse(root,
764 : j->quals,
765 : &j->larg,
766 : available_rels2,
767 : &j->rarg,
768 : child_rels);
769 : /* Return NULL representing constant TRUE */
770 32 : return NULL;
771 : }
772 : }
773 : /* Else return it unmodified */
774 282 : return node;
775 : }
776 94422 : if (is_notclause(node))
777 : {
778 : /* If the immediate argument of NOT is EXISTS, try to convert */
779 7266 : SubLink *sublink = (SubLink *) get_notclausearg((Expr *) node);
780 : JoinExpr *j;
781 : Relids child_rels;
782 :
783 7266 : if (sublink && IsA(sublink, SubLink))
784 : {
785 2810 : if (sublink->subLinkType == EXISTS_SUBLINK)
786 : {
787 2714 : if ((j = convert_EXISTS_sublink_to_join(root, sublink, true,
788 : available_rels1)) != NULL)
789 : {
790 : /* Yes; insert the new join node into the join tree */
791 2700 : j->larg = *jtlink1;
792 2700 : *jtlink1 = (Node *) j;
793 : /* Recursively process pulled-up jointree nodes */
794 2700 : j->rarg = pull_up_sublinks_jointree_recurse(root,
795 : j->rarg,
796 : &child_rels);
797 :
798 : /*
799 : * Now recursively process the pulled-up quals. Because
800 : * we are underneath a NOT, we can't pull up sublinks that
801 : * reference the left-hand stuff, but it's still okay to
802 : * pull up sublinks referencing j->rarg.
803 : */
804 2700 : j->quals = pull_up_sublinks_qual_recurse(root,
805 : j->quals,
806 : &j->rarg,
807 : child_rels,
808 : NULL, NULL);
809 : /* Return NULL representing constant TRUE */
810 2700 : return NULL;
811 : }
812 14 : if (available_rels2 != NULL &&
813 0 : (j = convert_EXISTS_sublink_to_join(root, sublink, true,
814 : available_rels2)) != NULL)
815 : {
816 : /* Yes; insert the new join node into the join tree */
817 0 : j->larg = *jtlink2;
818 0 : *jtlink2 = (Node *) j;
819 : /* Recursively process pulled-up jointree nodes */
820 0 : j->rarg = pull_up_sublinks_jointree_recurse(root,
821 : j->rarg,
822 : &child_rels);
823 :
824 : /*
825 : * Now recursively process the pulled-up quals. Because
826 : * we are underneath a NOT, we can't pull up sublinks that
827 : * reference the left-hand stuff, but it's still okay to
828 : * pull up sublinks referencing j->rarg.
829 : */
830 0 : j->quals = pull_up_sublinks_qual_recurse(root,
831 : j->quals,
832 : &j->rarg,
833 : child_rels,
834 : NULL, NULL);
835 : /* Return NULL representing constant TRUE */
836 0 : return NULL;
837 : }
838 : }
839 : }
840 : /* Else return it unmodified */
841 4566 : return node;
842 : }
843 87156 : if (is_andclause(node))
844 : {
845 : /* Recurse into AND clause */
846 15426 : List *newclauses = NIL;
847 : ListCell *l;
848 :
849 59934 : foreach(l, ((BoolExpr *) node)->args)
850 : {
851 44508 : Node *oldclause = (Node *) lfirst(l);
852 : Node *newclause;
853 :
854 44508 : newclause = pull_up_sublinks_qual_recurse(root,
855 : oldclause,
856 : jtlink1,
857 : available_rels1,
858 : jtlink2,
859 : available_rels2);
860 44508 : if (newclause)
861 41498 : newclauses = lappend(newclauses, newclause);
862 : }
863 : /* We might have got back fewer clauses than we started with */
864 15426 : if (newclauses == NIL)
865 120 : return NULL;
866 15306 : else if (list_length(newclauses) == 1)
867 1128 : return (Node *) linitial(newclauses);
868 : else
869 14178 : return (Node *) make_andclause(newclauses);
870 : }
871 : /* Stop if not an AND */
872 71730 : return node;
873 : }
874 :
875 : /*
876 : * preprocess_function_rtes
877 : * Constant-simplify any FUNCTION RTEs in the FROM clause, and then
878 : * attempt to "inline" any that are set-returning functions.
879 : *
880 : * If an RTE_FUNCTION rtable entry invokes a set-returning function that
881 : * contains just a simple SELECT, we can convert the rtable entry to an
882 : * RTE_SUBQUERY entry exposing the SELECT directly. This is especially
883 : * useful if the subquery can then be "pulled up" for further optimization,
884 : * but we do it even if not, to reduce executor overhead.
885 : *
886 : * This has to be done before we have started to do any optimization of
887 : * subqueries, else any such steps wouldn't get applied to subqueries
888 : * obtained via inlining. However, we do it after pull_up_sublinks
889 : * so that we can inline any functions used in SubLink subselects.
890 : *
891 : * The reason for applying const-simplification at this stage is that
892 : * (a) we'd need to do it anyway to inline a SRF, and (b) by doing it now,
893 : * we can be sure that pull_up_constant_function() will see constants
894 : * if there are constants to be seen. This approach also guarantees
895 : * that every FUNCTION RTE has been const-simplified, allowing planner.c's
896 : * preprocess_expression() to skip doing it again.
897 : *
898 : * Like most of the planner, this feels free to scribble on its input data
899 : * structure.
900 : */
901 : void
902 578650 : preprocess_function_rtes(PlannerInfo *root)
903 : {
904 : ListCell *rt;
905 :
906 1479290 : foreach(rt, root->parse->rtable)
907 : {
908 900646 : RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
909 :
910 900646 : if (rte->rtekind == RTE_FUNCTION)
911 : {
912 : Query *funcquery;
913 :
914 : /* Apply const-simplification */
915 52476 : rte->functions = (List *)
916 52476 : eval_const_expressions(root, (Node *) rte->functions);
917 :
918 : /* Check safety of expansion, and expand if possible */
919 52476 : funcquery = inline_set_returning_function(root, rte);
920 52470 : if (funcquery)
921 : {
922 : /* Successful expansion, convert the RTE to a subquery */
923 222 : rte->rtekind = RTE_SUBQUERY;
924 222 : rte->subquery = funcquery;
925 222 : rte->security_barrier = false;
926 :
927 : /*
928 : * Clear fields that should not be set in a subquery RTE.
929 : * However, we leave rte->functions filled in for the moment,
930 : * in case makeWholeRowVar needs to consult it. We'll clear
931 : * it in setrefs.c (see add_rte_to_flat_rtable) so that this
932 : * abuse of the data structure doesn't escape the planner.
933 : */
934 222 : rte->funcordinality = false;
935 : }
936 : }
937 : }
938 578644 : }
939 :
940 : /*
941 : * expand_virtual_generated_columns
942 : * Expand all virtual generated column references in a query.
943 : *
944 : * This scans the rangetable for relations with virtual generated columns, and
945 : * replaces all Var nodes in the query that reference these columns with the
946 : * generation expressions. Note that we do not descend into subqueries; that
947 : * is taken care of when the subqueries are planned.
948 : *
949 : * This has to be done after we have pulled up any SubLinks within the query's
950 : * quals; otherwise any virtual generated column references within the SubLinks
951 : * that should be transformed into joins wouldn't get expanded.
952 : *
953 : * Returns a modified copy of the query tree, if any relations with virtual
954 : * generated columns are present.
955 : */
956 : Query *
957 578644 : expand_virtual_generated_columns(PlannerInfo *root)
958 : {
959 578644 : Query *parse = root->parse;
960 : int rt_index;
961 : ListCell *lc;
962 :
963 578644 : rt_index = 0;
964 1479284 : foreach(lc, parse->rtable)
965 : {
966 900640 : RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
967 : Relation rel;
968 : TupleDesc tupdesc;
969 :
970 900640 : ++rt_index;
971 :
972 : /*
973 : * Only normal relations can have virtual generated columns.
974 : */
975 900640 : if (rte->rtekind != RTE_RELATION)
976 441838 : continue;
977 :
978 458802 : rel = table_open(rte->relid, NoLock);
979 :
980 458802 : tupdesc = RelationGetDescr(rel);
981 458802 : if (tupdesc->constr && tupdesc->constr->has_generated_virtual)
982 : {
983 1058 : List *tlist = NIL;
984 : pullup_replace_vars_context rvcontext;
985 :
986 3896 : for (int i = 0; i < tupdesc->natts; i++)
987 : {
988 2838 : Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
989 : TargetEntry *tle;
990 :
991 2838 : if (attr->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
992 : {
993 : Node *defexpr;
994 :
995 1352 : defexpr = build_generation_expression(rel, i + 1);
996 1352 : ChangeVarNodes(defexpr, 1, rt_index, 0);
997 :
998 1352 : tle = makeTargetEntry((Expr *) defexpr, i + 1, 0, false);
999 1352 : tlist = lappend(tlist, tle);
1000 : }
1001 : else
1002 : {
1003 : Var *var;
1004 :
1005 1486 : var = makeVar(rt_index,
1006 1486 : i + 1,
1007 : attr->atttypid,
1008 : attr->atttypmod,
1009 : attr->attcollation,
1010 : 0);
1011 :
1012 1486 : tle = makeTargetEntry((Expr *) var, i + 1, 0, false);
1013 1486 : tlist = lappend(tlist, tle);
1014 : }
1015 : }
1016 :
1017 : Assert(list_length(tlist) > 0);
1018 : Assert(!rte->lateral);
1019 :
1020 : /*
1021 : * The relation's targetlist items are now in the appropriate form
1022 : * to insert into the query, except that we may need to wrap them
1023 : * in PlaceHolderVars. Set up required context data for
1024 : * pullup_replace_vars.
1025 : */
1026 1058 : rvcontext.root = root;
1027 1058 : rvcontext.targetlist = tlist;
1028 1058 : rvcontext.target_rte = rte;
1029 1058 : rvcontext.result_relation = parse->resultRelation;
1030 : /* won't need these values */
1031 1058 : rvcontext.relids = NULL;
1032 1058 : rvcontext.nullinfo = NULL;
1033 : /* pass NULL for outer_hasSubLinks */
1034 1058 : rvcontext.outer_hasSubLinks = NULL;
1035 1058 : rvcontext.varno = rt_index;
1036 : /* this flag will be set below, if needed */
1037 1058 : rvcontext.wrap_option = REPLACE_WRAP_NONE;
1038 : /* initialize cache array with indexes 0 .. length(tlist) */
1039 1058 : rvcontext.rv_cache = palloc0((list_length(tlist) + 1) *
1040 : sizeof(Node *));
1041 :
1042 : /*
1043 : * If the query uses grouping sets, we need a PlaceHolderVar for
1044 : * each expression of the relation's targetlist items. (See
1045 : * comments in pull_up_simple_subquery().)
1046 : */
1047 1058 : if (parse->groupingSets)
1048 12 : rvcontext.wrap_option = REPLACE_WRAP_ALL;
1049 :
1050 : /*
1051 : * Apply pullup variable replacement throughout the query tree.
1052 : */
1053 1058 : parse = (Query *) pullup_replace_vars((Node *) parse, &rvcontext);
1054 : }
1055 :
1056 458802 : table_close(rel, NoLock);
1057 : }
1058 :
1059 578644 : return parse;
1060 : }
1061 :
1062 : /*
1063 : * pull_up_subqueries
1064 : * Look for subqueries in the rangetable that can be pulled up into
1065 : * the parent query. If the subquery has no special features like
1066 : * grouping/aggregation then we can merge it into the parent's jointree.
1067 : * Also, subqueries that are simple UNION ALL structures can be
1068 : * converted into "append relations".
1069 : */
1070 : void
1071 578644 : pull_up_subqueries(PlannerInfo *root)
1072 : {
1073 : /* Top level of jointree must always be a FromExpr */
1074 : Assert(IsA(root->parse->jointree, FromExpr));
1075 : /* Recursion starts with no containing join nor appendrel */
1076 1157282 : root->parse->jointree = (FromExpr *)
1077 578644 : pull_up_subqueries_recurse(root, (Node *) root->parse->jointree,
1078 : NULL, NULL);
1079 : /* We should still have a FromExpr */
1080 : Assert(IsA(root->parse->jointree, FromExpr));
1081 578638 : }
1082 :
1083 : /*
1084 : * pull_up_subqueries_recurse
1085 : * Recursive guts of pull_up_subqueries.
1086 : *
1087 : * This recursively processes the jointree and returns a modified jointree.
1088 : *
1089 : * If this jointree node is within either side of an outer join, then
1090 : * lowest_outer_join references the lowest such JoinExpr node; otherwise
1091 : * it is NULL. We use this to constrain the effects of LATERAL subqueries.
1092 : *
1093 : * If we are looking at a member subquery of an append relation,
1094 : * containing_appendrel describes that relation; else it is NULL.
1095 : * This forces use of the PlaceHolderVar mechanism for all non-Var targetlist
1096 : * items, and puts some additional restrictions on what can be pulled up.
1097 : *
1098 : * A tricky aspect of this code is that if we pull up a subquery we have
1099 : * to replace Vars that reference the subquery's outputs throughout the
1100 : * parent query, including quals attached to jointree nodes above the one
1101 : * we are currently processing! We handle this by being careful to maintain
1102 : * validity of the jointree structure while recursing, in the following sense:
1103 : * whenever we recurse, all qual expressions in the tree must be reachable
1104 : * from the top level, in case the recursive call needs to modify them.
1105 : *
1106 : * Notice also that we can't turn pullup_replace_vars loose on the whole
1107 : * jointree, because it'd return a mutated copy of the tree; we have to
1108 : * invoke it just on the quals, instead. This behavior is what makes it
1109 : * reasonable to pass lowest_outer_join as a pointer rather than some
1110 : * more-indirect way of identifying the lowest OJ. Likewise, we don't
1111 : * replace append_rel_list members but only their substructure, so the
1112 : * containing_appendrel reference is safe to use.
1113 : */
1114 : static Node *
1115 1394092 : pull_up_subqueries_recurse(PlannerInfo *root, Node *jtnode,
1116 : JoinExpr *lowest_outer_join,
1117 : AppendRelInfo *containing_appendrel)
1118 : {
1119 : /* Since this function recurses, it could be driven to stack overflow. */
1120 1394092 : check_stack_depth();
1121 : /* Also, since it's a bit expensive, let's check for query cancel. */
1122 1394092 : CHECK_FOR_INTERRUPTS();
1123 :
1124 : Assert(jtnode != NULL);
1125 1394092 : if (IsA(jtnode, RangeTblRef))
1126 : {
1127 717952 : int varno = ((RangeTblRef *) jtnode)->rtindex;
1128 717952 : RangeTblEntry *rte = rt_fetch(varno, root->parse->rtable);
1129 :
1130 : /*
1131 : * Is this a subquery RTE, and if so, is the subquery simple enough to
1132 : * pull up?
1133 : *
1134 : * If we are looking at an append-relation member, we can't pull it up
1135 : * unless is_safe_append_member says so.
1136 : */
1137 762554 : if (rte->rtekind == RTE_SUBQUERY &&
1138 78748 : is_simple_subquery(root, rte->subquery, rte, lowest_outer_join) &&
1139 4650 : (containing_appendrel == NULL ||
1140 4650 : is_safe_append_member(rte->subquery)))
1141 33138 : return pull_up_simple_subquery(root, jtnode, rte,
1142 : lowest_outer_join,
1143 : containing_appendrel);
1144 :
1145 : /*
1146 : * Alternatively, is it a simple UNION ALL subquery? If so, flatten
1147 : * into an "append relation".
1148 : *
1149 : * It's safe to do this regardless of whether this query is itself an
1150 : * appendrel member. (If you're thinking we should try to flatten the
1151 : * two levels of appendrel together, you're right; but we handle that
1152 : * in set_append_rel_pathlist, not here.)
1153 : */
1154 696278 : if (rte->rtekind == RTE_SUBQUERY &&
1155 11464 : is_simple_union_all(rte->subquery))
1156 1806 : return pull_up_simple_union_all(root, jtnode, rte);
1157 :
1158 : /*
1159 : * Or perhaps it's a simple VALUES RTE?
1160 : *
1161 : * We don't allow VALUES pullup below an outer join nor into an
1162 : * appendrel (such cases are impossible anyway at the moment).
1163 : */
1164 683008 : if (rte->rtekind == RTE_VALUES &&
1165 10104 : lowest_outer_join == NULL &&
1166 10104 : containing_appendrel == NULL &&
1167 10104 : is_simple_values(root, rte))
1168 1922 : return pull_up_simple_values(root, jtnode, rte);
1169 :
1170 : /*
1171 : * Or perhaps it's a FUNCTION RTE that we could inline?
1172 : */
1173 681086 : if (rte->rtekind == RTE_FUNCTION)
1174 52248 : return pull_up_constant_function(root, jtnode, rte,
1175 : containing_appendrel);
1176 :
1177 : /* Otherwise, do nothing at this node. */
1178 : }
1179 676140 : else if (IsA(jtnode, FromExpr))
1180 : {
1181 585408 : FromExpr *f = (FromExpr *) jtnode;
1182 : ListCell *l;
1183 :
1184 : Assert(containing_appendrel == NULL);
1185 : /* Recursively transform all the child nodes */
1186 1214076 : foreach(l, f->fromlist)
1187 : {
1188 628674 : lfirst(l) = pull_up_subqueries_recurse(root, lfirst(l),
1189 : lowest_outer_join,
1190 : NULL);
1191 : }
1192 : }
1193 90732 : else if (IsA(jtnode, JoinExpr))
1194 : {
1195 90732 : JoinExpr *j = (JoinExpr *) jtnode;
1196 :
1197 : Assert(containing_appendrel == NULL);
1198 : /* Recurse, being careful to tell myself when inside outer join */
1199 90732 : switch (j->jointype)
1200 : {
1201 38308 : case JOIN_INNER:
1202 38308 : j->larg = pull_up_subqueries_recurse(root, j->larg,
1203 : lowest_outer_join,
1204 : NULL);
1205 38308 : j->rarg = pull_up_subqueries_recurse(root, j->rarg,
1206 : lowest_outer_join,
1207 : NULL);
1208 38308 : break;
1209 50210 : case JOIN_LEFT:
1210 : case JOIN_SEMI:
1211 : case JOIN_ANTI:
1212 50210 : j->larg = pull_up_subqueries_recurse(root, j->larg,
1213 : j,
1214 : NULL);
1215 50210 : j->rarg = pull_up_subqueries_recurse(root, j->rarg,
1216 : j,
1217 : NULL);
1218 50210 : break;
1219 1090 : case JOIN_FULL:
1220 1090 : j->larg = pull_up_subqueries_recurse(root, j->larg,
1221 : j,
1222 : NULL);
1223 1090 : j->rarg = pull_up_subqueries_recurse(root, j->rarg,
1224 : j,
1225 : NULL);
1226 1090 : break;
1227 1124 : case JOIN_RIGHT:
1228 1124 : j->larg = pull_up_subqueries_recurse(root, j->larg,
1229 : j,
1230 : NULL);
1231 1124 : j->rarg = pull_up_subqueries_recurse(root, j->rarg,
1232 : j,
1233 : NULL);
1234 1124 : break;
1235 0 : default:
1236 0 : elog(ERROR, "unrecognized join type: %d",
1237 : (int) j->jointype);
1238 : break;
1239 : }
1240 : }
1241 : else
1242 0 : elog(ERROR, "unrecognized node type: %d",
1243 : (int) nodeTag(jtnode));
1244 1304972 : return jtnode;
1245 : }
1246 :
1247 : /*
1248 : * pull_up_simple_subquery
1249 : * Attempt to pull up a single simple subquery.
1250 : *
1251 : * jtnode is a RangeTblRef that has been tentatively identified as a simple
1252 : * subquery by pull_up_subqueries. We return the replacement jointree node,
1253 : * or jtnode itself if we determine that the subquery can't be pulled up
1254 : * after all.
1255 : *
1256 : * rte is the RangeTblEntry referenced by jtnode. Remaining parameters are
1257 : * as for pull_up_subqueries_recurse.
1258 : */
1259 : static Node *
1260 33138 : pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte,
1261 : JoinExpr *lowest_outer_join,
1262 : AppendRelInfo *containing_appendrel)
1263 : {
1264 33138 : Query *parse = root->parse;
1265 33138 : int varno = ((RangeTblRef *) jtnode)->rtindex;
1266 : Query *subquery;
1267 : PlannerInfo *subroot;
1268 : int rtoffset;
1269 : pullup_replace_vars_context rvcontext;
1270 : ListCell *lc;
1271 :
1272 : /*
1273 : * Make a modifiable copy of the subquery to hack on, so that the RTE will
1274 : * be left unchanged in case we decide below that we can't pull it up
1275 : * after all.
1276 : */
1277 33138 : subquery = copyObject(rte->subquery);
1278 :
1279 : /*
1280 : * Create a PlannerInfo data structure for this subquery.
1281 : *
1282 : * NOTE: the next few steps should match the first processing in
1283 : * subquery_planner(). Can we refactor to avoid code duplication, or
1284 : * would that just make things uglier?
1285 : */
1286 33138 : subroot = makeNode(PlannerInfo);
1287 33138 : subroot->parse = subquery;
1288 33138 : subroot->glob = root->glob;
1289 33138 : subroot->query_level = root->query_level;
1290 33138 : subroot->parent_root = root->parent_root;
1291 33138 : subroot->plan_params = NIL;
1292 33138 : subroot->outer_params = NULL;
1293 33138 : subroot->planner_cxt = CurrentMemoryContext;
1294 33138 : subroot->init_plans = NIL;
1295 33138 : subroot->cte_plan_ids = NIL;
1296 33138 : subroot->multiexpr_params = NIL;
1297 33138 : subroot->join_domains = NIL;
1298 33138 : subroot->eq_classes = NIL;
1299 33138 : subroot->ec_merging_done = false;
1300 33138 : subroot->last_rinfo_serial = 0;
1301 33138 : subroot->all_result_relids = NULL;
1302 33138 : subroot->leaf_result_relids = NULL;
1303 33138 : subroot->append_rel_list = NIL;
1304 33138 : subroot->row_identity_vars = NIL;
1305 33138 : subroot->rowMarks = NIL;
1306 33138 : memset(subroot->upper_rels, 0, sizeof(subroot->upper_rels));
1307 33138 : memset(subroot->upper_targets, 0, sizeof(subroot->upper_targets));
1308 33138 : subroot->processed_groupClause = NIL;
1309 33138 : subroot->processed_distinctClause = NIL;
1310 33138 : subroot->processed_tlist = NIL;
1311 33138 : subroot->update_colnos = NIL;
1312 33138 : subroot->grouping_map = NULL;
1313 33138 : subroot->minmax_aggs = NIL;
1314 33138 : subroot->qual_security_level = 0;
1315 33138 : subroot->placeholdersFrozen = false;
1316 33138 : subroot->hasRecursion = false;
1317 33138 : subroot->wt_param_id = -1;
1318 33138 : subroot->non_recursive_path = NULL;
1319 : /* We don't currently need a top JoinDomain for the subroot */
1320 :
1321 : /* No CTEs to worry about */
1322 : Assert(subquery->cteList == NIL);
1323 :
1324 : /*
1325 : * If the FROM clause is empty, replace it with a dummy RTE_RESULT RTE, so
1326 : * that we don't need so many special cases to deal with that situation.
1327 : */
1328 33138 : replace_empty_jointree(subquery);
1329 :
1330 : /*
1331 : * Pull up any SubLinks within the subquery's quals, so that we don't
1332 : * leave unoptimized SubLinks behind.
1333 : */
1334 33138 : if (subquery->hasSubLinks)
1335 2264 : pull_up_sublinks(subroot);
1336 :
1337 : /*
1338 : * Similarly, preprocess its function RTEs to inline any set-returning
1339 : * functions in its rangetable.
1340 : */
1341 33138 : preprocess_function_rtes(subroot);
1342 :
1343 : /*
1344 : * Scan the rangetable for relations with virtual generated columns, and
1345 : * replace all Var nodes in the query that reference these columns with
1346 : * the generation expressions.
1347 : */
1348 33138 : subquery = subroot->parse = expand_virtual_generated_columns(subroot);
1349 :
1350 : /*
1351 : * Recursively pull up the subquery's subqueries, so that
1352 : * pull_up_subqueries' processing is complete for its jointree and
1353 : * rangetable.
1354 : *
1355 : * Note: it's okay that the subquery's recursion starts with NULL for
1356 : * containing-join info, even if we are within an outer join in the upper
1357 : * query; the lower query starts with a clean slate for outer-join
1358 : * semantics. Likewise, we needn't pass down appendrel state.
1359 : */
1360 33138 : pull_up_subqueries(subroot);
1361 :
1362 : /*
1363 : * Now we must recheck whether the subquery is still simple enough to pull
1364 : * up. If not, abandon processing it.
1365 : *
1366 : * We don't really need to recheck all the conditions involved, but it's
1367 : * easier just to keep this "if" looking the same as the one in
1368 : * pull_up_subqueries_recurse.
1369 : */
1370 33138 : if (is_simple_subquery(root, subquery, rte, lowest_outer_join) &&
1371 3642 : (containing_appendrel == NULL || is_safe_append_member(subquery)))
1372 : {
1373 : /* good to go */
1374 : }
1375 : else
1376 : {
1377 : /*
1378 : * Give up, return unmodified RangeTblRef.
1379 : *
1380 : * Note: The work we just did will be redone when the subquery gets
1381 : * planned on its own. Perhaps we could avoid that by storing the
1382 : * modified subquery back into the rangetable, but I'm not gonna risk
1383 : * it now.
1384 : */
1385 202 : return jtnode;
1386 : }
1387 :
1388 : /*
1389 : * We must flatten any join alias Vars in the subquery's targetlist,
1390 : * because pulling up the subquery's subqueries might have changed their
1391 : * expansions into arbitrary expressions, which could affect
1392 : * pullup_replace_vars' decisions about whether PlaceHolderVar wrappers
1393 : * are needed for tlist entries. (Likely it'd be better to do
1394 : * flatten_join_alias_vars on the whole query tree at some earlier stage,
1395 : * maybe even in the rewriter; but for now let's just fix this case here.)
1396 : */
1397 32936 : subquery->targetList = (List *)
1398 32936 : flatten_join_alias_vars(subroot, subroot->parse,
1399 32936 : (Node *) subquery->targetList);
1400 :
1401 : /*
1402 : * Adjust level-0 varnos in subquery so that we can append its rangetable
1403 : * to upper query's. We have to fix the subquery's append_rel_list as
1404 : * well.
1405 : */
1406 32936 : rtoffset = list_length(parse->rtable);
1407 32936 : OffsetVarNodes((Node *) subquery, rtoffset, 0);
1408 32936 : OffsetVarNodes((Node *) subroot->append_rel_list, rtoffset, 0);
1409 :
1410 : /*
1411 : * Upper-level vars in subquery are now one level closer to their parent
1412 : * than before.
1413 : */
1414 32936 : IncrementVarSublevelsUp((Node *) subquery, -1, 1);
1415 32936 : IncrementVarSublevelsUp((Node *) subroot->append_rel_list, -1, 1);
1416 :
1417 : /*
1418 : * The subquery's targetlist items are now in the appropriate form to
1419 : * insert into the top query, except that we may need to wrap them in
1420 : * PlaceHolderVars. Set up required context data for pullup_replace_vars.
1421 : * (Note that we should include the subquery's inner joins in relids,
1422 : * since it may include join alias vars referencing them.)
1423 : */
1424 32936 : rvcontext.root = root;
1425 32936 : rvcontext.targetlist = subquery->targetList;
1426 32936 : rvcontext.target_rte = rte;
1427 32936 : rvcontext.result_relation = 0;
1428 32936 : if (rte->lateral)
1429 : {
1430 1120 : rvcontext.relids = get_relids_in_jointree((Node *) subquery->jointree,
1431 : true, true);
1432 1120 : rvcontext.nullinfo = get_nullingrels(parse);
1433 : }
1434 : else /* won't need these values */
1435 : {
1436 31816 : rvcontext.relids = NULL;
1437 31816 : rvcontext.nullinfo = NULL;
1438 : }
1439 32936 : rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
1440 32936 : rvcontext.varno = varno;
1441 : /* this flag will be set below, if needed */
1442 32936 : rvcontext.wrap_option = REPLACE_WRAP_NONE;
1443 : /* initialize cache array with indexes 0 .. length(tlist) */
1444 32936 : rvcontext.rv_cache = palloc0((list_length(subquery->targetList) + 1) *
1445 : sizeof(Node *));
1446 :
1447 : /*
1448 : * If the parent query uses grouping sets, we need a PlaceHolderVar for
1449 : * each expression of the subquery's targetlist items. This ensures that
1450 : * expressions retain their separate identity so that they will match
1451 : * grouping set columns when appropriate. (It'd be sufficient to wrap
1452 : * values used in grouping set columns, and do so only in non-aggregated
1453 : * portions of the tlist and havingQual, but that would require a lot of
1454 : * infrastructure that pullup_replace_vars hasn't currently got.)
1455 : */
1456 32936 : if (parse->groupingSets)
1457 332 : rvcontext.wrap_option = REPLACE_WRAP_ALL;
1458 :
1459 : /*
1460 : * Replace all of the top query's references to the subquery's outputs
1461 : * with copies of the adjusted subtlist items, being careful not to
1462 : * replace any of the jointree structure.
1463 : */
1464 32936 : perform_pullup_replace_vars(root, &rvcontext,
1465 : containing_appendrel);
1466 :
1467 : /*
1468 : * If the subquery had a LATERAL marker, propagate that to any of its
1469 : * child RTEs that could possibly now contain lateral cross-references.
1470 : * The children might or might not contain any actual lateral
1471 : * cross-references, but we have to mark the pulled-up child RTEs so that
1472 : * later planner stages will check for such.
1473 : */
1474 32930 : if (rte->lateral)
1475 : {
1476 2612 : foreach(lc, subquery->rtable)
1477 : {
1478 1492 : RangeTblEntry *child_rte = (RangeTblEntry *) lfirst(lc);
1479 :
1480 1492 : switch (child_rte->rtekind)
1481 : {
1482 678 : case RTE_RELATION:
1483 678 : if (child_rte->tablesample)
1484 36 : child_rte->lateral = true;
1485 678 : break;
1486 244 : case RTE_SUBQUERY:
1487 : case RTE_FUNCTION:
1488 : case RTE_VALUES:
1489 : case RTE_TABLEFUNC:
1490 244 : child_rte->lateral = true;
1491 244 : break;
1492 570 : case RTE_JOIN:
1493 : case RTE_CTE:
1494 : case RTE_NAMEDTUPLESTORE:
1495 : case RTE_RESULT:
1496 : case RTE_GROUP:
1497 : /* these can't contain any lateral references */
1498 570 : break;
1499 : }
1500 1492 : }
1501 : }
1502 :
1503 : /*
1504 : * Now append the adjusted rtable entries and their perminfos to upper
1505 : * query. (We hold off until after fixing the upper rtable entries; no
1506 : * point in running that code on the subquery ones too.)
1507 : */
1508 32930 : CombineRangeTables(&parse->rtable, &parse->rteperminfos,
1509 : subquery->rtable, subquery->rteperminfos);
1510 :
1511 : /*
1512 : * Pull up any FOR UPDATE/SHARE markers, too. (OffsetVarNodes already
1513 : * adjusted the marker rtindexes, so just concat the lists.)
1514 : */
1515 32930 : parse->rowMarks = list_concat(parse->rowMarks, subquery->rowMarks);
1516 :
1517 : /*
1518 : * We also have to fix the relid sets of any PlaceHolderVar nodes in the
1519 : * parent query. (This could perhaps be done by pullup_replace_vars(),
1520 : * but it seems cleaner to use two passes.) Note in particular that any
1521 : * PlaceHolderVar nodes just created by pullup_replace_vars() will be
1522 : * adjusted, so having created them with the subquery's varno is correct.
1523 : *
1524 : * Likewise, relids appearing in AppendRelInfo nodes have to be fixed. We
1525 : * already checked that this won't require introducing multiple subrelids
1526 : * into the single-slot AppendRelInfo structs.
1527 : */
1528 32930 : if (root->glob->lastPHId != 0 || root->append_rel_list)
1529 : {
1530 : Relids subrelids;
1531 :
1532 5342 : subrelids = get_relids_in_jointree((Node *) subquery->jointree,
1533 : true, false);
1534 5342 : if (root->glob->lastPHId != 0)
1535 1944 : substitute_phv_relids((Node *) parse, varno, subrelids);
1536 5342 : fix_append_rel_relids(root, varno, subrelids);
1537 : }
1538 :
1539 : /*
1540 : * And now add subquery's AppendRelInfos to our list.
1541 : */
1542 65860 : root->append_rel_list = list_concat(root->append_rel_list,
1543 32930 : subroot->append_rel_list);
1544 :
1545 : /*
1546 : * We don't have to do the equivalent bookkeeping for outer-join info,
1547 : * because that hasn't been set up yet. placeholder_list likewise.
1548 : */
1549 : Assert(root->join_info_list == NIL);
1550 : Assert(subroot->join_info_list == NIL);
1551 : Assert(root->placeholder_list == NIL);
1552 : Assert(subroot->placeholder_list == NIL);
1553 :
1554 : /*
1555 : * We no longer need the RTE's copy of the subquery's query tree. Getting
1556 : * rid of it saves nothing in particular so far as this level of query is
1557 : * concerned; but if this query level is in turn pulled up into a parent,
1558 : * we'd waste cycles copying the now-unused query tree.
1559 : */
1560 32930 : rte->subquery = NULL;
1561 :
1562 : /*
1563 : * Miscellaneous housekeeping.
1564 : *
1565 : * Although replace_rte_variables() faithfully updated parse->hasSubLinks
1566 : * if it copied any SubLinks out of the subquery's targetlist, we still
1567 : * could have SubLinks added to the query in the expressions of FUNCTION
1568 : * and VALUES RTEs copied up from the subquery. So it's necessary to copy
1569 : * subquery->hasSubLinks anyway. Perhaps this can be improved someday.
1570 : */
1571 32930 : parse->hasSubLinks |= subquery->hasSubLinks;
1572 :
1573 : /* If subquery had any RLS conditions, now main query does too */
1574 32930 : parse->hasRowSecurity |= subquery->hasRowSecurity;
1575 :
1576 : /*
1577 : * subquery won't be pulled up if it hasAggs, hasWindowFuncs, or
1578 : * hasTargetSRFs, so no work needed on those flags
1579 : */
1580 :
1581 : /*
1582 : * Return the adjusted subquery jointree to replace the RangeTblRef entry
1583 : * in parent's jointree; or, if the FromExpr is degenerate, just return
1584 : * its single member.
1585 : */
1586 : Assert(IsA(subquery->jointree, FromExpr));
1587 : Assert(subquery->jointree->fromlist != NIL);
1588 60908 : if (subquery->jointree->quals == NULL &&
1589 27978 : list_length(subquery->jointree->fromlist) == 1)
1590 27664 : return (Node *) linitial(subquery->jointree->fromlist);
1591 :
1592 5266 : return (Node *) subquery->jointree;
1593 : }
1594 :
1595 : /*
1596 : * pull_up_simple_union_all
1597 : * Pull up a single simple UNION ALL subquery.
1598 : *
1599 : * jtnode is a RangeTblRef that has been identified as a simple UNION ALL
1600 : * subquery by pull_up_subqueries. We pull up the leaf subqueries and
1601 : * build an "append relation" for the union set. The result value is just
1602 : * jtnode, since we don't actually need to change the query jointree.
1603 : */
1604 : static Node *
1605 1806 : pull_up_simple_union_all(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte)
1606 : {
1607 1806 : int varno = ((RangeTblRef *) jtnode)->rtindex;
1608 1806 : Query *subquery = rte->subquery;
1609 1806 : int rtoffset = list_length(root->parse->rtable);
1610 : List *rtable;
1611 :
1612 : /*
1613 : * Make a modifiable copy of the subquery's rtable, so we can adjust
1614 : * upper-level Vars in it. There are no such Vars in the setOperations
1615 : * tree proper, so fixing the rtable should be sufficient.
1616 : */
1617 1806 : rtable = copyObject(subquery->rtable);
1618 :
1619 : /*
1620 : * Upper-level vars in subquery are now one level closer to their parent
1621 : * than before. We don't have to worry about offsetting varnos, though,
1622 : * because the UNION leaf queries can't cross-reference each other.
1623 : */
1624 1806 : IncrementVarSublevelsUp_rtable(rtable, -1, 1);
1625 :
1626 : /*
1627 : * If the UNION ALL subquery had a LATERAL marker, propagate that to all
1628 : * its children. The individual children might or might not contain any
1629 : * actual lateral cross-references, but we have to mark the pulled-up
1630 : * child RTEs so that later planner stages will check for such.
1631 : */
1632 1806 : if (rte->lateral)
1633 : {
1634 : ListCell *rt;
1635 :
1636 198 : foreach(rt, rtable)
1637 : {
1638 132 : RangeTblEntry *child_rte = (RangeTblEntry *) lfirst(rt);
1639 :
1640 : Assert(child_rte->rtekind == RTE_SUBQUERY);
1641 132 : child_rte->lateral = true;
1642 : }
1643 : }
1644 :
1645 : /*
1646 : * Append child RTEs (and their perminfos) to parent rtable.
1647 : */
1648 1806 : CombineRangeTables(&root->parse->rtable, &root->parse->rteperminfos,
1649 : rtable, subquery->rteperminfos);
1650 :
1651 : /*
1652 : * Recursively scan the subquery's setOperations tree and add
1653 : * AppendRelInfo nodes for leaf subqueries to the parent's
1654 : * append_rel_list. Also apply pull_up_subqueries to the leaf subqueries.
1655 : */
1656 : Assert(subquery->setOperations);
1657 1806 : pull_up_union_leaf_queries(subquery->setOperations, root, varno, subquery,
1658 : rtoffset);
1659 :
1660 : /*
1661 : * Mark the parent as an append relation.
1662 : */
1663 1806 : rte->inh = true;
1664 :
1665 1806 : return jtnode;
1666 : }
1667 :
1668 : /*
1669 : * pull_up_union_leaf_queries -- recursive guts of pull_up_simple_union_all
1670 : *
1671 : * Build an AppendRelInfo for each leaf query in the setop tree, and then
1672 : * apply pull_up_subqueries to the leaf query.
1673 : *
1674 : * Note that setOpQuery is the Query containing the setOp node, whose tlist
1675 : * contains references to all the setop output columns. When called from
1676 : * pull_up_simple_union_all, this is *not* the same as root->parse, which is
1677 : * the parent Query we are pulling up into.
1678 : *
1679 : * parentRTindex is the appendrel parent's index in root->parse->rtable.
1680 : *
1681 : * The child RTEs have already been copied to the parent. childRToffset
1682 : * tells us where in the parent's range table they were copied. When called
1683 : * from flatten_simple_union_all, childRToffset is 0 since the child RTEs
1684 : * were already in root->parse->rtable and no RT index adjustment is needed.
1685 : */
1686 : static void
1687 8384 : pull_up_union_leaf_queries(Node *setOp, PlannerInfo *root, int parentRTindex,
1688 : Query *setOpQuery, int childRToffset)
1689 : {
1690 8384 : if (IsA(setOp, RangeTblRef))
1691 : {
1692 5310 : RangeTblRef *rtr = (RangeTblRef *) setOp;
1693 : int childRTindex;
1694 : AppendRelInfo *appinfo;
1695 :
1696 : /*
1697 : * Calculate the index in the parent's range table
1698 : */
1699 5310 : childRTindex = childRToffset + rtr->rtindex;
1700 :
1701 : /*
1702 : * Build a suitable AppendRelInfo, and attach to parent's list.
1703 : */
1704 5310 : appinfo = makeNode(AppendRelInfo);
1705 5310 : appinfo->parent_relid = parentRTindex;
1706 5310 : appinfo->child_relid = childRTindex;
1707 5310 : appinfo->parent_reltype = InvalidOid;
1708 5310 : appinfo->child_reltype = InvalidOid;
1709 5310 : make_setop_translation_list(setOpQuery, childRTindex, appinfo);
1710 5310 : appinfo->parent_reloid = InvalidOid;
1711 5310 : root->append_rel_list = lappend(root->append_rel_list, appinfo);
1712 :
1713 : /*
1714 : * Recursively apply pull_up_subqueries to the new child RTE. (We
1715 : * must build the AppendRelInfo first, because this will modify it;
1716 : * indeed, that's the only part of the upper query where Vars
1717 : * referencing childRTindex can exist at this point.)
1718 : *
1719 : * Note that we can pass NULL for containing-join info even if we're
1720 : * actually under an outer join, because the child's expressions
1721 : * aren't going to propagate up to the join. Also, we ignore the
1722 : * possibility that pull_up_subqueries_recurse() returns a different
1723 : * jointree node than what we pass it; if it does, the important thing
1724 : * is that it replaced the child relid in the AppendRelInfo node.
1725 : */
1726 5310 : rtr = makeNode(RangeTblRef);
1727 5310 : rtr->rtindex = childRTindex;
1728 5310 : (void) pull_up_subqueries_recurse(root, (Node *) rtr,
1729 : NULL, appinfo);
1730 : }
1731 3074 : else if (IsA(setOp, SetOperationStmt))
1732 : {
1733 3074 : SetOperationStmt *op = (SetOperationStmt *) setOp;
1734 :
1735 : /* Recurse to reach leaf queries */
1736 3074 : pull_up_union_leaf_queries(op->larg, root, parentRTindex, setOpQuery,
1737 : childRToffset);
1738 3074 : pull_up_union_leaf_queries(op->rarg, root, parentRTindex, setOpQuery,
1739 : childRToffset);
1740 : }
1741 : else
1742 : {
1743 0 : elog(ERROR, "unrecognized node type: %d",
1744 : (int) nodeTag(setOp));
1745 : }
1746 8384 : }
1747 :
1748 : /*
1749 : * make_setop_translation_list
1750 : * Build the list of translations from parent Vars to child Vars for
1751 : * a UNION ALL member. (At this point it's just a simple list of
1752 : * referencing Vars, but if we succeed in pulling up the member
1753 : * subquery, the Vars will get replaced by pulled-up expressions.)
1754 : * Also create the rather trivial reverse-translation array.
1755 : */
1756 : static void
1757 5310 : make_setop_translation_list(Query *query, int newvarno,
1758 : AppendRelInfo *appinfo)
1759 : {
1760 5310 : List *vars = NIL;
1761 : AttrNumber *pcolnos;
1762 : ListCell *l;
1763 :
1764 : /* Initialize reverse-translation array with all entries zero */
1765 : /* (entries for resjunk columns will stay that way) */
1766 5310 : appinfo->num_child_cols = list_length(query->targetList);
1767 5310 : appinfo->parent_colnos = pcolnos =
1768 5310 : (AttrNumber *) palloc0(appinfo->num_child_cols * sizeof(AttrNumber));
1769 :
1770 16398 : foreach(l, query->targetList)
1771 : {
1772 11088 : TargetEntry *tle = (TargetEntry *) lfirst(l);
1773 :
1774 11088 : if (tle->resjunk)
1775 0 : continue;
1776 :
1777 11088 : vars = lappend(vars, makeVarFromTargetEntry(newvarno, tle));
1778 11088 : pcolnos[tle->resno - 1] = tle->resno;
1779 : }
1780 :
1781 5310 : appinfo->translated_vars = vars;
1782 5310 : }
1783 :
1784 : /*
1785 : * is_simple_subquery
1786 : * Check a subquery in the range table to see if it's simple enough
1787 : * to pull up into the parent query.
1788 : *
1789 : * rte is the RTE_SUBQUERY RangeTblEntry that contained the subquery.
1790 : * (Note subquery is not necessarily equal to rte->subquery; it could be a
1791 : * processed copy of that.)
1792 : * lowest_outer_join is the lowest outer join above the subquery, or NULL.
1793 : */
1794 : static bool
1795 77740 : is_simple_subquery(PlannerInfo *root, Query *subquery, RangeTblEntry *rte,
1796 : JoinExpr *lowest_outer_join)
1797 : {
1798 : /*
1799 : * Let's just make sure it's a valid subselect ...
1800 : */
1801 77740 : if (!IsA(subquery, Query) ||
1802 77740 : subquery->commandType != CMD_SELECT)
1803 0 : elog(ERROR, "subquery is bogus");
1804 :
1805 : /*
1806 : * Can't currently pull up a query with setops (unless it's simple UNION
1807 : * ALL, which is handled by a different code path). Maybe after querytree
1808 : * redesign...
1809 : */
1810 77740 : if (subquery->setOperations)
1811 2546 : return false;
1812 :
1813 : /*
1814 : * Can't pull up a subquery involving grouping, aggregation, SRFs,
1815 : * sorting, limiting, or WITH. (XXX WITH could possibly be allowed later)
1816 : *
1817 : * We also don't pull up a subquery that has explicit FOR UPDATE/SHARE
1818 : * clauses, because pullup would cause the locking to occur semantically
1819 : * higher than it should. Implicit FOR UPDATE/SHARE is okay because in
1820 : * that case the locking was originally declared in the upper query
1821 : * anyway.
1822 : */
1823 75194 : if (subquery->hasAggs ||
1824 73676 : subquery->hasWindowFuncs ||
1825 73286 : subquery->hasTargetSRFs ||
1826 71598 : subquery->groupClause ||
1827 71518 : subquery->groupingSets ||
1828 71518 : subquery->havingQual ||
1829 71518 : subquery->sortClause ||
1830 70652 : subquery->distinctClause ||
1831 70394 : subquery->limitOffset ||
1832 69962 : subquery->limitCount ||
1833 69694 : subquery->hasForUpdate ||
1834 69164 : subquery->cteList)
1835 6192 : return false;
1836 :
1837 : /*
1838 : * Don't pull up if the RTE represents a security-barrier view; we
1839 : * couldn't prevent information leakage once the RTE's Vars are scattered
1840 : * about in the upper query.
1841 : */
1842 69002 : if (rte->security_barrier)
1843 1438 : return false;
1844 :
1845 : /*
1846 : * If the subquery is LATERAL, check for pullup restrictions from that.
1847 : */
1848 67564 : if (rte->lateral)
1849 : {
1850 : bool restricted;
1851 : Relids safe_upper_varnos;
1852 :
1853 : /*
1854 : * The subquery's WHERE and JOIN/ON quals mustn't contain any lateral
1855 : * references to rels outside a higher outer join (including the case
1856 : * where the outer join is within the subquery itself). In such a
1857 : * case, pulling up would result in a situation where we need to
1858 : * postpone quals from below an outer join to above it, which is
1859 : * probably completely wrong and in any case is a complication that
1860 : * doesn't seem worth addressing at the moment.
1861 : */
1862 2330 : if (lowest_outer_join != NULL)
1863 : {
1864 1312 : restricted = true;
1865 1312 : safe_upper_varnos = get_relids_in_jointree((Node *) lowest_outer_join,
1866 : true, true);
1867 : }
1868 : else
1869 : {
1870 1018 : restricted = false;
1871 1018 : safe_upper_varnos = NULL; /* doesn't matter */
1872 : }
1873 :
1874 2330 : if (jointree_contains_lateral_outer_refs(root,
1875 2330 : (Node *) subquery->jointree,
1876 : restricted, safe_upper_varnos))
1877 24 : return false;
1878 :
1879 : /*
1880 : * If there's an outer join above the LATERAL subquery, also disallow
1881 : * pullup if the subquery's targetlist has any references to rels
1882 : * outside the outer join, since these might get pulled into quals
1883 : * above the subquery (but in or below the outer join) and then lead
1884 : * to qual-postponement issues similar to the case checked for above.
1885 : * (We wouldn't need to prevent pullup if no such references appear in
1886 : * outer-query quals, but we don't have enough info here to check
1887 : * that. Also, maybe this restriction could be removed if we forced
1888 : * such refs to be wrapped in PlaceHolderVars, even when they're below
1889 : * the nearest outer join? But it's a pretty hokey usage, so not
1890 : * clear this is worth sweating over.)
1891 : *
1892 : * If you change this, see also the comments about lateral references
1893 : * in pullup_replace_vars_callback().
1894 : */
1895 2306 : if (lowest_outer_join != NULL)
1896 : {
1897 1312 : Relids lvarnos = pull_varnos_of_level(root,
1898 1312 : (Node *) subquery->targetList,
1899 : 1);
1900 :
1901 1312 : if (!bms_is_subset(lvarnos, safe_upper_varnos))
1902 12 : return false;
1903 : }
1904 : }
1905 :
1906 : /*
1907 : * Don't pull up a subquery that has any volatile functions in its
1908 : * targetlist. Otherwise we might introduce multiple evaluations of these
1909 : * functions, if they get copied to multiple places in the upper query,
1910 : * leading to surprising results. (Note: the PlaceHolderVar mechanism
1911 : * doesn't quite guarantee single evaluation; else we could pull up anyway
1912 : * and just wrap such items in PlaceHolderVars ...)
1913 : */
1914 67528 : if (contain_volatile_functions((Node *) subquery->targetList))
1915 256 : return false;
1916 :
1917 67272 : return true;
1918 : }
1919 :
1920 : /*
1921 : * pull_up_simple_values
1922 : * Pull up a single simple VALUES RTE.
1923 : *
1924 : * jtnode is a RangeTblRef that has been identified as a simple VALUES RTE
1925 : * by pull_up_subqueries. We always return a RangeTblRef representing a
1926 : * RESULT RTE to replace it (all failure cases should have been detected by
1927 : * is_simple_values()). Actually, what we return is just jtnode, because
1928 : * we replace the VALUES RTE in the rangetable with the RESULT RTE.
1929 : *
1930 : * rte is the RangeTblEntry referenced by jtnode. Because of the limited
1931 : * possible usage of VALUES RTEs, we do not need the remaining parameters
1932 : * of pull_up_subqueries_recurse.
1933 : */
1934 : static Node *
1935 1922 : pull_up_simple_values(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte)
1936 : {
1937 1922 : Query *parse = root->parse;
1938 1922 : int varno = ((RangeTblRef *) jtnode)->rtindex;
1939 : List *values_list;
1940 : List *tlist;
1941 : AttrNumber attrno;
1942 : pullup_replace_vars_context rvcontext;
1943 : ListCell *lc;
1944 :
1945 : Assert(rte->rtekind == RTE_VALUES);
1946 : Assert(list_length(rte->values_lists) == 1);
1947 :
1948 : /*
1949 : * Need a modifiable copy of the VALUES list to hack on, just in case it's
1950 : * multiply referenced.
1951 : */
1952 1922 : values_list = copyObject(linitial(rte->values_lists));
1953 :
1954 : /*
1955 : * The VALUES RTE can't contain any Vars of level zero, let alone any that
1956 : * are join aliases, so no need to flatten join alias Vars.
1957 : */
1958 : Assert(!contain_vars_of_level((Node *) values_list, 0));
1959 :
1960 : /*
1961 : * Set up required context data for pullup_replace_vars. In particular,
1962 : * we have to make the VALUES list look like a subquery targetlist.
1963 : */
1964 1922 : tlist = NIL;
1965 1922 : attrno = 1;
1966 4504 : foreach(lc, values_list)
1967 : {
1968 2582 : tlist = lappend(tlist,
1969 2582 : makeTargetEntry((Expr *) lfirst(lc),
1970 : attrno,
1971 : NULL,
1972 : false));
1973 2582 : attrno++;
1974 : }
1975 1922 : rvcontext.root = root;
1976 1922 : rvcontext.targetlist = tlist;
1977 1922 : rvcontext.target_rte = rte;
1978 1922 : rvcontext.result_relation = 0;
1979 1922 : rvcontext.relids = NULL; /* can't be any lateral references here */
1980 1922 : rvcontext.nullinfo = NULL;
1981 1922 : rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
1982 1922 : rvcontext.varno = varno;
1983 1922 : rvcontext.wrap_option = REPLACE_WRAP_NONE;
1984 : /* initialize cache array with indexes 0 .. length(tlist) */
1985 1922 : rvcontext.rv_cache = palloc0((list_length(tlist) + 1) *
1986 : sizeof(Node *));
1987 :
1988 : /*
1989 : * Replace all of the top query's references to the RTE's outputs with
1990 : * copies of the adjusted VALUES expressions, being careful not to replace
1991 : * any of the jointree structure. We can assume there's no outer joins or
1992 : * appendrels in the dummy Query that surrounds a VALUES RTE.
1993 : */
1994 1922 : perform_pullup_replace_vars(root, &rvcontext, NULL);
1995 :
1996 : /*
1997 : * There should be no appendrels to fix, nor any outer joins and hence no
1998 : * PlaceHolderVars.
1999 : */
2000 : Assert(root->append_rel_list == NIL);
2001 : Assert(root->join_info_list == NIL);
2002 : Assert(root->placeholder_list == NIL);
2003 :
2004 : /*
2005 : * Replace the VALUES RTE with a RESULT RTE. The VALUES RTE is the only
2006 : * rtable entry in the current query level, so this is easy.
2007 : */
2008 : Assert(list_length(parse->rtable) == 1);
2009 :
2010 : /* Create suitable RTE */
2011 1922 : rte = makeNode(RangeTblEntry);
2012 1922 : rte->rtekind = RTE_RESULT;
2013 1922 : rte->eref = makeAlias("*RESULT*", NIL);
2014 :
2015 : /* Replace rangetable */
2016 1922 : parse->rtable = list_make1(rte);
2017 :
2018 : /* We could manufacture a new RangeTblRef, but the one we have is fine */
2019 : Assert(varno == 1);
2020 :
2021 1922 : return jtnode;
2022 : }
2023 :
2024 : /*
2025 : * is_simple_values
2026 : * Check a VALUES RTE in the range table to see if it's simple enough
2027 : * to pull up into the parent query.
2028 : *
2029 : * rte is the RTE_VALUES RangeTblEntry to check.
2030 : */
2031 : static bool
2032 10104 : is_simple_values(PlannerInfo *root, RangeTblEntry *rte)
2033 : {
2034 : Assert(rte->rtekind == RTE_VALUES);
2035 :
2036 : /*
2037 : * There must be exactly one VALUES list, else it's not semantically
2038 : * correct to replace the VALUES RTE with a RESULT RTE, nor would we have
2039 : * a unique set of expressions to substitute into the parent query.
2040 : */
2041 10104 : if (list_length(rte->values_lists) != 1)
2042 8182 : return false;
2043 :
2044 : /*
2045 : * Because VALUES can't appear under an outer join (or at least, we won't
2046 : * try to pull it up if it does), we need not worry about LATERAL, nor
2047 : * about validity of PHVs for the VALUES' outputs.
2048 : */
2049 :
2050 : /*
2051 : * Don't pull up a VALUES that contains any set-returning or volatile
2052 : * functions. The considerations here are basically identical to the
2053 : * restrictions on a pull-able subquery's targetlist.
2054 : */
2055 3844 : if (expression_returns_set((Node *) rte->values_lists) ||
2056 1922 : contain_volatile_functions((Node *) rte->values_lists))
2057 0 : return false;
2058 :
2059 : /*
2060 : * Do not pull up a VALUES that's not the only RTE in its parent query.
2061 : * This is actually the only case that the parser will generate at the
2062 : * moment, and assuming this is true greatly simplifies
2063 : * pull_up_simple_values().
2064 : */
2065 1922 : if (list_length(root->parse->rtable) != 1 ||
2066 1922 : rte != (RangeTblEntry *) linitial(root->parse->rtable))
2067 0 : return false;
2068 :
2069 1922 : return true;
2070 : }
2071 :
2072 : /*
2073 : * pull_up_constant_function
2074 : * Pull up an RTE_FUNCTION expression that was simplified to a constant.
2075 : *
2076 : * jtnode is a RangeTblRef that has been identified as a FUNCTION RTE by
2077 : * pull_up_subqueries. If its expression is just a Const, hoist that value
2078 : * up into the parent query, and replace the RTE_FUNCTION with RTE_RESULT.
2079 : *
2080 : * In principle we could pull up any immutable expression, but we don't.
2081 : * That might result in multiple evaluations of the expression, which could
2082 : * be costly if it's not just a Const. Also, the main value of this is
2083 : * to let the constant participate in further const-folding, and of course
2084 : * that won't happen for a non-Const.
2085 : *
2086 : * The pulled-up value might need to be wrapped in a PlaceHolderVar if the
2087 : * RTE is below an outer join or is part of an appendrel; the extra
2088 : * parameters show whether that's needed.
2089 : */
2090 : static Node *
2091 52248 : pull_up_constant_function(PlannerInfo *root, Node *jtnode,
2092 : RangeTblEntry *rte,
2093 : AppendRelInfo *containing_appendrel)
2094 : {
2095 52248 : Query *parse = root->parse;
2096 : RangeTblFunction *rtf;
2097 : TypeFuncClass functypclass;
2098 : Oid funcrettype;
2099 : TupleDesc tupdesc;
2100 : pullup_replace_vars_context rvcontext;
2101 :
2102 : /* Fail if the RTE has ORDINALITY - we don't implement that here. */
2103 52248 : if (rte->funcordinality)
2104 698 : return jtnode;
2105 :
2106 : /* Fail if RTE isn't a single, simple Const expr */
2107 51550 : if (list_length(rte->functions) != 1)
2108 72 : return jtnode;
2109 51478 : rtf = linitial_node(RangeTblFunction, rte->functions);
2110 51478 : if (!IsA(rtf->funcexpr, Const))
2111 51106 : return jtnode;
2112 :
2113 : /*
2114 : * If the function's result is not a scalar, we punt. In principle we
2115 : * could break the composite constant value apart into per-column
2116 : * constants, but for now it seems not worth the work.
2117 : */
2118 372 : if (rtf->funccolcount != 1)
2119 30 : return jtnode; /* definitely composite */
2120 :
2121 : /* If it has a coldeflist, it certainly returns RECORD */
2122 342 : if (rtf->funccolnames != NIL)
2123 0 : return jtnode; /* must be a one-column RECORD type */
2124 :
2125 342 : functypclass = get_expr_result_type(rtf->funcexpr,
2126 : &funcrettype,
2127 : &tupdesc);
2128 342 : if (functypclass != TYPEFUNC_SCALAR)
2129 12 : return jtnode; /* must be a one-column composite type */
2130 :
2131 : /* Create context for applying pullup_replace_vars */
2132 330 : rvcontext.root = root;
2133 330 : rvcontext.targetlist = list_make1(makeTargetEntry((Expr *) rtf->funcexpr,
2134 : 1, /* resno */
2135 : NULL, /* resname */
2136 : false)); /* resjunk */
2137 330 : rvcontext.target_rte = rte;
2138 330 : rvcontext.result_relation = 0;
2139 :
2140 : /*
2141 : * Since this function was reduced to a Const, it doesn't contain any
2142 : * lateral references, even if it's marked as LATERAL. This means we
2143 : * don't need to fill relids or nullinfo.
2144 : */
2145 330 : rvcontext.relids = NULL;
2146 330 : rvcontext.nullinfo = NULL;
2147 :
2148 330 : rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
2149 330 : rvcontext.varno = ((RangeTblRef *) jtnode)->rtindex;
2150 : /* this flag will be set below, if needed */
2151 330 : rvcontext.wrap_option = REPLACE_WRAP_NONE;
2152 : /* initialize cache array with indexes 0 .. length(tlist) */
2153 330 : rvcontext.rv_cache = palloc0((list_length(rvcontext.targetlist) + 1) *
2154 : sizeof(Node *));
2155 :
2156 : /*
2157 : * If the parent query uses grouping sets, we need a PlaceHolderVar for
2158 : * each expression of the subquery's targetlist items. (See comments in
2159 : * pull_up_simple_subquery().)
2160 : */
2161 330 : if (parse->groupingSets)
2162 0 : rvcontext.wrap_option = REPLACE_WRAP_ALL;
2163 :
2164 : /*
2165 : * Replace all of the top query's references to the RTE's output with
2166 : * copies of the funcexpr, being careful not to replace any of the
2167 : * jointree structure.
2168 : */
2169 330 : perform_pullup_replace_vars(root, &rvcontext,
2170 : containing_appendrel);
2171 :
2172 : /*
2173 : * We don't need to bother with changing PlaceHolderVars in the parent
2174 : * query. Their references to the RT index are still good for now, and
2175 : * will get removed later if we're able to drop the RTE_RESULT.
2176 : */
2177 :
2178 : /*
2179 : * Convert the RTE to be RTE_RESULT type, signifying that we don't need to
2180 : * scan it anymore, and zero out RTE_FUNCTION-specific fields. Also make
2181 : * sure the RTE is not marked LATERAL, since elsewhere we don't expect
2182 : * RTE_RESULTs to be LATERAL.
2183 : */
2184 330 : rte->rtekind = RTE_RESULT;
2185 330 : rte->functions = NIL;
2186 330 : rte->lateral = false;
2187 :
2188 : /*
2189 : * We can reuse the RangeTblRef node.
2190 : */
2191 330 : return jtnode;
2192 : }
2193 :
2194 : /*
2195 : * is_simple_union_all
2196 : * Check a subquery to see if it's a simple UNION ALL.
2197 : *
2198 : * We require all the setops to be UNION ALL (no mixing) and there can't be
2199 : * any datatype coercions involved, ie, all the leaf queries must emit the
2200 : * same datatypes.
2201 : */
2202 : static bool
2203 11464 : is_simple_union_all(Query *subquery)
2204 : {
2205 : SetOperationStmt *topop;
2206 :
2207 : /* Let's just make sure it's a valid subselect ... */
2208 11464 : if (!IsA(subquery, Query) ||
2209 11464 : subquery->commandType != CMD_SELECT)
2210 0 : elog(ERROR, "subquery is bogus");
2211 :
2212 : /* Is it a set-operation query at all? */
2213 11464 : topop = castNode(SetOperationStmt, subquery->setOperations);
2214 11464 : if (!topop)
2215 8918 : return false;
2216 :
2217 : /* Can't handle ORDER BY, LIMIT/OFFSET, locking, or WITH */
2218 2546 : if (subquery->sortClause ||
2219 2482 : subquery->limitOffset ||
2220 2482 : subquery->limitCount ||
2221 2482 : subquery->rowMarks ||
2222 2482 : subquery->cteList)
2223 224 : return false;
2224 :
2225 : /* Recursively check the tree of set operations */
2226 2322 : return is_simple_union_all_recurse((Node *) topop, subquery,
2227 : topop->colTypes);
2228 : }
2229 :
2230 : static bool
2231 15620 : is_simple_union_all_recurse(Node *setOp, Query *setOpQuery, List *colTypes)
2232 : {
2233 : /* Since this function recurses, it could be driven to stack overflow. */
2234 15620 : check_stack_depth();
2235 :
2236 15620 : if (IsA(setOp, RangeTblRef))
2237 : {
2238 6582 : RangeTblRef *rtr = (RangeTblRef *) setOp;
2239 6582 : RangeTblEntry *rte = rt_fetch(rtr->rtindex, setOpQuery->rtable);
2240 6582 : Query *subquery = rte->subquery;
2241 :
2242 : Assert(subquery != NULL);
2243 :
2244 : /* Leaf nodes are OK if they match the toplevel column types */
2245 : /* We don't have to compare typmods or collations here */
2246 6582 : return tlist_same_datatypes(subquery->targetList, colTypes, true);
2247 : }
2248 9038 : else if (IsA(setOp, SetOperationStmt))
2249 : {
2250 9038 : SetOperationStmt *op = (SetOperationStmt *) setOp;
2251 :
2252 : /* Must be UNION ALL */
2253 9038 : if (op->op != SETOP_UNION || !op->all)
2254 4764 : return false;
2255 :
2256 : /* Recurse to check inputs */
2257 7874 : return is_simple_union_all_recurse(op->larg, setOpQuery, colTypes) &&
2258 3600 : is_simple_union_all_recurse(op->rarg, setOpQuery, colTypes);
2259 : }
2260 : else
2261 : {
2262 0 : elog(ERROR, "unrecognized node type: %d",
2263 : (int) nodeTag(setOp));
2264 : return false; /* keep compiler quiet */
2265 : }
2266 : }
2267 :
2268 : /*
2269 : * is_safe_append_member
2270 : * Check a subquery that is a leaf of a UNION ALL appendrel to see if it's
2271 : * safe to pull up.
2272 : */
2273 : static bool
2274 8292 : is_safe_append_member(Query *subquery)
2275 : {
2276 : FromExpr *jtnode;
2277 :
2278 : /*
2279 : * It's only safe to pull up the child if its jointree contains exactly
2280 : * one RTE, else the AppendRelInfo data structure breaks. The one base RTE
2281 : * could be buried in several levels of FromExpr, however. Also, if the
2282 : * child's jointree is completely empty, we can pull up because
2283 : * pull_up_simple_subquery will insert a single RTE_RESULT RTE instead.
2284 : *
2285 : * Also, the child can't have any WHERE quals because there's no place to
2286 : * put them in an appendrel. (This is a bit annoying...) If we didn't
2287 : * need to check this, we'd just test whether get_relids_in_jointree()
2288 : * yields a singleton set, to be more consistent with the coding of
2289 : * fix_append_rel_relids().
2290 : */
2291 8292 : jtnode = subquery->jointree;
2292 : Assert(IsA(jtnode, FromExpr));
2293 : /* Check the completely-empty case */
2294 8292 : if (jtnode->fromlist == NIL && jtnode->quals == NULL)
2295 548 : return true;
2296 : /* Check the more general case */
2297 14530 : while (IsA(jtnode, FromExpr))
2298 : {
2299 7756 : if (jtnode->quals != NULL)
2300 970 : return false;
2301 6786 : if (list_length(jtnode->fromlist) != 1)
2302 0 : return false;
2303 6786 : jtnode = linitial(jtnode->fromlist);
2304 : }
2305 6774 : if (!IsA(jtnode, RangeTblRef))
2306 228 : return false;
2307 :
2308 6546 : return true;
2309 : }
2310 :
2311 : /*
2312 : * jointree_contains_lateral_outer_refs
2313 : * Check for disallowed lateral references in a jointree's quals
2314 : *
2315 : * If restricted is false, all level-1 Vars are allowed (but we still must
2316 : * search the jointree, since it might contain outer joins below which there
2317 : * will be restrictions). If restricted is true, return true when any qual
2318 : * in the jointree contains level-1 Vars coming from outside the rels listed
2319 : * in safe_upper_varnos.
2320 : */
2321 : static bool
2322 4986 : jointree_contains_lateral_outer_refs(PlannerInfo *root, Node *jtnode,
2323 : bool restricted,
2324 : Relids safe_upper_varnos)
2325 : {
2326 4986 : if (jtnode == NULL)
2327 0 : return false;
2328 4986 : if (IsA(jtnode, RangeTblRef))
2329 2318 : return false;
2330 2668 : else if (IsA(jtnode, FromExpr))
2331 : {
2332 2366 : FromExpr *f = (FromExpr *) jtnode;
2333 : ListCell *l;
2334 :
2335 : /* First, recurse to check child joins */
2336 4394 : foreach(l, f->fromlist)
2337 : {
2338 2052 : if (jointree_contains_lateral_outer_refs(root,
2339 2052 : lfirst(l),
2340 : restricted,
2341 : safe_upper_varnos))
2342 24 : return true;
2343 : }
2344 :
2345 : /* Then check the top-level quals */
2346 2342 : if (restricted &&
2347 1348 : !bms_is_subset(pull_varnos_of_level(root, f->quals, 1),
2348 : safe_upper_varnos))
2349 0 : return true;
2350 : }
2351 302 : else if (IsA(jtnode, JoinExpr))
2352 : {
2353 302 : JoinExpr *j = (JoinExpr *) jtnode;
2354 :
2355 : /*
2356 : * If this is an outer join, we mustn't allow any upper lateral
2357 : * references in or below it.
2358 : */
2359 302 : if (j->jointype != JOIN_INNER)
2360 : {
2361 146 : restricted = true;
2362 146 : safe_upper_varnos = NULL;
2363 : }
2364 :
2365 : /* Check the child joins */
2366 302 : if (jointree_contains_lateral_outer_refs(root,
2367 : j->larg,
2368 : restricted,
2369 : safe_upper_varnos))
2370 0 : return true;
2371 302 : if (jointree_contains_lateral_outer_refs(root,
2372 : j->rarg,
2373 : restricted,
2374 : safe_upper_varnos))
2375 0 : return true;
2376 :
2377 : /* Check the JOIN's qual clauses */
2378 302 : if (restricted &&
2379 278 : !bms_is_subset(pull_varnos_of_level(root, j->quals, 1),
2380 : safe_upper_varnos))
2381 24 : return true;
2382 : }
2383 : else
2384 0 : elog(ERROR, "unrecognized node type: %d",
2385 : (int) nodeTag(jtnode));
2386 2620 : return false;
2387 : }
2388 :
2389 : /*
2390 : * Perform pullup_replace_vars everyplace it's needed in the query tree.
2391 : *
2392 : * Caller has already filled *rvcontext with data describing what to
2393 : * substitute for Vars referencing the target subquery. In addition
2394 : * we need the identity of the containing appendrel if any.
2395 : */
2396 : static void
2397 35188 : perform_pullup_replace_vars(PlannerInfo *root,
2398 : pullup_replace_vars_context *rvcontext,
2399 : AppendRelInfo *containing_appendrel)
2400 : {
2401 35188 : Query *parse = root->parse;
2402 : ListCell *lc;
2403 :
2404 : /*
2405 : * If we are considering an appendrel child subquery (that is, a UNION ALL
2406 : * member query that we're pulling up), then the only part of the upper
2407 : * query that could reference the child yet is the translated_vars list of
2408 : * the associated AppendRelInfo. Furthermore, we do not want to force use
2409 : * of PHVs in the AppendRelInfo --- there isn't any outer join between.
2410 : */
2411 35188 : if (containing_appendrel)
2412 : {
2413 3452 : ReplaceWrapOption save_wrap_option = rvcontext->wrap_option;
2414 :
2415 3452 : rvcontext->wrap_option = REPLACE_WRAP_NONE;
2416 3452 : containing_appendrel->translated_vars = (List *)
2417 3452 : pullup_replace_vars((Node *) containing_appendrel->translated_vars,
2418 : rvcontext);
2419 3452 : rvcontext->wrap_option = save_wrap_option;
2420 3452 : return;
2421 : }
2422 :
2423 : /*
2424 : * Replace all of the top query's references to the subquery's outputs
2425 : * with copies of the adjusted subtlist items, being careful not to
2426 : * replace any of the jointree structure. (This'd be a lot cleaner if we
2427 : * could use query_tree_mutator.) We have to use PHVs in the targetList,
2428 : * returningList, and havingQual, since those are certainly above any
2429 : * outer join. replace_vars_in_jointree tracks its location in the
2430 : * jointree and uses PHVs or not appropriately.
2431 : */
2432 31736 : parse->targetList = (List *)
2433 31736 : pullup_replace_vars((Node *) parse->targetList, rvcontext);
2434 31736 : parse->returningList = (List *)
2435 31736 : pullup_replace_vars((Node *) parse->returningList, rvcontext);
2436 :
2437 31736 : if (parse->onConflict)
2438 : {
2439 44 : parse->onConflict->onConflictSet = (List *)
2440 22 : pullup_replace_vars((Node *) parse->onConflict->onConflictSet,
2441 : rvcontext);
2442 22 : parse->onConflict->onConflictWhere =
2443 22 : pullup_replace_vars(parse->onConflict->onConflictWhere,
2444 : rvcontext);
2445 :
2446 : /*
2447 : * We assume ON CONFLICT's arbiterElems, arbiterWhere, exclRelTlist
2448 : * can't contain any references to a subquery.
2449 : */
2450 : }
2451 31736 : if (parse->mergeActionList)
2452 : {
2453 2840 : foreach(lc, parse->mergeActionList)
2454 : {
2455 1682 : MergeAction *action = lfirst(lc);
2456 :
2457 1682 : action->qual = pullup_replace_vars(action->qual, rvcontext);
2458 1682 : action->targetList = (List *)
2459 1682 : pullup_replace_vars((Node *) action->targetList, rvcontext);
2460 : }
2461 : }
2462 31736 : parse->mergeJoinCondition = pullup_replace_vars(parse->mergeJoinCondition,
2463 : rvcontext);
2464 31736 : replace_vars_in_jointree((Node *) parse->jointree, rvcontext);
2465 : Assert(parse->setOperations == NULL);
2466 31730 : parse->havingQual = pullup_replace_vars(parse->havingQual, rvcontext);
2467 :
2468 : /*
2469 : * Replace references in the translated_vars lists of appendrels.
2470 : */
2471 31742 : foreach(lc, root->append_rel_list)
2472 : {
2473 12 : AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(lc);
2474 :
2475 12 : appinfo->translated_vars = (List *)
2476 12 : pullup_replace_vars((Node *) appinfo->translated_vars, rvcontext);
2477 : }
2478 :
2479 : /*
2480 : * Replace references in the joinaliasvars lists of join RTEs and the
2481 : * groupexprs list of group RTE.
2482 : */
2483 91328 : foreach(lc, parse->rtable)
2484 : {
2485 59598 : RangeTblEntry *otherrte = (RangeTblEntry *) lfirst(lc);
2486 :
2487 59598 : if (otherrte->rtekind == RTE_JOIN)
2488 6750 : otherrte->joinaliasvars = (List *)
2489 6750 : pullup_replace_vars((Node *) otherrte->joinaliasvars,
2490 : rvcontext);
2491 52848 : else if (otherrte->rtekind == RTE_GROUP)
2492 736 : otherrte->groupexprs = (List *)
2493 736 : pullup_replace_vars((Node *) otherrte->groupexprs,
2494 : rvcontext);
2495 : }
2496 : }
2497 :
2498 : /*
2499 : * Helper routine for perform_pullup_replace_vars: do pullup_replace_vars on
2500 : * every expression in the jointree, without changing the jointree structure
2501 : * itself. Ugly, but there's no other way...
2502 : */
2503 : static void
2504 84634 : replace_vars_in_jointree(Node *jtnode,
2505 : pullup_replace_vars_context *context)
2506 : {
2507 84634 : if (jtnode == NULL)
2508 0 : return;
2509 84634 : if (IsA(jtnode, RangeTblRef))
2510 : {
2511 : /*
2512 : * If the RangeTblRef refers to a LATERAL subquery (that isn't the
2513 : * same subquery we're pulling up), it might contain references to the
2514 : * target subquery, which we must replace. We drive this from the
2515 : * jointree scan, rather than a scan of the rtable, so that we can
2516 : * avoid processing no-longer-referenced RTEs.
2517 : */
2518 42518 : int varno = ((RangeTblRef *) jtnode)->rtindex;
2519 :
2520 42518 : if (varno != context->varno) /* ignore target subquery itself */
2521 : {
2522 10782 : RangeTblEntry *rte = rt_fetch(varno, context->root->parse->rtable);
2523 :
2524 : Assert(rte != context->target_rte);
2525 10782 : if (rte->lateral)
2526 : {
2527 866 : switch (rte->rtekind)
2528 : {
2529 0 : case RTE_RELATION:
2530 : /* shouldn't be marked LATERAL unless tablesample */
2531 : Assert(rte->tablesample);
2532 0 : rte->tablesample = (TableSampleClause *)
2533 0 : pullup_replace_vars((Node *) rte->tablesample,
2534 : context);
2535 0 : break;
2536 408 : case RTE_SUBQUERY:
2537 408 : rte->subquery =
2538 408 : pullup_replace_vars_subquery(rte->subquery,
2539 : context);
2540 408 : break;
2541 350 : case RTE_FUNCTION:
2542 350 : rte->functions = (List *)
2543 350 : pullup_replace_vars((Node *) rte->functions,
2544 : context);
2545 350 : break;
2546 108 : case RTE_TABLEFUNC:
2547 108 : rte->tablefunc = (TableFunc *)
2548 108 : pullup_replace_vars((Node *) rte->tablefunc,
2549 : context);
2550 108 : break;
2551 0 : case RTE_VALUES:
2552 0 : rte->values_lists = (List *)
2553 0 : pullup_replace_vars((Node *) rte->values_lists,
2554 : context);
2555 0 : break;
2556 0 : case RTE_JOIN:
2557 : case RTE_CTE:
2558 : case RTE_NAMEDTUPLESTORE:
2559 : case RTE_RESULT:
2560 : case RTE_GROUP:
2561 : /* these shouldn't be marked LATERAL */
2562 : Assert(false);
2563 0 : break;
2564 : }
2565 42518 : }
2566 : }
2567 : }
2568 42116 : else if (IsA(jtnode, FromExpr))
2569 : {
2570 34264 : FromExpr *f = (FromExpr *) jtnode;
2571 : ListCell *l;
2572 :
2573 71458 : foreach(l, f->fromlist)
2574 37194 : replace_vars_in_jointree(lfirst(l), context);
2575 34264 : f->quals = pullup_replace_vars(f->quals, context);
2576 : }
2577 7852 : else if (IsA(jtnode, JoinExpr))
2578 : {
2579 7852 : JoinExpr *j = (JoinExpr *) jtnode;
2580 7852 : ReplaceWrapOption save_wrap_option = context->wrap_option;
2581 :
2582 7852 : replace_vars_in_jointree(j->larg, context);
2583 7852 : replace_vars_in_jointree(j->rarg, context);
2584 :
2585 : /*
2586 : * Use PHVs within the join quals of a full join for variable-free
2587 : * expressions. Otherwise, we cannot identify which side of the join
2588 : * a pulled-up variable-free expression came from, which can lead to
2589 : * failure to make a plan at all because none of the quals appear to
2590 : * be mergeable or hashable conditions.
2591 : */
2592 7852 : if (j->jointype == JOIN_FULL)
2593 622 : context->wrap_option = REPLACE_WRAP_VARFREE;
2594 :
2595 7852 : j->quals = pullup_replace_vars(j->quals, context);
2596 :
2597 7852 : context->wrap_option = save_wrap_option;
2598 : }
2599 : else
2600 0 : elog(ERROR, "unrecognized node type: %d",
2601 : (int) nodeTag(jtnode));
2602 : }
2603 :
2604 : /*
2605 : * Apply pullup variable replacement throughout an expression tree
2606 : *
2607 : * Returns a modified copy of the tree, so this can't be used where we
2608 : * need to do in-place replacement.
2609 : */
2610 : static Node *
2611 184928 : pullup_replace_vars(Node *expr, pullup_replace_vars_context *context)
2612 : {
2613 184928 : return replace_rte_variables(expr,
2614 : context->varno, 0,
2615 : pullup_replace_vars_callback,
2616 : context,
2617 : context->outer_hasSubLinks);
2618 : }
2619 :
2620 : static Node *
2621 107804 : pullup_replace_vars_callback(Var *var,
2622 : replace_rte_variables_context *context)
2623 : {
2624 107804 : pullup_replace_vars_context *rcon = (pullup_replace_vars_context *) context->callback_arg;
2625 107804 : int varattno = var->varattno;
2626 : bool need_phv;
2627 : Node *newnode;
2628 :
2629 : /* System columns are not replaced. */
2630 107804 : if (varattno < InvalidAttrNumber)
2631 42 : return (Node *) copyObject(var);
2632 :
2633 : /*
2634 : * We need a PlaceHolderVar if the Var-to-be-replaced has nonempty
2635 : * varnullingrels (unless we find below that the replacement expression is
2636 : * a Var or PlaceHolderVar that we can just add the nullingrels to). We
2637 : * also need one if the caller has instructed us that certain expression
2638 : * replacements need to be wrapped for identification purposes.
2639 : */
2640 202784 : need_phv = (var->varnullingrels != NULL) ||
2641 95022 : (rcon->wrap_option != REPLACE_WRAP_NONE);
2642 :
2643 : /*
2644 : * If PlaceHolderVars are needed, we cache the modified expressions in
2645 : * rcon->rv_cache[]. This is not in hopes of any material speed gain
2646 : * within this function, but to avoid generating identical PHVs with
2647 : * different IDs. That would result in duplicate evaluations at runtime,
2648 : * and possibly prevent optimizations that rely on recognizing different
2649 : * references to the same subquery output as being equal(). So it's worth
2650 : * a bit of extra effort to avoid it.
2651 : *
2652 : * The cached items have phlevelsup = 0 and phnullingrels = NULL; we'll
2653 : * copy them and adjust those values for this reference site below.
2654 : */
2655 107762 : if (need_phv &&
2656 14688 : varattno >= InvalidAttrNumber &&
2657 14688 : varattno <= list_length(rcon->targetlist) &&
2658 14688 : rcon->rv_cache[varattno] != NULL)
2659 : {
2660 : /* Just copy the entry and fall through to adjust phlevelsup etc */
2661 2744 : newnode = copyObject(rcon->rv_cache[varattno]);
2662 : }
2663 : else
2664 : {
2665 : /*
2666 : * Generate the replacement expression. This takes care of expanding
2667 : * wholerow references and dealing with non-default varreturningtype.
2668 : */
2669 105018 : newnode = ReplaceVarFromTargetList(var,
2670 : rcon->target_rte,
2671 : rcon->targetlist,
2672 : rcon->result_relation,
2673 : REPLACEVARS_REPORT_ERROR,
2674 : 0);
2675 :
2676 : /* Insert PlaceHolderVar if needed */
2677 105018 : if (need_phv)
2678 : {
2679 : bool wrap;
2680 :
2681 11944 : if (rcon->wrap_option == REPLACE_WRAP_ALL)
2682 : {
2683 : /* Caller told us to wrap all expressions in a PlaceHolderVar */
2684 788 : wrap = true;
2685 : }
2686 11156 : else if (varattno == InvalidAttrNumber)
2687 : {
2688 : /*
2689 : * Insert PlaceHolderVar for whole-tuple reference. Notice
2690 : * that we are wrapping one PlaceHolderVar around the whole
2691 : * RowExpr, rather than putting one around each element of the
2692 : * row. This is because we need the expression to yield NULL,
2693 : * not ROW(NULL,NULL,...) when it is forced to null by an
2694 : * outer join.
2695 : */
2696 66 : wrap = true;
2697 : }
2698 11090 : else if (newnode && IsA(newnode, Var) &&
2699 9096 : ((Var *) newnode)->varlevelsup == 0)
2700 : {
2701 : /*
2702 : * Simple Vars always escape being wrapped, unless they are
2703 : * lateral references to something outside the subquery being
2704 : * pulled up and the referenced rel is not under the same
2705 : * lowest nulling outer join.
2706 : */
2707 9080 : wrap = false;
2708 9080 : if (rcon->target_rte->lateral &&
2709 1410 : !bms_is_member(((Var *) newnode)->varno, rcon->relids))
2710 : {
2711 132 : nullingrel_info *nullinfo = rcon->nullinfo;
2712 132 : int lvarno = ((Var *) newnode)->varno;
2713 :
2714 : Assert(lvarno > 0 && lvarno <= nullinfo->rtlength);
2715 132 : if (!bms_is_subset(nullinfo->nullingrels[rcon->varno],
2716 132 : nullinfo->nullingrels[lvarno]))
2717 108 : wrap = true;
2718 : }
2719 : }
2720 2010 : else if (newnode && IsA(newnode, PlaceHolderVar) &&
2721 180 : ((PlaceHolderVar *) newnode)->phlevelsup == 0)
2722 : {
2723 : /* The same rules apply for a PlaceHolderVar */
2724 180 : wrap = false;
2725 180 : if (rcon->target_rte->lateral &&
2726 48 : !bms_is_subset(((PlaceHolderVar *) newnode)->phrels,
2727 48 : rcon->relids))
2728 : {
2729 48 : nullingrel_info *nullinfo = rcon->nullinfo;
2730 48 : Relids lvarnos = ((PlaceHolderVar *) newnode)->phrels;
2731 : int lvarno;
2732 :
2733 48 : lvarno = -1;
2734 72 : while ((lvarno = bms_next_member(lvarnos, lvarno)) >= 0)
2735 : {
2736 : Assert(lvarno > 0 && lvarno <= nullinfo->rtlength);
2737 48 : if (!bms_is_subset(nullinfo->nullingrels[rcon->varno],
2738 48 : nullinfo->nullingrels[lvarno]))
2739 : {
2740 24 : wrap = true;
2741 24 : break;
2742 : }
2743 : }
2744 : }
2745 : }
2746 : else
2747 : {
2748 : /*
2749 : * If the node contains Var(s) or PlaceHolderVar(s) of the
2750 : * subquery being pulled up, or of rels that are under the
2751 : * same lowest nulling outer join as the subquery, and does
2752 : * not contain any non-strict constructs, then instead of
2753 : * adding a PHV on top we can add the required nullingrels to
2754 : * those Vars/PHVs. (This is fundamentally a generalization
2755 : * of the above cases for bare Vars and PHVs.)
2756 : *
2757 : * This test is somewhat expensive, but it avoids pessimizing
2758 : * the plan in cases where the nullingrels get removed again
2759 : * later by outer join reduction.
2760 : *
2761 : * Note that we don't force wrapping of expressions containing
2762 : * lateral references, so long as they also contain Vars/PHVs
2763 : * of the subquery, or of rels that are under the same lowest
2764 : * nulling outer join as the subquery. This is okay because
2765 : * of the restriction to strict constructs: if those Vars/PHVs
2766 : * have been forced to NULL by an outer join then the end
2767 : * result of the expression will be NULL too, regardless of
2768 : * the lateral references. So it's not necessary to force the
2769 : * expression to be evaluated below the outer join. This can
2770 : * be a very valuable optimization, because it may allow us to
2771 : * avoid using a nested loop to pass the lateral reference
2772 : * down.
2773 : *
2774 : * This analysis could be tighter: in particular, a non-strict
2775 : * construct hidden within a lower-level PlaceHolderVar is not
2776 : * reason to add another PHV. But for now it doesn't seem
2777 : * worth the code to be more exact. This is also why it's
2778 : * preferable to handle bare PHVs in the above branch, rather
2779 : * than this branch. We also prefer to handle bare Vars in a
2780 : * separate branch, as it's cheaper this way and parallels the
2781 : * handling of PHVs.
2782 : *
2783 : * For a LATERAL subquery, we have to check the actual var
2784 : * membership of the node, but if it's non-lateral then any
2785 : * level-zero var must belong to the subquery.
2786 : */
2787 1830 : bool contain_nullable_vars = false;
2788 :
2789 1830 : if (!rcon->target_rte->lateral)
2790 : {
2791 1602 : if (contain_vars_of_level(newnode, 0))
2792 510 : contain_nullable_vars = true;
2793 : }
2794 : else
2795 : {
2796 : Relids all_varnos;
2797 :
2798 228 : all_varnos = pull_varnos(rcon->root, newnode);
2799 228 : if (bms_overlap(all_varnos, rcon->relids))
2800 132 : contain_nullable_vars = true;
2801 : else
2802 : {
2803 96 : nullingrel_info *nullinfo = rcon->nullinfo;
2804 : int varno;
2805 :
2806 96 : varno = -1;
2807 180 : while ((varno = bms_next_member(all_varnos, varno)) >= 0)
2808 : {
2809 : Assert(varno > 0 && varno <= nullinfo->rtlength);
2810 108 : if (bms_is_subset(nullinfo->nullingrels[rcon->varno],
2811 108 : nullinfo->nullingrels[varno]))
2812 : {
2813 24 : contain_nullable_vars = true;
2814 24 : break;
2815 : }
2816 : }
2817 : }
2818 : }
2819 :
2820 1830 : if (contain_nullable_vars &&
2821 666 : !contain_nonstrict_functions(newnode))
2822 : {
2823 : /* No wrap needed */
2824 288 : wrap = false;
2825 : }
2826 : else
2827 : {
2828 : /* Else wrap it in a PlaceHolderVar */
2829 1542 : wrap = true;
2830 : }
2831 : }
2832 :
2833 11944 : if (wrap)
2834 : {
2835 : newnode = (Node *)
2836 2528 : make_placeholder_expr(rcon->root,
2837 : (Expr *) newnode,
2838 : bms_make_singleton(rcon->varno));
2839 :
2840 : /*
2841 : * Cache it if possible (ie, if the attno is in range, which
2842 : * it probably always should be).
2843 : */
2844 5056 : if (varattno >= InvalidAttrNumber &&
2845 2528 : varattno <= list_length(rcon->targetlist))
2846 2528 : rcon->rv_cache[varattno] = copyObject(newnode);
2847 : }
2848 : }
2849 : }
2850 :
2851 : /* Propagate any varnullingrels into the replacement expression */
2852 107762 : if (var->varnullingrels != NULL)
2853 : {
2854 12740 : if (IsA(newnode, Var))
2855 : {
2856 8482 : Var *newvar = (Var *) newnode;
2857 :
2858 : Assert(newvar->varlevelsup == 0);
2859 8482 : newvar->varnullingrels = bms_add_members(newvar->varnullingrels,
2860 8482 : var->varnullingrels);
2861 : }
2862 4258 : else if (IsA(newnode, PlaceHolderVar))
2863 : {
2864 3970 : PlaceHolderVar *newphv = (PlaceHolderVar *) newnode;
2865 :
2866 : Assert(newphv->phlevelsup == 0);
2867 3970 : newphv->phnullingrels = bms_add_members(newphv->phnullingrels,
2868 3970 : var->varnullingrels);
2869 : }
2870 : else
2871 : {
2872 : /*
2873 : * There should be Vars/PHVs within the expression that we can
2874 : * modify. Vars/PHVs of the subquery should have the full
2875 : * var->varnullingrels added to them, but if there are lateral
2876 : * references within the expression, those must be marked with
2877 : * only the nullingrels that potentially apply to them. (This
2878 : * corresponds to the fact that the expression will now be
2879 : * evaluated at the join level of the Var that we are replacing:
2880 : * the lateral references may have bubbled up through fewer outer
2881 : * joins than the subquery's Vars have. Per the discussion above,
2882 : * we'll still get the right answers.) That relid set could be
2883 : * different for different lateral relations, so we have to do
2884 : * this work for each one.
2885 : *
2886 : * (Currently, the restrictions in is_simple_subquery() mean that
2887 : * at most we have to remove the lowest outer join's relid from
2888 : * the nullingrels of a lateral reference. However, we might
2889 : * relax those restrictions someday, so let's do this right.)
2890 : */
2891 288 : if (rcon->target_rte->lateral)
2892 : {
2893 84 : nullingrel_info *nullinfo = rcon->nullinfo;
2894 : Relids lvarnos;
2895 : int lvarno;
2896 :
2897 : /*
2898 : * Identify lateral varnos used within newnode. We must do
2899 : * this before injecting var->varnullingrels into the tree.
2900 : */
2901 84 : lvarnos = pull_varnos(rcon->root, newnode);
2902 84 : lvarnos = bms_del_members(lvarnos, rcon->relids);
2903 : /* For each one, add relevant nullingrels if any */
2904 84 : lvarno = -1;
2905 168 : while ((lvarno = bms_next_member(lvarnos, lvarno)) >= 0)
2906 : {
2907 : Relids lnullingrels;
2908 :
2909 : Assert(lvarno > 0 && lvarno <= nullinfo->rtlength);
2910 84 : lnullingrels = bms_intersect(var->varnullingrels,
2911 84 : nullinfo->nullingrels[lvarno]);
2912 84 : if (!bms_is_empty(lnullingrels))
2913 48 : newnode = add_nulling_relids(newnode,
2914 48 : bms_make_singleton(lvarno),
2915 : lnullingrels);
2916 : }
2917 : }
2918 :
2919 : /* Finally, deal with Vars/PHVs of the subquery itself */
2920 288 : newnode = add_nulling_relids(newnode,
2921 288 : rcon->relids,
2922 288 : var->varnullingrels);
2923 : /* Assert we did put the varnullingrels into the expression */
2924 : Assert(bms_is_subset(var->varnullingrels,
2925 : pull_varnos(rcon->root, newnode)));
2926 : }
2927 : }
2928 :
2929 : /* Must adjust varlevelsup if replaced Var is within a subquery */
2930 107762 : if (var->varlevelsup > 0)
2931 1172 : IncrementVarSublevelsUp(newnode, var->varlevelsup, 0);
2932 :
2933 107762 : return newnode;
2934 : }
2935 :
2936 : /*
2937 : * Apply pullup variable replacement to a subquery
2938 : *
2939 : * This needs to be different from pullup_replace_vars() because
2940 : * replace_rte_variables will think that it shouldn't increment sublevels_up
2941 : * before entering the Query; so we need to call it with sublevels_up == 1.
2942 : */
2943 : static Query *
2944 408 : pullup_replace_vars_subquery(Query *query,
2945 : pullup_replace_vars_context *context)
2946 : {
2947 : Assert(IsA(query, Query));
2948 408 : return (Query *) replace_rte_variables((Node *) query,
2949 : context->varno, 1,
2950 : pullup_replace_vars_callback,
2951 : context,
2952 : NULL);
2953 : }
2954 :
2955 :
2956 : /*
2957 : * flatten_simple_union_all
2958 : * Try to optimize top-level UNION ALL structure into an appendrel
2959 : *
2960 : * If a query's setOperations tree consists entirely of simple UNION ALL
2961 : * operations, flatten it into an append relation, which we can process more
2962 : * intelligently than the general setops case. Otherwise, do nothing.
2963 : *
2964 : * In most cases, this can succeed only for a top-level query, because for a
2965 : * subquery in FROM, the parent query's invocation of pull_up_subqueries would
2966 : * already have flattened the UNION via pull_up_simple_union_all. But there
2967 : * are a few cases we can support here but not in that code path, for example
2968 : * when the subquery also contains ORDER BY.
2969 : */
2970 : void
2971 6326 : flatten_simple_union_all(PlannerInfo *root)
2972 : {
2973 6326 : Query *parse = root->parse;
2974 : SetOperationStmt *topop;
2975 : Node *leftmostjtnode;
2976 : int leftmostRTI;
2977 : RangeTblEntry *leftmostRTE;
2978 : int childRTI;
2979 : RangeTblEntry *childRTE;
2980 : RangeTblRef *rtr;
2981 :
2982 : /* Shouldn't be called unless query has setops */
2983 6326 : topop = castNode(SetOperationStmt, parse->setOperations);
2984 : Assert(topop);
2985 :
2986 : /* Can't optimize away a recursive UNION */
2987 6326 : if (root->hasRecursion)
2988 5896 : return;
2989 :
2990 : /*
2991 : * Recursively check the tree of set operations. If not all UNION ALL
2992 : * with identical column types, punt.
2993 : */
2994 5424 : if (!is_simple_union_all_recurse((Node *) topop, parse, topop->colTypes))
2995 4994 : return;
2996 :
2997 : /*
2998 : * Locate the leftmost leaf query in the setops tree. The upper query's
2999 : * Vars all refer to this RTE (see transformSetOperationStmt).
3000 : */
3001 430 : leftmostjtnode = topop->larg;
3002 774 : while (leftmostjtnode && IsA(leftmostjtnode, SetOperationStmt))
3003 344 : leftmostjtnode = ((SetOperationStmt *) leftmostjtnode)->larg;
3004 : Assert(leftmostjtnode && IsA(leftmostjtnode, RangeTblRef));
3005 430 : leftmostRTI = ((RangeTblRef *) leftmostjtnode)->rtindex;
3006 430 : leftmostRTE = rt_fetch(leftmostRTI, parse->rtable);
3007 : Assert(leftmostRTE->rtekind == RTE_SUBQUERY);
3008 :
3009 : /*
3010 : * Make a copy of the leftmost RTE and add it to the rtable. This copy
3011 : * will represent the leftmost leaf query in its capacity as a member of
3012 : * the appendrel. The original will represent the appendrel as a whole.
3013 : * (We must do things this way because the upper query's Vars have to be
3014 : * seen as referring to the whole appendrel.)
3015 : */
3016 430 : childRTE = copyObject(leftmostRTE);
3017 430 : parse->rtable = lappend(parse->rtable, childRTE);
3018 430 : childRTI = list_length(parse->rtable);
3019 :
3020 : /* Modify the setops tree to reference the child copy */
3021 430 : ((RangeTblRef *) leftmostjtnode)->rtindex = childRTI;
3022 :
3023 : /* Modify the formerly-leftmost RTE to mark it as an appendrel parent */
3024 430 : leftmostRTE->inh = true;
3025 :
3026 : /*
3027 : * Form a RangeTblRef for the appendrel, and insert it into FROM. The top
3028 : * Query of a setops tree should have had an empty FromClause initially.
3029 : */
3030 430 : rtr = makeNode(RangeTblRef);
3031 430 : rtr->rtindex = leftmostRTI;
3032 : Assert(parse->jointree->fromlist == NIL);
3033 430 : parse->jointree->fromlist = list_make1(rtr);
3034 :
3035 : /*
3036 : * Now pretend the query has no setops. We must do this before trying to
3037 : * do subquery pullup, because of Assert in pull_up_simple_subquery.
3038 : */
3039 430 : parse->setOperations = NULL;
3040 :
3041 : /*
3042 : * Build AppendRelInfo information, and apply pull_up_subqueries to the
3043 : * leaf queries of the UNION ALL. (We must do that now because they
3044 : * weren't previously referenced by the jointree, and so were missed by
3045 : * the main invocation of pull_up_subqueries.)
3046 : */
3047 430 : pull_up_union_leaf_queries((Node *) topop, root, leftmostRTI, parse, 0);
3048 : }
3049 :
3050 :
3051 : /*
3052 : * reduce_outer_joins
3053 : * Attempt to reduce outer joins to plain inner joins.
3054 : *
3055 : * The idea here is that given a query like
3056 : * SELECT ... FROM a LEFT JOIN b ON (...) WHERE b.y = 42;
3057 : * we can reduce the LEFT JOIN to a plain JOIN if the "=" operator in WHERE
3058 : * is strict. The strict operator will always return NULL, causing the outer
3059 : * WHERE to fail, on any row where the LEFT JOIN filled in NULLs for b's
3060 : * columns. Therefore, there's no need for the join to produce null-extended
3061 : * rows in the first place --- which makes it a plain join not an outer join.
3062 : * (This scenario may not be very likely in a query written out by hand, but
3063 : * it's reasonably likely when pushing quals down into complex views.)
3064 : *
3065 : * More generally, an outer join can be reduced in strength if there is a
3066 : * strict qual above it in the qual tree that constrains a Var from the
3067 : * nullable side of the join to be non-null. (For FULL joins this applies
3068 : * to each side separately.)
3069 : *
3070 : * Another transformation we apply here is to recognize cases like
3071 : * SELECT ... FROM a LEFT JOIN b ON (a.x = b.y) WHERE b.y IS NULL;
3072 : * If the join clause is strict for b.y, then only null-extended rows could
3073 : * pass the upper WHERE, and we can conclude that what the query is really
3074 : * specifying is an anti-semijoin. We change the join type from JOIN_LEFT
3075 : * to JOIN_ANTI. The IS NULL clause then becomes redundant, and must be
3076 : * removed to prevent bogus selectivity calculations, but we leave it to
3077 : * distribute_qual_to_rels to get rid of such clauses.
3078 : *
3079 : * Also, we get rid of JOIN_RIGHT cases by flipping them around to become
3080 : * JOIN_LEFT. This saves some code here and in some later planner routines;
3081 : * the main benefit is to reduce the number of jointypes that can appear in
3082 : * SpecialJoinInfo nodes. Note that we can still generate Paths and Plans
3083 : * that use JOIN_RIGHT (or JOIN_RIGHT_ANTI) by switching the inputs again.
3084 : *
3085 : * To ease recognition of strict qual clauses, we require this routine to be
3086 : * run after expression preprocessing (i.e., qual canonicalization and JOIN
3087 : * alias-var expansion).
3088 : */
3089 : void
3090 33588 : reduce_outer_joins(PlannerInfo *root)
3091 : {
3092 : reduce_outer_joins_pass1_state *state1;
3093 : reduce_outer_joins_pass2_state state2;
3094 : ListCell *lc;
3095 :
3096 : /*
3097 : * To avoid doing strictness checks on more quals than necessary, we want
3098 : * to stop descending the jointree as soon as there are no outer joins
3099 : * below our current point. This consideration forces a two-pass process.
3100 : * The first pass gathers information about which base rels appear below
3101 : * each side of each join clause, and about whether there are outer
3102 : * join(s) below each side of each join clause. The second pass examines
3103 : * qual clauses and changes join types as it descends the tree.
3104 : */
3105 33588 : state1 = reduce_outer_joins_pass1((Node *) root->parse->jointree);
3106 :
3107 : /* planner.c shouldn't have called me if no outer joins */
3108 33588 : if (state1 == NULL || !state1->contains_outer)
3109 0 : elog(ERROR, "so where are the outer joins?");
3110 :
3111 33588 : state2.inner_reduced = NULL;
3112 33588 : state2.partial_reduced = NIL;
3113 :
3114 33588 : reduce_outer_joins_pass2((Node *) root->parse->jointree,
3115 : state1, &state2,
3116 : root, NULL, NIL);
3117 :
3118 : /*
3119 : * If we successfully reduced the strength of any outer joins, we must
3120 : * remove references to those joins as nulling rels. This is handled as
3121 : * an additional pass, for simplicity and because we can handle all
3122 : * fully-reduced joins in a single pass over the parse tree.
3123 : */
3124 33588 : if (!bms_is_empty(state2.inner_reduced))
3125 : {
3126 2778 : root->parse = (Query *)
3127 2778 : remove_nulling_relids((Node *) root->parse,
3128 2778 : state2.inner_reduced,
3129 : NULL);
3130 : /* There could be references in the append_rel_list, too */
3131 2778 : root->append_rel_list = (List *)
3132 2778 : remove_nulling_relids((Node *) root->append_rel_list,
3133 2778 : state2.inner_reduced,
3134 : NULL);
3135 : }
3136 :
3137 : /*
3138 : * Partially-reduced full joins have to be done one at a time, since
3139 : * they'll each need a different setting of except_relids.
3140 : */
3141 33638 : foreach(lc, state2.partial_reduced)
3142 : {
3143 50 : reduce_outer_joins_partial_state *statep = lfirst(lc);
3144 50 : Relids full_join_relids = bms_make_singleton(statep->full_join_rti);
3145 :
3146 50 : root->parse = (Query *)
3147 50 : remove_nulling_relids((Node *) root->parse,
3148 : full_join_relids,
3149 50 : statep->unreduced_side);
3150 50 : root->append_rel_list = (List *)
3151 50 : remove_nulling_relids((Node *) root->append_rel_list,
3152 : full_join_relids,
3153 50 : statep->unreduced_side);
3154 : }
3155 33588 : }
3156 :
3157 : /*
3158 : * reduce_outer_joins_pass1 - phase 1 data collection
3159 : *
3160 : * Returns a state node describing the given jointree node.
3161 : */
3162 : static reduce_outer_joins_pass1_state *
3163 192082 : reduce_outer_joins_pass1(Node *jtnode)
3164 : {
3165 : reduce_outer_joins_pass1_state *result;
3166 :
3167 : result = (reduce_outer_joins_pass1_state *)
3168 192082 : palloc(sizeof(reduce_outer_joins_pass1_state));
3169 192082 : result->relids = NULL;
3170 192082 : result->contains_outer = false;
3171 192082 : result->sub_states = NIL;
3172 :
3173 192082 : if (jtnode == NULL)
3174 0 : return result;
3175 192082 : if (IsA(jtnode, RangeTblRef))
3176 : {
3177 95850 : int varno = ((RangeTblRef *) jtnode)->rtindex;
3178 :
3179 95850 : result->relids = bms_make_singleton(varno);
3180 : }
3181 96232 : else if (IsA(jtnode, FromExpr))
3182 : {
3183 36684 : FromExpr *f = (FromExpr *) jtnode;
3184 : ListCell *l;
3185 :
3186 76082 : foreach(l, f->fromlist)
3187 : {
3188 : reduce_outer_joins_pass1_state *sub_state;
3189 :
3190 39398 : sub_state = reduce_outer_joins_pass1(lfirst(l));
3191 78796 : result->relids = bms_add_members(result->relids,
3192 39398 : sub_state->relids);
3193 39398 : result->contains_outer |= sub_state->contains_outer;
3194 39398 : result->sub_states = lappend(result->sub_states, sub_state);
3195 : }
3196 : }
3197 59548 : else if (IsA(jtnode, JoinExpr))
3198 : {
3199 59548 : JoinExpr *j = (JoinExpr *) jtnode;
3200 : reduce_outer_joins_pass1_state *sub_state;
3201 :
3202 : /* join's own RT index is not wanted in result->relids */
3203 59548 : if (IS_OUTER_JOIN(j->jointype))
3204 47704 : result->contains_outer = true;
3205 :
3206 59548 : sub_state = reduce_outer_joins_pass1(j->larg);
3207 119096 : result->relids = bms_add_members(result->relids,
3208 59548 : sub_state->relids);
3209 59548 : result->contains_outer |= sub_state->contains_outer;
3210 59548 : result->sub_states = lappend(result->sub_states, sub_state);
3211 :
3212 59548 : sub_state = reduce_outer_joins_pass1(j->rarg);
3213 119096 : result->relids = bms_add_members(result->relids,
3214 59548 : sub_state->relids);
3215 59548 : result->contains_outer |= sub_state->contains_outer;
3216 59548 : result->sub_states = lappend(result->sub_states, sub_state);
3217 : }
3218 : else
3219 0 : elog(ERROR, "unrecognized node type: %d",
3220 : (int) nodeTag(jtnode));
3221 192082 : return result;
3222 : }
3223 :
3224 : /*
3225 : * reduce_outer_joins_pass2 - phase 2 processing
3226 : *
3227 : * jtnode: current jointree node
3228 : * state1: state data collected by phase 1 for this node
3229 : * state2: where to accumulate info about successfully-reduced joins
3230 : * root: toplevel planner state
3231 : * nonnullable_rels: set of base relids forced non-null by upper quals
3232 : * forced_null_vars: multibitmapset of Vars forced null by upper quals
3233 : *
3234 : * Returns info in state2 about outer joins that were successfully simplified.
3235 : * Joins that were fully reduced to inner joins are all added to
3236 : * state2->inner_reduced. If a full join is reduced to a left join,
3237 : * it needs its own entry in state2->partial_reduced, since that will
3238 : * require custom processing to remove only the correct nullingrel markers.
3239 : */
3240 : static void
3241 83260 : reduce_outer_joins_pass2(Node *jtnode,
3242 : reduce_outer_joins_pass1_state *state1,
3243 : reduce_outer_joins_pass2_state *state2,
3244 : PlannerInfo *root,
3245 : Relids nonnullable_rels,
3246 : List *forced_null_vars)
3247 : {
3248 : /*
3249 : * pass 2 should never descend as far as an empty subnode or base rel,
3250 : * because it's only called on subtrees marked as contains_outer.
3251 : */
3252 83260 : if (jtnode == NULL)
3253 0 : elog(ERROR, "reached empty jointree");
3254 83260 : if (IsA(jtnode, RangeTblRef))
3255 0 : elog(ERROR, "reached base rel");
3256 83260 : else if (IsA(jtnode, FromExpr))
3257 : {
3258 34918 : FromExpr *f = (FromExpr *) jtnode;
3259 : ListCell *l;
3260 : ListCell *s;
3261 : Relids pass_nonnullable_rels;
3262 : List *pass_forced_null_vars;
3263 :
3264 : /* Scan quals to see if we can add any constraints */
3265 34918 : pass_nonnullable_rels = find_nonnullable_rels(f->quals);
3266 34918 : pass_nonnullable_rels = bms_add_members(pass_nonnullable_rels,
3267 : nonnullable_rels);
3268 34918 : pass_forced_null_vars = find_forced_null_vars(f->quals);
3269 34918 : pass_forced_null_vars = mbms_add_members(pass_forced_null_vars,
3270 : forced_null_vars);
3271 : /* And recurse --- but only into interesting subtrees */
3272 : Assert(list_length(f->fromlist) == list_length(state1->sub_states));
3273 72418 : forboth(l, f->fromlist, s, state1->sub_states)
3274 : {
3275 37500 : reduce_outer_joins_pass1_state *sub_state = lfirst(s);
3276 :
3277 37500 : if (sub_state->contains_outer)
3278 34948 : reduce_outer_joins_pass2(lfirst(l), sub_state,
3279 : state2, root,
3280 : pass_nonnullable_rels,
3281 : pass_forced_null_vars);
3282 : }
3283 34918 : bms_free(pass_nonnullable_rels);
3284 : /* can't so easily clean up var lists, unfortunately */
3285 : }
3286 48342 : else if (IsA(jtnode, JoinExpr))
3287 : {
3288 48342 : JoinExpr *j = (JoinExpr *) jtnode;
3289 48342 : int rtindex = j->rtindex;
3290 48342 : JoinType jointype = j->jointype;
3291 48342 : reduce_outer_joins_pass1_state *left_state = linitial(state1->sub_states);
3292 48342 : reduce_outer_joins_pass1_state *right_state = lsecond(state1->sub_states);
3293 :
3294 : /* Can we simplify this join? */
3295 48342 : switch (jointype)
3296 : {
3297 588 : case JOIN_INNER:
3298 588 : break;
3299 45076 : case JOIN_LEFT:
3300 45076 : if (bms_overlap(nonnullable_rels, right_state->relids))
3301 2854 : jointype = JOIN_INNER;
3302 45076 : break;
3303 1124 : case JOIN_RIGHT:
3304 1124 : if (bms_overlap(nonnullable_rels, left_state->relids))
3305 62 : jointype = JOIN_INNER;
3306 1124 : break;
3307 1090 : case JOIN_FULL:
3308 1090 : if (bms_overlap(nonnullable_rels, left_state->relids))
3309 : {
3310 24 : if (bms_overlap(nonnullable_rels, right_state->relids))
3311 12 : jointype = JOIN_INNER;
3312 : else
3313 : {
3314 12 : jointype = JOIN_LEFT;
3315 : /* Also report partial reduction in state2 */
3316 12 : report_reduced_full_join(state2, rtindex,
3317 : right_state->relids);
3318 : }
3319 : }
3320 : else
3321 : {
3322 1066 : if (bms_overlap(nonnullable_rels, right_state->relids))
3323 : {
3324 38 : jointype = JOIN_RIGHT;
3325 : /* Also report partial reduction in state2 */
3326 38 : report_reduced_full_join(state2, rtindex,
3327 : left_state->relids);
3328 : }
3329 : }
3330 1090 : break;
3331 464 : case JOIN_SEMI:
3332 : case JOIN_ANTI:
3333 :
3334 : /*
3335 : * These could only have been introduced by pull_up_sublinks,
3336 : * so there's no way that upper quals could refer to their
3337 : * righthand sides, and no point in checking. We don't expect
3338 : * to see JOIN_RIGHT_SEMI or JOIN_RIGHT_ANTI yet.
3339 : */
3340 464 : break;
3341 0 : default:
3342 0 : elog(ERROR, "unrecognized join type: %d",
3343 : (int) jointype);
3344 : break;
3345 : }
3346 :
3347 : /*
3348 : * Convert JOIN_RIGHT to JOIN_LEFT. Note that in the case where we
3349 : * reduced JOIN_FULL to JOIN_RIGHT, this will mean the JoinExpr no
3350 : * longer matches the internal ordering of any CoalesceExpr's built to
3351 : * represent merged join variables. We don't care about that at
3352 : * present, but be wary of it ...
3353 : */
3354 48342 : if (jointype == JOIN_RIGHT)
3355 : {
3356 : Node *tmparg;
3357 :
3358 1100 : tmparg = j->larg;
3359 1100 : j->larg = j->rarg;
3360 1100 : j->rarg = tmparg;
3361 1100 : jointype = JOIN_LEFT;
3362 1100 : right_state = linitial(state1->sub_states);
3363 1100 : left_state = lsecond(state1->sub_states);
3364 : }
3365 :
3366 : /*
3367 : * See if we can reduce JOIN_LEFT to JOIN_ANTI. This is the case if
3368 : * the join's own quals are strict for any var that was forced null by
3369 : * higher qual levels. NOTE: there are other ways that we could
3370 : * detect an anti-join, in particular if we were to check whether Vars
3371 : * coming from the RHS must be non-null because of table constraints.
3372 : * That seems complicated and expensive though (in particular, one
3373 : * would have to be wary of lower outer joins). For the moment this
3374 : * seems sufficient.
3375 : */
3376 48342 : if (jointype == JOIN_LEFT)
3377 : {
3378 : List *nonnullable_vars;
3379 : Bitmapset *overlap;
3380 :
3381 : /* Find Vars in j->quals that must be non-null in joined rows */
3382 43334 : nonnullable_vars = find_nonnullable_vars(j->quals);
3383 :
3384 : /*
3385 : * It's not sufficient to check whether nonnullable_vars and
3386 : * forced_null_vars overlap: we need to know if the overlap
3387 : * includes any RHS variables.
3388 : */
3389 43334 : overlap = mbms_overlap_sets(nonnullable_vars, forced_null_vars);
3390 43334 : if (bms_overlap(overlap, right_state->relids))
3391 1132 : jointype = JOIN_ANTI;
3392 : }
3393 :
3394 : /*
3395 : * Apply the jointype change, if any, to both jointree node and RTE.
3396 : * Also, if we changed an RTE to INNER, add its RTI to inner_reduced.
3397 : */
3398 48342 : if (rtindex && jointype != j->jointype)
3399 : {
3400 5172 : RangeTblEntry *rte = rt_fetch(rtindex, root->parse->rtable);
3401 :
3402 : Assert(rte->rtekind == RTE_JOIN);
3403 : Assert(rte->jointype == j->jointype);
3404 5172 : rte->jointype = jointype;
3405 5172 : if (jointype == JOIN_INNER)
3406 2928 : state2->inner_reduced = bms_add_member(state2->inner_reduced,
3407 : rtindex);
3408 : }
3409 48342 : j->jointype = jointype;
3410 :
3411 : /* Only recurse if there's more to do below here */
3412 48342 : if (left_state->contains_outer || right_state->contains_outer)
3413 : {
3414 : Relids local_nonnullable_rels;
3415 : List *local_forced_null_vars;
3416 : Relids pass_nonnullable_rels;
3417 : List *pass_forced_null_vars;
3418 :
3419 : /*
3420 : * If this join is (now) inner, we can add any constraints its
3421 : * quals provide to those we got from above. But if it is outer,
3422 : * we can pass down the local constraints only into the nullable
3423 : * side, because an outer join never eliminates any rows from its
3424 : * non-nullable side. Also, there is no point in passing upper
3425 : * constraints into the nullable side, since if there were any
3426 : * we'd have been able to reduce the join. (In the case of upper
3427 : * forced-null constraints, we *must not* pass them into the
3428 : * nullable side --- they either applied here, or not.) The upshot
3429 : * is that we pass either the local or the upper constraints,
3430 : * never both, to the children of an outer join.
3431 : *
3432 : * Note that a SEMI join works like an inner join here: it's okay
3433 : * to pass down both local and upper constraints. (There can't be
3434 : * any upper constraints affecting its inner side, but it's not
3435 : * worth having a separate code path to avoid passing them.)
3436 : *
3437 : * At a FULL join we just punt and pass nothing down --- is it
3438 : * possible to be smarter?
3439 : */
3440 14656 : if (jointype != JOIN_FULL)
3441 : {
3442 14520 : local_nonnullable_rels = find_nonnullable_rels(j->quals);
3443 14520 : local_forced_null_vars = find_forced_null_vars(j->quals);
3444 14520 : if (jointype == JOIN_INNER || jointype == JOIN_SEMI)
3445 : {
3446 : /* OK to merge upper and local constraints */
3447 1042 : local_nonnullable_rels = bms_add_members(local_nonnullable_rels,
3448 : nonnullable_rels);
3449 1042 : local_forced_null_vars = mbms_add_members(local_forced_null_vars,
3450 : forced_null_vars);
3451 : }
3452 : }
3453 : else
3454 : {
3455 : /* no use in calculating these */
3456 136 : local_nonnullable_rels = NULL;
3457 136 : local_forced_null_vars = NIL;
3458 : }
3459 :
3460 14656 : if (left_state->contains_outer)
3461 : {
3462 13882 : if (jointype == JOIN_INNER || jointype == JOIN_SEMI)
3463 : {
3464 : /* pass union of local and upper constraints */
3465 822 : pass_nonnullable_rels = local_nonnullable_rels;
3466 822 : pass_forced_null_vars = local_forced_null_vars;
3467 : }
3468 13060 : else if (jointype != JOIN_FULL) /* ie, LEFT or ANTI */
3469 : {
3470 : /* can't pass local constraints to non-nullable side */
3471 12952 : pass_nonnullable_rels = nonnullable_rels;
3472 12952 : pass_forced_null_vars = forced_null_vars;
3473 : }
3474 : else
3475 : {
3476 : /* no constraints pass through JOIN_FULL */
3477 108 : pass_nonnullable_rels = NULL;
3478 108 : pass_forced_null_vars = NIL;
3479 : }
3480 13882 : reduce_outer_joins_pass2(j->larg, left_state,
3481 : state2, root,
3482 : pass_nonnullable_rels,
3483 : pass_forced_null_vars);
3484 : }
3485 :
3486 14656 : if (right_state->contains_outer)
3487 : {
3488 842 : if (jointype != JOIN_FULL) /* ie, INNER/LEFT/SEMI/ANTI */
3489 : {
3490 : /* pass appropriate constraints, per comment above */
3491 814 : pass_nonnullable_rels = local_nonnullable_rels;
3492 814 : pass_forced_null_vars = local_forced_null_vars;
3493 : }
3494 : else
3495 : {
3496 : /* no constraints pass through JOIN_FULL */
3497 28 : pass_nonnullable_rels = NULL;
3498 28 : pass_forced_null_vars = NIL;
3499 : }
3500 842 : reduce_outer_joins_pass2(j->rarg, right_state,
3501 : state2, root,
3502 : pass_nonnullable_rels,
3503 : pass_forced_null_vars);
3504 : }
3505 14656 : bms_free(local_nonnullable_rels);
3506 : }
3507 : }
3508 : else
3509 0 : elog(ERROR, "unrecognized node type: %d",
3510 : (int) nodeTag(jtnode));
3511 83260 : }
3512 :
3513 : /* Helper for reduce_outer_joins_pass2 */
3514 : static void
3515 50 : report_reduced_full_join(reduce_outer_joins_pass2_state *state2,
3516 : int rtindex, Relids relids)
3517 : {
3518 : reduce_outer_joins_partial_state *statep;
3519 :
3520 50 : statep = palloc(sizeof(reduce_outer_joins_partial_state));
3521 50 : statep->full_join_rti = rtindex;
3522 50 : statep->unreduced_side = relids;
3523 50 : state2->partial_reduced = lappend(state2->partial_reduced, statep);
3524 50 : }
3525 :
3526 :
3527 : /*
3528 : * remove_useless_result_rtes
3529 : * Attempt to remove RTE_RESULT RTEs from the join tree.
3530 : * Also, elide single-child FromExprs where possible.
3531 : *
3532 : * We can remove RTE_RESULT entries from the join tree using the knowledge
3533 : * that RTE_RESULT returns exactly one row and has no output columns. Hence,
3534 : * if one is inner-joined to anything else, we can delete it. Optimizations
3535 : * are also possible for some outer-join cases, as detailed below.
3536 : *
3537 : * This pass also replaces single-child FromExprs with their child node
3538 : * where possible. It's appropriate to do that here and not earlier because
3539 : * RTE_RESULT removal might reduce a multiple-child FromExpr to have only one
3540 : * child. We can remove such a FromExpr if its quals are empty, or if it's
3541 : * semantically valid to merge the quals into those of the parent node.
3542 : * While removing unnecessary join tree nodes has some micro-efficiency value,
3543 : * the real reason to do this is to eliminate cases where the nullable side of
3544 : * an outer join node is a FromExpr whose single child is another outer join.
3545 : * To correctly determine whether the two outer joins can commute,
3546 : * deconstruct_jointree() must treat any quals of such a FromExpr as being
3547 : * degenerate quals of the upper outer join. The best way to do that is to
3548 : * make them actually *be* quals of the upper join, by dropping the FromExpr
3549 : * and hoisting the quals up into the upper join's quals. (Note that there is
3550 : * no hazard when the intermediate FromExpr has multiple children, since then
3551 : * it represents an inner join that cannot commute with the upper outer join.)
3552 : * As long as we have to do that, we might as well elide such FromExprs
3553 : * everywhere.
3554 : *
3555 : * Some of these optimizations depend on recognizing empty (constant-true)
3556 : * quals for FromExprs and JoinExprs. That makes it useful to apply this
3557 : * optimization pass after expression preprocessing, since that will have
3558 : * eliminated constant-true quals, allowing more cases to be recognized as
3559 : * optimizable. What's more, the usual reason for an RTE_RESULT to be present
3560 : * is that we pulled up a subquery or VALUES clause, thus very possibly
3561 : * replacing Vars with constants, making it more likely that a qual can be
3562 : * reduced to constant true. Also, because some optimizations depend on
3563 : * the outer-join type, it's best to have done reduce_outer_joins() first.
3564 : *
3565 : * A PlaceHolderVar referencing an RTE_RESULT RTE poses an obstacle to this
3566 : * process: we must remove the RTE_RESULT's relid from the PHV's phrels, but
3567 : * we must not reduce the phrels set to empty. If that would happen, and
3568 : * the RTE_RESULT is an immediate child of an outer join, we have to give up
3569 : * and not remove the RTE_RESULT: there is noplace else to evaluate the
3570 : * PlaceHolderVar. (That is, in such cases the RTE_RESULT *does* have output
3571 : * columns.) But if the RTE_RESULT is an immediate child of an inner join,
3572 : * we can usually change the PlaceHolderVar's phrels so as to evaluate it at
3573 : * the inner join instead. This is OK because we really only care that PHVs
3574 : * are evaluated above or below the correct outer joins. We can't, however,
3575 : * postpone the evaluation of a PHV to above where it is used; so there are
3576 : * some checks below on whether output PHVs are laterally referenced in the
3577 : * other join input rel(s).
3578 : *
3579 : * We used to try to do this work as part of pull_up_subqueries() where the
3580 : * potentially-optimizable cases get introduced; but it's way simpler, and
3581 : * more effective, to do it separately.
3582 : */
3583 : void
3584 257774 : remove_useless_result_rtes(PlannerInfo *root)
3585 : {
3586 257774 : Relids dropped_outer_joins = NULL;
3587 : ListCell *cell;
3588 :
3589 : /* Top level of jointree must always be a FromExpr */
3590 : Assert(IsA(root->parse->jointree, FromExpr));
3591 : /* Recurse ... */
3592 515548 : root->parse->jointree = (FromExpr *)
3593 257774 : remove_useless_results_recurse(root,
3594 257774 : (Node *) root->parse->jointree,
3595 : NULL,
3596 : &dropped_outer_joins);
3597 : /* We should still have a FromExpr */
3598 : Assert(IsA(root->parse->jointree, FromExpr));
3599 :
3600 : /*
3601 : * If we removed any outer-join nodes from the jointree, run around and
3602 : * remove references to those joins as nulling rels. (There could be such
3603 : * references in PHVs that we pulled up out of the original subquery that
3604 : * the RESULT rel replaced. This is kosher on the grounds that we now
3605 : * know that such an outer join wouldn't really have nulled anything.) We
3606 : * don't do this during the main recursion, for simplicity and because we
3607 : * can handle all such joins in a single pass over the parse tree.
3608 : */
3609 257774 : if (!bms_is_empty(dropped_outer_joins))
3610 : {
3611 60 : root->parse = (Query *)
3612 60 : remove_nulling_relids((Node *) root->parse,
3613 : dropped_outer_joins,
3614 : NULL);
3615 : /* There could be references in the append_rel_list, too */
3616 60 : root->append_rel_list = (List *)
3617 60 : remove_nulling_relids((Node *) root->append_rel_list,
3618 : dropped_outer_joins,
3619 : NULL);
3620 : }
3621 :
3622 : /*
3623 : * Remove any PlanRowMark referencing an RTE_RESULT RTE. We obviously
3624 : * must do that for any RTE_RESULT that we just removed. But one for a
3625 : * RTE that we did not remove can be dropped anyway: since the RTE has
3626 : * only one possible output row, there is no need for EPQ to mark and
3627 : * restore that row.
3628 : *
3629 : * It's necessary, not optional, to remove the PlanRowMark for a surviving
3630 : * RTE_RESULT RTE; otherwise we'll generate a whole-row Var for the
3631 : * RTE_RESULT, which the executor has no support for.
3632 : */
3633 259788 : foreach(cell, root->rowMarks)
3634 : {
3635 2014 : PlanRowMark *rc = (PlanRowMark *) lfirst(cell);
3636 :
3637 2014 : if (rt_fetch(rc->rti, root->parse->rtable)->rtekind == RTE_RESULT)
3638 894 : root->rowMarks = foreach_delete_current(root->rowMarks, cell);
3639 : }
3640 257774 : }
3641 :
3642 : /*
3643 : * remove_useless_results_recurse
3644 : * Recursive guts of remove_useless_result_rtes.
3645 : *
3646 : * This recursively processes the jointree and returns a modified jointree.
3647 : * In addition, the RT indexes of any removed outer-join nodes are added to
3648 : * *dropped_outer_joins.
3649 : *
3650 : * jtnode is the current jointree node. If it could be valid to merge
3651 : * its quals into those of the parent node, parent_quals should point to
3652 : * the parent's quals list; otherwise, pass NULL for parent_quals.
3653 : * (Note that in some cases, parent_quals points to the quals of a parent
3654 : * more than one level up in the tree.)
3655 : */
3656 : static Node *
3657 644652 : remove_useless_results_recurse(PlannerInfo *root, Node *jtnode,
3658 : Node **parent_quals,
3659 : Relids *dropped_outer_joins)
3660 : {
3661 : Assert(jtnode != NULL);
3662 644652 : if (IsA(jtnode, RangeTblRef))
3663 : {
3664 : /* Can't immediately do anything with a RangeTblRef */
3665 : }
3666 322756 : else if (IsA(jtnode, FromExpr))
3667 : {
3668 261994 : FromExpr *f = (FromExpr *) jtnode;
3669 261994 : Relids result_relids = NULL;
3670 : ListCell *cell;
3671 :
3672 : /*
3673 : * We can drop RTE_RESULT rels from the fromlist so long as at least
3674 : * one child remains, since joining to a one-row table changes
3675 : * nothing. (But we can't drop a RTE_RESULT that computes PHV(s) that
3676 : * are needed by some sibling. The cleanup transformation below would
3677 : * reassign the PHVs to be computed at the join, which is too late for
3678 : * the sibling's use.) The easiest way to mechanize this rule is to
3679 : * modify the list in-place.
3680 : */
3681 527348 : foreach(cell, f->fromlist)
3682 : {
3683 265354 : Node *child = (Node *) lfirst(cell);
3684 : int varno;
3685 :
3686 : /* Recursively transform child, allowing it to push up quals ... */
3687 265354 : child = remove_useless_results_recurse(root, child,
3688 : &f->quals,
3689 : dropped_outer_joins);
3690 : /* ... and stick it back into the tree */
3691 265354 : lfirst(cell) = child;
3692 :
3693 : /*
3694 : * If it's an RTE_RESULT with at least one sibling, and no sibling
3695 : * references dependent PHVs, we can drop it. We don't yet know
3696 : * what the inner join's final relid set will be, so postpone
3697 : * cleanup of PHVs etc till after this loop.
3698 : */
3699 270738 : if (list_length(f->fromlist) > 1 &&
3700 5384 : (varno = get_result_relid(root, child)) != 0 &&
3701 346 : !find_dependent_phvs_in_jointree(root, (Node *) f, varno))
3702 : {
3703 322 : f->fromlist = foreach_delete_current(f->fromlist, cell);
3704 322 : result_relids = bms_add_member(result_relids, varno);
3705 : }
3706 : }
3707 :
3708 : /*
3709 : * Clean up if we dropped any RTE_RESULT RTEs. This is a bit
3710 : * inefficient if there's more than one, but it seems better to
3711 : * optimize the support code for the single-relid case.
3712 : */
3713 261994 : if (result_relids)
3714 : {
3715 310 : int varno = -1;
3716 :
3717 632 : while ((varno = bms_next_member(result_relids, varno)) >= 0)
3718 322 : remove_result_refs(root, varno, (Node *) f);
3719 : }
3720 :
3721 : /*
3722 : * If the FromExpr now has only one child, see if we can elide it.
3723 : * This is always valid if there are no quals, except at the top of
3724 : * the jointree (since Query.jointree is required to point to a
3725 : * FromExpr). Otherwise, we can do it if we can push the quals up to
3726 : * the parent node.
3727 : *
3728 : * Note: while it would not be terribly hard to generalize this
3729 : * transformation to merge multi-child FromExprs into their parent
3730 : * FromExpr, that risks making the parent join too expensive to plan.
3731 : * We leave it to later processing to decide heuristically whether
3732 : * that's a good idea. Pulling up a single child is always OK,
3733 : * however.
3734 : */
3735 261994 : if (list_length(f->fromlist) == 1 &&
3736 260178 : f != root->parse->jointree &&
3737 4020 : (f->quals == NULL || parent_quals != NULL))
3738 : {
3739 : /*
3740 : * Merge any quals up to parent. They should be in implicit-AND
3741 : * format by now, so we just need to concatenate lists. Put the
3742 : * child quals at the front, on the grounds that they should
3743 : * nominally be evaluated earlier.
3744 : */
3745 2712 : if (f->quals != NULL)
3746 1322 : *parent_quals = (Node *)
3747 1322 : list_concat(castNode(List, f->quals),
3748 : castNode(List, *parent_quals));
3749 2712 : return (Node *) linitial(f->fromlist);
3750 : }
3751 : }
3752 60762 : else if (IsA(jtnode, JoinExpr))
3753 : {
3754 60762 : JoinExpr *j = (JoinExpr *) jtnode;
3755 : int varno;
3756 :
3757 : /*
3758 : * First, recurse. We can absorb pushed-up FromExpr quals from either
3759 : * child into this node if the jointype is INNER, since then this is
3760 : * equivalent to a FromExpr. When the jointype is LEFT, we can absorb
3761 : * quals from the RHS child into the current node, as they're
3762 : * essentially degenerate quals of the outer join. Moreover, if we've
3763 : * been passed down a parent_quals pointer then we can allow quals of
3764 : * the LHS child to be absorbed into the parent. (This is important
3765 : * to ensure we remove single-child FromExprs immediately below
3766 : * commutable left joins.) For other jointypes, we can't move child
3767 : * quals up, or at least there's no particular reason to.
3768 : */
3769 60762 : j->larg = remove_useless_results_recurse(root, j->larg,
3770 60762 : (j->jointype == JOIN_INNER) ?
3771 : &j->quals :
3772 45440 : (j->jointype == JOIN_LEFT) ?
3773 45440 : parent_quals : NULL,
3774 : dropped_outer_joins);
3775 60762 : j->rarg = remove_useless_results_recurse(root, j->rarg,
3776 60762 : (j->jointype == JOIN_INNER ||
3777 45440 : j->jointype == JOIN_LEFT) ?
3778 : &j->quals : NULL,
3779 : dropped_outer_joins);
3780 :
3781 : /* Apply join-type-specific optimization rules */
3782 60762 : switch (j->jointype)
3783 : {
3784 15322 : case JOIN_INNER:
3785 :
3786 : /*
3787 : * An inner join is equivalent to a FromExpr, so if either
3788 : * side was simplified to an RTE_RESULT rel, we can replace
3789 : * the join with a FromExpr with just the other side.
3790 : * Furthermore, we can elide that FromExpr according to the
3791 : * same rules as above.
3792 : *
3793 : * Just as in the FromExpr case, we can't simplify if the
3794 : * other input rel references any PHVs that are marked as to
3795 : * be evaluated at the RTE_RESULT rel, because we can't
3796 : * postpone their evaluation in that case. But we only have
3797 : * to check this in cases where it's syntactically legal for
3798 : * the other input to have a LATERAL reference to the
3799 : * RTE_RESULT rel. Only RHSes of inner and left joins are
3800 : * allowed to have such refs.
3801 : */
3802 15322 : if ((varno = get_result_relid(root, j->larg)) != 0 &&
3803 96 : !find_dependent_phvs_in_jointree(root, j->rarg, varno))
3804 : {
3805 96 : remove_result_refs(root, varno, j->rarg);
3806 96 : if (j->quals != NULL && parent_quals == NULL)
3807 12 : jtnode = (Node *)
3808 12 : makeFromExpr(list_make1(j->rarg), j->quals);
3809 : else
3810 : {
3811 : /* Merge any quals up to parent */
3812 84 : if (j->quals != NULL)
3813 60 : *parent_quals = (Node *)
3814 60 : list_concat(castNode(List, j->quals),
3815 : castNode(List, *parent_quals));
3816 84 : jtnode = j->rarg;
3817 : }
3818 : }
3819 15226 : else if ((varno = get_result_relid(root, j->rarg)) != 0)
3820 : {
3821 850 : remove_result_refs(root, varno, j->larg);
3822 850 : if (j->quals != NULL && parent_quals == NULL)
3823 12 : jtnode = (Node *)
3824 12 : makeFromExpr(list_make1(j->larg), j->quals);
3825 : else
3826 : {
3827 : /* Merge any quals up to parent */
3828 838 : if (j->quals != NULL)
3829 600 : *parent_quals = (Node *)
3830 600 : list_concat(castNode(List, j->quals),
3831 : castNode(List, *parent_quals));
3832 838 : jtnode = j->larg;
3833 : }
3834 : }
3835 15322 : break;
3836 42202 : case JOIN_LEFT:
3837 :
3838 : /*
3839 : * We can simplify this case if the RHS is an RTE_RESULT, with
3840 : * two different possibilities:
3841 : *
3842 : * If the qual is empty (JOIN ON TRUE), then the join can be
3843 : * strength-reduced to a plain inner join, since each LHS row
3844 : * necessarily has exactly one join partner. So we can always
3845 : * discard the RHS, much as in the JOIN_INNER case above.
3846 : * (Again, the LHS could not contain a lateral reference to
3847 : * the RHS.)
3848 : *
3849 : * Otherwise, it's still true that each LHS row should be
3850 : * returned exactly once, and since the RHS returns no columns
3851 : * (unless there are PHVs that have to be evaluated there), we
3852 : * don't much care if it's null-extended or not. So in this
3853 : * case also, we can just ignore the qual and discard the left
3854 : * join.
3855 : */
3856 42202 : if ((varno = get_result_relid(root, j->rarg)) != 0 &&
3857 150 : (j->quals == NULL ||
3858 90 : !find_dependent_phvs(root, varno)))
3859 : {
3860 60 : remove_result_refs(root, varno, j->larg);
3861 60 : *dropped_outer_joins = bms_add_member(*dropped_outer_joins,
3862 : j->rtindex);
3863 60 : jtnode = j->larg;
3864 : }
3865 42202 : break;
3866 664 : case JOIN_SEMI:
3867 :
3868 : /*
3869 : * We may simplify this case if the RHS is an RTE_RESULT; the
3870 : * join qual becomes effectively just a filter qual for the
3871 : * LHS, since we should either return the LHS row or not. The
3872 : * filter clause must go into a new FromExpr if we can't push
3873 : * it up to the parent.
3874 : *
3875 : * There is a fine point about PHVs that are supposed to be
3876 : * evaluated at the RHS. Such PHVs could only appear in the
3877 : * semijoin's qual, since the rest of the query cannot
3878 : * reference any outputs of the semijoin's RHS. Therefore,
3879 : * they can't actually go to null before being examined, and
3880 : * it'd be OK to just remove the PHV wrapping. We don't have
3881 : * infrastructure for that, but remove_result_refs() will
3882 : * relabel them as to be evaluated at the LHS, which is fine.
3883 : *
3884 : * Also, we don't need to worry about removing traces of the
3885 : * join's rtindex, since it hasn't got one.
3886 : */
3887 664 : if ((varno = get_result_relid(root, j->rarg)) != 0)
3888 : {
3889 : Assert(j->rtindex == 0);
3890 36 : remove_result_refs(root, varno, j->larg);
3891 36 : if (j->quals != NULL && parent_quals == NULL)
3892 0 : jtnode = (Node *)
3893 0 : makeFromExpr(list_make1(j->larg), j->quals);
3894 : else
3895 : {
3896 : /* Merge any quals up to parent */
3897 36 : if (j->quals != NULL)
3898 36 : *parent_quals = (Node *)
3899 36 : list_concat(castNode(List, j->quals),
3900 : castNode(List, *parent_quals));
3901 36 : jtnode = j->larg;
3902 : }
3903 : }
3904 664 : break;
3905 2574 : case JOIN_FULL:
3906 : case JOIN_ANTI:
3907 : /* We have no special smarts for these cases */
3908 2574 : break;
3909 0 : default:
3910 : /* Note: JOIN_RIGHT should be gone at this point */
3911 0 : elog(ERROR, "unrecognized join type: %d",
3912 : (int) j->jointype);
3913 : break;
3914 : }
3915 : }
3916 : else
3917 0 : elog(ERROR, "unrecognized node type: %d",
3918 : (int) nodeTag(jtnode));
3919 641940 : return jtnode;
3920 : }
3921 :
3922 : /*
3923 : * get_result_relid
3924 : * If jtnode is a RangeTblRef for an RTE_RESULT RTE, return its relid;
3925 : * otherwise return 0.
3926 : */
3927 : static int
3928 78798 : get_result_relid(PlannerInfo *root, Node *jtnode)
3929 : {
3930 : int varno;
3931 :
3932 78798 : if (!IsA(jtnode, RangeTblRef))
3933 8150 : return 0;
3934 70648 : varno = ((RangeTblRef *) jtnode)->rtindex;
3935 70648 : if (rt_fetch(varno, root->parse->rtable)->rtekind != RTE_RESULT)
3936 69170 : return 0;
3937 1478 : return varno;
3938 : }
3939 :
3940 : /*
3941 : * remove_result_refs
3942 : * Helper routine for dropping an unneeded RTE_RESULT RTE.
3943 : *
3944 : * This doesn't physically remove the RTE from the jointree, because that's
3945 : * more easily handled in remove_useless_results_recurse. What it does do
3946 : * is the necessary cleanup in the rest of the tree: we must adjust any PHVs
3947 : * that may reference the RTE. Be sure to call this at a point where the
3948 : * jointree is valid (no disconnected nodes).
3949 : *
3950 : * Note that we don't need to process the append_rel_list, since RTEs
3951 : * referenced directly in the jointree won't be appendrel members.
3952 : *
3953 : * varno is the RTE_RESULT's relid.
3954 : * newjtloc is the jointree location at which any PHVs referencing the
3955 : * RTE_RESULT should be evaluated instead.
3956 : */
3957 : static void
3958 1364 : remove_result_refs(PlannerInfo *root, int varno, Node *newjtloc)
3959 : {
3960 : /* Fix up PlaceHolderVars as needed */
3961 : /* If there are no PHVs anywhere, we can skip this bit */
3962 1364 : if (root->glob->lastPHId != 0)
3963 : {
3964 : Relids subrelids;
3965 :
3966 244 : subrelids = get_relids_in_jointree(newjtloc, true, false);
3967 : Assert(!bms_is_empty(subrelids));
3968 244 : substitute_phv_relids((Node *) root->parse, varno, subrelids);
3969 244 : fix_append_rel_relids(root, varno, subrelids);
3970 : }
3971 :
3972 : /*
3973 : * We also need to remove any PlanRowMark referencing the RTE, but we
3974 : * postpone that work until we return to remove_useless_result_rtes.
3975 : */
3976 1364 : }
3977 :
3978 :
3979 : /*
3980 : * find_dependent_phvs - are there any PlaceHolderVars whose relids are
3981 : * exactly the given varno?
3982 : *
3983 : * find_dependent_phvs should be used when we want to see if there are
3984 : * any such PHVs anywhere in the Query. Another use-case is to see if
3985 : * a subtree of the join tree contains such PHVs; but for that, we have
3986 : * to look not only at the join tree nodes themselves but at the
3987 : * referenced RTEs. For that, use find_dependent_phvs_in_jointree.
3988 : */
3989 :
3990 : typedef struct
3991 : {
3992 : Relids relids;
3993 : int sublevels_up;
3994 : } find_dependent_phvs_context;
3995 :
3996 : static bool
3997 2400 : find_dependent_phvs_walker(Node *node,
3998 : find_dependent_phvs_context *context)
3999 : {
4000 2400 : if (node == NULL)
4001 564 : return false;
4002 1836 : if (IsA(node, PlaceHolderVar))
4003 : {
4004 156 : PlaceHolderVar *phv = (PlaceHolderVar *) node;
4005 :
4006 312 : if (phv->phlevelsup == context->sublevels_up &&
4007 156 : bms_equal(context->relids, phv->phrels))
4008 114 : return true;
4009 : /* fall through to examine children */
4010 : }
4011 1722 : if (IsA(node, Query))
4012 : {
4013 : /* Recurse into subselects */
4014 : bool result;
4015 :
4016 48 : context->sublevels_up++;
4017 48 : result = query_tree_walker((Query *) node,
4018 : find_dependent_phvs_walker,
4019 : context, 0);
4020 48 : context->sublevels_up--;
4021 48 : return result;
4022 : }
4023 : /* Shouldn't need to handle most planner auxiliary nodes here */
4024 : Assert(!IsA(node, SpecialJoinInfo));
4025 : Assert(!IsA(node, PlaceHolderInfo));
4026 : Assert(!IsA(node, MinMaxAggInfo));
4027 :
4028 1674 : return expression_tree_walker(node, find_dependent_phvs_walker, context);
4029 : }
4030 :
4031 : static bool
4032 90 : find_dependent_phvs(PlannerInfo *root, int varno)
4033 : {
4034 : find_dependent_phvs_context context;
4035 :
4036 : /* If there are no PHVs anywhere, we needn't work hard */
4037 90 : if (root->glob->lastPHId == 0)
4038 0 : return false;
4039 :
4040 90 : context.relids = bms_make_singleton(varno);
4041 90 : context.sublevels_up = 0;
4042 :
4043 90 : if (query_tree_walker(root->parse, find_dependent_phvs_walker, &context, 0))
4044 90 : return true;
4045 : /* The append_rel_list could be populated already, so check it too */
4046 0 : if (expression_tree_walker((Node *) root->append_rel_list,
4047 : find_dependent_phvs_walker,
4048 : &context))
4049 0 : return true;
4050 0 : return false;
4051 : }
4052 :
4053 : static bool
4054 442 : find_dependent_phvs_in_jointree(PlannerInfo *root, Node *node, int varno)
4055 : {
4056 : find_dependent_phvs_context context;
4057 : Relids subrelids;
4058 : int relid;
4059 :
4060 : /* If there are no PHVs anywhere, we needn't work hard */
4061 442 : if (root->glob->lastPHId == 0)
4062 376 : return false;
4063 :
4064 66 : context.relids = bms_make_singleton(varno);
4065 66 : context.sublevels_up = 0;
4066 :
4067 : /*
4068 : * See if the jointree fragment itself contains references (in join quals)
4069 : */
4070 66 : if (find_dependent_phvs_walker(node, &context))
4071 0 : return true;
4072 :
4073 : /*
4074 : * Otherwise, identify the set of referenced RTEs (we can ignore joins,
4075 : * since they should be flattened already, so their join alias lists no
4076 : * longer matter), and tediously check each RTE. We can ignore RTEs that
4077 : * are not marked LATERAL, though, since they couldn't possibly contain
4078 : * any cross-references to other RTEs.
4079 : */
4080 66 : subrelids = get_relids_in_jointree(node, false, false);
4081 66 : relid = -1;
4082 144 : while ((relid = bms_next_member(subrelids, relid)) >= 0)
4083 : {
4084 102 : RangeTblEntry *rte = rt_fetch(relid, root->parse->rtable);
4085 :
4086 126 : if (rte->lateral &&
4087 24 : range_table_entry_walker(rte, find_dependent_phvs_walker, &context, 0))
4088 24 : return true;
4089 : }
4090 :
4091 42 : return false;
4092 : }
4093 :
4094 : /*
4095 : * substitute_phv_relids - adjust PlaceHolderVar relid sets after pulling up
4096 : * a subquery or removing an RTE_RESULT jointree item
4097 : *
4098 : * Find any PlaceHolderVar nodes in the given tree that reference the
4099 : * pulled-up relid, and change them to reference the replacement relid(s).
4100 : *
4101 : * NOTE: although this has the form of a walker, we cheat and modify the
4102 : * nodes in-place. This should be OK since the tree was copied by
4103 : * pullup_replace_vars earlier. Avoid scribbling on the original values of
4104 : * the bitmapsets, though, because expression_tree_mutator doesn't copy those.
4105 : */
4106 :
4107 : typedef struct
4108 : {
4109 : int varno;
4110 : int sublevels_up;
4111 : Relids subrelids;
4112 : } substitute_phv_relids_context;
4113 :
4114 : static bool
4115 259898 : substitute_phv_relids_walker(Node *node,
4116 : substitute_phv_relids_context *context)
4117 : {
4118 259898 : if (node == NULL)
4119 94366 : return false;
4120 165532 : if (IsA(node, PlaceHolderVar))
4121 : {
4122 7416 : PlaceHolderVar *phv = (PlaceHolderVar *) node;
4123 :
4124 14800 : if (phv->phlevelsup == context->sublevels_up &&
4125 7384 : bms_is_member(context->varno, phv->phrels))
4126 : {
4127 10684 : phv->phrels = bms_union(phv->phrels,
4128 5342 : context->subrelids);
4129 5342 : phv->phrels = bms_del_member(phv->phrels,
4130 : context->varno);
4131 : /* Assert we haven't broken the PHV */
4132 : Assert(!bms_is_empty(phv->phrels));
4133 : }
4134 : /* fall through to examine children */
4135 : }
4136 165532 : if (IsA(node, Query))
4137 : {
4138 : /* Recurse into subselects */
4139 : bool result;
4140 :
4141 3868 : context->sublevels_up++;
4142 3868 : result = query_tree_walker((Query *) node,
4143 : substitute_phv_relids_walker,
4144 : context, 0);
4145 3868 : context->sublevels_up--;
4146 3868 : return result;
4147 : }
4148 : /* Shouldn't need to handle planner auxiliary nodes here */
4149 : Assert(!IsA(node, SpecialJoinInfo));
4150 : Assert(!IsA(node, AppendRelInfo));
4151 : Assert(!IsA(node, PlaceHolderInfo));
4152 : Assert(!IsA(node, MinMaxAggInfo));
4153 :
4154 161664 : return expression_tree_walker(node, substitute_phv_relids_walker, context);
4155 : }
4156 :
4157 : static void
4158 2302 : substitute_phv_relids(Node *node, int varno, Relids subrelids)
4159 : {
4160 : substitute_phv_relids_context context;
4161 :
4162 2302 : context.varno = varno;
4163 2302 : context.sublevels_up = 0;
4164 2302 : context.subrelids = subrelids;
4165 :
4166 : /*
4167 : * Must be prepared to start with a Query or a bare expression tree.
4168 : */
4169 2302 : query_or_expression_tree_walker(node,
4170 : substitute_phv_relids_walker,
4171 : &context,
4172 : 0);
4173 2302 : }
4174 :
4175 : /*
4176 : * fix_append_rel_relids: update RT-index fields of AppendRelInfo nodes
4177 : *
4178 : * When we pull up a subquery, any AppendRelInfo references to the subquery's
4179 : * RT index have to be replaced by the substituted relid (and there had better
4180 : * be only one). We also need to apply substitute_phv_relids to their
4181 : * translated_vars lists, since those might contain PlaceHolderVars.
4182 : *
4183 : * We assume we may modify the AppendRelInfo nodes in-place.
4184 : */
4185 : static void
4186 5586 : fix_append_rel_relids(PlannerInfo *root, int varno, Relids subrelids)
4187 : {
4188 : ListCell *l;
4189 5586 : int subvarno = -1;
4190 :
4191 : /*
4192 : * We only want to extract the member relid once, but we mustn't fail
4193 : * immediately if there are multiple members; it could be that none of the
4194 : * AppendRelInfo nodes refer to it. So compute it on first use. Note that
4195 : * bms_singleton_member will complain if set is not singleton.
4196 : */
4197 11644 : foreach(l, root->append_rel_list)
4198 : {
4199 6058 : AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
4200 :
4201 : /* The parent_relid shouldn't ever be a pullup target */
4202 : Assert(appinfo->parent_relid != varno);
4203 :
4204 6058 : if (appinfo->child_relid == varno)
4205 : {
4206 3452 : if (subvarno < 0)
4207 3452 : subvarno = bms_singleton_member(subrelids);
4208 3452 : appinfo->child_relid = subvarno;
4209 : }
4210 :
4211 : /* Also fix up any PHVs in its translated vars */
4212 6058 : if (root->glob->lastPHId != 0)
4213 114 : substitute_phv_relids((Node *) appinfo->translated_vars,
4214 : varno, subrelids);
4215 : }
4216 5586 : }
4217 :
4218 : /*
4219 : * get_relids_in_jointree: get set of RT indexes present in a jointree
4220 : *
4221 : * Base-relation relids are always included in the result.
4222 : * If include_outer_joins is true, outer-join RT indexes are included.
4223 : * If include_inner_joins is true, inner-join RT indexes are included.
4224 : *
4225 : * Note that for most purposes in the planner, outer joins are included
4226 : * in standard relid sets. Setting include_inner_joins true is only
4227 : * appropriate for special purposes during subquery flattening.
4228 : */
4229 : Relids
4230 86276 : get_relids_in_jointree(Node *jtnode, bool include_outer_joins,
4231 : bool include_inner_joins)
4232 : {
4233 86276 : Relids result = NULL;
4234 :
4235 86276 : if (jtnode == NULL)
4236 0 : return result;
4237 86276 : if (IsA(jtnode, RangeTblRef))
4238 : {
4239 43496 : int varno = ((RangeTblRef *) jtnode)->rtindex;
4240 :
4241 43496 : result = bms_make_singleton(varno);
4242 : }
4243 42780 : else if (IsA(jtnode, FromExpr))
4244 : {
4245 36590 : FromExpr *f = (FromExpr *) jtnode;
4246 : ListCell *l;
4247 :
4248 74376 : foreach(l, f->fromlist)
4249 : {
4250 37786 : result = bms_join(result,
4251 37786 : get_relids_in_jointree(lfirst(l),
4252 : include_outer_joins,
4253 : include_inner_joins));
4254 : }
4255 : }
4256 6190 : else if (IsA(jtnode, JoinExpr))
4257 : {
4258 6190 : JoinExpr *j = (JoinExpr *) jtnode;
4259 :
4260 6190 : result = get_relids_in_jointree(j->larg,
4261 : include_outer_joins,
4262 : include_inner_joins);
4263 6190 : result = bms_join(result,
4264 : get_relids_in_jointree(j->rarg,
4265 : include_outer_joins,
4266 : include_inner_joins));
4267 6190 : if (j->rtindex)
4268 : {
4269 5912 : if (j->jointype == JOIN_INNER)
4270 : {
4271 2636 : if (include_inner_joins)
4272 1010 : result = bms_add_member(result, j->rtindex);
4273 : }
4274 : else
4275 : {
4276 3276 : if (include_outer_joins)
4277 2172 : result = bms_add_member(result, j->rtindex);
4278 : }
4279 : }
4280 : }
4281 : else
4282 0 : elog(ERROR, "unrecognized node type: %d",
4283 : (int) nodeTag(jtnode));
4284 86276 : return result;
4285 : }
4286 :
4287 : /*
4288 : * get_relids_for_join: get set of base+OJ RT indexes making up a join
4289 : */
4290 : Relids
4291 358 : get_relids_for_join(Query *query, int joinrelid)
4292 : {
4293 : Node *jtnode;
4294 :
4295 358 : jtnode = find_jointree_node_for_rel((Node *) query->jointree,
4296 : joinrelid);
4297 358 : if (!jtnode)
4298 0 : elog(ERROR, "could not find join node %d", joinrelid);
4299 358 : return get_relids_in_jointree(jtnode, true, false);
4300 : }
4301 :
4302 : /*
4303 : * find_jointree_node_for_rel: locate jointree node for a base or join RT index
4304 : *
4305 : * Returns NULL if not found
4306 : */
4307 : static Node *
4308 1730 : find_jointree_node_for_rel(Node *jtnode, int relid)
4309 : {
4310 1730 : if (jtnode == NULL)
4311 0 : return NULL;
4312 1730 : if (IsA(jtnode, RangeTblRef))
4313 : {
4314 454 : int varno = ((RangeTblRef *) jtnode)->rtindex;
4315 :
4316 454 : if (relid == varno)
4317 0 : return jtnode;
4318 : }
4319 1276 : else if (IsA(jtnode, FromExpr))
4320 : {
4321 368 : FromExpr *f = (FromExpr *) jtnode;
4322 : ListCell *l;
4323 :
4324 386 : foreach(l, f->fromlist)
4325 : {
4326 386 : jtnode = find_jointree_node_for_rel(lfirst(l), relid);
4327 386 : if (jtnode)
4328 368 : return jtnode;
4329 : }
4330 : }
4331 908 : else if (IsA(jtnode, JoinExpr))
4332 : {
4333 908 : JoinExpr *j = (JoinExpr *) jtnode;
4334 :
4335 908 : if (relid == j->rtindex)
4336 358 : return jtnode;
4337 550 : jtnode = find_jointree_node_for_rel(j->larg, relid);
4338 550 : if (jtnode)
4339 114 : return jtnode;
4340 436 : jtnode = find_jointree_node_for_rel(j->rarg, relid);
4341 436 : if (jtnode)
4342 436 : return jtnode;
4343 : }
4344 : else
4345 0 : elog(ERROR, "unrecognized node type: %d",
4346 : (int) nodeTag(jtnode));
4347 454 : return NULL;
4348 : }
4349 :
4350 : /*
4351 : * get_nullingrels: collect info about which outer joins null which relations
4352 : *
4353 : * The result struct contains, for each leaf relation used in the query,
4354 : * the set of relids of outer joins that potentially null that rel.
4355 : */
4356 : static nullingrel_info *
4357 1120 : get_nullingrels(Query *parse)
4358 : {
4359 1120 : nullingrel_info *result = palloc_object(nullingrel_info);
4360 :
4361 1120 : result->rtlength = list_length(parse->rtable);
4362 1120 : result->nullingrels = palloc0_array(Relids, result->rtlength + 1);
4363 1120 : get_nullingrels_recurse((Node *) parse->jointree, NULL, result);
4364 1120 : return result;
4365 : }
4366 :
4367 : /*
4368 : * Recursive guts of get_nullingrels().
4369 : *
4370 : * Note: at any recursion level, the passed-down upper_nullingrels must be
4371 : * treated as a constant, but it can be stored directly into *info
4372 : * if we're at leaf level. Upper recursion levels do not free their mutated
4373 : * copies of the nullingrels, because those are probably referenced by
4374 : * at least one leaf rel.
4375 : */
4376 : static void
4377 5926 : get_nullingrels_recurse(Node *jtnode, Relids upper_nullingrels,
4378 : nullingrel_info *info)
4379 : {
4380 5926 : if (jtnode == NULL)
4381 0 : return;
4382 5926 : if (IsA(jtnode, RangeTblRef))
4383 : {
4384 3144 : int varno = ((RangeTblRef *) jtnode)->rtindex;
4385 :
4386 : Assert(varno > 0 && varno <= info->rtlength);
4387 3144 : info->nullingrels[varno] = upper_nullingrels;
4388 : }
4389 2782 : else if (IsA(jtnode, FromExpr))
4390 : {
4391 1186 : FromExpr *f = (FromExpr *) jtnode;
4392 : ListCell *l;
4393 :
4394 2800 : foreach(l, f->fromlist)
4395 : {
4396 1614 : get_nullingrels_recurse(lfirst(l), upper_nullingrels, info);
4397 : }
4398 : }
4399 1596 : else if (IsA(jtnode, JoinExpr))
4400 : {
4401 1596 : JoinExpr *j = (JoinExpr *) jtnode;
4402 : Relids local_nullingrels;
4403 :
4404 1596 : switch (j->jointype)
4405 : {
4406 574 : case JOIN_INNER:
4407 574 : get_nullingrels_recurse(j->larg, upper_nullingrels, info);
4408 574 : get_nullingrels_recurse(j->rarg, upper_nullingrels, info);
4409 574 : break;
4410 1016 : case JOIN_LEFT:
4411 : case JOIN_SEMI:
4412 : case JOIN_ANTI:
4413 1016 : local_nullingrels = bms_add_member(bms_copy(upper_nullingrels),
4414 : j->rtindex);
4415 1016 : get_nullingrels_recurse(j->larg, upper_nullingrels, info);
4416 1016 : get_nullingrels_recurse(j->rarg, local_nullingrels, info);
4417 1016 : break;
4418 6 : case JOIN_FULL:
4419 6 : local_nullingrels = bms_add_member(bms_copy(upper_nullingrels),
4420 : j->rtindex);
4421 6 : get_nullingrels_recurse(j->larg, local_nullingrels, info);
4422 6 : get_nullingrels_recurse(j->rarg, local_nullingrels, info);
4423 6 : break;
4424 0 : case JOIN_RIGHT:
4425 0 : local_nullingrels = bms_add_member(bms_copy(upper_nullingrels),
4426 : j->rtindex);
4427 0 : get_nullingrels_recurse(j->larg, local_nullingrels, info);
4428 0 : get_nullingrels_recurse(j->rarg, upper_nullingrels, info);
4429 0 : break;
4430 0 : default:
4431 0 : elog(ERROR, "unrecognized join type: %d",
4432 : (int) j->jointype);
4433 : break;
4434 : }
4435 : }
4436 : else
4437 0 : elog(ERROR, "unrecognized node type: %d",
4438 : (int) nodeTag(jtnode));
4439 : }
|