LCOV - code coverage report
Current view: top level - src/backend/optimizer/util - pathnode.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 91.9 % 1579 1451
Test Date: 2026-11-24 16:16:11 Functions: 100.0 % 63 63
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 80.9 % 827 669

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

Generated by: LCOV version 2.0-1