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