LCOV - code coverage report
Current view: top level - src/backend/optimizer/util - pathnode.c (source / functions) Hit Total Coverage
Test: PostgreSQL 12beta2 Lines: 1250 1381 90.5 %
Date: 2019-06-19 14:06:47 Functions: 60 60 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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      383652 : compare_path_costs(Path *path1, Path *path2, CostSelector criterion)
      73             : {
      74      383652 :     if (criterion == STARTUP_COST)
      75             :     {
      76      196362 :         if (path1->startup_cost < path2->startup_cost)
      77       96058 :             return -1;
      78      100304 :         if (path1->startup_cost > path2->startup_cost)
      79       43328 :             return +1;
      80             : 
      81             :         /*
      82             :          * If paths have the same startup cost (not at all unlikely), order
      83             :          * them by total cost.
      84             :          */
      85       56976 :         if (path1->total_cost < path2->total_cost)
      86       35728 :             return -1;
      87       21248 :         if (path1->total_cost > path2->total_cost)
      88        1496 :             return +1;
      89             :     }
      90             :     else
      91             :     {
      92      187290 :         if (path1->total_cost < path2->total_cost)
      93      179686 :             return -1;
      94        7604 :         if (path1->total_cost > path2->total_cost)
      95        1522 :             return +1;
      96             : 
      97             :         /*
      98             :          * If paths have the same total cost, order them by startup cost.
      99             :          */
     100        6082 :         if (path1->startup_cost < path2->startup_cost)
     101           8 :             return -1;
     102        6074 :         if (path1->startup_cost > path2->startup_cost)
     103           0 :             return +1;
     104             :     }
     105       25826 :     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        2246 : compare_fractional_path_costs(Path *path1, Path *path2,
     119             :                               double fraction)
     120             : {
     121             :     Cost        cost1,
     122             :                 cost2;
     123             : 
     124        2246 :     if (fraction <= 0.0 || fraction >= 1.0)
     125        1034 :         return compare_path_costs(path1, path2, TOTAL_COST);
     126        2424 :     cost1 = path1->startup_cost +
     127        1212 :         fraction * (path1->total_cost - path1->startup_cost);
     128        2424 :     cost2 = path2->startup_cost +
     129        1212 :         fraction * (path2->total_cost - path2->startup_cost);
     130        1212 :     if (cost1 < cost2)
     131         630 :         return -1;
     132         582 :     if (cost1 > cost2)
     133         582 :         return +1;
     134           0 :     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     1703358 : 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     1703358 :     if (path1->total_cost > path2->total_cost * fuzz_factor)
     177             :     {
     178             :         /* path1 fuzzily worse on total cost */
     179      835214 :         if (CONSIDER_PATH_STARTUP_COST(path1) &&
     180       27210 :             path2->startup_cost > path1->startup_cost * fuzz_factor)
     181             :         {
     182             :             /* ... but path2 fuzzily worse on startup, so DIFFERENT */
     183       13116 :             return COSTS_DIFFERENT;
     184             :         }
     185             :         /* else path2 dominates */
     186      794888 :         return COSTS_BETTER2;
     187             :     }
     188      895354 :     if (path2->total_cost > path1->total_cost * fuzz_factor)
     189             :     {
     190             :         /* path2 fuzzily worse on total cost */
     191      464534 :         if (CONSIDER_PATH_STARTUP_COST(path2) &&
     192       11664 :             path1->startup_cost > path2->startup_cost * fuzz_factor)
     193             :         {
     194             :             /* ... but path1 fuzzily worse on startup, so DIFFERENT */
     195        6348 :             return COSTS_DIFFERENT;
     196             :         }
     197             :         /* else path1 dominates */
     198      446522 :         return COSTS_BETTER1;
     199             :     }
     200             :     /* fuzzily the same on total cost ... */
     201      442484 :     if (path1->startup_cost > path2->startup_cost * fuzz_factor)
     202             :     {
     203             :         /* ... but path1 fuzzily worse on startup, so path2 wins */
     204      174432 :         return COSTS_BETTER2;
     205             :     }
     206      268052 :     if (path2->startup_cost > path1->startup_cost * fuzz_factor)
     207             :     {
     208             :         /* ... but path2 fuzzily worse on startup, so path1 wins */
     209       36552 :         return COSTS_BETTER1;
     210             :     }
     211             :     /* fuzzily the same on both costs */
     212      231500 :     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     1108192 : 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     1108192 :     if (parent_rel->pathlist == NIL)
     256           0 :         elog(ERROR, "could not devise a query plan for the given query");
     257             : 
     258     1108192 :     cheapest_startup_path = cheapest_total_path = best_param_path = NULL;
     259     1108192 :     parameterized_paths = NIL;
     260             : 
     261     2422004 :     foreach(p, parent_rel->pathlist)
     262             :     {
     263     1313812 :         Path       *path = (Path *) lfirst(p);
     264             :         int         cmp;
     265             : 
     266     1313812 :         if (path->param_info)
     267             :         {
     268             :             /* Parameterized path, so add it to parameterized_paths */
     269       57986 :             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       57986 :             if (cheapest_total_path)
     276        7794 :                 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       50192 :             if (best_param_path == NULL)
     284       47068 :                 best_param_path = path;
     285             :             else
     286             :             {
     287        6248 :                 switch (bms_subset_compare(PATH_REQ_OUTER(path),
     288        6248 :                                            PATH_REQ_OUTER(best_param_path)))
     289             :                 {
     290             :                     case BMS_EQUAL:
     291             :                         /* keep the cheaper one */
     292          36 :                         if (compare_path_costs(path, best_param_path,
     293             :                                                TOTAL_COST) < 0)
     294           0 :                             best_param_path = path;
     295          36 :                         break;
     296             :                     case BMS_SUBSET1:
     297             :                         /* new path is less-parameterized */
     298         138 :                         best_param_path = path;
     299         138 :                         break;
     300             :                     case BMS_SUBSET2:
     301             :                         /* old path is less-parameterized, keep it */
     302           8 :                         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        2942 :                         break;
     311             :                 }
     312             :             }
     313             :         }
     314             :         else
     315             :         {
     316             :             /* Unparameterized path, so consider it for cheapest slots */
     317     1255826 :             if (cheapest_total_path == NULL)
     318             :             {
     319     1106760 :                 cheapest_startup_path = cheapest_total_path = path;
     320     1106760 :                 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      149066 :             cmp = compare_path_costs(cheapest_startup_path, path, STARTUP_COST);
     331      149066 :             if (cmp > 0 ||
     332          66 :                 (cmp == 0 &&
     333          66 :                  compare_pathkeys(cheapest_startup_path->pathkeys,
     334             :                                   path->pathkeys) == PATHKEYS_BETTER2))
     335       35764 :                 cheapest_startup_path = path;
     336             : 
     337      149066 :             cmp = compare_path_costs(cheapest_total_path, path, TOTAL_COST);
     338      149066 :             if (cmp > 0 ||
     339           0 :                 (cmp == 0 &&
     340           0 :                  compare_pathkeys(cheapest_total_path->pathkeys,
     341             :                                   path->pathkeys) == PATHKEYS_BETTER2))
     342           0 :                 cheapest_total_path = path;
     343             :         }
     344             :     }
     345             : 
     346             :     /* Add cheapest unparameterized path, if any, to parameterized_paths */
     347     1108192 :     if (cheapest_total_path)
     348     1106760 :         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     1108192 :     if (cheapest_total_path == NULL)
     355        1432 :         cheapest_total_path = best_param_path;
     356             :     Assert(cheapest_total_path != NULL);
     357             : 
     358     1108192 :     parent_rel->cheapest_startup_path = cheapest_startup_path;
     359     1108192 :     parent_rel->cheapest_total_path = cheapest_total_path;
     360     1108192 :     parent_rel->cheapest_unique_path = NULL; /* computed only if needed */
     361     1108192 :     parent_rel->cheapest_parameterized_paths = parameterized_paths;
     362     1108192 : }
     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     2009920 : add_path(RelOptInfo *parent_rel, Path *new_path)
     424             : {
     425     2009920 :     bool        accept_new = true;  /* unless we find a superior old path */
     426     2009920 :     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     2009920 :     CHECK_FOR_INTERRUPTS();
     437             : 
     438             :     /* Pretend parameterized paths have no pathkeys, per comment above */
     439     2009920 :     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     2009920 :     p1_prev = NULL;
     450     2858882 :     for (p1 = list_head(parent_rel->pathlist); p1 != NULL; p1 = p1_next)
     451             :     {
     452     1543968 :         Path       *old_path = (Path *) lfirst(p1);
     453     1543968 :         bool        remove_old = false; /* unless new proves superior */
     454             :         PathCostComparison costcmp;
     455             :         PathKeysComparison keyscmp;
     456             :         BMS_Comparison outercmp;
     457             : 
     458     1543968 :         p1_next = lnext(p1);
     459             : 
     460             :         /*
     461             :          * Do a fuzzy cost comparison with standard fuzziness limit.
     462             :          */
     463     1543968 :         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     1543968 :         if (costcmp != COSTS_DIFFERENT)
     478             :         {
     479             :             /* Similarly check to see if either dominates on pathkeys */
     480             :             List       *old_path_pathkeys;
     481             : 
     482     1524514 :             old_path_pathkeys = old_path->param_info ? NIL : old_path->pathkeys;
     483     1524514 :             keyscmp = compare_pathkeys(new_path_pathkeys,
     484             :                                        old_path_pathkeys);
     485     1524514 :             if (keyscmp != PATHKEYS_DIFFERENT)
     486             :             {
     487     1472808 :                 switch (costcmp)
     488             :                 {
     489             :                     case COSTS_EQUAL:
     490      167814 :                         outercmp = bms_subset_compare(PATH_REQ_OUTER(new_path),
     491      167814 :                                                       PATH_REQ_OUTER(old_path));
     492      166734 :                         if (keyscmp == PATHKEYS_BETTER1)
     493             :                         {
     494         920 :                             if ((outercmp == BMS_EQUAL ||
     495         920 :                                  outercmp == BMS_SUBSET1) &&
     496        1836 :                                 new_path->rows <= old_path->rows &&
     497         916 :                                 new_path->parallel_safe >= old_path->parallel_safe)
     498         916 :                                 remove_old = true;  /* new dominates old */
     499             :                         }
     500      165814 :                         else if (keyscmp == PATHKEYS_BETTER2)
     501             :                         {
     502        5370 :                             if ((outercmp == BMS_EQUAL ||
     503        5370 :                                  outercmp == BMS_SUBSET2) &&
     504       10740 :                                 new_path->rows >= old_path->rows &&
     505        5370 :                                 new_path->parallel_safe <= old_path->parallel_safe)
     506        5370 :                                 accept_new = false; /* old dominates new */
     507             :                         }
     508             :                         else    /* keyscmp == PATHKEYS_EQUAL */
     509             :                         {
     510      160444 :                             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      319012 :                                 if (new_path->parallel_safe >
     528      159506 :                                     old_path->parallel_safe)
     529          32 :                                     remove_old = true;  /* new dominates old */
     530      318948 :                                 else if (new_path->parallel_safe <
     531      159474 :                                          old_path->parallel_safe)
     532          68 :                                     accept_new = false; /* old dominates new */
     533      159406 :                                 else if (new_path->rows < old_path->rows)
     534           4 :                                     remove_old = true;  /* new dominates old */
     535      159402 :                                 else if (new_path->rows > old_path->rows)
     536          12 :                                     accept_new = false; /* old dominates new */
     537      159390 :                                 else if (compare_path_costs_fuzzily(new_path,
     538             :                                                                     old_path,
     539             :                                                                     1.0000000001) == COSTS_BETTER1)
     540        5338 :                                     remove_old = true;  /* new dominates old */
     541             :                                 else
     542      154052 :                                     accept_new = false; /* old equals or
     543             :                                                          * dominates new */
     544             :                             }
     545        1120 :                             else if (outercmp == BMS_SUBSET1 &&
     546         360 :                                      new_path->rows <= old_path->rows &&
     547         178 :                                      new_path->parallel_safe >= old_path->parallel_safe)
     548         178 :                                 remove_old = true;  /* new dominates old */
     549        1414 :                             else if (outercmp == BMS_SUBSET2 &&
     550        1294 :                                      new_path->rows >= old_path->rows &&
     551         640 :                                      new_path->parallel_safe <= old_path->parallel_safe)
     552         640 :                                 accept_new = false; /* old dominates new */
     553             :                             /* else different parameterizations, keep both */
     554             :                         }
     555      166734 :                         break;
     556             :                     case COSTS_BETTER1:
     557      468426 :                         if (keyscmp != PATHKEYS_BETTER2)
     558             :                         {
     559      330554 :                             outercmp = bms_subset_compare(PATH_REQ_OUTER(new_path),
     560      330554 :                                                           PATH_REQ_OUTER(old_path));
     561      323918 :                             if ((outercmp == BMS_EQUAL ||
     562      278268 :                                  outercmp == BMS_SUBSET1) &&
     563      555288 :                                 new_path->rows <= old_path->rows &&
     564      277020 :                                 new_path->parallel_safe >= old_path->parallel_safe)
     565      275588 :                                 remove_old = true;  /* new dominates old */
     566             :                         }
     567      468426 :                         break;
     568             :                     case COSTS_BETTER2:
     569      837648 :                         if (keyscmp != PATHKEYS_BETTER1)
     570             :                         {
     571      669116 :                             outercmp = bms_subset_compare(PATH_REQ_OUTER(new_path),
     572      669116 :                                                           PATH_REQ_OUTER(old_path));
     573      585166 :                             if ((outercmp == BMS_EQUAL ||
     574      556322 :                                  outercmp == BMS_SUBSET2) &&
     575     1092722 :                                 new_path->rows >= old_path->rows &&
     576      536400 :                                 new_path->parallel_safe <= old_path->parallel_safe)
     577      534864 :                                 accept_new = false; /* old dominates new */
     578             :                         }
     579      837648 :                         break;
     580             :                     case COSTS_DIFFERENT:
     581             : 
     582             :                         /*
     583             :                          * can't get here, but keep this case to keep compiler
     584             :                          * quiet
     585             :                          */
     586           0 :                         break;
     587             :                 }
     588             :             }
     589             :         }
     590             : 
     591             :         /*
     592             :          * Remove current element from pathlist if dominated by new.
     593             :          */
     594     1543968 :         if (remove_old)
     595             :         {
     596      282056 :             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      282056 :             if (!IsA(old_path, IndexPath))
     603      269214 :                 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     1261912 :             if (new_path->total_cost >= old_path->total_cost)
     610     1014070 :                 insert_after = p1;
     611             :             /* p1_prev advances */
     612     1261912 :             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     1543968 :         if (!accept_new)
     621      695006 :             break;
     622             :     }
     623             : 
     624     2009920 :     if (accept_new)
     625             :     {
     626             :         /* Accept the new path: insert it at proper place in pathlist */
     627     1314914 :         if (insert_after)
     628      175704 :             lappend_cell(parent_rel->pathlist, insert_after, new_path);
     629             :         else
     630     1139210 :             parent_rel->pathlist = lcons(new_path, parent_rel->pathlist);
     631             :     }
     632             :     else
     633             :     {
     634             :         /* Reject and recycle the new path */
     635      695006 :         if (!IsA(new_path, IndexPath))
     636      645516 :             pfree(new_path);
     637             :     }
     638     2009920 : }
     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     1739926 : 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     1739926 :     new_path_pathkeys = required_outer ? NIL : pathkeys;
     668             : 
     669             :     /* Decide whether new path's startup cost is interesting */
     670     1739926 :     consider_startup = required_outer ? parent_rel->consider_param_startup : parent_rel->consider_startup;
     671             : 
     672     2154868 :     foreach(p1, parent_rel->pathlist)
     673             :     {
     674     2033776 :         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     2033776 :         if (total_cost > old_path->total_cost * STD_FUZZ_FACTOR)
     686             :         {
     687             :             /* new path can win on startup cost only if consider_startup */
     688     2018466 :             if (startup_cost > old_path->startup_cost * STD_FUZZ_FACTOR ||
     689      679914 :                 !consider_startup)
     690             :             {
     691             :                 /* new path loses on cost, so check pathkeys... */
     692             :                 List       *old_path_pathkeys;
     693             : 
     694     1327596 :                 old_path_pathkeys = old_path->param_info ? NIL : old_path->pathkeys;
     695     1327596 :                 keyscmp = compare_pathkeys(new_path_pathkeys,
     696             :                                            old_path_pathkeys);
     697     1327596 :                 if (keyscmp == PATHKEYS_EQUAL ||
     698             :                     keyscmp == PATHKEYS_BETTER2)
     699             :                 {
     700             :                     /* new path does not win on pathkeys... */
     701      935032 :                     if (bms_equal(required_outer, PATH_REQ_OUTER(old_path)))
     702             :                     {
     703             :                         /* Found an old path that dominates the new one */
     704      923610 :                         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      695224 :             break;
     717             :         }
     718             :     }
     719             : 
     720      816316 :     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       33916 : add_partial_path(RelOptInfo *parent_rel, Path *new_path)
     764             : {
     765       33916 :     bool        accept_new = true;  /* unless we find a superior old path */
     766       33916 :     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       33916 :     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       33916 :     p1_prev = NULL;
     785       75818 :     for (p1 = list_head(parent_rel->partial_pathlist); p1 != NULL;
     786        7986 :          p1 = p1_next)
     787             :     {
     788       11502 :         Path       *old_path = (Path *) lfirst(p1);
     789       11502 :         bool        remove_old = false; /* unless new proves superior */
     790             :         PathKeysComparison keyscmp;
     791             : 
     792       11502 :         p1_next = lnext(p1);
     793             : 
     794             :         /* Compare pathkeys. */
     795       11502 :         keyscmp = compare_pathkeys(new_path->pathkeys, old_path->pathkeys);
     796             : 
     797             :         /* Unless pathkeys are incompable, keep just one of the two paths. */
     798       11502 :         if (keyscmp != PATHKEYS_DIFFERENT)
     799             :         {
     800       11502 :             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        5710 :                 if (keyscmp != PATHKEYS_BETTER1)
     804        2690 :                     accept_new = false;
     805             :             }
     806       11584 :             else if (old_path->total_cost > new_path->total_cost
     807        5792 :                      * STD_FUZZ_FACTOR)
     808             :             {
     809             :                 /* Old path costs more; keep it only if pathkeys are better. */
     810        4798 :                 if (keyscmp != PATHKEYS_BETTER2)
     811        2628 :                     remove_old = true;
     812             :             }
     813         994 :             else if (keyscmp == PATHKEYS_BETTER1)
     814             :             {
     815             :                 /* Costs are about the same, new path has better pathkeys. */
     816           0 :                 remove_old = true;
     817             :             }
     818         994 :             else if (keyscmp == PATHKEYS_BETTER2)
     819             :             {
     820             :                 /* Costs are about the same, old path has better pathkeys. */
     821         112 :                 accept_new = false;
     822             :             }
     823         882 :             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         168 :                 remove_old = true;
     827             :             }
     828             :             else
     829             :             {
     830             :                 /*
     831             :                  * Pathkeys are the same, and new path isn't materially
     832             :                  * cheaper.
     833             :                  */
     834         714 :                 accept_new = false;
     835             :             }
     836             :         }
     837             : 
     838             :         /*
     839             :          * Remove current element from partial_pathlist if dominated by new.
     840             :          */
     841       11502 :         if (remove_old)
     842             :         {
     843        2796 :             parent_rel->partial_pathlist =
     844        2796 :                 list_delete_cell(parent_rel->partial_pathlist, p1, p1_prev);
     845        2796 :             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        8706 :             if (new_path->total_cost >= old_path->total_cost)
     852        6496 :                 insert_after = p1;
     853             :             /* p1_prev advances */
     854        8706 :             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       11502 :         if (!accept_new)
     863        3516 :             break;
     864             :     }
     865             : 
     866       33916 :     if (accept_new)
     867             :     {
     868             :         /* Accept the new path: insert it at proper place */
     869       30400 :         if (insert_after)
     870        2724 :             lappend_cell(parent_rel->partial_pathlist, insert_after, new_path);
     871             :         else
     872       27676 :             parent_rel->partial_pathlist =
     873       27676 :                 lcons(new_path, parent_rel->partial_pathlist);
     874             :     }
     875             :     else
     876             :     {
     877             :         /* Reject and recycle the new path */
     878        3516 :         pfree(new_path);
     879             :     }
     880       33916 : }
     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       19204 : 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       24254 :     foreach(p1, parent_rel->partial_pathlist)
     910             :     {
     911       20412 :         Path       *old_path = (Path *) lfirst(p1);
     912             :         PathKeysComparison keyscmp;
     913             : 
     914       20412 :         keyscmp = compare_pathkeys(pathkeys, old_path->pathkeys);
     915       20412 :         if (keyscmp != PATHKEYS_DIFFERENT)
     916             :         {
     917       20412 :             if (total_cost > old_path->total_cost * STD_FUZZ_FACTOR &&
     918             :                 keyscmp != PATHKEYS_BETTER1)
     919       12458 :                 return false;
     920        7954 :             if (old_path->total_cost > total_cost * STD_FUZZ_FACTOR &&
     921             :                 keyscmp != PATHKEYS_BETTER2)
     922        2904 :                 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        3842 :     if (!add_path_precheck(parent_rel, total_cost, total_cost, pathkeys,
     938             :                            NULL))
     939         240 :         return false;
     940             : 
     941        3602 :     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      228696 : create_seqscan_path(PlannerInfo *root, RelOptInfo *rel,
     956             :                     Relids required_outer, int parallel_workers)
     957             : {
     958      228696 :     Path       *pathnode = makeNode(Path);
     959             : 
     960      228696 :     pathnode->pathtype = T_SeqScan;
     961      228696 :     pathnode->parent = rel;
     962      228696 :     pathnode->pathtarget = rel->reltarget;
     963      228696 :     pathnode->param_info = get_baserel_parampathinfo(root, rel,
     964             :                                                      required_outer);
     965      228696 :     pathnode->parallel_aware = parallel_workers > 0 ? true : false;
     966      228696 :     pathnode->parallel_safe = rel->consider_parallel;
     967      228696 :     pathnode->parallel_workers = parallel_workers;
     968      228696 :     pathnode->pathkeys = NIL;    /* seqscan has unordered result */
     969             : 
     970      228696 :     cost_seqscan(pathnode, root, rel, pathnode->param_info);
     971             : 
     972      228696 :     return pathnode;
     973             : }
     974             : 
     975             : /*
     976             :  * create_samplescan_path
     977             :  *    Creates a path node for a sampled table scan.
     978             :  */
     979             : Path *
     980         180 : create_samplescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
     981             : {
     982         180 :     Path       *pathnode = makeNode(Path);
     983             : 
     984         180 :     pathnode->pathtype = T_SampleScan;
     985         180 :     pathnode->parent = rel;
     986         180 :     pathnode->pathtarget = rel->reltarget;
     987         180 :     pathnode->param_info = get_baserel_parampathinfo(root, rel,
     988             :                                                      required_outer);
     989         180 :     pathnode->parallel_aware = false;
     990         180 :     pathnode->parallel_safe = rel->consider_parallel;
     991         180 :     pathnode->parallel_workers = 0;
     992         180 :     pathnode->pathkeys = NIL;    /* samplescan has unordered result */
     993             : 
     994         180 :     cost_samplescan(pathnode, root, rel, pathnode->param_info);
     995             : 
     996         180 :     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      366182 : 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      366182 :     IndexPath  *pathnode = makeNode(IndexPath);
    1036      366182 :     RelOptInfo *rel = index->rel;
    1037             : 
    1038      366182 :     pathnode->path.pathtype = indexonly ? T_IndexOnlyScan : T_IndexScan;
    1039      366182 :     pathnode->path.parent = rel;
    1040      366182 :     pathnode->path.pathtarget = rel->reltarget;
    1041      366182 :     pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
    1042             :                                                           required_outer);
    1043      366182 :     pathnode->path.parallel_aware = false;
    1044      366182 :     pathnode->path.parallel_safe = rel->consider_parallel;
    1045      366182 :     pathnode->path.parallel_workers = 0;
    1046      366182 :     pathnode->path.pathkeys = pathkeys;
    1047             : 
    1048      366182 :     pathnode->indexinfo = index;
    1049      366182 :     pathnode->indexclauses = indexclauses;
    1050      366182 :     pathnode->indexorderbys = indexorderbys;
    1051      366182 :     pathnode->indexorderbycols = indexorderbycols;
    1052      366182 :     pathnode->indexscandir = indexscandir;
    1053             : 
    1054      366182 :     cost_index(pathnode, root, loop_count, partial_path);
    1055             : 
    1056      366182 :     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      173102 : 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      173102 :     BitmapHeapPath *pathnode = makeNode(BitmapHeapPath);
    1080             : 
    1081      173102 :     pathnode->path.pathtype = T_BitmapHeapScan;
    1082      173102 :     pathnode->path.parent = rel;
    1083      173102 :     pathnode->path.pathtarget = rel->reltarget;
    1084      173102 :     pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
    1085             :                                                           required_outer);
    1086      173102 :     pathnode->path.parallel_aware = parallel_degree > 0 ? true : false;
    1087      173102 :     pathnode->path.parallel_safe = rel->consider_parallel;
    1088      173102 :     pathnode->path.parallel_workers = parallel_degree;
    1089      173102 :     pathnode->path.pathkeys = NIL;   /* always unordered */
    1090             : 
    1091      173102 :     pathnode->bitmapqual = bitmapqual;
    1092             : 
    1093      173102 :     cost_bitmap_heap_scan(&pathnode->path, root, rel,
    1094             :                           pathnode->path.param_info,
    1095             :                           bitmapqual, loop_count);
    1096             : 
    1097      173102 :     return pathnode;
    1098             : }
    1099             : 
    1100             : /*
    1101             :  * create_bitmap_and_path
    1102             :  *    Creates a path node representing a BitmapAnd.
    1103             :  */
    1104             : BitmapAndPath *
    1105          44 : create_bitmap_and_path(PlannerInfo *root,
    1106             :                        RelOptInfo *rel,
    1107             :                        List *bitmapquals)
    1108             : {
    1109          44 :     BitmapAndPath *pathnode = makeNode(BitmapAndPath);
    1110             : 
    1111          44 :     pathnode->path.pathtype = T_BitmapAnd;
    1112          44 :     pathnode->path.parent = rel;
    1113          44 :     pathnode->path.pathtarget = rel->reltarget;
    1114          44 :     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          44 :     pathnode->path.parallel_aware = false;
    1123          44 :     pathnode->path.parallel_safe = rel->consider_parallel;
    1124          44 :     pathnode->path.parallel_workers = 0;
    1125             : 
    1126          44 :     pathnode->path.pathkeys = NIL;   /* always unordered */
    1127             : 
    1128          44 :     pathnode->bitmapquals = bitmapquals;
    1129             : 
    1130             :     /* this sets bitmapselectivity as well as the regular cost fields: */
    1131          44 :     cost_bitmap_and_node(pathnode, root);
    1132             : 
    1133          44 :     return pathnode;
    1134             : }
    1135             : 
    1136             : /*
    1137             :  * create_bitmap_or_path
    1138             :  *    Creates a path node representing a BitmapOr.
    1139             :  */
    1140             : BitmapOrPath *
    1141         318 : create_bitmap_or_path(PlannerInfo *root,
    1142             :                       RelOptInfo *rel,
    1143             :                       List *bitmapquals)
    1144             : {
    1145         318 :     BitmapOrPath *pathnode = makeNode(BitmapOrPath);
    1146             : 
    1147         318 :     pathnode->path.pathtype = T_BitmapOr;
    1148         318 :     pathnode->path.parent = rel;
    1149         318 :     pathnode->path.pathtarget = rel->reltarget;
    1150         318 :     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         318 :     pathnode->path.parallel_aware = false;
    1159         318 :     pathnode->path.parallel_safe = rel->consider_parallel;
    1160         318 :     pathnode->path.parallel_workers = 0;
    1161             : 
    1162         318 :     pathnode->path.pathkeys = NIL;   /* always unordered */
    1163             : 
    1164         318 :     pathnode->bitmapquals = bitmapquals;
    1165             : 
    1166             :     /* this sets bitmapselectivity as well as the regular cost fields: */
    1167         318 :     cost_bitmap_or_node(pathnode, root);
    1168             : 
    1169         318 :     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         594 : create_tidscan_path(PlannerInfo *root, RelOptInfo *rel, List *tidquals,
    1178             :                     Relids required_outer)
    1179             : {
    1180         594 :     TidPath    *pathnode = makeNode(TidPath);
    1181             : 
    1182         594 :     pathnode->path.pathtype = T_TidScan;
    1183         594 :     pathnode->path.parent = rel;
    1184         594 :     pathnode->path.pathtarget = rel->reltarget;
    1185         594 :     pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
    1186             :                                                           required_outer);
    1187         594 :     pathnode->path.parallel_aware = false;
    1188         594 :     pathnode->path.parallel_safe = rel->consider_parallel;
    1189         594 :     pathnode->path.parallel_workers = 0;
    1190         594 :     pathnode->path.pathkeys = NIL;   /* always unordered */
    1191             : 
    1192         594 :     pathnode->tidquals = tidquals;
    1193             : 
    1194         594 :     cost_tidscan(&pathnode->path, root, rel, tidquals,
    1195             :                  pathnode->path.param_info);
    1196             : 
    1197         594 :     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       27800 : 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       27800 :     AppendPath *pathnode = makeNode(AppendPath);
    1217             :     ListCell   *l;
    1218             : 
    1219             :     Assert(!parallel_aware || parallel_workers > 0);
    1220             : 
    1221       27800 :     pathnode->path.pathtype = T_Append;
    1222       27800 :     pathnode->path.parent = rel;
    1223       27800 :     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       27800 :     if (partitioned_rels != NIL && root && rel->reloptkind == RELOPT_BASEREL)
    1236       11270 :         pathnode->path.param_info = get_baserel_parampathinfo(root,
    1237             :                                                               rel,
    1238             :                                                               required_outer);
    1239             :     else
    1240       16530 :         pathnode->path.param_info = get_appendrel_parampathinfo(rel,
    1241             :                                                                 required_outer);
    1242             : 
    1243       27800 :     pathnode->path.parallel_aware = parallel_aware;
    1244       27800 :     pathnode->path.parallel_safe = rel->consider_parallel;
    1245       27800 :     pathnode->path.parallel_workers = parallel_workers;
    1246       27800 :     pathnode->path.pathkeys = pathkeys;
    1247       27800 :     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       27800 :     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        9552 :         subpaths = list_qsort(subpaths, append_total_cost_compare);
    1268        9552 :         partial_subpaths = list_qsort(partial_subpaths,
    1269             :                                       append_startup_cost_compare);
    1270             :     }
    1271       27800 :     pathnode->first_partial_path = list_length(subpaths);
    1272       27800 :     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       27800 :     if (root != NULL && bms_equal(rel->relids, root->all_baserels))
    1279       13534 :         pathnode->limit_tuples = root->limit_tuples;
    1280             :     else
    1281       14266 :         pathnode->limit_tuples = -1.0;
    1282             : 
    1283      102058 :     foreach(l, pathnode->subpaths)
    1284             :     {
    1285       74258 :         Path       *subpath = (Path *) lfirst(l);
    1286             : 
    1287      134614 :         pathnode->path.parallel_safe = pathnode->path.parallel_safe &&
    1288       60356 :             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       27800 :     if (list_length(pathnode->subpaths) == 1)
    1304             :     {
    1305        4686 :         Path       *child = (Path *) linitial(pathnode->subpaths);
    1306             : 
    1307        4686 :         pathnode->path.rows = child->rows;
    1308        4686 :         pathnode->path.startup_cost = child->startup_cost;
    1309        4686 :         pathnode->path.total_cost = child->total_cost;
    1310        4686 :         pathnode->path.pathkeys = child->pathkeys;
    1311             :     }
    1312             :     else
    1313       23114 :         cost_append(pathnode);
    1314             : 
    1315             :     /* If the caller provided a row estimate, override the computed value. */
    1316       27800 :     if (rows >= 0)
    1317         244 :         pathnode->path.rows = rows;
    1318             : 
    1319       27800 :     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        2036 : append_total_cost_compare(const void *a, const void *b)
    1332             : {
    1333        2036 :     Path       *path1 = (Path *) lfirst(*(ListCell **) a);
    1334        2036 :     Path       *path2 = (Path *) lfirst(*(ListCell **) b);
    1335             :     int         cmp;
    1336             : 
    1337        2036 :     cmp = compare_path_costs(path1, path2, TOTAL_COST);
    1338        2036 :     if (cmp != 0)
    1339        1636 :         return -cmp;
    1340         400 :     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       17646 : append_startup_cost_compare(const void *a, const void *b)
    1353             : {
    1354       17646 :     Path       *path1 = (Path *) lfirst(*(ListCell **) a);
    1355       17646 :     Path       *path2 = (Path *) lfirst(*(ListCell **) b);
    1356             :     int         cmp;
    1357             : 
    1358       17646 :     cmp = compare_path_costs(path1, path2, STARTUP_COST);
    1359       17646 :     if (cmp != 0)
    1360        4026 :         return -cmp;
    1361       13620 :     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        1344 : 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        1344 :     MergeAppendPath *pathnode = makeNode(MergeAppendPath);
    1378             :     Cost        input_startup_cost;
    1379             :     Cost        input_total_cost;
    1380             :     ListCell   *l;
    1381             : 
    1382        1344 :     pathnode->path.pathtype = T_MergeAppend;
    1383        1344 :     pathnode->path.parent = rel;
    1384        1344 :     pathnode->path.pathtarget = rel->reltarget;
    1385        1344 :     pathnode->path.param_info = get_appendrel_parampathinfo(rel,
    1386             :                                                             required_outer);
    1387        1344 :     pathnode->path.parallel_aware = false;
    1388        1344 :     pathnode->path.parallel_safe = rel->consider_parallel;
    1389        1344 :     pathnode->path.parallel_workers = 0;
    1390        1344 :     pathnode->path.pathkeys = pathkeys;
    1391        1344 :     pathnode->partitioned_rels = list_copy(partitioned_rels);
    1392        1344 :     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        1344 :     if (bms_equal(rel->relids, root->all_baserels))
    1399         488 :         pathnode->limit_tuples = root->limit_tuples;
    1400             :     else
    1401         856 :         pathnode->limit_tuples = -1.0;
    1402             : 
    1403             :     /*
    1404             :      * Add up the sizes and costs of the input paths.
    1405             :      */
    1406        1344 :     pathnode->path.rows = 0;
    1407        1344 :     input_startup_cost = 0;
    1408        1344 :     input_total_cost = 0;
    1409        5158 :     foreach(l, subpaths)
    1410             :     {
    1411        3814 :         Path       *subpath = (Path *) lfirst(l);
    1412             : 
    1413        3814 :         pathnode->path.rows += subpath->rows;
    1414        6378 :         pathnode->path.parallel_safe = pathnode->path.parallel_safe &&
    1415        2564 :             subpath->parallel_safe;
    1416             : 
    1417        3814 :         if (pathkeys_contained_in(pathkeys, subpath->pathkeys))
    1418             :         {
    1419             :             /* Subpath is adequately ordered, we won't need to sort it */
    1420        3674 :             input_startup_cost += subpath->startup_cost;
    1421        3674 :             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         420 :             cost_sort(&sort_path,
    1429             :                       root,
    1430             :                       pathkeys,
    1431             :                       subpath->total_cost,
    1432         140 :                       subpath->parent->tuples,
    1433         140 :                       subpath->pathtarget->width,
    1434             :                       0.0,
    1435             :                       work_mem,
    1436             :                       pathnode->limit_tuples);
    1437         140 :             input_startup_cost += sort_path.startup_cost;
    1438         140 :             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        1344 :     if (list_length(subpaths) == 1)
    1451             :     {
    1452          38 :         pathnode->path.startup_cost = input_startup_cost;
    1453          38 :         pathnode->path.total_cost = input_total_cost;
    1454             :     }
    1455             :     else
    1456        1306 :         cost_merge_append(&pathnode->path, root,
    1457             :                           pathkeys, list_length(subpaths),
    1458             :                           input_startup_cost, input_total_cost,
    1459             :                           pathnode->path.rows);
    1460             : 
    1461        1344 :     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      110030 : create_group_result_path(PlannerInfo *root, RelOptInfo *rel,
    1473             :                          PathTarget *target, List *havingqual)
    1474             : {
    1475      110030 :     GroupResultPath *pathnode = makeNode(GroupResultPath);
    1476             : 
    1477      110030 :     pathnode->path.pathtype = T_Result;
    1478      110030 :     pathnode->path.parent = rel;
    1479      110030 :     pathnode->path.pathtarget = target;
    1480      110030 :     pathnode->path.param_info = NULL;    /* there are no other rels... */
    1481      110030 :     pathnode->path.parallel_aware = false;
    1482      110030 :     pathnode->path.parallel_safe = rel->consider_parallel;
    1483      110030 :     pathnode->path.parallel_workers = 0;
    1484      110030 :     pathnode->path.pathkeys = NIL;
    1485      110030 :     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      110030 :     pathnode->path.rows = 1;
    1493      110030 :     pathnode->path.startup_cost = target->cost.startup;
    1494      220060 :     pathnode->path.total_cost = target->cost.startup +
    1495      110030 :         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      110030 :     if (havingqual)
    1502             :     {
    1503             :         QualCost    qual_cost;
    1504             : 
    1505         260 :         cost_qual_eval(&qual_cost, havingqual, root);
    1506             :         /* havingqual is evaluated once at startup */
    1507         260 :         pathnode->path.startup_cost += qual_cost.startup + qual_cost.per_tuple;
    1508         260 :         pathnode->path.total_cost += qual_cost.startup + qual_cost.per_tuple;
    1509             :     }
    1510             : 
    1511      110030 :     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      214614 : create_material_path(RelOptInfo *rel, Path *subpath)
    1521             : {
    1522      214614 :     MaterialPath *pathnode = makeNode(MaterialPath);
    1523             : 
    1524             :     Assert(subpath->parent == rel);
    1525             : 
    1526      214614 :     pathnode->path.pathtype = T_Material;
    1527      214614 :     pathnode->path.parent = rel;
    1528      214614 :     pathnode->path.pathtarget = rel->reltarget;
    1529      214614 :     pathnode->path.param_info = subpath->param_info;
    1530      214614 :     pathnode->path.parallel_aware = false;
    1531      404482 :     pathnode->path.parallel_safe = rel->consider_parallel &&
    1532      189868 :         subpath->parallel_safe;
    1533      214614 :     pathnode->path.parallel_workers = subpath->parallel_workers;
    1534      214614 :     pathnode->path.pathkeys = subpath->pathkeys;
    1535             : 
    1536      214614 :     pathnode->subpath = subpath;
    1537             : 
    1538      214614 :     cost_material(&pathnode->path,
    1539             :                   subpath->startup_cost,
    1540             :                   subpath->total_cost,
    1541             :                   subpath->rows,
    1542      214614 :                   subpath->pathtarget->width);
    1543             : 
    1544      214614 :     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       10806 : 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       10806 :     if (rel->cheapest_unique_path)
    1577        9036 :         return (UniquePath *) rel->cheapest_unique_path;
    1578             : 
    1579             :     /* If it's not possible to unique-ify, return NULL */
    1580        1770 :     if (!(sjinfo->semi_can_btree || sjinfo->semi_can_hash))
    1581          62 :         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        1708 :     oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
    1594             : 
    1595        1708 :     pathnode = makeNode(UniquePath);
    1596             : 
    1597        1708 :     pathnode->path.pathtype = T_Unique;
    1598        1708 :     pathnode->path.parent = rel;
    1599        1708 :     pathnode->path.pathtarget = rel->reltarget;
    1600        1708 :     pathnode->path.param_info = subpath->param_info;
    1601        1708 :     pathnode->path.parallel_aware = false;
    1602        3256 :     pathnode->path.parallel_safe = rel->consider_parallel &&
    1603        1548 :         subpath->parallel_safe;
    1604        1708 :     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        1708 :     pathnode->path.pathkeys = NIL;
    1611             : 
    1612        1708 :     pathnode->subpath = subpath;
    1613        1708 :     pathnode->in_operators = sjinfo->semi_operators;
    1614        1708 :     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        1980 :     if (rel->rtekind == RTE_RELATION && sjinfo->semi_can_btree &&
    1623         272 :         relation_has_unique_index_for(root, rel, NIL,
    1624             :                                       sjinfo->semi_rhs_exprs,
    1625             :                                       sjinfo->semi_operators))
    1626             :     {
    1627           0 :         pathnode->umethod = UNIQUE_PATH_NOOP;
    1628           0 :         pathnode->path.rows = rel->rows;
    1629           0 :         pathnode->path.startup_cost = subpath->startup_cost;
    1630           0 :         pathnode->path.total_cost = subpath->total_cost;
    1631           0 :         pathnode->path.pathkeys = subpath->pathkeys;
    1632             : 
    1633           0 :         rel->cheapest_unique_path = (Path *) pathnode;
    1634             : 
    1635           0 :         MemoryContextSwitchTo(oldcontext);
    1636             : 
    1637           0 :         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        1708 :     if (rel->rtekind == RTE_SUBQUERY)
    1650             :     {
    1651         256 :         RangeTblEntry *rte = planner_rt_fetch(rel->relid, root);
    1652             : 
    1653         256 :         if (query_supports_distinctness(rte->subquery))
    1654             :         {
    1655             :             List       *sub_tlist_colnos;
    1656             : 
    1657         232 :             sub_tlist_colnos = translate_sub_tlist(sjinfo->semi_rhs_exprs,
    1658         232 :                                                    rel->relid);
    1659             : 
    1660         272 :             if (sub_tlist_colnos &&
    1661          40 :                 query_is_distinct_for(rte->subquery,
    1662             :                                       sub_tlist_colnos,
    1663             :                                       sjinfo->semi_operators))
    1664             :             {
    1665           0 :                 pathnode->umethod = UNIQUE_PATH_NOOP;
    1666           0 :                 pathnode->path.rows = rel->rows;
    1667           0 :                 pathnode->path.startup_cost = subpath->startup_cost;
    1668           0 :                 pathnode->path.total_cost = subpath->total_cost;
    1669           0 :                 pathnode->path.pathkeys = subpath->pathkeys;
    1670             : 
    1671           0 :                 rel->cheapest_unique_path = (Path *) pathnode;
    1672             : 
    1673           0 :                 MemoryContextSwitchTo(oldcontext);
    1674             : 
    1675           0 :                 return pathnode;
    1676             :             }
    1677             :         }
    1678             :     }
    1679             : 
    1680             :     /* Estimate number of output rows */
    1681        1708 :     pathnode->path.rows = estimate_num_groups(root,
    1682             :                                               sjinfo->semi_rhs_exprs,
    1683             :                                               rel->rows,
    1684             :                                               NULL);
    1685        1708 :     numCols = list_length(sjinfo->semi_rhs_exprs);
    1686             : 
    1687        1708 :     if (sjinfo->semi_can_btree)
    1688             :     {
    1689             :         /*
    1690             :          * Estimate cost for sort+unique implementation
    1691             :          */
    1692        3416 :         cost_sort(&sort_path, root, NIL,
    1693             :                   subpath->total_cost,
    1694             :                   rel->rows,
    1695        1708 :                   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        1708 :         sort_path.total_cost += cpu_operator_cost * rel->rows * numCols;
    1707             :     }
    1708             : 
    1709        1708 :     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        1708 :         int         hashentrysize = subpath->pathtarget->width + 64;
    1716             : 
    1717        1708 :         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           0 :             sjinfo->semi_can_hash = false;
    1724             :         }
    1725             :         else
    1726        1708 :             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        1708 :     if (sjinfo->semi_can_btree && sjinfo->semi_can_hash)
    1736             :     {
    1737        3416 :         if (agg_path.total_cost < sort_path.total_cost)
    1738        1658 :             pathnode->umethod = UNIQUE_PATH_HASH;
    1739             :         else
    1740          50 :             pathnode->umethod = UNIQUE_PATH_SORT;
    1741             :     }
    1742           0 :     else if (sjinfo->semi_can_btree)
    1743           0 :         pathnode->umethod = UNIQUE_PATH_SORT;
    1744           0 :     else if (sjinfo->semi_can_hash)
    1745           0 :         pathnode->umethod = UNIQUE_PATH_HASH;
    1746             :     else
    1747             :     {
    1748             :         /* we can get here only if we abandoned hashing above */
    1749           0 :         MemoryContextSwitchTo(oldcontext);
    1750           0 :         return NULL;
    1751             :     }
    1752             : 
    1753        1708 :     if (pathnode->umethod == UNIQUE_PATH_HASH)
    1754             :     {
    1755        1658 :         pathnode->path.startup_cost = agg_path.startup_cost;
    1756        1658 :         pathnode->path.total_cost = agg_path.total_cost;
    1757             :     }
    1758             :     else
    1759             :     {
    1760          50 :         pathnode->path.startup_cost = sort_path.startup_cost;
    1761          50 :         pathnode->path.total_cost = sort_path.total_cost;
    1762             :     }
    1763             : 
    1764        1708 :     rel->cheapest_unique_path = (Path *) pathnode;
    1765             : 
    1766        1708 :     MemoryContextSwitchTo(oldcontext);
    1767             : 
    1768        1708 :     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        2844 : create_gather_merge_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath,
    1779             :                          PathTarget *target, List *pathkeys,
    1780             :                          Relids required_outer, double *rows)
    1781             : {
    1782        2844 :     GatherMergePath *pathnode = makeNode(GatherMergePath);
    1783        2844 :     Cost        input_startup_cost = 0;
    1784        2844 :     Cost        input_total_cost = 0;
    1785             : 
    1786             :     Assert(subpath->parallel_safe);
    1787             :     Assert(pathkeys);
    1788             : 
    1789        2844 :     pathnode->path.pathtype = T_GatherMerge;
    1790        2844 :     pathnode->path.parent = rel;
    1791        2844 :     pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
    1792             :                                                           required_outer);
    1793        2844 :     pathnode->path.parallel_aware = false;
    1794             : 
    1795        2844 :     pathnode->subpath = subpath;
    1796        2844 :     pathnode->num_workers = subpath->parallel_workers;
    1797        2844 :     pathnode->path.pathkeys = pathkeys;
    1798        2844 :     pathnode->path.pathtarget = target ? target : rel->reltarget;
    1799        2844 :     pathnode->path.rows += subpath->rows;
    1800             : 
    1801        2844 :     if (pathkeys_contained_in(pathkeys, subpath->pathkeys))
    1802             :     {
    1803             :         /* Subpath is adequately ordered, we won't need to sort it */
    1804        2844 :         input_startup_cost += subpath->startup_cost;
    1805        2844 :         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           0 :         cost_sort(&sort_path,
    1813             :                   root,
    1814             :                   pathkeys,
    1815             :                   subpath->total_cost,
    1816             :                   subpath->rows,
    1817           0 :                   subpath->pathtarget->width,
    1818             :                   0.0,
    1819             :                   work_mem,
    1820             :                   -1);
    1821           0 :         input_startup_cost += sort_path.startup_cost;
    1822           0 :         input_total_cost += sort_path.total_cost;
    1823             :     }
    1824             : 
    1825        2844 :     cost_gather_merge(pathnode, root, rel, pathnode->path.param_info,
    1826             :                       input_startup_cost, input_total_cost, rows);
    1827             : 
    1828        2844 :     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         232 : translate_sub_tlist(List *tlist, int relid)
    1844             : {
    1845         232 :     List       *result = NIL;
    1846             :     ListCell   *l;
    1847             : 
    1848         272 :     foreach(l, tlist)
    1849             :     {
    1850         232 :         Var        *var = (Var *) lfirst(l);
    1851             : 
    1852         272 :         if (!var || !IsA(var, Var) ||
    1853          40 :             var->varno != relid)
    1854         192 :             return NIL;         /* punt */
    1855             : 
    1856          40 :         result = lappend_int(result, var->varattno);
    1857             :     }
    1858          40 :     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        7720 : create_gather_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath,
    1870             :                    PathTarget *target, Relids required_outer, double *rows)
    1871             : {
    1872        7720 :     GatherPath *pathnode = makeNode(GatherPath);
    1873             : 
    1874             :     Assert(subpath->parallel_safe);
    1875             : 
    1876        7720 :     pathnode->path.pathtype = T_Gather;
    1877        7720 :     pathnode->path.parent = rel;
    1878        7720 :     pathnode->path.pathtarget = target;
    1879        7720 :     pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
    1880             :                                                           required_outer);
    1881        7720 :     pathnode->path.parallel_aware = false;
    1882        7720 :     pathnode->path.parallel_safe = false;
    1883        7720 :     pathnode->path.parallel_workers = 0;
    1884        7720 :     pathnode->path.pathkeys = NIL;   /* Gather has unordered result */
    1885             : 
    1886        7720 :     pathnode->subpath = subpath;
    1887        7720 :     pathnode->num_workers = subpath->parallel_workers;
    1888        7720 :     pathnode->single_copy = false;
    1889             : 
    1890        7720 :     if (pathnode->num_workers == 0)
    1891             :     {
    1892           0 :         pathnode->path.pathkeys = subpath->pathkeys;
    1893           0 :         pathnode->num_workers = 1;
    1894           0 :         pathnode->single_copy = true;
    1895             :     }
    1896             : 
    1897        7720 :     cost_gather(pathnode, root, rel, pathnode->path.param_info, rows);
    1898             : 
    1899        7720 :     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        7924 : create_subqueryscan_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath,
    1909             :                          List *pathkeys, Relids required_outer)
    1910             : {
    1911        7924 :     SubqueryScanPath *pathnode = makeNode(SubqueryScanPath);
    1912             : 
    1913        7924 :     pathnode->path.pathtype = T_SubqueryScan;
    1914        7924 :     pathnode->path.parent = rel;
    1915        7924 :     pathnode->path.pathtarget = rel->reltarget;
    1916        7924 :     pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
    1917             :                                                           required_outer);
    1918        7924 :     pathnode->path.parallel_aware = false;
    1919       12050 :     pathnode->path.parallel_safe = rel->consider_parallel &&
    1920        4126 :         subpath->parallel_safe;
    1921        7924 :     pathnode->path.parallel_workers = subpath->parallel_workers;
    1922        7924 :     pathnode->path.pathkeys = pathkeys;
    1923        7924 :     pathnode->subpath = subpath;
    1924             : 
    1925        7924 :     cost_subqueryscan(pathnode, root, rel, pathnode->path.param_info);
    1926             : 
    1927        7924 :     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       28734 : create_functionscan_path(PlannerInfo *root, RelOptInfo *rel,
    1937             :                          List *pathkeys, Relids required_outer)
    1938             : {
    1939       28734 :     Path       *pathnode = makeNode(Path);
    1940             : 
    1941       28734 :     pathnode->pathtype = T_FunctionScan;
    1942       28734 :     pathnode->parent = rel;
    1943       28734 :     pathnode->pathtarget = rel->reltarget;
    1944       28734 :     pathnode->param_info = get_baserel_parampathinfo(root, rel,
    1945             :                                                      required_outer);
    1946       28734 :     pathnode->parallel_aware = false;
    1947       28734 :     pathnode->parallel_safe = rel->consider_parallel;
    1948       28734 :     pathnode->parallel_workers = 0;
    1949       28734 :     pathnode->pathkeys = pathkeys;
    1950             : 
    1951       28734 :     cost_functionscan(pathnode, root, rel, pathnode->param_info);
    1952             : 
    1953       28734 :     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         140 : create_tablefuncscan_path(PlannerInfo *root, RelOptInfo *rel,
    1963             :                           Relids required_outer)
    1964             : {
    1965         140 :     Path       *pathnode = makeNode(Path);
    1966             : 
    1967         140 :     pathnode->pathtype = T_TableFuncScan;
    1968         140 :     pathnode->parent = rel;
    1969         140 :     pathnode->pathtarget = rel->reltarget;
    1970         140 :     pathnode->param_info = get_baserel_parampathinfo(root, rel,
    1971             :                                                      required_outer);
    1972         140 :     pathnode->parallel_aware = false;
    1973         140 :     pathnode->parallel_safe = rel->consider_parallel;
    1974         140 :     pathnode->parallel_workers = 0;
    1975         140 :     pathnode->pathkeys = NIL;    /* result is always unordered */
    1976             : 
    1977         140 :     cost_tablefuncscan(pathnode, root, rel, pathnode->param_info);
    1978             : 
    1979         140 :     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        3774 : create_valuesscan_path(PlannerInfo *root, RelOptInfo *rel,
    1989             :                        Relids required_outer)
    1990             : {
    1991        3774 :     Path       *pathnode = makeNode(Path);
    1992             : 
    1993        3774 :     pathnode->pathtype = T_ValuesScan;
    1994        3774 :     pathnode->parent = rel;
    1995        3774 :     pathnode->pathtarget = rel->reltarget;
    1996        3774 :     pathnode->param_info = get_baserel_parampathinfo(root, rel,
    1997             :                                                      required_outer);
    1998        3774 :     pathnode->parallel_aware = false;
    1999        3774 :     pathnode->parallel_safe = rel->consider_parallel;
    2000        3774 :     pathnode->parallel_workers = 0;
    2001        3774 :     pathnode->pathkeys = NIL;    /* result is always unordered */
    2002             : 
    2003        3774 :     cost_valuesscan(pathnode, root, rel, pathnode->param_info);
    2004             : 
    2005        3774 :     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         894 : create_ctescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
    2015             : {
    2016         894 :     Path       *pathnode = makeNode(Path);
    2017             : 
    2018         894 :     pathnode->pathtype = T_CteScan;
    2019         894 :     pathnode->parent = rel;
    2020         894 :     pathnode->pathtarget = rel->reltarget;
    2021         894 :     pathnode->param_info = get_baserel_parampathinfo(root, rel,
    2022             :                                                      required_outer);
    2023         894 :     pathnode->parallel_aware = false;
    2024         894 :     pathnode->parallel_safe = rel->consider_parallel;
    2025         894 :     pathnode->parallel_workers = 0;
    2026         894 :     pathnode->pathkeys = NIL;    /* XXX for now, result is always unordered */
    2027             : 
    2028         894 :     cost_ctescan(pathnode, root, rel, pathnode->param_info);
    2029             : 
    2030         894 :     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         258 : create_namedtuplestorescan_path(PlannerInfo *root, RelOptInfo *rel,
    2040             :                                 Relids required_outer)
    2041             : {
    2042         258 :     Path       *pathnode = makeNode(Path);
    2043             : 
    2044         258 :     pathnode->pathtype = T_NamedTuplestoreScan;
    2045         258 :     pathnode->parent = rel;
    2046         258 :     pathnode->pathtarget = rel->reltarget;
    2047         258 :     pathnode->param_info = get_baserel_parampathinfo(root, rel,
    2048             :                                                      required_outer);
    2049         258 :     pathnode->parallel_aware = false;
    2050         258 :     pathnode->parallel_safe = rel->consider_parallel;
    2051         258 :     pathnode->parallel_workers = 0;
    2052         258 :     pathnode->pathkeys = NIL;    /* result is always unordered */
    2053             : 
    2054         258 :     cost_namedtuplestorescan(pathnode, root, rel, pathnode->param_info);
    2055             : 
    2056         258 :     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         598 : create_resultscan_path(PlannerInfo *root, RelOptInfo *rel,
    2066             :                        Relids required_outer)
    2067             : {
    2068         598 :     Path       *pathnode = makeNode(Path);
    2069             : 
    2070         598 :     pathnode->pathtype = T_Result;
    2071         598 :     pathnode->parent = rel;
    2072         598 :     pathnode->pathtarget = rel->reltarget;
    2073         598 :     pathnode->param_info = get_baserel_parampathinfo(root, rel,
    2074             :                                                      required_outer);
    2075         598 :     pathnode->parallel_aware = false;
    2076         598 :     pathnode->parallel_safe = rel->consider_parallel;
    2077         598 :     pathnode->parallel_workers = 0;
    2078         598 :     pathnode->pathkeys = NIL;    /* result is always unordered */
    2079             : 
    2080         598 :     cost_resultscan(pathnode, root, rel, pathnode->param_info);
    2081             : 
    2082         598 :     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         326 : create_worktablescan_path(PlannerInfo *root, RelOptInfo *rel,
    2092             :                           Relids required_outer)
    2093             : {
    2094         326 :     Path       *pathnode = makeNode(Path);
    2095             : 
    2096         326 :     pathnode->pathtype = T_WorkTableScan;
    2097         326 :     pathnode->parent = rel;
    2098         326 :     pathnode->pathtarget = rel->reltarget;
    2099         326 :     pathnode->param_info = get_baserel_parampathinfo(root, rel,
    2100             :                                                      required_outer);
    2101         326 :     pathnode->parallel_aware = false;
    2102         326 :     pathnode->parallel_safe = rel->consider_parallel;
    2103         326 :     pathnode->parallel_workers = 0;
    2104         326 :     pathnode->pathkeys = NIL;    /* result is always unordered */
    2105             : 
    2106             :     /* Cost is the same as for a regular CTE scan */
    2107         326 :     cost_ctescan(pathnode, root, rel, pathnode->param_info);
    2108             : 
    2109         326 :     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        2718 : 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        2718 :     ForeignPath *pathnode = makeNode(ForeignPath);
    2133             : 
    2134             :     /* Historically some FDWs were confused about when to use this */
    2135             :     Assert(IS_SIMPLE_REL(rel));
    2136             : 
    2137        2718 :     pathnode->path.pathtype = T_ForeignScan;
    2138        2718 :     pathnode->path.parent = rel;
    2139        2718 :     pathnode->path.pathtarget = target ? target : rel->reltarget;
    2140        2718 :     pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
    2141             :                                                           required_outer);
    2142        2718 :     pathnode->path.parallel_aware = false;
    2143        2718 :     pathnode->path.parallel_safe = rel->consider_parallel;
    2144        2718 :     pathnode->path.parallel_workers = 0;
    2145        2718 :     pathnode->path.rows = rows;
    2146        2718 :     pathnode->path.startup_cost = startup_cost;
    2147        2718 :     pathnode->path.total_cost = total_cost;
    2148        2718 :     pathnode->path.pathkeys = pathkeys;
    2149             : 
    2150        2718 :     pathnode->fdw_outerpath = fdw_outerpath;
    2151        2718 :     pathnode->fdw_private = fdw_private;
    2152             : 
    2153        2718 :     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         702 : 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         702 :     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         702 :     if (!bms_is_empty(required_outer) || !bms_is_empty(rel->lateral_relids))
    2186           0 :         elog(ERROR, "parameterized foreign joins are not supported yet");
    2187             : 
    2188         702 :     pathnode->path.pathtype = T_ForeignScan;
    2189         702 :     pathnode->path.parent = rel;
    2190         702 :     pathnode->path.pathtarget = target ? target : rel->reltarget;
    2191         702 :     pathnode->path.param_info = NULL;    /* XXX see above */
    2192         702 :     pathnode->path.parallel_aware = false;
    2193         702 :     pathnode->path.parallel_safe = rel->consider_parallel;
    2194         702 :     pathnode->path.parallel_workers = 0;
    2195         702 :     pathnode->path.rows = rows;
    2196         702 :     pathnode->path.startup_cost = startup_cost;
    2197         702 :     pathnode->path.total_cost = total_cost;
    2198         702 :     pathnode->path.pathkeys = pathkeys;
    2199             : 
    2200         702 :     pathnode->fdw_outerpath = fdw_outerpath;
    2201         702 :     pathnode->fdw_private = fdw_private;
    2202             : 
    2203         702 :     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         448 : 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         448 :     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         448 :     pathnode->path.pathtype = T_ForeignScan;
    2234         448 :     pathnode->path.parent = rel;
    2235         448 :     pathnode->path.pathtarget = target ? target : rel->reltarget;
    2236         448 :     pathnode->path.param_info = NULL;
    2237         448 :     pathnode->path.parallel_aware = false;
    2238         448 :     pathnode->path.parallel_safe = rel->consider_parallel;
    2239         448 :     pathnode->path.parallel_workers = 0;
    2240         448 :     pathnode->path.rows = rows;
    2241         448 :     pathnode->path.startup_cost = startup_cost;
    2242         448 :     pathnode->path.total_cost = total_cost;
    2243         448 :     pathnode->path.pathkeys = pathkeys;
    2244             : 
    2245         448 :     pathnode->fdw_outerpath = fdw_outerpath;
    2246         448 :     pathnode->fdw_private = fdw_private;
    2247             : 
    2248         448 :     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     1085450 : 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     1085450 :     if (!inner_paramrels)
    2269      839096 :         return bms_copy(outer_paramrels);
    2270             :     /* else, form the union ... */
    2271      246354 :     required_outer = bms_union(outer_paramrels, inner_paramrels);
    2272             :     /* ... and remove any mention of now-satisfied outer rels */
    2273      246354 :     required_outer = bms_del_members(required_outer,
    2274             :                                      outerrelids);
    2275             :     /* maintain invariant that required_outer is exactly NULL if empty */
    2276      246354 :     if (bms_is_empty(required_outer))
    2277             :     {
    2278      201250 :         bms_free(required_outer);
    2279      201250 :         required_outer = NULL;
    2280             :     }
    2281      246354 :     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      770540 : calc_non_nestloop_required_outer(Path *outer_path, Path *inner_path)
    2292             : {
    2293      770540 :     Relids      outer_paramrels = PATH_REQ_OUTER(outer_path);
    2294      770540 :     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      770540 :     required_outer = bms_union(outer_paramrels, inner_paramrels);
    2302             :     /* we do not need an explicit test for empty; bms_union gets it right */
    2303      770540 :     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      569384 : create_nestloop_path(PlannerInfo *root,
    2325             :                      RelOptInfo *joinrel,
    2326             :                      JoinType jointype,
    2327             :                      JoinCostWorkspace *workspace,
    2328             :                      JoinPathExtraData *extra,
    2329             :                      Path *outer_path,
    2330             :                      Path *inner_path,
    2331             :                      List *restrict_clauses,
    2332             :                      List *pathkeys,
    2333             :                      Relids required_outer)
    2334             : {
    2335      569384 :     NestPath   *pathnode = makeNode(NestPath);
    2336      569384 :     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      569384 :     if (bms_overlap(inner_req_outer, outer_path->parent->relids))
    2346             :     {
    2347      110614 :         Relids      inner_and_outer = bms_union(inner_path->parent->relids,
    2348             :                                                 inner_req_outer);
    2349      110614 :         List       *jclauses = NIL;
    2350             :         ListCell   *lc;
    2351             : 
    2352      230054 :         foreach(lc, restrict_clauses)
    2353             :         {
    2354      119440 :             RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
    2355             : 
    2356      119440 :             if (!join_clause_is_movable_into(rinfo,
    2357      119440 :                                              inner_path->parent->relids,
    2358             :                                              inner_and_outer))
    2359        7530 :                 jclauses = lappend(jclauses, rinfo);
    2360             :         }
    2361      110614 :         restrict_clauses = jclauses;
    2362             :     }
    2363             : 
    2364      569384 :     pathnode->path.pathtype = T_NestLoop;
    2365      569384 :     pathnode->path.parent = joinrel;
    2366      569384 :     pathnode->path.pathtarget = joinrel->reltarget;
    2367      569384 :     pathnode->path.param_info =
    2368      569384 :         get_joinrel_parampathinfo(root,
    2369             :                                   joinrel,
    2370             :                                   outer_path,
    2371             :                                   inner_path,
    2372             :                                   extra->sjinfo,
    2373             :                                   required_outer,
    2374             :                                   &restrict_clauses);
    2375      569384 :     pathnode->path.parallel_aware = false;
    2376     1649570 :     pathnode->path.parallel_safe = joinrel->consider_parallel &&
    2377     1079558 :         outer_path->parallel_safe && inner_path->parallel_safe;
    2378             :     /* This is a foolish way to estimate parallel_workers, but for now... */
    2379      569384 :     pathnode->path.parallel_workers = outer_path->parallel_workers;
    2380      569384 :     pathnode->path.pathkeys = pathkeys;
    2381      569384 :     pathnode->jointype = jointype;
    2382      569384 :     pathnode->inner_unique = extra->inner_unique;
    2383      569384 :     pathnode->outerjoinpath = outer_path;
    2384      569384 :     pathnode->innerjoinpath = inner_path;
    2385      569384 :     pathnode->joinrestrictinfo = restrict_clauses;
    2386             : 
    2387      569384 :     final_cost_nestloop(root, pathnode, workspace, extra);
    2388             : 
    2389      569384 :     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      116436 : create_mergejoin_path(PlannerInfo *root,
    2413             :                       RelOptInfo *joinrel,
    2414             :                       JoinType jointype,
    2415             :                       JoinCostWorkspace *workspace,
    2416             :                       JoinPathExtraData *extra,
    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      116436 :     MergePath  *pathnode = makeNode(MergePath);
    2427             : 
    2428      116436 :     pathnode->jpath.path.pathtype = T_MergeJoin;
    2429      116436 :     pathnode->jpath.path.parent = joinrel;
    2430      116436 :     pathnode->jpath.path.pathtarget = joinrel->reltarget;
    2431      116436 :     pathnode->jpath.path.param_info =
    2432      116436 :         get_joinrel_parampathinfo(root,
    2433             :                                   joinrel,
    2434             :                                   outer_path,
    2435             :                                   inner_path,
    2436             :                                   extra->sjinfo,
    2437             :                                   required_outer,
    2438             :                                   &restrict_clauses);
    2439      116436 :     pathnode->jpath.path.parallel_aware = false;
    2440      329284 :     pathnode->jpath.path.parallel_safe = joinrel->consider_parallel &&
    2441      212748 :         outer_path->parallel_safe && inner_path->parallel_safe;
    2442             :     /* This is a foolish way to estimate parallel_workers, but for now... */
    2443      116436 :     pathnode->jpath.path.parallel_workers = outer_path->parallel_workers;
    2444      116436 :     pathnode->jpath.path.pathkeys = pathkeys;
    2445      116436 :     pathnode->jpath.jointype = jointype;
    2446      116436 :     pathnode->jpath.inner_unique = extra->inner_unique;
    2447      116436 :     pathnode->jpath.outerjoinpath = outer_path;
    2448      116436 :     pathnode->jpath.innerjoinpath = inner_path;
    2449      116436 :     pathnode->jpath.joinrestrictinfo = restrict_clauses;
    2450      116436 :     pathnode->path_mergeclauses = mergeclauses;
    2451      116436 :     pathnode->outersortkeys = outersortkeys;
    2452      116436 :     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      116436 :     final_cost_mergejoin(root, pathnode, workspace, extra);
    2457             : 
    2458      116436 :     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      133400 : create_hashjoin_path(PlannerInfo *root,
    2479             :                      RelOptInfo *joinrel,
    2480             :                      JoinType jointype,
    2481             :                      JoinCostWorkspace *workspace,
    2482             :                      JoinPathExtraData *extra,
    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      133400 :     HashPath   *pathnode = makeNode(HashPath);
    2491             : 
    2492      133400 :     pathnode->jpath.path.pathtype = T_HashJoin;
    2493      133400 :     pathnode->jpath.path.parent = joinrel;
    2494      133400 :     pathnode->jpath.path.pathtarget = joinrel->reltarget;
    2495      133400 :     pathnode->jpath.path.param_info =
    2496      133400 :         get_joinrel_parampathinfo(root,
    2497             :                                   joinrel,
    2498             :                                   outer_path,
    2499             :                                   inner_path,
    2500             :                                   extra->sjinfo,
    2501             :                                   required_outer,
    2502             :                                   &restrict_clauses);
    2503      133400 :     pathnode->jpath.path.parallel_aware =
    2504      133400 :         joinrel->consider_parallel && parallel_hash;
    2505      376336 :     pathnode->jpath.path.parallel_safe = joinrel->consider_parallel &&
    2506      242614 :         outer_path->parallel_safe && inner_path->parallel_safe;
    2507             :     /* This is a foolish way to estimate parallel_workers, but for now... */
    2508      133400 :     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      133400 :     pathnode->jpath.path.pathkeys = NIL;
    2522      133400 :     pathnode->jpath.jointype = jointype;
    2523      133400 :     pathnode->jpath.inner_unique = extra->inner_unique;
    2524      133400 :     pathnode->jpath.outerjoinpath = outer_path;
    2525      133400 :     pathnode->jpath.innerjoinpath = inner_path;
    2526      133400 :     pathnode->jpath.joinrestrictinfo = restrict_clauses;
    2527      133400 :     pathnode->path_hashclauses = hashclauses;
    2528             :     /* final_cost_hashjoin will fill in pathnode->num_batches */
    2529             : 
    2530      133400 :     final_cost_hashjoin(root, pathnode, workspace, extra);
    2531             : 
    2532      133400 :     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      225894 : create_projection_path(PlannerInfo *root,
    2545             :                        RelOptInfo *rel,
    2546             :                        Path *subpath,
    2547             :                        PathTarget *target)
    2548             : {
    2549      225894 :     ProjectionPath *pathnode = makeNode(ProjectionPath);
    2550      225894 :     PathTarget *oldtarget = subpath->pathtarget;
    2551             : 
    2552      225894 :     pathnode->path.pathtype = T_Result;
    2553      225894 :     pathnode->path.parent = rel;
    2554      225894 :     pathnode->path.pathtarget = target;
    2555             :     /* For now, assume we are above any joins, so no parameterization */
    2556      225894 :     pathnode->path.param_info = NULL;
    2557      225894 :     pathnode->path.parallel_aware = false;
    2558      485010 :     pathnode->path.parallel_safe = rel->consider_parallel &&
    2559      258484 :         subpath->parallel_safe &&
    2560       32590 :         is_parallel_safe(root, (Node *) target->exprs);
    2561      225894 :     pathnode->path.parallel_workers = subpath->parallel_workers;
    2562             :     /* Projection does not change the sort order */
    2563      225894 :     pathnode->path.pathkeys = subpath->pathkeys;
    2564             : 
    2565      225894 :     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      234266 :     if (is_projection_capable_path(subpath) ||
    2577        8372 :         equal(oldtarget->exprs, target->exprs))
    2578             :     {
    2579             :         /* No separate Result node needed */
    2580      224776 :         pathnode->dummypp = true;
    2581             : 
    2582             :         /*
    2583             :          * Set cost of plan as subpath's cost, adjusted for tlist replacement.
    2584             :          */
    2585      224776 :         pathnode->path.rows = subpath->rows;
    2586      449552 :         pathnode->path.startup_cost = subpath->startup_cost +
    2587      224776 :             (target->cost.startup - oldtarget->cost.startup);
    2588      674328 :         pathnode->path.total_cost = subpath->total_cost +
    2589      449552 :             (target->cost.startup - oldtarget->cost.startup) +
    2590      224776 :             (target->cost.per_tuple - oldtarget->cost.per_tuple) * subpath->rows;
    2591             :     }
    2592             :     else
    2593             :     {
    2594             :         /* We really do need the Result node */
    2595        1118 :         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        1118 :         pathnode->path.rows = subpath->rows;
    2602        2236 :         pathnode->path.startup_cost = subpath->startup_cost +
    2603        1118 :             target->cost.startup;
    2604        3354 :         pathnode->path.total_cost = subpath->total_cost +
    2605        2236 :             target->cost.startup +
    2606        1118 :             (cpu_tuple_cost + target->cost.per_tuple) * subpath->rows;
    2607             :     }
    2608             : 
    2609      225894 :     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       14392 : 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       14392 :     if (!is_projection_capable_path(path))
    2647        6812 :         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        7580 :     oldcost = path->pathtarget->cost;
    2654        7580 :     path->pathtarget = target;
    2655             : 
    2656        7580 :     path->startup_cost += target->cost.startup - oldcost.startup;
    2657       15160 :     path->total_cost += target->cost.startup - oldcost.startup +
    2658        7580 :         (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        8758 :     if ((IsA(path, GatherPath) ||IsA(path, GatherMergePath)) &&
    2667        1178 :         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        2356 :         if (IsA(path, GatherPath))
    2680             :         {
    2681           0 :             GatherPath *gpath = (GatherPath *) path;
    2682             : 
    2683           0 :             gpath->subpath = (Path *)
    2684           0 :                 create_projection_path(root,
    2685           0 :                                        gpath->subpath->parent,
    2686             :                                        gpath->subpath,
    2687             :                                        target);
    2688             :         }
    2689             :         else
    2690             :         {
    2691        1178 :             GatherMergePath *gmpath = (GatherMergePath *) path;
    2692             : 
    2693        1178 :             gmpath->subpath = (Path *)
    2694        2356 :                 create_projection_path(root,
    2695        1178 :                                        gmpath->subpath->parent,
    2696             :                                        gmpath->subpath,
    2697             :                                        target);
    2698             :         }
    2699             :     }
    2700        9198 :     else if (path->parallel_safe &&
    2701        2796 :              !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           8 :         path->parallel_safe = false;
    2709             :     }
    2710             : 
    2711        7580 :     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        3316 : create_set_projection_path(PlannerInfo *root,
    2725             :                            RelOptInfo *rel,
    2726             :                            Path *subpath,
    2727             :                            PathTarget *target)
    2728             : {
    2729        3316 :     ProjectSetPath *pathnode = makeNode(ProjectSetPath);
    2730             :     double      tlist_rows;
    2731             :     ListCell   *lc;
    2732             : 
    2733        3316 :     pathnode->path.pathtype = T_ProjectSet;
    2734        3316 :     pathnode->path.parent = rel;
    2735        3316 :     pathnode->path.pathtarget = target;
    2736             :     /* For now, assume we are above any joins, so no parameterization */
    2737        3316 :     pathnode->path.param_info = NULL;
    2738        3316 :     pathnode->path.parallel_aware = false;
    2739        7050 :     pathnode->path.parallel_safe = rel->consider_parallel &&
    2740        3714 :         subpath->parallel_safe &&
    2741         398 :         is_parallel_safe(root, (Node *) target->exprs);
    2742        3316 :     pathnode->path.parallel_workers = subpath->parallel_workers;
    2743             :     /* Projection does not change the sort order XXX? */
    2744        3316 :     pathnode->path.pathkeys = subpath->pathkeys;
    2745             : 
    2746        3316 :     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        3316 :     tlist_rows = 1;
    2753        7766 :     foreach(lc, target->exprs)
    2754             :     {
    2755        4450 :         Node       *node = (Node *) lfirst(lc);
    2756             :         double      itemrows;
    2757             : 
    2758        4450 :         itemrows = expression_returns_set_rows(root, node);
    2759        4450 :         if (tlist_rows < itemrows)
    2760        3186 :             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        3316 :     pathnode->path.rows = subpath->rows * tlist_rows;
    2770        6632 :     pathnode->path.startup_cost = subpath->startup_cost +
    2771        3316 :         target->cost.startup;
    2772        9948 :     pathnode->path.total_cost = subpath->total_cost +
    2773        6632 :         target->cost.startup +
    2774        6632 :         (cpu_tuple_cost + target->cost.per_tuple) * subpath->rows +
    2775        3316 :         (pathnode->path.rows - subpath->rows) * cpu_tuple_cost / 2;
    2776             : 
    2777        3316 :     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       35216 : create_sort_path(PlannerInfo *root,
    2792             :                  RelOptInfo *rel,
    2793             :                  Path *subpath,
    2794             :                  List *pathkeys,
    2795             :                  double limit_tuples)
    2796             : {
    2797       35216 :     SortPath   *pathnode = makeNode(SortPath);
    2798             : 
    2799       35216 :     pathnode->path.pathtype = T_Sort;
    2800       35216 :     pathnode->path.parent = rel;
    2801             :     /* Sort doesn't project, so use source path's pathtarget */
    2802       35216 :     pathnode->path.pathtarget = subpath->pathtarget;
    2803             :     /* For now, assume we are above any joins, so no parameterization */
    2804       35216 :     pathnode->path.param_info = NULL;
    2805       35216 :     pathnode->path.parallel_aware = false;
    2806       53264 :     pathnode->path.parallel_safe = rel->consider_parallel &&
    2807       18048 :         subpath->parallel_safe;
    2808       35216 :     pathnode->path.parallel_workers = subpath->parallel_workers;
    2809       35216 :     pathnode->path.pathkeys = pathkeys;
    2810             : 
    2811       35216 :     pathnode->subpath = subpath;
    2812             : 
    2813       70432 :     cost_sort(&pathnode->path, root, pathkeys,
    2814             :               subpath->total_cost,
    2815             :               subpath->rows,
    2816       35216 :               subpath->pathtarget->width,
    2817             :               0.0,              /* XXX comparison_cost shouldn't be 0? */
    2818             :               work_mem, limit_tuples);
    2819             : 
    2820       35216 :     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         432 : create_group_path(PlannerInfo *root,
    2836             :                   RelOptInfo *rel,
    2837             :                   Path *subpath,
    2838             :                   List *groupClause,
    2839             :                   List *qual,
    2840             :                   double numGroups)
    2841             : {
    2842         432 :     GroupPath  *pathnode = makeNode(GroupPath);
    2843         432 :     PathTarget *target = rel->reltarget;
    2844             : 
    2845         432 :     pathnode->path.pathtype = T_Group;
    2846         432 :     pathnode->path.parent = rel;
    2847         432 :     pathnode->path.pathtarget = target;
    2848             :     /* For now, assume we are above any joins, so no parameterization */
    2849         432 :     pathnode->path.param_info = NULL;
    2850         432 :     pathnode->path.parallel_aware = false;
    2851         654 :     pathnode->path.parallel_safe = rel->consider_parallel &&
    2852         222 :         subpath->parallel_safe;
    2853         432 :     pathnode->path.parallel_workers = subpath->parallel_workers;
    2854             :     /* Group doesn't change sort ordering */
    2855         432 :     pathnode->path.pathkeys = subpath->pathkeys;
    2856             : 
    2857         432 :     pathnode->subpath = subpath;
    2858             : 
    2859         432 :     pathnode->groupClause = groupClause;
    2860         432 :     pathnode->qual = qual;
    2861             : 
    2862         432 :     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         432 :     pathnode->path.startup_cost += target->cost.startup;
    2871         864 :     pathnode->path.total_cost += target->cost.startup +
    2872         432 :         target->cost.per_tuple * pathnode->path.rows;
    2873             : 
    2874         432 :     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         526 : create_upper_unique_path(PlannerInfo *root,
    2895             :                          RelOptInfo *rel,
    2896             :                          Path *subpath,
    2897             :                          int numCols,
    2898             :                          double numGroups)
    2899             : {
    2900         526 :     UpperUniquePath *pathnode = makeNode(UpperUniquePath);
    2901             : 
    2902         526 :     pathnode->path.pathtype = T_Unique;
    2903         526 :     pathnode->path.parent = rel;
    2904             :     /* Unique doesn't project, so use source path's pathtarget */
    2905         526 :     pathnode->path.pathtarget = subpath->pathtarget;
    2906             :     /* For now, assume we are above any joins, so no parameterization */
    2907         526 :     pathnode->path.param_info = NULL;
    2908         526 :     pathnode->path.parallel_aware = false;
    2909         878 :     pathnode->path.parallel_safe = rel->consider_parallel &&
    2910         352 :         subpath->parallel_safe;
    2911         526 :     pathnode->path.parallel_workers = subpath->parallel_workers;
    2912             :     /* Unique doesn't change the input ordering */
    2913         526 :     pathnode->path.pathkeys = subpath->pathkeys;
    2914             : 
    2915         526 :     pathnode->subpath = subpath;
    2916         526 :     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         526 :     pathnode->path.startup_cost = subpath->startup_cost;
    2924        1052 :     pathnode->path.total_cost = subpath->total_cost +
    2925         526 :         cpu_operator_cost * subpath->rows * numCols;
    2926         526 :     pathnode->path.rows = numGroups;
    2927             : 
    2928         526 :     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       31608 : 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       31608 :     AggPath    *pathnode = makeNode(AggPath);
    2958             : 
    2959       31608 :     pathnode->path.pathtype = T_Agg;
    2960       31608 :     pathnode->path.parent = rel;
    2961       31608 :     pathnode->path.pathtarget = target;
    2962             :     /* For now, assume we are above any joins, so no parameterization */
    2963       31608 :     pathnode->path.param_info = NULL;
    2964       31608 :     pathnode->path.parallel_aware = false;
    2965       47510 :     pathnode->path.parallel_safe = rel->consider_parallel &&
    2966       15902 :         subpath->parallel_safe;
    2967       31608 :     pathnode->path.parallel_workers = subpath->parallel_workers;
    2968       31608 :     if (aggstrategy == AGG_SORTED)
    2969        4062 :         pathnode->path.pathkeys = subpath->pathkeys;  /* preserves order */
    2970             :     else
    2971       27546 :         pathnode->path.pathkeys = NIL;   /* output is unordered */
    2972       31608 :     pathnode->subpath = subpath;
    2973             : 
    2974       31608 :     pathnode->aggstrategy = aggstrategy;
    2975       31608 :     pathnode->aggsplit = aggsplit;
    2976       31608 :     pathnode->numGroups = numGroups;
    2977       31608 :     pathnode->groupClause = groupClause;
    2978       31608 :     pathnode->qual = qual;
    2979             : 
    2980       31608 :     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       31608 :     pathnode->path.startup_cost += target->cost.startup;
    2989       63216 :     pathnode->path.total_cost += target->cost.startup +
    2990       31608 :         target->cost.per_tuple * pathnode->path.rows;
    2991             : 
    2992       31608 :     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         948 : 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         948 :     GroupingSetsPath *pathnode = makeNode(GroupingSetsPath);
    3022         948 :     PathTarget *target = rel->reltarget;
    3023             :     ListCell   *lc;
    3024         948 :     bool        is_first = true;
    3025         948 :     bool        is_first_sort = true;
    3026             : 
    3027             :     /* The topmost generated Plan node will be an Agg */
    3028         948 :     pathnode->path.pathtype = T_Agg;
    3029         948 :     pathnode->path.parent = rel;
    3030         948 :     pathnode->path.pathtarget = target;
    3031         948 :     pathnode->path.param_info = subpath->param_info;
    3032         948 :     pathnode->path.parallel_aware = false;
    3033        1360 :     pathnode->path.parallel_safe = rel->consider_parallel &&
    3034         412 :         subpath->parallel_safe;
    3035         948 :     pathnode->path.parallel_workers = subpath->parallel_workers;
    3036         948 :     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        1364 :     if (aggstrategy == AGG_SORTED &&
    3043         644 :         list_length(rollups) == 1 &&
    3044         228 :         ((RollupData *) linitial(rollups))->groupClause == NIL)
    3045          28 :         aggstrategy = AGG_PLAIN;
    3046             : 
    3047        1360 :     if (aggstrategy == AGG_MIXED &&
    3048         412 :         list_length(rollups) == 1)
    3049           0 :         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         948 :     if (aggstrategy == AGG_SORTED && list_length(rollups) == 1)
    3057         200 :         pathnode->path.pathkeys = root->group_pathkeys;
    3058             :     else
    3059         748 :         pathnode->path.pathkeys = NIL;
    3060             : 
    3061         948 :     pathnode->aggstrategy = aggstrategy;
    3062         948 :     pathnode->rollups = rollups;
    3063         948 :     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        3236 :     foreach(lc, rollups)
    3070             :     {
    3071        2288 :         RollupData *rollup = lfirst(lc);
    3072        2288 :         List       *gsets = rollup->gsets;
    3073        2288 :         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        2288 :         if (is_first)
    3086             :         {
    3087         948 :             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         948 :             is_first = false;
    3097         948 :             if (!rollup->is_hashed)
    3098         416 :                 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        1340 :             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        2000 :                 cost_agg(&agg_path, root,
    3112        1000 :                          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        2000 :                 if (!rollup->is_hashed)
    3120         412 :                     is_first_sort = false;
    3121             :             }
    3122             :             else
    3123             :             {
    3124             :                 /* Account for cost of sort, but don't charge input cost again */
    3125         680 :                 cost_sort(&sort_path, root, NIL,
    3126             :                           0.0,
    3127             :                           subpath->rows,
    3128         340 :                           subpath->pathtarget->width,
    3129             :                           0.0,
    3130             :                           work_mem,
    3131             :                           -1.0);
    3132             : 
    3133             :                 /* Account for cost of aggregation */
    3134             : 
    3135         340 :                 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        1340 :             pathnode->path.total_cost += agg_path.total_cost;
    3147        1340 :             pathnode->path.rows += agg_path.rows;
    3148             :         }
    3149             :     }
    3150             : 
    3151             :     /* add tlist eval cost for each output row */
    3152         948 :     pathnode->path.startup_cost += target->cost.startup;
    3153        1896 :     pathnode->path.total_cost += target->cost.startup +
    3154         948 :         target->cost.per_tuple * pathnode->path.rows;
    3155             : 
    3156         948 :     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         398 : create_minmaxagg_path(PlannerInfo *root,
    3170             :                       RelOptInfo *rel,
    3171             :                       PathTarget *target,
    3172             :                       List *mmaggregates,
    3173             :                       List *quals)
    3174             : {
    3175         398 :     MinMaxAggPath *pathnode = makeNode(MinMaxAggPath);
    3176             :     Cost        initplan_cost;
    3177             :     ListCell   *lc;
    3178             : 
    3179             :     /* The topmost generated Plan node will be a Result */
    3180         398 :     pathnode->path.pathtype = T_Result;
    3181         398 :     pathnode->path.parent = rel;
    3182         398 :     pathnode->path.pathtarget = target;
    3183             :     /* For now, assume we are above any joins, so no parameterization */
    3184         398 :     pathnode->path.param_info = NULL;
    3185         398 :     pathnode->path.parallel_aware = false;
    3186             :     /* A MinMaxAggPath implies use of subplans, so cannot be parallel-safe */
    3187         398 :     pathnode->path.parallel_safe = false;
    3188         398 :     pathnode->path.parallel_workers = 0;
    3189             :     /* Result is one unordered row */
    3190         398 :     pathnode->path.rows = 1;
    3191         398 :     pathnode->path.pathkeys = NIL;
    3192             : 
    3193         398 :     pathnode->mmaggregates = mmaggregates;
    3194         398 :     pathnode->quals = quals;
    3195             : 
    3196             :     /* Calculate cost of all the initplans ... */
    3197         398 :     initplan_cost = 0;
    3198         820 :     foreach(lc, mmaggregates)
    3199             :     {
    3200         422 :         MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
    3201             : 
    3202         422 :         initplan_cost += mminfo->pathcost;
    3203             :     }
    3204             : 
    3205             :     /* add tlist eval cost for each output row, plus cpu_tuple_cost */
    3206         398 :     pathnode->path.startup_cost = initplan_cost + target->cost.startup;
    3207        1194 :     pathnode->path.total_cost = initplan_cost + target->cost.startup +
    3208         796 :         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         398 :     if (quals)
    3215             :     {
    3216             :         QualCost    qual_cost;
    3217             : 
    3218           0 :         cost_qual_eval(&qual_cost, quals, root);
    3219           0 :         pathnode->path.startup_cost += qual_cost.startup;
    3220           0 :         pathnode->path.total_cost += qual_cost.startup + qual_cost.per_tuple;
    3221             :     }
    3222             : 
    3223         398 :     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        1160 : create_windowagg_path(PlannerInfo *root,
    3241             :                       RelOptInfo *rel,
    3242             :                       Path *subpath,
    3243             :                       PathTarget *target,
    3244             :                       List *windowFuncs,
    3245             :                       WindowClause *winclause)
    3246             : {
    3247        1160 :     WindowAggPath *pathnode = makeNode(WindowAggPath);
    3248             : 
    3249        1160 :     pathnode->path.pathtype = T_WindowAgg;
    3250        1160 :     pathnode->path.parent = rel;
    3251        1160 :     pathnode->path.pathtarget = target;
    3252             :     /* For now, assume we are above any joins, so no parameterization */
    3253        1160 :     pathnode->path.param_info = NULL;
    3254        1160 :     pathnode->path.parallel_aware = false;
    3255        1160 :     pathnode->path.parallel_safe = rel->consider_parallel &&
    3256           0 :         subpath->parallel_safe;
    3257        1160 :     pathnode->path.parallel_workers = subpath->parallel_workers;
    3258             :     /* WindowAgg preserves the input sort order */
    3259        1160 :     pathnode->path.pathkeys = subpath->pathkeys;
    3260             : 
    3261        1160 :     pathnode->subpath = subpath;
    3262        1160 :     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        3480 :     cost_windowagg(&pathnode->path, root,
    3271             :                    windowFuncs,
    3272        1160 :                    list_length(winclause->partitionClause),
    3273        1160 :                    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        1160 :     pathnode->path.startup_cost += target->cost.startup;
    3280        2320 :     pathnode->path.total_cost += target->cost.startup +
    3281        1160 :         target->cost.per_tuple * pathnode->path.rows;
    3282             : 
    3283        1160 :     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         208 : 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         208 :     SetOpPath  *pathnode = makeNode(SetOpPath);
    3314             : 
    3315         208 :     pathnode->path.pathtype = T_SetOp;
    3316         208 :     pathnode->path.parent = rel;
    3317             :     /* SetOp doesn't project, so use source path's pathtarget */
    3318         208 :     pathnode->path.pathtarget = subpath->pathtarget;
    3319             :     /* For now, assume we are above any joins, so no parameterization */
    3320         208 :     pathnode->path.param_info = NULL;
    3321         208 :     pathnode->path.parallel_aware = false;
    3322         208 :     pathnode->path.parallel_safe = rel->consider_parallel &&
    3323           0 :         subpath->parallel_safe;
    3324         208 :     pathnode->path.parallel_workers = subpath->parallel_workers;
    3325             :     /* SetOp preserves the input sort order if in sort mode */
    3326         208 :     pathnode->path.pathkeys =
    3327         208 :         (strategy == SETOP_SORTED) ? subpath->pathkeys : NIL;
    3328             : 
    3329         208 :     pathnode->subpath = subpath;
    3330         208 :     pathnode->cmd = cmd;
    3331         208 :     pathnode->strategy = strategy;
    3332         208 :     pathnode->distinctList = distinctList;
    3333         208 :     pathnode->flagColIdx = flagColIdx;
    3334         208 :     pathnode->firstFlag = firstFlag;
    3335         208 :     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         208 :     pathnode->path.startup_cost = subpath->startup_cost;
    3342         416 :     pathnode->path.total_cost = subpath->total_cost +
    3343         208 :         cpu_operator_cost * subpath->rows * list_length(distinctList);
    3344         208 :     pathnode->path.rows = outputRows;
    3345             : 
    3346         208 :     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         326 : 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         326 :     RecursiveUnionPath *pathnode = makeNode(RecursiveUnionPath);
    3374             : 
    3375         326 :     pathnode->path.pathtype = T_RecursiveUnion;
    3376         326 :     pathnode->path.parent = rel;
    3377         326 :     pathnode->path.pathtarget = target;
    3378             :     /* For now, assume we are above any joins, so no parameterization */
    3379         326 :     pathnode->path.param_info = NULL;
    3380         326 :     pathnode->path.parallel_aware = false;
    3381         652 :     pathnode->path.parallel_safe = rel->consider_parallel &&
    3382         326 :         leftpath->parallel_safe && rightpath->parallel_safe;
    3383             :     /* Foolish, but we'll do it like joins for now: */
    3384         326 :     pathnode->path.parallel_workers = leftpath->parallel_workers;
    3385             :     /* RecursiveUnion result is always unsorted */
    3386         326 :     pathnode->path.pathkeys = NIL;
    3387             : 
    3388         326 :     pathnode->leftpath = leftpath;
    3389         326 :     pathnode->rightpath = rightpath;
    3390         326 :     pathnode->distinctList = distinctList;
    3391         326 :     pathnode->wtParam = wtParam;
    3392         326 :     pathnode->numGroups = numGroups;
    3393             : 
    3394         326 :     cost_recursive_union(&pathnode->path, leftpath, rightpath);
    3395             : 
    3396         326 :     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        5260 : create_lockrows_path(PlannerInfo *root, RelOptInfo *rel,
    3410             :                      Path *subpath, List *rowMarks, int epqParam)
    3411             : {
    3412        5260 :     LockRowsPath *pathnode = makeNode(LockRowsPath);
    3413             : 
    3414        5260 :     pathnode->path.pathtype = T_LockRows;
    3415        5260 :     pathnode->path.parent = rel;
    3416             :     /* LockRows doesn't project, so use source path's pathtarget */
    3417        5260 :     pathnode->path.pathtarget = subpath->pathtarget;
    3418             :     /* For now, assume we are above any joins, so no parameterization */
    3419        5260 :     pathnode->path.param_info = NULL;
    3420        5260 :     pathnode->path.parallel_aware = false;
    3421        5260 :     pathnode->path.parallel_safe = false;
    3422        5260 :     pathnode->path.parallel_workers = 0;
    3423        5260 :     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        5260 :     pathnode->path.pathkeys = NIL;
    3430             : 
    3431        5260 :     pathnode->subpath = subpath;
    3432        5260 :     pathnode->rowMarks = rowMarks;
    3433        5260 :     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        5260 :     pathnode->path.startup_cost = subpath->startup_cost;
    3441       10520 :     pathnode->path.total_cost = subpath->total_cost +
    3442        5260 :         cpu_tuple_cost * subpath->rows;
    3443             : 
    3444        5260 :     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       72748 : 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       72748 :     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       72748 :     pathnode->path.pathtype = T_ModifyTable;
    3490       72748 :     pathnode->path.parent = rel;
    3491             :     /* pathtarget is not interesting, just make it minimally valid */
    3492       72748 :     pathnode->path.pathtarget = rel->reltarget;
    3493             :     /* For now, assume we are above any joins, so no parameterization */
    3494       72748 :     pathnode->path.param_info = NULL;
    3495       72748 :     pathnode->path.parallel_aware = false;
    3496       72748 :     pathnode->path.parallel_safe = false;
    3497       72748 :     pathnode->path.parallel_workers = 0;
    3498       72748 :     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       72748 :     pathnode->path.startup_cost = 0;
    3511       72748 :     pathnode->path.total_cost = 0;
    3512       72748 :     pathnode->path.rows = 0;
    3513       72748 :     total_size = 0;
    3514      146658 :     foreach(lc, subpaths)
    3515             :     {
    3516       73910 :         Path       *subpath = (Path *) lfirst(lc);
    3517             : 
    3518       73910 :         if (lc == list_head(subpaths))  /* first node? */
    3519       72748 :             pathnode->path.startup_cost = subpath->startup_cost;
    3520       73910 :         pathnode->path.total_cost += subpath->total_cost;
    3521       73910 :         pathnode->path.rows += subpath->rows;
    3522       73910 :         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       72748 :     if (pathnode->path.rows > 0)
    3532       72680 :         total_size /= pathnode->path.rows;
    3533       72748 :     pathnode->path.pathtarget->width = rint(total_size);
    3534             : 
    3535       72748 :     pathnode->operation = operation;
    3536       72748 :     pathnode->canSetTag = canSetTag;
    3537       72748 :     pathnode->nominalRelation = nominalRelation;
    3538       72748 :     pathnode->rootRelation = rootRelation;
    3539       72748 :     pathnode->partColsUpdated = partColsUpdated;
    3540       72748 :     pathnode->resultRelations = resultRelations;
    3541       72748 :     pathnode->subpaths = subpaths;
    3542       72748 :     pathnode->subroots = subroots;
    3543       72748 :     pathnode->withCheckOptionLists = withCheckOptionLists;
    3544       72748 :     pathnode->returningLists = returningLists;
    3545       72748 :     pathnode->rowMarks = rowMarks;
    3546       72748 :     pathnode->onconflict = onconflict;
    3547       72748 :     pathnode->epqParam = epqParam;
    3548             : 
    3549       72748 :     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        3370 : create_limit_path(PlannerInfo *root, RelOptInfo *rel,
    3571             :                   Path *subpath,
    3572             :                   Node *limitOffset, Node *limitCount,
    3573             :                   int64 offset_est, int64 count_est)
    3574             : {
    3575        3370 :     LimitPath  *pathnode = makeNode(LimitPath);
    3576             : 
    3577        3370 :     pathnode->path.pathtype = T_Limit;
    3578        3370 :     pathnode->path.parent = rel;
    3579             :     /* Limit doesn't project, so use source path's pathtarget */
    3580        3370 :     pathnode->path.pathtarget = subpath->pathtarget;
    3581             :     /* For now, assume we are above any joins, so no parameterization */
    3582        3370 :     pathnode->path.param_info = NULL;
    3583        3370 :     pathnode->path.parallel_aware = false;
    3584        4460 :     pathnode->path.parallel_safe = rel->consider_parallel &&
    3585        1090 :         subpath->parallel_safe;
    3586        3370 :     pathnode->path.parallel_workers = subpath->parallel_workers;
    3587        3370 :     pathnode->path.rows = subpath->rows;
    3588        3370 :     pathnode->path.startup_cost = subpath->startup_cost;
    3589        3370 :     pathnode->path.total_cost = subpath->total_cost;
    3590        3370 :     pathnode->path.pathkeys = subpath->pathkeys;
    3591        3370 :     pathnode->subpath = subpath;
    3592        3370 :     pathnode->limitOffset = limitOffset;
    3593        3370 :     pathnode->limitCount = limitCount;
    3594             : 
    3595             :     /*
    3596             :      * Adjust the output rows count and costs according to the offset/limit.
    3597             :      */
    3598        3370 :     adjust_limit_rows_costs(&pathnode->path.rows,
    3599             :                             &pathnode->path.startup_cost,
    3600             :                             &pathnode->path.total_cost,
    3601             :                             offset_est, count_est);
    3602             : 
    3603        3370 :     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        3496 : 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        3496 :     double      input_rows = *rows;
    3630        3496 :     Cost        input_startup_cost = *startup_cost;
    3631        3496 :     Cost        input_total_cost = *total_cost;
    3632             : 
    3633        3496 :     if (offset_est != 0)
    3634             :     {
    3635             :         double      offset_rows;
    3636             : 
    3637         564 :         if (offset_est > 0)
    3638         548 :             offset_rows = (double) offset_est;
    3639             :         else
    3640          16 :             offset_rows = clamp_row_est(input_rows * 0.10);
    3641         564 :         if (offset_rows > *rows)
    3642          20 :             offset_rows = *rows;
    3643         564 :         if (input_rows > 0)
    3644        1128 :             *startup_cost +=
    3645         564 :                 (input_total_cost - input_startup_cost)
    3646         564 :                 * offset_rows / input_rows;
    3647         564 :         *rows -= offset_rows;
    3648         564 :         if (*rows < 1)
    3649          20 :             *rows = 1;
    3650             :     }
    3651             : 
    3652        3496 :     if (count_est != 0)
    3653             :     {
    3654             :         double      count_rows;
    3655             : 
    3656        3480 :         if (count_est > 0)
    3657        3476 :             count_rows = (double) count_est;
    3658             :         else
    3659           4 :             count_rows = clamp_row_est(input_rows * 0.10);
    3660        3480 :         if (count_rows > *rows)
    3661          42 :             count_rows = *rows;
    3662        3480 :         if (input_rows > 0)
    3663        6960 :             *total_cost = *startup_cost +
    3664        3480 :                 (input_total_cost - input_startup_cost)
    3665        3480 :                 * count_rows / input_rows;
    3666        3480 :         *rows = count_rows;
    3667        3480 :         if (*rows < 1)
    3668           0 :             *rows = 1;
    3669             :     }
    3670        3496 : }
    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         188 : reparameterize_path(PlannerInfo *root, Path *path,
    3692             :                     Relids required_outer,
    3693             :                     double loop_count)
    3694             : {
    3695         188 :     RelOptInfo *rel = path->parent;
    3696             : 
    3697             :     /* Can only increase, not decrease, path's parameterization */
    3698         188 :     if (!bms_is_subset(PATH_REQ_OUTER(path), required_outer))
    3699           0 :         return NULL;
    3700         188 :     switch (path->pathtype)
    3701             :     {
    3702             :         case T_SeqScan:
    3703         124 :             return create_seqscan_path(root, rel, required_outer, 0);
    3704             :         case T_SampleScan:
    3705           0 :             return (Path *) create_samplescan_path(root, rel, required_outer);
    3706             :         case T_IndexScan:
    3707             :         case T_IndexOnlyScan:
    3708             :             {
    3709           0 :                 IndexPath  *ipath = (IndexPath *) path;
    3710           0 :                 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           0 :                 memcpy(newpath, ipath, sizeof(IndexPath));
    3720           0 :                 newpath->path.param_info =
    3721           0 :                     get_baserel_parampathinfo(root, rel, required_outer);
    3722           0 :                 cost_index(newpath, root, loop_count, false);
    3723           0 :                 return (Path *) newpath;
    3724             :             }
    3725             :         case T_BitmapHeapScan:
    3726             :             {
    3727           0 :                 BitmapHeapPath *bpath = (BitmapHeapPath *) path;
    3728             : 
    3729           0 :                 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           0 :                 SubqueryScanPath *spath = (SubqueryScanPath *) path;
    3738             : 
    3739           0 :                 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          32 :             if (IsA(path, Path))
    3748          32 :                 return create_resultscan_path(root, rel, required_outer);
    3749           0 :             break;
    3750             :         case T_Append:
    3751             :             {
    3752           0 :                 AppendPath *apath = (AppendPath *) path;
    3753           0 :                 List       *childpaths = NIL;
    3754           0 :                 List       *partialpaths = NIL;
    3755             :                 int         i;
    3756             :                 ListCell   *lc;
    3757             : 
    3758             :                 /* Reparameterize the children */
    3759           0 :                 i = 0;
    3760           0 :                 foreach(lc, apath->subpaths)
    3761             :                 {
    3762           0 :                     Path       *spath = (Path *) lfirst(lc);
    3763             : 
    3764           0 :                     spath = reparameterize_path(root, spath,
    3765             :                                                 required_outer,
    3766             :                                                 loop_count);
    3767           0 :                     if (spath == NULL)
    3768           0 :                         return NULL;
    3769             :                     /* We have to re-split the regular and partial paths */
    3770           0 :                     if (i < apath->first_partial_path)
    3771           0 :                         childpaths = lappend(childpaths, spath);
    3772             :                     else
    3773           0 :                         partialpaths = lappend(partialpaths, spath);
    3774           0 :                     i++;
    3775             :                 }
    3776           0 :                 return (Path *)
    3777           0 :                     create_append_path(root, rel, childpaths, partialpaths,
    3778             :                                        apath->path.pathkeys, required_outer,
    3779             :                                        apath->path.parallel_workers,
    3780           0 :                                        apath->path.parallel_aware,
    3781             :                                        apath->partitioned_rels,
    3782             :                                        -1);
    3783             :             }
    3784             :         default:
    3785          32 :             break;
    3786             :     }
    3787          32 :     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        2164 : 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        4304 :     if (!path->param_info ||
    3851        2140 :         !bms_overlap(PATH_REQ_OUTER(path), child_rel->top_parent_relids))
    3852         128 :         return path;
    3853             : 
    3854             :     /* Reparameterize a copy of given path. */
    3855        2036 :     switch (nodeTag(path))
    3856             :     {
    3857             :         case T_Path:
    3858         288 :             FLAT_COPY_PATH(new_path, path, Path);
    3859         288 :             break;
    3860             : 
    3861             :         case T_IndexPath:
    3862             :             {
    3863             :                 IndexPath  *ipath;
    3864             : 
    3865        1280 :                 FLAT_COPY_PATH(ipath, path, IndexPath);
    3866        1280 :                 ADJUST_CHILD_ATTRS(ipath->indexclauses);
    3867        1280 :                 new_path = (Path *) ipath;
    3868             :             }
    3869        1280 :             break;
    3870             : 
    3871             :         case T_BitmapHeapPath:
    3872             :             {
    3873             :                 BitmapHeapPath *bhpath;
    3874             : 
    3875           0 :                 FLAT_COPY_PATH(bhpath, path, BitmapHeapPath);
    3876           0 :                 REPARAMETERIZE_CHILD_PATH(bhpath->bitmapqual);
    3877           0 :                 new_path = (Path *) bhpath;
    3878             :             }
    3879           0 :             break;
    3880             : 
    3881             :         case T_BitmapAndPath:
    3882             :             {
    3883             :                 BitmapAndPath *bapath;
    3884             : 
    3885           0 :                 FLAT_COPY_PATH(bapath, path, BitmapAndPath);
    3886           0 :                 REPARAMETERIZE_CHILD_PATH_LIST(bapath->bitmapquals);
    3887           0 :                 new_path = (Path *) bapath;
    3888             :             }
    3889           0 :             break;
    3890             : 
    3891             :         case T_BitmapOrPath:
    3892             :             {
    3893             :                 BitmapOrPath *bopath;
    3894             : 
    3895           0 :                 FLAT_COPY_PATH(bopath, path, BitmapOrPath);
    3896           0 :                 REPARAMETERIZE_CHILD_PATH_LIST(bopath->bitmapquals);
    3897           0 :                 new_path = (Path *) bopath;
    3898             :             }
    3899           0 :             break;
    3900             : 
    3901             :         case T_TidPath:
    3902             :             {
    3903             :                 TidPath    *tpath;
    3904             : 
    3905           0 :                 FLAT_COPY_PATH(tpath, path, TidPath);
    3906           0 :                 ADJUST_CHILD_ATTRS(tpath->tidquals);
    3907           0 :                 new_path = (Path *) tpath;
    3908             :             }
    3909           0 :             break;
    3910             : 
    3911             :         case T_ForeignPath:
    3912             :             {
    3913             :                 ForeignPath *fpath;
    3914             :                 ReparameterizeForeignPathByChild_function rfpc_func;
    3915             : 
    3916          52 :                 FLAT_COPY_PATH(fpath, path, ForeignPath);
    3917          52 :                 if (fpath->fdw_outerpath)
    3918           0 :                     REPARAMETERIZE_CHILD_PATH(fpath->fdw_outerpath);
    3919             : 
    3920             :                 /* Hand over to FDW if needed. */
    3921          52 :                 rfpc_func =
    3922          52 :                     path->parent->fdwroutine->ReparameterizeForeignPathByChild;
    3923          52 :                 if (rfpc_func)
    3924           0 :                     fpath->fdw_private = rfpc_func(root, fpath->fdw_private,
    3925             :                                                    child_rel);
    3926          52 :                 new_path = (Path *) fpath;
    3927             :             }
    3928          52 :             break;
    3929             : 
    3930             :         case T_CustomPath:
    3931             :             {
    3932             :                 CustomPath *cpath;
    3933             : 
    3934           0 :                 FLAT_COPY_PATH(cpath, path, CustomPath);
    3935           0 :                 REPARAMETERIZE_CHILD_PATH_LIST(cpath->custom_paths);
    3936           0 :                 if (cpath->methods &&
    3937           0 :                     cpath->methods->ReparameterizeCustomPathByChild)
    3938           0 :                     cpath->custom_private =
    3939           0 :                         cpath->methods->ReparameterizeCustomPathByChild(root,
    3940             :                                                                         cpath->custom_private,
    3941             :                                                                         child_rel);
    3942           0 :                 new_path = (Path *) cpath;
    3943             :             }
    3944           0 :             break;
    3945             : 
    3946             :         case T_NestPath:
    3947             :             {
    3948             :                 JoinPath   *jpath;
    3949             : 
    3950         200 :                 FLAT_COPY_PATH(jpath, path, NestPath);
    3951             : 
    3952         200 :                 REPARAMETERIZE_CHILD_PATH(jpath->outerjoinpath);
    3953         200 :                 REPARAMETERIZE_CHILD_PATH(jpath->innerjoinpath);
    3954         200 :                 ADJUST_CHILD_ATTRS(jpath->joinrestrictinfo);
    3955         200 :                 new_path = (Path *) jpath;
    3956             :             }
    3957         200 :             break;
    3958             : 
    3959             :         case T_MergePath:
    3960             :             {
    3961             :                 JoinPath   *jpath;
    3962             :                 MergePath  *mpath;
    3963             : 
    3964          24 :                 FLAT_COPY_PATH(mpath, path, MergePath);
    3965             : 
    3966          24 :                 jpath = (JoinPath *) mpath;
    3967          24 :                 REPARAMETERIZE_CHILD_PATH(jpath->outerjoinpath);
    3968          24 :                 REPARAMETERIZE_CHILD_PATH(jpath->innerjoinpath);
    3969          24 :                 ADJUST_CHILD_ATTRS(jpath->joinrestrictinfo);
    3970          24 :                 ADJUST_CHILD_ATTRS(mpath->path_mergeclauses);
    3971          24 :                 new_path = (Path *) mpath;
    3972             :             }
    3973          24 :             break;
    3974             : 
    3975             :         case T_HashPath:
    3976             :             {
    3977             :                 JoinPath   *jpath;
    3978             :                 HashPath   *hpath;
    3979             : 
    3980         112 :                 FLAT_COPY_PATH(hpath, path, HashPath);
    3981             : 
    3982         112 :                 jpath = (JoinPath *) hpath;
    3983         112 :                 REPARAMETERIZE_CHILD_PATH(jpath->outerjoinpath);
    3984         112 :                 REPARAMETERIZE_CHILD_PATH(jpath->innerjoinpath);
    3985         112 :                 ADJUST_CHILD_ATTRS(jpath->joinrestrictinfo);
    3986         112 :                 ADJUST_CHILD_ATTRS(hpath->path_hashclauses);
    3987         112 :                 new_path = (Path *) hpath;
    3988             :             }
    3989         112 :             break;
    3990             : 
    3991             :         case T_AppendPath:
    3992             :             {
    3993             :                 AppendPath *apath;
    3994             : 
    3995          80 :                 FLAT_COPY_PATH(apath, path, AppendPath);
    3996          80 :                 REPARAMETERIZE_CHILD_PATH_LIST(apath->subpaths);
    3997          80 :                 new_path = (Path *) apath;
    3998             :             }
    3999          80 :             break;
    4000             : 
    4001             :         case T_MergeAppendPath:
    4002             :             {
    4003             :                 MergeAppendPath *mapath;
    4004             : 
    4005           0 :                 FLAT_COPY_PATH(mapath, path, MergeAppendPath);
    4006           0 :                 REPARAMETERIZE_CHILD_PATH_LIST(mapath->subpaths);
    4007           0 :                 new_path = (Path *) mapath;
    4008             :             }
    4009           0 :             break;
    4010             : 
    4011             :         case T_MaterialPath:
    4012             :             {
    4013             :                 MaterialPath *mpath;
    4014             : 
    4015           0 :                 FLAT_COPY_PATH(mpath, path, MaterialPath);
    4016           0 :                 REPARAMETERIZE_CHILD_PATH(mpath->subpath);
    4017           0 :                 new_path = (Path *) mpath;
    4018             :             }
    4019           0 :             break;
    4020             : 
    4021             :         case T_UniquePath:
    4022             :             {
    4023             :                 UniquePath *upath;
    4024             : 
    4025           0 :                 FLAT_COPY_PATH(upath, path, UniquePath);
    4026           0 :                 REPARAMETERIZE_CHILD_PATH(upath->subpath);
    4027           0 :                 ADJUST_CHILD_ATTRS(upath->uniq_exprs);
    4028           0 :                 new_path = (Path *) upath;
    4029             :             }
    4030           0 :             break;
    4031             : 
    4032             :         case T_GatherPath:
    4033             :             {
    4034             :                 GatherPath *gpath;
    4035             : 
    4036           0 :                 FLAT_COPY_PATH(gpath, path, GatherPath);
    4037           0 :                 REPARAMETERIZE_CHILD_PATH(gpath->subpath);
    4038           0 :                 new_path = (Path *) gpath;
    4039             :             }
    4040           0 :             break;
    4041             : 
    4042             :         case T_GatherMergePath:
    4043             :             {
    4044             :                 GatherMergePath *gmpath;
    4045             : 
    4046           0 :                 FLAT_COPY_PATH(gmpath, path, GatherMergePath);
    4047           0 :                 REPARAMETERIZE_CHILD_PATH(gmpath->subpath);
    4048           0 :                 new_path = (Path *) gmpath;
    4049             :             }
    4050           0 :             break;
    4051             : 
    4052             :         default:
    4053             : 
    4054             :             /* We don't know how to reparameterize this path. */
    4055           0 :             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        2036 :     old_ppi = new_path->param_info;
    4064        2036 :     required_outer =
    4065        2036 :         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        2036 :     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        2036 :     if (new_ppi == NULL)
    4078             :     {
    4079             :         MemoryContext oldcontext;
    4080         956 :         RelOptInfo *rel = path->parent;
    4081             : 
    4082         956 :         oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
    4083             : 
    4084         956 :         new_ppi = makeNode(ParamPathInfo);
    4085         956 :         new_ppi->ppi_req_outer = bms_copy(required_outer);
    4086         956 :         new_ppi->ppi_rows = old_ppi->ppi_rows;
    4087         956 :         new_ppi->ppi_clauses = old_ppi->ppi_clauses;
    4088         956 :         ADJUST_CHILD_ATTRS(new_ppi->ppi_clauses);
    4089         956 :         rel->ppilist = lappend(rel->ppilist, new_ppi);
    4090             : 
    4091         956 :         MemoryContextSwitchTo(oldcontext);
    4092             :     }
    4093        2036 :     bms_free(required_outer);
    4094             : 
    4095        2036 :     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        2036 :     if (bms_overlap(path->parent->lateral_relids,
    4103        2036 :                     child_rel->top_parent_relids))
    4104             :     {
    4105         768 :         new_path->pathtarget = copy_pathtarget(new_path->pathtarget);
    4106         768 :         ADJUST_CHILD_ATTRS(new_path->pathtarget->exprs);
    4107             :     }
    4108             : 
    4109        2036 :     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          80 : reparameterize_pathlist_by_child(PlannerInfo *root,
    4118             :                                  List *pathlist,
    4119             :                                  RelOptInfo *child_rel)
    4120             : {
    4121             :     ListCell   *lc;
    4122          80 :     List       *result = NIL;
    4123             : 
    4124         240 :     foreach(lc, pathlist)
    4125             :     {
    4126         160 :         Path       *path = reparameterize_path_by_child(root, lfirst(lc),
    4127             :                                                         child_rel);
    4128             : 
    4129         160 :         if (path == NULL)
    4130             :         {
    4131           0 :             list_free(result);
    4132           0 :             return NIL;
    4133             :         }
    4134             : 
    4135         160 :         result = lappend(result, path);
    4136             :     }
    4137             : 
    4138          80 :     return result;
    4139             : }

Generated by: LCOV version 1.13