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