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

Generated by: LCOV version 1.14