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