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 * do expression preprocessing (including flattening JOIN alias vars)
14 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
15 * Portions Copyright (c) 1994, Regents of the University of California
21 *-------------------------------------------------------------------------
25 #include "nodes/makefuncs.h"
26 #include "nodes/nodeFuncs.h"
27 #include "optimizer/clauses.h"
28 #include "optimizer/placeholder.h"
29 #include "optimizer/prep.h"
30 #include "optimizer/subselect.h"
31 #include "optimizer/tlist.h"
32 #include "optimizer/var.h"
33 #include "parser/parsetree.h"
34 #include "rewrite/rewriteManip.h"
37 typedef struct reduce_outer_joins_state
39 Relids relids
; /* base relids within this subtree */
40 bool contains_outer
; /* does subtree contain outer join(s)? */
41 List
*sub_states
; /* List of states for subtree components */
42 } reduce_outer_joins_state
;
44 static Node
*pull_up_sublinks_jointree_recurse(PlannerInfo
*root
, Node
*jtnode
,
46 static Node
*pull_up_sublinks_qual_recurse(PlannerInfo
*root
, Node
*node
,
47 Relids available_rels
, List
**fromlist
);
48 static Node
*pull_up_simple_subquery(PlannerInfo
*root
, Node
*jtnode
,
50 bool below_outer_join
,
51 bool append_rel_member
);
52 static Node
*pull_up_simple_union_all(PlannerInfo
*root
, Node
*jtnode
,
54 static void pull_up_union_leaf_queries(Node
*setOp
, PlannerInfo
*root
,
55 int parentRTindex
, Query
*setOpQuery
,
57 static void make_setop_translation_list(Query
*query
, Index newvarno
,
58 List
**translated_vars
);
59 static bool is_simple_subquery(Query
*subquery
);
60 static bool is_simple_union_all(Query
*subquery
);
61 static bool is_simple_union_all_recurse(Node
*setOp
, Query
*setOpQuery
,
63 static List
*insert_targetlist_placeholders(PlannerInfo
*root
, List
*tlist
,
64 int varno
, bool wrap_non_vars
);
65 static bool is_safe_append_member(Query
*subquery
);
66 static void resolvenew_in_jointree(Node
*jtnode
, int varno
,
67 RangeTblEntry
*rte
, List
*subtlist
);
68 static reduce_outer_joins_state
*reduce_outer_joins_pass1(Node
*jtnode
);
69 static void reduce_outer_joins_pass2(Node
*jtnode
,
70 reduce_outer_joins_state
*state
,
72 Relids nonnullable_rels
,
73 List
*nonnullable_vars
,
74 List
*forced_null_vars
);
75 static void substitute_multiple_relids(Node
*node
,
76 int varno
, Relids subrelids
);
77 static void fix_append_rel_relids(List
*append_rel_list
, int varno
,
79 static Node
*find_jointree_node_for_rel(Node
*jtnode
, int relid
);
84 * Attempt to pull up ANY and EXISTS SubLinks to be treated as
85 * semijoins or anti-semijoins.
87 * A clause "foo op ANY (sub-SELECT)" can be processed by pulling the
88 * sub-SELECT up to become a rangetable entry and treating the implied
89 * comparisons as quals of a semijoin. However, this optimization *only*
90 * works at the top level of WHERE or a JOIN/ON clause, because we cannot
91 * distinguish whether the ANY ought to return FALSE or NULL in cases
92 * involving NULL inputs. Also, in an outer join's ON clause we can only
93 * do this if the sublink is degenerate (ie, references only the nullable
94 * side of the join). In that case we can effectively push the semijoin
95 * down into the nullable side of the join. If the sublink references any
96 * nonnullable-side variables then it would have to be evaluated as part
97 * of the outer join, which makes things way too complicated.
99 * Under similar conditions, EXISTS and NOT EXISTS clauses can be handled
100 * by pulling up the sub-SELECT and creating a semijoin or anti-semijoin.
102 * This routine searches for such clauses and does the necessary parsetree
103 * transformations if any are found.
105 * This routine has to run before preprocess_expression(), so the quals
106 * clauses are not yet reduced to implicit-AND format. That means we need
107 * to recursively search through explicit AND clauses, which are
108 * probably only binary ANDs. We stop as soon as we hit a non-AND item.
111 pull_up_sublinks(PlannerInfo
*root
)
115 /* Begin recursion through the jointree */
116 root
->parse
->jointree
= (FromExpr
*)
117 pull_up_sublinks_jointree_recurse(root
,
118 (Node
*) root
->parse
->jointree
,
123 * Recurse through jointree nodes for pull_up_sublinks()
125 * In addition to returning the possibly-modified jointree node, we return
126 * a relids set of the contained rels into *relids.
129 pull_up_sublinks_jointree_recurse(PlannerInfo
*root
, Node
*jtnode
,
136 else if (IsA(jtnode
, RangeTblRef
))
138 int varno
= ((RangeTblRef
*) jtnode
)->rtindex
;
140 *relids
= bms_make_singleton(varno
);
141 /* jtnode is returned unmodified */
143 else if (IsA(jtnode
, FromExpr
))
145 FromExpr
*f
= (FromExpr
*) jtnode
;
146 List
*newfromlist
= NIL
;
148 List
*subfromlist
= NIL
;
149 Relids frelids
= NULL
;
152 /* First, recurse to process children and collect their relids */
153 foreach(l
, f
->fromlist
)
158 newchild
= pull_up_sublinks_jointree_recurse(root
,
161 newfromlist
= lappend(newfromlist
, newchild
);
162 frelids
= bms_join(frelids
, childrelids
);
164 /* Now process qual --- all children are available for use */
165 newquals
= pull_up_sublinks_qual_recurse(root
, f
->quals
, frelids
,
167 /* Any pulled-up subqueries can just be attached to the fromlist */
168 newfromlist
= list_concat(newfromlist
, subfromlist
);
171 * Although we could include the pulled-up subqueries in the returned
172 * relids, there's no need since upper quals couldn't refer to their
176 jtnode
= (Node
*) makeFromExpr(newfromlist
, newquals
);
178 else if (IsA(jtnode
, JoinExpr
))
183 List
*subfromlist
= NIL
;
186 * Make a modifiable copy of join node, but don't bother copying
187 * its subnodes (yet).
189 j
= (JoinExpr
*) palloc(sizeof(JoinExpr
));
190 memcpy(j
, jtnode
, sizeof(JoinExpr
));
192 /* Recurse to process children and collect their relids */
193 j
->larg
= pull_up_sublinks_jointree_recurse(root
, j
->larg
,
195 j
->rarg
= pull_up_sublinks_jointree_recurse(root
, j
->rarg
,
199 * Now process qual, showing appropriate child relids as available,
200 * and then attach any pulled-up jointree items at the right place.
201 * The pulled-up items must go below where the quals that refer to
202 * them will be placed. Since the JoinExpr itself can only handle
203 * two child nodes, we hack up a valid jointree by inserting dummy
204 * FromExprs that have no quals. These should get flattened out
205 * during deconstruct_recurse(), so they won't impose any extra
211 j
->quals
= pull_up_sublinks_qual_recurse(root
, j
->quals
,
212 bms_union(leftrelids
,
215 /* We arbitrarily put pulled-up subqueries into right child */
217 j
->rarg
= (Node
*) makeFromExpr(lcons(j
->rarg
,
222 j
->quals
= pull_up_sublinks_qual_recurse(root
, j
->quals
,
225 /* Any pulled-up subqueries must go into right child */
227 j
->rarg
= (Node
*) makeFromExpr(lcons(j
->rarg
,
232 /* can't do anything with full-join quals */
235 j
->quals
= pull_up_sublinks_qual_recurse(root
, j
->quals
,
238 /* Any pulled-up subqueries must go into left child */
240 j
->larg
= (Node
*) makeFromExpr(lcons(j
->larg
,
245 elog(ERROR
, "unrecognized join type: %d",
251 * Although we could include the pulled-up subqueries in the returned
252 * relids, there's no need since upper quals couldn't refer to their
253 * outputs anyway. But we *do* need to include the join's own rtindex
254 * because we haven't yet collapsed join alias variables, so upper
255 * levels would mistakenly think they couldn't use references to this
258 *relids
= bms_add_member(bms_join(leftrelids
, rightrelids
),
263 elog(ERROR
, "unrecognized node type: %d",
264 (int) nodeTag(jtnode
));
269 * Recurse through top-level qual nodes for pull_up_sublinks()
271 * Caller must have initialized *fromlist to NIL. We append any new
272 * jointree items to that list.
275 pull_up_sublinks_qual_recurse(PlannerInfo
*root
, Node
*node
,
276 Relids available_rels
, List
**fromlist
)
280 if (IsA(node
, SubLink
))
282 SubLink
*sublink
= (SubLink
*) node
;
286 /* Is it a convertible ANY or EXISTS clause? */
287 if (sublink
->subLinkType
== ANY_SUBLINK
)
289 if (convert_ANY_sublink_to_join(root
, sublink
,
291 &new_qual
, &new_fromlist
))
293 *fromlist
= list_concat(*fromlist
, new_fromlist
);
297 else if (sublink
->subLinkType
== EXISTS_SUBLINK
)
299 if (convert_EXISTS_sublink_to_join(root
, sublink
, false,
301 &new_qual
, &new_fromlist
))
303 *fromlist
= list_concat(*fromlist
, new_fromlist
);
307 /* Else return it unmodified */
310 if (not_clause(node
))
312 /* If the immediate argument of NOT is EXISTS, try to convert */
313 SubLink
*sublink
= (SubLink
*) get_notclausearg((Expr
*) node
);
317 if (sublink
&& IsA(sublink
, SubLink
))
319 if (sublink
->subLinkType
== EXISTS_SUBLINK
)
321 if (convert_EXISTS_sublink_to_join(root
, sublink
, true,
323 &new_qual
, &new_fromlist
))
325 *fromlist
= list_concat(*fromlist
, new_fromlist
);
330 /* Else return it unmodified */
333 if (and_clause(node
))
335 /* Recurse into AND clause */
336 List
*newclauses
= NIL
;
339 foreach(l
, ((BoolExpr
*) node
)->args
)
341 Node
*oldclause
= (Node
*) lfirst(l
);
343 newclauses
= lappend(newclauses
,
344 pull_up_sublinks_qual_recurse(root
,
349 return (Node
*) make_andclause(newclauses
);
351 /* Stop if not an AND */
356 * inline_set_returning_functions
357 * Attempt to "inline" set-returning functions in the FROM clause.
359 * If an RTE_FUNCTION rtable entry invokes a set-returning function that
360 * contains just a simple SELECT, we can convert the rtable entry to an
361 * RTE_SUBQUERY entry exposing the SELECT directly. This is especially
362 * useful if the subquery can then be "pulled up" for further optimization,
363 * but we do it even if not, to reduce executor overhead.
365 * This has to be done before we have started to do any optimization of
366 * subqueries, else any such steps wouldn't get applied to subqueries
367 * obtained via inlining. However, we do it after pull_up_sublinks
368 * so that we can inline any functions used in SubLink subselects.
370 * Like most of the planner, this feels free to scribble on its input data
374 inline_set_returning_functions(PlannerInfo
*root
)
378 foreach(rt
, root
->parse
->rtable
)
380 RangeTblEntry
*rte
= (RangeTblEntry
*) lfirst(rt
);
382 if (rte
->rtekind
== RTE_FUNCTION
)
386 /* Check safety of expansion, and expand if possible */
387 funcquery
= inline_set_returning_function(root
, rte
);
390 /* Successful expansion, replace the rtable entry */
391 rte
->rtekind
= RTE_SUBQUERY
;
392 rte
->subquery
= funcquery
;
393 rte
->funcexpr
= NULL
;
394 rte
->funccoltypes
= NIL
;
395 rte
->funccoltypmods
= NIL
;
403 * Look for subqueries in the rangetable that can be pulled up into
404 * the parent query. If the subquery has no special features like
405 * grouping/aggregation then we can merge it into the parent's jointree.
406 * Also, subqueries that are simple UNION ALL structures can be
407 * converted into "append relations".
409 * below_outer_join is true if this jointree node is within the nullable
410 * side of an outer join. This forces use of the PlaceHolderVar mechanism
411 * for non-nullable targetlist items.
413 * append_rel_member is true if we are looking at a member subquery of
414 * an append relation. This forces use of the PlaceHolderVar mechanism
415 * for all non-Var targetlist items, and puts some additional restrictions
416 * on what can be pulled up.
418 * A tricky aspect of this code is that if we pull up a subquery we have
419 * to replace Vars that reference the subquery's outputs throughout the
420 * parent query, including quals attached to jointree nodes above the one
421 * we are currently processing! We handle this by being careful not to
422 * change the jointree structure while recursing: no nodes other than
423 * subquery RangeTblRef entries will be replaced. Also, we can't turn
424 * ResolveNew loose on the whole jointree, because it'll return a mutated
425 * copy of the tree; we have to invoke it just on the quals, instead.
428 pull_up_subqueries(PlannerInfo
*root
, Node
*jtnode
,
429 bool below_outer_join
, bool append_rel_member
)
433 if (IsA(jtnode
, RangeTblRef
))
435 int varno
= ((RangeTblRef
*) jtnode
)->rtindex
;
436 RangeTblEntry
*rte
= rt_fetch(varno
, root
->parse
->rtable
);
439 * Is this a subquery RTE, and if so, is the subquery simple enough to
442 * If we are looking at an append-relation member, we can't pull it up
443 * unless is_safe_append_member says so.
445 if (rte
->rtekind
== RTE_SUBQUERY
&&
446 is_simple_subquery(rte
->subquery
) &&
447 (!append_rel_member
|| is_safe_append_member(rte
->subquery
)))
448 return pull_up_simple_subquery(root
, jtnode
, rte
,
453 * Alternatively, is it a simple UNION ALL subquery? If so, flatten
454 * into an "append relation".
456 * It's safe to do this regardless of whether this query is
457 * itself an appendrel member. (If you're thinking we should try to
458 * flatten the two levels of appendrel together, you're right; but we
459 * handle that in set_append_rel_pathlist, not here.)
461 if (rte
->rtekind
== RTE_SUBQUERY
&&
462 is_simple_union_all(rte
->subquery
))
463 return pull_up_simple_union_all(root
, jtnode
, rte
);
465 /* Otherwise, do nothing at this node. */
467 else if (IsA(jtnode
, FromExpr
))
469 FromExpr
*f
= (FromExpr
*) jtnode
;
472 Assert(!append_rel_member
);
473 foreach(l
, f
->fromlist
)
474 lfirst(l
) = pull_up_subqueries(root
, lfirst(l
),
475 below_outer_join
, false);
477 else if (IsA(jtnode
, JoinExpr
))
479 JoinExpr
*j
= (JoinExpr
*) jtnode
;
481 Assert(!append_rel_member
);
482 /* Recurse, being careful to tell myself when inside outer join */
486 j
->larg
= pull_up_subqueries(root
, j
->larg
,
487 below_outer_join
, false);
488 j
->rarg
= pull_up_subqueries(root
, j
->rarg
,
489 below_outer_join
, false);
492 j
->larg
= pull_up_subqueries(root
, j
->larg
,
493 below_outer_join
, false);
494 j
->rarg
= pull_up_subqueries(root
, j
->rarg
,
498 j
->larg
= pull_up_subqueries(root
, j
->larg
,
500 j
->rarg
= pull_up_subqueries(root
, j
->rarg
,
504 j
->larg
= pull_up_subqueries(root
, j
->larg
,
506 j
->rarg
= pull_up_subqueries(root
, j
->rarg
,
507 below_outer_join
, false);
510 elog(ERROR
, "unrecognized join type: %d",
516 elog(ERROR
, "unrecognized node type: %d",
517 (int) nodeTag(jtnode
));
522 * pull_up_simple_subquery
523 * Attempt to pull up a single simple subquery.
525 * jtnode is a RangeTblRef that has been tentatively identified as a simple
526 * subquery by pull_up_subqueries. We return the replacement jointree node,
527 * or jtnode itself if we determine that the subquery can't be pulled up after
531 pull_up_simple_subquery(PlannerInfo
*root
, Node
*jtnode
, RangeTblEntry
*rte
,
532 bool below_outer_join
, bool append_rel_member
)
534 Query
*parse
= root
->parse
;
535 int varno
= ((RangeTblRef
*) jtnode
)->rtindex
;
537 PlannerInfo
*subroot
;
543 * Need a modifiable copy of the subquery to hack on. Even if we didn't
544 * sometimes choose not to pull up below, we must do this to avoid
545 * problems if the same subquery is referenced from multiple jointree
546 * items (which can't happen normally, but might after rule rewriting).
548 subquery
= copyObject(rte
->subquery
);
551 * Create a PlannerInfo data structure for this subquery.
553 * NOTE: the next few steps should match the first processing in
554 * subquery_planner(). Can we refactor to avoid code duplication, or
555 * would that just make things uglier?
557 subroot
= makeNode(PlannerInfo
);
558 subroot
->parse
= subquery
;
559 subroot
->glob
= root
->glob
;
560 subroot
->query_level
= root
->query_level
;
561 subroot
->parent_root
= root
->parent_root
;
562 subroot
->planner_cxt
= CurrentMemoryContext
;
563 subroot
->init_plans
= NIL
;
564 subroot
->cte_plan_ids
= NIL
;
565 subroot
->eq_classes
= NIL
;
566 subroot
->append_rel_list
= NIL
;
567 subroot
->hasRecursion
= false;
568 subroot
->wt_param_id
= -1;
569 subroot
->non_recursive_plan
= NULL
;
571 /* No CTEs to worry about */
572 Assert(subquery
->cteList
== NIL
);
575 * Pull up any SubLinks within the subquery's quals, so that we don't
576 * leave unoptimized SubLinks behind.
578 if (subquery
->hasSubLinks
)
579 pull_up_sublinks(subroot
);
582 * Similarly, inline any set-returning functions in its rangetable.
584 inline_set_returning_functions(subroot
);
587 * Recursively pull up the subquery's subqueries, so that
588 * pull_up_subqueries' processing is complete for its jointree and
591 * Note: below_outer_join = false is correct here even if we are within an
592 * outer join in the upper query; the lower query starts with a clean
593 * slate for outer-join semantics. Likewise, we say we aren't handling an
596 subquery
->jointree
= (FromExpr
*)
597 pull_up_subqueries(subroot
, (Node
*) subquery
->jointree
, false, false);
600 * Now we must recheck whether the subquery is still simple enough to pull
601 * up. If not, abandon processing it.
603 * We don't really need to recheck all the conditions involved, but it's
604 * easier just to keep this "if" looking the same as the one in
605 * pull_up_subqueries.
607 if (is_simple_subquery(subquery
) &&
608 (!append_rel_member
|| is_safe_append_member(subquery
)))
615 * Give up, return unmodified RangeTblRef.
617 * Note: The work we just did will be redone when the subquery gets
618 * planned on its own. Perhaps we could avoid that by storing the
619 * modified subquery back into the rangetable, but I'm not gonna risk
626 * Adjust level-0 varnos in subquery so that we can append its rangetable
627 * to upper query's. We have to fix the subquery's append_rel_list
630 rtoffset
= list_length(parse
->rtable
);
631 OffsetVarNodes((Node
*) subquery
, rtoffset
, 0);
632 OffsetVarNodes((Node
*) subroot
->append_rel_list
, rtoffset
, 0);
635 * Upper-level vars in subquery are now one level closer to their parent
638 IncrementVarSublevelsUp((Node
*) subquery
, -1, 1);
639 IncrementVarSublevelsUp((Node
*) subroot
->append_rel_list
, -1, 1);
642 * The subquery's targetlist items are now in the appropriate form to
643 * insert into the top query, but if we are under an outer join then
644 * non-nullable items have to be turned into PlaceHolderVars. If we
645 * are dealing with an appendrel member then anything that's not a
646 * simple Var has to be turned into a PlaceHolderVar.
648 if (below_outer_join
|| append_rel_member
)
649 subtlist
= insert_targetlist_placeholders(root
, subquery
->targetList
,
650 varno
, append_rel_member
);
652 subtlist
= subquery
->targetList
;
655 * Replace all of the top query's references to the subquery's outputs
656 * with copies of the adjusted subtlist items, being careful not to
657 * replace any of the jointree structure. (This'd be a lot cleaner if we
658 * could use query_tree_mutator.)
660 parse
->targetList
= (List
*)
661 ResolveNew((Node
*) parse
->targetList
,
663 subtlist
, CMD_SELECT
, 0);
664 parse
->returningList
= (List
*)
665 ResolveNew((Node
*) parse
->returningList
,
667 subtlist
, CMD_SELECT
, 0);
668 resolvenew_in_jointree((Node
*) parse
->jointree
, varno
,
670 Assert(parse
->setOperations
== NULL
);
672 ResolveNew(parse
->havingQual
,
674 subtlist
, CMD_SELECT
, 0);
675 root
->append_rel_list
= (List
*)
676 ResolveNew((Node
*) root
->append_rel_list
,
678 subtlist
, CMD_SELECT
, 0);
680 foreach(rt
, parse
->rtable
)
682 RangeTblEntry
*otherrte
= (RangeTblEntry
*) lfirst(rt
);
684 if (otherrte
->rtekind
== RTE_JOIN
)
685 otherrte
->joinaliasvars
= (List
*)
686 ResolveNew((Node
*) otherrte
->joinaliasvars
,
688 subtlist
, CMD_SELECT
, 0);
692 * Now append the adjusted rtable entries to upper query. (We hold off
693 * until after fixing the upper rtable entries; no point in running that
694 * code on the subquery ones too.)
696 parse
->rtable
= list_concat(parse
->rtable
, subquery
->rtable
);
699 * Pull up any FOR UPDATE/SHARE markers, too. (OffsetVarNodes already
700 * adjusted the marker rtindexes, so just concat the lists.)
702 parse
->rowMarks
= list_concat(parse
->rowMarks
, subquery
->rowMarks
);
705 * We also have to fix the relid sets of any FlattenedSubLink and
706 * PlaceHolderVar nodes in the parent query. (This could perhaps be done
707 * by ResolveNew, but it would clutter that routine's API unreasonably.)
708 * Note in particular that any PlaceHolderVar nodes just created by
709 * insert_targetlist_placeholders() will be adjusted, so having created
710 * them with the subquery's varno is correct.
712 * Likewise, relids appearing in AppendRelInfo nodes have to be fixed.
713 * We already checked that this won't require introducing multiple
714 * subrelids into the single-slot AppendRelInfo structs.
716 if (parse
->hasSubLinks
|| root
->glob
->lastPHId
!= 0 ||
717 root
->append_rel_list
)
721 subrelids
= get_relids_in_jointree((Node
*) subquery
->jointree
, false);
722 substitute_multiple_relids((Node
*) parse
, varno
, subrelids
);
723 fix_append_rel_relids(root
->append_rel_list
, varno
, subrelids
);
727 * And now add subquery's AppendRelInfos to our list.
729 root
->append_rel_list
= list_concat(root
->append_rel_list
,
730 subroot
->append_rel_list
);
733 * We don't have to do the equivalent bookkeeping for outer-join info,
734 * because that hasn't been set up yet. placeholder_list likewise.
736 Assert(root
->join_info_list
== NIL
);
737 Assert(subroot
->join_info_list
== NIL
);
738 Assert(root
->placeholder_list
== NIL
);
739 Assert(subroot
->placeholder_list
== NIL
);
742 * Miscellaneous housekeeping.
744 parse
->hasSubLinks
|= subquery
->hasSubLinks
;
745 /* subquery won't be pulled up if it hasAggs, so no work there */
748 * Return the adjusted subquery jointree to replace the RangeTblRef entry
749 * in parent's jointree.
751 return (Node
*) subquery
->jointree
;
755 * pull_up_simple_union_all
756 * Pull up a single simple UNION ALL subquery.
758 * jtnode is a RangeTblRef that has been identified as a simple UNION ALL
759 * subquery by pull_up_subqueries. We pull up the leaf subqueries and
760 * build an "append relation" for the union set. The result value is just
761 * jtnode, since we don't actually need to change the query jointree.
764 pull_up_simple_union_all(PlannerInfo
*root
, Node
*jtnode
, RangeTblEntry
*rte
)
766 int varno
= ((RangeTblRef
*) jtnode
)->rtindex
;
767 Query
*subquery
= rte
->subquery
;
772 * Append the subquery rtable entries to upper query.
774 rtoffset
= list_length(root
->parse
->rtable
);
777 * Append child RTEs to parent rtable.
779 * Upper-level vars in subquery are now one level closer to their
780 * parent than before. We don't have to worry about offsetting
781 * varnos, though, because any such vars must refer to stuff above the
782 * level of the query we are pulling into.
784 rtable
= copyObject(subquery
->rtable
);
785 IncrementVarSublevelsUp_rtable(rtable
, -1, 1);
786 root
->parse
->rtable
= list_concat(root
->parse
->rtable
, rtable
);
789 * Recursively scan the subquery's setOperations tree and add
790 * AppendRelInfo nodes for leaf subqueries to the parent's
793 Assert(subquery
->setOperations
);
794 pull_up_union_leaf_queries(subquery
->setOperations
, root
, varno
, subquery
,
798 * Mark the parent as an append relation.
806 * pull_up_union_leaf_queries -- recursive guts of pull_up_simple_union_all
808 * Note that setOpQuery is the Query containing the setOp node, whose rtable
809 * is where to look up the RTE if setOp is a RangeTblRef. This is *not* the
810 * same as root->parse, which is the top-level Query we are pulling up into.
812 * parentRTindex is the appendrel parent's index in root->parse->rtable.
814 * The child RTEs have already been copied to the parent. childRToffset
815 * tells us where in the parent's range table they were copied.
818 pull_up_union_leaf_queries(Node
*setOp
, PlannerInfo
*root
, int parentRTindex
,
819 Query
*setOpQuery
, int childRToffset
)
821 if (IsA(setOp
, RangeTblRef
))
823 RangeTblRef
*rtr
= (RangeTblRef
*) setOp
;
825 AppendRelInfo
*appinfo
;
828 * Calculate the index in the parent's range table
830 childRTindex
= childRToffset
+ rtr
->rtindex
;
833 * Build a suitable AppendRelInfo, and attach to parent's list.
835 appinfo
= makeNode(AppendRelInfo
);
836 appinfo
->parent_relid
= parentRTindex
;
837 appinfo
->child_relid
= childRTindex
;
838 appinfo
->parent_reltype
= InvalidOid
;
839 appinfo
->child_reltype
= InvalidOid
;
840 make_setop_translation_list(setOpQuery
, childRTindex
,
841 &appinfo
->translated_vars
);
842 appinfo
->parent_reloid
= InvalidOid
;
843 root
->append_rel_list
= lappend(root
->append_rel_list
, appinfo
);
846 * Recursively apply pull_up_subqueries to the new child RTE. (We
847 * must build the AppendRelInfo first, because this will modify it.)
848 * Note that we can pass below_outer_join = false even if we're
849 * actually under an outer join, because the child's expressions
850 * aren't going to propagate up above the join.
852 rtr
= makeNode(RangeTblRef
);
853 rtr
->rtindex
= childRTindex
;
854 (void) pull_up_subqueries(root
, (Node
*) rtr
, false, true);
856 else if (IsA(setOp
, SetOperationStmt
))
858 SetOperationStmt
*op
= (SetOperationStmt
*) setOp
;
860 /* Recurse to reach leaf queries */
861 pull_up_union_leaf_queries(op
->larg
, root
, parentRTindex
, setOpQuery
,
863 pull_up_union_leaf_queries(op
->rarg
, root
, parentRTindex
, setOpQuery
,
868 elog(ERROR
, "unrecognized node type: %d",
869 (int) nodeTag(setOp
));
874 * make_setop_translation_list
875 * Build the list of translations from parent Vars to child Vars for
876 * a UNION ALL member. (At this point it's just a simple list of
877 * referencing Vars, but if we succeed in pulling up the member
878 * subquery, the Vars will get replaced by pulled-up expressions.)
881 make_setop_translation_list(Query
*query
, Index newvarno
,
882 List
**translated_vars
)
887 foreach(l
, query
->targetList
)
889 TargetEntry
*tle
= (TargetEntry
*) lfirst(l
);
894 vars
= lappend(vars
, makeVar(newvarno
,
896 exprType((Node
*) tle
->expr
),
897 exprTypmod((Node
*) tle
->expr
),
901 *translated_vars
= vars
;
906 * Check a subquery in the range table to see if it's simple enough
907 * to pull up into the parent query.
910 is_simple_subquery(Query
*subquery
)
913 * Let's just make sure it's a valid subselect ...
915 if (!IsA(subquery
, Query
) ||
916 subquery
->commandType
!= CMD_SELECT
||
917 subquery
->utilityStmt
!= NULL
||
918 subquery
->intoClause
!= NULL
)
919 elog(ERROR
, "subquery is bogus");
922 * Can't currently pull up a query with setops (unless it's simple UNION
923 * ALL, which is handled by a different code path). Maybe after querytree
926 if (subquery
->setOperations
)
930 * Can't pull up a subquery involving grouping, aggregation, sorting,
931 * limiting, or WITH. (XXX WITH could possibly be allowed later)
933 if (subquery
->hasAggs
||
934 subquery
->groupClause
||
935 subquery
->havingQual
||
936 subquery
->sortClause
||
937 subquery
->distinctClause
||
938 subquery
->limitOffset
||
939 subquery
->limitCount
||
944 * Don't pull up a subquery that has any set-returning functions in its
945 * targetlist. Otherwise we might well wind up inserting set-returning
946 * functions into places where they mustn't go, such as quals of higher
949 if (expression_returns_set((Node
*) subquery
->targetList
))
953 * Don't pull up a subquery that has any volatile functions in its
954 * targetlist. Otherwise we might introduce multiple evaluations of these
955 * functions, if they get copied to multiple places in the upper query,
956 * leading to surprising results. (Note: the PlaceHolderVar mechanism
957 * doesn't quite guarantee single evaluation; else we could pull up anyway
958 * and just wrap such items in PlaceHolderVars ...)
960 if (contain_volatile_functions((Node
*) subquery
->targetList
))
964 * Hack: don't try to pull up a subquery with an empty jointree.
965 * query_planner() will correctly generate a Result plan for a jointree
966 * that's totally empty, but I don't think the right things happen if an
967 * empty FromExpr appears lower down in a jointree. It would pose a
968 * problem for the PlaceHolderVar mechanism too, since we'd have no
969 * way to identify where to evaluate a PHV coming out of the subquery.
970 * Not worth working hard on this, just to collapse SubqueryScan/Result
971 * into Result; especially since the SubqueryScan can often be optimized
972 * away by setrefs.c anyway.
974 if (subquery
->jointree
->fromlist
== NIL
)
981 * is_simple_union_all
982 * Check a subquery to see if it's a simple UNION ALL.
984 * We require all the setops to be UNION ALL (no mixing) and there can't be
985 * any datatype coercions involved, ie, all the leaf queries must emit the
989 is_simple_union_all(Query
*subquery
)
991 SetOperationStmt
*topop
;
993 /* Let's just make sure it's a valid subselect ... */
994 if (!IsA(subquery
, Query
) ||
995 subquery
->commandType
!= CMD_SELECT
||
996 subquery
->utilityStmt
!= NULL
||
997 subquery
->intoClause
!= NULL
)
998 elog(ERROR
, "subquery is bogus");
1000 /* Is it a set-operation query at all? */
1001 topop
= (SetOperationStmt
*) subquery
->setOperations
;
1004 Assert(IsA(topop
, SetOperationStmt
));
1006 /* Can't handle ORDER BY, LIMIT/OFFSET, locking, or WITH */
1007 if (subquery
->sortClause
||
1008 subquery
->limitOffset
||
1009 subquery
->limitCount
||
1010 subquery
->rowMarks
||
1014 /* Recursively check the tree of set operations */
1015 return is_simple_union_all_recurse((Node
*) topop
, subquery
,
1020 is_simple_union_all_recurse(Node
*setOp
, Query
*setOpQuery
, List
*colTypes
)
1022 if (IsA(setOp
, RangeTblRef
))
1024 RangeTblRef
*rtr
= (RangeTblRef
*) setOp
;
1025 RangeTblEntry
*rte
= rt_fetch(rtr
->rtindex
, setOpQuery
->rtable
);
1026 Query
*subquery
= rte
->subquery
;
1028 Assert(subquery
!= NULL
);
1030 /* Leaf nodes are OK if they match the toplevel column types */
1031 /* We don't have to compare typmods here */
1032 return tlist_same_datatypes(subquery
->targetList
, colTypes
, true);
1034 else if (IsA(setOp
, SetOperationStmt
))
1036 SetOperationStmt
*op
= (SetOperationStmt
*) setOp
;
1038 /* Must be UNION ALL */
1039 if (op
->op
!= SETOP_UNION
|| !op
->all
)
1042 /* Recurse to check inputs */
1043 return is_simple_union_all_recurse(op
->larg
, setOpQuery
, colTypes
) &&
1044 is_simple_union_all_recurse(op
->rarg
, setOpQuery
, colTypes
);
1048 elog(ERROR
, "unrecognized node type: %d",
1049 (int) nodeTag(setOp
));
1050 return false; /* keep compiler quiet */
1055 * insert_targetlist_placeholders
1056 * Insert PlaceHolderVar nodes into any non-junk targetlist items that are
1057 * not simple variables or strict functions of simple variables (and hence
1058 * might not correctly go to NULL when examined above the point of an outer
1059 * join). We assume we can modify the tlist items in-place.
1061 * varno is the upper-query relid of the subquery; this is used as the
1062 * syntactic location of the PlaceHolderVars.
1063 * If wrap_non_vars is true then *only* simple Var references escape being
1064 * wrapped with PlaceHolderVars.
1067 insert_targetlist_placeholders(PlannerInfo
*root
, List
*tlist
,
1068 int varno
, bool wrap_non_vars
)
1074 TargetEntry
*tle
= (TargetEntry
*) lfirst(lc
);
1076 /* ignore resjunk columns */
1081 * Simple Vars always escape being wrapped. This is common enough
1082 * to deserve a fast path even if we aren't doing wrap_non_vars.
1084 if (tle
->expr
&& IsA(tle
->expr
, Var
) &&
1085 ((Var
*) tle
->expr
)->varlevelsup
== 0)
1091 * If it contains a Var of current level, and does not contain
1092 * any non-strict constructs, then it's certainly nullable and we
1093 * don't need to insert a PlaceHolderVar. (Note: in future maybe
1094 * we should insert PlaceHolderVars anyway, when a tlist item is
1095 * expensive to evaluate?
1097 if (contain_vars_of_level((Node
*) tle
->expr
, 0) &&
1098 !contain_nonstrict_functions((Node
*) tle
->expr
))
1102 /* Else wrap it in a PlaceHolderVar */
1103 tle
->expr
= (Expr
*) make_placeholder_expr(root
,
1105 bms_make_singleton(varno
));
1111 * is_safe_append_member
1112 * Check a subquery that is a leaf of a UNION ALL appendrel to see if it's
1116 is_safe_append_member(Query
*subquery
)
1121 * It's only safe to pull up the child if its jointree contains exactly
1122 * one RTE, else the AppendRelInfo data structure breaks. The one base RTE
1123 * could be buried in several levels of FromExpr, however.
1125 * Also, the child can't have any WHERE quals because there's no place to
1126 * put them in an appendrel. (This is a bit annoying...) If we didn't
1127 * need to check this, we'd just test whether get_relids_in_jointree()
1128 * yields a singleton set, to be more consistent with the coding of
1129 * fix_append_rel_relids().
1131 jtnode
= subquery
->jointree
;
1132 while (IsA(jtnode
, FromExpr
))
1134 if (jtnode
->quals
!= NULL
)
1136 if (list_length(jtnode
->fromlist
) != 1)
1138 jtnode
= linitial(jtnode
->fromlist
);
1140 if (!IsA(jtnode
, RangeTblRef
))
1147 * Helper routine for pull_up_subqueries: do ResolveNew on every expression
1148 * in the jointree, without changing the jointree structure itself. Ugly,
1149 * but there's no other way...
1152 resolvenew_in_jointree(Node
*jtnode
, int varno
,
1153 RangeTblEntry
*rte
, List
*subtlist
)
1157 if (IsA(jtnode
, RangeTblRef
))
1159 /* nothing to do here */
1161 else if (IsA(jtnode
, FromExpr
))
1163 FromExpr
*f
= (FromExpr
*) jtnode
;
1166 foreach(l
, f
->fromlist
)
1167 resolvenew_in_jointree(lfirst(l
), varno
, rte
, subtlist
);
1168 f
->quals
= ResolveNew(f
->quals
,
1170 subtlist
, CMD_SELECT
, 0);
1172 else if (IsA(jtnode
, JoinExpr
))
1174 JoinExpr
*j
= (JoinExpr
*) jtnode
;
1176 resolvenew_in_jointree(j
->larg
, varno
, rte
, subtlist
);
1177 resolvenew_in_jointree(j
->rarg
, varno
, rte
, subtlist
);
1178 j
->quals
= ResolveNew(j
->quals
,
1180 subtlist
, CMD_SELECT
, 0);
1183 * We don't bother to update the colvars list, since it won't be used
1188 elog(ERROR
, "unrecognized node type: %d",
1189 (int) nodeTag(jtnode
));
1193 * reduce_outer_joins
1194 * Attempt to reduce outer joins to plain inner joins.
1196 * The idea here is that given a query like
1197 * SELECT ... FROM a LEFT JOIN b ON (...) WHERE b.y = 42;
1198 * we can reduce the LEFT JOIN to a plain JOIN if the "=" operator in WHERE
1199 * is strict. The strict operator will always return NULL, causing the outer
1200 * WHERE to fail, on any row where the LEFT JOIN filled in NULLs for b's
1201 * columns. Therefore, there's no need for the join to produce null-extended
1202 * rows in the first place --- which makes it a plain join not an outer join.
1203 * (This scenario may not be very likely in a query written out by hand, but
1204 * it's reasonably likely when pushing quals down into complex views.)
1206 * More generally, an outer join can be reduced in strength if there is a
1207 * strict qual above it in the qual tree that constrains a Var from the
1208 * nullable side of the join to be non-null. (For FULL joins this applies
1209 * to each side separately.)
1211 * Another transformation we apply here is to recognize cases like
1212 * SELECT ... FROM a LEFT JOIN b ON (a.x = b.y) WHERE b.y IS NULL;
1213 * If the join clause is strict for b.y, then only null-extended rows could
1214 * pass the upper WHERE, and we can conclude that what the query is really
1215 * specifying is an anti-semijoin. We change the join type from JOIN_LEFT
1216 * to JOIN_ANTI. The IS NULL clause then becomes redundant, and must be
1217 * removed to prevent bogus selectivity calculations, but we leave it to
1218 * distribute_qual_to_rels to get rid of such clauses.
1220 * Also, we get rid of JOIN_RIGHT cases by flipping them around to become
1221 * JOIN_LEFT. This saves some code here and in some later planner routines,
1222 * but the main reason to do it is to not need to invent a JOIN_REVERSE_ANTI
1225 * To ease recognition of strict qual clauses, we require this routine to be
1226 * run after expression preprocessing (i.e., qual canonicalization and JOIN
1227 * alias-var expansion).
1230 reduce_outer_joins(PlannerInfo
*root
)
1232 reduce_outer_joins_state
*state
;
1235 * To avoid doing strictness checks on more quals than necessary, we want
1236 * to stop descending the jointree as soon as there are no outer joins
1237 * below our current point. This consideration forces a two-pass process.
1238 * The first pass gathers information about which base rels appear below
1239 * each side of each join clause, and about whether there are outer
1240 * join(s) below each side of each join clause. The second pass examines
1241 * qual clauses and changes join types as it descends the tree.
1243 state
= reduce_outer_joins_pass1((Node
*) root
->parse
->jointree
);
1245 /* planner.c shouldn't have called me if no outer joins */
1246 if (state
== NULL
|| !state
->contains_outer
)
1247 elog(ERROR
, "so where are the outer joins?");
1249 reduce_outer_joins_pass2((Node
*) root
->parse
->jointree
,
1250 state
, root
, NULL
, NIL
, NIL
);
1254 * reduce_outer_joins_pass1 - phase 1 data collection
1256 * Returns a state node describing the given jointree node.
1258 static reduce_outer_joins_state
*
1259 reduce_outer_joins_pass1(Node
*jtnode
)
1261 reduce_outer_joins_state
*result
;
1263 result
= (reduce_outer_joins_state
*)
1264 palloc(sizeof(reduce_outer_joins_state
));
1265 result
->relids
= NULL
;
1266 result
->contains_outer
= false;
1267 result
->sub_states
= NIL
;
1271 if (IsA(jtnode
, RangeTblRef
))
1273 int varno
= ((RangeTblRef
*) jtnode
)->rtindex
;
1275 result
->relids
= bms_make_singleton(varno
);
1277 else if (IsA(jtnode
, FromExpr
))
1279 FromExpr
*f
= (FromExpr
*) jtnode
;
1282 foreach(l
, f
->fromlist
)
1284 reduce_outer_joins_state
*sub_state
;
1286 sub_state
= reduce_outer_joins_pass1(lfirst(l
));
1287 result
->relids
= bms_add_members(result
->relids
,
1289 result
->contains_outer
|= sub_state
->contains_outer
;
1290 result
->sub_states
= lappend(result
->sub_states
, sub_state
);
1293 else if (IsA(jtnode
, JoinExpr
))
1295 JoinExpr
*j
= (JoinExpr
*) jtnode
;
1296 reduce_outer_joins_state
*sub_state
;
1298 /* join's own RT index is not wanted in result->relids */
1299 if (IS_OUTER_JOIN(j
->jointype
))
1300 result
->contains_outer
= true;
1302 sub_state
= reduce_outer_joins_pass1(j
->larg
);
1303 result
->relids
= bms_add_members(result
->relids
,
1305 result
->contains_outer
|= sub_state
->contains_outer
;
1306 result
->sub_states
= lappend(result
->sub_states
, sub_state
);
1308 sub_state
= reduce_outer_joins_pass1(j
->rarg
);
1309 result
->relids
= bms_add_members(result
->relids
,
1311 result
->contains_outer
|= sub_state
->contains_outer
;
1312 result
->sub_states
= lappend(result
->sub_states
, sub_state
);
1315 elog(ERROR
, "unrecognized node type: %d",
1316 (int) nodeTag(jtnode
));
1321 * reduce_outer_joins_pass2 - phase 2 processing
1323 * jtnode: current jointree node
1324 * state: state data collected by phase 1 for this node
1325 * root: toplevel planner state
1326 * nonnullable_rels: set of base relids forced non-null by upper quals
1327 * nonnullable_vars: list of Vars forced non-null by upper quals
1328 * forced_null_vars: list of Vars forced null by upper quals
1331 reduce_outer_joins_pass2(Node
*jtnode
,
1332 reduce_outer_joins_state
*state
,
1334 Relids nonnullable_rels
,
1335 List
*nonnullable_vars
,
1336 List
*forced_null_vars
)
1339 * pass 2 should never descend as far as an empty subnode or base rel,
1340 * because it's only called on subtrees marked as contains_outer.
1343 elog(ERROR
, "reached empty jointree");
1344 if (IsA(jtnode
, RangeTblRef
))
1345 elog(ERROR
, "reached base rel");
1346 else if (IsA(jtnode
, FromExpr
))
1348 FromExpr
*f
= (FromExpr
*) jtnode
;
1351 Relids pass_nonnullable_rels
;
1352 List
*pass_nonnullable_vars
;
1353 List
*pass_forced_null_vars
;
1355 /* Scan quals to see if we can add any constraints */
1356 pass_nonnullable_rels
= find_nonnullable_rels(f
->quals
);
1357 pass_nonnullable_rels
= bms_add_members(pass_nonnullable_rels
,
1359 /* NB: we rely on list_concat to not damage its second argument */
1360 pass_nonnullable_vars
= find_nonnullable_vars(f
->quals
);
1361 pass_nonnullable_vars
= list_concat(pass_nonnullable_vars
,
1363 pass_forced_null_vars
= find_forced_null_vars(f
->quals
);
1364 pass_forced_null_vars
= list_concat(pass_forced_null_vars
,
1366 /* And recurse --- but only into interesting subtrees */
1367 Assert(list_length(f
->fromlist
) == list_length(state
->sub_states
));
1368 forboth(l
, f
->fromlist
, s
, state
->sub_states
)
1370 reduce_outer_joins_state
*sub_state
= lfirst(s
);
1372 if (sub_state
->contains_outer
)
1373 reduce_outer_joins_pass2(lfirst(l
), sub_state
, root
,
1374 pass_nonnullable_rels
,
1375 pass_nonnullable_vars
,
1376 pass_forced_null_vars
);
1378 bms_free(pass_nonnullable_rels
);
1379 /* can't so easily clean up var lists, unfortunately */
1381 else if (IsA(jtnode
, JoinExpr
))
1383 JoinExpr
*j
= (JoinExpr
*) jtnode
;
1384 int rtindex
= j
->rtindex
;
1385 JoinType jointype
= j
->jointype
;
1386 reduce_outer_joins_state
*left_state
= linitial(state
->sub_states
);
1387 reduce_outer_joins_state
*right_state
= lsecond(state
->sub_states
);
1388 List
*local_nonnullable_vars
= NIL
;
1389 bool computed_local_nonnullable_vars
= false;
1391 /* Can we simplify this join? */
1397 if (bms_overlap(nonnullable_rels
, right_state
->relids
))
1398 jointype
= JOIN_INNER
;
1401 if (bms_overlap(nonnullable_rels
, left_state
->relids
))
1402 jointype
= JOIN_INNER
;
1405 if (bms_overlap(nonnullable_rels
, left_state
->relids
))
1407 if (bms_overlap(nonnullable_rels
, right_state
->relids
))
1408 jointype
= JOIN_INNER
;
1410 jointype
= JOIN_LEFT
;
1414 if (bms_overlap(nonnullable_rels
, right_state
->relids
))
1415 jointype
= JOIN_RIGHT
;
1419 elog(ERROR
, "unrecognized join type: %d",
1425 * Convert JOIN_RIGHT to JOIN_LEFT. Note that in the case where we
1426 * reduced JOIN_FULL to JOIN_RIGHT, this will mean the JoinExpr no
1427 * longer matches the internal ordering of any CoalesceExpr's built to
1428 * represent merged join variables. We don't care about that at
1429 * present, but be wary of it ...
1431 if (jointype
== JOIN_RIGHT
)
1438 jointype
= JOIN_LEFT
;
1439 right_state
= linitial(state
->sub_states
);
1440 left_state
= lsecond(state
->sub_states
);
1444 * See if we can reduce JOIN_LEFT to JOIN_ANTI. This is the case
1445 * if the join's own quals are strict for any var that was forced
1446 * null by higher qual levels. NOTE: there are other ways that we
1447 * could detect an anti-join, in particular if we were to check
1448 * whether Vars coming from the RHS must be non-null because of
1449 * table constraints. That seems complicated and expensive though
1450 * (in particular, one would have to be wary of lower outer joins).
1451 * For the moment this seems sufficient.
1453 if (jointype
== JOIN_LEFT
)
1457 local_nonnullable_vars
= find_nonnullable_vars(j
->quals
);
1458 computed_local_nonnullable_vars
= true;
1461 * It's not sufficient to check whether local_nonnullable_vars
1462 * and forced_null_vars overlap: we need to know if the overlap
1463 * includes any RHS variables.
1465 overlap
= list_intersection(local_nonnullable_vars
,
1467 if (overlap
!= NIL
&&
1468 bms_overlap(pull_varnos((Node
*) overlap
),
1469 right_state
->relids
))
1470 jointype
= JOIN_ANTI
;
1473 /* Apply the jointype change, if any, to both jointree node and RTE */
1474 if (jointype
!= j
->jointype
)
1476 RangeTblEntry
*rte
= rt_fetch(rtindex
, root
->parse
->rtable
);
1478 Assert(rte
->rtekind
== RTE_JOIN
);
1479 Assert(rte
->jointype
== j
->jointype
);
1480 rte
->jointype
= j
->jointype
= jointype
;
1483 /* Only recurse if there's more to do below here */
1484 if (left_state
->contains_outer
|| right_state
->contains_outer
)
1486 Relids local_nonnullable_rels
;
1487 List
*local_forced_null_vars
;
1488 Relids pass_nonnullable_rels
;
1489 List
*pass_nonnullable_vars
;
1490 List
*pass_forced_null_vars
;
1493 * If this join is (now) inner, we can add any constraints its
1494 * quals provide to those we got from above. But if it is outer,
1495 * we can pass down the local constraints only into the nullable
1496 * side, because an outer join never eliminates any rows from its
1497 * non-nullable side. Also, there is no point in passing upper
1498 * constraints into the nullable side, since if there were any
1499 * we'd have been able to reduce the join. (In the case of
1500 * upper forced-null constraints, we *must not* pass them into
1501 * the nullable side --- they either applied here, or not.)
1502 * The upshot is that we pass either the local or the upper
1503 * constraints, never both, to the children of an outer join.
1505 * At a FULL join we just punt and pass nothing down --- is it
1506 * possible to be smarter?
1508 if (jointype
!= JOIN_FULL
)
1510 local_nonnullable_rels
= find_nonnullable_rels(j
->quals
);
1511 if (!computed_local_nonnullable_vars
)
1512 local_nonnullable_vars
= find_nonnullable_vars(j
->quals
);
1513 local_forced_null_vars
= find_forced_null_vars(j
->quals
);
1514 if (jointype
== JOIN_INNER
)
1516 /* OK to merge upper and local constraints */
1517 local_nonnullable_rels
= bms_add_members(local_nonnullable_rels
,
1519 local_nonnullable_vars
= list_concat(local_nonnullable_vars
,
1521 local_forced_null_vars
= list_concat(local_forced_null_vars
,
1527 /* no use in calculating these */
1528 local_nonnullable_rels
= NULL
;
1529 local_forced_null_vars
= NIL
;
1532 if (left_state
->contains_outer
)
1534 if (jointype
== JOIN_INNER
)
1536 /* pass union of local and upper constraints */
1537 pass_nonnullable_rels
= local_nonnullable_rels
;
1538 pass_nonnullable_vars
= local_nonnullable_vars
;
1539 pass_forced_null_vars
= local_forced_null_vars
;
1541 else if (jointype
!= JOIN_FULL
) /* ie, LEFT or ANTI */
1543 /* can't pass local constraints to non-nullable side */
1544 pass_nonnullable_rels
= nonnullable_rels
;
1545 pass_nonnullable_vars
= nonnullable_vars
;
1546 pass_forced_null_vars
= forced_null_vars
;
1550 /* no constraints pass through JOIN_FULL */
1551 pass_nonnullable_rels
= NULL
;
1552 pass_nonnullable_vars
= NIL
;
1553 pass_forced_null_vars
= NIL
;
1555 reduce_outer_joins_pass2(j
->larg
, left_state
, root
,
1556 pass_nonnullable_rels
,
1557 pass_nonnullable_vars
,
1558 pass_forced_null_vars
);
1561 if (right_state
->contains_outer
)
1563 if (jointype
!= JOIN_FULL
) /* ie, INNER, LEFT or ANTI */
1565 /* pass appropriate constraints, per comment above */
1566 pass_nonnullable_rels
= local_nonnullable_rels
;
1567 pass_nonnullable_vars
= local_nonnullable_vars
;
1568 pass_forced_null_vars
= local_forced_null_vars
;
1572 /* no constraints pass through JOIN_FULL */
1573 pass_nonnullable_rels
= NULL
;
1574 pass_nonnullable_vars
= NIL
;
1575 pass_forced_null_vars
= NIL
;
1577 reduce_outer_joins_pass2(j
->rarg
, right_state
, root
,
1578 pass_nonnullable_rels
,
1579 pass_nonnullable_vars
,
1580 pass_forced_null_vars
);
1582 bms_free(local_nonnullable_rels
);
1586 elog(ERROR
, "unrecognized node type: %d",
1587 (int) nodeTag(jtnode
));
1591 * substitute_multiple_relids - adjust node relid sets after pulling up
1594 * Find any FlattenedSubLink or PlaceHolderVar nodes in the given tree that
1595 * reference the pulled-up relid, and change them to reference the replacement
1596 * relid(s). We do not need to recurse into subqueries, since no subquery of
1597 * the current top query could (yet) contain such a reference.
1599 * NOTE: although this has the form of a walker, we cheat and modify the
1600 * nodes in-place. This should be OK since the tree was copied by ResolveNew
1601 * earlier. Avoid scribbling on the original values of the bitmapsets, though,
1602 * because expression_tree_mutator doesn't copy those.
1609 } substitute_multiple_relids_context
;
1612 substitute_multiple_relids_walker(Node
*node
,
1613 substitute_multiple_relids_context
*context
)
1617 if (IsA(node
, FlattenedSubLink
))
1619 FlattenedSubLink
*fslink
= (FlattenedSubLink
*) node
;
1621 if (bms_is_member(context
->varno
, fslink
->lefthand
))
1623 fslink
->lefthand
= bms_union(fslink
->lefthand
,
1624 context
->subrelids
);
1625 fslink
->lefthand
= bms_del_member(fslink
->lefthand
,
1628 if (bms_is_member(context
->varno
, fslink
->righthand
))
1630 fslink
->righthand
= bms_union(fslink
->righthand
,
1631 context
->subrelids
);
1632 fslink
->righthand
= bms_del_member(fslink
->righthand
,
1635 /* fall through to examine children */
1637 if (IsA(node
, PlaceHolderVar
))
1639 PlaceHolderVar
*phv
= (PlaceHolderVar
*) node
;
1641 if (bms_is_member(context
->varno
, phv
->phrels
))
1643 phv
->phrels
= bms_union(phv
->phrels
,
1644 context
->subrelids
);
1645 phv
->phrels
= bms_del_member(phv
->phrels
,
1648 /* fall through to examine children */
1650 /* Shouldn't need to handle planner auxiliary nodes here */
1651 Assert(!IsA(node
, SpecialJoinInfo
));
1652 Assert(!IsA(node
, AppendRelInfo
));
1653 Assert(!IsA(node
, PlaceHolderInfo
));
1655 return expression_tree_walker(node
, substitute_multiple_relids_walker
,
1660 substitute_multiple_relids(Node
*node
, int varno
, Relids subrelids
)
1662 substitute_multiple_relids_context context
;
1664 context
.varno
= varno
;
1665 context
.subrelids
= subrelids
;
1668 * Must be prepared to start with a Query or a bare expression tree.
1670 query_or_expression_tree_walker(node
,
1671 substitute_multiple_relids_walker
,
1677 * fix_append_rel_relids: update RT-index fields of AppendRelInfo nodes
1679 * When we pull up a subquery, any AppendRelInfo references to the subquery's
1680 * RT index have to be replaced by the substituted relid (and there had better
1681 * be only one). We also need to apply substitute_multiple_relids to their
1682 * translated_vars lists, since those might contain PlaceHolderVars.
1684 * We assume we may modify the AppendRelInfo nodes in-place.
1687 fix_append_rel_relids(List
*append_rel_list
, int varno
, Relids subrelids
)
1693 * We only want to extract the member relid once, but we mustn't fail
1694 * immediately if there are multiple members; it could be that none of the
1695 * AppendRelInfo nodes refer to it. So compute it on first use. Note that
1696 * bms_singleton_member will complain if set is not singleton.
1698 foreach(l
, append_rel_list
)
1700 AppendRelInfo
*appinfo
= (AppendRelInfo
*) lfirst(l
);
1702 /* The parent_relid shouldn't ever be a pullup target */
1703 Assert(appinfo
->parent_relid
!= varno
);
1705 if (appinfo
->child_relid
== varno
)
1708 subvarno
= bms_singleton_member(subrelids
);
1709 appinfo
->child_relid
= subvarno
;
1712 /* Also finish fixups for its translated vars */
1713 substitute_multiple_relids((Node
*) appinfo
->translated_vars
,
1719 * get_relids_in_jointree: get set of RT indexes present in a jointree
1721 * If include_joins is true, join RT indexes are included; if false,
1722 * only base rels are included.
1725 get_relids_in_jointree(Node
*jtnode
, bool include_joins
)
1727 Relids result
= NULL
;
1731 if (IsA(jtnode
, RangeTblRef
))
1733 int varno
= ((RangeTblRef
*) jtnode
)->rtindex
;
1735 result
= bms_make_singleton(varno
);
1737 else if (IsA(jtnode
, FromExpr
))
1739 FromExpr
*f
= (FromExpr
*) jtnode
;
1742 foreach(l
, f
->fromlist
)
1744 result
= bms_join(result
,
1745 get_relids_in_jointree(lfirst(l
),
1749 else if (IsA(jtnode
, JoinExpr
))
1751 JoinExpr
*j
= (JoinExpr
*) jtnode
;
1753 result
= get_relids_in_jointree(j
->larg
, include_joins
);
1754 result
= bms_join(result
,
1755 get_relids_in_jointree(j
->rarg
, include_joins
));
1757 result
= bms_add_member(result
, j
->rtindex
);
1760 elog(ERROR
, "unrecognized node type: %d",
1761 (int) nodeTag(jtnode
));
1766 * get_relids_for_join: get set of base RT indexes making up a join
1769 get_relids_for_join(PlannerInfo
*root
, int joinrelid
)
1773 jtnode
= find_jointree_node_for_rel((Node
*) root
->parse
->jointree
,
1776 elog(ERROR
, "could not find join node %d", joinrelid
);
1777 return get_relids_in_jointree(jtnode
, false);
1781 * find_jointree_node_for_rel: locate jointree node for a base or join RT index
1783 * Returns NULL if not found
1786 find_jointree_node_for_rel(Node
*jtnode
, int relid
)
1790 if (IsA(jtnode
, RangeTblRef
))
1792 int varno
= ((RangeTblRef
*) jtnode
)->rtindex
;
1797 else if (IsA(jtnode
, FromExpr
))
1799 FromExpr
*f
= (FromExpr
*) jtnode
;
1802 foreach(l
, f
->fromlist
)
1804 jtnode
= find_jointree_node_for_rel(lfirst(l
), relid
);
1809 else if (IsA(jtnode
, JoinExpr
))
1811 JoinExpr
*j
= (JoinExpr
*) jtnode
;
1813 if (relid
== j
->rtindex
)
1815 jtnode
= find_jointree_node_for_rel(j
->larg
, relid
);
1818 jtnode
= find_jointree_node_for_rel(j
->rarg
, relid
);
1823 elog(ERROR
, "unrecognized node type: %d",
1824 (int) nodeTag(jtnode
));