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