PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pathnode.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pathnode.c
4  * Routines to manipulate pathlists and create path nodes
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/util/pathnode.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include <math.h>
18 
19 #include "miscadmin.h"
20 #include "nodes/nodeFuncs.h"
21 #include "optimizer/clauses.h"
22 #include "optimizer/cost.h"
23 #include "optimizer/pathnode.h"
24 #include "optimizer/paths.h"
25 #include "optimizer/planmain.h"
26 #include "optimizer/restrictinfo.h"
27 #include "optimizer/var.h"
28 #include "parser/parsetree.h"
29 #include "utils/lsyscache.h"
30 #include "utils/selfuncs.h"
31 
32 
33 typedef enum
34 {
35  COSTS_EQUAL, /* path costs are fuzzily equal */
36  COSTS_BETTER1, /* first path is cheaper than second */
37  COSTS_BETTER2, /* second path is cheaper than first */
38  COSTS_DIFFERENT /* neither path dominates the other on cost */
40 
41 /*
42  * STD_FUZZ_FACTOR is the normal fuzz factor for compare_path_costs_fuzzily.
43  * XXX is it worth making this user-controllable? It provides a tradeoff
44  * between planner runtime and the accuracy of path cost comparisons.
45  */
46 #define STD_FUZZ_FACTOR 1.01
47 
48 static List *translate_sub_tlist(List *tlist, int relid);
49 
50 
51 /*****************************************************************************
52  * MISC. PATH UTILITIES
53  *****************************************************************************/
54 
55 /*
56  * compare_path_costs
57  * Return -1, 0, or +1 according as path1 is cheaper, the same cost,
58  * or more expensive than path2 for the specified criterion.
59  */
60 int
61 compare_path_costs(Path *path1, Path *path2, CostSelector criterion)
62 {
63  if (criterion == STARTUP_COST)
64  {
65  if (path1->startup_cost < path2->startup_cost)
66  return -1;
67  if (path1->startup_cost > path2->startup_cost)
68  return +1;
69 
70  /*
71  * If paths have the same startup cost (not at all unlikely), order
72  * them by total cost.
73  */
74  if (path1->total_cost < path2->total_cost)
75  return -1;
76  if (path1->total_cost > path2->total_cost)
77  return +1;
78  }
79  else
80  {
81  if (path1->total_cost < path2->total_cost)
82  return -1;
83  if (path1->total_cost > path2->total_cost)
84  return +1;
85 
86  /*
87  * If paths have the same total cost, order them by startup cost.
88  */
89  if (path1->startup_cost < path2->startup_cost)
90  return -1;
91  if (path1->startup_cost > path2->startup_cost)
92  return +1;
93  }
94  return 0;
95 }
96 
97 /*
98  * compare_path_fractional_costs
99  * Return -1, 0, or +1 according as path1 is cheaper, the same cost,
100  * or more expensive than path2 for fetching the specified fraction
101  * of the total tuples.
102  *
103  * If fraction is <= 0 or > 1, we interpret it as 1, ie, we select the
104  * path with the cheaper total_cost.
105  */
106 int
108  double fraction)
109 {
110  Cost cost1,
111  cost2;
112 
113  if (fraction <= 0.0 || fraction >= 1.0)
114  return compare_path_costs(path1, path2, TOTAL_COST);
115  cost1 = path1->startup_cost +
116  fraction * (path1->total_cost - path1->startup_cost);
117  cost2 = path2->startup_cost +
118  fraction * (path2->total_cost - path2->startup_cost);
119  if (cost1 < cost2)
120  return -1;
121  if (cost1 > cost2)
122  return +1;
123  return 0;
124 }
125 
126 /*
127  * compare_path_costs_fuzzily
128  * Compare the costs of two paths to see if either can be said to
129  * dominate the other.
130  *
131  * We use fuzzy comparisons so that add_path() can avoid keeping both of
132  * a pair of paths that really have insignificantly different cost.
133  *
134  * The fuzz_factor argument must be 1.0 plus delta, where delta is the
135  * fraction of the smaller cost that is considered to be a significant
136  * difference. For example, fuzz_factor = 1.01 makes the fuzziness limit
137  * be 1% of the smaller cost.
138  *
139  * The two paths are said to have "equal" costs if both startup and total
140  * costs are fuzzily the same. Path1 is said to be better than path2 if
141  * it has fuzzily better startup cost and fuzzily no worse total cost,
142  * or if it has fuzzily better total cost and fuzzily no worse startup cost.
143  * Path2 is better than path1 if the reverse holds. Finally, if one path
144  * is fuzzily better than the other on startup cost and fuzzily worse on
145  * total cost, we just say that their costs are "different", since neither
146  * dominates the other across the whole performance spectrum.
147  *
148  * This function also enforces a policy rule that paths for which the relevant
149  * one of parent->consider_startup and parent->consider_param_startup is false
150  * cannot survive comparisons solely on the grounds of good startup cost, so
151  * we never return COSTS_DIFFERENT when that is true for the total-cost loser.
152  * (But if total costs are fuzzily equal, we compare startup costs anyway,
153  * in hopes of eliminating one path or the other.)
154  */
155 static PathCostComparison
156 compare_path_costs_fuzzily(Path *path1, Path *path2, double fuzz_factor)
157 {
158 #define CONSIDER_PATH_STARTUP_COST(p) \
159  ((p)->param_info == NULL ? (p)->parent->consider_startup : (p)->parent->consider_param_startup)
160 
161  /*
162  * Check total cost first since it's more likely to be different; many
163  * paths have zero startup cost.
164  */
165  if (path1->total_cost > path2->total_cost * fuzz_factor)
166  {
167  /* path1 fuzzily worse on total cost */
168  if (CONSIDER_PATH_STARTUP_COST(path1) &&
169  path2->startup_cost > path1->startup_cost * fuzz_factor)
170  {
171  /* ... but path2 fuzzily worse on startup, so DIFFERENT */
172  return COSTS_DIFFERENT;
173  }
174  /* else path2 dominates */
175  return COSTS_BETTER2;
176  }
177  if (path2->total_cost > path1->total_cost * fuzz_factor)
178  {
179  /* path2 fuzzily worse on total cost */
180  if (CONSIDER_PATH_STARTUP_COST(path2) &&
181  path1->startup_cost > path2->startup_cost * fuzz_factor)
182  {
183  /* ... but path1 fuzzily worse on startup, so DIFFERENT */
184  return COSTS_DIFFERENT;
185  }
186  /* else path1 dominates */
187  return COSTS_BETTER1;
188  }
189  /* fuzzily the same on total cost ... */
190  if (path1->startup_cost > path2->startup_cost * fuzz_factor)
191  {
192  /* ... but path1 fuzzily worse on startup, so path2 wins */
193  return COSTS_BETTER2;
194  }
195  if (path2->startup_cost > path1->startup_cost * fuzz_factor)
196  {
197  /* ... but path2 fuzzily worse on startup, so path1 wins */
198  return COSTS_BETTER1;
199  }
200  /* fuzzily the same on both costs */
201  return COSTS_EQUAL;
202 
203 #undef CONSIDER_PATH_STARTUP_COST
204 }
205 
206 /*
207  * set_cheapest
208  * Find the minimum-cost paths from among a relation's paths,
209  * and save them in the rel's cheapest-path fields.
210  *
211  * cheapest_total_path is normally the cheapest-total-cost unparameterized
212  * path; but if there are no unparameterized paths, we assign it to be the
213  * best (cheapest least-parameterized) parameterized path. However, only
214  * unparameterized paths are considered candidates for cheapest_startup_path,
215  * so that will be NULL if there are no unparameterized paths.
216  *
217  * The cheapest_parameterized_paths list collects all parameterized paths
218  * that have survived the add_path() tournament for this relation. (Since
219  * add_path ignores pathkeys for a parameterized path, these will be paths
220  * that have best cost or best row count for their parameterization. We
221  * may also have both a parallel-safe and a non-parallel-safe path in some
222  * cases for the same parameterization in some cases, but this should be
223  * relatively rare since, most typically, all paths for the same relation
224  * will be parallel-safe or none of them will.)
225  *
226  * cheapest_parameterized_paths always includes the cheapest-total
227  * unparameterized path, too, if there is one; the users of that list find
228  * it more convenient if that's included.
229  *
230  * This is normally called only after we've finished constructing the path
231  * list for the rel node.
232  */
233 void
235 {
236  Path *cheapest_startup_path;
237  Path *cheapest_total_path;
238  Path *best_param_path;
239  List *parameterized_paths;
240  ListCell *p;
241 
242  Assert(IsA(parent_rel, RelOptInfo));
243 
244  if (parent_rel->pathlist == NIL)
245  elog(ERROR, "could not devise a query plan for the given query");
246 
247  cheapest_startup_path = cheapest_total_path = best_param_path = NULL;
248  parameterized_paths = NIL;
249 
250  foreach(p, parent_rel->pathlist)
251  {
252  Path *path = (Path *) lfirst(p);
253  int cmp;
254 
255  if (path->param_info)
256  {
257  /* Parameterized path, so add it to parameterized_paths */
258  parameterized_paths = lappend(parameterized_paths, path);
259 
260  /*
261  * If we have an unparameterized cheapest-total, we no longer care
262  * about finding the best parameterized path, so move on.
263  */
264  if (cheapest_total_path)
265  continue;
266 
267  /*
268  * Otherwise, track the best parameterized path, which is the one
269  * with least total cost among those of the minimum
270  * parameterization.
271  */
272  if (best_param_path == NULL)
273  best_param_path = path;
274  else
275  {
276  switch (bms_subset_compare(PATH_REQ_OUTER(path),
277  PATH_REQ_OUTER(best_param_path)))
278  {
279  case BMS_EQUAL:
280  /* keep the cheaper one */
281  if (compare_path_costs(path, best_param_path,
282  TOTAL_COST) < 0)
283  best_param_path = path;
284  break;
285  case BMS_SUBSET1:
286  /* new path is less-parameterized */
287  best_param_path = path;
288  break;
289  case BMS_SUBSET2:
290  /* old path is less-parameterized, keep it */
291  break;
292  case BMS_DIFFERENT:
293 
294  /*
295  * This means that neither path has the least possible
296  * parameterization for the rel. We'll sit on the old
297  * path until something better comes along.
298  */
299  break;
300  }
301  }
302  }
303  else
304  {
305  /* Unparameterized path, so consider it for cheapest slots */
306  if (cheapest_total_path == NULL)
307  {
308  cheapest_startup_path = cheapest_total_path = path;
309  continue;
310  }
311 
312  /*
313  * If we find two paths of identical costs, try to keep the
314  * better-sorted one. The paths might have unrelated sort
315  * orderings, in which case we can only guess which might be
316  * better to keep, but if one is superior then we definitely
317  * should keep that one.
318  */
319  cmp = compare_path_costs(cheapest_startup_path, path, STARTUP_COST);
320  if (cmp > 0 ||
321  (cmp == 0 &&
322  compare_pathkeys(cheapest_startup_path->pathkeys,
323  path->pathkeys) == PATHKEYS_BETTER2))
324  cheapest_startup_path = path;
325 
326  cmp = compare_path_costs(cheapest_total_path, path, TOTAL_COST);
327  if (cmp > 0 ||
328  (cmp == 0 &&
329  compare_pathkeys(cheapest_total_path->pathkeys,
330  path->pathkeys) == PATHKEYS_BETTER2))
331  cheapest_total_path = path;
332  }
333  }
334 
335  /* Add cheapest unparameterized path, if any, to parameterized_paths */
336  if (cheapest_total_path)
337  parameterized_paths = lcons(cheapest_total_path, parameterized_paths);
338 
339  /*
340  * If there is no unparameterized path, use the best parameterized path as
341  * cheapest_total_path (but not as cheapest_startup_path).
342  */
343  if (cheapest_total_path == NULL)
344  cheapest_total_path = best_param_path;
345  Assert(cheapest_total_path != NULL);
346 
347  parent_rel->cheapest_startup_path = cheapest_startup_path;
348  parent_rel->cheapest_total_path = cheapest_total_path;
349  parent_rel->cheapest_unique_path = NULL; /* computed only if needed */
350  parent_rel->cheapest_parameterized_paths = parameterized_paths;
351 }
352 
353 /*
354  * add_path
355  * Consider a potential implementation path for the specified parent rel,
356  * and add it to the rel's pathlist if it is worthy of consideration.
357  * A path is worthy if it has a better sort order (better pathkeys) or
358  * cheaper cost (on either dimension), or generates fewer rows, than any
359  * existing path that has the same or superset parameterization rels.
360  * We also consider parallel-safe paths more worthy than others.
361  *
362  * We also remove from the rel's pathlist any old paths that are dominated
363  * by new_path --- that is, new_path is cheaper, at least as well ordered,
364  * generates no more rows, requires no outer rels not required by the old
365  * path, and is no less parallel-safe.
366  *
367  * In most cases, a path with a superset parameterization will generate
368  * fewer rows (since it has more join clauses to apply), so that those two
369  * figures of merit move in opposite directions; this means that a path of
370  * one parameterization can seldom dominate a path of another. But such
371  * cases do arise, so we make the full set of checks anyway.
372  *
373  * There are two policy decisions embedded in this function, along with
374  * its sibling add_path_precheck. First, we treat all parameterized paths
375  * as having NIL pathkeys, so that they cannot win comparisons on the
376  * basis of sort order. This is to reduce the number of parameterized
377  * paths that are kept; see discussion in src/backend/optimizer/README.
378  *
379  * Second, we only consider cheap startup cost to be interesting if
380  * parent_rel->consider_startup is true for an unparameterized path, or
381  * parent_rel->consider_param_startup is true for a parameterized one.
382  * Again, this allows discarding useless paths sooner.
383  *
384  * The pathlist is kept sorted by total_cost, with cheaper paths
385  * at the front. Within this routine, that's simply a speed hack:
386  * doing it that way makes it more likely that we will reject an inferior
387  * path after a few comparisons, rather than many comparisons.
388  * However, add_path_precheck relies on this ordering to exit early
389  * when possible.
390  *
391  * NOTE: discarded Path objects are immediately pfree'd to reduce planner
392  * memory consumption. We dare not try to free the substructure of a Path,
393  * since much of it may be shared with other Paths or the query tree itself;
394  * but just recycling discarded Path nodes is a very useful savings in
395  * a large join tree. We can recycle the List nodes of pathlist, too.
396  *
397  * As noted in optimizer/README, deleting a previously-accepted Path is
398  * safe because we know that Paths of this rel cannot yet be referenced
399  * from any other rel, such as a higher-level join. However, in some cases
400  * it is possible that a Path is referenced by another Path for its own
401  * rel; we must not delete such a Path, even if it is dominated by the new
402  * Path. Currently this occurs only for IndexPath objects, which may be
403  * referenced as children of BitmapHeapPaths as well as being paths in
404  * their own right. Hence, we don't pfree IndexPaths when rejecting them.
405  *
406  * 'parent_rel' is the relation entry to which the path corresponds.
407  * 'new_path' is a potential path for parent_rel.
408  *
409  * Returns nothing, but modifies parent_rel->pathlist.
410  */
411 void
412 add_path(RelOptInfo *parent_rel, Path *new_path)
413 {
414  bool accept_new = true; /* unless we find a superior old path */
415  ListCell *insert_after = NULL; /* where to insert new item */
416  List *new_path_pathkeys;
417  ListCell *p1;
418  ListCell *p1_prev;
419  ListCell *p1_next;
420 
421  /*
422  * This is a convenient place to check for query cancel --- no part of the
423  * planner goes very long without calling add_path().
424  */
426 
427  /* Pretend parameterized paths have no pathkeys, per comment above */
428  new_path_pathkeys = new_path->param_info ? NIL : new_path->pathkeys;
429 
430  /*
431  * Loop to check proposed new path against old paths. Note it is possible
432  * for more than one old path to be tossed out because new_path dominates
433  * it.
434  *
435  * We can't use foreach here because the loop body may delete the current
436  * list cell.
437  */
438  p1_prev = NULL;
439  for (p1 = list_head(parent_rel->pathlist); p1 != NULL; p1 = p1_next)
440  {
441  Path *old_path = (Path *) lfirst(p1);
442  bool remove_old = false; /* unless new proves superior */
443  PathCostComparison costcmp;
444  PathKeysComparison keyscmp;
445  BMS_Comparison outercmp;
446 
447  p1_next = lnext(p1);
448 
449  /*
450  * Do a fuzzy cost comparison with standard fuzziness limit.
451  */
452  costcmp = compare_path_costs_fuzzily(new_path, old_path,
454 
455  /*
456  * If the two paths compare differently for startup and total cost,
457  * then we want to keep both, and we can skip comparing pathkeys and
458  * required_outer rels. If they compare the same, proceed with the
459  * other comparisons. Row count is checked last. (We make the tests
460  * in this order because the cost comparison is most likely to turn
461  * out "different", and the pathkeys comparison next most likely. As
462  * explained above, row count very seldom makes a difference, so even
463  * though it's cheap to compare there's not much point in checking it
464  * earlier.)
465  */
466  if (costcmp != COSTS_DIFFERENT)
467  {
468  /* Similarly check to see if either dominates on pathkeys */
469  List *old_path_pathkeys;
470 
471  old_path_pathkeys = old_path->param_info ? NIL : old_path->pathkeys;
472  keyscmp = compare_pathkeys(new_path_pathkeys,
473  old_path_pathkeys);
474  if (keyscmp != PATHKEYS_DIFFERENT)
475  {
476  switch (costcmp)
477  {
478  case COSTS_EQUAL:
479  outercmp = bms_subset_compare(PATH_REQ_OUTER(new_path),
480  PATH_REQ_OUTER(old_path));
481  if (keyscmp == PATHKEYS_BETTER1)
482  {
483  if ((outercmp == BMS_EQUAL ||
484  outercmp == BMS_SUBSET1) &&
485  new_path->rows <= old_path->rows &&
486  new_path->parallel_safe >= old_path->parallel_safe)
487  remove_old = true; /* new dominates old */
488  }
489  else if (keyscmp == PATHKEYS_BETTER2)
490  {
491  if ((outercmp == BMS_EQUAL ||
492  outercmp == BMS_SUBSET2) &&
493  new_path->rows >= old_path->rows &&
494  new_path->parallel_safe <= old_path->parallel_safe)
495  accept_new = false; /* old dominates new */
496  }
497  else /* keyscmp == PATHKEYS_EQUAL */
498  {
499  if (outercmp == BMS_EQUAL)
500  {
501  /*
502  * Same pathkeys and outer rels, and fuzzily
503  * the same cost, so keep just one; to decide
504  * which, first check parallel-safety, then
505  * rows, then do a fuzzy cost comparison with
506  * very small fuzz limit. (We used to do an
507  * exact cost comparison, but that results in
508  * annoying platform-specific plan variations
509  * due to roundoff in the cost estimates.) If
510  * things are still tied, arbitrarily keep
511  * only the old path. Notice that we will
512  * keep only the old path even if the
513  * less-fuzzy comparison decides the startup
514  * and total costs compare differently.
515  */
516  if (new_path->parallel_safe >
517  old_path->parallel_safe)
518  remove_old = true; /* new dominates old */
519  else if (new_path->parallel_safe <
520  old_path->parallel_safe)
521  accept_new = false; /* old dominates new */
522  else if (new_path->rows < old_path->rows)
523  remove_old = true; /* new dominates old */
524  else if (new_path->rows > old_path->rows)
525  accept_new = false; /* old dominates new */
526  else if (compare_path_costs_fuzzily(new_path,
527  old_path,
528  1.0000000001) == COSTS_BETTER1)
529  remove_old = true; /* new dominates old */
530  else
531  accept_new = false; /* old equals or
532  * dominates new */
533  }
534  else if (outercmp == BMS_SUBSET1 &&
535  new_path->rows <= old_path->rows &&
536  new_path->parallel_safe >= old_path->parallel_safe)
537  remove_old = true; /* new dominates old */
538  else if (outercmp == BMS_SUBSET2 &&
539  new_path->rows >= old_path->rows &&
540  new_path->parallel_safe <= old_path->parallel_safe)
541  accept_new = false; /* old dominates new */
542  /* else different parameterizations, keep both */
543  }
544  break;
545  case COSTS_BETTER1:
546  if (keyscmp != PATHKEYS_BETTER2)
547  {
548  outercmp = bms_subset_compare(PATH_REQ_OUTER(new_path),
549  PATH_REQ_OUTER(old_path));
550  if ((outercmp == BMS_EQUAL ||
551  outercmp == BMS_SUBSET1) &&
552  new_path->rows <= old_path->rows &&
553  new_path->parallel_safe >= old_path->parallel_safe)
554  remove_old = true; /* new dominates old */
555  }
556  break;
557  case COSTS_BETTER2:
558  if (keyscmp != PATHKEYS_BETTER1)
559  {
560  outercmp = bms_subset_compare(PATH_REQ_OUTER(new_path),
561  PATH_REQ_OUTER(old_path));
562  if ((outercmp == BMS_EQUAL ||
563  outercmp == BMS_SUBSET2) &&
564  new_path->rows >= old_path->rows &&
565  new_path->parallel_safe <= old_path->parallel_safe)
566  accept_new = false; /* old dominates new */
567  }
568  break;
569  case COSTS_DIFFERENT:
570 
571  /*
572  * can't get here, but keep this case to keep compiler
573  * quiet
574  */
575  break;
576  }
577  }
578  }
579 
580  /*
581  * Remove current element from pathlist if dominated by new.
582  */
583  if (remove_old)
584  {
585  parent_rel->pathlist = list_delete_cell(parent_rel->pathlist,
586  p1, p1_prev);
587 
588  /*
589  * Delete the data pointed-to by the deleted cell, if possible
590  */
591  if (!IsA(old_path, IndexPath))
592  pfree(old_path);
593  /* p1_prev does not advance */
594  }
595  else
596  {
597  /* new belongs after this old path if it has cost >= old's */
598  if (new_path->total_cost >= old_path->total_cost)
599  insert_after = p1;
600  /* p1_prev advances */
601  p1_prev = p1;
602  }
603 
604  /*
605  * If we found an old path that dominates new_path, we can quit
606  * scanning the pathlist; we will not add new_path, and we assume
607  * new_path cannot dominate any other elements of the pathlist.
608  */
609  if (!accept_new)
610  break;
611  }
612 
613  if (accept_new)
614  {
615  /* Accept the new path: insert it at proper place in pathlist */
616  if (insert_after)
617  lappend_cell(parent_rel->pathlist, insert_after, new_path);
618  else
619  parent_rel->pathlist = lcons(new_path, parent_rel->pathlist);
620  }
621  else
622  {
623  /* Reject and recycle the new path */
624  if (!IsA(new_path, IndexPath))
625  pfree(new_path);
626  }
627 }
628 
629 /*
630  * add_path_precheck
631  * Check whether a proposed new path could possibly get accepted.
632  * We assume we know the path's pathkeys and parameterization accurately,
633  * and have lower bounds for its costs.
634  *
635  * Note that we do not know the path's rowcount, since getting an estimate for
636  * that is too expensive to do before prechecking. We assume here that paths
637  * of a superset parameterization will generate fewer rows; if that holds,
638  * then paths with different parameterizations cannot dominate each other
639  * and so we can simply ignore existing paths of another parameterization.
640  * (In the infrequent cases where that rule of thumb fails, add_path will
641  * get rid of the inferior path.)
642  *
643  * At the time this is called, we haven't actually built a Path structure,
644  * so the required information has to be passed piecemeal.
645  */
646 bool
648  Cost startup_cost, Cost total_cost,
649  List *pathkeys, Relids required_outer)
650 {
651  List *new_path_pathkeys;
652  bool consider_startup;
653  ListCell *p1;
654 
655  /* Pretend parameterized paths have no pathkeys, per add_path policy */
656  new_path_pathkeys = required_outer ? NIL : pathkeys;
657 
658  /* Decide whether new path's startup cost is interesting */
659  consider_startup = required_outer ? parent_rel->consider_param_startup : parent_rel->consider_startup;
660 
661  foreach(p1, parent_rel->pathlist)
662  {
663  Path *old_path = (Path *) lfirst(p1);
664  PathKeysComparison keyscmp;
665 
666  /*
667  * We are looking for an old_path with the same parameterization (and
668  * by assumption the same rowcount) that dominates the new path on
669  * pathkeys as well as both cost metrics. If we find one, we can
670  * reject the new path.
671  *
672  * Cost comparisons here should match compare_path_costs_fuzzily.
673  */
674  if (total_cost > old_path->total_cost * STD_FUZZ_FACTOR)
675  {
676  /* new path can win on startup cost only if consider_startup */
677  if (startup_cost > old_path->startup_cost * STD_FUZZ_FACTOR ||
678  !consider_startup)
679  {
680  /* new path loses on cost, so check pathkeys... */
681  List *old_path_pathkeys;
682 
683  old_path_pathkeys = old_path->param_info ? NIL : old_path->pathkeys;
684  keyscmp = compare_pathkeys(new_path_pathkeys,
685  old_path_pathkeys);
686  if (keyscmp == PATHKEYS_EQUAL ||
687  keyscmp == PATHKEYS_BETTER2)
688  {
689  /* new path does not win on pathkeys... */
690  if (bms_equal(required_outer, PATH_REQ_OUTER(old_path)))
691  {
692  /* Found an old path that dominates the new one */
693  return false;
694  }
695  }
696  }
697  }
698  else
699  {
700  /*
701  * Since the pathlist is sorted by total_cost, we can stop looking
702  * once we reach a path with a total_cost larger than the new
703  * path's.
704  */
705  break;
706  }
707  }
708 
709  return true;
710 }
711 
712 /*
713  * add_partial_path
714  * Like add_path, our goal here is to consider whether a path is worthy
715  * of being kept around, but the considerations here are a bit different.
716  * A partial path is one which can be executed in any number of workers in
717  * parallel such that each worker will generate a subset of the path's
718  * overall result.
719  *
720  * As in add_path, the partial_pathlist is kept sorted with the cheapest
721  * total path in front. This is depended on by multiple places, which
722  * just take the front entry as the cheapest path without searching.
723  *
724  * We don't generate parameterized partial paths for several reasons. Most
725  * importantly, they're not safe to execute, because there's nothing to
726  * make sure that a parallel scan within the parameterized portion of the
727  * plan is running with the same value in every worker at the same time.
728  * Fortunately, it seems unlikely to be worthwhile anyway, because having
729  * each worker scan the entire outer relation and a subset of the inner
730  * relation will generally be a terrible plan. The inner (parameterized)
731  * side of the plan will be small anyway. There could be rare cases where
732  * this wins big - e.g. if join order constraints put a 1-row relation on
733  * the outer side of the topmost join with a parameterized plan on the inner
734  * side - but we'll have to be content not to handle such cases until
735  * somebody builds an executor infrastructure that can cope with them.
736  *
737  * Because we don't consider parameterized paths here, we also don't
738  * need to consider the row counts as a measure of quality: every path will
739  * produce the same number of rows. Neither do we need to consider startup
740  * costs: parallelism is only used for plans that will be run to completion.
741  * Therefore, this routine is much simpler than add_path: it needs to
742  * consider only pathkeys and total cost.
743  *
744  * As with add_path, we pfree paths that are found to be dominated by
745  * another partial path; this requires that there be no other references to
746  * such paths yet. Hence, GatherPaths must not be created for a rel until
747  * we're done creating all partial paths for it. Unlike add_path, we don't
748  * take an exception for IndexPaths as partial index paths won't be
749  * referenced by partial BitmapHeapPaths.
750  */
751 void
752 add_partial_path(RelOptInfo *parent_rel, Path *new_path)
753 {
754  bool accept_new = true; /* unless we find a superior old path */
755  ListCell *insert_after = NULL; /* where to insert new item */
756  ListCell *p1;
757  ListCell *p1_prev;
758  ListCell *p1_next;
759 
760  /* Check for query cancel. */
762 
763  /*
764  * As in add_path, throw out any paths which are dominated by the new
765  * path, but throw out the new path if some existing path dominates it.
766  */
767  p1_prev = NULL;
768  for (p1 = list_head(parent_rel->partial_pathlist); p1 != NULL;
769  p1 = p1_next)
770  {
771  Path *old_path = (Path *) lfirst(p1);
772  bool remove_old = false; /* unless new proves superior */
773  PathKeysComparison keyscmp;
774 
775  p1_next = lnext(p1);
776 
777  /* Compare pathkeys. */
778  keyscmp = compare_pathkeys(new_path->pathkeys, old_path->pathkeys);
779 
780  /* Unless pathkeys are incompable, keep just one of the two paths. */
781  if (keyscmp != PATHKEYS_DIFFERENT)
782  {
783  if (new_path->total_cost > old_path->total_cost * STD_FUZZ_FACTOR)
784  {
785  /* New path costs more; keep it only if pathkeys are better. */
786  if (keyscmp != PATHKEYS_BETTER1)
787  accept_new = false;
788  }
789  else if (old_path->total_cost > new_path->total_cost
790  * STD_FUZZ_FACTOR)
791  {
792  /* Old path costs more; keep it only if pathkeys are better. */
793  if (keyscmp != PATHKEYS_BETTER2)
794  remove_old = true;
795  }
796  else if (keyscmp == PATHKEYS_BETTER1)
797  {
798  /* Costs are about the same, new path has better pathkeys. */
799  remove_old = true;
800  }
801  else if (keyscmp == PATHKEYS_BETTER2)
802  {
803  /* Costs are about the same, old path has better pathkeys. */
804  accept_new = false;
805  }
806  else if (old_path->total_cost > new_path->total_cost * 1.0000000001)
807  {
808  /* Pathkeys are the same, and the old path costs more. */
809  remove_old = true;
810  }
811  else
812  {
813  /*
814  * Pathkeys are the same, and new path isn't materially
815  * cheaper.
816  */
817  accept_new = false;
818  }
819  }
820 
821  /*
822  * Remove current element from partial_pathlist if dominated by new.
823  */
824  if (remove_old)
825  {
826  parent_rel->partial_pathlist =
827  list_delete_cell(parent_rel->partial_pathlist, p1, p1_prev);
828  pfree(old_path);
829  /* p1_prev does not advance */
830  }
831  else
832  {
833  /* new belongs after this old path if it has cost >= old's */
834  if (new_path->total_cost >= old_path->total_cost)
835  insert_after = p1;
836  /* p1_prev advances */
837  p1_prev = p1;
838  }
839 
840  /*
841  * If we found an old path that dominates new_path, we can quit
842  * scanning the partial_pathlist; we will not add new_path, and we
843  * assume new_path cannot dominate any later path.
844  */
845  if (!accept_new)
846  break;
847  }
848 
849  if (accept_new)
850  {
851  /* Accept the new path: insert it at proper place */
852  if (insert_after)
853  lappend_cell(parent_rel->partial_pathlist, insert_after, new_path);
854  else
855  parent_rel->partial_pathlist =
856  lcons(new_path, parent_rel->partial_pathlist);
857  }
858  else
859  {
860  /* Reject and recycle the new path */
861  pfree(new_path);
862  }
863 }
864 
865 /*
866  * add_partial_path_precheck
867  * Check whether a proposed new partial path could possibly get accepted.
868  *
869  * Unlike add_path_precheck, we can ignore startup cost and parameterization,
870  * since they don't matter for partial paths (see add_partial_path). But
871  * we do want to make sure we don't add a partial path if there's already
872  * a complete path that dominates it, since in that case the proposed path
873  * is surely a loser.
874  */
875 bool
876 add_partial_path_precheck(RelOptInfo *parent_rel, Cost total_cost,
877  List *pathkeys)
878 {
879  ListCell *p1;
880 
881  /*
882  * Our goal here is twofold. First, we want to find out whether this path
883  * is clearly inferior to some existing partial path. If so, we want to
884  * reject it immediately. Second, we want to find out whether this path
885  * is clearly superior to some existing partial path -- at least, modulo
886  * final cost computations. If so, we definitely want to consider it.
887  *
888  * Unlike add_path(), we always compare pathkeys here. This is because we
889  * expect partial_pathlist to be very short, and getting a definitive
890  * answer at this stage avoids the need to call add_path_precheck.
891  */
892  foreach(p1, parent_rel->partial_pathlist)
893  {
894  Path *old_path = (Path *) lfirst(p1);
895  PathKeysComparison keyscmp;
896 
897  keyscmp = compare_pathkeys(pathkeys, old_path->pathkeys);
898  if (keyscmp != PATHKEYS_DIFFERENT)
899  {
900  if (total_cost > old_path->total_cost * STD_FUZZ_FACTOR &&
901  keyscmp != PATHKEYS_BETTER1)
902  return false;
903  if (old_path->total_cost > total_cost * STD_FUZZ_FACTOR &&
904  keyscmp != PATHKEYS_BETTER2)
905  return true;
906  }
907  }
908 
909  /*
910  * This path is neither clearly inferior to an existing partial path nor
911  * clearly good enough that it might replace one. Compare it to
912  * non-parallel plans. If it loses even before accounting for the cost of
913  * the Gather node, we should definitely reject it.
914  *
915  * Note that we pass the total_cost to add_path_precheck twice. This is
916  * because it's never advantageous to consider the startup cost of a
917  * partial path; the resulting plans, if run in parallel, will be run to
918  * completion.
919  */
920  if (!add_path_precheck(parent_rel, total_cost, total_cost, pathkeys,
921  NULL))
922  return false;
923 
924  return true;
925 }
926 
927 
928 /*****************************************************************************
929  * PATH NODE CREATION ROUTINES
930  *****************************************************************************/
931 
932 /*
933  * create_seqscan_path
934  * Creates a path corresponding to a sequential scan, returning the
935  * pathnode.
936  */
937 Path *
939  Relids required_outer, int parallel_workers)
940 {
941  Path *pathnode = makeNode(Path);
942 
943  pathnode->pathtype = T_SeqScan;
944  pathnode->parent = rel;
945  pathnode->pathtarget = rel->reltarget;
946  pathnode->param_info = get_baserel_parampathinfo(root, rel,
947  required_outer);
948  pathnode->parallel_aware = parallel_workers > 0 ? true : false;
949  pathnode->parallel_safe = rel->consider_parallel;
950  pathnode->parallel_workers = parallel_workers;
951  pathnode->pathkeys = NIL; /* seqscan has unordered result */
952 
953  cost_seqscan(pathnode, root, rel, pathnode->param_info);
954 
955  return pathnode;
956 }
957 
958 /*
959  * create_samplescan_path
960  * Creates a path node for a sampled table scan.
961  */
962 Path *
964 {
965  Path *pathnode = makeNode(Path);
966 
967  pathnode->pathtype = T_SampleScan;
968  pathnode->parent = rel;
969  pathnode->pathtarget = rel->reltarget;
970  pathnode->param_info = get_baserel_parampathinfo(root, rel,
971  required_outer);
972  pathnode->parallel_aware = false;
973  pathnode->parallel_safe = rel->consider_parallel;
974  pathnode->parallel_workers = 0;
975  pathnode->pathkeys = NIL; /* samplescan has unordered result */
976 
977  cost_samplescan(pathnode, root, rel, pathnode->param_info);
978 
979  return pathnode;
980 }
981 
982 /*
983  * create_index_path
984  * Creates a path node for an index scan.
985  *
986  * 'index' is a usable index.
987  * 'indexclauses' is a list of RestrictInfo nodes representing clauses
988  * to be used as index qual conditions in the scan.
989  * 'indexclausecols' is an integer list of index column numbers (zero based)
990  * the indexclauses can be used with.
991  * 'indexorderbys' is a list of bare expressions (no RestrictInfos)
992  * to be used as index ordering operators in the scan.
993  * 'indexorderbycols' is an integer list of index column numbers (zero based)
994  * the ordering operators can be used with.
995  * 'pathkeys' describes the ordering of the path.
996  * 'indexscandir' is ForwardScanDirection or BackwardScanDirection
997  * for an ordered index, or NoMovementScanDirection for
998  * an unordered index.
999  * 'indexonly' is true if an index-only scan is wanted.
1000  * 'required_outer' is the set of outer relids for a parameterized path.
1001  * 'loop_count' is the number of repetitions of the indexscan to factor into
1002  * estimates of caching behavior.
1003  * 'partial_path' is true if constructing a parallel index scan path.
1004  *
1005  * Returns the new path node.
1006  */
1007 IndexPath *
1010  List *indexclauses,
1011  List *indexclausecols,
1012  List *indexorderbys,
1013  List *indexorderbycols,
1014  List *pathkeys,
1015  ScanDirection indexscandir,
1016  bool indexonly,
1017  Relids required_outer,
1018  double loop_count,
1019  bool partial_path)
1020 {
1021  IndexPath *pathnode = makeNode(IndexPath);
1022  RelOptInfo *rel = index->rel;
1023  List *indexquals,
1024  *indexqualcols;
1025 
1026  pathnode->path.pathtype = indexonly ? T_IndexOnlyScan : T_IndexScan;
1027  pathnode->path.parent = rel;
1028  pathnode->path.pathtarget = rel->reltarget;
1029  pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1030  required_outer);
1031  pathnode->path.parallel_aware = false;
1032  pathnode->path.parallel_safe = rel->consider_parallel;
1033  pathnode->path.parallel_workers = 0;
1034  pathnode->path.pathkeys = pathkeys;
1035 
1036  /* Convert clauses to indexquals the executor can handle */
1037  expand_indexqual_conditions(index, indexclauses, indexclausecols,
1038  &indexquals, &indexqualcols);
1039 
1040  /* Fill in the pathnode */
1041  pathnode->indexinfo = index;
1042  pathnode->indexclauses = indexclauses;
1043  pathnode->indexquals = indexquals;
1044  pathnode->indexqualcols = indexqualcols;
1045  pathnode->indexorderbys = indexorderbys;
1046  pathnode->indexorderbycols = indexorderbycols;
1047  pathnode->indexscandir = indexscandir;
1048 
1049  cost_index(pathnode, root, loop_count, partial_path);
1050 
1051  return pathnode;
1052 }
1053 
1054 /*
1055  * create_bitmap_heap_path
1056  * Creates a path node for a bitmap scan.
1057  *
1058  * 'bitmapqual' is a tree of IndexPath, BitmapAndPath, and BitmapOrPath nodes.
1059  * 'required_outer' is the set of outer relids for a parameterized path.
1060  * 'loop_count' is the number of repetitions of the indexscan to factor into
1061  * estimates of caching behavior.
1062  *
1063  * loop_count should match the value used when creating the component
1064  * IndexPaths.
1065  */
1068  RelOptInfo *rel,
1069  Path *bitmapqual,
1070  Relids required_outer,
1071  double loop_count,
1072  int parallel_degree)
1073 {
1074  BitmapHeapPath *pathnode = makeNode(BitmapHeapPath);
1075 
1076  pathnode->path.pathtype = T_BitmapHeapScan;
1077  pathnode->path.parent = rel;
1078  pathnode->path.pathtarget = rel->reltarget;
1079  pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1080  required_outer);
1081  pathnode->path.parallel_aware = parallel_degree > 0 ? true : false;
1082  pathnode->path.parallel_safe = rel->consider_parallel;
1083  pathnode->path.parallel_workers = parallel_degree;
1084  pathnode->path.pathkeys = NIL; /* always unordered */
1085 
1086  pathnode->bitmapqual = bitmapqual;
1087 
1088  cost_bitmap_heap_scan(&pathnode->path, root, rel,
1089  pathnode->path.param_info,
1090  bitmapqual, loop_count);
1091 
1092  return pathnode;
1093 }
1094 
1095 /*
1096  * create_bitmap_and_path
1097  * Creates a path node representing a BitmapAnd.
1098  */
1099 BitmapAndPath *
1101  RelOptInfo *rel,
1102  List *bitmapquals)
1103 {
1104  BitmapAndPath *pathnode = makeNode(BitmapAndPath);
1105 
1106  pathnode->path.pathtype = T_BitmapAnd;
1107  pathnode->path.parent = rel;
1108  pathnode->path.pathtarget = rel->reltarget;
1109  pathnode->path.param_info = NULL; /* not used in bitmap trees */
1110 
1111  /*
1112  * Currently, a BitmapHeapPath, BitmapAndPath, or BitmapOrPath will be
1113  * parallel-safe if and only if rel->consider_parallel is set. So, we can
1114  * set the flag for this path based only on the relation-level flag,
1115  * without actually iterating over the list of children.
1116  */
1117  pathnode->path.parallel_aware = false;
1118  pathnode->path.parallel_safe = rel->consider_parallel;
1119  pathnode->path.parallel_workers = 0;
1120 
1121  pathnode->path.pathkeys = NIL; /* always unordered */
1122 
1123  pathnode->bitmapquals = bitmapquals;
1124 
1125  /* this sets bitmapselectivity as well as the regular cost fields: */
1126  cost_bitmap_and_node(pathnode, root);
1127 
1128  return pathnode;
1129 }
1130 
1131 /*
1132  * create_bitmap_or_path
1133  * Creates a path node representing a BitmapOr.
1134  */
1135 BitmapOrPath *
1137  RelOptInfo *rel,
1138  List *bitmapquals)
1139 {
1140  BitmapOrPath *pathnode = makeNode(BitmapOrPath);
1141 
1142  pathnode->path.pathtype = T_BitmapOr;
1143  pathnode->path.parent = rel;
1144  pathnode->path.pathtarget = rel->reltarget;
1145  pathnode->path.param_info = NULL; /* not used in bitmap trees */
1146 
1147  /*
1148  * Currently, a BitmapHeapPath, BitmapAndPath, or BitmapOrPath will be
1149  * parallel-safe if and only if rel->consider_parallel is set. So, we can
1150  * set the flag for this path based only on the relation-level flag,
1151  * without actually iterating over the list of children.
1152  */
1153  pathnode->path.parallel_aware = false;
1154  pathnode->path.parallel_safe = rel->consider_parallel;
1155  pathnode->path.parallel_workers = 0;
1156 
1157  pathnode->path.pathkeys = NIL; /* always unordered */
1158 
1159  pathnode->bitmapquals = bitmapquals;
1160 
1161  /* this sets bitmapselectivity as well as the regular cost fields: */
1162  cost_bitmap_or_node(pathnode, root);
1163 
1164  return pathnode;
1165 }
1166 
1167 /*
1168  * create_tidscan_path
1169  * Creates a path corresponding to a scan by TID, returning the pathnode.
1170  */
1171 TidPath *
1173  Relids required_outer)
1174 {
1175  TidPath *pathnode = makeNode(TidPath);
1176 
1177  pathnode->path.pathtype = T_TidScan;
1178  pathnode->path.parent = rel;
1179  pathnode->path.pathtarget = rel->reltarget;
1180  pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1181  required_outer);
1182  pathnode->path.parallel_aware = false;
1183  pathnode->path.parallel_safe = rel->consider_parallel;
1184  pathnode->path.parallel_workers = 0;
1185  pathnode->path.pathkeys = NIL; /* always unordered */
1186 
1187  pathnode->tidquals = tidquals;
1188 
1189  cost_tidscan(&pathnode->path, root, rel, tidquals,
1190  pathnode->path.param_info);
1191 
1192  return pathnode;
1193 }
1194 
1195 /*
1196  * create_append_path
1197  * Creates a path corresponding to an Append plan, returning the
1198  * pathnode.
1199  *
1200  * Note that we must handle subpaths = NIL, representing a dummy access path.
1201  */
1202 AppendPath *
1203 create_append_path(RelOptInfo *rel, List *subpaths, Relids required_outer,
1204  int parallel_workers, List *partitioned_rels)
1205 {
1206  AppendPath *pathnode = makeNode(AppendPath);
1207  ListCell *l;
1208 
1209  pathnode->path.pathtype = T_Append;
1210  pathnode->path.parent = rel;
1211  pathnode->path.pathtarget = rel->reltarget;
1213  required_outer);
1214  pathnode->path.parallel_aware = false;
1215  pathnode->path.parallel_safe = rel->consider_parallel;
1216  pathnode->path.parallel_workers = parallel_workers;
1217  pathnode->path.pathkeys = NIL; /* result is always considered
1218  * unsorted */
1219  pathnode->partitioned_rels = partitioned_rels;
1220  pathnode->subpaths = subpaths;
1221 
1222  /*
1223  * We don't bother with inventing a cost_append(), but just do it here.
1224  *
1225  * Compute rows and costs as sums of subplan rows and costs. We charge
1226  * nothing extra for the Append itself, which perhaps is too optimistic,
1227  * but since it doesn't do any selection or projection, it is a pretty
1228  * cheap node.
1229  */
1230  pathnode->path.rows = 0;
1231  pathnode->path.startup_cost = 0;
1232  pathnode->path.total_cost = 0;
1233  foreach(l, subpaths)
1234  {
1235  Path *subpath = (Path *) lfirst(l);
1236 
1237  pathnode->path.rows += subpath->rows;
1238 
1239  if (l == list_head(subpaths)) /* first node? */
1240  pathnode->path.startup_cost = subpath->startup_cost;
1241  pathnode->path.total_cost += subpath->total_cost;
1242  pathnode->path.parallel_safe = pathnode->path.parallel_safe &&
1243  subpath->parallel_safe;
1244 
1245  /* All child paths must have same parameterization */
1246  Assert(bms_equal(PATH_REQ_OUTER(subpath), required_outer));
1247  }
1248 
1249  return pathnode;
1250 }
1251 
1252 /*
1253  * create_merge_append_path
1254  * Creates a path corresponding to a MergeAppend plan, returning the
1255  * pathnode.
1256  */
1259  RelOptInfo *rel,
1260  List *subpaths,
1261  List *pathkeys,
1262  Relids required_outer,
1263  List *partitioned_rels)
1264 {
1266  Cost input_startup_cost;
1267  Cost input_total_cost;
1268  ListCell *l;
1269 
1270  pathnode->path.pathtype = T_MergeAppend;
1271  pathnode->path.parent = rel;
1272  pathnode->path.pathtarget = rel->reltarget;
1274  required_outer);
1275  pathnode->path.parallel_aware = false;
1276  pathnode->path.parallel_safe = rel->consider_parallel;
1277  pathnode->path.parallel_workers = 0;
1278  pathnode->path.pathkeys = pathkeys;
1279  pathnode->partitioned_rels = partitioned_rels;
1280  pathnode->subpaths = subpaths;
1281 
1282  /*
1283  * Apply query-wide LIMIT if known and path is for sole base relation.
1284  * (Handling this at this low level is a bit klugy.)
1285  */
1286  if (bms_equal(rel->relids, root->all_baserels))
1287  pathnode->limit_tuples = root->limit_tuples;
1288  else
1289  pathnode->limit_tuples = -1.0;
1290 
1291  /*
1292  * Add up the sizes and costs of the input paths.
1293  */
1294  pathnode->path.rows = 0;
1295  input_startup_cost = 0;
1296  input_total_cost = 0;
1297  foreach(l, subpaths)
1298  {
1299  Path *subpath = (Path *) lfirst(l);
1300 
1301  pathnode->path.rows += subpath->rows;
1302  pathnode->path.parallel_safe = pathnode->path.parallel_safe &&
1303  subpath->parallel_safe;
1304 
1305  if (pathkeys_contained_in(pathkeys, subpath->pathkeys))
1306  {
1307  /* Subpath is adequately ordered, we won't need to sort it */
1308  input_startup_cost += subpath->startup_cost;
1309  input_total_cost += subpath->total_cost;
1310  }
1311  else
1312  {
1313  /* We'll need to insert a Sort node, so include cost for that */
1314  Path sort_path; /* dummy for result of cost_sort */
1315 
1316  cost_sort(&sort_path,
1317  root,
1318  pathkeys,
1319  subpath->total_cost,
1320  subpath->parent->tuples,
1321  subpath->pathtarget->width,
1322  0.0,
1323  work_mem,
1324  pathnode->limit_tuples);
1325  input_startup_cost += sort_path.startup_cost;
1326  input_total_cost += sort_path.total_cost;
1327  }
1328 
1329  /* All child paths must have same parameterization */
1330  Assert(bms_equal(PATH_REQ_OUTER(subpath), required_outer));
1331  }
1332 
1333  /* Now we can compute total costs of the MergeAppend */
1334  cost_merge_append(&pathnode->path, root,
1335  pathkeys, list_length(subpaths),
1336  input_startup_cost, input_total_cost,
1337  pathnode->path.rows);
1338 
1339  return pathnode;
1340 }
1341 
1342 /*
1343  * create_result_path
1344  * Creates a path representing a Result-and-nothing-else plan.
1345  *
1346  * This is only used for degenerate cases, such as a query with an empty
1347  * jointree.
1348  */
1349 ResultPath *
1351  PathTarget *target, List *resconstantqual)
1352 {
1353  ResultPath *pathnode = makeNode(ResultPath);
1354 
1355  pathnode->path.pathtype = T_Result;
1356  pathnode->path.parent = rel;
1357  pathnode->path.pathtarget = target;
1358  pathnode->path.param_info = NULL; /* there are no other rels... */
1359  pathnode->path.parallel_aware = false;
1360  pathnode->path.parallel_safe = rel->consider_parallel;
1361  pathnode->path.parallel_workers = 0;
1362  pathnode->path.pathkeys = NIL;
1363  pathnode->quals = resconstantqual;
1364 
1365  /* Hardly worth defining a cost_result() function ... just do it */
1366  pathnode->path.rows = 1;
1367  pathnode->path.startup_cost = target->cost.startup;
1368  pathnode->path.total_cost = target->cost.startup +
1369  cpu_tuple_cost + target->cost.per_tuple;
1370  if (resconstantqual)
1371  {
1372  QualCost qual_cost;
1373 
1374  cost_qual_eval(&qual_cost, resconstantqual, root);
1375  /* resconstantqual is evaluated once at startup */
1376  pathnode->path.startup_cost += qual_cost.startup + qual_cost.per_tuple;
1377  pathnode->path.total_cost += qual_cost.startup + qual_cost.per_tuple;
1378  }
1379 
1380  return pathnode;
1381 }
1382 
1383 /*
1384  * create_material_path
1385  * Creates a path corresponding to a Material plan, returning the
1386  * pathnode.
1387  */
1388 MaterialPath *
1390 {
1391  MaterialPath *pathnode = makeNode(MaterialPath);
1392 
1393  Assert(subpath->parent == rel);
1394 
1395  pathnode->path.pathtype = T_Material;
1396  pathnode->path.parent = rel;
1397  pathnode->path.pathtarget = rel->reltarget;
1398  pathnode->path.param_info = subpath->param_info;
1399  pathnode->path.parallel_aware = false;
1400  pathnode->path.parallel_safe = rel->consider_parallel &&
1401  subpath->parallel_safe;
1402  pathnode->path.parallel_workers = subpath->parallel_workers;
1403  pathnode->path.pathkeys = subpath->pathkeys;
1404 
1405  pathnode->subpath = subpath;
1406 
1407  cost_material(&pathnode->path,
1408  subpath->startup_cost,
1409  subpath->total_cost,
1410  subpath->rows,
1411  subpath->pathtarget->width);
1412 
1413  return pathnode;
1414 }
1415 
1416 /*
1417  * create_unique_path
1418  * Creates a path representing elimination of distinct rows from the
1419  * input data. Distinct-ness is defined according to the needs of the
1420  * semijoin represented by sjinfo. If it is not possible to identify
1421  * how to make the data unique, NULL is returned.
1422  *
1423  * If used at all, this is likely to be called repeatedly on the same rel;
1424  * and the input subpath should always be the same (the cheapest_total path
1425  * for the rel). So we cache the result.
1426  */
1427 UniquePath *
1429  SpecialJoinInfo *sjinfo)
1430 {
1431  UniquePath *pathnode;
1432  Path sort_path; /* dummy for result of cost_sort */
1433  Path agg_path; /* dummy for result of cost_agg */
1434  MemoryContext oldcontext;
1435  int numCols;
1436 
1437  /* Caller made a mistake if subpath isn't cheapest_total ... */
1438  Assert(subpath == rel->cheapest_total_path);
1439  Assert(subpath->parent == rel);
1440  /* ... or if SpecialJoinInfo is the wrong one */
1441  Assert(sjinfo->jointype == JOIN_SEMI);
1442  Assert(bms_equal(rel->relids, sjinfo->syn_righthand));
1443 
1444  /* If result already cached, return it */
1445  if (rel->cheapest_unique_path)
1446  return (UniquePath *) rel->cheapest_unique_path;
1447 
1448  /* If it's not possible to unique-ify, return NULL */
1449  if (!(sjinfo->semi_can_btree || sjinfo->semi_can_hash))
1450  return NULL;
1451 
1452  /*
1453  * We must ensure path struct and subsidiary data are allocated in main
1454  * planning context; otherwise GEQO memory management causes trouble.
1455  */
1456  oldcontext = MemoryContextSwitchTo(root->planner_cxt);
1457 
1458  pathnode = makeNode(UniquePath);
1459 
1460  pathnode->path.pathtype = T_Unique;
1461  pathnode->path.parent = rel;
1462  pathnode->path.pathtarget = rel->reltarget;
1463  pathnode->path.param_info = subpath->param_info;
1464  pathnode->path.parallel_aware = false;
1465  pathnode->path.parallel_safe = rel->consider_parallel &&
1466  subpath->parallel_safe;
1467  pathnode->path.parallel_workers = subpath->parallel_workers;
1468 
1469  /*
1470  * Assume the output is unsorted, since we don't necessarily have pathkeys
1471  * to represent it. (This might get overridden below.)
1472  */
1473  pathnode->path.pathkeys = NIL;
1474 
1475  pathnode->subpath = subpath;
1476  pathnode->in_operators = sjinfo->semi_operators;
1477  pathnode->uniq_exprs = sjinfo->semi_rhs_exprs;
1478 
1479  /*
1480  * If the input is a relation and it has a unique index that proves the
1481  * semi_rhs_exprs are unique, then we don't need to do anything. Note
1482  * that relation_has_unique_index_for automatically considers restriction
1483  * clauses for the rel, as well.
1484  */
1485  if (rel->rtekind == RTE_RELATION && sjinfo->semi_can_btree &&
1487  sjinfo->semi_rhs_exprs,
1488  sjinfo->semi_operators))
1489  {
1490  pathnode->umethod = UNIQUE_PATH_NOOP;
1491  pathnode->path.rows = rel->rows;
1492  pathnode->path.startup_cost = subpath->startup_cost;
1493  pathnode->path.total_cost = subpath->total_cost;
1494  pathnode->path.pathkeys = subpath->pathkeys;
1495 
1496  rel->cheapest_unique_path = (Path *) pathnode;
1497 
1498  MemoryContextSwitchTo(oldcontext);
1499 
1500  return pathnode;
1501  }
1502 
1503  /*
1504  * If the input is a subquery whose output must be unique already, then we
1505  * don't need to do anything. The test for uniqueness has to consider
1506  * exactly which columns we are extracting; for example "SELECT DISTINCT
1507  * x,y" doesn't guarantee that x alone is distinct. So we cannot check for
1508  * this optimization unless semi_rhs_exprs consists only of simple Vars
1509  * referencing subquery outputs. (Possibly we could do something with
1510  * expressions in the subquery outputs, too, but for now keep it simple.)
1511  */
1512  if (rel->rtekind == RTE_SUBQUERY)
1513  {
1514  RangeTblEntry *rte = planner_rt_fetch(rel->relid, root);
1515 
1517  {
1518  List *sub_tlist_colnos;
1519 
1520  sub_tlist_colnos = translate_sub_tlist(sjinfo->semi_rhs_exprs,
1521  rel->relid);
1522 
1523  if (sub_tlist_colnos &&
1525  sub_tlist_colnos,
1526  sjinfo->semi_operators))
1527  {
1528  pathnode->umethod = UNIQUE_PATH_NOOP;
1529  pathnode->path.rows = rel->rows;
1530  pathnode->path.startup_cost = subpath->startup_cost;
1531  pathnode->path.total_cost = subpath->total_cost;
1532  pathnode->path.pathkeys = subpath->pathkeys;
1533 
1534  rel->cheapest_unique_path = (Path *) pathnode;
1535 
1536  MemoryContextSwitchTo(oldcontext);
1537 
1538  return pathnode;
1539  }
1540  }
1541  }
1542 
1543  /* Estimate number of output rows */
1544  pathnode->path.rows = estimate_num_groups(root,
1545  sjinfo->semi_rhs_exprs,
1546  rel->rows,
1547  NULL);
1548  numCols = list_length(sjinfo->semi_rhs_exprs);
1549 
1550  if (sjinfo->semi_can_btree)
1551  {
1552  /*
1553  * Estimate cost for sort+unique implementation
1554  */
1555  cost_sort(&sort_path, root, NIL,
1556  subpath->total_cost,
1557  rel->rows,
1558  subpath->pathtarget->width,
1559  0.0,
1560  work_mem,
1561  -1.0);
1562 
1563  /*
1564  * Charge one cpu_operator_cost per comparison per input tuple. We
1565  * assume all columns get compared at most of the tuples. (XXX
1566  * probably this is an overestimate.) This should agree with
1567  * create_upper_unique_path.
1568  */
1569  sort_path.total_cost += cpu_operator_cost * rel->rows * numCols;
1570  }
1571 
1572  if (sjinfo->semi_can_hash)
1573  {
1574  /*
1575  * Estimate the overhead per hashtable entry at 64 bytes (same as in
1576  * planner.c).
1577  */
1578  int hashentrysize = subpath->pathtarget->width + 64;
1579 
1580  if (hashentrysize * pathnode->path.rows > work_mem * 1024L)
1581  {
1582  /*
1583  * We should not try to hash. Hack the SpecialJoinInfo to
1584  * remember this, in case we come through here again.
1585  */
1586  sjinfo->semi_can_hash = false;
1587  }
1588  else
1589  cost_agg(&agg_path, root,
1590  AGG_HASHED, NULL,
1591  numCols, pathnode->path.rows,
1592  subpath->startup_cost,
1593  subpath->total_cost,
1594  rel->rows);
1595  }
1596 
1597  if (sjinfo->semi_can_btree && sjinfo->semi_can_hash)
1598  {
1599  if (agg_path.total_cost < sort_path.total_cost)
1600  pathnode->umethod = UNIQUE_PATH_HASH;
1601  else
1602  pathnode->umethod = UNIQUE_PATH_SORT;
1603  }
1604  else if (sjinfo->semi_can_btree)
1605  pathnode->umethod = UNIQUE_PATH_SORT;
1606  else if (sjinfo->semi_can_hash)
1607  pathnode->umethod = UNIQUE_PATH_HASH;
1608  else
1609  {
1610  /* we can get here only if we abandoned hashing above */
1611  MemoryContextSwitchTo(oldcontext);
1612  return NULL;
1613  }
1614 
1615  if (pathnode->umethod == UNIQUE_PATH_HASH)
1616  {
1617  pathnode->path.startup_cost = agg_path.startup_cost;
1618  pathnode->path.total_cost = agg_path.total_cost;
1619  }
1620  else
1621  {
1622  pathnode->path.startup_cost = sort_path.startup_cost;
1623  pathnode->path.total_cost = sort_path.total_cost;
1624  }
1625 
1626  rel->cheapest_unique_path = (Path *) pathnode;
1627 
1628  MemoryContextSwitchTo(oldcontext);
1629 
1630  return pathnode;
1631 }
1632 
1633 /*
1634  * create_gather_merge_path
1635  *
1636  * Creates a path corresponding to a gather merge scan, returning
1637  * the pathnode.
1638  */
1641  PathTarget *target, List *pathkeys,
1642  Relids required_outer, double *rows)
1643 {
1645  Cost input_startup_cost = 0;
1646  Cost input_total_cost = 0;
1647 
1648  Assert(subpath->parallel_safe);
1649  Assert(pathkeys);
1650 
1651  pathnode->path.pathtype = T_GatherMerge;
1652  pathnode->path.parent = rel;
1653  pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1654  required_outer);
1655  pathnode->path.parallel_aware = false;
1656 
1657  pathnode->subpath = subpath;
1658  pathnode->num_workers = subpath->parallel_workers;
1659  pathnode->path.pathkeys = pathkeys;
1660  pathnode->path.pathtarget = target ? target : rel->reltarget;
1661  pathnode->path.rows += subpath->rows;
1662 
1663  if (pathkeys_contained_in(pathkeys, subpath->pathkeys))
1664  {
1665  /* Subpath is adequately ordered, we won't need to sort it */
1666  input_startup_cost += subpath->startup_cost;
1667  input_total_cost += subpath->total_cost;
1668  }
1669  else
1670  {
1671  /* We'll need to insert a Sort node, so include cost for that */
1672  Path sort_path; /* dummy for result of cost_sort */
1673 
1674  cost_sort(&sort_path,
1675  root,
1676  pathkeys,
1677  subpath->total_cost,
1678  subpath->rows,
1679  subpath->pathtarget->width,
1680  0.0,
1681  work_mem,
1682  -1);
1683  input_startup_cost += sort_path.startup_cost;
1684  input_total_cost += sort_path.total_cost;
1685  }
1686 
1687  cost_gather_merge(pathnode, root, rel, pathnode->path.param_info,
1688  input_startup_cost, input_total_cost, rows);
1689 
1690  return pathnode;
1691 }
1692 
1693 /*
1694  * translate_sub_tlist - get subquery column numbers represented by tlist
1695  *
1696  * The given targetlist usually contains only Vars referencing the given relid.
1697  * Extract their varattnos (ie, the column numbers of the subquery) and return
1698  * as an integer List.
1699  *
1700  * If any of the tlist items is not a simple Var, we cannot determine whether
1701  * the subquery's uniqueness condition (if any) matches ours, so punt and
1702  * return NIL.
1703  */
1704 static List *
1705 translate_sub_tlist(List *tlist, int relid)
1706 {
1707  List *result = NIL;
1708  ListCell *l;
1709 
1710  foreach(l, tlist)
1711  {
1712  Var *var = (Var *) lfirst(l);
1713 
1714  if (!var || !IsA(var, Var) ||
1715  var->varno != relid)
1716  return NIL; /* punt */
1717 
1718  result = lappend_int(result, var->varattno);
1719  }
1720  return result;
1721 }
1722 
1723 /*
1724  * create_gather_path
1725  * Creates a path corresponding to a gather scan, returning the
1726  * pathnode.
1727  *
1728  * 'rows' may optionally be set to override row estimates from other sources.
1729  */
1730 GatherPath *
1732  PathTarget *target, Relids required_outer, double *rows)
1733 {
1734  GatherPath *pathnode = makeNode(GatherPath);
1735 
1736  Assert(subpath->parallel_safe);
1737 
1738  pathnode->path.pathtype = T_Gather;
1739  pathnode->path.parent = rel;
1740  pathnode->path.pathtarget = target;
1741  pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1742  required_outer);
1743  pathnode->path.parallel_aware = false;
1744  pathnode->path.parallel_safe = false;
1745  pathnode->path.parallel_workers = subpath->parallel_workers;
1746  pathnode->path.pathkeys = NIL; /* Gather has unordered result */
1747 
1748  pathnode->subpath = subpath;
1749  pathnode->single_copy = false;
1750 
1751  if (pathnode->path.parallel_workers == 0)
1752  {
1753  pathnode->path.parallel_workers = 1;
1754  pathnode->path.pathkeys = subpath->pathkeys;
1755  pathnode->single_copy = true;
1756  }
1757 
1758  cost_gather(pathnode, root, rel, pathnode->path.param_info, rows);
1759 
1760  return pathnode;
1761 }
1762 
1763 /*
1764  * create_subqueryscan_path
1765  * Creates a path corresponding to a scan of a subquery,
1766  * returning the pathnode.
1767  */
1770  List *pathkeys, Relids required_outer)
1771 {
1773 
1774  pathnode->path.pathtype = T_SubqueryScan;
1775  pathnode->path.parent = rel;
1776  pathnode->path.pathtarget = rel->reltarget;
1777  pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1778  required_outer);
1779  pathnode->path.parallel_aware = false;
1780  pathnode->path.parallel_safe = rel->consider_parallel &&
1781  subpath->parallel_safe;
1782  pathnode->path.parallel_workers = subpath->parallel_workers;
1783  pathnode->path.pathkeys = pathkeys;
1784  pathnode->subpath = subpath;
1785 
1786  cost_subqueryscan(pathnode, root, rel, pathnode->path.param_info);
1787 
1788  return pathnode;
1789 }
1790 
1791 /*
1792  * create_functionscan_path
1793  * Creates a path corresponding to a sequential scan of a function,
1794  * returning the pathnode.
1795  */
1796 Path *
1798  List *pathkeys, Relids required_outer)
1799 {
1800  Path *pathnode = makeNode(Path);
1801 
1802  pathnode->pathtype = T_FunctionScan;
1803  pathnode->parent = rel;
1804  pathnode->pathtarget = rel->reltarget;
1805  pathnode->param_info = get_baserel_parampathinfo(root, rel,
1806  required_outer);
1807  pathnode->parallel_aware = false;
1808  pathnode->parallel_safe = rel->consider_parallel;
1809  pathnode->parallel_workers = 0;
1810  pathnode->pathkeys = pathkeys;
1811 
1812  cost_functionscan(pathnode, root, rel, pathnode->param_info);
1813 
1814  return pathnode;
1815 }
1816 
1817 /*
1818  * create_tablefuncscan_path
1819  * Creates a path corresponding to a sequential scan of a table function,
1820  * returning the pathnode.
1821  */
1822 Path *
1824  Relids required_outer)
1825 {
1826  Path *pathnode = makeNode(Path);
1827 
1828  pathnode->pathtype = T_TableFuncScan;
1829  pathnode->parent = rel;
1830  pathnode->pathtarget = rel->reltarget;
1831  pathnode->param_info = get_baserel_parampathinfo(root, rel,
1832  required_outer);
1833  pathnode->parallel_aware = false;
1834  pathnode->parallel_safe = rel->consider_parallel;
1835  pathnode->parallel_workers = 0;
1836  pathnode->pathkeys = NIL; /* result is always unordered */
1837 
1838  cost_tablefuncscan(pathnode, root, rel, pathnode->param_info);
1839 
1840  return pathnode;
1841 }
1842 
1843 /*
1844  * create_valuesscan_path
1845  * Creates a path corresponding to a scan of a VALUES list,
1846  * returning the pathnode.
1847  */
1848 Path *
1850  Relids required_outer)
1851 {
1852  Path *pathnode = makeNode(Path);
1853 
1854  pathnode->pathtype = T_ValuesScan;
1855  pathnode->parent = rel;
1856  pathnode->pathtarget = rel->reltarget;
1857  pathnode->param_info = get_baserel_parampathinfo(root, rel,
1858  required_outer);
1859  pathnode->parallel_aware = false;
1860  pathnode->parallel_safe = rel->consider_parallel;
1861  pathnode->parallel_workers = 0;
1862  pathnode->pathkeys = NIL; /* result is always unordered */
1863 
1864  cost_valuesscan(pathnode, root, rel, pathnode->param_info);
1865 
1866  return pathnode;
1867 }
1868 
1869 /*
1870  * create_ctescan_path
1871  * Creates a path corresponding to a scan of a non-self-reference CTE,
1872  * returning the pathnode.
1873  */
1874 Path *
1875 create_ctescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
1876 {
1877  Path *pathnode = makeNode(Path);
1878 
1879  pathnode->pathtype = T_CteScan;
1880  pathnode->parent = rel;
1881  pathnode->pathtarget = rel->reltarget;
1882  pathnode->param_info = get_baserel_parampathinfo(root, rel,
1883  required_outer);
1884  pathnode->parallel_aware = false;
1885  pathnode->parallel_safe = rel->consider_parallel;
1886  pathnode->parallel_workers = 0;
1887  pathnode->pathkeys = NIL; /* XXX for now, result is always unordered */
1888 
1889  cost_ctescan(pathnode, root, rel, pathnode->param_info);
1890 
1891  return pathnode;
1892 }
1893 
1894 /*
1895  * create_worktablescan_path
1896  * Creates a path corresponding to a scan of a self-reference CTE,
1897  * returning the pathnode.
1898  */
1899 Path *
1901  Relids required_outer)
1902 {
1903  Path *pathnode = makeNode(Path);
1904 
1905  pathnode->pathtype = T_WorkTableScan;
1906  pathnode->parent = rel;
1907  pathnode->pathtarget = rel->reltarget;
1908  pathnode->param_info = get_baserel_parampathinfo(root, rel,
1909  required_outer);
1910  pathnode->parallel_aware = false;
1911  pathnode->parallel_safe = rel->consider_parallel;
1912  pathnode->parallel_workers = 0;
1913  pathnode->pathkeys = NIL; /* result is always unordered */
1914 
1915  /* Cost is the same as for a regular CTE scan */
1916  cost_ctescan(pathnode, root, rel, pathnode->param_info);
1917 
1918  return pathnode;
1919 }
1920 
1921 /*
1922  * create_foreignscan_path
1923  * Creates a path corresponding to a scan of a foreign table, foreign join,
1924  * or foreign upper-relation processing, returning the pathnode.
1925  *
1926  * This function is never called from core Postgres; rather, it's expected
1927  * to be called by the GetForeignPaths, GetForeignJoinPaths, or
1928  * GetForeignUpperPaths function of a foreign data wrapper. We make the FDW
1929  * supply all fields of the path, since we do not have any way to calculate
1930  * them in core. However, there is a usually-sane default for the pathtarget
1931  * (rel->reltarget), so we let a NULL for "target" select that.
1932  */
1933 ForeignPath *
1935  PathTarget *target,
1936  double rows, Cost startup_cost, Cost total_cost,
1937  List *pathkeys,
1938  Relids required_outer,
1939  Path *fdw_outerpath,
1940  List *fdw_private)
1941 {
1942  ForeignPath *pathnode = makeNode(ForeignPath);
1943 
1944  pathnode->path.pathtype = T_ForeignScan;
1945  pathnode->path.parent = rel;
1946  pathnode->path.pathtarget = target ? target : rel->reltarget;
1947  pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1948  required_outer);
1949  pathnode->path.parallel_aware = false;
1950  pathnode->path.parallel_safe = rel->consider_parallel;
1951  pathnode->path.parallel_workers = 0;
1952  pathnode->path.rows = rows;
1953  pathnode->path.startup_cost = startup_cost;
1954  pathnode->path.total_cost = total_cost;
1955  pathnode->path.pathkeys = pathkeys;
1956 
1957  pathnode->fdw_outerpath = fdw_outerpath;
1958  pathnode->fdw_private = fdw_private;
1959 
1960  return pathnode;
1961 }
1962 
1963 /*
1964  * calc_nestloop_required_outer
1965  * Compute the required_outer set for a nestloop join path
1966  *
1967  * Note: result must not share storage with either input
1968  */
1969 Relids
1970 calc_nestloop_required_outer(Path *outer_path, Path *inner_path)
1971 {
1972  Relids outer_paramrels = PATH_REQ_OUTER(outer_path);
1973  Relids inner_paramrels = PATH_REQ_OUTER(inner_path);
1974  Relids required_outer;
1975 
1976  /* inner_path can require rels from outer path, but not vice versa */
1977  Assert(!bms_overlap(outer_paramrels, inner_path->parent->relids));
1978  /* easy case if inner path is not parameterized */
1979  if (!inner_paramrels)
1980  return bms_copy(outer_paramrels);
1981  /* else, form the union ... */
1982  required_outer = bms_union(outer_paramrels, inner_paramrels);
1983  /* ... and remove any mention of now-satisfied outer rels */
1984  required_outer = bms_del_members(required_outer,
1985  outer_path->parent->relids);
1986  /* maintain invariant that required_outer is exactly NULL if empty */
1987  if (bms_is_empty(required_outer))
1988  {
1989  bms_free(required_outer);
1990  required_outer = NULL;
1991  }
1992  return required_outer;
1993 }
1994 
1995 /*
1996  * calc_non_nestloop_required_outer
1997  * Compute the required_outer set for a merge or hash join path
1998  *
1999  * Note: result must not share storage with either input
2000  */
2001 Relids
2002 calc_non_nestloop_required_outer(Path *outer_path, Path *inner_path)
2003 {
2004  Relids outer_paramrels = PATH_REQ_OUTER(outer_path);
2005  Relids inner_paramrels = PATH_REQ_OUTER(inner_path);
2006  Relids required_outer;
2007 
2008  /* neither path can require rels from the other */
2009  Assert(!bms_overlap(outer_paramrels, inner_path->parent->relids));
2010  Assert(!bms_overlap(inner_paramrels, outer_path->parent->relids));
2011  /* form the union ... */
2012  required_outer = bms_union(outer_paramrels, inner_paramrels);
2013  /* we do not need an explicit test for empty; bms_union gets it right */
2014  return required_outer;
2015 }
2016 
2017 /*
2018  * create_nestloop_path
2019  * Creates a pathnode corresponding to a nestloop join between two
2020  * relations.
2021  *
2022  * 'joinrel' is the join relation.
2023  * 'jointype' is the type of join required
2024  * 'workspace' is the result from initial_cost_nestloop
2025  * 'sjinfo' is extra info about the join for selectivity estimation
2026  * 'semifactors' contains valid data if jointype is SEMI or ANTI
2027  * 'outer_path' is the outer path
2028  * 'inner_path' is the inner path
2029  * 'restrict_clauses' are the RestrictInfo nodes to apply at the join
2030  * 'pathkeys' are the path keys of the new join path
2031  * 'required_outer' is the set of required outer rels
2032  *
2033  * Returns the resulting path node.
2034  */
2035 NestPath *
2037  RelOptInfo *joinrel,
2038  JoinType jointype,
2039  JoinCostWorkspace *workspace,
2040  SpecialJoinInfo *sjinfo,
2041  SemiAntiJoinFactors *semifactors,
2042  Path *outer_path,
2043  Path *inner_path,
2044  List *restrict_clauses,
2045  List *pathkeys,
2046  Relids required_outer)
2047 {
2048  NestPath *pathnode = makeNode(NestPath);
2049  Relids inner_req_outer = PATH_REQ_OUTER(inner_path);
2050 
2051  /*
2052  * If the inner path is parameterized by the outer, we must drop any
2053  * restrict_clauses that are due to be moved into the inner path. We have
2054  * to do this now, rather than postpone the work till createplan time,
2055  * because the restrict_clauses list can affect the size and cost
2056  * estimates for this path.
2057  */
2058  if (bms_overlap(inner_req_outer, outer_path->parent->relids))
2059  {
2060  Relids inner_and_outer = bms_union(inner_path->parent->relids,
2061  inner_req_outer);
2062  List *jclauses = NIL;
2063  ListCell *lc;
2064 
2065  foreach(lc, restrict_clauses)
2066  {
2067  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2068 
2069  if (!join_clause_is_movable_into(rinfo,
2070  inner_path->parent->relids,
2071  inner_and_outer))
2072  jclauses = lappend(jclauses, rinfo);
2073  }
2074  restrict_clauses = jclauses;
2075  }
2076 
2077  pathnode->path.pathtype = T_NestLoop;
2078  pathnode->path.parent = joinrel;
2079  pathnode->path.pathtarget = joinrel->reltarget;
2080  pathnode->path.param_info =
2082  joinrel,
2083  outer_path,
2084  inner_path,
2085  sjinfo,
2086  required_outer,
2087  &restrict_clauses);
2088  pathnode->path.parallel_aware = false;
2089  pathnode->path.parallel_safe = joinrel->consider_parallel &&
2090  outer_path->parallel_safe && inner_path->parallel_safe;
2091  /* This is a foolish way to estimate parallel_workers, but for now... */
2092  pathnode->path.parallel_workers = outer_path->parallel_workers;
2093  pathnode->path.pathkeys = pathkeys;
2094  pathnode->jointype = jointype;
2095  pathnode->outerjoinpath = outer_path;
2096  pathnode->innerjoinpath = inner_path;
2097  pathnode->joinrestrictinfo = restrict_clauses;
2098 
2099  final_cost_nestloop(root, pathnode, workspace, sjinfo, semifactors);
2100 
2101  return pathnode;
2102 }
2103 
2104 /*
2105  * create_mergejoin_path
2106  * Creates a pathnode corresponding to a mergejoin join between
2107  * two relations
2108  *
2109  * 'joinrel' is the join relation
2110  * 'jointype' is the type of join required
2111  * 'workspace' is the result from initial_cost_mergejoin
2112  * 'sjinfo' is extra info about the join for selectivity estimation
2113  * 'outer_path' is the outer path
2114  * 'inner_path' is the inner path
2115  * 'restrict_clauses' are the RestrictInfo nodes to apply at the join
2116  * 'pathkeys' are the path keys of the new join path
2117  * 'required_outer' is the set of required outer rels
2118  * 'mergeclauses' are the RestrictInfo nodes to use as merge clauses
2119  * (this should be a subset of the restrict_clauses list)
2120  * 'outersortkeys' are the sort varkeys for the outer relation
2121  * 'innersortkeys' are the sort varkeys for the inner relation
2122  */
2123 MergePath *
2125  RelOptInfo *joinrel,
2126  JoinType jointype,
2127  JoinCostWorkspace *workspace,
2128  SpecialJoinInfo *sjinfo,
2129  Path *outer_path,
2130  Path *inner_path,
2131  List *restrict_clauses,
2132  List *pathkeys,
2133  Relids required_outer,
2134  List *mergeclauses,
2135  List *outersortkeys,
2136  List *innersortkeys)
2137 {
2138  MergePath *pathnode = makeNode(MergePath);
2139 
2140  pathnode->jpath.path.pathtype = T_MergeJoin;
2141  pathnode->jpath.path.parent = joinrel;
2142  pathnode->jpath.path.pathtarget = joinrel->reltarget;
2143  pathnode->jpath.path.param_info =
2145  joinrel,
2146  outer_path,
2147  inner_path,
2148  sjinfo,
2149  required_outer,
2150  &restrict_clauses);
2151  pathnode->jpath.path.parallel_aware = false;
2152  pathnode->jpath.path.parallel_safe = joinrel->consider_parallel &&
2153  outer_path->parallel_safe && inner_path->parallel_safe;
2154  /* This is a foolish way to estimate parallel_workers, but for now... */
2155  pathnode->jpath.path.parallel_workers = outer_path->parallel_workers;
2156  pathnode->jpath.path.pathkeys = pathkeys;
2157  pathnode->jpath.jointype = jointype;
2158  pathnode->jpath.outerjoinpath = outer_path;
2159  pathnode->jpath.innerjoinpath = inner_path;
2160  pathnode->jpath.joinrestrictinfo = restrict_clauses;
2161  pathnode->path_mergeclauses = mergeclauses;
2162  pathnode->outersortkeys = outersortkeys;
2163  pathnode->innersortkeys = innersortkeys;
2164  /* pathnode->materialize_inner will be set by final_cost_mergejoin */
2165 
2166  final_cost_mergejoin(root, pathnode, workspace, sjinfo);
2167 
2168  return pathnode;
2169 }
2170 
2171 /*
2172  * create_hashjoin_path
2173  * Creates a pathnode corresponding to a hash join between two relations.
2174  *
2175  * 'joinrel' is the join relation
2176  * 'jointype' is the type of join required
2177  * 'workspace' is the result from initial_cost_hashjoin
2178  * 'sjinfo' is extra info about the join for selectivity estimation
2179  * 'semifactors' contains valid data if jointype is SEMI or ANTI
2180  * 'outer_path' is the cheapest outer path
2181  * 'inner_path' is the cheapest inner path
2182  * 'restrict_clauses' are the RestrictInfo nodes to apply at the join
2183  * 'required_outer' is the set of required outer rels
2184  * 'hashclauses' are the RestrictInfo nodes to use as hash clauses
2185  * (this should be a subset of the restrict_clauses list)
2186  */
2187 HashPath *
2189  RelOptInfo *joinrel,
2190  JoinType jointype,
2191  JoinCostWorkspace *workspace,
2192  SpecialJoinInfo *sjinfo,
2193  SemiAntiJoinFactors *semifactors,
2194  Path *outer_path,
2195  Path *inner_path,
2196  List *restrict_clauses,
2197  Relids required_outer,
2198  List *hashclauses)
2199 {
2200  HashPath *pathnode = makeNode(HashPath);
2201 
2202  pathnode->jpath.path.pathtype = T_HashJoin;
2203  pathnode->jpath.path.parent = joinrel;
2204  pathnode->jpath.path.pathtarget = joinrel->reltarget;
2205  pathnode->jpath.path.param_info =
2207  joinrel,
2208  outer_path,
2209  inner_path,
2210  sjinfo,
2211  required_outer,
2212  &restrict_clauses);
2213  pathnode->jpath.path.parallel_aware = false;
2214  pathnode->jpath.path.parallel_safe = joinrel->consider_parallel &&
2215  outer_path->parallel_safe && inner_path->parallel_safe;
2216  /* This is a foolish way to estimate parallel_workers, but for now... */
2217  pathnode->jpath.path.parallel_workers = outer_path->parallel_workers;
2218 
2219  /*
2220  * A hashjoin never has pathkeys, since its output ordering is
2221  * unpredictable due to possible batching. XXX If the inner relation is
2222  * small enough, we could instruct the executor that it must not batch,
2223  * and then we could assume that the output inherits the outer relation's
2224  * ordering, which might save a sort step. However there is considerable
2225  * downside if our estimate of the inner relation size is badly off. For
2226  * the moment we don't risk it. (Note also that if we wanted to take this
2227  * seriously, joinpath.c would have to consider many more paths for the
2228  * outer rel than it does now.)
2229  */
2230  pathnode->jpath.path.pathkeys = NIL;
2231  pathnode->jpath.jointype = jointype;
2232  pathnode->jpath.outerjoinpath = outer_path;
2233  pathnode->jpath.innerjoinpath = inner_path;
2234  pathnode->jpath.joinrestrictinfo = restrict_clauses;
2235  pathnode->path_hashclauses = hashclauses;
2236  /* final_cost_hashjoin will fill in pathnode->num_batches */
2237 
2238  final_cost_hashjoin(root, pathnode, workspace, sjinfo, semifactors);
2239 
2240  return pathnode;
2241 }
2242 
2243 /*
2244  * create_projection_path
2245  * Creates a pathnode that represents performing a projection.
2246  *
2247  * 'rel' is the parent relation associated with the result
2248  * 'subpath' is the path representing the source of data
2249  * 'target' is the PathTarget to be computed
2250  */
2253  RelOptInfo *rel,
2254  Path *subpath,
2255  PathTarget *target)
2256 {
2257  ProjectionPath *pathnode = makeNode(ProjectionPath);
2258  PathTarget *oldtarget = subpath->pathtarget;
2259 
2260  pathnode->path.pathtype = T_Result;
2261  pathnode->path.parent = rel;
2262  pathnode->path.pathtarget = target;
2263  /* For now, assume we are above any joins, so no parameterization */
2264  pathnode->path.param_info = NULL;
2265  pathnode->path.parallel_aware = false;
2266  pathnode->path.parallel_safe = rel->consider_parallel &&
2267  subpath->parallel_safe &&
2268  is_parallel_safe(root, (Node *) target->exprs);
2269  pathnode->path.parallel_workers = subpath->parallel_workers;
2270  /* Projection does not change the sort order */
2271  pathnode->path.pathkeys = subpath->pathkeys;
2272 
2273  pathnode->subpath = subpath;
2274 
2275  /*
2276  * We might not need a separate Result node. If the input plan node type
2277  * can project, we can just tell it to project something else. Or, if it
2278  * can't project but the desired target has the same expression list as
2279  * what the input will produce anyway, we can still give it the desired
2280  * tlist (possibly changing its ressortgroupref labels, but nothing else).
2281  * Note: in the latter case, create_projection_plan has to recheck our
2282  * conclusion; see comments therein.
2283  */
2284  if (is_projection_capable_path(subpath) ||
2285  equal(oldtarget->exprs, target->exprs))
2286  {
2287  /* No separate Result node needed */
2288  pathnode->dummypp = true;
2289 
2290  /*
2291  * Set cost of plan as subpath's cost, adjusted for tlist replacement.
2292  */
2293  pathnode->path.rows = subpath->rows;
2294  pathnode->path.startup_cost = subpath->startup_cost +
2295  (target->cost.startup - oldtarget->cost.startup);
2296  pathnode->path.total_cost = subpath->total_cost +
2297  (target->cost.startup - oldtarget->cost.startup) +
2298  (target->cost.per_tuple - oldtarget->cost.per_tuple) * subpath->rows;
2299  }
2300  else
2301  {
2302  /* We really do need the Result node */
2303  pathnode->dummypp = false;
2304 
2305  /*
2306  * The Result node's cost is cpu_tuple_cost per row, plus the cost of
2307  * evaluating the tlist. There is no qual to worry about.
2308  */
2309  pathnode->path.rows = subpath->rows;
2310  pathnode->path.startup_cost = subpath->startup_cost +
2311  target->cost.startup;
2312  pathnode->path.total_cost = subpath->total_cost +
2313  target->cost.startup +
2314  (cpu_tuple_cost + target->cost.per_tuple) * subpath->rows;
2315  }
2316 
2317  return pathnode;
2318 }
2319 
2320 /*
2321  * apply_projection_to_path
2322  * Add a projection step, or just apply the target directly to given path.
2323  *
2324  * This has the same net effect as create_projection_path(), except that if
2325  * a separate Result plan node isn't needed, we just replace the given path's
2326  * pathtarget with the desired one. This must be used only when the caller
2327  * knows that the given path isn't referenced elsewhere and so can be modified
2328  * in-place.
2329  *
2330  * If the input path is a GatherPath, we try to push the new target down to
2331  * its input as well; this is a yet more invasive modification of the input
2332  * path, which create_projection_path() can't do.
2333  *
2334  * Note that we mustn't change the source path's parent link; so when it is
2335  * add_path'd to "rel" things will be a bit inconsistent. So far that has
2336  * not caused any trouble.
2337  *
2338  * 'rel' is the parent relation associated with the result
2339  * 'path' is the path representing the source of data
2340  * 'target' is the PathTarget to be computed
2341  */
2342 Path *
2344  RelOptInfo *rel,
2345  Path *path,
2346  PathTarget *target)
2347 {
2348  QualCost oldcost;
2349 
2350  /*
2351  * If given path can't project, we might need a Result node, so make a
2352  * separate ProjectionPath.
2353  */
2354  if (!is_projection_capable_path(path))
2355  return (Path *) create_projection_path(root, rel, path, target);
2356 
2357  /*
2358  * We can just jam the desired tlist into the existing path, being sure to
2359  * update its cost estimates appropriately.
2360  */
2361  oldcost = path->pathtarget->cost;
2362  path->pathtarget = target;
2363 
2364  path->startup_cost += target->cost.startup - oldcost.startup;
2365  path->total_cost += target->cost.startup - oldcost.startup +
2366  (target->cost.per_tuple - oldcost.per_tuple) * path->rows;
2367 
2368  /*
2369  * If the path happens to be a Gather path, we'd like to arrange for the
2370  * subpath to return the required target list so that workers can help
2371  * project. But if there is something that is not parallel-safe in the
2372  * target expressions, then we can't.
2373  */
2374  if (IsA(path, GatherPath) &&
2375  is_parallel_safe(root, (Node *) target->exprs))
2376  {
2377  GatherPath *gpath = (GatherPath *) path;
2378 
2379  /*
2380  * We always use create_projection_path here, even if the subpath is
2381  * projection-capable, so as to avoid modifying the subpath in place.
2382  * It seems unlikely at present that there could be any other
2383  * references to the subpath, but better safe than sorry.
2384  *
2385  * Note that we don't change the GatherPath's cost estimates; it might
2386  * be appropriate to do so, to reflect the fact that the bulk of the
2387  * target evaluation will happen in workers.
2388  */
2389  gpath->subpath = (Path *)
2391  gpath->subpath->parent,
2392  gpath->subpath,
2393  target);
2394  }
2395  else if (path->parallel_safe &&
2396  !is_parallel_safe(root, (Node *) target->exprs))
2397  {
2398  /*
2399  * We're inserting a parallel-restricted target list into a path
2400  * currently marked parallel-safe, so we have to mark it as no longer
2401  * safe.
2402  */
2403  path->parallel_safe = false;
2404  }
2405 
2406  return path;
2407 }
2408 
2409 /*
2410  * create_set_projection_path
2411  * Creates a pathnode that represents performing a projection that
2412  * includes set-returning functions.
2413  *
2414  * 'rel' is the parent relation associated with the result
2415  * 'subpath' is the path representing the source of data
2416  * 'target' is the PathTarget to be computed
2417  */
2420  RelOptInfo *rel,
2421  Path *subpath,
2422  PathTarget *target)
2423 {
2424  ProjectSetPath *pathnode = makeNode(ProjectSetPath);
2425  double tlist_rows;
2426  ListCell *lc;
2427 
2428  pathnode->path.pathtype = T_ProjectSet;
2429  pathnode->path.parent = rel;
2430  pathnode->path.pathtarget = target;
2431  /* For now, assume we are above any joins, so no parameterization */
2432  pathnode->path.param_info = NULL;
2433  pathnode->path.parallel_aware = false;
2434  pathnode->path.parallel_safe = rel->consider_parallel &&
2435  subpath->parallel_safe &&
2436  is_parallel_safe(root, (Node *) target->exprs);
2437  pathnode->path.parallel_workers = subpath->parallel_workers;
2438  /* Projection does not change the sort order XXX? */
2439  pathnode->path.pathkeys = subpath->pathkeys;
2440 
2441  pathnode->subpath = subpath;
2442 
2443  /*
2444  * Estimate number of rows produced by SRFs for each row of input; if
2445  * there's more than one in this node, use the maximum.
2446  */
2447  tlist_rows = 1;
2448  foreach(lc, target->exprs)
2449  {
2450  Node *node = (Node *) lfirst(lc);
2451  double itemrows;
2452 
2453  itemrows = expression_returns_set_rows(node);
2454  if (tlist_rows < itemrows)
2455  tlist_rows = itemrows;
2456  }
2457 
2458  /*
2459  * In addition to the cost of evaluating the tlist, charge cpu_tuple_cost
2460  * per input row, and half of cpu_tuple_cost for each added output row.
2461  * This is slightly bizarre maybe, but it's what 9.6 did; we may revisit
2462  * this estimate later.
2463  */
2464  pathnode->path.rows = subpath->rows * tlist_rows;
2465  pathnode->path.startup_cost = subpath->startup_cost +
2466  target->cost.startup;
2467  pathnode->path.total_cost = subpath->total_cost +
2468  target->cost.startup +
2469  (cpu_tuple_cost + target->cost.per_tuple) * subpath->rows +
2470  (pathnode->path.rows - subpath->rows) * cpu_tuple_cost / 2;
2471 
2472  return pathnode;
2473 }
2474 
2475 /*
2476  * create_sort_path
2477  * Creates a pathnode that represents performing an explicit sort.
2478  *
2479  * 'rel' is the parent relation associated with the result
2480  * 'subpath' is the path representing the source of data
2481  * 'pathkeys' represents the desired sort order
2482  * 'limit_tuples' is the estimated bound on the number of output tuples,
2483  * or -1 if no LIMIT or couldn't estimate
2484  */
2485 SortPath *
2487  RelOptInfo *rel,
2488  Path *subpath,
2489  List *pathkeys,
2490  double limit_tuples)
2491 {
2492  SortPath *pathnode = makeNode(SortPath);
2493 
2494  pathnode->path.pathtype = T_Sort;
2495  pathnode->path.parent = rel;
2496  /* Sort doesn't project, so use source path's pathtarget */
2497  pathnode->path.pathtarget = subpath->pathtarget;
2498  /* For now, assume we are above any joins, so no parameterization */
2499  pathnode->path.param_info = NULL;
2500  pathnode->path.parallel_aware = false;
2501  pathnode->path.parallel_safe = rel->consider_parallel &&
2502  subpath->parallel_safe;
2503  pathnode->path.parallel_workers = subpath->parallel_workers;
2504  pathnode->path.pathkeys = pathkeys;
2505 
2506  pathnode->subpath = subpath;
2507 
2508  cost_sort(&pathnode->path, root, pathkeys,
2509  subpath->total_cost,
2510  subpath->rows,
2511  subpath->pathtarget->width,
2512  0.0, /* XXX comparison_cost shouldn't be 0? */
2513  work_mem, limit_tuples);
2514 
2515  return pathnode;
2516 }
2517 
2518 /*
2519  * create_group_path
2520  * Creates a pathnode that represents performing grouping of presorted input
2521  *
2522  * 'rel' is the parent relation associated with the result
2523  * 'subpath' is the path representing the source of data
2524  * 'target' is the PathTarget to be computed
2525  * 'groupClause' is a list of SortGroupClause's representing the grouping
2526  * 'qual' is the HAVING quals if any
2527  * 'numGroups' is the estimated number of groups
2528  */
2529 GroupPath *
2531  RelOptInfo *rel,
2532  Path *subpath,
2533  PathTarget *target,
2534  List *groupClause,
2535  List *qual,
2536  double numGroups)
2537 {
2538  GroupPath *pathnode = makeNode(GroupPath);
2539 
2540  pathnode->path.pathtype = T_Group;
2541  pathnode->path.parent = rel;
2542  pathnode->path.pathtarget = target;
2543  /* For now, assume we are above any joins, so no parameterization */
2544  pathnode->path.param_info = NULL;
2545  pathnode->path.parallel_aware = false;
2546  pathnode->path.parallel_safe = rel->consider_parallel &&
2547  subpath->parallel_safe;
2548  pathnode->path.parallel_workers = subpath->parallel_workers;
2549  /* Group doesn't change sort ordering */
2550  pathnode->path.pathkeys = subpath->pathkeys;
2551 
2552  pathnode->subpath = subpath;
2553 
2554  pathnode->groupClause = groupClause;
2555  pathnode->qual = qual;
2556 
2557  cost_group(&pathnode->path, root,
2558  list_length(groupClause),
2559  numGroups,
2560  subpath->startup_cost, subpath->total_cost,
2561  subpath->rows);
2562 
2563  /* add tlist eval cost for each output row */
2564  pathnode->path.startup_cost += target->cost.startup;
2565  pathnode->path.total_cost += target->cost.startup +
2566  target->cost.per_tuple * pathnode->path.rows;
2567 
2568  return pathnode;
2569 }
2570 
2571 /*
2572  * create_upper_unique_path
2573  * Creates a pathnode that represents performing an explicit Unique step
2574  * on presorted input.
2575  *
2576  * This produces a Unique plan node, but the use-case is so different from
2577  * create_unique_path that it doesn't seem worth trying to merge the two.
2578  *
2579  * 'rel' is the parent relation associated with the result
2580  * 'subpath' is the path representing the source of data
2581  * 'numCols' is the number of grouping columns
2582  * 'numGroups' is the estimated number of groups
2583  *
2584  * The input path must be sorted on the grouping columns, plus possibly
2585  * additional columns; so the first numCols pathkeys are the grouping columns
2586  */
2589  RelOptInfo *rel,
2590  Path *subpath,
2591  int numCols,
2592  double numGroups)
2593 {
2595 
2596  pathnode->path.pathtype = T_Unique;
2597  pathnode->path.parent = rel;
2598  /* Unique doesn't project, so use source path's pathtarget */
2599  pathnode->path.pathtarget = subpath->pathtarget;
2600  /* For now, assume we are above any joins, so no parameterization */
2601  pathnode->path.param_info = NULL;
2602  pathnode->path.parallel_aware = false;
2603  pathnode->path.parallel_safe = rel->consider_parallel &&
2604  subpath->parallel_safe;
2605  pathnode->path.parallel_workers = subpath->parallel_workers;
2606  /* Unique doesn't change the input ordering */
2607  pathnode->path.pathkeys = subpath->pathkeys;
2608 
2609  pathnode->subpath = subpath;
2610  pathnode->numkeys = numCols;
2611 
2612  /*
2613  * Charge one cpu_operator_cost per comparison per input tuple. We assume
2614  * all columns get compared at most of the tuples. (XXX probably this is
2615  * an overestimate.)
2616  */
2617  pathnode->path.startup_cost = subpath->startup_cost;
2618  pathnode->path.total_cost = subpath->total_cost +
2619  cpu_operator_cost * subpath->rows * numCols;
2620  pathnode->path.rows = numGroups;
2621 
2622  return pathnode;
2623 }
2624 
2625 /*
2626  * create_agg_path
2627  * Creates a pathnode that represents performing aggregation/grouping
2628  *
2629  * 'rel' is the parent relation associated with the result
2630  * 'subpath' is the path representing the source of data
2631  * 'target' is the PathTarget to be computed
2632  * 'aggstrategy' is the Agg node's basic implementation strategy
2633  * 'aggsplit' is the Agg node's aggregate-splitting mode
2634  * 'groupClause' is a list of SortGroupClause's representing the grouping
2635  * 'qual' is the HAVING quals if any
2636  * 'aggcosts' contains cost info about the aggregate functions to be computed
2637  * 'numGroups' is the estimated number of groups (1 if not grouping)
2638  */
2639 AggPath *
2641  RelOptInfo *rel,
2642  Path *subpath,
2643  PathTarget *target,
2644  AggStrategy aggstrategy,
2645  AggSplit aggsplit,
2646  List *groupClause,
2647  List *qual,
2648  const AggClauseCosts *aggcosts,
2649  double numGroups)
2650 {
2651  AggPath *pathnode = makeNode(AggPath);
2652 
2653  pathnode->path.pathtype = T_Agg;
2654  pathnode->path.parent = rel;
2655  pathnode->path.pathtarget = target;
2656  /* For now, assume we are above any joins, so no parameterization */
2657  pathnode->path.param_info = NULL;
2658  pathnode->path.parallel_aware = false;
2659  pathnode->path.parallel_safe = rel->consider_parallel &&
2660  subpath->parallel_safe;
2661  pathnode->path.parallel_workers = subpath->parallel_workers;
2662  if (aggstrategy == AGG_SORTED)
2663  pathnode->path.pathkeys = subpath->pathkeys; /* preserves order */
2664  else
2665  pathnode->path.pathkeys = NIL; /* output is unordered */
2666  pathnode->subpath = subpath;
2667 
2668  pathnode->aggstrategy = aggstrategy;
2669  pathnode->aggsplit = aggsplit;
2670  pathnode->numGroups = numGroups;
2671  pathnode->groupClause = groupClause;
2672  pathnode->qual = qual;
2673 
2674  cost_agg(&pathnode->path, root,
2675  aggstrategy, aggcosts,
2676  list_length(groupClause), numGroups,
2677  subpath->startup_cost, subpath->total_cost,
2678  subpath->rows);
2679 
2680  /* add tlist eval cost for each output row */
2681  pathnode->path.startup_cost += target->cost.startup;
2682  pathnode->path.total_cost += target->cost.startup +
2683  target->cost.per_tuple * pathnode->path.rows;
2684 
2685  return pathnode;
2686 }
2687 
2688 /*
2689  * create_groupingsets_path
2690  * Creates a pathnode that represents performing GROUPING SETS aggregation
2691  *
2692  * GroupingSetsPath represents sorted grouping with one or more grouping sets.
2693  * The input path's result must be sorted to match the last entry in
2694  * rollup_groupclauses.
2695  *
2696  * 'rel' is the parent relation associated with the result
2697  * 'subpath' is the path representing the source of data
2698  * 'target' is the PathTarget to be computed
2699  * 'having_qual' is the HAVING quals if any
2700  * 'rollup_lists' is a list of grouping sets
2701  * 'rollup_groupclauses' is a list of grouping clauses for grouping sets
2702  * 'agg_costs' contains cost info about the aggregate functions to be computed
2703  * 'numGroups' is the estimated number of groups
2704  */
2707  RelOptInfo *rel,
2708  Path *subpath,
2709  PathTarget *target,
2710  List *having_qual,
2711  List *rollup_lists,
2712  List *rollup_groupclauses,
2713  const AggClauseCosts *agg_costs,
2714  double numGroups)
2715 {
2717  int numGroupCols;
2718 
2719  /* The topmost generated Plan node will be an Agg */
2720  pathnode->path.pathtype = T_Agg;
2721  pathnode->path.parent = rel;
2722  pathnode->path.pathtarget = target;
2723  pathnode->path.param_info = subpath->param_info;
2724  pathnode->path.parallel_aware = false;
2725  pathnode->path.parallel_safe = rel->consider_parallel &&
2726  subpath->parallel_safe;
2727  pathnode->path.parallel_workers = subpath->parallel_workers;
2728  pathnode->subpath = subpath;
2729 
2730  /*
2731  * Output will be in sorted order by group_pathkeys if, and only if, there
2732  * is a single rollup operation on a non-empty list of grouping
2733  * expressions.
2734  */
2735  if (list_length(rollup_groupclauses) == 1 &&
2736  ((List *) linitial(rollup_groupclauses)) != NIL)
2737  pathnode->path.pathkeys = root->group_pathkeys;
2738  else
2739  pathnode->path.pathkeys = NIL;
2740 
2741  pathnode->rollup_groupclauses = rollup_groupclauses;
2742  pathnode->rollup_lists = rollup_lists;
2743  pathnode->qual = having_qual;
2744 
2745  Assert(rollup_lists != NIL);
2746  Assert(list_length(rollup_lists) == list_length(rollup_groupclauses));
2747 
2748  /* Account for cost of the topmost Agg node */
2749  numGroupCols = list_length((List *) linitial((List *) llast(rollup_lists)));
2750 
2751  cost_agg(&pathnode->path, root,
2752  (numGroupCols > 0) ? AGG_SORTED : AGG_PLAIN,
2753  agg_costs,
2754  numGroupCols,
2755  numGroups,
2756  subpath->startup_cost,
2757  subpath->total_cost,
2758  subpath->rows);
2759 
2760  /*
2761  * Add in the costs and output rows of the additional sorting/aggregation
2762  * steps, if any. Only total costs count, since the extra sorts aren't
2763  * run on startup.
2764  */
2765  if (list_length(rollup_lists) > 1)
2766  {
2767  ListCell *lc;
2768 
2769  foreach(lc, rollup_lists)
2770  {
2771  List *gsets = (List *) lfirst(lc);
2772  Path sort_path; /* dummy for result of cost_sort */
2773  Path agg_path; /* dummy for result of cost_agg */
2774 
2775  /* We must iterate over all but the last rollup_lists element */
2776  if (lnext(lc) == NULL)
2777  break;
2778 
2779  /* Account for cost of sort, but don't charge input cost again */
2780  cost_sort(&sort_path, root, NIL,
2781  0.0,
2782  subpath->rows,
2783  subpath->pathtarget->width,
2784  0.0,
2785  work_mem,
2786  -1.0);
2787 
2788  /* Account for cost of aggregation */
2789  numGroupCols = list_length((List *) linitial(gsets));
2790 
2791  cost_agg(&agg_path, root,
2792  AGG_SORTED,
2793  agg_costs,
2794  numGroupCols,
2795  numGroups, /* XXX surely not right for all steps? */
2796  sort_path.startup_cost,
2797  sort_path.total_cost,
2798  sort_path.rows);
2799 
2800  pathnode->path.total_cost += agg_path.total_cost;
2801  pathnode->path.rows += agg_path.rows;
2802  }
2803  }
2804 
2805  /* add tlist eval cost for each output row */
2806  pathnode->path.startup_cost += target->cost.startup;
2807  pathnode->path.total_cost += target->cost.startup +
2808  target->cost.per_tuple * pathnode->path.rows;
2809 
2810  return pathnode;
2811 }
2812 
2813 /*
2814  * create_minmaxagg_path
2815  * Creates a pathnode that represents computation of MIN/MAX aggregates
2816  *
2817  * 'rel' is the parent relation associated with the result
2818  * 'target' is the PathTarget to be computed
2819  * 'mmaggregates' is a list of MinMaxAggInfo structs
2820  * 'quals' is the HAVING quals if any
2821  */
2822 MinMaxAggPath *
2824  RelOptInfo *rel,
2825  PathTarget *target,
2826  List *mmaggregates,
2827  List *quals)
2828 {
2829  MinMaxAggPath *pathnode = makeNode(MinMaxAggPath);
2830  Cost initplan_cost;
2831  ListCell *lc;
2832 
2833  /* The topmost generated Plan node will be a Result */
2834  pathnode->path.pathtype = T_Result;
2835  pathnode->path.parent = rel;
2836  pathnode->path.pathtarget = target;
2837  /* For now, assume we are above any joins, so no parameterization */
2838  pathnode->path.param_info = NULL;
2839  pathnode->path.parallel_aware = false;
2840  /* A MinMaxAggPath implies use of subplans, so cannot be parallel-safe */
2841  pathnode->path.parallel_safe = false;
2842  pathnode->path.parallel_workers = 0;
2843  /* Result is one unordered row */
2844  pathnode->path.rows = 1;
2845  pathnode->path.pathkeys = NIL;
2846 
2847  pathnode->mmaggregates = mmaggregates;
2848  pathnode->quals = quals;
2849 
2850  /* Calculate cost of all the initplans ... */
2851  initplan_cost = 0;
2852  foreach(lc, mmaggregates)
2853  {
2854  MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
2855 
2856  initplan_cost += mminfo->pathcost;
2857  }
2858 
2859  /* add tlist eval cost for each output row, plus cpu_tuple_cost */
2860  pathnode->path.startup_cost = initplan_cost + target->cost.startup;
2861  pathnode->path.total_cost = initplan_cost + target->cost.startup +
2862  target->cost.per_tuple + cpu_tuple_cost;
2863 
2864  return pathnode;
2865 }
2866 
2867 /*
2868  * create_windowagg_path
2869  * Creates a pathnode that represents computation of window functions
2870  *
2871  * 'rel' is the parent relation associated with the result
2872  * 'subpath' is the path representing the source of data
2873  * 'target' is the PathTarget to be computed
2874  * 'windowFuncs' is a list of WindowFunc structs
2875  * 'winclause' is a WindowClause that is common to all the WindowFuncs
2876  * 'winpathkeys' is the pathkeys for the PARTITION keys + ORDER keys
2877  *
2878  * The actual sort order of the input must match winpathkeys, but might
2879  * have additional keys after those.
2880  */
2881 WindowAggPath *
2883  RelOptInfo *rel,
2884  Path *subpath,
2885  PathTarget *target,
2886  List *windowFuncs,
2887  WindowClause *winclause,
2888  List *winpathkeys)
2889 {
2890  WindowAggPath *pathnode = makeNode(WindowAggPath);
2891 
2892  pathnode->path.pathtype = T_WindowAgg;
2893  pathnode->path.parent = rel;
2894  pathnode->path.pathtarget = target;
2895  /* For now, assume we are above any joins, so no parameterization */
2896  pathnode->path.param_info = NULL;
2897  pathnode->path.parallel_aware = false;
2898  pathnode->path.parallel_safe = rel->consider_parallel &&
2899  subpath->parallel_safe;
2900  pathnode->path.parallel_workers = subpath->parallel_workers;
2901  /* WindowAgg preserves the input sort order */
2902  pathnode->path.pathkeys = subpath->pathkeys;
2903 
2904  pathnode->subpath = subpath;
2905  pathnode->winclause = winclause;
2906  pathnode->winpathkeys = winpathkeys;
2907 
2908  /*
2909  * For costing purposes, assume that there are no redundant partitioning
2910  * or ordering columns; it's not worth the trouble to deal with that
2911  * corner case here. So we just pass the unmodified list lengths to
2912  * cost_windowagg.
2913  */
2914  cost_windowagg(&pathnode->path, root,
2915  windowFuncs,
2916  list_length(winclause->partitionClause),
2917  list_length(winclause->orderClause),
2918  subpath->startup_cost,
2919  subpath->total_cost,
2920  subpath->rows);
2921 
2922  /* add tlist eval cost for each output row */
2923  pathnode->path.startup_cost += target->cost.startup;
2924  pathnode->path.total_cost += target->cost.startup +
2925  target->cost.per_tuple * pathnode->path.rows;
2926 
2927  return pathnode;
2928 }
2929 
2930 /*
2931  * create_setop_path
2932  * Creates a pathnode that represents computation of INTERSECT or EXCEPT
2933  *
2934  * 'rel' is the parent relation associated with the result
2935  * 'subpath' is the path representing the source of data
2936  * 'cmd' is the specific semantics (INTERSECT or EXCEPT, with/without ALL)
2937  * 'strategy' is the implementation strategy (sorted or hashed)
2938  * 'distinctList' is a list of SortGroupClause's representing the grouping
2939  * 'flagColIdx' is the column number where the flag column will be, if any
2940  * 'firstFlag' is the flag value for the first input relation when hashing;
2941  * or -1 when sorting
2942  * 'numGroups' is the estimated number of distinct groups
2943  * 'outputRows' is the estimated number of output rows
2944  */
2945 SetOpPath *
2947  RelOptInfo *rel,
2948  Path *subpath,
2949  SetOpCmd cmd,
2950  SetOpStrategy strategy,
2951  List *distinctList,
2952  AttrNumber flagColIdx,
2953  int firstFlag,
2954  double numGroups,
2955  double outputRows)
2956 {
2957  SetOpPath *pathnode = makeNode(SetOpPath);
2958 
2959  pathnode->path.pathtype = T_SetOp;
2960  pathnode->path.parent = rel;
2961  /* SetOp doesn't project, so use source path's pathtarget */
2962  pathnode->path.pathtarget = subpath->pathtarget;
2963  /* For now, assume we are above any joins, so no parameterization */
2964  pathnode->path.param_info = NULL;
2965  pathnode->path.parallel_aware = false;
2966  pathnode->path.parallel_safe = rel->consider_parallel &&
2967  subpath->parallel_safe;
2968  pathnode->path.parallel_workers = subpath->parallel_workers;
2969  /* SetOp preserves the input sort order if in sort mode */
2970  pathnode->path.pathkeys =
2971  (strategy == SETOP_SORTED) ? subpath->pathkeys : NIL;
2972 
2973  pathnode->subpath = subpath;
2974  pathnode->cmd = cmd;
2975  pathnode->strategy = strategy;
2976  pathnode->distinctList = distinctList;
2977  pathnode->flagColIdx = flagColIdx;
2978  pathnode->firstFlag = firstFlag;
2979  pathnode->numGroups = numGroups;
2980 
2981  /*
2982  * Charge one cpu_operator_cost per comparison per input tuple. We assume
2983  * all columns get compared at most of the tuples.
2984  */
2985  pathnode->path.startup_cost = subpath->startup_cost;
2986  pathnode->path.total_cost = subpath->total_cost +
2987  cpu_operator_cost * subpath->rows * list_length(distinctList);
2988  pathnode->path.rows = outputRows;
2989 
2990  return pathnode;
2991 }
2992 
2993 /*
2994  * create_recursiveunion_path
2995  * Creates a pathnode that represents a recursive UNION node
2996  *
2997  * 'rel' is the parent relation associated with the result
2998  * 'leftpath' is the source of data for the non-recursive term
2999  * 'rightpath' is the source of data for the recursive term
3000  * 'target' is the PathTarget to be computed
3001  * 'distinctList' is a list of SortGroupClause's representing the grouping
3002  * 'wtParam' is the ID of Param representing work table
3003  * 'numGroups' is the estimated number of groups
3004  *
3005  * For recursive UNION ALL, distinctList is empty and numGroups is zero
3006  */
3009  RelOptInfo *rel,
3010  Path *leftpath,
3011  Path *rightpath,
3012  PathTarget *target,
3013  List *distinctList,
3014  int wtParam,
3015  double numGroups)
3016 {
3018 
3019  pathnode->path.pathtype = T_RecursiveUnion;
3020  pathnode->path.parent = rel;
3021  pathnode->path.pathtarget = target;
3022  /* For now, assume we are above any joins, so no parameterization */
3023  pathnode->path.param_info = NULL;
3024  pathnode->path.parallel_aware = false;
3025  pathnode->path.parallel_safe = rel->consider_parallel &&
3026  leftpath->parallel_safe && rightpath->parallel_safe;
3027  /* Foolish, but we'll do it like joins for now: */
3028  pathnode->path.parallel_workers = leftpath->parallel_workers;
3029  /* RecursiveUnion result is always unsorted */
3030  pathnode->path.pathkeys = NIL;
3031 
3032  pathnode->leftpath = leftpath;
3033  pathnode->rightpath = rightpath;
3034  pathnode->distinctList = distinctList;
3035  pathnode->wtParam = wtParam;
3036  pathnode->numGroups = numGroups;
3037 
3038  cost_recursive_union(&pathnode->path, leftpath, rightpath);
3039 
3040  return pathnode;
3041 }
3042 
3043 /*
3044  * create_lockrows_path
3045  * Creates a pathnode that represents acquiring row locks
3046  *
3047  * 'rel' is the parent relation associated with the result
3048  * 'subpath' is the path representing the source of data
3049  * 'rowMarks' is a list of PlanRowMark's
3050  * 'epqParam' is the ID of Param for EvalPlanQual re-eval
3051  */
3052 LockRowsPath *
3054  Path *subpath, List *rowMarks, int epqParam)
3055 {
3056  LockRowsPath *pathnode = makeNode(LockRowsPath);
3057 
3058  pathnode->path.pathtype = T_LockRows;
3059  pathnode->path.parent = rel;
3060  /* LockRows doesn't project, so use source path's pathtarget */
3061  pathnode->path.pathtarget = subpath->pathtarget;
3062  /* For now, assume we are above any joins, so no parameterization */
3063  pathnode->path.param_info = NULL;
3064  pathnode->path.parallel_aware = false;
3065  pathnode->path.parallel_safe = false;
3066  pathnode->path.parallel_workers = 0;
3067  pathnode->path.rows = subpath->rows;
3068 
3069  /*
3070  * The result cannot be assumed sorted, since locking might cause the sort
3071  * key columns to be replaced with new values.
3072  */
3073  pathnode->path.pathkeys = NIL;
3074 
3075  pathnode->subpath = subpath;
3076  pathnode->rowMarks = rowMarks;
3077  pathnode->epqParam = epqParam;
3078 
3079  /*
3080  * We should charge something extra for the costs of row locking and
3081  * possible refetches, but it's hard to say how much. For now, use
3082  * cpu_tuple_cost per row.
3083  */
3084  pathnode->path.startup_cost = subpath->startup_cost;
3085  pathnode->path.total_cost = subpath->total_cost +
3086  cpu_tuple_cost * subpath->rows;
3087 
3088  return pathnode;
3089 }
3090 
3091 /*
3092  * create_modifytable_path
3093  * Creates a pathnode that represents performing INSERT/UPDATE/DELETE mods
3094  *
3095  * 'rel' is the parent relation associated with the result
3096  * 'operation' is the operation type
3097  * 'canSetTag' is true if we set the command tag/es_processed
3098  * 'nominalRelation' is the parent RT index for use of EXPLAIN
3099  * 'resultRelations' is an integer list of actual RT indexes of target rel(s)
3100  * 'subpaths' is a list of Path(s) producing source data (one per rel)
3101  * 'subroots' is a list of PlannerInfo structs (one per rel)
3102  * 'withCheckOptionLists' is a list of WCO lists (one per rel)
3103  * 'returningLists' is a list of RETURNING tlists (one per rel)
3104  * 'rowMarks' is a list of PlanRowMarks (non-locking only)
3105  * 'onconflict' is the ON CONFLICT clause, or NULL
3106  * 'epqParam' is the ID of Param for EvalPlanQual re-eval
3107  */
3110  CmdType operation, bool canSetTag,
3111  Index nominalRelation, List *partitioned_rels,
3112  List *resultRelations, List *subpaths,
3113  List *subroots,
3114  List *withCheckOptionLists, List *returningLists,
3115  List *rowMarks, OnConflictExpr *onconflict,
3116  int epqParam)
3117 {
3119  double total_size;
3120  ListCell *lc;
3121 
3122  Assert(list_length(resultRelations) == list_length(subpaths));
3123  Assert(list_length(resultRelations) == list_length(subroots));
3124  Assert(withCheckOptionLists == NIL ||
3125  list_length(resultRelations) == list_length(withCheckOptionLists));
3126  Assert(returningLists == NIL ||
3127  list_length(resultRelations) == list_length(returningLists));
3128 
3129  pathnode->path.pathtype = T_ModifyTable;
3130  pathnode->path.parent = rel;
3131  /* pathtarget is not interesting, just make it minimally valid */
3132  pathnode->path.pathtarget = rel->reltarget;
3133  /* For now, assume we are above any joins, so no parameterization */
3134  pathnode->path.param_info = NULL;
3135  pathnode->path.parallel_aware = false;
3136  pathnode->path.parallel_safe = false;
3137  pathnode->path.parallel_workers = 0;
3138  pathnode->path.pathkeys = NIL;
3139 
3140  /*
3141  * Compute cost & rowcount as sum of subpath costs & rowcounts.
3142  *
3143  * Currently, we don't charge anything extra for the actual table
3144  * modification work, nor for the WITH CHECK OPTIONS or RETURNING
3145  * expressions if any. It would only be window dressing, since
3146  * ModifyTable is always a top-level node and there is no way for the
3147  * costs to change any higher-level planning choices. But we might want
3148  * to make it look better sometime.
3149  */
3150  pathnode->path.startup_cost = 0;
3151  pathnode->path.total_cost = 0;
3152  pathnode->path.rows = 0;
3153  total_size = 0;
3154  foreach(lc, subpaths)
3155  {
3156  Path *subpath = (Path *) lfirst(lc);
3157 
3158  if (lc == list_head(subpaths)) /* first node? */
3159  pathnode->path.startup_cost = subpath->startup_cost;
3160  pathnode->path.total_cost += subpath->total_cost;
3161  pathnode->path.rows += subpath->rows;
3162  total_size += subpath->pathtarget->width * subpath->rows;
3163  }
3164 
3165  /*
3166  * Set width to the average width of the subpath outputs. XXX this is
3167  * totally wrong: we should report zero if no RETURNING, else an average
3168  * of the RETURNING tlist widths. But it's what happened historically,
3169  * and improving it is a task for another day.
3170  */
3171  if (pathnode->path.rows > 0)
3172  total_size /= pathnode->path.rows;
3173  pathnode->path.pathtarget->width = rint(total_size);
3174 
3175  pathnode->operation = operation;
3176  pathnode->canSetTag = canSetTag;
3177  pathnode->nominalRelation = nominalRelation;
3178  pathnode->partitioned_rels = partitioned_rels;
3179  pathnode->resultRelations = resultRelations;
3180  pathnode->subpaths = subpaths;
3181  pathnode->subroots = subroots;
3182  pathnode->withCheckOptionLists = withCheckOptionLists;
3183  pathnode->returningLists = returningLists;
3184  pathnode->rowMarks = rowMarks;
3185  pathnode->onconflict = onconflict;
3186  pathnode->epqParam = epqParam;
3187 
3188  return pathnode;
3189 }
3190 
3191 /*
3192  * create_limit_path
3193  * Creates a pathnode that represents performing LIMIT/OFFSET
3194  *
3195  * In addition to providing the actual OFFSET and LIMIT expressions,
3196  * the caller must provide estimates of their values for costing purposes.
3197  * The estimates are as computed by preprocess_limit(), ie, 0 represents
3198  * the clause not being present, and -1 means it's present but we could
3199  * not estimate its value.
3200  *
3201  * 'rel' is the parent relation associated with the result
3202  * 'subpath' is the path representing the source of data
3203  * 'limitOffset' is the actual OFFSET expression, or NULL
3204  * 'limitCount' is the actual LIMIT expression, or NULL
3205  * 'offset_est' is the estimated value of the OFFSET expression
3206  * 'count_est' is the estimated value of the LIMIT expression
3207  */
3208 LimitPath *
3210  Path *subpath,
3211  Node *limitOffset, Node *limitCount,
3212  int64 offset_est, int64 count_est)
3213 {
3214  LimitPath *pathnode = makeNode(LimitPath);
3215 
3216  pathnode->path.pathtype = T_Limit;
3217  pathnode->path.parent = rel;
3218  /* Limit doesn't project, so use source path's pathtarget */
3219  pathnode->path.pathtarget = subpath->pathtarget;
3220  /* For now, assume we are above any joins, so no parameterization */
3221  pathnode->path.param_info = NULL;
3222  pathnode->path.parallel_aware = false;
3223  pathnode->path.parallel_safe = rel->consider_parallel &&
3224  subpath->parallel_safe;
3225  pathnode->path.parallel_workers = subpath->parallel_workers;
3226  pathnode->path.rows = subpath->rows;
3227  pathnode->path.startup_cost = subpath->startup_cost;
3228  pathnode->path.total_cost = subpath->total_cost;
3229  pathnode->path.pathkeys = subpath->pathkeys;
3230  pathnode->subpath = subpath;
3231  pathnode->limitOffset = limitOffset;
3232  pathnode->limitCount = limitCount;
3233 
3234  /*
3235  * Adjust the output rows count and costs according to the offset/limit.
3236  * This is only a cosmetic issue if we are at top level, but if we are
3237  * building a subquery then it's important to report correct info to the
3238  * outer planner.
3239  *
3240  * When the offset or count couldn't be estimated, use 10% of the
3241  * estimated number of rows emitted from the subpath.
3242  *
3243  * XXX we don't bother to add eval costs of the offset/limit expressions
3244  * themselves to the path costs. In theory we should, but in most cases
3245  * those expressions are trivial and it's just not worth the trouble.
3246  */
3247  if (offset_est != 0)
3248  {
3249  double offset_rows;
3250 
3251  if (offset_est > 0)
3252  offset_rows = (double) offset_est;
3253  else
3254  offset_rows = clamp_row_est(subpath->rows * 0.10);
3255  if (offset_rows > pathnode->path.rows)
3256  offset_rows = pathnode->path.rows;
3257  if (subpath->rows > 0)
3258  pathnode->path.startup_cost +=
3259  (subpath->total_cost - subpath->startup_cost)
3260  * offset_rows / subpath->rows;
3261  pathnode->path.rows -= offset_rows;
3262  if (pathnode->path.rows < 1)
3263  pathnode->path.rows = 1;
3264  }
3265 
3266  if (count_est != 0)
3267  {
3268  double count_rows;
3269 
3270  if (count_est > 0)
3271  count_rows = (double) count_est;
3272  else
3273  count_rows = clamp_row_est(subpath->rows * 0.10);
3274  if (count_rows > pathnode->path.rows)
3275  count_rows = pathnode->path.rows;
3276  if (subpath->rows > 0)
3277  pathnode->path.total_cost = pathnode->path.startup_cost +
3278  (subpath->total_cost - subpath->startup_cost)
3279  * count_rows / subpath->rows;
3280  pathnode->path.rows = count_rows;
3281  if (pathnode->path.rows < 1)
3282  pathnode->path.rows = 1;
3283  }
3284 
3285  return pathnode;
3286 }
3287 
3288 
3289 /*
3290  * reparameterize_path
3291  * Attempt to modify a Path to have greater parameterization
3292  *
3293  * We use this to attempt to bring all child paths of an appendrel to the
3294  * same parameterization level, ensuring that they all enforce the same set
3295  * of join quals (and thus that that parameterization can be attributed to
3296  * an append path built from such paths). Currently, only a few path types
3297  * are supported here, though more could be added at need. We return NULL
3298  * if we can't reparameterize the given path.
3299  *
3300  * Note: we intentionally do not pass created paths to add_path(); it would
3301  * possibly try to delete them on the grounds of being cost-inferior to the
3302  * paths they were made from, and we don't want that. Paths made here are
3303  * not necessarily of general-purpose usefulness, but they can be useful
3304  * as members of an append path.
3305  */
3306 Path *
3308  Relids required_outer,
3309  double loop_count)
3310 {
3311  RelOptInfo *rel = path->parent;
3312 
3313  /* Can only increase, not decrease, path's parameterization */
3314  if (!bms_is_subset(PATH_REQ_OUTER(path), required_outer))
3315  return NULL;
3316  switch (path->pathtype)
3317  {
3318  case T_SeqScan:
3319  return create_seqscan_path(root, rel, required_outer, 0);
3320  case T_SampleScan:
3321  return (Path *) create_samplescan_path(root, rel, required_outer);
3322  case T_IndexScan:
3323  case T_IndexOnlyScan:
3324  {
3325  IndexPath *ipath = (IndexPath *) path;
3326  IndexPath *newpath = makeNode(IndexPath);
3327 
3328  /*
3329  * We can't use create_index_path directly, and would not want
3330  * to because it would re-compute the indexqual conditions
3331  * which is wasted effort. Instead we hack things a bit:
3332  * flat-copy the path node, revise its param_info, and redo
3333  * the cost estimate.
3334  */
3335  memcpy(newpath, ipath, sizeof(IndexPath));
3336  newpath->path.param_info =
3337  get_baserel_parampathinfo(root, rel, required_outer);
3338  cost_index(newpath, root, loop_count, false);
3339  return (Path *) newpath;
3340  }
3341  case T_BitmapHeapScan:
3342  {
3343  BitmapHeapPath *bpath = (BitmapHeapPath *) path;
3344 
3345  return (Path *) create_bitmap_heap_path(root,
3346  rel,
3347  bpath->bitmapqual,
3348  required_outer,
3349  loop_count, 0);
3350  }
3351  case T_SubqueryScan:
3352  {
3353  SubqueryScanPath *spath = (SubqueryScanPath *) path;
3354 
3355  return (Path *) create_subqueryscan_path(root,
3356  rel,
3357  spath->subpath,
3358  spath->path.pathkeys,
3359  required_outer);
3360  }
3361  default:
3362  break;
3363  }
3364  return NULL;
3365 }
Path * apply_projection_to_path(PlannerInfo *root, RelOptInfo *rel, Path *path, PathTarget *target)
Definition: pathnode.c:2343
void cost_group(Path *path, PlannerInfo *root, int numGroupCols, double numGroups, Cost input_startup_cost, Cost input_total_cost, double input_tuples)
Definition: costsize.c:1998
struct Path * cheapest_unique_path
Definition: relation.h:513
List * indexorderbycols
Definition: relation.h:981
List * group_pathkeys
Definition: relation.h:263
#define NIL
Definition: pg_list.h:69
List * qual
Definition: relation.h:1367
bool semi_can_btree
Definition: relation.h:1838
double estimate_num_groups(PlannerInfo *root, List *groupExprs, double input_rows, List **pgset)
Definition: selfuncs.c:3273
List * path_mergeclauses
Definition: relation.h:1286
List * outersortkeys
Definition: relation.h:1287
List * distinctList
Definition: relation.h:1450
MinMaxAggPath * create_minmaxagg_path(PlannerInfo *root, RelOptInfo *rel, PathTarget *target, List *mmaggregates, List *quals)
Definition: pathnode.c:2823
Definition: nodes.h:76
GatherPath * create_gather_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target, Relids required_outer, double *rows)
Definition: pathnode.c:1731
#define IsA(nodeptr, _type_)
Definition: nodes.h:571
JoinPath jpath
Definition: relation.h:1303
PathTarget * pathtarget
Definition: relation.h:899
List * returningLists
Definition: relation.h:1499
bool query_is_distinct_for(Query *query, List *colnos, List *opids)
Definition: analyzejoins.c:687
OnConflictExpr * onconflict
Definition: relation.h:1501
void cost_bitmap_heap_scan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info, Path *bitmapqual, double loop_count)
Definition: costsize.c:928
Node * limitOffset
Definition: relation.h:1512
Path * subpath
Definition: relation.h:1379
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:412
Path path
Definition: relation.h:975
SubqueryScanPath * create_subqueryscan_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, List *pathkeys, Relids required_outer)
Definition: pathnode.c:1769
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:110
Path * subpath
Definition: relation.h:1351
IndexOptInfo * indexinfo
Definition: relation.h:976
ParamPathInfo * get_baserel_parampathinfo(PlannerInfo *root, RelOptInfo *baserel, Relids required_outer)
Definition: relnode.c:1035
Index nominalRelation
Definition: relation.h:1492
Path * fdw_outerpath
Definition: relation.h:1076
void cost_tidscan(Path *path, PlannerInfo *root, RelOptInfo *baserel, List *tidquals, ParamPathInfo *param_info)
Definition: costsize.c:1164
void cost_windowagg(Path *path, PlannerInfo *root, List *windowFuncs, int numPartCols, int numOrderCols, Cost input_startup_cost, Cost input_total_cost, double input_tuples)
Definition: costsize.c:1928
Definition: nodes.h:78
SetOpStrategy strategy
Definition: relation.h:1449
AggStrategy aggstrategy
Definition: relation.h:1394
LockRowsPath * create_lockrows_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, List *rowMarks, int epqParam)
Definition: pathnode.c:3053
void cost_gather_merge(GatherMergePath *path, PlannerInfo *root, RelOptInfo *rel, ParamPathInfo *param_info, Cost input_startup_cost, Cost input_total_cost, double *rows)
Definition: costsize.c:387
SetOpPath * create_setop_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, SetOpCmd cmd, SetOpStrategy strategy, List *distinctList, AttrNumber flagColIdx, int firstFlag, double numGroups, double outputRows)
Definition: pathnode.c:2946
ParamPathInfo * get_joinrel_parampathinfo(PlannerInfo *root, RelOptInfo *joinrel, Path *outer_path, Path *inner_path, SpecialJoinInfo *sjinfo, Relids required_outer, List **restrict_clauses)
Definition: relnode.c:1126
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2934
List * qual
Definition: relation.h:1398
double expression_returns_set_rows(Node *clause)
Definition: clauses.c:801
UpperUniquePath * create_upper_unique_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, int numCols, double numGroups)
Definition: pathnode.c:2588
bool add_partial_path_precheck(RelOptInfo *parent_rel, Cost total_cost, List *pathkeys)
Definition: pathnode.c:876
bool add_path_precheck(RelOptInfo *parent_rel, Cost startup_cost, Cost total_cost, List *pathkeys, Relids required_outer)
Definition: pathnode.c:647
Path * innerjoinpath
Definition: relation.h:1238
struct Path * cheapest_startup_path
Definition: relation.h:511
void final_cost_nestloop(PlannerInfo *root, NestPath *path, JoinCostWorkspace *workspace, SpecialJoinInfo *sjinfo, SemiAntiJoinFactors *semifactors)
Definition: costsize.c:2121
double tuples
Definition: relation.h:533
Path * subpath
Definition: relation.h:1447
List * rowMarks
Definition: relation.h:1500
BitmapOrPath * create_bitmap_or_path(PlannerInfo *root, RelOptInfo *rel, List *bitmapquals)
Definition: pathnode.c:1136
int parallel_workers
Definition: relation.h:905
bool consider_param_startup
Definition: relation.h:501
void cost_bitmap_and_node(BitmapAndPath *path, PlannerInfo *root)
Definition: costsize.c:1072
MaterialPath * create_material_path(RelOptInfo *rel, Path *subpath)
Definition: pathnode.c:1389
#define llast(l)
Definition: pg_list.h:126
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
ParamPathInfo * param_info
Definition: relation.h:901
Relids calc_non_nestloop_required_outer(Path *outer_path, Path *inner_path)
Definition: pathnode.c:2002
Definition: nodes.h:520
ProjectionPath * create_projection_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target)
Definition: pathnode.c:2252
Definition: nodes.h:48
List * partial_pathlist
Definition: relation.h:510
AttrNumber varattno
Definition: primnodes.h:168
void cost_valuesscan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
Definition: costsize.c:1433
IndexPath * create_index_path(PlannerInfo *root, IndexOptInfo *index, List *indexclauses, List *indexclausecols, List *indexorderbys, List *indexorderbycols, List *pathkeys, ScanDirection indexscandir, bool indexonly, Relids required_outer, double loop_count, bool partial_path)
Definition: pathnode.c:1008
void cost_ctescan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
Definition: costsize.c:1483
return result
Definition: formatting.c:1618
List * cheapest_parameterized_paths
Definition: relation.h:514
bool single_copy
Definition: relation.h:1211
UniquePathMethod umethod
Definition: relation.h:1197
PathKeysComparison compare_pathkeys(List *keys1, List *keys2)
Definition: pathkeys.c:278
Definition: nodes.h:74
UniquePath * create_unique_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, SpecialJoinInfo *sjinfo)
Definition: pathnode.c:1428
Path * subpath
Definition: relation.h:1171
List * indexclauses
Definition: relation.h:977
AggSplit aggsplit
Definition: relation.h:1395
List * partitioned_rels
Definition: relation.h:1144
List * quals
Definition: relation.h:1423
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
LimitPath * create_limit_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, Node *limitOffset, Node *limitCount, int64 offset_est, int64 count_est)
Definition: pathnode.c:3209
Path * create_functionscan_path(PlannerInfo *root, RelOptInfo *rel, List *pathkeys, Relids required_outer)
Definition: pathnode.c:1797
double numGroups
Definition: relation.h:1396
double numGroups
Definition: relation.h:1453
SetOpStrategy
Definition: nodes.h:790
List * rowMarks
Definition: relation.h:1476
List * winpathkeys
Definition: relation.h:1438
Cost startup
Definition: relation.h:45
List * bitmapquals
Definition: relation.h:1019
Path path
Definition: relation.h:1122
JoinType
Definition: nodes.h:677
WindowClause * winclause
Definition: relation.h:1437
Path * create_valuesscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:1849
NestPath * create_nestloop_path(PlannerInfo *root, RelOptInfo *joinrel, JoinType jointype, JoinCostWorkspace *workspace, SpecialJoinInfo *sjinfo, SemiAntiJoinFactors *semifactors, Path *outer_path, Path *inner_path, List *restrict_clauses, List *pathkeys, Relids required_outer)
Definition: pathnode.c:2036
List * bitmapquals
Definition: relation.h:1032
Definition: type.h:90
NodeTag pathtype
Definition: relation.h:896
Relids syn_righthand
Definition: relation.h:1833
MergePath * create_mergejoin_path(PlannerInfo *root, RelOptInfo *joinrel, JoinType jointype, JoinCostWorkspace *workspace, SpecialJoinInfo *sjinfo, Path *outer_path, Path *inner_path, List *restrict_clauses, List *pathkeys, Relids required_outer, List *mergeclauses, List *outersortkeys, List *innersortkeys)
Definition: pathnode.c:2124
List * subpaths
Definition: relation.h:1125
void final_cost_mergejoin(PlannerInfo *root, MergePath *path, JoinCostWorkspace *workspace, SpecialJoinInfo *sjinfo)
Definition: costsize.c:2540
SetOpCmd cmd
Definition: relation.h:1448
ListCell * lappend_cell(List *list, ListCell *prev, void *datum)
Definition: list.c:209
bool consider_startup
Definition: relation.h:500
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition: clauses.c:1071
void cost_seqscan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
Definition: costsize.c:197
HashPath * create_hashjoin_path(PlannerInfo *root, RelOptInfo *joinrel, JoinType jointype, JoinCostWorkspace *workspace, SpecialJoinInfo *sjinfo, SemiAntiJoinFactors *semifactors, Path *outer_path, Path *inner_path, List *restrict_clauses, Relids required_outer, List *hashclauses)
Definition: pathnode.c:2188
Cost per_tuple
Definition: relation.h:46
List * indexquals
Definition: relation.h:978
Path * subpath
Definition: relation.h:1436
void pfree(void *pointer)
Definition: mcxt.c:950
RelOptInfo * rel
Definition: relation.h:594
Path path
Definition: relation.h:1158
#define linitial(l)
Definition: pg_list.h:110
#define planner_rt_fetch(rti, root)
Definition: relation.h:324
Definition: nodes.h:45
Relids all_baserels
Definition: relation.h:195
#define ERROR
Definition: elog.h:43
static List * translate_sub_tlist(List *tlist, int relid)
Definition: pathnode.c:1705
double limit_tuples
Definition: relation.h:291
List * partitionClause
Definition: parsenodes.h:1234
void cost_qual_eval(QualCost *cost, List *quals, PlannerInfo *root)
Definition: costsize.c:3359
void expand_indexqual_conditions(IndexOptInfo *index, List *indexclauses, List *indexclausecols, List **indexquals_p, List **indexqualcols_p)
Definition: indxpath.c:3522
Cost startup_cost
Definition: relation.h:910
RecursiveUnionPath * create_recursiveunion_path(PlannerInfo *root, RelOptInfo *rel, Path *leftpath, Path *rightpath, PathTarget *target, List *distinctList, int wtParam, double numGroups)
Definition: pathnode.c:3008
List * semi_rhs_exprs
Definition: relation.h:1841
void cost_subqueryscan(SubqueryScanPath *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
Definition: costsize.c:1267
bool semi_can_hash
Definition: relation.h:1839
Path * subpath
Definition: relation.h:1511
List * joinrestrictinfo
Definition: relation.h:1240
List * subroots
Definition: relation.h:1497
RelOptInfo * parent
Definition: relation.h:898
List * uniq_exprs
Definition: relation.h:1199
Path * bitmapqual
Definition: relation.h:1007
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:307
Definition: nodes.h:75
Path path
Definition: relation.h:1364
List * rollup_lists
Definition: relation.h:1412
int compare_path_costs(Path *path1, Path *path2, CostSelector criterion)
Definition: pathnode.c:61
AggPath * create_agg_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target, AggStrategy aggstrategy, AggSplit aggsplit, List *groupClause, List *qual, const AggClauseCosts *aggcosts, double numGroups)
Definition: pathnode.c:2640
struct Path * cheapest_total_path
Definition: relation.h:512
ForeignPath * create_foreignscan_path(PlannerInfo *root, RelOptInfo *rel, PathTarget *target, double rows, Cost startup_cost, Cost total_cost, List *pathkeys, Relids required_outer, Path *fdw_outerpath, List *fdw_private)
Definition: pathnode.c:1934
ProjectSetPath * create_set_projection_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target)
Definition: pathnode.c:2419
static PathCostComparison compare_path_costs_fuzzily(Path *path1, Path *path2, double fuzz_factor)
Definition: pathnode.c:156
ScanDirection
Definition: sdir.h:22
void cost_agg(Path *path, PlannerInfo *root, AggStrategy aggstrategy, const AggClauseCosts *aggcosts, int numGroupCols, double numGroups, Cost input_startup_cost, Cost input_total_cost, double input_tuples)
Definition: costsize.c:1836
GroupPath * create_group_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target, List *groupClause, List *qual, double numGroups)
Definition: pathnode.c:2530
List * subpaths
Definition: relation.h:1496
List * groupClause
Definition: relation.h:1397
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
AttrNumber flagColIdx
Definition: relation.h:1451
MergeAppendPath * create_merge_append_path(PlannerInfo *root, RelOptInfo *rel, List *subpaths, List *pathkeys, Relids required_outer, List *partitioned_rels)
Definition: pathnode.c:1258
Relids relids
Definition: relation.h:494
double cpu_operator_cost
Definition: costsize.c:108
Path * subpath
Definition: relation.h:1210
double rint(double x)
Definition: rint.c:22
void cost_samplescan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
Definition: costsize.c:274
#define lnext(lc)
Definition: pg_list.h:105
bool join_clause_is_movable_into(RestrictInfo *rinfo, Relids currentrelids, Relids current_and_outer)
Definition: restrictinfo.c:508
List * lappend_int(List *list, int datum)
Definition: list.c:146
Index relid
Definition: relation.h:522
Path * create_worktablescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:1900
List * lappend(List *list, void *datum)
Definition: list.c:128
Path * subpath
Definition: relation.h:1475
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:633
Index varno
Definition: primnodes.h:166
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:234
List * exprs
Definition: relation.h:828
BitmapAndPath * create_bitmap_and_path(PlannerInfo *root, RelOptInfo *rel, List *bitmapquals)
Definition: pathnode.c:1100
List * list_delete_cell(List *list, ListCell *cell, ListCell *prev)
Definition: list.c:528
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:317
Path * outerjoinpath
Definition: relation.h:1237
void cost_index(IndexPath *path, PlannerInfo *root, double loop_count, bool partial_path)
Definition: costsize.c:462
List * rollup_groupclauses
Definition: relation.h:1411
void final_cost_hashjoin(PlannerInfo *root, HashPath *path, JoinCostWorkspace *workspace, SpecialJoinInfo *sjinfo, SemiAntiJoinFactors *semifactors)
Definition: costsize.c:2935
List * indexorderbys
Definition: relation.h:980
void cost_recursive_union(Path *runion, Path *nrterm, Path *rterm)
Definition: costsize.c:1527
WindowAggPath * create_windowagg_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target, List *windowFuncs, WindowClause *winclause, List *winpathkeys)
Definition: pathnode.c:2882
Path * create_samplescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:963
List * groupClause
Definition: relation.h:1366
Path * create_tablefuncscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:1823
List * mmaggregates
Definition: relation.h:1422
List * partitioned_rels
Definition: relation.h:1494
List * tidquals
Definition: relation.h:1046
void cost_sort(Path *path, PlannerInfo *root, List *pathkeys, Cost input_cost, double tuples, int width, Cost comparison_cost, int sort_mem, double limit_tuples)
Definition: costsize.c:1607
GroupingSetsPath * create_groupingsets_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target, List *having_qual, List *rollup_lists, List *rollup_groupclauses, const AggClauseCosts *agg_costs, double numGroups)
Definition: pathnode.c:2706
int work_mem
Definition: globals.c:112
Path * subpath
Definition: relation.h:1365
unsigned int Index
Definition: c.h:365
RTEKind rtekind
Definition: relation.h:524
PathCostComparison
Definition: pathnode.c:33
List * in_operators
Definition: relation.h:1198
double rows
Definition: relation.h:497
GatherMergePath * create_gather_merge_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, PathTarget *target, List *pathkeys, Relids required_outer, double *rows)
Definition: pathnode.c:1640
SortPath * create_sort_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, List *pathkeys, double limit_tuples)
Definition: pathnode.c:2486
BMS_Comparison bms_subset_compare(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:344
Cost total_cost
Definition: relation.h:911
void cost_material(Path *path, Cost input_startup_cost, Cost input_total_cost, double tuples, int width)
Definition: costsize.c:1782
int firstFlag
Definition: relation.h:1452
List * lcons(void *datum, List *list)
Definition: list.c:259
List * pathkeys
Definition: relation.h:913
void bms_free(Bitmapset *a)
Definition: bitmapset.c:200
#define makeNode(_type_)
Definition: nodes.h:568
void cost_tablefuncscan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
Definition: costsize.c:1377
void cost_merge_append(Path *path, PlannerInfo *root, List *pathkeys, int n_streams, Cost input_startup_cost, Cost input_total_cost, double tuples)
Definition: costsize.c:1731
#define CONSIDER_PATH_STARTUP_COST(p)
Path path
Definition: relation.h:1233
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
Path * subpath
Definition: relation.h:1337
double rows
Definition: relation.h:909
bool parallel_safe
Definition: relation.h:904
ModifyTablePath * create_modifytable_path(PlannerInfo *root, RelOptInfo *rel, CmdType operation, bool canSetTag, Index nominalRelation, List *partitioned_rels, List *resultRelations, List *subpaths, List *subroots, List *withCheckOptionLists, List *returningLists, List *rowMarks, OnConflictExpr *onconflict, int epqParam)
Definition: pathnode.c:3109
int compare_fractional_path_costs(Path *path1, Path *path2, double fraction)
Definition: pathnode.c:107
List * quals
Definition: relation.h:1159
#define PATH_REQ_OUTER(path)
Definition: relation.h:918
JoinType jointype
Definition: relation.h:1834
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:217
#define STD_FUZZ_FACTOR
Definition: pathnode.c:46
QualCost cost
Definition: relation.h:830
AggSplit
Definition: nodes.h:760
static int list_length(const List *l)
Definition: pg_list.h:89
CostSelector
Definition: relation.h:34
bool consider_parallel
Definition: relation.h:502
List * innersortkeys
Definition: relation.h:1288
double cpu_tuple_cost
Definition: costsize.c:106
Path * subpath
Definition: relation.h:1393
bool query_supports_distinctness(Query *query)
Definition: analyzejoins.c:650
List * partitioned_rels
Definition: relation.h:1124
void cost_gather(GatherPath *path, PlannerInfo *root, RelOptInfo *rel, ParamPathInfo *param_info, double *rows)
Definition: costsize.c:349
Path * subpath
Definition: relation.h:1222
Path path
Definition: relation.h:1510
Path path
Definition: relation.h:1045
List * withCheckOptionLists
Definition: relation.h:1498
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:787
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:442
List * indexqualcols
Definition: relation.h:979
Definition: nodes.h:82
List * orderClause
Definition: parsenodes.h:1235
PathKeysComparison
Definition: paths.h:175
int width
Definition: relation.h:831
Query * subquery
Definition: parsenodes.h:934
AggStrategy
Definition: nodes.h:739
bool is_projection_capable_path(Path *path)
Definition: createplan.c:6423
TidPath * create_tidscan_path(PlannerInfo *root, RelOptInfo *rel, List *tidquals, Relids required_outer)
Definition: pathnode.c:1172
Path * reparameterize_path(PlannerInfo *root, Path *path, Relids required_outer, double loop_count)
Definition: pathnode.c:3307
void cost_functionscan(Path *path, PlannerInfo *root, RelOptInfo *baserel, ParamPathInfo *param_info)
Definition: costsize.c:1316
void add_partial_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:752
List * fdw_private
Definition: relation.h:1077
SetOpCmd
Definition: nodes.h:782
JoinType jointype
Definition: relation.h:1235
List * semi_operators
Definition: relation.h:1840
ScanDirection indexscandir
Definition: relation.h:982
CmdType operation
Definition: relation.h:1490
Definition: nodes.h:79
List * resultRelations
Definition: relation.h:1495
void cost_bitmap_or_node(BitmapOrPath *path, PlannerInfo *root)
Definition: costsize.c:1116
JoinPath jpath
Definition: relation.h:1285
ResultPath * create_result_path(PlannerInfo *root, RelOptInfo *rel, PathTarget *target, List *resconstantqual)
Definition: pathnode.c:1350
bool parallel_aware
Definition: relation.h:903
List * path_hashclauses
Definition: relation.h:1304
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
List * pathlist
Definition: relation.h:508
List * subpaths
Definition: relation.h:1145
MemoryContext planner_cxt
Definition: relation.h:286
#define elog
Definition: elog.h:219
ParamPathInfo * get_appendrel_parampathinfo(RelOptInfo *appendrel, Relids required_outer)
Definition: relnode.c:1334
bool relation_has_unique_index_for(PlannerInfo *root, RelOptInfo *rel, List *restrictlist, List *exprlist, List *oprlist)
Definition: indxpath.c:2956
Path * subpath
Definition: relation.h:1325
Path path
Definition: relation.h:1209
double clamp_row_est(double nrows)
Definition: costsize.c:173
Node * limitCount
Definition: relation.h:1513
Definition: pg_list.h:45
Path path
Definition: relation.h:1350
struct PathTarget * reltarget
Definition: relation.h:505
int16 AttrNumber
Definition: attnum.h:21
Path path
Definition: relation.h:1446
Path path
Definition: relation.h:1195
CmdType
Definition: nodes.h:653
Path * create_seqscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer, int parallel_workers)
Definition: pathnode.c:938
Path path
Definition: relation.h:1392
Definition: relation.h:892
double limit_tuples
Definition: relation.h:1146
BMS_Comparison
Definition: bitmapset.h:40
double Cost
Definition: nodes.h:644
Relids calc_nestloop_required_outer(Path *outer_path, Path *inner_path)
Definition: pathnode.c:1970
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:234
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:130
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:742
Path * subpath
Definition: relation.h:1196
BitmapHeapPath * create_bitmap_heap_path(PlannerInfo *root, RelOptInfo *rel, Path *bitmapqual, Relids required_outer, double loop_count, int parallel_degree)
Definition: pathnode.c:1067
Definition: nodes.h:84
Path * create_ctescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
Definition: pathnode.c:1875