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