Age Owner Branch data TLA 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
9714 tgl@sss.pgh.pa.us 68 :CBC 901512 : compare_path_costs(Path *path1, Path *path2, CostSelector criterion)
69 : : {
70 : : /* Number of disabled nodes, if different, trumps all else. */
760 rhaas@postgresql.org 71 [ + + ]: 901512 : if (unlikely(path1->disabled_nodes != path2->disabled_nodes))
72 : : {
73 [ + + ]: 79382 : if (path1->disabled_nodes < path2->disabled_nodes)
74 : 79372 : return -1;
75 : : else
76 : 10 : return +1;
77 : : }
78 : :
9714 tgl@sss.pgh.pa.us 79 [ + + ]: 822130 : if (criterion == STARTUP_COST)
80 : : {
81 [ + + ]: 418356 : if (path1->startup_cost < path2->startup_cost)
82 : 256163 : return -1;
83 [ + + ]: 162193 : if (path1->startup_cost > path2->startup_cost)
84 : 79572 : return +1;
85 : :
86 : : /*
87 : : * If paths have the same startup cost (not at all unlikely), order
88 : : * them by total cost.
89 : : */
90 [ + + ]: 82621 : if (path1->total_cost < path2->total_cost)
91 : 40239 : return -1;
92 [ + + ]: 42382 : if (path1->total_cost > path2->total_cost)
93 : 4364 : return +1;
94 : : }
95 : : else
96 : : {
97 [ + + ]: 403774 : if (path1->total_cost < path2->total_cost)
98 : 378197 : return -1;
99 [ + + ]: 25577 : if (path1->total_cost > path2->total_cost)
100 : 7031 : return +1;
101 : :
102 : : /*
103 : : * If paths have the same total cost, order them by startup cost.
104 : : */
105 [ + + ]: 18546 : if (path1->startup_cost < path2->startup_cost)
106 : 1481 : return -1;
107 [ + + ]: 17065 : if (path1->startup_cost > path2->startup_cost)
108 : 3 : return +1;
109 : : }
110 : 55080 : 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 : 3840 : 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. */
760 rhaas@postgresql.org 130 [ + + ]: 3840 : if (unlikely(path1->disabled_nodes != path2->disabled_nodes))
131 : : {
132 [ + - ]: 42 : if (path1->disabled_nodes < path2->disabled_nodes)
133 : 42 : return -1;
134 : : else
760 rhaas@postgresql.org 135 :UBC 0 : return +1;
136 : : }
137 : :
9714 tgl@sss.pgh.pa.us 138 [ + - + + ]:CBC 3798 : if (fraction <= 0.0 || fraction >= 1.0)
139 : 1060 : return compare_path_costs(path1, path2, TOTAL_COST);
140 : 2738 : cost1 = path1->startup_cost +
141 : 2738 : fraction * (path1->total_cost - path1->startup_cost);
142 : 2738 : cost2 = path2->startup_cost +
143 : 2738 : fraction * (path2->total_cost - path2->startup_cost);
144 [ + + ]: 2738 : if (cost1 < cost2)
145 : 2316 : return -1;
146 [ + - ]: 422 : if (cost1 > cost2)
147 : 422 : return +1;
9714 tgl@sss.pgh.pa.us 148 :UBC 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
4127 tgl@sss.pgh.pa.us 181 :CBC 3756580 : 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. */
760 rhaas@postgresql.org 187 [ + + ]: 3756580 : if (unlikely(path1->disabled_nodes != path2->disabled_nodes))
188 : : {
189 [ + + ]: 183856 : if (path1->disabled_nodes < path2->disabled_nodes)
190 : 64915 : return COSTS_BETTER1;
191 : : else
192 : 118941 : 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 : : */
5265 tgl@sss.pgh.pa.us 199 [ + + ]: 3572724 : if (path1->total_cost > path2->total_cost * fuzz_factor)
200 : : {
201 : : /* path1 fuzzily worse on total cost */
4127 202 [ + + + + ]: 1850196 : if (CONSIDER_PATH_STARTUP_COST(path1) &&
203 [ + + ]: 90365 : path2->startup_cost > path1->startup_cost * fuzz_factor)
204 : : {
205 : : /* ... but path2 fuzzily worse on startup, so DIFFERENT */
5350 206 : 47271 : return COSTS_DIFFERENT;
207 : : }
208 : : /* else path2 dominates */
209 : 1802925 : return COSTS_BETTER2;
210 : : }
5265 211 [ + + ]: 1722528 : if (path2->total_cost > path1->total_cost * fuzz_factor)
212 : : {
213 : : /* path2 fuzzily worse on total cost */
4127 214 [ + + + + ]: 853557 : if (CONSIDER_PATH_STARTUP_COST(path2) &&
215 [ + + ]: 33762 : path1->startup_cost > path2->startup_cost * fuzz_factor)
216 : : {
217 : : /* ... but path1 fuzzily worse on startup, so DIFFERENT */
5350 218 : 20194 : return COSTS_DIFFERENT;
219 : : }
220 : : /* else path1 dominates */
221 : 833363 : return COSTS_BETTER1;
222 : : }
223 : : /* fuzzily the same on total cost ... */
4127 224 [ + + ]: 868971 : if (path1->startup_cost > path2->startup_cost * fuzz_factor)
225 : : {
226 : : /* ... but path1 fuzzily worse on startup, so path2 wins */
5350 227 : 284904 : return COSTS_BETTER2;
228 : : }
4127 229 [ + + ]: 584067 : if (path2->startup_cost > path1->startup_cost * fuzz_factor)
230 : : {
231 : : /* ... but path2 fuzzily worse on startup, so path1 wins */
5350 232 : 47654 : return COSTS_BETTER1;
233 : : }
234 : : /* fuzzily the same on both costs */
235 : 536413 : 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
9714 268 : 1582039 : 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 : :
10291 bruce@momjian.us 276 [ - + ]: 1582039 : Assert(IsA(parent_rel, RelOptInfo));
277 : :
5135 tgl@sss.pgh.pa.us 278 [ - + ]: 1582039 : if (parent_rel->pathlist == NIL)
5135 tgl@sss.pgh.pa.us 279 [ # # ]:UBC 0 : elog(ERROR, "could not devise a query plan for the given query");
280 : :
5135 tgl@sss.pgh.pa.us 281 :CBC 1582039 : cheapest_startup_path = cheapest_total_path = best_param_path = NULL;
282 : 1582039 : parameterized_paths = NIL;
283 : :
5350 284 [ + - + + : 3630655 : foreach(p, parent_rel->pathlist)
+ + ]
285 : : {
10604 bruce@momjian.us 286 : 2048616 : Path *path = (Path *) lfirst(p);
287 : : int cmp;
288 : :
5267 tgl@sss.pgh.pa.us 289 [ + + ]: 2048616 : if (path->param_info)
290 : : {
291 : : /* Parameterized path, so add it to parameterized_paths */
5135 292 : 109771 : 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 [ + + ]: 109771 : if (cheapest_total_path)
299 : 27980 : 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 [ + + ]: 81791 : if (best_param_path == NULL)
307 : 72819 : best_param_path = path;
308 : : else
309 : : {
310 [ + - + + : 8972 : switch (bms_subset_compare(PATH_REQ_OUTER(path),
+ + - ]
311 [ + - ]: 8972 : PATH_REQ_OUTER(best_param_path)))
312 : : {
313 : 53 : case BMS_EQUAL:
314 : : /* keep the cheaper one */
315 [ - + ]: 53 : if (compare_path_costs(path, best_param_path,
316 : : TOTAL_COST) < 0)
5135 tgl@sss.pgh.pa.us 317 :UBC 0 : best_param_path = path;
5135 tgl@sss.pgh.pa.us 318 :CBC 53 : break;
319 : 923 : case BMS_SUBSET1:
320 : : /* new path is less-parameterized */
321 : 923 : best_param_path = path;
322 : 923 : break;
323 : 11 : case BMS_SUBSET2:
324 : : /* old path is less-parameterized, keep it */
325 : 11 : break;
326 : 7985 : 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 : 7985 : break;
334 : : }
335 : : }
336 : : }
337 : : else
338 : : {
339 : : /* Unparameterized path, so consider it for cheapest slots */
340 [ + + ]: 1938845 : if (cheapest_total_path == NULL)
341 : : {
342 : 1573289 : cheapest_startup_path = cheapest_total_path = path;
343 : 1573289 : 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 : 365556 : cmp = compare_path_costs(cheapest_startup_path, path, STARTUP_COST);
354 [ + + + + ]: 365556 : if (cmp > 0 ||
355 [ - + ]: 161 : (cmp == 0 &&
356 : 161 : compare_pathkeys(cheapest_startup_path->pathkeys,
357 : : path->pathkeys) == PATHKEYS_BETTER2))
358 : 57873 : cheapest_startup_path = path;
359 : :
360 : 365556 : cmp = compare_path_costs(cheapest_total_path, path, TOTAL_COST);
361 [ + - + + ]: 365556 : if (cmp > 0 ||
362 [ - + ]: 2 : (cmp == 0 &&
363 : 2 : compare_pathkeys(cheapest_total_path->pathkeys,
364 : : path->pathkeys) == PATHKEYS_BETTER2))
5135 tgl@sss.pgh.pa.us 365 :UBC 0 : cheapest_total_path = path;
366 : : }
367 : : }
368 : :
369 : : /* Add cheapest unparameterized path, if any, to parameterized_paths */
5135 tgl@sss.pgh.pa.us 370 [ + + ]:CBC 1582039 : if (cheapest_total_path)
371 : 1573289 : 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 [ + + ]: 1582039 : if (cheapest_total_path == NULL)
378 : 8750 : cheapest_total_path = best_param_path;
379 [ - + ]: 1582039 : Assert(cheapest_total_path != NULL);
380 : :
9714 381 : 1582039 : parent_rel->cheapest_startup_path = cheapest_startup_path;
382 : 1582039 : parent_rel->cheapest_total_path = cheapest_total_path;
5135 383 : 1582039 : parent_rel->cheapest_parameterized_paths = parameterized_paths;
9722 384 : 1582039 : }
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 : 3542696 : add_path(RelOptInfo *parent_rel, Path *new_path)
460 : : {
3378 461 : 3542696 : bool accept_new = true; /* unless we find a superior old path */
2624 462 : 3542696 : 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 : : */
7779 470 [ + + ]: 3542696 : CHECK_FOR_INTERRUPTS();
471 : :
472 : : /* Pretend parameterized paths have no pathkeys, per comment above */
5267 473 [ + + ]: 3542696 : 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 : : */
2624 480 [ + + + + : 5450995 : foreach(p1, parent_rel->pathlist)
+ + ]
481 : : {
9722 482 : 3249568 : Path *old_path = (Path *) lfirst(p1);
9657 bruce@momjian.us 483 : 3249568 : 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 : : */
4127 tgl@sss.pgh.pa.us 491 : 3249568 : 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 : : */
5350 505 [ + + ]: 3249568 : if (costcmp != COSTS_DIFFERENT)
506 : : {
507 : : /* Similarly check to see if either dominates on pathkeys */
508 : : List *old_path_pathkeys;
509 : :
5267 510 [ + + ]: 3182559 : old_path_pathkeys = old_path->param_info ? NIL : old_path->pathkeys;
5350 511 : 3182559 : keyscmp = compare_pathkeys(new_path_pathkeys,
512 : : old_path_pathkeys);
513 [ + + ]: 3182559 : if (keyscmp != PATHKEYS_DIFFERENT)
514 : : {
515 [ + + + - : 3019743 : switch (costcmp)
- ]
516 : : {
517 : 322611 : case COSTS_EQUAL:
5267 518 [ + + ]: 322611 : outercmp = bms_subset_compare(PATH_REQ_OUTER(new_path),
3378 519 [ + + ]: 322611 : PATH_REQ_OUTER(old_path));
5350 520 [ + + ]: 322611 : if (keyscmp == PATHKEYS_BETTER1)
521 : : {
5267 522 [ + + + - ]: 6460 : if ((outercmp == BMS_EQUAL ||
523 : 6460 : outercmp == BMS_SUBSET1) &&
3896 rhaas@postgresql.org 524 [ + + ]: 6460 : new_path->rows <= old_path->rows &&
525 [ + - ]: 6415 : new_path->parallel_safe >= old_path->parallel_safe)
3378 tgl@sss.pgh.pa.us 526 : 6415 : remove_old = true; /* new dominates old */
527 : : }
5350 528 [ + + ]: 316151 : else if (keyscmp == PATHKEYS_BETTER2)
529 : : {
5267 530 [ + + + - ]: 22589 : if ((outercmp == BMS_EQUAL ||
531 : 22589 : outercmp == BMS_SUBSET2) &&
3896 rhaas@postgresql.org 532 [ + + ]: 22589 : new_path->rows >= old_path->rows &&
533 [ + - ]: 15175 : new_path->parallel_safe <= old_path->parallel_safe)
3378 tgl@sss.pgh.pa.us 534 : 15175 : accept_new = false; /* old dominates new */
535 : : }
536 : : else /* keyscmp == PATHKEYS_EQUAL */
537 : : {
5350 538 [ + + ]: 293562 : 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 : : */
3896 rhaas@postgresql.org 555 : 287440 : if (new_path->parallel_safe >
556 [ + + ]: 287440 : old_path->parallel_safe)
557 : 28 : remove_old = true; /* new dominates old */
558 : 287412 : else if (new_path->parallel_safe <
559 [ + + ]: 287412 : old_path->parallel_safe)
560 : 36 : accept_new = false; /* old dominates new */
561 [ + + ]: 287376 : else if (new_path->rows < old_path->rows)
5267 tgl@sss.pgh.pa.us 562 : 24 : remove_old = true; /* new dominates old */
563 [ + + ]: 287352 : else if (new_path->rows > old_path->rows)
5215 bruce@momjian.us 564 : 110 : accept_new = false; /* old dominates new */
5132 tgl@sss.pgh.pa.us 565 [ + + ]: 287242 : else if (compare_path_costs_fuzzily(new_path,
566 : : old_path,
567 : : 1.0000000001) == COSTS_BETTER1)
5350 568 : 14007 : remove_old = true; /* new dominates old */
569 : : else
5215 bruce@momjian.us 570 : 273235 : accept_new = false; /* old equals or
571 : : * dominates new */
572 : : }
5267 tgl@sss.pgh.pa.us 573 [ + + ]: 6122 : else if (outercmp == BMS_SUBSET1 &&
3896 rhaas@postgresql.org 574 [ + + ]: 623 : new_path->rows <= old_path->rows &&
575 [ + - ]: 609 : new_path->parallel_safe >= old_path->parallel_safe)
3378 tgl@sss.pgh.pa.us 576 : 609 : remove_old = true; /* new dominates old */
5267 577 [ + + ]: 5513 : else if (outercmp == BMS_SUBSET2 &&
3896 rhaas@postgresql.org 578 [ + + ]: 4933 : new_path->rows >= old_path->rows &&
579 [ + - ]: 3893 : new_path->parallel_safe <= old_path->parallel_safe)
3378 tgl@sss.pgh.pa.us 580 : 3893 : accept_new = false; /* old dominates new */
581 : : /* else different parameterizations, keep both */
582 : : }
5350 583 : 322611 : break;
584 : 843225 : case COSTS_BETTER1:
585 [ + + ]: 843225 : if (keyscmp != PATHKEYS_BETTER2)
586 : : {
5267 587 [ + + ]: 566929 : outercmp = bms_subset_compare(PATH_REQ_OUTER(new_path),
3378 588 [ + + ]: 566929 : PATH_REQ_OUTER(old_path));
5267 589 [ + + + + ]: 566929 : if ((outercmp == BMS_EQUAL ||
590 : 489164 : outercmp == BMS_SUBSET1) &&
3896 rhaas@postgresql.org 591 [ + + ]: 489164 : new_path->rows <= old_path->rows &&
592 [ + + ]: 483957 : new_path->parallel_safe >= old_path->parallel_safe)
3378 tgl@sss.pgh.pa.us 593 : 481324 : remove_old = true; /* new dominates old */
594 : : }
5350 595 : 843225 : break;
596 : 1853907 : case COSTS_BETTER2:
597 [ + + ]: 1853907 : if (keyscmp != PATHKEYS_BETTER1)
598 : : {
5267 599 [ + + ]: 1190750 : outercmp = bms_subset_compare(PATH_REQ_OUTER(new_path),
3378 600 [ + + ]: 1190750 : PATH_REQ_OUTER(old_path));
5267 601 [ + + + + ]: 1190750 : if ((outercmp == BMS_EQUAL ||
602 : 1123637 : outercmp == BMS_SUBSET2) &&
3896 rhaas@postgresql.org 603 [ + + ]: 1123637 : new_path->rows >= old_path->rows &&
604 [ + + ]: 1050478 : new_path->parallel_safe <= old_path->parallel_safe)
3378 tgl@sss.pgh.pa.us 605 : 1048820 : accept_new = false; /* old dominates new */
606 : : }
5350 607 : 1853907 : break;
5350 tgl@sss.pgh.pa.us 608 :UBC 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 : : */
8183 neilc@samurai.com 622 [ + + ]:CBC 3249568 : if (remove_old)
623 : : {
2624 tgl@sss.pgh.pa.us 624 : 502407 : 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 : : */
7821 630 [ + + ]: 502407 : if (!IsA(old_path, IndexPath))
631 : 483073 : 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 : : */
760 rhaas@postgresql.org 639 [ + + ]: 2747161 : if (new_path->disabled_nodes > old_path->disabled_nodes ||
640 [ + + ]: 2631033 : (new_path->disabled_nodes == old_path->disabled_nodes &&
641 [ + + ]: 2617903 : new_path->total_cost >= old_path->total_cost))
2624 tgl@sss.pgh.pa.us 642 : 2302108 : 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 : : */
9657 bruce@momjian.us 650 [ + + ]: 3249568 : if (!accept_new)
9722 tgl@sss.pgh.pa.us 651 : 1341269 : break;
652 : : }
653 : :
654 [ + + ]: 3542696 : if (accept_new)
655 : : {
656 : : /* Accept the new path: insert it at proper place in pathlist */
2624 657 : 2201427 : parent_rel->pathlist =
658 : 2201427 : list_insert_nth(parent_rel->pathlist, insert_at, new_path);
659 : : }
660 : : else
661 : : {
662 : : /* Reject and recycle the new path */
7821 663 [ + + ]: 1341269 : if (!IsA(new_path, IndexPath))
664 : 1247814 : pfree(new_path);
665 : : }
11030 scrappy@hub.org 666 : 3542696 : }
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
760 rhaas@postgresql.org 686 : 3807132 : 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 */
5350 tgl@sss.pgh.pa.us 695 [ + + ]: 3807132 : new_path_pathkeys = required_outer ? NIL : pathkeys;
696 : :
697 : : /* Decide whether new path's startup cost is interesting */
4127 698 [ + + ]: 3807132 : consider_startup = required_outer ? parent_rel->consider_param_startup : parent_rel->consider_startup;
699 : :
5350 700 [ + + + + : 4863290 : foreach(p1, parent_rel->pathlist)
+ + ]
701 : : {
702 : 4575566 : 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 : : */
760 rhaas@postgresql.org 711 [ + + ]: 4575566 : if (unlikely(old_path->disabled_nodes != disabled_nodes))
712 : : {
713 [ + + ]: 126042 : if (disabled_nodes < old_path->disabled_nodes)
714 : 14354 : break;
715 : : }
716 [ + + ]: 4449524 : else if (total_cost <= old_path->total_cost * STD_FUZZ_FACTOR)
717 : 1414737 : 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 [ + + ]: 3146475 : if (startup_cost > old_path->startup_cost * STD_FUZZ_FACTOR ||
729 [ + + ]: 1478453 : !consider_startup)
730 : : {
731 : : /* new path loses on cost, so check pathkeys... */
732 : : List *old_path_pathkeys;
733 : :
734 [ + + ]: 3095903 : old_path_pathkeys = old_path->param_info ? NIL : old_path->pathkeys;
735 : 3095903 : keyscmp = compare_pathkeys(new_path_pathkeys,
736 : : old_path_pathkeys);
737 [ + + + + ]: 3095903 : if (keyscmp == PATHKEYS_EQUAL ||
738 : : keyscmp == PATHKEYS_BETTER2)
739 : : {
740 : : /* new path does not win on pathkeys... */
741 [ + + + + ]: 2137995 : if (bms_equal(required_outer, PATH_REQ_OUTER(old_path)))
742 : : {
743 : : /* Found an old path that dominates the new one */
744 : 2090317 : return false;
745 : : }
746 : : }
747 : : }
748 : : }
749 : :
5350 tgl@sss.pgh.pa.us 750 : 1716815 : 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
3896 rhaas@postgresql.org 793 : 241011 : add_partial_path(RelOptInfo *parent_rel, Path *new_path)
794 : : {
3378 tgl@sss.pgh.pa.us 795 : 241011 : bool accept_new = true; /* unless we find a superior old path */
2624 796 : 241011 : int insert_at = 0; /* where to insert new item */
797 : : ListCell *p1;
798 : :
799 : : /* Check for query cancel. */
3896 rhaas@postgresql.org 800 [ - + ]: 241011 : CHECK_FOR_INTERRUPTS();
801 : :
802 : : /* Path to be added must be parallel safe. */
3070 803 [ - + ]: 241011 : Assert(new_path->parallel_safe);
804 : :
805 : : /* Relation should be OK for parallelism, too. */
806 [ - + ]: 241011 : 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 : : */
2624 tgl@sss.pgh.pa.us 812 [ + + + + : 338429 : foreach(p1, parent_rel->partial_pathlist)
+ + ]
813 : : {
3896 rhaas@postgresql.org 814 : 193369 : Path *old_path = (Path *) lfirst(p1);
815 : 193369 : bool remove_old = false; /* unless new proves superior */
816 : : PathKeysComparison keyscmp;
817 : :
818 : : /* Compare pathkeys. */
819 : 193369 : 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 [ + + ]: 193369 : if (keyscmp != PATHKEYS_DIFFERENT)
827 : : {
828 : : PathCostComparison costcmp;
829 : :
830 : : /*
831 : : * Do a fuzzy cost comparison with standard fuzziness limit.
832 : : */
195 833 : 193202 : costcmp = compare_path_costs_fuzzily(new_path, old_path,
834 : : STD_FUZZ_FACTOR);
835 [ + + ]: 193202 : if (costcmp == COSTS_BETTER1)
836 : : {
837 [ + + ]: 69101 : if (keyscmp != PATHKEYS_BETTER2)
760 838 : 27658 : remove_old = true;
839 : : }
195 840 [ + + ]: 124101 : else if (costcmp == COSTS_BETTER2)
841 : : {
3896 842 [ + + ]: 95993 : if (keyscmp != PATHKEYS_BETTER1)
843 : 68442 : accept_new = false;
844 : : }
195 845 [ + + ]: 28108 : else if (costcmp == COSTS_EQUAL)
846 : : {
847 [ + + ]: 27687 : if (keyscmp == PATHKEYS_BETTER1)
3896 848 : 32 : remove_old = true;
195 849 [ + + ]: 27655 : else if (keyscmp == PATHKEYS_BETTER2)
850 : 1087 : accept_new = false;
851 [ + + ]: 26568 : else if (compare_path_costs_fuzzily(new_path, old_path,
852 : : 1.0000000001) == COSTS_BETTER1)
853 : 146 : remove_old = true;
854 : : else
855 : 26422 : accept_new = false;
856 : : }
857 : : }
858 : :
859 : : /*
860 : : * Remove current element from partial_pathlist if dominated by new.
861 : : */
3896 862 [ + + ]: 193369 : if (remove_old)
863 : : {
864 : 27836 : parent_rel->partial_pathlist =
2624 tgl@sss.pgh.pa.us 865 : 27836 : foreach_delete_current(parent_rel->partial_pathlist, p1);
3896 rhaas@postgresql.org 866 : 27836 : 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 : : */
213 874 [ + + ]: 165533 : if (new_path->disabled_nodes > old_path->disabled_nodes ||
875 [ + + ]: 162719 : (new_path->disabled_nodes == old_path->disabled_nodes &&
876 [ + + ]: 161734 : new_path->total_cost >= old_path->total_cost))
2624 tgl@sss.pgh.pa.us 877 : 123566 : 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 : : */
3896 rhaas@postgresql.org 885 [ + + ]: 193369 : if (!accept_new)
886 : 95951 : break;
887 : : }
888 : :
889 [ + + ]: 241011 : if (accept_new)
890 : : {
891 : : /* Accept the new path: insert it at proper place */
2624 tgl@sss.pgh.pa.us 892 : 145060 : parent_rel->partial_pathlist =
893 : 145060 : list_insert_nth(parent_rel->partial_pathlist, insert_at, new_path);
894 : : }
895 : : else
896 : : {
897 : : /* Reject and recycle the new path */
3896 rhaas@postgresql.org 898 : 95951 : pfree(new_path);
899 : : }
900 : 241011 : }
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
760 912 : 342961 : add_partial_path_precheck(RelOptInfo *parent_rel, int disabled_nodes,
913 : : Cost startup_cost, Cost total_cost, List *pathkeys)
914 : : {
195 915 : 342961 : 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 : : */
3896 930 [ + + + + : 429936 : foreach(p1, parent_rel->partial_pathlist)
+ + ]
931 : : {
932 : 350321 : 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 : : */
195 942 [ + + ]: 350321 : if (unlikely(old_path->disabled_nodes != disabled_nodes))
943 : : {
944 [ + + ]: 9582 : if (disabled_nodes < old_path->disabled_nodes)
945 : 3924 : costcmp = COSTS_BETTER1;
946 : : else
947 : 5658 : costcmp = COSTS_BETTER2;
948 : : }
949 [ + + ]: 340739 : else if (total_cost > old_path->total_cost * STD_FUZZ_FACTOR)
950 : : {
951 [ + + ]: 207652 : if (consider_startup &&
952 [ + + ]: 338 : old_path->startup_cost > startup_cost * STD_FUZZ_FACTOR)
953 : 248 : costcmp = COSTS_DIFFERENT;
954 : : else
955 : 207404 : costcmp = COSTS_BETTER2;
956 : : }
957 [ + + ]: 133087 : else if (old_path->total_cost > total_cost * STD_FUZZ_FACTOR)
958 : : {
959 [ + + ]: 129431 : if (consider_startup &&
960 [ + + ]: 528 : startup_cost > old_path->startup_cost * STD_FUZZ_FACTOR)
961 : 392 : costcmp = COSTS_DIFFERENT;
962 : : else
963 : 129039 : costcmp = COSTS_BETTER1;
964 : : }
965 [ + + ]: 3656 : else if (startup_cost > old_path->startup_cost * STD_FUZZ_FACTOR)
966 : 2397 : costcmp = COSTS_BETTER2;
967 [ + + ]: 1259 : else if (old_path->startup_cost > startup_cost * STD_FUZZ_FACTOR)
968 : 463 : costcmp = COSTS_BETTER1;
969 : : else
970 : 796 : 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 [ + + ]: 350321 : 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 : 349681 : keyscmp = compare_pathkeys(pathkeys, old_path->pathkeys);
984 [ + + ]: 349681 : 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 [ + + + + ]: 349529 : if (costcmp == COSTS_BETTER2 && keyscmp != PATHKEYS_BETTER1)
992 : 263346 : 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 [ + + + + ]: 176126 : if (costcmp == COSTS_BETTER1 && keyscmp != PATHKEYS_BETTER2)
999 : 89943 : 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 [ + + ]: 79615 : if (!add_path_precheck(parent_rel, disabled_nodes, startup_cost,
1009 : : total_cost, pathkeys, NULL))
3896 1010 : 2422 : return false;
1011 : :
1012 : 77193 : 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 *
3966 1026 : 332758 : create_seqscan_path(PlannerInfo *root, RelOptInfo *rel,
1027 : : Relids required_outer, int parallel_workers)
1028 : : {
10604 bruce@momjian.us 1029 : 332758 : Path *pathnode = makeNode(Path);
1030 : :
10605 1031 : 332758 : pathnode->pathtype = T_SeqScan;
1032 : 332758 : pathnode->parent = rel;
3842 tgl@sss.pgh.pa.us 1033 : 332758 : pathnode->pathtarget = rel->reltarget;
5267 1034 : 332758 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
1035 : : required_outer);
1838 michael@paquier.xyz 1036 : 332758 : pathnode->parallel_aware = (parallel_workers > 0);
3896 rhaas@postgresql.org 1037 : 332758 : pathnode->parallel_safe = rel->consider_parallel;
3755 1038 : 332758 : pathnode->parallel_workers = parallel_workers;
9897 tgl@sss.pgh.pa.us 1039 : 332758 : pathnode->pathkeys = NIL; /* seqscan has unordered result */
1040 : :
3896 rhaas@postgresql.org 1041 : 332758 : cost_seqscan(pathnode, root, rel, pathnode->param_info);
1042 : :
10246 bruce@momjian.us 1043 : 332758 : return pathnode;
1044 : : }
1045 : :
1046 : : /*
1047 : : * create_samplescan_path
1048 : : * Creates a path node for a sampled table scan.
1049 : : */
1050 : : Path *
4146 simon@2ndQuadrant.co 1051 : 247 : create_samplescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
1052 : : {
4138 bruce@momjian.us 1053 : 247 : Path *pathnode = makeNode(Path);
1054 : :
4146 simon@2ndQuadrant.co 1055 : 247 : pathnode->pathtype = T_SampleScan;
1056 : 247 : pathnode->parent = rel;
3842 tgl@sss.pgh.pa.us 1057 : 247 : pathnode->pathtarget = rel->reltarget;
4146 simon@2ndQuadrant.co 1058 : 247 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
1059 : : required_outer);
3966 rhaas@postgresql.org 1060 : 247 : pathnode->parallel_aware = false;
3896 1061 : 247 : pathnode->parallel_safe = rel->consider_parallel;
3755 1062 : 247 : pathnode->parallel_workers = 0;
4146 simon@2ndQuadrant.co 1063 : 247 : pathnode->pathkeys = NIL; /* samplescan has unordered result */
1064 : :
4075 tgl@sss.pgh.pa.us 1065 : 247 : cost_samplescan(pathnode, root, rel, pathnode->param_info);
1066 : :
4146 simon@2ndQuadrant.co 1067 : 247 : return pathnode;
1068 : : }
1069 : :
1070 : : /*
1071 : : * create_index_path
1072 : : * Creates a path node for an index scan.
1073 : : *
1074 : : * 'index' is a usable index.
1075 : : * 'indexclauses' is a list of IndexClause nodes representing clauses
1076 : : * to be enforced as qual conditions in the scan.
1077 : : * 'indexorderbys' is a list of bare expressions (no RestrictInfos)
1078 : : * to be used as index ordering operators in the scan.
1079 : : * 'indexorderbycols' is an integer list of index column numbers (zero based)
1080 : : * the ordering operators can be used with.
1081 : : * 'pathkeys' describes the ordering of the path.
1082 : : * 'indexscandir' is either ForwardScanDirection or BackwardScanDirection.
1083 : : * 'indexonly' is true if an index-only scan is wanted.
1084 : : * 'required_outer' is the set of outer relids for a parameterized path.
1085 : : * 'loop_count' is the number of repetitions of the indexscan to factor into
1086 : : * estimates of caching behavior.
1087 : : * 'partial_path' is true if constructing a parallel index scan path.
1088 : : *
1089 : : * Returns the new path node.
1090 : : */
1091 : : IndexPath *
7777 tgl@sss.pgh.pa.us 1092 : 652477 : 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 : : {
10604 bruce@momjian.us 1104 : 652477 : IndexPath *pathnode = makeNode(IndexPath);
7821 tgl@sss.pgh.pa.us 1105 : 652477 : RelOptInfo *rel = index->rel;
1106 : :
5458 1107 [ + + ]: 652477 : pathnode->path.pathtype = indexonly ? T_IndexOnlyScan : T_IndexScan;
7821 1108 : 652477 : pathnode->path.parent = rel;
3842 1109 : 652477 : pathnode->path.pathtarget = rel->reltarget;
5267 1110 : 652477 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1111 : : required_outer);
3966 rhaas@postgresql.org 1112 : 652477 : pathnode->path.parallel_aware = false;
3896 1113 : 652477 : pathnode->path.parallel_safe = rel->consider_parallel;
3755 1114 : 652477 : pathnode->path.parallel_workers = 0;
9411 tgl@sss.pgh.pa.us 1115 : 652477 : pathnode->path.pathkeys = pathkeys;
1116 : :
7818 1117 : 652477 : pathnode->indexinfo = index;
5384 1118 : 652477 : pathnode->indexclauses = indexclauses;
5771 1119 : 652477 : pathnode->indexorderbys = indexorderbys;
5384 1120 : 652477 : pathnode->indexorderbycols = indexorderbycols;
9714 1121 : 652477 : pathnode->indexscandir = indexscandir;
1122 : :
3504 rhaas@postgresql.org 1123 : 652477 : 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 : : */
194 1130 [ + + ]: 652477 : if (index->disabled)
1131 : 8191 : pathnode->path.disabled_nodes = 1;
1132 : :
10246 bruce@momjian.us 1133 : 652477 : 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 *
7777 tgl@sss.pgh.pa.us 1149 : 278127 : 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 : : {
7824 1156 : 278127 : BitmapHeapPath *pathnode = makeNode(BitmapHeapPath);
1157 : :
1158 : 278127 : pathnode->path.pathtype = T_BitmapHeapScan;
1159 : 278127 : pathnode->path.parent = rel;
3842 1160 : 278127 : pathnode->path.pathtarget = rel->reltarget;
5267 1161 : 278127 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1162 : : required_outer);
1838 michael@paquier.xyz 1163 : 278127 : pathnode->path.parallel_aware = (parallel_degree > 0);
3849 tgl@sss.pgh.pa.us 1164 : 278127 : pathnode->path.parallel_safe = rel->consider_parallel;
3483 rhaas@postgresql.org 1165 : 278127 : pathnode->path.parallel_workers = parallel_degree;
3378 tgl@sss.pgh.pa.us 1166 : 278127 : pathnode->path.pathkeys = NIL; /* always unordered */
1167 : :
7824 1168 : 278127 : pathnode->bitmapqual = bitmapqual;
1169 : :
5267 1170 : 278127 : cost_bitmap_heap_scan(&pathnode->path, root, rel,
1171 : : pathnode->path.param_info,
1172 : : bitmapqual, loop_count);
1173 : :
7822 1174 : 278127 : return pathnode;
1175 : : }
1176 : :
1177 : : /*
1178 : : * create_bitmap_and_path
1179 : : * Creates a path node representing a BitmapAnd.
1180 : : */
1181 : : BitmapAndPath *
7777 1182 : 44178 : create_bitmap_and_path(PlannerInfo *root,
1183 : : RelOptInfo *rel,
1184 : : List *bitmapquals)
1185 : : {
7822 1186 : 44178 : BitmapAndPath *pathnode = makeNode(BitmapAndPath);
2259 1187 : 44178 : Relids required_outer = NULL;
1188 : : ListCell *lc;
1189 : :
7822 1190 : 44178 : pathnode->path.pathtype = T_BitmapAnd;
1191 : 44178 : pathnode->path.parent = rel;
3842 1192 : 44178 : 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 : : */
2259 1199 [ + - + + : 132534 : foreach(lc, bitmapquals)
+ + ]
1200 : : {
1201 : 88356 : Path *bitmapqual = (Path *) lfirst(lc);
1202 : :
1203 : 88356 : required_outer = bms_add_members(required_outer,
1204 [ + + ]: 88356 : PATH_REQ_OUTER(bitmapqual));
1205 : : }
1206 : 44178 : 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 : : */
3966 rhaas@postgresql.org 1215 : 44178 : pathnode->path.parallel_aware = false;
3896 1216 : 44178 : pathnode->path.parallel_safe = rel->consider_parallel;
3755 1217 : 44178 : pathnode->path.parallel_workers = 0;
1218 : :
3378 tgl@sss.pgh.pa.us 1219 : 44178 : pathnode->path.pathkeys = NIL; /* always unordered */
1220 : :
7822 1221 : 44178 : pathnode->bitmapquals = bitmapquals;
1222 : :
1223 : : /* this sets bitmapselectivity as well as the regular cost fields: */
1224 : 44178 : cost_bitmap_and_node(pathnode, root);
1225 : :
1226 : 44178 : return pathnode;
1227 : : }
1228 : :
1229 : : /*
1230 : : * create_bitmap_or_path
1231 : : * Creates a path node representing a BitmapOr.
1232 : : */
1233 : : BitmapOrPath *
7777 1234 : 1686 : create_bitmap_or_path(PlannerInfo *root,
1235 : : RelOptInfo *rel,
1236 : : List *bitmapquals)
1237 : : {
7822 1238 : 1686 : BitmapOrPath *pathnode = makeNode(BitmapOrPath);
2259 1239 : 1686 : Relids required_outer = NULL;
1240 : : ListCell *lc;
1241 : :
7822 1242 : 1686 : pathnode->path.pathtype = T_BitmapOr;
1243 : 1686 : pathnode->path.parent = rel;
3842 1244 : 1686 : 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 : : */
2259 1251 [ + - + + : 3969 : foreach(lc, bitmapquals)
+ + ]
1252 : : {
1253 : 2283 : Path *bitmapqual = (Path *) lfirst(lc);
1254 : :
1255 : 2283 : required_outer = bms_add_members(required_outer,
1256 [ + + ]: 2283 : PATH_REQ_OUTER(bitmapqual));
1257 : : }
1258 : 1686 : 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 : : */
3966 rhaas@postgresql.org 1267 : 1686 : pathnode->path.parallel_aware = false;
3896 1268 : 1686 : pathnode->path.parallel_safe = rel->consider_parallel;
3755 1269 : 1686 : pathnode->path.parallel_workers = 0;
1270 : :
3378 tgl@sss.pgh.pa.us 1271 : 1686 : pathnode->path.pathkeys = NIL; /* always unordered */
1272 : :
7822 1273 : 1686 : pathnode->bitmapquals = bitmapquals;
1274 : :
1275 : : /* this sets bitmapselectivity as well as the regular cost fields: */
1276 : 1686 : cost_bitmap_or_node(pathnode, root);
1277 : :
7824 1278 : 1686 : 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 *
5138 1286 : 643 : create_tidscan_path(PlannerInfo *root, RelOptInfo *rel, List *tidquals,
1287 : : Relids required_outer)
1288 : : {
9657 bruce@momjian.us 1289 : 643 : TidPath *pathnode = makeNode(TidPath);
1290 : :
9798 1291 : 643 : pathnode->path.pathtype = T_TidScan;
1292 : 643 : pathnode->path.parent = rel;
3842 tgl@sss.pgh.pa.us 1293 : 643 : pathnode->path.pathtarget = rel->reltarget;
5138 1294 : 643 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1295 : : required_outer);
3966 rhaas@postgresql.org 1296 : 643 : pathnode->path.parallel_aware = false;
3896 1297 : 643 : pathnode->path.parallel_safe = rel->consider_parallel;
3755 1298 : 643 : pathnode->path.parallel_workers = 0;
3378 tgl@sss.pgh.pa.us 1299 : 643 : pathnode->path.pathkeys = NIL; /* always unordered */
1300 : :
7603 1301 : 643 : pathnode->tidquals = tidquals;
1302 : :
5138 1303 : 643 : cost_tidscan(&pathnode->path, root, rel, tidquals,
1304 : : pathnode->path.param_info);
1305 : :
9798 bruce@momjian.us 1306 : 643 : return pathnode;
1307 : : }
1308 : :
1309 : : /*
1310 : : * create_tidrangescan_path
1311 : : * Creates a path corresponding to a scan by a range of TIDs, returning
1312 : : * the pathnode.
1313 : : */
1314 : : TidRangePath *
2031 drowley@postgresql.o 1315 : 1698 : create_tidrangescan_path(PlannerInfo *root, RelOptInfo *rel,
1316 : : List *tidrangequals, Relids required_outer,
1317 : : int parallel_workers)
1318 : : {
1319 : 1698 : TidRangePath *pathnode = makeNode(TidRangePath);
1320 : :
1321 : 1698 : pathnode->path.pathtype = T_TidRangeScan;
1322 : 1698 : pathnode->path.parent = rel;
1323 : 1698 : pathnode->path.pathtarget = rel->reltarget;
1324 : 1698 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1325 : : required_outer);
297 1326 : 1698 : pathnode->path.parallel_aware = (parallel_workers > 0);
2031 1327 : 1698 : pathnode->path.parallel_safe = rel->consider_parallel;
297 1328 : 1698 : pathnode->path.parallel_workers = parallel_workers;
2031 1329 : 1698 : pathnode->path.pathkeys = NIL; /* always unordered */
1330 : :
1331 : 1698 : pathnode->tidrangequals = tidrangequals;
1332 : :
1333 : 1698 : cost_tidrangescan(&pathnode->path, root, rel, tidrangequals,
1334 : : pathnode->path.param_info);
1335 : :
1336 : 1698 : 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 *
3088 alvherre@alvh.no-ip. 1352 : 72688 : 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 : : {
9443 tgl@sss.pgh.pa.us 1359 : 72688 : AppendPath *pathnode = makeNode(AppendPath);
1360 : : ListCell *l;
1361 : :
3211 rhaas@postgresql.org 1362 [ + + - + ]: 72688 : Assert(!parallel_aware || parallel_workers > 0);
1363 : :
222 1364 : 72688 : pathnode->child_append_relid_sets = input.child_append_relid_sets;
9443 tgl@sss.pgh.pa.us 1365 : 72688 : pathnode->path.pathtype = T_Append;
1366 : 72688 : pathnode->path.parent = rel;
3842 1367 : 72688 : 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 : : */
222 rhaas@postgresql.org 1380 [ + + + + : 72688 : if (rel->reloptkind == RELOPT_BASEREL && root && input.subpaths != NIL)
+ + ]
3088 alvherre@alvh.no-ip. 1381 : 31069 : pathnode->path.param_info = get_baserel_parampathinfo(root,
1382 : : rel,
1383 : : required_outer);
1384 : : else
1385 : 41619 : pathnode->path.param_info = get_appendrel_parampathinfo(rel,
1386 : : required_outer);
1387 : :
3211 rhaas@postgresql.org 1388 : 72688 : pathnode->path.parallel_aware = parallel_aware;
3896 1389 : 72688 : pathnode->path.parallel_safe = rel->consider_parallel;
3755 1390 : 72688 : pathnode->path.parallel_workers = parallel_workers;
2725 tgl@sss.pgh.pa.us 1391 : 72688 : 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 : : */
3211 rhaas@postgresql.org 1402 [ + + ]: 72688 : 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 : : */
2725 tgl@sss.pgh.pa.us 1409 [ - + ]: 25866 : Assert(pathkeys == NIL);
1410 : :
222 rhaas@postgresql.org 1411 : 25866 : list_sort(input.subpaths, append_total_cost_compare);
1412 : 25866 : list_sort(input.partial_subpaths, append_startup_cost_compare);
1413 : : }
1414 : 72688 : pathnode->first_partial_path = list_length(input.subpaths);
1415 : 72688 : 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 : : */
1329 tgl@sss.pgh.pa.us 1421 [ + + + + ]: 72688 : if (root != NULL && bms_equal(rel->relids, root->all_query_rels))
2725 1422 : 34445 : pathnode->limit_tuples = root->limit_tuples;
1423 : : else
1424 : 38243 : pathnode->limit_tuples = -1.0;
1425 : :
3012 akapila@postgresql.o 1426 [ + + + + : 251638 : foreach(l, pathnode->subpaths)
+ + ]
1427 : : {
9313 bruce@momjian.us 1428 : 178950 : Path *subpath = (Path *) lfirst(l);
1429 : :
3896 rhaas@postgresql.org 1430 [ + + ]: 325337 : pathnode->path.parallel_safe = pathnode->path.parallel_safe &&
1431 [ + + ]: 146387 : subpath->parallel_safe;
1432 : :
1433 : : /* All child paths must have same parameterization */
5267 tgl@sss.pgh.pa.us 1434 [ + + - + ]: 178950 : Assert(bms_equal(PATH_REQ_OUTER(subpath), required_outer));
1435 : : }
1436 : :
3211 rhaas@postgresql.org 1437 [ + + - + ]: 72688 : Assert(!parallel_aware || pathnode->path.parallel_safe);
1438 : :
1439 : : /*
1440 : : * If there's exactly one child path then the output of the Append is
1441 : : * necessarily ordered the same as the child's, so we can inherit the
1442 : : * child's pathkeys if any, overriding whatever the caller might've said.
1443 : : * Furthermore, if the child's parallel awareness matches the Append's,
1444 : : * then the Append is a no-op and will be discarded later (in setrefs.c).
1445 : : * Then we can inherit the child's size, cost and disabled-node count too,
1446 : : * effectively charging zero for the Append. Otherwise, we must do the
1447 : : * normal costsize calculation.
1448 : : */
2736 tgl@sss.pgh.pa.us 1449 [ + + ]: 72688 : if (list_length(pathnode->subpaths) == 1)
1450 : : {
1451 : 15339 : Path *child = (Path *) linitial(pathnode->subpaths);
1452 : :
1524 1453 [ + + ]: 15339 : if (child->parallel_aware == parallel_aware)
1454 : : {
1455 : 14989 : pathnode->path.rows = child->rows;
23 rguo@postgresql.org 1456 : 14989 : pathnode->path.disabled_nodes = child->disabled_nodes;
1524 tgl@sss.pgh.pa.us 1457 : 14989 : pathnode->path.startup_cost = child->startup_cost;
1458 : 14989 : pathnode->path.total_cost = child->total_cost;
1459 : : }
1460 : : else
439 rguo@postgresql.org 1461 : 350 : cost_append(pathnode, root);
1462 : : /* Must do this last, else cost_append complains */
2736 tgl@sss.pgh.pa.us 1463 : 15339 : pathnode->path.pathkeys = child->pathkeys;
1464 : : }
1465 : : else
439 rguo@postgresql.org 1466 : 57349 : cost_append(pathnode, root);
1467 : :
1468 : : /* If the caller provided a row estimate, override the computed value. */
3211 rhaas@postgresql.org 1469 [ + + ]: 72688 : if (rows >= 0)
1470 : 483 : pathnode->path.rows = rows;
1471 : :
9443 tgl@sss.pgh.pa.us 1472 : 72688 : return pathnode;
1473 : : }
1474 : :
1475 : : /*
1476 : : * append_total_cost_compare
1477 : : * list_sort comparator for sorting append child paths
1478 : : * by total_cost descending
1479 : : *
1480 : : * For equal total costs, we fall back to comparing startup costs; if those
1481 : : * are equal too, break ties using bms_compare on the paths' relids.
1482 : : * (This is to avoid getting unpredictable results from list_sort.)
1483 : : */
1484 : : static int
2623 1485 : 12555 : append_total_cost_compare(const ListCell *a, const ListCell *b)
1486 : : {
1487 : 12555 : Path *path1 = (Path *) lfirst(a);
1488 : 12555 : Path *path2 = (Path *) lfirst(b);
1489 : : int cmp;
1490 : :
3176 1491 : 12555 : cmp = compare_path_costs(path1, path2, TOTAL_COST);
1492 [ + + ]: 12555 : if (cmp != 0)
1493 : 11616 : return -cmp;
1494 : 939 : return bms_compare(path1->parent->relids, path2->parent->relids);
1495 : : }
1496 : :
1497 : : /*
1498 : : * append_startup_cost_compare
1499 : : * list_sort comparator for sorting append child paths
1500 : : * by startup_cost descending
1501 : : *
1502 : : * For equal startup costs, we fall back to comparing total costs; if those
1503 : : * are equal too, break ties using bms_compare on the paths' relids.
1504 : : * (This is to avoid getting unpredictable results from list_sort.)
1505 : : */
1506 : : static int
2623 1507 : 38159 : append_startup_cost_compare(const ListCell *a, const ListCell *b)
1508 : : {
1509 : 38159 : Path *path1 = (Path *) lfirst(a);
1510 : 38159 : Path *path2 = (Path *) lfirst(b);
1511 : : int cmp;
1512 : :
3176 1513 : 38159 : cmp = compare_path_costs(path1, path2, STARTUP_COST);
1514 [ + + ]: 38159 : if (cmp != 0)
1515 : 18087 : return -cmp;
1516 : 20072 : return bms_compare(path1->parent->relids, path2->parent->relids);
1517 : : }
1518 : :
1519 : : /*
1520 : : * create_merge_append_path
1521 : : * Creates a path corresponding to a MergeAppend plan, returning the
1522 : : * pathnode.
1523 : : */
1524 : : MergeAppendPath *
5820 1525 : 7562 : create_merge_append_path(PlannerInfo *root,
1526 : : RelOptInfo *rel,
1527 : : List *subpaths,
1528 : : List *child_append_relid_sets,
1529 : : List *pathkeys,
1530 : : Relids required_outer)
1531 : : {
1532 : 7562 : MergeAppendPath *pathnode = makeNode(MergeAppendPath);
1533 : : int input_disabled_nodes;
1534 : : Cost input_startup_cost;
1535 : : Cost input_total_cost;
1536 : : ListCell *l;
1537 : :
1538 : : /*
1539 : : * We don't currently support parameterized MergeAppend paths, as
1540 : : * explained in the comments for generate_orderedappend_paths.
1541 : : */
783 rguo@postgresql.org 1542 [ + - - + ]: 7562 : Assert(bms_is_empty(rel->lateral_relids) && bms_is_empty(required_outer));
1543 : :
222 rhaas@postgresql.org 1544 : 7562 : pathnode->child_append_relid_sets = child_append_relid_sets;
5820 tgl@sss.pgh.pa.us 1545 : 7562 : pathnode->path.pathtype = T_MergeAppend;
1546 : 7562 : pathnode->path.parent = rel;
3842 1547 : 7562 : pathnode->path.pathtarget = rel->reltarget;
783 rguo@postgresql.org 1548 : 7562 : pathnode->path.param_info = NULL;
3966 rhaas@postgresql.org 1549 : 7562 : pathnode->path.parallel_aware = false;
3896 1550 : 7562 : pathnode->path.parallel_safe = rel->consider_parallel;
3755 1551 : 7562 : pathnode->path.parallel_workers = 0;
5820 tgl@sss.pgh.pa.us 1552 : 7562 : pathnode->path.pathkeys = pathkeys;
1553 : 7562 : pathnode->subpaths = subpaths;
1554 : :
1555 : : /*
1556 : : * Apply query-wide LIMIT if known and path is for sole base relation.
1557 : : * (Handling this at this low level is a bit klugy.)
1558 : : */
1329 1559 [ + + ]: 7562 : if (bms_equal(rel->relids, root->all_query_rels))
5267 1560 : 3503 : pathnode->limit_tuples = root->limit_tuples;
1561 : : else
1562 : 4059 : pathnode->limit_tuples = -1.0;
1563 : :
1564 : : /*
1565 : : * Add up the sizes and costs of the input paths.
1566 : : */
5350 1567 : 7562 : pathnode->path.rows = 0;
760 rhaas@postgresql.org 1568 : 7562 : input_disabled_nodes = 0;
5820 tgl@sss.pgh.pa.us 1569 : 7562 : input_startup_cost = 0;
1570 : 7562 : input_total_cost = 0;
1571 [ + - + + : 27464 : foreach(l, subpaths)
+ + ]
1572 : : {
1573 : 19902 : Path *subpath = (Path *) lfirst(l);
1574 : : int presorted_keys;
1575 : : Path sort_path; /* dummy for result of
1576 : : * cost_sort/cost_incremental_sort */
1577 : :
1578 : : /* All child paths should be unparameterized */
783 rguo@postgresql.org 1579 [ - + - - ]: 19902 : Assert(bms_is_empty(PATH_REQ_OUTER(subpath)));
1580 : :
5350 tgl@sss.pgh.pa.us 1581 : 19902 : pathnode->path.rows += subpath->rows;
3896 rhaas@postgresql.org 1582 [ + + ]: 37670 : pathnode->path.parallel_safe = pathnode->path.parallel_safe &&
1583 [ + + ]: 17768 : subpath->parallel_safe;
1584 : :
439 rguo@postgresql.org 1585 [ + + ]: 19902 : if (!pathkeys_count_contained_in(pathkeys, subpath->pathkeys,
1586 : : &presorted_keys))
1587 : : {
1588 : : /*
1589 : : * We'll need to insert a Sort node, so include costs for that. We
1590 : : * choose to use incremental sort if it is enabled and there are
1591 : : * presorted keys; otherwise we use full sort.
1592 : : *
1593 : : * We can use the parent's LIMIT if any, since we certainly won't
1594 : : * pull more than that many tuples from any child.
1595 : : */
1596 [ + - + + ]: 494 : if (enable_incremental_sort && presorted_keys > 0)
1597 : : {
1598 : 15 : cost_incremental_sort(&sort_path,
1599 : : root,
1600 : : pathkeys,
1601 : : presorted_keys,
1602 : : subpath->disabled_nodes,
1603 : : subpath->startup_cost,
1604 : : subpath->total_cost,
1605 : : subpath->rows,
1606 : 15 : subpath->pathtarget->width,
1607 : : 0.0,
1608 : : work_mem,
1609 : : pathnode->limit_tuples,
1610 : : NULL);
1611 : : }
1612 : : else
1613 : : {
1614 : 479 : cost_sort(&sort_path,
1615 : : root,
1616 : : pathkeys,
1617 : : subpath->disabled_nodes,
1618 : : subpath->total_cost,
1619 : : subpath->rows,
1620 : 479 : subpath->pathtarget->width,
1621 : : 0.0,
1622 : : work_mem,
1623 : : pathnode->limit_tuples);
1624 : : }
1625 : :
1626 : 494 : subpath = &sort_path;
1627 : : }
1628 : :
1629 : 19902 : input_disabled_nodes += subpath->disabled_nodes;
1630 : 19902 : input_startup_cost += subpath->startup_cost;
1631 : 19902 : input_total_cost += subpath->total_cost;
1632 : : }
1633 : :
1634 : : /*
1635 : : * Now we can compute total costs of the MergeAppend. If there's exactly
1636 : : * one child path and its parallel awareness matches that of the
1637 : : * MergeAppend, then the MergeAppend is a no-op and will be discarded
1638 : : * later (in setrefs.c); otherwise we do the normal cost calculation.
1639 : : */
1524 tgl@sss.pgh.pa.us 1640 [ + + ]: 7562 : if (list_length(subpaths) == 1 &&
1641 : 90 : ((Path *) linitial(subpaths))->parallel_aware ==
1642 [ + - ]: 90 : pathnode->path.parallel_aware)
1643 : : {
760 rhaas@postgresql.org 1644 : 90 : pathnode->path.disabled_nodes = input_disabled_nodes;
2736 tgl@sss.pgh.pa.us 1645 : 90 : pathnode->path.startup_cost = input_startup_cost;
1646 : 90 : pathnode->path.total_cost = input_total_cost;
1647 : : }
1648 : : else
1649 : 7472 : cost_merge_append(&pathnode->path, root,
1650 : : pathkeys, list_length(subpaths),
1651 : : input_disabled_nodes,
1652 : : input_startup_cost, input_total_cost,
1653 : : pathnode->path.rows);
1654 : :
5820 1655 : 7562 : return pathnode;
1656 : : }
1657 : :
1658 : : /*
1659 : : * create_group_result_path
1660 : : * Creates a path representing a Result-and-nothing-else plan.
1661 : : *
1662 : : * This is only used for degenerate grouping cases, in which we know we
1663 : : * need to produce one result row, possibly filtered by a HAVING qual.
1664 : : */
1665 : : GroupResultPath *
2792 1666 : 138790 : create_group_result_path(PlannerInfo *root, RelOptInfo *rel,
1667 : : PathTarget *target, List *havingqual)
1668 : : {
1669 : 138790 : GroupResultPath *pathnode = makeNode(GroupResultPath);
1670 : :
8719 1671 : 138790 : pathnode->path.pathtype = T_Result;
3867 1672 : 138790 : pathnode->path.parent = rel;
3849 1673 : 138790 : pathnode->path.pathtarget = target;
4862 bruce@momjian.us 1674 : 138790 : pathnode->path.param_info = NULL; /* there are no other rels... */
3966 rhaas@postgresql.org 1675 : 138790 : pathnode->path.parallel_aware = false;
3896 1676 : 138790 : pathnode->path.parallel_safe = rel->consider_parallel;
3755 1677 : 138790 : pathnode->path.parallel_workers = 0;
7386 tgl@sss.pgh.pa.us 1678 : 138790 : pathnode->path.pathkeys = NIL;
2792 1679 : 138790 : pathnode->quals = havingqual;
1680 : :
1681 : : /*
1682 : : * We can't quite use cost_resultscan() because the quals we want to
1683 : : * account for are not baserestrict quals of the rel. Might as well just
1684 : : * hack it here.
1685 : : */
5350 1686 : 138790 : pathnode->path.rows = 1;
3849 1687 : 138790 : pathnode->path.startup_cost = target->cost.startup;
1688 : 138790 : pathnode->path.total_cost = target->cost.startup +
1689 : 138790 : cpu_tuple_cost + target->cost.per_tuple;
1690 : :
1691 : : /*
1692 : : * Add cost of qual, if any --- but we ignore its selectivity, since our
1693 : : * rowcount estimate should be 1 no matter what the qual is.
1694 : : */
2792 1695 [ + + ]: 138790 : if (havingqual)
1696 : : {
1697 : : QualCost qual_cost;
1698 : :
1699 : 509 : cost_qual_eval(&qual_cost, havingqual, root);
1700 : : /* havingqual is evaluated once at startup */
3848 1701 : 509 : pathnode->path.startup_cost += qual_cost.startup + qual_cost.per_tuple;
1702 : 509 : pathnode->path.total_cost += qual_cost.startup + qual_cost.per_tuple;
1703 : : }
1704 : :
8719 1705 : 138790 : return pathnode;
1706 : : }
1707 : :
1708 : : /*
1709 : : * create_material_path
1710 : : * Creates a path corresponding to a Material plan, returning the
1711 : : * pathnode.
1712 : : */
1713 : : MaterialPath *
235 rhaas@postgresql.org 1714 : 467868 : create_material_path(RelOptInfo *rel, Path *subpath, bool enabled)
1715 : : {
8695 tgl@sss.pgh.pa.us 1716 : 467868 : MaterialPath *pathnode = makeNode(MaterialPath);
1717 : :
5267 1718 [ - + ]: 467868 : Assert(subpath->parent == rel);
1719 : :
8695 1720 : 467868 : pathnode->path.pathtype = T_Material;
1721 : 467868 : pathnode->path.parent = rel;
3842 1722 : 467868 : pathnode->path.pathtarget = rel->reltarget;
5267 1723 : 467868 : pathnode->path.param_info = subpath->param_info;
3966 rhaas@postgresql.org 1724 : 467868 : pathnode->path.parallel_aware = false;
3849 tgl@sss.pgh.pa.us 1725 [ + + ]: 895055 : pathnode->path.parallel_safe = rel->consider_parallel &&
1726 [ + + ]: 427187 : subpath->parallel_safe;
3755 rhaas@postgresql.org 1727 : 467868 : pathnode->path.parallel_workers = subpath->parallel_workers;
8695 tgl@sss.pgh.pa.us 1728 : 467868 : pathnode->path.pathkeys = subpath->pathkeys;
1729 : :
1730 : 467868 : pathnode->subpath = subpath;
1731 : :
1732 : 467868 : cost_material(&pathnode->path,
1733 : : enabled,
1734 : : subpath->disabled_nodes,
1735 : : subpath->startup_cost,
1736 : : subpath->total_cost,
1737 : : subpath->rows,
3867 1738 : 467868 : subpath->pathtarget->width);
1739 : :
8695 1740 : 467868 : return pathnode;
1741 : : }
1742 : :
1743 : : /*
1744 : : * create_memoize_path
1745 : : * Creates a path corresponding to a Memoize plan, returning the pathnode.
1746 : : */
1747 : : MemoizePath *
1894 drowley@postgresql.o 1748 : 199920 : create_memoize_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath,
1749 : : List *param_exprs, List *hash_operators,
1750 : : bool singlerow, bool binary_mode, Cardinality est_calls)
1751 : : {
1752 : 199920 : MemoizePath *pathnode = makeNode(MemoizePath);
1753 : :
1997 1754 [ - + ]: 199920 : Assert(subpath->parent == rel);
1755 : :
1894 1756 : 199920 : pathnode->path.pathtype = T_Memoize;
1997 1757 : 199920 : pathnode->path.parent = rel;
1758 : 199920 : pathnode->path.pathtarget = rel->reltarget;
1759 : 199920 : pathnode->path.param_info = subpath->param_info;
1760 : 199920 : pathnode->path.parallel_aware = false;
1761 [ + + ]: 389900 : pathnode->path.parallel_safe = rel->consider_parallel &&
1762 [ + + ]: 189980 : subpath->parallel_safe;
1763 : 199920 : pathnode->path.parallel_workers = subpath->parallel_workers;
1764 : 199920 : pathnode->path.pathkeys = subpath->pathkeys;
1765 : :
1766 : 199920 : pathnode->subpath = subpath;
1767 : 199920 : pathnode->hash_operators = hash_operators;
1768 : 199920 : pathnode->param_exprs = param_exprs;
1769 : 199920 : pathnode->singlerow = singlerow;
1761 1770 : 199920 : pathnode->binary_mode = binary_mode;
1771 : :
1772 : : /*
1773 : : * For now we set est_entries to 0. cost_memoize_rescan() does all the
1774 : : * hard work to determine how many cache entries there are likely to be,
1775 : : * so it seems best to leave it up to that function to fill this field in.
1776 : : * If left at 0, the executor will make a guess at a good value.
1777 : : */
1997 1778 : 199920 : pathnode->est_entries = 0;
1779 : :
418 1780 : 199920 : pathnode->est_calls = clamp_row_est(est_calls);
1781 : :
1782 : : /* These will also be set later in cost_memoize_rescan() */
1783 : 199920 : pathnode->est_unique_keys = 0.0;
1784 : 199920 : pathnode->est_hit_ratio = 0.0;
1785 : :
1786 : : /*
1787 : : * We should not be asked to generate this path type when memoization is
1788 : : * disabled, so set our count of disabled nodes equal to the subpath's
1789 : : * count.
1790 : : *
1791 : : * It would be nice to also Assert that memoization is enabled, but the
1792 : : * value of enable_memoize is not controlling: what we would need to check
1793 : : * is that the JoinPathExtraData's pgs_mask included PGS_NESTLOOP_MEMOIZE.
1794 : : */
760 rhaas@postgresql.org 1795 : 199920 : pathnode->path.disabled_nodes = subpath->disabled_nodes;
1796 : :
1797 : : /*
1798 : : * Add a small additional charge for caching the first entry. All the
1799 : : * harder calculations for rescans are performed in cost_memoize_rescan().
1800 : : */
1997 drowley@postgresql.o 1801 : 199920 : pathnode->path.startup_cost = subpath->startup_cost + cpu_tuple_cost;
1802 : 199920 : pathnode->path.total_cost = subpath->total_cost + cpu_tuple_cost;
1803 : 199920 : pathnode->path.rows = subpath->rows;
1804 : :
1805 : 199920 : return pathnode;
1806 : : }
1807 : :
1808 : : /*
1809 : : * create_gather_merge_path
1810 : : *
1811 : : * Creates a path corresponding to a gather merge scan, returning
1812 : : * the pathnode.
1813 : : */
1814 : : GatherMergePath *
3482 rhaas@postgresql.org 1815 : 15156 : create_gather_merge_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath,
1816 : : PathTarget *target, List *pathkeys,
1817 : : Relids required_outer, double *rows)
1818 : : {
1819 : 15156 : GatherMergePath *pathnode = makeNode(GatherMergePath);
760 1820 : 15156 : int input_disabled_nodes = 0;
3413 bruce@momjian.us 1821 : 15156 : Cost input_startup_cost = 0;
1822 : 15156 : Cost input_total_cost = 0;
1823 : :
3482 rhaas@postgresql.org 1824 [ - + ]: 15156 : Assert(subpath->parallel_safe);
1825 [ - + ]: 15156 : Assert(pathkeys);
1826 : :
1827 : : /*
1828 : : * The subpath should guarantee that it is adequately ordered either by
1829 : : * adding an explicit sort node or by using presorted input. We cannot
1830 : : * add an explicit Sort node for the subpath in createplan.c on additional
1831 : : * pathkeys, because we can't guarantee the sort would be safe. For
1832 : : * example, expressions may be volatile or otherwise parallel unsafe.
1833 : : */
789 rguo@postgresql.org 1834 [ - + ]: 15156 : if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
789 rguo@postgresql.org 1835 [ # # ]:UBC 0 : elog(ERROR, "gather merge input not sufficiently sorted");
1836 : :
3482 rhaas@postgresql.org 1837 :CBC 15156 : pathnode->path.pathtype = T_GatherMerge;
1838 : 15156 : pathnode->path.parent = rel;
1839 : 15156 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1840 : : required_outer);
1841 : 15156 : pathnode->path.parallel_aware = false;
1842 : :
1843 : 15156 : pathnode->subpath = subpath;
1844 : 15156 : pathnode->num_workers = subpath->parallel_workers;
1845 : 15156 : pathnode->path.pathkeys = pathkeys;
1846 [ - + ]: 15156 : pathnode->path.pathtarget = target ? target : rel->reltarget;
1847 : :
760 1848 : 15156 : input_disabled_nodes += subpath->disabled_nodes;
789 rguo@postgresql.org 1849 : 15156 : input_startup_cost += subpath->startup_cost;
1850 : 15156 : input_total_cost += subpath->total_cost;
1851 : :
3482 rhaas@postgresql.org 1852 : 15156 : cost_gather_merge(pathnode, root, rel, pathnode->path.param_info,
1853 : : input_disabled_nodes, input_startup_cost,
1854 : : input_total_cost, rows);
1855 : :
1856 : 15156 : return pathnode;
1857 : : }
1858 : :
1859 : : /*
1860 : : * create_gather_path
1861 : : * Creates a path corresponding to a gather scan, returning the
1862 : : * pathnode.
1863 : : *
1864 : : * 'rows' may optionally be set to override row estimates from other sources.
1865 : : */
1866 : : GatherPath *
4008 1867 : 21154 : create_gather_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath,
1868 : : PathTarget *target, Relids required_outer, double *rows)
1869 : : {
1870 : 21154 : GatherPath *pathnode = makeNode(GatherPath);
1871 : :
3896 1872 [ - + ]: 21154 : Assert(subpath->parallel_safe);
1873 : :
4008 1874 : 21154 : pathnode->path.pathtype = T_Gather;
1875 : 21154 : pathnode->path.parent = rel;
3835 1876 : 21154 : pathnode->path.pathtarget = target;
4008 1877 : 21154 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1878 : : required_outer);
3966 1879 : 21154 : pathnode->path.parallel_aware = false;
3896 1880 : 21154 : pathnode->path.parallel_safe = false;
3460 1881 : 21154 : pathnode->path.parallel_workers = 0;
3378 tgl@sss.pgh.pa.us 1882 : 21154 : pathnode->path.pathkeys = NIL; /* Gather has unordered result */
1883 : :
4008 rhaas@postgresql.org 1884 : 21154 : pathnode->subpath = subpath;
3460 1885 : 21154 : pathnode->num_workers = subpath->parallel_workers;
3896 1886 : 21154 : pathnode->single_copy = false;
1887 : :
3460 1888 [ - + ]: 21154 : if (pathnode->num_workers == 0)
1889 : : {
3896 rhaas@postgresql.org 1890 :UBC 0 : pathnode->path.pathkeys = subpath->pathkeys;
3460 1891 : 0 : pathnode->num_workers = 1;
3896 1892 : 0 : pathnode->single_copy = true;
1893 : : }
1894 : :
3835 rhaas@postgresql.org 1895 :CBC 21154 : cost_gather(pathnode, root, rel, pathnode->path.param_info, rows);
1896 : :
4008 1897 : 21154 : return pathnode;
1898 : : }
1899 : :
1900 : : /*
1901 : : * create_subqueryscan_path
1902 : : * Creates a path corresponding to a scan of a subquery,
1903 : : * returning the pathnode.
1904 : : *
1905 : : * Caller must pass trivial_pathtarget = true if it believes rel->reltarget to
1906 : : * be trivial, ie just a fetch of all the subquery output columns in order.
1907 : : * While we could determine that here, the caller can usually do it more
1908 : : * efficiently (or at least amortize it over multiple calls).
1909 : : */
1910 : : SubqueryScanPath *
3849 tgl@sss.pgh.pa.us 1911 : 48077 : create_subqueryscan_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath,
1912 : : bool trivial_pathtarget,
1913 : : List *pathkeys, Relids required_outer)
1914 : : {
1915 : 48077 : SubqueryScanPath *pathnode = makeNode(SubqueryScanPath);
1916 : :
1917 : 48077 : pathnode->path.pathtype = T_SubqueryScan;
1918 : 48077 : pathnode->path.parent = rel;
3842 1919 : 48077 : pathnode->path.pathtarget = rel->reltarget;
3849 1920 : 48077 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
1921 : : required_outer);
1922 : 48077 : pathnode->path.parallel_aware = false;
1923 [ + + ]: 80355 : pathnode->path.parallel_safe = rel->consider_parallel &&
1924 [ + + ]: 32278 : subpath->parallel_safe;
3755 rhaas@postgresql.org 1925 : 48077 : pathnode->path.parallel_workers = subpath->parallel_workers;
3849 tgl@sss.pgh.pa.us 1926 : 48077 : pathnode->path.pathkeys = pathkeys;
1927 : 48077 : pathnode->subpath = subpath;
1928 : :
1524 1929 : 48077 : cost_subqueryscan(pathnode, root, rel, pathnode->path.param_info,
1930 : : trivial_pathtarget);
1931 : :
9487 1932 : 48077 : return pathnode;
1933 : : }
1934 : :
1935 : : /*
1936 : : * create_functionscan_path
1937 : : * Creates a path corresponding to a sequential scan of a function,
1938 : : * returning the pathnode.
1939 : : */
1940 : : Path *
5157 1941 : 35102 : create_functionscan_path(PlannerInfo *root, RelOptInfo *rel,
1942 : : List *pathkeys, Relids required_outer)
1943 : : {
8897 1944 : 35102 : Path *pathnode = makeNode(Path);
1945 : :
1946 : 35102 : pathnode->pathtype = T_FunctionScan;
1947 : 35102 : pathnode->parent = rel;
3842 1948 : 35102 : pathnode->pathtarget = rel->reltarget;
5157 1949 : 35102 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
1950 : : required_outer);
3966 rhaas@postgresql.org 1951 : 35102 : pathnode->parallel_aware = false;
3896 1952 : 35102 : pathnode->parallel_safe = rel->consider_parallel;
3755 1953 : 35102 : pathnode->parallel_workers = 0;
4686 tgl@sss.pgh.pa.us 1954 : 35102 : pathnode->pathkeys = pathkeys;
1955 : :
5157 1956 : 35102 : cost_functionscan(pathnode, root, rel, pathnode->param_info);
1957 : :
3483 alvherre@alvh.no-ip. 1958 : 35102 : return pathnode;
1959 : : }
1960 : :
1961 : : /*
1962 : : * create_tablefuncscan_path
1963 : : * Creates a path corresponding to a sequential scan of a table function,
1964 : : * returning the pathnode.
1965 : : */
1966 : : Path *
1967 : 617 : create_tablefuncscan_path(PlannerInfo *root, RelOptInfo *rel,
1968 : : Relids required_outer)
1969 : : {
1970 : 617 : Path *pathnode = makeNode(Path);
1971 : :
1972 : 617 : pathnode->pathtype = T_TableFuncScan;
1973 : 617 : pathnode->parent = rel;
1974 : 617 : pathnode->pathtarget = rel->reltarget;
1975 : 617 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
1976 : : required_outer);
1977 : 617 : pathnode->parallel_aware = false;
1978 : 617 : pathnode->parallel_safe = rel->consider_parallel;
1979 : 617 : pathnode->parallel_workers = 0;
1980 : 617 : pathnode->pathkeys = NIL; /* result is always unordered */
1981 : :
1982 : 617 : cost_tablefuncscan(pathnode, root, rel, pathnode->param_info);
1983 : :
8897 tgl@sss.pgh.pa.us 1984 : 617 : return pathnode;
1985 : : }
1986 : :
1987 : : /*
1988 : : * create_valuesscan_path
1989 : : * Creates a path corresponding to a scan of a VALUES list,
1990 : : * returning the pathnode.
1991 : : */
1992 : : Path *
5152 1993 : 6788 : create_valuesscan_path(PlannerInfo *root, RelOptInfo *rel,
1994 : : Relids required_outer)
1995 : : {
7354 mail@joeconway.com 1996 : 6788 : Path *pathnode = makeNode(Path);
1997 : :
1998 : 6788 : pathnode->pathtype = T_ValuesScan;
1999 : 6788 : pathnode->parent = rel;
3842 tgl@sss.pgh.pa.us 2000 : 6788 : pathnode->pathtarget = rel->reltarget;
5152 2001 : 6788 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
2002 : : required_outer);
3966 rhaas@postgresql.org 2003 : 6788 : pathnode->parallel_aware = false;
3896 2004 : 6788 : pathnode->parallel_safe = rel->consider_parallel;
3755 2005 : 6788 : pathnode->parallel_workers = 0;
7354 mail@joeconway.com 2006 : 6788 : pathnode->pathkeys = NIL; /* result is always unordered */
2007 : :
5152 tgl@sss.pgh.pa.us 2008 : 6788 : cost_valuesscan(pathnode, root, rel, pathnode->param_info);
2009 : :
7354 mail@joeconway.com 2010 : 6788 : return pathnode;
2011 : : }
2012 : :
2013 : : /*
2014 : : * create_ctescan_path
2015 : : * Creates a path corresponding to a scan of a non-self-reference CTE,
2016 : : * returning the pathnode.
2017 : : */
2018 : : Path *
908 tgl@sss.pgh.pa.us 2019 : 3000 : create_ctescan_path(PlannerInfo *root, RelOptInfo *rel,
2020 : : List *pathkeys, Relids required_outer)
2021 : : {
6560 2022 : 3000 : Path *pathnode = makeNode(Path);
2023 : :
2024 : 3000 : pathnode->pathtype = T_CteScan;
2025 : 3000 : pathnode->parent = rel;
3842 2026 : 3000 : pathnode->pathtarget = rel->reltarget;
5138 2027 : 3000 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
2028 : : required_outer);
3966 rhaas@postgresql.org 2029 : 3000 : pathnode->parallel_aware = false;
3896 2030 : 3000 : pathnode->parallel_safe = rel->consider_parallel;
3755 2031 : 3000 : pathnode->parallel_workers = 0;
908 tgl@sss.pgh.pa.us 2032 : 3000 : pathnode->pathkeys = pathkeys;
2033 : :
5138 2034 : 3000 : cost_ctescan(pathnode, root, rel, pathnode->param_info);
2035 : :
6560 2036 : 3000 : return pathnode;
2037 : : }
2038 : :
2039 : : /*
2040 : : * create_namedtuplestorescan_path
2041 : : * Creates a path corresponding to a scan of a named tuplestore, returning
2042 : : * the pathnode.
2043 : : */
2044 : : Path *
3460 kgrittn@postgresql.o 2045 : 393 : create_namedtuplestorescan_path(PlannerInfo *root, RelOptInfo *rel,
2046 : : Relids required_outer)
2047 : : {
2048 : 393 : Path *pathnode = makeNode(Path);
2049 : :
2050 : 393 : pathnode->pathtype = T_NamedTuplestoreScan;
2051 : 393 : pathnode->parent = rel;
2052 : 393 : pathnode->pathtarget = rel->reltarget;
2053 : 393 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
2054 : : required_outer);
2055 : 393 : pathnode->parallel_aware = false;
2056 : 393 : pathnode->parallel_safe = rel->consider_parallel;
2057 : 393 : pathnode->parallel_workers = 0;
2058 : 393 : pathnode->pathkeys = NIL; /* result is always unordered */
2059 : :
2060 : 393 : cost_namedtuplestorescan(pathnode, root, rel, pathnode->param_info);
2061 : :
2062 : 393 : return pathnode;
2063 : : }
2064 : :
2065 : : /*
2066 : : * create_resultscan_path
2067 : : * Creates a path corresponding to a scan of an RTE_RESULT relation,
2068 : : * returning the pathnode.
2069 : : */
2070 : : Path *
2792 tgl@sss.pgh.pa.us 2071 : 3686 : create_resultscan_path(PlannerInfo *root, RelOptInfo *rel,
2072 : : Relids required_outer)
2073 : : {
2074 : 3686 : Path *pathnode = makeNode(Path);
2075 : :
2076 : 3686 : pathnode->pathtype = T_Result;
2077 : 3686 : pathnode->parent = rel;
2078 : 3686 : pathnode->pathtarget = rel->reltarget;
2079 : 3686 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
2080 : : required_outer);
2081 : 3686 : pathnode->parallel_aware = false;
2082 : 3686 : pathnode->parallel_safe = rel->consider_parallel;
2083 : 3686 : pathnode->parallel_workers = 0;
2084 : 3686 : pathnode->pathkeys = NIL; /* result is always unordered */
2085 : :
2086 : 3686 : cost_resultscan(pathnode, root, rel, pathnode->param_info);
2087 : :
2088 : 3686 : return pathnode;
2089 : : }
2090 : :
2091 : : /*
2092 : : * create_worktablescan_path
2093 : : * Creates a path corresponding to a scan of a self-reference CTE,
2094 : : * returning the pathnode.
2095 : : */
2096 : : Path *
5138 2097 : 640 : create_worktablescan_path(PlannerInfo *root, RelOptInfo *rel,
2098 : : Relids required_outer)
2099 : : {
6560 2100 : 640 : Path *pathnode = makeNode(Path);
2101 : :
2102 : 640 : pathnode->pathtype = T_WorkTableScan;
2103 : 640 : pathnode->parent = rel;
3842 2104 : 640 : pathnode->pathtarget = rel->reltarget;
5138 2105 : 640 : pathnode->param_info = get_baserel_parampathinfo(root, rel,
2106 : : required_outer);
3966 rhaas@postgresql.org 2107 : 640 : pathnode->parallel_aware = false;
3896 2108 : 640 : pathnode->parallel_safe = rel->consider_parallel;
3755 2109 : 640 : pathnode->parallel_workers = 0;
6560 tgl@sss.pgh.pa.us 2110 : 640 : pathnode->pathkeys = NIL; /* result is always unordered */
2111 : :
2112 : : /* Cost is the same as for a regular CTE scan */
5138 2113 : 640 : cost_ctescan(pathnode, root, rel, pathnode->param_info);
2114 : :
6560 2115 : 640 : return pathnode;
2116 : : }
2117 : :
2118 : : /*
2119 : : * create_foreignscan_path
2120 : : * Creates a path corresponding to a scan of a foreign base table,
2121 : : * returning the pathnode.
2122 : : *
2123 : : * This function is never called from core Postgres; rather, it's expected
2124 : : * to be called by the GetForeignPaths function of a foreign data wrapper.
2125 : : * We make the FDW supply all fields of the path, since we do not have any way
2126 : : * to calculate them in core. However, there is a usually-sane default for
2127 : : * the pathtarget (rel->reltarget), so we let a NULL for "target" select that.
2128 : : */
2129 : : ForeignPath *
5312 2130 : 1981 : create_foreignscan_path(PlannerInfo *root, RelOptInfo *rel,
2131 : : PathTarget *target,
2132 : : double rows, int disabled_nodes,
2133 : : Cost startup_cost, Cost total_cost,
2134 : : List *pathkeys,
2135 : : Relids required_outer,
2136 : : Path *fdw_outerpath,
2137 : : List *fdw_restrictinfo,
2138 : : List *fdw_private)
2139 : : {
5691 2140 : 1981 : ForeignPath *pathnode = makeNode(ForeignPath);
2141 : :
2142 : : /* Historically some FDWs were confused about when to use this */
2782 2143 [ + + - + ]: 1981 : Assert(IS_SIMPLE_REL(rel));
2144 : :
5691 2145 : 1981 : pathnode->path.pathtype = T_ForeignScan;
2146 : 1981 : pathnode->path.parent = rel;
3842 2147 [ + - ]: 1981 : pathnode->path.pathtarget = target ? target : rel->reltarget;
5267 2148 : 1981 : pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
2149 : : required_outer);
3966 rhaas@postgresql.org 2150 : 1981 : pathnode->path.parallel_aware = false;
3896 2151 : 1981 : pathnode->path.parallel_safe = rel->consider_parallel;
2782 tgl@sss.pgh.pa.us 2152 : 1981 : pathnode->path.parallel_workers = 0;
2153 : 1981 : pathnode->path.rows = rows;
760 rhaas@postgresql.org 2154 : 1981 : pathnode->path.disabled_nodes = disabled_nodes;
2782 tgl@sss.pgh.pa.us 2155 : 1981 : pathnode->path.startup_cost = startup_cost;
2156 : 1981 : pathnode->path.total_cost = total_cost;
2157 : 1981 : pathnode->path.pathkeys = pathkeys;
2158 : :
2159 : 1981 : pathnode->fdw_outerpath = fdw_outerpath;
1132 efujita@postgresql.o 2160 : 1981 : pathnode->fdw_restrictinfo = fdw_restrictinfo;
2782 tgl@sss.pgh.pa.us 2161 : 1981 : pathnode->fdw_private = fdw_private;
2162 : :
2163 : 1981 : return pathnode;
2164 : : }
2165 : :
2166 : : /*
2167 : : * create_foreign_join_path
2168 : : * Creates a path corresponding to a scan of a foreign join,
2169 : : * returning the pathnode.
2170 : : *
2171 : : * This function is never called from core Postgres; rather, it's expected
2172 : : * to be called by the GetForeignJoinPaths function of a foreign data wrapper.
2173 : : * We make the FDW supply all fields of the path, since we do not have any way
2174 : : * to calculate them in core. However, there is a usually-sane default for
2175 : : * the pathtarget (rel->reltarget), so we let a NULL for "target" select that.
2176 : : */
2177 : : ForeignPath *
2178 : 657 : create_foreign_join_path(PlannerInfo *root, RelOptInfo *rel,
2179 : : PathTarget *target,
2180 : : double rows, int disabled_nodes,
2181 : : Cost startup_cost, Cost total_cost,
2182 : : List *pathkeys,
2183 : : Relids required_outer,
2184 : : Path *fdw_outerpath,
2185 : : List *fdw_restrictinfo,
2186 : : List *fdw_private)
2187 : : {
2188 : 657 : ForeignPath *pathnode = makeNode(ForeignPath);
2189 : :
2190 : : /*
2191 : : * We should use get_joinrel_parampathinfo to handle parameterized paths,
2192 : : * but the API of this function doesn't support it, and existing
2193 : : * extensions aren't yet trying to build such paths anyway. For the
2194 : : * moment just throw an error if someone tries it; eventually we should
2195 : : * revisit this.
2196 : : */
2197 [ + - - + ]: 657 : if (!bms_is_empty(required_outer) || !bms_is_empty(rel->lateral_relids))
2782 tgl@sss.pgh.pa.us 2198 [ # # ]:UBC 0 : elog(ERROR, "parameterized foreign joins are not supported yet");
2199 : :
2782 tgl@sss.pgh.pa.us 2200 :CBC 657 : pathnode->path.pathtype = T_ForeignScan;
2201 : 657 : pathnode->path.parent = rel;
2202 [ + - ]: 657 : pathnode->path.pathtarget = target ? target : rel->reltarget;
2203 : 657 : pathnode->path.param_info = NULL; /* XXX see above */
2204 : 657 : pathnode->path.parallel_aware = false;
2205 : 657 : pathnode->path.parallel_safe = rel->consider_parallel;
2206 : 657 : pathnode->path.parallel_workers = 0;
2207 : 657 : pathnode->path.rows = rows;
760 rhaas@postgresql.org 2208 : 657 : pathnode->path.disabled_nodes = disabled_nodes;
2782 tgl@sss.pgh.pa.us 2209 : 657 : pathnode->path.startup_cost = startup_cost;
2210 : 657 : pathnode->path.total_cost = total_cost;
2211 : 657 : pathnode->path.pathkeys = pathkeys;
2212 : :
2213 : 657 : pathnode->fdw_outerpath = fdw_outerpath;
1132 efujita@postgresql.o 2214 : 657 : pathnode->fdw_restrictinfo = fdw_restrictinfo;
2782 tgl@sss.pgh.pa.us 2215 : 657 : pathnode->fdw_private = fdw_private;
2216 : :
2217 : 657 : return pathnode;
2218 : : }
2219 : :
2220 : : /*
2221 : : * create_foreign_upper_path
2222 : : * Creates a path corresponding to an upper relation that's computed
2223 : : * directly by an FDW, returning the pathnode.
2224 : : *
2225 : : * This function is never called from core Postgres; rather, it's expected to
2226 : : * be called by the GetForeignUpperPaths function of a foreign data wrapper.
2227 : : * We make the FDW supply all fields of the path, since we do not have any way
2228 : : * to calculate them in core. However, there is a usually-sane default for
2229 : : * the pathtarget (rel->reltarget), so we let a NULL for "target" select that.
2230 : : */
2231 : : ForeignPath *
2232 : 297 : create_foreign_upper_path(PlannerInfo *root, RelOptInfo *rel,
2233 : : PathTarget *target,
2234 : : double rows, int disabled_nodes,
2235 : : Cost startup_cost, Cost total_cost,
2236 : : List *pathkeys,
2237 : : Path *fdw_outerpath,
2238 : : List *fdw_restrictinfo,
2239 : : List *fdw_private)
2240 : : {
2241 : 297 : ForeignPath *pathnode = makeNode(ForeignPath);
2242 : :
2243 : : /*
2244 : : * Upper relations should never have any lateral references, since joining
2245 : : * is complete.
2246 : : */
2247 [ - + ]: 297 : Assert(bms_is_empty(rel->lateral_relids));
2248 : :
2249 : 297 : pathnode->path.pathtype = T_ForeignScan;
2250 : 297 : pathnode->path.parent = rel;
2251 [ - + ]: 297 : pathnode->path.pathtarget = target ? target : rel->reltarget;
2252 : 297 : pathnode->path.param_info = NULL;
2253 : 297 : pathnode->path.parallel_aware = false;
2254 : 297 : pathnode->path.parallel_safe = rel->consider_parallel;
3755 rhaas@postgresql.org 2255 : 297 : pathnode->path.parallel_workers = 0;
5312 tgl@sss.pgh.pa.us 2256 : 297 : pathnode->path.rows = rows;
760 rhaas@postgresql.org 2257 : 297 : pathnode->path.disabled_nodes = disabled_nodes;
5312 tgl@sss.pgh.pa.us 2258 : 297 : pathnode->path.startup_cost = startup_cost;
2259 : 297 : pathnode->path.total_cost = total_cost;
2260 : 297 : pathnode->path.pathkeys = pathkeys;
2261 : :
3939 rhaas@postgresql.org 2262 : 297 : pathnode->fdw_outerpath = fdw_outerpath;
1132 efujita@postgresql.o 2263 : 297 : pathnode->fdw_restrictinfo = fdw_restrictinfo;
5312 tgl@sss.pgh.pa.us 2264 : 297 : pathnode->fdw_private = fdw_private;
2265 : :
5691 2266 : 297 : return pathnode;
2267 : : }
2268 : :
2269 : : /*
2270 : : * calc_nestloop_required_outer
2271 : : * Compute the required_outer set for a nestloop join path
2272 : : *
2273 : : * Note: when considering a child join, the inputs nonetheless use top-level
2274 : : * parent relids
2275 : : *
2276 : : * Note: result must not share storage with either input
2277 : : */
2278 : : Relids
3323 rhaas@postgresql.org 2279 : 2482418 : calc_nestloop_required_outer(Relids outerrelids,
2280 : : Relids outer_paramrels,
2281 : : Relids innerrelids,
2282 : : Relids inner_paramrels)
2283 : : {
2284 : : Relids required_outer;
2285 : :
2286 : : /* inner_path can require rels from outer path, but not vice versa */
2287 [ - + ]: 2482418 : Assert(!bms_overlap(outer_paramrels, innerrelids));
2288 : : /* easy case if inner path is not parameterized */
5267 tgl@sss.pgh.pa.us 2289 [ + + ]: 2482418 : if (!inner_paramrels)
2290 : 1738689 : return bms_copy(outer_paramrels);
2291 : : /* else, form the union ... */
2292 : 743729 : required_outer = bms_union(outer_paramrels, inner_paramrels);
2293 : : /* ... and remove any mention of now-satisfied outer rels */
5350 2294 : 743729 : required_outer = bms_del_members(required_outer,
2295 : : outerrelids);
2296 : 743729 : return required_outer;
2297 : : }
2298 : :
2299 : : /*
2300 : : * calc_non_nestloop_required_outer
2301 : : * Compute the required_outer set for a merge or hash join path
2302 : : *
2303 : : * Note: result must not share storage with either input
2304 : : */
2305 : : Relids
2306 : 1557441 : calc_non_nestloop_required_outer(Path *outer_path, Path *inner_path)
2307 : : {
5215 bruce@momjian.us 2308 [ + + ]: 1557441 : Relids outer_paramrels = PATH_REQ_OUTER(outer_path);
2309 [ + + ]: 1557441 : Relids inner_paramrels = PATH_REQ_OUTER(inner_path);
2310 : : Relids innerrelids PG_USED_FOR_ASSERTS_ONLY;
2311 : : Relids outerrelids PG_USED_FOR_ASSERTS_ONLY;
2312 : : Relids required_outer;
2313 : :
2314 : : /*
2315 : : * Any parameterization of the input paths refers to topmost parents of
2316 : : * the relevant relations, because reparameterize_path_by_child() hasn't
2317 : : * been called yet. So we must consider topmost parents of the relations
2318 : : * being joined, too, while checking for disallowed parameterization
2319 : : * cases.
2320 : : */
984 tgl@sss.pgh.pa.us 2321 [ + + ]: 1557441 : if (inner_path->parent->top_parent_relids)
2322 : 116034 : innerrelids = inner_path->parent->top_parent_relids;
2323 : : else
2324 : 1441407 : innerrelids = inner_path->parent->relids;
2325 : :
2326 [ + + ]: 1557441 : if (outer_path->parent->top_parent_relids)
2327 : 116034 : outerrelids = outer_path->parent->top_parent_relids;
2328 : : else
2329 : 1441407 : outerrelids = outer_path->parent->relids;
2330 : :
2331 : : /* neither path can require rels from the other */
2332 [ - + ]: 1557441 : Assert(!bms_overlap(outer_paramrels, innerrelids));
2333 [ - + ]: 1557441 : Assert(!bms_overlap(inner_paramrels, outerrelids));
2334 : : /* form the union ... */
5267 2335 : 1557441 : required_outer = bms_union(outer_paramrels, inner_paramrels);
2336 : : /* we do not need an explicit test for empty; bms_union gets it right */
5350 2337 : 1557441 : return required_outer;
2338 : : }
2339 : :
2340 : : /*
2341 : : * create_nestloop_path
2342 : : * Creates a pathnode corresponding to a nestloop join between two
2343 : : * relations.
2344 : : *
2345 : : * 'joinrel' is the join relation.
2346 : : * 'jointype' is the type of join required
2347 : : * 'workspace' is the result from initial_cost_nestloop
2348 : : * 'extra' contains various information about the join
2349 : : * 'outer_path' is the outer path
2350 : : * 'inner_path' is the inner path
2351 : : * 'restrict_clauses' are the RestrictInfo nodes to apply at the join
2352 : : * 'pathkeys' are the path keys of the new join path
2353 : : * 'required_outer' is the set of required outer rels
2354 : : *
2355 : : * Returns the resulting path node.
2356 : : */
2357 : : NestPath *
7777 2358 : 1137496 : create_nestloop_path(PlannerInfo *root,
2359 : : RelOptInfo *joinrel,
2360 : : JoinType jointype,
2361 : : JoinCostWorkspace *workspace,
2362 : : JoinPathExtraData *extra,
2363 : : Path *outer_path,
2364 : : Path *inner_path,
2365 : : List *restrict_clauses,
2366 : : List *pathkeys,
2367 : : Relids required_outer)
2368 : : {
10082 bruce@momjian.us 2369 : 1137496 : NestPath *pathnode = makeNode(NestPath);
5267 tgl@sss.pgh.pa.us 2370 [ + + ]: 1137496 : Relids inner_req_outer = PATH_REQ_OUTER(inner_path);
2371 : : Relids outerrelids;
2372 : :
2373 : : /*
2374 : : * Paths are parameterized by top-level parents, so run parameterization
2375 : : * tests on the parent relids.
2376 : : */
915 2377 [ + + ]: 1137496 : if (outer_path->parent->top_parent_relids)
2378 : 68972 : outerrelids = outer_path->parent->top_parent_relids;
2379 : : else
2380 : 1068524 : outerrelids = outer_path->parent->relids;
2381 : :
2382 : : /*
2383 : : * If the inner path is parameterized by the outer, we must drop any
2384 : : * restrict_clauses that are due to be moved into the inner path. We have
2385 : : * to do this now, rather than postpone the work till createplan time,
2386 : : * because the restrict_clauses list can affect the size and cost
2387 : : * estimates for this path. We detect such clauses by checking for serial
2388 : : * number match to clauses already enforced in the inner path.
2389 : : */
2390 [ + + ]: 1137496 : if (bms_overlap(inner_req_outer, outerrelids))
2391 : : {
1329 2392 : 288439 : Bitmapset *enforced_serials = get_param_path_clause_serials(inner_path);
5267 2393 : 288439 : List *jclauses = NIL;
2394 : : ListCell *lc;
2395 : :
2396 [ + + + + : 649136 : foreach(lc, restrict_clauses)
+ + ]
2397 : : {
2398 : 360697 : RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2399 : :
1329 2400 [ + + ]: 360697 : if (!bms_is_member(rinfo->rinfo_serial, enforced_serials))
5350 2401 : 59682 : jclauses = lappend(jclauses, rinfo);
2402 : : }
5267 2403 : 288439 : restrict_clauses = jclauses;
2404 : : }
2405 : :
1869 peter@eisentraut.org 2406 : 1137496 : pathnode->jpath.path.pathtype = T_NestLoop;
2407 : 1137496 : pathnode->jpath.path.parent = joinrel;
2408 : 1137496 : pathnode->jpath.path.pathtarget = joinrel->reltarget;
2409 : 1137496 : pathnode->jpath.path.param_info =
5267 tgl@sss.pgh.pa.us 2410 : 1137496 : get_joinrel_parampathinfo(root,
2411 : : joinrel,
2412 : : outer_path,
2413 : : inner_path,
2414 : : extra->sjinfo,
2415 : : required_outer,
2416 : : &restrict_clauses);
1869 peter@eisentraut.org 2417 : 1137496 : pathnode->jpath.path.parallel_aware = false;
2418 : 3309484 : pathnode->jpath.path.parallel_safe = joinrel->consider_parallel &&
3896 rhaas@postgresql.org 2419 [ + + + + : 1137496 : outer_path->parallel_safe && inner_path->parallel_safe;
+ + ]
2420 : : /* This is a foolish way to estimate parallel_workers, but for now... */
1869 peter@eisentraut.org 2421 : 1137496 : pathnode->jpath.path.parallel_workers = outer_path->parallel_workers;
2422 : 1137496 : pathnode->jpath.path.pathkeys = pathkeys;
2423 : 1137496 : pathnode->jpath.jointype = jointype;
2424 : 1137496 : pathnode->jpath.inner_unique = extra->inner_unique;
2425 : 1137496 : pathnode->jpath.outerjoinpath = outer_path;
2426 : 1137496 : pathnode->jpath.innerjoinpath = inner_path;
2427 : 1137496 : pathnode->jpath.joinrestrictinfo = restrict_clauses;
2428 : :
3453 tgl@sss.pgh.pa.us 2429 : 1137496 : final_cost_nestloop(root, pathnode, workspace, extra);
2430 : :
10246 bruce@momjian.us 2431 : 1137496 : return pathnode;
2432 : : }
2433 : :
2434 : : /*
2435 : : * create_mergejoin_path
2436 : : * Creates a pathnode corresponding to a mergejoin join between
2437 : : * two relations
2438 : : *
2439 : : * 'joinrel' is the join relation
2440 : : * 'jointype' is the type of join required
2441 : : * 'workspace' is the result from initial_cost_mergejoin
2442 : : * 'extra' contains various information about the join
2443 : : * 'outer_path' is the outer path
2444 : : * 'inner_path' is the inner path
2445 : : * 'restrict_clauses' are the RestrictInfo nodes to apply at the join
2446 : : * 'pathkeys' are the path keys of the new join path
2447 : : * 'required_outer' is the set of required outer rels
2448 : : * 'mergeclauses' are the RestrictInfo nodes to use as merge clauses
2449 : : * (this should be a subset of the restrict_clauses list)
2450 : : * 'outersortkeys' are the sort varkeys for the outer relation
2451 : : * 'innersortkeys' are the sort varkeys for the inner relation
2452 : : * 'outer_presorted_keys' is the number of presorted keys of the outer path
2453 : : */
2454 : : MergePath *
7777 tgl@sss.pgh.pa.us 2455 : 332837 : create_mergejoin_path(PlannerInfo *root,
2456 : : RelOptInfo *joinrel,
2457 : : JoinType jointype,
2458 : : JoinCostWorkspace *workspace,
2459 : : JoinPathExtraData *extra,
2460 : : Path *outer_path,
2461 : : Path *inner_path,
2462 : : List *restrict_clauses,
2463 : : List *pathkeys,
2464 : : Relids required_outer,
2465 : : List *mergeclauses,
2466 : : List *outersortkeys,
2467 : : List *innersortkeys,
2468 : : int outer_presorted_keys)
2469 : : {
10604 bruce@momjian.us 2470 : 332837 : MergePath *pathnode = makeNode(MergePath);
2471 : :
10605 2472 : 332837 : pathnode->jpath.path.pathtype = T_MergeJoin;
2473 : 332837 : pathnode->jpath.path.parent = joinrel;
3842 tgl@sss.pgh.pa.us 2474 : 332837 : pathnode->jpath.path.pathtarget = joinrel->reltarget;
5267 2475 : 332837 : pathnode->jpath.path.param_info =
2476 : 332837 : get_joinrel_parampathinfo(root,
2477 : : joinrel,
2478 : : outer_path,
2479 : : inner_path,
2480 : : extra->sjinfo,
2481 : : required_outer,
2482 : : &restrict_clauses);
3966 rhaas@postgresql.org 2483 : 332837 : pathnode->jpath.path.parallel_aware = false;
3896 2484 : 972973 : pathnode->jpath.path.parallel_safe = joinrel->consider_parallel &&
2485 [ + + + + : 332837 : outer_path->parallel_safe && inner_path->parallel_safe;
+ + ]
2486 : : /* This is a foolish way to estimate parallel_workers, but for now... */
3755 2487 : 332837 : pathnode->jpath.path.parallel_workers = outer_path->parallel_workers;
5350 tgl@sss.pgh.pa.us 2488 : 332837 : pathnode->jpath.path.pathkeys = pathkeys;
9504 2489 : 332837 : pathnode->jpath.jointype = jointype;
3453 2490 : 332837 : pathnode->jpath.inner_unique = extra->inner_unique;
10605 bruce@momjian.us 2491 : 332837 : pathnode->jpath.outerjoinpath = outer_path;
2492 : 332837 : pathnode->jpath.innerjoinpath = inner_path;
9722 tgl@sss.pgh.pa.us 2493 : 332837 : pathnode->jpath.joinrestrictinfo = restrict_clauses;
10605 bruce@momjian.us 2494 : 332837 : pathnode->path_mergeclauses = mergeclauses;
2495 : 332837 : pathnode->outersortkeys = outersortkeys;
2496 : 332837 : pathnode->innersortkeys = innersortkeys;
500 rguo@postgresql.org 2497 : 332837 : pathnode->outer_presorted_keys = outer_presorted_keys;
2498 : : /* pathnode->skip_mark_restore will be set by final_cost_mergejoin */
2499 : : /* pathnode->materialize_inner will be set by final_cost_mergejoin */
2500 : :
3453 tgl@sss.pgh.pa.us 2501 : 332837 : final_cost_mergejoin(root, pathnode, workspace, extra);
2502 : :
10246 bruce@momjian.us 2503 : 332837 : return pathnode;
2504 : : }
2505 : :
2506 : : /*
2507 : : * create_hashjoin_path
2508 : : * Creates a pathnode corresponding to a hash join between two relations.
2509 : : *
2510 : : * 'joinrel' is the join relation
2511 : : * 'jointype' is the type of join required
2512 : : * 'workspace' is the result from initial_cost_hashjoin
2513 : : * 'extra' contains various information about the join
2514 : : * 'outer_path' is the cheapest outer path
2515 : : * 'inner_path' is the cheapest inner path
2516 : : * 'parallel_hash' to select Parallel Hash of inner path (shared hash table)
2517 : : * 'restrict_clauses' are the RestrictInfo nodes to apply at the join
2518 : : * 'required_outer' is the set of required outer rels
2519 : : * 'hashclauses' are the RestrictInfo nodes to use as hash clauses
2520 : : * (this should be a subset of the restrict_clauses list)
2521 : : */
2522 : : HashPath *
7777 tgl@sss.pgh.pa.us 2523 : 336425 : create_hashjoin_path(PlannerInfo *root,
2524 : : RelOptInfo *joinrel,
2525 : : JoinType jointype,
2526 : : JoinCostWorkspace *workspace,
2527 : : JoinPathExtraData *extra,
2528 : : Path *outer_path,
2529 : : Path *inner_path,
2530 : : bool parallel_hash,
2531 : : List *restrict_clauses,
2532 : : Relids required_outer,
2533 : : List *hashclauses)
2534 : : {
10604 bruce@momjian.us 2535 : 336425 : HashPath *pathnode = makeNode(HashPath);
2536 : :
10605 2537 : 336425 : pathnode->jpath.path.pathtype = T_HashJoin;
2538 : 336425 : pathnode->jpath.path.parent = joinrel;
3842 tgl@sss.pgh.pa.us 2539 : 336425 : pathnode->jpath.path.pathtarget = joinrel->reltarget;
5267 2540 : 336425 : pathnode->jpath.path.param_info =
2541 : 336425 : get_joinrel_parampathinfo(root,
2542 : : joinrel,
2543 : : outer_path,
2544 : : inner_path,
2545 : : extra->sjinfo,
2546 : : required_outer,
2547 : : &restrict_clauses);
3196 andres@anarazel.de 2548 : 336425 : pathnode->jpath.path.parallel_aware =
2549 [ + + + + ]: 336425 : joinrel->consider_parallel && parallel_hash;
3896 rhaas@postgresql.org 2550 : 982012 : pathnode->jpath.path.parallel_safe = joinrel->consider_parallel &&
2551 [ + + + + : 336425 : outer_path->parallel_safe && inner_path->parallel_safe;
+ + ]
2552 : : /* This is a foolish way to estimate parallel_workers, but for now... */
3755 2553 : 336425 : pathnode->jpath.path.parallel_workers = outer_path->parallel_workers;
2554 : :
2555 : : /*
2556 : : * A hashjoin never has pathkeys, since its output ordering is
2557 : : * unpredictable due to possible batching. XXX If the inner relation is
2558 : : * small enough, we could instruct the executor that it must not batch,
2559 : : * and then we could assume that the output inherits the outer relation's
2560 : : * ordering, which might save a sort step. However there is considerable
2561 : : * downside if our estimate of the inner relation size is badly off. For
2562 : : * the moment we don't risk it. (Note also that if we wanted to take this
2563 : : * seriously, joinpath.c would have to consider many more paths for the
2564 : : * outer rel than it does now.)
2565 : : */
9897 tgl@sss.pgh.pa.us 2566 : 336425 : pathnode->jpath.path.pathkeys = NIL;
5350 2567 : 336425 : pathnode->jpath.jointype = jointype;
3453 2568 : 336425 : pathnode->jpath.inner_unique = extra->inner_unique;
5350 2569 : 336425 : pathnode->jpath.outerjoinpath = outer_path;
2570 : 336425 : pathnode->jpath.innerjoinpath = inner_path;
2571 : 336425 : pathnode->jpath.joinrestrictinfo = restrict_clauses;
10605 bruce@momjian.us 2572 : 336425 : pathnode->path_hashclauses = hashclauses;
2573 : : /* final_cost_hashjoin will fill in pathnode->num_batches */
2574 : :
3453 tgl@sss.pgh.pa.us 2575 : 336425 : final_cost_hashjoin(root, pathnode, workspace, extra);
2576 : :
10246 bruce@momjian.us 2577 : 336425 : return pathnode;
2578 : : }
2579 : :
2580 : : /*
2581 : : * create_projection_path
2582 : : * Creates a pathnode that represents performing a projection.
2583 : : *
2584 : : * 'rel' is the parent relation associated with the result
2585 : : * 'subpath' is the path representing the source of data
2586 : : * 'target' is the PathTarget to be computed
2587 : : */
2588 : : ProjectionPath *
3849 tgl@sss.pgh.pa.us 2589 : 302726 : create_projection_path(PlannerInfo *root,
2590 : : RelOptInfo *rel,
2591 : : Path *subpath,
2592 : : PathTarget *target)
2593 : : {
2594 : 302726 : ProjectionPath *pathnode = makeNode(ProjectionPath);
2595 : : PathTarget *oldtarget;
2596 : :
2597 : : /*
2598 : : * We mustn't put a ProjectionPath directly above another; it's useless
2599 : : * and will confuse create_projection_plan. Rather than making sure all
2600 : : * callers handle that, let's implement it here, by stripping off any
2601 : : * ProjectionPath in what we're given. Given this rule, there won't be
2602 : : * more than one.
2603 : : */
1938 2604 [ + + ]: 302726 : if (IsA(subpath, ProjectionPath))
2605 : : {
2606 : 20 : ProjectionPath *subpp = (ProjectionPath *) subpath;
2607 : :
2608 [ - + ]: 20 : Assert(subpp->path.parent == rel);
2609 : 20 : subpath = subpp->subpath;
2610 [ - + ]: 20 : Assert(!IsA(subpath, ProjectionPath));
2611 : : }
2612 : :
3849 2613 : 302726 : pathnode->path.pathtype = T_Result;
2614 : 302726 : pathnode->path.parent = rel;
2615 : 302726 : pathnode->path.pathtarget = target;
397 rguo@postgresql.org 2616 : 302726 : pathnode->path.param_info = subpath->param_info;
3849 tgl@sss.pgh.pa.us 2617 : 302726 : pathnode->path.parallel_aware = false;
2618 : 714236 : pathnode->path.parallel_safe = rel->consider_parallel &&
3733 rhaas@postgresql.org 2619 [ + + + + : 404405 : subpath->parallel_safe &&
+ - ]
3684 tgl@sss.pgh.pa.us 2620 : 101679 : is_parallel_safe(root, (Node *) target->exprs);
3755 rhaas@postgresql.org 2621 : 302726 : pathnode->path.parallel_workers = subpath->parallel_workers;
2622 : : /* Projection does not change the sort order */
3849 tgl@sss.pgh.pa.us 2623 : 302726 : pathnode->path.pathkeys = subpath->pathkeys;
2624 : :
2625 : 302726 : pathnode->subpath = subpath;
2626 : :
2627 : : /*
2628 : : * We might not need a separate Result node. If the input plan node type
2629 : : * can project, we can just tell it to project something else. Or, if it
2630 : : * can't project but the desired target has the same expression list as
2631 : : * what the input will produce anyway, we can still give it the desired
2632 : : * tlist (possibly changing its ressortgroupref labels, but nothing else).
2633 : : * Note: in the latter case, create_projection_plan has to recheck our
2634 : : * conclusion; see comments therein.
2635 : : */
1938 2636 : 302726 : oldtarget = subpath->pathtarget;
3743 2637 [ + + + + ]: 316547 : if (is_projection_capable_path(subpath) ||
2638 : 13821 : equal(oldtarget->exprs, target->exprs))
2639 : : {
2640 : : /* No separate Result node needed */
2641 : 290346 : pathnode->dummypp = true;
2642 : :
2643 : : /*
2644 : : * Set cost of plan as subpath's cost, adjusted for tlist replacement.
2645 : : */
2646 : 290346 : pathnode->path.rows = subpath->rows;
760 rhaas@postgresql.org 2647 : 290346 : pathnode->path.disabled_nodes = subpath->disabled_nodes;
3743 tgl@sss.pgh.pa.us 2648 : 290346 : pathnode->path.startup_cost = subpath->startup_cost +
2649 : 290346 : (target->cost.startup - oldtarget->cost.startup);
2650 : 290346 : pathnode->path.total_cost = subpath->total_cost +
2651 : 290346 : (target->cost.startup - oldtarget->cost.startup) +
2652 : 290346 : (target->cost.per_tuple - oldtarget->cost.per_tuple) * subpath->rows;
2653 : : }
2654 : : else
2655 : : {
2656 : : /* We really do need the Result node */
2657 : 12380 : pathnode->dummypp = false;
2658 : :
2659 : : /*
2660 : : * The Result node's cost is cpu_tuple_cost per row, plus the cost of
2661 : : * evaluating the tlist. There is no qual to worry about.
2662 : : */
2663 : 12380 : pathnode->path.rows = subpath->rows;
760 rhaas@postgresql.org 2664 : 12380 : pathnode->path.disabled_nodes = subpath->disabled_nodes;
3743 tgl@sss.pgh.pa.us 2665 : 12380 : pathnode->path.startup_cost = subpath->startup_cost +
2666 : 12380 : target->cost.startup;
2667 : 12380 : pathnode->path.total_cost = subpath->total_cost +
2668 : 12380 : target->cost.startup +
2669 : 12380 : (cpu_tuple_cost + target->cost.per_tuple) * subpath->rows;
2670 : : }
2671 : :
3849 2672 : 302726 : return pathnode;
2673 : : }
2674 : :
2675 : : /*
2676 : : * apply_projection_to_path
2677 : : * Add a projection step, or just apply the target directly to given path.
2678 : : *
2679 : : * This has the same net effect as create_projection_path(), except that if
2680 : : * a separate Result plan node isn't needed, we just replace the given path's
2681 : : * pathtarget with the desired one. This must be used only when the caller
2682 : : * knows that the given path isn't referenced elsewhere and so can be modified
2683 : : * in-place.
2684 : : *
2685 : : * If the input path is a GatherPath or GatherMergePath, we try to push the
2686 : : * new target down to its input as well; this is a yet more invasive
2687 : : * modification of the input path, which create_projection_path() can't do.
2688 : : *
2689 : : * Note that we mustn't change the source path's parent link; so when it is
2690 : : * add_path'd to "rel" things will be a bit inconsistent. So far that has
2691 : : * not caused any trouble.
2692 : : *
2693 : : * 'rel' is the parent relation associated with the result
2694 : : * 'path' is the path representing the source of data
2695 : : * 'target' is the PathTarget to be computed
2696 : : */
2697 : : Path *
2698 : 11597 : apply_projection_to_path(PlannerInfo *root,
2699 : : RelOptInfo *rel,
2700 : : Path *path,
2701 : : PathTarget *target)
2702 : : {
2703 : : QualCost oldcost;
2704 : :
2705 : : /*
2706 : : * If given path can't project, we might need a Result node, so make a
2707 : : * separate ProjectionPath.
2708 : : */
3743 2709 [ + + ]: 11597 : if (!is_projection_capable_path(path))
3849 2710 : 1086 : return (Path *) create_projection_path(root, rel, path, target);
2711 : :
2712 : : /*
2713 : : * We can just jam the desired tlist into the existing path, being sure to
2714 : : * update its cost estimates appropriately.
2715 : : */
2716 : 10511 : oldcost = path->pathtarget->cost;
2717 : 10511 : path->pathtarget = target;
2718 : :
2719 : 10511 : path->startup_cost += target->cost.startup - oldcost.startup;
2720 : 10511 : path->total_cost += target->cost.startup - oldcost.startup +
2721 : 10511 : (target->cost.per_tuple - oldcost.per_tuple) * path->rows;
2722 : :
2723 : : /*
2724 : : * If the path happens to be a Gather or GatherMerge path, we'd like to
2725 : : * arrange for the subpath to return the required target list so that
2726 : : * workers can help project. But if there is something that is not
2727 : : * parallel-safe in the target expressions, then we can't.
2728 : : */
2318 2729 [ + - + + : 10531 : if ((IsA(path, GatherPath) || IsA(path, GatherMergePath)) &&
+ - ]
3684 2730 : 20 : is_parallel_safe(root, (Node *) target->exprs))
2731 : : {
2732 : : /*
2733 : : * We always use create_projection_path here, even if the subpath is
2734 : : * projection-capable, so as to avoid modifying the subpath in place.
2735 : : * It seems unlikely at present that there could be any other
2736 : : * references to the subpath, but better safe than sorry.
2737 : : *
2738 : : * Note that we don't change the parallel path's cost estimates; it
2739 : : * might be appropriate to do so, to reflect the fact that the bulk of
2740 : : * the target evaluation will happen in workers.
2741 : : */
3233 rhaas@postgresql.org 2742 [ - + ]: 20 : if (IsA(path, GatherPath))
2743 : : {
3233 rhaas@postgresql.org 2744 :UBC 0 : GatherPath *gpath = (GatherPath *) path;
2745 : :
2746 : 0 : gpath->subpath = (Path *)
2747 : 0 : create_projection_path(root,
2748 : 0 : gpath->subpath->parent,
2749 : : gpath->subpath,
2750 : : target);
2751 : : }
2752 : : else
2753 : : {
3233 rhaas@postgresql.org 2754 :CBC 20 : GatherMergePath *gmpath = (GatherMergePath *) path;
2755 : :
2756 : 20 : gmpath->subpath = (Path *)
2757 : 20 : create_projection_path(root,
2758 : 20 : gmpath->subpath->parent,
2759 : : gmpath->subpath,
2760 : : target);
2761 : : }
2762 : : }
3733 2763 [ + + ]: 10491 : else if (path->parallel_safe &&
3684 tgl@sss.pgh.pa.us 2764 [ + + ]: 3680 : !is_parallel_safe(root, (Node *) target->exprs))
2765 : : {
2766 : : /*
2767 : : * We're inserting a parallel-restricted target list into a path
2768 : : * currently marked parallel-safe, so we have to mark it as no longer
2769 : : * safe.
2770 : : */
3733 rhaas@postgresql.org 2771 : 10 : path->parallel_safe = false;
2772 : : }
2773 : :
3849 tgl@sss.pgh.pa.us 2774 : 10511 : return path;
2775 : : }
2776 : :
2777 : : /*
2778 : : * create_set_projection_path
2779 : : * Creates a pathnode that represents performing a projection that
2780 : : * includes set-returning functions.
2781 : : *
2782 : : * 'rel' is the parent relation associated with the result
2783 : : * 'subpath' is the path representing the source of data
2784 : : * 'target' is the PathTarget to be computed
2785 : : */
2786 : : ProjectSetPath *
3532 andres@anarazel.de 2787 : 10251 : create_set_projection_path(PlannerInfo *root,
2788 : : RelOptInfo *rel,
2789 : : Path *subpath,
2790 : : PathTarget *target)
2791 : : {
2792 : 10251 : ProjectSetPath *pathnode = makeNode(ProjectSetPath);
2793 : : double tlist_rows;
2794 : : ListCell *lc;
2795 : :
2796 : 10251 : pathnode->path.pathtype = T_ProjectSet;
2797 : 10251 : pathnode->path.parent = rel;
2798 : 10251 : pathnode->path.pathtarget = target;
2799 : : /* For now, assume we are above any joins, so no parameterization */
2800 : 10251 : pathnode->path.param_info = NULL;
2801 : 10251 : pathnode->path.parallel_aware = false;
2802 : 24049 : pathnode->path.parallel_safe = rel->consider_parallel &&
2803 [ + + + + : 13769 : subpath->parallel_safe &&
+ - ]
2804 : 3518 : is_parallel_safe(root, (Node *) target->exprs);
2805 : 10251 : pathnode->path.parallel_workers = subpath->parallel_workers;
2806 : : /* Projection does not change the sort order XXX? */
2807 : 10251 : pathnode->path.pathkeys = subpath->pathkeys;
2808 : :
2809 : 10251 : pathnode->subpath = subpath;
2810 : :
2811 : : /*
2812 : : * Estimate number of rows produced by SRFs for each row of input; if
2813 : : * there's more than one in this node, use the maximum.
2814 : : */
2815 : 10251 : tlist_rows = 1;
2816 [ + - + + : 22339 : foreach(lc, target->exprs)
+ + ]
2817 : : {
2818 : 12088 : Node *node = (Node *) lfirst(lc);
2819 : : double itemrows;
2820 : :
2780 tgl@sss.pgh.pa.us 2821 : 12088 : itemrows = expression_returns_set_rows(root, node);
3532 andres@anarazel.de 2822 [ + + ]: 12088 : if (tlist_rows < itemrows)
2823 : 9873 : tlist_rows = itemrows;
2824 : : }
2825 : :
2826 : : /*
2827 : : * In addition to the cost of evaluating the tlist, charge cpu_tuple_cost
2828 : : * per input row, and half of cpu_tuple_cost for each added output row.
2829 : : * This is slightly bizarre maybe, but it's what 9.6 did; we may revisit
2830 : : * this estimate later.
2831 : : */
760 rhaas@postgresql.org 2832 : 10251 : pathnode->path.disabled_nodes = subpath->disabled_nodes;
3532 andres@anarazel.de 2833 : 10251 : pathnode->path.rows = subpath->rows * tlist_rows;
2834 : 10251 : pathnode->path.startup_cost = subpath->startup_cost +
2835 : 10251 : target->cost.startup;
2836 : 10251 : pathnode->path.total_cost = subpath->total_cost +
2837 : 10251 : target->cost.startup +
2838 : 10251 : (cpu_tuple_cost + target->cost.per_tuple) * subpath->rows +
2839 : 10251 : (pathnode->path.rows - subpath->rows) * cpu_tuple_cost / 2;
2840 : :
2841 : 10251 : return pathnode;
2842 : : }
2843 : :
2844 : : /*
2845 : : * create_incremental_sort_path
2846 : : * Creates a pathnode that represents performing an incremental sort.
2847 : : *
2848 : : * 'rel' is the parent relation associated with the result
2849 : : * 'subpath' is the path representing the source of data
2850 : : * 'pathkeys' represents the desired sort order
2851 : : * 'presorted_keys' is the number of keys by which the input path is
2852 : : * already sorted
2853 : : * 'limit_tuples' is the estimated bound on the number of output tuples,
2854 : : * or -1 if no LIMIT or couldn't estimate
2855 : : */
2856 : : IncrementalSortPath *
2358 tomas.vondra@postgre 2857 : 7883 : create_incremental_sort_path(PlannerInfo *root,
2858 : : RelOptInfo *rel,
2859 : : Path *subpath,
2860 : : List *pathkeys,
2861 : : int presorted_keys,
2862 : : double limit_tuples)
2863 : : {
2864 : 7883 : IncrementalSortPath *sort = makeNode(IncrementalSortPath);
2865 : 7883 : SortPath *pathnode = &sort->spath;
2866 : :
2867 : 7883 : pathnode->path.pathtype = T_IncrementalSort;
2868 : 7883 : pathnode->path.parent = rel;
2869 : : /* Sort doesn't project, so use source path's pathtarget */
2870 : 7883 : pathnode->path.pathtarget = subpath->pathtarget;
397 rguo@postgresql.org 2871 : 7883 : pathnode->path.param_info = subpath->param_info;
2358 tomas.vondra@postgre 2872 : 7883 : pathnode->path.parallel_aware = false;
2873 [ + + ]: 11890 : pathnode->path.parallel_safe = rel->consider_parallel &&
2874 [ + + ]: 4007 : subpath->parallel_safe;
2875 : 7883 : pathnode->path.parallel_workers = subpath->parallel_workers;
2876 : 7883 : pathnode->path.pathkeys = pathkeys;
2877 : :
2878 : 7883 : pathnode->subpath = subpath;
2879 : :
2880 : 7883 : cost_incremental_sort(&pathnode->path,
2881 : : root, pathkeys, presorted_keys,
2882 : : subpath->disabled_nodes,
2883 : : subpath->startup_cost,
2884 : : subpath->total_cost,
2885 : : subpath->rows,
2886 : 7883 : subpath->pathtarget->width,
2887 : : 0.0, /* XXX comparison_cost shouldn't be 0? */
2888 : : work_mem, limit_tuples,
2889 : : &sort->numGroups);
2890 : :
2891 : 7883 : sort->nPresortedCols = presorted_keys;
2892 : :
2120 tgl@sss.pgh.pa.us 2893 : 7883 : return sort;
2894 : : }
2895 : :
2896 : : /*
2897 : : * create_sort_path
2898 : : * Creates a pathnode that represents performing an explicit sort.
2899 : : *
2900 : : * 'rel' is the parent relation associated with the result
2901 : : * 'subpath' is the path representing the source of data
2902 : : * 'pathkeys' represents the desired sort order
2903 : : * 'limit_tuples' is the estimated bound on the number of output tuples,
2904 : : * or -1 if no LIMIT or couldn't estimate
2905 : : */
2906 : : SortPath *
3849 2907 : 93041 : create_sort_path(PlannerInfo *root,
2908 : : RelOptInfo *rel,
2909 : : Path *subpath,
2910 : : List *pathkeys,
2911 : : double limit_tuples)
2912 : : {
2913 : 93041 : SortPath *pathnode = makeNode(SortPath);
2914 : :
2915 : 93041 : pathnode->path.pathtype = T_Sort;
2916 : 93041 : pathnode->path.parent = rel;
2917 : : /* Sort doesn't project, so use source path's pathtarget */
2918 : 93041 : pathnode->path.pathtarget = subpath->pathtarget;
397 rguo@postgresql.org 2919 : 93041 : pathnode->path.param_info = subpath->param_info;
3849 tgl@sss.pgh.pa.us 2920 : 93041 : pathnode->path.parallel_aware = false;
2921 [ + + ]: 163409 : pathnode->path.parallel_safe = rel->consider_parallel &&
2922 [ + + ]: 70368 : subpath->parallel_safe;
3755 rhaas@postgresql.org 2923 : 93041 : pathnode->path.parallel_workers = subpath->parallel_workers;
3849 tgl@sss.pgh.pa.us 2924 : 93041 : pathnode->path.pathkeys = pathkeys;
2925 : :
2926 : 93041 : pathnode->subpath = subpath;
2927 : :
2928 : 93041 : cost_sort(&pathnode->path, root, pathkeys,
2929 : : subpath->disabled_nodes,
2930 : : subpath->total_cost,
2931 : : subpath->rows,
2932 : 93041 : subpath->pathtarget->width,
2933 : : 0.0, /* XXX comparison_cost shouldn't be 0? */
2934 : : work_mem, limit_tuples);
2935 : :
2936 : 93041 : return pathnode;
2937 : : }
2938 : :
2939 : : /*
2940 : : * create_group_path
2941 : : * Creates a pathnode that represents performing grouping of presorted input
2942 : : *
2943 : : * 'rel' is the parent relation associated with the result
2944 : : * 'subpath' is the path representing the source of data
2945 : : * 'target' is the PathTarget to be computed
2946 : : * 'groupClause' is a list of SortGroupClause's representing the grouping
2947 : : * 'qual' is the HAVING quals if any
2948 : : * 'numGroups' is the estimated number of groups
2949 : : */
2950 : : GroupPath *
2951 : 1043 : create_group_path(PlannerInfo *root,
2952 : : RelOptInfo *rel,
2953 : : Path *subpath,
2954 : : List *groupClause,
2955 : : List *qual,
2956 : : double numGroups)
2957 : : {
2958 : 1043 : GroupPath *pathnode = makeNode(GroupPath);
3106 rhaas@postgresql.org 2959 : 1043 : PathTarget *target = rel->reltarget;
2960 : :
3849 tgl@sss.pgh.pa.us 2961 : 1043 : pathnode->path.pathtype = T_Group;
2962 : 1043 : pathnode->path.parent = rel;
2963 : 1043 : pathnode->path.pathtarget = target;
2964 : : /* For now, assume we are above any joins, so no parameterization */
2965 : 1043 : pathnode->path.param_info = NULL;
2966 : 1043 : pathnode->path.parallel_aware = false;
2967 [ + + ]: 1677 : pathnode->path.parallel_safe = rel->consider_parallel &&
2968 [ + + ]: 634 : subpath->parallel_safe;
3755 rhaas@postgresql.org 2969 : 1043 : pathnode->path.parallel_workers = subpath->parallel_workers;
2970 : : /* Group doesn't change sort ordering */
3849 tgl@sss.pgh.pa.us 2971 : 1043 : pathnode->path.pathkeys = subpath->pathkeys;
2972 : :
2973 : 1043 : pathnode->subpath = subpath;
2974 : :
2975 : 1043 : pathnode->groupClause = groupClause;
2976 : 1043 : pathnode->qual = qual;
2977 : :
2978 : 1043 : cost_group(&pathnode->path, root,
2979 : : list_length(groupClause),
2980 : : numGroups,
2981 : : qual,
2982 : : subpath->disabled_nodes,
2983 : : subpath->startup_cost, subpath->total_cost,
2984 : : subpath->rows);
2985 : :
2986 : : /* add tlist eval cost for each output row */
2987 : 1043 : pathnode->path.startup_cost += target->cost.startup;
2988 : 1043 : pathnode->path.total_cost += target->cost.startup +
2989 : 1043 : target->cost.per_tuple * pathnode->path.rows;
2990 : :
2991 : 1043 : return pathnode;
2992 : : }
2993 : :
2994 : : /*
2995 : : * create_unique_path
2996 : : * Creates a pathnode that represents performing an explicit Unique step
2997 : : * on presorted input.
2998 : : *
2999 : : * 'rel' is the parent relation associated with the result
3000 : : * 'subpath' is the path representing the source of data
3001 : : * 'numCols' is the number of grouping columns
3002 : : * 'numGroups' is the estimated number of groups
3003 : : *
3004 : : * The input path must be sorted on the grouping columns, plus possibly
3005 : : * additional columns; so the first numCols pathkeys are the grouping columns
3006 : : */
3007 : : UniquePath *
397 rguo@postgresql.org 3008 : 17834 : create_unique_path(PlannerInfo *root,
3009 : : RelOptInfo *rel,
3010 : : Path *subpath,
3011 : : int numCols,
3012 : : double numGroups)
3013 : : {
3014 : 17834 : UniquePath *pathnode = makeNode(UniquePath);
3015 : :
3849 tgl@sss.pgh.pa.us 3016 : 17834 : pathnode->path.pathtype = T_Unique;
3017 : 17834 : pathnode->path.parent = rel;
3018 : : /* Unique doesn't project, so use source path's pathtarget */
3019 : 17834 : pathnode->path.pathtarget = subpath->pathtarget;
397 rguo@postgresql.org 3020 : 17834 : pathnode->path.param_info = subpath->param_info;
3849 tgl@sss.pgh.pa.us 3021 : 17834 : pathnode->path.parallel_aware = false;
3022 [ + + ]: 32185 : pathnode->path.parallel_safe = rel->consider_parallel &&
3023 [ + + ]: 14351 : subpath->parallel_safe;
3755 rhaas@postgresql.org 3024 : 17834 : pathnode->path.parallel_workers = subpath->parallel_workers;
3025 : : /* Unique doesn't change the input ordering */
3849 tgl@sss.pgh.pa.us 3026 : 17834 : pathnode->path.pathkeys = subpath->pathkeys;
3027 : :
3028 : 17834 : pathnode->subpath = subpath;
3029 : 17834 : pathnode->numkeys = numCols;
3030 : :
3031 : : /*
3032 : : * Charge one cpu_operator_cost per comparison per input tuple. We assume
3033 : : * all columns get compared at most of the tuples. (XXX probably this is
3034 : : * an overestimate.)
3035 : : */
760 rhaas@postgresql.org 3036 : 17834 : pathnode->path.disabled_nodes = subpath->disabled_nodes;
3849 tgl@sss.pgh.pa.us 3037 : 17834 : pathnode->path.startup_cost = subpath->startup_cost;
3038 : 17834 : pathnode->path.total_cost = subpath->total_cost +
3039 : 17834 : cpu_operator_cost * subpath->rows * numCols;
3040 : 17834 : pathnode->path.rows = numGroups;
3041 : :
3042 : : /*
3043 : : * Mark the path as disabled if enable_groupagg is off. While this isn't
3044 : : * a grouping Agg node, it is the sort-based way of removing duplicates
3045 : : * and so is the natural counterpart to the AGG_HASHED path that
3046 : : * enable_hashagg controls; it seems close enough to justify letting that
3047 : : * switch control it.
3048 : : */
73 rguo@postgresql.org 3049 [ + + ]:GNC 17834 : if (!enable_groupagg)
3050 : 52 : pathnode->path.disabled_nodes++;
3051 : :
3849 tgl@sss.pgh.pa.us 3052 :CBC 17834 : return pathnode;
3053 : : }
3054 : :
3055 : : /*
3056 : : * create_agg_path
3057 : : * Creates a pathnode that represents performing aggregation/grouping
3058 : : *
3059 : : * 'rel' is the parent relation associated with the result
3060 : : * 'subpath' is the path representing the source of data
3061 : : * 'target' is the PathTarget to be computed
3062 : : * 'aggstrategy' is the Agg node's basic implementation strategy
3063 : : * 'aggsplit' is the Agg node's aggregate-splitting mode
3064 : : * 'groupClause' is a list of SortGroupClause's representing the grouping
3065 : : * 'qual' is the HAVING quals if any
3066 : : * 'aggcosts' contains cost info about the aggregate functions to be computed
3067 : : * 'numGroups' is the estimated number of groups (1 if not grouping)
3068 : : */
3069 : : AggPath *
3070 : 69375 : create_agg_path(PlannerInfo *root,
3071 : : RelOptInfo *rel,
3072 : : Path *subpath,
3073 : : PathTarget *target,
3074 : : AggStrategy aggstrategy,
3075 : : AggSplit aggsplit,
3076 : : List *groupClause,
3077 : : List *qual,
3078 : : const AggClauseCosts *aggcosts,
3079 : : double numGroups)
3080 : : {
3081 : 69375 : AggPath *pathnode = makeNode(AggPath);
3082 : :
3083 : 69375 : pathnode->path.pathtype = T_Agg;
3084 : 69375 : pathnode->path.parent = rel;
3085 : 69375 : pathnode->path.pathtarget = target;
397 rguo@postgresql.org 3086 : 69375 : pathnode->path.param_info = subpath->param_info;
3849 tgl@sss.pgh.pa.us 3087 : 69375 : pathnode->path.parallel_aware = false;
3088 [ + + ]: 119908 : pathnode->path.parallel_safe = rel->consider_parallel &&
3089 [ + + ]: 50533 : subpath->parallel_safe;
3755 rhaas@postgresql.org 3090 : 69375 : pathnode->path.parallel_workers = subpath->parallel_workers;
3091 : :
3849 tgl@sss.pgh.pa.us 3092 [ + + ]: 69375 : if (aggstrategy == AGG_SORTED)
3093 : : {
3094 : : /*
3095 : : * Attempt to preserve the order of the subpath. Additional pathkeys
3096 : : * may have been added in adjust_group_pathkeys_for_groupagg() to
3097 : : * support ORDER BY / DISTINCT aggregates. Pathkeys added there
3098 : : * belong to columns within the aggregate function, so we must strip
3099 : : * these additional pathkeys off as those columns are unavailable
3100 : : * above the aggregate node.
3101 : : */
1077 drowley@postgresql.o 3102 [ + + ]: 11959 : if (list_length(subpath->pathkeys) > root->num_groupby_pathkeys)
3103 : 646 : pathnode->path.pathkeys = list_copy_head(subpath->pathkeys,
3104 : : root->num_groupby_pathkeys);
3105 : : else
3106 : 11313 : pathnode->path.pathkeys = subpath->pathkeys; /* preserves order */
3107 : : }
3108 : : else
3849 tgl@sss.pgh.pa.us 3109 : 57416 : pathnode->path.pathkeys = NIL; /* output is unordered */
3110 : :
3111 : 69375 : pathnode->subpath = subpath;
3112 : :
3113 : 69375 : pathnode->aggstrategy = aggstrategy;
3738 3114 : 69375 : pathnode->aggsplit = aggsplit;
3849 3115 : 69375 : pathnode->numGroups = numGroups;
2397 jdavis@postgresql.or 3116 [ + + ]: 69375 : pathnode->transitionSpace = aggcosts ? aggcosts->transitionSpace : 0;
3849 tgl@sss.pgh.pa.us 3117 : 69375 : pathnode->groupClause = groupClause;
3118 : 69375 : pathnode->qual = qual;
3119 : :
3120 : 69375 : cost_agg(&pathnode->path, root,
3121 : : aggstrategy, aggcosts,
3122 : : list_length(groupClause), numGroups,
3123 : : qual,
3124 : : subpath->disabled_nodes,
3125 : : subpath->startup_cost, subpath->total_cost,
2377 jdavis@postgresql.or 3126 : 69375 : subpath->rows, subpath->pathtarget->width);
3127 : :
3128 : : /* add tlist eval cost for each output row */
3849 tgl@sss.pgh.pa.us 3129 : 69375 : pathnode->path.startup_cost += target->cost.startup;
3130 : 69375 : pathnode->path.total_cost += target->cost.startup +
3131 : 69375 : target->cost.per_tuple * pathnode->path.rows;
3132 : :
3133 : 69375 : return pathnode;
3134 : : }
3135 : :
3136 : : /*
3137 : : * create_groupingsets_path
3138 : : * Creates a pathnode that represents performing GROUPING SETS aggregation
3139 : : *
3140 : : * GroupingSetsPath represents sorted grouping with one or more grouping sets.
3141 : : * The input path's result must be sorted to match the last entry in
3142 : : * rollup_groupclauses.
3143 : : *
3144 : : * 'rel' is the parent relation associated with the result
3145 : : * 'subpath' is the path representing the source of data
3146 : : * 'target' is the PathTarget to be computed
3147 : : * 'having_qual' is the HAVING quals if any
3148 : : * 'rollups' is a list of RollupData nodes
3149 : : * 'agg_costs' contains cost info about the aggregate functions to be computed
3150 : : */
3151 : : GroupingSetsPath *
3152 : 2239 : create_groupingsets_path(PlannerInfo *root,
3153 : : RelOptInfo *rel,
3154 : : Path *subpath,
3155 : : List *having_qual,
3156 : : AggStrategy aggstrategy,
3157 : : List *rollups,
3158 : : const AggClauseCosts *agg_costs)
3159 : : {
3160 : 2239 : GroupingSetsPath *pathnode = makeNode(GroupingSetsPath);
3106 rhaas@postgresql.org 3161 : 2239 : PathTarget *target = rel->reltarget;
3162 : : ListCell *lc;
3464 rhodiumtoad@postgres 3163 : 2239 : bool is_first = true;
3164 : 2239 : bool is_first_sort = true;
3165 : :
3166 : : /* The topmost generated Plan node will be an Agg */
3849 tgl@sss.pgh.pa.us 3167 : 2239 : pathnode->path.pathtype = T_Agg;
3168 : 2239 : pathnode->path.parent = rel;
3169 : 2239 : pathnode->path.pathtarget = target;
3170 : 2239 : pathnode->path.param_info = subpath->param_info;
3171 : 2239 : pathnode->path.parallel_aware = false;
3172 [ + + ]: 3420 : pathnode->path.parallel_safe = rel->consider_parallel &&
3173 [ + + ]: 1181 : subpath->parallel_safe;
3755 rhaas@postgresql.org 3174 : 2239 : pathnode->path.parallel_workers = subpath->parallel_workers;
3849 tgl@sss.pgh.pa.us 3175 : 2239 : pathnode->subpath = subpath;
3176 : :
3177 : : /*
3178 : : * Simplify callers by downgrading AGG_SORTED to AGG_PLAIN, and AGG_MIXED
3179 : : * to AGG_HASHED, here if possible.
3180 : : */
3464 rhodiumtoad@postgres 3181 [ + + + + ]: 3185 : if (aggstrategy == AGG_SORTED &&
3182 : 946 : list_length(rollups) == 1 &&
3183 [ + + ]: 440 : ((RollupData *) linitial(rollups))->groupClause == NIL)
3184 : 45 : aggstrategy = AGG_PLAIN;
3185 : :
3186 [ + + - + ]: 3205 : if (aggstrategy == AGG_MIXED &&
3187 : 966 : list_length(rollups) == 1)
3464 rhodiumtoad@postgres 3188 :UBC 0 : aggstrategy = AGG_HASHED;
3189 : :
3190 : : /*
3191 : : * Output will be in sorted order by group_pathkeys if, and only if, there
3192 : : * is a single rollup operation on a non-empty list of grouping
3193 : : * expressions.
3194 : : */
3464 rhodiumtoad@postgres 3195 [ + + + + ]:CBC 2239 : if (aggstrategy == AGG_SORTED && list_length(rollups) == 1)
3849 tgl@sss.pgh.pa.us 3196 : 395 : pathnode->path.pathkeys = root->group_pathkeys;
3197 : : else
3198 : 1844 : pathnode->path.pathkeys = NIL;
3199 : :
3464 rhodiumtoad@postgres 3200 : 2239 : pathnode->aggstrategy = aggstrategy;
3201 : 2239 : pathnode->rollups = rollups;
3849 tgl@sss.pgh.pa.us 3202 : 2239 : pathnode->qual = having_qual;
2397 jdavis@postgresql.or 3203 [ + - ]: 2239 : pathnode->transitionSpace = agg_costs ? agg_costs->transitionSpace : 0;
3204 : :
3464 rhodiumtoad@postgres 3205 [ - + ]: 2239 : Assert(rollups != NIL);
3206 [ + + - + ]: 2239 : Assert(aggstrategy != AGG_PLAIN || list_length(rollups) == 1);
3207 [ + + - + ]: 2239 : Assert(aggstrategy != AGG_MIXED || list_length(rollups) > 1);
3208 : :
3209 [ + - + + : 7550 : foreach(lc, rollups)
+ + ]
3210 : : {
3211 : 5311 : RollupData *rollup = lfirst(lc);
3212 : 5311 : List *gsets = rollup->gsets;
3213 : 5311 : int numGroupCols = list_length(linitial(gsets));
3214 : :
3215 : : /*
3216 : : * In AGG_SORTED or AGG_PLAIN mode, the first rollup takes the
3217 : : * (already-sorted) input, and following ones do their own sort.
3218 : : *
3219 : : * In AGG_HASHED mode, there is one rollup for each grouping set.
3220 : : *
3221 : : * In AGG_MIXED mode, the first rollups are hashed, the first
3222 : : * non-hashed one takes the (already-sorted) input, and following ones
3223 : : * do their own sort.
3224 : : */
3225 [ + + ]: 5311 : if (is_first)
3226 : : {
3227 : 2239 : cost_agg(&pathnode->path, root,
3228 : : aggstrategy,
3229 : : agg_costs,
3230 : : numGroupCols,
3231 : : rollup->numGroups,
3232 : : having_qual,
3233 : : subpath->disabled_nodes,
3234 : : subpath->startup_cost,
3235 : : subpath->total_cost,
3236 : : subpath->rows,
2377 jdavis@postgresql.or 3237 : 2239 : subpath->pathtarget->width);
3464 rhodiumtoad@postgres 3238 : 2239 : is_first = false;
3239 [ + + ]: 2239 : if (!rollup->is_hashed)
3240 : 946 : is_first_sort = false;
3241 : : }
3242 : : else
3243 : : {
3244 : : Path sort_path; /* dummy for result of cost_sort */
3245 : : Path agg_path; /* dummy for result of cost_agg */
3246 : :
3247 [ + + + + ]: 3072 : if (rollup->is_hashed || is_first_sort)
3248 : : {
3249 : : /*
3250 : : * Account for cost of aggregation, but don't charge input
3251 : : * cost again
3252 : : */
3253 : 2316 : cost_agg(&agg_path, root,
3254 : 2316 : rollup->is_hashed ? AGG_HASHED : AGG_SORTED,
3255 : : agg_costs,
3256 : : numGroupCols,
3257 : : rollup->numGroups,
3258 : : having_qual,
3259 : : 0, 0.0, 0.0,
3260 : : subpath->rows,
2377 jdavis@postgresql.or 3261 [ + + ]: 2316 : subpath->pathtarget->width);
3464 rhodiumtoad@postgres 3262 [ + + ]: 2316 : if (!rollup->is_hashed)
3263 : 966 : is_first_sort = false;
3264 : : }
3265 : : else
3266 : : {
3267 : : /* Account for cost of sort, but don't charge input cost again */
760 rhaas@postgresql.org 3268 : 756 : cost_sort(&sort_path, root, NIL, 0,
3269 : : 0.0,
3270 : : subpath->rows,
3464 rhodiumtoad@postgres 3271 : 756 : subpath->pathtarget->width,
3272 : : 0.0,
3273 : : work_mem,
3274 : : -1.0);
3275 : :
3276 : : /* Account for cost of aggregation */
3277 : :
3278 : 756 : cost_agg(&agg_path, root,
3279 : : AGG_SORTED,
3280 : : agg_costs,
3281 : : numGroupCols,
3282 : : rollup->numGroups,
3283 : : having_qual,
3284 : : sort_path.disabled_nodes,
3285 : : sort_path.startup_cost,
3286 : : sort_path.total_cost,
3287 : : sort_path.rows,
2377 jdavis@postgresql.or 3288 : 756 : subpath->pathtarget->width);
3289 : : }
3290 : :
760 rhaas@postgresql.org 3291 : 3072 : pathnode->path.disabled_nodes += agg_path.disabled_nodes;
3849 tgl@sss.pgh.pa.us 3292 : 3072 : pathnode->path.total_cost += agg_path.total_cost;
3293 : 3072 : pathnode->path.rows += agg_path.rows;
3294 : : }
3295 : : }
3296 : :
3297 : : /* add tlist eval cost for each output row */
3298 : 2239 : pathnode->path.startup_cost += target->cost.startup;
3299 : 2239 : pathnode->path.total_cost += target->cost.startup +
3300 : 2239 : target->cost.per_tuple * pathnode->path.rows;
3301 : :
3302 : 2239 : return pathnode;
3303 : : }
3304 : :
3305 : : /*
3306 : : * create_minmaxagg_path
3307 : : * Creates a pathnode that represents computation of MIN/MAX aggregates
3308 : : *
3309 : : * 'rel' is the parent relation associated with the result
3310 : : * 'target' is the PathTarget to be computed
3311 : : * 'mmaggregates' is a list of MinMaxAggInfo structs
3312 : : * 'quals' is the HAVING quals if any
3313 : : */
3314 : : MinMaxAggPath *
3315 : 331 : create_minmaxagg_path(PlannerInfo *root,
3316 : : RelOptInfo *rel,
3317 : : PathTarget *target,
3318 : : List *mmaggregates,
3319 : : List *quals)
3320 : : {
3321 : 331 : MinMaxAggPath *pathnode = makeNode(MinMaxAggPath);
3322 : : Cost initplan_cost;
760 rhaas@postgresql.org 3323 : 331 : int initplan_disabled_nodes = 0;
3324 : : ListCell *lc;
3325 : :
3326 : : /* The topmost generated Plan node will be a Result */
3849 tgl@sss.pgh.pa.us 3327 : 331 : pathnode->path.pathtype = T_Result;
3328 : 331 : pathnode->path.parent = rel;
3329 : 331 : pathnode->path.pathtarget = target;
3330 : : /* For now, assume we are above any joins, so no parameterization */
3331 : 331 : pathnode->path.param_info = NULL;
3332 : 331 : pathnode->path.parallel_aware = false;
1165 3333 : 331 : pathnode->path.parallel_safe = true; /* might change below */
3755 rhaas@postgresql.org 3334 : 331 : pathnode->path.parallel_workers = 0;
3335 : : /* Result is one unordered row */
3849 tgl@sss.pgh.pa.us 3336 : 331 : pathnode->path.rows = 1;
3337 : 331 : pathnode->path.pathkeys = NIL;
3338 : :
3339 : 331 : pathnode->mmaggregates = mmaggregates;
3340 : 331 : pathnode->quals = quals;
3341 : :
3342 : : /* Calculate cost of all the initplans, and check parallel safety */
3343 : 331 : initplan_cost = 0;
3344 [ + - + + : 696 : foreach(lc, mmaggregates)
+ + ]
3345 : : {
3346 : 365 : MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
3347 : :
760 rhaas@postgresql.org 3348 : 365 : initplan_disabled_nodes += mminfo->path->disabled_nodes;
3849 tgl@sss.pgh.pa.us 3349 : 365 : initplan_cost += mminfo->pathcost;
1165 3350 [ + + ]: 365 : if (!mminfo->path->parallel_safe)
3351 : 83 : pathnode->path.parallel_safe = false;
3352 : : }
3353 : :
3354 : : /* add tlist eval cost for each output row, plus cpu_tuple_cost */
760 rhaas@postgresql.org 3355 : 331 : pathnode->path.disabled_nodes = initplan_disabled_nodes;
3849 tgl@sss.pgh.pa.us 3356 : 331 : pathnode->path.startup_cost = initplan_cost + target->cost.startup;
3357 : 331 : pathnode->path.total_cost = initplan_cost + target->cost.startup +
3358 : 331 : target->cost.per_tuple + cpu_tuple_cost;
3359 : :
3360 : : /*
3361 : : * Add cost of qual, if any --- but we ignore its selectivity, since our
3362 : : * rowcount estimate should be 1 no matter what the qual is.
3363 : : */
3244 3364 [ - + ]: 331 : if (quals)
3365 : : {
3366 : : QualCost qual_cost;
3367 : :
3244 tgl@sss.pgh.pa.us 3368 :UBC 0 : cost_qual_eval(&qual_cost, quals, root);
3369 : 0 : pathnode->path.startup_cost += qual_cost.startup;
3370 : 0 : pathnode->path.total_cost += qual_cost.startup + qual_cost.per_tuple;
3371 : : }
3372 : :
3373 : : /*
3374 : : * If the initplans were all parallel-safe, also check safety of the
3375 : : * target and quals. (The Result node itself isn't parallelizable, but if
3376 : : * we are in a subquery then it can be useful for the outer query to know
3377 : : * that this one is parallel-safe.)
3378 : : */
1165 tgl@sss.pgh.pa.us 3379 [ + + ]:CBC 331 : if (pathnode->path.parallel_safe)
3380 : 252 : pathnode->path.parallel_safe =
3381 [ + - + - ]: 504 : is_parallel_safe(root, (Node *) target->exprs) &&
3382 : 504 : is_parallel_safe(root, (Node *) quals);
3383 : :
3849 3384 : 331 : return pathnode;
3385 : : }
3386 : :
3387 : : /*
3388 : : * create_windowagg_path
3389 : : * Creates a pathnode that represents computation of window functions
3390 : : *
3391 : : * 'rel' is the parent relation associated with the result
3392 : : * 'subpath' is the path representing the source of data
3393 : : * 'target' is the PathTarget to be computed
3394 : : * 'windowFuncs' is a list of WindowFunc structs
3395 : : * 'runCondition' is a list of OpExprs to short-circuit WindowAgg execution
3396 : : * 'winclause' is a WindowClause that is common to all the WindowFuncs
3397 : : * 'qual' WindowClause.runconditions from lower-level WindowAggPaths.
3398 : : * Must always be NIL when topwindow == false
3399 : : * 'topwindow' pass as true only for the top-level WindowAgg. False for all
3400 : : * intermediate WindowAggs.
3401 : : *
3402 : : * The input must be sorted according to the WindowClause's PARTITION keys
3403 : : * plus ORDER BY keys.
3404 : : */
3405 : : WindowAggPath *
3406 : 2654 : create_windowagg_path(PlannerInfo *root,
3407 : : RelOptInfo *rel,
3408 : : Path *subpath,
3409 : : PathTarget *target,
3410 : : List *windowFuncs,
3411 : : List *runCondition,
3412 : : WindowClause *winclause,
3413 : : List *qual,
3414 : : bool topwindow)
3415 : : {
3416 : 2654 : WindowAggPath *pathnode = makeNode(WindowAggPath);
3417 : :
3418 : : /* qual can only be set for the topwindow */
1626 drowley@postgresql.o 3419 [ + + - + ]: 2654 : Assert(qual == NIL || topwindow);
3420 : :
3849 tgl@sss.pgh.pa.us 3421 : 2654 : pathnode->path.pathtype = T_WindowAgg;
3422 : 2654 : pathnode->path.parent = rel;
3423 : 2654 : pathnode->path.pathtarget = target;
3424 : : /* For now, assume we are above any joins, so no parameterization */
3425 : 2654 : pathnode->path.param_info = NULL;
3426 : 2654 : pathnode->path.parallel_aware = false;
3427 [ - + ]: 2654 : pathnode->path.parallel_safe = rel->consider_parallel &&
3849 tgl@sss.pgh.pa.us 3428 [ # # ]:UBC 0 : subpath->parallel_safe;
3755 rhaas@postgresql.org 3429 :CBC 2654 : pathnode->path.parallel_workers = subpath->parallel_workers;
3430 : : /* WindowAgg preserves the input sort order */
3849 tgl@sss.pgh.pa.us 3431 : 2654 : pathnode->path.pathkeys = subpath->pathkeys;
3432 : :
3433 : 2654 : pathnode->subpath = subpath;
3434 : 2654 : pathnode->winclause = winclause;
1626 drowley@postgresql.o 3435 : 2654 : pathnode->qual = qual;
868 3436 : 2654 : pathnode->runCondition = runCondition;
1626 3437 : 2654 : pathnode->topwindow = topwindow;
3438 : :
3439 : : /*
3440 : : * For costing purposes, assume that there are no redundant partitioning
3441 : : * or ordering columns; it's not worth the trouble to deal with that
3442 : : * corner case here. So we just pass the unmodified list lengths to
3443 : : * cost_windowagg.
3444 : : */
3849 tgl@sss.pgh.pa.us 3445 : 2654 : cost_windowagg(&pathnode->path, root,
3446 : : windowFuncs,
3447 : : winclause,
3448 : : subpath->disabled_nodes,
3449 : : subpath->startup_cost,
3450 : : subpath->total_cost,
3451 : : subpath->rows);
3452 : :
3453 : : /* add tlist eval cost for each output row */
3454 : 2654 : pathnode->path.startup_cost += target->cost.startup;
3455 : 2654 : pathnode->path.total_cost += target->cost.startup +
3456 : 2654 : target->cost.per_tuple * pathnode->path.rows;
3457 : :
3458 : 2654 : return pathnode;
3459 : : }
3460 : :
3461 : : /*
3462 : : * create_setop_path
3463 : : * Creates a pathnode that represents computation of INTERSECT or EXCEPT
3464 : : *
3465 : : * 'rel' is the parent relation associated with the result
3466 : : * 'leftpath' is the path representing the left-hand source of data
3467 : : * 'rightpath' is the path representing the right-hand source of data
3468 : : * 'cmd' is the specific semantics (INTERSECT or EXCEPT, with/without ALL)
3469 : : * 'strategy' is the implementation strategy (sorted or hashed)
3470 : : * 'groupList' is a list of SortGroupClause's representing the grouping
3471 : : * 'numGroups' is the estimated number of distinct groups in left-hand input
3472 : : * 'outputRows' is the estimated number of output rows
3473 : : *
3474 : : * leftpath and rightpath must produce the same columns. Moreover, if
3475 : : * strategy is SETOP_SORTED, leftpath and rightpath must both be sorted
3476 : : * by all the grouping columns.
3477 : : */
3478 : : SetOpPath *
3479 : 1224 : create_setop_path(PlannerInfo *root,
3480 : : RelOptInfo *rel,
3481 : : Path *leftpath,
3482 : : Path *rightpath,
3483 : : SetOpCmd cmd,
3484 : : SetOpStrategy strategy,
3485 : : List *groupList,
3486 : : double numGroups,
3487 : : double outputRows)
3488 : : {
3489 : 1224 : SetOpPath *pathnode = makeNode(SetOpPath);
3490 : :
3491 : 1224 : pathnode->path.pathtype = T_SetOp;
3492 : 1224 : pathnode->path.parent = rel;
640 3493 : 1224 : pathnode->path.pathtarget = rel->reltarget;
3494 : : /* For now, assume we are above any joins, so no parameterization */
3849 3495 : 1224 : pathnode->path.param_info = NULL;
3496 : 1224 : pathnode->path.parallel_aware = false;
3497 : 2448 : pathnode->path.parallel_safe = rel->consider_parallel &&
640 3498 [ - + - - : 1224 : leftpath->parallel_safe && rightpath->parallel_safe;
- - ]
3499 : 1224 : pathnode->path.parallel_workers =
3500 : 1224 : leftpath->parallel_workers + rightpath->parallel_workers;
3501 : : /* SetOp preserves the input sort order if in sort mode */
3849 3502 : 1224 : pathnode->path.pathkeys =
640 3503 [ + + ]: 1224 : (strategy == SETOP_SORTED) ? leftpath->pathkeys : NIL;
3504 : :
3505 : 1224 : pathnode->leftpath = leftpath;
3506 : 1224 : pathnode->rightpath = rightpath;
3849 3507 : 1224 : pathnode->cmd = cmd;
3508 : 1224 : pathnode->strategy = strategy;
640 3509 : 1224 : pathnode->groupList = groupList;
3849 3510 : 1224 : pathnode->numGroups = numGroups;
3511 : :
3512 : : /*
3513 : : * Compute cost estimates. As things stand, we end up with the same total
3514 : : * cost in this node for sort and hash methods, but different startup
3515 : : * costs. This could be refined perhaps, but it'll do for now.
3516 : : */
640 3517 : 1224 : pathnode->path.disabled_nodes =
3518 : 1224 : leftpath->disabled_nodes + rightpath->disabled_nodes;
3519 [ + + ]: 1224 : if (strategy == SETOP_SORTED)
3520 : : {
3521 : : /*
3522 : : * In sorted mode, we can emit output incrementally. Charge one
3523 : : * cpu_operator_cost per comparison per input tuple. Like cost_group,
3524 : : * we assume all columns get compared at most of the tuples.
3525 : : */
3526 : 637 : pathnode->path.startup_cost =
3527 : 637 : leftpath->startup_cost + rightpath->startup_cost;
3528 : 637 : pathnode->path.total_cost =
3529 : 1274 : leftpath->total_cost + rightpath->total_cost +
3530 : 637 : cpu_operator_cost * (leftpath->rows + rightpath->rows) * list_length(groupList);
3531 : :
3532 : : /*
3533 : : * Also charge a small amount per extracted tuple. Like cost_sort,
3534 : : * charge only operator cost not cpu_tuple_cost, since SetOp does no
3535 : : * qual-checking or projection.
3536 : : */
3537 : 637 : pathnode->path.total_cost += cpu_operator_cost * outputRows;
3538 : :
3539 : : /*
3540 : : * Mark the path as disabled if enable_groupagg is off. While this
3541 : : * isn't a grouping Agg node, it is the sort-based implementation and
3542 : : * so is the natural counterpart to the SETOP_HASHED path that
3543 : : * enable_hashagg controls; it seems close enough to justify letting
3544 : : * that switch control it.
3545 : : */
73 rguo@postgresql.org 3546 [ + + ]:GNC 637 : if (!enable_groupagg)
3547 : 55 : pathnode->path.disabled_nodes++;
3548 : : }
3549 : : else
3550 : : {
3551 : : Size hashtablesize;
3552 : :
3553 : : /*
3554 : : * In hashed mode, we must read all the input before we can emit
3555 : : * anything. Also charge comparison costs to represent the cost of
3556 : : * hash table lookups.
3557 : : */
640 tgl@sss.pgh.pa.us 3558 :CBC 587 : pathnode->path.startup_cost =
3559 : 1174 : leftpath->total_cost + rightpath->total_cost +
3560 : 587 : cpu_operator_cost * (leftpath->rows + rightpath->rows) * list_length(groupList);
3561 : 587 : pathnode->path.total_cost = pathnode->path.startup_cost;
3562 : :
3563 : : /*
3564 : : * Also charge a small amount per extracted tuple. Like cost_sort,
3565 : : * charge only operator cost not cpu_tuple_cost, since SetOp does no
3566 : : * qual-checking or projection.
3567 : : */
3568 : 587 : pathnode->path.total_cost += cpu_operator_cost * outputRows;
3569 : :
3570 : : /*
3571 : : * Mark the path as disabled if enable_hashagg is off. While this
3572 : : * isn't exactly a HashAgg node, it seems close enough to justify
3573 : : * letting that switch control it.
3574 : : */
3575 [ + + ]: 587 : if (!enable_hashagg)
3576 : 95 : pathnode->path.disabled_nodes++;
3577 : :
3578 : : /*
3579 : : * Also disable if it doesn't look like the hashtable will fit into
3580 : : * hash_mem. (Note: reject on equality, to ensure that an estimate of
3581 : : * SIZE_MAX disables hashing regardless of the hash_mem limit.)
3582 : : */
322 3583 : 587 : hashtablesize = EstimateSetOpHashTableSpace(numGroups,
3584 : 587 : leftpath->pathtarget->width);
3585 [ - + ]: 587 : if (hashtablesize >= get_hash_memory_limit())
640 tgl@sss.pgh.pa.us 3586 :UBC 0 : pathnode->path.disabled_nodes++;
3587 : : }
3849 tgl@sss.pgh.pa.us 3588 :CBC 1224 : pathnode->path.rows = outputRows;
3589 : :
3590 : 1224 : return pathnode;
3591 : : }
3592 : :
3593 : : /*
3594 : : * create_recursiveunion_path
3595 : : * Creates a pathnode that represents a recursive UNION node
3596 : : *
3597 : : * 'rel' is the parent relation associated with the result
3598 : : * 'leftpath' is the source of data for the non-recursive term
3599 : : * 'rightpath' is the source of data for the recursive term
3600 : : * 'target' is the PathTarget to be computed
3601 : : * 'distinctList' is a list of SortGroupClause's representing the grouping
3602 : : * 'wtParam' is the ID of Param representing work table
3603 : : * 'numGroups' is the estimated number of groups
3604 : : *
3605 : : * For recursive UNION ALL, distinctList is empty and numGroups is zero
3606 : : */
3607 : : RecursiveUnionPath *
3608 : 636 : create_recursiveunion_path(PlannerInfo *root,
3609 : : RelOptInfo *rel,
3610 : : Path *leftpath,
3611 : : Path *rightpath,
3612 : : PathTarget *target,
3613 : : List *distinctList,
3614 : : int wtParam,
3615 : : double numGroups)
3616 : : {
3617 : 636 : RecursiveUnionPath *pathnode = makeNode(RecursiveUnionPath);
3618 : :
3619 : 636 : pathnode->path.pathtype = T_RecursiveUnion;
3620 : 636 : pathnode->path.parent = rel;
3621 : 636 : pathnode->path.pathtarget = target;
3622 : : /* For now, assume we are above any joins, so no parameterization */
3623 : 636 : pathnode->path.param_info = NULL;
3624 : 636 : pathnode->path.parallel_aware = false;
3625 : 1272 : pathnode->path.parallel_safe = rel->consider_parallel &&
3626 [ - + - - : 636 : leftpath->parallel_safe && rightpath->parallel_safe;
- - ]
3627 : : /* Foolish, but we'll do it like joins for now: */
3755 rhaas@postgresql.org 3628 : 636 : pathnode->path.parallel_workers = leftpath->parallel_workers;
3629 : : /* RecursiveUnion result is always unsorted */
3849 tgl@sss.pgh.pa.us 3630 : 636 : pathnode->path.pathkeys = NIL;
3631 : :
3632 : 636 : pathnode->leftpath = leftpath;
3633 : 636 : pathnode->rightpath = rightpath;
3634 : 636 : pathnode->distinctList = distinctList;
3635 : 636 : pathnode->wtParam = wtParam;
3636 : 636 : pathnode->numGroups = numGroups;
3637 : :
3638 : 636 : cost_recursive_union(&pathnode->path, leftpath, rightpath);
3639 : :
3640 : 636 : return pathnode;
3641 : : }
3642 : :
3643 : : /*
3644 : : * create_lockrows_path
3645 : : * Creates a pathnode that represents acquiring row locks
3646 : : *
3647 : : * 'rel' is the parent relation associated with the result
3648 : : * 'subpath' is the path representing the source of data
3649 : : * 'rowMarks' is a list of PlanRowMark's
3650 : : * 'epqParam' is the ID of Param for EvalPlanQual re-eval
3651 : : */
3652 : : LockRowsPath *
3653 : 6706 : create_lockrows_path(PlannerInfo *root, RelOptInfo *rel,
3654 : : Path *subpath, List *rowMarks, int epqParam)
3655 : : {
3656 : 6706 : LockRowsPath *pathnode = makeNode(LockRowsPath);
3657 : :
3658 : 6706 : pathnode->path.pathtype = T_LockRows;
3659 : 6706 : pathnode->path.parent = rel;
3660 : : /* LockRows doesn't project, so use source path's pathtarget */
3661 : 6706 : pathnode->path.pathtarget = subpath->pathtarget;
3662 : : /* For now, assume we are above any joins, so no parameterization */
3663 : 6706 : pathnode->path.param_info = NULL;
3664 : 6706 : pathnode->path.parallel_aware = false;
3665 : 6706 : pathnode->path.parallel_safe = false;
3755 rhaas@postgresql.org 3666 : 6706 : pathnode->path.parallel_workers = 0;
3849 tgl@sss.pgh.pa.us 3667 : 6706 : pathnode->path.rows = subpath->rows;
3668 : :
3669 : : /*
3670 : : * The result cannot be assumed sorted, since locking might cause the sort
3671 : : * key columns to be replaced with new values.
3672 : : */
3673 : 6706 : pathnode->path.pathkeys = NIL;
3674 : :
3675 : 6706 : pathnode->subpath = subpath;
3676 : 6706 : pathnode->rowMarks = rowMarks;
3677 : 6706 : pathnode->epqParam = epqParam;
3678 : :
3679 : : /*
3680 : : * We should charge something extra for the costs of row locking and
3681 : : * possible refetches, but it's hard to say how much. For now, use
3682 : : * cpu_tuple_cost per row.
3683 : : */
760 rhaas@postgresql.org 3684 : 6706 : pathnode->path.disabled_nodes = subpath->disabled_nodes;
3849 tgl@sss.pgh.pa.us 3685 : 6706 : pathnode->path.startup_cost = subpath->startup_cost;
3686 : 6706 : pathnode->path.total_cost = subpath->total_cost +
3687 : 6706 : cpu_tuple_cost * subpath->rows;
3688 : :
3689 : 6706 : return pathnode;
3690 : : }
3691 : :
3692 : : /*
3693 : : * create_modifytable_path
3694 : : * Creates a pathnode that represents performing INSERT/UPDATE/DELETE/MERGE
3695 : : * mods
3696 : : *
3697 : : * 'rel' is the parent relation associated with the result
3698 : : * 'subpath' is a Path producing source data
3699 : : * 'operation' is the operation type
3700 : : * 'canSetTag' is true if we set the command tag/es_processed
3701 : : * 'nominalRelation' is the parent RT index for use of EXPLAIN
3702 : : * 'rootRelation' is the partitioned/inherited table root RTI, or 0 if none
3703 : : * 'resultRelations' is an integer list of actual RT indexes of target rel(s)
3704 : : * 'updateColnosLists' is a list of UPDATE target column number lists
3705 : : * (one sublist per rel); or NIL if not an UPDATE
3706 : : * 'withCheckOptionLists' is a list of WCO lists (one per rel)
3707 : : * 'returningLists' is a list of RETURNING tlists (one per rel)
3708 : : * 'rowMarks' is a list of PlanRowMarks (non-locking only)
3709 : : * 'onconflict' is the ON CONFLICT clause, or NULL
3710 : : * 'epqParam' is the ID of Param for EvalPlanQual re-eval
3711 : : * 'mergeActionLists' is a list of lists of MERGE actions (one per rel)
3712 : : * 'mergeJoinConditions' is a list of join conditions for MERGE (one per rel)
3713 : : */
3714 : : ModifyTablePath *
3715 : 62875 : create_modifytable_path(PlannerInfo *root, RelOptInfo *rel,
3716 : : Path *subpath,
3717 : : CmdType operation, bool canSetTag,
3718 : : Index nominalRelation, Index rootRelation,
3719 : : List *resultRelations,
3720 : : List *updateColnosLists,
3721 : : List *withCheckOptionLists, List *returningLists,
3722 : : List *rowMarks, OnConflictExpr *onconflict,
3723 : : List *mergeActionLists, List *mergeJoinConditions,
3724 : : int epqParam)
3725 : : {
3726 : 62875 : ModifyTablePath *pathnode = makeNode(ModifyTablePath);
3727 : :
1637 alvherre@alvh.no-ip. 3728 [ + + + + : 62875 : Assert(operation == CMD_MERGE ||
- + ]
3729 : : (operation == CMD_UPDATE ?
3730 : : list_length(resultRelations) == list_length(updateColnosLists) :
3731 : : updateColnosLists == NIL));
3849 tgl@sss.pgh.pa.us 3732 [ + + - + ]: 62875 : Assert(withCheckOptionLists == NIL ||
3733 : : list_length(resultRelations) == list_length(withCheckOptionLists));
3734 [ + + - + ]: 62875 : Assert(returningLists == NIL ||
3735 : : list_length(resultRelations) == list_length(returningLists));
3736 : :
3737 : 62875 : pathnode->path.pathtype = T_ModifyTable;
3738 : 62875 : pathnode->path.parent = rel;
3739 : : /* pathtarget is not interesting, just make it minimally valid */
3842 3740 : 62875 : pathnode->path.pathtarget = rel->reltarget;
3741 : : /* For now, assume we are above any joins, so no parameterization */
3849 3742 : 62875 : pathnode->path.param_info = NULL;
3743 : 62875 : pathnode->path.parallel_aware = false;
3744 : 62875 : pathnode->path.parallel_safe = false;
3755 rhaas@postgresql.org 3745 : 62875 : pathnode->path.parallel_workers = 0;
3849 tgl@sss.pgh.pa.us 3746 : 62875 : pathnode->path.pathkeys = NIL;
3747 : :
3748 : : /*
3749 : : * Compute cost & rowcount as subpath cost & rowcount (if RETURNING)
3750 : : *
3751 : : * Currently, we don't charge anything extra for the actual table
3752 : : * modification work, nor for the WITH CHECK OPTIONS or RETURNING
3753 : : * expressions if any. It would only be window dressing, since
3754 : : * ModifyTable is always a top-level node and there is no way for the
3755 : : * costs to change any higher-level planning choices. But we might want
3756 : : * to make it look better sometime.
3757 : : */
760 rhaas@postgresql.org 3758 : 62875 : pathnode->path.disabled_nodes = subpath->disabled_nodes;
1999 tgl@sss.pgh.pa.us 3759 : 62875 : pathnode->path.startup_cost = subpath->startup_cost;
3760 : 62875 : pathnode->path.total_cost = subpath->total_cost;
3761 [ + + ]: 62875 : if (returningLists != NIL)
3762 : : {
3763 : 2601 : pathnode->path.rows = subpath->rows;
3764 : :
3765 : : /*
3766 : : * Set width to match the subpath output. XXX this is totally wrong:
3767 : : * we should return an average of the RETURNING tlist widths. But
3768 : : * it's what happened historically, and improving it is a task for
3769 : : * another day. (Again, it's mostly window dressing.)
3770 : : */
3771 : 2601 : pathnode->path.pathtarget->width = subpath->pathtarget->width;
3772 : : }
3773 : : else
3774 : : {
3775 : 60274 : pathnode->path.rows = 0;
3776 : 60274 : pathnode->path.pathtarget->width = 0;
3777 : : }
3778 : :
3779 : 62875 : pathnode->subpath = subpath;
3849 3780 : 62875 : pathnode->operation = operation;
3781 : 62875 : pathnode->canSetTag = canSetTag;
3782 : 62875 : pathnode->nominalRelation = nominalRelation;
2905 3783 : 62875 : pathnode->rootRelation = rootRelation;
3849 3784 : 62875 : pathnode->resultRelations = resultRelations;
1999 3785 : 62875 : pathnode->updateColnosLists = updateColnosLists;
3849 3786 : 62875 : pathnode->withCheckOptionLists = withCheckOptionLists;
3787 : 62875 : pathnode->returningLists = returningLists;
3788 : 62875 : pathnode->rowMarks = rowMarks;
3789 : 62875 : pathnode->onconflict = onconflict;
3790 : 62875 : pathnode->epqParam = epqParam;
1637 alvherre@alvh.no-ip. 3791 : 62875 : pathnode->mergeActionLists = mergeActionLists;
904 dean.a.rasheed@gmail 3792 : 62875 : pathnode->mergeJoinConditions = mergeJoinConditions;
3793 : :
3849 tgl@sss.pgh.pa.us 3794 : 62875 : 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 : 4236 : 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 : 4236 : LimitPath *pathnode = makeNode(LimitPath);
3822 : :
3823 : 4236 : pathnode->path.pathtype = T_Limit;
3824 : 4236 : pathnode->path.parent = rel;
3825 : : /* Limit doesn't project, so use source path's pathtarget */
3826 : 4236 : pathnode->path.pathtarget = subpath->pathtarget;
3827 : : /* For now, assume we are above any joins, so no parameterization */
3828 : 4236 : pathnode->path.param_info = NULL;
3829 : 4236 : pathnode->path.parallel_aware = false;
3830 [ + + ]: 5871 : pathnode->path.parallel_safe = rel->consider_parallel &&
3831 [ + + ]: 1635 : subpath->parallel_safe;
3755 rhaas@postgresql.org 3832 : 4236 : pathnode->path.parallel_workers = subpath->parallel_workers;
3849 tgl@sss.pgh.pa.us 3833 : 4236 : pathnode->path.rows = subpath->rows;
760 rhaas@postgresql.org 3834 : 4236 : pathnode->path.disabled_nodes = subpath->disabled_nodes;
3849 tgl@sss.pgh.pa.us 3835 : 4236 : pathnode->path.startup_cost = subpath->startup_cost;
3836 : 4236 : pathnode->path.total_cost = subpath->total_cost;
3837 : 4236 : pathnode->path.pathkeys = subpath->pathkeys;
3838 : 4236 : pathnode->subpath = subpath;
3839 : 4236 : pathnode->limitOffset = limitOffset;
3840 : 4236 : pathnode->limitCount = limitCount;
2357 alvherre@alvh.no-ip. 3841 : 4236 : pathnode->limitOption = limitOption;
3842 : :
3843 : : /*
3844 : : * Adjust the output rows count and costs according to the offset/limit.
3845 : : */
2728 efujita@postgresql.o 3846 : 4236 : adjust_limit_rows_costs(&pathnode->path.rows,
3847 : : &pathnode->path.startup_cost,
3848 : : &pathnode->path.total_cost,
3849 : : offset_est, count_est);
3850 : :
3851 : 4236 : 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 : 4329 : 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 : 4329 : double input_rows = *rows;
3878 : 4329 : Cost input_startup_cost = *startup_cost;
3879 : 4329 : Cost input_total_cost = *total_cost;
3880 : :
3849 tgl@sss.pgh.pa.us 3881 [ + + ]: 4329 : 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
2728 efujita@postgresql.o 3888 : 20 : offset_rows = clamp_row_est(input_rows * 0.10);
3889 [ + + ]: 403 : if (offset_rows > *rows)
3890 : 28 : 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 : 32 : *rows = 1;
3898 : : }
3899 : :
3849 tgl@sss.pgh.pa.us 3900 [ + + ]: 4329 : if (count_est != 0)
3901 : : {
3902 : : double count_rows;
3903 : :
3904 [ + + ]: 4273 : if (count_est > 0)
3905 : 4268 : count_rows = (double) count_est;
3906 : : else
2728 efujita@postgresql.o 3907 : 5 : count_rows = clamp_row_est(input_rows * 0.10);
3908 [ + + ]: 4273 : if (count_rows > *rows)
3909 : 148 : count_rows = *rows;
3910 [ + - ]: 4273 : if (input_rows > 0)
3911 : 4273 : *total_cost = *startup_cost +
3912 : 4273 : (input_total_cost - input_startup_cost)
3913 : 4273 : * count_rows / input_rows;
3914 : 4273 : *rows = count_rows;
3915 [ - + ]: 4273 : if (*rows < 1)
2728 efujita@postgresql.o 3916 :UBC 0 : *rows = 1;
3917 : : }
3849 tgl@sss.pgh.pa.us 3918 :CBC 4329 : }
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 *
5267 3939 : 874 : reparameterize_path(PlannerInfo *root, Path *path,
3940 : : Relids required_outer,
3941 : : double loop_count)
3942 : : {
3943 : 874 : RelOptInfo *rel = path->parent;
3944 : :
3945 : : /* Can only increase, not decrease, path's parameterization */
3946 [ - + - + ]: 874 : if (!bms_is_subset(PATH_REQ_OUTER(path), required_outer))
5267 tgl@sss.pgh.pa.us 3947 :UBC 0 : return NULL;
5267 tgl@sss.pgh.pa.us 3948 [ + - - - :CBC 874 : switch (path->pathtype)
- + + - -
+ ]
3949 : : {
3950 : 746 : case T_SeqScan:
3966 rhaas@postgresql.org 3951 : 746 : return create_seqscan_path(root, rel, required_outer, 0);
4075 tgl@sss.pgh.pa.us 3952 :UBC 0 : case T_SampleScan:
292 peter@eisentraut.org 3953 : 0 : return create_samplescan_path(root, rel, required_outer);
5267 tgl@sss.pgh.pa.us 3954 : 0 : case T_IndexScan:
3955 : : case T_IndexOnlyScan:
3956 : : {
5215 bruce@momjian.us 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);
3504 rhaas@postgresql.org 3970 : 0 : cost_index(newpath, root, loop_count, false);
5215 bruce@momjian.us 3971 : 0 : return (Path *) newpath;
3972 : : }
5267 tgl@sss.pgh.pa.us 3973 : 0 : case T_BitmapHeapScan:
3974 : : {
5215 bruce@momjian.us 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 : : }
5267 tgl@sss.pgh.pa.us 3983 : 0 : case T_SubqueryScan:
3984 : : {
3849 3985 : 0 : SubqueryScanPath *spath = (SubqueryScanPath *) path;
1524 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 : :
3849 3999 : 0 : return (Path *) create_subqueryscan_path(root,
4000 : : rel,
4001 : : subpath,
4002 : : trivial_pathtarget,
4003 : : spath->path.pathkeys,
4004 : : required_outer);
4005 : : }
2792 tgl@sss.pgh.pa.us 4006 :CBC 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);
2792 tgl@sss.pgh.pa.us 4010 :UBC 0 : break;
3162 tgl@sss.pgh.pa.us 4011 :CBC 5 : case T_Append:
4012 : : {
4013 : 5 : AppendPath *apath = (AppendPath *) path;
222 rhaas@postgresql.org 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 */
3162 tgl@sss.pgh.pa.us 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)
3162 tgl@sss.pgh.pa.us 4030 :UBC 0 : return NULL;
4031 : : /* We have to re-split the regular and partial paths */
3162 tgl@sss.pgh.pa.us 4032 [ + - ]:CBC 5 : if (i < apath->first_partial_path)
222 rhaas@postgresql.org 4033 : 5 : new_append.subpaths = lappend(new_append.subpaths, spath);
4034 : : else
222 rhaas@postgresql.org 4035 :UBC 0 : new_append.partial_subpaths = lappend(new_append.partial_subpaths, spath);
3162 tgl@sss.pgh.pa.us 4036 :CBC 5 : i++;
4037 : : }
4038 : 5 : return (Path *)
222 rhaas@postgresql.org 4039 : 5 : create_append_path(root, rel, new_append,
4040 : : apath->path.pathkeys, required_outer,
4041 : : apath->path.parallel_workers,
3162 tgl@sss.pgh.pa.us 4042 : 5 : apath->path.parallel_aware,
4043 : : -1);
4044 : : }
1386 tgl@sss.pgh.pa.us 4045 :UBC 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;
234 rhaas@postgresql.org 4056 : 0 : enabled =
4057 : 0 : (mpath->path.disabled_nodes <= spath->disabled_nodes);
235 4058 : 0 : return (Path *) create_material_path(rel, spath, enabled);
4059 : : }
1894 drowley@postgresql.o 4060 : 0 : case T_Memoize:
4061 : : {
4062 : 0 : MemoizePath *mpath = (MemoizePath *) path;
1386 tgl@sss.pgh.pa.us 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;
1894 drowley@postgresql.o 4070 : 0 : return (Path *) create_memoize_path(root, rel,
4071 : : spath,
4072 : : mpath->param_exprs,
4073 : : mpath->hash_operators,
4074 : 0 : mpath->singlerow,
1761 4075 : 0 : mpath->binary_mode,
4076 : : mpath->est_calls);
4077 : : }
5267 tgl@sss.pgh.pa.us 4078 :CBC 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 *
3271 rhaas@postgresql.org 4109 : 72922 : 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 [ + + ]: 72922 : if (!path->param_info ||
4146 [ + - + + ]: 35320 : !bms_overlap(PATH_REQ_OUTER(path), child_rel->top_parent_relids))
4147 : 71968 : 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 [ + + + + : 954 : switch (nodeTag(path))
+ - - + -
+ + - + -
- ]
4162 : : {
4163 : 190 : case T_Path:
915 tgl@sss.pgh.pa.us 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 [ - + ]: 40 : Assert(scan_relid > 0);
4173 [ + - ]: 40 : rte = planner_rt_fetch(scan_relid, root);
4174 [ - + ]: 40 : Assert(rte->rtekind == RTE_RELATION);
4175 [ - + ]: 40 : Assert(rte->tablesample != NULL);
4176 : :
4177 : 40 : ADJUST_CHILD_ATTRS(rte->tablesample);
4178 : : }
3271 rhaas@postgresql.org 4179 : 190 : break;
4180 : :
4181 : 544 : case T_IndexPath:
4182 : : {
915 tgl@sss.pgh.pa.us 4183 : 544 : IndexPath *ipath = (IndexPath *) path;
4184 : :
4185 : 544 : ADJUST_CHILD_ATTRS(ipath->indexinfo->indrestrictinfo);
3271 rhaas@postgresql.org 4186 : 544 : ADJUST_CHILD_ATTRS(ipath->indexclauses);
4187 : 544 : new_path = (Path *) ipath;
4188 : : }
4189 : 544 : break;
4190 : :
4191 : 40 : case T_BitmapHeapPath:
4192 : : {
915 tgl@sss.pgh.pa.us 4193 : 40 : BitmapHeapPath *bhpath = (BitmapHeapPath *) path;
4194 : :
4195 : 40 : ADJUST_CHILD_ATTRS(bhpath->path.parent->baserestrictinfo);
3271 rhaas@postgresql.org 4196 [ - + ]: 40 : REPARAMETERIZE_CHILD_PATH(bhpath->bitmapqual);
4197 : 40 : new_path = (Path *) bhpath;
4198 : : }
4199 : 40 : break;
4200 : :
4201 : 20 : case T_BitmapAndPath:
4202 : : {
915 tgl@sss.pgh.pa.us 4203 : 20 : BitmapAndPath *bapath = (BitmapAndPath *) path;
4204 : :
3271 rhaas@postgresql.org 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 : : {
915 tgl@sss.pgh.pa.us 4212 : 20 : BitmapOrPath *bopath = (BitmapOrPath *) path;
4213 : :
3271 rhaas@postgresql.org 4214 [ + - - + ]: 20 : REPARAMETERIZE_CHILD_PATH_LIST(bopath->bitmapquals);
4215 : 20 : new_path = (Path *) bopath;
4216 : : }
4217 : 20 : break;
4218 : :
3271 rhaas@postgresql.org 4219 :UBC 0 : case T_ForeignPath:
4220 : : {
915 tgl@sss.pgh.pa.us 4221 : 0 : ForeignPath *fpath = (ForeignPath *) path;
4222 : : ReparameterizeForeignPathByChild_function rfpc_func;
4223 : :
4224 : 0 : ADJUST_CHILD_ATTRS(fpath->path.parent->baserestrictinfo);
3271 rhaas@postgresql.org 4225 [ # # ]: 0 : if (fpath->fdw_outerpath)
4226 [ # # ]: 0 : REPARAMETERIZE_CHILD_PATH(fpath->fdw_outerpath);
1132 efujita@postgresql.o 4227 [ # # ]: 0 : if (fpath->fdw_restrictinfo)
4228 : 0 : ADJUST_CHILD_ATTRS(fpath->fdw_restrictinfo);
4229 : :
4230 : : /* Hand over to FDW if needed. */
3271 rhaas@postgresql.org 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 : : {
915 tgl@sss.pgh.pa.us 4242 : 0 : CustomPath *cpath = (CustomPath *) path;
4243 : :
4244 : 0 : ADJUST_CHILD_ATTRS(cpath->path.parent->baserestrictinfo);
3271 rhaas@postgresql.org 4245 [ # # # # ]: 0 : REPARAMETERIZE_CHILD_PATH_LIST(cpath->custom_paths);
1132 efujita@postgresql.o 4246 [ # # ]: 0 : if (cpath->custom_restrictinfo)
4247 : 0 : ADJUST_CHILD_ATTRS(cpath->custom_restrictinfo);
3271 rhaas@postgresql.org 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 : :
3271 rhaas@postgresql.org 4258 :CBC 40 : case T_NestPath:
4259 : : {
915 tgl@sss.pgh.pa.us 4260 : 40 : NestPath *npath = (NestPath *) path;
4261 : 40 : JoinPath *jpath = (JoinPath *) npath;
4262 : :
3271 rhaas@postgresql.org 4263 [ - + ]: 40 : REPARAMETERIZE_CHILD_PATH(jpath->outerjoinpath);
4264 [ - + ]: 40 : REPARAMETERIZE_CHILD_PATH(jpath->innerjoinpath);
4265 : 40 : ADJUST_CHILD_ATTRS(jpath->joinrestrictinfo);
1869 peter@eisentraut.org 4266 : 40 : new_path = (Path *) npath;
4267 : : }
3271 rhaas@postgresql.org 4268 : 40 : break;
4269 : :
3271 rhaas@postgresql.org 4270 :UBC 0 : case T_MergePath:
4271 : : {
915 tgl@sss.pgh.pa.us 4272 : 0 : MergePath *mpath = (MergePath *) path;
4273 : 0 : JoinPath *jpath = (JoinPath *) mpath;
4274 : :
3271 rhaas@postgresql.org 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 : :
3271 rhaas@postgresql.org 4283 :CBC 40 : case T_HashPath:
4284 : : {
915 tgl@sss.pgh.pa.us 4285 : 40 : HashPath *hpath = (HashPath *) path;
4286 : 40 : JoinPath *jpath = (JoinPath *) hpath;
4287 : :
3271 rhaas@postgresql.org 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 : : {
915 tgl@sss.pgh.pa.us 4298 : 20 : AppendPath *apath = (AppendPath *) path;
4299 : :
3271 rhaas@postgresql.org 4300 [ + - - + ]: 20 : REPARAMETERIZE_CHILD_PATH_LIST(apath->subpaths);
4301 : 20 : new_path = (Path *) apath;
4302 : : }
4303 : 20 : break;
4304 : :
1386 tgl@sss.pgh.pa.us 4305 :UBC 0 : case T_MaterialPath:
4306 : : {
915 4307 : 0 : MaterialPath *mpath = (MaterialPath *) path;
4308 : :
1386 4309 [ # # ]: 0 : REPARAMETERIZE_CHILD_PATH(mpath->subpath);
4310 : 0 : new_path = (Path *) mpath;
4311 : : }
4312 : 0 : break;
4313 : :
1894 drowley@postgresql.o 4314 :CBC 40 : case T_MemoizePath:
4315 : : {
915 tgl@sss.pgh.pa.us 4316 : 40 : MemoizePath *mpath = (MemoizePath *) path;
4317 : :
1894 drowley@postgresql.o 4318 [ - + ]: 40 : REPARAMETERIZE_CHILD_PATH(mpath->subpath);
1385 tgl@sss.pgh.pa.us 4319 : 40 : ADJUST_CHILD_ATTRS(mpath->param_exprs);
1894 drowley@postgresql.o 4320 : 40 : new_path = (Path *) mpath;
4321 : : }
1997 4322 : 40 : break;
4323 : :
3271 rhaas@postgresql.org 4324 :UBC 0 : case T_GatherPath:
4325 : : {
915 tgl@sss.pgh.pa.us 4326 : 0 : GatherPath *gpath = (GatherPath *) path;
4327 : :
3271 rhaas@postgresql.org 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 : : */
3271 rhaas@postgresql.org 4343 :CBC 954 : old_ppi = new_path->param_info;
4344 : : required_outer =
4345 : 954 : adjust_child_relids_multilevel(root, old_ppi->ppi_req_outer,
4346 : : child_rel,
1494 tgl@sss.pgh.pa.us 4347 : 954 : child_rel->top_parent);
4348 : :
4349 : : /* If we already have a PPI for this parameterization, just return it */
3271 rhaas@postgresql.org 4350 : 954 : 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 [ + + ]: 954 : if (new_ppi == NULL)
4358 : : {
4359 : : MemoryContext oldcontext;
4360 : 834 : RelOptInfo *rel = path->parent;
4361 : :
4362 : 834 : oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
4363 : :
4364 : 834 : new_ppi = makeNode(ParamPathInfo);
4365 : 834 : new_ppi->ppi_req_outer = bms_copy(required_outer);
4366 : 834 : new_ppi->ppi_rows = old_ppi->ppi_rows;
4367 : 834 : new_ppi->ppi_clauses = old_ppi->ppi_clauses;
4368 : 834 : ADJUST_CHILD_ATTRS(new_ppi->ppi_clauses);
1329 tgl@sss.pgh.pa.us 4369 : 834 : new_ppi->ppi_serials = bms_copy(old_ppi->ppi_serials);
3271 rhaas@postgresql.org 4370 : 834 : rel->ppilist = lappend(rel->ppilist, new_ppi);
4371 : :
4372 : 834 : MemoryContextSwitchTo(oldcontext);
4373 : : }
4374 : 954 : bms_free(required_outer);
4375 : :
4376 : 954 : 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 [ + + ]: 954 : if (bms_overlap(path->parent->lateral_relids,
4384 : 954 : 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 : 954 : 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
915 tgl@sss.pgh.pa.us 4405 : 33367 : 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 [ + + ]: 33367 : if (!path->param_info ||
4424 [ + - + + ]: 32979 : !bms_overlap(PATH_REQ_OUTER(path), child_rel->top_parent_relids))
4425 : 796 : 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 [ + + + + : 32571 : switch (nodeTag(path))
+ - + + -
+ - - ]
4432 : : {
4433 : 22683 : case T_Path:
4434 : : case T_IndexPath:
4435 : 22683 : 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)
915 tgl@sss.pgh.pa.us 4466 [ # # ]:UBC 0 : REJECT_IF_PATH_NOT_REPARAMETERIZABLE(fpath->fdw_outerpath);
4467 : : }
915 tgl@sss.pgh.pa.us 4468 :CBC 74 : break;
4469 : :
915 tgl@sss.pgh.pa.us 4470 :UBC 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 : :
915 tgl@sss.pgh.pa.us 4478 :CBC 1232 : case T_NestPath:
4479 : : case T_MergePath:
4480 : : case T_HashPath:
4481 : : {
4482 : 1232 : JoinPath *jpath = (JoinPath *) path;
4483 : :
4484 [ - + ]: 1232 : REJECT_IF_PATH_NOT_REPARAMETERIZABLE(jpath->outerjoinpath);
4485 [ - + ]: 1232 : REJECT_IF_PATH_NOT_REPARAMETERIZABLE(jpath->innerjoinpath);
4486 : : }
4487 : 1232 : 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 : :
915 tgl@sss.pgh.pa.us 4497 :UBC 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 : :
915 tgl@sss.pgh.pa.us 4505 :CBC 8342 : case T_MemoizePath:
4506 : : {
4507 : 8342 : MemoizePath *mpath = (MemoizePath *) path;
4508 : :
4509 [ - + ]: 8342 : REJECT_IF_PATH_NOT_REPARAMETERIZABLE(mpath->subpath);
4510 : : }
4511 : 8342 : break;
4512 : :
915 tgl@sss.pgh.pa.us 4513 :UBC 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 : :
915 tgl@sss.pgh.pa.us 4526 :CBC 32571 : 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 *
3271 rhaas@postgresql.org 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 : : {
3271 rhaas@postgresql.org 4550 :UBC 0 : list_free(result);
4551 : 0 : return NIL;
4552 : : }
4553 : :
3271 rhaas@postgresql.org 4554 :CBC 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
915 tgl@sss.pgh.pa.us 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))
915 tgl@sss.pgh.pa.us 4574 :UBC 0 : return false;
4575 : : }
4576 : :
915 tgl@sss.pgh.pa.us 4577 :CBC 200 : return true;
4578 : : }
|