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 * get_cheapest_parameterized_child_path()
10 * accumulate_append_subpath()
11 * standard_join_search()
13 * src/backend/optimizer/path/joinrels.c
14 * join_search_one_level()
15 * make_rels_by_clause_joins()
16 * make_rels_by_clauseless_joins()
18 * has_join_restriction()
21 * restriction_is_constant_false()
23 * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
24 * Portions Copyright (c) 1994, Regents of the University of California
26 *-------------------------------------------------------------------------
30 * set_append_rel_pathlist
31 * Build access paths for an "append relation"
34 set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
35 Index rti, RangeTblEntry *rte)
37 int parentRTindex = rti;
38 List *live_childrels = NIL;
40 bool subpaths_valid = true;
41 List *all_child_pathkeys = NIL;
42 List *all_child_outers = NIL;
46 * Generate access paths for each member relation, and remember the
47 * cheapest path for each one. Also, identify all pathkeys (orderings)
48 * and parameterizations (required_outer sets) available for the member
51 foreach(l, root->append_rel_list)
53 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
55 RangeTblEntry *childRTE;
59 /* append_rel_list contains all append rels; ignore others */
60 if (appinfo->parent_relid != parentRTindex)
63 /* Re-locate the child RTE and RelOptInfo */
64 childRTindex = appinfo->child_relid;
65 childRTE = root->simple_rte_array[childRTindex];
66 childrel = root->simple_rel_array[childRTindex];
69 * Compute the child's access paths.
71 set_rel_pathlist(root, childrel, childRTindex, childRTE);
74 * If child is dummy, ignore it.
76 if (IS_DUMMY_REL(childrel))
80 * Child is live, so add it to the live_childrels list for use below.
82 live_childrels = lappend(live_childrels, childrel);
85 * If child has an unparameterized cheapest-total path, add that to
86 * the unparameterized Append path we are constructing for the parent.
87 * If not, there's no workable unparameterized path.
89 if (childrel->cheapest_total_path->param_info == NULL)
90 subpaths = accumulate_append_subpath(subpaths,
91 childrel->cheapest_total_path);
93 subpaths_valid = false;
96 * Collect lists of all the available path orderings and
97 * parameterizations for all the children. We use these as a
98 * heuristic to indicate which sort orderings and parameterizations we
99 * should build Append and MergeAppend paths for.
101 foreach(lcp, childrel->pathlist)
103 Path *childpath = (Path *) lfirst(lcp);
104 List *childkeys = childpath->pathkeys;
105 Relids childouter = PATH_REQ_OUTER(childpath);
107 /* Unsorted paths don't contribute to pathkey list */
108 if (childkeys != NIL)
113 /* Have we already seen this ordering? */
114 foreach(lpk, all_child_pathkeys)
116 List *existing_pathkeys = (List *) lfirst(lpk);
118 if (compare_pathkeys(existing_pathkeys,
119 childkeys) == PATHKEYS_EQUAL)
127 /* No, so add it to all_child_pathkeys */
128 all_child_pathkeys = lappend(all_child_pathkeys,
133 /* Unparameterized paths don't contribute to param-set list */
139 /* Have we already seen this param set? */
140 foreach(lco, all_child_outers)
142 Relids existing_outers = (Relids) lfirst(lco);
144 if (bms_equal(existing_outers, childouter))
152 /* No, so add it to all_child_outers */
153 all_child_outers = lappend(all_child_outers,
161 * If we found unparameterized paths for all children, build an unordered,
162 * unparameterized Append path for the rel. (Note: this is correct even
163 * if we have zero or one live subpath due to constraint exclusion.)
166 add_path(rel, (Path *) create_append_path(rel, subpaths, NULL));
169 * Also build unparameterized MergeAppend paths based on the collected
170 * list of child pathkeys.
173 generate_mergeappend_paths(root, rel, live_childrels,
177 * Build Append paths for each parameterization seen among the child rels.
178 * (This may look pretty expensive, but in most cases of practical
179 * interest, the child rels will expose mostly the same parameterizations,
180 * so that not that many cases actually get considered here.)
182 * The Append node itself cannot enforce quals, so all qual checking must
183 * be done in the child paths. This means that to have a parameterized
184 * Append path, we must have the exact same parameterization for each
185 * child path; otherwise some children might be failing to check the
186 * moved-down quals. To make them match up, we can try to increase the
187 * parameterization of lesser-parameterized paths.
189 foreach(l, all_child_outers)
191 Relids required_outer = (Relids) lfirst(l);
194 /* Select the child paths for an Append with this parameterization */
196 subpaths_valid = true;
197 foreach(lcr, live_childrels)
199 RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
202 subpath = get_cheapest_parameterized_child_path(root,
207 /* failed to make a suitable path for this child */
208 subpaths_valid = false;
211 subpaths = accumulate_append_subpath(subpaths, subpath);
215 add_path(rel, (Path *)
216 create_append_path(rel, subpaths, required_outer));
219 /* Select cheapest paths */
224 * generate_mergeappend_paths
225 * Generate MergeAppend paths for an append relation
227 * Generate a path for each ordering (pathkey list) appearing in
228 * all_child_pathkeys.
230 * We consider both cheapest-startup and cheapest-total cases, ie, for each
231 * interesting ordering, collect all the cheapest startup subpaths and all the
232 * cheapest total paths, and build a MergeAppend path for each case.
234 * We don't currently generate any parameterized MergeAppend paths. While
235 * it would not take much more code here to do so, it's very unclear that it
236 * is worth the planning cycles to investigate such paths: there's little
237 * use for an ordered path on the inside of a nestloop. In fact, it's likely
238 * that the current coding of add_path would reject such paths out of hand,
239 * because add_path gives no credit for sort ordering of parameterized paths,
240 * and a parameterized MergeAppend is going to be more expensive than the
241 * corresponding parameterized Append path. If we ever try harder to support
242 * parameterized mergejoin plans, it might be worth adding support for
243 * parameterized MergeAppends to feed such joins. (See notes in
244 * optimizer/README for why that might not ever happen, though.)
247 generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel,
248 List *live_childrels,
249 List *all_child_pathkeys)
253 foreach(lcp, all_child_pathkeys)
255 List *pathkeys = (List *) lfirst(lcp);
256 List *startup_subpaths = NIL;
257 List *total_subpaths = NIL;
258 bool startup_neq_total = false;
261 /* Select the child paths for this ordering... */
262 foreach(lcr, live_childrels)
264 RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
265 Path *cheapest_startup,
268 /* Locate the right paths, if they are available. */
270 get_cheapest_path_for_pathkeys(childrel->pathlist,
275 get_cheapest_path_for_pathkeys(childrel->pathlist,
281 * If we can't find any paths with the right order just use the
282 * cheapest-total path; we'll have to sort it later.
284 if (cheapest_startup == NULL || cheapest_total == NULL)
286 cheapest_startup = cheapest_total =
287 childrel->cheapest_total_path;
288 /* Assert we do have an unparameterized path for this child */
289 Assert(cheapest_total->param_info == NULL);
293 * Notice whether we actually have different paths for the
294 * "cheapest" and "total" cases; frequently there will be no point
295 * in two create_merge_append_path() calls.
297 if (cheapest_startup != cheapest_total)
298 startup_neq_total = true;
301 accumulate_append_subpath(startup_subpaths, cheapest_startup);
303 accumulate_append_subpath(total_subpaths, cheapest_total);
306 /* ... and build the MergeAppend paths */
307 add_path(rel, (Path *) create_merge_append_path(root,
312 if (startup_neq_total)
313 add_path(rel, (Path *) create_merge_append_path(root,
322 * get_cheapest_parameterized_child_path
323 * Get cheapest path for this relation that has exactly the requested
326 * Returns NULL if unable to create such a path.
329 get_cheapest_parameterized_child_path(PlannerInfo *root, RelOptInfo *rel,
330 Relids required_outer)
336 * Look up the cheapest existing path with no more than the needed
337 * parameterization. If it has exactly the needed parameterization, we're
340 cheapest = get_cheapest_path_for_pathkeys(rel->pathlist,
344 Assert(cheapest != NULL);
345 if (bms_equal(PATH_REQ_OUTER(cheapest), required_outer))
349 * Otherwise, we can "reparameterize" an existing path to match the given
350 * parameterization, which effectively means pushing down additional
351 * joinquals to be checked within the path's scan. However, some existing
352 * paths might check the available joinquals already while others don't;
353 * therefore, it's not clear which existing path will be cheapest after
354 * reparameterization. We have to go through them all and find out.
357 foreach(lc, rel->pathlist)
359 Path *path = (Path *) lfirst(lc);
361 /* Can't use it if it needs more than requested parameterization */
362 if (!bms_is_subset(PATH_REQ_OUTER(path), required_outer))
366 * Reparameterization can only increase the path's cost, so if it's
367 * already more expensive than the current cheapest, forget it.
369 if (cheapest != NULL &&
370 compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
373 /* Reparameterize if needed, then recheck cost */
374 if (!bms_equal(PATH_REQ_OUTER(path), required_outer))
376 path = reparameterize_path(root, path, required_outer, 1.0);
378 continue; /* failed to reparameterize this one */
379 Assert(bms_equal(PATH_REQ_OUTER(path), required_outer));
381 if (cheapest != NULL &&
382 compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
386 /* We have a new best path */
390 /* Return the best path, or NULL if we found no suitable candidate */
395 * accumulate_append_subpath
396 * Add a subpath to the list being built for an Append or MergeAppend
398 * It's possible that the child is itself an Append or MergeAppend path, in
399 * which case we can "cut out the middleman" and just add its child paths to
400 * our own list. (We don't try to do this earlier because we need to apply
401 * both levels of transformation to the quals.)
403 * Note that if we omit a child MergeAppend in this way, we are effectively
404 * omitting a sort step, which seems fine: if the parent is to be an Append,
405 * its result would be unsorted anyway, while if the parent is to be a
406 * MergeAppend, there's no point in a separate sort on a child.
409 accumulate_append_subpath(List *subpaths, Path *path)
411 if (IsA(path, AppendPath))
413 AppendPath *apath = (AppendPath *) path;
415 /* list_copy is important here to avoid sharing list substructure */
416 return list_concat(subpaths, list_copy(apath->subpaths));
418 else if (IsA(path, MergeAppendPath))
420 MergeAppendPath *mpath = (MergeAppendPath *) path;
422 /* list_copy is important here to avoid sharing list substructure */
423 return list_concat(subpaths, list_copy(mpath->subpaths));
426 return lappend(subpaths, path);
430 * standard_join_search
431 * Find possible joinpaths for a query by successively finding ways
432 * to join component relations into join relations.
434 * 'levels_needed' is the number of iterations needed, ie, the number of
435 * independent jointree items in the query. This is > 1.
437 * 'initial_rels' is a list of RelOptInfo nodes for each independent
438 * jointree item. These are the components to be joined together.
439 * Note that levels_needed == list_length(initial_rels).
441 * Returns the final level of join relations, i.e., the relation that is
442 * the result of joining all the original relations together.
443 * At least one implementation path must be provided for this relation and
444 * all required sub-relations.
446 * To support loadable plugins that modify planner behavior by changing the
447 * join searching algorithm, we provide a hook variable that lets a plugin
448 * replace or supplement this function. Any such hook must return the same
449 * final join relation as the standard code would, but it might have a
450 * different set of implementation paths attached, and only the sub-joinrels
451 * needed for these paths need have been instantiated.
453 * Note to plugin authors: the functions invoked during standard_join_search()
454 * modify root->join_rel_list and root->join_rel_hash. If you want to do more
455 * than one join-order search, you'll probably need to save and restore the
456 * original states of those data structures. See geqo_eval() for an example.
459 standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
465 * This function cannot be invoked recursively within any one planning
466 * problem, so join_rel_level[] can't be in use already.
468 Assert(root->join_rel_level == NULL);
471 * We employ a simple "dynamic programming" algorithm: we first find all
472 * ways to build joins of two jointree items, then all ways to build joins
473 * of three items (from two-item joins and single items), then four-item
474 * joins, and so on until we have considered all ways to join all the
475 * items into one rel.
477 * root->join_rel_level[j] is a list of all the j-item rels. Initially we
478 * set root->join_rel_level[1] to represent all the single-jointree-item
481 root->join_rel_level = (List **) palloc0((levels_needed + 1) * sizeof(List *));
483 root->join_rel_level[1] = initial_rels;
485 for (lev = 2; lev <= levels_needed; lev++)
490 * Determine all possible pairs of relations to be joined at this
491 * level, and build paths for making each one from every available
492 * pair of lower-level relations.
494 join_search_one_level(root, lev);
497 * Do cleanup work on each just-processed rel.
499 foreach(lc, root->join_rel_level[lev])
501 rel = (RelOptInfo *) lfirst(lc);
503 /* Find and save the cheapest paths for this rel */
506 #ifdef OPTIMIZER_DEBUG
507 debug_print_rel(root, rel);
513 * We should have a single rel at the final level.
515 if (root->join_rel_level[levels_needed] == NIL)
516 elog(ERROR, "failed to build any %d-way joins", levels_needed);
517 Assert(list_length(root->join_rel_level[levels_needed]) == 1);
519 rel = (RelOptInfo *) linitial(root->join_rel_level[levels_needed]);
521 root->join_rel_level = NULL;
527 * join_search_one_level
528 * Consider ways to produce join relations containing exactly 'level'
529 * jointree items. (This is one step of the dynamic-programming method
530 * embodied in standard_join_search.) Join rel nodes for each feasible
531 * combination of lower-level rels are created and returned in a list.
532 * Implementation paths are created for each such joinrel, too.
534 * level: level of rels we want to make this time
535 * root->join_rel_level[j], 1 <= j < level, is a list of rels containing j items
537 * The result is returned in root->join_rel_level[level].
540 join_search_one_level(PlannerInfo *root, int level)
542 List **joinrels = root->join_rel_level;
546 Assert(joinrels[level] == NIL);
548 /* Set join_cur_level so that new joinrels are added to proper list */
549 root->join_cur_level = level;
552 * First, consider left-sided and right-sided plans, in which rels of
553 * exactly level-1 member relations are joined against initial relations.
554 * We prefer to join using join clauses, but if we find a rel of level-1
555 * members that has no join clauses, we will generate Cartesian-product
556 * joins against all initial rels not already contained in it.
558 foreach(r, joinrels[level - 1])
560 RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
562 if (old_rel->joininfo != NIL || old_rel->has_eclass_joins ||
563 has_join_restriction(root, old_rel))
566 * There are join clauses or join order restrictions relevant to
567 * this rel, so consider joins between this rel and (only) those
568 * initial rels it is linked to by a clause or restriction.
570 * At level 2 this condition is symmetric, so there is no need to
571 * look at initial rels before this one in the list; we already
572 * considered such joins when we were at the earlier rel. (The
573 * mirror-image joins are handled automatically by make_join_rel.)
574 * In later passes (level > 2), we join rels of the previous level
575 * to each initial rel they don't already include but have a join
576 * clause or restriction with.
578 ListCell *other_rels;
580 if (level == 2) /* consider remaining initial rels */
581 other_rels = lnext(r);
582 else /* consider all initial rels */
583 other_rels = list_head(joinrels[1]);
585 make_rels_by_clause_joins(root,
592 * Oops, we have a relation that is not joined to any other
593 * relation, either directly or by join-order restrictions.
594 * Cartesian product time.
596 * We consider a cartesian product with each not-already-included
597 * initial rel, whether it has other join clauses or not. At
598 * level 2, if there are two or more clauseless initial rels, we
599 * will redundantly consider joining them in both directions; but
600 * such cases aren't common enough to justify adding complexity to
601 * avoid the duplicated effort.
603 make_rels_by_clauseless_joins(root,
605 list_head(joinrels[1]));
610 * Now, consider "bushy plans" in which relations of k initial rels are
611 * joined to relations of level-k initial rels, for 2 <= k <= level-2.
613 * We only consider bushy-plan joins for pairs of rels where there is a
614 * suitable join clause (or join order restriction), in order to avoid
615 * unreasonable growth of planning time.
619 int other_level = level - k;
622 * Since make_join_rel(x, y) handles both x,y and y,x cases, we only
623 * need to go as far as the halfway point.
628 foreach(r, joinrels[k])
630 RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
631 ListCell *other_rels;
635 * We can ignore relations without join clauses here, unless they
636 * participate in join-order restrictions --- then we might have
637 * to force a bushy join plan.
639 if (old_rel->joininfo == NIL && !old_rel->has_eclass_joins &&
640 !has_join_restriction(root, old_rel))
643 if (k == other_level)
644 other_rels = lnext(r); /* only consider remaining rels */
646 other_rels = list_head(joinrels[other_level]);
648 for_each_cell(r2, other_rels)
650 RelOptInfo *new_rel = (RelOptInfo *) lfirst(r2);
652 if (!bms_overlap(old_rel->relids, new_rel->relids))
655 * OK, we can build a rel of the right level from this
656 * pair of rels. Do so if there is at least one relevant
657 * join clause or join order restriction.
659 if (have_relevant_joinclause(root, old_rel, new_rel) ||
660 have_join_order_restriction(root, old_rel, new_rel))
662 (void) make_join_rel(root, old_rel, new_rel);
670 * Last-ditch effort: if we failed to find any usable joins so far, force
671 * a set of cartesian-product joins to be generated. This handles the
672 * special case where all the available rels have join clauses but we
673 * cannot use any of those clauses yet. This can only happen when we are
674 * considering a join sub-problem (a sub-joinlist) and all the rels in the
675 * sub-problem have only join clauses with rels outside the sub-problem.
678 * SELECT ... FROM a INNER JOIN b ON TRUE, c, d, ...
679 * WHERE a.w = c.x and b.y = d.z;
681 * If the "a INNER JOIN b" sub-problem does not get flattened into the
682 * upper level, we must be willing to make a cartesian join of a and b;
683 * but the code above will not have done so, because it thought that both
684 * a and b have joinclauses. We consider only left-sided and right-sided
685 * cartesian joins in this case (no bushy).
688 if (joinrels[level] == NIL)
691 * This loop is just like the first one, except we always call
692 * make_rels_by_clauseless_joins().
694 foreach(r, joinrels[level - 1])
696 RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
698 make_rels_by_clauseless_joins(root,
700 list_head(joinrels[1]));
704 * When special joins are involved, there may be no legal way
705 * to make an N-way join for some values of N. For example consider
707 * SELECT ... FROM t1 WHERE
708 * x IN (SELECT ... FROM t2,t3 WHERE ...) AND
709 * y IN (SELECT ... FROM t4,t5 WHERE ...)
711 * We will flatten this query to a 5-way join problem, but there are
712 * no 4-way joins that join_is_legal() will consider legal. We have
713 * to accept failure at level 4 and go on to discover a workable
714 * bushy plan at level 5.
716 * However, if there are no special joins and no lateral references
717 * then join_is_legal() should never fail, and so the following sanity
721 if (joinrels[level] == NIL &&
722 root->join_info_list == NIL &&
723 root->lateral_info_list == NIL)
724 elog(ERROR, "failed to build any %d-way joins", level);
729 * make_rels_by_clause_joins
730 * Build joins between the given relation 'old_rel' and other relations
731 * that participate in join clauses that 'old_rel' also participates in
732 * (or participate in join-order restrictions with it).
733 * The join rels are returned in root->join_rel_level[join_cur_level].
735 * Note: at levels above 2 we will generate the same joined relation in
736 * multiple ways --- for example (a join b) join c is the same RelOptInfo as
737 * (b join c) join a, though the second case will add a different set of Paths
738 * to it. This is the reason for using the join_rel_level mechanism, which
739 * automatically ensures that each new joinrel is only added to the list once.
741 * 'old_rel' is the relation entry for the relation to be joined
742 * 'other_rels': the first cell in a linked list containing the other
743 * rels to be considered for joining
745 * Currently, this is only used with initial rels in other_rels, but it
746 * will work for joining to joinrels too.
749 make_rels_by_clause_joins(PlannerInfo *root,
751 ListCell *other_rels)
755 for_each_cell(l, other_rels)
757 RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
759 if (!bms_overlap(old_rel->relids, other_rel->relids) &&
760 (have_relevant_joinclause(root, old_rel, other_rel) ||
761 have_join_order_restriction(root, old_rel, other_rel)))
763 (void) make_join_rel(root, old_rel, other_rel);
769 * make_rels_by_clauseless_joins
770 * Given a relation 'old_rel' and a list of other relations
771 * 'other_rels', create a join relation between 'old_rel' and each
772 * member of 'other_rels' that isn't already included in 'old_rel'.
773 * The join rels are returned in root->join_rel_level[join_cur_level].
775 * 'old_rel' is the relation entry for the relation to be joined
776 * 'other_rels': the first cell of a linked list containing the
777 * other rels to be considered for joining
779 * Currently, this is only used with initial rels in other_rels, but it would
780 * work for joining to joinrels too.
783 make_rels_by_clauseless_joins(PlannerInfo *root,
785 ListCell *other_rels)
789 for_each_cell(l, other_rels)
791 RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
793 if (!bms_overlap(other_rel->relids, old_rel->relids))
795 (void) make_join_rel(root, old_rel, other_rel);
802 * Determine whether a proposed join is legal given the query's
803 * join order constraints; and if it is, determine the join type.
805 * Caller must supply not only the two rels, but the union of their relids.
806 * (We could simplify the API by computing joinrelids locally, but this
807 * would be redundant work in the normal path through make_join_rel.)
809 * On success, *sjinfo_p is set to NULL if this is to be a plain inner join,
810 * else it's set to point to the associated SpecialJoinInfo node. Also,
811 * *reversed_p is set TRUE if the given relations need to be swapped to
812 * match the SpecialJoinInfo node.
815 join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
817 SpecialJoinInfo **sjinfo_p, bool *reversed_p)
819 SpecialJoinInfo *match_sjinfo;
822 bool must_be_leftjoin;
826 * Ensure output params are set on failure return. This is just to
827 * suppress uninitialized-variable warnings from overly anal compilers.
833 * If we have any special joins, the proposed join might be illegal; and
834 * in any case we have to determine its join type. Scan the join info
835 * list for matches and conflicts.
839 unique_ified = false;
840 must_be_leftjoin = false;
842 foreach(l, root->join_info_list)
844 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
847 * This special join is not relevant unless its RHS overlaps the
848 * proposed join. (Check this first as a fast path for dismissing
849 * most irrelevant SJs quickly.)
851 if (!bms_overlap(sjinfo->min_righthand, joinrelids))
855 * Also, not relevant if proposed join is fully contained within RHS
856 * (ie, we're still building up the RHS).
858 if (bms_is_subset(joinrelids, sjinfo->min_righthand))
862 * Also, not relevant if SJ is already done within either input.
864 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
865 bms_is_subset(sjinfo->min_righthand, rel1->relids))
867 if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
868 bms_is_subset(sjinfo->min_righthand, rel2->relids))
872 * If it's a semijoin and we already joined the RHS to any other rels
873 * within either input, then we must have unique-ified the RHS at that
874 * point (see below). Therefore the semijoin is no longer relevant in
877 if (sjinfo->jointype == JOIN_SEMI)
879 if (bms_is_subset(sjinfo->syn_righthand, rel1->relids) &&
880 !bms_equal(sjinfo->syn_righthand, rel1->relids))
882 if (bms_is_subset(sjinfo->syn_righthand, rel2->relids) &&
883 !bms_equal(sjinfo->syn_righthand, rel2->relids))
888 * If one input contains min_lefthand and the other contains
889 * min_righthand, then we can perform the SJ at this join.
891 * Reject if we get matches to more than one SJ; that implies we're
892 * considering something that's not really valid.
894 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
895 bms_is_subset(sjinfo->min_righthand, rel2->relids))
898 return false; /* invalid join path */
899 match_sjinfo = sjinfo;
902 else if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
903 bms_is_subset(sjinfo->min_righthand, rel1->relids))
906 return false; /* invalid join path */
907 match_sjinfo = sjinfo;
910 else if (sjinfo->jointype == JOIN_SEMI &&
911 bms_equal(sjinfo->syn_righthand, rel2->relids) &&
912 create_unique_path(root, rel2, rel2->cheapest_total_path,
916 * For a semijoin, we can join the RHS to anything else by
917 * unique-ifying the RHS (if the RHS can be unique-ified).
918 * We will only get here if we have the full RHS but less
919 * than min_lefthand on the LHS.
921 * The reason to consider such a join path is exemplified by
922 * SELECT ... FROM a,b WHERE (a.x,b.y) IN (SELECT c1,c2 FROM c)
923 * If we insist on doing this as a semijoin we will first have
924 * to form the cartesian product of A*B. But if we unique-ify
925 * C then the semijoin becomes a plain innerjoin and we can join
926 * in any order, eg C to A and then to B. When C is much smaller
927 * than A and B this can be a huge win. So we allow C to be
928 * joined to just A or just B here, and then make_join_rel has
929 * to handle the case properly.
931 * Note that actually we'll allow unique-ified C to be joined to
932 * some other relation D here, too. That is legal, if usually not
933 * very sane, and this routine is only concerned with legality not
934 * with whether the join is good strategy.
938 return false; /* invalid join path */
939 match_sjinfo = sjinfo;
943 else if (sjinfo->jointype == JOIN_SEMI &&
944 bms_equal(sjinfo->syn_righthand, rel1->relids) &&
945 create_unique_path(root, rel1, rel1->cheapest_total_path,
948 /* Reversed semijoin case */
950 return false; /* invalid join path */
951 match_sjinfo = sjinfo;
958 * Otherwise, the proposed join overlaps the RHS but isn't a valid
959 * implementation of this SJ. But don't panic quite yet: the RHS
960 * violation might have occurred previously, in one or both input
961 * relations, in which case we must have previously decided that
962 * it was OK to commute some other SJ with this one. If we need
963 * to perform this join to finish building up the RHS, rejecting
964 * it could lead to not finding any plan at all. (This can occur
965 * because of the heuristics elsewhere in this file that postpone
966 * clauseless joins: we might not consider doing a clauseless join
967 * within the RHS until after we've performed other, validly
968 * commutable SJs with one or both sides of the clauseless join.)
969 * This consideration boils down to the rule that if both inputs
970 * overlap the RHS, we can allow the join --- they are either
971 * fully within the RHS, or represent previously-allowed joins to
974 if (bms_overlap(rel1->relids, sjinfo->min_righthand) &&
975 bms_overlap(rel2->relids, sjinfo->min_righthand))
976 continue; /* assume valid previous violation of RHS */
979 * The proposed join could still be legal, but only if we're
980 * allowed to associate it into the RHS of this SJ. That means
981 * this SJ must be a LEFT join (not SEMI or ANTI, and certainly
982 * not FULL) and the proposed join must not overlap the LHS.
984 if (sjinfo->jointype != JOIN_LEFT ||
985 bms_overlap(joinrelids, sjinfo->min_lefthand))
986 return false; /* invalid join path */
989 * To be valid, the proposed join must be a LEFT join; otherwise
990 * it can't associate into this SJ's RHS. But we may not yet have
991 * found the SpecialJoinInfo matching the proposed join, so we
992 * can't test that yet. Remember the requirement for later.
994 must_be_leftjoin = true;
999 * Fail if violated any SJ's RHS and didn't match to a LEFT SJ: the
1000 * proposed join can't associate into an SJ's RHS.
1002 * Also, fail if the proposed join's predicate isn't strict; we're
1003 * essentially checking to see if we can apply outer-join identity 3, and
1004 * that's a requirement. (This check may be redundant with checks in
1005 * make_outerjoininfo, but I'm not quite sure, and it's cheap to test.)
1007 if (must_be_leftjoin &&
1008 (match_sjinfo == NULL ||
1009 match_sjinfo->jointype != JOIN_LEFT ||
1010 !match_sjinfo->lhs_strict))
1011 return false; /* invalid join path */
1014 * We also have to check for constraints imposed by LATERAL references.
1016 if (root->hasLateralRTEs)
1020 Relids join_lateral_rels;
1023 * The proposed rels could each contain lateral references to the
1024 * other, in which case the join is impossible. If there are lateral
1025 * references in just one direction, then the join has to be done with
1026 * a nestloop with the lateral referencer on the inside. If the join
1027 * matches an SJ that cannot be implemented by such a nestloop, the
1028 * join is impossible.
1030 * Also, if the lateral reference is only indirect, we should reject
1031 * the join; whatever rel(s) the reference chain goes through must be
1034 * Another case that might keep us from building a valid plan is the
1035 * implementation restriction described by have_dangerous_phv().
1037 lateral_fwd = bms_overlap(rel1->relids, rel2->lateral_relids);
1038 lateral_rev = bms_overlap(rel2->relids, rel1->lateral_relids);
1039 if (lateral_fwd && lateral_rev)
1040 return false; /* have lateral refs in both directions */
1043 /* has to be implemented as nestloop with rel1 on left */
1047 match_sjinfo->jointype == JOIN_FULL))
1048 return false; /* not implementable as nestloop */
1049 /* check there is a direct reference from rel2 to rel1 */
1050 foreach(l, root->lateral_info_list)
1052 LateralJoinInfo *ljinfo = (LateralJoinInfo *) lfirst(l);
1054 if (bms_is_subset(ljinfo->lateral_rhs, rel2->relids) &&
1055 bms_is_subset(ljinfo->lateral_lhs, rel1->relids))
1059 return false; /* only indirect refs, so reject */
1060 /* check we won't have a dangerous PHV */
1061 if (have_dangerous_phv(root, rel1->relids, rel2->lateral_relids))
1062 return false; /* might be unable to handle required PHV */
1064 else if (lateral_rev)
1066 /* has to be implemented as nestloop with rel2 on left */
1070 match_sjinfo->jointype == JOIN_FULL))
1071 return false; /* not implementable as nestloop */
1072 /* check there is a direct reference from rel1 to rel2 */
1073 foreach(l, root->lateral_info_list)
1075 LateralJoinInfo *ljinfo = (LateralJoinInfo *) lfirst(l);
1077 if (bms_is_subset(ljinfo->lateral_rhs, rel1->relids) &&
1078 bms_is_subset(ljinfo->lateral_lhs, rel2->relids))
1082 return false; /* only indirect refs, so reject */
1083 /* check we won't have a dangerous PHV */
1084 if (have_dangerous_phv(root, rel2->relids, rel1->lateral_relids))
1085 return false; /* might be unable to handle required PHV */
1089 * LATERAL references could also cause problems later on if we accept
1090 * this join: if the join's minimum parameterization includes any rels
1091 * that would have to be on the inside of an outer join with this join
1092 * rel, then it's never going to be possible to build the complete
1093 * query using this join. We should reject this join not only because
1094 * it'll save work, but because if we don't, the clauseless-join
1095 * heuristics might think that legality of this join means that some
1096 * other join rel need not be formed, and that could lead to failure
1097 * to find any plan at all. We have to consider not only rels that
1098 * are directly on the inner side of an OJ with the joinrel, but also
1099 * ones that are indirectly so, so search to find all such rels.
1101 join_lateral_rels = min_join_parameterization(root, joinrelids,
1103 if (join_lateral_rels)
1105 Relids join_plus_rhs = bms_copy(joinrelids);
1111 foreach(l, root->join_info_list)
1113 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
1115 if (bms_overlap(sjinfo->min_lefthand, join_plus_rhs) &&
1116 !bms_is_subset(sjinfo->min_righthand, join_plus_rhs))
1118 join_plus_rhs = bms_add_members(join_plus_rhs,
1119 sjinfo->min_righthand);
1122 /* full joins constrain both sides symmetrically */
1123 if (sjinfo->jointype == JOIN_FULL &&
1124 bms_overlap(sjinfo->min_righthand, join_plus_rhs) &&
1125 !bms_is_subset(sjinfo->min_lefthand, join_plus_rhs))
1127 join_plus_rhs = bms_add_members(join_plus_rhs,
1128 sjinfo->min_lefthand);
1133 if (bms_overlap(join_plus_rhs, join_lateral_rels))
1134 return false; /* will not be able to join to some RHS rel */
1138 /* Otherwise, it's a valid join */
1139 *sjinfo_p = match_sjinfo;
1140 *reversed_p = reversed;
1145 * has_join_restriction
1146 * Detect whether the specified relation has join-order restrictions,
1147 * due to being inside an outer join or an IN (sub-SELECT),
1148 * or participating in any LATERAL references or multi-rel PHVs.
1150 * Essentially, this tests whether have_join_order_restriction() could
1151 * succeed with this rel and some other one. It's OK if we sometimes
1152 * say "true" incorrectly. (Therefore, we don't bother with the relatively
1153 * expensive has_legal_joinclause test.)
1156 has_join_restriction(PlannerInfo *root, RelOptInfo *rel)
1160 if (rel->lateral_relids != NULL || rel->lateral_referencers != NULL)
1163 foreach(l, root->placeholder_list)
1165 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
1167 if (bms_is_subset(rel->relids, phinfo->ph_eval_at) &&
1168 !bms_equal(rel->relids, phinfo->ph_eval_at))
1172 foreach(l, root->join_info_list)
1174 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
1176 /* ignore full joins --- other mechanisms preserve their ordering */
1177 if (sjinfo->jointype == JOIN_FULL)
1180 /* ignore if SJ is already contained in rel */
1181 if (bms_is_subset(sjinfo->min_lefthand, rel->relids) &&
1182 bms_is_subset(sjinfo->min_righthand, rel->relids))
1185 /* restricted if it overlaps LHS or RHS, but doesn't contain SJ */
1186 if (bms_overlap(sjinfo->min_lefthand, rel->relids) ||
1187 bms_overlap(sjinfo->min_righthand, rel->relids))
1195 * is_dummy_rel --- has relation been proven empty?
1198 is_dummy_rel(RelOptInfo *rel)
1200 return IS_DUMMY_REL(rel);
1204 * Mark a relation as proven empty.
1206 * During GEQO planning, this can get invoked more than once on the same
1207 * baserel struct, so it's worth checking to see if the rel is already marked
1210 * Also, when called during GEQO join planning, we are in a short-lived
1211 * memory context. We must make sure that the dummy path attached to a
1212 * baserel survives the GEQO cycle, else the baserel is trashed for future
1213 * GEQO cycles. On the other hand, when we are marking a joinrel during GEQO,
1214 * we don't want the dummy path to clutter the main planning context. Upshot
1215 * is that the best solution is to explicitly make the dummy path in the same
1216 * context the given RelOptInfo is in.
1219 mark_dummy_rel(RelOptInfo *rel)
1221 MemoryContext oldcontext;
1223 /* Already marked? */
1224 if (is_dummy_rel(rel))
1227 /* No, so choose correct context to make the dummy path in */
1228 oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
1230 /* Set dummy size estimate */
1233 /* Evict any previously chosen paths */
1234 rel->pathlist = NIL;
1236 /* Set up the dummy path */
1237 add_path(rel, (Path *) create_append_path(rel, NIL, NULL));
1239 /* Set or update cheapest_total_path and related fields */
1242 MemoryContextSwitchTo(oldcontext);
1246 * restriction_is_constant_false --- is a restrictlist just false?
1248 * In cases where a qual is provably constant false, eval_const_expressions
1249 * will generally have thrown away anything that's ANDed with it. In outer
1250 * join situations this will leave us computing cartesian products only to
1251 * decide there's no match for an outer row, which is pretty stupid. So,
1252 * we need to detect the case.
1254 * If only_pushed_down is true, then consider only quals that are pushed-down
1255 * from the point of view of the joinrel.
1258 restriction_is_constant_false(List *restrictlist,
1259 RelOptInfo *joinrel,
1260 bool only_pushed_down)
1265 * Despite the above comment, the restriction list we see here might
1266 * possibly have other members besides the FALSE constant, since other
1267 * quals could get "pushed down" to the outer join level. So we check
1268 * each member of the list.
1270 foreach(lc, restrictlist)
1272 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1274 Assert(IsA(rinfo, RestrictInfo));
1275 if (only_pushed_down && !RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
1278 if (rinfo->clause && IsA(rinfo->clause, Const))
1280 Const *con = (Const *) rinfo->clause;
1282 /* constant NULL is as good as constant FALSE for our purposes */
1283 if (con->constisnull)
1285 if (!DatumGetBool(con->constvalue))