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