1 /*-------------------------------------------------------------------------
2  *
3  * allpaths.c
4  *	  Routines to find possible search paths for processing a query
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/allpaths.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 
16 #include "postgres.h"
17 
18 #include <limits.h>
19 #include <math.h>
20 
21 #include "access/sysattr.h"
22 #include "access/tsmapi.h"
23 #include "catalog/pg_class.h"
24 #include "catalog/pg_operator.h"
25 #include "catalog/pg_proc.h"
26 #include "foreign/fdwapi.h"
27 #include "nodes/makefuncs.h"
28 #include "nodes/nodeFuncs.h"
29 #ifdef OPTIMIZER_DEBUG
30 #include "nodes/print.h"
31 #endif
32 #include "optimizer/clauses.h"
33 #include "optimizer/cost.h"
34 #include "optimizer/geqo.h"
35 #include "optimizer/pathnode.h"
36 #include "optimizer/paths.h"
37 #include "optimizer/plancat.h"
38 #include "optimizer/planner.h"
39 #include "optimizer/prep.h"
40 #include "optimizer/restrictinfo.h"
41 #include "optimizer/tlist.h"
42 #include "optimizer/var.h"
43 #include "parser/parse_clause.h"
44 #include "parser/parsetree.h"
45 #include "rewrite/rewriteManip.h"
46 #include "utils/lsyscache.h"
47 
48 
49 /* results of subquery_is_pushdown_safe */
50 typedef struct pushdown_safety_info
51 {
52 	bool	   *unsafeColumns;	/* which output columns are unsafe to use */
53 	bool		unsafeVolatile; /* don't push down volatile quals */
54 	bool		unsafeLeaky;	/* don't push down leaky quals */
55 } pushdown_safety_info;
56 
57 /* These parameters are set by GUC */
58 bool		enable_geqo = false;	/* just in case GUC doesn't set it */
59 int			geqo_threshold;
60 int			min_parallel_table_scan_size;
61 int			min_parallel_index_scan_size;
62 
63 /* Hook for plugins to get control in set_rel_pathlist() */
64 set_rel_pathlist_hook_type set_rel_pathlist_hook = NULL;
65 
66 /* Hook for plugins to replace standard_join_search() */
67 join_search_hook_type join_search_hook = NULL;
68 
69 
70 static void set_base_rel_consider_startup(PlannerInfo *root);
71 static void set_base_rel_sizes(PlannerInfo *root);
72 static void set_base_rel_pathlists(PlannerInfo *root);
73 static void set_rel_size(PlannerInfo *root, RelOptInfo *rel,
74 			 Index rti, RangeTblEntry *rte);
75 static void set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
76 				 Index rti, RangeTblEntry *rte);
77 static void set_plain_rel_size(PlannerInfo *root, RelOptInfo *rel,
78 				   RangeTblEntry *rte);
79 static void create_plain_partial_paths(PlannerInfo *root, RelOptInfo *rel);
80 static void set_rel_consider_parallel(PlannerInfo *root, RelOptInfo *rel,
81 						  RangeTblEntry *rte);
82 static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
83 					   RangeTblEntry *rte);
84 static void set_tablesample_rel_size(PlannerInfo *root, RelOptInfo *rel,
85 						 RangeTblEntry *rte);
86 static void set_tablesample_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
87 							 RangeTblEntry *rte);
88 static void set_foreign_size(PlannerInfo *root, RelOptInfo *rel,
89 				 RangeTblEntry *rte);
90 static void set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel,
91 					 RangeTblEntry *rte);
92 static void set_append_rel_size(PlannerInfo *root, RelOptInfo *rel,
93 					Index rti, RangeTblEntry *rte);
94 static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
95 						Index rti, RangeTblEntry *rte);
96 static void generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel,
97 						   List *live_childrels,
98 						   List *all_child_pathkeys,
99 						   List *partitioned_rels);
100 static Path *get_cheapest_parameterized_child_path(PlannerInfo *root,
101 									  RelOptInfo *rel,
102 									  Relids required_outer);
103 static List *accumulate_append_subpath(List *subpaths, Path *path);
104 static void set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
105 					  Index rti, RangeTblEntry *rte);
106 static void set_function_pathlist(PlannerInfo *root, RelOptInfo *rel,
107 					  RangeTblEntry *rte);
108 static void set_values_pathlist(PlannerInfo *root, RelOptInfo *rel,
109 					RangeTblEntry *rte);
110 static void set_tablefunc_pathlist(PlannerInfo *root, RelOptInfo *rel,
111 					   RangeTblEntry *rte);
112 static void set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel,
113 				 RangeTblEntry *rte);
114 static void set_namedtuplestore_pathlist(PlannerInfo *root, RelOptInfo *rel,
115 							 RangeTblEntry *rte);
116 static void set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel,
117 					   RangeTblEntry *rte);
118 static RelOptInfo *make_rel_from_joinlist(PlannerInfo *root, List *joinlist);
119 static bool subquery_is_pushdown_safe(Query *subquery, Query *topquery,
120 						  pushdown_safety_info *safetyInfo);
121 static bool recurse_pushdown_safe(Node *setOp, Query *topquery,
122 					  pushdown_safety_info *safetyInfo);
123 static void check_output_expressions(Query *subquery,
124 						 pushdown_safety_info *safetyInfo);
125 static void compare_tlist_datatypes(List *tlist, List *colTypes,
126 						pushdown_safety_info *safetyInfo);
127 static bool targetIsInAllPartitionLists(TargetEntry *tle, Query *query);
128 static bool qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
129 					  pushdown_safety_info *safetyInfo);
130 static void subquery_push_qual(Query *subquery,
131 				   RangeTblEntry *rte, Index rti, Node *qual);
132 static void recurse_push_qual(Node *setOp, Query *topquery,
133 				  RangeTblEntry *rte, Index rti, Node *qual);
134 static void remove_unused_subquery_outputs(Query *subquery, RelOptInfo *rel);
135 static void add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel,
136 						List *live_childrels);
137 
138 
139 /*
140  * make_one_rel
141  *	  Finds all possible access paths for executing a query, returning a
142  *	  single rel that represents the join of all base rels in the query.
143  */
144 RelOptInfo *
make_one_rel(PlannerInfo * root,List * joinlist)145 make_one_rel(PlannerInfo *root, List *joinlist)
146 {
147 	RelOptInfo *rel;
148 	Index		rti;
149 
150 	/*
151 	 * Construct the all_baserels Relids set.
152 	 */
153 	root->all_baserels = NULL;
154 	for (rti = 1; rti < root->simple_rel_array_size; rti++)
155 	{
156 		RelOptInfo *brel = root->simple_rel_array[rti];
157 
158 		/* there may be empty slots corresponding to non-baserel RTEs */
159 		if (brel == NULL)
160 			continue;
161 
162 		Assert(brel->relid == rti); /* sanity check on array */
163 
164 		/* ignore RTEs that are "other rels" */
165 		if (brel->reloptkind != RELOPT_BASEREL)
166 			continue;
167 
168 		root->all_baserels = bms_add_member(root->all_baserels, brel->relid);
169 	}
170 
171 	/* Mark base rels as to whether we care about fast-start plans */
172 	set_base_rel_consider_startup(root);
173 
174 	/*
175 	 * Compute size estimates and consider_parallel flags for each base rel,
176 	 * then generate access paths.
177 	 */
178 	set_base_rel_sizes(root);
179 	set_base_rel_pathlists(root);
180 
181 	/*
182 	 * Generate access paths for the entire join tree.
183 	 */
184 	rel = make_rel_from_joinlist(root, joinlist);
185 
186 	/*
187 	 * The result should join all and only the query's base rels.
188 	 */
189 	Assert(bms_equal(rel->relids, root->all_baserels));
190 
191 	return rel;
192 }
193 
194 /*
195  * set_base_rel_consider_startup
196  *	  Set the consider_[param_]startup flags for each base-relation entry.
197  *
198  * For the moment, we only deal with consider_param_startup here; because the
199  * logic for consider_startup is pretty trivial and is the same for every base
200  * relation, we just let build_simple_rel() initialize that flag correctly to
201  * start with.  If that logic ever gets more complicated it would probably
202  * be better to move it here.
203  */
204 static void
set_base_rel_consider_startup(PlannerInfo * root)205 set_base_rel_consider_startup(PlannerInfo *root)
206 {
207 	/*
208 	 * Since parameterized paths can only be used on the inside of a nestloop
209 	 * join plan, there is usually little value in considering fast-start
210 	 * plans for them.  However, for relations that are on the RHS of a SEMI
211 	 * or ANTI join, a fast-start plan can be useful because we're only going
212 	 * to care about fetching one tuple anyway.
213 	 *
214 	 * To minimize growth of planning time, we currently restrict this to
215 	 * cases where the RHS is a single base relation, not a join; there is no
216 	 * provision for consider_param_startup to get set at all on joinrels.
217 	 * Also we don't worry about appendrels.  costsize.c's costing rules for
218 	 * nestloop semi/antijoins don't consider such cases either.
219 	 */
220 	ListCell   *lc;
221 
222 	foreach(lc, root->join_info_list)
223 	{
224 		SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
225 		int			varno;
226 
227 		if ((sjinfo->jointype == JOIN_SEMI || sjinfo->jointype == JOIN_ANTI) &&
228 			bms_get_singleton_member(sjinfo->syn_righthand, &varno))
229 		{
230 			RelOptInfo *rel = find_base_rel(root, varno);
231 
232 			rel->consider_param_startup = true;
233 		}
234 	}
235 }
236 
237 /*
238  * set_base_rel_sizes
239  *	  Set the size estimates (rows and widths) for each base-relation entry.
240  *	  Also determine whether to consider parallel paths for base relations.
241  *
242  * We do this in a separate pass over the base rels so that rowcount
243  * estimates are available for parameterized path generation, and also so
244  * that each rel's consider_parallel flag is set correctly before we begin to
245  * generate paths.
246  */
247 static void
set_base_rel_sizes(PlannerInfo * root)248 set_base_rel_sizes(PlannerInfo *root)
249 {
250 	Index		rti;
251 
252 	for (rti = 1; rti < root->simple_rel_array_size; rti++)
253 	{
254 		RelOptInfo *rel = root->simple_rel_array[rti];
255 		RangeTblEntry *rte;
256 
257 		/* there may be empty slots corresponding to non-baserel RTEs */
258 		if (rel == NULL)
259 			continue;
260 
261 		Assert(rel->relid == rti);	/* sanity check on array */
262 
263 		/* ignore RTEs that are "other rels" */
264 		if (rel->reloptkind != RELOPT_BASEREL)
265 			continue;
266 
267 		rte = root->simple_rte_array[rti];
268 
269 		/*
270 		 * If parallelism is allowable for this query in general, see whether
271 		 * it's allowable for this rel in particular.  We have to do this
272 		 * before set_rel_size(), because (a) if this rel is an inheritance
273 		 * parent, set_append_rel_size() will use and perhaps change the rel's
274 		 * consider_parallel flag, and (b) for some RTE types, set_rel_size()
275 		 * goes ahead and makes paths immediately.
276 		 */
277 		if (root->glob->parallelModeOK)
278 			set_rel_consider_parallel(root, rel, rte);
279 
280 		set_rel_size(root, rel, rti, rte);
281 	}
282 }
283 
284 /*
285  * set_base_rel_pathlists
286  *	  Finds all paths available for scanning each base-relation entry.
287  *	  Sequential scan and any available indices are considered.
288  *	  Each useful path is attached to its relation's 'pathlist' field.
289  */
290 static void
set_base_rel_pathlists(PlannerInfo * root)291 set_base_rel_pathlists(PlannerInfo *root)
292 {
293 	Index		rti;
294 
295 	for (rti = 1; rti < root->simple_rel_array_size; rti++)
296 	{
297 		RelOptInfo *rel = root->simple_rel_array[rti];
298 
299 		/* there may be empty slots corresponding to non-baserel RTEs */
300 		if (rel == NULL)
301 			continue;
302 
303 		Assert(rel->relid == rti);	/* sanity check on array */
304 
305 		/* ignore RTEs that are "other rels" */
306 		if (rel->reloptkind != RELOPT_BASEREL)
307 			continue;
308 
309 		set_rel_pathlist(root, rel, rti, root->simple_rte_array[rti]);
310 	}
311 }
312 
313 /*
314  * set_rel_size
315  *	  Set size estimates for a base relation
316  */
317 static void
set_rel_size(PlannerInfo * root,RelOptInfo * rel,Index rti,RangeTblEntry * rte)318 set_rel_size(PlannerInfo *root, RelOptInfo *rel,
319 			 Index rti, RangeTblEntry *rte)
320 {
321 	if (rel->reloptkind == RELOPT_BASEREL &&
322 		relation_excluded_by_constraints(root, rel, rte))
323 	{
324 		/*
325 		 * We proved we don't need to scan the rel via constraint exclusion,
326 		 * so set up a single dummy path for it.  Here we only check this for
327 		 * regular baserels; if it's an otherrel, CE was already checked in
328 		 * set_append_rel_size().
329 		 *
330 		 * In this case, we go ahead and set up the relation's path right away
331 		 * instead of leaving it for set_rel_pathlist to do.  This is because
332 		 * we don't have a convention for marking a rel as dummy except by
333 		 * assigning a dummy path to it.
334 		 */
335 		set_dummy_rel_pathlist(rel);
336 	}
337 	else if (rte->inh)
338 	{
339 		/* It's an "append relation", process accordingly */
340 		set_append_rel_size(root, rel, rti, rte);
341 	}
342 	else
343 	{
344 		switch (rel->rtekind)
345 		{
346 			case RTE_RELATION:
347 				if (rte->relkind == RELKIND_FOREIGN_TABLE)
348 				{
349 					/* Foreign table */
350 					set_foreign_size(root, rel, rte);
351 				}
352 				else if (rte->relkind == RELKIND_PARTITIONED_TABLE)
353 				{
354 					/*
355 					 * A partitioned table without leaf partitions is marked
356 					 * as a dummy rel.
357 					 */
358 					set_dummy_rel_pathlist(rel);
359 				}
360 				else if (rte->tablesample != NULL)
361 				{
362 					/* Sampled relation */
363 					set_tablesample_rel_size(root, rel, rte);
364 				}
365 				else
366 				{
367 					/* Plain relation */
368 					set_plain_rel_size(root, rel, rte);
369 				}
370 				break;
371 			case RTE_SUBQUERY:
372 
373 				/*
374 				 * Subqueries don't support making a choice between
375 				 * parameterized and unparameterized paths, so just go ahead
376 				 * and build their paths immediately.
377 				 */
378 				set_subquery_pathlist(root, rel, rti, rte);
379 				break;
380 			case RTE_FUNCTION:
381 				set_function_size_estimates(root, rel);
382 				break;
383 			case RTE_TABLEFUNC:
384 				set_tablefunc_size_estimates(root, rel);
385 				break;
386 			case RTE_VALUES:
387 				set_values_size_estimates(root, rel);
388 				break;
389 			case RTE_CTE:
390 
391 				/*
392 				 * CTEs don't support making a choice between parameterized
393 				 * and unparameterized paths, so just go ahead and build their
394 				 * paths immediately.
395 				 */
396 				if (rte->self_reference)
397 					set_worktable_pathlist(root, rel, rte);
398 				else
399 					set_cte_pathlist(root, rel, rte);
400 				break;
401 			case RTE_NAMEDTUPLESTORE:
402 				set_namedtuplestore_pathlist(root, rel, rte);
403 				break;
404 			default:
405 				elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
406 				break;
407 		}
408 	}
409 
410 	/*
411 	 * We insist that all non-dummy rels have a nonzero rowcount estimate.
412 	 */
413 	Assert(rel->rows > 0 || IS_DUMMY_REL(rel));
414 }
415 
416 /*
417  * set_rel_pathlist
418  *	  Build access paths for a base relation
419  */
420 static void
set_rel_pathlist(PlannerInfo * root,RelOptInfo * rel,Index rti,RangeTblEntry * rte)421 set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
422 				 Index rti, RangeTblEntry *rte)
423 {
424 	if (IS_DUMMY_REL(rel))
425 	{
426 		/* We already proved the relation empty, so nothing more to do */
427 	}
428 	else if (rte->inh)
429 	{
430 		/* It's an "append relation", process accordingly */
431 		set_append_rel_pathlist(root, rel, rti, rte);
432 	}
433 	else
434 	{
435 		switch (rel->rtekind)
436 		{
437 			case RTE_RELATION:
438 				if (rte->relkind == RELKIND_FOREIGN_TABLE)
439 				{
440 					/* Foreign table */
441 					set_foreign_pathlist(root, rel, rte);
442 				}
443 				else if (rte->tablesample != NULL)
444 				{
445 					/* Sampled relation */
446 					set_tablesample_rel_pathlist(root, rel, rte);
447 				}
448 				else
449 				{
450 					/* Plain relation */
451 					set_plain_rel_pathlist(root, rel, rte);
452 				}
453 				break;
454 			case RTE_SUBQUERY:
455 				/* Subquery --- fully handled during set_rel_size */
456 				break;
457 			case RTE_FUNCTION:
458 				/* RangeFunction */
459 				set_function_pathlist(root, rel, rte);
460 				break;
461 			case RTE_TABLEFUNC:
462 				/* Table Function */
463 				set_tablefunc_pathlist(root, rel, rte);
464 				break;
465 			case RTE_VALUES:
466 				/* Values list */
467 				set_values_pathlist(root, rel, rte);
468 				break;
469 			case RTE_CTE:
470 				/* CTE reference --- fully handled during set_rel_size */
471 				break;
472 			case RTE_NAMEDTUPLESTORE:
473 				/* tuplestore reference --- fully handled during set_rel_size */
474 				break;
475 			default:
476 				elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
477 				break;
478 		}
479 	}
480 
481 	/*
482 	 * Allow a plugin to editorialize on the set of Paths for this base
483 	 * relation.  It could add new paths (such as CustomPaths) by calling
484 	 * add_path(), or add_partial_path() if parallel aware.  It could also
485 	 * delete or modify paths added by the core code.
486 	 */
487 	if (set_rel_pathlist_hook)
488 		(*set_rel_pathlist_hook) (root, rel, rti, rte);
489 
490 	/*
491 	 * If this is a baserel, we should normally consider gathering any partial
492 	 * paths we may have created for it.  We have to do this after calling the
493 	 * set_rel_pathlist_hook, else it cannot add partial paths to be included
494 	 * here.
495 	 *
496 	 * However, if this is an inheritance child, skip it.  Otherwise, we could
497 	 * end up with a very large number of gather nodes, each trying to grab
498 	 * its own pool of workers.  Instead, we'll consider gathering partial
499 	 * paths for the parent appendrel.
500 	 */
501 	if (rel->reloptkind == RELOPT_BASEREL)
502 		generate_gather_paths(root, rel);
503 
504 	/* Now find the cheapest of the paths for this rel */
505 	set_cheapest(rel);
506 
507 #ifdef OPTIMIZER_DEBUG
508 	debug_print_rel(root, rel);
509 #endif
510 }
511 
512 /*
513  * set_plain_rel_size
514  *	  Set size estimates for a plain relation (no subquery, no inheritance)
515  */
516 static void
set_plain_rel_size(PlannerInfo * root,RelOptInfo * rel,RangeTblEntry * rte)517 set_plain_rel_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
518 {
519 	/*
520 	 * Test any partial indexes of rel for applicability.  We must do this
521 	 * first since partial unique indexes can affect size estimates.
522 	 */
523 	check_index_predicates(root, rel);
524 
525 	/* Mark rel with estimated output rows, width, etc */
526 	set_baserel_size_estimates(root, rel);
527 }
528 
529 /*
530  * If this relation could possibly be scanned from within a worker, then set
531  * its consider_parallel flag.
532  */
533 static void
set_rel_consider_parallel(PlannerInfo * root,RelOptInfo * rel,RangeTblEntry * rte)534 set_rel_consider_parallel(PlannerInfo *root, RelOptInfo *rel,
535 						  RangeTblEntry *rte)
536 {
537 	/*
538 	 * The flag has previously been initialized to false, so we can just
539 	 * return if it becomes clear that we can't safely set it.
540 	 */
541 	Assert(!rel->consider_parallel);
542 
543 	/* Don't call this if parallelism is disallowed for the entire query. */
544 	Assert(root->glob->parallelModeOK);
545 
546 	/* This should only be called for baserels and appendrel children. */
547 	Assert(IS_SIMPLE_REL(rel));
548 
549 	/* Assorted checks based on rtekind. */
550 	switch (rte->rtekind)
551 	{
552 		case RTE_RELATION:
553 
554 			/*
555 			 * Currently, parallel workers can't access the leader's temporary
556 			 * tables.  We could possibly relax this if we wrote all of its
557 			 * local buffers at the start of the query and made no changes
558 			 * thereafter (maybe we could allow hint bit changes), and if we
559 			 * taught the workers to read them.  Writing a large number of
560 			 * temporary buffers could be expensive, though, and we don't have
561 			 * the rest of the necessary infrastructure right now anyway.  So
562 			 * for now, bail out if we see a temporary table.
563 			 */
564 			if (get_rel_persistence(rte->relid) == RELPERSISTENCE_TEMP)
565 				return;
566 
567 			/*
568 			 * Table sampling can be pushed down to workers if the sample
569 			 * function and its arguments are safe.
570 			 */
571 			if (rte->tablesample != NULL)
572 			{
573 				char		proparallel = func_parallel(rte->tablesample->tsmhandler);
574 
575 				if (proparallel != PROPARALLEL_SAFE)
576 					return;
577 				if (!is_parallel_safe(root, (Node *) rte->tablesample->args))
578 					return;
579 			}
580 
581 			/*
582 			 * Ask FDWs whether they can support performing a ForeignScan
583 			 * within a worker.  Most often, the answer will be no.  For
584 			 * example, if the nature of the FDW is such that it opens a TCP
585 			 * connection with a remote server, each parallel worker would end
586 			 * up with a separate connection, and these connections might not
587 			 * be appropriately coordinated between workers and the leader.
588 			 */
589 			if (rte->relkind == RELKIND_FOREIGN_TABLE)
590 			{
591 				Assert(rel->fdwroutine);
592 				if (!rel->fdwroutine->IsForeignScanParallelSafe)
593 					return;
594 				if (!rel->fdwroutine->IsForeignScanParallelSafe(root, rel, rte))
595 					return;
596 			}
597 
598 			/*
599 			 * There are additional considerations for appendrels, which we'll
600 			 * deal with in set_append_rel_size and set_append_rel_pathlist.
601 			 * For now, just set consider_parallel based on the rel's own
602 			 * quals and targetlist.
603 			 */
604 			break;
605 
606 		case RTE_SUBQUERY:
607 
608 			/*
609 			 * There's no intrinsic problem with scanning a subquery-in-FROM
610 			 * (as distinct from a SubPlan or InitPlan) in a parallel worker.
611 			 * If the subquery doesn't happen to have any parallel-safe paths,
612 			 * then flagging it as consider_parallel won't change anything,
613 			 * but that's true for plain tables, too.  We must set
614 			 * consider_parallel based on the rel's own quals and targetlist,
615 			 * so that if a subquery path is parallel-safe but the quals and
616 			 * projection we're sticking onto it are not, we correctly mark
617 			 * the SubqueryScanPath as not parallel-safe.  (Note that
618 			 * set_subquery_pathlist() might push some of these quals down
619 			 * into the subquery itself, but that doesn't change anything.)
620 			 *
621 			 * We can't push sub-select containing LIMIT/OFFSET to workers as
622 			 * there is no guarantee that the row order will be fully
623 			 * deterministic, and applying LIMIT/OFFSET will lead to
624 			 * inconsistent results at the top-level.  (In some cases, where
625 			 * the result is ordered, we could relax this restriction.  But it
626 			 * doesn't currently seem worth expending extra effort to do so.)
627 			 */
628 			{
629 				Query	   *subquery = castNode(Query, rte->subquery);
630 
631 				if (limit_needed(subquery))
632 					return;
633 			}
634 			break;
635 
636 		case RTE_JOIN:
637 			/* Shouldn't happen; we're only considering baserels here. */
638 			Assert(false);
639 			return;
640 
641 		case RTE_FUNCTION:
642 			/* Check for parallel-restricted functions. */
643 			if (!is_parallel_safe(root, (Node *) rte->functions))
644 				return;
645 			break;
646 
647 		case RTE_TABLEFUNC:
648 			/* not parallel safe */
649 			return;
650 
651 		case RTE_VALUES:
652 			/* Check for parallel-restricted functions. */
653 			if (!is_parallel_safe(root, (Node *) rte->values_lists))
654 				return;
655 			break;
656 
657 		case RTE_CTE:
658 
659 			/*
660 			 * CTE tuplestores aren't shared among parallel workers, so we
661 			 * force all CTE scans to happen in the leader.  Also, populating
662 			 * the CTE would require executing a subplan that's not available
663 			 * in the worker, might be parallel-restricted, and must get
664 			 * executed only once.
665 			 */
666 			return;
667 
668 		case RTE_NAMEDTUPLESTORE:
669 
670 			/*
671 			 * tuplestore cannot be shared, at least without more
672 			 * infrastructure to support that.
673 			 */
674 			return;
675 	}
676 
677 	/*
678 	 * If there's anything in baserestrictinfo that's parallel-restricted, we
679 	 * give up on parallelizing access to this relation.  We could consider
680 	 * instead postponing application of the restricted quals until we're
681 	 * above all the parallelism in the plan tree, but it's not clear that
682 	 * that would be a win in very many cases, and it might be tricky to make
683 	 * outer join clauses work correctly.  It would likely break equivalence
684 	 * classes, too.
685 	 */
686 	if (!is_parallel_safe(root, (Node *) rel->baserestrictinfo))
687 		return;
688 
689 	/*
690 	 * Likewise, if the relation's outputs are not parallel-safe, give up.
691 	 * (Usually, they're just Vars, but sometimes they're not.)
692 	 */
693 	if (!is_parallel_safe(root, (Node *) rel->reltarget->exprs))
694 		return;
695 
696 	/* We have a winner. */
697 	rel->consider_parallel = true;
698 }
699 
700 /*
701  * set_plain_rel_pathlist
702  *	  Build access paths for a plain relation (no subquery, no inheritance)
703  */
704 static void
set_plain_rel_pathlist(PlannerInfo * root,RelOptInfo * rel,RangeTblEntry * rte)705 set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
706 {
707 	Relids		required_outer;
708 
709 	/*
710 	 * We don't support pushing join clauses into the quals of a seqscan, but
711 	 * it could still have required parameterization due to LATERAL refs in
712 	 * its tlist.
713 	 */
714 	required_outer = rel->lateral_relids;
715 
716 	/* Consider sequential scan */
717 	add_path(rel, create_seqscan_path(root, rel, required_outer, 0));
718 
719 	/* If appropriate, consider parallel sequential scan */
720 	if (rel->consider_parallel && required_outer == NULL)
721 		create_plain_partial_paths(root, rel);
722 
723 	/* Consider index scans */
724 	create_index_paths(root, rel);
725 
726 	/* Consider TID scans */
727 	create_tidscan_paths(root, rel);
728 }
729 
730 /*
731  * create_plain_partial_paths
732  *	  Build partial access paths for parallel scan of a plain relation
733  */
734 static void
create_plain_partial_paths(PlannerInfo * root,RelOptInfo * rel)735 create_plain_partial_paths(PlannerInfo *root, RelOptInfo *rel)
736 {
737 	int			parallel_workers;
738 
739 	parallel_workers = compute_parallel_worker(rel, rel->pages, -1);
740 
741 	/* If any limit was set to zero, the user doesn't want a parallel scan. */
742 	if (parallel_workers <= 0)
743 		return;
744 
745 	/* Add an unordered partial path based on a parallel sequential scan. */
746 	add_partial_path(rel, create_seqscan_path(root, rel, NULL, parallel_workers));
747 }
748 
749 /*
750  * set_tablesample_rel_size
751  *	  Set size estimates for a sampled relation
752  */
753 static void
set_tablesample_rel_size(PlannerInfo * root,RelOptInfo * rel,RangeTblEntry * rte)754 set_tablesample_rel_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
755 {
756 	TableSampleClause *tsc = rte->tablesample;
757 	TsmRoutine *tsm;
758 	BlockNumber pages;
759 	double		tuples;
760 
761 	/*
762 	 * Test any partial indexes of rel for applicability.  We must do this
763 	 * first since partial unique indexes can affect size estimates.
764 	 */
765 	check_index_predicates(root, rel);
766 
767 	/*
768 	 * Call the sampling method's estimation function to estimate the number
769 	 * of pages it will read and the number of tuples it will return.  (Note:
770 	 * we assume the function returns sane values.)
771 	 */
772 	tsm = GetTsmRoutine(tsc->tsmhandler);
773 	tsm->SampleScanGetSampleSize(root, rel, tsc->args,
774 								 &pages, &tuples);
775 
776 	/*
777 	 * For the moment, because we will only consider a SampleScan path for the
778 	 * rel, it's okay to just overwrite the pages and tuples estimates for the
779 	 * whole relation.  If we ever consider multiple path types for sampled
780 	 * rels, we'll need more complication.
781 	 */
782 	rel->pages = pages;
783 	rel->tuples = tuples;
784 
785 	/* Mark rel with estimated output rows, width, etc */
786 	set_baserel_size_estimates(root, rel);
787 }
788 
789 /*
790  * set_tablesample_rel_pathlist
791  *	  Build access paths for a sampled relation
792  */
793 static void
set_tablesample_rel_pathlist(PlannerInfo * root,RelOptInfo * rel,RangeTblEntry * rte)794 set_tablesample_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
795 {
796 	Relids		required_outer;
797 	Path	   *path;
798 
799 	/*
800 	 * We don't support pushing join clauses into the quals of a samplescan,
801 	 * but it could still have required parameterization due to LATERAL refs
802 	 * in its tlist or TABLESAMPLE arguments.
803 	 */
804 	required_outer = rel->lateral_relids;
805 
806 	/* Consider sampled scan */
807 	path = create_samplescan_path(root, rel, required_outer);
808 
809 	/*
810 	 * If the sampling method does not support repeatable scans, we must avoid
811 	 * plans that would scan the rel multiple times.  Ideally, we'd simply
812 	 * avoid putting the rel on the inside of a nestloop join; but adding such
813 	 * a consideration to the planner seems like a great deal of complication
814 	 * to support an uncommon usage of second-rate sampling methods.  Instead,
815 	 * if there is a risk that the query might perform an unsafe join, just
816 	 * wrap the SampleScan in a Materialize node.  We can check for joins by
817 	 * counting the membership of all_baserels (note that this correctly
818 	 * counts inheritance trees as single rels).  If we're inside a subquery,
819 	 * we can't easily check whether a join might occur in the outer query, so
820 	 * just assume one is possible.
821 	 *
822 	 * GetTsmRoutine is relatively expensive compared to the other tests here,
823 	 * so check repeatable_across_scans last, even though that's a bit odd.
824 	 */
825 	if ((root->query_level > 1 ||
826 		 bms_membership(root->all_baserels) != BMS_SINGLETON) &&
827 		!(GetTsmRoutine(rte->tablesample->tsmhandler)->repeatable_across_scans))
828 	{
829 		path = (Path *) create_material_path(rel, path);
830 	}
831 
832 	add_path(rel, path);
833 
834 	/* For the moment, at least, there are no other paths to consider */
835 }
836 
837 /*
838  * set_foreign_size
839  *		Set size estimates for a foreign table RTE
840  */
841 static void
set_foreign_size(PlannerInfo * root,RelOptInfo * rel,RangeTblEntry * rte)842 set_foreign_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
843 {
844 	/* Mark rel with estimated output rows, width, etc */
845 	set_foreign_size_estimates(root, rel);
846 
847 	/* Let FDW adjust the size estimates, if it can */
848 	rel->fdwroutine->GetForeignRelSize(root, rel, rte->relid);
849 
850 	/* ... but do not let it set the rows estimate to zero */
851 	rel->rows = clamp_row_est(rel->rows);
852 
853 	/* also, make sure rel->tuples is not insane relative to rel->rows */
854 	rel->tuples = Max(rel->tuples, rel->rows);
855 }
856 
857 /*
858  * set_foreign_pathlist
859  *		Build access paths for a foreign table RTE
860  */
861 static void
set_foreign_pathlist(PlannerInfo * root,RelOptInfo * rel,RangeTblEntry * rte)862 set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
863 {
864 	/* Call the FDW's GetForeignPaths function to generate path(s) */
865 	rel->fdwroutine->GetForeignPaths(root, rel, rte->relid);
866 }
867 
868 /*
869  * set_append_rel_size
870  *	  Set size estimates for a simple "append relation"
871  *
872  * The passed-in rel and RTE represent the entire append relation.  The
873  * relation's contents are computed by appending together the output of the
874  * individual member relations.  Note that in the non-partitioned inheritance
875  * case, the first member relation is actually the same table as is mentioned
876  * in the parent RTE ... but it has a different RTE and RelOptInfo.  This is
877  * a good thing because their outputs are not the same size.
878  */
879 static void
set_append_rel_size(PlannerInfo * root,RelOptInfo * rel,Index rti,RangeTblEntry * rte)880 set_append_rel_size(PlannerInfo *root, RelOptInfo *rel,
881 					Index rti, RangeTblEntry *rte)
882 {
883 	int			parentRTindex = rti;
884 	bool		has_live_children;
885 	double		parent_rows;
886 	double		parent_size;
887 	double	   *parent_attrsizes;
888 	int			nattrs;
889 	ListCell   *l;
890 
891 	Assert(IS_SIMPLE_REL(rel));
892 
893 	/*
894 	 * Initialize to compute size estimates for whole append relation.
895 	 *
896 	 * We handle width estimates by weighting the widths of different child
897 	 * rels proportionally to their number of rows.  This is sensible because
898 	 * the use of width estimates is mainly to compute the total relation
899 	 * "footprint" if we have to sort or hash it.  To do this, we sum the
900 	 * total equivalent size (in "double" arithmetic) and then divide by the
901 	 * total rowcount estimate.  This is done separately for the total rel
902 	 * width and each attribute.
903 	 *
904 	 * Note: if you consider changing this logic, beware that child rels could
905 	 * have zero rows and/or width, if they were excluded by constraints.
906 	 */
907 	has_live_children = false;
908 	parent_rows = 0;
909 	parent_size = 0;
910 	nattrs = rel->max_attr - rel->min_attr + 1;
911 	parent_attrsizes = (double *) palloc0(nattrs * sizeof(double));
912 
913 	foreach(l, root->append_rel_list)
914 	{
915 		AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
916 		int			childRTindex;
917 		RangeTblEntry *childRTE;
918 		RelOptInfo *childrel;
919 		List	   *childquals;
920 		Index		cq_min_security;
921 		bool		have_const_false_cq;
922 		ListCell   *parentvars;
923 		ListCell   *childvars;
924 		ListCell   *lc;
925 
926 		/* append_rel_list contains all append rels; ignore others */
927 		if (appinfo->parent_relid != parentRTindex)
928 			continue;
929 
930 		childRTindex = appinfo->child_relid;
931 		childRTE = root->simple_rte_array[childRTindex];
932 
933 		/*
934 		 * The child rel's RelOptInfo was already created during
935 		 * add_base_rels_to_query.
936 		 */
937 		childrel = find_base_rel(root, childRTindex);
938 		Assert(childrel->reloptkind == RELOPT_OTHER_MEMBER_REL);
939 
940 		/*
941 		 * We have to copy the parent's targetlist and quals to the child,
942 		 * with appropriate substitution of variables.  However, only the
943 		 * baserestrictinfo quals are needed before we can check for
944 		 * constraint exclusion; so do that first and then check to see if we
945 		 * can disregard this child.
946 		 *
947 		 * The child rel's targetlist might contain non-Var expressions, which
948 		 * means that substitution into the quals could produce opportunities
949 		 * for const-simplification, and perhaps even pseudoconstant quals.
950 		 * Therefore, transform each RestrictInfo separately to see if it
951 		 * reduces to a constant or pseudoconstant.  (We must process them
952 		 * separately to keep track of the security level of each qual.)
953 		 */
954 		childquals = NIL;
955 		cq_min_security = UINT_MAX;
956 		have_const_false_cq = false;
957 		foreach(lc, rel->baserestrictinfo)
958 		{
959 			RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
960 			Node	   *childqual;
961 			ListCell   *lc2;
962 
963 			Assert(IsA(rinfo, RestrictInfo));
964 			childqual = adjust_appendrel_attrs(root,
965 											   (Node *) rinfo->clause,
966 											   appinfo);
967 			childqual = eval_const_expressions(root, childqual);
968 			/* check for flat-out constant */
969 			if (childqual && IsA(childqual, Const))
970 			{
971 				if (((Const *) childqual)->constisnull ||
972 					!DatumGetBool(((Const *) childqual)->constvalue))
973 				{
974 					/* Restriction reduces to constant FALSE or NULL */
975 					have_const_false_cq = true;
976 					break;
977 				}
978 				/* Restriction reduces to constant TRUE, so drop it */
979 				continue;
980 			}
981 			/* might have gotten an AND clause, if so flatten it */
982 			foreach(lc2, make_ands_implicit((Expr *) childqual))
983 			{
984 				Node	   *onecq = (Node *) lfirst(lc2);
985 				bool		pseudoconstant;
986 
987 				/* check for pseudoconstant (no Vars or volatile functions) */
988 				pseudoconstant =
989 					!contain_vars_of_level(onecq, 0) &&
990 					!contain_volatile_functions(onecq);
991 				if (pseudoconstant)
992 				{
993 					/* tell createplan.c to check for gating quals */
994 					root->hasPseudoConstantQuals = true;
995 				}
996 				/* reconstitute RestrictInfo with appropriate properties */
997 				childquals = lappend(childquals,
998 									 make_restrictinfo((Expr *) onecq,
999 													   rinfo->is_pushed_down,
1000 													   rinfo->outerjoin_delayed,
1001 													   pseudoconstant,
1002 													   rinfo->security_level,
1003 													   NULL, NULL, NULL));
1004 				/* track minimum security level among child quals */
1005 				cq_min_security = Min(cq_min_security, rinfo->security_level);
1006 			}
1007 		}
1008 
1009 		/*
1010 		 * In addition to the quals inherited from the parent, we might have
1011 		 * securityQuals associated with this particular child node.
1012 		 * (Currently this can only happen in appendrels originating from
1013 		 * UNION ALL; inheritance child tables don't have their own
1014 		 * securityQuals, see expand_inherited_rtentry().)	Pull any such
1015 		 * securityQuals up into the baserestrictinfo for the child.  This is
1016 		 * similar to process_security_barrier_quals() for the parent rel,
1017 		 * except that we can't make any general deductions from such quals,
1018 		 * since they don't hold for the whole appendrel.
1019 		 */
1020 		if (childRTE->securityQuals)
1021 		{
1022 			Index		security_level = 0;
1023 
1024 			foreach(lc, childRTE->securityQuals)
1025 			{
1026 				List	   *qualset = (List *) lfirst(lc);
1027 				ListCell   *lc2;
1028 
1029 				foreach(lc2, qualset)
1030 				{
1031 					Expr	   *qual = (Expr *) lfirst(lc2);
1032 
1033 					/* not likely that we'd see constants here, so no check */
1034 					childquals = lappend(childquals,
1035 										 make_restrictinfo(qual,
1036 														   true, false, false,
1037 														   security_level,
1038 														   NULL, NULL, NULL));
1039 					cq_min_security = Min(cq_min_security, security_level);
1040 				}
1041 				security_level++;
1042 			}
1043 			Assert(security_level <= root->qual_security_level);
1044 		}
1045 
1046 		/*
1047 		 * OK, we've got all the baserestrictinfo quals for this child.
1048 		 */
1049 		childrel->baserestrictinfo = childquals;
1050 		childrel->baserestrict_min_security = cq_min_security;
1051 
1052 		if (have_const_false_cq)
1053 		{
1054 			/*
1055 			 * Some restriction clause reduced to constant FALSE or NULL after
1056 			 * substitution, so this child need not be scanned.
1057 			 */
1058 			set_dummy_rel_pathlist(childrel);
1059 			continue;
1060 		}
1061 
1062 		if (relation_excluded_by_constraints(root, childrel, childRTE))
1063 		{
1064 			/*
1065 			 * This child need not be scanned, so we can omit it from the
1066 			 * appendrel.
1067 			 */
1068 			set_dummy_rel_pathlist(childrel);
1069 			continue;
1070 		}
1071 
1072 		/*
1073 		 * CE failed, so finish copying/modifying targetlist and join quals.
1074 		 *
1075 		 * NB: the resulting childrel->reltarget->exprs may contain arbitrary
1076 		 * expressions, which otherwise would not occur in a rel's targetlist.
1077 		 * Code that might be looking at an appendrel child must cope with
1078 		 * such.  (Normally, a rel's targetlist would only include Vars and
1079 		 * PlaceHolderVars.)  XXX we do not bother to update the cost or width
1080 		 * fields of childrel->reltarget; not clear if that would be useful.
1081 		 */
1082 		childrel->joininfo = (List *)
1083 			adjust_appendrel_attrs(root,
1084 								   (Node *) rel->joininfo,
1085 								   appinfo);
1086 		childrel->reltarget->exprs = (List *)
1087 			adjust_appendrel_attrs(root,
1088 								   (Node *) rel->reltarget->exprs,
1089 								   appinfo);
1090 
1091 		/*
1092 		 * We have to make child entries in the EquivalenceClass data
1093 		 * structures as well.  This is needed either if the parent
1094 		 * participates in some eclass joins (because we will want to consider
1095 		 * inner-indexscan joins on the individual children) or if the parent
1096 		 * has useful pathkeys (because we should try to build MergeAppend
1097 		 * paths that produce those sort orderings).
1098 		 */
1099 		if (rel->has_eclass_joins || has_useful_pathkeys(root, rel))
1100 			add_child_rel_equivalences(root, appinfo, rel, childrel);
1101 		childrel->has_eclass_joins = rel->has_eclass_joins;
1102 
1103 		/*
1104 		 * Note: we could compute appropriate attr_needed data for the child's
1105 		 * variables, by transforming the parent's attr_needed through the
1106 		 * translated_vars mapping.  However, currently there's no need
1107 		 * because attr_needed is only examined for base relations not
1108 		 * otherrels.  So we just leave the child's attr_needed empty.
1109 		 */
1110 
1111 		/*
1112 		 * If parallelism is allowable for this query in general, see whether
1113 		 * it's allowable for this childrel in particular.  But if we've
1114 		 * already decided the appendrel is not parallel-safe as a whole,
1115 		 * there's no point in considering parallelism for this child.  For
1116 		 * consistency, do this before calling set_rel_size() for the child.
1117 		 */
1118 		if (root->glob->parallelModeOK && rel->consider_parallel)
1119 			set_rel_consider_parallel(root, childrel, childRTE);
1120 
1121 		/*
1122 		 * Compute the child's size.
1123 		 */
1124 		set_rel_size(root, childrel, childRTindex, childRTE);
1125 
1126 		/*
1127 		 * It is possible that constraint exclusion detected a contradiction
1128 		 * within a child subquery, even though we didn't prove one above. If
1129 		 * so, we can skip this child.
1130 		 */
1131 		if (IS_DUMMY_REL(childrel))
1132 			continue;
1133 
1134 		/* We have at least one live child. */
1135 		has_live_children = true;
1136 
1137 		/*
1138 		 * If any live child is not parallel-safe, treat the whole appendrel
1139 		 * as not parallel-safe.  In future we might be able to generate plans
1140 		 * in which some children are farmed out to workers while others are
1141 		 * not; but we don't have that today, so it's a waste to consider
1142 		 * partial paths anywhere in the appendrel unless it's all safe.
1143 		 * (Child rels visited before this one will be unmarked in
1144 		 * set_append_rel_pathlist().)
1145 		 */
1146 		if (!childrel->consider_parallel)
1147 			rel->consider_parallel = false;
1148 
1149 		/*
1150 		 * Accumulate size information from each live child.
1151 		 */
1152 		Assert(childrel->rows > 0);
1153 
1154 		parent_rows += childrel->rows;
1155 		parent_size += childrel->reltarget->width * childrel->rows;
1156 
1157 		/*
1158 		 * Accumulate per-column estimates too.  We need not do anything for
1159 		 * PlaceHolderVars in the parent list.  If child expression isn't a
1160 		 * Var, or we didn't record a width estimate for it, we have to fall
1161 		 * back on a datatype-based estimate.
1162 		 *
1163 		 * By construction, child's targetlist is 1-to-1 with parent's.
1164 		 */
1165 		forboth(parentvars, rel->reltarget->exprs,
1166 				childvars, childrel->reltarget->exprs)
1167 		{
1168 			Var		   *parentvar = (Var *) lfirst(parentvars);
1169 			Node	   *childvar = (Node *) lfirst(childvars);
1170 
1171 			if (IsA(parentvar, Var))
1172 			{
1173 				int			pndx = parentvar->varattno - rel->min_attr;
1174 				int32		child_width = 0;
1175 
1176 				if (IsA(childvar, Var) &&
1177 					((Var *) childvar)->varno == childrel->relid)
1178 				{
1179 					int			cndx = ((Var *) childvar)->varattno - childrel->min_attr;
1180 
1181 					child_width = childrel->attr_widths[cndx];
1182 				}
1183 				if (child_width <= 0)
1184 					child_width = get_typavgwidth(exprType(childvar),
1185 												  exprTypmod(childvar));
1186 				Assert(child_width > 0);
1187 				parent_attrsizes[pndx] += child_width * childrel->rows;
1188 			}
1189 		}
1190 	}
1191 
1192 	if (has_live_children)
1193 	{
1194 		/*
1195 		 * Save the finished size estimates.
1196 		 */
1197 		int			i;
1198 
1199 		Assert(parent_rows > 0);
1200 		rel->rows = parent_rows;
1201 		rel->reltarget->width = rint(parent_size / parent_rows);
1202 		for (i = 0; i < nattrs; i++)
1203 			rel->attr_widths[i] = rint(parent_attrsizes[i] / parent_rows);
1204 
1205 		/*
1206 		 * Set "raw tuples" count equal to "rows" for the appendrel; needed
1207 		 * because some places assume rel->tuples is valid for any baserel.
1208 		 */
1209 		rel->tuples = parent_rows;
1210 	}
1211 	else
1212 	{
1213 		/*
1214 		 * All children were excluded by constraints, so mark the whole
1215 		 * appendrel dummy.  We must do this in this phase so that the rel's
1216 		 * dummy-ness is visible when we generate paths for other rels.
1217 		 */
1218 		set_dummy_rel_pathlist(rel);
1219 	}
1220 
1221 	pfree(parent_attrsizes);
1222 }
1223 
1224 /*
1225  * set_append_rel_pathlist
1226  *	  Build access paths for an "append relation"
1227  */
1228 static void
set_append_rel_pathlist(PlannerInfo * root,RelOptInfo * rel,Index rti,RangeTblEntry * rte)1229 set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
1230 						Index rti, RangeTblEntry *rte)
1231 {
1232 	int			parentRTindex = rti;
1233 	List	   *live_childrels = NIL;
1234 	ListCell   *l;
1235 
1236 	/*
1237 	 * Generate access paths for each member relation, and remember the
1238 	 * non-dummy children.
1239 	 */
1240 	foreach(l, root->append_rel_list)
1241 	{
1242 		AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
1243 		int			childRTindex;
1244 		RangeTblEntry *childRTE;
1245 		RelOptInfo *childrel;
1246 
1247 		/* append_rel_list contains all append rels; ignore others */
1248 		if (appinfo->parent_relid != parentRTindex)
1249 			continue;
1250 
1251 		/* Re-locate the child RTE and RelOptInfo */
1252 		childRTindex = appinfo->child_relid;
1253 		childRTE = root->simple_rte_array[childRTindex];
1254 		childrel = root->simple_rel_array[childRTindex];
1255 
1256 		/*
1257 		 * If set_append_rel_size() decided the parent appendrel was
1258 		 * parallel-unsafe at some point after visiting this child rel, we
1259 		 * need to propagate the unsafety marking down to the child, so that
1260 		 * we don't generate useless partial paths for it.
1261 		 */
1262 		if (!rel->consider_parallel)
1263 			childrel->consider_parallel = false;
1264 
1265 		/*
1266 		 * Compute the child's access paths.
1267 		 */
1268 		set_rel_pathlist(root, childrel, childRTindex, childRTE);
1269 
1270 		/*
1271 		 * If child is dummy, ignore it.
1272 		 */
1273 		if (IS_DUMMY_REL(childrel))
1274 			continue;
1275 
1276 		/*
1277 		 * Child is live, so add it to the live_childrels list for use below.
1278 		 */
1279 		live_childrels = lappend(live_childrels, childrel);
1280 	}
1281 
1282 	/* Add paths to the "append" relation. */
1283 	add_paths_to_append_rel(root, rel, live_childrels);
1284 }
1285 
1286 
1287 /*
1288  * add_paths_to_append_rel
1289  *		Generate paths for given "append" relation given the set of non-dummy
1290  *		child rels.
1291  *
1292  * The function collects all parameterizations and orderings supported by the
1293  * non-dummy children. For every such parameterization or ordering, it creates
1294  * an append path collecting one path from each non-dummy child with given
1295  * parameterization or ordering. Similarly it collects partial paths from
1296  * non-dummy children to create partial append paths.
1297  */
1298 static void
add_paths_to_append_rel(PlannerInfo * root,RelOptInfo * rel,List * live_childrels)1299 add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel,
1300 						List *live_childrels)
1301 {
1302 	List	   *subpaths = NIL;
1303 	bool		subpaths_valid = true;
1304 	List	   *partial_subpaths = NIL;
1305 	bool		partial_subpaths_valid = true;
1306 	List	   *all_child_pathkeys = NIL;
1307 	List	   *all_child_outers = NIL;
1308 	ListCell   *l;
1309 	List	   *partitioned_rels = NIL;
1310 	RangeTblEntry *rte;
1311 	bool		build_partitioned_rels = false;
1312 
1313 	/*
1314 	 * A plain relation will already have a PartitionedChildRelInfo if it is
1315 	 * partitioned.  For a subquery RTE, no PartitionedChildRelInfo exists; we
1316 	 * collect all partitioned_rels associated with any child.  (This assumes
1317 	 * that we don't need to look through multiple levels of subquery RTEs; if
1318 	 * we ever do, we could create a PartitionedChildRelInfo with the
1319 	 * accumulated list of partitioned_rels which would then be found when
1320 	 * populated our parent rel with paths.  For the present, that appears to
1321 	 * be unnecessary.)
1322 	 */
1323 	rte = planner_rt_fetch(rel->relid, root);
1324 	switch (rte->rtekind)
1325 	{
1326 		case RTE_RELATION:
1327 			if (rte->relkind == RELKIND_PARTITIONED_TABLE)
1328 			{
1329 				partitioned_rels =
1330 					get_partitioned_child_rels(root, rel->relid);
1331 				Assert(list_length(partitioned_rels) >= 1);
1332 			}
1333 			break;
1334 		case RTE_SUBQUERY:
1335 			build_partitioned_rels = true;
1336 			break;
1337 		default:
1338 			elog(ERROR, "unexpected rtekind: %d", (int) rte->rtekind);
1339 	}
1340 
1341 	/*
1342 	 * For every non-dummy child, remember the cheapest path.  Also, identify
1343 	 * all pathkeys (orderings) and parameterizations (required_outer sets)
1344 	 * available for the non-dummy member relations.
1345 	 */
1346 	foreach(l, live_childrels)
1347 	{
1348 		RelOptInfo *childrel = lfirst(l);
1349 		ListCell   *lcp;
1350 
1351 		/*
1352 		 * If we need to build partitioned_rels, accumulate the partitioned
1353 		 * rels for this child.
1354 		 */
1355 		if (build_partitioned_rels)
1356 		{
1357 			List	   *cprels;
1358 
1359 			cprels = get_partitioned_child_rels(root, childrel->relid);
1360 			partitioned_rels = list_concat(partitioned_rels,
1361 										   list_copy(cprels));
1362 		}
1363 
1364 		/*
1365 		 * If child has an unparameterized cheapest-total path, add that to
1366 		 * the unparameterized Append path we are constructing for the parent.
1367 		 * If not, there's no workable unparameterized path.
1368 		 */
1369 		if (childrel->cheapest_total_path->param_info == NULL)
1370 			subpaths = accumulate_append_subpath(subpaths,
1371 												 childrel->cheapest_total_path);
1372 		else
1373 			subpaths_valid = false;
1374 
1375 		/* Same idea, but for a partial plan. */
1376 		if (childrel->partial_pathlist != NIL)
1377 			partial_subpaths = accumulate_append_subpath(partial_subpaths,
1378 														 linitial(childrel->partial_pathlist));
1379 		else
1380 			partial_subpaths_valid = false;
1381 
1382 		/*
1383 		 * Collect lists of all the available path orderings and
1384 		 * parameterizations for all the children.  We use these as a
1385 		 * heuristic to indicate which sort orderings and parameterizations we
1386 		 * should build Append and MergeAppend paths for.
1387 		 */
1388 		foreach(lcp, childrel->pathlist)
1389 		{
1390 			Path	   *childpath = (Path *) lfirst(lcp);
1391 			List	   *childkeys = childpath->pathkeys;
1392 			Relids		childouter = PATH_REQ_OUTER(childpath);
1393 
1394 			/* Unsorted paths don't contribute to pathkey list */
1395 			if (childkeys != NIL)
1396 			{
1397 				ListCell   *lpk;
1398 				bool		found = false;
1399 
1400 				/* Have we already seen this ordering? */
1401 				foreach(lpk, all_child_pathkeys)
1402 				{
1403 					List	   *existing_pathkeys = (List *) lfirst(lpk);
1404 
1405 					if (compare_pathkeys(existing_pathkeys,
1406 										 childkeys) == PATHKEYS_EQUAL)
1407 					{
1408 						found = true;
1409 						break;
1410 					}
1411 				}
1412 				if (!found)
1413 				{
1414 					/* No, so add it to all_child_pathkeys */
1415 					all_child_pathkeys = lappend(all_child_pathkeys,
1416 												 childkeys);
1417 				}
1418 			}
1419 
1420 			/* Unparameterized paths don't contribute to param-set list */
1421 			if (childouter)
1422 			{
1423 				ListCell   *lco;
1424 				bool		found = false;
1425 
1426 				/* Have we already seen this param set? */
1427 				foreach(lco, all_child_outers)
1428 				{
1429 					Relids		existing_outers = (Relids) lfirst(lco);
1430 
1431 					if (bms_equal(existing_outers, childouter))
1432 					{
1433 						found = true;
1434 						break;
1435 					}
1436 				}
1437 				if (!found)
1438 				{
1439 					/* No, so add it to all_child_outers */
1440 					all_child_outers = lappend(all_child_outers,
1441 											   childouter);
1442 				}
1443 			}
1444 		}
1445 	}
1446 
1447 	/*
1448 	 * If we found unparameterized paths for all children, build an unordered,
1449 	 * unparameterized Append path for the rel.  (Note: this is correct even
1450 	 * if we have zero or one live subpath due to constraint exclusion.)
1451 	 */
1452 	if (subpaths_valid)
1453 		add_path(rel, (Path *) create_append_path(rel, subpaths, NULL, 0,
1454 												  partitioned_rels));
1455 
1456 	/*
1457 	 * Consider an append of partial unordered, unparameterized partial paths.
1458 	 */
1459 	if (partial_subpaths_valid && partial_subpaths != NIL)
1460 	{
1461 		AppendPath *appendpath;
1462 		ListCell   *lc;
1463 		int			parallel_workers = 0;
1464 
1465 		/*
1466 		 * Decide on the number of workers to request for this append path.
1467 		 * For now, we just use the maximum value from among the members.  It
1468 		 * might be useful to use a higher number if the Append node were
1469 		 * smart enough to spread out the workers, but it currently isn't.
1470 		 */
1471 		foreach(lc, partial_subpaths)
1472 		{
1473 			Path	   *path = lfirst(lc);
1474 
1475 			parallel_workers = Max(parallel_workers, path->parallel_workers);
1476 		}
1477 		Assert(parallel_workers > 0);
1478 
1479 		/* Generate a partial append path. */
1480 		appendpath = create_append_path(rel, partial_subpaths, NULL,
1481 										parallel_workers, partitioned_rels);
1482 		add_partial_path(rel, (Path *) appendpath);
1483 	}
1484 
1485 	/*
1486 	 * Also build unparameterized MergeAppend paths based on the collected
1487 	 * list of child pathkeys.
1488 	 */
1489 	if (subpaths_valid)
1490 		generate_mergeappend_paths(root, rel, live_childrels,
1491 								   all_child_pathkeys,
1492 								   partitioned_rels);
1493 
1494 	/*
1495 	 * Build Append paths for each parameterization seen among the child rels.
1496 	 * (This may look pretty expensive, but in most cases of practical
1497 	 * interest, the child rels will expose mostly the same parameterizations,
1498 	 * so that not that many cases actually get considered here.)
1499 	 *
1500 	 * The Append node itself cannot enforce quals, so all qual checking must
1501 	 * be done in the child paths.  This means that to have a parameterized
1502 	 * Append path, we must have the exact same parameterization for each
1503 	 * child path; otherwise some children might be failing to check the
1504 	 * moved-down quals.  To make them match up, we can try to increase the
1505 	 * parameterization of lesser-parameterized paths.
1506 	 */
1507 	foreach(l, all_child_outers)
1508 	{
1509 		Relids		required_outer = (Relids) lfirst(l);
1510 		ListCell   *lcr;
1511 
1512 		/* Select the child paths for an Append with this parameterization */
1513 		subpaths = NIL;
1514 		subpaths_valid = true;
1515 		foreach(lcr, live_childrels)
1516 		{
1517 			RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
1518 			Path	   *subpath;
1519 
1520 			subpath = get_cheapest_parameterized_child_path(root,
1521 															childrel,
1522 															required_outer);
1523 			if (subpath == NULL)
1524 			{
1525 				/* failed to make a suitable path for this child */
1526 				subpaths_valid = false;
1527 				break;
1528 			}
1529 			subpaths = accumulate_append_subpath(subpaths, subpath);
1530 		}
1531 
1532 		if (subpaths_valid)
1533 			add_path(rel, (Path *)
1534 					 create_append_path(rel, subpaths, required_outer, 0,
1535 										partitioned_rels));
1536 	}
1537 }
1538 
1539 /*
1540  * generate_mergeappend_paths
1541  *		Generate MergeAppend paths for an append relation
1542  *
1543  * Generate a path for each ordering (pathkey list) appearing in
1544  * all_child_pathkeys.
1545  *
1546  * We consider both cheapest-startup and cheapest-total cases, ie, for each
1547  * interesting ordering, collect all the cheapest startup subpaths and all the
1548  * cheapest total paths, and build a MergeAppend path for each case.
1549  *
1550  * We don't currently generate any parameterized MergeAppend paths.  While
1551  * it would not take much more code here to do so, it's very unclear that it
1552  * is worth the planning cycles to investigate such paths: there's little
1553  * use for an ordered path on the inside of a nestloop.  In fact, it's likely
1554  * that the current coding of add_path would reject such paths out of hand,
1555  * because add_path gives no credit for sort ordering of parameterized paths,
1556  * and a parameterized MergeAppend is going to be more expensive than the
1557  * corresponding parameterized Append path.  If we ever try harder to support
1558  * parameterized mergejoin plans, it might be worth adding support for
1559  * parameterized MergeAppends to feed such joins.  (See notes in
1560  * optimizer/README for why that might not ever happen, though.)
1561  */
1562 static void
generate_mergeappend_paths(PlannerInfo * root,RelOptInfo * rel,List * live_childrels,List * all_child_pathkeys,List * partitioned_rels)1563 generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel,
1564 						   List *live_childrels,
1565 						   List *all_child_pathkeys,
1566 						   List *partitioned_rels)
1567 {
1568 	ListCell   *lcp;
1569 
1570 	foreach(lcp, all_child_pathkeys)
1571 	{
1572 		List	   *pathkeys = (List *) lfirst(lcp);
1573 		List	   *startup_subpaths = NIL;
1574 		List	   *total_subpaths = NIL;
1575 		bool		startup_neq_total = false;
1576 		ListCell   *lcr;
1577 
1578 		/* Select the child paths for this ordering... */
1579 		foreach(lcr, live_childrels)
1580 		{
1581 			RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
1582 			Path	   *cheapest_startup,
1583 					   *cheapest_total;
1584 
1585 			/* Locate the right paths, if they are available. */
1586 			cheapest_startup =
1587 				get_cheapest_path_for_pathkeys(childrel->pathlist,
1588 											   pathkeys,
1589 											   NULL,
1590 											   STARTUP_COST,
1591 											   false);
1592 			cheapest_total =
1593 				get_cheapest_path_for_pathkeys(childrel->pathlist,
1594 											   pathkeys,
1595 											   NULL,
1596 											   TOTAL_COST,
1597 											   false);
1598 
1599 			/*
1600 			 * If we can't find any paths with the right order just use the
1601 			 * cheapest-total path; we'll have to sort it later.
1602 			 */
1603 			if (cheapest_startup == NULL || cheapest_total == NULL)
1604 			{
1605 				cheapest_startup = cheapest_total =
1606 					childrel->cheapest_total_path;
1607 				/* Assert we do have an unparameterized path for this child */
1608 				Assert(cheapest_total->param_info == NULL);
1609 			}
1610 
1611 			/*
1612 			 * Notice whether we actually have different paths for the
1613 			 * "cheapest" and "total" cases; frequently there will be no point
1614 			 * in two create_merge_append_path() calls.
1615 			 */
1616 			if (cheapest_startup != cheapest_total)
1617 				startup_neq_total = true;
1618 
1619 			startup_subpaths =
1620 				accumulate_append_subpath(startup_subpaths, cheapest_startup);
1621 			total_subpaths =
1622 				accumulate_append_subpath(total_subpaths, cheapest_total);
1623 		}
1624 
1625 		/* ... and build the MergeAppend paths */
1626 		add_path(rel, (Path *) create_merge_append_path(root,
1627 														rel,
1628 														startup_subpaths,
1629 														pathkeys,
1630 														NULL,
1631 														partitioned_rels));
1632 		if (startup_neq_total)
1633 			add_path(rel, (Path *) create_merge_append_path(root,
1634 															rel,
1635 															total_subpaths,
1636 															pathkeys,
1637 															NULL,
1638 															partitioned_rels));
1639 	}
1640 }
1641 
1642 /*
1643  * get_cheapest_parameterized_child_path
1644  *		Get cheapest path for this relation that has exactly the requested
1645  *		parameterization.
1646  *
1647  * Returns NULL if unable to create such a path.
1648  */
1649 static Path *
get_cheapest_parameterized_child_path(PlannerInfo * root,RelOptInfo * rel,Relids required_outer)1650 get_cheapest_parameterized_child_path(PlannerInfo *root, RelOptInfo *rel,
1651 									  Relids required_outer)
1652 {
1653 	Path	   *cheapest;
1654 	ListCell   *lc;
1655 
1656 	/*
1657 	 * Look up the cheapest existing path with no more than the needed
1658 	 * parameterization.  If it has exactly the needed parameterization, we're
1659 	 * done.
1660 	 */
1661 	cheapest = get_cheapest_path_for_pathkeys(rel->pathlist,
1662 											  NIL,
1663 											  required_outer,
1664 											  TOTAL_COST,
1665 											  false);
1666 	Assert(cheapest != NULL);
1667 	if (bms_equal(PATH_REQ_OUTER(cheapest), required_outer))
1668 		return cheapest;
1669 
1670 	/*
1671 	 * Otherwise, we can "reparameterize" an existing path to match the given
1672 	 * parameterization, which effectively means pushing down additional
1673 	 * joinquals to be checked within the path's scan.  However, some existing
1674 	 * paths might check the available joinquals already while others don't;
1675 	 * therefore, it's not clear which existing path will be cheapest after
1676 	 * reparameterization.  We have to go through them all and find out.
1677 	 */
1678 	cheapest = NULL;
1679 	foreach(lc, rel->pathlist)
1680 	{
1681 		Path	   *path = (Path *) lfirst(lc);
1682 
1683 		/* Can't use it if it needs more than requested parameterization */
1684 		if (!bms_is_subset(PATH_REQ_OUTER(path), required_outer))
1685 			continue;
1686 
1687 		/*
1688 		 * Reparameterization can only increase the path's cost, so if it's
1689 		 * already more expensive than the current cheapest, forget it.
1690 		 */
1691 		if (cheapest != NULL &&
1692 			compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
1693 			continue;
1694 
1695 		/* Reparameterize if needed, then recheck cost */
1696 		if (!bms_equal(PATH_REQ_OUTER(path), required_outer))
1697 		{
1698 			path = reparameterize_path(root, path, required_outer, 1.0);
1699 			if (path == NULL)
1700 				continue;		/* failed to reparameterize this one */
1701 			Assert(bms_equal(PATH_REQ_OUTER(path), required_outer));
1702 
1703 			if (cheapest != NULL &&
1704 				compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
1705 				continue;
1706 		}
1707 
1708 		/* We have a new best path */
1709 		cheapest = path;
1710 	}
1711 
1712 	/* Return the best path, or NULL if we found no suitable candidate */
1713 	return cheapest;
1714 }
1715 
1716 /*
1717  * accumulate_append_subpath
1718  *		Add a subpath to the list being built for an Append or MergeAppend
1719  *
1720  * It's possible that the child is itself an Append or MergeAppend path, in
1721  * which case we can "cut out the middleman" and just add its child paths to
1722  * our own list.  (We don't try to do this earlier because we need to apply
1723  * both levels of transformation to the quals.)
1724  *
1725  * Note that if we omit a child MergeAppend in this way, we are effectively
1726  * omitting a sort step, which seems fine: if the parent is to be an Append,
1727  * its result would be unsorted anyway, while if the parent is to be a
1728  * MergeAppend, there's no point in a separate sort on a child.
1729  */
1730 static List *
accumulate_append_subpath(List * subpaths,Path * path)1731 accumulate_append_subpath(List *subpaths, Path *path)
1732 {
1733 	if (IsA(path, AppendPath))
1734 	{
1735 		AppendPath *apath = (AppendPath *) path;
1736 
1737 		/* list_copy is important here to avoid sharing list substructure */
1738 		return list_concat(subpaths, list_copy(apath->subpaths));
1739 	}
1740 	else if (IsA(path, MergeAppendPath))
1741 	{
1742 		MergeAppendPath *mpath = (MergeAppendPath *) path;
1743 
1744 		/* list_copy is important here to avoid sharing list substructure */
1745 		return list_concat(subpaths, list_copy(mpath->subpaths));
1746 	}
1747 	else
1748 		return lappend(subpaths, path);
1749 }
1750 
1751 /*
1752  * set_dummy_rel_pathlist
1753  *	  Build a dummy path for a relation that's been excluded by constraints
1754  *
1755  * Rather than inventing a special "dummy" path type, we represent this as an
1756  * AppendPath with no members (see also IS_DUMMY_APPEND/IS_DUMMY_REL macros).
1757  *
1758  * (See also mark_dummy_rel, which does basically the same thing, but is
1759  * typically used to change a rel into dummy state after we already made
1760  * paths for it.)
1761  */
1762 void
set_dummy_rel_pathlist(RelOptInfo * rel)1763 set_dummy_rel_pathlist(RelOptInfo *rel)
1764 {
1765 	/* Set dummy size estimates --- we leave attr_widths[] as zeroes */
1766 	rel->rows = 0;
1767 	rel->reltarget->width = 0;
1768 
1769 	/* Discard any pre-existing paths; no further need for them */
1770 	rel->pathlist = NIL;
1771 	rel->partial_pathlist = NIL;
1772 
1773 	/* Set up the dummy path */
1774 	add_path(rel, (Path *) create_append_path(rel, NIL,
1775 											  rel->lateral_relids,
1776 											  0, NIL));
1777 
1778 	/*
1779 	 * We set the cheapest-path fields immediately, just in case they were
1780 	 * pointing at some discarded path.  This is redundant when we're called
1781 	 * from set_rel_size(), but not when called from elsewhere, and doing it
1782 	 * twice is harmless anyway.
1783 	 */
1784 	set_cheapest(rel);
1785 }
1786 
1787 /* quick-and-dirty test to see if any joining is needed */
1788 static bool
has_multiple_baserels(PlannerInfo * root)1789 has_multiple_baserels(PlannerInfo *root)
1790 {
1791 	int			num_base_rels = 0;
1792 	Index		rti;
1793 
1794 	for (rti = 1; rti < root->simple_rel_array_size; rti++)
1795 	{
1796 		RelOptInfo *brel = root->simple_rel_array[rti];
1797 
1798 		if (brel == NULL)
1799 			continue;
1800 
1801 		/* ignore RTEs that are "other rels" */
1802 		if (brel->reloptkind == RELOPT_BASEREL)
1803 			if (++num_base_rels > 1)
1804 				return true;
1805 	}
1806 	return false;
1807 }
1808 
1809 /*
1810  * set_subquery_pathlist
1811  *		Generate SubqueryScan access paths for a subquery RTE
1812  *
1813  * We don't currently support generating parameterized paths for subqueries
1814  * by pushing join clauses down into them; it seems too expensive to re-plan
1815  * the subquery multiple times to consider different alternatives.
1816  * (XXX that could stand to be reconsidered, now that we use Paths.)
1817  * So the paths made here will be parameterized if the subquery contains
1818  * LATERAL references, otherwise not.  As long as that's true, there's no need
1819  * for a separate set_subquery_size phase: just make the paths right away.
1820  */
1821 static void
set_subquery_pathlist(PlannerInfo * root,RelOptInfo * rel,Index rti,RangeTblEntry * rte)1822 set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
1823 					  Index rti, RangeTblEntry *rte)
1824 {
1825 	Query	   *parse = root->parse;
1826 	Query	   *subquery = rte->subquery;
1827 	Relids		required_outer;
1828 	pushdown_safety_info safetyInfo;
1829 	double		tuple_fraction;
1830 	RelOptInfo *sub_final_rel;
1831 	ListCell   *lc;
1832 
1833 	/*
1834 	 * Must copy the Query so that planning doesn't mess up the RTE contents
1835 	 * (really really need to fix the planner to not scribble on its input,
1836 	 * someday ... but see remove_unused_subquery_outputs to start with).
1837 	 */
1838 	subquery = copyObject(subquery);
1839 
1840 	/*
1841 	 * If it's a LATERAL subquery, it might contain some Vars of the current
1842 	 * query level, requiring it to be treated as parameterized, even though
1843 	 * we don't support pushing down join quals into subqueries.
1844 	 */
1845 	required_outer = rel->lateral_relids;
1846 
1847 	/*
1848 	 * Zero out result area for subquery_is_pushdown_safe, so that it can set
1849 	 * flags as needed while recursing.  In particular, we need a workspace
1850 	 * for keeping track of unsafe-to-reference columns.  unsafeColumns[i]
1851 	 * will be set TRUE if we find that output column i of the subquery is
1852 	 * unsafe to use in a pushed-down qual.
1853 	 */
1854 	memset(&safetyInfo, 0, sizeof(safetyInfo));
1855 	safetyInfo.unsafeColumns = (bool *)
1856 		palloc0((list_length(subquery->targetList) + 1) * sizeof(bool));
1857 
1858 	/*
1859 	 * If the subquery has the "security_barrier" flag, it means the subquery
1860 	 * originated from a view that must enforce row level security.  Then we
1861 	 * must not push down quals that contain leaky functions.  (Ideally this
1862 	 * would be checked inside subquery_is_pushdown_safe, but since we don't
1863 	 * currently pass the RTE to that function, we must do it here.)
1864 	 */
1865 	safetyInfo.unsafeLeaky = rte->security_barrier;
1866 
1867 	/*
1868 	 * If there are any restriction clauses that have been attached to the
1869 	 * subquery relation, consider pushing them down to become WHERE or HAVING
1870 	 * quals of the subquery itself.  This transformation is useful because it
1871 	 * may allow us to generate a better plan for the subquery than evaluating
1872 	 * all the subquery output rows and then filtering them.
1873 	 *
1874 	 * There are several cases where we cannot push down clauses. Restrictions
1875 	 * involving the subquery are checked by subquery_is_pushdown_safe().
1876 	 * Restrictions on individual clauses are checked by
1877 	 * qual_is_pushdown_safe().  Also, we don't want to push down
1878 	 * pseudoconstant clauses; better to have the gating node above the
1879 	 * subquery.
1880 	 *
1881 	 * Non-pushed-down clauses will get evaluated as qpquals of the
1882 	 * SubqueryScan node.
1883 	 *
1884 	 * XXX Are there any cases where we want to make a policy decision not to
1885 	 * push down a pushable qual, because it'd result in a worse plan?
1886 	 */
1887 	if (rel->baserestrictinfo != NIL &&
1888 		subquery_is_pushdown_safe(subquery, subquery, &safetyInfo))
1889 	{
1890 		/* OK to consider pushing down individual quals */
1891 		List	   *upperrestrictlist = NIL;
1892 		ListCell   *l;
1893 
1894 		foreach(l, rel->baserestrictinfo)
1895 		{
1896 			RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1897 			Node	   *clause = (Node *) rinfo->clause;
1898 
1899 			if (!rinfo->pseudoconstant &&
1900 				qual_is_pushdown_safe(subquery, rti, clause, &safetyInfo))
1901 			{
1902 				/* Push it down */
1903 				subquery_push_qual(subquery, rte, rti, clause);
1904 			}
1905 			else
1906 			{
1907 				/* Keep it in the upper query */
1908 				upperrestrictlist = lappend(upperrestrictlist, rinfo);
1909 			}
1910 		}
1911 		rel->baserestrictinfo = upperrestrictlist;
1912 		/* We don't bother recomputing baserestrict_min_security */
1913 	}
1914 
1915 	pfree(safetyInfo.unsafeColumns);
1916 
1917 	/*
1918 	 * The upper query might not use all the subquery's output columns; if
1919 	 * not, we can simplify.
1920 	 */
1921 	remove_unused_subquery_outputs(subquery, rel);
1922 
1923 	/*
1924 	 * We can safely pass the outer tuple_fraction down to the subquery if the
1925 	 * outer level has no joining, aggregation, or sorting to do. Otherwise
1926 	 * we'd better tell the subquery to plan for full retrieval. (XXX This
1927 	 * could probably be made more intelligent ...)
1928 	 */
1929 	if (parse->hasAggs ||
1930 		parse->groupClause ||
1931 		parse->groupingSets ||
1932 		parse->havingQual ||
1933 		parse->distinctClause ||
1934 		parse->sortClause ||
1935 		has_multiple_baserels(root))
1936 		tuple_fraction = 0.0;	/* default case */
1937 	else
1938 		tuple_fraction = root->tuple_fraction;
1939 
1940 	/* plan_params should not be in use in current query level */
1941 	Assert(root->plan_params == NIL);
1942 
1943 	/* Generate a subroot and Paths for the subquery */
1944 	rel->subroot = subquery_planner(root->glob, subquery,
1945 									root,
1946 									false, tuple_fraction);
1947 
1948 	/* Isolate the params needed by this specific subplan */
1949 	rel->subplan_params = root->plan_params;
1950 	root->plan_params = NIL;
1951 
1952 	/*
1953 	 * It's possible that constraint exclusion proved the subquery empty. If
1954 	 * so, it's desirable to produce an unadorned dummy path so that we will
1955 	 * recognize appropriate optimizations at this query level.
1956 	 */
1957 	sub_final_rel = fetch_upper_rel(rel->subroot, UPPERREL_FINAL, NULL);
1958 
1959 	if (IS_DUMMY_REL(sub_final_rel))
1960 	{
1961 		set_dummy_rel_pathlist(rel);
1962 		return;
1963 	}
1964 
1965 	/*
1966 	 * Mark rel with estimated output rows, width, etc.  Note that we have to
1967 	 * do this before generating outer-query paths, else cost_subqueryscan is
1968 	 * not happy.
1969 	 */
1970 	set_subquery_size_estimates(root, rel);
1971 
1972 	/*
1973 	 * For each Path that subquery_planner produced, make a SubqueryScanPath
1974 	 * in the outer query.
1975 	 */
1976 	foreach(lc, sub_final_rel->pathlist)
1977 	{
1978 		Path	   *subpath = (Path *) lfirst(lc);
1979 		List	   *pathkeys;
1980 
1981 		/* Convert subpath's pathkeys to outer representation */
1982 		pathkeys = convert_subquery_pathkeys(root,
1983 											 rel,
1984 											 subpath->pathkeys,
1985 											 make_tlist_from_pathtarget(subpath->pathtarget));
1986 
1987 		/* Generate outer path using this subpath */
1988 		add_path(rel, (Path *)
1989 				 create_subqueryscan_path(root, rel, subpath,
1990 										  pathkeys, required_outer));
1991 	}
1992 }
1993 
1994 /*
1995  * set_function_pathlist
1996  *		Build the (single) access path for a function RTE
1997  */
1998 static void
set_function_pathlist(PlannerInfo * root,RelOptInfo * rel,RangeTblEntry * rte)1999 set_function_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
2000 {
2001 	Relids		required_outer;
2002 	List	   *pathkeys = NIL;
2003 
2004 	/*
2005 	 * We don't support pushing join clauses into the quals of a function
2006 	 * scan, but it could still have required parameterization due to LATERAL
2007 	 * refs in the function expression.
2008 	 */
2009 	required_outer = rel->lateral_relids;
2010 
2011 	/*
2012 	 * The result is considered unordered unless ORDINALITY was used, in which
2013 	 * case it is ordered by the ordinal column (the last one).  See if we
2014 	 * care, by checking for uses of that Var in equivalence classes.
2015 	 */
2016 	if (rte->funcordinality)
2017 	{
2018 		AttrNumber	ordattno = rel->max_attr;
2019 		Var		   *var = NULL;
2020 		ListCell   *lc;
2021 
2022 		/*
2023 		 * Is there a Var for it in rel's targetlist?  If not, the query did
2024 		 * not reference the ordinality column, or at least not in any way
2025 		 * that would be interesting for sorting.
2026 		 */
2027 		foreach(lc, rel->reltarget->exprs)
2028 		{
2029 			Var		   *node = (Var *) lfirst(lc);
2030 
2031 			/* checking varno/varlevelsup is just paranoia */
2032 			if (IsA(node, Var) &&
2033 				node->varattno == ordattno &&
2034 				node->varno == rel->relid &&
2035 				node->varlevelsup == 0)
2036 			{
2037 				var = node;
2038 				break;
2039 			}
2040 		}
2041 
2042 		/*
2043 		 * Try to build pathkeys for this Var with int8 sorting.  We tell
2044 		 * build_expression_pathkey not to build any new equivalence class; if
2045 		 * the Var isn't already mentioned in some EC, it means that nothing
2046 		 * cares about the ordering.
2047 		 */
2048 		if (var)
2049 			pathkeys = build_expression_pathkey(root,
2050 												(Expr *) var,
2051 												NULL,	/* below outer joins */
2052 												Int8LessOperator,
2053 												rel->relids,
2054 												false);
2055 	}
2056 
2057 	/* Generate appropriate path */
2058 	add_path(rel, create_functionscan_path(root, rel,
2059 										   pathkeys, required_outer));
2060 }
2061 
2062 /*
2063  * set_values_pathlist
2064  *		Build the (single) access path for a VALUES RTE
2065  */
2066 static void
set_values_pathlist(PlannerInfo * root,RelOptInfo * rel,RangeTblEntry * rte)2067 set_values_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
2068 {
2069 	Relids		required_outer;
2070 
2071 	/*
2072 	 * We don't support pushing join clauses into the quals of a values scan,
2073 	 * but it could still have required parameterization due to LATERAL refs
2074 	 * in the values expressions.
2075 	 */
2076 	required_outer = rel->lateral_relids;
2077 
2078 	/* Generate appropriate path */
2079 	add_path(rel, create_valuesscan_path(root, rel, required_outer));
2080 }
2081 
2082 /*
2083  * set_tablefunc_pathlist
2084  *		Build the (single) access path for a table func RTE
2085  */
2086 static void
set_tablefunc_pathlist(PlannerInfo * root,RelOptInfo * rel,RangeTblEntry * rte)2087 set_tablefunc_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
2088 {
2089 	Relids		required_outer;
2090 
2091 	/*
2092 	 * We don't support pushing join clauses into the quals of a tablefunc
2093 	 * scan, but it could still have required parameterization due to LATERAL
2094 	 * refs in the function expression.
2095 	 */
2096 	required_outer = rel->lateral_relids;
2097 
2098 	/* Generate appropriate path */
2099 	add_path(rel, create_tablefuncscan_path(root, rel,
2100 											required_outer));
2101 }
2102 
2103 /*
2104  * set_cte_pathlist
2105  *		Build the (single) access path for a non-self-reference CTE RTE
2106  *
2107  * There's no need for a separate set_cte_size phase, since we don't
2108  * support join-qual-parameterized paths for CTEs.
2109  */
2110 static void
set_cte_pathlist(PlannerInfo * root,RelOptInfo * rel,RangeTblEntry * rte)2111 set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
2112 {
2113 	Plan	   *cteplan;
2114 	PlannerInfo *cteroot;
2115 	Index		levelsup;
2116 	int			ndx;
2117 	ListCell   *lc;
2118 	int			plan_id;
2119 	Relids		required_outer;
2120 
2121 	/*
2122 	 * Find the referenced CTE, and locate the plan previously made for it.
2123 	 */
2124 	levelsup = rte->ctelevelsup;
2125 	cteroot = root;
2126 	while (levelsup-- > 0)
2127 	{
2128 		cteroot = cteroot->parent_root;
2129 		if (!cteroot)			/* shouldn't happen */
2130 			elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
2131 	}
2132 
2133 	/*
2134 	 * Note: cte_plan_ids can be shorter than cteList, if we are still working
2135 	 * on planning the CTEs (ie, this is a side-reference from another CTE).
2136 	 * So we mustn't use forboth here.
2137 	 */
2138 	ndx = 0;
2139 	foreach(lc, cteroot->parse->cteList)
2140 	{
2141 		CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
2142 
2143 		if (strcmp(cte->ctename, rte->ctename) == 0)
2144 			break;
2145 		ndx++;
2146 	}
2147 	if (lc == NULL)				/* shouldn't happen */
2148 		elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
2149 	if (ndx >= list_length(cteroot->cte_plan_ids))
2150 		elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
2151 	plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
2152 	Assert(plan_id > 0);
2153 	cteplan = (Plan *) list_nth(root->glob->subplans, plan_id - 1);
2154 
2155 	/* Mark rel with estimated output rows, width, etc */
2156 	set_cte_size_estimates(root, rel, cteplan->plan_rows);
2157 
2158 	/*
2159 	 * We don't support pushing join clauses into the quals of a CTE scan, but
2160 	 * it could still have required parameterization due to LATERAL refs in
2161 	 * its tlist.
2162 	 */
2163 	required_outer = rel->lateral_relids;
2164 
2165 	/* Generate appropriate path */
2166 	add_path(rel, create_ctescan_path(root, rel, required_outer));
2167 }
2168 
2169 /*
2170  * set_namedtuplestore_pathlist
2171  *		Build the (single) access path for a named tuplestore RTE
2172  *
2173  * There's no need for a separate set_namedtuplestore_size phase, since we
2174  * don't support join-qual-parameterized paths for tuplestores.
2175  */
2176 static void
set_namedtuplestore_pathlist(PlannerInfo * root,RelOptInfo * rel,RangeTblEntry * rte)2177 set_namedtuplestore_pathlist(PlannerInfo *root, RelOptInfo *rel,
2178 							 RangeTblEntry *rte)
2179 {
2180 	Relids		required_outer;
2181 
2182 	/* Mark rel with estimated output rows, width, etc */
2183 	set_namedtuplestore_size_estimates(root, rel);
2184 
2185 	/*
2186 	 * We don't support pushing join clauses into the quals of a tuplestore
2187 	 * scan, but it could still have required parameterization due to LATERAL
2188 	 * refs in its tlist.
2189 	 */
2190 	required_outer = rel->lateral_relids;
2191 
2192 	/* Generate appropriate path */
2193 	add_path(rel, create_namedtuplestorescan_path(root, rel, required_outer));
2194 
2195 	/* Select cheapest path (pretty easy in this case...) */
2196 	set_cheapest(rel);
2197 }
2198 
2199 /*
2200  * set_worktable_pathlist
2201  *		Build the (single) access path for a self-reference CTE RTE
2202  *
2203  * There's no need for a separate set_worktable_size phase, since we don't
2204  * support join-qual-parameterized paths for CTEs.
2205  */
2206 static void
set_worktable_pathlist(PlannerInfo * root,RelOptInfo * rel,RangeTblEntry * rte)2207 set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
2208 {
2209 	Path	   *ctepath;
2210 	PlannerInfo *cteroot;
2211 	Index		levelsup;
2212 	Relids		required_outer;
2213 
2214 	/*
2215 	 * We need to find the non-recursive term's path, which is in the plan
2216 	 * level that's processing the recursive UNION, which is one level *below*
2217 	 * where the CTE comes from.
2218 	 */
2219 	levelsup = rte->ctelevelsup;
2220 	if (levelsup == 0)			/* shouldn't happen */
2221 		elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
2222 	levelsup--;
2223 	cteroot = root;
2224 	while (levelsup-- > 0)
2225 	{
2226 		cteroot = cteroot->parent_root;
2227 		if (!cteroot)			/* shouldn't happen */
2228 			elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
2229 	}
2230 	ctepath = cteroot->non_recursive_path;
2231 	if (!ctepath)				/* shouldn't happen */
2232 		elog(ERROR, "could not find path for CTE \"%s\"", rte->ctename);
2233 
2234 	/* Mark rel with estimated output rows, width, etc */
2235 	set_cte_size_estimates(root, rel, ctepath->rows);
2236 
2237 	/*
2238 	 * We don't support pushing join clauses into the quals of a worktable
2239 	 * scan, but it could still have required parameterization due to LATERAL
2240 	 * refs in its tlist.  (I'm not sure this is actually possible given the
2241 	 * restrictions on recursive references, but it's easy enough to support.)
2242 	 */
2243 	required_outer = rel->lateral_relids;
2244 
2245 	/* Generate appropriate path */
2246 	add_path(rel, create_worktablescan_path(root, rel, required_outer));
2247 }
2248 
2249 /*
2250  * generate_gather_paths
2251  *		Generate parallel access paths for a relation by pushing a Gather or
2252  *		Gather Merge on top of a partial path.
2253  *
2254  * This must not be called until after we're done creating all partial paths
2255  * for the specified relation.  (Otherwise, add_partial_path might delete a
2256  * path that some GatherPath or GatherMergePath has a reference to.)
2257  */
2258 void
generate_gather_paths(PlannerInfo * root,RelOptInfo * rel)2259 generate_gather_paths(PlannerInfo *root, RelOptInfo *rel)
2260 {
2261 	Path	   *cheapest_partial_path;
2262 	Path	   *simple_gather_path;
2263 	ListCell   *lc;
2264 
2265 	/* If there are no partial paths, there's nothing to do here. */
2266 	if (rel->partial_pathlist == NIL)
2267 		return;
2268 
2269 	/*
2270 	 * The output of Gather is always unsorted, so there's only one partial
2271 	 * path of interest: the cheapest one.  That will be the one at the front
2272 	 * of partial_pathlist because of the way add_partial_path works.
2273 	 */
2274 	cheapest_partial_path = linitial(rel->partial_pathlist);
2275 	simple_gather_path = (Path *)
2276 		create_gather_path(root, rel, cheapest_partial_path, rel->reltarget,
2277 						   NULL, NULL);
2278 	add_path(rel, simple_gather_path);
2279 
2280 	/*
2281 	 * For each useful ordering, we can consider an order-preserving Gather
2282 	 * Merge.
2283 	 */
2284 	foreach(lc, rel->partial_pathlist)
2285 	{
2286 		Path	   *subpath = (Path *) lfirst(lc);
2287 		GatherMergePath *path;
2288 
2289 		if (subpath->pathkeys == NIL)
2290 			continue;
2291 
2292 		path = create_gather_merge_path(root, rel, subpath, rel->reltarget,
2293 										subpath->pathkeys, NULL, NULL);
2294 		add_path(rel, &path->path);
2295 	}
2296 }
2297 
2298 /*
2299  * make_rel_from_joinlist
2300  *	  Build access paths using a "joinlist" to guide the join path search.
2301  *
2302  * See comments for deconstruct_jointree() for definition of the joinlist
2303  * data structure.
2304  */
2305 static RelOptInfo *
make_rel_from_joinlist(PlannerInfo * root,List * joinlist)2306 make_rel_from_joinlist(PlannerInfo *root, List *joinlist)
2307 {
2308 	int			levels_needed;
2309 	List	   *initial_rels;
2310 	ListCell   *jl;
2311 
2312 	/*
2313 	 * Count the number of child joinlist nodes.  This is the depth of the
2314 	 * dynamic-programming algorithm we must employ to consider all ways of
2315 	 * joining the child nodes.
2316 	 */
2317 	levels_needed = list_length(joinlist);
2318 
2319 	if (levels_needed <= 0)
2320 		return NULL;			/* nothing to do? */
2321 
2322 	/*
2323 	 * Construct a list of rels corresponding to the child joinlist nodes.
2324 	 * This may contain both base rels and rels constructed according to
2325 	 * sub-joinlists.
2326 	 */
2327 	initial_rels = NIL;
2328 	foreach(jl, joinlist)
2329 	{
2330 		Node	   *jlnode = (Node *) lfirst(jl);
2331 		RelOptInfo *thisrel;
2332 
2333 		if (IsA(jlnode, RangeTblRef))
2334 		{
2335 			int			varno = ((RangeTblRef *) jlnode)->rtindex;
2336 
2337 			thisrel = find_base_rel(root, varno);
2338 		}
2339 		else if (IsA(jlnode, List))
2340 		{
2341 			/* Recurse to handle subproblem */
2342 			thisrel = make_rel_from_joinlist(root, (List *) jlnode);
2343 		}
2344 		else
2345 		{
2346 			elog(ERROR, "unrecognized joinlist node type: %d",
2347 				 (int) nodeTag(jlnode));
2348 			thisrel = NULL;		/* keep compiler quiet */
2349 		}
2350 
2351 		initial_rels = lappend(initial_rels, thisrel);
2352 	}
2353 
2354 	if (levels_needed == 1)
2355 	{
2356 		/*
2357 		 * Single joinlist node, so we're done.
2358 		 */
2359 		return (RelOptInfo *) linitial(initial_rels);
2360 	}
2361 	else
2362 	{
2363 		/*
2364 		 * Consider the different orders in which we could join the rels,
2365 		 * using a plugin, GEQO, or the regular join search code.
2366 		 *
2367 		 * We put the initial_rels list into a PlannerInfo field because
2368 		 * has_legal_joinclause() needs to look at it (ugly :-().
2369 		 */
2370 		root->initial_rels = initial_rels;
2371 
2372 		if (join_search_hook)
2373 			return (*join_search_hook) (root, levels_needed, initial_rels);
2374 		else if (enable_geqo && levels_needed >= geqo_threshold)
2375 			return geqo(root, levels_needed, initial_rels);
2376 		else
2377 			return standard_join_search(root, levels_needed, initial_rels);
2378 	}
2379 }
2380 
2381 /*
2382  * standard_join_search
2383  *	  Find possible joinpaths for a query by successively finding ways
2384  *	  to join component relations into join relations.
2385  *
2386  * 'levels_needed' is the number of iterations needed, ie, the number of
2387  *		independent jointree items in the query.  This is > 1.
2388  *
2389  * 'initial_rels' is a list of RelOptInfo nodes for each independent
2390  *		jointree item.  These are the components to be joined together.
2391  *		Note that levels_needed == list_length(initial_rels).
2392  *
2393  * Returns the final level of join relations, i.e., the relation that is
2394  * the result of joining all the original relations together.
2395  * At least one implementation path must be provided for this relation and
2396  * all required sub-relations.
2397  *
2398  * To support loadable plugins that modify planner behavior by changing the
2399  * join searching algorithm, we provide a hook variable that lets a plugin
2400  * replace or supplement this function.  Any such hook must return the same
2401  * final join relation as the standard code would, but it might have a
2402  * different set of implementation paths attached, and only the sub-joinrels
2403  * needed for these paths need have been instantiated.
2404  *
2405  * Note to plugin authors: the functions invoked during standard_join_search()
2406  * modify root->join_rel_list and root->join_rel_hash.  If you want to do more
2407  * than one join-order search, you'll probably need to save and restore the
2408  * original states of those data structures.  See geqo_eval() for an example.
2409  */
2410 RelOptInfo *
standard_join_search(PlannerInfo * root,int levels_needed,List * initial_rels)2411 standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
2412 {
2413 	int			lev;
2414 	RelOptInfo *rel;
2415 
2416 	/*
2417 	 * This function cannot be invoked recursively within any one planning
2418 	 * problem, so join_rel_level[] can't be in use already.
2419 	 */
2420 	Assert(root->join_rel_level == NULL);
2421 
2422 	/*
2423 	 * We employ a simple "dynamic programming" algorithm: we first find all
2424 	 * ways to build joins of two jointree items, then all ways to build joins
2425 	 * of three items (from two-item joins and single items), then four-item
2426 	 * joins, and so on until we have considered all ways to join all the
2427 	 * items into one rel.
2428 	 *
2429 	 * root->join_rel_level[j] is a list of all the j-item rels.  Initially we
2430 	 * set root->join_rel_level[1] to represent all the single-jointree-item
2431 	 * relations.
2432 	 */
2433 	root->join_rel_level = (List **) palloc0((levels_needed + 1) * sizeof(List *));
2434 
2435 	root->join_rel_level[1] = initial_rels;
2436 
2437 	for (lev = 2; lev <= levels_needed; lev++)
2438 	{
2439 		ListCell   *lc;
2440 
2441 		/*
2442 		 * Determine all possible pairs of relations to be joined at this
2443 		 * level, and build paths for making each one from every available
2444 		 * pair of lower-level relations.
2445 		 */
2446 		join_search_one_level(root, lev);
2447 
2448 		/*
2449 		 * Run generate_gather_paths() for each just-processed joinrel.  We
2450 		 * could not do this earlier because both regular and partial paths
2451 		 * can get added to a particular joinrel at multiple times within
2452 		 * join_search_one_level.  After that, we're done creating paths for
2453 		 * the joinrel, so run set_cheapest().
2454 		 */
2455 		foreach(lc, root->join_rel_level[lev])
2456 		{
2457 			rel = (RelOptInfo *) lfirst(lc);
2458 
2459 			/* Create GatherPaths for any useful partial paths for rel */
2460 			generate_gather_paths(root, rel);
2461 
2462 			/* Find and save the cheapest paths for this rel */
2463 			set_cheapest(rel);
2464 
2465 #ifdef OPTIMIZER_DEBUG
2466 			debug_print_rel(root, rel);
2467 #endif
2468 		}
2469 	}
2470 
2471 	/*
2472 	 * We should have a single rel at the final level.
2473 	 */
2474 	if (root->join_rel_level[levels_needed] == NIL)
2475 		elog(ERROR, "failed to build any %d-way joins", levels_needed);
2476 	Assert(list_length(root->join_rel_level[levels_needed]) == 1);
2477 
2478 	rel = (RelOptInfo *) linitial(root->join_rel_level[levels_needed]);
2479 
2480 	root->join_rel_level = NULL;
2481 
2482 	return rel;
2483 }
2484 
2485 /*****************************************************************************
2486  *			PUSHING QUALS DOWN INTO SUBQUERIES
2487  *****************************************************************************/
2488 
2489 /*
2490  * subquery_is_pushdown_safe - is a subquery safe for pushing down quals?
2491  *
2492  * subquery is the particular component query being checked.  topquery
2493  * is the top component of a set-operations tree (the same Query if no
2494  * set-op is involved).
2495  *
2496  * Conditions checked here:
2497  *
2498  * 1. If the subquery has a LIMIT clause, we must not push down any quals,
2499  * since that could change the set of rows returned.
2500  *
2501  * 2. If the subquery contains EXCEPT or EXCEPT ALL set ops we cannot push
2502  * quals into it, because that could change the results.
2503  *
2504  * 3. If the subquery uses DISTINCT, we cannot push volatile quals into it.
2505  * This is because upper-level quals should semantically be evaluated only
2506  * once per distinct row, not once per original row, and if the qual is
2507  * volatile then extra evaluations could change the results.  (This issue
2508  * does not apply to other forms of aggregation such as GROUP BY, because
2509  * when those are present we push into HAVING not WHERE, so that the quals
2510  * are still applied after aggregation.)
2511  *
2512  * 4. If the subquery contains window functions, we cannot push volatile quals
2513  * into it.  The issue here is a bit different from DISTINCT: a volatile qual
2514  * might succeed for some rows of a window partition and fail for others,
2515  * thereby changing the partition contents and thus the window functions'
2516  * results for rows that remain.
2517  *
2518  * 5. If the subquery contains any set-returning functions in its targetlist,
2519  * we cannot push volatile quals into it.  That would push them below the SRFs
2520  * and thereby change the number of times they are evaluated.  Also, a
2521  * volatile qual could succeed for some SRF output rows and fail for others,
2522  * a behavior that cannot occur if it's evaluated before SRF expansion.
2523  *
2524  * 6. If the subquery has nonempty grouping sets, we cannot push down any
2525  * quals.  The concern here is that a qual referencing a "constant" grouping
2526  * column could get constant-folded, which would be improper because the value
2527  * is potentially nullable by grouping-set expansion.  This restriction could
2528  * be removed if we had a parsetree representation that shows that such
2529  * grouping columns are not really constant.  (There are other ideas that
2530  * could be used to relax this restriction, but that's the approach most
2531  * likely to get taken in the future.  Note that there's not much to be gained
2532  * so long as subquery_planner can't move HAVING clauses to WHERE within such
2533  * a subquery.)
2534  *
2535  * In addition, we make several checks on the subquery's output columns to see
2536  * if it is safe to reference them in pushed-down quals.  If output column k
2537  * is found to be unsafe to reference, we set safetyInfo->unsafeColumns[k]
2538  * to TRUE, but we don't reject the subquery overall since column k might not
2539  * be referenced by some/all quals.  The unsafeColumns[] array will be
2540  * consulted later by qual_is_pushdown_safe().  It's better to do it this way
2541  * than to make the checks directly in qual_is_pushdown_safe(), because when
2542  * the subquery involves set operations we have to check the output
2543  * expressions in each arm of the set op.
2544  *
2545  * Note: pushing quals into a DISTINCT subquery is theoretically dubious:
2546  * we're effectively assuming that the quals cannot distinguish values that
2547  * the DISTINCT's equality operator sees as equal, yet there are many
2548  * counterexamples to that assumption.  However use of such a qual with a
2549  * DISTINCT subquery would be unsafe anyway, since there's no guarantee which
2550  * "equal" value will be chosen as the output value by the DISTINCT operation.
2551  * So we don't worry too much about that.  Another objection is that if the
2552  * qual is expensive to evaluate, running it for each original row might cost
2553  * more than we save by eliminating rows before the DISTINCT step.  But it
2554  * would be very hard to estimate that at this stage, and in practice pushdown
2555  * seldom seems to make things worse, so we ignore that problem too.
2556  *
2557  * Note: likewise, pushing quals into a subquery with window functions is a
2558  * bit dubious: the quals might remove some rows of a window partition while
2559  * leaving others, causing changes in the window functions' results for the
2560  * surviving rows.  We insist that such a qual reference only partitioning
2561  * columns, but again that only protects us if the qual does not distinguish
2562  * values that the partitioning equality operator sees as equal.  The risks
2563  * here are perhaps larger than for DISTINCT, since no de-duplication of rows
2564  * occurs and thus there is no theoretical problem with such a qual.  But
2565  * we'll do this anyway because the potential performance benefits are very
2566  * large, and we've seen no field complaints about the longstanding comparable
2567  * behavior with DISTINCT.
2568  */
2569 static bool
subquery_is_pushdown_safe(Query * subquery,Query * topquery,pushdown_safety_info * safetyInfo)2570 subquery_is_pushdown_safe(Query *subquery, Query *topquery,
2571 						  pushdown_safety_info *safetyInfo)
2572 {
2573 	SetOperationStmt *topop;
2574 
2575 	/* Check point 1 */
2576 	if (subquery->limitOffset != NULL || subquery->limitCount != NULL)
2577 		return false;
2578 
2579 	/* Check point 6 */
2580 	if (subquery->groupClause && subquery->groupingSets)
2581 		return false;
2582 
2583 	/* Check points 3, 4, and 5 */
2584 	if (subquery->distinctClause ||
2585 		subquery->hasWindowFuncs ||
2586 		subquery->hasTargetSRFs)
2587 		safetyInfo->unsafeVolatile = true;
2588 
2589 	/*
2590 	 * If we're at a leaf query, check for unsafe expressions in its target
2591 	 * list, and mark any unsafe ones in unsafeColumns[].  (Non-leaf nodes in
2592 	 * setop trees have only simple Vars in their tlists, so no need to check
2593 	 * them.)
2594 	 */
2595 	if (subquery->setOperations == NULL)
2596 		check_output_expressions(subquery, safetyInfo);
2597 
2598 	/* Are we at top level, or looking at a setop component? */
2599 	if (subquery == topquery)
2600 	{
2601 		/* Top level, so check any component queries */
2602 		if (subquery->setOperations != NULL)
2603 			if (!recurse_pushdown_safe(subquery->setOperations, topquery,
2604 									   safetyInfo))
2605 				return false;
2606 	}
2607 	else
2608 	{
2609 		/* Setop component must not have more components (too weird) */
2610 		if (subquery->setOperations != NULL)
2611 			return false;
2612 		/* Check whether setop component output types match top level */
2613 		topop = castNode(SetOperationStmt, topquery->setOperations);
2614 		Assert(topop);
2615 		compare_tlist_datatypes(subquery->targetList,
2616 								topop->colTypes,
2617 								safetyInfo);
2618 	}
2619 	return true;
2620 }
2621 
2622 /*
2623  * Helper routine to recurse through setOperations tree
2624  */
2625 static bool
recurse_pushdown_safe(Node * setOp,Query * topquery,pushdown_safety_info * safetyInfo)2626 recurse_pushdown_safe(Node *setOp, Query *topquery,
2627 					  pushdown_safety_info *safetyInfo)
2628 {
2629 	if (IsA(setOp, RangeTblRef))
2630 	{
2631 		RangeTblRef *rtr = (RangeTblRef *) setOp;
2632 		RangeTblEntry *rte = rt_fetch(rtr->rtindex, topquery->rtable);
2633 		Query	   *subquery = rte->subquery;
2634 
2635 		Assert(subquery != NULL);
2636 		return subquery_is_pushdown_safe(subquery, topquery, safetyInfo);
2637 	}
2638 	else if (IsA(setOp, SetOperationStmt))
2639 	{
2640 		SetOperationStmt *op = (SetOperationStmt *) setOp;
2641 
2642 		/* EXCEPT is no good (point 2 for subquery_is_pushdown_safe) */
2643 		if (op->op == SETOP_EXCEPT)
2644 			return false;
2645 		/* Else recurse */
2646 		if (!recurse_pushdown_safe(op->larg, topquery, safetyInfo))
2647 			return false;
2648 		if (!recurse_pushdown_safe(op->rarg, topquery, safetyInfo))
2649 			return false;
2650 	}
2651 	else
2652 	{
2653 		elog(ERROR, "unrecognized node type: %d",
2654 			 (int) nodeTag(setOp));
2655 	}
2656 	return true;
2657 }
2658 
2659 /*
2660  * check_output_expressions - check subquery's output expressions for safety
2661  *
2662  * There are several cases in which it's unsafe to push down an upper-level
2663  * qual if it references a particular output column of a subquery.  We check
2664  * each output column of the subquery and set unsafeColumns[k] to TRUE if
2665  * that column is unsafe for a pushed-down qual to reference.  The conditions
2666  * checked here are:
2667  *
2668  * 1. We must not push down any quals that refer to subselect outputs that
2669  * return sets, else we'd introduce functions-returning-sets into the
2670  * subquery's WHERE/HAVING quals.
2671  *
2672  * 2. We must not push down any quals that refer to subselect outputs that
2673  * contain volatile functions, for fear of introducing strange results due
2674  * to multiple evaluation of a volatile function.
2675  *
2676  * 3. If the subquery uses DISTINCT ON, we must not push down any quals that
2677  * refer to non-DISTINCT output columns, because that could change the set
2678  * of rows returned.  (This condition is vacuous for DISTINCT, because then
2679  * there are no non-DISTINCT output columns, so we needn't check.  Note that
2680  * subquery_is_pushdown_safe already reported that we can't use volatile
2681  * quals if there's DISTINCT or DISTINCT ON.)
2682  *
2683  * 4. If the subquery has any window functions, we must not push down quals
2684  * that reference any output columns that are not listed in all the subquery's
2685  * window PARTITION BY clauses.  We can push down quals that use only
2686  * partitioning columns because they should succeed or fail identically for
2687  * every row of any one window partition, and totally excluding some
2688  * partitions will not change a window function's results for remaining
2689  * partitions.  (Again, this also requires nonvolatile quals, but
2690  * subquery_is_pushdown_safe handles that.)
2691  */
2692 static void
check_output_expressions(Query * subquery,pushdown_safety_info * safetyInfo)2693 check_output_expressions(Query *subquery, pushdown_safety_info *safetyInfo)
2694 {
2695 	ListCell   *lc;
2696 
2697 	foreach(lc, subquery->targetList)
2698 	{
2699 		TargetEntry *tle = (TargetEntry *) lfirst(lc);
2700 
2701 		if (tle->resjunk)
2702 			continue;			/* ignore resjunk columns */
2703 
2704 		/* We need not check further if output col is already known unsafe */
2705 		if (safetyInfo->unsafeColumns[tle->resno])
2706 			continue;
2707 
2708 		/* Functions returning sets are unsafe (point 1) */
2709 		if (subquery->hasTargetSRFs &&
2710 			expression_returns_set((Node *) tle->expr))
2711 		{
2712 			safetyInfo->unsafeColumns[tle->resno] = true;
2713 			continue;
2714 		}
2715 
2716 		/* Volatile functions are unsafe (point 2) */
2717 		if (contain_volatile_functions((Node *) tle->expr))
2718 		{
2719 			safetyInfo->unsafeColumns[tle->resno] = true;
2720 			continue;
2721 		}
2722 
2723 		/* If subquery uses DISTINCT ON, check point 3 */
2724 		if (subquery->hasDistinctOn &&
2725 			!targetIsInSortList(tle, InvalidOid, subquery->distinctClause))
2726 		{
2727 			/* non-DISTINCT column, so mark it unsafe */
2728 			safetyInfo->unsafeColumns[tle->resno] = true;
2729 			continue;
2730 		}
2731 
2732 		/* If subquery uses window functions, check point 4 */
2733 		if (subquery->hasWindowFuncs &&
2734 			!targetIsInAllPartitionLists(tle, subquery))
2735 		{
2736 			/* not present in all PARTITION BY clauses, so mark it unsafe */
2737 			safetyInfo->unsafeColumns[tle->resno] = true;
2738 			continue;
2739 		}
2740 	}
2741 }
2742 
2743 /*
2744  * For subqueries using UNION/UNION ALL/INTERSECT/INTERSECT ALL, we can
2745  * push quals into each component query, but the quals can only reference
2746  * subquery columns that suffer no type coercions in the set operation.
2747  * Otherwise there are possible semantic gotchas.  So, we check the
2748  * component queries to see if any of them have output types different from
2749  * the top-level setop outputs.  unsafeColumns[k] is set true if column k
2750  * has different type in any component.
2751  *
2752  * We don't have to care about typmods here: the only allowed difference
2753  * between set-op input and output typmods is input is a specific typmod
2754  * and output is -1, and that does not require a coercion.
2755  *
2756  * tlist is a subquery tlist.
2757  * colTypes is an OID list of the top-level setop's output column types.
2758  * safetyInfo->unsafeColumns[] is the result array.
2759  */
2760 static void
compare_tlist_datatypes(List * tlist,List * colTypes,pushdown_safety_info * safetyInfo)2761 compare_tlist_datatypes(List *tlist, List *colTypes,
2762 						pushdown_safety_info *safetyInfo)
2763 {
2764 	ListCell   *l;
2765 	ListCell   *colType = list_head(colTypes);
2766 
2767 	foreach(l, tlist)
2768 	{
2769 		TargetEntry *tle = (TargetEntry *) lfirst(l);
2770 
2771 		if (tle->resjunk)
2772 			continue;			/* ignore resjunk columns */
2773 		if (colType == NULL)
2774 			elog(ERROR, "wrong number of tlist entries");
2775 		if (exprType((Node *) tle->expr) != lfirst_oid(colType))
2776 			safetyInfo->unsafeColumns[tle->resno] = true;
2777 		colType = lnext(colType);
2778 	}
2779 	if (colType != NULL)
2780 		elog(ERROR, "wrong number of tlist entries");
2781 }
2782 
2783 /*
2784  * targetIsInAllPartitionLists
2785  *		True if the TargetEntry is listed in the PARTITION BY clause
2786  *		of every window defined in the query.
2787  *
2788  * It would be safe to ignore windows not actually used by any window
2789  * function, but it's not easy to get that info at this stage; and it's
2790  * unlikely to be useful to spend any extra cycles getting it, since
2791  * unreferenced window definitions are probably infrequent in practice.
2792  */
2793 static bool
targetIsInAllPartitionLists(TargetEntry * tle,Query * query)2794 targetIsInAllPartitionLists(TargetEntry *tle, Query *query)
2795 {
2796 	ListCell   *lc;
2797 
2798 	foreach(lc, query->windowClause)
2799 	{
2800 		WindowClause *wc = (WindowClause *) lfirst(lc);
2801 
2802 		if (!targetIsInSortList(tle, InvalidOid, wc->partitionClause))
2803 			return false;
2804 	}
2805 	return true;
2806 }
2807 
2808 /*
2809  * qual_is_pushdown_safe - is a particular qual safe to push down?
2810  *
2811  * qual is a restriction clause applying to the given subquery (whose RTE
2812  * has index rti in the parent query).
2813  *
2814  * Conditions checked here:
2815  *
2816  * 1. The qual must not contain any subselects (mainly because I'm not sure
2817  * it will work correctly: sublinks will already have been transformed into
2818  * subplans in the qual, but not in the subquery).
2819  *
2820  * 2. If unsafeVolatile is set, the qual must not contain any volatile
2821  * functions.
2822  *
2823  * 3. If unsafeLeaky is set, the qual must not contain any leaky functions
2824  * that are passed Var nodes, and therefore might reveal values from the
2825  * subquery as side effects.
2826  *
2827  * 4. The qual must not refer to the whole-row output of the subquery
2828  * (since there is no easy way to name that within the subquery itself).
2829  *
2830  * 5. The qual must not refer to any subquery output columns that were
2831  * found to be unsafe to reference by subquery_is_pushdown_safe().
2832  */
2833 static bool
qual_is_pushdown_safe(Query * subquery,Index rti,Node * qual,pushdown_safety_info * safetyInfo)2834 qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
2835 					  pushdown_safety_info *safetyInfo)
2836 {
2837 	bool		safe = true;
2838 	List	   *vars;
2839 	ListCell   *vl;
2840 
2841 	/* Refuse subselects (point 1) */
2842 	if (contain_subplans(qual))
2843 		return false;
2844 
2845 	/* Refuse volatile quals if we found they'd be unsafe (point 2) */
2846 	if (safetyInfo->unsafeVolatile &&
2847 		contain_volatile_functions(qual))
2848 		return false;
2849 
2850 	/* Refuse leaky quals if told to (point 3) */
2851 	if (safetyInfo->unsafeLeaky &&
2852 		contain_leaked_vars(qual))
2853 		return false;
2854 
2855 	/*
2856 	 * It would be unsafe to push down window function calls, but at least for
2857 	 * the moment we could never see any in a qual anyhow.  (The same applies
2858 	 * to aggregates, which we check for in pull_var_clause below.)
2859 	 */
2860 	Assert(!contain_window_function(qual));
2861 
2862 	/*
2863 	 * Examine all Vars used in clause.  Since it's a restriction clause, all
2864 	 * such Vars must refer to subselect output columns ... unless this is
2865 	 * part of a LATERAL subquery, in which case there could be lateral
2866 	 * references.
2867 	 */
2868 	vars = pull_var_clause(qual, PVC_INCLUDE_PLACEHOLDERS);
2869 	foreach(vl, vars)
2870 	{
2871 		Var		   *var = (Var *) lfirst(vl);
2872 
2873 		/*
2874 		 * XXX Punt if we find any PlaceHolderVars in the restriction clause.
2875 		 * It's not clear whether a PHV could safely be pushed down, and even
2876 		 * less clear whether such a situation could arise in any cases of
2877 		 * practical interest anyway.  So for the moment, just refuse to push
2878 		 * down.
2879 		 */
2880 		if (!IsA(var, Var))
2881 		{
2882 			safe = false;
2883 			break;
2884 		}
2885 
2886 		/*
2887 		 * Punt if we find any lateral references.  It would be safe to push
2888 		 * these down, but we'd have to convert them into outer references,
2889 		 * which subquery_push_qual lacks the infrastructure to do.  The case
2890 		 * arises so seldom that it doesn't seem worth working hard on.
2891 		 */
2892 		if (var->varno != rti)
2893 		{
2894 			safe = false;
2895 			break;
2896 		}
2897 
2898 		/* Subqueries have no system columns */
2899 		Assert(var->varattno >= 0);
2900 
2901 		/* Check point 4 */
2902 		if (var->varattno == 0)
2903 		{
2904 			safe = false;
2905 			break;
2906 		}
2907 
2908 		/* Check point 5 */
2909 		if (safetyInfo->unsafeColumns[var->varattno])
2910 		{
2911 			safe = false;
2912 			break;
2913 		}
2914 	}
2915 
2916 	list_free(vars);
2917 
2918 	return safe;
2919 }
2920 
2921 /*
2922  * subquery_push_qual - push down a qual that we have determined is safe
2923  */
2924 static void
subquery_push_qual(Query * subquery,RangeTblEntry * rte,Index rti,Node * qual)2925 subquery_push_qual(Query *subquery, RangeTblEntry *rte, Index rti, Node *qual)
2926 {
2927 	if (subquery->setOperations != NULL)
2928 	{
2929 		/* Recurse to push it separately to each component query */
2930 		recurse_push_qual(subquery->setOperations, subquery,
2931 						  rte, rti, qual);
2932 	}
2933 	else
2934 	{
2935 		/*
2936 		 * We need to replace Vars in the qual (which must refer to outputs of
2937 		 * the subquery) with copies of the subquery's targetlist expressions.
2938 		 * Note that at this point, any uplevel Vars in the qual should have
2939 		 * been replaced with Params, so they need no work.
2940 		 *
2941 		 * This step also ensures that when we are pushing into a setop tree,
2942 		 * each component query gets its own copy of the qual.
2943 		 */
2944 		qual = ReplaceVarsFromTargetList(qual, rti, 0, rte,
2945 										 subquery->targetList,
2946 										 REPLACEVARS_REPORT_ERROR, 0,
2947 										 &subquery->hasSubLinks);
2948 
2949 		/*
2950 		 * Now attach the qual to the proper place: normally WHERE, but if the
2951 		 * subquery uses grouping or aggregation, put it in HAVING (since the
2952 		 * qual really refers to the group-result rows).
2953 		 */
2954 		if (subquery->hasAggs || subquery->groupClause || subquery->groupingSets || subquery->havingQual)
2955 			subquery->havingQual = make_and_qual(subquery->havingQual, qual);
2956 		else
2957 			subquery->jointree->quals =
2958 				make_and_qual(subquery->jointree->quals, qual);
2959 
2960 		/*
2961 		 * We need not change the subquery's hasAggs or hasSubLinks flags,
2962 		 * since we can't be pushing down any aggregates that weren't there
2963 		 * before, and we don't push down subselects at all.
2964 		 */
2965 	}
2966 }
2967 
2968 /*
2969  * Helper routine to recurse through setOperations tree
2970  */
2971 static void
recurse_push_qual(Node * setOp,Query * topquery,RangeTblEntry * rte,Index rti,Node * qual)2972 recurse_push_qual(Node *setOp, Query *topquery,
2973 				  RangeTblEntry *rte, Index rti, Node *qual)
2974 {
2975 	if (IsA(setOp, RangeTblRef))
2976 	{
2977 		RangeTblRef *rtr = (RangeTblRef *) setOp;
2978 		RangeTblEntry *subrte = rt_fetch(rtr->rtindex, topquery->rtable);
2979 		Query	   *subquery = subrte->subquery;
2980 
2981 		Assert(subquery != NULL);
2982 		subquery_push_qual(subquery, rte, rti, qual);
2983 	}
2984 	else if (IsA(setOp, SetOperationStmt))
2985 	{
2986 		SetOperationStmt *op = (SetOperationStmt *) setOp;
2987 
2988 		recurse_push_qual(op->larg, topquery, rte, rti, qual);
2989 		recurse_push_qual(op->rarg, topquery, rte, rti, qual);
2990 	}
2991 	else
2992 	{
2993 		elog(ERROR, "unrecognized node type: %d",
2994 			 (int) nodeTag(setOp));
2995 	}
2996 }
2997 
2998 /*****************************************************************************
2999  *			SIMPLIFYING SUBQUERY TARGETLISTS
3000  *****************************************************************************/
3001 
3002 /*
3003  * remove_unused_subquery_outputs
3004  *		Remove subquery targetlist items we don't need
3005  *
3006  * It's possible, even likely, that the upper query does not read all the
3007  * output columns of the subquery.  We can remove any such outputs that are
3008  * not needed by the subquery itself (e.g., as sort/group columns) and do not
3009  * affect semantics otherwise (e.g., volatile functions can't be removed).
3010  * This is useful not only because we might be able to remove expensive-to-
3011  * compute expressions, but because deletion of output columns might allow
3012  * optimizations such as join removal to occur within the subquery.
3013  *
3014  * To avoid affecting column numbering in the targetlist, we don't physically
3015  * remove unused tlist entries, but rather replace their expressions with NULL
3016  * constants.  This is implemented by modifying subquery->targetList.
3017  */
3018 static void
remove_unused_subquery_outputs(Query * subquery,RelOptInfo * rel)3019 remove_unused_subquery_outputs(Query *subquery, RelOptInfo *rel)
3020 {
3021 	Bitmapset  *attrs_used = NULL;
3022 	ListCell   *lc;
3023 
3024 	/*
3025 	 * Do nothing if subquery has UNION/INTERSECT/EXCEPT: in principle we
3026 	 * could update all the child SELECTs' tlists, but it seems not worth the
3027 	 * trouble presently.
3028 	 */
3029 	if (subquery->setOperations)
3030 		return;
3031 
3032 	/*
3033 	 * If subquery has regular DISTINCT (not DISTINCT ON), we're wasting our
3034 	 * time: all its output columns must be used in the distinctClause.
3035 	 */
3036 	if (subquery->distinctClause && !subquery->hasDistinctOn)
3037 		return;
3038 
3039 	/*
3040 	 * Collect a bitmap of all the output column numbers used by the upper
3041 	 * query.
3042 	 *
3043 	 * Add all the attributes needed for joins or final output.  Note: we must
3044 	 * look at rel's targetlist, not the attr_needed data, because attr_needed
3045 	 * isn't computed for inheritance child rels, cf set_append_rel_size().
3046 	 * (XXX might be worth changing that sometime.)
3047 	 */
3048 	pull_varattnos((Node *) rel->reltarget->exprs, rel->relid, &attrs_used);
3049 
3050 	/* Add all the attributes used by un-pushed-down restriction clauses. */
3051 	foreach(lc, rel->baserestrictinfo)
3052 	{
3053 		RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
3054 
3055 		pull_varattnos((Node *) rinfo->clause, rel->relid, &attrs_used);
3056 	}
3057 
3058 	/*
3059 	 * If there's a whole-row reference to the subquery, we can't remove
3060 	 * anything.
3061 	 */
3062 	if (bms_is_member(0 - FirstLowInvalidHeapAttributeNumber, attrs_used))
3063 		return;
3064 
3065 	/*
3066 	 * Run through the tlist and zap entries we don't need.  It's okay to
3067 	 * modify the tlist items in-place because set_subquery_pathlist made a
3068 	 * copy of the subquery.
3069 	 */
3070 	foreach(lc, subquery->targetList)
3071 	{
3072 		TargetEntry *tle = (TargetEntry *) lfirst(lc);
3073 		Node	   *texpr = (Node *) tle->expr;
3074 
3075 		/*
3076 		 * If it has a sortgroupref number, it's used in some sort/group
3077 		 * clause so we'd better not remove it.  Also, don't remove any
3078 		 * resjunk columns, since their reason for being has nothing to do
3079 		 * with anybody reading the subquery's output.  (It's likely that
3080 		 * resjunk columns in a sub-SELECT would always have ressortgroupref
3081 		 * set, but even if they don't, it seems imprudent to remove them.)
3082 		 */
3083 		if (tle->ressortgroupref || tle->resjunk)
3084 			continue;
3085 
3086 		/*
3087 		 * If it's used by the upper query, we can't remove it.
3088 		 */
3089 		if (bms_is_member(tle->resno - FirstLowInvalidHeapAttributeNumber,
3090 						  attrs_used))
3091 			continue;
3092 
3093 		/*
3094 		 * If it contains a set-returning function, we can't remove it since
3095 		 * that could change the number of rows returned by the subquery.
3096 		 */
3097 		if (subquery->hasTargetSRFs &&
3098 			expression_returns_set(texpr))
3099 			continue;
3100 
3101 		/*
3102 		 * If it contains volatile functions, we daren't remove it for fear
3103 		 * that the user is expecting their side-effects to happen.
3104 		 */
3105 		if (contain_volatile_functions(texpr))
3106 			continue;
3107 
3108 		/*
3109 		 * OK, we don't need it.  Replace the expression with a NULL constant.
3110 		 * Preserve the exposed type of the expression, in case something
3111 		 * looks at the rowtype of the subquery's result.
3112 		 */
3113 		tle->expr = (Expr *) makeNullConst(exprType(texpr),
3114 										   exprTypmod(texpr),
3115 										   exprCollation(texpr));
3116 	}
3117 }
3118 
3119 /*
3120  * create_partial_bitmap_paths
3121  *	  Build partial bitmap heap path for the relation
3122  */
3123 void
create_partial_bitmap_paths(PlannerInfo * root,RelOptInfo * rel,Path * bitmapqual)3124 create_partial_bitmap_paths(PlannerInfo *root, RelOptInfo *rel,
3125 							Path *bitmapqual)
3126 {
3127 	int			parallel_workers;
3128 	double		pages_fetched;
3129 
3130 	/* Compute heap pages for bitmap heap scan */
3131 	pages_fetched = compute_bitmap_pages(root, rel, bitmapqual, 1.0,
3132 										 NULL, NULL);
3133 
3134 	parallel_workers = compute_parallel_worker(rel, pages_fetched, -1);
3135 
3136 	if (parallel_workers <= 0)
3137 		return;
3138 
3139 	add_partial_path(rel, (Path *) create_bitmap_heap_path(root, rel,
3140 														   bitmapqual, rel->lateral_relids, 1.0, parallel_workers));
3141 }
3142 
3143 /*
3144  * Compute the number of parallel workers that should be used to scan a
3145  * relation.  We compute the parallel workers based on the size of the heap to
3146  * be scanned and the size of the index to be scanned, then choose a minimum
3147  * of those.
3148  *
3149  * "heap_pages" is the number of pages from the table that we expect to scan, or
3150  * -1 if we don't expect to scan any.
3151  *
3152  * "index_pages" is the number of pages from the index that we expect to scan, or
3153  * -1 if we don't expect to scan any.
3154  */
3155 int
compute_parallel_worker(RelOptInfo * rel,double heap_pages,double index_pages)3156 compute_parallel_worker(RelOptInfo *rel, double heap_pages, double index_pages)
3157 {
3158 	int			parallel_workers = 0;
3159 
3160 	/*
3161 	 * If the user has set the parallel_workers reloption, use that; otherwise
3162 	 * select a default number of workers.
3163 	 */
3164 	if (rel->rel_parallel_workers != -1)
3165 		parallel_workers = rel->rel_parallel_workers;
3166 	else
3167 	{
3168 		/*
3169 		 * If the number of pages being scanned is insufficient to justify a
3170 		 * parallel scan, just return zero ... unless it's an inheritance
3171 		 * child. In that case, we want to generate a parallel path here
3172 		 * anyway.  It might not be worthwhile just for this relation, but
3173 		 * when combined with all of its inheritance siblings it may well pay
3174 		 * off.
3175 		 */
3176 		if (rel->reloptkind == RELOPT_BASEREL &&
3177 			((heap_pages >= 0 && heap_pages < min_parallel_table_scan_size) ||
3178 			 (index_pages >= 0 && index_pages < min_parallel_index_scan_size)))
3179 			return 0;
3180 
3181 		if (heap_pages >= 0)
3182 		{
3183 			int			heap_parallel_threshold;
3184 			int			heap_parallel_workers = 1;
3185 
3186 			/*
3187 			 * Select the number of workers based on the log of the size of
3188 			 * the relation.  This probably needs to be a good deal more
3189 			 * sophisticated, but we need something here for now.  Note that
3190 			 * the upper limit of the min_parallel_table_scan_size GUC is
3191 			 * chosen to prevent overflow here.
3192 			 */
3193 			heap_parallel_threshold = Max(min_parallel_table_scan_size, 1);
3194 			while (heap_pages >= (BlockNumber) (heap_parallel_threshold * 3))
3195 			{
3196 				heap_parallel_workers++;
3197 				heap_parallel_threshold *= 3;
3198 				if (heap_parallel_threshold > INT_MAX / 3)
3199 					break;		/* avoid overflow */
3200 			}
3201 
3202 			parallel_workers = heap_parallel_workers;
3203 		}
3204 
3205 		if (index_pages >= 0)
3206 		{
3207 			int			index_parallel_workers = 1;
3208 			int			index_parallel_threshold;
3209 
3210 			/* same calculation as for heap_pages above */
3211 			index_parallel_threshold = Max(min_parallel_index_scan_size, 1);
3212 			while (index_pages >= (BlockNumber) (index_parallel_threshold * 3))
3213 			{
3214 				index_parallel_workers++;
3215 				index_parallel_threshold *= 3;
3216 				if (index_parallel_threshold > INT_MAX / 3)
3217 					break;		/* avoid overflow */
3218 			}
3219 
3220 			if (parallel_workers > 0)
3221 				parallel_workers = Min(parallel_workers, index_parallel_workers);
3222 			else
3223 				parallel_workers = index_parallel_workers;
3224 		}
3225 	}
3226 
3227 	/*
3228 	 * In no case use more than max_parallel_workers_per_gather workers.
3229 	 */
3230 	parallel_workers = Min(parallel_workers, max_parallel_workers_per_gather);
3231 
3232 	return parallel_workers;
3233 }
3234 
3235 
3236 /*****************************************************************************
3237  *			DEBUG SUPPORT
3238  *****************************************************************************/
3239 
3240 #ifdef OPTIMIZER_DEBUG
3241 
3242 static void
print_relids(PlannerInfo * root,Relids relids)3243 print_relids(PlannerInfo *root, Relids relids)
3244 {
3245 	int			x;
3246 	bool		first = true;
3247 
3248 	x = -1;
3249 	while ((x = bms_next_member(relids, x)) >= 0)
3250 	{
3251 		if (!first)
3252 			printf(" ");
3253 		if (x < root->simple_rel_array_size &&
3254 			root->simple_rte_array[x])
3255 			printf("%s", root->simple_rte_array[x]->eref->aliasname);
3256 		else
3257 			printf("%d", x);
3258 		first = false;
3259 	}
3260 }
3261 
3262 static void
print_restrictclauses(PlannerInfo * root,List * clauses)3263 print_restrictclauses(PlannerInfo *root, List *clauses)
3264 {
3265 	ListCell   *l;
3266 
3267 	foreach(l, clauses)
3268 	{
3269 		RestrictInfo *c = lfirst(l);
3270 
3271 		print_expr((Node *) c->clause, root->parse->rtable);
3272 		if (lnext(l))
3273 			printf(", ");
3274 	}
3275 }
3276 
3277 static void
print_path(PlannerInfo * root,Path * path,int indent)3278 print_path(PlannerInfo *root, Path *path, int indent)
3279 {
3280 	const char *ptype;
3281 	bool		join = false;
3282 	Path	   *subpath = NULL;
3283 	int			i;
3284 
3285 	switch (nodeTag(path))
3286 	{
3287 		case T_Path:
3288 			switch (path->pathtype)
3289 			{
3290 				case T_SeqScan:
3291 					ptype = "SeqScan";
3292 					break;
3293 				case T_SampleScan:
3294 					ptype = "SampleScan";
3295 					break;
3296 				case T_SubqueryScan:
3297 					ptype = "SubqueryScan";
3298 					break;
3299 				case T_FunctionScan:
3300 					ptype = "FunctionScan";
3301 					break;
3302 				case T_TableFuncScan:
3303 					ptype = "TableFuncScan";
3304 					break;
3305 				case T_ValuesScan:
3306 					ptype = "ValuesScan";
3307 					break;
3308 				case T_CteScan:
3309 					ptype = "CteScan";
3310 					break;
3311 				case T_WorkTableScan:
3312 					ptype = "WorkTableScan";
3313 					break;
3314 				default:
3315 					ptype = "???Path";
3316 					break;
3317 			}
3318 			break;
3319 		case T_IndexPath:
3320 			ptype = "IdxScan";
3321 			break;
3322 		case T_BitmapHeapPath:
3323 			ptype = "BitmapHeapScan";
3324 			break;
3325 		case T_BitmapAndPath:
3326 			ptype = "BitmapAndPath";
3327 			break;
3328 		case T_BitmapOrPath:
3329 			ptype = "BitmapOrPath";
3330 			break;
3331 		case T_TidPath:
3332 			ptype = "TidScan";
3333 			break;
3334 		case T_SubqueryScanPath:
3335 			ptype = "SubqueryScanScan";
3336 			break;
3337 		case T_ForeignPath:
3338 			ptype = "ForeignScan";
3339 			break;
3340 		case T_CustomPath:
3341 			ptype = "CustomScan";
3342 			break;
3343 		case T_NestPath:
3344 			ptype = "NestLoop";
3345 			join = true;
3346 			break;
3347 		case T_MergePath:
3348 			ptype = "MergeJoin";
3349 			join = true;
3350 			break;
3351 		case T_HashPath:
3352 			ptype = "HashJoin";
3353 			join = true;
3354 			break;
3355 		case T_AppendPath:
3356 			ptype = "Append";
3357 			break;
3358 		case T_MergeAppendPath:
3359 			ptype = "MergeAppend";
3360 			break;
3361 		case T_ResultPath:
3362 			ptype = "Result";
3363 			break;
3364 		case T_MaterialPath:
3365 			ptype = "Material";
3366 			subpath = ((MaterialPath *) path)->subpath;
3367 			break;
3368 		case T_UniquePath:
3369 			ptype = "Unique";
3370 			subpath = ((UniquePath *) path)->subpath;
3371 			break;
3372 		case T_GatherPath:
3373 			ptype = "Gather";
3374 			subpath = ((GatherPath *) path)->subpath;
3375 			break;
3376 		case T_GatherMergePath:
3377 			ptype = "GatherMerge";
3378 			subpath = ((GatherMergePath *) path)->subpath;
3379 			break;
3380 		case T_ProjectionPath:
3381 			ptype = "Projection";
3382 			subpath = ((ProjectionPath *) path)->subpath;
3383 			break;
3384 		case T_ProjectSetPath:
3385 			ptype = "ProjectSet";
3386 			subpath = ((ProjectSetPath *) path)->subpath;
3387 			break;
3388 		case T_SortPath:
3389 			ptype = "Sort";
3390 			subpath = ((SortPath *) path)->subpath;
3391 			break;
3392 		case T_GroupPath:
3393 			ptype = "Group";
3394 			subpath = ((GroupPath *) path)->subpath;
3395 			break;
3396 		case T_UpperUniquePath:
3397 			ptype = "UpperUnique";
3398 			subpath = ((UpperUniquePath *) path)->subpath;
3399 			break;
3400 		case T_AggPath:
3401 			ptype = "Agg";
3402 			subpath = ((AggPath *) path)->subpath;
3403 			break;
3404 		case T_GroupingSetsPath:
3405 			ptype = "GroupingSets";
3406 			subpath = ((GroupingSetsPath *) path)->subpath;
3407 			break;
3408 		case T_MinMaxAggPath:
3409 			ptype = "MinMaxAgg";
3410 			break;
3411 		case T_WindowAggPath:
3412 			ptype = "WindowAgg";
3413 			subpath = ((WindowAggPath *) path)->subpath;
3414 			break;
3415 		case T_SetOpPath:
3416 			ptype = "SetOp";
3417 			subpath = ((SetOpPath *) path)->subpath;
3418 			break;
3419 		case T_RecursiveUnionPath:
3420 			ptype = "RecursiveUnion";
3421 			break;
3422 		case T_LockRowsPath:
3423 			ptype = "LockRows";
3424 			subpath = ((LockRowsPath *) path)->subpath;
3425 			break;
3426 		case T_ModifyTablePath:
3427 			ptype = "ModifyTable";
3428 			break;
3429 		case T_LimitPath:
3430 			ptype = "Limit";
3431 			subpath = ((LimitPath *) path)->subpath;
3432 			break;
3433 		default:
3434 			ptype = "???Path";
3435 			break;
3436 	}
3437 
3438 	for (i = 0; i < indent; i++)
3439 		printf("\t");
3440 	printf("%s", ptype);
3441 
3442 	if (path->parent)
3443 	{
3444 		printf("(");
3445 		print_relids(root, path->parent->relids);
3446 		printf(")");
3447 	}
3448 	if (path->param_info)
3449 	{
3450 		printf(" required_outer (");
3451 		print_relids(root, path->param_info->ppi_req_outer);
3452 		printf(")");
3453 	}
3454 	printf(" rows=%.0f cost=%.2f..%.2f\n",
3455 		   path->rows, path->startup_cost, path->total_cost);
3456 
3457 	if (path->pathkeys)
3458 	{
3459 		for (i = 0; i < indent; i++)
3460 			printf("\t");
3461 		printf("  pathkeys: ");
3462 		print_pathkeys(path->pathkeys, root->parse->rtable);
3463 	}
3464 
3465 	if (join)
3466 	{
3467 		JoinPath   *jp = (JoinPath *) path;
3468 
3469 		for (i = 0; i < indent; i++)
3470 			printf("\t");
3471 		printf("  clauses: ");
3472 		print_restrictclauses(root, jp->joinrestrictinfo);
3473 		printf("\n");
3474 
3475 		if (IsA(path, MergePath))
3476 		{
3477 			MergePath  *mp = (MergePath *) path;
3478 
3479 			for (i = 0; i < indent; i++)
3480 				printf("\t");
3481 			printf("  sortouter=%d sortinner=%d materializeinner=%d\n",
3482 				   ((mp->outersortkeys) ? 1 : 0),
3483 				   ((mp->innersortkeys) ? 1 : 0),
3484 				   ((mp->materialize_inner) ? 1 : 0));
3485 		}
3486 
3487 		print_path(root, jp->outerjoinpath, indent + 1);
3488 		print_path(root, jp->innerjoinpath, indent + 1);
3489 	}
3490 
3491 	if (subpath)
3492 		print_path(root, subpath, indent + 1);
3493 }
3494 
3495 void
debug_print_rel(PlannerInfo * root,RelOptInfo * rel)3496 debug_print_rel(PlannerInfo *root, RelOptInfo *rel)
3497 {
3498 	ListCell   *l;
3499 
3500 	printf("RELOPTINFO (");
3501 	print_relids(root, rel->relids);
3502 	printf("): rows=%.0f width=%d\n", rel->rows, rel->reltarget->width);
3503 
3504 	if (rel->baserestrictinfo)
3505 	{
3506 		printf("\tbaserestrictinfo: ");
3507 		print_restrictclauses(root, rel->baserestrictinfo);
3508 		printf("\n");
3509 	}
3510 
3511 	if (rel->joininfo)
3512 	{
3513 		printf("\tjoininfo: ");
3514 		print_restrictclauses(root, rel->joininfo);
3515 		printf("\n");
3516 	}
3517 
3518 	printf("\tpath list:\n");
3519 	foreach(l, rel->pathlist)
3520 		print_path(root, lfirst(l), 1);
3521 	if (rel->cheapest_parameterized_paths)
3522 	{
3523 		printf("\n\tcheapest parameterized paths:\n");
3524 		foreach(l, rel->cheapest_parameterized_paths)
3525 			print_path(root, lfirst(l), 1);
3526 	}
3527 	if (rel->cheapest_startup_path)
3528 	{
3529 		printf("\n\tcheapest startup path:\n");
3530 		print_path(root, rel->cheapest_startup_path, 1);
3531 	}
3532 	if (rel->cheapest_total_path)
3533 	{
3534 		printf("\n\tcheapest total path:\n");
3535 		print_path(root, rel->cheapest_total_path, 1);
3536 	}
3537 	printf("\n");
3538 	fflush(stdout);
3539 }
3540 
3541 #endif							/* OPTIMIZER_DEBUG */
3542