LCOV - code coverage report
Current view: top level - src/backend/optimizer/util - clauses.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 1406 1596 88.1 %
Date: 2019-08-24 06:06:56 Functions: 67 68 98.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * clauses.c
       4             :  *    routines to manipulate qualification clauses
       5             :  *
       6             :  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/optimizer/util/clauses.c
      12             :  *
      13             :  * HISTORY
      14             :  *    AUTHOR            DATE            MAJOR EVENT
      15             :  *    Andrew Yu         Nov 3, 1994     clause.c and clauses.c combined
      16             :  *
      17             :  *-------------------------------------------------------------------------
      18             :  */
      19             : 
      20             : #include "postgres.h"
      21             : 
      22             : #include "access/htup_details.h"
      23             : #include "catalog/pg_aggregate.h"
      24             : #include "catalog/pg_class.h"
      25             : #include "catalog/pg_language.h"
      26             : #include "catalog/pg_operator.h"
      27             : #include "catalog/pg_proc.h"
      28             : #include "catalog/pg_type.h"
      29             : #include "executor/executor.h"
      30             : #include "executor/functions.h"
      31             : #include "funcapi.h"
      32             : #include "miscadmin.h"
      33             : #include "nodes/makefuncs.h"
      34             : #include "nodes/nodeFuncs.h"
      35             : #include "nodes/supportnodes.h"
      36             : #include "optimizer/clauses.h"
      37             : #include "optimizer/cost.h"
      38             : #include "optimizer/optimizer.h"
      39             : #include "optimizer/plancat.h"
      40             : #include "optimizer/planmain.h"
      41             : #include "parser/analyze.h"
      42             : #include "parser/parse_agg.h"
      43             : #include "parser/parse_coerce.h"
      44             : #include "parser/parse_func.h"
      45             : #include "rewrite/rewriteManip.h"
      46             : #include "tcop/tcopprot.h"
      47             : #include "utils/acl.h"
      48             : #include "utils/builtins.h"
      49             : #include "utils/datum.h"
      50             : #include "utils/fmgroids.h"
      51             : #include "utils/lsyscache.h"
      52             : #include "utils/memutils.h"
      53             : #include "utils/syscache.h"
      54             : #include "utils/typcache.h"
      55             : 
      56             : 
      57             : typedef struct
      58             : {
      59             :     PlannerInfo *root;
      60             :     AggSplit    aggsplit;
      61             :     AggClauseCosts *costs;
      62             : } get_agg_clause_costs_context;
      63             : 
      64             : typedef struct
      65             : {
      66             :     ParamListInfo boundParams;
      67             :     PlannerInfo *root;
      68             :     List       *active_fns;
      69             :     Node       *case_val;
      70             :     bool        estimate;
      71             : } eval_const_expressions_context;
      72             : 
      73             : typedef struct
      74             : {
      75             :     int         nargs;
      76             :     List       *args;
      77             :     int        *usecounts;
      78             : } substitute_actual_parameters_context;
      79             : 
      80             : typedef struct
      81             : {
      82             :     int         nargs;
      83             :     List       *args;
      84             :     int         sublevels_up;
      85             : } substitute_actual_srf_parameters_context;
      86             : 
      87             : typedef struct
      88             : {
      89             :     char       *proname;
      90             :     char       *prosrc;
      91             : } inline_error_callback_arg;
      92             : 
      93             : typedef struct
      94             : {
      95             :     char        max_hazard;     /* worst proparallel hazard found so far */
      96             :     char        max_interesting;    /* worst proparallel hazard of interest */
      97             :     List       *safe_param_ids; /* PARAM_EXEC Param IDs to treat as safe */
      98             : } max_parallel_hazard_context;
      99             : 
     100             : static bool contain_agg_clause_walker(Node *node, void *context);
     101             : static bool get_agg_clause_costs_walker(Node *node,
     102             :                                         get_agg_clause_costs_context *context);
     103             : static bool find_window_functions_walker(Node *node, WindowFuncLists *lists);
     104             : static bool contain_subplans_walker(Node *node, void *context);
     105             : static bool contain_mutable_functions_walker(Node *node, void *context);
     106             : static bool contain_volatile_functions_walker(Node *node, void *context);
     107             : static bool contain_volatile_functions_not_nextval_walker(Node *node, void *context);
     108             : static bool max_parallel_hazard_walker(Node *node,
     109             :                                        max_parallel_hazard_context *context);
     110             : static bool contain_nonstrict_functions_walker(Node *node, void *context);
     111             : static bool contain_context_dependent_node(Node *clause);
     112             : static bool contain_context_dependent_node_walker(Node *node, int *flags);
     113             : static bool contain_leaked_vars_walker(Node *node, void *context);
     114             : static Relids find_nonnullable_rels_walker(Node *node, bool top_level);
     115             : static List *find_nonnullable_vars_walker(Node *node, bool top_level);
     116             : static bool is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK);
     117             : static Node *eval_const_expressions_mutator(Node *node,
     118             :                                             eval_const_expressions_context *context);
     119             : static bool contain_non_const_walker(Node *node, void *context);
     120             : static bool ece_function_is_safe(Oid funcid,
     121             :                                  eval_const_expressions_context *context);
     122             : static List *simplify_or_arguments(List *args,
     123             :                                    eval_const_expressions_context *context,
     124             :                                    bool *haveNull, bool *forceTrue);
     125             : static List *simplify_and_arguments(List *args,
     126             :                                     eval_const_expressions_context *context,
     127             :                                     bool *haveNull, bool *forceFalse);
     128             : static Node *simplify_boolean_equality(Oid opno, List *args);
     129             : static Expr *simplify_function(Oid funcid,
     130             :                                Oid result_type, int32 result_typmod,
     131             :                                Oid result_collid, Oid input_collid, List **args_p,
     132             :                                bool funcvariadic, bool process_args, bool allow_non_const,
     133             :                                eval_const_expressions_context *context);
     134             : static List *reorder_function_arguments(List *args, HeapTuple func_tuple);
     135             : static List *add_function_defaults(List *args, HeapTuple func_tuple);
     136             : static List *fetch_function_defaults(HeapTuple func_tuple);
     137             : static void recheck_cast_function_args(List *args, Oid result_type,
     138             :                                        HeapTuple func_tuple);
     139             : static Expr *evaluate_function(Oid funcid, Oid result_type, int32 result_typmod,
     140             :                                Oid result_collid, Oid input_collid, List *args,
     141             :                                bool funcvariadic,
     142             :                                HeapTuple func_tuple,
     143             :                                eval_const_expressions_context *context);
     144             : static Expr *inline_function(Oid funcid, Oid result_type, Oid result_collid,
     145             :                              Oid input_collid, List *args,
     146             :                              bool funcvariadic,
     147             :                              HeapTuple func_tuple,
     148             :                              eval_const_expressions_context *context);
     149             : static Node *substitute_actual_parameters(Node *expr, int nargs, List *args,
     150             :                                           int *usecounts);
     151             : static Node *substitute_actual_parameters_mutator(Node *node,
     152             :                                                   substitute_actual_parameters_context *context);
     153             : static void sql_inline_error_callback(void *arg);
     154             : static Query *substitute_actual_srf_parameters(Query *expr,
     155             :                                                int nargs, List *args);
     156             : static Node *substitute_actual_srf_parameters_mutator(Node *node,
     157             :                                                       substitute_actual_srf_parameters_context *context);
     158             : static bool tlist_matches_coltypelist(List *tlist, List *coltypelist);
     159             : 
     160             : 
     161             : /*****************************************************************************
     162             :  *      Aggregate-function clause manipulation
     163             :  *****************************************************************************/
     164             : 
     165             : /*
     166             :  * contain_agg_clause
     167             :  *    Recursively search for Aggref/GroupingFunc nodes within a clause.
     168             :  *
     169             :  *    Returns true if any aggregate found.
     170             :  *
     171             :  * This does not descend into subqueries, and so should be used only after
     172             :  * reduction of sublinks to subplans, or in contexts where it's known there
     173             :  * are no subqueries.  There mustn't be outer-aggregate references either.
     174             :  *
     175             :  * (If you want something like this but able to deal with subqueries,
     176             :  * see rewriteManip.c's contain_aggs_of_level().)
     177             :  */
     178             : bool
     179        1082 : contain_agg_clause(Node *clause)
     180             : {
     181        1082 :     return contain_agg_clause_walker(clause, NULL);
     182             : }
     183             : 
     184             : static bool
     185        1656 : contain_agg_clause_walker(Node *node, void *context)
     186             : {
     187        1656 :     if (node == NULL)
     188          12 :         return false;
     189        1644 :     if (IsA(node, Aggref))
     190             :     {
     191             :         Assert(((Aggref *) node)->agglevelsup == 0);
     192         386 :         return true;            /* abort the tree traversal and return true */
     193             :     }
     194        1258 :     if (IsA(node, GroupingFunc))
     195             :     {
     196             :         Assert(((GroupingFunc *) node)->agglevelsup == 0);
     197           8 :         return true;            /* abort the tree traversal and return true */
     198             :     }
     199             :     Assert(!IsA(node, SubLink));
     200        1250 :     return expression_tree_walker(node, contain_agg_clause_walker, context);
     201             : }
     202             : 
     203             : /*
     204             :  * get_agg_clause_costs
     205             :  *    Recursively find the Aggref nodes in an expression tree, and
     206             :  *    accumulate cost information about them.
     207             :  *
     208             :  * 'aggsplit' tells us the expected partial-aggregation mode, which affects
     209             :  * the cost estimates.
     210             :  *
     211             :  * NOTE that the counts/costs are ADDED to those already in *costs ... so
     212             :  * the caller is responsible for zeroing the struct initially.
     213             :  *
     214             :  * We count the nodes, estimate their execution costs, and estimate the total
     215             :  * space needed for their transition state values if all are evaluated in
     216             :  * parallel (as would be done in a HashAgg plan).  Also, we check whether
     217             :  * partial aggregation is feasible.  See AggClauseCosts for the exact set
     218             :  * of statistics collected.
     219             :  *
     220             :  * In addition, we mark Aggref nodes with the correct aggtranstype, so
     221             :  * that that doesn't need to be done repeatedly.  (That makes this function's
     222             :  * name a bit of a misnomer.)
     223             :  *
     224             :  * This does not descend into subqueries, and so should be used only after
     225             :  * reduction of sublinks to subplans, or in contexts where it's known there
     226             :  * are no subqueries.  There mustn't be outer-aggregate references either.
     227             :  */
     228             : void
     229       49324 : get_agg_clause_costs(PlannerInfo *root, Node *clause, AggSplit aggsplit,
     230             :                      AggClauseCosts *costs)
     231             : {
     232             :     get_agg_clause_costs_context context;
     233             : 
     234       49324 :     context.root = root;
     235       49324 :     context.aggsplit = aggsplit;
     236       49324 :     context.costs = costs;
     237       49324 :     (void) get_agg_clause_costs_walker(clause, &context);
     238       49324 : }
     239             : 
     240             : static bool
     241      115726 : get_agg_clause_costs_walker(Node *node, get_agg_clause_costs_context *context)
     242             : {
     243      115726 :     if (node == NULL)
     244       24180 :         return false;
     245       91546 :     if (IsA(node, Aggref))
     246             :     {
     247       27978 :         Aggref     *aggref = (Aggref *) node;
     248       27978 :         AggClauseCosts *costs = context->costs;
     249             :         HeapTuple   aggTuple;
     250             :         Form_pg_aggregate aggform;
     251             :         Oid         aggtransfn;
     252             :         Oid         aggfinalfn;
     253             :         Oid         aggcombinefn;
     254             :         Oid         aggserialfn;
     255             :         Oid         aggdeserialfn;
     256             :         Oid         aggtranstype;
     257             :         int32       aggtransspace;
     258             :         QualCost    argcosts;
     259             : 
     260             :         Assert(aggref->agglevelsup == 0);
     261             : 
     262             :         /*
     263             :          * Fetch info about aggregate from pg_aggregate.  Note it's correct to
     264             :          * ignore the moving-aggregate variant, since what we're concerned
     265             :          * with here is aggregates not window functions.
     266             :          */
     267       27978 :         aggTuple = SearchSysCache1(AGGFNOID,
     268       27978 :                                    ObjectIdGetDatum(aggref->aggfnoid));
     269       27978 :         if (!HeapTupleIsValid(aggTuple))
     270           0 :             elog(ERROR, "cache lookup failed for aggregate %u",
     271             :                  aggref->aggfnoid);
     272       27978 :         aggform = (Form_pg_aggregate) GETSTRUCT(aggTuple);
     273       27978 :         aggtransfn = aggform->aggtransfn;
     274       27978 :         aggfinalfn = aggform->aggfinalfn;
     275       27978 :         aggcombinefn = aggform->aggcombinefn;
     276       27978 :         aggserialfn = aggform->aggserialfn;
     277       27978 :         aggdeserialfn = aggform->aggdeserialfn;
     278       27978 :         aggtranstype = aggform->aggtranstype;
     279       27978 :         aggtransspace = aggform->aggtransspace;
     280       27978 :         ReleaseSysCache(aggTuple);
     281             : 
     282             :         /*
     283             :          * Resolve the possibly-polymorphic aggregate transition type, unless
     284             :          * already done in a previous pass over the expression.
     285             :          */
     286       27978 :         if (OidIsValid(aggref->aggtranstype))
     287        2492 :             aggtranstype = aggref->aggtranstype;
     288             :         else
     289             :         {
     290             :             Oid         inputTypes[FUNC_MAX_ARGS];
     291             :             int         numArguments;
     292             : 
     293             :             /* extract argument types (ignoring any ORDER BY expressions) */
     294       25486 :             numArguments = get_aggregate_argtypes(aggref, inputTypes);
     295             : 
     296             :             /* resolve actual type of transition state, if polymorphic */
     297       25486 :             aggtranstype = resolve_aggregate_transtype(aggref->aggfnoid,
     298             :                                                        aggtranstype,
     299             :                                                        inputTypes,
     300             :                                                        numArguments);
     301       25486 :             aggref->aggtranstype = aggtranstype;
     302             :         }
     303             : 
     304             :         /*
     305             :          * Count it, and check for cases requiring ordered input.  Note that
     306             :          * ordered-set aggs always have nonempty aggorder.  Any ordered-input
     307             :          * case also defeats partial aggregation.
     308             :          */
     309       27978 :         costs->numAggs++;
     310       27978 :         if (aggref->aggorder != NIL || aggref->aggdistinct != NIL)
     311             :         {
     312        9188 :             costs->numOrderedAggs++;
     313        9188 :             costs->hasNonPartial = true;
     314             :         }
     315             : 
     316             :         /*
     317             :          * Check whether partial aggregation is feasible, unless we already
     318             :          * found out that we can't do it.
     319             :          */
     320       27978 :         if (!costs->hasNonPartial)
     321             :         {
     322             :             /*
     323             :              * If there is no combine function, then partial aggregation is
     324             :              * not possible.
     325             :              */
     326       18654 :             if (!OidIsValid(aggcombinefn))
     327        6122 :                 costs->hasNonPartial = true;
     328             : 
     329             :             /*
     330             :              * If we have any aggs with transtype INTERNAL then we must check
     331             :              * whether they have serialization/deserialization functions; if
     332             :              * not, we can't serialize partial-aggregation results.
     333             :              */
     334       12532 :             else if (aggtranstype == INTERNALOID &&
     335         376 :                      (!OidIsValid(aggserialfn) || !OidIsValid(aggdeserialfn)))
     336           0 :                 costs->hasNonSerial = true;
     337             :         }
     338             : 
     339             :         /*
     340             :          * Add the appropriate component function execution costs to
     341             :          * appropriate totals.
     342             :          */
     343       27978 :         if (DO_AGGSPLIT_COMBINE(context->aggsplit))
     344             :         {
     345             :             /* charge for combining previously aggregated states */
     346        1134 :             add_function_cost(context->root, aggcombinefn, NULL,
     347             :                               &costs->transCost);
     348             :         }
     349             :         else
     350       26844 :             add_function_cost(context->root, aggtransfn, NULL,
     351             :                               &costs->transCost);
     352       27978 :         if (DO_AGGSPLIT_DESERIALIZE(context->aggsplit) &&
     353             :             OidIsValid(aggdeserialfn))
     354          24 :             add_function_cost(context->root, aggdeserialfn, NULL,
     355             :                               &costs->transCost);
     356       27978 :         if (DO_AGGSPLIT_SERIALIZE(context->aggsplit) &&
     357             :             OidIsValid(aggserialfn))
     358          24 :             add_function_cost(context->root, aggserialfn, NULL,
     359             :                               &costs->finalCost);
     360       27978 :         if (!DO_AGGSPLIT_SKIPFINAL(context->aggsplit) &&
     361             :             OidIsValid(aggfinalfn))
     362       16540 :             add_function_cost(context->root, aggfinalfn, NULL,
     363             :                               &costs->finalCost);
     364             : 
     365             :         /*
     366             :          * These costs are incurred only by the initial aggregate node, so we
     367             :          * mustn't include them again at upper levels.
     368             :          */
     369       27978 :         if (!DO_AGGSPLIT_COMBINE(context->aggsplit))
     370             :         {
     371             :             /* add the input expressions' cost to per-input-row costs */
     372       26844 :             cost_qual_eval_node(&argcosts, (Node *) aggref->args, context->root);
     373       26844 :             costs->transCost.startup += argcosts.startup;
     374       26844 :             costs->transCost.per_tuple += argcosts.per_tuple;
     375             : 
     376             :             /*
     377             :              * Add any filter's cost to per-input-row costs.
     378             :              *
     379             :              * XXX Ideally we should reduce input expression costs according
     380             :              * to filter selectivity, but it's not clear it's worth the
     381             :              * trouble.
     382             :              */
     383       26844 :             if (aggref->aggfilter)
     384             :             {
     385         498 :                 cost_qual_eval_node(&argcosts, (Node *) aggref->aggfilter,
     386             :                                     context->root);
     387         498 :                 costs->transCost.startup += argcosts.startup;
     388         498 :                 costs->transCost.per_tuple += argcosts.per_tuple;
     389             :             }
     390             :         }
     391             : 
     392             :         /*
     393             :          * If there are direct arguments, treat their evaluation cost like the
     394             :          * cost of the finalfn.
     395             :          */
     396       27978 :         if (aggref->aggdirectargs)
     397             :         {
     398         176 :             cost_qual_eval_node(&argcosts, (Node *) aggref->aggdirectargs,
     399             :                                 context->root);
     400         176 :             costs->finalCost.startup += argcosts.startup;
     401         176 :             costs->finalCost.per_tuple += argcosts.per_tuple;
     402             :         }
     403             : 
     404             :         /*
     405             :          * If the transition type is pass-by-value then it doesn't add
     406             :          * anything to the required size of the hashtable.  If it is
     407             :          * pass-by-reference then we have to add the estimated size of the
     408             :          * value itself, plus palloc overhead.
     409             :          */
     410       27978 :         if (!get_typbyval(aggtranstype))
     411             :         {
     412             :             int32       avgwidth;
     413             : 
     414             :             /* Use average width if aggregate definition gave one */
     415        1936 :             if (aggtransspace > 0)
     416          60 :                 avgwidth = aggtransspace;
     417        1876 :             else if (aggtransfn == F_ARRAY_APPEND)
     418             :             {
     419             :                 /*
     420             :                  * If the transition function is array_append(), it'll use an
     421             :                  * expanded array as transvalue, which will occupy at least
     422             :                  * ALLOCSET_SMALL_INITSIZE and possibly more.  Use that as the
     423             :                  * estimate for lack of a better idea.
     424             :                  */
     425           8 :                 avgwidth = ALLOCSET_SMALL_INITSIZE;
     426             :             }
     427             :             else
     428             :             {
     429             :                 /*
     430             :                  * If transition state is of same type as first aggregated
     431             :                  * input, assume it's the same typmod (same width) as well.
     432             :                  * This works for cases like MAX/MIN and is probably somewhat
     433             :                  * reasonable otherwise.
     434             :                  */
     435        1868 :                 int32       aggtranstypmod = -1;
     436             : 
     437        1868 :                 if (aggref->args)
     438             :                 {
     439        1844 :                     TargetEntry *tle = (TargetEntry *) linitial(aggref->args);
     440             : 
     441        1844 :                     if (aggtranstype == exprType((Node *) tle->expr))
     442         270 :                         aggtranstypmod = exprTypmod((Node *) tle->expr);
     443             :                 }
     444             : 
     445        1868 :                 avgwidth = get_typavgwidth(aggtranstype, aggtranstypmod);
     446             :             }
     447             : 
     448        1936 :             avgwidth = MAXALIGN(avgwidth);
     449        1936 :             costs->transitionSpace += avgwidth + 2 * sizeof(void *);
     450             :         }
     451       26042 :         else if (aggtranstype == INTERNALOID)
     452             :         {
     453             :             /*
     454             :              * INTERNAL transition type is a special case: although INTERNAL
     455             :              * is pass-by-value, it's almost certainly being used as a pointer
     456             :              * to some large data structure.  The aggregate definition can
     457             :              * provide an estimate of the size.  If it doesn't, then we assume
     458             :              * ALLOCSET_DEFAULT_INITSIZE, which is a good guess if the data is
     459             :              * being kept in a private memory context, as is done by
     460             :              * array_agg() for instance.
     461             :              */
     462       15304 :             if (aggtransspace > 0)
     463         376 :                 costs->transitionSpace += aggtransspace;
     464             :             else
     465       14928 :                 costs->transitionSpace += ALLOCSET_DEFAULT_INITSIZE;
     466             :         }
     467             : 
     468             :         /*
     469             :          * We assume that the parser checked that there are no aggregates (of
     470             :          * this level anyway) in the aggregated arguments, direct arguments,
     471             :          * or filter clause.  Hence, we need not recurse into any of them.
     472             :          */
     473       27978 :         return false;
     474             :     }
     475             :     Assert(!IsA(node, SubLink));
     476       63568 :     return expression_tree_walker(node, get_agg_clause_costs_walker,
     477             :                                   (void *) context);
     478             : }
     479             : 
     480             : 
     481             : /*****************************************************************************
     482             :  *      Window-function clause manipulation
     483             :  *****************************************************************************/
     484             : 
     485             : /*
     486             :  * contain_window_function
     487             :  *    Recursively search for WindowFunc nodes within a clause.
     488             :  *
     489             :  * Since window functions don't have level fields, but are hard-wired to
     490             :  * be associated with the current query level, this is just the same as
     491             :  * rewriteManip.c's function.
     492             :  */
     493             : bool
     494         572 : contain_window_function(Node *clause)
     495             : {
     496         572 :     return contain_windowfuncs(clause);
     497             : }
     498             : 
     499             : /*
     500             :  * find_window_functions
     501             :  *    Locate all the WindowFunc nodes in an expression tree, and organize
     502             :  *    them by winref ID number.
     503             :  *
     504             :  * Caller must provide an upper bound on the winref IDs expected in the tree.
     505             :  */
     506             : WindowFuncLists *
     507        1104 : find_window_functions(Node *clause, Index maxWinRef)
     508             : {
     509        1104 :     WindowFuncLists *lists = palloc(sizeof(WindowFuncLists));
     510             : 
     511        1104 :     lists->numWindowFuncs = 0;
     512        1104 :     lists->maxWinRef = maxWinRef;
     513        1104 :     lists->windowFuncs = (List **) palloc0((maxWinRef + 1) * sizeof(List *));
     514        1104 :     (void) find_window_functions_walker(clause, lists);
     515        1104 :     return lists;
     516             : }
     517             : 
     518             : static bool
     519        9856 : find_window_functions_walker(Node *node, WindowFuncLists *lists)
     520             : {
     521        9856 :     if (node == NULL)
     522          96 :         return false;
     523        9760 :     if (IsA(node, WindowFunc))
     524             :     {
     525        1400 :         WindowFunc *wfunc = (WindowFunc *) node;
     526             : 
     527             :         /* winref is unsigned, so one-sided test is OK */
     528        1400 :         if (wfunc->winref > lists->maxWinRef)
     529           0 :             elog(ERROR, "WindowFunc contains out-of-range winref %u",
     530             :                  wfunc->winref);
     531             :         /* eliminate duplicates, so that we avoid repeated computation */
     532        1400 :         if (!list_member(lists->windowFuncs[wfunc->winref], wfunc))
     533             :         {
     534        2784 :             lists->windowFuncs[wfunc->winref] =
     535        1392 :                 lappend(lists->windowFuncs[wfunc->winref], wfunc);
     536        1392 :             lists->numWindowFuncs++;
     537             :         }
     538             : 
     539             :         /*
     540             :          * We assume that the parser checked that there are no window
     541             :          * functions in the arguments or filter clause.  Hence, we need not
     542             :          * recurse into them.  (If either the parser or the planner screws up
     543             :          * on this point, the executor will still catch it; see ExecInitExpr.)
     544             :          */
     545        1400 :         return false;
     546             :     }
     547             :     Assert(!IsA(node, SubLink));
     548        8360 :     return expression_tree_walker(node, find_window_functions_walker,
     549             :                                   (void *) lists);
     550             : }
     551             : 
     552             : 
     553             : /*****************************************************************************
     554             :  *      Support for expressions returning sets
     555             :  *****************************************************************************/
     556             : 
     557             : /*
     558             :  * expression_returns_set_rows
     559             :  *    Estimate the number of rows returned by a set-returning expression.
     560             :  *    The result is 1 if it's not a set-returning expression.
     561             :  *
     562             :  * We should only examine the top-level function or operator; it used to be
     563             :  * appropriate to recurse, but not anymore.  (Even if there are more SRFs in
     564             :  * the function's inputs, their multipliers are accounted for separately.)
     565             :  *
     566             :  * Note: keep this in sync with expression_returns_set() in nodes/nodeFuncs.c.
     567             :  */
     568             : double
     569       42134 : expression_returns_set_rows(PlannerInfo *root, Node *clause)
     570             : {
     571       42134 :     if (clause == NULL)
     572           0 :         return 1.0;
     573       42134 :     if (IsA(clause, FuncExpr))
     574             :     {
     575       32286 :         FuncExpr   *expr = (FuncExpr *) clause;
     576             : 
     577       32286 :         if (expr->funcretset)
     578       29950 :             return clamp_row_est(get_function_rows(root, expr->funcid, clause));
     579             :     }
     580       12184 :     if (IsA(clause, OpExpr))
     581             :     {
     582         320 :         OpExpr     *expr = (OpExpr *) clause;
     583             : 
     584         320 :         if (expr->opretset)
     585             :         {
     586           4 :             set_opfuncid(expr);
     587           4 :             return clamp_row_est(get_function_rows(root, expr->opfuncid, clause));
     588             :         }
     589             :     }
     590       12180 :     return 1.0;
     591             : }
     592             : 
     593             : 
     594             : /*****************************************************************************
     595             :  *      Subplan clause manipulation
     596             :  *****************************************************************************/
     597             : 
     598             : /*
     599             :  * contain_subplans
     600             :  *    Recursively search for subplan nodes within a clause.
     601             :  *
     602             :  * If we see a SubLink node, we will return true.  This is only possible if
     603             :  * the expression tree hasn't yet been transformed by subselect.c.  We do not
     604             :  * know whether the node will produce a true subplan or just an initplan,
     605             :  * but we make the conservative assumption that it will be a subplan.
     606             :  *
     607             :  * Returns true if any subplan found.
     608             :  */
     609             : bool
     610       22168 : contain_subplans(Node *clause)
     611             : {
     612       22168 :     return contain_subplans_walker(clause, NULL);
     613             : }
     614             : 
     615             : static bool
     616       83190 : contain_subplans_walker(Node *node, void *context)
     617             : {
     618       83190 :     if (node == NULL)
     619        1956 :         return false;
     620      162424 :     if (IsA(node, SubPlan) ||
     621      162380 :         IsA(node, AlternativeSubPlan) ||
     622       81190 :         IsA(node, SubLink))
     623         172 :         return true;            /* abort the tree traversal and return true */
     624       81062 :     return expression_tree_walker(node, contain_subplans_walker, context);
     625             : }
     626             : 
     627             : 
     628             : /*****************************************************************************
     629             :  *      Check clauses for mutable functions
     630             :  *****************************************************************************/
     631             : 
     632             : /*
     633             :  * contain_mutable_functions
     634             :  *    Recursively search for mutable functions within a clause.
     635             :  *
     636             :  * Returns true if any mutable function (or operator implemented by a
     637             :  * mutable function) is found.  This test is needed so that we don't
     638             :  * mistakenly think that something like "WHERE random() < 0.5" can be treated
     639             :  * as a constant qualification.
     640             :  *
     641             :  * We will recursively look into Query nodes (i.e., SubLink sub-selects)
     642             :  * but not into SubPlans.  See comments for contain_volatile_functions().
     643             :  */
     644             : bool
     645       59648 : contain_mutable_functions(Node *clause)
     646             : {
     647       59648 :     return contain_mutable_functions_walker(clause, NULL);
     648             : }
     649             : 
     650             : static bool
     651       47998 : contain_mutable_functions_checker(Oid func_id, void *context)
     652             : {
     653       47998 :     return (func_volatile(func_id) != PROVOLATILE_IMMUTABLE);
     654             : }
     655             : 
     656             : static bool
     657      168660 : contain_mutable_functions_walker(Node *node, void *context)
     658             : {
     659      168660 :     if (node == NULL)
     660        1178 :         return false;
     661             :     /* Check for mutable functions in node itself */
     662      167482 :     if (check_functions_in_node(node, contain_mutable_functions_checker,
     663             :                                 context))
     664        2790 :         return true;
     665             : 
     666      164692 :     if (IsA(node, SQLValueFunction))
     667             :     {
     668             :         /* all variants of SQLValueFunction are stable */
     669          72 :         return true;
     670             :     }
     671             : 
     672      164620 :     if (IsA(node, NextValueExpr))
     673             :     {
     674             :         /* NextValueExpr is volatile */
     675           0 :         return true;
     676             :     }
     677             : 
     678             :     /*
     679             :      * It should be safe to treat MinMaxExpr as immutable, because it will
     680             :      * depend on a non-cross-type btree comparison function, and those should
     681             :      * always be immutable.  Treating XmlExpr as immutable is more dubious,
     682             :      * and treating CoerceToDomain as immutable is outright dangerous.  But we
     683             :      * have done so historically, and changing this would probably cause more
     684             :      * problems than it would fix.  In practice, if you have a non-immutable
     685             :      * domain constraint you are in for pain anyhow.
     686             :      */
     687             : 
     688             :     /* Recurse to check arguments */
     689      164620 :     if (IsA(node, Query))
     690             :     {
     691             :         /* Recurse into subselects */
     692           0 :         return query_tree_walker((Query *) node,
     693             :                                  contain_mutable_functions_walker,
     694             :                                  context, 0);
     695             :     }
     696      164620 :     return expression_tree_walker(node, contain_mutable_functions_walker,
     697             :                                   context);
     698             : }
     699             : 
     700             : 
     701             : /*****************************************************************************
     702             :  *      Check clauses for volatile functions
     703             :  *****************************************************************************/
     704             : 
     705             : /*
     706             :  * contain_volatile_functions
     707             :  *    Recursively search for volatile functions within a clause.
     708             :  *
     709             :  * Returns true if any volatile function (or operator implemented by a
     710             :  * volatile function) is found. This test prevents, for example,
     711             :  * invalid conversions of volatile expressions into indexscan quals.
     712             :  *
     713             :  * We will recursively look into Query nodes (i.e., SubLink sub-selects)
     714             :  * but not into SubPlans.  This is a bit odd, but intentional.  If we are
     715             :  * looking at a SubLink, we are probably deciding whether a query tree
     716             :  * transformation is safe, and a contained sub-select should affect that;
     717             :  * for example, duplicating a sub-select containing a volatile function
     718             :  * would be bad.  However, once we've got to the stage of having SubPlans,
     719             :  * subsequent planning need not consider volatility within those, since
     720             :  * the executor won't change its evaluation rules for a SubPlan based on
     721             :  * volatility.
     722             :  */
     723             : bool
     724     1268926 : contain_volatile_functions(Node *clause)
     725             : {
     726     1268926 :     return contain_volatile_functions_walker(clause, NULL);
     727             : }
     728             : 
     729             : static bool
     730      548658 : contain_volatile_functions_checker(Oid func_id, void *context)
     731             : {
     732      548658 :     return (func_volatile(func_id) == PROVOLATILE_VOLATILE);
     733             : }
     734             : 
     735             : static bool
     736     3824218 : contain_volatile_functions_walker(Node *node, void *context)
     737             : {
     738     3824218 :     if (node == NULL)
     739      162026 :         return false;
     740             :     /* Check for volatile functions in node itself */
     741     3662192 :     if (check_functions_in_node(node, contain_volatile_functions_checker,
     742             :                                 context))
     743         556 :         return true;
     744             : 
     745     3661636 :     if (IsA(node, NextValueExpr))
     746             :     {
     747             :         /* NextValueExpr is volatile */
     748           0 :         return true;
     749             :     }
     750             : 
     751             :     /*
     752             :      * See notes in contain_mutable_functions_walker about why we treat
     753             :      * MinMaxExpr, XmlExpr, and CoerceToDomain as immutable, while
     754             :      * SQLValueFunction is stable.  Hence, none of them are of interest here.
     755             :      */
     756             : 
     757             :     /* Recurse to check arguments */
     758     3661636 :     if (IsA(node, Query))
     759             :     {
     760             :         /* Recurse into subselects */
     761        7720 :         return query_tree_walker((Query *) node,
     762             :                                  contain_volatile_functions_walker,
     763             :                                  context, 0);
     764             :     }
     765     3653916 :     return expression_tree_walker(node, contain_volatile_functions_walker,
     766             :                                   context);
     767             : }
     768             : 
     769             : /*
     770             :  * Special purpose version of contain_volatile_functions() for use in COPY:
     771             :  * ignore nextval(), but treat all other functions normally.
     772             :  */
     773             : bool
     774          58 : contain_volatile_functions_not_nextval(Node *clause)
     775             : {
     776          58 :     return contain_volatile_functions_not_nextval_walker(clause, NULL);
     777             : }
     778             : 
     779             : static bool
     780          34 : contain_volatile_functions_not_nextval_checker(Oid func_id, void *context)
     781             : {
     782          52 :     return (func_id != F_NEXTVAL_OID &&
     783          18 :             func_volatile(func_id) == PROVOLATILE_VOLATILE);
     784             : }
     785             : 
     786             : static bool
     787          98 : contain_volatile_functions_not_nextval_walker(Node *node, void *context)
     788             : {
     789          98 :     if (node == NULL)
     790           0 :         return false;
     791             :     /* Check for volatile functions in node itself */
     792          98 :     if (check_functions_in_node(node,
     793             :                                 contain_volatile_functions_not_nextval_checker,
     794             :                                 context))
     795           0 :         return true;
     796             : 
     797             :     /*
     798             :      * See notes in contain_mutable_functions_walker about why we treat
     799             :      * MinMaxExpr, XmlExpr, and CoerceToDomain as immutable, while
     800             :      * SQLValueFunction is stable.  Hence, none of them are of interest here.
     801             :      * Also, since we're intentionally ignoring nextval(), presumably we
     802             :      * should ignore NextValueExpr.
     803             :      */
     804             : 
     805             :     /* Recurse to check arguments */
     806          98 :     if (IsA(node, Query))
     807             :     {
     808             :         /* Recurse into subselects */
     809           0 :         return query_tree_walker((Query *) node,
     810             :                                  contain_volatile_functions_not_nextval_walker,
     811             :                                  context, 0);
     812             :     }
     813          98 :     return expression_tree_walker(node,
     814             :                                   contain_volatile_functions_not_nextval_walker,
     815             :                                   context);
     816             : }
     817             : 
     818             : 
     819             : /*****************************************************************************
     820             :  *      Check queries for parallel unsafe and/or restricted constructs
     821             :  *****************************************************************************/
     822             : 
     823             : /*
     824             :  * max_parallel_hazard
     825             :  *      Find the worst parallel-hazard level in the given query
     826             :  *
     827             :  * Returns the worst function hazard property (the earliest in this list:
     828             :  * PROPARALLEL_UNSAFE, PROPARALLEL_RESTRICTED, PROPARALLEL_SAFE) that can
     829             :  * be found in the given parsetree.  We use this to find out whether the query
     830             :  * can be parallelized at all.  The caller will also save the result in
     831             :  * PlannerGlobal so as to short-circuit checks of portions of the querytree
     832             :  * later, in the common case where everything is SAFE.
     833             :  */
     834             : char
     835      158882 : max_parallel_hazard(Query *parse)
     836             : {
     837             :     max_parallel_hazard_context context;
     838             : 
     839      158882 :     context.max_hazard = PROPARALLEL_SAFE;
     840      158882 :     context.max_interesting = PROPARALLEL_UNSAFE;
     841      158882 :     context.safe_param_ids = NIL;
     842      158882 :     (void) max_parallel_hazard_walker((Node *) parse, &context);
     843      158882 :     return context.max_hazard;
     844             : }
     845             : 
     846             : /*
     847             :  * is_parallel_safe
     848             :  *      Detect whether the given expr contains only parallel-safe functions
     849             :  *
     850             :  * root->glob->maxParallelHazard must previously have been set to the
     851             :  * result of max_parallel_hazard() on the whole query.
     852             :  */
     853             : bool
     854     1094102 : is_parallel_safe(PlannerInfo *root, Node *node)
     855             : {
     856             :     max_parallel_hazard_context context;
     857             :     PlannerInfo *proot;
     858             :     ListCell   *l;
     859             : 
     860             :     /*
     861             :      * Even if the original querytree contained nothing unsafe, we need to
     862             :      * search the expression if we have generated any PARAM_EXEC Params while
     863             :      * planning, because those are parallel-restricted and there might be one
     864             :      * in this expression.  But otherwise we don't need to look.
     865             :      */
     866     1670478 :     if (root->glob->maxParallelHazard == PROPARALLEL_SAFE &&
     867      576376 :         root->glob->paramExecTypes == NIL)
     868      562752 :         return true;
     869             :     /* Else use max_parallel_hazard's search logic, but stop on RESTRICTED */
     870      531350 :     context.max_hazard = PROPARALLEL_SAFE;
     871      531350 :     context.max_interesting = PROPARALLEL_RESTRICTED;
     872      531350 :     context.safe_param_ids = NIL;
     873             : 
     874             :     /*
     875             :      * The params that refer to the same or parent query level are considered
     876             :      * parallel-safe.  The idea is that we compute such params at Gather or
     877             :      * Gather Merge node and pass their value to workers.
     878             :      */
     879     1215840 :     for (proot = root; proot != NULL; proot = proot->parent_root)
     880             :     {
     881      711062 :         foreach(l, proot->init_plans)
     882             :         {
     883       26572 :             SubPlan    *initsubplan = (SubPlan *) lfirst(l);
     884             : 
     885       26572 :             context.safe_param_ids = list_concat(context.safe_param_ids,
     886       26572 :                                                  initsubplan->setParam);
     887             :         }
     888             :     }
     889             : 
     890      531350 :     return !max_parallel_hazard_walker(node, &context);
     891             : }
     892             : 
     893             : /* core logic for all parallel-hazard checks */
     894             : static bool
     895     1019430 : max_parallel_hazard_test(char proparallel, max_parallel_hazard_context *context)
     896             : {
     897     1019430 :     switch (proparallel)
     898             :     {
     899             :         case PROPARALLEL_SAFE:
     900             :             /* nothing to see here, move along */
     901      810042 :             break;
     902             :         case PROPARALLEL_RESTRICTED:
     903             :             /* increase max_hazard to RESTRICTED */
     904             :             Assert(context->max_hazard != PROPARALLEL_UNSAFE);
     905      171134 :             context->max_hazard = proparallel;
     906             :             /* done if we are not expecting any unsafe functions */
     907      171134 :             if (context->max_interesting == proparallel)
     908      107046 :                 return true;
     909       64088 :             break;
     910             :         case PROPARALLEL_UNSAFE:
     911       38254 :             context->max_hazard = proparallel;
     912             :             /* we're always done at the first unsafe construct */
     913       38254 :             return true;
     914             :         default:
     915           0 :             elog(ERROR, "unrecognized proparallel value \"%c\"", proparallel);
     916             :             break;
     917             :     }
     918      874130 :     return false;
     919             : }
     920             : 
     921             : /* check_functions_in_node callback */
     922             : static bool
     923      866696 : max_parallel_hazard_checker(Oid func_id, void *context)
     924             : {
     925      866696 :     return max_parallel_hazard_test(func_parallel(func_id),
     926             :                                     (max_parallel_hazard_context *) context);
     927             : }
     928             : 
     929             : static bool
     930    12036500 : max_parallel_hazard_walker(Node *node, max_parallel_hazard_context *context)
     931             : {
     932    12036500 :     if (node == NULL)
     933     2559476 :         return false;
     934             : 
     935             :     /* Check for hazardous functions in node itself */
     936     9477024 :     if (check_functions_in_node(node, max_parallel_hazard_checker,
     937             :                                 context))
     938       46988 :         return true;
     939             : 
     940             :     /*
     941             :      * It should be OK to treat MinMaxExpr as parallel-safe, since btree
     942             :      * opclass support functions are generally parallel-safe.  XmlExpr is a
     943             :      * bit more dubious but we can probably get away with it.  We err on the
     944             :      * side of caution by treating CoerceToDomain as parallel-restricted.
     945             :      * (Note: in principle that's wrong because a domain constraint could
     946             :      * contain a parallel-unsafe function; but useful constraints probably
     947             :      * never would have such, and assuming they do would cripple use of
     948             :      * parallel query in the presence of domain types.)  SQLValueFunction
     949             :      * should be safe in all cases.  NextValueExpr is parallel-unsafe.
     950             :      */
     951     9430036 :     if (IsA(node, CoerceToDomain))
     952             :     {
     953       26648 :         if (max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
     954       18888 :             return true;
     955             :     }
     956             : 
     957     9403388 :     else if (IsA(node, NextValueExpr))
     958             :     {
     959         134 :         if (max_parallel_hazard_test(PROPARALLEL_UNSAFE, context))
     960         134 :             return true;
     961             :     }
     962             : 
     963             :     /*
     964             :      * Treat window functions as parallel-restricted because we aren't sure
     965             :      * whether the input row ordering is fully deterministic, and the output
     966             :      * of window functions might vary across workers if not.  (In some cases,
     967             :      * like where the window frame orders by a primary key, we could relax
     968             :      * this restriction.  But it doesn't currently seem worth expending extra
     969             :      * effort to do so.)
     970             :      */
     971     9403254 :     else if (IsA(node, WindowFunc))
     972             :     {
     973        2428 :         if (max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
     974        1108 :             return true;
     975             :     }
     976             : 
     977             :     /*
     978             :      * As a notational convenience for callers, look through RestrictInfo.
     979             :      */
     980     9400826 :     else if (IsA(node, RestrictInfo))
     981             :     {
     982      134698 :         RestrictInfo *rinfo = (RestrictInfo *) node;
     983             : 
     984      134698 :         return max_parallel_hazard_walker((Node *) rinfo->clause, context);
     985             :     }
     986             : 
     987             :     /*
     988             :      * Really we should not see SubLink during a max_interesting == restricted
     989             :      * scan, but if we do, return true.
     990             :      */
     991     9266128 :     else if (IsA(node, SubLink))
     992             :     {
     993       44654 :         if (max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
     994           0 :             return true;
     995             :     }
     996             : 
     997             :     /*
     998             :      * Only parallel-safe SubPlans can be sent to workers.  Within the
     999             :      * testexpr of the SubPlan, Params representing the output columns of the
    1000             :      * subplan can be treated as parallel-safe, so temporarily add their IDs
    1001             :      * to the safe_param_ids list while examining the testexpr.
    1002             :      */
    1003     9221474 :     else if (IsA(node, SubPlan))
    1004             :     {
    1005       34722 :         SubPlan    *subplan = (SubPlan *) node;
    1006             :         List       *save_safe_param_ids;
    1007             : 
    1008       69288 :         if (!subplan->parallel_safe &&
    1009       34566 :             max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
    1010       34566 :             return true;
    1011         156 :         save_safe_param_ids = context->safe_param_ids;
    1012         156 :         context->safe_param_ids = list_concat_copy(context->safe_param_ids,
    1013         156 :                                                    subplan->paramIds);
    1014         156 :         if (max_parallel_hazard_walker(subplan->testexpr, context))
    1015           4 :             return true;        /* no need to restore safe_param_ids */
    1016         152 :         list_free(context->safe_param_ids);
    1017         152 :         context->safe_param_ids = save_safe_param_ids;
    1018             :         /* we must also check args, but no special Param treatment there */
    1019         152 :         if (max_parallel_hazard_walker((Node *) subplan->args, context))
    1020           0 :             return true;
    1021             :         /* don't want to recurse normally, so we're done */
    1022         152 :         return false;
    1023             :     }
    1024             : 
    1025             :     /*
    1026             :      * We can't pass Params to workers at the moment either, so they are also
    1027             :      * parallel-restricted, unless they are PARAM_EXTERN Params or are
    1028             :      * PARAM_EXEC Params listed in safe_param_ids, meaning they could be
    1029             :      * either generated within the worker or can be computed in master and
    1030             :      * then their value can be passed to the worker.
    1031             :      */
    1032     9186752 :     else if (IsA(node, Param))
    1033             :     {
    1034       74374 :         Param      *param = (Param *) node;
    1035             : 
    1036       74374 :         if (param->paramkind == PARAM_EXTERN)
    1037       21238 :             return false;
    1038             : 
    1039      105560 :         if (param->paramkind != PARAM_EXEC ||
    1040       52424 :             !list_member_int(context->safe_param_ids, param->paramid))
    1041             :         {
    1042       44304 :             if (max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
    1043       43616 :                 return true;
    1044             :         }
    1045        9520 :         return false;           /* nothing to recurse to */
    1046             :     }
    1047             : 
    1048             :     /*
    1049             :      * When we're first invoked on a completely unplanned tree, we must
    1050             :      * recurse into subqueries so to as to locate parallel-unsafe constructs
    1051             :      * anywhere in the tree.
    1052             :      */
    1053     9112378 :     else if (IsA(node, Query))
    1054             :     {
    1055      232286 :         Query      *query = (Query *) node;
    1056             : 
    1057             :         /* SELECT FOR UPDATE/SHARE must be treated as unsafe */
    1058      232286 :         if (query->rowMarks != NULL)
    1059             :         {
    1060        1568 :             context->max_hazard = PROPARALLEL_UNSAFE;
    1061        1568 :             return true;
    1062             :         }
    1063             : 
    1064             :         /* Recurse into subselects */
    1065      230718 :         return query_tree_walker(query,
    1066             :                                  max_parallel_hazard_walker,
    1067             :                                  context, 0);
    1068             :     }
    1069             : 
    1070             :     /* Recurse to check arguments */
    1071     8933826 :     return expression_tree_walker(node,
    1072             :                                   max_parallel_hazard_walker,
    1073             :                                   context);
    1074             : }
    1075             : 
    1076             : 
    1077             : /*****************************************************************************
    1078             :  *      Check clauses for nonstrict functions
    1079             :  *****************************************************************************/
    1080             : 
    1081             : /*
    1082             :  * contain_nonstrict_functions
    1083             :  *    Recursively search for nonstrict functions within a clause.
    1084             :  *
    1085             :  * Returns true if any nonstrict construct is found --- ie, anything that
    1086             :  * could produce non-NULL output with a NULL input.
    1087             :  *
    1088             :  * The idea here is that the caller has verified that the expression contains
    1089             :  * one or more Var or Param nodes (as appropriate for the caller's need), and
    1090             :  * now wishes to prove that the expression result will be NULL if any of these
    1091             :  * inputs is NULL.  If we return false, then the proof succeeded.
    1092             :  */
    1093             : bool
    1094       23604 : contain_nonstrict_functions(Node *clause)
    1095             : {
    1096       23604 :     return contain_nonstrict_functions_walker(clause, NULL);
    1097             : }
    1098             : 
    1099             : static bool
    1100         634 : contain_nonstrict_functions_checker(Oid func_id, void *context)
    1101             : {
    1102         634 :     return !func_strict(func_id);
    1103             : }
    1104             : 
    1105             : static bool
    1106       25084 : contain_nonstrict_functions_walker(Node *node, void *context)
    1107             : {
    1108       25084 :     if (node == NULL)
    1109           0 :         return false;
    1110       25084 :     if (IsA(node, Aggref))
    1111             :     {
    1112             :         /* an aggregate could return non-null with null input */
    1113           0 :         return true;
    1114             :     }
    1115       25084 :     if (IsA(node, GroupingFunc))
    1116             :     {
    1117             :         /*
    1118             :          * A GroupingFunc doesn't evaluate its arguments, and therefore must
    1119             :          * be treated as nonstrict.
    1120             :          */
    1121           0 :         return true;
    1122             :     }
    1123       25084 :     if (IsA(node, WindowFunc))
    1124             :     {
    1125             :         /* a window function could return non-null with null input */
    1126           0 :         return true;
    1127             :     }
    1128       25084 :     if (IsA(node, SubscriptingRef))
    1129             :     {
    1130             :         /*
    1131             :          * subscripting assignment is nonstrict, but subscripting itself is
    1132             :          * strict
    1133             :          */
    1134           0 :         if (((SubscriptingRef *) node)->refassgnexpr != NULL)
    1135           0 :             return true;
    1136             : 
    1137             :         /* else fall through to check args */
    1138             :     }
    1139       25084 :     if (IsA(node, DistinctExpr))
    1140             :     {
    1141             :         /* IS DISTINCT FROM is inherently non-strict */
    1142           0 :         return true;
    1143             :     }
    1144       25084 :     if (IsA(node, NullIfExpr))
    1145             :     {
    1146             :         /* NULLIF is inherently non-strict */
    1147           0 :         return true;
    1148             :     }
    1149       25084 :     if (IsA(node, BoolExpr))
    1150             :     {
    1151          12 :         BoolExpr   *expr = (BoolExpr *) node;
    1152             : 
    1153          12 :         switch (expr->boolop)
    1154             :         {
    1155             :             case AND_EXPR:
    1156             :             case OR_EXPR:
    1157             :                 /* AND, OR are inherently non-strict */
    1158          12 :                 return true;
    1159             :             default:
    1160           0 :                 break;
    1161             :         }
    1162             :     }
    1163       25072 :     if (IsA(node, SubLink))
    1164             :     {
    1165             :         /* In some cases a sublink might be strict, but in general not */
    1166           8 :         return true;
    1167             :     }
    1168       25064 :     if (IsA(node, SubPlan))
    1169           0 :         return true;
    1170       25064 :     if (IsA(node, AlternativeSubPlan))
    1171           0 :         return true;
    1172       25064 :     if (IsA(node, FieldStore))
    1173           0 :         return true;
    1174       25064 :     if (IsA(node, CoerceViaIO))
    1175             :     {
    1176             :         /*
    1177             :          * CoerceViaIO is strict regardless of whether the I/O functions are,
    1178             :          * so just go look at its argument; asking check_functions_in_node is
    1179             :          * useless expense and could deliver the wrong answer.
    1180             :          */
    1181         288 :         return contain_nonstrict_functions_walker((Node *) ((CoerceViaIO *) node)->arg,
    1182             :                                                   context);
    1183             :     }
    1184       24776 :     if (IsA(node, ArrayCoerceExpr))
    1185             :     {
    1186             :         /*
    1187             :          * ArrayCoerceExpr is strict at the array level, regardless of what
    1188             :          * the per-element expression is; so we should ignore elemexpr and
    1189             :          * recurse only into the arg.
    1190             :          */
    1191           0 :         return contain_nonstrict_functions_walker((Node *) ((ArrayCoerceExpr *) node)->arg,
    1192             :                                                   context);
    1193             :     }
    1194       24776 :     if (IsA(node, CaseExpr))
    1195         144 :         return true;
    1196       24632 :     if (IsA(node, ArrayExpr))
    1197           0 :         return true;
    1198       24632 :     if (IsA(node, RowExpr))
    1199           4 :         return true;
    1200       24628 :     if (IsA(node, RowCompareExpr))
    1201           0 :         return true;
    1202       24628 :     if (IsA(node, CoalesceExpr))
    1203          92 :         return true;
    1204       24536 :     if (IsA(node, MinMaxExpr))
    1205          40 :         return true;
    1206       24496 :     if (IsA(node, XmlExpr))
    1207           0 :         return true;
    1208       24496 :     if (IsA(node, NullTest))
    1209           8 :         return true;
    1210       24488 :     if (IsA(node, BooleanTest))
    1211           0 :         return true;
    1212             : 
    1213             :     /* Check other function-containing nodes */
    1214       24488 :     if (check_functions_in_node(node, contain_nonstrict_functions_checker,
    1215             :                                 context))
    1216           6 :         return true;
    1217             : 
    1218       24482 :     return expression_tree_walker(node, contain_nonstrict_functions_walker,
    1219             :                                   context);
    1220             : }
    1221             : 
    1222             : /*****************************************************************************
    1223             :  *      Check clauses for context-dependent nodes
    1224             :  *****************************************************************************/
    1225             : 
    1226             : /*
    1227             :  * contain_context_dependent_node
    1228             :  *    Recursively search for context-dependent nodes within a clause.
    1229             :  *
    1230             :  * CaseTestExpr nodes must appear directly within the corresponding CaseExpr,
    1231             :  * not nested within another one, or they'll see the wrong test value.  If one
    1232             :  * appears "bare" in the arguments of a SQL function, then we can't inline the
    1233             :  * SQL function for fear of creating such a situation.  The same applies for
    1234             :  * CaseTestExpr used within the elemexpr of an ArrayCoerceExpr.
    1235             :  *
    1236             :  * CoerceToDomainValue would have the same issue if domain CHECK expressions
    1237             :  * could get inlined into larger expressions, but presently that's impossible.
    1238             :  * Still, it might be allowed in future, or other node types with similar
    1239             :  * issues might get invented.  So give this function a generic name, and set
    1240             :  * up the recursion state to allow multiple flag bits.
    1241             :  */
    1242             : static bool
    1243        7244 : contain_context_dependent_node(Node *clause)
    1244             : {
    1245        7244 :     int         flags = 0;
    1246             : 
    1247        7244 :     return contain_context_dependent_node_walker(clause, &flags);
    1248             : }
    1249             : 
    1250             : #define CCDN_CASETESTEXPR_OK    0x0001  /* CaseTestExpr okay here? */
    1251             : 
    1252             : static bool
    1253        9488 : contain_context_dependent_node_walker(Node *node, int *flags)
    1254             : {
    1255        9488 :     if (node == NULL)
    1256        6314 :         return false;
    1257        3174 :     if (IsA(node, CaseTestExpr))
    1258           4 :         return !(*flags & CCDN_CASETESTEXPR_OK);
    1259        3170 :     else if (IsA(node, CaseExpr))
    1260             :     {
    1261           0 :         CaseExpr   *caseexpr = (CaseExpr *) node;
    1262             : 
    1263             :         /*
    1264             :          * If this CASE doesn't have a test expression, then it doesn't create
    1265             :          * a context in which CaseTestExprs should appear, so just fall
    1266             :          * through and treat it as a generic expression node.
    1267             :          */
    1268           0 :         if (caseexpr->arg)
    1269             :         {
    1270           0 :             int         save_flags = *flags;
    1271             :             bool        res;
    1272             : 
    1273             :             /*
    1274             :              * Note: in principle, we could distinguish the various sub-parts
    1275             :              * of a CASE construct and set the flag bit only for some of them,
    1276             :              * since we are only expecting CaseTestExprs to appear in the
    1277             :              * "expr" subtree of the CaseWhen nodes.  But it doesn't really
    1278             :              * seem worth any extra code.  If there are any bare CaseTestExprs
    1279             :              * elsewhere in the CASE, something's wrong already.
    1280             :              */
    1281           0 :             *flags |= CCDN_CASETESTEXPR_OK;
    1282           0 :             res = expression_tree_walker(node,
    1283             :                                          contain_context_dependent_node_walker,
    1284             :                                          (void *) flags);
    1285           0 :             *flags = save_flags;
    1286           0 :             return res;
    1287             :         }
    1288             :     }
    1289        3170 :     else if (IsA(node, ArrayCoerceExpr))
    1290             :     {
    1291           0 :         ArrayCoerceExpr *ac = (ArrayCoerceExpr *) node;
    1292             :         int         save_flags;
    1293             :         bool        res;
    1294             : 
    1295             :         /* Check the array expression */
    1296           0 :         if (contain_context_dependent_node_walker((Node *) ac->arg, flags))
    1297           0 :             return true;
    1298             : 
    1299             :         /* Check the elemexpr, which is allowed to contain CaseTestExpr */
    1300           0 :         save_flags = *flags;
    1301           0 :         *flags |= CCDN_CASETESTEXPR_OK;
    1302           0 :         res = contain_context_dependent_node_walker((Node *) ac->elemexpr,
    1303             :                                                     flags);
    1304           0 :         *flags = save_flags;
    1305           0 :         return res;
    1306             :     }
    1307        3170 :     return expression_tree_walker(node, contain_context_dependent_node_walker,
    1308             :                                   (void *) flags);
    1309             : }
    1310             : 
    1311             : /*****************************************************************************
    1312             :  *        Check clauses for Vars passed to non-leakproof functions
    1313             :  *****************************************************************************/
    1314             : 
    1315             : /*
    1316             :  * contain_leaked_vars
    1317             :  *      Recursively scan a clause to discover whether it contains any Var
    1318             :  *      nodes (of the current query level) that are passed as arguments to
    1319             :  *      leaky functions.
    1320             :  *
    1321             :  * Returns true if the clause contains any non-leakproof functions that are
    1322             :  * passed Var nodes of the current query level, and which might therefore leak
    1323             :  * data.  Such clauses must be applied after any lower-level security barrier
    1324             :  * clauses.
    1325             :  */
    1326             : bool
    1327        3484 : contain_leaked_vars(Node *clause)
    1328             : {
    1329        3484 :     return contain_leaked_vars_walker(clause, NULL);
    1330             : }
    1331             : 
    1332             : static bool
    1333        3512 : contain_leaked_vars_checker(Oid func_id, void *context)
    1334             : {
    1335        3512 :     return !get_func_leakproof(func_id);
    1336             : }
    1337             : 
    1338             : static bool
    1339        6992 : contain_leaked_vars_walker(Node *node, void *context)
    1340             : {
    1341        6992 :     if (node == NULL)
    1342           0 :         return false;
    1343             : 
    1344        6992 :     switch (nodeTag(node))
    1345             :     {
    1346             :         case T_Var:
    1347             :         case T_Const:
    1348             :         case T_Param:
    1349             :         case T_ArrayExpr:
    1350             :         case T_FieldSelect:
    1351             :         case T_FieldStore:
    1352             :         case T_NamedArgExpr:
    1353             :         case T_BoolExpr:
    1354             :         case T_RelabelType:
    1355             :         case T_CollateExpr:
    1356             :         case T_CaseExpr:
    1357             :         case T_CaseTestExpr:
    1358             :         case T_RowExpr:
    1359             :         case T_SQLValueFunction:
    1360             :         case T_NullTest:
    1361             :         case T_BooleanTest:
    1362             :         case T_NextValueExpr:
    1363             :         case T_List:
    1364             : 
    1365             :             /*
    1366             :              * We know these node types don't contain function calls; but
    1367             :              * something further down in the node tree might.
    1368             :              */
    1369        3444 :             break;
    1370             : 
    1371             :         case T_FuncExpr:
    1372             :         case T_OpExpr:
    1373             :         case T_DistinctExpr:
    1374             :         case T_NullIfExpr:
    1375             :         case T_ScalarArrayOpExpr:
    1376             :         case T_CoerceViaIO:
    1377             :         case T_ArrayCoerceExpr:
    1378             :         case T_SubscriptingRef:
    1379             : 
    1380             :             /*
    1381             :              * If node contains a leaky function call, and there's any Var
    1382             :              * underneath it, reject.
    1383             :              */
    1384        3512 :             if (check_functions_in_node(node, contain_leaked_vars_checker,
    1385        1676 :                                         context) &&
    1386        1676 :                 contain_var_clause(node))
    1387        1636 :                 return true;
    1388        1876 :             break;
    1389             : 
    1390             :         case T_RowCompareExpr:
    1391             :             {
    1392             :                 /*
    1393             :                  * It's worth special-casing this because a leaky comparison
    1394             :                  * function only compromises one pair of row elements, which
    1395             :                  * might not contain Vars while others do.
    1396             :                  */
    1397           0 :                 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
    1398             :                 ListCell   *opid;
    1399             :                 ListCell   *larg;
    1400             :                 ListCell   *rarg;
    1401             : 
    1402           0 :                 forthree(opid, rcexpr->opnos,
    1403             :                          larg, rcexpr->largs,
    1404             :                          rarg, rcexpr->rargs)
    1405             :                 {
    1406           0 :                     Oid         funcid = get_opcode(lfirst_oid(opid));
    1407             : 
    1408           0 :                     if (!get_func_leakproof(funcid) &&
    1409           0 :                         (contain_var_clause((Node *) lfirst(larg)) ||
    1410           0 :                          contain_var_clause((Node *) lfirst(rarg))))
    1411           0 :                         return true;
    1412             :                 }
    1413             :             }
    1414           0 :             break;
    1415             : 
    1416             :         case T_MinMaxExpr:
    1417             :             {
    1418             :                 /*
    1419             :                  * MinMaxExpr is leakproof if the comparison function it calls
    1420             :                  * is leakproof.
    1421             :                  */
    1422           0 :                 MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
    1423             :                 TypeCacheEntry *typentry;
    1424             :                 bool        leakproof;
    1425             : 
    1426             :                 /* Look up the btree comparison function for the datatype */
    1427           0 :                 typentry = lookup_type_cache(minmaxexpr->minmaxtype,
    1428             :                                              TYPECACHE_CMP_PROC);
    1429           0 :                 if (OidIsValid(typentry->cmp_proc))
    1430           0 :                     leakproof = get_func_leakproof(typentry->cmp_proc);
    1431             :                 else
    1432             :                 {
    1433             :                     /*
    1434             :                      * The executor will throw an error, but here we just
    1435             :                      * treat the missing function as leaky.
    1436             :                      */
    1437           0 :                     leakproof = false;
    1438             :                 }
    1439             : 
    1440           0 :                 if (!leakproof &&
    1441           0 :                     contain_var_clause((Node *) minmaxexpr->args))
    1442           0 :                     return true;
    1443             :             }
    1444           0 :             break;
    1445             : 
    1446             :         case T_CurrentOfExpr:
    1447             : 
    1448             :             /*
    1449             :              * WHERE CURRENT OF doesn't contain leaky function calls.
    1450             :              * Moreover, it is essential that this is considered non-leaky,
    1451             :              * since the planner must always generate a TID scan when CURRENT
    1452             :              * OF is present -- cf. cost_tidscan.
    1453             :              */
    1454          20 :             return false;
    1455             : 
    1456             :         default:
    1457             : 
    1458             :             /*
    1459             :              * If we don't recognize the node tag, assume it might be leaky.
    1460             :              * This prevents an unexpected security hole if someone adds a new
    1461             :              * node type that can call a function.
    1462             :              */
    1463          16 :             return true;
    1464             :     }
    1465        5320 :     return expression_tree_walker(node, contain_leaked_vars_walker,
    1466             :                                   context);
    1467             : }
    1468             : 
    1469             : /*
    1470             :  * find_nonnullable_rels
    1471             :  *      Determine which base rels are forced nonnullable by given clause.
    1472             :  *
    1473             :  * Returns the set of all Relids that are referenced in the clause in such
    1474             :  * a way that the clause cannot possibly return TRUE if any of these Relids
    1475             :  * is an all-NULL row.  (It is OK to err on the side of conservatism; hence
    1476             :  * the analysis here is simplistic.)
    1477             :  *
    1478             :  * The semantics here are subtly different from contain_nonstrict_functions:
    1479             :  * that function is concerned with NULL results from arbitrary expressions,
    1480             :  * but here we assume that the input is a Boolean expression, and wish to
    1481             :  * see if NULL inputs will provably cause a FALSE-or-NULL result.  We expect
    1482             :  * the expression to have been AND/OR flattened and converted to implicit-AND
    1483             :  * format.
    1484             :  *
    1485             :  * Note: this function is largely duplicative of find_nonnullable_vars().
    1486             :  * The reason not to simplify this function into a thin wrapper around
    1487             :  * find_nonnullable_vars() is that the tested conditions really are different:
    1488             :  * a clause like "t1.v1 IS NOT NULL OR t1.v2 IS NOT NULL" does not prove
    1489             :  * that either v1 or v2 can't be NULL, but it does prove that the t1 row
    1490             :  * as a whole can't be all-NULL.  Also, the behavior for PHVs is different.
    1491             :  *
    1492             :  * top_level is true while scanning top-level AND/OR structure; here, showing
    1493             :  * the result is either FALSE or NULL is good enough.  top_level is false when
    1494             :  * we have descended below a NOT or a strict function: now we must be able to
    1495             :  * prove that the subexpression goes to NULL.
    1496             :  *
    1497             :  * We don't use expression_tree_walker here because we don't want to descend
    1498             :  * through very many kinds of nodes; only the ones we can be sure are strict.
    1499             :  */
    1500             : Relids
    1501       78342 : find_nonnullable_rels(Node *clause)
    1502             : {
    1503       78342 :     return find_nonnullable_rels_walker(clause, true);
    1504             : }
    1505             : 
    1506             : static Relids
    1507      532466 : find_nonnullable_rels_walker(Node *node, bool top_level)
    1508             : {
    1509      532466 :     Relids      result = NULL;
    1510             :     ListCell   *l;
    1511             : 
    1512      532466 :     if (node == NULL)
    1513        3138 :         return NULL;
    1514      529328 :     if (IsA(node, Var))
    1515             :     {
    1516      167772 :         Var        *var = (Var *) node;
    1517             : 
    1518      167772 :         if (var->varlevelsup == 0)
    1519      167772 :             result = bms_make_singleton(var->varno);
    1520             :     }
    1521      361556 :     else if (IsA(node, List))
    1522             :     {
    1523             :         /*
    1524             :          * At top level, we are examining an implicit-AND list: if any of the
    1525             :          * arms produces FALSE-or-NULL then the result is FALSE-or-NULL. If
    1526             :          * not at top level, we are examining the arguments of a strict
    1527             :          * function: if any of them produce NULL then the result of the
    1528             :          * function must be NULL.  So in both cases, the set of nonnullable
    1529             :          * rels is the union of those found in the arms, and we pass down the
    1530             :          * top_level flag unmodified.
    1531             :          */
    1532      521738 :         foreach(l, (List *) node)
    1533             :         {
    1534      670180 :             result = bms_join(result,
    1535      335090 :                               find_nonnullable_rels_walker(lfirst(l),
    1536             :                                                            top_level));
    1537             :         }
    1538             :     }
    1539      174908 :     else if (IsA(node, FuncExpr))
    1540             :     {
    1541        2620 :         FuncExpr   *expr = (FuncExpr *) node;
    1542             : 
    1543        2620 :         if (func_strict(expr->funcid))
    1544        2614 :             result = find_nonnullable_rels_walker((Node *) expr->args, false);
    1545             :     }
    1546      172288 :     else if (IsA(node, OpExpr))
    1547             :     {
    1548       98912 :         OpExpr     *expr = (OpExpr *) node;
    1549             : 
    1550       98912 :         set_opfuncid(expr);
    1551       98912 :         if (func_strict(expr->opfuncid))
    1552       98912 :             result = find_nonnullable_rels_walker((Node *) expr->args, false);
    1553             :     }
    1554       73376 :     else if (IsA(node, ScalarArrayOpExpr))
    1555             :     {
    1556        9018 :         ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
    1557             : 
    1558        9018 :         if (is_strict_saop(expr, true))
    1559        9018 :             result = find_nonnullable_rels_walker((Node *) expr->args, false);
    1560             :     }
    1561       64358 :     else if (IsA(node, BoolExpr))
    1562             :     {
    1563        3774 :         BoolExpr   *expr = (BoolExpr *) node;
    1564             : 
    1565        3774 :         switch (expr->boolop)
    1566             :         {
    1567             :             case AND_EXPR:
    1568             :                 /* At top level we can just recurse (to the List case) */
    1569          30 :                 if (top_level)
    1570             :                 {
    1571          30 :                     result = find_nonnullable_rels_walker((Node *) expr->args,
    1572             :                                                           top_level);
    1573          30 :                     break;
    1574             :                 }
    1575             : 
    1576             :                 /*
    1577             :                  * Below top level, even if one arm produces NULL, the result
    1578             :                  * could be FALSE (hence not NULL).  However, if *all* the
    1579             :                  * arms produce NULL then the result is NULL, so we can take
    1580             :                  * the intersection of the sets of nonnullable rels, just as
    1581             :                  * for OR.  Fall through to share code.
    1582             :                  */
    1583             :                 /* FALL THRU */
    1584             :             case OR_EXPR:
    1585             : 
    1586             :                 /*
    1587             :                  * OR is strict if all of its arms are, so we can take the
    1588             :                  * intersection of the sets of nonnullable rels for each arm.
    1589             :                  * This works for both values of top_level.
    1590             :                  */
    1591        5326 :                 foreach(l, expr->args)
    1592             :                 {
    1593             :                     Relids      subresult;
    1594             : 
    1595        5132 :                     subresult = find_nonnullable_rels_walker(lfirst(l),
    1596             :                                                              top_level);
    1597        5132 :                     if (result == NULL) /* first subresult? */
    1598        2874 :                         result = subresult;
    1599             :                     else
    1600        2258 :                         result = bms_int_members(result, subresult);
    1601             : 
    1602             :                     /*
    1603             :                      * If the intersection is empty, we can stop looking. This
    1604             :                      * also justifies the test for first-subresult above.
    1605             :                      */
    1606        5132 :                     if (bms_is_empty(result))
    1607        2680 :                         break;
    1608             :                 }
    1609        2874 :                 break;
    1610             :             case NOT_EXPR:
    1611             :                 /* NOT will return null if its arg is null */
    1612         870 :                 result = find_nonnullable_rels_walker((Node *) expr->args,
    1613             :                                                       false);
    1614         870 :                 break;
    1615             :             default:
    1616           0 :                 elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop);
    1617             :                 break;
    1618             :         }
    1619             :     }
    1620       60584 :     else if (IsA(node, RelabelType))
    1621             :     {
    1622         796 :         RelabelType *expr = (RelabelType *) node;
    1623             : 
    1624         796 :         result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
    1625             :     }
    1626       59788 :     else if (IsA(node, CoerceViaIO))
    1627             :     {
    1628             :         /* not clear this is useful, but it can't hurt */
    1629          44 :         CoerceViaIO *expr = (CoerceViaIO *) node;
    1630             : 
    1631          44 :         result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
    1632             :     }
    1633       59744 :     else if (IsA(node, ArrayCoerceExpr))
    1634             :     {
    1635             :         /* ArrayCoerceExpr is strict at the array level; ignore elemexpr */
    1636           0 :         ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
    1637             : 
    1638           0 :         result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
    1639             :     }
    1640       59744 :     else if (IsA(node, ConvertRowtypeExpr))
    1641             :     {
    1642             :         /* not clear this is useful, but it can't hurt */
    1643           0 :         ConvertRowtypeExpr *expr = (ConvertRowtypeExpr *) node;
    1644             : 
    1645           0 :         result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
    1646             :     }
    1647       59744 :     else if (IsA(node, CollateExpr))
    1648             :     {
    1649           0 :         CollateExpr *expr = (CollateExpr *) node;
    1650             : 
    1651           0 :         result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
    1652             :     }
    1653       59744 :     else if (IsA(node, NullTest))
    1654             :     {
    1655             :         /* IS NOT NULL can be considered strict, but only at top level */
    1656        2298 :         NullTest   *expr = (NullTest *) node;
    1657             : 
    1658        2298 :         if (top_level && expr->nulltesttype == IS_NOT_NULL && !expr->argisrow)
    1659        1462 :             result = find_nonnullable_rels_walker((Node *) expr->arg, false);
    1660             :     }
    1661       57446 :     else if (IsA(node, BooleanTest))
    1662             :     {
    1663             :         /* Boolean tests that reject NULL are strict at top level */
    1664           8 :         BooleanTest *expr = (BooleanTest *) node;
    1665             : 
    1666          16 :         if (top_level &&
    1667          16 :             (expr->booltesttype == IS_TRUE ||
    1668          16 :              expr->booltesttype == IS_FALSE ||
    1669           8 :              expr->booltesttype == IS_NOT_UNKNOWN))
    1670           0 :             result = find_nonnullable_rels_walker((Node *) expr->arg, false);
    1671             :     }
    1672       57438 :     else if (IsA(node, PlaceHolderVar))
    1673             :     {
    1674         156 :         PlaceHolderVar *phv = (PlaceHolderVar *) node;
    1675             : 
    1676             :         /*
    1677             :          * If the contained expression forces any rels non-nullable, so does
    1678             :          * the PHV.
    1679             :          */
    1680         156 :         result = find_nonnullable_rels_walker((Node *) phv->phexpr, top_level);
    1681             : 
    1682             :         /*
    1683             :          * If the PHV's syntactic scope is exactly one rel, it will be forced
    1684             :          * to be evaluated at that rel, and so it will behave like a Var of
    1685             :          * that rel: if the rel's entire output goes to null, so will the PHV.
    1686             :          * (If the syntactic scope is a join, we know that the PHV will go to
    1687             :          * null if the whole join does; but that is AND semantics while we
    1688             :          * need OR semantics for find_nonnullable_rels' result, so we can't do
    1689             :          * anything with the knowledge.)
    1690             :          */
    1691         312 :         if (phv->phlevelsup == 0 &&
    1692         156 :             bms_membership(phv->phrels) == BMS_SINGLETON)
    1693          84 :             result = bms_add_members(result, phv->phrels);
    1694             :     }
    1695      529328 :     return result;
    1696             : }
    1697             : 
    1698             : /*
    1699             :  * find_nonnullable_vars
    1700             :  *      Determine which Vars are forced nonnullable by given clause.
    1701             :  *
    1702             :  * Returns a list of all level-zero Vars that are referenced in the clause in
    1703             :  * such a way that the clause cannot possibly return TRUE if any of these Vars
    1704             :  * is NULL.  (It is OK to err on the side of conservatism; hence the analysis
    1705             :  * here is simplistic.)
    1706             :  *
    1707             :  * The semantics here are subtly different from contain_nonstrict_functions:
    1708             :  * that function is concerned with NULL results from arbitrary expressions,
    1709             :  * but here we assume that the input is a Boolean expression, and wish to
    1710             :  * see if NULL inputs will provably cause a FALSE-or-NULL result.  We expect
    1711             :  * the expression to have been AND/OR flattened and converted to implicit-AND
    1712             :  * format.
    1713             :  *
    1714             :  * The result is a palloc'd List, but we have not copied the member Var nodes.
    1715             :  * Also, we don't bother trying to eliminate duplicate entries.
    1716             :  *
    1717             :  * top_level is true while scanning top-level AND/OR structure; here, showing
    1718             :  * the result is either FALSE or NULL is good enough.  top_level is false when
    1719             :  * we have descended below a NOT or a strict function: now we must be able to
    1720             :  * prove that the subexpression goes to NULL.
    1721             :  *
    1722             :  * We don't use expression_tree_walker here because we don't want to descend
    1723             :  * through very many kinds of nodes; only the ones we can be sure are strict.
    1724             :  */
    1725             : List *
    1726       65222 : find_nonnullable_vars(Node *clause)
    1727             : {
    1728       65222 :     return find_nonnullable_vars_walker(clause, true);
    1729             : }
    1730             : 
    1731             : static List *
    1732      456604 : find_nonnullable_vars_walker(Node *node, bool top_level)
    1733             : {
    1734      456604 :     List       *result = NIL;
    1735             :     ListCell   *l;
    1736             : 
    1737      456604 :     if (node == NULL)
    1738        3134 :         return NIL;
    1739      453470 :     if (IsA(node, Var))
    1740             :     {
    1741      139030 :         Var        *var = (Var *) node;
    1742             : 
    1743      139030 :         if (var->varlevelsup == 0)
    1744      139030 :             result = list_make1(var);
    1745             :     }
    1746      314440 :     else if (IsA(node, List))
    1747             :     {
    1748             :         /*
    1749             :          * At top level, we are examining an implicit-AND list: if any of the
    1750             :          * arms produces FALSE-or-NULL then the result is FALSE-or-NULL. If
    1751             :          * not at top level, we are examining the arguments of a strict
    1752             :          * function: if any of them produce NULL then the result of the
    1753             :          * function must be NULL.  So in both cases, the set of nonnullable
    1754             :          * vars is the union of those found in the arms, and we pass down the
    1755             :          * top_level flag unmodified.
    1756             :          */
    1757      446512 :         foreach(l, (List *) node)
    1758             :         {
    1759      288556 :             result = list_concat(result,
    1760      288556 :                                  find_nonnullable_vars_walker(lfirst(l),
    1761             :                                                               top_level));
    1762             :         }
    1763             :     }
    1764      156484 :     else if (IsA(node, FuncExpr))
    1765             :     {
    1766        2528 :         FuncExpr   *expr = (FuncExpr *) node;
    1767             : 
    1768        2528 :         if (func_strict(expr->funcid))
    1769        2522 :             result = find_nonnullable_vars_walker((Node *) expr->args, false);
    1770             :     }
    1771      153956 :     else if (IsA(node, OpExpr))
    1772             :     {
    1773       83428 :         OpExpr     *expr = (OpExpr *) node;
    1774             : 
    1775       83428 :         set_opfuncid(expr);
    1776       83428 :         if (func_strict(expr->opfuncid))
    1777       83428 :             result = find_nonnullable_vars_walker((Node *) expr->args, false);
    1778             :     }
    1779       70528 :     else if (IsA(node, ScalarArrayOpExpr))
    1780             :     {
    1781        9018 :         ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
    1782             : 
    1783        9018 :         if (is_strict_saop(expr, true))
    1784        9018 :             result = find_nonnullable_vars_walker((Node *) expr->args, false);
    1785             :     }
    1786       61510 :     else if (IsA(node, BoolExpr))
    1787             :     {
    1788        3698 :         BoolExpr   *expr = (BoolExpr *) node;
    1789             : 
    1790        3698 :         switch (expr->boolop)
    1791             :         {
    1792             :             case AND_EXPR:
    1793             :                 /* At top level we can just recurse (to the List case) */
    1794          30 :                 if (top_level)
    1795             :                 {
    1796          30 :                     result = find_nonnullable_vars_walker((Node *) expr->args,
    1797             :                                                           top_level);
    1798          30 :                     break;
    1799             :                 }
    1800             : 
    1801             :                 /*
    1802             :                  * Below top level, even if one arm produces NULL, the result
    1803             :                  * could be FALSE (hence not NULL).  However, if *all* the
    1804             :                  * arms produce NULL then the result is NULL, so we can take
    1805             :                  * the intersection of the sets of nonnullable vars, just as
    1806             :                  * for OR.  Fall through to share code.
    1807             :                  */
    1808             :                 /* FALL THRU */
    1809             :             case OR_EXPR:
    1810             : 
    1811             :                 /*
    1812             :                  * OR is strict if all of its arms are, so we can take the
    1813             :                  * intersection of the sets of nonnullable vars for each arm.
    1814             :                  * This works for both values of top_level.
    1815             :                  */
    1816        4920 :                 foreach(l, expr->args)
    1817             :                 {
    1818             :                     List       *subresult;
    1819             : 
    1820        4920 :                     subresult = find_nonnullable_vars_walker(lfirst(l),
    1821             :                                                              top_level);
    1822        4920 :                     if (result == NIL)  /* first subresult? */
    1823        2798 :                         result = subresult;
    1824             :                     else
    1825        2122 :                         result = list_intersection(result, subresult);
    1826             : 
    1827             :                     /*
    1828             :                      * If the intersection is empty, we can stop looking. This
    1829             :                      * also justifies the test for first-subresult above.
    1830             :                      */
    1831        4920 :                     if (result == NIL)
    1832        2798 :                         break;
    1833             :                 }
    1834        2798 :                 break;
    1835             :             case NOT_EXPR:
    1836             :                 /* NOT will return null if its arg is null */
    1837         870 :                 result = find_nonnullable_vars_walker((Node *) expr->args,
    1838             :                                                       false);
    1839         870 :                 break;
    1840             :             default:
    1841           0 :                 elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop);
    1842             :                 break;
    1843             :         }
    1844             :     }
    1845       57812 :     else if (IsA(node, RelabelType))
    1846             :     {
    1847         416 :         RelabelType *expr = (RelabelType *) node;
    1848             : 
    1849         416 :         result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
    1850             :     }
    1851       57396 :     else if (IsA(node, CoerceViaIO))
    1852             :     {
    1853             :         /* not clear this is useful, but it can't hurt */
    1854          44 :         CoerceViaIO *expr = (CoerceViaIO *) node;
    1855             : 
    1856          44 :         result = find_nonnullable_vars_walker((Node *) expr->arg, false);
    1857             :     }
    1858       57352 :     else if (IsA(node, ArrayCoerceExpr))
    1859             :     {
    1860             :         /* ArrayCoerceExpr is strict at the array level; ignore elemexpr */
    1861           0 :         ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
    1862             : 
    1863           0 :         result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
    1864             :     }
    1865       57352 :     else if (IsA(node, ConvertRowtypeExpr))
    1866             :     {
    1867             :         /* not clear this is useful, but it can't hurt */
    1868           0 :         ConvertRowtypeExpr *expr = (ConvertRowtypeExpr *) node;
    1869             : 
    1870           0 :         result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
    1871             :     }
    1872       57352 :     else if (IsA(node, CollateExpr))
    1873             :     {
    1874           0 :         CollateExpr *expr = (CollateExpr *) node;
    1875             : 
    1876           0 :         result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
    1877             :     }
    1878       57352 :     else if (IsA(node, NullTest))
    1879             :     {
    1880             :         /* IS NOT NULL can be considered strict, but only at top level */
    1881        2244 :         NullTest   *expr = (NullTest *) node;
    1882             : 
    1883        2244 :         if (top_level && expr->nulltesttype == IS_NOT_NULL && !expr->argisrow)
    1884        1450 :             result = find_nonnullable_vars_walker((Node *) expr->arg, false);
    1885             :     }
    1886       55108 :     else if (IsA(node, BooleanTest))
    1887             :     {
    1888             :         /* Boolean tests that reject NULL are strict at top level */
    1889           8 :         BooleanTest *expr = (BooleanTest *) node;
    1890             : 
    1891          16 :         if (top_level &&
    1892          16 :             (expr->booltesttype == IS_TRUE ||
    1893          16 :              expr->booltesttype == IS_FALSE ||
    1894           8 :              expr->booltesttype == IS_NOT_UNKNOWN))
    1895           0 :             result = find_nonnullable_vars_walker((Node *) expr->arg, false);
    1896             :     }
    1897       55100 :     else if (IsA(node, PlaceHolderVar))
    1898             :     {
    1899         128 :         PlaceHolderVar *phv = (PlaceHolderVar *) node;
    1900             : 
    1901         128 :         result = find_nonnullable_vars_walker((Node *) phv->phexpr, top_level);
    1902             :     }
    1903      453470 :     return result;
    1904             : }
    1905             : 
    1906             : /*
    1907             :  * find_forced_null_vars
    1908             :  *      Determine which Vars must be NULL for the given clause to return TRUE.
    1909             :  *
    1910             :  * This is the complement of find_nonnullable_vars: find the level-zero Vars
    1911             :  * that must be NULL for the clause to return TRUE.  (It is OK to err on the
    1912             :  * side of conservatism; hence the analysis here is simplistic.  In fact,
    1913             :  * we only detect simple "var IS NULL" tests at the top level.)
    1914             :  *
    1915             :  * The result is a palloc'd List, but we have not copied the member Var nodes.
    1916             :  * Also, we don't bother trying to eliminate duplicate entries.
    1917             :  */
    1918             : List *
    1919       84960 : find_forced_null_vars(Node *node)
    1920             : {
    1921       84960 :     List       *result = NIL;
    1922             :     Var        *var;
    1923             :     ListCell   *l;
    1924             : 
    1925       84960 :     if (node == NULL)
    1926        2724 :         return NIL;
    1927             :     /* Check single-clause cases using subroutine */
    1928       82236 :     var = find_forced_null_var(node);
    1929       82236 :     if (var)
    1930             :     {
    1931         686 :         result = list_make1(var);
    1932             :     }
    1933             :     /* Otherwise, handle AND-conditions */
    1934       81550 :     else if (IsA(node, List))
    1935             :     {
    1936             :         /*
    1937             :          * At top level, we are examining an implicit-AND list: if any of the
    1938             :          * arms produces FALSE-or-NULL then the result is FALSE-or-NULL.
    1939             :          */
    1940       82236 :         foreach(l, (List *) node)
    1941             :         {
    1942       48836 :             result = list_concat(result,
    1943       48836 :                                  find_forced_null_vars(lfirst(l)));
    1944             :         }
    1945             :     }
    1946       48150 :     else if (IsA(node, BoolExpr))
    1947             :     {
    1948        3602 :         BoolExpr   *expr = (BoolExpr *) node;
    1949             : 
    1950             :         /*
    1951             :          * We don't bother considering the OR case, because it's fairly
    1952             :          * unlikely anyone would write "v1 IS NULL OR v1 IS NULL". Likewise,
    1953             :          * the NOT case isn't worth expending code on.
    1954             :          */
    1955        3602 :         if (expr->boolop == AND_EXPR)
    1956             :         {
    1957             :             /* At top level we can just recurse (to the List case) */
    1958           0 :             result = find_forced_null_vars((Node *) expr->args);
    1959             :         }
    1960             :     }
    1961       82236 :     return result;
    1962             : }
    1963             : 
    1964             : /*
    1965             :  * find_forced_null_var
    1966             :  *      Return the Var forced null by the given clause, or NULL if it's
    1967             :  *      not an IS NULL-type clause.  For success, the clause must enforce
    1968             :  *      *only* nullness of the particular Var, not any other conditions.
    1969             :  *
    1970             :  * This is just the single-clause case of find_forced_null_vars(), without
    1971             :  * any allowance for AND conditions.  It's used by initsplan.c on individual
    1972             :  * qual clauses.  The reason for not just applying find_forced_null_vars()
    1973             :  * is that if an AND of an IS NULL clause with something else were to somehow
    1974             :  * survive AND/OR flattening, initsplan.c might get fooled into discarding
    1975             :  * the whole clause when only the IS NULL part of it had been proved redundant.
    1976             :  */
    1977             : Var *
    1978       83040 : find_forced_null_var(Node *node)
    1979             : {
    1980       83040 :     if (node == NULL)
    1981           0 :         return NULL;
    1982       83040 :     if (IsA(node, NullTest))
    1983             :     {
    1984             :         /* check for var IS NULL */
    1985        2112 :         NullTest   *expr = (NullTest *) node;
    1986             : 
    1987        2112 :         if (expr->nulltesttype == IS_NULL && !expr->argisrow)
    1988             :         {
    1989        1360 :             Var        *var = (Var *) expr->arg;
    1990             : 
    1991        2720 :             if (var && IsA(var, Var) &&
    1992        1360 :                 var->varlevelsup == 0)
    1993        1360 :                 return var;
    1994             :         }
    1995             :     }
    1996       80928 :     else if (IsA(node, BooleanTest))
    1997             :     {
    1998             :         /* var IS UNKNOWN is equivalent to var IS NULL */
    1999          16 :         BooleanTest *expr = (BooleanTest *) node;
    2000             : 
    2001          16 :         if (expr->booltesttype == IS_UNKNOWN)
    2002             :         {
    2003           0 :             Var        *var = (Var *) expr->arg;
    2004             : 
    2005           0 :             if (var && IsA(var, Var) &&
    2006           0 :                 var->varlevelsup == 0)
    2007           0 :                 return var;
    2008             :         }
    2009             :     }
    2010       81680 :     return NULL;
    2011             : }
    2012             : 
    2013             : /*
    2014             :  * Can we treat a ScalarArrayOpExpr as strict?
    2015             :  *
    2016             :  * If "falseOK" is true, then a "false" result can be considered strict,
    2017             :  * else we need to guarantee an actual NULL result for NULL input.
    2018             :  *
    2019             :  * "foo op ALL array" is strict if the op is strict *and* we can prove
    2020             :  * that the array input isn't an empty array.  We can check that
    2021             :  * for the cases of an array constant and an ARRAY[] construct.
    2022             :  *
    2023             :  * "foo op ANY array" is strict in the falseOK sense if the op is strict.
    2024             :  * If not falseOK, the test is the same as for "foo op ALL array".
    2025             :  */
    2026             : static bool
    2027       18036 : is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK)
    2028             : {
    2029             :     Node       *rightop;
    2030             : 
    2031             :     /* The contained operator must be strict. */
    2032       18036 :     set_sa_opfuncid(expr);
    2033       18036 :     if (!func_strict(expr->opfuncid))
    2034           0 :         return false;
    2035             :     /* If ANY and falseOK, that's all we need to check. */
    2036       18036 :     if (expr->useOr && falseOK)
    2037       17968 :         return true;
    2038             :     /* Else, we have to see if the array is provably non-empty. */
    2039             :     Assert(list_length(expr->args) == 2);
    2040          68 :     rightop = (Node *) lsecond(expr->args);
    2041          68 :     if (rightop && IsA(rightop, Const))
    2042           0 :     {
    2043          68 :         Datum       arraydatum = ((Const *) rightop)->constvalue;
    2044          68 :         bool        arrayisnull = ((Const *) rightop)->constisnull;
    2045             :         ArrayType  *arrayval;
    2046             :         int         nitems;
    2047             : 
    2048          68 :         if (arrayisnull)
    2049           0 :             return false;
    2050          68 :         arrayval = DatumGetArrayTypeP(arraydatum);
    2051          68 :         nitems = ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
    2052          68 :         if (nitems > 0)
    2053          68 :             return true;
    2054             :     }
    2055           0 :     else if (rightop && IsA(rightop, ArrayExpr))
    2056             :     {
    2057           0 :         ArrayExpr  *arrayexpr = (ArrayExpr *) rightop;
    2058             : 
    2059           0 :         if (arrayexpr->elements != NIL && !arrayexpr->multidims)
    2060           0 :             return true;
    2061             :     }
    2062           0 :     return false;
    2063             : }
    2064             : 
    2065             : 
    2066             : /*****************************************************************************
    2067             :  *      Check for "pseudo-constant" clauses
    2068             :  *****************************************************************************/
    2069             : 
    2070             : /*
    2071             :  * is_pseudo_constant_clause
    2072             :  *    Detect whether an expression is "pseudo constant", ie, it contains no
    2073             :  *    variables of the current query level and no uses of volatile functions.
    2074             :  *    Such an expr is not necessarily a true constant: it can still contain
    2075             :  *    Params and outer-level Vars, not to mention functions whose results
    2076             :  *    may vary from one statement to the next.  However, the expr's value
    2077             :  *    will be constant over any one scan of the current query, so it can be
    2078             :  *    used as, eg, an indexscan key.  (Actually, the condition for indexscan
    2079             :  *    keys is weaker than this; see is_pseudo_constant_for_index().)
    2080             :  *
    2081             :  * CAUTION: this function omits to test for one very important class of
    2082             :  * not-constant expressions, namely aggregates (Aggrefs).  In current usage
    2083             :  * this is only applied to WHERE clauses and so a check for Aggrefs would be
    2084             :  * a waste of cycles; but be sure to also check contain_agg_clause() if you
    2085             :  * want to know about pseudo-constness in other contexts.  The same goes
    2086             :  * for window functions (WindowFuncs).
    2087             :  */
    2088             : bool
    2089        1084 : is_pseudo_constant_clause(Node *clause)
    2090             : {
    2091             :     /*
    2092             :      * We could implement this check in one recursive scan.  But since the
    2093             :      * check for volatile functions is both moderately expensive and unlikely
    2094             :      * to fail, it seems better to look for Vars first and only check for
    2095             :      * volatile functions if we find no Vars.
    2096             :      */
    2097        2168 :     if (!contain_var_clause(clause) &&
    2098        1084 :         !contain_volatile_functions(clause))
    2099        1084 :         return true;
    2100           0 :     return false;
    2101             : }
    2102             : 
    2103             : /*
    2104             :  * is_pseudo_constant_clause_relids
    2105             :  *    Same as above, except caller already has available the var membership
    2106             :  *    of the expression; this lets us avoid the contain_var_clause() scan.
    2107             :  */
    2108             : bool
    2109      237818 : is_pseudo_constant_clause_relids(Node *clause, Relids relids)
    2110             : {
    2111      470536 :     if (bms_is_empty(relids) &&
    2112      232718 :         !contain_volatile_functions(clause))
    2113      232718 :         return true;
    2114        5100 :     return false;
    2115             : }
    2116             : 
    2117             : 
    2118             : /*****************************************************************************
    2119             :  *                                                                           *
    2120             :  *      General clause-manipulating routines                                 *
    2121             :  *                                                                           *
    2122             :  *****************************************************************************/
    2123             : 
    2124             : /*
    2125             :  * NumRelids
    2126             :  *      (formerly clause_relids)
    2127             :  *
    2128             :  * Returns the number of different relations referenced in 'clause'.
    2129             :  */
    2130             : int
    2131        1060 : NumRelids(Node *clause)
    2132             : {
    2133        1060 :     Relids      varnos = pull_varnos(clause);
    2134        1060 :     int         result = bms_num_members(varnos);
    2135             : 
    2136        1060 :     bms_free(varnos);
    2137        1060 :     return result;
    2138             : }
    2139             : 
    2140             : /*
    2141             :  * CommuteOpExpr: commute a binary operator clause
    2142             :  *
    2143             :  * XXX the clause is destructively modified!
    2144             :  */
    2145             : void
    2146       11722 : CommuteOpExpr(OpExpr *clause)
    2147             : {
    2148             :     Oid         opoid;
    2149             :     Node       *temp;
    2150             : 
    2151             :     /* Sanity checks: caller is at fault if these fail */
    2152       23444 :     if (!is_opclause(clause) ||
    2153       11722 :         list_length(clause->args) != 2)
    2154           0 :         elog(ERROR, "cannot commute non-binary-operator clause");
    2155             : 
    2156       11722 :     opoid = get_commutator(clause->opno);
    2157             : 
    2158       11722 :     if (!OidIsValid(opoid))
    2159           0 :         elog(ERROR, "could not find commutator for operator %u",
    2160             :              clause->opno);
    2161             : 
    2162             :     /*
    2163             :      * modify the clause in-place!
    2164             :      */
    2165       11722 :     clause->opno = opoid;
    2166       11722 :     clause->opfuncid = InvalidOid;
    2167             :     /* opresulttype, opretset, opcollid, inputcollid need not change */
    2168             : 
    2169       11722 :     temp = linitial(clause->args);
    2170       11722 :     linitial(clause->args) = lsecond(clause->args);
    2171       11722 :     lsecond(clause->args) = temp;
    2172       11722 : }
    2173             : 
    2174             : /*
    2175             :  * Helper for eval_const_expressions: check that datatype of an attribute
    2176             :  * is still what it was when the expression was parsed.  This is needed to
    2177             :  * guard against improper simplification after ALTER COLUMN TYPE.  (XXX we
    2178             :  * may well need to make similar checks elsewhere?)
    2179             :  *
    2180             :  * rowtypeid may come from a whole-row Var, and therefore it can be a domain
    2181             :  * over composite, but for this purpose we only care about checking the type
    2182             :  * of a contained field.
    2183             :  */
    2184             : static bool
    2185          80 : rowtype_field_matches(Oid rowtypeid, int fieldnum,
    2186             :                       Oid expectedtype, int32 expectedtypmod,
    2187             :                       Oid expectedcollation)
    2188             : {
    2189             :     TupleDesc   tupdesc;
    2190             :     Form_pg_attribute attr;
    2191             : 
    2192             :     /* No issue for RECORD, since there is no way to ALTER such a type */
    2193          80 :     if (rowtypeid == RECORDOID)
    2194           0 :         return true;
    2195          80 :     tupdesc = lookup_rowtype_tupdesc_domain(rowtypeid, -1, false);
    2196          80 :     if (fieldnum <= 0 || fieldnum > tupdesc->natts)
    2197             :     {
    2198           0 :         ReleaseTupleDesc(tupdesc);
    2199           0 :         return false;
    2200             :     }
    2201          80 :     attr = TupleDescAttr(tupdesc, fieldnum - 1);
    2202         160 :     if (attr->attisdropped ||
    2203         160 :         attr->atttypid != expectedtype ||
    2204         160 :         attr->atttypmod != expectedtypmod ||
    2205          80 :         attr->attcollation != expectedcollation)
    2206             :     {
    2207           0 :         ReleaseTupleDesc(tupdesc);
    2208           0 :         return false;
    2209             :     }
    2210          80 :     ReleaseTupleDesc(tupdesc);
    2211          80 :     return true;
    2212             : }
    2213             : 
    2214             : 
    2215             : /*--------------------
    2216             :  * eval_const_expressions
    2217             :  *
    2218             :  * Reduce any recognizably constant subexpressions of the given
    2219             :  * expression tree, for example "2 + 2" => "4".  More interestingly,
    2220             :  * we can reduce certain boolean expressions even when they contain
    2221             :  * non-constant subexpressions: "x OR true" => "true" no matter what
    2222             :  * the subexpression x is.  (XXX We assume that no such subexpression
    2223             :  * will have important side-effects, which is not necessarily a good
    2224             :  * assumption in the presence of user-defined functions; do we need a
    2225             :  * pg_proc flag that prevents discarding the execution of a function?)
    2226             :  *
    2227             :  * We do understand that certain functions may deliver non-constant
    2228             :  * results even with constant inputs, "nextval()" being the classic
    2229             :  * example.  Functions that are not marked "immutable" in pg_proc
    2230             :  * will not be pre-evaluated here, although we will reduce their
    2231             :  * arguments as far as possible.
    2232             :  *
    2233             :  * Whenever a function is eliminated from the expression by means of
    2234             :  * constant-expression evaluation or inlining, we add the function to
    2235             :  * root->glob->invalItems.  This ensures the plan is known to depend on
    2236             :  * such functions, even though they aren't referenced anymore.
    2237             :  *
    2238             :  * We assume that the tree has already been type-checked and contains
    2239             :  * only operators and functions that are reasonable to try to execute.
    2240             :  *
    2241             :  * NOTE: "root" can be passed as NULL if the caller never wants to do any
    2242             :  * Param substitutions nor receive info about inlined functions.
    2243             :  *
    2244             :  * NOTE: the planner assumes that this will always flatten nested AND and
    2245             :  * OR clauses into N-argument form.  See comments in prepqual.c.
    2246             :  *
    2247             :  * NOTE: another critical effect is that any function calls that require
    2248             :  * default arguments will be expanded, and named-argument calls will be
    2249             :  * converted to positional notation.  The executor won't handle either.
    2250             :  *--------------------
    2251             :  */
    2252             : Node *
    2253      624952 : eval_const_expressions(PlannerInfo *root, Node *node)
    2254             : {
    2255             :     eval_const_expressions_context context;
    2256             : 
    2257      624952 :     if (root)
    2258      535148 :         context.boundParams = root->glob->boundParams;    /* bound Params */
    2259             :     else
    2260       89804 :         context.boundParams = NULL;
    2261      624952 :     context.root = root;        /* for inlined-function dependencies */
    2262      624952 :     context.active_fns = NIL;   /* nothing being recursively simplified */
    2263      624952 :     context.case_val = NULL;    /* no CASE being examined */
    2264      624952 :     context.estimate = false;   /* safe transformations only */
    2265      624952 :     return eval_const_expressions_mutator(node, &context);
    2266             : }
    2267             : 
    2268             : /*--------------------
    2269             :  * estimate_expression_value
    2270             :  *
    2271             :  * This function attempts to estimate the value of an expression for
    2272             :  * planning purposes.  It is in essence a more aggressive version of
    2273             :  * eval_const_expressions(): we will perform constant reductions that are
    2274             :  * not necessarily 100% safe, but are reasonable for estimation purposes.
    2275             :  *
    2276             :  * Currently the extra steps that are taken in this mode are:
    2277             :  * 1. Substitute values for Params, where a bound Param value has been made
    2278             :  *    available by the caller of planner(), even if the Param isn't marked
    2279             :  *    constant.  This effectively means that we plan using the first supplied
    2280             :  *    value of the Param.
    2281             :  * 2. Fold stable, as well as immutable, functions to constants.
    2282             :  * 3. Reduce PlaceHolderVar nodes to their contained expressions.
    2283             :  *--------------------
    2284             :  */
    2285             : Node *
    2286      398570 : estimate_expression_value(PlannerInfo *root, Node *node)
    2287             : {
    2288             :     eval_const_expressions_context context;
    2289             : 
    2290      398570 :     context.boundParams = root->glob->boundParams;    /* bound Params */
    2291             :     /* we do not need to mark the plan as depending on inlined functions */
    2292      398570 :     context.root = NULL;
    2293      398570 :     context.active_fns = NIL;   /* nothing being recursively simplified */
    2294      398570 :     context.case_val = NULL;    /* no CASE being examined */
    2295      398570 :     context.estimate = true;    /* unsafe transformations OK */
    2296      398570 :     return eval_const_expressions_mutator(node, &context);
    2297             : }
    2298             : 
    2299             : /*
    2300             :  * The generic case in eval_const_expressions_mutator is to recurse using
    2301             :  * expression_tree_mutator, which will copy the given node unchanged but
    2302             :  * const-simplify its arguments (if any) as far as possible.  If the node
    2303             :  * itself does immutable processing, and each of its arguments were reduced
    2304             :  * to a Const, we can then reduce it to a Const using evaluate_expr.  (Some
    2305             :  * node types need more complicated logic; for example, a CASE expression
    2306             :  * might be reducible to a constant even if not all its subtrees are.)
    2307             :  */
    2308             : #define ece_generic_processing(node) \
    2309             :     expression_tree_mutator((Node *) (node), eval_const_expressions_mutator, \
    2310             :                             (void *) context)
    2311             : 
    2312             : /*
    2313             :  * Check whether all arguments of the given node were reduced to Consts.
    2314             :  * By going directly to expression_tree_walker, contain_non_const_walker
    2315             :  * is not applied to the node itself, only to its children.
    2316             :  */
    2317             : #define ece_all_arguments_const(node) \
    2318             :     (!expression_tree_walker((Node *) (node), contain_non_const_walker, NULL))
    2319             : 
    2320             : /* Generic macro for applying evaluate_expr */
    2321             : #define ece_evaluate_expr(node) \
    2322             :     ((Node *) evaluate_expr((Expr *) (node), \
    2323             :                             exprType((Node *) (node)), \
    2324             :                             exprTypmod((Node *) (node)), \
    2325             :                             exprCollation((Node *) (node))))
    2326             : 
    2327             : /*
    2328             :  * Recursive guts of eval_const_expressions/estimate_expression_value
    2329             :  */
    2330             : static Node *
    2331     5044028 : eval_const_expressions_mutator(Node *node,
    2332             :                                eval_const_expressions_context *context)
    2333             : {
    2334     5044028 :     if (node == NULL)
    2335      247120 :         return NULL;
    2336     4796908 :     switch (nodeTag(node))
    2337             :     {
    2338             :         case T_Param:
    2339             :             {
    2340      101016 :                 Param      *param = (Param *) node;
    2341      101016 :                 ParamListInfo paramLI = context->boundParams;
    2342             : 
    2343             :                 /* Look to see if we've been given a value for this Param */
    2344      101016 :                 if (param->paramkind == PARAM_EXTERN &&
    2345       19888 :                     paramLI != NULL &&
    2346       39776 :                     param->paramid > 0 &&
    2347       19888 :                     param->paramid <= paramLI->numParams)
    2348             :                 {
    2349             :                     ParamExternData *prm;
    2350             :                     ParamExternData prmdata;
    2351             : 
    2352             :                     /*
    2353             :                      * Give hook a chance in case parameter is dynamic.  Tell
    2354             :                      * it that this fetch is speculative, so it should avoid
    2355             :                      * erroring out if parameter is unavailable.
    2356             :                      */
    2357       19888 :                     if (paramLI->paramFetch != NULL)
    2358        2426 :                         prm = paramLI->paramFetch(paramLI, param->paramid,
    2359             :                                                   true, &prmdata);
    2360             :                     else
    2361       17462 :                         prm = &paramLI->params[param->paramid - 1];
    2362             : 
    2363             :                     /*
    2364             :                      * We don't just check OidIsValid, but insist that the
    2365             :                      * fetched type match the Param, just in case the hook did
    2366             :                      * something unexpected.  No need to throw an error here
    2367             :                      * though; leave that for runtime.
    2368             :                      */
    2369       39776 :                     if (OidIsValid(prm->ptype) &&
    2370       19888 :                         prm->ptype == param->paramtype)
    2371             :                     {
    2372             :                         /* OK to substitute parameter value? */
    2373       39768 :                         if (context->estimate ||
    2374       19882 :                             (prm->pflags & PARAM_FLAG_CONST))
    2375             :                         {
    2376             :                             /*
    2377             :                              * Return a Const representing the param value.
    2378             :                              * Must copy pass-by-ref datatypes, since the
    2379             :                              * Param might be in a memory context
    2380             :                              * shorter-lived than our output plan should be.
    2381             :                              */
    2382             :                             int16       typLen;
    2383             :                             bool        typByVal;
    2384             :                             Datum       pval;
    2385             : 
    2386       19882 :                             get_typlenbyval(param->paramtype,
    2387             :                                             &typLen, &typByVal);
    2388       19882 :                             if (prm->isnull || typByVal)
    2389       15794 :                                 pval = prm->value;
    2390             :                             else
    2391        4088 :                                 pval = datumCopy(prm->value, typByVal, typLen);
    2392       39764 :                             return (Node *) makeConst(param->paramtype,
    2393             :                                                       param->paramtypmod,
    2394             :                                                       param->paramcollid,
    2395             :                                                       (int) typLen,
    2396             :                                                       pval,
    2397       19882 :                                                       prm->isnull,
    2398             :                                                       typByVal);
    2399             :                         }
    2400             :                     }
    2401             :                 }
    2402             : 
    2403             :                 /*
    2404             :                  * Not replaceable, so just copy the Param (no need to
    2405             :                  * recurse)
    2406             :                  */
    2407       81134 :                 return (Node *) copyObject(param);
    2408             :             }
    2409             :         case T_WindowFunc:
    2410             :             {
    2411        1400 :                 WindowFunc *expr = (WindowFunc *) node;
    2412        1400 :                 Oid         funcid = expr->winfnoid;
    2413             :                 List       *args;
    2414             :                 Expr       *aggfilter;
    2415             :                 HeapTuple   func_tuple;
    2416             :                 WindowFunc *newexpr;
    2417             : 
    2418             :                 /*
    2419             :                  * We can't really simplify a WindowFunc node, but we mustn't
    2420             :                  * just fall through to the default processing, because we
    2421             :                  * have to apply expand_function_arguments to its argument
    2422             :                  * list.  That takes care of inserting default arguments and
    2423             :                  * expanding named-argument notation.
    2424             :                  */
    2425        1400 :                 func_tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    2426        1400 :                 if (!HeapTupleIsValid(func_tuple))
    2427           0 :                     elog(ERROR, "cache lookup failed for function %u", funcid);
    2428             : 
    2429        1400 :                 args = expand_function_arguments(expr->args, expr->wintype,
    2430             :                                                  func_tuple);
    2431             : 
    2432        1400 :                 ReleaseSysCache(func_tuple);
    2433             : 
    2434             :                 /* Now, recursively simplify the args (which are a List) */
    2435        1400 :                 args = (List *)
    2436             :                     expression_tree_mutator((Node *) args,
    2437             :                                             eval_const_expressions_mutator,
    2438             :                                             (void *) context);
    2439             :                 /* ... and the filter expression, which isn't */
    2440        1400 :                 aggfilter = (Expr *)
    2441        1400 :                     eval_const_expressions_mutator((Node *) expr->aggfilter,
    2442             :                                                    context);
    2443             : 
    2444             :                 /* And build the replacement WindowFunc node */
    2445        1400 :                 newexpr = makeNode(WindowFunc);
    2446        1400 :                 newexpr->winfnoid = expr->winfnoid;
    2447        1400 :                 newexpr->wintype = expr->wintype;
    2448        1400 :                 newexpr->wincollid = expr->wincollid;
    2449        1400 :                 newexpr->inputcollid = expr->inputcollid;
    2450        1400 :                 newexpr->args = args;
    2451        1400 :                 newexpr->aggfilter = aggfilter;
    2452        1400 :                 newexpr->winref = expr->winref;
    2453        1400 :                 newexpr->winstar = expr->winstar;
    2454        1400 :                 newexpr->winagg = expr->winagg;
    2455        1400 :                 newexpr->location = expr->location;
    2456             : 
    2457        1400 :                 return (Node *) newexpr;
    2458             :             }
    2459             :         case T_FuncExpr:
    2460             :             {
    2461      272746 :                 FuncExpr   *expr = (FuncExpr *) node;
    2462      272746 :                 List       *args = expr->args;
    2463             :                 Expr       *simple;
    2464             :                 FuncExpr   *newexpr;
    2465             : 
    2466             :                 /*
    2467             :                  * Code for op/func reduction is pretty bulky, so split it out
    2468             :                  * as a separate function.  Note: exprTypmod normally returns
    2469             :                  * -1 for a FuncExpr, but not when the node is recognizably a
    2470             :                  * length coercion; we want to preserve the typmod in the
    2471             :                  * eventual Const if so.
    2472             :                  */
    2473      272746 :                 simple = simplify_function(expr->funcid,
    2474             :                                            expr->funcresulttype,
    2475             :                                            exprTypmod(node),
    2476             :                                            expr->funccollid,
    2477             :                                            expr->inputcollid,
    2478             :                                            &args,
    2479      272746 :                                            expr->funcvariadic,
    2480             :                                            true,
    2481             :                                            true,
    2482             :                                            context);
    2483      271858 :                 if (simple)     /* successfully simplified it */
    2484       55744 :                     return (Node *) simple;
    2485             : 
    2486             :                 /*
    2487             :                  * The expression cannot be simplified any further, so build
    2488             :                  * and return a replacement FuncExpr node using the
    2489             :                  * possibly-simplified arguments.  Note that we have also
    2490             :                  * converted the argument list to positional notation.
    2491             :                  */
    2492      216114 :                 newexpr = makeNode(FuncExpr);
    2493      216114 :                 newexpr->funcid = expr->funcid;
    2494      216114 :                 newexpr->funcresulttype = expr->funcresulttype;
    2495      216114 :                 newexpr->funcretset = expr->funcretset;
    2496      216114 :                 newexpr->funcvariadic = expr->funcvariadic;
    2497      216114 :                 newexpr->funcformat = expr->funcformat;
    2498      216114 :                 newexpr->funccollid = expr->funccollid;
    2499      216114 :                 newexpr->inputcollid = expr->inputcollid;
    2500      216114 :                 newexpr->args = args;
    2501      216114 :                 newexpr->location = expr->location;
    2502      216114 :                 return (Node *) newexpr;
    2503             :             }
    2504             :         case T_OpExpr:
    2505             :             {
    2506      350532 :                 OpExpr     *expr = (OpExpr *) node;
    2507      350532 :                 List       *args = expr->args;
    2508             :                 Expr       *simple;
    2509             :                 OpExpr     *newexpr;
    2510             : 
    2511             :                 /*
    2512             :                  * Need to get OID of underlying function.  Okay to scribble
    2513             :                  * on input to this extent.
    2514             :                  */
    2515      350532 :                 set_opfuncid(expr);
    2516             : 
    2517             :                 /*
    2518             :                  * Code for op/func reduction is pretty bulky, so split it out
    2519             :                  * as a separate function.
    2520             :                  */
    2521      350532 :                 simple = simplify_function(expr->opfuncid,
    2522             :                                            expr->opresulttype, -1,
    2523             :                                            expr->opcollid,
    2524             :                                            expr->inputcollid,
    2525             :                                            &args,
    2526             :                                            false,
    2527             :                                            true,
    2528             :                                            true,
    2529             :                                            context);
    2530      350086 :                 if (simple)     /* successfully simplified it */
    2531        8608 :                     return (Node *) simple;
    2532             : 
    2533             :                 /*
    2534             :                  * If the operator is boolean equality or inequality, we know
    2535             :                  * how to simplify cases involving one constant and one
    2536             :                  * non-constant argument.
    2537             :                  */
    2538      682820 :                 if (expr->opno == BooleanEqualOperator ||
    2539      341342 :                     expr->opno == BooleanNotEqualOperator)
    2540             :                 {
    2541         204 :                     simple = (Expr *) simplify_boolean_equality(expr->opno,
    2542             :                                                                 args);
    2543         204 :                     if (simple) /* successfully simplified it */
    2544         104 :                         return (Node *) simple;
    2545             :                 }
    2546             : 
    2547             :                 /*
    2548             :                  * The expression cannot be simplified any further, so build
    2549             :                  * and return a replacement OpExpr node using the
    2550             :                  * possibly-simplified arguments.
    2551             :                  */
    2552      341374 :                 newexpr = makeNode(OpExpr);
    2553      341374 :                 newexpr->opno = expr->opno;
    2554      341374 :                 newexpr->opfuncid = expr->opfuncid;
    2555      341374 :                 newexpr->opresulttype = expr->opresulttype;
    2556      341374 :                 newexpr->opretset = expr->opretset;
    2557      341374 :                 newexpr->opcollid = expr->opcollid;
    2558      341374 :                 newexpr->inputcollid = expr->inputcollid;
    2559      341374 :                 newexpr->args = args;
    2560      341374 :                 newexpr->location = expr->location;
    2561      341374 :                 return (Node *) newexpr;
    2562             :             }
    2563             :         case T_DistinctExpr:
    2564             :             {
    2565         460 :                 DistinctExpr *expr = (DistinctExpr *) node;
    2566             :                 List       *args;
    2567             :                 ListCell   *arg;
    2568         460 :                 bool        has_null_input = false;
    2569         460 :                 bool        all_null_input = true;
    2570         460 :                 bool        has_nonconst_input = false;
    2571             :                 Expr       *simple;
    2572             :                 DistinctExpr *newexpr;
    2573             : 
    2574             :                 /*
    2575             :                  * Reduce constants in the DistinctExpr's arguments.  We know
    2576             :                  * args is either NIL or a List node, so we can call
    2577             :                  * expression_tree_mutator directly rather than recursing to
    2578             :                  * self.
    2579             :                  */
    2580         460 :                 args = (List *) expression_tree_mutator((Node *) expr->args,
    2581             :                                                         eval_const_expressions_mutator,
    2582             :                                                         (void *) context);
    2583             : 
    2584             :                 /*
    2585             :                  * We must do our own check for NULLs because DistinctExpr has
    2586             :                  * different results for NULL input than the underlying
    2587             :                  * operator does.
    2588             :                  */
    2589        1380 :                 foreach(arg, args)
    2590             :                 {
    2591         920 :                     if (IsA(lfirst(arg), Const))
    2592             :                     {
    2593          52 :                         has_null_input |= ((Const *) lfirst(arg))->constisnull;
    2594          52 :                         all_null_input &= ((Const *) lfirst(arg))->constisnull;
    2595             :                     }
    2596             :                     else
    2597         868 :                         has_nonconst_input = true;
    2598             :                 }
    2599             : 
    2600             :                 /* all constants? then can optimize this out */
    2601         460 :                 if (!has_nonconst_input)
    2602             :                 {
    2603             :                     /* all nulls? then not distinct */
    2604          16 :                     if (all_null_input)
    2605           0 :                         return makeBoolConst(false, false);
    2606             : 
    2607             :                     /* one null? then distinct */
    2608          16 :                     if (has_null_input)
    2609           0 :                         return makeBoolConst(true, false);
    2610             : 
    2611             :                     /* otherwise try to evaluate the '=' operator */
    2612             :                     /* (NOT okay to try to inline it, though!) */
    2613             : 
    2614             :                     /*
    2615             :                      * Need to get OID of underlying function.  Okay to
    2616             :                      * scribble on input to this extent.
    2617             :                      */
    2618          16 :                     set_opfuncid((OpExpr *) expr);  /* rely on struct
    2619             :                                                      * equivalence */
    2620             : 
    2621             :                     /*
    2622             :                      * Code for op/func reduction is pretty bulky, so split it
    2623             :                      * out as a separate function.
    2624             :                      */
    2625          16 :                     simple = simplify_function(expr->opfuncid,
    2626             :                                                expr->opresulttype, -1,
    2627             :                                                expr->opcollid,
    2628             :                                                expr->inputcollid,
    2629             :                                                &args,
    2630             :                                                false,
    2631             :                                                false,
    2632             :                                                false,
    2633             :                                                context);
    2634          16 :                     if (simple) /* successfully simplified it */
    2635             :                     {
    2636             :                         /*
    2637             :                          * Since the underlying operator is "=", must negate
    2638             :                          * its result
    2639             :                          */
    2640          16 :                         Const      *csimple = castNode(Const, simple);
    2641             : 
    2642          16 :                         csimple->constvalue =
    2643          16 :                             BoolGetDatum(!DatumGetBool(csimple->constvalue));
    2644          16 :                         return (Node *) csimple;
    2645             :                     }
    2646             :                 }
    2647             : 
    2648             :                 /*
    2649             :                  * The expression cannot be simplified any further, so build
    2650             :                  * and return a replacement DistinctExpr node using the
    2651             :                  * possibly-simplified arguments.
    2652             :                  */
    2653         444 :                 newexpr = makeNode(DistinctExpr);
    2654         444 :                 newexpr->opno = expr->opno;
    2655         444 :                 newexpr->opfuncid = expr->opfuncid;
    2656         444 :                 newexpr->opresulttype = expr->opresulttype;
    2657         444 :                 newexpr->opretset = expr->opretset;
    2658         444 :                 newexpr->opcollid = expr->opcollid;
    2659         444 :                 newexpr->inputcollid = expr->inputcollid;
    2660         444 :                 newexpr->args = args;
    2661         444 :                 newexpr->location = expr->location;
    2662         444 :                 return (Node *) newexpr;
    2663             :             }
    2664             :         case T_ScalarArrayOpExpr:
    2665             :             {
    2666             :                 ScalarArrayOpExpr *saop;
    2667             : 
    2668             :                 /* Copy the node and const-simplify its arguments */
    2669       20280 :                 saop = (ScalarArrayOpExpr *) ece_generic_processing(node);
    2670             : 
    2671             :                 /* Make sure we know underlying function */
    2672       20280 :                 set_sa_opfuncid(saop);
    2673             : 
    2674             :                 /*
    2675             :                  * If all arguments are Consts, and it's a safe function, we
    2676             :                  * can fold to a constant
    2677             :                  */
    2678       20408 :                 if (ece_all_arguments_const(saop) &&
    2679         128 :                     ece_function_is_safe(saop->opfuncid, context))
    2680         128 :                     return ece_evaluate_expr(saop);
    2681       20152 :                 return (Node *) saop;
    2682             :             }
    2683             :         case T_BoolExpr:
    2684             :             {
    2685       93924 :                 BoolExpr   *expr = (BoolExpr *) node;
    2686             : 
    2687       93924 :                 switch (expr->boolop)
    2688             :                 {
    2689             :                     case OR_EXPR:
    2690             :                         {
    2691             :                             List       *newargs;
    2692        6632 :                             bool        haveNull = false;
    2693        6632 :                             bool        forceTrue = false;
    2694             : 
    2695        6632 :                             newargs = simplify_or_arguments(expr->args,
    2696             :                                                             context,
    2697             :                                                             &haveNull,
    2698             :                                                             &forceTrue);
    2699        6632 :                             if (forceTrue)
    2700          28 :                                 return makeBoolConst(true, false);
    2701        6604 :                             if (haveNull)
    2702          20 :                                 newargs = lappend(newargs,
    2703          20 :                                                   makeBoolConst(false, true));
    2704             :                             /* If all the inputs are FALSE, result is FALSE */
    2705        6604 :                             if (newargs == NIL)
    2706           0 :                                 return makeBoolConst(false, false);
    2707             : 
    2708             :                             /*
    2709             :                              * If only one nonconst-or-NULL input, it's the
    2710             :                              * result
    2711             :                              */
    2712        6604 :                             if (list_length(newargs) == 1)
    2713           8 :                                 return (Node *) linitial(newargs);
    2714             :                             /* Else we still need an OR node */
    2715        6596 :                             return (Node *) make_orclause(newargs);
    2716             :                         }
    2717             :                     case AND_EXPR:
    2718             :                         {
    2719             :                             List       *newargs;
    2720       75662 :                             bool        haveNull = false;
    2721       75662 :                             bool        forceFalse = false;
    2722             : 
    2723       75662 :                             newargs = simplify_and_arguments(expr->args,
    2724             :                                                              context,
    2725             :                                                              &haveNull,
    2726             :                                                              &forceFalse);
    2727       75662 :                             if (forceFalse)
    2728         718 :                                 return makeBoolConst(false, false);
    2729       74944 :                             if (haveNull)
    2730           4 :                                 newargs = lappend(newargs,
    2731           4 :                                                   makeBoolConst(false, true));
    2732             :                             /* If all the inputs are TRUE, result is TRUE */
    2733       74944 :                             if (newargs == NIL)
    2734         102 :                                 return makeBoolConst(true, false);
    2735             : 
    2736             :                             /*
    2737             :                              * If only one nonconst-or-NULL input, it's the
    2738             :                              * result
    2739             :                              */
    2740       74842 :                             if (list_length(newargs) == 1)
    2741          10 :                                 return (Node *) linitial(newargs);
    2742             :                             /* Else we still need an AND node */
    2743       74832 :                             return (Node *) make_andclause(newargs);
    2744             :                         }
    2745             :                     case NOT_EXPR:
    2746             :                         {
    2747             :                             Node       *arg;
    2748             : 
    2749             :                             Assert(list_length(expr->args) == 1);
    2750       11630 :                             arg = eval_const_expressions_mutator(linitial(expr->args),
    2751             :                                                                  context);
    2752             : 
    2753             :                             /*
    2754             :                              * Use negate_clause() to see if we can simplify
    2755             :                              * away the NOT.
    2756             :                              */
    2757       11630 :                             return negate_clause(arg);
    2758             :                         }
    2759             :                     default:
    2760           0 :                         elog(ERROR, "unrecognized boolop: %d",
    2761             :                              (int) expr->boolop);
    2762             :                         break;
    2763             :                 }
    2764             :                 break;
    2765             :             }
    2766             :         case T_SubPlan:
    2767             :         case T_AlternativeSubPlan:
    2768             : 
    2769             :             /*
    2770             :              * Return a SubPlan unchanged --- too late to do anything with it.
    2771             :              *
    2772             :              * XXX should we ereport() here instead?  Probably this routine
    2773             :              * should never be invoked after SubPlan creation.
    2774             :              */
    2775         320 :             return node;
    2776             :         case T_RelabelType:
    2777             :             {
    2778             :                 /*
    2779             :                  * If we can simplify the input to a constant, then we don't
    2780             :                  * need the RelabelType node anymore: just change the type
    2781             :                  * field of the Const node.  Otherwise, must copy the
    2782             :                  * RelabelType node.
    2783             :                  */
    2784       79018 :                 RelabelType *relabel = (RelabelType *) node;
    2785             :                 Node       *arg;
    2786             : 
    2787       79018 :                 arg = eval_const_expressions_mutator((Node *) relabel->arg,
    2788             :                                                      context);
    2789             : 
    2790             :                 /*
    2791             :                  * If we find stacked RelabelTypes (eg, from foo :: int ::
    2792             :                  * oid) we can discard all but the top one.
    2793             :                  */
    2794      158412 :                 while (arg && IsA(arg, RelabelType))
    2795         376 :                     arg = (Node *) ((RelabelType *) arg)->arg;
    2796             : 
    2797       79018 :                 if (arg && IsA(arg, Const))
    2798             :                 {
    2799       51752 :                     Const      *con = (Const *) arg;
    2800             : 
    2801       51752 :                     con->consttype = relabel->resulttype;
    2802       51752 :                     con->consttypmod = relabel->resulttypmod;
    2803       51752 :                     con->constcollid = relabel->resultcollid;
    2804       51752 :                     return (Node *) con;
    2805             :                 }
    2806             :                 else
    2807             :                 {
    2808       27266 :                     RelabelType *newrelabel = makeNode(RelabelType);
    2809             : 
    2810       27266 :                     newrelabel->arg = (Expr *) arg;
    2811       27266 :                     newrelabel->resulttype = relabel->resulttype;
    2812       27266 :                     newrelabel->resulttypmod = relabel->resulttypmod;
    2813       27266 :                     newrelabel->resultcollid = relabel->resultcollid;
    2814       27266 :                     newrelabel->relabelformat = relabel->relabelformat;
    2815       27266 :                     newrelabel->location = relabel->location;
    2816       27266 :                     return (Node *) newrelabel;
    2817             :                 }
    2818             :             }
    2819             :         case T_CoerceViaIO:
    2820             :             {
    2821        7796 :                 CoerceViaIO *expr = (CoerceViaIO *) node;
    2822             :                 List       *args;
    2823             :                 Oid         outfunc;
    2824             :                 bool        outtypisvarlena;
    2825             :                 Oid         infunc;
    2826             :                 Oid         intypioparam;
    2827             :                 Expr       *simple;
    2828             :                 CoerceViaIO *newexpr;
    2829             : 
    2830             :                 /* Make a List so we can use simplify_function */
    2831        7796 :                 args = list_make1(expr->arg);
    2832             : 
    2833             :                 /*
    2834             :                  * CoerceViaIO represents calling the source type's output
    2835             :                  * function then the result type's input function.  So, try to
    2836             :                  * simplify it as though it were a stack of two such function
    2837             :                  * calls.  First we need to know what the functions are.
    2838             :                  *
    2839             :                  * Note that the coercion functions are assumed not to care
    2840             :                  * about input collation, so we just pass InvalidOid for that.
    2841             :                  */
    2842        7796 :                 getTypeOutputInfo(exprType((Node *) expr->arg),
    2843             :                                   &outfunc, &outtypisvarlena);
    2844        7796 :                 getTypeInputInfo(expr->resulttype,
    2845             :                                  &infunc, &intypioparam);
    2846             : 
    2847        7796 :                 simple = simplify_function(outfunc,
    2848             :                                            CSTRINGOID, -1,
    2849             :                                            InvalidOid,
    2850             :                                            InvalidOid,
    2851             :                                            &args,
    2852             :                                            false,
    2853             :                                            true,
    2854             :                                            true,
    2855             :                                            context);
    2856        7796 :                 if (simple)     /* successfully simplified output fn */
    2857             :                 {
    2858             :                     /*
    2859             :                      * Input functions may want 1 to 3 arguments.  We always
    2860             :                      * supply all three, trusting that nothing downstream will
    2861             :                      * complain.
    2862             :                      */
    2863         706 :                     args = list_make3(simple,
    2864             :                                       makeConst(OIDOID,
    2865             :                                                 -1,
    2866             :                                                 InvalidOid,
    2867             :                                                 sizeof(Oid),
    2868             :                                                 ObjectIdGetDatum(intypioparam),
    2869             :                                                 false,
    2870             :                                                 true),
    2871             :                                       makeConst(INT4OID,
    2872             :                                                 -1,
    2873             :                                                 InvalidOid,
    2874             :                                                 sizeof(int32),
    2875             :                                                 Int32GetDatum(-1),
    2876             :                                                 false,
    2877             :                                                 true));
    2878             : 
    2879         706 :                     simple = simplify_function(infunc,
    2880             :                                                expr->resulttype, -1,
    2881             :                                                expr->resultcollid,
    2882             :                                                InvalidOid,
    2883             :                                                &args,
    2884             :                                                false,
    2885             :                                                false,
    2886             :                                                true,
    2887             :                                                context);
    2888         682 :                     if (simple) /* successfully simplified input fn */
    2889         662 :                         return (Node *) simple;
    2890             :                 }
    2891             : 
    2892             :                 /*
    2893             :                  * The expression cannot be simplified any further, so build
    2894             :                  * and return a replacement CoerceViaIO node using the
    2895             :                  * possibly-simplified argument.
    2896             :                  */
    2897        7110 :                 newexpr = makeNode(CoerceViaIO);
    2898        7110 :                 newexpr->arg = (Expr *) linitial(args);
    2899        7110 :                 newexpr->resulttype = expr->resulttype;
    2900        7110 :                 newexpr->resultcollid = expr->resultcollid;
    2901        7110 :                 newexpr->coerceformat = expr->coerceformat;
    2902        7110 :                 newexpr->location = expr->location;
    2903        7110 :                 return (Node *) newexpr;
    2904             :             }
    2905             :         case T_ArrayCoerceExpr:
    2906             :             {
    2907        1704 :                 ArrayCoerceExpr *ac = makeNode(ArrayCoerceExpr);
    2908             :                 Node       *save_case_val;
    2909             : 
    2910             :                 /*
    2911             :                  * Copy the node and const-simplify its arguments.  We can't
    2912             :                  * use ece_generic_processing() here because we need to mess
    2913             :                  * with case_val only while processing the elemexpr.
    2914             :                  */
    2915        1704 :                 memcpy(ac, node, sizeof(ArrayCoerceExpr));
    2916        1704 :                 ac->arg = (Expr *)
    2917        1704 :                     eval_const_expressions_mutator((Node *) ac->arg,
    2918             :                                                    context);
    2919             : 
    2920             :                 /*
    2921             :                  * Set up for the CaseTestExpr node contained in the elemexpr.
    2922             :                  * We must prevent it from absorbing any outer CASE value.
    2923             :                  */
    2924        1704 :                 save_case_val = context->case_val;
    2925        1704 :                 context->case_val = NULL;
    2926             : 
    2927        1704 :                 ac->elemexpr = (Expr *)
    2928        1704 :                     eval_const_expressions_mutator((Node *) ac->elemexpr,
    2929             :                                                    context);
    2930             : 
    2931        1704 :                 context->case_val = save_case_val;
    2932             : 
    2933             :                 /*
    2934             :                  * If constant argument and the per-element expression is
    2935             :                  * immutable, we can simplify the whole thing to a constant.
    2936             :                  * Exception: although contain_mutable_functions considers
    2937             :                  * CoerceToDomain immutable for historical reasons, let's not
    2938             :                  * do so here; this ensures coercion to an array-over-domain
    2939             :                  * does not apply the domain's constraints until runtime.
    2940             :                  */
    2941        3182 :                 if (ac->arg && IsA(ac->arg, Const) &&
    2942        4418 :                     ac->elemexpr && !IsA(ac->elemexpr, CoerceToDomain) &&
    2943        1462 :                     !contain_mutable_functions((Node *) ac->elemexpr))
    2944        1462 :                     return ece_evaluate_expr(ac);
    2945             : 
    2946         242 :                 return (Node *) ac;
    2947             :             }
    2948             :         case T_CollateExpr:
    2949             :             {
    2950             :                 /*
    2951             :                  * If we can simplify the input to a constant, then we don't
    2952             :                  * need the CollateExpr node at all: just change the
    2953             :                  * constcollid field of the Const node.  Otherwise, replace
    2954             :                  * the CollateExpr with a RelabelType. (We do that so as to
    2955             :                  * improve uniformity of expression representation and thus
    2956             :                  * simplify comparison of expressions.)
    2957             :                  */
    2958        2536 :                 CollateExpr *collate = (CollateExpr *) node;
    2959             :                 Node       *arg;
    2960             : 
    2961        2536 :                 arg = eval_const_expressions_mutator((Node *) collate->arg,
    2962             :                                                      context);
    2963             : 
    2964        2536 :                 if (arg && IsA(arg, Const))
    2965             :                 {
    2966        2206 :                     Const      *con = (Const *) arg;
    2967             : 
    2968        2206 :                     con->constcollid = collate->collOid;
    2969        2206 :                     return (Node *) con;
    2970             :                 }
    2971         330 :                 else if (collate->collOid == exprCollation(arg))
    2972             :                 {
    2973             :                     /* Don't need a RelabelType either... */
    2974          54 :                     return arg;
    2975             :                 }
    2976             :                 else
    2977             :                 {
    2978         276 :                     RelabelType *relabel = makeNode(RelabelType);
    2979             : 
    2980         276 :                     relabel->resulttype = exprType(arg);
    2981         276 :                     relabel->resulttypmod = exprTypmod(arg);
    2982         276 :                     relabel->resultcollid = collate->collOid;
    2983         276 :                     relabel->relabelformat = COERCE_IMPLICIT_CAST;
    2984         276 :                     relabel->location = collate->location;
    2985             : 
    2986             :                     /* Don't create stacked RelabelTypes */
    2987         554 :                     while (arg && IsA(arg, RelabelType))
    2988           2 :                         arg = (Node *) ((RelabelType *) arg)->arg;
    2989         276 :                     relabel->arg = (Expr *) arg;
    2990             : 
    2991         276 :                     return (Node *) relabel;
    2992             :                 }
    2993             :             }
    2994             :         case T_CaseExpr:
    2995             :             {
    2996             :                 /*----------
    2997             :                  * CASE expressions can be simplified if there are constant
    2998             :                  * condition clauses:
    2999             :                  *      FALSE (or NULL): drop the alternative
    3000             :                  *      TRUE: drop all remaining alternatives
    3001             :                  * If the first non-FALSE alternative is a constant TRUE,
    3002             :                  * we can simplify the entire CASE to that alternative's
    3003             :                  * expression.  If there are no non-FALSE alternatives,
    3004             :                  * we simplify the entire CASE to the default result (ELSE).
    3005             :                  *
    3006             :                  * If we have a simple-form CASE with constant test
    3007             :                  * expression, we substitute the constant value for contained
    3008             :                  * CaseTestExpr placeholder nodes, so that we have the
    3009             :                  * opportunity to reduce constant test conditions.  For
    3010             :                  * example this allows
    3011             :                  *      CASE 0 WHEN 0 THEN 1 ELSE 1/0 END
    3012             :                  * to reduce to 1 rather than drawing a divide-by-0 error.
    3013             :                  * Note that when the test expression is constant, we don't
    3014             :                  * have to include it in the resulting CASE; for example
    3015             :                  *      CASE 0 WHEN x THEN y ELSE z END
    3016             :                  * is transformed by the parser to
    3017             :                  *      CASE 0 WHEN CaseTestExpr = x THEN y ELSE z END
    3018             :                  * which we can simplify to
    3019             :                  *      CASE WHEN 0 = x THEN y ELSE z END
    3020             :                  * It is not necessary for the executor to evaluate the "arg"
    3021             :                  * expression when executing the CASE, since any contained
    3022             :                  * CaseTestExprs that might have referred to it will have been
    3023             :                  * replaced by the constant.
    3024             :                  *----------
    3025             :                  */
    3026       26992 :                 CaseExpr   *caseexpr = (CaseExpr *) node;
    3027             :                 CaseExpr   *newcase;
    3028             :                 Node       *save_case_val;
    3029             :                 Node       *newarg;
    3030             :                 List       *newargs;
    3031             :                 bool        const_true_cond;
    3032       26992 :                 Node       *defresult = NULL;
    3033             :                 ListCell   *arg;
    3034             : 
    3035             :                 /* Simplify the test expression, if any */
    3036       26992 :                 newarg = eval_const_expressions_mutator((Node *) caseexpr->arg,
    3037             :                                                         context);
    3038             : 
    3039             :                 /* Set up for contained CaseTestExpr nodes */
    3040       26992 :                 save_case_val = context->case_val;
    3041       26992 :                 if (newarg && IsA(newarg, Const))
    3042             :                 {
    3043          12 :                     context->case_val = newarg;
    3044          12 :                     newarg = NULL;  /* not needed anymore, see above */
    3045             :                 }
    3046             :                 else
    3047       26980 :                     context->case_val = NULL;
    3048             : 
    3049             :                 /* Simplify the WHEN clauses */
    3050       26992 :                 newargs = NIL;
    3051       26992 :                 const_true_cond = false;
    3052       58246 :                 foreach(arg, caseexpr->args)
    3053             :                 {
    3054       31372 :                     CaseWhen   *oldcasewhen = lfirst_node(CaseWhen, arg);
    3055             :                     Node       *casecond;
    3056             :                     Node       *caseresult;
    3057             : 
    3058             :                     /* Simplify this alternative's test condition */
    3059       31372 :                     casecond = eval_const_expressions_mutator((Node *) oldcasewhen->expr,
    3060             :                                                               context);
    3061             : 
    3062             :                     /*
    3063             :                      * If the test condition is constant FALSE (or NULL), then
    3064             :                      * drop this WHEN clause completely, without processing
    3065             :                      * the result.
    3066             :                      */
    3067       31372 :                     if (casecond && IsA(casecond, Const))
    3068             :                     {
    3069         166 :                         Const      *const_input = (Const *) casecond;
    3070             : 
    3071         332 :                         if (const_input->constisnull ||
    3072         166 :                             !DatumGetBool(const_input->constvalue))
    3073          52 :                             continue;   /* drop alternative with FALSE cond */
    3074             :                         /* Else it's constant TRUE */
    3075         114 :                         const_true_cond = true;
    3076             :                     }
    3077             : 
    3078             :                     /* Simplify this alternative's result value */
    3079       31320 :                     caseresult = eval_const_expressions_mutator((Node *) oldcasewhen->result,
    3080             :                                                                 context);
    3081             : 
    3082             :                     /* If non-constant test condition, emit a new WHEN node */
    3083       31316 :                     if (!const_true_cond)
    3084             :                     {
    3085       31202 :                         CaseWhen   *newcasewhen = makeNode(CaseWhen);
    3086             : 
    3087       31202 :                         newcasewhen->expr = (Expr *) casecond;
    3088       31202 :                         newcasewhen->result = (Expr *) caseresult;
    3089       31202 :                         newcasewhen->location = oldcasewhen->location;
    3090       31202 :                         newargs = lappend(newargs, newcasewhen);
    3091       31202 :                         continue;
    3092             :                     }
    3093             : 
    3094             :                     /*
    3095             :                      * Found a TRUE condition, so none of the remaining
    3096             :                      * alternatives can be reached.  We treat the result as
    3097             :                      * the default result.
    3098             :                      */
    3099         114 :                     defresult = caseresult;
    3100         114 :                     break;
    3101             :                 }
    3102             : 
    3103             :                 /* Simplify the default result, unless we replaced it above */
    3104       26988 :                 if (!const_true_cond)
    3105       26874 :                     defresult = eval_const_expressions_mutator((Node *) caseexpr->defresult,
    3106             :                                                                context);
    3107             : 
    3108       26988 :                 context->case_val = save_case_val;
    3109             : 
    3110             :                 /*
    3111             :                  * If no non-FALSE alternatives, CASE reduces to the default
    3112             :                  * result
    3113             :                  */
    3114       26988 :                 if (newargs == NIL)
    3115         154 :                     return defresult;
    3116             :                 /* Otherwise we need a new CASE node */
    3117       26834 :                 newcase = makeNode(CaseExpr);
    3118       26834 :                 newcase->casetype = caseexpr->casetype;
    3119       26834 :                 newcase->casecollid = caseexpr->casecollid;
    3120       26834 :                 newcase->arg = (Expr *) newarg;
    3121       26834 :                 newcase->args = newargs;
    3122       26834 :                 newcase->defresult = (Expr *) defresult;
    3123       26834 :                 newcase->location = caseexpr->location;
    3124       26834 :                 return (Node *) newcase;
    3125             :             }
    3126             :         case T_CaseTestExpr:
    3127             :             {
    3128             :                 /*
    3129             :                  * If we know a constant test value for the current CASE
    3130             :                  * construct, substitute it for the placeholder.  Else just
    3131             :                  * return the placeholder as-is.
    3132             :                  */
    3133       13582 :                 if (context->case_val)
    3134          16 :                     return copyObject(context->case_val);
    3135             :                 else
    3136       13566 :                     return copyObject(node);
    3137             :             }
    3138             :         case T_SubscriptingRef:
    3139             :         case T_ArrayExpr:
    3140             :         case T_RowExpr:
    3141             :         case T_MinMaxExpr:
    3142             :             {
    3143             :                 /*
    3144             :                  * Generic handling for node types whose own processing is
    3145             :                  * known to be immutable, and for which we need no smarts
    3146             :                  * beyond "simplify if all inputs are constants".
    3147             :                  *
    3148             :                  * Treating MinMaxExpr this way amounts to assuming that the
    3149             :                  * btree comparison function it calls is immutable; see the
    3150             :                  * reasoning in contain_mutable_functions_walker.
    3151             :                  */
    3152             : 
    3153             :                 /* Copy the node and const-simplify its arguments */
    3154       17706 :                 node = ece_generic_processing(node);
    3155             :                 /* If all arguments are Consts, we can fold to a constant */
    3156       17706 :                 if (ece_all_arguments_const(node))
    3157       13612 :                     return ece_evaluate_expr(node);
    3158        4094 :                 return node;
    3159             :             }
    3160             :         case T_CoalesceExpr:
    3161             :             {
    3162       22266 :                 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
    3163             :                 CoalesceExpr *newcoalesce;
    3164             :                 List       *newargs;
    3165             :                 ListCell   *arg;
    3166             : 
    3167       22266 :                 newargs = NIL;
    3168       66236 :                 foreach(arg, coalesceexpr->args)
    3169             :                 {
    3170             :                     Node       *e;
    3171             : 
    3172       44524 :                     e = eval_const_expressions_mutator((Node *) lfirst(arg),
    3173             :                                                        context);
    3174             : 
    3175             :                     /*
    3176             :                      * We can remove null constants from the list. For a
    3177             :                      * non-null constant, if it has not been preceded by any
    3178             :                      * other non-null-constant expressions then it is the
    3179             :                      * result. Otherwise, it's the next argument, but we can
    3180             :                      * drop following arguments since they will never be
    3181             :                      * reached.
    3182             :                      */
    3183       44524 :                     if (IsA(e, Const))
    3184             :                     {
    3185         562 :                         if (((Const *) e)->constisnull)
    3186           8 :                             continue;   /* drop null constant */
    3187         554 :                         if (newargs == NIL)
    3188           8 :                             return e;   /* first expr */
    3189         546 :                         newargs = lappend(newargs, e);
    3190         546 :                         break;
    3191             :                     }
    3192       43962 :                     newargs = lappend(newargs, e);
    3193             :                 }
    3194             : 
    3195             :                 /*
    3196             :                  * If all the arguments were constant null, the result is just
    3197             :                  * null
    3198             :                  */
    3199       22258 :                 if (newargs == NIL)
    3200           0 :                     return (Node *) makeNullConst(coalesceexpr->coalescetype,
    3201             :                                                   -1,
    3202             :                                                   coalesceexpr->coalescecollid);
    3203             : 
    3204       22258 :                 newcoalesce = makeNode(CoalesceExpr);
    3205       22258 :                 newcoalesce->coalescetype = coalesceexpr->coalescetype;
    3206       22258 :                 newcoalesce->coalescecollid = coalesceexpr->coalescecollid;
    3207       22258 :                 newcoalesce->args = newargs;
    3208       22258 :                 newcoalesce->location = coalesceexpr->location;
    3209       22258 :                 return (Node *) newcoalesce;
    3210             :             }
    3211             :         case T_SQLValueFunction:
    3212             :             {
    3213             :                 /*
    3214             :                  * All variants of SQLValueFunction are stable, so if we are
    3215             :                  * estimating the expression's value, we should evaluate the
    3216             :                  * current function value.  Otherwise just copy.
    3217             :                  */
    3218        2120 :                 SQLValueFunction *svf = (SQLValueFunction *) node;
    3219             : 
    3220        2120 :                 if (context->estimate)
    3221         488 :                     return (Node *) evaluate_expr((Expr *) svf,
    3222             :                                                   svf->type,
    3223             :                                                   svf->typmod,
    3224             :                                                   InvalidOid);
    3225             :                 else
    3226        1632 :                     return copyObject((Node *) svf);
    3227             :             }
    3228             :         case T_FieldSelect:
    3229             :             {
    3230             :                 /*
    3231             :                  * We can optimize field selection from a whole-row Var into a
    3232             :                  * simple Var.  (This case won't be generated directly by the
    3233             :                  * parser, because ParseComplexProjection short-circuits it.
    3234             :                  * But it can arise while simplifying functions.)  Also, we
    3235             :                  * can optimize field selection from a RowExpr construct, or
    3236             :                  * of course from a constant.
    3237             :                  *
    3238             :                  * However, replacing a whole-row Var in this way has a
    3239             :                  * pitfall: if we've already built the rel targetlist for the
    3240             :                  * source relation, then the whole-row Var is scheduled to be
    3241             :                  * produced by the relation scan, but the simple Var probably
    3242             :                  * isn't, which will lead to a failure in setrefs.c.  This is
    3243             :                  * not a problem when handling simple single-level queries, in
    3244             :                  * which expression simplification always happens first.  It
    3245             :                  * is a risk for lateral references from subqueries, though.
    3246             :                  * To avoid such failures, don't optimize uplevel references.
    3247             :                  *
    3248             :                  * We must also check that the declared type of the field is
    3249             :                  * still the same as when the FieldSelect was created --- this
    3250             :                  * can change if someone did ALTER COLUMN TYPE on the rowtype.
    3251             :                  * If it isn't, we skip the optimization; the case will
    3252             :                  * probably fail at runtime, but that's not our problem here.
    3253             :                  */
    3254        1412 :                 FieldSelect *fselect = (FieldSelect *) node;
    3255             :                 FieldSelect *newfselect;
    3256             :                 Node       *arg;
    3257             : 
    3258        1412 :                 arg = eval_const_expressions_mutator((Node *) fselect->arg,
    3259             :                                                      context);
    3260        1642 :                 if (arg && IsA(arg, Var) &&
    3261         286 :                     ((Var *) arg)->varattno == InvalidAttrNumber &&
    3262          56 :                     ((Var *) arg)->varlevelsup == 0)
    3263             :                 {
    3264          96 :                     if (rowtype_field_matches(((Var *) arg)->vartype,
    3265          48 :                                               fselect->fieldnum,
    3266             :                                               fselect->resulttype,
    3267             :                                               fselect->resulttypmod,
    3268             :                                               fselect->resultcollid))
    3269          96 :                         return (Node *) makeVar(((Var *) arg)->varno,
    3270          48 :                                                 fselect->fieldnum,
    3271             :                                                 fselect->resulttype,
    3272             :                                                 fselect->resulttypmod,
    3273             :                                                 fselect->resultcollid,
    3274             :                                                 ((Var *) arg)->varlevelsup);
    3275             :                 }
    3276        1364 :                 if (arg && IsA(arg, RowExpr))
    3277             :                 {
    3278          16 :                     RowExpr    *rowexpr = (RowExpr *) arg;
    3279             : 
    3280          32 :                     if (fselect->fieldnum > 0 &&
    3281          16 :                         fselect->fieldnum <= list_length(rowexpr->args))
    3282             :                     {
    3283          16 :                         Node       *fld = (Node *) list_nth(rowexpr->args,
    3284          16 :                                                             fselect->fieldnum - 1);
    3285             : 
    3286          32 :                         if (rowtype_field_matches(rowexpr->row_typeid,
    3287          16 :                                                   fselect->fieldnum,
    3288             :                                                   fselect->resulttype,
    3289             :                                                   fselect->resulttypmod,
    3290          16 :                                                   fselect->resultcollid) &&
    3291          32 :                             fselect->resulttype == exprType(fld) &&
    3292          32 :                             fselect->resulttypmod == exprTypmod(fld) &&
    3293          16 :                             fselect->resultcollid == exprCollation(fld))
    3294          16 :                             return fld;
    3295             :                     }
    3296             :                 }
    3297        1348 :                 newfselect = makeNode(FieldSelect);
    3298        1348 :                 newfselect->arg = (Expr *) arg;
    3299        1348 :                 newfselect->fieldnum = fselect->fieldnum;
    3300        1348 :                 newfselect->resulttype = fselect->resulttype;
    3301        1348 :                 newfselect->resulttypmod = fselect->resulttypmod;
    3302        1348 :                 newfselect->resultcollid = fselect->resultcollid;
    3303        1348 :                 if (arg && IsA(arg, Const))
    3304             :                 {
    3305          16 :                     Const      *con = (Const *) arg;
    3306             : 
    3307          32 :                     if (rowtype_field_matches(con->consttype,
    3308          16 :                                               newfselect->fieldnum,
    3309             :                                               newfselect->resulttype,
    3310             :                                               newfselect->resulttypmod,
    3311             :                                               newfselect->resultcollid))
    3312          16 :                         return ece_evaluate_expr(newfselect);
    3313             :                 }
    3314        1332 :                 return (Node *) newfselect;
    3315             :             }
    3316             :         case T_NullTest:
    3317             :             {
    3318       18742 :                 NullTest   *ntest = (NullTest *) node;
    3319             :                 NullTest   *newntest;
    3320             :                 Node       *arg;
    3321             : 
    3322       18742 :                 arg = eval_const_expressions_mutator((Node *) ntest->arg,
    3323             :                                                      context);
    3324       18742 :                 if (ntest->argisrow && arg && IsA(arg, RowExpr))
    3325             :                 {
    3326             :                     /*
    3327             :                      * We break ROW(...) IS [NOT] NULL into separate tests on
    3328             :                      * its component fields.  This form is usually more
    3329             :                      * efficient to evaluate, as well as being more amenable
    3330             :                      * to optimization.
    3331             :                      */
    3332          20 :                     RowExpr    *rarg = (RowExpr *) arg;
    3333          20 :                     List       *newargs = NIL;
    3334             :                     ListCell   *l;
    3335             : 
    3336          80 :                     foreach(l, rarg->args)
    3337             :                     {
    3338          60 :                         Node       *relem = (Node *) lfirst(l);
    3339             : 
    3340             :                         /*
    3341             :                          * A constant field refutes the whole NullTest if it's
    3342             :                          * of the wrong nullness; else we can discard it.
    3343             :                          */
    3344          60 :                         if (relem && IsA(relem, Const))
    3345             :                         {
    3346           0 :                             Const      *carg = (Const *) relem;
    3347             : 
    3348           0 :                             if (carg->constisnull ?
    3349           0 :                                 (ntest->nulltesttype == IS_NOT_NULL) :
    3350           0 :                                 (ntest->nulltesttype == IS_NULL))
    3351           0 :                                 return makeBoolConst(false, false);
    3352           0 :                             continue;
    3353             :                         }
    3354             : 
    3355             :                         /*
    3356             :                          * Else, make a scalar (argisrow == false) NullTest
    3357             :                          * for this field.  Scalar semantics are required
    3358             :                          * because IS [NOT] NULL doesn't recurse; see comments
    3359             :                          * in ExecEvalRowNullInt().
    3360             :                          */
    3361          60 :                         newntest = makeNode(NullTest);
    3362          60 :                         newntest->arg = (Expr *) relem;
    3363          60 :                         newntest->nulltesttype = ntest->nulltesttype;
    3364          60 :                         newntest->argisrow = false;
    3365          60 :                         newntest->location = ntest->location;
    3366          60 :                         newargs = lappend(newargs, newntest);
    3367             :                     }
    3368             :                     /* If all the inputs were constants, result is TRUE */
    3369          20 :                     if (newargs == NIL)
    3370           0 :                         return makeBoolConst(true, false);
    3371             :                     /* If only one nonconst input, it's the result */
    3372          20 :                     if (list_length(newargs) == 1)
    3373           0 :                         return (Node *) linitial(newargs);
    3374             :                     /* Else we need an AND node */
    3375          20 :                     return (Node *) make_andclause(newargs);
    3376             :                 }
    3377       18722 :                 if (!ntest->argisrow && arg && IsA(arg, Const))
    3378             :                 {
    3379        1252 :                     Const      *carg = (Const *) arg;
    3380             :                     bool        result;
    3381             : 
    3382        1252 :                     switch (ntest->nulltesttype)
    3383             :                     {
    3384             :                         case IS_NULL:
    3385         140 :                             result = carg->constisnull;
    3386         140 :                             break;
    3387             :                         case IS_NOT_NULL:
    3388        1112 :                             result = !carg->constisnull;
    3389        1112 :                             break;
    3390             :                         default:
    3391           0 :                             elog(ERROR, "unrecognized nulltesttype: %d",
    3392             :                                  (int) ntest->nulltesttype);
    3393             :                             result = false; /* keep compiler quiet */
    3394             :                             break;
    3395             :                     }
    3396             : 
    3397        1252 :                     return makeBoolConst(result, false);
    3398             :                 }
    3399             : 
    3400       17470 :                 newntest = makeNode(NullTest);
    3401       17470 :                 newntest->arg = (Expr *) arg;
    3402       17470 :                 newntest->nulltesttype = ntest->nulltesttype;
    3403       17470 :                 newntest->argisrow = ntest->argisrow;
    3404       17470 :                 newntest->location = ntest->location;
    3405       17470 :                 return (Node *) newntest;
    3406             :             }
    3407             :         case T_BooleanTest:
    3408             :             {
    3409             :                 /*
    3410             :                  * This case could be folded into the generic handling used
    3411             :                  * for SubscriptingRef etc.  But because the simplification
    3412             :                  * logic is so trivial, applying evaluate_expr() to perform it
    3413             :                  * would be a heavy overhead.  BooleanTest is probably common
    3414             :                  * enough to justify keeping this bespoke implementation.
    3415             :                  */
    3416         410 :                 BooleanTest *btest = (BooleanTest *) node;
    3417             :                 BooleanTest *newbtest;
    3418             :                 Node       *arg;
    3419             : 
    3420         410 :                 arg = eval_const_expressions_mutator((Node *) btest->arg,
    3421             :                                                      context);
    3422         410 :                 if (arg && IsA(arg, Const))
    3423             :                 {
    3424         148 :                     Const      *carg = (Const *) arg;
    3425             :                     bool        result;
    3426             : 
    3427         148 :                     switch (btest->booltesttype)
    3428             :                     {
    3429             :                         case IS_TRUE:
    3430           0 :                             result = (!carg->constisnull &&
    3431           0 :                                       DatumGetBool(carg->constvalue));
    3432           0 :                             break;
    3433             :                         case IS_NOT_TRUE:
    3434         296 :                             result = (carg->constisnull ||
    3435         148 :                                       !DatumGetBool(carg->constvalue));
    3436         148 :                             break;
    3437             :                         case IS_FALSE:
    3438           0 :                             result = (!carg->constisnull &&
    3439           0 :                                       !DatumGetBool(carg->constvalue));
    3440           0 :                             break;
    3441             :                         case IS_NOT_FALSE:
    3442           0 :                             result = (carg->constisnull ||
    3443           0 :                                       DatumGetBool(carg->constvalue));
    3444           0 :                             break;
    3445             :                         case IS_UNKNOWN:
    3446           0 :                             result = carg->constisnull;
    3447           0 :                             break;
    3448             :                         case IS_NOT_UNKNOWN:
    3449           0 :                             result = !carg->constisnull;
    3450           0 :                             break;
    3451             :                         default:
    3452           0 :                             elog(ERROR, "unrecognized booltesttype: %d",
    3453             :                                  (int) btest->booltesttype);
    3454             :                             result = false; /* keep compiler quiet */
    3455             :                             break;
    3456             :                     }
    3457             : 
    3458         148 :                     return makeBoolConst(result, false);
    3459             :                 }
    3460             : 
    3461         262 :                 newbtest = makeNode(BooleanTest);
    3462         262 :                 newbtest->arg = (Expr *) arg;
    3463         262 :                 newbtest->booltesttype = btest->booltesttype;
    3464         262 :                 newbtest->location = btest->location;
    3465         262 :                 return (Node *) newbtest;
    3466             :             }
    3467             :         case T_CoerceToDomain:
    3468             :             {
    3469             :                 /*
    3470             :                  * If the domain currently has no constraints, we replace the
    3471             :                  * CoerceToDomain node with a simple RelabelType, which is
    3472             :                  * both far faster to execute and more amenable to later
    3473             :                  * optimization.  We must then mark the plan as needing to be
    3474             :                  * rebuilt if the domain's constraints change.
    3475             :                  *
    3476             :                  * Also, in estimation mode, always replace CoerceToDomain
    3477             :                  * nodes, effectively assuming that the coercion will succeed.
    3478             :                  */
    3479       93466 :                 CoerceToDomain *cdomain = (CoerceToDomain *) node;
    3480             :                 CoerceToDomain *newcdomain;
    3481             :                 Node       *arg;
    3482             : 
    3483       93466 :                 arg = eval_const_expressions_mutator((Node *) cdomain->arg,
    3484             :                                                      context);
    3485      186884 :                 if (context->estimate ||
    3486       93434 :                     !DomainHasConstraints(cdomain->resulttype))
    3487             :                 {
    3488             :                     /* Record dependency, if this isn't estimation mode */
    3489       66824 :                     if (context->root && !context->estimate)
    3490       66780 :                         record_plan_type_dependency(context->root,
    3491             :                                                     cdomain->resulttype);
    3492             : 
    3493             :                     /* Generate RelabelType to substitute for CoerceToDomain */
    3494             :                     /* This should match the RelabelType logic above */
    3495             : 
    3496      133648 :                     while (arg && IsA(arg, RelabelType))
    3497           0 :                         arg = (Node *) ((RelabelType *) arg)->arg;
    3498             : 
    3499       66824 :                     if (arg && IsA(arg, Const))
    3500             :                     {
    3501       64128 :                         Const      *con = (Const *) arg;
    3502             : 
    3503       64128 :                         con->consttype = cdomain->resulttype;
    3504       64128 :                         con->consttypmod = cdomain->resulttypmod;
    3505       64128 :                         con->constcollid = cdomain->resultcollid;
    3506       64128 :                         return (Node *) con;
    3507             :                     }
    3508             :                     else
    3509             :                     {
    3510        2696 :                         RelabelType *newrelabel = makeNode(RelabelType);
    3511             : 
    3512        2696 :                         newrelabel->arg = (Expr *) arg;
    3513        2696 :                         newrelabel->resulttype = cdomain->resulttype;
    3514        2696 :                         newrelabel->resulttypmod = cdomain->resulttypmod;
    3515        2696 :                         newrelabel->resultcollid = cdomain->resultcollid;
    3516        2696 :                         newrelabel->relabelformat = cdomain->coercionformat;
    3517        2696 :                         newrelabel->location = cdomain->location;
    3518        2696 :                         return (Node *) newrelabel;
    3519             :                     }
    3520             :                 }
    3521             : 
    3522       26626 :                 newcdomain = makeNode(CoerceToDomain);
    3523       26626 :                 newcdomain->arg = (Expr *) arg;
    3524       26626 :                 newcdomain->resulttype = cdomain->resulttype;
    3525       26626 :                 newcdomain->resulttypmod = cdomain->resulttypmod;
    3526       26626 :                 newcdomain->resultcollid = cdomain->resultcollid;
    3527       26626 :                 newcdomain->coercionformat = cdomain->coercionformat;
    3528       26626 :                 newcdomain->location = cdomain->location;
    3529       26626 :                 return (Node *) newcdomain;
    3530             :             }
    3531             :         case T_PlaceHolderVar:
    3532             : 
    3533             :             /*
    3534             :              * In estimation mode, just strip the PlaceHolderVar node
    3535             :              * altogether; this amounts to estimating that the contained value
    3536             :              * won't be forced to null by an outer join.  In regular mode we
    3537             :              * just use the default behavior (ie, simplify the expression but
    3538             :              * leave the PlaceHolderVar node intact).
    3539             :              */
    3540         828 :             if (context->estimate)
    3541             :             {
    3542         152 :                 PlaceHolderVar *phv = (PlaceHolderVar *) node;
    3543             : 
    3544         152 :                 return eval_const_expressions_mutator((Node *) phv->phexpr,
    3545             :                                                       context);
    3546             :             }
    3547         676 :             break;
    3548             :         case T_ConvertRowtypeExpr:
    3549             :             {
    3550          48 :                 ConvertRowtypeExpr *cre = castNode(ConvertRowtypeExpr, node);
    3551             :                 Node       *arg;
    3552             :                 ConvertRowtypeExpr *newcre;
    3553             : 
    3554          48 :                 arg = eval_const_expressions_mutator((Node *) cre->arg,
    3555             :                                                      context);
    3556             : 
    3557          48 :                 newcre = makeNode(ConvertRowtypeExpr);
    3558          48 :                 newcre->resulttype = cre->resulttype;
    3559          48 :                 newcre->convertformat = cre->convertformat;
    3560          48 :                 newcre->location = cre->location;
    3561             : 
    3562             :                 /*
    3563             :                  * In case of a nested ConvertRowtypeExpr, we can convert the
    3564             :                  * leaf row directly to the topmost row format without any
    3565             :                  * intermediate conversions. (This works because
    3566             :                  * ConvertRowtypeExpr is used only for child->parent
    3567             :                  * conversion in inheritance trees, which works by exact match
    3568             :                  * of column name, and a column absent in an intermediate
    3569             :                  * result can't be present in the final result.)
    3570             :                  *
    3571             :                  * No need to check more than one level deep, because the
    3572             :                  * above recursion will have flattened anything else.
    3573             :                  */
    3574          48 :                 if (arg != NULL && IsA(arg, ConvertRowtypeExpr))
    3575             :                 {
    3576           8 :                     ConvertRowtypeExpr *argcre = (ConvertRowtypeExpr *) arg;
    3577             : 
    3578           8 :                     arg = (Node *) argcre->arg;
    3579             : 
    3580             :                     /*
    3581             :                      * Make sure an outer implicit conversion can't hide an
    3582             :                      * inner explicit one.
    3583             :                      */
    3584           8 :                     if (newcre->convertformat == COERCE_IMPLICIT_CAST)
    3585           0 :                         newcre->convertformat = argcre->convertformat;
    3586             :                 }
    3587             : 
    3588          48 :                 newcre->arg = (Expr *) arg;
    3589             : 
    3590          48 :                 if (arg != NULL && IsA(arg, Const))
    3591          12 :                     return ece_evaluate_expr((Node *) newcre);
    3592          36 :                 return (Node *) newcre;
    3593             :             }
    3594             :         default:
    3595     3667604 :             break;
    3596             :     }
    3597             : 
    3598             :     /*
    3599             :      * For any node type not handled above, copy the node unchanged but
    3600             :      * const-simplify its subexpressions.  This is the correct thing for node
    3601             :      * types whose behavior might change between planning and execution, such
    3602             :      * as CurrentOfExpr.  It's also a safe default for new node types not
    3603             :      * known to this routine.
    3604             :      */
    3605     3668280 :     return ece_generic_processing(node);
    3606             : }
    3607             : 
    3608             : /*
    3609             :  * Subroutine for eval_const_expressions: check for non-Const nodes.
    3610             :  *
    3611             :  * We can abort recursion immediately on finding a non-Const node.  This is
    3612             :  * critical for performance, else eval_const_expressions_mutator would take
    3613             :  * O(N^2) time on non-simplifiable trees.  However, we do need to descend
    3614             :  * into List nodes since expression_tree_walker sometimes invokes the walker
    3615             :  * function directly on List subtrees.
    3616             :  */
    3617             : static bool
    3618       83856 : contain_non_const_walker(Node *node, void *context)
    3619             : {
    3620       83856 :     if (node == NULL)
    3621         262 :         return false;
    3622       83594 :     if (IsA(node, Const))
    3623       44484 :         return false;
    3624       39110 :     if (IsA(node, List))
    3625       14864 :         return expression_tree_walker(node, contain_non_const_walker, context);
    3626             :     /* Otherwise, abort the tree traversal and return true */
    3627       24246 :     return true;
    3628             : }
    3629             : 
    3630             : /*
    3631             :  * Subroutine for eval_const_expressions: check if a function is OK to evaluate
    3632             :  */
    3633             : static bool
    3634         128 : ece_function_is_safe(Oid funcid, eval_const_expressions_context *context)
    3635             : {
    3636         128 :     char        provolatile = func_volatile(funcid);
    3637             : 
    3638             :     /*
    3639             :      * Ordinarily we are only allowed to simplify immutable functions. But for
    3640             :      * purposes of estimation, we consider it okay to simplify functions that
    3641             :      * are merely stable; the risk that the result might change from planning
    3642             :      * time to execution time is worth taking in preference to not being able
    3643             :      * to estimate the value at all.
    3644             :      */
    3645         128 :     if (provolatile == PROVOLATILE_IMMUTABLE)
    3646         128 :         return true;
    3647           0 :     if (context->estimate && provolatile == PROVOLATILE_STABLE)
    3648           0 :         return true;
    3649           0 :     return false;
    3650             : }
    3651             : 
    3652             : /*
    3653             :  * Subroutine for eval_const_expressions: process arguments of an OR clause
    3654             :  *
    3655             :  * This includes flattening of nested ORs as well as recursion to
    3656             :  * eval_const_expressions to simplify the OR arguments.
    3657             :  *
    3658             :  * After simplification, OR arguments are handled as follows:
    3659             :  *      non constant: keep
    3660             :  *      FALSE: drop (does not affect result)
    3661             :  *      TRUE: force result to TRUE
    3662             :  *      NULL: keep only one
    3663             :  * We must keep one NULL input because OR expressions evaluate to NULL when no
    3664             :  * input is TRUE and at least one is NULL.  We don't actually include the NULL
    3665             :  * here, that's supposed to be done by the caller.
    3666             :  *
    3667             :  * The output arguments *haveNull and *forceTrue must be initialized false
    3668             :  * by the caller.  They will be set true if a NULL constant or TRUE constant,
    3669             :  * respectively, is detected anywhere in the argument list.
    3670             :  */
    3671             : static List *
    3672        6632 : simplify_or_arguments(List *args,
    3673             :                       eval_const_expressions_context *context,
    3674             :                       bool *haveNull, bool *forceTrue)
    3675             : {
    3676        6632 :     List       *newargs = NIL;
    3677             :     List       *unprocessed_args;
    3678             : 
    3679             :     /*
    3680             :      * We want to ensure that any OR immediately beneath another OR gets
    3681             :      * flattened into a single OR-list, so as to simplify later reasoning.
    3682             :      *
    3683             :      * To avoid stack overflow from recursion of eval_const_expressions, we
    3684             :      * resort to some tenseness here: we keep a list of not-yet-processed
    3685             :      * inputs, and handle flattening of nested ORs by prepending to the to-do
    3686             :      * list instead of recursing.  Now that the parser generates N-argument
    3687             :      * ORs from simple lists, this complexity is probably less necessary than
    3688             :      * it once was, but we might as well keep the logic.
    3689             :      */
    3690        6632 :     unprocessed_args = list_copy(args);
    3691       30352 :     while (unprocessed_args)
    3692             :     {
    3693       17116 :         Node       *arg = (Node *) linitial(unprocessed_args);
    3694             : 
    3695       17116 :         unprocessed_args = list_delete_first(unprocessed_args);
    3696             : 
    3697             :         /* flatten nested ORs as per above comment */
    3698       17116 :         if (is_orclause(arg))
    3699             :         {
    3700           4 :             List       *subargs = ((BoolExpr *) arg)->args;
    3701           4 :             List       *oldlist = unprocessed_args;
    3702             : 
    3703           4 :             unprocessed_args = list_concat_copy(subargs, unprocessed_args);
    3704             :             /* perhaps-overly-tense code to avoid leaking old lists */
    3705           4 :             list_free(oldlist);
    3706           4 :             continue;
    3707             :         }
    3708             : 
    3709             :         /* If it's not an OR, simplify it */
    3710       17112 :         arg = eval_const_expressions_mutator(arg, context);
    3711             : 
    3712             :         /*
    3713             :          * It is unlikely but not impossible for simplification of a non-OR
    3714             :          * clause to produce an OR.  Recheck, but don't be too tense about it
    3715             :          * since it's not a mainstream case.  In particular we don't worry
    3716             :          * about const-simplifying the input twice, nor about list leakage.
    3717             :          */
    3718       17112 :         if (is_orclause(arg))
    3719             :         {
    3720           0 :             List       *subargs = ((BoolExpr *) arg)->args;
    3721             : 
    3722           0 :             unprocessed_args = list_concat_copy(subargs, unprocessed_args);
    3723           0 :             continue;
    3724             :         }
    3725             : 
    3726             :         /*
    3727             :          * OK, we have a const-simplified non-OR argument.  Process it per
    3728             :          * comments above.
    3729             :          */
    3730       17112 :         if (IsA(arg, Const))
    3731             :         {
    3732        1184 :             Const      *const_input = (Const *) arg;
    3733             : 
    3734        1184 :             if (const_input->constisnull)
    3735          24 :                 *haveNull = true;
    3736        1160 :             else if (DatumGetBool(const_input->constvalue))
    3737             :             {
    3738          28 :                 *forceTrue = true;
    3739             : 
    3740             :                 /*
    3741             :                  * Once we detect a TRUE result we can just exit the loop
    3742             :                  * immediately.  However, if we ever add a notion of
    3743             :                  * non-removable functions, we'd need to keep scanning.
    3744             :                  */
    3745          28 :                 return NIL;
    3746             :             }
    3747             :             /* otherwise, we can drop the constant-false input */
    3748        1156 :             continue;
    3749             :         }
    3750             : 
    3751             :         /* else emit the simplified arg into the result list */
    3752       15928 :         newargs = lappend(newargs, arg);
    3753             :     }
    3754             : 
    3755        6604 :     return newargs;
    3756             : }
    3757             : 
    3758             : /*
    3759             :  * Subroutine for eval_const_expressions: process arguments of an AND clause
    3760             :  *
    3761             :  * This includes flattening of nested ANDs as well as recursion to
    3762             :  * eval_const_expressions to simplify the AND arguments.
    3763             :  *
    3764             :  * After simplification, AND arguments are handled as follows:
    3765             :  *      non constant: keep
    3766             :  *      TRUE: drop (does not affect result)
    3767             :  *      FALSE: force result to FALSE
    3768             :  *      NULL: keep only one
    3769             :  * We must keep one NULL input because AND expressions evaluate to NULL when
    3770             :  * no input is FALSE and at least one is NULL.  We don't actually include the
    3771             :  * NULL here, that's supposed to be done by the caller.
    3772             :  *
    3773             :  * The output arguments *haveNull and *forceFalse must be initialized false
    3774             :  * by the caller.  They will be set true if a null constant or false constant,
    3775             :  * respectively, is detected anywhere in the argument list.
    3776             :  */
    3777             : static List *
    3778       75662 : simplify_and_arguments(List *args,
    3779             :                        eval_const_expressions_context *context,
    3780             :                        bool *haveNull, bool *forceFalse)
    3781             : {
    3782       75662 :     List       *newargs = NIL;
    3783             :     List       *unprocessed_args;
    3784             : 
    3785             :     /* See comments in simplify_or_arguments */
    3786       75662 :     unprocessed_args = list_copy(args);
    3787      332262 :     while (unprocessed_args)
    3788             :     {
    3789      181656 :         Node       *arg = (Node *) linitial(unprocessed_args);
    3790             : 
    3791      181656 :         unprocessed_args = list_delete_first(unprocessed_args);
    3792             : 
    3793             :         /* flatten nested ANDs as per above comment */
    3794      181656 :         if (is_andclause(arg))
    3795             :         {
    3796         708 :             List       *subargs = ((BoolExpr *) arg)->args;
    3797         708 :             List       *oldlist = unprocessed_args;
    3798             : 
    3799         708 :             unprocessed_args = list_concat_copy(subargs, unprocessed_args);
    3800             :             /* perhaps-overly-tense code to avoid leaking old lists */
    3801         708 :             list_free(oldlist);
    3802         708 :             continue;
    3803             :         }
    3804             : 
    3805             :         /* If it's not an AND, simplify it */
    3806      180948 :         arg = eval_const_expressions_mutator(arg, context);
    3807             : 
    3808             :         /*
    3809             :          * It is unlikely but not impossible for simplification of a non-AND
    3810             :          * clause to produce an AND.  Recheck, but don't be too tense about it
    3811             :          * since it's not a mainstream case.  In particular we don't worry
    3812             :          * about const-simplifying the input twice, nor about list leakage.
    3813             :          */
    3814      180948 :         if (is_andclause(arg))
    3815             :         {
    3816          12 :             List       *subargs = ((BoolExpr *) arg)->args;
    3817             : 
    3818          12 :             unprocessed_args = list_concat_copy(subargs, unprocessed_args);
    3819          12 :             continue;
    3820             :         }
    3821             : 
    3822             :         /*
    3823             :          * OK, we have a const-simplified non-AND argument.  Process it per
    3824             :          * comments above.
    3825             :          */
    3826      180936 :         if (IsA(arg, Const))
    3827             :         {
    3828        1362 :             Const      *const_input = (Const *) arg;
    3829             : 
    3830        1362 :             if (const_input->constisnull)
    3831          12 :                 *haveNull = true;
    3832        1350 :             else if (!DatumGetBool(const_input->constvalue))
    3833             :             {
    3834         718 :                 *forceFalse = true;
    3835             : 
    3836             :                 /*
    3837             :                  * Once we detect a FALSE result we can just exit the loop
    3838             :                  * immediately.  However, if we ever add a notion of
    3839             :                  * non-removable functions, we'd need to keep scanning.
    3840             :                  */
    3841         718 :                 return NIL;
    3842             :             }
    3843             :             /* otherwise, we can drop the constant-true input */
    3844         644 :             continue;
    3845             :         }
    3846             : 
    3847             :         /* else emit the simplified arg into the result list */
    3848      179574 :         newargs = lappend(newargs, arg);
    3849             :     }
    3850             : 
    3851       74944 :     return newargs;
    3852             : }
    3853             : 
    3854             : /*
    3855             :  * Subroutine for eval_const_expressions: try to simplify boolean equality
    3856             :  * or inequality condition
    3857             :  *
    3858             :  * Inputs are the operator OID and the simplified arguments to the operator.
    3859             :  * Returns a simplified expression if successful, or NULL if cannot
    3860             :  * simplify the expression.
    3861             :  *
    3862             :  * The idea here is to reduce "x = true" to "x" and "x = false" to "NOT x",
    3863             :  * or similarly "x <> true" to "NOT x" and "x <> false" to "x".
    3864             :  * This is only marginally useful in itself, but doing it in constant folding
    3865             :  * ensures that we will recognize these forms as being equivalent in, for
    3866             :  * example, partial index matching.
    3867             :  *
    3868             :  * We come here only if simplify_function has failed; therefore we cannot
    3869             :  * see two constant inputs, nor a constant-NULL input.
    3870             :  */
    3871             : static Node *
    3872         204 : simplify_boolean_equality(Oid opno, List *args)
    3873             : {
    3874             :     Node       *leftop;
    3875             :     Node       *rightop;
    3876             : 
    3877             :     Assert(list_length(args) == 2);
    3878         204 :     leftop = linitial(args);
    3879         204 :     rightop = lsecond(args);
    3880         204 :     if (leftop && IsA(leftop, Const))
    3881             :     {
    3882             :         Assert(!((Const *) leftop)->constisnull);
    3883           0 :         if (opno == BooleanEqualOperator)
    3884             :         {
    3885           0 :             if (DatumGetBool(((Const *) leftop)->constvalue))
    3886           0 :                 return rightop; /* true = foo */
    3887             :             else
    3888           0 :                 return negate_clause(rightop);  /* false = foo */
    3889             :         }
    3890             :         else
    3891             :         {
    3892           0 :             if (DatumGetBool(((Const *) leftop)->constvalue))
    3893           0 :                 return negate_clause(rightop);  /* true <> foo */
    3894             :             else
    3895           0 :                 return rightop; /* false <> foo */
    3896             :         }
    3897             :     }
    3898         204 :     if (rightop && IsA(rightop, Const))
    3899             :     {
    3900             :         Assert(!((Const *) rightop)->constisnull);
    3901         104 :         if (opno == BooleanEqualOperator)
    3902             :         {
    3903         100 :             if (DatumGetBool(((Const *) rightop)->constvalue))
    3904          46 :                 return leftop;  /* foo = true */
    3905             :             else
    3906          54 :                 return negate_clause(leftop);   /* foo = false */
    3907             :         }
    3908             :         else
    3909             :         {
    3910           4 :             if (DatumGetBool(((Const *) rightop)->constvalue))
    3911           0 :                 return negate_clause(leftop);   /* foo <> true */
    3912             :             else
    3913           4 :                 return leftop;  /* foo <> false */
    3914             :         }
    3915             :     }
    3916         100 :     return NULL;
    3917             : }
    3918             : 
    3919             : /*
    3920             :  * Subroutine for eval_const_expressions: try to simplify a function call
    3921             :  * (which might originally have been an operator; we don't care)
    3922             :  *
    3923             :  * Inputs are the function OID, actual result type OID (which is needed for
    3924             :  * polymorphic functions), result typmod, result collation, the input
    3925             :  * collation to use for the function, the original argument list (not
    3926             :  * const-simplified yet, unless process_args is false), and some flags;
    3927             :  * also the context data for eval_const_expressions.
    3928             :  *
    3929             :  * Returns a simplified expression if successful, or NULL if cannot
    3930             :  * simplify the function call.
    3931             :  *
    3932             :  * This function is also responsible for converting named-notation argument
    3933             :  * lists into positional notation and/or adding any needed default argument
    3934             :  * expressions; which is a bit grotty, but it avoids extra fetches of the
    3935             :  * function's pg_proc tuple.  For this reason, the args list is
    3936             :  * pass-by-reference.  Conversion and const-simplification of the args list
    3937             :  * will be done even if simplification of the function call itself is not
    3938             :  * possible.
    3939             :  */
    3940             : static Expr *
    3941      631796 : simplify_function(Oid funcid, Oid result_type, int32 result_typmod,
    3942             :                   Oid result_collid, Oid input_collid, List **args_p,
    3943             :                   bool funcvariadic, bool process_args, bool allow_non_const,
    3944             :                   eval_const_expressions_context *context)
    3945             : {
    3946      631796 :     List       *args = *args_p;
    3947             :     HeapTuple   func_tuple;
    3948             :     Form_pg_proc func_form;
    3949             :     Expr       *newexpr;
    3950             : 
    3951             :     /*
    3952             :      * We have three strategies for simplification: execute the function to
    3953             :      * deliver a constant result, use a transform function to generate a
    3954             :      * substitute node tree, or expand in-line the body of the function
    3955             :      * definition (which only works for simple SQL-language functions, but
    3956             :      * that is a common case).  Each case needs access to the function's
    3957             :      * pg_proc tuple, so fetch it just once.
    3958             :      *
    3959             :      * Note: the allow_non_const flag suppresses both the second and third
    3960             :      * strategies; so if !allow_non_const, simplify_function can only return a
    3961             :      * Const or NULL.  Argument-list rewriting happens anyway, though.
    3962             :      */
    3963      631796 :     func_tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    3964      631796 :     if (!HeapTupleIsValid(func_tuple))
    3965           0 :         elog(ERROR, "cache lookup failed for function %u", funcid);
    3966      631796 :     func_form = (Form_pg_proc) GETSTRUCT(func_tuple);
    3967             : 
    3968             :     /*
    3969             :      * Process the function arguments, unless the caller did it already.
    3970             :      *
    3971             :      * Here we must deal with named or defaulted arguments, and then
    3972             :      * recursively apply eval_const_expressions to the whole argument list.
    3973             :      */
    3974      631796 :     if (process_args)
    3975             :     {
    3976      631074 :         args = expand_function_arguments(args, result_type, func_tuple);
    3977      631074 :         args = (List *) expression_tree_mutator((Node *) args,
    3978             :                                                 eval_const_expressions_mutator,
    3979             :                                                 (void *) context);
    3980             :         /* Argument processing done, give it back to the caller */
    3981      631058 :         *args_p = args;
    3982             :     }
    3983             : 
    3984             :     /* Now attempt simplification of the function call proper. */
    3985             : 
    3986      631780 :     newexpr = evaluate_function(funcid, result_type, result_typmod,
    3987             :                                 result_collid, input_collid,
    3988             :                                 args, funcvariadic,
    3989             :                                 func_tuple, context);
    3990             : 
    3991      630444 :     if (!newexpr && allow_non_const && OidIsValid(func_form->prosupport))
    3992             :     {
    3993             :         /*
    3994             :          * Build a SupportRequestSimplify node to pass to the support
    3995             :          * function, pointing to a dummy FuncExpr node containing the
    3996             :          * simplified arg list.  We use this approach to present a uniform
    3997             :          * interface to the support function regardless of how the target
    3998             :          * function is actually being invoked.
    3999             :          */
    4000             :         SupportRequestSimplify req;
    4001             :         FuncExpr    fexpr;
    4002             : 
    4003       24146 :         fexpr.xpr.type = T_FuncExpr;
    4004       24146 :         fexpr.funcid = funcid;
    4005       24146 :         fexpr.funcresulttype = result_type;
    4006       24146 :         fexpr.funcretset = func_form->proretset;
    4007       24146 :         fexpr.funcvariadic = funcvariadic;
    4008       24146 :         fexpr.funcformat = COERCE_EXPLICIT_CALL;
    4009       24146 :         fexpr.funccollid = result_collid;
    4010       24146 :         fexpr.inputcollid = input_collid;
    4011       24146 :         fexpr.args = args;
    4012       24146 :         fexpr.location = -1;
    4013             : 
    4014       24146 :         req.type = T_SupportRequestSimplify;
    4015       24146 :         req.root = context->root;
    4016       24146 :         req.fcall = &fexpr;
    4017             : 
    4018       24146 :         newexpr = (Expr *)
    4019       24146 :             DatumGetPointer(OidFunctionCall1(func_form->prosupport,
    4020             :                                              PointerGetDatum(&req)));
    4021             : 
    4022             :         /* catch a possible API misunderstanding */
    4023             :         Assert(newexpr != (Expr *) &fexpr);
    4024             :     }
    4025             : 
    4026      630444 :     if (!newexpr && allow_non_const)
    4027      571948 :         newexpr = inline_function(funcid, result_type, result_collid,
    4028             :                                   input_collid, args, funcvariadic,
    4029             :                                   func_tuple, context);
    4030             : 
    4031      630438 :     ReleaseSysCache(func_tuple);
    4032             : 
    4033      630438 :     return newexpr;
    4034             : }
    4035             : 
    4036             : /*
    4037             :  * expand_function_arguments: convert named-notation args to positional args
    4038             :  * and/or insert default args, as needed
    4039             :  *
    4040             :  * If we need to change anything, the input argument list is copied, not
    4041             :  * modified.
    4042             :  *
    4043             :  * Note: this gets applied to operator argument lists too, even though the
    4044             :  * cases it handles should never occur there.  This should be OK since it
    4045             :  * will fall through very quickly if there's nothing to do.
    4046             :  */
    4047             : List *
    4048      632760 : expand_function_arguments(List *args, Oid result_type, HeapTuple func_tuple)
    4049             : {
    4050      632760 :     Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
    4051      632760 :     bool        has_named_args = false;
    4052             :     ListCell   *lc;
    4053             : 
    4054             :     /* Do we have any named arguments? */
    4055     1738568 :     foreach(lc, args)
    4056             :     {
    4057     1106312 :         Node       *arg = (Node *) lfirst(lc);
    4058             : 
    4059     1106312 :         if (IsA(arg, NamedArgExpr))
    4060             :         {
    4061         504 :             has_named_args = true;
    4062         504 :             break;
    4063             :         }
    4064             :     }
    4065             : 
    4066             :     /* If so, we must apply reorder_function_arguments */
    4067      632760 :     if (has_named_args)
    4068             :     {
    4069         504 :         args = reorder_function_arguments(args, func_tuple);
    4070             :         /* Recheck argument types and add casts if needed */
    4071         504 :         recheck_cast_function_args(args, result_type, func_tuple);
    4072             :     }
    4073      632256 :     else if (list_length(args) < funcform->pronargs)
    4074             :     {
    4075             :         /* No named args, but we seem to be short some defaults */
    4076        1830 :         args = add_function_defaults(args, func_tuple);
    4077             :         /* Recheck argument types and add casts if needed */
    4078        1830 :         recheck_cast_function_args(args, result_type, func_tuple);
    4079             :     }
    4080             : 
    4081      632760 :     return args;
    4082             : }
    4083             : 
    4084             : /*
    4085             :  * reorder_function_arguments: convert named-notation args to positional args
    4086             :  *
    4087             :  * This function also inserts default argument values as needed, since it's
    4088             :  * impossible to form a truly valid positional call without that.
    4089             :  */
    4090             : static List *
    4091         504 : reorder_function_arguments(List *args, HeapTuple func_tuple)
    4092             : {
    4093         504 :     Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
    4094         504 :     int         pronargs = funcform->pronargs;
    4095         504 :     int         nargsprovided = list_length(args);
    4096             :     Node       *argarray[FUNC_MAX_ARGS];
    4097             :     ListCell   *lc;
    4098             :     int         i;
    4099             : 
    4100             :     Assert(nargsprovided <= pronargs);
    4101         504 :     if (pronargs > FUNC_MAX_ARGS)
    4102           0 :         elog(ERROR, "too many function arguments");
    4103         504 :     MemSet(argarray, 0, pronargs * sizeof(Node *));
    4104             : 
    4105             :     /* Deconstruct the argument list into an array indexed by argnumber */
    4106         504 :     i = 0;
    4107        1870 :     foreach(lc, args)
    4108             :     {
    4109        1366 :         Node       *arg = (Node *) lfirst(lc);
    4110             : 
    4111        1366 :         if (!IsA(arg, NamedArgExpr))
    4112             :         {
    4113             :             /* positional argument, assumed to precede all named args */
    4114             :             Assert(argarray[i] == NULL);
    4115         578 :             argarray[i++] = arg;
    4116             :         }
    4117             :         else
    4118             :         {
    4119         788 :             NamedArgExpr *na = (NamedArgExpr *) arg;
    4120             : 
    4121             :             Assert(argarray[na->argnumber] == NULL);
    4122         788 :             argarray[na->argnumber] = (Node *) na->arg;
    4123             :         }
    4124             :     }
    4125             : 
    4126             :     /*
    4127             :      * Fetch default expressions, if needed, and insert into array at proper
    4128             :      * locations (they aren't necessarily consecutive or all used)
    4129             :      */
    4130         504 :     if (nargsprovided < pronargs)
    4131             :     {
    4132         396 :         List       *defaults = fetch_function_defaults(func_tuple);
    4133             : 
    4134         396 :         i = pronargs - funcform->pronargdefaults;
    4135        1412 :         foreach(lc, defaults)
    4136             :         {
    4137        1016 :             if (argarray[i] == NULL)
    4138         594 :                 argarray[i] = (Node *) lfirst(lc);
    4139        1016 :             i++;
    4140             :         }
    4141             :     }
    4142             : 
    4143             :     /* Now reconstruct the args list in proper order */
    4144         504 :     args = NIL;
    4145        2464 :     for (i = 0; i < pronargs; i++)
    4146             :     {
    4147             :         Assert(argarray[i] != NULL);
    4148        1960 :         args = lappend(args, argarray[i]);
    4149             :     }
    4150             : 
    4151         504 :     return args;
    4152             : }
    4153             : 
    4154             : /*
    4155             :  * add_function_defaults: add missing function arguments from its defaults
    4156             :  *
    4157             :  * This is used only when the argument list was positional to begin with,
    4158             :  * and so we know we just need to add defaults at the end.
    4159             :  */
    4160             : static List *
    4161        1830 : add_function_defaults(List *args, HeapTuple func_tuple)
    4162             : {
    4163        1830 :     Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
    4164        1830 :     int         nargsprovided = list_length(args);
    4165             :     List       *defaults;
    4166             :     int         ndelete;
    4167             : 
    4168             :     /* Get all the default expressions from the pg_proc tuple */
    4169        1830 :     defaults = fetch_function_defaults(func_tuple);
    4170             : 
    4171             :     /* Delete any unused defaults from the list */
    4172        1830 :     ndelete = nargsprovided + list_length(defaults) - funcform->pronargs;
    4173        1830 :     if (ndelete < 0)
    4174           0 :         elog(ERROR, "not enough default arguments");
    4175        1830 :     if (ndelete > 0)
    4176         112 :         defaults = list_copy_tail(defaults, ndelete);
    4177             : 
    4178             :     /* And form the combined argument list, not modifying the input list */
    4179        1830 :     return list_concat_copy(args, defaults);
    4180             : }
    4181             : 
    4182             : /*
    4183             :  * fetch_function_defaults: get function's default arguments as expression list
    4184             :  */
    4185             : static List *
    4186        2226 : fetch_function_defaults(HeapTuple func_tuple)
    4187             : {
    4188             :     List       *defaults;
    4189             :     Datum       proargdefaults;
    4190             :     bool        isnull;
    4191             :     char       *str;
    4192             : 
    4193             :     /* The error cases here shouldn't happen, but check anyway */
    4194        2226 :     proargdefaults = SysCacheGetAttr(PROCOID, func_tuple,
    4195             :                                      Anum_pg_proc_proargdefaults,
    4196             :                                      &isnull);
    4197        2226 :     if (isnull)
    4198           0 :         elog(ERROR, "not enough default arguments");
    4199        2226 :     str = TextDatumGetCString(proargdefaults);
    4200        2226 :     defaults = castNode(List, stringToNode(str));
    4201        2226 :     pfree(str);
    4202        2226 :     return defaults;
    4203             : }
    4204             : 
    4205             : /*
    4206             :  * recheck_cast_function_args: recheck function args and typecast as needed
    4207             :  * after adding defaults.
    4208             :  *
    4209             :  * It is possible for some of the defaulted arguments to be polymorphic;
    4210             :  * therefore we can't assume that the default expressions have the correct
    4211             :  * data types already.  We have to re-resolve polymorphics and do coercion
    4212             :  * just like the parser did.
    4213             :  *
    4214             :  * This should be a no-op if there are no polymorphic arguments,
    4215             :  * but we do it anyway to be sure.
    4216             :  *
    4217             :  * Note: if any casts are needed, the args list is modified in-place;
    4218             :  * caller should have already copied the list structure.
    4219             :  */
    4220             : static void
    4221        2334 : recheck_cast_function_args(List *args, Oid result_type, HeapTuple func_tuple)
    4222             : {
    4223        2334 :     Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
    4224             :     int         nargs;
    4225             :     Oid         actual_arg_types[FUNC_MAX_ARGS];
    4226             :     Oid         declared_arg_types[FUNC_MAX_ARGS];
    4227             :     Oid         rettype;
    4228             :     ListCell   *lc;
    4229             : 
    4230        2334 :     if (list_length(args) > FUNC_MAX_ARGS)
    4231           0 :         elog(ERROR, "too many function arguments");
    4232        2334 :     nargs = 0;
    4233       10572 :     foreach(lc, args)
    4234             :     {
    4235        8238 :         actual_arg_types[nargs++] = exprType((Node *) lfirst(lc));
    4236             :     }
    4237             :     Assert(nargs == funcform->pronargs);
    4238        2334 :     memcpy(declared_arg_types, funcform->proargtypes.values,
    4239        2334 :            funcform->pronargs * sizeof(Oid));
    4240        2334 :     rettype = enforce_generic_type_consistency(actual_arg_types,
    4241             :                                                declared_arg_types,
    4242             :                                                nargs,
    4243             :                                                funcform->prorettype,
    4244             :                                                false);
    4245             :     /* let's just check we got the same answer as the parser did ... */
    4246        2334 :     if (rettype != result_type)
    4247           0 :         elog(ERROR, "function's resolved result type changed during planning");
    4248             : 
    4249             :     /* perform any necessary typecasting of arguments */
    4250        2334 :     make_fn_arguments(NULL, args, actual_arg_types, declared_arg_types);
    4251        2334 : }
    4252             : 
    4253             : /*
    4254             :  * evaluate_function: try to pre-evaluate a function call
    4255             :  *
    4256             :  * We can do this if the function is strict and has any constant-null inputs
    4257             :  * (just return a null constant), or if the function is immutable and has all
    4258             :  * constant inputs (call it and return the result as a Const node).  In
    4259             :  * estimation mode we are willing to pre-evaluate stable functions too.
    4260             :  *
    4261             :  * Returns a simplified expression if successful, or NULL if cannot
    4262             :  * simplify the function.
    4263             :  */
    4264             : static Expr *
    4265      631780 : evaluate_function(Oid funcid, Oid result_type, int32 result_typmod,
    4266             :                   Oid result_collid, Oid input_collid, List *args,
    4267             :                   bool funcvariadic,
    4268             :                   HeapTuple func_tuple,
    4269             :                   eval_const_expressions_context *context)
    4270             : {
    4271      631780 :     Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
    4272      631780 :     bool        has_nonconst_input = false;
    4273      631780 :     bool        has_null_input = false;
    4274             :     ListCell   *arg;
    4275             :     FuncExpr   *newexpr;
    4276             : 
    4277             :     /*
    4278             :      * Can't simplify if it returns a set.
    4279             :      */
    4280      631780 :     if (funcform->proretset)
    4281       30242 :         return NULL;
    4282             : 
    4283             :     /*
    4284             :      * Can't simplify if it returns RECORD.  The immediate problem is that it
    4285             :      * will be needing an expected tupdesc which we can't supply here.
    4286             :      *
    4287             :      * In the case where it has OUT parameters, it could get by without an
    4288             :      * expected tupdesc, but we still have issues: get_expr_result_type()
    4289             :      * doesn't know how to extract type info from a RECORD constant, and in
    4290             :      * the case of a NULL function result there doesn't seem to be any clean
    4291             :      * way to fix that.  In view of the likelihood of there being still other
    4292             :      * gotchas, seems best to leave the function call unreduced.
    4293             :      */
    4294      601538 :     if (funcform->prorettype == RECORDOID)
    4295        1236 :         return NULL;
    4296             : 
    4297             :     /*
    4298             :      * Check for constant inputs and especially constant-NULL inputs.
    4299             :      */
    4300     1670116 :     foreach(arg, args)
    4301             :     {
    4302     1069814 :         if (IsA(lfirst(arg), Const))
    4303      420152 :             has_null_input |= ((Const *) lfirst(arg))->constisnull;
    4304             :         else
    4305      649662 :             has_nonconst_input = true;
    4306             :     }
    4307             : 
    4308             :     /*
    4309             :      * If the function is strict and has a constant-NULL input, it will never
    4310             :      * be called at all, so we can replace the call by a NULL constant, even
    4311             :      * if there are other inputs that aren't constant, and even if the
    4312             :      * function is not otherwise immutable.
    4313             :      */
    4314      600302 :     if (funcform->proisstrict && has_null_input)
    4315         314 :         return (Expr *) makeNullConst(result_type, result_typmod,
    4316             :                                       result_collid);
    4317             : 
    4318             :     /*
    4319             :      * Otherwise, can simplify only if all inputs are constants. (For a
    4320             :      * non-strict function, constant NULL inputs are treated the same as
    4321             :      * constant non-NULL inputs.)
    4322             :      */
    4323      599988 :     if (has_nonconst_input)
    4324      495324 :         return NULL;
    4325             : 
    4326             :     /*
    4327             :      * Ordinarily we are only allowed to simplify immutable functions. But for
    4328             :      * purposes of estimation, we consider it okay to simplify functions that
    4329             :      * are merely stable; the risk that the result might change from planning
    4330             :      * time to execution time is worth taking in preference to not being able
    4331             :      * to estimate the value at all.
    4332             :      */
    4333      104664 :     if (funcform->provolatile == PROVOLATILE_IMMUTABLE)
    4334             :          /* okay */ ;
    4335       46002 :     else if (context->estimate && funcform->provolatile == PROVOLATILE_STABLE)
    4336             :          /* okay */ ;
    4337             :     else
    4338       45166 :         return NULL;
    4339             : 
    4340             :     /*
    4341             :      * OK, looks like we can simplify this operator/function.
    4342             :      *
    4343             :      * Build a new FuncExpr node containing the already-simplified arguments.
    4344             :      */
    4345       59498 :     newexpr = makeNode(FuncExpr);
    4346       59498 :     newexpr->funcid = funcid;
    4347       59498 :     newexpr->funcresulttype = result_type;
    4348       59498 :     newexpr->funcretset = false;
    4349       59498 :     newexpr->funcvariadic = funcvariadic;
    4350       59498 :     newexpr->funcformat = COERCE_EXPLICIT_CALL; /* doesn't matter */
    4351       59498 :     newexpr->funccollid = result_collid; /* doesn't matter */
    4352       59498 :     newexpr->inputcollid = input_collid;
    4353       59498 :     newexpr->args = args;
    4354       59498 :     newexpr->location = -1;
    4355             : 
    4356       59498 :     return evaluate_expr((Expr *) newexpr, result_type, result_typmod,
    4357             :                          result_collid);
    4358             : }
    4359             : 
    4360             : /*
    4361             :  * inline_function: try to expand a function call inline
    4362             :  *
    4363             :  * If the function is a sufficiently simple SQL-language function
    4364             :  * (just "SELECT expression"), then we can inline it and avoid the rather
    4365             :  * high per-call overhead of SQL functions.  Furthermore, this can expose
    4366             :  * opportunities for constant-folding within the function expression.
    4367             :  *
    4368             :  * We have to beware of some special cases however.  A directly or
    4369             :  * indirectly recursive function would cause us to recurse forever,
    4370             :  * so we keep track of which functions we are already expanding and
    4371             :  * do not re-expand them.  Also, if a parameter is used more than once
    4372             :  * in the SQL-function body, we require it not to contain any volatile
    4373             :  * functions (volatiles might deliver inconsistent answers) nor to be
    4374             :  * unreasonably expensive to evaluate.  The expensiveness check not only
    4375             :  * prevents us from doing multiple evaluations of an expensive parameter
    4376             :  * at runtime, but is a safety value to limit growth of an expression due
    4377             :  * to repeated inlining.
    4378             :  *
    4379             :  * We must also beware of changing the volatility or strictness status of
    4380             :  * functions by inlining them.
    4381             :  *
    4382             :  * Also, at the moment we can't inline functions returning RECORD.  This
    4383             :  * doesn't work in the general case because it discards information such
    4384             :  * as OUT-parameter declarations.
    4385             :  *
    4386             :  * Also, context-dependent expression nodes in the argument list are trouble.
    4387             :  *
    4388             :  * Returns a simplified expression if successful, or NULL if cannot
    4389             :  * simplify the function.
    4390             :  */
    4391             : static Expr *
    4392      571948 : inline_function(Oid funcid, Oid result_type, Oid result_collid,
    4393             :                 Oid input_collid, List *args,
    4394             :                 bool funcvariadic,
    4395             :                 HeapTuple func_tuple,
    4396             :                 eval_const_expressions_context *context)
    4397             : {
    4398      571948 :     Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
    4399             :     char       *src;
    4400             :     Datum       tmp;
    4401             :     bool        isNull;
    4402             :     bool        modifyTargetList;
    4403             :     MemoryContext oldcxt;
    4404             :     MemoryContext mycxt;
    4405             :     inline_error_callback_arg callback_arg;
    4406             :     ErrorContextCallback sqlerrcontext;
    4407             :     FuncExpr   *fexpr;
    4408             :     SQLFunctionParseInfoPtr pinfo;
    4409             :     ParseState *pstate;
    4410             :     List       *raw_parsetree_list;
    4411             :     Query      *querytree;
    4412             :     Node       *newexpr;
    4413             :     int        *usecounts;
    4414             :     ListCell   *arg;
    4415             :     int         i;
    4416             : 
    4417             :     /*
    4418             :      * Forget it if the function is not SQL-language or has other showstopper
    4419             :      * properties.  (The prokind and nargs checks are just paranoia.)
    4420             :      */
    4421      587580 :     if (funcform->prolang != SQLlanguageId ||
    4422       31264 :         funcform->prokind != PROKIND_FUNCTION ||
    4423       31252 :         funcform->prosecdef ||
    4424       30536 :         funcform->proretset ||
    4425       29704 :         funcform->prorettype == RECORDOID ||
    4426       29564 :         !heap_attisnull(func_tuple, Anum_pg_proc_proconfig, NULL) ||
    4427       14776 :         funcform->pronargs != list_length(args))
    4428      557172 :         return NULL;
    4429             : 
    4430             :     /* Check for recursive function, and give up trying to expand if so */
    4431       14776 :     if (list_member_oid(context->active_fns, funcid))
    4432        6212 :         return NULL;
    4433             : 
    4434             :     /* Check permission to call function (fail later, if not) */
    4435        8564 :     if (pg_proc_aclcheck(funcid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
    4436           4 :         return NULL;
    4437             : 
    4438             :     /* Check whether a plugin wants to hook function entry/exit */
    4439        8560 :     if (FmgrHookIsNeeded(funcid))
    4440           0 :         return NULL;
    4441             : 
    4442             :     /*
    4443             :      * Make a temporary memory context, so that we don't leak all the stuff
    4444             :      * that parsing might create.
    4445             :      */
    4446        8560 :     mycxt = AllocSetContextCreate(CurrentMemoryContext,
    4447             :                                   "inline_function",
    4448             :                                   ALLOCSET_DEFAULT_SIZES);
    4449        8560 :     oldcxt = MemoryContextSwitchTo(mycxt);
    4450             : 
    4451             :     /* Fetch the function body */
    4452        8560 :     tmp = SysCacheGetAttr(PROCOID,
    4453             :                           func_tuple,
    4454             :                           Anum_pg_proc_prosrc,
    4455             :                           &isNull);
    4456        8560 :     if (isNull)
    4457           0 :         elog(ERROR, "null prosrc for function %u", funcid);
    4458        8560 :     src = TextDatumGetCString(tmp);
    4459             : 
    4460             :     /*
    4461             :      * Setup error traceback support for ereport().  This is so that we can
    4462             :      * finger the function that bad information came from.
    4463             :      */
    4464        8560 :     callback_arg.proname = NameStr(funcform->proname);
    4465        8560 :     callback_arg.prosrc = src;
    4466             : 
    4467        8560 :     sqlerrcontext.callback = sql_inline_error_callback;
    4468        8560 :     sqlerrcontext.arg = (void *) &callback_arg;
    4469        8560 :     sqlerrcontext.previous = error_context_stack;
    4470        8560 :     error_context_stack = &sqlerrcontext;
    4471             : 
    4472             :     /*
    4473             :      * Set up to handle parameters while parsing the function body.  We need a
    4474             :      * dummy FuncExpr node containing the already-simplified arguments to pass
    4475             :      * to prepare_sql_fn_parse_info.  (It is really only needed if there are
    4476             :      * some polymorphic arguments, but for simplicity we always build it.)
    4477             :      */
    4478        8560 :     fexpr = makeNode(FuncExpr);
    4479        8560 :     fexpr->funcid = funcid;
    4480        8560 :     fexpr->funcresulttype = result_type;
    4481        8560 :     fexpr->funcretset = false;
    4482        8560 :     fexpr->funcvariadic = funcvariadic;
    4483        8560 :     fexpr->funcformat = COERCE_EXPLICIT_CALL;    /* doesn't matter */
    4484        8560 :     fexpr->funccollid = result_collid;   /* doesn't matter */
    4485        8560 :     fexpr->inputcollid = input_collid;
    4486        8560 :     fexpr->args = args;
    4487        8560 :     fexpr->location = -1;
    4488             : 
    4489        8560 :     pinfo = prepare_sql_fn_parse_info(func_tuple,
    4490             :                                       (Node *) fexpr,
    4491             :                                       input_collid);
    4492             : 
    4493             :     /*
    4494             :      * We just do parsing and parse analysis, not rewriting, because rewriting
    4495             :      * will not affect table-free-SELECT-only queries, which is all that we
    4496             :      * care about.  Also, we can punt as soon as we detect more than one
    4497             :      * command in the function body.
    4498             :      */
    4499        8560 :     raw_parsetree_list = pg_parse_query(src);
    4500        8560 :     if (list_length(raw_parsetree_list) != 1)
    4501          32 :         goto fail;
    4502             : 
    4503        8528 :     pstate = make_parsestate(NULL);
    4504        8528 :     pstate->p_sourcetext = src;
    4505        8528 :     sql_fn_parser_setup(pstate, pinfo);
    4506             : 
    4507        8528 :     querytree = transformTopLevelStmt(pstate, linitial(raw_parsetree_list));
    4508             : 
    4509        8522 :     free_parsestate(pstate);
    4510             : 
    4511             :     /*
    4512             :      * The single command must be a simple "SELECT expression".
    4513             :      *
    4514             :      * Note: if you change the tests involved in this, see also plpgsql's
    4515             :      * exec_simple_check_plan().  That generally needs to have the same idea
    4516             :      * of what's a "simple expression", so that inlining a function that
    4517             :      * previously wasn't inlined won't change plpgsql's conclusion.
    4518             :      */
    4519       17044 :     if (!IsA(querytree, Query) ||
    4520       16988 :         querytree->commandType != CMD_SELECT ||
    4521       16890 :         querytree->hasAggs ||
    4522       16848 :         querytree->hasWindowFuncs ||
    4523       16848 :         querytree->hasTargetSRFs ||
    4524       16628 :         querytree->hasSubLinks ||
    4525       16408 :         querytree->cteList ||
    4526       15680 :         querytree->rtable ||
    4527       14952 :         querytree->jointree->fromlist ||
    4528       14952 :         querytree->jointree->quals ||
    4529       14952 :         querytree->groupClause ||
    4530       14952 :         querytree->groupingSets ||
    4531       14952 :         querytree->havingQual ||
    4532       14952 :         querytree->windowClause ||
    4533       14952 :         querytree->distinctClause ||
    4534       14952 :         querytree->sortClause ||
    4535       14952 :         querytree->limitOffset ||
    4536       14906 :         querytree->limitCount ||
    4537       14860 :         querytree->setOperations ||
    4538        7430 :         list_length(querytree->targetList) != 1)
    4539             :         goto fail;
    4540             : 
    4541             :     /*
    4542             :      * Make sure the function (still) returns what it's declared to.  This
    4543             :      * will raise an error if wrong, but that's okay since the function would
    4544             :      * fail at runtime anyway.  Note that check_sql_fn_retval will also insert
    4545             :      * a RelabelType if needed to make the tlist expression match the declared
    4546             :      * type of the function.
    4547             :      *
    4548             :      * Note: we do not try this until we have verified that no rewriting was
    4549             :      * needed; that's probably not important, but let's be careful.
    4550             :      */
    4551        7406 :     if (check_sql_fn_retval(funcid, result_type, list_make1(querytree),
    4552             :                             &modifyTargetList, NULL))
    4553           8 :         goto fail;              /* reject whole-tuple-result cases */
    4554             : 
    4555             :     /* Now we can grab the tlist expression */
    4556        7398 :     newexpr = (Node *) ((TargetEntry *) linitial(querytree->targetList))->expr;
    4557             : 
    4558             :     /*
    4559             :      * If the SQL function returns VOID, we can only inline it if it is a
    4560             :      * SELECT of an expression returning VOID (ie, it's just a redirection to
    4561             :      * another VOID-returning function).  In all non-VOID-returning cases,
    4562             :      * check_sql_fn_retval should ensure that newexpr returns the function's
    4563             :      * declared result type, so this test shouldn't fail otherwise; but we may
    4564             :      * as well cope gracefully if it does.
    4565             :      */
    4566        7398 :     if (exprType(newexpr) != result_type)
    4567          12 :         goto fail;
    4568             : 
    4569             :     /* check_sql_fn_retval couldn't have made any dangerous tlist changes */
    4570             :     Assert(!modifyTargetList);
    4571             : 
    4572             :     /*
    4573             :      * Additional validity checks on the expression.  It mustn't be more
    4574             :      * volatile than the surrounding function (this is to avoid breaking hacks
    4575             :      * that involve pretending a function is immutable when it really ain't).
    4576             :      * If the surrounding function is declared strict, then the expression
    4577             :      * must contain only strict constructs and must use all of the function
    4578             :      * parameters (this is overkill, but an exact analysis is hard).
    4579             :      */
    4580        7670 :     if (funcform->provolatile == PROVOLATILE_IMMUTABLE &&
    4581         284 :         contain_mutable_functions(newexpr))
    4582             :         goto fail;
    4583        7694 :     else if (funcform->provolatile == PROVOLATILE_STABLE &&
    4584         312 :              contain_volatile_functions(newexpr))
    4585           0 :         goto fail;
    4586             : 
    4587        8026 :     if (funcform->proisstrict &&
    4588         644 :         contain_nonstrict_functions(newexpr))
    4589         138 :         goto fail;
    4590             : 
    4591             :     /*
    4592             :      * If any parameter expression contains a context-dependent node, we can't
    4593             :      * inline, for fear of putting such a node into the wrong context.
    4594             :      */
    4595        7244 :     if (contain_context_dependent_node((Node *) args))
    4596           4 :         goto fail;
    4597             : 
    4598             :     /*
    4599             :      * We may be able to do it; there are still checks on parameter usage to
    4600             :      * make, but those are most easily done in combination with the actual
    4601             :      * substitution of the inputs.  So start building expression with inputs
    4602             :      * substituted.
    4603             :      */
    4604        7240 :     usecounts = (int *) palloc0(funcform->pronargs * sizeof(int));
    4605        7240 :     newexpr = substitute_actual_parameters(newexpr, funcform->pronargs,
    4606             :                                            args, usecounts);
    4607             : 
    4608             :     /* Now check for parameter usage */
    4609        7240 :     i = 0;
    4610        8940 :     foreach(arg, args)
    4611             :     {
    4612        1700 :         Node       *param = lfirst(arg);
    4613             : 
    4614        1700 :         if (usecounts[i] == 0)
    4615             :         {
    4616             :             /* Param not used at all: uncool if func is strict */
    4617          40 :             if (funcform->proisstrict)
    4618           0 :                 goto fail;
    4619             :         }
    4620        1660 :         else if (usecounts[i] != 1)
    4621             :         {
    4622             :             /* Param used multiple times: uncool if expensive or volatile */
    4623             :             QualCost    eval_cost;
    4624             : 
    4625             :             /*
    4626             :              * We define "expensive" as "contains any subplan or more than 10
    4627             :              * operators".  Note that the subplan search has to be done
    4628             :              * explicitly, since cost_qual_eval() will barf on unplanned
    4629             :              * subselects.
    4630             :              */
    4631          50 :             if (contain_subplans(param))
    4632           0 :                 goto fail;
    4633          50 :             cost_qual_eval(&eval_cost, list_make1(param), NULL);
    4634         100 :             if (eval_cost.startup + eval_cost.per_tuple >
    4635          50 :                 10 * cpu_operator_cost)
    4636           0 :                 goto fail;
    4637             : 
    4638             :             /*
    4639             :              * Check volatility last since this is more expensive than the
    4640             :              * above tests
    4641             :              */
    4642          50 :             if (contain_volatile_functions(param))
    4643           0 :                 goto fail;
    4644             :         }
    4645        1700 :         i++;
    4646             :     }
    4647             : 
    4648             :     /*
    4649             :      * Whew --- we can make the substitution.  Copy the modified expression
    4650             :      * out of the temporary memory context, and clean up.
    4651             :      */
    4652        7240 :     MemoryContextSwitchTo(oldcxt);
    4653             : 
    4654        7240 :     newexpr = copyObject(newexpr);
    4655             : 
    4656        7240 :     MemoryContextDelete(mycxt);
    4657             : 
    4658             :     /*
    4659             :      * If the result is of a collatable type, force the result to expose the
    4660             :      * correct collation.  In most cases this does not matter, but it's
    4661             :      * possible that the function result is used directly as a sort key or in
    4662             :      * other places where we expect exprCollation() to tell the truth.
    4663             :      */
    4664        7240 :     if (OidIsValid(result_collid))
    4665             :     {
    4666         484 :         Oid         exprcoll = exprCollation(newexpr);
    4667             : 
    4668         484 :         if (OidIsValid(exprcoll) && exprcoll != result_collid)
    4669             :         {
    4670          32 :             CollateExpr *newnode = makeNode(CollateExpr);
    4671             : 
    4672          32 :             newnode->arg = (Expr *) newexpr;
    4673          32 :             newnode->collOid = result_collid;
    4674          32 :             newnode->location = -1;
    4675             : 
    4676          32 :             newexpr = (Node *) newnode;
    4677             :         }
    4678             :     }
    4679             : 
    4680             :     /*
    4681             :      * Since there is now no trace of the function in the plan tree, we must
    4682             :      * explicitly record the plan's dependency on the function.
    4683             :      */
    4684        7240 :     if (context->root)
    4685        7176 :         record_plan_function_dependency(context->root, funcid);
    4686             : 
    4687             :     /*
    4688             :      * Recursively try to simplify the modified expression.  Here we must add
    4689             :      * the current function to the context list of active functions.
    4690             :      */
    4691        7240 :     context->active_fns = lappend_oid(context->active_fns, funcid);
    4692        7240 :     newexpr = eval_const_expressions_mutator(newexpr, context);
    4693        7240 :     context->active_fns = list_delete_last(context->active_fns);
    4694             : 
    4695        7240 :     error_context_stack = sqlerrcontext.previous;
    4696             : 
    4697        7240 :     return (Expr *) newexpr;
    4698             : 
    4699             :     /* Here if func is not inlinable: release temp memory and return NULL */
    4700             : fail:
    4701        1314 :     MemoryContextSwitchTo(oldcxt);
    4702        1314 :     MemoryContextDelete(mycxt);
    4703        1314 :     error_context_stack = sqlerrcontext.previous;
    4704             : 
    4705        1314 :     return NULL;
    4706             : }
    4707             : 
    4708             : /*
    4709             :  * Replace Param nodes by appropriate actual parameters
    4710             :  */
    4711             : static Node *
    4712        7240 : substitute_actual_parameters(Node *expr, int nargs, List *args,
    4713             :                              int *usecounts)
    4714             : {
    4715             :     substitute_actual_parameters_context context;
    4716             : 
    4717        7240 :     context.nargs = nargs;
    4718        7240 :     context.args = args;
    4719        7240 :     context.usecounts = usecounts;
    4720             : 
    4721        7240 :     return substitute_actual_parameters_mutator(expr, &context);
    4722             : }
    4723             : 
    4724             : static Node *
    4725       17854 : substitute_actual_parameters_mutator(Node *node,
    4726             :                                      substitute_actual_parameters_context *context)
    4727             : {
    4728       17854 :     if (node == NULL)
    4729        6416 :         return NULL;
    4730       11438 :     if (IsA(node, Param))
    4731             :     {
    4732        1710 :         Param      *param = (Param *) node;
    4733             : 
    4734        1710 :         if (param->paramkind != PARAM_EXTERN)
    4735           0 :             elog(ERROR, "unexpected paramkind: %d", (int) param->paramkind);
    4736        1710 :         if (param->paramid <= 0 || param->paramid > context->nargs)
    4737           0 :             elog(ERROR, "invalid paramid: %d", param->paramid);
    4738             : 
    4739             :         /* Count usage of parameter */
    4740        1710 :         context->usecounts[param->paramid - 1]++;
    4741             : 
    4742             :         /* Select the appropriate actual arg and replace the Param with it */
    4743             :         /* We don't need to copy at this time (it'll get done later) */
    4744        1710 :         return list_nth(context->args, param->paramid - 1);
    4745             :     }
    4746        9728 :     return expression_tree_mutator(node, substitute_actual_parameters_mutator,
    4747             :                                    (void *) context);
    4748             : }
    4749             : 
    4750             : /*
    4751             :  * error context callback to let us supply a call-stack traceback
    4752             :  */
    4753             : static void
    4754           6 : sql_inline_error_callback(void *arg)
    4755             : {
    4756           6 :     inline_error_callback_arg *callback_arg = (inline_error_callback_arg *) arg;
    4757             :     int         syntaxerrposition;
    4758             : 
    4759             :     /* If it's a syntax error, convert to internal syntax error report */
    4760           6 :     syntaxerrposition = geterrposition();
    4761           6 :     if (syntaxerrposition > 0)
    4762             :     {
    4763           2 :         errposition(0);
    4764           2 :         internalerrposition(syntaxerrposition);
    4765           2 :         internalerrquery(callback_arg->prosrc);
    4766             :     }
    4767             : 
    4768           6 :     errcontext("SQL function \"%s\" during inlining", callback_arg->proname);
    4769           6 : }
    4770             : 
    4771             : /*
    4772             :  * evaluate_expr: pre-evaluate a constant expression
    4773             :  *
    4774             :  * We use the executor's routine ExecEvalExpr() to avoid duplication of
    4775             :  * code and ensure we get the same result as the executor would get.
    4776             :  */
    4777             : Expr *
    4778       82450 : evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod,
    4779             :               Oid result_collation)
    4780             : {
    4781             :     EState     *estate;
    4782             :     ExprState  *exprstate;
    4783             :     MemoryContext oldcontext;
    4784             :     Datum       const_val;
    4785             :     bool        const_is_null;
    4786             :     int16       resultTypLen;
    4787             :     bool        resultTypByVal;
    4788             : 
    4789             :     /*
    4790             :      * To use the executor, we need an EState.
    4791             :      */
    4792       82450 :     estate = CreateExecutorState();
    4793             : 
    4794             :     /* We can use the estate's working context to avoid memory leaks. */
    4795       82450 :     oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
    4796             : 
    4797             :     /* Make sure any opfuncids are filled in. */
    4798       82450 :     fix_opfuncids((Node *) expr);
    4799             : 
    4800             :     /*
    4801             :      * Prepare expr for execution.  (Note: we can't use ExecPrepareExpr
    4802             :      * because it'd result in recursively invoking eval_const_expressions.)
    4803             :      */
    4804       82450 :     exprstate = ExecInitExpr(expr, NULL);
    4805             : 
    4806             :     /*
    4807             :      * And evaluate it.
    4808             :      *
    4809             :      * It is OK to use a default econtext because none of the ExecEvalExpr()
    4810             :      * code used in this situation will use econtext.  That might seem
    4811             :      * fortuitous, but it's not so unreasonable --- a constant expression does
    4812             :      * not depend on context, by definition, n'est ce pas?
    4813             :      */
    4814       82438 :     const_val = ExecEvalExprSwitchContext(exprstate,
    4815       82438 :                                           GetPerTupleExprContext(estate),
    4816             :                                           &const_is_null);
    4817             : 
    4818             :     /* Get info needed about result datatype */
    4819       81094 :     get_typlenbyval(result_type, &resultTypLen, &resultTypByVal);
    4820             : 
    4821             :     /* Get back to outer memory context */
    4822       81094 :     MemoryContextSwitchTo(oldcontext);
    4823             : 
    4824             :     /*
    4825             :      * Must copy result out of sub-context used by expression eval.
    4826             :      *
    4827             :      * Also, if it's varlena, forcibly detoast it.  This protects us against
    4828             :      * storing TOAST pointers into plans that might outlive the referenced
    4829             :      * data.  (makeConst would handle detoasting anyway, but it's worth a few
    4830             :      * extra lines here so that we can do the copy and detoast in one step.)
    4831             :      */
    4832       81094 :     if (!const_is_null)
    4833             :     {
    4834       80292 :         if (resultTypLen == -1)
    4835       41666 :             const_val = PointerGetDatum(PG_DETOAST_DATUM_COPY(const_val));
    4836             :         else
    4837       38626 :             const_val = datumCopy(const_val, resultTypByVal, resultTypLen);
    4838             :     }
    4839             : 
    4840             :     /* Release all the junk we just created */
    4841       81094 :     FreeExecutorState(estate);
    4842             : 
    4843             :     /*
    4844             :      * Make the constant result node.
    4845             :      */
    4846       81094 :     return (Expr *) makeConst(result_type, result_typmod, result_collation,
    4847             :                               resultTypLen,
    4848             :                               const_val, const_is_null,
    4849             :                               resultTypByVal);
    4850             : }
    4851             : 
    4852             : 
    4853             : /*
    4854             :  * inline_set_returning_function
    4855             :  *      Attempt to "inline" a set-returning function in the FROM clause.
    4856             :  *
    4857             :  * "rte" is an RTE_FUNCTION rangetable entry.  If it represents a call of a
    4858             :  * set-returning SQL function that can safely be inlined, expand the function
    4859             :  * and return the substitute Query structure.  Otherwise, return NULL.
    4860             :  *
    4861             :  * We assume that the RTE's expression has already been put through
    4862             :  * eval_const_expressions(), which among other things will take care of
    4863             :  * default arguments and named-argument notation.
    4864             :  *
    4865             :  * This has a good deal of similarity to inline_function(), but that's
    4866             :  * for the non-set-returning case, and there are enough differences to
    4867             :  * justify separate functions.
    4868             :  */
    4869             : Query *
    4870       28560 : inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte)
    4871             : {
    4872             :     RangeTblFunction *rtfunc;
    4873             :     FuncExpr   *fexpr;
    4874             :     Oid         func_oid;
    4875             :     HeapTuple   func_tuple;
    4876             :     Form_pg_proc funcform;
    4877             :     char       *src;
    4878             :     Datum       tmp;
    4879             :     bool        isNull;
    4880             :     bool        modifyTargetList;
    4881             :     MemoryContext oldcxt;
    4882             :     MemoryContext mycxt;
    4883             :     inline_error_callback_arg callback_arg;
    4884             :     ErrorContextCallback sqlerrcontext;
    4885             :     SQLFunctionParseInfoPtr pinfo;
    4886             :     List       *raw_parsetree_list;
    4887             :     List       *querytree_list;
    4888             :     Query      *querytree;
    4889             : 
    4890             :     Assert(rte->rtekind == RTE_FUNCTION);
    4891             : 
    4892             :     /*
    4893             :      * It doesn't make a lot of sense for a SQL SRF to refer to itself in its
    4894             :      * own FROM clause, since that must cause infinite recursion at runtime.
    4895             :      * It will cause this code to recurse too, so check for stack overflow.
    4896             :      * (There's no need to do more.)
    4897             :      */
    4898       28560 :     check_stack_depth();
    4899             : 
    4900             :     /* Fail if the RTE has ORDINALITY - we don't implement that here. */
    4901       28560 :     if (rte->funcordinality)
    4902        6178 :         return NULL;
    4903             : 
    4904             :     /* Fail if RTE isn't a single, simple FuncExpr */
    4905       22382 :     if (list_length(rte->functions) != 1)
    4906          40 :         return NULL;
    4907       22342 :     rtfunc = (RangeTblFunction *) linitial(rte->functions);
    4908             : 
    4909       22342 :     if (!IsA(rtfunc->funcexpr, FuncExpr))
    4910         120 :         return NULL;
    4911       22222 :     fexpr = (FuncExpr *) rtfunc->funcexpr;
    4912             : 
    4913       22222 :     func_oid = fexpr->funcid;
    4914             : 
    4915             :     /*
    4916             :      * The function must be declared to return a set, else inlining would
    4917             :      * change the results if the contained SELECT didn't return exactly one
    4918             :      * row.
    4919             :      */
    4920       22222 :     if (!fexpr->funcretset)
    4921        2090 :         return NULL;
    4922             : 
    4923             :     /*
    4924             :      * Refuse to inline if the arguments contain any volatile functions or
    4925             :      * sub-selects.  Volatile functions are rejected because inlining may
    4926             :      * result in the arguments being evaluated multiple times, risking a
    4927             :      * change in behavior.  Sub-selects are rejected partly for implementation
    4928             :      * reasons (pushing them down another level might change their behavior)
    4929             :      * and partly because they're likely to be expensive and so multiple
    4930             :      * evaluation would be bad.
    4931             :      */
    4932       40204 :     if (contain_volatile_functions((Node *) fexpr->args) ||
    4933       20072 :         contain_subplans((Node *) fexpr->args))
    4934         188 :         return NULL;
    4935             : 
    4936             :     /* Check permission to call function (fail later, if not) */
    4937       19944 :     if (pg_proc_aclcheck(func_oid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
    4938           0 :         return NULL;
    4939             : 
    4940             :     /* Check whether a plugin wants to hook function entry/exit */
    4941       19944 :     if (FmgrHookIsNeeded(func_oid))
    4942           0 :         return NULL;
    4943             : 
    4944             :     /*
    4945             :      * OK, let's take a look at the function's pg_proc entry.
    4946             :      */
    4947       19944 :     func_tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(func_oid));
    4948       19944 :     if (!HeapTupleIsValid(func_tuple))
    4949           0 :         elog(ERROR, "cache lookup failed for function %u", func_oid);
    4950       19944 :     funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
    4951             : 
    4952             :     /*
    4953             :      * Forget it if the function is not SQL-language or has other showstopper
    4954             :      * properties.  In particular it mustn't be declared STRICT, since we
    4955             :      * couldn't enforce that.  It also mustn't be VOLATILE, because that is
    4956             :      * supposed to cause it to be executed with its own snapshot, rather than
    4957             :      * sharing the snapshot of the calling query.  We also disallow returning
    4958             :      * SETOF VOID, because inlining would result in exposing the actual result
    4959             :      * of the function's last SELECT, which should not happen in that case.
    4960             :      * (Rechecking prokind, proretset, and pronargs is just paranoia.)
    4961             :      */
    4962       20228 :     if (funcform->prolang != SQLlanguageId ||
    4963         568 :         funcform->prokind != PROKIND_FUNCTION ||
    4964         540 :         funcform->proisstrict ||
    4965         296 :         funcform->provolatile == PROVOLATILE_VOLATILE ||
    4966          76 :         funcform->prorettype == VOIDOID ||
    4967          72 :         funcform->prosecdef ||
    4968          72 :         !funcform->proretset ||
    4969          72 :         list_length(fexpr->args) != funcform->pronargs ||
    4970          36 :         !heap_attisnull(func_tuple, Anum_pg_proc_proconfig, NULL))
    4971             :     {
    4972       19908 :         ReleaseSysCache(func_tuple);
    4973       19908 :         return NULL;
    4974             :     }
    4975             : 
    4976             :     /*
    4977             :      * Make a temporary memory context, so that we don't leak all the stuff
    4978             :      * that parsing might create.
    4979             :      */
    4980          36 :     mycxt = AllocSetContextCreate(CurrentMemoryContext,
    4981             :                                   "inline_set_returning_function",
    4982             :                                   ALLOCSET_DEFAULT_SIZES);
    4983          36 :     oldcxt = MemoryContextSwitchTo(mycxt);
    4984             : 
    4985             :     /* Fetch the function body */
    4986          36 :     tmp = SysCacheGetAttr(PROCOID,
    4987             :                           func_tuple,
    4988             :                           Anum_pg_proc_prosrc,
    4989             :                           &isNull);
    4990          36 :     if (isNull)
    4991           0 :         elog(ERROR, "null prosrc for function %u", func_oid);
    4992          36 :     src = TextDatumGetCString(tmp);
    4993             : 
    4994             :     /*
    4995             :      * Setup error traceback support for ereport().  This is so that we can
    4996             :      * finger the function that bad information came from.
    4997             :      */
    4998          36 :     callback_arg.proname = NameStr(funcform->proname);
    4999          36 :     callback_arg.prosrc = src;
    5000             : 
    5001          36 :     sqlerrcontext.callback = sql_inline_error_callback;
    5002          36 :     sqlerrcontext.arg = (void *) &callback_arg;
    5003          36 :     sqlerrcontext.previous = error_context_stack;
    5004          36 :     error_context_stack = &sqlerrcontext;
    5005             : 
    5006             :     /*
    5007             :      * Set up to handle parameters while parsing the function body.  We can
    5008             :      * use the FuncExpr just created as the input for
    5009             :      * prepare_sql_fn_parse_info.
    5010             :      */
    5011          36 :     pinfo = prepare_sql_fn_parse_info(func_tuple,
    5012             :                                       (Node *) fexpr,
    5013             :                                       fexpr->inputcollid);
    5014             : 
    5015             :     /*
    5016             :      * Parse, analyze, and rewrite (unlike inline_function(), we can't skip
    5017             :      * rewriting here).  We can fail as soon as we find more than one query,
    5018             :      * though.
    5019             :      */
    5020          36 :     raw_parsetree_list = pg_parse_query(src);
    5021          36 :     if (list_length(raw_parsetree_list) != 1)
    5022           0 :         goto fail;
    5023             : 
    5024          36 :     querytree_list = pg_analyze_and_rewrite_params(linitial(raw_parsetree_list),
    5025             :                                                    src,
    5026             :                                                    (ParserSetupHook) sql_fn_parser_setup,
    5027             :                                                    pinfo, NULL);
    5028          36 :     if (list_length(querytree_list) != 1)
    5029           0 :         goto fail;
    5030          36 :     querytree = linitial(querytree_list);
    5031             : 
    5032             :     /*
    5033             :      * The single command must be a plain SELECT.
    5034             :      */
    5035          72 :     if (!IsA(querytree, Query) ||
    5036          36 :         querytree->commandType != CMD_SELECT)
    5037             :         goto fail;
    5038             : 
    5039             :     /*
    5040             :      * Make sure the function (still) returns what it's declared to.  This
    5041             :      * will raise an error if wrong, but that's okay since the function would
    5042             :      * fail at runtime anyway.  Note that check_sql_fn_retval will also insert
    5043             :      * RelabelType(s) and/or NULL columns if needed to make the tlist
    5044             :      * expression(s) match the declared type of the function.
    5045             :      *
    5046             :      * If the function returns a composite type, don't inline unless the check
    5047             :      * shows it's returning a whole tuple result; otherwise what it's
    5048             :      * returning is a single composite column which is not what we need. (Like
    5049             :      * check_sql_fn_retval, we deliberately exclude domains over composite
    5050             :      * here.)
    5051             :      */
    5052          36 :     if (!check_sql_fn_retval(func_oid, fexpr->funcresulttype,
    5053             :                              querytree_list,
    5054          32 :                              &modifyTargetList, NULL) &&
    5055          64 :         (get_typtype(fexpr->funcresulttype) == TYPTYPE_COMPOSITE ||
    5056          32 :          fexpr->funcresulttype == RECORDOID))
    5057             :         goto fail;              /* reject not-whole-tuple-result cases */
    5058             : 
    5059             :     /*
    5060             :      * If we had to modify the tlist to make it match, and the statement is
    5061             :      * one in which changing the tlist contents could change semantics, we
    5062             :      * have to punt and not inline.
    5063             :      */
    5064          36 :     if (modifyTargetList)
    5065           4 :         goto fail;
    5066             : 
    5067             :     /*
    5068             :      * If it returns RECORD, we have to check against the column type list
    5069             :      * provided in the RTE; check_sql_fn_retval can't do that.  (If no match,
    5070             :      * we just fail to inline, rather than complaining; see notes for
    5071             :      * tlist_matches_coltypelist.)  We don't have to do this for functions
    5072             :      * with declared OUT parameters, even though their funcresulttype is
    5073             :      * RECORDOID, so check get_func_result_type too.
    5074             :      */
    5075          32 :     if (fexpr->funcresulttype == RECORDOID &&
    5076           0 :         get_func_result_type(func_oid, NULL, NULL) == TYPEFUNC_RECORD &&
    5077           0 :         !tlist_matches_coltypelist(querytree->targetList,
    5078             :                                    rtfunc->funccoltypes))
    5079           0 :         goto fail;
    5080             : 
    5081             :     /*
    5082             :      * Looks good --- substitute parameters into the query.
    5083             :      */
    5084          64 :     querytree = substitute_actual_srf_parameters(querytree,
    5085          32 :                                                  funcform->pronargs,
    5086             :                                                  fexpr->args);
    5087             : 
    5088             :     /*
    5089             :      * Copy the modified query out of the temporary memory context, and clean
    5090             :      * up.
    5091             :      */
    5092          32 :     MemoryContextSwitchTo(oldcxt);
    5093             : 
    5094          32 :     querytree = copyObject(querytree);
    5095             : 
    5096          32 :     MemoryContextDelete(mycxt);
    5097          32 :     error_context_stack = sqlerrcontext.previous;
    5098          32 :     ReleaseSysCache(func_tuple);
    5099             : 
    5100             :     /*
    5101             :      * We don't have to fix collations here because the upper query is already
    5102             :      * parsed, ie, the collations in the RTE are what count.
    5103             :      */
    5104             : 
    5105             :     /*
    5106             :      * Since there is now no trace of the function in the plan tree, we must
    5107             :      * explicitly record the plan's dependency on the function.
    5108             :      */
    5109          32 :     record_plan_function_dependency(root, func_oid);
    5110             : 
    5111          32 :     return querytree;
    5112             : 
    5113             :     /* Here if func is not inlinable: release temp memory and return NULL */
    5114             : fail:
    5115           4 :     MemoryContextSwitchTo(oldcxt);
    5116           4 :     MemoryContextDelete(mycxt);
    5117           4 :     error_context_stack = sqlerrcontext.previous;
    5118           4 :     ReleaseSysCache(func_tuple);
    5119             : 
    5120           4 :     return NULL;
    5121             : }
    5122             : 
    5123             : /*
    5124             :  * Replace Param nodes by appropriate actual parameters
    5125             :  *
    5126             :  * This is just enough different from substitute_actual_parameters()
    5127             :  * that it needs its own code.
    5128             :  */
    5129             : static Query *
    5130          32 : substitute_actual_srf_parameters(Query *expr, int nargs, List *args)
    5131             : {
    5132             :     substitute_actual_srf_parameters_context context;
    5133             : 
    5134          32 :     context.nargs = nargs;
    5135          32 :     context.args = args;
    5136          32 :     context.sublevels_up = 1;
    5137             : 
    5138          32 :     return query_tree_mutator(expr,
    5139             :                               substitute_actual_srf_parameters_mutator,
    5140             :                               &context,
    5141             :                               0);
    5142             : }
    5143             : 
    5144             : static Node *
    5145         704 : substitute_actual_srf_parameters_mutator(Node *node,
    5146             :                                          substitute_actual_srf_parameters_context *context)
    5147             : {
    5148             :     Node       *result;
    5149             : 
    5150         704 :     if (node == NULL)
    5151         336 :         return NULL;
    5152         368 :     if (IsA(node, Query))
    5153             :     {
    5154           0 :         context->sublevels_up++;
    5155           0 :         result = (Node *) query_tree_mutator((Query *) node,
    5156             :                                              substitute_actual_srf_parameters_mutator,
    5157             :                                              (void *) context,
    5158             :                                              0);
    5159           0 :         context->sublevels_up--;
    5160           0 :         return result;
    5161             :     }
    5162         368 :     if (IsA(node, Param))
    5163             :     {
    5164          36 :         Param      *param = (Param *) node;
    5165             : 
    5166          36 :         if (param->paramkind == PARAM_EXTERN)
    5167             :         {
    5168          36 :             if (param->paramid <= 0 || param->paramid > context->nargs)
    5169           0 :                 elog(ERROR, "invalid paramid: %d", param->paramid);
    5170             : 
    5171             :             /*
    5172             :              * Since the parameter is being inserted into a subquery, we must
    5173             :              * adjust levels.
    5174             :              */
    5175          36 :             result = copyObject(list_nth(context->args, param->paramid - 1));
    5176          36 :             IncrementVarSublevelsUp(result, context->sublevels_up, 0);
    5177          36 :             return result;
    5178             :         }
    5179             :     }
    5180         332 :     return expression_tree_mutator(node,
    5181             :                                    substitute_actual_srf_parameters_mutator,
    5182             :                                    (void *) context);
    5183             : }
    5184             : 
    5185             : /*
    5186             :  * Check whether a SELECT targetlist emits the specified column types,
    5187             :  * to see if it's safe to inline a function returning record.
    5188             :  *
    5189             :  * We insist on exact match here.  The executor allows binary-coercible
    5190             :  * cases too, but we don't have a way to preserve the correct column types
    5191             :  * in the correct places if we inline the function in such a case.
    5192             :  *
    5193             :  * Note that we only check type OIDs not typmods; this agrees with what the
    5194             :  * executor would do at runtime, and attributing a specific typmod to a
    5195             :  * function result is largely wishful thinking anyway.
    5196             :  */
    5197             : static bool
    5198           0 : tlist_matches_coltypelist(List *tlist, List *coltypelist)
    5199             : {
    5200             :     ListCell   *tlistitem;
    5201             :     ListCell   *clistitem;
    5202             : 
    5203           0 :     clistitem = list_head(coltypelist);
    5204           0 :     foreach(tlistitem, tlist)
    5205             :     {
    5206           0 :         TargetEntry *tle = (TargetEntry *) lfirst(tlistitem);
    5207             :         Oid         coltype;
    5208             : 
    5209           0 :         if (tle->resjunk)
    5210           0 :             continue;           /* ignore junk columns */
    5211             : 
    5212           0 :         if (clistitem == NULL)
    5213           0 :             return false;       /* too many tlist items */
    5214             : 
    5215           0 :         coltype = lfirst_oid(clistitem);
    5216           0 :         clistitem = lnext(coltypelist, clistitem);
    5217             : 
    5218           0 :         if (exprType((Node *) tle->expr) != coltype)
    5219           0 :             return false;       /* column type mismatch */
    5220             :     }
    5221             : 
    5222           0 :     if (clistitem != NULL)
    5223           0 :         return false;           /* too few tlist items */
    5224             : 
    5225           0 :     return true;
    5226             : }

Generated by: LCOV version 1.13