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