1 /*-------------------------------------------------------------------------
4 * Target list, qualification, joininfo initialization routines
6 * Portions Copyright (c) 1996-2005, 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.114 2006/01/31 21:39:24 tgl Exp $
13 *-------------------------------------------------------------------------
17 #include "catalog/pg_operator.h"
18 #include "catalog/pg_type.h"
19 #include "nodes/makefuncs.h"
20 #include "optimizer/clauses.h"
21 #include "optimizer/cost.h"
22 #include "optimizer/joininfo.h"
23 #include "optimizer/pathnode.h"
24 #include "optimizer/paths.h"
25 #include "optimizer/planmain.h"
26 #include "optimizer/restrictinfo.h"
27 #include "optimizer/tlist.h"
28 #include "optimizer/var.h"
29 #include "parser/parsetree.h"
30 #include "parser/parse_expr.h"
31 #include "parser/parse_oper.h"
32 #include "utils/builtins.h"
33 #include "utils/lsyscache.h"
34 #include "utils/syscache.h"
37 /* These parameters are set by GUC */
38 int from_collapse_limit;
39 int join_collapse_limit;
42 static void add_vars_to_targetlist(PlannerInfo *root, List *vars,
44 static List *deconstruct_recurse(PlannerInfo *root, Node *jtnode,
45 bool below_outer_join, Relids *qualscope);
46 static OuterJoinInfo *make_outerjoininfo(PlannerInfo *root,
47 Relids left_rels, Relids right_rels,
48 bool is_full_join, Node *clause);
49 static void distribute_qual_to_rels(PlannerInfo *root, Node *clause,
52 bool below_outer_join,
55 Relids outerjoin_nonnullable);
56 static bool qual_is_redundant(PlannerInfo *root, RestrictInfo *restrictinfo,
58 static void check_mergejoinable(RestrictInfo *restrictinfo);
59 static void check_hashjoinable(RestrictInfo *restrictinfo);
62 /*****************************************************************************
66 *****************************************************************************/
69 * add_base_rels_to_query
71 * Scan the query's jointree and create baserel RelOptInfos for all
72 * the base relations (ie, table, subquery, and function RTEs)
73 * appearing in the jointree.
75 * At the end of this process, there should be one baserel RelOptInfo for
76 * every non-join RTE that is used in the query. Therefore, this routine
77 * is the only place that should call build_simple_rel with reloptkind
78 * RELOPT_BASEREL. However, otherrels will be built later for append relation
82 add_base_rels_to_query(PlannerInfo *root, Node *jtnode)
86 if (IsA(jtnode, RangeTblRef))
88 int varno = ((RangeTblRef *) jtnode)->rtindex;
90 (void) build_simple_rel(root, varno, RELOPT_BASEREL);
92 else if (IsA(jtnode, FromExpr))
94 FromExpr *f = (FromExpr *) jtnode;
97 foreach(l, f->fromlist)
98 add_base_rels_to_query(root, lfirst(l));
100 else if (IsA(jtnode, JoinExpr))
102 JoinExpr *j = (JoinExpr *) jtnode;
104 add_base_rels_to_query(root, j->larg);
105 add_base_rels_to_query(root, j->rarg);
108 elog(ERROR, "unrecognized node type: %d",
109 (int) nodeTag(jtnode));
113 /*****************************************************************************
117 *****************************************************************************/
120 * build_base_rel_tlists
121 * Add targetlist entries for each var needed in the query's final tlist
122 * to the appropriate base relations.
124 * We mark such vars as needed by "relation 0" to ensure that they will
125 * propagate up through all join plan steps.
128 build_base_rel_tlists(PlannerInfo *root, List *final_tlist)
130 List *tlist_vars = pull_var_clause((Node *) final_tlist, false);
132 if (tlist_vars != NIL)
134 add_vars_to_targetlist(root, tlist_vars, bms_make_singleton(0));
135 list_free(tlist_vars);
140 * add_vars_to_targetlist
141 * For each variable appearing in the list, add it to the owning
142 * relation's targetlist if not already present, and mark the variable
143 * as being needed for the indicated join (or for final output if
144 * where_needed includes "relation 0").
147 add_vars_to_targetlist(PlannerInfo *root, List *vars, Relids where_needed)
151 Assert(!bms_is_empty(where_needed));
155 Var *var = (Var *) lfirst(temp);
156 RelOptInfo *rel = find_base_rel(root, var->varno);
157 int attrno = var->varattno;
159 Assert(attrno >= rel->min_attr && attrno <= rel->max_attr);
160 attrno -= rel->min_attr;
161 if (bms_is_empty(rel->attr_needed[attrno]))
163 /* Variable not yet requested, so add to reltargetlist */
164 /* XXX is copyObject necessary here? */
165 rel->reltargetlist = lappend(rel->reltargetlist, copyObject(var));
167 rel->attr_needed[attrno] = bms_add_members(rel->attr_needed[attrno],
173 /*****************************************************************************
175 * JOIN TREE PROCESSING
177 *****************************************************************************/
180 * deconstruct_jointree
181 * Recursively scan the query's join tree for WHERE and JOIN/ON qual
182 * clauses, and add these to the appropriate restrictinfo and joininfo
183 * lists belonging to base RelOptInfos. Also, add OuterJoinInfo nodes
184 * to root->oj_info_list for any outer joins appearing in the query tree.
185 * Return a "joinlist" data structure showing the join order decisions
186 * that need to be made by make_one_rel().
188 * The "joinlist" result is a list of items that are either RangeTblRef
189 * jointree nodes or sub-joinlists. All the items at the same level of
190 * joinlist must be joined in an order to be determined by make_one_rel()
191 * (note that legal orders may be constrained by OuterJoinInfo nodes).
192 * A sub-joinlist represents a subproblem to be planned separately. Currently
193 * sub-joinlists arise only from FULL OUTER JOIN or when collapsing of
194 * subproblems is stopped by join_collapse_limit or from_collapse_limit.
196 * NOTE: when dealing with inner joins, it is appropriate to let a qual clause
197 * be evaluated at the lowest level where all the variables it mentions are
198 * available. However, we cannot push a qual down into the nullable side(s)
199 * of an outer join since the qual might eliminate matching rows and cause a
200 * NULL row to be incorrectly emitted by the join. Therefore, we artificially
201 * OR the minimum-relids of such an outer join into the required_relids of
202 * clauses appearing above it. This forces those clauses to be delayed until
203 * application of the outer join (or maybe even higher in the join tree).
206 deconstruct_jointree(PlannerInfo *root)
210 /* Start recursion at top of jointree */
211 Assert(root->parse->jointree != NULL &&
212 IsA(root->parse->jointree, FromExpr));
214 return deconstruct_recurse(root, (Node *) root->parse->jointree, false,
219 * deconstruct_recurse
220 * One recursion level of deconstruct_jointree processing.
223 * jtnode is the jointree node to examine
224 * below_outer_join is TRUE if this node is within the nullable side of a
225 * higher-level outer join
227 * *qualscope gets the set of base Relids syntactically included in this
228 * jointree node (do not modify or free this, as it may also be pointed
229 * to by RestrictInfo nodes)
230 * Return value is the appropriate joinlist for this jointree node
232 * In addition, entries will be added to root->oj_info_list for outer joins.
235 deconstruct_recurse(PlannerInfo *root, Node *jtnode, bool below_outer_join,
245 if (IsA(jtnode, RangeTblRef))
247 int varno = ((RangeTblRef *) jtnode)->rtindex;
249 /* No quals to deal with, just return correct result */
250 *qualscope = bms_make_singleton(varno);
251 joinlist = list_make1(jtnode);
253 else if (IsA(jtnode, FromExpr))
255 FromExpr *f = (FromExpr *) jtnode;
260 * First, recurse to handle child joins. We collapse subproblems
261 * into a single joinlist whenever the resulting joinlist wouldn't
262 * exceed from_collapse_limit members. Also, always collapse
263 * one-element subproblems, since that won't lengthen the joinlist
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;
369 * This is where we fail if upper levels of planner haven't
370 * rewritten UNION JOIN as an Append ...
373 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
374 errmsg("UNION JOIN is not implemented")));
375 nonnullable_rels = NULL; /* keep compiler quiet */
376 leftjoinlist = rightjoinlist = NIL;
379 elog(ERROR, "unrecognized join type: %d",
381 nonnullable_rels = NULL; /* keep compiler quiet */
382 leftjoinlist = rightjoinlist = NIL;
387 * For an OJ, form the OuterJoinInfo now, because we need the OJ's
388 * semantic scope (ojscope) to pass to distribute_qual_to_rels.
390 if (j->jointype != JOIN_INNER)
392 ojinfo = make_outerjoininfo(root, leftids, rightids,
393 (j->jointype == JOIN_FULL), j->quals);
394 ojscope = bms_union(ojinfo->min_lefthand, ojinfo->min_righthand);
402 /* Process the qual clauses */
403 foreach(qual, (List *) j->quals)
404 distribute_qual_to_rels(root, (Node *) lfirst(qual),
405 false, false, below_outer_join,
406 *qualscope, ojscope, nonnullable_rels);
408 /* Now we can add the OuterJoinInfo to oj_info_list */
410 root->oj_info_list = lappend(root->oj_info_list, ojinfo);
413 * Finally, compute the output joinlist. We fold subproblems together
414 * except at a FULL JOIN or where join_collapse_limit would be
417 if (j->jointype != JOIN_FULL &&
418 (list_length(leftjoinlist) + list_length(rightjoinlist) <=
419 join_collapse_limit))
420 joinlist = list_concat(leftjoinlist, rightjoinlist);
421 else /* force the join order at this node */
422 joinlist = list_make1(list_make2(leftjoinlist, rightjoinlist));
426 elog(ERROR, "unrecognized node type: %d",
427 (int) nodeTag(jtnode));
428 joinlist = NIL; /* keep compiler quiet */
435 * Build an OuterJoinInfo for the current outer join
438 * left_rels: the base Relids syntactically on outer side of join
439 * right_rels: the base Relids syntactically on inner side of join
440 * is_full_join: what it says
441 * clause: the outer join's join condition
443 * If the join is a RIGHT JOIN, left_rels and right_rels are switched by
444 * the caller, so that left_rels is always the nonnullable side. Hence
445 * we need only distinguish the LEFT and FULL cases.
447 * The node should eventually be put into root->oj_info_list, but we
448 * do not do that here.
450 static OuterJoinInfo *
451 make_outerjoininfo(PlannerInfo *root,
452 Relids left_rels, Relids right_rels,
453 bool is_full_join, Node *clause)
455 OuterJoinInfo *ojinfo = makeNode(OuterJoinInfo);
456 Relids clause_relids;
457 Relids strict_relids;
460 /* If it's a full join, no need to be very smart */
461 ojinfo->is_full_join = is_full_join;
464 ojinfo->min_lefthand = left_rels;
465 ojinfo->min_righthand = right_rels;
466 ojinfo->lhs_strict = false; /* don't care about this */
471 * Retrieve all relids mentioned within the join clause.
473 clause_relids = pull_varnos(clause);
476 * For which relids is the clause strict, ie, it cannot succeed if the
477 * rel's columns are all NULL?
479 strict_relids = find_nonnullable_rels(clause);
481 /* Remember whether the clause is strict for any LHS relations */
482 ojinfo->lhs_strict = bms_overlap(strict_relids, left_rels);
485 * Required LHS is basically the LHS rels mentioned in the clause...
486 * but if there aren't any, punt and make it the full LHS, to avoid
487 * having an empty min_lefthand which will confuse later processing.
488 * (We don't try to be smart about such cases, just correct.)
489 * We may have to add more rels based on lower outer joins; see below.
491 ojinfo->min_lefthand = bms_intersect(clause_relids, left_rels);
492 if (bms_is_empty(ojinfo->min_lefthand))
493 ojinfo->min_lefthand = bms_copy(left_rels);
496 * Required RHS is normally the full set of RHS rels. Sometimes we
497 * can exclude some, see below.
499 ojinfo->min_righthand = bms_copy(right_rels);
501 foreach(l, root->oj_info_list)
503 OuterJoinInfo *otherinfo = (OuterJoinInfo *) lfirst(l);
505 /* ignore full joins --- other mechanisms preserve their ordering */
506 if (otherinfo->is_full_join)
510 * For a lower OJ in our LHS, if our join condition uses the lower
511 * join's RHS and is not strict for that rel, we must preserve the
512 * ordering of the two OJs, so add lower OJ's full required relset to
515 if (bms_overlap(ojinfo->min_lefthand, otherinfo->min_righthand) &&
516 !bms_overlap(strict_relids, otherinfo->min_righthand))
518 ojinfo->min_lefthand = bms_add_members(ojinfo->min_lefthand,
519 otherinfo->min_lefthand);
520 ojinfo->min_lefthand = bms_add_members(ojinfo->min_lefthand,
521 otherinfo->min_righthand);
524 * For a lower OJ in our RHS, if our join condition does not use the
525 * lower join's RHS and the lower OJ's join condition is strict, we
526 * can interchange the ordering of the two OJs, so exclude the lower
527 * RHS from our min_righthand.
529 if (bms_overlap(ojinfo->min_righthand, otherinfo->min_righthand) &&
530 !bms_overlap(clause_relids, otherinfo->min_righthand) &&
531 otherinfo->lhs_strict)
533 ojinfo->min_righthand = bms_del_members(ojinfo->min_righthand,
534 otherinfo->min_righthand);
538 /* Neither set should be empty, else we might get confused later */
539 Assert(!bms_is_empty(ojinfo->min_lefthand));
540 Assert(!bms_is_empty(ojinfo->min_righthand));
541 /* Shouldn't overlap either */
542 Assert(!bms_overlap(ojinfo->min_lefthand, ojinfo->min_righthand));
548 /*****************************************************************************
552 *****************************************************************************/
555 * distribute_qual_to_rels
556 * Add clause information to either the baserestrictinfo or joininfo list
557 * (depending on whether the clause is a join) of each base relation
558 * mentioned in the clause. A RestrictInfo node is created and added to
559 * the appropriate list for each rel. Also, if the clause uses a
560 * mergejoinable operator and is not delayed by outer-join rules, enter
561 * the left- and right-side expressions into the query's lists of
564 * 'clause': the qual clause to be distributed
565 * 'is_pushed_down': if TRUE, force the clause to be marked 'is_pushed_down'
566 * (this indicates the clause came from a FromExpr, not a JoinExpr)
567 * 'is_deduced': TRUE if the qual came from implied-equality deduction
568 * 'below_outer_join': TRUE if the qual is from a JOIN/ON that is below the
569 * nullable side of a higher-level outer join.
570 * 'qualscope': set of baserels the qual's syntactic scope covers
571 * 'ojscope': NULL if not an outer-join qual, else the minimum set of baserels
572 * needed to form this join
573 * 'outerjoin_nonnullable': NULL if not an outer-join qual, else the set of
574 * baserels appearing on the outer (nonnullable) side of the join
575 * (for FULL JOIN this includes both sides of the join, and must in fact
578 * 'qualscope' identifies what level of JOIN the qual came from syntactically.
579 * 'ojscope' is needed if we decide to force the qual up to the outer-join
580 * level, which will be ojscope not necessarily qualscope.
583 distribute_qual_to_rels(PlannerInfo *root, Node *clause,
586 bool below_outer_join,
589 Relids outerjoin_nonnullable)
592 bool outerjoin_delayed;
594 bool maybe_outer_join;
595 RestrictInfo *restrictinfo;
600 * Retrieve all relids mentioned within the clause.
602 relids = pull_varnos(clause);
605 * Cross-check: clause should contain no relids not within its scope.
606 * Otherwise the parser messed up.
608 if (!bms_is_subset(relids, qualscope))
609 elog(ERROR, "JOIN qualification may not refer to other relations");
610 if (ojscope && !bms_is_subset(relids, ojscope))
611 elog(ERROR, "JOIN qualification may not refer to other relations");
614 * If the clause is variable-free, we force it to be evaluated at its
615 * original syntactic level. Note that this should not happen for
616 * top-level clauses, because query_planner() special-cases them. But it
617 * will happen for variable-free JOIN/ON clauses. We don't have to be
618 * real smart about such a case, we just have to be correct. Also note
619 * that for an outer-join clause, we must force it to the OJ's semantic
620 * level, not the syntactic scope.
622 if (bms_is_empty(relids))
623 relids = ojscope ? ojscope : qualscope;
626 * Check to see if clause application must be delayed by outer-join
632 * If the qual came from implied-equality deduction, we always
633 * evaluate the qual at its natural semantic level. It is the
634 * responsibility of the deducer not to create any quals that should
635 * be delayed by outer-join rules.
637 Assert(bms_equal(relids, qualscope));
639 /* Needn't feed it back for more deductions */
640 outerjoin_delayed = false;
641 maybe_equijoin = false;
642 maybe_outer_join = false;
644 else if (bms_overlap(relids, outerjoin_nonnullable))
647 * The qual is attached to an outer join and mentions (some of the)
648 * rels on the nonnullable side. Force the qual to be evaluated
649 * exactly at the level of joining corresponding to the outer join. We
650 * cannot let it get pushed down into the nonnullable side, since then
651 * we'd produce no output rows, rather than the intended single
652 * null-extended row, for any nonnullable-side rows failing the qual.
654 * Note: an outer-join qual that mentions only nullable-side rels can
655 * be pushed down into the nullable side without changing the join
656 * result, so we treat it the same as an ordinary inner-join qual,
657 * except for not setting maybe_equijoin (see below).
661 outerjoin_delayed = true;
664 * We can't use such a clause to deduce equijoin (the left and right
665 * sides might be unequal above the join because one of them has gone
666 * to NULL) ... but we might be able to use it for more limited
667 * purposes. Note: for the current uses of deductions from an
668 * outer-join clause, it seems safe to make the deductions even when
669 * the clause is below a higher-level outer join; so we do not check
670 * below_outer_join here.
672 maybe_equijoin = false;
673 maybe_outer_join = true;
678 * For a non-outer-join qual, we can evaluate the qual as soon as (1)
679 * we have all the rels it mentions, and (2) we are at or above any
680 * outer joins that can null any of these rels and are below the
681 * syntactic location of the given qual. To enforce the latter, scan
682 * the oj_info_list and merge the required-relid sets of any such OJs
683 * into the clause's own reference list. At the time we are called,
684 * the oj_info_list contains only outer joins below this qual.
686 Relids addrelids = NULL;
689 outerjoin_delayed = false;
690 foreach(l, root->oj_info_list)
692 OuterJoinInfo *ojinfo = (OuterJoinInfo *) lfirst(l);
694 if (bms_overlap(relids, ojinfo->min_righthand) ||
695 (ojinfo->is_full_join &&
696 bms_overlap(relids, ojinfo->min_lefthand)))
698 addrelids = bms_add_members(addrelids, ojinfo->min_lefthand);
699 addrelids = bms_add_members(addrelids, ojinfo->min_righthand);
700 outerjoin_delayed = true;
704 if (bms_is_subset(addrelids, relids))
707 * Qual is not delayed by any lower outer-join restriction. If it
708 * is not itself below or within an outer join, we can consider it
709 * "valid everywhere", so consider feeding it to the equijoin
710 * machinery. (If it is within an outer join, we can't consider
711 * it "valid everywhere": once the contained variables have gone
712 * to NULL, we'd be asserting things like NULL = NULL, which is
715 if (!below_outer_join && outerjoin_nonnullable == NULL)
716 maybe_equijoin = true;
718 maybe_equijoin = false;
722 relids = bms_union(relids, addrelids);
723 /* Should still be a subset of current scope ... */
724 Assert(bms_is_subset(relids, qualscope));
727 * Because application of the qual will be delayed by outer join,
728 * we mustn't assume its vars are equal everywhere.
730 maybe_equijoin = false;
733 maybe_outer_join = false;
737 * Mark the qual as "pushed down" if it can be applied at a level below
738 * its original syntactic level. This allows us to distinguish original
739 * JOIN/ON quals from higher-level quals pushed down to the same joinrel.
740 * A qual originating from WHERE is always considered "pushed down".
741 * Note that for an outer-join qual, we have to compare to ojscope not
745 is_pushed_down = !bms_equal(relids, ojscope ? ojscope : qualscope);
748 * Build the RestrictInfo node itself.
750 restrictinfo = make_restrictinfo((Expr *) clause,
756 * Figure out where to attach it.
758 switch (bms_membership(relids))
763 * There is only one relation participating in 'clause', so
764 * 'clause' is a restriction clause for that relation.
766 rel = find_base_rel(root, bms_singleton_member(relids));
769 * Check for a "mergejoinable" clause even though it's not a join
770 * clause. This is so that we can recognize that "a.x = a.y"
771 * makes x and y eligible to be considered equal, even when they
772 * belong to the same rel. Without this, we would not recognize
773 * that "a.x = a.y AND a.x = b.z AND a.y = c.q" allows us to
774 * consider z and q equal after their rels are joined.
776 check_mergejoinable(restrictinfo);
779 * If the clause was deduced from implied equality, check to see
780 * whether it is redundant with restriction clauses we already
781 * have for this rel. Note we cannot apply this check to
782 * user-written clauses, since we haven't found the canonical
783 * pathkey sets yet while processing user clauses. (NB: no
784 * comparable check is done in the join-clause case; redundancy
785 * will be detected when the join clause is moved into a join
786 * rel's restriction list.)
789 !qual_is_redundant(root, restrictinfo,
790 rel->baserestrictinfo))
792 /* Add clause to rel's restriction list */
793 rel->baserestrictinfo = lappend(rel->baserestrictinfo,
800 * 'clause' is a join clause, since there is more than one rel in
805 * Check for hash or mergejoinable operators.
807 * We don't bother setting the hashjoin info if we're not going to
808 * need it. We do want to know about mergejoinable ops in all
809 * cases, however, because we use mergejoinable ops for other
810 * purposes such as detecting redundant clauses.
812 check_mergejoinable(restrictinfo);
814 check_hashjoinable(restrictinfo);
817 * Add clause to the join lists of all the relevant relations.
819 add_join_clause_to_rels(root, restrictinfo, relids);
822 * Add vars used in the join clause to targetlists of their
823 * relations, so that they will be emitted by the plan nodes that
824 * scan those relations (else they won't be available at the join
827 vars = pull_var_clause(clause, false);
828 add_vars_to_targetlist(root, vars, relids);
834 * 'clause' references no rels, and therefore we have no place to
835 * attach it. Shouldn't get here if callers are working properly.
837 elog(ERROR, "cannot cope with variable-free clause");
842 * If the clause has a mergejoinable operator, we may be able to deduce
843 * more things from it under the principle of transitivity.
845 * If it is not an outer-join qualification nor bubbled up due to an outer
846 * join, then the two sides represent equivalent PathKeyItems for path
847 * keys: any path that is sorted by one side will also be sorted by the
848 * other (as soon as the two rels are joined, that is). Pass such clauses
849 * to add_equijoined_keys.
851 * If it is a left or right outer-join qualification that relates the two
852 * sides of the outer join (no funny business like leftvar1 = leftvar2 +
853 * rightvar), we add it to root->left_join_clauses or
854 * root->right_join_clauses according to which side the nonnullable
855 * variable appears on.
857 * If it is a full outer-join qualification, we add it to
858 * root->full_join_clauses. (Ideally we'd discard cases that aren't
859 * leftvar = rightvar, as we do for left/right joins, but this routine
860 * doesn't have the info needed to do that; and the current usage of the
861 * full_join_clauses list doesn't require that, so it's not currently
862 * worth complicating this routine's API to make it possible.)
864 if (restrictinfo->mergejoinoperator != InvalidOid)
867 add_equijoined_keys(root, restrictinfo);
868 else if (maybe_outer_join && restrictinfo->can_join)
870 if (bms_is_subset(restrictinfo->left_relids,
871 outerjoin_nonnullable) &&
872 !bms_overlap(restrictinfo->right_relids,
873 outerjoin_nonnullable))
875 /* we have outervar = innervar */
876 root->left_join_clauses = lappend(root->left_join_clauses,
879 else if (bms_is_subset(restrictinfo->right_relids,
880 outerjoin_nonnullable) &&
881 !bms_overlap(restrictinfo->left_relids,
882 outerjoin_nonnullable))
884 /* we have innervar = outervar */
885 root->right_join_clauses = lappend(root->right_join_clauses,
888 else if (bms_equal(outerjoin_nonnullable, qualscope))
890 /* FULL JOIN (above tests cannot match in this case) */
891 root->full_join_clauses = lappend(root->full_join_clauses,
899 * process_implied_equality
900 * Check to see whether we already have a restrictinfo item that says
901 * item1 = item2, and create one if not; or if delete_it is true,
902 * remove any such restrictinfo item.
904 * This processing is a consequence of transitivity of mergejoin equality:
905 * if we have mergejoinable clauses A = B and B = C, we can deduce A = C
906 * (where = is an appropriate mergejoinable operator). See path/pathkeys.c
910 process_implied_equality(PlannerInfo *root,
911 Node *item1, Node *item2,
912 Oid sortop1, Oid sortop2,
913 Relids item1_relids, Relids item2_relids,
917 BMS_Membership membership;
923 Operator eq_operator;
924 Form_pg_operator pgopform;
927 /* Get set of relids referenced in the two expressions */
928 relids = bms_union(item1_relids, item2_relids);
929 membership = bms_membership(relids);
932 * generate_implied_equalities() shouldn't call me on two constants.
934 Assert(membership != BMS_EMPTY_SET);
937 * If the exprs involve a single rel, we need to look at that rel's
938 * baserestrictinfo list. If multiple rels, we can scan the joininfo list
941 if (membership == BMS_SINGLETON)
943 rel1 = find_base_rel(root, bms_singleton_member(relids));
944 restrictlist = rel1->baserestrictinfo;
951 /* Copy relids, find and remove one member */
952 other_rels = bms_copy(relids);
953 first_rel = bms_first_member(other_rels);
954 bms_free(other_rels);
956 rel1 = find_base_rel(root, first_rel);
957 restrictlist = rel1->joininfo;
961 * Scan to see if equality is already known. If so, we're done in the add
962 * case, and done after removing it in the delete case.
964 foreach(itm, restrictlist)
966 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(itm);
970 if (restrictinfo->mergejoinoperator == InvalidOid)
971 continue; /* ignore non-mergejoinable clauses */
972 /* We now know the restrictinfo clause is a binary opclause */
973 left = get_leftop(restrictinfo->clause);
974 right = get_rightop(restrictinfo->clause);
975 if ((equal(item1, left) && equal(item2, right)) ||
976 (equal(item2, left) && equal(item1, right)))
978 /* found a matching clause */
981 if (membership == BMS_SINGLETON)
983 /* delete it from local restrictinfo list */
984 rel1->baserestrictinfo = list_delete_ptr(rel1->baserestrictinfo,
989 /* let joininfo.c do it */
990 remove_join_clause_from_rels(root, restrictinfo, relids);
997 /* Didn't find it. Done if deletion requested */
1002 * This equality is new information, so construct a clause representing it
1003 * to add to the query data structures.
1005 ltype = exprType(item1);
1006 rtype = exprType(item2);
1007 eq_operator = compatible_oper(list_make1(makeString("=")),
1008 ltype, rtype, true);
1009 if (!HeapTupleIsValid(eq_operator))
1012 * Would it be safe to just not add the equality to the query if we
1013 * have no suitable equality operator for the combination of
1014 * datatypes? NO, because sortkey selection may screw up anyway.
1017 (errcode(ERRCODE_UNDEFINED_FUNCTION),
1018 errmsg("could not identify an equality operator for types %s and %s",
1019 format_type_be(ltype), format_type_be(rtype))));
1021 pgopform = (Form_pg_operator) GETSTRUCT(eq_operator);
1024 * Let's just make sure this appears to be a compatible operator.
1026 if (pgopform->oprlsortop != sortop1 ||
1027 pgopform->oprrsortop != sortop2 ||
1028 pgopform->oprresult != BOOLOID)
1030 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1031 errmsg("equality operator for types %s and %s should be merge-joinable, but isn't",
1032 format_type_be(ltype), format_type_be(rtype))));
1035 * Now we can build the new clause. Copy to ensure it shares no
1036 * substructure with original (this is necessary in case there are
1037 * subselects in there...)
1039 clause = make_opclause(oprid(eq_operator), /* opno */
1040 BOOLOID, /* opresulttype */
1041 false, /* opretset */
1042 (Expr *) copyObject(item1),
1043 (Expr *) copyObject(item2));
1045 ReleaseSysCache(eq_operator);
1048 * Push the new clause into all the appropriate restrictinfo lists.
1050 * Note: we mark the qual "pushed down" to ensure that it can never be
1051 * taken for an original JOIN/ON clause.
1053 distribute_qual_to_rels(root, (Node *) clause,
1054 true, true, false, relids, NULL, NULL);
1059 * Detect whether an implied-equality qual that turns out to be a
1060 * restriction clause for a single base relation is redundant with
1061 * already-known restriction clauses for that rel. This occurs with,
1063 * SELECT * FROM tab WHERE f1 = f2 AND f2 = f3;
1064 * We need to suppress the redundant condition to avoid computing
1065 * too-small selectivity, not to mention wasting time at execution.
1067 * Note: quals of the form "var = const" are never considered redundant,
1068 * only those of the form "var = var". This is needed because when we
1069 * have constants in an implied-equality set, we use a different strategy
1070 * that suppresses all "var = var" deductions. We must therefore keep
1071 * all the "var = const" quals.
1074 qual_is_redundant(PlannerInfo *root,
1075 RestrictInfo *restrictinfo,
1085 /* Never redundant unless vars appear on both sides */
1086 if (bms_is_empty(restrictinfo->left_relids) ||
1087 bms_is_empty(restrictinfo->right_relids))
1090 newleft = get_leftop(restrictinfo->clause);
1091 newright = get_rightop(restrictinfo->clause);
1094 * Set cached pathkeys. NB: it is okay to do this now because this
1095 * routine is only invoked while we are generating implied equalities.
1096 * Therefore, the equi_key_list is already complete and so we can
1097 * correctly determine canonical pathkeys.
1099 cache_mergeclause_pathkeys(root, restrictinfo);
1100 /* If different, say "not redundant" (should never happen) */
1101 if (restrictinfo->left_pathkey != restrictinfo->right_pathkey)
1105 * Scan existing quals to find those referencing same pathkeys. Usually
1106 * there will be few, if any, so build a list of just the interesting
1110 foreach(olditem, restrictlist)
1112 RestrictInfo *oldrinfo = (RestrictInfo *) lfirst(olditem);
1114 if (oldrinfo->mergejoinoperator != InvalidOid)
1116 cache_mergeclause_pathkeys(root, oldrinfo);
1117 if (restrictinfo->left_pathkey == oldrinfo->left_pathkey &&
1118 restrictinfo->right_pathkey == oldrinfo->right_pathkey)
1119 oldquals = lcons(oldrinfo, oldquals);
1122 if (oldquals == NIL)
1126 * Now, we want to develop a list of exprs that are known equal to the
1127 * left side of the new qual. We traverse the old-quals list repeatedly
1128 * to transitively expand the exprs list. If at any point we find we can
1129 * reach the right-side expr of the new qual, we are done. We give up
1130 * when we can't expand the equalexprs list any more.
1132 equalexprs = list_make1(newleft);
1136 /* cannot use foreach here because of possible list_delete */
1137 olditem = list_head(oldquals);
1140 RestrictInfo *oldrinfo = (RestrictInfo *) lfirst(olditem);
1141 Node *oldleft = get_leftop(oldrinfo->clause);
1142 Node *oldright = get_rightop(oldrinfo->clause);
1143 Node *newguy = NULL;
1145 /* must advance olditem before list_delete possibly pfree's it */
1146 olditem = lnext(olditem);
1148 if (list_member(equalexprs, oldleft))
1150 else if (list_member(equalexprs, oldright))
1154 if (equal(newguy, newright))
1155 return true; /* we proved new clause is redundant */
1156 equalexprs = lcons(newguy, equalexprs);
1160 * Remove this qual from list, since we don't need it anymore.
1162 oldquals = list_delete_ptr(oldquals, oldrinfo);
1164 } while (someadded);
1166 return false; /* it's not redundant */
1170 /*****************************************************************************
1172 * CHECKS FOR MERGEJOINABLE AND HASHJOINABLE CLAUSES
1174 *****************************************************************************/
1177 * check_mergejoinable
1178 * If the restrictinfo's clause is mergejoinable, set the mergejoin
1179 * info fields in the restrictinfo.
1181 * Currently, we support mergejoin for binary opclauses where
1182 * the operator is a mergejoinable operator. The arguments can be
1183 * anything --- as long as there are no volatile functions in them.
1186 check_mergejoinable(RestrictInfo *restrictinfo)
1188 Expr *clause = restrictinfo->clause;
1193 if (!is_opclause(clause))
1195 if (list_length(((OpExpr *) clause)->args) != 2)
1198 opno = ((OpExpr *) clause)->opno;
1200 if (op_mergejoinable(opno,
1203 !contain_volatile_functions((Node *) clause))
1205 restrictinfo->mergejoinoperator = opno;
1206 restrictinfo->left_sortop = leftOp;
1207 restrictinfo->right_sortop = rightOp;
1212 * check_hashjoinable
1213 * If the restrictinfo's clause is hashjoinable, set the hashjoin
1214 * info fields in the restrictinfo.
1216 * Currently, we support hashjoin for binary opclauses where
1217 * the operator is a hashjoinable operator. The arguments can be
1218 * anything --- as long as there are no volatile functions in them.
1221 check_hashjoinable(RestrictInfo *restrictinfo)
1223 Expr *clause = restrictinfo->clause;
1226 if (!is_opclause(clause))
1228 if (list_length(((OpExpr *) clause)->args) != 2)
1231 opno = ((OpExpr *) clause)->opno;
1233 if (op_hashjoinable(opno) &&
1234 !contain_volatile_functions((Node *) clause))
1235 restrictinfo->hashjoinoperator = opno;