Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * joinrels.c
4 : * Routines to determine which relations should be joined
5 : *
6 : * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7 : * Portions Copyright (c) 1994, Regents of the University of California
8 : *
9 : *
10 : * IDENTIFICATION
11 : * src/backend/optimizer/path/joinrels.c
12 : *
13 : *-------------------------------------------------------------------------
14 : */
15 : #include "postgres.h"
16 :
17 : #include "miscadmin.h"
18 : #include "optimizer/appendinfo.h"
19 : #include "optimizer/joininfo.h"
20 : #include "optimizer/pathnode.h"
21 : #include "optimizer/paths.h"
22 : #include "partitioning/partbounds.h"
23 : #include "utils/memutils.h"
24 :
25 :
26 : static void make_rels_by_clause_joins(PlannerInfo *root,
27 : RelOptInfo *old_rel,
28 : List *other_rels,
29 : int first_rel_idx);
30 : static void make_rels_by_clauseless_joins(PlannerInfo *root,
31 : RelOptInfo *old_rel,
32 : List *other_rels);
33 : static bool has_join_restriction(PlannerInfo *root, RelOptInfo *rel);
34 : static bool has_legal_joinclause(PlannerInfo *root, RelOptInfo *rel);
35 : static bool restriction_is_constant_false(List *restrictlist,
36 : RelOptInfo *joinrel,
37 : bool only_pushed_down);
38 : static void populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1,
39 : RelOptInfo *rel2, RelOptInfo *joinrel,
40 : SpecialJoinInfo *sjinfo, List *restrictlist);
41 : static void try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1,
42 : RelOptInfo *rel2, RelOptInfo *joinrel,
43 : SpecialJoinInfo *parent_sjinfo,
44 : List *parent_restrictlist);
45 : static SpecialJoinInfo *build_child_join_sjinfo(PlannerInfo *root,
46 : SpecialJoinInfo *parent_sjinfo,
47 : Relids left_relids, Relids right_relids);
48 : static void free_child_join_sjinfo(SpecialJoinInfo *sjinfo);
49 : static void compute_partition_bounds(PlannerInfo *root, RelOptInfo *rel1,
50 : RelOptInfo *rel2, RelOptInfo *joinrel,
51 : SpecialJoinInfo *parent_sjinfo,
52 : List **parts1, List **parts2);
53 : static void get_matching_part_pairs(PlannerInfo *root, RelOptInfo *joinrel,
54 : RelOptInfo *rel1, RelOptInfo *rel2,
55 : List **parts1, List **parts2);
56 :
57 :
58 : /*
59 : * join_search_one_level
60 : * Consider ways to produce join relations containing exactly 'level'
61 : * jointree items. (This is one step of the dynamic-programming method
62 : * embodied in standard_join_search.) Join rel nodes for each feasible
63 : * combination of lower-level rels are created and returned in a list.
64 : * Implementation paths are created for each such joinrel, too.
65 : *
66 : * level: level of rels we want to make this time
67 : * root->join_rel_level[j], 1 <= j < level, is a list of rels containing j items
68 : *
69 : * The result is returned in root->join_rel_level[level].
70 : */
71 : void
72 117726 : join_search_one_level(PlannerInfo *root, int level)
73 : {
74 117726 : List **joinrels = root->join_rel_level;
75 : ListCell *r;
76 : int k;
77 :
78 : Assert(joinrels[level] == NIL);
79 :
80 : /* Set join_cur_level so that new joinrels are added to proper list */
81 117726 : root->join_cur_level = level;
82 :
83 : /*
84 : * First, consider left-sided and right-sided plans, in which rels of
85 : * exactly level-1 member relations are joined against initial relations.
86 : * We prefer to join using join clauses, but if we find a rel of level-1
87 : * members that has no join clauses, we will generate Cartesian-product
88 : * joins against all initial rels not already contained in it.
89 : */
90 412700 : foreach(r, joinrels[level - 1])
91 : {
92 294974 : RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
93 :
94 325272 : if (old_rel->joininfo != NIL || old_rel->has_eclass_joins ||
95 30298 : has_join_restriction(root, old_rel))
96 285146 : {
97 : int first_rel;
98 :
99 : /*
100 : * There are join clauses or join order restrictions relevant to
101 : * this rel, so consider joins between this rel and (only) those
102 : * initial rels it is linked to by a clause or restriction.
103 : *
104 : * At level 2 this condition is symmetric, so there is no need to
105 : * look at initial rels before this one in the list; we already
106 : * considered such joins when we were at the earlier rel. (The
107 : * mirror-image joins are handled automatically by make_join_rel.)
108 : * In later passes (level > 2), we join rels of the previous level
109 : * to each initial rel they don't already include but have a join
110 : * clause or restriction with.
111 : */
112 285146 : if (level == 2) /* consider remaining initial rels */
113 195274 : first_rel = foreach_current_index(r) + 1;
114 : else
115 89872 : first_rel = 0;
116 :
117 285146 : make_rels_by_clause_joins(root, old_rel, joinrels[1], first_rel);
118 : }
119 : else
120 : {
121 : /*
122 : * Oops, we have a relation that is not joined to any other
123 : * relation, either directly or by join-order restrictions.
124 : * Cartesian product time.
125 : *
126 : * We consider a cartesian product with each not-already-included
127 : * initial rel, whether it has other join clauses or not. At
128 : * level 2, if there are two or more clauseless initial rels, we
129 : * will redundantly consider joining them in both directions; but
130 : * such cases aren't common enough to justify adding complexity to
131 : * avoid the duplicated effort.
132 : */
133 9828 : make_rels_by_clauseless_joins(root,
134 : old_rel,
135 9828 : joinrels[1]);
136 : }
137 : }
138 :
139 : /*
140 : * Now, consider "bushy plans" in which relations of k initial rels are
141 : * joined to relations of level-k initial rels, for 2 <= k <= level-2.
142 : *
143 : * We only consider bushy-plan joins for pairs of rels where there is a
144 : * suitable join clause (or join order restriction), in order to avoid
145 : * unreasonable growth of planning time.
146 : */
147 117726 : for (k = 2;; k++)
148 10736 : {
149 128462 : int other_level = level - k;
150 :
151 : /*
152 : * Since make_join_rel(x, y) handles both x,y and y,x cases, we only
153 : * need to go as far as the halfway point.
154 : */
155 128462 : if (k > other_level)
156 117726 : break;
157 :
158 54982 : foreach(r, joinrels[k])
159 : {
160 44246 : RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
161 : int first_rel;
162 : ListCell *r2;
163 :
164 : /*
165 : * We can ignore relations without join clauses here, unless they
166 : * participate in join-order restrictions --- then we might have
167 : * to force a bushy join plan.
168 : */
169 44246 : if (old_rel->joininfo == NIL && !old_rel->has_eclass_joins &&
170 312 : !has_join_restriction(root, old_rel))
171 216 : continue;
172 :
173 44030 : if (k == other_level) /* only consider remaining rels */
174 31112 : first_rel = foreach_current_index(r) + 1;
175 : else
176 12918 : first_rel = 0;
177 :
178 189304 : for_each_from(r2, joinrels[other_level], first_rel)
179 : {
180 145274 : RelOptInfo *new_rel = (RelOptInfo *) lfirst(r2);
181 :
182 145274 : if (!bms_overlap(old_rel->relids, new_rel->relids))
183 : {
184 : /*
185 : * OK, we can build a rel of the right level from this
186 : * pair of rels. Do so if there is at least one relevant
187 : * join clause or join order restriction.
188 : */
189 18070 : if (have_relevant_joinclause(root, old_rel, new_rel) ||
190 1042 : have_join_order_restriction(root, old_rel, new_rel))
191 : {
192 16040 : (void) make_join_rel(root, old_rel, new_rel);
193 : }
194 : }
195 : }
196 : }
197 : }
198 :
199 : /*----------
200 : * Last-ditch effort: if we failed to find any usable joins so far, force
201 : * a set of cartesian-product joins to be generated. This handles the
202 : * special case where all the available rels have join clauses but we
203 : * cannot use any of those clauses yet. This can only happen when we are
204 : * considering a join sub-problem (a sub-joinlist) and all the rels in the
205 : * sub-problem have only join clauses with rels outside the sub-problem.
206 : * An example is
207 : *
208 : * SELECT ... FROM a INNER JOIN b ON TRUE, c, d, ...
209 : * WHERE a.w = c.x and b.y = d.z;
210 : *
211 : * If the "a INNER JOIN b" sub-problem does not get flattened into the
212 : * upper level, we must be willing to make a cartesian join of a and b;
213 : * but the code above will not have done so, because it thought that both
214 : * a and b have joinclauses. We consider only left-sided and right-sided
215 : * cartesian joins in this case (no bushy).
216 : *----------
217 : */
218 117726 : if (joinrels[level] == NIL)
219 : {
220 : /*
221 : * This loop is just like the first one, except we always call
222 : * make_rels_by_clauseless_joins().
223 : */
224 54 : foreach(r, joinrels[level - 1])
225 : {
226 36 : RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
227 :
228 36 : make_rels_by_clauseless_joins(root,
229 : old_rel,
230 36 : joinrels[1]);
231 : }
232 :
233 : /*----------
234 : * When special joins are involved, there may be no legal way
235 : * to make an N-way join for some values of N. For example consider
236 : *
237 : * SELECT ... FROM t1 WHERE
238 : * x IN (SELECT ... FROM t2,t3 WHERE ...) AND
239 : * y IN (SELECT ... FROM t4,t5 WHERE ...)
240 : *
241 : * We will flatten this query to a 5-way join problem, but there are
242 : * no 4-way joins that join_is_legal() will consider legal. We have
243 : * to accept failure at level 4 and go on to discover a workable
244 : * bushy plan at level 5.
245 : *
246 : * However, if there are no special joins and no lateral references
247 : * then join_is_legal() should never fail, and so the following sanity
248 : * check is useful.
249 : *----------
250 : */
251 18 : if (joinrels[level] == NIL &&
252 6 : root->join_info_list == NIL &&
253 0 : !root->hasLateralRTEs)
254 0 : elog(ERROR, "failed to build any %d-way joins", level);
255 : }
256 117726 : }
257 :
258 : /*
259 : * make_rels_by_clause_joins
260 : * Build joins between the given relation 'old_rel' and other relations
261 : * that participate in join clauses that 'old_rel' also participates in
262 : * (or participate in join-order restrictions with it).
263 : * The join rels are returned in root->join_rel_level[join_cur_level].
264 : *
265 : * Note: at levels above 2 we will generate the same joined relation in
266 : * multiple ways --- for example (a join b) join c is the same RelOptInfo as
267 : * (b join c) join a, though the second case will add a different set of Paths
268 : * to it. This is the reason for using the join_rel_level mechanism, which
269 : * automatically ensures that each new joinrel is only added to the list once.
270 : *
271 : * 'old_rel' is the relation entry for the relation to be joined
272 : * 'other_rels': a list containing the other rels to be considered for joining
273 : * 'first_rel_idx': the first rel to be considered in 'other_rels'
274 : *
275 : * Currently, this is only used with initial rels in other_rels, but it
276 : * will work for joining to joinrels too.
277 : */
278 : static void
279 285146 : make_rels_by_clause_joins(PlannerInfo *root,
280 : RelOptInfo *old_rel,
281 : List *other_rels,
282 : int first_rel_idx)
283 : {
284 : ListCell *l;
285 :
286 819620 : for_each_from(l, other_rels, first_rel_idx)
287 : {
288 534474 : RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
289 :
290 840900 : if (!bms_overlap(old_rel->relids, other_rel->relids) &&
291 371240 : (have_relevant_joinclause(root, old_rel, other_rel) ||
292 64814 : have_join_order_restriction(root, old_rel, other_rel)))
293 : {
294 253342 : (void) make_join_rel(root, old_rel, other_rel);
295 : }
296 : }
297 285146 : }
298 :
299 : /*
300 : * make_rels_by_clauseless_joins
301 : * Given a relation 'old_rel' and a list of other relations
302 : * 'other_rels', create a join relation between 'old_rel' and each
303 : * member of 'other_rels' that isn't already included in 'old_rel'.
304 : * The join rels are returned in root->join_rel_level[join_cur_level].
305 : *
306 : * 'old_rel' is the relation entry for the relation to be joined
307 : * 'other_rels': a list containing the other rels to be considered for joining
308 : *
309 : * Currently, this is only used with initial rels in other_rels, but it would
310 : * work for joining to joinrels too.
311 : */
312 : static void
313 9864 : make_rels_by_clauseless_joins(PlannerInfo *root,
314 : RelOptInfo *old_rel,
315 : List *other_rels)
316 : {
317 : ListCell *l;
318 :
319 31308 : foreach(l, other_rels)
320 : {
321 21444 : RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
322 :
323 21444 : if (!bms_overlap(other_rel->relids, old_rel->relids))
324 : {
325 10580 : (void) make_join_rel(root, old_rel, other_rel);
326 : }
327 : }
328 9864 : }
329 :
330 :
331 : /*
332 : * join_is_legal
333 : * Determine whether a proposed join is legal given the query's
334 : * join order constraints; and if it is, determine the join type.
335 : *
336 : * Caller must supply not only the two rels, but the union of their relids.
337 : * (We could simplify the API by computing joinrelids locally, but this
338 : * would be redundant work in the normal path through make_join_rel.
339 : * Note that this value does NOT include the RT index of any outer join that
340 : * might need to be performed here, so it's not the canonical identifier
341 : * of the join relation.)
342 : *
343 : * On success, *sjinfo_p is set to NULL if this is to be a plain inner join,
344 : * else it's set to point to the associated SpecialJoinInfo node. Also,
345 : * *reversed_p is set true if the given relations need to be swapped to
346 : * match the SpecialJoinInfo node.
347 : */
348 : static bool
349 285032 : join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
350 : Relids joinrelids,
351 : SpecialJoinInfo **sjinfo_p, bool *reversed_p)
352 : {
353 : SpecialJoinInfo *match_sjinfo;
354 : bool reversed;
355 : bool unique_ified;
356 : bool must_be_leftjoin;
357 : ListCell *l;
358 :
359 : /*
360 : * Ensure output params are set on failure return. This is just to
361 : * suppress uninitialized-variable warnings from overly anal compilers.
362 : */
363 285032 : *sjinfo_p = NULL;
364 285032 : *reversed_p = false;
365 :
366 : /*
367 : * If we have any special joins, the proposed join might be illegal; and
368 : * in any case we have to determine its join type. Scan the join info
369 : * list for matches and conflicts.
370 : */
371 285032 : match_sjinfo = NULL;
372 285032 : reversed = false;
373 285032 : unique_ified = false;
374 285032 : must_be_leftjoin = false;
375 :
376 577484 : foreach(l, root->join_info_list)
377 : {
378 300932 : SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
379 :
380 : /*
381 : * This special join is not relevant unless its RHS overlaps the
382 : * proposed join. (Check this first as a fast path for dismissing
383 : * most irrelevant SJs quickly.)
384 : */
385 300932 : if (!bms_overlap(sjinfo->min_righthand, joinrelids))
386 99724 : continue;
387 :
388 : /*
389 : * Also, not relevant if proposed join is fully contained within RHS
390 : * (ie, we're still building up the RHS).
391 : */
392 201208 : if (bms_is_subset(joinrelids, sjinfo->min_righthand))
393 5010 : continue;
394 :
395 : /*
396 : * Also, not relevant if SJ is already done within either input.
397 : */
398 365968 : if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
399 169770 : bms_is_subset(sjinfo->min_righthand, rel1->relids))
400 81586 : continue;
401 131342 : if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
402 16730 : bms_is_subset(sjinfo->min_righthand, rel2->relids))
403 8302 : continue;
404 :
405 : /*
406 : * If it's a semijoin and we already joined the RHS to any other rels
407 : * within either input, then we must have unique-ified the RHS at that
408 : * point (see below). Therefore the semijoin is no longer relevant in
409 : * this join path.
410 : */
411 106310 : if (sjinfo->jointype == JOIN_SEMI)
412 : {
413 7000 : if (bms_is_subset(sjinfo->syn_righthand, rel1->relids) &&
414 1216 : !bms_equal(sjinfo->syn_righthand, rel1->relids))
415 426 : continue;
416 6574 : if (bms_is_subset(sjinfo->syn_righthand, rel2->relids) &&
417 3882 : !bms_equal(sjinfo->syn_righthand, rel2->relids))
418 94 : continue;
419 : }
420 :
421 : /*
422 : * If one input contains min_lefthand and the other contains
423 : * min_righthand, then we can perform the SJ at this join.
424 : *
425 : * Reject if we get matches to more than one SJ; that implies we're
426 : * considering something that's not really valid.
427 : */
428 193894 : if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
429 88104 : bms_is_subset(sjinfo->min_righthand, rel2->relids))
430 : {
431 82036 : if (match_sjinfo)
432 8480 : return false; /* invalid join path */
433 82036 : match_sjinfo = sjinfo;
434 82036 : reversed = false;
435 : }
436 31958 : else if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
437 8204 : bms_is_subset(sjinfo->min_righthand, rel1->relids))
438 : {
439 7022 : if (match_sjinfo)
440 0 : return false; /* invalid join path */
441 7022 : match_sjinfo = sjinfo;
442 7022 : reversed = true;
443 : }
444 19380 : else if (sjinfo->jointype == JOIN_SEMI &&
445 3176 : bms_equal(sjinfo->syn_righthand, rel2->relids) &&
446 528 : create_unique_path(root, rel2, rel2->cheapest_total_path,
447 : sjinfo) != NULL)
448 : {
449 : /*----------
450 : * For a semijoin, we can join the RHS to anything else by
451 : * unique-ifying the RHS (if the RHS can be unique-ified).
452 : * We will only get here if we have the full RHS but less
453 : * than min_lefthand on the LHS.
454 : *
455 : * The reason to consider such a join path is exemplified by
456 : * SELECT ... FROM a,b WHERE (a.x,b.y) IN (SELECT c1,c2 FROM c)
457 : * If we insist on doing this as a semijoin we will first have
458 : * to form the cartesian product of A*B. But if we unique-ify
459 : * C then the semijoin becomes a plain innerjoin and we can join
460 : * in any order, eg C to A and then to B. When C is much smaller
461 : * than A and B this can be a huge win. So we allow C to be
462 : * joined to just A or just B here, and then make_join_rel has
463 : * to handle the case properly.
464 : *
465 : * Note that actually we'll allow unique-ified C to be joined to
466 : * some other relation D here, too. That is legal, if usually not
467 : * very sane, and this routine is only concerned with legality not
468 : * with whether the join is good strategy.
469 : *----------
470 : */
471 522 : if (match_sjinfo)
472 78 : return false; /* invalid join path */
473 444 : match_sjinfo = sjinfo;
474 444 : reversed = false;
475 444 : unique_ified = true;
476 : }
477 18336 : else if (sjinfo->jointype == JOIN_SEMI &&
478 2344 : bms_equal(sjinfo->syn_righthand, rel1->relids) &&
479 218 : create_unique_path(root, rel1, rel1->cheapest_total_path,
480 : sjinfo) != NULL)
481 : {
482 : /* Reversed semijoin case */
483 218 : if (match_sjinfo)
484 78 : return false; /* invalid join path */
485 140 : match_sjinfo = sjinfo;
486 140 : reversed = true;
487 140 : unique_ified = true;
488 : }
489 : else
490 : {
491 : /*
492 : * Otherwise, the proposed join overlaps the RHS but isn't a valid
493 : * implementation of this SJ. But don't panic quite yet: the RHS
494 : * violation might have occurred previously, in one or both input
495 : * relations, in which case we must have previously decided that
496 : * it was OK to commute some other SJ with this one. If we need
497 : * to perform this join to finish building up the RHS, rejecting
498 : * it could lead to not finding any plan at all. (This can occur
499 : * because of the heuristics elsewhere in this file that postpone
500 : * clauseless joins: we might not consider doing a clauseless join
501 : * within the RHS until after we've performed other, validly
502 : * commutable SJs with one or both sides of the clauseless join.)
503 : * This consideration boils down to the rule that if both inputs
504 : * overlap the RHS, we can allow the join --- they are either
505 : * fully within the RHS, or represent previously-allowed joins to
506 : * rels outside it.
507 : */
508 23472 : if (bms_overlap(rel1->relids, sjinfo->min_righthand) &&
509 7480 : bms_overlap(rel2->relids, sjinfo->min_righthand))
510 180 : continue; /* assume valid previous violation of RHS */
511 :
512 : /*
513 : * The proposed join could still be legal, but only if we're
514 : * allowed to associate it into the RHS of this SJ. That means
515 : * this SJ must be a LEFT join (not SEMI or ANTI, and certainly
516 : * not FULL) and the proposed join must not overlap the LHS.
517 : */
518 29584 : if (sjinfo->jointype != JOIN_LEFT ||
519 13772 : bms_overlap(joinrelids, sjinfo->min_lefthand))
520 8324 : return false; /* invalid join path */
521 :
522 : /*
523 : * To be valid, the proposed join must be a LEFT join; otherwise
524 : * it can't associate into this SJ's RHS. But we may not yet have
525 : * found the SpecialJoinInfo matching the proposed join, so we
526 : * can't test that yet. Remember the requirement for later.
527 : */
528 7488 : must_be_leftjoin = true;
529 : }
530 : }
531 :
532 : /*
533 : * Fail if violated any SJ's RHS and didn't match to a LEFT SJ: the
534 : * proposed join can't associate into an SJ's RHS.
535 : *
536 : * Also, fail if the proposed join's predicate isn't strict; we're
537 : * essentially checking to see if we can apply outer-join identity 3, and
538 : * that's a requirement. (This check may be redundant with checks in
539 : * make_outerjoininfo, but I'm not quite sure, and it's cheap to test.)
540 : */
541 276552 : if (must_be_leftjoin &&
542 4634 : (match_sjinfo == NULL ||
543 4634 : match_sjinfo->jointype != JOIN_LEFT ||
544 4634 : !match_sjinfo->lhs_strict))
545 1460 : return false; /* invalid join path */
546 :
547 : /*
548 : * We also have to check for constraints imposed by LATERAL references.
549 : */
550 275092 : if (root->hasLateralRTEs)
551 : {
552 : bool lateral_fwd;
553 : bool lateral_rev;
554 : Relids join_lateral_rels;
555 :
556 : /*
557 : * The proposed rels could each contain lateral references to the
558 : * other, in which case the join is impossible. If there are lateral
559 : * references in just one direction, then the join has to be done with
560 : * a nestloop with the lateral referencer on the inside. If the join
561 : * matches an SJ that cannot be implemented by such a nestloop, the
562 : * join is impossible.
563 : *
564 : * Also, if the lateral reference is only indirect, we should reject
565 : * the join; whatever rel(s) the reference chain goes through must be
566 : * joined to first.
567 : *
568 : * Another case that might keep us from building a valid plan is the
569 : * implementation restriction described by have_dangerous_phv().
570 : */
571 16696 : lateral_fwd = bms_overlap(rel1->relids, rel2->lateral_relids);
572 16696 : lateral_rev = bms_overlap(rel2->relids, rel1->lateral_relids);
573 16696 : if (lateral_fwd && lateral_rev)
574 18 : return false; /* have lateral refs in both directions */
575 16678 : if (lateral_fwd)
576 : {
577 : /* has to be implemented as nestloop with rel1 on left */
578 10622 : if (match_sjinfo &&
579 354 : (reversed ||
580 342 : unique_ified ||
581 342 : match_sjinfo->jointype == JOIN_FULL))
582 12 : return false; /* not implementable as nestloop */
583 : /* check there is a direct reference from rel2 to rel1 */
584 10610 : if (!bms_overlap(rel1->relids, rel2->direct_lateral_relids))
585 42 : return false; /* only indirect refs, so reject */
586 : /* check we won't have a dangerous PHV */
587 10568 : if (have_dangerous_phv(root, rel1->relids, rel2->lateral_relids))
588 72 : return false; /* might be unable to handle required PHV */
589 : }
590 6056 : else if (lateral_rev)
591 : {
592 : /* has to be implemented as nestloop with rel2 on left */
593 1130 : if (match_sjinfo &&
594 72 : (!reversed ||
595 72 : unique_ified ||
596 72 : match_sjinfo->jointype == JOIN_FULL))
597 0 : return false; /* not implementable as nestloop */
598 : /* check there is a direct reference from rel1 to rel2 */
599 1130 : if (!bms_overlap(rel2->relids, rel1->direct_lateral_relids))
600 0 : return false; /* only indirect refs, so reject */
601 : /* check we won't have a dangerous PHV */
602 1130 : if (have_dangerous_phv(root, rel2->relids, rel1->lateral_relids))
603 84 : return false; /* might be unable to handle required PHV */
604 : }
605 :
606 : /*
607 : * LATERAL references could also cause problems later on if we accept
608 : * this join: if the join's minimum parameterization includes any rels
609 : * that would have to be on the inside of an outer join with this join
610 : * rel, then it's never going to be possible to build the complete
611 : * query using this join. We should reject this join not only because
612 : * it'll save work, but because if we don't, the clauseless-join
613 : * heuristics might think that legality of this join means that some
614 : * other join rel need not be formed, and that could lead to failure
615 : * to find any plan at all. We have to consider not only rels that
616 : * are directly on the inner side of an OJ with the joinrel, but also
617 : * ones that are indirectly so, so search to find all such rels.
618 : */
619 16468 : join_lateral_rels = min_join_parameterization(root, joinrelids,
620 : rel1, rel2);
621 16468 : if (join_lateral_rels)
622 : {
623 1634 : Relids join_plus_rhs = bms_copy(joinrelids);
624 : bool more;
625 :
626 : do
627 : {
628 2018 : more = false;
629 3872 : foreach(l, root->join_info_list)
630 : {
631 1854 : SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
632 :
633 : /* ignore full joins --- their ordering is predetermined */
634 1854 : if (sjinfo->jointype == JOIN_FULL)
635 18 : continue;
636 :
637 1836 : if (bms_overlap(sjinfo->min_lefthand, join_plus_rhs) &&
638 1566 : !bms_is_subset(sjinfo->min_righthand, join_plus_rhs))
639 : {
640 534 : join_plus_rhs = bms_add_members(join_plus_rhs,
641 534 : sjinfo->min_righthand);
642 534 : more = true;
643 : }
644 : }
645 2018 : } while (more);
646 1634 : if (bms_overlap(join_plus_rhs, join_lateral_rels))
647 300 : return false; /* will not be able to join to some RHS rel */
648 : }
649 : }
650 :
651 : /* Otherwise, it's a valid join */
652 274564 : *sjinfo_p = match_sjinfo;
653 274564 : *reversed_p = reversed;
654 274564 : return true;
655 : }
656 :
657 : /*
658 : * init_dummy_sjinfo
659 : * Populate the given SpecialJoinInfo for a plain inner join between the
660 : * left and right relations specified by left_relids and right_relids
661 : * respectively.
662 : *
663 : * Normally, an inner join does not have a SpecialJoinInfo node associated with
664 : * it. But some functions involved in join planning require one containing at
665 : * least the information of which relations are being joined. So we initialize
666 : * that information here.
667 : */
668 : void
669 765068 : init_dummy_sjinfo(SpecialJoinInfo *sjinfo, Relids left_relids,
670 : Relids right_relids)
671 : {
672 765068 : sjinfo->type = T_SpecialJoinInfo;
673 765068 : sjinfo->min_lefthand = left_relids;
674 765068 : sjinfo->min_righthand = right_relids;
675 765068 : sjinfo->syn_lefthand = left_relids;
676 765068 : sjinfo->syn_righthand = right_relids;
677 765068 : sjinfo->jointype = JOIN_INNER;
678 765068 : sjinfo->ojrelid = 0;
679 765068 : sjinfo->commute_above_l = NULL;
680 765068 : sjinfo->commute_above_r = NULL;
681 765068 : sjinfo->commute_below_l = NULL;
682 765068 : sjinfo->commute_below_r = NULL;
683 : /* we don't bother trying to make the remaining fields valid */
684 765068 : sjinfo->lhs_strict = false;
685 765068 : sjinfo->semi_can_btree = false;
686 765068 : sjinfo->semi_can_hash = false;
687 765068 : sjinfo->semi_operators = NIL;
688 765068 : sjinfo->semi_rhs_exprs = NIL;
689 765068 : }
690 :
691 : /*
692 : * make_join_rel
693 : * Find or create a join RelOptInfo that represents the join of
694 : * the two given rels, and add to it path information for paths
695 : * created with the two rels as outer and inner rel.
696 : * (The join rel may already contain paths generated from other
697 : * pairs of rels that add up to the same set of base rels.)
698 : *
699 : * NB: will return NULL if attempted join is not valid. This can happen
700 : * when working with outer joins, or with IN or EXISTS clauses that have been
701 : * turned into joins.
702 : */
703 : RelOptInfo *
704 284714 : make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
705 : {
706 : Relids joinrelids;
707 : SpecialJoinInfo *sjinfo;
708 : bool reversed;
709 284714 : List *pushed_down_joins = NIL;
710 : SpecialJoinInfo sjinfo_data;
711 : RelOptInfo *joinrel;
712 : List *restrictlist;
713 :
714 : /* We should never try to join two overlapping sets of rels. */
715 : Assert(!bms_overlap(rel1->relids, rel2->relids));
716 :
717 : /* Construct Relids set that identifies the joinrel (without OJ as yet). */
718 284714 : joinrelids = bms_union(rel1->relids, rel2->relids);
719 :
720 : /* Check validity and determine join type. */
721 284714 : if (!join_is_legal(root, rel1, rel2, joinrelids,
722 : &sjinfo, &reversed))
723 : {
724 : /* invalid join path */
725 10360 : bms_free(joinrelids);
726 10360 : return NULL;
727 : }
728 :
729 : /*
730 : * Add outer join relid(s) to form the canonical relids. Any added outer
731 : * joins besides sjinfo itself are appended to pushed_down_joins.
732 : */
733 274354 : joinrelids = add_outer_joins_to_relids(root, joinrelids, sjinfo,
734 : &pushed_down_joins);
735 :
736 : /* Swap rels if needed to match the join info. */
737 274354 : if (reversed)
738 : {
739 7018 : RelOptInfo *trel = rel1;
740 :
741 7018 : rel1 = rel2;
742 7018 : rel2 = trel;
743 : }
744 :
745 : /*
746 : * If it's a plain inner join, then we won't have found anything in
747 : * join_info_list. Make up a SpecialJoinInfo so that selectivity
748 : * estimation functions will know what's being joined.
749 : */
750 274354 : if (sjinfo == NULL)
751 : {
752 185226 : sjinfo = &sjinfo_data;
753 185226 : init_dummy_sjinfo(sjinfo, rel1->relids, rel2->relids);
754 : }
755 :
756 : /*
757 : * Find or build the join RelOptInfo, and compute the restrictlist that
758 : * goes with this particular joining.
759 : */
760 274354 : joinrel = build_join_rel(root, joinrelids, rel1, rel2,
761 : sjinfo, pushed_down_joins,
762 : &restrictlist);
763 :
764 : /*
765 : * If we've already proven this join is empty, we needn't consider any
766 : * more paths for it.
767 : */
768 274354 : if (is_dummy_rel(joinrel))
769 : {
770 456 : bms_free(joinrelids);
771 456 : return joinrel;
772 : }
773 :
774 : /* Add paths to the join relation. */
775 273898 : populate_joinrel_with_paths(root, rel1, rel2, joinrel, sjinfo,
776 : restrictlist);
777 :
778 273898 : bms_free(joinrelids);
779 :
780 273898 : return joinrel;
781 : }
782 :
783 : /*
784 : * add_outer_joins_to_relids
785 : * Add relids to input_relids to represent any outer joins that will be
786 : * calculated at this join.
787 : *
788 : * input_relids is the union of the relid sets of the two input relations.
789 : * Note that we modify this in-place and return it; caller must bms_copy()
790 : * it first, if a separate value is desired.
791 : *
792 : * sjinfo represents the join being performed.
793 : *
794 : * If the current join completes the calculation of any outer joins that
795 : * have been pushed down per outer-join identity 3, those relids will be
796 : * added to the result along with sjinfo's own relid. If pushed_down_joins
797 : * is not NULL, then also the SpecialJoinInfos for such added outer joins will
798 : * be appended to *pushed_down_joins (so caller must initialize it to NIL).
799 : */
800 : Relids
801 281234 : add_outer_joins_to_relids(PlannerInfo *root, Relids input_relids,
802 : SpecialJoinInfo *sjinfo,
803 : List **pushed_down_joins)
804 : {
805 : /* Nothing to do if this isn't an outer join with an assigned relid. */
806 281234 : if (sjinfo == NULL || sjinfo->ojrelid == 0)
807 199472 : return input_relids;
808 :
809 : /*
810 : * If it's not a left join, we have no rules that would permit executing
811 : * it in non-syntactic order, so just form the syntactic relid set. (This
812 : * is just a quick-exit test; we'd come to the same conclusion anyway,
813 : * since its commute_below_l and commute_above_l sets must be empty.)
814 : */
815 81762 : if (sjinfo->jointype != JOIN_LEFT)
816 2064 : return bms_add_member(input_relids, sjinfo->ojrelid);
817 :
818 : /*
819 : * We cannot add the OJ relid if this join has been pushed into the RHS of
820 : * a syntactically-lower left join per OJ identity 3. (If it has, then we
821 : * cannot claim that its outputs represent the final state of its RHS.)
822 : * There will not be any other OJs that can be added either, so we're
823 : * done.
824 : */
825 79698 : if (!bms_is_subset(sjinfo->commute_below_l, input_relids))
826 4358 : return input_relids;
827 :
828 : /* OK to add OJ's own relid */
829 75340 : input_relids = bms_add_member(input_relids, sjinfo->ojrelid);
830 :
831 : /*
832 : * Contrariwise, if we are now forming the final result of such a commuted
833 : * pair of OJs, it's time to add the relid(s) of the pushed-down join(s).
834 : * We can skip this if this join was never a candidate to be pushed up.
835 : */
836 75340 : if (sjinfo->commute_above_l)
837 : {
838 14016 : Relids commute_above_rels = bms_copy(sjinfo->commute_above_l);
839 : ListCell *lc;
840 :
841 : /*
842 : * The current join could complete the nulling of more than one
843 : * pushed-down join, so we have to examine all the SpecialJoinInfos.
844 : * Because join_info_list was built in bottom-up order, it's
845 : * sufficient to traverse it once: an ojrelid we add in one loop
846 : * iteration would not have affected decisions of earlier iterations.
847 : */
848 44628 : foreach(lc, root->join_info_list)
849 : {
850 30612 : SpecialJoinInfo *othersj = (SpecialJoinInfo *) lfirst(lc);
851 :
852 30612 : if (othersj == sjinfo ||
853 16596 : othersj->ojrelid == 0 || othersj->jointype != JOIN_LEFT)
854 14028 : continue; /* definitely not interesting */
855 :
856 16584 : if (!bms_is_member(othersj->ojrelid, commute_above_rels))
857 2460 : continue;
858 :
859 : /* Add it if not already present but conditions now satisfied */
860 28248 : if (!bms_is_member(othersj->ojrelid, input_relids) &&
861 28224 : bms_is_subset(othersj->min_lefthand, input_relids) &&
862 21222 : bms_is_subset(othersj->min_righthand, input_relids) &&
863 7122 : bms_is_subset(othersj->commute_below_l, input_relids))
864 : {
865 7086 : input_relids = bms_add_member(input_relids, othersj->ojrelid);
866 : /* report such pushed down outer joins, if asked */
867 7086 : if (pushed_down_joins != NULL)
868 7086 : *pushed_down_joins = lappend(*pushed_down_joins, othersj);
869 :
870 : /*
871 : * We must also check any joins that othersj potentially
872 : * commutes with. They likewise must appear later in
873 : * join_info_list than othersj itself, so we can visit them
874 : * later in this loop.
875 : */
876 7086 : commute_above_rels = bms_add_members(commute_above_rels,
877 7086 : othersj->commute_above_l);
878 : }
879 : }
880 : }
881 :
882 75340 : return input_relids;
883 : }
884 :
885 : /*
886 : * populate_joinrel_with_paths
887 : * Add paths to the given joinrel for given pair of joining relations. The
888 : * SpecialJoinInfo provides details about the join and the restrictlist
889 : * contains the join clauses and the other clauses applicable for given pair
890 : * of the joining relations.
891 : */
892 : static void
893 279144 : populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1,
894 : RelOptInfo *rel2, RelOptInfo *joinrel,
895 : SpecialJoinInfo *sjinfo, List *restrictlist)
896 : {
897 : /*
898 : * Consider paths using each rel as both outer and inner. Depending on
899 : * the join type, a provably empty outer or inner rel might mean the join
900 : * is provably empty too; in which case throw away any previously computed
901 : * paths and mark the join as dummy. (We do it this way since it's
902 : * conceivable that dummy-ness of a multi-element join might only be
903 : * noticeable for certain construction paths.)
904 : *
905 : * Also, a provably constant-false join restriction typically means that
906 : * we can skip evaluating one or both sides of the join. We do this by
907 : * marking the appropriate rel as dummy. For outer joins, a
908 : * constant-false restriction that is pushed down still means the whole
909 : * join is dummy, while a non-pushed-down one means that no inner rows
910 : * will join so we can treat the inner rel as dummy.
911 : *
912 : * We need only consider the jointypes that appear in join_info_list, plus
913 : * JOIN_INNER.
914 : */
915 279144 : switch (sjinfo->jointype)
916 : {
917 186992 : case JOIN_INNER:
918 373966 : if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
919 186974 : restriction_is_constant_false(restrictlist, joinrel, false))
920 : {
921 186 : mark_dummy_rel(joinrel);
922 186 : break;
923 : }
924 186806 : add_paths_to_joinrel(root, joinrel, rel1, rel2,
925 : JOIN_INNER, sjinfo,
926 : restrictlist);
927 186806 : add_paths_to_joinrel(root, joinrel, rel2, rel1,
928 : JOIN_INNER, sjinfo,
929 : restrictlist);
930 186806 : break;
931 81364 : case JOIN_LEFT:
932 162674 : if (is_dummy_rel(rel1) ||
933 81310 : restriction_is_constant_false(restrictlist, joinrel, true))
934 : {
935 86 : mark_dummy_rel(joinrel);
936 86 : break;
937 : }
938 81470 : if (restriction_is_constant_false(restrictlist, joinrel, false) &&
939 192 : bms_is_subset(rel2->relids, sjinfo->syn_righthand))
940 168 : mark_dummy_rel(rel2);
941 81278 : add_paths_to_joinrel(root, joinrel, rel1, rel2,
942 : JOIN_LEFT, sjinfo,
943 : restrictlist);
944 81278 : add_paths_to_joinrel(root, joinrel, rel2, rel1,
945 : JOIN_RIGHT, sjinfo,
946 : restrictlist);
947 81278 : break;
948 1708 : case JOIN_FULL:
949 3416 : if ((is_dummy_rel(rel1) && is_dummy_rel(rel2)) ||
950 1708 : restriction_is_constant_false(restrictlist, joinrel, true))
951 : {
952 0 : mark_dummy_rel(joinrel);
953 0 : break;
954 : }
955 1708 : add_paths_to_joinrel(root, joinrel, rel1, rel2,
956 : JOIN_FULL, sjinfo,
957 : restrictlist);
958 1708 : add_paths_to_joinrel(root, joinrel, rel2, rel1,
959 : JOIN_FULL, sjinfo,
960 : restrictlist);
961 :
962 : /*
963 : * If there are join quals that aren't mergeable or hashable, we
964 : * may not be able to build any valid plan. Complain here so that
965 : * we can give a somewhat-useful error message. (Since we have no
966 : * flexibility of planning for a full join, there's no chance of
967 : * succeeding later with another pair of input rels.)
968 : */
969 1708 : if (joinrel->pathlist == NIL)
970 0 : ereport(ERROR,
971 : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
972 : errmsg("FULL JOIN is only supported with merge-joinable or hash-joinable join conditions")));
973 1708 : break;
974 4454 : case JOIN_SEMI:
975 :
976 : /*
977 : * We might have a normal semijoin, or a case where we don't have
978 : * enough rels to do the semijoin but can unique-ify the RHS and
979 : * then do an innerjoin (see comments in join_is_legal). In the
980 : * latter case we can't apply JOIN_SEMI joining.
981 : */
982 8682 : if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
983 4228 : bms_is_subset(sjinfo->min_righthand, rel2->relids))
984 : {
985 8450 : if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
986 4222 : restriction_is_constant_false(restrictlist, joinrel, false))
987 : {
988 12 : mark_dummy_rel(joinrel);
989 12 : break;
990 : }
991 4216 : add_paths_to_joinrel(root, joinrel, rel1, rel2,
992 : JOIN_SEMI, sjinfo,
993 : restrictlist);
994 4216 : add_paths_to_joinrel(root, joinrel, rel2, rel1,
995 : JOIN_RIGHT_SEMI, sjinfo,
996 : restrictlist);
997 : }
998 :
999 : /*
1000 : * If we know how to unique-ify the RHS and one input rel is
1001 : * exactly the RHS (not a superset) we can consider unique-ifying
1002 : * it and then doing a regular join. (The create_unique_path
1003 : * check here is probably redundant with what join_is_legal did,
1004 : * but if so the check is cheap because it's cached. So test
1005 : * anyway to be sure.)
1006 : */
1007 8884 : if (bms_equal(sjinfo->syn_righthand, rel2->relids) &&
1008 4442 : create_unique_path(root, rel2, rel2->cheapest_total_path,
1009 : sjinfo) != NULL)
1010 : {
1011 8656 : if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
1012 4328 : restriction_is_constant_false(restrictlist, joinrel, false))
1013 : {
1014 0 : mark_dummy_rel(joinrel);
1015 0 : break;
1016 : }
1017 4328 : add_paths_to_joinrel(root, joinrel, rel1, rel2,
1018 : JOIN_UNIQUE_INNER, sjinfo,
1019 : restrictlist);
1020 4328 : add_paths_to_joinrel(root, joinrel, rel2, rel1,
1021 : JOIN_UNIQUE_OUTER, sjinfo,
1022 : restrictlist);
1023 : }
1024 4442 : break;
1025 4626 : case JOIN_ANTI:
1026 9252 : if (is_dummy_rel(rel1) ||
1027 4626 : restriction_is_constant_false(restrictlist, joinrel, true))
1028 : {
1029 0 : mark_dummy_rel(joinrel);
1030 0 : break;
1031 : }
1032 4626 : if (restriction_is_constant_false(restrictlist, joinrel, false) &&
1033 0 : bms_is_subset(rel2->relids, sjinfo->syn_righthand))
1034 0 : mark_dummy_rel(rel2);
1035 4626 : add_paths_to_joinrel(root, joinrel, rel1, rel2,
1036 : JOIN_ANTI, sjinfo,
1037 : restrictlist);
1038 4626 : add_paths_to_joinrel(root, joinrel, rel2, rel1,
1039 : JOIN_RIGHT_ANTI, sjinfo,
1040 : restrictlist);
1041 4626 : break;
1042 0 : default:
1043 : /* other values not expected here */
1044 0 : elog(ERROR, "unrecognized join type: %d", (int) sjinfo->jointype);
1045 : break;
1046 : }
1047 :
1048 : /* Apply partitionwise join technique, if possible. */
1049 279144 : try_partitionwise_join(root, rel1, rel2, joinrel, sjinfo, restrictlist);
1050 279144 : }
1051 :
1052 :
1053 : /*
1054 : * have_join_order_restriction
1055 : * Detect whether the two relations should be joined to satisfy
1056 : * a join-order restriction arising from special or lateral joins.
1057 : *
1058 : * In practice this is always used with have_relevant_joinclause(), and so
1059 : * could be merged with that function, but it seems clearer to separate the
1060 : * two concerns. We need this test because there are degenerate cases where
1061 : * a clauseless join must be performed to satisfy join-order restrictions.
1062 : * Also, if one rel has a lateral reference to the other, or both are needed
1063 : * to compute some PHV, we should consider joining them even if the join would
1064 : * be clauseless.
1065 : *
1066 : * Note: this is only a problem if one side of a degenerate outer join
1067 : * contains multiple rels, or a clauseless join is required within an
1068 : * IN/EXISTS RHS; else we will find a join path via the "last ditch" case in
1069 : * join_search_one_level(). We could dispense with this test if we were
1070 : * willing to try bushy plans in the "last ditch" case, but that seems much
1071 : * less efficient.
1072 : */
1073 : bool
1074 68136 : have_join_order_restriction(PlannerInfo *root,
1075 : RelOptInfo *rel1, RelOptInfo *rel2)
1076 : {
1077 68136 : bool result = false;
1078 : ListCell *l;
1079 :
1080 : /*
1081 : * If either side has a direct lateral reference to the other, attempt the
1082 : * join regardless of outer-join considerations.
1083 : */
1084 126352 : if (bms_overlap(rel1->relids, rel2->direct_lateral_relids) ||
1085 58216 : bms_overlap(rel2->relids, rel1->direct_lateral_relids))
1086 10702 : return true;
1087 :
1088 : /*
1089 : * Likewise, if both rels are needed to compute some PlaceHolderVar,
1090 : * attempt the join regardless of outer-join considerations. (This is not
1091 : * very desirable, because a PHV with a large eval_at set will cause a lot
1092 : * of probably-useless joins to be considered, but failing to do this can
1093 : * cause us to fail to construct a plan at all.)
1094 : */
1095 58986 : foreach(l, root->placeholder_list)
1096 : {
1097 1606 : PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
1098 :
1099 1978 : if (bms_is_subset(rel1->relids, phinfo->ph_eval_at) &&
1100 372 : bms_is_subset(rel2->relids, phinfo->ph_eval_at))
1101 54 : return true;
1102 : }
1103 :
1104 : /*
1105 : * It's possible that the rels correspond to the left and right sides of a
1106 : * degenerate outer join, that is, one with no joinclause mentioning the
1107 : * non-nullable side; in which case we should force the join to occur.
1108 : *
1109 : * Also, the two rels could represent a clauseless join that has to be
1110 : * completed to build up the LHS or RHS of an outer join.
1111 : */
1112 148104 : foreach(l, root->join_info_list)
1113 : {
1114 91962 : SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
1115 :
1116 : /* ignore full joins --- other mechanisms handle them */
1117 91962 : if (sjinfo->jointype == JOIN_FULL)
1118 42 : continue;
1119 :
1120 : /* Can we perform the SJ with these rels? */
1121 111770 : if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
1122 19850 : bms_is_subset(sjinfo->min_righthand, rel2->relids))
1123 : {
1124 938 : result = true;
1125 938 : break;
1126 : }
1127 96674 : if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
1128 5692 : bms_is_subset(sjinfo->min_righthand, rel1->relids))
1129 : {
1130 162 : result = true;
1131 162 : break;
1132 : }
1133 :
1134 : /*
1135 : * Might we need to join these rels to complete the RHS? We have to
1136 : * use "overlap" tests since either rel might include a lower SJ that
1137 : * has been proven to commute with this one.
1138 : */
1139 109114 : if (bms_overlap(sjinfo->min_righthand, rel1->relids) &&
1140 18294 : bms_overlap(sjinfo->min_righthand, rel2->relids))
1141 : {
1142 108 : result = true;
1143 108 : break;
1144 : }
1145 :
1146 : /* Likewise for the LHS. */
1147 112348 : if (bms_overlap(sjinfo->min_lefthand, rel1->relids) &&
1148 21636 : bms_overlap(sjinfo->min_lefthand, rel2->relids))
1149 : {
1150 30 : result = true;
1151 30 : break;
1152 : }
1153 : }
1154 :
1155 : /*
1156 : * We do not force the join to occur if either input rel can legally be
1157 : * joined to anything else using joinclauses. This essentially means that
1158 : * clauseless bushy joins are put off as long as possible. The reason is
1159 : * that when there is a join order restriction high up in the join tree
1160 : * (that is, with many rels inside the LHS or RHS), we would otherwise
1161 : * expend lots of effort considering very stupid join combinations within
1162 : * its LHS or RHS.
1163 : */
1164 57380 : if (result)
1165 : {
1166 2380 : if (has_legal_joinclause(root, rel1) ||
1167 1142 : has_legal_joinclause(root, rel2))
1168 210 : result = false;
1169 : }
1170 :
1171 57380 : return result;
1172 : }
1173 :
1174 :
1175 : /*
1176 : * has_join_restriction
1177 : * Detect whether the specified relation has join-order restrictions,
1178 : * due to being inside an outer join or an IN (sub-SELECT),
1179 : * or participating in any LATERAL references or multi-rel PHVs.
1180 : *
1181 : * Essentially, this tests whether have_join_order_restriction() could
1182 : * succeed with this rel and some other one. It's OK if we sometimes
1183 : * say "true" incorrectly. (Therefore, we don't bother with the relatively
1184 : * expensive has_legal_joinclause test.)
1185 : */
1186 : static bool
1187 30610 : has_join_restriction(PlannerInfo *root, RelOptInfo *rel)
1188 : {
1189 : ListCell *l;
1190 :
1191 30610 : if (rel->lateral_relids != NULL || rel->lateral_referencers != NULL)
1192 18690 : return true;
1193 :
1194 12550 : foreach(l, root->placeholder_list)
1195 : {
1196 666 : PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
1197 :
1198 666 : if (bms_is_subset(rel->relids, phinfo->ph_eval_at) &&
1199 144 : !bms_equal(rel->relids, phinfo->ph_eval_at))
1200 36 : return true;
1201 : }
1202 :
1203 12636 : foreach(l, root->join_info_list)
1204 : {
1205 2592 : SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
1206 :
1207 : /* ignore full joins --- other mechanisms preserve their ordering */
1208 2592 : if (sjinfo->jointype == JOIN_FULL)
1209 86 : continue;
1210 :
1211 : /* ignore if SJ is already contained in rel */
1212 3882 : if (bms_is_subset(sjinfo->min_lefthand, rel->relids) &&
1213 1376 : bms_is_subset(sjinfo->min_righthand, rel->relids))
1214 354 : continue;
1215 :
1216 : /* restricted if it overlaps LHS or RHS, but doesn't contain SJ */
1217 3258 : if (bms_overlap(sjinfo->min_lefthand, rel->relids) ||
1218 1106 : bms_overlap(sjinfo->min_righthand, rel->relids))
1219 1840 : return true;
1220 : }
1221 :
1222 10044 : return false;
1223 : }
1224 :
1225 :
1226 : /*
1227 : * has_legal_joinclause
1228 : * Detect whether the specified relation can legally be joined
1229 : * to any other rels using join clauses.
1230 : *
1231 : * We consider only joins to single other relations in the current
1232 : * initial_rels list. This is sufficient to get a "true" result in most real
1233 : * queries, and an occasional erroneous "false" will only cost a bit more
1234 : * planning time. The reason for this limitation is that considering joins to
1235 : * other joins would require proving that the other join rel can legally be
1236 : * formed, which seems like too much trouble for something that's only a
1237 : * heuristic to save planning time. (Note: we must look at initial_rels
1238 : * and not all of the query, since when we are planning a sub-joinlist we
1239 : * may be forced to make clauseless joins within initial_rels even though
1240 : * there are join clauses linking to other parts of the query.)
1241 : */
1242 : static bool
1243 2380 : has_legal_joinclause(PlannerInfo *root, RelOptInfo *rel)
1244 : {
1245 : ListCell *lc;
1246 :
1247 8748 : foreach(lc, root->initial_rels)
1248 : {
1249 6578 : RelOptInfo *rel2 = (RelOptInfo *) lfirst(lc);
1250 :
1251 : /* ignore rels that are already in "rel" */
1252 6578 : if (bms_overlap(rel->relids, rel2->relids))
1253 2812 : continue;
1254 :
1255 3766 : if (have_relevant_joinclause(root, rel, rel2))
1256 : {
1257 : Relids joinrelids;
1258 : SpecialJoinInfo *sjinfo;
1259 : bool reversed;
1260 :
1261 : /* join_is_legal needs relids of the union */
1262 318 : joinrelids = bms_union(rel->relids, rel2->relids);
1263 :
1264 318 : if (join_is_legal(root, rel, rel2, joinrelids,
1265 : &sjinfo, &reversed))
1266 : {
1267 : /* Yes, this will work */
1268 210 : bms_free(joinrelids);
1269 210 : return true;
1270 : }
1271 :
1272 108 : bms_free(joinrelids);
1273 : }
1274 : }
1275 :
1276 2170 : return false;
1277 : }
1278 :
1279 :
1280 : /*
1281 : * There's a pitfall for creating parameterized nestloops: suppose the inner
1282 : * rel (call it A) has a parameter that is a PlaceHolderVar, and that PHV's
1283 : * minimum eval_at set includes the outer rel (B) and some third rel (C).
1284 : * We might think we could create a B/A nestloop join that's parameterized by
1285 : * C. But we would end up with a plan in which the PHV's expression has to be
1286 : * evaluated as a nestloop parameter at the B/A join; and the executor is only
1287 : * set up to handle simple Vars as NestLoopParams. Rather than add complexity
1288 : * and overhead to the executor for such corner cases, it seems better to
1289 : * forbid the join. (Note that we can still make use of A's parameterized
1290 : * path with pre-joined B+C as the outer rel. have_join_order_restriction()
1291 : * ensures that we will consider making such a join even if there are not
1292 : * other reasons to do so.)
1293 : *
1294 : * So we check whether any PHVs used in the query could pose such a hazard.
1295 : * We don't have any simple way of checking whether a risky PHV would actually
1296 : * be used in the inner plan, and the case is so unusual that it doesn't seem
1297 : * worth working very hard on it.
1298 : *
1299 : * This needs to be checked in two places. If the inner rel's minimum
1300 : * parameterization would trigger the restriction, then join_is_legal() should
1301 : * reject the join altogether, because there will be no workable paths for it.
1302 : * But joinpath.c has to check again for every proposed nestloop path, because
1303 : * the inner path might have more than the minimum parameterization, causing
1304 : * some PHV to be dangerous for it that otherwise wouldn't be.
1305 : */
1306 : bool
1307 43058 : have_dangerous_phv(PlannerInfo *root,
1308 : Relids outer_relids, Relids inner_params)
1309 : {
1310 : ListCell *lc;
1311 :
1312 45860 : foreach(lc, root->placeholder_list)
1313 : {
1314 3054 : PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
1315 :
1316 3054 : if (!bms_is_subset(phinfo->ph_eval_at, inner_params))
1317 2250 : continue; /* ignore, could not be a nestloop param */
1318 804 : if (!bms_overlap(phinfo->ph_eval_at, outer_relids))
1319 192 : continue; /* ignore, not relevant to this join */
1320 612 : if (bms_is_subset(phinfo->ph_eval_at, outer_relids))
1321 360 : continue; /* safe, it can be eval'd within outerrel */
1322 : /* Otherwise, it's potentially unsafe, so reject the join */
1323 252 : return true;
1324 : }
1325 :
1326 : /* OK to perform the join */
1327 42806 : return false;
1328 : }
1329 :
1330 :
1331 : /*
1332 : * is_dummy_rel --- has relation been proven empty?
1333 : */
1334 : bool
1335 2260258 : is_dummy_rel(RelOptInfo *rel)
1336 : {
1337 : Path *path;
1338 :
1339 : /*
1340 : * A rel that is known dummy will have just one path that is a childless
1341 : * Append. (Even if somehow it has more paths, a childless Append will
1342 : * have cost zero and hence should be at the front of the pathlist.)
1343 : */
1344 2260258 : if (rel->pathlist == NIL)
1345 1243408 : return false;
1346 1016850 : path = (Path *) linitial(rel->pathlist);
1347 :
1348 : /*
1349 : * Initially, a dummy path will just be a childless Append. But in later
1350 : * planning stages we might stick a ProjectSetPath and/or ProjectionPath
1351 : * on top, since Append can't project. Rather than make assumptions about
1352 : * which combinations can occur, just descend through whatever we find.
1353 : */
1354 : for (;;)
1355 : {
1356 1048148 : if (IsA(path, ProjectionPath))
1357 27544 : path = ((ProjectionPath *) path)->subpath;
1358 1020604 : else if (IsA(path, ProjectSetPath))
1359 3754 : path = ((ProjectSetPath *) path)->subpath;
1360 : else
1361 1016850 : break;
1362 : }
1363 1016850 : if (IS_DUMMY_APPEND(path))
1364 3644 : return true;
1365 1013206 : return false;
1366 : }
1367 :
1368 : /*
1369 : * Mark a relation as proven empty.
1370 : *
1371 : * During GEQO planning, this can get invoked more than once on the same
1372 : * baserel struct, so it's worth checking to see if the rel is already marked
1373 : * dummy.
1374 : *
1375 : * Also, when called during GEQO join planning, we are in a short-lived
1376 : * memory context. We must make sure that the dummy path attached to a
1377 : * baserel survives the GEQO cycle, else the baserel is trashed for future
1378 : * GEQO cycles. On the other hand, when we are marking a joinrel during GEQO,
1379 : * we don't want the dummy path to clutter the main planning context. Upshot
1380 : * is that the best solution is to explicitly make the dummy path in the same
1381 : * context the given RelOptInfo is in.
1382 : */
1383 : void
1384 542 : mark_dummy_rel(RelOptInfo *rel)
1385 : {
1386 : MemoryContext oldcontext;
1387 :
1388 : /* Already marked? */
1389 542 : if (is_dummy_rel(rel))
1390 18 : return;
1391 :
1392 : /* No, so choose correct context to make the dummy path in */
1393 524 : oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
1394 :
1395 : /* Set dummy size estimate */
1396 524 : rel->rows = 0;
1397 :
1398 : /* Evict any previously chosen paths */
1399 524 : rel->pathlist = NIL;
1400 524 : rel->partial_pathlist = NIL;
1401 :
1402 : /* Set up the dummy path */
1403 524 : add_path(rel, (Path *) create_append_path(NULL, rel, NIL, NIL,
1404 : NIL, rel->lateral_relids,
1405 : 0, false, -1));
1406 :
1407 : /* Set or update cheapest_total_path and related fields */
1408 524 : set_cheapest(rel);
1409 :
1410 524 : MemoryContextSwitchTo(oldcontext);
1411 : }
1412 :
1413 :
1414 : /*
1415 : * restriction_is_constant_false --- is a restrictlist just FALSE?
1416 : *
1417 : * In cases where a qual is provably constant FALSE, eval_const_expressions
1418 : * will generally have thrown away anything that's ANDed with it. In outer
1419 : * join situations this will leave us computing cartesian products only to
1420 : * decide there's no match for an outer row, which is pretty stupid. So,
1421 : * we need to detect the case.
1422 : *
1423 : * If only_pushed_down is true, then consider only quals that are pushed-down
1424 : * from the point of view of the joinrel.
1425 : */
1426 : static bool
1427 369072 : restriction_is_constant_false(List *restrictlist,
1428 : RelOptInfo *joinrel,
1429 : bool only_pushed_down)
1430 : {
1431 : ListCell *lc;
1432 :
1433 : /*
1434 : * Despite the above comment, the restriction list we see here might
1435 : * possibly have other members besides the FALSE constant, since other
1436 : * quals could get "pushed down" to the outer join level. So we check
1437 : * each member of the list.
1438 : */
1439 771448 : foreach(lc, restrictlist)
1440 : {
1441 402774 : RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
1442 :
1443 402774 : if (only_pushed_down && !RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
1444 107266 : continue;
1445 :
1446 295508 : if (rinfo->clause && IsA(rinfo->clause, Const))
1447 : {
1448 4914 : Const *con = (Const *) rinfo->clause;
1449 :
1450 : /* constant NULL is as good as constant FALSE for our purposes */
1451 4914 : if (con->constisnull)
1452 398 : return true;
1453 4806 : if (!DatumGetBool(con->constvalue))
1454 290 : return true;
1455 : }
1456 : }
1457 368674 : return false;
1458 : }
1459 :
1460 : /*
1461 : * Assess whether join between given two partitioned relations can be broken
1462 : * down into joins between matching partitions; a technique called
1463 : * "partitionwise join"
1464 : *
1465 : * Partitionwise join is possible when a. Joining relations have same
1466 : * partitioning scheme b. There exists an equi-join between the partition keys
1467 : * of the two relations.
1468 : *
1469 : * Partitionwise join is planned as follows (details: optimizer/README.)
1470 : *
1471 : * 1. Create the RelOptInfos for joins between matching partitions i.e
1472 : * child-joins and add paths to them.
1473 : *
1474 : * 2. Construct Append or MergeAppend paths across the set of child joins.
1475 : * This second phase is implemented by generate_partitionwise_join_paths().
1476 : *
1477 : * The RelOptInfo, SpecialJoinInfo and restrictlist for each child join are
1478 : * obtained by translating the respective parent join structures.
1479 : */
1480 : static void
1481 279144 : try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
1482 : RelOptInfo *joinrel, SpecialJoinInfo *parent_sjinfo,
1483 : List *parent_restrictlist)
1484 : {
1485 279144 : bool rel1_is_simple = IS_SIMPLE_REL(rel1);
1486 279144 : bool rel2_is_simple = IS_SIMPLE_REL(rel2);
1487 279144 : List *parts1 = NIL;
1488 279144 : List *parts2 = NIL;
1489 279144 : ListCell *lcr1 = NULL;
1490 279144 : ListCell *lcr2 = NULL;
1491 : int cnt_parts;
1492 :
1493 : /* Guard against stack overflow due to overly deep partition hierarchy. */
1494 279144 : check_stack_depth();
1495 :
1496 : /* Nothing to do, if the join relation is not partitioned. */
1497 279144 : if (joinrel->part_scheme == NULL || joinrel->nparts == 0)
1498 277144 : return;
1499 :
1500 : /* The join relation should have consider_partitionwise_join set. */
1501 : Assert(joinrel->consider_partitionwise_join);
1502 :
1503 : /*
1504 : * We can not perform partitionwise join if either of the joining
1505 : * relations is not partitioned.
1506 : */
1507 2138 : if (!IS_PARTITIONED_REL(rel1) || !IS_PARTITIONED_REL(rel2))
1508 18 : return;
1509 :
1510 : Assert(REL_HAS_ALL_PART_PROPS(rel1) && REL_HAS_ALL_PART_PROPS(rel2));
1511 :
1512 : /* The joining relations should have consider_partitionwise_join set. */
1513 : Assert(rel1->consider_partitionwise_join &&
1514 : rel2->consider_partitionwise_join);
1515 :
1516 : /*
1517 : * The partition scheme of the join relation should match that of the
1518 : * joining relations.
1519 : */
1520 : Assert(joinrel->part_scheme == rel1->part_scheme &&
1521 : joinrel->part_scheme == rel2->part_scheme);
1522 :
1523 : Assert(!(joinrel->partbounds_merged && (joinrel->nparts <= 0)));
1524 :
1525 2120 : compute_partition_bounds(root, rel1, rel2, joinrel, parent_sjinfo,
1526 : &parts1, &parts2);
1527 :
1528 2120 : if (joinrel->partbounds_merged)
1529 : {
1530 768 : lcr1 = list_head(parts1);
1531 768 : lcr2 = list_head(parts2);
1532 : }
1533 :
1534 : /*
1535 : * Create child-join relations for this partitioned join, if those don't
1536 : * exist. Add paths to child-joins for a pair of child relations
1537 : * corresponding to the given pair of parent relations.
1538 : */
1539 7430 : for (cnt_parts = 0; cnt_parts < joinrel->nparts; cnt_parts++)
1540 : {
1541 : RelOptInfo *child_rel1;
1542 : RelOptInfo *child_rel2;
1543 : bool rel1_empty;
1544 : bool rel2_empty;
1545 : SpecialJoinInfo *child_sjinfo;
1546 : List *child_restrictlist;
1547 : RelOptInfo *child_joinrel;
1548 : AppendRelInfo **appinfos;
1549 : int nappinfos;
1550 : Relids child_relids;
1551 :
1552 5430 : if (joinrel->partbounds_merged)
1553 : {
1554 2010 : child_rel1 = lfirst_node(RelOptInfo, lcr1);
1555 2010 : child_rel2 = lfirst_node(RelOptInfo, lcr2);
1556 2010 : lcr1 = lnext(parts1, lcr1);
1557 2010 : lcr2 = lnext(parts2, lcr2);
1558 : }
1559 : else
1560 : {
1561 3420 : child_rel1 = rel1->part_rels[cnt_parts];
1562 3420 : child_rel2 = rel2->part_rels[cnt_parts];
1563 : }
1564 :
1565 5430 : rel1_empty = (child_rel1 == NULL || IS_DUMMY_REL(child_rel1));
1566 5430 : rel2_empty = (child_rel2 == NULL || IS_DUMMY_REL(child_rel2));
1567 :
1568 : /*
1569 : * Check for cases where we can prove that this segment of the join
1570 : * returns no rows, due to one or both inputs being empty (including
1571 : * inputs that have been pruned away entirely). If so just ignore it.
1572 : * These rules are equivalent to populate_joinrel_with_paths's rules
1573 : * for dummy input relations.
1574 : */
1575 5430 : switch (parent_sjinfo->jointype)
1576 : {
1577 2624 : case JOIN_INNER:
1578 : case JOIN_SEMI:
1579 2624 : if (rel1_empty || rel2_empty)
1580 64 : continue; /* ignore this join segment */
1581 2588 : break;
1582 2084 : case JOIN_LEFT:
1583 : case JOIN_ANTI:
1584 2084 : if (rel1_empty)
1585 28 : continue; /* ignore this join segment */
1586 2056 : break;
1587 722 : case JOIN_FULL:
1588 722 : if (rel1_empty && rel2_empty)
1589 0 : continue; /* ignore this join segment */
1590 722 : break;
1591 0 : default:
1592 : /* other values not expected here */
1593 0 : elog(ERROR, "unrecognized join type: %d",
1594 : (int) parent_sjinfo->jointype);
1595 : break;
1596 : }
1597 :
1598 : /*
1599 : * If a child has been pruned entirely then we can't generate paths
1600 : * for it, so we have to reject partitionwise joining unless we were
1601 : * able to eliminate this partition above.
1602 : */
1603 5366 : if (child_rel1 == NULL || child_rel2 == NULL)
1604 : {
1605 : /*
1606 : * Mark the joinrel as unpartitioned so that later functions treat
1607 : * it correctly.
1608 : */
1609 120 : joinrel->nparts = 0;
1610 120 : return;
1611 : }
1612 :
1613 : /*
1614 : * If a leaf relation has consider_partitionwise_join=false, it means
1615 : * that it's a dummy relation for which we skipped setting up tlist
1616 : * expressions and adding EC members in set_append_rel_size(), so
1617 : * again we have to fail here.
1618 : */
1619 5246 : if (rel1_is_simple && !child_rel1->consider_partitionwise_join)
1620 : {
1621 : Assert(child_rel1->reloptkind == RELOPT_OTHER_MEMBER_REL);
1622 : Assert(IS_DUMMY_REL(child_rel1));
1623 0 : joinrel->nparts = 0;
1624 0 : return;
1625 : }
1626 5246 : if (rel2_is_simple && !child_rel2->consider_partitionwise_join)
1627 : {
1628 : Assert(child_rel2->reloptkind == RELOPT_OTHER_MEMBER_REL);
1629 : Assert(IS_DUMMY_REL(child_rel2));
1630 0 : joinrel->nparts = 0;
1631 0 : return;
1632 : }
1633 :
1634 : /* We should never try to join two overlapping sets of rels. */
1635 : Assert(!bms_overlap(child_rel1->relids, child_rel2->relids));
1636 :
1637 : /*
1638 : * Construct SpecialJoinInfo from parent join relations's
1639 : * SpecialJoinInfo.
1640 : */
1641 5246 : child_sjinfo = build_child_join_sjinfo(root, parent_sjinfo,
1642 : child_rel1->relids,
1643 : child_rel2->relids);
1644 :
1645 : /* Find the AppendRelInfo structures */
1646 5246 : child_relids = bms_union(child_rel1->relids, child_rel2->relids);
1647 5246 : appinfos = find_appinfos_by_relids(root, child_relids,
1648 : &nappinfos);
1649 :
1650 : /*
1651 : * Construct restrictions applicable to the child join from those
1652 : * applicable to the parent join.
1653 : */
1654 : child_restrictlist =
1655 5246 : (List *) adjust_appendrel_attrs(root,
1656 : (Node *) parent_restrictlist,
1657 : nappinfos, appinfos);
1658 :
1659 : /* Find or construct the child join's RelOptInfo */
1660 5246 : child_joinrel = joinrel->part_rels[cnt_parts];
1661 5246 : if (!child_joinrel)
1662 : {
1663 4774 : child_joinrel = build_child_join_rel(root, child_rel1, child_rel2,
1664 : joinrel, child_restrictlist,
1665 : child_sjinfo, nappinfos, appinfos);
1666 4774 : joinrel->part_rels[cnt_parts] = child_joinrel;
1667 4774 : joinrel->live_parts = bms_add_member(joinrel->live_parts, cnt_parts);
1668 4774 : joinrel->all_partrels = bms_add_members(joinrel->all_partrels,
1669 4774 : child_joinrel->relids);
1670 : }
1671 :
1672 : /* Assert we got the right one */
1673 : Assert(bms_equal(child_joinrel->relids,
1674 : adjust_child_relids(joinrel->relids,
1675 : nappinfos, appinfos)));
1676 :
1677 : /* And make paths for the child join */
1678 5246 : populate_joinrel_with_paths(root, child_rel1, child_rel2,
1679 : child_joinrel, child_sjinfo,
1680 : child_restrictlist);
1681 :
1682 : /*
1683 : * When there are thousands of partitions involved, this loop will
1684 : * accumulate a significant amount of memory usage from objects that
1685 : * are only needed within the loop. Free these local objects eagerly
1686 : * at the end of each iteration.
1687 : */
1688 5246 : pfree(appinfos);
1689 5246 : bms_free(child_relids);
1690 5246 : free_child_join_sjinfo(child_sjinfo);
1691 : }
1692 : }
1693 :
1694 : /*
1695 : * Construct the SpecialJoinInfo for a child-join by translating
1696 : * SpecialJoinInfo for the join between parents. left_relids and right_relids
1697 : * are the relids of left and right side of the join respectively.
1698 : *
1699 : * If translations are added to or removed from this function, consider
1700 : * updating free_child_join_sjinfo() accordingly.
1701 : */
1702 : static SpecialJoinInfo *
1703 5246 : build_child_join_sjinfo(PlannerInfo *root, SpecialJoinInfo *parent_sjinfo,
1704 : Relids left_relids, Relids right_relids)
1705 : {
1706 5246 : SpecialJoinInfo *sjinfo = makeNode(SpecialJoinInfo);
1707 : AppendRelInfo **left_appinfos;
1708 : int left_nappinfos;
1709 : AppendRelInfo **right_appinfos;
1710 : int right_nappinfos;
1711 :
1712 : /* Dummy SpecialJoinInfos can be created without any translation. */
1713 5246 : if (parent_sjinfo->jointype == JOIN_INNER)
1714 : {
1715 : Assert(parent_sjinfo->ojrelid == 0);
1716 2192 : init_dummy_sjinfo(sjinfo, left_relids, right_relids);
1717 2192 : return sjinfo;
1718 : }
1719 :
1720 3054 : memcpy(sjinfo, parent_sjinfo, sizeof(SpecialJoinInfo));
1721 3054 : left_appinfos = find_appinfos_by_relids(root, left_relids,
1722 : &left_nappinfos);
1723 3054 : right_appinfos = find_appinfos_by_relids(root, right_relids,
1724 : &right_nappinfos);
1725 :
1726 3054 : sjinfo->min_lefthand = adjust_child_relids(sjinfo->min_lefthand,
1727 : left_nappinfos, left_appinfos);
1728 3054 : sjinfo->min_righthand = adjust_child_relids(sjinfo->min_righthand,
1729 : right_nappinfos,
1730 : right_appinfos);
1731 3054 : sjinfo->syn_lefthand = adjust_child_relids(sjinfo->syn_lefthand,
1732 : left_nappinfos, left_appinfos);
1733 3054 : sjinfo->syn_righthand = adjust_child_relids(sjinfo->syn_righthand,
1734 : right_nappinfos,
1735 : right_appinfos);
1736 : /* outer-join relids need no adjustment */
1737 6108 : sjinfo->semi_rhs_exprs = (List *) adjust_appendrel_attrs(root,
1738 3054 : (Node *) sjinfo->semi_rhs_exprs,
1739 : right_nappinfos,
1740 : right_appinfos);
1741 :
1742 3054 : pfree(left_appinfos);
1743 3054 : pfree(right_appinfos);
1744 :
1745 3054 : return sjinfo;
1746 : }
1747 :
1748 : /*
1749 : * free_child_join_sjinfo
1750 : * Free memory consumed by a SpecialJoinInfo created by
1751 : * build_child_join_sjinfo()
1752 : *
1753 : * Only members that are translated copies of their counterpart in the parent
1754 : * SpecialJoinInfo are freed here.
1755 : */
1756 : static void
1757 5246 : free_child_join_sjinfo(SpecialJoinInfo *sjinfo)
1758 : {
1759 : /*
1760 : * Dummy SpecialJoinInfos of inner joins do not have any translated fields
1761 : * and hence no fields that to be freed.
1762 : */
1763 5246 : if (sjinfo->jointype != JOIN_INNER)
1764 : {
1765 3054 : bms_free(sjinfo->min_lefthand);
1766 3054 : bms_free(sjinfo->min_righthand);
1767 3054 : bms_free(sjinfo->syn_lefthand);
1768 3054 : bms_free(sjinfo->syn_righthand);
1769 :
1770 : /*
1771 : * semi_rhs_exprs may in principle be freed, but a simple pfree() does
1772 : * not suffice, so we leave it alone.
1773 : */
1774 : }
1775 :
1776 5246 : pfree(sjinfo);
1777 5246 : }
1778 :
1779 : /*
1780 : * compute_partition_bounds
1781 : * Compute the partition bounds for a join rel from those for inputs
1782 : */
1783 : static void
1784 2120 : compute_partition_bounds(PlannerInfo *root, RelOptInfo *rel1,
1785 : RelOptInfo *rel2, RelOptInfo *joinrel,
1786 : SpecialJoinInfo *parent_sjinfo,
1787 : List **parts1, List **parts2)
1788 : {
1789 : /*
1790 : * If we don't have the partition bounds for the join rel yet, try to
1791 : * compute those along with pairs of partitions to be joined.
1792 : */
1793 2120 : if (joinrel->nparts == -1)
1794 : {
1795 1956 : PartitionScheme part_scheme = joinrel->part_scheme;
1796 1956 : PartitionBoundInfo boundinfo = NULL;
1797 1956 : int nparts = 0;
1798 :
1799 : Assert(joinrel->boundinfo == NULL);
1800 : Assert(joinrel->part_rels == NULL);
1801 :
1802 : /*
1803 : * See if the partition bounds for inputs are exactly the same, in
1804 : * which case we don't need to work hard: the join rel will have the
1805 : * same partition bounds as inputs, and the partitions with the same
1806 : * cardinal positions will form the pairs.
1807 : *
1808 : * Note: even in cases where one or both inputs have merged bounds, it
1809 : * would be possible for both the bounds to be exactly the same, but
1810 : * it seems unlikely to be worth the cycles to check.
1811 : */
1812 1956 : if (!rel1->partbounds_merged &&
1813 1896 : !rel2->partbounds_merged &&
1814 3534 : rel1->nparts == rel2->nparts &&
1815 1638 : partition_bounds_equal(part_scheme->partnatts,
1816 : part_scheme->parttyplen,
1817 : part_scheme->parttypbyval,
1818 : rel1->boundinfo, rel2->boundinfo))
1819 : {
1820 1110 : boundinfo = rel1->boundinfo;
1821 1110 : nparts = rel1->nparts;
1822 : }
1823 : else
1824 : {
1825 : /* Try merging the partition bounds for inputs. */
1826 846 : boundinfo = partition_bounds_merge(part_scheme->partnatts,
1827 846 : part_scheme->partsupfunc,
1828 : part_scheme->partcollation,
1829 : rel1, rel2,
1830 : parent_sjinfo->jointype,
1831 : parts1, parts2);
1832 846 : if (boundinfo == NULL)
1833 : {
1834 114 : joinrel->nparts = 0;
1835 114 : return;
1836 : }
1837 732 : nparts = list_length(*parts1);
1838 732 : joinrel->partbounds_merged = true;
1839 : }
1840 :
1841 : Assert(nparts > 0);
1842 1842 : joinrel->boundinfo = boundinfo;
1843 1842 : joinrel->nparts = nparts;
1844 1842 : joinrel->part_rels =
1845 1842 : (RelOptInfo **) palloc0(sizeof(RelOptInfo *) * nparts);
1846 : }
1847 : else
1848 : {
1849 : Assert(joinrel->nparts > 0);
1850 : Assert(joinrel->boundinfo);
1851 : Assert(joinrel->part_rels);
1852 :
1853 : /*
1854 : * If the join rel's partbounds_merged flag is true, it means inputs
1855 : * are not guaranteed to have the same partition bounds, therefore we
1856 : * can't assume that the partitions at the same cardinal positions
1857 : * form the pairs; let get_matching_part_pairs() generate the pairs.
1858 : * Otherwise, nothing to do since we can assume that.
1859 : */
1860 164 : if (joinrel->partbounds_merged)
1861 : {
1862 36 : get_matching_part_pairs(root, joinrel, rel1, rel2,
1863 : parts1, parts2);
1864 : Assert(list_length(*parts1) == joinrel->nparts);
1865 : Assert(list_length(*parts2) == joinrel->nparts);
1866 : }
1867 : }
1868 : }
1869 :
1870 : /*
1871 : * get_matching_part_pairs
1872 : * Generate pairs of partitions to be joined from inputs
1873 : */
1874 : static void
1875 36 : get_matching_part_pairs(PlannerInfo *root, RelOptInfo *joinrel,
1876 : RelOptInfo *rel1, RelOptInfo *rel2,
1877 : List **parts1, List **parts2)
1878 : {
1879 36 : bool rel1_is_simple = IS_SIMPLE_REL(rel1);
1880 36 : bool rel2_is_simple = IS_SIMPLE_REL(rel2);
1881 : int cnt_parts;
1882 :
1883 36 : *parts1 = NIL;
1884 36 : *parts2 = NIL;
1885 :
1886 132 : for (cnt_parts = 0; cnt_parts < joinrel->nparts; cnt_parts++)
1887 : {
1888 96 : RelOptInfo *child_joinrel = joinrel->part_rels[cnt_parts];
1889 : RelOptInfo *child_rel1;
1890 : RelOptInfo *child_rel2;
1891 : Relids child_relids1;
1892 : Relids child_relids2;
1893 :
1894 : /*
1895 : * If this segment of the join is empty, it means that this segment
1896 : * was ignored when previously creating child-join paths for it in
1897 : * try_partitionwise_join() as it would not contribute to the join
1898 : * result, due to one or both inputs being empty; add NULL to each of
1899 : * the given lists so that this segment will be ignored again in that
1900 : * function.
1901 : */
1902 96 : if (!child_joinrel)
1903 : {
1904 0 : *parts1 = lappend(*parts1, NULL);
1905 0 : *parts2 = lappend(*parts2, NULL);
1906 0 : continue;
1907 : }
1908 :
1909 : /*
1910 : * Get a relids set of partition(s) involved in this join segment that
1911 : * are from the rel1 side.
1912 : */
1913 96 : child_relids1 = bms_intersect(child_joinrel->relids,
1914 96 : rel1->all_partrels);
1915 : Assert(bms_num_members(child_relids1) == bms_num_members(rel1->relids));
1916 :
1917 : /*
1918 : * Get a child rel for rel1 with the relids. Note that we should have
1919 : * the child rel even if rel1 is a join rel, because in that case the
1920 : * partitions specified in the relids would have matching/overlapping
1921 : * boundaries, so the specified partitions should be considered as
1922 : * ones to be joined when planning partitionwise joins of rel1,
1923 : * meaning that the child rel would have been built by the time we get
1924 : * here.
1925 : */
1926 96 : if (rel1_is_simple)
1927 : {
1928 0 : int varno = bms_singleton_member(child_relids1);
1929 :
1930 0 : child_rel1 = find_base_rel(root, varno);
1931 : }
1932 : else
1933 96 : child_rel1 = find_join_rel(root, child_relids1);
1934 : Assert(child_rel1);
1935 :
1936 : /*
1937 : * Get a relids set of partition(s) involved in this join segment that
1938 : * are from the rel2 side.
1939 : */
1940 96 : child_relids2 = bms_intersect(child_joinrel->relids,
1941 96 : rel2->all_partrels);
1942 : Assert(bms_num_members(child_relids2) == bms_num_members(rel2->relids));
1943 :
1944 : /*
1945 : * Get a child rel for rel2 with the relids. See above comments.
1946 : */
1947 96 : if (rel2_is_simple)
1948 : {
1949 96 : int varno = bms_singleton_member(child_relids2);
1950 :
1951 96 : child_rel2 = find_base_rel(root, varno);
1952 : }
1953 : else
1954 0 : child_rel2 = find_join_rel(root, child_relids2);
1955 : Assert(child_rel2);
1956 :
1957 : /*
1958 : * The join of rel1 and rel2 is legal, so is the join of the child
1959 : * rels obtained above; add them to the given lists as a join pair
1960 : * producing this join segment.
1961 : */
1962 96 : *parts1 = lappend(*parts1, child_rel1);
1963 96 : *parts2 = lappend(*parts2, child_rel2);
1964 : }
1965 36 : }
|