1 /*-------------------------------------------------------------------------
4 * Planner preprocessing for subqueries and join tree manipulation.
6 * NOTE: the intended sequence for invoking these operations is
8 * inline_set_returning_functions
10 * flatten_simple_union_all
11 * do expression preprocessing (including flattening JOIN alias vars)
15 * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
16 * Portions Copyright (c) 1994, Regents of the University of California
20 * src/backend/optimizer/prep/prepjointree.c
22 *-------------------------------------------------------------------------
26 #include "catalog/pg_type.h"
27 #include "nodes/makefuncs.h"
28 #include "nodes/nodeFuncs.h"
29 #include "optimizer/clauses.h"
30 #include "optimizer/placeholder.h"
31 #include "optimizer/prep.h"
32 #include "optimizer/subselect.h"
33 #include "optimizer/tlist.h"
34 #include "parser/parse_relation.h"
35 #include "parser/parsetree.h"
36 #include "rewrite/rewriteManip.h"
39 typedef struct pullup_replace_vars_context
42 List *targetlist; /* tlist of subquery being pulled up */
43 RangeTblEntry *target_rte; /* RTE of subquery */
44 Relids relids; /* relids within subquery, as numbered after
45 * pullup (set only if target_rte->lateral) */
46 bool *outer_hasSubLinks; /* -> outer query's hasSubLinks */
47 int varno; /* varno of subquery */
48 bool need_phvs; /* do we need PlaceHolderVars? */
49 bool wrap_non_vars; /* do we need 'em on *all* non-Vars? */
50 Node **rv_cache; /* cache for results with PHVs */
51 } pullup_replace_vars_context;
53 typedef struct reduce_outer_joins_state
55 Relids relids; /* base relids within this subtree */
56 bool contains_outer; /* does subtree contain outer join(s)? */
57 List *sub_states; /* List of states for subtree components */
58 } reduce_outer_joins_state;
60 static Node *pull_up_sublinks_jointree_recurse(PlannerInfo *root, Node *jtnode,
62 static Node *pull_up_sublinks_qual_recurse(PlannerInfo *root, Node *node,
63 Node **jtlink1, Relids available_rels1,
64 Node **jtlink2, Relids available_rels2);
65 static Node *pull_up_subqueries_recurse(PlannerInfo *root, Node *jtnode,
66 JoinExpr *lowest_outer_join,
67 JoinExpr *lowest_nulling_outer_join,
68 AppendRelInfo *containing_appendrel,
70 static Node *pull_up_simple_subquery(PlannerInfo *root, Node *jtnode,
72 JoinExpr *lowest_outer_join,
73 JoinExpr *lowest_nulling_outer_join,
74 AppendRelInfo *containing_appendrel,
76 static Node *pull_up_simple_union_all(PlannerInfo *root, Node *jtnode,
78 static void pull_up_union_leaf_queries(Node *setOp, PlannerInfo *root,
79 int parentRTindex, Query *setOpQuery,
81 static void make_setop_translation_list(Query *query, Index newvarno,
82 List **translated_vars);
83 static bool is_simple_subquery(Query *subquery, RangeTblEntry *rte,
84 JoinExpr *lowest_outer_join,
86 static Node *pull_up_simple_values(PlannerInfo *root, Node *jtnode,
88 static bool is_simple_values(PlannerInfo *root, RangeTblEntry *rte,
90 static bool is_simple_union_all(Query *subquery);
91 static bool is_simple_union_all_recurse(Node *setOp, Query *setOpQuery,
93 static bool is_safe_append_member(Query *subquery);
94 static bool jointree_contains_lateral_outer_refs(Node *jtnode, bool restricted,
95 Relids safe_upper_varnos);
96 static void replace_vars_in_jointree(Node *jtnode,
97 pullup_replace_vars_context *context,
98 JoinExpr *lowest_nulling_outer_join);
99 static Node *pullup_replace_vars(Node *expr,
100 pullup_replace_vars_context *context);
101 static Node *pullup_replace_vars_callback(Var *var,
102 replace_rte_variables_context *context);
103 static Query *pullup_replace_vars_subquery(Query *query,
104 pullup_replace_vars_context *context);
105 static Node *pull_up_subqueries_cleanup(Node *jtnode);
106 static reduce_outer_joins_state *reduce_outer_joins_pass1(Node *jtnode);
107 static void reduce_outer_joins_pass2(Node *jtnode,
108 reduce_outer_joins_state *state,
110 Relids nonnullable_rels,
111 List *nonnullable_vars,
112 List *forced_null_vars);
113 static void substitute_multiple_relids(Node *node,
114 int varno, Relids subrelids);
115 static void fix_append_rel_relids(List *append_rel_list, int varno,
117 static Node *find_jointree_node_for_rel(Node *jtnode, int relid);
122 * Attempt to pull up ANY and EXISTS SubLinks to be treated as
123 * semijoins or anti-semijoins.
125 * A clause "foo op ANY (sub-SELECT)" can be processed by pulling the
126 * sub-SELECT up to become a rangetable entry and treating the implied
127 * comparisons as quals of a semijoin. However, this optimization *only*
128 * works at the top level of WHERE or a JOIN/ON clause, because we cannot
129 * distinguish whether the ANY ought to return FALSE or NULL in cases
130 * involving NULL inputs. Also, in an outer join's ON clause we can only
131 * do this if the sublink is degenerate (ie, references only the nullable
132 * side of the join). In that case it is legal to push the semijoin
133 * down into the nullable side of the join. If the sublink references any
134 * nonnullable-side variables then it would have to be evaluated as part
135 * of the outer join, which makes things way too complicated.
137 * Under similar conditions, EXISTS and NOT EXISTS clauses can be handled
138 * by pulling up the sub-SELECT and creating a semijoin or anti-semijoin.
140 * This routine searches for such clauses and does the necessary parsetree
141 * transformations if any are found.
143 * This routine has to run before preprocess_expression(), so the quals
144 * clauses are not yet reduced to implicit-AND format, and are not guaranteed
145 * to be AND/OR-flat either. That means we need to recursively search through
146 * explicit AND clauses. We stop as soon as we hit a non-AND item.
149 pull_up_sublinks(PlannerInfo *root)
154 /* Begin recursion through the jointree */
155 jtnode = pull_up_sublinks_jointree_recurse(root,
156 (Node *) root->parse->jointree,
160 * root->parse->jointree must always be a FromExpr, so insert a dummy one
161 * if we got a bare RangeTblRef or JoinExpr out of the recursion.
163 if (IsA(jtnode, FromExpr))
164 root->parse->jointree = (FromExpr *) jtnode;
166 root->parse->jointree = makeFromExpr(list_make1(jtnode), NULL);
170 * Recurse through jointree nodes for pull_up_sublinks()
172 * In addition to returning the possibly-modified jointree node, we return
173 * a relids set of the contained rels into *relids.
176 pull_up_sublinks_jointree_recurse(PlannerInfo *root, Node *jtnode,
183 else if (IsA(jtnode, RangeTblRef))
185 int varno = ((RangeTblRef *) jtnode)->rtindex;
187 *relids = bms_make_singleton(varno);
188 /* jtnode is returned unmodified */
190 else if (IsA(jtnode, FromExpr))
192 FromExpr *f = (FromExpr *) jtnode;
193 List *newfromlist = NIL;
194 Relids frelids = NULL;
199 /* First, recurse to process children and collect their relids */
200 foreach(l, f->fromlist)
205 newchild = pull_up_sublinks_jointree_recurse(root,
208 newfromlist = lappend(newfromlist, newchild);
209 frelids = bms_join(frelids, childrelids);
211 /* Build the replacement FromExpr; no quals yet */
212 newf = makeFromExpr(newfromlist, NULL);
213 /* Set up a link representing the rebuilt jointree */
214 jtlink = (Node *) newf;
215 /* Now process qual --- all children are available for use */
216 newf->quals = pull_up_sublinks_qual_recurse(root, f->quals,
221 * Note that the result will be either newf, or a stack of JoinExprs
222 * with newf at the base. We rely on subsequent optimization steps to
223 * flatten this and rearrange the joins as needed.
225 * Although we could include the pulled-up subqueries in the returned
226 * relids, there's no need since upper quals couldn't refer to their
232 else if (IsA(jtnode, JoinExpr))
240 * Make a modifiable copy of join node, but don't bother copying its
243 j = (JoinExpr *) palloc(sizeof(JoinExpr));
244 memcpy(j, jtnode, sizeof(JoinExpr));
247 /* Recurse to process children and collect their relids */
248 j->larg = pull_up_sublinks_jointree_recurse(root, j->larg,
250 j->rarg = pull_up_sublinks_jointree_recurse(root, j->rarg,
254 * Now process qual, showing appropriate child relids as available,
255 * and attach any pulled-up jointree items at the right place. In the
256 * inner-join case we put new JoinExprs above the existing one (much
257 * as for a FromExpr-style join). In outer-join cases the new
258 * JoinExprs must go into the nullable side of the outer join. The
259 * point of the available_rels machinations is to ensure that we only
260 * pull up quals for which that's okay.
262 * We don't expect to see any pre-existing JOIN_SEMI or JOIN_ANTI
268 j->quals = pull_up_sublinks_qual_recurse(root, j->quals,
270 bms_union(leftrelids,
275 j->quals = pull_up_sublinks_qual_recurse(root, j->quals,
281 /* can't do anything with full-join quals */
284 j->quals = pull_up_sublinks_qual_recurse(root, j->quals,
290 elog(ERROR, "unrecognized join type: %d",
296 * Although we could include the pulled-up subqueries in the returned
297 * relids, there's no need since upper quals couldn't refer to their
298 * outputs anyway. But we *do* need to include the join's own rtindex
299 * because we haven't yet collapsed join alias variables, so upper
300 * levels would mistakenly think they couldn't use references to this
303 *relids = bms_join(leftrelids, rightrelids);
305 *relids = bms_add_member(*relids, j->rtindex);
309 elog(ERROR, "unrecognized node type: %d",
310 (int) nodeTag(jtnode));
315 * Recurse through top-level qual nodes for pull_up_sublinks()
317 * jtlink1 points to the link in the jointree where any new JoinExprs should
318 * be inserted if they reference available_rels1 (i.e., available_rels1
319 * denotes the relations present underneath jtlink1). Optionally, jtlink2 can
320 * point to a second link where new JoinExprs should be inserted if they
321 * reference available_rels2 (pass NULL for both those arguments if not used).
322 * Note that SubLinks referencing both sets of variables cannot be optimized.
323 * If we find multiple pull-up-able SubLinks, they'll get stacked onto jtlink1
324 * and/or jtlink2 in the order we encounter them. We rely on subsequent
325 * optimization to rearrange the stack if appropriate.
327 * Returns the replacement qual node, or NULL if the qual should be removed.
330 pull_up_sublinks_qual_recurse(PlannerInfo *root, Node *node,
331 Node **jtlink1, Relids available_rels1,
332 Node **jtlink2, Relids available_rels2)
336 if (IsA(node, SubLink))
338 SubLink *sublink = (SubLink *) node;
342 /* Is it a convertible ANY or EXISTS clause? */
343 if (sublink->subLinkType == ANY_SUBLINK)
345 if ((j = convert_ANY_sublink_to_join(root, sublink,
346 available_rels1)) != NULL)
348 /* Yes; insert the new join node into the join tree */
350 *jtlink1 = (Node *) j;
351 /* Recursively process pulled-up jointree nodes */
352 j->rarg = pull_up_sublinks_jointree_recurse(root,
357 * Now recursively process the pulled-up quals. Any inserted
358 * joins can get stacked onto either j->larg or j->rarg,
359 * depending on which rels they reference.
361 j->quals = pull_up_sublinks_qual_recurse(root,
367 /* Return NULL representing constant TRUE */
370 if (available_rels2 != NULL &&
371 (j = convert_ANY_sublink_to_join(root, sublink,
372 available_rels2)) != NULL)
374 /* Yes; insert the new join node into the join tree */
376 *jtlink2 = (Node *) j;
377 /* Recursively process pulled-up jointree nodes */
378 j->rarg = pull_up_sublinks_jointree_recurse(root,
383 * Now recursively process the pulled-up quals. Any inserted
384 * joins can get stacked onto either j->larg or j->rarg,
385 * depending on which rels they reference.
387 j->quals = pull_up_sublinks_qual_recurse(root,
393 /* Return NULL representing constant TRUE */
397 else if (sublink->subLinkType == EXISTS_SUBLINK)
399 if ((j = convert_EXISTS_sublink_to_join(root, sublink, false,
400 available_rels1)) != NULL)
402 /* Yes; insert the new join node into the join tree */
404 *jtlink1 = (Node *) j;
405 /* Recursively process pulled-up jointree nodes */
406 j->rarg = pull_up_sublinks_jointree_recurse(root,
411 * Now recursively process the pulled-up quals. Any inserted
412 * joins can get stacked onto either j->larg or j->rarg,
413 * depending on which rels they reference.
415 j->quals = pull_up_sublinks_qual_recurse(root,
421 /* Return NULL representing constant TRUE */
424 if (available_rels2 != NULL &&
425 (j = convert_EXISTS_sublink_to_join(root, sublink, false,
426 available_rels2)) != NULL)
428 /* Yes; insert the new join node into the join tree */
430 *jtlink2 = (Node *) j;
431 /* Recursively process pulled-up jointree nodes */
432 j->rarg = pull_up_sublinks_jointree_recurse(root,
437 * Now recursively process the pulled-up quals. Any inserted
438 * joins can get stacked onto either j->larg or j->rarg,
439 * depending on which rels they reference.
441 j->quals = pull_up_sublinks_qual_recurse(root,
447 /* Return NULL representing constant TRUE */
451 /* Else return it unmodified */
454 if (not_clause(node))
456 /* If the immediate argument of NOT is EXISTS, try to convert */
457 SubLink *sublink = (SubLink *) get_notclausearg((Expr *) node);
461 if (sublink && IsA(sublink, SubLink))
463 if (sublink->subLinkType == EXISTS_SUBLINK)
465 if ((j = convert_EXISTS_sublink_to_join(root, sublink, true,
466 available_rels1)) != NULL)
468 /* Yes; insert the new join node into the join tree */
470 *jtlink1 = (Node *) j;
471 /* Recursively process pulled-up jointree nodes */
472 j->rarg = pull_up_sublinks_jointree_recurse(root,
477 * Now recursively process the pulled-up quals. Because
478 * we are underneath a NOT, we can't pull up sublinks that
479 * reference the left-hand stuff, but it's still okay to
480 * pull up sublinks referencing j->rarg.
482 j->quals = pull_up_sublinks_qual_recurse(root,
487 /* Return NULL representing constant TRUE */
490 if (available_rels2 != NULL &&
491 (j = convert_EXISTS_sublink_to_join(root, sublink, true,
492 available_rels2)) != NULL)
494 /* Yes; insert the new join node into the join tree */
496 *jtlink2 = (Node *) j;
497 /* Recursively process pulled-up jointree nodes */
498 j->rarg = pull_up_sublinks_jointree_recurse(root,
503 * Now recursively process the pulled-up quals. Because
504 * we are underneath a NOT, we can't pull up sublinks that
505 * reference the left-hand stuff, but it's still okay to
506 * pull up sublinks referencing j->rarg.
508 j->quals = pull_up_sublinks_qual_recurse(root,
513 /* Return NULL representing constant TRUE */
518 /* Else return it unmodified */
521 if (and_clause(node))
523 /* Recurse into AND clause */
524 List *newclauses = NIL;
527 foreach(l, ((BoolExpr *) node)->args)
529 Node *oldclause = (Node *) lfirst(l);
532 newclause = pull_up_sublinks_qual_recurse(root,
539 newclauses = lappend(newclauses, newclause);
541 /* We might have got back fewer clauses than we started with */
542 if (newclauses == NIL)
544 else if (list_length(newclauses) == 1)
545 return (Node *) linitial(newclauses);
547 return (Node *) make_andclause(newclauses);
549 /* Stop if not an AND */
554 * inline_set_returning_functions
555 * Attempt to "inline" set-returning functions in the FROM clause.
557 * If an RTE_FUNCTION rtable entry invokes a set-returning function that
558 * contains just a simple SELECT, we can convert the rtable entry to an
559 * RTE_SUBQUERY entry exposing the SELECT directly. This is especially
560 * useful if the subquery can then be "pulled up" for further optimization,
561 * but we do it even if not, to reduce executor overhead.
563 * This has to be done before we have started to do any optimization of
564 * subqueries, else any such steps wouldn't get applied to subqueries
565 * obtained via inlining. However, we do it after pull_up_sublinks
566 * so that we can inline any functions used in SubLink subselects.
568 * Like most of the planner, this feels free to scribble on its input data
572 inline_set_returning_functions(PlannerInfo *root)
576 foreach(rt, root->parse->rtable)
578 RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
580 if (rte->rtekind == RTE_FUNCTION)
584 /* Check safety of expansion, and expand if possible */
585 funcquery = inline_set_returning_function(root, rte);
588 /* Successful expansion, replace the rtable entry */
589 rte->rtekind = RTE_SUBQUERY;
590 rte->subquery = funcquery;
591 rte->functions = NIL;
599 * Look for subqueries in the rangetable that can be pulled up into
600 * the parent query. If the subquery has no special features like
601 * grouping/aggregation then we can merge it into the parent's jointree.
602 * Also, subqueries that are simple UNION ALL structures can be
603 * converted into "append relations".
606 pull_up_subqueries(PlannerInfo *root)
608 /* Top level of jointree must always be a FromExpr */
609 Assert(IsA(root->parse->jointree, FromExpr));
610 /* Reset flag saying we need a deletion cleanup pass */
611 root->hasDeletedRTEs = false;
612 /* Recursion starts with no containing join nor appendrel */
613 root->parse->jointree = (FromExpr *)
614 pull_up_subqueries_recurse(root, (Node *) root->parse->jointree,
615 NULL, NULL, NULL, false);
616 /* Apply cleanup phase if necessary */
617 if (root->hasDeletedRTEs)
618 root->parse->jointree = (FromExpr *)
619 pull_up_subqueries_cleanup((Node *) root->parse->jointree);
620 Assert(IsA(root->parse->jointree, FromExpr));
624 * pull_up_subqueries_recurse
625 * Recursive guts of pull_up_subqueries.
627 * This recursively processes the jointree and returns a modified jointree.
628 * Or, if it's valid to drop the current node from the jointree completely,
631 * If this jointree node is within either side of an outer join, then
632 * lowest_outer_join references the lowest such JoinExpr node; otherwise
633 * it is NULL. We use this to constrain the effects of LATERAL subqueries.
635 * If this jointree node is within the nullable side of an outer join, then
636 * lowest_nulling_outer_join references the lowest such JoinExpr node;
637 * otherwise it is NULL. This forces use of the PlaceHolderVar mechanism for
638 * references to non-nullable targetlist items, but only for references above
641 * If we are looking at a member subquery of an append relation,
642 * containing_appendrel describes that relation; else it is NULL.
643 * This forces use of the PlaceHolderVar mechanism for all non-Var targetlist
644 * items, and puts some additional restrictions on what can be pulled up.
646 * deletion_ok is TRUE if the caller can cope with us returning NULL for a
647 * deletable leaf node (for example, a VALUES RTE that could be pulled up).
648 * If it's FALSE, we'll avoid pullup in such cases.
650 * A tricky aspect of this code is that if we pull up a subquery we have
651 * to replace Vars that reference the subquery's outputs throughout the
652 * parent query, including quals attached to jointree nodes above the one
653 * we are currently processing! We handle this by being careful not to
654 * change the jointree structure while recursing: no nodes other than leaf
655 * RangeTblRef entries and entirely-empty FromExprs will be replaced or
656 * deleted. Also, we can't turn pullup_replace_vars loose on the whole
657 * jointree, because it'll return a mutated copy of the tree; we have to
658 * invoke it just on the quals, instead. This behavior is what makes it
659 * reasonable to pass lowest_outer_join and lowest_nulling_outer_join as
660 * pointers rather than some more-indirect way of identifying the lowest
661 * OJs. Likewise, we don't replace append_rel_list members but only their
662 * substructure, so the containing_appendrel reference is safe to use.
664 * Because of the rule that no jointree nodes with substructure can be
665 * replaced, we cannot fully handle the case of deleting nodes from the tree:
666 * when we delete one child of a JoinExpr, we need to replace the JoinExpr
667 * with a FromExpr, and that can't happen here. Instead, we set the
668 * root->hasDeletedRTEs flag, which tells pull_up_subqueries() that an
669 * additional pass over the tree is needed to clean up.
672 pull_up_subqueries_recurse(PlannerInfo *root, Node *jtnode,
673 JoinExpr *lowest_outer_join,
674 JoinExpr *lowest_nulling_outer_join,
675 AppendRelInfo *containing_appendrel,
678 Assert(jtnode != NULL);
679 if (IsA(jtnode, RangeTblRef))
681 int varno = ((RangeTblRef *) jtnode)->rtindex;
682 RangeTblEntry *rte = rt_fetch(varno, root->parse->rtable);
685 * Is this a subquery RTE, and if so, is the subquery simple enough to
688 * If we are looking at an append-relation member, we can't pull it up
689 * unless is_safe_append_member says so.
691 if (rte->rtekind == RTE_SUBQUERY &&
692 is_simple_subquery(rte->subquery, rte,
693 lowest_outer_join, deletion_ok) &&
694 (containing_appendrel == NULL ||
695 is_safe_append_member(rte->subquery)))
696 return pull_up_simple_subquery(root, jtnode, rte,
698 lowest_nulling_outer_join,
699 containing_appendrel,
703 * Alternatively, is it a simple UNION ALL subquery? If so, flatten
704 * into an "append relation".
706 * It's safe to do this regardless of whether this query is itself an
707 * appendrel member. (If you're thinking we should try to flatten the
708 * two levels of appendrel together, you're right; but we handle that
709 * in set_append_rel_pathlist, not here.)
711 if (rte->rtekind == RTE_SUBQUERY &&
712 is_simple_union_all(rte->subquery))
713 return pull_up_simple_union_all(root, jtnode, rte);
716 * Or perhaps it's a simple VALUES RTE?
718 * We don't allow VALUES pullup below an outer join nor into an
719 * appendrel (such cases are impossible anyway at the moment).
721 if (rte->rtekind == RTE_VALUES &&
722 lowest_outer_join == NULL &&
723 containing_appendrel == NULL &&
724 is_simple_values(root, rte, deletion_ok))
725 return pull_up_simple_values(root, jtnode, rte);
727 /* Otherwise, do nothing at this node. */
729 else if (IsA(jtnode, FromExpr))
731 FromExpr *f = (FromExpr *) jtnode;
732 bool have_undeleted_child = false;
735 Assert(containing_appendrel == NULL);
738 * If the FromExpr has quals, it's not deletable even if its parent
739 * would allow deletion.
744 foreach(l, f->fromlist)
747 * In a non-deletable FromExpr, we can allow deletion of child
748 * nodes so long as at least one child remains; so it's okay
749 * either if any previous child survives, or if there's more to
750 * come. If all children are deletable in themselves, we'll force
751 * the last one to remain unflattened.
753 * As a separate matter, we can allow deletion of all children of
754 * the top-level FromExpr in a query, since that's a special case
757 bool sub_deletion_ok = (deletion_ok ||
758 have_undeleted_child ||
760 f == root->parse->jointree);
762 lfirst(l) = pull_up_subqueries_recurse(root, lfirst(l),
764 lowest_nulling_outer_join,
767 if (lfirst(l) != NULL)
768 have_undeleted_child = true;
771 if (deletion_ok && !have_undeleted_child)
773 /* OK to delete this FromExpr entirely */
774 root->hasDeletedRTEs = true; /* probably is set already */
778 else if (IsA(jtnode, JoinExpr))
780 JoinExpr *j = (JoinExpr *) jtnode;
782 Assert(containing_appendrel == NULL);
783 /* Recurse, being careful to tell myself when inside outer join */
789 * INNER JOIN can allow deletion of either child node, but not
790 * both. So right child gets permission to delete only if
791 * left child didn't get removed.
793 j->larg = pull_up_subqueries_recurse(root, j->larg,
795 lowest_nulling_outer_join,
798 j->rarg = pull_up_subqueries_recurse(root, j->rarg,
800 lowest_nulling_outer_join,
807 j->larg = pull_up_subqueries_recurse(root, j->larg,
809 lowest_nulling_outer_join,
812 j->rarg = pull_up_subqueries_recurse(root, j->rarg,
819 j->larg = pull_up_subqueries_recurse(root, j->larg,
824 j->rarg = pull_up_subqueries_recurse(root, j->rarg,
831 j->larg = pull_up_subqueries_recurse(root, j->larg,
836 j->rarg = pull_up_subqueries_recurse(root, j->rarg,
838 lowest_nulling_outer_join,
843 elog(ERROR, "unrecognized join type: %d",
849 elog(ERROR, "unrecognized node type: %d",
850 (int) nodeTag(jtnode));
855 * pull_up_simple_subquery
856 * Attempt to pull up a single simple subquery.
858 * jtnode is a RangeTblRef that has been tentatively identified as a simple
859 * subquery by pull_up_subqueries. We return the replacement jointree node,
860 * or NULL if the subquery can be deleted entirely, or jtnode itself if we
861 * determine that the subquery can't be pulled up after all.
863 * rte is the RangeTblEntry referenced by jtnode. Remaining parameters are
864 * as for pull_up_subqueries_recurse.
867 pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte,
868 JoinExpr *lowest_outer_join,
869 JoinExpr *lowest_nulling_outer_join,
870 AppendRelInfo *containing_appendrel,
873 Query *parse = root->parse;
874 int varno = ((RangeTblRef *) jtnode)->rtindex;
876 PlannerInfo *subroot;
878 pullup_replace_vars_context rvcontext;
882 * Need a modifiable copy of the subquery to hack on. Even if we didn't
883 * sometimes choose not to pull up below, we must do this to avoid
884 * problems if the same subquery is referenced from multiple jointree
885 * items (which can't happen normally, but might after rule rewriting).
887 subquery = copyObject(rte->subquery);
890 * Create a PlannerInfo data structure for this subquery.
892 * NOTE: the next few steps should match the first processing in
893 * subquery_planner(). Can we refactor to avoid code duplication, or
894 * would that just make things uglier?
896 subroot = makeNode(PlannerInfo);
897 subroot->parse = subquery;
898 subroot->glob = root->glob;
899 subroot->query_level = root->query_level;
900 subroot->parent_root = root->parent_root;
901 subroot->plan_params = NIL;
902 subroot->planner_cxt = CurrentMemoryContext;
903 subroot->init_plans = NIL;
904 subroot->cte_plan_ids = NIL;
905 subroot->multiexpr_params = NIL;
906 subroot->eq_classes = NIL;
907 subroot->append_rel_list = NIL;
908 subroot->rowMarks = NIL;
909 subroot->hasRecursion = false;
910 subroot->wt_param_id = -1;
911 subroot->non_recursive_plan = NULL;
913 /* No CTEs to worry about */
914 Assert(subquery->cteList == NIL);
917 * Pull up any SubLinks within the subquery's quals, so that we don't
918 * leave unoptimized SubLinks behind.
920 if (subquery->hasSubLinks)
921 pull_up_sublinks(subroot);
924 * Similarly, inline any set-returning functions in its rangetable.
926 inline_set_returning_functions(subroot);
929 * Recursively pull up the subquery's subqueries, so that
930 * pull_up_subqueries' processing is complete for its jointree and
933 * Note: it's okay that the subquery's recursion starts with NULL for
934 * containing-join info, even if we are within an outer join in the upper
935 * query; the lower query starts with a clean slate for outer-join
936 * semantics. Likewise, we needn't pass down appendrel state.
938 pull_up_subqueries(subroot);
941 * Now we must recheck whether the subquery is still simple enough to pull
942 * up. If not, abandon processing it.
944 * We don't really need to recheck all the conditions involved, but it's
945 * easier just to keep this "if" looking the same as the one in
946 * pull_up_subqueries_recurse.
948 if (is_simple_subquery(subquery, rte,
949 lowest_outer_join, deletion_ok) &&
950 (containing_appendrel == NULL || is_safe_append_member(subquery)))
957 * Give up, return unmodified RangeTblRef.
959 * Note: The work we just did will be redone when the subquery gets
960 * planned on its own. Perhaps we could avoid that by storing the
961 * modified subquery back into the rangetable, but I'm not gonna risk
968 * We must flatten any join alias Vars in the subquery's targetlist,
969 * because pulling up the subquery's subqueries might have changed their
970 * expansions into arbitrary expressions, which could affect
971 * pullup_replace_vars' decisions about whether PlaceHolderVar wrappers
972 * are needed for tlist entries. (Likely it'd be better to do
973 * flatten_join_alias_vars on the whole query tree at some earlier stage,
974 * maybe even in the rewriter; but for now let's just fix this case here.)
976 subquery->targetList = (List *)
977 flatten_join_alias_vars(subroot, (Node *) subquery->targetList);
980 * Adjust level-0 varnos in subquery so that we can append its rangetable
981 * to upper query's. We have to fix the subquery's append_rel_list as
984 rtoffset = list_length(parse->rtable);
985 OffsetVarNodes((Node *) subquery, rtoffset, 0);
986 OffsetVarNodes((Node *) subroot->append_rel_list, rtoffset, 0);
989 * Upper-level vars in subquery are now one level closer to their parent
992 IncrementVarSublevelsUp((Node *) subquery, -1, 1);
993 IncrementVarSublevelsUp((Node *) subroot->append_rel_list, -1, 1);
996 * The subquery's targetlist items are now in the appropriate form to
997 * insert into the top query, but if we are under an outer join then
998 * non-nullable items and lateral references may have to be turned into
999 * PlaceHolderVars. If we are dealing with an appendrel member then
1000 * anything that's not a simple Var has to be turned into a
1001 * PlaceHolderVar. Set up required context data for pullup_replace_vars.
1003 rvcontext.root = root;
1004 rvcontext.targetlist = subquery->targetList;
1005 rvcontext.target_rte = rte;
1007 rvcontext.relids = get_relids_in_jointree((Node *) subquery->jointree,
1009 else /* won't need relids */
1010 rvcontext.relids = NULL;
1011 rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
1012 rvcontext.varno = varno;
1013 rvcontext.need_phvs = (lowest_nulling_outer_join != NULL ||
1014 containing_appendrel != NULL);
1015 rvcontext.wrap_non_vars = (containing_appendrel != NULL);
1016 /* initialize cache array with indexes 0 .. length(tlist) */
1017 rvcontext.rv_cache = palloc0((list_length(subquery->targetList) + 1) *
1021 * Replace all of the top query's references to the subquery's outputs
1022 * with copies of the adjusted subtlist items, being careful not to
1023 * replace any of the jointree structure. (This'd be a lot cleaner if we
1024 * could use query_tree_mutator.) We have to use PHVs in the targetList,
1025 * returningList, and havingQual, since those are certainly above any
1026 * outer join. replace_vars_in_jointree tracks its location in the
1027 * jointree and uses PHVs or not appropriately.
1029 parse->targetList = (List *)
1030 pullup_replace_vars((Node *) parse->targetList, &rvcontext);
1031 parse->returningList = (List *)
1032 pullup_replace_vars((Node *) parse->returningList, &rvcontext);
1033 if (parse->onConflict)
1034 parse->onConflict->onConflictSet = (List *)
1035 pullup_replace_vars((Node *) parse->onConflict->onConflictSet, &rvcontext);
1036 replace_vars_in_jointree((Node *) parse->jointree, &rvcontext,
1037 lowest_nulling_outer_join);
1038 Assert(parse->setOperations == NULL);
1039 parse->havingQual = pullup_replace_vars(parse->havingQual, &rvcontext);
1042 * Replace references in the translated_vars lists of appendrels. When
1043 * pulling up an appendrel member, we do not need PHVs in the list of the
1044 * parent appendrel --- there isn't any outer join between. Elsewhere, use
1045 * PHVs for safety. (This analysis could be made tighter but it seems
1046 * unlikely to be worth much trouble.)
1048 foreach(lc, root->append_rel_list)
1050 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(lc);
1051 bool save_need_phvs = rvcontext.need_phvs;
1053 if (appinfo == containing_appendrel)
1054 rvcontext.need_phvs = false;
1055 appinfo->translated_vars = (List *)
1056 pullup_replace_vars((Node *) appinfo->translated_vars, &rvcontext);
1057 rvcontext.need_phvs = save_need_phvs;
1061 * Replace references in the joinaliasvars lists of join RTEs.
1063 * You might think that we could avoid using PHVs for alias vars of joins
1064 * below lowest_nulling_outer_join, but that doesn't work because the
1065 * alias vars could be referenced above that join; we need the PHVs to be
1066 * present in such references after the alias vars get flattened. (It
1067 * might be worth trying to be smarter here, someday.)
1069 foreach(lc, parse->rtable)
1071 RangeTblEntry *otherrte = (RangeTblEntry *) lfirst(lc);
1073 if (otherrte->rtekind == RTE_JOIN)
1074 otherrte->joinaliasvars = (List *)
1075 pullup_replace_vars((Node *) otherrte->joinaliasvars,
1080 * If the subquery had a LATERAL marker, propagate that to any of its
1081 * child RTEs that could possibly now contain lateral cross-references.
1082 * The children might or might not contain any actual lateral
1083 * cross-references, but we have to mark the pulled-up child RTEs so that
1084 * later planner stages will check for such.
1088 foreach(lc, subquery->rtable)
1090 RangeTblEntry *child_rte = (RangeTblEntry *) lfirst(lc);
1092 switch (child_rte->rtekind)
1097 child_rte->lateral = true;
1102 /* these can't contain any lateral references */
1109 * Now append the adjusted rtable entries to upper query. (We hold off
1110 * until after fixing the upper rtable entries; no point in running that
1111 * code on the subquery ones too.)
1113 parse->rtable = list_concat(parse->rtable, subquery->rtable);
1116 * Pull up any FOR UPDATE/SHARE markers, too. (OffsetVarNodes already
1117 * adjusted the marker rtindexes, so just concat the lists.)
1119 parse->rowMarks = list_concat(parse->rowMarks, subquery->rowMarks);
1122 * We also have to fix the relid sets of any PlaceHolderVar nodes in the
1123 * parent query. (This could perhaps be done by pullup_replace_vars(),
1124 * but it seems cleaner to use two passes.) Note in particular that any
1125 * PlaceHolderVar nodes just created by pullup_replace_vars() will be
1126 * adjusted, so having created them with the subquery's varno is correct.
1128 * Likewise, relids appearing in AppendRelInfo nodes have to be fixed. We
1129 * already checked that this won't require introducing multiple subrelids
1130 * into the single-slot AppendRelInfo structs.
1132 if (parse->hasSubLinks || root->glob->lastPHId != 0 ||
1133 root->append_rel_list)
1137 subrelids = get_relids_in_jointree((Node *) subquery->jointree, false);
1138 substitute_multiple_relids((Node *) parse, varno, subrelids);
1139 fix_append_rel_relids(root->append_rel_list, varno, subrelids);
1143 * And now add subquery's AppendRelInfos to our list.
1145 root->append_rel_list = list_concat(root->append_rel_list,
1146 subroot->append_rel_list);
1149 * We don't have to do the equivalent bookkeeping for outer-join or
1150 * LATERAL info, because that hasn't been set up yet. placeholder_list
1153 Assert(root->join_info_list == NIL);
1154 Assert(subroot->join_info_list == NIL);
1155 Assert(root->lateral_info_list == NIL);
1156 Assert(subroot->lateral_info_list == NIL);
1157 Assert(root->placeholder_list == NIL);
1158 Assert(subroot->placeholder_list == NIL);
1161 * Miscellaneous housekeeping.
1163 * Although replace_rte_variables() faithfully updated parse->hasSubLinks
1164 * if it copied any SubLinks out of the subquery's targetlist, we still
1165 * could have SubLinks added to the query in the expressions of FUNCTION
1166 * and VALUES RTEs copied up from the subquery. So it's necessary to copy
1167 * subquery->hasSubLinks anyway. Perhaps this can be improved someday.
1169 parse->hasSubLinks |= subquery->hasSubLinks;
1172 * subquery won't be pulled up if it hasAggs or hasWindowFuncs, so no work
1173 * needed on those flags
1177 * Return the adjusted subquery jointree to replace the RangeTblRef entry
1178 * in parent's jointree; or, if we're flattening a subquery with empty
1179 * FROM list, return NULL to signal deletion of the subquery from the
1180 * parent jointree (and set hasDeletedRTEs to ensure cleanup later).
1182 if (subquery->jointree->fromlist == NIL)
1184 Assert(deletion_ok);
1185 Assert(subquery->jointree->quals == NULL);
1186 root->hasDeletedRTEs = true;
1190 return (Node *) subquery->jointree;
1194 * pull_up_simple_union_all
1195 * Pull up a single simple UNION ALL subquery.
1197 * jtnode is a RangeTblRef that has been identified as a simple UNION ALL
1198 * subquery by pull_up_subqueries. We pull up the leaf subqueries and
1199 * build an "append relation" for the union set. The result value is just
1200 * jtnode, since we don't actually need to change the query jointree.
1203 pull_up_simple_union_all(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte)
1205 int varno = ((RangeTblRef *) jtnode)->rtindex;
1206 Query *subquery = rte->subquery;
1207 int rtoffset = list_length(root->parse->rtable);
1211 * Make a modifiable copy of the subquery's rtable, so we can adjust
1212 * upper-level Vars in it. There are no such Vars in the setOperations
1213 * tree proper, so fixing the rtable should be sufficient.
1215 rtable = copyObject(subquery->rtable);
1218 * Upper-level vars in subquery are now one level closer to their parent
1219 * than before. We don't have to worry about offsetting varnos, though,
1220 * because the UNION leaf queries can't cross-reference each other.
1222 IncrementVarSublevelsUp_rtable(rtable, -1, 1);
1225 * If the UNION ALL subquery had a LATERAL marker, propagate that to all
1226 * its children. The individual children might or might not contain any
1227 * actual lateral cross-references, but we have to mark the pulled-up
1228 * child RTEs so that later planner stages will check for such.
1236 RangeTblEntry *child_rte = (RangeTblEntry *) lfirst(rt);
1238 Assert(child_rte->rtekind == RTE_SUBQUERY);
1239 child_rte->lateral = true;
1244 * Append child RTEs to parent rtable.
1246 root->parse->rtable = list_concat(root->parse->rtable, rtable);
1249 * Recursively scan the subquery's setOperations tree and add
1250 * AppendRelInfo nodes for leaf subqueries to the parent's
1251 * append_rel_list. Also apply pull_up_subqueries to the leaf subqueries.
1253 Assert(subquery->setOperations);
1254 pull_up_union_leaf_queries(subquery->setOperations, root, varno, subquery,
1258 * Mark the parent as an append relation.
1266 * pull_up_union_leaf_queries -- recursive guts of pull_up_simple_union_all
1268 * Build an AppendRelInfo for each leaf query in the setop tree, and then
1269 * apply pull_up_subqueries to the leaf query.
1271 * Note that setOpQuery is the Query containing the setOp node, whose tlist
1272 * contains references to all the setop output columns. When called from
1273 * pull_up_simple_union_all, this is *not* the same as root->parse, which is
1274 * the parent Query we are pulling up into.
1276 * parentRTindex is the appendrel parent's index in root->parse->rtable.
1278 * The child RTEs have already been copied to the parent. childRToffset
1279 * tells us where in the parent's range table they were copied. When called
1280 * from flatten_simple_union_all, childRToffset is 0 since the child RTEs
1281 * were already in root->parse->rtable and no RT index adjustment is needed.
1284 pull_up_union_leaf_queries(Node *setOp, PlannerInfo *root, int parentRTindex,
1285 Query *setOpQuery, int childRToffset)
1287 if (IsA(setOp, RangeTblRef))
1289 RangeTblRef *rtr = (RangeTblRef *) setOp;
1291 AppendRelInfo *appinfo;
1294 * Calculate the index in the parent's range table
1296 childRTindex = childRToffset + rtr->rtindex;
1299 * Build a suitable AppendRelInfo, and attach to parent's list.
1301 appinfo = makeNode(AppendRelInfo);
1302 appinfo->parent_relid = parentRTindex;
1303 appinfo->child_relid = childRTindex;
1304 appinfo->parent_reltype = InvalidOid;
1305 appinfo->child_reltype = InvalidOid;
1306 make_setop_translation_list(setOpQuery, childRTindex,
1307 &appinfo->translated_vars);
1308 appinfo->parent_reloid = InvalidOid;
1309 root->append_rel_list = lappend(root->append_rel_list, appinfo);
1312 * Recursively apply pull_up_subqueries to the new child RTE. (We
1313 * must build the AppendRelInfo first, because this will modify it.)
1314 * Note that we can pass NULL for containing-join info even if we're
1315 * actually under an outer join, because the child's expressions
1316 * aren't going to propagate up to the join. Also, we ignore the
1317 * possibility that pull_up_subqueries_recurse() returns a different
1318 * jointree node than what we pass it; if it does, the important thing
1319 * is that it replaced the child relid in the AppendRelInfo node.
1321 rtr = makeNode(RangeTblRef);
1322 rtr->rtindex = childRTindex;
1323 (void) pull_up_subqueries_recurse(root, (Node *) rtr,
1324 NULL, NULL, appinfo, false);
1326 else if (IsA(setOp, SetOperationStmt))
1328 SetOperationStmt *op = (SetOperationStmt *) setOp;
1330 /* Recurse to reach leaf queries */
1331 pull_up_union_leaf_queries(op->larg, root, parentRTindex, setOpQuery,
1333 pull_up_union_leaf_queries(op->rarg, root, parentRTindex, setOpQuery,
1338 elog(ERROR, "unrecognized node type: %d",
1339 (int) nodeTag(setOp));
1344 * make_setop_translation_list
1345 * Build the list of translations from parent Vars to child Vars for
1346 * a UNION ALL member. (At this point it's just a simple list of
1347 * referencing Vars, but if we succeed in pulling up the member
1348 * subquery, the Vars will get replaced by pulled-up expressions.)
1351 make_setop_translation_list(Query *query, Index newvarno,
1352 List **translated_vars)
1357 foreach(l, query->targetList)
1359 TargetEntry *tle = (TargetEntry *) lfirst(l);
1364 vars = lappend(vars, makeVarFromTargetEntry(newvarno, tle));
1367 *translated_vars = vars;
1371 * is_simple_subquery
1372 * Check a subquery in the range table to see if it's simple enough
1373 * to pull up into the parent query.
1375 * rte is the RTE_SUBQUERY RangeTblEntry that contained the subquery.
1376 * (Note subquery is not necessarily equal to rte->subquery; it could be a
1377 * processed copy of that.)
1378 * lowest_outer_join is the lowest outer join above the subquery, or NULL.
1379 * deletion_ok is TRUE if it'd be okay to delete the subquery entirely.
1382 is_simple_subquery(Query *subquery, RangeTblEntry *rte,
1383 JoinExpr *lowest_outer_join,
1387 * Let's just make sure it's a valid subselect ...
1389 if (!IsA(subquery, Query) ||
1390 subquery->commandType != CMD_SELECT ||
1391 subquery->utilityStmt != NULL)
1392 elog(ERROR, "subquery is bogus");
1395 * Can't currently pull up a query with setops (unless it's simple UNION
1396 * ALL, which is handled by a different code path). Maybe after querytree
1399 if (subquery->setOperations)
1403 * Can't pull up a subquery involving grouping, aggregation, sorting,
1404 * limiting, or WITH. (XXX WITH could possibly be allowed later)
1406 * We also don't pull up a subquery that has explicit FOR UPDATE/SHARE
1407 * clauses, because pullup would cause the locking to occur semantically
1408 * higher than it should. Implicit FOR UPDATE/SHARE is okay because in
1409 * that case the locking was originally declared in the upper query
1412 if (subquery->hasAggs ||
1413 subquery->hasWindowFuncs ||
1414 subquery->groupClause ||
1415 subquery->havingQual ||
1416 subquery->sortClause ||
1417 subquery->distinctClause ||
1418 subquery->limitOffset ||
1419 subquery->limitCount ||
1420 subquery->hasForUpdate ||
1425 * Don't pull up if the RTE represents a security-barrier view; we
1426 * couldn't prevent information leakage once the RTE's Vars are scattered
1427 * about in the upper query.
1429 if (rte->security_barrier)
1433 * Don't pull up a subquery with an empty jointree, unless it has no quals
1434 * and deletion_ok is TRUE. query_planner() will correctly generate a
1435 * Result plan for a jointree that's totally empty, but we can't cope with
1436 * an empty FromExpr appearing lower down in a jointree: we identify join
1437 * rels via baserelid sets, so we couldn't distinguish a join containing
1438 * such a FromExpr from one without it. This would for example break the
1439 * PlaceHolderVar mechanism, since we'd have no way to identify where to
1440 * evaluate a PHV coming out of the subquery. We can only handle such
1441 * cases if the place where the subquery is linked is a FromExpr or inner
1442 * JOIN that would still be nonempty after removal of the subquery, so
1443 * that it's still identifiable via its contained baserelids. Safe
1444 * contexts are signaled by deletion_ok. But even in a safe context, we
1445 * must keep the subquery if it has any quals, because it's unclear where
1446 * to put them in the upper query. (Note that deletion of a subquery is
1447 * also dependent on the check below that its targetlist contains no
1448 * set-returning functions. Deletion from a FROM list or inner JOIN is
1449 * okay only if the subquery must return exactly one row.)
1451 if (subquery->jointree->fromlist == NIL &&
1452 (subquery->jointree->quals || !deletion_ok))
1456 * If the subquery is LATERAL, check for pullup restrictions from that.
1461 Relids safe_upper_varnos;
1464 * The subquery's WHERE and JOIN/ON quals mustn't contain any lateral
1465 * references to rels outside a higher outer join (including the case
1466 * where the outer join is within the subquery itself). In such a
1467 * case, pulling up would result in a situation where we need to
1468 * postpone quals from below an outer join to above it, which is
1469 * probably completely wrong and in any case is a complication that
1470 * doesn't seem worth addressing at the moment.
1472 if (lowest_outer_join != NULL)
1475 safe_upper_varnos = get_relids_in_jointree((Node *) lowest_outer_join,
1481 safe_upper_varnos = NULL; /* doesn't matter */
1484 if (jointree_contains_lateral_outer_refs((Node *) subquery->jointree,
1485 restricted, safe_upper_varnos))
1489 * If there's an outer join above the LATERAL subquery, also disallow
1490 * pullup if the subquery's targetlist has any references to rels
1491 * outside the outer join, since these might get pulled into quals
1492 * above the subquery (but in or below the outer join) and then lead
1493 * to qual-postponement issues similar to the case checked for above.
1494 * (We wouldn't need to prevent pullup if no such references appear in
1495 * outer-query quals, but we don't have enough info here to check
1496 * that. Also, maybe this restriction could be removed if we forced
1497 * such refs to be wrapped in PlaceHolderVars, even when they're below
1498 * the nearest outer join? But it's a pretty hokey usage, so not
1499 * clear this is worth sweating over.)
1501 if (lowest_outer_join != NULL)
1503 Relids lvarnos = pull_varnos_of_level((Node *) subquery->targetList, 1);
1505 if (!bms_is_subset(lvarnos, safe_upper_varnos))
1511 * Don't pull up a subquery that has any set-returning functions in its
1512 * targetlist. Otherwise we might well wind up inserting set-returning
1513 * functions into places where they mustn't go, such as quals of higher
1514 * queries. This also ensures deletion of an empty jointree is valid.
1516 if (expression_returns_set((Node *) subquery->targetList))
1520 * Don't pull up a subquery that has any volatile functions in its
1521 * targetlist. Otherwise we might introduce multiple evaluations of these
1522 * functions, if they get copied to multiple places in the upper query,
1523 * leading to surprising results. (Note: the PlaceHolderVar mechanism
1524 * doesn't quite guarantee single evaluation; else we could pull up anyway
1525 * and just wrap such items in PlaceHolderVars ...)
1527 if (contain_volatile_functions((Node *) subquery->targetList))
1534 * pull_up_simple_values
1535 * Pull up a single simple VALUES RTE.
1537 * jtnode is a RangeTblRef that has been identified as a simple VALUES RTE
1538 * by pull_up_subqueries. We always return NULL indicating that the RTE
1539 * can be deleted entirely (all failure cases should have been detected by
1540 * is_simple_values()).
1542 * rte is the RangeTblEntry referenced by jtnode. Because of the limited
1543 * possible usage of VALUES RTEs, we do not need the remaining parameters
1544 * of pull_up_subqueries_recurse.
1547 pull_up_simple_values(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte)
1549 Query *parse = root->parse;
1550 int varno = ((RangeTblRef *) jtnode)->rtindex;
1554 pullup_replace_vars_context rvcontext;
1557 Assert(rte->rtekind == RTE_VALUES);
1558 Assert(list_length(rte->values_lists) == 1);
1561 * Need a modifiable copy of the VALUES list to hack on, just in case it's
1562 * multiply referenced.
1564 values_list = (List *) copyObject(linitial(rte->values_lists));
1567 * The VALUES RTE can't contain any Vars of level zero, let alone any that
1568 * are join aliases, so no need to flatten join alias Vars.
1570 Assert(!contain_vars_of_level((Node *) values_list, 0));
1573 * Set up required context data for pullup_replace_vars. In particular,
1574 * we have to make the VALUES list look like a subquery targetlist.
1578 foreach(lc, values_list)
1580 tlist = lappend(tlist,
1581 makeTargetEntry((Expr *) lfirst(lc),
1587 rvcontext.root = root;
1588 rvcontext.targetlist = tlist;
1589 rvcontext.target_rte = rte;
1590 rvcontext.relids = NULL;
1591 rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
1592 rvcontext.varno = varno;
1593 rvcontext.need_phvs = false;
1594 rvcontext.wrap_non_vars = false;
1595 /* initialize cache array with indexes 0 .. length(tlist) */
1596 rvcontext.rv_cache = palloc0((list_length(tlist) + 1) *
1600 * Replace all of the top query's references to the RTE's outputs with
1601 * copies of the adjusted VALUES expressions, being careful not to replace
1602 * any of the jointree structure. (This'd be a lot cleaner if we could use
1603 * query_tree_mutator.) Much of this should be no-ops in the dummy Query
1604 * that surrounds a VALUES RTE, but it's not enough code to be worth
1607 parse->targetList = (List *)
1608 pullup_replace_vars((Node *) parse->targetList, &rvcontext);
1609 parse->returningList = (List *)
1610 pullup_replace_vars((Node *) parse->returningList, &rvcontext);
1611 if (parse->onConflict)
1612 parse->onConflict->onConflictSet = (List *)
1613 pullup_replace_vars((Node *) parse->onConflict->onConflictSet, &rvcontext);
1614 replace_vars_in_jointree((Node *) parse->jointree, &rvcontext, NULL);
1615 Assert(parse->setOperations == NULL);
1616 parse->havingQual = pullup_replace_vars(parse->havingQual, &rvcontext);
1619 * There should be no appendrels to fix, nor any join alias Vars, nor any
1620 * outer joins and hence no PlaceHolderVars.
1622 Assert(root->append_rel_list == NIL);
1623 Assert(list_length(parse->rtable) == 1);
1624 Assert(root->join_info_list == NIL);
1625 Assert(root->lateral_info_list == NIL);
1626 Assert(root->placeholder_list == NIL);
1629 * Return NULL to signal deletion of the VALUES RTE from the parent
1630 * jointree (and set hasDeletedRTEs to ensure cleanup later).
1632 root->hasDeletedRTEs = true;
1638 * Check a VALUES RTE in the range table to see if it's simple enough
1639 * to pull up into the parent query.
1641 * rte is the RTE_VALUES RangeTblEntry to check.
1642 * deletion_ok is TRUE if it'd be okay to delete the VALUES RTE entirely.
1645 is_simple_values(PlannerInfo *root, RangeTblEntry *rte, bool deletion_ok)
1647 Assert(rte->rtekind == RTE_VALUES);
1650 * We can only pull up a VALUES RTE if deletion_ok is TRUE. It's
1651 * basically the same case as a sub-select with empty FROM list; see
1652 * comments in is_simple_subquery().
1658 * Also, there must be exactly one VALUES list, else it's not semantically
1659 * correct to delete the VALUES RTE.
1661 if (list_length(rte->values_lists) != 1)
1665 * Because VALUES can't appear under an outer join (or at least, we won't
1666 * try to pull it up if it does), we need not worry about LATERAL.
1670 * Don't pull up a VALUES that contains any set-returning or volatile
1671 * functions. Again, the considerations here are basically identical to
1672 * restrictions on a subquery's targetlist.
1674 if (expression_returns_set((Node *) rte->values_lists) ||
1675 contain_volatile_functions((Node *) rte->values_lists))
1679 * Do not pull up a VALUES that's not the only RTE in its parent query.
1680 * This is actually the only case that the parser will generate at the
1681 * moment, and assuming this is true greatly simplifies
1682 * pull_up_simple_values().
1684 if (list_length(root->parse->rtable) != 1 ||
1685 rte != (RangeTblEntry *) linitial(root->parse->rtable))
1692 * is_simple_union_all
1693 * Check a subquery to see if it's a simple UNION ALL.
1695 * We require all the setops to be UNION ALL (no mixing) and there can't be
1696 * any datatype coercions involved, ie, all the leaf queries must emit the
1700 is_simple_union_all(Query *subquery)
1702 SetOperationStmt *topop;
1704 /* Let's just make sure it's a valid subselect ... */
1705 if (!IsA(subquery, Query) ||
1706 subquery->commandType != CMD_SELECT ||
1707 subquery->utilityStmt != NULL)
1708 elog(ERROR, "subquery is bogus");
1710 /* Is it a set-operation query at all? */
1711 topop = (SetOperationStmt *) subquery->setOperations;
1714 Assert(IsA(topop, SetOperationStmt));
1716 /* Can't handle ORDER BY, LIMIT/OFFSET, locking, or WITH */
1717 if (subquery->sortClause ||
1718 subquery->limitOffset ||
1719 subquery->limitCount ||
1720 subquery->rowMarks ||
1724 /* Recursively check the tree of set operations */
1725 return is_simple_union_all_recurse((Node *) topop, subquery,
1730 is_simple_union_all_recurse(Node *setOp, Query *setOpQuery, List *colTypes)
1732 if (IsA(setOp, RangeTblRef))
1734 RangeTblRef *rtr = (RangeTblRef *) setOp;
1735 RangeTblEntry *rte = rt_fetch(rtr->rtindex, setOpQuery->rtable);
1736 Query *subquery = rte->subquery;
1738 Assert(subquery != NULL);
1740 /* Leaf nodes are OK if they match the toplevel column types */
1741 /* We don't have to compare typmods or collations here */
1742 return tlist_same_datatypes(subquery->targetList, colTypes, true);
1744 else if (IsA(setOp, SetOperationStmt))
1746 SetOperationStmt *op = (SetOperationStmt *) setOp;
1748 /* Must be UNION ALL */
1749 if (op->op != SETOP_UNION || !op->all)
1752 /* Recurse to check inputs */
1753 return is_simple_union_all_recurse(op->larg, setOpQuery, colTypes) &&
1754 is_simple_union_all_recurse(op->rarg, setOpQuery, colTypes);
1758 elog(ERROR, "unrecognized node type: %d",
1759 (int) nodeTag(setOp));
1760 return false; /* keep compiler quiet */
1765 * is_safe_append_member
1766 * Check a subquery that is a leaf of a UNION ALL appendrel to see if it's
1770 is_safe_append_member(Query *subquery)
1775 * It's only safe to pull up the child if its jointree contains exactly
1776 * one RTE, else the AppendRelInfo data structure breaks. The one base RTE
1777 * could be buried in several levels of FromExpr, however.
1779 * Also, the child can't have any WHERE quals because there's no place to
1780 * put them in an appendrel. (This is a bit annoying...) If we didn't
1781 * need to check this, we'd just test whether get_relids_in_jointree()
1782 * yields a singleton set, to be more consistent with the coding of
1783 * fix_append_rel_relids().
1785 jtnode = subquery->jointree;
1786 while (IsA(jtnode, FromExpr))
1788 if (jtnode->quals != NULL)
1790 if (list_length(jtnode->fromlist) != 1)
1792 jtnode = linitial(jtnode->fromlist);
1794 if (!IsA(jtnode, RangeTblRef))
1801 * jointree_contains_lateral_outer_refs
1802 * Check for disallowed lateral references in a jointree's quals
1804 * If restricted is false, all level-1 Vars are allowed (but we still must
1805 * search the jointree, since it might contain outer joins below which there
1806 * will be restrictions). If restricted is true, return TRUE when any qual
1807 * in the jointree contains level-1 Vars coming from outside the rels listed
1808 * in safe_upper_varnos.
1811 jointree_contains_lateral_outer_refs(Node *jtnode, bool restricted,
1812 Relids safe_upper_varnos)
1816 if (IsA(jtnode, RangeTblRef))
1818 else if (IsA(jtnode, FromExpr))
1820 FromExpr *f = (FromExpr *) jtnode;
1823 /* First, recurse to check child joins */
1824 foreach(l, f->fromlist)
1826 if (jointree_contains_lateral_outer_refs(lfirst(l),
1832 /* Then check the top-level quals */
1834 !bms_is_subset(pull_varnos_of_level(f->quals, 1),
1838 else if (IsA(jtnode, JoinExpr))
1840 JoinExpr *j = (JoinExpr *) jtnode;
1843 * If this is an outer join, we mustn't allow any upper lateral
1844 * references in or below it.
1846 if (j->jointype != JOIN_INNER)
1849 safe_upper_varnos = NULL;
1852 /* Check the child joins */
1853 if (jointree_contains_lateral_outer_refs(j->larg,
1857 if (jointree_contains_lateral_outer_refs(j->rarg,
1862 /* Check the JOIN's qual clauses */
1864 !bms_is_subset(pull_varnos_of_level(j->quals, 1),
1869 elog(ERROR, "unrecognized node type: %d",
1870 (int) nodeTag(jtnode));
1875 * Helper routine for pull_up_subqueries: do pullup_replace_vars on every
1876 * expression in the jointree, without changing the jointree structure itself.
1877 * Ugly, but there's no other way...
1879 * If we are at or below lowest_nulling_outer_join, we can suppress use of
1880 * PlaceHolderVars wrapped around the replacement expressions.
1883 replace_vars_in_jointree(Node *jtnode,
1884 pullup_replace_vars_context *context,
1885 JoinExpr *lowest_nulling_outer_join)
1889 if (IsA(jtnode, RangeTblRef))
1892 * If the RangeTblRef refers to a LATERAL subquery (that isn't the
1893 * same subquery we're pulling up), it might contain references to the
1894 * target subquery, which we must replace. We drive this from the
1895 * jointree scan, rather than a scan of the rtable, for a couple of
1896 * reasons: we can avoid processing no-longer-referenced RTEs, and we
1897 * can use the appropriate setting of need_phvs depending on whether
1898 * the RTE is above possibly-nulling outer joins or not.
1900 int varno = ((RangeTblRef *) jtnode)->rtindex;
1902 if (varno != context->varno) /* ignore target subquery itself */
1904 RangeTblEntry *rte = rt_fetch(varno, context->root->parse->rtable);
1906 Assert(rte != context->target_rte);
1909 switch (rte->rtekind)
1913 pullup_replace_vars_subquery(rte->subquery,
1917 rte->functions = (List *)
1918 pullup_replace_vars((Node *) rte->functions,
1922 rte->values_lists = (List *)
1923 pullup_replace_vars((Node *) rte->values_lists,
1929 /* these shouldn't be marked LATERAL */
1936 else if (IsA(jtnode, FromExpr))
1938 FromExpr *f = (FromExpr *) jtnode;
1941 foreach(l, f->fromlist)
1942 replace_vars_in_jointree(lfirst(l), context,
1943 lowest_nulling_outer_join);
1944 f->quals = pullup_replace_vars(f->quals, context);
1946 else if (IsA(jtnode, JoinExpr))
1948 JoinExpr *j = (JoinExpr *) jtnode;
1949 bool save_need_phvs = context->need_phvs;
1951 if (j == lowest_nulling_outer_join)
1953 /* no more PHVs in or below this join */
1954 context->need_phvs = false;
1955 lowest_nulling_outer_join = NULL;
1957 replace_vars_in_jointree(j->larg, context, lowest_nulling_outer_join);
1958 replace_vars_in_jointree(j->rarg, context, lowest_nulling_outer_join);
1959 j->quals = pullup_replace_vars(j->quals, context);
1962 * We don't bother to update the colvars list, since it won't be used
1965 context->need_phvs = save_need_phvs;
1968 elog(ERROR, "unrecognized node type: %d",
1969 (int) nodeTag(jtnode));
1973 * Apply pullup variable replacement throughout an expression tree
1975 * Returns a modified copy of the tree, so this can't be used where we
1976 * need to do in-place replacement.
1979 pullup_replace_vars(Node *expr, pullup_replace_vars_context *context)
1981 return replace_rte_variables(expr,
1983 pullup_replace_vars_callback,
1985 context->outer_hasSubLinks);
1989 pullup_replace_vars_callback(Var *var,
1990 replace_rte_variables_context *context)
1992 pullup_replace_vars_context *rcon = (pullup_replace_vars_context *) context->callback_arg;
1993 int varattno = var->varattno;
1997 * If PlaceHolderVars are needed, we cache the modified expressions in
1998 * rcon->rv_cache[]. This is not in hopes of any material speed gain
1999 * within this function, but to avoid generating identical PHVs with
2000 * different IDs. That would result in duplicate evaluations at runtime,
2001 * and possibly prevent optimizations that rely on recognizing different
2002 * references to the same subquery output as being equal(). So it's worth
2003 * a bit of extra effort to avoid it.
2005 if (rcon->need_phvs &&
2006 varattno >= InvalidAttrNumber &&
2007 varattno <= list_length(rcon->targetlist) &&
2008 rcon->rv_cache[varattno] != NULL)
2010 /* Just copy the entry and fall through to adjust its varlevelsup */
2011 newnode = copyObject(rcon->rv_cache[varattno]);
2013 else if (varattno == InvalidAttrNumber)
2015 /* Must expand whole-tuple reference into RowExpr */
2019 bool save_need_phvs = rcon->need_phvs;
2020 int save_sublevelsup = context->sublevels_up;
2023 * If generating an expansion for a var of a named rowtype (ie, this
2024 * is a plain relation RTE), then we must include dummy items for
2025 * dropped columns. If the var is RECORD (ie, this is a JOIN), then
2026 * omit dropped columns. Either way, attach column names to the
2027 * RowExpr for use of ruleutils.c.
2029 * In order to be able to cache the results, we always generate the
2030 * expansion with varlevelsup = 0, and then adjust if needed.
2032 expandRTE(rcon->target_rte,
2033 var->varno, 0 /* not varlevelsup */ , var->location,
2034 (var->vartype != RECORDOID),
2035 &colnames, &fields);
2036 /* Adjust the generated per-field Vars, but don't insert PHVs */
2037 rcon->need_phvs = false;
2038 context->sublevels_up = 0; /* to match the expandRTE output */
2039 fields = (List *) replace_rte_variables_mutator((Node *) fields,
2041 rcon->need_phvs = save_need_phvs;
2042 context->sublevels_up = save_sublevelsup;
2044 rowexpr = makeNode(RowExpr);
2045 rowexpr->args = fields;
2046 rowexpr->row_typeid = var->vartype;
2047 rowexpr->row_format = COERCE_IMPLICIT_CAST;
2048 rowexpr->colnames = colnames;
2049 rowexpr->location = var->location;
2050 newnode = (Node *) rowexpr;
2053 * Insert PlaceHolderVar if needed. Notice that we are wrapping one
2054 * PlaceHolderVar around the whole RowExpr, rather than putting one
2055 * around each element of the row. This is because we need the
2056 * expression to yield NULL, not ROW(NULL,NULL,...) when it is forced
2057 * to null by an outer join.
2059 if (rcon->need_phvs)
2061 /* RowExpr is certainly not strict, so always need PHV */
2063 make_placeholder_expr(rcon->root,
2065 bms_make_singleton(rcon->varno));
2066 /* cache it with the PHV, and with varlevelsup still zero */
2067 rcon->rv_cache[InvalidAttrNumber] = copyObject(newnode);
2072 /* Normal case referencing one targetlist element */
2073 TargetEntry *tle = get_tle_by_resno(rcon->targetlist, varattno);
2075 if (tle == NULL) /* shouldn't happen */
2076 elog(ERROR, "could not find attribute %d in subquery targetlist",
2079 /* Make a copy of the tlist item to return */
2080 newnode = copyObject(tle->expr);
2082 /* Insert PlaceHolderVar if needed */
2083 if (rcon->need_phvs)
2087 if (newnode && IsA(newnode, Var) &&
2088 ((Var *) newnode)->varlevelsup == 0)
2091 * Simple Vars always escape being wrapped, unless they are
2092 * lateral references to something outside the subquery being
2093 * pulled up. (Even then, we could omit the PlaceHolderVar if
2094 * the referenced rel is under the same lowest outer join, but
2095 * it doesn't seem worth the trouble to check that.)
2097 if (rcon->target_rte->lateral &&
2098 !bms_is_member(((Var *) newnode)->varno, rcon->relids))
2103 else if (newnode && IsA(newnode, PlaceHolderVar) &&
2104 ((PlaceHolderVar *) newnode)->phlevelsup == 0)
2106 /* No need to wrap a PlaceHolderVar with another one, either */
2109 else if (rcon->wrap_non_vars)
2111 /* Wrap all non-Vars in a PlaceHolderVar */
2117 * If it contains a Var of the subquery being pulled up, and
2118 * does not contain any non-strict constructs, then it's
2119 * certainly nullable so we don't need to insert a
2122 * This analysis could be tighter: in particular, a non-strict
2123 * construct hidden within a lower-level PlaceHolderVar is not
2124 * reason to add another PHV. But for now it doesn't seem
2125 * worth the code to be more exact.
2127 * Note: in future maybe we should insert a PlaceHolderVar
2128 * anyway, if the tlist item is expensive to evaluate?
2130 * For a LATERAL subquery, we have to check the actual var
2131 * membership of the node, but if it's non-lateral then any
2132 * level-zero var must belong to the subquery.
2134 if ((rcon->target_rte->lateral ?
2135 bms_overlap(pull_varnos((Node *) newnode), rcon->relids) :
2136 contain_vars_of_level((Node *) newnode, 0)) &&
2137 !contain_nonstrict_functions((Node *) newnode))
2139 /* No wrap needed */
2144 /* Else wrap it in a PlaceHolderVar */
2151 make_placeholder_expr(rcon->root,
2153 bms_make_singleton(rcon->varno));
2156 * Cache it if possible (ie, if the attno is in range, which it
2157 * probably always should be). We can cache the value even if we
2158 * decided we didn't need a PHV, since this result will be
2159 * suitable for any request that has need_phvs.
2161 if (varattno > InvalidAttrNumber &&
2162 varattno <= list_length(rcon->targetlist))
2163 rcon->rv_cache[varattno] = copyObject(newnode);
2167 /* Must adjust varlevelsup if tlist item is from higher query */
2168 if (var->varlevelsup > 0)
2169 IncrementVarSublevelsUp(newnode, var->varlevelsup, 0);
2175 * Apply pullup variable replacement to a subquery
2177 * This needs to be different from pullup_replace_vars() because
2178 * replace_rte_variables will think that it shouldn't increment sublevels_up
2179 * before entering the Query; so we need to call it with sublevels_up == 1.
2182 pullup_replace_vars_subquery(Query *query,
2183 pullup_replace_vars_context *context)
2185 Assert(IsA(query, Query));
2186 return (Query *) replace_rte_variables((Node *) query,
2188 pullup_replace_vars_callback,
2194 * pull_up_subqueries_cleanup
2195 * Recursively fix up jointree after deletion of some subqueries.
2197 * The jointree now contains some NULL subtrees, which we need to get rid of.
2198 * In a FromExpr, just rebuild the child-node list with null entries deleted.
2199 * In an inner JOIN, replace the JoinExpr node with a one-child FromExpr.
2202 pull_up_subqueries_cleanup(Node *jtnode)
2204 Assert(jtnode != NULL);
2205 if (IsA(jtnode, RangeTblRef))
2207 /* Nothing to do at leaf nodes. */
2209 else if (IsA(jtnode, FromExpr))
2211 FromExpr *f = (FromExpr *) jtnode;
2212 List *newfrom = NIL;
2215 foreach(l, f->fromlist)
2217 Node *child = (Node *) lfirst(l);
2221 child = pull_up_subqueries_cleanup(child);
2222 newfrom = lappend(newfrom, child);
2224 f->fromlist = newfrom;
2226 else if (IsA(jtnode, JoinExpr))
2228 JoinExpr *j = (JoinExpr *) jtnode;
2231 j->larg = pull_up_subqueries_cleanup(j->larg);
2233 j->rarg = pull_up_subqueries_cleanup(j->rarg);
2234 if (j->larg == NULL)
2236 Assert(j->jointype == JOIN_INNER);
2237 Assert(j->rarg != NULL);
2238 return (Node *) makeFromExpr(list_make1(j->rarg), j->quals);
2240 else if (j->rarg == NULL)
2242 Assert(j->jointype == JOIN_INNER);
2243 return (Node *) makeFromExpr(list_make1(j->larg), j->quals);
2247 elog(ERROR, "unrecognized node type: %d",
2248 (int) nodeTag(jtnode));
2254 * flatten_simple_union_all
2255 * Try to optimize top-level UNION ALL structure into an appendrel
2257 * If a query's setOperations tree consists entirely of simple UNION ALL
2258 * operations, flatten it into an append relation, which we can process more
2259 * intelligently than the general setops case. Otherwise, do nothing.
2261 * In most cases, this can succeed only for a top-level query, because for a
2262 * subquery in FROM, the parent query's invocation of pull_up_subqueries would
2263 * already have flattened the UNION via pull_up_simple_union_all. But there
2264 * are a few cases we can support here but not in that code path, for example
2265 * when the subquery also contains ORDER BY.
2268 flatten_simple_union_all(PlannerInfo *root)
2270 Query *parse = root->parse;
2271 SetOperationStmt *topop;
2272 Node *leftmostjtnode;
2274 RangeTblEntry *leftmostRTE;
2276 RangeTblEntry *childRTE;
2279 /* Shouldn't be called unless query has setops */
2280 topop = (SetOperationStmt *) parse->setOperations;
2281 Assert(topop && IsA(topop, SetOperationStmt));
2283 /* Can't optimize away a recursive UNION */
2284 if (root->hasRecursion)
2288 * Recursively check the tree of set operations. If not all UNION ALL
2289 * with identical column types, punt.
2291 if (!is_simple_union_all_recurse((Node *) topop, parse, topop->colTypes))
2295 * Locate the leftmost leaf query in the setops tree. The upper query's
2296 * Vars all refer to this RTE (see transformSetOperationStmt).
2298 leftmostjtnode = topop->larg;
2299 while (leftmostjtnode && IsA(leftmostjtnode, SetOperationStmt))
2300 leftmostjtnode = ((SetOperationStmt *) leftmostjtnode)->larg;
2301 Assert(leftmostjtnode && IsA(leftmostjtnode, RangeTblRef));
2302 leftmostRTI = ((RangeTblRef *) leftmostjtnode)->rtindex;
2303 leftmostRTE = rt_fetch(leftmostRTI, parse->rtable);
2304 Assert(leftmostRTE->rtekind == RTE_SUBQUERY);
2307 * Make a copy of the leftmost RTE and add it to the rtable. This copy
2308 * will represent the leftmost leaf query in its capacity as a member of
2309 * the appendrel. The original will represent the appendrel as a whole.
2310 * (We must do things this way because the upper query's Vars have to be
2311 * seen as referring to the whole appendrel.)
2313 childRTE = copyObject(leftmostRTE);
2314 parse->rtable = lappend(parse->rtable, childRTE);
2315 childRTI = list_length(parse->rtable);
2317 /* Modify the setops tree to reference the child copy */
2318 ((RangeTblRef *) leftmostjtnode)->rtindex = childRTI;
2320 /* Modify the formerly-leftmost RTE to mark it as an appendrel parent */
2321 leftmostRTE->inh = true;
2324 * Form a RangeTblRef for the appendrel, and insert it into FROM. The top
2325 * Query of a setops tree should have had an empty FromClause initially.
2327 rtr = makeNode(RangeTblRef);
2328 rtr->rtindex = leftmostRTI;
2329 Assert(parse->jointree->fromlist == NIL);
2330 parse->jointree->fromlist = list_make1(rtr);
2333 * Now pretend the query has no setops. We must do this before trying to
2334 * do subquery pullup, because of Assert in pull_up_simple_subquery.
2336 parse->setOperations = NULL;
2339 * Build AppendRelInfo information, and apply pull_up_subqueries to the
2340 * leaf queries of the UNION ALL. (We must do that now because they
2341 * weren't previously referenced by the jointree, and so were missed by
2342 * the main invocation of pull_up_subqueries.)
2344 pull_up_union_leaf_queries((Node *) topop, root, leftmostRTI, parse, 0);
2349 * reduce_outer_joins
2350 * Attempt to reduce outer joins to plain inner joins.
2352 * The idea here is that given a query like
2353 * SELECT ... FROM a LEFT JOIN b ON (...) WHERE b.y = 42;
2354 * we can reduce the LEFT JOIN to a plain JOIN if the "=" operator in WHERE
2355 * is strict. The strict operator will always return NULL, causing the outer
2356 * WHERE to fail, on any row where the LEFT JOIN filled in NULLs for b's
2357 * columns. Therefore, there's no need for the join to produce null-extended
2358 * rows in the first place --- which makes it a plain join not an outer join.
2359 * (This scenario may not be very likely in a query written out by hand, but
2360 * it's reasonably likely when pushing quals down into complex views.)
2362 * More generally, an outer join can be reduced in strength if there is a
2363 * strict qual above it in the qual tree that constrains a Var from the
2364 * nullable side of the join to be non-null. (For FULL joins this applies
2365 * to each side separately.)
2367 * Another transformation we apply here is to recognize cases like
2368 * SELECT ... FROM a LEFT JOIN b ON (a.x = b.y) WHERE b.y IS NULL;
2369 * If the join clause is strict for b.y, then only null-extended rows could
2370 * pass the upper WHERE, and we can conclude that what the query is really
2371 * specifying is an anti-semijoin. We change the join type from JOIN_LEFT
2372 * to JOIN_ANTI. The IS NULL clause then becomes redundant, and must be
2373 * removed to prevent bogus selectivity calculations, but we leave it to
2374 * distribute_qual_to_rels to get rid of such clauses.
2376 * Also, we get rid of JOIN_RIGHT cases by flipping them around to become
2377 * JOIN_LEFT. This saves some code here and in some later planner routines,
2378 * but the main reason to do it is to not need to invent a JOIN_REVERSE_ANTI
2381 * To ease recognition of strict qual clauses, we require this routine to be
2382 * run after expression preprocessing (i.e., qual canonicalization and JOIN
2383 * alias-var expansion).
2386 reduce_outer_joins(PlannerInfo *root)
2388 reduce_outer_joins_state *state;
2391 * To avoid doing strictness checks on more quals than necessary, we want
2392 * to stop descending the jointree as soon as there are no outer joins
2393 * below our current point. This consideration forces a two-pass process.
2394 * The first pass gathers information about which base rels appear below
2395 * each side of each join clause, and about whether there are outer
2396 * join(s) below each side of each join clause. The second pass examines
2397 * qual clauses and changes join types as it descends the tree.
2399 state = reduce_outer_joins_pass1((Node *) root->parse->jointree);
2401 /* planner.c shouldn't have called me if no outer joins */
2402 if (state == NULL || !state->contains_outer)
2403 elog(ERROR, "so where are the outer joins?");
2405 reduce_outer_joins_pass2((Node *) root->parse->jointree,
2406 state, root, NULL, NIL, NIL);
2410 * reduce_outer_joins_pass1 - phase 1 data collection
2412 * Returns a state node describing the given jointree node.
2414 static reduce_outer_joins_state *
2415 reduce_outer_joins_pass1(Node *jtnode)
2417 reduce_outer_joins_state *result;
2419 result = (reduce_outer_joins_state *)
2420 palloc(sizeof(reduce_outer_joins_state));
2421 result->relids = NULL;
2422 result->contains_outer = false;
2423 result->sub_states = NIL;
2427 if (IsA(jtnode, RangeTblRef))
2429 int varno = ((RangeTblRef *) jtnode)->rtindex;
2431 result->relids = bms_make_singleton(varno);
2433 else if (IsA(jtnode, FromExpr))
2435 FromExpr *f = (FromExpr *) jtnode;
2438 foreach(l, f->fromlist)
2440 reduce_outer_joins_state *sub_state;
2442 sub_state = reduce_outer_joins_pass1(lfirst(l));
2443 result->relids = bms_add_members(result->relids,
2445 result->contains_outer |= sub_state->contains_outer;
2446 result->sub_states = lappend(result->sub_states, sub_state);
2449 else if (IsA(jtnode, JoinExpr))
2451 JoinExpr *j = (JoinExpr *) jtnode;
2452 reduce_outer_joins_state *sub_state;
2454 /* join's own RT index is not wanted in result->relids */
2455 if (IS_OUTER_JOIN(j->jointype))
2456 result->contains_outer = true;
2458 sub_state = reduce_outer_joins_pass1(j->larg);
2459 result->relids = bms_add_members(result->relids,
2461 result->contains_outer |= sub_state->contains_outer;
2462 result->sub_states = lappend(result->sub_states, sub_state);
2464 sub_state = reduce_outer_joins_pass1(j->rarg);
2465 result->relids = bms_add_members(result->relids,
2467 result->contains_outer |= sub_state->contains_outer;
2468 result->sub_states = lappend(result->sub_states, sub_state);
2471 elog(ERROR, "unrecognized node type: %d",
2472 (int) nodeTag(jtnode));
2477 * reduce_outer_joins_pass2 - phase 2 processing
2479 * jtnode: current jointree node
2480 * state: state data collected by phase 1 for this node
2481 * root: toplevel planner state
2482 * nonnullable_rels: set of base relids forced non-null by upper quals
2483 * nonnullable_vars: list of Vars forced non-null by upper quals
2484 * forced_null_vars: list of Vars forced null by upper quals
2487 reduce_outer_joins_pass2(Node *jtnode,
2488 reduce_outer_joins_state *state,
2490 Relids nonnullable_rels,
2491 List *nonnullable_vars,
2492 List *forced_null_vars)
2495 * pass 2 should never descend as far as an empty subnode or base rel,
2496 * because it's only called on subtrees marked as contains_outer.
2499 elog(ERROR, "reached empty jointree");
2500 if (IsA(jtnode, RangeTblRef))
2501 elog(ERROR, "reached base rel");
2502 else if (IsA(jtnode, FromExpr))
2504 FromExpr *f = (FromExpr *) jtnode;
2507 Relids pass_nonnullable_rels;
2508 List *pass_nonnullable_vars;
2509 List *pass_forced_null_vars;
2511 /* Scan quals to see if we can add any constraints */
2512 pass_nonnullable_rels = find_nonnullable_rels(f->quals);
2513 pass_nonnullable_rels = bms_add_members(pass_nonnullable_rels,
2515 /* NB: we rely on list_concat to not damage its second argument */
2516 pass_nonnullable_vars = find_nonnullable_vars(f->quals);
2517 pass_nonnullable_vars = list_concat(pass_nonnullable_vars,
2519 pass_forced_null_vars = find_forced_null_vars(f->quals);
2520 pass_forced_null_vars = list_concat(pass_forced_null_vars,
2522 /* And recurse --- but only into interesting subtrees */
2523 Assert(list_length(f->fromlist) == list_length(state->sub_states));
2524 forboth(l, f->fromlist, s, state->sub_states)
2526 reduce_outer_joins_state *sub_state = lfirst(s);
2528 if (sub_state->contains_outer)
2529 reduce_outer_joins_pass2(lfirst(l), sub_state, root,
2530 pass_nonnullable_rels,
2531 pass_nonnullable_vars,
2532 pass_forced_null_vars);
2534 bms_free(pass_nonnullable_rels);
2535 /* can't so easily clean up var lists, unfortunately */
2537 else if (IsA(jtnode, JoinExpr))
2539 JoinExpr *j = (JoinExpr *) jtnode;
2540 int rtindex = j->rtindex;
2541 JoinType jointype = j->jointype;
2542 reduce_outer_joins_state *left_state = linitial(state->sub_states);
2543 reduce_outer_joins_state *right_state = lsecond(state->sub_states);
2544 List *local_nonnullable_vars = NIL;
2545 bool computed_local_nonnullable_vars = false;
2547 /* Can we simplify this join? */
2553 if (bms_overlap(nonnullable_rels, right_state->relids))
2554 jointype = JOIN_INNER;
2557 if (bms_overlap(nonnullable_rels, left_state->relids))
2558 jointype = JOIN_INNER;
2561 if (bms_overlap(nonnullable_rels, left_state->relids))
2563 if (bms_overlap(nonnullable_rels, right_state->relids))
2564 jointype = JOIN_INNER;
2566 jointype = JOIN_LEFT;
2570 if (bms_overlap(nonnullable_rels, right_state->relids))
2571 jointype = JOIN_RIGHT;
2578 * These could only have been introduced by pull_up_sublinks,
2579 * so there's no way that upper quals could refer to their
2580 * righthand sides, and no point in checking.
2584 elog(ERROR, "unrecognized join type: %d",
2590 * Convert JOIN_RIGHT to JOIN_LEFT. Note that in the case where we
2591 * reduced JOIN_FULL to JOIN_RIGHT, this will mean the JoinExpr no
2592 * longer matches the internal ordering of any CoalesceExpr's built to
2593 * represent merged join variables. We don't care about that at
2594 * present, but be wary of it ...
2596 if (jointype == JOIN_RIGHT)
2603 jointype = JOIN_LEFT;
2604 right_state = linitial(state->sub_states);
2605 left_state = lsecond(state->sub_states);
2609 * See if we can reduce JOIN_LEFT to JOIN_ANTI. This is the case if
2610 * the join's own quals are strict for any var that was forced null by
2611 * higher qual levels. NOTE: there are other ways that we could
2612 * detect an anti-join, in particular if we were to check whether Vars
2613 * coming from the RHS must be non-null because of table constraints.
2614 * That seems complicated and expensive though (in particular, one
2615 * would have to be wary of lower outer joins). For the moment this
2618 if (jointype == JOIN_LEFT)
2622 local_nonnullable_vars = find_nonnullable_vars(j->quals);
2623 computed_local_nonnullable_vars = true;
2626 * It's not sufficient to check whether local_nonnullable_vars and
2627 * forced_null_vars overlap: we need to know if the overlap
2628 * includes any RHS variables.
2630 overlap = list_intersection(local_nonnullable_vars,
2632 if (overlap != NIL &&
2633 bms_overlap(pull_varnos((Node *) overlap),
2634 right_state->relids))
2635 jointype = JOIN_ANTI;
2638 /* Apply the jointype change, if any, to both jointree node and RTE */
2639 if (rtindex && jointype != j->jointype)
2641 RangeTblEntry *rte = rt_fetch(rtindex, root->parse->rtable);
2643 Assert(rte->rtekind == RTE_JOIN);
2644 Assert(rte->jointype == j->jointype);
2645 rte->jointype = jointype;
2647 j->jointype = jointype;
2649 /* Only recurse if there's more to do below here */
2650 if (left_state->contains_outer || right_state->contains_outer)
2652 Relids local_nonnullable_rels;
2653 List *local_forced_null_vars;
2654 Relids pass_nonnullable_rels;
2655 List *pass_nonnullable_vars;
2656 List *pass_forced_null_vars;
2659 * If this join is (now) inner, we can add any constraints its
2660 * quals provide to those we got from above. But if it is outer,
2661 * we can pass down the local constraints only into the nullable
2662 * side, because an outer join never eliminates any rows from its
2663 * non-nullable side. Also, there is no point in passing upper
2664 * constraints into the nullable side, since if there were any
2665 * we'd have been able to reduce the join. (In the case of upper
2666 * forced-null constraints, we *must not* pass them into the
2667 * nullable side --- they either applied here, or not.) The upshot
2668 * is that we pass either the local or the upper constraints,
2669 * never both, to the children of an outer join.
2671 * Note that a SEMI join works like an inner join here: it's okay
2672 * to pass down both local and upper constraints. (There can't be
2673 * any upper constraints affecting its inner side, but it's not
2674 * worth having a separate code path to avoid passing them.)
2676 * At a FULL join we just punt and pass nothing down --- is it
2677 * possible to be smarter?
2679 if (jointype != JOIN_FULL)
2681 local_nonnullable_rels = find_nonnullable_rels(j->quals);
2682 if (!computed_local_nonnullable_vars)
2683 local_nonnullable_vars = find_nonnullable_vars(j->quals);
2684 local_forced_null_vars = find_forced_null_vars(j->quals);
2685 if (jointype == JOIN_INNER || jointype == JOIN_SEMI)
2687 /* OK to merge upper and local constraints */
2688 local_nonnullable_rels = bms_add_members(local_nonnullable_rels,
2690 local_nonnullable_vars = list_concat(local_nonnullable_vars,
2692 local_forced_null_vars = list_concat(local_forced_null_vars,
2698 /* no use in calculating these */
2699 local_nonnullable_rels = NULL;
2700 local_forced_null_vars = NIL;
2703 if (left_state->contains_outer)
2705 if (jointype == JOIN_INNER || jointype == JOIN_SEMI)
2707 /* pass union of local and upper constraints */
2708 pass_nonnullable_rels = local_nonnullable_rels;
2709 pass_nonnullable_vars = local_nonnullable_vars;
2710 pass_forced_null_vars = local_forced_null_vars;
2712 else if (jointype != JOIN_FULL) /* ie, LEFT or ANTI */
2714 /* can't pass local constraints to non-nullable side */
2715 pass_nonnullable_rels = nonnullable_rels;
2716 pass_nonnullable_vars = nonnullable_vars;
2717 pass_forced_null_vars = forced_null_vars;
2721 /* no constraints pass through JOIN_FULL */
2722 pass_nonnullable_rels = NULL;
2723 pass_nonnullable_vars = NIL;
2724 pass_forced_null_vars = NIL;
2726 reduce_outer_joins_pass2(j->larg, left_state, root,
2727 pass_nonnullable_rels,
2728 pass_nonnullable_vars,
2729 pass_forced_null_vars);
2732 if (right_state->contains_outer)
2734 if (jointype != JOIN_FULL) /* ie, INNER/LEFT/SEMI/ANTI */
2736 /* pass appropriate constraints, per comment above */
2737 pass_nonnullable_rels = local_nonnullable_rels;
2738 pass_nonnullable_vars = local_nonnullable_vars;
2739 pass_forced_null_vars = local_forced_null_vars;
2743 /* no constraints pass through JOIN_FULL */
2744 pass_nonnullable_rels = NULL;
2745 pass_nonnullable_vars = NIL;
2746 pass_forced_null_vars = NIL;
2748 reduce_outer_joins_pass2(j->rarg, right_state, root,
2749 pass_nonnullable_rels,
2750 pass_nonnullable_vars,
2751 pass_forced_null_vars);
2753 bms_free(local_nonnullable_rels);
2757 elog(ERROR, "unrecognized node type: %d",
2758 (int) nodeTag(jtnode));
2762 * substitute_multiple_relids - adjust node relid sets after pulling up
2765 * Find any PlaceHolderVar nodes in the given tree that reference the
2766 * pulled-up relid, and change them to reference the replacement relid(s).
2768 * NOTE: although this has the form of a walker, we cheat and modify the
2769 * nodes in-place. This should be OK since the tree was copied by
2770 * pullup_replace_vars earlier. Avoid scribbling on the original values of
2771 * the bitmapsets, though, because expression_tree_mutator doesn't copy those.
2779 } substitute_multiple_relids_context;
2782 substitute_multiple_relids_walker(Node *node,
2783 substitute_multiple_relids_context *context)
2787 if (IsA(node, PlaceHolderVar))
2789 PlaceHolderVar *phv = (PlaceHolderVar *) node;
2791 if (phv->phlevelsup == context->sublevels_up &&
2792 bms_is_member(context->varno, phv->phrels))
2794 phv->phrels = bms_union(phv->phrels,
2795 context->subrelids);
2796 phv->phrels = bms_del_member(phv->phrels,
2799 /* fall through to examine children */
2801 if (IsA(node, Query))
2803 /* Recurse into subselects */
2806 context->sublevels_up++;
2807 result = query_tree_walker((Query *) node,
2808 substitute_multiple_relids_walker,
2809 (void *) context, 0);
2810 context->sublevels_up--;
2813 /* Shouldn't need to handle planner auxiliary nodes here */
2814 Assert(!IsA(node, SpecialJoinInfo));
2815 Assert(!IsA(node, LateralJoinInfo));
2816 Assert(!IsA(node, AppendRelInfo));
2817 Assert(!IsA(node, PlaceHolderInfo));
2818 Assert(!IsA(node, MinMaxAggInfo));
2820 return expression_tree_walker(node, substitute_multiple_relids_walker,
2825 substitute_multiple_relids(Node *node, int varno, Relids subrelids)
2827 substitute_multiple_relids_context context;
2829 context.varno = varno;
2830 context.sublevels_up = 0;
2831 context.subrelids = subrelids;
2834 * Must be prepared to start with a Query or a bare expression tree.
2836 query_or_expression_tree_walker(node,
2837 substitute_multiple_relids_walker,
2843 * fix_append_rel_relids: update RT-index fields of AppendRelInfo nodes
2845 * When we pull up a subquery, any AppendRelInfo references to the subquery's
2846 * RT index have to be replaced by the substituted relid (and there had better
2847 * be only one). We also need to apply substitute_multiple_relids to their
2848 * translated_vars lists, since those might contain PlaceHolderVars.
2850 * We assume we may modify the AppendRelInfo nodes in-place.
2853 fix_append_rel_relids(List *append_rel_list, int varno, Relids subrelids)
2859 * We only want to extract the member relid once, but we mustn't fail
2860 * immediately if there are multiple members; it could be that none of the
2861 * AppendRelInfo nodes refer to it. So compute it on first use. Note that
2862 * bms_singleton_member will complain if set is not singleton.
2864 foreach(l, append_rel_list)
2866 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
2868 /* The parent_relid shouldn't ever be a pullup target */
2869 Assert(appinfo->parent_relid != varno);
2871 if (appinfo->child_relid == varno)
2874 subvarno = bms_singleton_member(subrelids);
2875 appinfo->child_relid = subvarno;
2878 /* Also finish fixups for its translated vars */
2879 substitute_multiple_relids((Node *) appinfo->translated_vars,
2885 * get_relids_in_jointree: get set of RT indexes present in a jointree
2887 * If include_joins is true, join RT indexes are included; if false,
2888 * only base rels are included.
2891 get_relids_in_jointree(Node *jtnode, bool include_joins)
2893 Relids result = NULL;
2897 if (IsA(jtnode, RangeTblRef))
2899 int varno = ((RangeTblRef *) jtnode)->rtindex;
2901 result = bms_make_singleton(varno);
2903 else if (IsA(jtnode, FromExpr))
2905 FromExpr *f = (FromExpr *) jtnode;
2908 foreach(l, f->fromlist)
2910 result = bms_join(result,
2911 get_relids_in_jointree(lfirst(l),
2915 else if (IsA(jtnode, JoinExpr))
2917 JoinExpr *j = (JoinExpr *) jtnode;
2919 result = get_relids_in_jointree(j->larg, include_joins);
2920 result = bms_join(result,
2921 get_relids_in_jointree(j->rarg, include_joins));
2922 if (include_joins && j->rtindex)
2923 result = bms_add_member(result, j->rtindex);
2926 elog(ERROR, "unrecognized node type: %d",
2927 (int) nodeTag(jtnode));
2932 * get_relids_for_join: get set of base RT indexes making up a join
2935 get_relids_for_join(PlannerInfo *root, int joinrelid)
2939 jtnode = find_jointree_node_for_rel((Node *) root->parse->jointree,
2942 elog(ERROR, "could not find join node %d", joinrelid);
2943 return get_relids_in_jointree(jtnode, false);
2947 * find_jointree_node_for_rel: locate jointree node for a base or join RT index
2949 * Returns NULL if not found
2952 find_jointree_node_for_rel(Node *jtnode, int relid)
2956 if (IsA(jtnode, RangeTblRef))
2958 int varno = ((RangeTblRef *) jtnode)->rtindex;
2963 else if (IsA(jtnode, FromExpr))
2965 FromExpr *f = (FromExpr *) jtnode;
2968 foreach(l, f->fromlist)
2970 jtnode = find_jointree_node_for_rel(lfirst(l), relid);
2975 else if (IsA(jtnode, JoinExpr))
2977 JoinExpr *j = (JoinExpr *) jtnode;
2979 if (relid == j->rtindex)
2981 jtnode = find_jointree_node_for_rel(j->larg, relid);
2984 jtnode = find_jointree_node_for_rel(j->rarg, relid);
2989 elog(ERROR, "unrecognized node type: %d",
2990 (int) nodeTag(jtnode));