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 : 929578 : compare_path_costs(Path *path1, Path *path2, CostSelector criterion)
69 : : {
70 : : /* Number of disabled nodes, if different, trumps all else. */
71 [ + + ]: 929578 : if (unlikely(path1->disabled_nodes != path2->disabled_nodes))
72 : : {
73 [ + + ]: 81662 : if (path1->disabled_nodes < path2->disabled_nodes)
74 : 81654 : return -1;
75 : : else
76 : 8 : return +1;
77 : : }
78 : :
79 [ + + ]: 847916 : if (criterion == STARTUP_COST)
80 : : {
81 [ + + ]: 431742 : if (path1->startup_cost < path2->startup_cost)
82 : 258900 : return -1;
83 [ + + ]: 172842 : if (path1->startup_cost > path2->startup_cost)
84 : 83876 : return +1;
85 : :
86 : : /*
87 : : * If paths have the same startup cost (not at all unlikely), order
88 : : * them by total cost.
89 : : */
90 [ + + ]: 88966 : if (path1->total_cost < path2->total_cost)
91 : 43737 : return -1;
92 [ + + ]: 45229 : if (path1->total_cost > path2->total_cost)
93 : 4443 : return +1;
94 : : }
95 : : else
96 : : {
97 [ + + ]: 416174 : if (path1->total_cost < path2->total_cost)
98 : 388212 : return -1;
99 [ + + ]: 27962 : if (path1->total_cost > path2->total_cost)
100 : 7401 : return +1;
101 : :
102 : : /*
103 : : * If paths have the same total cost, order them by startup cost.
104 : : */
105 [ + + ]: 20561 : if (path1->startup_cost < path2->startup_cost)
106 : 1509 : return -1;
107 [ + + ]: 19052 : if (path1->startup_cost > path2->startup_cost)
108 : 75 : return +1;
109 : : }
110 : 59763 : 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 : 3709 : 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 [ + + ]: 3709 : 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 [ + - + + ]: 3667 : if (fraction <= 0.0 || fraction >= 1.0)
139 : 965 : return compare_path_costs(path1, path2, TOTAL_COST);
140 : 2702 : cost1 = path1->startup_cost +
141 : 2702 : fraction * (path1->total_cost - path1->startup_cost);
142 : 2702 : cost2 = path2->startup_cost +
143 : 2702 : fraction * (path2->total_cost - path2->startup_cost);
144 [ + + ]: 2702 : if (cost1 < cost2)
145 : 2280 : return -1;
146 [ + - ]: 422 : if (cost1 > cost2)
147 : 422 : 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 : 3883132 : 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 [ + + ]: 3883132 : if (unlikely(path1->disabled_nodes != path2->disabled_nodes))
188 : : {
189 [ + + ]: 188703 : if (path1->disabled_nodes < path2->disabled_nodes)
190 : 66931 : return COSTS_BETTER1;
191 : : else
192 : 121772 : 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 [ + + ]: 3694429 : if (path1->total_cost > path2->total_cost * fuzz_factor)
200 : : {
201 : : /* path1 fuzzily worse on total cost */
202 [ + + + + ]: 1908517 : if (CONSIDER_PATH_STARTUP_COST(path1) &&
203 [ + + ]: 89914 : path2->startup_cost > path1->startup_cost * fuzz_factor)
204 : : {
205 : : /* ... but path2 fuzzily worse on startup, so DIFFERENT */
206 : 47848 : return COSTS_DIFFERENT;
207 : : }
208 : : /* else path2 dominates */
209 : 1860669 : return COSTS_BETTER2;
210 : : }
211 [ + + ]: 1785912 : if (path2->total_cost > path1->total_cost * fuzz_factor)
212 : : {
213 : : /* path2 fuzzily worse on total cost */
214 [ + + + + ]: 895570 : if (CONSIDER_PATH_STARTUP_COST(path2) &&
215 [ + + ]: 34354 : path1->startup_cost > path2->startup_cost * fuzz_factor)
216 : : {
217 : : /* ... but path1 fuzzily worse on startup, so DIFFERENT */
218 : 20823 : return COSTS_DIFFERENT;
219 : : }
220 : : /* else path1 dominates */
221 : 874747 : return COSTS_BETTER1;
222 : : }
223 : : /* fuzzily the same on total cost ... */
224 [ + + ]: 890342 : if (path1->startup_cost > path2->startup_cost * fuzz_factor)
225 : : {
226 : : /* ... but path1 fuzzily worse on startup, so path2 wins */
227 : 285870 : return COSTS_BETTER2;
228 : : }
229 [ + + ]: 604472 : if (path2->startup_cost > path1->startup_cost * fuzz_factor)
230 : : {
231 : : /* ... but path2 fuzzily worse on startup, so path1 wins */
232 : 46591 : return COSTS_BETTER1;
233 : : }
234 : : /* fuzzily the same on both costs */
235 : 557881 : 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 : 1627559 : 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 [ - + ]: 1627559 : if (parent_rel->pathlist == NIL)
279 [ # # ]: 0 : elog(ERROR, "could not devise a query plan for the given query");
280 : :
281 : 1627559 : cheapest_startup_path = cheapest_total_path = best_param_path = NULL;
282 : 1627559 : parameterized_paths = NIL;
283 : :
284 [ + - + + : 3738423 : foreach(p, parent_rel->pathlist)
+ + ]
285 : : {
286 : 2110864 : Path *path = (Path *) lfirst(p);
287 : : int cmp;
288 : :
289 [ + + ]: 2110864 : if (path->param_info)
290 : : {
291 : : /* Parameterized path, so add it to parameterized_paths */
292 : 113899 : 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 [ + + ]: 113899 : if (cheapest_total_path)
299 : 26129 : 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 [ + + ]: 87770 : if (best_param_path == NULL)
307 : 78320 : best_param_path = path;
308 : : else
309 : : {
310 [ + - + + : 9450 : switch (bms_subset_compare(PATH_REQ_OUTER(path),
+ + - ]
311 [ + - ]: 9450 : 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 : 985 : case BMS_SUBSET1:
320 : : /* new path is less-parameterized */
321 : 985 : best_param_path = path;
322 : 985 : break;
323 : 20 : case BMS_SUBSET2:
324 : : /* old path is less-parameterized, keep it */
325 : 20 : break;
326 : 8400 : 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 : 8400 : break;
334 : : }
335 : : }
336 : : }
337 : : else
338 : : {
339 : : /* Unparameterized path, so consider it for cheapest slots */
340 [ + + ]: 1996965 : if (cheapest_total_path == NULL)
341 : : {
342 : 1618801 : cheapest_startup_path = cheapest_total_path = path;
343 : 1618801 : 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 : 378164 : cmp = compare_path_costs(cheapest_startup_path, path, STARTUP_COST);
354 [ + + + + ]: 378164 : if (cmp > 0 ||
355 [ - + ]: 1394 : (cmp == 0 &&
356 : 1394 : compare_pathkeys(cheapest_startup_path->pathkeys,
357 : : path->pathkeys) == PATHKEYS_BETTER2))
358 : 62111 : cheapest_startup_path = path;
359 : :
360 : 378164 : cmp = compare_path_costs(cheapest_total_path, path, TOTAL_COST);
361 [ + - + + ]: 378164 : 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 [ + + ]: 1627559 : if (cheapest_total_path)
371 : 1618801 : 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 [ + + ]: 1627559 : if (cheapest_total_path == NULL)
378 : 8758 : cheapest_total_path = best_param_path;
379 : : Assert(cheapest_total_path != NULL);
380 : :
381 : 1627559 : parent_rel->cheapest_startup_path = cheapest_startup_path;
382 : 1627559 : parent_rel->cheapest_total_path = cheapest_total_path;
383 : 1627559 : parent_rel->cheapest_parameterized_paths = parameterized_paths;
384 : 1627559 : }
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 : 3663754 : add_path(RelOptInfo *parent_rel, Path *new_path)
460 : : {
461 : 3663754 : bool accept_new = true; /* unless we find a superior old path */
462 : 3663754 : 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 [ + + ]: 3663754 : CHECK_FOR_INTERRUPTS();
471 : :
472 : : /* Pretend parameterized paths have no pathkeys, per comment above */
473 [ + + ]: 3663754 : 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 [ + + + + : 5646082 : foreach(p1, parent_rel->pathlist)
+ + ]
481 : : {
482 : 3366896 : Path *old_path = (Path *) lfirst(p1);
483 : 3366896 : 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 : 3366896 : 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 [ + + ]: 3366896 : if (costcmp != COSTS_DIFFERENT)
506 : : {
507 : : /* Similarly check to see if either dominates on pathkeys */
508 : : List *old_path_pathkeys;
509 : :
510 [ + + ]: 3298680 : old_path_pathkeys = old_path->param_info ? NIL : old_path->pathkeys;
511 : 3298680 : keyscmp = compare_pathkeys(new_path_pathkeys,
512 : : old_path_pathkeys);
513 [ + + ]: 3298680 : if (keyscmp != PATHKEYS_DIFFERENT)
514 : : {
515 [ + + + - : 3124190 : switch (costcmp)
- ]
516 : : {
517 : 329351 : case COSTS_EQUAL:
518 [ + + ]: 329351 : outercmp = bms_subset_compare(PATH_REQ_OUTER(new_path),
519 [ + + ]: 329351 : PATH_REQ_OUTER(old_path));
520 [ + + ]: 329351 : if (keyscmp == PATHKEYS_BETTER1)
521 : : {
522 [ + + + - ]: 6745 : if ((outercmp == BMS_EQUAL ||
523 : 6745 : outercmp == BMS_SUBSET1) &&
524 [ + + ]: 6745 : new_path->rows <= old_path->rows &&
525 [ + - ]: 6689 : new_path->parallel_safe >= old_path->parallel_safe)
526 : 6689 : remove_old = true; /* new dominates old */
527 : : }
528 [ + + ]: 322606 : else if (keyscmp == PATHKEYS_BETTER2)
529 : : {
530 [ + + + - ]: 19641 : if ((outercmp == BMS_EQUAL ||
531 : 19641 : outercmp == BMS_SUBSET2) &&
532 [ + + ]: 19641 : new_path->rows >= old_path->rows &&
533 [ + - ]: 15957 : new_path->parallel_safe <= old_path->parallel_safe)
534 : 15957 : accept_new = false; /* old dominates new */
535 : : }
536 : : else /* keyscmp == PATHKEYS_EQUAL */
537 : : {
538 [ + + ]: 302965 : 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 : 296531 : if (new_path->parallel_safe >
556 [ + + ]: 296531 : old_path->parallel_safe)
557 : 28 : remove_old = true; /* new dominates old */
558 : 296503 : else if (new_path->parallel_safe <
559 [ + + ]: 296503 : old_path->parallel_safe)
560 : 36 : accept_new = false; /* old dominates new */
561 [ + + ]: 296467 : else if (new_path->rows < old_path->rows)
562 : 39 : remove_old = true; /* new dominates old */
563 [ + + ]: 296428 : else if (new_path->rows > old_path->rows)
564 : 111 : accept_new = false; /* old dominates new */
565 [ + + ]: 296317 : else if (compare_path_costs_fuzzily(new_path,
566 : : old_path,
567 : : 1.0000000001) == COSTS_BETTER1)
568 : 12427 : remove_old = true; /* new dominates old */
569 : : else
570 : 283890 : accept_new = false; /* old equals or
571 : : * dominates new */
572 : : }
573 [ + + ]: 6434 : 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 [ + + ]: 5817 : else if (outercmp == BMS_SUBSET2 &&
578 [ + + ]: 5043 : new_path->rows >= old_path->rows &&
579 [ + - ]: 4808 : new_path->parallel_safe <= old_path->parallel_safe)
580 : 4808 : accept_new = false; /* old dominates new */
581 : : /* else different parameterizations, keep both */
582 : : }
583 : 329351 : break;
584 : 885482 : case COSTS_BETTER1:
585 [ + + ]: 885482 : if (keyscmp != PATHKEYS_BETTER2)
586 : : {
587 [ + + ]: 597065 : outercmp = bms_subset_compare(PATH_REQ_OUTER(new_path),
588 [ + + ]: 597065 : PATH_REQ_OUTER(old_path));
589 [ + + + + ]: 597065 : if ((outercmp == BMS_EQUAL ||
590 : 513498 : outercmp == BMS_SUBSET1) &&
591 [ + + ]: 513498 : new_path->rows <= old_path->rows &&
592 [ + + ]: 509632 : new_path->parallel_safe >= old_path->parallel_safe)
593 : 506974 : remove_old = true; /* new dominates old */
594 : : }
595 : 885482 : break;
596 : 1909357 : case COSTS_BETTER2:
597 [ + + ]: 1909357 : if (keyscmp != PATHKEYS_BETTER1)
598 : : {
599 [ + + ]: 1222695 : outercmp = bms_subset_compare(PATH_REQ_OUTER(new_path),
600 [ + + ]: 1222695 : PATH_REQ_OUTER(old_path));
601 [ + + + + ]: 1222695 : if ((outercmp == BMS_EQUAL ||
602 : 1152998 : outercmp == BMS_SUBSET2) &&
603 [ + + ]: 1152998 : new_path->rows >= old_path->rows &&
604 [ + + ]: 1081412 : new_path->parallel_safe <= old_path->parallel_safe)
605 : 1079766 : accept_new = false; /* old dominates new */
606 : : }
607 : 1909357 : 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 [ + + ]: 3366896 : if (remove_old)
623 : : {
624 : 526774 : 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 [ + + ]: 526774 : if (!IsA(old_path, IndexPath))
631 : 506539 : 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 [ + + ]: 2840122 : if (new_path->disabled_nodes > old_path->disabled_nodes ||
640 [ + + ]: 2721131 : (new_path->disabled_nodes == old_path->disabled_nodes &&
641 [ + + ]: 2707542 : new_path->total_cost >= old_path->total_cost))
642 : 2378357 : 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 [ + + ]: 3366896 : if (!accept_new)
651 : 1384568 : break;
652 : : }
653 : :
654 [ + + ]: 3663754 : if (accept_new)
655 : : {
656 : : /* Accept the new path: insert it at proper place in pathlist */
657 : 2279186 : parent_rel->pathlist =
658 : 2279186 : list_insert_nth(parent_rel->pathlist, insert_at, new_path);
659 : : }
660 : : else
661 : : {
662 : : /* Reject and recycle the new path */
663 [ + + ]: 1384568 : if (!IsA(new_path, IndexPath))
664 : 1292635 : pfree(new_path);
665 : : }
666 : 3663754 : }
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 : 4050213 : 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 [ + + ]: 4050213 : new_path_pathkeys = required_outer ? NIL : pathkeys;
696 : :
697 : : /* Decide whether new path's startup cost is interesting */
698 [ + + ]: 4050213 : consider_startup = required_outer ? parent_rel->consider_param_startup : parent_rel->consider_startup;
699 : :
700 [ + + + + : 5156498 : foreach(p1, parent_rel->pathlist)
+ + ]
701 : : {
702 : 4857141 : 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 [ + + ]: 4857141 : if (unlikely(old_path->disabled_nodes != disabled_nodes))
712 : : {
713 [ + + ]: 130869 : if (disabled_nodes < old_path->disabled_nodes)
714 : 14878 : break;
715 : : }
716 [ + + ]: 4726272 : else if (total_cost <= old_path->total_cost * STD_FUZZ_FACTOR)
717 : 1468739 : 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 [ + + ]: 3373524 : if (startup_cost > old_path->startup_cost * STD_FUZZ_FACTOR ||
729 [ + + ]: 1604998 : !consider_startup)
730 : : {
731 : : /* new path loses on cost, so check pathkeys... */
732 : : List *old_path_pathkeys;
733 : :
734 [ + + ]: 3321551 : old_path_pathkeys = old_path->param_info ? NIL : old_path->pathkeys;
735 : 3321551 : keyscmp = compare_pathkeys(new_path_pathkeys,
736 : : old_path_pathkeys);
737 [ + + + + ]: 3321551 : if (keyscmp == PATHKEYS_EQUAL ||
738 : : keyscmp == PATHKEYS_BETTER2)
739 : : {
740 : : /* new path does not win on pathkeys... */
741 [ + + + + ]: 2319569 : if (bms_equal(required_outer, PATH_REQ_OUTER(old_path)))
742 : : {
743 : : /* Found an old path that dominates the new one */
744 : 2267239 : return false;
745 : : }
746 : : }
747 : : }
748 : : }
749 : :
750 : 1782974 : 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 : 243372 : add_partial_path(RelOptInfo *parent_rel, Path *new_path)
794 : : {
795 : 243372 : bool accept_new = true; /* unless we find a superior old path */
796 : 243372 : int insert_at = 0; /* where to insert new item */
797 : : ListCell *p1;
798 : :
799 : : /* Check for query cancel. */
800 [ - + ]: 243372 : 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 [ + + + + : 340068 : foreach(p1, parent_rel->partial_pathlist)
+ + ]
813 : : {
814 : 193274 : Path *old_path = (Path *) lfirst(p1);
815 : 193274 : bool remove_old = false; /* unless new proves superior */
816 : : PathKeysComparison keyscmp;
817 : :
818 : : /* Compare pathkeys. */
819 : 193274 : 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 [ + + ]: 193274 : if (keyscmp != PATHKEYS_DIFFERENT)
827 : : {
828 : : PathCostComparison costcmp;
829 : :
830 : : /*
831 : : * Do a fuzzy cost comparison with standard fuzziness limit.
832 : : */
833 : 193107 : costcmp = compare_path_costs_fuzzily(new_path, old_path,
834 : : STD_FUZZ_FACTOR);
835 [ + + ]: 193107 : if (costcmp == COSTS_BETTER1)
836 : : {
837 [ + + ]: 69000 : if (keyscmp != PATHKEYS_BETTER2)
838 : 27629 : remove_old = true;
839 : : }
840 [ + + ]: 124107 : else if (costcmp == COSTS_BETTER2)
841 : : {
842 [ + + ]: 95730 : if (keyscmp != PATHKEYS_BETTER1)
843 : 68834 : accept_new = false;
844 : : }
845 [ + + ]: 28377 : else if (costcmp == COSTS_EQUAL)
846 : : {
847 [ + + ]: 27956 : if (keyscmp == PATHKEYS_BETTER1)
848 : 32 : remove_old = true;
849 [ + + ]: 27924 : else if (keyscmp == PATHKEYS_BETTER2)
850 : 1112 : accept_new = false;
851 [ + + ]: 26812 : else if (compare_path_costs_fuzzily(new_path, old_path,
852 : : 1.0000000001) == COSTS_BETTER1)
853 : 180 : remove_old = true;
854 : : else
855 : 26632 : accept_new = false;
856 : : }
857 : : }
858 : :
859 : : /*
860 : : * Remove current element from partial_pathlist if dominated by new.
861 : : */
862 [ + + ]: 193274 : if (remove_old)
863 : : {
864 : 27841 : parent_rel->partial_pathlist =
865 : 27841 : foreach_delete_current(parent_rel->partial_pathlist, p1);
866 : 27841 : 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 [ + + ]: 165433 : if (new_path->disabled_nodes > old_path->disabled_nodes ||
875 [ + + ]: 162651 : (new_path->disabled_nodes == old_path->disabled_nodes &&
876 [ + + ]: 161686 : new_path->total_cost >= old_path->total_cost))
877 : 123522 : 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 [ + + ]: 193274 : if (!accept_new)
886 : 96578 : break;
887 : : }
888 : :
889 [ + + ]: 243372 : if (accept_new)
890 : : {
891 : : /* Accept the new path: insert it at proper place */
892 : 146794 : parent_rel->partial_pathlist =
893 : 146794 : list_insert_nth(parent_rel->partial_pathlist, insert_at, new_path);
894 : : }
895 : : else
896 : : {
897 : : /* Reject and recycle the new path */
898 : 96578 : pfree(new_path);
899 : : }
900 : 243372 : }
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 : 342535 : add_partial_path_precheck(RelOptInfo *parent_rel, int disabled_nodes,
913 : : Cost startup_cost, Cost total_cost, List *pathkeys)
914 : : {
915 : 342535 : 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 [ + + + + : 428231 : foreach(p1, parent_rel->partial_pathlist)
+ + ]
931 : : {
932 : 349299 : 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 [ + + ]: 349299 : if (unlikely(old_path->disabled_nodes != disabled_nodes))
943 : : {
944 [ + + ]: 9447 : if (disabled_nodes < old_path->disabled_nodes)
945 : 3910 : costcmp = COSTS_BETTER1;
946 : : else
947 : 5537 : costcmp = COSTS_BETTER2;
948 : : }
949 [ + + ]: 339852 : else if (total_cost > old_path->total_cost * STD_FUZZ_FACTOR)
950 : : {
951 [ + + ]: 206391 : if (consider_startup &&
952 [ + + ]: 338 : old_path->startup_cost > startup_cost * STD_FUZZ_FACTOR)
953 : 248 : costcmp = COSTS_DIFFERENT;
954 : : else
955 : 206143 : costcmp = COSTS_BETTER2;
956 : : }
957 [ + + ]: 133461 : else if (old_path->total_cost > total_cost * STD_FUZZ_FACTOR)
958 : : {
959 [ + + ]: 129774 : if (consider_startup &&
960 [ + + ]: 528 : startup_cost > old_path->startup_cost * STD_FUZZ_FACTOR)
961 : 392 : costcmp = COSTS_DIFFERENT;
962 : : else
963 : 129382 : costcmp = COSTS_BETTER1;
964 : : }
965 [ + + ]: 3687 : else if (startup_cost > old_path->startup_cost * STD_FUZZ_FACTOR)
966 : 2379 : costcmp = COSTS_BETTER2;
967 [ + + ]: 1308 : else if (old_path->startup_cost > startup_cost * STD_FUZZ_FACTOR)
968 : 484 : costcmp = COSTS_BETTER1;
969 : : else
970 : 824 : 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 [ + + ]: 349299 : 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 : 348659 : keyscmp = compare_pathkeys(pathkeys, old_path->pathkeys);
984 [ + + ]: 348659 : 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 [ + + + + ]: 348507 : if (costcmp == COSTS_BETTER2 && keyscmp != PATHKEYS_BETTER1)
992 : 263603 : 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 [ + + + + ]: 175241 : if (costcmp == COSTS_BETTER1 && keyscmp != PATHKEYS_BETTER2)
999 : 90337 : 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 [ + + ]: 78932 : if (!add_path_precheck(parent_rel, disabled_nodes, startup_cost,
1009 : : total_cost, pathkeys, NULL))
1010 : 1746 : return false;
1011 : :
1012 : 77186 : 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 : 344046 : create_seqscan_path(PlannerInfo *root, RelOptInfo *rel,
1027 : : Relids required_outer, int parallel_workers)
1028 : : {
1029 : 344046 : Path *pathnode = makeNode(Path);
1030 : :
1031 : 344046 : pathnode->pathtype = T_SeqScan;
1032 : 344046 : pathnode->parent = rel;
1033 : 344046 : pathnode->pathtarget = rel->reltarget;
1034 : 344046 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
1035 : : required_outer);
1036 : 344046 : pathnode->parallel_aware = (parallel_workers > 0);
1037 : 344046 : pathnode->parallel_safe = rel->consider_parallel;
1038 : 344046 : pathnode->parallel_workers = parallel_workers;
1039 : 344046 : pathnode->pathkeys = NIL; /* seqscan has unordered result */
1040 : :
1041 : 344046 : cost_seqscan(pathnode, root, rel, pathnode->param_info);
1042 : :
1043 : 344046 : return pathnode;
1044 : : }
1045 : :
1046 : : /*
1047 : : * create_samplescan_path
1048 : : * Creates a path node for a sampled table scan.
1049 : : */
1050 : : Path *
1051 : 245 : create_samplescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
1052 : : {
1053 : 245 : Path *pathnode = makeNode(Path);
1054 : :
1055 : 245 : pathnode->pathtype = T_SampleScan;
1056 : 245 : pathnode->parent = rel;
1057 : 245 : pathnode->pathtarget = rel->reltarget;
1058 : 245 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
1059 : : required_outer);
1060 : 245 : pathnode->parallel_aware = false;
1061 : 245 : pathnode->parallel_safe = rel->consider_parallel;
1062 : 245 : pathnode->parallel_workers = 0;
1063 : 245 : pathnode->pathkeys = NIL; /* samplescan has unordered result */
1064 : :
1065 : 245 : cost_samplescan(pathnode, root, rel, pathnode->param_info);
1066 : :
1067 : 245 : 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 : 672740 : 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 : 672740 : IndexPath *pathnode = makeNode(IndexPath);
1105 : 672740 : RelOptInfo *rel = index->rel;
1106 : :
1107 [ + + ]: 672740 : pathnode->path.pathtype = indexonly ? T_IndexOnlyScan : T_IndexScan;
1108 : 672740 : pathnode->path.parent = rel;
1109 : 672740 : pathnode->path.pathtarget = rel->reltarget;
1110 : 672740 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1111 : : required_outer);
1112 : 672740 : pathnode->path.parallel_aware = false;
1113 : 672740 : pathnode->path.parallel_safe = rel->consider_parallel;
1114 : 672740 : pathnode->path.parallel_workers = 0;
1115 : 672740 : pathnode->path.pathkeys = pathkeys;
1116 : :
1117 : 672740 : pathnode->indexinfo = index;
1118 : 672740 : pathnode->indexclauses = indexclauses;
1119 : 672740 : pathnode->indexorderbys = indexorderbys;
1120 : 672740 : pathnode->indexorderbycols = indexorderbycols;
1121 : 672740 : pathnode->indexscandir = indexscandir;
1122 : :
1123 : 672740 : 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 [ + + ]: 672740 : if (index->disabled)
1131 : 8565 : pathnode->path.disabled_nodes = 1;
1132 : :
1133 : 672740 : 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 : 287455 : 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 : 287455 : BitmapHeapPath *pathnode = makeNode(BitmapHeapPath);
1157 : :
1158 : 287455 : pathnode->path.pathtype = T_BitmapHeapScan;
1159 : 287455 : pathnode->path.parent = rel;
1160 : 287455 : pathnode->path.pathtarget = rel->reltarget;
1161 : 287455 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1162 : : required_outer);
1163 : 287455 : pathnode->path.parallel_aware = (parallel_degree > 0);
1164 : 287455 : pathnode->path.parallel_safe = rel->consider_parallel;
1165 : 287455 : pathnode->path.parallel_workers = parallel_degree;
1166 : 287455 : pathnode->path.pathkeys = NIL; /* always unordered */
1167 : :
1168 : 287455 : pathnode->bitmapqual = bitmapqual;
1169 : :
1170 : 287455 : cost_bitmap_heap_scan(&pathnode->path, root, rel,
1171 : : pathnode->path.param_info,
1172 : : bitmapqual, loop_count);
1173 : :
1174 : 287455 : return pathnode;
1175 : : }
1176 : :
1177 : : /*
1178 : : * create_bitmap_and_path
1179 : : * Creates a path node representing a BitmapAnd.
1180 : : */
1181 : : BitmapAndPath *
1182 : 45020 : create_bitmap_and_path(PlannerInfo *root,
1183 : : RelOptInfo *rel,
1184 : : List *bitmapquals)
1185 : : {
1186 : 45020 : BitmapAndPath *pathnode = makeNode(BitmapAndPath);
1187 : 45020 : Relids required_outer = NULL;
1188 : : ListCell *lc;
1189 : :
1190 : 45020 : pathnode->path.pathtype = T_BitmapAnd;
1191 : 45020 : pathnode->path.parent = rel;
1192 : 45020 : 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 [ + - + + : 135060 : foreach(lc, bitmapquals)
+ + ]
1200 : : {
1201 : 90040 : Path *bitmapqual = (Path *) lfirst(lc);
1202 : :
1203 : 90040 : required_outer = bms_add_members(required_outer,
1204 [ + + ]: 90040 : PATH_REQ_OUTER(bitmapqual));
1205 : : }
1206 : 45020 : 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 : 45020 : pathnode->path.parallel_aware = false;
1216 : 45020 : pathnode->path.parallel_safe = rel->consider_parallel;
1217 : 45020 : pathnode->path.parallel_workers = 0;
1218 : :
1219 : 45020 : pathnode->path.pathkeys = NIL; /* always unordered */
1220 : :
1221 : 45020 : pathnode->bitmapquals = bitmapquals;
1222 : :
1223 : : /* this sets bitmapselectivity as well as the regular cost fields: */
1224 : 45020 : cost_bitmap_and_node(pathnode, root);
1225 : :
1226 : 45020 : return pathnode;
1227 : : }
1228 : :
1229 : : /*
1230 : : * create_bitmap_or_path
1231 : : * Creates a path node representing a BitmapOr.
1232 : : */
1233 : : BitmapOrPath *
1234 : 1800 : create_bitmap_or_path(PlannerInfo *root,
1235 : : RelOptInfo *rel,
1236 : : List *bitmapquals)
1237 : : {
1238 : 1800 : BitmapOrPath *pathnode = makeNode(BitmapOrPath);
1239 : 1800 : Relids required_outer = NULL;
1240 : : ListCell *lc;
1241 : :
1242 : 1800 : pathnode->path.pathtype = T_BitmapOr;
1243 : 1800 : pathnode->path.parent = rel;
1244 : 1800 : 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 [ + - + + : 4211 : foreach(lc, bitmapquals)
+ + ]
1252 : : {
1253 : 2411 : Path *bitmapqual = (Path *) lfirst(lc);
1254 : :
1255 : 2411 : required_outer = bms_add_members(required_outer,
1256 [ + + ]: 2411 : PATH_REQ_OUTER(bitmapqual));
1257 : : }
1258 : 1800 : 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 : 1800 : pathnode->path.parallel_aware = false;
1268 : 1800 : pathnode->path.parallel_safe = rel->consider_parallel;
1269 : 1800 : pathnode->path.parallel_workers = 0;
1270 : :
1271 : 1800 : pathnode->path.pathkeys = NIL; /* always unordered */
1272 : :
1273 : 1800 : pathnode->bitmapquals = bitmapquals;
1274 : :
1275 : : /* this sets bitmapselectivity as well as the regular cost fields: */
1276 : 1800 : cost_bitmap_or_node(pathnode, root);
1277 : :
1278 : 1800 : 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 : 636 : create_tidscan_path(PlannerInfo *root, RelOptInfo *rel, List *tidquals,
1287 : : Relids required_outer)
1288 : : {
1289 : 636 : TidPath *pathnode = makeNode(TidPath);
1290 : :
1291 : 636 : pathnode->path.pathtype = T_TidScan;
1292 : 636 : pathnode->path.parent = rel;
1293 : 636 : pathnode->path.pathtarget = rel->reltarget;
1294 : 636 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1295 : : required_outer);
1296 : 636 : pathnode->path.parallel_aware = false;
1297 : 636 : pathnode->path.parallel_safe = rel->consider_parallel;
1298 : 636 : pathnode->path.parallel_workers = 0;
1299 : 636 : pathnode->path.pathkeys = NIL; /* always unordered */
1300 : :
1301 : 636 : pathnode->tidquals = tidquals;
1302 : :
1303 : 636 : cost_tidscan(&pathnode->path, root, rel, tidquals,
1304 : : pathnode->path.param_info);
1305 : :
1306 : 636 : 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 : 74505 : 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 : 74505 : AppendPath *pathnode = makeNode(AppendPath);
1360 : : ListCell *l;
1361 : :
1362 : : Assert(!parallel_aware || parallel_workers > 0);
1363 : :
1364 : 74505 : pathnode->child_append_relid_sets = input.child_append_relid_sets;
1365 : 74505 : pathnode->path.pathtype = T_Append;
1366 : 74505 : pathnode->path.parent = rel;
1367 : 74505 : 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 [ + + + + : 74505 : if (rel->reloptkind == RELOPT_BASEREL && root && input.subpaths != NIL)
+ + ]
1381 : 32353 : pathnode->path.param_info = get_baserel_parampathinfo(root,
1382 : : rel,
1383 : : required_outer);
1384 : : else
1385 : 42152 : pathnode->path.param_info = get_appendrel_parampathinfo(rel,
1386 : : required_outer);
1387 : :
1388 : 74505 : pathnode->path.parallel_aware = parallel_aware;
1389 : 74505 : pathnode->path.parallel_safe = rel->consider_parallel;
1390 : 74505 : pathnode->path.parallel_workers = parallel_workers;
1391 : 74505 : 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 [ + + ]: 74505 : 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 : 26560 : list_sort(input.subpaths, append_total_cost_compare);
1412 : 26560 : list_sort(input.partial_subpaths, append_startup_cost_compare);
1413 : : }
1414 : 74505 : pathnode->first_partial_path = list_length(input.subpaths);
1415 : 74505 : 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 [ + + + + ]: 74505 : if (root != NULL && bms_equal(rel->relids, root->all_query_rels))
1422 : 35739 : pathnode->limit_tuples = root->limit_tuples;
1423 : : else
1424 : 38766 : pathnode->limit_tuples = -1.0;
1425 : :
1426 [ + + + + : 258701 : foreach(l, pathnode->subpaths)
+ + ]
1427 : : {
1428 : 184196 : Path *subpath = (Path *) lfirst(l);
1429 : :
1430 [ + + ]: 335021 : pathnode->path.parallel_safe = pathnode->path.parallel_safe &&
1431 [ + + ]: 335021 : 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 and cost too, effectively charging
1446 : : * zero for the Append. Otherwise, we must do the normal costsize
1447 : : * calculation.
1448 : : */
1449 [ + + ]: 74505 : if (list_length(pathnode->subpaths) == 1)
1450 : : {
1451 : 15768 : Path *child = (Path *) linitial(pathnode->subpaths);
1452 : :
1453 [ + + ]: 15768 : if (child->parallel_aware == parallel_aware)
1454 : : {
1455 : 15398 : pathnode->path.rows = child->rows;
1456 : 15398 : pathnode->path.startup_cost = child->startup_cost;
1457 : 15398 : pathnode->path.total_cost = child->total_cost;
1458 : : }
1459 : : else
1460 : 370 : cost_append(pathnode, root);
1461 : : /* Must do this last, else cost_append complains */
1462 : 15768 : pathnode->path.pathkeys = child->pathkeys;
1463 : : }
1464 : : else
1465 : 58737 : cost_append(pathnode, root);
1466 : :
1467 : : /* If the caller provided a row estimate, override the computed value. */
1468 [ + + ]: 74505 : if (rows >= 0)
1469 : 500 : pathnode->path.rows = rows;
1470 : :
1471 : 74505 : return pathnode;
1472 : : }
1473 : :
1474 : : /*
1475 : : * append_total_cost_compare
1476 : : * list_sort comparator for sorting append child paths
1477 : : * by total_cost descending
1478 : : *
1479 : : * For equal total costs, we fall back to comparing startup costs; if those
1480 : : * are equal too, break ties using bms_compare on the paths' relids.
1481 : : * (This is to avoid getting unpredictable results from list_sort.)
1482 : : */
1483 : : static int
1484 : 13798 : append_total_cost_compare(const ListCell *a, const ListCell *b)
1485 : : {
1486 : 13798 : Path *path1 = (Path *) lfirst(a);
1487 : 13798 : Path *path2 = (Path *) lfirst(b);
1488 : : int cmp;
1489 : :
1490 : 13798 : cmp = compare_path_costs(path1, path2, TOTAL_COST);
1491 [ + + ]: 13798 : if (cmp != 0)
1492 : 12517 : return -cmp;
1493 : 1281 : return bms_compare(path1->parent->relids, path2->parent->relids);
1494 : : }
1495 : :
1496 : : /*
1497 : : * append_startup_cost_compare
1498 : : * list_sort comparator for sorting append child paths
1499 : : * by startup_cost descending
1500 : : *
1501 : : * For equal startup costs, we fall back to comparing total costs; if those
1502 : : * are equal too, break ties using bms_compare on the paths' relids.
1503 : : * (This is to avoid getting unpredictable results from list_sort.)
1504 : : */
1505 : : static int
1506 : 39633 : append_startup_cost_compare(const ListCell *a, const ListCell *b)
1507 : : {
1508 : 39633 : Path *path1 = (Path *) lfirst(a);
1509 : 39633 : Path *path2 = (Path *) lfirst(b);
1510 : : int cmp;
1511 : :
1512 : 39633 : cmp = compare_path_costs(path1, path2, STARTUP_COST);
1513 [ + + ]: 39633 : if (cmp != 0)
1514 : 18641 : return -cmp;
1515 : 20992 : return bms_compare(path1->parent->relids, path2->parent->relids);
1516 : : }
1517 : :
1518 : : /*
1519 : : * create_merge_append_path
1520 : : * Creates a path corresponding to a MergeAppend plan, returning the
1521 : : * pathnode.
1522 : : */
1523 : : MergeAppendPath *
1524 : 7487 : create_merge_append_path(PlannerInfo *root,
1525 : : RelOptInfo *rel,
1526 : : List *subpaths,
1527 : : List *child_append_relid_sets,
1528 : : List *pathkeys,
1529 : : Relids required_outer)
1530 : : {
1531 : 7487 : MergeAppendPath *pathnode = makeNode(MergeAppendPath);
1532 : : int input_disabled_nodes;
1533 : : Cost input_startup_cost;
1534 : : Cost input_total_cost;
1535 : : ListCell *l;
1536 : :
1537 : : /*
1538 : : * We don't currently support parameterized MergeAppend paths, as
1539 : : * explained in the comments for generate_orderedappend_paths.
1540 : : */
1541 : : Assert(bms_is_empty(rel->lateral_relids) && bms_is_empty(required_outer));
1542 : :
1543 : 7487 : pathnode->child_append_relid_sets = child_append_relid_sets;
1544 : 7487 : pathnode->path.pathtype = T_MergeAppend;
1545 : 7487 : pathnode->path.parent = rel;
1546 : 7487 : pathnode->path.pathtarget = rel->reltarget;
1547 : 7487 : pathnode->path.param_info = NULL;
1548 : 7487 : pathnode->path.parallel_aware = false;
1549 : 7487 : pathnode->path.parallel_safe = rel->consider_parallel;
1550 : 7487 : pathnode->path.parallel_workers = 0;
1551 : 7487 : pathnode->path.pathkeys = pathkeys;
1552 : 7487 : pathnode->subpaths = subpaths;
1553 : :
1554 : : /*
1555 : : * Apply query-wide LIMIT if known and path is for sole base relation.
1556 : : * (Handling this at this low level is a bit klugy.)
1557 : : */
1558 [ + + ]: 7487 : if (bms_equal(rel->relids, root->all_query_rels))
1559 : 3463 : pathnode->limit_tuples = root->limit_tuples;
1560 : : else
1561 : 4024 : pathnode->limit_tuples = -1.0;
1562 : :
1563 : : /*
1564 : : * Add up the sizes and costs of the input paths.
1565 : : */
1566 : 7487 : pathnode->path.rows = 0;
1567 : 7487 : input_disabled_nodes = 0;
1568 : 7487 : input_startup_cost = 0;
1569 : 7487 : input_total_cost = 0;
1570 [ + - + + : 27154 : foreach(l, subpaths)
+ + ]
1571 : : {
1572 : 19667 : Path *subpath = (Path *) lfirst(l);
1573 : : int presorted_keys;
1574 : : Path sort_path; /* dummy for result of
1575 : : * cost_sort/cost_incremental_sort */
1576 : :
1577 : : /* All child paths should be unparameterized */
1578 : : Assert(bms_is_empty(PATH_REQ_OUTER(subpath)));
1579 : :
1580 : 19667 : pathnode->path.rows += subpath->rows;
1581 [ + + ]: 37200 : pathnode->path.parallel_safe = pathnode->path.parallel_safe &&
1582 [ + + ]: 17533 : subpath->parallel_safe;
1583 : :
1584 [ + + ]: 19667 : if (!pathkeys_count_contained_in(pathkeys, subpath->pathkeys,
1585 : : &presorted_keys))
1586 : : {
1587 : : /*
1588 : : * We'll need to insert a Sort node, so include costs for that. We
1589 : : * choose to use incremental sort if it is enabled and there are
1590 : : * presorted keys; otherwise we use full sort.
1591 : : *
1592 : : * We can use the parent's LIMIT if any, since we certainly won't
1593 : : * pull more than that many tuples from any child.
1594 : : */
1595 [ + - + + ]: 494 : if (enable_incremental_sort && presorted_keys > 0)
1596 : : {
1597 : 15 : cost_incremental_sort(&sort_path,
1598 : : root,
1599 : : pathkeys,
1600 : : presorted_keys,
1601 : : subpath->disabled_nodes,
1602 : : subpath->startup_cost,
1603 : : subpath->total_cost,
1604 : : subpath->rows,
1605 : 15 : subpath->pathtarget->width,
1606 : : 0.0,
1607 : : work_mem,
1608 : : pathnode->limit_tuples,
1609 : : NULL);
1610 : : }
1611 : : else
1612 : : {
1613 : 479 : cost_sort(&sort_path,
1614 : : root,
1615 : : pathkeys,
1616 : : subpath->disabled_nodes,
1617 : : subpath->total_cost,
1618 : : subpath->rows,
1619 : 479 : subpath->pathtarget->width,
1620 : : 0.0,
1621 : : work_mem,
1622 : : pathnode->limit_tuples);
1623 : : }
1624 : :
1625 : 494 : subpath = &sort_path;
1626 : : }
1627 : :
1628 : 19667 : input_disabled_nodes += subpath->disabled_nodes;
1629 : 19667 : input_startup_cost += subpath->startup_cost;
1630 : 19667 : input_total_cost += subpath->total_cost;
1631 : : }
1632 : :
1633 : : /*
1634 : : * Now we can compute total costs of the MergeAppend. If there's exactly
1635 : : * one child path and its parallel awareness matches that of the
1636 : : * MergeAppend, then the MergeAppend is a no-op and will be discarded
1637 : : * later (in setrefs.c); otherwise we do the normal cost calculation.
1638 : : */
1639 [ + + ]: 7487 : if (list_length(subpaths) == 1 &&
1640 : 95 : ((Path *) linitial(subpaths))->parallel_aware ==
1641 [ + - ]: 95 : pathnode->path.parallel_aware)
1642 : : {
1643 : 95 : pathnode->path.disabled_nodes = input_disabled_nodes;
1644 : 95 : pathnode->path.startup_cost = input_startup_cost;
1645 : 95 : pathnode->path.total_cost = input_total_cost;
1646 : : }
1647 : : else
1648 : 7392 : cost_merge_append(&pathnode->path, root,
1649 : : pathkeys, list_length(subpaths),
1650 : : input_disabled_nodes,
1651 : : input_startup_cost, input_total_cost,
1652 : : pathnode->path.rows);
1653 : :
1654 : 7487 : return pathnode;
1655 : : }
1656 : :
1657 : : /*
1658 : : * create_group_result_path
1659 : : * Creates a path representing a Result-and-nothing-else plan.
1660 : : *
1661 : : * This is only used for degenerate grouping cases, in which we know we
1662 : : * need to produce one result row, possibly filtered by a HAVING qual.
1663 : : */
1664 : : GroupResultPath *
1665 : 142325 : create_group_result_path(PlannerInfo *root, RelOptInfo *rel,
1666 : : PathTarget *target, List *havingqual)
1667 : : {
1668 : 142325 : GroupResultPath *pathnode = makeNode(GroupResultPath);
1669 : :
1670 : 142325 : pathnode->path.pathtype = T_Result;
1671 : 142325 : pathnode->path.parent = rel;
1672 : 142325 : pathnode->path.pathtarget = target;
1673 : 142325 : pathnode->path.param_info = NULL; /* there are no other rels... */
1674 : 142325 : pathnode->path.parallel_aware = false;
1675 : 142325 : pathnode->path.parallel_safe = rel->consider_parallel;
1676 : 142325 : pathnode->path.parallel_workers = 0;
1677 : 142325 : pathnode->path.pathkeys = NIL;
1678 : 142325 : pathnode->quals = havingqual;
1679 : :
1680 : : /*
1681 : : * We can't quite use cost_resultscan() because the quals we want to
1682 : : * account for are not baserestrict quals of the rel. Might as well just
1683 : : * hack it here.
1684 : : */
1685 : 142325 : pathnode->path.rows = 1;
1686 : 142325 : pathnode->path.startup_cost = target->cost.startup;
1687 : 142325 : pathnode->path.total_cost = target->cost.startup +
1688 : 142325 : cpu_tuple_cost + target->cost.per_tuple;
1689 : :
1690 : : /*
1691 : : * Add cost of qual, if any --- but we ignore its selectivity, since our
1692 : : * rowcount estimate should be 1 no matter what the qual is.
1693 : : */
1694 [ + + ]: 142325 : if (havingqual)
1695 : : {
1696 : : QualCost qual_cost;
1697 : :
1698 : 512 : cost_qual_eval(&qual_cost, havingqual, root);
1699 : : /* havingqual is evaluated once at startup */
1700 : 512 : pathnode->path.startup_cost += qual_cost.startup + qual_cost.per_tuple;
1701 : 512 : pathnode->path.total_cost += qual_cost.startup + qual_cost.per_tuple;
1702 : : }
1703 : :
1704 : 142325 : return pathnode;
1705 : : }
1706 : :
1707 : : /*
1708 : : * create_material_path
1709 : : * Creates a path corresponding to a Material plan, returning the
1710 : : * pathnode.
1711 : : */
1712 : : MaterialPath *
1713 : 499221 : create_material_path(RelOptInfo *rel, Path *subpath, bool enabled)
1714 : : {
1715 : 499221 : MaterialPath *pathnode = makeNode(MaterialPath);
1716 : :
1717 : : Assert(subpath->parent == rel);
1718 : :
1719 : 499221 : pathnode->path.pathtype = T_Material;
1720 : 499221 : pathnode->path.parent = rel;
1721 : 499221 : pathnode->path.pathtarget = rel->reltarget;
1722 : 499221 : pathnode->path.param_info = subpath->param_info;
1723 : 499221 : pathnode->path.parallel_aware = false;
1724 [ + + ]: 956811 : pathnode->path.parallel_safe = rel->consider_parallel &&
1725 [ + + ]: 457590 : subpath->parallel_safe;
1726 : 499221 : pathnode->path.parallel_workers = subpath->parallel_workers;
1727 : 499221 : pathnode->path.pathkeys = subpath->pathkeys;
1728 : :
1729 : 499221 : pathnode->subpath = subpath;
1730 : :
1731 : 499221 : cost_material(&pathnode->path,
1732 : : enabled,
1733 : : subpath->disabled_nodes,
1734 : : subpath->startup_cost,
1735 : : subpath->total_cost,
1736 : : subpath->rows,
1737 : 499221 : subpath->pathtarget->width);
1738 : :
1739 : 499221 : return pathnode;
1740 : : }
1741 : :
1742 : : /*
1743 : : * create_memoize_path
1744 : : * Creates a path corresponding to a Memoize plan, returning the pathnode.
1745 : : */
1746 : : MemoizePath *
1747 : 208886 : create_memoize_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath,
1748 : : List *param_exprs, List *hash_operators,
1749 : : bool singlerow, bool binary_mode, Cardinality est_calls)
1750 : : {
1751 : 208886 : MemoizePath *pathnode = makeNode(MemoizePath);
1752 : :
1753 : : Assert(subpath->parent == rel);
1754 : :
1755 : 208886 : pathnode->path.pathtype = T_Memoize;
1756 : 208886 : pathnode->path.parent = rel;
1757 : 208886 : pathnode->path.pathtarget = rel->reltarget;
1758 : 208886 : pathnode->path.param_info = subpath->param_info;
1759 : 208886 : pathnode->path.parallel_aware = false;
1760 [ + + ]: 407490 : pathnode->path.parallel_safe = rel->consider_parallel &&
1761 [ + + ]: 198604 : subpath->parallel_safe;
1762 : 208886 : pathnode->path.parallel_workers = subpath->parallel_workers;
1763 : 208886 : pathnode->path.pathkeys = subpath->pathkeys;
1764 : :
1765 : 208886 : pathnode->subpath = subpath;
1766 : 208886 : pathnode->hash_operators = hash_operators;
1767 : 208886 : pathnode->param_exprs = param_exprs;
1768 : 208886 : pathnode->singlerow = singlerow;
1769 : 208886 : pathnode->binary_mode = binary_mode;
1770 : :
1771 : : /*
1772 : : * For now we set est_entries to 0. cost_memoize_rescan() does all the
1773 : : * hard work to determine how many cache entries there are likely to be,
1774 : : * so it seems best to leave it up to that function to fill this field in.
1775 : : * If left at 0, the executor will make a guess at a good value.
1776 : : */
1777 : 208886 : pathnode->est_entries = 0;
1778 : :
1779 : 208886 : pathnode->est_calls = clamp_row_est(est_calls);
1780 : :
1781 : : /* These will also be set later in cost_memoize_rescan() */
1782 : 208886 : pathnode->est_unique_keys = 0.0;
1783 : 208886 : pathnode->est_hit_ratio = 0.0;
1784 : :
1785 : : /*
1786 : : * We should not be asked to generate this path type when memoization is
1787 : : * disabled, so set our count of disabled nodes equal to the subpath's
1788 : : * count.
1789 : : *
1790 : : * It would be nice to also Assert that memoization is enabled, but the
1791 : : * value of enable_memoize is not controlling: what we would need to check
1792 : : * is that the JoinPathExtraData's pgs_mask included PGS_NESTLOOP_MEMOIZE.
1793 : : */
1794 : 208886 : pathnode->path.disabled_nodes = subpath->disabled_nodes;
1795 : :
1796 : : /*
1797 : : * Add a small additional charge for caching the first entry. All the
1798 : : * harder calculations for rescans are performed in cost_memoize_rescan().
1799 : : */
1800 : 208886 : pathnode->path.startup_cost = subpath->startup_cost + cpu_tuple_cost;
1801 : 208886 : pathnode->path.total_cost = subpath->total_cost + cpu_tuple_cost;
1802 : 208886 : pathnode->path.rows = subpath->rows;
1803 : :
1804 : 208886 : return pathnode;
1805 : : }
1806 : :
1807 : : /*
1808 : : * create_gather_merge_path
1809 : : *
1810 : : * Creates a path corresponding to a gather merge scan, returning
1811 : : * the pathnode.
1812 : : */
1813 : : GatherMergePath *
1814 : 15856 : create_gather_merge_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath,
1815 : : PathTarget *target, List *pathkeys,
1816 : : Relids required_outer, double *rows)
1817 : : {
1818 : 15856 : GatherMergePath *pathnode = makeNode(GatherMergePath);
1819 : 15856 : int input_disabled_nodes = 0;
1820 : 15856 : Cost input_startup_cost = 0;
1821 : 15856 : Cost input_total_cost = 0;
1822 : :
1823 : : Assert(subpath->parallel_safe);
1824 : : Assert(pathkeys);
1825 : :
1826 : : /*
1827 : : * The subpath should guarantee that it is adequately ordered either by
1828 : : * adding an explicit sort node or by using presorted input. We cannot
1829 : : * add an explicit Sort node for the subpath in createplan.c on additional
1830 : : * pathkeys, because we can't guarantee the sort would be safe. For
1831 : : * example, expressions may be volatile or otherwise parallel unsafe.
1832 : : */
1833 [ - + ]: 15856 : if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
1834 [ # # ]: 0 : elog(ERROR, "gather merge input not sufficiently sorted");
1835 : :
1836 : 15856 : pathnode->path.pathtype = T_GatherMerge;
1837 : 15856 : pathnode->path.parent = rel;
1838 : 15856 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1839 : : required_outer);
1840 : 15856 : pathnode->path.parallel_aware = false;
1841 : :
1842 : 15856 : pathnode->subpath = subpath;
1843 : 15856 : pathnode->num_workers = subpath->parallel_workers;
1844 : 15856 : pathnode->path.pathkeys = pathkeys;
1845 [ - + ]: 15856 : pathnode->path.pathtarget = target ? target : rel->reltarget;
1846 : :
1847 : 15856 : input_disabled_nodes += subpath->disabled_nodes;
1848 : 15856 : input_startup_cost += subpath->startup_cost;
1849 : 15856 : input_total_cost += subpath->total_cost;
1850 : :
1851 : 15856 : cost_gather_merge(pathnode, root, rel, pathnode->path.param_info,
1852 : : input_disabled_nodes, input_startup_cost,
1853 : : input_total_cost, rows);
1854 : :
1855 : 15856 : return pathnode;
1856 : : }
1857 : :
1858 : : /*
1859 : : * create_gather_path
1860 : : * Creates a path corresponding to a gather scan, returning the
1861 : : * pathnode.
1862 : : *
1863 : : * 'rows' may optionally be set to override row estimates from other sources.
1864 : : */
1865 : : GatherPath *
1866 : 22061 : create_gather_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath,
1867 : : PathTarget *target, Relids required_outer, double *rows)
1868 : : {
1869 : 22061 : GatherPath *pathnode = makeNode(GatherPath);
1870 : :
1871 : : Assert(subpath->parallel_safe);
1872 : :
1873 : 22061 : pathnode->path.pathtype = T_Gather;
1874 : 22061 : pathnode->path.parent = rel;
1875 : 22061 : pathnode->path.pathtarget = target;
1876 : 22061 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1877 : : required_outer);
1878 : 22061 : pathnode->path.parallel_aware = false;
1879 : 22061 : pathnode->path.parallel_safe = false;
1880 : 22061 : pathnode->path.parallel_workers = 0;
1881 : 22061 : pathnode->path.pathkeys = NIL; /* Gather has unordered result */
1882 : :
1883 : 22061 : pathnode->subpath = subpath;
1884 : 22061 : pathnode->num_workers = subpath->parallel_workers;
1885 : 22061 : pathnode->single_copy = false;
1886 : :
1887 [ - + ]: 22061 : if (pathnode->num_workers == 0)
1888 : : {
1889 : 0 : pathnode->path.pathkeys = subpath->pathkeys;
1890 : 0 : pathnode->num_workers = 1;
1891 : 0 : pathnode->single_copy = true;
1892 : : }
1893 : :
1894 : 22061 : cost_gather(pathnode, root, rel, pathnode->path.param_info, rows);
1895 : :
1896 : 22061 : return pathnode;
1897 : : }
1898 : :
1899 : : /*
1900 : : * create_subqueryscan_path
1901 : : * Creates a path corresponding to a scan of a subquery,
1902 : : * returning the pathnode.
1903 : : *
1904 : : * Caller must pass trivial_pathtarget = true if it believes rel->reltarget to
1905 : : * be trivial, ie just a fetch of all the subquery output columns in order.
1906 : : * While we could determine that here, the caller can usually do it more
1907 : : * efficiently (or at least amortize it over multiple calls).
1908 : : */
1909 : : SubqueryScanPath *
1910 : 49566 : create_subqueryscan_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath,
1911 : : bool trivial_pathtarget,
1912 : : List *pathkeys, Relids required_outer)
1913 : : {
1914 : 49566 : SubqueryScanPath *pathnode = makeNode(SubqueryScanPath);
1915 : :
1916 : 49566 : pathnode->path.pathtype = T_SubqueryScan;
1917 : 49566 : pathnode->path.parent = rel;
1918 : 49566 : pathnode->path.pathtarget = rel->reltarget;
1919 : 49566 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1920 : : required_outer);
1921 : 49566 : pathnode->path.parallel_aware = false;
1922 [ + + ]: 83117 : pathnode->path.parallel_safe = rel->consider_parallel &&
1923 [ + + ]: 33551 : subpath->parallel_safe;
1924 : 49566 : pathnode->path.parallel_workers = subpath->parallel_workers;
1925 : 49566 : pathnode->path.pathkeys = pathkeys;
1926 : 49566 : pathnode->subpath = subpath;
1927 : :
1928 : 49566 : cost_subqueryscan(pathnode, root, rel, pathnode->path.param_info,
1929 : : trivial_pathtarget);
1930 : :
1931 : 49566 : return pathnode;
1932 : : }
1933 : :
1934 : : /*
1935 : : * create_functionscan_path
1936 : : * Creates a path corresponding to a sequential scan of a function,
1937 : : * returning the pathnode.
1938 : : */
1939 : : Path *
1940 : 35085 : create_functionscan_path(PlannerInfo *root, RelOptInfo *rel,
1941 : : List *pathkeys, Relids required_outer)
1942 : : {
1943 : 35085 : Path *pathnode = makeNode(Path);
1944 : :
1945 : 35085 : pathnode->pathtype = T_FunctionScan;
1946 : 35085 : pathnode->parent = rel;
1947 : 35085 : pathnode->pathtarget = rel->reltarget;
1948 : 35085 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
1949 : : required_outer);
1950 : 35085 : pathnode->parallel_aware = false;
1951 : 35085 : pathnode->parallel_safe = rel->consider_parallel;
1952 : 35085 : pathnode->parallel_workers = 0;
1953 : 35085 : pathnode->pathkeys = pathkeys;
1954 : :
1955 : 35085 : cost_functionscan(pathnode, root, rel, pathnode->param_info);
1956 : :
1957 : 35085 : return pathnode;
1958 : : }
1959 : :
1960 : : /*
1961 : : * create_tablefuncscan_path
1962 : : * Creates a path corresponding to a sequential scan of a table function,
1963 : : * returning the pathnode.
1964 : : */
1965 : : Path *
1966 : 604 : create_tablefuncscan_path(PlannerInfo *root, RelOptInfo *rel,
1967 : : Relids required_outer)
1968 : : {
1969 : 604 : Path *pathnode = makeNode(Path);
1970 : :
1971 : 604 : pathnode->pathtype = T_TableFuncScan;
1972 : 604 : pathnode->parent = rel;
1973 : 604 : pathnode->pathtarget = rel->reltarget;
1974 : 604 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
1975 : : required_outer);
1976 : 604 : pathnode->parallel_aware = false;
1977 : 604 : pathnode->parallel_safe = rel->consider_parallel;
1978 : 604 : pathnode->parallel_workers = 0;
1979 : 604 : pathnode->pathkeys = NIL; /* result is always unordered */
1980 : :
1981 : 604 : cost_tablefuncscan(pathnode, root, rel, pathnode->param_info);
1982 : :
1983 : 604 : return pathnode;
1984 : : }
1985 : :
1986 : : /*
1987 : : * create_valuesscan_path
1988 : : * Creates a path corresponding to a scan of a VALUES list,
1989 : : * returning the pathnode.
1990 : : */
1991 : : Path *
1992 : 7015 : create_valuesscan_path(PlannerInfo *root, RelOptInfo *rel,
1993 : : Relids required_outer)
1994 : : {
1995 : 7015 : Path *pathnode = makeNode(Path);
1996 : :
1997 : 7015 : pathnode->pathtype = T_ValuesScan;
1998 : 7015 : pathnode->parent = rel;
1999 : 7015 : pathnode->pathtarget = rel->reltarget;
2000 : 7015 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
2001 : : required_outer);
2002 : 7015 : pathnode->parallel_aware = false;
2003 : 7015 : pathnode->parallel_safe = rel->consider_parallel;
2004 : 7015 : pathnode->parallel_workers = 0;
2005 : 7015 : pathnode->pathkeys = NIL; /* result is always unordered */
2006 : :
2007 : 7015 : cost_valuesscan(pathnode, root, rel, pathnode->param_info);
2008 : :
2009 : 7015 : return pathnode;
2010 : : }
2011 : :
2012 : : /*
2013 : : * create_ctescan_path
2014 : : * Creates a path corresponding to a scan of a non-self-reference CTE,
2015 : : * returning the pathnode.
2016 : : */
2017 : : Path *
2018 : 2933 : create_ctescan_path(PlannerInfo *root, RelOptInfo *rel,
2019 : : List *pathkeys, Relids required_outer)
2020 : : {
2021 : 2933 : Path *pathnode = makeNode(Path);
2022 : :
2023 : 2933 : pathnode->pathtype = T_CteScan;
2024 : 2933 : pathnode->parent = rel;
2025 : 2933 : pathnode->pathtarget = rel->reltarget;
2026 : 2933 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
2027 : : required_outer);
2028 : 2933 : pathnode->parallel_aware = false;
2029 : 2933 : pathnode->parallel_safe = rel->consider_parallel;
2030 : 2933 : pathnode->parallel_workers = 0;
2031 : 2933 : pathnode->pathkeys = pathkeys;
2032 : :
2033 : 2933 : cost_ctescan(pathnode, root, rel, pathnode->param_info);
2034 : :
2035 : 2933 : return pathnode;
2036 : : }
2037 : :
2038 : : /*
2039 : : * create_namedtuplestorescan_path
2040 : : * Creates a path corresponding to a scan of a named tuplestore, returning
2041 : : * the pathnode.
2042 : : */
2043 : : Path *
2044 : 443 : create_namedtuplestorescan_path(PlannerInfo *root, RelOptInfo *rel,
2045 : : Relids required_outer)
2046 : : {
2047 : 443 : Path *pathnode = makeNode(Path);
2048 : :
2049 : 443 : pathnode->pathtype = T_NamedTuplestoreScan;
2050 : 443 : pathnode->parent = rel;
2051 : 443 : pathnode->pathtarget = rel->reltarget;
2052 : 443 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
2053 : : required_outer);
2054 : 443 : pathnode->parallel_aware = false;
2055 : 443 : pathnode->parallel_safe = rel->consider_parallel;
2056 : 443 : pathnode->parallel_workers = 0;
2057 : 443 : pathnode->pathkeys = NIL; /* result is always unordered */
2058 : :
2059 : 443 : cost_namedtuplestorescan(pathnode, root, rel, pathnode->param_info);
2060 : :
2061 : 443 : return pathnode;
2062 : : }
2063 : :
2064 : : /*
2065 : : * create_resultscan_path
2066 : : * Creates a path corresponding to a scan of an RTE_RESULT relation,
2067 : : * returning the pathnode.
2068 : : */
2069 : : Path *
2070 : 3746 : create_resultscan_path(PlannerInfo *root, RelOptInfo *rel,
2071 : : Relids required_outer)
2072 : : {
2073 : 3746 : Path *pathnode = makeNode(Path);
2074 : :
2075 : 3746 : pathnode->pathtype = T_Result;
2076 : 3746 : pathnode->parent = rel;
2077 : 3746 : pathnode->pathtarget = rel->reltarget;
2078 : 3746 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
2079 : : required_outer);
2080 : 3746 : pathnode->parallel_aware = false;
2081 : 3746 : pathnode->parallel_safe = rel->consider_parallel;
2082 : 3746 : pathnode->parallel_workers = 0;
2083 : 3746 : pathnode->pathkeys = NIL; /* result is always unordered */
2084 : :
2085 : 3746 : cost_resultscan(pathnode, root, rel, pathnode->param_info);
2086 : :
2087 : 3746 : return pathnode;
2088 : : }
2089 : :
2090 : : /*
2091 : : * create_worktablescan_path
2092 : : * Creates a path corresponding to a scan of a self-reference CTE,
2093 : : * returning the pathnode.
2094 : : */
2095 : : Path *
2096 : 642 : create_worktablescan_path(PlannerInfo *root, RelOptInfo *rel,
2097 : : Relids required_outer)
2098 : : {
2099 : 642 : Path *pathnode = makeNode(Path);
2100 : :
2101 : 642 : pathnode->pathtype = T_WorkTableScan;
2102 : 642 : pathnode->parent = rel;
2103 : 642 : pathnode->pathtarget = rel->reltarget;
2104 : 642 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
2105 : : required_outer);
2106 : 642 : pathnode->parallel_aware = false;
2107 : 642 : pathnode->parallel_safe = rel->consider_parallel;
2108 : 642 : pathnode->parallel_workers = 0;
2109 : 642 : pathnode->pathkeys = NIL; /* result is always unordered */
2110 : :
2111 : : /* Cost is the same as for a regular CTE scan */
2112 : 642 : cost_ctescan(pathnode, root, rel, pathnode->param_info);
2113 : :
2114 : 642 : return pathnode;
2115 : : }
2116 : :
2117 : : /*
2118 : : * create_foreignscan_path
2119 : : * Creates a path corresponding to a scan of a foreign base table,
2120 : : * returning the pathnode.
2121 : : *
2122 : : * This function is never called from core Postgres; rather, it's expected
2123 : : * to be called by the GetForeignPaths function of a foreign data wrapper.
2124 : : * We make the FDW supply all fields of the path, since we do not have any way
2125 : : * to calculate them in core. However, there is a usually-sane default for
2126 : : * the pathtarget (rel->reltarget), so we let a NULL for "target" select that.
2127 : : */
2128 : : ForeignPath *
2129 : 1921 : create_foreignscan_path(PlannerInfo *root, RelOptInfo *rel,
2130 : : PathTarget *target,
2131 : : double rows, int disabled_nodes,
2132 : : Cost startup_cost, Cost total_cost,
2133 : : List *pathkeys,
2134 : : Relids required_outer,
2135 : : Path *fdw_outerpath,
2136 : : List *fdw_restrictinfo,
2137 : : List *fdw_private)
2138 : : {
2139 : 1921 : ForeignPath *pathnode = makeNode(ForeignPath);
2140 : :
2141 : : /* Historically some FDWs were confused about when to use this */
2142 : : Assert(IS_SIMPLE_REL(rel));
2143 : :
2144 : 1921 : pathnode->path.pathtype = T_ForeignScan;
2145 : 1921 : pathnode->path.parent = rel;
2146 [ + - ]: 1921 : pathnode->path.pathtarget = target ? target : rel->reltarget;
2147 : 1921 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
2148 : : required_outer);
2149 : 1921 : pathnode->path.parallel_aware = false;
2150 : 1921 : pathnode->path.parallel_safe = rel->consider_parallel;
2151 : 1921 : pathnode->path.parallel_workers = 0;
2152 : 1921 : pathnode->path.rows = rows;
2153 : 1921 : pathnode->path.disabled_nodes = disabled_nodes;
2154 : 1921 : pathnode->path.startup_cost = startup_cost;
2155 : 1921 : pathnode->path.total_cost = total_cost;
2156 : 1921 : pathnode->path.pathkeys = pathkeys;
2157 : :
2158 : 1921 : pathnode->fdw_outerpath = fdw_outerpath;
2159 : 1921 : pathnode->fdw_restrictinfo = fdw_restrictinfo;
2160 : 1921 : pathnode->fdw_private = fdw_private;
2161 : :
2162 : 1921 : return pathnode;
2163 : : }
2164 : :
2165 : : /*
2166 : : * create_foreign_join_path
2167 : : * Creates a path corresponding to a scan of a foreign join,
2168 : : * returning the pathnode.
2169 : : *
2170 : : * This function is never called from core Postgres; rather, it's expected
2171 : : * to be called by the GetForeignJoinPaths function of a foreign data wrapper.
2172 : : * We make the FDW supply all fields of the path, since we do not have any way
2173 : : * to calculate them in core. However, there is a usually-sane default for
2174 : : * the pathtarget (rel->reltarget), so we let a NULL for "target" select that.
2175 : : */
2176 : : ForeignPath *
2177 : 607 : create_foreign_join_path(PlannerInfo *root, RelOptInfo *rel,
2178 : : PathTarget *target,
2179 : : double rows, int disabled_nodes,
2180 : : Cost startup_cost, Cost total_cost,
2181 : : List *pathkeys,
2182 : : Relids required_outer,
2183 : : Path *fdw_outerpath,
2184 : : List *fdw_restrictinfo,
2185 : : List *fdw_private)
2186 : : {
2187 : 607 : ForeignPath *pathnode = makeNode(ForeignPath);
2188 : :
2189 : : /*
2190 : : * We should use get_joinrel_parampathinfo to handle parameterized paths,
2191 : : * but the API of this function doesn't support it, and existing
2192 : : * extensions aren't yet trying to build such paths anyway. For the
2193 : : * moment just throw an error if someone tries it; eventually we should
2194 : : * revisit this.
2195 : : */
2196 [ + - - + ]: 607 : if (!bms_is_empty(required_outer) || !bms_is_empty(rel->lateral_relids))
2197 [ # # ]: 0 : elog(ERROR, "parameterized foreign joins are not supported yet");
2198 : :
2199 : 607 : pathnode->path.pathtype = T_ForeignScan;
2200 : 607 : pathnode->path.parent = rel;
2201 [ + - ]: 607 : pathnode->path.pathtarget = target ? target : rel->reltarget;
2202 : 607 : pathnode->path.param_info = NULL; /* XXX see above */
2203 : 607 : pathnode->path.parallel_aware = false;
2204 : 607 : pathnode->path.parallel_safe = rel->consider_parallel;
2205 : 607 : pathnode->path.parallel_workers = 0;
2206 : 607 : pathnode->path.rows = rows;
2207 : 607 : pathnode->path.disabled_nodes = disabled_nodes;
2208 : 607 : pathnode->path.startup_cost = startup_cost;
2209 : 607 : pathnode->path.total_cost = total_cost;
2210 : 607 : pathnode->path.pathkeys = pathkeys;
2211 : :
2212 : 607 : pathnode->fdw_outerpath = fdw_outerpath;
2213 : 607 : pathnode->fdw_restrictinfo = fdw_restrictinfo;
2214 : 607 : pathnode->fdw_private = fdw_private;
2215 : :
2216 : 607 : return pathnode;
2217 : : }
2218 : :
2219 : : /*
2220 : : * create_foreign_upper_path
2221 : : * Creates a path corresponding to an upper relation that's computed
2222 : : * directly by an FDW, returning the pathnode.
2223 : : *
2224 : : * This function is never called from core Postgres; rather, it's expected to
2225 : : * be called by the GetForeignUpperPaths function of a foreign data wrapper.
2226 : : * We make the FDW supply all fields of the path, since we do not have any way
2227 : : * to calculate them in core. However, there is a usually-sane default for
2228 : : * the pathtarget (rel->reltarget), so we let a NULL for "target" select that.
2229 : : */
2230 : : ForeignPath *
2231 : 298 : create_foreign_upper_path(PlannerInfo *root, RelOptInfo *rel,
2232 : : PathTarget *target,
2233 : : double rows, int disabled_nodes,
2234 : : Cost startup_cost, Cost total_cost,
2235 : : List *pathkeys,
2236 : : Path *fdw_outerpath,
2237 : : List *fdw_restrictinfo,
2238 : : List *fdw_private)
2239 : : {
2240 : 298 : ForeignPath *pathnode = makeNode(ForeignPath);
2241 : :
2242 : : /*
2243 : : * Upper relations should never have any lateral references, since joining
2244 : : * is complete.
2245 : : */
2246 : : Assert(bms_is_empty(rel->lateral_relids));
2247 : :
2248 : 298 : pathnode->path.pathtype = T_ForeignScan;
2249 : 298 : pathnode->path.parent = rel;
2250 [ - + ]: 298 : pathnode->path.pathtarget = target ? target : rel->reltarget;
2251 : 298 : pathnode->path.param_info = NULL;
2252 : 298 : pathnode->path.parallel_aware = false;
2253 : 298 : pathnode->path.parallel_safe = rel->consider_parallel;
2254 : 298 : pathnode->path.parallel_workers = 0;
2255 : 298 : pathnode->path.rows = rows;
2256 : 298 : pathnode->path.disabled_nodes = disabled_nodes;
2257 : 298 : pathnode->path.startup_cost = startup_cost;
2258 : 298 : pathnode->path.total_cost = total_cost;
2259 : 298 : pathnode->path.pathkeys = pathkeys;
2260 : :
2261 : 298 : pathnode->fdw_outerpath = fdw_outerpath;
2262 : 298 : pathnode->fdw_restrictinfo = fdw_restrictinfo;
2263 : 298 : pathnode->fdw_private = fdw_private;
2264 : :
2265 : 298 : return pathnode;
2266 : : }
2267 : :
2268 : : /*
2269 : : * calc_nestloop_required_outer
2270 : : * Compute the required_outer set for a nestloop join path
2271 : : *
2272 : : * Note: when considering a child join, the inputs nonetheless use top-level
2273 : : * parent relids
2274 : : *
2275 : : * Note: result must not share storage with either input
2276 : : */
2277 : : Relids
2278 : 2639344 : calc_nestloop_required_outer(Relids outerrelids,
2279 : : Relids outer_paramrels,
2280 : : Relids innerrelids,
2281 : : Relids inner_paramrels)
2282 : : {
2283 : : Relids required_outer;
2284 : :
2285 : : /* inner_path can require rels from outer path, but not vice versa */
2286 : : Assert(!bms_overlap(outer_paramrels, innerrelids));
2287 : : /* easy case if inner path is not parameterized */
2288 [ + + ]: 2639344 : if (!inner_paramrels)
2289 : 1860439 : return bms_copy(outer_paramrels);
2290 : : /* else, form the union ... */
2291 : 778905 : required_outer = bms_union(outer_paramrels, inner_paramrels);
2292 : : /* ... and remove any mention of now-satisfied outer rels */
2293 : 778905 : required_outer = bms_del_members(required_outer,
2294 : : outerrelids);
2295 : 778905 : return required_outer;
2296 : : }
2297 : :
2298 : : /*
2299 : : * calc_non_nestloop_required_outer
2300 : : * Compute the required_outer set for a merge or hash join path
2301 : : *
2302 : : * Note: result must not share storage with either input
2303 : : */
2304 : : Relids
2305 : 1666940 : calc_non_nestloop_required_outer(Path *outer_path, Path *inner_path)
2306 : : {
2307 [ + + ]: 1666940 : Relids outer_paramrels = PATH_REQ_OUTER(outer_path);
2308 [ + + ]: 1666940 : Relids inner_paramrels = PATH_REQ_OUTER(inner_path);
2309 : : Relids innerrelids PG_USED_FOR_ASSERTS_ONLY;
2310 : : Relids outerrelids PG_USED_FOR_ASSERTS_ONLY;
2311 : : Relids required_outer;
2312 : :
2313 : : /*
2314 : : * Any parameterization of the input paths refers to topmost parents of
2315 : : * the relevant relations, because reparameterize_path_by_child() hasn't
2316 : : * been called yet. So we must consider topmost parents of the relations
2317 : : * being joined, too, while checking for disallowed parameterization
2318 : : * cases.
2319 : : */
2320 [ + + ]: 1666940 : if (inner_path->parent->top_parent_relids)
2321 : 115969 : innerrelids = inner_path->parent->top_parent_relids;
2322 : : else
2323 : 1550971 : innerrelids = inner_path->parent->relids;
2324 : :
2325 [ + + ]: 1666940 : if (outer_path->parent->top_parent_relids)
2326 : 115969 : outerrelids = outer_path->parent->top_parent_relids;
2327 : : else
2328 : 1550971 : outerrelids = outer_path->parent->relids;
2329 : :
2330 : : /* neither path can require rels from the other */
2331 : : Assert(!bms_overlap(outer_paramrels, innerrelids));
2332 : : Assert(!bms_overlap(inner_paramrels, outerrelids));
2333 : : /* form the union ... */
2334 : 1666940 : required_outer = bms_union(outer_paramrels, inner_paramrels);
2335 : : /* we do not need an explicit test for empty; bms_union gets it right */
2336 : 1666940 : return required_outer;
2337 : : }
2338 : :
2339 : : /*
2340 : : * create_nestloop_path
2341 : : * Creates a pathnode corresponding to a nestloop join between two
2342 : : * relations.
2343 : : *
2344 : : * 'joinrel' is the join relation.
2345 : : * 'jointype' is the type of join required
2346 : : * 'workspace' is the result from initial_cost_nestloop
2347 : : * 'extra' contains various information about the join
2348 : : * 'outer_path' is the outer path
2349 : : * 'inner_path' is the inner path
2350 : : * 'restrict_clauses' are the RestrictInfo nodes to apply at the join
2351 : : * 'pathkeys' are the path keys of the new join path
2352 : : * 'required_outer' is the set of required outer rels
2353 : : *
2354 : : * Returns the resulting path node.
2355 : : */
2356 : : NestPath *
2357 : 1173141 : create_nestloop_path(PlannerInfo *root,
2358 : : RelOptInfo *joinrel,
2359 : : JoinType jointype,
2360 : : JoinCostWorkspace *workspace,
2361 : : JoinPathExtraData *extra,
2362 : : Path *outer_path,
2363 : : Path *inner_path,
2364 : : List *restrict_clauses,
2365 : : List *pathkeys,
2366 : : Relids required_outer)
2367 : : {
2368 : 1173141 : NestPath *pathnode = makeNode(NestPath);
2369 [ + + ]: 1173141 : Relids inner_req_outer = PATH_REQ_OUTER(inner_path);
2370 : : Relids outerrelids;
2371 : :
2372 : : /*
2373 : : * Paths are parameterized by top-level parents, so run parameterization
2374 : : * tests on the parent relids.
2375 : : */
2376 [ + + ]: 1173141 : if (outer_path->parent->top_parent_relids)
2377 : 66149 : outerrelids = outer_path->parent->top_parent_relids;
2378 : : else
2379 : 1106992 : outerrelids = outer_path->parent->relids;
2380 : :
2381 : : /*
2382 : : * If the inner path is parameterized by the outer, we must drop any
2383 : : * restrict_clauses that are due to be moved into the inner path. We have
2384 : : * to do this now, rather than postpone the work till createplan time,
2385 : : * because the restrict_clauses list can affect the size and cost
2386 : : * estimates for this path. We detect such clauses by checking for serial
2387 : : * number match to clauses already enforced in the inner path.
2388 : : */
2389 [ + + ]: 1173141 : if (bms_overlap(inner_req_outer, outerrelids))
2390 : : {
2391 : 296557 : Bitmapset *enforced_serials = get_param_path_clause_serials(inner_path);
2392 : 296557 : List *jclauses = NIL;
2393 : : ListCell *lc;
2394 : :
2395 [ + + + + : 663024 : foreach(lc, restrict_clauses)
+ + ]
2396 : : {
2397 : 366467 : RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2398 : :
2399 [ + + ]: 366467 : if (!bms_is_member(rinfo->rinfo_serial, enforced_serials))
2400 : 59562 : jclauses = lappend(jclauses, rinfo);
2401 : : }
2402 : 296557 : restrict_clauses = jclauses;
2403 : : }
2404 : :
2405 : 1173141 : pathnode->jpath.path.pathtype = T_NestLoop;
2406 : 1173141 : pathnode->jpath.path.parent = joinrel;
2407 : 1173141 : pathnode->jpath.path.pathtarget = joinrel->reltarget;
2408 : 1173141 : pathnode->jpath.path.param_info =
2409 : 1173141 : get_joinrel_parampathinfo(root,
2410 : : joinrel,
2411 : : outer_path,
2412 : : inner_path,
2413 : : extra->sjinfo,
2414 : : required_outer,
2415 : : &restrict_clauses);
2416 : 1173141 : pathnode->jpath.path.parallel_aware = false;
2417 : 3415864 : pathnode->jpath.path.parallel_safe = joinrel->consider_parallel &&
2418 [ + + + + : 1173141 : outer_path->parallel_safe && inner_path->parallel_safe;
+ + ]
2419 : : /* This is a foolish way to estimate parallel_workers, but for now... */
2420 : 1173141 : pathnode->jpath.path.parallel_workers = outer_path->parallel_workers;
2421 : 1173141 : pathnode->jpath.path.pathkeys = pathkeys;
2422 : 1173141 : pathnode->jpath.jointype = jointype;
2423 : 1173141 : pathnode->jpath.inner_unique = extra->inner_unique;
2424 : 1173141 : pathnode->jpath.outerjoinpath = outer_path;
2425 : 1173141 : pathnode->jpath.innerjoinpath = inner_path;
2426 : 1173141 : pathnode->jpath.joinrestrictinfo = restrict_clauses;
2427 : :
2428 : 1173141 : final_cost_nestloop(root, pathnode, workspace, extra);
2429 : :
2430 : 1173141 : return pathnode;
2431 : : }
2432 : :
2433 : : /*
2434 : : * create_mergejoin_path
2435 : : * Creates a pathnode corresponding to a mergejoin join between
2436 : : * two relations
2437 : : *
2438 : : * 'joinrel' is the join relation
2439 : : * 'jointype' is the type of join required
2440 : : * 'workspace' is the result from initial_cost_mergejoin
2441 : : * 'extra' contains various information about the join
2442 : : * 'outer_path' is the outer path
2443 : : * 'inner_path' is the inner path
2444 : : * 'restrict_clauses' are the RestrictInfo nodes to apply at the join
2445 : : * 'pathkeys' are the path keys of the new join path
2446 : : * 'required_outer' is the set of required outer rels
2447 : : * 'mergeclauses' are the RestrictInfo nodes to use as merge clauses
2448 : : * (this should be a subset of the restrict_clauses list)
2449 : : * 'outersortkeys' are the sort varkeys for the outer relation
2450 : : * 'innersortkeys' are the sort varkeys for the inner relation
2451 : : * 'outer_presorted_keys' is the number of presorted keys of the outer path
2452 : : */
2453 : : MergePath *
2454 : 351447 : create_mergejoin_path(PlannerInfo *root,
2455 : : RelOptInfo *joinrel,
2456 : : JoinType jointype,
2457 : : JoinCostWorkspace *workspace,
2458 : : JoinPathExtraData *extra,
2459 : : Path *outer_path,
2460 : : Path *inner_path,
2461 : : List *restrict_clauses,
2462 : : List *pathkeys,
2463 : : Relids required_outer,
2464 : : List *mergeclauses,
2465 : : List *outersortkeys,
2466 : : List *innersortkeys,
2467 : : int outer_presorted_keys)
2468 : : {
2469 : 351447 : MergePath *pathnode = makeNode(MergePath);
2470 : :
2471 : 351447 : pathnode->jpath.path.pathtype = T_MergeJoin;
2472 : 351447 : pathnode->jpath.path.parent = joinrel;
2473 : 351447 : pathnode->jpath.path.pathtarget = joinrel->reltarget;
2474 : 351447 : pathnode->jpath.path.param_info =
2475 : 351447 : get_joinrel_parampathinfo(root,
2476 : : joinrel,
2477 : : outer_path,
2478 : : inner_path,
2479 : : extra->sjinfo,
2480 : : required_outer,
2481 : : &restrict_clauses);
2482 : 351447 : pathnode->jpath.path.parallel_aware = false;
2483 : 1028760 : pathnode->jpath.path.parallel_safe = joinrel->consider_parallel &&
2484 [ + + + + : 351447 : outer_path->parallel_safe && inner_path->parallel_safe;
+ + ]
2485 : : /* This is a foolish way to estimate parallel_workers, but for now... */
2486 : 351447 : pathnode->jpath.path.parallel_workers = outer_path->parallel_workers;
2487 : 351447 : pathnode->jpath.path.pathkeys = pathkeys;
2488 : 351447 : pathnode->jpath.jointype = jointype;
2489 : 351447 : pathnode->jpath.inner_unique = extra->inner_unique;
2490 : 351447 : pathnode->jpath.outerjoinpath = outer_path;
2491 : 351447 : pathnode->jpath.innerjoinpath = inner_path;
2492 : 351447 : pathnode->jpath.joinrestrictinfo = restrict_clauses;
2493 : 351447 : pathnode->path_mergeclauses = mergeclauses;
2494 : 351447 : pathnode->outersortkeys = outersortkeys;
2495 : 351447 : pathnode->innersortkeys = innersortkeys;
2496 : 351447 : pathnode->outer_presorted_keys = outer_presorted_keys;
2497 : : /* pathnode->skip_mark_restore will be set by final_cost_mergejoin */
2498 : : /* pathnode->materialize_inner will be set by final_cost_mergejoin */
2499 : :
2500 : 351447 : final_cost_mergejoin(root, pathnode, workspace, extra);
2501 : :
2502 : 351447 : return pathnode;
2503 : : }
2504 : :
2505 : : /*
2506 : : * create_hashjoin_path
2507 : : * Creates a pathnode corresponding to a hash join between two relations.
2508 : : *
2509 : : * 'joinrel' is the join relation
2510 : : * 'jointype' is the type of join required
2511 : : * 'workspace' is the result from initial_cost_hashjoin
2512 : : * 'extra' contains various information about the join
2513 : : * 'outer_path' is the cheapest outer path
2514 : : * 'inner_path' is the cheapest inner path
2515 : : * 'parallel_hash' to select Parallel Hash of inner path (shared hash table)
2516 : : * 'restrict_clauses' are the RestrictInfo nodes to apply at the join
2517 : : * 'required_outer' is the set of required outer rels
2518 : : * 'hashclauses' are the RestrictInfo nodes to use as hash clauses
2519 : : * (this should be a subset of the restrict_clauses list)
2520 : : */
2521 : : HashPath *
2522 : 348723 : create_hashjoin_path(PlannerInfo *root,
2523 : : RelOptInfo *joinrel,
2524 : : JoinType jointype,
2525 : : JoinCostWorkspace *workspace,
2526 : : JoinPathExtraData *extra,
2527 : : Path *outer_path,
2528 : : Path *inner_path,
2529 : : bool parallel_hash,
2530 : : List *restrict_clauses,
2531 : : Relids required_outer,
2532 : : List *hashclauses)
2533 : : {
2534 : 348723 : HashPath *pathnode = makeNode(HashPath);
2535 : :
2536 : 348723 : pathnode->jpath.path.pathtype = T_HashJoin;
2537 : 348723 : pathnode->jpath.path.parent = joinrel;
2538 : 348723 : pathnode->jpath.path.pathtarget = joinrel->reltarget;
2539 : 348723 : pathnode->jpath.path.param_info =
2540 : 348723 : get_joinrel_parampathinfo(root,
2541 : : joinrel,
2542 : : outer_path,
2543 : : inner_path,
2544 : : extra->sjinfo,
2545 : : required_outer,
2546 : : &restrict_clauses);
2547 : 348723 : pathnode->jpath.path.parallel_aware =
2548 [ + + + + ]: 348723 : joinrel->consider_parallel && parallel_hash;
2549 : 1019305 : pathnode->jpath.path.parallel_safe = joinrel->consider_parallel &&
2550 [ + + + + : 348723 : outer_path->parallel_safe && inner_path->parallel_safe;
+ + ]
2551 : : /* This is a foolish way to estimate parallel_workers, but for now... */
2552 : 348723 : pathnode->jpath.path.parallel_workers = outer_path->parallel_workers;
2553 : :
2554 : : /*
2555 : : * A hashjoin never has pathkeys, since its output ordering is
2556 : : * unpredictable due to possible batching. XXX If the inner relation is
2557 : : * small enough, we could instruct the executor that it must not batch,
2558 : : * and then we could assume that the output inherits the outer relation's
2559 : : * ordering, which might save a sort step. However there is considerable
2560 : : * downside if our estimate of the inner relation size is badly off. For
2561 : : * the moment we don't risk it. (Note also that if we wanted to take this
2562 : : * seriously, joinpath.c would have to consider many more paths for the
2563 : : * outer rel than it does now.)
2564 : : */
2565 : 348723 : pathnode->jpath.path.pathkeys = NIL;
2566 : 348723 : pathnode->jpath.jointype = jointype;
2567 : 348723 : pathnode->jpath.inner_unique = extra->inner_unique;
2568 : 348723 : pathnode->jpath.outerjoinpath = outer_path;
2569 : 348723 : pathnode->jpath.innerjoinpath = inner_path;
2570 : 348723 : pathnode->jpath.joinrestrictinfo = restrict_clauses;
2571 : 348723 : pathnode->path_hashclauses = hashclauses;
2572 : : /* final_cost_hashjoin will fill in pathnode->num_batches */
2573 : :
2574 : 348723 : final_cost_hashjoin(root, pathnode, workspace, extra);
2575 : :
2576 : 348723 : return pathnode;
2577 : : }
2578 : :
2579 : : /*
2580 : : * create_projection_path
2581 : : * Creates a pathnode that represents performing a projection.
2582 : : *
2583 : : * 'rel' is the parent relation associated with the result
2584 : : * 'subpath' is the path representing the source of data
2585 : : * 'target' is the PathTarget to be computed
2586 : : */
2587 : : ProjectionPath *
2588 : 317891 : create_projection_path(PlannerInfo *root,
2589 : : RelOptInfo *rel,
2590 : : Path *subpath,
2591 : : PathTarget *target)
2592 : : {
2593 : 317891 : ProjectionPath *pathnode = makeNode(ProjectionPath);
2594 : : PathTarget *oldtarget;
2595 : :
2596 : : /*
2597 : : * We mustn't put a ProjectionPath directly above another; it's useless
2598 : : * and will confuse create_projection_plan. Rather than making sure all
2599 : : * callers handle that, let's implement it here, by stripping off any
2600 : : * ProjectionPath in what we're given. Given this rule, there won't be
2601 : : * more than one.
2602 : : */
2603 [ + + ]: 317891 : if (IsA(subpath, ProjectionPath))
2604 : : {
2605 : 20 : ProjectionPath *subpp = (ProjectionPath *) subpath;
2606 : :
2607 : : Assert(subpp->path.parent == rel);
2608 : 20 : subpath = subpp->subpath;
2609 : : Assert(!IsA(subpath, ProjectionPath));
2610 : : }
2611 : :
2612 : 317891 : pathnode->path.pathtype = T_Result;
2613 : 317891 : pathnode->path.parent = rel;
2614 : 317891 : pathnode->path.pathtarget = target;
2615 : 317891 : pathnode->path.param_info = subpath->param_info;
2616 : 317891 : pathnode->path.parallel_aware = false;
2617 : 748929 : pathnode->path.parallel_safe = rel->consider_parallel &&
2618 [ + + + + : 423737 : subpath->parallel_safe &&
+ - ]
2619 : 105846 : is_parallel_safe(root, (Node *) target->exprs);
2620 : 317891 : pathnode->path.parallel_workers = subpath->parallel_workers;
2621 : : /* Projection does not change the sort order */
2622 : 317891 : pathnode->path.pathkeys = subpath->pathkeys;
2623 : :
2624 : 317891 : pathnode->subpath = subpath;
2625 : :
2626 : : /*
2627 : : * We might not need a separate Result node. If the input plan node type
2628 : : * can project, we can just tell it to project something else. Or, if it
2629 : : * can't project but the desired target has the same expression list as
2630 : : * what the input will produce anyway, we can still give it the desired
2631 : : * tlist (possibly changing its ressortgroupref labels, but nothing else).
2632 : : * Note: in the latter case, create_projection_plan has to recheck our
2633 : : * conclusion; see comments therein.
2634 : : */
2635 : 317891 : oldtarget = subpath->pathtarget;
2636 [ + + + + ]: 332157 : if (is_projection_capable_path(subpath) ||
2637 : 14266 : equal(oldtarget->exprs, target->exprs))
2638 : : {
2639 : : /* No separate Result node needed */
2640 : 305120 : pathnode->dummypp = true;
2641 : :
2642 : : /*
2643 : : * Set cost of plan as subpath's cost, adjusted for tlist replacement.
2644 : : */
2645 : 305120 : pathnode->path.rows = subpath->rows;
2646 : 305120 : pathnode->path.disabled_nodes = subpath->disabled_nodes;
2647 : 305120 : pathnode->path.startup_cost = subpath->startup_cost +
2648 : 305120 : (target->cost.startup - oldtarget->cost.startup);
2649 : 305120 : pathnode->path.total_cost = subpath->total_cost +
2650 : 305120 : (target->cost.startup - oldtarget->cost.startup) +
2651 : 305120 : (target->cost.per_tuple - oldtarget->cost.per_tuple) * subpath->rows;
2652 : : }
2653 : : else
2654 : : {
2655 : : /* We really do need the Result node */
2656 : 12771 : pathnode->dummypp = false;
2657 : :
2658 : : /*
2659 : : * The Result node's cost is cpu_tuple_cost per row, plus the cost of
2660 : : * evaluating the tlist. There is no qual to worry about.
2661 : : */
2662 : 12771 : pathnode->path.rows = subpath->rows;
2663 : 12771 : pathnode->path.disabled_nodes = subpath->disabled_nodes;
2664 : 12771 : pathnode->path.startup_cost = subpath->startup_cost +
2665 : 12771 : target->cost.startup;
2666 : 12771 : pathnode->path.total_cost = subpath->total_cost +
2667 : 12771 : target->cost.startup +
2668 : 12771 : (cpu_tuple_cost + target->cost.per_tuple) * subpath->rows;
2669 : : }
2670 : :
2671 : 317891 : return pathnode;
2672 : : }
2673 : :
2674 : : /*
2675 : : * apply_projection_to_path
2676 : : * Add a projection step, or just apply the target directly to given path.
2677 : : *
2678 : : * This has the same net effect as create_projection_path(), except that if
2679 : : * a separate Result plan node isn't needed, we just replace the given path's
2680 : : * pathtarget with the desired one. This must be used only when the caller
2681 : : * knows that the given path isn't referenced elsewhere and so can be modified
2682 : : * in-place.
2683 : : *
2684 : : * If the input path is a GatherPath or GatherMergePath, we try to push the
2685 : : * new target down to its input as well; this is a yet more invasive
2686 : : * modification of the input path, which create_projection_path() can't do.
2687 : : *
2688 : : * Note that we mustn't change the source path's parent link; so when it is
2689 : : * add_path'd to "rel" things will be a bit inconsistent. So far that has
2690 : : * not caused any trouble.
2691 : : *
2692 : : * 'rel' is the parent relation associated with the result
2693 : : * 'path' is the path representing the source of data
2694 : : * 'target' is the PathTarget to be computed
2695 : : */
2696 : : Path *
2697 : 11717 : apply_projection_to_path(PlannerInfo *root,
2698 : : RelOptInfo *rel,
2699 : : Path *path,
2700 : : PathTarget *target)
2701 : : {
2702 : : QualCost oldcost;
2703 : :
2704 : : /*
2705 : : * If given path can't project, we might need a Result node, so make a
2706 : : * separate ProjectionPath.
2707 : : */
2708 [ + + ]: 11717 : if (!is_projection_capable_path(path))
2709 : 1087 : return (Path *) create_projection_path(root, rel, path, target);
2710 : :
2711 : : /*
2712 : : * We can just jam the desired tlist into the existing path, being sure to
2713 : : * update its cost estimates appropriately.
2714 : : */
2715 : 10630 : oldcost = path->pathtarget->cost;
2716 : 10630 : path->pathtarget = target;
2717 : :
2718 : 10630 : path->startup_cost += target->cost.startup - oldcost.startup;
2719 : 10630 : path->total_cost += target->cost.startup - oldcost.startup +
2720 : 10630 : (target->cost.per_tuple - oldcost.per_tuple) * path->rows;
2721 : :
2722 : : /*
2723 : : * If the path happens to be a Gather or GatherMerge path, we'd like to
2724 : : * arrange for the subpath to return the required target list so that
2725 : : * workers can help project. But if there is something that is not
2726 : : * parallel-safe in the target expressions, then we can't.
2727 : : */
2728 [ + - + + : 10650 : if ((IsA(path, GatherPath) || IsA(path, GatherMergePath)) &&
+ - ]
2729 : 20 : is_parallel_safe(root, (Node *) target->exprs))
2730 : : {
2731 : : /*
2732 : : * We always use create_projection_path here, even if the subpath is
2733 : : * projection-capable, so as to avoid modifying the subpath in place.
2734 : : * It seems unlikely at present that there could be any other
2735 : : * references to the subpath, but better safe than sorry.
2736 : : *
2737 : : * Note that we don't change the parallel path's cost estimates; it
2738 : : * might be appropriate to do so, to reflect the fact that the bulk of
2739 : : * the target evaluation will happen in workers.
2740 : : */
2741 [ - + ]: 20 : if (IsA(path, GatherPath))
2742 : : {
2743 : 0 : GatherPath *gpath = (GatherPath *) path;
2744 : :
2745 : 0 : gpath->subpath = (Path *)
2746 : 0 : create_projection_path(root,
2747 : 0 : gpath->subpath->parent,
2748 : : gpath->subpath,
2749 : : target);
2750 : : }
2751 : : else
2752 : : {
2753 : 20 : GatherMergePath *gmpath = (GatherMergePath *) path;
2754 : :
2755 : 20 : gmpath->subpath = (Path *)
2756 : 20 : create_projection_path(root,
2757 : 20 : gmpath->subpath->parent,
2758 : : gmpath->subpath,
2759 : : target);
2760 : : }
2761 : : }
2762 [ + + ]: 10610 : else if (path->parallel_safe &&
2763 [ + + ]: 3769 : !is_parallel_safe(root, (Node *) target->exprs))
2764 : : {
2765 : : /*
2766 : : * We're inserting a parallel-restricted target list into a path
2767 : : * currently marked parallel-safe, so we have to mark it as no longer
2768 : : * safe.
2769 : : */
2770 : 10 : path->parallel_safe = false;
2771 : : }
2772 : :
2773 : 10630 : return path;
2774 : : }
2775 : :
2776 : : /*
2777 : : * create_set_projection_path
2778 : : * Creates a pathnode that represents performing a projection that
2779 : : * includes set-returning functions.
2780 : : *
2781 : : * 'rel' is the parent relation associated with the result
2782 : : * 'subpath' is the path representing the source of data
2783 : : * 'target' is the PathTarget to be computed
2784 : : */
2785 : : ProjectSetPath *
2786 : 10355 : create_set_projection_path(PlannerInfo *root,
2787 : : RelOptInfo *rel,
2788 : : Path *subpath,
2789 : : PathTarget *target)
2790 : : {
2791 : 10355 : ProjectSetPath *pathnode = makeNode(ProjectSetPath);
2792 : : double tlist_rows;
2793 : : ListCell *lc;
2794 : :
2795 : 10355 : pathnode->path.pathtype = T_ProjectSet;
2796 : 10355 : pathnode->path.parent = rel;
2797 : 10355 : pathnode->path.pathtarget = target;
2798 : : /* For now, assume we are above any joins, so no parameterization */
2799 : 10355 : pathnode->path.param_info = NULL;
2800 : 10355 : pathnode->path.parallel_aware = false;
2801 : 24341 : pathnode->path.parallel_safe = rel->consider_parallel &&
2802 [ + + + + : 13957 : subpath->parallel_safe &&
+ - ]
2803 : 3602 : is_parallel_safe(root, (Node *) target->exprs);
2804 : 10355 : pathnode->path.parallel_workers = subpath->parallel_workers;
2805 : : /* Projection does not change the sort order XXX? */
2806 : 10355 : pathnode->path.pathkeys = subpath->pathkeys;
2807 : :
2808 : 10355 : pathnode->subpath = subpath;
2809 : :
2810 : : /*
2811 : : * Estimate number of rows produced by SRFs for each row of input; if
2812 : : * there's more than one in this node, use the maximum.
2813 : : */
2814 : 10355 : tlist_rows = 1;
2815 [ + - + + : 22631 : foreach(lc, target->exprs)
+ + ]
2816 : : {
2817 : 12276 : Node *node = (Node *) lfirst(lc);
2818 : : double itemrows;
2819 : :
2820 : 12276 : itemrows = expression_returns_set_rows(root, node);
2821 [ + + ]: 12276 : if (tlist_rows < itemrows)
2822 : 9971 : tlist_rows = itemrows;
2823 : : }
2824 : :
2825 : : /*
2826 : : * In addition to the cost of evaluating the tlist, charge cpu_tuple_cost
2827 : : * per input row, and half of cpu_tuple_cost for each added output row.
2828 : : * This is slightly bizarre maybe, but it's what 9.6 did; we may revisit
2829 : : * this estimate later.
2830 : : */
2831 : 10355 : pathnode->path.disabled_nodes = subpath->disabled_nodes;
2832 : 10355 : pathnode->path.rows = subpath->rows * tlist_rows;
2833 : 10355 : pathnode->path.startup_cost = subpath->startup_cost +
2834 : 10355 : target->cost.startup;
2835 : 10355 : pathnode->path.total_cost = subpath->total_cost +
2836 : 10355 : target->cost.startup +
2837 : 10355 : (cpu_tuple_cost + target->cost.per_tuple) * subpath->rows +
2838 : 10355 : (pathnode->path.rows - subpath->rows) * cpu_tuple_cost / 2;
2839 : :
2840 : 10355 : return pathnode;
2841 : : }
2842 : :
2843 : : /*
2844 : : * create_incremental_sort_path
2845 : : * Creates a pathnode that represents performing an incremental sort.
2846 : : *
2847 : : * 'rel' is the parent relation associated with the result
2848 : : * 'subpath' is the path representing the source of data
2849 : : * 'pathkeys' represents the desired sort order
2850 : : * 'presorted_keys' is the number of keys by which the input path is
2851 : : * already sorted
2852 : : * 'limit_tuples' is the estimated bound on the number of output tuples,
2853 : : * or -1 if no LIMIT or couldn't estimate
2854 : : */
2855 : : IncrementalSortPath *
2856 : 8043 : create_incremental_sort_path(PlannerInfo *root,
2857 : : RelOptInfo *rel,
2858 : : Path *subpath,
2859 : : List *pathkeys,
2860 : : int presorted_keys,
2861 : : double limit_tuples)
2862 : : {
2863 : 8043 : IncrementalSortPath *sort = makeNode(IncrementalSortPath);
2864 : 8043 : SortPath *pathnode = &sort->spath;
2865 : :
2866 : 8043 : pathnode->path.pathtype = T_IncrementalSort;
2867 : 8043 : pathnode->path.parent = rel;
2868 : : /* Sort doesn't project, so use source path's pathtarget */
2869 : 8043 : pathnode->path.pathtarget = subpath->pathtarget;
2870 : 8043 : pathnode->path.param_info = subpath->param_info;
2871 : 8043 : pathnode->path.parallel_aware = false;
2872 [ + + ]: 12131 : pathnode->path.parallel_safe = rel->consider_parallel &&
2873 [ + + ]: 4088 : subpath->parallel_safe;
2874 : 8043 : pathnode->path.parallel_workers = subpath->parallel_workers;
2875 : 8043 : pathnode->path.pathkeys = pathkeys;
2876 : :
2877 : 8043 : pathnode->subpath = subpath;
2878 : :
2879 : 8043 : cost_incremental_sort(&pathnode->path,
2880 : : root, pathkeys, presorted_keys,
2881 : : subpath->disabled_nodes,
2882 : : subpath->startup_cost,
2883 : : subpath->total_cost,
2884 : : subpath->rows,
2885 : 8043 : subpath->pathtarget->width,
2886 : : 0.0, /* XXX comparison_cost shouldn't be 0? */
2887 : : work_mem, limit_tuples,
2888 : : &sort->numGroups);
2889 : :
2890 : 8043 : sort->nPresortedCols = presorted_keys;
2891 : :
2892 : 8043 : return sort;
2893 : : }
2894 : :
2895 : : /*
2896 : : * create_sort_path
2897 : : * Creates a pathnode that represents performing an explicit sort.
2898 : : *
2899 : : * 'rel' is the parent relation associated with the result
2900 : : * 'subpath' is the path representing the source of data
2901 : : * 'pathkeys' represents the desired sort order
2902 : : * 'limit_tuples' is the estimated bound on the number of output tuples,
2903 : : * or -1 if no LIMIT or couldn't estimate
2904 : : */
2905 : : SortPath *
2906 : 97337 : create_sort_path(PlannerInfo *root,
2907 : : RelOptInfo *rel,
2908 : : Path *subpath,
2909 : : List *pathkeys,
2910 : : double limit_tuples)
2911 : : {
2912 : 97337 : SortPath *pathnode = makeNode(SortPath);
2913 : :
2914 : 97337 : pathnode->path.pathtype = T_Sort;
2915 : 97337 : pathnode->path.parent = rel;
2916 : : /* Sort doesn't project, so use source path's pathtarget */
2917 : 97337 : pathnode->path.pathtarget = subpath->pathtarget;
2918 : 97337 : pathnode->path.param_info = subpath->param_info;
2919 : 97337 : pathnode->path.parallel_aware = false;
2920 [ + + ]: 170456 : pathnode->path.parallel_safe = rel->consider_parallel &&
2921 [ + + ]: 73119 : subpath->parallel_safe;
2922 : 97337 : pathnode->path.parallel_workers = subpath->parallel_workers;
2923 : 97337 : pathnode->path.pathkeys = pathkeys;
2924 : :
2925 : 97337 : pathnode->subpath = subpath;
2926 : :
2927 : 97337 : cost_sort(&pathnode->path, root, pathkeys,
2928 : : subpath->disabled_nodes,
2929 : : subpath->total_cost,
2930 : : subpath->rows,
2931 : 97337 : subpath->pathtarget->width,
2932 : : 0.0, /* XXX comparison_cost shouldn't be 0? */
2933 : : work_mem, limit_tuples);
2934 : :
2935 : 97337 : return pathnode;
2936 : : }
2937 : :
2938 : : /*
2939 : : * create_group_path
2940 : : * Creates a pathnode that represents performing grouping of presorted input
2941 : : *
2942 : : * 'rel' is the parent relation associated with the result
2943 : : * 'subpath' is the path representing the source of data
2944 : : * 'target' is the PathTarget to be computed
2945 : : * 'groupClause' is a list of SortGroupClause's representing the grouping
2946 : : * 'qual' is the HAVING quals if any
2947 : : * 'numGroups' is the estimated number of groups
2948 : : */
2949 : : GroupPath *
2950 : 1043 : create_group_path(PlannerInfo *root,
2951 : : RelOptInfo *rel,
2952 : : Path *subpath,
2953 : : List *groupClause,
2954 : : List *qual,
2955 : : double numGroups)
2956 : : {
2957 : 1043 : GroupPath *pathnode = makeNode(GroupPath);
2958 : 1043 : PathTarget *target = rel->reltarget;
2959 : :
2960 : 1043 : pathnode->path.pathtype = T_Group;
2961 : 1043 : pathnode->path.parent = rel;
2962 : 1043 : pathnode->path.pathtarget = target;
2963 : : /* For now, assume we are above any joins, so no parameterization */
2964 : 1043 : pathnode->path.param_info = NULL;
2965 : 1043 : pathnode->path.parallel_aware = false;
2966 [ + + ]: 1677 : pathnode->path.parallel_safe = rel->consider_parallel &&
2967 [ + + ]: 634 : subpath->parallel_safe;
2968 : 1043 : pathnode->path.parallel_workers = subpath->parallel_workers;
2969 : : /* Group doesn't change sort ordering */
2970 : 1043 : pathnode->path.pathkeys = subpath->pathkeys;
2971 : :
2972 : 1043 : pathnode->subpath = subpath;
2973 : :
2974 : 1043 : pathnode->groupClause = groupClause;
2975 : 1043 : pathnode->qual = qual;
2976 : :
2977 : 1043 : cost_group(&pathnode->path, root,
2978 : : list_length(groupClause),
2979 : : numGroups,
2980 : : qual,
2981 : : subpath->disabled_nodes,
2982 : : subpath->startup_cost, subpath->total_cost,
2983 : : subpath->rows);
2984 : :
2985 : : /* add tlist eval cost for each output row */
2986 : 1043 : pathnode->path.startup_cost += target->cost.startup;
2987 : 1043 : pathnode->path.total_cost += target->cost.startup +
2988 : 1043 : target->cost.per_tuple * pathnode->path.rows;
2989 : :
2990 : 1043 : return pathnode;
2991 : : }
2992 : :
2993 : : /*
2994 : : * create_unique_path
2995 : : * Creates a pathnode that represents performing an explicit Unique step
2996 : : * on presorted input.
2997 : : *
2998 : : * 'rel' is the parent relation associated with the result
2999 : : * 'subpath' is the path representing the source of data
3000 : : * 'numCols' is the number of grouping columns
3001 : : * 'numGroups' is the estimated number of groups
3002 : : *
3003 : : * The input path must be sorted on the grouping columns, plus possibly
3004 : : * additional columns; so the first numCols pathkeys are the grouping columns
3005 : : */
3006 : : UniquePath *
3007 : 18119 : create_unique_path(PlannerInfo *root,
3008 : : RelOptInfo *rel,
3009 : : Path *subpath,
3010 : : int numCols,
3011 : : double numGroups)
3012 : : {
3013 : 18119 : UniquePath *pathnode = makeNode(UniquePath);
3014 : :
3015 : 18119 : pathnode->path.pathtype = T_Unique;
3016 : 18119 : pathnode->path.parent = rel;
3017 : : /* Unique doesn't project, so use source path's pathtarget */
3018 : 18119 : pathnode->path.pathtarget = subpath->pathtarget;
3019 : 18119 : pathnode->path.param_info = subpath->param_info;
3020 : 18119 : pathnode->path.parallel_aware = false;
3021 [ + + ]: 32732 : pathnode->path.parallel_safe = rel->consider_parallel &&
3022 [ + + ]: 14613 : subpath->parallel_safe;
3023 : 18119 : pathnode->path.parallel_workers = subpath->parallel_workers;
3024 : : /* Unique doesn't change the input ordering */
3025 : 18119 : pathnode->path.pathkeys = subpath->pathkeys;
3026 : :
3027 : 18119 : pathnode->subpath = subpath;
3028 : 18119 : pathnode->numkeys = numCols;
3029 : :
3030 : : /*
3031 : : * Charge one cpu_operator_cost per comparison per input tuple. We assume
3032 : : * all columns get compared at most of the tuples. (XXX probably this is
3033 : : * an overestimate.)
3034 : : */
3035 : 18119 : pathnode->path.disabled_nodes = subpath->disabled_nodes;
3036 : 18119 : pathnode->path.startup_cost = subpath->startup_cost;
3037 : 18119 : pathnode->path.total_cost = subpath->total_cost +
3038 : 18119 : cpu_operator_cost * subpath->rows * numCols;
3039 : 18119 : pathnode->path.rows = numGroups;
3040 : :
3041 : : /*
3042 : : * Mark the path as disabled if enable_groupagg is off. While this isn't
3043 : : * a grouping Agg node, it is the sort-based way of removing duplicates
3044 : : * and so is the natural counterpart to the AGG_HASHED path that
3045 : : * enable_hashagg controls; it seems close enough to justify letting that
3046 : : * switch control it.
3047 : : */
3048 [ + + ]: 18119 : if (!enable_groupagg)
3049 : 42 : pathnode->path.disabled_nodes++;
3050 : :
3051 : 18119 : return pathnode;
3052 : : }
3053 : :
3054 : : /*
3055 : : * create_agg_path
3056 : : * Creates a pathnode that represents performing aggregation/grouping
3057 : : *
3058 : : * 'rel' is the parent relation associated with the result
3059 : : * 'subpath' is the path representing the source of data
3060 : : * 'target' is the PathTarget to be computed
3061 : : * 'aggstrategy' is the Agg node's basic implementation strategy
3062 : : * 'aggsplit' is the Agg node's aggregate-splitting mode
3063 : : * 'groupClause' is a list of SortGroupClause's representing the grouping
3064 : : * 'qual' is the HAVING quals if any
3065 : : * 'aggcosts' contains cost info about the aggregate functions to be computed
3066 : : * 'numGroups' is the estimated number of groups (1 if not grouping)
3067 : : */
3068 : : AggPath *
3069 : 69563 : create_agg_path(PlannerInfo *root,
3070 : : RelOptInfo *rel,
3071 : : Path *subpath,
3072 : : PathTarget *target,
3073 : : AggStrategy aggstrategy,
3074 : : AggSplit aggsplit,
3075 : : List *groupClause,
3076 : : List *qual,
3077 : : const AggClauseCosts *aggcosts,
3078 : : double numGroups)
3079 : : {
3080 : 69563 : AggPath *pathnode = makeNode(AggPath);
3081 : :
3082 : 69563 : pathnode->path.pathtype = T_Agg;
3083 : 69563 : pathnode->path.parent = rel;
3084 : 69563 : pathnode->path.pathtarget = target;
3085 : 69563 : pathnode->path.param_info = subpath->param_info;
3086 : 69563 : pathnode->path.parallel_aware = false;
3087 [ + + ]: 120270 : pathnode->path.parallel_safe = rel->consider_parallel &&
3088 [ + + ]: 50707 : subpath->parallel_safe;
3089 : 69563 : pathnode->path.parallel_workers = subpath->parallel_workers;
3090 : :
3091 [ + + ]: 69563 : if (aggstrategy == AGG_SORTED)
3092 : : {
3093 : : /*
3094 : : * Attempt to preserve the order of the subpath. Additional pathkeys
3095 : : * may have been added in adjust_group_pathkeys_for_groupagg() to
3096 : : * support ORDER BY / DISTINCT aggregates. Pathkeys added there
3097 : : * belong to columns within the aggregate function, so we must strip
3098 : : * these additional pathkeys off as those columns are unavailable
3099 : : * above the aggregate node.
3100 : : */
3101 [ + + ]: 11912 : if (list_length(subpath->pathkeys) > root->num_groupby_pathkeys)
3102 : 646 : pathnode->path.pathkeys = list_copy_head(subpath->pathkeys,
3103 : : root->num_groupby_pathkeys);
3104 : : else
3105 : 11266 : pathnode->path.pathkeys = subpath->pathkeys; /* preserves order */
3106 : : }
3107 : : else
3108 : 57651 : pathnode->path.pathkeys = NIL; /* output is unordered */
3109 : :
3110 : 69563 : pathnode->subpath = subpath;
3111 : :
3112 : 69563 : pathnode->aggstrategy = aggstrategy;
3113 : 69563 : pathnode->aggsplit = aggsplit;
3114 : 69563 : pathnode->numGroups = numGroups;
3115 [ + + ]: 69563 : pathnode->transitionSpace = aggcosts ? aggcosts->transitionSpace : 0;
3116 : 69563 : pathnode->groupClause = groupClause;
3117 : 69563 : pathnode->qual = qual;
3118 : :
3119 : 69563 : cost_agg(&pathnode->path, root,
3120 : : aggstrategy, aggcosts,
3121 : : list_length(groupClause), numGroups,
3122 : : qual,
3123 : : subpath->disabled_nodes,
3124 : : subpath->startup_cost, subpath->total_cost,
3125 : 69563 : subpath->rows, subpath->pathtarget->width);
3126 : :
3127 : : /* add tlist eval cost for each output row */
3128 : 69563 : pathnode->path.startup_cost += target->cost.startup;
3129 : 69563 : pathnode->path.total_cost += target->cost.startup +
3130 : 69563 : target->cost.per_tuple * pathnode->path.rows;
3131 : :
3132 : 69563 : return pathnode;
3133 : : }
3134 : :
3135 : : /*
3136 : : * create_groupingsets_path
3137 : : * Creates a pathnode that represents performing GROUPING SETS aggregation
3138 : : *
3139 : : * GroupingSetsPath represents sorted grouping with one or more grouping sets.
3140 : : * The input path's result must be sorted to match the last entry in
3141 : : * rollup_groupclauses.
3142 : : *
3143 : : * 'rel' is the parent relation associated with the result
3144 : : * 'subpath' is the path representing the source of data
3145 : : * 'target' is the PathTarget to be computed
3146 : : * 'having_qual' is the HAVING quals if any
3147 : : * 'rollups' is a list of RollupData nodes
3148 : : * 'agg_costs' contains cost info about the aggregate functions to be computed
3149 : : */
3150 : : GroupingSetsPath *
3151 : 2239 : create_groupingsets_path(PlannerInfo *root,
3152 : : RelOptInfo *rel,
3153 : : Path *subpath,
3154 : : List *having_qual,
3155 : : AggStrategy aggstrategy,
3156 : : List *rollups,
3157 : : const AggClauseCosts *agg_costs)
3158 : : {
3159 : 2239 : GroupingSetsPath *pathnode = makeNode(GroupingSetsPath);
3160 : 2239 : PathTarget *target = rel->reltarget;
3161 : : ListCell *lc;
3162 : 2239 : bool is_first = true;
3163 : 2239 : bool is_first_sort = true;
3164 : :
3165 : : /* The topmost generated Plan node will be an Agg */
3166 : 2239 : pathnode->path.pathtype = T_Agg;
3167 : 2239 : pathnode->path.parent = rel;
3168 : 2239 : pathnode->path.pathtarget = target;
3169 : 2239 : pathnode->path.param_info = subpath->param_info;
3170 : 2239 : pathnode->path.parallel_aware = false;
3171 [ + + ]: 3420 : pathnode->path.parallel_safe = rel->consider_parallel &&
3172 [ + + ]: 1181 : subpath->parallel_safe;
3173 : 2239 : pathnode->path.parallel_workers = subpath->parallel_workers;
3174 : 2239 : pathnode->subpath = subpath;
3175 : :
3176 : : /*
3177 : : * Simplify callers by downgrading AGG_SORTED to AGG_PLAIN, and AGG_MIXED
3178 : : * to AGG_HASHED, here if possible.
3179 : : */
3180 [ + + + + ]: 3185 : if (aggstrategy == AGG_SORTED &&
3181 : 946 : list_length(rollups) == 1 &&
3182 [ + + ]: 440 : ((RollupData *) linitial(rollups))->groupClause == NIL)
3183 : 45 : aggstrategy = AGG_PLAIN;
3184 : :
3185 [ + + - + ]: 3205 : if (aggstrategy == AGG_MIXED &&
3186 : 966 : list_length(rollups) == 1)
3187 : 0 : aggstrategy = AGG_HASHED;
3188 : :
3189 : : /*
3190 : : * Output will be in sorted order by group_pathkeys if, and only if, there
3191 : : * is a single rollup operation on a non-empty list of grouping
3192 : : * expressions.
3193 : : */
3194 [ + + + + ]: 2239 : if (aggstrategy == AGG_SORTED && list_length(rollups) == 1)
3195 : 395 : pathnode->path.pathkeys = root->group_pathkeys;
3196 : : else
3197 : 1844 : pathnode->path.pathkeys = NIL;
3198 : :
3199 : 2239 : pathnode->aggstrategy = aggstrategy;
3200 : 2239 : pathnode->rollups = rollups;
3201 : 2239 : pathnode->qual = having_qual;
3202 [ + - ]: 2239 : pathnode->transitionSpace = agg_costs ? agg_costs->transitionSpace : 0;
3203 : :
3204 : : Assert(rollups != NIL);
3205 : : Assert(aggstrategy != AGG_PLAIN || list_length(rollups) == 1);
3206 : : Assert(aggstrategy != AGG_MIXED || list_length(rollups) > 1);
3207 : :
3208 [ + - + + : 7550 : foreach(lc, rollups)
+ + ]
3209 : : {
3210 : 5311 : RollupData *rollup = lfirst(lc);
3211 : 5311 : List *gsets = rollup->gsets;
3212 : 5311 : int numGroupCols = list_length(linitial(gsets));
3213 : :
3214 : : /*
3215 : : * In AGG_SORTED or AGG_PLAIN mode, the first rollup takes the
3216 : : * (already-sorted) input, and following ones do their own sort.
3217 : : *
3218 : : * In AGG_HASHED mode, there is one rollup for each grouping set.
3219 : : *
3220 : : * In AGG_MIXED mode, the first rollups are hashed, the first
3221 : : * non-hashed one takes the (already-sorted) input, and following ones
3222 : : * do their own sort.
3223 : : */
3224 [ + + ]: 5311 : if (is_first)
3225 : : {
3226 : 2239 : cost_agg(&pathnode->path, root,
3227 : : aggstrategy,
3228 : : agg_costs,
3229 : : numGroupCols,
3230 : : rollup->numGroups,
3231 : : having_qual,
3232 : : subpath->disabled_nodes,
3233 : : subpath->startup_cost,
3234 : : subpath->total_cost,
3235 : : subpath->rows,
3236 : 2239 : subpath->pathtarget->width);
3237 : 2239 : is_first = false;
3238 [ + + ]: 2239 : if (!rollup->is_hashed)
3239 : 946 : is_first_sort = false;
3240 : : }
3241 : : else
3242 : : {
3243 : : Path sort_path; /* dummy for result of cost_sort */
3244 : : Path agg_path; /* dummy for result of cost_agg */
3245 : :
3246 [ + + + + ]: 3072 : if (rollup->is_hashed || is_first_sort)
3247 : : {
3248 : : /*
3249 : : * Account for cost of aggregation, but don't charge input
3250 : : * cost again
3251 : : */
3252 : 2316 : cost_agg(&agg_path, root,
3253 : 2316 : rollup->is_hashed ? AGG_HASHED : AGG_SORTED,
3254 : : agg_costs,
3255 : : numGroupCols,
3256 : : rollup->numGroups,
3257 : : having_qual,
3258 : : 0, 0.0, 0.0,
3259 : : subpath->rows,
3260 [ + + ]: 2316 : subpath->pathtarget->width);
3261 [ + + ]: 2316 : if (!rollup->is_hashed)
3262 : 966 : is_first_sort = false;
3263 : : }
3264 : : else
3265 : : {
3266 : : /* Account for cost of sort, but don't charge input cost again */
3267 : 756 : cost_sort(&sort_path, root, NIL, 0,
3268 : : 0.0,
3269 : : subpath->rows,
3270 : 756 : subpath->pathtarget->width,
3271 : : 0.0,
3272 : : work_mem,
3273 : : -1.0);
3274 : :
3275 : : /* Account for cost of aggregation */
3276 : :
3277 : 756 : cost_agg(&agg_path, root,
3278 : : AGG_SORTED,
3279 : : agg_costs,
3280 : : numGroupCols,
3281 : : rollup->numGroups,
3282 : : having_qual,
3283 : : sort_path.disabled_nodes,
3284 : : sort_path.startup_cost,
3285 : : sort_path.total_cost,
3286 : : sort_path.rows,
3287 : 756 : subpath->pathtarget->width);
3288 : : }
3289 : :
3290 : 3072 : pathnode->path.disabled_nodes += agg_path.disabled_nodes;
3291 : 3072 : pathnode->path.total_cost += agg_path.total_cost;
3292 : 3072 : pathnode->path.rows += agg_path.rows;
3293 : : }
3294 : : }
3295 : :
3296 : : /* add tlist eval cost for each output row */
3297 : 2239 : pathnode->path.startup_cost += target->cost.startup;
3298 : 2239 : pathnode->path.total_cost += target->cost.startup +
3299 : 2239 : target->cost.per_tuple * pathnode->path.rows;
3300 : :
3301 : 2239 : return pathnode;
3302 : : }
3303 : :
3304 : : /*
3305 : : * create_minmaxagg_path
3306 : : * Creates a pathnode that represents computation of MIN/MAX aggregates
3307 : : *
3308 : : * 'rel' is the parent relation associated with the result
3309 : : * 'target' is the PathTarget to be computed
3310 : : * 'mmaggregates' is a list of MinMaxAggInfo structs
3311 : : * 'quals' is the HAVING quals if any
3312 : : */
3313 : : MinMaxAggPath *
3314 : 331 : create_minmaxagg_path(PlannerInfo *root,
3315 : : RelOptInfo *rel,
3316 : : PathTarget *target,
3317 : : List *mmaggregates,
3318 : : List *quals)
3319 : : {
3320 : 331 : MinMaxAggPath *pathnode = makeNode(MinMaxAggPath);
3321 : : Cost initplan_cost;
3322 : 331 : int initplan_disabled_nodes = 0;
3323 : : ListCell *lc;
3324 : :
3325 : : /* The topmost generated Plan node will be a Result */
3326 : 331 : pathnode->path.pathtype = T_Result;
3327 : 331 : pathnode->path.parent = rel;
3328 : 331 : pathnode->path.pathtarget = target;
3329 : : /* For now, assume we are above any joins, so no parameterization */
3330 : 331 : pathnode->path.param_info = NULL;
3331 : 331 : pathnode->path.parallel_aware = false;
3332 : 331 : pathnode->path.parallel_safe = true; /* might change below */
3333 : 331 : pathnode->path.parallel_workers = 0;
3334 : : /* Result is one unordered row */
3335 : 331 : pathnode->path.rows = 1;
3336 : 331 : pathnode->path.pathkeys = NIL;
3337 : :
3338 : 331 : pathnode->mmaggregates = mmaggregates;
3339 : 331 : pathnode->quals = quals;
3340 : :
3341 : : /* Calculate cost of all the initplans, and check parallel safety */
3342 : 331 : initplan_cost = 0;
3343 [ + - + + : 696 : foreach(lc, mmaggregates)
+ + ]
3344 : : {
3345 : 365 : MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
3346 : :
3347 : 365 : initplan_disabled_nodes += mminfo->path->disabled_nodes;
3348 : 365 : initplan_cost += mminfo->pathcost;
3349 [ + + ]: 365 : if (!mminfo->path->parallel_safe)
3350 : 83 : pathnode->path.parallel_safe = false;
3351 : : }
3352 : :
3353 : : /* add tlist eval cost for each output row, plus cpu_tuple_cost */
3354 : 331 : pathnode->path.disabled_nodes = initplan_disabled_nodes;
3355 : 331 : pathnode->path.startup_cost = initplan_cost + target->cost.startup;
3356 : 331 : pathnode->path.total_cost = initplan_cost + target->cost.startup +
3357 : 331 : target->cost.per_tuple + cpu_tuple_cost;
3358 : :
3359 : : /*
3360 : : * Add cost of qual, if any --- but we ignore its selectivity, since our
3361 : : * rowcount estimate should be 1 no matter what the qual is.
3362 : : */
3363 [ - + ]: 331 : if (quals)
3364 : : {
3365 : : QualCost qual_cost;
3366 : :
3367 : 0 : cost_qual_eval(&qual_cost, quals, root);
3368 : 0 : pathnode->path.startup_cost += qual_cost.startup;
3369 : 0 : pathnode->path.total_cost += qual_cost.startup + qual_cost.per_tuple;
3370 : : }
3371 : :
3372 : : /*
3373 : : * If the initplans were all parallel-safe, also check safety of the
3374 : : * target and quals. (The Result node itself isn't parallelizable, but if
3375 : : * we are in a subquery then it can be useful for the outer query to know
3376 : : * that this one is parallel-safe.)
3377 : : */
3378 [ + + ]: 331 : if (pathnode->path.parallel_safe)
3379 : 252 : pathnode->path.parallel_safe =
3380 [ + - + - ]: 504 : is_parallel_safe(root, (Node *) target->exprs) &&
3381 : 504 : is_parallel_safe(root, (Node *) quals);
3382 : :
3383 : 331 : return pathnode;
3384 : : }
3385 : :
3386 : : /*
3387 : : * create_windowagg_path
3388 : : * Creates a pathnode that represents computation of window functions
3389 : : *
3390 : : * 'rel' is the parent relation associated with the result
3391 : : * 'subpath' is the path representing the source of data
3392 : : * 'target' is the PathTarget to be computed
3393 : : * 'windowFuncs' is a list of WindowFunc structs
3394 : : * 'runCondition' is a list of OpExprs to short-circuit WindowAgg execution
3395 : : * 'winclause' is a WindowClause that is common to all the WindowFuncs
3396 : : * 'qual' WindowClause.runconditions from lower-level WindowAggPaths.
3397 : : * Must always be NIL when topwindow == false
3398 : : * 'topwindow' pass as true only for the top-level WindowAgg. False for all
3399 : : * intermediate WindowAggs.
3400 : : *
3401 : : * The input must be sorted according to the WindowClause's PARTITION keys
3402 : : * plus ORDER BY keys.
3403 : : */
3404 : : WindowAggPath *
3405 : 2649 : create_windowagg_path(PlannerInfo *root,
3406 : : RelOptInfo *rel,
3407 : : Path *subpath,
3408 : : PathTarget *target,
3409 : : List *windowFuncs,
3410 : : List *runCondition,
3411 : : WindowClause *winclause,
3412 : : List *qual,
3413 : : bool topwindow)
3414 : : {
3415 : 2649 : WindowAggPath *pathnode = makeNode(WindowAggPath);
3416 : :
3417 : : /* qual can only be set for the topwindow */
3418 : : Assert(qual == NIL || topwindow);
3419 : :
3420 : 2649 : pathnode->path.pathtype = T_WindowAgg;
3421 : 2649 : pathnode->path.parent = rel;
3422 : 2649 : pathnode->path.pathtarget = target;
3423 : : /* For now, assume we are above any joins, so no parameterization */
3424 : 2649 : pathnode->path.param_info = NULL;
3425 : 2649 : pathnode->path.parallel_aware = false;
3426 [ - + ]: 2649 : pathnode->path.parallel_safe = rel->consider_parallel &&
3427 [ # # ]: 0 : subpath->parallel_safe;
3428 : 2649 : pathnode->path.parallel_workers = subpath->parallel_workers;
3429 : : /* WindowAgg preserves the input sort order */
3430 : 2649 : pathnode->path.pathkeys = subpath->pathkeys;
3431 : :
3432 : 2649 : pathnode->subpath = subpath;
3433 : 2649 : pathnode->winclause = winclause;
3434 : 2649 : pathnode->qual = qual;
3435 : 2649 : pathnode->runCondition = runCondition;
3436 : 2649 : pathnode->topwindow = topwindow;
3437 : :
3438 : : /*
3439 : : * For costing purposes, assume that there are no redundant partitioning
3440 : : * or ordering columns; it's not worth the trouble to deal with that
3441 : : * corner case here. So we just pass the unmodified list lengths to
3442 : : * cost_windowagg.
3443 : : */
3444 : 2649 : cost_windowagg(&pathnode->path, root,
3445 : : windowFuncs,
3446 : : winclause,
3447 : : subpath->disabled_nodes,
3448 : : subpath->startup_cost,
3449 : : subpath->total_cost,
3450 : : subpath->rows);
3451 : :
3452 : : /* add tlist eval cost for each output row */
3453 : 2649 : pathnode->path.startup_cost += target->cost.startup;
3454 : 2649 : pathnode->path.total_cost += target->cost.startup +
3455 : 2649 : target->cost.per_tuple * pathnode->path.rows;
3456 : :
3457 : 2649 : return pathnode;
3458 : : }
3459 : :
3460 : : /*
3461 : : * create_setop_path
3462 : : * Creates a pathnode that represents computation of INTERSECT or EXCEPT
3463 : : *
3464 : : * 'rel' is the parent relation associated with the result
3465 : : * 'leftpath' is the path representing the left-hand source of data
3466 : : * 'rightpath' is the path representing the right-hand source of data
3467 : : * 'cmd' is the specific semantics (INTERSECT or EXCEPT, with/without ALL)
3468 : : * 'strategy' is the implementation strategy (sorted or hashed)
3469 : : * 'groupList' is a list of SortGroupClause's representing the grouping
3470 : : * 'numGroups' is the estimated number of distinct groups in left-hand input
3471 : : * 'outputRows' is the estimated number of output rows
3472 : : *
3473 : : * leftpath and rightpath must produce the same columns. Moreover, if
3474 : : * strategy is SETOP_SORTED, leftpath and rightpath must both be sorted
3475 : : * by all the grouping columns.
3476 : : */
3477 : : SetOpPath *
3478 : 1234 : create_setop_path(PlannerInfo *root,
3479 : : RelOptInfo *rel,
3480 : : Path *leftpath,
3481 : : Path *rightpath,
3482 : : SetOpCmd cmd,
3483 : : SetOpStrategy strategy,
3484 : : List *groupList,
3485 : : double numGroups,
3486 : : double outputRows)
3487 : : {
3488 : 1234 : SetOpPath *pathnode = makeNode(SetOpPath);
3489 : :
3490 : 1234 : pathnode->path.pathtype = T_SetOp;
3491 : 1234 : pathnode->path.parent = rel;
3492 : 1234 : pathnode->path.pathtarget = rel->reltarget;
3493 : : /* For now, assume we are above any joins, so no parameterization */
3494 : 1234 : pathnode->path.param_info = NULL;
3495 : 1234 : pathnode->path.parallel_aware = false;
3496 : 2468 : pathnode->path.parallel_safe = rel->consider_parallel &&
3497 [ - + - - : 1234 : leftpath->parallel_safe && rightpath->parallel_safe;
- - ]
3498 : 1234 : pathnode->path.parallel_workers =
3499 : 1234 : leftpath->parallel_workers + rightpath->parallel_workers;
3500 : : /* SetOp preserves the input sort order if in sort mode */
3501 : 1234 : pathnode->path.pathkeys =
3502 [ + + ]: 1234 : (strategy == SETOP_SORTED) ? leftpath->pathkeys : NIL;
3503 : :
3504 : 1234 : pathnode->leftpath = leftpath;
3505 : 1234 : pathnode->rightpath = rightpath;
3506 : 1234 : pathnode->cmd = cmd;
3507 : 1234 : pathnode->strategy = strategy;
3508 : 1234 : pathnode->groupList = groupList;
3509 : 1234 : pathnode->numGroups = numGroups;
3510 : :
3511 : : /*
3512 : : * Compute cost estimates. As things stand, we end up with the same total
3513 : : * cost in this node for sort and hash methods, but different startup
3514 : : * costs. This could be refined perhaps, but it'll do for now.
3515 : : */
3516 : 1234 : pathnode->path.disabled_nodes =
3517 : 1234 : leftpath->disabled_nodes + rightpath->disabled_nodes;
3518 [ + + ]: 1234 : if (strategy == SETOP_SORTED)
3519 : : {
3520 : : /*
3521 : : * In sorted mode, we can emit output incrementally. Charge one
3522 : : * cpu_operator_cost per comparison per input tuple. Like cost_group,
3523 : : * we assume all columns get compared at most of the tuples.
3524 : : */
3525 : 642 : pathnode->path.startup_cost =
3526 : 642 : leftpath->startup_cost + rightpath->startup_cost;
3527 : 642 : pathnode->path.total_cost =
3528 : 1284 : leftpath->total_cost + rightpath->total_cost +
3529 : 642 : cpu_operator_cost * (leftpath->rows + rightpath->rows) * list_length(groupList);
3530 : :
3531 : : /*
3532 : : * Also charge a small amount per extracted tuple. Like cost_sort,
3533 : : * charge only operator cost not cpu_tuple_cost, since SetOp does no
3534 : : * qual-checking or projection.
3535 : : */
3536 : 642 : pathnode->path.total_cost += cpu_operator_cost * outputRows;
3537 : :
3538 : : /*
3539 : : * Mark the path as disabled if enable_groupagg is off. While this
3540 : : * isn't a grouping Agg node, it is the sort-based implementation and
3541 : : * so is the natural counterpart to the SETOP_HASHED path that
3542 : : * enable_hashagg controls; it seems close enough to justify letting
3543 : : * that switch control it.
3544 : : */
3545 [ + + ]: 642 : if (!enable_groupagg)
3546 : 55 : pathnode->path.disabled_nodes++;
3547 : : }
3548 : : else
3549 : : {
3550 : : Size hashtablesize;
3551 : :
3552 : : /*
3553 : : * In hashed mode, we must read all the input before we can emit
3554 : : * anything. Also charge comparison costs to represent the cost of
3555 : : * hash table lookups.
3556 : : */
3557 : 592 : pathnode->path.startup_cost =
3558 : 1184 : leftpath->total_cost + rightpath->total_cost +
3559 : 592 : cpu_operator_cost * (leftpath->rows + rightpath->rows) * list_length(groupList);
3560 : 592 : pathnode->path.total_cost = pathnode->path.startup_cost;
3561 : :
3562 : : /*
3563 : : * Also charge a small amount per extracted tuple. Like cost_sort,
3564 : : * charge only operator cost not cpu_tuple_cost, since SetOp does no
3565 : : * qual-checking or projection.
3566 : : */
3567 : 592 : pathnode->path.total_cost += cpu_operator_cost * outputRows;
3568 : :
3569 : : /*
3570 : : * Mark the path as disabled if enable_hashagg is off. While this
3571 : : * isn't exactly a HashAgg node, it seems close enough to justify
3572 : : * letting that switch control it.
3573 : : */
3574 [ + + ]: 592 : if (!enable_hashagg)
3575 : 95 : pathnode->path.disabled_nodes++;
3576 : :
3577 : : /*
3578 : : * Also disable if it doesn't look like the hashtable will fit into
3579 : : * hash_mem. (Note: reject on equality, to ensure that an estimate of
3580 : : * SIZE_MAX disables hashing regardless of the hash_mem limit.)
3581 : : */
3582 : 592 : hashtablesize = EstimateSetOpHashTableSpace(numGroups,
3583 : 592 : leftpath->pathtarget->width);
3584 [ - + ]: 592 : if (hashtablesize >= get_hash_memory_limit())
3585 : 0 : pathnode->path.disabled_nodes++;
3586 : : }
3587 : 1234 : pathnode->path.rows = outputRows;
3588 : :
3589 : 1234 : return pathnode;
3590 : : }
3591 : :
3592 : : /*
3593 : : * create_recursiveunion_path
3594 : : * Creates a pathnode that represents a recursive UNION node
3595 : : *
3596 : : * 'rel' is the parent relation associated with the result
3597 : : * 'leftpath' is the source of data for the non-recursive term
3598 : : * 'rightpath' is the source of data for the recursive term
3599 : : * 'target' is the PathTarget to be computed
3600 : : * 'distinctList' is a list of SortGroupClause's representing the grouping
3601 : : * 'wtParam' is the ID of Param representing work table
3602 : : * 'numGroups' is the estimated number of groups
3603 : : *
3604 : : * For recursive UNION ALL, distinctList is empty and numGroups is zero
3605 : : */
3606 : : RecursiveUnionPath *
3607 : 638 : create_recursiveunion_path(PlannerInfo *root,
3608 : : RelOptInfo *rel,
3609 : : Path *leftpath,
3610 : : Path *rightpath,
3611 : : PathTarget *target,
3612 : : List *distinctList,
3613 : : int wtParam,
3614 : : double numGroups)
3615 : : {
3616 : 638 : RecursiveUnionPath *pathnode = makeNode(RecursiveUnionPath);
3617 : :
3618 : 638 : pathnode->path.pathtype = T_RecursiveUnion;
3619 : 638 : pathnode->path.parent = rel;
3620 : 638 : pathnode->path.pathtarget = target;
3621 : : /* For now, assume we are above any joins, so no parameterization */
3622 : 638 : pathnode->path.param_info = NULL;
3623 : 638 : pathnode->path.parallel_aware = false;
3624 : 1276 : pathnode->path.parallel_safe = rel->consider_parallel &&
3625 [ - + - - : 638 : leftpath->parallel_safe && rightpath->parallel_safe;
- - ]
3626 : : /* Foolish, but we'll do it like joins for now: */
3627 : 638 : pathnode->path.parallel_workers = leftpath->parallel_workers;
3628 : : /* RecursiveUnion result is always unsorted */
3629 : 638 : pathnode->path.pathkeys = NIL;
3630 : :
3631 : 638 : pathnode->leftpath = leftpath;
3632 : 638 : pathnode->rightpath = rightpath;
3633 : 638 : pathnode->distinctList = distinctList;
3634 : 638 : pathnode->wtParam = wtParam;
3635 : 638 : pathnode->numGroups = numGroups;
3636 : :
3637 : 638 : cost_recursive_union(&pathnode->path, leftpath, rightpath);
3638 : :
3639 : 638 : return pathnode;
3640 : : }
3641 : :
3642 : : /*
3643 : : * create_lockrows_path
3644 : : * Creates a pathnode that represents acquiring row locks
3645 : : *
3646 : : * 'rel' is the parent relation associated with the result
3647 : : * 'subpath' is the path representing the source of data
3648 : : * 'rowMarks' is a list of PlanRowMark's
3649 : : * 'epqParam' is the ID of Param for EvalPlanQual re-eval
3650 : : */
3651 : : LockRowsPath *
3652 : 6834 : create_lockrows_path(PlannerInfo *root, RelOptInfo *rel,
3653 : : Path *subpath, List *rowMarks, int epqParam)
3654 : : {
3655 : 6834 : LockRowsPath *pathnode = makeNode(LockRowsPath);
3656 : :
3657 : 6834 : pathnode->path.pathtype = T_LockRows;
3658 : 6834 : pathnode->path.parent = rel;
3659 : : /* LockRows doesn't project, so use source path's pathtarget */
3660 : 6834 : pathnode->path.pathtarget = subpath->pathtarget;
3661 : : /* For now, assume we are above any joins, so no parameterization */
3662 : 6834 : pathnode->path.param_info = NULL;
3663 : 6834 : pathnode->path.parallel_aware = false;
3664 : 6834 : pathnode->path.parallel_safe = false;
3665 : 6834 : pathnode->path.parallel_workers = 0;
3666 : 6834 : pathnode->path.rows = subpath->rows;
3667 : :
3668 : : /*
3669 : : * The result cannot be assumed sorted, since locking might cause the sort
3670 : : * key columns to be replaced with new values.
3671 : : */
3672 : 6834 : pathnode->path.pathkeys = NIL;
3673 : :
3674 : 6834 : pathnode->subpath = subpath;
3675 : 6834 : pathnode->rowMarks = rowMarks;
3676 : 6834 : pathnode->epqParam = epqParam;
3677 : :
3678 : : /*
3679 : : * We should charge something extra for the costs of row locking and
3680 : : * possible refetches, but it's hard to say how much. For now, use
3681 : : * cpu_tuple_cost per row.
3682 : : */
3683 : 6834 : pathnode->path.disabled_nodes = subpath->disabled_nodes;
3684 : 6834 : pathnode->path.startup_cost = subpath->startup_cost;
3685 : 6834 : pathnode->path.total_cost = subpath->total_cost +
3686 : 6834 : cpu_tuple_cost * subpath->rows;
3687 : :
3688 : 6834 : return pathnode;
3689 : : }
3690 : :
3691 : : /*
3692 : : * create_modifytable_path
3693 : : * Creates a pathnode that represents performing INSERT/UPDATE/DELETE/MERGE
3694 : : * mods
3695 : : *
3696 : : * 'rel' is the parent relation associated with the result
3697 : : * 'subpath' is a Path producing source data
3698 : : * 'operation' is the operation type
3699 : : * 'canSetTag' is true if we set the command tag/es_processed
3700 : : * 'nominalRelation' is the parent RT index for use of EXPLAIN
3701 : : * 'rootRelation' is the partitioned/inherited table root RTI, or 0 if none
3702 : : * 'resultRelations' is an integer list of actual RT indexes of target rel(s)
3703 : : * 'updateColnosLists' is a list of UPDATE target column number lists
3704 : : * (one sublist per rel); or NIL if not an UPDATE
3705 : : * 'withCheckOptionLists' is a list of WCO lists (one per rel)
3706 : : * 'returningLists' is a list of RETURNING tlists (one per rel)
3707 : : * 'rowMarks' is a list of PlanRowMarks (non-locking only)
3708 : : * 'onconflict' is the ON CONFLICT clause, or NULL
3709 : : * 'epqParam' is the ID of Param for EvalPlanQual re-eval
3710 : : * 'mergeActionLists' is a list of lists of MERGE actions (one per rel)
3711 : : * 'mergeJoinConditions' is a list of join conditions for MERGE (one per rel)
3712 : : */
3713 : : ModifyTablePath *
3714 : 65759 : create_modifytable_path(PlannerInfo *root, RelOptInfo *rel,
3715 : : Path *subpath,
3716 : : CmdType operation, bool canSetTag,
3717 : : Index nominalRelation, Index rootRelation,
3718 : : List *resultRelations,
3719 : : List *updateColnosLists,
3720 : : List *withCheckOptionLists, List *returningLists,
3721 : : List *rowMarks, OnConflictExpr *onconflict,
3722 : : List *mergeActionLists, List *mergeJoinConditions,
3723 : : ForPortionOfExpr *forPortionOf, int epqParam)
3724 : : {
3725 : 65759 : ModifyTablePath *pathnode = makeNode(ModifyTablePath);
3726 : :
3727 : : Assert(operation == CMD_MERGE ||
3728 : : (operation == CMD_UPDATE ?
3729 : : list_length(resultRelations) == list_length(updateColnosLists) :
3730 : : updateColnosLists == NIL));
3731 : : Assert(withCheckOptionLists == NIL ||
3732 : : list_length(resultRelations) == list_length(withCheckOptionLists));
3733 : : Assert(returningLists == NIL ||
3734 : : list_length(resultRelations) == list_length(returningLists));
3735 : :
3736 : 65759 : pathnode->path.pathtype = T_ModifyTable;
3737 : 65759 : pathnode->path.parent = rel;
3738 : : /* pathtarget is not interesting, just make it minimally valid */
3739 : 65759 : pathnode->path.pathtarget = rel->reltarget;
3740 : : /* For now, assume we are above any joins, so no parameterization */
3741 : 65759 : pathnode->path.param_info = NULL;
3742 : 65759 : pathnode->path.parallel_aware = false;
3743 : 65759 : pathnode->path.parallel_safe = false;
3744 : 65759 : pathnode->path.parallel_workers = 0;
3745 : 65759 : pathnode->path.pathkeys = NIL;
3746 : :
3747 : : /*
3748 : : * Compute cost & rowcount as subpath cost & rowcount (if RETURNING)
3749 : : *
3750 : : * Currently, we don't charge anything extra for the actual table
3751 : : * modification work, nor for the WITH CHECK OPTIONS or RETURNING
3752 : : * expressions if any. It would only be window dressing, since
3753 : : * ModifyTable is always a top-level node and there is no way for the
3754 : : * costs to change any higher-level planning choices. But we might want
3755 : : * to make it look better sometime.
3756 : : */
3757 : 65759 : pathnode->path.disabled_nodes = subpath->disabled_nodes;
3758 : 65759 : pathnode->path.startup_cost = subpath->startup_cost;
3759 : 65759 : pathnode->path.total_cost = subpath->total_cost;
3760 [ + + ]: 65759 : if (returningLists != NIL)
3761 : : {
3762 : 2604 : pathnode->path.rows = subpath->rows;
3763 : :
3764 : : /*
3765 : : * Set width to match the subpath output. XXX this is totally wrong:
3766 : : * we should return an average of the RETURNING tlist widths. But
3767 : : * it's what happened historically, and improving it is a task for
3768 : : * another day. (Again, it's mostly window dressing.)
3769 : : */
3770 : 2604 : pathnode->path.pathtarget->width = subpath->pathtarget->width;
3771 : : }
3772 : : else
3773 : : {
3774 : 63155 : pathnode->path.rows = 0;
3775 : 63155 : pathnode->path.pathtarget->width = 0;
3776 : : }
3777 : :
3778 : 65759 : pathnode->subpath = subpath;
3779 : 65759 : pathnode->operation = operation;
3780 : 65759 : pathnode->canSetTag = canSetTag;
3781 : 65759 : pathnode->nominalRelation = nominalRelation;
3782 : 65759 : pathnode->rootRelation = rootRelation;
3783 : 65759 : pathnode->resultRelations = resultRelations;
3784 : 65759 : pathnode->updateColnosLists = updateColnosLists;
3785 : 65759 : pathnode->withCheckOptionLists = withCheckOptionLists;
3786 : 65759 : pathnode->returningLists = returningLists;
3787 : 65759 : pathnode->rowMarks = rowMarks;
3788 : 65759 : pathnode->onconflict = onconflict;
3789 : 65759 : pathnode->forPortionOf = forPortionOf;
3790 : 65759 : pathnode->epqParam = epqParam;
3791 : 65759 : pathnode->mergeActionLists = mergeActionLists;
3792 : 65759 : pathnode->mergeJoinConditions = mergeJoinConditions;
3793 : :
3794 : 65759 : return pathnode;
3795 : : }
3796 : :
3797 : : /*
3798 : : * create_limit_path
3799 : : * Creates a pathnode that represents performing LIMIT/OFFSET
3800 : : *
3801 : : * In addition to providing the actual OFFSET and LIMIT expressions,
3802 : : * the caller must provide estimates of their values for costing purposes.
3803 : : * The estimates are as computed by preprocess_limit(), ie, 0 represents
3804 : : * the clause not being present, and -1 means it's present but we could
3805 : : * not estimate its value.
3806 : : *
3807 : : * 'rel' is the parent relation associated with the result
3808 : : * 'subpath' is the path representing the source of data
3809 : : * 'limitOffset' is the actual OFFSET expression, or NULL
3810 : : * 'limitCount' is the actual LIMIT expression, or NULL
3811 : : * 'offset_est' is the estimated value of the OFFSET expression
3812 : : * 'count_est' is the estimated value of the LIMIT expression
3813 : : */
3814 : : LimitPath *
3815 : 4346 : create_limit_path(PlannerInfo *root, RelOptInfo *rel,
3816 : : Path *subpath,
3817 : : Node *limitOffset, Node *limitCount,
3818 : : LimitOption limitOption,
3819 : : int64 offset_est, int64 count_est)
3820 : : {
3821 : 4346 : LimitPath *pathnode = makeNode(LimitPath);
3822 : :
3823 : 4346 : pathnode->path.pathtype = T_Limit;
3824 : 4346 : pathnode->path.parent = rel;
3825 : : /* Limit doesn't project, so use source path's pathtarget */
3826 : 4346 : pathnode->path.pathtarget = subpath->pathtarget;
3827 : : /* For now, assume we are above any joins, so no parameterization */
3828 : 4346 : pathnode->path.param_info = NULL;
3829 : 4346 : pathnode->path.parallel_aware = false;
3830 [ + + ]: 6066 : pathnode->path.parallel_safe = rel->consider_parallel &&
3831 [ + + ]: 1720 : subpath->parallel_safe;
3832 : 4346 : pathnode->path.parallel_workers = subpath->parallel_workers;
3833 : 4346 : pathnode->path.rows = subpath->rows;
3834 : 4346 : pathnode->path.disabled_nodes = subpath->disabled_nodes;
3835 : 4346 : pathnode->path.startup_cost = subpath->startup_cost;
3836 : 4346 : pathnode->path.total_cost = subpath->total_cost;
3837 : 4346 : pathnode->path.pathkeys = subpath->pathkeys;
3838 : 4346 : pathnode->subpath = subpath;
3839 : 4346 : pathnode->limitOffset = limitOffset;
3840 : 4346 : pathnode->limitCount = limitCount;
3841 : 4346 : pathnode->limitOption = limitOption;
3842 : :
3843 : : /*
3844 : : * Adjust the output rows count and costs according to the offset/limit.
3845 : : */
3846 : 4346 : adjust_limit_rows_costs(&pathnode->path.rows,
3847 : : &pathnode->path.startup_cost,
3848 : : &pathnode->path.total_cost,
3849 : : offset_est, count_est);
3850 : :
3851 : 4346 : return pathnode;
3852 : : }
3853 : :
3854 : : /*
3855 : : * adjust_limit_rows_costs
3856 : : * Adjust the size and cost estimates for a LimitPath node according to the
3857 : : * offset/limit.
3858 : : *
3859 : : * This is only a cosmetic issue if we are at top level, but if we are
3860 : : * building a subquery then it's important to report correct info to the outer
3861 : : * planner.
3862 : : *
3863 : : * When the offset or count couldn't be estimated, use 10% of the estimated
3864 : : * number of rows emitted from the subpath.
3865 : : *
3866 : : * XXX we don't bother to add eval costs of the offset/limit expressions
3867 : : * themselves to the path costs. In theory we should, but in most cases those
3868 : : * expressions are trivial and it's just not worth the trouble.
3869 : : */
3870 : : void
3871 : 4439 : adjust_limit_rows_costs(double *rows, /* in/out parameter */
3872 : : Cost *startup_cost, /* in/out parameter */
3873 : : Cost *total_cost, /* in/out parameter */
3874 : : int64 offset_est,
3875 : : int64 count_est)
3876 : : {
3877 : 4439 : double input_rows = *rows;
3878 : 4439 : Cost input_startup_cost = *startup_cost;
3879 : 4439 : Cost input_total_cost = *total_cost;
3880 : :
3881 [ + + ]: 4439 : if (offset_est != 0)
3882 : : {
3883 : : double offset_rows;
3884 : :
3885 [ + + ]: 403 : if (offset_est > 0)
3886 : 383 : offset_rows = (double) offset_est;
3887 : : else
3888 : 20 : offset_rows = clamp_row_est(input_rows * 0.10);
3889 [ + + ]: 403 : if (offset_rows > *rows)
3890 : 26 : offset_rows = *rows;
3891 [ + - ]: 403 : if (input_rows > 0)
3892 : 403 : *startup_cost +=
3893 : 403 : (input_total_cost - input_startup_cost)
3894 : 403 : * offset_rows / input_rows;
3895 : 403 : *rows -= offset_rows;
3896 [ + + ]: 403 : if (*rows < 1)
3897 : 30 : *rows = 1;
3898 : : }
3899 : :
3900 [ + + ]: 4439 : if (count_est != 0)
3901 : : {
3902 : : double count_rows;
3903 : :
3904 [ + + ]: 4383 : if (count_est > 0)
3905 : 4378 : count_rows = (double) count_est;
3906 : : else
3907 : 5 : count_rows = clamp_row_est(input_rows * 0.10);
3908 [ + + ]: 4383 : if (count_rows > *rows)
3909 : 150 : count_rows = *rows;
3910 [ + - ]: 4383 : if (input_rows > 0)
3911 : 4383 : *total_cost = *startup_cost +
3912 : 4383 : (input_total_cost - input_startup_cost)
3913 : 4383 : * count_rows / input_rows;
3914 : 4383 : *rows = count_rows;
3915 [ - + ]: 4383 : if (*rows < 1)
3916 : 0 : *rows = 1;
3917 : : }
3918 : 4439 : }
3919 : :
3920 : :
3921 : : /*
3922 : : * reparameterize_path
3923 : : * Attempt to modify a Path to have greater parameterization
3924 : : *
3925 : : * We use this to attempt to bring all child paths of an appendrel to the
3926 : : * same parameterization level, ensuring that they all enforce the same set
3927 : : * of join quals (and thus that that parameterization can be attributed to
3928 : : * an append path built from such paths). Currently, only a few path types
3929 : : * are supported here, though more could be added at need. We return NULL
3930 : : * if we can't reparameterize the given path.
3931 : : *
3932 : : * Note: we intentionally do not pass created paths to add_path(); it would
3933 : : * possibly try to delete them on the grounds of being cost-inferior to the
3934 : : * paths they were made from, and we don't want that. Paths made here are
3935 : : * not necessarily of general-purpose usefulness, but they can be useful
3936 : : * as members of an append path.
3937 : : */
3938 : : Path *
3939 : 858 : reparameterize_path(PlannerInfo *root, Path *path,
3940 : : Relids required_outer,
3941 : : double loop_count)
3942 : : {
3943 : 858 : RelOptInfo *rel = path->parent;
3944 : :
3945 : : /* Can only increase, not decrease, path's parameterization */
3946 [ - + - + ]: 858 : if (!bms_is_subset(PATH_REQ_OUTER(path), required_outer))
3947 : 0 : return NULL;
3948 [ + - - - : 858 : switch (path->pathtype)
- + + - -
+ ]
3949 : : {
3950 : 730 : case T_SeqScan:
3951 : 730 : return create_seqscan_path(root, rel, required_outer, 0);
3952 : 0 : case T_SampleScan:
3953 : 0 : return create_samplescan_path(root, rel, required_outer);
3954 : 0 : case T_IndexScan:
3955 : : case T_IndexOnlyScan:
3956 : : {
3957 : 0 : IndexPath *ipath = (IndexPath *) path;
3958 : 0 : IndexPath *newpath = makeNode(IndexPath);
3959 : :
3960 : : /*
3961 : : * We can't use create_index_path directly, and would not want
3962 : : * to because it would re-compute the indexqual conditions
3963 : : * which is wasted effort. Instead we hack things a bit:
3964 : : * flat-copy the path node, revise its param_info, and redo
3965 : : * the cost estimate.
3966 : : */
3967 : 0 : memcpy(newpath, ipath, sizeof(IndexPath));
3968 : 0 : newpath->path.param_info =
3969 : 0 : get_baserel_parampathinfo(root, rel, required_outer);
3970 : 0 : cost_index(newpath, root, loop_count, false);
3971 : 0 : return (Path *) newpath;
3972 : : }
3973 : 0 : case T_BitmapHeapScan:
3974 : : {
3975 : 0 : BitmapHeapPath *bpath = (BitmapHeapPath *) path;
3976 : :
3977 : 0 : return (Path *) create_bitmap_heap_path(root,
3978 : : rel,
3979 : : bpath->bitmapqual,
3980 : : required_outer,
3981 : : loop_count, 0);
3982 : : }
3983 : 0 : case T_SubqueryScan:
3984 : : {
3985 : 0 : SubqueryScanPath *spath = (SubqueryScanPath *) path;
3986 : 0 : Path *subpath = spath->subpath;
3987 : : bool trivial_pathtarget;
3988 : :
3989 : : /*
3990 : : * If existing node has zero extra cost, we must have decided
3991 : : * its target is trivial. (The converse is not true, because
3992 : : * it might have a trivial target but quals to enforce; but in
3993 : : * that case the new node will too, so it doesn't matter
3994 : : * whether we get the right answer here.)
3995 : : */
3996 : 0 : trivial_pathtarget =
3997 : 0 : (subpath->total_cost == spath->path.total_cost);
3998 : :
3999 : 0 : return (Path *) create_subqueryscan_path(root,
4000 : : rel,
4001 : : subpath,
4002 : : trivial_pathtarget,
4003 : : spath->path.pathkeys,
4004 : : required_outer);
4005 : : }
4006 : 65 : case T_Result:
4007 : : /* Supported only for RTE_RESULT scan paths */
4008 [ + - ]: 65 : if (IsA(path, Path))
4009 : 65 : return create_resultscan_path(root, rel, required_outer);
4010 : 0 : break;
4011 : 5 : case T_Append:
4012 : : {
4013 : 5 : AppendPath *apath = (AppendPath *) path;
4014 : 5 : AppendPathInput new_append = {0};
4015 : : int i;
4016 : : ListCell *lc;
4017 : :
4018 : 5 : new_append.child_append_relid_sets = apath->child_append_relid_sets;
4019 : :
4020 : : /* Reparameterize the children */
4021 : 5 : i = 0;
4022 [ + - + + : 10 : foreach(lc, apath->subpaths)
+ + ]
4023 : : {
4024 : 5 : Path *spath = (Path *) lfirst(lc);
4025 : :
4026 : 5 : spath = reparameterize_path(root, spath,
4027 : : required_outer,
4028 : : loop_count);
4029 [ - + ]: 5 : if (spath == NULL)
4030 : 0 : return NULL;
4031 : : /* We have to re-split the regular and partial paths */
4032 [ + - ]: 5 : if (i < apath->first_partial_path)
4033 : 5 : new_append.subpaths = lappend(new_append.subpaths, spath);
4034 : : else
4035 : 0 : new_append.partial_subpaths = lappend(new_append.partial_subpaths, spath);
4036 : 5 : i++;
4037 : : }
4038 : 5 : return (Path *)
4039 : 5 : create_append_path(root, rel, new_append,
4040 : : apath->path.pathkeys, required_outer,
4041 : : apath->path.parallel_workers,
4042 : 5 : apath->path.parallel_aware,
4043 : : -1);
4044 : : }
4045 : 0 : case T_Material:
4046 : : {
4047 : 0 : MaterialPath *mpath = (MaterialPath *) path;
4048 : 0 : Path *spath = mpath->subpath;
4049 : : bool enabled;
4050 : :
4051 : 0 : spath = reparameterize_path(root, spath,
4052 : : required_outer,
4053 : : loop_count);
4054 [ # # ]: 0 : if (spath == NULL)
4055 : 0 : return NULL;
4056 : 0 : enabled =
4057 : 0 : (mpath->path.disabled_nodes <= spath->disabled_nodes);
4058 : 0 : return (Path *) create_material_path(rel, spath, enabled);
4059 : : }
4060 : 0 : case T_Memoize:
4061 : : {
4062 : 0 : MemoizePath *mpath = (MemoizePath *) path;
4063 : 0 : Path *spath = mpath->subpath;
4064 : :
4065 : 0 : spath = reparameterize_path(root, spath,
4066 : : required_outer,
4067 : : loop_count);
4068 [ # # ]: 0 : if (spath == NULL)
4069 : 0 : return NULL;
4070 : 0 : return (Path *) create_memoize_path(root, rel,
4071 : : spath,
4072 : : mpath->param_exprs,
4073 : : mpath->hash_operators,
4074 : 0 : mpath->singlerow,
4075 : 0 : mpath->binary_mode,
4076 : : mpath->est_calls);
4077 : : }
4078 : 58 : default:
4079 : 58 : break;
4080 : : }
4081 : 58 : return NULL;
4082 : : }
4083 : :
4084 : : /*
4085 : : * reparameterize_path_by_child
4086 : : * Given a path parameterized by the parent of the given child relation,
4087 : : * translate the path to be parameterized by the given child relation.
4088 : : *
4089 : : * Most fields in the path are not changed, but any expressions must be
4090 : : * adjusted to refer to the correct varnos, and any subpaths must be
4091 : : * recursively reparameterized. Other fields that refer to specific relids
4092 : : * also need adjustment.
4093 : : *
4094 : : * The cost, number of rows, width and parallel path properties depend upon
4095 : : * path->parent, which does not change during the translation. So we need
4096 : : * not change those.
4097 : : *
4098 : : * Currently, only a few path types are supported here, though more could be
4099 : : * added at need. We return NULL if we can't reparameterize the given path.
4100 : : *
4101 : : * Note that this function can change referenced RangeTblEntries, RelOptInfos
4102 : : * and IndexOptInfos as well as the Path structures. Therefore, it's only safe
4103 : : * to call during create_plan(), when we have made a final choice of which Path
4104 : : * to use for each RangeTblEntry/RelOptInfo/IndexOptInfo.
4105 : : *
4106 : : * Keep this code in sync with path_is_reparameterizable_by_child()!
4107 : : */
4108 : : Path *
4109 : 73621 : reparameterize_path_by_child(PlannerInfo *root, Path *path,
4110 : : RelOptInfo *child_rel)
4111 : : {
4112 : : Path *new_path;
4113 : : ParamPathInfo *new_ppi;
4114 : : ParamPathInfo *old_ppi;
4115 : : Relids required_outer;
4116 : :
4117 : : #define ADJUST_CHILD_ATTRS(node) \
4118 : : ((node) = (void *) adjust_appendrel_attrs_multilevel(root, \
4119 : : (Node *) (node), \
4120 : : child_rel, \
4121 : : child_rel->top_parent))
4122 : :
4123 : : #define REPARAMETERIZE_CHILD_PATH(path) \
4124 : : do { \
4125 : : (path) = reparameterize_path_by_child(root, (path), child_rel); \
4126 : : if ((path) == NULL) \
4127 : : return NULL; \
4128 : : } while(0)
4129 : :
4130 : : #define REPARAMETERIZE_CHILD_PATH_LIST(pathlist) \
4131 : : do { \
4132 : : if ((pathlist) != NIL) \
4133 : : { \
4134 : : (pathlist) = reparameterize_pathlist_by_child(root, (pathlist), \
4135 : : child_rel); \
4136 : : if ((pathlist) == NIL) \
4137 : : return NULL; \
4138 : : } \
4139 : : } while(0)
4140 : :
4141 : : /*
4142 : : * If the path is not parameterized by the parent of the given relation,
4143 : : * it doesn't need reparameterization.
4144 : : */
4145 [ + + ]: 73621 : if (!path->param_info ||
4146 [ + - + + ]: 37380 : !bms_overlap(PATH_REQ_OUTER(path), child_rel->top_parent_relids))
4147 : 72774 : return path;
4148 : :
4149 : : /*
4150 : : * If possible, reparameterize the given path.
4151 : : *
4152 : : * This function is currently only applied to the inner side of a nestloop
4153 : : * join that is being partitioned by the partitionwise-join code. Hence,
4154 : : * we need only support path types that plausibly arise in that context.
4155 : : * (In particular, supporting sorted path types would be a waste of code
4156 : : * and cycles: even if we translated them here, they'd just lose in
4157 : : * subsequent cost comparisons.) If we do see an unsupported path type,
4158 : : * that just means we won't be able to generate a partitionwise-join plan
4159 : : * using that path type.
4160 : : */
4161 [ + + + + : 847 : switch (nodeTag(path))
+ - - + -
+ + - + -
- ]
4162 : : {
4163 : 190 : case T_Path:
4164 : 190 : new_path = path;
4165 : 190 : ADJUST_CHILD_ATTRS(new_path->parent->baserestrictinfo);
4166 [ + + ]: 190 : if (path->pathtype == T_SampleScan)
4167 : : {
4168 : 40 : Index scan_relid = path->parent->relid;
4169 : : RangeTblEntry *rte;
4170 : :
4171 : : /* it should be a base rel with a tablesample clause... */
4172 : : Assert(scan_relid > 0);
4173 [ + - ]: 40 : rte = planner_rt_fetch(scan_relid, root);
4174 : : Assert(rte->rtekind == RTE_RELATION);
4175 : : Assert(rte->tablesample != NULL);
4176 : :
4177 : 40 : ADJUST_CHILD_ATTRS(rte->tablesample);
4178 : : }
4179 : 190 : break;
4180 : :
4181 : 447 : case T_IndexPath:
4182 : : {
4183 : 447 : IndexPath *ipath = (IndexPath *) path;
4184 : :
4185 : 447 : ADJUST_CHILD_ATTRS(ipath->indexinfo->indrestrictinfo);
4186 : 447 : ADJUST_CHILD_ATTRS(ipath->indexclauses);
4187 : 447 : new_path = (Path *) ipath;
4188 : : }
4189 : 447 : break;
4190 : :
4191 : 40 : case T_BitmapHeapPath:
4192 : : {
4193 : 40 : BitmapHeapPath *bhpath = (BitmapHeapPath *) path;
4194 : :
4195 : 40 : ADJUST_CHILD_ATTRS(bhpath->path.parent->baserestrictinfo);
4196 [ - + ]: 40 : REPARAMETERIZE_CHILD_PATH(bhpath->bitmapqual);
4197 : 40 : new_path = (Path *) bhpath;
4198 : : }
4199 : 40 : break;
4200 : :
4201 : 20 : case T_BitmapAndPath:
4202 : : {
4203 : 20 : BitmapAndPath *bapath = (BitmapAndPath *) path;
4204 : :
4205 [ + - - + ]: 20 : REPARAMETERIZE_CHILD_PATH_LIST(bapath->bitmapquals);
4206 : 20 : new_path = (Path *) bapath;
4207 : : }
4208 : 20 : break;
4209 : :
4210 : 20 : case T_BitmapOrPath:
4211 : : {
4212 : 20 : BitmapOrPath *bopath = (BitmapOrPath *) path;
4213 : :
4214 [ + - - + ]: 20 : REPARAMETERIZE_CHILD_PATH_LIST(bopath->bitmapquals);
4215 : 20 : new_path = (Path *) bopath;
4216 : : }
4217 : 20 : break;
4218 : :
4219 : 0 : case T_ForeignPath:
4220 : : {
4221 : 0 : ForeignPath *fpath = (ForeignPath *) path;
4222 : : ReparameterizeForeignPathByChild_function rfpc_func;
4223 : :
4224 : 0 : ADJUST_CHILD_ATTRS(fpath->path.parent->baserestrictinfo);
4225 [ # # ]: 0 : if (fpath->fdw_outerpath)
4226 [ # # ]: 0 : REPARAMETERIZE_CHILD_PATH(fpath->fdw_outerpath);
4227 [ # # ]: 0 : if (fpath->fdw_restrictinfo)
4228 : 0 : ADJUST_CHILD_ATTRS(fpath->fdw_restrictinfo);
4229 : :
4230 : : /* Hand over to FDW if needed. */
4231 : 0 : rfpc_func =
4232 : 0 : path->parent->fdwroutine->ReparameterizeForeignPathByChild;
4233 [ # # ]: 0 : if (rfpc_func)
4234 : 0 : fpath->fdw_private = rfpc_func(root, fpath->fdw_private,
4235 : : child_rel);
4236 : 0 : new_path = (Path *) fpath;
4237 : : }
4238 : 0 : break;
4239 : :
4240 : 0 : case T_CustomPath:
4241 : : {
4242 : 0 : CustomPath *cpath = (CustomPath *) path;
4243 : :
4244 : 0 : ADJUST_CHILD_ATTRS(cpath->path.parent->baserestrictinfo);
4245 [ # # # # ]: 0 : REPARAMETERIZE_CHILD_PATH_LIST(cpath->custom_paths);
4246 [ # # ]: 0 : if (cpath->custom_restrictinfo)
4247 : 0 : ADJUST_CHILD_ATTRS(cpath->custom_restrictinfo);
4248 [ # # ]: 0 : if (cpath->methods &&
4249 [ # # ]: 0 : cpath->methods->ReparameterizeCustomPathByChild)
4250 : 0 : cpath->custom_private =
4251 : 0 : cpath->methods->ReparameterizeCustomPathByChild(root,
4252 : : cpath->custom_private,
4253 : : child_rel);
4254 : 0 : new_path = (Path *) cpath;
4255 : : }
4256 : 0 : break;
4257 : :
4258 : 30 : case T_NestPath:
4259 : : {
4260 : 30 : NestPath *npath = (NestPath *) path;
4261 : 30 : JoinPath *jpath = (JoinPath *) npath;
4262 : :
4263 [ - + ]: 30 : REPARAMETERIZE_CHILD_PATH(jpath->outerjoinpath);
4264 [ - + ]: 30 : REPARAMETERIZE_CHILD_PATH(jpath->innerjoinpath);
4265 : 30 : ADJUST_CHILD_ATTRS(jpath->joinrestrictinfo);
4266 : 30 : new_path = (Path *) npath;
4267 : : }
4268 : 30 : break;
4269 : :
4270 : 0 : case T_MergePath:
4271 : : {
4272 : 0 : MergePath *mpath = (MergePath *) path;
4273 : 0 : JoinPath *jpath = (JoinPath *) mpath;
4274 : :
4275 [ # # ]: 0 : REPARAMETERIZE_CHILD_PATH(jpath->outerjoinpath);
4276 [ # # ]: 0 : REPARAMETERIZE_CHILD_PATH(jpath->innerjoinpath);
4277 : 0 : ADJUST_CHILD_ATTRS(jpath->joinrestrictinfo);
4278 : 0 : ADJUST_CHILD_ATTRS(mpath->path_mergeclauses);
4279 : 0 : new_path = (Path *) mpath;
4280 : : }
4281 : 0 : break;
4282 : :
4283 : 40 : case T_HashPath:
4284 : : {
4285 : 40 : HashPath *hpath = (HashPath *) path;
4286 : 40 : JoinPath *jpath = (JoinPath *) hpath;
4287 : :
4288 [ - + ]: 40 : REPARAMETERIZE_CHILD_PATH(jpath->outerjoinpath);
4289 [ - + ]: 40 : REPARAMETERIZE_CHILD_PATH(jpath->innerjoinpath);
4290 : 40 : ADJUST_CHILD_ATTRS(jpath->joinrestrictinfo);
4291 : 40 : ADJUST_CHILD_ATTRS(hpath->path_hashclauses);
4292 : 40 : new_path = (Path *) hpath;
4293 : : }
4294 : 40 : break;
4295 : :
4296 : 20 : case T_AppendPath:
4297 : : {
4298 : 20 : AppendPath *apath = (AppendPath *) path;
4299 : :
4300 [ + - - + ]: 20 : REPARAMETERIZE_CHILD_PATH_LIST(apath->subpaths);
4301 : 20 : new_path = (Path *) apath;
4302 : : }
4303 : 20 : break;
4304 : :
4305 : 0 : case T_MaterialPath:
4306 : : {
4307 : 0 : MaterialPath *mpath = (MaterialPath *) path;
4308 : :
4309 [ # # ]: 0 : REPARAMETERIZE_CHILD_PATH(mpath->subpath);
4310 : 0 : new_path = (Path *) mpath;
4311 : : }
4312 : 0 : break;
4313 : :
4314 : 40 : case T_MemoizePath:
4315 : : {
4316 : 40 : MemoizePath *mpath = (MemoizePath *) path;
4317 : :
4318 [ - + ]: 40 : REPARAMETERIZE_CHILD_PATH(mpath->subpath);
4319 : 40 : ADJUST_CHILD_ATTRS(mpath->param_exprs);
4320 : 40 : new_path = (Path *) mpath;
4321 : : }
4322 : 40 : break;
4323 : :
4324 : 0 : case T_GatherPath:
4325 : : {
4326 : 0 : GatherPath *gpath = (GatherPath *) path;
4327 : :
4328 [ # # ]: 0 : REPARAMETERIZE_CHILD_PATH(gpath->subpath);
4329 : 0 : new_path = (Path *) gpath;
4330 : : }
4331 : 0 : break;
4332 : :
4333 : 0 : default:
4334 : : /* We don't know how to reparameterize this path. */
4335 : 0 : return NULL;
4336 : : }
4337 : :
4338 : : /*
4339 : : * Adjust the parameterization information, which refers to the topmost
4340 : : * parent. The topmost parent can be multiple levels away from the given
4341 : : * child, hence use multi-level expression adjustment routines.
4342 : : */
4343 : 847 : old_ppi = new_path->param_info;
4344 : : required_outer =
4345 : 847 : adjust_child_relids_multilevel(root, old_ppi->ppi_req_outer,
4346 : : child_rel,
4347 : 847 : child_rel->top_parent);
4348 : :
4349 : : /* If we already have a PPI for this parameterization, just return it */
4350 : 847 : new_ppi = find_param_path_info(new_path->parent, required_outer);
4351 : :
4352 : : /*
4353 : : * If not, build a new one and link it to the list of PPIs. For the same
4354 : : * reason as explained in mark_dummy_rel(), allocate new PPI in the same
4355 : : * context the given RelOptInfo is in.
4356 : : */
4357 [ + + ]: 847 : if (new_ppi == NULL)
4358 : : {
4359 : : MemoryContext oldcontext;
4360 : 727 : RelOptInfo *rel = path->parent;
4361 : :
4362 : 727 : oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
4363 : :
4364 : 727 : new_ppi = makeNode(ParamPathInfo);
4365 : 727 : new_ppi->ppi_req_outer = bms_copy(required_outer);
4366 : 727 : new_ppi->ppi_rows = old_ppi->ppi_rows;
4367 : 727 : new_ppi->ppi_clauses = old_ppi->ppi_clauses;
4368 : 727 : ADJUST_CHILD_ATTRS(new_ppi->ppi_clauses);
4369 : 727 : new_ppi->ppi_serials = bms_copy(old_ppi->ppi_serials);
4370 : 727 : rel->ppilist = lappend(rel->ppilist, new_ppi);
4371 : :
4372 : 727 : MemoryContextSwitchTo(oldcontext);
4373 : : }
4374 : 847 : bms_free(required_outer);
4375 : :
4376 : 847 : new_path->param_info = new_ppi;
4377 : :
4378 : : /*
4379 : : * Adjust the path target if the parent of the outer relation is
4380 : : * referenced in the targetlist. This can happen when only the parent of
4381 : : * outer relation is laterally referenced in this relation.
4382 : : */
4383 [ + + ]: 847 : if (bms_overlap(path->parent->lateral_relids,
4384 : 847 : child_rel->top_parent_relids))
4385 : : {
4386 : 400 : new_path->pathtarget = copy_pathtarget(new_path->pathtarget);
4387 : 400 : ADJUST_CHILD_ATTRS(new_path->pathtarget->exprs);
4388 : : }
4389 : :
4390 : 847 : return new_path;
4391 : : }
4392 : :
4393 : : /*
4394 : : * path_is_reparameterizable_by_child
4395 : : * Given a path parameterized by the parent of the given child relation,
4396 : : * see if it can be translated to be parameterized by the child relation.
4397 : : *
4398 : : * This must return true if and only if reparameterize_path_by_child()
4399 : : * would succeed on this path. Currently it's sufficient to verify that
4400 : : * the path and all of its subpaths (if any) are of the types handled by
4401 : : * that function. However, subpaths that are not parameterized can be
4402 : : * disregarded since they won't require translation.
4403 : : */
4404 : : bool
4405 : 27929 : path_is_reparameterizable_by_child(Path *path, RelOptInfo *child_rel)
4406 : : {
4407 : : #define REJECT_IF_PATH_NOT_REPARAMETERIZABLE(path) \
4408 : : do { \
4409 : : if (!path_is_reparameterizable_by_child(path, child_rel)) \
4410 : : return false; \
4411 : : } while(0)
4412 : :
4413 : : #define REJECT_IF_PATH_LIST_NOT_REPARAMETERIZABLE(pathlist) \
4414 : : do { \
4415 : : if (!pathlist_is_reparameterizable_by_child(pathlist, child_rel)) \
4416 : : return false; \
4417 : : } while(0)
4418 : :
4419 : : /*
4420 : : * If the path is not parameterized by the parent of the given relation,
4421 : : * it doesn't need reparameterization.
4422 : : */
4423 [ + + ]: 27929 : if (!path->param_info ||
4424 [ + - + + ]: 27553 : !bms_overlap(PATH_REQ_OUTER(path), child_rel->top_parent_relids))
4425 : 784 : return true;
4426 : :
4427 : : /*
4428 : : * Check that the path type is one that reparameterize_path_by_child() can
4429 : : * handle, and recursively check subpaths.
4430 : : */
4431 [ + + + + : 27145 : switch (nodeTag(path))
+ - + + -
+ - - ]
4432 : : {
4433 : 18911 : case T_Path:
4434 : : case T_IndexPath:
4435 : 18911 : break;
4436 : :
4437 : 40 : case T_BitmapHeapPath:
4438 : : {
4439 : 40 : BitmapHeapPath *bhpath = (BitmapHeapPath *) path;
4440 : :
4441 [ - + ]: 40 : REJECT_IF_PATH_NOT_REPARAMETERIZABLE(bhpath->bitmapqual);
4442 : : }
4443 : 40 : break;
4444 : :
4445 : 20 : case T_BitmapAndPath:
4446 : : {
4447 : 20 : BitmapAndPath *bapath = (BitmapAndPath *) path;
4448 : :
4449 [ - + ]: 20 : REJECT_IF_PATH_LIST_NOT_REPARAMETERIZABLE(bapath->bitmapquals);
4450 : : }
4451 : 20 : break;
4452 : :
4453 : 20 : case T_BitmapOrPath:
4454 : : {
4455 : 20 : BitmapOrPath *bopath = (BitmapOrPath *) path;
4456 : :
4457 [ - + ]: 20 : REJECT_IF_PATH_LIST_NOT_REPARAMETERIZABLE(bopath->bitmapquals);
4458 : : }
4459 : 20 : break;
4460 : :
4461 : 74 : case T_ForeignPath:
4462 : : {
4463 : 74 : ForeignPath *fpath = (ForeignPath *) path;
4464 : :
4465 [ - + ]: 74 : if (fpath->fdw_outerpath)
4466 [ # # ]: 0 : REJECT_IF_PATH_NOT_REPARAMETERIZABLE(fpath->fdw_outerpath);
4467 : : }
4468 : 74 : break;
4469 : :
4470 : 0 : case T_CustomPath:
4471 : : {
4472 : 0 : CustomPath *cpath = (CustomPath *) path;
4473 : :
4474 [ # # ]: 0 : REJECT_IF_PATH_LIST_NOT_REPARAMETERIZABLE(cpath->custom_paths);
4475 : : }
4476 : 0 : break;
4477 : :
4478 : 1004 : case T_NestPath:
4479 : : case T_MergePath:
4480 : : case T_HashPath:
4481 : : {
4482 : 1004 : JoinPath *jpath = (JoinPath *) path;
4483 : :
4484 [ - + ]: 1004 : REJECT_IF_PATH_NOT_REPARAMETERIZABLE(jpath->outerjoinpath);
4485 [ - + ]: 1004 : REJECT_IF_PATH_NOT_REPARAMETERIZABLE(jpath->innerjoinpath);
4486 : : }
4487 : 1004 : break;
4488 : :
4489 : 160 : case T_AppendPath:
4490 : : {
4491 : 160 : AppendPath *apath = (AppendPath *) path;
4492 : :
4493 [ - + ]: 160 : REJECT_IF_PATH_LIST_NOT_REPARAMETERIZABLE(apath->subpaths);
4494 : : }
4495 : 160 : break;
4496 : :
4497 : 0 : case T_MaterialPath:
4498 : : {
4499 : 0 : MaterialPath *mpath = (MaterialPath *) path;
4500 : :
4501 [ # # ]: 0 : REJECT_IF_PATH_NOT_REPARAMETERIZABLE(mpath->subpath);
4502 : : }
4503 : 0 : break;
4504 : :
4505 : 6916 : case T_MemoizePath:
4506 : : {
4507 : 6916 : MemoizePath *mpath = (MemoizePath *) path;
4508 : :
4509 [ - + ]: 6916 : REJECT_IF_PATH_NOT_REPARAMETERIZABLE(mpath->subpath);
4510 : : }
4511 : 6916 : break;
4512 : :
4513 : 0 : case T_GatherPath:
4514 : : {
4515 : 0 : GatherPath *gpath = (GatherPath *) path;
4516 : :
4517 [ # # ]: 0 : REJECT_IF_PATH_NOT_REPARAMETERIZABLE(gpath->subpath);
4518 : : }
4519 : 0 : break;
4520 : :
4521 : 0 : default:
4522 : : /* We don't know how to reparameterize this path. */
4523 : 0 : return false;
4524 : : }
4525 : :
4526 : 27145 : return true;
4527 : : }
4528 : :
4529 : : /*
4530 : : * reparameterize_pathlist_by_child
4531 : : * Helper function to reparameterize a list of paths by given child rel.
4532 : : *
4533 : : * Returns NIL to indicate failure, so pathlist had better not be NIL.
4534 : : */
4535 : : static List *
4536 : 60 : reparameterize_pathlist_by_child(PlannerInfo *root,
4537 : : List *pathlist,
4538 : : RelOptInfo *child_rel)
4539 : : {
4540 : : ListCell *lc;
4541 : 60 : List *result = NIL;
4542 : :
4543 [ + - + + : 180 : foreach(lc, pathlist)
+ + ]
4544 : : {
4545 : 120 : Path *path = reparameterize_path_by_child(root, lfirst(lc),
4546 : : child_rel);
4547 : :
4548 [ - + ]: 120 : if (path == NULL)
4549 : : {
4550 : 0 : list_free(result);
4551 : 0 : return NIL;
4552 : : }
4553 : :
4554 : 120 : result = lappend(result, path);
4555 : : }
4556 : :
4557 : 60 : return result;
4558 : : }
4559 : :
4560 : : /*
4561 : : * pathlist_is_reparameterizable_by_child
4562 : : * Helper function to check if a list of paths can be reparameterized.
4563 : : */
4564 : : static bool
4565 : 200 : pathlist_is_reparameterizable_by_child(List *pathlist, RelOptInfo *child_rel)
4566 : : {
4567 : : ListCell *lc;
4568 : :
4569 [ + - + + : 600 : foreach(lc, pathlist)
+ + ]
4570 : : {
4571 : 400 : Path *path = (Path *) lfirst(lc);
4572 : :
4573 [ - + ]: 400 : if (!path_is_reparameterizable_by_child(path, child_rel))
4574 : 0 : return false;
4575 : : }
4576 : :
4577 : 200 : return true;
4578 : : }
|