LCOV - differential code coverage report
Current view: top level - src/backend/optimizer/util - pathnode.c (source / functions) Coverage Total Hit UNC LBC UBC GBC GNC CBC DUB DCB
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 89.5 % 1602 1434 28 16 124 2 82 1350 10 61
Current Date: 2024-04-14 14:21:10 Functions: 100.0 % 65 65 14 51 3
Baseline: 16@8cea358b128 Branches: 76.4 % 901 688 31 9 173 2 43 643
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed [..60] days: 75.0 % 96 72 24 64 8
(60,120] days: 100.0 % 8 8 8
(180,240] days: 100.0 % 3 3 3
(240..) days: 90.4 % 1495 1351 4 16 124 2 10 1339
Function coverage date bins:
[..60] days: 100.0 % 3 3 3
(240..) days: 100.0 % 62 62 11 51
Branch coverage date bins:
[..60] days: 57.8 % 64 37 23 4 31 6
(60,120] days: 75.0 % 8 6 2 6
(180,240] days: 100.0 % 2 2 2
(240..) days: 77.8 % 827 643 6 9 169 2 6 635

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

Generated by: LCOV version 2.1-beta2-3-g6141622