Disallow empty passwords in LDAP authentication, the same way
[PostgreSQL.git] / src / backend / optimizer / path / joinrels.c
blob1007cf05339a073c575d03de892c6fdb0eee0a84
1 /*-------------------------------------------------------------------------
3 * joinrels.c
4 * Routines to determine which relations should be joined
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * IDENTIFICATION
11 * $PostgreSQL$
13 *-------------------------------------------------------------------------
15 #include "postgres.h"
17 #include "optimizer/joininfo.h"
18 #include "optimizer/pathnode.h"
19 #include "optimizer/paths.h"
22 static List *make_rels_by_clause_joins(PlannerInfo *root,
23 RelOptInfo *old_rel,
24 ListCell *other_rels);
25 static List *make_rels_by_clauseless_joins(PlannerInfo *root,
26 RelOptInfo *old_rel,
27 ListCell *other_rels);
28 static bool has_join_restriction(PlannerInfo *root, RelOptInfo *rel);
29 static bool has_legal_joinclause(PlannerInfo *root, RelOptInfo *rel);
30 static bool is_dummy_rel(RelOptInfo *rel);
31 static void mark_dummy_rel(RelOptInfo *rel);
32 static bool restriction_is_constant_false(List *restrictlist);
36 * join_search_one_level
37 * Consider ways to produce join relations containing exactly 'level'
38 * jointree items. (This is one step of the dynamic-programming method
39 * embodied in standard_join_search.) Join rel nodes for each feasible
40 * combination of lower-level rels are created and returned in a list.
41 * Implementation paths are created for each such joinrel, too.
43 * level: level of rels we want to make this time.
44 * joinrels[j], 1 <= j < level, is a list of rels containing j items.
46 List *
47 join_search_one_level(PlannerInfo *root, int level, List **joinrels)
49 List *result_rels = NIL;
50 List *new_rels;
51 ListCell *r;
52 int k;
55 * First, consider left-sided and right-sided plans, in which rels of
56 * exactly level-1 member relations are joined against initial relations.
57 * We prefer to join using join clauses, but if we find a rel of level-1
58 * members that has no join clauses, we will generate Cartesian-product
59 * joins against all initial rels not already contained in it.
61 * In the first pass (level == 2), we try to join each initial rel to each
62 * initial rel that appears later in joinrels[1]. (The mirror-image joins
63 * are handled automatically by make_join_rel.) In later passes, we try
64 * to join rels of size level-1 from joinrels[level-1] to each initial rel
65 * in joinrels[1].
67 foreach(r, joinrels[level - 1])
69 RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
70 ListCell *other_rels;
72 if (level == 2)
73 other_rels = lnext(r); /* only consider remaining initial
74 * rels */
75 else
76 other_rels = list_head(joinrels[1]); /* consider all initial
77 * rels */
79 if (old_rel->joininfo != NIL || old_rel->has_eclass_joins ||
80 has_join_restriction(root, old_rel))
83 * Note that if all available join clauses for this rel require
84 * more than one other rel, we will fail to make any joins against
85 * it here. In most cases that's OK; it'll be considered by
86 * "bushy plan" join code in a higher-level pass where we have
87 * those other rels collected into a join rel.
89 * See also the last-ditch case below.
91 new_rels = make_rels_by_clause_joins(root,
92 old_rel,
93 other_rels);
95 else
98 * Oops, we have a relation that is not joined to any other
99 * relation, either directly or by join-order restrictions.
100 * Cartesian product time.
102 new_rels = make_rels_by_clauseless_joins(root,
103 old_rel,
104 other_rels);
108 * At levels above 2 we will generate the same joined relation in
109 * multiple ways --- for example (a join b) join c is the same
110 * RelOptInfo as (b join c) join a, though the second case will add a
111 * different set of Paths to it. To avoid making extra work for
112 * subsequent passes, do not enter the same RelOptInfo into our output
113 * list multiple times.
115 result_rels = list_concat_unique_ptr(result_rels, new_rels);
119 * Now, consider "bushy plans" in which relations of k initial rels are
120 * joined to relations of level-k initial rels, for 2 <= k <= level-2.
122 * We only consider bushy-plan joins for pairs of rels where there is a
123 * suitable join clause (or join order restriction), in order to avoid
124 * unreasonable growth of planning time.
126 for (k = 2;; k++)
128 int other_level = level - k;
131 * Since make_join_rel(x, y) handles both x,y and y,x cases, we only
132 * need to go as far as the halfway point.
134 if (k > other_level)
135 break;
137 foreach(r, joinrels[k])
139 RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
140 ListCell *other_rels;
141 ListCell *r2;
144 * We can ignore clauseless joins here, *except* when they
145 * participate in join-order restrictions --- then we might have
146 * to force a bushy join plan.
148 if (old_rel->joininfo == NIL && !old_rel->has_eclass_joins &&
149 !has_join_restriction(root, old_rel))
150 continue;
152 if (k == other_level)
153 other_rels = lnext(r); /* only consider remaining rels */
154 else
155 other_rels = list_head(joinrels[other_level]);
157 for_each_cell(r2, other_rels)
159 RelOptInfo *new_rel = (RelOptInfo *) lfirst(r2);
161 if (!bms_overlap(old_rel->relids, new_rel->relids))
164 * OK, we can build a rel of the right level from this
165 * pair of rels. Do so if there is at least one usable
166 * join clause or a relevant join restriction.
168 if (have_relevant_joinclause(root, old_rel, new_rel) ||
169 have_join_order_restriction(root, old_rel, new_rel))
171 RelOptInfo *jrel;
173 jrel = make_join_rel(root, old_rel, new_rel);
174 /* Avoid making duplicate entries ... */
175 if (jrel)
176 result_rels = list_append_unique_ptr(result_rels,
177 jrel);
185 * Last-ditch effort: if we failed to find any usable joins so far, force
186 * a set of cartesian-product joins to be generated. This handles the
187 * special case where all the available rels have join clauses but we
188 * cannot use any of those clauses yet. An example is
190 * SELECT * FROM a,b,c WHERE (a.f1 + b.f2 + c.f3) = 0;
192 * The join clause will be usable at level 3, but at level 2 we have no
193 * choice but to make cartesian joins. We consider only left-sided and
194 * right-sided cartesian joins in this case (no bushy).
196 if (result_rels == NIL)
199 * This loop is just like the first one, except we always call
200 * make_rels_by_clauseless_joins().
202 foreach(r, joinrels[level - 1])
204 RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
205 ListCell *other_rels;
207 if (level == 2)
208 other_rels = lnext(r); /* only consider remaining initial
209 * rels */
210 else
211 other_rels = list_head(joinrels[1]); /* consider all initial
212 * rels */
214 new_rels = make_rels_by_clauseless_joins(root,
215 old_rel,
216 other_rels);
218 result_rels = list_concat_unique_ptr(result_rels, new_rels);
221 /*----------
222 * When special joins are involved, there may be no legal way
223 * to make an N-way join for some values of N. For example consider
225 * SELECT ... FROM t1 WHERE
226 * x IN (SELECT ... FROM t2,t3 WHERE ...) AND
227 * y IN (SELECT ... FROM t4,t5 WHERE ...)
229 * We will flatten this query to a 5-way join problem, but there are
230 * no 4-way joins that join_is_legal() will consider legal. We have
231 * to accept failure at level 4 and go on to discover a workable
232 * bushy plan at level 5.
234 * However, if there are no special joins then join_is_legal() should
235 * never fail, and so the following sanity check is useful.
236 *----------
238 if (result_rels == NIL && root->join_info_list == NIL)
239 elog(ERROR, "failed to build any %d-way joins", level);
242 return result_rels;
246 * make_rels_by_clause_joins
247 * Build joins between the given relation 'old_rel' and other relations
248 * that participate in join clauses that 'old_rel' also participates in
249 * (or participate in join-order restrictions with it).
250 * The join rel nodes are returned in a list.
252 * 'old_rel' is the relation entry for the relation to be joined
253 * 'other_rels': the first cell in a linked list containing the other
254 * rels to be considered for joining
256 * Currently, this is only used with initial rels in other_rels, but it
257 * will work for joining to joinrels too.
259 static List *
260 make_rels_by_clause_joins(PlannerInfo *root,
261 RelOptInfo *old_rel,
262 ListCell *other_rels)
264 List *result = NIL;
265 ListCell *l;
267 for_each_cell(l, other_rels)
269 RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
271 if (!bms_overlap(old_rel->relids, other_rel->relids) &&
272 (have_relevant_joinclause(root, old_rel, other_rel) ||
273 have_join_order_restriction(root, old_rel, other_rel)))
275 RelOptInfo *jrel;
277 jrel = make_join_rel(root, old_rel, other_rel);
278 if (jrel)
279 result = lcons(jrel, result);
283 return result;
287 * make_rels_by_clauseless_joins
288 * Given a relation 'old_rel' and a list of other relations
289 * 'other_rels', create a join relation between 'old_rel' and each
290 * member of 'other_rels' that isn't already included in 'old_rel'.
291 * The join rel nodes are returned in a list.
293 * 'old_rel' is the relation entry for the relation to be joined
294 * 'other_rels': the first cell of a linked list containing the
295 * other rels to be considered for joining
297 * Currently, this is only used with initial rels in other_rels, but it would
298 * work for joining to joinrels too.
300 static List *
301 make_rels_by_clauseless_joins(PlannerInfo *root,
302 RelOptInfo *old_rel,
303 ListCell *other_rels)
305 List *result = NIL;
306 ListCell *i;
308 for_each_cell(i, other_rels)
310 RelOptInfo *other_rel = (RelOptInfo *) lfirst(i);
312 if (!bms_overlap(other_rel->relids, old_rel->relids))
314 RelOptInfo *jrel;
316 jrel = make_join_rel(root, old_rel, other_rel);
319 * As long as given other_rels are distinct, don't need to test to
320 * see if jrel is already part of output list.
322 if (jrel)
323 result = lcons(jrel, result);
327 return result;
332 * join_is_legal
333 * Determine whether a proposed join is legal given the query's
334 * join order constraints; and if it is, determine the join type.
336 * Caller must supply not only the two rels, but the union of their relids.
337 * (We could simplify the API by computing joinrelids locally, but this
338 * would be redundant work in the normal path through make_join_rel.)
340 * On success, *sjinfo_p is set to NULL if this is to be a plain inner join,
341 * else it's set to point to the associated SpecialJoinInfo node. Also,
342 * *reversed_p is set TRUE if the given relations need to be swapped to
343 * match the SpecialJoinInfo node.
345 static bool
346 join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
347 Relids joinrelids,
348 SpecialJoinInfo **sjinfo_p, bool *reversed_p)
350 SpecialJoinInfo *match_sjinfo;
351 bool reversed;
352 bool is_valid_inner;
353 ListCell *l;
356 * Ensure output params are set on failure return. This is just to
357 * suppress uninitialized-variable warnings from overly anal compilers.
359 *sjinfo_p = NULL;
360 *reversed_p = false;
363 * If we have any special joins, the proposed join might be illegal; and
364 * in any case we have to determine its join type. Scan the join info
365 * list for conflicts.
367 match_sjinfo = NULL;
368 reversed = false;
369 is_valid_inner = true;
371 foreach(l, root->join_info_list)
373 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
376 * This special join is not relevant unless its RHS overlaps the
377 * proposed join. (Check this first as a fast path for dismissing
378 * most irrelevant SJs quickly.)
380 if (!bms_overlap(sjinfo->min_righthand, joinrelids))
381 continue;
384 * Also, not relevant if proposed join is fully contained within RHS
385 * (ie, we're still building up the RHS).
387 if (bms_is_subset(joinrelids, sjinfo->min_righthand))
388 continue;
391 * Also, not relevant if SJ is already done within either input.
393 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
394 bms_is_subset(sjinfo->min_righthand, rel1->relids))
395 continue;
396 if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
397 bms_is_subset(sjinfo->min_righthand, rel2->relids))
398 continue;
401 * If one input contains min_lefthand and the other contains
402 * min_righthand, then we can perform the SJ at this join.
404 * Barf if we get matches to more than one SJ (is that possible?)
406 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
407 bms_is_subset(sjinfo->min_righthand, rel2->relids))
409 if (match_sjinfo)
410 return false; /* invalid join path */
411 match_sjinfo = sjinfo;
412 reversed = false;
414 else if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
415 bms_is_subset(sjinfo->min_righthand, rel1->relids))
417 if (match_sjinfo)
418 return false; /* invalid join path */
419 match_sjinfo = sjinfo;
420 reversed = true;
422 else if (sjinfo->jointype == JOIN_SEMI &&
423 bms_equal(sjinfo->syn_righthand, rel2->relids) &&
424 create_unique_path(root, rel2, rel2->cheapest_total_path,
425 sjinfo) != NULL)
427 /*----------
428 * For a semijoin, we can join the RHS to anything else by
429 * unique-ifying the RHS (if the RHS can be unique-ified).
430 * We will only get here if we have the full RHS but less
431 * than min_lefthand on the LHS.
433 * The reason to consider such a join path is exemplified by
434 * SELECT ... FROM a,b WHERE (a.x,b.y) IN (SELECT c1,c2 FROM c)
435 * If we insist on doing this as a semijoin we will first have
436 * to form the cartesian product of A*B. But if we unique-ify
437 * C then the semijoin becomes a plain innerjoin and we can join
438 * in any order, eg C to A and then to B. When C is much smaller
439 * than A and B this can be a huge win. So we allow C to be
440 * joined to just A or just B here, and then make_join_rel has
441 * to handle the case properly.
443 * Note that actually we'll allow unique-ified C to be joined to
444 * some other relation D here, too. That is legal, if usually not
445 * very sane, and this routine is only concerned with legality not
446 * with whether the join is good strategy.
447 *----------
449 if (match_sjinfo)
450 return false; /* invalid join path */
451 match_sjinfo = sjinfo;
452 reversed = false;
454 else if (sjinfo->jointype == JOIN_SEMI &&
455 bms_equal(sjinfo->syn_righthand, rel1->relids) &&
456 create_unique_path(root, rel1, rel1->cheapest_total_path,
457 sjinfo) != NULL)
459 /* Reversed semijoin case */
460 if (match_sjinfo)
461 return false; /* invalid join path */
462 match_sjinfo = sjinfo;
463 reversed = true;
465 else
467 /*----------
468 * Otherwise, the proposed join overlaps the RHS but isn't
469 * a valid implementation of this SJ. It might still be
470 * a legal join, however. If both inputs overlap the RHS,
471 * assume that it's OK. Since the inputs presumably got past
472 * this function's checks previously, they can't overlap the
473 * LHS and their violations of the RHS boundary must represent
474 * SJs that have been determined to commute with this one.
475 * We have to allow this to work correctly in cases like
476 * (a LEFT JOIN (b JOIN (c LEFT JOIN d)))
477 * when the c/d join has been determined to commute with the join
478 * to a, and hence d is not part of min_righthand for the upper
479 * join. It should be legal to join b to c/d but this will appear
480 * as a violation of the upper join's RHS.
481 * Furthermore, if one input overlaps the RHS and the other does
482 * not, we should still allow the join if it is a valid
483 * implementation of some other SJ. We have to allow this to
484 * support the associative identity
485 * (a LJ b on Pab) LJ c ON Pbc = a LJ (b LJ c ON Pbc) on Pab
486 * since joining B directly to C violates the lower SJ's RHS.
487 * We assume that make_outerjoininfo() set things up correctly
488 * so that we'll only match to some SJ if the join is valid.
489 * Set flag here to check at bottom of loop.
491 * For a semijoin, assume it's okay if either side fully contains
492 * the RHS (per the unique-ification case above).
493 *----------
495 if (sjinfo->jointype != JOIN_SEMI &&
496 bms_overlap(rel1->relids, sjinfo->min_righthand) &&
497 bms_overlap(rel2->relids, sjinfo->min_righthand))
499 /* seems OK */
500 Assert(!bms_overlap(joinrelids, sjinfo->min_lefthand));
502 else if (sjinfo->jointype == JOIN_SEMI &&
503 (bms_is_subset(sjinfo->syn_righthand, rel1->relids) ||
504 bms_is_subset(sjinfo->syn_righthand, rel2->relids)))
506 /* seems OK */
508 else
509 is_valid_inner = false;
513 /* Fail if violated some SJ's RHS and didn't match to another SJ */
514 if (match_sjinfo == NULL && !is_valid_inner)
515 return false; /* invalid join path */
517 /* Otherwise, it's a valid join */
518 *sjinfo_p = match_sjinfo;
519 *reversed_p = reversed;
520 return true;
525 * make_join_rel
526 * Find or create a join RelOptInfo that represents the join of
527 * the two given rels, and add to it path information for paths
528 * created with the two rels as outer and inner rel.
529 * (The join rel may already contain paths generated from other
530 * pairs of rels that add up to the same set of base rels.)
532 * NB: will return NULL if attempted join is not valid. This can happen
533 * when working with outer joins, or with IN or EXISTS clauses that have been
534 * turned into joins.
536 RelOptInfo *
537 make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
539 Relids joinrelids;
540 SpecialJoinInfo *sjinfo;
541 bool reversed;
542 SpecialJoinInfo sjinfo_data;
543 RelOptInfo *joinrel;
544 List *restrictlist;
546 /* We should never try to join two overlapping sets of rels. */
547 Assert(!bms_overlap(rel1->relids, rel2->relids));
549 /* Construct Relids set that identifies the joinrel. */
550 joinrelids = bms_union(rel1->relids, rel2->relids);
552 /* Check validity and determine join type. */
553 if (!join_is_legal(root, rel1, rel2, joinrelids,
554 &sjinfo, &reversed))
556 /* invalid join path */
557 bms_free(joinrelids);
558 return NULL;
561 /* Swap rels if needed to match the join info. */
562 if (reversed)
564 RelOptInfo *trel = rel1;
566 rel1 = rel2;
567 rel2 = trel;
571 * If it's a plain inner join, then we won't have found anything in
572 * join_info_list. Make up a SpecialJoinInfo so that selectivity
573 * estimation functions will know what's being joined.
575 if (sjinfo == NULL)
577 sjinfo = &sjinfo_data;
578 sjinfo->type = T_SpecialJoinInfo;
579 sjinfo->min_lefthand = rel1->relids;
580 sjinfo->min_righthand = rel2->relids;
581 sjinfo->syn_lefthand = rel1->relids;
582 sjinfo->syn_righthand = rel2->relids;
583 sjinfo->jointype = JOIN_INNER;
584 /* we don't bother trying to make the remaining fields valid */
585 sjinfo->lhs_strict = false;
586 sjinfo->delay_upper_joins = false;
587 sjinfo->join_quals = NIL;
591 * Find or build the join RelOptInfo, and compute the restrictlist that
592 * goes with this particular joining.
594 joinrel = build_join_rel(root, joinrelids, rel1, rel2, sjinfo,
595 &restrictlist);
598 * If we've already proven this join is empty, we needn't consider any
599 * more paths for it.
601 if (is_dummy_rel(joinrel))
603 bms_free(joinrelids);
604 return joinrel;
608 * Consider paths using each rel as both outer and inner. Depending on
609 * the join type, a provably empty outer or inner rel might mean the join
610 * is provably empty too; in which case throw away any previously computed
611 * paths and mark the join as dummy. (We do it this way since it's
612 * conceivable that dummy-ness of a multi-element join might only be
613 * noticeable for certain construction paths.)
615 * Also, a provably constant-false join restriction typically means that
616 * we can skip evaluating one or both sides of the join. We do this by
617 * marking the appropriate rel as dummy.
619 * We need only consider the jointypes that appear in join_info_list, plus
620 * JOIN_INNER.
622 switch (sjinfo->jointype)
624 case JOIN_INNER:
625 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
626 restriction_is_constant_false(restrictlist))
628 mark_dummy_rel(joinrel);
629 break;
631 add_paths_to_joinrel(root, joinrel, rel1, rel2,
632 JOIN_INNER, sjinfo,
633 restrictlist);
634 add_paths_to_joinrel(root, joinrel, rel2, rel1,
635 JOIN_INNER, sjinfo,
636 restrictlist);
637 break;
638 case JOIN_LEFT:
639 if (is_dummy_rel(rel1))
641 mark_dummy_rel(joinrel);
642 break;
644 if (restriction_is_constant_false(restrictlist) &&
645 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
646 mark_dummy_rel(rel2);
647 add_paths_to_joinrel(root, joinrel, rel1, rel2,
648 JOIN_LEFT, sjinfo,
649 restrictlist);
650 add_paths_to_joinrel(root, joinrel, rel2, rel1,
651 JOIN_RIGHT, sjinfo,
652 restrictlist);
653 break;
654 case JOIN_FULL:
655 if (is_dummy_rel(rel1) && is_dummy_rel(rel2))
657 mark_dummy_rel(joinrel);
658 break;
660 add_paths_to_joinrel(root, joinrel, rel1, rel2,
661 JOIN_FULL, sjinfo,
662 restrictlist);
663 add_paths_to_joinrel(root, joinrel, rel2, rel1,
664 JOIN_FULL, sjinfo,
665 restrictlist);
666 break;
667 case JOIN_SEMI:
670 * We might have a normal semijoin, or a case where we don't have
671 * enough rels to do the semijoin but can unique-ify the RHS and
672 * then do an innerjoin (see comments in join_is_legal). In the
673 * latter case we can't apply JOIN_SEMI joining.
675 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
676 bms_is_subset(sjinfo->min_righthand, rel2->relids))
678 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
679 restriction_is_constant_false(restrictlist))
681 mark_dummy_rel(joinrel);
682 break;
684 add_paths_to_joinrel(root, joinrel, rel1, rel2,
685 JOIN_SEMI, sjinfo,
686 restrictlist);
690 * If we know how to unique-ify the RHS and one input rel is
691 * exactly the RHS (not a superset) we can consider unique-ifying
692 * it and then doing a regular join. (The create_unique_path
693 * check here is probably redundant with what join_is_legal did,
694 * but if so the check is cheap because it's cached. So test
695 * anyway to be sure.)
697 if (bms_equal(sjinfo->syn_righthand, rel2->relids) &&
698 create_unique_path(root, rel2, rel2->cheapest_total_path,
699 sjinfo) != NULL)
701 add_paths_to_joinrel(root, joinrel, rel1, rel2,
702 JOIN_UNIQUE_INNER, sjinfo,
703 restrictlist);
704 add_paths_to_joinrel(root, joinrel, rel2, rel1,
705 JOIN_UNIQUE_OUTER, sjinfo,
706 restrictlist);
708 break;
709 case JOIN_ANTI:
710 if (is_dummy_rel(rel1))
712 mark_dummy_rel(joinrel);
713 break;
715 if (restriction_is_constant_false(restrictlist) &&
716 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
717 mark_dummy_rel(rel2);
718 add_paths_to_joinrel(root, joinrel, rel1, rel2,
719 JOIN_ANTI, sjinfo,
720 restrictlist);
721 break;
722 default:
723 /* other values not expected here */
724 elog(ERROR, "unrecognized join type: %d", (int) sjinfo->jointype);
725 break;
728 bms_free(joinrelids);
730 return joinrel;
735 * have_join_order_restriction
736 * Detect whether the two relations should be joined to satisfy
737 * a join-order restriction arising from special joins.
739 * In practice this is always used with have_relevant_joinclause(), and so
740 * could be merged with that function, but it seems clearer to separate the
741 * two concerns. We need this test because there are degenerate cases where
742 * a clauseless join must be performed to satisfy join-order restrictions.
744 * Note: this is only a problem if one side of a degenerate outer join
745 * contains multiple rels, or a clauseless join is required within an
746 * IN/EXISTS RHS; else we will find a join path via the "last ditch" case in
747 * join_search_one_level(). We could dispense with this test if we were
748 * willing to try bushy plans in the "last ditch" case, but that seems much
749 * less efficient.
751 bool
752 have_join_order_restriction(PlannerInfo *root,
753 RelOptInfo *rel1, RelOptInfo *rel2)
755 bool result = false;
756 ListCell *l;
759 * It's possible that the rels correspond to the left and right sides of a
760 * degenerate outer join, that is, one with no joinclause mentioning the
761 * non-nullable side; in which case we should force the join to occur.
763 * Also, the two rels could represent a clauseless join that has to be
764 * completed to build up the LHS or RHS of an outer join.
766 foreach(l, root->join_info_list)
768 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
770 /* ignore full joins --- other mechanisms handle them */
771 if (sjinfo->jointype == JOIN_FULL)
772 continue;
774 /* Can we perform the SJ with these rels? */
775 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
776 bms_is_subset(sjinfo->min_righthand, rel2->relids))
778 result = true;
779 break;
781 if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
782 bms_is_subset(sjinfo->min_righthand, rel1->relids))
784 result = true;
785 break;
789 * Might we need to join these rels to complete the RHS? We have to
790 * use "overlap" tests since either rel might include a lower SJ that
791 * has been proven to commute with this one.
793 if (bms_overlap(sjinfo->min_righthand, rel1->relids) &&
794 bms_overlap(sjinfo->min_righthand, rel2->relids))
796 result = true;
797 break;
800 /* Likewise for the LHS. */
801 if (bms_overlap(sjinfo->min_lefthand, rel1->relids) &&
802 bms_overlap(sjinfo->min_lefthand, rel2->relids))
804 result = true;
805 break;
810 * We do not force the join to occur if either input rel can legally be
811 * joined to anything else using joinclauses. This essentially means that
812 * clauseless bushy joins are put off as long as possible. The reason is
813 * that when there is a join order restriction high up in the join tree
814 * (that is, with many rels inside the LHS or RHS), we would otherwise
815 * expend lots of effort considering very stupid join combinations within
816 * its LHS or RHS.
818 if (result)
820 if (has_legal_joinclause(root, rel1) ||
821 has_legal_joinclause(root, rel2))
822 result = false;
825 return result;
830 * has_join_restriction
831 * Detect whether the specified relation has join-order restrictions
832 * due to being inside an outer join or an IN (sub-SELECT).
834 * Essentially, this tests whether have_join_order_restriction() could
835 * succeed with this rel and some other one. It's OK if we sometimes
836 * say "true" incorrectly. (Therefore, we don't bother with the relatively
837 * expensive has_legal_joinclause test.)
839 static bool
840 has_join_restriction(PlannerInfo *root, RelOptInfo *rel)
842 ListCell *l;
844 foreach(l, root->join_info_list)
846 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
848 /* ignore full joins --- other mechanisms preserve their ordering */
849 if (sjinfo->jointype == JOIN_FULL)
850 continue;
852 /* ignore if SJ is already contained in rel */
853 if (bms_is_subset(sjinfo->min_lefthand, rel->relids) &&
854 bms_is_subset(sjinfo->min_righthand, rel->relids))
855 continue;
857 /* restricted if it overlaps LHS or RHS, but doesn't contain SJ */
858 if (bms_overlap(sjinfo->min_lefthand, rel->relids) ||
859 bms_overlap(sjinfo->min_righthand, rel->relids))
860 return true;
863 return false;
868 * has_legal_joinclause
869 * Detect whether the specified relation can legally be joined
870 * to any other rels using join clauses.
872 * We consider only joins to single other relations in the current
873 * initial_rels list. This is sufficient to get a "true" result in most real
874 * queries, and an occasional erroneous "false" will only cost a bit more
875 * planning time. The reason for this limitation is that considering joins to
876 * other joins would require proving that the other join rel can legally be
877 * formed, which seems like too much trouble for something that's only a
878 * heuristic to save planning time. (Note: we must look at initial_rels
879 * and not all of the query, since when we are planning a sub-joinlist we
880 * may be forced to make clauseless joins within initial_rels even though
881 * there are join clauses linking to other parts of the query.)
883 static bool
884 has_legal_joinclause(PlannerInfo *root, RelOptInfo *rel)
886 ListCell *lc;
888 foreach(lc, root->initial_rels)
890 RelOptInfo *rel2 = (RelOptInfo *) lfirst(lc);
892 /* ignore rels that are already in "rel" */
893 if (bms_overlap(rel->relids, rel2->relids))
894 continue;
896 if (have_relevant_joinclause(root, rel, rel2))
898 Relids joinrelids;
899 SpecialJoinInfo *sjinfo;
900 bool reversed;
902 /* join_is_legal needs relids of the union */
903 joinrelids = bms_union(rel->relids, rel2->relids);
905 if (join_is_legal(root, rel, rel2, joinrelids,
906 &sjinfo, &reversed))
908 /* Yes, this will work */
909 bms_free(joinrelids);
910 return true;
913 bms_free(joinrelids);
917 return false;
922 * is_dummy_rel --- has relation been proven empty?
924 * If so, it will have a single path that is dummy.
926 static bool
927 is_dummy_rel(RelOptInfo *rel)
929 return (rel->cheapest_total_path != NULL &&
930 IS_DUMMY_PATH(rel->cheapest_total_path));
934 * Mark a rel as proven empty.
936 static void
937 mark_dummy_rel(RelOptInfo *rel)
939 /* Set dummy size estimate */
940 rel->rows = 0;
942 /* Evict any previously chosen paths */
943 rel->pathlist = NIL;
945 /* Set up the dummy path */
946 add_path(rel, (Path *) create_append_path(rel, NIL));
948 /* Set or update cheapest_total_path */
949 set_cheapest(rel);
954 * restriction_is_constant_false --- is a restrictlist just FALSE?
956 * In cases where a qual is provably constant FALSE, eval_const_expressions
957 * will generally have thrown away anything that's ANDed with it. In outer
958 * join situations this will leave us computing cartesian products only to
959 * decide there's no match for an outer row, which is pretty stupid. So,
960 * we need to detect the case.
962 static bool
963 restriction_is_constant_false(List *restrictlist)
965 ListCell *lc;
968 * Despite the above comment, the restriction list we see here might
969 * possibly have other members besides the FALSE constant, since other
970 * quals could get "pushed down" to the outer join level. So we check
971 * each member of the list.
973 foreach(lc, restrictlist)
975 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
977 Assert(IsA(rinfo, RestrictInfo));
978 if (rinfo->clause && IsA(rinfo->clause, Const))
980 Const *con = (Const *) rinfo->clause;
982 /* constant NULL is as good as constant FALSE for our purposes */
983 if (con->constisnull)
984 return true;
985 if (!DatumGetBool(con->constvalue))
986 return true;
989 return false;