LCOV - code coverage report
Current view: top level - src/backend/optimizer/plan - createplan.c (source / functions) Hit Total Coverage
Test: PostgreSQL 12devel Lines: 2008 2095 95.8 %
Date: 2019-04-23 21:21:41 Functions: 106 107 99.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * createplan.c
       4             :  *    Routines to create the desired plan for processing a query.
       5             :  *    Planning is complete, we just need to convert the selected
       6             :  *    Path into a Plan.
       7             :  *
       8             :  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
       9             :  * Portions Copyright (c) 1994, Regents of the University of California
      10             :  *
      11             :  *
      12             :  * IDENTIFICATION
      13             :  *    src/backend/optimizer/plan/createplan.c
      14             :  *
      15             :  *-------------------------------------------------------------------------
      16             :  */
      17             : #include "postgres.h"
      18             : 
      19             : #include <limits.h>
      20             : #include <math.h>
      21             : 
      22             : #include "access/sysattr.h"
      23             : #include "catalog/pg_class.h"
      24             : #include "foreign/fdwapi.h"
      25             : #include "miscadmin.h"
      26             : #include "nodes/extensible.h"
      27             : #include "nodes/makefuncs.h"
      28             : #include "nodes/nodeFuncs.h"
      29             : #include "optimizer/clauses.h"
      30             : #include "optimizer/cost.h"
      31             : #include "optimizer/optimizer.h"
      32             : #include "optimizer/paramassign.h"
      33             : #include "optimizer/paths.h"
      34             : #include "optimizer/placeholder.h"
      35             : #include "optimizer/plancat.h"
      36             : #include "optimizer/planmain.h"
      37             : #include "optimizer/restrictinfo.h"
      38             : #include "optimizer/subselect.h"
      39             : #include "optimizer/tlist.h"
      40             : #include "parser/parse_clause.h"
      41             : #include "parser/parsetree.h"
      42             : #include "partitioning/partprune.h"
      43             : #include "utils/lsyscache.h"
      44             : 
      45             : 
      46             : /*
      47             :  * Flag bits that can appear in the flags argument of create_plan_recurse().
      48             :  * These can be OR-ed together.
      49             :  *
      50             :  * CP_EXACT_TLIST specifies that the generated plan node must return exactly
      51             :  * the tlist specified by the path's pathtarget (this overrides both
      52             :  * CP_SMALL_TLIST and CP_LABEL_TLIST, if those are set).  Otherwise, the
      53             :  * plan node is allowed to return just the Vars and PlaceHolderVars needed
      54             :  * to evaluate the pathtarget.
      55             :  *
      56             :  * CP_SMALL_TLIST specifies that a narrower tlist is preferred.  This is
      57             :  * passed down by parent nodes such as Sort and Hash, which will have to
      58             :  * store the returned tuples.
      59             :  *
      60             :  * CP_LABEL_TLIST specifies that the plan node must return columns matching
      61             :  * any sortgrouprefs specified in its pathtarget, with appropriate
      62             :  * ressortgroupref labels.  This is passed down by parent nodes such as Sort
      63             :  * and Group, which need these values to be available in their inputs.
      64             :  *
      65             :  * CP_IGNORE_TLIST specifies that the caller plans to replace the targetlist,
      66             :  * and therefore it doesn't matter a bit what target list gets generated.
      67             :  */
      68             : #define CP_EXACT_TLIST      0x0001  /* Plan must return specified tlist */
      69             : #define CP_SMALL_TLIST      0x0002  /* Prefer narrower tlists */
      70             : #define CP_LABEL_TLIST      0x0004  /* tlist must contain sortgrouprefs */
      71             : #define CP_IGNORE_TLIST     0x0008  /* caller will replace tlist */
      72             : 
      73             : 
      74             : static Plan *create_plan_recurse(PlannerInfo *root, Path *best_path,
      75             :                     int flags);
      76             : static Plan *create_scan_plan(PlannerInfo *root, Path *best_path,
      77             :                  int flags);
      78             : static List *build_path_tlist(PlannerInfo *root, Path *path);
      79             : static bool use_physical_tlist(PlannerInfo *root, Path *path, int flags);
      80             : static List *get_gating_quals(PlannerInfo *root, List *quals);
      81             : static Plan *create_gating_plan(PlannerInfo *root, Path *path, Plan *plan,
      82             :                    List *gating_quals);
      83             : static Plan *create_join_plan(PlannerInfo *root, JoinPath *best_path);
      84             : static Plan *create_append_plan(PlannerInfo *root, AppendPath *best_path);
      85             : static Plan *create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path);
      86             : static Result *create_group_result_plan(PlannerInfo *root,
      87             :                          GroupResultPath *best_path);
      88             : static ProjectSet *create_project_set_plan(PlannerInfo *root, ProjectSetPath *best_path);
      89             : static Material *create_material_plan(PlannerInfo *root, MaterialPath *best_path,
      90             :                      int flags);
      91             : static Plan *create_unique_plan(PlannerInfo *root, UniquePath *best_path,
      92             :                    int flags);
      93             : static Gather *create_gather_plan(PlannerInfo *root, GatherPath *best_path);
      94             : static Plan *create_projection_plan(PlannerInfo *root,
      95             :                        ProjectionPath *best_path,
      96             :                        int flags);
      97             : static Plan *inject_projection_plan(Plan *subplan, List *tlist, bool parallel_safe);
      98             : static Sort *create_sort_plan(PlannerInfo *root, SortPath *best_path, int flags);
      99             : static Group *create_group_plan(PlannerInfo *root, GroupPath *best_path);
     100             : static Unique *create_upper_unique_plan(PlannerInfo *root, UpperUniquePath *best_path,
     101             :                          int flags);
     102             : static Agg *create_agg_plan(PlannerInfo *root, AggPath *best_path);
     103             : static Plan *create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path);
     104             : static Result *create_minmaxagg_plan(PlannerInfo *root, MinMaxAggPath *best_path);
     105             : static WindowAgg *create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path);
     106             : static SetOp *create_setop_plan(PlannerInfo *root, SetOpPath *best_path,
     107             :                   int flags);
     108             : static RecursiveUnion *create_recursiveunion_plan(PlannerInfo *root, RecursiveUnionPath *best_path);
     109             : static LockRows *create_lockrows_plan(PlannerInfo *root, LockRowsPath *best_path,
     110             :                      int flags);
     111             : static ModifyTable *create_modifytable_plan(PlannerInfo *root, ModifyTablePath *best_path);
     112             : static Limit *create_limit_plan(PlannerInfo *root, LimitPath *best_path,
     113             :                   int flags);
     114             : static SeqScan *create_seqscan_plan(PlannerInfo *root, Path *best_path,
     115             :                     List *tlist, List *scan_clauses);
     116             : static SampleScan *create_samplescan_plan(PlannerInfo *root, Path *best_path,
     117             :                        List *tlist, List *scan_clauses);
     118             : static Scan *create_indexscan_plan(PlannerInfo *root, IndexPath *best_path,
     119             :                       List *tlist, List *scan_clauses, bool indexonly);
     120             : static BitmapHeapScan *create_bitmap_scan_plan(PlannerInfo *root,
     121             :                         BitmapHeapPath *best_path,
     122             :                         List *tlist, List *scan_clauses);
     123             : static Plan *create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
     124             :                       List **qual, List **indexqual, List **indexECs);
     125             : static void bitmap_subplan_mark_shared(Plan *plan);
     126             : static TidScan *create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
     127             :                     List *tlist, List *scan_clauses);
     128             : static SubqueryScan *create_subqueryscan_plan(PlannerInfo *root,
     129             :                          SubqueryScanPath *best_path,
     130             :                          List *tlist, List *scan_clauses);
     131             : static FunctionScan *create_functionscan_plan(PlannerInfo *root, Path *best_path,
     132             :                          List *tlist, List *scan_clauses);
     133             : static ValuesScan *create_valuesscan_plan(PlannerInfo *root, Path *best_path,
     134             :                        List *tlist, List *scan_clauses);
     135             : static TableFuncScan *create_tablefuncscan_plan(PlannerInfo *root, Path *best_path,
     136             :                           List *tlist, List *scan_clauses);
     137             : static CteScan *create_ctescan_plan(PlannerInfo *root, Path *best_path,
     138             :                     List *tlist, List *scan_clauses);
     139             : static NamedTuplestoreScan *create_namedtuplestorescan_plan(PlannerInfo *root,
     140             :                                 Path *best_path, List *tlist, List *scan_clauses);
     141             : static Result *create_resultscan_plan(PlannerInfo *root, Path *best_path,
     142             :                        List *tlist, List *scan_clauses);
     143             : static WorkTableScan *create_worktablescan_plan(PlannerInfo *root, Path *best_path,
     144             :                           List *tlist, List *scan_clauses);
     145             : static ForeignScan *create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
     146             :                         List *tlist, List *scan_clauses);
     147             : static CustomScan *create_customscan_plan(PlannerInfo *root,
     148             :                        CustomPath *best_path,
     149             :                        List *tlist, List *scan_clauses);
     150             : static NestLoop *create_nestloop_plan(PlannerInfo *root, NestPath *best_path);
     151             : static MergeJoin *create_mergejoin_plan(PlannerInfo *root, MergePath *best_path);
     152             : static HashJoin *create_hashjoin_plan(PlannerInfo *root, HashPath *best_path);
     153             : static Node *replace_nestloop_params(PlannerInfo *root, Node *expr);
     154             : static Node *replace_nestloop_params_mutator(Node *node, PlannerInfo *root);
     155             : static void fix_indexqual_references(PlannerInfo *root, IndexPath *index_path,
     156             :                          List **stripped_indexquals_p,
     157             :                          List **fixed_indexquals_p);
     158             : static List *fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path);
     159             : static Node *fix_indexqual_clause(PlannerInfo *root,
     160             :                      IndexOptInfo *index, int indexcol,
     161             :                      Node *clause, List *indexcolnos);
     162             : static Node *fix_indexqual_operand(Node *node, IndexOptInfo *index, int indexcol);
     163             : static List *get_switched_clauses(List *clauses, Relids outerrelids);
     164             : static List *order_qual_clauses(PlannerInfo *root, List *clauses);
     165             : static void copy_generic_path_info(Plan *dest, Path *src);
     166             : static void copy_plan_costsize(Plan *dest, Plan *src);
     167             : static void label_sort_with_costsize(PlannerInfo *root, Sort *plan,
     168             :                          double limit_tuples);
     169             : static SeqScan *make_seqscan(List *qptlist, List *qpqual, Index scanrelid);
     170             : static SampleScan *make_samplescan(List *qptlist, List *qpqual, Index scanrelid,
     171             :                 TableSampleClause *tsc);
     172             : static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid,
     173             :                Oid indexid, List *indexqual, List *indexqualorig,
     174             :                List *indexorderby, List *indexorderbyorig,
     175             :                List *indexorderbyops,
     176             :                ScanDirection indexscandir);
     177             : static IndexOnlyScan *make_indexonlyscan(List *qptlist, List *qpqual,
     178             :                    Index scanrelid, Oid indexid,
     179             :                    List *indexqual, List *indexorderby,
     180             :                    List *indextlist,
     181             :                    ScanDirection indexscandir);
     182             : static BitmapIndexScan *make_bitmap_indexscan(Index scanrelid, Oid indexid,
     183             :                       List *indexqual,
     184             :                       List *indexqualorig);
     185             : static BitmapHeapScan *make_bitmap_heapscan(List *qptlist,
     186             :                      List *qpqual,
     187             :                      Plan *lefttree,
     188             :                      List *bitmapqualorig,
     189             :                      Index scanrelid);
     190             : static TidScan *make_tidscan(List *qptlist, List *qpqual, Index scanrelid,
     191             :              List *tidquals);
     192             : static SubqueryScan *make_subqueryscan(List *qptlist,
     193             :                   List *qpqual,
     194             :                   Index scanrelid,
     195             :                   Plan *subplan);
     196             : static FunctionScan *make_functionscan(List *qptlist, List *qpqual,
     197             :                   Index scanrelid, List *functions, bool funcordinality);
     198             : static ValuesScan *make_valuesscan(List *qptlist, List *qpqual,
     199             :                 Index scanrelid, List *values_lists);
     200             : static TableFuncScan *make_tablefuncscan(List *qptlist, List *qpqual,
     201             :                    Index scanrelid, TableFunc *tablefunc);
     202             : static CteScan *make_ctescan(List *qptlist, List *qpqual,
     203             :              Index scanrelid, int ctePlanId, int cteParam);
     204             : static NamedTuplestoreScan *make_namedtuplestorescan(List *qptlist, List *qpqual,
     205             :                          Index scanrelid, char *enrname);
     206             : static WorkTableScan *make_worktablescan(List *qptlist, List *qpqual,
     207             :                    Index scanrelid, int wtParam);
     208             : static RecursiveUnion *make_recursive_union(List *tlist,
     209             :                      Plan *lefttree,
     210             :                      Plan *righttree,
     211             :                      int wtParam,
     212             :                      List *distinctList,
     213             :                      long numGroups);
     214             : static BitmapAnd *make_bitmap_and(List *bitmapplans);
     215             : static BitmapOr *make_bitmap_or(List *bitmapplans);
     216             : static NestLoop *make_nestloop(List *tlist,
     217             :               List *joinclauses, List *otherclauses, List *nestParams,
     218             :               Plan *lefttree, Plan *righttree,
     219             :               JoinType jointype, bool inner_unique);
     220             : static HashJoin *make_hashjoin(List *tlist,
     221             :               List *joinclauses, List *otherclauses,
     222             :               List *hashclauses,
     223             :               Plan *lefttree, Plan *righttree,
     224             :               JoinType jointype, bool inner_unique);
     225             : static Hash *make_hash(Plan *lefttree,
     226             :           Oid skewTable,
     227             :           AttrNumber skewColumn,
     228             :           bool skewInherit);
     229             : static MergeJoin *make_mergejoin(List *tlist,
     230             :                List *joinclauses, List *otherclauses,
     231             :                List *mergeclauses,
     232             :                Oid *mergefamilies,
     233             :                Oid *mergecollations,
     234             :                int *mergestrategies,
     235             :                bool *mergenullsfirst,
     236             :                Plan *lefttree, Plan *righttree,
     237             :                JoinType jointype, bool inner_unique,
     238             :                bool skip_mark_restore);
     239             : static Sort *make_sort(Plan *lefttree, int numCols,
     240             :           AttrNumber *sortColIdx, Oid *sortOperators,
     241             :           Oid *collations, bool *nullsFirst);
     242             : static Plan *prepare_sort_from_pathkeys(Plan *lefttree, List *pathkeys,
     243             :                            Relids relids,
     244             :                            const AttrNumber *reqColIdx,
     245             :                            bool adjust_tlist_in_place,
     246             :                            int *p_numsortkeys,
     247             :                            AttrNumber **p_sortColIdx,
     248             :                            Oid **p_sortOperators,
     249             :                            Oid **p_collations,
     250             :                            bool **p_nullsFirst);
     251             : static EquivalenceMember *find_ec_member_for_tle(EquivalenceClass *ec,
     252             :                        TargetEntry *tle,
     253             :                        Relids relids);
     254             : static Sort *make_sort_from_pathkeys(Plan *lefttree, List *pathkeys,
     255             :                         Relids relids);
     256             : static Sort *make_sort_from_groupcols(List *groupcls,
     257             :                          AttrNumber *grpColIdx,
     258             :                          Plan *lefttree);
     259             : static Material *make_material(Plan *lefttree);
     260             : static WindowAgg *make_windowagg(List *tlist, Index winref,
     261             :                int partNumCols, AttrNumber *partColIdx, Oid *partOperators, Oid *partCollations,
     262             :                int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators, Oid *ordCollations,
     263             :                int frameOptions, Node *startOffset, Node *endOffset,
     264             :                Oid startInRangeFunc, Oid endInRangeFunc,
     265             :                Oid inRangeColl, bool inRangeAsc, bool inRangeNullsFirst,
     266             :                Plan *lefttree);
     267             : static Group *make_group(List *tlist, List *qual, int numGroupCols,
     268             :            AttrNumber *grpColIdx, Oid *grpOperators, Oid *grpCollations,
     269             :            Plan *lefttree);
     270             : static Unique *make_unique_from_sortclauses(Plan *lefttree, List *distinctList);
     271             : static Unique *make_unique_from_pathkeys(Plan *lefttree,
     272             :                           List *pathkeys, int numCols);
     273             : static Gather *make_gather(List *qptlist, List *qpqual,
     274             :             int nworkers, int rescan_param, bool single_copy, Plan *subplan);
     275             : static SetOp *make_setop(SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree,
     276             :            List *distinctList, AttrNumber flagColIdx, int firstFlag,
     277             :            long numGroups);
     278             : static LockRows *make_lockrows(Plan *lefttree, List *rowMarks, int epqParam);
     279             : static Result *make_result(List *tlist, Node *resconstantqual, Plan *subplan);
     280             : static ProjectSet *make_project_set(List *tlist, Plan *subplan);
     281             : static ModifyTable *make_modifytable(PlannerInfo *root,
     282             :                  CmdType operation, bool canSetTag,
     283             :                  Index nominalRelation, Index rootRelation,
     284             :                  bool partColsUpdated,
     285             :                  List *resultRelations, List *subplans, List *subroots,
     286             :                  List *withCheckOptionLists, List *returningLists,
     287             :                  List *rowMarks, OnConflictExpr *onconflict, int epqParam);
     288             : static GatherMerge *create_gather_merge_plan(PlannerInfo *root,
     289             :                          GatherMergePath *best_path);
     290             : 
     291             : 
     292             : /*
     293             :  * create_plan
     294             :  *    Creates the access plan for a query by recursively processing the
     295             :  *    desired tree of pathnodes, starting at the node 'best_path'.  For
     296             :  *    every pathnode found, we create a corresponding plan node containing
     297             :  *    appropriate id, target list, and qualification information.
     298             :  *
     299             :  *    The tlists and quals in the plan tree are still in planner format,
     300             :  *    ie, Vars still correspond to the parser's numbering.  This will be
     301             :  *    fixed later by setrefs.c.
     302             :  *
     303             :  *    best_path is the best access path
     304             :  *
     305             :  *    Returns a Plan tree.
     306             :  */
     307             : Plan *
     308      287944 : create_plan(PlannerInfo *root, Path *best_path)
     309             : {
     310             :     Plan       *plan;
     311             : 
     312             :     /* plan_params should not be in use in current query level */
     313             :     Assert(root->plan_params == NIL);
     314             : 
     315             :     /* Initialize this module's workspace in PlannerInfo */
     316      287944 :     root->curOuterRels = NULL;
     317      287944 :     root->curOuterParams = NIL;
     318             : 
     319             :     /* Recursively process the path tree, demanding the correct tlist result */
     320      287944 :     plan = create_plan_recurse(root, best_path, CP_EXACT_TLIST);
     321             : 
     322             :     /*
     323             :      * Make sure the topmost plan node's targetlist exposes the original
     324             :      * column names and other decorative info.  Targetlists generated within
     325             :      * the planner don't bother with that stuff, but we must have it on the
     326             :      * top-level tlist seen at execution time.  However, ModifyTable plan
     327             :      * nodes don't have a tlist matching the querytree targetlist.
     328             :      */
     329      287824 :     if (!IsA(plan, ModifyTable))
     330      218508 :         apply_tlist_labeling(plan->targetlist, root->processed_tlist);
     331             : 
     332             :     /*
     333             :      * Attach any initPlans created in this query level to the topmost plan
     334             :      * node.  (In principle the initplans could go in any plan node at or
     335             :      * above where they're referenced, but there seems no reason to put them
     336             :      * any lower than the topmost node for the query level.  Also, see
     337             :      * comments for SS_finalize_plan before you try to change this.)
     338             :      */
     339      287824 :     SS_attach_initplans(root, plan);
     340             : 
     341             :     /* Check we successfully assigned all NestLoopParams to plan nodes */
     342      287824 :     if (root->curOuterParams != NIL)
     343           0 :         elog(ERROR, "failed to assign all NestLoopParams to plan nodes");
     344             : 
     345             :     /*
     346             :      * Reset plan_params to ensure param IDs used for nestloop params are not
     347             :      * re-used later
     348             :      */
     349      287824 :     root->plan_params = NIL;
     350             : 
     351      287824 :     return plan;
     352             : }
     353             : 
     354             : /*
     355             :  * create_plan_recurse
     356             :  *    Recursive guts of create_plan().
     357             :  */
     358             : static Plan *
     359      767576 : create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
     360             : {
     361             :     Plan       *plan;
     362             : 
     363             :     /* Guard against stack overflow due to overly complex plans */
     364      767576 :     check_stack_depth();
     365             : 
     366      767576 :     switch (best_path->pathtype)
     367             :     {
     368             :         case T_SeqScan:
     369             :         case T_SampleScan:
     370             :         case T_IndexScan:
     371             :         case T_IndexOnlyScan:
     372             :         case T_BitmapHeapScan:
     373             :         case T_TidScan:
     374             :         case T_SubqueryScan:
     375             :         case T_FunctionScan:
     376             :         case T_TableFuncScan:
     377             :         case T_ValuesScan:
     378             :         case T_CteScan:
     379             :         case T_WorkTableScan:
     380             :         case T_NamedTuplestoreScan:
     381             :         case T_ForeignScan:
     382             :         case T_CustomScan:
     383      249986 :             plan = create_scan_plan(root, best_path, flags);
     384      249986 :             break;
     385             :         case T_HashJoin:
     386             :         case T_MergeJoin:
     387             :         case T_NestLoop:
     388       58258 :             plan = create_join_plan(root,
     389             :                                     (JoinPath *) best_path);
     390       58258 :             break;
     391             :         case T_Append:
     392        6740 :             plan = create_append_plan(root,
     393             :                                       (AppendPath *) best_path);
     394        6740 :             break;
     395             :         case T_MergeAppend:
     396         232 :             plan = create_merge_append_plan(root,
     397             :                                             (MergeAppendPath *) best_path);
     398         232 :             break;
     399             :         case T_Result:
     400      314324 :             if (IsA(best_path, ProjectionPath))
     401             :             {
     402      205772 :                 plan = create_projection_plan(root,
     403             :                                               (ProjectionPath *) best_path,
     404             :                                               flags);
     405             :             }
     406      108552 :             else if (IsA(best_path, MinMaxAggPath))
     407             :             {
     408         336 :                 plan = (Plan *) create_minmaxagg_plan(root,
     409             :                                                       (MinMaxAggPath *) best_path);
     410             :             }
     411      108216 :             else if (IsA(best_path, GroupResultPath))
     412             :             {
     413      107690 :                 plan = (Plan *) create_group_result_plan(root,
     414             :                                                          (GroupResultPath *) best_path);
     415             :             }
     416             :             else
     417             :             {
     418             :                 /* Simple RTE_RESULT base relation */
     419             :                 Assert(IsA(best_path, Path));
     420         526 :                 plan = create_scan_plan(root, best_path, flags);
     421             :             }
     422      314324 :             break;
     423             :         case T_ProjectSet:
     424        3578 :             plan = (Plan *) create_project_set_plan(root,
     425             :                                                     (ProjectSetPath *) best_path);
     426        3578 :             break;
     427             :         case T_Material:
     428        2656 :             plan = (Plan *) create_material_plan(root,
     429             :                                                  (MaterialPath *) best_path,
     430             :                                                  flags);
     431        2656 :             break;
     432             :         case T_Unique:
     433         512 :             if (IsA(best_path, UpperUniquePath))
     434             :             {
     435         316 :                 plan = (Plan *) create_upper_unique_plan(root,
     436             :                                                          (UpperUniquePath *) best_path,
     437             :                                                          flags);
     438             :             }
     439             :             else
     440             :             {
     441             :                 Assert(IsA(best_path, UniquePath));
     442         196 :                 plan = create_unique_plan(root,
     443             :                                           (UniquePath *) best_path,
     444             :                                           flags);
     445             :             }
     446         512 :             break;
     447             :         case T_Gather:
     448         524 :             plan = (Plan *) create_gather_plan(root,
     449             :                                                (GatherPath *) best_path);
     450         524 :             break;
     451             :         case T_Sort:
     452       27392 :             plan = (Plan *) create_sort_plan(root,
     453             :                                              (SortPath *) best_path,
     454             :                                              flags);
     455       27392 :             break;
     456             :         case T_Group:
     457         106 :             plan = (Plan *) create_group_plan(root,
     458             :                                               (GroupPath *) best_path);
     459         106 :             break;
     460             :         case T_Agg:
     461       24580 :             if (IsA(best_path, GroupingSetsPath))
     462         404 :                 plan = create_groupingsets_plan(root,
     463             :                                                 (GroupingSetsPath *) best_path);
     464             :             else
     465             :             {
     466             :                 Assert(IsA(best_path, AggPath));
     467       24176 :                 plan = (Plan *) create_agg_plan(root,
     468             :                                                 (AggPath *) best_path);
     469             :             }
     470       24580 :             break;
     471             :         case T_WindowAgg:
     472        1124 :             plan = (Plan *) create_windowagg_plan(root,
     473             :                                                   (WindowAggPath *) best_path);
     474        1124 :             break;
     475             :         case T_SetOp:
     476         396 :             plan = (Plan *) create_setop_plan(root,
     477             :                                               (SetOpPath *) best_path,
     478             :                                               flags);
     479         396 :             break;
     480             :         case T_RecursiveUnion:
     481         326 :             plan = (Plan *) create_recursiveunion_plan(root,
     482             :                                                        (RecursiveUnionPath *) best_path);
     483         326 :             break;
     484             :         case T_LockRows:
     485        4718 :             plan = (Plan *) create_lockrows_plan(root,
     486             :                                                  (LockRowsPath *) best_path,
     487             :                                                  flags);
     488        4718 :             break;
     489             :         case T_ModifyTable:
     490       69436 :             plan = (Plan *) create_modifytable_plan(root,
     491             :                                                     (ModifyTablePath *) best_path);
     492       69316 :             break;
     493             :         case T_Limit:
     494        2552 :             plan = (Plan *) create_limit_plan(root,
     495             :                                               (LimitPath *) best_path,
     496             :                                               flags);
     497        2552 :             break;
     498             :         case T_GatherMerge:
     499         136 :             plan = (Plan *) create_gather_merge_plan(root,
     500             :                                                      (GatherMergePath *) best_path);
     501         136 :             break;
     502             :         default:
     503           0 :             elog(ERROR, "unrecognized node type: %d",
     504             :                  (int) best_path->pathtype);
     505             :             plan = NULL;        /* keep compiler quiet */
     506             :             break;
     507             :     }
     508             : 
     509      767456 :     return plan;
     510             : }
     511             : 
     512             : /*
     513             :  * create_scan_plan
     514             :  *   Create a scan plan for the parent relation of 'best_path'.
     515             :  */
     516             : static Plan *
     517      250512 : create_scan_plan(PlannerInfo *root, Path *best_path, int flags)
     518             : {
     519      250512 :     RelOptInfo *rel = best_path->parent;
     520             :     List       *scan_clauses;
     521             :     List       *gating_clauses;
     522             :     List       *tlist;
     523             :     Plan       *plan;
     524             : 
     525             :     /*
     526             :      * Extract the relevant restriction clauses from the parent relation. The
     527             :      * executor must apply all these restrictions during the scan, except for
     528             :      * pseudoconstants which we'll take care of below.
     529             :      *
     530             :      * If this is a plain indexscan or index-only scan, we need not consider
     531             :      * restriction clauses that are implied by the index's predicate, so use
     532             :      * indrestrictinfo not baserestrictinfo.  Note that we can't do that for
     533             :      * bitmap indexscans, since there's not necessarily a single index
     534             :      * involved; but it doesn't matter since create_bitmap_scan_plan() will be
     535             :      * able to get rid of such clauses anyway via predicate proof.
     536             :      */
     537      250512 :     switch (best_path->pathtype)
     538             :     {
     539             :         case T_IndexScan:
     540             :         case T_IndexOnlyScan:
     541       75156 :             scan_clauses = castNode(IndexPath, best_path)->indexinfo->indrestrictinfo;
     542       75156 :             break;
     543             :         default:
     544      175356 :             scan_clauses = rel->baserestrictinfo;
     545      175356 :             break;
     546             :     }
     547             : 
     548             :     /*
     549             :      * If this is a parameterized scan, we also need to enforce all the join
     550             :      * clauses available from the outer relation(s).
     551             :      *
     552             :      * For paranoia's sake, don't modify the stored baserestrictinfo list.
     553             :      */
     554      250512 :     if (best_path->param_info)
     555       17070 :         scan_clauses = list_concat(list_copy(scan_clauses),
     556       17070 :                                    best_path->param_info->ppi_clauses);
     557             : 
     558             :     /*
     559             :      * Detect whether we have any pseudoconstant quals to deal with.  Then, if
     560             :      * we'll need a gating Result node, it will be able to project, so there
     561             :      * are no requirements on the child's tlist.
     562             :      */
     563      250512 :     gating_clauses = get_gating_quals(root, scan_clauses);
     564      250512 :     if (gating_clauses)
     565        2368 :         flags = 0;
     566             : 
     567             :     /*
     568             :      * For table scans, rather than using the relation targetlist (which is
     569             :      * only those Vars actually needed by the query), we prefer to generate a
     570             :      * tlist containing all Vars in order.  This will allow the executor to
     571             :      * optimize away projection of the table tuples, if possible.
     572             :      *
     573             :      * But if the caller is going to ignore our tlist anyway, then don't
     574             :      * bother generating one at all.  We use an exact equality test here, so
     575             :      * that this only applies when CP_IGNORE_TLIST is the only flag set.
     576             :      */
     577      250512 :     if (flags == CP_IGNORE_TLIST)
     578             :     {
     579       67224 :         tlist = NULL;
     580             :     }
     581      183288 :     else if (use_physical_tlist(root, best_path, flags))
     582             :     {
     583       76076 :         if (best_path->pathtype == T_IndexOnlyScan)
     584             :         {
     585             :             /* For index-only scan, the preferred tlist is the index's */
     586        2802 :             tlist = copyObject(((IndexPath *) best_path)->indexinfo->indextlist);
     587             : 
     588             :             /*
     589             :              * Transfer sortgroupref data to the replacement tlist, if
     590             :              * requested (use_physical_tlist checked that this will work).
     591             :              */
     592        2802 :             if (flags & CP_LABEL_TLIST)
     593        1364 :                 apply_pathtarget_labeling_to_tlist(tlist, best_path->pathtarget);
     594             :         }
     595             :         else
     596             :         {
     597       73274 :             tlist = build_physical_tlist(root, rel);
     598       73274 :             if (tlist == NIL)
     599             :             {
     600             :                 /* Failed because of dropped cols, so use regular method */
     601         144 :                 tlist = build_path_tlist(root, best_path);
     602             :             }
     603             :             else
     604             :             {
     605             :                 /* As above, transfer sortgroupref data to replacement tlist */
     606       73130 :                 if (flags & CP_LABEL_TLIST)
     607        7386 :                     apply_pathtarget_labeling_to_tlist(tlist, best_path->pathtarget);
     608             :             }
     609             :         }
     610             :     }
     611             :     else
     612             :     {
     613      107212 :         tlist = build_path_tlist(root, best_path);
     614             :     }
     615             : 
     616      250512 :     switch (best_path->pathtype)
     617             :     {
     618             :         case T_SeqScan:
     619      112344 :             plan = (Plan *) create_seqscan_plan(root,
     620             :                                                 best_path,
     621             :                                                 tlist,
     622             :                                                 scan_clauses);
     623      112344 :             break;
     624             : 
     625             :         case T_SampleScan:
     626         180 :             plan = (Plan *) create_samplescan_plan(root,
     627             :                                                    best_path,
     628             :                                                    tlist,
     629             :                                                    scan_clauses);
     630         180 :             break;
     631             : 
     632             :         case T_IndexScan:
     633       68836 :             plan = (Plan *) create_indexscan_plan(root,
     634             :                                                   (IndexPath *) best_path,
     635             :                                                   tlist,
     636             :                                                   scan_clauses,
     637             :                                                   false);
     638       68836 :             break;
     639             : 
     640             :         case T_IndexOnlyScan:
     641        6320 :             plan = (Plan *) create_indexscan_plan(root,
     642             :                                                   (IndexPath *) best_path,
     643             :                                                   tlist,
     644             :                                                   scan_clauses,
     645             :                                                   true);
     646        6320 :             break;
     647             : 
     648             :         case T_BitmapHeapScan:
     649       18902 :             plan = (Plan *) create_bitmap_scan_plan(root,
     650             :                                                     (BitmapHeapPath *) best_path,
     651             :                                                     tlist,
     652             :                                                     scan_clauses);
     653       18902 :             break;
     654             : 
     655             :         case T_TidScan:
     656         404 :             plan = (Plan *) create_tidscan_plan(root,
     657             :                                                 (TidPath *) best_path,
     658             :                                                 tlist,
     659             :                                                 scan_clauses);
     660         404 :             break;
     661             : 
     662             :         case T_SubqueryScan:
     663        8088 :             plan = (Plan *) create_subqueryscan_plan(root,
     664             :                                                      (SubqueryScanPath *) best_path,
     665             :                                                      tlist,
     666             :                                                      scan_clauses);
     667        8088 :             break;
     668             : 
     669             :         case T_FunctionScan:
     670       28204 :             plan = (Plan *) create_functionscan_plan(root,
     671             :                                                      best_path,
     672             :                                                      tlist,
     673             :                                                      scan_clauses);
     674       28204 :             break;
     675             : 
     676             :         case T_TableFuncScan:
     677         140 :             plan = (Plan *) create_tablefuncscan_plan(root,
     678             :                                                       best_path,
     679             :                                                       tlist,
     680             :                                                       scan_clauses);
     681         140 :             break;
     682             : 
     683             :         case T_ValuesScan:
     684        3730 :             plan = (Plan *) create_valuesscan_plan(root,
     685             :                                                    best_path,
     686             :                                                    tlist,
     687             :                                                    scan_clauses);
     688        3730 :             break;
     689             : 
     690             :         case T_CteScan:
     691         868 :             plan = (Plan *) create_ctescan_plan(root,
     692             :                                                 best_path,
     693             :                                                 tlist,
     694             :                                                 scan_clauses);
     695         868 :             break;
     696             : 
     697             :         case T_NamedTuplestoreScan:
     698         260 :             plan = (Plan *) create_namedtuplestorescan_plan(root,
     699             :                                                             best_path,
     700             :                                                             tlist,
     701             :                                                             scan_clauses);
     702         260 :             break;
     703             : 
     704             :         case T_Result:
     705         526 :             plan = (Plan *) create_resultscan_plan(root,
     706             :                                                    best_path,
     707             :                                                    tlist,
     708             :                                                    scan_clauses);
     709         526 :             break;
     710             : 
     711             :         case T_WorkTableScan:
     712         326 :             plan = (Plan *) create_worktablescan_plan(root,
     713             :                                                       best_path,
     714             :                                                       tlist,
     715             :                                                       scan_clauses);
     716         326 :             break;
     717             : 
     718             :         case T_ForeignScan:
     719        1384 :             plan = (Plan *) create_foreignscan_plan(root,
     720             :                                                     (ForeignPath *) best_path,
     721             :                                                     tlist,
     722             :                                                     scan_clauses);
     723        1384 :             break;
     724             : 
     725             :         case T_CustomScan:
     726           0 :             plan = (Plan *) create_customscan_plan(root,
     727             :                                                    (CustomPath *) best_path,
     728             :                                                    tlist,
     729             :                                                    scan_clauses);
     730           0 :             break;
     731             : 
     732             :         default:
     733           0 :             elog(ERROR, "unrecognized node type: %d",
     734             :                  (int) best_path->pathtype);
     735             :             plan = NULL;        /* keep compiler quiet */
     736             :             break;
     737             :     }
     738             : 
     739             :     /*
     740             :      * If there are any pseudoconstant clauses attached to this node, insert a
     741             :      * gating Result node that evaluates the pseudoconstants as one-time
     742             :      * quals.
     743             :      */
     744      250512 :     if (gating_clauses)
     745        2368 :         plan = create_gating_plan(root, best_path, plan, gating_clauses);
     746             : 
     747      250512 :     return plan;
     748             : }
     749             : 
     750             : /*
     751             :  * Build a target list (ie, a list of TargetEntry) for the Path's output.
     752             :  *
     753             :  * This is almost just make_tlist_from_pathtarget(), but we also have to
     754             :  * deal with replacing nestloop params.
     755             :  */
     756             : static List *
     757      519236 : build_path_tlist(PlannerInfo *root, Path *path)
     758             : {
     759      519236 :     List       *tlist = NIL;
     760      519236 :     Index      *sortgrouprefs = path->pathtarget->sortgrouprefs;
     761      519236 :     int         resno = 1;
     762             :     ListCell   *v;
     763             : 
     764     2062878 :     foreach(v, path->pathtarget->exprs)
     765             :     {
     766     1543642 :         Node       *node = (Node *) lfirst(v);
     767             :         TargetEntry *tle;
     768             : 
     769             :         /*
     770             :          * If it's a parameterized path, there might be lateral references in
     771             :          * the tlist, which need to be replaced with Params.  There's no need
     772             :          * to remake the TargetEntry nodes, so apply this to each list item
     773             :          * separately.
     774             :          */
     775     1543642 :         if (path->param_info)
     776       10912 :             node = replace_nestloop_params(root, node);
     777             : 
     778     1543642 :         tle = makeTargetEntry((Expr *) node,
     779             :                               resno,
     780             :                               NULL,
     781             :                               false);
     782     1543642 :         if (sortgrouprefs)
     783     1033068 :             tle->ressortgroupref = sortgrouprefs[resno - 1];
     784             : 
     785     1543642 :         tlist = lappend(tlist, tle);
     786     1543642 :         resno++;
     787             :     }
     788      519236 :     return tlist;
     789             : }
     790             : 
     791             : /*
     792             :  * use_physical_tlist
     793             :  *      Decide whether to use a tlist matching relation structure,
     794             :  *      rather than only those Vars actually referenced.
     795             :  */
     796             : static bool
     797      389060 : use_physical_tlist(PlannerInfo *root, Path *path, int flags)
     798             : {
     799      389060 :     RelOptInfo *rel = path->parent;
     800             :     int         i;
     801             :     ListCell   *lc;
     802             : 
     803             :     /*
     804             :      * Forget it if either exact tlist or small tlist is demanded.
     805             :      */
     806      389060 :     if (flags & (CP_EXACT_TLIST | CP_SMALL_TLIST))
     807      295952 :         return false;
     808             : 
     809             :     /*
     810             :      * We can do this for real relation scans, subquery scans, function scans,
     811             :      * tablefunc scans, values scans, and CTE scans (but not for, eg, joins).
     812             :      */
     813      106346 :     if (rel->rtekind != RTE_RELATION &&
     814       25324 :         rel->rtekind != RTE_SUBQUERY &&
     815       16720 :         rel->rtekind != RTE_FUNCTION &&
     816        9172 :         rel->rtekind != RTE_TABLEFUNC &&
     817        8392 :         rel->rtekind != RTE_VALUES &&
     818        3854 :         rel->rtekind != RTE_CTE)
     819        3648 :         return false;
     820             : 
     821             :     /*
     822             :      * Can't do it with inheritance cases either (mainly because Append
     823             :      * doesn't project; this test may be unnecessary now that
     824             :      * create_append_plan instructs its children to return an exact tlist).
     825             :      */
     826       89460 :     if (rel->reloptkind != RELOPT_BASEREL)
     827        1426 :         return false;
     828             : 
     829             :     /*
     830             :      * Also, don't do it to a CustomPath; the premise that we're extracting
     831             :      * columns from a simple physical tuple is unlikely to hold for those.
     832             :      * (When it does make sense, the custom path creator can set up the path's
     833             :      * pathtarget that way.)
     834             :      */
     835       88034 :     if (IsA(path, CustomPath))
     836           0 :         return false;
     837             : 
     838             :     /*
     839             :      * If a bitmap scan's tlist is empty, keep it as-is.  This may allow the
     840             :      * executor to skip heap page fetches, and in any case, the benefit of
     841             :      * using a physical tlist instead would be minimal.
     842             :      */
     843       92858 :     if (IsA(path, BitmapHeapPath) &&
     844        4824 :         path->pathtarget->exprs == NIL)
     845        1466 :         return false;
     846             : 
     847             :     /*
     848             :      * Can't do it if any system columns or whole-row Vars are requested.
     849             :      * (This could possibly be fixed but would take some fragile assumptions
     850             :      * in setrefs.c, I think.)
     851             :      */
     852      596330 :     for (i = rel->min_attr; i <= 0; i++)
     853             :     {
     854      519698 :         if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
     855        9936 :             return false;
     856             :     }
     857             : 
     858             :     /*
     859             :      * Can't do it if the rel is required to emit any placeholder expressions,
     860             :      * either.
     861             :      */
     862       77128 :     foreach(lc, root->placeholder_list)
     863             :     {
     864         578 :         PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
     865             : 
     866        1132 :         if (bms_nonempty_difference(phinfo->ph_needed, rel->relids) &&
     867         554 :             bms_is_subset(phinfo->ph_eval_at, rel->relids))
     868          82 :             return false;
     869             :     }
     870             : 
     871             :     /*
     872             :      * Also, can't do it if CP_LABEL_TLIST is specified and path is requested
     873             :      * to emit any sort/group columns that are not simple Vars.  (If they are
     874             :      * simple Vars, they should appear in the physical tlist, and
     875             :      * apply_pathtarget_labeling_to_tlist will take care of getting them
     876             :      * labeled again.)  We also have to check that no two sort/group columns
     877             :      * are the same Var, else that element of the physical tlist would need
     878             :      * conflicting ressortgroupref labels.
     879             :      */
     880       76550 :     if ((flags & CP_LABEL_TLIST) && path->pathtarget->sortgrouprefs)
     881             :     {
     882        1014 :         Bitmapset  *sortgroupatts = NULL;
     883             : 
     884        1014 :         i = 0;
     885        2656 :         foreach(lc, path->pathtarget->exprs)
     886             :         {
     887        1824 :             Expr       *expr = (Expr *) lfirst(lc);
     888             : 
     889        1824 :             if (path->pathtarget->sortgrouprefs[i])
     890             :             {
     891        1408 :                 if (expr && IsA(expr, Var))
     892        1226 :                 {
     893        1236 :                     int         attno = ((Var *) expr)->varattno;
     894             : 
     895        1236 :                     attno -= FirstLowInvalidHeapAttributeNumber;
     896        1236 :                     if (bms_is_member(attno, sortgroupatts))
     897          10 :                         return false;
     898        1226 :                     sortgroupatts = bms_add_member(sortgroupatts, attno);
     899             :                 }
     900             :                 else
     901         172 :                     return false;
     902             :             }
     903        1642 :             i++;
     904             :         }
     905             :     }
     906             : 
     907       76368 :     return true;
     908             : }
     909             : 
     910             : /*
     911             :  * get_gating_quals
     912             :  *    See if there are pseudoconstant quals in a node's quals list
     913             :  *
     914             :  * If the node's quals list includes any pseudoconstant quals,
     915             :  * return just those quals.
     916             :  */
     917             : static List *
     918      308770 : get_gating_quals(PlannerInfo *root, List *quals)
     919             : {
     920             :     /* No need to look if we know there are no pseudoconstants */
     921      308770 :     if (!root->hasPseudoConstantQuals)
     922      306130 :         return NIL;
     923             : 
     924             :     /* Sort into desirable execution order while still in RestrictInfo form */
     925        2640 :     quals = order_qual_clauses(root, quals);
     926             : 
     927             :     /* Pull out any pseudoconstant quals from the RestrictInfo list */
     928        2640 :     return extract_actual_clauses(quals, true);
     929             : }
     930             : 
     931             : /*
     932             :  * create_gating_plan
     933             :  *    Deal with pseudoconstant qual clauses
     934             :  *
     935             :  * Add a gating Result node atop the already-built plan.
     936             :  */
     937             : static Plan *
     938        2380 : create_gating_plan(PlannerInfo *root, Path *path, Plan *plan,
     939             :                    List *gating_quals)
     940             : {
     941             :     Plan       *gplan;
     942             :     Plan       *splan;
     943             : 
     944             :     Assert(gating_quals);
     945             : 
     946             :     /*
     947             :      * We might have a trivial Result plan already.  Stacking one Result atop
     948             :      * another is silly, so if that applies, just discard the input plan.
     949             :      * (We're assuming its targetlist is uninteresting; it should be either
     950             :      * the same as the result of build_path_tlist, or a simplified version.)
     951             :      */
     952        2380 :     splan = plan;
     953        2380 :     if (IsA(plan, Result))
     954             :     {
     955          24 :         Result     *rplan = (Result *) plan;
     956             : 
     957          48 :         if (rplan->plan.lefttree == NULL &&
     958          24 :             rplan->resconstantqual == NULL)
     959          24 :             splan = NULL;
     960             :     }
     961             : 
     962             :     /*
     963             :      * Since we need a Result node anyway, always return the path's requested
     964             :      * tlist; that's never a wrong choice, even if the parent node didn't ask
     965             :      * for CP_EXACT_TLIST.
     966             :      */
     967        2380 :     gplan = (Plan *) make_result(build_path_tlist(root, path),
     968             :                                  (Node *) gating_quals,
     969             :                                  splan);
     970             : 
     971             :     /*
     972             :      * Notice that we don't change cost or size estimates when doing gating.
     973             :      * The costs of qual eval were already included in the subplan's cost.
     974             :      * Leaving the size alone amounts to assuming that the gating qual will
     975             :      * succeed, which is the conservative estimate for planning upper queries.
     976             :      * We certainly don't want to assume the output size is zero (unless the
     977             :      * gating qual is actually constant FALSE, and that case is dealt with in
     978             :      * clausesel.c).  Interpolating between the two cases is silly, because it
     979             :      * doesn't reflect what will really happen at runtime, and besides which
     980             :      * in most cases we have only a very bad idea of the probability of the
     981             :      * gating qual being true.
     982             :      */
     983        2380 :     copy_plan_costsize(gplan, plan);
     984             : 
     985             :     /* Gating quals could be unsafe, so better use the Path's safety flag */
     986        2380 :     gplan->parallel_safe = path->parallel_safe;
     987             : 
     988        2380 :     return gplan;
     989             : }
     990             : 
     991             : /*
     992             :  * create_join_plan
     993             :  *    Create a join plan for 'best_path' and (recursively) plans for its
     994             :  *    inner and outer paths.
     995             :  */
     996             : static Plan *
     997       58258 : create_join_plan(PlannerInfo *root, JoinPath *best_path)
     998             : {
     999             :     Plan       *plan;
    1000             :     List       *gating_clauses;
    1001             : 
    1002       58258 :     switch (best_path->path.pathtype)
    1003             :     {
    1004             :         case T_MergeJoin:
    1005        1926 :             plan = (Plan *) create_mergejoin_plan(root,
    1006             :                                                   (MergePath *) best_path);
    1007        1926 :             break;
    1008             :         case T_HashJoin:
    1009       26406 :             plan = (Plan *) create_hashjoin_plan(root,
    1010             :                                                  (HashPath *) best_path);
    1011       26406 :             break;
    1012             :         case T_NestLoop:
    1013       29926 :             plan = (Plan *) create_nestloop_plan(root,
    1014             :                                                  (NestPath *) best_path);
    1015       29926 :             break;
    1016             :         default:
    1017           0 :             elog(ERROR, "unrecognized node type: %d",
    1018             :                  (int) best_path->path.pathtype);
    1019             :             plan = NULL;        /* keep compiler quiet */
    1020             :             break;
    1021             :     }
    1022             : 
    1023             :     /*
    1024             :      * If there are any pseudoconstant clauses attached to this node, insert a
    1025             :      * gating Result node that evaluates the pseudoconstants as one-time
    1026             :      * quals.
    1027             :      */
    1028       58258 :     gating_clauses = get_gating_quals(root, best_path->joinrestrictinfo);
    1029       58258 :     if (gating_clauses)
    1030          12 :         plan = create_gating_plan(root, (Path *) best_path, plan,
    1031             :                                   gating_clauses);
    1032             : 
    1033             : #ifdef NOT_USED
    1034             : 
    1035             :     /*
    1036             :      * * Expensive function pullups may have pulled local predicates * into
    1037             :      * this path node.  Put them in the qpqual of the plan node. * JMH,
    1038             :      * 6/15/92
    1039             :      */
    1040             :     if (get_loc_restrictinfo(best_path) != NIL)
    1041             :         set_qpqual((Plan) plan,
    1042             :                    list_concat(get_qpqual((Plan) plan),
    1043             :                                get_actual_clauses(get_loc_restrictinfo(best_path))));
    1044             : #endif
    1045             : 
    1046       58258 :     return plan;
    1047             : }
    1048             : 
    1049             : /*
    1050             :  * create_append_plan
    1051             :  *    Create an Append plan for 'best_path' and (recursively) plans
    1052             :  *    for its subpaths.
    1053             :  *
    1054             :  *    Returns a Plan node.
    1055             :  */
    1056             : static Plan *
    1057        6740 : create_append_plan(PlannerInfo *root, AppendPath *best_path)
    1058             : {
    1059             :     Append     *plan;
    1060        6740 :     List       *tlist = build_path_tlist(root, &best_path->path);
    1061        6740 :     List       *pathkeys = best_path->path.pathkeys;
    1062        6740 :     List       *subplans = NIL;
    1063             :     ListCell   *subpaths;
    1064        6740 :     RelOptInfo *rel = best_path->path.parent;
    1065        6740 :     PartitionPruneInfo *partpruneinfo = NULL;
    1066        6740 :     int         nodenumsortkeys = 0;
    1067        6740 :     AttrNumber *nodeSortColIdx = NULL;
    1068        6740 :     Oid        *nodeSortOperators = NULL;
    1069        6740 :     Oid        *nodeCollations = NULL;
    1070        6740 :     bool       *nodeNullsFirst = NULL;
    1071             : 
    1072             :     /*
    1073             :      * The subpaths list could be empty, if every child was proven empty by
    1074             :      * constraint exclusion.  In that case generate a dummy plan that returns
    1075             :      * no rows.
    1076             :      *
    1077             :      * Note that an AppendPath with no members is also generated in certain
    1078             :      * cases where there was no appending construct at all, but we know the
    1079             :      * relation is empty (see set_dummy_rel_pathlist and mark_dummy_rel).
    1080             :      */
    1081        6740 :     if (best_path->subpaths == NIL)
    1082             :     {
    1083             :         /* Generate a Result plan with constant-FALSE gating qual */
    1084             :         Plan       *plan;
    1085             : 
    1086         328 :         plan = (Plan *) make_result(tlist,
    1087         328 :                                     (Node *) list_make1(makeBoolConst(false,
    1088             :                                                                       false)),
    1089             :                                     NULL);
    1090             : 
    1091         328 :         copy_generic_path_info(plan, (Path *) best_path);
    1092             : 
    1093         328 :         return plan;
    1094             :     }
    1095             : 
    1096             :     /*
    1097             :      * Otherwise build an Append plan.  Note that if there's just one child,
    1098             :      * the Append is pretty useless; but we wait till setrefs.c to get rid of
    1099             :      * it.  Doing so here doesn't work because the varno of the child scan
    1100             :      * plan won't match the parent-rel Vars it'll be asked to emit.
    1101             :      *
    1102             :      * We don't have the actual creation of the Append node split out into a
    1103             :      * separate make_xxx function.  This is because we want to run
    1104             :      * prepare_sort_from_pathkeys on it before we do so on the individual
    1105             :      * child plans, to make cross-checking the sort info easier.
    1106             :      */
    1107        6412 :     plan = makeNode(Append);
    1108        6412 :     plan->plan.targetlist = tlist;
    1109        6412 :     plan->plan.qual = NIL;
    1110        6412 :     plan->plan.lefttree = NULL;
    1111        6412 :     plan->plan.righttree = NULL;
    1112             : 
    1113        6412 :     if (pathkeys != NIL)
    1114             :     {
    1115             :         /* Compute sort column info, and adjust the Append's tlist as needed */
    1116         104 :         (void) prepare_sort_from_pathkeys((Plan *) plan, pathkeys,
    1117         104 :                                           best_path->path.parent->relids,
    1118             :                                           NULL,
    1119             :                                           true,
    1120             :                                           &nodenumsortkeys,
    1121             :                                           &nodeSortColIdx,
    1122             :                                           &nodeSortOperators,
    1123             :                                           &nodeCollations,
    1124             :                                           &nodeNullsFirst);
    1125             :     }
    1126             : 
    1127             :     /* Build the plan for each child */
    1128       23598 :     foreach(subpaths, best_path->subpaths)
    1129             :     {
    1130       17186 :         Path       *subpath = (Path *) lfirst(subpaths);
    1131             :         Plan       *subplan;
    1132             : 
    1133             :         /* Must insist that all children return the same tlist */
    1134       17186 :         subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
    1135             : 
    1136             :         /*
    1137             :          * For ordered Appends, we must insert a Sort node if subplan isn't
    1138             :          * sufficiently ordered.
    1139             :          */
    1140       17186 :         if (pathkeys != NIL)
    1141             :         {
    1142             :             int         numsortkeys;
    1143             :             AttrNumber *sortColIdx;
    1144             :             Oid        *sortOperators;
    1145             :             Oid        *collations;
    1146             :             bool       *nullsFirst;
    1147             : 
    1148             :             /*
    1149             :              * Compute sort column info, and adjust subplan's tlist as needed.
    1150             :              * We must apply prepare_sort_from_pathkeys even to subplans that
    1151             :              * don't need an explicit sort, to make sure they are returning
    1152             :              * the same sort key columns the Append expects.
    1153             :              */
    1154         536 :             subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
    1155         268 :                                                  subpath->parent->relids,
    1156             :                                                  nodeSortColIdx,
    1157             :                                                  false,
    1158             :                                                  &numsortkeys,
    1159             :                                                  &sortColIdx,
    1160             :                                                  &sortOperators,
    1161             :                                                  &collations,
    1162             :                                                  &nullsFirst);
    1163             : 
    1164             :             /*
    1165             :              * Check that we got the same sort key information.  We just
    1166             :              * Assert that the sortops match, since those depend only on the
    1167             :              * pathkeys; but it seems like a good idea to check the sort
    1168             :              * column numbers explicitly, to ensure the tlists match up.
    1169             :              */
    1170             :             Assert(numsortkeys == nodenumsortkeys);
    1171         268 :             if (memcmp(sortColIdx, nodeSortColIdx,
    1172             :                        numsortkeys * sizeof(AttrNumber)) != 0)
    1173           0 :                 elog(ERROR, "Append child's targetlist doesn't match Append");
    1174             :             Assert(memcmp(sortOperators, nodeSortOperators,
    1175             :                           numsortkeys * sizeof(Oid)) == 0);
    1176             :             Assert(memcmp(collations, nodeCollations,
    1177             :                           numsortkeys * sizeof(Oid)) == 0);
    1178             :             Assert(memcmp(nullsFirst, nodeNullsFirst,
    1179             :                           numsortkeys * sizeof(bool)) == 0);
    1180             : 
    1181             :             /* Now, insert a Sort node if subplan isn't sufficiently ordered */
    1182         268 :             if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
    1183             :             {
    1184           4 :                 Sort       *sort = make_sort(subplan, numsortkeys,
    1185             :                                              sortColIdx, sortOperators,
    1186             :                                              collations, nullsFirst);
    1187             : 
    1188           4 :                 label_sort_with_costsize(root, sort, best_path->limit_tuples);
    1189           4 :                 subplan = (Plan *) sort;
    1190             :             }
    1191             :         }
    1192             : 
    1193       17186 :         subplans = lappend(subplans, subplan);
    1194             :     }
    1195             : 
    1196             :     /*
    1197             :      * If any quals exist, they may be useful to perform further partition
    1198             :      * pruning during execution.  Gather information needed by the executor to
    1199             :      * do partition pruning.
    1200             :      */
    1201       12812 :     if (enable_partition_pruning &&
    1202       11226 :         rel->reloptkind == RELOPT_BASEREL &&
    1203        4826 :         best_path->partitioned_rels != NIL)
    1204             :     {
    1205             :         List       *prunequal;
    1206             : 
    1207        2126 :         prunequal = extract_actual_clauses(rel->baserestrictinfo, false);
    1208             : 
    1209        2126 :         if (best_path->path.param_info)
    1210             :         {
    1211          80 :             List       *prmquals = best_path->path.param_info->ppi_clauses;
    1212             : 
    1213          80 :             prmquals = extract_actual_clauses(prmquals, false);
    1214          80 :             prmquals = (List *) replace_nestloop_params(root,
    1215             :                                                         (Node *) prmquals);
    1216             : 
    1217          80 :             prunequal = list_concat(prunequal, prmquals);
    1218             :         }
    1219             : 
    1220        2126 :         if (prunequal != NIL)
    1221        1514 :             partpruneinfo =
    1222        1514 :                 make_partition_pruneinfo(root, rel,
    1223             :                                          best_path->subpaths,
    1224             :                                          best_path->partitioned_rels,
    1225             :                                          prunequal);
    1226             :     }
    1227             : 
    1228        6412 :     plan->appendplans = subplans;
    1229        6412 :     plan->first_partial_plan = best_path->first_partial_path;
    1230        6412 :     plan->part_prune_info = partpruneinfo;
    1231             : 
    1232        6412 :     copy_generic_path_info(&plan->plan, (Path *) best_path);
    1233             : 
    1234        6412 :     return (Plan *) plan;
    1235             : }
    1236             : 
    1237             : /*
    1238             :  * create_merge_append_plan
    1239             :  *    Create a MergeAppend plan for 'best_path' and (recursively) plans
    1240             :  *    for its subpaths.
    1241             :  *
    1242             :  *    Returns a Plan node.
    1243             :  */
    1244             : static Plan *
    1245         232 : create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path)
    1246             : {
    1247         232 :     MergeAppend *node = makeNode(MergeAppend);
    1248         232 :     Plan       *plan = &node->plan;
    1249         232 :     List       *tlist = build_path_tlist(root, &best_path->path);
    1250         232 :     List       *pathkeys = best_path->path.pathkeys;
    1251         232 :     List       *subplans = NIL;
    1252             :     ListCell   *subpaths;
    1253         232 :     RelOptInfo *rel = best_path->path.parent;
    1254         232 :     PartitionPruneInfo *partpruneinfo = NULL;
    1255             : 
    1256             :     /*
    1257             :      * We don't have the actual creation of the MergeAppend node split out
    1258             :      * into a separate make_xxx function.  This is because we want to run
    1259             :      * prepare_sort_from_pathkeys on it before we do so on the individual
    1260             :      * child plans, to make cross-checking the sort info easier.
    1261             :      */
    1262         232 :     copy_generic_path_info(plan, (Path *) best_path);
    1263         232 :     plan->targetlist = tlist;
    1264         232 :     plan->qual = NIL;
    1265         232 :     plan->lefttree = NULL;
    1266         232 :     plan->righttree = NULL;
    1267             : 
    1268             :     /* Compute sort column info, and adjust MergeAppend's tlist as needed */
    1269         464 :     (void) prepare_sort_from_pathkeys(plan, pathkeys,
    1270         232 :                                       best_path->path.parent->relids,
    1271             :                                       NULL,
    1272             :                                       true,
    1273             :                                       &node->numCols,
    1274             :                                       &node->sortColIdx,
    1275             :                                       &node->sortOperators,
    1276             :                                       &node->collations,
    1277             :                                       &node->nullsFirst);
    1278             : 
    1279             :     /*
    1280             :      * Now prepare the child plans.  We must apply prepare_sort_from_pathkeys
    1281             :      * even to subplans that don't need an explicit sort, to make sure they
    1282             :      * are returning the same sort key columns the MergeAppend expects.
    1283             :      */
    1284         948 :     foreach(subpaths, best_path->subpaths)
    1285             :     {
    1286         716 :         Path       *subpath = (Path *) lfirst(subpaths);
    1287             :         Plan       *subplan;
    1288             :         int         numsortkeys;
    1289             :         AttrNumber *sortColIdx;
    1290             :         Oid        *sortOperators;
    1291             :         Oid        *collations;
    1292             :         bool       *nullsFirst;
    1293             : 
    1294             :         /* Build the child plan */
    1295             :         /* Must insist that all children return the same tlist */
    1296         716 :         subplan = create_plan_recurse(root, subpath, CP_EXACT_TLIST);
    1297             : 
    1298             :         /* Compute sort column info, and adjust subplan's tlist as needed */
    1299         716 :         subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
    1300         716 :                                              subpath->parent->relids,
    1301         716 :                                              node->sortColIdx,
    1302             :                                              false,
    1303             :                                              &numsortkeys,
    1304             :                                              &sortColIdx,
    1305             :                                              &sortOperators,
    1306             :                                              &collations,
    1307             :                                              &nullsFirst);
    1308             : 
    1309             :         /*
    1310             :          * Check that we got the same sort key information.  We just Assert
    1311             :          * that the sortops match, since those depend only on the pathkeys;
    1312             :          * but it seems like a good idea to check the sort column numbers
    1313             :          * explicitly, to ensure the tlists really do match up.
    1314             :          */
    1315             :         Assert(numsortkeys == node->numCols);
    1316         716 :         if (memcmp(sortColIdx, node->sortColIdx,
    1317             :                    numsortkeys * sizeof(AttrNumber)) != 0)
    1318           0 :             elog(ERROR, "MergeAppend child's targetlist doesn't match MergeAppend");
    1319             :         Assert(memcmp(sortOperators, node->sortOperators,
    1320             :                       numsortkeys * sizeof(Oid)) == 0);
    1321             :         Assert(memcmp(collations, node->collations,
    1322             :                       numsortkeys * sizeof(Oid)) == 0);
    1323             :         Assert(memcmp(nullsFirst, node->nullsFirst,
    1324             :                       numsortkeys * sizeof(bool)) == 0);
    1325             : 
    1326             :         /* Now, insert a Sort node if subplan isn't sufficiently ordered */
    1327         716 :         if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
    1328             :         {
    1329          40 :             Sort       *sort = make_sort(subplan, numsortkeys,
    1330             :                                          sortColIdx, sortOperators,
    1331             :                                          collations, nullsFirst);
    1332             : 
    1333          40 :             label_sort_with_costsize(root, sort, best_path->limit_tuples);
    1334          40 :             subplan = (Plan *) sort;
    1335             :         }
    1336             : 
    1337         716 :         subplans = lappend(subplans, subplan);
    1338             :     }
    1339             : 
    1340             :     /*
    1341             :      * If any quals exist, they may be useful to perform further partition
    1342             :      * pruning during execution.  Gather information needed by the executor to
    1343             :      * do partition pruning.
    1344             :      */
    1345         464 :     if (enable_partition_pruning &&
    1346         416 :         rel->reloptkind == RELOPT_BASEREL &&
    1347         184 :         best_path->partitioned_rels != NIL)
    1348             :     {
    1349             :         List       *prunequal;
    1350             : 
    1351          56 :         prunequal = extract_actual_clauses(rel->baserestrictinfo, false);
    1352             : 
    1353          56 :         if (best_path->path.param_info)
    1354             :         {
    1355           0 :             List       *prmquals = best_path->path.param_info->ppi_clauses;
    1356             : 
    1357           0 :             prmquals = extract_actual_clauses(prmquals, false);
    1358           0 :             prmquals = (List *) replace_nestloop_params(root,
    1359             :                                                         (Node *) prmquals);
    1360             : 
    1361           0 :             prunequal = list_concat(prunequal, prmquals);
    1362             :         }
    1363             : 
    1364          56 :         if (prunequal != NIL)
    1365          28 :             partpruneinfo = make_partition_pruneinfo(root, rel,
    1366             :                                                      best_path->subpaths,
    1367             :                                                      best_path->partitioned_rels,
    1368             :                                                      prunequal);
    1369             :     }
    1370             : 
    1371         232 :     node->mergeplans = subplans;
    1372         232 :     node->part_prune_info = partpruneinfo;
    1373             : 
    1374         232 :     return (Plan *) node;
    1375             : }
    1376             : 
    1377             : /*
    1378             :  * create_group_result_plan
    1379             :  *    Create a Result plan for 'best_path'.
    1380             :  *    This is only used for degenerate grouping cases.
    1381             :  *
    1382             :  *    Returns a Plan node.
    1383             :  */
    1384             : static Result *
    1385      107690 : create_group_result_plan(PlannerInfo *root, GroupResultPath *best_path)
    1386             : {
    1387             :     Result     *plan;
    1388             :     List       *tlist;
    1389             :     List       *quals;
    1390             : 
    1391      107690 :     tlist = build_path_tlist(root, &best_path->path);
    1392             : 
    1393             :     /* best_path->quals is just bare clauses */
    1394      107690 :     quals = order_qual_clauses(root, best_path->quals);
    1395             : 
    1396      107690 :     plan = make_result(tlist, (Node *) quals, NULL);
    1397             : 
    1398      107690 :     copy_generic_path_info(&plan->plan, (Path *) best_path);
    1399             : 
    1400      107690 :     return plan;
    1401             : }
    1402             : 
    1403             : /*
    1404             :  * create_project_set_plan
    1405             :  *    Create a ProjectSet plan for 'best_path'.
    1406             :  *
    1407             :  *    Returns a Plan node.
    1408             :  */
    1409             : static ProjectSet *
    1410        3578 : create_project_set_plan(PlannerInfo *root, ProjectSetPath *best_path)
    1411             : {
    1412             :     ProjectSet *plan;
    1413             :     Plan       *subplan;
    1414             :     List       *tlist;
    1415             : 
    1416             :     /* Since we intend to project, we don't need to constrain child tlist */
    1417        3578 :     subplan = create_plan_recurse(root, best_path->subpath, 0);
    1418             : 
    1419        3578 :     tlist = build_path_tlist(root, &best_path->path);
    1420             : 
    1421        3578 :     plan = make_project_set(tlist, subplan);
    1422             : 
    1423        3578 :     copy_generic_path_info(&plan->plan, (Path *) best_path);
    1424             : 
    1425        3578 :     return plan;
    1426             : }
    1427             : 
    1428             : /*
    1429             :  * create_material_plan
    1430             :  *    Create a Material plan for 'best_path' and (recursively) plans
    1431             :  *    for its subpaths.
    1432             :  *
    1433             :  *    Returns a Plan node.
    1434             :  */
    1435             : static Material *
    1436        2656 : create_material_plan(PlannerInfo *root, MaterialPath *best_path, int flags)
    1437             : {
    1438             :     Material   *plan;
    1439             :     Plan       *subplan;
    1440             : 
    1441             :     /*
    1442             :      * We don't want any excess columns in the materialized tuples, so request
    1443             :      * a smaller tlist.  Otherwise, since Material doesn't project, tlist
    1444             :      * requirements pass through.
    1445             :      */
    1446        2656 :     subplan = create_plan_recurse(root, best_path->subpath,
    1447             :                                   flags | CP_SMALL_TLIST);
    1448             : 
    1449        2656 :     plan = make_material(subplan);
    1450             : 
    1451        2656 :     copy_generic_path_info(&plan->plan, (Path *) best_path);
    1452             : 
    1453        2656 :     return plan;
    1454             : }
    1455             : 
    1456             : /*
    1457             :  * create_unique_plan
    1458             :  *    Create a Unique plan for 'best_path' and (recursively) plans
    1459             :  *    for its subpaths.
    1460             :  *
    1461             :  *    Returns a Plan node.
    1462             :  */
    1463             : static Plan *
    1464         196 : create_unique_plan(PlannerInfo *root, UniquePath *best_path, int flags)
    1465             : {
    1466             :     Plan       *plan;
    1467             :     Plan       *subplan;
    1468             :     List       *in_operators;
    1469             :     List       *uniq_exprs;
    1470             :     List       *newtlist;
    1471             :     int         nextresno;
    1472             :     bool        newitems;
    1473             :     int         numGroupCols;
    1474             :     AttrNumber *groupColIdx;
    1475             :     Oid        *groupCollations;
    1476             :     int         groupColPos;
    1477             :     ListCell   *l;
    1478             : 
    1479             :     /* Unique doesn't project, so tlist requirements pass through */
    1480         196 :     subplan = create_plan_recurse(root, best_path->subpath, flags);
    1481             : 
    1482             :     /* Done if we don't need to do any actual unique-ifying */
    1483         196 :     if (best_path->umethod == UNIQUE_PATH_NOOP)
    1484           0 :         return subplan;
    1485             : 
    1486             :     /*
    1487             :      * As constructed, the subplan has a "flat" tlist containing just the Vars
    1488             :      * needed here and at upper levels.  The values we are supposed to
    1489             :      * unique-ify may be expressions in these variables.  We have to add any
    1490             :      * such expressions to the subplan's tlist.
    1491             :      *
    1492             :      * The subplan may have a "physical" tlist if it is a simple scan plan. If
    1493             :      * we're going to sort, this should be reduced to the regular tlist, so
    1494             :      * that we don't sort more data than we need to.  For hashing, the tlist
    1495             :      * should be left as-is if we don't need to add any expressions; but if we
    1496             :      * do have to add expressions, then a projection step will be needed at
    1497             :      * runtime anyway, so we may as well remove unneeded items. Therefore
    1498             :      * newtlist starts from build_path_tlist() not just a copy of the
    1499             :      * subplan's tlist; and we don't install it into the subplan unless we are
    1500             :      * sorting or stuff has to be added.
    1501             :      */
    1502         196 :     in_operators = best_path->in_operators;
    1503         196 :     uniq_exprs = best_path->uniq_exprs;
    1504             : 
    1505             :     /* initialize modified subplan tlist as just the "required" vars */
    1506         196 :     newtlist = build_path_tlist(root, &best_path->path);
    1507         196 :     nextresno = list_length(newtlist) + 1;
    1508         196 :     newitems = false;
    1509             : 
    1510         404 :     foreach(l, uniq_exprs)
    1511             :     {
    1512         208 :         Expr       *uniqexpr = lfirst(l);
    1513             :         TargetEntry *tle;
    1514             : 
    1515         208 :         tle = tlist_member(uniqexpr, newtlist);
    1516         208 :         if (!tle)
    1517             :         {
    1518          24 :             tle = makeTargetEntry((Expr *) uniqexpr,
    1519             :                                   nextresno,
    1520             :                                   NULL,
    1521             :                                   false);
    1522          24 :             newtlist = lappend(newtlist, tle);
    1523          24 :             nextresno++;
    1524          24 :             newitems = true;
    1525             :         }
    1526             :     }
    1527             : 
    1528             :     /* Use change_plan_targetlist in case we need to insert a Result node */
    1529         196 :     if (newitems || best_path->umethod == UNIQUE_PATH_SORT)
    1530          26 :         subplan = change_plan_targetlist(subplan, newtlist,
    1531          26 :                                          best_path->path.parallel_safe);
    1532             : 
    1533             :     /*
    1534             :      * Build control information showing which subplan output columns are to
    1535             :      * be examined by the grouping step.  Unfortunately we can't merge this
    1536             :      * with the previous loop, since we didn't then know which version of the
    1537             :      * subplan tlist we'd end up using.
    1538             :      */
    1539         196 :     newtlist = subplan->targetlist;
    1540         196 :     numGroupCols = list_length(uniq_exprs);
    1541         196 :     groupColIdx = (AttrNumber *) palloc(numGroupCols * sizeof(AttrNumber));
    1542         196 :     groupCollations = (Oid *) palloc(numGroupCols * sizeof(Oid));
    1543             : 
    1544         196 :     groupColPos = 0;
    1545         404 :     foreach(l, uniq_exprs)
    1546             :     {
    1547         208 :         Expr       *uniqexpr = lfirst(l);
    1548             :         TargetEntry *tle;
    1549             : 
    1550         208 :         tle = tlist_member(uniqexpr, newtlist);
    1551         208 :         if (!tle)               /* shouldn't happen */
    1552           0 :             elog(ERROR, "failed to find unique expression in subplan tlist");
    1553         208 :         groupColIdx[groupColPos] = tle->resno;
    1554         208 :         groupCollations[groupColPos] = exprCollation((Node *) tle->expr);
    1555         208 :         groupColPos++;
    1556             :     }
    1557             : 
    1558         196 :     if (best_path->umethod == UNIQUE_PATH_HASH)
    1559             :     {
    1560             :         Oid        *groupOperators;
    1561             : 
    1562             :         /*
    1563             :          * Get the hashable equality operators for the Agg node to use.
    1564             :          * Normally these are the same as the IN clause operators, but if
    1565             :          * those are cross-type operators then the equality operators are the
    1566             :          * ones for the IN clause operators' RHS datatype.
    1567             :          */
    1568         194 :         groupOperators = (Oid *) palloc(numGroupCols * sizeof(Oid));
    1569         194 :         groupColPos = 0;
    1570         400 :         foreach(l, in_operators)
    1571             :         {
    1572         206 :             Oid         in_oper = lfirst_oid(l);
    1573             :             Oid         eq_oper;
    1574             : 
    1575         206 :             if (!get_compatible_hash_operators(in_oper, NULL, &eq_oper))
    1576           0 :                 elog(ERROR, "could not find compatible hash operator for operator %u",
    1577             :                      in_oper);
    1578         206 :             groupOperators[groupColPos++] = eq_oper;
    1579             :         }
    1580             : 
    1581             :         /*
    1582             :          * Since the Agg node is going to project anyway, we can give it the
    1583             :          * minimum output tlist, without any stuff we might have added to the
    1584             :          * subplan tlist.
    1585             :          */
    1586         194 :         plan = (Plan *) make_agg(build_path_tlist(root, &best_path->path),
    1587             :                                  NIL,
    1588             :                                  AGG_HASHED,
    1589             :                                  AGGSPLIT_SIMPLE,
    1590             :                                  numGroupCols,
    1591             :                                  groupColIdx,
    1592             :                                  groupOperators,
    1593             :                                  groupCollations,
    1594             :                                  NIL,
    1595             :                                  NIL,
    1596             :                                  best_path->path.rows,
    1597             :                                  subplan);
    1598             :     }
    1599             :     else
    1600             :     {
    1601           2 :         List       *sortList = NIL;
    1602             :         Sort       *sort;
    1603             : 
    1604             :         /* Create an ORDER BY list to sort the input compatibly */
    1605           2 :         groupColPos = 0;
    1606           4 :         foreach(l, in_operators)
    1607             :         {
    1608           2 :             Oid         in_oper = lfirst_oid(l);
    1609             :             Oid         sortop;
    1610             :             Oid         eqop;
    1611             :             TargetEntry *tle;
    1612             :             SortGroupClause *sortcl;
    1613             : 
    1614           2 :             sortop = get_ordering_op_for_equality_op(in_oper, false);
    1615           2 :             if (!OidIsValid(sortop))    /* shouldn't happen */
    1616           0 :                 elog(ERROR, "could not find ordering operator for equality operator %u",
    1617             :                      in_oper);
    1618             : 
    1619             :             /*
    1620             :              * The Unique node will need equality operators.  Normally these
    1621             :              * are the same as the IN clause operators, but if those are
    1622             :              * cross-type operators then the equality operators are the ones
    1623             :              * for the IN clause operators' RHS datatype.
    1624             :              */
    1625           2 :             eqop = get_equality_op_for_ordering_op(sortop, NULL);
    1626           2 :             if (!OidIsValid(eqop))  /* shouldn't happen */
    1627           0 :                 elog(ERROR, "could not find equality operator for ordering operator %u",
    1628             :                      sortop);
    1629             : 
    1630           2 :             tle = get_tle_by_resno(subplan->targetlist,
    1631           2 :                                    groupColIdx[groupColPos]);
    1632             :             Assert(tle != NULL);
    1633             : 
    1634           2 :             sortcl = makeNode(SortGroupClause);
    1635           2 :             sortcl->tleSortGroupRef = assignSortGroupRef(tle,
    1636             :                                                          subplan->targetlist);
    1637           2 :             sortcl->eqop = eqop;
    1638           2 :             sortcl->sortop = sortop;
    1639           2 :             sortcl->nulls_first = false;
    1640           2 :             sortcl->hashable = false;    /* no need to make this accurate */
    1641           2 :             sortList = lappend(sortList, sortcl);
    1642           2 :             groupColPos++;
    1643             :         }
    1644           2 :         sort = make_sort_from_sortclauses(sortList, subplan);
    1645           2 :         label_sort_with_costsize(root, sort, -1.0);
    1646           2 :         plan = (Plan *) make_unique_from_sortclauses((Plan *) sort, sortList);
    1647             :     }
    1648             : 
    1649             :     /* Copy cost data from Path to Plan */
    1650         196 :     copy_generic_path_info(plan, &best_path->path);
    1651             : 
    1652         196 :     return plan;
    1653             : }
    1654             : 
    1655             : /*
    1656             :  * create_gather_plan
    1657             :  *
    1658             :  *    Create a Gather plan for 'best_path' and (recursively) plans
    1659             :  *    for its subpaths.
    1660             :  */
    1661             : static Gather *
    1662         524 : create_gather_plan(PlannerInfo *root, GatherPath *best_path)
    1663             : {
    1664             :     Gather     *gather_plan;
    1665             :     Plan       *subplan;
    1666             :     List       *tlist;
    1667             : 
    1668             :     /*
    1669             :      * Although the Gather node can project, we prefer to push down such work
    1670             :      * to its child node, so demand an exact tlist from the child.
    1671             :      */
    1672         524 :     subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
    1673             : 
    1674         524 :     tlist = build_path_tlist(root, &best_path->path);
    1675             : 
    1676         524 :     gather_plan = make_gather(tlist,
    1677             :                               NIL,
    1678             :                               best_path->num_workers,
    1679             :                               assign_special_exec_param(root),
    1680         524 :                               best_path->single_copy,
    1681             :                               subplan);
    1682             : 
    1683         524 :     copy_generic_path_info(&gather_plan->plan, &best_path->path);
    1684             : 
    1685             :     /* use parallel mode for parallel plans. */
    1686         524 :     root->glob->parallelModeNeeded = true;
    1687             : 
    1688         524 :     return gather_plan;
    1689             : }
    1690             : 
    1691             : /*
    1692             :  * create_gather_merge_plan
    1693             :  *
    1694             :  *    Create a Gather Merge plan for 'best_path' and (recursively)
    1695             :  *    plans for its subpaths.
    1696             :  */
    1697             : static GatherMerge *
    1698         136 : create_gather_merge_plan(PlannerInfo *root, GatherMergePath *best_path)
    1699             : {
    1700             :     GatherMerge *gm_plan;
    1701             :     Plan       *subplan;
    1702         136 :     List       *pathkeys = best_path->path.pathkeys;
    1703         136 :     List       *tlist = build_path_tlist(root, &best_path->path);
    1704             : 
    1705             :     /* As with Gather, it's best to project away columns in the workers. */
    1706         136 :     subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
    1707             : 
    1708             :     /* Create a shell for a GatherMerge plan. */
    1709         136 :     gm_plan = makeNode(GatherMerge);
    1710         136 :     gm_plan->plan.targetlist = tlist;
    1711         136 :     gm_plan->num_workers = best_path->num_workers;
    1712         136 :     copy_generic_path_info(&gm_plan->plan, &best_path->path);
    1713             : 
    1714             :     /* Assign the rescan Param. */
    1715         136 :     gm_plan->rescan_param = assign_special_exec_param(root);
    1716             : 
    1717             :     /* Gather Merge is pointless with no pathkeys; use Gather instead. */
    1718             :     Assert(pathkeys != NIL);
    1719             : 
    1720             :     /* Compute sort column info, and adjust subplan's tlist as needed */
    1721         272 :     subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
    1722         136 :                                          best_path->subpath->parent->relids,
    1723         136 :                                          gm_plan->sortColIdx,
    1724             :                                          false,
    1725             :                                          &gm_plan->numCols,
    1726             :                                          &gm_plan->sortColIdx,
    1727             :                                          &gm_plan->sortOperators,
    1728             :                                          &gm_plan->collations,
    1729             :                                          &gm_plan->nullsFirst);
    1730             : 
    1731             : 
    1732             :     /* Now, insert a Sort node if subplan isn't sufficiently ordered */
    1733         136 :     if (!pathkeys_contained_in(pathkeys, best_path->subpath->pathkeys))
    1734           0 :         subplan = (Plan *) make_sort(subplan, gm_plan->numCols,
    1735             :                                      gm_plan->sortColIdx,
    1736             :                                      gm_plan->sortOperators,
    1737             :                                      gm_plan->collations,
    1738             :                                      gm_plan->nullsFirst);
    1739             : 
    1740             :     /* Now insert the subplan under GatherMerge. */
    1741         136 :     gm_plan->plan.lefttree = subplan;
    1742             : 
    1743             :     /* use parallel mode for parallel plans. */
    1744         136 :     root->glob->parallelModeNeeded = true;
    1745             : 
    1746         136 :     return gm_plan;
    1747             : }
    1748             : 
    1749             : /*
    1750             :  * create_projection_plan
    1751             :  *
    1752             :  *    Create a plan tree to do a projection step and (recursively) plans
    1753             :  *    for its subpaths.  We may need a Result node for the projection,
    1754             :  *    but sometimes we can just let the subplan do the work.
    1755             :  */
    1756             : static Plan *
    1757      205772 : create_projection_plan(PlannerInfo *root, ProjectionPath *best_path, int flags)
    1758             : {
    1759             :     Plan       *plan;
    1760             :     Plan       *subplan;
    1761             :     List       *tlist;
    1762      205772 :     bool        needs_result_node = false;
    1763             : 
    1764             :     /*
    1765             :      * Convert our subpath to a Plan and determine whether we need a Result
    1766             :      * node.
    1767             :      *
    1768             :      * In most cases where we don't need to project, creation_projection_path
    1769             :      * will have set dummypp, but not always.  First, some createplan.c
    1770             :      * routines change the tlists of their nodes.  (An example is that
    1771             :      * create_merge_append_plan might add resjunk sort columns to a
    1772             :      * MergeAppend.)  Second, create_projection_path has no way of knowing
    1773             :      * what path node will be placed on top of the projection path and
    1774             :      * therefore can't predict whether it will require an exact tlist. For
    1775             :      * both of these reasons, we have to recheck here.
    1776             :      */
    1777      205772 :     if (use_physical_tlist(root, &best_path->path, flags))
    1778             :     {
    1779             :         /*
    1780             :          * Our caller doesn't really care what tlist we return, so we don't
    1781             :          * actually need to project.  However, we may still need to ensure
    1782             :          * proper sortgroupref labels, if the caller cares about those.
    1783             :          */
    1784         292 :         subplan = create_plan_recurse(root, best_path->subpath, 0);
    1785         292 :         tlist = subplan->targetlist;
    1786         292 :         if (flags & CP_LABEL_TLIST)
    1787         116 :             apply_pathtarget_labeling_to_tlist(tlist,
    1788             :                                                best_path->path.pathtarget);
    1789             :     }
    1790      205480 :     else if (is_projection_capable_path(best_path->subpath))
    1791             :     {
    1792             :         /*
    1793             :          * Our caller requires that we return the exact tlist, but no separate
    1794             :          * result node is needed because the subpath is projection-capable.
    1795             :          * Tell create_plan_recurse that we're going to ignore the tlist it
    1796             :          * produces.
    1797             :          */
    1798      199594 :         subplan = create_plan_recurse(root, best_path->subpath,
    1799             :                                       CP_IGNORE_TLIST);
    1800      199594 :         tlist = build_path_tlist(root, &best_path->path);
    1801             :     }
    1802             :     else
    1803             :     {
    1804             :         /*
    1805             :          * It looks like we need a result node, unless by good fortune the
    1806             :          * requested tlist is exactly the one the child wants to produce.
    1807             :          */
    1808        5886 :         subplan = create_plan_recurse(root, best_path->subpath, 0);
    1809        5886 :         tlist = build_path_tlist(root, &best_path->path);
    1810        5886 :         needs_result_node = !tlist_same_exprs(tlist, subplan->targetlist);
    1811             :     }
    1812             : 
    1813             :     /*
    1814             :      * If we make a different decision about whether to include a Result node
    1815             :      * than create_projection_path did, we'll have made slightly wrong cost
    1816             :      * estimates; but label the plan with the cost estimates we actually used,
    1817             :      * not "corrected" ones.  (XXX this could be cleaned up if we moved more
    1818             :      * of the sortcolumn setup logic into Path creation, but that would add
    1819             :      * expense to creating Paths we might end up not using.)
    1820             :      */
    1821      205772 :     if (!needs_result_node)
    1822             :     {
    1823             :         /* Don't need a separate Result, just assign tlist to subplan */
    1824      204972 :         plan = subplan;
    1825      204972 :         plan->targetlist = tlist;
    1826             : 
    1827             :         /* Label plan with the estimated costs we actually used */
    1828      204972 :         plan->startup_cost = best_path->path.startup_cost;
    1829      204972 :         plan->total_cost = best_path->path.total_cost;
    1830      204972 :         plan->plan_rows = best_path->path.rows;
    1831      204972 :         plan->plan_width = best_path->path.pathtarget->width;
    1832      204972 :         plan->parallel_safe = best_path->path.parallel_safe;
    1833             :         /* ... but don't change subplan's parallel_aware flag */
    1834             :     }
    1835             :     else
    1836             :     {
    1837             :         /* We need a Result node */
    1838         800 :         plan = (Plan *) make_result(tlist, NULL, subplan);
    1839             : 
    1840         800 :         copy_generic_path_info(plan, (Path *) best_path);
    1841             :     }
    1842             : 
    1843      205772 :     return plan;
    1844             : }
    1845             : 
    1846             : /*
    1847             :  * inject_projection_plan
    1848             :  *    Insert a Result node to do a projection step.
    1849             :  *
    1850             :  * This is used in a few places where we decide on-the-fly that we need a
    1851             :  * projection step as part of the tree generated for some Path node.
    1852             :  * We should try to get rid of this in favor of doing it more honestly.
    1853             :  *
    1854             :  * One reason it's ugly is we have to be told the right parallel_safe marking
    1855             :  * to apply (since the tlist might be unsafe even if the child plan is safe).
    1856             :  */
    1857             : static Plan *
    1858          16 : inject_projection_plan(Plan *subplan, List *tlist, bool parallel_safe)
    1859             : {
    1860             :     Plan       *plan;
    1861             : 
    1862          16 :     plan = (Plan *) make_result(tlist, NULL, subplan);
    1863             : 
    1864             :     /*
    1865             :      * In principle, we should charge tlist eval cost plus cpu_per_tuple per
    1866             :      * row for the Result node.  But the former has probably been factored in
    1867             :      * already and the latter was not accounted for during Path construction,
    1868             :      * so being formally correct might just make the EXPLAIN output look less
    1869             :      * consistent not more so.  Hence, just copy the subplan's cost.
    1870             :      */
    1871          16 :     copy_plan_costsize(plan, subplan);
    1872          16 :     plan->parallel_safe = parallel_safe;
    1873             : 
    1874          16 :     return plan;
    1875             : }
    1876             : 
    1877             : /*
    1878             :  * change_plan_targetlist
    1879             :  *    Externally available wrapper for inject_projection_plan.
    1880             :  *
    1881             :  * This is meant for use by FDW plan-generation functions, which might
    1882             :  * want to adjust the tlist computed by some subplan tree.  In general,
    1883             :  * a Result node is needed to compute the new tlist, but we can optimize
    1884             :  * some cases.
    1885             :  *
    1886             :  * In most cases, tlist_parallel_safe can just be passed as the parallel_safe
    1887             :  * flag of the FDW's own Path node.
    1888             :  */
    1889             : Plan *
    1890          66 : change_plan_targetlist(Plan *subplan, List *tlist, bool tlist_parallel_safe)
    1891             : {
    1892             :     /*
    1893             :      * If the top plan node can't do projections and its existing target list
    1894             :      * isn't already what we need, we need to add a Result node to help it
    1895             :      * along.
    1896             :      */
    1897          72 :     if (!is_projection_capable_plan(subplan) &&
    1898           6 :         !tlist_same_exprs(tlist, subplan->targetlist))
    1899           0 :         subplan = inject_projection_plan(subplan, tlist,
    1900           0 :                                          subplan->parallel_safe &&
    1901             :                                          tlist_parallel_safe);
    1902             :     else
    1903             :     {
    1904             :         /* Else we can just replace the plan node's tlist */
    1905          66 :         subplan->targetlist = tlist;
    1906          66 :         subplan->parallel_safe &= tlist_parallel_safe;
    1907             :     }
    1908          66 :     return subplan;
    1909             : }
    1910             : 
    1911             : /*
    1912             :  * create_sort_plan
    1913             :  *
    1914             :  *    Create a Sort plan for 'best_path' and (recursively) plans
    1915             :  *    for its subpaths.
    1916             :  */
    1917             : static Sort *
    1918       27392 : create_sort_plan(PlannerInfo *root, SortPath *best_path, int flags)
    1919             : {
    1920             :     Sort       *plan;
    1921             :     Plan       *subplan;
    1922             : 
    1923             :     /*
    1924             :      * We don't want any excess columns in the sorted tuples, so request a
    1925             :      * smaller tlist.  Otherwise, since Sort doesn't project, tlist
    1926             :      * requirements pass through.
    1927             :      */
    1928       27392 :     subplan = create_plan_recurse(root, best_path->subpath,
    1929             :                                   flags | CP_SMALL_TLIST);
    1930             : 
    1931             :     /*
    1932             :      * make_sort_from_pathkeys() indirectly calls find_ec_member_for_tle(),
    1933             :      * which will ignore any child EC members that don't belong to the given
    1934             :      * relids. Thus, if this sort path is based on a child relation, we must
    1935             :      * pass its relids.
    1936             :      */
    1937       27600 :     plan = make_sort_from_pathkeys(subplan, best_path->path.pathkeys,
    1938       54640 :                                    IS_OTHER_REL(best_path->subpath->parent) ?
    1939         208 :                                    best_path->path.parent->relids : NULL);
    1940             : 
    1941       27392 :     copy_generic_path_info(&plan->plan, (Path *) best_path);
    1942             : 
    1943       27392 :     return plan;
    1944             : }
    1945             : 
    1946             : /*
    1947             :  * create_group_plan
    1948             :  *
    1949             :  *    Create a Group plan for 'best_path' and (recursively) plans
    1950             :  *    for its subpaths.
    1951             :  */
    1952             : static Group *
    1953         106 : create_group_plan(PlannerInfo *root, GroupPath *best_path)
    1954             : {
    1955             :     Group      *plan;
    1956             :     Plan       *subplan;
    1957             :     List       *tlist;
    1958             :     List       *quals;
    1959             : 
    1960             :     /*
    1961             :      * Group can project, so no need to be terribly picky about child tlist,
    1962             :      * but we do need grouping columns to be available
    1963             :      */
    1964         106 :     subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
    1965             : 
    1966         106 :     tlist = build_path_tlist(root, &best_path->path);
    1967             : 
    1968         106 :     quals = order_qual_clauses(root, best_path->qual);
    1969             : 
    1970         212 :     plan = make_group(tlist,
    1971             :                       quals,
    1972         106 :                       list_length(best_path->groupClause),
    1973             :                       extract_grouping_cols(best_path->groupClause,
    1974             :                                             subplan->targetlist),
    1975             :                       extract_grouping_ops(best_path->groupClause),
    1976             :                       extract_grouping_collations(best_path->groupClause,
    1977             :                                                   subplan->targetlist),
    1978             :                       subplan);
    1979             : 
    1980         106 :     copy_generic_path_info(&plan->plan, (Path *) best_path);
    1981             : 
    1982         106 :     return plan;
    1983             : }
    1984             : 
    1985             : /*
    1986             :  * create_upper_unique_plan
    1987             :  *
    1988             :  *    Create a Unique plan for 'best_path' and (recursively) plans
    1989             :  *    for its subpaths.
    1990             :  */
    1991             : static Unique *
    1992         316 : create_upper_unique_plan(PlannerInfo *root, UpperUniquePath *best_path, int flags)
    1993             : {
    1994             :     Unique     *plan;
    1995             :     Plan       *subplan;
    1996             : 
    1997             :     /*
    1998             :      * Unique doesn't project, so tlist requirements pass through; moreover we
    1999             :      * need grouping columns to be labeled.
    2000             :      */
    2001         316 :     subplan = create_plan_recurse(root, best_path->subpath,
    2002             :                                   flags | CP_LABEL_TLIST);
    2003             : 
    2004         316 :     plan = make_unique_from_pathkeys(subplan,
    2005             :                                      best_path->path.pathkeys,
    2006             :                                      best_path->numkeys);
    2007             : 
    2008         316 :     copy_generic_path_info(&plan->plan, (Path *) best_path);
    2009             : 
    2010         316 :     return plan;
    2011             : }
    2012             : 
    2013             : /*
    2014             :  * create_agg_plan
    2015             :  *
    2016             :  *    Create an Agg plan for 'best_path' and (recursively) plans
    2017             :  *    for its subpaths.
    2018             :  */
    2019             : static Agg *
    2020       24176 : create_agg_plan(PlannerInfo *root, AggPath *best_path)
    2021             : {
    2022             :     Agg        *plan;
    2023             :     Plan       *subplan;
    2024             :     List       *tlist;
    2025             :     List       *quals;
    2026             : 
    2027             :     /*
    2028             :      * Agg can project, so no need to be terribly picky about child tlist, but
    2029             :      * we do need grouping columns to be available
    2030             :      */
    2031       24176 :     subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
    2032             : 
    2033       24176 :     tlist = build_path_tlist(root, &best_path->path);
    2034             : 
    2035       24176 :     quals = order_qual_clauses(root, best_path->qual);
    2036             : 
    2037       48352 :     plan = make_agg(tlist, quals,
    2038             :                     best_path->aggstrategy,
    2039             :                     best_path->aggsplit,
    2040       24176 :                     list_length(best_path->groupClause),
    2041             :                     extract_grouping_cols(best_path->groupClause,
    2042             :                                           subplan->targetlist),
    2043             :                     extract_grouping_ops(best_path->groupClause),
    2044             :                     extract_grouping_collations(best_path->groupClause,
    2045             :                                                 subplan->targetlist),
    2046             :                     NIL,
    2047             :                     NIL,
    2048             :                     best_path->numGroups,
    2049             :                     subplan);
    2050             : 
    2051       24176 :     copy_generic_path_info(&plan->plan, (Path *) best_path);
    2052             : 
    2053       24176 :     return plan;
    2054             : }
    2055             : 
    2056             : /*
    2057             :  * Given a groupclause for a collection of grouping sets, produce the
    2058             :  * corresponding groupColIdx.
    2059             :  *
    2060             :  * root->grouping_map maps the tleSortGroupRef to the actual column position in
    2061             :  * the input tuple. So we get the ref from the entries in the groupclause and
    2062             :  * look them up there.
    2063             :  */
    2064             : static AttrNumber *
    2065         784 : remap_groupColIdx(PlannerInfo *root, List *groupClause)
    2066             : {
    2067         784 :     AttrNumber *grouping_map = root->grouping_map;
    2068             :     AttrNumber *new_grpColIdx;
    2069             :     ListCell   *lc;
    2070             :     int         i;
    2071             : 
    2072             :     Assert(grouping_map);
    2073             : 
    2074         784 :     new_grpColIdx = palloc0(sizeof(AttrNumber) * list_length(groupClause));
    2075             : 
    2076         784 :     i = 0;
    2077        1824 :     foreach(lc, groupClause)
    2078             :     {
    2079        1040 :         SortGroupClause *clause = lfirst(lc);
    2080             : 
    2081        1040 :         new_grpColIdx[i++] = grouping_map[clause->tleSortGroupRef];
    2082             :     }
    2083             : 
    2084         784 :     return new_grpColIdx;
    2085             : }
    2086             : 
    2087             : /*
    2088             :  * create_groupingsets_plan
    2089             :  *    Create a plan for 'best_path' and (recursively) plans
    2090             :  *    for its subpaths.
    2091             :  *
    2092             :  *    What we emit is an Agg plan with some vestigial Agg and Sort nodes
    2093             :  *    hanging off the side.  The top Agg implements the last grouping set
    2094             :  *    specified in the GroupingSetsPath, and any additional grouping sets
    2095             :  *    each give rise to a subsidiary Agg and Sort node in the top Agg's
    2096             :  *    "chain" list.  These nodes don't participate in the plan directly,
    2097             :  *    but they are a convenient way to represent the required data for
    2098             :  *    the extra steps.
    2099             :  *
    2100             :  *    Returns a Plan node.
    2101             :  */
    2102             : static Plan *
    2103         404 : create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path)
    2104             : {
    2105             :     Agg        *plan;
    2106             :     Plan       *subplan;
    2107         404 :     List       *rollups = best_path->rollups;
    2108             :     AttrNumber *grouping_map;
    2109             :     int         maxref;
    2110             :     List       *chain;
    2111             :     ListCell   *lc;
    2112             : 
    2113             :     /* Shouldn't get here without grouping sets */
    2114             :     Assert(root->parse->groupingSets);
    2115             :     Assert(rollups != NIL);
    2116             : 
    2117             :     /*
    2118             :      * Agg can project, so no need to be terribly picky about child tlist, but
    2119             :      * we do need grouping columns to be available
    2120             :      */
    2121         404 :     subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
    2122             : 
    2123             :     /*
    2124             :      * Compute the mapping from tleSortGroupRef to column index in the child's
    2125             :      * tlist.  First, identify max SortGroupRef in groupClause, for array
    2126             :      * sizing.
    2127             :      */
    2128         404 :     maxref = 0;
    2129        1248 :     foreach(lc, root->parse->groupClause)
    2130             :     {
    2131         844 :         SortGroupClause *gc = (SortGroupClause *) lfirst(lc);
    2132             : 
    2133         844 :         if (gc->tleSortGroupRef > maxref)
    2134         828 :             maxref = gc->tleSortGroupRef;
    2135             :     }
    2136             : 
    2137         404 :     grouping_map = (AttrNumber *) palloc0((maxref + 1) * sizeof(AttrNumber));
    2138             : 
    2139             :     /* Now look up the column numbers in the child's tlist */
    2140        1248 :     foreach(lc, root->parse->groupClause)
    2141             :     {
    2142         844 :         SortGroupClause *gc = (SortGroupClause *) lfirst(lc);
    2143         844 :         TargetEntry *tle = get_sortgroupclause_tle(gc, subplan->targetlist);
    2144             : 
    2145         844 :         grouping_map[gc->tleSortGroupRef] = tle->resno;
    2146             :     }
    2147             : 
    2148             :     /*
    2149             :      * During setrefs.c, we'll need the grouping_map to fix up the cols lists
    2150             :      * in GroupingFunc nodes.  Save it for setrefs.c to use.
    2151             :      *
    2152             :      * This doesn't work if we're in an inheritance subtree (see notes in
    2153             :      * create_modifytable_plan).  Fortunately we can't be because there would
    2154             :      * never be grouping in an UPDATE/DELETE; but let's Assert that.
    2155             :      */
    2156             :     Assert(root->inhTargetKind == INHKIND_NONE);
    2157             :     Assert(root->grouping_map == NULL);
    2158         404 :     root->grouping_map = grouping_map;
    2159             : 
    2160             :     /*
    2161             :      * Generate the side nodes that describe the other sort and group
    2162             :      * operations besides the top one.  Note that we don't worry about putting
    2163             :      * accurate cost estimates in the side nodes; only the topmost Agg node's
    2164             :      * costs will be shown by EXPLAIN.
    2165             :      */
    2166         404 :     chain = NIL;
    2167         404 :     if (list_length(rollups) > 1)
    2168             :     {
    2169         232 :         ListCell   *lc2 = lnext(list_head(rollups));
    2170         232 :         bool        is_first_sort = ((RollupData *) linitial(rollups))->is_hashed;
    2171             : 
    2172         612 :         for_each_cell(lc, lc2)
    2173             :         {
    2174         380 :             RollupData *rollup = lfirst(lc);
    2175             :             AttrNumber *new_grpColIdx;
    2176         380 :             Plan       *sort_plan = NULL;
    2177             :             Plan       *agg_plan;
    2178             :             AggStrategy strat;
    2179             : 
    2180         380 :             new_grpColIdx = remap_groupColIdx(root, rollup->groupClause);
    2181             : 
    2182         380 :             if (!rollup->is_hashed && !is_first_sort)
    2183             :             {
    2184         112 :                 sort_plan = (Plan *)
    2185         112 :                     make_sort_from_groupcols(rollup->groupClause,
    2186             :                                              new_grpColIdx,
    2187             :                                              subplan);
    2188             :             }
    2189             : 
    2190         380 :             if (!rollup->is_hashed)
    2191         204 :                 is_first_sort = false;
    2192             : 
    2193         380 :             if (rollup->is_hashed)
    2194         176 :                 strat = AGG_HASHED;
    2195         204 :             else if (list_length(linitial(rollup->gsets)) == 0)
    2196          64 :                 strat = AGG_PLAIN;
    2197             :             else
    2198         140 :                 strat = AGG_SORTED;
    2199             : 
    2200         760 :             agg_plan = (Plan *) make_agg(NIL,
    2201             :                                          NIL,
    2202             :                                          strat,
    2203             :                                          AGGSPLIT_SIMPLE,
    2204         380 :                                          list_length((List *) linitial(rollup->gsets)),
    2205             :                                          new_grpColIdx,
    2206             :                                          extract_grouping_ops(rollup->groupClause),
    2207             :                                          extract_grouping_collations(rollup->groupClause, subplan->targetlist),
    2208             :                                          rollup->gsets,
    2209             :                                          NIL,
    2210             :                                          rollup->numGroups,
    2211             :                                          sort_plan);
    2212             : 
    2213             :             /*
    2214             :              * Remove stuff we don't need to avoid bloating debug output.
    2215             :              */
    2216         380 :             if (sort_plan)
    2217             :             {
    2218         112 :                 sort_plan->targetlist = NIL;
    2219         112 :                 sort_plan->lefttree = NULL;
    2220             :             }
    2221             : 
    2222         380 :             chain = lappend(chain, agg_plan);
    2223             :         }
    2224             :     }
    2225             : 
    2226             :     /*
    2227             :      * Now make the real Agg node
    2228             :      */
    2229             :     {
    2230         404 :         RollupData *rollup = linitial(rollups);
    2231             :         AttrNumber *top_grpColIdx;
    2232             :         int         numGroupCols;
    2233             : 
    2234         404 :         top_grpColIdx = remap_groupColIdx(root, rollup->groupClause);
    2235             : 
    2236         404 :         numGroupCols = list_length((List *) linitial(rollup->gsets));
    2237             : 
    2238         404 :         plan = make_agg(build_path_tlist(root, &best_path->path),
    2239             :                         best_path->qual,
    2240             :                         best_path->aggstrategy,
    2241             :                         AGGSPLIT_SIMPLE,
    2242             :                         numGroupCols,
    2243             :                         top_grpColIdx,
    2244             :                         extract_grouping_ops(rollup->groupClause),
    2245             :                         extract_grouping_collations(rollup->groupClause, subplan->targetlist),
    2246             :                         rollup->gsets,
    2247             :                         chain,
    2248             :                         rollup->numGroups,
    2249             :                         subplan);
    2250             : 
    2251             :         /* Copy cost data from Path to Plan */
    2252         404 :         copy_generic_path_info(&plan->plan, &best_path->path);
    2253             :     }
    2254             : 
    2255         404 :     return (Plan *) plan;
    2256             : }
    2257             : 
    2258             : /*
    2259             :  * create_minmaxagg_plan
    2260             :  *
    2261             :  *    Create a Result plan for 'best_path' and (recursively) plans
    2262             :  *    for its subpaths.
    2263             :  */
    2264             : static Result *
    2265         336 : create_minmaxagg_plan(PlannerInfo *root, MinMaxAggPath *best_path)
    2266             : {
    2267             :     Result     *plan;
    2268             :     List       *tlist;
    2269             :     ListCell   *lc;
    2270             : 
    2271             :     /* Prepare an InitPlan for each aggregate's subquery. */
    2272         696 :     foreach(lc, best_path->mmaggregates)
    2273             :     {
    2274         360 :         MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
    2275         360 :         PlannerInfo *subroot = mminfo->subroot;
    2276         360 :         Query      *subparse = subroot->parse;
    2277             :         Plan       *plan;
    2278             : 
    2279             :         /*
    2280             :          * Generate the plan for the subquery. We already have a Path, but we
    2281             :          * have to convert it to a Plan and attach a LIMIT node above it.
    2282             :          * Since we are entering a different planner context (subroot),
    2283             :          * recurse to create_plan not create_plan_recurse.
    2284             :          */
    2285         360 :         plan = create_plan(subroot, mminfo->path);
    2286             : 
    2287         360 :         plan = (Plan *) make_limit(plan,
    2288             :                                    subparse->limitOffset,
    2289             :                                    subparse->limitCount);
    2290             : 
    2291             :         /* Must apply correct cost/width data to Limit node */
    2292         360 :         plan->startup_cost = mminfo->path->startup_cost;
    2293         360 :         plan->total_cost = mminfo->pathcost;
    2294         360 :         plan->plan_rows = 1;
    2295         360 :         plan->plan_width = mminfo->path->pathtarget->width;
    2296         360 :         plan->parallel_aware = false;
    2297         360 :         plan->parallel_safe = mminfo->path->parallel_safe;
    2298             : 
    2299             :         /* Convert the plan into an InitPlan in the outer query. */
    2300         360 :         SS_make_initplan_from_plan(root, subroot, plan, mminfo->param);
    2301             :     }
    2302             : 
    2303             :     /* Generate the output plan --- basically just a Result */
    2304         336 :     tlist = build_path_tlist(root, &best_path->path);
    2305             : 
    2306         336 :     plan = make_result(tlist, (Node *) best_path->quals, NULL);
    2307             : 
    2308         336 :     copy_generic_path_info(&plan->plan, (Path *) best_path);
    2309             : 
    2310             :     /*
    2311             :      * During setrefs.c, we'll need to replace references to the Agg nodes
    2312             :      * with InitPlan output params.  (We can't just do that locally in the
    2313             :      * MinMaxAgg node, because path nodes above here may have Agg references
    2314             :      * as well.)  Save the mmaggregates list to tell setrefs.c to do that.
    2315             :      *
    2316             :      * This doesn't work if we're in an inheritance subtree (see notes in
    2317             :      * create_modifytable_plan).  Fortunately we can't be because there would
    2318             :      * never be aggregates in an UPDATE/DELETE; but let's Assert that.
    2319             :      */
    2320             :     Assert(root->inhTargetKind == INHKIND_NONE);
    2321             :     Assert(root->minmax_aggs == NIL);
    2322         336 :     root->minmax_aggs = best_path->mmaggregates;
    2323             : 
    2324         336 :     return plan;
    2325             : }
    2326             : 
    2327             : /*
    2328             :  * create_windowagg_plan
    2329             :  *
    2330             :  *    Create a WindowAgg plan for 'best_path' and (recursively) plans
    2331             :  *    for its subpaths.
    2332             :  */
    2333             : static WindowAgg *
    2334        1124 : create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path)
    2335             : {
    2336             :     WindowAgg  *plan;
    2337        1124 :     WindowClause *wc = best_path->winclause;
    2338        1124 :     int         numPart = list_length(wc->partitionClause);
    2339        1124 :     int         numOrder = list_length(wc->orderClause);
    2340             :     Plan       *subplan;
    2341             :     List       *tlist;
    2342             :     int         partNumCols;
    2343             :     AttrNumber *partColIdx;
    2344             :     Oid        *partOperators;
    2345             :     Oid        *partCollations;
    2346             :     int         ordNumCols;
    2347             :     AttrNumber *ordColIdx;
    2348             :     Oid        *ordOperators;
    2349             :     Oid        *ordCollations;
    2350             :     ListCell   *lc;
    2351             : 
    2352             :     /*
    2353             :      * WindowAgg can project, so no need to be terribly picky about child
    2354             :      * tlist, but we do need grouping columns to be available
    2355             :      */
    2356        1124 :     subplan = create_plan_recurse(root, best_path->subpath, CP_LABEL_TLIST);
    2357             : 
    2358        1124 :     tlist = build_path_tlist(root, &best_path->path);
    2359             : 
    2360             :     /*
    2361             :      * Convert SortGroupClause lists into arrays of attr indexes and equality
    2362             :      * operators, as wanted by executor.  (Note: in principle, it's possible
    2363             :      * to drop some of the sort columns, if they were proved redundant by
    2364             :      * pathkey logic.  However, it doesn't seem worth going out of our way to
    2365             :      * optimize such cases.  In any case, we must *not* remove the ordering
    2366             :      * column for RANGE OFFSET cases, as the executor needs that for in_range
    2367             :      * tests even if it's known to be equal to some partitioning column.)
    2368             :      */
    2369        1124 :     partColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numPart);
    2370        1124 :     partOperators = (Oid *) palloc(sizeof(Oid) * numPart);
    2371        1124 :     partCollations = (Oid *) palloc(sizeof(Oid) * numPart);
    2372             : 
    2373        1124 :     partNumCols = 0;
    2374        1476 :     foreach(lc, wc->partitionClause)
    2375             :     {
    2376         352 :         SortGroupClause *sgc = (SortGroupClause *) lfirst(lc);
    2377         352 :         TargetEntry *tle = get_sortgroupclause_tle(sgc, subplan->targetlist);
    2378             : 
    2379             :         Assert(OidIsValid(sgc->eqop));
    2380         352 :         partColIdx[partNumCols] = tle->resno;
    2381         352 :         partOperators[partNumCols] = sgc->eqop;
    2382         352 :         partCollations[partNumCols] = exprCollation((Node *) tle->expr);
    2383         352 :         partNumCols++;
    2384             :     }
    2385             : 
    2386        1124 :     ordColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numOrder);
    2387        1124 :     ordOperators = (Oid *) palloc(sizeof(Oid) * numOrder);
    2388        1124 :     ordCollations = (Oid *) palloc(sizeof(Oid) * numOrder);
    2389             : 
    2390        1124 :     ordNumCols = 0;
    2391        2144 :     foreach(lc, wc->orderClause)
    2392             :     {
    2393        1020 :         SortGroupClause *sgc = (SortGroupClause *) lfirst(lc);
    2394        1020 :         TargetEntry *tle = get_sortgroupclause_tle(sgc, subplan->targetlist);
    2395             : 
    2396             :         Assert(OidIsValid(sgc->eqop));
    2397        1020 :         ordColIdx[ordNumCols] = tle->resno;
    2398        1020 :         ordOperators[ordNumCols] = sgc->eqop;
    2399        1020 :         ordCollations[ordNumCols] = exprCollation((Node *) tle->expr);
    2400        1020 :         ordNumCols++;
    2401             :     }
    2402             : 
    2403             :     /* And finally we can make the WindowAgg node */
    2404        2248 :     plan = make_windowagg(tlist,
    2405             :                           wc->winref,
    2406             :                           partNumCols,
    2407             :                           partColIdx,
    2408             :                           partOperators,
    2409             :                           partCollations,
    2410             :                           ordNumCols,
    2411             :                           ordColIdx,
    2412             :                           ordOperators,
    2413             :                           ordCollations,
    2414             :                           wc->frameOptions,
    2415             :                           wc->startOffset,
    2416             :                           wc->endOffset,
    2417             :                           wc->startInRangeFunc,
    2418             :                           wc->endInRangeFunc,
    2419             :                           wc->inRangeColl,
    2420        1124 :                           wc->inRangeAsc,
    2421        1124 :                           wc->inRangeNullsFirst,
    2422             :                           subplan);
    2423             : 
    2424        1124 :     copy_generic_path_info(&plan->plan, (Path *) best_path);
    2425             : 
    2426        1124 :     return plan;
    2427             : }
    2428             : 
    2429             : /*
    2430             :  * create_setop_plan
    2431             :  *
    2432             :  *    Create a SetOp plan for 'best_path' and (recursively) plans
    2433             :  *    for its subpaths.
    2434             :  */
    2435             : static SetOp *
    2436         396 : create_setop_plan(PlannerInfo *root, SetOpPath *best_path, int flags)
    2437             : {
    2438             :     SetOp      *plan;
    2439             :     Plan       *subplan;
    2440             :     long        numGroups;
    2441             : 
    2442             :     /*
    2443             :      * SetOp doesn't project, so tlist requirements pass through; moreover we
    2444             :      * need grouping columns to be labeled.
    2445             :      */
    2446         396 :     subplan = create_plan_recurse(root, best_path->subpath,
    2447             :                                   flags | CP_LABEL_TLIST);
    2448             : 
    2449             :     /* Convert numGroups to long int --- but 'ware overflow! */
    2450         396 :     numGroups = (long) Min(best_path->numGroups, (double) LONG_MAX);
    2451             : 
    2452         792 :     plan = make_setop(best_path->cmd,
    2453             :                       best_path->strategy,
    2454             :                       subplan,
    2455             :                       best_path->distinctList,
    2456         396 :                       best_path->flagColIdx,
    2457             :                       best_path->firstFlag,
    2458             :                       numGroups);
    2459             : 
    2460         396 :     copy_generic_path_info(&plan->plan, (Path *) best_path);
    2461             : 
    2462         396 :     return plan;
    2463             : }
    2464             : 
    2465             : /*
    2466             :  * create_recursiveunion_plan
    2467             :  *
    2468             :  *    Create a RecursiveUnion plan for 'best_path' and (recursively) plans
    2469             :  *    for its subpaths.
    2470             :  */
    2471             : static RecursiveUnion *
    2472         326 : create_recursiveunion_plan(PlannerInfo *root, RecursiveUnionPath *best_path)
    2473             : {
    2474             :     RecursiveUnion *plan;
    2475             :     Plan       *leftplan;
    2476             :     Plan       *rightplan;
    2477             :     List       *tlist;
    2478             :     long        numGroups;
    2479             : 
    2480             :     /* Need both children to produce same tlist, so force it */
    2481         326 :     leftplan = create_plan_recurse(root, best_path->leftpath, CP_EXACT_TLIST);
    2482         326 :     rightplan = create_plan_recurse(root, best_path->rightpath, CP_EXACT_TLIST);
    2483             : 
    2484         326 :     tlist = build_path_tlist(root, &best_path->path);
    2485             : 
    2486             :     /* Convert numGroups to long int --- but 'ware overflow! */
    2487         326 :     numGroups = (long) Min(best_path->numGroups, (double) LONG_MAX);
    2488             : 
    2489         326 :     plan = make_recursive_union(tlist,
    2490             :                                 leftplan,
    2491             :                                 rightplan,
    2492             :                                 best_path->wtParam,
    2493             :                                 best_path->distinctList,
    2494             :                                 numGroups);
    2495             : 
    2496         326 :     copy_generic_path_info(&plan->plan, (Path *) best_path);
    2497             : 
    2498         326 :     return plan;
    2499             : }
    2500             : 
    2501             : /*
    2502             :  * create_lockrows_plan
    2503             :  *
    2504             :  *    Create a LockRows plan for 'best_path' and (recursively) plans
    2505             :  *    for its subpaths.
    2506             :  */
    2507             : static LockRows *
    2508        4718 : create_lockrows_plan(PlannerInfo *root, LockRowsPath *best_path,
    2509             :                      int flags)
    2510             : {
    2511             :     LockRows   *plan;
    2512             :     Plan       *subplan;
    2513             : 
    2514             :     /* LockRows doesn't project, so tlist requirements pass through */
    2515        4718 :     subplan = create_plan_recurse(root, best_path->subpath, flags);
    2516             : 
    2517        4718 :     plan = make_lockrows(subplan, best_path->rowMarks, best_path->epqParam);
    2518             : 
    2519        4718 :     copy_generic_path_info(&plan->plan, (Path *) best_path);
    2520             : 
    2521        4718 :     return plan;
    2522             : }
    2523             : 
    2524             : /*
    2525             :  * create_modifytable_plan
    2526             :  *    Create a ModifyTable plan for 'best_path'.
    2527             :  *
    2528             :  *    Returns a Plan node.
    2529             :  */
    2530             : static ModifyTable *
    2531       69436 : create_modifytable_plan(PlannerInfo *root, ModifyTablePath *best_path)
    2532             : {
    2533             :     ModifyTable *plan;
    2534       69436 :     List       *subplans = NIL;
    2535             :     ListCell   *subpaths,
    2536             :                *subroots;
    2537             : 
    2538             :     /* Build the plan for each input path */
    2539      139912 :     forboth(subpaths, best_path->subpaths,
    2540             :             subroots, best_path->subroots)
    2541             :     {
    2542       70476 :         Path       *subpath = (Path *) lfirst(subpaths);
    2543       70476 :         PlannerInfo *subroot = (PlannerInfo *) lfirst(subroots);
    2544             :         Plan       *subplan;
    2545             : 
    2546             :         /*
    2547             :          * In an inherited UPDATE/DELETE, reference the per-child modified
    2548             :          * subroot while creating Plans from Paths for the child rel.  This is
    2549             :          * a kluge, but otherwise it's too hard to ensure that Plan creation
    2550             :          * functions (particularly in FDWs) don't depend on the contents of
    2551             :          * "root" matching what they saw at Path creation time.  The main
    2552             :          * downside is that creation functions for Plans that might appear
    2553             :          * below a ModifyTable cannot expect to modify the contents of "root"
    2554             :          * and have it "stick" for subsequent processing such as setrefs.c.
    2555             :          * That's not great, but it seems better than the alternative.
    2556             :          */
    2557       70476 :         subplan = create_plan_recurse(subroot, subpath, CP_EXACT_TLIST);
    2558             : 
    2559             :         /* Transfer resname/resjunk labeling, too, to keep executor happy */
    2560       70476 :         apply_tlist_labeling(subplan->targetlist, subroot->processed_tlist);
    2561             : 
    2562       70476 :         subplans = lappend(subplans, subplan);
    2563             :     }
    2564             : 
    2565      208308 :     plan = make_modifytable(root,
    2566             :                             best_path->operation,
    2567       69436 :                             best_path->canSetTag,
    2568             :                             best_path->nominalRelation,
    2569             :                             best_path->rootRelation,
    2570       69436 :                             best_path->partColsUpdated,
    2571             :                             best_path->resultRelations,
    2572             :                             subplans,
    2573             :                             best_path->subroots,
    2574             :                             best_path->withCheckOptionLists,
    2575             :                             best_path->returningLists,
    2576             :                             best_path->rowMarks,
    2577             :                             best_path->onconflict,
    2578             :                             best_path->epqParam);
    2579             : 
    2580       69316 :     copy_generic_path_info(&plan->plan, &best_path->path);
    2581             : 
    2582       69316 :     return plan;
    2583             : }
    2584             : 
    2585             : /*
    2586             :  * create_limit_plan
    2587             :  *
    2588             :  *    Create a Limit plan for 'best_path' and (recursively) plans
    2589             :  *    for its subpaths.
    2590             :  */
    2591             : static Limit *
    2592        2552 : create_limit_plan(PlannerInfo *root, LimitPath *best_path, int flags)
    2593             : {
    2594             :     Limit      *plan;
    2595             :     Plan       *subplan;
    2596             : 
    2597             :     /* Limit doesn't project, so tlist requirements pass through */
    2598        2552 :     subplan = create_plan_recurse(root, best_path->subpath, flags);
    2599             : 
    2600        2552 :     plan = make_limit(subplan,
    2601             :                       best_path->limitOffset,
    2602             :                       best_path->limitCount);
    2603             : 
    2604        2552 :     copy_generic_path_info(&plan->plan, (Path *) best_path);
    2605             : 
    2606        2552 :     return plan;
    2607             : }
    2608             : 
    2609             : 
    2610             : /*****************************************************************************
    2611             :  *
    2612             :  *  BASE-RELATION SCAN METHODS
    2613             :  *
    2614             :  *****************************************************************************/
    2615             : 
    2616             : 
    2617             : /*
    2618             :  * create_seqscan_plan
    2619             :  *   Returns a seqscan plan for the base relation scanned by 'best_path'
    2620             :  *   with restriction clauses 'scan_clauses' and targetlist 'tlist'.
    2621             :  */
    2622             : static SeqScan *
    2623      112344 : create_seqscan_plan(PlannerInfo *root, Path *best_path,
    2624             :                     List *tlist, List *scan_clauses)
    2625             : {
    2626             :     SeqScan    *scan_plan;
    2627      112344 :     Index       scan_relid = best_path->parent->relid;
    2628             : 
    2629             :     /* it should be a base rel... */
    2630             :     Assert(scan_relid > 0);
    2631             :     Assert(best_path->parent->rtekind == RTE_RELATION);
    2632             : 
    2633             :     /* Sort clauses into best execution order */
    2634      112344 :     scan_clauses = order_qual_clauses(root, scan_clauses);
    2635             : 
    2636             :     /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
    2637      112344 :     scan_clauses = extract_actual_clauses(scan_clauses, false);
    2638             : 
    2639             :     /* Replace any outer-relation variables with nestloop params */
    2640      112344 :     if (best_path->param_info)
    2641             :     {
    2642         204 :         scan_clauses = (List *)
    2643             :             replace_nestloop_params(root, (Node *) scan_clauses);
    2644             :     }
    2645             : 
    2646      112344 :     scan_plan = make_seqscan(tlist,
    2647             :                              scan_clauses,
    2648             :                              scan_relid);
    2649             : 
    2650      112344 :     copy_generic_path_info(&scan_plan->plan, best_path);
    2651             : 
    2652      112344 :     return scan_plan;
    2653             : }
    2654             : 
    2655             : /*
    2656             :  * create_samplescan_plan
    2657             :  *   Returns a samplescan plan for the base relation scanned by 'best_path'
    2658             :  *   with restriction clauses 'scan_clauses' and targetlist 'tlist'.
    2659             :  */
    2660             : static SampleScan *
    2661         180 : create_samplescan_plan(PlannerInfo *root, Path *best_path,
    2662             :                        List *tlist, List *scan_clauses)
    2663             : {
    2664             :     SampleScan *scan_plan;
    2665         180 :     Index       scan_relid = best_path->parent->relid;
    2666             :     RangeTblEntry *rte;
    2667             :     TableSampleClause *tsc;
    2668             : 
    2669             :     /* it should be a base rel with a tablesample clause... */
    2670             :     Assert(scan_relid > 0);
    2671         180 :     rte = planner_rt_fetch(scan_relid, root);
    2672             :     Assert(rte->rtekind == RTE_RELATION);
    2673         180 :     tsc = rte->tablesample;
    2674             :     Assert(tsc != NULL);
    2675             : 
    2676             :     /* Sort clauses into best execution order */
    2677         180 :     scan_clauses = order_qual_clauses(root, scan_clauses);
    2678             : 
    2679             :     /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
    2680         180 :     scan_clauses = extract_actual_clauses(scan_clauses, false);
    2681             : 
    2682             :     /* Replace any outer-relation variables with nestloop params */
    2683         180 :     if (best_path->param_info)
    2684             :     {
    2685          12 :         scan_clauses = (List *)
    2686             :             replace_nestloop_params(root, (Node *) scan_clauses);
    2687          12 :         tsc = (TableSampleClause *)
    2688             :             replace_nestloop_params(root, (Node *) tsc);
    2689             :     }
    2690             : 
    2691         180 :     scan_plan = make_samplescan(tlist,
    2692             :                                 scan_clauses,
    2693             :                                 scan_relid,
    2694             :                                 tsc);
    2695             : 
    2696         180 :     copy_generic_path_info(&scan_plan->scan.plan, best_path);
    2697             : 
    2698         180 :     return scan_plan;
    2699             : }
    2700             : 
    2701             : /*
    2702             :  * create_indexscan_plan
    2703             :  *    Returns an indexscan plan for the base relation scanned by 'best_path'
    2704             :  *    with restriction clauses 'scan_clauses' and targetlist 'tlist'.
    2705             :  *
    2706             :  * We use this for both plain IndexScans and IndexOnlyScans, because the
    2707             :  * qual preprocessing work is the same for both.  Note that the caller tells
    2708             :  * us which to build --- we don't look at best_path->path.pathtype, because
    2709             :  * create_bitmap_subplan needs to be able to override the prior decision.
    2710             :  */
    2711             : static Scan *
    2712       94226 : create_indexscan_plan(PlannerInfo *root,
    2713             :                       IndexPath *best_path,
    2714             :                       List *tlist,
    2715             :                       List *scan_clauses,
    2716             :                       bool indexonly)
    2717             : {
    2718             :     Scan       *scan_plan;
    2719       94226 :     List       *indexclauses = best_path->indexclauses;
    2720       94226 :     List       *indexorderbys = best_path->indexorderbys;
    2721       94226 :     Index       baserelid = best_path->path.parent->relid;
    2722       94226 :     Oid         indexoid = best_path->indexinfo->indexoid;
    2723             :     List       *qpqual;
    2724             :     List       *stripped_indexquals;
    2725             :     List       *fixed_indexquals;
    2726             :     List       *fixed_indexorderbys;
    2727       94226 :     List       *indexorderbyops = NIL;
    2728             :     ListCell   *l;
    2729             : 
    2730             :     /* it should be a base rel... */
    2731             :     Assert(baserelid > 0);
    2732             :     Assert(best_path->path.parent->rtekind == RTE_RELATION);
    2733             : 
    2734             :     /*
    2735             :      * Extract the index qual expressions (stripped of RestrictInfos) from the
    2736             :      * IndexClauses list, and prepare a copy with index Vars substituted for
    2737             :      * table Vars.  (This step also does replace_nestloop_params on the
    2738             :      * fixed_indexquals.)
    2739             :      */
    2740       94226 :     fix_indexqual_references(root, best_path,
    2741             :                              &stripped_indexquals,
    2742             :                              &fixed_indexquals);
    2743             : 
    2744             :     /*
    2745             :      * Likewise fix up index attr references in the ORDER BY expressions.
    2746             :      */
    2747       94226 :     fixed_indexorderbys = fix_indexorderby_references(root, best_path);
    2748             : 
    2749             :     /*
    2750             :      * The qpqual list must contain all restrictions not automatically handled
    2751             :      * by the index, other than pseudoconstant clauses which will be handled
    2752             :      * by a separate gating plan node.  All the predicates in the indexquals
    2753             :      * will be checked (either by the index itself, or by nodeIndexscan.c),
    2754             :      * but if there are any "special" operators involved then they must be
    2755             :      * included in qpqual.  The upshot is that qpqual must contain
    2756             :      * scan_clauses minus whatever appears in indexquals.
    2757             :      *
    2758             :      * is_redundant_with_indexclauses() detects cases where a scan clause is
    2759             :      * present in the indexclauses list or is generated from the same
    2760             :      * EquivalenceClass as some indexclause, and is therefore redundant with
    2761             :      * it, though not equal.  (The latter happens when indxpath.c prefers a
    2762             :      * different derived equality than what generate_join_implied_equalities
    2763             :      * picked for a parameterized scan's ppi_clauses.)  Note that it will not
    2764             :      * match to lossy index clauses, which is critical because we have to
    2765             :      * include the original clause in qpqual in that case.
    2766             :      *
    2767             :      * In some situations (particularly with OR'd index conditions) we may
    2768             :      * have scan_clauses that are not equal to, but are logically implied by,
    2769             :      * the index quals; so we also try a predicate_implied_by() check to see
    2770             :      * if we can discard quals that way.  (predicate_implied_by assumes its
    2771             :      * first input contains only immutable functions, so we have to check
    2772             :      * that.)
    2773             :      *
    2774             :      * Note: if you change this bit of code you should also look at
    2775             :      * extract_nonindex_conditions() in costsize.c.
    2776             :      */
    2777       94226 :     qpqual = NIL;
    2778      201706 :     foreach(l, scan_clauses)
    2779             :     {
    2780      107480 :         RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
    2781             : 
    2782      107480 :         if (rinfo->pseudoconstant)
    2783         474 :             continue;           /* we may drop pseudoconstants here */
    2784      107006 :         if (is_redundant_with_indexclauses(rinfo, indexclauses))
    2785       83494 :             continue;           /* dup or derived from same EquivalenceClass */
    2786       45200 :         if (!contain_mutable_functions((Node *) rinfo->clause) &&
    2787       21688 :             predicate_implied_by(list_make1(rinfo->clause), stripped_indexquals,
    2788             :                                  false))
    2789         100 :             continue;           /* provably implied by indexquals */
    2790       23412 :         qpqual = lappend(qpqual, rinfo);
    2791             :     }
    2792             : 
    2793             :     /* Sort clauses into best execution order */
    2794       94226 :     qpqual = order_qual_clauses(root, qpqual);
    2795             : 
    2796             :     /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
    2797       94226 :     qpqual = extract_actual_clauses(qpqual, false);
    2798             : 
    2799             :     /*
    2800             :      * We have to replace any outer-relation variables with nestloop params in
    2801             :      * the indexqualorig, qpqual, and indexorderbyorig expressions.  A bit
    2802             :      * annoying to have to do this separately from the processing in
    2803             :      * fix_indexqual_references --- rethink this when generalizing the inner
    2804             :      * indexscan support.  But note we can't really do this earlier because
    2805             :      * it'd break the comparisons to predicates above ... (or would it?  Those
    2806             :      * wouldn't have outer refs)
    2807             :      */
    2808       94226 :     if (best_path->path.param_info)
    2809             :     {
    2810       16158 :         stripped_indexquals = (List *)
    2811       16158 :             replace_nestloop_params(root, (Node *) stripped_indexquals);
    2812       16158 :         qpqual = (List *)
    2813             :             replace_nestloop_params(root, (Node *) qpqual);
    2814       16158 :         indexorderbys = (List *)
    2815             :             replace_nestloop_params(root, (Node *) indexorderbys);
    2816             :     }
    2817             : 
    2818             :     /*
    2819             :      * If there are ORDER BY expressions, look up the sort operators for their
    2820             :      * result datatypes.
    2821             :      */
    2822       94226 :     if (indexorderbys)
    2823             :     {
    2824             :         ListCell   *pathkeyCell,
    2825             :                    *exprCell;
    2826             : 
    2827             :         /*
    2828             :          * PathKey contains OID of the btree opfamily we're sorting by, but
    2829             :          * that's not quite enough because we need the expression's datatype
    2830             :          * to look up the sort operator in the operator family.
    2831             :          */
    2832             :         Assert(list_length(best_path->path.pathkeys) == list_length(indexorderbys));
    2833         420 :         forboth(pathkeyCell, best_path->path.pathkeys, exprCell, indexorderbys)
    2834             :         {
    2835         210 :             PathKey    *pathkey = (PathKey *) lfirst(pathkeyCell);
    2836         210 :             Node       *expr = (Node *) lfirst(exprCell);
    2837         210 :             Oid         exprtype = exprType(expr);
    2838             :             Oid         sortop;
    2839             : 
    2840             :             /* Get sort operator from opfamily */
    2841         210 :             sortop = get_opfamily_member(pathkey->pk_opfamily,
    2842             :                                          exprtype,
    2843             :                                          exprtype,
    2844         210 :                                          pathkey->pk_strategy);
    2845         210 :             if (!OidIsValid(sortop))
    2846           0 :                 elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
    2847             :                      pathkey->pk_strategy, exprtype, exprtype, pathkey->pk_opfamily);
    2848         210 :             indexorderbyops = lappend_oid(indexorderbyops, sortop);
    2849             :         }
    2850             :     }
    2851             : 
    2852             :     /* Finally ready to build the plan node */
    2853       94226 :     if (indexonly)
    2854       12640 :         scan_plan = (Scan *) make_indexonlyscan(tlist,
    2855             :                                                 qpqual,
    2856             :                                                 baserelid,
    2857             :                                                 indexoid,
    2858             :                                                 fixed_indexquals,
    2859             :                                                 fixed_indexorderbys,
    2860        6320 :                                                 best_path->indexinfo->indextlist,
    2861             :                                                 best_path->indexscandir);
    2862             :     else
    2863       87906 :         scan_plan = (Scan *) make_indexscan(tlist,
    2864             :                                             qpqual,
    2865             :                                             baserelid,
    2866             :                                             indexoid,
    2867             :                                             fixed_indexquals,
    2868             :                                             stripped_indexquals,
    2869             :                                             fixed_indexorderbys,
    2870             :                                             indexorderbys,
    2871             :                                             indexorderbyops,
    2872             :                                             best_path->indexscandir);
    2873             : 
    2874       94226 :     copy_generic_path_info(&scan_plan->plan, &best_path->path);
    2875             : 
    2876       94226 :     return scan_plan;
    2877             : }
    2878             : 
    2879             : /*
    2880             :  * create_bitmap_scan_plan
    2881             :  *    Returns a bitmap scan plan for the base relation scanned by 'best_path'
    2882             :  *    with restriction clauses 'scan_clauses' and targetlist 'tlist'.
    2883             :  */
    2884             : static BitmapHeapScan *
    2885       18902 : create_bitmap_scan_plan(PlannerInfo *root,
    2886             :                         BitmapHeapPath *best_path,
    2887             :                         List *tlist,
    2888             :                         List *scan_clauses)
    2889             : {
    2890       18902 :     Index       baserelid = best_path->path.parent->relid;
    2891             :     Plan       *bitmapqualplan;
    2892             :     List       *bitmapqualorig;
    2893             :     List       *indexquals;
    2894             :     List       *indexECs;
    2895             :     List       *qpqual;
    2896             :     ListCell   *l;
    2897             :     BitmapHeapScan *scan_plan;
    2898             : 
    2899             :     /* it should be a base rel... */
    2900             :     Assert(baserelid > 0);
    2901             :     Assert(best_path->path.parent->rtekind == RTE_RELATION);
    2902             : 
    2903             :     /* Process the bitmapqual tree into a Plan tree and qual lists */
    2904       18902 :     bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
    2905             :                                            &bitmapqualorig, &indexquals,
    2906             :                                            &indexECs);
    2907             : 
    2908       18902 :     if (best_path->path.parallel_aware)
    2909          12 :         bitmap_subplan_mark_shared(bitmapqualplan);
    2910             : 
    2911             :     /*
    2912             :      * The qpqual list must contain all restrictions not automatically handled
    2913             :      * by the index, other than pseudoconstant clauses which will be handled
    2914             :      * by a separate gating plan node.  All the predicates in the indexquals
    2915             :      * will be checked (either by the index itself, or by
    2916             :      * nodeBitmapHeapscan.c), but if there are any "special" operators
    2917             :      * involved then they must be added to qpqual.  The upshot is that qpqual
    2918             :      * must contain scan_clauses minus whatever appears in indexquals.
    2919             :      *
    2920             :      * This loop is similar to the comparable code in create_indexscan_plan(),
    2921             :      * but with some differences because it has to compare the scan clauses to
    2922             :      * stripped (no RestrictInfos) indexquals.  See comments there for more
    2923             :      * info.
    2924             :      *
    2925             :      * In normal cases simple equal() checks will be enough to spot duplicate
    2926             :      * clauses, so we try that first.  We next see if the scan clause is
    2927             :      * redundant with any top-level indexqual by virtue of being generated
    2928             :      * from the same EC.  After that, try predicate_implied_by().
    2929             :      *
    2930             :      * Unlike create_indexscan_plan(), the predicate_implied_by() test here is
    2931             :      * useful for getting rid of qpquals that are implied by index predicates,
    2932             :      * because the predicate conditions are included in the "indexquals"
    2933             :      * returned by create_bitmap_subplan().  Bitmap scans have to do it that
    2934             :      * way because predicate conditions need to be rechecked if the scan
    2935             :      * becomes lossy, so they have to be included in bitmapqualorig.
    2936             :      */
    2937       18902 :     qpqual = NIL;
    2938       39358 :     foreach(l, scan_clauses)
    2939             :     {
    2940       20456 :         RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
    2941       20456 :         Node       *clause = (Node *) rinfo->clause;
    2942             : 
    2943       20456 :         if (rinfo->pseudoconstant)
    2944           0 :             continue;           /* we may drop pseudoconstants here */
    2945       20456 :         if (list_member(indexquals, clause))
    2946       19160 :             continue;           /* simple duplicate */
    2947        1296 :         if (rinfo->parent_ec && list_member_ptr(indexECs, rinfo->parent_ec))
    2948         112 :             continue;           /* derived from same EquivalenceClass */
    2949        2324 :         if (!contain_mutable_functions(clause) &&
    2950        1140 :             predicate_implied_by(list_make1(clause), indexquals, false))
    2951          68 :             continue;           /* provably implied by indexquals */
    2952        1116 :         qpqual = lappend(qpqual, rinfo);
    2953             :     }
    2954             : 
    2955             :     /* Sort clauses into best execution order */
    2956       18902 :     qpqual = order_qual_clauses(root, qpqual);
    2957             : 
    2958             :     /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
    2959       18902 :     qpqual = extract_actual_clauses(qpqual, false);
    2960             : 
    2961             :     /*
    2962             :      * When dealing with special operators, we will at this point have
    2963             :      * duplicate clauses in qpqual and bitmapqualorig.  We may as well drop
    2964             :      * 'em from bitmapqualorig, since there's no point in making the tests
    2965             :      * twice.
    2966             :      */
    2967       18902 :     bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
    2968             : 
    2969             :     /*
    2970             :      * We have to replace any outer-relation variables with nestloop params in
    2971             :      * the qpqual and bitmapqualorig expressions.  (This was already done for
    2972             :      * expressions attached to plan nodes in the bitmapqualplan tree.)
    2973             :      */
    2974       18902 :     if (best_path->path.param_info)
    2975             :     {
    2976         524 :         qpqual = (List *)
    2977             :             replace_nestloop_params(root, (Node *) qpqual);
    2978         524 :         bitmapqualorig = (List *)
    2979         524 :             replace_nestloop_params(root, (Node *) bitmapqualorig);
    2980             :     }
    2981             : 
    2982             :     /* Finally ready to build the plan node */
    2983       18902 :     scan_plan = make_bitmap_heapscan(tlist,
    2984             :                                      qpqual,
    2985             :                                      bitmapqualplan,
    2986             :                                      bitmapqualorig,
    2987             :                                      baserelid);
    2988             : 
    2989       18902 :     copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
    2990             : 
    2991       18902 :     return scan_plan;
    2992             : }
    2993             : 
    2994             : /*
    2995             :  * Given a bitmapqual tree, generate the Plan tree that implements it
    2996             :  *
    2997             :  * As byproducts, we also return in *qual and *indexqual the qual lists
    2998             :  * (in implicit-AND form, without RestrictInfos) describing the original index
    2999             :  * conditions and the generated indexqual conditions.  (These are the same in
    3000             :  * simple cases, but when special index operators are involved, the former
    3001             :  * list includes the special conditions while the latter includes the actual
    3002             :  * indexable conditions derived from them.)  Both lists include partial-index
    3003             :  * predicates, because we have to recheck predicates as well as index
    3004             :  * conditions if the bitmap scan becomes lossy.
    3005             :  *
    3006             :  * In addition, we return a list of EquivalenceClass pointers for all the
    3007             :  * top-level indexquals that were possibly-redundantly derived from ECs.
    3008             :  * This allows removal of scan_clauses that are redundant with such quals.
    3009             :  * (We do not attempt to detect such redundancies for quals that are within
    3010             :  * OR subtrees.  This could be done in a less hacky way if we returned the
    3011             :  * indexquals in RestrictInfo form, but that would be slower and still pretty
    3012             :  * messy, since we'd have to build new RestrictInfos in many cases.)
    3013             :  */
    3014             : static Plan *
    3015       19218 : create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
    3016             :                       List **qual, List **indexqual, List **indexECs)
    3017             : {
    3018             :     Plan       *plan;
    3019             : 
    3020       19218 :     if (IsA(bitmapqual, BitmapAndPath))
    3021             :     {
    3022          44 :         BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
    3023          44 :         List       *subplans = NIL;
    3024          44 :         List       *subquals = NIL;
    3025          44 :         List       *subindexquals = NIL;
    3026          44 :         List       *subindexECs = NIL;
    3027             :         ListCell   *l;
    3028             : 
    3029             :         /*
    3030             :          * There may well be redundant quals among the subplans, since a
    3031             :          * top-level WHERE qual might have gotten used to form several
    3032             :          * different index quals.  We don't try exceedingly hard to eliminate
    3033             :          * redundancies, but we do eliminate obvious duplicates by using
    3034             :          * list_concat_unique.
    3035             :          */
    3036         132 :         foreach(l, apath->bitmapquals)
    3037             :         {
    3038             :             Plan       *subplan;
    3039             :             List       *subqual;
    3040             :             List       *subindexqual;
    3041             :             List       *subindexEC;
    3042             : 
    3043          88 :             subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
    3044             :                                             &subqual, &subindexqual,
    3045             :                                             &subindexEC);
    3046          88 :             subplans = lappend(subplans, subplan);
    3047          88 :             subquals = list_concat_unique(subquals, subqual);
    3048          88 :             subindexquals = list_concat_unique(subindexquals, subindexqual);
    3049             :             /* Duplicates in indexECs aren't worth getting rid of */
    3050          88 :             subindexECs = list_concat(subindexECs, subindexEC);
    3051             :         }
    3052          44 :         plan = (Plan *) make_bitmap_and(subplans);
    3053          44 :         plan->startup_cost = apath->path.startup_cost;
    3054          44 :         plan->total_cost = apath->path.total_cost;
    3055          44 :         plan->plan_rows =
    3056          44 :             clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
    3057          44 :         plan->plan_width = 0;    /* meaningless */
    3058          44 :         plan->parallel_aware = false;
    3059          44 :         plan->parallel_safe = apath->path.parallel_safe;
    3060          44 :         *qual = subquals;
    3061          44 :         *indexqual = subindexquals;
    3062          44 :         *indexECs = subindexECs;
    3063             :     }
    3064       19174 :     else if (IsA(bitmapqual, BitmapOrPath))
    3065             :     {
    3066         104 :         BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
    3067         104 :         List       *subplans = NIL;
    3068         104 :         List       *subquals = NIL;
    3069         104 :         List       *subindexquals = NIL;
    3070         104 :         bool        const_true_subqual = false;
    3071         104 :         bool        const_true_subindexqual = false;
    3072             :         ListCell   *l;
    3073             : 
    3074             :         /*
    3075             :          * Here, we only detect qual-free subplans.  A qual-free subplan would
    3076             :          * cause us to generate "... OR true ..."  which we may as well reduce
    3077             :          * to just "true".  We do not try to eliminate redundant subclauses
    3078             :          * because (a) it's not as likely as in the AND case, and (b) we might
    3079             :          * well be working with hundreds or even thousands of OR conditions,
    3080             :          * perhaps from a long IN list.  The performance of list_append_unique
    3081             :          * would be unacceptable.
    3082             :          */
    3083         332 :         foreach(l, opath->bitmapquals)
    3084             :         {
    3085             :             Plan       *subplan;
    3086             :             List       *subqual;
    3087             :             List       *subindexqual;
    3088             :             List       *subindexEC;
    3089             : 
    3090         228 :             subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
    3091             :                                             &subqual, &subindexqual,
    3092             :                                             &subindexEC);
    3093         228 :             subplans = lappend(subplans, subplan);
    3094         228 :             if (subqual == NIL)
    3095           0 :                 const_true_subqual = true;
    3096         228 :             else if (!const_true_subqual)
    3097         456 :                 subquals = lappend(subquals,
    3098         228 :                                    make_ands_explicit(subqual));
    3099         228 :             if (subindexqual == NIL)
    3100           0 :                 const_true_subindexqual = true;
    3101         228 :             else if (!const_true_subindexqual)
    3102         456 :                 subindexquals = lappend(subindexquals,
    3103         228 :                                         make_ands_explicit(subindexqual));
    3104             :         }
    3105             : 
    3106             :         /*
    3107             :          * In the presence of ScalarArrayOpExpr quals, we might have built
    3108             :          * BitmapOrPaths with just one subpath; don't add an OR step.
    3109             :          */
    3110         104 :         if (list_length(subplans) == 1)
    3111             :         {
    3112           0 :             plan = (Plan *) linitial(subplans);
    3113             :         }
    3114             :         else
    3115             :         {
    3116         104 :             plan = (Plan *) make_bitmap_or(subplans);
    3117         104 :             plan->startup_cost = opath->path.startup_cost;
    3118         104 :             plan->total_cost = opath->path.total_cost;
    3119         104 :             plan->plan_rows =
    3120         104 :                 clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
    3121         104 :             plan->plan_width = 0;    /* meaningless */
    3122         104 :             plan->parallel_aware = false;
    3123         104 :             plan->parallel_safe = opath->path.parallel_safe;
    3124             :         }
    3125             : 
    3126             :         /*
    3127             :          * If there were constant-TRUE subquals, the OR reduces to constant
    3128             :          * TRUE.  Also, avoid generating one-element ORs, which could happen
    3129             :          * due to redundancy elimination or ScalarArrayOpExpr quals.
    3130             :          */
    3131         104 :         if (const_true_subqual)
    3132           0 :             *qual = NIL;
    3133         104 :         else if (list_length(subquals) <= 1)
    3134           0 :             *qual = subquals;
    3135             :         else
    3136         104 :             *qual = list_make1(make_orclause(subquals));
    3137         104 :         if (const_true_subindexqual)
    3138           0 :             *indexqual = NIL;
    3139         104 :         else if (list_length(subindexquals) <= 1)
    3140           0 :             *indexqual = subindexquals;
    3141             :         else
    3142         104 :             *indexqual = list_make1(make_orclause(subindexquals));
    3143         104 :         *indexECs = NIL;
    3144             :     }
    3145       19070 :     else if (IsA(bitmapqual, IndexPath))
    3146             :     {
    3147       19070 :         IndexPath  *ipath = (IndexPath *) bitmapqual;
    3148             :         IndexScan  *iscan;
    3149             :         List       *subquals;
    3150             :         List       *subindexquals;
    3151             :         List       *subindexECs;
    3152             :         ListCell   *l;
    3153             : 
    3154             :         /* Use the regular indexscan plan build machinery... */
    3155       19070 :         iscan = castNode(IndexScan,
    3156             :                          create_indexscan_plan(root, ipath,
    3157             :                                                NIL, NIL, false));
    3158             :         /* then convert to a bitmap indexscan */
    3159       19070 :         plan = (Plan *) make_bitmap_indexscan(iscan->scan.scanrelid,
    3160             :                                               iscan->indexid,
    3161             :                                               iscan->indexqual,
    3162             :                                               iscan->indexqualorig);
    3163             :         /* and set its cost/width fields appropriately */
    3164       19070 :         plan->startup_cost = 0.0;
    3165       19070 :         plan->total_cost = ipath->indextotalcost;
    3166       19070 :         plan->plan_rows =
    3167       19070 :             clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
    3168       19070 :         plan->plan_width = 0;    /* meaningless */
    3169       19070 :         plan->parallel_aware = false;
    3170       19070 :         plan->parallel_safe = ipath->path.parallel_safe;
    3171             :         /* Extract original index clauses, actual index quals, relevant ECs */
    3172       19070 :         subquals = NIL;
    3173       19070 :         subindexquals = NIL;
    3174       19070 :         subindexECs = NIL;
    3175       38592 :         foreach(l, ipath->indexclauses)
    3176             :         {
    3177       19522 :             IndexClause *iclause = (IndexClause *) lfirst(l);
    3178       19522 :             RestrictInfo *rinfo = iclause->rinfo;
    3179             : 
    3180             :             Assert(!rinfo->pseudoconstant);
    3181       19522 :             subquals = lappend(subquals, rinfo->clause);
    3182       19522 :             subindexquals = list_concat(subindexquals,
    3183             :                                         get_actual_clauses(iclause->indexquals));
    3184       19522 :             if (rinfo->parent_ec)
    3185         112 :                 subindexECs = lappend(subindexECs, rinfo->parent_ec);
    3186             :         }
    3187             :         /* We can add any index predicate conditions, too */
    3188       19194 :         foreach(l, ipath->indexinfo->indpred)
    3189             :         {
    3190         124 :             Expr       *pred = (Expr *) lfirst(l);
    3191             : 
    3192             :             /*
    3193             :              * We know that the index predicate must have been implied by the
    3194             :              * query condition as a whole, but it may or may not be implied by
    3195             :              * the conditions that got pushed into the bitmapqual.  Avoid
    3196             :              * generating redundant conditions.
    3197             :              */
    3198         124 :             if (!predicate_implied_by(list_make1(pred), subquals, false))
    3199             :             {
    3200         104 :                 subquals = lappend(subquals, pred);
    3201         104 :                 subindexquals = lappend(subindexquals, pred);
    3202             :             }
    3203             :         }
    3204       19070 :         *qual = subquals;
    3205       19070 :         *indexqual = subindexquals;
    3206       19070 :         *indexECs = subindexECs;
    3207             :     }
    3208             :     else
    3209             :     {
    3210           0 :         elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
    3211             :         plan = NULL;            /* keep compiler quiet */
    3212             :     }
    3213             : 
    3214       19218 :     return plan;
    3215             : }
    3216             : 
    3217             : /*
    3218             :  * create_tidscan_plan
    3219             :  *   Returns a tidscan plan for the base relation scanned by 'best_path'
    3220             :  *   with restriction clauses 'scan_clauses' and targetlist 'tlist'.
    3221             :  */
    3222             : static TidScan *
    3223         404 : create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
    3224             :                     List *tlist, List *scan_clauses)
    3225             : {
    3226             :     TidScan    *scan_plan;
    3227         404 :     Index       scan_relid = best_path->path.parent->relid;
    3228         404 :     List       *tidquals = best_path->tidquals;
    3229             : 
    3230             :     /* it should be a base rel... */
    3231             :     Assert(scan_relid > 0);
    3232             :     Assert(best_path->path.parent->rtekind == RTE_RELATION);
    3233             : 
    3234             :     /*
    3235             :      * The qpqual list must contain all restrictions not enforced by the
    3236             :      * tidquals list.  Since tidquals has OR semantics, we have to be careful
    3237             :      * about matching it up to scan_clauses.  It's convenient to handle the
    3238             :      * single-tidqual case separately from the multiple-tidqual case.  In the
    3239             :      * single-tidqual case, we look through the scan_clauses while they are
    3240             :      * still in RestrictInfo form, and drop any that are redundant with the
    3241             :      * tidqual.
    3242             :      *
    3243             :      * In normal cases simple pointer equality checks will be enough to spot
    3244             :      * duplicate RestrictInfos, so we try that first.
    3245             :      *
    3246             :      * Another common case is that a scan_clauses entry is generated from the
    3247             :      * same EquivalenceClass as some tidqual, and is therefore redundant with
    3248             :      * it, though not equal.
    3249             :      *
    3250             :      * Unlike indexpaths, we don't bother with predicate_implied_by(); the
    3251             :      * number of cases where it could win are pretty small.
    3252             :      */
    3253         404 :     if (list_length(tidquals) == 1)
    3254             :     {
    3255         388 :         List       *qpqual = NIL;
    3256             :         ListCell   *l;
    3257             : 
    3258         824 :         foreach(l, scan_clauses)
    3259             :         {
    3260         436 :             RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
    3261             : 
    3262         436 :             if (rinfo->pseudoconstant)
    3263           0 :                 continue;       /* we may drop pseudoconstants here */
    3264         436 :             if (list_member_ptr(tidquals, rinfo))
    3265         380 :                 continue;       /* simple duplicate */
    3266          56 :             if (is_redundant_derived_clause(rinfo, tidquals))
    3267           8 :                 continue;       /* derived from same EquivalenceClass */
    3268          48 :             qpqual = lappend(qpqual, rinfo);
    3269             :         }
    3270         388 :         scan_clauses = qpqual;
    3271             :     }
    3272             : 
    3273             :     /* Sort clauses into best execution order */
    3274         404 :     scan_clauses = order_qual_clauses(root, scan_clauses);
    3275             : 
    3276             :     /* Reduce RestrictInfo lists to bare expressions; ignore pseudoconstants */
    3277         404 :     tidquals = extract_actual_clauses(tidquals, false);
    3278         404 :     scan_clauses = extract_actual_clauses(scan_clauses, false);
    3279             : 
    3280             :     /*
    3281             :      * If we have multiple tidquals, it's more convenient to remove duplicate
    3282             :      * scan_clauses after stripping the RestrictInfos.  In this situation,
    3283             :      * because the tidquals represent OR sub-clauses, they could not have come
    3284             :      * from EquivalenceClasses so we don't have to worry about matching up
    3285             :      * non-identical clauses.  On the other hand, because tidpath.c will have
    3286             :      * extracted those sub-clauses from some OR clause and built its own list,
    3287             :      * we will certainly not have pointer equality to any scan clause.  So
    3288             :      * convert the tidquals list to an explicit OR clause and see if we can
    3289             :      * match it via equal() to any scan clause.
    3290             :      */
    3291         404 :     if (list_length(tidquals) > 1)
    3292          16 :         scan_clauses = list_difference(scan_clauses,
    3293          16 :                                        list_make1(make_orclause(tidquals)));
    3294             : 
    3295             :     /* Replace any outer-relation variables with nestloop params */
    3296         404 :     if (best_path->path.param_info)
    3297             :     {
    3298          16 :         tidquals = (List *)
    3299             :             replace_nestloop_params(root, (Node *) tidquals);
    3300          16 :         scan_clauses = (List *)
    3301             :             replace_nestloop_params(root, (Node *) scan_clauses);
    3302             :     }
    3303             : 
    3304         404 :     scan_plan = make_tidscan(tlist,
    3305             :                              scan_clauses,
    3306             :                              scan_relid,
    3307             :                              tidquals);
    3308             : 
    3309         404 :     copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
    3310             : 
    3311         404 :     return scan_plan;
    3312             : }
    3313             : 
    3314             : /*
    3315             :  * create_subqueryscan_plan
    3316             :  *   Returns a subqueryscan plan for the base relation scanned by 'best_path'
    3317             :  *   with restriction clauses 'scan_clauses' and targetlist 'tlist'.
    3318             :  */
    3319             : static SubqueryScan *
    3320        8088 : create_subqueryscan_plan(PlannerInfo *root, SubqueryScanPath *best_path,
    3321             :                          List *tlist, List *scan_clauses)
    3322             : {
    3323             :     SubqueryScan *scan_plan;
    3324        8088 :     RelOptInfo *rel = best_path->path.parent;
    3325        8088 :     Index       scan_relid = rel->relid;
    3326             :     Plan       *subplan;
    3327             : 
    3328             :     /* it should be a subquery base rel... */
    3329             :     Assert(scan_relid > 0);
    3330             :     Assert(rel->rtekind == RTE_SUBQUERY);
    3331             : 
    3332             :     /*
    3333             :      * Recursively create Plan from Path for subquery.  Since we are entering
    3334             :      * a different planner context (subroot), recurse to create_plan not
    3335             :      * create_plan_recurse.
    3336             :      */
    3337        8088 :     subplan = create_plan(rel->subroot, best_path->subpath);
    3338             : 
    3339             :     /* Sort clauses into best execution order */
    3340        8088 :     scan_clauses = order_qual_clauses(root, scan_clauses);
    3341             : 
    3342             :     /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
    3343        8088 :     scan_clauses = extract_actual_clauses(scan_clauses, false);
    3344             : 
    3345             :     /* Replace any outer-relation variables with nestloop params */
    3346        8088 :     if (best_path->path.param_info)
    3347             :     {
    3348         192 :         scan_clauses = (List *)
    3349             :             replace_nestloop_params(root, (Node *) scan_clauses);
    3350         192 :         process_subquery_nestloop_params(root,
    3351             :                                          rel->subplan_params);
    3352             :     }
    3353             : 
    3354        8088 :     scan_plan = make_subqueryscan(tlist,
    3355             :                                   scan_clauses,
    3356             :                                   scan_relid,
    3357             :                                   subplan);
    3358             : 
    3359        8088 :     copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
    3360             : 
    3361        8088 :     return scan_plan;
    3362             : }
    3363             : 
    3364             : /*
    3365             :  * create_functionscan_plan
    3366             :  *   Returns a functionscan plan for the base relation scanned by 'best_path'
    3367             :  *   with restriction clauses 'scan_clauses' and targetlist 'tlist'.
    3368             :  */
    3369             : static FunctionScan *
    3370       28204 : create_functionscan_plan(PlannerInfo *root, Path *best_path,
    3371             :                          List *tlist, List *scan_clauses)
    3372             : {
    3373             :     FunctionScan *scan_plan;
    3374       28204 :     Index       scan_relid = best_path->parent->relid;
    3375             :     RangeTblEntry *rte;
    3376             :     List       *functions;
    3377             : 
    3378             :     /* it should be a function base rel... */
    3379             :     Assert(scan_relid > 0);
    3380       28204 :     rte = planner_rt_fetch(scan_relid, root);
    3381             :     Assert(rte->rtekind == RTE_FUNCTION);
    3382       28204 :     functions = rte->functions;
    3383             : 
    3384             :     /* Sort clauses into best execution order */
    3385       28204 :     scan_clauses = order_qual_clauses(root, scan_clauses);
    3386             : 
    3387             :     /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
    3388       28204 :     scan_clauses = extract_actual_clauses(scan_clauses, false);
    3389             : 
    3390             :     /* Replace any outer-relation variables with nestloop params */
    3391       28204 :     if (best_path->param_info)
    3392             :     {
    3393         296 :         scan_clauses = (List *)
    3394             :             replace_nestloop_params(root, (Node *) scan_clauses);
    3395             :         /* The function expressions could contain nestloop params, too */
    3396         296 :         functions = (List *) replace_nestloop_params(root, (Node *) functions);
    3397             :     }
    3398             : 
    3399       28204 :     scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
    3400       28204 :                                   functions, rte->funcordinality);
    3401             : 
    3402       28204 :     copy_generic_path_info(&scan_plan->scan.plan, best_path);
    3403             : 
    3404       28204 :     return scan_plan;
    3405             : }
    3406             : 
    3407             : /*
    3408             :  * create_tablefuncscan_plan
    3409             :  *   Returns a tablefuncscan plan for the base relation scanned by 'best_path'
    3410             :  *   with restriction clauses 'scan_clauses' and targetlist 'tlist'.
    3411             :  */
    3412             : static TableFuncScan *
    3413         140 : create_tablefuncscan_plan(PlannerInfo *root, Path *best_path,
    3414             :                           List *tlist, List *scan_clauses)
    3415             : {
    3416             :     TableFuncScan *scan_plan;
    3417         140 :     Index       scan_relid = best_path->parent->relid;
    3418             :     RangeTblEntry *rte;
    3419             :     TableFunc  *tablefunc;
    3420             : 
    3421             :     /* it should be a function base rel... */
    3422             :     Assert(scan_relid > 0);
    3423         140 :     rte = planner_rt_fetch(scan_relid, root);
    3424             :     Assert(rte->rtekind == RTE_TABLEFUNC);
    3425         140 :     tablefunc = rte->tablefunc;
    3426             : 
    3427             :     /* Sort clauses into best execution order */
    3428         140 :     scan_clauses = order_qual_clauses(root, scan_clauses);
    3429             : 
    3430             :     /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
    3431         140 :     scan_clauses = extract_actual_clauses(scan_clauses, false);
    3432             : 
    3433             :     /* Replace any outer-relation variables with nestloop params */
    3434         140 :     if (best_path->param_info)
    3435             :     {
    3436          96 :         scan_clauses = (List *)
    3437             :             replace_nestloop_params(root, (Node *) scan_clauses);
    3438             :         /* The function expressions could contain nestloop params, too */
    3439          96 :         tablefunc = (TableFunc *) replace_nestloop_params(root, (Node *) tablefunc);
    3440             :     }
    3441             : 
    3442         140 :     scan_plan = make_tablefuncscan(tlist, scan_clauses, scan_relid,
    3443             :                                    tablefunc);
    3444             : 
    3445         140 :     copy_generic_path_info(&scan_plan->scan.plan, best_path);
    3446             : 
    3447         140 :     return scan_plan;
    3448             : }
    3449             : 
    3450             : /*
    3451             :  * create_valuesscan_plan
    3452             :  *   Returns a valuesscan plan for the base relation scanned by 'best_path'
    3453             :  *   with restriction clauses 'scan_clauses' and targetlist 'tlist'.
    3454             :  */
    3455             : static ValuesScan *
    3456        3730 : create_valuesscan_plan(PlannerInfo *root, Path *best_path,
    3457             :                        List *tlist, List *scan_clauses)
    3458             : {
    3459             :     ValuesScan *scan_plan;
    3460        3730 :     Index       scan_relid = best_path->parent->relid;
    3461             :     RangeTblEntry *rte;
    3462             :     List       *values_lists;
    3463             : 
    3464             :     /* it should be a values base rel... */
    3465             :     Assert(scan_relid > 0);
    3466        3730 :     rte = planner_rt_fetch(scan_relid, root);
    3467             :     Assert(rte->rtekind == RTE_VALUES);
    3468        3730 :     values_lists = rte->values_lists;
    3469             : 
    3470             :     /* Sort clauses into best execution order */
    3471        3730 :     scan_clauses = order_qual_clauses(root, scan_clauses);
    3472             : 
    3473             :     /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
    3474        3730 :     scan_clauses = extract_actual_clauses(scan_clauses, false);
    3475             : 
    3476             :     /* Replace any outer-relation variables with nestloop params */
    3477        3730 :     if (best_path->param_info)
    3478             :     {
    3479          28 :         scan_clauses = (List *)
    3480             :             replace_nestloop_params(root, (Node *) scan_clauses);
    3481             :         /* The values lists could contain nestloop params, too */
    3482          28 :         values_lists = (List *)
    3483             :             replace_nestloop_params(root, (Node *) values_lists);
    3484             :     }
    3485             : 
    3486        3730 :     scan_plan = make_valuesscan(tlist, scan_clauses, scan_relid,
    3487             :                                 values_lists);
    3488             : 
    3489        3730 :     copy_generic_path_info(&scan_plan->scan.plan, best_path);
    3490             : 
    3491        3730 :     return scan_plan;
    3492             : }
    3493             : 
    3494             : /*
    3495             :  * create_ctescan_plan
    3496             :  *   Returns a ctescan plan for the base relation scanned by 'best_path'
    3497             :  *   with restriction clauses 'scan_clauses' and targetlist 'tlist'.
    3498             :  */
    3499             : static CteScan *
    3500         868 : create_ctescan_plan(PlannerInfo *root, Path *best_path,
    3501             :                     List *tlist, List *scan_clauses)
    3502             : {
    3503             :     CteScan    *scan_plan;
    3504         868 :     Index       scan_relid = best_path->parent->relid;
    3505             :     RangeTblEntry *rte;
    3506         868 :     SubPlan    *ctesplan = NULL;
    3507             :     int         plan_id;
    3508             :     int         cte_param_id;
    3509             :     PlannerInfo *cteroot;
    3510             :     Index       levelsup;
    3511             :     int         ndx;
    3512             :     ListCell   *lc;
    3513             : 
    3514             :     Assert(scan_relid > 0);
    3515         868 :     rte = planner_rt_fetch(scan_relid, root);
    3516             :     Assert(rte->rtekind == RTE_CTE);
    3517             :     Assert(!rte->self_reference);
    3518             : 
    3519             :     /*
    3520             :      * Find the referenced CTE, and locate the SubPlan previously made for it.
    3521             :      */
    3522         868 :     levelsup = rte->ctelevelsup;
    3523         868 :     cteroot = root;
    3524        2002 :     while (levelsup-- > 0)
    3525             :     {
    3526         266 :         cteroot = cteroot->parent_root;
    3527         266 :         if (!cteroot)           /* shouldn't happen */
    3528           0 :             elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
    3529             :     }
    3530             : 
    3531             :     /*
    3532             :      * Note: cte_plan_ids can be shorter than cteList, if we are still working
    3533             :      * on planning the CTEs (ie, this is a side-reference from another CTE).
    3534             :      * So we mustn't use forboth here.
    3535             :      */
    3536         868 :     ndx = 0;
    3537         990 :     foreach(lc, cteroot->parse->cteList)
    3538             :     {
    3539         990 :         CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
    3540             : 
    3541         990 :         if (strcmp(cte->ctename, rte->ctename) == 0)
    3542         868 :             break;
    3543         122 :         ndx++;
    3544             :     }
    3545         868 :     if (lc == NULL)             /* shouldn't happen */
    3546           0 :         elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
    3547         868 :     if (ndx >= list_length(cteroot->cte_plan_ids))
    3548           0 :         elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
    3549         868 :     plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
    3550             :     Assert(plan_id > 0);
    3551         952 :     foreach(lc, cteroot->init_plans)
    3552             :     {
    3553         952 :         ctesplan = (SubPlan *) lfirst(lc);
    3554         952 :         if (ctesplan->plan_id == plan_id)
    3555         868 :             break;
    3556             :     }
    3557         868 :     if (lc == NULL)             /* shouldn't happen */
    3558           0 :         elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
    3559             : 
    3560             :     /*
    3561             :      * We need the CTE param ID, which is the sole member of the SubPlan's
    3562             :      * setParam list.
    3563             :      */
    3564         868 :     cte_param_id = linitial_int(ctesplan->setParam);
    3565             : 
    3566             :     /* Sort clauses into best execution order */
    3567         868 :     scan_clauses = order_qual_clauses(root, scan_clauses);
    3568             : 
    3569             :     /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
    3570         868 :     scan_clauses = extract_actual_clauses(scan_clauses, false);
    3571             : 
    3572             :     /* Replace any outer-relation variables with nestloop params */
    3573         868 :     if (best_path->param_info)
    3574             :     {
    3575           0 :         scan_clauses = (List *)
    3576             :             replace_nestloop_params(root, (Node *) scan_clauses);
    3577             :     }
    3578             : 
    3579         868 :     scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
    3580             :                              plan_id, cte_param_id);
    3581             : 
    3582         868 :     copy_generic_path_info(&scan_plan->scan.plan, best_path);
    3583             : 
    3584         868 :     return scan_plan;
    3585             : }
    3586             : 
    3587             : /*
    3588             :  * create_namedtuplestorescan_plan
    3589             :  *   Returns a tuplestorescan plan for the base relation scanned by
    3590             :  *  'best_path' with restriction clauses 'scan_clauses' and targetlist
    3591             :  *  'tlist'.
    3592             :  */
    3593             : static NamedTuplestoreScan *
    3594         260 : create_namedtuplestorescan_plan(PlannerInfo *root, Path *best_path,
    3595             :                                 List *tlist, List *scan_clauses)
    3596             : {
    3597             :     NamedTuplestoreScan *scan_plan;
    3598         260 :     Index       scan_relid = best_path->parent->relid;
    3599             :     RangeTblEntry *rte;
    3600             : 
    3601             :     Assert(scan_relid > 0);
    3602         260 :     rte = planner_rt_fetch(scan_relid, root);
    3603             :     Assert(rte->rtekind == RTE_NAMEDTUPLESTORE);
    3604             : 
    3605             :     /* Sort clauses into best execution order */
    3606         260 :     scan_clauses = order_qual_clauses(root, scan_clauses);
    3607             : 
    3608             :     /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
    3609         260 :     scan_clauses = extract_actual_clauses(scan_clauses, false);
    3610             : 
    3611             :     /* Replace any outer-relation variables with nestloop params */
    3612         260 :     if (best_path->param_info)
    3613             :     {
    3614           0 :         scan_clauses = (List *)
    3615             :             replace_nestloop_params(root, (Node *) scan_clauses);
    3616             :     }
    3617             : 
    3618         260 :     scan_plan = make_namedtuplestorescan(tlist, scan_clauses, scan_relid,
    3619             :                                          rte->enrname);
    3620             : 
    3621         260 :     copy_generic_path_info(&scan_plan->scan.plan, best_path);
    3622             : 
    3623         260 :     return scan_plan;
    3624             : }
    3625             : 
    3626             : /*
    3627             :  * create_resultscan_plan
    3628             :  *   Returns a Result plan for the RTE_RESULT base relation scanned by
    3629             :  *  'best_path' with restriction clauses 'scan_clauses' and targetlist
    3630             :  *  'tlist'.
    3631             :  */
    3632             : static Result *
    3633         526 : create_resultscan_plan(PlannerInfo *root, Path *best_path,
    3634             :                        List *tlist, List *scan_clauses)
    3635             : {
    3636             :     Result     *scan_plan;
    3637         526 :     Index       scan_relid = best_path->parent->relid;
    3638             :     RangeTblEntry *rte PG_USED_FOR_ASSERTS_ONLY;
    3639             : 
    3640             :     Assert(scan_relid > 0);
    3641         526 :     rte = planner_rt_fetch(scan_relid, root);
    3642             :     Assert(rte->rtekind == RTE_RESULT);
    3643             : 
    3644             :     /* Sort clauses into best execution order */
    3645         526 :     scan_clauses = order_qual_clauses(root, scan_clauses);
    3646             : 
    3647             :     /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
    3648         526 :     scan_clauses = extract_actual_clauses(scan_clauses, false);
    3649             : 
    3650             :     /* Replace any outer-relation variables with nestloop params */
    3651         526 :     if (best_path->param_info)
    3652             :     {
    3653          60 :         scan_clauses = (List *)
    3654             :             replace_nestloop_params(root, (Node *) scan_clauses);
    3655             :     }
    3656             : 
    3657         526 :     scan_plan = make_result(tlist, (Node *) scan_clauses, NULL);
    3658             : 
    3659         526 :     copy_generic_path_info(&scan_plan->plan, best_path);
    3660             : 
    3661         526 :     return scan_plan;
    3662             : }
    3663             : 
    3664             : /*
    3665             :  * create_worktablescan_plan
    3666             :  *   Returns a worktablescan plan for the base relation scanned by 'best_path'
    3667             :  *   with restriction clauses 'scan_clauses' and targetlist 'tlist'.
    3668             :  */
    3669             : static WorkTableScan *
    3670         326 : create_worktablescan_plan(PlannerInfo *root, Path *best_path,
    3671             :                           List *tlist, List *scan_clauses)
    3672             : {
    3673             :     WorkTableScan *scan_plan;
    3674         326 :     Index       scan_relid = best_path->parent->relid;
    3675             :     RangeTblEntry *rte;
    3676             :     Index       levelsup;
    3677             :     PlannerInfo *cteroot;
    3678             : 
    3679             :     Assert(scan_relid > 0);
    3680         326 :     rte = planner_rt_fetch(scan_relid, root);
    3681             :     Assert(rte->rtekind == RTE_CTE);
    3682             :     Assert(rte->self_reference);
    3683             : 
    3684             :     /*
    3685             :      * We need to find the worktable param ID, which is in the plan level
    3686             :      * that's processing the recursive UNION, which is one level *below* where
    3687             :      * the CTE comes from.
    3688             :      */
    3689         326 :     levelsup = rte->ctelevelsup;
    3690         326 :     if (levelsup == 0)          /* shouldn't happen */
    3691           0 :         elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
    3692         326 :     levelsup--;
    3693         326 :     cteroot = root;
    3694        1014 :     while (levelsup-- > 0)
    3695             :     {
    3696         362 :         cteroot = cteroot->parent_root;
    3697         362 :         if (!cteroot)           /* shouldn't happen */
    3698           0 :             elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
    3699             :     }
    3700         326 :     if (cteroot->wt_param_id < 0) /* shouldn't happen */
    3701           0 :         elog(ERROR, "could not find param ID for CTE \"%s\"", rte->ctename);
    3702             : 
    3703             :     /* Sort clauses into best execution order */
    3704         326 :     scan_clauses = order_qual_clauses(root, scan_clauses);
    3705             : 
    3706             :     /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
    3707         326 :     scan_clauses = extract_actual_clauses(scan_clauses, false);
    3708             : 
    3709             :     /* Replace any outer-relation variables with nestloop params */
    3710         326 :     if (best_path->param_info)
    3711             :     {
    3712           0 :         scan_clauses = (List *)
    3713             :             replace_nestloop_params(root, (Node *) scan_clauses);
    3714             :     }
    3715             : 
    3716         326 :     scan_plan = make_worktablescan(tlist, scan_clauses, scan_relid,
    3717             :                                    cteroot->wt_param_id);
    3718             : 
    3719         326 :     copy_generic_path_info(&scan_plan->scan.plan, best_path);
    3720             : 
    3721         326 :     return scan_plan;
    3722             : }
    3723             : 
    3724             : /*
    3725             :  * create_foreignscan_plan
    3726             :  *   Returns a foreignscan plan for the relation scanned by 'best_path'
    3727             :  *   with restriction clauses 'scan_clauses' and targetlist 'tlist'.
    3728             :  */
    3729             : static ForeignScan *
    3730        1384 : create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
    3731             :                         List *tlist, List *scan_clauses)
    3732             : {
    3733             :     ForeignScan *scan_plan;
    3734        1384 :     RelOptInfo *rel = best_path->path.parent;
    3735        1384 :     Index       scan_relid = rel->relid;
    3736        1384 :     Oid         rel_oid = InvalidOid;
    3737        1384 :     Plan       *outer_plan = NULL;
    3738             : 
    3739             :     Assert(rel->fdwroutine != NULL);
    3740             : 
    3741             :     /* transform the child path if any */
    3742        1384 :     if (best_path->fdw_outerpath)
    3743          40 :         outer_plan = create_plan_recurse(root, best_path->fdw_outerpath,
    3744             :                                          CP_EXACT_TLIST);
    3745             : 
    3746             :     /*
    3747             :      * If we're scanning a base relation, fetch its OID.  (Irrelevant if
    3748             :      * scanning a join relation.)
    3749             :      */
    3750        1384 :     if (scan_relid > 0)
    3751             :     {
    3752             :         RangeTblEntry *rte;
    3753             : 
    3754             :         Assert(rel->rtekind == RTE_RELATION);
    3755         950 :         rte = planner_rt_fetch(scan_relid, root);
    3756             :         Assert(rte->rtekind == RTE_RELATION);
    3757         950 :         rel_oid = rte->relid;
    3758             :     }
    3759             : 
    3760             :     /*
    3761             :      * Sort clauses into best execution order.  We do this first since the FDW
    3762             :      * might have more info than we do and wish to adjust the ordering.
    3763             :      */
    3764        1384 :     scan_clauses = order_qual_clauses(root, scan_clauses);
    3765             : 
    3766             :     /*
    3767             :      * Let the FDW perform its processing on the restriction clauses and
    3768             :      * generate the plan node.  Note that the FDW might remove restriction
    3769             :      * clauses that it intends to execute remotely, or even add more (if it
    3770             :      * has selected some join clauses for remote use but also wants them
    3771             :      * rechecked locally).
    3772             :      */
    3773        1384 :     scan_plan = rel->fdwroutine->GetForeignPlan(root, rel, rel_oid,
    3774             :                                                 best_path,
    3775             :                                                 tlist, scan_clauses,
    3776             :                                                 outer_plan);
    3777             : 
    3778             :     /* Copy cost data from Path to Plan; no need to make FDW do this */
    3779        1384 :     copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
    3780             : 
    3781             :     /* Copy foreign server OID; likewise, no need to make FDW do this */
    3782        1384 :     scan_plan->fs_server = rel->serverid;
    3783             : 
    3784             :     /*
    3785             :      * Likewise, copy the relids that are represented by this foreign scan. An
    3786             :      * upper rel doesn't have relids set, but it covers all the base relations
    3787             :      * participating in the underlying scan, so use root's all_baserels.
    3788             :      */
    3789        1384 :     if (rel->reloptkind == RELOPT_UPPER_REL)
    3790         184 :         scan_plan->fs_relids = root->all_baserels;
    3791             :     else
    3792        1200 :         scan_plan->fs_relids = best_path->path.parent->relids;
    3793             : 
    3794             :     /*
    3795             :      * If this is a foreign join, and to make it valid to push down we had to
    3796             :      * assume that the current user is the same as some user explicitly named
    3797             :      * in the query, mark the finished plan as depending on the current user.
    3798             :      */
    3799        1384 :     if (rel->useridiscurrent)
    3800           4 :         root->glob->dependsOnRole = true;
    3801             : 
    3802             :     /*
    3803             :      * Replace any outer-relation variables with nestloop params in the qual,
    3804             :      * fdw_exprs and fdw_recheck_quals expressions.  We do this last so that
    3805             :      * the FDW doesn't have to be involved.  (Note that parts of fdw_exprs or
    3806             :      * fdw_recheck_quals could have come from join clauses, so doing this
    3807             :      * beforehand on the scan_clauses wouldn't work.)  We assume
    3808             :      * fdw_scan_tlist contains no such variables.
    3809             :      */
    3810        1384 :     if (best_path->path.param_info)
    3811             :     {
    3812          12 :         scan_plan->scan.plan.qual = (List *)
    3813          12 :             replace_nestloop_params(root, (Node *) scan_plan->scan.plan.qual);
    3814          12 :         scan_plan->fdw_exprs = (List *)
    3815          12 :             replace_nestloop_params(root, (Node *) scan_plan->fdw_exprs);
    3816          12 :         scan_plan->fdw_recheck_quals = (List *)
    3817          12 :             replace_nestloop_params(root,
    3818          12 :                                     (Node *) scan_plan->fdw_recheck_quals);
    3819             :     }
    3820             : 
    3821             :     /*
    3822             :      * If rel is a base relation, detect whether any system columns are
    3823             :      * requested from the rel.  (If rel is a join relation, rel->relid will be
    3824             :      * 0, but there can be no Var with relid 0 in the rel's targetlist or the
    3825             :      * restriction clauses, so we skip this in that case.  Note that any such
    3826             :      * columns in base relations that were joined are assumed to be contained
    3827             :      * in fdw_scan_tlist.)  This is a bit of a kluge and might go away
    3828             :      * someday, so we intentionally leave it out of the API presented to FDWs.
    3829             :      */
    3830        1384 :     scan_plan->fsSystemCol = false;
    3831        1384 :     if (scan_relid > 0)
    3832             :     {
    3833         950 :         Bitmapset  *attrs_used = NULL;
    3834             :         ListCell   *lc;
    3835             :         int         i;
    3836             : 
    3837             :         /*
    3838             :          * First, examine all the attributes needed for joins or final output.
    3839             :          * Note: we must look at rel's targetlist, not the attr_needed data,
    3840             :          * because attr_needed isn't computed for inheritance child rels.
    3841             :          */
    3842         950 :         pull_varattnos((Node *) rel->reltarget->exprs, scan_relid, &attrs_used);
    3843             : 
    3844             :         /* Add all the attributes used by restriction clauses. */
    3845        1414 :         foreach(lc, rel->baserestrictinfo)
    3846             :         {
    3847         464 :             RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
    3848             : 
    3849         464 :             pull_varattnos((Node *) rinfo->clause, scan_relid, &attrs_used);
    3850             :         }
    3851             : 
    3852             :         /* Now, are any system columns requested from rel? */
    3853        5652 :         for (i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++)
    3854             :         {
    3855        5070 :             if (bms_is_member(i - FirstLowInvalidHeapAttributeNumber, attrs_used))
    3856             :             {
    3857         368 :                 scan_plan->fsSystemCol = true;
    3858         368 :                 break;
    3859             :             }
    3860             :         }
    3861             : 
    3862         950 :         bms_free(attrs_used);
    3863             :     }
    3864             : 
    3865        1384 :     return scan_plan;
    3866             : }
    3867             : 
    3868             : /*
    3869             :  * create_custom_plan
    3870             :  *
    3871             :  * Transform a CustomPath into a Plan.
    3872             :  */
    3873             : static CustomScan *
    3874           0 : create_customscan_plan(PlannerInfo *root, CustomPath *best_path,
    3875             :                        List *tlist, List *scan_clauses)
    3876             : {
    3877             :     CustomScan *cplan;
    3878           0 :     RelOptInfo *rel = best_path->path.parent;
    3879           0 :     List       *custom_plans = NIL;
    3880             :     ListCell   *lc;
    3881             : 
    3882             :     /* Recursively transform child paths. */
    3883           0 :     foreach(lc, best_path->custom_paths)
    3884             :     {
    3885           0 :         Plan       *plan = create_plan_recurse(root, (Path *) lfirst(lc),
    3886             :                                                CP_EXACT_TLIST);
    3887             : 
    3888           0 :         custom_plans = lappend(custom_plans, plan);
    3889             :     }
    3890             : 
    3891             :     /*
    3892             :      * Sort clauses into the best execution order, although custom-scan
    3893             :      * provider can reorder them again.
    3894             :      */
    3895           0 :     scan_clauses = order_qual_clauses(root, scan_clauses);
    3896             : 
    3897             :     /*
    3898             :      * Invoke custom plan provider to create the Plan node represented by the
    3899             :      * CustomPath.
    3900             :      */
    3901           0 :     cplan = castNode(CustomScan,
    3902             :                      best_path->methods->PlanCustomPath(root,
    3903             :                                                         rel,
    3904             :                                                         best_path,
    3905             :                                                         tlist,
    3906             :                                                         scan_clauses,
    3907             :                                                         custom_plans));
    3908             : 
    3909             :     /*
    3910             :      * Copy cost data from Path to Plan; no need to make custom-plan providers
    3911             :      * do this
    3912             :      */
    3913           0 :     copy_generic_path_info(&cplan->scan.plan, &best_path->path);
    3914             : 
    3915             :     /* Likewise, copy the relids that are represented by this custom scan */
    3916           0 :     cplan->custom_relids = best_path->path.parent->relids;
    3917             : 
    3918             :     /*
    3919             :      * Replace any outer-relation variables with nestloop params in the qual
    3920             :      * and custom_exprs expressions.  We do this last so that the custom-plan
    3921             :      * provider doesn't have to be involved.  (Note that parts of custom_exprs
    3922             :      * could have come from join clauses, so doing this beforehand on the
    3923             :      * scan_clauses wouldn't work.)  We assume custom_scan_tlist contains no
    3924             :      * such variables.
    3925             :      */
    3926           0 :     if (best_path->path.param_info)
    3927             :     {
    3928           0 :         cplan->scan.plan.qual = (List *)
    3929           0 :             replace_nestloop_params(root, (Node *) cplan->scan.plan.qual);
    3930           0 :         cplan->custom_exprs = (List *)
    3931           0 :             replace_nestloop_params(root, (Node *) cplan->custom_exprs);
    3932             :     }
    3933             : 
    3934           0 :     return cplan;
    3935             : }
    3936             : 
    3937             : 
    3938             : /*****************************************************************************
    3939             :  *
    3940             :  *  JOIN METHODS
    3941             :  *
    3942             :  *****************************************************************************/
    3943             : 
    3944             : static NestLoop *
    3945       29926 : create_nestloop_plan(PlannerInfo *root,
    3946             :                      NestPath *best_path)
    3947             : {
    3948             :     NestLoop   *join_plan;
    3949             :     Plan       *outer_plan;
    3950             :     Plan       *inner_plan;
    3951       29926 :     List       *tlist = build_path_tlist(root, &best_path->path);
    3952       29926 :     List       *joinrestrictclauses = best_path->joinrestrictinfo;
    3953             :     List       *joinclauses;
    3954             :     List       *otherclauses;
    3955             :     Relids      outerrelids;
    3956             :     List       *nestParams;
    3957       29926 :     Relids      saveOuterRels = root->curOuterRels;
    3958             : 
    3959             :     /* NestLoop can project, so no need to be picky about child tlists */
    3960       29926 :     outer_plan = create_plan_recurse(root, best_path->outerjoinpath, 0);
    3961             : 
    3962             :     /* For a nestloop, include outer relids in curOuterRels for inner side */
    3963       29926 :     root->curOuterRels = bms_union(root->curOuterRels,
    3964       29926 :                                    best_path->outerjoinpath->parent->relids);
    3965             : 
    3966       29926 :     inner_plan = create_plan_recurse(root, best_path->innerjoinpath, 0);
    3967             : 
    3968             :     /* Restore curOuterRels */
    3969       29926 :     bms_free(root->curOuterRels);
    3970       29926 :     root->curOuterRels = saveOuterRels;
    3971             : 
    3972             :     /* Sort join qual clauses into best execution order */
    3973       29926 :     joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
    3974             : 
    3975             :     /* Get the join qual clauses (in plain expression form) */
    3976             :     /* Any pseudoconstant clauses are ignored here */
    3977       29926 :     if (IS_OUTER_JOIN(best_path->jointype))
    3978             :     {
    3979       13862 :         extract_actual_join_clauses(joinrestrictclauses,
    3980       13862 :                                     best_path->path.parent->relids,
    3981             :                                     &joinclauses, &otherclauses);
    3982             :     }
    3983             :     else
    3984             :     {
    3985             :         /* We can treat all clauses alike for an inner join */
    3986       16064 :         joinclauses = extract_actual_clauses(joinrestrictclauses, false);
    3987       16064 :         otherclauses = NIL;
    3988             :     }
    3989             : 
    3990             :     /* Replace any outer-relation variables with nestloop params */
    3991       29926 :     if (best_path->path.param_info)
    3992             :     {
    3993         376 :         joinclauses = (List *)
    3994         376 :             replace_nestloop_params(root, (Node *) joinclauses);
    3995         376 :         otherclauses = (List *)
    3996         376 :             replace_nestloop_params(root, (Node *) otherclauses);
    3997             :     }
    3998             : 
    3999             :     /*
    4000             :      * Identify any nestloop parameters that should be supplied by this join
    4001             :      * node, and remove them from root->curOuterParams.
    4002             :      */
    4003       29926 :     outerrelids = best_path->outerjoinpath->parent->relids;
    4004       29926 :     nestParams = identify_current_nestloop_params(root, outerrelids);
    4005             : 
    4006       29926 :     join_plan = make_nestloop(tlist,
    4007             :                               joinclauses,
    4008             :                               otherclauses,
    4009             :                               nestParams,
    4010             :                               outer_plan,
    4011             :                               inner_plan,
    4012             :                               best_path->jointype,
    4013       29926 :                               best_path->inner_unique);
    4014             : 
    4015       29926 :     copy_generic_path_info(&join_plan->join.plan, &best_path->path);
    4016             : 
    4017       29926 :     return join_plan;
    4018             : }
    4019             : 
    4020             : static MergeJoin *
    4021        1926 : create_mergejoin_plan(PlannerInfo *root,
    4022             :                       MergePath *best_path)
    4023             : {
    4024             :     MergeJoin  *join_plan;
    4025             :     Plan       *outer_plan;
    4026             :     Plan       *inner_plan;
    4027        1926 :     List       *tlist = build_path_tlist(root, &best_path->jpath.path);
    4028             :     List       *joinclauses;
    4029             :     List       *otherclauses;
    4030             :     List       *mergeclauses;
    4031             :     List       *outerpathkeys;
    4032             :     List       *innerpathkeys;
    4033             :     int         nClauses;
    4034             :     Oid        *mergefamilies;
    4035             :     Oid        *mergecollations;
    4036             :     int        *mergestrategies;
    4037             :     bool       *mergenullsfirst;
    4038             :     PathKey    *opathkey;
    4039             :     EquivalenceClass *opeclass;
    4040             :     int         i;
    4041             :     ListCell   *lc;
    4042             :     ListCell   *lop;
    4043             :     ListCell   *lip;
    4044        1926 :     Path       *outer_path = best_path->jpath.outerjoinpath;
    4045        1926 :     Path       *inner_path = best_path->jpath.innerjoinpath;
    4046             : 
    4047             :     /*
    4048             :      * MergeJoin can project, so we don't have to demand exact tlists from the
    4049             :      * inputs.  However, if we're intending to sort an input's result, it's
    4050             :      * best to request a small tlist so we aren't sorting more data than
    4051             :      * necessary.
    4052             :      */
    4053        1926 :     outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
    4054        1926 :                                      (best_path->outersortkeys != NIL) ? CP_SMALL_TLIST : 0);
    4055             : 
    4056        1926 :     inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
    4057        1926 :                                      (best_path->innersortkeys != NIL) ? CP_SMALL_TLIST : 0);
    4058             : 
    4059             :     /* Sort join qual clauses into best execution order */
    4060             :     /* NB: do NOT reorder the mergeclauses */
    4061        1926 :     joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
    4062             : 
    4063             :     /* Get the join qual clauses (in plain expression form) */
    4064             :     /* Any pseudoconstant clauses are ignored here */
    4065        1926 :     if (IS_OUTER_JOIN(best_path->jpath.jointype))
    4066             :     {
    4067        1146 :         extract_actual_join_clauses(joinclauses,
    4068        1146 :                                     best_path->jpath.path.parent->relids,
    4069             :                                     &joinclauses, &otherclauses);
    4070             :     }
    4071             :     else
    4072             :     {
    4073             :         /* We can treat all clauses alike for an inner join */
    4074         780 :         joinclauses = extract_actual_clauses(joinclauses, false);
    4075         780 :         otherclauses = NIL;
    4076             :     }
    4077             : 
    4078             :     /*
    4079             :      * Remove the mergeclauses from the list of join qual clauses, leaving the
    4080             :      * list of quals that must be checked as qpquals.
    4081             :      */
    4082        1926 :     mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
    4083        1926 :     joinclauses = list_difference(joinclauses, mergeclauses);
    4084             : 
    4085             :     /*
    4086             :      * Replace any outer-relation variables with nestloop params.  There
    4087             :      * should not be any in the mergeclauses.
    4088             :      */
    4089        1926 :     if (best_path->jpath.path.param_info)
    4090             :     {
    4091           4 :         joinclauses = (List *)
    4092           4 :             replace_nestloop_params(root, (Node *) joinclauses);
    4093           4 :         otherclauses = (List *)
    4094           4 :             replace_nestloop_params(root, (Node *) otherclauses);
    4095             :     }
    4096             : 
    4097             :     /*
    4098             :      * Rearrange mergeclauses, if needed, so that the outer variable is always
    4099             :      * on the left; mark the mergeclause restrictinfos with correct
    4100             :      * outer_is_left status.
    4101             :      */
    4102        1926 :     mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
    4103        1926 :                                         best_path->jpath.outerjoinpath->parent->relids);
    4104             : 
    4105             :     /*
    4106             :      * Create explicit sort nodes for the outer and inner paths if necessary.
    4107             :      */
    4108        1926 :     if (best_path->outersortkeys)
    4109             :     {
    4110        1180 :         Relids      outer_relids = outer_path->parent->relids;
    4111        1180 :         Sort       *sort = make_sort_from_pathkeys(outer_plan,
    4112             :                                                    best_path->outersortkeys,
    4113             :                                                    outer_relids);
    4114             : 
    4115        1180 :         label_sort_with_costsize(root, sort, -1.0);
    4116        1180 :         outer_plan = (Plan *) sort;
    4117        1180 :         outerpathkeys = best_path->outersortkeys;
    4118             :     }
    4119             :     else
    4120         746 :         outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
    4121             : 
    4122        1926 :     if (best_path->innersortkeys)
    4123             :     {
    4124        1594 :         Relids      inner_relids = inner_path->parent->relids;
    4125        1594 :         Sort       *sort = make_sort_from_pathkeys(inner_plan,
    4126             :                                                    best_path->innersortkeys,
    4127             :                                                    inner_relids);
    4128             : 
    4129        1594 :         label_sort_with_costsize(root, sort, -1.0);
    4130        1594 :         inner_plan = (Plan *) sort;
    4131        1594 :         innerpathkeys = best_path->innersortkeys;
    4132             :     }
    4133             :     else
    4134         332 :         innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
    4135             : 
    4136             :     /*
    4137             :      * If specified, add a materialize node to shield the inner plan from the
    4138             :      * need to handle mark/restore.
    4139             :      */
    4140        1926 :     if (best_path->materialize_inner)
    4141             :     {
    4142          82 :         Plan       *matplan = (Plan *) make_material(inner_plan);
    4143             : 
    4144             :         /*
    4145             :          * We assume the materialize will not spill to disk, and therefore
    4146             :          * charge just cpu_operator_cost per tuple.  (Keep this estimate in
    4147             :          * sync with final_cost_mergejoin.)
    4148             :          */
    4149          82 :         copy_plan_costsize(matplan, inner_plan);
    4150          82 :         matplan->total_cost += cpu_operator_cost * matplan->plan_rows;
    4151             : 
    4152          82 :         inner_plan = matplan;
    4153             :     }
    4154             : 
    4155             :     /*
    4156             :      * Compute the opfamily/collation/strategy/nullsfirst arrays needed by the
    4157             :      * executor.  The information is in the pathkeys for the two inputs, but
    4158             :      * we need to be careful about the possibility of mergeclauses sharing a
    4159             :      * pathkey, as well as the possibility that the inner pathkeys are not in
    4160             :      * an order matching the mergeclauses.
    4161             :      */
    4162        1926 :     nClauses = list_length(mergeclauses);
    4163             :     Assert(nClauses == list_length(best_path->path_mergeclauses));
    4164        1926 :     mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
    4165        1926 :     mergecollations = (Oid *) palloc(nClauses * sizeof(Oid));
    4166        1926 :     mergestrategies = (int *) palloc(nClauses * sizeof(int));
    4167        1926 :     mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
    4168             : 
    4169        1926 :     opathkey = NULL;
    4170        1926 :     opeclass = NULL;
    4171        1926 :     lop = list_head(outerpathkeys);
    4172        1926 :     lip = list_head(innerpathkeys);
    4173        1926 :     i = 0;
    4174        3994 :     foreach(lc, best_path->path_mergeclauses)
    4175             :     {
    4176        2068 :         RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
    4177             :         EquivalenceClass *oeclass;
    4178             :         EquivalenceClass *ieclass;
    4179        2068 :         PathKey    *ipathkey = NULL;
    4180        2068 :         EquivalenceClass *ipeclass = NULL;
    4181        2068 :         bool        first_inner_match = false;
    4182             : 
    4183             :         /* fetch outer/inner eclass from mergeclause */
    4184        2068 :         if (rinfo->outer_is_left)
    4185             :         {
    4186        1450 :             oeclass = rinfo->left_ec;
    4187        1450 :             ieclass = rinfo->right_ec;
    4188             :         }
    4189             :         else
    4190             :         {
    4191         618 :             oeclass = rinfo->right_ec;
    4192         618 :             ieclass = rinfo->left_ec;
    4193             :         }
    4194             :         Assert(oeclass != NULL);
    4195             :         Assert(ieclass != NULL);
    4196             : 
    4197             :         /*
    4198             :          * We must identify the pathkey elements associated with this clause
    4199             :          * by matching the eclasses (which should give a unique match, since
    4200             :          * the pathkey lists should be canonical).  In typical cases the merge
    4201             :          * clauses are one-to-one with the pathkeys, but when dealing with
    4202             :          * partially redundant query conditions, things are more complicated.
    4203             :          *
    4204             :          * lop and lip reference the first as-yet-unmatched pathkey elements.
    4205             :          * If they're NULL then all pathkey elements have been matched.
    4206             :          *
    4207             :          * The ordering of the outer pathkeys should match the mergeclauses,
    4208             :          * by construction (see find_mergeclauses_for_outer_pathkeys()). There
    4209             :          * could be more than one mergeclause for the same outer pathkey, but
    4210             :          * no pathkey may be entirely skipped over.
    4211             :          */
    4212        2068 :         if (oeclass != opeclass)    /* multiple matches are not interesting */
    4213             :         {
    4214             :             /* doesn't match the current opathkey, so must match the next */
    4215        2060 :             if (lop == NULL)
    4216           0 :                 elog(ERROR, "outer pathkeys do not match mergeclauses");
    4217        2060 :             opathkey = (PathKey *) lfirst(lop);
    4218        2060 :             opeclass = opathkey->pk_eclass;
    4219        2060 :             lop = lnext(lop);
    4220        2060 :             if (oeclass != opeclass)
    4221           0 :                 elog(ERROR, "outer pathkeys do not match mergeclauses");
    4222             :         }
    4223             : 
    4224             :         /*
    4225             :          * The inner pathkeys likewise should not have skipped-over keys, but
    4226             :          * it's possible for a mergeclause to reference some earlier inner
    4227             :          * pathkey if we had redundant pathkeys.  For example we might have
    4228             :          * mergeclauses like "o.a = i.x AND o.b = i.y AND o.c = i.x".  The
    4229             :          * implied inner ordering is then "ORDER BY x, y, x", but the pathkey
    4230             :          * mechanism drops the second sort by x as redundant, and this code
    4231             :          * must cope.
    4232             :          *
    4233             :          * It's also possible for the implied inner-rel ordering to be like
    4234             :          * "ORDER BY x, y, x DESC".  We still drop the second instance of x as
    4235             :          * redundant; but this means that the sort ordering of a redundant
    4236             :          * inner pathkey should not be considered significant.  So we must
    4237             :          * detect whether this is the first clause matching an inner pathkey.
    4238             :          */
    4239        2068 :         if (lip)
    4240             :         {
    4241        2056 :             ipathkey = (PathKey *) lfirst(lip);
    4242        2056 :             ipeclass = ipathkey->pk_eclass;
    4243        2056 :             if (ieclass == ipeclass)
    4244             :             {
    4245             :                 /* successful first match to this inner pathkey */
    4246        2056 :                 lip = lnext(lip);
    4247        2056 :                 first_inner_match = true;
    4248             :             }
    4249             :         }
    4250        2068 :         if (!first_inner_match)
    4251             :         {
    4252             :             /* redundant clause ... must match something before lip */
    4253             :             ListCell   *l2;
    4254             : 
    4255          12 :             foreach(l2, innerpathkeys)
    4256             :             {
    4257          12 :                 if (l2 == lip)
    4258           0 :                     break;
    4259          12 :                 ipathkey = (PathKey *) lfirst(l2);
    4260          12 :                 ipeclass = ipathkey->pk_eclass;
    4261          12 :                 if (ieclass == ipeclass)
    4262          12 :                     break;
    4263             :             }
    4264          12 :             if (ieclass != ipeclass)
    4265           0 :                 elog(ERROR, "inner pathkeys do not match mergeclauses");
    4266             :         }
    4267             : 
    4268             :         /*
    4269             :          * The pathkeys should always match each other as to opfamily and
    4270             :          * collation (which affect equality), but if we're considering a
    4271             :          * redundant inner pathkey, its sort ordering might not match.  In
    4272             :          * such cases we may ignore the inner pathkey's sort ordering and use
    4273             :          * the outer's.  (In effect, we're lying to the executor about the
    4274             :          * sort direction of this inner column, but it does not matter since
    4275             :          * the run-time row comparisons would only reach this column when
    4276             :          * there's equality for the earlier column containing the same eclass.
    4277             :          * There could be only one value in this column for the range of inner
    4278             :          * rows having a given value in the earlier column, so it does not
    4279             :          * matter which way we imagine this column to be ordered.)  But a
    4280             :          * non-redundant inner pathkey had better match outer's ordering too.
    4281             :          */
    4282        4136 :         if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
    4283        2068 :             opathkey->pk_eclass->ec_collation != ipathkey->pk_eclass->ec_collation)
    4284           0 :             elog(ERROR, "left and right pathkeys do not match in mergejoin");
    4285        4124 :         if (first_inner_match &&
    4286        4112 :             (opathkey->pk_strategy != ipathkey->pk_strategy ||
    4287        2056 :              opathkey->pk_nulls_first != ipathkey->pk_nulls_first))
    4288           0 :             elog(ERROR, "left and right pathkeys do not match in mergejoin");
    4289             : 
    4290             :         /* OK, save info for executor */
    4291        2068 :         mergefamilies[i] = opathkey->pk_opfamily;
    4292        2068 :         mergecollations[i] = opathkey->pk_eclass->ec_collation;
    4293        2068 :         mergestrategies[i] = opathkey->pk_strategy;
    4294        2068 :         mergenullsfirst[i] = opathkey->pk_nulls_first;
    4295        2068 :         i++;
    4296             :     }
    4297             : 
    4298             :     /*
    4299             :      * Note: it is not an error if we have additional pathkey elements (i.e.,
    4300             :      * lop or lip isn't NULL here).  The input paths might be better-sorted
    4301             :      * than we need for the current mergejoin.
    4302             :      */
    4303             : 
    4304             :     /*
    4305             :      * Now we can build the mergejoin node.
    4306             :      */
    4307        3852 :     join_plan = make_mergejoin(tlist,
    4308             :                                joinclauses,
    4309             :                                otherclauses,
    4310             :                                mergeclauses,
    4311             :                                mergefamilies,
    4312             :                                mergecollations,
    4313             :                                mergestrategies,
    4314             :                                mergenullsfirst,
    4315             :                                outer_plan,
    4316             :                                inner_plan,
    4317             :                                best_path->jpath.jointype,
    4318        1926 :                                best_path->jpath.inner_unique,
    4319        1926 :                                best_path->skip_mark_restore);
    4320             : 
    4321             :     /* Costs of sort and material steps are included in path cost already */
    4322        1926 :     copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
    4323             : 
    4324        1926 :     return join_plan;
    4325             : }
    4326             : 
    4327             : static HashJoin *
    4328       26406 : create_hashjoin_plan(PlannerInfo *root,
    4329             :                      HashPath *best_path)
    4330             : {
    4331             :     HashJoin   *join_plan;
    4332             :     Hash       *hash_plan;
    4333             :     Plan       *outer_plan;
    4334             :     Plan       *inner_plan;
    4335       26406 :     List       *tlist = build_path_tlist(root, &best_path->jpath.path);
    4336             :     List       *joinclauses;
    4337             :     List       *otherclauses;
    4338             :     List       *hashclauses;
    4339       26406 :     Oid         skewTable = InvalidOid;
    4340       26406 :     AttrNumber  skewColumn = InvalidAttrNumber;
    4341       26406 :     bool        skewInherit = false;
    4342             : 
    4343             :     /*
    4344             :      * HashJoin can project, so we don't have to demand exact tlists from the
    4345             :      * inputs.  However, it's best to request a small tlist from the inner
    4346             :      * side, so that we aren't storing more data than necessary.  Likewise, if
    4347             :      * we anticipate batching, request a small tlist from the outer side so
    4348             :      * that we don't put extra data in the outer batch files.
    4349             :      */
    4350       26406 :     outer_plan = create_plan_recurse(root, best_path->jpath.outerjoinpath,
    4351       26406 :                                      (best_path->num_batches > 1) ? CP_SMALL_TLIST : 0);
    4352             : 
    4353       26406 :     inner_plan = create_plan_recurse(root, best_path->jpath.innerjoinpath,
    4354             :                                      CP_SMALL_TLIST);
    4355             : 
    4356             :     /* Sort join qual clauses into best execution order */
    4357       26406 :     joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
    4358             :     /* There's no point in sorting the hash clauses ... */
    4359             : 
    4360             :     /* Get the join qual clauses (in plain expression form) */
    4361             :     /* Any pseudoconstant clauses are ignored here */
    4362       26406 :     if (IS_OUTER_JOIN(best_path->jpath.jointype))
    4363             :     {
    4364       14582 :         extract_actual_join_clauses(joinclauses,
    4365       14582 :                                     best_path->jpath.path.parent->relids,
    4366             :                                     &joinclauses, &otherclauses);
    4367             :     }
    4368             :     else
    4369             :     {
    4370             :         /* We can treat all clauses alike for an inner join */
    4371       11824 :         joinclauses = extract_actual_clauses(joinclauses, false);
    4372       11824 :         otherclauses = NIL;
    4373             :     }
    4374             : 
    4375             :     /*
    4376             :      * Remove the hashclauses from the list of join qual clauses, leaving the
    4377             :      * list of quals that must be checked as qpquals.
    4378             :      */
    4379       26406 :     hashclauses = get_actual_clauses(best_path->path_hashclauses);
    4380       26406 :     joinclauses = list_difference(joinclauses, hashclauses);
    4381             : 
    4382             :     /*
    4383             :      * Replace any outer-relation variables with nestloop params.  There
    4384             :      * should not be any in the hashclauses.
    4385             :      */
    4386       26406 :     if (best_path->jpath.path.param_info)
    4387             :     {
    4388         132 :         joinclauses = (List *)
    4389         132 :             replace_nestloop_params(root, (Node *) joinclauses);
    4390         132 :         otherclauses = (List *)
    4391         132 :             replace_nestloop_params(root, (Node *) otherclauses);
    4392             :     }
    4393             : 
    4394             :     /*
    4395             :      * Rearrange hashclauses, if needed, so that the outer variable is always
    4396             :      * on the left.
    4397             :      */
    4398       26406 :     hashclauses = get_switched_clauses(best_path->path_hashclauses,
    4399       26406 :                                        best_path->jpath.outerjoinpath->parent->relids);
    4400             : 
    4401             :     /*
    4402             :      * If there is a single join clause and we can identify the outer variable
    4403             :      * as a simple column reference, supply its identity for possible use in
    4404             :      * skew optimization.  (Note: in principle we could do skew optimization
    4405             :      * with multiple join clauses, but we'd have to be able to determine the
    4406             :      * most common combinations of outer values, which we don't currently have
    4407             :      * enough stats for.)
    4408             :      */
    4409       26406 :     if (list_length(hashclauses) == 1)
    4410             :     {
    4411       25338 :         OpExpr     *clause = (OpExpr *) linitial(hashclauses);
    4412             :         Node       *node;
    4413             : 
    4414             :         Assert(is_opclause(clause));
    4415       25338 :         node = (Node *) linitial(clause->args);
    4416       25338 :         if (IsA(node, RelabelType))
    4417         608 :             node = (Node *) ((RelabelType *) node)->arg;
    4418       25338 :         if (IsA(node, Var))
    4419             :         {
    4420       25176 :             Var        *var = (Var *) node;
    4421             :             RangeTblEntry *rte;
    4422             : 
    4423       25176 :             rte = root->simple_rte_array[var->varno];
    4424       25176 :             if (rte->rtekind == RTE_RELATION)
    4425             :             {
    4426       18804 :                 skewTable = rte->relid;
    4427       18804 :                 skewColumn = var->varattno;
    4428       18804 :                 skewInherit = rte->inh;
    4429             :             }
    4430             :         }
    4431             :     }
    4432             : 
    4433             :     /*
    4434             :      * Build the hash node and hash join node.
    4435             :      */
    4436       26406 :     hash_plan = make_hash(inner_plan,
    4437             :                           skewTable,
    4438             :                           skewColumn,
    4439             :                           skewInherit);
    4440             : 
    4441             :     /*
    4442             :      * Set Hash node's startup & total costs equal to total cost of input
    4443             :      * plan; this only affects EXPLAIN display not decisions.
    4444             :      */
    4445       26406 :     copy_plan_costsize(&hash_plan->plan, inner_plan);
    4446       26406 :     hash_plan->plan.startup_cost = hash_plan->plan.total_cost;
    4447             : 
    4448             :     /*
    4449             :      * If parallel-aware, the executor will also need an estimate of the total
    4450             :      * number of rows expected from all participants so that it can size the
    4451             :      * shared hash table.
    4452             :      */
    4453       26406 :     if (best_path->jpath.path.parallel_aware)
    4454             :     {
    4455          92 :         hash_plan->plan.parallel_aware = true;
    4456          92 :         hash_plan->rows_total = best_path->inner_rows_total;
    4457             :     }
    4458             : 
    4459       26406 :     join_plan = make_hashjoin(tlist,
    4460             :                               joinclauses,
    4461             :                               otherclauses,
    4462             :                               hashclauses,
    4463             :                               outer_plan,
    4464             :                               (Plan *) hash_plan,
    4465             :                               best_path->jpath.jointype,
    4466       26406 :                               best_path->jpath.inner_unique);
    4467             : 
    4468       26406 :     copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
    4469             : 
    4470       26406 :     return join_plan;
    4471             : }
    4472             : 
    4473             : 
    4474             : /*****************************************************************************
    4475             :  *
    4476             :  *  SUPPORTING ROUTINES
    4477             :  *
    4478             :  *****************************************************************************/
    4479             : 
    4480             : /*
    4481             :  * replace_nestloop_params
    4482             :  *    Replace outer-relation Vars and PlaceHolderVars in the given expression
    4483             :  *    with nestloop Params
    4484             :  *
    4485             :  * All Vars and PlaceHolderVars belonging to the relation(s) identified by
    4486             :  * root->curOuterRels are replaced by Params, and entries are added to
    4487             :  * root->curOuterParams if not already present.
    4488             :  */
    4489             : static Node *
    4490      168778 : replace_nestloop_params(PlannerInfo *root, Node *expr)
    4491             : {
    4492             :     /* No setup needed for tree walk, so away we go */
    4493      168778 :     return replace_nestloop_params_mutator(expr, root);
    4494             : }
    4495             : 
    4496             : static Node *
    4497      576992 : replace_nestloop_params_mutator(Node *node, PlannerInfo *root)
    4498             : {
    4499      576992 :     if (node == NULL)
    4500       31754 :         return NULL;
    4501      545238 :     if (IsA(node, Var))
    4502             :     {
    4503      173434 :         Var        *var = (Var *) node;
    4504             : 
    4505             :         /* Upper-level Vars should be long gone at this point */
    4506             :         Assert(var->varlevelsup == 0);
    4507             :         /* If not to be replaced, we can just return the Var unmodified */
    4508      173434 :         if (!bms_is_member(var->varno, root->curOuterRels))
    4509      138840 :             return node;
    4510             :         /* Replace the Var with a nestloop Param */
    4511       34594 :         return (Node *) replace_nestloop_param_var(root, var);
    4512             :     }
    4513      371804 :     if (IsA(node, PlaceHolderVar))
    4514             :     {
    4515         272 :         PlaceHolderVar *phv = (PlaceHolderVar *) node;
    4516             : 
    4517             :         /* Upper-level PlaceHolderVars should be long gone at this point */
    4518             :         Assert(phv->phlevelsup == 0);
    4519             : 
    4520             :         /*
    4521             :          * Check whether we need to replace the PHV.  We use bms_overlap as a
    4522             :          * cheap/quick test to see if the PHV might be evaluated in the outer
    4523             :          * rels, and then grab its PlaceHolderInfo to tell for sure.
    4524             :          */
    4525         412 :         if (!bms_overlap(phv->phrels, root->curOuterRels) ||
    4526         140 :             !bms_is_subset(find_placeholder_info(root, phv, false)->ph_eval_at,
    4527         140 :                            root->curOuterRels))
    4528             :         {
    4529             :             /*
    4530             :              * We can't replace the whole PHV, but we might still need to
    4531             :              * replace Vars or PHVs within its expression, in case it ends up
    4532             :              * actually getting evaluated here.  (It might get evaluated in
    4533             :              * this plan node, or some child node; in the latter case we don't
    4534             :              * really need to process the expression here, but we haven't got
    4535             :              * enough info to tell if that's the case.)  Flat-copy the PHV
    4536             :              * node and then recurse on its expression.
    4537             :              *
    4538             :              * Note that after doing this, we might have different
    4539             :              * representations of the contents of the same PHV in different
    4540             :              * parts of the plan tree.  This is OK because equal() will just
    4541             :              * match on phid/phlevelsup, so setrefs.c will still recognize an
    4542             :              * upper-level reference to a lower-level copy of the same PHV.
    4543             :              */
    4544         148 :             PlaceHolderVar *newphv = makeNode(PlaceHolderVar);
    4545             : 
    4546         148 :             memcpy(newphv, phv, sizeof(PlaceHolderVar));
    4547         148 :             newphv->phexpr = (Expr *)
    4548         148 :                 replace_nestloop_params_mutator((Node *) phv->phexpr,
    4549             :                                                 root);
    4550         148 :             return (Node *) newphv;
    4551             :         }
    4552             :         /* Replace the PlaceHolderVar with a nestloop Param */
    4553         124 :         return (Node *) replace_nestloop_param_placeholdervar(root, phv);
    4554             :     }
    4555      371532 :     return expression_tree_mutator(node,
    4556             :                                    replace_nestloop_params_mutator,
    4557             :                                    (void *) root);
    4558             : }
    4559             : 
    4560             : /*
    4561             :  * fix_indexqual_references
    4562             :  *    Adjust indexqual clauses to the form the executor's indexqual
    4563             :  *    machinery needs.
    4564             :  *
    4565             :  * We have three tasks here:
    4566             :  *  * Select the actual qual clauses out of the input IndexClause list,
    4567             :  *    and remove RestrictInfo nodes from the qual clauses.
    4568             :  *  * Replace any outer-relation Var or PHV nodes with nestloop Params.
    4569             :  *    (XXX eventually, that responsibility should go elsewhere?)
    4570             :  *  * Index keys must be represented by Var nodes with varattno set to the
    4571             :  *    index's attribute number, not the attribute number in the original rel.
    4572             :  *
    4573             :  * *stripped_indexquals_p receives a list of the actual qual clauses.
    4574             :  *
    4575             :  * *fixed_indexquals_p receives a list of the adjusted quals.  This is a copy
    4576             :  * that shares no substructure with the original; this is needed in case there
    4577             :  * are subplans in it (we need two separate copies of the subplan tree, or
    4578             :  * things will go awry).
    4579             :  */
    4580             : static void
    4581       94226 : fix_indexqual_references(PlannerInfo *root, IndexPath *index_path,
    4582             :                          List **stripped_indexquals_p, List **fixed_indexquals_p)
    4583             : {
    4584       94226 :     IndexOptInfo *index = index_path->indexinfo;
    4585             :     List       *stripped_indexquals;
    4586             :     List       *fixed_indexquals;
    4587             :     ListCell   *lc;
    4588             : 
    4589       94226 :     stripped_indexquals = fixed_indexquals = NIL;
    4590             : 
    4591      199350 :     foreach(lc, index_path->indexclauses)
    4592             :     {
    4593      105124 :         IndexClause *iclause = lfirst_node(IndexClause, lc);
    4594      105124 :         int         indexcol = iclause->indexcol;
    4595             :         ListCell   *lc2;
    4596             : 
    4597      210766 :         foreach(lc2, iclause->indexquals)
    4598             :         {
    4599      105642 :             RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc2);
    4600      105642 :             Node       *clause = (Node *) rinfo->clause;
    4601             : 
    4602      105642 :             stripped_indexquals = lappend(stripped_indexquals, clause);
    4603      105642 :             clause = fix_indexqual_clause(root, index, indexcol,
    4604             :                                           clause, iclause->indexcols);
    4605      105642 :             fixed_indexquals = lappend(fixed_indexquals, clause);
    4606             :         }
    4607             :     }
    4608             : 
    4609       94226 :     *stripped_indexquals_p = stripped_indexquals;
    4610       94226 :     *fixed_indexquals_p = fixed_indexquals;
    4611       94226 : }
    4612             : 
    4613             : /*
    4614             :  * fix_indexorderby_references
    4615             :  *    Adjust indexorderby clauses to the form the executor's index
    4616             :  *    machinery needs.
    4617             :  *
    4618             :  * This is a simplified version of fix_indexqual_references.  The input is
    4619             :  * bare clauses and a separate indexcol list, instead of IndexClauses.
    4620             :  */
    4621             : static List *
    4622       94226 : fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path)
    4623             : {
    4624       94226 :     IndexOptInfo *index = index_path->indexinfo;
    4625             :     List       *fixed_indexorderbys;
    4626             :     ListCell   *lcc,
    4627             :                *lci;
    4628             : 
    4629       94226 :     fixed_indexorderbys = NIL;
    4630             : 
    4631       94436 :     forboth(lcc, index_path->indexorderbys, lci, index_path->indexorderbycols)
    4632             :     {
    4633         210 :         Node       *clause = (Node *) lfirst(lcc);
    4634         210 :         int         indexcol = lfirst_int(lci);
    4635             : 
    4636         210 :         clause = fix_indexqual_clause(root, index, indexcol, clause, NIL);
    4637         210 :         fixed_indexorderbys = lappend(fixed_indexorderbys, clause);
    4638             :     }
    4639             : 
    4640       94226 :     return fixed_indexorderbys;
    4641             : }
    4642             : 
    4643             : /*
    4644             :  * fix_indexqual_clause
    4645             :  *    Convert a single indexqual clause to the form needed by the executor.
    4646             :  *
    4647             :  * We replace nestloop params here, and replace the index key variables
    4648             :  * or expressions by index Var nodes.
    4649             :  */
    4650             : static Node *
    4651      105852 : fix_indexqual_clause(PlannerInfo *root, IndexOptInfo *index, int indexcol,
    4652             :                      Node *clause, List *indexcolnos)
    4653             : {
    4654             :     /*
    4655             :      * Replace any outer-relation variables with nestloop params.
    4656             :      *
    4657             :      * This also makes a copy of the clause, so it's safe to modify it
    4658             :      * in-place below.
    4659             :      */
    4660      105852 :     clause = replace_nestloop_params(root, clause);
    4661             : 
    4662      105852 :     if (IsA(clause, OpExpr))
    4663             :     {
    4664      104848 :         OpExpr     *op = (OpExpr *) clause;
    4665             : 
    4666             :         /* Replace the indexkey expression with an index Var. */
    4667      104848 :         linitial(op->args) = fix_indexqual_operand(linitial(op->args),
    4668             :                                                    index,
    4669             :                                                    indexcol);
    4670             :     }
    4671        1004 :     else if (IsA(clause, RowCompareExpr))
    4672             :     {
    4673          48 :         RowCompareExpr *rc = (RowCompareExpr *) clause;
    4674             :         ListCell   *lca,
    4675             :                    *lcai;
    4676             : 
    4677             :         /* Replace the indexkey expressions with index Vars. */
    4678             :         Assert(list_length(rc->largs) == list_length(indexcolnos));
    4679         144 :         forboth(lca, rc->largs, lcai, indexcolnos)
    4680             :         {
    4681          96 :             lfirst(lca) = fix_indexqual_operand(lfirst(lca),
    4682             :                                                 index,
    4683             :                                                 lfirst_int(lcai));
    4684             :         }
    4685             :     }
    4686         956 :     else if (IsA(clause, ScalarArrayOpExpr))
    4687             :     {
    4688         234 :         ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
    4689             : 
    4690             :         /* Replace the indexkey expression with an index Var. */
    4691         234 :         linitial(saop->args) = fix_indexqual_operand(linitial(saop->args),
    4692             :                                                      index,
    4693             :                                                      indexcol);
    4694             :     }
    4695         722 :     else if (IsA(clause, NullTest))
    4696             :     {
    4697         722 :         NullTest   *nt = (NullTest *) clause;
    4698             : 
    4699             :         /* Replace the indexkey expression with an index Var. */
    4700         722 :         nt->arg = (Expr *) fix_indexqual_operand((Node *) nt->arg,
    4701             :                                                  index,
    4702             :                                                  indexcol);
    4703             :     }
    4704             :     else
    4705           0 :         elog(ERROR, "unsupported indexqual type: %d",
    4706             :              (int) nodeTag(clause));
    4707             : 
    4708      105852 :     return clause;
    4709             : }
    4710             : 
    4711             : /*
    4712             :  * fix_indexqual_operand
    4713             :  *    Convert an indexqual expression to a Var referencing the index column.
    4714             :  *
    4715             :  * We represent index keys by Var nodes having varno == INDEX_VAR and varattno
    4716             :  * equal to the index's attribute number (index column position).
    4717             :  *
    4718             :  * Most of the code here is just for sanity cross-checking that the given
    4719             :  * expression actually matches the index column it's claimed to.
    4720             :  */
    4721             : static Node *
    4722      105900 : fix_indexqual_operand(Node *node, IndexOptInfo *index, int indexcol)
    4723             : {
    4724             :     Var        *result;
    4725             :     int         pos;
    4726             :     ListCell   *indexpr_item;
    4727             : 
    4728             :     /*
    4729             :      * Remove any binary-compatible relabeling of the indexkey
    4730             :      */
    4731      105900 :     if (IsA(node, RelabelType))
    4732         708 :         node = (Node *) ((RelabelType *) node)->arg;
    4733             : 
    4734             :     Assert(indexcol >= 0 && indexcol < index->ncolumns);
    4735             : 
    4736      105900 :     if (index->indexkeys[indexcol] != 0)
    4737             :     {
    4738             :         /* It's a simple index column */
    4739      211468 :         if (IsA(node, Var) &&
    4740      211468 :             ((Var *) node)->varno == index->rel->relid &&
    4741      105734 :             ((Var *) node)->varattno == index->indexkeys[indexcol])
    4742             :         {
    4743      105734 :             result = (Var *) copyObject(node);
    4744      105734 :             result->varno = INDEX_VAR;
    4745      105734 :             result->varattno = indexcol + 1;
    4746      105734 :             return (Node *) result;
    4747             :         }
    4748             :         else
    4749           0 :             elog(ERROR, "index key does not match expected index column");
    4750             :     }
    4751             : 
    4752             :     /* It's an index expression, so find and cross-check the expression */
    4753         166 :     indexpr_item = list_head(index->indexprs);
    4754         166 :     for (pos = 0; pos < index->ncolumns; pos++)
    4755             :     {
    4756         166 :         if (index->indexkeys[pos] == 0)
    4757             :         {
    4758         166 :             if (indexpr_item == NULL)
    4759           0 :                 elog(ERROR, "too few entries in indexprs list");
    4760         166 :             if (pos == indexcol)
    4761             :             {
    4762             :                 Node       *indexkey;
    4763             : 
    4764         166 :                 indexkey = (Node *) lfirst(indexpr_item);
    4765         166 :                 if (indexkey && IsA(indexkey, RelabelType))
    4766          10 :                     indexkey = (Node *) ((RelabelType *) indexkey)->arg;
    4767         166 :                 if (equal(node, indexkey))
    4768             :                 {
    4769         332 :                     result = makeVar(INDEX_VAR, indexcol + 1,
    4770         166 :                                      exprType(lfirst(indexpr_item)), -1,
    4771         166 :                                      exprCollation(lfirst(indexpr_item)),
    4772             :                                      0);
    4773         166 :                     return (Node *) result;
    4774             :                 }
    4775             :                 else
    4776           0 :                     elog(ERROR, "index key does not match expected index column");
    4777             :             }
    4778           0 :             indexpr_item = lnext(indexpr_item);
    4779             :         }
    4780             :     }
    4781             : 
    4782             :     /* Oops... */
    4783           0 :     elog(ERROR, "index key does not match expected index column");
    4784             :     return NULL;                /* keep compiler quiet */
    4785             : }
    4786             : 
    4787             : /*
    4788             :  * get_switched_clauses
    4789             :  *    Given a list of merge or hash joinclauses (as RestrictInfo nodes),
    4790             :  *    extract the bare clauses, and rearrange the elements within the
    4791             :  *    clauses, if needed, so the outer join variable is on the left and
    4792             :  *    the inner is on the right.  The original clause data structure is not
    4793             :  *    touched; a modified list is returned.  We do, however, set the transient
    4794             :  *    outer_is_left field in each RestrictInfo to show which side was which.
    4795             :  */
    4796             : static List *
    4797       28332 : get_switched_clauses(List *clauses, Relids outerrelids)
    4798             : {
    4799       28332 :     List       *t_list = NIL;
    4800             :     ListCell   *l;
    4801             : 
    4802       58086 :     foreach(l, clauses)
    4803             :     {
    4804       29754 :         RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
    4805       29754 :         OpExpr     *clause = (OpExpr *) restrictinfo->clause;
    4806             : 
    4807             :         Assert(is_opclause(clause));
    4808       29754 :         if (bms_is_subset(restrictinfo->right_relids, outerrelids))
    4809             :         {
    4810             :             /*
    4811             :              * Duplicate just enough of the structure to allow commuting the
    4812             :              * clause without changing the original list.  Could use
    4813             :              * copyObject, but a complete deep copy is overkill.
    4814             :              */
    4815       11588 :             OpExpr     *temp = makeNode(OpExpr);
    4816             : 
    4817       11588 :             temp->opno = clause->opno;
    4818       11588 :             temp->opfuncid = InvalidOid;
    4819       11588 :             temp->opresulttype = clause->opresulttype;
    4820       11588 :             temp->opretset = clause->opretset;
    4821       11588 :             temp->opcollid = clause->opcollid;
    4822       11588 :             temp->inputcollid = clause->inputcollid;
    4823       11588 :             temp->args = list_copy(clause->args);
    4824       11588 :             temp->location = clause->location;
    4825             :             /* Commute it --- note this modifies the temp node in-place. */
    4826       11588 :             CommuteOpExpr(temp);
    4827       11588 :             t_list = lappend(t_list, temp);
    4828       11588 :             restrictinfo->outer_is_left = false;
    4829             :         }
    4830             :         else
    4831             :         {
    4832             :             Assert(bms_is_subset(restrictinfo->left_relids, outerrelids));
    4833       18166 :             t_list = lappend(t_list, clause);
    4834       18166 :             restrictinfo->outer_is_left = true;
    4835             :         }
    4836             :     }
    4837       28332 :     return t_list;
    4838             : }
    4839             : 
    4840             : /*
    4841             :  * order_qual_clauses
    4842             :  *      Given a list of qual clauses that will all be evaluated at the same
    4843             :  *      plan node, sort the list into the order we want to check the quals
    4844             :  *      in at runtime.
    4845             :  *
    4846             :  * When security barrier quals are used in the query, we may have quals with
    4847             :  * different security levels in the list.  Quals of lower security_level
    4848             :  * must go before quals of higher security_level, except that we can grant
    4849             :  * exceptions to move up quals that are leakproof.  When security level
    4850             :  * doesn't force the decision, we prefer to order clauses by estimated
    4851             :  * execution cost, cheapest first.
    4852             :  *
    4853             :  * Ideally the order should be driven by a combination of execution cost and
    4854             :  * selectivity, but it's not immediately clear how to account for both,
    4855             :  * and given the uncertainty of the estimates the reliability of the decisions
    4856             :  * would be doubtful anyway.  So we just order by security level then
    4857             :  * estimated per-tuple cost, being careful not to change the order when
    4858             :  * (as is often the case) the estimates are identical.
    4859             :  *
    4860             :  * Although this will work on either bare clauses or RestrictInfos, it's
    4861             :  * much faster to apply it to RestrictInfos, since it can re-use cost
    4862             :  * information that is cached in RestrictInfos.  XXX in the bare-clause
    4863             :  * case, we are also not able to apply security considerations.  That is
    4864             :  * all right for the moment, because the bare-clause case doesn't occur
    4865             :  * anywhere that barrier quals could be present, but it would be better to
    4866             :  * get rid of it.
    4867             :  *
    4868             :  * Note: some callers pass lists that contain entries that will later be
    4869             :  * removed; this is the easiest way to let this routine see RestrictInfos
    4870             :  * instead of bare clauses.  This is another reason why trying to consider
    4871             :  * selectivity in the ordering would likely do the wrong thing.
    4872             :  */
    4873             : static List *
    4874      462452 : order_qual_clauses(PlannerInfo *root, List *clauses)
    4875             : {
    4876             :     typedef struct
    4877             :     {
    4878             :         Node       *clause;
    4879             :         Cost        cost;
    4880             :         Index       security_level;
    4881             :     } QualItem;
    4882      462452 :     int         nitems = list_length(clauses);
    4883             :     QualItem   *items;
    4884             :     ListCell   *lc;
    4885             :     int         i;
    4886             :     List       *result;
    4887             : 
    4888             :     /* No need to work hard for 0 or 1 clause */
    4889      462452 :     if (nitems <= 1)
    4890      438326 :         return clauses;
    4891             : 
    4892             :     /*
    4893             :      * Collect the items and costs into an array.  This is to avoid repeated
    4894             :      * cost_qual_eval work if the inputs aren't RestrictInfos.
    4895             :      */
    4896       24126 :     items = (QualItem *) palloc(nitems * sizeof(QualItem));
    4897       24126 :     i = 0;
    4898       78606 :     foreach(lc, clauses)
    4899             :     {
    4900       54480 :         Node       *clause = (Node *) lfirst(lc);
    4901             :         QualCost    qcost;
    4902             : 
    4903       54480 :         cost_qual_eval_node(&qcost, clause, root);
    4904       54480 :         items[i].clause = clause;
    4905       54480 :         items[i].cost = qcost.per_tuple;
    4906       54480 :         if (IsA(clause, RestrictInfo))
    4907             :         {
    4908       54412 :             RestrictInfo *rinfo = (RestrictInfo *) clause;
    4909             : 
    4910             :             /*
    4911             :              * If a clause is leakproof, it doesn't have to be constrained by
    4912             :              * its nominal security level.  If it's also reasonably cheap
    4913             :              * (here defined as 10X cpu_operator_cost), pretend it has
    4914             :              * security_level 0, which will allow it to go in front of
    4915             :              * more-expensive quals of lower security levels.  Of course, that
    4916             :              * will also force it to go in front of cheaper quals of its own
    4917             :              * security level, which is not so great, but we can alleviate
    4918             :              * that risk by applying the cost limit cutoff.
    4919             :              */
    4920       54412 :             if (rinfo->leakproof && items[i].cost < 10 * cpu_operator_cost)
    4921         616 :                 items[i].security_level = 0;
    4922             :             else
    4923       53796 :                 items[i].security_level = rinfo->security_level;
    4924             :         }
    4925             :         else
    4926          68 :             items[i].security_level = 0;
    4927       54480 :         i++;
    4928             :     }
    4929             : 
    4930             :     /*
    4931             :      * Sort.  We don't use qsort() because it's not guaranteed stable for
    4932             :      * equal keys.  The expected number of entries is small enough that a
    4933             :      * simple insertion sort should be good enough.
    4934             :      */
    4935       54480 :     for (i = 1; i < nitems; i++)
    4936             :     {
    4937       30354 :         QualItem    newitem = items[i];
    4938             :         int         j;
    4939             : 
    4940             :         /* insert newitem into the already-sorted subarray */
    4941       35976 :         for (j = i; j > 0; j--)
    4942             :         {
    4943       31676 :             QualItem   *olditem = &items[j - 1];
    4944             : 
    4945       63012 :             if (newitem.security_level > olditem->security_level ||
    4946       61640 :                 (newitem.security_level == olditem->security_level &&
    4947       30304 :                  newitem.cost >= olditem->cost))
    4948             :                 break;
    4949        5622 :             items[j] = *olditem;
    4950             :         }
    4951       30354 :         items[j] = newitem;
    4952             :     }
    4953             : 
    4954             :     /* Convert back to a list */
    4955       24126 :     result = NIL;
    4956       78606 :     for (i = 0; i < nitems; i++)
    4957       54480 :         result = lappend(result, items[i].clause);
    4958             : 
    4959       24126 :     return result;
    4960             : }
    4961             : 
    4962             : /*
    4963             :  * Copy cost and size info from a Path node to the Plan node created from it.
    4964             :  * The executor usually won't use this info, but it's needed by EXPLAIN.
    4965             :  * Also copy the parallel-related flags, which the executor *will* use.
    4966             :  */
    4967             : static void
    4968      581554 : copy_generic_path_info(Plan *dest, Path *src)
    4969             : {
    4970      581554 :     dest->startup_cost = src->startup_cost;
    4971      581554 :     dest->total_cost = src->total_cost;
    4972      581554 :     dest->plan_rows = src->rows;
    4973      581554 :     dest->plan_width = src->pathtarget->width;
    4974      581554 :     dest->parallel_aware = src->parallel_aware;
    4975      581554 :     dest->parallel_safe = src->parallel_safe;
    4976      581554 : }
    4977             : 
    4978             : /*
    4979             :  * Copy cost and size info from a lower plan node to an inserted node.
    4980             :  * (Most callers alter the info after copying it.)
    4981             :  */
    4982             : static void
    4983       28884 : copy_plan_costsize(Plan *dest, Plan *src)
    4984             : {
    4985       28884 :     dest->startup_cost = src->startup_cost;
    4986       28884 :     dest->total_cost = src->total_cost;
    4987       28884 :     dest->plan_rows = src->plan_rows;
    4988       28884 :     dest->plan_width = src->plan_width;
    4989             :     /* Assume the inserted node is not parallel-aware. */
    4990       28884 :     dest->parallel_aware = false;
    4991             :     /* Assume the inserted node is parallel-safe, if child plan is. */
    4992       28884 :     dest->parallel_safe = src->parallel_safe;
    4993       28884 : }
    4994             : 
    4995             : /*
    4996             :  * Some places in this file build Sort nodes that don't have a directly
    4997             :  * corresponding Path node.  The cost of the sort is, or should have been,
    4998             :  * included in the cost of the Path node we're working from, but since it's
    4999             :  * not split out, we have to re-figure it using cost_sort().  This is just
    5000             :  * to label the Sort node nicely for EXPLAIN.
    5001             :  *
    5002             :  * limit_tuples is as for cost_sort (in particular, pass -1 if no limit)
    5003             :  */
    5004             : static void
    5005        2820 : label_sort_with_costsize(PlannerInfo *root, Sort *plan, double limit_tuples)
    5006             : {
    5007        2820 :     Plan       *lefttree = plan->plan.lefttree;
    5008             :     Path        sort_path;      /* dummy for result of cost_sort */
    5009             : 
    5010        2820 :     cost_sort(&sort_path, root, NIL,
    5011             :               lefttree->total_cost,
    5012             :               lefttree->plan_rows,
    5013             :               lefttree->plan_width,
    5014             :               0.0,
    5015             :               work_mem,
    5016             :               limit_tuples);
    5017        2820 :     plan->plan.startup_cost = sort_path.startup_cost;
    5018        2820 :     plan->plan.total_cost = sort_path.total_cost;
    5019        2820 :     plan->plan.plan_rows = lefttree->plan_rows;
    5020        2820 :     plan->plan.plan_width = lefttree->plan_width;
    5021        2820 :     plan->plan.parallel_aware = false;
    5022        2820 :     plan->plan.parallel_safe = lefttree->parallel_safe;
    5023        2820 : }
    5024             : 
    5025             : /*
    5026             :  * bitmap_subplan_mark_shared
    5027             :  *   Set isshared flag in bitmap subplan so that it will be created in
    5028             :  *   shared memory.
    5029             :  */
    5030             : static void
    5031          12 : bitmap_subplan_mark_shared(Plan *plan)
    5032             : {
    5033          12 :     if (IsA(plan, BitmapAnd))
    5034           0 :         bitmap_subplan_mark_shared(
    5035           0 :                                    linitial(((BitmapAnd *) plan)->bitmapplans));
    5036          12 :     else if (IsA(plan, BitmapOr))
    5037             :     {
    5038           0 :         ((BitmapOr *) plan)->isshared = true;
    5039           0 :         bitmap_subplan_mark_shared(
    5040           0 :                                    linitial(((BitmapOr *) plan)->bitmapplans));
    5041             :     }
    5042          12 :     else if (IsA(plan, BitmapIndexScan))
    5043          12 :         ((BitmapIndexScan *) plan)->isshared = true;
    5044             :     else
    5045           0 :         elog(ERROR, "unrecognized node type: %d", nodeTag(plan));
    5046          12 : }
    5047             : 
    5048             : /*****************************************************************************
    5049             :  *
    5050             :  *  PLAN NODE BUILDING ROUTINES
    5051             :  *
    5052             :  * In general, these functions are not passed the original Path and therefore
    5053             :  * leave it to the caller to fill in the cost/width fields from the Path,
    5054             :  * typically by calling copy_generic_path_info().  This convention is
    5055             :  * somewhat historical, but it does support a few places above where we build
    5056             :  * a plan node without having an exactly corresponding Path node.  Under no
    5057             :  * circumstances should one of these functions do its own cost calculations,
    5058             :  * as that would be redundant with calculations done while building Paths.
    5059             :  *
    5060             :  *****************************************************************************/
    5061             : 
    5062             : static SeqScan *
    5063      112344 : make_seqscan(List *qptlist,
    5064             :              List *qpqual,
    5065             :              Index scanrelid)
    5066             : {
    5067      112344 :     SeqScan    *node = makeNode(SeqScan);
    5068      112344 :     Plan       *plan = &node->plan;
    5069             : 
    5070      112344 :     plan->targetlist = qptlist;
    5071      112344 :     plan->qual = qpqual;
    5072      112344 :     plan->lefttree = NULL;
    5073      112344 :     plan->righttree = NULL;
    5074      112344 :     node->scanrelid = scanrelid;
    5075             : 
    5076      112344 :     return node;
    5077             : }
    5078             : 
    5079             : static SampleScan *
    5080         180 : make_samplescan(List *qptlist,
    5081             :                 List *qpqual,
    5082             :                 Index scanrelid,
    5083             :                 TableSampleClause *tsc)
    5084             : {
    5085         180 :     SampleScan *node = makeNode(SampleScan);
    5086         180 :     Plan       *plan = &node->scan.plan;
    5087             : 
    5088         180 :     plan->targetlist = qptlist;
    5089         180 :     plan->qual = qpqual;
    5090         180 :     plan->lefttree = NULL;
    5091         180 :     plan->righttree = NULL;
    5092         180 :     node->scan.scanrelid = scanrelid;
    5093         180 :     node->tablesample = tsc;
    5094             : 
    5095         180 :     return node;
    5096             : }
    5097             : 
    5098             : static IndexScan *
    5099       87906 : make_indexscan(List *qptlist,
    5100             :                List *qpqual,
    5101             :                Index scanrelid,
    5102             :                Oid indexid,
    5103             :                List *indexqual,
    5104             :                List *indexqualorig,
    5105             :                List *indexorderby,
    5106             :                List *indexorderbyorig,
    5107             :                List *indexorderbyops,
    5108             :                ScanDirection indexscandir)
    5109             : {
    5110       87906 :     IndexScan  *node = makeNode(IndexScan);
    5111       87906 :     Plan       *plan = &node->scan.plan;
    5112             : 
    5113       87906 :     plan->targetlist = qptlist;
    5114       87906 :     plan->qual = qpqual;
    5115       87906 :     plan->lefttree = NULL;
    5116       87906 :     plan->righttree = NULL;
    5117       87906 :     node->scan.scanrelid = scanrelid;
    5118       87906 :     node->indexid = indexid;
    5119       87906 :     node->indexqual = indexqual;
    5120       87906 :     node->indexqualorig = indexqualorig;
    5121       87906 :     node->indexorderby = indexorderby;
    5122       87906 :     node->indexorderbyorig = indexorderbyorig;
    5123       87906 :     node->indexorderbyops = indexorderbyops;
    5124       87906 :     node->indexorderdir = indexscandir;
    5125             : 
    5126       87906 :     return node;
    5127             : }
    5128             : 
    5129             : static IndexOnlyScan *
    5130        6320 : make_indexonlyscan(List *qptlist,
    5131             :                    List *qpqual,
    5132             :                    Index scanrelid,
    5133             :                    Oid indexid,
    5134             :                    List *indexqual,
    5135             :                    List *indexorderby,
    5136             :                    List *indextlist,
    5137             :                    ScanDirection indexscandir)
    5138             : {
    5139        6320 :     IndexOnlyScan *node = makeNode(IndexOnlyScan);
    5140        6320 :     Plan       *plan = &node->scan.plan;
    5141             : 
    5142        6320 :     plan->targetlist = qptlist;
    5143        6320 :     plan->qual = qpqual;
    5144        6320 :     plan->lefttree = NULL;
    5145        6320 :     plan->righttree = NULL;
    5146        6320 :     node->scan.scanrelid = scanrelid;
    5147        6320 :     node->indexid = indexid;
    5148        6320 :     node->indexqual = indexqual;
    5149        6320 :     node->indexorderby = indexorderby;
    5150        6320 :     node->indextlist = indextlist;
    5151        6320 :     node->indexorderdir = indexscandir;
    5152             : 
    5153        6320 :     return node;
    5154             : }
    5155             : 
    5156             : static BitmapIndexScan *
    5157       19070 : make_bitmap_indexscan(Index scanrelid,
    5158             :                       Oid indexid,
    5159             :                       List *indexqual,
    5160             :                       List *indexqualorig)
    5161             : {
    5162       19070 :     BitmapIndexScan *node = makeNode(BitmapIndexScan);
    5163       19070 :     Plan       *plan = &node->scan.plan;
    5164             : 
    5165       19070 :     plan->targetlist = NIL;      /* not used */
    5166       19070 :     plan->qual = NIL;            /* not used */
    5167       19070 :     plan->lefttree = NULL;
    5168       19070 :     plan->righttree = NULL;
    5169       19070 :     node->scan.scanrelid = scanrelid;
    5170       19070 :     node->indexid = indexid;
    5171       19070 :     node->indexqual = indexqual;
    5172       19070 :     node->indexqualorig = indexqualorig;
    5173             : 
    5174       19070 :     return node;
    5175             : }
    5176             : 
    5177             : static BitmapHeapScan *
    5178       18902 : make_bitmap_heapscan(List *qptlist,
    5179             :                      List *qpqual,
    5180             :                      Plan *lefttree,
    5181             :                      List *bitmapqualorig,
    5182             :                      Index scanrelid)
    5183             : {
    5184       18902 :     BitmapHeapScan *node = makeNode(BitmapHeapScan);
    5185       18902 :     Plan       *plan = &node->scan.plan;
    5186             : 
    5187       18902 :     plan->targetlist = qptlist;
    5188       18902 :     plan->qual = qpqual;
    5189       18902 :     plan->lefttree = lefttree;
    5190       18902 :     plan->righttree = NULL;
    5191       18902 :     node->scan.scanrelid = scanrelid;
    5192       18902 :     node->bitmapqualorig = bitmapqualorig;
    5193             : 
    5194       18902 :     return node;
    5195             : }
    5196             : 
    5197             : static TidScan *
    5198         404 : make_tidscan(List *qptlist,
    5199             :              List *qpqual,
    5200             :              Index scanrelid,
    5201             :              List *tidquals)
    5202             : {
    5203         404 :     TidScan    *node = makeNode(TidScan);
    5204         404 :     Plan       *plan = &node->scan.plan;
    5205             : 
    5206         404 :     plan->targetlist = qptlist;
    5207         404 :     plan->qual = qpqual;
    5208         404 :     plan->lefttree = NULL;
    5209         404 :     plan->righttree = NULL;
    5210         404 :     node->scan.scanrelid = scanrelid;
    5211         404 :     node->tidquals = tidquals;
    5212             : 
    5213         404 :     return node;
    5214             : }
    5215             : 
    5216             : static SubqueryScan *
    5217        8088 : make_subqueryscan(List *qptlist,
    5218             :                   List *qpqual,
    5219             :                   Index scanrelid,
    5220             :                   Plan *subplan)
    5221             : {
    5222        8088 :     SubqueryScan *node = makeNode(SubqueryScan);
    5223        8088 :     Plan       *plan = &node->scan.plan;
    5224             : 
    5225        8088 :     plan->targetlist = qptlist;
    5226        8088 :     plan->qual = qpqual;
    5227        8088 :     plan->lefttree = NULL;
    5228        8088 :     plan->righttree = NULL;
    5229        8088 :     node->scan.scanrelid = scanrelid;
    5230        8088 :     node->subplan = subplan;
    5231             : 
    5232        8088 :     return node;
    5233             : }
    5234             : 
    5235             : static FunctionScan *
    5236       28204 : make_functionscan(List *qptlist,
    5237             :                   List *qpqual,
    5238             :                   Index scanrelid,
    5239             :                   List *functions,
    5240             :                   bool funcordinality)
    5241             : {
    5242       28204 :     FunctionScan *node = makeNode(FunctionScan);
    5243       28204 :     Plan       *plan = &node->scan.plan;
    5244             : 
    5245       28204 :     plan->targetlist = qptlist;
    5246       28204 :     plan->qual = qpqual;
    5247       28204 :     plan->lefttree = NULL;
    5248       28204 :     plan->righttree = NULL;
    5249       28204 :     node->scan.scanrelid = scanrelid;
    5250       28204 :     node->functions = functions;
    5251       28204 :     node->funcordinality = funcordinality;
    5252             : 
    5253       28204 :     return node;
    5254             : }
    5255             : 
    5256             : static TableFuncScan *
    5257         140 : make_tablefuncscan(List *qptlist,
    5258             :                    List *qpqual,
    5259             :                    Index scanrelid,
    5260             :                    TableFunc *tablefunc)
    5261             : {
    5262         140 :     TableFuncScan *node = makeNode(TableFuncScan);
    5263         140 :     Plan       *plan = &node->scan.plan;
    5264             : 
    5265         140 :     plan->targetlist = qptlist;
    5266         140 :     plan->qual = qpqual;
    5267         140 :     plan->lefttree = NULL;
    5268         140 :     plan->righttree = NULL;
    5269         140 :     node->scan.scanrelid = scanrelid;
    5270         140 :     node->tablefunc = tablefunc;
    5271             : 
    5272         140 :     return node;
    5273             : }
    5274             : 
    5275             : static ValuesScan *
    5276        3730 : make_valuesscan(List *qptlist,
    5277             :                 List *qpqual,
    5278             :                 Index scanrelid,
    5279             :                 List *values_lists)
    5280             : {
    5281        3730 :     ValuesScan *node = makeNode(ValuesScan);
    5282        3730 :     Plan       *plan = &node->scan.plan;
    5283             : 
    5284        3730 :     plan->targetlist = qptlist;
    5285        3730 :     plan->qual = qpqual;
    5286        3730 :     plan->lefttree = NULL;
    5287        3730 :     plan->righttree = NULL;
    5288        3730 :     node->scan.scanrelid = scanrelid;
    5289        3730 :     node->values_lists = values_lists;
    5290             : 
    5291        3730 :     return node;
    5292             : }
    5293             : 
    5294             : static CteScan *
    5295         868 : make_ctescan(List *qptlist,
    5296             :              List *qpqual,
    5297             :              Index scanrelid,
    5298             :              int ctePlanId,
    5299             :              int cteParam)
    5300             : {
    5301         868 :     CteScan    *node = makeNode(CteScan);
    5302         868 :     Plan       *plan = &node->scan.plan;
    5303             : 
    5304         868 :     plan->targetlist = qptlist;
    5305         868 :     plan->qual = qpqual;
    5306         868 :     plan->lefttree = NULL;
    5307         868 :     plan->righttree = NULL;
    5308         868 :     node->scan.scanrelid = scanrelid;
    5309         868 :     node->ctePlanId = ctePlanId;
    5310         868 :     node->cteParam = cteParam;
    5311             : 
    5312         868 :     return node;
    5313             : }
    5314             : 
    5315             : static NamedTuplestoreScan *
    5316         260 : make_namedtuplestorescan(List *qptlist,
    5317             :                          List *qpqual,
    5318             :                          Index scanrelid,
    5319             :                          char *enrname)
    5320             : {
    5321         260 :     NamedTuplestoreScan *node = makeNode(NamedTuplestoreScan);
    5322         260 :     Plan       *plan = &node->scan.plan;
    5323             : 
    5324             :     /* cost should be inserted by caller */
    5325         260 :     plan->targetlist = qptlist;
    5326         260 :     plan->qual = qpqual;
    5327         260 :     plan->lefttree = NULL;
    5328         260 :     plan->righttree = NULL;
    5329         260 :     node->scan.scanrelid = scanrelid;
    5330         260 :     node->enrname = enrname;
    5331             : 
    5332         260 :     return node;
    5333             : }
    5334             : 
    5335             : static WorkTableScan *
    5336         326 : make_worktablescan(List *qptlist,
    5337             :                    List *qpqual,
    5338             :                    Index scanrelid,
    5339             :                    int wtParam)
    5340             : {
    5341         326 :     WorkTableScan *node = makeNode(WorkTableScan);
    5342         326 :     Plan       *plan = &node->scan.plan;
    5343             : 
    5344         326 :     plan->targetlist = qptlist;
    5345         326 :     plan->qual = qpqual;
    5346         326 :     plan->lefttree = NULL;
    5347         326 :     plan->righttree = NULL;
    5348         326 :     node->scan.scanrelid = scanrelid;
    5349         326 :     node->wtParam = wtParam;
    5350             : 
    5351         326 :     return node;
    5352             : }
    5353             : 
    5354             : ForeignScan *
    5355        1384 : make_foreignscan(List *qptlist,
    5356             :                  List *qpqual,
    5357             :                  Index scanrelid,
    5358             :                  List *fdw_exprs,
    5359             :                  List *fdw_private,
    5360             :                  List *fdw_scan_tlist,
    5361             :                  List *fdw_recheck_quals,
    5362             :                  Plan *outer_plan)
    5363             : {
    5364        1384 :     ForeignScan *node = makeNode(ForeignScan);
    5365        1384 :     Plan       *plan = &node->scan.plan;
    5366             : 
    5367             :     /* cost will be filled in by create_foreignscan_plan */
    5368        1384 :     plan->targetlist = qptlist;
    5369        1384 :     plan->qual = qpqual;
    5370        1384 :     plan->lefttree = outer_plan;
    5371        1384 :     plan->righttree = NULL;
    5372        1384 :     node->scan.scanrelid = scanrelid;
    5373        1384 :     node->operation = CMD_SELECT;
    5374             :     /* fs_server will be filled in by create_foreignscan_plan */
    5375        1384 :     node->fs_server = InvalidOid;
    5376        1384 :     node->fdw_exprs = fdw_exprs;
    5377        1384 :     node->fdw_private = fdw_private;
    5378        1384 :     node->fdw_scan_tlist = fdw_scan_tlist;
    5379        1384 :     node->fdw_recheck_quals = fdw_recheck_quals;
    5380             :     /* fs_relids will be filled in by create_foreignscan_plan */
    5381        1384 :     node->fs_relids = NULL;
    5382             :     /* fsSystemCol will be filled in by create_foreignscan_plan */
    5383        1384 :     node->fsSystemCol = false;
    5384             : 
    5385        1384 :     return node;
    5386             : }
    5387             : 
    5388             : static RecursiveUnion *
    5389         326 : make_recursive_union(List *tlist,
    5390             :                      Plan *lefttree,
    5391             :                      Plan *righttree,
    5392             :                      int wtParam,
    5393             :                      List *distinctList,
    5394             :                      long numGroups)
    5395             : {
    5396         326 :     RecursiveUnion *node = makeNode(RecursiveUnion);
    5397         326 :     Plan       *plan = &node->plan;
    5398         326 :     int         numCols = list_length(distinctList);
    5399             : 
    5400         326 :     plan->targetlist = tlist;
    5401         326 :     plan->qual = NIL;
    5402         326 :     plan->lefttree = lefttree;
    5403         326 :     plan->righttree = righttree;
    5404         326 :     node->wtParam = wtParam;
    5405             : 
    5406             :     /*
    5407             :      * convert SortGroupClause list into arrays of attr indexes and equality
    5408             :      * operators, as wanted by executor
    5409             :      */
    5410         326 :     node->numCols = numCols;
    5411         326 :     if (numCols > 0)
    5412             :     {
    5413         158 :         int         keyno = 0;
    5414             :         AttrNumber *dupColIdx;
    5415             :         Oid        *dupOperators;
    5416             :         Oid        *dupCollations;
    5417             :         ListCell   *slitem;
    5418             : 
    5419         158 :         dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
    5420         158 :         dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
    5421         158 :         dupCollations = (Oid *) palloc(sizeof(Oid) * numCols);
    5422             : 
    5423         608 :         foreach(slitem, distinctList)
    5424             :         {
    5425         450 :             SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
    5426         450 :             TargetEntry *tle = get_sortgroupclause_tle(sortcl,
    5427             :                                                        plan->targetlist);
    5428             : 
    5429         450 :             dupColIdx[keyno] = tle->resno;
    5430         450 :             dupOperators[keyno] = sortcl->eqop;
    5431         450 :             dupCollations[keyno] = exprCollation((Node *) tle->expr);
    5432             :             Assert(OidIsValid(dupOperators[keyno]));
    5433         450 :             keyno++;
    5434             :         }
    5435         158 :         node->dupColIdx = dupColIdx;
    5436         158 :         node->dupOperators = dupOperators;
    5437         158 :         node->dupCollations = dupCollations;
    5438             :     }
    5439         326 :     node->numGroups = numGroups;
    5440             : 
    5441         326 :     return node;
    5442             : }
    5443             : 
    5444             : static BitmapAnd *
    5445          44 : make_bitmap_and(List *bitmapplans)
    5446             : {
    5447          44 :     BitmapAnd  *node = makeNode(BitmapAnd);
    5448          44 :     Plan       *plan = &node->plan;
    5449             : 
    5450          44 :     plan->targetlist = NIL;
    5451          44 :     plan->qual = NIL;
    5452          44 :     plan->lefttree = NULL;
    5453          44 :     plan->righttree = NULL;
    5454          44 :     node->bitmapplans = bitmapplans;
    5455             : 
    5456          44 :     return node;
    5457             : }
    5458             : 
    5459             : static BitmapOr *
    5460         104 : make_bitmap_or(List *bitmapplans)
    5461             : {
    5462         104 :     BitmapOr   *node = makeNode(BitmapOr);
    5463         104 :     Plan       *plan = &node->plan;
    5464             : 
    5465         104 :     plan->targetlist = NIL;
    5466         104 :     plan->qual = NIL;
    5467         104 :     plan->lefttree = NULL;
    5468         104 :     plan->righttree = NULL;
    5469         104 :     node->bitmapplans = bitmapplans;
    5470             : 
    5471         104 :     return node;
    5472             : }
    5473             : 
    5474             : static NestLoop *
    5475       29926 : make_nestloop(List *tlist,
    5476             :               List *joinclauses,
    5477             :               List *otherclauses,
    5478             :               List *nestParams,
    5479             :               Plan *lefttree,
    5480             :               Plan *righttree,
    5481             :               JoinType jointype,
    5482             :               bool inner_unique)
    5483             : {
    5484       29926 :     NestLoop   *node = makeNode(NestLoop);
    5485       29926 :     Plan       *plan = &node->join.plan;
    5486             : 
    5487       29926 :     plan->targetlist = tlist;
    5488       29926 :     plan->qual = otherclauses;
    5489       29926 :     plan->lefttree = lefttree;
    5490       29926 :     plan->righttree = righttree;
    5491       29926 :     node->join.jointype = jointype;
    5492       29926 :     node->join.inner_unique = inner_unique;
    5493       29926 :     node->join.joinqual = joinclauses;
    5494       29926 :     node->nestParams = nestParams;
    5495             : 
    5496       29926 :     return node;
    5497             : }
    5498             : 
    5499             : static HashJoin *
    5500       26406 : make_hashjoin(List *tlist,
    5501             :               List *joinclauses,
    5502             :               List *otherclauses,
    5503             :               List *hashclauses,
    5504             :               Plan *lefttree,
    5505             :               Plan *righttree,
    5506             :               JoinType jointype,
    5507             :               bool inner_unique)
    5508             : {
    5509       26406 :     HashJoin   *node = makeNode(HashJoin);
    5510       26406 :     Plan       *plan = &node->join.plan;
    5511             : 
    5512       26406 :     plan->targetlist = tlist;
    5513       26406 :     plan->qual = otherclauses;
    5514       26406 :     plan->lefttree = lefttree;
    5515       26406 :     plan->righttree = righttree;
    5516       26406 :     node->hashclauses = hashclauses;
    5517       26406 :     node->join.jointype = jointype;
    5518       26406 :     node->join.inner_unique = inner_unique;
    5519       26406 :     node->join.joinqual = joinclauses;
    5520             : 
    5521       26406 :     return node;
    5522             : }
    5523             : 
    5524             : static Hash *
    5525       26406 : make_hash(Plan *lefttree,
    5526             :           Oid skewTable,
    5527             :           AttrNumber skewColumn,
    5528             :           bool skewInherit)
    5529             : {
    5530       26406 :     Hash       *node = makeNode(Hash);
    5531       26406 :     Plan       *plan = &node->plan;
    5532             : 
    5533       26406 :     plan->targetlist = lefttree->targetlist;
    5534       26406 :     plan->qual = NIL;
    5535       26406 :     plan->lefttree = lefttree;
    5536       26406 :     plan->righttree = NULL;
    5537             : 
    5538       26406 :     node->skewTable = skewTable;
    5539       26406 :     node->skewColumn = skewColumn;
    5540       26406 :     node->skewInherit = skewInherit;
    5541             : 
    5542       26406 :     return node;
    5543             : }
    5544             : 
    5545             : static MergeJoin *
    5546        1926 : make_mergejoin(List *tlist,
    5547             :                List *joinclauses,
    5548             :                List *otherclauses,
    5549             :                List *mergeclauses,
    5550             :                Oid *mergefamilies,
    5551             :                Oid *mergecollations,
    5552             :                int *mergestrategies,
    5553             :                bool *mergenullsfirst,
    5554             :                Plan *lefttree,
    5555             :                Plan *righttree,
    5556             :                JoinType jointype,
    5557             :                bool inner_unique,
    5558             :                bool skip_mark_restore)
    5559             : {
    5560        1926 :     MergeJoin  *node = makeNode(MergeJoin);
    5561        1926 :     Plan       *plan = &node->join.plan;
    5562             : 
    5563        1926 :     plan->targetlist = tlist;
    5564        1926 :     plan->qual = otherclauses;
    5565        1926 :     plan->lefttree = lefttree;
    5566        1926 :     plan->righttree = righttree;
    5567        1926 :     node->skip_mark_restore = skip_mark_restore;
    5568        1926 :     node->mergeclauses = mergeclauses;
    5569        1926 :     node->mergeFamilies = mergefamilies;
    5570        1926 :     node->mergeCollations = mergecollations;
    5571        1926 :     node->mergeStrategies = mergestrategies;
    5572        1926 :     node->mergeNullsFirst = mergenullsfirst;
    5573        1926 :     node->join.jointype = jointype;
    5574        1926 :     node->join.inner_unique = inner_unique;
    5575        1926 :     node->join.joinqual = joinclauses;
    5576             : 
    5577        1926 :     return node;
    5578             : }
    5579             : 
    5580             : /*
    5581             :  * make_sort --- basic routine to build a Sort plan node
    5582             :  *
    5583             :  * Caller must have built the sortColIdx, sortOperators, collations, and
    5584             :  * nullsFirst arrays already.
    5585             :  */
    5586             : static Sort *
    5587       30324 : make_sort(Plan *lefttree, int numCols,
    5588             :           AttrNumber *sortColIdx, Oid *sortOperators,
    5589             :           Oid *collations, bool *nullsFirst)
    5590             : {
    5591       30324 :     Sort       *node = makeNode(Sort);
    5592       30324 :     Plan       *plan = &node->plan;
    5593             : 
    5594       30324 :     plan->targetlist = lefttree->targetlist;
    5595       30324 :     plan->qual = NIL;
    5596       30324 :     plan->lefttree = lefttree;
    5597       30324 :     plan->righttree = NULL;
    5598       30324 :     node->numCols = numCols;
    5599       30324 :     node->sortColIdx = sortColIdx;
    5600       30324 :     node->sortOperators = sortOperators;
    5601       30324 :     node->collations = collations;
    5602       30324 :     node->nullsFirst = nullsFirst;
    5603             : 
    5604       30324 :     return node;
    5605             : }
    5606             : 
    5607             : /*
    5608             :  * prepare_sort_from_pathkeys
    5609             :  *    Prepare to sort according to given pathkeys
    5610             :  *
    5611             :  * This is used to set up for Sort, MergeAppend, and Gather Merge nodes.  It
    5612             :  * calculates the executor's representation of the sort key information, and
    5613             :  * adjusts the plan targetlist if needed to add resjunk sort columns.
    5614             :  *
    5615             :  * Input parameters:
    5616             :  *    'lefttree' is the plan node which yields input tuples
    5617             :  *    'pathkeys' is the list of pathkeys by which the result is to be sorted
    5618             :  *    'relids' identifies the child relation being sorted, if any
    5619             :  *    'reqColIdx' is NULL or an array of required sort key column numbers
    5620             :  *    'adjust_tlist_in_place' is true if lefttree must be modified in-place
    5621             :  *
    5622             :  * We must convert the pathkey information into arrays of sort key column
    5623             :  * numbers, sort operator OIDs, collation OIDs, and nulls-first flags,
    5624             :  * which is the representation the executor wants.  These are returned into
    5625             :  * the output parameters *p_numsortkeys etc.
    5626             :  *
    5627             :  * When looking for matches to an EquivalenceClass's members, we will only
    5628             :  * consider child EC members if they belong to given 'relids'.  This protects
    5629             :  * against possible incorrect matches to child expressions that contain no
    5630             :  * Vars.
    5631             :  *
    5632             :  * If reqColIdx isn't NULL then it contains sort key column numbers that
    5633             :  * we should match.  This is used when making child plans for a MergeAppend;
    5634             :  * it's an error if we can't match the columns.
    5635             :  *
    5636             :  * If the pathkeys include expressions that aren't simple Vars, we will
    5637             :  * usually need to add resjunk items to the input plan's targetlist to
    5638             :  * compute these expressions, since a Sort or MergeAppend node itself won't
    5639             :  * do any such calculations.  If the input plan type isn't one that can do
    5640             :  * projections, this means adding a Result node just to do the projection.
    5641             :  * However, the caller can pass adjust_tlist_in_place = true to force the
    5642             :  * lefttree tlist to be modified in-place regardless of whether the node type
    5643             :  * can project --- we use this for fixing the tlist of MergeAppend itself.
    5644             :  *
    5645             :  * Returns the node which is to be the input to the Sort (either lefttree,
    5646             :  * or a Result stacked atop lefttree).
    5647             :  */
    5648             : static Plan *
    5649       31622 : prepare_sort_from_pathkeys(Plan *lefttree, List *pathkeys,
    5650             :                            Relids relids,
    5651             :                            const AttrNumber *reqColIdx,
    5652             :                            bool adjust_tlist_in_place,
    5653             :                            int *p_numsortkeys,
    5654             :                            AttrNumber **p_sortColIdx,
    5655             :                            Oid **p_sortOperators,
    5656             :                            Oid **p_collations,
    5657             :                            bool **p_nullsFirst)
    5658             : {
    5659       31622 :     List       *tlist = lefttree->targetlist;
    5660             :     ListCell   *i;
    5661             :     int         numsortkeys;
    5662             :     AttrNumber *sortColIdx;
    5663             :     Oid        *sortOperators;
    5664             :     Oid        *collations;
    5665             :     bool       *nullsFirst;
    5666             : 
    5667             :     /*
    5668             :      * We will need at most list_length(pathkeys) sort columns; possibly less
    5669             :      */
    5670       31622 :     numsortkeys = list_length(pathkeys);
    5671       31622 :     sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
    5672       31622 :     sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
    5673       31622 :     collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
    5674       31622 :     nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
    5675             : 
    5676       31622 :     numsortkeys = 0;
    5677             : 
    5678       72286 :     foreach(i, pathkeys)
    5679             :     {
    5680       40664 :         PathKey    *pathkey = (PathKey *) lfirst(i);
    5681       40664 :         EquivalenceClass *ec = pathkey->pk_eclass;
    5682             :         EquivalenceMember *em;
    5683       40664 :         TargetEntry *tle = NULL;
    5684       40664 :         Oid         pk_datatype = InvalidOid;
    5685             :         Oid         sortop;
    5686             :         ListCell   *j;
    5687             : 
    5688       40664 :         if (ec->ec_has_volatile)
    5689             :         {
    5690             :             /*
    5691             :              * If the pathkey's EquivalenceClass is volatile, then it must
    5692             :              * have come from an ORDER BY clause, and we have to match it to
    5693             :              * that same targetlist entry.
    5694             :              */
    5695          48 :             if (ec->ec_sortref == 0) /* can't happen */
    5696           0 :                 elog(ERROR, "volatile EquivalenceClass has no sortref");
    5697          48 :             tle = get_sortgroupref_tle(ec->ec_sortref, tlist);
    5698             :             Assert(tle);
    5699             :             Assert(list_length(ec->ec_members) == 1);
    5700          48 :             pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
    5701             :         }
    5702       40616 :         else if (reqColIdx != NULL)
    5703             :         {
    5704             :             /*
    5705             :              * If we are given a sort column number to match, only consider
    5706             :              * the single TLE at that position.  It's possible that there is
    5707             :              * no such TLE, in which case fall through and generate a resjunk
    5708             :              * targetentry (we assume this must have happened in the parent
    5709             :              * plan as well).  If there is a TLE but it doesn't match the
    5710             :              * pathkey's EC, we do the same, which is probably the wrong thing
    5711             :              * but we'll leave it to caller to complain about the mismatch.
    5712             :              */
    5713        1408 :             tle = get_tle_by_resno(tlist, reqColIdx[numsortkeys]);
    5714        1408 :             if (tle)
    5715             :             {
    5716        1344 :                 em = find_ec_member_for_tle(ec, tle, relids);
    5717        1344 :                 if (em)
    5718             :                 {
    5719             :                     /* found expr at right place in tlist */
    5720        1344 :                     pk_datatype = em->em_datatype;
    5721             :                 }
    5722             :                 else
    5723           0 :                     tle = NULL;
    5724             :             }
    5725             :         }
    5726             :         else
    5727             :         {
    5728             :             /*
    5729             :              * Otherwise, we can sort by any non-constant expression listed in
    5730             :              * the pathkey's EquivalenceClass.  For now, we take the first
    5731             :              * tlist item found in the EC. If there's no match, we'll generate
    5732             :              * a resjunk entry using the first EC member that is an expression
    5733             :              * in the input's vars.  (The non-const restriction only matters
    5734             :              * if the EC is below_outer_join; but if it isn't, it won't
    5735             :              * contain consts anyway, else we'd have discarded the pathkey as
    5736             :              * redundant.)
    5737             :              *
    5738             :              * XXX if we have a choice, is there any way of figuring out which
    5739             :              * might be cheapest to execute?  (For example, int4lt is likely
    5740             :              * much cheaper to execute than numericlt, but both might appear
    5741             :              * in the same equivalence class...)  Not clear that we ever will
    5742             :              * have an interesting choice in practice, so it may not matter.
    5743             :              */
    5744       85984 :             foreach(j, tlist)
    5745             :             {
    5746       85876 :                 tle = (TargetEntry *) lfirst(j);
    5747       85876 :                 em = find_ec_member_for_tle(ec, tle, relids);
    5748       85876 :                 if (em)
    5749             :                 {
    5750             :                     /* found expr already in tlist */
    5751       39100 :                     pk_datatype = em->em_datatype;
    5752       39100 :                     break;
    5753             :                 }
    5754       46776 :                 tle = NULL;
    5755             :             }
    5756             :         }
    5757             : 
    5758       40664 :         if (!tle)
    5759             :         {
    5760             :             /*
    5761             :              * No matching tlist item; look for a computable expression. Note
    5762             :              * that we treat Aggrefs as if they were variables; this is
    5763             :              * necessary when attempting to sort the output from an Agg node
    5764             :              * for use in a WindowFunc (since grouping_planner will have
    5765             :              * treated the Aggrefs as variables, too).  Likewise, if we find a
    5766             :              * WindowFunc in a sort expression, treat it as a variable.
    5767             :              */
    5768         172 :             Expr       *sortexpr = NULL;
    5769             : 
    5770         632 :             foreach(j, ec->ec_members)
    5771             :             {
    5772         632 :                 EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
    5773             :                 List       *exprvars;
    5774             :                 ListCell   *k;
    5775             : 
    5776             :                 /*
    5777             :                  * We shouldn't be trying to sort by an equivalence class that
    5778             :                  * contains a constant, so no need to consider such cases any
    5779             :                  * further.
    5780             :                  */
    5781         632 :                 if (em->em_is_const)
    5782           0 :                     continue;
    5783             : 
    5784             :                 /*
    5785             :                  * Ignore child members unless they belong to the rel being
    5786             :                  * sorted.
    5787             :                  */
    5788        1032 :                 if (em->em_is_child &&
    5789         400 :                     !bms_is_subset(em->em_relids, relids))
    5790         288 :                     continue;
    5791             : 
    5792         344 :                 sortexpr = em->em_expr;
    5793         344 :                 exprvars = pull_var_clause((Node *) sortexpr,
    5794             :                                            PVC_INCLUDE_AGGREGATES |
    5795             :                                            PVC_INCLUDE_WINDOWFUNCS |
    5796             :                                            PVC_INCLUDE_PLACEHOLDERS);
    5797         572 :                 foreach(k, exprvars)
    5798             :                 {
    5799         400 :                     if (!tlist_member_ignore_relabel(lfirst(k), tlist))
    5800         172 :                         break;
    5801             :                 }
    5802         344 :                 list_free(exprvars);
    5803         344 :                 if (!k)
    5804             :                 {
    5805         172 :                     pk_datatype = em->em_datatype;
    5806         172 :                     break;      /* found usable expression */
    5807             :                 }
    5808             :             }
    5809         172 :             if (!j)
    5810           0 :                 elog(ERROR, "could not find pathkey item to sort");
    5811             : 
    5812             :             /*
    5813             :              * Do we need to insert a Result node?
    5814             :              */
    5815         328 :             if (!adjust_tlist_in_place &&
    5816         156 :                 !is_projection_capable_plan(lefttree))
    5817             :             {
    5818             :                 /* copy needed so we don't modify input's tlist below */
    5819          16 :                 tlist = copyObject(tlist);
    5820          16 :                 lefttree = inject_projection_plan(lefttree, tlist,
    5821          16 :                                                   lefttree->parallel_safe);
    5822             :             }
    5823             : 
    5824             :             /* Don't bother testing is_projection_capable_plan again */
    5825         172 :             adjust_tlist_in_place = true;
    5826             : 
    5827             :             /*
    5828             :              * Add resjunk entry to input's tlist
    5829             :              */
    5830         172 :             tle = makeTargetEntry(sortexpr,
    5831         172 :                                   list_length(tlist) + 1,
    5832             :                                   NULL,
    5833             :                                   true);
    5834         172 :             tlist = lappend(tlist, tle);
    5835         172 :             lefttree->targetlist = tlist;    /* just in case NIL before */
    5836             :         }
    5837             : 
    5838             :         /*
    5839             :          * Look up the correct sort operator from the PathKey's slightly
    5840             :          * abstracted representation.
    5841             :          */
    5842       40664 :         sortop = get_opfamily_member(pathkey->pk_opfamily,
    5843             :                                      pk_datatype,
    5844             :                                      pk_datatype,
    5845       40664 :                                      pathkey->pk_strategy);
    5846       40664 :         if (!OidIsValid(sortop))    /* should not happen */
    5847           0 :             elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
    5848             :                  pathkey->pk_strategy, pk_datatype, pk_datatype,
    5849             :                  pathkey->pk_opfamily);
    5850             : 
    5851             :         /* Add the column to the sort arrays */
    5852       40664 :         sortColIdx[numsortkeys] = tle->resno;
    5853       40664 :         sortOperators[numsortkeys] = sortop;
    5854       40664 :         collations[numsortkeys] = ec->ec_collation;
    5855       40664 :         nullsFirst[numsortkeys] = pathkey->pk_nulls_first;
    5856       40664 :         numsortkeys++;
    5857             :     }
    5858             : 
    5859             :     /* Return results */
    5860       31622 :     *p_numsortkeys = numsortkeys;
    5861       31622 :     *p_sortColIdx = sortColIdx;
    5862       31622 :     *p_sortOperators = sortOperators;
    5863       31622 :     *p_collations = collations;
    5864       31622 :     *p_nullsFirst = nullsFirst;
    5865             : 
    5866       31622 :     return lefttree;
    5867             : }
    5868             : 
    5869             : /*
    5870             :  * find_ec_member_for_tle
    5871             :  *      Locate an EquivalenceClass member matching the given TLE, if any
    5872             :  *
    5873             :  * Child EC members are ignored unless they belong to given 'relids'.
    5874             :  */
    5875             : static EquivalenceMember *
    5876       87946 : find_ec_member_for_tle(EquivalenceClass *ec,
    5877             :                        TargetEntry *tle,
    5878             :                        Relids relids)
    5879             : {
    5880             :     Expr       *tlexpr;
    5881             :     ListCell   *lc;
    5882             : 
    5883             :     /* We ignore binary-compatible relabeling on both ends */
    5884       87946 :     tlexpr = tle->expr;
    5885      180886 :     while (tlexpr && IsA(tlexpr, RelabelType))
    5886        4994 :         tlexpr = ((RelabelType *) tlexpr)->arg;
    5887             : 
    5888      158380 :     foreach(lc, ec->ec_members)
    5889             :     {
    5890      111368 :         EquivalenceMember *em = (EquivalenceMember *) lfirst(lc);
    5891             :         Expr       *emexpr;
    5892             : 
    5893             :         /*
    5894             :          * We shouldn't be trying to sort by an equivalence class that
    5895             :          * contains a constant, so no need to consider such cases any further.
    5896             :          */
    5897      111368 :         if (em->em_is_const)
    5898           0 :             continue;
    5899             : 
    5900             :         /*
    5901             :          * Ignore child members unless they belong to the rel being sorted.
    5902             :          */
    5903      132582 :         if (em->em_is_child &&
    5904       21214 :             !bms_is_subset(em->em_relids, relids))
    5905       19294 :             continue;
    5906             : 
    5907             :         /* Match if same expression (after stripping relabel) */
    5908       92074 :         emexpr = em->em_expr;
    5909      186102 :         while (emexpr && IsA(emexpr, RelabelType))
    5910        1954 :             emexpr = ((RelabelType *) emexpr)->arg;
    5911             : 
    5912       92074 :         if (equal(emexpr, tlexpr))
    5913       40934 :             return em;
    5914             :     }
    5915             : 
    5916       47012 :     return NULL;
    5917             : }
    5918             : 
    5919             : /*
    5920             :  * make_sort_from_pathkeys
    5921             :  *    Create sort plan to sort according to given pathkeys
    5922             :  *
    5923             :  *    'lefttree' is the node which yields input tuples
    5924             :  *    'pathkeys' is the list of pathkeys by which the result is to be sorted
    5925             :  *    'relids' is the set of relations required by prepare_sort_from_pathkeys()
    5926             :  */
    5927             : static Sort *
    5928       30166 : make_sort_from_pathkeys(Plan *lefttree, List *pathkeys, Relids relids)
    5929             : {
    5930             :     int         numsortkeys;
    5931             :     AttrNumber *sortColIdx;
    5932             :     Oid        *sortOperators;
    5933             :     Oid        *collations;
    5934             :     bool       *nullsFirst;
    5935             : 
    5936             :     /* Compute sort column info, and adjust lefttree as needed */
    5937       30166 :     lefttree = prepare_sort_from_pathkeys(lefttree, pathkeys,
    5938             :                                           relids,
    5939             :                                           NULL,
    5940             :                                           false,
    5941             :                                           &numsortkeys,
    5942             :                                           &sortColIdx,
    5943             :                                           &sortOperators,
    5944             :                                           &collations,
    5945             :                                           &nullsFirst);
    5946             : 
    5947             :     /* Now build the Sort node */
    5948       30166 :     return make_sort(lefttree, numsortkeys,
    5949             :                      sortColIdx, sortOperators,
    5950             :                      collations, nullsFirst);
    5951             : }
    5952             : 
    5953             : /*
    5954             :  * make_sort_from_sortclauses
    5955             :  *    Create sort plan to sort according to given sortclauses
    5956             :  *
    5957             :  *    'sortcls' is a list of SortGroupClauses
    5958             :  *    'lefttree' is the node which yields input tuples
    5959             :  */
    5960             : Sort *
    5961           2 : make_sort_from_sortclauses(List *sortcls, Plan *lefttree)
    5962             : {
    5963           2 :     List       *sub_tlist = lefttree->targetlist;
    5964             :     ListCell   *l;
    5965             :     int         numsortkeys;
    5966             :     AttrNumber *sortColIdx;
    5967             :     Oid        *sortOperators;
    5968             :     Oid        *collations;
    5969             :     bool       *nullsFirst;
    5970             : 
    5971             :     /* Convert list-ish representation to arrays wanted by executor */
    5972           2 :     numsortkeys = list_length(sortcls);
    5973           2 :     sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
    5974           2 :     sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
    5975           2 :     collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
    5976           2 :     nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
    5977             : 
    5978           2 :     numsortkeys = 0;
    5979           4 :     foreach(l, sortcls)
    5980             :     {
    5981           2 :         SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
    5982           2 :         TargetEntry *tle = get_sortgroupclause_tle(sortcl, sub_tlist);
    5983             : 
    5984           2 :         sortColIdx[numsortkeys] = tle->resno;
    5985           2 :         sortOperators[numsortkeys] = sortcl->sortop;
    5986           2 :         collations[numsortkeys] = exprCollation((Node *) tle->expr);
    5987           2 :         nullsFirst[numsortkeys] = sortcl->nulls_first;
    5988           2 :         numsortkeys++;
    5989             :     }
    5990             : 
    5991           2 :     return make_sort(lefttree, numsortkeys,
    5992             :                      sortColIdx, sortOperators,
    5993             :                      collations, nullsFirst);
    5994             : }
    5995             : 
    5996             : /*
    5997             :  * make_sort_from_groupcols
    5998             :  *    Create sort plan to sort based on grouping columns
    5999             :  *
    6000             :  * 'groupcls' is the list of SortGroupClauses
    6001             :  * 'grpColIdx' gives the column numbers to use
    6002             :  *
    6003             :  * This might look like it could be merged with make_sort_from_sortclauses,
    6004             :  * but presently we *must* use the grpColIdx[] array to locate sort columns,
    6005             :  * because the child plan's tlist is not marked with ressortgroupref info
    6006             :  * appropriate to the grouping node.  So, only the sort ordering info
    6007             :  * is used from the SortGroupClause entries.
    6008             :  */
    6009             : static Sort *
    6010         112 : make_sort_from_groupcols(List *groupcls,
    6011             :                          AttrNumber *grpColIdx,
    6012             :                          Plan *lefttree)
    6013             : {
    6014         112 :     List       *sub_tlist = lefttree->targetlist;
    6015             :     ListCell   *l;
    6016             :     int         numsortkeys;
    6017             :     AttrNumber *sortColIdx;
    6018             :     Oid        *sortOperators;
    6019             :     Oid        *collations;
    6020             :     bool       *nullsFirst;
    6021             : 
    6022             :     /* Convert list-ish representation to arrays wanted by executor */
    6023         112 :     numsortkeys = list_length(groupcls);
    6024         112 :     sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
    6025         112 :     sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
    6026         112 :     collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
    6027         112 :     nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
    6028             : 
    6029         112 :     numsortkeys = 0;
    6030         268 :     foreach(l, groupcls)
    6031             :     {
    6032         156 :         SortGroupClause *grpcl = (SortGroupClause *) lfirst(l);
    6033         156 :         TargetEntry *tle = get_tle_by_resno(sub_tlist, grpColIdx[numsortkeys]);
    6034             : 
    6035         156 :         if (!tle)
    6036           0 :             elog(ERROR, "could not retrieve tle for sort-from-groupcols");
    6037             : 
    6038         156 :         sortColIdx[numsortkeys] = tle->resno;
    6039         156 :         sortOperators[numsortkeys] = grpcl->sortop;
    6040         156 :         collations[numsortkeys] = exprCollation((Node *) tle->expr);
    6041         156 :         nullsFirst[numsortkeys] = grpcl->nulls_first;
    6042         156 :         numsortkeys++;
    6043             :     }
    6044             : 
    6045         112 :     return make_sort(lefttree, numsortkeys,
    6046             :                      sortColIdx, sortOperators,
    6047             :                      collations, nullsFirst);
    6048             : }
    6049             : 
    6050             : static Material *
    6051        2766 : make_material(Plan *lefttree)
    6052             : {
    6053        2766 :     Material   *node = makeNode(Material);
    6054        2766 :     Plan       *plan = &node->plan;
    6055             : 
    6056        2766 :     plan->targetlist = lefttree->targetlist;
    6057        2766 :     plan->qual = NIL;
    6058        2766 :     plan->lefttree = lefttree;
    6059        2766 :     plan->righttree = NULL;
    6060             : 
    6061        2766 :     return node;
    6062             : }
    6063             : 
    6064             : /*
    6065             :  * materialize_finished_plan: stick a Material node atop a completed plan
    6066             :  *
    6067             :  * There are a couple of places where we want to attach a Material node
    6068             :  * after completion of create_plan(), without any MaterialPath path.
    6069             :  * Those places should probably be refactored someday to do this on the
    6070             :  * Path representation, but it's not worth the trouble yet.
    6071             :  */
    6072             : Plan *
    6073          28 : materialize_finished_plan(Plan *subplan)
    6074             : {
    6075             :     Plan       *matplan;
    6076             :     Path        matpath;        /* dummy for result of cost_material */
    6077             : 
    6078          28 :     matplan = (Plan *) make_material(subplan);
    6079             : 
    6080             :     /*
    6081             :      * XXX horrid kluge: if there are any initPlans attached to the subplan,
    6082             :      * move them up to the Material node, which is now effectively the top
    6083             :      * plan node in its query level.  This prevents failure in
    6084             :      * SS_finalize_plan(), which see for comments.  We don't bother adjusting
    6085             :      * the subplan's cost estimate for this.
    6086             :      */
    6087          28 :     matplan->initPlan = subplan->initPlan;
    6088          28 :     subplan->initPlan = NIL;
    6089             : 
    6090             :     /* Set cost data */
    6091          28 :     cost_material(&matpath,
    6092             :                   subplan->startup_cost,
    6093             :                   subplan->total_cost,
    6094             :                   subplan->plan_rows,
    6095             :                   subplan->plan_width);
    6096          28 :     matplan->startup_cost = matpath.startup_cost;
    6097          28 :     matplan->total_cost = matpath.total_cost;
    6098          28 :     matplan->plan_rows = subplan->plan_rows;
    6099          28 :     matplan->plan_width = subplan->plan_width;
    6100          28 :     matplan->parallel_aware = false;
    6101          28 :     matplan->parallel_safe = subplan->parallel_safe;
    6102             : 
    6103          28 :     return matplan;
    6104             : }
    6105             : 
    6106             : Agg *
    6107       25154 : make_agg(List *tlist, List *qual,
    6108             :          AggStrategy aggstrategy, AggSplit aggsplit,
    6109             :          int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Oid *grpCollations,
    6110             :          List *groupingSets, List *chain,
    6111             :          double dNumGroups, Plan *lefttree)
    6112             : {
    6113       25154 :     Agg        *node = makeNode(Agg);
    6114       25154 :     Plan       *plan = &node->plan;
    6115             :     long        numGroups;
    6116             : 
    6117             :     /* Reduce to long, but 'ware overflow! */
    6118       25154 :     numGroups = (long) Min(dNumGroups, (double) LONG_MAX);
    6119             : 
    6120       25154 :     node->aggstrategy = aggstrategy;
    6121       25154 :     node->aggsplit = aggsplit;
    6122       25154 :     node->numCols = numGroupCols;
    6123       25154 :     node->grpColIdx = grpColIdx;
    6124       25154 :     node->grpOperators = grpOperators;
    6125       25154 :     node->grpCollations = grpCollations;
    6126       25154 :     node->numGroups = numGroups;
    6127       25154 :     node->aggParams = NULL;      /* SS_finalize_plan() will fill this */
    6128       25154 :     node->groupingSets = groupingSets;
    6129       25154 :     node->chain = chain;
    6130             : 
    6131       25154 :     plan->qual = qual;
    6132       25154 :     plan->targetlist = tlist;
    6133       25154 :     plan->lefttree = lefttree;
    6134       25154 :     plan->righttree = NULL;
    6135             : 
    6136       25154 :     return node;
    6137             : }
    6138             : 
    6139             : static WindowAgg *
    6140        1124 : make_windowagg(List *tlist, Index winref,
    6141             :                int partNumCols, AttrNumber *partColIdx, Oid *partOperators, Oid *partCollations,
    6142             :                int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators, Oid *ordCollations,
    6143             :                int frameOptions, Node *startOffset, Node *endOffset,
    6144             :                Oid startInRangeFunc, Oid endInRangeFunc,
    6145             :                Oid inRangeColl, bool inRangeAsc, bool inRangeNullsFirst,
    6146             :                Plan *lefttree)
    6147             : {
    6148        1124 :     WindowAgg  *node = makeNode(WindowAgg);
    6149        1124 :     Plan       *plan = &node->plan;
    6150             : 
    6151        1124 :     node->winref = winref;
    6152        1124 :     node->partNumCols = partNumCols;
    6153        1124 :     node->partColIdx = partColIdx;
    6154        1124 :     node->partOperators = partOperators;
    6155        1124 :     node->partCollations = partCollations;
    6156        1124 :     node->ordNumCols = ordNumCols;
    6157        1124 :     node->ordColIdx = ordColIdx;
    6158        1124 :     node->ordOperators = ordOperators;
    6159        1124 :     node->ordCollations = ordCollations;
    6160        1124 :     node->frameOptions = frameOptions;
    6161        1124 :     node->startOffset = startOffset;
    6162        1124 :     node->endOffset = endOffset;
    6163        1124 :     node->startInRangeFunc = startInRangeFunc;
    6164        1124 :     node->endInRangeFunc = endInRangeFunc;
    6165        1124 :     node->inRangeColl = inRangeColl;
    6166        1124 :     node->inRangeAsc = inRangeAsc;
    6167        1124 :     node->inRangeNullsFirst = inRangeNullsFirst;
    6168             : 
    6169        1124 :     plan->targetlist = tlist;
    6170        1124 :     plan->lefttree = lefttree;
    6171        1124 :     plan->righttree = NULL;
    6172             :     /* WindowAgg nodes never have a qual clause */
    6173        1124 :     plan->qual = NIL;
    6174             : 
    6175        1124 :     return node;
    6176             : }
    6177             : 
    6178             : static Group *
    6179         106 : make_group(List *tlist,
    6180             :            List *qual,
    6181             :            int numGroupCols,
    6182             :            AttrNumber *grpColIdx,
    6183             :            Oid *grpOperators,
    6184             :            Oid *grpCollations,
    6185             :            Plan *lefttree)
    6186             : {
    6187         106 :     Group      *node = makeNode(Group);
    6188         106 :     Plan       *plan = &node->plan;
    6189             : 
    6190         106 :     node->numCols = numGroupCols;
    6191         106 :     node->grpColIdx = grpColIdx;
    6192         106 :     node->grpOperators = grpOperators;
    6193         106 :     node->grpCollations = grpCollations;
    6194             : 
    6195         106 :     plan->qual = qual;
    6196         106 :     plan->targetlist = tlist;
    6197         106 :     plan->lefttree = lefttree;
    6198         106 :     plan->righttree = NULL;
    6199             : 
    6200         106 :     return node;
    6201             : }
    6202             : 
    6203             : /*
    6204             :  * distinctList is a list of SortGroupClauses, identifying the targetlist items
    6205             :  * that should be considered by the Unique filter.  The input path must
    6206             :  * already be sorted accordingly.
    6207             :  */
    6208             : static Unique *
    6209           2 : make_unique_from_sortclauses(Plan *lefttree, List *distinctList)
    6210             : {
    6211           2 :     Unique     *node = makeNode(Unique);
    6212           2 :     Plan       *plan = &node->plan;
    6213           2 :     int         numCols = list_length(distinctList);
    6214           2 :     int         keyno = 0;
    6215             :     AttrNumber *uniqColIdx;
    6216             :     Oid        *uniqOperators;
    6217             :     Oid        *uniqCollations;
    6218             :     ListCell   *slitem;
    6219             : 
    6220           2 :     plan->targetlist = lefttree->targetlist;
    6221           2 :     plan->qual = NIL;
    6222           2 :     plan->lefttree = lefttree;
    6223           2 :     plan->righttree = NULL;
    6224             : 
    6225             :     /*
    6226             :      * convert SortGroupClause list into arrays of attr indexes and equality
    6227             :      * operators, as wanted by executor
    6228             :      */
    6229             :     Assert(numCols > 0);
    6230           2 :     uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
    6231           2 :     uniqOperators = (Oid *) palloc(sizeof(Oid) * numCols);
    6232           2 :     uniqCollations = (Oid *) palloc(sizeof(Oid) * numCols);
    6233             : 
    6234           4 :     foreach(slitem, distinctList)
    6235             :     {
    6236           2 :         SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
    6237           2 :         TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
    6238             : 
    6239           2 :         uniqColIdx[keyno] = tle->resno;
    6240           2 :         uniqOperators[keyno] = sortcl->eqop;
    6241           2 :         uniqCollations[keyno] = exprCollation((Node *) tle->expr);
    6242             :         Assert(OidIsValid(uniqOperators[keyno]));
    6243           2 :         keyno++;
    6244             :     }
    6245             : 
    6246           2 :     node->numCols = numCols;
    6247           2 :     node->uniqColIdx = uniqColIdx;
    6248           2 :     node->uniqOperators = uniqOperators;
    6249           2 :     node->uniqCollations = uniqCollations;
    6250             : 
    6251           2 :     return node;
    6252             : }
    6253             : 
    6254             : /*
    6255             :  * as above, but use pathkeys to identify the sort columns and semantics
    6256             :  */
    6257             : static Unique *
    6258         316 : make_unique_from_pathkeys(Plan *lefttree, List *pathkeys, int numCols)
    6259             : {
    6260         316 :     Unique     *node = makeNode(Unique);
    6261         316 :     Plan       *plan = &node->plan;
    6262         316 :     int         keyno = 0;
    6263             :     AttrNumber *uniqColIdx;
    6264             :     Oid        *uniqOperators;
    6265             :     Oid        *uniqCollations;
    6266             :     ListCell   *lc;
    6267             : 
    6268         316 :     plan->targetlist = lefttree->targetlist;
    6269         316 :     plan->qual = NIL;
    6270         316 :     plan->lefttree = lefttree;
    6271         316 :     plan->righttree = NULL;
    6272             : 
    6273             :     /*
    6274             :      * Convert pathkeys list into arrays of attr indexes and equality
    6275             :      * operators, as wanted by executor.  This has a lot in common with
    6276             :      * prepare_sort_from_pathkeys ... maybe unify sometime?
    6277             :      */
    6278             :     Assert(numCols >= 0 && numCols <= list_length(pathkeys));
    6279         316 :     uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
    6280         316 :     uniqOperators = (Oid *) palloc(sizeof(Oid) * numCols);
    6281         316 :     uniqCollations = (Oid *) palloc(sizeof(Oid) * numCols);
    6282             : 
    6283         810 :     foreach(lc, pathkeys)
    6284             :     {
    6285         514 :         PathKey    *pathkey = (PathKey *) lfirst(lc);
    6286         514 :         EquivalenceClass *ec = pathkey->pk_eclass;
    6287             :         EquivalenceMember *em;
    6288         514 :         TargetEntry *tle = NULL;
    6289         514 :         Oid         pk_datatype = InvalidOid;
    6290             :         Oid         eqop;
    6291             :         ListCell   *j;
    6292             : 
    6293             :         /* Ignore pathkeys beyond the specified number of columns */
    6294         514 :         if (keyno >= numCols)
    6295          20 :             break;
    6296             : 
    6297         494 :         if (ec->ec_has_volatile)
    6298             :         {
    6299             :             /*
    6300             :              * If the pathkey's EquivalenceClass is volatile, then it must
    6301             :              * have come from an ORDER BY clause, and we have to match it to
    6302             :              * that same targetlist entry.
    6303             :              */
    6304           4 :             if (ec->ec_sortref == 0) /* can't happen */
    6305           0 :                 elog(ERROR, "volatile EquivalenceClass has no sortref");
    6306           4 :             tle = get_sortgroupref_tle(ec->ec_sortref, plan->targetlist);
    6307             :             Assert(tle);
    6308             :             Assert(list_length(ec->ec_members) == 1);
    6309           4 :             pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
    6310             :         }
    6311             :         else
    6312             :         {
    6313             :             /*
    6314             :              * Otherwise, we can use any non-constant expression listed in the
    6315             :              * pathkey's EquivalenceClass.  For now, we take the first tlist
    6316             :              * item found in the EC.
    6317             :              */
    6318         726 :             foreach(j, plan->targetlist)
    6319             :             {
    6320         726 :                 tle = (TargetEntry *) lfirst(j);
    6321         726 :                 em = find_ec_member_for_tle(ec, tle, NULL);
    6322         726 :                 if (em)
    6323             :                 {
    6324             :                     /* found expr already in tlist */
    6325         490 :                     pk_datatype = em->em_datatype;
    6326         490 :                     break;
    6327             :                 }
    6328         236 :                 tle = NULL;
    6329             :             }
    6330             :         }
    6331             : 
    6332         494 :         if (!tle)
    6333           0 :             elog(ERROR, "could not find pathkey item to sort");
    6334             : 
    6335             :         /*
    6336             :          * Look up the correct equality operator from the PathKey's slightly
    6337             :          * abstracted representation.
    6338             :          */
    6339         494 :         eqop = get_opfamily_member(pathkey->pk_opfamily,
    6340             :                                    pk_datatype,
    6341             :                                    pk_datatype,
    6342             :                                    BTEqualStrategyNumber);
    6343         494 :         if (!OidIsValid(eqop))  /* should not happen */
    6344           0 :             elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
    6345             :                  BTEqualStrategyNumber, pk_datatype, pk_datatype,
    6346             :                  pathkey->pk_opfamily);
    6347             : 
    6348         494 :         uniqColIdx[keyno] = tle->resno;
    6349         494 :         uniqOperators[keyno] = eqop;
    6350         494 :         uniqCollations[keyno] = ec->ec_collation;
    6351             : 
    6352         494 :         keyno++;
    6353             :     }
    6354             : 
    6355         316 :     node->numCols = numCols;
    6356         316 :     node->uniqColIdx = uniqColIdx;
    6357         316 :     node->uniqOperators = uniqOperators;
    6358         316 :     node->uniqCollations = uniqCollations;
    6359             : 
    6360         316 :     return node;
    6361             : }
    6362             : 
    6363             : static Gather *
    6364         524 : make_gather(List *qptlist,
    6365             :             List *qpqual,
    6366             :             int nworkers,
    6367             :             int rescan_param,
    6368             :             bool single_copy,
    6369             :             Plan *subplan)
    6370             : {
    6371         524 :     Gather     *node = makeNode(Gather);
    6372         524 :     Plan       *plan = &node->plan;
    6373             : 
    6374         524 :     plan->targetlist = qptlist;
    6375         524 :     plan->qual = qpqual;
    6376         524 :     plan->lefttree = subplan;
    6377         524 :     plan->righttree = NULL;
    6378         524 :     node->num_workers = nworkers;
    6379         524 :     node->rescan_param = rescan_param;
    6380         524 :     node->single_copy = single_copy;
    6381         524 :     node->invisible = false;
    6382         524 :     node->initParam = NULL;
    6383             : 
    6384         524 :     return node;
    6385             : }
    6386             : 
    6387             : /*
    6388             :  * distinctList is a list of SortGroupClauses, identifying the targetlist
    6389             :  * items that should be considered by the SetOp filter.  The input path must
    6390             :  * already be sorted accordingly.
    6391             :  */
    6392             : static SetOp *
    6393         396 : make_setop(SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree,
    6394             :            List *distinctList, AttrNumber flagColIdx, int firstFlag,
    6395             :            long numGroups)
    6396             : {
    6397         396 :     SetOp      *node = makeNode(SetOp);
    6398         396 :     Plan       *plan = &node->plan;
    6399         396 :     int         numCols = list_length(distinctList);
    6400         396 :     int         keyno = 0;
    6401             :     AttrNumber *dupColIdx;
    6402             :     Oid        *dupOperators;
    6403             :     Oid        *dupCollations;
    6404             :     ListCell   *slitem;
    6405             : 
    6406         396 :     plan->targetlist = lefttree->targetlist;
    6407         396 :     plan->qual = NIL;
    6408         396 :     plan->lefttree = lefttree;
    6409         396 :     plan->righttree = NULL;
    6410             : 
    6411             :     /*
    6412             :      * convert SortGroupClause list into arrays of attr indexes and equality
    6413             :      * operators, as wanted by executor
    6414             :      */
    6415         396 :     dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
    6416         396 :     dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
    6417         396 :     dupCollations = (Oid *) palloc(sizeof(Oid) * numCols);
    6418             : 
    6419         848 :     foreach(slitem, distinctList)
    6420             :     {
    6421         452 :         SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
    6422         452 :         TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
    6423             : 
    6424         452 :         dupColIdx[keyno] = tle->resno;
    6425         452 :         dupOperators[keyno] = sortcl->eqop;
    6426         452 :         dupCollations[keyno] = exprCollation((Node *) tle->expr);
    6427             :         Assert(OidIsValid(dupOperators[keyno]));
    6428         452 :         keyno++;
    6429             :     }
    6430             : 
    6431         396 :     node->cmd = cmd;
    6432         396 :     node->strategy = strategy;
    6433         396 :     node->numCols = numCols;
    6434         396 :     node->dupColIdx = dupColIdx;
    6435         396 :     node->dupOperators = dupOperators;
    6436         396 :     node->dupCollations = dupCollations;
    6437         396 :     node->flagColIdx = flagColIdx;
    6438         396 :     node->firstFlag = firstFlag;
    6439         396 :     node->numGroups = numGroups;
    6440             : 
    6441         396 :     return node;
    6442             : }
    6443             : 
    6444             : /*
    6445             :  * make_lockrows
    6446             :  *    Build a LockRows plan node
    6447             :  */
    6448             : static LockRows *
    6449        4718 : make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
    6450             : {
    6451        4718 :     LockRows   *node = makeNode(LockRows);
    6452        4718 :     Plan       *plan = &node->plan;
    6453             : 
    6454        4718 :     plan->targetlist = lefttree->targetlist;
    6455        4718 :     plan->qual = NIL;
    6456        4718 :     plan->lefttree = lefttree;
    6457        4718 :     plan->righttree = NULL;
    6458             : 
    6459        4718 :     node->rowMarks = rowMarks;
    6460        4718 :     node->epqParam = epqParam;
    6461             : 
    6462        4718 :     return node;
    6463             : }
    6464             : 
    6465             : /*
    6466             :  * make_limit
    6467             :  *    Build a Limit plan node
    6468             :  */
    6469             : Limit *
    6470        2912 : make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount)
    6471             : {
    6472        2912 :     Limit      *node = makeNode(Limit);
    6473        2912 :     Plan       *plan = &node->plan;
    6474             : 
    6475        2912 :     plan->targetlist = lefttree->targetlist;
    6476        2912 :     plan->qual = NIL;
    6477        2912 :     plan->lefttree = lefttree;
    6478        2912 :     plan->righttree = NULL;
    6479             : 
    6480        2912 :     node->limitOffset = limitOffset;
    6481        2912 :     node->limitCount = limitCount;
    6482             : 
    6483        2912 :     return node;
    6484             : }
    6485             : 
    6486             : /*
    6487             :  * make_result
    6488             :  *    Build a Result plan node
    6489             :  */
    6490             : static Result *
    6491      112076 : make_result(List *tlist,
    6492             :             Node *resconstantqual,
    6493             :             Plan *subplan)
    6494             : {
    6495      112076 :     Result     *node = makeNode(Result);
    6496      112076 :     Plan       *plan = &node->plan;
    6497             : 
    6498      112076 :     plan->targetlist = tlist;
    6499      112076 :     plan->qual = NIL;
    6500      112076 :     plan->lefttree = subplan;
    6501      112076 :     plan->righttree = NULL;
    6502      112076 :     node->resconstantqual = resconstantqual;
    6503             : 
    6504      112076 :     return node;
    6505             : }
    6506             : 
    6507             : /*
    6508             :  * make_project_set
    6509             :  *    Build a ProjectSet plan node
    6510             :  */
    6511             : static ProjectSet *
    6512        3578 : make_project_set(List *tlist,
    6513             :                  Plan *subplan)
    6514             : {
    6515        3578 :     ProjectSet *node = makeNode(ProjectSet);
    6516        3578 :     Plan       *plan = &node->plan;
    6517             : 
    6518        3578 :     plan->targetlist = tlist;
    6519        3578 :     plan->qual = NIL;
    6520        3578 :     plan->lefttree = subplan;
    6521        3578 :     plan->righttree = NULL;
    6522             : 
    6523        3578 :     return node;
    6524             : }
    6525             : 
    6526             : /*
    6527             :  * make_modifytable
    6528             :  *    Build a ModifyTable plan node
    6529             :  */
    6530             : static ModifyTable *
    6531       69436 : make_modifytable(PlannerInfo *root,
    6532             :                  CmdType operation, bool canSetTag,
    6533             :                  Index nominalRelation, Index rootRelation,
    6534             :                  bool partColsUpdated,
    6535             :                  List *resultRelations, List *subplans, List *subroots,
    6536             :                  List *withCheckOptionLists, List *returningLists,
    6537             :                  List *rowMarks, OnConflictExpr *onconflict, int epqParam)
    6538             : {
    6539       69436 :     ModifyTable *node = makeNode(ModifyTable);
    6540             :     List       *fdw_private_list;
    6541             :     Bitmapset  *direct_modify_plans;
    6542             :     ListCell   *lc;
    6543             :     ListCell   *lc2;
    6544             :     int         i;
    6545             : 
    6546             :     Assert(list_length(resultRelations) == list_length(subplans));
    6547             :     Assert(list_length(resultRelations) == list_length(subroots));
    6548             :     Assert(withCheckOptionLists == NIL ||
    6549             :            list_length(resultRelations) == list_length(withCheckOptionLists));
    6550             :     Assert(returningLists == NIL ||
    6551             :            list_length(resultRelations) == list_length(returningLists));
    6552             : 
    6553       69436 :     node->plan.lefttree = NULL;
    6554       69436 :     node->plan.righttree = NULL;
    6555       69436 :     node->plan.qual = NIL;
    6556             :     /* setrefs.c will fill in the targetlist, if needed */
    6557       69436 :     node->plan.targetlist = NIL;
    6558             : 
    6559       69436 :     node->operation = operation;
    6560       69436 :     node->canSetTag = canSetTag;
    6561       69436 :     node->nominalRelation = nominalRelation;
    6562       69436 :     node->rootRelation = rootRelation;
    6563       69436 :     node->partColsUpdated = partColsUpdated;
    6564       69436 :     node->resultRelations = resultRelations;
    6565       69436 :     node->resultRelIndex = -1;   /* will be set correctly in setrefs.c */
    6566       69436 :     node->rootResultRelIndex = -1;   /* will be set correctly in setrefs.c */
    6567       69436 :     node->plans = subplans;
    6568       69436 :     if (!onconflict)
    6569             :     {
    6570       68488 :         node->onConflictAction = ONCONFLICT_NONE;
    6571       68488 :         node->onConflictSet = NIL;
    6572       68488 :         node->onConflictWhere = NULL;
    6573       68488 :         node->arbiterIndexes = NIL;
    6574       68488 :         node->exclRelRTI = 0;
    6575       68488 :         node->exclRelTlist = NIL;
    6576             :     }
    6577             :     else
    6578             :     {
    6579         948 :         node->onConflictAction = onconflict->action;
    6580         948 :         node->onConflictSet = onconflict->onConflictSet;
    6581         948 :         node->onConflictWhere = onconflict->onConflictWhere;
    6582             : 
    6583             :         /*
    6584             :          * If a set of unique index inference elements was provided (an
    6585             :          * INSERT...ON CONFLICT "inference specification"), then infer
    6586             :          * appropriate unique indexes (or throw an error if none are
    6587             :          * available).
    6588             :          */
    6589         948 :         node->arbiterIndexes = infer_arbiter_indexes(root);
    6590             : 
    6591         828 :         node->exclRelRTI = onconflict->exclRelIndex;
    6592         828 :         node->exclRelTlist = onconflict->exclRelTlist;
    6593             :     }
    6594       69316 :     node->withCheckOptionLists = withCheckOptionLists;
    6595       69316 :     node->returningLists = returningLists;
    6596       69316 :     node->rowMarks = rowMarks;
    6597       69316 :     node->epqParam = epqParam;
    6598             : 
    6599             :     /*
    6600             :      * For each result relation that is a foreign table, allow the FDW to
    6601             :      * construct private plan data, and accumulate it all into a list.
    6602             :      */
    6603       69316 :     fdw_private_list = NIL;
    6604       69316 :     direct_modify_plans = NULL;
    6605       69316 :     i = 0;
    6606      139672 :     forboth(lc, resultRelations, lc2, subroots)
    6607             :     {
    6608       70356 :         Index       rti = lfirst_int(lc);
    6609       70356 :         PlannerInfo *subroot = lfirst_node(PlannerInfo, lc2);
    6610             :         FdwRoutine *fdwroutine;
    6611             :         List       *fdw_private;
    6612             :         bool        direct_modify;
    6613             : 
    6614             :         /*
    6615             :          * If possible, we want to get the FdwRoutine from our RelOptInfo for
    6616             :          * the table.  But sometimes we don't have a RelOptInfo and must get
    6617             :          * it the hard way.  (In INSERT, the target relation is not scanned,
    6618             :          * so it's not a baserel; and there are also corner cases for
    6619             :          * updatable views where the target rel isn't a baserel.)
    6620             :          */
    6621      140680 :         if (rti < subroot->simple_rel_array_size &&
    6622       70324 :             subroot->simple_rel_array[rti] != NULL)
    6623       12850 :         {
    6624       12850 :             RelOptInfo *resultRel = subroot->simple_rel_array[rti];
    6625             : 
    6626       12850 :             fdwroutine = resultRel->fdwroutine;
    6627             :         }
    6628             :         else
    6629             :         {
    6630       57506 :             RangeTblEntry *rte = planner_rt_fetch(rti, subroot);
    6631             : 
    6632             :             Assert(rte->rtekind == RTE_RELATION);
    6633       57506 :             if (rte->relkind == RELKIND_FOREIGN_TABLE)
    6634          96 :                 fdwroutine = GetFdwRoutineByRelId(rte->relid);
    6635             :             else
    6636       57410 :                 fdwroutine = NULL;
    6637             :         }
    6638             : 
    6639             :         /*
    6640             :          * Try to modify the foreign table directly if (1) the FDW provides
    6641             :          * callback functions needed for that and (2) there are no local
    6642             :          * structures that need to be run for each modified row: row-level
    6643             :          * triggers on the foreign table, stored generated columns, WITH CHECK
    6644             :          * OPTIONs from parent views.
    6645             :          */
    6646       70356 :         direct_modify = false;
    6647       70694 :         if (fdwroutine != NULL &&
    6648         666 :             fdwroutine->PlanDirectModify != NULL &&
    6649         656 :             fdwroutine->BeginDirectModify != NULL &&
    6650         656 :             fdwroutine->IterateDirectModify != NULL &&
    6651         656 :             fdwroutine->EndDirectModify != NULL &&
    6652         304 :             withCheckOptionLists == NIL &&
    6653         546 :             !has_row_triggers(subroot, rti, operation) &&
    6654         242 :             !has_stored_generated_columns(subroot, rti))
    6655         238 :             direct_modify = fdwroutine->PlanDirectModify(subroot, node, rti, i);
    6656       70356 :         if (direct_modify)
    6657         152 :             direct_modify_plans = bms_add_member(direct_modify_plans, i);
    6658             : 
    6659       70356 :         if (!direct_modify &&
    6660         186 :             fdwroutine != NULL &&
    6661         186 :             fdwroutine->PlanForeignModify != NULL)
    6662         176 :             fdw_private = fdwroutine->PlanForeignModify(subroot, node, rti, i);
    6663             :         else
    6664       70180 :             fdw_private = NIL;
    6665       70356 :         fdw_private_list = lappend(fdw_private_list, fdw_private);
    6666       70356 :         i++;
    6667             :     }
    6668       69316 :     node->fdwPrivLists = fdw_private_list;
    6669       69316 :     node->fdwDirectModifyPlans = direct_modify_plans;
    6670             : 
    6671       69316 :     return node;
    6672             : }
    6673             : 
    6674             : /*
    6675             :  * is_projection_capable_path
    6676             :  *      Check whether a given Path node is able to do projection.
    6677             :  */
    6678             : bool
    6679      441626 : is_projection_capable_path(Path *path)
    6680             : {
    6681             :     /* Most plan types can project, so just list the ones that can't */
    6682      441626 :     switch (path->pathtype)
    6683             :     {
    6684             :         case T_Hash:
    6685             :         case T_Material:
    6686             :         case T_Sort:
    6687             :         case T_Unique:
    6688             :         case T_SetOp:
    6689             :         case T_LockRows:
    6690             :         case T_Limit:
    6691             :         case T_ModifyTable:
    6692             :         case T_MergeAppend:
    6693             :         case T_RecursiveUnion:
    6694       18318 :             return false;
    6695             :         case T_Append:
    6696             : 
    6697             :             /*
    6698             :              * Append can't project, but if an AppendPath is being used to
    6699             :              * represent a dummy path, what will actually be generated is a
    6700             :              * Result which can project.
    6701             :              */
    6702        1116 :             return IS_DUMMY_APPEND(path);
    6703             :         case T_ProjectSet:
    6704             : 
    6705             :             /*
    6706             :              * Although ProjectSet certainly projects, say "no" because we
    6707             :              * don't want the planner to randomly replace its tlist with
    6708             :              * something else; the SRFs have to stay at top level.  This might
    6709             :              * get relaxed later.
    6710             :              */
    6711        1860 :             return false;
    6712             :         default:
    6713      420332 :             break;
    6714             :     }
    6715      420332 :     return true;
    6716             : }
    6717             : 
    6718             : /*
    6719             :  * is_projection_capable_plan
    6720             :  *      Check whether a given Plan node is able to do projection.
    6721             :  */
    6722             : bool
    6723         222 : is_projection_capable_plan(Plan *plan)
    6724             : {
    6725             :     /* Most plan types can project, so just list the ones that can't */
    6726         222 :     switch (nodeTag(plan))
    6727             :     {
    6728             :         case T_Hash:
    6729             :         case T_Material:
    6730             :         case T_Sort:
    6731             :         case T_Unique:
    6732             :         case T_SetOp:
    6733             :         case T_LockRows:
    6734             :         case T_Limit:
    6735             :         case T_ModifyTable:
    6736             :         case T_Append:
    6737             :         case T_MergeAppend:
    6738             :         case T_RecursiveUnion:
    6739          22 :             return false;
    6740             :         case T_ProjectSet:
    6741             : 
    6742             :             /*
    6743             :              * Although ProjectSet certainly projects, say "no" because we
    6744             :              * don't want the planner to randomly replace its tlist with
    6745             :              * something else; the SRFs have to stay at top level.  This might
    6746             :              * get relaxed later.
    6747             :              */
    6748           0 :             return false;
    6749             :         default:
    6750         200 :             break;
    6751             :     }
    6752         200 :     return true;
    6753             : }

Generated by: LCOV version 1.13