LCOV - code coverage report
Current view: top level - src/backend/optimizer/path - allpaths.c (source / functions) Hit Total Coverage
Test: PostgreSQL Lines: 745 810 92.0 %
Date: 2017-09-29 15:12:54 Functions: 41 43 95.3 %
Legend: Lines: hit not hit

          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-2017, 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 "nodes/makefuncs.h"
      28             : #include "nodes/nodeFuncs.h"
      29             : #ifdef OPTIMIZER_DEBUG
      30             : #include "nodes/print.h"
      31             : #endif
      32             : #include "optimizer/clauses.h"
      33             : #include "optimizer/cost.h"
      34             : #include "optimizer/geqo.h"
      35             : #include "optimizer/pathnode.h"
      36             : #include "optimizer/paths.h"
      37             : #include "optimizer/plancat.h"
      38             : #include "optimizer/planner.h"
      39             : #include "optimizer/prep.h"
      40             : #include "optimizer/restrictinfo.h"
      41             : #include "optimizer/tlist.h"
      42             : #include "optimizer/var.h"
      43             : #include "parser/parse_clause.h"
      44             : #include "parser/parsetree.h"
      45             : #include "rewrite/rewriteManip.h"
      46             : #include "utils/lsyscache.h"
      47             : 
      48             : 
      49             : /* results of subquery_is_pushdown_safe */
      50             : typedef struct pushdown_safety_info
      51             : {
      52             :     bool       *unsafeColumns;  /* which output columns are unsafe to use */
      53             :     bool        unsafeVolatile; /* don't push down volatile quals */
      54             :     bool        unsafeLeaky;    /* don't push down leaky quals */
      55             : } pushdown_safety_info;
      56             : 
      57             : /* These parameters are set by GUC */
      58             : bool        enable_geqo = false;    /* just in case GUC doesn't set it */
      59             : int         geqo_threshold;
      60             : int         min_parallel_table_scan_size;
      61             : int         min_parallel_index_scan_size;
      62             : 
      63             : /* Hook for plugins to get control in set_rel_pathlist() */
      64             : set_rel_pathlist_hook_type set_rel_pathlist_hook = NULL;
      65             : 
      66             : /* Hook for plugins to replace standard_join_search() */
      67             : join_search_hook_type join_search_hook = NULL;
      68             : 
      69             : 
      70             : static void set_base_rel_consider_startup(PlannerInfo *root);
      71             : static void set_base_rel_sizes(PlannerInfo *root);
      72             : static void set_base_rel_pathlists(PlannerInfo *root);
      73             : static void set_rel_size(PlannerInfo *root, RelOptInfo *rel,
      74             :              Index rti, RangeTblEntry *rte);
      75             : static void set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
      76             :                  Index rti, RangeTblEntry *rte);
      77             : static void set_plain_rel_size(PlannerInfo *root, RelOptInfo *rel,
      78             :                    RangeTblEntry *rte);
      79             : static void create_plain_partial_paths(PlannerInfo *root, RelOptInfo *rel);
      80             : static void set_rel_consider_parallel(PlannerInfo *root, RelOptInfo *rel,
      81             :                           RangeTblEntry *rte);
      82             : static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
      83             :                        RangeTblEntry *rte);
      84             : static void set_tablesample_rel_size(PlannerInfo *root, RelOptInfo *rel,
      85             :                          RangeTblEntry *rte);
      86             : static void set_tablesample_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
      87             :                              RangeTblEntry *rte);
      88             : static void set_foreign_size(PlannerInfo *root, RelOptInfo *rel,
      89             :                  RangeTblEntry *rte);
      90             : static void set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel,
      91             :                      RangeTblEntry *rte);
      92             : static void set_append_rel_size(PlannerInfo *root, RelOptInfo *rel,
      93             :                     Index rti, RangeTblEntry *rte);
      94             : static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
      95             :                         Index rti, RangeTblEntry *rte);
      96             : static void generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel,
      97             :                            List *live_childrels,
      98             :                            List *all_child_pathkeys,
      99             :                            List *partitioned_rels);
     100             : static Path *get_cheapest_parameterized_child_path(PlannerInfo *root,
     101             :                                       RelOptInfo *rel,
     102             :                                       Relids required_outer);
     103             : static List *accumulate_append_subpath(List *subpaths, Path *path);
     104             : static void set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
     105             :                       Index rti, RangeTblEntry *rte);
     106             : static void set_function_pathlist(PlannerInfo *root, RelOptInfo *rel,
     107             :                       RangeTblEntry *rte);
     108             : static void set_values_pathlist(PlannerInfo *root, RelOptInfo *rel,
     109             :                     RangeTblEntry *rte);
     110             : static void set_tablefunc_pathlist(PlannerInfo *root, RelOptInfo *rel,
     111             :                        RangeTblEntry *rte);
     112             : static void set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel,
     113             :                  RangeTblEntry *rte);
     114             : static void set_namedtuplestore_pathlist(PlannerInfo *root, RelOptInfo *rel,
     115             :                              RangeTblEntry *rte);
     116             : static void set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel,
     117             :                        RangeTblEntry *rte);
     118             : static RelOptInfo *make_rel_from_joinlist(PlannerInfo *root, List *joinlist);
     119             : static bool subquery_is_pushdown_safe(Query *subquery, Query *topquery,
     120             :                           pushdown_safety_info *safetyInfo);
     121             : static bool recurse_pushdown_safe(Node *setOp, Query *topquery,
     122             :                       pushdown_safety_info *safetyInfo);
     123             : static void check_output_expressions(Query *subquery,
     124             :                          pushdown_safety_info *safetyInfo);
     125             : static void compare_tlist_datatypes(List *tlist, List *colTypes,
     126             :                         pushdown_safety_info *safetyInfo);
     127             : static bool targetIsInAllPartitionLists(TargetEntry *tle, Query *query);
     128             : static bool qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
     129             :                       pushdown_safety_info *safetyInfo);
     130             : static void subquery_push_qual(Query *subquery,
     131             :                    RangeTblEntry *rte, Index rti, Node *qual);
     132             : static void recurse_push_qual(Node *setOp, Query *topquery,
     133             :                   RangeTblEntry *rte, Index rti, Node *qual);
     134             : static void remove_unused_subquery_outputs(Query *subquery, RelOptInfo *rel);
     135             : static void add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel,
     136             :                         List *live_childrels);
     137             : 
     138             : 
     139             : /*
     140             :  * make_one_rel
     141             :  *    Finds all possible access paths for executing a query, returning a
     142             :  *    single rel that represents the join of all base rels in the query.
     143             :  */
     144             : RelOptInfo *
     145       13818 : make_one_rel(PlannerInfo *root, List *joinlist)
     146             : {
     147             :     RelOptInfo *rel;
     148             :     Index       rti;
     149             : 
     150             :     /*
     151             :      * Construct the all_baserels Relids set.
     152             :      */
     153       13818 :     root->all_baserels = NULL;
     154       40855 :     for (rti = 1; rti < root->simple_rel_array_size; rti++)
     155             :     {
     156       27037 :         RelOptInfo *brel = root->simple_rel_array[rti];
     157             : 
     158             :         /* there may be empty slots corresponding to non-baserel RTEs */
     159       27037 :         if (brel == NULL)
     160        7514 :             continue;
     161             : 
     162       19523 :         Assert(brel->relid == rti); /* sanity check on array */
     163             : 
     164             :         /* ignore RTEs that are "other rels" */
     165       19523 :         if (brel->reloptkind != RELOPT_BASEREL)
     166        1983 :             continue;
     167             : 
     168       17540 :         root->all_baserels = bms_add_member(root->all_baserels, brel->relid);
     169             :     }
     170             : 
     171             :     /* Mark base rels as to whether we care about fast-start plans */
     172       13818 :     set_base_rel_consider_startup(root);
     173             : 
     174             :     /*
     175             :      * Compute size estimates and consider_parallel flags for each base rel,
     176             :      * then generate access paths.
     177             :      */
     178       13818 :     set_base_rel_sizes(root);
     179       13818 :     set_base_rel_pathlists(root);
     180             : 
     181             :     /*
     182             :      * Generate access paths for the entire join tree.
     183             :      */
     184       13818 :     rel = make_rel_from_joinlist(root, joinlist);
     185             : 
     186             :     /*
     187             :      * The result should join all and only the query's base rels.
     188             :      */
     189       13818 :     Assert(bms_equal(rel->relids, root->all_baserels));
     190             : 
     191       13818 :     return rel;
     192             : }
     193             : 
     194             : /*
     195             :  * set_base_rel_consider_startup
     196             :  *    Set the consider_[param_]startup flags for each base-relation entry.
     197             :  *
     198             :  * For the moment, we only deal with consider_param_startup here; because the
     199             :  * logic for consider_startup is pretty trivial and is the same for every base
     200             :  * relation, we just let build_simple_rel() initialize that flag correctly to
     201             :  * start with.  If that logic ever gets more complicated it would probably
     202             :  * be better to move it here.
     203             :  */
     204             : static void
     205       13818 : set_base_rel_consider_startup(PlannerInfo *root)
     206             : {
     207             :     /*
     208             :      * Since parameterized paths can only be used on the inside of a nestloop
     209             :      * join plan, there is usually little value in considering fast-start
     210             :      * plans for them.  However, for relations that are on the RHS of a SEMI
     211             :      * or ANTI join, a fast-start plan can be useful because we're only going
     212             :      * to care about fetching one tuple anyway.
     213             :      *
     214             :      * To minimize growth of planning time, we currently restrict this to
     215             :      * cases where the RHS is a single base relation, not a join; there is no
     216             :      * provision for consider_param_startup to get set at all on joinrels.
     217             :      * Also we don't worry about appendrels.  costsize.c's costing rules for
     218             :      * nestloop semi/antijoins don't consider such cases either.
     219             :      */
     220             :     ListCell   *lc;
     221             : 
     222       14913 :     foreach(lc, root->join_info_list)
     223             :     {
     224        1095 :         SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
     225             :         int         varno;
     226             : 
     227        1384 :         if ((sjinfo->jointype == JOIN_SEMI || sjinfo->jointype == JOIN_ANTI) &&
     228         289 :             bms_get_singleton_member(sjinfo->syn_righthand, &varno))
     229             :         {
     230         276 :             RelOptInfo *rel = find_base_rel(root, varno);
     231             : 
     232         276 :             rel->consider_param_startup = true;
     233             :         }
     234             :     }
     235       13818 : }
     236             : 
     237             : /*
     238             :  * set_base_rel_sizes
     239             :  *    Set the size estimates (rows and widths) for each base-relation entry.
     240             :  *    Also determine whether to consider parallel paths for base relations.
     241             :  *
     242             :  * We do this in a separate pass over the base rels so that rowcount
     243             :  * estimates are available for parameterized path generation, and also so
     244             :  * that each rel's consider_parallel flag is set correctly before we begin to
     245             :  * generate paths.
     246             :  */
     247             : static void
     248       13818 : set_base_rel_sizes(PlannerInfo *root)
     249             : {
     250             :     Index       rti;
     251             : 
     252       40855 :     for (rti = 1; rti < root->simple_rel_array_size; rti++)
     253             :     {
     254       27037 :         RelOptInfo *rel = root->simple_rel_array[rti];
     255             :         RangeTblEntry *rte;
     256             : 
     257             :         /* there may be empty slots corresponding to non-baserel RTEs */
     258       27037 :         if (rel == NULL)
     259        7514 :             continue;
     260             : 
     261       19523 :         Assert(rel->relid == rti);   /* sanity check on array */
     262             : 
     263             :         /* ignore RTEs that are "other rels" */
     264       19523 :         if (rel->reloptkind != RELOPT_BASEREL)
     265        1983 :             continue;
     266             : 
     267       17540 :         rte = root->simple_rte_array[rti];
     268             : 
     269             :         /*
     270             :          * If parallelism is allowable for this query in general, see whether
     271             :          * it's allowable for this rel in particular.  We have to do this
     272             :          * before set_rel_size(), because (a) if this rel is an inheritance
     273             :          * parent, set_append_rel_size() will use and perhaps change the rel's
     274             :          * consider_parallel flag, and (b) for some RTE types, set_rel_size()
     275             :          * goes ahead and makes paths immediately.
     276             :          */
     277       17540 :         if (root->glob->parallelModeOK)
     278       13944 :             set_rel_consider_parallel(root, rel, rte);
     279             : 
     280       17540 :         set_rel_size(root, rel, rti, rte);
     281             :     }
     282       13818 : }
     283             : 
     284             : /*
     285             :  * set_base_rel_pathlists
     286             :  *    Finds all paths available for scanning each base-relation entry.
     287             :  *    Sequential scan and any available indices are considered.
     288             :  *    Each useful path is attached to its relation's 'pathlist' field.
     289             :  */
     290             : static void
     291       13818 : set_base_rel_pathlists(PlannerInfo *root)
     292             : {
     293             :     Index       rti;
     294             : 
     295       40855 :     for (rti = 1; rti < root->simple_rel_array_size; rti++)
     296             :     {
     297       27037 :         RelOptInfo *rel = root->simple_rel_array[rti];
     298             : 
     299             :         /* there may be empty slots corresponding to non-baserel RTEs */
     300       27037 :         if (rel == NULL)
     301        7514 :             continue;
     302             : 
     303       19523 :         Assert(rel->relid == rti);   /* sanity check on array */
     304             : 
     305             :         /* ignore RTEs that are "other rels" */
     306       19523 :         if (rel->reloptkind != RELOPT_BASEREL)
     307        1983 :             continue;
     308             : 
     309       17540 :         set_rel_pathlist(root, rel, rti, root->simple_rte_array[rti]);
     310             :     }
     311       13818 : }
     312             : 
     313             : /*
     314             :  * set_rel_size
     315             :  *    Set size estimates for a base relation
     316             :  */
     317             : static void
     318       19014 : set_rel_size(PlannerInfo *root, RelOptInfo *rel,
     319             :              Index rti, RangeTblEntry *rte)
     320             : {
     321       36554 :     if (rel->reloptkind == RELOPT_BASEREL &&
     322       17540 :         relation_excluded_by_constraints(root, rel, rte))
     323             :     {
     324             :         /*
     325             :          * We proved we don't need to scan the rel via constraint exclusion,
     326             :          * so set up a single dummy path for it.  Here we only check this for
     327             :          * regular baserels; if it's an otherrel, CE was already checked in
     328             :          * set_append_rel_size().
     329             :          *
     330             :          * In this case, we go ahead and set up the relation's path right away
     331             :          * instead of leaving it for set_rel_pathlist to do.  This is because
     332             :          * we don't have a convention for marking a rel as dummy except by
     333             :          * assigning a dummy path to it.
     334             :          */
     335          31 :         set_dummy_rel_pathlist(rel);
     336             :     }
     337       18983 :     else if (rte->inh)
     338             :     {
     339             :         /* It's an "append relation", process accordingly */
     340         589 :         set_append_rel_size(root, rel, rti, rte);
     341             :     }
     342             :     else
     343             :     {
     344       18394 :         switch (rel->rtekind)
     345             :         {
     346             :             case RTE_RELATION:
     347       15547 :                 if (rte->relkind == RELKIND_FOREIGN_TABLE)
     348             :                 {
     349             :                     /* Foreign table */
     350           0 :                     set_foreign_size(root, rel, rte);
     351             :                 }
     352       15547 :                 else if (rte->relkind == RELKIND_PARTITIONED_TABLE)
     353             :                 {
     354             :                     /*
     355             :                      * A partitioned table without leaf partitions is marked
     356             :                      * as a dummy rel.
     357             :                      */
     358           0 :                     set_dummy_rel_pathlist(rel);
     359             :                 }
     360       15547 :                 else if (rte->tablesample != NULL)
     361             :                 {
     362             :                     /* Sampled relation */
     363          36 :                     set_tablesample_rel_size(root, rel, rte);
     364             :                 }
     365             :                 else
     366             :                 {
     367             :                     /* Plain relation */
     368       15511 :                     set_plain_rel_size(root, rel, rte);
     369             :                 }
     370       15547 :                 break;
     371             :             case RTE_SUBQUERY:
     372             : 
     373             :                 /*
     374             :                  * Subqueries don't support making a choice between
     375             :                  * parameterized and unparameterized paths, so just go ahead
     376             :                  * and build their paths immediately.
     377             :                  */
     378         790 :                 set_subquery_pathlist(root, rel, rti, rte);
     379         790 :                 break;
     380             :             case RTE_FUNCTION:
     381        1327 :                 set_function_size_estimates(root, rel);
     382        1327 :                 break;
     383             :             case RTE_TABLEFUNC:
     384          22 :                 set_tablefunc_size_estimates(root, rel);
     385          22 :                 break;
     386             :             case RTE_VALUES:
     387         463 :                 set_values_size_estimates(root, rel);
     388         463 :                 break;
     389             :             case RTE_CTE:
     390             : 
     391             :                 /*
     392             :                  * CTEs don't support making a choice between parameterized
     393             :                  * and unparameterized paths, so just go ahead and build their
     394             :                  * paths immediately.
     395             :                  */
     396         202 :                 if (rte->self_reference)
     397          40 :                     set_worktable_pathlist(root, rel, rte);
     398             :                 else
     399         162 :                     set_cte_pathlist(root, rel, rte);
     400         202 :                 break;
     401             :             case RTE_NAMEDTUPLESTORE:
     402          43 :                 set_namedtuplestore_pathlist(root, rel, rte);
     403          43 :                 break;
     404             :             default:
     405           0 :                 elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
     406             :                 break;
     407             :         }
     408             :     }
     409             : 
     410             :     /*
     411             :      * We insist that all non-dummy rels have a nonzero rowcount estimate.
     412             :      */
     413       19014 :     Assert(rel->rows > 0 || IS_DUMMY_REL(rel));
     414       19014 : }
     415             : 
     416             : /*
     417             :  * set_rel_pathlist
     418             :  *    Build access paths for a base relation
     419             :  */
     420             : static void
     421       19087 : set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
     422             :                  Index rti, RangeTblEntry *rte)
     423             : {
     424       19087 :     if (IS_DUMMY_REL(rel))
     425             :     {
     426             :         /* We already proved the relation empty, so nothing more to do */
     427             :     }
     428       18975 :     else if (rte->inh)
     429             :     {
     430             :         /* It's an "append relation", process accordingly */
     431         584 :         set_append_rel_pathlist(root, rel, rti, rte);
     432             :     }
     433             :     else
     434             :     {
     435       18391 :         switch (rel->rtekind)
     436             :         {
     437             :             case RTE_RELATION:
     438       15547 :                 if (rte->relkind == RELKIND_FOREIGN_TABLE)
     439             :                 {
     440             :                     /* Foreign table */
     441           0 :                     set_foreign_pathlist(root, rel, rte);
     442             :                 }
     443       15547 :                 else if (rte->tablesample != NULL)
     444             :                 {
     445             :                     /* Sampled relation */
     446          36 :                     set_tablesample_rel_pathlist(root, rel, rte);
     447             :                 }
     448             :                 else
     449             :                 {
     450             :                     /* Plain relation */
     451       15511 :                     set_plain_rel_pathlist(root, rel, rte);
     452             :                 }
     453       15547 :                 break;
     454             :             case RTE_SUBQUERY:
     455             :                 /* Subquery --- fully handled during set_rel_size */
     456         787 :                 break;
     457             :             case RTE_FUNCTION:
     458             :                 /* RangeFunction */
     459        1327 :                 set_function_pathlist(root, rel, rte);
     460        1327 :                 break;
     461             :             case RTE_TABLEFUNC:
     462             :                 /* Table Function */
     463          22 :                 set_tablefunc_pathlist(root, rel, rte);
     464          22 :                 break;
     465             :             case RTE_VALUES:
     466             :                 /* Values list */
     467         463 :                 set_values_pathlist(root, rel, rte);
     468         463 :                 break;
     469             :             case RTE_CTE:
     470             :                 /* CTE reference --- fully handled during set_rel_size */
     471         202 :                 break;
     472             :             case RTE_NAMEDTUPLESTORE:
     473             :                 /* tuplestore reference --- fully handled during set_rel_size */
     474          43 :                 break;
     475             :             default:
     476           0 :                 elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
     477             :                 break;
     478             :         }
     479             :     }
     480             : 
     481             :     /*
     482             :      * If this is a baserel, consider gathering any partial paths we may have
     483             :      * created for it.  (If we tried to gather inheritance children, we could
     484             :      * end up with a very large number of gather nodes, each trying to grab
     485             :      * its own pool of workers, so don't do this for otherrels.  Instead,
     486             :      * we'll consider gathering partial paths for the parent appendrel.)
     487             :      */
     488       19087 :     if (rel->reloptkind == RELOPT_BASEREL)
     489       17540 :         generate_gather_paths(root, rel);
     490             : 
     491             :     /*
     492             :      * Allow a plugin to editorialize on the set of Paths for this base
     493             :      * relation.  It could add new paths (such as CustomPaths) by calling
     494             :      * add_path(), or delete or modify paths added by the core code.
     495             :      */
     496       19087 :     if (set_rel_pathlist_hook)
     497           0 :         (*set_rel_pathlist_hook) (root, rel, rti, rte);
     498             : 
     499             :     /* Now find the cheapest of the paths for this rel */
     500       19087 :     set_cheapest(rel);
     501             : 
     502             : #ifdef OPTIMIZER_DEBUG
     503             :     debug_print_rel(root, rel);
     504             : #endif
     505       19087 : }
     506             : 
     507             : /*
     508             :  * set_plain_rel_size
     509             :  *    Set size estimates for a plain relation (no subquery, no inheritance)
     510             :  */
     511             : static void
     512       15511 : set_plain_rel_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
     513             : {
     514             :     /*
     515             :      * Test any partial indexes of rel for applicability.  We must do this
     516             :      * first since partial unique indexes can affect size estimates.
     517             :      */
     518       15511 :     check_index_predicates(root, rel);
     519             : 
     520             :     /* Mark rel with estimated output rows, width, etc */
     521       15511 :     set_baserel_size_estimates(root, rel);
     522       15511 : }
     523             : 
     524             : /*
     525             :  * If this relation could possibly be scanned from within a worker, then set
     526             :  * its consider_parallel flag.
     527             :  */
     528             : static void
     529       14992 : set_rel_consider_parallel(PlannerInfo *root, RelOptInfo *rel,
     530             :                           RangeTblEntry *rte)
     531             : {
     532             :     /*
     533             :      * The flag has previously been initialized to false, so we can just
     534             :      * return if it becomes clear that we can't safely set it.
     535             :      */
     536       14992 :     Assert(!rel->consider_parallel);
     537             : 
     538             :     /* Don't call this if parallelism is disallowed for the entire query. */
     539       14992 :     Assert(root->glob->parallelModeOK);
     540             : 
     541             :     /* This should only be called for baserels and appendrel children. */
     542       14992 :     Assert(IS_SIMPLE_REL(rel));
     543             : 
     544             :     /* Assorted checks based on rtekind. */
     545       14992 :     switch (rte->rtekind)
     546             :     {
     547             :         case RTE_RELATION:
     548             : 
     549             :             /*
     550             :              * Currently, parallel workers can't access the leader's temporary
     551             :              * tables.  We could possibly relax this if the wrote all of its
     552             :              * local buffers at the start of the query and made no changes
     553             :              * thereafter (maybe we could allow hint bit changes), and if we
     554             :              * taught the workers to read them.  Writing a large number of
     555             :              * temporary buffers could be expensive, though, and we don't have
     556             :              * the rest of the necessary infrastructure right now anyway.  So
     557             :              * for now, bail out if we see a temporary table.
     558             :              */
     559       12797 :             if (get_rel_persistence(rte->relid) == RELPERSISTENCE_TEMP)
     560         667 :                 return;
     561             : 
     562             :             /*
     563             :              * Table sampling can be pushed down to workers if the sample
     564             :              * function and its arguments are safe.
     565             :              */
     566       12130 :             if (rte->tablesample != NULL)
     567             :             {
     568          36 :                 char        proparallel = func_parallel(rte->tablesample->tsmhandler);
     569             : 
     570          36 :                 if (proparallel != PROPARALLEL_SAFE)
     571           0 :                     return;
     572          36 :                 if (!is_parallel_safe(root, (Node *) rte->tablesample->args))
     573           2 :                     return;
     574             :             }
     575             : 
     576             :             /*
     577             :              * Ask FDWs whether they can support performing a ForeignScan
     578             :              * within a worker.  Most often, the answer will be no.  For
     579             :              * example, if the nature of the FDW is such that it opens a TCP
     580             :              * connection with a remote server, each parallel worker would end
     581             :              * up with a separate connection, and these connections might not
     582             :              * be appropriately coordinated between workers and the leader.
     583             :              */
     584       12128 :             if (rte->relkind == RELKIND_FOREIGN_TABLE)
     585             :             {
     586           0 :                 Assert(rel->fdwroutine);
     587           0 :                 if (!rel->fdwroutine->IsForeignScanParallelSafe)
     588           0 :                     return;
     589           0 :                 if (!rel->fdwroutine->IsForeignScanParallelSafe(root, rel, rte))
     590           0 :                     return;
     591             :             }
     592             : 
     593             :             /*
     594             :              * There are additional considerations for appendrels, which we'll
     595             :              * deal with in set_append_rel_size and set_append_rel_pathlist.
     596             :              * For now, just set consider_parallel based on the rel's own
     597             :              * quals and targetlist.
     598             :              */
     599       12128 :             break;
     600             : 
     601             :         case RTE_SUBQUERY:
     602             : 
     603             :             /*
     604             :              * There's no intrinsic problem with scanning a subquery-in-FROM
     605             :              * (as distinct from a SubPlan or InitPlan) in a parallel worker.
     606             :              * If the subquery doesn't happen to have any parallel-safe paths,
     607             :              * then flagging it as consider_parallel won't change anything,
     608             :              * but that's true for plain tables, too.  We must set
     609             :              * consider_parallel based on the rel's own quals and targetlist,
     610             :              * so that if a subquery path is parallel-safe but the quals and
     611             :              * projection we're sticking onto it are not, we correctly mark
     612             :              * the SubqueryScanPath as not parallel-safe.  (Note that
     613             :              * set_subquery_pathlist() might push some of these quals down
     614             :              * into the subquery itself, but that doesn't change anything.)
     615             :              */
     616         874 :             break;
     617             : 
     618             :         case RTE_JOIN:
     619             :             /* Shouldn't happen; we're only considering baserels here. */
     620           0 :             Assert(false);
     621             :             return;
     622             : 
     623             :         case RTE_FUNCTION:
     624             :             /* Check for parallel-restricted functions. */
     625         896 :             if (!is_parallel_safe(root, (Node *) rte->functions))
     626         451 :                 return;
     627         445 :             break;
     628             : 
     629             :         case RTE_TABLEFUNC:
     630             :             /* not parallel safe */
     631          22 :             return;
     632             : 
     633             :         case RTE_VALUES:
     634             :             /* Check for parallel-restricted functions. */
     635         236 :             if (!is_parallel_safe(root, (Node *) rte->values_lists))
     636           5 :                 return;
     637         231 :             break;
     638             : 
     639             :         case RTE_CTE:
     640             : 
     641             :             /*
     642             :              * CTE tuplestores aren't shared among parallel workers, so we
     643             :              * force all CTE scans to happen in the leader.  Also, populating
     644             :              * the CTE would require executing a subplan that's not available
     645             :              * in the worker, might be parallel-restricted, and must get
     646             :              * executed only once.
     647             :              */
     648         125 :             return;
     649             : 
     650             :         case RTE_NAMEDTUPLESTORE:
     651             : 
     652             :             /*
     653             :              * tuplestore cannot be shared, at least without more
     654             :              * infrastructure to support that.
     655             :              */
     656          42 :             return;
     657             :     }
     658             : 
     659             :     /*
     660             :      * If there's anything in baserestrictinfo that's parallel-restricted, we
     661             :      * give up on parallelizing access to this relation.  We could consider
     662             :      * instead postponing application of the restricted quals until we're
     663             :      * above all the parallelism in the plan tree, but it's not clear that
     664             :      * that would be a win in very many cases, and it might be tricky to make
     665             :      * outer join clauses work correctly.  It would likely break equivalence
     666             :      * classes, too.
     667             :      */
     668       13678 :     if (!is_parallel_safe(root, (Node *) rel->baserestrictinfo))
     669        1655 :         return;
     670             : 
     671             :     /*
     672             :      * Likewise, if the relation's outputs are not parallel-safe, give up.
     673             :      * (Usually, they're just Vars, but sometimes they're not.)
     674             :      */
     675       12023 :     if (!is_parallel_safe(root, (Node *) rel->reltarget->exprs))
     676           2 :         return;
     677             : 
     678             :     /* We have a winner. */
     679       12021 :     rel->consider_parallel = true;
     680             : }
     681             : 
     682             : /*
     683             :  * set_plain_rel_pathlist
     684             :  *    Build access paths for a plain relation (no subquery, no inheritance)
     685             :  */
     686             : static void
     687       15511 : set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
     688             : {
     689             :     Relids      required_outer;
     690             : 
     691             :     /*
     692             :      * We don't support pushing join clauses into the quals of a seqscan, but
     693             :      * it could still have required parameterization due to LATERAL refs in
     694             :      * its tlist.
     695             :      */
     696       15511 :     required_outer = rel->lateral_relids;
     697             : 
     698             :     /* Consider sequential scan */
     699       15511 :     add_path(rel, create_seqscan_path(root, rel, required_outer, 0));
     700             : 
     701             :     /* If appropriate, consider parallel sequential scan */
     702       15511 :     if (rel->consider_parallel && required_outer == NULL)
     703       10254 :         create_plain_partial_paths(root, rel);
     704             : 
     705             :     /* Consider index scans */
     706       15511 :     create_index_paths(root, rel);
     707             : 
     708             :     /* Consider TID scans */
     709       15511 :     create_tidscan_paths(root, rel);
     710       15511 : }
     711             : 
     712             : /*
     713             :  * create_plain_partial_paths
     714             :  *    Build partial access paths for parallel scan of a plain relation
     715             :  */
     716             : static void
     717       10254 : create_plain_partial_paths(PlannerInfo *root, RelOptInfo *rel)
     718             : {
     719             :     int         parallel_workers;
     720             : 
     721       10254 :     parallel_workers = compute_parallel_worker(rel, rel->pages, -1);
     722             : 
     723             :     /* If any limit was set to zero, the user doesn't want a parallel scan. */
     724       10254 :     if (parallel_workers <= 0)
     725       19854 :         return;
     726             : 
     727             :     /* Add an unordered partial path based on a parallel sequential scan. */
     728         654 :     add_partial_path(rel, create_seqscan_path(root, rel, NULL, parallel_workers));
     729             : }
     730             : 
     731             : /*
     732             :  * set_tablesample_rel_size
     733             :  *    Set size estimates for a sampled relation
     734             :  */
     735             : static void
     736          36 : set_tablesample_rel_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
     737             : {
     738          36 :     TableSampleClause *tsc = rte->tablesample;
     739             :     TsmRoutine *tsm;
     740             :     BlockNumber pages;
     741             :     double      tuples;
     742             : 
     743             :     /*
     744             :      * Test any partial indexes of rel for applicability.  We must do this
     745             :      * first since partial unique indexes can affect size estimates.
     746             :      */
     747          36 :     check_index_predicates(root, rel);
     748             : 
     749             :     /*
     750             :      * Call the sampling method's estimation function to estimate the number
     751             :      * of pages it will read and the number of tuples it will return.  (Note:
     752             :      * we assume the function returns sane values.)
     753             :      */
     754          36 :     tsm = GetTsmRoutine(tsc->tsmhandler);
     755          36 :     tsm->SampleScanGetSampleSize(root, rel, tsc->args,
     756             :                                  &pages, &tuples);
     757             : 
     758             :     /*
     759             :      * For the moment, because we will only consider a SampleScan path for the
     760             :      * rel, it's okay to just overwrite the pages and tuples estimates for the
     761             :      * whole relation.  If we ever consider multiple path types for sampled
     762             :      * rels, we'll need more complication.
     763             :      */
     764          36 :     rel->pages = pages;
     765          36 :     rel->tuples = tuples;
     766             : 
     767             :     /* Mark rel with estimated output rows, width, etc */
     768          36 :     set_baserel_size_estimates(root, rel);
     769          36 : }
     770             : 
     771             : /*
     772             :  * set_tablesample_rel_pathlist
     773             :  *    Build access paths for a sampled relation
     774             :  */
     775             : static void
     776          36 : set_tablesample_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
     777             : {
     778             :     Relids      required_outer;
     779             :     Path       *path;
     780             : 
     781             :     /*
     782             :      * We don't support pushing join clauses into the quals of a samplescan,
     783             :      * but it could still have required parameterization due to LATERAL refs
     784             :      * in its tlist or TABLESAMPLE arguments.
     785             :      */
     786          36 :     required_outer = rel->lateral_relids;
     787             : 
     788             :     /* Consider sampled scan */
     789          36 :     path = create_samplescan_path(root, rel, required_outer);
     790             : 
     791             :     /*
     792             :      * If the sampling method does not support repeatable scans, we must avoid
     793             :      * plans that would scan the rel multiple times.  Ideally, we'd simply
     794             :      * avoid putting the rel on the inside of a nestloop join; but adding such
     795             :      * a consideration to the planner seems like a great deal of complication
     796             :      * to support an uncommon usage of second-rate sampling methods.  Instead,
     797             :      * if there is a risk that the query might perform an unsafe join, just
     798             :      * wrap the SampleScan in a Materialize node.  We can check for joins by
     799             :      * counting the membership of all_baserels (note that this correctly
     800             :      * counts inheritance trees as single rels).  If we're inside a subquery,
     801             :      * we can't easily check whether a join might occur in the outer query, so
     802             :      * just assume one is possible.
     803             :      *
     804             :      * GetTsmRoutine is relatively expensive compared to the other tests here,
     805             :      * so check repeatable_across_scans last, even though that's a bit odd.
     806             :      */
     807          70 :     if ((root->query_level > 1 ||
     808          39 :          bms_membership(root->all_baserels) != BMS_SINGLETON) &&
     809           5 :         !(GetTsmRoutine(rte->tablesample->tsmhandler)->repeatable_across_scans))
     810             :     {
     811           0 :         path = (Path *) create_material_path(rel, path);
     812             :     }
     813             : 
     814          36 :     add_path(rel, path);
     815             : 
     816             :     /* For the moment, at least, there are no other paths to consider */
     817          36 : }
     818             : 
     819             : /*
     820             :  * set_foreign_size
     821             :  *      Set size estimates for a foreign table RTE
     822             :  */
     823             : static void
     824           0 : set_foreign_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
     825             : {
     826             :     /* Mark rel with estimated output rows, width, etc */
     827           0 :     set_foreign_size_estimates(root, rel);
     828             : 
     829             :     /* Let FDW adjust the size estimates, if it can */
     830           0 :     rel->fdwroutine->GetForeignRelSize(root, rel, rte->relid);
     831             : 
     832             :     /* ... but do not let it set the rows estimate to zero */
     833           0 :     rel->rows = clamp_row_est(rel->rows);
     834           0 : }
     835             : 
     836             : /*
     837             :  * set_foreign_pathlist
     838             :  *      Build access paths for a foreign table RTE
     839             :  */
     840             : static void
     841           0 : set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
     842             : {
     843             :     /* Call the FDW's GetForeignPaths function to generate path(s) */
     844           0 :     rel->fdwroutine->GetForeignPaths(root, rel, rte->relid);
     845           0 : }
     846             : 
     847             : /*
     848             :  * set_append_rel_size
     849             :  *    Set size estimates for a simple "append relation"
     850             :  *
     851             :  * The passed-in rel and RTE represent the entire append relation.  The
     852             :  * relation's contents are computed by appending together the output of the
     853             :  * individual member relations.  Note that in the non-partitioned inheritance
     854             :  * case, the first member relation is actually the same table as is mentioned
     855             :  * in the parent RTE ... but it has a different RTE and RelOptInfo.  This is
     856             :  * a good thing because their outputs are not the same size.
     857             :  */
     858             : static void
     859         589 : set_append_rel_size(PlannerInfo *root, RelOptInfo *rel,
     860             :                     Index rti, RangeTblEntry *rte)
     861             : {
     862         589 :     int         parentRTindex = rti;
     863             :     bool        has_live_children;
     864             :     double      parent_rows;
     865             :     double      parent_size;
     866             :     double     *parent_attrsizes;
     867             :     int         nattrs;
     868             :     ListCell   *l;
     869             : 
     870         589 :     Assert(IS_SIMPLE_REL(rel));
     871             : 
     872             :     /*
     873             :      * Initialize to compute size estimates for whole append relation.
     874             :      *
     875             :      * We handle width estimates by weighting the widths of different child
     876             :      * rels proportionally to their number of rows.  This is sensible because
     877             :      * the use of width estimates is mainly to compute the total relation
     878             :      * "footprint" if we have to sort or hash it.  To do this, we sum the
     879             :      * total equivalent size (in "double" arithmetic) and then divide by the
     880             :      * total rowcount estimate.  This is done separately for the total rel
     881             :      * width and each attribute.
     882             :      *
     883             :      * Note: if you consider changing this logic, beware that child rels could
     884             :      * have zero rows and/or width, if they were excluded by constraints.
     885             :      */
     886         589 :     has_live_children = false;
     887         589 :     parent_rows = 0;
     888         589 :     parent_size = 0;
     889         589 :     nattrs = rel->max_attr - rel->min_attr + 1;
     890         589 :     parent_attrsizes = (double *) palloc0(nattrs * sizeof(double));
     891             : 
     892        2495 :     foreach(l, root->append_rel_list)
     893             :     {
     894        1906 :         AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
     895             :         int         childRTindex;
     896             :         RangeTblEntry *childRTE;
     897             :         RelOptInfo *childrel;
     898             :         List       *childquals;
     899             :         Index       cq_min_security;
     900             :         bool        have_const_false_cq;
     901             :         ListCell   *parentvars;
     902             :         ListCell   *childvars;
     903             :         ListCell   *lc;
     904             : 
     905             :         /* append_rel_list contains all append rels; ignore others */
     906        1906 :         if (appinfo->parent_relid != parentRTindex)
     907         774 :             continue;
     908             : 
     909        1566 :         childRTindex = appinfo->child_relid;
     910        1566 :         childRTE = root->simple_rte_array[childRTindex];
     911             : 
     912             :         /*
     913             :          * The child rel's RelOptInfo was already created during
     914             :          * add_base_rels_to_query.
     915             :          */
     916        1566 :         childrel = find_base_rel(root, childRTindex);
     917        1566 :         Assert(childrel->reloptkind == RELOPT_OTHER_MEMBER_REL);
     918             : 
     919             :         /*
     920             :          * We have to copy the parent's targetlist and quals to the child,
     921             :          * with appropriate substitution of variables.  However, only the
     922             :          * baserestrictinfo quals are needed before we can check for
     923             :          * constraint exclusion; so do that first and then check to see if we
     924             :          * can disregard this child.
     925             :          *
     926             :          * The child rel's targetlist might contain non-Var expressions, which
     927             :          * means that substitution into the quals could produce opportunities
     928             :          * for const-simplification, and perhaps even pseudoconstant quals.
     929             :          * Therefore, transform each RestrictInfo separately to see if it
     930             :          * reduces to a constant or pseudoconstant.  (We must process them
     931             :          * separately to keep track of the security level of each qual.)
     932             :          */
     933        1566 :         childquals = NIL;
     934        1566 :         cq_min_security = UINT_MAX;
     935        1566 :         have_const_false_cq = false;
     936        2358 :         foreach(lc, rel->baserestrictinfo)
     937             :         {
     938         793 :             RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
     939             :             Node       *childqual;
     940             :             ListCell   *lc2;
     941             : 
     942         793 :             Assert(IsA(rinfo, RestrictInfo));
     943         793 :             childqual = adjust_appendrel_attrs(root,
     944         793 :                                                (Node *) rinfo->clause,
     945             :                                                1, &appinfo);
     946         793 :             childqual = eval_const_expressions(root, childqual);
     947             :             /* check for flat-out constant */
     948         793 :             if (childqual && IsA(childqual, Const))
     949             :             {
     950           8 :                 if (((Const *) childqual)->constisnull ||
     951           4 :                     !DatumGetBool(((Const *) childqual)->constvalue))
     952             :                 {
     953             :                     /* Restriction reduces to constant FALSE or NULL */
     954           1 :                     have_const_false_cq = true;
     955           1 :                     break;
     956             :                 }
     957             :                 /* Restriction reduces to constant TRUE, so drop it */
     958           3 :                 continue;
     959             :             }
     960             :             /* might have gotten an AND clause, if so flatten it */
     961        1580 :             foreach(lc2, make_ands_implicit((Expr *) childqual))
     962             :             {
     963         791 :                 Node       *onecq = (Node *) lfirst(lc2);
     964             :                 bool        pseudoconstant;
     965             : 
     966             :                 /* check for pseudoconstant (no Vars or volatile functions) */
     967         791 :                 pseudoconstant =
     968         791 :                     !contain_vars_of_level(onecq, 0) &&
     969           0 :                     !contain_volatile_functions(onecq);
     970         791 :                 if (pseudoconstant)
     971             :                 {
     972             :                     /* tell createplan.c to check for gating quals */
     973           0 :                     root->hasPseudoConstantQuals = true;
     974             :                 }
     975             :                 /* reconstitute RestrictInfo with appropriate properties */
     976         791 :                 childquals = lappend(childquals,
     977        2373 :                                      make_restrictinfo((Expr *) onecq,
     978         791 :                                                        rinfo->is_pushed_down,
     979         791 :                                                        rinfo->outerjoin_delayed,
     980             :                                                        pseudoconstant,
     981             :                                                        rinfo->security_level,
     982             :                                                        NULL, NULL, NULL));
     983             :                 /* track minimum security level among child quals */
     984         791 :                 cq_min_security = Min(cq_min_security, rinfo->security_level);
     985             :             }
     986             :         }
     987             : 
     988             :         /*
     989             :          * In addition to the quals inherited from the parent, we might have
     990             :          * securityQuals associated with this particular child node.
     991             :          * (Currently this can only happen in appendrels originating from
     992             :          * UNION ALL; inheritance child tables don't have their own
     993             :          * securityQuals, see expand_inherited_rtentry().)  Pull any such
     994             :          * securityQuals up into the baserestrictinfo for the child.  This is
     995             :          * similar to process_security_barrier_quals() for the parent rel,
     996             :          * except that we can't make any general deductions from such quals,
     997             :          * since they don't hold for the whole appendrel.
     998             :          */
     999        1566 :         if (childRTE->securityQuals)
    1000             :         {
    1001           2 :             Index       security_level = 0;
    1002             : 
    1003           4 :             foreach(lc, childRTE->securityQuals)
    1004             :             {
    1005           2 :                 List       *qualset = (List *) lfirst(lc);
    1006             :                 ListCell   *lc2;
    1007             : 
    1008           4 :                 foreach(lc2, qualset)
    1009             :                 {
    1010           2 :                     Expr       *qual = (Expr *) lfirst(lc2);
    1011             : 
    1012             :                     /* not likely that we'd see constants here, so no check */
    1013           2 :                     childquals = lappend(childquals,
    1014           2 :                                          make_restrictinfo(qual,
    1015             :                                                            true, false, false,
    1016             :                                                            security_level,
    1017             :                                                            NULL, NULL, NULL));
    1018           2 :                     cq_min_security = Min(cq_min_security, security_level);
    1019             :                 }
    1020           2 :                 security_level++;
    1021             :             }
    1022           2 :             Assert(security_level <= root->qual_security_level);
    1023             :         }
    1024             : 
    1025             :         /*
    1026             :          * OK, we've got all the baserestrictinfo quals for this child.
    1027             :          */
    1028        1566 :         childrel->baserestrictinfo = childquals;
    1029        1566 :         childrel->baserestrict_min_security = cq_min_security;
    1030             : 
    1031        1566 :         if (have_const_false_cq)
    1032             :         {
    1033             :             /*
    1034             :              * Some restriction clause reduced to constant FALSE or NULL after
    1035             :              * substitution, so this child need not be scanned.
    1036             :              */
    1037           1 :             set_dummy_rel_pathlist(childrel);
    1038           1 :             continue;
    1039             :         }
    1040             : 
    1041        1565 :         if (relation_excluded_by_constraints(root, childrel, childRTE))
    1042             :         {
    1043             :             /*
    1044             :              * This child need not be scanned, so we can omit it from the
    1045             :              * appendrel.
    1046             :              */
    1047          91 :             set_dummy_rel_pathlist(childrel);
    1048          91 :             continue;
    1049             :         }
    1050             : 
    1051             :         /*
    1052             :          * CE failed, so finish copying/modifying targetlist and join quals.
    1053             :          *
    1054             :          * NB: the resulting childrel->reltarget->exprs may contain arbitrary
    1055             :          * expressions, which otherwise would not occur in a rel's targetlist.
    1056             :          * Code that might be looking at an appendrel child must cope with
    1057             :          * such.  (Normally, a rel's targetlist would only include Vars and
    1058             :          * PlaceHolderVars.)  XXX we do not bother to update the cost or width
    1059             :          * fields of childrel->reltarget; not clear if that would be useful.
    1060             :          */
    1061        1474 :         childrel->joininfo = (List *)
    1062        1474 :             adjust_appendrel_attrs(root,
    1063        1474 :                                    (Node *) rel->joininfo,
    1064             :                                    1, &appinfo);
    1065        2948 :         childrel->reltarget->exprs = (List *)
    1066        1474 :             adjust_appendrel_attrs(root,
    1067        1474 :                                    (Node *) rel->reltarget->exprs,
    1068             :                                    1, &appinfo);
    1069             : 
    1070             :         /*
    1071             :          * We have to make child entries in the EquivalenceClass data
    1072             :          * structures as well.  This is needed either if the parent
    1073             :          * participates in some eclass joins (because we will want to consider
    1074             :          * inner-indexscan joins on the individual children) or if the parent
    1075             :          * has useful pathkeys (because we should try to build MergeAppend
    1076             :          * paths that produce those sort orderings).
    1077             :          */
    1078        1474 :         if (rel->has_eclass_joins || has_useful_pathkeys(root, rel))
    1079         845 :             add_child_rel_equivalences(root, appinfo, rel, childrel);
    1080        1474 :         childrel->has_eclass_joins = rel->has_eclass_joins;
    1081             : 
    1082             :         /*
    1083             :          * Note: we could compute appropriate attr_needed data for the child's
    1084             :          * variables, by transforming the parent's attr_needed through the
    1085             :          * translated_vars mapping.  However, currently there's no need
    1086             :          * because attr_needed is only examined for base relations not
    1087             :          * otherrels.  So we just leave the child's attr_needed empty.
    1088             :          */
    1089             : 
    1090             :         /*
    1091             :          * If parallelism is allowable for this query in general, see whether
    1092             :          * it's allowable for this childrel in particular.  But if we've
    1093             :          * already decided the appendrel is not parallel-safe as a whole,
    1094             :          * there's no point in considering parallelism for this child.  For
    1095             :          * consistency, do this before calling set_rel_size() for the child.
    1096             :          */
    1097        1474 :         if (root->glob->parallelModeOK && rel->consider_parallel)
    1098        1048 :             set_rel_consider_parallel(root, childrel, childRTE);
    1099             : 
    1100             :         /*
    1101             :          * Compute the child's size.
    1102             :          */
    1103        1474 :         set_rel_size(root, childrel, childRTindex, childRTE);
    1104             : 
    1105             :         /*
    1106             :          * It is possible that constraint exclusion detected a contradiction
    1107             :          * within a child subquery, even though we didn't prove one above. If
    1108             :          * so, we can skip this child.
    1109             :          */
    1110        1474 :         if (IS_DUMMY_REL(childrel))
    1111           2 :             continue;
    1112             : 
    1113             :         /* We have at least one live child. */
    1114        1472 :         has_live_children = true;
    1115             : 
    1116             :         /*
    1117             :          * If any live child is not parallel-safe, treat the whole appendrel
    1118             :          * as not parallel-safe.  In future we might be able to generate plans
    1119             :          * in which some children are farmed out to workers while others are
    1120             :          * not; but we don't have that today, so it's a waste to consider
    1121             :          * partial paths anywhere in the appendrel unless it's all safe.
    1122             :          * (Child rels visited before this one will be unmarked in
    1123             :          * set_append_rel_pathlist().)
    1124             :          */
    1125        1472 :         if (!childrel->consider_parallel)
    1126         434 :             rel->consider_parallel = false;
    1127             : 
    1128             :         /*
    1129             :          * Accumulate size information from each live child.
    1130             :          */
    1131        1472 :         Assert(childrel->rows > 0);
    1132             : 
    1133        1472 :         parent_rows += childrel->rows;
    1134        1472 :         parent_size += childrel->reltarget->width * childrel->rows;
    1135             : 
    1136             :         /*
    1137             :          * Accumulate per-column estimates too.  We need not do anything for
    1138             :          * PlaceHolderVars in the parent list.  If child expression isn't a
    1139             :          * Var, or we didn't record a width estimate for it, we have to fall
    1140             :          * back on a datatype-based estimate.
    1141             :          *
    1142             :          * By construction, child's targetlist is 1-to-1 with parent's.
    1143             :          */
    1144        4238 :         forboth(parentvars, rel->reltarget->exprs,
    1145             :                 childvars, childrel->reltarget->exprs)
    1146             :         {
    1147        2766 :             Var        *parentvar = (Var *) lfirst(parentvars);
    1148        2766 :             Node       *childvar = (Node *) lfirst(childvars);
    1149             : 
    1150        2766 :             if (IsA(parentvar, Var))
    1151             :             {
    1152        2676 :                 int         pndx = parentvar->varattno - rel->min_attr;
    1153        2676 :                 int32       child_width = 0;
    1154             : 
    1155        5258 :                 if (IsA(childvar, Var) &&
    1156        2582 :                     ((Var *) childvar)->varno == childrel->relid)
    1157             :                 {
    1158        2579 :                     int         cndx = ((Var *) childvar)->varattno - childrel->min_attr;
    1159             : 
    1160        2579 :                     child_width = childrel->attr_widths[cndx];
    1161             :                 }
    1162        2676 :                 if (child_width <= 0)
    1163          97 :                     child_width = get_typavgwidth(exprType(childvar),
    1164             :                                                   exprTypmod(childvar));
    1165        2676 :                 Assert(child_width > 0);
    1166        2676 :                 parent_attrsizes[pndx] += child_width * childrel->rows;
    1167             :             }
    1168             :         }
    1169             :     }
    1170             : 
    1171         589 :     if (has_live_children)
    1172             :     {
    1173             :         /*
    1174             :          * Save the finished size estimates.
    1175             :          */
    1176             :         int         i;
    1177             : 
    1178         584 :         Assert(parent_rows > 0);
    1179         584 :         rel->rows = parent_rows;
    1180         584 :         rel->reltarget->width = rint(parent_size / parent_rows);
    1181        4464 :         for (i = 0; i < nattrs; i++)
    1182        3880 :             rel->attr_widths[i] = rint(parent_attrsizes[i] / parent_rows);
    1183             : 
    1184             :         /*
    1185             :          * Set "raw tuples" count equal to "rows" for the appendrel; needed
    1186             :          * because some places assume rel->tuples is valid for any baserel.
    1187             :          */
    1188         584 :         rel->tuples = parent_rows;
    1189             :     }
    1190             :     else
    1191             :     {
    1192             :         /*
    1193             :          * All children were excluded by constraints, so mark the whole
    1194             :          * appendrel dummy.  We must do this in this phase so that the rel's
    1195             :          * dummy-ness is visible when we generate paths for other rels.
    1196             :          */
    1197           5 :         set_dummy_rel_pathlist(rel);
    1198             :     }
    1199             : 
    1200         589 :     pfree(parent_attrsizes);
    1201         589 : }
    1202             : 
    1203             : /*
    1204             :  * set_append_rel_pathlist
    1205             :  *    Build access paths for an "append relation"
    1206             :  */
    1207             : static void
    1208         584 : set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
    1209             :                         Index rti, RangeTblEntry *rte)
    1210             : {
    1211         584 :     int         parentRTindex = rti;
    1212         584 :     List       *live_childrels = NIL;
    1213             :     ListCell   *l;
    1214             : 
    1215             :     /*
    1216             :      * Generate access paths for each member relation, and remember the
    1217             :      * non-dummy children.
    1218             :      */
    1219        2469 :     foreach(l, root->append_rel_list)
    1220             :     {
    1221        1885 :         AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
    1222             :         int         childRTindex;
    1223             :         RangeTblEntry *childRTE;
    1224             :         RelOptInfo *childrel;
    1225             : 
    1226             :         /* append_rel_list contains all append rels; ignore others */
    1227        1885 :         if (appinfo->parent_relid != parentRTindex)
    1228         338 :             continue;
    1229             : 
    1230             :         /* Re-locate the child RTE and RelOptInfo */
    1231        1547 :         childRTindex = appinfo->child_relid;
    1232        1547 :         childRTE = root->simple_rte_array[childRTindex];
    1233        1547 :         childrel = root->simple_rel_array[childRTindex];
    1234             : 
    1235             :         /*
    1236             :          * If set_append_rel_size() decided the parent appendrel was
    1237             :          * parallel-unsafe at some point after visiting this child rel, we
    1238             :          * need to propagate the unsafety marking down to the child, so that
    1239             :          * we don't generate useless partial paths for it.
    1240             :          */
    1241        1547 :         if (!rel->consider_parallel)
    1242         445 :             childrel->consider_parallel = false;
    1243             : 
    1244             :         /*
    1245             :          * Compute the child's access paths.
    1246             :          */
    1247        1547 :         set_rel_pathlist(root, childrel, childRTindex, childRTE);
    1248             : 
    1249             :         /*
    1250             :          * If child is dummy, ignore it.
    1251             :          */
    1252        1547 :         if (IS_DUMMY_REL(childrel))
    1253          75 :             continue;
    1254             : 
    1255             :         /*
    1256             :          * Child is live, so add it to the live_childrels list for use below.
    1257             :          */
    1258        1472 :         live_childrels = lappend(live_childrels, childrel);
    1259             :     }
    1260             : 
    1261             :     /* Add paths to the "append" relation. */
    1262         584 :     add_paths_to_append_rel(root, rel, live_childrels);
    1263         584 : }
    1264             : 
    1265             : 
    1266             : /*
    1267             :  * add_paths_to_append_rel
    1268             :  *      Generate paths for given "append" relation given the set of non-dummy
    1269             :  *      child rels.
    1270             :  *
    1271             :  * The function collects all parameterizations and orderings supported by the
    1272             :  * non-dummy children. For every such parameterization or ordering, it creates
    1273             :  * an append path collecting one path from each non-dummy child with given
    1274             :  * parameterization or ordering. Similarly it collects partial paths from
    1275             :  * non-dummy children to create partial append paths.
    1276             :  */
    1277             : static void
    1278         584 : add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel,
    1279             :                         List *live_childrels)
    1280             : {
    1281         584 :     List       *subpaths = NIL;
    1282         584 :     bool        subpaths_valid = true;
    1283         584 :     List       *partial_subpaths = NIL;
    1284         584 :     bool        partial_subpaths_valid = true;
    1285         584 :     List       *all_child_pathkeys = NIL;
    1286         584 :     List       *all_child_outers = NIL;
    1287             :     ListCell   *l;
    1288         584 :     List       *partitioned_rels = NIL;
    1289             :     RangeTblEntry *rte;
    1290             : 
    1291         584 :     rte = planner_rt_fetch(rel->relid, root);
    1292         584 :     if (rte->relkind == RELKIND_PARTITIONED_TABLE)
    1293             :     {
    1294          62 :         partitioned_rels = get_partitioned_child_rels(root, rel->relid);
    1295             :         /* The root partitioned table is included as a child rel */
    1296          62 :         Assert(list_length(partitioned_rels) >= 1);
    1297             :     }
    1298             : 
    1299             :     /*
    1300             :      * For every non-dummy child, remember the cheapest path.  Also, identify
    1301             :      * all pathkeys (orderings) and parameterizations (required_outer sets)
    1302             :      * available for the non-dummy member relations.
    1303             :      */
    1304        2056 :     foreach(l, live_childrels)
    1305             :     {
    1306        1472 :         RelOptInfo *childrel = lfirst(l);
    1307             :         ListCell   *lcp;
    1308             : 
    1309             :         /*
    1310             :          * If child has an unparameterized cheapest-total path, add that to
    1311             :          * the unparameterized Append path we are constructing for the parent.
    1312             :          * If not, there's no workable unparameterized path.
    1313             :          */
    1314        1472 :         if (childrel->cheapest_total_path->param_info == NULL)
    1315        1462 :             subpaths = accumulate_append_subpath(subpaths,
    1316        1462 :                                                  childrel->cheapest_total_path);
    1317             :         else
    1318          10 :             subpaths_valid = false;
    1319             : 
    1320             :         /* Same idea, but for a partial plan. */
    1321        1472 :         if (childrel->partial_pathlist != NIL)
    1322         618 :             partial_subpaths = accumulate_append_subpath(partial_subpaths,
    1323         618 :                                                          linitial(childrel->partial_pathlist));
    1324             :         else
    1325         854 :             partial_subpaths_valid = false;
    1326             : 
    1327             :         /*
    1328             :          * Collect lists of all the available path orderings and
    1329             :          * parameterizations for all the children.  We use these as a
    1330             :          * heuristic to indicate which sort orderings and parameterizations we
    1331             :          * should build Append and MergeAppend paths for.
    1332             :          */
    1333        3170 :         foreach(lcp, childrel->pathlist)
    1334             :         {
    1335        1698 :             Path       *childpath = (Path *) lfirst(lcp);
    1336        1698 :             List       *childkeys = childpath->pathkeys;
    1337        1698 :             Relids      childouter = PATH_REQ_OUTER(childpath);
    1338             : 
    1339             :             /* Unsorted paths don't contribute to pathkey list */
    1340        1698 :             if (childkeys != NIL)
    1341             :             {
    1342             :                 ListCell   *lpk;
    1343         233 :                 bool        found = false;
    1344             : 
    1345             :                 /* Have we already seen this ordering? */
    1346         233 :                 foreach(lpk, all_child_pathkeys)
    1347             :                 {
    1348         152 :                     List       *existing_pathkeys = (List *) lfirst(lpk);
    1349             : 
    1350         152 :                     if (compare_pathkeys(existing_pathkeys,
    1351             :                                          childkeys) == PATHKEYS_EQUAL)
    1352             :                     {
    1353         152 :                         found = true;
    1354         152 :                         break;
    1355             :                     }
    1356             :                 }
    1357         233 :                 if (!found)
    1358             :                 {
    1359             :                     /* No, so add it to all_child_pathkeys */
    1360          81 :                     all_child_pathkeys = lappend(all_child_pathkeys,
    1361             :                                                  childkeys);
    1362             :                 }
    1363             :             }
    1364             : 
    1365             :             /* Unparameterized paths don't contribute to param-set list */
    1366        1698 :             if (childouter)
    1367             :             {
    1368             :                 ListCell   *lco;
    1369          90 :                 bool        found = false;
    1370             : 
    1371             :                 /* Have we already seen this param set? */
    1372         106 :                 foreach(lco, all_child_outers)
    1373             :                 {
    1374          53 :                     Relids      existing_outers = (Relids) lfirst(lco);
    1375             : 
    1376          53 :                     if (bms_equal(existing_outers, childouter))
    1377             :                     {
    1378          37 :                         found = true;
    1379          37 :                         break;
    1380             :                     }
    1381             :                 }
    1382          90 :                 if (!found)
    1383             :                 {
    1384             :                     /* No, so add it to all_child_outers */
    1385          53 :                     all_child_outers = lappend(all_child_outers,
    1386             :                                                childouter);
    1387             :                 }
    1388             :             }
    1389             :         }
    1390             :     }
    1391             : 
    1392             :     /*
    1393             :      * If we found unparameterized paths for all children, build an unordered,
    1394             :      * unparameterized Append path for the rel.  (Note: this is correct even
    1395             :      * if we have zero or one live subpath due to constraint exclusion.)
    1396             :      */
    1397         584 :     if (subpaths_valid)
    1398         579 :         add_path(rel, (Path *) create_append_path(rel, subpaths, NULL, 0,
    1399             :                                                   partitioned_rels));
    1400             : 
    1401             :     /*
    1402             :      * Consider an append of partial unordered, unparameterized partial paths.
    1403             :      */
    1404         584 :     if (partial_subpaths_valid)
    1405             :     {
    1406             :         AppendPath *appendpath;
    1407             :         ListCell   *lc;
    1408         199 :         int         parallel_workers = 0;
    1409             : 
    1410             :         /*
    1411             :          * Decide on the number of workers to request for this append path.
    1412             :          * For now, we just use the maximum value from among the members.  It
    1413             :          * might be useful to use a higher number if the Append node were
    1414             :          * smart enough to spread out the workers, but it currently isn't.
    1415             :          */
    1416         799 :         foreach(lc, partial_subpaths)
    1417             :         {
    1418         600 :             Path       *path = lfirst(lc);
    1419             : 
    1420         600 :             parallel_workers = Max(parallel_workers, path->parallel_workers);
    1421             :         }
    1422         199 :         Assert(parallel_workers > 0);
    1423             : 
    1424             :         /* Generate a partial append path. */
    1425         199 :         appendpath = create_append_path(rel, partial_subpaths, NULL,
    1426             :                                         parallel_workers, partitioned_rels);
    1427         199 :         add_partial_path(rel, (Path *) appendpath);
    1428             :     }
    1429             : 
    1430             :     /*
    1431             :      * Also build unparameterized MergeAppend paths based on the collected
    1432             :      * list of child pathkeys.
    1433             :      */
    1434         584 :     if (subpaths_valid)
    1435         579 :         generate_mergeappend_paths(root, rel, live_childrels,
    1436             :                                    all_child_pathkeys,
    1437             :                                    partitioned_rels);
    1438             : 
    1439             :     /*
    1440             :      * Build Append paths for each parameterization seen among the child rels.
    1441             :      * (This may look pretty expensive, but in most cases of practical
    1442             :      * interest, the child rels will expose mostly the same parameterizations,
    1443             :      * so that not that many cases actually get considered here.)
    1444             :      *
    1445             :      * The Append node itself cannot enforce quals, so all qual checking must
    1446             :      * be done in the child paths.  This means that to have a parameterized
    1447             :      * Append path, we must have the exact same parameterization for each
    1448             :      * child path; otherwise some children might be failing to check the
    1449             :      * moved-down quals.  To make them match up, we can try to increase the
    1450             :      * parameterization of lesser-parameterized paths.
    1451             :      */
    1452         637 :     foreach(l, all_child_outers)
    1453             :     {
    1454          53 :         Relids      required_outer = (Relids) lfirst(l);
    1455             :         ListCell   *lcr;
    1456             : 
    1457             :         /* Select the child paths for an Append with this parameterization */
    1458          53 :         subpaths = NIL;
    1459          53 :         subpaths_valid = true;
    1460         165 :         foreach(lcr, live_childrels)
    1461             :         {
    1462         112 :             RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
    1463             :             Path       *subpath;
    1464             : 
    1465         112 :             subpath = get_cheapest_parameterized_child_path(root,
    1466             :                                                             childrel,
    1467             :                                                             required_outer);
    1468         112 :             if (subpath == NULL)
    1469             :             {
    1470             :                 /* failed to make a suitable path for this child */
    1471           0 :                 subpaths_valid = false;
    1472           0 :                 break;
    1473             :             }
    1474         112 :             subpaths = accumulate_append_subpath(subpaths, subpath);
    1475             :         }
    1476             : 
    1477          53 :         if (subpaths_valid)
    1478          53 :             add_path(rel, (Path *)
    1479          53 :                      create_append_path(rel, subpaths, required_outer, 0,
    1480             :                                         partitioned_rels));
    1481             :     }
    1482         584 : }
    1483             : 
    1484             : /*
    1485             :  * generate_mergeappend_paths
    1486             :  *      Generate MergeAppend paths for an append relation
    1487             :  *
    1488             :  * Generate a path for each ordering (pathkey list) appearing in
    1489             :  * all_child_pathkeys.
    1490             :  *
    1491             :  * We consider both cheapest-startup and cheapest-total cases, ie, for each
    1492             :  * interesting ordering, collect all the cheapest startup subpaths and all the
    1493             :  * cheapest total paths, and build a MergeAppend path for each case.
    1494             :  *
    1495             :  * We don't currently generate any parameterized MergeAppend paths.  While
    1496             :  * it would not take much more code here to do so, it's very unclear that it
    1497             :  * is worth the planning cycles to investigate such paths: there's little
    1498             :  * use for an ordered path on the inside of a nestloop.  In fact, it's likely
    1499             :  * that the current coding of add_path would reject such paths out of hand,
    1500             :  * because add_path gives no credit for sort ordering of parameterized paths,
    1501             :  * and a parameterized MergeAppend is going to be more expensive than the
    1502             :  * corresponding parameterized Append path.  If we ever try harder to support
    1503             :  * parameterized mergejoin plans, it might be worth adding support for
    1504             :  * parameterized MergeAppends to feed such joins.  (See notes in
    1505             :  * optimizer/README for why that might not ever happen, though.)
    1506             :  */
    1507             : static void
    1508         579 : generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel,
    1509             :                            List *live_childrels,
    1510             :                            List *all_child_pathkeys,
    1511             :                            List *partitioned_rels)
    1512             : {
    1513             :     ListCell   *lcp;
    1514             : 
    1515         660 :     foreach(lcp, all_child_pathkeys)
    1516             :     {
    1517          81 :         List       *pathkeys = (List *) lfirst(lcp);
    1518          81 :         List       *startup_subpaths = NIL;
    1519          81 :         List       *total_subpaths = NIL;
    1520          81 :         bool        startup_neq_total = false;
    1521             :         ListCell   *lcr;
    1522             : 
    1523             :         /* Select the child paths for this ordering... */
    1524         281 :         foreach(lcr, live_childrels)
    1525             :         {
    1526         200 :             RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
    1527             :             Path       *cheapest_startup,
    1528             :                        *cheapest_total;
    1529             : 
    1530             :             /* Locate the right paths, if they are available. */
    1531         200 :             cheapest_startup =
    1532         200 :                 get_cheapest_path_for_pathkeys(childrel->pathlist,
    1533             :                                                pathkeys,
    1534             :                                                NULL,
    1535             :                                                STARTUP_COST,
    1536             :                                                false);
    1537         200 :             cheapest_total =
    1538         200 :                 get_cheapest_path_for_pathkeys(childrel->pathlist,
    1539             :                                                pathkeys,
    1540             :                                                NULL,
    1541             :                                                TOTAL_COST,
    1542             :                                                false);
    1543             : 
    1544             :             /*
    1545             :              * If we can't find any paths with the right order just use the
    1546             :              * cheapest-total path; we'll have to sort it later.
    1547             :              */
    1548         200 :             if (cheapest_startup == NULL || cheapest_total == NULL)
    1549             :             {
    1550          36 :                 cheapest_startup = cheapest_total =
    1551             :                     childrel->cheapest_total_path;
    1552             :                 /* Assert we do have an unparameterized path for this child */
    1553          36 :                 Assert(cheapest_total->param_info == NULL);
    1554             :             }
    1555             : 
    1556             :             /*
    1557             :              * Notice whether we actually have different paths for the
    1558             :              * "cheapest" and "total" cases; frequently there will be no point
    1559             :              * in two create_merge_append_path() calls.
    1560             :              */
    1561         200 :             if (cheapest_startup != cheapest_total)
    1562           0 :                 startup_neq_total = true;
    1563             : 
    1564         200 :             startup_subpaths =
    1565             :                 accumulate_append_subpath(startup_subpaths, cheapest_startup);
    1566         200 :             total_subpaths =
    1567             :                 accumulate_append_subpath(total_subpaths, cheapest_total);
    1568             :         }
    1569             : 
    1570             :         /* ... and build the MergeAppend paths */
    1571          81 :         add_path(rel, (Path *) create_merge_append_path(root,
    1572             :                                                         rel,
    1573             :                                                         startup_subpaths,
    1574             :                                                         pathkeys,
    1575             :                                                         NULL,
    1576             :                                                         partitioned_rels));
    1577          81 :         if (startup_neq_total)
    1578           0 :             add_path(rel, (Path *) create_merge_append_path(root,
    1579             :                                                             rel,
    1580             :                                                             total_subpaths,
    1581             :                                                             pathkeys,
    1582             :                                                             NULL,
    1583             :                                                             partitioned_rels));
    1584             :     }
    1585         579 : }
    1586             : 
    1587             : /*
    1588             :  * get_cheapest_parameterized_child_path
    1589             :  *      Get cheapest path for this relation that has exactly the requested
    1590             :  *      parameterization.
    1591             :  *
    1592             :  * Returns NULL if unable to create such a path.
    1593             :  */
    1594             : static Path *
    1595         112 : get_cheapest_parameterized_child_path(PlannerInfo *root, RelOptInfo *rel,
    1596             :                                       Relids required_outer)
    1597             : {
    1598             :     Path       *cheapest;
    1599             :     ListCell   *lc;
    1600             : 
    1601             :     /*
    1602             :      * Look up the cheapest existing path with no more than the needed
    1603             :      * parameterization.  If it has exactly the needed parameterization, we're
    1604             :      * done.
    1605             :      */
    1606         112 :     cheapest = get_cheapest_path_for_pathkeys(rel->pathlist,
    1607             :                                               NIL,
    1608             :                                               required_outer,
    1609             :                                               TOTAL_COST,
    1610             :                                               false);
    1611         112 :     Assert(cheapest != NULL);
    1612         112 :     if (bms_equal(PATH_REQ_OUTER(cheapest), required_outer))
    1613          72 :         return cheapest;
    1614             : 
    1615             :     /*
    1616             :      * Otherwise, we can "reparameterize" an existing path to match the given
    1617             :      * parameterization, which effectively means pushing down additional
    1618             :      * joinquals to be checked within the path's scan.  However, some existing
    1619             :      * paths might check the available joinquals already while others don't;
    1620             :      * therefore, it's not clear which existing path will be cheapest after
    1621             :      * reparameterization.  We have to go through them all and find out.
    1622             :      */
    1623          40 :     cheapest = NULL;
    1624         118 :     foreach(lc, rel->pathlist)
    1625             :     {
    1626          78 :         Path       *path = (Path *) lfirst(lc);
    1627             : 
    1628             :         /* Can't use it if it needs more than requested parameterization */
    1629          78 :         if (!bms_is_subset(PATH_REQ_OUTER(path), required_outer))
    1630           0 :             continue;
    1631             : 
    1632             :         /*
    1633             :          * Reparameterization can only increase the path's cost, so if it's
    1634             :          * already more expensive than the current cheapest, forget it.
    1635             :          */
    1636         116 :         if (cheapest != NULL &&
    1637          38 :             compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
    1638          38 :             continue;
    1639             : 
    1640             :         /* Reparameterize if needed, then recheck cost */
    1641          40 :         if (!bms_equal(PATH_REQ_OUTER(path), required_outer))
    1642             :         {
    1643          40 :             path = reparameterize_path(root, path, required_outer, 1.0);
    1644          40 :             if (path == NULL)
    1645           0 :                 continue;       /* failed to reparameterize this one */
    1646          40 :             Assert(bms_equal(PATH_REQ_OUTER(path), required_outer));
    1647             : 
    1648          40 :             if (cheapest != NULL &&
    1649           0 :                 compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
    1650           0 :                 continue;
    1651             :         }
    1652             : 
    1653             :         /* We have a new best path */
    1654          40 :         cheapest = path;
    1655             :     }
    1656             : 
    1657             :     /* Return the best path, or NULL if we found no suitable candidate */
    1658          40 :     return cheapest;
    1659             : }
    1660             : 
    1661             : /*
    1662             :  * accumulate_append_subpath
    1663             :  *      Add a subpath to the list being built for an Append or MergeAppend
    1664             :  *
    1665             :  * It's possible that the child is itself an Append or MergeAppend path, in
    1666             :  * which case we can "cut out the middleman" and just add its child paths to
    1667             :  * our own list.  (We don't try to do this earlier because we need to apply
    1668             :  * both levels of transformation to the quals.)
    1669             :  *
    1670             :  * Note that if we omit a child MergeAppend in this way, we are effectively
    1671             :  * omitting a sort step, which seems fine: if the parent is to be an Append,
    1672             :  * its result would be unsorted anyway, while if the parent is to be a
    1673             :  * MergeAppend, there's no point in a separate sort on a child.
    1674             :  */
    1675             : static List *
    1676        2592 : accumulate_append_subpath(List *subpaths, Path *path)
    1677             : {
    1678        2592 :     if (IsA(path, AppendPath))
    1679             :     {
    1680          43 :         AppendPath *apath = (AppendPath *) path;
    1681             : 
    1682             :         /* list_copy is important here to avoid sharing list substructure */
    1683          43 :         return list_concat(subpaths, list_copy(apath->subpaths));
    1684             :     }
    1685        2549 :     else if (IsA(path, MergeAppendPath))
    1686             :     {
    1687          24 :         MergeAppendPath *mpath = (MergeAppendPath *) path;
    1688             : 
    1689             :         /* list_copy is important here to avoid sharing list substructure */
    1690          24 :         return list_concat(subpaths, list_copy(mpath->subpaths));
    1691             :     }
    1692             :     else
    1693        2525 :         return lappend(subpaths, path);
    1694             : }
    1695             : 
    1696             : /*
    1697             :  * set_dummy_rel_pathlist
    1698             :  *    Build a dummy path for a relation that's been excluded by constraints
    1699             :  *
    1700             :  * Rather than inventing a special "dummy" path type, we represent this as an
    1701             :  * AppendPath with no members (see also IS_DUMMY_PATH/IS_DUMMY_REL macros).
    1702             :  *
    1703             :  * This is exported because inheritance_planner() has need for it.
    1704             :  */
    1705             : void
    1706         132 : set_dummy_rel_pathlist(RelOptInfo *rel)
    1707             : {
    1708             :     /* Set dummy size estimates --- we leave attr_widths[] as zeroes */
    1709         132 :     rel->rows = 0;
    1710         132 :     rel->reltarget->width = 0;
    1711             : 
    1712             :     /* Discard any pre-existing paths; no further need for them */
    1713         132 :     rel->pathlist = NIL;
    1714         132 :     rel->partial_pathlist = NIL;
    1715             : 
    1716         132 :     add_path(rel, (Path *) create_append_path(rel, NIL, NULL, 0, NIL));
    1717             : 
    1718             :     /*
    1719             :      * We set the cheapest path immediately, to ensure that IS_DUMMY_REL()
    1720             :      * will recognize the relation as dummy if anyone asks.  This is redundant
    1721             :      * when we're called from set_rel_size(), but not when called from
    1722             :      * elsewhere, and doing it twice is harmless anyway.
    1723             :      */
    1724         132 :     set_cheapest(rel);
    1725         132 : }
    1726             : 
    1727             : /* quick-and-dirty test to see if any joining is needed */
    1728             : static bool
    1729         328 : has_multiple_baserels(PlannerInfo *root)
    1730             : {
    1731         328 :     int         num_base_rels = 0;
    1732             :     Index       rti;
    1733             : 
    1734        1016 :     for (rti = 1; rti < root->simple_rel_array_size; rti++)
    1735             :     {
    1736         841 :         RelOptInfo *brel = root->simple_rel_array[rti];
    1737             : 
    1738         841 :         if (brel == NULL)
    1739         283 :             continue;
    1740             : 
    1741             :         /* ignore RTEs that are "other rels" */
    1742         558 :         if (brel->reloptkind == RELOPT_BASEREL)
    1743         481 :             if (++num_base_rels > 1)
    1744         153 :                 return true;
    1745             :     }
    1746         175 :     return false;
    1747             : }
    1748             : 
    1749             : /*
    1750             :  * set_subquery_pathlist
    1751             :  *      Generate SubqueryScan access paths for a subquery RTE
    1752             :  *
    1753             :  * We don't currently support generating parameterized paths for subqueries
    1754             :  * by pushing join clauses down into them; it seems too expensive to re-plan
    1755             :  * the subquery multiple times to consider different alternatives.
    1756             :  * (XXX that could stand to be reconsidered, now that we use Paths.)
    1757             :  * So the paths made here will be parameterized if the subquery contains
    1758             :  * LATERAL references, otherwise not.  As long as that's true, there's no need
    1759             :  * for a separate set_subquery_size phase: just make the paths right away.
    1760             :  */
    1761             : static void
    1762         790 : set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
    1763             :                       Index rti, RangeTblEntry *rte)
    1764             : {
    1765         790 :     Query      *parse = root->parse;
    1766         790 :     Query      *subquery = rte->subquery;
    1767             :     Relids      required_outer;
    1768             :     pushdown_safety_info safetyInfo;
    1769             :     double      tuple_fraction;
    1770             :     RelOptInfo *sub_final_rel;
    1771             :     ListCell   *lc;
    1772             : 
    1773             :     /*
    1774             :      * Must copy the Query so that planning doesn't mess up the RTE contents
    1775             :      * (really really need to fix the planner to not scribble on its input,
    1776             :      * someday ... but see remove_unused_subquery_outputs to start with).
    1777             :      */
    1778         790 :     subquery = copyObject(subquery);
    1779             : 
    1780             :     /*
    1781             :      * If it's a LATERAL subquery, it might contain some Vars of the current
    1782             :      * query level, requiring it to be treated as parameterized, even though
    1783             :      * we don't support pushing down join quals into subqueries.
    1784             :      */
    1785         790 :     required_outer = rel->lateral_relids;
    1786             : 
    1787             :     /*
    1788             :      * Zero out result area for subquery_is_pushdown_safe, so that it can set
    1789             :      * flags as needed while recursing.  In particular, we need a workspace
    1790             :      * for keeping track of unsafe-to-reference columns.  unsafeColumns[i]
    1791             :      * will be set TRUE if we find that output column i of the subquery is
    1792             :      * unsafe to use in a pushed-down qual.
    1793             :      */
    1794         790 :     memset(&safetyInfo, 0, sizeof(safetyInfo));
    1795         790 :     safetyInfo.unsafeColumns = (bool *)
    1796         790 :         palloc0((list_length(subquery->targetList) + 1) * sizeof(bool));
    1797             : 
    1798             :     /*
    1799             :      * If the subquery has the "security_barrier" flag, it means the subquery
    1800             :      * originated from a view that must enforce row level security.  Then we
    1801             :      * must not push down quals that contain leaky functions.  (Ideally this
    1802             :      * would be checked inside subquery_is_pushdown_safe, but since we don't
    1803             :      * currently pass the RTE to that function, we must do it here.)
    1804             :      */
    1805         790 :     safetyInfo.unsafeLeaky = rte->security_barrier;
    1806             : 
    1807             :     /*
    1808             :      * If there are any restriction clauses that have been attached to the
    1809             :      * subquery relation, consider pushing them down to become WHERE or HAVING
    1810             :      * quals of the subquery itself.  This transformation is useful because it
    1811             :      * may allow us to generate a better plan for the subquery than evaluating
    1812             :      * all the subquery output rows and then filtering them.
    1813             :      *
    1814             :      * There are several cases where we cannot push down clauses. Restrictions
    1815             :      * involving the subquery are checked by subquery_is_pushdown_safe().
    1816             :      * Restrictions on individual clauses are checked by
    1817             :      * qual_is_pushdown_safe().  Also, we don't want to push down
    1818             :      * pseudoconstant clauses; better to have the gating node above the
    1819             :      * subquery.
    1820             :      *
    1821             :      * Non-pushed-down clauses will get evaluated as qpquals of the
    1822             :      * SubqueryScan node.
    1823             :      *
    1824             :      * XXX Are there any cases where we want to make a policy decision not to
    1825             :      * push down a pushable qual, because it'd result in a worse plan?
    1826             :      */
    1827         930 :     if (rel->baserestrictinfo != NIL &&
    1828         140 :         subquery_is_pushdown_safe(subquery, subquery, &safetyInfo))
    1829             :     {
    1830             :         /* OK to consider pushing down individual quals */
    1831         125 :         List       *upperrestrictlist = NIL;
    1832             :         ListCell   *l;
    1833             : 
    1834         330 :         foreach(l, rel->baserestrictinfo)
    1835             :         {
    1836         205 :             RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
    1837         205 :             Node       *clause = (Node *) rinfo->clause;
    1838             : 
    1839         410 :             if (!rinfo->pseudoconstant &&
    1840         205 :                 qual_is_pushdown_safe(subquery, rti, clause, &safetyInfo))
    1841             :             {
    1842             :                 /* Push it down */
    1843         136 :                 subquery_push_qual(subquery, rte, rti, clause);
    1844             :             }
    1845             :             else
    1846             :             {
    1847             :                 /* Keep it in the upper query */
    1848          69 :                 upperrestrictlist = lappend(upperrestrictlist, rinfo);
    1849             :             }
    1850             :         }
    1851         125 :         rel->baserestrictinfo = upperrestrictlist;
    1852             :         /* We don't bother recomputing baserestrict_min_security */
    1853             :     }
    1854             : 
    1855         790 :     pfree(safetyInfo.unsafeColumns);
    1856             : 
    1857             :     /*
    1858             :      * The upper query might not use all the subquery's output columns; if
    1859             :      * not, we can simplify.
    1860             :      */
    1861         790 :     remove_unused_subquery_outputs(subquery, rel);
    1862             : 
    1863             :     /*
    1864             :      * We can safely pass the outer tuple_fraction down to the subquery if the
    1865             :      * outer level has no joining, aggregation, or sorting to do. Otherwise
    1866             :      * we'd better tell the subquery to plan for full retrieval. (XXX This
    1867             :      * could probably be made more intelligent ...)
    1868             :      */
    1869        1553 :     if (parse->hasAggs ||
    1870        1525 :         parse->groupClause ||
    1871        1524 :         parse->groupingSets ||
    1872        1524 :         parse->havingQual ||
    1873        1523 :         parse->distinctClause ||
    1874        1089 :         parse->sortClause ||
    1875         328 :         has_multiple_baserels(root))
    1876         615 :         tuple_fraction = 0.0;   /* default case */
    1877             :     else
    1878         175 :         tuple_fraction = root->tuple_fraction;
    1879             : 
    1880             :     /* plan_params should not be in use in current query level */
    1881         790 :     Assert(root->plan_params == NIL);
    1882             : 
    1883             :     /* Generate a subroot and Paths for the subquery */
    1884         790 :     rel->subroot = subquery_planner(root->glob, subquery,
    1885             :                                     root,
    1886             :                                     false, tuple_fraction);
    1887             : 
    1888             :     /* Isolate the params needed by this specific subplan */
    1889         790 :     rel->subplan_params = root->plan_params;
    1890         790 :     root->plan_params = NIL;
    1891             : 
    1892             :     /*
    1893             :      * It's possible that constraint exclusion proved the subquery empty. If
    1894             :      * so, it's desirable to produce an unadorned dummy path so that we will
    1895             :      * recognize appropriate optimizations at this query level.
    1896             :      */
    1897         790 :     sub_final_rel = fetch_upper_rel(rel->subroot, UPPERREL_FINAL, NULL);
    1898             : 
    1899         790 :     if (IS_DUMMY_REL(sub_final_rel))
    1900             :     {
    1901           3 :         set_dummy_rel_pathlist(rel);
    1902         793 :         return;
    1903             :     }
    1904             : 
    1905             :     /*
    1906             :      * Mark rel with estimated output rows, width, etc.  Note that we have to
    1907             :      * do this before generating outer-query paths, else cost_subqueryscan is
    1908             :      * not happy.
    1909             :      */
    1910         787 :     set_subquery_size_estimates(root, rel);
    1911             : 
    1912             :     /*
    1913             :      * For each Path that subquery_planner produced, make a SubqueryScanPath
    1914             :      * in the outer query.
    1915             :      */
    1916        1610 :     foreach(lc, sub_final_rel->pathlist)
    1917             :     {
    1918         823 :         Path       *subpath = (Path *) lfirst(lc);
    1919             :         List       *pathkeys;
    1920             : 
    1921             :         /* Convert subpath's pathkeys to outer representation */
    1922         823 :         pathkeys = convert_subquery_pathkeys(root,
    1923             :                                              rel,
    1924             :                                              subpath->pathkeys,
    1925             :                                              make_tlist_from_pathtarget(subpath->pathtarget));
    1926             : 
    1927             :         /* Generate outer path using this subpath */
    1928         823 :         add_path(rel, (Path *)
    1929         823 :                  create_subqueryscan_path(root, rel, subpath,
    1930             :                                           pathkeys, required_outer));
    1931             :     }
    1932             : }
    1933             : 
    1934             : /*
    1935             :  * set_function_pathlist
    1936             :  *      Build the (single) access path for a function RTE
    1937             :  */
    1938             : static void
    1939        1327 : set_function_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
    1940             : {
    1941             :     Relids      required_outer;
    1942        1327 :     List       *pathkeys = NIL;
    1943             : 
    1944             :     /*
    1945             :      * We don't support pushing join clauses into the quals of a function
    1946             :      * scan, but it could still have required parameterization due to LATERAL
    1947             :      * refs in the function expression.
    1948             :      */
    1949        1327 :     required_outer = rel->lateral_relids;
    1950             : 
    1951             :     /*
    1952             :      * The result is considered unordered unless ORDINALITY was used, in which
    1953             :      * case it is ordered by the ordinal column (the last one).  See if we
    1954             :      * care, by checking for uses of that Var in equivalence classes.
    1955             :      */
    1956        1327 :     if (rte->funcordinality)
    1957             :     {
    1958          63 :         AttrNumber  ordattno = rel->max_attr;
    1959          63 :         Var        *var = NULL;
    1960             :         ListCell   *lc;
    1961             : 
    1962             :         /*
    1963             :          * Is there a Var for it in rel's targetlist?  If not, the query did
    1964             :          * not reference the ordinality column, or at least not in any way
    1965             :          * that would be interesting for sorting.
    1966             :          */
    1967         251 :         foreach(lc, rel->reltarget->exprs)
    1968             :         {
    1969         250 :             Var        *node = (Var *) lfirst(lc);
    1970             : 
    1971             :             /* checking varno/varlevelsup is just paranoia */
    1972         500 :             if (IsA(node, Var) &&
    1973         312 :                 node->varattno == ordattno &&
    1974         124 :                 node->varno == rel->relid &&
    1975          62 :                 node->varlevelsup == 0)
    1976             :             {
    1977          62 :                 var = node;
    1978          62 :                 break;
    1979             :             }
    1980             :         }
    1981             : 
    1982             :         /*
    1983             :          * Try to build pathkeys for this Var with int8 sorting.  We tell
    1984             :          * build_expression_pathkey not to build any new equivalence class; if
    1985             :          * the Var isn't already mentioned in some EC, it means that nothing
    1986             :          * cares about the ordering.
    1987             :          */
    1988          63 :         if (var)
    1989          62 :             pathkeys = build_expression_pathkey(root,
    1990             :                                                 (Expr *) var,
    1991             :                                                 NULL,   /* below outer joins */
    1992             :                                                 Int8LessOperator,
    1993             :                                                 rel->relids,
    1994             :                                                 false);
    1995             :     }
    1996             : 
    1997             :     /* Generate appropriate path */
    1998        1327 :     add_path(rel, create_functionscan_path(root, rel,
    1999             :                                            pathkeys, required_outer));
    2000        1327 : }
    2001             : 
    2002             : /*
    2003             :  * set_values_pathlist
    2004             :  *      Build the (single) access path for a VALUES RTE
    2005             :  */
    2006             : static void
    2007         463 : set_values_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
    2008             : {
    2009             :     Relids      required_outer;
    2010             : 
    2011             :     /*
    2012             :      * We don't support pushing join clauses into the quals of a values scan,
    2013             :      * but it could still have required parameterization due to LATERAL refs
    2014             :      * in the values expressions.
    2015             :      */
    2016         463 :     required_outer = rel->lateral_relids;
    2017             : 
    2018             :     /* Generate appropriate path */
    2019         463 :     add_path(rel, create_valuesscan_path(root, rel, required_outer));
    2020         463 : }
    2021             : 
    2022             : /*
    2023             :  * set_tablefunc_pathlist
    2024             :  *      Build the (single) access path for a table func RTE
    2025             :  */
    2026             : static void
    2027          22 : set_tablefunc_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
    2028             : {
    2029             :     Relids      required_outer;
    2030             : 
    2031             :     /*
    2032             :      * We don't support pushing join clauses into the quals of a tablefunc
    2033             :      * scan, but it could still have required parameterization due to LATERAL
    2034             :      * refs in the function expression.
    2035             :      */
    2036          22 :     required_outer = rel->lateral_relids;
    2037             : 
    2038             :     /* Generate appropriate path */
    2039          22 :     add_path(rel, create_tablefuncscan_path(root, rel,
    2040             :                                             required_outer));
    2041          22 : }
    2042             : 
    2043             : /*
    2044             :  * set_cte_pathlist
    2045             :  *      Build the (single) access path for a non-self-reference CTE RTE
    2046             :  *
    2047             :  * There's no need for a separate set_cte_size phase, since we don't
    2048             :  * support join-qual-parameterized paths for CTEs.
    2049             :  */
    2050             : static void
    2051         162 : set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
    2052             : {
    2053             :     Plan       *cteplan;
    2054             :     PlannerInfo *cteroot;
    2055             :     Index       levelsup;
    2056             :     int         ndx;
    2057             :     ListCell   *lc;
    2058             :     int         plan_id;
    2059             :     Relids      required_outer;
    2060             : 
    2061             :     /*
    2062             :      * Find the referenced CTE, and locate the plan previously made for it.
    2063             :      */
    2064         162 :     levelsup = rte->ctelevelsup;
    2065         162 :     cteroot = root;
    2066         384 :     while (levelsup-- > 0)
    2067             :     {
    2068          60 :         cteroot = cteroot->parent_root;
    2069          60 :         if (!cteroot)           /* shouldn't happen */
    2070           0 :             elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
    2071             :     }
    2072             : 
    2073             :     /*
    2074             :      * Note: cte_plan_ids can be shorter than cteList, if we are still working
    2075             :      * on planning the CTEs (ie, this is a side-reference from another CTE).
    2076             :      * So we mustn't use forboth here.
    2077             :      */
    2078         162 :     ndx = 0;
    2079         188 :     foreach(lc, cteroot->parse->cteList)
    2080             :     {
    2081         188 :         CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
    2082             : 
    2083         188 :         if (strcmp(cte->ctename, rte->ctename) == 0)
    2084         162 :             break;
    2085          26 :         ndx++;
    2086             :     }
    2087         162 :     if (lc == NULL)             /* shouldn't happen */
    2088           0 :         elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
    2089         162 :     if (ndx >= list_length(cteroot->cte_plan_ids))
    2090           0 :         elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
    2091         162 :     plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
    2092         162 :     Assert(plan_id > 0);
    2093         162 :     cteplan = (Plan *) list_nth(root->glob->subplans, plan_id - 1);
    2094             : 
    2095             :     /* Mark rel with estimated output rows, width, etc */
    2096         162 :     set_cte_size_estimates(root, rel, cteplan->plan_rows);
    2097             : 
    2098             :     /*
    2099             :      * We don't support pushing join clauses into the quals of a CTE scan, but
    2100             :      * it could still have required parameterization due to LATERAL refs in
    2101             :      * its tlist.
    2102             :      */
    2103         162 :     required_outer = rel->lateral_relids;
    2104             : 
    2105             :     /* Generate appropriate path */
    2106         162 :     add_path(rel, create_ctescan_path(root, rel, required_outer));
    2107         162 : }
    2108             : 
    2109             : /*
    2110             :  * set_namedtuplestore_pathlist
    2111             :  *      Build the (single) access path for a named tuplestore RTE
    2112             :  *
    2113             :  * There's no need for a separate set_namedtuplestore_size phase, since we
    2114             :  * don't support join-qual-parameterized paths for tuplestores.
    2115             :  */
    2116             : static void
    2117          43 : set_namedtuplestore_pathlist(PlannerInfo *root, RelOptInfo *rel,
    2118             :                              RangeTblEntry *rte)
    2119             : {
    2120             :     Relids      required_outer;
    2121             : 
    2122             :     /* Mark rel with estimated output rows, width, etc */
    2123          43 :     set_namedtuplestore_size_estimates(root, rel);
    2124             : 
    2125             :     /*
    2126             :      * We don't support pushing join clauses into the quals of a tuplestore
    2127             :      * scan, but it could still have required parameterization due to LATERAL
    2128             :      * refs in its tlist.
    2129             :      */
    2130          43 :     required_outer = rel->lateral_relids;
    2131             : 
    2132             :     /* Generate appropriate path */
    2133          43 :     add_path(rel, create_namedtuplestorescan_path(root, rel, required_outer));
    2134             : 
    2135             :     /* Select cheapest path (pretty easy in this case...) */
    2136          43 :     set_cheapest(rel);
    2137          43 : }
    2138             : 
    2139             : /*
    2140             :  * set_worktable_pathlist
    2141             :  *      Build the (single) access path for a self-reference CTE RTE
    2142             :  *
    2143             :  * There's no need for a separate set_worktable_size phase, since we don't
    2144             :  * support join-qual-parameterized paths for CTEs.
    2145             :  */
    2146             : static void
    2147          40 : set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
    2148             : {
    2149             :     Path       *ctepath;
    2150             :     PlannerInfo *cteroot;
    2151             :     Index       levelsup;
    2152             :     Relids      required_outer;
    2153             : 
    2154             :     /*
    2155             :      * We need to find the non-recursive term's path, which is in the plan
    2156             :      * level that's processing the recursive UNION, which is one level *below*
    2157             :      * where the CTE comes from.
    2158             :      */
    2159          40 :     levelsup = rte->ctelevelsup;
    2160          40 :     if (levelsup == 0)          /* shouldn't happen */
    2161           0 :         elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
    2162          40 :     levelsup--;
    2163          40 :     cteroot = root;
    2164         126 :     while (levelsup-- > 0)
    2165             :     {
    2166          46 :         cteroot = cteroot->parent_root;
    2167          46 :         if (!cteroot)           /* shouldn't happen */
    2168           0 :             elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
    2169             :     }
    2170          40 :     ctepath = cteroot->non_recursive_path;
    2171          40 :     if (!ctepath)               /* shouldn't happen */
    2172           0 :         elog(ERROR, "could not find path for CTE \"%s\"", rte->ctename);
    2173             : 
    2174             :     /* Mark rel with estimated output rows, width, etc */
    2175          40 :     set_cte_size_estimates(root, rel, ctepath->rows);
    2176             : 
    2177             :     /*
    2178             :      * We don't support pushing join clauses into the quals of a worktable
    2179             :      * scan, but it could still have required parameterization due to LATERAL
    2180             :      * refs in its tlist.  (I'm not sure this is actually possible given the
    2181             :      * restrictions on recursive references, but it's easy enough to support.)
    2182             :      */
    2183          40 :     required_outer = rel->lateral_relids;
    2184             : 
    2185             :     /* Generate appropriate path */
    2186          40 :     add_path(rel, create_worktablescan_path(root, rel, required_outer));
    2187          40 : }
    2188             : 
    2189             : /*
    2190             :  * generate_gather_paths
    2191             :  *      Generate parallel access paths for a relation by pushing a Gather or
    2192             :  *      Gather Merge on top of a partial path.
    2193             :  *
    2194             :  * This must not be called until after we're done creating all partial paths
    2195             :  * for the specified relation.  (Otherwise, add_partial_path might delete a
    2196             :  * path that some GatherPath or GatherMergePath has a reference to.)
    2197             :  */
    2198             : void
    2199       22891 : generate_gather_paths(PlannerInfo *root, RelOptInfo *rel)
    2200             : {
    2201             :     Path       *cheapest_partial_path;
    2202             :     Path       *simple_gather_path;
    2203             :     ListCell   *lc;
    2204             : 
    2205             :     /* If there are no partial paths, there's nothing to do here. */
    2206       22891 :     if (rel->partial_pathlist == NIL)
    2207       45524 :         return;
    2208             : 
    2209             :     /*
    2210             :      * The output of Gather is always unsorted, so there's only one partial
    2211             :      * path of interest: the cheapest one.  That will be the one at the front
    2212             :      * of partial_pathlist because of the way add_partial_path works.
    2213             :      */
    2214         258 :     cheapest_partial_path = linitial(rel->partial_pathlist);
    2215         258 :     simple_gather_path = (Path *)
    2216         258 :         create_gather_path(root, rel, cheapest_partial_path, rel->reltarget,
    2217             :                            NULL, NULL);
    2218         258 :     add_path(rel, simple_gather_path);
    2219             : 
    2220             :     /*
    2221             :      * For each useful ordering, we can consider an order-preserving Gather
    2222             :      * Merge.
    2223             :      */
    2224         517 :     foreach(lc, rel->partial_pathlist)
    2225             :     {
    2226         259 :         Path       *subpath = (Path *) lfirst(lc);
    2227             :         GatherMergePath *path;
    2228             : 
    2229         259 :         if (subpath->pathkeys == NIL)
    2230         256 :             continue;
    2231             : 
    2232           3 :         path = create_gather_merge_path(root, rel, subpath, rel->reltarget,
    2233             :                                         subpath->pathkeys, NULL, NULL);
    2234           3 :         add_path(rel, &path->path);
    2235             :     }
    2236             : }
    2237             : 
    2238             : /*
    2239             :  * make_rel_from_joinlist
    2240             :  *    Build access paths using a "joinlist" to guide the join path search.
    2241             :  *
    2242             :  * See comments for deconstruct_jointree() for definition of the joinlist
    2243             :  * data structure.
    2244             :  */
    2245             : static RelOptInfo *
    2246       13938 : make_rel_from_joinlist(PlannerInfo *root, List *joinlist)
    2247             : {
    2248             :     int         levels_needed;
    2249             :     List       *initial_rels;
    2250             :     ListCell   *jl;
    2251             : 
    2252             :     /*
    2253             :      * Count the number of child joinlist nodes.  This is the depth of the
    2254             :      * dynamic-programming algorithm we must employ to consider all ways of
    2255             :      * joining the child nodes.
    2256             :      */
    2257       13938 :     levels_needed = list_length(joinlist);
    2258             : 
    2259       13938 :     if (levels_needed <= 0)
    2260           0 :         return NULL;            /* nothing to do? */
    2261             : 
    2262             :     /*
    2263             :      * Construct a list of rels corresponding to the child joinlist nodes.
    2264             :      * This may contain both base rels and rels constructed according to
    2265             :      * sub-joinlists.
    2266             :      */
    2267       13938 :     initial_rels = NIL;
    2268       31598 :     foreach(jl, joinlist)
    2269             :     {
    2270       17660 :         Node       *jlnode = (Node *) lfirst(jl);
    2271             :         RelOptInfo *thisrel;
    2272             : 
    2273       17660 :         if (IsA(jlnode, RangeTblRef))
    2274             :         {
    2275       17540 :             int         varno = ((RangeTblRef *) jlnode)->rtindex;
    2276             : 
    2277       17540 :             thisrel = find_base_rel(root, varno);
    2278             :         }
    2279         120 :         else if (IsA(jlnode, List))
    2280             :         {
    2281             :             /* Recurse to handle subproblem */
    2282         120 :             thisrel = make_rel_from_joinlist(root, (List *) jlnode);
    2283             :         }
    2284             :         else
    2285             :         {
    2286           0 :             elog(ERROR, "unrecognized joinlist node type: %d",
    2287             :                  (int) nodeTag(jlnode));
    2288             :             thisrel = NULL;     /* keep compiler quiet */
    2289             :         }
    2290             : 
    2291       17660 :         initial_rels = lappend(initial_rels, thisrel);
    2292             :     }
    2293             : 
    2294       13938 :     if (levels_needed == 1)
    2295             :     {
    2296             :         /*
    2297             :          * Single joinlist node, so we're done.
    2298             :          */
    2299       10847 :         return (RelOptInfo *) linitial(initial_rels);
    2300             :     }
    2301             :     else
    2302             :     {
    2303             :         /*
    2304             :          * Consider the different orders in which we could join the rels,
    2305             :          * using a plugin, GEQO, or the regular join search code.
    2306             :          *
    2307             :          * We put the initial_rels list into a PlannerInfo field because
    2308             :          * has_legal_joinclause() needs to look at it (ugly :-().
    2309             :          */
    2310        3091 :         root->initial_rels = initial_rels;
    2311             : 
    2312        3091 :         if (join_search_hook)
    2313           0 :             return (*join_search_hook) (root, levels_needed, initial_rels);
    2314        3091 :         else if (enable_geqo && levels_needed >= geqo_threshold)
    2315           1 :             return geqo(root, levels_needed, initial_rels);
    2316             :         else
    2317        3090 :             return standard_join_search(root, levels_needed, initial_rels);
    2318             :     }
    2319             : }
    2320             : 
    2321             : /*
    2322             :  * standard_join_search
    2323             :  *    Find possible joinpaths for a query by successively finding ways
    2324             :  *    to join component relations into join relations.
    2325             :  *
    2326             :  * 'levels_needed' is the number of iterations needed, ie, the number of
    2327             :  *      independent jointree items in the query.  This is > 1.
    2328             :  *
    2329             :  * 'initial_rels' is a list of RelOptInfo nodes for each independent
    2330             :  *      jointree item.  These are the components to be joined together.
    2331             :  *      Note that levels_needed == list_length(initial_rels).
    2332             :  *
    2333             :  * Returns the final level of join relations, i.e., the relation that is
    2334             :  * the result of joining all the original relations together.
    2335             :  * At least one implementation path must be provided for this relation and
    2336             :  * all required sub-relations.
    2337             :  *
    2338             :  * To support loadable plugins that modify planner behavior by changing the
    2339             :  * join searching algorithm, we provide a hook variable that lets a plugin
    2340             :  * replace or supplement this function.  Any such hook must return the same
    2341             :  * final join relation as the standard code would, but it might have a
    2342             :  * different set of implementation paths attached, and only the sub-joinrels
    2343             :  * needed for these paths need have been instantiated.
    2344             :  *
    2345             :  * Note to plugin authors: the functions invoked during standard_join_search()
    2346             :  * modify root->join_rel_list and root->join_rel_hash.  If you want to do more
    2347             :  * than one join-order search, you'll probably need to save and restore the
    2348             :  * original states of those data structures.  See geqo_eval() for an example.
    2349             :  */
    2350             : RelOptInfo *
    2351        3090 : standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
    2352             : {
    2353             :     int         lev;
    2354             :     RelOptInfo *rel;
    2355             : 
    2356             :     /*
    2357             :      * This function cannot be invoked recursively within any one planning
    2358             :      * problem, so join_rel_level[] can't be in use already.
    2359             :      */
    2360        3090 :     Assert(root->join_rel_level == NULL);
    2361             : 
    2362             :     /*
    2363             :      * We employ a simple "dynamic programming" algorithm: we first find all
    2364             :      * ways to build joins of two jointree items, then all ways to build joins
    2365             :      * of three items (from two-item joins and single items), then four-item
    2366             :      * joins, and so on until we have considered all ways to join all the
    2367             :      * items into one rel.
    2368             :      *
    2369             :      * root->join_rel_level[j] is a list of all the j-item rels.  Initially we
    2370             :      * set root->join_rel_level[1] to represent all the single-jointree-item
    2371             :      * relations.
    2372             :      */
    2373        3090 :     root->join_rel_level = (List **) palloc0((levels_needed + 1) * sizeof(List *));
    2374             : 
    2375        3090 :     root->join_rel_level[1] = initial_rels;
    2376             : 
    2377        6808 :     for (lev = 2; lev <= levels_needed; lev++)
    2378             :     {
    2379             :         ListCell   *lc;
    2380             : 
    2381             :         /*
    2382             :          * Determine all possible pairs of relations to be joined at this
    2383             :          * level, and build paths for making each one from every available
    2384             :          * pair of lower-level relations.
    2385             :          */
    2386        3718 :         join_search_one_level(root, lev);
    2387             : 
    2388             :         /*
    2389             :          * Run generate_gather_paths() for each just-processed joinrel.  We
    2390             :          * could not do this earlier because both regular and partial paths
    2391             :          * can get added to a particular joinrel at multiple times within
    2392             :          * join_search_one_level.  After that, we're done creating paths for
    2393             :          * the joinrel, so run set_cheapest().
    2394             :          */
    2395        8553 :         foreach(lc, root->join_rel_level[lev])
    2396             :         {
    2397        4835 :             rel = (RelOptInfo *) lfirst(lc);
    2398             : 
    2399             :             /* Create GatherPaths for any useful partial paths for rel */
    2400        4835 :             generate_gather_paths(root, rel);
    2401             : 
    2402             :             /* Find and save the cheapest paths for this rel */
    2403        4835 :             set_cheapest(rel);
    2404             : 
    2405             : #ifdef OPTIMIZER_DEBUG
    2406             :             debug_print_rel(root, rel);
    2407             : #endif
    2408             :         }
    2409             :     }
    2410             : 
    2411             :     /*
    2412             :      * We should have a single rel at the final level.
    2413             :      */
    2414        3090 :     if (root->join_rel_level[levels_needed] == NIL)
    2415           0 :         elog(ERROR, "failed to build any %d-way joins", levels_needed);
    2416        3090 :     Assert(list_length(root->join_rel_level[levels_needed]) == 1);
    2417             : 
    2418        3090 :     rel = (RelOptInfo *) linitial(root->join_rel_level[levels_needed]);
    2419             : 
    2420        3090 :     root->join_rel_level = NULL;
    2421             : 
    2422        3090 :     return rel;
    2423             : }
    2424             : 
    2425             : /*****************************************************************************
    2426             :  *          PUSHING QUALS DOWN INTO SUBQUERIES
    2427             :  *****************************************************************************/
    2428             : 
    2429             : /*
    2430             :  * subquery_is_pushdown_safe - is a subquery safe for pushing down quals?
    2431             :  *
    2432             :  * subquery is the particular component query being checked.  topquery
    2433             :  * is the top component of a set-operations tree (the same Query if no
    2434             :  * set-op is involved).
    2435             :  *
    2436             :  * Conditions checked here:
    2437             :  *
    2438             :  * 1. If the subquery has a LIMIT clause, we must not push down any quals,
    2439             :  * since that could change the set of rows returned.
    2440             :  *
    2441             :  * 2. If the subquery contains EXCEPT or EXCEPT ALL set ops we cannot push
    2442             :  * quals into it, because that could change the results.
    2443             :  *
    2444             :  * 3. If the subquery uses DISTINCT, we cannot push volatile quals into it.
    2445             :  * This is because upper-level quals should semantically be evaluated only
    2446             :  * once per distinct row, not once per original row, and if the qual is
    2447             :  * volatile then extra evaluations could change the results.  (This issue
    2448             :  * does not apply to other forms of aggregation such as GROUP BY, because
    2449             :  * when those are present we push into HAVING not WHERE, so that the quals
    2450             :  * are still applied after aggregation.)
    2451             :  *
    2452             :  * 4. If the subquery contains window functions, we cannot push volatile quals
    2453             :  * into it.  The issue here is a bit different from DISTINCT: a volatile qual
    2454             :  * might succeed for some rows of a window partition and fail for others,
    2455             :  * thereby changing the partition contents and thus the window functions'
    2456             :  * results for rows that remain.
    2457             :  *
    2458             :  * 5. If the subquery contains any set-returning functions in its targetlist,
    2459             :  * we cannot push volatile quals into it.  That would push them below the SRFs
    2460             :  * and thereby change the number of times they are evaluated.  Also, a
    2461             :  * volatile qual could succeed for some SRF output rows and fail for others,
    2462             :  * a behavior that cannot occur if it's evaluated before SRF expansion.
    2463             :  *
    2464             :  * In addition, we make several checks on the subquery's output columns to see
    2465             :  * if it is safe to reference them in pushed-down quals.  If output column k
    2466             :  * is found to be unsafe to reference, we set safetyInfo->unsafeColumns[k]
    2467             :  * to TRUE, but we don't reject the subquery overall since column k might not
    2468             :  * be referenced by some/all quals.  The unsafeColumns[] array will be
    2469             :  * consulted later by qual_is_pushdown_safe().  It's better to do it this way
    2470             :  * than to make the checks directly in qual_is_pushdown_safe(), because when
    2471             :  * the subquery involves set operations we have to check the output
    2472             :  * expressions in each arm of the set op.
    2473             :  *
    2474             :  * Note: pushing quals into a DISTINCT subquery is theoretically dubious:
    2475             :  * we're effectively assuming that the quals cannot distinguish values that
    2476             :  * the DISTINCT's equality operator sees as equal, yet there are many
    2477             :  * counterexamples to that assumption.  However use of such a qual with a
    2478             :  * DISTINCT subquery would be unsafe anyway, since there's no guarantee which
    2479             :  * "equal" value will be chosen as the output value by the DISTINCT operation.
    2480             :  * So we don't worry too much about that.  Another objection is that if the
    2481             :  * qual is expensive to evaluate, running it for each original row might cost
    2482             :  * more than we save by eliminating rows before the DISTINCT step.  But it
    2483             :  * would be very hard to estimate that at this stage, and in practice pushdown
    2484             :  * seldom seems to make things worse, so we ignore that problem too.
    2485             :  *
    2486             :  * Note: likewise, pushing quals into a subquery with window functions is a
    2487             :  * bit dubious: the quals might remove some rows of a window partition while
    2488             :  * leaving others, causing changes in the window functions' results for the
    2489             :  * surviving rows.  We insist that such a qual reference only partitioning
    2490             :  * columns, but again that only protects us if the qual does not distinguish
    2491             :  * values that the partitioning equality operator sees as equal.  The risks
    2492             :  * here are perhaps larger than for DISTINCT, since no de-duplication of rows
    2493             :  * occurs and thus there is no theoretical problem with such a qual.  But
    2494             :  * we'll do this anyway because the potential performance benefits are very
    2495             :  * large, and we've seen no field complaints about the longstanding comparable
    2496             :  * behavior with DISTINCT.
    2497             :  */
    2498             : static bool
    2499         154 : subquery_is_pushdown_safe(Query *subquery, Query *topquery,
    2500             :                           pushdown_safety_info *safetyInfo)
    2501             : {
    2502             :     SetOperationStmt *topop;
    2503             : 
    2504             :     /* Check point 1 */
    2505         154 :     if (subquery->limitOffset != NULL || subquery->limitCount != NULL)
    2506          15 :         return false;
    2507             : 
    2508             :     /* Check points 3, 4, and 5 */
    2509         277 :     if (subquery->distinctClause ||
    2510         271 :         subquery->hasWindowFuncs ||
    2511         133 :         subquery->hasTargetSRFs)
    2512          39 :         safetyInfo->unsafeVolatile = true;
    2513             : 
    2514             :     /*
    2515             :      * If we're at a leaf query, check for unsafe expressions in its target
    2516             :      * list, and mark any unsafe ones in unsafeColumns[].  (Non-leaf nodes in
    2517             :      * setop trees have only simple Vars in their tlists, so no need to check
    2518             :      * them.)
    2519             :      */
    2520         139 :     if (subquery->setOperations == NULL)
    2521         132 :         check_output_expressions(subquery, safetyInfo);
    2522             : 
    2523             :     /* Are we at top level, or looking at a setop component? */
    2524         139 :     if (subquery == topquery)
    2525             :     {
    2526             :         /* Top level, so check any component queries */
    2527         125 :         if (subquery->setOperations != NULL)
    2528           7 :             if (!recurse_pushdown_safe(subquery->setOperations, topquery,
    2529             :                                        safetyInfo))
    2530           0 :                 return false;
    2531             :     }
    2532             :     else
    2533             :     {
    2534             :         /* Setop component must not have more components (too weird) */
    2535          14 :         if (subquery->setOperations != NULL)
    2536           0 :             return false;
    2537             :         /* Check whether setop component output types match top level */
    2538          14 :         topop = castNode(SetOperationStmt, topquery->setOperations);
    2539          14 :         Assert(topop);
    2540          14 :         compare_tlist_datatypes(subquery->targetList,
    2541             :                                 topop->colTypes,
    2542             :                                 safetyInfo);
    2543             :     }
    2544         139 :     return true;
    2545             : }
    2546             : 
    2547             : /*
    2548             :  * Helper routine to recurse through setOperations tree
    2549             :  */
    2550             : static bool
    2551          21 : recurse_pushdown_safe(Node *setOp, Query *topquery,
    2552             :                       pushdown_safety_info *safetyInfo)
    2553             : {
    2554          21 :     if (IsA(setOp, RangeTblRef))
    2555             :     {
    2556          14 :         RangeTblRef *rtr = (RangeTblRef *) setOp;
    2557          14 :         RangeTblEntry *rte = rt_fetch(rtr->rtindex, topquery->rtable);
    2558          14 :         Query      *subquery = rte->subquery;
    2559             : 
    2560          14 :         Assert(subquery != NULL);
    2561          14 :         return subquery_is_pushdown_safe(subquery, topquery, safetyInfo);
    2562             :     }
    2563           7 :     else if (IsA(setOp, SetOperationStmt))
    2564             :     {
    2565           7 :         SetOperationStmt *op = (SetOperationStmt *) setOp;
    2566             : 
    2567             :         /* EXCEPT is no good (point 2 for subquery_is_pushdown_safe) */
    2568           7 :         if (op->op == SETOP_EXCEPT)
    2569           0 :             return false;
    2570             :         /* Else recurse */
    2571           7 :         if (!recurse_pushdown_safe(op->larg, topquery, safetyInfo))
    2572           0 :             return false;
    2573           7 :         if (!recurse_pushdown_safe(op->rarg, topquery, safetyInfo))
    2574           0 :             return false;
    2575             :     }
    2576             :     else
    2577             :     {
    2578           0 :         elog(ERROR, "unrecognized node type: %d",
    2579             :              (int) nodeTag(setOp));
    2580             :     }
    2581           7 :     return true;
    2582             : }
    2583             : 
    2584             : /*
    2585             :  * check_output_expressions - check subquery's output expressions for safety
    2586             :  *
    2587             :  * There are several cases in which it's unsafe to push down an upper-level
    2588             :  * qual if it references a particular output column of a subquery.  We check
    2589             :  * each output column of the subquery and set unsafeColumns[k] to TRUE if
    2590             :  * that column is unsafe for a pushed-down qual to reference.  The conditions
    2591             :  * checked here are:
    2592             :  *
    2593             :  * 1. We must not push down any quals that refer to subselect outputs that
    2594             :  * return sets, else we'd introduce functions-returning-sets into the
    2595             :  * subquery's WHERE/HAVING quals.
    2596             :  *
    2597             :  * 2. We must not push down any quals that refer to subselect outputs that
    2598             :  * contain volatile functions, for fear of introducing strange results due
    2599             :  * to multiple evaluation of a volatile function.
    2600             :  *
    2601             :  * 3. If the subquery uses DISTINCT ON, we must not push down any quals that
    2602             :  * refer to non-DISTINCT output columns, because that could change the set
    2603             :  * of rows returned.  (This condition is vacuous for DISTINCT, because then
    2604             :  * there are no non-DISTINCT output columns, so we needn't check.  Note that
    2605             :  * subquery_is_pushdown_safe already reported that we can't use volatile
    2606             :  * quals if there's DISTINCT or DISTINCT ON.)
    2607             :  *
    2608             :  * 4. If the subquery has any window functions, we must not push down quals
    2609             :  * that reference any output columns that are not listed in all the subquery's
    2610             :  * window PARTITION BY clauses.  We can push down quals that use only
    2611             :  * partitioning columns because they should succeed or fail identically for
    2612             :  * every row of any one window partition, and totally excluding some
    2613             :  * partitions will not change a window function's results for remaining
    2614             :  * partitions.  (Again, this also requires nonvolatile quals, but
    2615             :  * subquery_is_pushdown_safe handles that.)
    2616             :  */
    2617             : static void
    2618         132 : check_output_expressions(Query *subquery, pushdown_safety_info *safetyInfo)
    2619             : {
    2620             :     ListCell   *lc;
    2621             : 
    2622         893 :     foreach(lc, subquery->targetList)
    2623             :     {
    2624         761 :         TargetEntry *tle = (TargetEntry *) lfirst(lc);
    2625             : 
    2626         761 :         if (tle->resjunk)
    2627           9 :             continue;           /* ignore resjunk columns */
    2628             : 
    2629             :         /* We need not check further if output col is already known unsafe */
    2630         752 :         if (safetyInfo->unsafeColumns[tle->resno])
    2631           4 :             continue;
    2632             : 
    2633             :         /* Functions returning sets are unsafe (point 1) */
    2634         909 :         if (subquery->hasTargetSRFs &&
    2635         161 :             expression_returns_set((Node *) tle->expr))
    2636             :         {
    2637          82 :             safetyInfo->unsafeColumns[tle->resno] = true;
    2638          82 :             continue;
    2639             :         }
    2640             : 
    2641             :         /* Volatile functions are unsafe (point 2) */
    2642         666 :         if (contain_volatile_functions((Node *) tle->expr))
    2643             :         {
    2644          10 :             safetyInfo->unsafeColumns[tle->resno] = true;
    2645          10 :             continue;
    2646             :         }
    2647             : 
    2648             :         /* If subquery uses DISTINCT ON, check point 3 */
    2649         656 :         if (subquery->hasDistinctOn &&
    2650           0 :             !targetIsInSortList(tle, InvalidOid, subquery->distinctClause))
    2651             :         {
    2652             :             /* non-DISTINCT column, so mark it unsafe */
    2653           0 :             safetyInfo->unsafeColumns[tle->resno] = true;
    2654           0 :             continue;
    2655             :         }
    2656             : 
    2657             :         /* If subquery uses window functions, check point 4 */
    2658         677 :         if (subquery->hasWindowFuncs &&
    2659          21 :             !targetIsInAllPartitionLists(tle, subquery))
    2660             :         {
    2661             :             /* not present in all PARTITION BY clauses, so mark it unsafe */
    2662          20 :             safetyInfo->unsafeColumns[tle->resno] = true;
    2663          20 :             continue;
    2664             :         }
    2665             :     }
    2666         132 : }
    2667             : 
    2668             : /*
    2669             :  * For subqueries using UNION/UNION ALL/INTERSECT/INTERSECT ALL, we can
    2670             :  * push quals into each component query, but the quals can only reference
    2671             :  * subquery columns that suffer no type coercions in the set operation.
    2672             :  * Otherwise there are possible semantic gotchas.  So, we check the
    2673             :  * component queries to see if any of them have output types different from
    2674             :  * the top-level setop outputs.  unsafeColumns[k] is set true if column k
    2675             :  * has different type in any component.
    2676             :  *
    2677             :  * We don't have to care about typmods here: the only allowed difference
    2678             :  * between set-op input and output typmods is input is a specific typmod
    2679             :  * and output is -1, and that does not require a coercion.
    2680             :  *
    2681             :  * tlist is a subquery tlist.
    2682             :  * colTypes is an OID list of the top-level setop's output column types.
    2683             :  * safetyInfo->unsafeColumns[] is the result array.
    2684             :  */
    2685             : static void
    2686          14 : compare_tlist_datatypes(List *tlist, List *colTypes,
    2687             :                         pushdown_safety_info *safetyInfo)
    2688             : {
    2689             :     ListCell   *l;
    2690          14 :     ListCell   *colType = list_head(colTypes);
    2691             : 
    2692          40 :     foreach(l, tlist)
    2693             :     {
    2694          26 :         TargetEntry *tle = (TargetEntry *) lfirst(l);
    2695             : 
    2696          26 :         if (tle->resjunk)
    2697           0 :             continue;           /* ignore resjunk columns */
    2698          26 :         if (colType == NULL)
    2699           0 :             elog(ERROR, "wrong number of tlist entries");
    2700          26 :         if (exprType((Node *) tle->expr) != lfirst_oid(colType))
    2701           0 :             safetyInfo->unsafeColumns[tle->resno] = true;
    2702          26 :         colType = lnext(colType);
    2703             :     }
    2704          14 :     if (colType != NULL)
    2705           0 :         elog(ERROR, "wrong number of tlist entries");
    2706          14 : }
    2707             : 
    2708             : /*
    2709             :  * targetIsInAllPartitionLists
    2710             :  *      True if the TargetEntry is listed in the PARTITION BY clause
    2711             :  *      of every window defined in the query.
    2712             :  *
    2713             :  * It would be safe to ignore windows not actually used by any window
    2714             :  * function, but it's not easy to get that info at this stage; and it's
    2715             :  * unlikely to be useful to spend any extra cycles getting it, since
    2716             :  * unreferenced window definitions are probably infrequent in practice.
    2717             :  */
    2718             : static bool
    2719          21 : targetIsInAllPartitionLists(TargetEntry *tle, Query *query)
    2720             : {
    2721             :     ListCell   *lc;
    2722             : 
    2723          23 :     foreach(lc, query->windowClause)
    2724             :     {
    2725          22 :         WindowClause *wc = (WindowClause *) lfirst(lc);
    2726             : 
    2727          22 :         if (!targetIsInSortList(tle, InvalidOid, wc->partitionClause))
    2728          20 :             return false;
    2729             :     }
    2730           1 :     return true;
    2731             : }
    2732             : 
    2733             : /*
    2734             :  * qual_is_pushdown_safe - is a particular qual safe to push down?
    2735             :  *
    2736             :  * qual is a restriction clause applying to the given subquery (whose RTE
    2737             :  * has index rti in the parent query).
    2738             :  *
    2739             :  * Conditions checked here:
    2740             :  *
    2741             :  * 1. The qual must not contain any subselects (mainly because I'm not sure
    2742             :  * it will work correctly: sublinks will already have been transformed into
    2743             :  * subplans in the qual, but not in the subquery).
    2744             :  *
    2745             :  * 2. If unsafeVolatile is set, the qual must not contain any volatile
    2746             :  * functions.
    2747             :  *
    2748             :  * 3. If unsafeLeaky is set, the qual must not contain any leaky functions
    2749             :  * that are passed Var nodes, and therefore might reveal values from the
    2750             :  * subquery as side effects.
    2751             :  *
    2752             :  * 4. The qual must not refer to the whole-row output of the subquery
    2753             :  * (since there is no easy way to name that within the subquery itself).
    2754             :  *
    2755             :  * 5. The qual must not refer to any subquery output columns that were
    2756             :  * found to be unsafe to reference by subquery_is_pushdown_safe().
    2757             :  */
    2758             : static bool
    2759         205 : qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
    2760             :                       pushdown_safety_info *safetyInfo)
    2761             : {
    2762         205 :     bool        safe = true;
    2763             :     List       *vars;
    2764             :     ListCell   *vl;
    2765             : 
    2766             :     /* Refuse subselects (point 1) */
    2767         205 :     if (contain_subplans(qual))
    2768          10 :         return false;
    2769             : 
    2770             :     /* Refuse volatile quals if we found they'd be unsafe (point 2) */
    2771         259 :     if (safetyInfo->unsafeVolatile &&
    2772          64 :         contain_volatile_functions(qual))
    2773           3 :         return false;
    2774             : 
    2775             :     /* Refuse leaky quals if told to (point 3) */
    2776         232 :     if (safetyInfo->unsafeLeaky &&
    2777          40 :         contain_leaked_vars(qual))
    2778          20 :         return false;
    2779             : 
    2780             :     /*
    2781             :      * It would be unsafe to push down window function calls, but at least for
    2782             :      * the moment we could never see any in a qual anyhow.  (The same applies
    2783             :      * to aggregates, which we check for in pull_var_clause below.)
    2784             :      */
    2785         172 :     Assert(!contain_window_function(qual));
    2786             : 
    2787             :     /*
    2788             :      * Examine all Vars used in clause; since it's a restriction clause, all
    2789             :      * such Vars must refer to subselect output columns.
    2790             :      */
    2791         172 :     vars = pull_var_clause(qual, PVC_INCLUDE_PLACEHOLDERS);
    2792         310 :     foreach(vl, vars)
    2793             :     {
    2794         174 :         Var        *var = (Var *) lfirst(vl);
    2795             : 
    2796             :         /*
    2797             :          * XXX Punt if we find any PlaceHolderVars in the restriction clause.
    2798             :          * It's not clear whether a PHV could safely be pushed down, and even
    2799             :          * less clear whether such a situation could arise in any cases of
    2800             :          * practical interest anyway.  So for the moment, just refuse to push
    2801             :          * down.
    2802             :          */
    2803         174 :         if (!IsA(var, Var))
    2804             :         {
    2805           0 :             safe = false;
    2806           0 :             break;
    2807             :         }
    2808             : 
    2809         174 :         Assert(var->varno == rti);
    2810         174 :         Assert(var->varattno >= 0);
    2811             : 
    2812             :         /* Check point 4 */
    2813         174 :         if (var->varattno == 0)
    2814             :         {
    2815           0 :             safe = false;
    2816           0 :             break;
    2817             :         }
    2818             : 
    2819             :         /* Check point 5 */
    2820         174 :         if (safetyInfo->unsafeColumns[var->varattno])
    2821             :         {
    2822          36 :             safe = false;
    2823          36 :             break;
    2824             :         }
    2825             :     }
    2826             : 
    2827         172 :     list_free(vars);
    2828             : 
    2829         172 :     return safe;
    2830             : }
    2831             : 
    2832             : /*
    2833             :  * subquery_push_qual - push down a qual that we have determined is safe
    2834             :  */
    2835             : static void
    2836         142 : subquery_push_qual(Query *subquery, RangeTblEntry *rte, Index rti, Node *qual)
    2837             : {
    2838         142 :     if (subquery->setOperations != NULL)
    2839             :     {
    2840             :         /* Recurse to push it separately to each component query */
    2841           3 :         recurse_push_qual(subquery->setOperations, subquery,
    2842             :                           rte, rti, qual);
    2843             :     }
    2844             :     else
    2845             :     {
    2846             :         /*
    2847             :          * We need to replace Vars in the qual (which must refer to outputs of
    2848             :          * the subquery) with copies of the subquery's targetlist expressions.
    2849             :          * Note that at this point, any uplevel Vars in the qual should have
    2850             :          * been replaced with Params, so they need no work.
    2851             :          *
    2852             :          * This step also ensures that when we are pushing into a setop tree,
    2853             :          * each component query gets its own copy of the qual.
    2854             :          */
    2855         139 :         qual = ReplaceVarsFromTargetList(qual, rti, 0, rte,
    2856             :                                          subquery->targetList,
    2857             :                                          REPLACEVARS_REPORT_ERROR, 0,
    2858             :                                          &subquery->hasSubLinks);
    2859             : 
    2860             :         /*
    2861             :          * Now attach the qual to the proper place: normally WHERE, but if the
    2862             :          * subquery uses grouping or aggregation, put it in HAVING (since the
    2863             :          * qual really refers to the group-result rows).
    2864             :          */
    2865         139 :         if (subquery->hasAggs || subquery->groupClause || subquery->groupingSets || subquery->havingQual)
    2866          25 :             subquery->havingQual = make_and_qual(subquery->havingQual, qual);
    2867             :         else
    2868         228 :             subquery->jointree->quals =
    2869         114 :                 make_and_qual(subquery->jointree->quals, qual);
    2870             : 
    2871             :         /*
    2872             :          * We need not change the subquery's hasAggs or hasSubLinks flags,
    2873             :          * since we can't be pushing down any aggregates that weren't there
    2874             :          * before, and we don't push down subselects at all.
    2875             :          */
    2876             :     }
    2877         142 : }
    2878             : 
    2879             : /*
    2880             :  * Helper routine to recurse through setOperations tree
    2881             :  */
    2882             : static void
    2883           9 : recurse_push_qual(Node *setOp, Query *topquery,
    2884             :                   RangeTblEntry *rte, Index rti, Node *qual)
    2885             : {
    2886           9 :     if (IsA(setOp, RangeTblRef))
    2887             :     {
    2888           6 :         RangeTblRef *rtr = (RangeTblRef *) setOp;
    2889           6 :         RangeTblEntry *subrte = rt_fetch(rtr->rtindex, topquery->rtable);
    2890           6 :         Query      *subquery = subrte->subquery;
    2891             : 
    2892           6 :         Assert(subquery != NULL);
    2893           6 :         subquery_push_qual(subquery, rte, rti, qual);
    2894             :     }
    2895           3 :     else if (IsA(setOp, SetOperationStmt))
    2896             :     {
    2897           3 :         SetOperationStmt *op = (SetOperationStmt *) setOp;
    2898             : 
    2899           3 :         recurse_push_qual(op->larg, topquery, rte, rti, qual);
    2900           3 :         recurse_push_qual(op->rarg, topquery, rte, rti, qual);
    2901             :     }
    2902             :     else
    2903             :     {
    2904           0 :         elog(ERROR, "unrecognized node type: %d",
    2905             :              (int) nodeTag(setOp));
    2906             :     }
    2907           9 : }
    2908             : 
    2909             : /*****************************************************************************
    2910             :  *          SIMPLIFYING SUBQUERY TARGETLISTS
    2911             :  *****************************************************************************/
    2912             : 
    2913             : /*
    2914             :  * remove_unused_subquery_outputs
    2915             :  *      Remove subquery targetlist items we don't need
    2916             :  *
    2917             :  * It's possible, even likely, that the upper query does not read all the
    2918             :  * output columns of the subquery.  We can remove any such outputs that are
    2919             :  * not needed by the subquery itself (e.g., as sort/group columns) and do not
    2920             :  * affect semantics otherwise (e.g., volatile functions can't be removed).
    2921             :  * This is useful not only because we might be able to remove expensive-to-
    2922             :  * compute expressions, but because deletion of output columns might allow
    2923             :  * optimizations such as join removal to occur within the subquery.
    2924             :  *
    2925             :  * To avoid affecting column numbering in the targetlist, we don't physically
    2926             :  * remove unused tlist entries, but rather replace their expressions with NULL
    2927             :  * constants.  This is implemented by modifying subquery->targetList.
    2928             :  */
    2929             : static void
    2930         790 : remove_unused_subquery_outputs(Query *subquery, RelOptInfo *rel)
    2931             : {
    2932         790 :     Bitmapset  *attrs_used = NULL;
    2933             :     ListCell   *lc;
    2934             : 
    2935             :     /*
    2936             :      * Do nothing if subquery has UNION/INTERSECT/EXCEPT: in principle we
    2937             :      * could update all the child SELECTs' tlists, but it seems not worth the
    2938             :      * trouble presently.
    2939             :      */
    2940         790 :     if (subquery->setOperations)
    2941          83 :         return;
    2942             : 
    2943             :     /*
    2944             :      * If subquery has regular DISTINCT (not DISTINCT ON), we're wasting our
    2945             :      * time: all its output columns must be used in the distinctClause.
    2946             :      */
    2947         771 :     if (subquery->distinctClause && !subquery->hasDistinctOn)
    2948          15 :         return;
    2949             : 
    2950             :     /*
    2951             :      * Collect a bitmap of all the output column numbers used by the upper
    2952             :      * query.
    2953             :      *
    2954             :      * Add all the attributes needed for joins or final output.  Note: we must
    2955             :      * look at rel's targetlist, not the attr_needed data, because attr_needed
    2956             :      * isn't computed for inheritance child rels, cf set_append_rel_size().
    2957             :      * (XXX might be worth changing that sometime.)
    2958             :      */
    2959         756 :     pull_varattnos((Node *) rel->reltarget->exprs, rel->relid, &attrs_used);
    2960             : 
    2961             :     /* Add all the attributes used by un-pushed-down restriction clauses. */
    2962         842 :     foreach(lc, rel->baserestrictinfo)
    2963             :     {
    2964          86 :         RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
    2965             : 
    2966          86 :         pull_varattnos((Node *) rinfo->clause, rel->relid, &attrs_used);
    2967             :     }
    2968             : 
    2969             :     /*
    2970             :      * If there's a whole-row reference to the subquery, we can't remove
    2971             :      * anything.
    2972             :      */
    2973         756 :     if (bms_is_member(0 - FirstLowInvalidHeapAttributeNumber, attrs_used))
    2974          30 :         return;
    2975             : 
    2976             :     /*
    2977             :      * Run through the tlist and zap entries we don't need.  It's okay to
    2978             :      * modify the tlist items in-place because set_subquery_pathlist made a
    2979             :      * copy of the subquery.
    2980             :      */
    2981        2429 :     foreach(lc, subquery->targetList)
    2982             :     {
    2983        1703 :         TargetEntry *tle = (TargetEntry *) lfirst(lc);
    2984        1703 :         Node       *texpr = (Node *) tle->expr;
    2985             : 
    2986             :         /*
    2987             :          * If it has a sortgroupref number, it's used in some sort/group
    2988             :          * clause so we'd better not remove it.  Also, don't remove any
    2989             :          * resjunk columns, since their reason for being has nothing to do
    2990             :          * with anybody reading the subquery's output.  (It's likely that
    2991             :          * resjunk columns in a sub-SELECT would always have ressortgroupref
    2992             :          * set, but even if they don't, it seems imprudent to remove them.)
    2993             :          */
    2994        1703 :         if (tle->ressortgroupref || tle->resjunk)
    2995         132 :             continue;
    2996             : 
    2997             :         /*
    2998             :          * If it's used by the upper query, we can't remove it.
    2999             :          */
    3000        1571 :         if (bms_is_member(tle->resno - FirstLowInvalidHeapAttributeNumber,
    3001             :                           attrs_used))
    3002        1293 :             continue;
    3003             : 
    3004             :         /*
    3005             :          * If it contains a set-returning function, we can't remove it since
    3006             :          * that could change the number of rows returned by the subquery.
    3007             :          */
    3008         298 :         if (subquery->hasTargetSRFs &&
    3009          20 :             expression_returns_set(texpr))
    3010           2 :             continue;
    3011             : 
    3012             :         /*
    3013             :          * If it contains volatile functions, we daren't remove it for fear
    3014             :          * that the user is expecting their side-effects to happen.
    3015             :          */
    3016         276 :         if (contain_volatile_functions(texpr))
    3017           2 :             continue;
    3018             : 
    3019             :         /*
    3020             :          * OK, we don't need it.  Replace the expression with a NULL constant.
    3021             :          * Preserve the exposed type of the expression, in case something
    3022             :          * looks at the rowtype of the subquery's result.
    3023             :          */
    3024         274 :         tle->expr = (Expr *) makeNullConst(exprType(texpr),
    3025             :                                            exprTypmod(texpr),
    3026             :                                            exprCollation(texpr));
    3027             :     }
    3028             : }
    3029             : 
    3030             : /*
    3031             :  * create_partial_bitmap_paths
    3032             :  *    Build partial bitmap heap path for the relation
    3033             :  */
    3034             : void
    3035        4852 : create_partial_bitmap_paths(PlannerInfo *root, RelOptInfo *rel,
    3036             :                             Path *bitmapqual)
    3037             : {
    3038             :     int         parallel_workers;
    3039             :     double      pages_fetched;
    3040             : 
    3041             :     /* Compute heap pages for bitmap heap scan */
    3042        4852 :     pages_fetched = compute_bitmap_pages(root, rel, bitmapqual, 1.0,
    3043             :                                          NULL, NULL);
    3044             : 
    3045        4852 :     parallel_workers = compute_parallel_worker(rel, pages_fetched, -1);
    3046             : 
    3047        4852 :     if (parallel_workers <= 0)
    3048        9561 :         return;
    3049             : 
    3050         143 :     add_partial_path(rel, (Path *) create_bitmap_heap_path(root, rel,
    3051             :                                                            bitmapqual, rel->lateral_relids, 1.0, parallel_workers));
    3052             : }
    3053             : 
    3054             : /*
    3055             :  * Compute the number of parallel workers that should be used to scan a
    3056             :  * relation.  We compute the parallel workers based on the size of the heap to
    3057             :  * be scanned and the size of the index to be scanned, then choose a minimum
    3058             :  * of those.
    3059             :  *
    3060             :  * "heap_pages" is the number of pages from the table that we expect to scan, or
    3061             :  * -1 if we don't expect to scan any.
    3062             :  *
    3063             :  * "index_pages" is the number of pages from the index that we expect to scan, or
    3064             :  * -1 if we don't expect to scan any.
    3065             :  */
    3066             : int
    3067       21924 : compute_parallel_worker(RelOptInfo *rel, double heap_pages, double index_pages)
    3068             : {
    3069       21924 :     int         parallel_workers = 0;
    3070             : 
    3071             :     /*
    3072             :      * If the user has set the parallel_workers reloption, use that; otherwise
    3073             :      * select a default number of workers.
    3074             :      */
    3075       21924 :     if (rel->rel_parallel_workers != -1)
    3076          61 :         parallel_workers = rel->rel_parallel_workers;
    3077             :     else
    3078             :     {
    3079             :         /*
    3080             :          * If the number of pages being scanned is insufficient to justify a
    3081             :          * parallel scan, just return zero ... unless it's an inheritance
    3082             :          * child. In that case, we want to generate a parallel path here
    3083             :          * anyway.  It might not be worthwhile just for this relation, but
    3084             :          * when combined with all of its inheritance siblings it may well pay
    3085             :          * off.
    3086             :          */
    3087       21863 :         if (rel->reloptkind == RELOPT_BASEREL &&
    3088       19891 :             ((heap_pages >= 0 && heap_pages < min_parallel_table_scan_size) ||
    3089        1010 :              (index_pages >= 0 && index_pages < min_parallel_index_scan_size)))
    3090       20892 :             return 0;
    3091             : 
    3092         971 :         if (heap_pages >= 0)
    3093             :         {
    3094             :             int         heap_parallel_threshold;
    3095         829 :             int         heap_parallel_workers = 1;
    3096             : 
    3097             :             /*
    3098             :              * Select the number of workers based on the log of the size of
    3099             :              * the relation.  This probably needs to be a good deal more
    3100             :              * sophisticated, but we need something here for now.  Note that
    3101             :              * the upper limit of the min_parallel_table_scan_size GUC is
    3102             :              * chosen to prevent overflow here.
    3103             :              */
    3104         829 :             heap_parallel_threshold = Max(min_parallel_table_scan_size, 1);
    3105        1690 :             while (heap_pages >= (BlockNumber) (heap_parallel_threshold * 3))
    3106             :             {
    3107          32 :                 heap_parallel_workers++;
    3108          32 :                 heap_parallel_threshold *= 3;
    3109          32 :                 if (heap_parallel_threshold > INT_MAX / 3)
    3110           0 :                     break;      /* avoid overflow */
    3111             :             }
    3112             : 
    3113         829 :             parallel_workers = heap_parallel_workers;
    3114             :         }
    3115             : 
    3116         971 :         if (index_pages >= 0)
    3117             :         {
    3118         217 :             int         index_parallel_workers = 1;
    3119             :             int         index_parallel_threshold;
    3120             : 
    3121             :             /* same calculation as for heap_pages above */
    3122         217 :             index_parallel_threshold = Max(min_parallel_index_scan_size, 1);
    3123         434 :             while (index_pages >= (BlockNumber) (index_parallel_threshold * 3))
    3124             :             {
    3125           0 :                 index_parallel_workers++;
    3126           0 :                 index_parallel_threshold *= 3;
    3127           0 :                 if (index_parallel_threshold > INT_MAX / 3)
    3128           0 :                     break;      /* avoid overflow */
    3129             :             }
    3130             : 
    3131         217 :             if (parallel_workers > 0)
    3132          75 :                 parallel_workers = Min(parallel_workers, index_parallel_workers);
    3133             :             else
    3134         142 :                 parallel_workers = index_parallel_workers;
    3135             :         }
    3136             :     }
    3137             : 
    3138             :     /*
    3139             :      * In no case use more than max_parallel_workers_per_gather workers.
    3140             :      */
    3141        1032 :     parallel_workers = Min(parallel_workers, max_parallel_workers_per_gather);
    3142             : 
    3143        1032 :     return parallel_workers;
    3144             : }
    3145             : 
    3146             : 
    3147             : /*****************************************************************************
    3148             :  *          DEBUG SUPPORT
    3149             :  *****************************************************************************/
    3150             : 
    3151             : #ifdef OPTIMIZER_DEBUG
    3152             : 
    3153             : static void
    3154             : print_relids(PlannerInfo *root, Relids relids)
    3155             : {
    3156             :     int         x;
    3157             :     bool        first = true;
    3158             : 
    3159             :     x = -1;
    3160             :     while ((x = bms_next_member(relids, x)) >= 0)
    3161             :     {
    3162             :         if (!first)
    3163             :             printf(" ");
    3164             :         if (x < root->simple_rel_array_size &&
    3165             :             root->simple_rte_array[x])
    3166             :             printf("%s", root->simple_rte_array[x]->eref->aliasname);
    3167             :         else
    3168             :             printf("%d", x);
    3169             :         first = false;
    3170             :     }
    3171             : }
    3172             : 
    3173             : static void
    3174             : print_restrictclauses(PlannerInfo *root, List *clauses)
    3175             : {
    3176             :     ListCell   *l;
    3177             : 
    3178             :     foreach(l, clauses)
    3179             :     {
    3180             :         RestrictInfo *c = lfirst(l);
    3181             : 
    3182             :         print_expr((Node *) c->clause, root->parse->rtable);
    3183             :         if (lnext(l))
    3184             :             printf(", ");
    3185             :     }
    3186             : }
    3187             : 
    3188             : static void
    3189             : print_path(PlannerInfo *root, Path *path, int indent)
    3190             : {
    3191             :     const char *ptype;
    3192             :     bool        join = false;
    3193             :     Path       *subpath = NULL;
    3194             :     int         i;
    3195             : 
    3196             :     switch (nodeTag(path))
    3197             :     {
    3198             :         case T_Path:
    3199             :             switch (path->pathtype)
    3200             :             {
    3201             :                 case T_SeqScan:
    3202             :                     ptype = "SeqScan";
    3203             :                     break;
    3204             :                 case T_SampleScan:
    3205             :                     ptype = "SampleScan";
    3206             :                     break;
    3207             :                 case T_SubqueryScan:
    3208             :                     ptype = "SubqueryScan";
    3209             :                     break;
    3210             :                 case T_FunctionScan:
    3211             :                     ptype = "FunctionScan";
    3212             :                     break;
    3213             :                 case T_TableFuncScan:
    3214             :                     ptype = "TableFuncScan";
    3215             :                     break;
    3216             :                 case T_ValuesScan:
    3217             :                     ptype = "ValuesScan";
    3218             :                     break;
    3219             :                 case T_CteScan:
    3220             :                     ptype = "CteScan";
    3221             :                     break;
    3222             :                 case T_WorkTableScan:
    3223             :                     ptype = "WorkTableScan";
    3224             :                     break;
    3225             :                 default:
    3226             :                     ptype = "???Path";
    3227             :                     break;
    3228             :             }
    3229             :             break;
    3230             :         case T_IndexPath:
    3231             :             ptype = "IdxScan";
    3232             :             break;
    3233             :         case T_BitmapHeapPath:
    3234             :             ptype = "BitmapHeapScan";
    3235             :             break;
    3236             :         case T_BitmapAndPath:
    3237             :             ptype = "BitmapAndPath";
    3238             :             break;
    3239             :         case T_BitmapOrPath:
    3240             :             ptype = "BitmapOrPath";
    3241             :             break;
    3242             :         case T_TidPath:
    3243             :             ptype = "TidScan";
    3244             :             break;
    3245             :         case T_SubqueryScanPath:
    3246             :             ptype = "SubqueryScanScan";
    3247             :             break;
    3248             :         case T_ForeignPath:
    3249             :             ptype = "ForeignScan";
    3250             :             break;
    3251             :         case T_AppendPath:
    3252             :             ptype = "Append";
    3253             :             break;
    3254             :         case T_MergeAppendPath:
    3255             :             ptype = "MergeAppend";
    3256             :             break;
    3257             :         case T_ResultPath:
    3258             :             ptype = "Result";
    3259             :             break;
    3260             :         case T_MaterialPath:
    3261             :             ptype = "Material";
    3262             :             subpath = ((MaterialPath *) path)->subpath;
    3263             :             break;
    3264             :         case T_UniquePath:
    3265             :             ptype = "Unique";
    3266             :             subpath = ((UniquePath *) path)->subpath;
    3267             :             break;
    3268             :         case T_GatherPath:
    3269             :             ptype = "Gather";
    3270             :             subpath = ((GatherPath *) path)->subpath;
    3271             :             break;
    3272             :         case T_ProjectionPath:
    3273             :             ptype = "Projection";
    3274             :             subpath = ((ProjectionPath *) path)->subpath;
    3275             :             break;
    3276             :         case T_ProjectSetPath:
    3277             :             ptype = "ProjectSet";
    3278             :             subpath = ((ProjectSetPath *) path)->subpath;
    3279             :             break;
    3280             :         case T_SortPath:
    3281             :             ptype = "Sort";
    3282             :             subpath = ((SortPath *) path)->subpath;
    3283             :             break;
    3284             :         case T_GroupPath:
    3285             :             ptype = "Group";
    3286             :             subpath = ((GroupPath *) path)->subpath;
    3287             :             break;
    3288             :         case T_UpperUniquePath:
    3289             :             ptype = "UpperUnique";
    3290             :             subpath = ((UpperUniquePath *) path)->subpath;
    3291             :             break;
    3292             :         case T_AggPath:
    3293             :             ptype = "Agg";
    3294             :             subpath = ((AggPath *) path)->subpath;
    3295             :             break;
    3296             :         case T_GroupingSetsPath:
    3297             :             ptype = "GroupingSets";
    3298             :             subpath = ((GroupingSetsPath *) path)->subpath;
    3299             :             break;
    3300             :         case T_MinMaxAggPath:
    3301             :             ptype = "MinMaxAgg";
    3302             :             break;
    3303             :         case T_WindowAggPath:
    3304             :             ptype = "WindowAgg";
    3305             :             subpath = ((WindowAggPath *) path)->subpath;
    3306             :             break;
    3307             :         case T_SetOpPath:
    3308             :             ptype = "SetOp";
    3309             :             subpath = ((SetOpPath *) path)->subpath;
    3310             :             break;
    3311             :         case T_RecursiveUnionPath:
    3312             :             ptype = "RecursiveUnion";
    3313             :             break;
    3314             :         case T_LockRowsPath:
    3315             :             ptype = "LockRows";
    3316             :             subpath = ((LockRowsPath *) path)->subpath;
    3317             :             break;
    3318             :         case T_ModifyTablePath:
    3319             :             ptype = "ModifyTable";
    3320             :             break;
    3321             :         case T_LimitPath:
    3322             :             ptype = "Limit";
    3323             :             subpath = ((LimitPath *) path)->subpath;
    3324             :             break;
    3325             :         case T_NestPath:
    3326             :             ptype = "NestLoop";
    3327             :             join = true;
    3328             :             break;
    3329             :         case T_MergePath:
    3330             :             ptype = "MergeJoin";
    3331             :             join = true;
    3332             :             break;
    3333             :         case T_HashPath:
    3334             :             ptype = "HashJoin";
    3335             :             join = true;
    3336             :             break;
    3337             :         default:
    3338             :             ptype = "???Path";
    3339             :             break;
    3340             :     }
    3341             : 
    3342             :     for (i = 0; i < indent; i++)
    3343             :         printf("\t");
    3344             :     printf("%s", ptype);
    3345             : 
    3346             :     if (path->parent)
    3347             :     {
    3348             :         printf("(");
    3349             :         print_relids(root, path->parent->relids);
    3350             :         printf(")");
    3351             :     }
    3352             :     if (path->param_info)
    3353             :     {
    3354             :         printf(" required_outer (");
    3355             :         print_relids(root, path->param_info->ppi_req_outer);
    3356             :         printf(")");
    3357             :     }
    3358             :     printf(" rows=%.0f cost=%.2f..%.2f\n",
    3359             :            path->rows, path->startup_cost, path->total_cost);
    3360             : 
    3361             :     if (path->pathkeys)
    3362             :     {
    3363             :         for (i = 0; i < indent; i++)
    3364             :             printf("\t");
    3365             :         printf("  pathkeys: ");
    3366             :         print_pathkeys(path->pathkeys, root->parse->rtable);
    3367             :     }
    3368             : 
    3369             :     if (join)
    3370             :     {
    3371             :         JoinPath   *jp = (JoinPath *) path;
    3372             : 
    3373             :         for (i = 0; i < indent; i++)
    3374             :             printf("\t");
    3375             :         printf("  clauses: ");
    3376             :         print_restrictclauses(root, jp->joinrestrictinfo);
    3377             :         printf("\n");
    3378             : 
    3379             :         if (IsA(path, MergePath))
    3380             :         {
    3381             :             MergePath  *mp = (MergePath *) path;
    3382             : 
    3383             :             for (i = 0; i < indent; i++)
    3384             :                 printf("\t");
    3385             :             printf("  sortouter=%d sortinner=%d materializeinner=%d\n",
    3386             :                    ((mp->outersortkeys) ? 1 : 0),
    3387             :                    ((mp->innersortkeys) ? 1 : 0),
    3388             :                    ((mp->materialize_inner) ? 1 : 0));
    3389             :         }
    3390             : 
    3391             :         print_path(root, jp->outerjoinpath, indent + 1);
    3392             :         print_path(root, jp->innerjoinpath, indent + 1);
    3393             :     }
    3394             : 
    3395             :     if (subpath)
    3396             :         print_path(root, subpath, indent + 1);
    3397             : }
    3398             : 
    3399             : void
    3400             : debug_print_rel(PlannerInfo *root, RelOptInfo *rel)
    3401             : {
    3402             :     ListCell   *l;
    3403             : 
    3404             :     printf("RELOPTINFO (");
    3405             :     print_relids(root, rel->relids);
    3406             :     printf("): rows=%.0f width=%d\n", rel->rows, rel->reltarget->width);
    3407             : 
    3408             :     if (rel->baserestrictinfo)
    3409             :     {
    3410             :         printf("\tbaserestrictinfo: ");
    3411             :         print_restrictclauses(root, rel->baserestrictinfo);
    3412             :         printf("\n");
    3413             :     }
    3414             : 
    3415             :     if (rel->joininfo)
    3416             :     {
    3417             :         printf("\tjoininfo: ");
    3418             :         print_restrictclauses(root, rel->joininfo);
    3419             :         printf("\n");
    3420             :     }
    3421             : 
    3422             :     printf("\tpath list:\n");
    3423             :     foreach(l, rel->pathlist)
    3424             :         print_path(root, lfirst(l), 1);
    3425             :     if (rel->cheapest_parameterized_paths)
    3426             :     {
    3427             :         printf("\n\tcheapest parameterized paths:\n");
    3428             :         foreach(l, rel->cheapest_parameterized_paths)
    3429             :             print_path(root, lfirst(l), 1);
    3430             :     }
    3431             :     if (rel->cheapest_startup_path)
    3432             :     {
    3433             :         printf("\n\tcheapest startup path:\n");
    3434             :         print_path(root, rel->cheapest_startup_path, 1);
    3435             :     }
    3436             :     if (rel->cheapest_total_path)
    3437             :     {
    3438             :         printf("\n\tcheapest total path:\n");
    3439             :         print_path(root, rel->cheapest_total_path, 1);
    3440             :     }
    3441             :     printf("\n");
    3442             :     fflush(stdout);
    3443             : }
    3444             : 
    3445             : #endif                          /* OPTIMIZER_DEBUG */

Generated by: LCOV version 1.11