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

Generated by: LCOV version 1.13