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