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