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