1 /*-------------------------------------------------------------------------
4 * Target list, qualification, joininfo initialization routines
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/optimizer/plan/initsplan.c,v 1.141 2008/08/14 18:47:59 tgl Exp $
13 *-------------------------------------------------------------------------
17 #include "catalog/pg_operator.h"
18 #include "catalog/pg_type.h"
19 #include "optimizer/clauses.h"
20 #include "optimizer/cost.h"
21 #include "optimizer/joininfo.h"
22 #include "optimizer/pathnode.h"
23 #include "optimizer/paths.h"
24 #include "optimizer/planmain.h"
25 #include "optimizer/prep.h"
26 #include "optimizer/restrictinfo.h"
27 #include "optimizer/var.h"
28 #include "parser/parse_expr.h"
29 #include "parser/parse_oper.h"
30 #include "utils/builtins.h"
31 #include "utils/lsyscache.h"
32 #include "utils/syscache.h"
35 /* These parameters are set by GUC */
36 int from_collapse_limit;
37 int join_collapse_limit;
40 static List *deconstruct_recurse(PlannerInfo *root, Node *jtnode,
41 bool below_outer_join,
42 Relids *qualscope, Relids *inner_join_rels);
43 static SpecialJoinInfo *make_outerjoininfo(PlannerInfo *root,
44 Relids left_rels, Relids right_rels,
45 Relids inner_join_rels,
46 JoinType jointype, List *clause);
47 static void distribute_qual_to_rels(PlannerInfo *root, Node *clause,
49 bool below_outer_join,
52 Relids outerjoin_nonnullable);
53 static void distribute_sublink_quals_to_rels(PlannerInfo *root,
54 FlattenedSubLink *fslink,
55 bool below_outer_join);
56 static bool check_outerjoin_delay(PlannerInfo *root, Relids *relids_p,
58 static bool check_redundant_nullability_qual(PlannerInfo *root, Node *clause);
59 static void check_mergejoinable(RestrictInfo *restrictinfo);
60 static void check_hashjoinable(RestrictInfo *restrictinfo);
63 /*****************************************************************************
67 *****************************************************************************/
70 * add_base_rels_to_query
72 * Scan the query's jointree and create baserel RelOptInfos for all
73 * the base relations (ie, table, subquery, and function RTEs)
74 * appearing in the jointree.
76 * The initial invocation must pass root->parse->jointree as the value of
77 * jtnode. Internally, the function recurses through the jointree.
79 * At the end of this process, there should be one baserel RelOptInfo for
80 * every non-join RTE that is used in the query. Therefore, this routine
81 * is the only place that should call build_simple_rel with reloptkind
82 * RELOPT_BASEREL. (Note: build_simple_rel recurses internally to build
83 * "other rel" RelOptInfos for the members of any appendrels we find here.)
86 add_base_rels_to_query(PlannerInfo *root, Node *jtnode)
90 if (IsA(jtnode, RangeTblRef))
92 int varno = ((RangeTblRef *) jtnode)->rtindex;
94 (void) build_simple_rel(root, varno, RELOPT_BASEREL);
96 else if (IsA(jtnode, FromExpr))
98 FromExpr *f = (FromExpr *) jtnode;
101 foreach(l, f->fromlist)
102 add_base_rels_to_query(root, lfirst(l));
104 else if (IsA(jtnode, JoinExpr))
106 JoinExpr *j = (JoinExpr *) jtnode;
108 add_base_rels_to_query(root, j->larg);
109 add_base_rels_to_query(root, j->rarg);
112 elog(ERROR, "unrecognized node type: %d",
113 (int) nodeTag(jtnode));
117 /*****************************************************************************
121 *****************************************************************************/
124 * build_base_rel_tlists
125 * Add targetlist entries for each var needed in the query's final tlist
126 * to the appropriate base relations.
128 * We mark such vars as needed by "relation 0" to ensure that they will
129 * propagate up through all join plan steps.
132 build_base_rel_tlists(PlannerInfo *root, List *final_tlist)
134 List *tlist_vars = pull_var_clause((Node *) final_tlist, false);
136 if (tlist_vars != NIL)
138 add_vars_to_targetlist(root, tlist_vars, bms_make_singleton(0));
139 list_free(tlist_vars);
144 * add_vars_to_targetlist
145 * For each variable appearing in the list, add it to the owning
146 * relation's targetlist if not already present, and mark the variable
147 * as being needed for the indicated join (or for final output if
148 * where_needed includes "relation 0").
151 add_vars_to_targetlist(PlannerInfo *root, List *vars, Relids where_needed)
155 Assert(!bms_is_empty(where_needed));
159 Var *var = (Var *) lfirst(temp);
160 RelOptInfo *rel = find_base_rel(root, var->varno);
161 int attrno = var->varattno;
163 Assert(attrno >= rel->min_attr && attrno <= rel->max_attr);
164 attrno -= rel->min_attr;
165 if (bms_is_empty(rel->attr_needed[attrno]))
167 /* Variable not yet requested, so add to reltargetlist */
168 /* XXX is copyObject necessary here? */
169 rel->reltargetlist = lappend(rel->reltargetlist, copyObject(var));
171 rel->attr_needed[attrno] = bms_add_members(rel->attr_needed[attrno],
177 /*****************************************************************************
179 * JOIN TREE PROCESSING
181 *****************************************************************************/
184 * deconstruct_jointree
185 * Recursively scan the query's join tree for WHERE and JOIN/ON qual
186 * clauses, and add these to the appropriate restrictinfo and joininfo
187 * lists belonging to base RelOptInfos. Also, add SpecialJoinInfo nodes
188 * to root->join_info_list for any outer joins appearing in the query tree.
189 * Return a "joinlist" data structure showing the join order decisions
190 * that need to be made by make_one_rel().
192 * The "joinlist" result is a list of items that are either RangeTblRef
193 * jointree nodes or sub-joinlists. All the items at the same level of
194 * joinlist must be joined in an order to be determined by make_one_rel()
195 * (note that legal orders may be constrained by SpecialJoinInfo nodes).
196 * A sub-joinlist represents a subproblem to be planned separately. Currently
197 * sub-joinlists arise only from FULL OUTER JOIN or when collapsing of
198 * subproblems is stopped by join_collapse_limit or from_collapse_limit.
200 * NOTE: when dealing with inner joins, it is appropriate to let a qual clause
201 * be evaluated at the lowest level where all the variables it mentions are
202 * available. However, we cannot push a qual down into the nullable side(s)
203 * of an outer join since the qual might eliminate matching rows and cause a
204 * NULL row to be incorrectly emitted by the join. Therefore, we artificially
205 * OR the minimum-relids of such an outer join into the required_relids of
206 * clauses appearing above it. This forces those clauses to be delayed until
207 * application of the outer join (or maybe even higher in the join tree).
210 deconstruct_jointree(PlannerInfo *root)
213 Relids inner_join_rels;
215 /* Start recursion at top of jointree */
216 Assert(root->parse->jointree != NULL &&
217 IsA(root->parse->jointree, FromExpr));
219 return deconstruct_recurse(root, (Node *) root->parse->jointree, false,
220 &qualscope, &inner_join_rels);
224 * deconstruct_recurse
225 * One recursion level of deconstruct_jointree processing.
228 * jtnode is the jointree node to examine
229 * below_outer_join is TRUE if this node is within the nullable side of a
230 * higher-level outer join
232 * *qualscope gets the set of base Relids syntactically included in this
233 * jointree node (do not modify or free this, as it may also be pointed
234 * to by RestrictInfo and SpecialJoinInfo nodes)
235 * *inner_join_rels gets the set of base Relids syntactically included in
236 * inner joins appearing at or below this jointree node (do not modify
237 * or free this, either)
238 * Return value is the appropriate joinlist for this jointree node
240 * In addition, entries will be added to root->join_info_list for outer joins.
243 deconstruct_recurse(PlannerInfo *root, Node *jtnode, bool below_outer_join,
244 Relids *qualscope, Relids *inner_join_rels)
251 *inner_join_rels = NULL;
254 if (IsA(jtnode, RangeTblRef))
256 int varno = ((RangeTblRef *) jtnode)->rtindex;
258 /* No quals to deal with, just return correct result */
259 *qualscope = bms_make_singleton(varno);
260 /* A single baserel does not create an inner join */
261 *inner_join_rels = NULL;
262 joinlist = list_make1(jtnode);
264 else if (IsA(jtnode, FromExpr))
266 FromExpr *f = (FromExpr *) jtnode;
271 * First, recurse to handle child joins. We collapse subproblems into
272 * a single joinlist whenever the resulting joinlist wouldn't exceed
273 * from_collapse_limit members. Also, always collapse one-element
274 * subproblems, since that won't lengthen the joinlist anyway.
277 *inner_join_rels = NULL;
279 remaining = list_length(f->fromlist);
280 foreach(l, f->fromlist)
282 Relids sub_qualscope;
286 sub_joinlist = deconstruct_recurse(root, lfirst(l),
290 *qualscope = bms_add_members(*qualscope, sub_qualscope);
291 sub_members = list_length(sub_joinlist);
293 if (sub_members <= 1 ||
294 list_length(joinlist) + sub_members + remaining <= from_collapse_limit)
295 joinlist = list_concat(joinlist, sub_joinlist);
297 joinlist = lappend(joinlist, sub_joinlist);
301 * A FROM with more than one list element is an inner join subsuming
302 * all below it, so we should report inner_join_rels = qualscope. If
303 * there was exactly one element, we should (and already did) report
304 * whatever its inner_join_rels were. If there were no elements (is
305 * that possible?) the initialization before the loop fixed it.
307 if (list_length(f->fromlist) > 1)
308 *inner_join_rels = *qualscope;
311 * Now process the top-level quals.
313 foreach(l, (List *) f->quals)
315 Node *qual = (Node *) lfirst(l);
317 /* FlattenedSubLink wrappers need special processing */
318 if (qual && IsA(qual, FlattenedSubLink))
319 distribute_sublink_quals_to_rels(root,
320 (FlattenedSubLink *) qual,
323 distribute_qual_to_rels(root, qual,
324 false, below_outer_join,
325 *qualscope, NULL, NULL);
328 else if (IsA(jtnode, JoinExpr))
330 JoinExpr *j = (JoinExpr *) jtnode;
339 SpecialJoinInfo *sjinfo;
343 * Order of operations here is subtle and critical. First we recurse
344 * to handle sub-JOINs. Their join quals will be placed without
345 * regard for whether this level is an outer join, which is correct.
346 * Then we place our own join quals, which are restricted by lower
347 * outer joins in any case, and are forced to this level if this is an
348 * outer join and they mention the outer side. Finally, if this is an
349 * outer join, we create a join_info_list entry for the join. This
350 * will prevent quals above us in the join tree that use those rels
351 * from being pushed down below this level. (It's okay for upper
352 * quals to be pushed down to the outer side, however.)
357 leftjoinlist = deconstruct_recurse(root, j->larg,
359 &leftids, &left_inners);
360 rightjoinlist = deconstruct_recurse(root, j->rarg,
362 &rightids, &right_inners);
363 *qualscope = bms_union(leftids, rightids);
364 *inner_join_rels = *qualscope;
365 /* Inner join adds no restrictions for quals */
366 nonnullable_rels = NULL;
370 leftjoinlist = deconstruct_recurse(root, j->larg,
372 &leftids, &left_inners);
373 rightjoinlist = deconstruct_recurse(root, j->rarg,
375 &rightids, &right_inners);
376 *qualscope = bms_union(leftids, rightids);
377 *inner_join_rels = bms_union(left_inners, right_inners);
378 nonnullable_rels = leftids;
381 leftjoinlist = deconstruct_recurse(root, j->larg,
383 &leftids, &left_inners);
384 rightjoinlist = deconstruct_recurse(root, j->rarg,
386 &rightids, &right_inners);
387 *qualscope = bms_union(leftids, rightids);
388 *inner_join_rels = bms_union(left_inners, right_inners);
389 /* each side is both outer and inner */
390 nonnullable_rels = *qualscope;
393 /* JOIN_RIGHT was eliminated during reduce_outer_joins() */
394 elog(ERROR, "unrecognized join type: %d",
396 nonnullable_rels = NULL; /* keep compiler quiet */
397 leftjoinlist = rightjoinlist = NIL;
402 * For an OJ, form the SpecialJoinInfo now, because we need the OJ's
403 * semantic scope (ojscope) to pass to distribute_qual_to_rels. But
404 * we mustn't add it to join_info_list just yet, because we don't want
405 * distribute_qual_to_rels to think it is an outer join below us.
407 if (j->jointype != JOIN_INNER)
409 sjinfo = make_outerjoininfo(root,
414 ojscope = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
422 /* Process the qual clauses */
423 foreach(l, (List *) j->quals)
425 Node *qual = (Node *) lfirst(l);
427 /* FlattenedSubLink wrappers need special processing */
428 if (qual && IsA(qual, FlattenedSubLink))
429 distribute_sublink_quals_to_rels(root,
430 (FlattenedSubLink *) qual,
433 distribute_qual_to_rels(root, qual,
434 false, below_outer_join,
436 ojscope, nonnullable_rels);
439 /* Now we can add the SpecialJoinInfo to join_info_list */
441 root->join_info_list = lappend(root->join_info_list, sjinfo);
444 * Finally, compute the output joinlist. We fold subproblems together
445 * except at a FULL JOIN or where join_collapse_limit would be
448 if (j->jointype == JOIN_FULL)
450 /* force the join order exactly at this node */
451 joinlist = list_make1(list_make2(leftjoinlist, rightjoinlist));
453 else if (list_length(leftjoinlist) + list_length(rightjoinlist) <=
456 /* OK to combine subproblems */
457 joinlist = list_concat(leftjoinlist, rightjoinlist);
461 /* can't combine, but needn't force join order above here */
465 /* avoid creating useless 1-element sublists */
466 if (list_length(leftjoinlist) == 1)
467 leftpart = (Node *) linitial(leftjoinlist);
469 leftpart = (Node *) leftjoinlist;
470 if (list_length(rightjoinlist) == 1)
471 rightpart = (Node *) linitial(rightjoinlist);
473 rightpart = (Node *) rightjoinlist;
474 joinlist = list_make2(leftpart, rightpart);
479 elog(ERROR, "unrecognized node type: %d",
480 (int) nodeTag(jtnode));
481 joinlist = NIL; /* keep compiler quiet */
488 * Build a SpecialJoinInfo for the current outer join
491 * left_rels: the base Relids syntactically on outer side of join
492 * right_rels: the base Relids syntactically on inner side of join
493 * inner_join_rels: base Relids participating in inner joins below this one
494 * jointype: what it says (must always be LEFT, FULL, SEMI, or ANTI)
495 * clause: the outer join's join condition (in implicit-AND format)
497 * The node should eventually be appended to root->join_info_list, but we
498 * do not do that here.
500 * Note: we assume that this function is invoked bottom-up, so that
501 * root->join_info_list already contains entries for all outer joins that are
502 * syntactically below this one.
504 static SpecialJoinInfo *
505 make_outerjoininfo(PlannerInfo *root,
506 Relids left_rels, Relids right_rels,
507 Relids inner_join_rels,
508 JoinType jointype, List *clause)
510 SpecialJoinInfo *sjinfo = makeNode(SpecialJoinInfo);
511 Relids clause_relids;
512 Relids strict_relids;
514 Relids min_righthand;
518 * We should not see RIGHT JOIN here because left/right were switched
521 Assert(jointype != JOIN_INNER);
522 Assert(jointype != JOIN_RIGHT);
525 * Presently the executor cannot support FOR UPDATE/SHARE marking of rels
526 * appearing on the nullable side of an outer join. (It's somewhat unclear
527 * what that would mean, anyway: what should we mark when a result row is
528 * generated from no element of the nullable relation?) So, complain if
529 * any nullable rel is FOR UPDATE/SHARE.
531 * You might be wondering why this test isn't made far upstream in the
532 * parser. It's because the parser hasn't got enough info --- consider
533 * FOR UPDATE applied to a view. Only after rewriting and flattening do
534 * we know whether the view contains an outer join.
536 foreach(l, root->parse->rowMarks)
538 RowMarkClause *rc = (RowMarkClause *) lfirst(l);
540 if (bms_is_member(rc->rti, right_rels) ||
541 (jointype == JOIN_FULL && bms_is_member(rc->rti, left_rels)))
543 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
544 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to the nullable side of an outer join")));
547 sjinfo->syn_lefthand = left_rels;
548 sjinfo->syn_righthand = right_rels;
549 sjinfo->jointype = jointype;
550 /* this always starts out false */
551 sjinfo->delay_upper_joins = false;
552 sjinfo->join_quals = clause;
554 /* If it's a full join, no need to be very smart */
555 if (jointype == JOIN_FULL)
557 sjinfo->min_lefthand = bms_copy(left_rels);
558 sjinfo->min_righthand = bms_copy(right_rels);
559 sjinfo->lhs_strict = false; /* don't care about this */
564 * Retrieve all relids mentioned within the join clause.
566 clause_relids = pull_varnos((Node *) clause);
569 * For which relids is the clause strict, ie, it cannot succeed if the
570 * rel's columns are all NULL?
572 strict_relids = find_nonnullable_rels((Node *) clause);
574 /* Remember whether the clause is strict for any LHS relations */
575 sjinfo->lhs_strict = bms_overlap(strict_relids, left_rels);
578 * Required LHS always includes the LHS rels mentioned in the clause. We
579 * may have to add more rels based on lower outer joins; see below.
581 min_lefthand = bms_intersect(clause_relids, left_rels);
584 * Similarly for required RHS. But here, we must also include any lower
585 * inner joins, to ensure we don't try to commute with any of them.
587 min_righthand = bms_int_members(bms_union(clause_relids, inner_join_rels),
590 foreach(l, root->join_info_list)
592 SpecialJoinInfo *otherinfo = (SpecialJoinInfo *) lfirst(l);
594 /* ignore full joins --- other mechanisms preserve their ordering */
595 if (otherinfo->jointype == JOIN_FULL)
599 * For a lower OJ in our LHS, if our join condition uses the lower
600 * join's RHS and is not strict for that rel, we must preserve the
601 * ordering of the two OJs, so add lower OJ's full syntactic relset to
602 * min_lefthand. (We must use its full syntactic relset, not just its
603 * min_lefthand + min_righthand. This is because there might be other
604 * OJs below this one that this one can commute with, but we cannot
605 * commute with them if we don't with this one.)
607 * Note: I believe we have to insist on being strict for at least one
608 * rel in the lower OJ's min_righthand, not its whole syn_righthand.
610 if (bms_overlap(left_rels, otherinfo->syn_righthand) &&
611 bms_overlap(clause_relids, otherinfo->syn_righthand) &&
612 !bms_overlap(strict_relids, otherinfo->min_righthand))
614 min_lefthand = bms_add_members(min_lefthand,
615 otherinfo->syn_lefthand);
616 min_lefthand = bms_add_members(min_lefthand,
617 otherinfo->syn_righthand);
621 * For a lower OJ in our RHS, if our join condition does not use the
622 * lower join's RHS and the lower OJ's join condition is strict, we
623 * can interchange the ordering of the two OJs; otherwise we must add
624 * lower OJ's full syntactic relset to min_righthand.
626 * Here, we have to consider that "our join condition" includes any
627 * clauses that syntactically appeared above the lower OJ and below
628 * ours; those are equivalent to degenerate clauses in our OJ and must
629 * be treated as such. Such clauses obviously can't reference our
630 * LHS, and they must be non-strict for the lower OJ's RHS (else
631 * reduce_outer_joins would have reduced the lower OJ to a plain
632 * join). Hence the other ways in which we handle clauses within our
633 * join condition are not affected by them. The net effect is
634 * therefore sufficiently represented by the delay_upper_joins flag
635 * saved for us by check_outerjoin_delay.
637 if (bms_overlap(right_rels, otherinfo->syn_righthand))
639 if (bms_overlap(clause_relids, otherinfo->syn_righthand) ||
640 !otherinfo->lhs_strict || otherinfo->delay_upper_joins)
642 min_righthand = bms_add_members(min_righthand,
643 otherinfo->syn_lefthand);
644 min_righthand = bms_add_members(min_righthand,
645 otherinfo->syn_righthand);
651 * If we found nothing to put in min_lefthand, punt and make it the full
652 * LHS, to avoid having an empty min_lefthand which will confuse later
653 * processing. (We don't try to be smart about such cases, just correct.)
654 * Likewise for min_righthand.
656 if (bms_is_empty(min_lefthand))
657 min_lefthand = bms_copy(left_rels);
658 if (bms_is_empty(min_righthand))
659 min_righthand = bms_copy(right_rels);
661 /* Now they'd better be nonempty */
662 Assert(!bms_is_empty(min_lefthand));
663 Assert(!bms_is_empty(min_righthand));
664 /* Shouldn't overlap either */
665 Assert(!bms_overlap(min_lefthand, min_righthand));
667 sjinfo->min_lefthand = min_lefthand;
668 sjinfo->min_righthand = min_righthand;
674 /*****************************************************************************
678 *****************************************************************************/
681 * distribute_qual_to_rels
682 * Add clause information to either the baserestrictinfo or joininfo list
683 * (depending on whether the clause is a join) of each base relation
684 * mentioned in the clause. A RestrictInfo node is created and added to
685 * the appropriate list for each rel. Alternatively, if the clause uses a
686 * mergejoinable operator and is not delayed by outer-join rules, enter
687 * the left- and right-side expressions into the query's list of
688 * EquivalenceClasses.
690 * 'clause': the qual clause to be distributed
691 * 'is_deduced': TRUE if the qual came from implied-equality deduction
692 * 'below_outer_join': TRUE if the qual is from a JOIN/ON that is below the
693 * nullable side of a higher-level outer join
694 * 'qualscope': set of baserels the qual's syntactic scope covers
695 * 'ojscope': NULL if not an outer-join qual, else the minimum set of baserels
696 * needed to form this join
697 * 'outerjoin_nonnullable': NULL if not an outer-join qual, else the set of
698 * baserels appearing on the outer (nonnullable) side of the join
699 * (for FULL JOIN this includes both sides of the join, and must in fact
702 * 'qualscope' identifies what level of JOIN the qual came from syntactically.
703 * 'ojscope' is needed if we decide to force the qual up to the outer-join
704 * level, which will be ojscope not necessarily qualscope.
707 distribute_qual_to_rels(PlannerInfo *root, Node *clause,
709 bool below_outer_join,
712 Relids outerjoin_nonnullable)
716 bool outerjoin_delayed;
717 bool pseudoconstant = false;
718 bool maybe_equivalence;
719 bool maybe_outer_join;
720 RestrictInfo *restrictinfo;
723 * Retrieve all relids mentioned within the clause.
725 relids = pull_varnos(clause);
728 * Cross-check: clause should contain no relids not within its scope.
729 * Otherwise the parser messed up.
731 if (!bms_is_subset(relids, qualscope))
732 elog(ERROR, "JOIN qualification cannot refer to other relations");
733 if (ojscope && !bms_is_subset(relids, ojscope))
734 elog(ERROR, "JOIN qualification cannot refer to other relations");
737 * If the clause is variable-free, our normal heuristic for pushing it
738 * down to just the mentioned rels doesn't work, because there are none.
740 * If the clause is an outer-join clause, we must force it to the OJ's
741 * semantic level to preserve semantics.
743 * Otherwise, when the clause contains volatile functions, we force it to
744 * be evaluated at its original syntactic level. This preserves the
745 * expected semantics.
747 * When the clause contains no volatile functions either, it is actually a
748 * pseudoconstant clause that will not change value during any one
749 * execution of the plan, and hence can be used as a one-time qual in a
750 * gating Result plan node. We put such a clause into the regular
751 * RestrictInfo lists for the moment, but eventually createplan.c will
752 * pull it out and make a gating Result node immediately above whatever
753 * plan node the pseudoconstant clause is assigned to. It's usually best
754 * to put a gating node as high in the plan tree as possible. If we are
755 * not below an outer join, we can actually push the pseudoconstant qual
756 * all the way to the top of the tree. If we are below an outer join, we
757 * leave the qual at its original syntactic level (we could push it up to
758 * just below the outer join, but that seems more complex than it's
761 if (bms_is_empty(relids))
765 /* clause is attached to outer join, eval it there */
766 relids = bms_copy(ojscope);
767 /* mustn't use as gating qual, so don't mark pseudoconstant */
771 /* eval at original syntactic level */
772 relids = bms_copy(qualscope);
773 if (!contain_volatile_functions(clause))
775 /* mark as gating qual */
776 pseudoconstant = true;
777 /* tell createplan.c to check for gating quals */
778 root->hasPseudoConstantQuals = true;
779 /* if not below outer join, push it to top of tree */
780 if (!below_outer_join)
781 relids = get_relids_in_jointree((Node *) root->parse->jointree);
787 * Check to see if clause application must be delayed by outer-join
790 * A word about is_pushed_down: we mark the qual as "pushed down" if
791 * it is (potentially) applicable at a level different from its original
792 * syntactic level. This flag is used to distinguish OUTER JOIN ON quals
793 * from other quals pushed down to the same joinrel. The rules are:
794 * WHERE quals and INNER JOIN quals: is_pushed_down = true.
795 * Non-degenerate OUTER JOIN quals: is_pushed_down = false.
796 * Degenerate OUTER JOIN quals: is_pushed_down = true.
797 * A "degenerate" OUTER JOIN qual is one that doesn't mention the
798 * non-nullable side, and hence can be pushed down into the nullable side
799 * without changing the join result. It is correct to treat it as a
800 * regular filter condition at the level where it is evaluated.
802 * Note: it is not immediately obvious that a simple boolean is enough
803 * for this: if for some reason we were to attach a degenerate qual to
804 * its original join level, it would need to be treated as an outer join
805 * qual there. However, this cannot happen, because all the rels the
806 * clause mentions must be in the outer join's min_righthand, therefore
807 * the join it needs must be formed before the outer join; and we always
808 * attach quals to the lowest level where they can be evaluated. But
809 * if we were ever to re-introduce a mechanism for delaying evaluation
810 * of "expensive" quals, this area would need work.
816 * If the qual came from implied-equality deduction, it should not be
817 * outerjoin-delayed, else deducer blew it. But we can't check this
818 * because the join_info_list may now contain OJs above where the qual
822 is_pushed_down = true;
823 outerjoin_delayed = false;
824 /* Don't feed it back for more deductions */
825 maybe_equivalence = false;
826 maybe_outer_join = false;
828 else if (bms_overlap(relids, outerjoin_nonnullable))
831 * The qual is attached to an outer join and mentions (some of the)
832 * rels on the nonnullable side, so it's not degenerate.
834 * We can't use such a clause to deduce equivalence (the left and
835 * right sides might be unequal above the join because one of them has
836 * gone to NULL) ... but we might be able to use it for more limited
837 * deductions, if it is mergejoinable. So consider adding it to the
838 * lists of set-aside outer-join clauses.
840 is_pushed_down = false;
841 maybe_equivalence = false;
842 maybe_outer_join = true;
844 /* Check to see if must be delayed by lower outer join */
845 outerjoin_delayed = check_outerjoin_delay(root, &relids, false);
848 * Now force the qual to be evaluated exactly at the level of joining
849 * corresponding to the outer join. We cannot let it get pushed down
850 * into the nonnullable side, since then we'd produce no output rows,
851 * rather than the intended single null-extended row, for any
852 * nonnullable-side rows failing the qual.
854 * (Do this step after calling check_outerjoin_delay, because that
859 Assert(!pseudoconstant);
864 * Normal qual clause or degenerate outer-join clause. Either way, we
865 * can mark it as pushed-down.
867 is_pushed_down = true;
869 /* Check to see if must be delayed by lower outer join */
870 outerjoin_delayed = check_outerjoin_delay(root, &relids, true);
872 if (outerjoin_delayed)
874 /* Should still be a subset of current scope ... */
875 Assert(bms_is_subset(relids, qualscope));
878 * Because application of the qual will be delayed by outer join,
879 * we mustn't assume its vars are equal everywhere.
881 maybe_equivalence = false;
884 * It's possible that this is an IS NULL clause that's redundant
885 * with a lower antijoin; if so we can just discard it. We need
886 * not test in any of the other cases, because this will only
887 * be possible for pushed-down, delayed clauses.
889 if (check_redundant_nullability_qual(root, clause))
895 * Qual is not delayed by any lower outer-join restriction, so we
896 * can consider feeding it to the equivalence machinery. However,
897 * if it's itself within an outer-join clause, treat it as though
898 * it appeared below that outer join (note that we can only get
899 * here when the clause references only nullable-side rels).
901 maybe_equivalence = true;
902 if (outerjoin_nonnullable != NULL)
903 below_outer_join = true;
907 * Since it doesn't mention the LHS, it's certainly not useful as a
908 * set-aside OJ clause, even if it's in an OJ.
910 maybe_outer_join = false;
914 * Build the RestrictInfo node itself.
916 restrictinfo = make_restrictinfo((Expr *) clause,
923 * If it's a join clause (either naturally, or because delayed by
924 * outer-join rules), add vars used in the clause to targetlists of their
925 * relations, so that they will be emitted by the plan nodes that scan
926 * those relations (else they won't be available at the join node!).
928 * Note: if the clause gets absorbed into an EquivalenceClass then this
929 * may be unnecessary, but for now we have to do it to cover the case
930 * where the EC becomes ec_broken and we end up reinserting the original
931 * clauses into the plan.
933 if (bms_membership(relids) == BMS_MULTIPLE)
935 List *vars = pull_var_clause(clause, false);
937 add_vars_to_targetlist(root, vars, relids);
942 * We check "mergejoinability" of every clause, not only join clauses,
943 * because we want to know about equivalences between vars of the same
944 * relation, or between vars and consts.
946 check_mergejoinable(restrictinfo);
949 * If it is a true equivalence clause, send it to the EquivalenceClass
950 * machinery. We do *not* attach it directly to any restriction or join
951 * lists. The EC code will propagate it to the appropriate places later.
953 * If the clause has a mergejoinable operator and is not
954 * outerjoin-delayed, yet isn't an equivalence because it is an outer-join
955 * clause, the EC code may yet be able to do something with it. We add it
956 * to appropriate lists for further consideration later. Specifically:
958 * If it is a left or right outer-join qualification that relates the two
959 * sides of the outer join (no funny business like leftvar1 = leftvar2 +
960 * rightvar), we add it to root->left_join_clauses or
961 * root->right_join_clauses according to which side the nonnullable
962 * variable appears on.
964 * If it is a full outer-join qualification, we add it to
965 * root->full_join_clauses. (Ideally we'd discard cases that aren't
966 * leftvar = rightvar, as we do for left/right joins, but this routine
967 * doesn't have the info needed to do that; and the current usage of the
968 * full_join_clauses list doesn't require that, so it's not currently
969 * worth complicating this routine's API to make it possible.)
971 * If none of the above hold, pass it off to
972 * distribute_restrictinfo_to_rels().
974 if (restrictinfo->mergeopfamilies)
976 if (maybe_equivalence)
978 if (process_equivalence(root, restrictinfo, below_outer_join))
980 /* EC rejected it, so pass to distribute_restrictinfo_to_rels */
982 else if (maybe_outer_join && restrictinfo->can_join)
984 if (bms_is_subset(restrictinfo->left_relids,
985 outerjoin_nonnullable) &&
986 !bms_overlap(restrictinfo->right_relids,
987 outerjoin_nonnullable))
989 /* we have outervar = innervar */
990 root->left_join_clauses = lappend(root->left_join_clauses,
994 if (bms_is_subset(restrictinfo->right_relids,
995 outerjoin_nonnullable) &&
996 !bms_overlap(restrictinfo->left_relids,
997 outerjoin_nonnullable))
999 /* we have innervar = outervar */
1000 root->right_join_clauses = lappend(root->right_join_clauses,
1004 if (bms_equal(outerjoin_nonnullable, qualscope))
1006 /* FULL JOIN (above tests cannot match in this case) */
1007 root->full_join_clauses = lappend(root->full_join_clauses,
1014 /* No EC special case applies, so push it into the clause lists */
1015 distribute_restrictinfo_to_rels(root, restrictinfo);
1019 * distribute_sublink_quals_to_rels
1020 * Pull sublink quals out of a FlattenedSubLink node and distribute
1021 * them appropriately; then add a SpecialJoinInfo node to the query's
1022 * join_info_list. The FlattenedSubLink node itself is no longer
1023 * needed and does not propagate into further processing.
1026 distribute_sublink_quals_to_rels(PlannerInfo *root,
1027 FlattenedSubLink *fslink,
1028 bool below_outer_join)
1030 List *quals = make_ands_implicit(fslink->quals);
1031 SpecialJoinInfo *sjinfo;
1037 * Build a suitable SpecialJoinInfo for the sublink. Note: using
1038 * righthand as inner_join_rels is the conservative worst case;
1039 * it might be possible to use a smaller set and thereby allow
1040 * the sublink join to commute with others inside its RHS.
1042 sjinfo = make_outerjoininfo(root,
1043 fslink->lefthand, fslink->righthand,
1048 qualscope = bms_union(sjinfo->syn_lefthand, sjinfo->syn_righthand);
1049 ojscope = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
1051 /* Distribute the join quals much as for a regular LEFT JOIN */
1054 Node *qual = (Node *) lfirst(l);
1056 distribute_qual_to_rels(root, qual,
1057 false, below_outer_join,
1062 /* Now we can add the SpecialJoinInfo to join_info_list */
1063 root->join_info_list = lappend(root->join_info_list, sjinfo);
1067 * check_outerjoin_delay
1068 * Detect whether a qual referencing the given relids must be delayed
1069 * in application due to the presence of a lower outer join, and/or
1070 * may force extra delay of higher-level outer joins.
1072 * If the qual must be delayed, add relids to *relids_p to reflect the lowest
1073 * safe level for evaluating the qual, and return TRUE. Any extra delay for
1074 * higher-level joins is reflected by setting delay_upper_joins to TRUE in
1075 * SpecialJoinInfo structs.
1077 * For an is_pushed_down qual, we can evaluate the qual as soon as (1) we have
1078 * all the rels it mentions, and (2) we are at or above any outer joins that
1079 * can null any of these rels and are below the syntactic location of the
1080 * given qual. We must enforce (2) because pushing down such a clause below
1081 * the OJ might cause the OJ to emit null-extended rows that should not have
1082 * been formed, or that should have been rejected by the clause. (This is
1083 * only an issue for non-strict quals, since if we can prove a qual mentioning
1084 * only nullable rels is strict, we'd have reduced the outer join to an inner
1085 * join in reduce_outer_joins().)
1087 * To enforce (2), scan the join_info_list and merge the required-relid sets of
1088 * any such OJs into the clause's own reference list. At the time we are
1089 * called, the join_info_list contains only outer joins below this qual. We
1090 * have to repeat the scan until no new relids get added; this ensures that
1091 * the qual is suitably delayed regardless of the order in which OJs get
1092 * executed. As an example, if we have one OJ with LHS=A, RHS=B, and one with
1093 * LHS=B, RHS=C, it is implied that these can be done in either order; if the
1094 * B/C join is done first then the join to A can null C, so a qual actually
1095 * mentioning only C cannot be applied below the join to A.
1097 * For a non-pushed-down qual, this isn't going to determine where we place the
1098 * qual, but we need to determine outerjoin_delayed anyway for possible use
1099 * in reconsider_outer_join_clauses().
1101 * Lastly, a pushed-down qual that references the nullable side of any current
1102 * join_info_list member and has to be evaluated above that OJ (because its
1103 * required relids overlap the LHS too) causes that OJ's delay_upper_joins
1104 * flag to be set TRUE. This will prevent any higher-level OJs from
1105 * being interchanged with that OJ, which would result in not having any
1106 * correct place to evaluate the qual. (The case we care about here is a
1107 * sub-select WHERE clause within the RHS of some outer join. The WHERE
1108 * clause must effectively be treated as a degenerate clause of that outer
1109 * join's condition. Rather than trying to match such clauses with joins
1110 * directly, we set delay_upper_joins here, and when the upper outer join
1111 * is processed by make_outerjoininfo, it will refrain from allowing the
1112 * two OJs to commute.)
1115 check_outerjoin_delay(PlannerInfo *root, Relids *relids_p,
1116 bool is_pushed_down)
1118 Relids relids = *relids_p;
1119 bool outerjoin_delayed;
1122 outerjoin_delayed = false;
1128 foreach(l, root->join_info_list)
1130 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
1132 /* do we reference any nullable rels of this OJ? */
1133 if (bms_overlap(relids, sjinfo->min_righthand) ||
1134 (sjinfo->jointype == JOIN_FULL &&
1135 bms_overlap(relids, sjinfo->min_lefthand)))
1137 /* yes, so set the result flag */
1138 outerjoin_delayed = true;
1139 /* have we included all its rels in relids? */
1140 if (!bms_is_subset(sjinfo->min_lefthand, relids) ||
1141 !bms_is_subset(sjinfo->min_righthand, relids))
1143 /* no, so add them in */
1144 relids = bms_add_members(relids, sjinfo->min_lefthand);
1145 relids = bms_add_members(relids, sjinfo->min_righthand);
1146 /* we'll need another iteration */
1149 /* set delay_upper_joins if needed */
1150 if (is_pushed_down && sjinfo->jointype != JOIN_FULL &&
1151 bms_overlap(relids, sjinfo->min_lefthand))
1152 sjinfo->delay_upper_joins = true;
1155 } while (found_some);
1158 return outerjoin_delayed;
1162 * check_redundant_nullability_qual
1163 * Check to see if the qual is an IS NULL qual that is redundant with
1164 * a lower JOIN_ANTI join.
1166 * We want to suppress redundant IS NULL quals, not so much to save cycles
1167 * as to avoid generating bogus selectivity estimates for them. So if
1168 * redundancy is detected here, distribute_qual_to_rels() just throws away
1172 check_redundant_nullability_qual(PlannerInfo *root, Node *clause)
1174 Var *forced_null_var;
1175 Index forced_null_rel;
1176 SpecialJoinInfo *match_sjinfo = NULL;
1179 /* Check for IS NULL, and identify the Var forced to NULL */
1180 forced_null_var = find_forced_null_var(clause);
1181 if (forced_null_var == NULL)
1183 forced_null_rel = forced_null_var->varno;
1186 * Search to see if there's a matching antijoin that is not masked by
1187 * a higher outer join. Because we have to scan the join info bottom-up,
1188 * we have to continue looking after finding a match to check for masking
1189 * joins. This logic should agree with reduce_outer_joins's code
1190 * to detect antijoins on the basis of IS NULL clauses. (It's tempting
1191 * to consider adding some data structures to avoid redundant work,
1192 * but in practice this code shouldn't get executed often enough to
1193 * make it worth the trouble.)
1195 foreach(lc, root->join_info_list)
1197 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
1199 /* Check for match ... */
1200 if (sjinfo->jointype == JOIN_ANTI &&
1201 bms_is_member(forced_null_rel, sjinfo->syn_righthand))
1203 List *nonnullable_vars;
1205 nonnullable_vars = find_nonnullable_vars((Node *) sjinfo->join_quals);
1206 if (list_member(nonnullable_vars, forced_null_var))
1208 match_sjinfo = sjinfo;
1213 * Else, if we had a lower match, check to see if the target var is
1214 * from the nullable side of this OJ. If so, this OJ masks the
1215 * lower one and we can no longer consider the IS NULL as redundant
1216 * with the lower antijoin.
1220 if (bms_is_member(forced_null_rel, sjinfo->syn_righthand) ||
1221 (sjinfo->jointype == JOIN_FULL &&
1222 bms_is_member(forced_null_rel, sjinfo->syn_lefthand)))
1223 match_sjinfo = NULL;
1226 return (match_sjinfo != NULL);
1230 * distribute_restrictinfo_to_rels
1231 * Push a completed RestrictInfo into the proper restriction or join
1234 * This is the last step of distribute_qual_to_rels() for ordinary qual
1235 * clauses. Clauses that are interesting for equivalence-class processing
1236 * are diverted to the EC machinery, but may ultimately get fed back here.
1239 distribute_restrictinfo_to_rels(PlannerInfo *root,
1240 RestrictInfo *restrictinfo)
1242 Relids relids = restrictinfo->required_relids;
1245 switch (bms_membership(relids))
1250 * There is only one relation participating in the clause, so it
1251 * is a restriction clause for that relation.
1253 rel = find_base_rel(root, bms_singleton_member(relids));
1255 /* Add clause to rel's restriction list */
1256 rel->baserestrictinfo = lappend(rel->baserestrictinfo,
1262 * The clause is a join clause, since there is more than one rel
1267 * Check for hashjoinable operators. (We don't bother setting the
1268 * hashjoin info if we're not going to need it.)
1270 if (enable_hashjoin)
1271 check_hashjoinable(restrictinfo);
1274 * Add clause to the join lists of all the relevant relations.
1276 add_join_clause_to_rels(root, restrictinfo, relids);
1281 * clause references no rels, and therefore we have no place to
1282 * attach it. Shouldn't get here if callers are working properly.
1284 elog(ERROR, "cannot cope with variable-free clause");
1290 * process_implied_equality
1291 * Create a restrictinfo item that says "item1 op item2", and push it
1292 * into the appropriate lists. (In practice opno is always a btree
1293 * equality operator.)
1295 * "qualscope" is the nominal syntactic level to impute to the restrictinfo.
1296 * This must contain at least all the rels used in the expressions, but it
1297 * is used only to set the qual application level when both exprs are
1298 * variable-free. Otherwise the qual is applied at the lowest join level
1299 * that provides all its variables.
1301 * "both_const" indicates whether both items are known pseudo-constant;
1302 * in this case it is worth applying eval_const_expressions() in case we
1303 * can produce constant TRUE or constant FALSE. (Otherwise it's not,
1304 * because the expressions went through eval_const_expressions already.)
1306 * This is currently used only when an EquivalenceClass is found to
1307 * contain pseudoconstants. See path/pathkeys.c for more details.
1310 process_implied_equality(PlannerInfo *root,
1315 bool below_outer_join,
1321 * Build the new clause. Copy to ensure it shares no substructure with
1322 * original (this is necessary in case there are subselects in there...)
1324 clause = make_opclause(opno,
1325 BOOLOID, /* opresulttype */
1326 false, /* opretset */
1327 (Expr *) copyObject(item1),
1328 (Expr *) copyObject(item2));
1330 /* If both constant, try to reduce to a boolean constant. */
1333 clause = (Expr *) eval_const_expressions(root, (Node *) clause);
1335 /* If we produced const TRUE, just drop the clause */
1336 if (clause && IsA(clause, Const))
1338 Const *cclause = (Const *) clause;
1340 Assert(cclause->consttype == BOOLOID);
1341 if (!cclause->constisnull && DatumGetBool(cclause->constvalue))
1346 /* Make a copy of qualscope to avoid problems if source EC changes */
1347 qualscope = bms_copy(qualscope);
1350 * Push the new clause into all the appropriate restrictinfo lists.
1352 distribute_qual_to_rels(root, (Node *) clause,
1353 true, below_outer_join,
1354 qualscope, NULL, NULL);
1358 * build_implied_join_equality --- build a RestrictInfo for a derived equality
1360 * This overlaps the functionality of process_implied_equality(), but we
1361 * must return the RestrictInfo, not push it into the joininfo tree.
1364 build_implied_join_equality(Oid opno,
1369 RestrictInfo *restrictinfo;
1373 * Build the new clause. Copy to ensure it shares no substructure with
1374 * original (this is necessary in case there are subselects in there...)
1376 clause = make_opclause(opno,
1377 BOOLOID, /* opresulttype */
1378 false, /* opretset */
1379 (Expr *) copyObject(item1),
1380 (Expr *) copyObject(item2));
1382 /* Make a copy of qualscope to avoid problems if source EC changes */
1383 qualscope = bms_copy(qualscope);
1386 * Build the RestrictInfo node itself.
1388 restrictinfo = make_restrictinfo(clause,
1389 true, /* is_pushed_down */
1390 false, /* outerjoin_delayed */
1391 false, /* pseudoconstant */
1394 /* Set mergejoinability info always, and hashjoinability if enabled */
1395 check_mergejoinable(restrictinfo);
1396 if (enable_hashjoin)
1397 check_hashjoinable(restrictinfo);
1399 return restrictinfo;
1403 /*****************************************************************************
1405 * CHECKS FOR MERGEJOINABLE AND HASHJOINABLE CLAUSES
1407 *****************************************************************************/
1410 * check_mergejoinable
1411 * If the restrictinfo's clause is mergejoinable, set the mergejoin
1412 * info fields in the restrictinfo.
1414 * Currently, we support mergejoin for binary opclauses where
1415 * the operator is a mergejoinable operator. The arguments can be
1416 * anything --- as long as there are no volatile functions in them.
1419 check_mergejoinable(RestrictInfo *restrictinfo)
1421 Expr *clause = restrictinfo->clause;
1424 if (restrictinfo->pseudoconstant)
1426 if (!is_opclause(clause))
1428 if (list_length(((OpExpr *) clause)->args) != 2)
1431 opno = ((OpExpr *) clause)->opno;
1433 if (op_mergejoinable(opno) &&
1434 !contain_volatile_functions((Node *) clause))
1435 restrictinfo->mergeopfamilies = get_mergejoin_opfamilies(opno);
1438 * Note: op_mergejoinable is just a hint; if we fail to find the operator
1439 * in any btree opfamilies, mergeopfamilies remains NIL and so the clause
1440 * is not treated as mergejoinable.
1445 * check_hashjoinable
1446 * If the restrictinfo's clause is hashjoinable, set the hashjoin
1447 * info fields in the restrictinfo.
1449 * Currently, we support hashjoin for binary opclauses where
1450 * the operator is a hashjoinable operator. The arguments can be
1451 * anything --- as long as there are no volatile functions in them.
1454 check_hashjoinable(RestrictInfo *restrictinfo)
1456 Expr *clause = restrictinfo->clause;
1459 if (restrictinfo->pseudoconstant)
1461 if (!is_opclause(clause))
1463 if (list_length(((OpExpr *) clause)->args) != 2)
1466 opno = ((OpExpr *) clause)->opno;
1468 if (op_hashjoinable(opno) &&
1469 !contain_volatile_functions((Node *) clause))
1470 restrictinfo->hashjoinoperator = opno;