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

Generated by: LCOV version 1.13