LCOV - differential code coverage report
Current view: top level - src/backend/optimizer/path - allpaths.c (source / functions) Coverage Total Hit UNC LBC UBC GNC CBC DCB
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 94.6 % 1142 1080 2 60 14 1066 15
Current Date: 2024-04-14 14:21:10 Functions: 100.0 % 49 49 6 43 1
Baseline: 16@8cea358b128 Branches: 82.8 % 1030 853 2 1 174 8 845
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: 100.0 % 5 5 5
(180,240] days: 100.0 % 9 9 9
(240..) days: 94.5 % 1128 1066 2 60 1066
Function coverage date bins:
(240..) days: 100.0 % 49 49 6 43
Branch coverage date bins:
[..60] days: 50.0 % 2 1 1 1
(180,240] days: 87.5 % 8 7 1 7
(240..) days: 82.8 % 1020 845 1 174 845

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * allpaths.c
                                  4                 :                :  *    Routines to find possible search paths for processing a query
                                  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/path/allpaths.c
                                 12                 :                :  *
                                 13                 :                :  *-------------------------------------------------------------------------
                                 14                 :                :  */
                                 15                 :                : 
                                 16                 :                : #include "postgres.h"
                                 17                 :                : 
                                 18                 :                : #include <limits.h>
                                 19                 :                : #include <math.h>
                                 20                 :                : 
                                 21                 :                : #include "access/sysattr.h"
                                 22                 :                : #include "access/tsmapi.h"
                                 23                 :                : #include "catalog/pg_class.h"
                                 24                 :                : #include "catalog/pg_operator.h"
                                 25                 :                : #include "catalog/pg_proc.h"
                                 26                 :                : #include "foreign/fdwapi.h"
                                 27                 :                : #include "miscadmin.h"
                                 28                 :                : #include "nodes/makefuncs.h"
                                 29                 :                : #include "nodes/nodeFuncs.h"
                                 30                 :                : #include "nodes/supportnodes.h"
                                 31                 :                : #ifdef OPTIMIZER_DEBUG
                                 32                 :                : #include "nodes/print.h"
                                 33                 :                : #endif
                                 34                 :                : #include "optimizer/appendinfo.h"
                                 35                 :                : #include "optimizer/clauses.h"
                                 36                 :                : #include "optimizer/cost.h"
                                 37                 :                : #include "optimizer/geqo.h"
                                 38                 :                : #include "optimizer/optimizer.h"
                                 39                 :                : #include "optimizer/pathnode.h"
                                 40                 :                : #include "optimizer/paths.h"
                                 41                 :                : #include "optimizer/plancat.h"
                                 42                 :                : #include "optimizer/planner.h"
                                 43                 :                : #include "optimizer/tlist.h"
                                 44                 :                : #include "parser/parse_clause.h"
                                 45                 :                : #include "parser/parsetree.h"
                                 46                 :                : #include "partitioning/partbounds.h"
                                 47                 :                : #include "port/pg_bitutils.h"
                                 48                 :                : #include "rewrite/rewriteManip.h"
                                 49                 :                : #include "utils/lsyscache.h"
                                 50                 :                : 
                                 51                 :                : 
                                 52                 :                : /* Bitmask flags for pushdown_safety_info.unsafeFlags */
                                 53                 :                : #define UNSAFE_HAS_VOLATILE_FUNC        (1 << 0)
                                 54                 :                : #define UNSAFE_HAS_SET_FUNC             (1 << 1)
                                 55                 :                : #define UNSAFE_NOTIN_DISTINCTON_CLAUSE  (1 << 2)
                                 56                 :                : #define UNSAFE_NOTIN_PARTITIONBY_CLAUSE (1 << 3)
                                 57                 :                : #define UNSAFE_TYPE_MISMATCH            (1 << 4)
                                 58                 :                : 
                                 59                 :                : /* results of subquery_is_pushdown_safe */
                                 60                 :                : typedef struct pushdown_safety_info
                                 61                 :                : {
                                 62                 :                :     unsigned char *unsafeFlags; /* bitmask of reasons why this target list
                                 63                 :                :                                  * column is unsafe for qual pushdown, or 0 if
                                 64                 :                :                                  * no reason. */
                                 65                 :                :     bool        unsafeVolatile; /* don't push down volatile quals */
                                 66                 :                :     bool        unsafeLeaky;    /* don't push down leaky quals */
                                 67                 :                : } pushdown_safety_info;
                                 68                 :                : 
                                 69                 :                : /* Return type for qual_is_pushdown_safe */
                                 70                 :                : typedef enum pushdown_safe_type
                                 71                 :                : {
                                 72                 :                :     PUSHDOWN_UNSAFE,            /* unsafe to push qual into subquery */
                                 73                 :                :     PUSHDOWN_SAFE,              /* safe to push qual into subquery */
                                 74                 :                :     PUSHDOWN_WINDOWCLAUSE_RUNCOND,  /* unsafe, but may work as WindowClause
                                 75                 :                :                                      * run condition */
                                 76                 :                : } pushdown_safe_type;
                                 77                 :                : 
                                 78                 :                : /* These parameters are set by GUC */
                                 79                 :                : bool        enable_geqo = false;    /* just in case GUC doesn't set it */
                                 80                 :                : int         geqo_threshold;
                                 81                 :                : int         min_parallel_table_scan_size;
                                 82                 :                : int         min_parallel_index_scan_size;
                                 83                 :                : 
                                 84                 :                : /* Hook for plugins to get control in set_rel_pathlist() */
                                 85                 :                : set_rel_pathlist_hook_type set_rel_pathlist_hook = NULL;
                                 86                 :                : 
                                 87                 :                : /* Hook for plugins to replace standard_join_search() */
                                 88                 :                : join_search_hook_type join_search_hook = NULL;
                                 89                 :                : 
                                 90                 :                : 
                                 91                 :                : static void set_base_rel_consider_startup(PlannerInfo *root);
                                 92                 :                : static void set_base_rel_sizes(PlannerInfo *root);
                                 93                 :                : static void set_base_rel_pathlists(PlannerInfo *root);
                                 94                 :                : static void set_rel_size(PlannerInfo *root, RelOptInfo *rel,
                                 95                 :                :                          Index rti, RangeTblEntry *rte);
                                 96                 :                : static void set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
                                 97                 :                :                              Index rti, RangeTblEntry *rte);
                                 98                 :                : static void set_plain_rel_size(PlannerInfo *root, RelOptInfo *rel,
                                 99                 :                :                                RangeTblEntry *rte);
                                100                 :                : static void create_plain_partial_paths(PlannerInfo *root, RelOptInfo *rel);
                                101                 :                : static void set_rel_consider_parallel(PlannerInfo *root, RelOptInfo *rel,
                                102                 :                :                                       RangeTblEntry *rte);
                                103                 :                : static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
                                104                 :                :                                    RangeTblEntry *rte);
                                105                 :                : static void set_tablesample_rel_size(PlannerInfo *root, RelOptInfo *rel,
                                106                 :                :                                      RangeTblEntry *rte);
                                107                 :                : static void set_tablesample_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
                                108                 :                :                                          RangeTblEntry *rte);
                                109                 :                : static void set_foreign_size(PlannerInfo *root, RelOptInfo *rel,
                                110                 :                :                              RangeTblEntry *rte);
                                111                 :                : static void set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel,
                                112                 :                :                                  RangeTblEntry *rte);
                                113                 :                : static void set_append_rel_size(PlannerInfo *root, RelOptInfo *rel,
                                114                 :                :                                 Index rti, RangeTblEntry *rte);
                                115                 :                : static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
                                116                 :                :                                     Index rti, RangeTblEntry *rte);
                                117                 :                : static void generate_orderedappend_paths(PlannerInfo *root, RelOptInfo *rel,
                                118                 :                :                                          List *live_childrels,
                                119                 :                :                                          List *all_child_pathkeys);
                                120                 :                : static Path *get_cheapest_parameterized_child_path(PlannerInfo *root,
                                121                 :                :                                                    RelOptInfo *rel,
                                122                 :                :                                                    Relids required_outer);
                                123                 :                : static void accumulate_append_subpath(Path *path,
                                124                 :                :                                       List **subpaths,
                                125                 :                :                                       List **special_subpaths);
                                126                 :                : static Path *get_singleton_append_subpath(Path *path);
                                127                 :                : static void set_dummy_rel_pathlist(RelOptInfo *rel);
                                128                 :                : static void set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
                                129                 :                :                                   Index rti, RangeTblEntry *rte);
                                130                 :                : static void set_function_pathlist(PlannerInfo *root, RelOptInfo *rel,
                                131                 :                :                                   RangeTblEntry *rte);
                                132                 :                : static void set_values_pathlist(PlannerInfo *root, RelOptInfo *rel,
                                133                 :                :                                 RangeTblEntry *rte);
                                134                 :                : static void set_tablefunc_pathlist(PlannerInfo *root, RelOptInfo *rel,
                                135                 :                :                                    RangeTblEntry *rte);
                                136                 :                : static void set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel,
                                137                 :                :                              RangeTblEntry *rte);
                                138                 :                : static void set_namedtuplestore_pathlist(PlannerInfo *root, RelOptInfo *rel,
                                139                 :                :                                          RangeTblEntry *rte);
                                140                 :                : static void set_result_pathlist(PlannerInfo *root, RelOptInfo *rel,
                                141                 :                :                                 RangeTblEntry *rte);
                                142                 :                : static void set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel,
                                143                 :                :                                    RangeTblEntry *rte);
                                144                 :                : static RelOptInfo *make_rel_from_joinlist(PlannerInfo *root, List *joinlist);
                                145                 :                : static bool subquery_is_pushdown_safe(Query *subquery, Query *topquery,
                                146                 :                :                                       pushdown_safety_info *safetyInfo);
                                147                 :                : static bool recurse_pushdown_safe(Node *setOp, Query *topquery,
                                148                 :                :                                   pushdown_safety_info *safetyInfo);
                                149                 :                : static void check_output_expressions(Query *subquery,
                                150                 :                :                                      pushdown_safety_info *safetyInfo);
                                151                 :                : static void compare_tlist_datatypes(List *tlist, List *colTypes,
                                152                 :                :                                     pushdown_safety_info *safetyInfo);
                                153                 :                : static bool targetIsInAllPartitionLists(TargetEntry *tle, Query *query);
                                154                 :                : static pushdown_safe_type qual_is_pushdown_safe(Query *subquery, Index rti,
                                155                 :                :                                                 RestrictInfo *rinfo,
                                156                 :                :                                                 pushdown_safety_info *safetyInfo);
                                157                 :                : static void subquery_push_qual(Query *subquery,
                                158                 :                :                                RangeTblEntry *rte, Index rti, Node *qual);
                                159                 :                : static void recurse_push_qual(Node *setOp, Query *topquery,
                                160                 :                :                               RangeTblEntry *rte, Index rti, Node *qual);
                                161                 :                : static void remove_unused_subquery_outputs(Query *subquery, RelOptInfo *rel,
                                162                 :                :                                            Bitmapset *extra_used_attrs);
                                163                 :                : 
                                164                 :                : 
                                165                 :                : /*
                                166                 :                :  * make_one_rel
                                167                 :                :  *    Finds all possible access paths for executing a query, returning a
                                168                 :                :  *    single rel that represents the join of all base rels in the query.
                                169                 :                :  */
                                170                 :                : RelOptInfo *
 6690 tgl@sss.pgh.pa.us         171                 :CBC      139535 : make_one_rel(PlannerInfo *root, List *joinlist)
                                172                 :                : {
                                173                 :                :     RelOptInfo *rel;
                                174                 :                :     Index       rti;
                                175                 :                :     double      total_pages;
                                176                 :                : 
                                177                 :                :     /* Mark base rels as to whether we care about fast-start plans */
 3238                           178                 :         139535 :     set_base_rel_consider_startup(root);
                                179                 :                : 
                                180                 :                :     /*
                                181                 :                :      * Compute size estimates and consider_parallel flags for each base rel.
                                182                 :                :      */
 4461                           183                 :         139535 :     set_base_rel_sizes(root);
                                184                 :                : 
                                185                 :                :     /*
                                186                 :                :      * We should now have size estimates for every actual table involved in
                                187                 :                :      * the query, and we also know which if any have been deleted from the
                                188                 :                :      * query by join removal, pruned by partition pruning, or eliminated by
                                189                 :                :      * constraint exclusion.  So we can now compute total_table_pages.
                                190                 :                :      *
                                191                 :                :      * Note that appendrels are not double-counted here, even though we don't
                                192                 :                :      * bother to distinguish RelOptInfos for appendrel parents, because the
                                193                 :                :      * parents will have pages = 0.
                                194                 :                :      *
                                195                 :                :      * XXX if a table is self-joined, we will count it once per appearance,
                                196                 :                :      * which perhaps is the wrong thing ... but that's not completely clear,
                                197                 :                :      * and detecting self-joins here is difficult, so ignore it for now.
                                198                 :                :      */
 1985                           199                 :         139521 :     total_pages = 0;
                                200         [ +  + ]:         422590 :     for (rti = 1; rti < root->simple_rel_array_size; rti++)
                                201                 :                :     {
                                202                 :         283069 :         RelOptInfo *brel = root->simple_rel_array[rti];
                                203                 :                : 
                                204                 :                :         /* there may be empty slots corresponding to non-baserel RTEs */
                                205         [ +  + ]:         283069 :         if (brel == NULL)
                                206                 :          65571 :             continue;
                                207                 :                : 
                                208         [ -  + ]:         217498 :         Assert(brel->relid == rti); /* sanity check on array */
                                209                 :                : 
                                210         [ +  + ]:         217498 :         if (IS_DUMMY_REL(brel))
                                211                 :            548 :             continue;
                                212                 :                : 
                                213   [ +  +  +  - ]:         216950 :         if (IS_SIMPLE_REL(brel))
                                214                 :         216950 :             total_pages += (double) brel->pages;
                                215                 :                :     }
                                216                 :         139521 :     root->total_table_pages = total_pages;
                                217                 :                : 
                                218                 :                :     /*
                                219                 :                :      * Generate access paths for each base rel.
                                220                 :                :      */
 8554                           221                 :         139521 :     set_base_rel_pathlists(root);
                                222                 :                : 
                                223                 :                :     /*
                                224                 :                :      * Generate access paths for the entire join tree.
                                225                 :                :      */
 6690                           226                 :         139521 :     rel = make_rel_from_joinlist(root, joinlist);
                                227                 :                : 
                                228                 :                :     /*
                                229                 :                :      * The result should join all and only the query's base + outer-join rels.
                                230                 :                :      */
  440                           231         [ -  + ]:         139520 :     Assert(bms_equal(rel->relids, root->all_query_rels));
                                232                 :                : 
 4461                           233                 :         139520 :     return rel;
                                234                 :                : }
                                235                 :                : 
                                236                 :                : /*
                                237                 :                :  * set_base_rel_consider_startup
                                238                 :                :  *    Set the consider_[param_]startup flags for each base-relation entry.
                                239                 :                :  *
                                240                 :                :  * For the moment, we only deal with consider_param_startup here; because the
                                241                 :                :  * logic for consider_startup is pretty trivial and is the same for every base
                                242                 :                :  * relation, we just let build_simple_rel() initialize that flag correctly to
                                243                 :                :  * start with.  If that logic ever gets more complicated it would probably
                                244                 :                :  * be better to move it here.
                                245                 :                :  */
                                246                 :                : static void
 3238                           247                 :         139535 : set_base_rel_consider_startup(PlannerInfo *root)
                                248                 :                : {
                                249                 :                :     /*
                                250                 :                :      * Since parameterized paths can only be used on the inside of a nestloop
                                251                 :                :      * join plan, there is usually little value in considering fast-start
                                252                 :                :      * plans for them.  However, for relations that are on the RHS of a SEMI
                                253                 :                :      * or ANTI join, a fast-start plan can be useful because we're only going
                                254                 :                :      * to care about fetching one tuple anyway.
                                255                 :                :      *
                                256                 :                :      * To minimize growth of planning time, we currently restrict this to
                                257                 :                :      * cases where the RHS is a single base relation, not a join; there is no
                                258                 :                :      * provision for consider_param_startup to get set at all on joinrels.
                                259                 :                :      * Also we don't worry about appendrels.  costsize.c's costing rules for
                                260                 :                :      * nestloop semi/antijoins don't consider such cases either.
                                261                 :                :      */
                                262                 :                :     ListCell   *lc;
                                263                 :                : 
                                264   [ +  +  +  +  :         158532 :     foreach(lc, root->join_info_list)
                                              +  + ]
                                265                 :                :     {
                                266                 :          18997 :         SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
                                267                 :                :         int         varno;
                                268                 :                : 
                                269   [ +  +  +  +  :          21563 :         if ((sjinfo->jointype == JOIN_SEMI || sjinfo->jointype == JOIN_ANTI) &&
                                              +  + ]
                                270                 :           2566 :             bms_get_singleton_member(sjinfo->syn_righthand, &varno))
                                271                 :                :         {
                                272                 :           2475 :             RelOptInfo *rel = find_base_rel(root, varno);
                                273                 :                : 
                                274                 :           2475 :             rel->consider_param_startup = true;
                                275                 :                :         }
                                276                 :                :     }
                                277                 :         139535 : }
                                278                 :                : 
                                279                 :                : /*
                                280                 :                :  * set_base_rel_sizes
                                281                 :                :  *    Set the size estimates (rows and widths) for each base-relation entry.
                                282                 :                :  *    Also determine whether to consider parallel paths for base relations.
                                283                 :                :  *
                                284                 :                :  * We do this in a separate pass over the base rels so that rowcount
                                285                 :                :  * estimates are available for parameterized path generation, and also so
                                286                 :                :  * that each rel's consider_parallel flag is set correctly before we begin to
                                287                 :                :  * generate paths.
                                288                 :                :  */
                                289                 :                : static void
 4461                           290                 :         139535 : set_base_rel_sizes(PlannerInfo *root)
                                291                 :                : {
                                292                 :                :     Index       rti;
                                293                 :                : 
                                294         [ +  + ]:         422605 :     for (rti = 1; rti < root->simple_rel_array_size; rti++)
                                295                 :                :     {
                                296                 :         283084 :         RelOptInfo *rel = root->simple_rel_array[rti];
                                297                 :                :         RangeTblEntry *rte;
                                298                 :                : 
                                299                 :                :         /* there may be empty slots corresponding to non-baserel RTEs */
                                300         [ +  + ]:         283084 :         if (rel == NULL)
                                301                 :          65572 :             continue;
                                302                 :                : 
 2489                           303         [ -  + ]:         217512 :         Assert(rel->relid == rti);   /* sanity check on array */
                                304                 :                : 
                                305                 :                :         /* ignore RTEs that are "other rels" */
 4461                           306         [ +  + ]:         217512 :         if (rel->reloptkind != RELOPT_BASEREL)
                                307                 :          21369 :             continue;
                                308                 :                : 
 3077 rhaas@postgresql.org      309                 :         196143 :         rte = root->simple_rte_array[rti];
                                310                 :                : 
                                311                 :                :         /*
                                312                 :                :          * If parallelism is allowable for this query in general, see whether
                                313                 :                :          * it's allowable for this rel in particular.  We have to do this
                                314                 :                :          * before set_rel_size(), because (a) if this rel is an inheritance
                                315                 :                :          * parent, set_append_rel_size() will use and perhaps change the rel's
                                316                 :                :          * consider_parallel flag, and (b) for some RTE types, set_rel_size()
                                317                 :                :          * goes ahead and makes paths immediately.
                                318                 :                :          */
                                319         [ +  + ]:         196143 :         if (root->glob->parallelModeOK)
                                320                 :         154369 :             set_rel_consider_parallel(root, rel, rte);
                                321                 :                : 
                                322                 :         196143 :         set_rel_size(root, rel, rti, rte);
                                323                 :                :     }
10141 scrappy@hub.org           324                 :         139521 : }
                                325                 :                : 
                                326                 :                : /*
                                327                 :                :  * set_base_rel_pathlists
                                328                 :                :  *    Finds all paths available for scanning each base-relation entry.
                                329                 :                :  *    Sequential scan and any available indices are considered.
                                330                 :                :  *    Each useful path is attached to its relation's 'pathlist' field.
                                331                 :                :  */
                                332                 :                : static void
 6888 tgl@sss.pgh.pa.us         333                 :         139521 : set_base_rel_pathlists(PlannerInfo *root)
                                334                 :                : {
                                335                 :                :     Index       rti;
                                336                 :                : 
 6648                           337         [ +  + ]:         422590 :     for (rti = 1; rti < root->simple_rel_array_size; rti++)
                                338                 :                :     {
                                339                 :         283069 :         RelOptInfo *rel = root->simple_rel_array[rti];
                                340                 :                : 
                                341                 :                :         /* there may be empty slots corresponding to non-baserel RTEs */
 6887                           342         [ +  + ]:         283069 :         if (rel == NULL)
                                343                 :          65571 :             continue;
                                344                 :                : 
 2489                           345         [ -  + ]:         217498 :         Assert(rel->relid == rti);   /* sanity check on array */
                                346                 :                : 
                                347                 :                :         /* ignore RTEs that are "other rels" */
 6887                           348         [ +  + ]:         217498 :         if (rel->reloptkind != RELOPT_BASEREL)
                                349                 :          21369 :             continue;
                                350                 :                : 
 6203                           351                 :         196129 :         set_rel_pathlist(root, rel, rti, root->simple_rte_array[rti]);
                                352                 :                :     }
 6645                           353                 :         139521 : }
                                354                 :                : 
                                355                 :                : /*
                                356                 :                :  * set_rel_size
                                357                 :                :  *    Set size estimates for a base relation
                                358                 :                :  */
                                359                 :                : static void
 4461                           360                 :         217402 : set_rel_size(PlannerInfo *root, RelOptInfo *rel,
                                361                 :                :              Index rti, RangeTblEntry *rte)
                                362                 :                : {
 4586                           363   [ +  +  +  + ]:         413545 :     if (rel->reloptkind == RELOPT_BASEREL &&
                                364                 :         196143 :         relation_excluded_by_constraints(root, rel, rte))
                                365                 :                :     {
                                366                 :                :         /*
                                367                 :                :          * We proved we don't need to scan the rel via constraint exclusion,
                                368                 :                :          * so set up a single dummy path for it.  Here we only check this for
                                369                 :                :          * regular baserels; if it's an otherrel, CE was already checked in
                                370                 :                :          * set_append_rel_size().
                                371                 :                :          *
                                372                 :                :          * In this case, we go ahead and set up the relation's path right away
                                373                 :                :          * instead of leaving it for set_rel_pathlist to do.  This is because
                                374                 :                :          * we don't have a convention for marking a rel as dummy except by
                                375                 :                :          * assigning a dummy path to it.
                                376                 :                :          */
                                377                 :            234 :         set_dummy_rel_pathlist(rel);
                                378                 :                :     }
                                379         [ +  + ]:         217168 :     else if (rte->inh)
                                380                 :                :     {
                                381                 :                :         /* It's an "append relation", process accordingly */
 4461                           382                 :          10128 :         set_append_rel_size(root, rel, rti, rte);
                                383                 :                :     }
                                384                 :                :     else
                                385                 :                :     {
 4800                           386   [ +  +  +  +  :         207040 :         switch (rel->rtekind)
                                        +  +  +  +  
                                                 - ]
                                387                 :                :         {
                                388                 :         174602 :             case RTE_RELATION:
                                389         [ +  + ]:         174602 :                 if (rte->relkind == RELKIND_FOREIGN_TABLE)
                                390                 :                :                 {
                                391                 :                :                     /* Foreign table */
 4461                           392                 :           1179 :                     set_foreign_size(root, rel, rte);
                                393                 :                :                 }
 2581 rhaas@postgresql.org      394         [ +  + ]:         173423 :                 else if (rte->relkind == RELKIND_PARTITIONED_TABLE)
                                395                 :                :                 {
                                396                 :                :                     /*
                                397                 :                :                      * We could get here if asked to scan a partitioned table
                                398                 :                :                      * with ONLY.  In that case we shouldn't scan any of the
                                399                 :                :                      * partitions, so mark it as a dummy rel.
                                400                 :                :                      */
                                401                 :             20 :                     set_dummy_rel_pathlist(rel);
                                402                 :                :                 }
 3257 simon@2ndQuadrant.co      403         [ +  + ]:         173403 :                 else if (rte->tablesample != NULL)
                                404                 :                :                 {
                                405                 :                :                     /* Sampled relation */
                                406                 :            150 :                     set_tablesample_rel_size(root, rel, rte);
                                407                 :                :                 }
                                408                 :                :                 else
                                409                 :                :                 {
                                410                 :                :                     /* Plain relation */
 4461 tgl@sss.pgh.pa.us         411                 :         173253 :                     set_plain_rel_size(root, rel, rte);
                                412                 :                :                 }
 4800                           413                 :         174588 :                 break;
                                414                 :           3788 :             case RTE_SUBQUERY:
                                415                 :                : 
                                416                 :                :                 /*
                                417                 :                :                  * Subqueries don't support making a choice between
                                418                 :                :                  * parameterized and unparameterized paths, so just go ahead
                                419                 :                :                  * and build their paths immediately.
                                420                 :                :                  */
                                421                 :           3788 :                 set_subquery_pathlist(root, rel, rti, rte);
                                422                 :           3788 :                 break;
                                423                 :          21525 :             case RTE_FUNCTION:
 4461                           424                 :          21525 :                 set_function_size_estimates(root, rel);
 4800                           425                 :          21525 :                 break;
 2594 alvherre@alvh.no-ip.      426                 :            254 :             case RTE_TABLEFUNC:
                                427                 :            254 :                 set_tablefunc_size_estimates(root, rel);
                                428                 :            254 :                 break;
 4800 tgl@sss.pgh.pa.us         429                 :           3858 :             case RTE_VALUES:
 4461                           430                 :           3858 :                 set_values_size_estimates(root, rel);
 4800                           431                 :           3858 :                 break;
                                432                 :           2009 :             case RTE_CTE:
                                433                 :                : 
                                434                 :                :                 /*
                                435                 :                :                  * CTEs don't support making a choice between parameterized
                                436                 :                :                  * and unparameterized paths, so just go ahead and build their
                                437                 :                :                  * paths immediately.
                                438                 :                :                  */
                                439         [ +  + ]:           2009 :                 if (rte->self_reference)
                                440                 :            406 :                     set_worktable_pathlist(root, rel, rte);
                                441                 :                :                 else
                                442                 :           1603 :                     set_cte_pathlist(root, rel, rte);
                                443                 :           2009 :                 break;
 2571 kgrittn@postgresql.o      444                 :            223 :             case RTE_NAMEDTUPLESTORE:
                                445                 :                :                 /* Might as well just build the path immediately */
                                446                 :            223 :                 set_namedtuplestore_pathlist(root, rel, rte);
                                447                 :            223 :                 break;
 1903 tgl@sss.pgh.pa.us         448                 :            781 :             case RTE_RESULT:
                                449                 :                :                 /* Might as well just build the path immediately */
                                450                 :            781 :                 set_result_pathlist(root, rel, rte);
                                451                 :            781 :                 break;
 4800 tgl@sss.pgh.pa.us         452                 :UBC           0 :             default:
                                453         [ #  # ]:              0 :                 elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
                                454                 :                :                 break;
                                455                 :                :         }
                                456                 :                :     }
                                457                 :                : 
                                458                 :                :     /*
                                459                 :                :      * We insist that all non-dummy rels have a nonzero rowcount estimate.
                                460                 :                :      */
 3185 tgl@sss.pgh.pa.us         461   [ +  +  -  + ]:CBC      217387 :     Assert(rel->rows > 0 || IS_DUMMY_REL(rel));
 4461                           462                 :         217387 : }
                                463                 :                : 
                                464                 :                : /*
                                465                 :                :  * set_rel_pathlist
                                466                 :                :  *    Build access paths for a base relation
                                467                 :                :  */
                                468                 :                : static void
                                469                 :         217411 : set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
                                470                 :                :                  Index rti, RangeTblEntry *rte)
                                471                 :                : {
                                472         [ +  + ]:         217411 :     if (IS_DUMMY_REL(rel))
                                473                 :                :     {
                                474                 :                :         /* We already proved the relation empty, so nothing more to do */
                                475                 :                :     }
                                476         [ +  + ]:         216932 :     else if (rte->inh)
                                477                 :                :     {
                                478                 :                :         /* It's an "append relation", process accordingly */
                                479                 :           9980 :         set_append_rel_pathlist(root, rel, rti, rte);
                                480                 :                :     }
                                481                 :                :     else
                                482                 :                :     {
                                483   [ +  +  +  +  :         206952 :         switch (rel->rtekind)
                                        +  +  +  +  
                                                 - ]
                                484                 :                :         {
                                485                 :         174568 :             case RTE_RELATION:
                                486         [ +  + ]:         174568 :                 if (rte->relkind == RELKIND_FOREIGN_TABLE)
                                487                 :                :                 {
                                488                 :                :                     /* Foreign table */
                                489                 :           1177 :                     set_foreign_pathlist(root, rel, rte);
                                490                 :                :                 }
 3257 simon@2ndQuadrant.co      491         [ +  + ]:         173391 :                 else if (rte->tablesample != NULL)
                                492                 :                :                 {
                                493                 :                :                     /* Sampled relation */
                                494                 :            150 :                     set_tablesample_rel_pathlist(root, rel, rte);
                                495                 :                :                 }
                                496                 :                :                 else
                                497                 :                :                 {
                                498                 :                :                     /* Plain relation */
 4461 tgl@sss.pgh.pa.us         499                 :         173241 :                     set_plain_rel_pathlist(root, rel, rte);
                                500                 :                :                 }
                                501                 :         174568 :                 break;
                                502                 :           3734 :             case RTE_SUBQUERY:
                                503                 :                :                 /* Subquery --- fully handled during set_rel_size */
                                504                 :           3734 :                 break;
                                505                 :          21525 :             case RTE_FUNCTION:
                                506                 :                :                 /* RangeFunction */
                                507                 :          21525 :                 set_function_pathlist(root, rel, rte);
                                508                 :          21525 :                 break;
 2594 alvherre@alvh.no-ip.      509                 :            254 :             case RTE_TABLEFUNC:
                                510                 :                :                 /* Table Function */
                                511                 :            254 :                 set_tablefunc_pathlist(root, rel, rte);
                                512                 :            254 :                 break;
 4461 tgl@sss.pgh.pa.us         513                 :           3858 :             case RTE_VALUES:
                                514                 :                :                 /* Values list */
                                515                 :           3858 :                 set_values_pathlist(root, rel, rte);
                                516                 :           3858 :                 break;
                                517                 :           2009 :             case RTE_CTE:
                                518                 :                :                 /* CTE reference --- fully handled during set_rel_size */
                                519                 :           2009 :                 break;
 2571 kgrittn@postgresql.o      520                 :            223 :             case RTE_NAMEDTUPLESTORE:
                                521                 :                :                 /* tuplestore reference --- fully handled during set_rel_size */
                                522                 :            223 :                 break;
 1903 tgl@sss.pgh.pa.us         523                 :            781 :             case RTE_RESULT:
                                524                 :                :                 /* simple Result --- fully handled during set_rel_size */
                                525                 :            781 :                 break;
 4461 tgl@sss.pgh.pa.us         526                 :UBC           0 :             default:
                                527         [ #  # ]:              0 :                 elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
                                528                 :                :                 break;
                                529                 :                :         }
                                530                 :                :     }
                                531                 :                : 
                                532                 :                :     /*
                                533                 :                :      * Allow a plugin to editorialize on the set of Paths for this base
                                534                 :                :      * relation.  It could add new paths (such as CustomPaths) by calling
                                535                 :                :      * add_path(), or add_partial_path() if parallel aware.  It could also
                                536                 :                :      * delete or modify paths added by the core code.
                                537                 :                :      */
 1891 tgl@sss.pgh.pa.us         538         [ -  + ]:CBC      217411 :     if (set_rel_pathlist_hook)
 1891 tgl@sss.pgh.pa.us         539                 :UBC           0 :         (*set_rel_pathlist_hook) (root, rel, rti, rte);
                                540                 :                : 
                                541                 :                :     /*
                                542                 :                :      * If this is a baserel, we should normally consider gathering any partial
                                543                 :                :      * paths we may have created for it.  We have to do this after calling the
                                544                 :                :      * set_rel_pathlist_hook, else it cannot add partial paths to be included
                                545                 :                :      * here.
                                546                 :                :      *
                                547                 :                :      * However, if this is an inheritance child, skip it.  Otherwise, we could
                                548                 :                :      * end up with a very large number of gather nodes, each trying to grab
                                549                 :                :      * its own pool of workers.  Instead, we'll consider gathering partial
                                550                 :                :      * paths for the parent appendrel.
                                551                 :                :      *
                                552                 :                :      * Also, if this is the topmost scan/join rel, we postpone gathering until
                                553                 :                :      * the final scan/join targetlist is available (see grouping_planner).
                                554                 :                :      */
 2225 rhaas@postgresql.org      555         [ +  + ]:CBC      217411 :     if (rel->reloptkind == RELOPT_BASEREL &&
  440 tgl@sss.pgh.pa.us         556         [ +  + ]:         196129 :         !bms_equal(rel->relids, root->all_query_rels))
 1468 tomas.vondra@postgre      557                 :          97259 :         generate_useful_gather_paths(root, rel, false);
                                558                 :                : 
                                559                 :                :     /* Now find the cheapest of the paths for this rel */
 3432 tgl@sss.pgh.pa.us         560                 :         217411 :     set_cheapest(rel);
                                561                 :                : 
                                562                 :                : #ifdef OPTIMIZER_DEBUG
                                563                 :                :     pprint(rel);
                                564                 :                : #endif
 8554                           565                 :         217411 : }
                                566                 :                : 
                                567                 :                : /*
                                568                 :                :  * set_plain_rel_size
                                569                 :                :  *    Set size estimates for a plain relation (no subquery, no inheritance)
                                570                 :                :  */
                                571                 :                : static void
 4461                           572                 :         173253 : set_plain_rel_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
                                573                 :                : {
                                574                 :                :     /*
                                575                 :                :      * Test any partial indexes of rel for applicability.  We must do this
                                576                 :                :      * first since partial unique indexes can affect size estimates.
                                577                 :                :      */
 2936                           578                 :         173253 :     check_index_predicates(root, rel);
                                579                 :                : 
                                580                 :                :     /* Mark rel with estimated output rows, width, etc */
 6203                           581                 :         173253 :     set_baserel_size_estimates(root, rel);
 4461                           582                 :         173241 : }
                                583                 :                : 
                                584                 :                : /*
                                585                 :                :  * If this relation could possibly be scanned from within a worker, then set
                                586                 :                :  * its consider_parallel flag.
                                587                 :                :  */
                                588                 :                : static void
 3077 rhaas@postgresql.org      589                 :         169576 : set_rel_consider_parallel(PlannerInfo *root, RelOptInfo *rel,
                                590                 :                :                           RangeTblEntry *rte)
                                591                 :                : {
                                592                 :                :     /*
                                593                 :                :      * The flag has previously been initialized to false, so we can just
                                594                 :                :      * return if it becomes clear that we can't safely set it.
                                595                 :                :      */
 2842 tgl@sss.pgh.pa.us         596         [ -  + ]:         169576 :     Assert(!rel->consider_parallel);
                                597                 :                : 
                                598                 :                :     /* Don't call this if parallelism is disallowed for the entire query. */
 3077 rhaas@postgresql.org      599         [ -  + ]:         169576 :     Assert(root->glob->parallelModeOK);
                                600                 :                : 
                                601                 :                :     /* This should only be called for baserels and appendrel children. */
 2568                           602   [ +  +  -  + ]:         169576 :     Assert(IS_SIMPLE_REL(rel));
                                603                 :                : 
                                604                 :                :     /* Assorted checks based on rtekind. */
 3077                           605   [ +  +  -  +  :         169576 :     switch (rte->rtekind)
                                     +  +  +  +  +  
                                                 - ]
                                606                 :                :     {
                                607                 :         150823 :         case RTE_RELATION:
                                608                 :                : 
                                609                 :                :             /*
                                610                 :                :              * Currently, parallel workers can't access the leader's temporary
                                611                 :                :              * tables.  We could possibly relax this if we wrote all of its
                                612                 :                :              * local buffers at the start of the query and made no changes
                                613                 :                :              * thereafter (maybe we could allow hint bit changes), and if we
                                614                 :                :              * taught the workers to read them.  Writing a large number of
                                615                 :                :              * temporary buffers could be expensive, though, and we don't have
                                616                 :                :              * the rest of the necessary infrastructure right now anyway.  So
                                617                 :                :              * for now, bail out if we see a temporary table.
                                618                 :                :              */
                                619         [ +  + ]:         150823 :             if (get_rel_persistence(rte->relid) == RELPERSISTENCE_TEMP)
                                620                 :           3803 :                 return;
                                621                 :                : 
                                622                 :                :             /*
                                623                 :                :              * Table sampling can be pushed down to workers if the sample
                                624                 :                :              * function and its arguments are safe.
                                625                 :                :              */
                                626         [ +  + ]:         147020 :             if (rte->tablesample != NULL)
                                627                 :                :             {
 2693 tgl@sss.pgh.pa.us         628                 :            162 :                 char        proparallel = func_parallel(rte->tablesample->tsmhandler);
                                629                 :                : 
 3077 rhaas@postgresql.org      630         [ +  + ]:            162 :                 if (proparallel != PROPARALLEL_SAFE)
                                631                 :             18 :                     return;
 2795 tgl@sss.pgh.pa.us         632         [ +  + ]:            144 :                 if (!is_parallel_safe(root, (Node *) rte->tablesample->args))
 3077 rhaas@postgresql.org      633                 :              6 :                     return;
                                634                 :                :             }
                                635                 :                : 
                                636                 :                :             /*
                                637                 :                :              * Ask FDWs whether they can support performing a ForeignScan
                                638                 :                :              * within a worker.  Most often, the answer will be no.  For
                                639                 :                :              * example, if the nature of the FDW is such that it opens a TCP
                                640                 :                :              * connection with a remote server, each parallel worker would end
                                641                 :                :              * up with a separate connection, and these connections might not
                                642                 :                :              * be appropriately coordinated between workers and the leader.
                                643                 :                :              */
 2970                           644         [ +  + ]:         146996 :             if (rte->relkind == RELKIND_FOREIGN_TABLE)
                                645                 :                :             {
                                646         [ -  + ]:            762 :                 Assert(rel->fdwroutine);
                                647         [ +  + ]:            762 :                 if (!rel->fdwroutine->IsForeignScanParallelSafe)
                                648                 :            730 :                     return;
                                649         [ -  + ]:             32 :                 if (!rel->fdwroutine->IsForeignScanParallelSafe(root, rel, rte))
 2970 rhaas@postgresql.org      650                 :UBC           0 :                     return;
                                651                 :                :             }
                                652                 :                : 
                                653                 :                :             /*
                                654                 :                :              * There are additional considerations for appendrels, which we'll
                                655                 :                :              * deal with in set_append_rel_size and set_append_rel_pathlist.
                                656                 :                :              * For now, just set consider_parallel based on the rel's own
                                657                 :                :              * quals and targetlist.
                                658                 :                :              */
 3077 rhaas@postgresql.org      659                 :CBC      146266 :             break;
                                660                 :                : 
                                661                 :           3111 :         case RTE_SUBQUERY:
                                662                 :                : 
                                663                 :                :             /*
                                664                 :                :              * There's no intrinsic problem with scanning a subquery-in-FROM
                                665                 :                :              * (as distinct from a SubPlan or InitPlan) in a parallel worker.
                                666                 :                :              * If the subquery doesn't happen to have any parallel-safe paths,
                                667                 :                :              * then flagging it as consider_parallel won't change anything,
                                668                 :                :              * but that's true for plain tables, too.  We must set
                                669                 :                :              * consider_parallel based on the rel's own quals and targetlist,
                                670                 :                :              * so that if a subquery path is parallel-safe but the quals and
                                671                 :                :              * projection we're sticking onto it are not, we correctly mark
                                672                 :                :              * the SubqueryScanPath as not parallel-safe.  (Note that
                                673                 :                :              * set_subquery_pathlist() might push some of these quals down
                                674                 :                :              * into the subquery itself, but that doesn't change anything.)
                                675                 :                :              *
                                676                 :                :              * We can't push sub-select containing LIMIT/OFFSET to workers as
                                677                 :                :              * there is no guarantee that the row order will be fully
                                678                 :                :              * deterministic, and applying LIMIT/OFFSET will lead to
                                679                 :                :              * inconsistent results at the top-level.  (In some cases, where
                                680                 :                :              * the result is ordered, we could relax this restriction.  But it
                                681                 :                :              * doesn't currently seem worth expending extra effort to do so.)
                                682                 :                :              */
                                683                 :                :             {
 2039 akapila@postgresql.o      684                 :           3111 :                 Query      *subquery = castNode(Query, rte->subquery);
                                685                 :                : 
                                686         [ +  + ]:           3111 :                 if (limit_needed(subquery))
                                687                 :            223 :                     return;
                                688                 :                :             }
 2842 tgl@sss.pgh.pa.us         689                 :           2888 :             break;
                                690                 :                : 
 3077 rhaas@postgresql.org      691                 :UBC           0 :         case RTE_JOIN:
                                692                 :                :             /* Shouldn't happen; we're only considering baserels here. */
                                693                 :              0 :             Assert(false);
                                694                 :                :             return;
                                695                 :                : 
 3077 rhaas@postgresql.org      696                 :CBC       11575 :         case RTE_FUNCTION:
                                697                 :                :             /* Check for parallel-restricted functions. */
 2795 tgl@sss.pgh.pa.us         698         [ +  + ]:          11575 :             if (!is_parallel_safe(root, (Node *) rte->functions))
 3077 rhaas@postgresql.org      699                 :           5505 :                 return;
                                700                 :           6070 :             break;
                                701                 :                : 
 2594 alvherre@alvh.no-ip.      702                 :            254 :         case RTE_TABLEFUNC:
                                703                 :                :             /* not parallel safe */
                                704                 :            254 :             return;
                                705                 :                : 
 3077 rhaas@postgresql.org      706                 :           1375 :         case RTE_VALUES:
                                707                 :                :             /* Check for parallel-restricted functions. */
 2795 tgl@sss.pgh.pa.us         708         [ +  + ]:           1375 :             if (!is_parallel_safe(root, (Node *) rte->values_lists))
                                709                 :              3 :                 return;
 3077 rhaas@postgresql.org      710                 :           1372 :             break;
                                711                 :                : 
                                712                 :           1628 :         case RTE_CTE:
                                713                 :                : 
                                714                 :                :             /*
                                715                 :                :              * CTE tuplestores aren't shared among parallel workers, so we
                                716                 :                :              * force all CTE scans to happen in the leader.  Also, populating
                                717                 :                :              * the CTE would require executing a subplan that's not available
                                718                 :                :              * in the worker, might be parallel-restricted, and must get
                                719                 :                :              * executed only once.
                                720                 :                :              */
                                721                 :           1628 :             return;
                                722                 :                : 
 2571 kgrittn@postgresql.o      723                 :            209 :         case RTE_NAMEDTUPLESTORE:
                                724                 :                : 
                                725                 :                :             /*
                                726                 :                :              * tuplestore cannot be shared, at least without more
                                727                 :                :              * infrastructure to support that.
                                728                 :                :              */
                                729                 :            209 :             return;
                                730                 :                : 
 1903 tgl@sss.pgh.pa.us         731                 :            601 :         case RTE_RESULT:
                                732                 :                :             /* RESULT RTEs, in themselves, are no problem. */
                                733                 :            601 :             break;
                                734                 :                :     }
                                735                 :                : 
                                736                 :                :     /*
                                737                 :                :      * If there's anything in baserestrictinfo that's parallel-restricted, we
                                738                 :                :      * give up on parallelizing access to this relation.  We could consider
                                739                 :                :      * instead postponing application of the restricted quals until we're
                                740                 :                :      * above all the parallelism in the plan tree, but it's not clear that
                                741                 :                :      * that would be a win in very many cases, and it might be tricky to make
                                742                 :                :      * outer join clauses work correctly.  It would likely break equivalence
                                743                 :                :      * classes, too.
                                744                 :                :      */
 2795                           745         [ +  + ]:         157197 :     if (!is_parallel_safe(root, (Node *) rel->baserestrictinfo))
 3077 rhaas@postgresql.org      746                 :          10976 :         return;
                                747                 :                : 
                                748                 :                :     /*
                                749                 :                :      * Likewise, if the relation's outputs are not parallel-safe, give up.
                                750                 :                :      * (Usually, they're just Vars, but sometimes they're not.)
                                751                 :                :      */
 2795 tgl@sss.pgh.pa.us         752         [ +  + ]:         146221 :     if (!is_parallel_safe(root, (Node *) rel->reltarget->exprs))
 2866 rhaas@postgresql.org      753                 :              9 :         return;
                                754                 :                : 
                                755                 :                :     /* We have a winner. */
 3077                           756                 :         146212 :     rel->consider_parallel = true;
                                757                 :                : }
                                758                 :                : 
                                759                 :                : /*
                                760                 :                :  * set_plain_rel_pathlist
                                761                 :                :  *    Build access paths for a plain relation (no subquery, no inheritance)
                                762                 :                :  */
                                763                 :                : static void
 4461 tgl@sss.pgh.pa.us         764                 :         173241 : set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
                                765                 :                : {
                                766                 :                :     Relids      required_outer;
                                767                 :                : 
                                768                 :                :     /*
                                769                 :                :      * We don't support pushing join clauses into the quals of a seqscan, but
                                770                 :                :      * it could still have required parameterization due to LATERAL refs in
                                771                 :                :      * its tlist.
                                772                 :                :      */
 4249                           773                 :         173241 :     required_outer = rel->lateral_relids;
                                774                 :                : 
                                775                 :                :     /* Consider sequential scan */
 3077 rhaas@postgresql.org      776                 :         173241 :     add_path(rel, create_seqscan_path(root, rel, required_outer, 0));
                                777                 :                : 
                                778                 :                :     /* If appropriate, consider parallel sequential scan */
 3007                           779   [ +  +  +  + ]:         173241 :     if (rel->consider_parallel && required_outer == NULL)
 2906 tgl@sss.pgh.pa.us         780                 :         128810 :         create_plain_partial_paths(root, rel);
                                781                 :                : 
                                782                 :                :     /* Consider index scans */
 6929                           783                 :         173241 :     create_index_paths(root, rel);
                                784                 :                : 
                                785                 :                :     /* Consider TID scans */
 8554                           786                 :         173241 :     create_tidscan_paths(root, rel);
                                787                 :         173241 : }
                                788                 :                : 
                                789                 :                : /*
                                790                 :                :  * create_plain_partial_paths
                                791                 :                :  *    Build partial access paths for parallel scan of a plain relation
                                792                 :                :  */
                                793                 :                : static void
 2906                           794                 :         128810 : create_plain_partial_paths(PlannerInfo *root, RelOptInfo *rel)
                                795                 :                : {
                                796                 :                :     int         parallel_workers;
                                797                 :                : 
 2263 rhaas@postgresql.org      798                 :         128810 :     parallel_workers = compute_parallel_worker(rel, rel->pages, -1,
                                799                 :                :                                                max_parallel_workers_per_gather);
                                800                 :                : 
                                801                 :                :     /* If any limit was set to zero, the user doesn't want a parallel scan. */
 2866 tgl@sss.pgh.pa.us         802         [ +  + ]:         128810 :     if (parallel_workers <= 0)
                                803                 :         115775 :         return;
                                804                 :                : 
                                805                 :                :     /* Add an unordered partial path based on a parallel sequential scan. */
      rhaas@postgresql.org      806                 :          13035 :     add_partial_path(rel, create_seqscan_path(root, rel, NULL, parallel_workers));
                                807                 :                : }
                                808                 :                : 
                                809                 :                : /*
                                810                 :                :  * set_tablesample_rel_size
                                811                 :                :  *    Set size estimates for a sampled relation
                                812                 :                :  */
                                813                 :                : static void
 3257 simon@2ndQuadrant.co      814                 :            150 : set_tablesample_rel_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
                                815                 :                : {
 3186 tgl@sss.pgh.pa.us         816                 :            150 :     TableSampleClause *tsc = rte->tablesample;
                                817                 :                :     TsmRoutine *tsm;
                                818                 :                :     BlockNumber pages;
                                819                 :                :     double      tuples;
                                820                 :                : 
                                821                 :                :     /*
                                822                 :                :      * Test any partial indexes of rel for applicability.  We must do this
                                823                 :                :      * first since partial unique indexes can affect size estimates.
                                824                 :                :      */
 2936                           825                 :            150 :     check_index_predicates(root, rel);
                                826                 :                : 
                                827                 :                :     /*
                                828                 :                :      * Call the sampling method's estimation function to estimate the number
                                829                 :                :      * of pages it will read and the number of tuples it will return.  (Note:
                                830                 :                :      * we assume the function returns sane values.)
                                831                 :                :      */
 3186                           832                 :            150 :     tsm = GetTsmRoutine(tsc->tsmhandler);
                                833                 :            150 :     tsm->SampleScanGetSampleSize(root, rel, tsc->args,
                                834                 :                :                                  &pages, &tuples);
                                835                 :                : 
                                836                 :                :     /*
                                837                 :                :      * For the moment, because we will only consider a SampleScan path for the
                                838                 :                :      * rel, it's okay to just overwrite the pages and tuples estimates for the
                                839                 :                :      * whole relation.  If we ever consider multiple path types for sampled
                                840                 :                :      * rels, we'll need more complication.
                                841                 :                :      */
                                842                 :            150 :     rel->pages = pages;
                                843                 :            150 :     rel->tuples = tuples;
                                844                 :                : 
                                845                 :                :     /* Mark rel with estimated output rows, width, etc */
 3257 simon@2ndQuadrant.co      846                 :            150 :     set_baserel_size_estimates(root, rel);
                                847                 :            150 : }
                                848                 :                : 
                                849                 :                : /*
                                850                 :                :  * set_tablesample_rel_pathlist
                                851                 :                :  *    Build access paths for a sampled relation
                                852                 :                :  */
                                853                 :                : static void
                                854                 :            150 : set_tablesample_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
                                855                 :                : {
                                856                 :                :     Relids      required_outer;
                                857                 :                :     Path       *path;
                                858                 :                : 
                                859                 :                :     /*
                                860                 :                :      * We don't support pushing join clauses into the quals of a samplescan,
                                861                 :                :      * but it could still have required parameterization due to LATERAL refs
                                862                 :                :      * in its tlist or TABLESAMPLE arguments.
                                863                 :                :      */
                                864                 :            150 :     required_outer = rel->lateral_relids;
                                865                 :                : 
                                866                 :                :     /* Consider sampled scan */
                                867                 :            150 :     path = create_samplescan_path(root, rel, required_outer);
                                868                 :                : 
                                869                 :                :     /*
                                870                 :                :      * If the sampling method does not support repeatable scans, we must avoid
                                871                 :                :      * plans that would scan the rel multiple times.  Ideally, we'd simply
                                872                 :                :      * avoid putting the rel on the inside of a nestloop join; but adding such
                                873                 :                :      * a consideration to the planner seems like a great deal of complication
                                874                 :                :      * to support an uncommon usage of second-rate sampling methods.  Instead,
                                875                 :                :      * if there is a risk that the query might perform an unsafe join, just
                                876                 :                :      * wrap the SampleScan in a Materialize node.  We can check for joins by
                                877                 :                :      * counting the membership of all_query_rels (note that this correctly
                                878                 :                :      * counts inheritance trees as single rels).  If we're inside a subquery,
                                879                 :                :      * we can't easily check whether a join might occur in the outer query, so
                                880                 :                :      * just assume one is possible.
                                881                 :                :      *
                                882                 :                :      * GetTsmRoutine is relatively expensive compared to the other tests here,
                                883                 :                :      * so check repeatable_across_scans last, even though that's a bit odd.
                                884                 :                :      */
 3186 tgl@sss.pgh.pa.us         885   [ +  +  +  + ]:            287 :     if ((root->query_level > 1 ||
  440                           886                 :            137 :          bms_membership(root->all_query_rels) != BMS_SINGLETON) &&
 2489                           887         [ +  + ]:             46 :         !(GetTsmRoutine(rte->tablesample->tsmhandler)->repeatable_across_scans))
                                888                 :                :     {
 3186                           889                 :              4 :         path = (Path *) create_material_path(rel, path);
                                890                 :                :     }
                                891                 :                : 
                                892                 :            150 :     add_path(rel, path);
                                893                 :                : 
                                894                 :                :     /* For the moment, at least, there are no other paths to consider */
 3257 simon@2ndQuadrant.co      895                 :            150 : }
                                896                 :                : 
                                897                 :                : /*
                                898                 :                :  * set_foreign_size
                                899                 :                :  *      Set size estimates for a foreign table RTE
                                900                 :                :  */
                                901                 :                : static void
 4461 tgl@sss.pgh.pa.us         902                 :           1179 : set_foreign_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
                                903                 :                : {
                                904                 :                :     /* Mark rel with estimated output rows, width, etc */
                                905                 :           1179 :     set_foreign_size_estimates(root, rel);
                                906                 :                : 
                                907                 :                :     /* Let FDW adjust the size estimates, if it can */
 4419                           908                 :           1179 :     rel->fdwroutine->GetForeignRelSize(root, rel, rte->relid);
                                909                 :                : 
                                910                 :                :     /* ... but do not let it set the rows estimate to zero */
 3185                           911                 :           1177 :     rel->rows = clamp_row_est(rel->rows);
                                912                 :                : 
                                913                 :                :     /*
                                914                 :                :      * Also, make sure rel->tuples is not insane relative to rel->rows.
                                915                 :                :      * Notably, this ensures sanity if pg_class.reltuples contains -1 and the
                                916                 :                :      * FDW doesn't do anything to replace that.
                                917                 :                :      */
 1381                           918         [ +  + ]:           1177 :     rel->tuples = Max(rel->tuples, rel->rows);
 4461                           919                 :           1177 : }
                                920                 :                : 
                                921                 :                : /*
                                922                 :                :  * set_foreign_pathlist
                                923                 :                :  *      Build access paths for a foreign table RTE
                                924                 :                :  */
                                925                 :                : static void
                                926                 :           1177 : set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
                                927                 :                : {
                                928                 :                :     /* Call the FDW's GetForeignPaths function to generate path(s) */
 4419                           929                 :           1177 :     rel->fdwroutine->GetForeignPaths(root, rel, rte->relid);
 4461                           930                 :           1177 : }
                                931                 :                : 
                                932                 :                : /*
                                933                 :                :  * set_append_rel_size
                                934                 :                :  *    Set size estimates for a simple "append relation"
                                935                 :                :  *
                                936                 :                :  * The passed-in rel and RTE represent the entire append relation.  The
                                937                 :                :  * relation's contents are computed by appending together the output of the
                                938                 :                :  * individual member relations.  Note that in the non-partitioned inheritance
                                939                 :                :  * case, the first member relation is actually the same table as is mentioned
                                940                 :                :  * in the parent RTE ... but it has a different RTE and RelOptInfo.  This is
                                941                 :                :  * a good thing because their outputs are not the same size.
                                942                 :                :  */
                                943                 :                : static void
                                944                 :          10128 : set_append_rel_size(PlannerInfo *root, RelOptInfo *rel,
                                945                 :                :                     Index rti, RangeTblEntry *rte)
                                946                 :                : {
 8365                           947                 :          10128 :     int         parentRTindex = rti;
                                948                 :                :     bool        has_live_children;
                                949                 :                :     double      parent_rows;
                                950                 :                :     double      parent_size;
                                951                 :                :     double     *parent_attrsizes;
                                952                 :                :     int         nattrs;
                                953                 :                :     ListCell   *l;
                                954                 :                : 
                                955                 :                :     /* Guard against stack overflow due to overly deep inheritance tree. */
 2404 rhaas@postgresql.org      956                 :          10128 :     check_stack_depth();
                                957                 :                : 
 2568                           958   [ +  +  -  + ]:          10128 :     Assert(IS_SIMPLE_REL(rel));
                                959                 :                : 
                                960                 :                :     /*
                                961                 :                :      * If this is a partitioned baserel, set the consider_partitionwise_join
                                962                 :                :      * flag; currently, we only consider partitionwise joins with the baserel
                                963                 :                :      * if its targetlist doesn't contain a whole-row Var.
                                964                 :                :      */
 2053 efujita@postgresql.o      965         [ +  + ]:          10128 :     if (enable_partitionwise_join &&
                                966         [ +  + ]:           2035 :         rel->reloptkind == RELOPT_BASEREL &&
                                967         [ +  - ]:           1693 :         rte->relkind == RELKIND_PARTITIONED_TABLE &&
  440 tgl@sss.pgh.pa.us         968         [ +  + ]:           1693 :         bms_is_empty(rel->attr_needed[InvalidAttrNumber - rel->min_attr]))
 2053 efujita@postgresql.o      969                 :           1655 :         rel->consider_partitionwise_join = true;
                                970                 :                : 
                                971                 :                :     /*
                                972                 :                :      * Initialize to compute size estimates for whole append relation.
                                973                 :                :      *
                                974                 :                :      * We handle width estimates by weighting the widths of different child
                                975                 :                :      * rels proportionally to their number of rows.  This is sensible because
                                976                 :                :      * the use of width estimates is mainly to compute the total relation
                                977                 :                :      * "footprint" if we have to sort or hash it.  To do this, we sum the
                                978                 :                :      * total equivalent size (in "double" arithmetic) and then divide by the
                                979                 :                :      * total rowcount estimate.  This is done separately for the total rel
                                980                 :                :      * width and each attribute.
                                981                 :                :      *
                                982                 :                :      * Note: if you consider changing this logic, beware that child rels could
                                983                 :                :      * have zero rows and/or width, if they were excluded by constraints.
                                984                 :                :      */
 3185 tgl@sss.pgh.pa.us         985                 :          10128 :     has_live_children = false;
 5770                           986                 :          10128 :     parent_rows = 0;
                                987                 :          10128 :     parent_size = 0;
                                988                 :          10128 :     nattrs = rel->max_attr - rel->min_attr + 1;
                                989                 :          10128 :     parent_attrsizes = (double *) palloc0(nattrs * sizeof(double));
                                990                 :                : 
 6648                           991   [ +  +  +  +  :          52258 :     foreach(l, root->append_rel_list)
                                              +  + ]
                                992                 :                :     {
                                993                 :          42131 :         AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
                                994                 :                :         int         childRTindex;
                                995                 :                :         RangeTblEntry *childRTE;
                                996                 :                :         RelOptInfo *childrel;
                                997                 :                :         List       *childrinfos;
                                998                 :                :         ListCell   *parentvars;
                                999                 :                :         ListCell   *childvars;
                               1000                 :                :         ListCell   *lc;
                               1001                 :                : 
                               1002                 :                :         /* append_rel_list contains all append rels; ignore others */
                               1003         [ +  + ]:          42131 :         if (appinfo->parent_relid != parentRTindex)
                               1004                 :          20941 :             continue;
                               1005                 :                : 
                               1006                 :          21316 :         childRTindex = appinfo->child_relid;
 6203                          1007                 :          21316 :         childRTE = root->simple_rte_array[childRTindex];
                               1008                 :                : 
                               1009                 :                :         /*
                               1010                 :                :          * The child rel's RelOptInfo was already created during
                               1011                 :                :          * add_other_rels_to_query.
                               1012                 :                :          */
 6417                          1013                 :          21316 :         childrel = find_base_rel(root, childRTindex);
                               1014         [ -  + ]:          21316 :         Assert(childrel->reloptkind == RELOPT_OTHER_MEMBER_REL);
                               1015                 :                : 
                               1016                 :                :         /* We may have already proven the child to be dummy. */
 1842                          1017         [ +  + ]:          21316 :         if (IS_DUMMY_REL(childrel))
 1899 alvherre@alvh.no-ip.     1018                 :              9 :             continue;
                               1019                 :                : 
                               1020                 :                :         /*
                               1021                 :                :          * We have to copy the parent's targetlist and quals to the child,
                               1022                 :                :          * with appropriate substitution of variables.  However, the
                               1023                 :                :          * baserestrictinfo quals were already copied/substituted when the
                               1024                 :                :          * child RelOptInfo was built.  So we don't need any additional setup
                               1025                 :                :          * before applying constraint exclusion.
                               1026                 :                :          */
 5857 tgl@sss.pgh.pa.us        1027         [ +  + ]:          21307 :         if (relation_excluded_by_constraints(root, childrel, childRTE))
                               1028                 :                :         {
                               1029                 :                :             /*
                               1030                 :                :              * This child need not be scanned, so we can omit it from the
                               1031                 :                :              * appendrel.
                               1032                 :                :              */
 6168                          1033                 :             48 :             set_dummy_rel_pathlist(childrel);
 6203                          1034                 :             48 :             continue;
                               1035                 :                :         }
                               1036                 :                : 
                               1037                 :                :         /*
                               1038                 :                :          * Constraint exclusion failed, so copy the parent's join quals and
                               1039                 :                :          * targetlist to the child, with appropriate variable substitutions.
                               1040                 :                :          *
                               1041                 :                :          * We skip join quals that came from above outer joins that can null
                               1042                 :                :          * this rel, since they would be of no value while generating paths
                               1043                 :                :          * for the child.  This saves some effort while processing the child
                               1044                 :                :          * rel, and it also avoids an implementation restriction in
                               1045                 :                :          * adjust_appendrel_attrs (it can't apply nullingrels to a non-Var).
                               1046                 :                :          */
  399                          1047                 :          21259 :         childrinfos = NIL;
                               1048   [ +  +  +  +  :          27577 :         foreach(lc, rel->joininfo)
                                              +  + ]
                               1049                 :                :         {
                               1050                 :           6318 :             RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
                               1051                 :                : 
                               1052         [ +  + ]:           6318 :             if (!bms_overlap(rinfo->clause_relids, rel->nulling_relids))
                               1053                 :           5157 :                 childrinfos = lappend(childrinfos,
                               1054                 :           5157 :                                       adjust_appendrel_attrs(root,
                               1055                 :                :                                                              (Node *) rinfo,
                               1056                 :                :                                                              1, &appinfo));
                               1057                 :                :         }
                               1058                 :          21259 :         childrel->joininfo = childrinfos;
                               1059                 :                : 
                               1060                 :                :         /*
                               1061                 :                :          * Now for the child's targetlist.
                               1062                 :                :          *
                               1063                 :                :          * NB: the resulting childrel->reltarget->exprs may contain arbitrary
                               1064                 :                :          * expressions, which otherwise would not occur in a rel's targetlist.
                               1065                 :                :          * Code that might be looking at an appendrel child must cope with
                               1066                 :                :          * such.  (Normally, a rel's targetlist would only include Vars and
                               1067                 :                :          * PlaceHolderVars.)  XXX we do not bother to update the cost or width
                               1068                 :                :          * fields of childrel->reltarget; not clear if that would be useful.
                               1069                 :                :          */
 1910 efujita@postgresql.o     1070                 :          42518 :         childrel->reltarget->exprs = (List *)
                               1071                 :          21259 :             adjust_appendrel_attrs(root,
                               1072                 :          21259 :                                    (Node *) rel->reltarget->exprs,
                               1073                 :                :                                    1, &appinfo);
                               1074                 :                : 
                               1075                 :                :         /*
                               1076                 :                :          * We have to make child entries in the EquivalenceClass data
                               1077                 :                :          * structures as well.  This is needed either if the parent
                               1078                 :                :          * participates in some eclass joins (because we will want to consider
                               1079                 :                :          * inner-indexscan joins on the individual children) or if the parent
                               1080                 :                :          * has useful pathkeys (because we should try to build MergeAppend
                               1081                 :                :          * paths that produce those sort orderings).
                               1082                 :                :          */
                               1083   [ +  +  +  + ]:          21259 :         if (rel->has_eclass_joins || has_useful_pathkeys(root, rel))
                               1084                 :          10809 :             add_child_rel_equivalences(root, appinfo, rel, childrel);
                               1085                 :          21259 :         childrel->has_eclass_joins = rel->has_eclass_joins;
                               1086                 :                : 
                               1087                 :                :         /*
                               1088                 :                :          * Note: we could compute appropriate attr_needed data for the child's
                               1089                 :                :          * variables, by transforming the parent's attr_needed through the
                               1090                 :                :          * translated_vars mapping.  However, currently there's no need
                               1091                 :                :          * because attr_needed is only examined for base relations not
                               1092                 :                :          * otherrels.  So we just leave the child's attr_needed empty.
                               1093                 :                :          */
                               1094                 :                : 
                               1095                 :                :         /*
                               1096                 :                :          * If we consider partitionwise joins with the parent rel, do the same
                               1097                 :                :          * for partitioned child rels.
                               1098                 :                :          *
                               1099                 :                :          * Note: here we abuse the consider_partitionwise_join flag by setting
                               1100                 :                :          * it for child rels that are not themselves partitioned.  We do so to
                               1101                 :                :          * tell try_partitionwise_join() that the child rel is sufficiently
                               1102                 :                :          * valid to be used as a per-partition input, even if it later gets
                               1103                 :                :          * proven to be dummy.  (It's not usable until we've set up the
                               1104                 :                :          * reltarget and EC entries, which we just did.)
                               1105                 :                :          */
                               1106         [ +  + ]:          21259 :         if (rel->consider_partitionwise_join)
 2053                          1107                 :           5452 :             childrel->consider_partitionwise_join = true;
                               1108                 :                : 
                               1109                 :                :         /*
                               1110                 :                :          * If parallelism is allowable for this query in general, see whether
                               1111                 :                :          * it's allowable for this childrel in particular.  But if we've
                               1112                 :                :          * already decided the appendrel is not parallel-safe as a whole,
                               1113                 :                :          * there's no point in considering parallelism for this child.  For
                               1114                 :                :          * consistency, do this before calling set_rel_size() for the child.
                               1115                 :                :          */
 2842 tgl@sss.pgh.pa.us        1116   [ +  +  +  + ]:          21259 :         if (root->glob->parallelModeOK && rel->consider_parallel)
                               1117                 :          15207 :             set_rel_consider_parallel(root, childrel, childRTE);
                               1118                 :                : 
                               1119                 :                :         /*
                               1120                 :                :          * Compute the child's size.
                               1121                 :                :          */
 4461                          1122                 :          21259 :         set_rel_size(root, childrel, childRTindex, childRTE);
                               1123                 :                : 
                               1124                 :                :         /*
                               1125                 :                :          * It is possible that constraint exclusion detected a contradiction
                               1126                 :                :          * within a child subquery, even though we didn't prove one above. If
                               1127                 :                :          * so, we can skip this child.
                               1128                 :                :          */
                               1129         [ +  + ]:          21258 :         if (IS_DUMMY_REL(childrel))
 4586                          1130                 :             69 :             continue;
                               1131                 :                : 
                               1132                 :                :         /* We have at least one live child. */
 3185                          1133                 :          21189 :         has_live_children = true;
                               1134                 :                : 
                               1135                 :                :         /*
                               1136                 :                :          * If any live child is not parallel-safe, treat the whole appendrel
                               1137                 :                :          * as not parallel-safe.  In future we might be able to generate plans
                               1138                 :                :          * in which some children are farmed out to workers while others are
                               1139                 :                :          * not; but we don't have that today, so it's a waste to consider
                               1140                 :                :          * partial paths anywhere in the appendrel unless it's all safe.
                               1141                 :                :          * (Child rels visited before this one will be unmarked in
                               1142                 :                :          * set_append_rel_pathlist().)
                               1143                 :                :          */
 2842                          1144         [ +  + ]:          21189 :         if (!childrel->consider_parallel)
                               1145                 :           6295 :             rel->consider_parallel = false;
                               1146                 :                : 
                               1147                 :                :         /*
                               1148                 :                :          * Accumulate size information from each live child.
                               1149                 :                :          */
 3185                          1150         [ -  + ]:          21189 :         Assert(childrel->rows > 0);
                               1151                 :                : 
                               1152                 :          21189 :         parent_rows += childrel->rows;
 2953                          1153                 :          21189 :         parent_size += childrel->reltarget->width * childrel->rows;
                               1154                 :                : 
                               1155                 :                :         /*
                               1156                 :                :          * Accumulate per-column estimates too.  We need not do anything for
                               1157                 :                :          * PlaceHolderVars in the parent list.  If child expression isn't a
                               1158                 :                :          * Var, or we didn't record a width estimate for it, we have to fall
                               1159                 :                :          * back on a datatype-based estimate.
                               1160                 :                :          *
                               1161                 :                :          * By construction, child's targetlist is 1-to-1 with parent's.
                               1162                 :                :          */
                               1163   [ +  +  +  +  :          67533 :         forboth(parentvars, rel->reltarget->exprs,
                                     +  +  +  +  +  
                                        +  +  -  +  
                                                 + ]
                               1164                 :                :                 childvars, childrel->reltarget->exprs)
                               1165                 :                :         {
 3185                          1166                 :          46344 :             Var        *parentvar = (Var *) lfirst(parentvars);
                               1167                 :          46344 :             Node       *childvar = (Node *) lfirst(childvars);
                               1168                 :                : 
 1110                          1169   [ +  +  +  + ]:          46344 :             if (IsA(parentvar, Var) && parentvar->varno == parentRTindex)
                               1170                 :                :             {
 3185                          1171                 :          40462 :                 int         pndx = parentvar->varattno - rel->min_attr;
                               1172                 :          40462 :                 int32       child_width = 0;
                               1173                 :                : 
                               1174         [ +  + ]:          40462 :                 if (IsA(childvar, Var) &&
                               1175         [ +  + ]:          39426 :                     ((Var *) childvar)->varno == childrel->relid)
                               1176                 :                :                 {
                               1177                 :          39393 :                     int         cndx = ((Var *) childvar)->varattno - childrel->min_attr;
                               1178                 :                : 
                               1179                 :          39393 :                     child_width = childrel->attr_widths[cndx];
                               1180                 :                :                 }
                               1181         [ +  + ]:          40462 :                 if (child_width <= 0)
                               1182                 :           1069 :                     child_width = get_typavgwidth(exprType(childvar),
                               1183                 :                :                                                   exprTypmod(childvar));
                               1184         [ -  + ]:          40462 :                 Assert(child_width > 0);
                               1185                 :          40462 :                 parent_attrsizes[pndx] += child_width * childrel->rows;
                               1186                 :                :             }
                               1187                 :                :         }
                               1188                 :                :     }
                               1189                 :                : 
                               1190         [ +  + ]:          10127 :     if (has_live_children)
                               1191                 :                :     {
                               1192                 :                :         /*
                               1193                 :                :          * Save the finished size estimates.
                               1194                 :                :          */
                               1195                 :                :         int         i;
                               1196                 :                : 
                               1197         [ -  + ]:           9980 :         Assert(parent_rows > 0);
                               1198                 :           9980 :         rel->rows = parent_rows;
 2953                          1199                 :           9980 :         rel->reltarget->width = rint(parent_size / parent_rows);
 5770                          1200         [ +  + ]:         101586 :         for (i = 0; i < nattrs; i++)
                               1201                 :          91606 :             rel->attr_widths[i] = rint(parent_attrsizes[i] / parent_rows);
                               1202                 :                : 
                               1203                 :                :         /*
                               1204                 :                :          * Set "raw tuples" count equal to "rows" for the appendrel; needed
                               1205                 :                :          * because some places assume rel->tuples is valid for any baserel.
                               1206                 :                :          */
 3185                          1207                 :           9980 :         rel->tuples = parent_rows;
                               1208                 :                : 
                               1209                 :                :         /*
                               1210                 :                :          * Note that we leave rel->pages as zero; this is important to avoid
                               1211                 :                :          * double-counting the appendrel tree in total_table_pages.
                               1212                 :                :          */
                               1213                 :                :     }
                               1214                 :                :     else
                               1215                 :                :     {
                               1216                 :                :         /*
                               1217                 :                :          * All children were excluded by constraints, so mark the whole
                               1218                 :                :          * appendrel dummy.  We must do this in this phase so that the rel's
                               1219                 :                :          * dummy-ness is visible when we generate paths for other rels.
                               1220                 :                :          */
                               1221                 :            147 :         set_dummy_rel_pathlist(rel);
                               1222                 :                :     }
                               1223                 :                : 
 5770                          1224                 :          10127 :     pfree(parent_attrsizes);
 4461                          1225                 :          10127 : }
                               1226                 :                : 
                               1227                 :                : /*
                               1228                 :                :  * set_append_rel_pathlist
                               1229                 :                :  *    Build access paths for an "append relation"
                               1230                 :                :  */
                               1231                 :                : static void
                               1232                 :           9980 : set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
                               1233                 :                :                         Index rti, RangeTblEntry *rte)
                               1234                 :                : {
                               1235                 :           9980 :     int         parentRTindex = rti;
                               1236                 :           9980 :     List       *live_childrels = NIL;
                               1237                 :                :     ListCell   *l;
                               1238                 :                : 
                               1239                 :                :     /*
                               1240                 :                :      * Generate access paths for each member relation, and remember the
                               1241                 :                :      * non-dummy children.
                               1242                 :                :      */
                               1243   [ +  -  +  +  :          51903 :     foreach(l, root->append_rel_list)
                                              +  + ]
                               1244                 :                :     {
                               1245                 :          41923 :         AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
                               1246                 :                :         int         childRTindex;
                               1247                 :                :         RangeTblEntry *childRTE;
                               1248                 :                :         RelOptInfo *childrel;
                               1249                 :                : 
                               1250                 :                :         /* append_rel_list contains all append rels; ignore others */
                               1251         [ +  + ]:          41923 :         if (appinfo->parent_relid != parentRTindex)
                               1252                 :          20641 :             continue;
                               1253                 :                : 
                               1254                 :                :         /* Re-locate the child RTE and RelOptInfo */
                               1255                 :          21282 :         childRTindex = appinfo->child_relid;
                               1256                 :          21282 :         childRTE = root->simple_rte_array[childRTindex];
                               1257                 :          21282 :         childrel = root->simple_rel_array[childRTindex];
                               1258                 :                : 
                               1259                 :                :         /*
                               1260                 :                :          * If set_append_rel_size() decided the parent appendrel was
                               1261                 :                :          * parallel-unsafe at some point after visiting this child rel, we
                               1262                 :                :          * need to propagate the unsafety marking down to the child, so that
                               1263                 :                :          * we don't generate useless partial paths for it.
                               1264                 :                :          */
 2842                          1265         [ +  + ]:          21282 :         if (!rel->consider_parallel)
                               1266                 :           6348 :             childrel->consider_parallel = false;
                               1267                 :                : 
                               1268                 :                :         /*
                               1269                 :                :          * Compute the child's access paths.
                               1270                 :                :          */
 4461                          1271                 :          21282 :         set_rel_pathlist(root, childrel, childRTindex, childRTE);
                               1272                 :                : 
                               1273                 :                :         /*
                               1274                 :                :          * If child is dummy, ignore it.
                               1275                 :                :          */
                               1276         [ +  + ]:          21282 :         if (IS_DUMMY_REL(childrel))
                               1277                 :             93 :             continue;
                               1278                 :                : 
                               1279                 :                :         /*
                               1280                 :                :          * Child is live, so add it to the live_childrels list for use below.
                               1281                 :                :          */
 4264                          1282                 :          21189 :         live_childrels = lappend(live_childrels, childrel);
                               1283                 :                :     }
                               1284                 :                : 
                               1285                 :                :     /* Add paths to the append relation. */
 2588 rhaas@postgresql.org     1286                 :           9980 :     add_paths_to_append_rel(root, rel, live_childrels);
                               1287                 :           9980 : }
                               1288                 :                : 
                               1289                 :                : 
                               1290                 :                : /*
                               1291                 :                :  * add_paths_to_append_rel
                               1292                 :                :  *      Generate paths for the given append relation given the set of non-dummy
                               1293                 :                :  *      child rels.
                               1294                 :                :  *
                               1295                 :                :  * The function collects all parameterizations and orderings supported by the
                               1296                 :                :  * non-dummy children. For every such parameterization or ordering, it creates
                               1297                 :                :  * an append path collecting one path from each non-dummy child with given
                               1298                 :                :  * parameterization or ordering. Similarly it collects partial paths from
                               1299                 :                :  * non-dummy children to create partial append paths.
                               1300                 :                :  */
                               1301                 :                : void
                               1302                 :          17261 : add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel,
                               1303                 :                :                         List *live_childrels)
                               1304                 :                : {
                               1305                 :          17261 :     List       *subpaths = NIL;
                               1306                 :          17261 :     bool        subpaths_valid = true;
  192 drowley@postgresql.o     1307                 :GNC       17261 :     List       *startup_subpaths = NIL;
                               1308                 :          17261 :     bool        startup_subpaths_valid = true;
 2588 rhaas@postgresql.org     1309                 :CBC       17261 :     List       *partial_subpaths = NIL;
 2322                          1310                 :          17261 :     List       *pa_partial_subpaths = NIL;
                               1311                 :          17261 :     List       *pa_nonpartial_subpaths = NIL;
 2588                          1312                 :          17261 :     bool        partial_subpaths_valid = true;
                               1313                 :                :     bool        pa_subpaths_valid;
                               1314                 :          17261 :     List       *all_child_pathkeys = NIL;
                               1315                 :          17261 :     List       *all_child_outers = NIL;
                               1316                 :                :     ListCell   *l;
 2322                          1317                 :          17261 :     double      partial_rows = -1;
                               1318                 :                : 
                               1319                 :                :     /* If appropriate, consider parallel append */
 2125 akapila@postgresql.o     1320   [ +  +  +  + ]:          17261 :     pa_subpaths_valid = enable_parallel_append && rel->consider_parallel;
                               1321                 :                : 
                               1322                 :                :     /*
                               1323                 :                :      * For every non-dummy child, remember the cheapest path.  Also, identify
                               1324                 :                :      * all pathkeys (orderings) and parameterizations (required_outer sets)
                               1325                 :                :      * available for the non-dummy member relations.
                               1326                 :                :      */
 2588 rhaas@postgresql.org     1327   [ +  -  +  +  :          52809 :     foreach(l, live_childrels)
                                              +  + ]
                               1328                 :                :     {
                               1329                 :          35548 :         RelOptInfo *childrel = lfirst(l);
                               1330                 :                :         ListCell   *lcp;
 2322                          1331                 :          35548 :         Path       *cheapest_partial_path = NULL;
                               1332                 :                : 
                               1333                 :                :         /*
                               1334                 :                :          * If child has an unparameterized cheapest-total path, add that to
                               1335                 :                :          * the unparameterized Append path we are constructing for the parent.
                               1336                 :                :          * If not, there's no workable unparameterized path.
                               1337                 :                :          *
                               1338                 :                :          * With partitionwise aggregates, the child rel's pathlist may be
                               1339                 :                :          * empty, so don't assume that a path exists here.
                               1340                 :                :          */
 2215                          1341         [ +  - ]:          35548 :         if (childrel->pathlist != NIL &&
                               1342         [ +  + ]:          35548 :             childrel->cheapest_total_path->param_info == NULL)
 2322                          1343                 :          35182 :             accumulate_append_subpath(childrel->cheapest_total_path,
                               1344                 :                :                                       &subpaths, NULL);
                               1345                 :                :         else
 4264 tgl@sss.pgh.pa.us        1346                 :            366 :             subpaths_valid = false;
                               1347                 :                : 
                               1348                 :                :         /*
                               1349                 :                :          * When the planner is considering cheap startup plans, we'll also
                               1350                 :                :          * collect all the cheapest_startup_paths (if set) and build an
                               1351                 :                :          * AppendPath containing those as subpaths.
                               1352                 :                :          */
  187 drowley@postgresql.o     1353   [ +  +  +  + ]:GNC       35548 :         if (rel->consider_startup && childrel->cheapest_startup_path != NULL)
                               1354                 :                :         {
                               1355                 :                :             /* cheapest_startup_path must not be a parameterized path. */
                               1356         [ -  + ]:            643 :             Assert(childrel->cheapest_startup_path->param_info == NULL);
  192                          1357                 :            643 :             accumulate_append_subpath(childrel->cheapest_startup_path,
                               1358                 :                :                                       &startup_subpaths,
                               1359                 :                :                                       NULL);
                               1360                 :                :         }
                               1361                 :                :         else
                               1362                 :          34905 :             startup_subpaths_valid = false;
                               1363                 :                : 
                               1364                 :                : 
                               1365                 :                :         /* Same idea, but for a partial plan. */
 3007 rhaas@postgresql.org     1366         [ +  + ]:CBC       35548 :         if (childrel->partial_pathlist != NIL)
                               1367                 :                :         {
 2322                          1368                 :          23682 :             cheapest_partial_path = linitial(childrel->partial_pathlist);
                               1369                 :          23682 :             accumulate_append_subpath(cheapest_partial_path,
                               1370                 :                :                                       &partial_subpaths, NULL);
                               1371                 :                :         }
                               1372                 :                :         else
 3007                          1373                 :          11866 :             partial_subpaths_valid = false;
                               1374                 :                : 
                               1375                 :                :         /*
                               1376                 :                :          * Same idea, but for a parallel append mixing partial and non-partial
                               1377                 :                :          * paths.
                               1378                 :                :          */
 2322                          1379         [ +  + ]:          35548 :         if (pa_subpaths_valid)
                               1380                 :                :         {
                               1381                 :          25036 :             Path       *nppath = NULL;
                               1382                 :                : 
                               1383                 :                :             nppath =
                               1384                 :          25036 :                 get_cheapest_parallel_safe_total_inner(childrel->pathlist);
                               1385                 :                : 
                               1386   [ +  +  +  + ]:          25036 :             if (cheapest_partial_path == NULL && nppath == NULL)
                               1387                 :                :             {
                               1388                 :                :                 /* Neither a partial nor a parallel-safe path?  Forget it. */
                               1389                 :            225 :                 pa_subpaths_valid = false;
                               1390                 :                :             }
                               1391   [ +  +  +  + ]:          24811 :             else if (nppath == NULL ||
                               1392                 :          23457 :                      (cheapest_partial_path != NULL &&
                               1393         [ +  + ]:          23457 :                       cheapest_partial_path->total_cost < nppath->total_cost))
                               1394                 :                :             {
                               1395                 :                :                 /* Partial path is cheaper or the only option. */
                               1396         [ -  + ]:          23339 :                 Assert(cheapest_partial_path != NULL);
                               1397                 :          23339 :                 accumulate_append_subpath(cheapest_partial_path,
                               1398                 :                :                                           &pa_partial_subpaths,
                               1399                 :                :                                           &pa_nonpartial_subpaths);
                               1400                 :                :             }
                               1401                 :                :             else
                               1402                 :                :             {
                               1403                 :                :                 /*
                               1404                 :                :                  * Either we've got only a non-partial path, or we think that
                               1405                 :                :                  * a single backend can execute the best non-partial path
                               1406                 :                :                  * faster than all the parallel backends working together can
                               1407                 :                :                  * execute the best partial path.
                               1408                 :                :                  *
                               1409                 :                :                  * It might make sense to be more aggressive here.  Even if
                               1410                 :                :                  * the best non-partial path is more expensive than the best
                               1411                 :                :                  * partial path, it could still be better to choose the
                               1412                 :                :                  * non-partial path if there are several such paths that can
                               1413                 :                :                  * be given to different workers.  For now, we don't try to
                               1414                 :                :                  * figure that out.
                               1415                 :                :                  */
                               1416                 :           1472 :                 accumulate_append_subpath(nppath,
                               1417                 :                :                                           &pa_nonpartial_subpaths,
                               1418                 :                :                                           NULL);
                               1419                 :                :             }
                               1420                 :                :         }
                               1421                 :                : 
                               1422                 :                :         /*
                               1423                 :                :          * Collect lists of all the available path orderings and
                               1424                 :                :          * parameterizations for all the children.  We use these as a
                               1425                 :                :          * heuristic to indicate which sort orderings and parameterizations we
                               1426                 :                :          * should build Append and MergeAppend paths for.
                               1427                 :                :          */
 4461 tgl@sss.pgh.pa.us        1428   [ +  -  +  +  :          81052 :         foreach(lcp, childrel->pathlist)
                                              +  + ]
                               1429                 :                :         {
                               1430                 :          45504 :             Path       *childpath = (Path *) lfirst(lcp);
                               1431                 :          45504 :             List       *childkeys = childpath->pathkeys;
 4378                          1432         [ +  + ]:          45504 :             Relids      childouter = PATH_REQ_OUTER(childpath);
                               1433                 :                : 
                               1434                 :                :             /* Unsorted paths don't contribute to pathkey list */
 4461                          1435         [ +  + ]:          45504 :             if (childkeys != NIL)
                               1436                 :                :             {
                               1437                 :                :                 ListCell   *lpk;
                               1438                 :          10055 :                 bool        found = false;
                               1439                 :                : 
                               1440                 :                :                 /* Have we already seen this ordering? */
                               1441   [ +  +  +  +  :          10149 :                 foreach(lpk, all_child_pathkeys)
                                              +  + ]
                               1442                 :                :                 {
                               1443                 :           7265 :                     List       *existing_pathkeys = (List *) lfirst(lpk);
                               1444                 :                : 
                               1445         [ +  + ]:           7265 :                     if (compare_pathkeys(existing_pathkeys,
                               1446                 :                :                                          childkeys) == PATHKEYS_EQUAL)
                               1447                 :                :                     {
                               1448                 :           7171 :                         found = true;
                               1449                 :           7171 :                         break;
                               1450                 :                :                     }
                               1451                 :                :                 }
                               1452         [ +  + ]:          10055 :                 if (!found)
                               1453                 :                :                 {
                               1454                 :                :                     /* No, so add it to all_child_pathkeys */
                               1455                 :           2884 :                     all_child_pathkeys = lappend(all_child_pathkeys,
                               1456                 :                :                                                  childkeys);
                               1457                 :                :                 }
                               1458                 :                :             }
                               1459                 :                : 
                               1460                 :                :             /* Unparameterized paths don't contribute to param-set list */
                               1461         [ +  + ]:          45504 :             if (childouter)
                               1462                 :                :             {
                               1463                 :                :                 ListCell   *lco;
                               1464                 :           2940 :                 bool        found = false;
                               1465                 :                : 
                               1466                 :                :                 /* Have we already seen this param set? */
                               1467   [ +  +  +  +  :           3246 :                 foreach(lco, all_child_outers)
                                              +  + ]
                               1468                 :                :                 {
 4326 bruce@momjian.us         1469                 :           2133 :                     Relids      existing_outers = (Relids) lfirst(lco);
                               1470                 :                : 
 4461 tgl@sss.pgh.pa.us        1471         [ +  + ]:           2133 :                     if (bms_equal(existing_outers, childouter))
                               1472                 :                :                     {
                               1473                 :           1827 :                         found = true;
                               1474                 :           1827 :                         break;
                               1475                 :                :                     }
                               1476                 :                :                 }
                               1477         [ +  + ]:           2940 :                 if (!found)
                               1478                 :                :                 {
                               1479                 :                :                     /* No, so add it to all_child_outers */
                               1480                 :           1113 :                     all_child_outers = lappend(all_child_outers,
                               1481                 :                :                                                childouter);
                               1482                 :                :                 }
                               1483                 :                :             }
                               1484                 :                :         }
                               1485                 :                :     }
                               1486                 :                : 
                               1487                 :                :     /*
                               1488                 :                :      * If we found unparameterized paths for all children, build an unordered,
                               1489                 :                :      * unparameterized Append path for the rel.  (Note: this is correct even
                               1490                 :                :      * if we have zero or one live subpath due to constraint exclusion.)
                               1491                 :                :      */
 4264                          1492         [ +  + ]:          17261 :     if (subpaths_valid)
 2199 alvherre@alvh.no-ip.     1493                 :          17105 :         add_path(rel, (Path *) create_append_path(root, rel, subpaths, NIL,
                               1494                 :                :                                                   NIL, NULL, 0, false,
                               1495                 :                :                                                   -1));
                               1496                 :                : 
                               1497                 :                :     /* build an AppendPath for the cheap startup paths, if valid */
  192 drowley@postgresql.o     1498         [ +  + ]:GNC       17261 :     if (startup_subpaths_valid)
                               1499                 :            272 :         add_path(rel, (Path *) create_append_path(root, rel, startup_subpaths,
                               1500                 :                :                                                   NIL, NIL, NULL, 0, false, -1));
                               1501                 :                : 
                               1502                 :                :     /*
                               1503                 :                :      * Consider an append of unordered, unparameterized partial paths.  Make
                               1504                 :                :      * it parallel-aware if possible.
                               1505                 :                :      */
 1865 tgl@sss.pgh.pa.us        1506   [ +  +  +  - ]:CBC       17261 :     if (partial_subpaths_valid && partial_subpaths != NIL)
                               1507                 :                :     {
                               1508                 :                :         AppendPath *appendpath;
                               1509                 :                :         ListCell   *lc;
 2866 rhaas@postgresql.org     1510                 :          10648 :         int         parallel_workers = 0;
                               1511                 :                : 
                               1512                 :                :         /* Find the highest number of workers requested for any subpath. */
 3007                          1513   [ +  -  +  +  :          36366 :         foreach(lc, partial_subpaths)
                                              +  + ]
                               1514                 :                :         {
                               1515                 :          25718 :             Path       *path = lfirst(lc);
                               1516                 :                : 
 2866                          1517                 :          25718 :             parallel_workers = Max(parallel_workers, path->parallel_workers);
                               1518                 :                :         }
                               1519         [ -  + ]:          10648 :         Assert(parallel_workers > 0);
                               1520                 :                : 
                               1521                 :                :         /*
                               1522                 :                :          * If the use of parallel append is permitted, always request at least
                               1523                 :                :          * log2(# of children) workers.  We assume it can be useful to have
                               1524                 :                :          * extra workers in this case because they will be spread out across
                               1525                 :                :          * the children.  The precise formula is just a guess, but we don't
                               1526                 :                :          * want to end up with a radically different answer for a table with N
                               1527                 :                :          * partitions vs. an unpartitioned table with the same data, so the
                               1528                 :                :          * use of some kind of log-scaling here seems to make some sense.
                               1529                 :                :          */
 2322                          1530         [ +  + ]:          10648 :         if (enable_parallel_append)
                               1531                 :                :         {
                               1532         [ +  + ]:          10624 :             parallel_workers = Max(parallel_workers,
                               1533                 :                :                                    pg_leftmost_one_pos32(list_length(live_childrels)) + 1);
                               1534                 :          10624 :             parallel_workers = Min(parallel_workers,
                               1535                 :                :                                    max_parallel_workers_per_gather);
                               1536                 :                :         }
                               1537         [ -  + ]:          10648 :         Assert(parallel_workers > 0);
                               1538                 :                : 
                               1539                 :                :         /* Generate a partial append path. */
 2199 alvherre@alvh.no-ip.     1540                 :          10648 :         appendpath = create_append_path(root, rel, NIL, partial_subpaths,
                               1541                 :                :                                         NIL, NULL, parallel_workers,
                               1542                 :                :                                         enable_parallel_append,
                               1543                 :                :                                         -1);
                               1544                 :                : 
                               1545                 :                :         /*
                               1546                 :                :          * Make sure any subsequent partial paths use the same row count
                               1547                 :                :          * estimate.
                               1548                 :                :          */
 2322 rhaas@postgresql.org     1549                 :          10648 :         partial_rows = appendpath->path.rows;
                               1550                 :                : 
                               1551                 :                :         /* Add the path. */
                               1552                 :          10648 :         add_partial_path(rel, (Path *) appendpath);
                               1553                 :                :     }
                               1554                 :                : 
                               1555                 :                :     /*
                               1556                 :                :      * Consider a parallel-aware append using a mix of partial and non-partial
                               1557                 :                :      * paths.  (This only makes sense if there's at least one child which has
                               1558                 :                :      * a non-partial path that is substantially cheaper than any partial path;
                               1559                 :                :      * otherwise, we should use the append path added in the previous step.)
                               1560                 :                :      */
                               1561   [ +  +  +  + ]:          17261 :     if (pa_subpaths_valid && pa_nonpartial_subpaths != NIL)
                               1562                 :                :     {
                               1563                 :                :         AppendPath *appendpath;
                               1564                 :                :         ListCell   *lc;
                               1565                 :            767 :         int         parallel_workers = 0;
                               1566                 :                : 
                               1567                 :                :         /*
                               1568                 :                :          * Find the highest number of workers requested for any partial
                               1569                 :                :          * subpath.
                               1570                 :                :          */
                               1571   [ +  +  +  +  :           1209 :         foreach(lc, pa_partial_subpaths)
                                              +  + ]
                               1572                 :                :         {
                               1573                 :            442 :             Path       *path = lfirst(lc);
                               1574                 :                : 
                               1575                 :            442 :             parallel_workers = Max(parallel_workers, path->parallel_workers);
                               1576                 :                :         }
                               1577                 :                : 
                               1578                 :                :         /*
                               1579                 :                :          * Same formula here as above.  It's even more important in this
                               1580                 :                :          * instance because the non-partial paths won't contribute anything to
                               1581                 :                :          * the planned number of parallel workers.
                               1582                 :                :          */
                               1583         [ +  - ]:            767 :         parallel_workers = Max(parallel_workers,
                               1584                 :                :                                pg_leftmost_one_pos32(list_length(live_childrels)) + 1);
                               1585                 :            767 :         parallel_workers = Min(parallel_workers,
                               1586                 :                :                                max_parallel_workers_per_gather);
                               1587         [ -  + ]:            767 :         Assert(parallel_workers > 0);
                               1588                 :                : 
 2199 alvherre@alvh.no-ip.     1589                 :            767 :         appendpath = create_append_path(root, rel, pa_nonpartial_subpaths,
                               1590                 :                :                                         pa_partial_subpaths,
                               1591                 :                :                                         NIL, NULL, parallel_workers, true,
                               1592                 :                :                                         partial_rows);
 3007 rhaas@postgresql.org     1593                 :            767 :         add_partial_path(rel, (Path *) appendpath);
                               1594                 :                :     }
                               1595                 :                : 
                               1596                 :                :     /*
                               1597                 :                :      * Also build unparameterized ordered append paths based on the collected
                               1598                 :                :      * list of child pathkeys.
                               1599                 :                :      */
 4264 tgl@sss.pgh.pa.us        1600         [ +  + ]:          17261 :     if (subpaths_valid)
 1836                          1601                 :          17105 :         generate_orderedappend_paths(root, rel, live_childrels,
                               1602                 :                :                                      all_child_pathkeys);
                               1603                 :                : 
                               1604                 :                :     /*
                               1605                 :                :      * Build Append paths for each parameterization seen among the child rels.
                               1606                 :                :      * (This may look pretty expensive, but in most cases of practical
                               1607                 :                :      * interest, the child rels will expose mostly the same parameterizations,
                               1608                 :                :      * so that not that many cases actually get considered here.)
                               1609                 :                :      *
                               1610                 :                :      * The Append node itself cannot enforce quals, so all qual checking must
                               1611                 :                :      * be done in the child paths.  This means that to have a parameterized
                               1612                 :                :      * Append path, we must have the exact same parameterization for each
                               1613                 :                :      * child path; otherwise some children might be failing to check the
                               1614                 :                :      * moved-down quals.  To make them match up, we can try to increase the
                               1615                 :                :      * parameterization of lesser-parameterized paths.
                               1616                 :                :      */
 4461                          1617   [ +  +  +  +  :          18374 :     foreach(l, all_child_outers)
                                              +  + ]
                               1618                 :                :     {
 4326 bruce@momjian.us         1619                 :           1113 :         Relids      required_outer = (Relids) lfirst(l);
                               1620                 :                :         ListCell   *lcr;
                               1621                 :                : 
                               1622                 :                :         /* Select the child paths for an Append with this parameterization */
 4461 tgl@sss.pgh.pa.us        1623                 :           1113 :         subpaths = NIL;
 4264                          1624                 :           1113 :         subpaths_valid = true;
 4461                          1625   [ +  -  +  +  :           4095 :         foreach(lcr, live_childrels)
                                              +  + ]
                               1626                 :                :         {
                               1627                 :           2988 :             RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
                               1628                 :                :             Path       *subpath;
                               1629                 :                : 
 2215 rhaas@postgresql.org     1630         [ -  + ]:           2988 :             if (childrel->pathlist == NIL)
                               1631                 :                :             {
                               1632                 :                :                 /* failed to make a suitable path for this child */
 2215 rhaas@postgresql.org     1633                 :UBC           0 :                 subpaths_valid = false;
                               1634                 :              0 :                 break;
                               1635                 :                :             }
                               1636                 :                : 
 3934 tgl@sss.pgh.pa.us        1637                 :CBC        2988 :             subpath = get_cheapest_parameterized_child_path(root,
                               1638                 :                :                                                             childrel,
                               1639                 :                :                                                             required_outer);
                               1640         [ +  + ]:           2988 :             if (subpath == NULL)
                               1641                 :                :             {
                               1642                 :                :                 /* failed to make a suitable path for this child */
                               1643                 :              6 :                 subpaths_valid = false;
                               1644                 :              6 :                 break;
                               1645                 :                :             }
 1168                          1646                 :           2982 :             accumulate_append_subpath(subpath, &subpaths, NULL);
                               1647                 :                :         }
                               1648                 :                : 
 4264                          1649         [ +  + ]:           1113 :         if (subpaths_valid)
 4378                          1650                 :           1107 :             add_path(rel, (Path *)
 2199 alvherre@alvh.no-ip.     1651                 :           1107 :                      create_append_path(root, rel, subpaths, NIL,
                               1652                 :                :                                         NIL, required_outer, 0, false,
                               1653                 :                :                                         -1));
                               1654                 :                :     }
                               1655                 :                : 
                               1656                 :                :     /*
                               1657                 :                :      * When there is only a single child relation, the Append path can inherit
                               1658                 :                :      * any ordering available for the child rel's path, so that it's useful to
                               1659                 :                :      * consider ordered partial paths.  Above we only considered the cheapest
                               1660                 :                :      * partial path for each child, but let's also make paths using any
                               1661                 :                :      * partial paths that have pathkeys.
                               1662                 :                :      */
 1847 tgl@sss.pgh.pa.us        1663         [ +  + ]:          17261 :     if (list_length(live_childrels) == 1)
                               1664                 :                :     {
                               1665                 :           6950 :         RelOptInfo *childrel = (RelOptInfo *) linitial(live_childrels);
                               1666                 :                : 
                               1667                 :                :         /* skip the cheapest partial path, since we already used that above */
 1259 drowley@postgresql.o     1668   [ +  +  +  +  :           7052 :         for_each_from(l, childrel->partial_pathlist, 1)
                                              +  + ]
                               1669                 :                :         {
 1847 tgl@sss.pgh.pa.us        1670                 :            102 :             Path       *path = (Path *) lfirst(l);
                               1671                 :                :             AppendPath *appendpath;
                               1672                 :                : 
                               1673                 :                :             /* skip paths with no pathkeys. */
 1259 drowley@postgresql.o     1674         [ -  + ]:            102 :             if (path->pathkeys == NIL)
 1847 tgl@sss.pgh.pa.us        1675                 :UBC           0 :                 continue;
                               1676                 :                : 
 1847 tgl@sss.pgh.pa.us        1677                 :CBC         102 :             appendpath = create_append_path(root, rel, NIL, list_make1(path),
                               1678                 :                :                                             NIL, NULL,
                               1679                 :                :                                             path->parallel_workers, true,
                               1680                 :                :                                             partial_rows);
                               1681                 :            102 :             add_partial_path(rel, (Path *) appendpath);
                               1682                 :                :         }
                               1683                 :                :     }
 4461                          1684                 :          17261 : }
                               1685                 :                : 
                               1686                 :                : /*
                               1687                 :                :  * generate_orderedappend_paths
                               1688                 :                :  *      Generate ordered append paths for an append relation
                               1689                 :                :  *
                               1690                 :                :  * Usually we generate MergeAppend paths here, but there are some special
                               1691                 :                :  * cases where we can generate simple Append paths, because the subpaths
                               1692                 :                :  * can provide tuples in the required order already.
                               1693                 :                :  *
                               1694                 :                :  * We generate a path for each ordering (pathkey list) appearing in
                               1695                 :                :  * all_child_pathkeys.
                               1696                 :                :  *
                               1697                 :                :  * We consider both cheapest-startup and cheapest-total cases, ie, for each
                               1698                 :                :  * interesting ordering, collect all the cheapest startup subpaths and all the
                               1699                 :                :  * cheapest total paths, and build a suitable path for each case.
                               1700                 :                :  *
                               1701                 :                :  * We don't currently generate any parameterized ordered paths here.  While
                               1702                 :                :  * it would not take much more code here to do so, it's very unclear that it
                               1703                 :                :  * is worth the planning cycles to investigate such paths: there's little
                               1704                 :                :  * use for an ordered path on the inside of a nestloop.  In fact, it's likely
                               1705                 :                :  * that the current coding of add_path would reject such paths out of hand,
                               1706                 :                :  * because add_path gives no credit for sort ordering of parameterized paths,
                               1707                 :                :  * and a parameterized MergeAppend is going to be more expensive than the
                               1708                 :                :  * corresponding parameterized Append path.  If we ever try harder to support
                               1709                 :                :  * parameterized mergejoin plans, it might be worth adding support for
                               1710                 :                :  * parameterized paths here to feed such joins.  (See notes in
                               1711                 :                :  * optimizer/README for why that might not ever happen, though.)
                               1712                 :                :  */
                               1713                 :                : static void
 1836                          1714                 :          17105 : generate_orderedappend_paths(PlannerInfo *root, RelOptInfo *rel,
                               1715                 :                :                              List *live_childrels,
                               1716                 :                :                              List *all_child_pathkeys)
                               1717                 :                : {
                               1718                 :                :     ListCell   *lcp;
                               1719                 :          17105 :     List       *partition_pathkeys = NIL;
                               1720                 :          17105 :     List       *partition_pathkeys_desc = NIL;
                               1721                 :          17105 :     bool        partition_pathkeys_partial = true;
                               1722                 :          17105 :     bool        partition_pathkeys_desc_partial = true;
                               1723                 :                : 
                               1724                 :                :     /*
                               1725                 :                :      * Some partitioned table setups may allow us to use an Append node
                               1726                 :                :      * instead of a MergeAppend.  This is possible in cases such as RANGE
                               1727                 :                :      * partitioned tables where it's guaranteed that an earlier partition must
                               1728                 :                :      * contain rows which come earlier in the sort order.  To detect whether
                               1729                 :                :      * this is relevant, build pathkey descriptions of the partition ordering,
                               1730                 :                :      * for both forward and reverse scans.
                               1731                 :                :      */
                               1732   [ +  +  +  +  :          30459 :     if (rel->part_scheme != NULL && IS_SIMPLE_REL(rel) &&
                                        +  +  +  + ]
  985 drowley@postgresql.o     1733                 :          13354 :         partitions_are_ordered(rel->boundinfo, rel->live_parts))
                               1734                 :                :     {
 1836 tgl@sss.pgh.pa.us        1735                 :          10979 :         partition_pathkeys = build_partition_pathkeys(root, rel,
                               1736                 :                :                                                       ForwardScanDirection,
                               1737                 :                :                                                       &partition_pathkeys_partial);
                               1738                 :                : 
                               1739                 :          10979 :         partition_pathkeys_desc = build_partition_pathkeys(root, rel,
                               1740                 :                :                                                            BackwardScanDirection,
                               1741                 :                :                                                            &partition_pathkeys_desc_partial);
                               1742                 :                : 
                               1743                 :                :         /*
                               1744                 :                :          * You might think we should truncate_useless_pathkeys here, but
                               1745                 :                :          * allowing partition keys which are a subset of the query's pathkeys
                               1746                 :                :          * can often be useful.  For example, consider a table partitioned by
                               1747                 :                :          * RANGE (a, b), and a query with ORDER BY a, b, c.  If we have child
                               1748                 :                :          * paths that can produce the a, b, c ordering (perhaps via indexes on
                               1749                 :                :          * (a, b, c)) then it works to consider the appendrel output as
                               1750                 :                :          * ordered by a, b, c.
                               1751                 :                :          */
                               1752                 :                :     }
                               1753                 :                : 
                               1754                 :                :     /* Now consider each interesting sort ordering */
 4461                          1755   [ +  +  +  +  :          19959 :     foreach(lcp, all_child_pathkeys)
                                              +  + ]
                               1756                 :                :     {
                               1757                 :           2854 :         List       *pathkeys = (List *) lfirst(lcp);
 4753 bruce@momjian.us         1758                 :           2854 :         List       *startup_subpaths = NIL;
                               1759                 :           2854 :         List       *total_subpaths = NIL;
  823 tomas.vondra@postgre     1760                 :           2854 :         List       *fractional_subpaths = NIL;
 4753 bruce@momjian.us         1761                 :           2854 :         bool        startup_neq_total = false;
                               1762                 :                :         bool        match_partition_order;
                               1763                 :                :         bool        match_partition_order_desc;
                               1764                 :                :         int         end_index;
                               1765                 :                :         int         first_index;
                               1766                 :                :         int         direction;
                               1767                 :                : 
                               1768                 :                :         /*
                               1769                 :                :          * Determine if this sort ordering matches any partition pathkeys we
                               1770                 :                :          * have, for both ascending and descending partition order.  If the
                               1771                 :                :          * partition pathkeys happen to be contained in pathkeys then it still
                               1772                 :                :          * works, as described above, providing that the partition pathkeys
                               1773                 :                :          * are complete and not just a prefix of the partition keys.  (In such
                               1774                 :                :          * cases we'll be relying on the child paths to have sorted the
                               1775                 :                :          * lower-order columns of the required pathkeys.)
                               1776                 :                :          */
 1836 tgl@sss.pgh.pa.us        1777                 :           2854 :         match_partition_order =
                               1778         [ +  + ]:           4649 :             pathkeys_contained_in(pathkeys, partition_pathkeys) ||
                               1779   [ +  +  +  + ]:           1863 :             (!partition_pathkeys_partial &&
                               1780                 :             68 :              pathkeys_contained_in(partition_pathkeys, pathkeys));
                               1781                 :                : 
                               1782   [ +  +  +  + ]:           6363 :         match_partition_order_desc = !match_partition_order &&
                               1783                 :           1762 :             (pathkeys_contained_in(pathkeys, partition_pathkeys_desc) ||
                               1784   [ +  +  +  + ]:           1767 :              (!partition_pathkeys_desc_partial &&
                               1785                 :             20 :               pathkeys_contained_in(partition_pathkeys_desc, pathkeys)));
                               1786                 :                : 
                               1787                 :                :         /*
                               1788                 :                :          * When the required pathkeys match the reverse of the partition
                               1789                 :                :          * order, we must build the list of paths in reverse starting with the
                               1790                 :                :          * last matching partition first.  We can get away without making any
                               1791                 :                :          * special cases for this in the loop below by just looping backward
                               1792                 :                :          * over the child relations in this case.
                               1793                 :                :          */
  419 drowley@postgresql.o     1794         [ +  + ]:           2854 :         if (match_partition_order_desc)
                               1795                 :                :         {
                               1796                 :                :             /* loop backward */
                               1797                 :             21 :             first_index = list_length(live_childrels) - 1;
                               1798                 :             21 :             end_index = -1;
                               1799                 :             21 :             direction = -1;
                               1800                 :                : 
                               1801                 :                :             /*
                               1802                 :                :              * Set this to true to save us having to check for
                               1803                 :                :              * match_partition_order_desc in the loop below.
                               1804                 :                :              */
                               1805                 :             21 :             match_partition_order = true;
                               1806                 :                :         }
                               1807                 :                :         else
                               1808                 :                :         {
                               1809                 :                :             /* for all other case, loop forward */
                               1810                 :           2833 :             first_index = 0;
                               1811                 :           2833 :             end_index = list_length(live_childrels);
                               1812                 :           2833 :             direction = 1;
                               1813                 :                :         }
                               1814                 :                : 
                               1815                 :                :         /* Select the child paths for this ordering... */
                               1816         [ +  + ]:          10524 :         for (int i = first_index; i != end_index; i += direction)
                               1817                 :                :         {
                               1818                 :           7670 :             RelOptInfo *childrel = list_nth_node(RelOptInfo, live_childrels, i);
                               1819                 :                :             Path       *cheapest_startup,
                               1820                 :                :                        *cheapest_total,
  823 tomas.vondra@postgre     1821                 :           7670 :                        *cheapest_fractional = NULL;
                               1822                 :                : 
                               1823                 :                :             /* Locate the right paths, if they are available. */
                               1824                 :                :             cheapest_startup =
 4931 tgl@sss.pgh.pa.us        1825                 :           7670 :                 get_cheapest_path_for_pathkeys(childrel->pathlist,
                               1826                 :                :                                                pathkeys,
                               1827                 :                :                                                NULL,
                               1828                 :                :                                                STARTUP_COST,
                               1829                 :                :                                                false);
                               1830                 :                :             cheapest_total =
                               1831                 :           7670 :                 get_cheapest_path_for_pathkeys(childrel->pathlist,
                               1832                 :                :                                                pathkeys,
                               1833                 :                :                                                NULL,
                               1834                 :                :                                                TOTAL_COST,
                               1835                 :                :                                                false);
                               1836                 :                : 
                               1837                 :                :             /*
                               1838                 :                :              * If we can't find any paths with the right order just use the
                               1839                 :                :              * cheapest-total path; we'll have to sort it later.
                               1840                 :                :              */
 4461                          1841   [ +  +  -  + ]:           7670 :             if (cheapest_startup == NULL || cheapest_total == NULL)
                               1842                 :                :             {
 4378                          1843                 :            134 :                 cheapest_startup = cheapest_total =
                               1844                 :                :                     childrel->cheapest_total_path;
                               1845                 :                :                 /* Assert we do have an unparameterized path for this child */
 4264                          1846         [ -  + ]:            134 :                 Assert(cheapest_total->param_info == NULL);
                               1847                 :                :             }
                               1848                 :                : 
                               1849                 :                :             /*
                               1850                 :                :              * When building a fractional path, determine a cheapest
                               1851                 :                :              * fractional path for each child relation too. Looking at startup
                               1852                 :                :              * and total costs is not enough, because the cheapest fractional
                               1853                 :                :              * path may be dominated by two separate paths (one for startup,
                               1854                 :                :              * one for total).
                               1855                 :                :              *
                               1856                 :                :              * When needed (building fractional path), determine the cheapest
                               1857                 :                :              * fractional path too.
                               1858                 :                :              */
  823 tomas.vondra@postgre     1859         [ +  + ]:           7670 :             if (root->tuple_fraction > 0)
                               1860                 :                :             {
  703 tgl@sss.pgh.pa.us        1861                 :            334 :                 double      path_fraction = (1.0 / root->tuple_fraction);
                               1862                 :                : 
                               1863                 :                :                 cheapest_fractional =
  823 tomas.vondra@postgre     1864                 :            334 :                     get_cheapest_fractional_path_for_pathkeys(childrel->pathlist,
                               1865                 :                :                                                               pathkeys,
                               1866                 :                :                                                               NULL,
                               1867                 :                :                                                               path_fraction);
                               1868                 :                : 
                               1869                 :                :                 /*
                               1870                 :                :                  * If we found no path with matching pathkeys, use the
                               1871                 :                :                  * cheapest total path instead.
                               1872                 :                :                  *
                               1873                 :                :                  * XXX We might consider partially sorted paths too (with an
                               1874                 :                :                  * incremental sort on top). But we'd have to build all the
                               1875                 :                :                  * incremental paths, do the costing etc.
                               1876                 :                :                  */
                               1877         [ +  + ]:            334 :                 if (!cheapest_fractional)
                               1878                 :             22 :                     cheapest_fractional = cheapest_total;
                               1879                 :                :             }
                               1880                 :                : 
                               1881                 :                :             /*
                               1882                 :                :              * Notice whether we actually have different paths for the
                               1883                 :                :              * "cheapest" and "total" cases; frequently there will be no point
                               1884                 :                :              * in two create_merge_append_path() calls.
                               1885                 :                :              */
 4931 tgl@sss.pgh.pa.us        1886         [ +  + ]:           7670 :             if (cheapest_startup != cheapest_total)
                               1887                 :             24 :                 startup_neq_total = true;
                               1888                 :                : 
                               1889                 :                :             /*
                               1890                 :                :              * Collect the appropriate child paths.  The required logic varies
                               1891                 :                :              * for the Append and MergeAppend cases.
                               1892                 :                :              */
 1836                          1893         [ +  + ]:           7670 :             if (match_partition_order)
                               1894                 :                :             {
                               1895                 :                :                 /*
                               1896                 :                :                  * We're going to make a plain Append path.  We don't need
                               1897                 :                :                  * most of what accumulate_append_subpath would do, but we do
                               1898                 :                :                  * want to cut out child Appends or MergeAppends if they have
                               1899                 :                :                  * just a single subpath (and hence aren't doing anything
                               1900                 :                :                  * useful).
                               1901                 :                :                  */
                               1902                 :           2989 :                 cheapest_startup = get_singleton_append_subpath(cheapest_startup);
                               1903                 :           2989 :                 cheapest_total = get_singleton_append_subpath(cheapest_total);
                               1904                 :                : 
                               1905                 :           2989 :                 startup_subpaths = lappend(startup_subpaths, cheapest_startup);
                               1906                 :           2989 :                 total_subpaths = lappend(total_subpaths, cheapest_total);
                               1907                 :                : 
  823 tomas.vondra@postgre     1908         [ +  + ]:           2989 :                 if (cheapest_fractional)
                               1909                 :                :                 {
                               1910                 :             60 :                     cheapest_fractional = get_singleton_append_subpath(cheapest_fractional);
                               1911                 :             60 :                     fractional_subpaths = lappend(fractional_subpaths, cheapest_fractional);
                               1912                 :                :                 }
                               1913                 :                :             }
                               1914                 :                :             else
                               1915                 :                :             {
                               1916                 :                :                 /*
                               1917                 :                :                  * Otherwise, rely on accumulate_append_subpath to collect the
                               1918                 :                :                  * child paths for the MergeAppend.
                               1919                 :                :                  */
 1836 tgl@sss.pgh.pa.us        1920                 :           4681 :                 accumulate_append_subpath(cheapest_startup,
                               1921                 :                :                                           &startup_subpaths, NULL);
                               1922                 :           4681 :                 accumulate_append_subpath(cheapest_total,
                               1923                 :                :                                           &total_subpaths, NULL);
                               1924                 :                : 
  823 tomas.vondra@postgre     1925         [ +  + ]:           4681 :                 if (cheapest_fractional)
                               1926                 :            274 :                     accumulate_append_subpath(cheapest_fractional,
                               1927                 :                :                                               &fractional_subpaths, NULL);
                               1928                 :                :             }
                               1929                 :                :         }
                               1930                 :                : 
                               1931                 :                :         /* ... and build the Append or MergeAppend paths */
  419 drowley@postgresql.o     1932         [ +  + ]:           2854 :         if (match_partition_order)
                               1933                 :                :         {
                               1934                 :                :             /* We only need Append */
 1836 tgl@sss.pgh.pa.us        1935                 :           1113 :             add_path(rel, (Path *) create_append_path(root,
                               1936                 :                :                                                       rel,
                               1937                 :                :                                                       startup_subpaths,
                               1938                 :                :                                                       NIL,
                               1939                 :                :                                                       pathkeys,
                               1940                 :                :                                                       NULL,
                               1941                 :                :                                                       0,
                               1942                 :                :                                                       false,
                               1943                 :                :                                                       -1));
                               1944         [ -  + ]:           1113 :             if (startup_neq_total)
 1836 tgl@sss.pgh.pa.us        1945                 :UBC           0 :                 add_path(rel, (Path *) create_append_path(root,
                               1946                 :                :                                                           rel,
                               1947                 :                :                                                           total_subpaths,
                               1948                 :                :                                                           NIL,
                               1949                 :                :                                                           pathkeys,
                               1950                 :                :                                                           NULL,
                               1951                 :                :                                                           0,
                               1952                 :                :                                                           false,
                               1953                 :                :                                                           -1));
                               1954                 :                : 
  823 tomas.vondra@postgre     1955         [ +  + ]:CBC        1113 :             if (fractional_subpaths)
                               1956                 :             30 :                 add_path(rel, (Path *) create_append_path(root,
                               1957                 :                :                                                           rel,
                               1958                 :                :                                                           fractional_subpaths,
                               1959                 :                :                                                           NIL,
                               1960                 :                :                                                           pathkeys,
                               1961                 :                :                                                           NULL,
                               1962                 :                :                                                           0,
                               1963                 :                :                                                           false,
                               1964                 :                :                                                           -1));
                               1965                 :                :         }
                               1966                 :                :         else
                               1967                 :                :         {
                               1968                 :                :             /* We need MergeAppend */
 4931 tgl@sss.pgh.pa.us        1969                 :           1741 :             add_path(rel, (Path *) create_merge_append_path(root,
                               1970                 :                :                                                             rel,
                               1971                 :                :                                                             startup_subpaths,
                               1972                 :                :                                                             pathkeys,
                               1973                 :                :                                                             NULL));
 1836                          1974         [ +  + ]:           1741 :             if (startup_neq_total)
                               1975                 :             12 :                 add_path(rel, (Path *) create_merge_append_path(root,
                               1976                 :                :                                                                 rel,
                               1977                 :                :                                                                 total_subpaths,
                               1978                 :                :                                                                 pathkeys,
                               1979                 :                :                                                                 NULL));
                               1980                 :                : 
  823 tomas.vondra@postgre     1981         [ +  + ]:           1741 :             if (fractional_subpaths)
                               1982                 :             98 :                 add_path(rel, (Path *) create_merge_append_path(root,
                               1983                 :                :                                                                 rel,
                               1984                 :                :                                                                 fractional_subpaths,
                               1985                 :                :                                                                 pathkeys,
                               1986                 :                :                                                                 NULL));
                               1987                 :                :         }
                               1988                 :                :     }
10141 scrappy@hub.org          1989                 :          17105 : }
                               1990                 :                : 
                               1991                 :                : /*
                               1992                 :                :  * get_cheapest_parameterized_child_path
                               1993                 :                :  *      Get cheapest path for this relation that has exactly the requested
                               1994                 :                :  *      parameterization.
                               1995                 :                :  *
                               1996                 :                :  * Returns NULL if unable to create such a path.
                               1997                 :                :  */
                               1998                 :                : static Path *
 3934 tgl@sss.pgh.pa.us        1999                 :           2988 : get_cheapest_parameterized_child_path(PlannerInfo *root, RelOptInfo *rel,
                               2000                 :                :                                       Relids required_outer)
                               2001                 :                : {
                               2002                 :                :     Path       *cheapest;
                               2003                 :                :     ListCell   *lc;
                               2004                 :                : 
                               2005                 :                :     /*
                               2006                 :                :      * Look up the cheapest existing path with no more than the needed
                               2007                 :                :      * parameterization.  If it has exactly the needed parameterization, we're
                               2008                 :                :      * done.
                               2009                 :                :      */
                               2010                 :           2988 :     cheapest = get_cheapest_path_for_pathkeys(rel->pathlist,
                               2011                 :                :                                               NIL,
                               2012                 :                :                                               required_outer,
                               2013                 :                :                                               TOTAL_COST,
                               2014                 :                :                                               false);
                               2015         [ -  + ]:           2988 :     Assert(cheapest != NULL);
                               2016   [ +  +  +  + ]:           2988 :     if (bms_equal(PATH_REQ_OUTER(cheapest), required_outer))
                               2017                 :           2842 :         return cheapest;
                               2018                 :                : 
                               2019                 :                :     /*
                               2020                 :                :      * Otherwise, we can "reparameterize" an existing path to match the given
                               2021                 :                :      * parameterization, which effectively means pushing down additional
                               2022                 :                :      * joinquals to be checked within the path's scan.  However, some existing
                               2023                 :                :      * paths might check the available joinquals already while others don't;
                               2024                 :                :      * therefore, it's not clear which existing path will be cheapest after
                               2025                 :                :      * reparameterization.  We have to go through them all and find out.
                               2026                 :                :      */
                               2027                 :            146 :     cheapest = NULL;
                               2028   [ +  -  +  +  :            506 :     foreach(lc, rel->pathlist)
                                              +  + ]
                               2029                 :                :     {
                               2030                 :            360 :         Path       *path = (Path *) lfirst(lc);
                               2031                 :                : 
                               2032                 :                :         /* Can't use it if it needs more than requested parameterization */
                               2033   [ +  +  +  + ]:            360 :         if (!bms_is_subset(PATH_REQ_OUTER(path), required_outer))
                               2034                 :             12 :             continue;
                               2035                 :                : 
                               2036                 :                :         /*
                               2037                 :                :          * Reparameterization can only increase the path's cost, so if it's
                               2038                 :                :          * already more expensive than the current cheapest, forget it.
                               2039                 :                :          */
                               2040   [ +  +  +  + ]:            540 :         if (cheapest != NULL &&
                               2041                 :            192 :             compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
                               2042                 :            156 :             continue;
                               2043                 :                : 
                               2044                 :                :         /* Reparameterize if needed, then recheck cost */
                               2045   [ +  +  +  + ]:            192 :         if (!bms_equal(PATH_REQ_OUTER(path), required_outer))
                               2046                 :                :         {
                               2047                 :            154 :             path = reparameterize_path(root, path, required_outer, 1.0);
                               2048         [ +  + ]:            154 :             if (path == NULL)
                               2049                 :             16 :                 continue;       /* failed to reparameterize this one */
                               2050   [ +  -  -  + ]:            138 :             Assert(bms_equal(PATH_REQ_OUTER(path), required_outer));
                               2051                 :                : 
                               2052   [ -  +  -  - ]:            138 :             if (cheapest != NULL &&
 3934 tgl@sss.pgh.pa.us        2053                 :UBC           0 :                 compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
                               2054                 :              0 :                 continue;
                               2055                 :                :         }
                               2056                 :                : 
                               2057                 :                :         /* We have a new best path */
 3934 tgl@sss.pgh.pa.us        2058                 :CBC         176 :         cheapest = path;
                               2059                 :                :     }
                               2060                 :                : 
                               2061                 :                :     /* Return the best path, or NULL if we found no suitable candidate */
                               2062                 :            146 :     return cheapest;
                               2063                 :                : }
                               2064                 :                : 
                               2065                 :                : /*
                               2066                 :                :  * accumulate_append_subpath
                               2067                 :                :  *      Add a subpath to the list being built for an Append or MergeAppend.
                               2068                 :                :  *
                               2069                 :                :  * It's possible that the child is itself an Append or MergeAppend path, in
                               2070                 :                :  * which case we can "cut out the middleman" and just add its child paths to
                               2071                 :                :  * our own list.  (We don't try to do this earlier because we need to apply
                               2072                 :                :  * both levels of transformation to the quals.)
                               2073                 :                :  *
                               2074                 :                :  * Note that if we omit a child MergeAppend in this way, we are effectively
                               2075                 :                :  * omitting a sort step, which seems fine: if the parent is to be an Append,
                               2076                 :                :  * its result would be unsorted anyway, while if the parent is to be a
                               2077                 :                :  * MergeAppend, there's no point in a separate sort on a child.
                               2078                 :                :  *
                               2079                 :                :  * Normally, either path is a partial path and subpaths is a list of partial
                               2080                 :                :  * paths, or else path is a non-partial plan and subpaths is a list of those.
                               2081                 :                :  * However, if path is a parallel-aware Append, then we add its partial path
                               2082                 :                :  * children to subpaths and the rest to special_subpaths.  If the latter is
                               2083                 :                :  * NULL, we don't flatten the path at all (unless it contains only partial
                               2084                 :                :  * paths).
                               2085                 :                :  */
                               2086                 :                : static void
 1168                          2087                 :          96936 : accumulate_append_subpath(Path *path, List **subpaths, List **special_subpaths)
                               2088                 :                : {
 4931                          2089         [ +  + ]:          96936 :     if (IsA(path, AppendPath))
                               2090                 :                :     {
 4753 bruce@momjian.us         2091                 :           6940 :         AppendPath *apath = (AppendPath *) path;
                               2092                 :                : 
 2322 rhaas@postgresql.org     2093   [ +  +  +  + ]:           6940 :         if (!apath->path.parallel_aware || apath->first_partial_path == 0)
                               2094                 :                :         {
 1707 tgl@sss.pgh.pa.us        2095                 :           6844 :             *subpaths = list_concat(*subpaths, apath->subpaths);
 2322 rhaas@postgresql.org     2096                 :           6844 :             return;
                               2097                 :                :         }
                               2098         [ +  + ]:             96 :         else if (special_subpaths != NULL)
                               2099                 :                :         {
                               2100                 :                :             List       *new_special_subpaths;
                               2101                 :                : 
                               2102                 :                :             /* Split Parallel Append into partial and non-partial subpaths */
                               2103                 :             48 :             *subpaths = list_concat(*subpaths,
                               2104                 :             48 :                                     list_copy_tail(apath->subpaths,
                               2105                 :                :                                                    apath->first_partial_path));
  641 drowley@postgresql.o     2106                 :             48 :             new_special_subpaths = list_copy_head(apath->subpaths,
                               2107                 :                :                                                   apath->first_partial_path);
 2322 rhaas@postgresql.org     2108                 :             48 :             *special_subpaths = list_concat(*special_subpaths,
                               2109                 :                :                                             new_special_subpaths);
 2286                          2110                 :             48 :             return;
                               2111                 :                :         }
                               2112                 :                :     }
 3670 tgl@sss.pgh.pa.us        2113         [ +  + ]:          89996 :     else if (IsA(path, MergeAppendPath))
                               2114                 :                :     {
                               2115                 :            310 :         MergeAppendPath *mpath = (MergeAppendPath *) path;
                               2116                 :                : 
 1707                          2117                 :            310 :         *subpaths = list_concat(*subpaths, mpath->subpaths);
 2322 rhaas@postgresql.org     2118                 :            310 :         return;
                               2119                 :                :     }
                               2120                 :                : 
                               2121                 :          89734 :     *subpaths = lappend(*subpaths, path);
                               2122                 :                : }
                               2123                 :                : 
                               2124                 :                : /*
                               2125                 :                :  * get_singleton_append_subpath
                               2126                 :                :  *      Returns the single subpath of an Append/MergeAppend, or just
                               2127                 :                :  *      return 'path' if it's not a single sub-path Append/MergeAppend.
                               2128                 :                :  *
                               2129                 :                :  * Note: 'path' must not be a parallel-aware path.
                               2130                 :                :  */
                               2131                 :                : static Path *
 1836 tgl@sss.pgh.pa.us        2132                 :           6038 : get_singleton_append_subpath(Path *path)
                               2133                 :                : {
                               2134         [ -  + ]:           6038 :     Assert(!path->parallel_aware);
                               2135                 :                : 
                               2136         [ +  + ]:           6038 :     if (IsA(path, AppendPath))
                               2137                 :                :     {
                               2138                 :            170 :         AppendPath *apath = (AppendPath *) path;
                               2139                 :                : 
                               2140         [ +  + ]:            170 :         if (list_length(apath->subpaths) == 1)
                               2141                 :             78 :             return (Path *) linitial(apath->subpaths);
                               2142                 :                :     }
                               2143         [ +  + ]:           5868 :     else if (IsA(path, MergeAppendPath))
                               2144                 :                :     {
                               2145                 :            126 :         MergeAppendPath *mpath = (MergeAppendPath *) path;
                               2146                 :                : 
                               2147         [ -  + ]:            126 :         if (list_length(mpath->subpaths) == 1)
 1836 tgl@sss.pgh.pa.us        2148                 :UBC           0 :             return (Path *) linitial(mpath->subpaths);
                               2149                 :                :     }
                               2150                 :                : 
 1836 tgl@sss.pgh.pa.us        2151                 :CBC        5960 :     return path;
                               2152                 :                : }
                               2153                 :                : 
                               2154                 :                : /*
                               2155                 :                :  * set_dummy_rel_pathlist
                               2156                 :                :  *    Build a dummy path for a relation that's been excluded by constraints
                               2157                 :                :  *
                               2158                 :                :  * Rather than inventing a special "dummy" path type, we represent this as an
                               2159                 :                :  * AppendPath with no members (see also IS_DUMMY_APPEND/IS_DUMMY_REL macros).
                               2160                 :                :  *
                               2161                 :                :  * (See also mark_dummy_rel, which does basically the same thing, but is
                               2162                 :                :  * typically used to change a rel into dummy state after we already made
                               2163                 :                :  * paths for it.)
                               2164                 :                :  */
                               2165                 :                : static void
 6168                          2166                 :            503 : set_dummy_rel_pathlist(RelOptInfo *rel)
                               2167                 :                : {
                               2168                 :                :     /* Set dummy size estimates --- we leave attr_widths[] as zeroes */
                               2169                 :            503 :     rel->rows = 0;
 2953                          2170                 :            503 :     rel->reltarget->width = 0;
                               2171                 :                : 
                               2172                 :                :     /* Discard any pre-existing paths; no further need for them */
 4461                          2173                 :            503 :     rel->pathlist = NIL;
 3007 rhaas@postgresql.org     2174                 :            503 :     rel->partial_pathlist = NIL;
                               2175                 :                : 
                               2176                 :                :     /* Set up the dummy path */
 1858 tgl@sss.pgh.pa.us        2177                 :            503 :     add_path(rel, (Path *) create_append_path(NULL, rel, NIL, NIL,
                               2178                 :                :                                               NIL, rel->lateral_relids,
                               2179                 :                :                                               0, false, -1));
                               2180                 :                : 
                               2181                 :                :     /*
                               2182                 :                :      * We set the cheapest-path fields immediately, just in case they were
                               2183                 :                :      * pointing at some discarded path.  This is redundant in current usage
                               2184                 :                :      * because set_rel_pathlist will do it later, but it's cheap so we keep it
                               2185                 :                :      * for safety and consistency with mark_dummy_rel.
                               2186                 :                :      */
 6168                          2187                 :            503 :     set_cheapest(rel);
                               2188                 :            503 : }
                               2189                 :                : 
                               2190                 :                : /*
                               2191                 :                :  * find_window_run_conditions
                               2192                 :                :  *      Determine if 'wfunc' is really a WindowFunc and call its prosupport
                               2193                 :                :  *      function to determine the function's monotonic properties.  We then
                               2194                 :                :  *      see if 'opexpr' can be used to short-circuit execution.
                               2195                 :                :  *
                               2196                 :                :  * For example row_number() over (order by ...) always produces a value one
                               2197                 :                :  * higher than the previous.  If someone has a window function in a subquery
                               2198                 :                :  * and has a WHERE clause in the outer query to filter rows <= 10, then we may
                               2199                 :                :  * as well stop processing the windowagg once the row number reaches 11.  Here
                               2200                 :                :  * we check if 'opexpr' might help us to stop doing needless extra processing
                               2201                 :                :  * in WindowAgg nodes.
                               2202                 :                :  *
                               2203                 :                :  * '*keep_original' is set to true if the caller should also use 'opexpr' for
                               2204                 :                :  * its original purpose.  This is set to false if the caller can assume that
                               2205                 :                :  * the run condition will handle all of the required filtering.
                               2206                 :                :  *
                               2207                 :                :  * Returns true if 'opexpr' was found to be useful and was added to the
                               2208                 :                :  * WindowClauses runCondition.  We also set *keep_original accordingly and add
                               2209                 :                :  * 'attno' to *run_cond_attrs offset by FirstLowInvalidHeapAttributeNumber.
                               2210                 :                :  * If the 'opexpr' cannot be used then we set *keep_original to true and
                               2211                 :                :  * return false.
                               2212                 :                :  */
                               2213                 :                : static bool
  737 drowley@postgresql.o     2214                 :            114 : find_window_run_conditions(Query *subquery, RangeTblEntry *rte, Index rti,
                               2215                 :                :                            AttrNumber attno, WindowFunc *wfunc, OpExpr *opexpr,
                               2216                 :                :                            bool wfunc_left, bool *keep_original,
                               2217                 :                :                            Bitmapset **run_cond_attrs)
                               2218                 :                : {
                               2219                 :                :     Oid         prosupport;
                               2220                 :                :     Expr       *otherexpr;
                               2221                 :                :     SupportRequestWFuncMonotonic req;
                               2222                 :                :     SupportRequestWFuncMonotonic *res;
                               2223                 :                :     WindowClause *wclause;
                               2224                 :                :     List       *opinfos;
                               2225                 :                :     OpExpr     *runopexpr;
                               2226                 :                :     Oid         runoperator;
                               2227                 :                :     ListCell   *lc;
                               2228                 :                : 
                               2229                 :            114 :     *keep_original = true;
                               2230                 :                : 
                               2231         [ -  + ]:            114 :     while (IsA(wfunc, RelabelType))
  737 drowley@postgresql.o     2232                 :UBC           0 :         wfunc = (WindowFunc *) ((RelabelType *) wfunc)->arg;
                               2233                 :                : 
                               2234                 :                :     /* we can only work with window functions */
  737 drowley@postgresql.o     2235         [ +  + ]:CBC         114 :     if (!IsA(wfunc, WindowFunc))
                               2236                 :             12 :         return false;
                               2237                 :                : 
                               2238                 :                :     /* can't use it if there are subplans in the WindowFunc */
  394                          2239         [ +  + ]:            102 :     if (contain_subplans((Node *) wfunc))
                               2240                 :              3 :         return false;
                               2241                 :                : 
  737                          2242                 :             99 :     prosupport = get_func_support(wfunc->winfnoid);
                               2243                 :                : 
                               2244                 :                :     /* Check if there's a support function for 'wfunc' */
                               2245         [ +  + ]:             99 :     if (!OidIsValid(prosupport))
                               2246                 :              9 :         return false;
                               2247                 :                : 
                               2248                 :                :     /* get the Expr from the other side of the OpExpr */
                               2249         [ +  + ]:             90 :     if (wfunc_left)
                               2250                 :             78 :         otherexpr = lsecond(opexpr->args);
                               2251                 :                :     else
                               2252                 :             12 :         otherexpr = linitial(opexpr->args);
                               2253                 :                : 
                               2254                 :                :     /*
                               2255                 :                :      * The value being compared must not change during the evaluation of the
                               2256                 :                :      * window partition.
                               2257                 :                :      */
                               2258         [ -  + ]:             90 :     if (!is_pseudo_constant_clause((Node *) otherexpr))
  737 drowley@postgresql.o     2259                 :UBC           0 :         return false;
                               2260                 :                : 
                               2261                 :                :     /* find the window clause belonging to the window function */
  737 drowley@postgresql.o     2262                 :CBC          90 :     wclause = (WindowClause *) list_nth(subquery->windowClause,
                               2263                 :             90 :                                         wfunc->winref - 1);
                               2264                 :                : 
                               2265                 :             90 :     req.type = T_SupportRequestWFuncMonotonic;
                               2266                 :             90 :     req.window_func = wfunc;
                               2267                 :             90 :     req.window_clause = wclause;
                               2268                 :                : 
                               2269                 :                :     /* call the support function */
                               2270                 :                :     res = (SupportRequestWFuncMonotonic *)
                               2271                 :             90 :         DatumGetPointer(OidFunctionCall1(prosupport,
                               2272                 :                :                                          PointerGetDatum(&req)));
                               2273                 :                : 
                               2274                 :                :     /*
                               2275                 :                :      * Nothing to do if the function is neither monotonically increasing nor
                               2276                 :                :      * monotonically decreasing.
                               2277                 :                :      */
                               2278   [ +  -  -  + ]:             90 :     if (res == NULL || res->monotonic == MONOTONICFUNC_NONE)
  737 drowley@postgresql.o     2279                 :UBC           0 :         return false;
                               2280                 :                : 
  737 drowley@postgresql.o     2281                 :CBC          90 :     runopexpr = NULL;
                               2282                 :             90 :     runoperator = InvalidOid;
                               2283                 :             90 :     opinfos = get_op_btree_interpretation(opexpr->opno);
                               2284                 :                : 
                               2285   [ +  -  +  -  :             90 :     foreach(lc, opinfos)
                                              +  - ]
                               2286                 :                :     {
                               2287                 :             90 :         OpBtreeInterpretation *opinfo = (OpBtreeInterpretation *) lfirst(lc);
                               2288                 :             90 :         int         strategy = opinfo->strategy;
                               2289                 :                : 
                               2290                 :                :         /* handle < / <= */
                               2291   [ +  +  +  + ]:             90 :         if (strategy == BTLessStrategyNumber ||
                               2292                 :                :             strategy == BTLessEqualStrategyNumber)
                               2293                 :                :         {
                               2294                 :                :             /*
                               2295                 :                :              * < / <= is supported for monotonically increasing functions in
                               2296                 :                :              * the form <wfunc> op <pseudoconst> and <pseudoconst> op <wfunc>
                               2297                 :                :              * for monotonically decreasing functions.
                               2298                 :                :              */
                               2299   [ +  +  +  + ]:             66 :             if ((wfunc_left && (res->monotonic & MONOTONICFUNC_INCREASING)) ||
                               2300   [ +  +  +  + ]:              9 :                 (!wfunc_left && (res->monotonic & MONOTONICFUNC_DECREASING)))
                               2301                 :                :             {
                               2302                 :             60 :                 *keep_original = false;
                               2303                 :             60 :                 runopexpr = opexpr;
                               2304                 :             60 :                 runoperator = opexpr->opno;
                               2305                 :                :             }
                               2306                 :             66 :             break;
                               2307                 :                :         }
                               2308                 :                :         /* handle > / >= */
                               2309   [ +  +  +  + ]:             24 :         else if (strategy == BTGreaterStrategyNumber ||
                               2310                 :                :                  strategy == BTGreaterEqualStrategyNumber)
                               2311                 :                :         {
                               2312                 :                :             /*
                               2313                 :                :              * > / >= is supported for monotonically decreasing functions in
                               2314                 :                :              * the form <wfunc> op <pseudoconst> and <pseudoconst> op <wfunc>
                               2315                 :                :              * for monotonically increasing functions.
                               2316                 :                :              */
                               2317   [ +  +  -  + ]:              9 :             if ((wfunc_left && (res->monotonic & MONOTONICFUNC_DECREASING)) ||
                               2318   [ +  -  +  - ]:              6 :                 (!wfunc_left && (res->monotonic & MONOTONICFUNC_INCREASING)))
                               2319                 :                :             {
                               2320                 :              9 :                 *keep_original = false;
                               2321                 :              9 :                 runopexpr = opexpr;
                               2322                 :              9 :                 runoperator = opexpr->opno;
                               2323                 :                :             }
                               2324                 :              9 :             break;
                               2325                 :                :         }
                               2326                 :                :         /* handle = */
                               2327         [ +  - ]:             15 :         else if (strategy == BTEqualStrategyNumber)
                               2328                 :                :         {
                               2329                 :                :             int16       newstrategy;
                               2330                 :                : 
                               2331                 :                :             /*
                               2332                 :                :              * When both monotonically increasing and decreasing then the
                               2333                 :                :              * return value of the window function will be the same each time.
                               2334                 :                :              * We can simply use 'opexpr' as the run condition without
                               2335                 :                :              * modifying it.
                               2336                 :                :              */
                               2337         [ +  + ]:             15 :             if ((res->monotonic & MONOTONICFUNC_BOTH) == MONOTONICFUNC_BOTH)
                               2338                 :                :             {
                               2339                 :              3 :                 *keep_original = false;
                               2340                 :              3 :                 runopexpr = opexpr;
  618                          2341                 :              3 :                 runoperator = opexpr->opno;
  737                          2342                 :              3 :                 break;
                               2343                 :                :             }
                               2344                 :                : 
                               2345                 :                :             /*
                               2346                 :                :              * When monotonically increasing we make a qual with <wfunc> <=
                               2347                 :                :              * <value> or <value> >= <wfunc> in order to filter out values
                               2348                 :                :              * which are above the value in the equality condition.  For
                               2349                 :                :              * monotonically decreasing functions we want to filter values
                               2350                 :                :              * below the value in the equality condition.
                               2351                 :                :              */
                               2352         [ +  - ]:             12 :             if (res->monotonic & MONOTONICFUNC_INCREASING)
                               2353         [ +  - ]:             12 :                 newstrategy = wfunc_left ? BTLessEqualStrategyNumber : BTGreaterEqualStrategyNumber;
                               2354                 :                :             else
  737 drowley@postgresql.o     2355         [ #  # ]:UBC           0 :                 newstrategy = wfunc_left ? BTGreaterEqualStrategyNumber : BTLessEqualStrategyNumber;
                               2356                 :                : 
                               2357                 :                :             /* We must keep the original equality qual */
  737 drowley@postgresql.o     2358                 :CBC          12 :             *keep_original = true;
                               2359                 :             12 :             runopexpr = opexpr;
                               2360                 :                : 
                               2361                 :                :             /* determine the operator to use for the runCondition qual */
                               2362                 :             12 :             runoperator = get_opfamily_member(opinfo->opfamily_id,
                               2363                 :                :                                               opinfo->oplefttype,
                               2364                 :                :                                               opinfo->oprighttype,
                               2365                 :                :                                               newstrategy);
                               2366                 :             12 :             break;
                               2367                 :                :         }
                               2368                 :                :     }
                               2369                 :                : 
                               2370         [ +  + ]:             90 :     if (runopexpr != NULL)
                               2371                 :                :     {
                               2372                 :                :         Expr       *newexpr;
                               2373                 :                : 
                               2374                 :                :         /*
                               2375                 :                :          * Build the qual required for the run condition keeping the
                               2376                 :                :          * WindowFunc on the same side as it was originally.
                               2377                 :                :          */
                               2378         [ +  + ]:             84 :         if (wfunc_left)
                               2379                 :             75 :             newexpr = make_opclause(runoperator,
                               2380                 :                :                                     runopexpr->opresulttype,
                               2381                 :             75 :                                     runopexpr->opretset, (Expr *) wfunc,
                               2382                 :                :                                     otherexpr, runopexpr->opcollid,
                               2383                 :                :                                     runopexpr->inputcollid);
                               2384                 :                :         else
                               2385                 :              9 :             newexpr = make_opclause(runoperator,
                               2386                 :                :                                     runopexpr->opresulttype,
                               2387                 :              9 :                                     runopexpr->opretset,
                               2388                 :                :                                     otherexpr, (Expr *) wfunc,
                               2389                 :                :                                     runopexpr->opcollid,
                               2390                 :                :                                     runopexpr->inputcollid);
                               2391                 :                : 
                               2392                 :             84 :         wclause->runCondition = lappend(wclause->runCondition, newexpr);
                               2393                 :                : 
                               2394                 :                :         /* record that this attno was used in a run condition */
  688                          2395                 :             84 :         *run_cond_attrs = bms_add_member(*run_cond_attrs,
                               2396                 :                :                                          attno - FirstLowInvalidHeapAttributeNumber);
  737                          2397                 :             84 :         return true;
                               2398                 :                :     }
                               2399                 :                : 
                               2400                 :                :     /* unsupported OpExpr */
                               2401                 :              6 :     return false;
                               2402                 :                : }
                               2403                 :                : 
                               2404                 :                : /*
                               2405                 :                :  * check_and_push_window_quals
                               2406                 :                :  *      Check if 'clause' is a qual that can be pushed into a WindowFunc's
                               2407                 :                :  *      WindowClause as a 'runCondition' qual.  These, when present, allow
                               2408                 :                :  *      some unnecessary work to be skipped during execution.
                               2409                 :                :  *
                               2410                 :                :  * 'run_cond_attrs' will be populated with all targetlist resnos of subquery
                               2411                 :                :  * targets (offset by FirstLowInvalidHeapAttributeNumber) that we pushed
                               2412                 :                :  * window quals for.
                               2413                 :                :  *
                               2414                 :                :  * Returns true if the caller still must keep the original qual or false if
                               2415                 :                :  * the caller can safely ignore the original qual because the WindowAgg node
                               2416                 :                :  * will use the runCondition to stop returning tuples.
                               2417                 :                :  */
                               2418                 :                : static bool
                               2419                 :            120 : check_and_push_window_quals(Query *subquery, RangeTblEntry *rte, Index rti,
                               2420                 :                :                             Node *clause, Bitmapset **run_cond_attrs)
                               2421                 :                : {
                               2422                 :            120 :     OpExpr     *opexpr = (OpExpr *) clause;
                               2423                 :            120 :     bool        keep_original = true;
                               2424                 :                :     Var        *var1;
                               2425                 :                :     Var        *var2;
                               2426                 :                : 
                               2427                 :                :     /* We're only able to use OpExprs with 2 operands */
                               2428         [ +  + ]:            120 :     if (!IsA(opexpr, OpExpr))
                               2429                 :              9 :         return true;
                               2430                 :                : 
                               2431         [ -  + ]:            111 :     if (list_length(opexpr->args) != 2)
  737 drowley@postgresql.o     2432                 :UBC           0 :         return true;
                               2433                 :                : 
                               2434                 :                :     /*
                               2435                 :                :      * Currently, we restrict this optimization to strict OpExprs.  The reason
                               2436                 :                :      * for this is that during execution, once the runcondition becomes false,
                               2437                 :                :      * we stop evaluating WindowFuncs.  To avoid leaving around stale window
                               2438                 :                :      * function result values, we set them to NULL.  Having only strict
                               2439                 :                :      * OpExprs here ensures that we properly filter out the tuples with NULLs
                               2440                 :                :      * in the top-level WindowAgg.
                               2441                 :                :      */
  494 drowley@postgresql.o     2442                 :CBC         111 :     set_opfuncid(opexpr);
                               2443         [ -  + ]:            111 :     if (!func_strict(opexpr->opfuncid))
  494 drowley@postgresql.o     2444                 :UBC           0 :         return true;
                               2445                 :                : 
                               2446                 :                :     /*
                               2447                 :                :      * Check for plain Vars that reference window functions in the subquery.
                               2448                 :                :      * If we find any, we'll ask find_window_run_conditions() if 'opexpr' can
                               2449                 :                :      * be used as part of the run condition.
                               2450                 :                :      */
                               2451                 :                : 
                               2452                 :                :     /* Check the left side of the OpExpr */
  737 drowley@postgresql.o     2453                 :CBC         111 :     var1 = linitial(opexpr->args);
                               2454   [ +  +  +  - ]:            111 :     if (IsA(var1, Var) && var1->varattno > 0)
                               2455                 :                :     {
                               2456                 :             93 :         TargetEntry *tle = list_nth(subquery->targetList, var1->varattno - 1);
                               2457                 :             93 :         WindowFunc *wfunc = (WindowFunc *) tle->expr;
                               2458                 :                : 
                               2459         [ +  + ]:             93 :         if (find_window_run_conditions(subquery, rte, rti, tle->resno, wfunc,
                               2460                 :                :                                        opexpr, true, &keep_original,
                               2461                 :                :                                        run_cond_attrs))
                               2462                 :             75 :             return keep_original;
                               2463                 :                :     }
                               2464                 :                : 
                               2465                 :                :     /* and check the right side */
                               2466                 :             36 :     var2 = lsecond(opexpr->args);
                               2467   [ +  +  +  - ]:             36 :     if (IsA(var2, Var) && var2->varattno > 0)
                               2468                 :                :     {
                               2469                 :             21 :         TargetEntry *tle = list_nth(subquery->targetList, var2->varattno - 1);
                               2470                 :             21 :         WindowFunc *wfunc = (WindowFunc *) tle->expr;
                               2471                 :                : 
                               2472         [ +  + ]:             21 :         if (find_window_run_conditions(subquery, rte, rti, tle->resno, wfunc,
                               2473                 :                :                                        opexpr, false, &keep_original,
                               2474                 :                :                                        run_cond_attrs))
                               2475                 :              9 :             return keep_original;
                               2476                 :                :     }
                               2477                 :                : 
                               2478                 :             27 :     return true;
                               2479                 :                : }
                               2480                 :                : 
                               2481                 :                : /*
                               2482                 :                :  * set_subquery_pathlist
                               2483                 :                :  *      Generate SubqueryScan access paths for a subquery RTE
                               2484                 :                :  *
                               2485                 :                :  * We don't currently support generating parameterized paths for subqueries
                               2486                 :                :  * by pushing join clauses down into them; it seems too expensive to re-plan
                               2487                 :                :  * the subquery multiple times to consider different alternatives.
                               2488                 :                :  * (XXX that could stand to be reconsidered, now that we use Paths.)
                               2489                 :                :  * So the paths made here will be parameterized if the subquery contains
                               2490                 :                :  * LATERAL references, otherwise not.  As long as that's true, there's no need
                               2491                 :                :  * for a separate set_subquery_size phase: just make the paths right away.
                               2492                 :                :  */
                               2493                 :                : static void
 6888 tgl@sss.pgh.pa.us        2494                 :           3788 : set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
                               2495                 :                :                       Index rti, RangeTblEntry *rte)
                               2496                 :                : {
 6883                          2497                 :           3788 :     Query      *parse = root->parse;
 8308                          2498                 :           3788 :     Query      *subquery = rte->subquery;
                               2499                 :                :     bool        trivial_pathtarget;
                               2500                 :                :     Relids      required_outer;
                               2501                 :                :     pushdown_safety_info safetyInfo;
                               2502                 :                :     double      tuple_fraction;
                               2503                 :                :     RelOptInfo *sub_final_rel;
  688 drowley@postgresql.o     2504                 :           3788 :     Bitmapset  *run_cond_attrs = NULL;
                               2505                 :                :     ListCell   *lc;
                               2506                 :                : 
                               2507                 :                :     /*
                               2508                 :                :      * Must copy the Query so that planning doesn't mess up the RTE contents
                               2509                 :                :      * (really really need to fix the planner to not scribble on its input,
                               2510                 :                :      * someday ... but see remove_unused_subquery_outputs to start with).
                               2511                 :                :      */
 5514 tgl@sss.pgh.pa.us        2512                 :           3788 :     subquery = copyObject(subquery);
                               2513                 :                : 
                               2514                 :                :     /*
                               2515                 :                :      * If it's a LATERAL subquery, it might contain some Vars of the current
                               2516                 :                :      * query level, requiring it to be treated as parameterized, even though
                               2517                 :                :      * we don't support pushing down join quals into subqueries.
                               2518                 :                :      */
 4249                          2519                 :           3788 :     required_outer = rel->lateral_relids;
                               2520                 :                : 
                               2521                 :                :     /*
                               2522                 :                :      * Zero out result area for subquery_is_pushdown_safe, so that it can set
                               2523                 :                :      * flags as needed while recursing.  In particular, we need a workspace
                               2524                 :                :      * for keeping track of the reasons why columns are unsafe to reference.
                               2525                 :                :      * These reasons are stored in the bits inside unsafeFlags[i] when we
                               2526                 :                :      * discover reasons that column i of the subquery is unsafe to be used in
                               2527                 :                :      * a pushed-down qual.
                               2528                 :                :      */
 3579                          2529                 :           3788 :     memset(&safetyInfo, 0, sizeof(safetyInfo));
  394 drowley@postgresql.o     2530                 :           3788 :     safetyInfo.unsafeFlags = (unsigned char *)
                               2531                 :           3788 :         palloc0((list_length(subquery->targetList) + 1) * sizeof(unsigned char));
                               2532                 :                : 
                               2533                 :                :     /*
                               2534                 :                :      * If the subquery has the "security_barrier" flag, it means the subquery
                               2535                 :                :      * originated from a view that must enforce row-level security.  Then we
                               2536                 :                :      * must not push down quals that contain leaky functions.  (Ideally this
                               2537                 :                :      * would be checked inside subquery_is_pushdown_safe, but since we don't
                               2538                 :                :      * currently pass the RTE to that function, we must do it here.)
                               2539                 :                :      */
 3579 tgl@sss.pgh.pa.us        2540                 :           3788 :     safetyInfo.unsafeLeaky = rte->security_barrier;
                               2541                 :                : 
                               2542                 :                :     /*
                               2543                 :                :      * If there are any restriction clauses that have been attached to the
                               2544                 :                :      * subquery relation, consider pushing them down to become WHERE or HAVING
                               2545                 :                :      * quals of the subquery itself.  This transformation is useful because it
                               2546                 :                :      * may allow us to generate a better plan for the subquery than evaluating
                               2547                 :                :      * all the subquery output rows and then filtering them.
                               2548                 :                :      *
                               2549                 :                :      * There are several cases where we cannot push down clauses. Restrictions
                               2550                 :                :      * involving the subquery are checked by subquery_is_pushdown_safe().
                               2551                 :                :      * Restrictions on individual clauses are checked by
                               2552                 :                :      * qual_is_pushdown_safe().  Also, we don't want to push down
                               2553                 :                :      * pseudoconstant clauses; better to have the gating node above the
                               2554                 :                :      * subquery.
                               2555                 :                :      *
                               2556                 :                :      * Non-pushed-down clauses will get evaluated as qpquals of the
                               2557                 :                :      * SubqueryScan node.
                               2558                 :                :      *
                               2559                 :                :      * XXX Are there any cases where we want to make a policy decision not to
                               2560                 :                :      * push down a pushable qual, because it'd result in a worse plan?
                               2561                 :                :      */
 7899                          2562   [ +  +  +  + ]:           4436 :     if (rel->baserestrictinfo != NIL &&
 3579                          2563                 :            648 :         subquery_is_pushdown_safe(subquery, subquery, &safetyInfo))
                               2564                 :                :     {
                               2565                 :                :         /* OK to consider pushing down individual quals */
 8308                          2566                 :            578 :         List       *upperrestrictlist = NIL;
                               2567                 :                :         ListCell   *l;
                               2568                 :                : 
 7263 neilc@samurai.com        2569   [ +  -  +  +  :           1344 :         foreach(l, rel->baserestrictinfo)
                                              +  + ]
                               2570                 :                :         {
                               2571                 :            766 :             RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
  737 drowley@postgresql.o     2572                 :            766 :             Node       *clause = (Node *) rinfo->clause;
                               2573                 :                : 
  394                          2574         [ +  + ]:            766 :             if (rinfo->pseudoconstant)
                               2575                 :                :             {
                               2576                 :              2 :                 upperrestrictlist = lappend(upperrestrictlist, rinfo);
                               2577                 :              2 :                 continue;
                               2578                 :                :             }
                               2579                 :                : 
                               2580   [ +  +  +  - ]:            764 :             switch (qual_is_pushdown_safe(subquery, rti, rinfo, &safetyInfo))
                               2581                 :                :             {
                               2582                 :            438 :                 case PUSHDOWN_SAFE:
                               2583                 :                :                     /* Push it down */
                               2584                 :            438 :                     subquery_push_qual(subquery, rte, rti, clause);
                               2585                 :            438 :                     break;
                               2586                 :                : 
                               2587                 :            120 :                 case PUSHDOWN_WINDOWCLAUSE_RUNCOND:
                               2588                 :                : 
                               2589                 :                :                     /*
                               2590                 :                :                      * Since we can't push the qual down into the subquery,
                               2591                 :                :                      * check if it happens to reference a window function.  If
                               2592                 :                :                      * so then it might be useful to use for the WindowAgg's
                               2593                 :                :                      * runCondition.
                               2594                 :                :                      */
                               2595   [ +  -  +  + ]:            240 :                     if (!subquery->hasWindowFuncs ||
                               2596                 :            120 :                         check_and_push_window_quals(subquery, rte, rti, clause,
                               2597                 :                :                                                     &run_cond_attrs))
                               2598                 :                :                     {
                               2599                 :                :                         /*
                               2600                 :                :                          * subquery has no window funcs or the clause is not a
                               2601                 :                :                          * suitable window run condition qual or it is, but
                               2602                 :                :                          * the original must also be kept in the upper query.
                               2603                 :                :                          */
                               2604                 :             48 :                         upperrestrictlist = lappend(upperrestrictlist, rinfo);
                               2605                 :                :                     }
                               2606                 :            120 :                     break;
                               2607                 :                : 
                               2608                 :            206 :                 case PUSHDOWN_UNSAFE:
  737                          2609                 :            206 :                     upperrestrictlist = lappend(upperrestrictlist, rinfo);
  394                          2610                 :            206 :                     break;
                               2611                 :                :             }
                               2612                 :                :         }
 8308 tgl@sss.pgh.pa.us        2613                 :            578 :         rel->baserestrictinfo = upperrestrictlist;
                               2614                 :                :         /* We don't bother recomputing baserestrict_min_security */
                               2615                 :                :     }
                               2616                 :                : 
  394 drowley@postgresql.o     2617                 :           3788 :     pfree(safetyInfo.unsafeFlags);
                               2618                 :                : 
                               2619                 :                :     /*
                               2620                 :                :      * The upper query might not use all the subquery's output columns; if
                               2621                 :                :      * not, we can simplify.  Pass the attributes that were pushed down into
                               2622                 :                :      * WindowAgg run conditions to ensure we don't accidentally think those
                               2623                 :                :      * are unused.
                               2624                 :                :      */
  688                          2625                 :           3788 :     remove_unused_subquery_outputs(subquery, rel, run_cond_attrs);
                               2626                 :                : 
                               2627                 :                :     /*
                               2628                 :                :      * We can safely pass the outer tuple_fraction down to the subquery if the
                               2629                 :                :      * outer level has no joining, aggregation, or sorting to do. Otherwise
                               2630                 :                :      * we'd better tell the subquery to plan for full retrieval. (XXX This
                               2631                 :                :      * could probably be made more intelligent ...)
                               2632                 :                :      */
 6883 tgl@sss.pgh.pa.us        2633         [ +  + ]:           3788 :     if (parse->hasAggs ||
                               2634         [ +  + ]:           3276 :         parse->groupClause ||
 3256 andres@anarazel.de       2635         [ +  - ]:           3273 :         parse->groupingSets ||
  544 tgl@sss.pgh.pa.us        2636         [ +  - ]:           3273 :         root->hasHavingQual ||
 6883                          2637         [ +  + ]:           3273 :         parse->distinctClause ||
                               2638   [ +  +  +  + ]:           5263 :         parse->sortClause ||
  187 tgl@sss.pgh.pa.us        2639                 :GNC        2202 :         bms_membership(root->all_baserels) == BMS_MULTIPLE)
 6883 tgl@sss.pgh.pa.us        2640                 :CBC        2144 :         tuple_fraction = 0.0;   /* default case */
                               2641                 :                :     else
                               2642                 :           1644 :         tuple_fraction = root->tuple_fraction;
                               2643                 :                : 
                               2644                 :                :     /* plan_params should not be in use in current query level */
 4239                          2645         [ -  + ]:           3788 :     Assert(root->plan_params == NIL);
                               2646                 :                : 
                               2647                 :                :     /* Generate a subroot and Paths for the subquery */
   12 drowley@postgresql.o     2648                 :GNC        3788 :     rel->subroot = subquery_planner(root->glob, subquery, root, false,
                               2649                 :                :                                     tuple_fraction, NULL);
                               2650                 :                : 
                               2651                 :                :     /* Isolate the params needed by this specific subplan */
 4239 tgl@sss.pgh.pa.us        2652                 :CBC        3788 :     rel->subplan_params = root->plan_params;
                               2653                 :           3788 :     root->plan_params = NIL;
                               2654                 :                : 
                               2655                 :                :     /*
                               2656                 :                :      * It's possible that constraint exclusion proved the subquery empty. If
                               2657                 :                :      * so, it's desirable to produce an unadorned dummy path so that we will
                               2658                 :                :      * recognize appropriate optimizations at this query level.
                               2659                 :                :      */
 2960                          2660                 :           3788 :     sub_final_rel = fetch_upper_rel(rel->subroot, UPPERREL_FINAL, NULL);
                               2661                 :                : 
                               2662         [ +  + ]:           3788 :     if (IS_DUMMY_REL(sub_final_rel))
                               2663                 :                :     {
 4586                          2664                 :             54 :         set_dummy_rel_pathlist(rel);
                               2665                 :             54 :         return;
                               2666                 :                :     }
                               2667                 :                : 
                               2668                 :                :     /*
                               2669                 :                :      * Mark rel with estimated output rows, width, etc.  Note that we have to
                               2670                 :                :      * do this before generating outer-query paths, else cost_subqueryscan is
                               2671                 :                :      * not happy.
                               2672                 :                :      */
 4607                          2673                 :           3734 :     set_subquery_size_estimates(root, rel);
                               2674                 :                : 
                               2675                 :                :     /*
                               2676                 :                :      * Also detect whether the reltarget is trivial, so that we can pass that
                               2677                 :                :      * info to cost_subqueryscan (rather than re-deriving it multiple times).
                               2678                 :                :      * It's trivial if it fetches all the subplan output columns in order.
                               2679                 :                :      */
  635                          2680         [ +  + ]:           3734 :     if (list_length(rel->reltarget->exprs) != list_length(subquery->targetList))
                               2681                 :           1147 :         trivial_pathtarget = false;
                               2682                 :                :     else
                               2683                 :                :     {
                               2684                 :           2587 :         trivial_pathtarget = true;
                               2685   [ +  -  +  +  :           7377 :         foreach(lc, rel->reltarget->exprs)
                                              +  + ]
                               2686                 :                :         {
                               2687                 :           4930 :             Node       *node = (Node *) lfirst(lc);
                               2688                 :                :             Var        *var;
                               2689                 :                : 
                               2690         [ -  + ]:           4930 :             if (!IsA(node, Var))
                               2691                 :                :             {
  635 tgl@sss.pgh.pa.us        2692                 :UBC           0 :                 trivial_pathtarget = false;
                               2693                 :              0 :                 break;
                               2694                 :                :             }
  635 tgl@sss.pgh.pa.us        2695                 :CBC        4930 :             var = (Var *) node;
                               2696         [ +  - ]:           4930 :             if (var->varno != rti ||
                               2697         [ +  + ]:           4930 :                 var->varattno != foreach_current_index(lc) + 1)
                               2698                 :                :             {
                               2699                 :            140 :                 trivial_pathtarget = false;
                               2700                 :            140 :                 break;
                               2701                 :                :             }
                               2702                 :                :         }
                               2703                 :                :     }
                               2704                 :                : 
                               2705                 :                :     /*
                               2706                 :                :      * For each Path that subquery_planner produced, make a SubqueryScanPath
                               2707                 :                :      * in the outer query.
                               2708                 :                :      */
 2960                          2709   [ +  -  +  +  :           7791 :     foreach(lc, sub_final_rel->pathlist)
                                              +  + ]
                               2710                 :                :     {
                               2711                 :           4057 :         Path       *subpath = (Path *) lfirst(lc);
                               2712                 :                :         List       *pathkeys;
                               2713                 :                : 
                               2714                 :                :         /* Convert subpath's pathkeys to outer representation */
                               2715                 :           4057 :         pathkeys = convert_subquery_pathkeys(root,
                               2716                 :                :                                              rel,
                               2717                 :                :                                              subpath->pathkeys,
                               2718                 :                :                                              make_tlist_from_pathtarget(subpath->pathtarget));
                               2719                 :                : 
                               2720                 :                :         /* Generate outer path using this subpath */
                               2721                 :           4057 :         add_path(rel, (Path *)
                               2722                 :           4057 :                  create_subqueryscan_path(root, rel, subpath,
                               2723                 :                :                                           trivial_pathtarget,
                               2724                 :                :                                           pathkeys, required_outer));
                               2725                 :                :     }
                               2726                 :                : 
                               2727                 :                :     /* If outer rel allows parallelism, do same for partial paths. */
 2181 rhaas@postgresql.org     2728   [ +  +  +  + ]:           3734 :     if (rel->consider_parallel && bms_is_empty(required_outer))
                               2729                 :                :     {
                               2730                 :                :         /* If consider_parallel is false, there should be no partial paths. */
                               2731   [ +  +  -  + ]:           1922 :         Assert(sub_final_rel->consider_parallel ||
                               2732                 :                :                sub_final_rel->partial_pathlist == NIL);
                               2733                 :                : 
                               2734                 :                :         /* Same for partial paths. */
                               2735   [ +  +  +  +  :           1943 :         foreach(lc, sub_final_rel->partial_pathlist)
                                              +  + ]
                               2736                 :                :         {
                               2737                 :             21 :             Path       *subpath = (Path *) lfirst(lc);
                               2738                 :                :             List       *pathkeys;
                               2739                 :                : 
                               2740                 :                :             /* Convert subpath's pathkeys to outer representation */
                               2741                 :             21 :             pathkeys = convert_subquery_pathkeys(root,
                               2742                 :                :                                                  rel,
                               2743                 :                :                                                  subpath->pathkeys,
                               2744                 :                :                                                  make_tlist_from_pathtarget(subpath->pathtarget));
                               2745                 :                : 
                               2746                 :                :             /* Generate outer path using this subpath */
                               2747                 :             21 :             add_partial_path(rel, (Path *)
                               2748                 :             21 :                              create_subqueryscan_path(root, rel, subpath,
                               2749                 :                :                                                       trivial_pathtarget,
                               2750                 :                :                                                       pathkeys,
                               2751                 :                :                                                       required_outer));
                               2752                 :                :         }
                               2753                 :                :     }
                               2754                 :                : }
                               2755                 :                : 
                               2756                 :                : /*
                               2757                 :                :  * set_function_pathlist
                               2758                 :                :  *      Build the (single) access path for a function RTE
                               2759                 :                :  */
                               2760                 :                : static void
 6888 tgl@sss.pgh.pa.us        2761                 :          21525 : set_function_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
                               2762                 :                : {
                               2763                 :                :     Relids      required_outer;
 3797                          2764                 :          21525 :     List       *pathkeys = NIL;
                               2765                 :                : 
                               2766                 :                :     /*
                               2767                 :                :      * We don't support pushing join clauses into the quals of a function
                               2768                 :                :      * scan, but it could still have required parameterization due to LATERAL
                               2769                 :                :      * refs in the function expression.
                               2770                 :                :      */
 4249                          2771                 :          21525 :     required_outer = rel->lateral_relids;
                               2772                 :                : 
                               2773                 :                :     /*
                               2774                 :                :      * The result is considered unordered unless ORDINALITY was used, in which
                               2775                 :                :      * case it is ordered by the ordinal column (the last one).  See if we
                               2776                 :                :      * care, by checking for uses of that Var in equivalence classes.
                               2777                 :                :      */
 3797                          2778         [ +  + ]:          21525 :     if (rte->funcordinality)
                               2779                 :                :     {
                               2780                 :            342 :         AttrNumber  ordattno = rel->max_attr;
                               2781                 :            342 :         Var        *var = NULL;
                               2782                 :                :         ListCell   *lc;
                               2783                 :                : 
                               2784                 :                :         /*
                               2785                 :                :          * Is there a Var for it in rel's targetlist?  If not, the query did
                               2786                 :                :          * not reference the ordinality column, or at least not in any way
                               2787                 :                :          * that would be interesting for sorting.
                               2788                 :                :          */
 2953                          2789   [ +  -  +  +  :            919 :         foreach(lc, rel->reltarget->exprs)
                                              +  + ]
                               2790                 :                :         {
 3797                          2791                 :            916 :             Var        *node = (Var *) lfirst(lc);
                               2792                 :                : 
                               2793                 :                :             /* checking varno/varlevelsup is just paranoia */
                               2794         [ +  - ]:            916 :             if (IsA(node, Var) &&
                               2795         [ +  + ]:            916 :                 node->varattno == ordattno &&
                               2796         [ +  - ]:            339 :                 node->varno == rel->relid &&
                               2797         [ +  - ]:            339 :                 node->varlevelsup == 0)
                               2798                 :                :             {
                               2799                 :            339 :                 var = node;
                               2800                 :            339 :                 break;
                               2801                 :                :             }
                               2802                 :                :         }
                               2803                 :                : 
                               2804                 :                :         /*
                               2805                 :                :          * Try to build pathkeys for this Var with int8 sorting.  We tell
                               2806                 :                :          * build_expression_pathkey not to build any new equivalence class; if
                               2807                 :                :          * the Var isn't already mentioned in some EC, it means that nothing
                               2808                 :                :          * cares about the ordering.
                               2809                 :                :          */
                               2810         [ +  + ]:            342 :         if (var)
                               2811                 :            339 :             pathkeys = build_expression_pathkey(root,
                               2812                 :                :                                                 (Expr *) var,
                               2813                 :                :                                                 Int8LessOperator,
                               2814                 :                :                                                 rel->relids,
                               2815                 :                :                                                 false);
                               2816                 :                :     }
                               2817                 :                : 
                               2818                 :                :     /* Generate appropriate path */
                               2819                 :          21525 :     add_path(rel, create_functionscan_path(root, rel,
                               2820                 :                :                                            pathkeys, required_outer));
 8008                          2821                 :          21525 : }
                               2822                 :                : 
                               2823                 :                : /*
                               2824                 :                :  * set_values_pathlist
                               2825                 :                :  *      Build the (single) access path for a VALUES RTE
                               2826                 :                :  */
                               2827                 :                : static void
 6465 mail@joeconway.com       2828                 :           3858 : set_values_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
                               2829                 :                : {
                               2830                 :                :     Relids      required_outer;
                               2831                 :                : 
                               2832                 :                :     /*
                               2833                 :                :      * We don't support pushing join clauses into the quals of a values scan,
                               2834                 :                :      * but it could still have required parameterization due to LATERAL refs
                               2835                 :                :      * in the values expressions.
                               2836                 :                :      */
 4249 tgl@sss.pgh.pa.us        2837                 :           3858 :     required_outer = rel->lateral_relids;
                               2838                 :                : 
                               2839                 :                :     /* Generate appropriate path */
 4263                          2840                 :           3858 :     add_path(rel, create_valuesscan_path(root, rel, required_outer));
 6465 mail@joeconway.com       2841                 :           3858 : }
                               2842                 :                : 
                               2843                 :                : /*
                               2844                 :                :  * set_tablefunc_pathlist
                               2845                 :                :  *      Build the (single) access path for a table func RTE
                               2846                 :                :  */
                               2847                 :                : static void
 2594 alvherre@alvh.no-ip.     2848                 :            254 : set_tablefunc_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
                               2849                 :                : {
                               2850                 :                :     Relids      required_outer;
                               2851                 :                : 
                               2852                 :                :     /*
                               2853                 :                :      * We don't support pushing join clauses into the quals of a tablefunc
                               2854                 :                :      * scan, but it could still have required parameterization due to LATERAL
                               2855                 :                :      * refs in the function expression.
                               2856                 :                :      */
                               2857                 :            254 :     required_outer = rel->lateral_relids;
                               2858                 :                : 
                               2859                 :                :     /* Generate appropriate path */
                               2860                 :            254 :     add_path(rel, create_tablefuncscan_path(root, rel,
                               2861                 :                :                                             required_outer));
                               2862                 :            254 : }
                               2863                 :                : 
                               2864                 :                : /*
                               2865                 :                :  * set_cte_pathlist
                               2866                 :                :  *      Build the (single) access path for a non-self-reference CTE RTE
                               2867                 :                :  *
                               2868                 :                :  * There's no need for a separate set_cte_size phase, since we don't
                               2869                 :                :  * support join-qual-parameterized paths for CTEs.
                               2870                 :                :  */
                               2871                 :                : static void
 5671 tgl@sss.pgh.pa.us        2872                 :           1603 : set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
                               2873                 :                : {
                               2874                 :                :     Path       *ctepath;
                               2875                 :                :     Plan       *cteplan;
                               2876                 :                :     PlannerInfo *cteroot;
                               2877                 :                :     Index       levelsup;
                               2878                 :                :     List       *pathkeys;
                               2879                 :                :     int         ndx;
                               2880                 :                :     ListCell   *lc;
                               2881                 :                :     int         plan_id;
                               2882                 :                :     Relids      required_outer;
                               2883                 :                : 
                               2884                 :                :     /*
                               2885                 :                :      * Find the referenced CTE, and locate the path and plan previously made
                               2886                 :                :      * for it.
                               2887                 :                :      */
                               2888                 :           1603 :     levelsup = rte->ctelevelsup;
                               2889                 :           1603 :     cteroot = root;
                               2890         [ +  + ]:           2848 :     while (levelsup-- > 0)
                               2891                 :                :     {
                               2892                 :           1245 :         cteroot = cteroot->parent_root;
                               2893         [ -  + ]:           1245 :         if (!cteroot)           /* shouldn't happen */
 5671 tgl@sss.pgh.pa.us        2894         [ #  # ]:UBC           0 :             elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
                               2895                 :                :     }
                               2896                 :                : 
                               2897                 :                :     /*
                               2898                 :                :      * Note: cte_plan_ids can be shorter than cteList, if we are still working
                               2899                 :                :      * on planning the CTEs (ie, this is a side-reference from another CTE).
                               2900                 :                :      * So we mustn't use forboth here.
                               2901                 :                :      */
 5671 tgl@sss.pgh.pa.us        2902                 :CBC        1603 :     ndx = 0;
                               2903   [ +  -  +  -  :           2294 :     foreach(lc, cteroot->parse->cteList)
                                              +  - ]
                               2904                 :                :     {
                               2905                 :           2294 :         CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
                               2906                 :                : 
                               2907         [ +  + ]:           2294 :         if (strcmp(cte->ctename, rte->ctename) == 0)
                               2908                 :           1603 :             break;
                               2909                 :            691 :         ndx++;
                               2910                 :                :     }
                               2911         [ -  + ]:           1603 :     if (lc == NULL)             /* shouldn't happen */
 5671 tgl@sss.pgh.pa.us        2912         [ #  # ]:UBC           0 :         elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
 5671 tgl@sss.pgh.pa.us        2913         [ -  + ]:CBC        1603 :     if (ndx >= list_length(cteroot->cte_plan_ids))
 5671 tgl@sss.pgh.pa.us        2914         [ #  # ]:UBC           0 :         elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
 5671 tgl@sss.pgh.pa.us        2915                 :CBC        1603 :     plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
  724                          2916         [ -  + ]:           1603 :     if (plan_id <= 0)
  724 tgl@sss.pgh.pa.us        2917         [ #  # ]:UBC           0 :         elog(ERROR, "no plan was made for CTE \"%s\"", rte->ctename);
                               2918                 :                : 
   19 tgl@sss.pgh.pa.us        2919         [ -  + ]:GNC        1603 :     Assert(list_length(root->glob->subpaths) == list_length(root->glob->subplans));
                               2920                 :           1603 :     ctepath = (Path *) list_nth(root->glob->subpaths, plan_id - 1);
 5671 tgl@sss.pgh.pa.us        2921                 :CBC        1603 :     cteplan = (Plan *) list_nth(root->glob->subplans, plan_id - 1);
                               2922                 :                : 
                               2923                 :                :     /* Mark rel with estimated output rows, width, etc */
 2960                          2924                 :           1603 :     set_cte_size_estimates(root, rel, cteplan->plan_rows);
                               2925                 :                : 
                               2926                 :                :     /* Convert the ctepath's pathkeys to outer query's representation */
   19 tgl@sss.pgh.pa.us        2927                 :GNC        1603 :     pathkeys = convert_subquery_pathkeys(root,
                               2928                 :                :                                          rel,
                               2929                 :                :                                          ctepath->pathkeys,
                               2930                 :                :                                          cteplan->targetlist);
                               2931                 :                : 
                               2932                 :                :     /*
                               2933                 :                :      * We don't support pushing join clauses into the quals of a CTE scan, but
                               2934                 :                :      * it could still have required parameterization due to LATERAL refs in
                               2935                 :                :      * its tlist.
                               2936                 :                :      */
 4249 tgl@sss.pgh.pa.us        2937                 :CBC        1603 :     required_outer = rel->lateral_relids;
                               2938                 :                : 
                               2939                 :                :     /* Generate appropriate path */
   19 tgl@sss.pgh.pa.us        2940                 :GNC        1603 :     add_path(rel, create_ctescan_path(root, rel, pathkeys, required_outer));
 5671 tgl@sss.pgh.pa.us        2941                 :CBC        1603 : }
                               2942                 :                : 
                               2943                 :                : /*
                               2944                 :                :  * set_namedtuplestore_pathlist
                               2945                 :                :  *      Build the (single) access path for a named tuplestore RTE
                               2946                 :                :  *
                               2947                 :                :  * There's no need for a separate set_namedtuplestore_size phase, since we
                               2948                 :                :  * don't support join-qual-parameterized paths for tuplestores.
                               2949                 :                :  */
                               2950                 :                : static void
 2571 kgrittn@postgresql.o     2951                 :            223 : set_namedtuplestore_pathlist(PlannerInfo *root, RelOptInfo *rel,
                               2952                 :                :                              RangeTblEntry *rte)
                               2953                 :                : {
                               2954                 :                :     Relids      required_outer;
                               2955                 :                : 
                               2956                 :                :     /* Mark rel with estimated output rows, width, etc */
                               2957                 :            223 :     set_namedtuplestore_size_estimates(root, rel);
                               2958                 :                : 
                               2959                 :                :     /*
                               2960                 :                :      * We don't support pushing join clauses into the quals of a tuplestore
                               2961                 :                :      * scan, but it could still have required parameterization due to LATERAL
                               2962                 :                :      * refs in its tlist.
                               2963                 :                :      */
                               2964                 :            223 :     required_outer = rel->lateral_relids;
                               2965                 :                : 
                               2966                 :                :     /* Generate appropriate path */
                               2967                 :            223 :     add_path(rel, create_namedtuplestorescan_path(root, rel, required_outer));
                               2968                 :            223 : }
                               2969                 :                : 
                               2970                 :                : /*
                               2971                 :                :  * set_result_pathlist
                               2972                 :                :  *      Build the (single) access path for an RTE_RESULT RTE
                               2973                 :                :  *
                               2974                 :                :  * There's no need for a separate set_result_size phase, since we
                               2975                 :                :  * don't support join-qual-parameterized paths for these RTEs.
                               2976                 :                :  */
                               2977                 :                : static void
 1903 tgl@sss.pgh.pa.us        2978                 :            781 : set_result_pathlist(PlannerInfo *root, RelOptInfo *rel,
                               2979                 :                :                     RangeTblEntry *rte)
                               2980                 :                : {
                               2981                 :                :     Relids      required_outer;
                               2982                 :                : 
                               2983                 :                :     /* Mark rel with estimated output rows, width, etc */
                               2984                 :            781 :     set_result_size_estimates(root, rel);
                               2985                 :                : 
                               2986                 :                :     /*
                               2987                 :                :      * We don't support pushing join clauses into the quals of a Result scan,
                               2988                 :                :      * but it could still have required parameterization due to LATERAL refs
                               2989                 :                :      * in its tlist.
                               2990                 :                :      */
                               2991                 :            781 :     required_outer = rel->lateral_relids;
                               2992                 :                : 
                               2993                 :                :     /* Generate appropriate path */
                               2994                 :            781 :     add_path(rel, create_resultscan_path(root, rel, required_outer));
                               2995                 :            781 : }
                               2996                 :                : 
                               2997                 :                : /*
                               2998                 :                :  * set_worktable_pathlist
                               2999                 :                :  *      Build the (single) access path for a self-reference CTE RTE
                               3000                 :                :  *
                               3001                 :                :  * There's no need for a separate set_worktable_size phase, since we don't
                               3002                 :                :  * support join-qual-parameterized paths for CTEs.
                               3003                 :                :  */
                               3004                 :                : static void
 5671                          3005                 :            406 : set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
                               3006                 :                : {
                               3007                 :                :     Path       *ctepath;
                               3008                 :                :     PlannerInfo *cteroot;
                               3009                 :                :     Index       levelsup;
                               3010                 :                :     Relids      required_outer;
                               3011                 :                : 
                               3012                 :                :     /*
                               3013                 :                :      * We need to find the non-recursive term's path, which is in the plan
                               3014                 :                :      * level that's processing the recursive UNION, which is one level *below*
                               3015                 :                :      * where the CTE comes from.
                               3016                 :                :      */
                               3017                 :            406 :     levelsup = rte->ctelevelsup;
                               3018         [ -  + ]:            406 :     if (levelsup == 0)          /* shouldn't happen */
 5671 tgl@sss.pgh.pa.us        3019         [ #  # ]:UBC           0 :         elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
 5671 tgl@sss.pgh.pa.us        3020                 :CBC         406 :     levelsup--;
                               3021                 :            406 :     cteroot = root;
                               3022         [ +  + ]:            922 :     while (levelsup-- > 0)
                               3023                 :                :     {
                               3024                 :            516 :         cteroot = cteroot->parent_root;
                               3025         [ -  + ]:            516 :         if (!cteroot)           /* shouldn't happen */
 5671 tgl@sss.pgh.pa.us        3026         [ #  # ]:UBC           0 :             elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
                               3027                 :                :     }
 2960 tgl@sss.pgh.pa.us        3028                 :CBC         406 :     ctepath = cteroot->non_recursive_path;
                               3029         [ -  + ]:            406 :     if (!ctepath)               /* shouldn't happen */
 2960 tgl@sss.pgh.pa.us        3030         [ #  # ]:UBC           0 :         elog(ERROR, "could not find path for CTE \"%s\"", rte->ctename);
                               3031                 :                : 
                               3032                 :                :     /* Mark rel with estimated output rows, width, etc */
 2960 tgl@sss.pgh.pa.us        3033                 :CBC         406 :     set_cte_size_estimates(root, rel, ctepath->rows);
                               3034                 :                : 
                               3035                 :                :     /*
                               3036                 :                :      * We don't support pushing join clauses into the quals of a worktable
                               3037                 :                :      * scan, but it could still have required parameterization due to LATERAL
                               3038                 :                :      * refs in its tlist.  (I'm not sure this is actually possible given the
                               3039                 :                :      * restrictions on recursive references, but it's easy enough to support.)
                               3040                 :                :      */
 4249                          3041                 :            406 :     required_outer = rel->lateral_relids;
                               3042                 :                : 
                               3043                 :                :     /* Generate appropriate path */
                               3044                 :            406 :     add_path(rel, create_worktablescan_path(root, rel, required_outer));
 5671                          3045                 :            406 : }
                               3046                 :                : 
                               3047                 :                : /*
                               3048                 :                :  * generate_gather_paths
                               3049                 :                :  *      Generate parallel access paths for a relation by pushing a Gather or
                               3050                 :                :  *      Gather Merge on top of a partial path.
                               3051                 :                :  *
                               3052                 :                :  * This must not be called until after we're done creating all partial paths
                               3053                 :                :  * for the specified relation.  (Otherwise, add_partial_path might delete a
                               3054                 :                :  * path that some GatherPath or GatherMergePath has a reference to.)
                               3055                 :                :  *
                               3056                 :                :  * If we're generating paths for a scan or join relation, override_rows will
                               3057                 :                :  * be false, and we'll just use the relation's size estimate.  When we're
                               3058                 :                :  * being called for a partially-grouped or partially-distinct path, though, we
                               3059                 :                :  * need to override the rowcount estimate.  (It's not clear that the
                               3060                 :                :  * particular value we're using here is actually best, but the underlying rel
                               3061                 :                :  * has no estimate so we must do something.)
                               3062                 :                :  */
                               3063                 :                : void
 2239 rhaas@postgresql.org     3064                 :           8065 : generate_gather_paths(PlannerInfo *root, RelOptInfo *rel, bool override_rows)
                               3065                 :                : {
                               3066                 :                :     Path       *cheapest_partial_path;
                               3067                 :                :     Path       *simple_gather_path;
                               3068                 :                :     ListCell   *lc;
                               3069                 :                :     double      rows;
                               3070                 :           8065 :     double     *rowsp = NULL;
                               3071                 :                : 
                               3072                 :                :     /* If there are no partial paths, there's nothing to do here. */
 3007                          3073         [ -  + ]:           8065 :     if (rel->partial_pathlist == NIL)
 3007 rhaas@postgresql.org     3074                 :UBC           0 :         return;
                               3075                 :                : 
                               3076                 :                :     /* Should we override the rel's rowcount estimate? */
 2239 rhaas@postgresql.org     3077         [ +  + ]:CBC        8065 :     if (override_rows)
                               3078                 :            869 :         rowsp = &rows;
                               3079                 :                : 
                               3080                 :                :     /*
                               3081                 :                :      * The output of Gather is always unsorted, so there's only one partial
                               3082                 :                :      * path of interest: the cheapest one.  That will be the one at the front
                               3083                 :                :      * of partial_pathlist because of the way add_partial_path works.
                               3084                 :                :      */
 3007                          3085                 :           8065 :     cheapest_partial_path = linitial(rel->partial_pathlist);
 2239                          3086                 :           8065 :     rows =
                               3087                 :           8065 :         cheapest_partial_path->rows * cheapest_partial_path->parallel_workers;
                               3088                 :                :     simple_gather_path = (Path *)
 2946                          3089                 :           8065 :         create_gather_path(root, rel, cheapest_partial_path, rel->reltarget,
                               3090                 :                :                            NULL, rowsp);
 3007                          3091                 :           8065 :     add_path(rel, simple_gather_path);
                               3092                 :                : 
                               3093                 :                :     /*
                               3094                 :                :      * For each useful ordering, we can consider an order-preserving Gather
                               3095                 :                :      * Merge.
                               3096                 :                :      */
 2524 bruce@momjian.us         3097   [ +  -  +  +  :          16779 :     foreach(lc, rel->partial_pathlist)
                                              +  + ]
                               3098                 :                :     {
                               3099                 :           8714 :         Path       *subpath = (Path *) lfirst(lc);
                               3100                 :                :         GatherMergePath *path;
                               3101                 :                : 
 2593 rhaas@postgresql.org     3102         [ +  + ]:           8714 :         if (subpath->pathkeys == NIL)
                               3103                 :           7902 :             continue;
                               3104                 :                : 
 2239                          3105                 :            812 :         rows = subpath->rows * subpath->parallel_workers;
 2593                          3106                 :            812 :         path = create_gather_merge_path(root, rel, subpath, rel->reltarget,
                               3107                 :                :                                         subpath->pathkeys, NULL, rowsp);
                               3108                 :            812 :         add_path(rel, &path->path);
                               3109                 :                :     }
                               3110                 :                : }
                               3111                 :                : 
                               3112                 :                : /*
                               3113                 :                :  * get_useful_pathkeys_for_relation
                               3114                 :                :  *      Determine which orderings of a relation might be useful.
                               3115                 :                :  *
                               3116                 :                :  * Getting data in sorted order can be useful either because the requested
                               3117                 :                :  * order matches the final output ordering for the overall query we're
                               3118                 :                :  * planning, or because it enables an efficient merge join.  Here, we try
                               3119                 :                :  * to figure out which pathkeys to consider.
                               3120                 :                :  *
                               3121                 :                :  * This allows us to do incremental sort on top of an index scan under a gather
                               3122                 :                :  * merge node, i.e. parallelized.
                               3123                 :                :  *
                               3124                 :                :  * If the require_parallel_safe is true, we also require the expressions to
                               3125                 :                :  * be parallel safe (which allows pushing the sort below Gather Merge).
                               3126                 :                :  *
                               3127                 :                :  * XXX At the moment this can only ever return a list with a single element,
                               3128                 :                :  * because it looks at query_pathkeys only. So we might return the pathkeys
                               3129                 :                :  * directly, but it seems plausible we'll want to consider other orderings
                               3130                 :                :  * in the future. For example, we might want to consider pathkeys useful for
                               3131                 :                :  * merge joins.
                               3132                 :                :  */
                               3133                 :                : static List *
 1210 tomas.vondra@postgre     3134                 :           8065 : get_useful_pathkeys_for_relation(PlannerInfo *root, RelOptInfo *rel,
                               3135                 :                :                                  bool require_parallel_safe)
                               3136                 :                : {
 1468                          3137                 :           8065 :     List       *useful_pathkeys_list = NIL;
                               3138                 :                : 
                               3139                 :                :     /*
                               3140                 :                :      * Considering query_pathkeys is always worth it, because it might allow
                               3141                 :                :      * us to avoid a total sort when we have a partially presorted path
                               3142                 :                :      * available or to push the total sort into the parallel portion of the
                               3143                 :                :      * query.
                               3144                 :                :      */
                               3145         [ +  + ]:           8065 :     if (root->query_pathkeys)
                               3146                 :                :     {
                               3147                 :                :         ListCell   *lc;
 1431 tgl@sss.pgh.pa.us        3148                 :           3473 :         int         npathkeys = 0;  /* useful pathkeys */
                               3149                 :                : 
 1468 tomas.vondra@postgre     3150   [ +  -  +  +  :           7458 :         foreach(lc, root->query_pathkeys)
                                              +  + ]
                               3151                 :                :         {
                               3152                 :           5011 :             PathKey    *pathkey = (PathKey *) lfirst(lc);
                               3153                 :           5011 :             EquivalenceClass *pathkey_ec = pathkey->pk_eclass;
                               3154                 :                : 
                               3155                 :                :             /*
                               3156                 :                :              * We can only build a sort for pathkeys that contain a
                               3157                 :                :              * safe-to-compute-early EC member computable from the current
                               3158                 :                :              * relation's reltarget, so ignore the remainder of the list as
                               3159                 :                :              * soon as we find a pathkey without such a member.
                               3160                 :                :              *
                               3161                 :                :              * It's still worthwhile to return any prefix of the pathkeys list
                               3162                 :                :              * that meets this requirement, as we may be able to do an
                               3163                 :                :              * incremental sort.
                               3164                 :                :              *
                               3165                 :                :              * If requested, ensure the sort expression is parallel-safe too.
                               3166                 :                :              */
 1090 tgl@sss.pgh.pa.us        3167         [ +  + ]:           5011 :             if (!relation_can_be_sorted_early(root, rel, pathkey_ec,
                               3168                 :                :                                               require_parallel_safe))
 1468 tomas.vondra@postgre     3169                 :           1026 :                 break;
                               3170                 :                : 
                               3171                 :           3985 :             npathkeys++;
                               3172                 :                :         }
                               3173                 :                : 
                               3174                 :                :         /*
                               3175                 :                :          * The whole query_pathkeys list matches, so append it directly, to
                               3176                 :                :          * allow comparing pathkeys easily by comparing list pointer. If we
                               3177                 :                :          * have to truncate the pathkeys, we gotta do a copy though.
                               3178                 :                :          */
                               3179         [ +  + ]:           3473 :         if (npathkeys == list_length(root->query_pathkeys))
                               3180                 :           2447 :             useful_pathkeys_list = lappend(useful_pathkeys_list,
                               3181                 :           2447 :                                            root->query_pathkeys);
                               3182         [ +  + ]:           1026 :         else if (npathkeys > 0)
                               3183                 :            230 :             useful_pathkeys_list = lappend(useful_pathkeys_list,
  641 drowley@postgresql.o     3184                 :            230 :                                            list_copy_head(root->query_pathkeys,
                               3185                 :                :                                                           npathkeys));
                               3186                 :                :     }
                               3187                 :                : 
 1468 tomas.vondra@postgre     3188                 :           8065 :     return useful_pathkeys_list;
                               3189                 :                : }
                               3190                 :                : 
                               3191                 :                : /*
                               3192                 :                :  * generate_useful_gather_paths
                               3193                 :                :  *      Generate parallel access paths for a relation by pushing a Gather or
                               3194                 :                :  *      Gather Merge on top of a partial path.
                               3195                 :                :  *
                               3196                 :                :  * Unlike plain generate_gather_paths, this looks both at pathkeys of input
                               3197                 :                :  * paths (aiming to preserve the ordering), but also considers ordering that
                               3198                 :                :  * might be useful for nodes above the gather merge node, and tries to add
                               3199                 :                :  * a sort (regular or incremental) to provide that.
                               3200                 :                :  */
                               3201                 :                : void
                               3202                 :         257481 : generate_useful_gather_paths(PlannerInfo *root, RelOptInfo *rel, bool override_rows)
                               3203                 :                : {
                               3204                 :                :     ListCell   *lc;
                               3205                 :                :     double      rows;
                               3206                 :         257481 :     double     *rowsp = NULL;
                               3207                 :         257481 :     List       *useful_pathkeys_list = NIL;
                               3208                 :         257481 :     Path       *cheapest_partial_path = NULL;
                               3209                 :                : 
                               3210                 :                :     /* If there are no partial paths, there's nothing to do here. */
                               3211         [ +  + ]:         257481 :     if (rel->partial_pathlist == NIL)
                               3212                 :         249416 :         return;
                               3213                 :                : 
                               3214                 :                :     /* Should we override the rel's rowcount estimate? */
                               3215         [ +  + ]:           8065 :     if (override_rows)
                               3216                 :            869 :         rowsp = &rows;
                               3217                 :                : 
                               3218                 :                :     /* generate the regular gather (merge) paths */
                               3219                 :           8065 :     generate_gather_paths(root, rel, override_rows);
                               3220                 :                : 
                               3221                 :                :     /* consider incremental sort for interesting orderings */
 1210                          3222                 :           8065 :     useful_pathkeys_list = get_useful_pathkeys_for_relation(root, rel, true);
                               3223                 :                : 
                               3224                 :                :     /* used for explicit (full) sort paths */
 1468                          3225                 :           8065 :     cheapest_partial_path = linitial(rel->partial_pathlist);
                               3226                 :                : 
                               3227                 :                :     /*
                               3228                 :                :      * Consider sorted paths for each interesting ordering. We generate both
                               3229                 :                :      * incremental and full sort.
                               3230                 :                :      */
                               3231   [ +  +  +  +  :          10742 :     foreach(lc, useful_pathkeys_list)
                                              +  + ]
                               3232                 :                :     {
                               3233                 :           2677 :         List       *useful_pathkeys = lfirst(lc);
                               3234                 :                :         ListCell   *lc2;
                               3235                 :                :         bool        is_sorted;
                               3236                 :                :         int         presorted_keys;
                               3237                 :                : 
                               3238   [ +  -  +  +  :           5946 :         foreach(lc2, rel->partial_pathlist)
                                              +  + ]
                               3239                 :                :         {
                               3240                 :           3269 :             Path       *subpath = (Path *) lfirst(lc2);
                               3241                 :                :             GatherMergePath *path;
                               3242                 :                : 
                               3243                 :           3269 :             is_sorted = pathkeys_count_contained_in(useful_pathkeys,
                               3244                 :                :                                                     subpath->pathkeys,
                               3245                 :                :                                                     &presorted_keys);
                               3246                 :                : 
                               3247                 :                :             /*
                               3248                 :                :              * We don't need to consider the case where a subpath is already
                               3249                 :                :              * fully sorted because generate_gather_paths already creates a
                               3250                 :                :              * gather merge path for every subpath that has pathkeys present.
                               3251                 :                :              *
                               3252                 :                :              * But since the subpath is already sorted, we know we don't need
                               3253                 :                :              * to consider adding a sort (full or incremental) on top of it,
                               3254                 :                :              * so we can continue here.
                               3255                 :                :              */
                               3256         [ +  + ]:           3269 :             if (is_sorted)
                               3257                 :            606 :                 continue;
                               3258                 :                : 
                               3259                 :                :             /*
                               3260                 :                :              * Try at least sorting the cheapest path and also try
                               3261                 :                :              * incrementally sorting any path which is partially sorted
                               3262                 :                :              * already (no need to deal with paths which have presorted keys
                               3263                 :                :              * when incremental sort is disabled unless it's the cheapest
                               3264                 :                :              * input path).
                               3265                 :                :              */
  485 drowley@postgresql.o     3266         [ +  + ]:           2663 :             if (subpath != cheapest_partial_path &&
                               3267   [ +  +  +  + ]:             99 :                 (presorted_keys == 0 || !enable_incremental_sort))
                               3268                 :             27 :                 continue;
                               3269                 :                : 
                               3270                 :                :             /*
                               3271                 :                :              * Consider regular sort for any path that's not presorted or if
                               3272                 :                :              * incremental sort is disabled.  We've no need to consider both
                               3273                 :                :              * sort and incremental sort on the same path.  We assume that
                               3274                 :                :              * incremental sort is always faster when there are presorted
                               3275                 :                :              * keys.
                               3276                 :                :              *
                               3277                 :                :              * This is not redundant with the gather paths created in
                               3278                 :                :              * generate_gather_paths, because that doesn't generate ordered
                               3279                 :                :              * output. Here we add an explicit sort to match the useful
                               3280                 :                :              * ordering.
                               3281                 :                :              */
                               3282   [ +  +  +  + ]:           2636 :             if (presorted_keys == 0 || !enable_incremental_sort)
                               3283                 :                :             {
                               3284                 :           2556 :                 subpath = (Path *) create_sort_path(root,
                               3285                 :                :                                                     rel,
                               3286                 :                :                                                     subpath,
                               3287                 :                :                                                     useful_pathkeys,
                               3288                 :                :                                                     -1.0);
                               3289                 :           2556 :                 rows = subpath->rows * subpath->parallel_workers;
                               3290                 :                :             }
                               3291                 :                :             else
                               3292                 :             80 :                 subpath = (Path *) create_incremental_sort_path(root,
                               3293                 :                :                                                                 rel,
                               3294                 :                :                                                                 subpath,
                               3295                 :                :                                                                 useful_pathkeys,
                               3296                 :                :                                                                 presorted_keys,
                               3297                 :                :                                                                 -1);
                               3298                 :           2636 :             path = create_gather_merge_path(root, rel,
                               3299                 :                :                                             subpath,
                               3300                 :           2636 :                                             rel->reltarget,
                               3301                 :                :                                             subpath->pathkeys,
                               3302                 :                :                                             NULL,
                               3303                 :                :                                             rowsp);
                               3304                 :                : 
                               3305                 :           2636 :             add_path(rel, &path->path);
                               3306                 :                :         }
                               3307                 :                :     }
                               3308                 :                : }
                               3309                 :                : 
                               3310                 :                : /*
                               3311                 :                :  * make_rel_from_joinlist
                               3312                 :                :  *    Build access paths using a "joinlist" to guide the join path search.
                               3313                 :                :  *
                               3314                 :                :  * See comments for deconstruct_jointree() for definition of the joinlist
                               3315                 :                :  * data structure.
                               3316                 :                :  */
                               3317                 :                : static RelOptInfo *
 6690 tgl@sss.pgh.pa.us        3318                 :         141162 : make_rel_from_joinlist(PlannerInfo *root, List *joinlist)
                               3319                 :                : {
                               3320                 :                :     int         levels_needed;
                               3321                 :                :     List       *initial_rels;
                               3322                 :                :     ListCell   *jl;
                               3323                 :                : 
                               3324                 :                :     /*
                               3325                 :                :      * Count the number of child joinlist nodes.  This is the depth of the
                               3326                 :                :      * dynamic-programming algorithm we must employ to consider all ways of
                               3327                 :                :      * joining the child nodes.
                               3328                 :                :      */
                               3329                 :         141162 :     levels_needed = list_length(joinlist);
                               3330                 :                : 
 8598                          3331         [ -  + ]:         141162 :     if (levels_needed <= 0)
 8598 tgl@sss.pgh.pa.us        3332                 :UBC           0 :         return NULL;            /* nothing to do? */
                               3333                 :                : 
                               3334                 :                :     /*
                               3335                 :                :      * Construct a list of rels corresponding to the child joinlist nodes.
                               3336                 :                :      * This may contain both base rels and rels constructed according to
                               3337                 :                :      * sub-joinlists.
                               3338                 :                :      */
 6690 tgl@sss.pgh.pa.us        3339                 :CBC      141162 :     initial_rels = NIL;
                               3340   [ +  -  +  +  :         338932 :     foreach(jl, joinlist)
                                              +  + ]
                               3341                 :                :     {
                               3342                 :         197770 :         Node       *jlnode = (Node *) lfirst(jl);
                               3343                 :                :         RelOptInfo *thisrel;
                               3344                 :                : 
                               3345         [ +  + ]:         197770 :         if (IsA(jlnode, RangeTblRef))
                               3346                 :                :         {
                               3347                 :         196129 :             int         varno = ((RangeTblRef *) jlnode)->rtindex;
                               3348                 :                : 
                               3349                 :         196129 :             thisrel = find_base_rel(root, varno);
                               3350                 :                :         }
                               3351         [ +  - ]:           1641 :         else if (IsA(jlnode, List))
                               3352                 :                :         {
                               3353                 :                :             /* Recurse to handle subproblem */
                               3354                 :           1641 :             thisrel = make_rel_from_joinlist(root, (List *) jlnode);
                               3355                 :                :         }
                               3356                 :                :         else
                               3357                 :                :         {
 6690 tgl@sss.pgh.pa.us        3358         [ #  # ]:UBC           0 :             elog(ERROR, "unrecognized joinlist node type: %d",
                               3359                 :                :                  (int) nodeTag(jlnode));
                               3360                 :                :             thisrel = NULL;     /* keep compiler quiet */
                               3361                 :                :         }
                               3362                 :                : 
 6690 tgl@sss.pgh.pa.us        3363                 :CBC      197770 :         initial_rels = lappend(initial_rels, thisrel);
                               3364                 :                :     }
                               3365                 :                : 
 8598                          3366         [ +  + ]:         141162 :     if (levels_needed == 1)
                               3367                 :                :     {
                               3368                 :                :         /*
                               3369                 :                :          * Single joinlist node, so we're done.
                               3370                 :                :          */
 7263 neilc@samurai.com        3371                 :         100277 :         return (RelOptInfo *) linitial(initial_rels);
                               3372                 :                :     }
                               3373                 :                :     else
                               3374                 :                :     {
                               3375                 :                :         /*
                               3376                 :                :          * Consider the different orders in which we could join the rels,
                               3377                 :                :          * using a plugin, GEQO, or the regular join search code.
                               3378                 :                :          *
                               3379                 :                :          * We put the initial_rels list into a PlannerInfo field because
                               3380                 :                :          * has_legal_joinclause() needs to look at it (ugly :-().
                               3381                 :                :          */
 5938 tgl@sss.pgh.pa.us        3382                 :          40885 :         root->initial_rels = initial_rels;
                               3383                 :                : 
 6045                          3384         [ -  + ]:          40885 :         if (join_search_hook)
 6045 tgl@sss.pgh.pa.us        3385                 :UBC           0 :             return (*join_search_hook) (root, levels_needed, initial_rels);
 6045 tgl@sss.pgh.pa.us        3386   [ +  -  +  + ]:CBC       40885 :         else if (enable_geqo && levels_needed >= geqo_threshold)
 8598                          3387                 :              3 :             return geqo(root, levels_needed, initial_rels);
                               3388                 :                :         else
 6045                          3389                 :          40882 :             return standard_join_search(root, levels_needed, initial_rels);
                               3390                 :                :     }
                               3391                 :                : }
                               3392                 :                : 
                               3393                 :                : /*
                               3394                 :                :  * standard_join_search
                               3395                 :                :  *    Find possible joinpaths for a query by successively finding ways
                               3396                 :                :  *    to join component relations into join relations.
                               3397                 :                :  *
                               3398                 :                :  * 'levels_needed' is the number of iterations needed, ie, the number of
                               3399                 :                :  *      independent jointree items in the query.  This is > 1.
                               3400                 :                :  *
                               3401                 :                :  * 'initial_rels' is a list of RelOptInfo nodes for each independent
                               3402                 :                :  *      jointree item.  These are the components to be joined together.
                               3403                 :                :  *      Note that levels_needed == list_length(initial_rels).
                               3404                 :                :  *
                               3405                 :                :  * Returns the final level of join relations, i.e., the relation that is
                               3406                 :                :  * the result of joining all the original relations together.
                               3407                 :                :  * At least one implementation path must be provided for this relation and
                               3408                 :                :  * all required sub-relations.
                               3409                 :                :  *
                               3410                 :                :  * To support loadable plugins that modify planner behavior by changing the
                               3411                 :                :  * join searching algorithm, we provide a hook variable that lets a plugin
                               3412                 :                :  * replace or supplement this function.  Any such hook must return the same
                               3413                 :                :  * final join relation as the standard code would, but it might have a
                               3414                 :                :  * different set of implementation paths attached, and only the sub-joinrels
                               3415                 :                :  * needed for these paths need have been instantiated.
                               3416                 :                :  *
                               3417                 :                :  * Note to plugin authors: the functions invoked during standard_join_search()
                               3418                 :                :  * modify root->join_rel_list and root->join_rel_hash.  If you want to do more
                               3419                 :                :  * than one join-order search, you'll probably need to save and restore the
                               3420                 :                :  * original states of those data structures.  See geqo_eval() for an example.
                               3421                 :                :  */
                               3422                 :                : RelOptInfo *
                               3423                 :          40882 : standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
                               3424                 :                : {
                               3425                 :                :     int         lev;
                               3426                 :                :     RelOptInfo *rel;
                               3427                 :                : 
                               3428                 :                :     /*
                               3429                 :                :      * This function cannot be invoked recursively within any one planning
                               3430                 :                :      * problem, so join_rel_level[] can't be in use already.
                               3431                 :                :      */
 5251                          3432         [ -  + ]:          40882 :     Assert(root->join_rel_level == NULL);
                               3433                 :                : 
                               3434                 :                :     /*
                               3435                 :                :      * We employ a simple "dynamic programming" algorithm: we first find all
                               3436                 :                :      * ways to build joins of two jointree items, then all ways to build joins
                               3437                 :                :      * of three items (from two-item joins and single items), then four-item
                               3438                 :                :      * joins, and so on until we have considered all ways to join all the
                               3439                 :                :      * items into one rel.
                               3440                 :                :      *
                               3441                 :                :      * root->join_rel_level[j] is a list of all the j-item rels.  Initially we
                               3442                 :                :      * set root->join_rel_level[1] to represent all the single-jointree-item
                               3443                 :                :      * relations.
                               3444                 :                :      */
                               3445                 :          40882 :     root->join_rel_level = (List **) palloc0((levels_needed + 1) * sizeof(List *));
                               3446                 :                : 
                               3447                 :          40882 :     root->join_rel_level[1] = initial_rels;
                               3448                 :                : 
 8833                          3449         [ +  + ]:          97476 :     for (lev = 2; lev <= levels_needed; lev++)
                               3450                 :                :     {
                               3451                 :                :         ListCell   *lc;
                               3452                 :                : 
                               3453                 :                :         /*
                               3454                 :                :          * Determine all possible pairs of relations to be joined at this
                               3455                 :                :          * level, and build paths for making each one from every available
                               3456                 :                :          * pair of lower-level relations.
                               3457                 :                :          */
 5251                          3458                 :          56595 :         join_search_one_level(root, lev);
                               3459                 :                : 
                               3460                 :                :         /*
                               3461                 :                :          * Run generate_partitionwise_join_paths() and
                               3462                 :                :          * generate_useful_gather_paths() for each just-processed joinrel.  We
                               3463                 :                :          * could not do this earlier because both regular and partial paths
                               3464                 :                :          * can get added to a particular joinrel at multiple times within
                               3465                 :                :          * join_search_one_level.
                               3466                 :                :          *
                               3467                 :                :          * After that, we're done creating paths for the joinrel, so run
                               3468                 :                :          * set_cheapest().
                               3469                 :                :          */
                               3470   [ +  +  +  +  :         143623 :         foreach(lc, root->join_rel_level[lev])
                                              +  + ]
                               3471                 :                :         {
                               3472                 :          87029 :             rel = (RelOptInfo *) lfirst(lc);
                               3473                 :                : 
                               3474                 :                :             /* Create paths for partitionwise joins. */
 2249 peter_e@gmx.net          3475                 :          87029 :             generate_partitionwise_join_paths(root, rel);
                               3476                 :                : 
                               3477                 :                :             /*
                               3478                 :                :              * Except for the topmost scan/join rel, consider gathering
                               3479                 :                :              * partial paths.  We'll do the same for the topmost scan/join rel
                               3480                 :                :              * once we know the final targetlist (see grouping_planner's and
                               3481                 :                :              * its call to apply_scanjoin_target_to_paths).
                               3482                 :                :              */
  440 tgl@sss.pgh.pa.us        3483         [ +  + ]:          87029 :             if (!bms_equal(rel->relids, root->all_query_rels))
 1468 tomas.vondra@postgre     3484                 :          46382 :                 generate_useful_gather_paths(root, rel, false);
                               3485                 :                : 
                               3486                 :                :             /* Find and save the cheapest paths for this rel */
 8825 tgl@sss.pgh.pa.us        3487                 :          87029 :             set_cheapest(rel);
                               3488                 :                : 
                               3489                 :                : #ifdef OPTIMIZER_DEBUG
                               3490                 :                :             pprint(rel);
                               3491                 :                : #endif
                               3492                 :                :         }
                               3493                 :                :     }
                               3494                 :                : 
                               3495                 :                :     /*
                               3496                 :                :      * We should have a single rel at the final level.
                               3497                 :                :      */
 5251                          3498         [ -  + ]:          40881 :     if (root->join_rel_level[levels_needed] == NIL)
 7424 tgl@sss.pgh.pa.us        3499         [ #  # ]:UBC           0 :         elog(ERROR, "failed to build any %d-way joins", levels_needed);
 5251 tgl@sss.pgh.pa.us        3500         [ -  + ]:CBC       40881 :     Assert(list_length(root->join_rel_level[levels_needed]) == 1);
                               3501                 :                : 
                               3502                 :          40881 :     rel = (RelOptInfo *) linitial(root->join_rel_level[levels_needed]);
                               3503                 :                : 
                               3504                 :          40881 :     root->join_rel_level = NULL;
                               3505                 :                : 
 8833                          3506                 :          40881 :     return rel;
                               3507                 :                : }
                               3508                 :                : 
                               3509                 :                : /*****************************************************************************
                               3510                 :                :  *          PUSHING QUALS DOWN INTO SUBQUERIES
                               3511                 :                :  *****************************************************************************/
                               3512                 :                : 
                               3513                 :                : /*
                               3514                 :                :  * subquery_is_pushdown_safe - is a subquery safe for pushing down quals?
                               3515                 :                :  *
                               3516                 :                :  * subquery is the particular component query being checked.  topquery
                               3517                 :                :  * is the top component of a set-operations tree (the same Query if no
                               3518                 :                :  * set-op is involved).
                               3519                 :                :  *
                               3520                 :                :  * Conditions checked here:
                               3521                 :                :  *
                               3522                 :                :  * 1. If the subquery has a LIMIT clause, we must not push down any quals,
                               3523                 :                :  * since that could change the set of rows returned.
                               3524                 :                :  *
                               3525                 :                :  * 2. If the subquery contains EXCEPT or EXCEPT ALL set ops we cannot push
                               3526                 :                :  * quals into it, because that could change the results.
                               3527                 :                :  *
                               3528                 :                :  * 3. If the subquery uses DISTINCT, we cannot push volatile quals into it.
                               3529                 :                :  * This is because upper-level quals should semantically be evaluated only
                               3530                 :                :  * once per distinct row, not once per original row, and if the qual is
                               3531                 :                :  * volatile then extra evaluations could change the results.  (This issue
                               3532                 :                :  * does not apply to other forms of aggregation such as GROUP BY, because
                               3533                 :                :  * when those are present we push into HAVING not WHERE, so that the quals
                               3534                 :                :  * are still applied after aggregation.)
                               3535                 :                :  *
                               3536                 :                :  * 4. If the subquery contains window functions, we cannot push volatile quals
                               3537                 :                :  * into it.  The issue here is a bit different from DISTINCT: a volatile qual
                               3538                 :                :  * might succeed for some rows of a window partition and fail for others,
                               3539                 :                :  * thereby changing the partition contents and thus the window functions'
                               3540                 :                :  * results for rows that remain.
                               3541                 :                :  *
                               3542                 :                :  * 5. If the subquery contains any set-returning functions in its targetlist,
                               3543                 :                :  * we cannot push volatile quals into it.  That would push them below the SRFs
                               3544                 :                :  * and thereby change the number of times they are evaluated.  Also, a
                               3545                 :                :  * volatile qual could succeed for some SRF output rows and fail for others,
                               3546                 :                :  * a behavior that cannot occur if it's evaluated before SRF expansion.
                               3547                 :                :  *
                               3548                 :                :  * 6. If the subquery has nonempty grouping sets, we cannot push down any
                               3549                 :                :  * quals.  The concern here is that a qual referencing a "constant" grouping
                               3550                 :                :  * column could get constant-folded, which would be improper because the value
                               3551                 :                :  * is potentially nullable by grouping-set expansion.  This restriction could
                               3552                 :                :  * be removed if we had a parsetree representation that shows that such
                               3553                 :                :  * grouping columns are not really constant.  (There are other ideas that
                               3554                 :                :  * could be used to relax this restriction, but that's the approach most
                               3555                 :                :  * likely to get taken in the future.  Note that there's not much to be gained
                               3556                 :                :  * so long as subquery_planner can't move HAVING clauses to WHERE within such
                               3557                 :                :  * a subquery.)
                               3558                 :                :  *
                               3559                 :                :  * In addition, we make several checks on the subquery's output columns to see
                               3560                 :                :  * if it is safe to reference them in pushed-down quals.  If output column k
                               3561                 :                :  * is found to be unsafe to reference, we set the reason for that inside
                               3562                 :                :  * safetyInfo->unsafeFlags[k], but we don't reject the subquery overall since
                               3563                 :                :  * column k might not be referenced by some/all quals.  The unsafeFlags[]
                               3564                 :                :  * array will be consulted later by qual_is_pushdown_safe().  It's better to
                               3565                 :                :  * do it this way than to make the checks directly in qual_is_pushdown_safe(),
                               3566                 :                :  * because when the subquery involves set operations we have to check the
                               3567                 :                :  * output expressions in each arm of the set op.
                               3568                 :                :  *
                               3569                 :                :  * Note: pushing quals into a DISTINCT subquery is theoretically dubious:
                               3570                 :                :  * we're effectively assuming that the quals cannot distinguish values that
                               3571                 :                :  * the DISTINCT's equality operator sees as equal, yet there are many
                               3572                 :                :  * counterexamples to that assumption.  However use of such a qual with a
                               3573                 :                :  * DISTINCT subquery would be unsafe anyway, since there's no guarantee which
                               3574                 :                :  * "equal" value will be chosen as the output value by the DISTINCT operation.
                               3575                 :                :  * So we don't worry too much about that.  Another objection is that if the
                               3576                 :                :  * qual is expensive to evaluate, running it for each original row might cost
                               3577                 :                :  * more than we save by eliminating rows before the DISTINCT step.  But it
                               3578                 :                :  * would be very hard to estimate that at this stage, and in practice pushdown
                               3579                 :                :  * seldom seems to make things worse, so we ignore that problem too.
                               3580                 :                :  *
                               3581                 :                :  * Note: likewise, pushing quals into a subquery with window functions is a
                               3582                 :                :  * bit dubious: the quals might remove some rows of a window partition while
                               3583                 :                :  * leaving others, causing changes in the window functions' results for the
                               3584                 :                :  * surviving rows.  We insist that such a qual reference only partitioning
                               3585                 :                :  * columns, but again that only protects us if the qual does not distinguish
                               3586                 :                :  * values that the partitioning equality operator sees as equal.  The risks
                               3587                 :                :  * here are perhaps larger than for DISTINCT, since no de-duplication of rows
                               3588                 :                :  * occurs and thus there is no theoretical problem with such a qual.  But
                               3589                 :                :  * we'll do this anyway because the potential performance benefits are very
                               3590                 :                :  * large, and we've seen no field complaints about the longstanding comparable
                               3591                 :                :  * behavior with DISTINCT.
                               3592                 :                :  */
                               3593                 :                : static bool
 7661                          3594                 :            722 : subquery_is_pushdown_safe(Query *subquery, Query *topquery,
                               3595                 :                :                           pushdown_safety_info *safetyInfo)
                               3596                 :                : {
                               3597                 :                :     SetOperationStmt *topop;
                               3598                 :                : 
                               3599                 :                :     /* Check point 1 */
 7694                          3600   [ +  +  +  + ]:            722 :     if (subquery->limitOffset != NULL || subquery->limitCount != NULL)
 7899                          3601                 :             64 :         return false;
                               3602                 :                : 
                               3603                 :                :     /* Check point 6 */
 1331                          3604   [ +  +  +  + ]:            658 :     if (subquery->groupClause && subquery->groupingSets)
                               3605                 :              6 :         return false;
                               3606                 :                : 
                               3607                 :                :     /* Check points 3, 4, and 5 */
 2756                          3608         [ +  + ]:            652 :     if (subquery->distinctClause ||
                               3609         [ +  + ]:            616 :         subquery->hasWindowFuncs ||
                               3610         [ +  + ]:            487 :         subquery->hasTargetSRFs)
 3579                          3611                 :            254 :         safetyInfo->unsafeVolatile = true;
                               3612                 :                : 
                               3613                 :                :     /*
                               3614                 :                :      * If we're at a leaf query, check for unsafe expressions in its target
                               3615                 :                :      * list, and mark any reasons why they're unsafe in unsafeFlags[].
                               3616                 :                :      * (Non-leaf nodes in setop trees have only simple Vars in their tlists,
                               3617                 :                :      * so no need to check them.)
                               3618                 :                :      */
 3966                          3619         [ +  + ]:            652 :     if (subquery->setOperations == NULL)
 3579                          3620                 :            615 :         check_output_expressions(subquery, safetyInfo);
                               3621                 :                : 
                               3622                 :                :     /* Are we at top level, or looking at a setop component? */
 7899                          3623         [ +  + ]:            652 :     if (subquery == topquery)
                               3624                 :                :     {
                               3625                 :                :         /* Top level, so check any component queries */
                               3626         [ +  + ]:            578 :         if (subquery->setOperations != NULL)
 7661                          3627         [ -  + ]:             37 :             if (!recurse_pushdown_safe(subquery->setOperations, topquery,
                               3628                 :                :                                        safetyInfo))
 7899 tgl@sss.pgh.pa.us        3629                 :UBC           0 :                 return false;
                               3630                 :                :     }
                               3631                 :                :     else
                               3632                 :                :     {
                               3633                 :                :         /* Setop component must not have more components (too weird) */
 7899 tgl@sss.pgh.pa.us        3634         [ -  + ]:CBC          74 :         if (subquery->setOperations != NULL)
 7899 tgl@sss.pgh.pa.us        3635                 :UBC           0 :             return false;
                               3636                 :                :         /* Check whether setop component output types match top level */
 2609 peter_e@gmx.net          3637                 :CBC          74 :         topop = castNode(SetOperationStmt, topquery->setOperations);
                               3638         [ -  + ]:             74 :         Assert(topop);
 7661 tgl@sss.pgh.pa.us        3639                 :             74 :         compare_tlist_datatypes(subquery->targetList,
                               3640                 :                :                                 topop->colTypes,
                               3641                 :                :                                 safetyInfo);
                               3642                 :                :     }
 7899                          3643                 :            652 :     return true;
                               3644                 :                : }
                               3645                 :                : 
                               3646                 :                : /*
                               3647                 :                :  * Helper routine to recurse through setOperations tree
                               3648                 :                :  */
                               3649                 :                : static bool
 7661                          3650                 :            111 : recurse_pushdown_safe(Node *setOp, Query *topquery,
                               3651                 :                :                       pushdown_safety_info *safetyInfo)
                               3652                 :                : {
 7899                          3653         [ +  + ]:            111 :     if (IsA(setOp, RangeTblRef))
                               3654                 :                :     {
                               3655                 :             74 :         RangeTblRef *rtr = (RangeTblRef *) setOp;
                               3656                 :             74 :         RangeTblEntry *rte = rt_fetch(rtr->rtindex, topquery->rtable);
                               3657                 :             74 :         Query      *subquery = rte->subquery;
                               3658                 :                : 
                               3659         [ -  + ]:             74 :         Assert(subquery != NULL);
 3579                          3660                 :             74 :         return subquery_is_pushdown_safe(subquery, topquery, safetyInfo);
                               3661                 :                :     }
 7899                          3662         [ +  - ]:             37 :     else if (IsA(setOp, SetOperationStmt))
                               3663                 :                :     {
                               3664                 :             37 :         SetOperationStmt *op = (SetOperationStmt *) setOp;
                               3665                 :                : 
                               3666                 :                :         /* EXCEPT is no good (point 2 for subquery_is_pushdown_safe) */
                               3667         [ -  + ]:             37 :         if (op->op == SETOP_EXCEPT)
 7899 tgl@sss.pgh.pa.us        3668                 :UBC           0 :             return false;
                               3669                 :                :         /* Else recurse */
 3579 tgl@sss.pgh.pa.us        3670         [ -  + ]:CBC          37 :         if (!recurse_pushdown_safe(op->larg, topquery, safetyInfo))
 7899 tgl@sss.pgh.pa.us        3671                 :UBC           0 :             return false;
 3579 tgl@sss.pgh.pa.us        3672         [ -  + ]:CBC          37 :         if (!recurse_pushdown_safe(op->rarg, topquery, safetyInfo))
 7899 tgl@sss.pgh.pa.us        3673                 :UBC           0 :             return false;
                               3674                 :                :     }
                               3675                 :                :     else
                               3676                 :                :     {
 7569                          3677         [ #  # ]:              0 :         elog(ERROR, "unrecognized node type: %d",
                               3678                 :                :              (int) nodeTag(setOp));
                               3679                 :                :     }
 7899 tgl@sss.pgh.pa.us        3680                 :CBC          37 :     return true;
                               3681                 :                : }
                               3682                 :                : 
                               3683                 :                : /*
                               3684                 :                :  * check_output_expressions - check subquery's output expressions for safety
                               3685                 :                :  *
                               3686                 :                :  * There are several cases in which it's unsafe to push down an upper-level
                               3687                 :                :  * qual if it references a particular output column of a subquery.  We check
                               3688                 :                :  * each output column of the subquery and set flags in unsafeFlags[k] when we
                               3689                 :                :  * see that column is unsafe for a pushed-down qual to reference.  The
                               3690                 :                :  * conditions checked here are:
                               3691                 :                :  *
                               3692                 :                :  * 1. We must not push down any quals that refer to subselect outputs that
                               3693                 :                :  * return sets, else we'd introduce functions-returning-sets into the
                               3694                 :                :  * subquery's WHERE/HAVING quals.
                               3695                 :                :  *
                               3696                 :                :  * 2. We must not push down any quals that refer to subselect outputs that
                               3697                 :                :  * contain volatile functions, for fear of introducing strange results due
                               3698                 :                :  * to multiple evaluation of a volatile function.
                               3699                 :                :  *
                               3700                 :                :  * 3. If the subquery uses DISTINCT ON, we must not push down any quals that
                               3701                 :                :  * refer to non-DISTINCT output columns, because that could change the set
                               3702                 :                :  * of rows returned.  (This condition is vacuous for DISTINCT, because then
                               3703                 :                :  * there are no non-DISTINCT output columns, so we needn't check.  Note that
                               3704                 :                :  * subquery_is_pushdown_safe already reported that we can't use volatile
                               3705                 :                :  * quals if there's DISTINCT or DISTINCT ON.)
                               3706                 :                :  *
                               3707                 :                :  * 4. If the subquery has any window functions, we must not push down quals
                               3708                 :                :  * that reference any output columns that are not listed in all the subquery's
                               3709                 :                :  * window PARTITION BY clauses.  We can push down quals that use only
                               3710                 :                :  * partitioning columns because they should succeed or fail identically for
                               3711                 :                :  * every row of any one window partition, and totally excluding some
                               3712                 :                :  * partitions will not change a window function's results for remaining
                               3713                 :                :  * partitions.  (Again, this also requires nonvolatile quals, but
                               3714                 :                :  * subquery_is_pushdown_safe handles that.).  Subquery columns marked as
                               3715                 :                :  * unsafe for this reason can still have WindowClause run conditions pushed
                               3716                 :                :  * down.
                               3717                 :                :  */
                               3718                 :                : static void
 3579                          3719                 :            615 : check_output_expressions(Query *subquery, pushdown_safety_info *safetyInfo)
                               3720                 :                : {
                               3721                 :                :     ListCell   *lc;
                               3722                 :                : 
 3966                          3723   [ +  -  +  +  :           4286 :     foreach(lc, subquery->targetList)
                                              +  + ]
                               3724                 :                :     {
                               3725                 :           3671 :         TargetEntry *tle = (TargetEntry *) lfirst(lc);
                               3726                 :                : 
                               3727         [ +  + ]:           3671 :         if (tle->resjunk)
                               3728                 :             72 :             continue;           /* ignore resjunk columns */
                               3729                 :                : 
                               3730                 :                :         /* Functions returning sets are unsafe (point 1) */
 2770                          3731         [ +  + ]:           3599 :         if (subquery->hasTargetSRFs &&
  394 drowley@postgresql.o     3732         [ +  - ]:            295 :             (safetyInfo->unsafeFlags[tle->resno] &
                               3733         [ +  + ]:            295 :              UNSAFE_HAS_SET_FUNC) == 0 &&
 2770 tgl@sss.pgh.pa.us        3734                 :            295 :             expression_returns_set((Node *) tle->expr))
                               3735                 :                :         {
  394 drowley@postgresql.o     3736                 :            164 :             safetyInfo->unsafeFlags[tle->resno] |= UNSAFE_HAS_SET_FUNC;
 3966 tgl@sss.pgh.pa.us        3737                 :            164 :             continue;
                               3738                 :                :         }
                               3739                 :                : 
                               3740                 :                :         /* Volatile functions are unsafe (point 2) */
  394 drowley@postgresql.o     3741         [ +  + ]:           3435 :         if ((safetyInfo->unsafeFlags[tle->resno] &
                               3742         [ +  + ]:           3429 :              UNSAFE_HAS_VOLATILE_FUNC) == 0 &&
                               3743                 :           3429 :             contain_volatile_functions((Node *) tle->expr))
                               3744                 :                :         {
                               3745                 :             39 :             safetyInfo->unsafeFlags[tle->resno] |= UNSAFE_HAS_VOLATILE_FUNC;
 3966 tgl@sss.pgh.pa.us        3746                 :             39 :             continue;
                               3747                 :                :         }
                               3748                 :                : 
                               3749                 :                :         /* If subquery uses DISTINCT ON, check point 3 */
                               3750         [ -  + ]:           3396 :         if (subquery->hasDistinctOn &&
  394 drowley@postgresql.o     3751         [ #  # ]:UBC           0 :             (safetyInfo->unsafeFlags[tle->resno] &
                               3752                 :              0 :              UNSAFE_NOTIN_DISTINCTON_CLAUSE) == 0 &&
 3966 tgl@sss.pgh.pa.us        3753         [ #  # ]:              0 :             !targetIsInSortList(tle, InvalidOid, subquery->distinctClause))
                               3754                 :                :         {
                               3755                 :                :             /* non-DISTINCT column, so mark it unsafe */
  394 drowley@postgresql.o     3756                 :              0 :             safetyInfo->unsafeFlags[tle->resno] |= UNSAFE_NOTIN_DISTINCTON_CLAUSE;
 3966 tgl@sss.pgh.pa.us        3757                 :              0 :             continue;
                               3758                 :                :         }
                               3759                 :                : 
                               3760                 :                :         /* If subquery uses window functions, check point 4 */
 3579 tgl@sss.pgh.pa.us        3761         [ +  + ]:CBC        3396 :         if (subquery->hasWindowFuncs &&
  394 drowley@postgresql.o     3762         [ +  - ]:            570 :             (safetyInfo->unsafeFlags[tle->resno] &
                               3763                 :           1092 :              UNSAFE_NOTIN_DISTINCTON_CLAUSE) == 0 &&
 3579 tgl@sss.pgh.pa.us        3764         [ +  + ]:            570 :             !targetIsInAllPartitionLists(tle, subquery))
                               3765                 :                :         {
                               3766                 :                :             /* not present in all PARTITION BY clauses, so mark it unsafe */
  394 drowley@postgresql.o     3767                 :            522 :             safetyInfo->unsafeFlags[tle->resno] |= UNSAFE_NOTIN_PARTITIONBY_CLAUSE;
 3579 tgl@sss.pgh.pa.us        3768                 :            522 :             continue;
                               3769                 :                :         }
                               3770                 :                :     }
 3966                          3771                 :            615 : }
                               3772                 :                : 
                               3773                 :                : /*
                               3774                 :                :  * For subqueries using UNION/UNION ALL/INTERSECT/INTERSECT ALL, we can
                               3775                 :                :  * push quals into each component query, but the quals can only reference
                               3776                 :                :  * subquery columns that suffer no type coercions in the set operation.
                               3777                 :                :  * Otherwise there are possible semantic gotchas.  So, we check the
                               3778                 :                :  * component queries to see if any of them have output types different from
                               3779                 :                :  * the top-level setop outputs.  We set the UNSAFE_TYPE_MISMATCH bit in
                               3780                 :                :  * unsafeFlags[k] if column k has different type in any component.
                               3781                 :                :  *
                               3782                 :                :  * We don't have to care about typmods here: the only allowed difference
                               3783                 :                :  * between set-op input and output typmods is input is a specific typmod
                               3784                 :                :  * and output is -1, and that does not require a coercion.
                               3785                 :                :  *
                               3786                 :                :  * tlist is a subquery tlist.
                               3787                 :                :  * colTypes is an OID list of the top-level setop's output column types.
                               3788                 :                :  * safetyInfo is the pushdown_safety_info to set unsafeFlags[] for.
                               3789                 :                :  */
                               3790                 :                : static void
 7661                          3791                 :             74 : compare_tlist_datatypes(List *tlist, List *colTypes,
                               3792                 :                :                         pushdown_safety_info *safetyInfo)
                               3793                 :                : {
                               3794                 :                :     ListCell   *l;
 7263 neilc@samurai.com        3795                 :             74 :     ListCell   *colType = list_head(colTypes);
                               3796                 :                : 
                               3797   [ +  -  +  +  :            240 :     foreach(l, tlist)
                                              +  + ]
                               3798                 :                :     {
                               3799                 :            166 :         TargetEntry *tle = (TargetEntry *) lfirst(l);
                               3800                 :                : 
 6948 tgl@sss.pgh.pa.us        3801         [ -  + ]:            166 :         if (tle->resjunk)
 7661 tgl@sss.pgh.pa.us        3802                 :UBC           0 :             continue;           /* ignore resjunk columns */
 7263 neilc@samurai.com        3803         [ -  + ]:CBC         166 :         if (colType == NULL)
 7661 tgl@sss.pgh.pa.us        3804         [ #  # ]:UBC           0 :             elog(ERROR, "wrong number of tlist entries");
 6948 tgl@sss.pgh.pa.us        3805         [ +  + ]:CBC         166 :         if (exprType((Node *) tle->expr) != lfirst_oid(colType))
  394 drowley@postgresql.o     3806                 :             14 :             safetyInfo->unsafeFlags[tle->resno] |= UNSAFE_TYPE_MISMATCH;
 1735 tgl@sss.pgh.pa.us        3807                 :            166 :         colType = lnext(colTypes, colType);
                               3808                 :                :     }
 7263 neilc@samurai.com        3809         [ -  + ]:             74 :     if (colType != NULL)
 7661 tgl@sss.pgh.pa.us        3810         [ #  # ]:UBC           0 :         elog(ERROR, "wrong number of tlist entries");
 7661 tgl@sss.pgh.pa.us        3811                 :CBC          74 : }
                               3812                 :                : 
                               3813                 :                : /*
                               3814                 :                :  * targetIsInAllPartitionLists
                               3815                 :                :  *      True if the TargetEntry is listed in the PARTITION BY clause
                               3816                 :                :  *      of every window defined in the query.
                               3817                 :                :  *
                               3818                 :                :  * It would be safe to ignore windows not actually used by any window
                               3819                 :                :  * function, but it's not easy to get that info at this stage; and it's
                               3820                 :                :  * unlikely to be useful to spend any extra cycles getting it, since
                               3821                 :                :  * unreferenced window definitions are probably infrequent in practice.
                               3822                 :                :  */
                               3823                 :                : static bool
 3579                          3824                 :            570 : targetIsInAllPartitionLists(TargetEntry *tle, Query *query)
                               3825                 :                : {
                               3826                 :                :     ListCell   *lc;
                               3827                 :                : 
                               3828   [ +  -  +  +  :            630 :     foreach(lc, query->windowClause)
                                              +  + ]
                               3829                 :                :     {
                               3830                 :            582 :         WindowClause *wc = (WindowClause *) lfirst(lc);
                               3831                 :                : 
                               3832         [ +  + ]:            582 :         if (!targetIsInSortList(tle, InvalidOid, wc->partitionClause))
                               3833                 :            522 :             return false;
                               3834                 :                :     }
                               3835                 :             48 :     return true;
                               3836                 :                : }
                               3837                 :                : 
                               3838                 :                : /*
                               3839                 :                :  * qual_is_pushdown_safe - is a particular rinfo safe to push down?
                               3840                 :                :  *
                               3841                 :                :  * rinfo is a restriction clause applying to the given subquery (whose RTE
                               3842                 :                :  * has index rti in the parent query).
                               3843                 :                :  *
                               3844                 :                :  * Conditions checked here:
                               3845                 :                :  *
                               3846                 :                :  * 1. rinfo's clause must not contain any SubPlans (mainly because it's
                               3847                 :                :  * unclear that it will work correctly: SubLinks will already have been
                               3848                 :                :  * transformed into SubPlans in the qual, but not in the subquery).  Note that
                               3849                 :                :  * SubLinks that transform to initplans are safe, and will be accepted here
                               3850                 :                :  * because what we'll see in the qual is just a Param referencing the initplan
                               3851                 :                :  * output.
                               3852                 :                :  *
                               3853                 :                :  * 2. If unsafeVolatile is set, rinfo's clause must not contain any volatile
                               3854                 :                :  * functions.
                               3855                 :                :  *
                               3856                 :                :  * 3. If unsafeLeaky is set, rinfo's clause must not contain any leaky
                               3857                 :                :  * functions that are passed Var nodes, and therefore might reveal values from
                               3858                 :                :  * the subquery as side effects.
                               3859                 :                :  *
                               3860                 :                :  * 4. rinfo's clause must not refer to the whole-row output of the subquery
                               3861                 :                :  * (since there is no easy way to name that within the subquery itself).
                               3862                 :                :  *
                               3863                 :                :  * 5. rinfo's clause must not refer to any subquery output columns that were
                               3864                 :                :  * found to be unsafe to reference by subquery_is_pushdown_safe().
                               3865                 :                :  */
                               3866                 :                : static pushdown_safe_type
 1112 drowley@postgresql.o     3867                 :            764 : qual_is_pushdown_safe(Query *subquery, Index rti, RestrictInfo *rinfo,
                               3868                 :                :                       pushdown_safety_info *safetyInfo)
                               3869                 :                : {
  394                          3870                 :            764 :     pushdown_safe_type safe = PUSHDOWN_SAFE;
 1112                          3871                 :            764 :     Node       *qual = (Node *) rinfo->clause;
                               3872                 :                :     List       *vars;
                               3873                 :                :     ListCell   *vl;
                               3874                 :                : 
                               3875                 :                :     /* Refuse subselects (point 1) */
 7694 tgl@sss.pgh.pa.us        3876         [ +  + ]:            764 :     if (contain_subplans(qual))
  394 drowley@postgresql.o     3877                 :             33 :         return PUSHDOWN_UNSAFE;
                               3878                 :                : 
                               3879                 :                :     /* Refuse volatile quals if we found they'd be unsafe (point 2) */
 3579 tgl@sss.pgh.pa.us        3880   [ +  +  +  + ]:           1035 :     if (safetyInfo->unsafeVolatile &&
 1112 drowley@postgresql.o     3881                 :            304 :         contain_volatile_functions((Node *) rinfo))
  394                          3882                 :              9 :         return PUSHDOWN_UNSAFE;
                               3883                 :                : 
                               3884                 :                :     /* Refuse leaky quals if told to (point 3) */
 3579 tgl@sss.pgh.pa.us        3885   [ +  +  +  + ]:            863 :     if (safetyInfo->unsafeLeaky &&
 3275 sfrost@snowman.net       3886                 :            141 :         contain_leaked_vars(qual))
  394 drowley@postgresql.o     3887                 :             69 :         return PUSHDOWN_UNSAFE;
                               3888                 :                : 
                               3889                 :                :     /*
                               3890                 :                :      * Examine all Vars used in clause.  Since it's a restriction clause, all
                               3891                 :                :      * such Vars must refer to subselect output columns ... unless this is
                               3892                 :                :      * part of a LATERAL subquery, in which case there could be lateral
                               3893                 :                :      * references.
                               3894                 :                :      *
                               3895                 :                :      * By omitting the relevant flags, this also gives us a cheap sanity check
                               3896                 :                :      * that no aggregates or window functions appear in the qual.  Those would
                               3897                 :                :      * be unsafe to push down, but at least for the moment we could never see
                               3898                 :                :      * any in a qual anyhow.
                               3899                 :                :      */
 2957 tgl@sss.pgh.pa.us        3900                 :            653 :     vars = pull_var_clause(qual, PVC_INCLUDE_PLACEHOLDERS);
 7694                          3901   [ +  +  +  +  :           1265 :     foreach(vl, vars)
                                              +  + ]
                               3902                 :                :     {
 7559 bruce@momjian.us         3903                 :            707 :         Var        *var = (Var *) lfirst(vl);
                               3904                 :                : 
                               3905                 :                :         /*
                               3906                 :                :          * XXX Punt if we find any PlaceHolderVars in the restriction clause.
                               3907                 :                :          * It's not clear whether a PHV could safely be pushed down, and even
                               3908                 :                :          * less clear whether such a situation could arise in any cases of
                               3909                 :                :          * practical interest anyway.  So for the moment, just refuse to push
                               3910                 :                :          * down.
                               3911                 :                :          */
 5654 tgl@sss.pgh.pa.us        3912         [ -  + ]:            707 :         if (!IsA(var, Var))
                               3913                 :                :         {
  394 drowley@postgresql.o     3914                 :UBC           0 :             safe = PUSHDOWN_UNSAFE;
 5654 tgl@sss.pgh.pa.us        3915                 :              0 :             break;
                               3916                 :                :         }
                               3917                 :                : 
                               3918                 :                :         /*
                               3919                 :                :          * Punt if we find any lateral references.  It would be safe to push
                               3920                 :                :          * these down, but we'd have to convert them into outer references,
                               3921                 :                :          * which subquery_push_qual lacks the infrastructure to do.  The case
                               3922                 :                :          * arises so seldom that it doesn't seem worth working hard on.
                               3923                 :                :          */
 1371 tgl@sss.pgh.pa.us        3924         [ +  + ]:CBC         707 :         if (var->varno != rti)
                               3925                 :                :         {
  394 drowley@postgresql.o     3926                 :              6 :             safe = PUSHDOWN_UNSAFE;
 1371 tgl@sss.pgh.pa.us        3927                 :              6 :             break;
                               3928                 :                :         }
                               3929                 :                : 
                               3930                 :                :         /* Subqueries have no system columns */
 3966                          3931         [ -  + ]:            701 :         Assert(var->varattno >= 0);
                               3932                 :                : 
                               3933                 :                :         /* Check point 4 */
 6635                          3934         [ -  + ]:            701 :         if (var->varattno == 0)
                               3935                 :                :         {
  394 drowley@postgresql.o     3936                 :UBC           0 :             safe = PUSHDOWN_UNSAFE;
 6635 tgl@sss.pgh.pa.us        3937                 :              0 :             break;
                               3938                 :                :         }
                               3939                 :                : 
                               3940                 :                :         /* Check point 5 */
  394 drowley@postgresql.o     3941         [ +  + ]:CBC         701 :         if (safetyInfo->unsafeFlags[var->varattno] != 0)
                               3942                 :                :         {
                               3943         [ +  + ]:            248 :             if (safetyInfo->unsafeFlags[var->varattno] &
                               3944                 :                :                 (UNSAFE_HAS_VOLATILE_FUNC | UNSAFE_HAS_SET_FUNC |
                               3945                 :                :                  UNSAFE_NOTIN_DISTINCTON_CLAUSE | UNSAFE_TYPE_MISMATCH))
                               3946                 :                :             {
                               3947                 :             89 :                 safe = PUSHDOWN_UNSAFE;
                               3948                 :             89 :                 break;
                               3949                 :                :             }
                               3950                 :                :             else
                               3951                 :                :             {
                               3952                 :                :                 /* UNSAFE_NOTIN_PARTITIONBY_CLAUSE is ok for run conditions */
                               3953                 :            159 :                 safe = PUSHDOWN_WINDOWCLAUSE_RUNCOND;
                               3954                 :                :                 /* don't break, we might find another Var that's unsafe */
                               3955                 :                :             }
                               3956                 :                :         }
                               3957                 :                :     }
                               3958                 :                : 
 7259 neilc@samurai.com        3959                 :            653 :     list_free(vars);
                               3960                 :                : 
 7694 tgl@sss.pgh.pa.us        3961                 :            653 :     return safe;
                               3962                 :                : }
                               3963                 :                : 
                               3964                 :                : /*
                               3965                 :                :  * subquery_push_qual - push down a qual that we have determined is safe
                               3966                 :                :  */
                               3967                 :                : static void
 6889                          3968                 :            488 : subquery_push_qual(Query *subquery, RangeTblEntry *rte, Index rti, Node *qual)
                               3969                 :                : {
 7899                          3970         [ +  + ]:            488 :     if (subquery->setOperations != NULL)
                               3971                 :                :     {
                               3972                 :                :         /* Recurse to push it separately to each component query */
 7178                          3973                 :             25 :         recurse_push_qual(subquery->setOperations, subquery,
                               3974                 :                :                           rte, rti, qual);
                               3975                 :                :     }
                               3976                 :                :     else
                               3977                 :                :     {
                               3978                 :                :         /*
                               3979                 :                :          * We need to replace Vars in the qual (which must refer to outputs of
                               3980                 :                :          * the subquery) with copies of the subquery's targetlist expressions.
                               3981                 :                :          * Note that at this point, any uplevel Vars in the qual should have
                               3982                 :                :          * been replaced with Params, so they need no work.
                               3983                 :                :          *
                               3984                 :                :          * This step also ensures that when we are pushing into a setop tree,
                               3985                 :                :          * each component query gets its own copy of the qual.
                               3986                 :                :          */
 4175                          3987                 :            463 :         qual = ReplaceVarsFromTargetList(qual, rti, 0, rte,
                               3988                 :                :                                          subquery->targetList,
                               3989                 :                :                                          REPLACEVARS_REPORT_ERROR, 0,
                               3990                 :                :                                          &subquery->hasSubLinks);
                               3991                 :                : 
                               3992                 :                :         /*
                               3993                 :                :          * Now attach the qual to the proper place: normally WHERE, but if the
                               3994                 :                :          * subquery uses grouping or aggregation, put it in HAVING (since the
                               3995                 :                :          * qual really refers to the group-result rows).
                               3996                 :                :          */
 3256 andres@anarazel.de       3997   [ +  +  +  -  :            463 :         if (subquery->hasAggs || subquery->groupClause || subquery->groupingSets || subquery->havingQual)
                                        +  -  -  + ]
 6975 tgl@sss.pgh.pa.us        3998                 :             81 :             subquery->havingQual = make_and_qual(subquery->havingQual, qual);
                               3999                 :                :         else
                               4000                 :            382 :             subquery->jointree->quals =
                               4001                 :            382 :                 make_and_qual(subquery->jointree->quals, qual);
                               4002                 :                : 
                               4003                 :                :         /*
                               4004                 :                :          * We need not change the subquery's hasAggs or hasSubLinks flags,
                               4005                 :                :          * since we can't be pushing down any aggregates that weren't there
                               4006                 :                :          * before, and we don't push down subselects at all.
                               4007                 :                :          */
                               4008                 :                :     }
 7899                          4009                 :            488 : }
                               4010                 :                : 
                               4011                 :                : /*
                               4012                 :                :  * Helper routine to recurse through setOperations tree
                               4013                 :                :  */
                               4014                 :                : static void
                               4015                 :             75 : recurse_push_qual(Node *setOp, Query *topquery,
                               4016                 :                :                   RangeTblEntry *rte, Index rti, Node *qual)
                               4017                 :                : {
                               4018         [ +  + ]:             75 :     if (IsA(setOp, RangeTblRef))
                               4019                 :                :     {
                               4020                 :             50 :         RangeTblRef *rtr = (RangeTblRef *) setOp;
 7279                          4021                 :             50 :         RangeTblEntry *subrte = rt_fetch(rtr->rtindex, topquery->rtable);
                               4022                 :             50 :         Query      *subquery = subrte->subquery;
                               4023                 :                : 
 7899                          4024         [ -  + ]:             50 :         Assert(subquery != NULL);
 6889                          4025                 :             50 :         subquery_push_qual(subquery, rte, rti, qual);
                               4026                 :                :     }
 7899                          4027         [ +  - ]:             25 :     else if (IsA(setOp, SetOperationStmt))
                               4028                 :                :     {
                               4029                 :             25 :         SetOperationStmt *op = (SetOperationStmt *) setOp;
                               4030                 :                : 
 6889                          4031                 :             25 :         recurse_push_qual(op->larg, topquery, rte, rti, qual);
                               4032                 :             25 :         recurse_push_qual(op->rarg, topquery, rte, rti, qual);
                               4033                 :                :     }
                               4034                 :                :     else
                               4035                 :                :     {
 7569 tgl@sss.pgh.pa.us        4036         [ #  # ]:UBC           0 :         elog(ERROR, "unrecognized node type: %d",
                               4037                 :                :              (int) nodeTag(setOp));
                               4038                 :                :     }
 7899 tgl@sss.pgh.pa.us        4039                 :CBC          75 : }
                               4040                 :                : 
                               4041                 :                : /*****************************************************************************
                               4042                 :                :  *          SIMPLIFYING SUBQUERY TARGETLISTS
                               4043                 :                :  *****************************************************************************/
                               4044                 :                : 
                               4045                 :                : /*
                               4046                 :                :  * remove_unused_subquery_outputs
                               4047                 :                :  *      Remove subquery targetlist items we don't need
                               4048                 :                :  *
                               4049                 :                :  * It's possible, even likely, that the upper query does not read all the
                               4050                 :                :  * output columns of the subquery.  We can remove any such outputs that are
                               4051                 :                :  * not needed by the subquery itself (e.g., as sort/group columns) and do not
                               4052                 :                :  * affect semantics otherwise (e.g., volatile functions can't be removed).
                               4053                 :                :  * This is useful not only because we might be able to remove expensive-to-
                               4054                 :                :  * compute expressions, but because deletion of output columns might allow
                               4055                 :                :  * optimizations such as join removal to occur within the subquery.
                               4056                 :                :  *
                               4057                 :                :  * extra_used_attrs can be passed as non-NULL to mark any columns (offset by
                               4058                 :                :  * FirstLowInvalidHeapAttributeNumber) that we should not remove.  This
                               4059                 :                :  * parameter is modified by the function, so callers must make a copy if they
                               4060                 :                :  * need to use the passed in Bitmapset after calling this function.
                               4061                 :                :  *
                               4062                 :                :  * To avoid affecting column numbering in the targetlist, we don't physically
                               4063                 :                :  * remove unused tlist entries, but rather replace their expressions with NULL
                               4064                 :                :  * constants.  This is implemented by modifying subquery->targetList.
                               4065                 :                :  */
                               4066                 :                : static void
  688 drowley@postgresql.o     4067                 :           3788 : remove_unused_subquery_outputs(Query *subquery, RelOptInfo *rel,
                               4068                 :                :                                Bitmapset *extra_used_attrs)
                               4069                 :                : {
                               4070                 :                :     Bitmapset  *attrs_used;
                               4071                 :                :     ListCell   *lc;
                               4072                 :                : 
                               4073                 :                :     /*
                               4074                 :                :      * Just point directly to extra_used_attrs. No need to bms_copy as none of
                               4075                 :                :      * the current callers use the Bitmapset after calling this function.
                               4076                 :                :      */
                               4077                 :           3788 :     attrs_used = extra_used_attrs;
                               4078                 :                : 
                               4079                 :                :     /*
                               4080                 :                :      * Do nothing if subquery has UNION/INTERSECT/EXCEPT: in principle we
                               4081                 :                :      * could update all the child SELECTs' tlists, but it seems not worth the
                               4082                 :                :      * trouble presently.
                               4083                 :                :      */
 3594 tgl@sss.pgh.pa.us        4084         [ +  + ]:           3788 :     if (subquery->setOperations)
                               4085                 :            524 :         return;
                               4086                 :                : 
                               4087                 :                :     /*
                               4088                 :                :      * If subquery has regular DISTINCT (not DISTINCT ON), we're wasting our
                               4089                 :                :      * time: all its output columns must be used in the distinctClause.
                               4090                 :                :      */
                               4091   [ +  +  +  + ]:           3525 :     if (subquery->distinctClause && !subquery->hasDistinctOn)
                               4092                 :            119 :         return;
                               4093                 :                : 
                               4094                 :                :     /*
                               4095                 :                :      * Collect a bitmap of all the output column numbers used by the upper
                               4096                 :                :      * query.
                               4097                 :                :      *
                               4098                 :                :      * Add all the attributes needed for joins or final output.  Note: we must
                               4099                 :                :      * look at rel's targetlist, not the attr_needed data, because attr_needed
                               4100                 :                :      * isn't computed for inheritance child rels, cf set_append_rel_size().
                               4101                 :                :      * (XXX might be worth changing that sometime.)
                               4102                 :                :      */
 2953                          4103                 :           3406 :     pull_varattnos((Node *) rel->reltarget->exprs, rel->relid, &attrs_used);
                               4104                 :                : 
                               4105                 :                :     /* Add all the attributes used by un-pushed-down restriction clauses. */
 3594                          4106   [ +  +  +  +  :           3744 :     foreach(lc, rel->baserestrictinfo)
                                              +  + ]
                               4107                 :                :     {
                               4108                 :            338 :         RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
                               4109                 :                : 
                               4110                 :            338 :         pull_varattnos((Node *) rinfo->clause, rel->relid, &attrs_used);
                               4111                 :                :     }
                               4112                 :                : 
                               4113                 :                :     /*
                               4114                 :                :      * If there's a whole-row reference to the subquery, we can't remove
                               4115                 :                :      * anything.
                               4116                 :                :      */
                               4117         [ +  + ]:           3406 :     if (bms_is_member(0 - FirstLowInvalidHeapAttributeNumber, attrs_used))
                               4118                 :            142 :         return;
                               4119                 :                : 
                               4120                 :                :     /*
                               4121                 :                :      * Run through the tlist and zap entries we don't need.  It's okay to
                               4122                 :                :      * modify the tlist items in-place because set_subquery_pathlist made a
                               4123                 :                :      * copy of the subquery.
                               4124                 :                :      */
                               4125   [ +  -  +  +  :          13856 :     foreach(lc, subquery->targetList)
                                              +  + ]
                               4126                 :                :     {
                               4127                 :          10592 :         TargetEntry *tle = (TargetEntry *) lfirst(lc);
                               4128                 :          10592 :         Node       *texpr = (Node *) tle->expr;
                               4129                 :                : 
                               4130                 :                :         /*
                               4131                 :                :          * If it has a sortgroupref number, it's used in some sort/group
                               4132                 :                :          * clause so we'd better not remove it.  Also, don't remove any
                               4133                 :                :          * resjunk columns, since their reason for being has nothing to do
                               4134                 :                :          * with anybody reading the subquery's output.  (It's likely that
                               4135                 :                :          * resjunk columns in a sub-SELECT would always have ressortgroupref
                               4136                 :                :          * set, but even if they don't, it seems imprudent to remove them.)
                               4137                 :                :          */
                               4138   [ +  +  -  + ]:          10592 :         if (tle->ressortgroupref || tle->resjunk)
                               4139                 :           1175 :             continue;
                               4140                 :                : 
                               4141                 :                :         /*
                               4142                 :                :          * If it's used by the upper query, we can't remove it.
                               4143                 :                :          */
                               4144         [ +  + ]:           9417 :         if (bms_is_member(tle->resno - FirstLowInvalidHeapAttributeNumber,
                               4145                 :                :                           attrs_used))
                               4146                 :           6341 :             continue;
                               4147                 :                : 
                               4148                 :                :         /*
                               4149                 :                :          * If it contains a set-returning function, we can't remove it since
                               4150                 :                :          * that could change the number of rows returned by the subquery.
                               4151                 :                :          */
 2770                          4152   [ +  +  +  + ]:           3542 :         if (subquery->hasTargetSRFs &&
                               4153                 :            466 :             expression_returns_set(texpr))
 3594                          4154                 :            338 :             continue;
                               4155                 :                : 
                               4156                 :                :         /*
                               4157                 :                :          * If it contains volatile functions, we daren't remove it for fear
                               4158                 :                :          * that the user is expecting their side-effects to happen.
                               4159                 :                :          */
                               4160         [ +  + ]:           2738 :         if (contain_volatile_functions(texpr))
                               4161                 :             12 :             continue;
                               4162                 :                : 
                               4163                 :                :         /*
                               4164                 :                :          * OK, we don't need it.  Replace the expression with a NULL constant.
                               4165                 :                :          * Preserve the exposed type of the expression, in case something
                               4166                 :                :          * looks at the rowtype of the subquery's result.
                               4167                 :                :          */
                               4168                 :           2726 :         tle->expr = (Expr *) makeNullConst(exprType(texpr),
                               4169                 :                :                                            exprTypmod(texpr),
                               4170                 :                :                                            exprCollation(texpr));
                               4171                 :                :     }
                               4172                 :                : }
                               4173                 :                : 
                               4174                 :                : /*
                               4175                 :                :  * create_partial_bitmap_paths
                               4176                 :                :  *    Build partial bitmap heap path for the relation
                               4177                 :                :  */
                               4178                 :                : void
 2594 rhaas@postgresql.org     4179                 :          63083 : create_partial_bitmap_paths(PlannerInfo *root, RelOptInfo *rel,
                               4180                 :                :                             Path *bitmapqual)
                               4181                 :                : {
                               4182                 :                :     int         parallel_workers;
                               4183                 :                :     double      pages_fetched;
                               4184                 :                : 
                               4185                 :                :     /* Compute heap pages for bitmap heap scan */
                               4186                 :          63083 :     pages_fetched = compute_bitmap_pages(root, rel, bitmapqual, 1.0,
                               4187                 :                :                                          NULL, NULL);
                               4188                 :                : 
 2263                          4189                 :          63083 :     parallel_workers = compute_parallel_worker(rel, pages_fetched, -1,
                               4190                 :                :                                                max_parallel_workers_per_gather);
                               4191                 :                : 
 2594                          4192         [ +  + ]:          63083 :     if (parallel_workers <= 0)
                               4193                 :          60996 :         return;
                               4194                 :                : 
                               4195                 :           2087 :     add_partial_path(rel, (Path *) create_bitmap_heap_path(root, rel,
                               4196                 :                :                                                            bitmapqual, rel->lateral_relids, 1.0, parallel_workers));
                               4197                 :                : }
                               4198                 :                : 
                               4199                 :                : /*
                               4200                 :                :  * Compute the number of parallel workers that should be used to scan a
                               4201                 :                :  * relation.  We compute the parallel workers based on the size of the heap to
                               4202                 :                :  * be scanned and the size of the index to be scanned, then choose a minimum
                               4203                 :                :  * of those.
                               4204                 :                :  *
                               4205                 :                :  * "heap_pages" is the number of pages from the table that we expect to scan, or
                               4206                 :                :  * -1 if we don't expect to scan any.
                               4207                 :                :  *
                               4208                 :                :  * "index_pages" is the number of pages from the index that we expect to scan, or
                               4209                 :                :  * -1 if we don't expect to scan any.
                               4210                 :                :  *
                               4211                 :                :  * "max_workers" is caller's limit on the number of workers.  This typically
                               4212                 :                :  * comes from a GUC.
                               4213                 :                :  */
                               4214                 :                : int
 2263                          4215                 :         318151 : compute_parallel_worker(RelOptInfo *rel, double heap_pages, double index_pages,
                               4216                 :                :                         int max_workers)
                               4217                 :                : {
 2615                          4218                 :         318151 :     int         parallel_workers = 0;
                               4219                 :                : 
                               4220                 :                :     /*
                               4221                 :                :      * If the user has set the parallel_workers reloption, use that; otherwise
                               4222                 :                :      * select a default number of workers.
                               4223                 :                :      */
 2643                          4224         [ +  + ]:         318151 :     if (rel->rel_parallel_workers != -1)
                               4225                 :            945 :         parallel_workers = rel->rel_parallel_workers;
                               4226                 :                :     else
                               4227                 :                :     {
                               4228                 :                :         /*
                               4229                 :                :          * If the number of pages being scanned is insufficient to justify a
                               4230                 :                :          * parallel scan, just return zero ... unless it's an inheritance
                               4231                 :                :          * child. In that case, we want to generate a parallel path here
                               4232                 :                :          * anyway.  It might not be worthwhile just for this relation, but
                               4233                 :                :          * when combined with all of its inheritance siblings it may well pay
                               4234                 :                :          * off.
                               4235                 :                :          */
 2588                          4236   [ +  +  +  + ]:         317206 :         if (rel->reloptkind == RELOPT_BASEREL &&
                               4237   [ +  +  +  + ]:         298237 :             ((heap_pages >= 0 && heap_pages < min_parallel_table_scan_size) ||
 2489 tgl@sss.pgh.pa.us        4238         [ +  + ]:           8635 :              (index_pages >= 0 && index_pages < min_parallel_index_scan_size)))
 2643 rhaas@postgresql.org     4239                 :         297883 :             return 0;
                               4240                 :                : 
 2588                          4241         [ +  + ]:          19323 :         if (heap_pages >= 0)
                               4242                 :                :         {
                               4243                 :                :             int         heap_parallel_threshold;
                               4244                 :          18336 :             int         heap_parallel_workers = 1;
                               4245                 :                : 
                               4246                 :                :             /*
                               4247                 :                :              * Select the number of workers based on the log of the size of
                               4248                 :                :              * the relation.  This probably needs to be a good deal more
                               4249                 :                :              * sophisticated, but we need something here for now.  Note that
                               4250                 :                :              * the upper limit of the min_parallel_table_scan_size GUC is
                               4251                 :                :              * chosen to prevent overflow here.
                               4252                 :                :              */
 2615                          4253                 :          18336 :             heap_parallel_threshold = Max(min_parallel_table_scan_size, 1);
                               4254         [ +  + ]:          20720 :             while (heap_pages >= (BlockNumber) (heap_parallel_threshold * 3))
                               4255                 :                :             {
                               4256                 :           2384 :                 heap_parallel_workers++;
                               4257                 :           2384 :                 heap_parallel_threshold *= 3;
                               4258         [ -  + ]:           2384 :                 if (heap_parallel_threshold > INT_MAX / 3)
 2615 rhaas@postgresql.org     4259                 :UBC           0 :                     break;      /* avoid overflow */
                               4260                 :                :             }
                               4261                 :                : 
 2615 rhaas@postgresql.org     4262                 :CBC       18336 :             parallel_workers = heap_parallel_workers;
                               4263                 :                :         }
                               4264                 :                : 
 2588                          4265         [ +  + ]:          19323 :         if (index_pages >= 0)
                               4266                 :                :         {
                               4267                 :           4752 :             int         index_parallel_workers = 1;
                               4268                 :                :             int         index_parallel_threshold;
                               4269                 :                : 
                               4270                 :                :             /* same calculation as for heap_pages above */
 2615                          4271                 :           4752 :             index_parallel_threshold = Max(min_parallel_index_scan_size, 1);
                               4272         [ +  + ]:           4890 :             while (index_pages >= (BlockNumber) (index_parallel_threshold * 3))
                               4273                 :                :             {
                               4274                 :            138 :                 index_parallel_workers++;
                               4275                 :            138 :                 index_parallel_threshold *= 3;
                               4276         [ -  + ]:            138 :                 if (index_parallel_threshold > INT_MAX / 3)
 2615 rhaas@postgresql.org     4277                 :UBC           0 :                     break;      /* avoid overflow */
                               4278                 :                :             }
                               4279                 :                : 
 2615 rhaas@postgresql.org     4280         [ +  + ]:CBC        4752 :             if (parallel_workers > 0)
                               4281                 :           3765 :                 parallel_workers = Min(parallel_workers, index_parallel_workers);
                               4282                 :                :             else
                               4283                 :            987 :                 parallel_workers = index_parallel_workers;
                               4284                 :                :         }
                               4285                 :                :     }
                               4286                 :                : 
                               4287                 :                :     /* In no case use more than caller supplied maximum number of workers */
 2263                          4288                 :          20268 :     parallel_workers = Min(parallel_workers, max_workers);
                               4289                 :                : 
 2643                          4290                 :          20268 :     return parallel_workers;
                               4291                 :                : }
                               4292                 :                : 
                               4293                 :                : /*
                               4294                 :                :  * generate_partitionwise_join_paths
                               4295                 :                :  *      Create paths representing partitionwise join for given partitioned
                               4296                 :                :  *      join relation.
                               4297                 :                :  *
                               4298                 :                :  * This must not be called until after we are done adding paths for all
                               4299                 :                :  * child-joins. Otherwise, add_path might delete a path to which some path
                               4300                 :                :  * generated here has a reference.
                               4301                 :                :  */
                               4302                 :                : void
 2249 peter_e@gmx.net          4303                 :          90826 : generate_partitionwise_join_paths(PlannerInfo *root, RelOptInfo *rel)
                               4304                 :                : {
 2382 rhaas@postgresql.org     4305                 :          90826 :     List       *live_children = NIL;
                               4306                 :                :     int         cnt_parts;
                               4307                 :                :     int         num_parts;
                               4308                 :                :     RelOptInfo **part_rels;
                               4309                 :                : 
                               4310                 :                :     /* Handle only join relations here. */
                               4311   [ +  +  -  + ]:          90826 :     if (!IS_JOIN_REL(rel))
 2382 rhaas@postgresql.org     4312                 :UBC           0 :         return;
                               4313                 :                : 
                               4314                 :                :     /* We've nothing to do if the relation is not partitioned. */
 2260 rhaas@postgresql.org     4315   [ +  +  +  +  :CBC       90826 :     if (!IS_PARTITIONED_REL(rel))
                                     +  +  +  -  -  
                                                 + ]
 2382                          4316                 :          89998 :         return;
                               4317                 :                : 
                               4318                 :                :     /* The relation should have consider_partitionwise_join set. */
 2053 efujita@postgresql.o     4319         [ -  + ]:            828 :     Assert(rel->consider_partitionwise_join);
                               4320                 :                : 
                               4321                 :                :     /* Guard against stack overflow due to overly deep partition hierarchy. */
 2382 rhaas@postgresql.org     4322                 :            828 :     check_stack_depth();
                               4323                 :                : 
                               4324                 :            828 :     num_parts = rel->nparts;
                               4325                 :            828 :     part_rels = rel->part_rels;
                               4326                 :                : 
                               4327                 :                :     /* Collect non-dummy child-joins. */
                               4328         [ +  + ]:           3103 :     for (cnt_parts = 0; cnt_parts < num_parts; cnt_parts++)
                               4329                 :                :     {
                               4330                 :           2275 :         RelOptInfo *child_rel = part_rels[cnt_parts];
                               4331                 :                : 
                               4332                 :                :         /* If it's been pruned entirely, it's certainly dummy. */
 1842 tgl@sss.pgh.pa.us        4333         [ +  + ]:           2275 :         if (child_rel == NULL)
                               4334                 :             26 :             continue;
                               4335                 :                : 
                               4336                 :                :         /* Make partitionwise join paths for this partitioned child-join. */
 2249 peter_e@gmx.net          4337                 :           2249 :         generate_partitionwise_join_paths(root, child_rel);
                               4338                 :                : 
                               4339                 :                :         /* If we failed to make any path for this child, we must give up. */
  497 tgl@sss.pgh.pa.us        4340         [ -  + ]:           2249 :         if (child_rel->pathlist == NIL)
                               4341                 :                :         {
                               4342                 :                :             /*
                               4343                 :                :              * Mark the parent joinrel as unpartitioned so that later
                               4344                 :                :              * functions treat it correctly.
                               4345                 :                :              */
  497 tgl@sss.pgh.pa.us        4346                 :LBC        (24) :             rel->nparts = 0;
                               4347                 :           (24) :             return;
                               4348                 :                :         }
                               4349                 :                : 
                               4350                 :                :         /* Else, identify the cheapest path for it. */
 1865 tgl@sss.pgh.pa.us        4351                 :CBC        2249 :         set_cheapest(child_rel);
                               4352                 :                : 
                               4353                 :                :         /* Dummy children need not be scanned, so ignore those. */
 2382 rhaas@postgresql.org     4354         [ -  + ]:           2249 :         if (IS_DUMMY_REL(child_rel))
 2382 rhaas@postgresql.org     4355                 :UBC           0 :             continue;
                               4356                 :                : 
                               4357                 :                : #ifdef OPTIMIZER_DEBUG
                               4358                 :                :         pprint(child_rel);
                               4359                 :                : #endif
                               4360                 :                : 
 2382 rhaas@postgresql.org     4361                 :CBC        2249 :         live_children = lappend(live_children, child_rel);
                               4362                 :                :     }
                               4363                 :                : 
                               4364                 :                :     /* If all child-joins are dummy, parent join is also dummy. */
                               4365         [ -  + ]:            828 :     if (!live_children)
                               4366                 :                :     {
 2382 rhaas@postgresql.org     4367                 :UBC           0 :         mark_dummy_rel(rel);
                               4368                 :              0 :         return;
                               4369                 :                :     }
                               4370                 :                : 
                               4371                 :                :     /* Build additional paths for this rel from child-join paths. */
 2382 rhaas@postgresql.org     4372                 :CBC         828 :     add_paths_to_append_rel(root, rel, live_children);
                               4373                 :            828 :     list_free(live_children);
                               4374                 :                : }
        

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