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

Generated by: LCOV version 1.13