LCOV - code coverage report
Current view: top level - src/backend/optimizer/path - allpaths.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 93.7 % 1741 1632
Test Date: 2026-01-26 10:56:24 Functions: 96.2 % 52 50
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 78.2 % 1311 1025

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

Generated by: LCOV version 2.3.2-1