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