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