PostgreSQL Source Code  git master
joinrels.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * joinrels.c
4  * Routines to determine which relations should be joined
5  *
6  * Portions Copyright (c) 1996-2017, 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 "catalog/partition.h"
19 #include "nodes/relation.h"
20 #include "optimizer/clauses.h"
21 #include "optimizer/joininfo.h"
22 #include "optimizer/pathnode.h"
23 #include "optimizer/paths.h"
24 #include "optimizer/prep.h"
25 #include "optimizer/cost.h"
26 #include "utils/memutils.h"
27 #include "utils/lsyscache.h"
28 
29 
30 static void make_rels_by_clause_joins(PlannerInfo *root,
31  RelOptInfo *old_rel,
32  ListCell *other_rels);
34  RelOptInfo *old_rel,
35  ListCell *other_rels);
36 static bool has_join_restriction(PlannerInfo *root, RelOptInfo *rel);
37 static bool has_legal_joinclause(PlannerInfo *root, RelOptInfo *rel);
38 static bool is_dummy_rel(RelOptInfo *rel);
39 static bool restriction_is_constant_false(List *restrictlist,
40  bool only_pushed_down);
41 static void populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1,
42  RelOptInfo *rel2, RelOptInfo *joinrel,
43  SpecialJoinInfo *sjinfo, List *restrictlist);
44 static void try_partition_wise_join(PlannerInfo *root, RelOptInfo *rel1,
45  RelOptInfo *rel2, RelOptInfo *joinrel,
46  SpecialJoinInfo *parent_sjinfo,
47  List *parent_restrictlist);
48 static int match_expr_to_partition_keys(Expr *expr, RelOptInfo *rel,
49  bool strict_op);
50 
51 
52 /*
53  * join_search_one_level
54  * Consider ways to produce join relations containing exactly 'level'
55  * jointree items. (This is one step of the dynamic-programming method
56  * embodied in standard_join_search.) Join rel nodes for each feasible
57  * combination of lower-level rels are created and returned in a list.
58  * Implementation paths are created for each such joinrel, too.
59  *
60  * level: level of rels we want to make this time
61  * root->join_rel_level[j], 1 <= j < level, is a list of rels containing j items
62  *
63  * The result is returned in root->join_rel_level[level].
64  */
65 void
67 {
68  List **joinrels = root->join_rel_level;
69  ListCell *r;
70  int k;
71 
72  Assert(joinrels[level] == NIL);
73 
74  /* Set join_cur_level so that new joinrels are added to proper list */
75  root->join_cur_level = level;
76 
77  /*
78  * First, consider left-sided and right-sided plans, in which rels of
79  * exactly level-1 member relations are joined against initial relations.
80  * We prefer to join using join clauses, but if we find a rel of level-1
81  * members that has no join clauses, we will generate Cartesian-product
82  * joins against all initial rels not already contained in it.
83  */
84  foreach(r, joinrels[level - 1])
85  {
86  RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
87 
88  if (old_rel->joininfo != NIL || old_rel->has_eclass_joins ||
89  has_join_restriction(root, old_rel))
90  {
91  /*
92  * There are join clauses or join order restrictions relevant to
93  * this rel, so consider joins between this rel and (only) those
94  * initial rels it is linked to by a clause or restriction.
95  *
96  * At level 2 this condition is symmetric, so there is no need to
97  * look at initial rels before this one in the list; we already
98  * considered such joins when we were at the earlier rel. (The
99  * mirror-image joins are handled automatically by make_join_rel.)
100  * In later passes (level > 2), we join rels of the previous level
101  * to each initial rel they don't already include but have a join
102  * clause or restriction with.
103  */
104  ListCell *other_rels;
105 
106  if (level == 2) /* consider remaining initial rels */
107  other_rels = lnext(r);
108  else /* consider all initial rels */
109  other_rels = list_head(joinrels[1]);
110 
112  old_rel,
113  other_rels);
114  }
115  else
116  {
117  /*
118  * Oops, we have a relation that is not joined to any other
119  * relation, either directly or by join-order restrictions.
120  * Cartesian product time.
121  *
122  * We consider a cartesian product with each not-already-included
123  * initial rel, whether it has other join clauses or not. At
124  * level 2, if there are two or more clauseless initial rels, we
125  * will redundantly consider joining them in both directions; but
126  * such cases aren't common enough to justify adding complexity to
127  * avoid the duplicated effort.
128  */
130  old_rel,
131  list_head(joinrels[1]));
132  }
133  }
134 
135  /*
136  * Now, consider "bushy plans" in which relations of k initial rels are
137  * joined to relations of level-k initial rels, for 2 <= k <= level-2.
138  *
139  * We only consider bushy-plan joins for pairs of rels where there is a
140  * suitable join clause (or join order restriction), in order to avoid
141  * unreasonable growth of planning time.
142  */
143  for (k = 2;; k++)
144  {
145  int other_level = level - k;
146 
147  /*
148  * Since make_join_rel(x, y) handles both x,y and y,x cases, we only
149  * need to go as far as the halfway point.
150  */
151  if (k > other_level)
152  break;
153 
154  foreach(r, joinrels[k])
155  {
156  RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
157  ListCell *other_rels;
158  ListCell *r2;
159 
160  /*
161  * We can ignore relations without join clauses here, unless they
162  * participate in join-order restrictions --- then we might have
163  * to force a bushy join plan.
164  */
165  if (old_rel->joininfo == NIL && !old_rel->has_eclass_joins &&
166  !has_join_restriction(root, old_rel))
167  continue;
168 
169  if (k == other_level)
170  other_rels = lnext(r); /* only consider remaining rels */
171  else
172  other_rels = list_head(joinrels[other_level]);
173 
174  for_each_cell(r2, other_rels)
175  {
176  RelOptInfo *new_rel = (RelOptInfo *) lfirst(r2);
177 
178  if (!bms_overlap(old_rel->relids, new_rel->relids))
179  {
180  /*
181  * OK, we can build a rel of the right level from this
182  * pair of rels. Do so if there is at least one relevant
183  * join clause or join order restriction.
184  */
185  if (have_relevant_joinclause(root, old_rel, new_rel) ||
186  have_join_order_restriction(root, old_rel, new_rel))
187  {
188  (void) make_join_rel(root, old_rel, new_rel);
189  }
190  }
191  }
192  }
193  }
194 
195  /*----------
196  * Last-ditch effort: if we failed to find any usable joins so far, force
197  * a set of cartesian-product joins to be generated. This handles the
198  * special case where all the available rels have join clauses but we
199  * cannot use any of those clauses yet. This can only happen when we are
200  * considering a join sub-problem (a sub-joinlist) and all the rels in the
201  * sub-problem have only join clauses with rels outside the sub-problem.
202  * An example is
203  *
204  * SELECT ... FROM a INNER JOIN b ON TRUE, c, d, ...
205  * WHERE a.w = c.x and b.y = d.z;
206  *
207  * If the "a INNER JOIN b" sub-problem does not get flattened into the
208  * upper level, we must be willing to make a cartesian join of a and b;
209  * but the code above will not have done so, because it thought that both
210  * a and b have joinclauses. We consider only left-sided and right-sided
211  * cartesian joins in this case (no bushy).
212  *----------
213  */
214  if (joinrels[level] == NIL)
215  {
216  /*
217  * This loop is just like the first one, except we always call
218  * make_rels_by_clauseless_joins().
219  */
220  foreach(r, joinrels[level - 1])
221  {
222  RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
223 
225  old_rel,
226  list_head(joinrels[1]));
227  }
228 
229  /*----------
230  * When special joins are involved, there may be no legal way
231  * to make an N-way join for some values of N. For example consider
232  *
233  * SELECT ... FROM t1 WHERE
234  * x IN (SELECT ... FROM t2,t3 WHERE ...) AND
235  * y IN (SELECT ... FROM t4,t5 WHERE ...)
236  *
237  * We will flatten this query to a 5-way join problem, but there are
238  * no 4-way joins that join_is_legal() will consider legal. We have
239  * to accept failure at level 4 and go on to discover a workable
240  * bushy plan at level 5.
241  *
242  * However, if there are no special joins and no lateral references
243  * then join_is_legal() should never fail, and so the following sanity
244  * check is useful.
245  *----------
246  */
247  if (joinrels[level] == NIL &&
248  root->join_info_list == NIL &&
249  !root->hasLateralRTEs)
250  elog(ERROR, "failed to build any %d-way joins", level);
251  }
252 }
253 
254 /*
255  * make_rels_by_clause_joins
256  * Build joins between the given relation 'old_rel' and other relations
257  * that participate in join clauses that 'old_rel' also participates in
258  * (or participate in join-order restrictions with it).
259  * The join rels are returned in root->join_rel_level[join_cur_level].
260  *
261  * Note: at levels above 2 we will generate the same joined relation in
262  * multiple ways --- for example (a join b) join c is the same RelOptInfo as
263  * (b join c) join a, though the second case will add a different set of Paths
264  * to it. This is the reason for using the join_rel_level mechanism, which
265  * automatically ensures that each new joinrel is only added to the list once.
266  *
267  * 'old_rel' is the relation entry for the relation to be joined
268  * 'other_rels': the first cell in a linked list containing the other
269  * rels to be considered for joining
270  *
271  * Currently, this is only used with initial rels in other_rels, but it
272  * will work for joining to joinrels too.
273  */
274 static void
276  RelOptInfo *old_rel,
277  ListCell *other_rels)
278 {
279  ListCell *l;
280 
281  for_each_cell(l, other_rels)
282  {
283  RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
284 
285  if (!bms_overlap(old_rel->relids, other_rel->relids) &&
286  (have_relevant_joinclause(root, old_rel, other_rel) ||
287  have_join_order_restriction(root, old_rel, other_rel)))
288  {
289  (void) make_join_rel(root, old_rel, other_rel);
290  }
291  }
292 }
293 
294 /*
295  * make_rels_by_clauseless_joins
296  * Given a relation 'old_rel' and a list of other relations
297  * 'other_rels', create a join relation between 'old_rel' and each
298  * member of 'other_rels' that isn't already included in 'old_rel'.
299  * The join rels are returned in root->join_rel_level[join_cur_level].
300  *
301  * 'old_rel' is the relation entry for the relation to be joined
302  * 'other_rels': the first cell of a linked list containing the
303  * other rels to be considered for joining
304  *
305  * Currently, this is only used with initial rels in other_rels, but it would
306  * work for joining to joinrels too.
307  */
308 static void
310  RelOptInfo *old_rel,
311  ListCell *other_rels)
312 {
313  ListCell *l;
314 
315  for_each_cell(l, other_rels)
316  {
317  RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
318 
319  if (!bms_overlap(other_rel->relids, old_rel->relids))
320  {
321  (void) make_join_rel(root, old_rel, other_rel);
322  }
323  }
324 }
325 
326 
327 /*
328  * join_is_legal
329  * Determine whether a proposed join is legal given the query's
330  * join order constraints; and if it is, determine the join type.
331  *
332  * Caller must supply not only the two rels, but the union of their relids.
333  * (We could simplify the API by computing joinrelids locally, but this
334  * would be redundant work in the normal path through make_join_rel.)
335  *
336  * On success, *sjinfo_p is set to NULL if this is to be a plain inner join,
337  * else it's set to point to the associated SpecialJoinInfo node. Also,
338  * *reversed_p is set true if the given relations need to be swapped to
339  * match the SpecialJoinInfo node.
340  */
341 static bool
343  Relids joinrelids,
344  SpecialJoinInfo **sjinfo_p, bool *reversed_p)
345 {
346  SpecialJoinInfo *match_sjinfo;
347  bool reversed;
348  bool unique_ified;
349  bool must_be_leftjoin;
350  ListCell *l;
351 
352  /*
353  * Ensure output params are set on failure return. This is just to
354  * suppress uninitialized-variable warnings from overly anal compilers.
355  */
356  *sjinfo_p = NULL;
357  *reversed_p = false;
358 
359  /*
360  * If we have any special joins, the proposed join might be illegal; and
361  * in any case we have to determine its join type. Scan the join info
362  * list for matches and conflicts.
363  */
364  match_sjinfo = NULL;
365  reversed = false;
366  unique_ified = false;
367  must_be_leftjoin = false;
368 
369  foreach(l, root->join_info_list)
370  {
371  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
372 
373  /*
374  * This special join is not relevant unless its RHS overlaps the
375  * proposed join. (Check this first as a fast path for dismissing
376  * most irrelevant SJs quickly.)
377  */
378  if (!bms_overlap(sjinfo->min_righthand, joinrelids))
379  continue;
380 
381  /*
382  * Also, not relevant if proposed join is fully contained within RHS
383  * (ie, we're still building up the RHS).
384  */
385  if (bms_is_subset(joinrelids, sjinfo->min_righthand))
386  continue;
387 
388  /*
389  * Also, not relevant if SJ is already done within either input.
390  */
391  if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
392  bms_is_subset(sjinfo->min_righthand, rel1->relids))
393  continue;
394  if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
395  bms_is_subset(sjinfo->min_righthand, rel2->relids))
396  continue;
397 
398  /*
399  * If it's a semijoin and we already joined the RHS to any other rels
400  * within either input, then we must have unique-ified the RHS at that
401  * point (see below). Therefore the semijoin is no longer relevant in
402  * this join path.
403  */
404  if (sjinfo->jointype == JOIN_SEMI)
405  {
406  if (bms_is_subset(sjinfo->syn_righthand, rel1->relids) &&
407  !bms_equal(sjinfo->syn_righthand, rel1->relids))
408  continue;
409  if (bms_is_subset(sjinfo->syn_righthand, rel2->relids) &&
410  !bms_equal(sjinfo->syn_righthand, rel2->relids))
411  continue;
412  }
413 
414  /*
415  * If one input contains min_lefthand and the other contains
416  * min_righthand, then we can perform the SJ at this join.
417  *
418  * Reject if we get matches to more than one SJ; that implies we're
419  * considering something that's not really valid.
420  */
421  if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
422  bms_is_subset(sjinfo->min_righthand, rel2->relids))
423  {
424  if (match_sjinfo)
425  return false; /* invalid join path */
426  match_sjinfo = sjinfo;
427  reversed = false;
428  }
429  else if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
430  bms_is_subset(sjinfo->min_righthand, rel1->relids))
431  {
432  if (match_sjinfo)
433  return false; /* invalid join path */
434  match_sjinfo = sjinfo;
435  reversed = true;
436  }
437  else if (sjinfo->jointype == JOIN_SEMI &&
438  bms_equal(sjinfo->syn_righthand, rel2->relids) &&
439  create_unique_path(root, rel2, rel2->cheapest_total_path,
440  sjinfo) != NULL)
441  {
442  /*----------
443  * For a semijoin, we can join the RHS to anything else by
444  * unique-ifying the RHS (if the RHS can be unique-ified).
445  * We will only get here if we have the full RHS but less
446  * than min_lefthand on the LHS.
447  *
448  * The reason to consider such a join path is exemplified by
449  * SELECT ... FROM a,b WHERE (a.x,b.y) IN (SELECT c1,c2 FROM c)
450  * If we insist on doing this as a semijoin we will first have
451  * to form the cartesian product of A*B. But if we unique-ify
452  * C then the semijoin becomes a plain innerjoin and we can join
453  * in any order, eg C to A and then to B. When C is much smaller
454  * than A and B this can be a huge win. So we allow C to be
455  * joined to just A or just B here, and then make_join_rel has
456  * to handle the case properly.
457  *
458  * Note that actually we'll allow unique-ified C to be joined to
459  * some other relation D here, too. That is legal, if usually not
460  * very sane, and this routine is only concerned with legality not
461  * with whether the join is good strategy.
462  *----------
463  */
464  if (match_sjinfo)
465  return false; /* invalid join path */
466  match_sjinfo = sjinfo;
467  reversed = false;
468  unique_ified = true;
469  }
470  else if (sjinfo->jointype == JOIN_SEMI &&
471  bms_equal(sjinfo->syn_righthand, rel1->relids) &&
472  create_unique_path(root, rel1, rel1->cheapest_total_path,
473  sjinfo) != NULL)
474  {
475  /* Reversed semijoin case */
476  if (match_sjinfo)
477  return false; /* invalid join path */
478  match_sjinfo = sjinfo;
479  reversed = true;
480  unique_ified = true;
481  }
482  else
483  {
484  /*
485  * Otherwise, the proposed join overlaps the RHS but isn't a valid
486  * implementation of this SJ. But don't panic quite yet: the RHS
487  * violation might have occurred previously, in one or both input
488  * relations, in which case we must have previously decided that
489  * it was OK to commute some other SJ with this one. If we need
490  * to perform this join to finish building up the RHS, rejecting
491  * it could lead to not finding any plan at all. (This can occur
492  * because of the heuristics elsewhere in this file that postpone
493  * clauseless joins: we might not consider doing a clauseless join
494  * within the RHS until after we've performed other, validly
495  * commutable SJs with one or both sides of the clauseless join.)
496  * This consideration boils down to the rule that if both inputs
497  * overlap the RHS, we can allow the join --- they are either
498  * fully within the RHS, or represent previously-allowed joins to
499  * rels outside it.
500  */
501  if (bms_overlap(rel1->relids, sjinfo->min_righthand) &&
502  bms_overlap(rel2->relids, sjinfo->min_righthand))
503  continue; /* assume valid previous violation of RHS */
504 
505  /*
506  * The proposed join could still be legal, but only if we're
507  * allowed to associate it into the RHS of this SJ. That means
508  * this SJ must be a LEFT join (not SEMI or ANTI, and certainly
509  * not FULL) and the proposed join must not overlap the LHS.
510  */
511  if (sjinfo->jointype != JOIN_LEFT ||
512  bms_overlap(joinrelids, sjinfo->min_lefthand))
513  return false; /* invalid join path */
514 
515  /*
516  * To be valid, the proposed join must be a LEFT join; otherwise
517  * it can't associate into this SJ's RHS. But we may not yet have
518  * found the SpecialJoinInfo matching the proposed join, so we
519  * can't test that yet. Remember the requirement for later.
520  */
521  must_be_leftjoin = true;
522  }
523  }
524 
525  /*
526  * Fail if violated any SJ's RHS and didn't match to a LEFT SJ: the
527  * proposed join can't associate into an SJ's RHS.
528  *
529  * Also, fail if the proposed join's predicate isn't strict; we're
530  * essentially checking to see if we can apply outer-join identity 3, and
531  * that's a requirement. (This check may be redundant with checks in
532  * make_outerjoininfo, but I'm not quite sure, and it's cheap to test.)
533  */
534  if (must_be_leftjoin &&
535  (match_sjinfo == NULL ||
536  match_sjinfo->jointype != JOIN_LEFT ||
537  !match_sjinfo->lhs_strict))
538  return false; /* invalid join path */
539 
540  /*
541  * We also have to check for constraints imposed by LATERAL references.
542  */
543  if (root->hasLateralRTEs)
544  {
545  bool lateral_fwd;
546  bool lateral_rev;
547  Relids join_lateral_rels;
548 
549  /*
550  * The proposed rels could each contain lateral references to the
551  * other, in which case the join is impossible. If there are lateral
552  * references in just one direction, then the join has to be done with
553  * a nestloop with the lateral referencer on the inside. If the join
554  * matches an SJ that cannot be implemented by such a nestloop, the
555  * join is impossible.
556  *
557  * Also, if the lateral reference is only indirect, we should reject
558  * the join; whatever rel(s) the reference chain goes through must be
559  * joined to first.
560  *
561  * Another case that might keep us from building a valid plan is the
562  * implementation restriction described by have_dangerous_phv().
563  */
564  lateral_fwd = bms_overlap(rel1->relids, rel2->lateral_relids);
565  lateral_rev = bms_overlap(rel2->relids, rel1->lateral_relids);
566  if (lateral_fwd && lateral_rev)
567  return false; /* have lateral refs in both directions */
568  if (lateral_fwd)
569  {
570  /* has to be implemented as nestloop with rel1 on left */
571  if (match_sjinfo &&
572  (reversed ||
573  unique_ified ||
574  match_sjinfo->jointype == JOIN_FULL))
575  return false; /* not implementable as nestloop */
576  /* check there is a direct reference from rel2 to rel1 */
577  if (!bms_overlap(rel1->relids, rel2->direct_lateral_relids))
578  return false; /* only indirect refs, so reject */
579  /* check we won't have a dangerous PHV */
580  if (have_dangerous_phv(root, rel1->relids, rel2->lateral_relids))
581  return false; /* might be unable to handle required PHV */
582  }
583  else if (lateral_rev)
584  {
585  /* has to be implemented as nestloop with rel2 on left */
586  if (match_sjinfo &&
587  (!reversed ||
588  unique_ified ||
589  match_sjinfo->jointype == JOIN_FULL))
590  return false; /* not implementable as nestloop */
591  /* check there is a direct reference from rel1 to rel2 */
592  if (!bms_overlap(rel2->relids, rel1->direct_lateral_relids))
593  return false; /* only indirect refs, so reject */
594  /* check we won't have a dangerous PHV */
595  if (have_dangerous_phv(root, rel2->relids, rel1->lateral_relids))
596  return false; /* might be unable to handle required PHV */
597  }
598 
599  /*
600  * LATERAL references could also cause problems later on if we accept
601  * this join: if the join's minimum parameterization includes any rels
602  * that would have to be on the inside of an outer join with this join
603  * rel, then it's never going to be possible to build the complete
604  * query using this join. We should reject this join not only because
605  * it'll save work, but because if we don't, the clauseless-join
606  * heuristics might think that legality of this join means that some
607  * other join rel need not be formed, and that could lead to failure
608  * to find any plan at all. We have to consider not only rels that
609  * are directly on the inner side of an OJ with the joinrel, but also
610  * ones that are indirectly so, so search to find all such rels.
611  */
612  join_lateral_rels = min_join_parameterization(root, joinrelids,
613  rel1, rel2);
614  if (join_lateral_rels)
615  {
616  Relids join_plus_rhs = bms_copy(joinrelids);
617  bool more;
618 
619  do
620  {
621  more = false;
622  foreach(l, root->join_info_list)
623  {
624  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
625 
626  if (bms_overlap(sjinfo->min_lefthand, join_plus_rhs) &&
627  !bms_is_subset(sjinfo->min_righthand, join_plus_rhs))
628  {
629  join_plus_rhs = bms_add_members(join_plus_rhs,
630  sjinfo->min_righthand);
631  more = true;
632  }
633  /* full joins constrain both sides symmetrically */
634  if (sjinfo->jointype == JOIN_FULL &&
635  bms_overlap(sjinfo->min_righthand, join_plus_rhs) &&
636  !bms_is_subset(sjinfo->min_lefthand, join_plus_rhs))
637  {
638  join_plus_rhs = bms_add_members(join_plus_rhs,
639  sjinfo->min_lefthand);
640  more = true;
641  }
642  }
643  } while (more);
644  if (bms_overlap(join_plus_rhs, join_lateral_rels))
645  return false; /* will not be able to join to some RHS rel */
646  }
647  }
648 
649  /* Otherwise, it's a valid join */
650  *sjinfo_p = match_sjinfo;
651  *reversed_p = reversed;
652  return true;
653 }
654 
655 
656 /*
657  * make_join_rel
658  * Find or create a join RelOptInfo that represents the join of
659  * the two given rels, and add to it path information for paths
660  * created with the two rels as outer and inner rel.
661  * (The join rel may already contain paths generated from other
662  * pairs of rels that add up to the same set of base rels.)
663  *
664  * NB: will return NULL if attempted join is not valid. This can happen
665  * when working with outer joins, or with IN or EXISTS clauses that have been
666  * turned into joins.
667  */
668 RelOptInfo *
670 {
671  Relids joinrelids;
672  SpecialJoinInfo *sjinfo;
673  bool reversed;
674  SpecialJoinInfo sjinfo_data;
675  RelOptInfo *joinrel;
676  List *restrictlist;
677 
678  /* We should never try to join two overlapping sets of rels. */
679  Assert(!bms_overlap(rel1->relids, rel2->relids));
680 
681  /* Construct Relids set that identifies the joinrel. */
682  joinrelids = bms_union(rel1->relids, rel2->relids);
683 
684  /* Check validity and determine join type. */
685  if (!join_is_legal(root, rel1, rel2, joinrelids,
686  &sjinfo, &reversed))
687  {
688  /* invalid join path */
689  bms_free(joinrelids);
690  return NULL;
691  }
692 
693  /* Swap rels if needed to match the join info. */
694  if (reversed)
695  {
696  RelOptInfo *trel = rel1;
697 
698  rel1 = rel2;
699  rel2 = trel;
700  }
701 
702  /*
703  * If it's a plain inner join, then we won't have found anything in
704  * join_info_list. Make up a SpecialJoinInfo so that selectivity
705  * estimation functions will know what's being joined.
706  */
707  if (sjinfo == NULL)
708  {
709  sjinfo = &sjinfo_data;
710  sjinfo->type = T_SpecialJoinInfo;
711  sjinfo->min_lefthand = rel1->relids;
712  sjinfo->min_righthand = rel2->relids;
713  sjinfo->syn_lefthand = rel1->relids;
714  sjinfo->syn_righthand = rel2->relids;
715  sjinfo->jointype = JOIN_INNER;
716  /* we don't bother trying to make the remaining fields valid */
717  sjinfo->lhs_strict = false;
718  sjinfo->delay_upper_joins = false;
719  sjinfo->semi_can_btree = false;
720  sjinfo->semi_can_hash = false;
721  sjinfo->semi_operators = NIL;
722  sjinfo->semi_rhs_exprs = NIL;
723  }
724 
725  /*
726  * Find or build the join RelOptInfo, and compute the restrictlist that
727  * goes with this particular joining.
728  */
729  joinrel = build_join_rel(root, joinrelids, rel1, rel2, sjinfo,
730  &restrictlist);
731 
732  /*
733  * If we've already proven this join is empty, we needn't consider any
734  * more paths for it.
735  */
736  if (is_dummy_rel(joinrel))
737  {
738  bms_free(joinrelids);
739  return joinrel;
740  }
741 
742  /* Add paths to the join relation. */
743  populate_joinrel_with_paths(root, rel1, rel2, joinrel, sjinfo,
744  restrictlist);
745 
746  bms_free(joinrelids);
747 
748  return joinrel;
749 }
750 
751 /*
752  * populate_joinrel_with_paths
753  * Add paths to the given joinrel for given pair of joining relations. The
754  * SpecialJoinInfo provides details about the join and the restrictlist
755  * contains the join clauses and the other clauses applicable for given pair
756  * of the joining relations.
757  */
758 static void
760  RelOptInfo *rel2, RelOptInfo *joinrel,
761  SpecialJoinInfo *sjinfo, List *restrictlist)
762 {
763  /*
764  * Consider paths using each rel as both outer and inner. Depending on
765  * the join type, a provably empty outer or inner rel might mean the join
766  * is provably empty too; in which case throw away any previously computed
767  * paths and mark the join as dummy. (We do it this way since it's
768  * conceivable that dummy-ness of a multi-element join might only be
769  * noticeable for certain construction paths.)
770  *
771  * Also, a provably constant-false join restriction typically means that
772  * we can skip evaluating one or both sides of the join. We do this by
773  * marking the appropriate rel as dummy. For outer joins, a
774  * constant-false restriction that is pushed down still means the whole
775  * join is dummy, while a non-pushed-down one means that no inner rows
776  * will join so we can treat the inner rel as dummy.
777  *
778  * We need only consider the jointypes that appear in join_info_list, plus
779  * JOIN_INNER.
780  */
781  switch (sjinfo->jointype)
782  {
783  case JOIN_INNER:
784  if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
785  restriction_is_constant_false(restrictlist, false))
786  {
787  mark_dummy_rel(joinrel);
788  break;
789  }
790  add_paths_to_joinrel(root, joinrel, rel1, rel2,
791  JOIN_INNER, sjinfo,
792  restrictlist);
793  add_paths_to_joinrel(root, joinrel, rel2, rel1,
794  JOIN_INNER, sjinfo,
795  restrictlist);
796  break;
797  case JOIN_LEFT:
798  if (is_dummy_rel(rel1) ||
799  restriction_is_constant_false(restrictlist, true))
800  {
801  mark_dummy_rel(joinrel);
802  break;
803  }
804  if (restriction_is_constant_false(restrictlist, false) &&
805  bms_is_subset(rel2->relids, sjinfo->syn_righthand))
806  mark_dummy_rel(rel2);
807  add_paths_to_joinrel(root, joinrel, rel1, rel2,
808  JOIN_LEFT, sjinfo,
809  restrictlist);
810  add_paths_to_joinrel(root, joinrel, rel2, rel1,
811  JOIN_RIGHT, sjinfo,
812  restrictlist);
813  break;
814  case JOIN_FULL:
815  if ((is_dummy_rel(rel1) && is_dummy_rel(rel2)) ||
816  restriction_is_constant_false(restrictlist, true))
817  {
818  mark_dummy_rel(joinrel);
819  break;
820  }
821  add_paths_to_joinrel(root, joinrel, rel1, rel2,
822  JOIN_FULL, sjinfo,
823  restrictlist);
824  add_paths_to_joinrel(root, joinrel, rel2, rel1,
825  JOIN_FULL, sjinfo,
826  restrictlist);
827 
828  /*
829  * If there are join quals that aren't mergeable or hashable, we
830  * may not be able to build any valid plan. Complain here so that
831  * we can give a somewhat-useful error message. (Since we have no
832  * flexibility of planning for a full join, there's no chance of
833  * succeeding later with another pair of input rels.)
834  */
835  if (joinrel->pathlist == NIL)
836  ereport(ERROR,
837  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
838  errmsg("FULL JOIN is only supported with merge-joinable or hash-joinable join conditions")));
839  break;
840  case JOIN_SEMI:
841 
842  /*
843  * We might have a normal semijoin, or a case where we don't have
844  * enough rels to do the semijoin but can unique-ify the RHS and
845  * then do an innerjoin (see comments in join_is_legal). In the
846  * latter case we can't apply JOIN_SEMI joining.
847  */
848  if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
849  bms_is_subset(sjinfo->min_righthand, rel2->relids))
850  {
851  if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
852  restriction_is_constant_false(restrictlist, false))
853  {
854  mark_dummy_rel(joinrel);
855  break;
856  }
857  add_paths_to_joinrel(root, joinrel, rel1, rel2,
858  JOIN_SEMI, sjinfo,
859  restrictlist);
860  }
861 
862  /*
863  * If we know how to unique-ify the RHS and one input rel is
864  * exactly the RHS (not a superset) we can consider unique-ifying
865  * it and then doing a regular join. (The create_unique_path
866  * check here is probably redundant with what join_is_legal did,
867  * but if so the check is cheap because it's cached. So test
868  * anyway to be sure.)
869  */
870  if (bms_equal(sjinfo->syn_righthand, rel2->relids) &&
871  create_unique_path(root, rel2, rel2->cheapest_total_path,
872  sjinfo) != NULL)
873  {
874  if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
875  restriction_is_constant_false(restrictlist, false))
876  {
877  mark_dummy_rel(joinrel);
878  break;
879  }
880  add_paths_to_joinrel(root, joinrel, rel1, rel2,
881  JOIN_UNIQUE_INNER, sjinfo,
882  restrictlist);
883  add_paths_to_joinrel(root, joinrel, rel2, rel1,
884  JOIN_UNIQUE_OUTER, sjinfo,
885  restrictlist);
886  }
887  break;
888  case JOIN_ANTI:
889  if (is_dummy_rel(rel1) ||
890  restriction_is_constant_false(restrictlist, true))
891  {
892  mark_dummy_rel(joinrel);
893  break;
894  }
895  if (restriction_is_constant_false(restrictlist, false) &&
896  bms_is_subset(rel2->relids, sjinfo->syn_righthand))
897  mark_dummy_rel(rel2);
898  add_paths_to_joinrel(root, joinrel, rel1, rel2,
899  JOIN_ANTI, sjinfo,
900  restrictlist);
901  break;
902  default:
903  /* other values not expected here */
904  elog(ERROR, "unrecognized join type: %d", (int) sjinfo->jointype);
905  break;
906  }
907 
908  /* Apply partition-wise join technique, if possible. */
909  try_partition_wise_join(root, rel1, rel2, joinrel, sjinfo, restrictlist);
910 }
911 
912 
913 /*
914  * have_join_order_restriction
915  * Detect whether the two relations should be joined to satisfy
916  * a join-order restriction arising from special or lateral joins.
917  *
918  * In practice this is always used with have_relevant_joinclause(), and so
919  * could be merged with that function, but it seems clearer to separate the
920  * two concerns. We need this test because there are degenerate cases where
921  * a clauseless join must be performed to satisfy join-order restrictions.
922  * Also, if one rel has a lateral reference to the other, or both are needed
923  * to compute some PHV, we should consider joining them even if the join would
924  * be clauseless.
925  *
926  * Note: this is only a problem if one side of a degenerate outer join
927  * contains multiple rels, or a clauseless join is required within an
928  * IN/EXISTS RHS; else we will find a join path via the "last ditch" case in
929  * join_search_one_level(). We could dispense with this test if we were
930  * willing to try bushy plans in the "last ditch" case, but that seems much
931  * less efficient.
932  */
933 bool
935  RelOptInfo *rel1, RelOptInfo *rel2)
936 {
937  bool result = false;
938  ListCell *l;
939 
940  /*
941  * If either side has a direct lateral reference to the other, attempt the
942  * join regardless of outer-join considerations.
943  */
944  if (bms_overlap(rel1->relids, rel2->direct_lateral_relids) ||
946  return true;
947 
948  /*
949  * Likewise, if both rels are needed to compute some PlaceHolderVar,
950  * attempt the join regardless of outer-join considerations. (This is not
951  * very desirable, because a PHV with a large eval_at set will cause a lot
952  * of probably-useless joins to be considered, but failing to do this can
953  * cause us to fail to construct a plan at all.)
954  */
955  foreach(l, root->placeholder_list)
956  {
957  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
958 
959  if (bms_is_subset(rel1->relids, phinfo->ph_eval_at) &&
960  bms_is_subset(rel2->relids, phinfo->ph_eval_at))
961  return true;
962  }
963 
964  /*
965  * It's possible that the rels correspond to the left and right sides of a
966  * degenerate outer join, that is, one with no joinclause mentioning the
967  * non-nullable side; in which case we should force the join to occur.
968  *
969  * Also, the two rels could represent a clauseless join that has to be
970  * completed to build up the LHS or RHS of an outer join.
971  */
972  foreach(l, root->join_info_list)
973  {
974  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
975 
976  /* ignore full joins --- other mechanisms handle them */
977  if (sjinfo->jointype == JOIN_FULL)
978  continue;
979 
980  /* Can we perform the SJ with these rels? */
981  if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
982  bms_is_subset(sjinfo->min_righthand, rel2->relids))
983  {
984  result = true;
985  break;
986  }
987  if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
988  bms_is_subset(sjinfo->min_righthand, rel1->relids))
989  {
990  result = true;
991  break;
992  }
993 
994  /*
995  * Might we need to join these rels to complete the RHS? We have to
996  * use "overlap" tests since either rel might include a lower SJ that
997  * has been proven to commute with this one.
998  */
999  if (bms_overlap(sjinfo->min_righthand, rel1->relids) &&
1000  bms_overlap(sjinfo->min_righthand, rel2->relids))
1001  {
1002  result = true;
1003  break;
1004  }
1005 
1006  /* Likewise for the LHS. */
1007  if (bms_overlap(sjinfo->min_lefthand, rel1->relids) &&
1008  bms_overlap(sjinfo->min_lefthand, rel2->relids))
1009  {
1010  result = true;
1011  break;
1012  }
1013  }
1014 
1015  /*
1016  * We do not force the join to occur if either input rel can legally be
1017  * joined to anything else using joinclauses. This essentially means that
1018  * clauseless bushy joins are put off as long as possible. The reason is
1019  * that when there is a join order restriction high up in the join tree
1020  * (that is, with many rels inside the LHS or RHS), we would otherwise
1021  * expend lots of effort considering very stupid join combinations within
1022  * its LHS or RHS.
1023  */
1024  if (result)
1025  {
1026  if (has_legal_joinclause(root, rel1) ||
1027  has_legal_joinclause(root, rel2))
1028  result = false;
1029  }
1030 
1031  return result;
1032 }
1033 
1034 
1035 /*
1036  * has_join_restriction
1037  * Detect whether the specified relation has join-order restrictions,
1038  * due to being inside an outer join or an IN (sub-SELECT),
1039  * or participating in any LATERAL references or multi-rel PHVs.
1040  *
1041  * Essentially, this tests whether have_join_order_restriction() could
1042  * succeed with this rel and some other one. It's OK if we sometimes
1043  * say "true" incorrectly. (Therefore, we don't bother with the relatively
1044  * expensive has_legal_joinclause test.)
1045  */
1046 static bool
1048 {
1049  ListCell *l;
1050 
1051  if (rel->lateral_relids != NULL || rel->lateral_referencers != NULL)
1052  return true;
1053 
1054  foreach(l, root->placeholder_list)
1055  {
1056  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
1057 
1058  if (bms_is_subset(rel->relids, phinfo->ph_eval_at) &&
1059  !bms_equal(rel->relids, phinfo->ph_eval_at))
1060  return true;
1061  }
1062 
1063  foreach(l, root->join_info_list)
1064  {
1065  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
1066 
1067  /* ignore full joins --- other mechanisms preserve their ordering */
1068  if (sjinfo->jointype == JOIN_FULL)
1069  continue;
1070 
1071  /* ignore if SJ is already contained in rel */
1072  if (bms_is_subset(sjinfo->min_lefthand, rel->relids) &&
1073  bms_is_subset(sjinfo->min_righthand, rel->relids))
1074  continue;
1075 
1076  /* restricted if it overlaps LHS or RHS, but doesn't contain SJ */
1077  if (bms_overlap(sjinfo->min_lefthand, rel->relids) ||
1078  bms_overlap(sjinfo->min_righthand, rel->relids))
1079  return true;
1080  }
1081 
1082  return false;
1083 }
1084 
1085 
1086 /*
1087  * has_legal_joinclause
1088  * Detect whether the specified relation can legally be joined
1089  * to any other rels using join clauses.
1090  *
1091  * We consider only joins to single other relations in the current
1092  * initial_rels list. This is sufficient to get a "true" result in most real
1093  * queries, and an occasional erroneous "false" will only cost a bit more
1094  * planning time. The reason for this limitation is that considering joins to
1095  * other joins would require proving that the other join rel can legally be
1096  * formed, which seems like too much trouble for something that's only a
1097  * heuristic to save planning time. (Note: we must look at initial_rels
1098  * and not all of the query, since when we are planning a sub-joinlist we
1099  * may be forced to make clauseless joins within initial_rels even though
1100  * there are join clauses linking to other parts of the query.)
1101  */
1102 static bool
1104 {
1105  ListCell *lc;
1106 
1107  foreach(lc, root->initial_rels)
1108  {
1109  RelOptInfo *rel2 = (RelOptInfo *) lfirst(lc);
1110 
1111  /* ignore rels that are already in "rel" */
1112  if (bms_overlap(rel->relids, rel2->relids))
1113  continue;
1114 
1115  if (have_relevant_joinclause(root, rel, rel2))
1116  {
1117  Relids joinrelids;
1118  SpecialJoinInfo *sjinfo;
1119  bool reversed;
1120 
1121  /* join_is_legal needs relids of the union */
1122  joinrelids = bms_union(rel->relids, rel2->relids);
1123 
1124  if (join_is_legal(root, rel, rel2, joinrelids,
1125  &sjinfo, &reversed))
1126  {
1127  /* Yes, this will work */
1128  bms_free(joinrelids);
1129  return true;
1130  }
1131 
1132  bms_free(joinrelids);
1133  }
1134  }
1135 
1136  return false;
1137 }
1138 
1139 
1140 /*
1141  * There's a pitfall for creating parameterized nestloops: suppose the inner
1142  * rel (call it A) has a parameter that is a PlaceHolderVar, and that PHV's
1143  * minimum eval_at set includes the outer rel (B) and some third rel (C).
1144  * We might think we could create a B/A nestloop join that's parameterized by
1145  * C. But we would end up with a plan in which the PHV's expression has to be
1146  * evaluated as a nestloop parameter at the B/A join; and the executor is only
1147  * set up to handle simple Vars as NestLoopParams. Rather than add complexity
1148  * and overhead to the executor for such corner cases, it seems better to
1149  * forbid the join. (Note that we can still make use of A's parameterized
1150  * path with pre-joined B+C as the outer rel. have_join_order_restriction()
1151  * ensures that we will consider making such a join even if there are not
1152  * other reasons to do so.)
1153  *
1154  * So we check whether any PHVs used in the query could pose such a hazard.
1155  * We don't have any simple way of checking whether a risky PHV would actually
1156  * be used in the inner plan, and the case is so unusual that it doesn't seem
1157  * worth working very hard on it.
1158  *
1159  * This needs to be checked in two places. If the inner rel's minimum
1160  * parameterization would trigger the restriction, then join_is_legal() should
1161  * reject the join altogether, because there will be no workable paths for it.
1162  * But joinpath.c has to check again for every proposed nestloop path, because
1163  * the inner path might have more than the minimum parameterization, causing
1164  * some PHV to be dangerous for it that otherwise wouldn't be.
1165  */
1166 bool
1168  Relids outer_relids, Relids inner_params)
1169 {
1170  ListCell *lc;
1171 
1172  foreach(lc, root->placeholder_list)
1173  {
1174  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
1175 
1176  if (!bms_is_subset(phinfo->ph_eval_at, inner_params))
1177  continue; /* ignore, could not be a nestloop param */
1178  if (!bms_overlap(phinfo->ph_eval_at, outer_relids))
1179  continue; /* ignore, not relevant to this join */
1180  if (bms_is_subset(phinfo->ph_eval_at, outer_relids))
1181  continue; /* safe, it can be eval'd within outerrel */
1182  /* Otherwise, it's potentially unsafe, so reject the join */
1183  return true;
1184  }
1185 
1186  /* OK to perform the join */
1187  return false;
1188 }
1189 
1190 
1191 /*
1192  * is_dummy_rel --- has relation been proven empty?
1193  */
1194 static bool
1196 {
1197  return IS_DUMMY_REL(rel);
1198 }
1199 
1200 /*
1201  * Mark a relation as proven empty.
1202  *
1203  * During GEQO planning, this can get invoked more than once on the same
1204  * baserel struct, so it's worth checking to see if the rel is already marked
1205  * dummy.
1206  *
1207  * Also, when called during GEQO join planning, we are in a short-lived
1208  * memory context. We must make sure that the dummy path attached to a
1209  * baserel survives the GEQO cycle, else the baserel is trashed for future
1210  * GEQO cycles. On the other hand, when we are marking a joinrel during GEQO,
1211  * we don't want the dummy path to clutter the main planning context. Upshot
1212  * is that the best solution is to explicitly make the dummy path in the same
1213  * context the given RelOptInfo is in.
1214  */
1215 void
1217 {
1218  MemoryContext oldcontext;
1219 
1220  /* Already marked? */
1221  if (is_dummy_rel(rel))
1222  return;
1223 
1224  /* No, so choose correct context to make the dummy path in */
1225  oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
1226 
1227  /* Set dummy size estimate */
1228  rel->rows = 0;
1229 
1230  /* Evict any previously chosen paths */
1231  rel->pathlist = NIL;
1232  rel->partial_pathlist = NIL;
1233 
1234  /* Set up the dummy path */
1235  add_path(rel, (Path *) create_append_path(rel, NIL, NULL, 0, NIL));
1236 
1237  /* Set or update cheapest_total_path and related fields */
1238  set_cheapest(rel);
1239 
1240  MemoryContextSwitchTo(oldcontext);
1241 }
1242 
1243 
1244 /*
1245  * restriction_is_constant_false --- is a restrictlist just FALSE?
1246  *
1247  * In cases where a qual is provably constant FALSE, eval_const_expressions
1248  * will generally have thrown away anything that's ANDed with it. In outer
1249  * join situations this will leave us computing cartesian products only to
1250  * decide there's no match for an outer row, which is pretty stupid. So,
1251  * we need to detect the case.
1252  *
1253  * If only_pushed_down is true, then consider only pushed-down quals.
1254  */
1255 static bool
1256 restriction_is_constant_false(List *restrictlist, bool only_pushed_down)
1257 {
1258  ListCell *lc;
1259 
1260  /*
1261  * Despite the above comment, the restriction list we see here might
1262  * possibly have other members besides the FALSE constant, since other
1263  * quals could get "pushed down" to the outer join level. So we check
1264  * each member of the list.
1265  */
1266  foreach(lc, restrictlist)
1267  {
1268  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
1269 
1270  if (only_pushed_down && !rinfo->is_pushed_down)
1271  continue;
1272 
1273  if (rinfo->clause && IsA(rinfo->clause, Const))
1274  {
1275  Const *con = (Const *) rinfo->clause;
1276 
1277  /* constant NULL is as good as constant FALSE for our purposes */
1278  if (con->constisnull)
1279  return true;
1280  if (!DatumGetBool(con->constvalue))
1281  return true;
1282  }
1283  }
1284  return false;
1285 }
1286 
1287 /*
1288  * Assess whether join between given two partitioned relations can be broken
1289  * down into joins between matching partitions; a technique called
1290  * "partition-wise join"
1291  *
1292  * Partition-wise join is possible when a. Joining relations have same
1293  * partitioning scheme b. There exists an equi-join between the partition keys
1294  * of the two relations.
1295  *
1296  * Partition-wise join is planned as follows (details: optimizer/README.)
1297  *
1298  * 1. Create the RelOptInfos for joins between matching partitions i.e
1299  * child-joins and add paths to them.
1300  *
1301  * 2. Construct Append or MergeAppend paths across the set of child joins.
1302  * This second phase is implemented by generate_partition_wise_join_paths().
1303  *
1304  * The RelOptInfo, SpecialJoinInfo and restrictlist for each child join are
1305  * obtained by translating the respective parent join structures.
1306  */
1307 static void
1309  RelOptInfo *joinrel, SpecialJoinInfo *parent_sjinfo,
1310  List *parent_restrictlist)
1311 {
1312  int nparts;
1313  int cnt_parts;
1314 
1315  /* Guard against stack overflow due to overly deep partition hierarchy. */
1317 
1318  /* Nothing to do, if the join relation is not partitioned. */
1319  if (!IS_PARTITIONED_REL(joinrel))
1320  return;
1321 
1322  /*
1323  * set_rel_pathlist() may not create paths in children of an empty
1324  * partitioned table and so we can not add paths to child-joins. So, deem
1325  * such a join as unpartitioned. When a partitioned relation is deemed
1326  * empty because all its children are empty, dummy path will be set in
1327  * each of the children. In such a case we could still consider the join
1328  * as partitioned, but it might not help much.
1329  */
1330  if (IS_DUMMY_REL(rel1) || IS_DUMMY_REL(rel2))
1331  return;
1332 
1333  /*
1334  * Since this join relation is partitioned, all the base relations
1335  * participating in this join must be partitioned and so are all the
1336  * intermediate join relations.
1337  */
1340 
1341  /*
1342  * The partition scheme of the join relation should match that of the
1343  * joining relations.
1344  */
1345  Assert(joinrel->part_scheme == rel1->part_scheme &&
1346  joinrel->part_scheme == rel2->part_scheme);
1347 
1348  /*
1349  * Since we allow partition-wise join only when the partition bounds of
1350  * the joining relations exactly match, the partition bounds of the join
1351  * should match those of the joining relations.
1352  */
1354  joinrel->part_scheme->parttyplen,
1355  joinrel->part_scheme->parttypbyval,
1356  joinrel->boundinfo, rel1->boundinfo));
1358  joinrel->part_scheme->parttyplen,
1359  joinrel->part_scheme->parttypbyval,
1360  joinrel->boundinfo, rel2->boundinfo));
1361 
1362  nparts = joinrel->nparts;
1363 
1364  /* Allocate space to hold child-joins RelOptInfos, if not already done. */
1365  if (!joinrel->part_rels)
1366  joinrel->part_rels =
1367  (RelOptInfo **) palloc0(sizeof(RelOptInfo *) * nparts);
1368 
1369  /*
1370  * Create child-join relations for this partitioned join, if those don't
1371  * exist. Add paths to child-joins for a pair of child relations
1372  * corresponding to the given pair of parent relations.
1373  */
1374  for (cnt_parts = 0; cnt_parts < nparts; cnt_parts++)
1375  {
1376  RelOptInfo *child_rel1 = rel1->part_rels[cnt_parts];
1377  RelOptInfo *child_rel2 = rel2->part_rels[cnt_parts];
1378  SpecialJoinInfo *child_sjinfo;
1379  List *child_restrictlist;
1380  RelOptInfo *child_joinrel;
1381  Relids child_joinrelids;
1382  AppendRelInfo **appinfos;
1383  int nappinfos;
1384 
1385  /* We should never try to join two overlapping sets of rels. */
1386  Assert(!bms_overlap(child_rel1->relids, child_rel2->relids));
1387  child_joinrelids = bms_union(child_rel1->relids, child_rel2->relids);
1388  appinfos = find_appinfos_by_relids(root, child_joinrelids, &nappinfos);
1389 
1390  /*
1391  * Construct SpecialJoinInfo from parent join relations's
1392  * SpecialJoinInfo.
1393  */
1394  child_sjinfo = build_child_join_sjinfo(root, parent_sjinfo,
1395  child_rel1->relids,
1396  child_rel2->relids);
1397 
1398  /*
1399  * Construct restrictions applicable to the child join from those
1400  * applicable to the parent join.
1401  */
1402  child_restrictlist =
1403  (List *) adjust_appendrel_attrs(root,
1404  (Node *) parent_restrictlist,
1405  nappinfos, appinfos);
1406  pfree(appinfos);
1407 
1408  child_joinrel = joinrel->part_rels[cnt_parts];
1409  if (!child_joinrel)
1410  {
1411  child_joinrel = build_child_join_rel(root, child_rel1, child_rel2,
1412  joinrel, child_restrictlist,
1413  child_sjinfo,
1414  child_sjinfo->jointype);
1415  joinrel->part_rels[cnt_parts] = child_joinrel;
1416  }
1417 
1418  Assert(bms_equal(child_joinrel->relids, child_joinrelids));
1419 
1420  populate_joinrel_with_paths(root, child_rel1, child_rel2,
1421  child_joinrel, child_sjinfo,
1422  child_restrictlist);
1423  }
1424 }
1425 
1426 /*
1427  * Returns true if there exists an equi-join condition for each pair of
1428  * partition keys from given relations being joined.
1429  */
1430 bool
1432  List *restrictlist)
1433 {
1434  PartitionScheme part_scheme = rel1->part_scheme;
1435  ListCell *lc;
1436  int cnt_pks;
1437  bool pk_has_clause[PARTITION_MAX_KEYS];
1438  bool strict_op;
1439 
1440  /*
1441  * This function should be called when the joining relations have same
1442  * partitioning scheme.
1443  */
1444  Assert(rel1->part_scheme == rel2->part_scheme);
1445  Assert(part_scheme);
1446 
1447  memset(pk_has_clause, 0, sizeof(pk_has_clause));
1448  foreach(lc, restrictlist)
1449  {
1450  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
1451  OpExpr *opexpr;
1452  Expr *expr1;
1453  Expr *expr2;
1454  int ipk1;
1455  int ipk2;
1456 
1457  /* If processing an outer join, only use its own join clauses. */
1458  if (IS_OUTER_JOIN(jointype) && rinfo->is_pushed_down)
1459  continue;
1460 
1461  /* Skip clauses which can not be used for a join. */
1462  if (!rinfo->can_join)
1463  continue;
1464 
1465  /* Skip clauses which are not equality conditions. */
1466  if (!rinfo->mergeopfamilies && !OidIsValid(rinfo->hashjoinoperator))
1467  continue;
1468 
1469  opexpr = (OpExpr *) rinfo->clause;
1470  Assert(is_opclause(opexpr));
1471 
1472  /*
1473  * The equi-join between partition keys is strict if equi-join between
1474  * at least one partition key is using a strict operator. See
1475  * explanation about outer join reordering identity 3 in
1476  * optimizer/README
1477  */
1478  strict_op = op_strict(opexpr->opno);
1479 
1480  /* Match the operands to the relation. */
1481  if (bms_is_subset(rinfo->left_relids, rel1->relids) &&
1482  bms_is_subset(rinfo->right_relids, rel2->relids))
1483  {
1484  expr1 = linitial(opexpr->args);
1485  expr2 = lsecond(opexpr->args);
1486  }
1487  else if (bms_is_subset(rinfo->left_relids, rel2->relids) &&
1488  bms_is_subset(rinfo->right_relids, rel1->relids))
1489  {
1490  expr1 = lsecond(opexpr->args);
1491  expr2 = linitial(opexpr->args);
1492  }
1493  else
1494  continue;
1495 
1496  /*
1497  * Only clauses referencing the partition keys are useful for
1498  * partition-wise join.
1499  */
1500  ipk1 = match_expr_to_partition_keys(expr1, rel1, strict_op);
1501  if (ipk1 < 0)
1502  continue;
1503  ipk2 = match_expr_to_partition_keys(expr2, rel2, strict_op);
1504  if (ipk2 < 0)
1505  continue;
1506 
1507  /*
1508  * If the clause refers to keys at different ordinal positions, it can
1509  * not be used for partition-wise join.
1510  */
1511  if (ipk1 != ipk2)
1512  continue;
1513 
1514  /*
1515  * The clause allows partition-wise join if only it uses the same
1516  * operator family as that specified by the partition key.
1517  */
1519  {
1520  if (!op_in_opfamily(rinfo->hashjoinoperator,
1521  part_scheme->partopfamily[ipk1]))
1522  continue;
1523  }
1524  else if (!list_member_oid(rinfo->mergeopfamilies,
1525  part_scheme->partopfamily[ipk1]))
1526  continue;
1527 
1528  /* Mark the partition key as having an equi-join clause. */
1529  pk_has_clause[ipk1] = true;
1530  }
1531 
1532  /* Check whether every partition key has an equi-join condition. */
1533  for (cnt_pks = 0; cnt_pks < part_scheme->partnatts; cnt_pks++)
1534  {
1535  if (!pk_has_clause[cnt_pks])
1536  return false;
1537  }
1538 
1539  return true;
1540 }
1541 
1542 /*
1543  * Find the partition key from the given relation matching the given
1544  * expression. If found, return the index of the partition key, else return -1.
1545  */
1546 static int
1547 match_expr_to_partition_keys(Expr *expr, RelOptInfo *rel, bool strict_op)
1548 {
1549  int cnt;
1550 
1551  /* This function should be called only for partitioned relations. */
1552  Assert(rel->part_scheme);
1553 
1554  /* Remove any relabel decorations. */
1555  while (IsA(expr, RelabelType))
1556  expr = (Expr *) (castNode(RelabelType, expr))->arg;
1557 
1558  for (cnt = 0; cnt < rel->part_scheme->partnatts; cnt++)
1559  {
1560  ListCell *lc;
1561 
1562  Assert(rel->partexprs);
1563  foreach(lc, rel->partexprs[cnt])
1564  {
1565  if (equal(lfirst(lc), expr))
1566  return cnt;
1567  }
1568 
1569  if (!strict_op)
1570  continue;
1571 
1572  /*
1573  * If it's a strict equi-join a NULL partition key on one side will
1574  * not join a NULL partition key on the other side. So, rows with NULL
1575  * partition key from a partition on one side can not join with those
1576  * from a non-matching partition on the other side. So, search the
1577  * nullable partition keys as well.
1578  */
1579  Assert(rel->nullable_partexprs);
1580  foreach(lc, rel->nullable_partexprs[cnt])
1581  {
1582  if (equal(lfirst(lc), expr))
1583  return cnt;
1584  }
1585  }
1586 
1587  return -1;
1588 }
Datum constvalue
Definition: primnodes.h:196
bool has_eclass_joins
Definition: relation.h:651
SpecialJoinInfo * build_child_join_sjinfo(PlannerInfo *root, SpecialJoinInfo *parent_sjinfo, Relids left_relids, Relids right_relids)
Definition: prepunion.c:2472
#define NIL
Definition: pg_list.h:69
static bool is_dummy_rel(RelOptInfo *rel)
Definition: joinrels.c:1195
bool op_in_opfamily(Oid opno, Oid opfamily)
Definition: lsyscache.c:63
bool semi_can_btree
Definition: relation.h:2015
int join_cur_level
Definition: relation.h:226
bool op_strict(Oid opno)
Definition: lsyscache.c:1281
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:420
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:111
Relids ph_eval_at
Definition: relation.h:2154
static bool restriction_is_constant_false(List *restrictlist, bool only_pushed_down)
Definition: joinrels.c:1256
static void populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: joinrels.c:759
List * join_info_list
Definition: relation.h:250
Relids min_righthand
Definition: relation.h:2008
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2974
#define castNode(_type_, nodeptr)
Definition: nodes.h:579
#define IS_PARTITIONED_REL(rel)
Definition: relation.h:674
static void make_rels_by_clauseless_joins(PlannerInfo *root, RelOptInfo *old_rel, ListCell *other_rels)
Definition: joinrels.c:309
NodeTag type
Definition: relation.h:2006
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:723
bool have_join_order_restriction(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
Definition: joinrels.c:934
Definition: nodes.h:510
int errcode(int sqlerrcode)
Definition: elog.c:575
List * partial_pathlist
Definition: relation.h:601
#define PARTITION_MAX_KEYS
Relids left_relids
Definition: relation.h:1862
UniquePath * create_unique_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, SpecialJoinInfo *sjinfo)
Definition: pathnode.c:1440
List ** nullable_partexprs
Definition: relation.h:663
AppendPath * create_append_path(RelOptInfo *rel, List *subpaths, Relids required_outer, int parallel_workers, List *partitioned_rels)
Definition: pathnode.c:1211
#define OidIsValid(objectId)
Definition: c.h:576
List * mergeopfamilies
Definition: relation.h:1880
#define lsecond(l)
Definition: pg_list.h:116
Relids syn_lefthand
Definition: relation.h:2009
bool partition_bounds_equal(int partnatts, int16 *parttyplen, bool *parttypbyval, PartitionBoundInfo b1, PartitionBoundInfo b2)
Definition: partition.c:734
JoinType
Definition: nodes.h:674
Relids syn_righthand
Definition: relation.h:2010
bool have_partkey_equi_join(RelOptInfo *rel1, RelOptInfo *rel2, JoinType jointype, List *restrictlist)
Definition: joinrels.c:1431
static void make_rels_by_clause_joins(PlannerInfo *root, RelOptInfo *old_rel, ListCell *other_rels)
Definition: joinrels.c:275
Relids lateral_relids
Definition: relation.h:610
void pfree(void *pointer)
Definition: mcxt.c:949
List ** partexprs
Definition: relation.h:662
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
static bool has_legal_joinclause(PlannerInfo *root, RelOptInfo *rel)
Definition: joinrels.c:1103
#define is_opclause(clause)
Definition: clauses.h:20
static bool has_join_restriction(PlannerInfo *root, RelOptInfo *rel)
Definition: joinrels.c:1047
List * semi_rhs_exprs
Definition: relation.h:2018
bool semi_can_hash
Definition: relation.h:2016
bool hasLateralRTEs
Definition: relation.h:303
#define IS_DUMMY_REL(r)
Definition: relation.h:1275
Relids min_join_parameterization(PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:807
bool can_join
Definition: relation.h:1841
RelOptInfo * build_child_join_rel(PlannerInfo *root, RelOptInfo *outer_rel, RelOptInfo *inner_rel, RelOptInfo *parent_joinrel, List *restrictlist, SpecialJoinInfo *sjinfo, JoinType jointype)
Definition: relnode.c:682
int16 * parttyplen
Definition: relation.h:355
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
#define lfirst_node(type, lc)
Definition: pg_list.h:109
static bool join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, Relids joinrelids, SpecialJoinInfo **sjinfo_p, bool *reversed_p)
Definition: joinrels.c:342
struct Path * cheapest_total_path
Definition: relation.h:603
List * joininfo
Definition: relation.h:649
void check_stack_depth(void)
Definition: postgres.c:3150
int nparts
Definition: relation.h:658
#define DatumGetBool(X)
Definition: postgres.h:399
static void try_partition_wise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *parent_sjinfo, List *parent_restrictlist)
Definition: joinrels.c:1308
#define REL_HAS_ALL_PART_PROPS(rel)
Definition: relation.h:681
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
static int match_expr_to_partition_keys(Expr *expr, RelOptInfo *rel, bool strict_op)
Definition: joinrels.c:1547
Relids relids
Definition: relation.h:585
void join_search_one_level(PlannerInfo *root, int level)
Definition: joinrels.c:66
#define lnext(lc)
Definition: pg_list.h:105
#define ereport(elevel, rest)
Definition: elog.h:122
Relids lateral_referencers
Definition: relation.h:621
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: prepunion.c:1945
Expr * clause
Definition: relation.h:1835
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:242
RelOptInfo * make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
Definition: joinrels.c:669
Relids direct_lateral_relids
Definition: relation.h:609
bool delay_upper_joins
Definition: relation.h:2013
void * palloc0(Size size)
Definition: mcxt.c:877
void mark_dummy_rel(RelOptInfo *rel)
Definition: joinrels.c:1216
struct PartitionBoundInfoData * boundinfo
Definition: relation.h:659
RelOptInfo * build_join_rel(PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List **restrictlist_ptr)
Definition: relnode.c:480
AppendRelInfo ** find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
Definition: prepunion.c:2516
#define PARTITION_STRATEGY_HASH
Definition: parsenodes.h:787
double rows
Definition: relation.h:588
bool is_pushed_down
Definition: relation.h:1837
void bms_free(Bitmapset *a)
Definition: bitmapset.c:201
Relids right_relids
Definition: relation.h:1863
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:505
#define Assert(condition)
Definition: c.h:670
#define lfirst(lc)
Definition: pg_list.h:106
List ** join_rel_level
Definition: relation.h:225
JoinType jointype
Definition: relation.h:2011
struct RelOptInfo ** part_rels
Definition: relation.h:660
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:218
Oid hashjoinoperator
Definition: relation.h:1893
#define for_each_cell(cell, initcell)
Definition: pg_list.h:169
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:443
int errmsg(const char *fmt,...)
Definition: elog.c:797
List * semi_operators
Definition: relation.h:2017
List * placeholder_list
Definition: relation.h:258
bool have_relevant_joinclause(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
Definition: joininfo.c:36
void add_paths_to_joinrel(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: joinpath.c:117
void * arg
PartitionScheme part_scheme
Definition: relation.h:657
List * initial_rels
Definition: relation.h:272
List * pathlist
Definition: relation.h:599
Oid opno
Definition: primnodes.h:496
#define elog
Definition: elog.h:219
List * args
Definition: primnodes.h:502
Definition: pg_list.h:45
Relids min_lefthand
Definition: relation.h:2007
bool constisnull
Definition: primnodes.h:197
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:755
bool have_dangerous_phv(PlannerInfo *root, Relids outer_relids, Relids inner_params)
Definition: joinrels.c:1167
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:131
static MemoryContext GetMemoryChunkContext(void *pointer)
Definition: memutils.h:107