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