Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * pathnode.c
4 : : * Routines to manipulate pathlists and create path nodes
5 : : *
6 : : * Portions Copyright (c) 1996-2026, 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 "access/htup_details.h"
18 : : #include "executor/nodeSetOp.h"
19 : : #include "foreign/fdwapi.h"
20 : : #include "miscadmin.h"
21 : : #include "nodes/extensible.h"
22 : : #include "optimizer/appendinfo.h"
23 : : #include "optimizer/clauses.h"
24 : : #include "optimizer/cost.h"
25 : : #include "optimizer/optimizer.h"
26 : : #include "optimizer/pathnode.h"
27 : : #include "optimizer/paths.h"
28 : : #include "optimizer/planmain.h"
29 : : #include "optimizer/tlist.h"
30 : : #include "parser/parsetree.h"
31 : : #include "utils/memutils.h"
32 : : #include "utils/selfuncs.h"
33 : :
34 : : typedef enum
35 : : {
36 : : COSTS_EQUAL, /* path costs are fuzzily equal */
37 : : COSTS_BETTER1, /* first path is cheaper than second */
38 : : COSTS_BETTER2, /* second path is cheaper than first */
39 : : COSTS_DIFFERENT, /* neither path dominates the other on cost */
40 : : } PathCostComparison;
41 : :
42 : : /*
43 : : * STD_FUZZ_FACTOR is the normal fuzz factor for compare_path_costs_fuzzily.
44 : : * XXX is it worth making this user-controllable? It provides a tradeoff
45 : : * between planner runtime and the accuracy of path cost comparisons.
46 : : */
47 : : #define STD_FUZZ_FACTOR 1.01
48 : :
49 : : static int append_total_cost_compare(const ListCell *a, const ListCell *b);
50 : : static int append_startup_cost_compare(const ListCell *a, const ListCell *b);
51 : : static List *reparameterize_pathlist_by_child(PlannerInfo *root,
52 : : List *pathlist,
53 : : RelOptInfo *child_rel);
54 : : static bool pathlist_is_reparameterizable_by_child(List *pathlist,
55 : : RelOptInfo *child_rel);
56 : :
57 : :
58 : : /*****************************************************************************
59 : : * MISC. PATH UTILITIES
60 : : *****************************************************************************/
61 : :
62 : : /*
63 : : * compare_path_costs
64 : : * Return -1, 0, or +1 according as path1 is cheaper, the same cost,
65 : : * or more expensive than path2 for the specified criterion.
66 : : */
67 : : int
68 : 919166 : compare_path_costs(Path *path1, Path *path2, CostSelector criterion)
69 : : {
70 : : /* Number of disabled nodes, if different, trumps all else. */
71 [ + + ]: 919166 : if (unlikely(path1->disabled_nodes != path2->disabled_nodes))
72 : : {
73 [ + + ]: 80278 : if (path1->disabled_nodes < path2->disabled_nodes)
74 : 80270 : return -1;
75 : : else
76 : 8 : return +1;
77 : : }
78 : :
79 [ + + ]: 838888 : if (criterion == STARTUP_COST)
80 : : {
81 [ + + ]: 426532 : if (path1->startup_cost < path2->startup_cost)
82 : 256814 : return -1;
83 [ + + ]: 169718 : if (path1->startup_cost > path2->startup_cost)
84 : 83058 : return +1;
85 : :
86 : : /*
87 : : * If paths have the same startup cost (not at all unlikely), order
88 : : * them by total cost.
89 : : */
90 [ + + ]: 86660 : if (path1->total_cost < path2->total_cost)
91 : 42429 : return -1;
92 [ + + ]: 44231 : if (path1->total_cost > path2->total_cost)
93 : 4293 : return +1;
94 : : }
95 : : else
96 : : {
97 [ + + ]: 412356 : if (path1->total_cost < path2->total_cost)
98 : 384353 : return -1;
99 [ + + ]: 28003 : if (path1->total_cost > path2->total_cost)
100 : 7355 : return +1;
101 : :
102 : : /*
103 : : * If paths have the same total cost, order them by startup cost.
104 : : */
105 [ + + ]: 20648 : if (path1->startup_cost < path2->startup_cost)
106 : 1527 : return -1;
107 [ + + ]: 19121 : if (path1->startup_cost > path2->startup_cost)
108 : 65 : return +1;
109 : : }
110 : 58994 : return 0;
111 : : }
112 : :
113 : : /*
114 : : * compare_fractional_path_costs
115 : : * Return -1, 0, or +1 according as path1 is cheaper, the same cost,
116 : : * or more expensive than path2 for fetching the specified fraction
117 : : * of the total tuples.
118 : : *
119 : : * If fraction is <= 0 or > 1, we interpret it as 1, ie, we select the
120 : : * path with the cheaper total_cost.
121 : : */
122 : : int
123 : 3771 : compare_fractional_path_costs(Path *path1, Path *path2,
124 : : double fraction)
125 : : {
126 : : Cost cost1,
127 : : cost2;
128 : :
129 : : /* Number of disabled nodes, if different, trumps all else. */
130 [ + + ]: 3771 : if (unlikely(path1->disabled_nodes != path2->disabled_nodes))
131 : : {
132 [ + - ]: 42 : if (path1->disabled_nodes < path2->disabled_nodes)
133 : 42 : return -1;
134 : : else
135 : 0 : return +1;
136 : : }
137 : :
138 [ + - + + ]: 3729 : if (fraction <= 0.0 || fraction >= 1.0)
139 : 987 : return compare_path_costs(path1, path2, TOTAL_COST);
140 : 2742 : cost1 = path1->startup_cost +
141 : 2742 : fraction * (path1->total_cost - path1->startup_cost);
142 : 2742 : cost2 = path2->startup_cost +
143 : 2742 : fraction * (path2->total_cost - path2->startup_cost);
144 [ + + ]: 2742 : if (cost1 < cost2)
145 : 2319 : return -1;
146 [ + - ]: 423 : if (cost1 > cost2)
147 : 423 : return +1;
148 : 0 : return 0;
149 : : }
150 : :
151 : : /*
152 : : * compare_path_costs_fuzzily
153 : : * Compare the costs of two paths to see if either can be said to
154 : : * dominate the other.
155 : : *
156 : : * We use fuzzy comparisons so that add_path() can avoid keeping both of
157 : : * a pair of paths that really have insignificantly different cost.
158 : : *
159 : : * The fuzz_factor argument must be 1.0 plus delta, where delta is the
160 : : * fraction of the smaller cost that is considered to be a significant
161 : : * difference. For example, fuzz_factor = 1.01 makes the fuzziness limit
162 : : * be 1% of the smaller cost.
163 : : *
164 : : * The two paths are said to have "equal" costs if both startup and total
165 : : * costs are fuzzily the same. Path1 is said to be better than path2 if
166 : : * it has fuzzily better startup cost and fuzzily no worse total cost,
167 : : * or if it has fuzzily better total cost and fuzzily no worse startup cost.
168 : : * Path2 is better than path1 if the reverse holds. Finally, if one path
169 : : * is fuzzily better than the other on startup cost and fuzzily worse on
170 : : * total cost, we just say that their costs are "different", since neither
171 : : * dominates the other across the whole performance spectrum.
172 : : *
173 : : * This function also enforces a policy rule that paths for which the relevant
174 : : * one of parent->consider_startup and parent->consider_param_startup is false
175 : : * cannot survive comparisons solely on the grounds of good startup cost, so
176 : : * we never return COSTS_DIFFERENT when that is true for the total-cost loser.
177 : : * (But if total costs are fuzzily equal, we compare startup costs anyway,
178 : : * in hopes of eliminating one path or the other.)
179 : : */
180 : : static PathCostComparison
181 : 3849001 : compare_path_costs_fuzzily(Path *path1, Path *path2, double fuzz_factor)
182 : : {
183 : : #define CONSIDER_PATH_STARTUP_COST(p) \
184 : : ((p)->param_info == NULL ? (p)->parent->consider_startup : (p)->parent->consider_param_startup)
185 : :
186 : : /* Number of disabled nodes, if different, trumps all else. */
187 [ + + ]: 3849001 : if (unlikely(path1->disabled_nodes != path2->disabled_nodes))
188 : : {
189 [ + + ]: 185682 : if (path1->disabled_nodes < path2->disabled_nodes)
190 : 66026 : return COSTS_BETTER1;
191 : : else
192 : 119656 : return COSTS_BETTER2;
193 : : }
194 : :
195 : : /*
196 : : * Check total cost first since it's more likely to be different; many
197 : : * paths have zero startup cost.
198 : : */
199 [ + + ]: 3663319 : if (path1->total_cost > path2->total_cost * fuzz_factor)
200 : : {
201 : : /* path1 fuzzily worse on total cost */
202 [ + + + + ]: 1895516 : if (CONSIDER_PATH_STARTUP_COST(path1) &&
203 [ + + ]: 89041 : path2->startup_cost > path1->startup_cost * fuzz_factor)
204 : : {
205 : : /* ... but path2 fuzzily worse on startup, so DIFFERENT */
206 : 47317 : return COSTS_DIFFERENT;
207 : : }
208 : : /* else path2 dominates */
209 : 1848199 : return COSTS_BETTER2;
210 : : }
211 [ + + ]: 1767803 : if (path2->total_cost > path1->total_cost * fuzz_factor)
212 : : {
213 : : /* path2 fuzzily worse on total cost */
214 [ + + + + ]: 884597 : if (CONSIDER_PATH_STARTUP_COST(path2) &&
215 [ + + ]: 33721 : path1->startup_cost > path2->startup_cost * fuzz_factor)
216 : : {
217 : : /* ... but path1 fuzzily worse on startup, so DIFFERENT */
218 : 20272 : return COSTS_DIFFERENT;
219 : : }
220 : : /* else path1 dominates */
221 : 864325 : return COSTS_BETTER1;
222 : : }
223 : : /* fuzzily the same on total cost ... */
224 [ + + ]: 883206 : if (path1->startup_cost > path2->startup_cost * fuzz_factor)
225 : : {
226 : : /* ... but path1 fuzzily worse on startup, so path2 wins */
227 : 281944 : return COSTS_BETTER2;
228 : : }
229 [ + + ]: 601262 : if (path2->startup_cost > path1->startup_cost * fuzz_factor)
230 : : {
231 : : /* ... but path2 fuzzily worse on startup, so path1 wins */
232 : 46048 : return COSTS_BETTER1;
233 : : }
234 : : /* fuzzily the same on both costs */
235 : 555214 : return COSTS_EQUAL;
236 : :
237 : : #undef CONSIDER_PATH_STARTUP_COST
238 : : }
239 : :
240 : : /*
241 : : * set_cheapest
242 : : * Find the minimum-cost paths from among a relation's paths,
243 : : * and save them in the rel's cheapest-path fields.
244 : : *
245 : : * cheapest_total_path is normally the cheapest-total-cost unparameterized
246 : : * path; but if there are no unparameterized paths, we assign it to be the
247 : : * best (cheapest least-parameterized) parameterized path. However, only
248 : : * unparameterized paths are considered candidates for cheapest_startup_path,
249 : : * so that will be NULL if there are no unparameterized paths.
250 : : *
251 : : * The cheapest_parameterized_paths list collects all parameterized paths
252 : : * that have survived the add_path() tournament for this relation. (Since
253 : : * add_path ignores pathkeys for a parameterized path, these will be paths
254 : : * that have best cost or best row count for their parameterization. We
255 : : * may also have both a parallel-safe and a non-parallel-safe path in some
256 : : * cases for the same parameterization in some cases, but this should be
257 : : * relatively rare since, most typically, all paths for the same relation
258 : : * will be parallel-safe or none of them will.)
259 : : *
260 : : * cheapest_parameterized_paths always includes the cheapest-total
261 : : * unparameterized path, too, if there is one; the users of that list find
262 : : * it more convenient if that's included.
263 : : *
264 : : * This is normally called only after we've finished constructing the path
265 : : * list for the rel node.
266 : : */
267 : : void
268 : 1618736 : set_cheapest(RelOptInfo *parent_rel)
269 : : {
270 : : Path *cheapest_startup_path;
271 : : Path *cheapest_total_path;
272 : : Path *best_param_path;
273 : : List *parameterized_paths;
274 : : ListCell *p;
275 : :
276 : : Assert(IsA(parent_rel, RelOptInfo));
277 : :
278 [ - + ]: 1618736 : if (parent_rel->pathlist == NIL)
279 [ # # ]: 0 : elog(ERROR, "could not devise a query plan for the given query");
280 : :
281 : 1618736 : cheapest_startup_path = cheapest_total_path = best_param_path = NULL;
282 : 1618736 : parameterized_paths = NIL;
283 : :
284 [ + - + + : 3715084 : foreach(p, parent_rel->pathlist)
+ + ]
285 : : {
286 : 2096348 : Path *path = (Path *) lfirst(p);
287 : : int cmp;
288 : :
289 [ + + ]: 2096348 : if (path->param_info)
290 : : {
291 : : /* Parameterized path, so add it to parameterized_paths */
292 : 112437 : parameterized_paths = lappend(parameterized_paths, path);
293 : :
294 : : /*
295 : : * If we have an unparameterized cheapest-total, we no longer care
296 : : * about finding the best parameterized path, so move on.
297 : : */
298 [ + + ]: 112437 : if (cheapest_total_path)
299 : 25830 : continue;
300 : :
301 : : /*
302 : : * Otherwise, track the best parameterized path, which is the one
303 : : * with least total cost among those of the minimum
304 : : * parameterization.
305 : : */
306 [ + + ]: 86607 : if (best_param_path == NULL)
307 : 77195 : best_param_path = path;
308 : : else
309 : : {
310 [ + - + + : 9412 : switch (bms_subset_compare(PATH_REQ_OUTER(path),
+ + - ]
311 [ + - ]: 9412 : PATH_REQ_OUTER(best_param_path)))
312 : : {
313 : 45 : case BMS_EQUAL:
314 : : /* keep the cheaper one */
315 [ - + ]: 45 : if (compare_path_costs(path, best_param_path,
316 : : TOTAL_COST) < 0)
317 : 0 : best_param_path = path;
318 : 45 : break;
319 : 992 : case BMS_SUBSET1:
320 : : /* new path is less-parameterized */
321 : 992 : best_param_path = path;
322 : 992 : break;
323 : 21 : case BMS_SUBSET2:
324 : : /* old path is less-parameterized, keep it */
325 : 21 : break;
326 : 8354 : case BMS_DIFFERENT:
327 : :
328 : : /*
329 : : * This means that neither path has the least possible
330 : : * parameterization for the rel. We'll sit on the old
331 : : * path until something better comes along.
332 : : */
333 : 8354 : break;
334 : : }
335 : : }
336 : : }
337 : : else
338 : : {
339 : : /* Unparameterized path, so consider it for cheapest slots */
340 [ + + ]: 1983911 : if (cheapest_total_path == NULL)
341 : : {
342 : 1609957 : cheapest_startup_path = cheapest_total_path = path;
343 : 1609957 : continue;
344 : : }
345 : :
346 : : /*
347 : : * If we find two paths of identical costs, try to keep the
348 : : * better-sorted one. The paths might have unrelated sort
349 : : * orderings, in which case we can only guess which might be
350 : : * better to keep, but if one is superior then we definitely
351 : : * should keep that one.
352 : : */
353 : 373954 : cmp = compare_path_costs(cheapest_startup_path, path, STARTUP_COST);
354 [ + + + + ]: 373954 : if (cmp > 0 ||
355 [ - + ]: 1388 : (cmp == 0 &&
356 : 1388 : compare_pathkeys(cheapest_startup_path->pathkeys,
357 : : path->pathkeys) == PATHKEYS_BETTER2))
358 : 61254 : cheapest_startup_path = path;
359 : :
360 : 373954 : cmp = compare_path_costs(cheapest_total_path, path, TOTAL_COST);
361 [ + - + + ]: 373954 : if (cmp > 0 ||
362 [ - + ]: 974 : (cmp == 0 &&
363 : 974 : compare_pathkeys(cheapest_total_path->pathkeys,
364 : : path->pathkeys) == PATHKEYS_BETTER2))
365 : 0 : cheapest_total_path = path;
366 : : }
367 : : }
368 : :
369 : : /* Add cheapest unparameterized path, if any, to parameterized_paths */
370 [ + + ]: 1618736 : if (cheapest_total_path)
371 : 1609957 : parameterized_paths = lcons(cheapest_total_path, parameterized_paths);
372 : :
373 : : /*
374 : : * If there is no unparameterized path, use the best parameterized path as
375 : : * cheapest_total_path (but not as cheapest_startup_path).
376 : : */
377 [ + + ]: 1618736 : if (cheapest_total_path == NULL)
378 : 8779 : cheapest_total_path = best_param_path;
379 : : Assert(cheapest_total_path != NULL);
380 : :
381 : 1618736 : parent_rel->cheapest_startup_path = cheapest_startup_path;
382 : 1618736 : parent_rel->cheapest_total_path = cheapest_total_path;
383 : 1618736 : parent_rel->cheapest_parameterized_paths = parameterized_paths;
384 : 1618736 : }
385 : :
386 : : /*
387 : : * add_path
388 : : * Consider a potential implementation path for the specified parent rel,
389 : : * and add it to the rel's pathlist if it is worthy of consideration.
390 : : *
391 : : * A path is worthy if it has a better sort order (better pathkeys) or
392 : : * cheaper cost (as defined below), or generates fewer rows, than any
393 : : * existing path that has the same or superset parameterization rels. We
394 : : * also consider parallel-safe paths more worthy than others.
395 : : *
396 : : * Cheaper cost can mean either a cheaper total cost or a cheaper startup
397 : : * cost; if one path is cheaper in one of these aspects and another is
398 : : * cheaper in the other, we keep both. However, when some path type is
399 : : * disabled (e.g. due to enable_seqscan=false), the number of times that
400 : : * a disabled path type is used is considered to be a higher-order
401 : : * component of the cost. Hence, if path A uses no disabled path type,
402 : : * and path B uses 1 or more disabled path types, A is cheaper, no matter
403 : : * what we estimate for the startup and total costs. The startup and total
404 : : * cost essentially act as a tiebreak when comparing paths that use equal
405 : : * numbers of disabled path nodes; but in practice this tiebreak is almost
406 : : * always used, since normally no path types are disabled.
407 : : *
408 : : * In addition to possibly adding new_path, we also remove from the rel's
409 : : * pathlist any old paths that are dominated by new_path --- that is,
410 : : * new_path is cheaper, at least as well ordered, generates no more rows,
411 : : * requires no outer rels not required by the old path, and is no less
412 : : * parallel-safe.
413 : : *
414 : : * In most cases, a path with a superset parameterization will generate
415 : : * fewer rows (since it has more join clauses to apply), so that those two
416 : : * figures of merit move in opposite directions; this means that a path of
417 : : * one parameterization can seldom dominate a path of another. But such
418 : : * cases do arise, so we make the full set of checks anyway.
419 : : *
420 : : * There are two policy decisions embedded in this function, along with
421 : : * its sibling add_path_precheck. First, we treat all parameterized paths
422 : : * as having NIL pathkeys, so that they cannot win comparisons on the
423 : : * basis of sort order. This is to reduce the number of parameterized
424 : : * paths that are kept; see discussion in src/backend/optimizer/README.
425 : : *
426 : : * Second, we only consider cheap startup cost to be interesting if
427 : : * parent_rel->consider_startup is true for an unparameterized path, or
428 : : * parent_rel->consider_param_startup is true for a parameterized one.
429 : : * Again, this allows discarding useless paths sooner.
430 : : *
431 : : * The pathlist is kept sorted by disabled_nodes and then by total_cost,
432 : : * with cheaper paths at the front. Within this routine, that's simply a
433 : : * speed hack: doing it that way makes it more likely that we will reject
434 : : * an inferior path after a few comparisons, rather than many comparisons.
435 : : * However, add_path_precheck relies on this ordering to exit early
436 : : * when possible.
437 : : *
438 : : * NOTE: discarded Path objects are immediately pfree'd to reduce planner
439 : : * memory consumption. We dare not try to free the substructure of a Path,
440 : : * since much of it may be shared with other Paths or the query tree itself;
441 : : * but just recycling discarded Path nodes is a very useful savings in
442 : : * a large join tree. We can recycle the List nodes of pathlist, too.
443 : : *
444 : : * As noted in optimizer/README, deleting a previously-accepted Path is
445 : : * safe because we know that Paths of this rel cannot yet be referenced
446 : : * from any other rel, such as a higher-level join. However, in some cases
447 : : * it is possible that a Path is referenced by another Path for its own
448 : : * rel; we must not delete such a Path, even if it is dominated by the new
449 : : * Path. Currently this occurs only for IndexPath objects, which may be
450 : : * referenced as children of BitmapHeapPaths as well as being paths in
451 : : * their own right. Hence, we don't pfree IndexPaths when rejecting them.
452 : : *
453 : : * 'parent_rel' is the relation entry to which the path corresponds.
454 : : * 'new_path' is a potential path for parent_rel.
455 : : *
456 : : * Returns nothing, but modifies parent_rel->pathlist.
457 : : */
458 : : void
459 : 3634601 : add_path(RelOptInfo *parent_rel, Path *new_path)
460 : : {
461 : 3634601 : bool accept_new = true; /* unless we find a superior old path */
462 : 3634601 : int insert_at = 0; /* where to insert new item */
463 : : List *new_path_pathkeys;
464 : : ListCell *p1;
465 : :
466 : : /*
467 : : * This is a convenient place to check for query cancel --- no part of the
468 : : * planner goes very long without calling add_path().
469 : : */
470 [ + + ]: 3634601 : CHECK_FOR_INTERRUPTS();
471 : :
472 : : /* Pretend parameterized paths have no pathkeys, per comment above */
473 [ + + ]: 3634601 : new_path_pathkeys = new_path->param_info ? NIL : new_path->pathkeys;
474 : :
475 : : /*
476 : : * Loop to check proposed new path against old paths. Note it is possible
477 : : * for more than one old path to be tossed out because new_path dominates
478 : : * it.
479 : : */
480 [ + + + + : 5594359 : foreach(p1, parent_rel->pathlist)
+ + ]
481 : : {
482 : 3334616 : Path *old_path = (Path *) lfirst(p1);
483 : 3334616 : bool remove_old = false; /* unless new proves superior */
484 : : PathCostComparison costcmp;
485 : : PathKeysComparison keyscmp;
486 : : BMS_Comparison outercmp;
487 : :
488 : : /*
489 : : * Do a fuzzy cost comparison with standard fuzziness limit.
490 : : */
491 : 3334616 : costcmp = compare_path_costs_fuzzily(new_path, old_path,
492 : : STD_FUZZ_FACTOR);
493 : :
494 : : /*
495 : : * If the two paths compare differently for startup and total cost,
496 : : * then we want to keep both, and we can skip comparing pathkeys and
497 : : * required_outer rels. If they compare the same, proceed with the
498 : : * other comparisons. Row count is checked last. (We make the tests
499 : : * in this order because the cost comparison is most likely to turn
500 : : * out "different", and the pathkeys comparison next most likely. As
501 : : * explained above, row count very seldom makes a difference, so even
502 : : * though it's cheap to compare there's not much point in checking it
503 : : * earlier.)
504 : : */
505 [ + + ]: 3334616 : if (costcmp != COSTS_DIFFERENT)
506 : : {
507 : : /* Similarly check to see if either dominates on pathkeys */
508 : : List *old_path_pathkeys;
509 : :
510 [ + + ]: 3267483 : old_path_pathkeys = old_path->param_info ? NIL : old_path->pathkeys;
511 : 3267483 : keyscmp = compare_pathkeys(new_path_pathkeys,
512 : : old_path_pathkeys);
513 [ + + ]: 3267483 : if (keyscmp != PATHKEYS_DIFFERENT)
514 : : {
515 [ + + + - : 3094621 : switch (costcmp)
- ]
516 : : {
517 : 327703 : case COSTS_EQUAL:
518 [ + + ]: 327703 : outercmp = bms_subset_compare(PATH_REQ_OUTER(new_path),
519 [ + + ]: 327703 : PATH_REQ_OUTER(old_path));
520 [ + + ]: 327703 : if (keyscmp == PATHKEYS_BETTER1)
521 : : {
522 [ + + + - ]: 6734 : if ((outercmp == BMS_EQUAL ||
523 : 6734 : outercmp == BMS_SUBSET1) &&
524 [ + + ]: 6734 : new_path->rows <= old_path->rows &&
525 [ + - ]: 6678 : new_path->parallel_safe >= old_path->parallel_safe)
526 : 6678 : remove_old = true; /* new dominates old */
527 : : }
528 [ + + ]: 320969 : else if (keyscmp == PATHKEYS_BETTER2)
529 : : {
530 [ + + + - ]: 19680 : if ((outercmp == BMS_EQUAL ||
531 : 19680 : outercmp == BMS_SUBSET2) &&
532 [ + + ]: 19680 : new_path->rows >= old_path->rows &&
533 [ + - ]: 15787 : new_path->parallel_safe <= old_path->parallel_safe)
534 : 15787 : accept_new = false; /* old dominates new */
535 : : }
536 : : else /* keyscmp == PATHKEYS_EQUAL */
537 : : {
538 [ + + ]: 301289 : if (outercmp == BMS_EQUAL)
539 : : {
540 : : /*
541 : : * Same pathkeys and outer rels, and fuzzily
542 : : * the same cost, so keep just one; to decide
543 : : * which, first check parallel-safety, then
544 : : * rows, then do a fuzzy cost comparison with
545 : : * very small fuzz limit. (We used to do an
546 : : * exact cost comparison, but that results in
547 : : * annoying platform-specific plan variations
548 : : * due to roundoff in the cost estimates.) If
549 : : * things are still tied, arbitrarily keep
550 : : * only the old path. Notice that we will
551 : : * keep only the old path even if the
552 : : * less-fuzzy comparison decides the startup
553 : : * and total costs compare differently.
554 : : */
555 : 295051 : if (new_path->parallel_safe >
556 [ + + ]: 295051 : old_path->parallel_safe)
557 : 28 : remove_old = true; /* new dominates old */
558 : 295023 : else if (new_path->parallel_safe <
559 [ + + ]: 295023 : old_path->parallel_safe)
560 : 36 : accept_new = false; /* old dominates new */
561 [ + + ]: 294987 : else if (new_path->rows < old_path->rows)
562 : 39 : remove_old = true; /* new dominates old */
563 [ + + ]: 294948 : else if (new_path->rows > old_path->rows)
564 : 111 : accept_new = false; /* old dominates new */
565 [ + + ]: 294837 : else if (compare_path_costs_fuzzily(new_path,
566 : : old_path,
567 : : 1.0000000001) == COSTS_BETTER1)
568 : 12384 : remove_old = true; /* new dominates old */
569 : : else
570 : 282453 : accept_new = false; /* old equals or
571 : : * dominates new */
572 : : }
573 [ + + ]: 6238 : else if (outercmp == BMS_SUBSET1 &&
574 [ + + ]: 631 : new_path->rows <= old_path->rows &&
575 [ + - ]: 617 : new_path->parallel_safe >= old_path->parallel_safe)
576 : 617 : remove_old = true; /* new dominates old */
577 [ + + ]: 5621 : else if (outercmp == BMS_SUBSET2 &&
578 [ + + ]: 4873 : new_path->rows >= old_path->rows &&
579 [ + - ]: 4584 : new_path->parallel_safe <= old_path->parallel_safe)
580 : 4584 : accept_new = false; /* old dominates new */
581 : : /* else different parameterizations, keep both */
582 : : }
583 : 327703 : break;
584 : 874091 : case COSTS_BETTER1:
585 [ + + ]: 874091 : if (keyscmp != PATHKEYS_BETTER2)
586 : : {
587 [ + + ]: 589822 : outercmp = bms_subset_compare(PATH_REQ_OUTER(new_path),
588 [ + + ]: 589822 : PATH_REQ_OUTER(old_path));
589 [ + + + + ]: 589822 : if ((outercmp == BMS_EQUAL ||
590 : 507417 : outercmp == BMS_SUBSET1) &&
591 [ + + ]: 507417 : new_path->rows <= old_path->rows &&
592 [ + + ]: 503467 : new_path->parallel_safe >= old_path->parallel_safe)
593 : 500823 : remove_old = true; /* new dominates old */
594 : : }
595 : 874091 : break;
596 : 1892827 : case COSTS_BETTER2:
597 [ + + ]: 1892827 : if (keyscmp != PATHKEYS_BETTER1)
598 : : {
599 [ + + ]: 1212664 : outercmp = bms_subset_compare(PATH_REQ_OUTER(new_path),
600 [ + + ]: 1212664 : PATH_REQ_OUTER(old_path));
601 [ + + + + ]: 1212664 : if ((outercmp == BMS_EQUAL ||
602 : 1144016 : outercmp == BMS_SUBSET2) &&
603 [ + + ]: 1144016 : new_path->rows >= old_path->rows &&
604 [ + + ]: 1073519 : new_path->parallel_safe <= old_path->parallel_safe)
605 : 1071887 : accept_new = false; /* old dominates new */
606 : : }
607 : 1892827 : break;
608 : 0 : case COSTS_DIFFERENT:
609 : :
610 : : /*
611 : : * can't get here, but keep this case to keep compiler
612 : : * quiet
613 : : */
614 : 0 : break;
615 : : }
616 : : }
617 : : }
618 : :
619 : : /*
620 : : * Remove current element from pathlist if dominated by new.
621 : : */
622 [ + + ]: 3334616 : if (remove_old)
623 : : {
624 : 520569 : parent_rel->pathlist = foreach_delete_current(parent_rel->pathlist,
625 : : p1);
626 : :
627 : : /*
628 : : * Delete the data pointed-to by the deleted cell, if possible
629 : : */
630 [ + + ]: 520569 : if (!IsA(old_path, IndexPath))
631 : 500466 : pfree(old_path);
632 : : }
633 : : else
634 : : {
635 : : /*
636 : : * new belongs after this old path if it has more disabled nodes
637 : : * or if it has the same number of nodes but a greater total cost
638 : : */
639 [ + + ]: 2814047 : if (new_path->disabled_nodes > old_path->disabled_nodes ||
640 [ + + ]: 2697158 : (new_path->disabled_nodes == old_path->disabled_nodes &&
641 [ + + ]: 2683876 : new_path->total_cost >= old_path->total_cost))
642 : 2362081 : insert_at = foreach_current_index(p1) + 1;
643 : : }
644 : :
645 : : /*
646 : : * If we found an old path that dominates new_path, we can quit
647 : : * scanning the pathlist; we will not add new_path, and we assume
648 : : * new_path cannot dominate any other elements of the pathlist.
649 : : */
650 [ + + ]: 3334616 : if (!accept_new)
651 : 1374858 : break;
652 : : }
653 : :
654 [ + + ]: 3634601 : if (accept_new)
655 : : {
656 : : /* Accept the new path: insert it at proper place in pathlist */
657 : 2259743 : parent_rel->pathlist =
658 : 2259743 : list_insert_nth(parent_rel->pathlist, insert_at, new_path);
659 : : }
660 : : else
661 : : {
662 : : /* Reject and recycle the new path */
663 [ + + ]: 1374858 : if (!IsA(new_path, IndexPath))
664 : 1281190 : pfree(new_path);
665 : : }
666 : 3634601 : }
667 : :
668 : : /*
669 : : * add_path_precheck
670 : : * Check whether a proposed new path could possibly get accepted.
671 : : * We assume we know the path's pathkeys and parameterization accurately,
672 : : * and have lower bounds for its costs.
673 : : *
674 : : * Note that we do not know the path's rowcount, since getting an estimate for
675 : : * that is too expensive to do before prechecking. We assume here that paths
676 : : * of a superset parameterization will generate fewer rows; if that holds,
677 : : * then paths with different parameterizations cannot dominate each other
678 : : * and so we can simply ignore existing paths of another parameterization.
679 : : * (In the infrequent cases where that rule of thumb fails, add_path will
680 : : * get rid of the inferior path.)
681 : : *
682 : : * At the time this is called, we haven't actually built a Path structure,
683 : : * so the required information has to be passed piecemeal.
684 : : */
685 : : bool
686 : 4018202 : add_path_precheck(RelOptInfo *parent_rel, int disabled_nodes,
687 : : Cost startup_cost, Cost total_cost,
688 : : List *pathkeys, Relids required_outer)
689 : : {
690 : : List *new_path_pathkeys;
691 : : bool consider_startup;
692 : : ListCell *p1;
693 : :
694 : : /* Pretend parameterized paths have no pathkeys, per add_path policy */
695 [ + + ]: 4018202 : new_path_pathkeys = required_outer ? NIL : pathkeys;
696 : :
697 : : /* Decide whether new path's startup cost is interesting */
698 [ + + ]: 4018202 : consider_startup = required_outer ? parent_rel->consider_param_startup : parent_rel->consider_startup;
699 : :
700 [ + + + + : 5115469 : foreach(p1, parent_rel->pathlist)
+ + ]
701 : : {
702 : 4818391 : Path *old_path = (Path *) lfirst(p1);
703 : : PathKeysComparison keyscmp;
704 : :
705 : : /*
706 : : * Since the pathlist is sorted by disabled_nodes and then by
707 : : * total_cost, we can stop looking once we reach a path with more
708 : : * disabled nodes, or the same number of disabled nodes plus a
709 : : * total_cost larger than the new path's.
710 : : */
711 [ + + ]: 4818391 : if (unlikely(old_path->disabled_nodes != disabled_nodes))
712 : : {
713 [ + + ]: 129075 : if (disabled_nodes < old_path->disabled_nodes)
714 : 14722 : break;
715 : : }
716 [ + + ]: 4689316 : else if (total_cost <= old_path->total_cost * STD_FUZZ_FACTOR)
717 : 1459436 : break;
718 : :
719 : : /*
720 : : * We are looking for an old_path with the same parameterization (and
721 : : * by assumption the same rowcount) that dominates the new path on
722 : : * pathkeys as well as both cost metrics. If we find one, we can
723 : : * reject the new path.
724 : : *
725 : : * Cost comparisons here should match compare_path_costs_fuzzily.
726 : : */
727 : : /* new path can win on startup cost only if consider_startup */
728 [ + + ]: 3344233 : if (startup_cost > old_path->startup_cost * STD_FUZZ_FACTOR ||
729 [ + + ]: 1587943 : !consider_startup)
730 : : {
731 : : /* new path loses on cost, so check pathkeys... */
732 : : List *old_path_pathkeys;
733 : :
734 [ + + ]: 3293505 : old_path_pathkeys = old_path->param_info ? NIL : old_path->pathkeys;
735 : 3293505 : keyscmp = compare_pathkeys(new_path_pathkeys,
736 : : old_path_pathkeys);
737 [ + + + + ]: 3293505 : if (keyscmp == PATHKEYS_EQUAL ||
738 : : keyscmp == PATHKEYS_BETTER2)
739 : : {
740 : : /* new path does not win on pathkeys... */
741 [ + + + + ]: 2299509 : if (bms_equal(required_outer, PATH_REQ_OUTER(old_path)))
742 : : {
743 : : /* Found an old path that dominates the new one */
744 : 2246966 : return false;
745 : : }
746 : : }
747 : : }
748 : : }
749 : :
750 : 1771236 : return true;
751 : : }
752 : :
753 : : /*
754 : : * add_partial_path
755 : : * Like add_path, our goal here is to consider whether a path is worthy
756 : : * of being kept around, but the considerations here are a bit different.
757 : : * A partial path is one which can be executed in any number of workers in
758 : : * parallel such that each worker will generate a subset of the path's
759 : : * overall result.
760 : : *
761 : : * As in add_path, the partial_pathlist is kept sorted first by smallest
762 : : * number of disabled nodes and then by lowest total cost. This is depended
763 : : * on by multiple places, which just take the front entry as the cheapest
764 : : * path without searching.
765 : : *
766 : : * We don't generate parameterized partial paths for several reasons. Most
767 : : * importantly, they're not safe to execute, because there's nothing to
768 : : * make sure that a parallel scan within the parameterized portion of the
769 : : * plan is running with the same value in every worker at the same time.
770 : : * Fortunately, it seems unlikely to be worthwhile anyway, because having
771 : : * each worker scan the entire outer relation and a subset of the inner
772 : : * relation will generally be a terrible plan. The inner (parameterized)
773 : : * side of the plan will be small anyway. There could be rare cases where
774 : : * this wins big - e.g. if join order constraints put a 1-row relation on
775 : : * the outer side of the topmost join with a parameterized plan on the inner
776 : : * side - but we'll have to be content not to handle such cases until
777 : : * somebody builds an executor infrastructure that can cope with them.
778 : : *
779 : : * Because we don't consider parameterized paths here, we also don't
780 : : * need to consider the row counts as a measure of quality: every path will
781 : : * produce the same number of rows. However, we do need to consider the
782 : : * startup costs: this partial path could be used beneath a Limit node,
783 : : * so a fast-start plan could be correct.
784 : : *
785 : : * As with add_path, we pfree paths that are found to be dominated by
786 : : * another partial path; this requires that there be no other references to
787 : : * such paths yet. Hence, GatherPaths must not be created for a rel until
788 : : * we're done creating all partial paths for it. Unlike add_path, we don't
789 : : * take an exception for IndexPaths as partial index paths won't be
790 : : * referenced by partial BitmapHeapPaths.
791 : : */
792 : : void
793 : 241582 : add_partial_path(RelOptInfo *parent_rel, Path *new_path)
794 : : {
795 : 241582 : bool accept_new = true; /* unless we find a superior old path */
796 : 241582 : int insert_at = 0; /* where to insert new item */
797 : : ListCell *p1;
798 : :
799 : : /* Check for query cancel. */
800 [ - + ]: 241582 : CHECK_FOR_INTERRUPTS();
801 : :
802 : : /* Path to be added must be parallel safe. */
803 : : Assert(new_path->parallel_safe);
804 : :
805 : : /* Relation should be OK for parallelism, too. */
806 : : Assert(parent_rel->consider_parallel);
807 : :
808 : : /*
809 : : * As in add_path, throw out any paths which are dominated by the new
810 : : * path, but throw out the new path if some existing path dominates it.
811 : : */
812 [ + + + + : 338019 : foreach(p1, parent_rel->partial_pathlist)
+ + ]
813 : : {
814 : 192907 : Path *old_path = (Path *) lfirst(p1);
815 : 192907 : bool remove_old = false; /* unless new proves superior */
816 : : PathKeysComparison keyscmp;
817 : :
818 : : /* Compare pathkeys. */
819 : 192907 : keyscmp = compare_pathkeys(new_path->pathkeys, old_path->pathkeys);
820 : :
821 : : /*
822 : : * Unless pathkeys are incompatible, see if one of the paths dominates
823 : : * the other (both in startup and total cost). It may happen that one
824 : : * path has lower startup cost, the other has lower total cost.
825 : : */
826 [ + + ]: 192907 : if (keyscmp != PATHKEYS_DIFFERENT)
827 : : {
828 : : PathCostComparison costcmp;
829 : :
830 : : /*
831 : : * Do a fuzzy cost comparison with standard fuzziness limit.
832 : : */
833 : 192740 : costcmp = compare_path_costs_fuzzily(new_path, old_path,
834 : : STD_FUZZ_FACTOR);
835 [ + + ]: 192740 : if (costcmp == COSTS_BETTER1)
836 : : {
837 [ + + ]: 68769 : if (keyscmp != PATHKEYS_BETTER2)
838 : 27458 : remove_old = true;
839 : : }
840 [ + + ]: 123971 : else if (costcmp == COSTS_BETTER2)
841 : : {
842 [ + + ]: 95623 : if (keyscmp != PATHKEYS_BETTER1)
843 : 68739 : accept_new = false;
844 : : }
845 [ + + ]: 28348 : else if (costcmp == COSTS_EQUAL)
846 : : {
847 [ + + ]: 27927 : if (keyscmp == PATHKEYS_BETTER1)
848 : 32 : remove_old = true;
849 [ + + ]: 27895 : else if (keyscmp == PATHKEYS_BETTER2)
850 : 1087 : accept_new = false;
851 [ + + ]: 26808 : else if (compare_path_costs_fuzzily(new_path, old_path,
852 : : 1.0000000001) == COSTS_BETTER1)
853 : 164 : remove_old = true;
854 : : else
855 : 26644 : accept_new = false;
856 : : }
857 : : }
858 : :
859 : : /*
860 : : * Remove current element from partial_pathlist if dominated by new.
861 : : */
862 [ + + ]: 192907 : if (remove_old)
863 : : {
864 : 27654 : parent_rel->partial_pathlist =
865 : 27654 : foreach_delete_current(parent_rel->partial_pathlist, p1);
866 : 27654 : pfree(old_path);
867 : : }
868 : : else
869 : : {
870 : : /*
871 : : * new belongs after this old path if it has more disabled nodes
872 : : * or if it has the same number of nodes but a greater total cost
873 : : */
874 [ + + ]: 165253 : if (new_path->disabled_nodes > old_path->disabled_nodes ||
875 [ + + ]: 162485 : (new_path->disabled_nodes == old_path->disabled_nodes &&
876 [ + + ]: 161520 : new_path->total_cost >= old_path->total_cost))
877 : 123430 : insert_at = foreach_current_index(p1) + 1;
878 : : }
879 : :
880 : : /*
881 : : * If we found an old path that dominates new_path, we can quit
882 : : * scanning the partial_pathlist; we will not add new_path, and we
883 : : * assume new_path cannot dominate any later path.
884 : : */
885 [ + + ]: 192907 : if (!accept_new)
886 : 96470 : break;
887 : : }
888 : :
889 [ + + ]: 241582 : if (accept_new)
890 : : {
891 : : /* Accept the new path: insert it at proper place */
892 : 145112 : parent_rel->partial_pathlist =
893 : 145112 : list_insert_nth(parent_rel->partial_pathlist, insert_at, new_path);
894 : : }
895 : : else
896 : : {
897 : : /* Reject and recycle the new path */
898 : 96470 : pfree(new_path);
899 : : }
900 : 241582 : }
901 : :
902 : : /*
903 : : * add_partial_path_precheck
904 : : * Check whether a proposed new partial path could possibly get accepted.
905 : : *
906 : : * Unlike add_path_precheck, we can ignore parameterization, since it doesn't
907 : : * matter for partial paths (see add_partial_path). But we do want to make
908 : : * sure we don't add a partial path if there's already a complete path that
909 : : * dominates it, since in that case the proposed path is surely a loser.
910 : : */
911 : : bool
912 : 342427 : add_partial_path_precheck(RelOptInfo *parent_rel, int disabled_nodes,
913 : : Cost startup_cost, Cost total_cost, List *pathkeys)
914 : : {
915 : 342427 : bool consider_startup = parent_rel->consider_startup;
916 : : ListCell *p1;
917 : :
918 : : /*
919 : : * Our goal here is twofold. First, we want to find out whether this path
920 : : * is clearly inferior to some existing partial path. If so, we want to
921 : : * reject it immediately. Second, we want to find out whether this path
922 : : * is clearly superior to some existing partial path -- at least, modulo
923 : : * final cost computations. If so, we definitely want to consider it.
924 : : *
925 : : * Unlike add_path(), we never try to exit this loop early. This is
926 : : * because we expect partial_pathlist to be very short, and getting a
927 : : * definitive answer at this stage avoids the need to call
928 : : * add_path_precheck.
929 : : */
930 [ + + + + : 428121 : foreach(p1, parent_rel->partial_pathlist)
+ + ]
931 : : {
932 : 349200 : Path *old_path = (Path *) lfirst(p1);
933 : : PathCostComparison costcmp;
934 : : PathKeysComparison keyscmp;
935 : :
936 : : /*
937 : : * First, compare costs and disabled nodes. This logic should be
938 : : * identical to compare_path_costs_fuzzily, except that one of the
939 : : * paths hasn't been created yet, and the fuzz factor is always
940 : : * STD_FUZZ_FACTOR.
941 : : */
942 [ + + ]: 349200 : if (unlikely(old_path->disabled_nodes != disabled_nodes))
943 : : {
944 [ + + ]: 9438 : if (disabled_nodes < old_path->disabled_nodes)
945 : 3905 : costcmp = COSTS_BETTER1;
946 : : else
947 : 5533 : costcmp = COSTS_BETTER2;
948 : : }
949 [ + + ]: 339762 : else if (total_cost > old_path->total_cost * STD_FUZZ_FACTOR)
950 : : {
951 [ + + ]: 206356 : if (consider_startup &&
952 [ + + ]: 338 : old_path->startup_cost > startup_cost * STD_FUZZ_FACTOR)
953 : 248 : costcmp = COSTS_DIFFERENT;
954 : : else
955 : 206108 : costcmp = COSTS_BETTER2;
956 : : }
957 [ + + ]: 133406 : else if (old_path->total_cost > total_cost * STD_FUZZ_FACTOR)
958 : : {
959 [ + + ]: 129714 : if (consider_startup &&
960 [ + + ]: 528 : startup_cost > old_path->startup_cost * STD_FUZZ_FACTOR)
961 : 392 : costcmp = COSTS_DIFFERENT;
962 : : else
963 : 129322 : costcmp = COSTS_BETTER1;
964 : : }
965 [ + + ]: 3692 : else if (startup_cost > old_path->startup_cost * STD_FUZZ_FACTOR)
966 : 2387 : costcmp = COSTS_BETTER2;
967 [ + + ]: 1305 : else if (old_path->startup_cost > startup_cost * STD_FUZZ_FACTOR)
968 : 485 : costcmp = COSTS_BETTER1;
969 : : else
970 : 820 : costcmp = COSTS_EQUAL;
971 : :
972 : : /*
973 : : * If one path wins on startup cost and the other on total cost, we
974 : : * can't say for sure which is better.
975 : : */
976 [ + + ]: 349200 : if (costcmp == COSTS_DIFFERENT)
977 : 640 : continue;
978 : :
979 : : /*
980 : : * If the two paths have different pathkeys, we can't say for sure
981 : : * which is better.
982 : : */
983 : 348560 : keyscmp = compare_pathkeys(pathkeys, old_path->pathkeys);
984 [ + + ]: 348560 : if (keyscmp == PATHKEYS_DIFFERENT)
985 : 152 : continue;
986 : :
987 : : /*
988 : : * If the existing path is cheaper and the pathkeys are equal or
989 : : * worse, the new path is not interesting.
990 : : */
991 [ + + + + ]: 348408 : if (costcmp == COSTS_BETTER2 && keyscmp != PATHKEYS_BETTER1)
992 : 263506 : return false;
993 : :
994 : : /*
995 : : * If the new path is cheaper and the pathkeys are equal or better, it
996 : : * is definitely interesting.
997 : : */
998 [ + + + + ]: 175169 : if (costcmp == COSTS_BETTER1 && keyscmp != PATHKEYS_BETTER2)
999 : 90267 : return true;
1000 : : }
1001 : :
1002 : : /*
1003 : : * This path is neither clearly inferior to an existing partial path nor
1004 : : * clearly good enough that it might replace one. Compare it to
1005 : : * non-parallel plans. If it loses even before accounting for the cost of
1006 : : * the Gather node, we should definitely reject it.
1007 : : */
1008 [ + + ]: 78921 : if (!add_path_precheck(parent_rel, disabled_nodes, startup_cost,
1009 : : total_cost, pathkeys, NULL))
1010 : 1758 : return false;
1011 : :
1012 : 77163 : return true;
1013 : : }
1014 : :
1015 : :
1016 : : /*****************************************************************************
1017 : : * PATH NODE CREATION ROUTINES
1018 : : *****************************************************************************/
1019 : :
1020 : : /*
1021 : : * create_seqscan_path
1022 : : * Creates a path corresponding to a sequential scan, returning the
1023 : : * pathnode.
1024 : : */
1025 : : Path *
1026 : 340670 : create_seqscan_path(PlannerInfo *root, RelOptInfo *rel,
1027 : : Relids required_outer, int parallel_workers)
1028 : : {
1029 : 340670 : Path *pathnode = makeNode(Path);
1030 : :
1031 : 340670 : pathnode->pathtype = T_SeqScan;
1032 : 340670 : pathnode->parent = rel;
1033 : 340670 : pathnode->pathtarget = rel->reltarget;
1034 : 340670 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
1035 : : required_outer);
1036 : 340670 : pathnode->parallel_aware = (parallel_workers > 0);
1037 : 340670 : pathnode->parallel_safe = rel->consider_parallel;
1038 : 340670 : pathnode->parallel_workers = parallel_workers;
1039 : 340670 : pathnode->pathkeys = NIL; /* seqscan has unordered result */
1040 : :
1041 : 340670 : cost_seqscan(pathnode, root, rel, pathnode->param_info);
1042 : :
1043 : 340670 : return pathnode;
1044 : : }
1045 : :
1046 : : /*
1047 : : * create_samplescan_path
1048 : : * Creates a path node for a sampled table scan.
1049 : : */
1050 : : Path *
1051 : 247 : create_samplescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
1052 : : {
1053 : 247 : Path *pathnode = makeNode(Path);
1054 : :
1055 : 247 : pathnode->pathtype = T_SampleScan;
1056 : 247 : pathnode->parent = rel;
1057 : 247 : pathnode->pathtarget = rel->reltarget;
1058 : 247 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
1059 : : required_outer);
1060 : 247 : pathnode->parallel_aware = false;
1061 : 247 : pathnode->parallel_safe = rel->consider_parallel;
1062 : 247 : pathnode->parallel_workers = 0;
1063 : 247 : pathnode->pathkeys = NIL; /* samplescan has unordered result */
1064 : :
1065 : 247 : cost_samplescan(pathnode, root, rel, pathnode->param_info);
1066 : :
1067 : 247 : return pathnode;
1068 : : }
1069 : :
1070 : : /*
1071 : : * create_index_path
1072 : : * Creates a path node for an index scan.
1073 : : *
1074 : : * 'index' is a usable index.
1075 : : * 'indexclauses' is a list of IndexClause nodes representing clauses
1076 : : * to be enforced as qual conditions in the scan.
1077 : : * 'indexorderbys' is a list of bare expressions (no RestrictInfos)
1078 : : * to be used as index ordering operators in the scan.
1079 : : * 'indexorderbycols' is an integer list of index column numbers (zero based)
1080 : : * the ordering operators can be used with.
1081 : : * 'pathkeys' describes the ordering of the path.
1082 : : * 'indexscandir' is either ForwardScanDirection or BackwardScanDirection.
1083 : : * 'indexonly' is true if an index-only scan is wanted.
1084 : : * 'required_outer' is the set of outer relids for a parameterized path.
1085 : : * 'loop_count' is the number of repetitions of the indexscan to factor into
1086 : : * estimates of caching behavior.
1087 : : * 'partial_path' is true if constructing a parallel index scan path.
1088 : : *
1089 : : * Returns the new path node.
1090 : : */
1091 : : IndexPath *
1092 : 663813 : create_index_path(PlannerInfo *root,
1093 : : IndexOptInfo *index,
1094 : : List *indexclauses,
1095 : : List *indexorderbys,
1096 : : List *indexorderbycols,
1097 : : List *pathkeys,
1098 : : ScanDirection indexscandir,
1099 : : bool indexonly,
1100 : : Relids required_outer,
1101 : : double loop_count,
1102 : : bool partial_path)
1103 : : {
1104 : 663813 : IndexPath *pathnode = makeNode(IndexPath);
1105 : 663813 : RelOptInfo *rel = index->rel;
1106 : :
1107 [ + + ]: 663813 : pathnode->path.pathtype = indexonly ? T_IndexOnlyScan : T_IndexScan;
1108 : 663813 : pathnode->path.parent = rel;
1109 : 663813 : pathnode->path.pathtarget = rel->reltarget;
1110 : 663813 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1111 : : required_outer);
1112 : 663813 : pathnode->path.parallel_aware = false;
1113 : 663813 : pathnode->path.parallel_safe = rel->consider_parallel;
1114 : 663813 : pathnode->path.parallel_workers = 0;
1115 : 663813 : pathnode->path.pathkeys = pathkeys;
1116 : :
1117 : 663813 : pathnode->indexinfo = index;
1118 : 663813 : pathnode->indexclauses = indexclauses;
1119 : 663813 : pathnode->indexorderbys = indexorderbys;
1120 : 663813 : pathnode->indexorderbycols = indexorderbycols;
1121 : 663813 : pathnode->indexscandir = indexscandir;
1122 : :
1123 : 663813 : cost_index(pathnode, root, loop_count, partial_path);
1124 : :
1125 : : /*
1126 : : * cost_index will set disabled_nodes to 1 if this rel is not allowed to
1127 : : * use index scans in general, but it doesn't have the IndexOptInfo to
1128 : : * know whether this specific index has been disabled.
1129 : : */
1130 [ + + ]: 663813 : if (index->disabled)
1131 : 8391 : pathnode->path.disabled_nodes = 1;
1132 : :
1133 : 663813 : return pathnode;
1134 : : }
1135 : :
1136 : : /*
1137 : : * create_bitmap_heap_path
1138 : : * Creates a path node for a bitmap scan.
1139 : : *
1140 : : * 'bitmapqual' is a tree of IndexPath, BitmapAndPath, and BitmapOrPath nodes.
1141 : : * 'required_outer' is the set of outer relids for a parameterized path.
1142 : : * 'loop_count' is the number of repetitions of the indexscan to factor into
1143 : : * estimates of caching behavior.
1144 : : *
1145 : : * loop_count should match the value used when creating the component
1146 : : * IndexPaths.
1147 : : */
1148 : : BitmapHeapPath *
1149 : 283853 : create_bitmap_heap_path(PlannerInfo *root,
1150 : : RelOptInfo *rel,
1151 : : Path *bitmapqual,
1152 : : Relids required_outer,
1153 : : double loop_count,
1154 : : int parallel_degree)
1155 : : {
1156 : 283853 : BitmapHeapPath *pathnode = makeNode(BitmapHeapPath);
1157 : :
1158 : 283853 : pathnode->path.pathtype = T_BitmapHeapScan;
1159 : 283853 : pathnode->path.parent = rel;
1160 : 283853 : pathnode->path.pathtarget = rel->reltarget;
1161 : 283853 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1162 : : required_outer);
1163 : 283853 : pathnode->path.parallel_aware = (parallel_degree > 0);
1164 : 283853 : pathnode->path.parallel_safe = rel->consider_parallel;
1165 : 283853 : pathnode->path.parallel_workers = parallel_degree;
1166 : 283853 : pathnode->path.pathkeys = NIL; /* always unordered */
1167 : :
1168 : 283853 : pathnode->bitmapqual = bitmapqual;
1169 : :
1170 : 283853 : cost_bitmap_heap_scan(&pathnode->path, root, rel,
1171 : : pathnode->path.param_info,
1172 : : bitmapqual, loop_count);
1173 : :
1174 : 283853 : return pathnode;
1175 : : }
1176 : :
1177 : : /*
1178 : : * create_bitmap_and_path
1179 : : * Creates a path node representing a BitmapAnd.
1180 : : */
1181 : : BitmapAndPath *
1182 : 44356 : create_bitmap_and_path(PlannerInfo *root,
1183 : : RelOptInfo *rel,
1184 : : List *bitmapquals)
1185 : : {
1186 : 44356 : BitmapAndPath *pathnode = makeNode(BitmapAndPath);
1187 : 44356 : Relids required_outer = NULL;
1188 : : ListCell *lc;
1189 : :
1190 : 44356 : pathnode->path.pathtype = T_BitmapAnd;
1191 : 44356 : pathnode->path.parent = rel;
1192 : 44356 : pathnode->path.pathtarget = rel->reltarget;
1193 : :
1194 : : /*
1195 : : * Identify the required outer rels as the union of what the child paths
1196 : : * depend on. (Alternatively, we could insist that the caller pass this
1197 : : * in, but it's more convenient and reliable to compute it here.)
1198 : : */
1199 [ + - + + : 133068 : foreach(lc, bitmapquals)
+ + ]
1200 : : {
1201 : 88712 : Path *bitmapqual = (Path *) lfirst(lc);
1202 : :
1203 : 88712 : required_outer = bms_add_members(required_outer,
1204 [ + + ]: 88712 : PATH_REQ_OUTER(bitmapqual));
1205 : : }
1206 : 44356 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1207 : : required_outer);
1208 : :
1209 : : /*
1210 : : * Currently, a BitmapHeapPath, BitmapAndPath, or BitmapOrPath will be
1211 : : * parallel-safe if and only if rel->consider_parallel is set. So, we can
1212 : : * set the flag for this path based only on the relation-level flag,
1213 : : * without actually iterating over the list of children.
1214 : : */
1215 : 44356 : pathnode->path.parallel_aware = false;
1216 : 44356 : pathnode->path.parallel_safe = rel->consider_parallel;
1217 : 44356 : pathnode->path.parallel_workers = 0;
1218 : :
1219 : 44356 : pathnode->path.pathkeys = NIL; /* always unordered */
1220 : :
1221 : 44356 : pathnode->bitmapquals = bitmapquals;
1222 : :
1223 : : /* this sets bitmapselectivity as well as the regular cost fields: */
1224 : 44356 : cost_bitmap_and_node(pathnode, root);
1225 : :
1226 : 44356 : return pathnode;
1227 : : }
1228 : :
1229 : : /*
1230 : : * create_bitmap_or_path
1231 : : * Creates a path node representing a BitmapOr.
1232 : : */
1233 : : BitmapOrPath *
1234 : 1700 : create_bitmap_or_path(PlannerInfo *root,
1235 : : RelOptInfo *rel,
1236 : : List *bitmapquals)
1237 : : {
1238 : 1700 : BitmapOrPath *pathnode = makeNode(BitmapOrPath);
1239 : 1700 : Relids required_outer = NULL;
1240 : : ListCell *lc;
1241 : :
1242 : 1700 : pathnode->path.pathtype = T_BitmapOr;
1243 : 1700 : pathnode->path.parent = rel;
1244 : 1700 : pathnode->path.pathtarget = rel->reltarget;
1245 : :
1246 : : /*
1247 : : * Identify the required outer rels as the union of what the child paths
1248 : : * depend on. (Alternatively, we could insist that the caller pass this
1249 : : * in, but it's more convenient and reliable to compute it here.)
1250 : : */
1251 [ + - + + : 4011 : foreach(lc, bitmapquals)
+ + ]
1252 : : {
1253 : 2311 : Path *bitmapqual = (Path *) lfirst(lc);
1254 : :
1255 : 2311 : required_outer = bms_add_members(required_outer,
1256 [ + + ]: 2311 : PATH_REQ_OUTER(bitmapqual));
1257 : : }
1258 : 1700 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1259 : : required_outer);
1260 : :
1261 : : /*
1262 : : * Currently, a BitmapHeapPath, BitmapAndPath, or BitmapOrPath will be
1263 : : * parallel-safe if and only if rel->consider_parallel is set. So, we can
1264 : : * set the flag for this path based only on the relation-level flag,
1265 : : * without actually iterating over the list of children.
1266 : : */
1267 : 1700 : pathnode->path.parallel_aware = false;
1268 : 1700 : pathnode->path.parallel_safe = rel->consider_parallel;
1269 : 1700 : pathnode->path.parallel_workers = 0;
1270 : :
1271 : 1700 : pathnode->path.pathkeys = NIL; /* always unordered */
1272 : :
1273 : 1700 : pathnode->bitmapquals = bitmapquals;
1274 : :
1275 : : /* this sets bitmapselectivity as well as the regular cost fields: */
1276 : 1700 : cost_bitmap_or_node(pathnode, root);
1277 : :
1278 : 1700 : return pathnode;
1279 : : }
1280 : :
1281 : : /*
1282 : : * create_tidscan_path
1283 : : * Creates a path corresponding to a scan by TID, returning the pathnode.
1284 : : */
1285 : : TidPath *
1286 : 643 : create_tidscan_path(PlannerInfo *root, RelOptInfo *rel, List *tidquals,
1287 : : Relids required_outer)
1288 : : {
1289 : 643 : TidPath *pathnode = makeNode(TidPath);
1290 : :
1291 : 643 : pathnode->path.pathtype = T_TidScan;
1292 : 643 : pathnode->path.parent = rel;
1293 : 643 : pathnode->path.pathtarget = rel->reltarget;
1294 : 643 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1295 : : required_outer);
1296 : 643 : pathnode->path.parallel_aware = false;
1297 : 643 : pathnode->path.parallel_safe = rel->consider_parallel;
1298 : 643 : pathnode->path.parallel_workers = 0;
1299 : 643 : pathnode->path.pathkeys = NIL; /* always unordered */
1300 : :
1301 : 643 : pathnode->tidquals = tidquals;
1302 : :
1303 : 643 : cost_tidscan(&pathnode->path, root, rel, tidquals,
1304 : : pathnode->path.param_info);
1305 : :
1306 : 643 : return pathnode;
1307 : : }
1308 : :
1309 : : /*
1310 : : * create_tidrangescan_path
1311 : : * Creates a path corresponding to a scan by a range of TIDs, returning
1312 : : * the pathnode.
1313 : : */
1314 : : TidRangePath *
1315 : 1703 : create_tidrangescan_path(PlannerInfo *root, RelOptInfo *rel,
1316 : : List *tidrangequals, Relids required_outer,
1317 : : int parallel_workers)
1318 : : {
1319 : 1703 : TidRangePath *pathnode = makeNode(TidRangePath);
1320 : :
1321 : 1703 : pathnode->path.pathtype = T_TidRangeScan;
1322 : 1703 : pathnode->path.parent = rel;
1323 : 1703 : pathnode->path.pathtarget = rel->reltarget;
1324 : 1703 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1325 : : required_outer);
1326 : 1703 : pathnode->path.parallel_aware = (parallel_workers > 0);
1327 : 1703 : pathnode->path.parallel_safe = rel->consider_parallel;
1328 : 1703 : pathnode->path.parallel_workers = parallel_workers;
1329 : 1703 : pathnode->path.pathkeys = NIL; /* always unordered */
1330 : :
1331 : 1703 : pathnode->tidrangequals = tidrangequals;
1332 : :
1333 : 1703 : cost_tidrangescan(&pathnode->path, root, rel, tidrangequals,
1334 : : pathnode->path.param_info);
1335 : :
1336 : 1703 : return pathnode;
1337 : : }
1338 : :
1339 : : /*
1340 : : * create_append_path
1341 : : * Creates a path corresponding to an Append plan, returning the
1342 : : * pathnode.
1343 : : *
1344 : : * Note that we must handle subpaths = NIL, representing a dummy access path.
1345 : : * Also, there are callers that pass root = NULL.
1346 : : *
1347 : : * 'rows', when passed as a non-negative number, will be used to overwrite the
1348 : : * returned path's row estimate. Otherwise, the row estimate is calculated
1349 : : * by totalling the row estimates from the 'subpaths' list.
1350 : : */
1351 : : AppendPath *
1352 : 73167 : create_append_path(PlannerInfo *root,
1353 : : RelOptInfo *rel,
1354 : : AppendPathInput input,
1355 : : List *pathkeys, Relids required_outer,
1356 : : int parallel_workers, bool parallel_aware,
1357 : : double rows)
1358 : : {
1359 : 73167 : AppendPath *pathnode = makeNode(AppendPath);
1360 : : ListCell *l;
1361 : :
1362 : : Assert(!parallel_aware || parallel_workers > 0);
1363 : :
1364 : 73167 : pathnode->child_append_relid_sets = input.child_append_relid_sets;
1365 : 73167 : pathnode->path.pathtype = T_Append;
1366 : 73167 : pathnode->path.parent = rel;
1367 : 73167 : pathnode->path.pathtarget = rel->reltarget;
1368 : :
1369 : : /*
1370 : : * If this is for a baserel (not a join or non-leaf partition), we prefer
1371 : : * to apply get_baserel_parampathinfo to construct a full ParamPathInfo
1372 : : * for the path. This supports building a Memoize path atop this path,
1373 : : * and if this is a partitioned table the info may be useful for run-time
1374 : : * pruning (cf make_partition_pruneinfo()).
1375 : : *
1376 : : * However, if we don't have "root" then that won't work and we fall back
1377 : : * on the simpler get_appendrel_parampathinfo. There's no point in doing
1378 : : * the more expensive thing for a dummy path, either.
1379 : : */
1380 [ + + + + : 73167 : if (rel->reloptkind == RELOPT_BASEREL && root && input.subpaths != NIL)
+ + ]
1381 : 31635 : pathnode->path.param_info = get_baserel_parampathinfo(root,
1382 : : rel,
1383 : : required_outer);
1384 : : else
1385 : 41532 : pathnode->path.param_info = get_appendrel_parampathinfo(rel,
1386 : : required_outer);
1387 : :
1388 : 73167 : pathnode->path.parallel_aware = parallel_aware;
1389 : 73167 : pathnode->path.parallel_safe = rel->consider_parallel;
1390 : 73167 : pathnode->path.parallel_workers = parallel_workers;
1391 : 73167 : pathnode->path.pathkeys = pathkeys;
1392 : :
1393 : : /*
1394 : : * For parallel append, non-partial paths are sorted by descending total
1395 : : * costs. That way, the total time to finish all non-partial paths is
1396 : : * minimized. Also, the partial paths are sorted by descending startup
1397 : : * costs. There may be some paths that require to do startup work by a
1398 : : * single worker. In such case, it's better for workers to choose the
1399 : : * expensive ones first, whereas the leader should choose the cheapest
1400 : : * startup plan.
1401 : : */
1402 [ + + ]: 73167 : if (pathnode->path.parallel_aware)
1403 : : {
1404 : : /*
1405 : : * We mustn't fiddle with the order of subpaths when the Append has
1406 : : * pathkeys. The order they're listed in is critical to keeping the
1407 : : * pathkeys valid.
1408 : : */
1409 : : Assert(pathkeys == NIL);
1410 : :
1411 : 25997 : list_sort(input.subpaths, append_total_cost_compare);
1412 : 25997 : list_sort(input.partial_subpaths, append_startup_cost_compare);
1413 : : }
1414 : 73167 : pathnode->first_partial_path = list_length(input.subpaths);
1415 : 73167 : pathnode->subpaths = list_concat(input.subpaths, input.partial_subpaths);
1416 : :
1417 : : /*
1418 : : * Apply query-wide LIMIT if known and path is for sole base relation.
1419 : : * (Handling this at this low level is a bit klugy.)
1420 : : */
1421 [ + + + + ]: 73167 : if (root != NULL && bms_equal(rel->relids, root->all_query_rels))
1422 : 34821 : pathnode->limit_tuples = root->limit_tuples;
1423 : : else
1424 : 38346 : pathnode->limit_tuples = -1.0;
1425 : :
1426 [ + + + + : 253655 : foreach(l, pathnode->subpaths)
+ + ]
1427 : : {
1428 : 180488 : Path *subpath = (Path *) lfirst(l);
1429 : :
1430 [ + + ]: 327945 : pathnode->path.parallel_safe = pathnode->path.parallel_safe &&
1431 [ + + ]: 327945 : subpath->parallel_safe;
1432 : :
1433 : : /* All child paths must have same parameterization */
1434 : : Assert(bms_equal(PATH_REQ_OUTER(subpath), required_outer));
1435 : : }
1436 : :
1437 : : Assert(!parallel_aware || pathnode->path.parallel_safe);
1438 : :
1439 : : /*
1440 : : * If there's exactly one child path then the output of the Append is
1441 : : * necessarily ordered the same as the child's, so we can inherit the
1442 : : * child's pathkeys if any, overriding whatever the caller might've said.
1443 : : * Furthermore, if the child's parallel awareness matches the Append's,
1444 : : * then the Append is a no-op and will be discarded later (in setrefs.c).
1445 : : * Then we can inherit the child's size, cost and disabled-node count too,
1446 : : * effectively charging zero for the Append. Otherwise, we must do the
1447 : : * normal costsize calculation.
1448 : : */
1449 [ + + ]: 73167 : if (list_length(pathnode->subpaths) == 1)
1450 : : {
1451 : 15350 : Path *child = (Path *) linitial(pathnode->subpaths);
1452 : :
1453 [ + + ]: 15350 : if (child->parallel_aware == parallel_aware)
1454 : : {
1455 : 15000 : pathnode->path.rows = child->rows;
1456 : 15000 : pathnode->path.disabled_nodes = child->disabled_nodes;
1457 : 15000 : pathnode->path.startup_cost = child->startup_cost;
1458 : 15000 : pathnode->path.total_cost = child->total_cost;
1459 : : }
1460 : : else
1461 : 350 : cost_append(pathnode, root);
1462 : : /* Must do this last, else cost_append complains */
1463 : 15350 : pathnode->path.pathkeys = child->pathkeys;
1464 : : }
1465 : : else
1466 : 57817 : cost_append(pathnode, root);
1467 : :
1468 : : /* If the caller provided a row estimate, override the computed value. */
1469 [ + + ]: 73167 : if (rows >= 0)
1470 : 500 : pathnode->path.rows = rows;
1471 : :
1472 : 73167 : return pathnode;
1473 : : }
1474 : :
1475 : : /*
1476 : : * append_total_cost_compare
1477 : : * list_sort comparator for sorting append child paths
1478 : : * by total_cost descending
1479 : : *
1480 : : * For equal total costs, we fall back to comparing startup costs; if those
1481 : : * are equal too, break ties using bms_compare on the paths' relids.
1482 : : * (This is to avoid getting unpredictable results from list_sort.)
1483 : : */
1484 : : static int
1485 : 13754 : append_total_cost_compare(const ListCell *a, const ListCell *b)
1486 : : {
1487 : 13754 : Path *path1 = (Path *) lfirst(a);
1488 : 13754 : Path *path2 = (Path *) lfirst(b);
1489 : : int cmp;
1490 : :
1491 : 13754 : cmp = compare_path_costs(path1, path2, TOTAL_COST);
1492 [ + + ]: 13754 : if (cmp != 0)
1493 : 12466 : return -cmp;
1494 : 1288 : return bms_compare(path1->parent->relids, path2->parent->relids);
1495 : : }
1496 : :
1497 : : /*
1498 : : * append_startup_cost_compare
1499 : : * list_sort comparator for sorting append child paths
1500 : : * by startup_cost descending
1501 : : *
1502 : : * For equal startup costs, we fall back to comparing total costs; if those
1503 : : * are equal too, break ties using bms_compare on the paths' relids.
1504 : : * (This is to avoid getting unpredictable results from list_sort.)
1505 : : */
1506 : : static int
1507 : 38197 : append_startup_cost_compare(const ListCell *a, const ListCell *b)
1508 : : {
1509 : 38197 : Path *path1 = (Path *) lfirst(a);
1510 : 38197 : Path *path2 = (Path *) lfirst(b);
1511 : : int cmp;
1512 : :
1513 : 38197 : cmp = compare_path_costs(path1, path2, STARTUP_COST);
1514 [ + + ]: 38197 : if (cmp != 0)
1515 : 18133 : return -cmp;
1516 : 20064 : return bms_compare(path1->parent->relids, path2->parent->relids);
1517 : : }
1518 : :
1519 : : /*
1520 : : * create_merge_append_path
1521 : : * Creates a path corresponding to a MergeAppend plan, returning the
1522 : : * pathnode.
1523 : : */
1524 : : MergeAppendPath *
1525 : 7487 : create_merge_append_path(PlannerInfo *root,
1526 : : RelOptInfo *rel,
1527 : : List *subpaths,
1528 : : List *child_append_relid_sets,
1529 : : List *pathkeys,
1530 : : Relids required_outer)
1531 : : {
1532 : 7487 : MergeAppendPath *pathnode = makeNode(MergeAppendPath);
1533 : : int input_disabled_nodes;
1534 : : Cost input_startup_cost;
1535 : : Cost input_total_cost;
1536 : : ListCell *l;
1537 : :
1538 : : /*
1539 : : * We don't currently support parameterized MergeAppend paths, as
1540 : : * explained in the comments for generate_orderedappend_paths.
1541 : : */
1542 : : Assert(bms_is_empty(rel->lateral_relids) && bms_is_empty(required_outer));
1543 : :
1544 : 7487 : pathnode->child_append_relid_sets = child_append_relid_sets;
1545 : 7487 : pathnode->path.pathtype = T_MergeAppend;
1546 : 7487 : pathnode->path.parent = rel;
1547 : 7487 : pathnode->path.pathtarget = rel->reltarget;
1548 : 7487 : pathnode->path.param_info = NULL;
1549 : 7487 : pathnode->path.parallel_aware = false;
1550 : 7487 : pathnode->path.parallel_safe = rel->consider_parallel;
1551 : 7487 : pathnode->path.parallel_workers = 0;
1552 : 7487 : pathnode->path.pathkeys = pathkeys;
1553 : 7487 : pathnode->subpaths = subpaths;
1554 : :
1555 : : /*
1556 : : * Apply query-wide LIMIT if known and path is for sole base relation.
1557 : : * (Handling this at this low level is a bit klugy.)
1558 : : */
1559 [ + + ]: 7487 : if (bms_equal(rel->relids, root->all_query_rels))
1560 : 3463 : pathnode->limit_tuples = root->limit_tuples;
1561 : : else
1562 : 4024 : pathnode->limit_tuples = -1.0;
1563 : :
1564 : : /*
1565 : : * Add up the sizes and costs of the input paths.
1566 : : */
1567 : 7487 : pathnode->path.rows = 0;
1568 : 7487 : input_disabled_nodes = 0;
1569 : 7487 : input_startup_cost = 0;
1570 : 7487 : input_total_cost = 0;
1571 [ + - + + : 27154 : foreach(l, subpaths)
+ + ]
1572 : : {
1573 : 19667 : Path *subpath = (Path *) lfirst(l);
1574 : : int presorted_keys;
1575 : : Path sort_path; /* dummy for result of
1576 : : * cost_sort/cost_incremental_sort */
1577 : :
1578 : : /* All child paths should be unparameterized */
1579 : : Assert(bms_is_empty(PATH_REQ_OUTER(subpath)));
1580 : :
1581 : 19667 : pathnode->path.rows += subpath->rows;
1582 [ + + ]: 37200 : pathnode->path.parallel_safe = pathnode->path.parallel_safe &&
1583 [ + + ]: 17533 : subpath->parallel_safe;
1584 : :
1585 [ + + ]: 19667 : if (!pathkeys_count_contained_in(pathkeys, subpath->pathkeys,
1586 : : &presorted_keys))
1587 : : {
1588 : : /*
1589 : : * We'll need to insert a Sort node, so include costs for that. We
1590 : : * choose to use incremental sort if it is enabled and there are
1591 : : * presorted keys; otherwise we use full sort.
1592 : : *
1593 : : * We can use the parent's LIMIT if any, since we certainly won't
1594 : : * pull more than that many tuples from any child.
1595 : : */
1596 [ + - + + ]: 494 : if (enable_incremental_sort && presorted_keys > 0)
1597 : : {
1598 : 15 : cost_incremental_sort(&sort_path,
1599 : : root,
1600 : : pathkeys,
1601 : : presorted_keys,
1602 : : subpath->disabled_nodes,
1603 : : subpath->startup_cost,
1604 : : subpath->total_cost,
1605 : : subpath->rows,
1606 : 15 : subpath->pathtarget->width,
1607 : : 0.0,
1608 : : work_mem,
1609 : : pathnode->limit_tuples,
1610 : : NULL);
1611 : : }
1612 : : else
1613 : : {
1614 : 479 : cost_sort(&sort_path,
1615 : : root,
1616 : : pathkeys,
1617 : : subpath->disabled_nodes,
1618 : : subpath->total_cost,
1619 : : subpath->rows,
1620 : 479 : subpath->pathtarget->width,
1621 : : 0.0,
1622 : : work_mem,
1623 : : pathnode->limit_tuples);
1624 : : }
1625 : :
1626 : 494 : subpath = &sort_path;
1627 : : }
1628 : :
1629 : 19667 : input_disabled_nodes += subpath->disabled_nodes;
1630 : 19667 : input_startup_cost += subpath->startup_cost;
1631 : 19667 : input_total_cost += subpath->total_cost;
1632 : : }
1633 : :
1634 : : /*
1635 : : * Now we can compute total costs of the MergeAppend. If there's exactly
1636 : : * one child path and its parallel awareness matches that of the
1637 : : * MergeAppend, then the MergeAppend is a no-op and will be discarded
1638 : : * later (in setrefs.c); otherwise we do the normal cost calculation.
1639 : : */
1640 [ + + ]: 7487 : if (list_length(subpaths) == 1 &&
1641 : 95 : ((Path *) linitial(subpaths))->parallel_aware ==
1642 [ + - ]: 95 : pathnode->path.parallel_aware)
1643 : : {
1644 : 95 : pathnode->path.disabled_nodes = input_disabled_nodes;
1645 : 95 : pathnode->path.startup_cost = input_startup_cost;
1646 : 95 : pathnode->path.total_cost = input_total_cost;
1647 : : }
1648 : : else
1649 : 7392 : cost_merge_append(&pathnode->path, root,
1650 : : pathkeys, list_length(subpaths),
1651 : : input_disabled_nodes,
1652 : : input_startup_cost, input_total_cost,
1653 : : pathnode->path.rows);
1654 : :
1655 : 7487 : return pathnode;
1656 : : }
1657 : :
1658 : : /*
1659 : : * create_group_result_path
1660 : : * Creates a path representing a Result-and-nothing-else plan.
1661 : : *
1662 : : * This is only used for degenerate grouping cases, in which we know we
1663 : : * need to produce one result row, possibly filtered by a HAVING qual.
1664 : : */
1665 : : GroupResultPath *
1666 : 142159 : create_group_result_path(PlannerInfo *root, RelOptInfo *rel,
1667 : : PathTarget *target, List *havingqual)
1668 : : {
1669 : 142159 : GroupResultPath *pathnode = makeNode(GroupResultPath);
1670 : :
1671 : 142159 : pathnode->path.pathtype = T_Result;
1672 : 142159 : pathnode->path.parent = rel;
1673 : 142159 : pathnode->path.pathtarget = target;
1674 : 142159 : pathnode->path.param_info = NULL; /* there are no other rels... */
1675 : 142159 : pathnode->path.parallel_aware = false;
1676 : 142159 : pathnode->path.parallel_safe = rel->consider_parallel;
1677 : 142159 : pathnode->path.parallel_workers = 0;
1678 : 142159 : pathnode->path.pathkeys = NIL;
1679 : 142159 : pathnode->quals = havingqual;
1680 : :
1681 : : /*
1682 : : * We can't quite use cost_resultscan() because the quals we want to
1683 : : * account for are not baserestrict quals of the rel. Might as well just
1684 : : * hack it here.
1685 : : */
1686 : 142159 : pathnode->path.rows = 1;
1687 : 142159 : pathnode->path.startup_cost = target->cost.startup;
1688 : 142159 : pathnode->path.total_cost = target->cost.startup +
1689 : 142159 : cpu_tuple_cost + target->cost.per_tuple;
1690 : :
1691 : : /*
1692 : : * Add cost of qual, if any --- but we ignore its selectivity, since our
1693 : : * rowcount estimate should be 1 no matter what the qual is.
1694 : : */
1695 [ + + ]: 142159 : if (havingqual)
1696 : : {
1697 : : QualCost qual_cost;
1698 : :
1699 : 514 : cost_qual_eval(&qual_cost, havingqual, root);
1700 : : /* havingqual is evaluated once at startup */
1701 : 514 : pathnode->path.startup_cost += qual_cost.startup + qual_cost.per_tuple;
1702 : 514 : pathnode->path.total_cost += qual_cost.startup + qual_cost.per_tuple;
1703 : : }
1704 : :
1705 : 142159 : return pathnode;
1706 : : }
1707 : :
1708 : : /*
1709 : : * create_material_path
1710 : : * Creates a path corresponding to a Material plan, returning the
1711 : : * pathnode.
1712 : : */
1713 : : MaterialPath *
1714 : 496236 : create_material_path(RelOptInfo *rel, Path *subpath, bool enabled)
1715 : : {
1716 : 496236 : MaterialPath *pathnode = makeNode(MaterialPath);
1717 : :
1718 : : Assert(subpath->parent == rel);
1719 : :
1720 : 496236 : pathnode->path.pathtype = T_Material;
1721 : 496236 : pathnode->path.parent = rel;
1722 : 496236 : pathnode->path.pathtarget = rel->reltarget;
1723 : 496236 : pathnode->path.param_info = subpath->param_info;
1724 : 496236 : pathnode->path.parallel_aware = false;
1725 [ + + ]: 950693 : pathnode->path.parallel_safe = rel->consider_parallel &&
1726 [ + + ]: 454457 : subpath->parallel_safe;
1727 : 496236 : pathnode->path.parallel_workers = subpath->parallel_workers;
1728 : 496236 : pathnode->path.pathkeys = subpath->pathkeys;
1729 : :
1730 : 496236 : pathnode->subpath = subpath;
1731 : :
1732 : 496236 : cost_material(&pathnode->path,
1733 : : enabled,
1734 : : subpath->disabled_nodes,
1735 : : subpath->startup_cost,
1736 : : subpath->total_cost,
1737 : : subpath->rows,
1738 : 496236 : subpath->pathtarget->width);
1739 : :
1740 : 496236 : return pathnode;
1741 : : }
1742 : :
1743 : : /*
1744 : : * create_memoize_path
1745 : : * Creates a path corresponding to a Memoize plan, returning the pathnode.
1746 : : */
1747 : : MemoizePath *
1748 : 206804 : create_memoize_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath,
1749 : : List *param_exprs, List *hash_operators,
1750 : : bool singlerow, bool binary_mode, Cardinality est_calls)
1751 : : {
1752 : 206804 : MemoizePath *pathnode = makeNode(MemoizePath);
1753 : :
1754 : : Assert(subpath->parent == rel);
1755 : :
1756 : 206804 : pathnode->path.pathtype = T_Memoize;
1757 : 206804 : pathnode->path.parent = rel;
1758 : 206804 : pathnode->path.pathtarget = rel->reltarget;
1759 : 206804 : pathnode->path.param_info = subpath->param_info;
1760 : 206804 : pathnode->path.parallel_aware = false;
1761 [ + + ]: 403389 : pathnode->path.parallel_safe = rel->consider_parallel &&
1762 [ + + ]: 196585 : subpath->parallel_safe;
1763 : 206804 : pathnode->path.parallel_workers = subpath->parallel_workers;
1764 : 206804 : pathnode->path.pathkeys = subpath->pathkeys;
1765 : :
1766 : 206804 : pathnode->subpath = subpath;
1767 : 206804 : pathnode->hash_operators = hash_operators;
1768 : 206804 : pathnode->param_exprs = param_exprs;
1769 : 206804 : pathnode->singlerow = singlerow;
1770 : 206804 : pathnode->binary_mode = binary_mode;
1771 : :
1772 : : /*
1773 : : * For now we set est_entries to 0. cost_memoize_rescan() does all the
1774 : : * hard work to determine how many cache entries there are likely to be,
1775 : : * so it seems best to leave it up to that function to fill this field in.
1776 : : * If left at 0, the executor will make a guess at a good value.
1777 : : */
1778 : 206804 : pathnode->est_entries = 0;
1779 : :
1780 : 206804 : pathnode->est_calls = clamp_row_est(est_calls);
1781 : :
1782 : : /* These will also be set later in cost_memoize_rescan() */
1783 : 206804 : pathnode->est_unique_keys = 0.0;
1784 : 206804 : pathnode->est_hit_ratio = 0.0;
1785 : :
1786 : : /*
1787 : : * We should not be asked to generate this path type when memoization is
1788 : : * disabled, so set our count of disabled nodes equal to the subpath's
1789 : : * count.
1790 : : *
1791 : : * It would be nice to also Assert that memoization is enabled, but the
1792 : : * value of enable_memoize is not controlling: what we would need to check
1793 : : * is that the JoinPathExtraData's pgs_mask included PGS_NESTLOOP_MEMOIZE.
1794 : : */
1795 : 206804 : pathnode->path.disabled_nodes = subpath->disabled_nodes;
1796 : :
1797 : : /*
1798 : : * Add a small additional charge for caching the first entry. All the
1799 : : * harder calculations for rescans are performed in cost_memoize_rescan().
1800 : : */
1801 : 206804 : pathnode->path.startup_cost = subpath->startup_cost + cpu_tuple_cost;
1802 : 206804 : pathnode->path.total_cost = subpath->total_cost + cpu_tuple_cost;
1803 : 206804 : pathnode->path.rows = subpath->rows;
1804 : :
1805 : 206804 : return pathnode;
1806 : : }
1807 : :
1808 : : /*
1809 : : * create_gather_merge_path
1810 : : *
1811 : : * Creates a path corresponding to a gather merge scan, returning
1812 : : * the pathnode.
1813 : : */
1814 : : GatherMergePath *
1815 : 15507 : create_gather_merge_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath,
1816 : : PathTarget *target, List *pathkeys,
1817 : : Relids required_outer, double *rows)
1818 : : {
1819 : 15507 : GatherMergePath *pathnode = makeNode(GatherMergePath);
1820 : 15507 : int input_disabled_nodes = 0;
1821 : 15507 : Cost input_startup_cost = 0;
1822 : 15507 : Cost input_total_cost = 0;
1823 : :
1824 : : Assert(subpath->parallel_safe);
1825 : : Assert(pathkeys);
1826 : :
1827 : : /*
1828 : : * The subpath should guarantee that it is adequately ordered either by
1829 : : * adding an explicit sort node or by using presorted input. We cannot
1830 : : * add an explicit Sort node for the subpath in createplan.c on additional
1831 : : * pathkeys, because we can't guarantee the sort would be safe. For
1832 : : * example, expressions may be volatile or otherwise parallel unsafe.
1833 : : */
1834 [ - + ]: 15507 : if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
1835 [ # # ]: 0 : elog(ERROR, "gather merge input not sufficiently sorted");
1836 : :
1837 : 15507 : pathnode->path.pathtype = T_GatherMerge;
1838 : 15507 : pathnode->path.parent = rel;
1839 : 15507 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1840 : : required_outer);
1841 : 15507 : pathnode->path.parallel_aware = false;
1842 : :
1843 : 15507 : pathnode->subpath = subpath;
1844 : 15507 : pathnode->num_workers = subpath->parallel_workers;
1845 : 15507 : pathnode->path.pathkeys = pathkeys;
1846 [ - + ]: 15507 : pathnode->path.pathtarget = target ? target : rel->reltarget;
1847 : :
1848 : 15507 : input_disabled_nodes += subpath->disabled_nodes;
1849 : 15507 : input_startup_cost += subpath->startup_cost;
1850 : 15507 : input_total_cost += subpath->total_cost;
1851 : :
1852 : 15507 : cost_gather_merge(pathnode, root, rel, pathnode->path.param_info,
1853 : : input_disabled_nodes, input_startup_cost,
1854 : : input_total_cost, rows);
1855 : :
1856 : 15507 : return pathnode;
1857 : : }
1858 : :
1859 : : /*
1860 : : * create_gather_path
1861 : : * Creates a path corresponding to a gather scan, returning the
1862 : : * pathnode.
1863 : : *
1864 : : * 'rows' may optionally be set to override row estimates from other sources.
1865 : : */
1866 : : GatherPath *
1867 : 21625 : create_gather_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath,
1868 : : PathTarget *target, Relids required_outer, double *rows)
1869 : : {
1870 : 21625 : GatherPath *pathnode = makeNode(GatherPath);
1871 : :
1872 : : Assert(subpath->parallel_safe);
1873 : :
1874 : 21625 : pathnode->path.pathtype = T_Gather;
1875 : 21625 : pathnode->path.parent = rel;
1876 : 21625 : pathnode->path.pathtarget = target;
1877 : 21625 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1878 : : required_outer);
1879 : 21625 : pathnode->path.parallel_aware = false;
1880 : 21625 : pathnode->path.parallel_safe = false;
1881 : 21625 : pathnode->path.parallel_workers = 0;
1882 : 21625 : pathnode->path.pathkeys = NIL; /* Gather has unordered result */
1883 : :
1884 : 21625 : pathnode->subpath = subpath;
1885 : 21625 : pathnode->num_workers = subpath->parallel_workers;
1886 : 21625 : pathnode->single_copy = false;
1887 : :
1888 [ - + ]: 21625 : if (pathnode->num_workers == 0)
1889 : : {
1890 : 0 : pathnode->path.pathkeys = subpath->pathkeys;
1891 : 0 : pathnode->num_workers = 1;
1892 : 0 : pathnode->single_copy = true;
1893 : : }
1894 : :
1895 : 21625 : cost_gather(pathnode, root, rel, pathnode->path.param_info, rows);
1896 : :
1897 : 21625 : return pathnode;
1898 : : }
1899 : :
1900 : : /*
1901 : : * create_subqueryscan_path
1902 : : * Creates a path corresponding to a scan of a subquery,
1903 : : * returning the pathnode.
1904 : : *
1905 : : * Caller must pass trivial_pathtarget = true if it believes rel->reltarget to
1906 : : * be trivial, ie just a fetch of all the subquery output columns in order.
1907 : : * While we could determine that here, the caller can usually do it more
1908 : : * efficiently (or at least amortize it over multiple calls).
1909 : : */
1910 : : SubqueryScanPath *
1911 : 48994 : create_subqueryscan_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath,
1912 : : bool trivial_pathtarget,
1913 : : List *pathkeys, Relids required_outer)
1914 : : {
1915 : 48994 : SubqueryScanPath *pathnode = makeNode(SubqueryScanPath);
1916 : :
1917 : 48994 : pathnode->path.pathtype = T_SubqueryScan;
1918 : 48994 : pathnode->path.parent = rel;
1919 : 48994 : pathnode->path.pathtarget = rel->reltarget;
1920 : 48994 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1921 : : required_outer);
1922 : 48994 : pathnode->path.parallel_aware = false;
1923 [ + + ]: 82170 : pathnode->path.parallel_safe = rel->consider_parallel &&
1924 [ + + ]: 33176 : subpath->parallel_safe;
1925 : 48994 : pathnode->path.parallel_workers = subpath->parallel_workers;
1926 : 48994 : pathnode->path.pathkeys = pathkeys;
1927 : 48994 : pathnode->subpath = subpath;
1928 : :
1929 : 48994 : cost_subqueryscan(pathnode, root, rel, pathnode->path.param_info,
1930 : : trivial_pathtarget);
1931 : :
1932 : 48994 : return pathnode;
1933 : : }
1934 : :
1935 : : /*
1936 : : * create_functionscan_path
1937 : : * Creates a path corresponding to a sequential scan of a function,
1938 : : * returning the pathnode.
1939 : : */
1940 : : Path *
1941 : 35117 : create_functionscan_path(PlannerInfo *root, RelOptInfo *rel,
1942 : : List *pathkeys, Relids required_outer)
1943 : : {
1944 : 35117 : Path *pathnode = makeNode(Path);
1945 : :
1946 : 35117 : pathnode->pathtype = T_FunctionScan;
1947 : 35117 : pathnode->parent = rel;
1948 : 35117 : pathnode->pathtarget = rel->reltarget;
1949 : 35117 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
1950 : : required_outer);
1951 : 35117 : pathnode->parallel_aware = false;
1952 : 35117 : pathnode->parallel_safe = rel->consider_parallel;
1953 : 35117 : pathnode->parallel_workers = 0;
1954 : 35117 : pathnode->pathkeys = pathkeys;
1955 : :
1956 : 35117 : cost_functionscan(pathnode, root, rel, pathnode->param_info);
1957 : :
1958 : 35117 : return pathnode;
1959 : : }
1960 : :
1961 : : /*
1962 : : * create_tablefuncscan_path
1963 : : * Creates a path corresponding to a sequential scan of a table function,
1964 : : * returning the pathnode.
1965 : : */
1966 : : Path *
1967 : 604 : create_tablefuncscan_path(PlannerInfo *root, RelOptInfo *rel,
1968 : : Relids required_outer)
1969 : : {
1970 : 604 : Path *pathnode = makeNode(Path);
1971 : :
1972 : 604 : pathnode->pathtype = T_TableFuncScan;
1973 : 604 : pathnode->parent = rel;
1974 : 604 : pathnode->pathtarget = rel->reltarget;
1975 : 604 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
1976 : : required_outer);
1977 : 604 : pathnode->parallel_aware = false;
1978 : 604 : pathnode->parallel_safe = rel->consider_parallel;
1979 : 604 : pathnode->parallel_workers = 0;
1980 : 604 : pathnode->pathkeys = NIL; /* result is always unordered */
1981 : :
1982 : 604 : cost_tablefuncscan(pathnode, root, rel, pathnode->param_info);
1983 : :
1984 : 604 : return pathnode;
1985 : : }
1986 : :
1987 : : /*
1988 : : * create_valuesscan_path
1989 : : * Creates a path corresponding to a scan of a VALUES list,
1990 : : * returning the pathnode.
1991 : : */
1992 : : Path *
1993 : 6928 : create_valuesscan_path(PlannerInfo *root, RelOptInfo *rel,
1994 : : Relids required_outer)
1995 : : {
1996 : 6928 : Path *pathnode = makeNode(Path);
1997 : :
1998 : 6928 : pathnode->pathtype = T_ValuesScan;
1999 : 6928 : pathnode->parent = rel;
2000 : 6928 : pathnode->pathtarget = rel->reltarget;
2001 : 6928 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
2002 : : required_outer);
2003 : 6928 : pathnode->parallel_aware = false;
2004 : 6928 : pathnode->parallel_safe = rel->consider_parallel;
2005 : 6928 : pathnode->parallel_workers = 0;
2006 : 6928 : pathnode->pathkeys = NIL; /* result is always unordered */
2007 : :
2008 : 6928 : cost_valuesscan(pathnode, root, rel, pathnode->param_info);
2009 : :
2010 : 6928 : return pathnode;
2011 : : }
2012 : :
2013 : : /*
2014 : : * create_ctescan_path
2015 : : * Creates a path corresponding to a scan of a non-self-reference CTE,
2016 : : * returning the pathnode.
2017 : : */
2018 : : Path *
2019 : 2933 : create_ctescan_path(PlannerInfo *root, RelOptInfo *rel,
2020 : : List *pathkeys, Relids required_outer)
2021 : : {
2022 : 2933 : Path *pathnode = makeNode(Path);
2023 : :
2024 : 2933 : pathnode->pathtype = T_CteScan;
2025 : 2933 : pathnode->parent = rel;
2026 : 2933 : pathnode->pathtarget = rel->reltarget;
2027 : 2933 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
2028 : : required_outer);
2029 : 2933 : pathnode->parallel_aware = false;
2030 : 2933 : pathnode->parallel_safe = rel->consider_parallel;
2031 : 2933 : pathnode->parallel_workers = 0;
2032 : 2933 : pathnode->pathkeys = pathkeys;
2033 : :
2034 : 2933 : cost_ctescan(pathnode, root, rel, pathnode->param_info);
2035 : :
2036 : 2933 : return pathnode;
2037 : : }
2038 : :
2039 : : /*
2040 : : * create_namedtuplestorescan_path
2041 : : * Creates a path corresponding to a scan of a named tuplestore, returning
2042 : : * the pathnode.
2043 : : */
2044 : : Path *
2045 : 435 : create_namedtuplestorescan_path(PlannerInfo *root, RelOptInfo *rel,
2046 : : Relids required_outer)
2047 : : {
2048 : 435 : Path *pathnode = makeNode(Path);
2049 : :
2050 : 435 : pathnode->pathtype = T_NamedTuplestoreScan;
2051 : 435 : pathnode->parent = rel;
2052 : 435 : pathnode->pathtarget = rel->reltarget;
2053 : 435 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
2054 : : required_outer);
2055 : 435 : pathnode->parallel_aware = false;
2056 : 435 : pathnode->parallel_safe = rel->consider_parallel;
2057 : 435 : pathnode->parallel_workers = 0;
2058 : 435 : pathnode->pathkeys = NIL; /* result is always unordered */
2059 : :
2060 : 435 : cost_namedtuplestorescan(pathnode, root, rel, pathnode->param_info);
2061 : :
2062 : 435 : return pathnode;
2063 : : }
2064 : :
2065 : : /*
2066 : : * create_resultscan_path
2067 : : * Creates a path corresponding to a scan of an RTE_RESULT relation,
2068 : : * returning the pathnode.
2069 : : */
2070 : : Path *
2071 : 3686 : create_resultscan_path(PlannerInfo *root, RelOptInfo *rel,
2072 : : Relids required_outer)
2073 : : {
2074 : 3686 : Path *pathnode = makeNode(Path);
2075 : :
2076 : 3686 : pathnode->pathtype = T_Result;
2077 : 3686 : pathnode->parent = rel;
2078 : 3686 : pathnode->pathtarget = rel->reltarget;
2079 : 3686 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
2080 : : required_outer);
2081 : 3686 : pathnode->parallel_aware = false;
2082 : 3686 : pathnode->parallel_safe = rel->consider_parallel;
2083 : 3686 : pathnode->parallel_workers = 0;
2084 : 3686 : pathnode->pathkeys = NIL; /* result is always unordered */
2085 : :
2086 : 3686 : cost_resultscan(pathnode, root, rel, pathnode->param_info);
2087 : :
2088 : 3686 : return pathnode;
2089 : : }
2090 : :
2091 : : /*
2092 : : * create_worktablescan_path
2093 : : * Creates a path corresponding to a scan of a self-reference CTE,
2094 : : * returning the pathnode.
2095 : : */
2096 : : Path *
2097 : 642 : create_worktablescan_path(PlannerInfo *root, RelOptInfo *rel,
2098 : : Relids required_outer)
2099 : : {
2100 : 642 : Path *pathnode = makeNode(Path);
2101 : :
2102 : 642 : pathnode->pathtype = T_WorkTableScan;
2103 : 642 : pathnode->parent = rel;
2104 : 642 : pathnode->pathtarget = rel->reltarget;
2105 : 642 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
2106 : : required_outer);
2107 : 642 : pathnode->parallel_aware = false;
2108 : 642 : pathnode->parallel_safe = rel->consider_parallel;
2109 : 642 : pathnode->parallel_workers = 0;
2110 : 642 : pathnode->pathkeys = NIL; /* result is always unordered */
2111 : :
2112 : : /* Cost is the same as for a regular CTE scan */
2113 : 642 : cost_ctescan(pathnode, root, rel, pathnode->param_info);
2114 : :
2115 : 642 : return pathnode;
2116 : : }
2117 : :
2118 : : /*
2119 : : * create_foreignscan_path
2120 : : * Creates a path corresponding to a scan of a foreign base table,
2121 : : * returning the pathnode.
2122 : : *
2123 : : * This function is never called from core Postgres; rather, it's expected
2124 : : * to be called by the GetForeignPaths function of a foreign data wrapper.
2125 : : * We make the FDW supply all fields of the path, since we do not have any way
2126 : : * to calculate them in core. However, there is a usually-sane default for
2127 : : * the pathtarget (rel->reltarget), so we let a NULL for "target" select that.
2128 : : */
2129 : : ForeignPath *
2130 : 1990 : create_foreignscan_path(PlannerInfo *root, RelOptInfo *rel,
2131 : : PathTarget *target,
2132 : : double rows, int disabled_nodes,
2133 : : Cost startup_cost, Cost total_cost,
2134 : : List *pathkeys,
2135 : : Relids required_outer,
2136 : : Path *fdw_outerpath,
2137 : : List *fdw_restrictinfo,
2138 : : List *fdw_private)
2139 : : {
2140 : 1990 : ForeignPath *pathnode = makeNode(ForeignPath);
2141 : :
2142 : : /* Historically some FDWs were confused about when to use this */
2143 : : Assert(IS_SIMPLE_REL(rel));
2144 : :
2145 : 1990 : pathnode->path.pathtype = T_ForeignScan;
2146 : 1990 : pathnode->path.parent = rel;
2147 [ + - ]: 1990 : pathnode->path.pathtarget = target ? target : rel->reltarget;
2148 : 1990 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
2149 : : required_outer);
2150 : 1990 : pathnode->path.parallel_aware = false;
2151 : 1990 : pathnode->path.parallel_safe = rel->consider_parallel;
2152 : 1990 : pathnode->path.parallel_workers = 0;
2153 : 1990 : pathnode->path.rows = rows;
2154 : 1990 : pathnode->path.disabled_nodes = disabled_nodes;
2155 : 1990 : pathnode->path.startup_cost = startup_cost;
2156 : 1990 : pathnode->path.total_cost = total_cost;
2157 : 1990 : pathnode->path.pathkeys = pathkeys;
2158 : :
2159 : 1990 : pathnode->fdw_outerpath = fdw_outerpath;
2160 : 1990 : pathnode->fdw_restrictinfo = fdw_restrictinfo;
2161 : 1990 : pathnode->fdw_private = fdw_private;
2162 : :
2163 : 1990 : return pathnode;
2164 : : }
2165 : :
2166 : : /*
2167 : : * create_foreign_join_path
2168 : : * Creates a path corresponding to a scan of a foreign join,
2169 : : * returning the pathnode.
2170 : : *
2171 : : * This function is never called from core Postgres; rather, it's expected
2172 : : * to be called by the GetForeignJoinPaths function of a foreign data wrapper.
2173 : : * We make the FDW supply all fields of the path, since we do not have any way
2174 : : * to calculate them in core. However, there is a usually-sane default for
2175 : : * the pathtarget (rel->reltarget), so we let a NULL for "target" select that.
2176 : : */
2177 : : ForeignPath *
2178 : 657 : create_foreign_join_path(PlannerInfo *root, RelOptInfo *rel,
2179 : : PathTarget *target,
2180 : : double rows, int disabled_nodes,
2181 : : Cost startup_cost, Cost total_cost,
2182 : : List *pathkeys,
2183 : : Relids required_outer,
2184 : : Path *fdw_outerpath,
2185 : : List *fdw_restrictinfo,
2186 : : List *fdw_private)
2187 : : {
2188 : 657 : ForeignPath *pathnode = makeNode(ForeignPath);
2189 : :
2190 : : /*
2191 : : * We should use get_joinrel_parampathinfo to handle parameterized paths,
2192 : : * but the API of this function doesn't support it, and existing
2193 : : * extensions aren't yet trying to build such paths anyway. For the
2194 : : * moment just throw an error if someone tries it; eventually we should
2195 : : * revisit this.
2196 : : */
2197 [ + - - + ]: 657 : if (!bms_is_empty(required_outer) || !bms_is_empty(rel->lateral_relids))
2198 [ # # ]: 0 : elog(ERROR, "parameterized foreign joins are not supported yet");
2199 : :
2200 : 657 : pathnode->path.pathtype = T_ForeignScan;
2201 : 657 : pathnode->path.parent = rel;
2202 [ + - ]: 657 : pathnode->path.pathtarget = target ? target : rel->reltarget;
2203 : 657 : pathnode->path.param_info = NULL; /* XXX see above */
2204 : 657 : pathnode->path.parallel_aware = false;
2205 : 657 : pathnode->path.parallel_safe = rel->consider_parallel;
2206 : 657 : pathnode->path.parallel_workers = 0;
2207 : 657 : pathnode->path.rows = rows;
2208 : 657 : pathnode->path.disabled_nodes = disabled_nodes;
2209 : 657 : pathnode->path.startup_cost = startup_cost;
2210 : 657 : pathnode->path.total_cost = total_cost;
2211 : 657 : pathnode->path.pathkeys = pathkeys;
2212 : :
2213 : 657 : pathnode->fdw_outerpath = fdw_outerpath;
2214 : 657 : pathnode->fdw_restrictinfo = fdw_restrictinfo;
2215 : 657 : pathnode->fdw_private = fdw_private;
2216 : :
2217 : 657 : return pathnode;
2218 : : }
2219 : :
2220 : : /*
2221 : : * create_foreign_upper_path
2222 : : * Creates a path corresponding to an upper relation that's computed
2223 : : * directly by an FDW, returning the pathnode.
2224 : : *
2225 : : * This function is never called from core Postgres; rather, it's expected to
2226 : : * be called by the GetForeignUpperPaths function of a foreign data wrapper.
2227 : : * We make the FDW supply all fields of the path, since we do not have any way
2228 : : * to calculate them in core. However, there is a usually-sane default for
2229 : : * the pathtarget (rel->reltarget), so we let a NULL for "target" select that.
2230 : : */
2231 : : ForeignPath *
2232 : 298 : create_foreign_upper_path(PlannerInfo *root, RelOptInfo *rel,
2233 : : PathTarget *target,
2234 : : double rows, int disabled_nodes,
2235 : : Cost startup_cost, Cost total_cost,
2236 : : List *pathkeys,
2237 : : Path *fdw_outerpath,
2238 : : List *fdw_restrictinfo,
2239 : : List *fdw_private)
2240 : : {
2241 : 298 : ForeignPath *pathnode = makeNode(ForeignPath);
2242 : :
2243 : : /*
2244 : : * Upper relations should never have any lateral references, since joining
2245 : : * is complete.
2246 : : */
2247 : : Assert(bms_is_empty(rel->lateral_relids));
2248 : :
2249 : 298 : pathnode->path.pathtype = T_ForeignScan;
2250 : 298 : pathnode->path.parent = rel;
2251 [ - + ]: 298 : pathnode->path.pathtarget = target ? target : rel->reltarget;
2252 : 298 : pathnode->path.param_info = NULL;
2253 : 298 : pathnode->path.parallel_aware = false;
2254 : 298 : pathnode->path.parallel_safe = rel->consider_parallel;
2255 : 298 : pathnode->path.parallel_workers = 0;
2256 : 298 : pathnode->path.rows = rows;
2257 : 298 : pathnode->path.disabled_nodes = disabled_nodes;
2258 : 298 : pathnode->path.startup_cost = startup_cost;
2259 : 298 : pathnode->path.total_cost = total_cost;
2260 : 298 : pathnode->path.pathkeys = pathkeys;
2261 : :
2262 : 298 : pathnode->fdw_outerpath = fdw_outerpath;
2263 : 298 : pathnode->fdw_restrictinfo = fdw_restrictinfo;
2264 : 298 : pathnode->fdw_private = fdw_private;
2265 : :
2266 : 298 : return pathnode;
2267 : : }
2268 : :
2269 : : /*
2270 : : * calc_nestloop_required_outer
2271 : : * Compute the required_outer set for a nestloop join path
2272 : : *
2273 : : * Note: when considering a child join, the inputs nonetheless use top-level
2274 : : * parent relids
2275 : : *
2276 : : * Note: result must not share storage with either input
2277 : : */
2278 : : Relids
2279 : 2616283 : calc_nestloop_required_outer(Relids outerrelids,
2280 : : Relids outer_paramrels,
2281 : : Relids innerrelids,
2282 : : Relids inner_paramrels)
2283 : : {
2284 : : Relids required_outer;
2285 : :
2286 : : /* inner_path can require rels from outer path, but not vice versa */
2287 : : Assert(!bms_overlap(outer_paramrels, innerrelids));
2288 : : /* easy case if inner path is not parameterized */
2289 [ + + ]: 2616283 : if (!inner_paramrels)
2290 : 1844835 : return bms_copy(outer_paramrels);
2291 : : /* else, form the union ... */
2292 : 771448 : required_outer = bms_union(outer_paramrels, inner_paramrels);
2293 : : /* ... and remove any mention of now-satisfied outer rels */
2294 : 771448 : required_outer = bms_del_members(required_outer,
2295 : : outerrelids);
2296 : 771448 : return required_outer;
2297 : : }
2298 : :
2299 : : /*
2300 : : * calc_non_nestloop_required_outer
2301 : : * Compute the required_outer set for a merge or hash join path
2302 : : *
2303 : : * Note: result must not share storage with either input
2304 : : */
2305 : : Relids
2306 : 1655500 : calc_non_nestloop_required_outer(Path *outer_path, Path *inner_path)
2307 : : {
2308 [ + + ]: 1655500 : Relids outer_paramrels = PATH_REQ_OUTER(outer_path);
2309 [ + + ]: 1655500 : Relids inner_paramrels = PATH_REQ_OUTER(inner_path);
2310 : : Relids innerrelids PG_USED_FOR_ASSERTS_ONLY;
2311 : : Relids outerrelids PG_USED_FOR_ASSERTS_ONLY;
2312 : : Relids required_outer;
2313 : :
2314 : : /*
2315 : : * Any parameterization of the input paths refers to topmost parents of
2316 : : * the relevant relations, because reparameterize_path_by_child() hasn't
2317 : : * been called yet. So we must consider topmost parents of the relations
2318 : : * being joined, too, while checking for disallowed parameterization
2319 : : * cases.
2320 : : */
2321 [ + + ]: 1655500 : if (inner_path->parent->top_parent_relids)
2322 : 115969 : innerrelids = inner_path->parent->top_parent_relids;
2323 : : else
2324 : 1539531 : innerrelids = inner_path->parent->relids;
2325 : :
2326 [ + + ]: 1655500 : if (outer_path->parent->top_parent_relids)
2327 : 115969 : outerrelids = outer_path->parent->top_parent_relids;
2328 : : else
2329 : 1539531 : outerrelids = outer_path->parent->relids;
2330 : :
2331 : : /* neither path can require rels from the other */
2332 : : Assert(!bms_overlap(outer_paramrels, innerrelids));
2333 : : Assert(!bms_overlap(inner_paramrels, outerrelids));
2334 : : /* form the union ... */
2335 : 1655500 : required_outer = bms_union(outer_paramrels, inner_paramrels);
2336 : : /* we do not need an explicit test for empty; bms_union gets it right */
2337 : 1655500 : return required_outer;
2338 : : }
2339 : :
2340 : : /*
2341 : : * create_nestloop_path
2342 : : * Creates a pathnode corresponding to a nestloop join between two
2343 : : * relations.
2344 : : *
2345 : : * 'joinrel' is the join relation.
2346 : : * 'jointype' is the type of join required
2347 : : * 'workspace' is the result from initial_cost_nestloop
2348 : : * 'extra' contains various information about the join
2349 : : * 'outer_path' is the outer path
2350 : : * 'inner_path' is the inner path
2351 : : * 'restrict_clauses' are the RestrictInfo nodes to apply at the join
2352 : : * 'pathkeys' are the path keys of the new join path
2353 : : * 'required_outer' is the set of required outer rels
2354 : : *
2355 : : * Returns the resulting path node.
2356 : : */
2357 : : NestPath *
2358 : 1163242 : create_nestloop_path(PlannerInfo *root,
2359 : : RelOptInfo *joinrel,
2360 : : JoinType jointype,
2361 : : JoinCostWorkspace *workspace,
2362 : : JoinPathExtraData *extra,
2363 : : Path *outer_path,
2364 : : Path *inner_path,
2365 : : List *restrict_clauses,
2366 : : List *pathkeys,
2367 : : Relids required_outer)
2368 : : {
2369 : 1163242 : NestPath *pathnode = makeNode(NestPath);
2370 [ + + ]: 1163242 : Relids inner_req_outer = PATH_REQ_OUTER(inner_path);
2371 : : Relids outerrelids;
2372 : :
2373 : : /*
2374 : : * Paths are parameterized by top-level parents, so run parameterization
2375 : : * tests on the parent relids.
2376 : : */
2377 [ + + ]: 1163242 : if (outer_path->parent->top_parent_relids)
2378 : 66149 : outerrelids = outer_path->parent->top_parent_relids;
2379 : : else
2380 : 1097093 : outerrelids = outer_path->parent->relids;
2381 : :
2382 : : /*
2383 : : * If the inner path is parameterized by the outer, we must drop any
2384 : : * restrict_clauses that are due to be moved into the inner path. We have
2385 : : * to do this now, rather than postpone the work till createplan time,
2386 : : * because the restrict_clauses list can affect the size and cost
2387 : : * estimates for this path. We detect such clauses by checking for serial
2388 : : * number match to clauses already enforced in the inner path.
2389 : : */
2390 [ + + ]: 1163242 : if (bms_overlap(inner_req_outer, outerrelids))
2391 : : {
2392 : 294245 : Bitmapset *enforced_serials = get_param_path_clause_serials(inner_path);
2393 : 294245 : List *jclauses = NIL;
2394 : : ListCell *lc;
2395 : :
2396 [ + + + + : 657949 : foreach(lc, restrict_clauses)
+ + ]
2397 : : {
2398 : 363704 : RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2399 : :
2400 [ + + ]: 363704 : if (!bms_is_member(rinfo->rinfo_serial, enforced_serials))
2401 : 58878 : jclauses = lappend(jclauses, rinfo);
2402 : : }
2403 : 294245 : restrict_clauses = jclauses;
2404 : : }
2405 : :
2406 : 1163242 : pathnode->jpath.path.pathtype = T_NestLoop;
2407 : 1163242 : pathnode->jpath.path.parent = joinrel;
2408 : 1163242 : pathnode->jpath.path.pathtarget = joinrel->reltarget;
2409 : 1163242 : pathnode->jpath.path.param_info =
2410 : 1163242 : get_joinrel_parampathinfo(root,
2411 : : joinrel,
2412 : : outer_path,
2413 : : inner_path,
2414 : : extra->sjinfo,
2415 : : required_outer,
2416 : : &restrict_clauses);
2417 : 1163242 : pathnode->jpath.path.parallel_aware = false;
2418 : 3386224 : pathnode->jpath.path.parallel_safe = joinrel->consider_parallel &&
2419 [ + + + + : 1163242 : outer_path->parallel_safe && inner_path->parallel_safe;
+ + ]
2420 : : /* This is a foolish way to estimate parallel_workers, but for now... */
2421 : 1163242 : pathnode->jpath.path.parallel_workers = outer_path->parallel_workers;
2422 : 1163242 : pathnode->jpath.path.pathkeys = pathkeys;
2423 : 1163242 : pathnode->jpath.jointype = jointype;
2424 : 1163242 : pathnode->jpath.inner_unique = extra->inner_unique;
2425 : 1163242 : pathnode->jpath.outerjoinpath = outer_path;
2426 : 1163242 : pathnode->jpath.innerjoinpath = inner_path;
2427 : 1163242 : pathnode->jpath.joinrestrictinfo = restrict_clauses;
2428 : :
2429 : 1163242 : final_cost_nestloop(root, pathnode, workspace, extra);
2430 : :
2431 : 1163242 : return pathnode;
2432 : : }
2433 : :
2434 : : /*
2435 : : * create_mergejoin_path
2436 : : * Creates a pathnode corresponding to a mergejoin join between
2437 : : * two relations
2438 : : *
2439 : : * 'joinrel' is the join relation
2440 : : * 'jointype' is the type of join required
2441 : : * 'workspace' is the result from initial_cost_mergejoin
2442 : : * 'extra' contains various information about the join
2443 : : * 'outer_path' is the outer path
2444 : : * 'inner_path' is the inner path
2445 : : * 'restrict_clauses' are the RestrictInfo nodes to apply at the join
2446 : : * 'pathkeys' are the path keys of the new join path
2447 : : * 'required_outer' is the set of required outer rels
2448 : : * 'mergeclauses' are the RestrictInfo nodes to use as merge clauses
2449 : : * (this should be a subset of the restrict_clauses list)
2450 : : * 'outersortkeys' are the sort varkeys for the outer relation
2451 : : * 'innersortkeys' are the sort varkeys for the inner relation
2452 : : * 'outer_presorted_keys' is the number of presorted keys of the outer path
2453 : : */
2454 : : MergePath *
2455 : 350251 : create_mergejoin_path(PlannerInfo *root,
2456 : : RelOptInfo *joinrel,
2457 : : JoinType jointype,
2458 : : JoinCostWorkspace *workspace,
2459 : : JoinPathExtraData *extra,
2460 : : Path *outer_path,
2461 : : Path *inner_path,
2462 : : List *restrict_clauses,
2463 : : List *pathkeys,
2464 : : Relids required_outer,
2465 : : List *mergeclauses,
2466 : : List *outersortkeys,
2467 : : List *innersortkeys,
2468 : : int outer_presorted_keys)
2469 : : {
2470 : 350251 : MergePath *pathnode = makeNode(MergePath);
2471 : :
2472 : 350251 : pathnode->jpath.path.pathtype = T_MergeJoin;
2473 : 350251 : pathnode->jpath.path.parent = joinrel;
2474 : 350251 : pathnode->jpath.path.pathtarget = joinrel->reltarget;
2475 : 350251 : pathnode->jpath.path.param_info =
2476 : 350251 : get_joinrel_parampathinfo(root,
2477 : : joinrel,
2478 : : outer_path,
2479 : : inner_path,
2480 : : extra->sjinfo,
2481 : : required_outer,
2482 : : &restrict_clauses);
2483 : 350251 : pathnode->jpath.path.parallel_aware = false;
2484 : 1024641 : pathnode->jpath.path.parallel_safe = joinrel->consider_parallel &&
2485 [ + + + + : 350251 : outer_path->parallel_safe && inner_path->parallel_safe;
+ + ]
2486 : : /* This is a foolish way to estimate parallel_workers, but for now... */
2487 : 350251 : pathnode->jpath.path.parallel_workers = outer_path->parallel_workers;
2488 : 350251 : pathnode->jpath.path.pathkeys = pathkeys;
2489 : 350251 : pathnode->jpath.jointype = jointype;
2490 : 350251 : pathnode->jpath.inner_unique = extra->inner_unique;
2491 : 350251 : pathnode->jpath.outerjoinpath = outer_path;
2492 : 350251 : pathnode->jpath.innerjoinpath = inner_path;
2493 : 350251 : pathnode->jpath.joinrestrictinfo = restrict_clauses;
2494 : 350251 : pathnode->path_mergeclauses = mergeclauses;
2495 : 350251 : pathnode->outersortkeys = outersortkeys;
2496 : 350251 : pathnode->innersortkeys = innersortkeys;
2497 : 350251 : pathnode->outer_presorted_keys = outer_presorted_keys;
2498 : : /* pathnode->skip_mark_restore will be set by final_cost_mergejoin */
2499 : : /* pathnode->materialize_inner will be set by final_cost_mergejoin */
2500 : :
2501 : 350251 : final_cost_mergejoin(root, pathnode, workspace, extra);
2502 : :
2503 : 350251 : return pathnode;
2504 : : }
2505 : :
2506 : : /*
2507 : : * create_hashjoin_path
2508 : : * Creates a pathnode corresponding to a hash join between two relations.
2509 : : *
2510 : : * 'joinrel' is the join relation
2511 : : * 'jointype' is the type of join required
2512 : : * 'workspace' is the result from initial_cost_hashjoin
2513 : : * 'extra' contains various information about the join
2514 : : * 'outer_path' is the cheapest outer path
2515 : : * 'inner_path' is the cheapest inner path
2516 : : * 'parallel_hash' to select Parallel Hash of inner path (shared hash table)
2517 : : * 'restrict_clauses' are the RestrictInfo nodes to apply at the join
2518 : : * 'required_outer' is the set of required outer rels
2519 : : * 'hashclauses' are the RestrictInfo nodes to use as hash clauses
2520 : : * (this should be a subset of the restrict_clauses list)
2521 : : */
2522 : : HashPath *
2523 : 348010 : create_hashjoin_path(PlannerInfo *root,
2524 : : RelOptInfo *joinrel,
2525 : : JoinType jointype,
2526 : : JoinCostWorkspace *workspace,
2527 : : JoinPathExtraData *extra,
2528 : : Path *outer_path,
2529 : : Path *inner_path,
2530 : : bool parallel_hash,
2531 : : List *restrict_clauses,
2532 : : Relids required_outer,
2533 : : List *hashclauses)
2534 : : {
2535 : 348010 : HashPath *pathnode = makeNode(HashPath);
2536 : :
2537 : 348010 : pathnode->jpath.path.pathtype = T_HashJoin;
2538 : 348010 : pathnode->jpath.path.parent = joinrel;
2539 : 348010 : pathnode->jpath.path.pathtarget = joinrel->reltarget;
2540 : 348010 : pathnode->jpath.path.param_info =
2541 : 348010 : get_joinrel_parampathinfo(root,
2542 : : joinrel,
2543 : : outer_path,
2544 : : inner_path,
2545 : : extra->sjinfo,
2546 : : required_outer,
2547 : : &restrict_clauses);
2548 : 348010 : pathnode->jpath.path.parallel_aware =
2549 [ + + + + ]: 348010 : joinrel->consider_parallel && parallel_hash;
2550 : 1016626 : pathnode->jpath.path.parallel_safe = joinrel->consider_parallel &&
2551 [ + + + + : 348010 : outer_path->parallel_safe && inner_path->parallel_safe;
+ + ]
2552 : : /* This is a foolish way to estimate parallel_workers, but for now... */
2553 : 348010 : pathnode->jpath.path.parallel_workers = outer_path->parallel_workers;
2554 : :
2555 : : /*
2556 : : * A hashjoin never has pathkeys, since its output ordering is
2557 : : * unpredictable due to possible batching. XXX If the inner relation is
2558 : : * small enough, we could instruct the executor that it must not batch,
2559 : : * and then we could assume that the output inherits the outer relation's
2560 : : * ordering, which might save a sort step. However there is considerable
2561 : : * downside if our estimate of the inner relation size is badly off. For
2562 : : * the moment we don't risk it. (Note also that if we wanted to take this
2563 : : * seriously, joinpath.c would have to consider many more paths for the
2564 : : * outer rel than it does now.)
2565 : : */
2566 : 348010 : pathnode->jpath.path.pathkeys = NIL;
2567 : 348010 : pathnode->jpath.jointype = jointype;
2568 : 348010 : pathnode->jpath.inner_unique = extra->inner_unique;
2569 : 348010 : pathnode->jpath.outerjoinpath = outer_path;
2570 : 348010 : pathnode->jpath.innerjoinpath = inner_path;
2571 : 348010 : pathnode->jpath.joinrestrictinfo = restrict_clauses;
2572 : 348010 : pathnode->path_hashclauses = hashclauses;
2573 : : /* final_cost_hashjoin will fill in pathnode->num_batches */
2574 : :
2575 : 348010 : final_cost_hashjoin(root, pathnode, workspace, extra);
2576 : :
2577 : 348010 : return pathnode;
2578 : : }
2579 : :
2580 : : /*
2581 : : * create_projection_path
2582 : : * Creates a pathnode that represents performing a projection.
2583 : : *
2584 : : * 'rel' is the parent relation associated with the result
2585 : : * 'subpath' is the path representing the source of data
2586 : : * 'target' is the PathTarget to be computed
2587 : : */
2588 : : ProjectionPath *
2589 : 308735 : create_projection_path(PlannerInfo *root,
2590 : : RelOptInfo *rel,
2591 : : Path *subpath,
2592 : : PathTarget *target)
2593 : : {
2594 : 308735 : ProjectionPath *pathnode = makeNode(ProjectionPath);
2595 : : PathTarget *oldtarget;
2596 : :
2597 : : /*
2598 : : * We mustn't put a ProjectionPath directly above another; it's useless
2599 : : * and will confuse create_projection_plan. Rather than making sure all
2600 : : * callers handle that, let's implement it here, by stripping off any
2601 : : * ProjectionPath in what we're given. Given this rule, there won't be
2602 : : * more than one.
2603 : : */
2604 [ + + ]: 308735 : if (IsA(subpath, ProjectionPath))
2605 : : {
2606 : 20 : ProjectionPath *subpp = (ProjectionPath *) subpath;
2607 : :
2608 : : Assert(subpp->path.parent == rel);
2609 : 20 : subpath = subpp->subpath;
2610 : : Assert(!IsA(subpath, ProjectionPath));
2611 : : }
2612 : :
2613 : 308735 : pathnode->path.pathtype = T_Result;
2614 : 308735 : pathnode->path.parent = rel;
2615 : 308735 : pathnode->path.pathtarget = target;
2616 : 308735 : pathnode->path.param_info = subpath->param_info;
2617 : 308735 : pathnode->path.parallel_aware = false;
2618 : 727554 : pathnode->path.parallel_safe = rel->consider_parallel &&
2619 [ + + + + : 411678 : subpath->parallel_safe &&
+ - ]
2620 : 102943 : is_parallel_safe(root, (Node *) target->exprs);
2621 : 308735 : pathnode->path.parallel_workers = subpath->parallel_workers;
2622 : : /* Projection does not change the sort order */
2623 : 308735 : pathnode->path.pathkeys = subpath->pathkeys;
2624 : :
2625 : 308735 : pathnode->subpath = subpath;
2626 : :
2627 : : /*
2628 : : * We might not need a separate Result node. If the input plan node type
2629 : : * can project, we can just tell it to project something else. Or, if it
2630 : : * can't project but the desired target has the same expression list as
2631 : : * what the input will produce anyway, we can still give it the desired
2632 : : * tlist (possibly changing its ressortgroupref labels, but nothing else).
2633 : : * Note: in the latter case, create_projection_plan has to recheck our
2634 : : * conclusion; see comments therein.
2635 : : */
2636 : 308735 : oldtarget = subpath->pathtarget;
2637 [ + + + + ]: 322668 : if (is_projection_capable_path(subpath) ||
2638 : 13933 : equal(oldtarget->exprs, target->exprs))
2639 : : {
2640 : : /* No separate Result node needed */
2641 : 296227 : pathnode->dummypp = true;
2642 : :
2643 : : /*
2644 : : * Set cost of plan as subpath's cost, adjusted for tlist replacement.
2645 : : */
2646 : 296227 : pathnode->path.rows = subpath->rows;
2647 : 296227 : pathnode->path.disabled_nodes = subpath->disabled_nodes;
2648 : 296227 : pathnode->path.startup_cost = subpath->startup_cost +
2649 : 296227 : (target->cost.startup - oldtarget->cost.startup);
2650 : 296227 : pathnode->path.total_cost = subpath->total_cost +
2651 : 296227 : (target->cost.startup - oldtarget->cost.startup) +
2652 : 296227 : (target->cost.per_tuple - oldtarget->cost.per_tuple) * subpath->rows;
2653 : : }
2654 : : else
2655 : : {
2656 : : /* We really do need the Result node */
2657 : 12508 : pathnode->dummypp = false;
2658 : :
2659 : : /*
2660 : : * The Result node's cost is cpu_tuple_cost per row, plus the cost of
2661 : : * evaluating the tlist. There is no qual to worry about.
2662 : : */
2663 : 12508 : pathnode->path.rows = subpath->rows;
2664 : 12508 : pathnode->path.disabled_nodes = subpath->disabled_nodes;
2665 : 12508 : pathnode->path.startup_cost = subpath->startup_cost +
2666 : 12508 : target->cost.startup;
2667 : 12508 : pathnode->path.total_cost = subpath->total_cost +
2668 : 12508 : target->cost.startup +
2669 : 12508 : (cpu_tuple_cost + target->cost.per_tuple) * subpath->rows;
2670 : : }
2671 : :
2672 : 308735 : return pathnode;
2673 : : }
2674 : :
2675 : : /*
2676 : : * apply_projection_to_path
2677 : : * Add a projection step, or just apply the target directly to given path.
2678 : : *
2679 : : * This has the same net effect as create_projection_path(), except that if
2680 : : * a separate Result plan node isn't needed, we just replace the given path's
2681 : : * pathtarget with the desired one. This must be used only when the caller
2682 : : * knows that the given path isn't referenced elsewhere and so can be modified
2683 : : * in-place.
2684 : : *
2685 : : * If the input path is a GatherPath or GatherMergePath, we try to push the
2686 : : * new target down to its input as well; this is a yet more invasive
2687 : : * modification of the input path, which create_projection_path() can't do.
2688 : : *
2689 : : * Note that we mustn't change the source path's parent link; so when it is
2690 : : * add_path'd to "rel" things will be a bit inconsistent. So far that has
2691 : : * not caused any trouble.
2692 : : *
2693 : : * 'rel' is the parent relation associated with the result
2694 : : * 'path' is the path representing the source of data
2695 : : * 'target' is the PathTarget to be computed
2696 : : */
2697 : : Path *
2698 : 11643 : apply_projection_to_path(PlannerInfo *root,
2699 : : RelOptInfo *rel,
2700 : : Path *path,
2701 : : PathTarget *target)
2702 : : {
2703 : : QualCost oldcost;
2704 : :
2705 : : /*
2706 : : * If given path can't project, we might need a Result node, so make a
2707 : : * separate ProjectionPath.
2708 : : */
2709 [ + + ]: 11643 : if (!is_projection_capable_path(path))
2710 : 1094 : return (Path *) create_projection_path(root, rel, path, target);
2711 : :
2712 : : /*
2713 : : * We can just jam the desired tlist into the existing path, being sure to
2714 : : * update its cost estimates appropriately.
2715 : : */
2716 : 10549 : oldcost = path->pathtarget->cost;
2717 : 10549 : path->pathtarget = target;
2718 : :
2719 : 10549 : path->startup_cost += target->cost.startup - oldcost.startup;
2720 : 10549 : path->total_cost += target->cost.startup - oldcost.startup +
2721 : 10549 : (target->cost.per_tuple - oldcost.per_tuple) * path->rows;
2722 : :
2723 : : /*
2724 : : * If the path happens to be a Gather or GatherMerge path, we'd like to
2725 : : * arrange for the subpath to return the required target list so that
2726 : : * workers can help project. But if there is something that is not
2727 : : * parallel-safe in the target expressions, then we can't.
2728 : : */
2729 [ + - + + : 10569 : if ((IsA(path, GatherPath) || IsA(path, GatherMergePath)) &&
+ - ]
2730 : 20 : is_parallel_safe(root, (Node *) target->exprs))
2731 : : {
2732 : : /*
2733 : : * We always use create_projection_path here, even if the subpath is
2734 : : * projection-capable, so as to avoid modifying the subpath in place.
2735 : : * It seems unlikely at present that there could be any other
2736 : : * references to the subpath, but better safe than sorry.
2737 : : *
2738 : : * Note that we don't change the parallel path's cost estimates; it
2739 : : * might be appropriate to do so, to reflect the fact that the bulk of
2740 : : * the target evaluation will happen in workers.
2741 : : */
2742 [ - + ]: 20 : if (IsA(path, GatherPath))
2743 : : {
2744 : 0 : GatherPath *gpath = (GatherPath *) path;
2745 : :
2746 : 0 : gpath->subpath = (Path *)
2747 : 0 : create_projection_path(root,
2748 : 0 : gpath->subpath->parent,
2749 : : gpath->subpath,
2750 : : target);
2751 : : }
2752 : : else
2753 : : {
2754 : 20 : GatherMergePath *gmpath = (GatherMergePath *) path;
2755 : :
2756 : 20 : gmpath->subpath = (Path *)
2757 : 20 : create_projection_path(root,
2758 : 20 : gmpath->subpath->parent,
2759 : : gmpath->subpath,
2760 : : target);
2761 : : }
2762 : : }
2763 [ + + ]: 10529 : else if (path->parallel_safe &&
2764 [ + + ]: 3706 : !is_parallel_safe(root, (Node *) target->exprs))
2765 : : {
2766 : : /*
2767 : : * We're inserting a parallel-restricted target list into a path
2768 : : * currently marked parallel-safe, so we have to mark it as no longer
2769 : : * safe.
2770 : : */
2771 : 10 : path->parallel_safe = false;
2772 : : }
2773 : :
2774 : 10549 : return path;
2775 : : }
2776 : :
2777 : : /*
2778 : : * create_set_projection_path
2779 : : * Creates a pathnode that represents performing a projection that
2780 : : * includes set-returning functions.
2781 : : *
2782 : : * 'rel' is the parent relation associated with the result
2783 : : * 'subpath' is the path representing the source of data
2784 : : * 'target' is the PathTarget to be computed
2785 : : */
2786 : : ProjectSetPath *
2787 : 10289 : create_set_projection_path(PlannerInfo *root,
2788 : : RelOptInfo *rel,
2789 : : Path *subpath,
2790 : : PathTarget *target)
2791 : : {
2792 : 10289 : ProjectSetPath *pathnode = makeNode(ProjectSetPath);
2793 : : double tlist_rows;
2794 : : ListCell *lc;
2795 : :
2796 : 10289 : pathnode->path.pathtype = T_ProjectSet;
2797 : 10289 : pathnode->path.parent = rel;
2798 : 10289 : pathnode->path.pathtarget = target;
2799 : : /* For now, assume we are above any joins, so no parameterization */
2800 : 10289 : pathnode->path.param_info = NULL;
2801 : 10289 : pathnode->path.parallel_aware = false;
2802 : 24151 : pathnode->path.parallel_safe = rel->consider_parallel &&
2803 [ + + + + : 13833 : subpath->parallel_safe &&
+ - ]
2804 : 3544 : is_parallel_safe(root, (Node *) target->exprs);
2805 : 10289 : pathnode->path.parallel_workers = subpath->parallel_workers;
2806 : : /* Projection does not change the sort order XXX? */
2807 : 10289 : pathnode->path.pathkeys = subpath->pathkeys;
2808 : :
2809 : 10289 : pathnode->subpath = subpath;
2810 : :
2811 : : /*
2812 : : * Estimate number of rows produced by SRFs for each row of input; if
2813 : : * there's more than one in this node, use the maximum.
2814 : : */
2815 : 10289 : tlist_rows = 1;
2816 [ + - + + : 22508 : foreach(lc, target->exprs)
+ + ]
2817 : : {
2818 : 12219 : Node *node = (Node *) lfirst(lc);
2819 : : double itemrows;
2820 : :
2821 : 12219 : itemrows = expression_returns_set_rows(root, node);
2822 [ + + ]: 12219 : if (tlist_rows < itemrows)
2823 : 9909 : tlist_rows = itemrows;
2824 : : }
2825 : :
2826 : : /*
2827 : : * In addition to the cost of evaluating the tlist, charge cpu_tuple_cost
2828 : : * per input row, and half of cpu_tuple_cost for each added output row.
2829 : : * This is slightly bizarre maybe, but it's what 9.6 did; we may revisit
2830 : : * this estimate later.
2831 : : */
2832 : 10289 : pathnode->path.disabled_nodes = subpath->disabled_nodes;
2833 : 10289 : pathnode->path.rows = subpath->rows * tlist_rows;
2834 : 10289 : pathnode->path.startup_cost = subpath->startup_cost +
2835 : 10289 : target->cost.startup;
2836 : 10289 : pathnode->path.total_cost = subpath->total_cost +
2837 : 10289 : target->cost.startup +
2838 : 10289 : (cpu_tuple_cost + target->cost.per_tuple) * subpath->rows +
2839 : 10289 : (pathnode->path.rows - subpath->rows) * cpu_tuple_cost / 2;
2840 : :
2841 : 10289 : return pathnode;
2842 : : }
2843 : :
2844 : : /*
2845 : : * create_incremental_sort_path
2846 : : * Creates a pathnode that represents performing an incremental sort.
2847 : : *
2848 : : * 'rel' is the parent relation associated with the result
2849 : : * 'subpath' is the path representing the source of data
2850 : : * 'pathkeys' represents the desired sort order
2851 : : * 'presorted_keys' is the number of keys by which the input path is
2852 : : * already sorted
2853 : : * 'limit_tuples' is the estimated bound on the number of output tuples,
2854 : : * or -1 if no LIMIT or couldn't estimate
2855 : : */
2856 : : IncrementalSortPath *
2857 : 7981 : create_incremental_sort_path(PlannerInfo *root,
2858 : : RelOptInfo *rel,
2859 : : Path *subpath,
2860 : : List *pathkeys,
2861 : : int presorted_keys,
2862 : : double limit_tuples)
2863 : : {
2864 : 7981 : IncrementalSortPath *sort = makeNode(IncrementalSortPath);
2865 : 7981 : SortPath *pathnode = &sort->spath;
2866 : :
2867 : 7981 : pathnode->path.pathtype = T_IncrementalSort;
2868 : 7981 : pathnode->path.parent = rel;
2869 : : /* Sort doesn't project, so use source path's pathtarget */
2870 : 7981 : pathnode->path.pathtarget = subpath->pathtarget;
2871 : 7981 : pathnode->path.param_info = subpath->param_info;
2872 : 7981 : pathnode->path.parallel_aware = false;
2873 [ + + ]: 12059 : pathnode->path.parallel_safe = rel->consider_parallel &&
2874 [ + + ]: 4078 : subpath->parallel_safe;
2875 : 7981 : pathnode->path.parallel_workers = subpath->parallel_workers;
2876 : 7981 : pathnode->path.pathkeys = pathkeys;
2877 : :
2878 : 7981 : pathnode->subpath = subpath;
2879 : :
2880 : 7981 : cost_incremental_sort(&pathnode->path,
2881 : : root, pathkeys, presorted_keys,
2882 : : subpath->disabled_nodes,
2883 : : subpath->startup_cost,
2884 : : subpath->total_cost,
2885 : : subpath->rows,
2886 : 7981 : subpath->pathtarget->width,
2887 : : 0.0, /* XXX comparison_cost shouldn't be 0? */
2888 : : work_mem, limit_tuples,
2889 : : &sort->numGroups);
2890 : :
2891 : 7981 : sort->nPresortedCols = presorted_keys;
2892 : :
2893 : 7981 : return sort;
2894 : : }
2895 : :
2896 : : /*
2897 : : * create_sort_path
2898 : : * Creates a pathnode that represents performing an explicit sort.
2899 : : *
2900 : : * 'rel' is the parent relation associated with the result
2901 : : * 'subpath' is the path representing the source of data
2902 : : * 'pathkeys' represents the desired sort order
2903 : : * 'limit_tuples' is the estimated bound on the number of output tuples,
2904 : : * or -1 if no LIMIT or couldn't estimate
2905 : : */
2906 : : SortPath *
2907 : 95898 : create_sort_path(PlannerInfo *root,
2908 : : RelOptInfo *rel,
2909 : : Path *subpath,
2910 : : List *pathkeys,
2911 : : double limit_tuples)
2912 : : {
2913 : 95898 : SortPath *pathnode = makeNode(SortPath);
2914 : :
2915 : 95898 : pathnode->path.pathtype = T_Sort;
2916 : 95898 : pathnode->path.parent = rel;
2917 : : /* Sort doesn't project, so use source path's pathtarget */
2918 : 95898 : pathnode->path.pathtarget = subpath->pathtarget;
2919 : 95898 : pathnode->path.param_info = subpath->param_info;
2920 : 95898 : pathnode->path.parallel_aware = false;
2921 [ + + ]: 167772 : pathnode->path.parallel_safe = rel->consider_parallel &&
2922 [ + + ]: 71874 : subpath->parallel_safe;
2923 : 95898 : pathnode->path.parallel_workers = subpath->parallel_workers;
2924 : 95898 : pathnode->path.pathkeys = pathkeys;
2925 : :
2926 : 95898 : pathnode->subpath = subpath;
2927 : :
2928 : 95898 : cost_sort(&pathnode->path, root, pathkeys,
2929 : : subpath->disabled_nodes,
2930 : : subpath->total_cost,
2931 : : subpath->rows,
2932 : 95898 : subpath->pathtarget->width,
2933 : : 0.0, /* XXX comparison_cost shouldn't be 0? */
2934 : : work_mem, limit_tuples);
2935 : :
2936 : 95898 : return pathnode;
2937 : : }
2938 : :
2939 : : /*
2940 : : * create_group_path
2941 : : * Creates a pathnode that represents performing grouping of presorted input
2942 : : *
2943 : : * 'rel' is the parent relation associated with the result
2944 : : * 'subpath' is the path representing the source of data
2945 : : * 'target' is the PathTarget to be computed
2946 : : * 'groupClause' is a list of SortGroupClause's representing the grouping
2947 : : * 'qual' is the HAVING quals if any
2948 : : * 'numGroups' is the estimated number of groups
2949 : : */
2950 : : GroupPath *
2951 : 1043 : create_group_path(PlannerInfo *root,
2952 : : RelOptInfo *rel,
2953 : : Path *subpath,
2954 : : List *groupClause,
2955 : : List *qual,
2956 : : double numGroups)
2957 : : {
2958 : 1043 : GroupPath *pathnode = makeNode(GroupPath);
2959 : 1043 : PathTarget *target = rel->reltarget;
2960 : :
2961 : 1043 : pathnode->path.pathtype = T_Group;
2962 : 1043 : pathnode->path.parent = rel;
2963 : 1043 : pathnode->path.pathtarget = target;
2964 : : /* For now, assume we are above any joins, so no parameterization */
2965 : 1043 : pathnode->path.param_info = NULL;
2966 : 1043 : pathnode->path.parallel_aware = false;
2967 [ + + ]: 1677 : pathnode->path.parallel_safe = rel->consider_parallel &&
2968 [ + + ]: 634 : subpath->parallel_safe;
2969 : 1043 : pathnode->path.parallel_workers = subpath->parallel_workers;
2970 : : /* Group doesn't change sort ordering */
2971 : 1043 : pathnode->path.pathkeys = subpath->pathkeys;
2972 : :
2973 : 1043 : pathnode->subpath = subpath;
2974 : :
2975 : 1043 : pathnode->groupClause = groupClause;
2976 : 1043 : pathnode->qual = qual;
2977 : :
2978 : 1043 : cost_group(&pathnode->path, root,
2979 : : list_length(groupClause),
2980 : : numGroups,
2981 : : qual,
2982 : : subpath->disabled_nodes,
2983 : : subpath->startup_cost, subpath->total_cost,
2984 : : subpath->rows);
2985 : :
2986 : : /* add tlist eval cost for each output row */
2987 : 1043 : pathnode->path.startup_cost += target->cost.startup;
2988 : 1043 : pathnode->path.total_cost += target->cost.startup +
2989 : 1043 : target->cost.per_tuple * pathnode->path.rows;
2990 : :
2991 : 1043 : return pathnode;
2992 : : }
2993 : :
2994 : : /*
2995 : : * create_unique_path
2996 : : * Creates a pathnode that represents performing an explicit Unique step
2997 : : * on presorted input.
2998 : : *
2999 : : * 'rel' is the parent relation associated with the result
3000 : : * 'subpath' is the path representing the source of data
3001 : : * 'numCols' is the number of grouping columns
3002 : : * 'numGroups' is the estimated number of groups
3003 : : *
3004 : : * The input path must be sorted on the grouping columns, plus possibly
3005 : : * additional columns; so the first numCols pathkeys are the grouping columns
3006 : : */
3007 : : UniquePath *
3008 : 17866 : create_unique_path(PlannerInfo *root,
3009 : : RelOptInfo *rel,
3010 : : Path *subpath,
3011 : : int numCols,
3012 : : double numGroups)
3013 : : {
3014 : 17866 : UniquePath *pathnode = makeNode(UniquePath);
3015 : :
3016 : 17866 : pathnode->path.pathtype = T_Unique;
3017 : 17866 : pathnode->path.parent = rel;
3018 : : /* Unique doesn't project, so use source path's pathtarget */
3019 : 17866 : pathnode->path.pathtarget = subpath->pathtarget;
3020 : 17866 : pathnode->path.param_info = subpath->param_info;
3021 : 17866 : pathnode->path.parallel_aware = false;
3022 [ + + ]: 32250 : pathnode->path.parallel_safe = rel->consider_parallel &&
3023 [ + + ]: 14384 : subpath->parallel_safe;
3024 : 17866 : pathnode->path.parallel_workers = subpath->parallel_workers;
3025 : : /* Unique doesn't change the input ordering */
3026 : 17866 : pathnode->path.pathkeys = subpath->pathkeys;
3027 : :
3028 : 17866 : pathnode->subpath = subpath;
3029 : 17866 : pathnode->numkeys = numCols;
3030 : :
3031 : : /*
3032 : : * Charge one cpu_operator_cost per comparison per input tuple. We assume
3033 : : * all columns get compared at most of the tuples. (XXX probably this is
3034 : : * an overestimate.)
3035 : : */
3036 : 17866 : pathnode->path.disabled_nodes = subpath->disabled_nodes;
3037 : 17866 : pathnode->path.startup_cost = subpath->startup_cost;
3038 : 17866 : pathnode->path.total_cost = subpath->total_cost +
3039 : 17866 : cpu_operator_cost * subpath->rows * numCols;
3040 : 17866 : pathnode->path.rows = numGroups;
3041 : :
3042 : : /*
3043 : : * Mark the path as disabled if enable_groupagg is off. While this isn't
3044 : : * a grouping Agg node, it is the sort-based way of removing duplicates
3045 : : * and so is the natural counterpart to the AGG_HASHED path that
3046 : : * enable_hashagg controls; it seems close enough to justify letting that
3047 : : * switch control it.
3048 : : */
3049 [ + + ]: 17866 : if (!enable_groupagg)
3050 : 42 : pathnode->path.disabled_nodes++;
3051 : :
3052 : 17866 : return pathnode;
3053 : : }
3054 : :
3055 : : /*
3056 : : * create_agg_path
3057 : : * Creates a pathnode that represents performing aggregation/grouping
3058 : : *
3059 : : * 'rel' is the parent relation associated with the result
3060 : : * 'subpath' is the path representing the source of data
3061 : : * 'target' is the PathTarget to be computed
3062 : : * 'aggstrategy' is the Agg node's basic implementation strategy
3063 : : * 'aggsplit' is the Agg node's aggregate-splitting mode
3064 : : * 'groupClause' is a list of SortGroupClause's representing the grouping
3065 : : * 'qual' is the HAVING quals if any
3066 : : * 'aggcosts' contains cost info about the aggregate functions to be computed
3067 : : * 'numGroups' is the estimated number of groups (1 if not grouping)
3068 : : */
3069 : : AggPath *
3070 : 69287 : create_agg_path(PlannerInfo *root,
3071 : : RelOptInfo *rel,
3072 : : Path *subpath,
3073 : : PathTarget *target,
3074 : : AggStrategy aggstrategy,
3075 : : AggSplit aggsplit,
3076 : : List *groupClause,
3077 : : List *qual,
3078 : : const AggClauseCosts *aggcosts,
3079 : : double numGroups)
3080 : : {
3081 : 69287 : AggPath *pathnode = makeNode(AggPath);
3082 : :
3083 : 69287 : pathnode->path.pathtype = T_Agg;
3084 : 69287 : pathnode->path.parent = rel;
3085 : 69287 : pathnode->path.pathtarget = target;
3086 : 69287 : pathnode->path.param_info = subpath->param_info;
3087 : 69287 : pathnode->path.parallel_aware = false;
3088 [ + + ]: 119764 : pathnode->path.parallel_safe = rel->consider_parallel &&
3089 [ + + ]: 50477 : subpath->parallel_safe;
3090 : 69287 : pathnode->path.parallel_workers = subpath->parallel_workers;
3091 : :
3092 [ + + ]: 69287 : if (aggstrategy == AGG_SORTED)
3093 : : {
3094 : : /*
3095 : : * Attempt to preserve the order of the subpath. Additional pathkeys
3096 : : * may have been added in adjust_group_pathkeys_for_groupagg() to
3097 : : * support ORDER BY / DISTINCT aggregates. Pathkeys added there
3098 : : * belong to columns within the aggregate function, so we must strip
3099 : : * these additional pathkeys off as those columns are unavailable
3100 : : * above the aggregate node.
3101 : : */
3102 [ + + ]: 11922 : if (list_length(subpath->pathkeys) > root->num_groupby_pathkeys)
3103 : 646 : pathnode->path.pathkeys = list_copy_head(subpath->pathkeys,
3104 : : root->num_groupby_pathkeys);
3105 : : else
3106 : 11276 : pathnode->path.pathkeys = subpath->pathkeys; /* preserves order */
3107 : : }
3108 : : else
3109 : 57365 : pathnode->path.pathkeys = NIL; /* output is unordered */
3110 : :
3111 : 69287 : pathnode->subpath = subpath;
3112 : :
3113 : 69287 : pathnode->aggstrategy = aggstrategy;
3114 : 69287 : pathnode->aggsplit = aggsplit;
3115 : 69287 : pathnode->numGroups = numGroups;
3116 [ + + ]: 69287 : pathnode->transitionSpace = aggcosts ? aggcosts->transitionSpace : 0;
3117 : 69287 : pathnode->groupClause = groupClause;
3118 : 69287 : pathnode->qual = qual;
3119 : :
3120 : 69287 : cost_agg(&pathnode->path, root,
3121 : : aggstrategy, aggcosts,
3122 : : list_length(groupClause), numGroups,
3123 : : qual,
3124 : : subpath->disabled_nodes,
3125 : : subpath->startup_cost, subpath->total_cost,
3126 : 69287 : subpath->rows, subpath->pathtarget->width);
3127 : :
3128 : : /* add tlist eval cost for each output row */
3129 : 69287 : pathnode->path.startup_cost += target->cost.startup;
3130 : 69287 : pathnode->path.total_cost += target->cost.startup +
3131 : 69287 : target->cost.per_tuple * pathnode->path.rows;
3132 : :
3133 : 69287 : return pathnode;
3134 : : }
3135 : :
3136 : : /*
3137 : : * create_groupingsets_path
3138 : : * Creates a pathnode that represents performing GROUPING SETS aggregation
3139 : : *
3140 : : * GroupingSetsPath represents sorted grouping with one or more grouping sets.
3141 : : * The input path's result must be sorted to match the last entry in
3142 : : * rollup_groupclauses.
3143 : : *
3144 : : * 'rel' is the parent relation associated with the result
3145 : : * 'subpath' is the path representing the source of data
3146 : : * 'target' is the PathTarget to be computed
3147 : : * 'having_qual' is the HAVING quals if any
3148 : : * 'rollups' is a list of RollupData nodes
3149 : : * 'agg_costs' contains cost info about the aggregate functions to be computed
3150 : : */
3151 : : GroupingSetsPath *
3152 : 2239 : create_groupingsets_path(PlannerInfo *root,
3153 : : RelOptInfo *rel,
3154 : : Path *subpath,
3155 : : List *having_qual,
3156 : : AggStrategy aggstrategy,
3157 : : List *rollups,
3158 : : const AggClauseCosts *agg_costs)
3159 : : {
3160 : 2239 : GroupingSetsPath *pathnode = makeNode(GroupingSetsPath);
3161 : 2239 : PathTarget *target = rel->reltarget;
3162 : : ListCell *lc;
3163 : 2239 : bool is_first = true;
3164 : 2239 : bool is_first_sort = true;
3165 : :
3166 : : /* The topmost generated Plan node will be an Agg */
3167 : 2239 : pathnode->path.pathtype = T_Agg;
3168 : 2239 : pathnode->path.parent = rel;
3169 : 2239 : pathnode->path.pathtarget = target;
3170 : 2239 : pathnode->path.param_info = subpath->param_info;
3171 : 2239 : pathnode->path.parallel_aware = false;
3172 [ + + ]: 3420 : pathnode->path.parallel_safe = rel->consider_parallel &&
3173 [ + + ]: 1181 : subpath->parallel_safe;
3174 : 2239 : pathnode->path.parallel_workers = subpath->parallel_workers;
3175 : 2239 : pathnode->subpath = subpath;
3176 : :
3177 : : /*
3178 : : * Simplify callers by downgrading AGG_SORTED to AGG_PLAIN, and AGG_MIXED
3179 : : * to AGG_HASHED, here if possible.
3180 : : */
3181 [ + + + + ]: 3185 : if (aggstrategy == AGG_SORTED &&
3182 : 946 : list_length(rollups) == 1 &&
3183 [ + + ]: 440 : ((RollupData *) linitial(rollups))->groupClause == NIL)
3184 : 45 : aggstrategy = AGG_PLAIN;
3185 : :
3186 [ + + - + ]: 3205 : if (aggstrategy == AGG_MIXED &&
3187 : 966 : list_length(rollups) == 1)
3188 : 0 : aggstrategy = AGG_HASHED;
3189 : :
3190 : : /*
3191 : : * Output will be in sorted order by group_pathkeys if, and only if, there
3192 : : * is a single rollup operation on a non-empty list of grouping
3193 : : * expressions.
3194 : : */
3195 [ + + + + ]: 2239 : if (aggstrategy == AGG_SORTED && list_length(rollups) == 1)
3196 : 395 : pathnode->path.pathkeys = root->group_pathkeys;
3197 : : else
3198 : 1844 : pathnode->path.pathkeys = NIL;
3199 : :
3200 : 2239 : pathnode->aggstrategy = aggstrategy;
3201 : 2239 : pathnode->rollups = rollups;
3202 : 2239 : pathnode->qual = having_qual;
3203 [ + - ]: 2239 : pathnode->transitionSpace = agg_costs ? agg_costs->transitionSpace : 0;
3204 : :
3205 : : Assert(rollups != NIL);
3206 : : Assert(aggstrategy != AGG_PLAIN || list_length(rollups) == 1);
3207 : : Assert(aggstrategy != AGG_MIXED || list_length(rollups) > 1);
3208 : :
3209 [ + - + + : 7550 : foreach(lc, rollups)
+ + ]
3210 : : {
3211 : 5311 : RollupData *rollup = lfirst(lc);
3212 : 5311 : List *gsets = rollup->gsets;
3213 : 5311 : int numGroupCols = list_length(linitial(gsets));
3214 : :
3215 : : /*
3216 : : * In AGG_SORTED or AGG_PLAIN mode, the first rollup takes the
3217 : : * (already-sorted) input, and following ones do their own sort.
3218 : : *
3219 : : * In AGG_HASHED mode, there is one rollup for each grouping set.
3220 : : *
3221 : : * In AGG_MIXED mode, the first rollups are hashed, the first
3222 : : * non-hashed one takes the (already-sorted) input, and following ones
3223 : : * do their own sort.
3224 : : */
3225 [ + + ]: 5311 : if (is_first)
3226 : : {
3227 : 2239 : cost_agg(&pathnode->path, root,
3228 : : aggstrategy,
3229 : : agg_costs,
3230 : : numGroupCols,
3231 : : rollup->numGroups,
3232 : : having_qual,
3233 : : subpath->disabled_nodes,
3234 : : subpath->startup_cost,
3235 : : subpath->total_cost,
3236 : : subpath->rows,
3237 : 2239 : subpath->pathtarget->width);
3238 : 2239 : is_first = false;
3239 [ + + ]: 2239 : if (!rollup->is_hashed)
3240 : 946 : is_first_sort = false;
3241 : : }
3242 : : else
3243 : : {
3244 : : Path sort_path; /* dummy for result of cost_sort */
3245 : : Path agg_path; /* dummy for result of cost_agg */
3246 : :
3247 [ + + + + ]: 3072 : if (rollup->is_hashed || is_first_sort)
3248 : : {
3249 : : /*
3250 : : * Account for cost of aggregation, but don't charge input
3251 : : * cost again
3252 : : */
3253 : 2316 : cost_agg(&agg_path, root,
3254 : 2316 : rollup->is_hashed ? AGG_HASHED : AGG_SORTED,
3255 : : agg_costs,
3256 : : numGroupCols,
3257 : : rollup->numGroups,
3258 : : having_qual,
3259 : : 0, 0.0, 0.0,
3260 : : subpath->rows,
3261 [ + + ]: 2316 : subpath->pathtarget->width);
3262 [ + + ]: 2316 : if (!rollup->is_hashed)
3263 : 966 : is_first_sort = false;
3264 : : }
3265 : : else
3266 : : {
3267 : : /* Account for cost of sort, but don't charge input cost again */
3268 : 756 : cost_sort(&sort_path, root, NIL, 0,
3269 : : 0.0,
3270 : : subpath->rows,
3271 : 756 : subpath->pathtarget->width,
3272 : : 0.0,
3273 : : work_mem,
3274 : : -1.0);
3275 : :
3276 : : /* Account for cost of aggregation */
3277 : :
3278 : 756 : cost_agg(&agg_path, root,
3279 : : AGG_SORTED,
3280 : : agg_costs,
3281 : : numGroupCols,
3282 : : rollup->numGroups,
3283 : : having_qual,
3284 : : sort_path.disabled_nodes,
3285 : : sort_path.startup_cost,
3286 : : sort_path.total_cost,
3287 : : sort_path.rows,
3288 : 756 : subpath->pathtarget->width);
3289 : : }
3290 : :
3291 : 3072 : pathnode->path.disabled_nodes += agg_path.disabled_nodes;
3292 : 3072 : pathnode->path.total_cost += agg_path.total_cost;
3293 : 3072 : pathnode->path.rows += agg_path.rows;
3294 : : }
3295 : : }
3296 : :
3297 : : /* add tlist eval cost for each output row */
3298 : 2239 : pathnode->path.startup_cost += target->cost.startup;
3299 : 2239 : pathnode->path.total_cost += target->cost.startup +
3300 : 2239 : target->cost.per_tuple * pathnode->path.rows;
3301 : :
3302 : 2239 : return pathnode;
3303 : : }
3304 : :
3305 : : /*
3306 : : * create_minmaxagg_path
3307 : : * Creates a pathnode that represents computation of MIN/MAX aggregates
3308 : : *
3309 : : * 'rel' is the parent relation associated with the result
3310 : : * 'target' is the PathTarget to be computed
3311 : : * 'mmaggregates' is a list of MinMaxAggInfo structs
3312 : : * 'quals' is the HAVING quals if any
3313 : : */
3314 : : MinMaxAggPath *
3315 : 331 : create_minmaxagg_path(PlannerInfo *root,
3316 : : RelOptInfo *rel,
3317 : : PathTarget *target,
3318 : : List *mmaggregates,
3319 : : List *quals)
3320 : : {
3321 : 331 : MinMaxAggPath *pathnode = makeNode(MinMaxAggPath);
3322 : : Cost initplan_cost;
3323 : 331 : int initplan_disabled_nodes = 0;
3324 : : ListCell *lc;
3325 : :
3326 : : /* The topmost generated Plan node will be a Result */
3327 : 331 : pathnode->path.pathtype = T_Result;
3328 : 331 : pathnode->path.parent = rel;
3329 : 331 : pathnode->path.pathtarget = target;
3330 : : /* For now, assume we are above any joins, so no parameterization */
3331 : 331 : pathnode->path.param_info = NULL;
3332 : 331 : pathnode->path.parallel_aware = false;
3333 : 331 : pathnode->path.parallel_safe = true; /* might change below */
3334 : 331 : pathnode->path.parallel_workers = 0;
3335 : : /* Result is one unordered row */
3336 : 331 : pathnode->path.rows = 1;
3337 : 331 : pathnode->path.pathkeys = NIL;
3338 : :
3339 : 331 : pathnode->mmaggregates = mmaggregates;
3340 : 331 : pathnode->quals = quals;
3341 : :
3342 : : /* Calculate cost of all the initplans, and check parallel safety */
3343 : 331 : initplan_cost = 0;
3344 [ + - + + : 696 : foreach(lc, mmaggregates)
+ + ]
3345 : : {
3346 : 365 : MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
3347 : :
3348 : 365 : initplan_disabled_nodes += mminfo->path->disabled_nodes;
3349 : 365 : initplan_cost += mminfo->pathcost;
3350 [ + + ]: 365 : if (!mminfo->path->parallel_safe)
3351 : 83 : pathnode->path.parallel_safe = false;
3352 : : }
3353 : :
3354 : : /* add tlist eval cost for each output row, plus cpu_tuple_cost */
3355 : 331 : pathnode->path.disabled_nodes = initplan_disabled_nodes;
3356 : 331 : pathnode->path.startup_cost = initplan_cost + target->cost.startup;
3357 : 331 : pathnode->path.total_cost = initplan_cost + target->cost.startup +
3358 : 331 : target->cost.per_tuple + cpu_tuple_cost;
3359 : :
3360 : : /*
3361 : : * Add cost of qual, if any --- but we ignore its selectivity, since our
3362 : : * rowcount estimate should be 1 no matter what the qual is.
3363 : : */
3364 [ - + ]: 331 : if (quals)
3365 : : {
3366 : : QualCost qual_cost;
3367 : :
3368 : 0 : cost_qual_eval(&qual_cost, quals, root);
3369 : 0 : pathnode->path.startup_cost += qual_cost.startup;
3370 : 0 : pathnode->path.total_cost += qual_cost.startup + qual_cost.per_tuple;
3371 : : }
3372 : :
3373 : : /*
3374 : : * If the initplans were all parallel-safe, also check safety of the
3375 : : * target and quals. (The Result node itself isn't parallelizable, but if
3376 : : * we are in a subquery then it can be useful for the outer query to know
3377 : : * that this one is parallel-safe.)
3378 : : */
3379 [ + + ]: 331 : if (pathnode->path.parallel_safe)
3380 : 252 : pathnode->path.parallel_safe =
3381 [ + - + - ]: 504 : is_parallel_safe(root, (Node *) target->exprs) &&
3382 : 504 : is_parallel_safe(root, (Node *) quals);
3383 : :
3384 : 331 : return pathnode;
3385 : : }
3386 : :
3387 : : /*
3388 : : * create_windowagg_path
3389 : : * Creates a pathnode that represents computation of window functions
3390 : : *
3391 : : * 'rel' is the parent relation associated with the result
3392 : : * 'subpath' is the path representing the source of data
3393 : : * 'target' is the PathTarget to be computed
3394 : : * 'windowFuncs' is a list of WindowFunc structs
3395 : : * 'runCondition' is a list of OpExprs to short-circuit WindowAgg execution
3396 : : * 'winclause' is a WindowClause that is common to all the WindowFuncs
3397 : : * 'qual' WindowClause.runconditions from lower-level WindowAggPaths.
3398 : : * Must always be NIL when topwindow == false
3399 : : * 'topwindow' pass as true only for the top-level WindowAgg. False for all
3400 : : * intermediate WindowAggs.
3401 : : *
3402 : : * The input must be sorted according to the WindowClause's PARTITION keys
3403 : : * plus ORDER BY keys.
3404 : : */
3405 : : WindowAggPath *
3406 : 2654 : create_windowagg_path(PlannerInfo *root,
3407 : : RelOptInfo *rel,
3408 : : Path *subpath,
3409 : : PathTarget *target,
3410 : : List *windowFuncs,
3411 : : List *runCondition,
3412 : : WindowClause *winclause,
3413 : : List *qual,
3414 : : bool topwindow)
3415 : : {
3416 : 2654 : WindowAggPath *pathnode = makeNode(WindowAggPath);
3417 : :
3418 : : /* qual can only be set for the topwindow */
3419 : : Assert(qual == NIL || topwindow);
3420 : :
3421 : 2654 : pathnode->path.pathtype = T_WindowAgg;
3422 : 2654 : pathnode->path.parent = rel;
3423 : 2654 : pathnode->path.pathtarget = target;
3424 : : /* For now, assume we are above any joins, so no parameterization */
3425 : 2654 : pathnode->path.param_info = NULL;
3426 : 2654 : pathnode->path.parallel_aware = false;
3427 [ - + ]: 2654 : pathnode->path.parallel_safe = rel->consider_parallel &&
3428 [ # # ]: 0 : subpath->parallel_safe;
3429 : 2654 : pathnode->path.parallel_workers = subpath->parallel_workers;
3430 : : /* WindowAgg preserves the input sort order */
3431 : 2654 : pathnode->path.pathkeys = subpath->pathkeys;
3432 : :
3433 : 2654 : pathnode->subpath = subpath;
3434 : 2654 : pathnode->winclause = winclause;
3435 : 2654 : pathnode->qual = qual;
3436 : 2654 : pathnode->runCondition = runCondition;
3437 : 2654 : pathnode->topwindow = topwindow;
3438 : :
3439 : : /*
3440 : : * For costing purposes, assume that there are no redundant partitioning
3441 : : * or ordering columns; it's not worth the trouble to deal with that
3442 : : * corner case here. So we just pass the unmodified list lengths to
3443 : : * cost_windowagg.
3444 : : */
3445 : 2654 : cost_windowagg(&pathnode->path, root,
3446 : : windowFuncs,
3447 : : winclause,
3448 : : subpath->disabled_nodes,
3449 : : subpath->startup_cost,
3450 : : subpath->total_cost,
3451 : : subpath->rows);
3452 : :
3453 : : /* add tlist eval cost for each output row */
3454 : 2654 : pathnode->path.startup_cost += target->cost.startup;
3455 : 2654 : pathnode->path.total_cost += target->cost.startup +
3456 : 2654 : target->cost.per_tuple * pathnode->path.rows;
3457 : :
3458 : 2654 : return pathnode;
3459 : : }
3460 : :
3461 : : /*
3462 : : * create_setop_path
3463 : : * Creates a pathnode that represents computation of INTERSECT or EXCEPT
3464 : : *
3465 : : * 'rel' is the parent relation associated with the result
3466 : : * 'leftpath' is the path representing the left-hand source of data
3467 : : * 'rightpath' is the path representing the right-hand source of data
3468 : : * 'cmd' is the specific semantics (INTERSECT or EXCEPT, with/without ALL)
3469 : : * 'strategy' is the implementation strategy (sorted or hashed)
3470 : : * 'groupList' is a list of SortGroupClause's representing the grouping
3471 : : * 'numGroups' is the estimated number of distinct groups in left-hand input
3472 : : * 'outputRows' is the estimated number of output rows
3473 : : *
3474 : : * leftpath and rightpath must produce the same columns. Moreover, if
3475 : : * strategy is SETOP_SORTED, leftpath and rightpath must both be sorted
3476 : : * by all the grouping columns.
3477 : : */
3478 : : SetOpPath *
3479 : 1234 : create_setop_path(PlannerInfo *root,
3480 : : RelOptInfo *rel,
3481 : : Path *leftpath,
3482 : : Path *rightpath,
3483 : : SetOpCmd cmd,
3484 : : SetOpStrategy strategy,
3485 : : List *groupList,
3486 : : double numGroups,
3487 : : double outputRows)
3488 : : {
3489 : 1234 : SetOpPath *pathnode = makeNode(SetOpPath);
3490 : :
3491 : 1234 : pathnode->path.pathtype = T_SetOp;
3492 : 1234 : pathnode->path.parent = rel;
3493 : 1234 : pathnode->path.pathtarget = rel->reltarget;
3494 : : /* For now, assume we are above any joins, so no parameterization */
3495 : 1234 : pathnode->path.param_info = NULL;
3496 : 1234 : pathnode->path.parallel_aware = false;
3497 : 2468 : pathnode->path.parallel_safe = rel->consider_parallel &&
3498 [ - + - - : 1234 : leftpath->parallel_safe && rightpath->parallel_safe;
- - ]
3499 : 1234 : pathnode->path.parallel_workers =
3500 : 1234 : leftpath->parallel_workers + rightpath->parallel_workers;
3501 : : /* SetOp preserves the input sort order if in sort mode */
3502 : 1234 : pathnode->path.pathkeys =
3503 [ + + ]: 1234 : (strategy == SETOP_SORTED) ? leftpath->pathkeys : NIL;
3504 : :
3505 : 1234 : pathnode->leftpath = leftpath;
3506 : 1234 : pathnode->rightpath = rightpath;
3507 : 1234 : pathnode->cmd = cmd;
3508 : 1234 : pathnode->strategy = strategy;
3509 : 1234 : pathnode->groupList = groupList;
3510 : 1234 : pathnode->numGroups = numGroups;
3511 : :
3512 : : /*
3513 : : * Compute cost estimates. As things stand, we end up with the same total
3514 : : * cost in this node for sort and hash methods, but different startup
3515 : : * costs. This could be refined perhaps, but it'll do for now.
3516 : : */
3517 : 1234 : pathnode->path.disabled_nodes =
3518 : 1234 : leftpath->disabled_nodes + rightpath->disabled_nodes;
3519 [ + + ]: 1234 : if (strategy == SETOP_SORTED)
3520 : : {
3521 : : /*
3522 : : * In sorted mode, we can emit output incrementally. Charge one
3523 : : * cpu_operator_cost per comparison per input tuple. Like cost_group,
3524 : : * we assume all columns get compared at most of the tuples.
3525 : : */
3526 : 642 : pathnode->path.startup_cost =
3527 : 642 : leftpath->startup_cost + rightpath->startup_cost;
3528 : 642 : pathnode->path.total_cost =
3529 : 1284 : leftpath->total_cost + rightpath->total_cost +
3530 : 642 : cpu_operator_cost * (leftpath->rows + rightpath->rows) * list_length(groupList);
3531 : :
3532 : : /*
3533 : : * Also charge a small amount per extracted tuple. Like cost_sort,
3534 : : * charge only operator cost not cpu_tuple_cost, since SetOp does no
3535 : : * qual-checking or projection.
3536 : : */
3537 : 642 : pathnode->path.total_cost += cpu_operator_cost * outputRows;
3538 : :
3539 : : /*
3540 : : * Mark the path as disabled if enable_groupagg is off. While this
3541 : : * isn't a grouping Agg node, it is the sort-based implementation and
3542 : : * so is the natural counterpart to the SETOP_HASHED path that
3543 : : * enable_hashagg controls; it seems close enough to justify letting
3544 : : * that switch control it.
3545 : : */
3546 [ + + ]: 642 : if (!enable_groupagg)
3547 : 55 : pathnode->path.disabled_nodes++;
3548 : : }
3549 : : else
3550 : : {
3551 : : Size hashtablesize;
3552 : :
3553 : : /*
3554 : : * In hashed mode, we must read all the input before we can emit
3555 : : * anything. Also charge comparison costs to represent the cost of
3556 : : * hash table lookups.
3557 : : */
3558 : 592 : pathnode->path.startup_cost =
3559 : 1184 : leftpath->total_cost + rightpath->total_cost +
3560 : 592 : cpu_operator_cost * (leftpath->rows + rightpath->rows) * list_length(groupList);
3561 : 592 : pathnode->path.total_cost = pathnode->path.startup_cost;
3562 : :
3563 : : /*
3564 : : * Also charge a small amount per extracted tuple. Like cost_sort,
3565 : : * charge only operator cost not cpu_tuple_cost, since SetOp does no
3566 : : * qual-checking or projection.
3567 : : */
3568 : 592 : pathnode->path.total_cost += cpu_operator_cost * outputRows;
3569 : :
3570 : : /*
3571 : : * Mark the path as disabled if enable_hashagg is off. While this
3572 : : * isn't exactly a HashAgg node, it seems close enough to justify
3573 : : * letting that switch control it.
3574 : : */
3575 [ + + ]: 592 : if (!enable_hashagg)
3576 : 95 : pathnode->path.disabled_nodes++;
3577 : :
3578 : : /*
3579 : : * Also disable if it doesn't look like the hashtable will fit into
3580 : : * hash_mem. (Note: reject on equality, to ensure that an estimate of
3581 : : * SIZE_MAX disables hashing regardless of the hash_mem limit.)
3582 : : */
3583 : 592 : hashtablesize = EstimateSetOpHashTableSpace(numGroups,
3584 : 592 : leftpath->pathtarget->width);
3585 [ - + ]: 592 : if (hashtablesize >= get_hash_memory_limit())
3586 : 0 : pathnode->path.disabled_nodes++;
3587 : : }
3588 : 1234 : pathnode->path.rows = outputRows;
3589 : :
3590 : 1234 : return pathnode;
3591 : : }
3592 : :
3593 : : /*
3594 : : * create_recursiveunion_path
3595 : : * Creates a pathnode that represents a recursive UNION node
3596 : : *
3597 : : * 'rel' is the parent relation associated with the result
3598 : : * 'leftpath' is the source of data for the non-recursive term
3599 : : * 'rightpath' is the source of data for the recursive term
3600 : : * 'target' is the PathTarget to be computed
3601 : : * 'distinctList' is a list of SortGroupClause's representing the grouping
3602 : : * 'wtParam' is the ID of Param representing work table
3603 : : * 'numGroups' is the estimated number of groups
3604 : : *
3605 : : * For recursive UNION ALL, distinctList is empty and numGroups is zero
3606 : : */
3607 : : RecursiveUnionPath *
3608 : 638 : create_recursiveunion_path(PlannerInfo *root,
3609 : : RelOptInfo *rel,
3610 : : Path *leftpath,
3611 : : Path *rightpath,
3612 : : PathTarget *target,
3613 : : List *distinctList,
3614 : : int wtParam,
3615 : : double numGroups)
3616 : : {
3617 : 638 : RecursiveUnionPath *pathnode = makeNode(RecursiveUnionPath);
3618 : :
3619 : 638 : pathnode->path.pathtype = T_RecursiveUnion;
3620 : 638 : pathnode->path.parent = rel;
3621 : 638 : pathnode->path.pathtarget = target;
3622 : : /* For now, assume we are above any joins, so no parameterization */
3623 : 638 : pathnode->path.param_info = NULL;
3624 : 638 : pathnode->path.parallel_aware = false;
3625 : 1276 : pathnode->path.parallel_safe = rel->consider_parallel &&
3626 [ - + - - : 638 : leftpath->parallel_safe && rightpath->parallel_safe;
- - ]
3627 : : /* Foolish, but we'll do it like joins for now: */
3628 : 638 : pathnode->path.parallel_workers = leftpath->parallel_workers;
3629 : : /* RecursiveUnion result is always unsorted */
3630 : 638 : pathnode->path.pathkeys = NIL;
3631 : :
3632 : 638 : pathnode->leftpath = leftpath;
3633 : 638 : pathnode->rightpath = rightpath;
3634 : 638 : pathnode->distinctList = distinctList;
3635 : 638 : pathnode->wtParam = wtParam;
3636 : 638 : pathnode->numGroups = numGroups;
3637 : :
3638 : 638 : cost_recursive_union(&pathnode->path, leftpath, rightpath);
3639 : :
3640 : 638 : return pathnode;
3641 : : }
3642 : :
3643 : : /*
3644 : : * create_lockrows_path
3645 : : * Creates a pathnode that represents acquiring row locks
3646 : : *
3647 : : * 'rel' is the parent relation associated with the result
3648 : : * 'subpath' is the path representing the source of data
3649 : : * 'rowMarks' is a list of PlanRowMark's
3650 : : * 'epqParam' is the ID of Param for EvalPlanQual re-eval
3651 : : */
3652 : : LockRowsPath *
3653 : 6786 : create_lockrows_path(PlannerInfo *root, RelOptInfo *rel,
3654 : : Path *subpath, List *rowMarks, int epqParam)
3655 : : {
3656 : 6786 : LockRowsPath *pathnode = makeNode(LockRowsPath);
3657 : :
3658 : 6786 : pathnode->path.pathtype = T_LockRows;
3659 : 6786 : pathnode->path.parent = rel;
3660 : : /* LockRows doesn't project, so use source path's pathtarget */
3661 : 6786 : pathnode->path.pathtarget = subpath->pathtarget;
3662 : : /* For now, assume we are above any joins, so no parameterization */
3663 : 6786 : pathnode->path.param_info = NULL;
3664 : 6786 : pathnode->path.parallel_aware = false;
3665 : 6786 : pathnode->path.parallel_safe = false;
3666 : 6786 : pathnode->path.parallel_workers = 0;
3667 : 6786 : pathnode->path.rows = subpath->rows;
3668 : :
3669 : : /*
3670 : : * The result cannot be assumed sorted, since locking might cause the sort
3671 : : * key columns to be replaced with new values.
3672 : : */
3673 : 6786 : pathnode->path.pathkeys = NIL;
3674 : :
3675 : 6786 : pathnode->subpath = subpath;
3676 : 6786 : pathnode->rowMarks = rowMarks;
3677 : 6786 : pathnode->epqParam = epqParam;
3678 : :
3679 : : /*
3680 : : * We should charge something extra for the costs of row locking and
3681 : : * possible refetches, but it's hard to say how much. For now, use
3682 : : * cpu_tuple_cost per row.
3683 : : */
3684 : 6786 : pathnode->path.disabled_nodes = subpath->disabled_nodes;
3685 : 6786 : pathnode->path.startup_cost = subpath->startup_cost;
3686 : 6786 : pathnode->path.total_cost = subpath->total_cost +
3687 : 6786 : cpu_tuple_cost * subpath->rows;
3688 : :
3689 : 6786 : return pathnode;
3690 : : }
3691 : :
3692 : : /*
3693 : : * create_modifytable_path
3694 : : * Creates a pathnode that represents performing INSERT/UPDATE/DELETE/MERGE
3695 : : * mods
3696 : : *
3697 : : * 'rel' is the parent relation associated with the result
3698 : : * 'subpath' is a Path producing source data
3699 : : * 'operation' is the operation type
3700 : : * 'canSetTag' is true if we set the command tag/es_processed
3701 : : * 'nominalRelation' is the parent RT index for use of EXPLAIN
3702 : : * 'rootRelation' is the partitioned/inherited table root RTI, or 0 if none
3703 : : * 'resultRelations' is an integer list of actual RT indexes of target rel(s)
3704 : : * 'updateColnosLists' is a list of UPDATE target column number lists
3705 : : * (one sublist per rel); or NIL if not an UPDATE
3706 : : * 'withCheckOptionLists' is a list of WCO lists (one per rel)
3707 : : * 'returningLists' is a list of RETURNING tlists (one per rel)
3708 : : * 'rowMarks' is a list of PlanRowMarks (non-locking only)
3709 : : * 'onconflict' is the ON CONFLICT clause, or NULL
3710 : : * 'epqParam' is the ID of Param for EvalPlanQual re-eval
3711 : : * 'mergeActionLists' is a list of lists of MERGE actions (one per rel)
3712 : : * 'mergeJoinConditions' is a list of join conditions for MERGE (one per rel)
3713 : : */
3714 : : ModifyTablePath *
3715 : 65531 : create_modifytable_path(PlannerInfo *root, RelOptInfo *rel,
3716 : : Path *subpath,
3717 : : CmdType operation, bool canSetTag,
3718 : : Index nominalRelation, Index rootRelation,
3719 : : List *resultRelations,
3720 : : List *updateColnosLists,
3721 : : List *withCheckOptionLists, List *returningLists,
3722 : : List *rowMarks, OnConflictExpr *onconflict,
3723 : : List *mergeActionLists, List *mergeJoinConditions,
3724 : : ForPortionOfExpr *forPortionOf, int epqParam)
3725 : : {
3726 : 65531 : ModifyTablePath *pathnode = makeNode(ModifyTablePath);
3727 : :
3728 : : Assert(operation == CMD_MERGE ||
3729 : : (operation == CMD_UPDATE ?
3730 : : list_length(resultRelations) == list_length(updateColnosLists) :
3731 : : updateColnosLists == NIL));
3732 : : Assert(withCheckOptionLists == NIL ||
3733 : : list_length(resultRelations) == list_length(withCheckOptionLists));
3734 : : Assert(returningLists == NIL ||
3735 : : list_length(resultRelations) == list_length(returningLists));
3736 : :
3737 : 65531 : pathnode->path.pathtype = T_ModifyTable;
3738 : 65531 : pathnode->path.parent = rel;
3739 : : /* pathtarget is not interesting, just make it minimally valid */
3740 : 65531 : pathnode->path.pathtarget = rel->reltarget;
3741 : : /* For now, assume we are above any joins, so no parameterization */
3742 : 65531 : pathnode->path.param_info = NULL;
3743 : 65531 : pathnode->path.parallel_aware = false;
3744 : 65531 : pathnode->path.parallel_safe = false;
3745 : 65531 : pathnode->path.parallel_workers = 0;
3746 : 65531 : pathnode->path.pathkeys = NIL;
3747 : :
3748 : : /*
3749 : : * Compute cost & rowcount as subpath cost & rowcount (if RETURNING)
3750 : : *
3751 : : * Currently, we don't charge anything extra for the actual table
3752 : : * modification work, nor for the WITH CHECK OPTIONS or RETURNING
3753 : : * expressions if any. It would only be window dressing, since
3754 : : * ModifyTable is always a top-level node and there is no way for the
3755 : : * costs to change any higher-level planning choices. But we might want
3756 : : * to make it look better sometime.
3757 : : */
3758 : 65531 : pathnode->path.disabled_nodes = subpath->disabled_nodes;
3759 : 65531 : pathnode->path.startup_cost = subpath->startup_cost;
3760 : 65531 : pathnode->path.total_cost = subpath->total_cost;
3761 [ + + ]: 65531 : if (returningLists != NIL)
3762 : : {
3763 : 2606 : pathnode->path.rows = subpath->rows;
3764 : :
3765 : : /*
3766 : : * Set width to match the subpath output. XXX this is totally wrong:
3767 : : * we should return an average of the RETURNING tlist widths. But
3768 : : * it's what happened historically, and improving it is a task for
3769 : : * another day. (Again, it's mostly window dressing.)
3770 : : */
3771 : 2606 : pathnode->path.pathtarget->width = subpath->pathtarget->width;
3772 : : }
3773 : : else
3774 : : {
3775 : 62925 : pathnode->path.rows = 0;
3776 : 62925 : pathnode->path.pathtarget->width = 0;
3777 : : }
3778 : :
3779 : 65531 : pathnode->subpath = subpath;
3780 : 65531 : pathnode->operation = operation;
3781 : 65531 : pathnode->canSetTag = canSetTag;
3782 : 65531 : pathnode->nominalRelation = nominalRelation;
3783 : 65531 : pathnode->rootRelation = rootRelation;
3784 : 65531 : pathnode->resultRelations = resultRelations;
3785 : 65531 : pathnode->updateColnosLists = updateColnosLists;
3786 : 65531 : pathnode->withCheckOptionLists = withCheckOptionLists;
3787 : 65531 : pathnode->returningLists = returningLists;
3788 : 65531 : pathnode->rowMarks = rowMarks;
3789 : 65531 : pathnode->onconflict = onconflict;
3790 : 65531 : pathnode->forPortionOf = forPortionOf;
3791 : 65531 : pathnode->epqParam = epqParam;
3792 : 65531 : pathnode->mergeActionLists = mergeActionLists;
3793 : 65531 : pathnode->mergeJoinConditions = mergeJoinConditions;
3794 : :
3795 : 65531 : return pathnode;
3796 : : }
3797 : :
3798 : : /*
3799 : : * create_limit_path
3800 : : * Creates a pathnode that represents performing LIMIT/OFFSET
3801 : : *
3802 : : * In addition to providing the actual OFFSET and LIMIT expressions,
3803 : : * the caller must provide estimates of their values for costing purposes.
3804 : : * The estimates are as computed by preprocess_limit(), ie, 0 represents
3805 : : * the clause not being present, and -1 means it's present but we could
3806 : : * not estimate its value.
3807 : : *
3808 : : * 'rel' is the parent relation associated with the result
3809 : : * 'subpath' is the path representing the source of data
3810 : : * 'limitOffset' is the actual OFFSET expression, or NULL
3811 : : * 'limitCount' is the actual LIMIT expression, or NULL
3812 : : * 'offset_est' is the estimated value of the OFFSET expression
3813 : : * 'count_est' is the estimated value of the LIMIT expression
3814 : : */
3815 : : LimitPath *
3816 : 4319 : create_limit_path(PlannerInfo *root, RelOptInfo *rel,
3817 : : Path *subpath,
3818 : : Node *limitOffset, Node *limitCount,
3819 : : LimitOption limitOption,
3820 : : int64 offset_est, int64 count_est)
3821 : : {
3822 : 4319 : LimitPath *pathnode = makeNode(LimitPath);
3823 : :
3824 : 4319 : pathnode->path.pathtype = T_Limit;
3825 : 4319 : pathnode->path.parent = rel;
3826 : : /* Limit doesn't project, so use source path's pathtarget */
3827 : 4319 : pathnode->path.pathtarget = subpath->pathtarget;
3828 : : /* For now, assume we are above any joins, so no parameterization */
3829 : 4319 : pathnode->path.param_info = NULL;
3830 : 4319 : pathnode->path.parallel_aware = false;
3831 [ + + ]: 6039 : pathnode->path.parallel_safe = rel->consider_parallel &&
3832 [ + + ]: 1720 : subpath->parallel_safe;
3833 : 4319 : pathnode->path.parallel_workers = subpath->parallel_workers;
3834 : 4319 : pathnode->path.rows = subpath->rows;
3835 : 4319 : pathnode->path.disabled_nodes = subpath->disabled_nodes;
3836 : 4319 : pathnode->path.startup_cost = subpath->startup_cost;
3837 : 4319 : pathnode->path.total_cost = subpath->total_cost;
3838 : 4319 : pathnode->path.pathkeys = subpath->pathkeys;
3839 : 4319 : pathnode->subpath = subpath;
3840 : 4319 : pathnode->limitOffset = limitOffset;
3841 : 4319 : pathnode->limitCount = limitCount;
3842 : 4319 : pathnode->limitOption = limitOption;
3843 : :
3844 : : /*
3845 : : * Adjust the output rows count and costs according to the offset/limit.
3846 : : */
3847 : 4319 : adjust_limit_rows_costs(&pathnode->path.rows,
3848 : : &pathnode->path.startup_cost,
3849 : : &pathnode->path.total_cost,
3850 : : offset_est, count_est);
3851 : :
3852 : 4319 : return pathnode;
3853 : : }
3854 : :
3855 : : /*
3856 : : * adjust_limit_rows_costs
3857 : : * Adjust the size and cost estimates for a LimitPath node according to the
3858 : : * offset/limit.
3859 : : *
3860 : : * This is only a cosmetic issue if we are at top level, but if we are
3861 : : * building a subquery then it's important to report correct info to the outer
3862 : : * planner.
3863 : : *
3864 : : * When the offset or count couldn't be estimated, use 10% of the estimated
3865 : : * number of rows emitted from the subpath.
3866 : : *
3867 : : * XXX we don't bother to add eval costs of the offset/limit expressions
3868 : : * themselves to the path costs. In theory we should, but in most cases those
3869 : : * expressions are trivial and it's just not worth the trouble.
3870 : : */
3871 : : void
3872 : 4412 : adjust_limit_rows_costs(double *rows, /* in/out parameter */
3873 : : Cost *startup_cost, /* in/out parameter */
3874 : : Cost *total_cost, /* in/out parameter */
3875 : : int64 offset_est,
3876 : : int64 count_est)
3877 : : {
3878 : 4412 : double input_rows = *rows;
3879 : 4412 : Cost input_startup_cost = *startup_cost;
3880 : 4412 : Cost input_total_cost = *total_cost;
3881 : :
3882 [ + + ]: 4412 : if (offset_est != 0)
3883 : : {
3884 : : double offset_rows;
3885 : :
3886 [ + + ]: 403 : if (offset_est > 0)
3887 : 383 : offset_rows = (double) offset_est;
3888 : : else
3889 : 20 : offset_rows = clamp_row_est(input_rows * 0.10);
3890 [ + + ]: 403 : if (offset_rows > *rows)
3891 : 28 : offset_rows = *rows;
3892 [ + - ]: 403 : if (input_rows > 0)
3893 : 403 : *startup_cost +=
3894 : 403 : (input_total_cost - input_startup_cost)
3895 : 403 : * offset_rows / input_rows;
3896 : 403 : *rows -= offset_rows;
3897 [ + + ]: 403 : if (*rows < 1)
3898 : 32 : *rows = 1;
3899 : : }
3900 : :
3901 [ + + ]: 4412 : if (count_est != 0)
3902 : : {
3903 : : double count_rows;
3904 : :
3905 [ + + ]: 4356 : if (count_est > 0)
3906 : 4351 : count_rows = (double) count_est;
3907 : : else
3908 : 5 : count_rows = clamp_row_est(input_rows * 0.10);
3909 [ + + ]: 4356 : if (count_rows > *rows)
3910 : 156 : count_rows = *rows;
3911 [ + - ]: 4356 : if (input_rows > 0)
3912 : 4356 : *total_cost = *startup_cost +
3913 : 4356 : (input_total_cost - input_startup_cost)
3914 : 4356 : * count_rows / input_rows;
3915 : 4356 : *rows = count_rows;
3916 [ - + ]: 4356 : if (*rows < 1)
3917 : 0 : *rows = 1;
3918 : : }
3919 : 4412 : }
3920 : :
3921 : :
3922 : : /*
3923 : : * reparameterize_path
3924 : : * Attempt to modify a Path to have greater parameterization
3925 : : *
3926 : : * We use this to attempt to bring all child paths of an appendrel to the
3927 : : * same parameterization level, ensuring that they all enforce the same set
3928 : : * of join quals (and thus that that parameterization can be attributed to
3929 : : * an append path built from such paths). Currently, only a few path types
3930 : : * are supported here, though more could be added at need. We return NULL
3931 : : * if we can't reparameterize the given path.
3932 : : *
3933 : : * Note: we intentionally do not pass created paths to add_path(); it would
3934 : : * possibly try to delete them on the grounds of being cost-inferior to the
3935 : : * paths they were made from, and we don't want that. Paths made here are
3936 : : * not necessarily of general-purpose usefulness, but they can be useful
3937 : : * as members of an append path.
3938 : : */
3939 : : Path *
3940 : 852 : reparameterize_path(PlannerInfo *root, Path *path,
3941 : : Relids required_outer,
3942 : : double loop_count)
3943 : : {
3944 : 852 : RelOptInfo *rel = path->parent;
3945 : :
3946 : : /* Can only increase, not decrease, path's parameterization */
3947 [ - + - + ]: 852 : if (!bms_is_subset(PATH_REQ_OUTER(path), required_outer))
3948 : 0 : return NULL;
3949 [ + - - - : 852 : switch (path->pathtype)
- + + - -
+ ]
3950 : : {
3951 : 724 : case T_SeqScan:
3952 : 724 : return create_seqscan_path(root, rel, required_outer, 0);
3953 : 0 : case T_SampleScan:
3954 : 0 : return create_samplescan_path(root, rel, required_outer);
3955 : 0 : case T_IndexScan:
3956 : : case T_IndexOnlyScan:
3957 : : {
3958 : 0 : IndexPath *ipath = (IndexPath *) path;
3959 : 0 : IndexPath *newpath = makeNode(IndexPath);
3960 : :
3961 : : /*
3962 : : * We can't use create_index_path directly, and would not want
3963 : : * to because it would re-compute the indexqual conditions
3964 : : * which is wasted effort. Instead we hack things a bit:
3965 : : * flat-copy the path node, revise its param_info, and redo
3966 : : * the cost estimate.
3967 : : */
3968 : 0 : memcpy(newpath, ipath, sizeof(IndexPath));
3969 : 0 : newpath->path.param_info =
3970 : 0 : get_baserel_parampathinfo(root, rel, required_outer);
3971 : 0 : cost_index(newpath, root, loop_count, false);
3972 : 0 : return (Path *) newpath;
3973 : : }
3974 : 0 : case T_BitmapHeapScan:
3975 : : {
3976 : 0 : BitmapHeapPath *bpath = (BitmapHeapPath *) path;
3977 : :
3978 : 0 : return (Path *) create_bitmap_heap_path(root,
3979 : : rel,
3980 : : bpath->bitmapqual,
3981 : : required_outer,
3982 : : loop_count, 0);
3983 : : }
3984 : 0 : case T_SubqueryScan:
3985 : : {
3986 : 0 : SubqueryScanPath *spath = (SubqueryScanPath *) path;
3987 : 0 : Path *subpath = spath->subpath;
3988 : : bool trivial_pathtarget;
3989 : :
3990 : : /*
3991 : : * If existing node has zero extra cost, we must have decided
3992 : : * its target is trivial. (The converse is not true, because
3993 : : * it might have a trivial target but quals to enforce; but in
3994 : : * that case the new node will too, so it doesn't matter
3995 : : * whether we get the right answer here.)
3996 : : */
3997 : 0 : trivial_pathtarget =
3998 : 0 : (subpath->total_cost == spath->path.total_cost);
3999 : :
4000 : 0 : return (Path *) create_subqueryscan_path(root,
4001 : : rel,
4002 : : subpath,
4003 : : trivial_pathtarget,
4004 : : spath->path.pathkeys,
4005 : : required_outer);
4006 : : }
4007 : 65 : case T_Result:
4008 : : /* Supported only for RTE_RESULT scan paths */
4009 [ + - ]: 65 : if (IsA(path, Path))
4010 : 65 : return create_resultscan_path(root, rel, required_outer);
4011 : 0 : break;
4012 : 5 : case T_Append:
4013 : : {
4014 : 5 : AppendPath *apath = (AppendPath *) path;
4015 : 5 : AppendPathInput new_append = {0};
4016 : : int i;
4017 : : ListCell *lc;
4018 : :
4019 : 5 : new_append.child_append_relid_sets = apath->child_append_relid_sets;
4020 : :
4021 : : /* Reparameterize the children */
4022 : 5 : i = 0;
4023 [ + - + + : 10 : foreach(lc, apath->subpaths)
+ + ]
4024 : : {
4025 : 5 : Path *spath = (Path *) lfirst(lc);
4026 : :
4027 : 5 : spath = reparameterize_path(root, spath,
4028 : : required_outer,
4029 : : loop_count);
4030 [ - + ]: 5 : if (spath == NULL)
4031 : 0 : return NULL;
4032 : : /* We have to re-split the regular and partial paths */
4033 [ + - ]: 5 : if (i < apath->first_partial_path)
4034 : 5 : new_append.subpaths = lappend(new_append.subpaths, spath);
4035 : : else
4036 : 0 : new_append.partial_subpaths = lappend(new_append.partial_subpaths, spath);
4037 : 5 : i++;
4038 : : }
4039 : 5 : return (Path *)
4040 : 5 : create_append_path(root, rel, new_append,
4041 : : apath->path.pathkeys, required_outer,
4042 : : apath->path.parallel_workers,
4043 : 5 : apath->path.parallel_aware,
4044 : : -1);
4045 : : }
4046 : 0 : case T_Material:
4047 : : {
4048 : 0 : MaterialPath *mpath = (MaterialPath *) path;
4049 : 0 : Path *spath = mpath->subpath;
4050 : : bool enabled;
4051 : :
4052 : 0 : spath = reparameterize_path(root, spath,
4053 : : required_outer,
4054 : : loop_count);
4055 [ # # ]: 0 : if (spath == NULL)
4056 : 0 : return NULL;
4057 : 0 : enabled =
4058 : 0 : (mpath->path.disabled_nodes <= spath->disabled_nodes);
4059 : 0 : return (Path *) create_material_path(rel, spath, enabled);
4060 : : }
4061 : 0 : case T_Memoize:
4062 : : {
4063 : 0 : MemoizePath *mpath = (MemoizePath *) path;
4064 : 0 : Path *spath = mpath->subpath;
4065 : :
4066 : 0 : spath = reparameterize_path(root, spath,
4067 : : required_outer,
4068 : : loop_count);
4069 [ # # ]: 0 : if (spath == NULL)
4070 : 0 : return NULL;
4071 : 0 : return (Path *) create_memoize_path(root, rel,
4072 : : spath,
4073 : : mpath->param_exprs,
4074 : : mpath->hash_operators,
4075 : 0 : mpath->singlerow,
4076 : 0 : mpath->binary_mode,
4077 : : mpath->est_calls);
4078 : : }
4079 : 58 : default:
4080 : 58 : break;
4081 : : }
4082 : 58 : return NULL;
4083 : : }
4084 : :
4085 : : /*
4086 : : * reparameterize_path_by_child
4087 : : * Given a path parameterized by the parent of the given child relation,
4088 : : * translate the path to be parameterized by the given child relation.
4089 : : *
4090 : : * Most fields in the path are not changed, but any expressions must be
4091 : : * adjusted to refer to the correct varnos, and any subpaths must be
4092 : : * recursively reparameterized. Other fields that refer to specific relids
4093 : : * also need adjustment.
4094 : : *
4095 : : * The cost, number of rows, width and parallel path properties depend upon
4096 : : * path->parent, which does not change during the translation. So we need
4097 : : * not change those.
4098 : : *
4099 : : * Currently, only a few path types are supported here, though more could be
4100 : : * added at need. We return NULL if we can't reparameterize the given path.
4101 : : *
4102 : : * Note that this function can change referenced RangeTblEntries, RelOptInfos
4103 : : * and IndexOptInfos as well as the Path structures. Therefore, it's only safe
4104 : : * to call during create_plan(), when we have made a final choice of which Path
4105 : : * to use for each RangeTblEntry/RelOptInfo/IndexOptInfo.
4106 : : *
4107 : : * Keep this code in sync with path_is_reparameterizable_by_child()!
4108 : : */
4109 : : Path *
4110 : 73024 : reparameterize_path_by_child(PlannerInfo *root, Path *path,
4111 : : RelOptInfo *child_rel)
4112 : : {
4113 : : Path *new_path;
4114 : : ParamPathInfo *new_ppi;
4115 : : ParamPathInfo *old_ppi;
4116 : : Relids required_outer;
4117 : :
4118 : : #define ADJUST_CHILD_ATTRS(node) \
4119 : : ((node) = (void *) adjust_appendrel_attrs_multilevel(root, \
4120 : : (Node *) (node), \
4121 : : child_rel, \
4122 : : child_rel->top_parent))
4123 : :
4124 : : #define REPARAMETERIZE_CHILD_PATH(path) \
4125 : : do { \
4126 : : (path) = reparameterize_path_by_child(root, (path), child_rel); \
4127 : : if ((path) == NULL) \
4128 : : return NULL; \
4129 : : } while(0)
4130 : :
4131 : : #define REPARAMETERIZE_CHILD_PATH_LIST(pathlist) \
4132 : : do { \
4133 : : if ((pathlist) != NIL) \
4134 : : { \
4135 : : (pathlist) = reparameterize_pathlist_by_child(root, (pathlist), \
4136 : : child_rel); \
4137 : : if ((pathlist) == NIL) \
4138 : : return NULL; \
4139 : : } \
4140 : : } while(0)
4141 : :
4142 : : /*
4143 : : * If the path is not parameterized by the parent of the given relation,
4144 : : * it doesn't need reparameterization.
4145 : : */
4146 [ + + ]: 73024 : if (!path->param_info ||
4147 [ + - + + ]: 36868 : !bms_overlap(PATH_REQ_OUTER(path), child_rel->top_parent_relids))
4148 : 72177 : return path;
4149 : :
4150 : : /*
4151 : : * If possible, reparameterize the given path.
4152 : : *
4153 : : * This function is currently only applied to the inner side of a nestloop
4154 : : * join that is being partitioned by the partitionwise-join code. Hence,
4155 : : * we need only support path types that plausibly arise in that context.
4156 : : * (In particular, supporting sorted path types would be a waste of code
4157 : : * and cycles: even if we translated them here, they'd just lose in
4158 : : * subsequent cost comparisons.) If we do see an unsupported path type,
4159 : : * that just means we won't be able to generate a partitionwise-join plan
4160 : : * using that path type.
4161 : : */
4162 [ + + + + : 847 : switch (nodeTag(path))
+ - - + -
+ + - + -
- ]
4163 : : {
4164 : 190 : case T_Path:
4165 : 190 : new_path = path;
4166 : 190 : ADJUST_CHILD_ATTRS(new_path->parent->baserestrictinfo);
4167 [ + + ]: 190 : if (path->pathtype == T_SampleScan)
4168 : : {
4169 : 40 : Index scan_relid = path->parent->relid;
4170 : : RangeTblEntry *rte;
4171 : :
4172 : : /* it should be a base rel with a tablesample clause... */
4173 : : Assert(scan_relid > 0);
4174 [ + - ]: 40 : rte = planner_rt_fetch(scan_relid, root);
4175 : : Assert(rte->rtekind == RTE_RELATION);
4176 : : Assert(rte->tablesample != NULL);
4177 : :
4178 : 40 : ADJUST_CHILD_ATTRS(rte->tablesample);
4179 : : }
4180 : 190 : break;
4181 : :
4182 : 447 : case T_IndexPath:
4183 : : {
4184 : 447 : IndexPath *ipath = (IndexPath *) path;
4185 : :
4186 : 447 : ADJUST_CHILD_ATTRS(ipath->indexinfo->indrestrictinfo);
4187 : 447 : ADJUST_CHILD_ATTRS(ipath->indexclauses);
4188 : 447 : new_path = (Path *) ipath;
4189 : : }
4190 : 447 : break;
4191 : :
4192 : 40 : case T_BitmapHeapPath:
4193 : : {
4194 : 40 : BitmapHeapPath *bhpath = (BitmapHeapPath *) path;
4195 : :
4196 : 40 : ADJUST_CHILD_ATTRS(bhpath->path.parent->baserestrictinfo);
4197 [ - + ]: 40 : REPARAMETERIZE_CHILD_PATH(bhpath->bitmapqual);
4198 : 40 : new_path = (Path *) bhpath;
4199 : : }
4200 : 40 : break;
4201 : :
4202 : 20 : case T_BitmapAndPath:
4203 : : {
4204 : 20 : BitmapAndPath *bapath = (BitmapAndPath *) path;
4205 : :
4206 [ + - - + ]: 20 : REPARAMETERIZE_CHILD_PATH_LIST(bapath->bitmapquals);
4207 : 20 : new_path = (Path *) bapath;
4208 : : }
4209 : 20 : break;
4210 : :
4211 : 20 : case T_BitmapOrPath:
4212 : : {
4213 : 20 : BitmapOrPath *bopath = (BitmapOrPath *) path;
4214 : :
4215 [ + - - + ]: 20 : REPARAMETERIZE_CHILD_PATH_LIST(bopath->bitmapquals);
4216 : 20 : new_path = (Path *) bopath;
4217 : : }
4218 : 20 : break;
4219 : :
4220 : 0 : case T_ForeignPath:
4221 : : {
4222 : 0 : ForeignPath *fpath = (ForeignPath *) path;
4223 : : ReparameterizeForeignPathByChild_function rfpc_func;
4224 : :
4225 : 0 : ADJUST_CHILD_ATTRS(fpath->path.parent->baserestrictinfo);
4226 [ # # ]: 0 : if (fpath->fdw_outerpath)
4227 [ # # ]: 0 : REPARAMETERIZE_CHILD_PATH(fpath->fdw_outerpath);
4228 [ # # ]: 0 : if (fpath->fdw_restrictinfo)
4229 : 0 : ADJUST_CHILD_ATTRS(fpath->fdw_restrictinfo);
4230 : :
4231 : : /* Hand over to FDW if needed. */
4232 : 0 : rfpc_func =
4233 : 0 : path->parent->fdwroutine->ReparameterizeForeignPathByChild;
4234 [ # # ]: 0 : if (rfpc_func)
4235 : 0 : fpath->fdw_private = rfpc_func(root, fpath->fdw_private,
4236 : : child_rel);
4237 : 0 : new_path = (Path *) fpath;
4238 : : }
4239 : 0 : break;
4240 : :
4241 : 0 : case T_CustomPath:
4242 : : {
4243 : 0 : CustomPath *cpath = (CustomPath *) path;
4244 : :
4245 : 0 : ADJUST_CHILD_ATTRS(cpath->path.parent->baserestrictinfo);
4246 [ # # # # ]: 0 : REPARAMETERIZE_CHILD_PATH_LIST(cpath->custom_paths);
4247 [ # # ]: 0 : if (cpath->custom_restrictinfo)
4248 : 0 : ADJUST_CHILD_ATTRS(cpath->custom_restrictinfo);
4249 [ # # ]: 0 : if (cpath->methods &&
4250 [ # # ]: 0 : cpath->methods->ReparameterizeCustomPathByChild)
4251 : 0 : cpath->custom_private =
4252 : 0 : cpath->methods->ReparameterizeCustomPathByChild(root,
4253 : : cpath->custom_private,
4254 : : child_rel);
4255 : 0 : new_path = (Path *) cpath;
4256 : : }
4257 : 0 : break;
4258 : :
4259 : 30 : case T_NestPath:
4260 : : {
4261 : 30 : NestPath *npath = (NestPath *) path;
4262 : 30 : JoinPath *jpath = (JoinPath *) npath;
4263 : :
4264 [ - + ]: 30 : REPARAMETERIZE_CHILD_PATH(jpath->outerjoinpath);
4265 [ - + ]: 30 : REPARAMETERIZE_CHILD_PATH(jpath->innerjoinpath);
4266 : 30 : ADJUST_CHILD_ATTRS(jpath->joinrestrictinfo);
4267 : 30 : new_path = (Path *) npath;
4268 : : }
4269 : 30 : break;
4270 : :
4271 : 0 : case T_MergePath:
4272 : : {
4273 : 0 : MergePath *mpath = (MergePath *) path;
4274 : 0 : JoinPath *jpath = (JoinPath *) mpath;
4275 : :
4276 [ # # ]: 0 : REPARAMETERIZE_CHILD_PATH(jpath->outerjoinpath);
4277 [ # # ]: 0 : REPARAMETERIZE_CHILD_PATH(jpath->innerjoinpath);
4278 : 0 : ADJUST_CHILD_ATTRS(jpath->joinrestrictinfo);
4279 : 0 : ADJUST_CHILD_ATTRS(mpath->path_mergeclauses);
4280 : 0 : new_path = (Path *) mpath;
4281 : : }
4282 : 0 : break;
4283 : :
4284 : 40 : case T_HashPath:
4285 : : {
4286 : 40 : HashPath *hpath = (HashPath *) path;
4287 : 40 : JoinPath *jpath = (JoinPath *) hpath;
4288 : :
4289 [ - + ]: 40 : REPARAMETERIZE_CHILD_PATH(jpath->outerjoinpath);
4290 [ - + ]: 40 : REPARAMETERIZE_CHILD_PATH(jpath->innerjoinpath);
4291 : 40 : ADJUST_CHILD_ATTRS(jpath->joinrestrictinfo);
4292 : 40 : ADJUST_CHILD_ATTRS(hpath->path_hashclauses);
4293 : 40 : new_path = (Path *) hpath;
4294 : : }
4295 : 40 : break;
4296 : :
4297 : 20 : case T_AppendPath:
4298 : : {
4299 : 20 : AppendPath *apath = (AppendPath *) path;
4300 : :
4301 [ + - - + ]: 20 : REPARAMETERIZE_CHILD_PATH_LIST(apath->subpaths);
4302 : 20 : new_path = (Path *) apath;
4303 : : }
4304 : 20 : break;
4305 : :
4306 : 0 : case T_MaterialPath:
4307 : : {
4308 : 0 : MaterialPath *mpath = (MaterialPath *) path;
4309 : :
4310 [ # # ]: 0 : REPARAMETERIZE_CHILD_PATH(mpath->subpath);
4311 : 0 : new_path = (Path *) mpath;
4312 : : }
4313 : 0 : break;
4314 : :
4315 : 40 : case T_MemoizePath:
4316 : : {
4317 : 40 : MemoizePath *mpath = (MemoizePath *) path;
4318 : :
4319 [ - + ]: 40 : REPARAMETERIZE_CHILD_PATH(mpath->subpath);
4320 : 40 : ADJUST_CHILD_ATTRS(mpath->param_exprs);
4321 : 40 : new_path = (Path *) mpath;
4322 : : }
4323 : 40 : break;
4324 : :
4325 : 0 : case T_GatherPath:
4326 : : {
4327 : 0 : GatherPath *gpath = (GatherPath *) path;
4328 : :
4329 [ # # ]: 0 : REPARAMETERIZE_CHILD_PATH(gpath->subpath);
4330 : 0 : new_path = (Path *) gpath;
4331 : : }
4332 : 0 : break;
4333 : :
4334 : 0 : default:
4335 : : /* We don't know how to reparameterize this path. */
4336 : 0 : return NULL;
4337 : : }
4338 : :
4339 : : /*
4340 : : * Adjust the parameterization information, which refers to the topmost
4341 : : * parent. The topmost parent can be multiple levels away from the given
4342 : : * child, hence use multi-level expression adjustment routines.
4343 : : */
4344 : 847 : old_ppi = new_path->param_info;
4345 : : required_outer =
4346 : 847 : adjust_child_relids_multilevel(root, old_ppi->ppi_req_outer,
4347 : : child_rel,
4348 : 847 : child_rel->top_parent);
4349 : :
4350 : : /* If we already have a PPI for this parameterization, just return it */
4351 : 847 : new_ppi = find_param_path_info(new_path->parent, required_outer);
4352 : :
4353 : : /*
4354 : : * If not, build a new one and link it to the list of PPIs. For the same
4355 : : * reason as explained in mark_dummy_rel(), allocate new PPI in the same
4356 : : * context the given RelOptInfo is in.
4357 : : */
4358 [ + + ]: 847 : if (new_ppi == NULL)
4359 : : {
4360 : : MemoryContext oldcontext;
4361 : 727 : RelOptInfo *rel = path->parent;
4362 : :
4363 : 727 : oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
4364 : :
4365 : 727 : new_ppi = makeNode(ParamPathInfo);
4366 : 727 : new_ppi->ppi_req_outer = bms_copy(required_outer);
4367 : 727 : new_ppi->ppi_rows = old_ppi->ppi_rows;
4368 : 727 : new_ppi->ppi_clauses = old_ppi->ppi_clauses;
4369 : 727 : ADJUST_CHILD_ATTRS(new_ppi->ppi_clauses);
4370 : 727 : new_ppi->ppi_serials = bms_copy(old_ppi->ppi_serials);
4371 : 727 : rel->ppilist = lappend(rel->ppilist, new_ppi);
4372 : :
4373 : 727 : MemoryContextSwitchTo(oldcontext);
4374 : : }
4375 : 847 : bms_free(required_outer);
4376 : :
4377 : 847 : new_path->param_info = new_ppi;
4378 : :
4379 : : /*
4380 : : * Adjust the path target if the parent of the outer relation is
4381 : : * referenced in the targetlist. This can happen when only the parent of
4382 : : * outer relation is laterally referenced in this relation.
4383 : : */
4384 [ + + ]: 847 : if (bms_overlap(path->parent->lateral_relids,
4385 : 847 : child_rel->top_parent_relids))
4386 : : {
4387 : 400 : new_path->pathtarget = copy_pathtarget(new_path->pathtarget);
4388 : 400 : ADJUST_CHILD_ATTRS(new_path->pathtarget->exprs);
4389 : : }
4390 : :
4391 : 847 : return new_path;
4392 : : }
4393 : :
4394 : : /*
4395 : : * path_is_reparameterizable_by_child
4396 : : * Given a path parameterized by the parent of the given child relation,
4397 : : * see if it can be translated to be parameterized by the child relation.
4398 : : *
4399 : : * This must return true if and only if reparameterize_path_by_child()
4400 : : * would succeed on this path. Currently it's sufficient to verify that
4401 : : * the path and all of its subpaths (if any) are of the types handled by
4402 : : * that function. However, subpaths that are not parameterized can be
4403 : : * disregarded since they won't require translation.
4404 : : */
4405 : : bool
4406 : 27929 : path_is_reparameterizable_by_child(Path *path, RelOptInfo *child_rel)
4407 : : {
4408 : : #define REJECT_IF_PATH_NOT_REPARAMETERIZABLE(path) \
4409 : : do { \
4410 : : if (!path_is_reparameterizable_by_child(path, child_rel)) \
4411 : : return false; \
4412 : : } while(0)
4413 : :
4414 : : #define REJECT_IF_PATH_LIST_NOT_REPARAMETERIZABLE(pathlist) \
4415 : : do { \
4416 : : if (!pathlist_is_reparameterizable_by_child(pathlist, child_rel)) \
4417 : : return false; \
4418 : : } while(0)
4419 : :
4420 : : /*
4421 : : * If the path is not parameterized by the parent of the given relation,
4422 : : * it doesn't need reparameterization.
4423 : : */
4424 [ + + ]: 27929 : if (!path->param_info ||
4425 [ + - + + ]: 27553 : !bms_overlap(PATH_REQ_OUTER(path), child_rel->top_parent_relids))
4426 : 784 : return true;
4427 : :
4428 : : /*
4429 : : * Check that the path type is one that reparameterize_path_by_child() can
4430 : : * handle, and recursively check subpaths.
4431 : : */
4432 [ + + + + : 27145 : switch (nodeTag(path))
+ - + + -
+ - - ]
4433 : : {
4434 : 18911 : case T_Path:
4435 : : case T_IndexPath:
4436 : 18911 : break;
4437 : :
4438 : 40 : case T_BitmapHeapPath:
4439 : : {
4440 : 40 : BitmapHeapPath *bhpath = (BitmapHeapPath *) path;
4441 : :
4442 [ - + ]: 40 : REJECT_IF_PATH_NOT_REPARAMETERIZABLE(bhpath->bitmapqual);
4443 : : }
4444 : 40 : break;
4445 : :
4446 : 20 : case T_BitmapAndPath:
4447 : : {
4448 : 20 : BitmapAndPath *bapath = (BitmapAndPath *) path;
4449 : :
4450 [ - + ]: 20 : REJECT_IF_PATH_LIST_NOT_REPARAMETERIZABLE(bapath->bitmapquals);
4451 : : }
4452 : 20 : break;
4453 : :
4454 : 20 : case T_BitmapOrPath:
4455 : : {
4456 : 20 : BitmapOrPath *bopath = (BitmapOrPath *) path;
4457 : :
4458 [ - + ]: 20 : REJECT_IF_PATH_LIST_NOT_REPARAMETERIZABLE(bopath->bitmapquals);
4459 : : }
4460 : 20 : break;
4461 : :
4462 : 74 : case T_ForeignPath:
4463 : : {
4464 : 74 : ForeignPath *fpath = (ForeignPath *) path;
4465 : :
4466 [ - + ]: 74 : if (fpath->fdw_outerpath)
4467 [ # # ]: 0 : REJECT_IF_PATH_NOT_REPARAMETERIZABLE(fpath->fdw_outerpath);
4468 : : }
4469 : 74 : break;
4470 : :
4471 : 0 : case T_CustomPath:
4472 : : {
4473 : 0 : CustomPath *cpath = (CustomPath *) path;
4474 : :
4475 [ # # ]: 0 : REJECT_IF_PATH_LIST_NOT_REPARAMETERIZABLE(cpath->custom_paths);
4476 : : }
4477 : 0 : break;
4478 : :
4479 : 1004 : case T_NestPath:
4480 : : case T_MergePath:
4481 : : case T_HashPath:
4482 : : {
4483 : 1004 : JoinPath *jpath = (JoinPath *) path;
4484 : :
4485 [ - + ]: 1004 : REJECT_IF_PATH_NOT_REPARAMETERIZABLE(jpath->outerjoinpath);
4486 [ - + ]: 1004 : REJECT_IF_PATH_NOT_REPARAMETERIZABLE(jpath->innerjoinpath);
4487 : : }
4488 : 1004 : break;
4489 : :
4490 : 160 : case T_AppendPath:
4491 : : {
4492 : 160 : AppendPath *apath = (AppendPath *) path;
4493 : :
4494 [ - + ]: 160 : REJECT_IF_PATH_LIST_NOT_REPARAMETERIZABLE(apath->subpaths);
4495 : : }
4496 : 160 : break;
4497 : :
4498 : 0 : case T_MaterialPath:
4499 : : {
4500 : 0 : MaterialPath *mpath = (MaterialPath *) path;
4501 : :
4502 [ # # ]: 0 : REJECT_IF_PATH_NOT_REPARAMETERIZABLE(mpath->subpath);
4503 : : }
4504 : 0 : break;
4505 : :
4506 : 6916 : case T_MemoizePath:
4507 : : {
4508 : 6916 : MemoizePath *mpath = (MemoizePath *) path;
4509 : :
4510 [ - + ]: 6916 : REJECT_IF_PATH_NOT_REPARAMETERIZABLE(mpath->subpath);
4511 : : }
4512 : 6916 : break;
4513 : :
4514 : 0 : case T_GatherPath:
4515 : : {
4516 : 0 : GatherPath *gpath = (GatherPath *) path;
4517 : :
4518 [ # # ]: 0 : REJECT_IF_PATH_NOT_REPARAMETERIZABLE(gpath->subpath);
4519 : : }
4520 : 0 : break;
4521 : :
4522 : 0 : default:
4523 : : /* We don't know how to reparameterize this path. */
4524 : 0 : return false;
4525 : : }
4526 : :
4527 : 27145 : return true;
4528 : : }
4529 : :
4530 : : /*
4531 : : * reparameterize_pathlist_by_child
4532 : : * Helper function to reparameterize a list of paths by given child rel.
4533 : : *
4534 : : * Returns NIL to indicate failure, so pathlist had better not be NIL.
4535 : : */
4536 : : static List *
4537 : 60 : reparameterize_pathlist_by_child(PlannerInfo *root,
4538 : : List *pathlist,
4539 : : RelOptInfo *child_rel)
4540 : : {
4541 : : ListCell *lc;
4542 : 60 : List *result = NIL;
4543 : :
4544 [ + - + + : 180 : foreach(lc, pathlist)
+ + ]
4545 : : {
4546 : 120 : Path *path = reparameterize_path_by_child(root, lfirst(lc),
4547 : : child_rel);
4548 : :
4549 [ - + ]: 120 : if (path == NULL)
4550 : : {
4551 : 0 : list_free(result);
4552 : 0 : return NIL;
4553 : : }
4554 : :
4555 : 120 : result = lappend(result, path);
4556 : : }
4557 : :
4558 : 60 : return result;
4559 : : }
4560 : :
4561 : : /*
4562 : : * pathlist_is_reparameterizable_by_child
4563 : : * Helper function to check if a list of paths can be reparameterized.
4564 : : */
4565 : : static bool
4566 : 200 : pathlist_is_reparameterizable_by_child(List *pathlist, RelOptInfo *child_rel)
4567 : : {
4568 : : ListCell *lc;
4569 : :
4570 [ + - + + : 600 : foreach(lc, pathlist)
+ + ]
4571 : : {
4572 : 400 : Path *path = (Path *) lfirst(lc);
4573 : :
4574 [ - + ]: 400 : if (!path_is_reparameterizable_by_child(path, child_rel))
4575 : 0 : return false;
4576 : : }
4577 : :
4578 : 200 : return true;
4579 : : }
|