LCOV - code coverage report
Current view: top level - src/backend/optimizer/plan - subselect.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 842 891 94.5 %
Date: 2020-05-25 05:06:35 Functions: 34 34 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * subselect.c
       4             :  *    Planning routines for subselects.
       5             :  *
       6             :  * This module deals with SubLinks and CTEs, but not subquery RTEs (i.e.,
       7             :  * not sub-SELECT-in-FROM cases).
       8             :  *
       9             :  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
      10             :  * Portions Copyright (c) 1994, Regents of the University of California
      11             :  *
      12             :  * IDENTIFICATION
      13             :  *    src/backend/optimizer/plan/subselect.c
      14             :  *
      15             :  *-------------------------------------------------------------------------
      16             :  */
      17             : #include "postgres.h"
      18             : 
      19             : #include "access/htup_details.h"
      20             : #include "catalog/pg_operator.h"
      21             : #include "catalog/pg_type.h"
      22             : #include "executor/executor.h"
      23             : #include "miscadmin.h"
      24             : #include "nodes/makefuncs.h"
      25             : #include "nodes/nodeFuncs.h"
      26             : #include "optimizer/clauses.h"
      27             : #include "optimizer/cost.h"
      28             : #include "optimizer/optimizer.h"
      29             : #include "optimizer/paramassign.h"
      30             : #include "optimizer/pathnode.h"
      31             : #include "optimizer/planmain.h"
      32             : #include "optimizer/planner.h"
      33             : #include "optimizer/prep.h"
      34             : #include "optimizer/subselect.h"
      35             : #include "parser/parse_relation.h"
      36             : #include "rewrite/rewriteManip.h"
      37             : #include "utils/builtins.h"
      38             : #include "utils/lsyscache.h"
      39             : #include "utils/syscache.h"
      40             : 
      41             : 
      42             : typedef struct convert_testexpr_context
      43             : {
      44             :     PlannerInfo *root;
      45             :     List       *subst_nodes;    /* Nodes to substitute for Params */
      46             : } convert_testexpr_context;
      47             : 
      48             : typedef struct process_sublinks_context
      49             : {
      50             :     PlannerInfo *root;
      51             :     bool        isTopQual;
      52             : } process_sublinks_context;
      53             : 
      54             : typedef struct finalize_primnode_context
      55             : {
      56             :     PlannerInfo *root;
      57             :     Bitmapset  *paramids;       /* Non-local PARAM_EXEC paramids found */
      58             : } finalize_primnode_context;
      59             : 
      60             : typedef struct inline_cte_walker_context
      61             : {
      62             :     const char *ctename;        /* name and relative level of target CTE */
      63             :     int         levelsup;
      64             :     int         refcount;       /* number of remaining references */
      65             :     Query      *ctequery;       /* query to substitute */
      66             : } inline_cte_walker_context;
      67             : 
      68             : 
      69             : static Node *build_subplan(PlannerInfo *root, Plan *plan, PlannerInfo *subroot,
      70             :                            List *plan_params,
      71             :                            SubLinkType subLinkType, int subLinkId,
      72             :                            Node *testexpr, bool adjust_testexpr,
      73             :                            bool unknownEqFalse);
      74             : static List *generate_subquery_params(PlannerInfo *root, List *tlist,
      75             :                                       List **paramIds);
      76             : static List *generate_subquery_vars(PlannerInfo *root, List *tlist,
      77             :                                     Index varno);
      78             : static Node *convert_testexpr(PlannerInfo *root,
      79             :                               Node *testexpr,
      80             :                               List *subst_nodes);
      81             : static Node *convert_testexpr_mutator(Node *node,
      82             :                                       convert_testexpr_context *context);
      83             : static bool subplan_is_hashable(Plan *plan);
      84             : static bool testexpr_is_hashable(Node *testexpr);
      85             : static bool hash_ok_operator(OpExpr *expr);
      86             : static bool contain_dml(Node *node);
      87             : static bool contain_dml_walker(Node *node, void *context);
      88             : static bool contain_outer_selfref(Node *node);
      89             : static bool contain_outer_selfref_walker(Node *node, Index *depth);
      90             : static void inline_cte(PlannerInfo *root, CommonTableExpr *cte);
      91             : static bool inline_cte_walker(Node *node, inline_cte_walker_context *context);
      92             : static bool simplify_EXISTS_query(PlannerInfo *root, Query *query);
      93             : static Query *convert_EXISTS_to_ANY(PlannerInfo *root, Query *subselect,
      94             :                                     Node **testexpr, List **paramIds);
      95             : static Node *replace_correlation_vars_mutator(Node *node, PlannerInfo *root);
      96             : static Node *process_sublinks_mutator(Node *node,
      97             :                                       process_sublinks_context *context);
      98             : static Bitmapset *finalize_plan(PlannerInfo *root,
      99             :                                 Plan *plan,
     100             :                                 int gather_param,
     101             :                                 Bitmapset *valid_params,
     102             :                                 Bitmapset *scan_params);
     103             : static bool finalize_primnode(Node *node, finalize_primnode_context *context);
     104             : static bool finalize_agg_primnode(Node *node, finalize_primnode_context *context);
     105             : 
     106             : 
     107             : /*
     108             :  * Get the datatype/typmod/collation of the first column of the plan's output.
     109             :  *
     110             :  * This information is stored for ARRAY_SUBLINK execution and for
     111             :  * exprType()/exprTypmod()/exprCollation(), which have no way to get at the
     112             :  * plan associated with a SubPlan node.  We really only need the info for
     113             :  * EXPR_SUBLINK and ARRAY_SUBLINK subplans, but for consistency we save it
     114             :  * always.
     115             :  */
     116             : static void
     117       48860 : get_first_col_type(Plan *plan, Oid *coltype, int32 *coltypmod,
     118             :                    Oid *colcollation)
     119             : {
     120             :     /* In cases such as EXISTS, tlist might be empty; arbitrarily use VOID */
     121       48860 :     if (plan->targetlist)
     122             :     {
     123       47032 :         TargetEntry *tent = linitial_node(TargetEntry, plan->targetlist);
     124             : 
     125       47032 :         if (!tent->resjunk)
     126             :         {
     127       47032 :             *coltype = exprType((Node *) tent->expr);
     128       47032 :             *coltypmod = exprTypmod((Node *) tent->expr);
     129       47032 :             *colcollation = exprCollation((Node *) tent->expr);
     130       47032 :             return;
     131             :         }
     132             :     }
     133        1828 :     *coltype = VOIDOID;
     134        1828 :     *coltypmod = -1;
     135        1828 :     *colcollation = InvalidOid;
     136             : }
     137             : 
     138             : /*
     139             :  * Convert a SubLink (as created by the parser) into a SubPlan.
     140             :  *
     141             :  * We are given the SubLink's contained query, type, ID, and testexpr.  We are
     142             :  * also told if this expression appears at top level of a WHERE/HAVING qual.
     143             :  *
     144             :  * Note: we assume that the testexpr has been AND/OR flattened (actually,
     145             :  * it's been through eval_const_expressions), but not converted to
     146             :  * implicit-AND form; and any SubLinks in it should already have been
     147             :  * converted to SubPlans.  The subquery is as yet untouched, however.
     148             :  *
     149             :  * The result is whatever we need to substitute in place of the SubLink node
     150             :  * in the executable expression.  If we're going to do the subplan as a
     151             :  * regular subplan, this will be the constructed SubPlan node.  If we're going
     152             :  * to do the subplan as an InitPlan, the SubPlan node instead goes into
     153             :  * root->init_plans, and what we return here is an expression tree
     154             :  * representing the InitPlan's result: usually just a Param node representing
     155             :  * a single scalar result, but possibly a row comparison tree containing
     156             :  * multiple Param nodes, or for a MULTIEXPR subquery a simple NULL constant
     157             :  * (since the real output Params are elsewhere in the tree, and the MULTIEXPR
     158             :  * subquery itself is in a resjunk tlist entry whose value is uninteresting).
     159             :  */
     160             : static Node *
     161       46622 : make_subplan(PlannerInfo *root, Query *orig_subquery,
     162             :              SubLinkType subLinkType, int subLinkId,
     163             :              Node *testexpr, bool isTopQual)
     164             : {
     165             :     Query      *subquery;
     166       46622 :     bool        simple_exists = false;
     167             :     double      tuple_fraction;
     168             :     PlannerInfo *subroot;
     169             :     RelOptInfo *final_rel;
     170             :     Path       *best_path;
     171             :     Plan       *plan;
     172             :     List       *plan_params;
     173             :     Node       *result;
     174             : 
     175             :     /*
     176             :      * Copy the source Query node.  This is a quick and dirty kluge to resolve
     177             :      * the fact that the parser can generate trees with multiple links to the
     178             :      * same sub-Query node, but the planner wants to scribble on the Query.
     179             :      * Try to clean this up when we do querytree redesign...
     180             :      */
     181       46622 :     subquery = copyObject(orig_subquery);
     182             : 
     183             :     /*
     184             :      * If it's an EXISTS subplan, we might be able to simplify it.
     185             :      */
     186       46622 :     if (subLinkType == EXISTS_SUBLINK)
     187        1676 :         simple_exists = simplify_EXISTS_query(root, subquery);
     188             : 
     189             :     /*
     190             :      * For an EXISTS subplan, tell lower-level planner to expect that only the
     191             :      * first tuple will be retrieved.  For ALL and ANY subplans, we will be
     192             :      * able to stop evaluating if the test condition fails or matches, so very
     193             :      * often not all the tuples will be retrieved; for lack of a better idea,
     194             :      * specify 50% retrieval.  For EXPR, MULTIEXPR, and ROWCOMPARE subplans,
     195             :      * use default behavior (we're only expecting one row out, anyway).
     196             :      *
     197             :      * NOTE: if you change these numbers, also change cost_subplan() in
     198             :      * path/costsize.c.
     199             :      *
     200             :      * XXX If an ANY subplan is uncorrelated, build_subplan may decide to hash
     201             :      * its output.  In that case it would've been better to specify full
     202             :      * retrieval.  At present, however, we can only check hashability after
     203             :      * we've made the subplan :-(.  (Determining whether it'll fit in work_mem
     204             :      * is the really hard part.)  Therefore, we don't want to be too
     205             :      * optimistic about the percentage of tuples retrieved, for fear of
     206             :      * selecting a plan that's bad for the materialization case.
     207             :      */
     208       46622 :     if (subLinkType == EXISTS_SUBLINK)
     209        1676 :         tuple_fraction = 1.0;   /* just like a LIMIT 1 */
     210       44946 :     else if (subLinkType == ALL_SUBLINK ||
     211             :              subLinkType == ANY_SUBLINK)
     212         214 :         tuple_fraction = 0.5;   /* 50% */
     213             :     else
     214       44732 :         tuple_fraction = 0.0;   /* default behavior */
     215             : 
     216             :     /* plan_params should not be in use in current query level */
     217             :     Assert(root->plan_params == NIL);
     218             : 
     219             :     /* Generate Paths for the subquery */
     220       46622 :     subroot = subquery_planner(root->glob, subquery,
     221             :                                root,
     222             :                                false, tuple_fraction);
     223             : 
     224             :     /* Isolate the params needed by this specific subplan */
     225       46622 :     plan_params = root->plan_params;
     226       46622 :     root->plan_params = NIL;
     227             : 
     228             :     /*
     229             :      * Select best Path and turn it into a Plan.  At least for now, there
     230             :      * seems no reason to postpone doing that.
     231             :      */
     232       46622 :     final_rel = fetch_upper_rel(subroot, UPPERREL_FINAL, NULL);
     233       46622 :     best_path = get_cheapest_fractional_path(final_rel, tuple_fraction);
     234             : 
     235       46622 :     plan = create_plan(subroot, best_path);
     236             : 
     237             :     /* And convert to SubPlan or InitPlan format. */
     238       46622 :     result = build_subplan(root, plan, subroot, plan_params,
     239             :                            subLinkType, subLinkId,
     240             :                            testexpr, true, isTopQual);
     241             : 
     242             :     /*
     243             :      * If it's a correlated EXISTS with an unimportant targetlist, we might be
     244             :      * able to transform it to the equivalent of an IN and then implement it
     245             :      * by hashing.  We don't have enough information yet to tell which way is
     246             :      * likely to be better (it depends on the expected number of executions of
     247             :      * the EXISTS qual, and we are much too early in planning the outer query
     248             :      * to be able to guess that).  So we generate both plans, if possible, and
     249             :      * leave it to the executor to decide which to use.
     250             :      */
     251       46622 :     if (simple_exists && IsA(result, SubPlan))
     252             :     {
     253             :         Node       *newtestexpr;
     254             :         List       *paramIds;
     255             : 
     256             :         /* Make a second copy of the original subquery */
     257        1500 :         subquery = copyObject(orig_subquery);
     258             :         /* and re-simplify */
     259        1500 :         simple_exists = simplify_EXISTS_query(root, subquery);
     260             :         Assert(simple_exists);
     261             :         /* See if it can be converted to an ANY query */
     262        1500 :         subquery = convert_EXISTS_to_ANY(root, subquery,
     263             :                                          &newtestexpr, &paramIds);
     264        1500 :         if (subquery)
     265             :         {
     266             :             /* Generate Paths for the ANY subquery; we'll need all rows */
     267        1174 :             subroot = subquery_planner(root->glob, subquery,
     268             :                                        root,
     269             :                                        false, 0.0);
     270             : 
     271             :             /* Isolate the params needed by this specific subplan */
     272        1174 :             plan_params = root->plan_params;
     273        1174 :             root->plan_params = NIL;
     274             : 
     275             :             /* Select best Path and turn it into a Plan */
     276        1174 :             final_rel = fetch_upper_rel(subroot, UPPERREL_FINAL, NULL);
     277        1174 :             best_path = final_rel->cheapest_total_path;
     278             : 
     279        1174 :             plan = create_plan(subroot, best_path);
     280             : 
     281             :             /* Now we can check if it'll fit in work_mem */
     282             :             /* XXX can we check this at the Path stage? */
     283        1174 :             if (subplan_is_hashable(plan))
     284             :             {
     285             :                 SubPlan    *hashplan;
     286             :                 AlternativeSubPlan *asplan;
     287             : 
     288             :                 /* OK, convert to SubPlan format. */
     289        1174 :                 hashplan = castNode(SubPlan,
     290             :                                     build_subplan(root, plan, subroot,
     291             :                                                   plan_params,
     292             :                                                   ANY_SUBLINK, 0,
     293             :                                                   newtestexpr,
     294             :                                                   false, true));
     295             :                 /* Check we got what we expected */
     296             :                 Assert(hashplan->parParam == NIL);
     297             :                 Assert(hashplan->useHashTable);
     298             :                 /* build_subplan won't have filled in paramIds */
     299        1174 :                 hashplan->paramIds = paramIds;
     300             : 
     301             :                 /* Leave it to the executor to decide which plan to use */
     302        1174 :                 asplan = makeNode(AlternativeSubPlan);
     303        1174 :                 asplan->subplans = list_make2(result, hashplan);
     304        1174 :                 result = (Node *) asplan;
     305             :             }
     306             :         }
     307             :     }
     308             : 
     309       46622 :     return result;
     310             : }
     311             : 
     312             : /*
     313             :  * Build a SubPlan node given the raw inputs --- subroutine for make_subplan
     314             :  *
     315             :  * Returns either the SubPlan, or a replacement expression if we decide to
     316             :  * make it an InitPlan, as explained in the comments for make_subplan.
     317             :  */
     318             : static Node *
     319       47796 : build_subplan(PlannerInfo *root, Plan *plan, PlannerInfo *subroot,
     320             :               List *plan_params,
     321             :               SubLinkType subLinkType, int subLinkId,
     322             :               Node *testexpr, bool adjust_testexpr,
     323             :               bool unknownEqFalse)
     324             : {
     325             :     Node       *result;
     326             :     SubPlan    *splan;
     327             :     bool        isInitPlan;
     328             :     ListCell   *lc;
     329             : 
     330             :     /*
     331             :      * Initialize the SubPlan node.  Note plan_id, plan_name, and cost fields
     332             :      * are set further down.
     333             :      */
     334       47796 :     splan = makeNode(SubPlan);
     335       47796 :     splan->subLinkType = subLinkType;
     336       47796 :     splan->testexpr = NULL;
     337       47796 :     splan->paramIds = NIL;
     338       47796 :     get_first_col_type(plan, &splan->firstColType, &splan->firstColTypmod,
     339             :                        &splan->firstColCollation);
     340       47796 :     splan->useHashTable = false;
     341       47796 :     splan->unknownEqFalse = unknownEqFalse;
     342       47796 :     splan->parallel_safe = plan->parallel_safe;
     343       47796 :     splan->setParam = NIL;
     344       47796 :     splan->parParam = NIL;
     345       47796 :     splan->args = NIL;
     346             : 
     347             :     /*
     348             :      * Make parParam and args lists of param IDs and expressions that current
     349             :      * query level will pass to this child plan.
     350             :      */
     351      103440 :     foreach(lc, plan_params)
     352             :     {
     353       55644 :         PlannerParamItem *pitem = (PlannerParamItem *) lfirst(lc);
     354       55644 :         Node       *arg = pitem->item;
     355             : 
     356             :         /*
     357             :          * The Var, PlaceHolderVar, or Aggref has already been adjusted to
     358             :          * have the correct varlevelsup, phlevelsup, or agglevelsup.
     359             :          *
     360             :          * If it's a PlaceHolderVar or Aggref, its arguments might contain
     361             :          * SubLinks, which have not yet been processed (see the comments for
     362             :          * SS_replace_correlation_vars).  Do that now.
     363             :          */
     364       55644 :         if (IsA(arg, PlaceHolderVar) ||
     365       55636 :             IsA(arg, Aggref))
     366          40 :             arg = SS_process_sublinks(root, arg, false);
     367             : 
     368       55644 :         splan->parParam = lappend_int(splan->parParam, pitem->paramId);
     369       55644 :         splan->args = lappend(splan->args, arg);
     370             :     }
     371             : 
     372             :     /*
     373             :      * Un-correlated or undirect correlated plans of EXISTS, EXPR, ARRAY,
     374             :      * ROWCOMPARE, or MULTIEXPR types can be used as initPlans.  For EXISTS,
     375             :      * EXPR, or ARRAY, we return a Param referring to the result of evaluating
     376             :      * the initPlan.  For ROWCOMPARE, we must modify the testexpr tree to
     377             :      * contain PARAM_EXEC Params instead of the PARAM_SUBLINK Params emitted
     378             :      * by the parser, and then return that tree.  For MULTIEXPR, we return a
     379             :      * null constant: the resjunk targetlist item containing the SubLink does
     380             :      * not need to return anything useful, since the referencing Params are
     381             :      * elsewhere.
     382             :      */
     383       47796 :     if (splan->parParam == NIL && subLinkType == EXISTS_SUBLINK)
     384         158 :     {
     385             :         Param      *prm;
     386             : 
     387             :         Assert(testexpr == NULL);
     388         158 :         prm = generate_new_exec_param(root, BOOLOID, -1, InvalidOid);
     389         158 :         splan->setParam = list_make1_int(prm->paramid);
     390         158 :         isInitPlan = true;
     391         158 :         result = (Node *) prm;
     392             :     }
     393       47638 :     else if (splan->parParam == NIL && subLinkType == EXPR_SUBLINK)
     394        8986 :     {
     395        8986 :         TargetEntry *te = linitial(plan->targetlist);
     396             :         Param      *prm;
     397             : 
     398             :         Assert(!te->resjunk);
     399             :         Assert(testexpr == NULL);
     400        8986 :         prm = generate_new_exec_param(root,
     401        8986 :                                       exprType((Node *) te->expr),
     402        8986 :                                       exprTypmod((Node *) te->expr),
     403        8986 :                                       exprCollation((Node *) te->expr));
     404        8986 :         splan->setParam = list_make1_int(prm->paramid);
     405        8986 :         isInitPlan = true;
     406        8986 :         result = (Node *) prm;
     407             :     }
     408       38652 :     else if (splan->parParam == NIL && subLinkType == ARRAY_SUBLINK)
     409          58 :     {
     410          58 :         TargetEntry *te = linitial(plan->targetlist);
     411             :         Oid         arraytype;
     412             :         Param      *prm;
     413             : 
     414             :         Assert(!te->resjunk);
     415             :         Assert(testexpr == NULL);
     416          58 :         arraytype = get_promoted_array_type(exprType((Node *) te->expr));
     417          58 :         if (!OidIsValid(arraytype))
     418           0 :             elog(ERROR, "could not find array type for datatype %s",
     419             :                  format_type_be(exprType((Node *) te->expr)));
     420          58 :         prm = generate_new_exec_param(root,
     421             :                                       arraytype,
     422          58 :                                       exprTypmod((Node *) te->expr),
     423          58 :                                       exprCollation((Node *) te->expr));
     424          58 :         splan->setParam = list_make1_int(prm->paramid);
     425          58 :         isInitPlan = true;
     426          58 :         result = (Node *) prm;
     427             :     }
     428       38594 :     else if (splan->parParam == NIL && subLinkType == ROWCOMPARE_SUBLINK)
     429           0 :     {
     430             :         /* Adjust the Params */
     431             :         List       *params;
     432             : 
     433             :         Assert(testexpr != NULL);
     434           0 :         params = generate_subquery_params(root,
     435             :                                           plan->targetlist,
     436             :                                           &splan->paramIds);
     437           0 :         result = convert_testexpr(root,
     438             :                                   testexpr,
     439             :                                   params);
     440           0 :         splan->setParam = list_copy(splan->paramIds);
     441           0 :         isInitPlan = true;
     442             : 
     443             :         /*
     444             :          * The executable expression is returned to become part of the outer
     445             :          * plan's expression tree; it is not kept in the initplan node.
     446             :          */
     447             :     }
     448       38594 :     else if (subLinkType == MULTIEXPR_SUBLINK)
     449             :     {
     450             :         /*
     451             :          * Whether it's an initplan or not, it needs to set a PARAM_EXEC Param
     452             :          * for each output column.
     453             :          */
     454             :         List       *params;
     455             : 
     456             :         Assert(testexpr == NULL);
     457          58 :         params = generate_subquery_params(root,
     458             :                                           plan->targetlist,
     459             :                                           &splan->setParam);
     460             : 
     461             :         /*
     462             :          * Save the list of replacement Params in the n'th cell of
     463             :          * root->multiexpr_params; setrefs.c will use it to replace
     464             :          * PARAM_MULTIEXPR Params.
     465             :          */
     466         116 :         while (list_length(root->multiexpr_params) < subLinkId)
     467          58 :             root->multiexpr_params = lappend(root->multiexpr_params, NIL);
     468          58 :         lc = list_nth_cell(root->multiexpr_params, subLinkId - 1);
     469             :         Assert(lfirst(lc) == NIL);
     470          58 :         lfirst(lc) = params;
     471             : 
     472             :         /* It can be an initplan if there are no parParams. */
     473          58 :         if (splan->parParam == NIL)
     474             :         {
     475          16 :             isInitPlan = true;
     476          16 :             result = (Node *) makeNullConst(RECORDOID, -1, InvalidOid);
     477             :         }
     478             :         else
     479             :         {
     480          42 :             isInitPlan = false;
     481          42 :             result = (Node *) splan;
     482             :         }
     483             :     }
     484             :     else
     485             :     {
     486             :         /*
     487             :          * Adjust the Params in the testexpr, unless caller said it's not
     488             :          * needed.
     489             :          */
     490       38536 :         if (testexpr && adjust_testexpr)
     491         214 :         {
     492             :             List       *params;
     493             : 
     494         214 :             params = generate_subquery_params(root,
     495             :                                               plan->targetlist,
     496             :                                               &splan->paramIds);
     497         214 :             splan->testexpr = convert_testexpr(root,
     498             :                                                testexpr,
     499             :                                                params);
     500             :         }
     501             :         else
     502       38322 :             splan->testexpr = testexpr;
     503             : 
     504             :         /*
     505             :          * We can't convert subplans of ALL_SUBLINK or ANY_SUBLINK types to
     506             :          * initPlans, even when they are uncorrelated or undirect correlated,
     507             :          * because we need to scan the output of the subplan for each outer
     508             :          * tuple.  But if it's a not-direct-correlated IN (= ANY) test, we
     509             :          * might be able to use a hashtable to avoid comparing all the tuples.
     510             :          */
     511       38536 :         if (subLinkType == ANY_SUBLINK &&
     512        2704 :             splan->parParam == NIL &&
     513        2656 :             subplan_is_hashable(plan) &&
     514        1328 :             testexpr_is_hashable(splan->testexpr))
     515        1328 :             splan->useHashTable = true;
     516             : 
     517             :         /*
     518             :          * Otherwise, we have the option to tack a Material node onto the top
     519             :          * of the subplan, to reduce the cost of reading it repeatedly.  This
     520             :          * is pointless for a direct-correlated subplan, since we'd have to
     521             :          * recompute its results each time anyway.  For uncorrelated/undirect
     522             :          * correlated subplans, we add Material unless the subplan's top plan
     523             :          * node would materialize its output anyway.  Also, if enable_material
     524             :          * is false, then the user does not want us to materialize anything
     525             :          * unnecessarily, so we don't.
     526             :          */
     527       37208 :         else if (splan->parParam == NIL && enable_material &&
     528          12 :                  !ExecMaterializesOutput(nodeTag(plan)))
     529          12 :             plan = materialize_finished_plan(plan);
     530             : 
     531       38536 :         result = (Node *) splan;
     532       38536 :         isInitPlan = false;
     533             :     }
     534             : 
     535             :     /*
     536             :      * Add the subplan and its PlannerInfo to the global lists.
     537             :      */
     538       47796 :     root->glob->subplans = lappend(root->glob->subplans, plan);
     539       47796 :     root->glob->subroots = lappend(root->glob->subroots, subroot);
     540       47796 :     splan->plan_id = list_length(root->glob->subplans);
     541             : 
     542       47796 :     if (isInitPlan)
     543        9218 :         root->init_plans = lappend(root->init_plans, splan);
     544             : 
     545             :     /*
     546             :      * A parameterless subplan (not initplan) should be prepared to handle
     547             :      * REWIND efficiently.  If it has direct parameters then there's no point
     548             :      * since it'll be reset on each scan anyway; and if it's an initplan then
     549             :      * there's no point since it won't get re-run without parameter changes
     550             :      * anyway.  The input of a hashed subplan doesn't need REWIND either.
     551             :      */
     552       47796 :     if (splan->parParam == NIL && !isInitPlan && !splan->useHashTable)
     553          12 :         root->glob->rewindPlanIDs = bms_add_member(root->glob->rewindPlanIDs,
     554             :                                                    splan->plan_id);
     555             : 
     556             :     /* Label the subplan for EXPLAIN purposes */
     557       47796 :     splan->plan_name = palloc(32 + 12 * list_length(splan->setParam));
     558       47796 :     sprintf(splan->plan_name, "%s %d",
     559             :             isInitPlan ? "InitPlan" : "SubPlan",
     560             :             splan->plan_id);
     561       47796 :     if (splan->setParam)
     562             :     {
     563        9260 :         char       *ptr = splan->plan_name + strlen(splan->plan_name);
     564             : 
     565        9260 :         ptr += sprintf(ptr, " (returns ");
     566       18576 :         foreach(lc, splan->setParam)
     567             :         {
     568        9316 :             ptr += sprintf(ptr, "$%d%s",
     569             :                            lfirst_int(lc),
     570        9316 :                            lnext(splan->setParam, lc) ? "," : ")");
     571             :         }
     572             :     }
     573             : 
     574             :     /* Lastly, fill in the cost estimates for use later */
     575       47796 :     cost_subplan(root, splan, plan);
     576             : 
     577       47796 :     return result;
     578             : }
     579             : 
     580             : /*
     581             :  * generate_subquery_params: build a list of Params representing the output
     582             :  * columns of a sublink's sub-select, given the sub-select's targetlist.
     583             :  *
     584             :  * We also return an integer list of the paramids of the Params.
     585             :  */
     586             : static List *
     587         272 : generate_subquery_params(PlannerInfo *root, List *tlist, List **paramIds)
     588             : {
     589             :     List       *result;
     590             :     List       *ids;
     591             :     ListCell   *lc;
     592             : 
     593         272 :     result = ids = NIL;
     594         636 :     foreach(lc, tlist)
     595             :     {
     596         364 :         TargetEntry *tent = (TargetEntry *) lfirst(lc);
     597             :         Param      *param;
     598             : 
     599         364 :         if (tent->resjunk)
     600           4 :             continue;
     601             : 
     602         360 :         param = generate_new_exec_param(root,
     603         360 :                                         exprType((Node *) tent->expr),
     604         360 :                                         exprTypmod((Node *) tent->expr),
     605         360 :                                         exprCollation((Node *) tent->expr));
     606         360 :         result = lappend(result, param);
     607         360 :         ids = lappend_int(ids, param->paramid);
     608             :     }
     609             : 
     610         272 :     *paramIds = ids;
     611         272 :     return result;
     612             : }
     613             : 
     614             : /*
     615             :  * generate_subquery_vars: build a list of Vars representing the output
     616             :  * columns of a sublink's sub-select, given the sub-select's targetlist.
     617             :  * The Vars have the specified varno (RTE index).
     618             :  */
     619             : static List *
     620         584 : generate_subquery_vars(PlannerInfo *root, List *tlist, Index varno)
     621             : {
     622             :     List       *result;
     623             :     ListCell   *lc;
     624             : 
     625         584 :     result = NIL;
     626        1196 :     foreach(lc, tlist)
     627             :     {
     628         612 :         TargetEntry *tent = (TargetEntry *) lfirst(lc);
     629             :         Var        *var;
     630             : 
     631         612 :         if (tent->resjunk)
     632           0 :             continue;
     633             : 
     634         612 :         var = makeVarFromTargetEntry(varno, tent);
     635         612 :         result = lappend(result, var);
     636             :     }
     637             : 
     638         584 :     return result;
     639             : }
     640             : 
     641             : /*
     642             :  * convert_testexpr: convert the testexpr given by the parser into
     643             :  * actually executable form.  This entails replacing PARAM_SUBLINK Params
     644             :  * with Params or Vars representing the results of the sub-select.  The
     645             :  * nodes to be substituted are passed in as the List result from
     646             :  * generate_subquery_params or generate_subquery_vars.
     647             :  */
     648             : static Node *
     649         798 : convert_testexpr(PlannerInfo *root,
     650             :                  Node *testexpr,
     651             :                  List *subst_nodes)
     652             : {
     653             :     convert_testexpr_context context;
     654             : 
     655         798 :     context.root = root;
     656         798 :     context.subst_nodes = subst_nodes;
     657         798 :     return convert_testexpr_mutator(testexpr, &context);
     658             : }
     659             : 
     660             : static Node *
     661        3984 : convert_testexpr_mutator(Node *node,
     662             :                          convert_testexpr_context *context)
     663             : {
     664        3984 :     if (node == NULL)
     665          12 :         return NULL;
     666        3972 :     if (IsA(node, Param))
     667             :     {
     668         858 :         Param      *param = (Param *) node;
     669             : 
     670         858 :         if (param->paramkind == PARAM_SUBLINK)
     671             :         {
     672        1716 :             if (param->paramid <= 0 ||
     673         858 :                 param->paramid > list_length(context->subst_nodes))
     674           0 :                 elog(ERROR, "unexpected PARAM_SUBLINK ID: %d", param->paramid);
     675             : 
     676             :             /*
     677             :              * We copy the list item to avoid having doubly-linked
     678             :              * substructure in the modified parse tree.  This is probably
     679             :              * unnecessary when it's a Param, but be safe.
     680             :              */
     681         858 :             return (Node *) copyObject(list_nth(context->subst_nodes,
     682             :                                                 param->paramid - 1));
     683             :         }
     684             :     }
     685        3114 :     if (IsA(node, SubLink))
     686             :     {
     687             :         /*
     688             :          * If we come across a nested SubLink, it is neither necessary nor
     689             :          * correct to recurse into it: any PARAM_SUBLINKs we might find inside
     690             :          * belong to the inner SubLink not the outer. So just return it as-is.
     691             :          *
     692             :          * This reasoning depends on the assumption that nothing will pull
     693             :          * subexpressions into or out of the testexpr field of a SubLink, at
     694             :          * least not without replacing PARAM_SUBLINKs first.  If we did want
     695             :          * to do that we'd need to rethink the parser-output representation
     696             :          * altogether, since currently PARAM_SUBLINKs are only unique per
     697             :          * SubLink not globally across the query.  The whole point of
     698             :          * replacing them with Vars or PARAM_EXEC nodes is to make them
     699             :          * globally unique before they escape from the SubLink's testexpr.
     700             :          *
     701             :          * Note: this can't happen when called during SS_process_sublinks,
     702             :          * because that recursively processes inner SubLinks first.  It can
     703             :          * happen when called from convert_ANY_sublink_to_join, though.
     704             :          */
     705           8 :         return node;
     706             :     }
     707        3106 :     return expression_tree_mutator(node,
     708             :                                    convert_testexpr_mutator,
     709             :                                    (void *) context);
     710             : }
     711             : 
     712             : /*
     713             :  * subplan_is_hashable: can we implement an ANY subplan by hashing?
     714             :  */
     715             : static bool
     716        2502 : subplan_is_hashable(Plan *plan)
     717             : {
     718             :     double      subquery_size;
     719             : 
     720             :     /*
     721             :      * The estimated size of the subquery result must fit in work_mem. (Note:
     722             :      * we use heap tuple overhead here even though the tuples will actually be
     723             :      * stored as MinimalTuples; this provides some fudge factor for hashtable
     724             :      * overhead.)
     725             :      */
     726        5004 :     subquery_size = plan->plan_rows *
     727        2502 :         (MAXALIGN(plan->plan_width) + MAXALIGN(SizeofHeapTupleHeader));
     728        2502 :     if (subquery_size > work_mem * 1024L)
     729           0 :         return false;
     730             : 
     731        2502 :     return true;
     732             : }
     733             : 
     734             : /*
     735             :  * testexpr_is_hashable: is an ANY SubLink's test expression hashable?
     736             :  */
     737             : static bool
     738        1328 : testexpr_is_hashable(Node *testexpr)
     739             : {
     740             :     /*
     741             :      * The testexpr must be a single OpExpr, or an AND-clause containing only
     742             :      * OpExprs.
     743             :      *
     744             :      * The combining operators must be hashable and strict. The need for
     745             :      * hashability is obvious, since we want to use hashing. Without
     746             :      * strictness, behavior in the presence of nulls is too unpredictable.  We
     747             :      * actually must assume even more than plain strictness: they can't yield
     748             :      * NULL for non-null inputs, either (see nodeSubplan.c).  However, hash
     749             :      * indexes and hash joins assume that too.
     750             :      */
     751        1328 :     if (testexpr && IsA(testexpr, OpExpr))
     752             :     {
     753         800 :         if (hash_ok_operator((OpExpr *) testexpr))
     754         800 :             return true;
     755             :     }
     756         528 :     else if (is_andclause(testexpr))
     757             :     {
     758             :         ListCell   *l;
     759             : 
     760        1584 :         foreach(l, ((BoolExpr *) testexpr)->args)
     761             :         {
     762        1056 :             Node       *andarg = (Node *) lfirst(l);
     763             : 
     764        1056 :             if (!IsA(andarg, OpExpr))
     765           0 :                 return false;
     766        1056 :             if (!hash_ok_operator((OpExpr *) andarg))
     767           0 :                 return false;
     768             :         }
     769         528 :         return true;
     770             :     }
     771             : 
     772           0 :     return false;
     773             : }
     774             : 
     775             : /*
     776             :  * Check expression is hashable + strict
     777             :  *
     778             :  * We could use op_hashjoinable() and op_strict(), but do it like this to
     779             :  * avoid a redundant cache lookup.
     780             :  */
     781             : static bool
     782        6312 : hash_ok_operator(OpExpr *expr)
     783             : {
     784        6312 :     Oid         opid = expr->opno;
     785             : 
     786             :     /* quick out if not a binary operator */
     787        6312 :     if (list_length(expr->args) != 2)
     788           0 :         return false;
     789        6312 :     if (opid == ARRAY_EQ_OP)
     790             :     {
     791             :         /* array_eq is strict, but must check input type to ensure hashable */
     792             :         /* XXX record_eq will need same treatment when it becomes hashable */
     793           0 :         Node       *leftarg = linitial(expr->args);
     794             : 
     795           0 :         return op_hashjoinable(opid, exprType(leftarg));
     796             :     }
     797             :     else
     798             :     {
     799             :         /* else must look up the operator properties */
     800             :         HeapTuple   tup;
     801             :         Form_pg_operator optup;
     802             : 
     803        6312 :         tup = SearchSysCache1(OPEROID, ObjectIdGetDatum(opid));
     804        6312 :         if (!HeapTupleIsValid(tup))
     805           0 :             elog(ERROR, "cache lookup failed for operator %u", opid);
     806        6312 :         optup = (Form_pg_operator) GETSTRUCT(tup);
     807        6312 :         if (!optup->oprcanhash || !func_strict(optup->oprcode))
     808             :         {
     809         324 :             ReleaseSysCache(tup);
     810         324 :             return false;
     811             :         }
     812        5988 :         ReleaseSysCache(tup);
     813        5988 :         return true;
     814             :     }
     815             : }
     816             : 
     817             : 
     818             : /*
     819             :  * SS_process_ctes: process a query's WITH list
     820             :  *
     821             :  * Consider each CTE in the WITH list and either ignore it (if it's an
     822             :  * unreferenced SELECT), "inline" it to create a regular sub-SELECT-in-FROM,
     823             :  * or convert it to an initplan.
     824             :  *
     825             :  * A side effect is to fill in root->cte_plan_ids with a list that
     826             :  * parallels root->parse->cteList and provides the subplan ID for
     827             :  * each CTE's initplan, or a dummy ID (-1) if we didn't make an initplan.
     828             :  */
     829             : void
     830        1222 : SS_process_ctes(PlannerInfo *root)
     831             : {
     832             :     ListCell   *lc;
     833             : 
     834             :     Assert(root->cte_plan_ids == NIL);
     835             : 
     836        2612 :     foreach(lc, root->parse->cteList)
     837             :     {
     838        1390 :         CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
     839        1390 :         CmdType     cmdType = ((Query *) cte->ctequery)->commandType;
     840             :         Query      *subquery;
     841             :         PlannerInfo *subroot;
     842             :         RelOptInfo *final_rel;
     843             :         Path       *best_path;
     844             :         Plan       *plan;
     845             :         SubPlan    *splan;
     846             :         int         paramid;
     847             : 
     848             :         /*
     849             :          * Ignore SELECT CTEs that are not actually referenced anywhere.
     850             :          */
     851        1390 :         if (cte->cterefcount == 0 && cmdType == CMD_SELECT)
     852             :         {
     853             :             /* Make a dummy entry in cte_plan_ids */
     854           8 :             root->cte_plan_ids = lappend_int(root->cte_plan_ids, -1);
     855           8 :             continue;
     856             :         }
     857             : 
     858             :         /*
     859             :          * Consider inlining the CTE (creating RTE_SUBQUERY RTE(s)) instead of
     860             :          * implementing it as a separately-planned CTE.
     861             :          *
     862             :          * We cannot inline if any of these conditions hold:
     863             :          *
     864             :          * 1. The user said not to (the CTEMaterializeAlways option).
     865             :          *
     866             :          * 2. The CTE is recursive.
     867             :          *
     868             :          * 3. The CTE has side-effects; this includes either not being a plain
     869             :          * SELECT, or containing volatile functions.  Inlining might change
     870             :          * the side-effects, which would be bad.
     871             :          *
     872             :          * 4. The CTE is multiply-referenced and contains a self-reference to
     873             :          * a recursive CTE outside itself.  Inlining would result in multiple
     874             :          * recursive self-references, which we don't support.
     875             :          *
     876             :          * Otherwise, we have an option whether to inline or not.  That should
     877             :          * always be a win if there's just a single reference, but if the CTE
     878             :          * is multiply-referenced then it's unclear: inlining adds duplicate
     879             :          * computations, but the ability to absorb restrictions from the outer
     880             :          * query level could outweigh that.  We do not have nearly enough
     881             :          * information at this point to tell whether that's true, so we let
     882             :          * the user express a preference.  Our default behavior is to inline
     883             :          * only singly-referenced CTEs, but a CTE marked CTEMaterializeNever
     884             :          * will be inlined even if multiply referenced.
     885             :          *
     886             :          * Note: we check for volatile functions last, because that's more
     887             :          * expensive than the other tests needed.
     888             :          */
     889        1382 :         if ((cte->ctematerialized == CTEMaterializeNever ||
     890        1362 :              (cte->ctematerialized == CTEMaterializeDefault &&
     891        1290 :               cte->cterefcount == 1)) &&
     892        1146 :             !cte->cterecursive &&
     893         696 :             cmdType == CMD_SELECT &&
     894         696 :             !contain_dml(cte->ctequery) &&
     895         690 :             (cte->cterefcount <= 1 ||
     896          12 :              !contain_outer_selfref(cte->ctequery)) &&
     897         682 :             !contain_volatile_functions(cte->ctequery))
     898             :         {
     899         678 :             inline_cte(root, cte);
     900             :             /* Make a dummy entry in cte_plan_ids */
     901         678 :             root->cte_plan_ids = lappend_int(root->cte_plan_ids, -1);
     902         678 :             continue;
     903             :         }
     904             : 
     905             :         /*
     906             :          * Copy the source Query node.  Probably not necessary, but let's keep
     907             :          * this similar to make_subplan.
     908             :          */
     909         704 :         subquery = (Query *) copyObject(cte->ctequery);
     910             : 
     911             :         /* plan_params should not be in use in current query level */
     912             :         Assert(root->plan_params == NIL);
     913             : 
     914             :         /*
     915             :          * Generate Paths for the CTE query.  Always plan for full retrieval
     916             :          * --- we don't have enough info to predict otherwise.
     917             :          */
     918         704 :         subroot = subquery_planner(root->glob, subquery,
     919             :                                    root,
     920         704 :                                    cte->cterecursive, 0.0);
     921             : 
     922             :         /*
     923             :          * Since the current query level doesn't yet contain any RTEs, it
     924             :          * should not be possible for the CTE to have requested parameters of
     925             :          * this level.
     926             :          */
     927         704 :         if (root->plan_params)
     928           0 :             elog(ERROR, "unexpected outer reference in CTE query");
     929             : 
     930             :         /*
     931             :          * Select best Path and turn it into a Plan.  At least for now, there
     932             :          * seems no reason to postpone doing that.
     933             :          */
     934         704 :         final_rel = fetch_upper_rel(subroot, UPPERREL_FINAL, NULL);
     935         704 :         best_path = final_rel->cheapest_total_path;
     936             : 
     937         704 :         plan = create_plan(subroot, best_path);
     938             : 
     939             :         /*
     940             :          * Make a SubPlan node for it.  This is just enough unlike
     941             :          * build_subplan that we can't share code.
     942             :          *
     943             :          * Note plan_id, plan_name, and cost fields are set further down.
     944             :          */
     945         704 :         splan = makeNode(SubPlan);
     946         704 :         splan->subLinkType = CTE_SUBLINK;
     947         704 :         splan->testexpr = NULL;
     948         704 :         splan->paramIds = NIL;
     949         704 :         get_first_col_type(plan, &splan->firstColType, &splan->firstColTypmod,
     950             :                            &splan->firstColCollation);
     951         704 :         splan->useHashTable = false;
     952         704 :         splan->unknownEqFalse = false;
     953             : 
     954             :         /*
     955             :          * CTE scans are not considered for parallelism (cf
     956             :          * set_rel_consider_parallel), and even if they were, initPlans aren't
     957             :          * parallel-safe.
     958             :          */
     959         704 :         splan->parallel_safe = false;
     960         704 :         splan->setParam = NIL;
     961         704 :         splan->parParam = NIL;
     962         704 :         splan->args = NIL;
     963             : 
     964             :         /*
     965             :          * The node can't have any inputs (since it's an initplan), so the
     966             :          * parParam and args lists remain empty.  (It could contain references
     967             :          * to earlier CTEs' output param IDs, but CTE outputs are not
     968             :          * propagated via the args list.)
     969             :          */
     970             : 
     971             :         /*
     972             :          * Assign a param ID to represent the CTE's output.  No ordinary
     973             :          * "evaluation" of this param slot ever happens, but we use the param
     974             :          * ID for setParam/chgParam signaling just as if the CTE plan were
     975             :          * returning a simple scalar output.  (Also, the executor abuses the
     976             :          * ParamExecData slot for this param ID for communication among
     977             :          * multiple CteScan nodes that might be scanning this CTE.)
     978             :          */
     979         704 :         paramid = assign_special_exec_param(root);
     980         704 :         splan->setParam = list_make1_int(paramid);
     981             : 
     982             :         /*
     983             :          * Add the subplan and its PlannerInfo to the global lists.
     984             :          */
     985         704 :         root->glob->subplans = lappend(root->glob->subplans, plan);
     986         704 :         root->glob->subroots = lappend(root->glob->subroots, subroot);
     987         704 :         splan->plan_id = list_length(root->glob->subplans);
     988             : 
     989         704 :         root->init_plans = lappend(root->init_plans, splan);
     990             : 
     991         704 :         root->cte_plan_ids = lappend_int(root->cte_plan_ids, splan->plan_id);
     992             : 
     993             :         /* Label the subplan for EXPLAIN purposes */
     994         704 :         splan->plan_name = psprintf("CTE %s", cte->ctename);
     995             : 
     996             :         /* Lastly, fill in the cost estimates for use later */
     997         704 :         cost_subplan(root, splan, plan);
     998             :     }
     999        1222 : }
    1000             : 
    1001             : /*
    1002             :  * contain_dml: is any subquery not a plain SELECT?
    1003             :  *
    1004             :  * We reject SELECT FOR UPDATE/SHARE as well as INSERT etc.
    1005             :  */
    1006             : static bool
    1007         696 : contain_dml(Node *node)
    1008             : {
    1009         696 :     return contain_dml_walker(node, NULL);
    1010             : }
    1011             : 
    1012             : static bool
    1013       45400 : contain_dml_walker(Node *node, void *context)
    1014             : {
    1015       45400 :     if (node == NULL)
    1016       11070 :         return false;
    1017       34330 :     if (IsA(node, Query))
    1018             :     {
    1019         924 :         Query      *query = (Query *) node;
    1020             : 
    1021         924 :         if (query->commandType != CMD_SELECT ||
    1022         924 :             query->rowMarks != NIL)
    1023           6 :             return true;
    1024             : 
    1025         918 :         return query_tree_walker(query, contain_dml_walker, context, 0);
    1026             :     }
    1027       33406 :     return expression_tree_walker(node, contain_dml_walker, context);
    1028             : }
    1029             : 
    1030             : /*
    1031             :  * contain_outer_selfref: is there an external recursive self-reference?
    1032             :  */
    1033             : static bool
    1034          12 : contain_outer_selfref(Node *node)
    1035             : {
    1036          12 :     Index       depth = 0;
    1037             : 
    1038             :     /*
    1039             :      * We should be starting with a Query, so that depth will be 1 while
    1040             :      * examining its immediate contents.
    1041             :      */
    1042             :     Assert(IsA(node, Query));
    1043             : 
    1044          12 :     return contain_outer_selfref_walker(node, &depth);
    1045             : }
    1046             : 
    1047             : static bool
    1048         300 : contain_outer_selfref_walker(Node *node, Index *depth)
    1049             : {
    1050         300 :     if (node == NULL)
    1051         156 :         return false;
    1052         144 :     if (IsA(node, RangeTblEntry))
    1053             :     {
    1054          16 :         RangeTblEntry *rte = (RangeTblEntry *) node;
    1055             : 
    1056             :         /*
    1057             :          * Check for a self-reference to a CTE that's above the Query that our
    1058             :          * search started at.
    1059             :          */
    1060          16 :         if (rte->rtekind == RTE_CTE &&
    1061           8 :             rte->self_reference &&
    1062           8 :             rte->ctelevelsup >= *depth)
    1063           8 :             return true;
    1064           8 :         return false;           /* allow range_table_walker to continue */
    1065             :     }
    1066         128 :     if (IsA(node, Query))
    1067             :     {
    1068             :         /* Recurse into subquery, tracking nesting depth properly */
    1069          16 :         Query      *query = (Query *) node;
    1070             :         bool        result;
    1071             : 
    1072          16 :         (*depth)++;
    1073             : 
    1074          16 :         result = query_tree_walker(query, contain_outer_selfref_walker,
    1075             :                                    (void *) depth, QTW_EXAMINE_RTES_BEFORE);
    1076             : 
    1077          16 :         (*depth)--;
    1078             : 
    1079          16 :         return result;
    1080             :     }
    1081         112 :     return expression_tree_walker(node, contain_outer_selfref_walker,
    1082             :                                   (void *) depth);
    1083             : }
    1084             : 
    1085             : /*
    1086             :  * inline_cte: convert RTE_CTE references to given CTE into RTE_SUBQUERYs
    1087             :  */
    1088             : static void
    1089         678 : inline_cte(PlannerInfo *root, CommonTableExpr *cte)
    1090             : {
    1091             :     struct inline_cte_walker_context context;
    1092             : 
    1093         678 :     context.ctename = cte->ctename;
    1094             :     /* Start at levelsup = -1 because we'll immediately increment it */
    1095         678 :     context.levelsup = -1;
    1096         678 :     context.refcount = cte->cterefcount;
    1097         678 :     context.ctequery = castNode(Query, cte->ctequery);
    1098             : 
    1099         678 :     (void) inline_cte_walker((Node *) root->parse, &context);
    1100             : 
    1101             :     /* Assert we replaced all references */
    1102             :     Assert(context.refcount == 0);
    1103         678 : }
    1104             : 
    1105             : static bool
    1106      123100 : inline_cte_walker(Node *node, inline_cte_walker_context *context)
    1107             : {
    1108      123100 :     if (node == NULL)
    1109       34984 :         return false;
    1110       88116 :     if (IsA(node, Query))
    1111             :     {
    1112        3158 :         Query      *query = (Query *) node;
    1113             : 
    1114        3158 :         context->levelsup++;
    1115             : 
    1116             :         /*
    1117             :          * Visit the query's RTE nodes after their contents; otherwise
    1118             :          * query_tree_walker would descend into the newly inlined CTE query,
    1119             :          * which we don't want.
    1120             :          */
    1121        3158 :         (void) query_tree_walker(query, inline_cte_walker, context,
    1122             :                                  QTW_EXAMINE_RTES_AFTER);
    1123             : 
    1124        3158 :         context->levelsup--;
    1125             : 
    1126        3158 :         return false;
    1127             :     }
    1128       84958 :     else if (IsA(node, RangeTblEntry))
    1129             :     {
    1130        4948 :         RangeTblEntry *rte = (RangeTblEntry *) node;
    1131             : 
    1132        4948 :         if (rte->rtekind == RTE_CTE &&
    1133        1094 :             strcmp(rte->ctename, context->ctename) == 0 &&
    1134         682 :             rte->ctelevelsup == context->levelsup)
    1135             :         {
    1136             :             /*
    1137             :              * Found a reference to replace.  Generate a copy of the CTE query
    1138             :              * with appropriate level adjustment for outer references (e.g.,
    1139             :              * to other CTEs).
    1140             :              */
    1141         682 :             Query      *newquery = copyObject(context->ctequery);
    1142             : 
    1143         682 :             if (context->levelsup > 0)
    1144         518 :                 IncrementVarSublevelsUp((Node *) newquery, context->levelsup, 1);
    1145             : 
    1146             :             /*
    1147             :              * Convert the RTE_CTE RTE into a RTE_SUBQUERY.
    1148             :              *
    1149             :              * Historically, a FOR UPDATE clause has been treated as extending
    1150             :              * into views and subqueries, but not into CTEs.  We preserve this
    1151             :              * distinction by not trying to push rowmarks into the new
    1152             :              * subquery.
    1153             :              */
    1154         682 :             rte->rtekind = RTE_SUBQUERY;
    1155         682 :             rte->subquery = newquery;
    1156         682 :             rte->security_barrier = false;
    1157             : 
    1158             :             /* Zero out CTE-specific fields */
    1159         682 :             rte->ctename = NULL;
    1160         682 :             rte->ctelevelsup = 0;
    1161         682 :             rte->self_reference = false;
    1162         682 :             rte->coltypes = NIL;
    1163         682 :             rte->coltypmods = NIL;
    1164         682 :             rte->colcollations = NIL;
    1165             : 
    1166             :             /* Count the number of replacements we've done */
    1167         682 :             context->refcount--;
    1168             :         }
    1169             : 
    1170        4948 :         return false;
    1171             :     }
    1172             : 
    1173       80010 :     return expression_tree_walker(node, inline_cte_walker, context);
    1174             : }
    1175             : 
    1176             : 
    1177             : /*
    1178             :  * convert_ANY_sublink_to_join: try to convert an ANY SubLink to a join
    1179             :  *
    1180             :  * The caller has found an ANY SubLink at the top level of one of the query's
    1181             :  * qual clauses, but has not checked the properties of the SubLink further.
    1182             :  * Decide whether it is appropriate to process this SubLink in join style.
    1183             :  * If so, form a JoinExpr and return it.  Return NULL if the SubLink cannot
    1184             :  * be converted to a join.
    1185             :  *
    1186             :  * The only non-obvious input parameter is available_rels: this is the set
    1187             :  * of query rels that can safely be referenced in the sublink expression.
    1188             :  * (We must restrict this to avoid changing the semantics when a sublink
    1189             :  * is present in an outer join's ON qual.)  The conversion must fail if
    1190             :  * the converted qual would reference any but these parent-query relids.
    1191             :  *
    1192             :  * On success, the returned JoinExpr has larg = NULL and rarg = the jointree
    1193             :  * item representing the pulled-up subquery.  The caller must set larg to
    1194             :  * represent the relation(s) on the lefthand side of the new join, and insert
    1195             :  * the JoinExpr into the upper query's jointree at an appropriate place
    1196             :  * (typically, where the lefthand relation(s) had been).  Note that the
    1197             :  * passed-in SubLink must also be removed from its original position in the
    1198             :  * query quals, since the quals of the returned JoinExpr replace it.
    1199             :  * (Notionally, we replace the SubLink with a constant TRUE, then elide the
    1200             :  * redundant constant from the qual.)
    1201             :  *
    1202             :  * On success, the caller is also responsible for recursively applying
    1203             :  * pull_up_sublinks processing to the rarg and quals of the returned JoinExpr.
    1204             :  * (On failure, there is no need to do anything, since pull_up_sublinks will
    1205             :  * be applied when we recursively plan the sub-select.)
    1206             :  *
    1207             :  * Side effects of a successful conversion include adding the SubLink's
    1208             :  * subselect to the query's rangetable, so that it can be referenced in
    1209             :  * the JoinExpr's rarg.
    1210             :  */
    1211             : JoinExpr *
    1212         632 : convert_ANY_sublink_to_join(PlannerInfo *root, SubLink *sublink,
    1213             :                             Relids available_rels)
    1214             : {
    1215             :     JoinExpr   *result;
    1216         632 :     Query      *parse = root->parse;
    1217         632 :     Query      *subselect = (Query *) sublink->subselect;
    1218             :     Relids      upper_varnos;
    1219             :     int         rtindex;
    1220             :     ParseNamespaceItem *nsitem;
    1221             :     RangeTblEntry *rte;
    1222             :     RangeTblRef *rtr;
    1223             :     List       *subquery_vars;
    1224             :     Node       *quals;
    1225             :     ParseState *pstate;
    1226             : 
    1227             :     Assert(sublink->subLinkType == ANY_SUBLINK);
    1228             : 
    1229             :     /*
    1230             :      * The sub-select must not refer to any Vars of the parent query. (Vars of
    1231             :      * higher levels should be okay, though.)
    1232             :      */
    1233         632 :     if (contain_vars_of_level((Node *) subselect, 1))
    1234          40 :         return NULL;
    1235             : 
    1236             :     /*
    1237             :      * The test expression must contain some Vars of the parent query, else
    1238             :      * it's not gonna be a join.  (Note that it won't have Vars referring to
    1239             :      * the subquery, rather Params.)
    1240             :      */
    1241         592 :     upper_varnos = pull_varnos(sublink->testexpr);
    1242         592 :     if (bms_is_empty(upper_varnos))
    1243           8 :         return NULL;
    1244             : 
    1245             :     /*
    1246             :      * However, it can't refer to anything outside available_rels.
    1247             :      */
    1248         584 :     if (!bms_is_subset(upper_varnos, available_rels))
    1249           0 :         return NULL;
    1250             : 
    1251             :     /*
    1252             :      * The combining operators and left-hand expressions mustn't be volatile.
    1253             :      */
    1254         584 :     if (contain_volatile_functions(sublink->testexpr))
    1255           0 :         return NULL;
    1256             : 
    1257             :     /* Create a dummy ParseState for addRangeTableEntryForSubquery */
    1258         584 :     pstate = make_parsestate(NULL);
    1259             : 
    1260             :     /*
    1261             :      * Okay, pull up the sub-select into upper range table.
    1262             :      *
    1263             :      * We rely here on the assumption that the outer query has no references
    1264             :      * to the inner (necessarily true, other than the Vars that we build
    1265             :      * below). Therefore this is a lot easier than what pull_up_subqueries has
    1266             :      * to go through.
    1267             :      */
    1268         584 :     nsitem = addRangeTableEntryForSubquery(pstate,
    1269             :                                            subselect,
    1270             :                                            makeAlias("ANY_subquery", NIL),
    1271             :                                            false,
    1272             :                                            false);
    1273         584 :     rte = nsitem->p_rte;
    1274         584 :     parse->rtable = lappend(parse->rtable, rte);
    1275         584 :     rtindex = list_length(parse->rtable);
    1276             : 
    1277             :     /*
    1278             :      * Form a RangeTblRef for the pulled-up sub-select.
    1279             :      */
    1280         584 :     rtr = makeNode(RangeTblRef);
    1281         584 :     rtr->rtindex = rtindex;
    1282             : 
    1283             :     /*
    1284             :      * Build a list of Vars representing the subselect outputs.
    1285             :      */
    1286         584 :     subquery_vars = generate_subquery_vars(root,
    1287             :                                            subselect->targetList,
    1288             :                                            rtindex);
    1289             : 
    1290             :     /*
    1291             :      * Build the new join's qual expression, replacing Params with these Vars.
    1292             :      */
    1293         584 :     quals = convert_testexpr(root, sublink->testexpr, subquery_vars);
    1294             : 
    1295             :     /*
    1296             :      * And finally, build the JoinExpr node.
    1297             :      */
    1298         584 :     result = makeNode(JoinExpr);
    1299         584 :     result->jointype = JOIN_SEMI;
    1300         584 :     result->isNatural = false;
    1301         584 :     result->larg = NULL;     /* caller must fill this in */
    1302         584 :     result->rarg = (Node *) rtr;
    1303         584 :     result->usingClause = NIL;
    1304         584 :     result->quals = quals;
    1305         584 :     result->alias = NULL;
    1306         584 :     result->rtindex = 0;     /* we don't need an RTE for it */
    1307             : 
    1308         584 :     return result;
    1309             : }
    1310             : 
    1311             : /*
    1312             :  * convert_EXISTS_sublink_to_join: try to convert an EXISTS SubLink to a join
    1313             :  *
    1314             :  * The API of this function is identical to convert_ANY_sublink_to_join's,
    1315             :  * except that we also support the case where the caller has found NOT EXISTS,
    1316             :  * so we need an additional input parameter "under_not".
    1317             :  */
    1318             : JoinExpr *
    1319        8034 : convert_EXISTS_sublink_to_join(PlannerInfo *root, SubLink *sublink,
    1320             :                                bool under_not, Relids available_rels)
    1321             : {
    1322             :     JoinExpr   *result;
    1323        8034 :     Query      *parse = root->parse;
    1324        8034 :     Query      *subselect = (Query *) sublink->subselect;
    1325             :     Node       *whereClause;
    1326             :     int         rtoffset;
    1327             :     int         varno;
    1328             :     Relids      clause_varnos;
    1329             :     Relids      upper_varnos;
    1330             : 
    1331             :     Assert(sublink->subLinkType == EXISTS_SUBLINK);
    1332             : 
    1333             :     /*
    1334             :      * Can't flatten if it contains WITH.  (We could arrange to pull up the
    1335             :      * WITH into the parent query's cteList, but that risks changing the
    1336             :      * semantics, since a WITH ought to be executed once per associated query
    1337             :      * call.)  Note that convert_ANY_sublink_to_join doesn't have to reject
    1338             :      * this case, since it just produces a subquery RTE that doesn't have to
    1339             :      * get flattened into the parent query.
    1340             :      */
    1341        8034 :     if (subselect->cteList)
    1342           0 :         return NULL;
    1343             : 
    1344             :     /*
    1345             :      * Copy the subquery so we can modify it safely (see comments in
    1346             :      * make_subplan).
    1347             :      */
    1348        8034 :     subselect = copyObject(subselect);
    1349             : 
    1350             :     /*
    1351             :      * See if the subquery can be simplified based on the knowledge that it's
    1352             :      * being used in EXISTS().  If we aren't able to get rid of its
    1353             :      * targetlist, we have to fail, because the pullup operation leaves us
    1354             :      * with noplace to evaluate the targetlist.
    1355             :      */
    1356        8034 :     if (!simplify_EXISTS_query(root, subselect))
    1357          18 :         return NULL;
    1358             : 
    1359             :     /*
    1360             :      * Separate out the WHERE clause.  (We could theoretically also remove
    1361             :      * top-level plain JOIN/ON clauses, but it's probably not worth the
    1362             :      * trouble.)
    1363             :      */
    1364        8016 :     whereClause = subselect->jointree->quals;
    1365        8016 :     subselect->jointree->quals = NULL;
    1366             : 
    1367             :     /*
    1368             :      * The rest of the sub-select must not refer to any Vars of the parent
    1369             :      * query.  (Vars of higher levels should be okay, though.)
    1370             :      */
    1371        8016 :     if (contain_vars_of_level((Node *) subselect, 1))
    1372           4 :         return NULL;
    1373             : 
    1374             :     /*
    1375             :      * On the other hand, the WHERE clause must contain some Vars of the
    1376             :      * parent query, else it's not gonna be a join.
    1377             :      */
    1378        8012 :     if (!contain_vars_of_level(whereClause, 1))
    1379          40 :         return NULL;
    1380             : 
    1381             :     /*
    1382             :      * We don't risk optimizing if the WHERE clause is volatile, either.
    1383             :      */
    1384        7972 :     if (contain_volatile_functions(whereClause))
    1385           0 :         return NULL;
    1386             : 
    1387             :     /*
    1388             :      * The subquery must have a nonempty jointree, but we can make it so.
    1389             :      */
    1390        7972 :     replace_empty_jointree(subselect);
    1391             : 
    1392             :     /*
    1393             :      * Prepare to pull up the sub-select into top range table.
    1394             :      *
    1395             :      * We rely here on the assumption that the outer query has no references
    1396             :      * to the inner (necessarily true). Therefore this is a lot easier than
    1397             :      * what pull_up_subqueries has to go through.
    1398             :      *
    1399             :      * In fact, it's even easier than what convert_ANY_sublink_to_join has to
    1400             :      * do.  The machinations of simplify_EXISTS_query ensured that there is
    1401             :      * nothing interesting in the subquery except an rtable and jointree, and
    1402             :      * even the jointree FromExpr no longer has quals.  So we can just append
    1403             :      * the rtable to our own and use the FromExpr in our jointree. But first,
    1404             :      * adjust all level-zero varnos in the subquery to account for the rtable
    1405             :      * merger.
    1406             :      */
    1407        7972 :     rtoffset = list_length(parse->rtable);
    1408        7972 :     OffsetVarNodes((Node *) subselect, rtoffset, 0);
    1409        7972 :     OffsetVarNodes(whereClause, rtoffset, 0);
    1410             : 
    1411             :     /*
    1412             :      * Upper-level vars in subquery will now be one level closer to their
    1413             :      * parent than before; in particular, anything that had been level 1
    1414             :      * becomes level zero.
    1415             :      */
    1416        7972 :     IncrementVarSublevelsUp((Node *) subselect, -1, 1);
    1417        7972 :     IncrementVarSublevelsUp(whereClause, -1, 1);
    1418             : 
    1419             :     /*
    1420             :      * Now that the WHERE clause is adjusted to match the parent query
    1421             :      * environment, we can easily identify all the level-zero rels it uses.
    1422             :      * The ones <= rtoffset belong to the upper query; the ones > rtoffset do
    1423             :      * not.
    1424             :      */
    1425        7972 :     clause_varnos = pull_varnos(whereClause);
    1426        7972 :     upper_varnos = NULL;
    1427       23932 :     while ((varno = bms_first_member(clause_varnos)) >= 0)
    1428             :     {
    1429       15960 :         if (varno <= rtoffset)
    1430        7988 :             upper_varnos = bms_add_member(upper_varnos, varno);
    1431             :     }
    1432        7972 :     bms_free(clause_varnos);
    1433             :     Assert(!bms_is_empty(upper_varnos));
    1434             : 
    1435             :     /*
    1436             :      * Now that we've got the set of upper-level varnos, we can make the last
    1437             :      * check: only available_rels can be referenced.
    1438             :      */
    1439        7972 :     if (!bms_is_subset(upper_varnos, available_rels))
    1440           0 :         return NULL;
    1441             : 
    1442             :     /* Now we can attach the modified subquery rtable to the parent */
    1443        7972 :     parse->rtable = list_concat(parse->rtable, subselect->rtable);
    1444             : 
    1445             :     /*
    1446             :      * And finally, build the JoinExpr node.
    1447             :      */
    1448        7972 :     result = makeNode(JoinExpr);
    1449        7972 :     result->jointype = under_not ? JOIN_ANTI : JOIN_SEMI;
    1450        7972 :     result->isNatural = false;
    1451        7972 :     result->larg = NULL;     /* caller must fill this in */
    1452             :     /* flatten out the FromExpr node if it's useless */
    1453        7972 :     if (list_length(subselect->jointree->fromlist) == 1)
    1454        7968 :         result->rarg = (Node *) linitial(subselect->jointree->fromlist);
    1455             :     else
    1456           4 :         result->rarg = (Node *) subselect->jointree;
    1457        7972 :     result->usingClause = NIL;
    1458        7972 :     result->quals = whereClause;
    1459        7972 :     result->alias = NULL;
    1460        7972 :     result->rtindex = 0;     /* we don't need an RTE for it */
    1461             : 
    1462        7972 :     return result;
    1463             : }
    1464             : 
    1465             : /*
    1466             :  * simplify_EXISTS_query: remove any useless stuff in an EXISTS's subquery
    1467             :  *
    1468             :  * The only thing that matters about an EXISTS query is whether it returns
    1469             :  * zero or more than zero rows.  Therefore, we can remove certain SQL features
    1470             :  * that won't affect that.  The only part that is really likely to matter in
    1471             :  * typical usage is simplifying the targetlist: it's a common habit to write
    1472             :  * "SELECT * FROM" even though there is no need to evaluate any columns.
    1473             :  *
    1474             :  * Note: by suppressing the targetlist we could cause an observable behavioral
    1475             :  * change, namely that any errors that might occur in evaluating the tlist
    1476             :  * won't occur, nor will other side-effects of volatile functions.  This seems
    1477             :  * unlikely to bother anyone in practice.
    1478             :  *
    1479             :  * Returns true if was able to discard the targetlist, else false.
    1480             :  */
    1481             : static bool
    1482       11210 : simplify_EXISTS_query(PlannerInfo *root, Query *query)
    1483             : {
    1484             :     /*
    1485             :      * We don't try to simplify at all if the query uses set operations,
    1486             :      * aggregates, grouping sets, SRFs, modifying CTEs, HAVING, OFFSET, or FOR
    1487             :      * UPDATE/SHARE; none of these seem likely in normal usage and their
    1488             :      * possible effects are complex.  (Note: we could ignore an "OFFSET 0"
    1489             :      * clause, but that traditionally is used as an optimization fence, so we
    1490             :      * don't.)
    1491             :      */
    1492       11210 :     if (query->commandType != CMD_SELECT ||
    1493       11210 :         query->setOperations ||
    1494       11210 :         query->hasAggs ||
    1495       11210 :         query->groupingSets ||
    1496       11210 :         query->hasWindowFuncs ||
    1497       11210 :         query->hasTargetSRFs ||
    1498       11210 :         query->hasModifyingCTE ||
    1499       11210 :         query->havingQual ||
    1500       11210 :         query->limitOffset ||
    1501       11210 :         query->rowMarks)
    1502          28 :         return false;
    1503             : 
    1504             :     /*
    1505             :      * LIMIT with a constant positive (or NULL) value doesn't affect the
    1506             :      * semantics of EXISTS, so let's ignore such clauses.  This is worth doing
    1507             :      * because people accustomed to certain other DBMSes may be in the habit
    1508             :      * of writing EXISTS(SELECT ... LIMIT 1) as an optimization.  If there's a
    1509             :      * LIMIT with anything else as argument, though, we can't simplify.
    1510             :      */
    1511       11182 :     if (query->limitCount)
    1512             :     {
    1513             :         /*
    1514             :          * The LIMIT clause has not yet been through eval_const_expressions,
    1515             :          * so we have to apply that here.  It might seem like this is a waste
    1516             :          * of cycles, since the only case plausibly worth worrying about is
    1517             :          * "LIMIT 1" ... but what we'll actually see is "LIMIT int8(1::int4)",
    1518             :          * so we have to fold constants or we're not going to recognize it.
    1519             :          */
    1520          16 :         Node       *node = eval_const_expressions(root, query->limitCount);
    1521             :         Const      *limit;
    1522             : 
    1523             :         /* Might as well update the query if we simplified the clause. */
    1524          16 :         query->limitCount = node;
    1525             : 
    1526          16 :         if (!IsA(node, Const))
    1527           0 :             return false;
    1528             : 
    1529          16 :         limit = (Const *) node;
    1530             :         Assert(limit->consttype == INT8OID);
    1531          16 :         if (!limit->constisnull && DatumGetInt64(limit->constvalue) <= 0)
    1532           8 :             return false;
    1533             : 
    1534             :         /* Whether or not the targetlist is safe, we can drop the LIMIT. */
    1535           8 :         query->limitCount = NULL;
    1536             :     }
    1537             : 
    1538             :     /*
    1539             :      * Otherwise, we can throw away the targetlist, as well as any GROUP,
    1540             :      * WINDOW, DISTINCT, and ORDER BY clauses; none of those clauses will
    1541             :      * change a nonzero-rows result to zero rows or vice versa.  (Furthermore,
    1542             :      * since our parsetree representation of these clauses depends on the
    1543             :      * targetlist, we'd better throw them away if we drop the targetlist.)
    1544             :      */
    1545       11174 :     query->targetList = NIL;
    1546       11174 :     query->groupClause = NIL;
    1547       11174 :     query->windowClause = NIL;
    1548       11174 :     query->distinctClause = NIL;
    1549       11174 :     query->sortClause = NIL;
    1550       11174 :     query->hasDistinctOn = false;
    1551             : 
    1552       11174 :     return true;
    1553             : }
    1554             : 
    1555             : /*
    1556             :  * convert_EXISTS_to_ANY: try to convert EXISTS to a hashable ANY sublink
    1557             :  *
    1558             :  * The subselect is expected to be a fresh copy that we can munge up,
    1559             :  * and to have been successfully passed through simplify_EXISTS_query.
    1560             :  *
    1561             :  * On success, the modified subselect is returned, and we store a suitable
    1562             :  * upper-level test expression at *testexpr, plus a list of the subselect's
    1563             :  * output Params at *paramIds.  (The test expression is already Param-ified
    1564             :  * and hence need not go through convert_testexpr, which is why we have to
    1565             :  * deal with the Param IDs specially.)
    1566             :  *
    1567             :  * On failure, returns NULL.
    1568             :  */
    1569             : static Query *
    1570        1500 : convert_EXISTS_to_ANY(PlannerInfo *root, Query *subselect,
    1571             :                       Node **testexpr, List **paramIds)
    1572             : {
    1573             :     Node       *whereClause;
    1574             :     List       *leftargs,
    1575             :                *rightargs,
    1576             :                *opids,
    1577             :                *opcollations,
    1578             :                *newWhere,
    1579             :                *tlist,
    1580             :                *testlist,
    1581             :                *paramids;
    1582             :     ListCell   *lc,
    1583             :                *rc,
    1584             :                *oc,
    1585             :                *cc;
    1586             :     AttrNumber  resno;
    1587             : 
    1588             :     /*
    1589             :      * Query must not require a targetlist, since we have to insert a new one.
    1590             :      * Caller should have dealt with the case already.
    1591             :      */
    1592             :     Assert(subselect->targetList == NIL);
    1593             : 
    1594             :     /*
    1595             :      * Separate out the WHERE clause.  (We could theoretically also remove
    1596             :      * top-level plain JOIN/ON clauses, but it's probably not worth the
    1597             :      * trouble.)
    1598             :      */
    1599        1500 :     whereClause = subselect->jointree->quals;
    1600        1500 :     subselect->jointree->quals = NULL;
    1601             : 
    1602             :     /*
    1603             :      * The rest of the sub-select must not refer to any Vars of the parent
    1604             :      * query.  (Vars of higher levels should be okay, though.)
    1605             :      *
    1606             :      * Note: we need not check for Aggrefs separately because we know the
    1607             :      * sub-select is as yet unoptimized; any uplevel Aggref must therefore
    1608             :      * contain an uplevel Var reference.  This is not the case below ...
    1609             :      */
    1610        1500 :     if (contain_vars_of_level((Node *) subselect, 1))
    1611         166 :         return NULL;
    1612             : 
    1613             :     /*
    1614             :      * We don't risk optimizing if the WHERE clause is volatile, either.
    1615             :      */
    1616        1334 :     if (contain_volatile_functions(whereClause))
    1617           0 :         return NULL;
    1618             : 
    1619             :     /*
    1620             :      * Clean up the WHERE clause by doing const-simplification etc on it.
    1621             :      * Aside from simplifying the processing we're about to do, this is
    1622             :      * important for being able to pull chunks of the WHERE clause up into the
    1623             :      * parent query.  Since we are invoked partway through the parent's
    1624             :      * preprocess_expression() work, earlier steps of preprocess_expression()
    1625             :      * wouldn't get applied to the pulled-up stuff unless we do them here. For
    1626             :      * the parts of the WHERE clause that get put back into the child query,
    1627             :      * this work is partially duplicative, but it shouldn't hurt.
    1628             :      *
    1629             :      * Note: we do not run flatten_join_alias_vars.  This is OK because any
    1630             :      * parent aliases were flattened already, and we're not going to pull any
    1631             :      * child Vars (of any description) into the parent.
    1632             :      *
    1633             :      * Note: passing the parent's root to eval_const_expressions is
    1634             :      * technically wrong, but we can get away with it since only the
    1635             :      * boundParams (if any) are used, and those would be the same in a
    1636             :      * subroot.
    1637             :      */
    1638        1334 :     whereClause = eval_const_expressions(root, whereClause);
    1639        1334 :     whereClause = (Node *) canonicalize_qual((Expr *) whereClause, false);
    1640        1334 :     whereClause = (Node *) make_ands_implicit((Expr *) whereClause);
    1641             : 
    1642             :     /*
    1643             :      * We now have a flattened implicit-AND list of clauses, which we try to
    1644             :      * break apart into "outervar = innervar" hash clauses. Anything that
    1645             :      * can't be broken apart just goes back into the newWhere list.  Note that
    1646             :      * we aren't trying hard yet to ensure that we have only outer or only
    1647             :      * inner on each side; we'll check that if we get to the end.
    1648             :      */
    1649        1334 :     leftargs = rightargs = opids = opcollations = newWhere = NIL;
    1650        5444 :     foreach(lc, (List *) whereClause)
    1651             :     {
    1652        4110 :         OpExpr     *expr = (OpExpr *) lfirst(lc);
    1653             : 
    1654        7068 :         if (IsA(expr, OpExpr) &&
    1655        2958 :             hash_ok_operator(expr))
    1656             :         {
    1657        2634 :             Node       *leftarg = (Node *) linitial(expr->args);
    1658        2634 :             Node       *rightarg = (Node *) lsecond(expr->args);
    1659             : 
    1660        2634 :             if (contain_vars_of_level(leftarg, 1))
    1661             :             {
    1662         172 :                 leftargs = lappend(leftargs, leftarg);
    1663         172 :                 rightargs = lappend(rightargs, rightarg);
    1664         172 :                 opids = lappend_oid(opids, expr->opno);
    1665         172 :                 opcollations = lappend_oid(opcollations, expr->inputcollid);
    1666         172 :                 continue;
    1667             :             }
    1668        2462 :             if (contain_vars_of_level(rightarg, 1))
    1669             :             {
    1670             :                 /*
    1671             :                  * We must commute the clause to put the outer var on the
    1672             :                  * left, because the hashing code in nodeSubplan.c expects
    1673             :                  * that.  This probably shouldn't ever fail, since hashable
    1674             :                  * operators ought to have commutators, but be paranoid.
    1675             :                  */
    1676        1498 :                 expr->opno = get_commutator(expr->opno);
    1677        1498 :                 if (OidIsValid(expr->opno) && hash_ok_operator(expr))
    1678             :                 {
    1679        1498 :                     leftargs = lappend(leftargs, rightarg);
    1680        1498 :                     rightargs = lappend(rightargs, leftarg);
    1681        1498 :                     opids = lappend_oid(opids, expr->opno);
    1682        1498 :                     opcollations = lappend_oid(opcollations, expr->inputcollid);
    1683        1498 :                     continue;
    1684             :                 }
    1685             :                 /* If no commutator, no chance to optimize the WHERE clause */
    1686           0 :                 return NULL;
    1687             :             }
    1688             :         }
    1689             :         /* Couldn't handle it as a hash clause */
    1690        2440 :         newWhere = lappend(newWhere, expr);
    1691             :     }
    1692             : 
    1693             :     /*
    1694             :      * If we didn't find anything we could convert, fail.
    1695             :      */
    1696        1334 :     if (leftargs == NIL)
    1697         160 :         return NULL;
    1698             : 
    1699             :     /*
    1700             :      * There mustn't be any parent Vars or Aggs in the stuff that we intend to
    1701             :      * put back into the child query.  Note: you might think we don't need to
    1702             :      * check for Aggs separately, because an uplevel Agg must contain an
    1703             :      * uplevel Var in its argument.  But it is possible that the uplevel Var
    1704             :      * got optimized away by eval_const_expressions.  Consider
    1705             :      *
    1706             :      * SUM(CASE WHEN false THEN uplevelvar ELSE 0 END)
    1707             :      */
    1708        2348 :     if (contain_vars_of_level((Node *) newWhere, 1) ||
    1709        1174 :         contain_vars_of_level((Node *) rightargs, 1))
    1710           0 :         return NULL;
    1711        1186 :     if (root->parse->hasAggs &&
    1712          24 :         (contain_aggs_of_level((Node *) newWhere, 1) ||
    1713          12 :          contain_aggs_of_level((Node *) rightargs, 1)))
    1714           0 :         return NULL;
    1715             : 
    1716             :     /*
    1717             :      * And there can't be any child Vars in the stuff we intend to pull up.
    1718             :      * (Note: we'd need to check for child Aggs too, except we know the child
    1719             :      * has no aggs at all because of simplify_EXISTS_query's check. The same
    1720             :      * goes for window functions.)
    1721             :      */
    1722        1174 :     if (contain_vars_of_level((Node *) leftargs, 0))
    1723           0 :         return NULL;
    1724             : 
    1725             :     /*
    1726             :      * Also reject sublinks in the stuff we intend to pull up.  (It might be
    1727             :      * possible to support this, but doesn't seem worth the complication.)
    1728             :      */
    1729        1174 :     if (contain_subplans((Node *) leftargs))
    1730           0 :         return NULL;
    1731             : 
    1732             :     /*
    1733             :      * Okay, adjust the sublevelsup in the stuff we're pulling up.
    1734             :      */
    1735        1174 :     IncrementVarSublevelsUp((Node *) leftargs, -1, 1);
    1736             : 
    1737             :     /*
    1738             :      * Put back any child-level-only WHERE clauses.
    1739             :      */
    1740        1174 :     if (newWhere)
    1741        1070 :         subselect->jointree->quals = (Node *) make_ands_explicit(newWhere);
    1742             : 
    1743             :     /*
    1744             :      * Build a new targetlist for the child that emits the expressions we
    1745             :      * need.  Concurrently, build a testexpr for the parent using Params to
    1746             :      * reference the child outputs.  (Since we generate Params directly here,
    1747             :      * there will be no need to convert the testexpr in build_subplan.)
    1748             :      */
    1749        1174 :     tlist = testlist = paramids = NIL;
    1750        1174 :     resno = 1;
    1751        2844 :     forfour(lc, leftargs, rc, rightargs, oc, opids, cc, opcollations)
    1752             :     {
    1753        1670 :         Node       *leftarg = (Node *) lfirst(lc);
    1754        1670 :         Node       *rightarg = (Node *) lfirst(rc);
    1755        1670 :         Oid         opid = lfirst_oid(oc);
    1756        1670 :         Oid         opcollation = lfirst_oid(cc);
    1757             :         Param      *param;
    1758             : 
    1759        1670 :         param = generate_new_exec_param(root,
    1760             :                                         exprType(rightarg),
    1761             :                                         exprTypmod(rightarg),
    1762             :                                         exprCollation(rightarg));
    1763        1670 :         tlist = lappend(tlist,
    1764        1670 :                         makeTargetEntry((Expr *) rightarg,
    1765        1670 :                                         resno++,
    1766             :                                         NULL,
    1767             :                                         false));
    1768        1670 :         testlist = lappend(testlist,
    1769        1670 :                            make_opclause(opid, BOOLOID, false,
    1770             :                                          (Expr *) leftarg, (Expr *) param,
    1771             :                                          InvalidOid, opcollation));
    1772        1670 :         paramids = lappend_int(paramids, param->paramid);
    1773             :     }
    1774             : 
    1775             :     /* Put everything where it should go, and we're done */
    1776        1174 :     subselect->targetList = tlist;
    1777        1174 :     *testexpr = (Node *) make_ands_explicit(testlist);
    1778        1174 :     *paramIds = paramids;
    1779             : 
    1780        1174 :     return subselect;
    1781             : }
    1782             : 
    1783             : 
    1784             : /*
    1785             :  * Replace correlation vars (uplevel vars) with Params.
    1786             :  *
    1787             :  * Uplevel PlaceHolderVars and aggregates are replaced, too.
    1788             :  *
    1789             :  * Note: it is critical that this runs immediately after SS_process_sublinks.
    1790             :  * Since we do not recurse into the arguments of uplevel PHVs and aggregates,
    1791             :  * they will get copied to the appropriate subplan args list in the parent
    1792             :  * query with uplevel vars not replaced by Params, but only adjusted in level
    1793             :  * (see replace_outer_placeholdervar and replace_outer_agg).  That's exactly
    1794             :  * what we want for the vars of the parent level --- but if a PHV's or
    1795             :  * aggregate's argument contains any further-up variables, they have to be
    1796             :  * replaced with Params in their turn. That will happen when the parent level
    1797             :  * runs SS_replace_correlation_vars.  Therefore it must do so after expanding
    1798             :  * its sublinks to subplans.  And we don't want any steps in between, else
    1799             :  * those steps would never get applied to the argument expressions, either in
    1800             :  * the parent or the child level.
    1801             :  *
    1802             :  * Another fairly tricky thing going on here is the handling of SubLinks in
    1803             :  * the arguments of uplevel PHVs/aggregates.  Those are not touched inside the
    1804             :  * intermediate query level, either.  Instead, SS_process_sublinks recurses on
    1805             :  * them after copying the PHV or Aggref expression into the parent plan level
    1806             :  * (this is actually taken care of in build_subplan).
    1807             :  */
    1808             : Node *
    1809      135604 : SS_replace_correlation_vars(PlannerInfo *root, Node *expr)
    1810             : {
    1811             :     /* No setup needed for tree walk, so away we go */
    1812      135604 :     return replace_correlation_vars_mutator(expr, root);
    1813             : }
    1814             : 
    1815             : static Node *
    1816     1045426 : replace_correlation_vars_mutator(Node *node, PlannerInfo *root)
    1817             : {
    1818     1045426 :     if (node == NULL)
    1819       41700 :         return NULL;
    1820     1003726 :     if (IsA(node, Var))
    1821             :     {
    1822      258404 :         if (((Var *) node)->varlevelsup > 0)
    1823       64996 :             return (Node *) replace_outer_var(root, (Var *) node);
    1824             :     }
    1825      938730 :     if (IsA(node, PlaceHolderVar))
    1826             :     {
    1827          28 :         if (((PlaceHolderVar *) node)->phlevelsup > 0)
    1828          28 :             return (Node *) replace_outer_placeholdervar(root,
    1829             :                                                          (PlaceHolderVar *) node);
    1830             :     }
    1831      938702 :     if (IsA(node, Aggref))
    1832             :     {
    1833       11512 :         if (((Aggref *) node)->agglevelsup > 0)
    1834          32 :             return (Node *) replace_outer_agg(root, (Aggref *) node);
    1835             :     }
    1836      938670 :     if (IsA(node, GroupingFunc))
    1837             :     {
    1838          32 :         if (((GroupingFunc *) node)->agglevelsup > 0)
    1839          16 :             return (Node *) replace_outer_grouping(root, (GroupingFunc *) node);
    1840             :     }
    1841      938654 :     return expression_tree_mutator(node,
    1842             :                                    replace_correlation_vars_mutator,
    1843             :                                    (void *) root);
    1844             : }
    1845             : 
    1846             : /*
    1847             :  * Expand SubLinks to SubPlans in the given expression.
    1848             :  *
    1849             :  * The isQual argument tells whether or not this expression is a WHERE/HAVING
    1850             :  * qualifier expression.  If it is, any sublinks appearing at top level need
    1851             :  * not distinguish FALSE from UNKNOWN return values.
    1852             :  */
    1853             : Node *
    1854      109490 : SS_process_sublinks(PlannerInfo *root, Node *expr, bool isQual)
    1855             : {
    1856             :     process_sublinks_context context;
    1857             : 
    1858      109490 :     context.root = root;
    1859      109490 :     context.isTopQual = isQual;
    1860      109490 :     return process_sublinks_mutator(expr, &context);
    1861             : }
    1862             : 
    1863             : static Node *
    1864     1729246 : process_sublinks_mutator(Node *node, process_sublinks_context *context)
    1865             : {
    1866             :     process_sublinks_context locContext;
    1867             : 
    1868     1729246 :     locContext.root = context->root;
    1869             : 
    1870     1729246 :     if (node == NULL)
    1871       88958 :         return NULL;
    1872     1640288 :     if (IsA(node, SubLink))
    1873             :     {
    1874       46622 :         SubLink    *sublink = (SubLink *) node;
    1875             :         Node       *testexpr;
    1876             : 
    1877             :         /*
    1878             :          * First, recursively process the lefthand-side expressions, if any.
    1879             :          * They're not top-level anymore.
    1880             :          */
    1881       46622 :         locContext.isTopQual = false;
    1882       46622 :         testexpr = process_sublinks_mutator(sublink->testexpr, &locContext);
    1883             : 
    1884             :         /*
    1885             :          * Now build the SubPlan node and make the expr to return.
    1886             :          */
    1887       46622 :         return make_subplan(context->root,
    1888       46622 :                             (Query *) sublink->subselect,
    1889             :                             sublink->subLinkType,
    1890             :                             sublink->subLinkId,
    1891             :                             testexpr,
    1892       46622 :                             context->isTopQual);
    1893             :     }
    1894             : 
    1895             :     /*
    1896             :      * Don't recurse into the arguments of an outer PHV or aggregate here. Any
    1897             :      * SubLinks in the arguments have to be dealt with at the outer query
    1898             :      * level; they'll be handled when build_subplan collects the PHV or Aggref
    1899             :      * into the arguments to be passed down to the current subplan.
    1900             :      */
    1901     1593666 :     if (IsA(node, PlaceHolderVar))
    1902             :     {
    1903          64 :         if (((PlaceHolderVar *) node)->phlevelsup > 0)
    1904           0 :             return node;
    1905             :     }
    1906     1593602 :     else if (IsA(node, Aggref))
    1907             :     {
    1908        6322 :         if (((Aggref *) node)->agglevelsup > 0)
    1909          12 :             return node;
    1910             :     }
    1911             : 
    1912             :     /*
    1913             :      * We should never see a SubPlan expression in the input (since this is
    1914             :      * the very routine that creates 'em to begin with).  We shouldn't find
    1915             :      * ourselves invoked directly on a Query, either.
    1916             :      */
    1917             :     Assert(!IsA(node, SubPlan));
    1918             :     Assert(!IsA(node, AlternativeSubPlan));
    1919             :     Assert(!IsA(node, Query));
    1920             : 
    1921             :     /*
    1922             :      * Because make_subplan() could return an AND or OR clause, we have to
    1923             :      * take steps to preserve AND/OR flatness of a qual.  We assume the input
    1924             :      * has been AND/OR flattened and so we need no recursion here.
    1925             :      *
    1926             :      * (Due to the coding here, we will not get called on the List subnodes of
    1927             :      * an AND; and the input is *not* yet in implicit-AND format.  So no check
    1928             :      * is needed for a bare List.)
    1929             :      *
    1930             :      * Anywhere within the top-level AND/OR clause structure, we can tell
    1931             :      * make_subplan() that NULL and FALSE are interchangeable.  So isTopQual
    1932             :      * propagates down in both cases.  (Note that this is unlike the meaning
    1933             :      * of "top level qual" used in most other places in Postgres.)
    1934             :      */
    1935     1593654 :     if (is_andclause(node))
    1936             :     {
    1937       23816 :         List       *newargs = NIL;
    1938             :         ListCell   *l;
    1939             : 
    1940             :         /* Still at qual top-level */
    1941       23816 :         locContext.isTopQual = context->isTopQual;
    1942             : 
    1943       81882 :         foreach(l, ((BoolExpr *) node)->args)
    1944             :         {
    1945             :             Node       *newarg;
    1946             : 
    1947       58066 :             newarg = process_sublinks_mutator(lfirst(l), &locContext);
    1948       58066 :             if (is_andclause(newarg))
    1949           0 :                 newargs = list_concat(newargs, ((BoolExpr *) newarg)->args);
    1950             :             else
    1951       58066 :                 newargs = lappend(newargs, newarg);
    1952             :         }
    1953       23816 :         return (Node *) make_andclause(newargs);
    1954             :     }
    1955             : 
    1956     1569838 :     if (is_orclause(node))
    1957             :     {
    1958        2912 :         List       *newargs = NIL;
    1959             :         ListCell   *l;
    1960             : 
    1961             :         /* Still at qual top-level */
    1962        2912 :         locContext.isTopQual = context->isTopQual;
    1963             : 
    1964        9518 :         foreach(l, ((BoolExpr *) node)->args)
    1965             :         {
    1966             :             Node       *newarg;
    1967             : 
    1968        6606 :             newarg = process_sublinks_mutator(lfirst(l), &locContext);
    1969        6606 :             if (is_orclause(newarg))
    1970           0 :                 newargs = list_concat(newargs, ((BoolExpr *) newarg)->args);
    1971             :             else
    1972        6606 :                 newargs = lappend(newargs, newarg);
    1973             :         }
    1974        2912 :         return (Node *) make_orclause(newargs);
    1975             :     }
    1976             : 
    1977             :     /*
    1978             :      * If we recurse down through anything other than an AND or OR node, we
    1979             :      * are definitely not at top qual level anymore.
    1980             :      */
    1981     1566926 :     locContext.isTopQual = false;
    1982             : 
    1983     1566926 :     return expression_tree_mutator(node,
    1984             :                                    process_sublinks_mutator,
    1985             :                                    (void *) &locContext);
    1986             : }
    1987             : 
    1988             : /*
    1989             :  * SS_identify_outer_params - identify the Params available from outer levels
    1990             :  *
    1991             :  * This must be run after SS_replace_correlation_vars and SS_process_sublinks
    1992             :  * processing is complete in a given query level as well as all of its
    1993             :  * descendant levels (which means it's most practical to do it at the end of
    1994             :  * processing the query level).  We compute the set of paramIds that outer
    1995             :  * levels will make available to this level+descendants, and record it in
    1996             :  * root->outer_params for use while computing extParam/allParam sets in final
    1997             :  * plan cleanup.  (We can't just compute it then, because the upper levels'
    1998             :  * plan_params lists are transient and will be gone by then.)
    1999             :  */
    2000             : void
    2001      318348 : SS_identify_outer_params(PlannerInfo *root)
    2002             : {
    2003             :     Bitmapset  *outer_params;
    2004             :     PlannerInfo *proot;
    2005             :     ListCell   *l;
    2006             : 
    2007             :     /*
    2008             :      * If no parameters have been assigned anywhere in the tree, we certainly
    2009             :      * don't need to do anything here.
    2010             :      */
    2011      318348 :     if (root->glob->paramExecTypes == NIL)
    2012      171364 :         return;
    2013             : 
    2014             :     /*
    2015             :      * Scan all query levels above this one to see which parameters are due to
    2016             :      * be available from them, either because lower query levels have
    2017             :      * requested them (via plan_params) or because they will be available from
    2018             :      * initPlans of those levels.
    2019             :      */
    2020      146984 :     outer_params = NULL;
    2021      191862 :     for (proot = root->parent_root; proot != NULL; proot = proot->parent_root)
    2022             :     {
    2023             :         /* Include ordinary Var/PHV/Aggref params */
    2024      102474 :         foreach(l, proot->plan_params)
    2025             :         {
    2026       57596 :             PlannerParamItem *pitem = (PlannerParamItem *) lfirst(l);
    2027             : 
    2028       57596 :             outer_params = bms_add_member(outer_params, pitem->paramId);
    2029             :         }
    2030             :         /* Include any outputs of outer-level initPlans */
    2031       46120 :         foreach(l, proot->init_plans)
    2032             :         {
    2033        1242 :             SubPlan    *initsubplan = (SubPlan *) lfirst(l);
    2034             :             ListCell   *l2;
    2035             : 
    2036        2484 :             foreach(l2, initsubplan->setParam)
    2037             :             {
    2038        1242 :                 outer_params = bms_add_member(outer_params, lfirst_int(l2));
    2039             :             }
    2040             :         }
    2041             :         /* Include worktable ID, if a recursive query is being planned */
    2042       44878 :         if (proot->wt_param_id >= 0)
    2043         748 :             outer_params = bms_add_member(outer_params, proot->wt_param_id);
    2044             :     }
    2045      146984 :     root->outer_params = outer_params;
    2046             : }
    2047             : 
    2048             : /*
    2049             :  * SS_charge_for_initplans - account for initplans in Path costs & parallelism
    2050             :  *
    2051             :  * If any initPlans have been created in the current query level, they will
    2052             :  * get attached to the Plan tree created from whichever Path we select from
    2053             :  * the given rel.  Increment all that rel's Paths' costs to account for them,
    2054             :  * and make sure the paths get marked as parallel-unsafe, since we can't
    2055             :  * currently transmit initPlans to parallel workers.
    2056             :  *
    2057             :  * This is separate from SS_attach_initplans because we might conditionally
    2058             :  * create more initPlans during create_plan(), depending on which Path we
    2059             :  * select.  However, Paths that would generate such initPlans are expected
    2060             :  * to have included their cost already.
    2061             :  */
    2062             : void
    2063      318348 : SS_charge_for_initplans(PlannerInfo *root, RelOptInfo *final_rel)
    2064             : {
    2065             :     Cost        initplan_cost;
    2066             :     ListCell   *lc;
    2067             : 
    2068             :     /* Nothing to do if no initPlans */
    2069      318348 :     if (root->init_plans == NIL)
    2070      308776 :         return;
    2071             : 
    2072             :     /*
    2073             :      * Compute the cost increment just once, since it will be the same for all
    2074             :      * Paths.  We assume each initPlan gets run once during top plan startup.
    2075             :      * This is a conservative overestimate, since in fact an initPlan might be
    2076             :      * executed later than plan startup, or even not at all.
    2077             :      */
    2078        9572 :     initplan_cost = 0;
    2079       19494 :     foreach(lc, root->init_plans)
    2080             :     {
    2081        9922 :         SubPlan    *initsubplan = (SubPlan *) lfirst(lc);
    2082             : 
    2083        9922 :         initplan_cost += initsubplan->startup_cost + initsubplan->per_call_cost;
    2084             :     }
    2085             : 
    2086             :     /*
    2087             :      * Now adjust the costs and parallel_safe flags.
    2088             :      */
    2089       19180 :     foreach(lc, final_rel->pathlist)
    2090             :     {
    2091        9608 :         Path       *path = (Path *) lfirst(lc);
    2092             : 
    2093        9608 :         path->startup_cost += initplan_cost;
    2094        9608 :         path->total_cost += initplan_cost;
    2095        9608 :         path->parallel_safe = false;
    2096             :     }
    2097             : 
    2098             :     /*
    2099             :      * Forget about any partial paths and clear consider_parallel, too;
    2100             :      * they're not usable if we attached an initPlan.
    2101             :      */
    2102        9572 :     final_rel->partial_pathlist = NIL;
    2103        9572 :     final_rel->consider_parallel = false;
    2104             : 
    2105             :     /* We needn't do set_cheapest() here, caller will do it */
    2106             : }
    2107             : 
    2108             : /*
    2109             :  * SS_attach_initplans - attach initplans to topmost plan node
    2110             :  *
    2111             :  * Attach any initplans created in the current query level to the specified
    2112             :  * plan node, which should normally be the topmost node for the query level.
    2113             :  * (In principle the initPlans could go in any node at or above where they're
    2114             :  * referenced; but there seems no reason to put them any lower than the
    2115             :  * topmost node, so we don't bother to track exactly where they came from.)
    2116             :  * We do not touch the plan node's cost; the initplans should have been
    2117             :  * accounted for in path costing.
    2118             :  */
    2119             : void
    2120      317682 : SS_attach_initplans(PlannerInfo *root, Plan *plan)
    2121             : {
    2122      317682 :     plan->initPlan = root->init_plans;
    2123      317682 : }
    2124             : 
    2125             : /*
    2126             :  * SS_finalize_plan - do final parameter processing for a completed Plan.
    2127             :  *
    2128             :  * This recursively computes the extParam and allParam sets for every Plan
    2129             :  * node in the given plan tree.  (Oh, and RangeTblFunction.funcparams too.)
    2130             :  *
    2131             :  * We assume that SS_finalize_plan has already been run on any initplans or
    2132             :  * subplans the plan tree could reference.
    2133             :  */
    2134             : void
    2135      162526 : SS_finalize_plan(PlannerInfo *root, Plan *plan)
    2136             : {
    2137             :     /* No setup needed, just recurse through plan tree. */
    2138      162526 :     (void) finalize_plan(root, plan, -1, root->outer_params, NULL);
    2139      162526 : }
    2140             : 
    2141             : /*
    2142             :  * Recursive processing of all nodes in the plan tree
    2143             :  *
    2144             :  * gather_param is the rescan_param of an ancestral Gather/GatherMerge,
    2145             :  * or -1 if there is none.
    2146             :  *
    2147             :  * valid_params is the set of param IDs supplied by outer plan levels
    2148             :  * that are valid to reference in this plan node or its children.
    2149             :  *
    2150             :  * scan_params is a set of param IDs to force scan plan nodes to reference.
    2151             :  * This is for EvalPlanQual support, and is always NULL at the top of the
    2152             :  * recursion.
    2153             :  *
    2154             :  * The return value is the computed allParam set for the given Plan node.
    2155             :  * This is just an internal notational convenience: we can add a child
    2156             :  * plan's allParams to the set of param IDs of interest to this level
    2157             :  * in the same statement that recurses to that child.
    2158             :  *
    2159             :  * Do not scribble on caller's values of valid_params or scan_params!
    2160             :  *
    2161             :  * Note: although we attempt to deal with initPlans anywhere in the tree, the
    2162             :  * logic is not really right.  The problem is that a plan node might return an
    2163             :  * output Param of its initPlan as a targetlist item, in which case it's valid
    2164             :  * for the parent plan level to reference that same Param; the parent's usage
    2165             :  * will be converted into a Var referencing the child plan node by setrefs.c.
    2166             :  * But this function would see the parent's reference as out of scope and
    2167             :  * complain about it.  For now, this does not matter because the planner only
    2168             :  * attaches initPlans to the topmost plan node in a query level, so the case
    2169             :  * doesn't arise.  If we ever merge this processing into setrefs.c, maybe it
    2170             :  * can be handled more cleanly.
    2171             :  */
    2172             : static Bitmapset *
    2173     1060404 : finalize_plan(PlannerInfo *root, Plan *plan,
    2174             :               int gather_param,
    2175             :               Bitmapset *valid_params,
    2176             :               Bitmapset *scan_params)
    2177             : {
    2178             :     finalize_primnode_context context;
    2179             :     int         locally_added_param;
    2180             :     Bitmapset  *nestloop_params;
    2181             :     Bitmapset  *initExtParam;
    2182             :     Bitmapset  *initSetParam;
    2183             :     Bitmapset  *child_params;
    2184             :     ListCell   *l;
    2185             : 
    2186     1060404 :     if (plan == NULL)
    2187      653790 :         return NULL;
    2188             : 
    2189      406614 :     context.root = root;
    2190      406614 :     context.paramids = NULL;    /* initialize set to empty */
    2191      406614 :     locally_added_param = -1;   /* there isn't one */
    2192      406614 :     nestloop_params = NULL;     /* there aren't any */
    2193             : 
    2194             :     /*
    2195             :      * Examine any initPlans to determine the set of external params they
    2196             :      * reference and the set of output params they supply.  (We assume
    2197             :      * SS_finalize_plan was run on them already.)
    2198             :      */
    2199      406614 :     initExtParam = initSetParam = NULL;
    2200      416896 :     foreach(l, plan->initPlan)
    2201             :     {
    2202       10282 :         SubPlan    *initsubplan = (SubPlan *) lfirst(l);
    2203       10282 :         Plan       *initplan = planner_subplan_get_plan(root, initsubplan);
    2204             :         ListCell   *l2;
    2205             : 
    2206       10282 :         initExtParam = bms_add_members(initExtParam, initplan->extParam);
    2207       20580 :         foreach(l2, initsubplan->setParam)
    2208             :         {
    2209       10298 :             initSetParam = bms_add_member(initSetParam, lfirst_int(l2));
    2210             :         }
    2211             :     }
    2212             : 
    2213             :     /* Any setParams are validly referenceable in this node and children */
    2214      406614 :     if (initSetParam)
    2215        9900 :         valid_params = bms_union(valid_params, initSetParam);
    2216             : 
    2217             :     /*
    2218             :      * When we call finalize_primnode, context.paramids sets are automatically
    2219             :      * merged together.  But when recursing to self, we have to do it the hard
    2220             :      * way.  We want the paramids set to include params in subplans as well as
    2221             :      * at this level.
    2222             :      */
    2223             : 
    2224             :     /* Find params in targetlist and qual */
    2225      406614 :     finalize_primnode((Node *) plan->targetlist, &context);
    2226      406614 :     finalize_primnode((Node *) plan->qual, &context);
    2227             : 
    2228             :     /*
    2229             :      * If it's a parallel-aware scan node, mark it as dependent on the parent
    2230             :      * Gather/GatherMerge's rescan Param.
    2231             :      */
    2232      406614 :     if (plan->parallel_aware)
    2233             :     {
    2234        1276 :         if (gather_param < 0)
    2235           0 :             elog(ERROR, "parallel-aware plan node is not below a Gather");
    2236        1276 :         context.paramids =
    2237        1276 :             bms_add_member(context.paramids, gather_param);
    2238             :     }
    2239             : 
    2240             :     /* Check additional node-type-specific fields */
    2241      406614 :     switch (nodeTag(plan))
    2242             :     {
    2243       48198 :         case T_Result:
    2244       48198 :             finalize_primnode(((Result *) plan)->resconstantqual,
    2245             :                               &context);
    2246       48198 :             break;
    2247             : 
    2248       68690 :         case T_SeqScan:
    2249       68690 :             context.paramids = bms_add_members(context.paramids, scan_params);
    2250       68690 :             break;
    2251             : 
    2252          36 :         case T_SampleScan:
    2253          36 :             finalize_primnode((Node *) ((SampleScan *) plan)->tablesample,
    2254             :                               &context);
    2255          36 :             context.paramids = bms_add_members(context.paramids, scan_params);
    2256          36 :             break;
    2257             : 
    2258       57758 :         case T_IndexScan:
    2259       57758 :             finalize_primnode((Node *) ((IndexScan *) plan)->indexqual,
    2260             :                               &context);
    2261       57758 :             finalize_primnode((Node *) ((IndexScan *) plan)->indexorderby,
    2262             :                               &context);
    2263             : 
    2264             :             /*
    2265             :              * we need not look at indexqualorig, since it will have the same
    2266             :              * param references as indexqual.  Likewise, we can ignore
    2267             :              * indexorderbyorig.
    2268             :              */
    2269       57758 :             context.paramids = bms_add_members(context.paramids, scan_params);
    2270       57758 :             break;
    2271             : 
    2272        2090 :         case T_IndexOnlyScan:
    2273        2090 :             finalize_primnode((Node *) ((IndexOnlyScan *) plan)->indexqual,
    2274             :                               &context);
    2275        2090 :             finalize_primnode((Node *) ((IndexOnlyScan *) plan)->indexorderby,
    2276             :                               &context);
    2277             : 
    2278             :             /*
    2279             :              * we need not look at indextlist, since it cannot contain Params.
    2280             :              */
    2281        2090 :             context.paramids = bms_add_members(context.paramids, scan_params);
    2282        2090 :             break;
    2283             : 
    2284        9366 :         case T_BitmapIndexScan:
    2285        9366 :             finalize_primnode((Node *) ((BitmapIndexScan *) plan)->indexqual,
    2286             :                               &context);
    2287             : 
    2288             :             /*
    2289             :              * we need not look at indexqualorig, since it will have the same
    2290             :              * param references as indexqual.
    2291             :              */
    2292        9366 :             break;
    2293             : 
    2294        9306 :         case T_BitmapHeapScan:
    2295        9306 :             finalize_primnode((Node *) ((BitmapHeapScan *) plan)->bitmapqualorig,
    2296             :                               &context);
    2297        9306 :             context.paramids = bms_add_members(context.paramids, scan_params);
    2298        9306 :             break;
    2299             : 
    2300         382 :         case T_TidScan:
    2301         382 :             finalize_primnode((Node *) ((TidScan *) plan)->tidquals,
    2302             :                               &context);
    2303         382 :             context.paramids = bms_add_members(context.paramids, scan_params);
    2304         382 :             break;
    2305             : 
    2306        4482 :         case T_SubqueryScan:
    2307             :             {
    2308        4482 :                 SubqueryScan *sscan = (SubqueryScan *) plan;
    2309             :                 RelOptInfo *rel;
    2310             :                 Bitmapset  *subquery_params;
    2311             : 
    2312             :                 /* We must run finalize_plan on the subquery */
    2313        4482 :                 rel = find_base_rel(root, sscan->scan.scanrelid);
    2314        4482 :                 subquery_params = rel->subroot->outer_params;
    2315        4482 :                 if (gather_param >= 0)
    2316           8 :                     subquery_params = bms_add_member(bms_copy(subquery_params),
    2317             :                                                      gather_param);
    2318        4482 :                 finalize_plan(rel->subroot, sscan->subplan, gather_param,
    2319             :                               subquery_params, NULL);
    2320             : 
    2321             :                 /* Now we can add its extParams to the parent's params */
    2322        8964 :                 context.paramids = bms_add_members(context.paramids,
    2323        4482 :                                                    sscan->subplan->extParam);
    2324             :                 /* We need scan_params too, though */
    2325        4482 :                 context.paramids = bms_add_members(context.paramids,
    2326             :                                                    scan_params);
    2327             :             }
    2328        4482 :             break;
    2329             : 
    2330       23226 :         case T_FunctionScan:
    2331             :             {
    2332       23226 :                 FunctionScan *fscan = (FunctionScan *) plan;
    2333             :                 ListCell   *lc;
    2334             : 
    2335             :                 /*
    2336             :                  * Call finalize_primnode independently on each function
    2337             :                  * expression, so that we can record which params are
    2338             :                  * referenced in each, in order to decide which need
    2339             :                  * re-evaluating during rescan.
    2340             :                  */
    2341       46468 :                 foreach(lc, fscan->functions)
    2342             :                 {
    2343       23242 :                     RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
    2344             :                     finalize_primnode_context funccontext;
    2345             : 
    2346       23242 :                     funccontext = context;
    2347       23242 :                     funccontext.paramids = NULL;
    2348             : 
    2349       23242 :                     finalize_primnode(rtfunc->funcexpr, &funccontext);
    2350             : 
    2351             :                     /* remember results for execution */
    2352       23242 :                     rtfunc->funcparams = funccontext.paramids;
    2353             : 
    2354             :                     /* add the function's params to the overall set */
    2355       23242 :                     context.paramids = bms_add_members(context.paramids,
    2356       23242 :                                                        funccontext.paramids);
    2357             :                 }
    2358             : 
    2359       23226 :                 context.paramids = bms_add_members(context.paramids,
    2360             :                                                    scan_params);
    2361             :             }
    2362       23226 :             break;
    2363             : 
    2364          96 :         case T_TableFuncScan:
    2365          96 :             finalize_primnode((Node *) ((TableFuncScan *) plan)->tablefunc,
    2366             :                               &context);
    2367          96 :             context.paramids = bms_add_members(context.paramids, scan_params);
    2368          96 :             break;
    2369             : 
    2370        2828 :         case T_ValuesScan:
    2371        2828 :             finalize_primnode((Node *) ((ValuesScan *) plan)->values_lists,
    2372             :                               &context);
    2373        2828 :             context.paramids = bms_add_members(context.paramids, scan_params);
    2374        2828 :             break;
    2375             : 
    2376         892 :         case T_CteScan:
    2377             :             {
    2378             :                 /*
    2379             :                  * You might think we should add the node's cteParam to
    2380             :                  * paramids, but we shouldn't because that param is just a
    2381             :                  * linkage mechanism for multiple CteScan nodes for the same
    2382             :                  * CTE; it is never used for changed-param signaling.  What we
    2383             :                  * have to do instead is to find the referenced CTE plan and
    2384             :                  * incorporate its external paramids, so that the correct
    2385             :                  * things will happen if the CTE references outer-level
    2386             :                  * variables.  See test cases for bug #4902.  (We assume
    2387             :                  * SS_finalize_plan was run on the CTE plan already.)
    2388             :                  */
    2389         892 :                 int         plan_id = ((CteScan *) plan)->ctePlanId;
    2390             :                 Plan       *cteplan;
    2391             : 
    2392             :                 /* so, do this ... */
    2393         892 :                 if (plan_id < 1 || plan_id > list_length(root->glob->subplans))
    2394           0 :                     elog(ERROR, "could not find plan for CteScan referencing plan ID %d",
    2395             :                          plan_id);
    2396         892 :                 cteplan = (Plan *) list_nth(root->glob->subplans, plan_id - 1);
    2397         892 :                 context.paramids =
    2398         892 :                     bms_add_members(context.paramids, cteplan->extParam);
    2399             : 
    2400             : #ifdef NOT_USED
    2401             :                 /* ... but not this */
    2402             :                 context.paramids =
    2403             :                     bms_add_member(context.paramids,
    2404             :                                    ((CteScan *) plan)->cteParam);
    2405             : #endif
    2406             : 
    2407         892 :                 context.paramids = bms_add_members(context.paramids,
    2408             :                                                    scan_params);
    2409             :             }
    2410         892 :             break;
    2411             : 
    2412         332 :         case T_WorkTableScan:
    2413         332 :             context.paramids =
    2414         332 :                 bms_add_member(context.paramids,
    2415             :                                ((WorkTableScan *) plan)->wtParam);
    2416         332 :             context.paramids = bms_add_members(context.paramids, scan_params);
    2417         332 :             break;
    2418             : 
    2419         208 :         case T_NamedTuplestoreScan:
    2420         208 :             context.paramids = bms_add_members(context.paramids, scan_params);
    2421         208 :             break;
    2422             : 
    2423         510 :         case T_ForeignScan:
    2424             :             {
    2425         510 :                 ForeignScan *fscan = (ForeignScan *) plan;
    2426             : 
    2427         510 :                 finalize_primnode((Node *) fscan->fdw_exprs,
    2428             :                                   &context);
    2429         510 :                 finalize_primnode((Node *) fscan->fdw_recheck_quals,
    2430             :                                   &context);
    2431             : 
    2432             :                 /* We assume fdw_scan_tlist cannot contain Params */
    2433         510 :                 context.paramids = bms_add_members(context.paramids,
    2434             :                                                    scan_params);
    2435             :             }
    2436         510 :             break;
    2437             : 
    2438           0 :         case T_CustomScan:
    2439           0 :             {
    2440           0 :                 CustomScan *cscan = (CustomScan *) plan;
    2441             :                 ListCell   *lc;
    2442             : 
    2443           0 :                 finalize_primnode((Node *) cscan->custom_exprs,
    2444             :                                   &context);
    2445             :                 /* We assume custom_scan_tlist cannot contain Params */
    2446           0 :                 context.paramids =
    2447           0 :                     bms_add_members(context.paramids, scan_params);
    2448             : 
    2449             :                 /* child nodes if any */
    2450           0 :                 foreach(lc, cscan->custom_plans)
    2451             :                 {
    2452           0 :                     context.paramids =
    2453           0 :                         bms_add_members(context.paramids,
    2454           0 :                                         finalize_plan(root,
    2455           0 :                                                       (Plan *) lfirst(lc),
    2456             :                                                       gather_param,
    2457             :                                                       valid_params,
    2458             :                                                       scan_params));
    2459             :                 }
    2460             :             }
    2461           0 :             break;
    2462             : 
    2463       72980 :         case T_ModifyTable:
    2464       72980 :             {
    2465       72980 :                 ModifyTable *mtplan = (ModifyTable *) plan;
    2466             :                 ListCell   *l;
    2467             : 
    2468             :                 /* Force descendant scan nodes to reference epqParam */
    2469       72980 :                 locally_added_param = mtplan->epqParam;
    2470       72980 :                 valid_params = bms_add_member(bms_copy(valid_params),
    2471             :                                               locally_added_param);
    2472       72980 :                 scan_params = bms_add_member(bms_copy(scan_params),
    2473             :                                              locally_added_param);
    2474       72980 :                 finalize_primnode((Node *) mtplan->returningLists,
    2475             :                                   &context);
    2476       72980 :                 finalize_primnode((Node *) mtplan->onConflictSet,
    2477             :                                   &context);
    2478       72980 :                 finalize_primnode((Node *) mtplan->onConflictWhere,
    2479             :                                   &context);
    2480             :                 /* exclRelTlist contains only Vars, doesn't need examination */
    2481      147158 :                 foreach(l, mtplan->plans)
    2482             :                 {
    2483       74178 :                     context.paramids =
    2484       74178 :                         bms_add_members(context.paramids,
    2485       74178 :                                         finalize_plan(root,
    2486       74178 :                                                       (Plan *) lfirst(l),
    2487             :                                                       gather_param,
    2488             :                                                       valid_params,
    2489             :                                                       scan_params));
    2490             :                 }
    2491             :             }
    2492       72980 :             break;
    2493             : 
    2494        2288 :         case T_Append:
    2495        2288 :             {
    2496             :                 ListCell   *l;
    2497             : 
    2498        7954 :                 foreach(l, ((Append *) plan)->appendplans)
    2499             :                 {
    2500        5666 :                     context.paramids =
    2501        5666 :                         bms_add_members(context.paramids,
    2502        5666 :                                         finalize_plan(root,
    2503        5666 :                                                       (Plan *) lfirst(l),
    2504             :                                                       gather_param,
    2505             :                                                       valid_params,
    2506             :                                                       scan_params));
    2507             :                 }
    2508             :             }
    2509        2288 :             break;
    2510             : 
    2511          60 :         case T_MergeAppend:
    2512          60 :             {
    2513             :                 ListCell   *l;
    2514             : 
    2515         264 :                 foreach(l, ((MergeAppend *) plan)->mergeplans)
    2516             :                 {
    2517         204 :                     context.paramids =
    2518         204 :                         bms_add_members(context.paramids,
    2519         204 :                                         finalize_plan(root,
    2520         204 :                                                       (Plan *) lfirst(l),
    2521             :                                                       gather_param,
    2522             :                                                       valid_params,
    2523             :                                                       scan_params));
    2524             :                 }
    2525             :             }
    2526          60 :             break;
    2527             : 
    2528          28 :         case T_BitmapAnd:
    2529          28 :             {
    2530             :                 ListCell   *l;
    2531             : 
    2532          84 :                 foreach(l, ((BitmapAnd *) plan)->bitmapplans)
    2533             :                 {
    2534          56 :                     context.paramids =
    2535          56 :                         bms_add_members(context.paramids,
    2536          56 :                                         finalize_plan(root,
    2537          56 :                                                       (Plan *) lfirst(l),
    2538             :                                                       gather_param,
    2539             :                                                       valid_params,
    2540             :                                                       scan_params));
    2541             :                 }
    2542             :             }
    2543          28 :             break;
    2544             : 
    2545          32 :         case T_BitmapOr:
    2546          32 :             {
    2547             :                 ListCell   *l;
    2548             : 
    2549          96 :                 foreach(l, ((BitmapOr *) plan)->bitmapplans)
    2550             :                 {
    2551          64 :                     context.paramids =
    2552          64 :                         bms_add_members(context.paramids,
    2553          64 :                                         finalize_plan(root,
    2554          64 :                                                       (Plan *) lfirst(l),
    2555             :                                                       gather_param,
    2556             :                                                       valid_params,
    2557             :                                                       scan_params));
    2558             :                 }
    2559             :             }
    2560          32 :             break;
    2561             : 
    2562       28772 :         case T_NestLoop:
    2563       28772 :             {
    2564             :                 ListCell   *l;
    2565             : 
    2566       28772 :                 finalize_primnode((Node *) ((Join *) plan)->joinqual,
    2567             :                                   &context);
    2568             :                 /* collect set of params that will be passed to right child */
    2569       49276 :                 foreach(l, ((NestLoop *) plan)->nestParams)
    2570             :                 {
    2571       20504 :                     NestLoopParam *nlp = (NestLoopParam *) lfirst(l);
    2572             : 
    2573       20504 :                     nestloop_params = bms_add_member(nestloop_params,
    2574             :                                                      nlp->paramno);
    2575             :                 }
    2576             :             }
    2577       28772 :             break;
    2578             : 
    2579        1040 :         case T_MergeJoin:
    2580        1040 :             finalize_primnode((Node *) ((Join *) plan)->joinqual,
    2581             :                               &context);
    2582        1040 :             finalize_primnode((Node *) ((MergeJoin *) plan)->mergeclauses,
    2583             :                               &context);
    2584        1040 :             break;
    2585             : 
    2586       15126 :         case T_HashJoin:
    2587       15126 :             finalize_primnode((Node *) ((Join *) plan)->joinqual,
    2588             :                               &context);
    2589       15126 :             finalize_primnode((Node *) ((HashJoin *) plan)->hashclauses,
    2590             :                               &context);
    2591       15126 :             break;
    2592             : 
    2593        1672 :         case T_Limit:
    2594        1672 :             finalize_primnode(((Limit *) plan)->limitOffset,
    2595             :                               &context);
    2596        1672 :             finalize_primnode(((Limit *) plan)->limitCount,
    2597             :                               &context);
    2598        1672 :             break;
    2599             : 
    2600         332 :         case T_RecursiveUnion:
    2601             :             /* child nodes are allowed to reference wtParam */
    2602         332 :             locally_added_param = ((RecursiveUnion *) plan)->wtParam;
    2603         332 :             valid_params = bms_add_member(bms_copy(valid_params),
    2604             :                                           locally_added_param);
    2605             :             /* wtParam does *not* get added to scan_params */
    2606         332 :             break;
    2607             : 
    2608        4876 :         case T_LockRows:
    2609             :             /* Force descendant scan nodes to reference epqParam */
    2610        4876 :             locally_added_param = ((LockRows *) plan)->epqParam;
    2611        4876 :             valid_params = bms_add_member(bms_copy(valid_params),
    2612             :                                           locally_added_param);
    2613        4876 :             scan_params = bms_add_member(bms_copy(scan_params),
    2614             :                                          locally_added_param);
    2615        4876 :             break;
    2616             : 
    2617       13200 :         case T_Agg:
    2618       12292 :             {
    2619       13200 :                 Agg        *agg = (Agg *) plan;
    2620             : 
    2621             :                 /*
    2622             :                  * AGG_HASHED plans need to know which Params are referenced
    2623             :                  * in aggregate calls.  Do a separate scan to identify them.
    2624             :                  */
    2625       13200 :                 if (agg->aggstrategy == AGG_HASHED)
    2626             :                 {
    2627             :                     finalize_primnode_context aggcontext;
    2628             : 
    2629         908 :                     aggcontext.root = root;
    2630         908 :                     aggcontext.paramids = NULL;
    2631         908 :                     finalize_agg_primnode((Node *) agg->plan.targetlist,
    2632             :                                           &aggcontext);
    2633         908 :                     finalize_agg_primnode((Node *) agg->plan.qual,
    2634             :                                           &aggcontext);
    2635         908 :                     agg->aggParams = aggcontext.paramids;
    2636             :                 }
    2637             :             }
    2638       13200 :             break;
    2639             : 
    2640          40 :         case T_WindowAgg:
    2641          40 :             finalize_primnode(((WindowAgg *) plan)->startOffset,
    2642             :                               &context);
    2643          40 :             finalize_primnode(((WindowAgg *) plan)->endOffset,
    2644             :                               &context);
    2645          40 :             break;
    2646             : 
    2647         500 :         case T_Gather:
    2648             :             /* child nodes are allowed to reference rescan_param, if any */
    2649         500 :             locally_added_param = ((Gather *) plan)->rescan_param;
    2650         500 :             if (locally_added_param >= 0)
    2651             :             {
    2652         500 :                 valid_params = bms_add_member(bms_copy(valid_params),
    2653             :                                               locally_added_param);
    2654             : 
    2655             :                 /*
    2656             :                  * We currently don't support nested Gathers.  The issue so
    2657             :                  * far as this function is concerned would be how to identify
    2658             :                  * which child nodes depend on which Gather.
    2659             :                  */
    2660             :                 Assert(gather_param < 0);
    2661             :                 /* Pass down rescan_param to child parallel-aware nodes */
    2662         500 :                 gather_param = locally_added_param;
    2663             :             }
    2664             :             /* rescan_param does *not* get added to scan_params */
    2665         500 :             break;
    2666             : 
    2667         152 :         case T_GatherMerge:
    2668             :             /* child nodes are allowed to reference rescan_param, if any */
    2669         152 :             locally_added_param = ((GatherMerge *) plan)->rescan_param;
    2670         152 :             if (locally_added_param >= 0)
    2671             :             {
    2672         152 :                 valid_params = bms_add_member(bms_copy(valid_params),
    2673             :                                               locally_added_param);
    2674             : 
    2675             :                 /*
    2676             :                  * We currently don't support nested Gathers.  The issue so
    2677             :                  * far as this function is concerned would be how to identify
    2678             :                  * which child nodes depend on which Gather.
    2679             :                  */
    2680             :                 Assert(gather_param < 0);
    2681             :                 /* Pass down rescan_param to child parallel-aware nodes */
    2682         152 :                 gather_param = locally_added_param;
    2683             :             }
    2684             :             /* rescan_param does *not* get added to scan_params */
    2685         152 :             break;
    2686             : 
    2687       37116 :         case T_ProjectSet:
    2688             :         case T_Hash:
    2689             :         case T_Material:
    2690             :         case T_Sort:
    2691             :         case T_IncrementalSort:
    2692             :         case T_Unique:
    2693             :         case T_SetOp:
    2694             :         case T_Group:
    2695             :             /* no node-type-specific fields need fixing */
    2696       37116 :             break;
    2697             : 
    2698           0 :         default:
    2699           0 :             elog(ERROR, "unrecognized node type: %d",
    2700             :                  (int) nodeTag(plan));
    2701             :     }
    2702             : 
    2703             :     /* Process left and right child plans, if any */
    2704      406614 :     child_params = finalize_plan(root,
    2705      406614 :                                  plan->lefttree,
    2706             :                                  gather_param,
    2707             :                                  valid_params,
    2708             :                                  scan_params);
    2709      406614 :     context.paramids = bms_add_members(context.paramids, child_params);
    2710             : 
    2711      406614 :     if (nestloop_params)
    2712             :     {
    2713             :         /* right child can reference nestloop_params as well as valid_params */
    2714       19196 :         child_params = finalize_plan(root,
    2715       19196 :                                      plan->righttree,
    2716             :                                      gather_param,
    2717             :                                      bms_union(nestloop_params, valid_params),
    2718             :                                      scan_params);
    2719             :         /* ... and they don't count as parameters used at my level */
    2720       19196 :         child_params = bms_difference(child_params, nestloop_params);
    2721       19196 :         bms_free(nestloop_params);
    2722             :     }
    2723             :     else
    2724             :     {
    2725             :         /* easy case */
    2726      387418 :         child_params = finalize_plan(root,
    2727      387418 :                                      plan->righttree,
    2728             :                                      gather_param,
    2729             :                                      valid_params,
    2730             :                                      scan_params);
    2731             :     }
    2732      406614 :     context.paramids = bms_add_members(context.paramids, child_params);
    2733             : 
    2734             :     /*
    2735             :      * Any locally generated parameter doesn't count towards its generating
    2736             :      * plan node's external dependencies.  (Note: if we changed valid_params
    2737             :      * and/or scan_params, we leak those bitmapsets; not worth the notational
    2738             :      * trouble to clean them up.)
    2739             :      */
    2740      406614 :     if (locally_added_param >= 0)
    2741             :     {
    2742       78840 :         context.paramids = bms_del_member(context.paramids,
    2743             :                                           locally_added_param);
    2744             :     }
    2745             : 
    2746             :     /* Now we have all the paramids referenced in this node and children */
    2747             : 
    2748      406614 :     if (!bms_is_subset(context.paramids, valid_params))
    2749           0 :         elog(ERROR, "plan should not reference subplan's variable");
    2750             : 
    2751             :     /*
    2752             :      * The plan node's allParam and extParam fields should include all its
    2753             :      * referenced paramids, plus contributions from any child initPlans.
    2754             :      * However, any setParams of the initPlans should not be present in the
    2755             :      * parent node's extParams, only in its allParams.  (It's possible that
    2756             :      * some initPlans have extParams that are setParams of other initPlans.)
    2757             :      */
    2758             : 
    2759             :     /* allParam must include initplans' extParams and setParams */
    2760      406614 :     plan->allParam = bms_union(context.paramids, initExtParam);
    2761      406614 :     plan->allParam = bms_add_members(plan->allParam, initSetParam);
    2762             :     /* extParam must include any initplan extParams */
    2763      406614 :     plan->extParam = bms_union(context.paramids, initExtParam);
    2764             :     /* but not any initplan setParams */
    2765      406614 :     plan->extParam = bms_del_members(plan->extParam, initSetParam);
    2766             : 
    2767             :     /*
    2768             :      * For speed at execution time, make sure extParam/allParam are actually
    2769             :      * NULL if they are empty sets.
    2770             :      */
    2771      406614 :     if (bms_is_empty(plan->extParam))
    2772      248346 :         plan->extParam = NULL;
    2773      406614 :     if (bms_is_empty(plan->allParam))
    2774      238604 :         plan->allParam = NULL;
    2775             : 
    2776      406614 :     return plan->allParam;
    2777             : }
    2778             : 
    2779             : /*
    2780             :  * finalize_primnode: add IDs of all PARAM_EXEC params appearing in the given
    2781             :  * expression tree to the result set.
    2782             :  */
    2783             : static bool
    2784     7444554 : finalize_primnode(Node *node, finalize_primnode_context *context)
    2785             : {
    2786     7444554 :     if (node == NULL)
    2787      884458 :         return false;
    2788     6560096 :     if (IsA(node, Param))
    2789             :     {
    2790      100758 :         if (((Param *) node)->paramkind == PARAM_EXEC)
    2791             :         {
    2792       99106 :             int         paramid = ((Param *) node)->paramid;
    2793             : 
    2794       99106 :             context->paramids = bms_add_member(context->paramids, paramid);
    2795             :         }
    2796      100758 :         return false;           /* no more to do here */
    2797             :     }
    2798     6459338 :     if (IsA(node, SubPlan))
    2799             :     {
    2800       48294 :         SubPlan    *subplan = (SubPlan *) node;
    2801       48294 :         Plan       *plan = planner_subplan_get_plan(context->root, subplan);
    2802             :         ListCell   *lc;
    2803             :         Bitmapset  *subparamids;
    2804             : 
    2805             :         /* Recurse into the testexpr, but not into the Plan */
    2806       48294 :         finalize_primnode(subplan->testexpr, context);
    2807             : 
    2808             :         /*
    2809             :          * Remove any param IDs of output parameters of the subplan that were
    2810             :          * referenced in the testexpr.  These are not interesting for
    2811             :          * parameter change signaling since we always re-evaluate the subplan.
    2812             :          * Note that this wouldn't work too well if there might be uses of the
    2813             :          * same param IDs elsewhere in the plan, but that can't happen because
    2814             :          * generate_new_exec_param never tries to merge params.
    2815             :          */
    2816       50234 :         foreach(lc, subplan->paramIds)
    2817             :         {
    2818        1940 :             context->paramids = bms_del_member(context->paramids,
    2819             :                                                lfirst_int(lc));
    2820             :         }
    2821             : 
    2822             :         /* Also examine args list */
    2823       48294 :         finalize_primnode((Node *) subplan->args, context);
    2824             : 
    2825             :         /*
    2826             :          * Add params needed by the subplan to paramids, but excluding those
    2827             :          * we will pass down to it.  (We assume SS_finalize_plan was run on
    2828             :          * the subplan already.)
    2829             :          */
    2830       48294 :         subparamids = bms_copy(plan->extParam);
    2831      116904 :         foreach(lc, subplan->parParam)
    2832             :         {
    2833       68610 :             subparamids = bms_del_member(subparamids, lfirst_int(lc));
    2834             :         }
    2835       48294 :         context->paramids = bms_join(context->paramids, subparamids);
    2836             : 
    2837       48294 :         return false;           /* no more to do here */
    2838             :     }
    2839     6411044 :     return expression_tree_walker(node, finalize_primnode,
    2840             :                                   (void *) context);
    2841             : }
    2842             : 
    2843             : /*
    2844             :  * finalize_agg_primnode: find all Aggref nodes in the given expression tree,
    2845             :  * and add IDs of all PARAM_EXEC params appearing within their aggregated
    2846             :  * arguments to the result set.
    2847             :  */
    2848             : static bool
    2849        6028 : finalize_agg_primnode(Node *node, finalize_primnode_context *context)
    2850             : {
    2851        6028 :     if (node == NULL)
    2852         876 :         return false;
    2853        5152 :     if (IsA(node, Aggref))
    2854             :     {
    2855         632 :         Aggref     *agg = (Aggref *) node;
    2856             : 
    2857             :         /* we should not consider the direct arguments, if any */
    2858         632 :         finalize_primnode((Node *) agg->args, context);
    2859         632 :         finalize_primnode((Node *) agg->aggfilter, context);
    2860         632 :         return false;           /* there can't be any Aggrefs below here */
    2861             :     }
    2862        4520 :     return expression_tree_walker(node, finalize_agg_primnode,
    2863             :                                   (void *) context);
    2864             : }
    2865             : 
    2866             : /*
    2867             :  * SS_make_initplan_output_param - make a Param for an initPlan's output
    2868             :  *
    2869             :  * The plan is expected to return a scalar value of the given type/collation.
    2870             :  *
    2871             :  * Note that in some cases the initplan may not ever appear in the finished
    2872             :  * plan tree.  If that happens, we'll have wasted a PARAM_EXEC slot, which
    2873             :  * is no big deal.
    2874             :  */
    2875             : Param *
    2876         422 : SS_make_initplan_output_param(PlannerInfo *root,
    2877             :                               Oid resulttype, int32 resulttypmod,
    2878             :                               Oid resultcollation)
    2879             : {
    2880         422 :     return generate_new_exec_param(root, resulttype,
    2881             :                                    resulttypmod, resultcollation);
    2882             : }
    2883             : 
    2884             : /*
    2885             :  * SS_make_initplan_from_plan - given a plan tree, make it an InitPlan
    2886             :  *
    2887             :  * We build an EXPR_SUBLINK SubPlan node and put it into the initplan
    2888             :  * list for the outer query level.  A Param that represents the initplan's
    2889             :  * output has already been assigned using SS_make_initplan_output_param.
    2890             :  */
    2891             : void
    2892         360 : SS_make_initplan_from_plan(PlannerInfo *root,
    2893             :                            PlannerInfo *subroot, Plan *plan,
    2894             :                            Param *prm)
    2895             : {
    2896             :     SubPlan    *node;
    2897             : 
    2898             :     /*
    2899             :      * Add the subplan and its PlannerInfo to the global lists.
    2900             :      */
    2901         360 :     root->glob->subplans = lappend(root->glob->subplans, plan);
    2902         360 :     root->glob->subroots = lappend(root->glob->subroots, subroot);
    2903             : 
    2904             :     /*
    2905             :      * Create a SubPlan node and add it to the outer list of InitPlans. Note
    2906             :      * it has to appear after any other InitPlans it might depend on (see
    2907             :      * comments in ExecReScan).
    2908             :      */
    2909         360 :     node = makeNode(SubPlan);
    2910         360 :     node->subLinkType = EXPR_SUBLINK;
    2911         360 :     node->plan_id = list_length(root->glob->subplans);
    2912         360 :     node->plan_name = psprintf("InitPlan %d (returns $%d)",
    2913             :                                node->plan_id, prm->paramid);
    2914         360 :     get_first_col_type(plan, &node->firstColType, &node->firstColTypmod,
    2915             :                        &node->firstColCollation);
    2916         360 :     node->setParam = list_make1_int(prm->paramid);
    2917             : 
    2918         360 :     root->init_plans = lappend(root->init_plans, node);
    2919             : 
    2920             :     /*
    2921             :      * The node can't have any inputs (since it's an initplan), so the
    2922             :      * parParam and args lists remain empty.
    2923             :      */
    2924             : 
    2925             :     /* Set costs of SubPlan using info from the plan tree */
    2926         360 :     cost_subplan(subroot, node, plan);
    2927         360 : }

Generated by: LCOV version 1.13