Age Owner Branch data TLA 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 *child_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
5251 tgl@sss.pgh.pa.us 72 :CBC 56595 : join_search_one_level(PlannerInfo *root, int level)
73 : : {
74 : 56595 : List **joinrels = root->join_rel_level;
75 : : ListCell *r;
76 : : int k;
77 : :
78 [ - + ]: 56595 : Assert(joinrels[level] == NIL);
79 : :
80 : : /* Set join_cur_level so that new joinrels are added to proper list */
81 : 56595 : 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 : : */
8424 bruce@momjian.us 90 [ + + + + : 200218 : foreach(r, joinrels[level - 1])
+ + ]
91 : : {
9187 92 : 143624 : RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
93 : :
6267 tgl@sss.pgh.pa.us 94 [ + + + + : 157813 : if (old_rel->joininfo != NIL || old_rel->has_eclass_joins ||
+ + ]
95 : 14189 : has_join_restriction(root, old_rel))
8833 96 : 138722 : {
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 : : */
4377 112 [ + + ]: 138722 : if (level == 2) /* consider remaining initial rels */
252 drowley@postgresql.o 113 :GNC 92995 : first_rel = foreach_current_index(r) + 1;
114 : : else
115 : 45727 : first_rel = 0;
116 : :
117 : 138722 : 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 : : */
5251 tgl@sss.pgh.pa.us 133 :CBC 4902 : make_rels_by_clauseless_joins(root,
134 : : old_rel,
1735 135 : 4902 : 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 : : */
8424 bruce@momjian.us 147 : 56594 : for (k = 2;; k++)
9091 148 : 5875 : {
8615 tgl@sss.pgh.pa.us 149 : 62469 : 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 [ + + ]: 62469 : if (k > other_level)
8833 156 : 56594 : break;
157 : :
8615 158 [ + - + + : 29494 : foreach(r, joinrels[k])
+ + ]
159 : : {
160 : 23619 : 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 : : */
6294 169 [ + + + + ]: 23619 : if (old_rel->joininfo == NIL && !old_rel->has_eclass_joins &&
6267 170 [ + + ]: 236 : !has_join_restriction(root, old_rel))
6333 171 : 188 : continue;
172 : :
252 drowley@postgresql.o 173 [ + + ]:GNC 23431 : if (k == other_level) /* only consider remaining rels */
174 : 16483 : first_rel = foreach_current_index(r) + 1;
175 : : else
176 : 6948 : first_rel = 0;
177 : :
178 [ + - + + : 98721 : for_each_from(r2, joinrels[other_level], first_rel)
+ + ]
179 : : {
8615 tgl@sss.pgh.pa.us 180 :CBC 75290 : RelOptInfo *new_rel = (RelOptInfo *) lfirst(r2);
181 : :
7736 182 [ + + ]: 75290 : 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 : : */
6267 189 [ + + + + ]: 9548 : if (have_relevant_joinclause(root, old_rel, new_rel) ||
190 : 533 : have_join_order_restriction(root, old_rel, new_rel))
191 : : {
5251 192 : 8509 : (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 : : */
4260 218 [ + + ]: 56594 : 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 [ + - + + : 27 : foreach(r, joinrels[level - 1])
+ + ]
225 : : {
226 : 18 : RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
227 : :
228 : 18 : make_rels_by_clauseless_joins(root,
229 : : old_rel,
1735 230 : 18 : 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 : : */
4249 251 [ + + ]: 9 : if (joinrels[level] == NIL &&
252 [ - + ]: 3 : root->join_info_list == NIL &&
3047 tgl@sss.pgh.pa.us 253 [ # # ]:UBC 0 : !root->hasLateralRTEs)
4260 254 [ # # ]: 0 : elog(ERROR, "failed to build any %d-way joins", level);
255 : : }
10141 scrappy@hub.org 256 :CBC 56594 : }
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
6888 tgl@sss.pgh.pa.us 279 : 138722 : 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 : :
252 drowley@postgresql.o 286 [ + - + + :GNC 410656 : for_each_from(l, other_rels, first_rel_idx)
+ + ]
287 : : {
6884 tgl@sss.pgh.pa.us 288 :CBC 271934 : RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
289 : :
290 [ + + + + ]: 426637 : if (!bms_overlap(old_rel->relids, other_rel->relids) &&
6267 291 [ + + ]: 188390 : (have_relevant_joinclause(root, old_rel, other_rel) ||
292 : 33687 : have_join_order_restriction(root, old_rel, other_rel)))
293 : : {
5251 294 : 126350 : (void) make_join_rel(root, old_rel, other_rel);
295 : : }
296 : : }
10141 scrappy@hub.org 297 : 138722 : }
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
6888 tgl@sss.pgh.pa.us 313 : 4920 : make_rels_by_clauseless_joins(PlannerInfo *root,
314 : : RelOptInfo *old_rel,
315 : : List *other_rels)
316 : : {
317 : : ListCell *l;
318 : :
1735 319 [ + - + + : 16008 : foreach(l, other_rels)
+ + ]
320 : : {
5251 321 : 11089 : RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
322 : :
7736 323 [ + + ]: 11089 : if (!bms_overlap(other_rel->relids, old_rel->relids))
324 : : {
5251 325 : 5490 : (void) make_join_rel(root, old_rel, other_rel);
326 : : }
327 : : }
10141 scrappy@hub.org 328 : 4919 : }
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
6015 tgl@sss.pgh.pa.us 349 : 142908 : 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 : : */
5722 363 : 142908 : *sjinfo_p = NULL;
364 : 142908 : *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 : 142908 : match_sjinfo = NULL;
372 : 142908 : reversed = false;
5383 373 : 142908 : unique_ified = false;
3174 374 : 142908 : must_be_leftjoin = false;
375 : :
5722 376 [ + + + + : 311830 : foreach(l, root->join_info_list)
+ + ]
377 : : {
378 : 174021 : 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 [ + + ]: 174021 : if (!bms_overlap(sjinfo->min_righthand, joinrelids))
6690 386 : 59196 : 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 : : */
5722 392 [ + + ]: 114825 : if (bms_is_subset(joinrelids, sjinfo->min_righthand))
6690 393 : 2512 : continue;
394 : :
395 : : /*
396 : : * Also, not relevant if SJ is already done within either input.
397 : : */
5722 398 [ + + + + ]: 209711 : if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
399 : 97398 : bms_is_subset(sjinfo->min_righthand, rel1->relids))
6690 400 : 47693 : continue;
5722 401 [ + + + + ]: 73642 : if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
402 : 9022 : bms_is_subset(sjinfo->min_righthand, rel2->relids))
6690 403 : 4594 : 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 : : */
5379 411 [ + + ]: 60026 : if (sjinfo->jointype == JOIN_SEMI)
412 : : {
413 [ + + ]: 3862 : if (bms_is_subset(sjinfo->syn_righthand, rel1->relids) &&
414 [ + + ]: 773 : !bms_equal(sjinfo->syn_righthand, rel1->relids))
415 : 306 : continue;
416 [ + + ]: 3556 : if (bms_is_subset(sjinfo->syn_righthand, rel2->relids) &&
417 [ + + ]: 2021 : !bms_equal(sjinfo->syn_righthand, rel2->relids))
418 : 107 : 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 : : */
5722 428 [ + + + + ]: 109242 : if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
429 : 49629 : bms_is_subset(sjinfo->min_righthand, rel2->relids))
430 : : {
431 [ - + ]: 46567 : if (match_sjinfo)
5995 bruce@momjian.us 432 :UBC 0 : return false; /* invalid join path */
5722 tgl@sss.pgh.pa.us 433 :CBC 46567 : match_sjinfo = sjinfo;
434 : 46567 : reversed = false;
435 : : }
436 [ + + + + ]: 17308 : else if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
437 : 4262 : bms_is_subset(sjinfo->min_righthand, rel1->relids))
438 : : {
439 [ - + ]: 3668 : if (match_sjinfo)
5995 bruce@momjian.us 440 :UBC 0 : return false; /* invalid join path */
5722 tgl@sss.pgh.pa.us 441 :CBC 3668 : match_sjinfo = sjinfo;
442 : 3668 : reversed = true;
443 : : }
5622 444 [ + + + + ]: 10864 : else if (sjinfo->jointype == JOIN_SEMI &&
5616 445 [ + + ]: 1759 : bms_equal(sjinfo->syn_righthand, rel2->relids) &&
446 : 273 : 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 : : */
5622 471 [ + + ]: 270 : if (match_sjinfo)
472 : 42 : return false; /* invalid join path */
473 : 228 : match_sjinfo = sjinfo;
474 : 228 : reversed = false;
5383 475 : 228 : unique_ified = true;
476 : : }
5622 477 [ + + + + ]: 10324 : else if (sjinfo->jointype == JOIN_SEMI &&
5616 478 [ + - ]: 1361 : bms_equal(sjinfo->syn_righthand, rel1->relids) &&
479 : 145 : create_unique_path(root, rel1, rel1->cheapest_total_path,
480 : : sjinfo) != NULL)
481 : : {
482 : : /* Reversed semijoin case */
5622 483 [ + + ]: 145 : if (match_sjinfo)
484 : 39 : return false; /* invalid join path */
485 : 106 : match_sjinfo = sjinfo;
486 : 106 : reversed = true;
5383 487 : 106 : 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 : : */
3168 508 [ + + + + ]: 12841 : if (bms_overlap(rel1->relids, sjinfo->min_righthand) &&
509 : 3878 : bms_overlap(rel2->relids, sjinfo->min_righthand))
510 : 87 : 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 : : */
3174 518 [ + + + + ]: 16615 : if (sjinfo->jointype != JOIN_LEFT ||
3175 519 : 7739 : bms_overlap(joinrelids, sjinfo->min_lefthand))
520 : 5018 : 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 : : */
3174 528 : 3858 : 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 [ + + + + ]: 137809 : if (must_be_leftjoin &&
542 : 2440 : (match_sjinfo == NULL ||
543 [ + - ]: 2440 : match_sjinfo->jointype != JOIN_LEFT ||
544 [ - + ]: 2440 : !match_sjinfo->lhs_strict))
6015 545 : 724 : return false; /* invalid join path */
546 : :
547 : : /*
548 : : * We also have to check for constraints imposed by LATERAL references.
549 : : */
3047 550 [ + + ]: 137085 : 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 : 7304 : lateral_fwd = bms_overlap(rel1->relids, rel2->lateral_relids);
572 : 7304 : lateral_rev = bms_overlap(rel2->relids, rel1->lateral_relids);
573 [ + + + + ]: 7304 : if (lateral_fwd && lateral_rev)
574 : 9 : return false; /* have lateral refs in both directions */
575 [ + + ]: 7295 : if (lateral_fwd)
576 : : {
577 : : /* has to be implemented as nestloop with rel1 on left */
4249 578 [ + + + - ]: 4727 : if (match_sjinfo &&
3180 579 [ + + ]: 141 : (reversed ||
580 : 135 : unique_ified ||
581 [ - + ]: 135 : match_sjinfo->jointype == JOIN_FULL))
4249 582 : 6 : return false; /* not implementable as nestloop */
583 : : /* check there is a direct reference from rel2 to rel1 */
3047 584 [ + + ]: 4721 : if (!bms_overlap(rel1->relids, rel2->direct_lateral_relids))
585 : 21 : return false; /* only indirect refs, so reject */
586 : : /* check we won't have a dangerous PHV */
587 [ + + ]: 4700 : if (have_dangerous_phv(root, rel1->relids, rel2->lateral_relids))
588 : 36 : return false; /* might be unable to handle required PHV */
589 : : }
590 [ + + ]: 2568 : else if (lateral_rev)
591 : : {
592 : : /* has to be implemented as nestloop with rel2 on left */
4249 593 [ + + ]: 560 : if (match_sjinfo &&
3180 594 [ + - + - ]: 36 : (!reversed ||
595 : 36 : unique_ified ||
596 [ - + ]: 36 : match_sjinfo->jointype == JOIN_FULL))
4249 tgl@sss.pgh.pa.us 597 :UBC 0 : return false; /* not implementable as nestloop */
598 : : /* check there is a direct reference from rel1 to rel2 */
3047 tgl@sss.pgh.pa.us 599 [ - + ]:CBC 560 : if (!bms_overlap(rel2->relids, rel1->direct_lateral_relids))
3047 tgl@sss.pgh.pa.us 600 :UBC 0 : return false; /* only indirect refs, so reject */
601 : : /* check we won't have a dangerous PHV */
3047 tgl@sss.pgh.pa.us 602 [ + + ]:CBC 560 : if (have_dangerous_phv(root, rel2->relids, rel1->lateral_relids))
603 : 42 : 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 : 7190 : join_lateral_rels = min_join_parameterization(root, joinrelids,
620 : : rel1, rel2);
621 [ + + ]: 7190 : if (join_lateral_rels)
622 : : {
623 : 776 : Relids join_plus_rhs = bms_copy(joinrelids);
624 : : bool more;
625 : :
626 : : do
627 : : {
628 : 932 : more = false;
629 [ + + + + : 1643 : foreach(l, root->join_info_list)
+ + ]
630 : : {
631 : 711 : SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
632 : :
633 : : /* ignore full joins --- their ordering is predetermined */
1833 634 [ + + ]: 711 : if (sjinfo->jointype == JOIN_FULL)
635 : 9 : continue;
636 : :
3047 637 [ + + ]: 702 : if (bms_overlap(sjinfo->min_lefthand, join_plus_rhs) &&
638 [ + + ]: 591 : !bms_is_subset(sjinfo->min_righthand, join_plus_rhs))
639 : : {
640 : 201 : join_plus_rhs = bms_add_members(join_plus_rhs,
2489 641 : 201 : sjinfo->min_righthand);
3047 642 : 201 : more = true;
643 : : }
644 : : }
645 [ + + ]: 932 : } while (more);
646 [ + + ]: 776 : if (bms_overlap(join_plus_rhs, join_lateral_rels))
647 : 114 : return false; /* will not be able to join to some RHS rel */
648 : : }
649 : : }
650 : :
651 : : /* Otherwise, it's a valid join */
5722 652 : 136857 : *sjinfo_p = match_sjinfo;
653 : 136857 : *reversed_p = reversed;
6015 654 : 136857 : 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
20 amitlan@postgresql.o 669 :GNC 369634 : init_dummy_sjinfo(SpecialJoinInfo *sjinfo, Relids left_relids,
670 : : Relids right_relids)
671 : : {
672 : 369634 : sjinfo->type = T_SpecialJoinInfo;
673 : 369634 : sjinfo->min_lefthand = left_relids;
674 : 369634 : sjinfo->min_righthand = right_relids;
675 : 369634 : sjinfo->syn_lefthand = left_relids;
676 : 369634 : sjinfo->syn_righthand = right_relids;
677 : 369634 : sjinfo->jointype = JOIN_INNER;
678 : 369634 : sjinfo->ojrelid = 0;
679 : 369634 : sjinfo->commute_above_l = NULL;
680 : 369634 : sjinfo->commute_above_r = NULL;
681 : 369634 : sjinfo->commute_below_l = NULL;
682 : 369634 : sjinfo->commute_below_r = NULL;
683 : : /* we don't bother trying to make the remaining fields valid */
684 : 369634 : sjinfo->lhs_strict = false;
685 : 369634 : sjinfo->semi_can_btree = false;
686 : 369634 : sjinfo->semi_can_hash = false;
687 : 369634 : sjinfo->semi_operators = NIL;
688 : 369634 : sjinfo->semi_rhs_exprs = NIL;
689 : 369634 : }
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 *
6015 tgl@sss.pgh.pa.us 704 :CBC 142725 : make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
705 : : {
706 : : Relids joinrelids;
707 : : SpecialJoinInfo *sjinfo;
708 : : bool reversed;
333 709 : 142725 : 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. */
6015 715 [ - + ]: 142725 : Assert(!bms_overlap(rel1->relids, rel2->relids));
716 : :
717 : : /* Construct Relids set that identifies the joinrel (without OJ as yet). */
718 : 142725 : joinrelids = bms_union(rel1->relids, rel2->relids);
719 : :
720 : : /* Check validity and determine join type. */
5722 721 [ + + ]: 142725 : if (!join_is_legal(root, rel1, rel2, joinrelids,
722 : : &sjinfo, &reversed))
723 : : {
724 : : /* invalid join path */
6015 725 : 5973 : bms_free(joinrelids);
726 : 5973 : 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 : : */
333 733 : 136752 : joinrelids = add_outer_joins_to_relids(root, joinrelids, sjinfo,
734 : : &pushed_down_joins);
735 : :
736 : : /* Swap rels if needed to match the join info. */
5722 737 [ + + ]: 136752 : if (reversed)
738 : : {
739 : 3696 : RelOptInfo *trel = rel1;
740 : :
741 : 3696 : rel1 = rel2;
742 : 3696 : 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 [ + + ]: 136752 : if (sjinfo == NULL)
751 : : {
752 : 86446 : sjinfo = &sjinfo_data;
20 amitlan@postgresql.o 753 :GNC 86446 : 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 : : */
333 tgl@sss.pgh.pa.us 760 :CBC 136752 : 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 : : */
5865 768 [ + + ]: 136752 : if (is_dummy_rel(joinrel))
769 : : {
770 : 228 : bms_free(joinrelids);
771 : 228 : return joinrel;
772 : : }
773 : :
774 : : /* Add paths to the join relation. */
2588 rhaas@postgresql.org 775 : 136524 : populate_joinrel_with_paths(root, rel1, rel2, joinrel, sjinfo,
776 : : restrictlist);
777 : :
778 : 136523 : bms_free(joinrelids);
779 : :
780 : 136523 : 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
333 tgl@sss.pgh.pa.us 801 : 140168 : 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 [ + + + + ]: 140168 : if (sjinfo == NULL || sjinfo->ojrelid == 0)
807 : 93586 : 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 [ + + ]: 46582 : if (sjinfo->jointype != JOIN_LEFT)
816 : 1020 : 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 [ + + ]: 45562 : if (!bms_is_subset(sjinfo->commute_below_l, input_relids))
826 : 2308 : return input_relids;
827 : :
828 : : /* OK to add OJ's own relid */
829 : 43254 : 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 [ + + ]: 43254 : if (sjinfo->commute_above_l)
837 : : {
838 : 8214 : 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 [ + - + + : 30352 : foreach(lc, root->join_info_list)
+ + ]
849 : : {
850 : 22138 : SpecialJoinInfo *othersj = (SpecialJoinInfo *) lfirst(lc);
851 : :
852 [ + + ]: 22138 : if (othersj == sjinfo ||
853 [ + + - + ]: 13924 : othersj->ojrelid == 0 || othersj->jointype != JOIN_LEFT)
854 : 8220 : continue; /* definitely not interesting */
855 : :
856 [ + + ]: 13918 : if (!bms_is_member(othersj->ojrelid, commute_above_rels))
857 : 5650 : continue;
858 : :
859 : : /* Add it if not already present but conditions now satisfied */
860 [ + - + + ]: 16536 : if (!bms_is_member(othersj->ojrelid, input_relids) &&
861 [ + + ]: 16524 : bms_is_subset(othersj->min_lefthand, input_relids) &&
862 [ + + ]: 12420 : bms_is_subset(othersj->min_righthand, input_relids) &&
863 : 4164 : bms_is_subset(othersj->commute_below_l, input_relids))
864 : : {
865 : 4146 : input_relids = bms_add_member(input_relids, othersj->ojrelid);
866 : : /* report such pushed down outer joins, if asked */
867 [ + - ]: 4146 : if (pushed_down_joins != NULL)
868 : 4146 : *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 : 4146 : commute_above_rels = bms_add_members(commute_above_rels,
877 : 4146 : othersj->commute_above_l);
878 : : }
879 : : }
880 : : }
881 : :
882 : 43254 : 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
2588 rhaas@postgresql.org 893 : 139072 : 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 : : */
5722 tgl@sss.pgh.pa.us 915 [ + + + + : 139072 : switch (sjinfo->jointype)
+ - ]
916 : : {
8615 917 : 87254 : case JOIN_INNER:
5719 918 [ + + + + : 174499 : if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
+ + ]
2186 919 : 87245 : restriction_is_constant_false(restrictlist, joinrel, false))
920 : : {
5719 921 : 90 : mark_dummy_rel(joinrel);
5865 922 : 90 : break;
923 : : }
5722 924 : 87164 : add_paths_to_joinrel(root, joinrel, rel1, rel2,
925 : : JOIN_INNER, sjinfo,
926 : : restrictlist);
927 : 87163 : add_paths_to_joinrel(root, joinrel, rel2, rel1,
928 : : JOIN_INNER, sjinfo,
929 : : restrictlist);
8615 930 : 87163 : break;
931 : 46395 : case JOIN_LEFT:
4961 932 [ + + + + ]: 92763 : if (is_dummy_rel(rel1) ||
2186 933 : 46368 : restriction_is_constant_false(restrictlist, joinrel, true))
934 : : {
5719 935 : 43 : mark_dummy_rel(joinrel);
5865 936 : 43 : break;
937 : : }
2186 938 [ + + + + ]: 46442 : if (restriction_is_constant_false(restrictlist, joinrel, false) &&
5719 939 : 90 : bms_is_subset(rel2->relids, sjinfo->syn_righthand))
940 : 78 : mark_dummy_rel(rel2);
5722 941 : 46352 : add_paths_to_joinrel(root, joinrel, rel1, rel2,
942 : : JOIN_LEFT, sjinfo,
943 : : restrictlist);
944 : 46352 : add_paths_to_joinrel(root, joinrel, rel2, rel1,
945 : : JOIN_RIGHT, sjinfo,
946 : : restrictlist);
8615 947 : 46352 : break;
948 : 854 : case JOIN_FULL:
4961 949 [ - + - - : 1708 : if ((is_dummy_rel(rel1) && is_dummy_rel(rel2)) ||
+ + ]
2186 950 : 854 : restriction_is_constant_false(restrictlist, joinrel, true))
951 : : {
5719 tgl@sss.pgh.pa.us 952 :GBC 6 : mark_dummy_rel(joinrel);
5865 953 : 6 : break;
954 : : }
5722 tgl@sss.pgh.pa.us 955 :CBC 848 : add_paths_to_joinrel(root, joinrel, rel1, rel2,
956 : : JOIN_FULL, sjinfo,
957 : : restrictlist);
958 : 848 : 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 : : */
4854 969 [ - + ]: 848 : if (joinrel->pathlist == NIL)
4854 tgl@sss.pgh.pa.us 970 [ # # ]:UBC 0 : ereport(ERROR,
971 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
972 : : errmsg("FULL JOIN is only supported with merge-joinable or hash-joinable join conditions")));
7755 tgl@sss.pgh.pa.us 973 :CBC 848 : break;
5722 974 : 2310 : 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 : : */
5622 982 [ + + + - ]: 4471 : if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
983 : 2161 : bms_is_subset(sjinfo->min_righthand, rel2->relids))
984 : : {
985 [ + + + - : 4319 : if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
+ + ]
2186 986 : 2158 : restriction_is_constant_false(restrictlist, joinrel, false))
987 : : {
5622 988 : 6 : mark_dummy_rel(joinrel);
989 : 6 : break;
990 : : }
991 : 2155 : add_paths_to_joinrel(root, joinrel, rel1, rel2,
992 : : JOIN_SEMI, sjinfo,
993 : : restrictlist);
994 : : }
995 : :
996 : : /*
997 : : * If we know how to unique-ify the RHS and one input rel is
998 : : * exactly the RHS (not a superset) we can consider unique-ifying
999 : : * it and then doing a regular join. (The create_unique_path
1000 : : * check here is probably redundant with what join_is_legal did,
1001 : : * but if so the check is cheap because it's cached. So test
1002 : : * anyway to be sure.)
1003 : : */
5722 1004 [ + - + + ]: 4608 : if (bms_equal(sjinfo->syn_righthand, rel2->relids) &&
1005 : 2304 : create_unique_path(root, rel2, rel2->cheapest_total_path,
1006 : : sjinfo) != NULL)
1007 : : {
4961 1008 [ + - + - : 4494 : if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
- + ]
2186 1009 : 2247 : restriction_is_constant_false(restrictlist, joinrel, false))
1010 : : {
4961 tgl@sss.pgh.pa.us 1011 :UBC 0 : mark_dummy_rel(joinrel);
1012 : 0 : break;
1013 : : }
5722 tgl@sss.pgh.pa.us 1014 :CBC 2247 : add_paths_to_joinrel(root, joinrel, rel1, rel2,
1015 : : JOIN_UNIQUE_INNER, sjinfo,
1016 : : restrictlist);
1017 : 2247 : add_paths_to_joinrel(root, joinrel, rel2, rel1,
1018 : : JOIN_UNIQUE_OUTER, sjinfo,
1019 : : restrictlist);
1020 : : }
7755 1021 : 2304 : break;
5722 1022 : 2259 : case JOIN_ANTI:
4961 1023 [ + - - + ]: 4518 : if (is_dummy_rel(rel1) ||
2186 1024 : 2259 : restriction_is_constant_false(restrictlist, joinrel, true))
1025 : : {
5719 tgl@sss.pgh.pa.us 1026 :UBC 0 : mark_dummy_rel(joinrel);
5865 1027 : 0 : break;
1028 : : }
2186 tgl@sss.pgh.pa.us 1029 [ - + - - ]:CBC 2259 : if (restriction_is_constant_false(restrictlist, joinrel, false) &&
5719 tgl@sss.pgh.pa.us 1030 :UBC 0 : bms_is_subset(rel2->relids, sjinfo->syn_righthand))
1031 : 0 : mark_dummy_rel(rel2);
5722 tgl@sss.pgh.pa.us 1032 :CBC 2259 : add_paths_to_joinrel(root, joinrel, rel1, rel2,
1033 : : JOIN_ANTI, sjinfo,
1034 : : restrictlist);
375 1035 : 2259 : add_paths_to_joinrel(root, joinrel, rel2, rel1,
1036 : : JOIN_RIGHT_ANTI, sjinfo,
1037 : : restrictlist);
7755 1038 : 2259 : break;
8615 tgl@sss.pgh.pa.us 1039 :UBC 0 : default:
1040 : : /* other values not expected here */
5722 1041 [ # # ]: 0 : elog(ERROR, "unrecognized join type: %d", (int) sjinfo->jointype);
1042 : : break;
1043 : : }
1044 : :
1045 : : /* Apply partitionwise join technique, if possible. */
2249 peter_e@gmx.net 1046 :CBC 139071 : try_partitionwise_join(root, rel1, rel2, joinrel, sjinfo, restrictlist);
10141 scrappy@hub.org 1047 : 139071 : }
1048 : :
1049 : :
1050 : : /*
1051 : : * have_join_order_restriction
1052 : : * Detect whether the two relations should be joined to satisfy
1053 : : * a join-order restriction arising from special or lateral joins.
1054 : : *
1055 : : * In practice this is always used with have_relevant_joinclause(), and so
1056 : : * could be merged with that function, but it seems clearer to separate the
1057 : : * two concerns. We need this test because there are degenerate cases where
1058 : : * a clauseless join must be performed to satisfy join-order restrictions.
1059 : : * Also, if one rel has a lateral reference to the other, or both are needed
1060 : : * to compute some PHV, we should consider joining them even if the join would
1061 : : * be clauseless.
1062 : : *
1063 : : * Note: this is only a problem if one side of a degenerate outer join
1064 : : * contains multiple rels, or a clauseless join is required within an
1065 : : * IN/EXISTS RHS; else we will find a join path via the "last ditch" case in
1066 : : * join_search_one_level(). We could dispense with this test if we were
1067 : : * willing to try bushy plans in the "last ditch" case, but that seems much
1068 : : * less efficient.
1069 : : */
1070 : : bool
6267 tgl@sss.pgh.pa.us 1071 : 35360 : have_join_order_restriction(PlannerInfo *root,
1072 : : RelOptInfo *rel1, RelOptInfo *rel2)
1073 : : {
6015 1074 : 35360 : bool result = false;
1075 : : ListCell *l;
1076 : :
1077 : : /*
1078 : : * If either side has a direct lateral reference to the other, attempt the
1079 : : * join regardless of outer-join considerations.
1080 : : */
3047 1081 [ + + + + ]: 66292 : if (bms_overlap(rel1->relids, rel2->direct_lateral_relids) ||
1082 : 30932 : bms_overlap(rel2->relids, rel1->direct_lateral_relids))
1083 : 4814 : return true;
1084 : :
1085 : : /*
1086 : : * Likewise, if both rels are needed to compute some PlaceHolderVar,
1087 : : * attempt the join regardless of outer-join considerations. (This is not
1088 : : * very desirable, because a PHV with a large eval_at set will cause a lot
1089 : : * of probably-useless joins to be considered, but failing to do this can
1090 : : * cause us to fail to construct a plan at all.)
1091 : : */
1092 [ + + + + : 31346 : foreach(l, root->placeholder_list)
+ + ]
1093 : : {
1094 : 827 : PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
1095 : :
1096 [ + + + + ]: 1013 : if (bms_is_subset(rel1->relids, phinfo->ph_eval_at) &&
1097 : 186 : bms_is_subset(rel2->relids, phinfo->ph_eval_at))
1098 : 27 : return true;
1099 : : }
1100 : :
1101 : : /*
1102 : : * It's possible that the rels correspond to the left and right sides of a
1103 : : * degenerate outer join, that is, one with no joinclause mentioning the
1104 : : * non-nullable side; in which case we should force the join to occur.
1105 : : *
1106 : : * Also, the two rels could represent a clauseless join that has to be
1107 : : * completed to build up the LHS or RHS of an outer join.
1108 : : */
5722 1109 [ + + + + : 86195 : foreach(l, root->join_info_list)
+ + ]
1110 : : {
1111 : 56301 : SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
1112 : :
1113 : : /* ignore full joins --- other mechanisms handle them */
1114 [ + + ]: 56301 : if (sjinfo->jointype == JOIN_FULL)
6267 1115 : 21 : continue;
1116 : :
1117 : : /* Can we perform the SJ with these rels? */
5722 1118 [ + + + + ]: 70463 : if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
1119 : 14183 : bms_is_subset(sjinfo->min_righthand, rel2->relids))
1120 : : {
6015 1121 : 463 : result = true;
1122 : 463 : break;
1123 : : }
5722 1124 [ + + + + ]: 58077 : if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
1125 : 2260 : bms_is_subset(sjinfo->min_righthand, rel1->relids))
1126 : : {
6015 1127 : 87 : result = true;
1128 : 87 : break;
1129 : : }
1130 : :
1131 : : /*
1132 : : * Might we need to join these rels to complete the RHS? We have to
1133 : : * use "overlap" tests since either rel might include a lower SJ that
1134 : : * has been proven to commute with this one.
1135 : : */
5722 1136 [ + + + + ]: 69241 : if (bms_overlap(sjinfo->min_righthand, rel1->relids) &&
1137 : 13511 : bms_overlap(sjinfo->min_righthand, rel2->relids))
1138 : : {
6015 1139 : 60 : result = true;
1140 : 60 : break;
1141 : : }
1142 : :
1143 : : /* Likewise for the LHS. */
5722 1144 [ + + + + ]: 71060 : if (bms_overlap(sjinfo->min_lefthand, rel1->relids) &&
1145 : 15390 : bms_overlap(sjinfo->min_lefthand, rel2->relids))
1146 : : {
6015 1147 : 15 : result = true;
1148 : 15 : break;
1149 : : }
1150 : : }
1151 : :
1152 : : /*
1153 : : * We do not force the join to occur if either input rel can legally be
1154 : : * joined to anything else using joinclauses. This essentially means that
1155 : : * clauseless bushy joins are put off as long as possible. The reason is
1156 : : * that when there is a join order restriction high up in the join tree
1157 : : * (that is, with many rels inside the LHS or RHS), we would otherwise
1158 : : * expend lots of effort considering very stupid join combinations within
1159 : : * its LHS or RHS.
1160 : : */
1161 [ + + ]: 30519 : if (result)
1162 : : {
1163 [ + + + + ]: 1202 : if (has_legal_joinclause(root, rel1) ||
1164 : 577 : has_legal_joinclause(root, rel2))
1165 : 105 : result = false;
1166 : : }
1167 : :
1168 : 30519 : return result;
1169 : : }
1170 : :
1171 : :
1172 : : /*
1173 : : * has_join_restriction
1174 : : * Detect whether the specified relation has join-order restrictions,
1175 : : * due to being inside an outer join or an IN (sub-SELECT),
1176 : : * or participating in any LATERAL references or multi-rel PHVs.
1177 : : *
1178 : : * Essentially, this tests whether have_join_order_restriction() could
1179 : : * succeed with this rel and some other one. It's OK if we sometimes
1180 : : * say "true" incorrectly. (Therefore, we don't bother with the relatively
1181 : : * expensive has_legal_joinclause test.)
1182 : : */
1183 : : static bool
6267 1184 : 14425 : has_join_restriction(PlannerInfo *root, RelOptInfo *rel)
1185 : : {
1186 : : ListCell *l;
1187 : :
3047 1188 [ + + + + ]: 14425 : if (rel->lateral_relids != NULL || rel->lateral_referencers != NULL)
1189 : 8397 : return true;
1190 : :
1191 [ + + + + : 6358 : foreach(l, root->placeholder_list)
+ + ]
1192 : : {
1193 : 351 : PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
1194 : :
1195 [ + + ]: 351 : if (bms_is_subset(rel->relids, phinfo->ph_eval_at) &&
1196 [ + + ]: 75 : !bms_equal(rel->relids, phinfo->ph_eval_at))
4249 1197 : 21 : return true;
1198 : : }
1199 : :
5722 1200 [ + + + + : 6380 : foreach(l, root->join_info_list)
+ + ]
1201 : : {
1202 : 1290 : SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
1203 : :
1204 : : /* ignore full joins --- other mechanisms preserve their ordering */
1205 [ + + ]: 1290 : if (sjinfo->jointype == JOIN_FULL)
6267 1206 : 43 : continue;
1207 : :
1208 : : /* ignore if SJ is already contained in rel */
5722 1209 [ + + + + ]: 1920 : if (bms_is_subset(sjinfo->min_lefthand, rel->relids) &&
1210 : 673 : bms_is_subset(sjinfo->min_righthand, rel->relids))
6267 1211 : 165 : continue;
1212 : :
1213 : : /* restricted if it overlaps LHS or RHS, but doesn't contain SJ */
5722 1214 [ + + + + ]: 1644 : if (bms_overlap(sjinfo->min_lefthand, rel->relids) ||
1215 : 562 : bms_overlap(sjinfo->min_righthand, rel->relids))
6267 1216 : 917 : return true;
1217 : : }
1218 : :
1219 : 5090 : return false;
1220 : : }
1221 : :
1222 : :
1223 : : /*
1224 : : * has_legal_joinclause
1225 : : * Detect whether the specified relation can legally be joined
1226 : : * to any other rels using join clauses.
1227 : : *
1228 : : * We consider only joins to single other relations in the current
1229 : : * initial_rels list. This is sufficient to get a "true" result in most real
1230 : : * queries, and an occasional erroneous "false" will only cost a bit more
1231 : : * planning time. The reason for this limitation is that considering joins to
1232 : : * other joins would require proving that the other join rel can legally be
1233 : : * formed, which seems like too much trouble for something that's only a
1234 : : * heuristic to save planning time. (Note: we must look at initial_rels
1235 : : * and not all of the query, since when we are planning a sub-joinlist we
1236 : : * may be forced to make clauseless joins within initial_rels even though
1237 : : * there are join clauses linking to other parts of the query.)
1238 : : */
1239 : : static bool
6015 1240 : 1202 : has_legal_joinclause(PlannerInfo *root, RelOptInfo *rel)
1241 : : {
1242 : : ListCell *lc;
1243 : :
5938 1244 [ + - + + : 4494 : foreach(lc, root->initial_rels)
+ + ]
1245 : : {
1246 : 3397 : RelOptInfo *rel2 = (RelOptInfo *) lfirst(lc);
1247 : :
1248 : : /* ignore rels that are already in "rel" */
6015 1249 [ + + ]: 3397 : if (bms_overlap(rel->relids, rel2->relids))
1250 : 1424 : continue;
1251 : :
1252 [ + + ]: 1973 : if (have_relevant_joinclause(root, rel, rel2))
1253 : : {
1254 : : Relids joinrelids;
1255 : : SpecialJoinInfo *sjinfo;
1256 : : bool reversed;
1257 : :
1258 : : /* join_is_legal needs relids of the union */
1259 : 183 : joinrelids = bms_union(rel->relids, rel2->relids);
1260 : :
5722 1261 [ + + ]: 183 : if (join_is_legal(root, rel, rel2, joinrelids,
1262 : : &sjinfo, &reversed))
1263 : : {
1264 : : /* Yes, this will work */
6015 1265 : 105 : bms_free(joinrelids);
1266 : 105 : return true;
1267 : : }
1268 : :
1269 : 78 : bms_free(joinrelids);
1270 : : }
1271 : : }
1272 : :
1273 : 1097 : return false;
1274 : : }
1275 : :
1276 : :
1277 : : /*
1278 : : * There's a pitfall for creating parameterized nestloops: suppose the inner
1279 : : * rel (call it A) has a parameter that is a PlaceHolderVar, and that PHV's
1280 : : * minimum eval_at set includes the outer rel (B) and some third rel (C).
1281 : : * We might think we could create a B/A nestloop join that's parameterized by
1282 : : * C. But we would end up with a plan in which the PHV's expression has to be
1283 : : * evaluated as a nestloop parameter at the B/A join; and the executor is only
1284 : : * set up to handle simple Vars as NestLoopParams. Rather than add complexity
1285 : : * and overhead to the executor for such corner cases, it seems better to
1286 : : * forbid the join. (Note that we can still make use of A's parameterized
1287 : : * path with pre-joined B+C as the outer rel. have_join_order_restriction()
1288 : : * ensures that we will consider making such a join even if there are not
1289 : : * other reasons to do so.)
1290 : : *
1291 : : * So we check whether any PHVs used in the query could pose such a hazard.
1292 : : * We don't have any simple way of checking whether a risky PHV would actually
1293 : : * be used in the inner plan, and the case is so unusual that it doesn't seem
1294 : : * worth working very hard on it.
1295 : : *
1296 : : * This needs to be checked in two places. If the inner rel's minimum
1297 : : * parameterization would trigger the restriction, then join_is_legal() should
1298 : : * reject the join altogether, because there will be no workable paths for it.
1299 : : * But joinpath.c has to check again for every proposed nestloop path, because
1300 : : * the inner path might have more than the minimum parameterization, causing
1301 : : * some PHV to be dangerous for it that otherwise wouldn't be.
1302 : : */
1303 : : bool
3047 1304 : 21842 : have_dangerous_phv(PlannerInfo *root,
1305 : : Relids outer_relids, Relids inner_params)
1306 : : {
1307 : : ListCell *lc;
1308 : :
1309 [ + + + + : 23255 : foreach(lc, root->placeholder_list)
+ + ]
1310 : : {
1311 : 1539 : PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
1312 : :
1313 [ + + ]: 1539 : if (!bms_is_subset(phinfo->ph_eval_at, inner_params))
1314 : 1137 : continue; /* ignore, could not be a nestloop param */
1315 [ + + ]: 402 : if (!bms_overlap(phinfo->ph_eval_at, outer_relids))
1316 : 96 : continue; /* ignore, not relevant to this join */
1317 [ + + ]: 306 : if (bms_is_subset(phinfo->ph_eval_at, outer_relids))
1318 : 180 : continue; /* safe, it can be eval'd within outerrel */
1319 : : /* Otherwise, it's potentially unsafe, so reject the join */
1320 : 126 : return true;
1321 : : }
1322 : :
1323 : : /* OK to perform the join */
1324 : 21716 : return false;
1325 : : }
1326 : :
1327 : :
1328 : : /*
1329 : : * is_dummy_rel --- has relation been proven empty?
1330 : : */
1331 : : bool
5865 1332 : 1089993 : is_dummy_rel(RelOptInfo *rel)
1333 : : {
1334 : : Path *path;
1335 : :
1336 : : /*
1337 : : * A rel that is known dummy will have just one path that is a childless
1338 : : * Append. (Even if somehow it has more paths, a childless Append will
1339 : : * have cost zero and hence should be at the front of the pathlist.)
1340 : : */
1865 1341 [ + + ]: 1089993 : if (rel->pathlist == NIL)
1342 : 595763 : return false;
1343 : 494230 : path = (Path *) linitial(rel->pathlist);
1344 : :
1345 : : /*
1346 : : * Initially, a dummy path will just be a childless Append. But in later
1347 : : * planning stages we might stick a ProjectSetPath and/or ProjectionPath
1348 : : * on top, since Append can't project. Rather than make assumptions about
1349 : : * which combinations can occur, just descend through whatever we find.
1350 : : */
1351 : : for (;;)
1352 : : {
1353 [ + + ]: 509601 : if (IsA(path, ProjectionPath))
1354 : 13538 : path = ((ProjectionPath *) path)->subpath;
1355 [ + + ]: 496063 : else if (IsA(path, ProjectSetPath))
1356 : 1833 : path = ((ProjectSetPath *) path)->subpath;
1357 : : else
1358 : 494230 : break;
1359 : : }
1360 [ + + + + ]: 494230 : if (IS_DUMMY_APPEND(path))
1361 : 2235 : return true;
1362 : 491995 : return false;
1363 : : }
1364 : :
1365 : : /*
1366 : : * Mark a relation as proven empty.
1367 : : *
1368 : : * During GEQO planning, this can get invoked more than once on the same
1369 : : * baserel struct, so it's worth checking to see if the rel is already marked
1370 : : * dummy.
1371 : : *
1372 : : * Also, when called during GEQO join planning, we are in a short-lived
1373 : : * memory context. We must make sure that the dummy path attached to a
1374 : : * baserel survives the GEQO cycle, else the baserel is trashed for future
1375 : : * GEQO cycles. On the other hand, when we are marking a joinrel during GEQO,
1376 : : * we don't want the dummy path to clutter the main planning context. Upshot
1377 : : * is that the best solution is to explicitly make the dummy path in the same
1378 : : * context the given RelOptInfo is in.
1379 : : */
1380 : : void
5719 1381 : 268 : mark_dummy_rel(RelOptInfo *rel)
1382 : : {
1383 : : MemoryContext oldcontext;
1384 : :
1385 : : /* Already marked? */
4750 1386 [ + + ]: 268 : if (is_dummy_rel(rel))
1387 : 6 : return;
1388 : :
1389 : : /* No, so choose correct context to make the dummy path in */
1390 : 262 : oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
1391 : :
1392 : : /* Set dummy size estimate */
5865 1393 : 262 : rel->rows = 0;
1394 : :
1395 : : /* Evict any previously chosen paths */
1396 : 262 : rel->pathlist = NIL;
3007 rhaas@postgresql.org 1397 : 262 : rel->partial_pathlist = NIL;
1398 : :
1399 : : /* Set up the dummy path */
1858 tgl@sss.pgh.pa.us 1400 : 262 : add_path(rel, (Path *) create_append_path(NULL, rel, NIL, NIL,
1401 : : NIL, rel->lateral_relids,
1402 : : 0, false, -1));
1403 : :
1404 : : /* Set or update cheapest_total_path and related fields */
5719 1405 : 262 : set_cheapest(rel);
1406 : :
4750 1407 : 262 : MemoryContextSwitchTo(oldcontext);
1408 : : }
1409 : :
1410 : :
1411 : : /*
1412 : : * restriction_is_constant_false --- is a restrictlist just FALSE?
1413 : : *
1414 : : * In cases where a qual is provably constant FALSE, eval_const_expressions
1415 : : * will generally have thrown away anything that's ANDed with it. In outer
1416 : : * join situations this will leave us computing cartesian products only to
1417 : : * decide there's no match for an outer row, which is pretty stupid. So,
1418 : : * we need to detect the case.
1419 : : *
1420 : : * If only_pushed_down is true, then consider only quals that are pushed-down
1421 : : * from the point of view of the joinrel.
1422 : : */
1423 : : static bool
2186 1424 : 189742 : restriction_is_constant_false(List *restrictlist,
1425 : : RelOptInfo *joinrel,
1426 : : bool only_pushed_down)
1427 : : {
1428 : : ListCell *lc;
1429 : :
1430 : : /*
1431 : : * Despite the above comment, the restriction list we see here might
1432 : : * possibly have other members besides the FALSE constant, since other
1433 : : * quals could get "pushed down" to the outer join level. So we check
1434 : : * each member of the list.
1435 : : */
5719 1436 [ + + + + : 397444 : foreach(lc, restrictlist)
+ + ]
1437 : : {
2561 1438 : 207898 : RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
1439 : :
2186 1440 [ + + + + : 207898 : if (only_pushed_down && !RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
+ - ]
4961 1441 : 60037 : continue;
1442 : :
5719 1443 [ + - + + ]: 147861 : if (rinfo->clause && IsA(rinfo->clause, Const))
1444 : : {
5421 bruce@momjian.us 1445 : 2308 : Const *con = (Const *) rinfo->clause;
1446 : :
1447 : : /* constant NULL is as good as constant FALSE for our purposes */
5719 tgl@sss.pgh.pa.us 1448 [ + + ]: 2308 : if (con->constisnull)
1449 : 196 : return true;
1450 [ + + ]: 2254 : if (!DatumGetBool(con->constvalue))
1451 : 142 : return true;
1452 : : }
1453 : : }
1454 : 189546 : return false;
1455 : : }
1456 : :
1457 : : /*
1458 : : * Assess whether join between given two partitioned relations can be broken
1459 : : * down into joins between matching partitions; a technique called
1460 : : * "partitionwise join"
1461 : : *
1462 : : * Partitionwise join is possible when a. Joining relations have same
1463 : : * partitioning scheme b. There exists an equi-join between the partition keys
1464 : : * of the two relations.
1465 : : *
1466 : : * Partitionwise join is planned as follows (details: optimizer/README.)
1467 : : *
1468 : : * 1. Create the RelOptInfos for joins between matching partitions i.e
1469 : : * child-joins and add paths to them.
1470 : : *
1471 : : * 2. Construct Append or MergeAppend paths across the set of child joins.
1472 : : * This second phase is implemented by generate_partitionwise_join_paths().
1473 : : *
1474 : : * The RelOptInfo, SpecialJoinInfo and restrictlist for each child join are
1475 : : * obtained by translating the respective parent join structures.
1476 : : */
1477 : : static void
2249 peter_e@gmx.net 1478 : 139071 : try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
1479 : : RelOptInfo *joinrel, SpecialJoinInfo *parent_sjinfo,
1480 : : List *parent_restrictlist)
1481 : : {
1910 efujita@postgresql.o 1482 [ + + + + ]: 139071 : bool rel1_is_simple = IS_SIMPLE_REL(rel1);
1483 [ + + + + ]: 139071 : bool rel2_is_simple = IS_SIMPLE_REL(rel2);
1467 1484 : 139071 : List *parts1 = NIL;
1485 : 139071 : List *parts2 = NIL;
1486 : 139071 : ListCell *lcr1 = NULL;
1487 : 139071 : ListCell *lcr2 = NULL;
1488 : : int cnt_parts;
1489 : :
1490 : : /* Guard against stack overflow due to overly deep partition hierarchy. */
2382 rhaas@postgresql.org 1491 : 139071 : check_stack_depth();
1492 : :
1493 : : /* Nothing to do, if the join relation is not partitioned. */
1467 efujita@postgresql.o 1494 [ + + + + ]: 139071 : if (joinrel->part_scheme == NULL || joinrel->nparts == 0)
2382 rhaas@postgresql.org 1495 : 138104 : return;
1496 : :
1497 : : /* The join relation should have consider_partitionwise_join set. */
2053 efujita@postgresql.o 1498 [ - + ]: 1036 : Assert(joinrel->consider_partitionwise_join);
1499 : :
1500 : : /*
1501 : : * We can not perform partitionwise join if either of the joining
1502 : : * relations is not partitioned.
1503 : : */
1467 1504 [ + - + + : 1036 : if (!IS_PARTITIONED_REL(rel1) || !IS_PARTITIONED_REL(rel2))
+ + + - +
- + - + -
+ - + - -
+ ]
1505 : 9 : return;
1506 : :
2382 rhaas@postgresql.org 1507 [ + - + - : 1027 : Assert(REL_HAS_ALL_PART_PROPS(rel1) && REL_HAS_ALL_PART_PROPS(rel2));
+ - + - +
- + - + -
+ - + - +
- + - -
+ ]
1508 : :
1509 : : /* The joining relations should have consider_partitionwise_join set. */
2053 efujita@postgresql.o 1510 [ + - - + ]: 1027 : Assert(rel1->consider_partitionwise_join &&
1511 : : rel2->consider_partitionwise_join);
1512 : :
1513 : : /*
1514 : : * The partition scheme of the join relation should match that of the
1515 : : * joining relations.
1516 : : */
2382 rhaas@postgresql.org 1517 [ + - - + ]: 1027 : Assert(joinrel->part_scheme == rel1->part_scheme &&
1518 : : joinrel->part_scheme == rel2->part_scheme);
1519 : :
1467 efujita@postgresql.o 1520 [ + + - + ]: 1027 : Assert(!(joinrel->partbounds_merged && (joinrel->nparts <= 0)));
1521 : :
1522 : 1027 : compute_partition_bounds(root, rel1, rel2, joinrel, parent_sjinfo,
1523 : : &parts1, &parts2);
1524 : :
1525 [ + + ]: 1027 : if (joinrel->partbounds_merged)
1526 : : {
1527 : 384 : lcr1 = list_head(parts1);
1528 : 384 : lcr2 = list_head(parts2);
1529 : : }
1530 : :
1531 : : /*
1532 : : * Create child-join relations for this partitioned join, if those don't
1533 : : * exist. Add paths to child-joins for a pair of child relations
1534 : : * corresponding to the given pair of parent relations.
1535 : : */
1536 [ + + ]: 3601 : for (cnt_parts = 0; cnt_parts < joinrel->nparts; cnt_parts++)
1537 : : {
1538 : : RelOptInfo *child_rel1;
1539 : : RelOptInfo *child_rel2;
1540 : : bool rel1_empty;
1541 : : bool rel2_empty;
1542 : : SpecialJoinInfo *child_sjinfo;
1543 : : List *child_restrictlist;
1544 : : RelOptInfo *child_joinrel;
1545 : : AppendRelInfo **appinfos;
1546 : : int nappinfos;
1547 : :
1548 [ + + ]: 2634 : if (joinrel->partbounds_merged)
1549 : : {
1550 : 1005 : child_rel1 = lfirst_node(RelOptInfo, lcr1);
1551 : 1005 : child_rel2 = lfirst_node(RelOptInfo, lcr2);
1552 : 1005 : lcr1 = lnext(parts1, lcr1);
1553 : 1005 : lcr2 = lnext(parts2, lcr2);
1554 : : }
1555 : : else
1556 : : {
1557 : 1629 : child_rel1 = rel1->part_rels[cnt_parts];
1558 : 1629 : child_rel2 = rel2->part_rels[cnt_parts];
1559 : : }
1560 : :
1561 [ + + - + ]: 2634 : rel1_empty = (child_rel1 == NULL || IS_DUMMY_REL(child_rel1));
1562 [ + + - + ]: 2634 : rel2_empty = (child_rel2 == NULL || IS_DUMMY_REL(child_rel2));
1563 : :
1564 : : /*
1565 : : * Check for cases where we can prove that this segment of the join
1566 : : * returns no rows, due to one or both inputs being empty (including
1567 : : * inputs that have been pruned away entirely). If so just ignore it.
1568 : : * These rules are equivalent to populate_joinrel_with_paths's rules
1569 : : * for dummy input relations.
1570 : : */
1842 tgl@sss.pgh.pa.us 1571 [ + + + - ]: 2634 : switch (parent_sjinfo->jointype)
1572 : : {
1573 : 1231 : case JOIN_INNER:
1574 : : case JOIN_SEMI:
1575 [ + + + + ]: 1231 : if (rel1_empty || rel2_empty)
1576 : 26 : continue; /* ignore this join segment */
1577 : 1219 : break;
1578 : 1042 : case JOIN_LEFT:
1579 : : case JOIN_ANTI:
1580 [ + + ]: 1042 : if (rel1_empty)
1581 : 14 : continue; /* ignore this join segment */
1582 : 1028 : break;
1583 : 361 : case JOIN_FULL:
1584 [ + + - + ]: 361 : if (rel1_empty && rel2_empty)
1842 tgl@sss.pgh.pa.us 1585 :UBC 0 : continue; /* ignore this join segment */
1842 tgl@sss.pgh.pa.us 1586 :CBC 361 : break;
1842 tgl@sss.pgh.pa.us 1587 :UBC 0 : default:
1588 : : /* other values not expected here */
1589 [ # # ]: 0 : elog(ERROR, "unrecognized join type: %d",
1590 : : (int) parent_sjinfo->jointype);
1591 : : break;
1592 : : }
1593 : :
1594 : : /*
1595 : : * If a child has been pruned entirely then we can't generate paths
1596 : : * for it, so we have to reject partitionwise joining unless we were
1597 : : * able to eliminate this partition above.
1598 : : */
1842 tgl@sss.pgh.pa.us 1599 [ + + + + ]:CBC 2608 : if (child_rel1 == NULL || child_rel2 == NULL)
1600 : : {
1601 : : /*
1602 : : * Mark the joinrel as unpartitioned so that later functions treat
1603 : : * it correctly.
1604 : : */
1605 : 60 : joinrel->nparts = 0;
1606 : 60 : return;
1607 : : }
1608 : :
1609 : : /*
1610 : : * If a leaf relation has consider_partitionwise_join=false, it means
1611 : : * that it's a dummy relation for which we skipped setting up tlist
1612 : : * expressions and adding EC members in set_append_rel_size(), so
1613 : : * again we have to fail here.
1614 : : */
1910 efujita@postgresql.o 1615 [ + + - + ]: 2548 : if (rel1_is_simple && !child_rel1->consider_partitionwise_join)
1616 : : {
1910 efujita@postgresql.o 1617 [ # # ]:UBC 0 : Assert(child_rel1->reloptkind == RELOPT_OTHER_MEMBER_REL);
1618 [ # # ]: 0 : Assert(IS_DUMMY_REL(child_rel1));
1842 tgl@sss.pgh.pa.us 1619 : 0 : joinrel->nparts = 0;
1620 : 0 : return;
1621 : : }
1910 efujita@postgresql.o 1622 [ + + - + ]:CBC 2548 : if (rel2_is_simple && !child_rel2->consider_partitionwise_join)
1623 : : {
1910 efujita@postgresql.o 1624 [ # # ]:UBC 0 : Assert(child_rel2->reloptkind == RELOPT_OTHER_MEMBER_REL);
1625 [ # # ]: 0 : Assert(IS_DUMMY_REL(child_rel2));
1842 tgl@sss.pgh.pa.us 1626 : 0 : joinrel->nparts = 0;
1627 : 0 : return;
1628 : : }
1629 : :
1630 : : /* We should never try to join two overlapping sets of rels. */
2382 rhaas@postgresql.org 1631 [ - + ]:CBC 2548 : Assert(!bms_overlap(child_rel1->relids, child_rel2->relids));
1632 : :
1633 : : /*
1634 : : * Construct SpecialJoinInfo from parent join relations's
1635 : : * SpecialJoinInfo.
1636 : : */
1637 : 2548 : child_sjinfo = build_child_join_sjinfo(root, parent_sjinfo,
1638 : : child_rel1->relids,
1639 : : child_rel2->relids);
1640 : :
1641 : : /* Find the AppendRelInfo structures */
268 tgl@sss.pgh.pa.us 1642 : 2548 : appinfos = find_appinfos_by_relids(root,
1643 : 2548 : bms_union(child_rel1->relids,
1644 : 2548 : child_rel2->relids),
1645 : : &nappinfos);
1646 : :
1647 : : /*
1648 : : * Construct restrictions applicable to the child join from those
1649 : : * applicable to the parent join.
1650 : : */
1651 : : child_restrictlist =
2382 rhaas@postgresql.org 1652 : 2548 : (List *) adjust_appendrel_attrs(root,
1653 : : (Node *) parent_restrictlist,
1654 : : nappinfos, appinfos);
1655 : :
1656 : : /* Find or construct the child join's RelOptInfo */
1657 : 2548 : child_joinrel = joinrel->part_rels[cnt_parts];
1658 [ + + ]: 2548 : if (!child_joinrel)
1659 : : {
1660 : 2312 : child_joinrel = build_child_join_rel(root, child_rel1, child_rel2,
1661 : : joinrel, child_restrictlist,
1662 : : child_sjinfo);
1663 : 2312 : joinrel->part_rels[cnt_parts] = child_joinrel;
985 drowley@postgresql.o 1664 : 2312 : joinrel->live_parts = bms_add_member(joinrel->live_parts, cnt_parts);
1467 efujita@postgresql.o 1665 : 2312 : joinrel->all_partrels = bms_add_members(joinrel->all_partrels,
1666 : 2312 : child_joinrel->relids);
1667 : : }
1668 : :
1669 : : /* Assert we got the right one */
267 tgl@sss.pgh.pa.us 1670 [ - + ]: 2548 : Assert(bms_equal(child_joinrel->relids,
1671 : : adjust_child_relids(joinrel->relids,
1672 : : nappinfos, appinfos)));
1673 : :
1674 : : /* And make paths for the child join */
2382 rhaas@postgresql.org 1675 : 2548 : populate_joinrel_with_paths(root, child_rel1, child_rel2,
1676 : : child_joinrel, child_sjinfo,
1677 : : child_restrictlist);
1678 : :
267 tgl@sss.pgh.pa.us 1679 : 2548 : pfree(appinfos);
20 amitlan@postgresql.o 1680 :GNC 2548 : free_child_join_sjinfo(child_sjinfo);
1681 : : }
1682 : : }
1683 : :
1684 : : /*
1685 : : * Construct the SpecialJoinInfo for a child-join by translating
1686 : : * SpecialJoinInfo for the join between parents. left_relids and right_relids
1687 : : * are the relids of left and right side of the join respectively.
1688 : : *
1689 : : * If translations are added to or removed from this function, consider
1690 : : * updating free_child_join_sjinfo() accordingly.
1691 : : */
1692 : : static SpecialJoinInfo *
1915 alvherre@alvh.no-ip. 1693 :CBC 2548 : build_child_join_sjinfo(PlannerInfo *root, SpecialJoinInfo *parent_sjinfo,
1694 : : Relids left_relids, Relids right_relids)
1695 : : {
1696 : 2548 : SpecialJoinInfo *sjinfo = makeNode(SpecialJoinInfo);
1697 : : AppendRelInfo **left_appinfos;
1698 : : int left_nappinfos;
1699 : : AppendRelInfo **right_appinfos;
1700 : : int right_nappinfos;
1701 : :
1702 : : /* Dummy SpecialJoinInfos can be created without any translation. */
20 amitlan@postgresql.o 1703 [ + + ]:GNC 2548 : if (parent_sjinfo->jointype == JOIN_INNER)
1704 : : {
1705 [ - + ]: 1021 : Assert(parent_sjinfo->ojrelid == 0);
1706 : 1021 : init_dummy_sjinfo(sjinfo, left_relids, right_relids);
1707 : 1021 : return sjinfo;
1708 : : }
1709 : :
1915 alvherre@alvh.no-ip. 1710 :CBC 1527 : memcpy(sjinfo, parent_sjinfo, sizeof(SpecialJoinInfo));
1711 : 1527 : left_appinfos = find_appinfos_by_relids(root, left_relids,
1712 : : &left_nappinfos);
1713 : 1527 : right_appinfos = find_appinfos_by_relids(root, right_relids,
1714 : : &right_nappinfos);
1715 : :
1716 : 1527 : sjinfo->min_lefthand = adjust_child_relids(sjinfo->min_lefthand,
1717 : : left_nappinfos, left_appinfos);
1718 : 1527 : sjinfo->min_righthand = adjust_child_relids(sjinfo->min_righthand,
1719 : : right_nappinfos,
1720 : : right_appinfos);
1721 : 1527 : sjinfo->syn_lefthand = adjust_child_relids(sjinfo->syn_lefthand,
1722 : : left_nappinfos, left_appinfos);
1723 : 1527 : sjinfo->syn_righthand = adjust_child_relids(sjinfo->syn_righthand,
1724 : : right_nappinfos,
1725 : : right_appinfos);
1726 : : /* outer-join relids need no adjustment */
1727 : 3054 : sjinfo->semi_rhs_exprs = (List *) adjust_appendrel_attrs(root,
1728 : 1527 : (Node *) sjinfo->semi_rhs_exprs,
1729 : : right_nappinfos,
1730 : : right_appinfos);
1731 : :
1732 : 1527 : pfree(left_appinfos);
1733 : 1527 : pfree(right_appinfos);
1734 : :
1735 : 1527 : return sjinfo;
1736 : : }
1737 : :
1738 : : /*
1739 : : * free_child_join_sjinfo
1740 : : * Free memory consumed by a SpecialJoinInfo created by
1741 : : * build_child_join_sjinfo()
1742 : : *
1743 : : * Only members that are translated copies of their counterpart in the parent
1744 : : * SpecialJoinInfo are freed here.
1745 : : */
1746 : : static void
20 amitlan@postgresql.o 1747 :GNC 2548 : free_child_join_sjinfo(SpecialJoinInfo *sjinfo)
1748 : : {
1749 : : /*
1750 : : * Dummy SpecialJoinInfos of inner joins do not have any translated fields
1751 : : * and hence no fields that to be freed.
1752 : : */
1753 [ + + ]: 2548 : if (sjinfo->jointype != JOIN_INNER)
1754 : : {
1755 : 1527 : bms_free(sjinfo->min_lefthand);
1756 : 1527 : bms_free(sjinfo->min_righthand);
1757 : 1527 : bms_free(sjinfo->syn_lefthand);
1758 : 1527 : bms_free(sjinfo->syn_righthand);
1759 : :
1760 : : /*
1761 : : * semi_rhs_exprs may in principle be freed, but a simple pfree() does
1762 : : * not suffice, so we leave it alone.
1763 : : */
1764 : : }
1765 : :
1766 : 2548 : pfree(sjinfo);
1767 : 2548 : }
1768 : :
1769 : : /*
1770 : : * compute_partition_bounds
1771 : : * Compute the partition bounds for a join rel from those for inputs
1772 : : */
1773 : : static void
1467 efujita@postgresql.o 1774 :CBC 1027 : compute_partition_bounds(PlannerInfo *root, RelOptInfo *rel1,
1775 : : RelOptInfo *rel2, RelOptInfo *joinrel,
1776 : : SpecialJoinInfo *parent_sjinfo,
1777 : : List **parts1, List **parts2)
1778 : : {
1779 : : /*
1780 : : * If we don't have the partition bounds for the join rel yet, try to
1781 : : * compute those along with pairs of partitions to be joined.
1782 : : */
1783 [ + + ]: 1027 : if (joinrel->nparts == -1)
1784 : : {
1785 : 945 : PartitionScheme part_scheme = joinrel->part_scheme;
1786 : 945 : PartitionBoundInfo boundinfo = NULL;
1787 : 945 : int nparts = 0;
1788 : :
1789 [ - + ]: 945 : Assert(joinrel->boundinfo == NULL);
1790 [ - + ]: 945 : Assert(joinrel->part_rels == NULL);
1791 : :
1792 : : /*
1793 : : * See if the partition bounds for inputs are exactly the same, in
1794 : : * which case we don't need to work hard: the join rel will have the
1795 : : * same partition bounds as inputs, and the partitions with the same
1796 : : * cardinal positions will form the pairs.
1797 : : *
1798 : : * Note: even in cases where one or both inputs have merged bounds, it
1799 : : * would be possible for both the bounds to be exactly the same, but
1800 : : * it seems unlikely to be worth the cycles to check.
1801 : : */
1802 [ + + ]: 945 : if (!rel1->partbounds_merged &&
1803 [ + - ]: 915 : !rel2->partbounds_merged &&
1804 [ + + + + ]: 1701 : rel1->nparts == rel2->nparts &&
1805 : 786 : partition_bounds_equal(part_scheme->partnatts,
1806 : : part_scheme->parttyplen,
1807 : : part_scheme->parttypbyval,
1808 : : rel1->boundinfo, rel2->boundinfo))
1809 : : {
1810 : 522 : boundinfo = rel1->boundinfo;
1811 : 522 : nparts = rel1->nparts;
1812 : : }
1813 : : else
1814 : : {
1815 : : /* Try merging the partition bounds for inputs. */
1816 : 423 : boundinfo = partition_bounds_merge(part_scheme->partnatts,
1817 : 423 : part_scheme->partsupfunc,
1818 : : part_scheme->partcollation,
1819 : : rel1, rel2,
1820 : : parent_sjinfo->jointype,
1821 : : parts1, parts2);
1822 [ + + ]: 423 : if (boundinfo == NULL)
1823 : : {
1824 : 57 : joinrel->nparts = 0;
1825 : 57 : return;
1826 : : }
1827 : 366 : nparts = list_length(*parts1);
1828 : 366 : joinrel->partbounds_merged = true;
1829 : : }
1830 : :
1831 [ - + ]: 888 : Assert(nparts > 0);
1832 : 888 : joinrel->boundinfo = boundinfo;
1833 : 888 : joinrel->nparts = nparts;
1834 : 888 : joinrel->part_rels =
1835 : 888 : (RelOptInfo **) palloc0(sizeof(RelOptInfo *) * nparts);
1836 : : }
1837 : : else
1838 : : {
1839 [ - + ]: 82 : Assert(joinrel->nparts > 0);
1840 [ - + ]: 82 : Assert(joinrel->boundinfo);
1841 [ - + ]: 82 : Assert(joinrel->part_rels);
1842 : :
1843 : : /*
1844 : : * If the join rel's partbounds_merged flag is true, it means inputs
1845 : : * are not guaranteed to have the same partition bounds, therefore we
1846 : : * can't assume that the partitions at the same cardinal positions
1847 : : * form the pairs; let get_matching_part_pairs() generate the pairs.
1848 : : * Otherwise, nothing to do since we can assume that.
1849 : : */
1850 [ + + ]: 82 : if (joinrel->partbounds_merged)
1851 : : {
1852 : 18 : get_matching_part_pairs(root, joinrel, rel1, rel2,
1853 : : parts1, parts2);
1854 [ - + ]: 18 : Assert(list_length(*parts1) == joinrel->nparts);
1855 [ - + ]: 18 : Assert(list_length(*parts2) == joinrel->nparts);
1856 : : }
1857 : : }
1858 : : }
1859 : :
1860 : : /*
1861 : : * get_matching_part_pairs
1862 : : * Generate pairs of partitions to be joined from inputs
1863 : : */
1864 : : static void
1865 : 18 : get_matching_part_pairs(PlannerInfo *root, RelOptInfo *joinrel,
1866 : : RelOptInfo *rel1, RelOptInfo *rel2,
1867 : : List **parts1, List **parts2)
1868 : : {
1869 [ + - - + ]: 18 : bool rel1_is_simple = IS_SIMPLE_REL(rel1);
1870 [ - + - - ]: 18 : bool rel2_is_simple = IS_SIMPLE_REL(rel2);
1871 : : int cnt_parts;
1872 : :
1873 : 18 : *parts1 = NIL;
1874 : 18 : *parts2 = NIL;
1875 : :
1876 [ + + ]: 66 : for (cnt_parts = 0; cnt_parts < joinrel->nparts; cnt_parts++)
1877 : : {
1878 : 48 : RelOptInfo *child_joinrel = joinrel->part_rels[cnt_parts];
1879 : : RelOptInfo *child_rel1;
1880 : : RelOptInfo *child_rel2;
1881 : : Relids child_relids1;
1882 : : Relids child_relids2;
1883 : :
1884 : : /*
1885 : : * If this segment of the join is empty, it means that this segment
1886 : : * was ignored when previously creating child-join paths for it in
1887 : : * try_partitionwise_join() as it would not contribute to the join
1888 : : * result, due to one or both inputs being empty; add NULL to each of
1889 : : * the given lists so that this segment will be ignored again in that
1890 : : * function.
1891 : : */
1892 [ - + ]: 48 : if (!child_joinrel)
1893 : : {
1467 efujita@postgresql.o 1894 :UBC 0 : *parts1 = lappend(*parts1, NULL);
1895 : 0 : *parts2 = lappend(*parts2, NULL);
1896 : 0 : continue;
1897 : : }
1898 : :
1899 : : /*
1900 : : * Get a relids set of partition(s) involved in this join segment that
1901 : : * are from the rel1 side.
1902 : : */
1467 efujita@postgresql.o 1903 :CBC 48 : child_relids1 = bms_intersect(child_joinrel->relids,
1904 : 48 : rel1->all_partrels);
1905 [ - + ]: 48 : Assert(bms_num_members(child_relids1) == bms_num_members(rel1->relids));
1906 : :
1907 : : /*
1908 : : * Get a child rel for rel1 with the relids. Note that we should have
1909 : : * the child rel even if rel1 is a join rel, because in that case the
1910 : : * partitions specified in the relids would have matching/overlapping
1911 : : * boundaries, so the specified partitions should be considered as
1912 : : * ones to be joined when planning partitionwise joins of rel1,
1913 : : * meaning that the child rel would have been built by the time we get
1914 : : * here.
1915 : : */
1916 [ - + ]: 48 : if (rel1_is_simple)
1917 : : {
1467 efujita@postgresql.o 1918 :UBC 0 : int varno = bms_singleton_member(child_relids1);
1919 : :
1920 : 0 : child_rel1 = find_base_rel(root, varno);
1921 : : }
1922 : : else
1467 efujita@postgresql.o 1923 :CBC 48 : child_rel1 = find_join_rel(root, child_relids1);
1924 [ - + ]: 48 : Assert(child_rel1);
1925 : :
1926 : : /*
1927 : : * Get a relids set of partition(s) involved in this join segment that
1928 : : * are from the rel2 side.
1929 : : */
1930 : 48 : child_relids2 = bms_intersect(child_joinrel->relids,
1931 : 48 : rel2->all_partrels);
1932 [ - + ]: 48 : Assert(bms_num_members(child_relids2) == bms_num_members(rel2->relids));
1933 : :
1934 : : /*
1935 : : * Get a child rel for rel2 with the relids. See above comments.
1936 : : */
1937 [ + - ]: 48 : if (rel2_is_simple)
1938 : : {
1939 : 48 : int varno = bms_singleton_member(child_relids2);
1940 : :
1941 : 48 : child_rel2 = find_base_rel(root, varno);
1942 : : }
1943 : : else
1467 efujita@postgresql.o 1944 :UBC 0 : child_rel2 = find_join_rel(root, child_relids2);
1467 efujita@postgresql.o 1945 [ - + ]:CBC 48 : Assert(child_rel2);
1946 : :
1947 : : /*
1948 : : * The join of rel1 and rel2 is legal, so is the join of the child
1949 : : * rels obtained above; add them to the given lists as a join pair
1950 : : * producing this join segment.
1951 : : */
1952 : 48 : *parts1 = lappend(*parts1, child_rel1);
1953 : 48 : *parts2 = lappend(*parts2, child_rel2);
1954 : : }
1955 : 18 : }
|