LCOV - differential code coverage report
Current view: top level - src/backend/optimizer/util - pathnode.c (source / functions) Coverage Total Hit UNC LBC UIC UBC GBC GIC GNC CBC EUB ECB DUB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 91.4 % 1510 1380 15 28 68 19 24 659 16 681 86 662 1 9
Current Date: 2023-04-08 15:15:32 Functions: 100.0 % 63 63 53 10 51 2
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

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

Generated by: LCOV version v1.16-55-g56c0a2a