1 /*-------------------------------------------------------------------------
4 * Routines copied from PostgreSQL core distribution.
6 * The main purpose of this files is having access to static functions in core.
7 * Another purpose is tweaking functions behavior by replacing part of them by
8 * macro definitions. See at the end of pg_hint_plan.c for details. Anyway,
9 * this file *must* contain required functions without making any change.
11 * This file contains the following functions from corresponding files.
13 * src/backend/optimizer/path/allpaths.c
16 * set_plain_rel_pathlist()
17 * add_paths_to_append_rel()
18 * try_partitionwise_join()
21 * standard_join_search(): This funcion is not static. The reason for
22 * including this function is make_rels_by_clause_joins. In order to
23 * avoid generating apparently unwanted join combination, we decided to
24 * change the behavior of make_join_rel, which is called under this
27 * src/backend/optimizer/path/joinrels.c
30 * join_search_one_level(): We have to modify this to call my definition of
31 * make_rels_by_clause_joins.
34 * make_rels_by_clause_joins()
35 * make_rels_by_clauseless_joins()
37 * has_join_restriction()
38 * restriction_is_constant_false()
39 * update_child_rel_info()
40 * build_child_join_sjinfo()
41 * get_matching_part_pairs()
42 * compute_partition_bounds()
45 * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
46 * Portions Copyright (c) 1994, Regents of the University of California
48 *-------------------------------------------------------------------------
51 static void populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1,
52 RelOptInfo *rel2, RelOptInfo *joinrel,
53 SpecialJoinInfo *sjinfo, List *restrictlist);
56 * set_plain_rel_pathlist
57 * Build access paths for a plain relation (no subquery, no inheritance)
60 set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
62 Relids required_outer;
65 * We don't support pushing join clauses into the quals of a seqscan, but
66 * it could still have required parameterization due to LATERAL refs in
69 required_outer = rel->lateral_relids;
71 /* Consider sequential scan */
72 add_path(rel, create_seqscan_path(root, rel, required_outer, 0));
74 /* If appropriate, consider parallel sequential scan */
75 if (rel->consider_parallel && required_outer == NULL)
76 create_plain_partial_paths(root, rel);
78 /* Consider index scans */
79 create_index_paths(root, rel);
81 /* Consider TID scans */
82 create_tidscan_paths(root, rel);
87 * set_append_rel_pathlist
88 * Build access paths for an "append relation"
91 set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
92 Index rti, RangeTblEntry *rte)
94 int parentRTindex = rti;
95 List *live_childrels = NIL;
99 * Generate access paths for each member relation, and remember the
100 * non-dummy children.
102 foreach(l, root->append_rel_list)
104 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
106 RangeTblEntry *childRTE;
107 RelOptInfo *childrel;
109 /* append_rel_list contains all append rels; ignore others */
110 if (appinfo->parent_relid != parentRTindex)
113 /* Re-locate the child RTE and RelOptInfo */
114 childRTindex = appinfo->child_relid;
115 childRTE = root->simple_rte_array[childRTindex];
116 childrel = root->simple_rel_array[childRTindex];
119 * If set_append_rel_size() decided the parent appendrel was
120 * parallel-unsafe at some point after visiting this child rel, we
121 * need to propagate the unsafety marking down to the child, so that
122 * we don't generate useless partial paths for it.
124 if (!rel->consider_parallel)
125 childrel->consider_parallel = false;
128 * Compute the child's access paths.
130 set_rel_pathlist(root, childrel, childRTindex, childRTE);
133 * If child is dummy, ignore it.
135 if (IS_DUMMY_REL(childrel))
138 /* Bubble up childrel's partitioned children. */
139 if (rel->part_scheme)
140 rel->partitioned_child_rels =
141 list_concat(rel->partitioned_child_rels,
142 childrel->partitioned_child_rels);
145 * Child is live, so add it to the live_childrels list for use below.
147 live_childrels = lappend(live_childrels, childrel);
150 /* Add paths to the append relation. */
151 add_paths_to_append_rel(root, rel, live_childrels);
156 * standard_join_search
157 * Find possible joinpaths for a query by successively finding ways
158 * to join component relations into join relations.
160 * 'levels_needed' is the number of iterations needed, ie, the number of
161 * independent jointree items in the query. This is > 1.
163 * 'initial_rels' is a list of RelOptInfo nodes for each independent
164 * jointree item. These are the components to be joined together.
165 * Note that levels_needed == list_length(initial_rels).
167 * Returns the final level of join relations, i.e., the relation that is
168 * the result of joining all the original relations together.
169 * At least one implementation path must be provided for this relation and
170 * all required sub-relations.
172 * To support loadable plugins that modify planner behavior by changing the
173 * join searching algorithm, we provide a hook variable that lets a plugin
174 * replace or supplement this function. Any such hook must return the same
175 * final join relation as the standard code would, but it might have a
176 * different set of implementation paths attached, and only the sub-joinrels
177 * needed for these paths need have been instantiated.
179 * Note to plugin authors: the functions invoked during standard_join_search()
180 * modify root->join_rel_list and root->join_rel_hash. If you want to do more
181 * than one join-order search, you'll probably need to save and restore the
182 * original states of those data structures. See geqo_eval() for an example.
185 standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
191 * This function cannot be invoked recursively within any one planning
192 * problem, so join_rel_level[] can't be in use already.
194 Assert(root->join_rel_level == NULL);
197 * We employ a simple "dynamic programming" algorithm: we first find all
198 * ways to build joins of two jointree items, then all ways to build joins
199 * of three items (from two-item joins and single items), then four-item
200 * joins, and so on until we have considered all ways to join all the
201 * items into one rel.
203 * root->join_rel_level[j] is a list of all the j-item rels. Initially we
204 * set root->join_rel_level[1] to represent all the single-jointree-item
207 root->join_rel_level = (List **) palloc0((levels_needed + 1) * sizeof(List *));
209 root->join_rel_level[1] = initial_rels;
211 for (lev = 2; lev <= levels_needed; lev++)
216 * Determine all possible pairs of relations to be joined at this
217 * level, and build paths for making each one from every available
218 * pair of lower-level relations.
220 join_search_one_level(root, lev);
223 * Run generate_partitionwise_join_paths() and generate_gather_paths()
224 * for each just-processed joinrel. We could not do this earlier
225 * because both regular and partial paths can get added to a
226 * particular joinrel at multiple times within join_search_one_level.
228 * After that, we're done creating paths for the joinrel, so run
231 foreach(lc, root->join_rel_level[lev])
233 rel = (RelOptInfo *) lfirst(lc);
235 /* Create paths for partitionwise joins. */
236 generate_partitionwise_join_paths(root, rel);
239 * Except for the topmost scan/join rel, consider gathering
240 * partial paths. We'll do the same for the topmost scan/join rel
241 * once we know the final targetlist (see grouping_planner).
243 if (lev < levels_needed)
244 generate_useful_gather_paths(root, rel, false);
246 /* Find and save the cheapest paths for this rel */
249 #ifdef OPTIMIZER_DEBUG
250 debug_print_rel(root, rel);
256 * We should have a single rel at the final level.
258 if (root->join_rel_level[levels_needed] == NIL)
259 elog(ERROR, "failed to build any %d-way joins", levels_needed);
260 Assert(list_length(root->join_rel_level[levels_needed]) == 1);
262 rel = (RelOptInfo *) linitial(root->join_rel_level[levels_needed]);
264 root->join_rel_level = NULL;
270 * create_plain_partial_paths
271 * Build partial access paths for parallel scan of a plain relation
274 create_plain_partial_paths(PlannerInfo *root, RelOptInfo *rel)
276 int parallel_workers;
278 parallel_workers = compute_parallel_worker(rel, rel->pages, -1,
279 max_parallel_workers_per_gather);
281 /* If any limit was set to zero, the user doesn't want a parallel scan. */
282 if (parallel_workers <= 0)
285 /* Add an unordered partial path based on a parallel sequential scan. */
286 add_partial_path(rel, create_seqscan_path(root, rel, NULL, parallel_workers));
291 * join_search_one_level
292 * Consider ways to produce join relations containing exactly 'level'
293 * jointree items. (This is one step of the dynamic-programming method
294 * embodied in standard_join_search.) Join rel nodes for each feasible
295 * combination of lower-level rels are created and returned in a list.
296 * Implementation paths are created for each such joinrel, too.
298 * level: level of rels we want to make this time
299 * root->join_rel_level[j], 1 <= j < level, is a list of rels containing j items
301 * The result is returned in root->join_rel_level[level].
304 join_search_one_level(PlannerInfo *root, int level)
306 List **joinrels = root->join_rel_level;
310 Assert(joinrels[level] == NIL);
312 /* Set join_cur_level so that new joinrels are added to proper list */
313 root->join_cur_level = level;
316 * First, consider left-sided and right-sided plans, in which rels of
317 * exactly level-1 member relations are joined against initial relations.
318 * We prefer to join using join clauses, but if we find a rel of level-1
319 * members that has no join clauses, we will generate Cartesian-product
320 * joins against all initial rels not already contained in it.
322 foreach(r, joinrels[level - 1])
324 RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
326 if (old_rel->joininfo != NIL || old_rel->has_eclass_joins ||
327 has_join_restriction(root, old_rel))
330 * There are join clauses or join order restrictions relevant to
331 * this rel, so consider joins between this rel and (only) those
332 * initial rels it is linked to by a clause or restriction.
334 * At level 2 this condition is symmetric, so there is no need to
335 * look at initial rels before this one in the list; we already
336 * considered such joins when we were at the earlier rel. (The
337 * mirror-image joins are handled automatically by make_join_rel.)
338 * In later passes (level > 2), we join rels of the previous level
339 * to each initial rel they don't already include but have a join
340 * clause or restriction with.
342 List *other_rels_list;
343 ListCell *other_rels;
345 if (level == 2) /* consider remaining initial rels */
347 other_rels_list = joinrels[level - 1];
348 other_rels = lnext(other_rels_list, r);
350 else /* consider all initial rels */
352 other_rels_list = joinrels[1];
353 other_rels = list_head(other_rels_list);
356 make_rels_by_clause_joins(root,
364 * Oops, we have a relation that is not joined to any other
365 * relation, either directly or by join-order restrictions.
366 * Cartesian product time.
368 * We consider a cartesian product with each not-already-included
369 * initial rel, whether it has other join clauses or not. At
370 * level 2, if there are two or more clauseless initial rels, we
371 * will redundantly consider joining them in both directions; but
372 * such cases aren't common enough to justify adding complexity to
373 * avoid the duplicated effort.
375 make_rels_by_clauseless_joins(root,
382 * Now, consider "bushy plans" in which relations of k initial rels are
383 * joined to relations of level-k initial rels, for 2 <= k <= level-2.
385 * We only consider bushy-plan joins for pairs of rels where there is a
386 * suitable join clause (or join order restriction), in order to avoid
387 * unreasonable growth of planning time.
391 int other_level = level - k;
394 * Since make_join_rel(x, y) handles both x,y and y,x cases, we only
395 * need to go as far as the halfway point.
400 foreach(r, joinrels[k])
402 RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
403 List *other_rels_list;
404 ListCell *other_rels;
408 * We can ignore relations without join clauses here, unless they
409 * participate in join-order restrictions --- then we might have
410 * to force a bushy join plan.
412 if (old_rel->joininfo == NIL && !old_rel->has_eclass_joins &&
413 !has_join_restriction(root, old_rel))
416 if (k == other_level)
418 /* only consider remaining rels */
419 other_rels_list = joinrels[k];
420 other_rels = lnext(other_rels_list, r);
424 other_rels_list = joinrels[other_level];
425 other_rels = list_head(other_rels_list);
428 for_each_cell(r2, other_rels_list, other_rels)
430 RelOptInfo *new_rel = (RelOptInfo *) lfirst(r2);
432 if (!bms_overlap(old_rel->relids, new_rel->relids))
435 * OK, we can build a rel of the right level from this
436 * pair of rels. Do so if there is at least one relevant
437 * join clause or join order restriction.
439 if (have_relevant_joinclause(root, old_rel, new_rel) ||
440 have_join_order_restriction(root, old_rel, new_rel))
442 (void) make_join_rel(root, old_rel, new_rel);
450 * Last-ditch effort: if we failed to find any usable joins so far, force
451 * a set of cartesian-product joins to be generated. This handles the
452 * special case where all the available rels have join clauses but we
453 * cannot use any of those clauses yet. This can only happen when we are
454 * considering a join sub-problem (a sub-joinlist) and all the rels in the
455 * sub-problem have only join clauses with rels outside the sub-problem.
458 * SELECT ... FROM a INNER JOIN b ON TRUE, c, d, ...
459 * WHERE a.w = c.x and b.y = d.z;
461 * If the "a INNER JOIN b" sub-problem does not get flattened into the
462 * upper level, we must be willing to make a cartesian join of a and b;
463 * but the code above will not have done so, because it thought that both
464 * a and b have joinclauses. We consider only left-sided and right-sided
465 * cartesian joins in this case (no bushy).
468 if (joinrels[level] == NIL)
471 * This loop is just like the first one, except we always call
472 * make_rels_by_clauseless_joins().
474 foreach(r, joinrels[level - 1])
476 RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
478 make_rels_by_clauseless_joins(root,
484 * When special joins are involved, there may be no legal way
485 * to make an N-way join for some values of N. For example consider
487 * SELECT ... FROM t1 WHERE
488 * x IN (SELECT ... FROM t2,t3 WHERE ...) AND
489 * y IN (SELECT ... FROM t4,t5 WHERE ...)
491 * We will flatten this query to a 5-way join problem, but there are
492 * no 4-way joins that join_is_legal() will consider legal. We have
493 * to accept failure at level 4 and go on to discover a workable
494 * bushy plan at level 5.
496 * However, if there are no special joins and no lateral references
497 * then join_is_legal() should never fail, and so the following sanity
501 if (joinrels[level] == NIL &&
502 root->join_info_list == NIL &&
503 !root->hasLateralRTEs)
504 elog(ERROR, "failed to build any %d-way joins", level);
510 * make_rels_by_clause_joins
511 * Build joins between the given relation 'old_rel' and other relations
512 * that participate in join clauses that 'old_rel' also participates in
513 * (or participate in join-order restrictions with it).
514 * The join rels are returned in root->join_rel_level[join_cur_level].
516 * Note: at levels above 2 we will generate the same joined relation in
517 * multiple ways --- for example (a join b) join c is the same RelOptInfo as
518 * (b join c) join a, though the second case will add a different set of Paths
519 * to it. This is the reason for using the join_rel_level mechanism, which
520 * automatically ensures that each new joinrel is only added to the list once.
522 * 'old_rel' is the relation entry for the relation to be joined
523 * 'other_rels_list': a list containing the other
524 * rels to be considered for joining
525 * 'other_rels': the first cell to be considered
527 * Currently, this is only used with initial rels in other_rels, but it
528 * will work for joining to joinrels too.
531 make_rels_by_clause_joins(PlannerInfo *root,
533 List *other_rels_list,
534 ListCell *other_rels)
538 for_each_cell(l, other_rels_list, other_rels)
540 RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
542 if (!bms_overlap(old_rel->relids, other_rel->relids) &&
543 (have_relevant_joinclause(root, old_rel, other_rel) ||
544 have_join_order_restriction(root, old_rel, other_rel)))
546 (void) make_join_rel(root, old_rel, other_rel);
553 * make_rels_by_clauseless_joins
554 * Given a relation 'old_rel' and a list of other relations
555 * 'other_rels', create a join relation between 'old_rel' and each
556 * member of 'other_rels' that isn't already included in 'old_rel'.
557 * The join rels are returned in root->join_rel_level[join_cur_level].
559 * 'old_rel' is the relation entry for the relation to be joined
560 * 'other_rels': a list containing the other rels to be considered for joining
562 * Currently, this is only used with initial rels in other_rels, but it would
563 * work for joining to joinrels too.
566 make_rels_by_clauseless_joins(PlannerInfo *root,
572 foreach(l, other_rels)
574 RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
576 if (!bms_overlap(other_rel->relids, old_rel->relids))
578 (void) make_join_rel(root, old_rel, other_rel);
586 * Determine whether a proposed join is legal given the query's
587 * join order constraints; and if it is, determine the join type.
589 * Caller must supply not only the two rels, but the union of their relids.
590 * (We could simplify the API by computing joinrelids locally, but this
591 * would be redundant work in the normal path through make_join_rel.)
593 * On success, *sjinfo_p is set to NULL if this is to be a plain inner join,
594 * else it's set to point to the associated SpecialJoinInfo node. Also,
595 * *reversed_p is set true if the given relations need to be swapped to
596 * match the SpecialJoinInfo node.
599 join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
601 SpecialJoinInfo **sjinfo_p, bool *reversed_p)
603 SpecialJoinInfo *match_sjinfo;
606 bool must_be_leftjoin;
610 * Ensure output params are set on failure return. This is just to
611 * suppress uninitialized-variable warnings from overly anal compilers.
617 * If we have any special joins, the proposed join might be illegal; and
618 * in any case we have to determine its join type. Scan the join info
619 * list for matches and conflicts.
623 unique_ified = false;
624 must_be_leftjoin = false;
626 foreach(l, root->join_info_list)
628 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
631 * This special join is not relevant unless its RHS overlaps the
632 * proposed join. (Check this first as a fast path for dismissing
633 * most irrelevant SJs quickly.)
635 if (!bms_overlap(sjinfo->min_righthand, joinrelids))
639 * Also, not relevant if proposed join is fully contained within RHS
640 * (ie, we're still building up the RHS).
642 if (bms_is_subset(joinrelids, sjinfo->min_righthand))
646 * Also, not relevant if SJ is already done within either input.
648 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
649 bms_is_subset(sjinfo->min_righthand, rel1->relids))
651 if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
652 bms_is_subset(sjinfo->min_righthand, rel2->relids))
656 * If it's a semijoin and we already joined the RHS to any other rels
657 * within either input, then we must have unique-ified the RHS at that
658 * point (see below). Therefore the semijoin is no longer relevant in
661 if (sjinfo->jointype == JOIN_SEMI)
663 if (bms_is_subset(sjinfo->syn_righthand, rel1->relids) &&
664 !bms_equal(sjinfo->syn_righthand, rel1->relids))
666 if (bms_is_subset(sjinfo->syn_righthand, rel2->relids) &&
667 !bms_equal(sjinfo->syn_righthand, rel2->relids))
672 * If one input contains min_lefthand and the other contains
673 * min_righthand, then we can perform the SJ at this join.
675 * Reject if we get matches to more than one SJ; that implies we're
676 * considering something that's not really valid.
678 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
679 bms_is_subset(sjinfo->min_righthand, rel2->relids))
682 return false; /* invalid join path */
683 match_sjinfo = sjinfo;
686 else if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
687 bms_is_subset(sjinfo->min_righthand, rel1->relids))
690 return false; /* invalid join path */
691 match_sjinfo = sjinfo;
694 else if (sjinfo->jointype == JOIN_SEMI &&
695 bms_equal(sjinfo->syn_righthand, rel2->relids) &&
696 create_unique_path(root, rel2, rel2->cheapest_total_path,
700 * For a semijoin, we can join the RHS to anything else by
701 * unique-ifying the RHS (if the RHS can be unique-ified).
702 * We will only get here if we have the full RHS but less
703 * than min_lefthand on the LHS.
705 * The reason to consider such a join path is exemplified by
706 * SELECT ... FROM a,b WHERE (a.x,b.y) IN (SELECT c1,c2 FROM c)
707 * If we insist on doing this as a semijoin we will first have
708 * to form the cartesian product of A*B. But if we unique-ify
709 * C then the semijoin becomes a plain innerjoin and we can join
710 * in any order, eg C to A and then to B. When C is much smaller
711 * than A and B this can be a huge win. So we allow C to be
712 * joined to just A or just B here, and then make_join_rel has
713 * to handle the case properly.
715 * Note that actually we'll allow unique-ified C to be joined to
716 * some other relation D here, too. That is legal, if usually not
717 * very sane, and this routine is only concerned with legality not
718 * with whether the join is good strategy.
722 return false; /* invalid join path */
723 match_sjinfo = sjinfo;
727 else if (sjinfo->jointype == JOIN_SEMI &&
728 bms_equal(sjinfo->syn_righthand, rel1->relids) &&
729 create_unique_path(root, rel1, rel1->cheapest_total_path,
732 /* Reversed semijoin case */
734 return false; /* invalid join path */
735 match_sjinfo = sjinfo;
742 * Otherwise, the proposed join overlaps the RHS but isn't a valid
743 * implementation of this SJ. But don't panic quite yet: the RHS
744 * violation might have occurred previously, in one or both input
745 * relations, in which case we must have previously decided that
746 * it was OK to commute some other SJ with this one. If we need
747 * to perform this join to finish building up the RHS, rejecting
748 * it could lead to not finding any plan at all. (This can occur
749 * because of the heuristics elsewhere in this file that postpone
750 * clauseless joins: we might not consider doing a clauseless join
751 * within the RHS until after we've performed other, validly
752 * commutable SJs with one or both sides of the clauseless join.)
753 * This consideration boils down to the rule that if both inputs
754 * overlap the RHS, we can allow the join --- they are either
755 * fully within the RHS, or represent previously-allowed joins to
758 if (bms_overlap(rel1->relids, sjinfo->min_righthand) &&
759 bms_overlap(rel2->relids, sjinfo->min_righthand))
760 continue; /* assume valid previous violation of RHS */
763 * The proposed join could still be legal, but only if we're
764 * allowed to associate it into the RHS of this SJ. That means
765 * this SJ must be a LEFT join (not SEMI or ANTI, and certainly
766 * not FULL) and the proposed join must not overlap the LHS.
768 if (sjinfo->jointype != JOIN_LEFT ||
769 bms_overlap(joinrelids, sjinfo->min_lefthand))
770 return false; /* invalid join path */
773 * To be valid, the proposed join must be a LEFT join; otherwise
774 * it can't associate into this SJ's RHS. But we may not yet have
775 * found the SpecialJoinInfo matching the proposed join, so we
776 * can't test that yet. Remember the requirement for later.
778 must_be_leftjoin = true;
783 * Fail if violated any SJ's RHS and didn't match to a LEFT SJ: the
784 * proposed join can't associate into an SJ's RHS.
786 * Also, fail if the proposed join's predicate isn't strict; we're
787 * essentially checking to see if we can apply outer-join identity 3, and
788 * that's a requirement. (This check may be redundant with checks in
789 * make_outerjoininfo, but I'm not quite sure, and it's cheap to test.)
791 if (must_be_leftjoin &&
792 (match_sjinfo == NULL ||
793 match_sjinfo->jointype != JOIN_LEFT ||
794 !match_sjinfo->lhs_strict))
795 return false; /* invalid join path */
798 * We also have to check for constraints imposed by LATERAL references.
800 if (root->hasLateralRTEs)
804 Relids join_lateral_rels;
807 * The proposed rels could each contain lateral references to the
808 * other, in which case the join is impossible. If there are lateral
809 * references in just one direction, then the join has to be done with
810 * a nestloop with the lateral referencer on the inside. If the join
811 * matches an SJ that cannot be implemented by such a nestloop, the
812 * join is impossible.
814 * Also, if the lateral reference is only indirect, we should reject
815 * the join; whatever rel(s) the reference chain goes through must be
818 * Another case that might keep us from building a valid plan is the
819 * implementation restriction described by have_dangerous_phv().
821 lateral_fwd = bms_overlap(rel1->relids, rel2->lateral_relids);
822 lateral_rev = bms_overlap(rel2->relids, rel1->lateral_relids);
823 if (lateral_fwd && lateral_rev)
824 return false; /* have lateral refs in both directions */
827 /* has to be implemented as nestloop with rel1 on left */
831 match_sjinfo->jointype == JOIN_FULL))
832 return false; /* not implementable as nestloop */
833 /* check there is a direct reference from rel2 to rel1 */
834 if (!bms_overlap(rel1->relids, rel2->direct_lateral_relids))
835 return false; /* only indirect refs, so reject */
836 /* check we won't have a dangerous PHV */
837 if (have_dangerous_phv(root, rel1->relids, rel2->lateral_relids))
838 return false; /* might be unable to handle required PHV */
840 else if (lateral_rev)
842 /* has to be implemented as nestloop with rel2 on left */
846 match_sjinfo->jointype == JOIN_FULL))
847 return false; /* not implementable as nestloop */
848 /* check there is a direct reference from rel1 to rel2 */
849 if (!bms_overlap(rel2->relids, rel1->direct_lateral_relids))
850 return false; /* only indirect refs, so reject */
851 /* check we won't have a dangerous PHV */
852 if (have_dangerous_phv(root, rel2->relids, rel1->lateral_relids))
853 return false; /* might be unable to handle required PHV */
857 * LATERAL references could also cause problems later on if we accept
858 * this join: if the join's minimum parameterization includes any rels
859 * that would have to be on the inside of an outer join with this join
860 * rel, then it's never going to be possible to build the complete
861 * query using this join. We should reject this join not only because
862 * it'll save work, but because if we don't, the clauseless-join
863 * heuristics might think that legality of this join means that some
864 * other join rel need not be formed, and that could lead to failure
865 * to find any plan at all. We have to consider not only rels that
866 * are directly on the inner side of an OJ with the joinrel, but also
867 * ones that are indirectly so, so search to find all such rels.
869 join_lateral_rels = min_join_parameterization(root, joinrelids,
871 if (join_lateral_rels)
873 Relids join_plus_rhs = bms_copy(joinrelids);
879 foreach(l, root->join_info_list)
881 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
883 /* ignore full joins --- their ordering is predetermined */
884 if (sjinfo->jointype == JOIN_FULL)
887 if (bms_overlap(sjinfo->min_lefthand, join_plus_rhs) &&
888 !bms_is_subset(sjinfo->min_righthand, join_plus_rhs))
890 join_plus_rhs = bms_add_members(join_plus_rhs,
891 sjinfo->min_righthand);
896 if (bms_overlap(join_plus_rhs, join_lateral_rels))
897 return false; /* will not be able to join to some RHS rel */
901 /* Otherwise, it's a valid join */
902 *sjinfo_p = match_sjinfo;
903 *reversed_p = reversed;
909 * has_join_restriction
910 * Detect whether the specified relation has join-order restrictions,
911 * due to being inside an outer join or an IN (sub-SELECT),
912 * or participating in any LATERAL references or multi-rel PHVs.
914 * Essentially, this tests whether have_join_order_restriction() could
915 * succeed with this rel and some other one. It's OK if we sometimes
916 * say "true" incorrectly. (Therefore, we don't bother with the relatively
917 * expensive has_legal_joinclause test.)
920 has_join_restriction(PlannerInfo *root, RelOptInfo *rel)
924 if (rel->lateral_relids != NULL || rel->lateral_referencers != NULL)
927 foreach(l, root->placeholder_list)
929 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
931 if (bms_is_subset(rel->relids, phinfo->ph_eval_at) &&
932 !bms_equal(rel->relids, phinfo->ph_eval_at))
936 foreach(l, root->join_info_list)
938 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
940 /* ignore full joins --- other mechanisms preserve their ordering */
941 if (sjinfo->jointype == JOIN_FULL)
944 /* ignore if SJ is already contained in rel */
945 if (bms_is_subset(sjinfo->min_lefthand, rel->relids) &&
946 bms_is_subset(sjinfo->min_righthand, rel->relids))
949 /* restricted if it overlaps LHS or RHS, but doesn't contain SJ */
950 if (bms_overlap(sjinfo->min_lefthand, rel->relids) ||
951 bms_overlap(sjinfo->min_righthand, rel->relids))
959 * restriction_is_constant_false --- is a restrictlist just FALSE?
961 * In cases where a qual is provably constant FALSE, eval_const_expressions
962 * will generally have thrown away anything that's ANDed with it. In outer
963 * join situations this will leave us computing cartesian products only to
964 * decide there's no match for an outer row, which is pretty stupid. So,
965 * we need to detect the case.
967 * If only_pushed_down is true, then consider only quals that are pushed-down
968 * from the point of view of the joinrel.
971 restriction_is_constant_false(List *restrictlist,
973 bool only_pushed_down)
978 * Despite the above comment, the restriction list we see here might
979 * possibly have other members besides the FALSE constant, since other
980 * quals could get "pushed down" to the outer join level. So we check
981 * each member of the list.
983 foreach(lc, restrictlist)
985 RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
987 if (only_pushed_down && !RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
990 if (rinfo->clause && IsA(rinfo->clause, Const))
992 Const *con = (Const *) rinfo->clause;
994 /* constant NULL is as good as constant FALSE for our purposes */
995 if (con->constisnull)
997 if (!DatumGetBool(con->constvalue))
1005 * Construct the SpecialJoinInfo for a child-join by translating
1006 * SpecialJoinInfo for the join between parents. left_relids and right_relids
1007 * are the relids of left and right side of the join respectively.
1009 static SpecialJoinInfo *
1010 build_child_join_sjinfo(PlannerInfo *root, SpecialJoinInfo *parent_sjinfo,
1011 Relids left_relids, Relids right_relids)
1013 SpecialJoinInfo *sjinfo = makeNode(SpecialJoinInfo);
1014 AppendRelInfo **left_appinfos;
1016 AppendRelInfo **right_appinfos;
1017 int right_nappinfos;
1019 memcpy(sjinfo, parent_sjinfo, sizeof(SpecialJoinInfo));
1020 left_appinfos = find_appinfos_by_relids(root, left_relids,
1022 right_appinfos = find_appinfos_by_relids(root, right_relids,
1025 sjinfo->min_lefthand = adjust_child_relids(sjinfo->min_lefthand,
1026 left_nappinfos, left_appinfos);
1027 sjinfo->min_righthand = adjust_child_relids(sjinfo->min_righthand,
1030 sjinfo->syn_lefthand = adjust_child_relids(sjinfo->syn_lefthand,
1031 left_nappinfos, left_appinfos);
1032 sjinfo->syn_righthand = adjust_child_relids(sjinfo->syn_righthand,
1035 sjinfo->semi_rhs_exprs = (List *) adjust_appendrel_attrs(root,
1036 (Node *) sjinfo->semi_rhs_exprs,
1040 pfree(left_appinfos);
1041 pfree(right_appinfos);
1047 * get_matching_part_pairs
1048 * Generate pairs of partitions to be joined from inputs
1051 get_matching_part_pairs(PlannerInfo *root, RelOptInfo *joinrel,
1052 RelOptInfo *rel1, RelOptInfo *rel2,
1053 List **parts1, List **parts2)
1055 bool rel1_is_simple = IS_SIMPLE_REL(rel1);
1056 bool rel2_is_simple = IS_SIMPLE_REL(rel2);
1062 for (cnt_parts = 0; cnt_parts < joinrel->nparts; cnt_parts++)
1064 RelOptInfo *child_joinrel = joinrel->part_rels[cnt_parts];
1065 RelOptInfo *child_rel1;
1066 RelOptInfo *child_rel2;
1067 Relids child_relids1;
1068 Relids child_relids2;
1071 * If this segment of the join is empty, it means that this segment
1072 * was ignored when previously creating child-join paths for it in
1073 * try_partitionwise_join() as it would not contribute to the join
1074 * result, due to one or both inputs being empty; add NULL to each of
1075 * the given lists so that this segment will be ignored again in that
1080 *parts1 = lappend(*parts1, NULL);
1081 *parts2 = lappend(*parts2, NULL);
1086 * Get a relids set of partition(s) involved in this join segment that
1087 * are from the rel1 side.
1089 child_relids1 = bms_intersect(child_joinrel->relids,
1090 rel1->all_partrels);
1091 Assert(bms_num_members(child_relids1) == bms_num_members(rel1->relids));
1094 * Get a child rel for rel1 with the relids. Note that we should have
1095 * the child rel even if rel1 is a join rel, because in that case the
1096 * partitions specified in the relids would have matching/overlapping
1097 * boundaries, so the specified partitions should be considered as
1098 * ones to be joined when planning partitionwise joins of rel1,
1099 * meaning that the child rel would have been built by the time we get
1104 int varno = bms_singleton_member(child_relids1);
1106 child_rel1 = find_base_rel(root, varno);
1109 child_rel1 = find_join_rel(root, child_relids1);
1113 * Get a relids set of partition(s) involved in this join segment that
1114 * are from the rel2 side.
1116 child_relids2 = bms_intersect(child_joinrel->relids,
1117 rel2->all_partrels);
1118 Assert(bms_num_members(child_relids2) == bms_num_members(rel2->relids));
1121 * Get a child rel for rel2 with the relids. See above comments.
1125 int varno = bms_singleton_member(child_relids2);
1127 child_rel2 = find_base_rel(root, varno);
1130 child_rel2 = find_join_rel(root, child_relids2);
1134 * The join of rel1 and rel2 is legal, so is the join of the child
1135 * rels obtained above; add them to the given lists as a join pair
1136 * producing this join segment.
1138 *parts1 = lappend(*parts1, child_rel1);
1139 *parts2 = lappend(*parts2, child_rel2);
1145 * compute_partition_bounds
1146 * Compute the partition bounds for a join rel from those for inputs
1149 compute_partition_bounds(PlannerInfo *root, RelOptInfo *rel1,
1150 RelOptInfo *rel2, RelOptInfo *joinrel,
1151 SpecialJoinInfo *parent_sjinfo,
1152 List **parts1, List **parts2)
1155 * If we don't have the partition bounds for the join rel yet, try to
1156 * compute those along with pairs of partitions to be joined.
1158 if (joinrel->nparts == -1)
1160 PartitionScheme part_scheme = joinrel->part_scheme;
1161 PartitionBoundInfo boundinfo = NULL;
1164 Assert(joinrel->boundinfo == NULL);
1165 Assert(joinrel->part_rels == NULL);
1168 * See if the partition bounds for inputs are exactly the same, in
1169 * which case we don't need to work hard: the join rel have the same
1170 * partition bounds as inputs, and the partitions with the same
1171 * cardinal positions form the pairs.
1173 * Note: even in cases where one or both inputs have merged bounds, it
1174 * would be possible for both the bounds to be exactly the same, but
1175 * it seems unlikely to be worth the cycles to check.
1177 if (!rel1->partbounds_merged &&
1178 !rel2->partbounds_merged &&
1179 rel1->nparts == rel2->nparts &&
1180 partition_bounds_equal(part_scheme->partnatts,
1181 part_scheme->parttyplen,
1182 part_scheme->parttypbyval,
1183 rel1->boundinfo, rel2->boundinfo))
1185 boundinfo = rel1->boundinfo;
1186 nparts = rel1->nparts;
1190 /* Try merging the partition bounds for inputs. */
1191 boundinfo = partition_bounds_merge(part_scheme->partnatts,
1192 part_scheme->partsupfunc,
1193 part_scheme->partcollation,
1195 parent_sjinfo->jointype,
1197 if (boundinfo == NULL)
1199 joinrel->nparts = 0;
1202 nparts = list_length(*parts1);
1203 joinrel->partbounds_merged = true;
1207 joinrel->boundinfo = boundinfo;
1208 joinrel->nparts = nparts;
1209 joinrel->part_rels =
1210 (RelOptInfo **) palloc0(sizeof(RelOptInfo *) * nparts);
1214 Assert(joinrel->nparts > 0);
1215 Assert(joinrel->boundinfo);
1216 Assert(joinrel->part_rels);
1219 * If the join rel's partbounds_merged flag is true, it means inputs
1220 * are not guaranteed to have the same partition bounds, therefore we
1221 * can't assume that the partitions at the same cardinal positions
1222 * form the pairs; let get_matching_part_pairs() generate the pairs.
1223 * Otherwise, nothing to do since we can assume that.
1225 if (joinrel->partbounds_merged)
1227 get_matching_part_pairs(root, joinrel, rel1, rel2,
1229 Assert(list_length(*parts1) == joinrel->nparts);
1230 Assert(list_length(*parts2) == joinrel->nparts);
1237 * Assess whether join between given two partitioned relations can be broken
1238 * down into joins between matching partitions; a technique called
1239 * "partitionwise join"
1241 * Partitionwise join is possible when a. Joining relations have same
1242 * partitioning scheme b. There exists an equi-join between the partition keys
1243 * of the two relations.
1245 * Partitionwise join is planned as follows (details: optimizer/README.)
1247 * 1. Create the RelOptInfos for joins between matching partitions i.e
1248 * child-joins and add paths to them.
1250 * 2. Construct Append or MergeAppend paths across the set of child joins.
1251 * This second phase is implemented by generate_partitionwise_join_paths().
1253 * The RelOptInfo, SpecialJoinInfo and restrictlist for each child join are
1254 * obtained by translating the respective parent join structures.
1257 try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
1258 RelOptInfo *joinrel, SpecialJoinInfo *parent_sjinfo,
1259 List *parent_restrictlist)
1261 bool rel1_is_simple = IS_SIMPLE_REL(rel1);
1262 bool rel2_is_simple = IS_SIMPLE_REL(rel2);
1265 ListCell *lcr1 = NULL;
1266 ListCell *lcr2 = NULL;
1269 /* Guard against stack overflow due to overly deep partition hierarchy. */
1270 check_stack_depth();
1272 /* Nothing to do, if the join relation is not partitioned. */
1273 if (joinrel->part_scheme == NULL || joinrel->nparts == 0)
1276 /* The join relation should have consider_partitionwise_join set. */
1277 Assert(joinrel->consider_partitionwise_join);
1280 * We can not perform partitionwise join if either of the joining
1281 * relations is not partitioned.
1283 if (!IS_PARTITIONED_REL(rel1) || !IS_PARTITIONED_REL(rel2))
1286 Assert(REL_HAS_ALL_PART_PROPS(rel1) && REL_HAS_ALL_PART_PROPS(rel2));
1288 /* The joining relations should have consider_partitionwise_join set. */
1289 Assert(rel1->consider_partitionwise_join &&
1290 rel2->consider_partitionwise_join);
1293 * The partition scheme of the join relation should match that of the
1294 * joining relations.
1296 Assert(joinrel->part_scheme == rel1->part_scheme &&
1297 joinrel->part_scheme == rel2->part_scheme);
1299 Assert(!(joinrel->partbounds_merged && (joinrel->nparts <= 0)));
1301 compute_partition_bounds(root, rel1, rel2, joinrel, parent_sjinfo,
1304 if (joinrel->partbounds_merged)
1306 lcr1 = list_head(parts1);
1307 lcr2 = list_head(parts2);
1311 * Create child-join relations for this partitioned join, if those don't
1312 * exist. Add paths to child-joins for a pair of child relations
1313 * corresponding to the given pair of parent relations.
1315 for (cnt_parts = 0; cnt_parts < joinrel->nparts; cnt_parts++)
1317 RelOptInfo *child_rel1;
1318 RelOptInfo *child_rel2;
1321 SpecialJoinInfo *child_sjinfo;
1322 List *child_restrictlist;
1323 RelOptInfo *child_joinrel;
1324 Relids child_joinrelids;
1325 AppendRelInfo **appinfos;
1328 if (joinrel->partbounds_merged)
1330 child_rel1 = lfirst_node(RelOptInfo, lcr1);
1331 child_rel2 = lfirst_node(RelOptInfo, lcr2);
1332 lcr1 = lnext(parts1, lcr1);
1333 lcr2 = lnext(parts2, lcr2);
1337 child_rel1 = rel1->part_rels[cnt_parts];
1338 child_rel2 = rel2->part_rels[cnt_parts];
1341 rel1_empty = (child_rel1 == NULL || IS_DUMMY_REL(child_rel1));
1342 rel2_empty = (child_rel2 == NULL || IS_DUMMY_REL(child_rel2));
1345 * Check for cases where we can prove that this segment of the join
1346 * returns no rows, due to one or both inputs being empty (including
1347 * inputs that have been pruned away entirely). If so just ignore it.
1348 * These rules are equivalent to populate_joinrel_with_paths's rules
1349 * for dummy input relations.
1351 switch (parent_sjinfo->jointype)
1355 if (rel1_empty || rel2_empty)
1356 continue; /* ignore this join segment */
1361 continue; /* ignore this join segment */
1364 if (rel1_empty && rel2_empty)
1365 continue; /* ignore this join segment */
1368 /* other values not expected here */
1369 elog(ERROR, "unrecognized join type: %d",
1370 (int) parent_sjinfo->jointype);
1375 * If a child has been pruned entirely then we can't generate paths
1376 * for it, so we have to reject partitionwise joining unless we were
1377 * able to eliminate this partition above.
1379 if (child_rel1 == NULL || child_rel2 == NULL)
1382 * Mark the joinrel as unpartitioned so that later functions treat
1385 joinrel->nparts = 0;
1390 * If a leaf relation has consider_partitionwise_join=false, it means
1391 * that it's a dummy relation for which we skipped setting up tlist
1392 * expressions and adding EC members in set_append_rel_size(), so
1393 * again we have to fail here.
1395 if (rel1_is_simple && !child_rel1->consider_partitionwise_join)
1397 Assert(child_rel1->reloptkind == RELOPT_OTHER_MEMBER_REL);
1398 Assert(IS_DUMMY_REL(child_rel1));
1399 joinrel->nparts = 0;
1402 if (rel2_is_simple && !child_rel2->consider_partitionwise_join)
1404 Assert(child_rel2->reloptkind == RELOPT_OTHER_MEMBER_REL);
1405 Assert(IS_DUMMY_REL(child_rel2));
1406 joinrel->nparts = 0;
1410 /* We should never try to join two overlapping sets of rels. */
1411 Assert(!bms_overlap(child_rel1->relids, child_rel2->relids));
1412 child_joinrelids = bms_union(child_rel1->relids, child_rel2->relids);
1413 appinfos = find_appinfos_by_relids(root, child_joinrelids, &nappinfos);
1416 * Construct SpecialJoinInfo from parent join relations's
1419 child_sjinfo = build_child_join_sjinfo(root, parent_sjinfo,
1421 child_rel2->relids);
1424 * Construct restrictions applicable to the child join from those
1425 * applicable to the parent join.
1427 child_restrictlist =
1428 (List *) adjust_appendrel_attrs(root,
1429 (Node *) parent_restrictlist,
1430 nappinfos, appinfos);
1433 child_joinrel = joinrel->part_rels[cnt_parts];
1436 child_joinrel = build_child_join_rel(root, child_rel1, child_rel2,
1437 joinrel, child_restrictlist,
1439 child_sjinfo->jointype);
1440 joinrel->part_rels[cnt_parts] = child_joinrel;
1441 joinrel->all_partrels = bms_add_members(joinrel->all_partrels,
1442 child_joinrel->relids);
1445 Assert(bms_equal(child_joinrel->relids, child_joinrelids));
1447 populate_joinrel_with_paths(root, child_rel1, child_rel2,
1448 child_joinrel, child_sjinfo,
1449 child_restrictlist);