1 /*-------------------------------------------------------------------------
4 * Target list, qualification, joininfo initialization routines
6 * Portions Copyright (c) 1996-2006, 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.124 2006/12/07 19:33:40 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 void add_vars_to_targetlist(PlannerInfo *root, List *vars,
42 static List *deconstruct_recurse(PlannerInfo *root, Node *jtnode,
43 bool below_outer_join, Relids *qualscope);
44 static OuterJoinInfo *make_outerjoininfo(PlannerInfo *root,
45 Relids left_rels, Relids right_rels,
46 bool is_full_join, Node *clause);
47 static void distribute_qual_to_rels(PlannerInfo *root, Node *clause,
50 bool below_outer_join,
53 Relids outerjoin_nonnullable);
54 static bool qual_is_redundant(PlannerInfo *root, RestrictInfo *restrictinfo,
56 static void check_mergejoinable(RestrictInfo *restrictinfo);
57 static void check_hashjoinable(RestrictInfo *restrictinfo);
60 /*****************************************************************************
64 *****************************************************************************/
67 * add_base_rels_to_query
69 * Scan the query's jointree and create baserel RelOptInfos for all
70 * the base relations (ie, table, subquery, and function RTEs)
71 * appearing in the jointree.
73 * The initial invocation must pass root->parse->jointree as the value of
74 * jtnode. Internally, the function recurses through the jointree.
76 * At the end of this process, there should be one baserel RelOptInfo for
77 * every non-join RTE that is used in the query. Therefore, this routine
78 * is the only place that should call build_simple_rel with reloptkind
79 * RELOPT_BASEREL. (Note: build_simple_rel recurses internally to build
80 * "other rel" RelOptInfos for the members of any appendrels we find here.)
83 add_base_rels_to_query(PlannerInfo *root, Node *jtnode)
87 if (IsA(jtnode, RangeTblRef))
89 int varno = ((RangeTblRef *) jtnode)->rtindex;
91 (void) build_simple_rel(root, varno, RELOPT_BASEREL);
93 else if (IsA(jtnode, FromExpr))
95 FromExpr *f = (FromExpr *) jtnode;
98 foreach(l, f->fromlist)
99 add_base_rels_to_query(root, lfirst(l));
101 else if (IsA(jtnode, JoinExpr))
103 JoinExpr *j = (JoinExpr *) jtnode;
105 add_base_rels_to_query(root, j->larg);
106 add_base_rels_to_query(root, j->rarg);
109 elog(ERROR, "unrecognized node type: %d",
110 (int) nodeTag(jtnode));
114 /*****************************************************************************
118 *****************************************************************************/
121 * build_base_rel_tlists
122 * Add targetlist entries for each var needed in the query's final tlist
123 * to the appropriate base relations.
125 * We mark such vars as needed by "relation 0" to ensure that they will
126 * propagate up through all join plan steps.
129 build_base_rel_tlists(PlannerInfo *root, List *final_tlist)
131 List *tlist_vars = pull_var_clause((Node *) final_tlist, false);
133 if (tlist_vars != NIL)
135 add_vars_to_targetlist(root, tlist_vars, bms_make_singleton(0));
136 list_free(tlist_vars);
141 * add_vars_to_targetlist
142 * For each variable appearing in the list, add it to the owning
143 * relation's targetlist if not already present, and mark the variable
144 * as being needed for the indicated join (or for final output if
145 * where_needed includes "relation 0").
148 add_vars_to_targetlist(PlannerInfo *root, List *vars, Relids where_needed)
152 Assert(!bms_is_empty(where_needed));
156 Var *var = (Var *) lfirst(temp);
157 RelOptInfo *rel = find_base_rel(root, var->varno);
158 int attrno = var->varattno;
160 Assert(attrno >= rel->min_attr && attrno <= rel->max_attr);
161 attrno -= rel->min_attr;
162 if (bms_is_empty(rel->attr_needed[attrno]))
164 /* Variable not yet requested, so add to reltargetlist */
165 /* XXX is copyObject necessary here? */
166 rel->reltargetlist = lappend(rel->reltargetlist, copyObject(var));
168 rel->attr_needed[attrno] = bms_add_members(rel->attr_needed[attrno],
174 /*****************************************************************************
176 * JOIN TREE PROCESSING
178 *****************************************************************************/
181 * deconstruct_jointree
182 * Recursively scan the query's join tree for WHERE and JOIN/ON qual
183 * clauses, and add these to the appropriate restrictinfo and joininfo
184 * lists belonging to base RelOptInfos. Also, add OuterJoinInfo nodes
185 * to root->oj_info_list for any outer joins appearing in the query tree.
186 * Return a "joinlist" data structure showing the join order decisions
187 * that need to be made by make_one_rel().
189 * The "joinlist" result is a list of items that are either RangeTblRef
190 * jointree nodes or sub-joinlists. All the items at the same level of
191 * joinlist must be joined in an order to be determined by make_one_rel()
192 * (note that legal orders may be constrained by OuterJoinInfo nodes).
193 * A sub-joinlist represents a subproblem to be planned separately. Currently
194 * sub-joinlists arise only from FULL OUTER JOIN or when collapsing of
195 * subproblems is stopped by join_collapse_limit or from_collapse_limit.
197 * NOTE: when dealing with inner joins, it is appropriate to let a qual clause
198 * be evaluated at the lowest level where all the variables it mentions are
199 * available. However, we cannot push a qual down into the nullable side(s)
200 * of an outer join since the qual might eliminate matching rows and cause a
201 * NULL row to be incorrectly emitted by the join. Therefore, we artificially
202 * OR the minimum-relids of such an outer join into the required_relids of
203 * clauses appearing above it. This forces those clauses to be delayed until
204 * application of the outer join (or maybe even higher in the join tree).
207 deconstruct_jointree(PlannerInfo *root)
211 /* Start recursion at top of jointree */
212 Assert(root->parse->jointree != NULL &&
213 IsA(root->parse->jointree, FromExpr));
215 return deconstruct_recurse(root, (Node *) root->parse->jointree, false,
220 * deconstruct_recurse
221 * One recursion level of deconstruct_jointree processing.
224 * jtnode is the jointree node to examine
225 * below_outer_join is TRUE if this node is within the nullable side of a
226 * higher-level outer join
228 * *qualscope gets the set of base Relids syntactically included in this
229 * jointree node (do not modify or free this, as it may also be pointed
230 * to by RestrictInfo nodes)
231 * Return value is the appropriate joinlist for this jointree node
233 * In addition, entries will be added to root->oj_info_list for outer joins.
236 deconstruct_recurse(PlannerInfo *root, Node *jtnode, bool below_outer_join,
246 if (IsA(jtnode, RangeTblRef))
248 int varno = ((RangeTblRef *) jtnode)->rtindex;
250 /* No quals to deal with, just return correct result */
251 *qualscope = bms_make_singleton(varno);
252 joinlist = list_make1(jtnode);
254 else if (IsA(jtnode, FromExpr))
256 FromExpr *f = (FromExpr *) jtnode;
261 * First, recurse to handle child joins. We collapse subproblems into
262 * a single joinlist whenever the resulting joinlist wouldn't exceed
263 * from_collapse_limit members. Also, always collapse one-element
264 * subproblems, since that won't lengthen the joinlist anyway.
268 remaining = list_length(f->fromlist);
269 foreach(l, f->fromlist)
271 Relids sub_qualscope;
275 sub_joinlist = deconstruct_recurse(root, lfirst(l),
278 *qualscope = bms_add_members(*qualscope, sub_qualscope);
279 sub_members = list_length(sub_joinlist);
281 if (sub_members <= 1 ||
282 list_length(joinlist) + sub_members + remaining <= from_collapse_limit)
283 joinlist = list_concat(joinlist, sub_joinlist);
285 joinlist = lappend(joinlist, sub_joinlist);
289 * Now process the top-level quals. These are always marked as
290 * "pushed down", since they clearly didn't come from a JOIN expr.
292 foreach(l, (List *) f->quals)
293 distribute_qual_to_rels(root, (Node *) lfirst(l),
294 true, false, below_outer_join,
295 *qualscope, NULL, NULL);
297 else if (IsA(jtnode, JoinExpr))
299 JoinExpr *j = (JoinExpr *) jtnode;
306 OuterJoinInfo *ojinfo;
310 * Order of operations here is subtle and critical. First we recurse
311 * to handle sub-JOINs. Their join quals will be placed without
312 * regard for whether this level is an outer join, which is correct.
313 * Then we place our own join quals, which are restricted by lower
314 * outer joins in any case, and are forced to this level if this is an
315 * outer join and they mention the outer side. Finally, if this is an
316 * outer join, we create an oj_info_list entry for the join. This
317 * will prevent quals above us in the join tree that use those rels
318 * from being pushed down below this level. (It's okay for upper
319 * quals to be pushed down to the outer side, however.)
324 leftjoinlist = deconstruct_recurse(root, j->larg,
327 rightjoinlist = deconstruct_recurse(root, j->rarg,
330 *qualscope = bms_union(leftids, rightids);
331 /* Inner join adds no restrictions for quals */
332 nonnullable_rels = NULL;
335 leftjoinlist = deconstruct_recurse(root, j->larg,
338 rightjoinlist = deconstruct_recurse(root, j->rarg,
341 *qualscope = bms_union(leftids, rightids);
342 nonnullable_rels = leftids;
345 leftjoinlist = deconstruct_recurse(root, j->larg,
348 rightjoinlist = deconstruct_recurse(root, j->rarg,
351 *qualscope = bms_union(leftids, rightids);
352 /* each side is both outer and inner */
353 nonnullable_rels = *qualscope;
356 /* notice we switch leftids and rightids */
357 leftjoinlist = deconstruct_recurse(root, j->larg,
360 rightjoinlist = deconstruct_recurse(root, j->rarg,
363 *qualscope = bms_union(leftids, rightids);
364 nonnullable_rels = leftids;
367 elog(ERROR, "unrecognized join type: %d",
369 nonnullable_rels = NULL; /* keep compiler quiet */
370 leftjoinlist = rightjoinlist = NIL;
375 * For an OJ, form the OuterJoinInfo now, because we need the OJ's
376 * semantic scope (ojscope) to pass to distribute_qual_to_rels.
378 if (j->jointype != JOIN_INNER)
380 ojinfo = make_outerjoininfo(root, leftids, rightids,
381 (j->jointype == JOIN_FULL), j->quals);
382 ojscope = bms_union(ojinfo->min_lefthand, ojinfo->min_righthand);
390 /* Process the qual clauses */
391 foreach(qual, (List *) j->quals)
392 distribute_qual_to_rels(root, (Node *) lfirst(qual),
393 false, false, below_outer_join,
394 *qualscope, ojscope, nonnullable_rels);
396 /* Now we can add the OuterJoinInfo to oj_info_list */
398 root->oj_info_list = lappend(root->oj_info_list, ojinfo);
401 * Finally, compute the output joinlist. We fold subproblems together
402 * except at a FULL JOIN or where join_collapse_limit would be
405 if (j->jointype != JOIN_FULL &&
406 (list_length(leftjoinlist) + list_length(rightjoinlist) <=
407 join_collapse_limit))
408 joinlist = list_concat(leftjoinlist, rightjoinlist);
410 /* force the join order at this node */
411 joinlist = list_make1(list_make2(leftjoinlist, rightjoinlist));
415 elog(ERROR, "unrecognized node type: %d",
416 (int) nodeTag(jtnode));
417 joinlist = NIL; /* keep compiler quiet */
424 * Build an OuterJoinInfo for the current outer join
427 * left_rels: the base Relids syntactically on outer side of join
428 * right_rels: the base Relids syntactically on inner side of join
429 * is_full_join: what it says
430 * clause: the outer join's join condition
432 * If the join is a RIGHT JOIN, left_rels and right_rels are switched by
433 * the caller, so that left_rels is always the nonnullable side. Hence
434 * we need only distinguish the LEFT and FULL cases.
436 * The node should eventually be put into root->oj_info_list, but we
437 * do not do that here.
439 static OuterJoinInfo *
440 make_outerjoininfo(PlannerInfo *root,
441 Relids left_rels, Relids right_rels,
442 bool is_full_join, Node *clause)
444 OuterJoinInfo *ojinfo = makeNode(OuterJoinInfo);
445 Relids clause_relids;
446 Relids strict_relids;
450 * Presently the executor cannot support FOR UPDATE/SHARE marking of rels
451 * appearing on the nullable side of an outer join. (It's somewhat unclear
452 * what that would mean, anyway: what should we mark when a result row is
453 * generated from no element of the nullable relation?) So, complain if
454 * any nullable rel is FOR UPDATE/SHARE.
456 * You might be wondering why this test isn't made far upstream in the
457 * parser. It's because the parser hasn't got enough info --- consider
458 * FOR UPDATE applied to a view. Only after rewriting and flattening do
459 * we know whether the view contains an outer join.
461 foreach(l, root->parse->rowMarks)
463 RowMarkClause *rc = (RowMarkClause *) lfirst(l);
465 if (bms_is_member(rc->rti, right_rels) ||
466 (is_full_join && bms_is_member(rc->rti, left_rels)))
468 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
469 errmsg("SELECT FOR UPDATE/SHARE cannot be applied to the nullable side of an outer join")));
472 /* If it's a full join, no need to be very smart */
473 ojinfo->is_full_join = is_full_join;
476 ojinfo->min_lefthand = left_rels;
477 ojinfo->min_righthand = right_rels;
478 ojinfo->lhs_strict = false; /* don't care about this */
483 * Retrieve all relids mentioned within the join clause.
485 clause_relids = pull_varnos(clause);
488 * For which relids is the clause strict, ie, it cannot succeed if the
489 * rel's columns are all NULL?
491 strict_relids = find_nonnullable_rels(clause);
493 /* Remember whether the clause is strict for any LHS relations */
494 ojinfo->lhs_strict = bms_overlap(strict_relids, left_rels);
497 * Required LHS is basically the LHS rels mentioned in the clause... but
498 * if there aren't any, punt and make it the full LHS, to avoid having an
499 * empty min_lefthand which will confuse later processing. (We don't try
500 * to be smart about such cases, just correct.) We may have to add more
501 * rels based on lower outer joins; see below.
503 ojinfo->min_lefthand = bms_intersect(clause_relids, left_rels);
504 if (bms_is_empty(ojinfo->min_lefthand))
505 ojinfo->min_lefthand = bms_copy(left_rels);
508 * Required RHS is normally the full set of RHS rels. Sometimes we can
509 * exclude some, see below.
511 ojinfo->min_righthand = bms_copy(right_rels);
513 foreach(l, root->oj_info_list)
515 OuterJoinInfo *otherinfo = (OuterJoinInfo *) lfirst(l);
517 /* ignore full joins --- other mechanisms preserve their ordering */
518 if (otherinfo->is_full_join)
522 * For a lower OJ in our LHS, if our join condition uses the lower
523 * join's RHS and is not strict for that rel, we must preserve the
524 * ordering of the two OJs, so add lower OJ's full required relset to
527 if (bms_overlap(ojinfo->min_lefthand, otherinfo->min_righthand) &&
528 !bms_overlap(strict_relids, otherinfo->min_righthand))
530 ojinfo->min_lefthand = bms_add_members(ojinfo->min_lefthand,
531 otherinfo->min_lefthand);
532 ojinfo->min_lefthand = bms_add_members(ojinfo->min_lefthand,
533 otherinfo->min_righthand);
537 * For a lower OJ in our RHS, if our join condition does not use the
538 * lower join's RHS and the lower OJ's join condition is strict, we
539 * can interchange the ordering of the two OJs, so exclude the lower
540 * RHS from our min_righthand.
542 if (bms_overlap(ojinfo->min_righthand, otherinfo->min_righthand) &&
543 !bms_overlap(clause_relids, otherinfo->min_righthand) &&
544 otherinfo->lhs_strict)
546 ojinfo->min_righthand = bms_del_members(ojinfo->min_righthand,
547 otherinfo->min_righthand);
551 /* Neither set should be empty, else we might get confused later */
552 Assert(!bms_is_empty(ojinfo->min_lefthand));
553 Assert(!bms_is_empty(ojinfo->min_righthand));
554 /* Shouldn't overlap either */
555 Assert(!bms_overlap(ojinfo->min_lefthand, ojinfo->min_righthand));
561 /*****************************************************************************
565 *****************************************************************************/
568 * distribute_qual_to_rels
569 * Add clause information to either the baserestrictinfo or joininfo list
570 * (depending on whether the clause is a join) of each base relation
571 * mentioned in the clause. A RestrictInfo node is created and added to
572 * the appropriate list for each rel. Also, if the clause uses a
573 * mergejoinable operator and is not delayed by outer-join rules, enter
574 * the left- and right-side expressions into the query's lists of
577 * 'clause': the qual clause to be distributed
578 * 'is_pushed_down': if TRUE, force the clause to be marked 'is_pushed_down'
579 * (this indicates the clause came from a FromExpr, not a JoinExpr)
580 * 'is_deduced': TRUE if the qual came from implied-equality deduction
581 * 'below_outer_join': TRUE if the qual is from a JOIN/ON that is below the
582 * nullable side of a higher-level outer join.
583 * 'qualscope': set of baserels the qual's syntactic scope covers
584 * 'ojscope': NULL if not an outer-join qual, else the minimum set of baserels
585 * needed to form this join
586 * 'outerjoin_nonnullable': NULL if not an outer-join qual, else the set of
587 * baserels appearing on the outer (nonnullable) side of the join
588 * (for FULL JOIN this includes both sides of the join, and must in fact
591 * 'qualscope' identifies what level of JOIN the qual came from syntactically.
592 * 'ojscope' is needed if we decide to force the qual up to the outer-join
593 * level, which will be ojscope not necessarily qualscope.
596 distribute_qual_to_rels(PlannerInfo *root, Node *clause,
599 bool below_outer_join,
602 Relids outerjoin_nonnullable)
605 bool outerjoin_delayed;
606 bool pseudoconstant = false;
608 bool maybe_outer_join;
609 RestrictInfo *restrictinfo;
614 * Retrieve all relids mentioned within the clause.
616 relids = pull_varnos(clause);
619 * Cross-check: clause should contain no relids not within its scope.
620 * Otherwise the parser messed up.
622 if (!bms_is_subset(relids, qualscope))
623 elog(ERROR, "JOIN qualification may not refer to other relations");
624 if (ojscope && !bms_is_subset(relids, ojscope))
625 elog(ERROR, "JOIN qualification may not refer to other relations");
628 * If the clause is variable-free, our normal heuristic for pushing it
629 * down to just the mentioned rels doesn't work, because there are none.
631 * If the clause is an outer-join clause, we must force it to the OJ's
632 * semantic level to preserve semantics.
634 * Otherwise, when the clause contains volatile functions, we force it to
635 * be evaluated at its original syntactic level. This preserves the
636 * expected semantics.
638 * When the clause contains no volatile functions either, it is actually a
639 * pseudoconstant clause that will not change value during any one
640 * execution of the plan, and hence can be used as a one-time qual in a
641 * gating Result plan node. We put such a clause into the regular
642 * RestrictInfo lists for the moment, but eventually createplan.c will
643 * pull it out and make a gating Result node immediately above whatever
644 * plan node the pseudoconstant clause is assigned to. It's usually best
645 * to put a gating node as high in the plan tree as possible. If we are
646 * not below an outer join, we can actually push the pseudoconstant qual
647 * all the way to the top of the tree. If we are below an outer join, we
648 * leave the qual at its original syntactic level (we could push it up to
649 * just below the outer join, but that seems more complex than it's
652 if (bms_is_empty(relids))
656 /* clause is attached to outer join, eval it there */
658 /* mustn't use as gating qual, so don't mark pseudoconstant */
662 /* eval at original syntactic level */
664 if (!contain_volatile_functions(clause))
666 /* mark as gating qual */
667 pseudoconstant = true;
668 /* tell createplan.c to check for gating quals */
669 root->hasPseudoConstantQuals = true;
670 /* if not below outer join, push it to top of tree */
671 if (!below_outer_join)
673 relids = get_relids_in_jointree((Node *) root->parse->jointree);
674 is_pushed_down = true;
681 * Check to see if clause application must be delayed by outer-join
687 * If the qual came from implied-equality deduction, we always
688 * evaluate the qual at its natural semantic level. It is the
689 * responsibility of the deducer not to create any quals that should
690 * be delayed by outer-join rules.
692 Assert(bms_equal(relids, qualscope));
694 Assert(!pseudoconstant);
695 /* Needn't feed it back for more deductions */
696 outerjoin_delayed = false;
697 maybe_equijoin = false;
698 maybe_outer_join = false;
700 else if (bms_overlap(relids, outerjoin_nonnullable))
703 * The qual is attached to an outer join and mentions (some of the)
704 * rels on the nonnullable side. Force the qual to be evaluated
705 * exactly at the level of joining corresponding to the outer join. We
706 * cannot let it get pushed down into the nonnullable side, since then
707 * we'd produce no output rows, rather than the intended single
708 * null-extended row, for any nonnullable-side rows failing the qual.
710 * Note: an outer-join qual that mentions only nullable-side rels can
711 * be pushed down into the nullable side without changing the join
712 * result, so we treat it the same as an ordinary inner-join qual,
713 * except for not setting maybe_equijoin (see below).
717 outerjoin_delayed = true;
718 Assert(!pseudoconstant);
721 * We can't use such a clause to deduce equijoin (the left and right
722 * sides might be unequal above the join because one of them has gone
723 * to NULL) ... but we might be able to use it for more limited
724 * purposes. Note: for the current uses of deductions from an
725 * outer-join clause, it seems safe to make the deductions even when
726 * the clause is below a higher-level outer join; so we do not check
727 * below_outer_join here.
729 maybe_equijoin = false;
730 maybe_outer_join = true;
735 * For a non-outer-join qual, we can evaluate the qual as soon as (1)
736 * we have all the rels it mentions, and (2) we are at or above any
737 * outer joins that can null any of these rels and are below the
738 * syntactic location of the given qual. We must enforce (2) because
739 * pushing down such a clause below the OJ might cause the OJ to emit
740 * null-extended rows that should not have been formed, or that should
741 * have been rejected by the clause. (This is only an issue for
742 * non-strict quals, since if we can prove a qual mentioning only
743 * nullable rels is strict, we'd have reduced the outer join to an
744 * inner join in reduce_outer_joins().)
746 * To enforce (2), scan the oj_info_list and merge the required-relid
747 * sets of any such OJs into the clause's own reference list. At the
748 * time we are called, the oj_info_list contains only outer joins
749 * below this qual. We have to repeat the scan until no new relids
750 * get added; this ensures that the qual is suitably delayed regardless
751 * of the order in which OJs get executed. As an example, if we have
752 * one OJ with LHS=A, RHS=B, and one with LHS=B, RHS=C, it is implied
753 * that these can be done in either order; if the B/C join is done
754 * first then the join to A can null C, so a qual actually mentioning
755 * only C cannot be applied below the join to A.
759 outerjoin_delayed = false;
764 foreach(l, root->oj_info_list)
766 OuterJoinInfo *ojinfo = (OuterJoinInfo *) lfirst(l);
768 /* do we have any nullable rels of this OJ? */
769 if (bms_overlap(relids, ojinfo->min_righthand) ||
770 (ojinfo->is_full_join &&
771 bms_overlap(relids, ojinfo->min_lefthand)))
773 /* yes; do we have all its rels? */
774 if (!bms_is_subset(ojinfo->min_lefthand, relids) ||
775 !bms_is_subset(ojinfo->min_righthand, relids))
777 /* no, so add them in */
778 relids = bms_add_members(relids,
779 ojinfo->min_lefthand);
780 relids = bms_add_members(relids,
781 ojinfo->min_righthand);
782 outerjoin_delayed = true;
783 /* we'll need another iteration */
788 } while (found_some);
790 if (outerjoin_delayed)
792 /* Should still be a subset of current scope ... */
793 Assert(bms_is_subset(relids, qualscope));
795 * Because application of the qual will be delayed by outer join,
796 * we mustn't assume its vars are equal everywhere.
798 maybe_equijoin = false;
803 * Qual is not delayed by any lower outer-join restriction. If it
804 * is not itself below or within an outer join, we can consider it
805 * "valid everywhere", so consider feeding it to the equijoin
806 * machinery. (If it is within an outer join, we can't consider
807 * it "valid everywhere": once the contained variables have gone
808 * to NULL, we'd be asserting things like NULL = NULL, which is
811 if (!below_outer_join && outerjoin_nonnullable == NULL)
812 maybe_equijoin = true;
814 maybe_equijoin = false;
817 /* Since it doesn't mention the LHS, it's certainly not an OJ clause */
818 maybe_outer_join = false;
822 * Mark the qual as "pushed down" if it can be applied at a level below
823 * its original syntactic level. This allows us to distinguish original
824 * JOIN/ON quals from higher-level quals pushed down to the same joinrel.
825 * A qual originating from WHERE is always considered "pushed down". Note
826 * that for an outer-join qual, we have to compare to ojscope not
830 is_pushed_down = !bms_equal(relids, ojscope ? ojscope : qualscope);
833 * Build the RestrictInfo node itself.
835 restrictinfo = make_restrictinfo((Expr *) clause,
842 * Figure out where to attach it.
844 switch (bms_membership(relids))
849 * There is only one relation participating in 'clause', so
850 * 'clause' is a restriction clause for that relation.
852 rel = find_base_rel(root, bms_singleton_member(relids));
855 * Check for a "mergejoinable" clause even though it's not a join
856 * clause. This is so that we can recognize that "a.x = a.y"
857 * makes x and y eligible to be considered equal, even when they
858 * belong to the same rel. Without this, we would not recognize
859 * that "a.x = a.y AND a.x = b.z AND a.y = c.q" allows us to
860 * consider z and q equal after their rels are joined.
862 check_mergejoinable(restrictinfo);
865 * If the clause was deduced from implied equality, check to see
866 * whether it is redundant with restriction clauses we already
867 * have for this rel. Note we cannot apply this check to
868 * user-written clauses, since we haven't found the canonical
869 * pathkey sets yet while processing user clauses. (NB: no
870 * comparable check is done in the join-clause case; redundancy
871 * will be detected when the join clause is moved into a join
872 * rel's restriction list.)
875 !qual_is_redundant(root, restrictinfo,
876 rel->baserestrictinfo))
878 /* Add clause to rel's restriction list */
879 rel->baserestrictinfo = lappend(rel->baserestrictinfo,
886 * 'clause' is a join clause, since there is more than one rel in
891 * Check for hash or mergejoinable operators.
893 * We don't bother setting the hashjoin info if we're not going to
894 * need it. We do want to know about mergejoinable ops in all
895 * cases, however, because we use mergejoinable ops for other
896 * purposes such as detecting redundant clauses.
898 check_mergejoinable(restrictinfo);
900 check_hashjoinable(restrictinfo);
903 * Add clause to the join lists of all the relevant relations.
905 add_join_clause_to_rels(root, restrictinfo, relids);
908 * Add vars used in the join clause to targetlists of their
909 * relations, so that they will be emitted by the plan nodes that
910 * scan those relations (else they won't be available at the join
913 vars = pull_var_clause(clause, false);
914 add_vars_to_targetlist(root, vars, relids);
920 * 'clause' references no rels, and therefore we have no place to
921 * attach it. Shouldn't get here if callers are working properly.
923 elog(ERROR, "cannot cope with variable-free clause");
928 * If the clause has a mergejoinable operator, we may be able to deduce
929 * more things from it under the principle of transitivity.
931 * If it is not an outer-join qualification nor bubbled up due to an outer
932 * join, then the two sides represent equivalent PathKeyItems for path
933 * keys: any path that is sorted by one side will also be sorted by the
934 * other (as soon as the two rels are joined, that is). Pass such clauses
935 * to add_equijoined_keys.
937 * If it is a left or right outer-join qualification that relates the two
938 * sides of the outer join (no funny business like leftvar1 = leftvar2 +
939 * rightvar), we add it to root->left_join_clauses or
940 * root->right_join_clauses according to which side the nonnullable
941 * variable appears on.
943 * If it is a full outer-join qualification, we add it to
944 * root->full_join_clauses. (Ideally we'd discard cases that aren't
945 * leftvar = rightvar, as we do for left/right joins, but this routine
946 * doesn't have the info needed to do that; and the current usage of the
947 * full_join_clauses list doesn't require that, so it's not currently
948 * worth complicating this routine's API to make it possible.)
950 if (restrictinfo->mergejoinoperator != InvalidOid)
953 add_equijoined_keys(root, restrictinfo);
954 else if (maybe_outer_join && restrictinfo->can_join)
956 if (bms_is_subset(restrictinfo->left_relids,
957 outerjoin_nonnullable) &&
958 !bms_overlap(restrictinfo->right_relids,
959 outerjoin_nonnullable))
961 /* we have outervar = innervar */
962 root->left_join_clauses = lappend(root->left_join_clauses,
965 else if (bms_is_subset(restrictinfo->right_relids,
966 outerjoin_nonnullable) &&
967 !bms_overlap(restrictinfo->left_relids,
968 outerjoin_nonnullable))
970 /* we have innervar = outervar */
971 root->right_join_clauses = lappend(root->right_join_clauses,
974 else if (bms_equal(outerjoin_nonnullable, qualscope))
976 /* FULL JOIN (above tests cannot match in this case) */
977 root->full_join_clauses = lappend(root->full_join_clauses,
985 * process_implied_equality
986 * Check to see whether we already have a restrictinfo item that says
987 * item1 = item2, and create one if not; or if delete_it is true,
988 * remove any such restrictinfo item.
990 * This processing is a consequence of transitivity of mergejoin equality:
991 * if we have mergejoinable clauses A = B and B = C, we can deduce A = C
992 * (where = is an appropriate mergejoinable operator). See path/pathkeys.c
996 process_implied_equality(PlannerInfo *root,
997 Node *item1, Node *item2,
998 Oid sortop1, Oid sortop2,
999 Relids item1_relids, Relids item2_relids,
1003 BMS_Membership membership;
1009 Operator eq_operator;
1010 Form_pg_operator pgopform;
1013 /* Get set of relids referenced in the two expressions */
1014 relids = bms_union(item1_relids, item2_relids);
1015 membership = bms_membership(relids);
1018 * generate_implied_equalities() shouldn't call me on two constants.
1020 Assert(membership != BMS_EMPTY_SET);
1023 * If the exprs involve a single rel, we need to look at that rel's
1024 * baserestrictinfo list. If multiple rels, we can scan the joininfo list
1027 if (membership == BMS_SINGLETON)
1029 rel1 = find_base_rel(root, bms_singleton_member(relids));
1030 restrictlist = rel1->baserestrictinfo;
1037 /* Copy relids, find and remove one member */
1038 other_rels = bms_copy(relids);
1039 first_rel = bms_first_member(other_rels);
1040 bms_free(other_rels);
1042 rel1 = find_base_rel(root, first_rel);
1043 restrictlist = rel1->joininfo;
1047 * Scan to see if equality is already known. If so, we're done in the add
1048 * case, and done after removing it in the delete case.
1050 foreach(itm, restrictlist)
1052 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(itm);
1056 if (restrictinfo->mergejoinoperator == InvalidOid)
1057 continue; /* ignore non-mergejoinable clauses */
1058 /* We now know the restrictinfo clause is a binary opclause */
1059 left = get_leftop(restrictinfo->clause);
1060 right = get_rightop(restrictinfo->clause);
1061 if ((equal(item1, left) && equal(item2, right)) ||
1062 (equal(item2, left) && equal(item1, right)))
1064 /* found a matching clause */
1067 if (membership == BMS_SINGLETON)
1069 /* delete it from local restrictinfo list */
1070 rel1->baserestrictinfo = list_delete_ptr(rel1->baserestrictinfo,
1075 /* let joininfo.c do it */
1076 remove_join_clause_from_rels(root, restrictinfo, relids);
1083 /* Didn't find it. Done if deletion requested */
1088 * This equality is new information, so construct a clause representing it
1089 * to add to the query data structures.
1091 ltype = exprType(item1);
1092 rtype = exprType(item2);
1093 eq_operator = compatible_oper(NULL, list_make1(makeString("=")),
1096 if (!HeapTupleIsValid(eq_operator))
1099 * Would it be safe to just not add the equality to the query if we
1100 * have no suitable equality operator for the combination of
1101 * datatypes? NO, because sortkey selection may screw up anyway.
1104 (errcode(ERRCODE_UNDEFINED_FUNCTION),
1105 errmsg("could not identify an equality operator for types %s and %s",
1106 format_type_be(ltype), format_type_be(rtype))));
1108 pgopform = (Form_pg_operator) GETSTRUCT(eq_operator);
1111 * Let's just make sure this appears to be a compatible operator.
1113 if (pgopform->oprlsortop != sortop1 ||
1114 pgopform->oprrsortop != sortop2 ||
1115 pgopform->oprresult != BOOLOID)
1117 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1118 errmsg("equality operator for types %s and %s should be merge-joinable, but isn't",
1119 format_type_be(ltype), format_type_be(rtype))));
1122 * Now we can build the new clause. Copy to ensure it shares no
1123 * substructure with original (this is necessary in case there are
1124 * subselects in there...)
1126 clause = make_opclause(oprid(eq_operator), /* opno */
1127 BOOLOID, /* opresulttype */
1128 false, /* opretset */
1129 (Expr *) copyObject(item1),
1130 (Expr *) copyObject(item2));
1132 ReleaseSysCache(eq_operator);
1135 * Push the new clause into all the appropriate restrictinfo lists.
1137 * Note: we mark the qual "pushed down" to ensure that it can never be
1138 * taken for an original JOIN/ON clause.
1140 distribute_qual_to_rels(root, (Node *) clause,
1141 true, true, false, relids, NULL, NULL);
1146 * Detect whether an implied-equality qual that turns out to be a
1147 * restriction clause for a single base relation is redundant with
1148 * already-known restriction clauses for that rel. This occurs with,
1150 * SELECT * FROM tab WHERE f1 = f2 AND f2 = f3;
1151 * We need to suppress the redundant condition to avoid computing
1152 * too-small selectivity, not to mention wasting time at execution.
1154 * Note: quals of the form "var = const" are never considered redundant,
1155 * only those of the form "var = var". This is needed because when we
1156 * have constants in an implied-equality set, we use a different strategy
1157 * that suppresses all "var = var" deductions. We must therefore keep
1158 * all the "var = const" quals.
1161 qual_is_redundant(PlannerInfo *root,
1162 RestrictInfo *restrictinfo,
1172 /* Never redundant unless vars appear on both sides */
1173 if (bms_is_empty(restrictinfo->left_relids) ||
1174 bms_is_empty(restrictinfo->right_relids))
1177 newleft = get_leftop(restrictinfo->clause);
1178 newright = get_rightop(restrictinfo->clause);
1181 * Set cached pathkeys. NB: it is okay to do this now because this
1182 * routine is only invoked while we are generating implied equalities.
1183 * Therefore, the equi_key_list is already complete and so we can
1184 * correctly determine canonical pathkeys.
1186 cache_mergeclause_pathkeys(root, restrictinfo);
1187 /* If different, say "not redundant" (should never happen) */
1188 if (restrictinfo->left_pathkey != restrictinfo->right_pathkey)
1192 * Scan existing quals to find those referencing same pathkeys. Usually
1193 * there will be few, if any, so build a list of just the interesting
1197 foreach(olditem, restrictlist)
1199 RestrictInfo *oldrinfo = (RestrictInfo *) lfirst(olditem);
1201 if (oldrinfo->mergejoinoperator != InvalidOid)
1203 cache_mergeclause_pathkeys(root, oldrinfo);
1204 if (restrictinfo->left_pathkey == oldrinfo->left_pathkey &&
1205 restrictinfo->right_pathkey == oldrinfo->right_pathkey)
1206 oldquals = lcons(oldrinfo, oldquals);
1209 if (oldquals == NIL)
1213 * Now, we want to develop a list of exprs that are known equal to the
1214 * left side of the new qual. We traverse the old-quals list repeatedly
1215 * to transitively expand the exprs list. If at any point we find we can
1216 * reach the right-side expr of the new qual, we are done. We give up
1217 * when we can't expand the equalexprs list any more.
1219 equalexprs = list_make1(newleft);
1223 /* cannot use foreach here because of possible list_delete */
1224 olditem = list_head(oldquals);
1227 RestrictInfo *oldrinfo = (RestrictInfo *) lfirst(olditem);
1228 Node *oldleft = get_leftop(oldrinfo->clause);
1229 Node *oldright = get_rightop(oldrinfo->clause);
1230 Node *newguy = NULL;
1232 /* must advance olditem before list_delete possibly pfree's it */
1233 olditem = lnext(olditem);
1235 if (list_member(equalexprs, oldleft))
1237 else if (list_member(equalexprs, oldright))
1241 if (equal(newguy, newright))
1242 return true; /* we proved new clause is redundant */
1243 equalexprs = lcons(newguy, equalexprs);
1247 * Remove this qual from list, since we don't need it anymore.
1249 oldquals = list_delete_ptr(oldquals, oldrinfo);
1251 } while (someadded);
1253 return false; /* it's not redundant */
1257 /*****************************************************************************
1259 * CHECKS FOR MERGEJOINABLE AND HASHJOINABLE CLAUSES
1261 *****************************************************************************/
1264 * check_mergejoinable
1265 * If the restrictinfo's clause is mergejoinable, set the mergejoin
1266 * info fields in the restrictinfo.
1268 * Currently, we support mergejoin for binary opclauses where
1269 * the operator is a mergejoinable operator. The arguments can be
1270 * anything --- as long as there are no volatile functions in them.
1273 check_mergejoinable(RestrictInfo *restrictinfo)
1275 Expr *clause = restrictinfo->clause;
1280 if (restrictinfo->pseudoconstant)
1282 if (!is_opclause(clause))
1284 if (list_length(((OpExpr *) clause)->args) != 2)
1287 opno = ((OpExpr *) clause)->opno;
1289 if (op_mergejoinable(opno,
1292 !contain_volatile_functions((Node *) clause))
1294 restrictinfo->mergejoinoperator = opno;
1295 restrictinfo->left_sortop = leftOp;
1296 restrictinfo->right_sortop = rightOp;
1301 * check_hashjoinable
1302 * If the restrictinfo's clause is hashjoinable, set the hashjoin
1303 * info fields in the restrictinfo.
1305 * Currently, we support hashjoin for binary opclauses where
1306 * the operator is a hashjoinable operator. The arguments can be
1307 * anything --- as long as there are no volatile functions in them.
1310 check_hashjoinable(RestrictInfo *restrictinfo)
1312 Expr *clause = restrictinfo->clause;
1315 if (restrictinfo->pseudoconstant)
1317 if (!is_opclause(clause))
1319 if (list_length(((OpExpr *) clause)->args) != 2)
1322 opno = ((OpExpr *) clause)->opno;
1324 if (op_hashjoinable(opno) &&
1325 !contain_volatile_functions((Node *) clause))
1326 restrictinfo->hashjoinoperator = opno;