1 /*-------------------------------------------------------------------------
4 * Routines copied from PostgreSQL core distribution.
6 * src/backend/optimizer/path/allpaths.c
7 * set_append_rel_pathlist()
8 * generate_mergeappend_paths()
9 * accumulate_append_subpath()
10 * standard_join_search()
12 * src/backend/optimizer/path/joinrels.c
13 * join_search_one_level()
14 * make_rels_by_clause_joins()
15 * make_rels_by_clauseless_joins()
17 * has_join_restriction()
20 * restriction_is_constant_false()
22 * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
23 * Portions Copyright (c) 1994, Regents of the University of California
25 *-------------------------------------------------------------------------
29 * set_append_rel_pathlist
30 * Build access paths for an "append relation"
33 set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
34 Index rti, RangeTblEntry *rte)
36 int parentRTindex = rti;
37 List *live_childrels = NIL;
39 List *all_child_pathkeys = NIL;
40 List *all_child_outers = NIL;
44 * Generate access paths for each member relation, and remember the
45 * cheapest path for each one. Also, identify all pathkeys (orderings)
46 * and parameterizations (required_outer sets) available for the member
49 foreach(l, root->append_rel_list)
51 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
53 RangeTblEntry *childRTE;
57 /* append_rel_list contains all append rels; ignore others */
58 if (appinfo->parent_relid != parentRTindex)
61 /* Re-locate the child RTE and RelOptInfo */
62 childRTindex = appinfo->child_relid;
63 childRTE = root->simple_rte_array[childRTindex];
64 childrel = root->simple_rel_array[childRTindex];
67 * Compute the child's access paths.
69 set_rel_pathlist(root, childrel, childRTindex, childRTE);
72 * If child is dummy, ignore it.
74 if (IS_DUMMY_REL(childrel))
78 * Child is live, so add its cheapest access path to the Append path
79 * we are constructing for the parent.
81 subpaths = accumulate_append_subpath(subpaths,
82 childrel->cheapest_total_path);
84 /* Remember which childrels are live, for logic below */
85 live_childrels = lappend(live_childrels, childrel);
88 * Collect lists of all the available path orderings and
89 * parameterizations for all the children. We use these as a
90 * heuristic to indicate which sort orderings and parameterizations we
91 * should build Append and MergeAppend paths for.
93 foreach(lcp, childrel->pathlist)
95 Path *childpath = (Path *) lfirst(lcp);
96 List *childkeys = childpath->pathkeys;
97 Relids childouter = PATH_REQ_OUTER(childpath);
99 /* Unsorted paths don't contribute to pathkey list */
100 if (childkeys != NIL)
105 /* Have we already seen this ordering? */
106 foreach(lpk, all_child_pathkeys)
108 List *existing_pathkeys = (List *) lfirst(lpk);
110 if (compare_pathkeys(existing_pathkeys,
111 childkeys) == PATHKEYS_EQUAL)
119 /* No, so add it to all_child_pathkeys */
120 all_child_pathkeys = lappend(all_child_pathkeys,
125 /* Unparameterized paths don't contribute to param-set list */
131 /* Have we already seen this param set? */
132 foreach(lco, all_child_outers)
134 Relids existing_outers = (Relids) lfirst(lco);
136 if (bms_equal(existing_outers, childouter))
144 /* No, so add it to all_child_outers */
145 all_child_outers = lappend(all_child_outers,
153 * Next, build an unordered, unparameterized Append path for the rel.
154 * (Note: this is correct even if we have zero or one live subpath due to
155 * constraint exclusion.)
157 add_path(rel, (Path *) create_append_path(rel, subpaths, NULL));
160 * Build unparameterized MergeAppend paths based on the collected list of
163 generate_mergeappend_paths(root, rel, live_childrels, all_child_pathkeys);
166 * Build Append paths for each parameterization seen among the child rels.
167 * (This may look pretty expensive, but in most cases of practical
168 * interest, the child rels will expose mostly the same parameterizations,
169 * so that not that many cases actually get considered here.)
171 * The Append node itself cannot enforce quals, so all qual checking must
172 * be done in the child paths. This means that to have a parameterized
173 * Append path, we must have the exact same parameterization for each
174 * child path; otherwise some children might be failing to check the
175 * moved-down quals. To make them match up, we can try to increase the
176 * parameterization of lesser-parameterized paths.
178 foreach(l, all_child_outers)
180 Relids required_outer = (Relids) lfirst(l);
184 /* Select the child paths for an Append with this parameterization */
186 foreach(lcr, live_childrels)
188 RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
189 Path *cheapest_total;
192 get_cheapest_path_for_pathkeys(childrel->pathlist,
196 Assert(cheapest_total != NULL);
198 /* Children must have exactly the desired parameterization */
199 if (!bms_equal(PATH_REQ_OUTER(cheapest_total), required_outer))
201 cheapest_total = reparameterize_path(root, cheapest_total,
202 required_outer, 1.0);
203 if (cheapest_total == NULL)
210 subpaths = accumulate_append_subpath(subpaths, cheapest_total);
214 add_path(rel, (Path *)
215 create_append_path(rel, subpaths, required_outer));
218 /* Select cheapest paths */
223 * generate_mergeappend_paths
224 * Generate MergeAppend paths for an append relation
226 * Generate a path for each ordering (pathkey list) appearing in
227 * all_child_pathkeys.
229 * We consider both cheapest-startup and cheapest-total cases, ie, for each
230 * interesting ordering, collect all the cheapest startup subpaths and all the
231 * cheapest total paths, and build a MergeAppend path for each case.
233 * We don't currently generate any parameterized MergeAppend paths. While
234 * it would not take much more code here to do so, it's very unclear that it
235 * is worth the planning cycles to investigate such paths: there's little
236 * use for an ordered path on the inside of a nestloop. In fact, it's likely
237 * that the current coding of add_path would reject such paths out of hand,
238 * because add_path gives no credit for sort ordering of parameterized paths,
239 * and a parameterized MergeAppend is going to be more expensive than the
240 * corresponding parameterized Append path. If we ever try harder to support
241 * parameterized mergejoin plans, it might be worth adding support for
242 * parameterized MergeAppends to feed such joins. (See notes in
243 * optimizer/README for why that might not ever happen, though.)
246 generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel,
247 List *live_childrels,
248 List *all_child_pathkeys)
252 foreach(lcp, all_child_pathkeys)
254 List *pathkeys = (List *) lfirst(lcp);
255 List *startup_subpaths = NIL;
256 List *total_subpaths = NIL;
257 bool startup_neq_total = false;
260 /* Select the child paths for this ordering... */
261 foreach(lcr, live_childrels)
263 RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
264 Path *cheapest_startup,
267 /* Locate the right paths, if they are available. */
269 get_cheapest_path_for_pathkeys(childrel->pathlist,
274 get_cheapest_path_for_pathkeys(childrel->pathlist,
280 * If we can't find any paths with the right order just use the
281 * cheapest-total path; we'll have to sort it later.
283 if (cheapest_startup == NULL || cheapest_total == NULL)
285 cheapest_startup = cheapest_total =
286 childrel->cheapest_total_path;
287 /* Assert we do have an unparameterized path for this child */
288 Assert(cheapest_total->param_info == NULL);
292 * Notice whether we actually have different paths for the
293 * "cheapest" and "total" cases; frequently there will be no point
294 * in two create_merge_append_path() calls.
296 if (cheapest_startup != cheapest_total)
297 startup_neq_total = true;
300 accumulate_append_subpath(startup_subpaths, cheapest_startup);
302 accumulate_append_subpath(total_subpaths, cheapest_total);
305 /* ... and build the MergeAppend paths */
306 add_path(rel, (Path *) create_merge_append_path(root,
311 if (startup_neq_total)
312 add_path(rel, (Path *) create_merge_append_path(root,
321 * accumulate_append_subpath
322 * Add a subpath to the list being built for an Append or MergeAppend
324 * It's possible that the child is itself an Append path, in which case
325 * we can "cut out the middleman" and just add its child paths to our
326 * own list. (We don't try to do this earlier because we need to
327 * apply both levels of transformation to the quals.)
330 accumulate_append_subpath(List *subpaths, Path *path)
332 if (IsA(path, AppendPath))
334 AppendPath *apath = (AppendPath *) path;
336 /* list_copy is important here to avoid sharing list substructure */
337 return list_concat(subpaths, list_copy(apath->subpaths));
340 return lappend(subpaths, path);
344 * standard_join_search
345 * Find possible joinpaths for a query by successively finding ways
346 * to join component relations into join relations.
348 * 'levels_needed' is the number of iterations needed, ie, the number of
349 * independent jointree items in the query. This is > 1.
351 * 'initial_rels' is a list of RelOptInfo nodes for each independent
352 * jointree item. These are the components to be joined together.
353 * Note that levels_needed == list_length(initial_rels).
355 * Returns the final level of join relations, i.e., the relation that is
356 * the result of joining all the original relations together.
357 * At least one implementation path must be provided for this relation and
358 * all required sub-relations.
360 * To support loadable plugins that modify planner behavior by changing the
361 * join searching algorithm, we provide a hook variable that lets a plugin
362 * replace or supplement this function. Any such hook must return the same
363 * final join relation as the standard code would, but it might have a
364 * different set of implementation paths attached, and only the sub-joinrels
365 * needed for these paths need have been instantiated.
367 * Note to plugin authors: the functions invoked during standard_join_search()
368 * modify root->join_rel_list and root->join_rel_hash. If you want to do more
369 * than one join-order search, you'll probably need to save and restore the
370 * original states of those data structures. See geqo_eval() for an example.
373 standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
379 * This function cannot be invoked recursively within any one planning
380 * problem, so join_rel_level[] can't be in use already.
382 Assert(root->join_rel_level == NULL);
385 * We employ a simple "dynamic programming" algorithm: we first find all
386 * ways to build joins of two jointree items, then all ways to build joins
387 * of three items (from two-item joins and single items), then four-item
388 * joins, and so on until we have considered all ways to join all the
389 * items into one rel.
391 * root->join_rel_level[j] is a list of all the j-item rels. Initially we
392 * set root->join_rel_level[1] to represent all the single-jointree-item
395 root->join_rel_level = (List **) palloc0((levels_needed + 1) * sizeof(List *));
397 root->join_rel_level[1] = initial_rels;
399 for (lev = 2; lev <= levels_needed; lev++)
404 * Determine all possible pairs of relations to be joined at this
405 * level, and build paths for making each one from every available
406 * pair of lower-level relations.
408 join_search_one_level(root, lev);
411 * Do cleanup work on each just-processed rel.
413 foreach(lc, root->join_rel_level[lev])
415 rel = (RelOptInfo *) lfirst(lc);
417 /* Find and save the cheapest paths for this rel */
420 #ifdef OPTIMIZER_DEBUG
421 debug_print_rel(root, rel);
427 * We should have a single rel at the final level.
429 if (root->join_rel_level[levels_needed] == NIL)
430 elog(ERROR, "failed to build any %d-way joins", levels_needed);
431 Assert(list_length(root->join_rel_level[levels_needed]) == 1);
433 rel = (RelOptInfo *) linitial(root->join_rel_level[levels_needed]);
435 root->join_rel_level = NULL;
441 * join_search_one_level
442 * Consider ways to produce join relations containing exactly 'level'
443 * jointree items. (This is one step of the dynamic-programming method
444 * embodied in standard_join_search.) Join rel nodes for each feasible
445 * combination of lower-level rels are created and returned in a list.
446 * Implementation paths are created for each such joinrel, too.
448 * level: level of rels we want to make this time
449 * root->join_rel_level[j], 1 <= j < level, is a list of rels containing j items
451 * The result is returned in root->join_rel_level[level].
454 join_search_one_level(PlannerInfo *root, int level)
456 List **joinrels = root->join_rel_level;
460 Assert(joinrels[level] == NIL);
462 /* Set join_cur_level so that new joinrels are added to proper list */
463 root->join_cur_level = level;
466 * First, consider left-sided and right-sided plans, in which rels of
467 * exactly level-1 member relations are joined against initial relations.
468 * We prefer to join using join clauses, but if we find a rel of level-1
469 * members that has no join clauses, we will generate Cartesian-product
470 * joins against all initial rels not already contained in it.
472 foreach(r, joinrels[level - 1])
474 RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
476 if (old_rel->joininfo != NIL || old_rel->has_eclass_joins ||
477 has_join_restriction(root, old_rel))
480 * There are join clauses or join order restrictions relevant to
481 * this rel, so consider joins between this rel and (only) those
482 * initial rels it is linked to by a clause or restriction.
484 * At level 2 this condition is symmetric, so there is no need to
485 * look at initial rels before this one in the list; we already
486 * considered such joins when we were at the earlier rel. (The
487 * mirror-image joins are handled automatically by make_join_rel.)
488 * In later passes (level > 2), we join rels of the previous level
489 * to each initial rel they don't already include but have a join
490 * clause or restriction with.
492 ListCell *other_rels;
494 if (level == 2) /* consider remaining initial rels */
495 other_rels = lnext(r);
496 else /* consider all initial rels */
497 other_rels = list_head(joinrels[1]);
499 make_rels_by_clause_joins(root,
506 * Oops, we have a relation that is not joined to any other
507 * relation, either directly or by join-order restrictions.
508 * Cartesian product time.
510 * We consider a cartesian product with each not-already-included
511 * initial rel, whether it has other join clauses or not. At
512 * level 2, if there are two or more clauseless initial rels, we
513 * will redundantly consider joining them in both directions; but
514 * such cases aren't common enough to justify adding complexity to
515 * avoid the duplicated effort.
517 make_rels_by_clauseless_joins(root,
519 list_head(joinrels[1]));
524 * Now, consider "bushy plans" in which relations of k initial rels are
525 * joined to relations of level-k initial rels, for 2 <= k <= level-2.
527 * We only consider bushy-plan joins for pairs of rels where there is a
528 * suitable join clause (or join order restriction), in order to avoid
529 * unreasonable growth of planning time.
533 int other_level = level - k;
536 * Since make_join_rel(x, y) handles both x,y and y,x cases, we only
537 * need to go as far as the halfway point.
542 foreach(r, joinrels[k])
544 RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
545 ListCell *other_rels;
549 * We can ignore relations without join clauses here, unless they
550 * participate in join-order restrictions --- then we might have
551 * to force a bushy join plan.
553 if (old_rel->joininfo == NIL && !old_rel->has_eclass_joins &&
554 !has_join_restriction(root, old_rel))
557 if (k == other_level)
558 other_rels = lnext(r); /* only consider remaining rels */
560 other_rels = list_head(joinrels[other_level]);
562 for_each_cell(r2, other_rels)
564 RelOptInfo *new_rel = (RelOptInfo *) lfirst(r2);
566 if (!bms_overlap(old_rel->relids, new_rel->relids))
569 * OK, we can build a rel of the right level from this
570 * pair of rels. Do so if there is at least one relevant
571 * join clause or join order restriction.
573 if (have_relevant_joinclause(root, old_rel, new_rel) ||
574 have_join_order_restriction(root, old_rel, new_rel))
576 (void) make_join_rel(root, old_rel, new_rel);
584 * Last-ditch effort: if we failed to find any usable joins so far, force
585 * a set of cartesian-product joins to be generated. This handles the
586 * special case where all the available rels have join clauses but we
587 * cannot use any of those clauses yet. This can only happen when we are
588 * considering a join sub-problem (a sub-joinlist) and all the rels in the
589 * sub-problem have only join clauses with rels outside the sub-problem.
592 * SELECT ... FROM a INNER JOIN b ON TRUE, c, d, ...
593 * WHERE a.w = c.x and b.y = d.z;
595 * If the "a INNER JOIN b" sub-problem does not get flattened into the
596 * upper level, we must be willing to make a cartesian join of a and b;
597 * but the code above will not have done so, because it thought that both
598 * a and b have joinclauses. We consider only left-sided and right-sided
599 * cartesian joins in this case (no bushy).
602 if (joinrels[level] == NIL)
605 * This loop is just like the first one, except we always call
606 * make_rels_by_clauseless_joins().
608 foreach(r, joinrels[level - 1])
610 RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
612 make_rels_by_clauseless_joins(root,
614 list_head(joinrels[1]));
618 * When special joins are involved, there may be no legal way
619 * to make an N-way join for some values of N. For example consider
621 * SELECT ... FROM t1 WHERE
622 * x IN (SELECT ... FROM t2,t3 WHERE ...) AND
623 * y IN (SELECT ... FROM t4,t5 WHERE ...)
625 * We will flatten this query to a 5-way join problem, but there are
626 * no 4-way joins that join_is_legal() will consider legal. We have
627 * to accept failure at level 4 and go on to discover a workable
628 * bushy plan at level 5.
630 * However, if there are no special joins and no lateral references
631 * then join_is_legal() should never fail, and so the following sanity
635 if (joinrels[level] == NIL &&
636 root->join_info_list == NIL &&
637 root->lateral_info_list == NIL)
638 elog(ERROR, "failed to build any %d-way joins", level);
643 * make_rels_by_clause_joins
644 * Build joins between the given relation 'old_rel' and other relations
645 * that participate in join clauses that 'old_rel' also participates in
646 * (or participate in join-order restrictions with it).
647 * The join rels are returned in root->join_rel_level[join_cur_level].
649 * Note: at levels above 2 we will generate the same joined relation in
650 * multiple ways --- for example (a join b) join c is the same RelOptInfo as
651 * (b join c) join a, though the second case will add a different set of Paths
652 * to it. This is the reason for using the join_rel_level mechanism, which
653 * automatically ensures that each new joinrel is only added to the list once.
655 * 'old_rel' is the relation entry for the relation to be joined
656 * 'other_rels': the first cell in a linked list containing the other
657 * rels to be considered for joining
659 * Currently, this is only used with initial rels in other_rels, but it
660 * will work for joining to joinrels too.
663 make_rels_by_clause_joins(PlannerInfo *root,
665 ListCell *other_rels)
669 for_each_cell(l, other_rels)
671 RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
673 if (!bms_overlap(old_rel->relids, other_rel->relids) &&
674 (have_relevant_joinclause(root, old_rel, other_rel) ||
675 have_join_order_restriction(root, old_rel, other_rel)))
677 (void) make_join_rel(root, old_rel, other_rel);
683 * make_rels_by_clauseless_joins
684 * Given a relation 'old_rel' and a list of other relations
685 * 'other_rels', create a join relation between 'old_rel' and each
686 * member of 'other_rels' that isn't already included in 'old_rel'.
687 * The join rels are returned in root->join_rel_level[join_cur_level].
689 * 'old_rel' is the relation entry for the relation to be joined
690 * 'other_rels': the first cell of a linked list containing the
691 * other rels to be considered for joining
693 * Currently, this is only used with initial rels in other_rels, but it would
694 * work for joining to joinrels too.
697 make_rels_by_clauseless_joins(PlannerInfo *root,
699 ListCell *other_rels)
703 for_each_cell(l, other_rels)
705 RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
707 if (!bms_overlap(other_rel->relids, old_rel->relids))
709 (void) make_join_rel(root, old_rel, other_rel);
716 * Determine whether a proposed join is legal given the query's
717 * join order constraints; and if it is, determine the join type.
719 * Caller must supply not only the two rels, but the union of their relids.
720 * (We could simplify the API by computing joinrelids locally, but this
721 * would be redundant work in the normal path through make_join_rel.)
723 * On success, *sjinfo_p is set to NULL if this is to be a plain inner join,
724 * else it's set to point to the associated SpecialJoinInfo node. Also,
725 * *reversed_p is set TRUE if the given relations need to be swapped to
726 * match the SpecialJoinInfo node.
729 join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
731 SpecialJoinInfo **sjinfo_p, bool *reversed_p)
733 SpecialJoinInfo *match_sjinfo;
742 * Ensure output params are set on failure return. This is just to
743 * suppress uninitialized-variable warnings from overly anal compilers.
749 * If we have any special joins, the proposed join might be illegal; and
750 * in any case we have to determine its join type. Scan the join info
751 * list for conflicts.
755 unique_ified = false;
756 is_valid_inner = true;
758 foreach(l, root->join_info_list)
760 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
763 * This special join is not relevant unless its RHS overlaps the
764 * proposed join. (Check this first as a fast path for dismissing
765 * most irrelevant SJs quickly.)
767 if (!bms_overlap(sjinfo->min_righthand, joinrelids))
771 * Also, not relevant if proposed join is fully contained within RHS
772 * (ie, we're still building up the RHS).
774 if (bms_is_subset(joinrelids, sjinfo->min_righthand))
778 * Also, not relevant if SJ is already done within either input.
780 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
781 bms_is_subset(sjinfo->min_righthand, rel1->relids))
783 if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
784 bms_is_subset(sjinfo->min_righthand, rel2->relids))
788 * If it's a semijoin and we already joined the RHS to any other rels
789 * within either input, then we must have unique-ified the RHS at that
790 * point (see below). Therefore the semijoin is no longer relevant in
793 if (sjinfo->jointype == JOIN_SEMI)
795 if (bms_is_subset(sjinfo->syn_righthand, rel1->relids) &&
796 !bms_equal(sjinfo->syn_righthand, rel1->relids))
798 if (bms_is_subset(sjinfo->syn_righthand, rel2->relids) &&
799 !bms_equal(sjinfo->syn_righthand, rel2->relids))
804 * If one input contains min_lefthand and the other contains
805 * min_righthand, then we can perform the SJ at this join.
807 * Barf if we get matches to more than one SJ (is that possible?)
809 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
810 bms_is_subset(sjinfo->min_righthand, rel2->relids))
813 return false; /* invalid join path */
814 match_sjinfo = sjinfo;
817 else if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
818 bms_is_subset(sjinfo->min_righthand, rel1->relids))
821 return false; /* invalid join path */
822 match_sjinfo = sjinfo;
825 else if (sjinfo->jointype == JOIN_SEMI &&
826 bms_equal(sjinfo->syn_righthand, rel2->relids) &&
827 create_unique_path(root, rel2, rel2->cheapest_total_path,
831 * For a semijoin, we can join the RHS to anything else by
832 * unique-ifying the RHS (if the RHS can be unique-ified).
833 * We will only get here if we have the full RHS but less
834 * than min_lefthand on the LHS.
836 * The reason to consider such a join path is exemplified by
837 * SELECT ... FROM a,b WHERE (a.x,b.y) IN (SELECT c1,c2 FROM c)
838 * If we insist on doing this as a semijoin we will first have
839 * to form the cartesian product of A*B. But if we unique-ify
840 * C then the semijoin becomes a plain innerjoin and we can join
841 * in any order, eg C to A and then to B. When C is much smaller
842 * than A and B this can be a huge win. So we allow C to be
843 * joined to just A or just B here, and then make_join_rel has
844 * to handle the case properly.
846 * Note that actually we'll allow unique-ified C to be joined to
847 * some other relation D here, too. That is legal, if usually not
848 * very sane, and this routine is only concerned with legality not
849 * with whether the join is good strategy.
853 return false; /* invalid join path */
854 match_sjinfo = sjinfo;
858 else if (sjinfo->jointype == JOIN_SEMI &&
859 bms_equal(sjinfo->syn_righthand, rel1->relids) &&
860 create_unique_path(root, rel1, rel1->cheapest_total_path,
863 /* Reversed semijoin case */
865 return false; /* invalid join path */
866 match_sjinfo = sjinfo;
873 * Otherwise, the proposed join overlaps the RHS but isn't
874 * a valid implementation of this SJ. It might still be
875 * a legal join, however. If both inputs overlap the RHS,
876 * assume that it's OK. Since the inputs presumably got past
877 * this function's checks previously, they can't overlap the
878 * LHS and their violations of the RHS boundary must represent
879 * SJs that have been determined to commute with this one.
880 * We have to allow this to work correctly in cases like
881 * (a LEFT JOIN (b JOIN (c LEFT JOIN d)))
882 * when the c/d join has been determined to commute with the join
883 * to a, and hence d is not part of min_righthand for the upper
884 * join. It should be legal to join b to c/d but this will appear
885 * as a violation of the upper join's RHS.
886 * Furthermore, if one input overlaps the RHS and the other does
887 * not, we should still allow the join if it is a valid
888 * implementation of some other SJ. We have to allow this to
889 * support the associative identity
890 * (a LJ b on Pab) LJ c ON Pbc = a LJ (b LJ c ON Pbc) on Pab
891 * since joining B directly to C violates the lower SJ's RHS.
892 * We assume that make_outerjoininfo() set things up correctly
893 * so that we'll only match to some SJ if the join is valid.
894 * Set flag here to check at bottom of loop.
897 if (sjinfo->jointype != JOIN_SEMI &&
898 bms_overlap(rel1->relids, sjinfo->min_righthand) &&
899 bms_overlap(rel2->relids, sjinfo->min_righthand))
902 Assert(!bms_overlap(joinrelids, sjinfo->min_lefthand));
905 is_valid_inner = false;
910 * Fail if violated some SJ's RHS and didn't match to another SJ. However,
911 * "matching" to a semijoin we are implementing by unique-ification
912 * doesn't count (think: it's really an inner join).
914 if (!is_valid_inner &&
915 (match_sjinfo == NULL || unique_ified))
916 return false; /* invalid join path */
919 * We also have to check for constraints imposed by LATERAL references.
920 * The proposed rels could each contain lateral references to the other,
921 * in which case the join is impossible. If there are lateral references
922 * in just one direction, then the join has to be done with a nestloop
923 * with the lateral referencer on the inside. If the join matches an SJ
924 * that cannot be implemented by such a nestloop, the join is impossible.
926 lateral_fwd = lateral_rev = false;
927 foreach(l, root->lateral_info_list)
929 LateralJoinInfo *ljinfo = (LateralJoinInfo *) lfirst(l);
931 if (bms_is_subset(ljinfo->lateral_rhs, rel2->relids) &&
932 bms_overlap(ljinfo->lateral_lhs, rel1->relids))
934 /* has to be implemented as nestloop with rel1 on left */
936 return false; /* have lateral refs in both directions */
938 if (!bms_is_subset(ljinfo->lateral_lhs, rel1->relids))
939 return false; /* rel1 can't compute the required parameter */
941 (reversed || match_sjinfo->jointype == JOIN_FULL))
942 return false; /* not implementable as nestloop */
944 if (bms_is_subset(ljinfo->lateral_rhs, rel1->relids) &&
945 bms_overlap(ljinfo->lateral_lhs, rel2->relids))
947 /* has to be implemented as nestloop with rel2 on left */
949 return false; /* have lateral refs in both directions */
951 if (!bms_is_subset(ljinfo->lateral_lhs, rel2->relids))
952 return false; /* rel2 can't compute the required parameter */
954 (!reversed || match_sjinfo->jointype == JOIN_FULL))
955 return false; /* not implementable as nestloop */
959 /* Otherwise, it's a valid join */
960 *sjinfo_p = match_sjinfo;
961 *reversed_p = reversed;
966 * has_join_restriction
967 * Detect whether the specified relation has join-order restrictions,
968 * due to being inside an outer join or an IN (sub-SELECT),
969 * or participating in any LATERAL references.
971 * Essentially, this tests whether have_join_order_restriction() could
972 * succeed with this rel and some other one. It's OK if we sometimes
973 * say "true" incorrectly. (Therefore, we don't bother with the relatively
974 * expensive has_legal_joinclause test.)
977 has_join_restriction(PlannerInfo *root, RelOptInfo *rel)
981 foreach(l, root->lateral_info_list)
983 LateralJoinInfo *ljinfo = (LateralJoinInfo *) lfirst(l);
985 if (bms_is_subset(ljinfo->lateral_rhs, rel->relids) ||
986 bms_overlap(ljinfo->lateral_lhs, rel->relids))
990 foreach(l, root->join_info_list)
992 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
994 /* ignore full joins --- other mechanisms preserve their ordering */
995 if (sjinfo->jointype == JOIN_FULL)
998 /* ignore if SJ is already contained in rel */
999 if (bms_is_subset(sjinfo->min_lefthand, rel->relids) &&
1000 bms_is_subset(sjinfo->min_righthand, rel->relids))
1003 /* restricted if it overlaps LHS or RHS, but doesn't contain SJ */
1004 if (bms_overlap(sjinfo->min_lefthand, rel->relids) ||
1005 bms_overlap(sjinfo->min_righthand, rel->relids))
1013 * is_dummy_rel --- has relation been proven empty?
1016 is_dummy_rel(RelOptInfo *rel)
1018 return IS_DUMMY_REL(rel);
1022 * Mark a relation as proven empty.
1024 * During GEQO planning, this can get invoked more than once on the same
1025 * baserel struct, so it's worth checking to see if the rel is already marked
1028 * Also, when called during GEQO join planning, we are in a short-lived
1029 * memory context. We must make sure that the dummy path attached to a
1030 * baserel survives the GEQO cycle, else the baserel is trashed for future
1031 * GEQO cycles. On the other hand, when we are marking a joinrel during GEQO,
1032 * we don't want the dummy path to clutter the main planning context. Upshot
1033 * is that the best solution is to explicitly make the dummy path in the same
1034 * context the given RelOptInfo is in.
1037 mark_dummy_rel(RelOptInfo *rel)
1039 MemoryContext oldcontext;
1041 /* Already marked? */
1042 if (is_dummy_rel(rel))
1045 /* No, so choose correct context to make the dummy path in */
1046 oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
1048 /* Set dummy size estimate */
1051 /* Evict any previously chosen paths */
1052 rel->pathlist = NIL;
1054 /* Set up the dummy path */
1055 add_path(rel, (Path *) create_append_path(rel, NIL, NULL));
1057 /* Set or update cheapest_total_path and related fields */
1060 MemoryContextSwitchTo(oldcontext);
1064 * restriction_is_constant_false --- is a restrictlist just FALSE?
1066 * In cases where a qual is provably constant FALSE, eval_const_expressions
1067 * will generally have thrown away anything that's ANDed with it. In outer
1068 * join situations this will leave us computing cartesian products only to
1069 * decide there's no match for an outer row, which is pretty stupid. So,
1070 * we need to detect the case.
1072 * If only_pushed_down is TRUE, then consider only pushed-down quals.
1075 restriction_is_constant_false(List *restrictlist, bool only_pushed_down)
1080 * Despite the above comment, the restriction list we see here might
1081 * possibly have other members besides the FALSE constant, since other
1082 * quals could get "pushed down" to the outer join level. So we check
1083 * each member of the list.
1085 foreach(lc, restrictlist)
1087 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1089 Assert(IsA(rinfo, RestrictInfo));
1090 if (only_pushed_down && !rinfo->is_pushed_down)
1093 if (rinfo->clause && IsA(rinfo->clause, Const))
1095 Const *con = (Const *) rinfo->clause;
1097 /* constant NULL is as good as constant FALSE for our purposes */
1098 if (con->constisnull)
1100 if (!DatumGetBool(con->constvalue))