LCOV - differential code coverage report
Current view: top level - src/backend/optimizer/path - allpaths.c (source / functions) Coverage Total Hit UNC LBC UIC UBC GBC GIC GNC CBC EUB ECB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 94.6 % 1143 1081 2 17 41 2 12 744 47 278 48 758 28
Current Date: 2023-04-08 15:15:32 Functions: 100.0 % 50 50 50 49 1
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

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

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