LCOV - code coverage report
Current view: top level - src/backend/optimizer/util - clauses.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 88.2 % 2006 1770
Test Date: 2026-05-02 16:16:32 Functions: 100.0 % 80 80
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-2026, 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 "access/table.h"
      24              : #include "catalog/pg_class.h"
      25              : #include "catalog/pg_inherits.h"
      26              : #include "catalog/pg_language.h"
      27              : #include "catalog/pg_operator.h"
      28              : #include "catalog/pg_proc.h"
      29              : #include "catalog/pg_type.h"
      30              : #include "executor/executor.h"
      31              : #include "executor/functions.h"
      32              : #include "funcapi.h"
      33              : #include "miscadmin.h"
      34              : #include "nodes/makefuncs.h"
      35              : #include "nodes/multibitmapset.h"
      36              : #include "nodes/nodeFuncs.h"
      37              : #include "nodes/subscripting.h"
      38              : #include "nodes/supportnodes.h"
      39              : #include "optimizer/clauses.h"
      40              : #include "optimizer/cost.h"
      41              : #include "optimizer/optimizer.h"
      42              : #include "optimizer/pathnode.h"
      43              : #include "optimizer/plancat.h"
      44              : #include "optimizer/planmain.h"
      45              : #include "parser/analyze.h"
      46              : #include "parser/parse_coerce.h"
      47              : #include "parser/parse_collate.h"
      48              : #include "parser/parse_func.h"
      49              : #include "parser/parse_oper.h"
      50              : #include "parser/parsetree.h"
      51              : #include "rewrite/rewriteHandler.h"
      52              : #include "rewrite/rewriteManip.h"
      53              : #include "tcop/tcopprot.h"
      54              : #include "utils/acl.h"
      55              : #include "utils/builtins.h"
      56              : #include "utils/datum.h"
      57              : #include "utils/fmgroids.h"
      58              : #include "utils/json.h"
      59              : #include "utils/jsonb.h"
      60              : #include "utils/jsonpath.h"
      61              : #include "utils/lsyscache.h"
      62              : #include "utils/memutils.h"
      63              : #include "utils/rel.h"
      64              : #include "utils/syscache.h"
      65              : #include "utils/typcache.h"
      66              : 
      67              : typedef struct
      68              : {
      69              :     ParamListInfo boundParams;
      70              :     PlannerInfo *root;
      71              :     List       *active_fns;
      72              :     Node       *case_val;
      73              :     bool        estimate;
      74              : } eval_const_expressions_context;
      75              : 
      76              : typedef struct
      77              : {
      78              :     int         nargs;
      79              :     List       *args;
      80              :     int        *usecounts;
      81              : } substitute_actual_parameters_context;
      82              : 
      83              : typedef struct
      84              : {
      85              :     int         nargs;
      86              :     List       *args;
      87              :     int         sublevels_up;
      88              : } substitute_actual_parameters_in_from_context;
      89              : 
      90              : typedef struct
      91              : {
      92              :     char       *proname;
      93              :     char       *prosrc;
      94              : } inline_error_callback_arg;
      95              : 
      96              : typedef struct
      97              : {
      98              :     char        max_hazard;     /* worst proparallel hazard found so far */
      99              :     char        max_interesting;    /* worst proparallel hazard of interest */
     100              :     List       *safe_param_ids; /* PARAM_EXEC Param IDs to treat as safe */
     101              : } max_parallel_hazard_context;
     102              : 
     103              : static bool contain_agg_clause_walker(Node *node, void *context);
     104              : static bool find_window_functions_walker(Node *node, WindowFuncLists *lists);
     105              : static bool contain_subplans_walker(Node *node, void *context);
     106              : static bool contain_mutable_functions_walker(Node *node, void *context);
     107              : static bool contain_volatile_functions_walker(Node *node, void *context);
     108              : static bool contain_volatile_functions_not_nextval_walker(Node *node, void *context);
     109              : static bool max_parallel_hazard_walker(Node *node,
     110              :                                        max_parallel_hazard_context *context);
     111              : static bool contain_nonstrict_functions_walker(Node *node, void *context);
     112              : static bool contain_exec_param_walker(Node *node, List *param_ids);
     113              : static bool contain_context_dependent_node(Node *clause);
     114              : static bool contain_context_dependent_node_walker(Node *node, int *flags);
     115              : static bool contain_leaked_vars_walker(Node *node, void *context);
     116              : static Relids find_nonnullable_rels_walker(Node *node, bool top_level);
     117              : static List *find_nonnullable_vars_walker(Node *node, bool top_level);
     118              : static void find_subquery_safe_quals(Node *jtnode, List **safe_quals);
     119              : static bool is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK);
     120              : static bool convert_saop_to_hashed_saop_walker(Node *node, void *context);
     121              : static Node *eval_const_expressions_mutator(Node *node,
     122              :                                             eval_const_expressions_context *context);
     123              : static bool contain_non_const_walker(Node *node, void *context);
     124              : static bool ece_function_is_safe(Oid funcid,
     125              :                                  eval_const_expressions_context *context);
     126              : static List *simplify_or_arguments(List *args,
     127              :                                    eval_const_expressions_context *context,
     128              :                                    bool *haveNull, bool *forceTrue);
     129              : static List *simplify_and_arguments(List *args,
     130              :                                     eval_const_expressions_context *context,
     131              :                                     bool *haveNull, bool *forceFalse);
     132              : static Node *simplify_boolean_equality(Oid opno, List *args);
     133              : static Expr *simplify_function(Oid funcid,
     134              :                                Oid result_type, int32 result_typmod,
     135              :                                Oid result_collid, Oid input_collid, List **args_p,
     136              :                                bool funcvariadic, bool process_args, bool allow_non_const,
     137              :                                eval_const_expressions_context *context);
     138              : static Node *simplify_aggref(Aggref *aggref,
     139              :                              eval_const_expressions_context *context);
     140              : static List *reorder_function_arguments(List *args, int pronargs,
     141              :                                         HeapTuple func_tuple);
     142              : static List *add_function_defaults(List *args, int pronargs,
     143              :                                    HeapTuple func_tuple);
     144              : static List *fetch_function_defaults(HeapTuple func_tuple);
     145              : static void recheck_cast_function_args(List *args, Oid result_type,
     146              :                                        Oid *proargtypes, int pronargs,
     147              :                                        HeapTuple func_tuple);
     148              : static Expr *evaluate_function(Oid funcid, Oid result_type, int32 result_typmod,
     149              :                                Oid result_collid, Oid input_collid, List *args,
     150              :                                bool funcvariadic,
     151              :                                HeapTuple func_tuple,
     152              :                                eval_const_expressions_context *context);
     153              : static Expr *inline_function(Oid funcid, Oid result_type, Oid result_collid,
     154              :                              Oid input_collid, List *args,
     155              :                              bool funcvariadic,
     156              :                              HeapTuple func_tuple,
     157              :                              eval_const_expressions_context *context);
     158              : static Node *substitute_actual_parameters(Node *expr, int nargs, List *args,
     159              :                                           int *usecounts);
     160              : static Node *substitute_actual_parameters_mutator(Node *node,
     161              :                                                   substitute_actual_parameters_context *context);
     162              : static void sql_inline_error_callback(void *arg);
     163              : static Query *inline_sql_function_in_from(PlannerInfo *root,
     164              :                                           RangeTblFunction *rtfunc,
     165              :                                           FuncExpr *fexpr,
     166              :                                           HeapTuple func_tuple,
     167              :                                           Form_pg_proc funcform,
     168              :                                           const char *src);
     169              : static Query *substitute_actual_parameters_in_from(Query *expr,
     170              :                                                    int nargs, List *args);
     171              : static Node *substitute_actual_parameters_in_from_mutator(Node *node,
     172              :                                                           substitute_actual_parameters_in_from_context *context);
     173              : static bool pull_paramids_walker(Node *node, Bitmapset **context);
     174              : 
     175              : 
     176              : /*****************************************************************************
     177              :  *      Aggregate-function clause manipulation
     178              :  *****************************************************************************/
     179              : 
     180              : /*
     181              :  * contain_agg_clause
     182              :  *    Recursively search for Aggref/GroupingFunc nodes within a clause.
     183              :  *
     184              :  *    Returns true if any aggregate found.
     185              :  *
     186              :  * This does not descend into subqueries, and so should be used only after
     187              :  * reduction of sublinks to subplans, or in contexts where it's known there
     188              :  * are no subqueries.  There mustn't be outer-aggregate references either.
     189              :  *
     190              :  * (If you want something like this but able to deal with subqueries,
     191              :  * see rewriteManip.c's contain_aggs_of_level().)
     192              :  */
     193              : bool
     194         8746 : contain_agg_clause(Node *clause)
     195              : {
     196         8746 :     return contain_agg_clause_walker(clause, NULL);
     197              : }
     198              : 
     199              : static bool
     200        11022 : contain_agg_clause_walker(Node *node, void *context)
     201              : {
     202        11022 :     if (node == NULL)
     203           30 :         return false;
     204        10992 :     if (IsA(node, Aggref))
     205              :     {
     206              :         Assert(((Aggref *) node)->agglevelsup == 0);
     207          793 :         return true;            /* abort the tree traversal and return true */
     208              :     }
     209        10199 :     if (IsA(node, GroupingFunc))
     210              :     {
     211              :         Assert(((GroupingFunc *) node)->agglevelsup == 0);
     212           25 :         return true;            /* abort the tree traversal and return true */
     213              :     }
     214              :     Assert(!IsA(node, SubLink));
     215        10174 :     return expression_tree_walker(node, contain_agg_clause_walker, context);
     216              : }
     217              : 
     218              : /*****************************************************************************
     219              :  *      Window-function clause manipulation
     220              :  *****************************************************************************/
     221              : 
     222              : /*
     223              :  * contain_window_function
     224              :  *    Recursively search for WindowFunc nodes within a clause.
     225              :  *
     226              :  * Since window functions don't have level fields, but are hard-wired to
     227              :  * be associated with the current query level, this is just the same as
     228              :  * rewriteManip.c's function.
     229              :  */
     230              : bool
     231         7367 : contain_window_function(Node *clause)
     232              : {
     233         7367 :     return contain_windowfuncs(clause);
     234              : }
     235              : 
     236              : /*
     237              :  * find_window_functions
     238              :  *    Locate all the WindowFunc nodes in an expression tree, and organize
     239              :  *    them by winref ID number.
     240              :  *
     241              :  * Caller must provide an upper bound on the winref IDs expected in the tree.
     242              :  */
     243              : WindowFuncLists *
     244         2222 : find_window_functions(Node *clause, Index maxWinRef)
     245              : {
     246         2222 :     WindowFuncLists *lists = palloc_object(WindowFuncLists);
     247              : 
     248         2222 :     lists->numWindowFuncs = 0;
     249         2222 :     lists->maxWinRef = maxWinRef;
     250         2222 :     lists->windowFuncs = (List **) palloc0((maxWinRef + 1) * sizeof(List *));
     251         2222 :     (void) find_window_functions_walker(clause, lists);
     252         2222 :     return lists;
     253              : }
     254              : 
     255              : static bool
     256        18720 : find_window_functions_walker(Node *node, WindowFuncLists *lists)
     257              : {
     258        18720 :     if (node == NULL)
     259          179 :         return false;
     260        18541 :     if (IsA(node, WindowFunc))
     261              :     {
     262         3077 :         WindowFunc *wfunc = (WindowFunc *) node;
     263              : 
     264              :         /* winref is unsigned, so one-sided test is OK */
     265         3077 :         if (wfunc->winref > lists->maxWinRef)
     266            0 :             elog(ERROR, "WindowFunc contains out-of-range winref %u",
     267              :                  wfunc->winref);
     268              : 
     269         6154 :         lists->windowFuncs[wfunc->winref] =
     270         3077 :             lappend(lists->windowFuncs[wfunc->winref], wfunc);
     271         3077 :         lists->numWindowFuncs++;
     272              : 
     273              :         /*
     274              :          * We assume that the parser checked that there are no window
     275              :          * functions in the arguments or filter clause.  Hence, we need not
     276              :          * recurse into them.  (If either the parser or the planner screws up
     277              :          * on this point, the executor will still catch it; see ExecInitExpr.)
     278              :          */
     279         3077 :         return false;
     280              :     }
     281              :     Assert(!IsA(node, SubLink));
     282        15464 :     return expression_tree_walker(node, find_window_functions_walker, lists);
     283              : }
     284              : 
     285              : 
     286              : /*****************************************************************************
     287              :  *      Support for expressions returning sets
     288              :  *****************************************************************************/
     289              : 
     290              : /*
     291              :  * expression_returns_set_rows
     292              :  *    Estimate the number of rows returned by a set-returning expression.
     293              :  *    The result is 1 if it's not a set-returning expression.
     294              :  *
     295              :  * We should only examine the top-level function or operator; it used to be
     296              :  * appropriate to recurse, but not anymore.  (Even if there are more SRFs in
     297              :  * the function's inputs, their multipliers are accounted for separately.)
     298              :  *
     299              :  * Note: keep this in sync with expression_returns_set() in nodes/nodeFuncs.c.
     300              :  */
     301              : double
     302       328255 : expression_returns_set_rows(PlannerInfo *root, Node *clause)
     303              : {
     304       328255 :     if (clause == NULL)
     305            0 :         return 1.0;
     306       328255 :     if (IsA(clause, FuncExpr))
     307              :     {
     308        47300 :         FuncExpr   *expr = (FuncExpr *) clause;
     309              : 
     310        47300 :         if (expr->funcretset)
     311        40776 :             return clamp_row_est(get_function_rows(root, expr->funcid, clause));
     312              :     }
     313       287479 :     if (IsA(clause, OpExpr))
     314              :     {
     315         2626 :         OpExpr     *expr = (OpExpr *) clause;
     316              : 
     317         2626 :         if (expr->opretset)
     318              :         {
     319            5 :             set_opfuncid(expr);
     320            5 :             return clamp_row_est(get_function_rows(root, expr->opfuncid, clause));
     321              :         }
     322              :     }
     323       287474 :     return 1.0;
     324              : }
     325              : 
     326              : 
     327              : /*****************************************************************************
     328              :  *      Subplan clause manipulation
     329              :  *****************************************************************************/
     330              : 
     331              : /*
     332              :  * contain_subplans
     333              :  *    Recursively search for subplan nodes within a clause.
     334              :  *
     335              :  * If we see a SubLink node, we will return true.  This is only possible if
     336              :  * the expression tree hasn't yet been transformed by subselect.c.  We do not
     337              :  * know whether the node will produce a true subplan or just an initplan,
     338              :  * but we make the conservative assumption that it will be a subplan.
     339              :  *
     340              :  * Returns true if any subplan found.
     341              :  */
     342              : bool
     343        41566 : contain_subplans(Node *clause)
     344              : {
     345        41566 :     return contain_subplans_walker(clause, NULL);
     346              : }
     347              : 
     348              : static bool
     349       173676 : contain_subplans_walker(Node *node, void *context)
     350              : {
     351       173676 :     if (node == NULL)
     352         5016 :         return false;
     353       168660 :     if (IsA(node, SubPlan) ||
     354       168581 :         IsA(node, AlternativeSubPlan) ||
     355       168581 :         IsA(node, SubLink))
     356          249 :         return true;            /* abort the tree traversal and return true */
     357       168411 :     return expression_tree_walker(node, contain_subplans_walker, context);
     358              : }
     359              : 
     360              : 
     361              : /*****************************************************************************
     362              :  *      Check clauses for mutable functions
     363              :  *****************************************************************************/
     364              : 
     365              : /*
     366              :  * contain_mutable_functions
     367              :  *    Recursively search for mutable functions within a clause.
     368              :  *
     369              :  * Returns true if any mutable function (or operator implemented by a
     370              :  * mutable function) is found.  This test is needed so that we don't
     371              :  * mistakenly think that something like "WHERE random() < 0.5" can be treated
     372              :  * as a constant qualification.
     373              :  *
     374              :  * This will give the right answer only for clauses that have been put
     375              :  * through expression preprocessing.  Callers outside the planner typically
     376              :  * should use contain_mutable_functions_after_planning() instead, for the
     377              :  * reasons given there.
     378              :  *
     379              :  * We will recursively look into Query nodes (i.e., SubLink sub-selects)
     380              :  * but not into SubPlans.  See comments for contain_volatile_functions().
     381              :  */
     382              : bool
     383       119385 : contain_mutable_functions(Node *clause)
     384              : {
     385       119385 :     return contain_mutable_functions_walker(clause, NULL);
     386              : }
     387              : 
     388              : static bool
     389        90190 : contain_mutable_functions_checker(Oid func_id, void *context)
     390              : {
     391        90190 :     return (func_volatile(func_id) != PROVOLATILE_IMMUTABLE);
     392              : }
     393              : 
     394              : static bool
     395       323924 : contain_mutable_functions_walker(Node *node, void *context)
     396              : {
     397       323924 :     if (node == NULL)
     398         1871 :         return false;
     399              :     /* Check for mutable functions in node itself */
     400       322053 :     if (check_functions_in_node(node, contain_mutable_functions_checker,
     401              :                                 context))
     402         6006 :         return true;
     403              : 
     404       316047 :     if (IsA(node, JsonConstructorExpr))
     405              :     {
     406          176 :         const JsonConstructorExpr *ctor = (JsonConstructorExpr *) node;
     407              :         ListCell   *lc;
     408              :         bool        is_jsonb;
     409              : 
     410          176 :         is_jsonb = ctor->returning->format->format_type == JS_FORMAT_JSONB;
     411              : 
     412              :         /*
     413              :          * Check argument_type => json[b] conversions specifically.  We still
     414              :          * recurse to check 'args' below, but here we want to specifically
     415              :          * check whether or not the emitted clause would fail to be immutable
     416              :          * because of TimeZone, for example.
     417              :          */
     418          296 :         foreach(lc, ctor->args)
     419              :         {
     420          264 :             Oid         typid = exprType(lfirst(lc));
     421              : 
     422          528 :             if (is_jsonb ?
     423          144 :                 !to_jsonb_is_immutable(typid) :
     424          120 :                 !to_json_is_immutable(typid))
     425          144 :                 return true;
     426              :         }
     427              : 
     428              :         /* Check all subnodes */
     429              :     }
     430              : 
     431       315903 :     if (IsA(node, JsonExpr))
     432              :     {
     433          188 :         JsonExpr   *jexpr = castNode(JsonExpr, node);
     434              :         Const      *cnst;
     435              : 
     436          188 :         if (!IsA(jexpr->path_spec, Const))
     437            0 :             return true;
     438              : 
     439          188 :         cnst = castNode(Const, jexpr->path_spec);
     440              : 
     441              :         Assert(cnst->consttype == JSONPATHOID);
     442          188 :         if (cnst->constisnull)
     443            0 :             return false;
     444              : 
     445          188 :         if (jspIsMutable(DatumGetJsonPathP(cnst->constvalue),
     446              :                          jexpr->passing_names, jexpr->passing_values))
     447          108 :             return true;
     448              :     }
     449              : 
     450       315795 :     if (IsA(node, SQLValueFunction))
     451              :     {
     452              :         /* all variants of SQLValueFunction are stable */
     453          259 :         return true;
     454              :     }
     455              : 
     456       315536 :     if (IsA(node, NextValueExpr))
     457              :     {
     458              :         /* NextValueExpr is volatile */
     459            0 :         return true;
     460              :     }
     461              : 
     462              :     /*
     463              :      * It should be safe to treat MinMaxExpr as immutable, because it will
     464              :      * depend on a non-cross-type btree comparison function, and those should
     465              :      * always be immutable.  Treating XmlExpr as immutable is more dubious,
     466              :      * and treating CoerceToDomain as immutable is outright dangerous.  But we
     467              :      * have done so historically, and changing this would probably cause more
     468              :      * problems than it would fix.  In practice, if you have a non-immutable
     469              :      * domain constraint you are in for pain anyhow.
     470              :      */
     471              : 
     472              :     /* Recurse to check arguments */
     473       315536 :     if (IsA(node, Query))
     474              :     {
     475              :         /* Recurse into subselects */
     476            0 :         return query_tree_walker((Query *) node,
     477              :                                  contain_mutable_functions_walker,
     478              :                                  context, 0);
     479              :     }
     480       315536 :     return expression_tree_walker(node, contain_mutable_functions_walker,
     481              :                                   context);
     482              : }
     483              : 
     484              : /*
     485              :  * contain_mutable_functions_after_planning
     486              :  *    Test whether given expression contains mutable functions.
     487              :  *
     488              :  * This is a wrapper for contain_mutable_functions() that is safe to use from
     489              :  * outside the planner.  The difference is that it first runs the expression
     490              :  * through expression_planner().  There are two key reasons why we need that:
     491              :  *
     492              :  * First, function default arguments will get inserted, which may affect
     493              :  * volatility (consider "default now()").
     494              :  *
     495              :  * Second, inline-able functions will get inlined, which may allow us to
     496              :  * conclude that the function is really less volatile than it's marked.
     497              :  * As an example, polymorphic functions must be marked with the most volatile
     498              :  * behavior that they have for any input type, but once we inline the
     499              :  * function we may be able to conclude that it's not so volatile for the
     500              :  * particular input type we're dealing with.
     501              :  */
     502              : bool
     503         2517 : contain_mutable_functions_after_planning(Expr *expr)
     504              : {
     505              :     /* We assume here that expression_planner() won't scribble on its input */
     506         2517 :     expr = expression_planner(expr);
     507              : 
     508              :     /* Now we can search for non-immutable functions */
     509         2517 :     return contain_mutable_functions((Node *) expr);
     510              : }
     511              : 
     512              : 
     513              : /*****************************************************************************
     514              :  *      Check clauses for volatile functions
     515              :  *****************************************************************************/
     516              : 
     517              : /*
     518              :  * contain_volatile_functions
     519              :  *    Recursively search for volatile functions within a clause.
     520              :  *
     521              :  * Returns true if any volatile function (or operator implemented by a
     522              :  * volatile function) is found. This test prevents, for example,
     523              :  * invalid conversions of volatile expressions into indexscan quals.
     524              :  *
     525              :  * This will give the right answer only for clauses that have been put
     526              :  * through expression preprocessing.  Callers outside the planner typically
     527              :  * should use contain_volatile_functions_after_planning() instead, for the
     528              :  * reasons given there.
     529              :  *
     530              :  * We will recursively look into Query nodes (i.e., SubLink sub-selects)
     531              :  * but not into SubPlans.  This is a bit odd, but intentional.  If we are
     532              :  * looking at a SubLink, we are probably deciding whether a query tree
     533              :  * transformation is safe, and a contained sub-select should affect that;
     534              :  * for example, duplicating a sub-select containing a volatile function
     535              :  * would be bad.  However, once we've got to the stage of having SubPlans,
     536              :  * subsequent planning need not consider volatility within those, since
     537              :  * the executor won't change its evaluation rules for a SubPlan based on
     538              :  * volatility.
     539              :  *
     540              :  * For some node types, for example, RestrictInfo and PathTarget, we cache
     541              :  * whether we found any volatile functions or not and reuse that value in any
     542              :  * future checks for that node.  All of the logic for determining if the
     543              :  * cached value should be set to VOLATILITY_NOVOLATILE or VOLATILITY_VOLATILE
     544              :  * belongs in this function.  Any code which makes changes to these nodes
     545              :  * which could change the outcome this function must set the cached value back
     546              :  * to VOLATILITY_UNKNOWN.  That allows this function to redetermine the
     547              :  * correct value during the next call, should we need to redetermine if the
     548              :  * node contains any volatile functions again in the future.
     549              :  */
     550              : bool
     551      2648712 : contain_volatile_functions(Node *clause)
     552              : {
     553      2648712 :     return contain_volatile_functions_walker(clause, NULL);
     554              : }
     555              : 
     556              : static bool
     557       752978 : contain_volatile_functions_checker(Oid func_id, void *context)
     558              : {
     559       752978 :     return (func_volatile(func_id) == PROVOLATILE_VOLATILE);
     560              : }
     561              : 
     562              : static bool
     563      6104707 : contain_volatile_functions_walker(Node *node, void *context)
     564              : {
     565      6104707 :     if (node == NULL)
     566       185451 :         return false;
     567              :     /* Check for volatile functions in node itself */
     568      5919256 :     if (check_functions_in_node(node, contain_volatile_functions_checker,
     569              :                                 context))
     570         1603 :         return true;
     571              : 
     572      5917653 :     if (IsA(node, NextValueExpr))
     573              :     {
     574              :         /* NextValueExpr is volatile */
     575           28 :         return true;
     576              :     }
     577              : 
     578      5917625 :     if (IsA(node, RestrictInfo))
     579              :     {
     580      1011321 :         RestrictInfo *rinfo = (RestrictInfo *) node;
     581              : 
     582              :         /*
     583              :          * For RestrictInfo, check if we've checked the volatility of it
     584              :          * before.  If so, we can just use the cached value and not bother
     585              :          * checking it again.  Otherwise, check it and cache if whether we
     586              :          * found any volatile functions.
     587              :          */
     588      1011321 :         if (rinfo->has_volatile == VOLATILITY_NOVOLATILE)
     589       608991 :             return false;
     590       402330 :         else if (rinfo->has_volatile == VOLATILITY_VOLATILE)
     591           54 :             return true;
     592              :         else
     593              :         {
     594              :             bool        hasvolatile;
     595              : 
     596       402276 :             hasvolatile = contain_volatile_functions_walker((Node *) rinfo->clause,
     597              :                                                             context);
     598       402276 :             if (hasvolatile)
     599           98 :                 rinfo->has_volatile = VOLATILITY_VOLATILE;
     600              :             else
     601       402178 :                 rinfo->has_volatile = VOLATILITY_NOVOLATILE;
     602              : 
     603       402276 :             return hasvolatile;
     604              :         }
     605              :     }
     606              : 
     607      4906304 :     if (IsA(node, PathTarget))
     608              :     {
     609       278714 :         PathTarget *target = (PathTarget *) node;
     610              : 
     611              :         /*
     612              :          * We also do caching for PathTarget the same as we do above for
     613              :          * RestrictInfos.
     614              :          */
     615       278714 :         if (target->has_volatile_expr == VOLATILITY_NOVOLATILE)
     616       231208 :             return false;
     617        47506 :         else if (target->has_volatile_expr == VOLATILITY_VOLATILE)
     618            0 :             return true;
     619              :         else
     620              :         {
     621              :             bool        hasvolatile;
     622              : 
     623        47506 :             hasvolatile = contain_volatile_functions_walker((Node *) target->exprs,
     624              :                                                             context);
     625              : 
     626        47506 :             if (hasvolatile)
     627            0 :                 target->has_volatile_expr = VOLATILITY_VOLATILE;
     628              :             else
     629        47506 :                 target->has_volatile_expr = VOLATILITY_NOVOLATILE;
     630              : 
     631        47506 :             return hasvolatile;
     632              :         }
     633              :     }
     634              : 
     635              :     /*
     636              :      * See notes in contain_mutable_functions_walker about why we treat
     637              :      * MinMaxExpr, XmlExpr, and CoerceToDomain as immutable, while
     638              :      * SQLValueFunction is stable.  Hence, none of them are of interest here.
     639              :      */
     640              : 
     641              :     /* Recurse to check arguments */
     642      4627590 :     if (IsA(node, Query))
     643              :     {
     644              :         /* Recurse into subselects */
     645         5419 :         return query_tree_walker((Query *) node,
     646              :                                  contain_volatile_functions_walker,
     647              :                                  context, 0);
     648              :     }
     649      4622171 :     return expression_tree_walker(node, contain_volatile_functions_walker,
     650              :                                   context);
     651              : }
     652              : 
     653              : /*
     654              :  * contain_volatile_functions_after_planning
     655              :  *    Test whether given expression contains volatile functions.
     656              :  *
     657              :  * This is a wrapper for contain_volatile_functions() that is safe to use from
     658              :  * outside the planner.  The difference is that it first runs the expression
     659              :  * through expression_planner().  There are two key reasons why we need that:
     660              :  *
     661              :  * First, function default arguments will get inserted, which may affect
     662              :  * volatility (consider "default random()").
     663              :  *
     664              :  * Second, inline-able functions will get inlined, which may allow us to
     665              :  * conclude that the function is really less volatile than it's marked.
     666              :  * As an example, polymorphic functions must be marked with the most volatile
     667              :  * behavior that they have for any input type, but once we inline the
     668              :  * function we may be able to conclude that it's not so volatile for the
     669              :  * particular input type we're dealing with.
     670              :  */
     671              : bool
     672          875 : contain_volatile_functions_after_planning(Expr *expr)
     673              : {
     674              :     /* We assume here that expression_planner() won't scribble on its input */
     675          875 :     expr = expression_planner(expr);
     676              : 
     677              :     /* Now we can search for volatile functions */
     678          867 :     return contain_volatile_functions((Node *) expr);
     679              : }
     680              : 
     681              : /*
     682              :  * Special purpose version of contain_volatile_functions() for use in COPY:
     683              :  * ignore nextval(), but treat all other functions normally.
     684              :  */
     685              : bool
     686          159 : contain_volatile_functions_not_nextval(Node *clause)
     687              : {
     688          159 :     return contain_volatile_functions_not_nextval_walker(clause, NULL);
     689              : }
     690              : 
     691              : static bool
     692           41 : contain_volatile_functions_not_nextval_checker(Oid func_id, void *context)
     693              : {
     694           66 :     return (func_id != F_NEXTVAL &&
     695           25 :             func_volatile(func_id) == PROVOLATILE_VOLATILE);
     696              : }
     697              : 
     698              : static bool
     699          199 : contain_volatile_functions_not_nextval_walker(Node *node, void *context)
     700              : {
     701          199 :     if (node == NULL)
     702            0 :         return false;
     703              :     /* Check for volatile functions in node itself */
     704          199 :     if (check_functions_in_node(node,
     705              :                                 contain_volatile_functions_not_nextval_checker,
     706              :                                 context))
     707            4 :         return true;
     708              : 
     709              :     /*
     710              :      * See notes in contain_mutable_functions_walker about why we treat
     711              :      * MinMaxExpr, XmlExpr, and CoerceToDomain as immutable, while
     712              :      * SQLValueFunction is stable.  Hence, none of them are of interest here.
     713              :      * Also, since we're intentionally ignoring nextval(), presumably we
     714              :      * should ignore NextValueExpr.
     715              :      */
     716              : 
     717              :     /* Recurse to check arguments */
     718          195 :     if (IsA(node, Query))
     719              :     {
     720              :         /* Recurse into subselects */
     721            0 :         return query_tree_walker((Query *) node,
     722              :                                  contain_volatile_functions_not_nextval_walker,
     723              :                                  context, 0);
     724              :     }
     725          195 :     return expression_tree_walker(node,
     726              :                                   contain_volatile_functions_not_nextval_walker,
     727              :                                   context);
     728              : }
     729              : 
     730              : 
     731              : /*****************************************************************************
     732              :  *      Check queries for parallel unsafe and/or restricted constructs
     733              :  *****************************************************************************/
     734              : 
     735              : /*
     736              :  * max_parallel_hazard
     737              :  *      Find the worst parallel-hazard level in the given query
     738              :  *
     739              :  * Returns the worst function hazard property (the earliest in this list:
     740              :  * PROPARALLEL_UNSAFE, PROPARALLEL_RESTRICTED, PROPARALLEL_SAFE) that can
     741              :  * be found in the given parsetree.  We use this to find out whether the query
     742              :  * can be parallelized at all.  The caller will also save the result in
     743              :  * PlannerGlobal so as to short-circuit checks of portions of the querytree
     744              :  * later, in the common case where everything is SAFE.
     745              :  */
     746              : char
     747       257702 : max_parallel_hazard(Query *parse)
     748              : {
     749              :     max_parallel_hazard_context context;
     750              : 
     751       257702 :     context.max_hazard = PROPARALLEL_SAFE;
     752       257702 :     context.max_interesting = PROPARALLEL_UNSAFE;
     753       257702 :     context.safe_param_ids = NIL;
     754       257702 :     (void) max_parallel_hazard_walker((Node *) parse, &context);
     755       257702 :     return context.max_hazard;
     756              : }
     757              : 
     758              : /*
     759              :  * is_parallel_safe
     760              :  *      Detect whether the given expr contains only parallel-safe functions
     761              :  *
     762              :  * root->glob->maxParallelHazard must previously have been set to the
     763              :  * result of max_parallel_hazard() on the whole query.
     764              :  */
     765              : bool
     766      1892880 : is_parallel_safe(PlannerInfo *root, Node *node)
     767              : {
     768              :     max_parallel_hazard_context context;
     769              :     PlannerInfo *proot;
     770              :     ListCell   *l;
     771              : 
     772              :     /*
     773              :      * Even if the original querytree contained nothing unsafe, we need to
     774              :      * search the expression if we have generated any PARAM_EXEC Params while
     775              :      * planning, because those are parallel-restricted and there might be one
     776              :      * in this expression.  But otherwise we don't need to look.
     777              :      */
     778      1892880 :     if (root->glob->maxParallelHazard == PROPARALLEL_SAFE &&
     779      1146835 :         root->glob->paramExecTypes == NIL)
     780      1119901 :         return true;
     781              :     /* Else use max_parallel_hazard's search logic, but stop on RESTRICTED */
     782       772979 :     context.max_hazard = PROPARALLEL_SAFE;
     783       772979 :     context.max_interesting = PROPARALLEL_RESTRICTED;
     784       772979 :     context.safe_param_ids = NIL;
     785              : 
     786              :     /*
     787              :      * The params that refer to the same or parent query level are considered
     788              :      * parallel-safe.  The idea is that we compute such params at Gather or
     789              :      * Gather Merge node and pass their value to workers.
     790              :      */
     791      1887604 :     for (proot = root; proot != NULL; proot = proot->parent_root)
     792              :     {
     793      1168377 :         foreach(l, proot->init_plans)
     794              :         {
     795        53752 :             SubPlan    *initsubplan = (SubPlan *) lfirst(l);
     796              : 
     797        53752 :             context.safe_param_ids = list_concat(context.safe_param_ids,
     798        53752 :                                                  initsubplan->setParam);
     799              :         }
     800              :     }
     801              : 
     802       772979 :     return !max_parallel_hazard_walker(node, &context);
     803              : }
     804              : 
     805              : /* core logic for all parallel-hazard checks */
     806              : static bool
     807      1293142 : max_parallel_hazard_test(char proparallel, max_parallel_hazard_context *context)
     808              : {
     809      1293142 :     switch (proparallel)
     810              :     {
     811      1085568 :         case PROPARALLEL_SAFE:
     812              :             /* nothing to see here, move along */
     813      1085568 :             break;
     814       156456 :         case PROPARALLEL_RESTRICTED:
     815              :             /* increase max_hazard to RESTRICTED */
     816              :             Assert(context->max_hazard != PROPARALLEL_UNSAFE);
     817       156456 :             context->max_hazard = proparallel;
     818              :             /* done if we are not expecting any unsafe functions */
     819       156456 :             if (context->max_interesting == proparallel)
     820        77471 :                 return true;
     821        78985 :             break;
     822        51118 :         case PROPARALLEL_UNSAFE:
     823        51118 :             context->max_hazard = proparallel;
     824              :             /* we're always done at the first unsafe construct */
     825        51118 :             return true;
     826            0 :         default:
     827            0 :             elog(ERROR, "unrecognized proparallel value \"%c\"", proparallel);
     828              :             break;
     829              :     }
     830      1164553 :     return false;
     831              : }
     832              : 
     833              : /* check_functions_in_node callback */
     834              : static bool
     835      1178621 : max_parallel_hazard_checker(Oid func_id, void *context)
     836              : {
     837      1178621 :     return max_parallel_hazard_test(func_parallel(func_id),
     838              :                                     (max_parallel_hazard_context *) context);
     839              : }
     840              : 
     841              : static bool
     842     16890784 : max_parallel_hazard_walker(Node *node, max_parallel_hazard_context *context)
     843              : {
     844     16890784 :     if (node == NULL)
     845      4704969 :         return false;
     846              : 
     847              :     /* Check for hazardous functions in node itself */
     848     12185815 :     if (check_functions_in_node(node, max_parallel_hazard_checker,
     849              :                                 context))
     850        67253 :         return true;
     851              : 
     852              :     /*
     853              :      * It should be OK to treat MinMaxExpr as parallel-safe, since btree
     854              :      * opclass support functions are generally parallel-safe.  XmlExpr is a
     855              :      * bit more dubious but we can probably get away with it.  We err on the
     856              :      * side of caution by treating CoerceToDomain as parallel-restricted.
     857              :      * (Note: in principle that's wrong because a domain constraint could
     858              :      * contain a parallel-unsafe function; but useful constraints probably
     859              :      * never would have such, and assuming they do would cripple use of
     860              :      * parallel query in the presence of domain types.)  SQLValueFunction
     861              :      * should be safe in all cases.  NextValueExpr is parallel-unsafe.
     862              :      */
     863     12118562 :     if (IsA(node, CoerceToDomain))
     864              :     {
     865        15796 :         if (max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
     866         4336 :             return true;
     867              :     }
     868              : 
     869     12102766 :     else if (IsA(node, NextValueExpr))
     870              :     {
     871          320 :         if (max_parallel_hazard_test(PROPARALLEL_UNSAFE, context))
     872          320 :             return true;
     873              :     }
     874              : 
     875              :     /*
     876              :      * Treat window functions as parallel-restricted because we aren't sure
     877              :      * whether the input row ordering is fully deterministic, and the output
     878              :      * of window functions might vary across workers if not.  (In some cases,
     879              :      * like where the window frame orders by a primary key, we could relax
     880              :      * this restriction.  But it doesn't currently seem worth expending extra
     881              :      * effort to do so.)
     882              :      */
     883     12102446 :     else if (IsA(node, WindowFunc))
     884              :     {
     885         5156 :         if (max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
     886         2283 :             return true;
     887              :     }
     888              : 
     889              :     /*
     890              :      * As a notational convenience for callers, look through RestrictInfo.
     891              :      */
     892     12097290 :     else if (IsA(node, RestrictInfo))
     893              :     {
     894       199273 :         RestrictInfo *rinfo = (RestrictInfo *) node;
     895              : 
     896       199273 :         return max_parallel_hazard_walker((Node *) rinfo->clause, context);
     897              :     }
     898              : 
     899              :     /*
     900              :      * Really we should not see SubLink during a max_interesting == restricted
     901              :      * scan, but if we do, return true.
     902              :      */
     903     11898017 :     else if (IsA(node, SubLink))
     904              :     {
     905        34741 :         if (max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
     906            0 :             return true;
     907              :     }
     908              : 
     909              :     /*
     910              :      * Only parallel-safe SubPlans can be sent to workers.  Within the
     911              :      * testexpr of the SubPlan, Params representing the output columns of the
     912              :      * subplan can be treated as parallel-safe, so temporarily add their IDs
     913              :      * to the safe_param_ids list while examining the testexpr.
     914              :      */
     915     11863276 :     else if (IsA(node, SubPlan))
     916              :     {
     917        24816 :         SubPlan    *subplan = (SubPlan *) node;
     918              :         List       *save_safe_param_ids;
     919              : 
     920        49367 :         if (!subplan->parallel_safe &&
     921        24551 :             max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
     922        24551 :             return true;
     923          265 :         save_safe_param_ids = context->safe_param_ids;
     924          530 :         context->safe_param_ids = list_concat_copy(context->safe_param_ids,
     925          265 :                                                    subplan->paramIds);
     926          265 :         if (max_parallel_hazard_walker(subplan->testexpr, context))
     927            5 :             return true;        /* no need to restore safe_param_ids */
     928          260 :         list_free(context->safe_param_ids);
     929          260 :         context->safe_param_ids = save_safe_param_ids;
     930              :         /* we must also check args, but no special Param treatment there */
     931          260 :         if (max_parallel_hazard_walker((Node *) subplan->args, context))
     932            0 :             return true;
     933              :         /* don't want to recurse normally, so we're done */
     934          260 :         return false;
     935              :     }
     936              : 
     937              :     /*
     938              :      * We can't pass Params to workers at the moment either, so they are also
     939              :      * parallel-restricted, unless they are PARAM_EXTERN Params or are
     940              :      * PARAM_EXEC Params listed in safe_param_ids, meaning they could be
     941              :      * either generated within workers or can be computed by the leader and
     942              :      * then their value can be passed to workers.
     943              :      */
     944     11838460 :     else if (IsA(node, Param))
     945              :     {
     946        84226 :         Param      *param = (Param *) node;
     947              : 
     948        84226 :         if (param->paramkind == PARAM_EXTERN)
     949        42268 :             return false;
     950              : 
     951        41958 :         if (param->paramkind != PARAM_EXEC ||
     952        37794 :             !list_member_int(context->safe_param_ids, param->paramid))
     953              :         {
     954        33957 :             if (max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
     955        29846 :                 return true;
     956              :         }
     957        12112 :         return false;           /* nothing to recurse to */
     958              :     }
     959              : 
     960              :     /*
     961              :      * When we're first invoked on a completely unplanned tree, we must
     962              :      * recurse into subqueries so to as to locate parallel-unsafe constructs
     963              :      * anywhere in the tree.
     964              :      */
     965     11754234 :     else if (IsA(node, Query))
     966              :     {
     967       343584 :         Query      *query = (Query *) node;
     968              : 
     969              :         /* SELECT FOR UPDATE/SHARE must be treated as unsafe */
     970       343584 :         if (query->rowMarks != NULL)
     971              :         {
     972         3849 :             context->max_hazard = PROPARALLEL_UNSAFE;
     973         3849 :             return true;
     974              :         }
     975              : 
     976              :         /* Recurse into subselects */
     977       339735 :         return query_tree_walker(query,
     978              :                                  max_parallel_hazard_walker,
     979              :                                  context, 0);
     980              :     }
     981              : 
     982              :     /* Recurse to check arguments */
     983     11459724 :     return expression_tree_walker(node,
     984              :                                   max_parallel_hazard_walker,
     985              :                                   context);
     986              : }
     987              : 
     988              : 
     989              : /*****************************************************************************
     990              :  *      Check clauses for nonstrict functions
     991              :  *****************************************************************************/
     992              : 
     993              : /*
     994              :  * contain_nonstrict_functions
     995              :  *    Recursively search for nonstrict functions within a clause.
     996              :  *
     997              :  * Returns true if any nonstrict construct is found --- ie, anything that
     998              :  * could produce non-NULL output with a NULL input.
     999              :  *
    1000              :  * The idea here is that the caller has verified that the expression contains
    1001              :  * one or more Var or Param nodes (as appropriate for the caller's need), and
    1002              :  * now wishes to prove that the expression result will be NULL if any of these
    1003              :  * inputs is NULL.  If we return false, then the proof succeeded.
    1004              :  */
    1005              : bool
    1006         1901 : contain_nonstrict_functions(Node *clause)
    1007              : {
    1008         1901 :     return contain_nonstrict_functions_walker(clause, NULL);
    1009              : }
    1010              : 
    1011              : static bool
    1012         1962 : contain_nonstrict_functions_checker(Oid func_id, void *context)
    1013              : {
    1014         1962 :     return !func_strict(func_id);
    1015              : }
    1016              : 
    1017              : static bool
    1018         6657 : contain_nonstrict_functions_walker(Node *node, void *context)
    1019              : {
    1020         6657 :     if (node == NULL)
    1021            0 :         return false;
    1022         6657 :     if (IsA(node, Aggref))
    1023              :     {
    1024              :         /* an aggregate could return non-null with null input */
    1025            0 :         return true;
    1026              :     }
    1027         6657 :     if (IsA(node, GroupingFunc))
    1028              :     {
    1029              :         /*
    1030              :          * A GroupingFunc doesn't evaluate its arguments, and therefore must
    1031              :          * be treated as nonstrict.
    1032              :          */
    1033            0 :         return true;
    1034              :     }
    1035         6657 :     if (IsA(node, WindowFunc))
    1036              :     {
    1037              :         /* a window function could return non-null with null input */
    1038            0 :         return true;
    1039              :     }
    1040         6657 :     if (IsA(node, SubscriptingRef))
    1041              :     {
    1042            0 :         SubscriptingRef *sbsref = (SubscriptingRef *) node;
    1043              :         const SubscriptRoutines *sbsroutines;
    1044              : 
    1045              :         /* Subscripting assignment is always presumed nonstrict */
    1046            0 :         if (sbsref->refassgnexpr != NULL)
    1047            0 :             return true;
    1048              :         /* Otherwise we must look up the subscripting support methods */
    1049            0 :         sbsroutines = getSubscriptingRoutines(sbsref->refcontainertype, NULL);
    1050            0 :         if (!(sbsroutines && sbsroutines->fetch_strict))
    1051            0 :             return true;
    1052              :         /* else fall through to check args */
    1053              :     }
    1054         6657 :     if (IsA(node, DistinctExpr))
    1055              :     {
    1056              :         /* IS DISTINCT FROM is inherently non-strict */
    1057            0 :         return true;
    1058              :     }
    1059         6657 :     if (IsA(node, NullIfExpr))
    1060              :     {
    1061              :         /* NULLIF is inherently non-strict */
    1062            0 :         return true;
    1063              :     }
    1064         6657 :     if (IsA(node, BoolExpr))
    1065              :     {
    1066           15 :         BoolExpr   *expr = (BoolExpr *) node;
    1067              : 
    1068           15 :         switch (expr->boolop)
    1069              :         {
    1070           15 :             case AND_EXPR:
    1071              :             case OR_EXPR:
    1072              :                 /* AND, OR are inherently non-strict */
    1073           15 :                 return true;
    1074            0 :             default:
    1075            0 :                 break;
    1076              :         }
    1077              :     }
    1078         6642 :     if (IsA(node, SubLink))
    1079              :     {
    1080              :         /* In some cases a sublink might be strict, but in general not */
    1081           10 :         return true;
    1082              :     }
    1083         6632 :     if (IsA(node, SubPlan))
    1084            0 :         return true;
    1085         6632 :     if (IsA(node, AlternativeSubPlan))
    1086            0 :         return true;
    1087         6632 :     if (IsA(node, FieldStore))
    1088            0 :         return true;
    1089         6632 :     if (IsA(node, CoerceViaIO))
    1090              :     {
    1091              :         /*
    1092              :          * CoerceViaIO is strict regardless of whether the I/O functions are,
    1093              :          * so just go look at its argument; asking check_functions_in_node is
    1094              :          * useless expense and could deliver the wrong answer.
    1095              :          */
    1096          875 :         return contain_nonstrict_functions_walker((Node *) ((CoerceViaIO *) node)->arg,
    1097              :                                                   context);
    1098              :     }
    1099         5757 :     if (IsA(node, ArrayCoerceExpr))
    1100              :     {
    1101              :         /*
    1102              :          * ArrayCoerceExpr is strict at the array level, regardless of what
    1103              :          * the per-element expression is; so we should ignore elemexpr and
    1104              :          * recurse only into the arg.
    1105              :          */
    1106            0 :         return contain_nonstrict_functions_walker((Node *) ((ArrayCoerceExpr *) node)->arg,
    1107              :                                                   context);
    1108              :     }
    1109         5757 :     if (IsA(node, CaseExpr))
    1110           52 :         return true;
    1111         5705 :     if (IsA(node, ArrayExpr))
    1112            0 :         return true;
    1113         5705 :     if (IsA(node, RowExpr))
    1114            2 :         return true;
    1115         5703 :     if (IsA(node, RowCompareExpr))
    1116            0 :         return true;
    1117         5703 :     if (IsA(node, CoalesceExpr))
    1118          211 :         return true;
    1119         5492 :     if (IsA(node, MinMaxExpr))
    1120           50 :         return true;
    1121         5442 :     if (IsA(node, XmlExpr))
    1122            0 :         return true;
    1123         5442 :     if (IsA(node, NullTest))
    1124           20 :         return true;
    1125         5422 :     if (IsA(node, BooleanTest))
    1126            0 :         return true;
    1127         5422 :     if (IsA(node, JsonConstructorExpr))
    1128           10 :         return true;
    1129              : 
    1130              :     /* Check other function-containing nodes */
    1131         5412 :     if (check_functions_in_node(node, contain_nonstrict_functions_checker,
    1132              :                                 context))
    1133            0 :         return true;
    1134              : 
    1135         5412 :     return expression_tree_walker(node, contain_nonstrict_functions_walker,
    1136              :                                   context);
    1137              : }
    1138              : 
    1139              : /*****************************************************************************
    1140              :  *      Check clauses for Params
    1141              :  *****************************************************************************/
    1142              : 
    1143              : /*
    1144              :  * contain_exec_param
    1145              :  *    Recursively search for PARAM_EXEC Params within a clause.
    1146              :  *
    1147              :  * Returns true if the clause contains any PARAM_EXEC Param with a paramid
    1148              :  * appearing in the given list of Param IDs.  Does not descend into
    1149              :  * subqueries!
    1150              :  */
    1151              : bool
    1152         2452 : contain_exec_param(Node *clause, List *param_ids)
    1153              : {
    1154         2452 :     return contain_exec_param_walker(clause, param_ids);
    1155              : }
    1156              : 
    1157              : static bool
    1158         2692 : contain_exec_param_walker(Node *node, List *param_ids)
    1159              : {
    1160         2692 :     if (node == NULL)
    1161           30 :         return false;
    1162         2662 :     if (IsA(node, Param))
    1163              :     {
    1164           10 :         Param      *p = (Param *) node;
    1165              : 
    1166           20 :         if (p->paramkind == PARAM_EXEC &&
    1167           10 :             list_member_int(param_ids, p->paramid))
    1168           10 :             return true;
    1169              :     }
    1170         2652 :     return expression_tree_walker(node, contain_exec_param_walker, param_ids);
    1171              : }
    1172              : 
    1173              : /*****************************************************************************
    1174              :  *      Check clauses for context-dependent nodes
    1175              :  *****************************************************************************/
    1176              : 
    1177              : /*
    1178              :  * contain_context_dependent_node
    1179              :  *    Recursively search for context-dependent nodes within a clause.
    1180              :  *
    1181              :  * CaseTestExpr nodes must appear directly within the corresponding CaseExpr,
    1182              :  * not nested within another one, or they'll see the wrong test value.  If one
    1183              :  * appears "bare" in the arguments of a SQL function, then we can't inline the
    1184              :  * SQL function for fear of creating such a situation.  The same applies for
    1185              :  * CaseTestExpr used within the elemexpr of an ArrayCoerceExpr.
    1186              :  *
    1187              :  * CoerceToDomainValue would have the same issue if domain CHECK expressions
    1188              :  * could get inlined into larger expressions, but presently that's impossible.
    1189              :  * Still, it might be allowed in future, or other node types with similar
    1190              :  * issues might get invented.  So give this function a generic name, and set
    1191              :  * up the recursion state to allow multiple flag bits.
    1192              :  */
    1193              : static bool
    1194         2535 : contain_context_dependent_node(Node *clause)
    1195              : {
    1196         2535 :     int         flags = 0;
    1197              : 
    1198         2535 :     return contain_context_dependent_node_walker(clause, &flags);
    1199              : }
    1200              : 
    1201              : #define CCDN_CASETESTEXPR_OK    0x0001  /* CaseTestExpr okay here? */
    1202              : 
    1203              : static bool
    1204         7799 : contain_context_dependent_node_walker(Node *node, int *flags)
    1205              : {
    1206         7799 :     if (node == NULL)
    1207          136 :         return false;
    1208         7663 :     if (IsA(node, CaseTestExpr))
    1209            5 :         return !(*flags & CCDN_CASETESTEXPR_OK);
    1210         7658 :     else if (IsA(node, CaseExpr))
    1211              :     {
    1212            0 :         CaseExpr   *caseexpr = (CaseExpr *) node;
    1213              : 
    1214              :         /*
    1215              :          * If this CASE doesn't have a test expression, then it doesn't create
    1216              :          * a context in which CaseTestExprs should appear, so just fall
    1217              :          * through and treat it as a generic expression node.
    1218              :          */
    1219            0 :         if (caseexpr->arg)
    1220              :         {
    1221            0 :             int         save_flags = *flags;
    1222              :             bool        res;
    1223              : 
    1224              :             /*
    1225              :              * Note: in principle, we could distinguish the various sub-parts
    1226              :              * of a CASE construct and set the flag bit only for some of them,
    1227              :              * since we are only expecting CaseTestExprs to appear in the
    1228              :              * "expr" subtree of the CaseWhen nodes.  But it doesn't really
    1229              :              * seem worth any extra code.  If there are any bare CaseTestExprs
    1230              :              * elsewhere in the CASE, something's wrong already.
    1231              :              */
    1232            0 :             *flags |= CCDN_CASETESTEXPR_OK;
    1233            0 :             res = expression_tree_walker(node,
    1234              :                                          contain_context_dependent_node_walker,
    1235              :                                          flags);
    1236            0 :             *flags = save_flags;
    1237            0 :             return res;
    1238              :         }
    1239              :     }
    1240         7658 :     else if (IsA(node, ArrayCoerceExpr))
    1241              :     {
    1242            0 :         ArrayCoerceExpr *ac = (ArrayCoerceExpr *) node;
    1243              :         int         save_flags;
    1244              :         bool        res;
    1245              : 
    1246              :         /* Check the array expression */
    1247            0 :         if (contain_context_dependent_node_walker((Node *) ac->arg, flags))
    1248            0 :             return true;
    1249              : 
    1250              :         /* Check the elemexpr, which is allowed to contain CaseTestExpr */
    1251            0 :         save_flags = *flags;
    1252            0 :         *flags |= CCDN_CASETESTEXPR_OK;
    1253            0 :         res = contain_context_dependent_node_walker((Node *) ac->elemexpr,
    1254              :                                                     flags);
    1255            0 :         *flags = save_flags;
    1256            0 :         return res;
    1257              :     }
    1258         7658 :     return expression_tree_walker(node, contain_context_dependent_node_walker,
    1259              :                                   flags);
    1260              : }
    1261              : 
    1262              : /*****************************************************************************
    1263              :  *        Check clauses for Vars passed to non-leakproof functions
    1264              :  *****************************************************************************/
    1265              : 
    1266              : /*
    1267              :  * contain_leaked_vars
    1268              :  *      Recursively scan a clause to discover whether it contains any Var
    1269              :  *      nodes (of the current query level) that are passed as arguments to
    1270              :  *      leaky functions.
    1271              :  *
    1272              :  * Returns true if the clause contains any non-leakproof functions that are
    1273              :  * passed Var nodes of the current query level, and which might therefore leak
    1274              :  * data.  Such clauses must be applied after any lower-level security barrier
    1275              :  * clauses.
    1276              :  */
    1277              : bool
    1278         6698 : contain_leaked_vars(Node *clause)
    1279              : {
    1280         6698 :     return contain_leaked_vars_walker(clause, NULL);
    1281              : }
    1282              : 
    1283              : static bool
    1284         6598 : contain_leaked_vars_checker(Oid func_id, void *context)
    1285              : {
    1286         6598 :     return !get_func_leakproof(func_id);
    1287              : }
    1288              : 
    1289              : static bool
    1290        15195 : contain_leaked_vars_walker(Node *node, void *context)
    1291              : {
    1292        15195 :     if (node == NULL)
    1293            0 :         return false;
    1294              : 
    1295        15195 :     switch (nodeTag(node))
    1296              :     {
    1297         8537 :         case T_Var:
    1298              :         case T_Const:
    1299              :         case T_Param:
    1300              :         case T_ArrayExpr:
    1301              :         case T_FieldSelect:
    1302              :         case T_FieldStore:
    1303              :         case T_NamedArgExpr:
    1304              :         case T_BoolExpr:
    1305              :         case T_RelabelType:
    1306              :         case T_CollateExpr:
    1307              :         case T_CaseExpr:
    1308              :         case T_CaseTestExpr:
    1309              :         case T_RowExpr:
    1310              :         case T_SQLValueFunction:
    1311              :         case T_NullTest:
    1312              :         case T_BooleanTest:
    1313              :         case T_NextValueExpr:
    1314              :         case T_ReturningExpr:
    1315              :         case T_List:
    1316              : 
    1317              :             /*
    1318              :              * We know these node types don't contain function calls; but
    1319              :              * something further down in the node tree might.
    1320              :              */
    1321         8537 :             break;
    1322              : 
    1323         6598 :         case T_FuncExpr:
    1324              :         case T_OpExpr:
    1325              :         case T_DistinctExpr:
    1326              :         case T_NullIfExpr:
    1327              :         case T_ScalarArrayOpExpr:
    1328              :         case T_CoerceViaIO:
    1329              :         case T_ArrayCoerceExpr:
    1330              : 
    1331              :             /*
    1332              :              * If node contains a leaky function call, and there's any Var
    1333              :              * underneath it, reject.
    1334              :              */
    1335         6598 :             if (check_functions_in_node(node, contain_leaked_vars_checker,
    1336         2390 :                                         context) &&
    1337         2390 :                 contain_var_clause(node))
    1338         2346 :                 return true;
    1339         4252 :             break;
    1340              : 
    1341            0 :         case T_SubscriptingRef:
    1342              :             {
    1343            0 :                 SubscriptingRef *sbsref = (SubscriptingRef *) node;
    1344              :                 const SubscriptRoutines *sbsroutines;
    1345              : 
    1346              :                 /* Consult the subscripting support method info */
    1347            0 :                 sbsroutines = getSubscriptingRoutines(sbsref->refcontainertype,
    1348              :                                                       NULL);
    1349            0 :                 if (!sbsroutines ||
    1350            0 :                     !(sbsref->refassgnexpr != NULL ?
    1351            0 :                       sbsroutines->store_leakproof :
    1352            0 :                       sbsroutines->fetch_leakproof))
    1353              :                 {
    1354              :                     /* Node is leaky, so reject if it contains Vars */
    1355            0 :                     if (contain_var_clause(node))
    1356            0 :                         return true;
    1357              :                 }
    1358              :             }
    1359            0 :             break;
    1360              : 
    1361            0 :         case T_RowCompareExpr:
    1362              :             {
    1363              :                 /*
    1364              :                  * It's worth special-casing this because a leaky comparison
    1365              :                  * function only compromises one pair of row elements, which
    1366              :                  * might not contain Vars while others do.
    1367              :                  */
    1368            0 :                 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
    1369              :                 ListCell   *opid;
    1370              :                 ListCell   *larg;
    1371              :                 ListCell   *rarg;
    1372              : 
    1373            0 :                 forthree(opid, rcexpr->opnos,
    1374              :                          larg, rcexpr->largs,
    1375              :                          rarg, rcexpr->rargs)
    1376              :                 {
    1377            0 :                     Oid         funcid = get_opcode(lfirst_oid(opid));
    1378              : 
    1379            0 :                     if (!get_func_leakproof(funcid) &&
    1380            0 :                         (contain_var_clause((Node *) lfirst(larg)) ||
    1381            0 :                          contain_var_clause((Node *) lfirst(rarg))))
    1382            0 :                         return true;
    1383              :                 }
    1384              :             }
    1385            0 :             break;
    1386              : 
    1387            0 :         case T_MinMaxExpr:
    1388              :             {
    1389              :                 /*
    1390              :                  * MinMaxExpr is leakproof if the comparison function it calls
    1391              :                  * is leakproof.
    1392              :                  */
    1393            0 :                 MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
    1394              :                 TypeCacheEntry *typentry;
    1395              :                 bool        leakproof;
    1396              : 
    1397              :                 /* Look up the btree comparison function for the datatype */
    1398            0 :                 typentry = lookup_type_cache(minmaxexpr->minmaxtype,
    1399              :                                              TYPECACHE_CMP_PROC);
    1400            0 :                 if (OidIsValid(typentry->cmp_proc))
    1401            0 :                     leakproof = get_func_leakproof(typentry->cmp_proc);
    1402              :                 else
    1403              :                 {
    1404              :                     /*
    1405              :                      * The executor will throw an error, but here we just
    1406              :                      * treat the missing function as leaky.
    1407              :                      */
    1408            0 :                     leakproof = false;
    1409              :                 }
    1410              : 
    1411            0 :                 if (!leakproof &&
    1412            0 :                     contain_var_clause((Node *) minmaxexpr->args))
    1413            0 :                     return true;
    1414              :             }
    1415            0 :             break;
    1416              : 
    1417           35 :         case T_CurrentOfExpr:
    1418              : 
    1419              :             /*
    1420              :              * WHERE CURRENT OF doesn't contain leaky function calls.
    1421              :              * Moreover, it is essential that this is considered non-leaky,
    1422              :              * since the planner must always generate a TID scan when CURRENT
    1423              :              * OF is present -- cf. cost_tidscan.
    1424              :              */
    1425           35 :             return false;
    1426              : 
    1427           25 :         default:
    1428              : 
    1429              :             /*
    1430              :              * If we don't recognize the node tag, assume it might be leaky.
    1431              :              * This prevents an unexpected security hole if someone adds a new
    1432              :              * node type that can call a function.
    1433              :              */
    1434           25 :             return true;
    1435              :     }
    1436        12789 :     return expression_tree_walker(node, contain_leaked_vars_walker,
    1437              :                                   context);
    1438              : }
    1439              : 
    1440              : /*****************************************************************************
    1441              :  *        Nullability analysis
    1442              :  *****************************************************************************/
    1443              : 
    1444              : /*
    1445              :  * find_nonnullable_rels
    1446              :  *      Determine which base rels are forced nonnullable by given clause.
    1447              :  *
    1448              :  * Returns the set of all Relids that are referenced in the clause in such
    1449              :  * a way that the clause cannot possibly return TRUE if any of these Relids
    1450              :  * is an all-NULL row.  (It is OK to err on the side of conservatism; hence
    1451              :  * the analysis here is simplistic.)
    1452              :  *
    1453              :  * The semantics here are subtly different from contain_nonstrict_functions:
    1454              :  * that function is concerned with NULL results from arbitrary expressions,
    1455              :  * but here we assume that the input is a Boolean expression, and wish to
    1456              :  * see if NULL inputs will provably cause a FALSE-or-NULL result.  We expect
    1457              :  * the expression to have been AND/OR flattened and converted to implicit-AND
    1458              :  * format.
    1459              :  *
    1460              :  * Note: this function is largely duplicative of find_nonnullable_vars().
    1461              :  * The reason not to simplify this function into a thin wrapper around
    1462              :  * find_nonnullable_vars() is that the tested conditions really are different:
    1463              :  * a clause like "t1.v1 IS NOT NULL OR t1.v2 IS NOT NULL" does not prove
    1464              :  * that either v1 or v2 can't be NULL, but it does prove that the t1 row
    1465              :  * as a whole can't be all-NULL.  Also, the behavior for PHVs is different.
    1466              :  *
    1467              :  * top_level is true while scanning top-level AND/OR structure; here, showing
    1468              :  * the result is either FALSE or NULL is good enough.  top_level is false when
    1469              :  * we have descended below a NOT or a strict function: now we must be able to
    1470              :  * prove that the subexpression goes to NULL.
    1471              :  *
    1472              :  * We don't use expression_tree_walker here because we don't want to descend
    1473              :  * through very many kinds of nodes; only the ones we can be sure are strict.
    1474              :  */
    1475              : Relids
    1476        82083 : find_nonnullable_rels(Node *clause)
    1477              : {
    1478        82083 :     return find_nonnullable_rels_walker(clause, true);
    1479              : }
    1480              : 
    1481              : static Relids
    1482       544127 : find_nonnullable_rels_walker(Node *node, bool top_level)
    1483              : {
    1484       544127 :     Relids      result = NULL;
    1485              :     ListCell   *l;
    1486              : 
    1487       544127 :     if (node == NULL)
    1488         5141 :         return NULL;
    1489       538986 :     if (IsA(node, Var))
    1490              :     {
    1491       173559 :         Var        *var = (Var *) node;
    1492              : 
    1493       173559 :         if (var->varlevelsup == 0)
    1494       173559 :             result = bms_make_singleton(var->varno);
    1495              :     }
    1496       365427 :     else if (IsA(node, List))
    1497              :     {
    1498              :         /*
    1499              :          * At top level, we are examining an implicit-AND list: if any of the
    1500              :          * arms produces FALSE-or-NULL then the result is FALSE-or-NULL. If
    1501              :          * not at top level, we are examining the arguments of a strict
    1502              :          * function: if any of them produce NULL then the result of the
    1503              :          * function must be NULL.  So in both cases, the set of nonnullable
    1504              :          * rels is the union of those found in the arms, and we pass down the
    1505              :          * top_level flag unmodified.
    1506              :          */
    1507       524548 :         foreach(l, (List *) node)
    1508              :         {
    1509       333462 :             result = bms_join(result,
    1510       333462 :                               find_nonnullable_rels_walker(lfirst(l),
    1511              :                                                            top_level));
    1512              :         }
    1513              :     }
    1514       174341 :     else if (IsA(node, FuncExpr))
    1515              :     {
    1516         6724 :         FuncExpr   *expr = (FuncExpr *) node;
    1517              : 
    1518         6724 :         if (func_strict(expr->funcid))
    1519         6570 :             result = find_nonnullable_rels_walker((Node *) expr->args, false);
    1520              :     }
    1521       167617 :     else if (IsA(node, OpExpr))
    1522              :     {
    1523        97662 :         OpExpr     *expr = (OpExpr *) node;
    1524              : 
    1525        97662 :         set_opfuncid(expr);
    1526        97662 :         if (func_strict(expr->opfuncid))
    1527        97662 :             result = find_nonnullable_rels_walker((Node *) expr->args, false);
    1528              :     }
    1529        69955 :     else if (IsA(node, ScalarArrayOpExpr))
    1530              :     {
    1531         6371 :         ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
    1532              : 
    1533         6371 :         if (is_strict_saop(expr, true))
    1534         6371 :             result = find_nonnullable_rels_walker((Node *) expr->args, false);
    1535              :     }
    1536        63584 :     else if (IsA(node, BoolExpr))
    1537              :     {
    1538         7270 :         BoolExpr   *expr = (BoolExpr *) node;
    1539              : 
    1540         7270 :         switch (expr->boolop)
    1541              :         {
    1542          329 :             case AND_EXPR:
    1543              :                 /* At top level we can just recurse (to the List case) */
    1544          329 :                 if (top_level)
    1545              :                 {
    1546          329 :                     result = find_nonnullable_rels_walker((Node *) expr->args,
    1547              :                                                           top_level);
    1548          329 :                     break;
    1549              :                 }
    1550              : 
    1551              :                 /*
    1552              :                  * Below top level, even if one arm produces NULL, the result
    1553              :                  * could be FALSE (hence not NULL).  However, if *all* the
    1554              :                  * arms produce NULL then the result is NULL, so we can take
    1555              :                  * the intersection of the sets of nonnullable rels, just as
    1556              :                  * for OR.  Fall through to share code.
    1557              :                  */
    1558              :                 pg_fallthrough;
    1559              :             case OR_EXPR:
    1560              : 
    1561              :                 /*
    1562              :                  * OR is strict if all of its arms are, so we can take the
    1563              :                  * intersection of the sets of nonnullable rels for each arm.
    1564              :                  * This works for both values of top_level.
    1565              :                  */
    1566         9202 :                 foreach(l, expr->args)
    1567              :                 {
    1568              :                     Relids      subresult;
    1569              : 
    1570         7419 :                     subresult = find_nonnullable_rels_walker(lfirst(l),
    1571              :                                                              top_level);
    1572         7419 :                     if (result == NULL) /* first subresult? */
    1573         3729 :                         result = subresult;
    1574              :                     else
    1575         3690 :                         result = bms_int_members(result, subresult);
    1576              : 
    1577              :                     /*
    1578              :                      * If the intersection is empty, we can stop looking. This
    1579              :                      * also justifies the test for first-subresult above.
    1580              :                      */
    1581         7419 :                     if (bms_is_empty(result))
    1582         1946 :                         break;
    1583              :                 }
    1584         3729 :                 break;
    1585         3212 :             case NOT_EXPR:
    1586              :                 /* NOT will return null if its arg is null */
    1587         3212 :                 result = find_nonnullable_rels_walker((Node *) expr->args,
    1588              :                                                       false);
    1589         3212 :                 break;
    1590            0 :             default:
    1591            0 :                 elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop);
    1592              :                 break;
    1593              :         }
    1594              :     }
    1595        56314 :     else if (IsA(node, RelabelType))
    1596              :     {
    1597         3442 :         RelabelType *expr = (RelabelType *) node;
    1598              : 
    1599         3442 :         result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
    1600              :     }
    1601        52872 :     else if (IsA(node, CoerceViaIO))
    1602              :     {
    1603              :         /* not clear this is useful, but it can't hurt */
    1604          177 :         CoerceViaIO *expr = (CoerceViaIO *) node;
    1605              : 
    1606          177 :         result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
    1607              :     }
    1608        52695 :     else if (IsA(node, ArrayCoerceExpr))
    1609              :     {
    1610              :         /* ArrayCoerceExpr is strict at the array level; ignore elemexpr */
    1611            0 :         ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
    1612              : 
    1613            0 :         result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
    1614              :     }
    1615        52695 :     else if (IsA(node, ConvertRowtypeExpr))
    1616              :     {
    1617              :         /* not clear this is useful, but it can't hurt */
    1618            0 :         ConvertRowtypeExpr *expr = (ConvertRowtypeExpr *) node;
    1619              : 
    1620            0 :         result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
    1621              :     }
    1622        52695 :     else if (IsA(node, CollateExpr))
    1623              :     {
    1624            0 :         CollateExpr *expr = (CollateExpr *) node;
    1625              : 
    1626            0 :         result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
    1627              :     }
    1628        52695 :     else if (IsA(node, NullTest))
    1629              :     {
    1630              :         /* IS NOT NULL can be considered strict, but only at top level */
    1631         4278 :         NullTest   *expr = (NullTest *) node;
    1632              : 
    1633         4278 :         if (top_level && expr->nulltesttype == IS_NOT_NULL && !expr->argisrow)
    1634         2821 :             result = find_nonnullable_rels_walker((Node *) expr->arg, false);
    1635              :     }
    1636        48417 :     else if (IsA(node, BooleanTest))
    1637              :     {
    1638              :         /* Boolean tests that reject NULL are strict at top level */
    1639          109 :         BooleanTest *expr = (BooleanTest *) node;
    1640              : 
    1641          109 :         if (top_level &&
    1642          109 :             (expr->booltesttype == IS_TRUE ||
    1643          109 :              expr->booltesttype == IS_FALSE ||
    1644            5 :              expr->booltesttype == IS_NOT_UNKNOWN))
    1645          104 :             result = find_nonnullable_rels_walker((Node *) expr->arg, false);
    1646              :     }
    1647        48308 :     else if (IsA(node, SubPlan))
    1648              :     {
    1649          108 :         SubPlan    *splan = (SubPlan *) node;
    1650              : 
    1651              :         /*
    1652              :          * For some types of SubPlan, we can infer strictness from Vars in the
    1653              :          * testexpr (the LHS of the original SubLink).
    1654              :          *
    1655              :          * For ANY_SUBLINK, if the subquery produces zero rows, the result is
    1656              :          * always FALSE.  If the subquery produces more than one row, the
    1657              :          * per-row results of the testexpr are combined using OR semantics.
    1658              :          * Hence ANY_SUBLINK can be strict only at top level, but there it's
    1659              :          * as strict as the testexpr is.
    1660              :          *
    1661              :          * For ROWCOMPARE_SUBLINK, if the subquery produces zero rows, the
    1662              :          * result is always NULL.  Otherwise, the result is as strict as the
    1663              :          * testexpr is.  So we can check regardless of top_level.
    1664              :          *
    1665              :          * We can't prove anything for other sublink types (in particular,
    1666              :          * note that ALL_SUBLINK will return TRUE if the subquery is empty).
    1667              :          */
    1668          108 :         if ((top_level && splan->subLinkType == ANY_SUBLINK) ||
    1669           73 :             splan->subLinkType == ROWCOMPARE_SUBLINK)
    1670           35 :             result = find_nonnullable_rels_walker(splan->testexpr, top_level);
    1671              :     }
    1672        48200 :     else if (IsA(node, PlaceHolderVar))
    1673              :     {
    1674          440 :         PlaceHolderVar *phv = (PlaceHolderVar *) node;
    1675              : 
    1676              :         /*
    1677              :          * If the contained expression forces any rels non-nullable, so does
    1678              :          * the PHV.
    1679              :          */
    1680          440 :         result = find_nonnullable_rels_walker((Node *) phv->phexpr, top_level);
    1681              : 
    1682              :         /*
    1683              :          * If the PHV's syntactic scope is exactly one rel, it will be forced
    1684              :          * to be evaluated at that rel, and so it will behave like a Var of
    1685              :          * that rel: if the rel's entire output goes to null, so will the PHV.
    1686              :          * (If the syntactic scope is a join, we know that the PHV will go to
    1687              :          * null if the whole join does; but that is AND semantics while we
    1688              :          * need OR semantics for find_nonnullable_rels' result, so we can't do
    1689              :          * anything with the knowledge.)
    1690              :          */
    1691          880 :         if (phv->phlevelsup == 0 &&
    1692          440 :             bms_membership(phv->phrels) == BMS_SINGLETON)
    1693          280 :             result = bms_add_members(result, phv->phrels);
    1694              :     }
    1695       538986 :     return result;
    1696              : }
    1697              : 
    1698              : /*
    1699              :  * find_nonnullable_vars
    1700              :  *      Determine which Vars are forced nonnullable by given clause.
    1701              :  *
    1702              :  * Returns the set of all level-zero Vars that are referenced in the clause in
    1703              :  * such a way that the clause cannot possibly return TRUE if any of these Vars
    1704              :  * is NULL.  (It is OK to err on the side of conservatism; hence the analysis
    1705              :  * here is simplistic.)
    1706              :  *
    1707              :  * The semantics here are subtly different from contain_nonstrict_functions:
    1708              :  * that function is concerned with NULL results from arbitrary expressions,
    1709              :  * but here we assume that the input is a Boolean expression, and wish to
    1710              :  * see if NULL inputs will provably cause a FALSE-or-NULL result.  We expect
    1711              :  * the expression to have been AND/OR flattened and converted to implicit-AND
    1712              :  * format (but the results are still good if it wasn't AND/OR flattened).
    1713              :  *
    1714              :  * Attnos of the identified Vars are returned in a multibitmapset (a List of
    1715              :  * Bitmapsets).  List indexes correspond to relids (varnos), while the per-rel
    1716              :  * Bitmapsets hold varattnos offset by FirstLowInvalidHeapAttributeNumber.
    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         1170 : find_nonnullable_vars(Node *clause)
    1728              : {
    1729         1170 :     return find_nonnullable_vars_walker(clause, true);
    1730              : }
    1731              : 
    1732              : static List *
    1733         7723 : find_nonnullable_vars_walker(Node *node, bool top_level)
    1734              : {
    1735         7723 :     List       *result = NIL;
    1736              :     ListCell   *l;
    1737              : 
    1738         7723 :     if (node == NULL)
    1739           35 :         return NIL;
    1740         7688 :     if (IsA(node, Var))
    1741              :     {
    1742         3038 :         Var        *var = (Var *) node;
    1743              : 
    1744         3038 :         if (var->varlevelsup == 0)
    1745         3038 :             result = mbms_add_member(result,
    1746              :                                      var->varno,
    1747         3038 :                                      var->varattno - FirstLowInvalidHeapAttributeNumber);
    1748              :     }
    1749         4650 :     else if (IsA(node, List))
    1750              :     {
    1751              :         /*
    1752              :          * At top level, we are examining an implicit-AND list: if any of the
    1753              :          * arms produces FALSE-or-NULL then the result is FALSE-or-NULL. If
    1754              :          * not at top level, we are examining the arguments of a strict
    1755              :          * function: if any of them produce NULL then the result of the
    1756              :          * function must be NULL.  So in both cases, the set of nonnullable
    1757              :          * vars is the union of those found in the arms, and we pass down the
    1758              :          * top_level flag unmodified.
    1759              :          */
    1760         7435 :         foreach(l, (List *) node)
    1761              :         {
    1762         4712 :             result = mbms_add_members(result,
    1763         4712 :                                       find_nonnullable_vars_walker(lfirst(l),
    1764              :                                                                    top_level));
    1765              :         }
    1766              :     }
    1767         1927 :     else if (IsA(node, FuncExpr))
    1768              :     {
    1769           10 :         FuncExpr   *expr = (FuncExpr *) node;
    1770              : 
    1771           10 :         if (func_strict(expr->funcid))
    1772           10 :             result = find_nonnullable_vars_walker((Node *) expr->args, false);
    1773              :     }
    1774         1917 :     else if (IsA(node, OpExpr))
    1775              :     {
    1776         1558 :         OpExpr     *expr = (OpExpr *) node;
    1777              : 
    1778         1558 :         set_opfuncid(expr);
    1779         1558 :         if (func_strict(expr->opfuncid))
    1780         1558 :             result = find_nonnullable_vars_walker((Node *) expr->args, false);
    1781              :     }
    1782          359 :     else if (IsA(node, ScalarArrayOpExpr))
    1783              :     {
    1784            0 :         ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
    1785              : 
    1786            0 :         if (is_strict_saop(expr, true))
    1787            0 :             result = find_nonnullable_vars_walker((Node *) expr->args, false);
    1788              :     }
    1789          359 :     else if (IsA(node, BoolExpr))
    1790              :     {
    1791           98 :         BoolExpr   *expr = (BoolExpr *) node;
    1792              : 
    1793           98 :         switch (expr->boolop)
    1794              :         {
    1795            0 :             case AND_EXPR:
    1796              : 
    1797              :                 /*
    1798              :                  * At top level we can just recurse (to the List case), since
    1799              :                  * the result should be the union of what we can prove in each
    1800              :                  * arm.
    1801              :                  */
    1802            0 :                 if (top_level)
    1803              :                 {
    1804            0 :                     result = find_nonnullable_vars_walker((Node *) expr->args,
    1805              :                                                           top_level);
    1806            0 :                     break;
    1807              :                 }
    1808              : 
    1809              :                 /*
    1810              :                  * Below top level, even if one arm produces NULL, the result
    1811              :                  * could be FALSE (hence not NULL).  However, if *all* the
    1812              :                  * arms produce NULL then the result is NULL, so we can take
    1813              :                  * the intersection of the sets of nonnullable vars, just as
    1814              :                  * for OR.  Fall through to share code.
    1815              :                  */
    1816              :                 pg_fallthrough;
    1817              :             case OR_EXPR:
    1818              : 
    1819              :                 /*
    1820              :                  * OR is strict if all of its arms are, so we can take the
    1821              :                  * intersection of the sets of nonnullable vars for each arm.
    1822              :                  * This works for both values of top_level.
    1823              :                  */
    1824          176 :                 foreach(l, expr->args)
    1825              :                 {
    1826              :                     List       *subresult;
    1827              : 
    1828          176 :                     subresult = find_nonnullable_vars_walker(lfirst(l),
    1829              :                                                              top_level);
    1830          176 :                     if (result == NIL)  /* first subresult? */
    1831           78 :                         result = subresult;
    1832              :                     else
    1833           98 :                         result = mbms_int_members(result, subresult);
    1834              : 
    1835              :                     /*
    1836              :                      * If the intersection is empty, we can stop looking. This
    1837              :                      * also justifies the test for first-subresult above.
    1838              :                      */
    1839          176 :                     if (result == NIL)
    1840           78 :                         break;
    1841              :                 }
    1842           78 :                 break;
    1843           20 :             case NOT_EXPR:
    1844              :                 /* NOT will return null if its arg is null */
    1845           20 :                 result = find_nonnullable_vars_walker((Node *) expr->args,
    1846              :                                                       false);
    1847           20 :                 break;
    1848            0 :             default:
    1849            0 :                 elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop);
    1850              :                 break;
    1851              :         }
    1852              :     }
    1853          261 :     else if (IsA(node, RelabelType))
    1854              :     {
    1855           46 :         RelabelType *expr = (RelabelType *) node;
    1856              : 
    1857           46 :         result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
    1858              :     }
    1859          215 :     else if (IsA(node, CoerceViaIO))
    1860              :     {
    1861              :         /* not clear this is useful, but it can't hurt */
    1862           16 :         CoerceViaIO *expr = (CoerceViaIO *) node;
    1863              : 
    1864           16 :         result = find_nonnullable_vars_walker((Node *) expr->arg, false);
    1865              :     }
    1866          199 :     else if (IsA(node, ArrayCoerceExpr))
    1867              :     {
    1868              :         /* ArrayCoerceExpr is strict at the array level; ignore elemexpr */
    1869            0 :         ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
    1870              : 
    1871            0 :         result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
    1872              :     }
    1873          199 :     else if (IsA(node, ConvertRowtypeExpr))
    1874              :     {
    1875              :         /* not clear this is useful, but it can't hurt */
    1876            0 :         ConvertRowtypeExpr *expr = (ConvertRowtypeExpr *) node;
    1877              : 
    1878            0 :         result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
    1879              :     }
    1880          199 :     else if (IsA(node, CollateExpr))
    1881              :     {
    1882            0 :         CollateExpr *expr = (CollateExpr *) node;
    1883              : 
    1884            0 :         result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
    1885              :     }
    1886          199 :     else if (IsA(node, NullTest))
    1887              :     {
    1888              :         /* IS NOT NULL can be considered strict, but only at top level */
    1889           93 :         NullTest   *expr = (NullTest *) node;
    1890              : 
    1891           93 :         if (top_level && expr->nulltesttype == IS_NOT_NULL && !expr->argisrow)
    1892           15 :             result = find_nonnullable_vars_walker((Node *) expr->arg, false);
    1893              :     }
    1894          106 :     else if (IsA(node, BooleanTest))
    1895              :     {
    1896              :         /* Boolean tests that reject NULL are strict at top level */
    1897            0 :         BooleanTest *expr = (BooleanTest *) node;
    1898              : 
    1899            0 :         if (top_level &&
    1900            0 :             (expr->booltesttype == IS_TRUE ||
    1901            0 :              expr->booltesttype == IS_FALSE ||
    1902            0 :              expr->booltesttype == IS_NOT_UNKNOWN))
    1903            0 :             result = find_nonnullable_vars_walker((Node *) expr->arg, false);
    1904              :     }
    1905          106 :     else if (IsA(node, SubPlan))
    1906              :     {
    1907            0 :         SubPlan    *splan = (SubPlan *) node;
    1908              : 
    1909              :         /* See analysis in find_nonnullable_rels_walker */
    1910            0 :         if ((top_level && splan->subLinkType == ANY_SUBLINK) ||
    1911            0 :             splan->subLinkType == ROWCOMPARE_SUBLINK)
    1912            0 :             result = find_nonnullable_vars_walker(splan->testexpr, top_level);
    1913              :     }
    1914          106 :     else if (IsA(node, PlaceHolderVar))
    1915              :     {
    1916            0 :         PlaceHolderVar *phv = (PlaceHolderVar *) node;
    1917              : 
    1918            0 :         result = find_nonnullable_vars_walker((Node *) phv->phexpr, top_level);
    1919              :     }
    1920         7688 :     return result;
    1921              : }
    1922              : 
    1923              : /*
    1924              :  * find_forced_null_vars
    1925              :  *      Determine which Vars must be NULL for the given clause to return TRUE.
    1926              :  *
    1927              :  * This is the complement of find_nonnullable_vars: find the level-zero Vars
    1928              :  * that must be NULL for the clause to return TRUE.  (It is OK to err on the
    1929              :  * side of conservatism; hence the analysis here is simplistic.  In fact,
    1930              :  * we only detect simple "var IS NULL" tests at the top level.)
    1931              :  *
    1932              :  * As with find_nonnullable_vars, we return the varattnos of the identified
    1933              :  * Vars in a multibitmapset.
    1934              :  */
    1935              : List *
    1936        94006 : find_forced_null_vars(Node *node)
    1937              : {
    1938        94006 :     List       *result = NIL;
    1939              :     Var        *var;
    1940              :     ListCell   *l;
    1941              : 
    1942        94006 :     if (node == NULL)
    1943         4431 :         return NIL;
    1944              :     /* Check single-clause cases using subroutine */
    1945        89575 :     var = find_forced_null_var(node);
    1946        89575 :     if (var)
    1947              :     {
    1948         1156 :         result = mbms_add_member(result,
    1949              :                                  var->varno,
    1950         1156 :                                  var->varattno - FirstLowInvalidHeapAttributeNumber);
    1951              :     }
    1952              :     /* Otherwise, handle AND-conditions */
    1953        88419 :     else if (IsA(node, List))
    1954              :     {
    1955              :         /*
    1956              :          * At top level, we are examining an implicit-AND list: if any of the
    1957              :          * arms produces FALSE-or-NULL then the result is FALSE-or-NULL.
    1958              :          */
    1959        89575 :         foreach(l, (List *) node)
    1960              :         {
    1961        54738 :             result = mbms_add_members(result,
    1962        54738 :                                       find_forced_null_vars((Node *) lfirst(l)));
    1963              :         }
    1964              :     }
    1965        53582 :     else if (IsA(node, BoolExpr))
    1966              :     {
    1967         4377 :         BoolExpr   *expr = (BoolExpr *) node;
    1968              : 
    1969              :         /*
    1970              :          * We don't bother considering the OR case, because it's fairly
    1971              :          * unlikely anyone would write "v1 IS NULL OR v1 IS NULL". Likewise,
    1972              :          * the NOT case isn't worth expending code on.
    1973              :          */
    1974         4377 :         if (expr->boolop == AND_EXPR)
    1975              :         {
    1976              :             /* At top level we can just recurse (to the List case) */
    1977            0 :             result = find_forced_null_vars((Node *) expr->args);
    1978              :         }
    1979              :     }
    1980        89575 :     return result;
    1981              : }
    1982              : 
    1983              : /*
    1984              :  * find_forced_null_var
    1985              :  *      Return the Var forced null by the given clause, or NULL if it's
    1986              :  *      not an IS NULL-type clause.  For success, the clause must enforce
    1987              :  *      *only* nullness of the particular Var, not any other conditions.
    1988              :  *
    1989              :  * This is just the single-clause case of find_forced_null_vars(), without
    1990              :  * any allowance for AND conditions.  It's used by initsplan.c on individual
    1991              :  * qual clauses.  The reason for not just applying find_forced_null_vars()
    1992              :  * is that if an AND of an IS NULL clause with something else were to somehow
    1993              :  * survive AND/OR flattening, initsplan.c might get fooled into discarding
    1994              :  * the whole clause when only the IS NULL part of it had been proved redundant.
    1995              :  */
    1996              : Var *
    1997       473523 : find_forced_null_var(Node *node)
    1998              : {
    1999       473523 :     if (node == NULL)
    2000            0 :         return NULL;
    2001       473523 :     if (IsA(node, NullTest))
    2002              :     {
    2003              :         /* check for var IS NULL */
    2004         9556 :         NullTest   *expr = (NullTest *) node;
    2005              : 
    2006         9556 :         if (expr->nulltesttype == IS_NULL && !expr->argisrow)
    2007              :         {
    2008         3511 :             Var        *var = (Var *) expr->arg;
    2009              : 
    2010         3511 :             if (var && IsA(var, Var) &&
    2011         3403 :                 var->varlevelsup == 0)
    2012         3403 :                 return var;
    2013              :         }
    2014              :     }
    2015       463967 :     else if (IsA(node, BooleanTest))
    2016              :     {
    2017              :         /* var IS UNKNOWN is equivalent to var IS NULL */
    2018          570 :         BooleanTest *expr = (BooleanTest *) node;
    2019              : 
    2020          570 :         if (expr->booltesttype == IS_UNKNOWN)
    2021              :         {
    2022           45 :             Var        *var = (Var *) expr->arg;
    2023              : 
    2024           45 :             if (var && IsA(var, Var) &&
    2025           45 :                 var->varlevelsup == 0)
    2026           45 :                 return var;
    2027              :         }
    2028              :     }
    2029       470075 :     return NULL;
    2030              : }
    2031              : 
    2032              : /*
    2033              :  * query_outputs_are_not_nullable
    2034              :  *      Returns TRUE if the output values of the Query are certainly not NULL.
    2035              :  *      All output columns must return non-NULL to answer TRUE.
    2036              :  *
    2037              :  * The reason this takes a Query, and not just an individual tlist expression,
    2038              :  * is so that we can make use of the query's WHERE/ON clauses to prove it does
    2039              :  * not return nulls.
    2040              :  *
    2041              :  * In current usage, the passed sub-Query hasn't yet been through any planner
    2042              :  * processing.  This means that applying find_nonnullable_vars() to its WHERE
    2043              :  * clauses isn't really ideal: for lack of const-simplification, we might be
    2044              :  * unable to prove not-nullness in some cases where we could have proved it
    2045              :  * afterwards.  However, we should not get any false positive results.
    2046              :  *
    2047              :  * Like the other forms of nullability analysis above, we can err on the
    2048              :  * side of conservatism: if we're not sure, it's okay to return FALSE.
    2049              :  */
    2050              : bool
    2051          120 : query_outputs_are_not_nullable(Query *query)
    2052              : {
    2053              :     PlannerInfo subroot;
    2054          120 :     List       *safe_quals = NIL;
    2055          120 :     List       *nonnullable_vars = NIL;
    2056          120 :     bool        computed_nonnullable_vars = false;
    2057              : 
    2058              :     /*
    2059              :      * If the query contains set operations, punt.  The set ops themselves
    2060              :      * couldn't introduce nulls that weren't in their inputs, but the tlist
    2061              :      * present in the top-level query is just dummy and won't give us useful
    2062              :      * info.  We could get an answer by recursing to examine each leaf query,
    2063              :      * but for the moment it doesn't seem worth the extra complication.
    2064              :      */
    2065          120 :     if (query->setOperations)
    2066            0 :         return false;
    2067              : 
    2068              :     /*
    2069              :      * If the query contains grouping sets, punt.  Grouping sets can introduce
    2070              :      * NULL values, and we currently lack the PlannerInfo needed to flatten
    2071              :      * grouping Vars in the query's outputs.
    2072              :      */
    2073          120 :     if (query->groupingSets)
    2074            5 :         return false;
    2075              : 
    2076              :     /*
    2077              :      * We need a PlannerInfo to pass to expr_is_nonnullable.  Fortunately, we
    2078              :      * can cons up an entirely dummy one, because only the "parse" link in the
    2079              :      * struct is used by expr_is_nonnullable.
    2080              :      */
    2081        10810 :     MemSet(&subroot, 0, sizeof(subroot));
    2082          115 :     subroot.parse = query;
    2083              : 
    2084              :     /*
    2085              :      * Examine each targetlist entry to prove that it can't produce NULL.
    2086              :      */
    2087          300 :     foreach_node(TargetEntry, tle, query->targetList)
    2088              :     {
    2089          130 :         Expr       *expr = tle->expr;
    2090              : 
    2091              :         /* Resjunk columns can be ignored: they don't produce output values */
    2092          130 :         if (tle->resjunk)
    2093            0 :             continue;
    2094              : 
    2095              :         /*
    2096              :          * Look through binary relabelings, since we know those don't
    2097              :          * introduce nulls.
    2098              :          */
    2099          130 :         while (expr && IsA(expr, RelabelType))
    2100            0 :             expr = ((RelabelType *) expr)->arg;
    2101              : 
    2102          130 :         if (expr == NULL)       /* paranoia */
    2103           30 :             return false;
    2104              : 
    2105              :         /*
    2106              :          * Since the subquery hasn't yet been through expression
    2107              :          * preprocessing, we must explicitly flatten grouping Vars and join
    2108              :          * alias Vars in the given expression.  Note that flatten_group_exprs
    2109              :          * must be applied before flatten_join_alias_vars, as grouping Vars
    2110              :          * can wrap join alias Vars.
    2111              :          *
    2112              :          * We must also apply flatten_join_alias_vars to the quals extracted
    2113              :          * by find_subquery_safe_quals.  We do not need to apply
    2114              :          * flatten_group_exprs to these quals, though, because grouping Vars
    2115              :          * cannot appear in jointree quals.
    2116              :          */
    2117              : 
    2118              :         /*
    2119              :          * We have verified that the query does not contain grouping sets,
    2120              :          * meaning the grouping Vars will not have varnullingrels that need
    2121              :          * preserving, so it's safe to use NULL as the root here.
    2122              :          */
    2123          130 :         if (query->hasGroupRTE)
    2124           10 :             expr = (Expr *) flatten_group_exprs(NULL, query, (Node *) expr);
    2125              : 
    2126              :         /*
    2127              :          * We won't be dealing with arbitrary expressions, so it's safe to use
    2128              :          * NULL as the root, so long as adjust_standard_join_alias_expression
    2129              :          * can handle everything the parser would make as a join alias
    2130              :          * expression.
    2131              :          */
    2132          130 :         expr = (Expr *) flatten_join_alias_vars(NULL, query, (Node *) expr);
    2133              : 
    2134              :         /*
    2135              :          * Check to see if the expr cannot be NULL.  Since we're on a raw
    2136              :          * parse tree, we need to look up the not-null constraints from the
    2137              :          * system catalogs.
    2138              :          */
    2139          130 :         if (expr_is_nonnullable(&subroot, expr, NOTNULL_SOURCE_CATALOG))
    2140           80 :             continue;
    2141              : 
    2142           50 :         if (IsA(expr, Var))
    2143              :         {
    2144           50 :             Var        *var = (Var *) expr;
    2145              : 
    2146              :             /*
    2147              :              * For a plain Var, even if that didn't work, we can conclude that
    2148              :              * the Var is not nullable if find_nonnullable_vars can find a
    2149              :              * "var IS NOT NULL" or similarly strict condition among the quals
    2150              :              * on non-outerjoined-rels.  Compute the list of Vars having such
    2151              :              * quals if we didn't already.
    2152              :              */
    2153           50 :             if (!computed_nonnullable_vars)
    2154              :             {
    2155           50 :                 find_subquery_safe_quals((Node *) query->jointree, &safe_quals);
    2156           50 :                 safe_quals = (List *)
    2157           50 :                     flatten_join_alias_vars(NULL, query, (Node *) safe_quals);
    2158           50 :                 nonnullable_vars = find_nonnullable_vars((Node *) safe_quals);
    2159           50 :                 computed_nonnullable_vars = true;
    2160              :             }
    2161              : 
    2162           50 :             if (!mbms_is_member(var->varno,
    2163           50 :                                 var->varattno - FirstLowInvalidHeapAttributeNumber,
    2164              :                                 nonnullable_vars))
    2165           30 :                 return false;   /* we failed to prove the Var non-null */
    2166              :         }
    2167              :         else
    2168              :         {
    2169              :             /* Punt otherwise */
    2170            0 :             return false;
    2171              :         }
    2172              :     }
    2173              : 
    2174           85 :     return true;
    2175              : }
    2176              : 
    2177              : /*
    2178              :  * find_subquery_safe_quals
    2179              :  *      Traverse jointree to locate quals on non-outerjoined-rels.
    2180              :  *
    2181              :  * We locate all WHERE and JOIN/ON quals that constrain the rels that are not
    2182              :  * below the nullable side of any outer join, and add them to the *safe_quals
    2183              :  * list (forming a list with implicit-AND semantics).  These quals can be used
    2184              :  * to prove non-nullability of the subquery's outputs.
    2185              :  *
    2186              :  * Top-level caller must initialize *safe_quals to NIL.
    2187              :  */
    2188              : static void
    2189          135 : find_subquery_safe_quals(Node *jtnode, List **safe_quals)
    2190              : {
    2191          135 :     if (jtnode == NULL)
    2192            0 :         return;
    2193          135 :     if (IsA(jtnode, RangeTblRef))
    2194              :     {
    2195              :         /* Leaf node: nothing to do */
    2196           60 :         return;
    2197              :     }
    2198           75 :     else if (IsA(jtnode, FromExpr))
    2199              :     {
    2200           50 :         FromExpr   *f = (FromExpr *) jtnode;
    2201              : 
    2202              :         /* All elements of the FROM list are allowable */
    2203          155 :         foreach_ptr(Node, child_node, f->fromlist)
    2204           55 :             find_subquery_safe_quals(child_node, safe_quals);
    2205              :         /* ... and its WHERE quals are too */
    2206           50 :         if (f->quals)
    2207           15 :             *safe_quals = lappend(*safe_quals, f->quals);
    2208              :     }
    2209           25 :     else if (IsA(jtnode, JoinExpr))
    2210              :     {
    2211           25 :         JoinExpr   *j = (JoinExpr *) jtnode;
    2212              : 
    2213           25 :         switch (j->jointype)
    2214              :         {
    2215            5 :             case JOIN_INNER:
    2216              :                 /* visit both children */
    2217            5 :                 find_subquery_safe_quals(j->larg, safe_quals);
    2218            5 :                 find_subquery_safe_quals(j->rarg, safe_quals);
    2219              :                 /* and grab the ON quals too */
    2220            5 :                 if (j->quals)
    2221            5 :                     *safe_quals = lappend(*safe_quals, j->quals);
    2222            5 :                 break;
    2223              : 
    2224           20 :             case JOIN_LEFT:
    2225              :             case JOIN_SEMI:
    2226              :             case JOIN_ANTI:
    2227              : 
    2228              :                 /*
    2229              :                  * Only the left input is possibly non-nullable; furthermore,
    2230              :                  * the quals of this join don't constrain the left input.
    2231              :                  * Note: we probably can't see SEMI or ANTI joins at this
    2232              :                  * point, but if we do, we can treat them like LEFT joins.
    2233              :                  */
    2234           20 :                 find_subquery_safe_quals(j->larg, safe_quals);
    2235           20 :                 break;
    2236              : 
    2237            0 :             case JOIN_RIGHT:
    2238              :                 /* Reverse of the above case */
    2239            0 :                 find_subquery_safe_quals(j->rarg, safe_quals);
    2240            0 :                 break;
    2241              : 
    2242            0 :             case JOIN_FULL:
    2243              :                 /* Neither side is non-nullable, so stop descending */
    2244            0 :                 break;
    2245              : 
    2246            0 :             default:
    2247            0 :                 elog(ERROR, "unrecognized join type: %d",
    2248              :                      (int) j->jointype);
    2249              :                 break;
    2250              :         }
    2251              :     }
    2252              :     else
    2253            0 :         elog(ERROR, "unrecognized node type: %d",
    2254              :              (int) nodeTag(jtnode));
    2255              : }
    2256              : 
    2257              : /*
    2258              :  * Can we treat a ScalarArrayOpExpr as strict?
    2259              :  *
    2260              :  * If "falseOK" is true, then a "false" result can be considered strict,
    2261              :  * else we need to guarantee an actual NULL result for NULL input.
    2262              :  *
    2263              :  * "foo op ALL array" is strict if the op is strict *and* we can prove
    2264              :  * that the array input isn't an empty array.  We can check that
    2265              :  * for the cases of an array constant and an ARRAY[] construct.
    2266              :  *
    2267              :  * "foo op ANY array" is strict in the falseOK sense if the op is strict.
    2268              :  * If not falseOK, the test is the same as for "foo op ALL array".
    2269              :  */
    2270              : static bool
    2271         6371 : is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK)
    2272              : {
    2273              :     Node       *rightop;
    2274              : 
    2275              :     /* The contained operator must be strict. */
    2276         6371 :     set_sa_opfuncid(expr);
    2277         6371 :     if (!func_strict(expr->opfuncid))
    2278            0 :         return false;
    2279              :     /* If ANY and falseOK, that's all we need to check. */
    2280         6371 :     if (expr->useOr && falseOK)
    2281         6265 :         return true;
    2282              :     /* Else, we have to see if the array is provably non-empty. */
    2283              :     Assert(list_length(expr->args) == 2);
    2284          106 :     rightop = (Node *) lsecond(expr->args);
    2285          106 :     if (rightop && IsA(rightop, Const))
    2286            0 :     {
    2287          106 :         Datum       arraydatum = ((Const *) rightop)->constvalue;
    2288          106 :         bool        arrayisnull = ((Const *) rightop)->constisnull;
    2289              :         ArrayType  *arrayval;
    2290              :         int         nitems;
    2291              : 
    2292          106 :         if (arrayisnull)
    2293            0 :             return false;
    2294          106 :         arrayval = DatumGetArrayTypeP(arraydatum);
    2295          106 :         nitems = ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
    2296          106 :         if (nitems > 0)
    2297          106 :             return true;
    2298              :     }
    2299            0 :     else if (rightop && IsA(rightop, ArrayExpr))
    2300              :     {
    2301            0 :         ArrayExpr  *arrayexpr = (ArrayExpr *) rightop;
    2302              : 
    2303            0 :         if (arrayexpr->elements != NIL && !arrayexpr->multidims)
    2304            0 :             return true;
    2305              :     }
    2306            0 :     return false;
    2307              : }
    2308              : 
    2309              : 
    2310              : /*****************************************************************************
    2311              :  *      Check for "pseudo-constant" clauses
    2312              :  *****************************************************************************/
    2313              : 
    2314              : /*
    2315              :  * is_pseudo_constant_clause
    2316              :  *    Detect whether an expression is "pseudo constant", ie, it contains no
    2317              :  *    variables of the current query level and no uses of volatile functions.
    2318              :  *    Such an expr is not necessarily a true constant: it can still contain
    2319              :  *    Params and outer-level Vars, not to mention functions whose results
    2320              :  *    may vary from one statement to the next.  However, the expr's value
    2321              :  *    will be constant over any one scan of the current query, so it can be
    2322              :  *    used as, eg, an indexscan key.  (Actually, the condition for indexscan
    2323              :  *    keys is weaker than this; see is_pseudo_constant_for_index().)
    2324              :  *
    2325              :  * CAUTION: this function omits to test for one very important class of
    2326              :  * not-constant expressions, namely aggregates (Aggrefs).  In current usage
    2327              :  * this is only applied to WHERE clauses and so a check for Aggrefs would be
    2328              :  * a waste of cycles; but be sure to also check contain_agg_clause() if you
    2329              :  * want to know about pseudo-constness in other contexts.  The same goes
    2330              :  * for window functions (WindowFuncs).
    2331              :  */
    2332              : bool
    2333         4772 : is_pseudo_constant_clause(Node *clause)
    2334              : {
    2335              :     /*
    2336              :      * We could implement this check in one recursive scan.  But since the
    2337              :      * check for volatile functions is both moderately expensive and unlikely
    2338              :      * to fail, it seems better to look for Vars first and only check for
    2339              :      * volatile functions if we find no Vars.
    2340              :      */
    2341         4772 :     if (!contain_var_clause(clause) &&
    2342         4772 :         !contain_volatile_functions(clause))
    2343         4772 :         return true;
    2344            0 :     return false;
    2345              : }
    2346              : 
    2347              : /*
    2348              :  * is_pseudo_constant_clause_relids
    2349              :  *    Same as above, except caller already has available the var membership
    2350              :  *    of the expression; this lets us avoid the contain_var_clause() scan.
    2351              :  */
    2352              : bool
    2353       353047 : is_pseudo_constant_clause_relids(Node *clause, Relids relids)
    2354              : {
    2355       353047 :     if (bms_is_empty(relids) &&
    2356       346580 :         !contain_volatile_functions(clause))
    2357       346580 :         return true;
    2358         6467 :     return false;
    2359              : }
    2360              : 
    2361              : 
    2362              : /*****************************************************************************
    2363              :  *                                                                           *
    2364              :  *      General clause-manipulating routines                                 *
    2365              :  *                                                                           *
    2366              :  *****************************************************************************/
    2367              : 
    2368              : /*
    2369              :  * NumRelids
    2370              :  *      (formerly clause_relids)
    2371              :  *
    2372              :  * Returns the number of different base relations referenced in 'clause'.
    2373              :  */
    2374              : int
    2375         1407 : NumRelids(PlannerInfo *root, Node *clause)
    2376              : {
    2377              :     int         result;
    2378         1407 :     Relids      varnos = pull_varnos(root, clause);
    2379              : 
    2380         1407 :     varnos = bms_del_members(varnos, root->outer_join_rels);
    2381         1407 :     result = bms_num_members(varnos);
    2382         1407 :     bms_free(varnos);
    2383         1407 :     return result;
    2384              : }
    2385              : 
    2386              : /*
    2387              :  * CommuteOpExpr: commute a binary operator clause
    2388              :  *
    2389              :  * XXX the clause is destructively modified!
    2390              :  */
    2391              : void
    2392        18536 : CommuteOpExpr(OpExpr *clause)
    2393              : {
    2394              :     Oid         opoid;
    2395              :     Node       *temp;
    2396              : 
    2397              :     /* Sanity checks: caller is at fault if these fail */
    2398        37072 :     if (!is_opclause(clause) ||
    2399        18536 :         list_length(clause->args) != 2)
    2400            0 :         elog(ERROR, "cannot commute non-binary-operator clause");
    2401              : 
    2402        18536 :     opoid = get_commutator(clause->opno);
    2403              : 
    2404        18536 :     if (!OidIsValid(opoid))
    2405            0 :         elog(ERROR, "could not find commutator for operator %u",
    2406              :              clause->opno);
    2407              : 
    2408              :     /*
    2409              :      * modify the clause in-place!
    2410              :      */
    2411        18536 :     clause->opno = opoid;
    2412        18536 :     clause->opfuncid = InvalidOid;
    2413              :     /* opresulttype, opretset, opcollid, inputcollid need not change */
    2414              : 
    2415        18536 :     temp = linitial(clause->args);
    2416        18536 :     linitial(clause->args) = lsecond(clause->args);
    2417        18536 :     lsecond(clause->args) = temp;
    2418        18536 : }
    2419              : 
    2420              : /*
    2421              :  * Helper for eval_const_expressions: check that datatype of an attribute
    2422              :  * is still what it was when the expression was parsed.  This is needed to
    2423              :  * guard against improper simplification after ALTER COLUMN TYPE.  (XXX we
    2424              :  * may well need to make similar checks elsewhere?)
    2425              :  *
    2426              :  * rowtypeid may come from a whole-row Var, and therefore it can be a domain
    2427              :  * over composite, but for this purpose we only care about checking the type
    2428              :  * of a contained field.
    2429              :  */
    2430              : static bool
    2431          594 : rowtype_field_matches(Oid rowtypeid, int fieldnum,
    2432              :                       Oid expectedtype, int32 expectedtypmod,
    2433              :                       Oid expectedcollation)
    2434              : {
    2435              :     TupleDesc   tupdesc;
    2436              :     Form_pg_attribute attr;
    2437              : 
    2438              :     /* No issue for RECORD, since there is no way to ALTER such a type */
    2439          594 :     if (rowtypeid == RECORDOID)
    2440           42 :         return true;
    2441          552 :     tupdesc = lookup_rowtype_tupdesc_domain(rowtypeid, -1, false);
    2442          552 :     if (fieldnum <= 0 || fieldnum > tupdesc->natts)
    2443              :     {
    2444            0 :         ReleaseTupleDesc(tupdesc);
    2445            0 :         return false;
    2446              :     }
    2447          552 :     attr = TupleDescAttr(tupdesc, fieldnum - 1);
    2448          552 :     if (attr->attisdropped ||
    2449          552 :         attr->atttypid != expectedtype ||
    2450          552 :         attr->atttypmod != expectedtypmod ||
    2451          552 :         attr->attcollation != expectedcollation)
    2452              :     {
    2453            0 :         ReleaseTupleDesc(tupdesc);
    2454            0 :         return false;
    2455              :     }
    2456          552 :     ReleaseTupleDesc(tupdesc);
    2457          552 :     return true;
    2458              : }
    2459              : 
    2460              : 
    2461              : /*--------------------
    2462              :  * eval_const_expressions
    2463              :  *
    2464              :  * Reduce any recognizably constant subexpressions of the given
    2465              :  * expression tree, for example "2 + 2" => "4".  More interestingly,
    2466              :  * we can reduce certain boolean expressions even when they contain
    2467              :  * non-constant subexpressions: "x OR true" => "true" no matter what
    2468              :  * the subexpression x is.  (XXX We assume that no such subexpression
    2469              :  * will have important side-effects, which is not necessarily a good
    2470              :  * assumption in the presence of user-defined functions; do we need a
    2471              :  * pg_proc flag that prevents discarding the execution of a function?)
    2472              :  *
    2473              :  * We do understand that certain functions may deliver non-constant
    2474              :  * results even with constant inputs, "nextval()" being the classic
    2475              :  * example.  Functions that are not marked "immutable" in pg_proc
    2476              :  * will not be pre-evaluated here, although we will reduce their
    2477              :  * arguments as far as possible.
    2478              :  *
    2479              :  * Whenever a function is eliminated from the expression by means of
    2480              :  * constant-expression evaluation or inlining, we add the function to
    2481              :  * root->glob->invalItems.  This ensures the plan is known to depend on
    2482              :  * such functions, even though they aren't referenced anymore.
    2483              :  *
    2484              :  * We assume that the tree has already been type-checked and contains
    2485              :  * only operators and functions that are reasonable to try to execute.
    2486              :  *
    2487              :  * NOTE: "root" can be passed as NULL if the caller never wants to do any
    2488              :  * Param substitutions nor receive info about inlined functions nor reduce
    2489              :  * NullTest for Vars to constant true or constant false.
    2490              :  *
    2491              :  * NOTE: the planner assumes that this will always flatten nested AND and
    2492              :  * OR clauses into N-argument form.  See comments in prepqual.c.
    2493              :  *
    2494              :  * NOTE: another critical effect is that any function calls that require
    2495              :  * default arguments will be expanded, and named-argument calls will be
    2496              :  * converted to positional notation.  The executor won't handle either.
    2497              :  *--------------------
    2498              :  */
    2499              : Node *
    2500       898914 : eval_const_expressions(PlannerInfo *root, Node *node)
    2501              : {
    2502              :     eval_const_expressions_context context;
    2503              : 
    2504       898914 :     if (root)
    2505       754808 :         context.boundParams = root->glob->boundParams;    /* bound Params */
    2506              :     else
    2507       144106 :         context.boundParams = NULL;
    2508       898914 :     context.root = root;        /* for inlined-function dependencies */
    2509       898914 :     context.active_fns = NIL;   /* nothing being recursively simplified */
    2510       898914 :     context.case_val = NULL;    /* no CASE being examined */
    2511       898914 :     context.estimate = false;   /* safe transformations only */
    2512       898914 :     return eval_const_expressions_mutator(node, &context);
    2513              : }
    2514              : 
    2515              : #define MIN_ARRAY_SIZE_FOR_HASHED_SAOP 9
    2516              : /*--------------------
    2517              :  * convert_saop_to_hashed_saop
    2518              :  *
    2519              :  * Recursively search 'node' for ScalarArrayOpExprs and fill in the hash
    2520              :  * function for any ScalarArrayOpExpr that looks like it would be useful to
    2521              :  * evaluate using a hash table rather than a linear search.
    2522              :  *
    2523              :  * We'll use a hash table if all of the following conditions are met:
    2524              :  * 1. The 2nd argument of the array contain only Consts.
    2525              :  * 2. useOr is true or there is a valid negator operator for the
    2526              :  *    ScalarArrayOpExpr's opno.
    2527              :  * 3. There's valid hash function for both left and righthand operands and
    2528              :  *    these hash functions are the same.
    2529              :  * 4. If the array contains enough elements for us to consider it to be
    2530              :  *    worthwhile using a hash table rather than a linear search.
    2531              :  */
    2532              : void
    2533       656726 : convert_saop_to_hashed_saop(Node *node)
    2534              : {
    2535       656726 :     (void) convert_saop_to_hashed_saop_walker(node, NULL);
    2536       656726 : }
    2537              : 
    2538              : static bool
    2539      4828301 : convert_saop_to_hashed_saop_walker(Node *node, void *context)
    2540              : {
    2541      4828301 :     if (node == NULL)
    2542       112292 :         return false;
    2543              : 
    2544      4716009 :     if (IsA(node, ScalarArrayOpExpr))
    2545              :     {
    2546        25575 :         ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) node;
    2547        25575 :         Expr       *arrayarg = (Expr *) lsecond(saop->args);
    2548              :         Oid         lefthashfunc;
    2549              :         Oid         righthashfunc;
    2550              : 
    2551        25575 :         if (arrayarg && IsA(arrayarg, Const) &&
    2552        13051 :             !((Const *) arrayarg)->constisnull)
    2553              :         {
    2554        13026 :             if (saop->useOr)
    2555              :             {
    2556        11180 :                 if (get_op_hash_functions(saop->opno, &lefthashfunc, &righthashfunc) &&
    2557        10903 :                     lefthashfunc == righthashfunc)
    2558              :                 {
    2559        10868 :                     Datum       arrdatum = ((Const *) arrayarg)->constvalue;
    2560        10868 :                     ArrayType  *arr = (ArrayType *) DatumGetPointer(arrdatum);
    2561              :                     int         nitems;
    2562              : 
    2563              :                     /*
    2564              :                      * Only fill in the hash functions if the array looks
    2565              :                      * large enough for it to be worth hashing instead of
    2566              :                      * doing a linear search.
    2567              :                      */
    2568        10868 :                     nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));
    2569              : 
    2570        10868 :                     if (nitems >= MIN_ARRAY_SIZE_FOR_HASHED_SAOP)
    2571              :                     {
    2572              :                         /* Looks good. Fill in the hash functions */
    2573          161 :                         saop->hashfuncid = lefthashfunc;
    2574              :                     }
    2575        12573 :                     return false;
    2576              :                 }
    2577              :             }
    2578              :             else                /* !saop->useOr */
    2579              :             {
    2580         1846 :                 Oid         negator = get_negator(saop->opno);
    2581              : 
    2582              :                 /*
    2583              :                  * Check if this is a NOT IN using an operator whose negator
    2584              :                  * is hashable.  If so we can still build a hash table and
    2585              :                  * just ensure the lookup items are not in the hash table.
    2586              :                  */
    2587         3692 :                 if (OidIsValid(negator) &&
    2588         1846 :                     get_op_hash_functions(negator, &lefthashfunc, &righthashfunc) &&
    2589         1705 :                     lefthashfunc == righthashfunc)
    2590              :                 {
    2591         1705 :                     Datum       arrdatum = ((Const *) arrayarg)->constvalue;
    2592         1705 :                     ArrayType  *arr = (ArrayType *) DatumGetPointer(arrdatum);
    2593              :                     int         nitems;
    2594              : 
    2595              :                     /*
    2596              :                      * Only fill in the hash functions if the array looks
    2597              :                      * large enough for it to be worth hashing instead of
    2598              :                      * doing a linear search.
    2599              :                      */
    2600         1705 :                     nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));
    2601              : 
    2602         1705 :                     if (nitems >= MIN_ARRAY_SIZE_FOR_HASHED_SAOP)
    2603              :                     {
    2604              :                         /* Looks good. Fill in the hash functions */
    2605           82 :                         saop->hashfuncid = lefthashfunc;
    2606              : 
    2607              :                         /*
    2608              :                          * Also set the negfuncid.  The executor will need
    2609              :                          * that to perform hashtable lookups.
    2610              :                          */
    2611           82 :                         saop->negfuncid = get_opcode(negator);
    2612              :                     }
    2613         1705 :                     return false;
    2614              :                 }
    2615              :             }
    2616              :         }
    2617              :     }
    2618              : 
    2619      4703436 :     return expression_tree_walker(node, convert_saop_to_hashed_saop_walker, NULL);
    2620              : }
    2621              : 
    2622              : 
    2623              : /*--------------------
    2624              :  * estimate_expression_value
    2625              :  *
    2626              :  * This function attempts to estimate the value of an expression for
    2627              :  * planning purposes.  It is in essence a more aggressive version of
    2628              :  * eval_const_expressions(): we will perform constant reductions that are
    2629              :  * not necessarily 100% safe, but are reasonable for estimation purposes.
    2630              :  *
    2631              :  * Currently the extra steps that are taken in this mode are:
    2632              :  * 1. Substitute values for Params, where a bound Param value has been made
    2633              :  *    available by the caller of planner(), even if the Param isn't marked
    2634              :  *    constant.  This effectively means that we plan using the first supplied
    2635              :  *    value of the Param.
    2636              :  * 2. Fold stable, as well as immutable, functions to constants.
    2637              :  * 3. Reduce PlaceHolderVar nodes to their contained expressions.
    2638              :  *--------------------
    2639              :  */
    2640              : Node *
    2641       713958 : estimate_expression_value(PlannerInfo *root, Node *node)
    2642              : {
    2643              :     eval_const_expressions_context context;
    2644              : 
    2645       713958 :     context.boundParams = root->glob->boundParams;    /* bound Params */
    2646              :     /* we do not need to mark the plan as depending on inlined functions */
    2647       713958 :     context.root = NULL;
    2648       713958 :     context.active_fns = NIL;   /* nothing being recursively simplified */
    2649       713958 :     context.case_val = NULL;    /* no CASE being examined */
    2650       713958 :     context.estimate = true;    /* unsafe transformations OK */
    2651       713958 :     return eval_const_expressions_mutator(node, &context);
    2652              : }
    2653              : 
    2654              : /*
    2655              :  * The generic case in eval_const_expressions_mutator is to recurse using
    2656              :  * expression_tree_mutator, which will copy the given node unchanged but
    2657              :  * const-simplify its arguments (if any) as far as possible.  If the node
    2658              :  * itself does immutable processing, and each of its arguments were reduced
    2659              :  * to a Const, we can then reduce it to a Const using evaluate_expr.  (Some
    2660              :  * node types need more complicated logic; for example, a CASE expression
    2661              :  * might be reducible to a constant even if not all its subtrees are.)
    2662              :  */
    2663              : #define ece_generic_processing(node) \
    2664              :     expression_tree_mutator((Node *) (node), eval_const_expressions_mutator, \
    2665              :                             context)
    2666              : 
    2667              : /*
    2668              :  * Check whether all arguments of the given node were reduced to Consts.
    2669              :  * By going directly to expression_tree_walker, contain_non_const_walker
    2670              :  * is not applied to the node itself, only to its children.
    2671              :  */
    2672              : #define ece_all_arguments_const(node) \
    2673              :     (!expression_tree_walker((Node *) (node), contain_non_const_walker, NULL))
    2674              : 
    2675              : /* Generic macro for applying evaluate_expr */
    2676              : #define ece_evaluate_expr(node) \
    2677              :     ((Node *) evaluate_expr((Expr *) (node), \
    2678              :                             exprType((Node *) (node)), \
    2679              :                             exprTypmod((Node *) (node)), \
    2680              :                             exprCollation((Node *) (node))))
    2681              : 
    2682              : /*
    2683              :  * Recursive guts of eval_const_expressions/estimate_expression_value
    2684              :  */
    2685              : static Node *
    2686      7142620 : eval_const_expressions_mutator(Node *node,
    2687              :                                eval_const_expressions_context *context)
    2688              : {
    2689              : 
    2690              :     /* since this function recurses, it could be driven to stack overflow */
    2691      7142620 :     check_stack_depth();
    2692              : 
    2693      7142620 :     if (node == NULL)
    2694       308753 :         return NULL;
    2695      6833867 :     switch (nodeTag(node))
    2696              :     {
    2697       111514 :         case T_Param:
    2698              :             {
    2699       111514 :                 Param      *param = (Param *) node;
    2700       111514 :                 ParamListInfo paramLI = context->boundParams;
    2701              : 
    2702              :                 /* Look to see if we've been given a value for this Param */
    2703       111514 :                 if (param->paramkind == PARAM_EXTERN &&
    2704        32770 :                     paramLI != NULL &&
    2705        32770 :                     param->paramid > 0 &&
    2706        32770 :                     param->paramid <= paramLI->numParams)
    2707              :                 {
    2708              :                     ParamExternData *prm;
    2709              :                     ParamExternData prmdata;
    2710              : 
    2711              :                     /*
    2712              :                      * Give hook a chance in case parameter is dynamic.  Tell
    2713              :                      * it that this fetch is speculative, so it should avoid
    2714              :                      * erroring out if parameter is unavailable.
    2715              :                      */
    2716        32770 :                     if (paramLI->paramFetch != NULL)
    2717         4245 :                         prm = paramLI->paramFetch(paramLI, param->paramid,
    2718              :                                                   true, &prmdata);
    2719              :                     else
    2720        28525 :                         prm = &paramLI->params[param->paramid - 1];
    2721              : 
    2722              :                     /*
    2723              :                      * We don't just check OidIsValid, but insist that the
    2724              :                      * fetched type match the Param, just in case the hook did
    2725              :                      * something unexpected.  No need to throw an error here
    2726              :                      * though; leave that for runtime.
    2727              :                      */
    2728        32770 :                     if (OidIsValid(prm->ptype) &&
    2729        32770 :                         prm->ptype == param->paramtype)
    2730              :                     {
    2731              :                         /* OK to substitute parameter value? */
    2732        32770 :                         if (context->estimate ||
    2733        32770 :                             (prm->pflags & PARAM_FLAG_CONST))
    2734              :                         {
    2735              :                             /*
    2736              :                              * Return a Const representing the param value.
    2737              :                              * Must copy pass-by-ref datatypes, since the
    2738              :                              * Param might be in a memory context
    2739              :                              * shorter-lived than our output plan should be.
    2740              :                              */
    2741              :                             int16       typLen;
    2742              :                             bool        typByVal;
    2743              :                             Datum       pval;
    2744              :                             Const      *con;
    2745              : 
    2746        32770 :                             get_typlenbyval(param->paramtype,
    2747              :                                             &typLen, &typByVal);
    2748        32770 :                             if (prm->isnull || typByVal)
    2749        20579 :                                 pval = prm->value;
    2750              :                             else
    2751        12191 :                                 pval = datumCopy(prm->value, typByVal, typLen);
    2752        32770 :                             con = makeConst(param->paramtype,
    2753              :                                             param->paramtypmod,
    2754              :                                             param->paramcollid,
    2755              :                                             (int) typLen,
    2756              :                                             pval,
    2757        32770 :                                             prm->isnull,
    2758              :                                             typByVal);
    2759        32770 :                             con->location = param->location;
    2760        32770 :                             return (Node *) con;
    2761              :                         }
    2762              :                     }
    2763              :                 }
    2764              : 
    2765              :                 /*
    2766              :                  * Not replaceable, so just copy the Param (no need to
    2767              :                  * recurse)
    2768              :                  */
    2769        78744 :                 return (Node *) copyObject(param);
    2770              :             }
    2771         3077 :         case T_WindowFunc:
    2772              :             {
    2773         3077 :                 WindowFunc *expr = (WindowFunc *) node;
    2774         3077 :                 Oid         funcid = expr->winfnoid;
    2775              :                 List       *args;
    2776              :                 Expr       *aggfilter;
    2777              :                 HeapTuple   func_tuple;
    2778              :                 WindowFunc *newexpr;
    2779              : 
    2780              :                 /*
    2781              :                  * We can't really simplify a WindowFunc node, but we mustn't
    2782              :                  * just fall through to the default processing, because we
    2783              :                  * have to apply expand_function_arguments to its argument
    2784              :                  * list.  That takes care of inserting default arguments and
    2785              :                  * expanding named-argument notation.
    2786              :                  */
    2787         3077 :                 func_tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    2788         3077 :                 if (!HeapTupleIsValid(func_tuple))
    2789            0 :                     elog(ERROR, "cache lookup failed for function %u", funcid);
    2790              : 
    2791         3077 :                 args = expand_function_arguments(expr->args,
    2792              :                                                  false, expr->wintype,
    2793              :                                                  func_tuple);
    2794              : 
    2795         3077 :                 ReleaseSysCache(func_tuple);
    2796              : 
    2797              :                 /* Now, recursively simplify the args (which are a List) */
    2798              :                 args = (List *)
    2799         3077 :                     expression_tree_mutator((Node *) args,
    2800              :                                             eval_const_expressions_mutator,
    2801              :                                             context);
    2802              :                 /* ... and the filter expression, which isn't */
    2803              :                 aggfilter = (Expr *)
    2804         3077 :                     eval_const_expressions_mutator((Node *) expr->aggfilter,
    2805              :                                                    context);
    2806              : 
    2807              :                 /* And build the replacement WindowFunc node */
    2808         3077 :                 newexpr = makeNode(WindowFunc);
    2809         3077 :                 newexpr->winfnoid = expr->winfnoid;
    2810         3077 :                 newexpr->wintype = expr->wintype;
    2811         3077 :                 newexpr->wincollid = expr->wincollid;
    2812         3077 :                 newexpr->inputcollid = expr->inputcollid;
    2813         3077 :                 newexpr->args = args;
    2814         3077 :                 newexpr->aggfilter = aggfilter;
    2815         3077 :                 newexpr->runCondition = expr->runCondition;
    2816         3077 :                 newexpr->winref = expr->winref;
    2817         3077 :                 newexpr->winstar = expr->winstar;
    2818         3077 :                 newexpr->winagg = expr->winagg;
    2819         3077 :                 newexpr->ignore_nulls = expr->ignore_nulls;
    2820         3077 :                 newexpr->location = expr->location;
    2821              : 
    2822         3077 :                 return (Node *) newexpr;
    2823              :             }
    2824       380690 :         case T_FuncExpr:
    2825              :             {
    2826       380690 :                 FuncExpr   *expr = (FuncExpr *) node;
    2827       380690 :                 List       *args = expr->args;
    2828              :                 Expr       *simple;
    2829              :                 FuncExpr   *newexpr;
    2830              : 
    2831              :                 /*
    2832              :                  * Code for op/func reduction is pretty bulky, so split it out
    2833              :                  * as a separate function.  Note: exprTypmod normally returns
    2834              :                  * -1 for a FuncExpr, but not when the node is recognizably a
    2835              :                  * length coercion; we want to preserve the typmod in the
    2836              :                  * eventual Const if so.
    2837              :                  */
    2838       380690 :                 simple = simplify_function(expr->funcid,
    2839              :                                            expr->funcresulttype,
    2840              :                                            exprTypmod(node),
    2841              :                                            expr->funccollid,
    2842              :                                            expr->inputcollid,
    2843              :                                            &args,
    2844       380690 :                                            expr->funcvariadic,
    2845              :                                            true,
    2846              :                                            true,
    2847              :                                            context);
    2848       378834 :                 if (simple)     /* successfully simplified it */
    2849       110946 :                     return (Node *) simple;
    2850              : 
    2851              :                 /*
    2852              :                  * The expression cannot be simplified any further, so build
    2853              :                  * and return a replacement FuncExpr node using the
    2854              :                  * possibly-simplified arguments.  Note that we have also
    2855              :                  * converted the argument list to positional notation.
    2856              :                  */
    2857       267888 :                 newexpr = makeNode(FuncExpr);
    2858       267888 :                 newexpr->funcid = expr->funcid;
    2859       267888 :                 newexpr->funcresulttype = expr->funcresulttype;
    2860       267888 :                 newexpr->funcretset = expr->funcretset;
    2861       267888 :                 newexpr->funcvariadic = expr->funcvariadic;
    2862       267888 :                 newexpr->funcformat = expr->funcformat;
    2863       267888 :                 newexpr->funccollid = expr->funccollid;
    2864       267888 :                 newexpr->inputcollid = expr->inputcollid;
    2865       267888 :                 newexpr->args = args;
    2866       267888 :                 newexpr->location = expr->location;
    2867       267888 :                 return (Node *) newexpr;
    2868              :             }
    2869        38101 :         case T_Aggref:
    2870        38101 :             node = ece_generic_processing(node);
    2871        38101 :             if (context->root != NULL)
    2872        38101 :                 return simplify_aggref((Aggref *) node, context);
    2873            0 :             return node;
    2874       555910 :         case T_OpExpr:
    2875              :             {
    2876       555910 :                 OpExpr     *expr = (OpExpr *) node;
    2877       555910 :                 List       *args = expr->args;
    2878              :                 Expr       *simple;
    2879              :                 OpExpr     *newexpr;
    2880              : 
    2881              :                 /*
    2882              :                  * Need to get OID of underlying function.  Okay to scribble
    2883              :                  * on input to this extent.
    2884              :                  */
    2885       555910 :                 set_opfuncid(expr);
    2886              : 
    2887              :                 /*
    2888              :                  * Code for op/func reduction is pretty bulky, so split it out
    2889              :                  * as a separate function.
    2890              :                  */
    2891       555910 :                 simple = simplify_function(expr->opfuncid,
    2892              :                                            expr->opresulttype, -1,
    2893              :                                            expr->opcollid,
    2894              :                                            expr->inputcollid,
    2895              :                                            &args,
    2896              :                                            false,
    2897              :                                            true,
    2898              :                                            true,
    2899              :                                            context);
    2900       555132 :                 if (simple)     /* successfully simplified it */
    2901        20205 :                     return (Node *) simple;
    2902              : 
    2903              :                 /*
    2904              :                  * If the operator is boolean equality or inequality, we know
    2905              :                  * how to simplify cases involving one constant and one
    2906              :                  * non-constant argument.
    2907              :                  */
    2908       534927 :                 if (expr->opno == BooleanEqualOperator ||
    2909       533303 :                     expr->opno == BooleanNotEqualOperator)
    2910              :                 {
    2911         1764 :                     simple = (Expr *) simplify_boolean_equality(expr->opno,
    2912              :                                                                 args);
    2913         1764 :                     if (simple) /* successfully simplified it */
    2914         1279 :                         return (Node *) simple;
    2915              :                 }
    2916              : 
    2917              :                 /*
    2918              :                  * The expression cannot be simplified any further, so build
    2919              :                  * and return a replacement OpExpr node using the
    2920              :                  * possibly-simplified arguments.
    2921              :                  */
    2922       533648 :                 newexpr = makeNode(OpExpr);
    2923       533648 :                 newexpr->opno = expr->opno;
    2924       533648 :                 newexpr->opfuncid = expr->opfuncid;
    2925       533648 :                 newexpr->opresulttype = expr->opresulttype;
    2926       533648 :                 newexpr->opretset = expr->opretset;
    2927       533648 :                 newexpr->opcollid = expr->opcollid;
    2928       533648 :                 newexpr->inputcollid = expr->inputcollid;
    2929       533648 :                 newexpr->args = args;
    2930       533648 :                 newexpr->location = expr->location;
    2931       533648 :                 return (Node *) newexpr;
    2932              :             }
    2933          960 :         case T_DistinctExpr:
    2934              :             {
    2935          960 :                 DistinctExpr *expr = (DistinctExpr *) node;
    2936              :                 List       *args;
    2937              :                 ListCell   *arg;
    2938          960 :                 bool        has_null_input = false;
    2939          960 :                 bool        all_null_input = true;
    2940          960 :                 bool        has_nonconst_input = false;
    2941          960 :                 bool        has_nullable_nonconst = false;
    2942              :                 Expr       *simple;
    2943              :                 DistinctExpr *newexpr;
    2944              : 
    2945              :                 /*
    2946              :                  * Reduce constants in the DistinctExpr's arguments.  We know
    2947              :                  * args is either NIL or a List node, so we can call
    2948              :                  * expression_tree_mutator directly rather than recursing to
    2949              :                  * self.
    2950              :                  */
    2951          960 :                 args = (List *) expression_tree_mutator((Node *) expr->args,
    2952              :                                                         eval_const_expressions_mutator,
    2953              :                                                         context);
    2954              : 
    2955              :                 /*
    2956              :                  * We must do our own check for NULLs because DistinctExpr has
    2957              :                  * different results for NULL input than the underlying
    2958              :                  * operator does.  We also check if any non-constant input is
    2959              :                  * potentially nullable.
    2960              :                  */
    2961         2880 :                 foreach(arg, args)
    2962              :                 {
    2963         1920 :                     if (IsA(lfirst(arg), Const))
    2964              :                     {
    2965          335 :                         has_null_input |= ((Const *) lfirst(arg))->constisnull;
    2966          335 :                         all_null_input &= ((Const *) lfirst(arg))->constisnull;
    2967              :                     }
    2968              :                     else
    2969              :                     {
    2970         1585 :                         has_nonconst_input = true;
    2971         1585 :                         all_null_input = false;
    2972              : 
    2973         1585 :                         if (!has_nullable_nonconst &&
    2974          940 :                             !expr_is_nonnullable(context->root,
    2975          940 :                                                  (Expr *) lfirst(arg),
    2976              :                                                  NOTNULL_SOURCE_HASHTABLE))
    2977          855 :                             has_nullable_nonconst = true;
    2978              :                     }
    2979              :                 }
    2980              : 
    2981          960 :                 if (!has_nonconst_input)
    2982              :                 {
    2983              :                     /*
    2984              :                      * All inputs are constants.  We can optimize this out
    2985              :                      * completely.
    2986              :                      */
    2987              : 
    2988              :                     /* all nulls? then not distinct */
    2989           45 :                     if (all_null_input)
    2990           10 :                         return makeBoolConst(false, false);
    2991              : 
    2992              :                     /* one null? then distinct */
    2993           35 :                     if (has_null_input)
    2994           15 :                         return makeBoolConst(true, false);
    2995              : 
    2996              :                     /* otherwise try to evaluate the '=' operator */
    2997              :                     /* (NOT okay to try to inline it, though!) */
    2998              : 
    2999              :                     /*
    3000              :                      * Need to get OID of underlying function.  Okay to
    3001              :                      * scribble on input to this extent.
    3002              :                      */
    3003           20 :                     set_opfuncid((OpExpr *) expr);  /* rely on struct
    3004              :                                                      * equivalence */
    3005              : 
    3006              :                     /*
    3007              :                      * Code for op/func reduction is pretty bulky, so split it
    3008              :                      * out as a separate function.
    3009              :                      */
    3010           20 :                     simple = simplify_function(expr->opfuncid,
    3011              :                                                expr->opresulttype, -1,
    3012              :                                                expr->opcollid,
    3013              :                                                expr->inputcollid,
    3014              :                                                &args,
    3015              :                                                false,
    3016              :                                                false,
    3017              :                                                false,
    3018              :                                                context);
    3019           20 :                     if (simple) /* successfully simplified it */
    3020              :                     {
    3021              :                         /*
    3022              :                          * Since the underlying operator is "=", must negate
    3023              :                          * its result
    3024              :                          */
    3025           20 :                         Const      *csimple = castNode(Const, simple);
    3026              : 
    3027           20 :                         csimple->constvalue =
    3028           20 :                             BoolGetDatum(!DatumGetBool(csimple->constvalue));
    3029           20 :                         return (Node *) csimple;
    3030              :                     }
    3031              :                 }
    3032          915 :                 else if (!has_nullable_nonconst)
    3033              :                 {
    3034              :                     /*
    3035              :                      * There are non-constant inputs, but since all of them
    3036              :                      * are proven non-nullable, "IS DISTINCT FROM" semantics
    3037              :                      * are much simpler.
    3038              :                      */
    3039              : 
    3040              :                     OpExpr     *eqexpr;
    3041              : 
    3042              :                     /*
    3043              :                      * If one input is an explicit NULL constant, and the
    3044              :                      * other is a non-nullable expression, the result is
    3045              :                      * always TRUE.
    3046              :                      */
    3047           60 :                     if (has_null_input)
    3048           20 :                         return makeBoolConst(true, false);
    3049              : 
    3050              :                     /*
    3051              :                      * Otherwise, both inputs are known non-nullable.  In this
    3052              :                      * case, "IS DISTINCT FROM" is equivalent to the standard
    3053              :                      * inequality operator (usually "<>").  We convert this to
    3054              :                      * an OpExpr, which is a more efficient representation for
    3055              :                      * the planner.  It can enable the use of partial indexes
    3056              :                      * and constraint exclusion.  Furthermore, if the clause
    3057              :                      * is negated (ie, "IS NOT DISTINCT FROM"), the resulting
    3058              :                      * "=" operator can allow the planner to use index scans,
    3059              :                      * merge joins, hash joins, and EC-based qual deductions.
    3060              :                      */
    3061           40 :                     eqexpr = makeNode(OpExpr);
    3062           40 :                     eqexpr->opno = expr->opno;
    3063           40 :                     eqexpr->opfuncid = expr->opfuncid;
    3064           40 :                     eqexpr->opresulttype = BOOLOID;
    3065           40 :                     eqexpr->opretset = expr->opretset;
    3066           40 :                     eqexpr->opcollid = expr->opcollid;
    3067           40 :                     eqexpr->inputcollid = expr->inputcollid;
    3068           40 :                     eqexpr->args = args;
    3069           40 :                     eqexpr->location = expr->location;
    3070              : 
    3071           40 :                     return eval_const_expressions_mutator(negate_clause((Node *) eqexpr),
    3072              :                                                           context);
    3073              :                 }
    3074          855 :                 else if (has_null_input)
    3075              :                 {
    3076              :                     /*
    3077              :                      * One input is a nullable non-constant expression, and
    3078              :                      * the other is an explicit NULL constant.  We can
    3079              :                      * transform this to a NullTest with !argisrow, which is
    3080              :                      * much more amenable to optimization.
    3081              :                      */
    3082              : 
    3083           40 :                     NullTest   *nt = makeNode(NullTest);
    3084              : 
    3085           80 :                     nt->arg = (Expr *) (IsA(linitial(args), Const) ?
    3086           40 :                                         lsecond(args) : linitial(args));
    3087           40 :                     nt->nulltesttype = IS_NOT_NULL;
    3088              : 
    3089              :                     /*
    3090              :                      * argisrow = false is correct whether or not arg is
    3091              :                      * composite
    3092              :                      */
    3093           40 :                     nt->argisrow = false;
    3094           40 :                     nt->location = expr->location;
    3095              : 
    3096           40 :                     return eval_const_expressions_mutator((Node *) nt, context);
    3097              :                 }
    3098              : 
    3099              :                 /*
    3100              :                  * The expression cannot be simplified any further, so build
    3101              :                  * and return a replacement DistinctExpr node using the
    3102              :                  * possibly-simplified arguments.
    3103              :                  */
    3104          815 :                 newexpr = makeNode(DistinctExpr);
    3105          815 :                 newexpr->opno = expr->opno;
    3106          815 :                 newexpr->opfuncid = expr->opfuncid;
    3107          815 :                 newexpr->opresulttype = expr->opresulttype;
    3108          815 :                 newexpr->opretset = expr->opretset;
    3109          815 :                 newexpr->opcollid = expr->opcollid;
    3110          815 :                 newexpr->inputcollid = expr->inputcollid;
    3111          815 :                 newexpr->args = args;
    3112          815 :                 newexpr->location = expr->location;
    3113          815 :                 return (Node *) newexpr;
    3114              :             }
    3115          904 :         case T_NullIfExpr:
    3116              :             {
    3117              :                 NullIfExpr *expr;
    3118              :                 ListCell   *arg;
    3119          904 :                 bool        has_nonconst_input = false;
    3120              : 
    3121              :                 /* Copy the node and const-simplify its arguments */
    3122          904 :                 expr = (NullIfExpr *) ece_generic_processing(node);
    3123              : 
    3124              :                 /* If either argument is NULL they can't be equal */
    3125         2707 :                 foreach(arg, expr->args)
    3126              :                 {
    3127         1808 :                     if (!IsA(lfirst(arg), Const))
    3128          878 :                         has_nonconst_input = true;
    3129          930 :                     else if (((Const *) lfirst(arg))->constisnull)
    3130            5 :                         return (Node *) linitial(expr->args);
    3131              :                 }
    3132              : 
    3133              :                 /*
    3134              :                  * Need to get OID of underlying function before checking if
    3135              :                  * the function is OK to evaluate.
    3136              :                  */
    3137          899 :                 set_opfuncid((OpExpr *) expr);
    3138              : 
    3139          930 :                 if (!has_nonconst_input &&
    3140           31 :                     ece_function_is_safe(expr->opfuncid, context))
    3141           31 :                     return ece_evaluate_expr(expr);
    3142              : 
    3143          868 :                 return (Node *) expr;
    3144              :             }
    3145        29353 :         case T_ScalarArrayOpExpr:
    3146              :             {
    3147              :                 ScalarArrayOpExpr *saop;
    3148              : 
    3149              :                 /* Copy the node and const-simplify its arguments */
    3150        29353 :                 saop = (ScalarArrayOpExpr *) ece_generic_processing(node);
    3151              : 
    3152              :                 /* Make sure we know underlying function */
    3153        29353 :                 set_sa_opfuncid(saop);
    3154              : 
    3155              :                 /*
    3156              :                  * If all arguments are Consts, and it's a safe function, we
    3157              :                  * can fold to a constant
    3158              :                  */
    3159        29622 :                 if (ece_all_arguments_const(saop) &&
    3160          269 :                     ece_function_is_safe(saop->opfuncid, context))
    3161          269 :                     return ece_evaluate_expr(saop);
    3162        29084 :                 return (Node *) saop;
    3163              :             }
    3164       146871 :         case T_BoolExpr:
    3165              :             {
    3166       146871 :                 BoolExpr   *expr = (BoolExpr *) node;
    3167              : 
    3168       146871 :                 switch (expr->boolop)
    3169              :                 {
    3170        15328 :                     case OR_EXPR:
    3171              :                         {
    3172              :                             List       *newargs;
    3173        15328 :                             bool        haveNull = false;
    3174        15328 :                             bool        forceTrue = false;
    3175              : 
    3176        15328 :                             newargs = simplify_or_arguments(expr->args,
    3177              :                                                             context,
    3178              :                                                             &haveNull,
    3179              :                                                             &forceTrue);
    3180        15328 :                             if (forceTrue)
    3181          106 :                                 return makeBoolConst(true, false);
    3182        15222 :                             if (haveNull)
    3183         3802 :                                 newargs = lappend(newargs,
    3184         3802 :                                                   makeBoolConst(false, true));
    3185              :                             /* If all the inputs are FALSE, result is FALSE */
    3186        15222 :                             if (newargs == NIL)
    3187           28 :                                 return makeBoolConst(false, false);
    3188              : 
    3189              :                             /*
    3190              :                              * If only one nonconst-or-NULL input, it's the
    3191              :                              * result
    3192              :                              */
    3193        15194 :                             if (list_length(newargs) == 1)
    3194           87 :                                 return (Node *) linitial(newargs);
    3195              :                             /* Else we still need an OR node */
    3196        15107 :                             return (Node *) make_orclause(newargs);
    3197              :                         }
    3198       116558 :                     case AND_EXPR:
    3199              :                         {
    3200              :                             List       *newargs;
    3201       116558 :                             bool        haveNull = false;
    3202       116558 :                             bool        forceFalse = false;
    3203              : 
    3204       116558 :                             newargs = simplify_and_arguments(expr->args,
    3205              :                                                              context,
    3206              :                                                              &haveNull,
    3207              :                                                              &forceFalse);
    3208       116558 :                             if (forceFalse)
    3209          655 :                                 return makeBoolConst(false, false);
    3210       115903 :                             if (haveNull)
    3211           25 :                                 newargs = lappend(newargs,
    3212           25 :                                                   makeBoolConst(false, true));
    3213              :                             /* If all the inputs are TRUE, result is TRUE */
    3214       115903 :                             if (newargs == NIL)
    3215          186 :                                 return makeBoolConst(true, false);
    3216              : 
    3217              :                             /*
    3218              :                              * If only one nonconst-or-NULL input, it's the
    3219              :                              * result
    3220              :                              */
    3221       115717 :                             if (list_length(newargs) == 1)
    3222          177 :                                 return (Node *) linitial(newargs);
    3223              :                             /* Else we still need an AND node */
    3224       115540 :                             return (Node *) make_andclause(newargs);
    3225              :                         }
    3226        14985 :                     case NOT_EXPR:
    3227              :                         {
    3228              :                             Node       *arg;
    3229              : 
    3230              :                             Assert(list_length(expr->args) == 1);
    3231        14985 :                             arg = eval_const_expressions_mutator(linitial(expr->args),
    3232              :                                                                  context);
    3233              : 
    3234              :                             /*
    3235              :                              * Use negate_clause() to see if we can simplify
    3236              :                              * away the NOT.
    3237              :                              */
    3238        14985 :                             return negate_clause(arg);
    3239              :                         }
    3240            0 :                     default:
    3241            0 :                         elog(ERROR, "unrecognized boolop: %d",
    3242              :                              (int) expr->boolop);
    3243              :                         break;
    3244              :                 }
    3245              :                 break;
    3246              :             }
    3247          637 :         case T_JsonValueExpr:
    3248              :             {
    3249          637 :                 JsonValueExpr *jve = (JsonValueExpr *) node;
    3250          637 :                 Node       *raw_expr = (Node *) jve->raw_expr;
    3251          637 :                 Node       *formatted_expr = (Node *) jve->formatted_expr;
    3252              : 
    3253              :                 /*
    3254              :                  * If we can fold formatted_expr to a constant, we can elide
    3255              :                  * the JsonValueExpr altogether.  Otherwise we must process
    3256              :                  * raw_expr too.  But JsonFormat is a flat node and requires
    3257              :                  * no simplification, only copying.
    3258              :                  */
    3259          637 :                 formatted_expr = eval_const_expressions_mutator(formatted_expr,
    3260              :                                                                 context);
    3261          637 :                 if (formatted_expr && IsA(formatted_expr, Const))
    3262          443 :                     return formatted_expr;
    3263              : 
    3264          194 :                 raw_expr = eval_const_expressions_mutator(raw_expr, context);
    3265              : 
    3266          194 :                 return (Node *) makeJsonValueExpr((Expr *) raw_expr,
    3267              :                                                   (Expr *) formatted_expr,
    3268          194 :                                                   copyObject(jve->format));
    3269              :             }
    3270         1403 :         case T_JsonConstructorExpr:
    3271              :             {
    3272         1403 :                 JsonConstructorExpr *jce = (JsonConstructorExpr *) node;
    3273              : 
    3274              :                 /*
    3275              :                  * JSCTOR_JSON_ARRAY_QUERY carries a pre-built executable form
    3276              :                  * in its func field (a COALESCE-wrapped JSON_ARRAYAGG
    3277              :                  * subquery, constructed during parse analysis).  Replace the
    3278              :                  * node with that expression and continue simplifying.
    3279              :                  */
    3280         1403 :                 if (jce->type == JSCTOR_JSON_ARRAY_QUERY)
    3281           60 :                     return eval_const_expressions_mutator((Node *) jce->func,
    3282              :                                                           context);
    3283              :             }
    3284         1343 :             break;
    3285          485 :         case T_SubPlan:
    3286              :         case T_AlternativeSubPlan:
    3287              : 
    3288              :             /*
    3289              :              * Return a SubPlan unchanged --- too late to do anything with it.
    3290              :              *
    3291              :              * XXX should we ereport() here instead?  Probably this routine
    3292              :              * should never be invoked after SubPlan creation.
    3293              :              */
    3294          485 :             return node;
    3295       130004 :         case T_RelabelType:
    3296              :             {
    3297       130004 :                 RelabelType *relabel = (RelabelType *) node;
    3298              :                 Node       *arg;
    3299              : 
    3300              :                 /* Simplify the input ... */
    3301       130004 :                 arg = eval_const_expressions_mutator((Node *) relabel->arg,
    3302              :                                                      context);
    3303              :                 /* ... and attach a new RelabelType node, if needed */
    3304       130000 :                 return applyRelabelType(arg,
    3305              :                                         relabel->resulttype,
    3306              :                                         relabel->resulttypmod,
    3307              :                                         relabel->resultcollid,
    3308              :                                         relabel->relabelformat,
    3309              :                                         relabel->location,
    3310              :                                         true);
    3311              :             }
    3312        24885 :         case T_CoerceViaIO:
    3313              :             {
    3314        24885 :                 CoerceViaIO *expr = (CoerceViaIO *) node;
    3315              :                 List       *args;
    3316              :                 Oid         outfunc;
    3317              :                 bool        outtypisvarlena;
    3318              :                 Oid         infunc;
    3319              :                 Oid         intypioparam;
    3320              :                 Expr       *simple;
    3321              :                 CoerceViaIO *newexpr;
    3322              : 
    3323              :                 /* Make a List so we can use simplify_function */
    3324        24885 :                 args = list_make1(expr->arg);
    3325              : 
    3326              :                 /*
    3327              :                  * CoerceViaIO represents calling the source type's output
    3328              :                  * function then the result type's input function.  So, try to
    3329              :                  * simplify it as though it were a stack of two such function
    3330              :                  * calls.  First we need to know what the functions are.
    3331              :                  *
    3332              :                  * Note that the coercion functions are assumed not to care
    3333              :                  * about input collation, so we just pass InvalidOid for that.
    3334              :                  */
    3335        24885 :                 getTypeOutputInfo(exprType((Node *) expr->arg),
    3336              :                                   &outfunc, &outtypisvarlena);
    3337        24885 :                 getTypeInputInfo(expr->resulttype,
    3338              :                                  &infunc, &intypioparam);
    3339              : 
    3340        24885 :                 simple = simplify_function(outfunc,
    3341              :                                            CSTRINGOID, -1,
    3342              :                                            InvalidOid,
    3343              :                                            InvalidOid,
    3344              :                                            &args,
    3345              :                                            false,
    3346              :                                            true,
    3347              :                                            true,
    3348              :                                            context);
    3349        24885 :                 if (simple)     /* successfully simplified output fn */
    3350              :                 {
    3351              :                     /*
    3352              :                      * Input functions may want 1 to 3 arguments.  We always
    3353              :                      * supply all three, trusting that nothing downstream will
    3354              :                      * complain.
    3355              :                      */
    3356         1901 :                     args = list_make3(simple,
    3357              :                                       makeConst(OIDOID,
    3358              :                                                 -1,
    3359              :                                                 InvalidOid,
    3360              :                                                 sizeof(Oid),
    3361              :                                                 ObjectIdGetDatum(intypioparam),
    3362              :                                                 false,
    3363              :                                                 true),
    3364              :                                       makeConst(INT4OID,
    3365              :                                                 -1,
    3366              :                                                 InvalidOid,
    3367              :                                                 sizeof(int32),
    3368              :                                                 Int32GetDatum(-1),
    3369              :                                                 false,
    3370              :                                                 true));
    3371              : 
    3372         1901 :                     simple = simplify_function(infunc,
    3373              :                                                expr->resulttype, -1,
    3374              :                                                expr->resultcollid,
    3375              :                                                InvalidOid,
    3376              :                                                &args,
    3377              :                                                false,
    3378              :                                                false,
    3379              :                                                true,
    3380              :                                                context);
    3381         1828 :                     if (simple) /* successfully simplified input fn */
    3382         1765 :                         return (Node *) simple;
    3383              :                 }
    3384              : 
    3385              :                 /*
    3386              :                  * The expression cannot be simplified any further, so build
    3387              :                  * and return a replacement CoerceViaIO node using the
    3388              :                  * possibly-simplified argument.
    3389              :                  */
    3390        23047 :                 newexpr = makeNode(CoerceViaIO);
    3391        23047 :                 newexpr->arg = (Expr *) linitial(args);
    3392        23047 :                 newexpr->resulttype = expr->resulttype;
    3393        23047 :                 newexpr->resultcollid = expr->resultcollid;
    3394        23047 :                 newexpr->coerceformat = expr->coerceformat;
    3395        23047 :                 newexpr->location = expr->location;
    3396        23047 :                 return (Node *) newexpr;
    3397              :             }
    3398         8169 :         case T_ArrayCoerceExpr:
    3399              :             {
    3400         8169 :                 ArrayCoerceExpr *ac = makeNode(ArrayCoerceExpr);
    3401              :                 Node       *save_case_val;
    3402              : 
    3403              :                 /*
    3404              :                  * Copy the node and const-simplify its arguments.  We can't
    3405              :                  * use ece_generic_processing() here because we need to mess
    3406              :                  * with case_val only while processing the elemexpr.
    3407              :                  */
    3408         8169 :                 memcpy(ac, node, sizeof(ArrayCoerceExpr));
    3409         8169 :                 ac->arg = (Expr *)
    3410         8169 :                     eval_const_expressions_mutator((Node *) ac->arg,
    3411              :                                                    context);
    3412              : 
    3413              :                 /*
    3414              :                  * Set up for the CaseTestExpr node contained in the elemexpr.
    3415              :                  * We must prevent it from absorbing any outer CASE value.
    3416              :                  */
    3417         8169 :                 save_case_val = context->case_val;
    3418         8169 :                 context->case_val = NULL;
    3419              : 
    3420         8169 :                 ac->elemexpr = (Expr *)
    3421         8169 :                     eval_const_expressions_mutator((Node *) ac->elemexpr,
    3422              :                                                    context);
    3423              : 
    3424         8169 :                 context->case_val = save_case_val;
    3425              : 
    3426              :                 /*
    3427              :                  * If constant argument and the per-element expression is
    3428              :                  * immutable, we can simplify the whole thing to a constant.
    3429              :                  * Exception: although contain_mutable_functions considers
    3430              :                  * CoerceToDomain immutable for historical reasons, let's not
    3431              :                  * do so here; this ensures coercion to an array-over-domain
    3432              :                  * does not apply the domain's constraints until runtime.
    3433              :                  */
    3434         8169 :                 if (ac->arg && IsA(ac->arg, Const) &&
    3435          884 :                     ac->elemexpr && !IsA(ac->elemexpr, CoerceToDomain) &&
    3436          864 :                     !contain_mutable_functions((Node *) ac->elemexpr))
    3437          864 :                     return ece_evaluate_expr(ac);
    3438              : 
    3439         7305 :                 return (Node *) ac;
    3440              :             }
    3441         8238 :         case T_CollateExpr:
    3442              :             {
    3443              :                 /*
    3444              :                  * We replace CollateExpr with RelabelType, so as to improve
    3445              :                  * uniformity of expression representation and thus simplify
    3446              :                  * comparison of expressions.  Hence this looks very nearly
    3447              :                  * the same as the RelabelType case, and we can apply the same
    3448              :                  * optimizations to avoid unnecessary RelabelTypes.
    3449              :                  */
    3450         8238 :                 CollateExpr *collate = (CollateExpr *) node;
    3451              :                 Node       *arg;
    3452              : 
    3453              :                 /* Simplify the input ... */
    3454         8238 :                 arg = eval_const_expressions_mutator((Node *) collate->arg,
    3455              :                                                      context);
    3456              :                 /* ... and attach a new RelabelType node, if needed */
    3457         8238 :                 return applyRelabelType(arg,
    3458              :                                         exprType(arg),
    3459              :                                         exprTypmod(arg),
    3460              :                                         collate->collOid,
    3461              :                                         COERCE_IMPLICIT_CAST,
    3462              :                                         collate->location,
    3463              :                                         true);
    3464              :             }
    3465        28163 :         case T_CaseExpr:
    3466              :             {
    3467              :                 /*----------
    3468              :                  * CASE expressions can be simplified if there are constant
    3469              :                  * condition clauses:
    3470              :                  *      FALSE (or NULL): drop the alternative
    3471              :                  *      TRUE: drop all remaining alternatives
    3472              :                  * If the first non-FALSE alternative is a constant TRUE,
    3473              :                  * we can simplify the entire CASE to that alternative's
    3474              :                  * expression.  If there are no non-FALSE alternatives,
    3475              :                  * we simplify the entire CASE to the default result (ELSE).
    3476              :                  *
    3477              :                  * If we have a simple-form CASE with constant test
    3478              :                  * expression, we substitute the constant value for contained
    3479              :                  * CaseTestExpr placeholder nodes, so that we have the
    3480              :                  * opportunity to reduce constant test conditions.  For
    3481              :                  * example this allows
    3482              :                  *      CASE 0 WHEN 0 THEN 1 ELSE 1/0 END
    3483              :                  * to reduce to 1 rather than drawing a divide-by-0 error.
    3484              :                  * Note that when the test expression is constant, we don't
    3485              :                  * have to include it in the resulting CASE; for example
    3486              :                  *      CASE 0 WHEN x THEN y ELSE z END
    3487              :                  * is transformed by the parser to
    3488              :                  *      CASE 0 WHEN CaseTestExpr = x THEN y ELSE z END
    3489              :                  * which we can simplify to
    3490              :                  *      CASE WHEN 0 = x THEN y ELSE z END
    3491              :                  * It is not necessary for the executor to evaluate the "arg"
    3492              :                  * expression when executing the CASE, since any contained
    3493              :                  * CaseTestExprs that might have referred to it will have been
    3494              :                  * replaced by the constant.
    3495              :                  *----------
    3496              :                  */
    3497        28163 :                 CaseExpr   *caseexpr = (CaseExpr *) node;
    3498              :                 CaseExpr   *newcase;
    3499              :                 Node       *save_case_val;
    3500              :                 Node       *newarg;
    3501              :                 List       *newargs;
    3502              :                 bool        const_true_cond;
    3503        28163 :                 Node       *defresult = NULL;
    3504              :                 ListCell   *arg;
    3505              : 
    3506              :                 /* Simplify the test expression, if any */
    3507        28163 :                 newarg = eval_const_expressions_mutator((Node *) caseexpr->arg,
    3508              :                                                         context);
    3509              : 
    3510              :                 /* Set up for contained CaseTestExpr nodes */
    3511        28163 :                 save_case_val = context->case_val;
    3512        28163 :                 if (newarg && IsA(newarg, Const))
    3513              :                 {
    3514           65 :                     context->case_val = newarg;
    3515           65 :                     newarg = NULL;  /* not needed anymore, see above */
    3516              :                 }
    3517              :                 else
    3518        28098 :                     context->case_val = NULL;
    3519              : 
    3520              :                 /* Simplify the WHEN clauses */
    3521        28163 :                 newargs = NIL;
    3522        28163 :                 const_true_cond = false;
    3523        86304 :                 foreach(arg, caseexpr->args)
    3524              :                 {
    3525        58562 :                     CaseWhen   *oldcasewhen = lfirst_node(CaseWhen, arg);
    3526              :                     Node       *casecond;
    3527              :                     Node       *caseresult;
    3528              : 
    3529              :                     /* Simplify this alternative's test condition */
    3530        58562 :                     casecond = eval_const_expressions_mutator((Node *) oldcasewhen->expr,
    3531              :                                                               context);
    3532              : 
    3533              :                     /*
    3534              :                      * If the test condition is constant FALSE (or NULL), then
    3535              :                      * drop this WHEN clause completely, without processing
    3536              :                      * the result.
    3537              :                      */
    3538        58562 :                     if (casecond && IsA(casecond, Const))
    3539              :                     {
    3540          880 :                         Const      *const_input = (Const *) casecond;
    3541              : 
    3542          880 :                         if (const_input->constisnull ||
    3543          880 :                             !DatumGetBool(const_input->constvalue))
    3544          463 :                             continue;   /* drop alternative with FALSE cond */
    3545              :                         /* Else it's constant TRUE */
    3546          417 :                         const_true_cond = true;
    3547              :                     }
    3548              : 
    3549              :                     /* Simplify this alternative's result value */
    3550        58099 :                     caseresult = eval_const_expressions_mutator((Node *) oldcasewhen->result,
    3551              :                                                                 context);
    3552              : 
    3553              :                     /* If non-constant test condition, emit a new WHEN node */
    3554        58095 :                     if (!const_true_cond)
    3555        57678 :                     {
    3556        57678 :                         CaseWhen   *newcasewhen = makeNode(CaseWhen);
    3557              : 
    3558        57678 :                         newcasewhen->expr = (Expr *) casecond;
    3559        57678 :                         newcasewhen->result = (Expr *) caseresult;
    3560        57678 :                         newcasewhen->location = oldcasewhen->location;
    3561        57678 :                         newargs = lappend(newargs, newcasewhen);
    3562        57678 :                         continue;
    3563              :                     }
    3564              : 
    3565              :                     /*
    3566              :                      * Found a TRUE condition, so none of the remaining
    3567              :                      * alternatives can be reached.  We treat the result as
    3568              :                      * the default result.
    3569              :                      */
    3570          417 :                     defresult = caseresult;
    3571          417 :                     break;
    3572              :                 }
    3573              : 
    3574              :                 /* Simplify the default result, unless we replaced it above */
    3575        28159 :                 if (!const_true_cond)
    3576        27742 :                     defresult = eval_const_expressions_mutator((Node *) caseexpr->defresult,
    3577              :                                                                context);
    3578              : 
    3579        28159 :                 context->case_val = save_case_val;
    3580              : 
    3581              :                 /*
    3582              :                  * If no non-FALSE alternatives, CASE reduces to the default
    3583              :                  * result
    3584              :                  */
    3585        28159 :                 if (newargs == NIL)
    3586          633 :                     return defresult;
    3587              :                 /* Otherwise we need a new CASE node */
    3588        27526 :                 newcase = makeNode(CaseExpr);
    3589        27526 :                 newcase->casetype = caseexpr->casetype;
    3590        27526 :                 newcase->casecollid = caseexpr->casecollid;
    3591        27526 :                 newcase->arg = (Expr *) newarg;
    3592        27526 :                 newcase->args = newargs;
    3593        27526 :                 newcase->defresult = (Expr *) defresult;
    3594        27526 :                 newcase->location = caseexpr->location;
    3595        27526 :                 return (Node *) newcase;
    3596              :             }
    3597        28187 :         case T_CaseTestExpr:
    3598              :             {
    3599              :                 /*
    3600              :                  * If we know a constant test value for the current CASE
    3601              :                  * construct, substitute it for the placeholder.  Else just
    3602              :                  * return the placeholder as-is.
    3603              :                  */
    3604        28187 :                 if (context->case_val)
    3605          119 :                     return copyObject(context->case_val);
    3606              :                 else
    3607        28068 :                     return copyObject(node);
    3608              :             }
    3609        48542 :         case T_SubscriptingRef:
    3610              :         case T_ArrayExpr:
    3611              :         case T_RowExpr:
    3612              :         case T_MinMaxExpr:
    3613              :             {
    3614              :                 /*
    3615              :                  * Generic handling for node types whose own processing is
    3616              :                  * known to be immutable, and for which we need no smarts
    3617              :                  * beyond "simplify if all inputs are constants".
    3618              :                  *
    3619              :                  * Treating SubscriptingRef this way assumes that subscripting
    3620              :                  * fetch and assignment are both immutable.  This constrains
    3621              :                  * type-specific subscripting implementations; maybe we should
    3622              :                  * relax it someday.
    3623              :                  *
    3624              :                  * Treating MinMaxExpr this way amounts to assuming that the
    3625              :                  * btree comparison function it calls is immutable; see the
    3626              :                  * reasoning in contain_mutable_functions_walker.
    3627              :                  */
    3628              : 
    3629              :                 /* Copy the node and const-simplify its arguments */
    3630        48542 :                 node = ece_generic_processing(node);
    3631              :                 /* If all arguments are Consts, we can fold to a constant */
    3632        48542 :                 if (ece_all_arguments_const(node))
    3633        23318 :                     return ece_evaluate_expr(node);
    3634        25224 :                 return node;
    3635              :             }
    3636         2456 :         case T_CoalesceExpr:
    3637              :             {
    3638         2456 :                 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
    3639              :                 CoalesceExpr *newcoalesce;
    3640              :                 List       *newargs;
    3641              :                 ListCell   *arg;
    3642              : 
    3643         2456 :                 newargs = NIL;
    3644         5856 :                 foreach(arg, coalesceexpr->args)
    3645              :                 {
    3646              :                     Node       *e;
    3647              : 
    3648         4820 :                     e = eval_const_expressions_mutator((Node *) lfirst(arg),
    3649              :                                                        context);
    3650              : 
    3651              :                     /*
    3652              :                      * We can remove null constants from the list.  For a
    3653              :                      * nonnullable expression, if it has not been preceded by
    3654              :                      * any non-null-constant expressions then it is the
    3655              :                      * result.  Otherwise, it's the next argument, but we can
    3656              :                      * drop following arguments since they will never be
    3657              :                      * reached.
    3658              :                      */
    3659         4820 :                     if (IsA(e, Const))
    3660              :                     {
    3661         1386 :                         if (((Const *) e)->constisnull)
    3662           46 :                             continue;   /* drop null constant */
    3663         1340 :                         if (newargs == NIL)
    3664          133 :                             return e;   /* first expr */
    3665         1257 :                         newargs = lappend(newargs, e);
    3666         1257 :                         break;
    3667              :                     }
    3668         3434 :                     if (expr_is_nonnullable(context->root, (Expr *) e,
    3669              :                                             NOTNULL_SOURCE_HASHTABLE))
    3670              :                     {
    3671           80 :                         if (newargs == NIL)
    3672           50 :                             return e;   /* first expr */
    3673           30 :                         newargs = lappend(newargs, e);
    3674           30 :                         break;
    3675              :                     }
    3676              : 
    3677         3354 :                     newargs = lappend(newargs, e);
    3678              :                 }
    3679              : 
    3680              :                 /*
    3681              :                  * If all the arguments were constant null, the result is just
    3682              :                  * null
    3683              :                  */
    3684         2323 :                 if (newargs == NIL)
    3685            0 :                     return (Node *) makeNullConst(coalesceexpr->coalescetype,
    3686              :                                                   -1,
    3687              :                                                   coalesceexpr->coalescecollid);
    3688              : 
    3689              :                 /*
    3690              :                  * If there's exactly one surviving argument, we no longer
    3691              :                  * need COALESCE at all: the result is that argument
    3692              :                  */
    3693         2323 :                 if (list_length(newargs) == 1)
    3694           15 :                     return (Node *) linitial(newargs);
    3695              : 
    3696         2308 :                 newcoalesce = makeNode(CoalesceExpr);
    3697         2308 :                 newcoalesce->coalescetype = coalesceexpr->coalescetype;
    3698         2308 :                 newcoalesce->coalescecollid = coalesceexpr->coalescecollid;
    3699         2308 :                 newcoalesce->args = newargs;
    3700         2308 :                 newcoalesce->location = coalesceexpr->location;
    3701         2308 :                 return (Node *) newcoalesce;
    3702              :             }
    3703         4107 :         case T_SQLValueFunction:
    3704              :             {
    3705              :                 /*
    3706              :                  * All variants of SQLValueFunction are stable, so if we are
    3707              :                  * estimating the expression's value, we should evaluate the
    3708              :                  * current function value.  Otherwise just copy.
    3709              :                  */
    3710         4107 :                 SQLValueFunction *svf = (SQLValueFunction *) node;
    3711              : 
    3712         4107 :                 if (context->estimate)
    3713          742 :                     return (Node *) evaluate_expr((Expr *) svf,
    3714              :                                                   svf->type,
    3715              :                                                   svf->typmod,
    3716              :                                                   InvalidOid);
    3717              :                 else
    3718         3365 :                     return copyObject((Node *) svf);
    3719              :             }
    3720        24843 :         case T_FieldSelect:
    3721              :             {
    3722              :                 /*
    3723              :                  * We can optimize field selection from a whole-row Var into a
    3724              :                  * simple Var.  (This case won't be generated directly by the
    3725              :                  * parser, because ParseComplexProjection short-circuits it.
    3726              :                  * But it can arise while simplifying functions.)  Also, we
    3727              :                  * can optimize field selection from a RowExpr construct, or
    3728              :                  * of course from a constant.
    3729              :                  *
    3730              :                  * However, replacing a whole-row Var in this way has a
    3731              :                  * pitfall: if we've already built the rel targetlist for the
    3732              :                  * source relation, then the whole-row Var is scheduled to be
    3733              :                  * produced by the relation scan, but the simple Var probably
    3734              :                  * isn't, which will lead to a failure in setrefs.c.  This is
    3735              :                  * not a problem when handling simple single-level queries, in
    3736              :                  * which expression simplification always happens first.  It
    3737              :                  * is a risk for lateral references from subqueries, though.
    3738              :                  * To avoid such failures, don't optimize uplevel references.
    3739              :                  *
    3740              :                  * We must also check that the declared type of the field is
    3741              :                  * still the same as when the FieldSelect was created --- this
    3742              :                  * can change if someone did ALTER COLUMN TYPE on the rowtype.
    3743              :                  * If it isn't, we skip the optimization; the case will
    3744              :                  * probably fail at runtime, but that's not our problem here.
    3745              :                  */
    3746        24843 :                 FieldSelect *fselect = (FieldSelect *) node;
    3747              :                 FieldSelect *newfselect;
    3748              :                 Node       *arg;
    3749              : 
    3750        24843 :                 arg = eval_const_expressions_mutator((Node *) fselect->arg,
    3751              :                                                      context);
    3752        24843 :                 if (arg && IsA(arg, Var) &&
    3753        21924 :                     ((Var *) arg)->varattno == InvalidAttrNumber &&
    3754           75 :                     ((Var *) arg)->varlevelsup == 0)
    3755              :                 {
    3756           65 :                     if (rowtype_field_matches(((Var *) arg)->vartype,
    3757           65 :                                               fselect->fieldnum,
    3758              :                                               fselect->resulttype,
    3759              :                                               fselect->resulttypmod,
    3760              :                                               fselect->resultcollid))
    3761              :                     {
    3762              :                         Var        *newvar;
    3763              : 
    3764           65 :                         newvar = makeVar(((Var *) arg)->varno,
    3765           65 :                                          fselect->fieldnum,
    3766              :                                          fselect->resulttype,
    3767              :                                          fselect->resulttypmod,
    3768              :                                          fselect->resultcollid,
    3769              :                                          ((Var *) arg)->varlevelsup);
    3770              :                         /* New Var has same OLD/NEW returning as old one */
    3771           65 :                         newvar->varreturningtype = ((Var *) arg)->varreturningtype;
    3772              :                         /* New Var is nullable by same rels as the old one */
    3773           65 :                         newvar->varnullingrels = ((Var *) arg)->varnullingrels;
    3774           65 :                         return (Node *) newvar;
    3775              :                     }
    3776              :                 }
    3777        24778 :                 if (arg && IsA(arg, RowExpr))
    3778              :                 {
    3779           20 :                     RowExpr    *rowexpr = (RowExpr *) arg;
    3780              : 
    3781           40 :                     if (fselect->fieldnum > 0 &&
    3782           20 :                         fselect->fieldnum <= list_length(rowexpr->args))
    3783              :                     {
    3784           20 :                         Node       *fld = (Node *) list_nth(rowexpr->args,
    3785           20 :                                                             fselect->fieldnum - 1);
    3786              : 
    3787           20 :                         if (rowtype_field_matches(rowexpr->row_typeid,
    3788           20 :                                                   fselect->fieldnum,
    3789              :                                                   fselect->resulttype,
    3790              :                                                   fselect->resulttypmod,
    3791           20 :                                                   fselect->resultcollid) &&
    3792           40 :                             fselect->resulttype == exprType(fld) &&
    3793           40 :                             fselect->resulttypmod == exprTypmod(fld) &&
    3794           20 :                             fselect->resultcollid == exprCollation(fld))
    3795           20 :                             return fld;
    3796              :                     }
    3797              :                 }
    3798        24758 :                 newfselect = makeNode(FieldSelect);
    3799        24758 :                 newfselect->arg = (Expr *) arg;
    3800        24758 :                 newfselect->fieldnum = fselect->fieldnum;
    3801        24758 :                 newfselect->resulttype = fselect->resulttype;
    3802        24758 :                 newfselect->resulttypmod = fselect->resulttypmod;
    3803        24758 :                 newfselect->resultcollid = fselect->resultcollid;
    3804        24758 :                 if (arg && IsA(arg, Const))
    3805              :                 {
    3806          509 :                     Const      *con = (Const *) arg;
    3807              : 
    3808          509 :                     if (rowtype_field_matches(con->consttype,
    3809          509 :                                               newfselect->fieldnum,
    3810              :                                               newfselect->resulttype,
    3811              :                                               newfselect->resulttypmod,
    3812              :                                               newfselect->resultcollid))
    3813          509 :                         return ece_evaluate_expr(newfselect);
    3814              :                 }
    3815        24249 :                 return (Node *) newfselect;
    3816              :             }
    3817        28132 :         case T_NullTest:
    3818              :             {
    3819        28132 :                 NullTest   *ntest = (NullTest *) node;
    3820              :                 NullTest   *newntest;
    3821              :                 Node       *arg;
    3822              : 
    3823        28132 :                 arg = eval_const_expressions_mutator((Node *) ntest->arg,
    3824              :                                                      context);
    3825        28131 :                 if (ntest->argisrow && arg && IsA(arg, RowExpr))
    3826              :                 {
    3827              :                     /*
    3828              :                      * We break ROW(...) IS [NOT] NULL into separate tests on
    3829              :                      * its component fields.  This form is usually more
    3830              :                      * efficient to evaluate, as well as being more amenable
    3831              :                      * to optimization.
    3832              :                      */
    3833           37 :                     RowExpr    *rarg = (RowExpr *) arg;
    3834           37 :                     List       *newargs = NIL;
    3835              :                     ListCell   *l;
    3836              : 
    3837          136 :                     foreach(l, rarg->args)
    3838              :                     {
    3839           99 :                         Node       *relem = (Node *) lfirst(l);
    3840              : 
    3841              :                         /*
    3842              :                          * A constant field refutes the whole NullTest if it's
    3843              :                          * of the wrong nullness; else we can discard it.
    3844              :                          */
    3845           99 :                         if (relem && IsA(relem, Const))
    3846            0 :                         {
    3847            0 :                             Const      *carg = (Const *) relem;
    3848              : 
    3849            0 :                             if (carg->constisnull ?
    3850            0 :                                 (ntest->nulltesttype == IS_NOT_NULL) :
    3851            0 :                                 (ntest->nulltesttype == IS_NULL))
    3852            0 :                                 return makeBoolConst(false, false);
    3853            0 :                             continue;
    3854              :                         }
    3855              : 
    3856              :                         /*
    3857              :                          * A proven non-nullable field refutes the whole
    3858              :                          * NullTest if the test is IS NULL; else we can
    3859              :                          * discard it.
    3860              :                          */
    3861          198 :                         if (relem &&
    3862           99 :                             expr_is_nonnullable(context->root, (Expr *) relem,
    3863              :                                                 NOTNULL_SOURCE_HASHTABLE))
    3864              :                         {
    3865            0 :                             if (ntest->nulltesttype == IS_NULL)
    3866            0 :                                 return makeBoolConst(false, false);
    3867            0 :                             continue;
    3868              :                         }
    3869              : 
    3870              :                         /*
    3871              :                          * Else, make a scalar (argisrow == false) NullTest
    3872              :                          * for this field.  Scalar semantics are required
    3873              :                          * because IS [NOT] NULL doesn't recurse; see comments
    3874              :                          * in ExecEvalRowNullInt().
    3875              :                          */
    3876           99 :                         newntest = makeNode(NullTest);
    3877           99 :                         newntest->arg = (Expr *) relem;
    3878           99 :                         newntest->nulltesttype = ntest->nulltesttype;
    3879           99 :                         newntest->argisrow = false;
    3880           99 :                         newntest->location = ntest->location;
    3881           99 :                         newargs = lappend(newargs, newntest);
    3882              :                     }
    3883              :                     /* If all the inputs were constants, result is TRUE */
    3884           37 :                     if (newargs == NIL)
    3885            0 :                         return makeBoolConst(true, false);
    3886              :                     /* If only one nonconst input, it's the result */
    3887           37 :                     if (list_length(newargs) == 1)
    3888            0 :                         return (Node *) linitial(newargs);
    3889              :                     /* Else we need an AND node */
    3890           37 :                     return (Node *) make_andclause(newargs);
    3891              :                 }
    3892        28094 :                 if (!ntest->argisrow && arg && IsA(arg, Const))
    3893              :                 {
    3894          281 :                     Const      *carg = (Const *) arg;
    3895              :                     bool        result;
    3896              : 
    3897          281 :                     switch (ntest->nulltesttype)
    3898              :                     {
    3899          238 :                         case IS_NULL:
    3900          238 :                             result = carg->constisnull;
    3901          238 :                             break;
    3902           43 :                         case IS_NOT_NULL:
    3903           43 :                             result = !carg->constisnull;
    3904           43 :                             break;
    3905            0 :                         default:
    3906            0 :                             elog(ERROR, "unrecognized nulltesttype: %d",
    3907              :                                  (int) ntest->nulltesttype);
    3908              :                             result = false; /* keep compiler quiet */
    3909              :                             break;
    3910              :                     }
    3911              : 
    3912          281 :                     return makeBoolConst(result, false);
    3913              :                 }
    3914        55284 :                 if (!ntest->argisrow && arg &&
    3915        27471 :                     expr_is_nonnullable(context->root, (Expr *) arg,
    3916              :                                         NOTNULL_SOURCE_HASHTABLE))
    3917              :                 {
    3918              :                     bool        result;
    3919              : 
    3920          536 :                     switch (ntest->nulltesttype)
    3921              :                     {
    3922          128 :                         case IS_NULL:
    3923          128 :                             result = false;
    3924          128 :                             break;
    3925          408 :                         case IS_NOT_NULL:
    3926          408 :                             result = true;
    3927          408 :                             break;
    3928            0 :                         default:
    3929            0 :                             elog(ERROR, "unrecognized nulltesttype: %d",
    3930              :                                  (int) ntest->nulltesttype);
    3931              :                             result = false; /* keep compiler quiet */
    3932              :                             break;
    3933              :                     }
    3934              : 
    3935          536 :                     return makeBoolConst(result, false);
    3936              :                 }
    3937              : 
    3938        27277 :                 newntest = makeNode(NullTest);
    3939        27277 :                 newntest->arg = (Expr *) arg;
    3940        27277 :                 newntest->nulltesttype = ntest->nulltesttype;
    3941        27277 :                 newntest->argisrow = ntest->argisrow;
    3942        27277 :                 newntest->location = ntest->location;
    3943        27277 :                 return (Node *) newntest;
    3944              :             }
    3945         1689 :         case T_BooleanTest:
    3946              :             {
    3947              :                 /*
    3948              :                  * This case could be folded into the generic handling used
    3949              :                  * for ArrayExpr etc.  But because the simplification logic is
    3950              :                  * so trivial, applying evaluate_expr() to perform it would be
    3951              :                  * a heavy overhead.  BooleanTest is probably common enough to
    3952              :                  * justify keeping this bespoke implementation.
    3953              :                  */
    3954         1689 :                 BooleanTest *btest = (BooleanTest *) node;
    3955              :                 BooleanTest *newbtest;
    3956              :                 Node       *arg;
    3957              : 
    3958         1689 :                 arg = eval_const_expressions_mutator((Node *) btest->arg,
    3959              :                                                      context);
    3960         1689 :                 if (arg && IsA(arg, Const))
    3961              :                 {
    3962              :                     /*
    3963              :                      * If arg is Const, simplify to constant.
    3964              :                      */
    3965          195 :                     Const      *carg = (Const *) arg;
    3966              :                     bool        result;
    3967              : 
    3968          195 :                     switch (btest->booltesttype)
    3969              :                     {
    3970            0 :                         case IS_TRUE:
    3971            0 :                             result = (!carg->constisnull &&
    3972            0 :                                       DatumGetBool(carg->constvalue));
    3973            0 :                             break;
    3974          195 :                         case IS_NOT_TRUE:
    3975          390 :                             result = (carg->constisnull ||
    3976          195 :                                       !DatumGetBool(carg->constvalue));
    3977          195 :                             break;
    3978            0 :                         case IS_FALSE:
    3979            0 :                             result = (!carg->constisnull &&
    3980            0 :                                       !DatumGetBool(carg->constvalue));
    3981            0 :                             break;
    3982            0 :                         case IS_NOT_FALSE:
    3983            0 :                             result = (carg->constisnull ||
    3984            0 :                                       DatumGetBool(carg->constvalue));
    3985            0 :                             break;
    3986            0 :                         case IS_UNKNOWN:
    3987            0 :                             result = carg->constisnull;
    3988            0 :                             break;
    3989            0 :                         case IS_NOT_UNKNOWN:
    3990            0 :                             result = !carg->constisnull;
    3991            0 :                             break;
    3992            0 :                         default:
    3993            0 :                             elog(ERROR, "unrecognized booltesttype: %d",
    3994              :                                  (int) btest->booltesttype);
    3995              :                             result = false; /* keep compiler quiet */
    3996              :                             break;
    3997              :                     }
    3998              : 
    3999          195 :                     return makeBoolConst(result, false);
    4000              :                 }
    4001         2988 :                 if (arg &&
    4002         1494 :                     expr_is_nonnullable(context->root, (Expr *) arg,
    4003              :                                         NOTNULL_SOURCE_HASHTABLE))
    4004              :                 {
    4005              :                     /*
    4006              :                      * If arg is proven non-nullable, simplify to boolean
    4007              :                      * expression or constant.
    4008              :                      */
    4009           67 :                     switch (btest->booltesttype)
    4010              :                     {
    4011           20 :                         case IS_TRUE:
    4012              :                         case IS_NOT_FALSE:
    4013           20 :                             return arg;
    4014              : 
    4015           27 :                         case IS_FALSE:
    4016              :                         case IS_NOT_TRUE:
    4017           27 :                             return (Node *) make_notclause((Expr *) arg);
    4018              : 
    4019           10 :                         case IS_UNKNOWN:
    4020           10 :                             return makeBoolConst(false, false);
    4021              : 
    4022           10 :                         case IS_NOT_UNKNOWN:
    4023           10 :                             return makeBoolConst(true, false);
    4024              : 
    4025            0 :                         default:
    4026            0 :                             elog(ERROR, "unrecognized booltesttype: %d",
    4027              :                                  (int) btest->booltesttype);
    4028              :                             break;
    4029              :                     }
    4030              :                 }
    4031              : 
    4032         1427 :                 newbtest = makeNode(BooleanTest);
    4033         1427 :                 newbtest->arg = (Expr *) arg;
    4034         1427 :                 newbtest->booltesttype = btest->booltesttype;
    4035         1427 :                 newbtest->location = btest->location;
    4036         1427 :                 return (Node *) newbtest;
    4037              :             }
    4038        18529 :         case T_CoerceToDomain:
    4039              :             {
    4040              :                 /*
    4041              :                  * If the domain currently has no constraints, we replace the
    4042              :                  * CoerceToDomain node with a simple RelabelType, which is
    4043              :                  * both far faster to execute and more amenable to later
    4044              :                  * optimization.  We must then mark the plan as needing to be
    4045              :                  * rebuilt if the domain's constraints change.
    4046              :                  *
    4047              :                  * Also, in estimation mode, always replace CoerceToDomain
    4048              :                  * nodes, effectively assuming that the coercion will succeed.
    4049              :                  */
    4050        18529 :                 CoerceToDomain *cdomain = (CoerceToDomain *) node;
    4051              :                 CoerceToDomain *newcdomain;
    4052              :                 Node       *arg;
    4053              : 
    4054        18529 :                 arg = eval_const_expressions_mutator((Node *) cdomain->arg,
    4055              :                                                      context);
    4056        18509 :                 if (context->estimate ||
    4057        18469 :                     !DomainHasConstraints(cdomain->resulttype, NULL))
    4058              :                 {
    4059              :                     /* Record dependency, if this isn't estimation mode */
    4060        12273 :                     if (context->root && !context->estimate)
    4061        12189 :                         record_plan_type_dependency(context->root,
    4062              :                                                     cdomain->resulttype);
    4063              : 
    4064              :                     /* Generate RelabelType to substitute for CoerceToDomain */
    4065        12273 :                     return applyRelabelType(arg,
    4066              :                                             cdomain->resulttype,
    4067              :                                             cdomain->resulttypmod,
    4068              :                                             cdomain->resultcollid,
    4069              :                                             cdomain->coercionformat,
    4070              :                                             cdomain->location,
    4071              :                                             true);
    4072              :                 }
    4073              : 
    4074         6236 :                 newcdomain = makeNode(CoerceToDomain);
    4075         6236 :                 newcdomain->arg = (Expr *) arg;
    4076         6236 :                 newcdomain->resulttype = cdomain->resulttype;
    4077         6236 :                 newcdomain->resulttypmod = cdomain->resulttypmod;
    4078         6236 :                 newcdomain->resultcollid = cdomain->resultcollid;
    4079         6236 :                 newcdomain->coercionformat = cdomain->coercionformat;
    4080         6236 :                 newcdomain->location = cdomain->location;
    4081         6236 :                 return (Node *) newcdomain;
    4082              :             }
    4083         3907 :         case T_PlaceHolderVar:
    4084              : 
    4085              :             /*
    4086              :              * In estimation mode, just strip the PlaceHolderVar node
    4087              :              * altogether; this amounts to estimating that the contained value
    4088              :              * won't be forced to null by an outer join.  In regular mode we
    4089              :              * just use the default behavior (ie, simplify the expression but
    4090              :              * leave the PlaceHolderVar node intact).
    4091              :              */
    4092         3907 :             if (context->estimate)
    4093              :             {
    4094          710 :                 PlaceHolderVar *phv = (PlaceHolderVar *) node;
    4095              : 
    4096          710 :                 return eval_const_expressions_mutator((Node *) phv->phexpr,
    4097              :                                                       context);
    4098              :             }
    4099         3197 :             break;
    4100           75 :         case T_ConvertRowtypeExpr:
    4101              :             {
    4102           75 :                 ConvertRowtypeExpr *cre = castNode(ConvertRowtypeExpr, node);
    4103              :                 Node       *arg;
    4104              :                 ConvertRowtypeExpr *newcre;
    4105              : 
    4106           75 :                 arg = eval_const_expressions_mutator((Node *) cre->arg,
    4107              :                                                      context);
    4108              : 
    4109           75 :                 newcre = makeNode(ConvertRowtypeExpr);
    4110           75 :                 newcre->resulttype = cre->resulttype;
    4111           75 :                 newcre->convertformat = cre->convertformat;
    4112           75 :                 newcre->location = cre->location;
    4113              : 
    4114              :                 /*
    4115              :                  * In case of a nested ConvertRowtypeExpr, we can convert the
    4116              :                  * leaf row directly to the topmost row format without any
    4117              :                  * intermediate conversions. (This works because
    4118              :                  * ConvertRowtypeExpr is used only for child->parent
    4119              :                  * conversion in inheritance trees, which works by exact match
    4120              :                  * of column name, and a column absent in an intermediate
    4121              :                  * result can't be present in the final result.)
    4122              :                  *
    4123              :                  * No need to check more than one level deep, because the
    4124              :                  * above recursion will have flattened anything else.
    4125              :                  */
    4126           75 :                 if (arg != NULL && IsA(arg, ConvertRowtypeExpr))
    4127              :                 {
    4128           10 :                     ConvertRowtypeExpr *argcre = (ConvertRowtypeExpr *) arg;
    4129              : 
    4130           10 :                     arg = (Node *) argcre->arg;
    4131              : 
    4132              :                     /*
    4133              :                      * Make sure an outer implicit conversion can't hide an
    4134              :                      * inner explicit one.
    4135              :                      */
    4136           10 :                     if (newcre->convertformat == COERCE_IMPLICIT_CAST)
    4137            0 :                         newcre->convertformat = argcre->convertformat;
    4138              :                 }
    4139              : 
    4140           75 :                 newcre->arg = (Expr *) arg;
    4141              : 
    4142           75 :                 if (arg != NULL && IsA(arg, Const))
    4143           15 :                     return ece_evaluate_expr((Node *) newcre);
    4144           60 :                 return (Node *) newcre;
    4145              :             }
    4146      5204036 :         default:
    4147      5204036 :             break;
    4148              :     }
    4149              : 
    4150              :     /*
    4151              :      * For any node type not handled above, copy the node unchanged but
    4152              :      * const-simplify its subexpressions.  This is the correct thing for node
    4153              :      * types whose behavior might change between planning and execution, such
    4154              :      * as CurrentOfExpr.  It's also a safe default for new node types not
    4155              :      * known to this routine.
    4156              :      */
    4157      5208576 :     return ece_generic_processing(node);
    4158              : }
    4159              : 
    4160              : /*
    4161              :  * Subroutine for eval_const_expressions: check for non-Const nodes.
    4162              :  *
    4163              :  * We can abort recursion immediately on finding a non-Const node.  This is
    4164              :  * critical for performance, else eval_const_expressions_mutator would take
    4165              :  * O(N^2) time on non-simplifiable trees.  However, we do need to descend
    4166              :  * into List nodes since expression_tree_walker sometimes invokes the walker
    4167              :  * function directly on List subtrees.
    4168              :  */
    4169              : static bool
    4170       167851 : contain_non_const_walker(Node *node, void *context)
    4171              : {
    4172       167851 :     if (node == NULL)
    4173          596 :         return false;
    4174       167255 :     if (IsA(node, Const))
    4175        86391 :         return false;
    4176        80864 :     if (IsA(node, List))
    4177        26556 :         return expression_tree_walker(node, contain_non_const_walker, context);
    4178              :     /* Otherwise, abort the tree traversal and return true */
    4179        54308 :     return true;
    4180              : }
    4181              : 
    4182              : /*
    4183              :  * Subroutine for eval_const_expressions: check if a function is OK to evaluate
    4184              :  */
    4185              : static bool
    4186          300 : ece_function_is_safe(Oid funcid, eval_const_expressions_context *context)
    4187              : {
    4188          300 :     char        provolatile = func_volatile(funcid);
    4189              : 
    4190              :     /*
    4191              :      * Ordinarily we are only allowed to simplify immutable functions. But for
    4192              :      * purposes of estimation, we consider it okay to simplify functions that
    4193              :      * are merely stable; the risk that the result might change from planning
    4194              :      * time to execution time is worth taking in preference to not being able
    4195              :      * to estimate the value at all.
    4196              :      */
    4197          300 :     if (provolatile == PROVOLATILE_IMMUTABLE)
    4198          300 :         return true;
    4199            0 :     if (context->estimate && provolatile == PROVOLATILE_STABLE)
    4200            0 :         return true;
    4201            0 :     return false;
    4202              : }
    4203              : 
    4204              : /*
    4205              :  * Subroutine for eval_const_expressions: process arguments of an OR clause
    4206              :  *
    4207              :  * This includes flattening of nested ORs as well as recursion to
    4208              :  * eval_const_expressions to simplify the OR arguments.
    4209              :  *
    4210              :  * After simplification, OR arguments are handled as follows:
    4211              :  *      non constant: keep
    4212              :  *      FALSE: drop (does not affect result)
    4213              :  *      TRUE: force result to TRUE
    4214              :  *      NULL: keep only one
    4215              :  * We must keep one NULL input because OR expressions evaluate to NULL when no
    4216              :  * input is TRUE and at least one is NULL.  We don't actually include the NULL
    4217              :  * here, that's supposed to be done by the caller.
    4218              :  *
    4219              :  * The output arguments *haveNull and *forceTrue must be initialized false
    4220              :  * by the caller.  They will be set true if a NULL constant or TRUE constant,
    4221              :  * respectively, is detected anywhere in the argument list.
    4222              :  */
    4223              : static List *
    4224        15328 : simplify_or_arguments(List *args,
    4225              :                       eval_const_expressions_context *context,
    4226              :                       bool *haveNull, bool *forceTrue)
    4227              : {
    4228        15328 :     List       *newargs = NIL;
    4229              :     List       *unprocessed_args;
    4230              : 
    4231              :     /*
    4232              :      * We want to ensure that any OR immediately beneath another OR gets
    4233              :      * flattened into a single OR-list, so as to simplify later reasoning.
    4234              :      *
    4235              :      * To avoid stack overflow from recursion of eval_const_expressions, we
    4236              :      * resort to some tenseness here: we keep a list of not-yet-processed
    4237              :      * inputs, and handle flattening of nested ORs by prepending to the to-do
    4238              :      * list instead of recursing.  Now that the parser generates N-argument
    4239              :      * ORs from simple lists, this complexity is probably less necessary than
    4240              :      * it once was, but we might as well keep the logic.
    4241              :      */
    4242        15328 :     unprocessed_args = list_copy(args);
    4243        49684 :     while (unprocessed_args)
    4244              :     {
    4245        34462 :         Node       *arg = (Node *) linitial(unprocessed_args);
    4246              : 
    4247        34462 :         unprocessed_args = list_delete_first(unprocessed_args);
    4248              : 
    4249              :         /* flatten nested ORs as per above comment */
    4250        34462 :         if (is_orclause(arg))
    4251            7 :         {
    4252            7 :             List       *subargs = ((BoolExpr *) arg)->args;
    4253            7 :             List       *oldlist = unprocessed_args;
    4254              : 
    4255            7 :             unprocessed_args = list_concat_copy(subargs, unprocessed_args);
    4256              :             /* perhaps-overly-tense code to avoid leaking old lists */
    4257            7 :             list_free(oldlist);
    4258            7 :             continue;
    4259              :         }
    4260              : 
    4261              :         /* If it's not an OR, simplify it */
    4262        34455 :         arg = eval_const_expressions_mutator(arg, context);
    4263              : 
    4264              :         /*
    4265              :          * It is unlikely but not impossible for simplification of a non-OR
    4266              :          * clause to produce an OR.  Recheck, but don't be too tense about it
    4267              :          * since it's not a mainstream case.  In particular we don't worry
    4268              :          * about const-simplifying the input twice, nor about list leakage.
    4269              :          */
    4270        34455 :         if (is_orclause(arg))
    4271            0 :         {
    4272            0 :             List       *subargs = ((BoolExpr *) arg)->args;
    4273              : 
    4274            0 :             unprocessed_args = list_concat_copy(subargs, unprocessed_args);
    4275            0 :             continue;
    4276              :         }
    4277              : 
    4278              :         /*
    4279              :          * OK, we have a const-simplified non-OR argument.  Process it per
    4280              :          * comments above.
    4281              :          */
    4282        34455 :         if (IsA(arg, Const))
    4283         3959 :         {
    4284         4065 :             Const      *const_input = (Const *) arg;
    4285              : 
    4286         4065 :             if (const_input->constisnull)
    4287         3813 :                 *haveNull = true;
    4288          252 :             else if (DatumGetBool(const_input->constvalue))
    4289              :             {
    4290          106 :                 *forceTrue = true;
    4291              : 
    4292              :                 /*
    4293              :                  * Once we detect a TRUE result we can just exit the loop
    4294              :                  * immediately.  However, if we ever add a notion of
    4295              :                  * non-removable functions, we'd need to keep scanning.
    4296              :                  */
    4297          106 :                 return NIL;
    4298              :             }
    4299              :             /* otherwise, we can drop the constant-false input */
    4300         3959 :             continue;
    4301              :         }
    4302              : 
    4303              :         /* else emit the simplified arg into the result list */
    4304        30390 :         newargs = lappend(newargs, arg);
    4305              :     }
    4306              : 
    4307        15222 :     return newargs;
    4308              : }
    4309              : 
    4310              : /*
    4311              :  * Subroutine for eval_const_expressions: process arguments of an AND clause
    4312              :  *
    4313              :  * This includes flattening of nested ANDs as well as recursion to
    4314              :  * eval_const_expressions to simplify the AND arguments.
    4315              :  *
    4316              :  * After simplification, AND arguments are handled as follows:
    4317              :  *      non constant: keep
    4318              :  *      TRUE: drop (does not affect result)
    4319              :  *      FALSE: force result to FALSE
    4320              :  *      NULL: keep only one
    4321              :  * We must keep one NULL input because AND expressions evaluate to NULL when
    4322              :  * no input is FALSE and at least one is NULL.  We don't actually include the
    4323              :  * NULL here, that's supposed to be done by the caller.
    4324              :  *
    4325              :  * The output arguments *haveNull and *forceFalse must be initialized false
    4326              :  * by the caller.  They will be set true if a null constant or false constant,
    4327              :  * respectively, is detected anywhere in the argument list.
    4328              :  */
    4329              : static List *
    4330       116558 : simplify_and_arguments(List *args,
    4331              :                        eval_const_expressions_context *context,
    4332              :                        bool *haveNull, bool *forceFalse)
    4333              : {
    4334       116558 :     List       *newargs = NIL;
    4335              :     List       *unprocessed_args;
    4336              : 
    4337              :     /* See comments in simplify_or_arguments */
    4338       116558 :     unprocessed_args = list_copy(args);
    4339       423746 :     while (unprocessed_args)
    4340              :     {
    4341       307843 :         Node       *arg = (Node *) linitial(unprocessed_args);
    4342              : 
    4343       307843 :         unprocessed_args = list_delete_first(unprocessed_args);
    4344              : 
    4345              :         /* flatten nested ANDs as per above comment */
    4346       307843 :         if (is_andclause(arg))
    4347         4035 :         {
    4348         4035 :             List       *subargs = ((BoolExpr *) arg)->args;
    4349         4035 :             List       *oldlist = unprocessed_args;
    4350              : 
    4351         4035 :             unprocessed_args = list_concat_copy(subargs, unprocessed_args);
    4352              :             /* perhaps-overly-tense code to avoid leaking old lists */
    4353         4035 :             list_free(oldlist);
    4354         4035 :             continue;
    4355              :         }
    4356              : 
    4357              :         /* If it's not an AND, simplify it */
    4358       303808 :         arg = eval_const_expressions_mutator(arg, context);
    4359              : 
    4360              :         /*
    4361              :          * It is unlikely but not impossible for simplification of a non-AND
    4362              :          * clause to produce an AND.  Recheck, but don't be too tense about it
    4363              :          * since it's not a mainstream case.  In particular we don't worry
    4364              :          * about const-simplifying the input twice, nor about list leakage.
    4365              :          */
    4366       303808 :         if (is_andclause(arg))
    4367           30 :         {
    4368           30 :             List       *subargs = ((BoolExpr *) arg)->args;
    4369              : 
    4370           30 :             unprocessed_args = list_concat_copy(subargs, unprocessed_args);
    4371           30 :             continue;
    4372              :         }
    4373              : 
    4374              :         /*
    4375              :          * OK, we have a const-simplified non-AND argument.  Process it per
    4376              :          * comments above.
    4377              :          */
    4378       303778 :         if (IsA(arg, Const))
    4379         1238 :         {
    4380         1893 :             Const      *const_input = (Const *) arg;
    4381              : 
    4382         1893 :             if (const_input->constisnull)
    4383           35 :                 *haveNull = true;
    4384         1858 :             else if (!DatumGetBool(const_input->constvalue))
    4385              :             {
    4386          655 :                 *forceFalse = true;
    4387              : 
    4388              :                 /*
    4389              :                  * Once we detect a FALSE result we can just exit the loop
    4390              :                  * immediately.  However, if we ever add a notion of
    4391              :                  * non-removable functions, we'd need to keep scanning.
    4392              :                  */
    4393          655 :                 return NIL;
    4394              :             }
    4395              :             /* otherwise, we can drop the constant-true input */
    4396         1238 :             continue;
    4397              :         }
    4398              : 
    4399              :         /* else emit the simplified arg into the result list */
    4400       301885 :         newargs = lappend(newargs, arg);
    4401              :     }
    4402              : 
    4403       115903 :     return newargs;
    4404              : }
    4405              : 
    4406              : /*
    4407              :  * Subroutine for eval_const_expressions: try to simplify boolean equality
    4408              :  * or inequality condition
    4409              :  *
    4410              :  * Inputs are the operator OID and the simplified arguments to the operator.
    4411              :  * Returns a simplified expression if successful, or NULL if cannot
    4412              :  * simplify the expression.
    4413              :  *
    4414              :  * The idea here is to reduce "x = true" to "x" and "x = false" to "NOT x",
    4415              :  * or similarly "x <> true" to "NOT x" and "x <> false" to "x".
    4416              :  * This is only marginally useful in itself, but doing it in constant folding
    4417              :  * ensures that we will recognize these forms as being equivalent in, for
    4418              :  * example, partial index matching.
    4419              :  *
    4420              :  * We come here only if simplify_function has failed; therefore we cannot
    4421              :  * see two constant inputs, nor a constant-NULL input.
    4422              :  */
    4423              : static Node *
    4424         1764 : simplify_boolean_equality(Oid opno, List *args)
    4425              : {
    4426              :     Node       *leftop;
    4427              :     Node       *rightop;
    4428              : 
    4429              :     Assert(list_length(args) == 2);
    4430         1764 :     leftop = linitial(args);
    4431         1764 :     rightop = lsecond(args);
    4432         1764 :     if (leftop && IsA(leftop, Const))
    4433              :     {
    4434              :         Assert(!((Const *) leftop)->constisnull);
    4435            0 :         if (opno == BooleanEqualOperator)
    4436              :         {
    4437            0 :             if (DatumGetBool(((Const *) leftop)->constvalue))
    4438            0 :                 return rightop; /* true = foo */
    4439              :             else
    4440            0 :                 return negate_clause(rightop);  /* false = foo */
    4441              :         }
    4442              :         else
    4443              :         {
    4444            0 :             if (DatumGetBool(((Const *) leftop)->constvalue))
    4445            0 :                 return negate_clause(rightop);  /* true <> foo */
    4446              :             else
    4447            0 :                 return rightop; /* false <> foo */
    4448              :         }
    4449              :     }
    4450         1764 :     if (rightop && IsA(rightop, Const))
    4451              :     {
    4452              :         Assert(!((Const *) rightop)->constisnull);
    4453         1279 :         if (opno == BooleanEqualOperator)
    4454              :         {
    4455         1224 :             if (DatumGetBool(((Const *) rightop)->constvalue))
    4456          182 :                 return leftop;  /* foo = true */
    4457              :             else
    4458         1042 :                 return negate_clause(leftop);   /* foo = false */
    4459              :         }
    4460              :         else
    4461              :         {
    4462           55 :             if (DatumGetBool(((Const *) rightop)->constvalue))
    4463           50 :                 return negate_clause(leftop);   /* foo <> true */
    4464              :             else
    4465            5 :                 return leftop;  /* foo <> false */
    4466              :         }
    4467              :     }
    4468          485 :     return NULL;
    4469              : }
    4470              : 
    4471              : /*
    4472              :  * Subroutine for eval_const_expressions: try to simplify a function call
    4473              :  * (which might originally have been an operator; we don't care)
    4474              :  *
    4475              :  * Inputs are the function OID, actual result type OID (which is needed for
    4476              :  * polymorphic functions), result typmod, result collation, the input
    4477              :  * collation to use for the function, the original argument list (not
    4478              :  * const-simplified yet, unless process_args is false), and some flags;
    4479              :  * also the context data for eval_const_expressions.
    4480              :  *
    4481              :  * Returns a simplified expression if successful, or NULL if cannot
    4482              :  * simplify the function call.
    4483              :  *
    4484              :  * This function is also responsible for converting named-notation argument
    4485              :  * lists into positional notation and/or adding any needed default argument
    4486              :  * expressions; which is a bit grotty, but it avoids extra fetches of the
    4487              :  * function's pg_proc tuple.  For this reason, the args list is
    4488              :  * pass-by-reference.  Conversion and const-simplification of the args list
    4489              :  * will be done even if simplification of the function call itself is not
    4490              :  * possible.
    4491              :  */
    4492              : static Expr *
    4493       963406 : simplify_function(Oid funcid, Oid result_type, int32 result_typmod,
    4494              :                   Oid result_collid, Oid input_collid, List **args_p,
    4495              :                   bool funcvariadic, bool process_args, bool allow_non_const,
    4496              :                   eval_const_expressions_context *context)
    4497              : {
    4498       963406 :     List       *args = *args_p;
    4499              :     HeapTuple   func_tuple;
    4500              :     Form_pg_proc func_form;
    4501              :     Expr       *newexpr;
    4502              : 
    4503              :     /*
    4504              :      * We have three strategies for simplification: execute the function to
    4505              :      * deliver a constant result, use a transform function to generate a
    4506              :      * substitute node tree, or expand in-line the body of the function
    4507              :      * definition (which only works for simple SQL-language functions, but
    4508              :      * that is a common case).  Each case needs access to the function's
    4509              :      * pg_proc tuple, so fetch it just once.
    4510              :      *
    4511              :      * Note: the allow_non_const flag suppresses both the second and third
    4512              :      * strategies; so if !allow_non_const, simplify_function can only return a
    4513              :      * Const or NULL.  Argument-list rewriting happens anyway, though.
    4514              :      */
    4515       963406 :     func_tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
    4516       963406 :     if (!HeapTupleIsValid(func_tuple))
    4517            0 :         elog(ERROR, "cache lookup failed for function %u", funcid);
    4518       963406 :     func_form = (Form_pg_proc) GETSTRUCT(func_tuple);
    4519              : 
    4520              :     /*
    4521              :      * Process the function arguments, unless the caller did it already.
    4522              :      *
    4523              :      * Here we must deal with named or defaulted arguments, and then
    4524              :      * recursively apply eval_const_expressions to the whole argument list.
    4525              :      */
    4526       963406 :     if (process_args)
    4527              :     {
    4528       961485 :         args = expand_function_arguments(args, false, result_type, func_tuple);
    4529       961485 :         args = (List *) expression_tree_mutator((Node *) args,
    4530              :                                                 eval_const_expressions_mutator,
    4531              :                                                 context);
    4532              :         /* Argument processing done, give it back to the caller */
    4533       961408 :         *args_p = args;
    4534              :     }
    4535              : 
    4536              :     /* Now attempt simplification of the function call proper. */
    4537              : 
    4538       963329 :     newexpr = evaluate_function(funcid, result_type, result_typmod,
    4539              :                                 result_collid, input_collid,
    4540              :                                 args, funcvariadic,
    4541              :                                 func_tuple, context);
    4542              : 
    4543       960708 :     if (!newexpr && allow_non_const && OidIsValid(func_form->prosupport))
    4544              :     {
    4545              :         /*
    4546              :          * Build a SupportRequestSimplify node to pass to the support
    4547              :          * function, pointing to a dummy FuncExpr node containing the
    4548              :          * simplified arg list.  We use this approach to present a uniform
    4549              :          * interface to the support function regardless of how the target
    4550              :          * function is actually being invoked.
    4551              :          */
    4552              :         SupportRequestSimplify req;
    4553              :         FuncExpr    fexpr;
    4554              : 
    4555        27120 :         fexpr.xpr.type = T_FuncExpr;
    4556        27120 :         fexpr.funcid = funcid;
    4557        27120 :         fexpr.funcresulttype = result_type;
    4558        27120 :         fexpr.funcretset = func_form->proretset;
    4559        27120 :         fexpr.funcvariadic = funcvariadic;
    4560        27120 :         fexpr.funcformat = COERCE_EXPLICIT_CALL;
    4561        27120 :         fexpr.funccollid = result_collid;
    4562        27120 :         fexpr.inputcollid = input_collid;
    4563        27120 :         fexpr.args = args;
    4564        27120 :         fexpr.location = -1;
    4565              : 
    4566        27120 :         req.type = T_SupportRequestSimplify;
    4567        27120 :         req.root = context->root;
    4568        27120 :         req.fcall = &fexpr;
    4569              : 
    4570              :         newexpr = (Expr *)
    4571        27120 :             DatumGetPointer(OidFunctionCall1(func_form->prosupport,
    4572              :                                              PointerGetDatum(&req)));
    4573              : 
    4574              :         /* catch a possible API misunderstanding */
    4575              :         Assert(newexpr != (Expr *) &fexpr);
    4576              :     }
    4577              : 
    4578       960708 :     if (!newexpr && allow_non_const)
    4579       828400 :         newexpr = inline_function(funcid, result_type, result_collid,
    4580              :                                   input_collid, args, funcvariadic,
    4581              :                                   func_tuple, context);
    4582              : 
    4583       960699 :     ReleaseSysCache(func_tuple);
    4584              : 
    4585       960699 :     return newexpr;
    4586              : }
    4587              : 
    4588              : /*
    4589              :  * simplify_aggref
    4590              :  *      Call the Aggref.aggfnoid's prosupport function to allow it to
    4591              :  *      determine if simplification of the Aggref is possible.  Returns the
    4592              :  *      newly simplified node if conversion took place; otherwise, returns the
    4593              :  *      original Aggref.
    4594              :  *
    4595              :  * See SupportRequestSimplifyAggref comments in supportnodes.h for further
    4596              :  * details.
    4597              :  */
    4598              : static Node *
    4599        38101 : simplify_aggref(Aggref *aggref, eval_const_expressions_context *context)
    4600              : {
    4601        38101 :     Oid         prosupport = get_func_support(aggref->aggfnoid);
    4602              : 
    4603        38101 :     if (OidIsValid(prosupport))
    4604              :     {
    4605              :         SupportRequestSimplifyAggref req;
    4606              :         Node       *newnode;
    4607              : 
    4608              :         /*
    4609              :          * Build a SupportRequestSimplifyAggref node to pass to the support
    4610              :          * function.
    4611              :          */
    4612        14016 :         req.type = T_SupportRequestSimplifyAggref;
    4613        14016 :         req.root = context->root;
    4614        14016 :         req.aggref = aggref;
    4615              : 
    4616        14016 :         newnode = (Node *) DatumGetPointer(OidFunctionCall1(prosupport,
    4617              :                                                             PointerGetDatum(&req)));
    4618              : 
    4619              :         /*
    4620              :          * We expect the support function to return either a new Node or NULL
    4621              :          * (when simplification isn't possible).
    4622              :          */
    4623              :         Assert(newnode != (Node *) aggref || newnode == NULL);
    4624              : 
    4625        14016 :         if (newnode != NULL)
    4626          297 :             return newnode;
    4627              :     }
    4628              : 
    4629        37804 :     return (Node *) aggref;
    4630              : }
    4631              : 
    4632              : /*
    4633              :  * var_is_nonnullable: check to see if the Var cannot be NULL
    4634              :  *
    4635              :  * If the Var is defined NOT NULL and meanwhile is not nulled by any outer
    4636              :  * joins or grouping sets, then we can know that it cannot be NULL.
    4637              :  *
    4638              :  * "source" specifies where we should look for NOT NULL proofs.
    4639              :  */
    4640              : bool
    4641        25603 : var_is_nonnullable(PlannerInfo *root, Var *var, NotNullSource source)
    4642              : {
    4643              :     Assert(IsA(var, Var));
    4644              : 
    4645              :     /* skip upper-level Vars */
    4646        25603 :     if (var->varlevelsup != 0)
    4647           55 :         return false;
    4648              : 
    4649              :     /* could the Var be nulled by any outer joins or grouping sets? */
    4650        25548 :     if (!bms_is_empty(var->varnullingrels))
    4651         3591 :         return false;
    4652              : 
    4653              :     /*
    4654              :      * If the Var has a non-default returning type, it could be NULL
    4655              :      * regardless of any NOT NULL constraint.  For example, OLD.col is NULL
    4656              :      * for INSERT, and NEW.col is NULL for DELETE.
    4657              :      */
    4658        21957 :     if (var->varreturningtype != VAR_RETURNING_DEFAULT)
    4659           20 :         return false;
    4660              : 
    4661              :     /* system columns cannot be NULL */
    4662        21937 :     if (var->varattno < 0)
    4663           30 :         return true;
    4664              : 
    4665              :     /* we don't trust whole-row Vars */
    4666        21907 :     if (var->varattno == 0)
    4667           48 :         return false;
    4668              : 
    4669              :     /* Check if the Var is defined as NOT NULL. */
    4670        21859 :     switch (source)
    4671              :     {
    4672         6272 :         case NOTNULL_SOURCE_RELOPT:
    4673              :             {
    4674              :                 /*
    4675              :                  * We retrieve the column NOT NULL constraint information from
    4676              :                  * the corresponding RelOptInfo.
    4677              :                  */
    4678              :                 RelOptInfo *rel;
    4679              :                 Bitmapset  *notnullattnums;
    4680              : 
    4681         6272 :                 rel = find_base_rel(root, var->varno);
    4682         6272 :                 notnullattnums = rel->notnullattnums;
    4683              : 
    4684         6272 :                 return bms_is_member(var->varattno, notnullattnums);
    4685              :             }
    4686        15472 :         case NOTNULL_SOURCE_HASHTABLE:
    4687              :             {
    4688              :                 /*
    4689              :                  * We retrieve the column NOT NULL constraint information from
    4690              :                  * the hash table.
    4691              :                  */
    4692              :                 RangeTblEntry *rte;
    4693              :                 Bitmapset  *notnullattnums;
    4694              : 
    4695        15472 :                 rte = planner_rt_fetch(var->varno, root);
    4696              : 
    4697              :                 /* We can only reason about ordinary relations */
    4698        15472 :                 if (rte->rtekind != RTE_RELATION)
    4699         1394 :                     return false;
    4700              : 
    4701              :                 /*
    4702              :                  * We must skip inheritance parent tables, as some child
    4703              :                  * tables may have a NOT NULL constraint for a column while
    4704              :                  * others may not.  This cannot happen with partitioned
    4705              :                  * tables, though.
    4706              :                  */
    4707        14078 :                 if (rte->inh && rte->relkind != RELKIND_PARTITIONED_TABLE)
    4708          175 :                     return false;
    4709              : 
    4710        13903 :                 notnullattnums = find_relation_notnullatts(root, rte->relid);
    4711              : 
    4712        13903 :                 return bms_is_member(var->varattno, notnullattnums);
    4713              :             }
    4714          115 :         case NOTNULL_SOURCE_CATALOG:
    4715              :             {
    4716              :                 /*
    4717              :                  * We check the attnullability field in the tuple descriptor.
    4718              :                  * This is necessary rather than checking the attnotnull field
    4719              :                  * from the attribute relation, because attnotnull is also set
    4720              :                  * for invalid (NOT VALID) NOT NULL constraints, which do not
    4721              :                  * guarantee the absence of NULLs.
    4722              :                  */
    4723              :                 RangeTblEntry *rte;
    4724              :                 Relation    rel;
    4725              :                 CompactAttribute *attr;
    4726              :                 bool        result;
    4727              : 
    4728          115 :                 rte = planner_rt_fetch(var->varno, root);
    4729              : 
    4730              :                 /* We can only reason about ordinary relations */
    4731          115 :                 if (rte->rtekind != RTE_RELATION)
    4732            0 :                     return false;
    4733              : 
    4734              :                 /*
    4735              :                  * We must skip inheritance parent tables, as some child
    4736              :                  * tables may have a NOT NULL constraint for a column while
    4737              :                  * others may not.  This cannot happen with partitioned
    4738              :                  * tables, though.
    4739              :                  *
    4740              :                  * Note that we need to check if the relation actually has any
    4741              :                  * children, as we might not have done that yet.
    4742              :                  */
    4743          115 :                 if (rte->inh && has_subclass(rte->relid) &&
    4744            0 :                     rte->relkind != RELKIND_PARTITIONED_TABLE)
    4745            0 :                     return false;
    4746              : 
    4747              :                 /* We need not lock the relation since it was already locked */
    4748          115 :                 rel = table_open(rte->relid, NoLock);
    4749          115 :                 attr = TupleDescCompactAttr(RelationGetDescr(rel),
    4750          115 :                                             var->varattno - 1);
    4751          115 :                 result = (attr->attnullability == ATTNULLABLE_VALID);
    4752          115 :                 table_close(rel, NoLock);
    4753              : 
    4754          115 :                 return result;
    4755              :             }
    4756            0 :         default:
    4757            0 :             elog(ERROR, "unrecognized NotNullSource: %d",
    4758              :                  (int) source);
    4759              :             break;
    4760              :     }
    4761              : 
    4762              :     return false;
    4763              : }
    4764              : 
    4765              : /*
    4766              :  * expr_is_nonnullable: check to see if the Expr cannot be NULL
    4767              :  *
    4768              :  * Returns true iff the given 'expr' cannot produce SQL NULLs.
    4769              :  *
    4770              :  * source: specifies where we should look for NOT NULL proofs for Vars.
    4771              :  *  - NOTNULL_SOURCE_RELOPT: Used when RelOptInfos have been generated.  We
    4772              :  *  retrieve nullability information directly from the RelOptInfo corresponding
    4773              :  *  to the Var.
    4774              :  *  - NOTNULL_SOURCE_HASHTABLE: Used when RelOptInfos are not yet available,
    4775              :  *  but we have already collected relation-level not-null constraints into the
    4776              :  *  global hash table.
    4777              :  *  - NOTNULL_SOURCE_CATALOG: Used for raw parse trees where neither
    4778              :  *  RelOptInfos nor the hash table are available.  In this case, we check the
    4779              :  *  column's attnullability in the tuple descriptor.
    4780              :  *
    4781              :  * For now, we support only a limited set of expression types.  Support for
    4782              :  * additional node types can be added in the future.
    4783              :  */
    4784              : bool
    4785        42470 : expr_is_nonnullable(PlannerInfo *root, Expr *expr, NotNullSource source)
    4786              : {
    4787              :     /* since this function recurses, it could be driven to stack overflow */
    4788        42470 :     check_stack_depth();
    4789              : 
    4790        42470 :     switch (nodeTag(expr))
    4791              :     {
    4792        37460 :         case T_Var:
    4793              :             {
    4794        37460 :                 if (root)
    4795        25603 :                     return var_is_nonnullable(root, (Var *) expr, source);
    4796              :             }
    4797        11857 :             break;
    4798          417 :         case T_Const:
    4799          417 :             return !((Const *) expr)->constisnull;
    4800          165 :         case T_CoalesceExpr:
    4801              :             {
    4802              :                 /*
    4803              :                  * A CoalesceExpr returns NULL if and only if all its
    4804              :                  * arguments are NULL.  Therefore, we can determine that a
    4805              :                  * CoalesceExpr cannot be NULL if at least one of its
    4806              :                  * arguments can be proven non-nullable.
    4807              :                  */
    4808          165 :                 CoalesceExpr *coalesceexpr = (CoalesceExpr *) expr;
    4809              : 
    4810          570 :                 foreach_ptr(Expr, arg, coalesceexpr->args)
    4811              :                 {
    4812          330 :                     if (expr_is_nonnullable(root, arg, source))
    4813           45 :                         return true;
    4814              :                 }
    4815              :             }
    4816          120 :             break;
    4817           15 :         case T_MinMaxExpr:
    4818              :             {
    4819              :                 /*
    4820              :                  * Like CoalesceExpr, a MinMaxExpr returns NULL only if all
    4821              :                  * its arguments evaluate to NULL.
    4822              :                  */
    4823           15 :                 MinMaxExpr *minmaxexpr = (MinMaxExpr *) expr;
    4824              : 
    4825           50 :                 foreach_ptr(Expr, arg, minmaxexpr->args)
    4826              :                 {
    4827           30 :                     if (expr_is_nonnullable(root, arg, source))
    4828            5 :                         return true;
    4829              :                 }
    4830              :             }
    4831           10 :             break;
    4832           81 :         case T_CaseExpr:
    4833              :             {
    4834              :                 /*
    4835              :                  * A CASE expression is non-nullable if all branch results are
    4836              :                  * non-nullable.  We must also verify that the default result
    4837              :                  * (ELSE) exists and is non-nullable.
    4838              :                  */
    4839           81 :                 CaseExpr   *caseexpr = (CaseExpr *) expr;
    4840              : 
    4841              :                 /* The default result must be present and non-nullable */
    4842           81 :                 if (caseexpr->defresult == NULL ||
    4843           81 :                     !expr_is_nonnullable(root, caseexpr->defresult, source))
    4844           66 :                     return false;
    4845              : 
    4846              :                 /* All branch results must be non-nullable */
    4847           25 :                 foreach_ptr(CaseWhen, casewhen, caseexpr->args)
    4848              :                 {
    4849           15 :                     if (!expr_is_nonnullable(root, casewhen->result, source))
    4850           10 :                         return false;
    4851              :                 }
    4852              : 
    4853            5 :                 return true;
    4854              :             }
    4855              :             break;
    4856            5 :         case T_ArrayExpr:
    4857              :             {
    4858              :                 /*
    4859              :                  * An ARRAY[] expression always returns a valid Array object,
    4860              :                  * even if it is empty (ARRAY[]) or contains NULLs
    4861              :                  * (ARRAY[NULL]).  It never evaluates to a SQL NULL.
    4862              :                  */
    4863            5 :                 return true;
    4864              :             }
    4865            7 :         case T_NullTest:
    4866              :             {
    4867              :                 /*
    4868              :                  * An IS NULL / IS NOT NULL expression always returns a
    4869              :                  * boolean value.  It never returns SQL NULL.
    4870              :                  */
    4871            7 :                 return true;
    4872              :             }
    4873            5 :         case T_BooleanTest:
    4874              :             {
    4875              :                 /*
    4876              :                  * A BooleanTest expression always evaluates to a boolean
    4877              :                  * value.  It never returns SQL NULL.
    4878              :                  */
    4879            5 :                 return true;
    4880              :             }
    4881            5 :         case T_DistinctExpr:
    4882              :             {
    4883              :                 /*
    4884              :                  * IS DISTINCT FROM never returns NULL, effectively acting as
    4885              :                  * though NULL were a normal data value.
    4886              :                  */
    4887            5 :                 return true;
    4888              :             }
    4889           63 :         case T_RelabelType:
    4890              :             {
    4891              :                 /*
    4892              :                  * RelabelType does not change the nullability of the data.
    4893              :                  * The result is non-nullable if and only if the argument is
    4894              :                  * non-nullable.
    4895              :                  */
    4896           63 :                 return expr_is_nonnullable(root, ((RelabelType *) expr)->arg,
    4897              :                                            source);
    4898              :             }
    4899         4247 :         default:
    4900         4247 :             break;
    4901              :     }
    4902              : 
    4903        16234 :     return false;
    4904              : }
    4905              : 
    4906              : /*
    4907              :  * expand_function_arguments: convert named-notation args to positional args
    4908              :  * and/or insert default args, as needed
    4909              :  *
    4910              :  * Returns a possibly-transformed version of the args list.
    4911              :  *
    4912              :  * If include_out_arguments is true, then the args list and the result
    4913              :  * include OUT arguments.
    4914              :  *
    4915              :  * The expected result type of the call must be given, for sanity-checking
    4916              :  * purposes.  Also, we ask the caller to provide the function's actual
    4917              :  * pg_proc tuple, not just its OID.
    4918              :  *
    4919              :  * If we need to change anything, the input argument list is copied, not
    4920              :  * modified.
    4921              :  *
    4922              :  * Note: this gets applied to operator argument lists too, even though the
    4923              :  * cases it handles should never occur there.  This should be OK since it
    4924              :  * will fall through very quickly if there's nothing to do.
    4925              :  */
    4926              : List *
    4927       964852 : expand_function_arguments(List *args, bool include_out_arguments,
    4928              :                           Oid result_type, HeapTuple func_tuple)
    4929              : {
    4930       964852 :     Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
    4931       964852 :     Oid        *proargtypes = funcform->proargtypes.values;
    4932       964852 :     int         pronargs = funcform->pronargs;
    4933       964852 :     bool        has_named_args = false;
    4934              :     ListCell   *lc;
    4935              : 
    4936              :     /*
    4937              :      * If we are asked to match to OUT arguments, then use the proallargtypes
    4938              :      * array (which includes those); otherwise use proargtypes (which
    4939              :      * doesn't).  Of course, if proallargtypes is null, we always use
    4940              :      * proargtypes.  (Fetching proallargtypes is annoyingly expensive
    4941              :      * considering that we may have nothing to do here, but fortunately the
    4942              :      * common case is include_out_arguments == false.)
    4943              :      */
    4944       964852 :     if (include_out_arguments)
    4945              :     {
    4946              :         Datum       proallargtypes;
    4947              :         bool        isNull;
    4948              : 
    4949          290 :         proallargtypes = SysCacheGetAttr(PROCOID, func_tuple,
    4950              :                                          Anum_pg_proc_proallargtypes,
    4951              :                                          &isNull);
    4952          290 :         if (!isNull)
    4953              :         {
    4954          118 :             ArrayType  *arr = DatumGetArrayTypeP(proallargtypes);
    4955              : 
    4956          118 :             pronargs = ARR_DIMS(arr)[0];
    4957          118 :             if (ARR_NDIM(arr) != 1 ||
    4958          118 :                 pronargs < 0 ||
    4959          118 :                 ARR_HASNULL(arr) ||
    4960          118 :                 ARR_ELEMTYPE(arr) != OIDOID)
    4961            0 :                 elog(ERROR, "proallargtypes is not a 1-D Oid array or it contains nulls");
    4962              :             Assert(pronargs >= funcform->pronargs);
    4963          118 :             proargtypes = (Oid *) ARR_DATA_PTR(arr);
    4964              :         }
    4965              :     }
    4966              : 
    4967              :     /* Do we have any named arguments? */
    4968      2663150 :     foreach(lc, args)
    4969              :     {
    4970      1707605 :         Node       *arg = (Node *) lfirst(lc);
    4971              : 
    4972      1707605 :         if (IsA(arg, NamedArgExpr))
    4973              :         {
    4974         9307 :             has_named_args = true;
    4975         9307 :             break;
    4976              :         }
    4977              :     }
    4978              : 
    4979              :     /* If so, we must apply reorder_function_arguments */
    4980       964852 :     if (has_named_args)
    4981              :     {
    4982         9307 :         args = reorder_function_arguments(args, pronargs, func_tuple);
    4983              :         /* Recheck argument types and add casts if needed */
    4984         9307 :         recheck_cast_function_args(args, result_type,
    4985              :                                    proargtypes, pronargs,
    4986              :                                    func_tuple);
    4987              :     }
    4988       955545 :     else if (list_length(args) < pronargs)
    4989              :     {
    4990              :         /* No named args, but we seem to be short some defaults */
    4991         5495 :         args = add_function_defaults(args, pronargs, func_tuple);
    4992              :         /* Recheck argument types and add casts if needed */
    4993         5495 :         recheck_cast_function_args(args, result_type,
    4994              :                                    proargtypes, pronargs,
    4995              :                                    func_tuple);
    4996              :     }
    4997              : 
    4998       964852 :     return args;
    4999              : }
    5000              : 
    5001              : /*
    5002              :  * reorder_function_arguments: convert named-notation args to positional args
    5003              :  *
    5004              :  * This function also inserts default argument values as needed, since it's
    5005              :  * impossible to form a truly valid positional call without that.
    5006              :  */
    5007              : static List *
    5008         9307 : reorder_function_arguments(List *args, int pronargs, HeapTuple func_tuple)
    5009              : {
    5010         9307 :     Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
    5011         9307 :     int         nargsprovided = list_length(args);
    5012              :     Node       *argarray[FUNC_MAX_ARGS];
    5013              :     ListCell   *lc;
    5014              :     int         i;
    5015              : 
    5016              :     Assert(nargsprovided <= pronargs);
    5017         9307 :     if (pronargs < 0 || pronargs > FUNC_MAX_ARGS)
    5018            0 :         elog(ERROR, "too many function arguments");
    5019         9307 :     memset(argarray, 0, pronargs * sizeof(Node *));
    5020              : 
    5021              :     /* Deconstruct the argument list into an array indexed by argnumber */
    5022         9307 :     i = 0;
    5023        37847 :     foreach(lc, args)
    5024              :     {
    5025        28540 :         Node       *arg = (Node *) lfirst(lc);
    5026              : 
    5027        28540 :         if (!IsA(arg, NamedArgExpr))
    5028              :         {
    5029              :             /* positional argument, assumed to precede all named args */
    5030              :             Assert(argarray[i] == NULL);
    5031         1842 :             argarray[i++] = arg;
    5032              :         }
    5033              :         else
    5034              :         {
    5035        26698 :             NamedArgExpr *na = (NamedArgExpr *) arg;
    5036              : 
    5037              :             Assert(na->argnumber >= 0 && na->argnumber < pronargs);
    5038              :             Assert(argarray[na->argnumber] == NULL);
    5039        26698 :             argarray[na->argnumber] = (Node *) na->arg;
    5040              :         }
    5041              :     }
    5042              : 
    5043              :     /*
    5044              :      * Fetch default expressions, if needed, and insert into array at proper
    5045              :      * locations (they aren't necessarily consecutive or all used)
    5046              :      */
    5047         9307 :     if (nargsprovided < pronargs)
    5048              :     {
    5049         4396 :         List       *defaults = fetch_function_defaults(func_tuple);
    5050              : 
    5051         4396 :         i = pronargs - funcform->pronargdefaults;
    5052        24462 :         foreach(lc, defaults)
    5053              :         {
    5054        20066 :             if (argarray[i] == NULL)
    5055         8737 :                 argarray[i] = (Node *) lfirst(lc);
    5056        20066 :             i++;
    5057              :         }
    5058              :     }
    5059              : 
    5060              :     /* Now reconstruct the args list in proper order */
    5061         9307 :     args = NIL;
    5062        46584 :     for (i = 0; i < pronargs; i++)
    5063              :     {
    5064              :         Assert(argarray[i] != NULL);
    5065        37277 :         args = lappend(args, argarray[i]);
    5066              :     }
    5067              : 
    5068         9307 :     return args;
    5069              : }
    5070              : 
    5071              : /*
    5072              :  * add_function_defaults: add missing function arguments from its defaults
    5073              :  *
    5074              :  * This is used only when the argument list was positional to begin with,
    5075              :  * and so we know we just need to add defaults at the end.
    5076              :  */
    5077              : static List *
    5078         5495 : add_function_defaults(List *args, int pronargs, HeapTuple func_tuple)
    5079              : {
    5080         5495 :     int         nargsprovided = list_length(args);
    5081              :     List       *defaults;
    5082              :     int         ndelete;
    5083              : 
    5084              :     /* Get all the default expressions from the pg_proc tuple */
    5085         5495 :     defaults = fetch_function_defaults(func_tuple);
    5086              : 
    5087              :     /* Delete any unused defaults from the list */
    5088         5495 :     ndelete = nargsprovided + list_length(defaults) - pronargs;
    5089         5495 :     if (ndelete < 0)
    5090            0 :         elog(ERROR, "not enough default arguments");
    5091         5495 :     if (ndelete > 0)
    5092          179 :         defaults = list_delete_first_n(defaults, ndelete);
    5093              : 
    5094              :     /* And form the combined argument list, not modifying the input list */
    5095         5495 :     return list_concat_copy(args, defaults);
    5096              : }
    5097              : 
    5098              : /*
    5099              :  * fetch_function_defaults: get function's default arguments as expression list
    5100              :  */
    5101              : static List *
    5102         9891 : fetch_function_defaults(HeapTuple func_tuple)
    5103              : {
    5104              :     List       *defaults;
    5105              :     Datum       proargdefaults;
    5106              :     char       *str;
    5107              : 
    5108         9891 :     proargdefaults = SysCacheGetAttrNotNull(PROCOID, func_tuple,
    5109              :                                             Anum_pg_proc_proargdefaults);
    5110         9891 :     str = TextDatumGetCString(proargdefaults);
    5111         9891 :     defaults = castNode(List, stringToNode(str));
    5112         9891 :     pfree(str);
    5113         9891 :     return defaults;
    5114              : }
    5115              : 
    5116              : /*
    5117              :  * recheck_cast_function_args: recheck function args and typecast as needed
    5118              :  * after adding defaults.
    5119              :  *
    5120              :  * It is possible for some of the defaulted arguments to be polymorphic;
    5121              :  * therefore we can't assume that the default expressions have the correct
    5122              :  * data types already.  We have to re-resolve polymorphics and do coercion
    5123              :  * just like the parser did.
    5124              :  *
    5125              :  * This should be a no-op if there are no polymorphic arguments,
    5126              :  * but we do it anyway to be sure.
    5127              :  *
    5128              :  * Note: if any casts are needed, the args list is modified in-place;
    5129              :  * caller should have already copied the list structure.
    5130              :  */
    5131              : static void
    5132        14802 : recheck_cast_function_args(List *args, Oid result_type,
    5133              :                            Oid *proargtypes, int pronargs,
    5134              :                            HeapTuple func_tuple)
    5135              : {
    5136        14802 :     Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
    5137              :     int         nargs;
    5138              :     Oid         actual_arg_types[FUNC_MAX_ARGS];
    5139              :     Oid         declared_arg_types[FUNC_MAX_ARGS];
    5140              :     Oid         rettype;
    5141              :     ListCell   *lc;
    5142              : 
    5143        14802 :     if (list_length(args) > FUNC_MAX_ARGS)
    5144            0 :         elog(ERROR, "too many function arguments");
    5145        14802 :     nargs = 0;
    5146        72409 :     foreach(lc, args)
    5147              :     {
    5148        57607 :         actual_arg_types[nargs++] = exprType((Node *) lfirst(lc));
    5149              :     }
    5150              :     Assert(nargs == pronargs);
    5151        14802 :     memcpy(declared_arg_types, proargtypes, pronargs * sizeof(Oid));
    5152        14802 :     rettype = enforce_generic_type_consistency(actual_arg_types,
    5153              :                                                declared_arg_types,
    5154              :                                                nargs,
    5155              :                                                funcform->prorettype,
    5156              :                                                false);
    5157              :     /* let's just check we got the same answer as the parser did ... */
    5158        14802 :     if (rettype != result_type)
    5159            0 :         elog(ERROR, "function's resolved result type changed during planning");
    5160              : 
    5161              :     /* perform any necessary typecasting of arguments */
    5162        14802 :     make_fn_arguments(NULL, args, actual_arg_types, declared_arg_types);
    5163        14802 : }
    5164              : 
    5165              : /*
    5166              :  * evaluate_function: try to pre-evaluate a function call
    5167              :  *
    5168              :  * We can do this if the function is strict and has any constant-null inputs
    5169              :  * (just return a null constant), or if the function is immutable and has all
    5170              :  * constant inputs (call it and return the result as a Const node).  In
    5171              :  * estimation mode we are willing to pre-evaluate stable functions too.
    5172              :  *
    5173              :  * Returns a simplified expression if successful, or NULL if cannot
    5174              :  * simplify the function.
    5175              :  */
    5176              : static Expr *
    5177       963329 : evaluate_function(Oid funcid, Oid result_type, int32 result_typmod,
    5178              :                   Oid result_collid, Oid input_collid, List *args,
    5179              :                   bool funcvariadic,
    5180              :                   HeapTuple func_tuple,
    5181              :                   eval_const_expressions_context *context)
    5182              : {
    5183       963329 :     Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
    5184       963329 :     bool        has_nonconst_input = false;
    5185       963329 :     bool        has_null_input = false;
    5186              :     ListCell   *arg;
    5187              :     FuncExpr   *newexpr;
    5188              : 
    5189              :     /*
    5190              :      * Can't simplify if it returns a set.
    5191              :      */
    5192       963329 :     if (funcform->proretset)
    5193        45763 :         return NULL;
    5194              : 
    5195              :     /*
    5196              :      * Can't simplify if it returns RECORD.  The immediate problem is that it
    5197              :      * will be needing an expected tupdesc which we can't supply here.
    5198              :      *
    5199              :      * In the case where it has OUT parameters, we could build an expected
    5200              :      * tupdesc from those, but there may be other gotchas lurking.  In
    5201              :      * particular, if the function were to return NULL, we would produce a
    5202              :      * null constant with no remaining indication of which concrete record
    5203              :      * type it is.  For now, seems best to leave the function call unreduced.
    5204              :      */
    5205       917566 :     if (funcform->prorettype == RECORDOID)
    5206         3873 :         return NULL;
    5207              : 
    5208              :     /*
    5209              :      * Check for constant inputs and especially constant-NULL inputs.
    5210              :      */
    5211      2546001 :     foreach(arg, args)
    5212              :     {
    5213      1632308 :         if (IsA(lfirst(arg), Const))
    5214       724143 :             has_null_input |= ((Const *) lfirst(arg))->constisnull;
    5215              :         else
    5216       908165 :             has_nonconst_input = true;
    5217              :     }
    5218              : 
    5219              :     /*
    5220              :      * If the function is strict and has a constant-NULL input, it will never
    5221              :      * be called at all, so we can replace the call by a NULL constant, even
    5222              :      * if there are other inputs that aren't constant, and even if the
    5223              :      * function is not otherwise immutable.
    5224              :      */
    5225       913693 :     if (funcform->proisstrict && has_null_input)
    5226         4422 :         return (Expr *) makeNullConst(result_type, result_typmod,
    5227              :                                       result_collid);
    5228              : 
    5229              :     /*
    5230              :      * Otherwise, can simplify only if all inputs are constants. (For a
    5231              :      * non-strict function, constant NULL inputs are treated the same as
    5232              :      * constant non-NULL inputs.)
    5233              :      */
    5234       909271 :     if (has_nonconst_input)
    5235       691612 :         return NULL;
    5236              : 
    5237              :     /*
    5238              :      * Ordinarily we are only allowed to simplify immutable functions. But for
    5239              :      * purposes of estimation, we consider it okay to simplify functions that
    5240              :      * are merely stable; the risk that the result might change from planning
    5241              :      * time to execution time is worth taking in preference to not being able
    5242              :      * to estimate the value at all.
    5243              :      */
    5244       217659 :     if (funcform->provolatile == PROVOLATILE_IMMUTABLE)
    5245              :          /* okay */ ;
    5246        89134 :     else if (context->estimate && funcform->provolatile == PROVOLATILE_STABLE)
    5247              :          /* okay */ ;
    5248              :     else
    5249        87251 :         return NULL;
    5250              : 
    5251              :     /*
    5252              :      * OK, looks like we can simplify this operator/function.
    5253              :      *
    5254              :      * Build a new FuncExpr node containing the already-simplified arguments.
    5255              :      */
    5256       130408 :     newexpr = makeNode(FuncExpr);
    5257       130408 :     newexpr->funcid = funcid;
    5258       130408 :     newexpr->funcresulttype = result_type;
    5259       130408 :     newexpr->funcretset = false;
    5260       130408 :     newexpr->funcvariadic = funcvariadic;
    5261       130408 :     newexpr->funcformat = COERCE_EXPLICIT_CALL; /* doesn't matter */
    5262       130408 :     newexpr->funccollid = result_collid; /* doesn't matter */
    5263       130408 :     newexpr->inputcollid = input_collid;
    5264       130408 :     newexpr->args = args;
    5265       130408 :     newexpr->location = -1;
    5266              : 
    5267       130408 :     return evaluate_expr((Expr *) newexpr, result_type, result_typmod,
    5268              :                          result_collid);
    5269              : }
    5270              : 
    5271              : /*
    5272              :  * inline_function: try to expand a function call inline
    5273              :  *
    5274              :  * If the function is a sufficiently simple SQL-language function
    5275              :  * (just "SELECT expression"), then we can inline it and avoid the rather
    5276              :  * high per-call overhead of SQL functions.  Furthermore, this can expose
    5277              :  * opportunities for constant-folding within the function expression.
    5278              :  *
    5279              :  * We have to beware of some special cases however.  A directly or
    5280              :  * indirectly recursive function would cause us to recurse forever,
    5281              :  * so we keep track of which functions we are already expanding and
    5282              :  * do not re-expand them.  Also, if a parameter is used more than once
    5283              :  * in the SQL-function body, we require it not to contain any volatile
    5284              :  * functions (volatiles might deliver inconsistent answers) nor to be
    5285              :  * unreasonably expensive to evaluate.  The expensiveness check not only
    5286              :  * prevents us from doing multiple evaluations of an expensive parameter
    5287              :  * at runtime, but is a safety value to limit growth of an expression due
    5288              :  * to repeated inlining.
    5289              :  *
    5290              :  * We must also beware of changing the volatility or strictness status of
    5291              :  * functions by inlining them.
    5292              :  *
    5293              :  * Also, at the moment we can't inline functions returning RECORD.  This
    5294              :  * doesn't work in the general case because it discards information such
    5295              :  * as OUT-parameter declarations.
    5296              :  *
    5297              :  * Also, context-dependent expression nodes in the argument list are trouble.
    5298              :  *
    5299              :  * Returns a simplified expression if successful, or NULL if cannot
    5300              :  * simplify the function.
    5301              :  */
    5302              : static Expr *
    5303       828400 : inline_function(Oid funcid, Oid result_type, Oid result_collid,
    5304              :                 Oid input_collid, List *args,
    5305              :                 bool funcvariadic,
    5306              :                 HeapTuple func_tuple,
    5307              :                 eval_const_expressions_context *context)
    5308              : {
    5309       828400 :     Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
    5310              :     char       *src;
    5311              :     Datum       tmp;
    5312              :     bool        isNull;
    5313              :     MemoryContext oldcxt;
    5314              :     MemoryContext mycxt;
    5315              :     inline_error_callback_arg callback_arg;
    5316              :     ErrorContextCallback sqlerrcontext;
    5317              :     FuncExpr   *fexpr;
    5318              :     SQLFunctionParseInfoPtr pinfo;
    5319              :     TupleDesc   rettupdesc;
    5320              :     ParseState *pstate;
    5321              :     List       *raw_parsetree_list;
    5322              :     List       *querytree_list;
    5323              :     Query      *querytree;
    5324              :     Node       *newexpr;
    5325              :     int        *usecounts;
    5326              :     ListCell   *arg;
    5327              :     int         i;
    5328              : 
    5329              :     /*
    5330              :      * Forget it if the function is not SQL-language or has other showstopper
    5331              :      * properties.  (The prokind and nargs checks are just paranoia.)
    5332              :      */
    5333       828400 :     if (funcform->prolang != SQLlanguageId ||
    5334         7487 :         funcform->prokind != PROKIND_FUNCTION ||
    5335         7487 :         funcform->prosecdef ||
    5336         7477 :         funcform->proretset ||
    5337         6044 :         funcform->prorettype == RECORDOID ||
    5338        11543 :         !heap_attisnull(func_tuple, Anum_pg_proc_proconfig, NULL) ||
    5339         5754 :         funcform->pronargs != list_length(args))
    5340       822646 :         return NULL;
    5341              : 
    5342              :     /* Check for recursive function, and give up trying to expand if so */
    5343         5754 :     if (list_member_oid(context->active_fns, funcid))
    5344           10 :         return NULL;
    5345              : 
    5346              :     /* Check permission to call function (fail later, if not) */
    5347         5744 :     if (object_aclcheck(ProcedureRelationId, funcid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
    5348           13 :         return NULL;
    5349              : 
    5350              :     /* Check whether a plugin wants to hook function entry/exit */
    5351         5731 :     if (FmgrHookIsNeeded(funcid))
    5352            0 :         return NULL;
    5353              : 
    5354              :     /*
    5355              :      * Make a temporary memory context, so that we don't leak all the stuff
    5356              :      * that parsing might create.
    5357              :      */
    5358         5731 :     mycxt = AllocSetContextCreate(CurrentMemoryContext,
    5359              :                                   "inline_function",
    5360              :                                   ALLOCSET_DEFAULT_SIZES);
    5361         5731 :     oldcxt = MemoryContextSwitchTo(mycxt);
    5362              : 
    5363              :     /*
    5364              :      * We need a dummy FuncExpr node containing the already-simplified
    5365              :      * arguments.  (In some cases we don't really need it, but building it is
    5366              :      * cheap enough that it's not worth contortions to avoid.)
    5367              :      */
    5368         5731 :     fexpr = makeNode(FuncExpr);
    5369         5731 :     fexpr->funcid = funcid;
    5370         5731 :     fexpr->funcresulttype = result_type;
    5371         5731 :     fexpr->funcretset = false;
    5372         5731 :     fexpr->funcvariadic = funcvariadic;
    5373         5731 :     fexpr->funcformat = COERCE_EXPLICIT_CALL;    /* doesn't matter */
    5374         5731 :     fexpr->funccollid = result_collid;   /* doesn't matter */
    5375         5731 :     fexpr->inputcollid = input_collid;
    5376         5731 :     fexpr->args = args;
    5377         5731 :     fexpr->location = -1;
    5378              : 
    5379              :     /* Fetch the function body */
    5380         5731 :     tmp = SysCacheGetAttrNotNull(PROCOID, func_tuple, Anum_pg_proc_prosrc);
    5381         5731 :     src = TextDatumGetCString(tmp);
    5382              : 
    5383              :     /*
    5384              :      * Setup error traceback support for ereport().  This is so that we can
    5385              :      * finger the function that bad information came from.
    5386              :      */
    5387         5731 :     callback_arg.proname = NameStr(funcform->proname);
    5388         5731 :     callback_arg.prosrc = src;
    5389              : 
    5390         5731 :     sqlerrcontext.callback = sql_inline_error_callback;
    5391         5731 :     sqlerrcontext.arg = &callback_arg;
    5392         5731 :     sqlerrcontext.previous = error_context_stack;
    5393         5731 :     error_context_stack = &sqlerrcontext;
    5394              : 
    5395              :     /* If we have prosqlbody, pay attention to that not prosrc */
    5396         5731 :     tmp = SysCacheGetAttr(PROCOID,
    5397              :                           func_tuple,
    5398              :                           Anum_pg_proc_prosqlbody,
    5399              :                           &isNull);
    5400         5731 :     if (!isNull)
    5401              :     {
    5402              :         Node       *n;
    5403              :         List       *query_list;
    5404              : 
    5405         3297 :         n = stringToNode(TextDatumGetCString(tmp));
    5406         3297 :         if (IsA(n, List))
    5407         2652 :             query_list = linitial_node(List, castNode(List, n));
    5408              :         else
    5409          645 :             query_list = list_make1(n);
    5410         3297 :         if (list_length(query_list) != 1)
    5411            5 :             goto fail;
    5412         3292 :         querytree = linitial(query_list);
    5413              : 
    5414              :         /*
    5415              :          * Because we'll insist below that the querytree have an empty rtable
    5416              :          * and no sublinks, it cannot have any relation references that need
    5417              :          * to be locked or rewritten.  So we can omit those steps.
    5418              :          */
    5419              :     }
    5420              :     else
    5421              :     {
    5422              :         /* Set up to handle parameters while parsing the function body. */
    5423         2434 :         pinfo = prepare_sql_fn_parse_info(func_tuple,
    5424              :                                           (Node *) fexpr,
    5425              :                                           input_collid);
    5426              : 
    5427              :         /*
    5428              :          * We just do parsing and parse analysis, not rewriting, because
    5429              :          * rewriting will not affect table-free-SELECT-only queries, which is
    5430              :          * all that we care about.  Also, we can punt as soon as we detect
    5431              :          * more than one command in the function body.
    5432              :          */
    5433         2434 :         raw_parsetree_list = pg_parse_query(src);
    5434         2434 :         if (list_length(raw_parsetree_list) != 1)
    5435           45 :             goto fail;
    5436              : 
    5437         2389 :         pstate = make_parsestate(NULL);
    5438         2389 :         pstate->p_sourcetext = src;
    5439         2389 :         sql_fn_parser_setup(pstate, pinfo);
    5440              : 
    5441         2389 :         querytree = transformTopLevelStmt(pstate, linitial(raw_parsetree_list));
    5442              : 
    5443         2385 :         free_parsestate(pstate);
    5444              :     }
    5445              : 
    5446              :     /*
    5447              :      * The single command must be a simple "SELECT expression".
    5448              :      *
    5449              :      * Note: if you change the tests involved in this, see also plpgsql's
    5450              :      * exec_simple_check_plan().  That generally needs to have the same idea
    5451              :      * of what's a "simple expression", so that inlining a function that
    5452              :      * previously wasn't inlined won't change plpgsql's conclusion.
    5453              :      */
    5454         5677 :     if (!IsA(querytree, Query) ||
    5455         5677 :         querytree->commandType != CMD_SELECT ||
    5456         5576 :         querytree->hasAggs ||
    5457         5421 :         querytree->hasWindowFuncs ||
    5458         5421 :         querytree->hasTargetSRFs ||
    5459         5421 :         querytree->hasSubLinks ||
    5460         4280 :         querytree->cteList ||
    5461         4280 :         querytree->rtable ||
    5462         2718 :         querytree->jointree->fromlist ||
    5463         2718 :         querytree->jointree->quals ||
    5464         2718 :         querytree->groupClause ||
    5465         2718 :         querytree->groupingSets ||
    5466         2718 :         querytree->havingQual ||
    5467         2718 :         querytree->windowClause ||
    5468         2718 :         querytree->distinctClause ||
    5469         2718 :         querytree->sortClause ||
    5470         2718 :         querytree->limitOffset ||
    5471         2718 :         querytree->limitCount ||
    5472         5320 :         querytree->setOperations ||
    5473         2660 :         list_length(querytree->targetList) != 1)
    5474         3067 :         goto fail;
    5475              : 
    5476              :     /* If the function result is composite, resolve it */
    5477         2610 :     (void) get_expr_result_type((Node *) fexpr,
    5478              :                                 NULL,
    5479              :                                 &rettupdesc);
    5480              : 
    5481              :     /*
    5482              :      * Make sure the function (still) returns what it's declared to.  This
    5483              :      * will raise an error if wrong, but that's okay since the function would
    5484              :      * fail at runtime anyway.  Note that check_sql_fn_retval will also insert
    5485              :      * a coercion if needed to make the tlist expression match the declared
    5486              :      * type of the function.
    5487              :      *
    5488              :      * Note: we do not try this until we have verified that no rewriting was
    5489              :      * needed; that's probably not important, but let's be careful.
    5490              :      */
    5491         2610 :     querytree_list = list_make1(querytree);
    5492         2610 :     if (check_sql_fn_retval(list_make1(querytree_list),
    5493              :                             result_type, rettupdesc,
    5494         2610 :                             funcform->prokind,
    5495              :                             false))
    5496           10 :         goto fail;              /* reject whole-tuple-result cases */
    5497              : 
    5498              :     /*
    5499              :      * Given the tests above, check_sql_fn_retval shouldn't have decided to
    5500              :      * inject a projection step, but let's just make sure.
    5501              :      */
    5502         2596 :     if (querytree != linitial(querytree_list))
    5503            0 :         goto fail;
    5504              : 
    5505              :     /* Now we can grab the tlist expression */
    5506         2596 :     newexpr = (Node *) ((TargetEntry *) linitial(querytree->targetList))->expr;
    5507              : 
    5508              :     /*
    5509              :      * If the SQL function returns VOID, we can only inline it if it is a
    5510              :      * SELECT of an expression returning VOID (ie, it's just a redirection to
    5511              :      * another VOID-returning function).  In all non-VOID-returning cases,
    5512              :      * check_sql_fn_retval should ensure that newexpr returns the function's
    5513              :      * declared result type, so this test shouldn't fail otherwise; but we may
    5514              :      * as well cope gracefully if it does.
    5515              :      */
    5516         2596 :     if (exprType(newexpr) != result_type)
    5517           15 :         goto fail;
    5518              : 
    5519              :     /*
    5520              :      * Additional validity checks on the expression.  It mustn't be more
    5521              :      * volatile than the surrounding function (this is to avoid breaking hacks
    5522              :      * that involve pretending a function is immutable when it really ain't).
    5523              :      * If the surrounding function is declared strict, then the expression
    5524              :      * must contain only strict constructs and must use all of the function
    5525              :      * parameters (this is overkill, but an exact analysis is hard).
    5526              :      */
    5527         3139 :     if (funcform->provolatile == PROVOLATILE_IMMUTABLE &&
    5528          558 :         contain_mutable_functions(newexpr))
    5529            9 :         goto fail;
    5530         3347 :     else if (funcform->provolatile == PROVOLATILE_STABLE &&
    5531          775 :              contain_volatile_functions(newexpr))
    5532            0 :         goto fail;
    5533              : 
    5534         3900 :     if (funcform->proisstrict &&
    5535         1328 :         contain_nonstrict_functions(newexpr))
    5536           37 :         goto fail;
    5537              : 
    5538              :     /*
    5539              :      * If any parameter expression contains a context-dependent node, we can't
    5540              :      * inline, for fear of putting such a node into the wrong context.
    5541              :      */
    5542         2535 :     if (contain_context_dependent_node((Node *) args))
    5543            5 :         goto fail;
    5544              : 
    5545              :     /*
    5546              :      * We may be able to do it; there are still checks on parameter usage to
    5547              :      * make, but those are most easily done in combination with the actual
    5548              :      * substitution of the inputs.  So start building expression with inputs
    5549              :      * substituted.
    5550              :      */
    5551         2530 :     usecounts = (int *) palloc0(funcform->pronargs * sizeof(int));
    5552         2530 :     newexpr = substitute_actual_parameters(newexpr, funcform->pronargs,
    5553              :                                            args, usecounts);
    5554              : 
    5555              :     /* Now check for parameter usage */
    5556         2530 :     i = 0;
    5557         6757 :     foreach(arg, args)
    5558              :     {
    5559         4227 :         Node       *param = lfirst(arg);
    5560              : 
    5561         4227 :         if (usecounts[i] == 0)
    5562              :         {
    5563              :             /* Param not used at all: uncool if func is strict */
    5564          211 :             if (funcform->proisstrict)
    5565            0 :                 goto fail;
    5566              :         }
    5567         4016 :         else if (usecounts[i] != 1)
    5568              :         {
    5569              :             /* Param used multiple times: uncool if expensive or volatile */
    5570              :             QualCost    eval_cost;
    5571              : 
    5572              :             /*
    5573              :              * We define "expensive" as "contains any subplan or more than 10
    5574              :              * operators".  Note that the subplan search has to be done
    5575              :              * explicitly, since cost_qual_eval() will barf on unplanned
    5576              :              * subselects.
    5577              :              */
    5578          281 :             if (contain_subplans(param))
    5579            0 :                 goto fail;
    5580          281 :             cost_qual_eval(&eval_cost, list_make1(param), NULL);
    5581          281 :             if (eval_cost.startup + eval_cost.per_tuple >
    5582          281 :                 10 * cpu_operator_cost)
    5583            0 :                 goto fail;
    5584              : 
    5585              :             /*
    5586              :              * Check volatility last since this is more expensive than the
    5587              :              * above tests
    5588              :              */
    5589          281 :             if (contain_volatile_functions(param))
    5590            0 :                 goto fail;
    5591              :         }
    5592         4227 :         i++;
    5593              :     }
    5594              : 
    5595              :     /*
    5596              :      * Whew --- we can make the substitution.  Copy the modified expression
    5597              :      * out of the temporary memory context, and clean up.
    5598              :      */
    5599         2530 :     MemoryContextSwitchTo(oldcxt);
    5600              : 
    5601         2530 :     newexpr = copyObject(newexpr);
    5602              : 
    5603         2530 :     MemoryContextDelete(mycxt);
    5604              : 
    5605              :     /*
    5606              :      * If the result is of a collatable type, force the result to expose the
    5607              :      * correct collation.  In most cases this does not matter, but it's
    5608              :      * possible that the function result is used directly as a sort key or in
    5609              :      * other places where we expect exprCollation() to tell the truth.
    5610              :      */
    5611         2530 :     if (OidIsValid(result_collid))
    5612              :     {
    5613         1172 :         Oid         exprcoll = exprCollation(newexpr);
    5614              : 
    5615         1172 :         if (OidIsValid(exprcoll) && exprcoll != result_collid)
    5616              :         {
    5617           18 :             CollateExpr *newnode = makeNode(CollateExpr);
    5618              : 
    5619           18 :             newnode->arg = (Expr *) newexpr;
    5620           18 :             newnode->collOid = result_collid;
    5621           18 :             newnode->location = -1;
    5622              : 
    5623           18 :             newexpr = (Node *) newnode;
    5624              :         }
    5625              :     }
    5626              : 
    5627              :     /*
    5628              :      * Since there is now no trace of the function in the plan tree, we must
    5629              :      * explicitly record the plan's dependency on the function.
    5630              :      */
    5631         2530 :     if (context->root)
    5632         2377 :         record_plan_function_dependency(context->root, funcid);
    5633              : 
    5634              :     /*
    5635              :      * Recursively try to simplify the modified expression.  Here we must add
    5636              :      * the current function to the context list of active functions.
    5637              :      */
    5638         2530 :     context->active_fns = lappend_oid(context->active_fns, funcid);
    5639         2530 :     newexpr = eval_const_expressions_mutator(newexpr, context);
    5640         2529 :     context->active_fns = list_delete_last(context->active_fns);
    5641              : 
    5642         2529 :     error_context_stack = sqlerrcontext.previous;
    5643              : 
    5644         2529 :     return (Expr *) newexpr;
    5645              : 
    5646              :     /* Here if func is not inlinable: release temp memory and return NULL */
    5647         3193 : fail:
    5648         3193 :     MemoryContextSwitchTo(oldcxt);
    5649         3193 :     MemoryContextDelete(mycxt);
    5650         3193 :     error_context_stack = sqlerrcontext.previous;
    5651              : 
    5652         3193 :     return NULL;
    5653              : }
    5654              : 
    5655              : /*
    5656              :  * Replace Param nodes by appropriate actual parameters
    5657              :  */
    5658              : static Node *
    5659         2530 : substitute_actual_parameters(Node *expr, int nargs, List *args,
    5660              :                              int *usecounts)
    5661              : {
    5662              :     substitute_actual_parameters_context context;
    5663              : 
    5664         2530 :     context.nargs = nargs;
    5665         2530 :     context.args = args;
    5666         2530 :     context.usecounts = usecounts;
    5667              : 
    5668         2530 :     return substitute_actual_parameters_mutator(expr, &context);
    5669              : }
    5670              : 
    5671              : static Node *
    5672        14266 : substitute_actual_parameters_mutator(Node *node,
    5673              :                                      substitute_actual_parameters_context *context)
    5674              : {
    5675        14266 :     if (node == NULL)
    5676          399 :         return NULL;
    5677        13867 :     if (IsA(node, Param))
    5678              :     {
    5679         4315 :         Param      *param = (Param *) node;
    5680              : 
    5681         4315 :         if (param->paramkind != PARAM_EXTERN)
    5682            0 :             elog(ERROR, "unexpected paramkind: %d", (int) param->paramkind);
    5683         4315 :         if (param->paramid <= 0 || param->paramid > context->nargs)
    5684            0 :             elog(ERROR, "invalid paramid: %d", param->paramid);
    5685              : 
    5686              :         /* Count usage of parameter */
    5687         4315 :         context->usecounts[param->paramid - 1]++;
    5688              : 
    5689              :         /* Select the appropriate actual arg and replace the Param with it */
    5690              :         /* We don't need to copy at this time (it'll get done later) */
    5691         4315 :         return list_nth(context->args, param->paramid - 1);
    5692              :     }
    5693         9552 :     return expression_tree_mutator(node, substitute_actual_parameters_mutator, context);
    5694              : }
    5695              : 
    5696              : /*
    5697              :  * error context callback to let us supply a call-stack traceback
    5698              :  */
    5699              : static void
    5700           13 : sql_inline_error_callback(void *arg)
    5701              : {
    5702           13 :     inline_error_callback_arg *callback_arg = (inline_error_callback_arg *) arg;
    5703              :     int         syntaxerrposition;
    5704              : 
    5705              :     /* If it's a syntax error, convert to internal syntax error report */
    5706           13 :     syntaxerrposition = geterrposition();
    5707           13 :     if (syntaxerrposition > 0)
    5708              :     {
    5709            4 :         errposition(0);
    5710            4 :         internalerrposition(syntaxerrposition);
    5711            4 :         internalerrquery(callback_arg->prosrc);
    5712              :     }
    5713              : 
    5714           13 :     errcontext("SQL function \"%s\" during inlining", callback_arg->proname);
    5715           13 : }
    5716              : 
    5717              : /*
    5718              :  * evaluate_expr: pre-evaluate a constant expression
    5719              :  *
    5720              :  * We use the executor's routine ExecEvalExpr() to avoid duplication of
    5721              :  * code and ensure we get the same result as the executor would get.
    5722              :  */
    5723              : Expr *
    5724       157126 : evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod,
    5725              :               Oid result_collation)
    5726              : {
    5727              :     EState     *estate;
    5728              :     ExprState  *exprstate;
    5729              :     MemoryContext oldcontext;
    5730              :     Datum       const_val;
    5731              :     bool        const_is_null;
    5732              :     int16       resultTypLen;
    5733              :     bool        resultTypByVal;
    5734              : 
    5735              :     /*
    5736              :      * To use the executor, we need an EState.
    5737              :      */
    5738       157126 :     estate = CreateExecutorState();
    5739              : 
    5740              :     /* We can use the estate's working context to avoid memory leaks. */
    5741       157126 :     oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
    5742              : 
    5743              :     /* Make sure any opfuncids are filled in. */
    5744       157126 :     fix_opfuncids((Node *) expr);
    5745              : 
    5746              :     /*
    5747              :      * Prepare expr for execution.  (Note: we can't use ExecPrepareExpr
    5748              :      * because it'd result in recursively invoking eval_const_expressions.)
    5749              :      */
    5750       157126 :     exprstate = ExecInitExpr(expr, NULL);
    5751              : 
    5752              :     /*
    5753              :      * And evaluate it.
    5754              :      *
    5755              :      * It is OK to use a default econtext because none of the ExecEvalExpr()
    5756              :      * code used in this situation will use econtext.  That might seem
    5757              :      * fortuitous, but it's not so unreasonable --- a constant expression does
    5758              :      * not depend on context, by definition, n'est ce pas?
    5759              :      */
    5760       157110 :     const_val = ExecEvalExprSwitchContext(exprstate,
    5761       157110 :                                           GetPerTupleExprContext(estate),
    5762              :                                           &const_is_null);
    5763              : 
    5764              :     /* Get info needed about result datatype */
    5765       154477 :     get_typlenbyval(result_type, &resultTypLen, &resultTypByVal);
    5766              : 
    5767              :     /* Get back to outer memory context */
    5768       154477 :     MemoryContextSwitchTo(oldcontext);
    5769              : 
    5770              :     /*
    5771              :      * Must copy result out of sub-context used by expression eval.
    5772              :      *
    5773              :      * Also, if it's varlena, forcibly detoast it.  This protects us against
    5774              :      * storing TOAST pointers into plans that might outlive the referenced
    5775              :      * data.  (makeConst would handle detoasting anyway, but it's worth a few
    5776              :      * extra lines here so that we can do the copy and detoast in one step.)
    5777              :      */
    5778       154477 :     if (!const_is_null)
    5779              :     {
    5780       149502 :         if (resultTypLen == -1)
    5781        70304 :             const_val = PointerGetDatum(PG_DETOAST_DATUM_COPY(const_val));
    5782              :         else
    5783        79198 :             const_val = datumCopy(const_val, resultTypByVal, resultTypLen);
    5784              :     }
    5785              : 
    5786              :     /* Release all the junk we just created */
    5787       154477 :     FreeExecutorState(estate);
    5788              : 
    5789              :     /*
    5790              :      * Make the constant result node.
    5791              :      */
    5792       154477 :     return (Expr *) makeConst(result_type, result_typmod, result_collation,
    5793              :                               resultTypLen,
    5794              :                               const_val, const_is_null,
    5795              :                               resultTypByVal);
    5796              : }
    5797              : 
    5798              : 
    5799              : /*
    5800              :  * inline_function_in_from
    5801              :  *      Attempt to "inline" a function in the FROM clause.
    5802              :  *
    5803              :  * "rte" is an RTE_FUNCTION rangetable entry.  If it represents a call of a
    5804              :  * function that can be inlined, expand the function and return the
    5805              :  * substitute Query structure.  Otherwise, return NULL.
    5806              :  *
    5807              :  * We assume that the RTE's expression has already been put through
    5808              :  * eval_const_expressions(), which among other things will take care of
    5809              :  * default arguments and named-argument notation.
    5810              :  *
    5811              :  * This has a good deal of similarity to inline_function(), but that's
    5812              :  * for the general-expression case, and there are enough differences to
    5813              :  * justify separate functions.
    5814              :  */
    5815              : Query *
    5816        35956 : inline_function_in_from(PlannerInfo *root, RangeTblEntry *rte)
    5817              : {
    5818              :     RangeTblFunction *rtfunc;
    5819              :     FuncExpr   *fexpr;
    5820              :     Oid         func_oid;
    5821              :     HeapTuple   func_tuple;
    5822              :     Form_pg_proc funcform;
    5823              :     MemoryContext oldcxt;
    5824              :     MemoryContext mycxt;
    5825              :     Datum       tmp;
    5826              :     char       *src;
    5827              :     inline_error_callback_arg callback_arg;
    5828              :     ErrorContextCallback sqlerrcontext;
    5829        35956 :     Query      *querytree = NULL;
    5830              : 
    5831              :     Assert(rte->rtekind == RTE_FUNCTION);
    5832              : 
    5833              :     /*
    5834              :      * Guard against infinite recursion during expansion by checking for stack
    5835              :      * overflow.  (There's no need to do more.)
    5836              :      */
    5837        35956 :     check_stack_depth();
    5838              : 
    5839              :     /* Fail if the RTE has ORDINALITY - we don't implement that here. */
    5840        35956 :     if (rte->funcordinality)
    5841          774 :         return NULL;
    5842              : 
    5843              :     /* Fail if RTE isn't a single, simple FuncExpr */
    5844        35182 :     if (list_length(rte->functions) != 1)
    5845           57 :         return NULL;
    5846        35125 :     rtfunc = (RangeTblFunction *) linitial(rte->functions);
    5847              : 
    5848        35125 :     if (!IsA(rtfunc->funcexpr, FuncExpr))
    5849          345 :         return NULL;
    5850        34780 :     fexpr = (FuncExpr *) rtfunc->funcexpr;
    5851              : 
    5852        34780 :     func_oid = fexpr->funcid;
    5853              : 
    5854              :     /*
    5855              :      * Refuse to inline if the arguments contain any volatile functions or
    5856              :      * sub-selects.  Volatile functions are rejected because inlining may
    5857              :      * result in the arguments being evaluated multiple times, risking a
    5858              :      * change in behavior.  Sub-selects are rejected partly for implementation
    5859              :      * reasons (pushing them down another level might change their behavior)
    5860              :      * and partly because they're likely to be expensive and so multiple
    5861              :      * evaluation would be bad.
    5862              :      */
    5863        69442 :     if (contain_volatile_functions((Node *) fexpr->args) ||
    5864        34662 :         contain_subplans((Node *) fexpr->args))
    5865          283 :         return NULL;
    5866              : 
    5867              :     /* Check permission to call function (fail later, if not) */
    5868        34497 :     if (object_aclcheck(ProcedureRelationId, func_oid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
    5869            6 :         return NULL;
    5870              : 
    5871              :     /* Check whether a plugin wants to hook function entry/exit */
    5872        34491 :     if (FmgrHookIsNeeded(func_oid))
    5873            0 :         return NULL;
    5874              : 
    5875              :     /*
    5876              :      * OK, let's take a look at the function's pg_proc entry.
    5877              :      */
    5878        34491 :     func_tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(func_oid));
    5879        34491 :     if (!HeapTupleIsValid(func_tuple))
    5880            0 :         elog(ERROR, "cache lookup failed for function %u", func_oid);
    5881        34491 :     funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
    5882              : 
    5883              :     /*
    5884              :      * If the function SETs any configuration parameters, inlining would cause
    5885              :      * us to miss making those changes.
    5886              :      */
    5887        34491 :     if (!heap_attisnull(func_tuple, Anum_pg_proc_proconfig, NULL))
    5888              :     {
    5889            8 :         ReleaseSysCache(func_tuple);
    5890            8 :         return NULL;
    5891              :     }
    5892              : 
    5893              :     /*
    5894              :      * Make a temporary memory context, so that we don't leak all the stuff
    5895              :      * that parsing and rewriting might create.  If we succeed, we'll copy
    5896              :      * just the finished query tree back up to the caller's context.
    5897              :      */
    5898        34483 :     mycxt = AllocSetContextCreate(CurrentMemoryContext,
    5899              :                                   "inline_function_in_from",
    5900              :                                   ALLOCSET_DEFAULT_SIZES);
    5901        34483 :     oldcxt = MemoryContextSwitchTo(mycxt);
    5902              : 
    5903              :     /* Fetch the function body */
    5904        34483 :     tmp = SysCacheGetAttrNotNull(PROCOID, func_tuple, Anum_pg_proc_prosrc);
    5905        34483 :     src = TextDatumGetCString(tmp);
    5906              : 
    5907              :     /*
    5908              :      * If the function has an attached support function that can handle
    5909              :      * SupportRequestInlineInFrom, then attempt to inline with that.
    5910              :      */
    5911        34483 :     if (funcform->prosupport)
    5912              :     {
    5913              :         SupportRequestInlineInFrom req;
    5914              : 
    5915        12898 :         req.type = T_SupportRequestInlineInFrom;
    5916        12898 :         req.root = root;
    5917        12898 :         req.rtfunc = rtfunc;
    5918        12898 :         req.proc = func_tuple;
    5919              : 
    5920              :         querytree = (Query *)
    5921        12898 :             DatumGetPointer(OidFunctionCall1(funcform->prosupport,
    5922              :                                              PointerGetDatum(&req)));
    5923              :     }
    5924              : 
    5925              :     /*
    5926              :      * Setup error traceback support for ereport().  This is so that we can
    5927              :      * finger the function that bad information came from.  We don't install
    5928              :      * this while running the support function, since it'd be likely to do the
    5929              :      * wrong thing: any parse errors reported during that are very likely not
    5930              :      * against the raw function source text.
    5931              :      */
    5932        34483 :     callback_arg.proname = NameStr(funcform->proname);
    5933        34483 :     callback_arg.prosrc = src;
    5934              : 
    5935        34483 :     sqlerrcontext.callback = sql_inline_error_callback;
    5936        34483 :     sqlerrcontext.arg = &callback_arg;
    5937        34483 :     sqlerrcontext.previous = error_context_stack;
    5938        34483 :     error_context_stack = &sqlerrcontext;
    5939              : 
    5940              :     /*
    5941              :      * If SupportRequestInlineInFrom didn't work, try our built-in inlining
    5942              :      * mechanism.
    5943              :      */
    5944        34483 :     if (!querytree)
    5945        34463 :         querytree = inline_sql_function_in_from(root, rtfunc, fexpr,
    5946              :                                                 func_tuple, funcform, src);
    5947              : 
    5948        34479 :     if (!querytree)
    5949        34274 :         goto fail;              /* no luck there either, fail */
    5950              : 
    5951              :     /*
    5952              :      * The result had better be a SELECT Query.
    5953              :      */
    5954              :     Assert(IsA(querytree, Query));
    5955              :     Assert(querytree->commandType == CMD_SELECT);
    5956              : 
    5957              :     /*
    5958              :      * Looks good --- substitute parameters into the query.
    5959              :      */
    5960          205 :     querytree = substitute_actual_parameters_in_from(querytree,
    5961          205 :                                                      funcform->pronargs,
    5962              :                                                      fexpr->args);
    5963              : 
    5964              :     /*
    5965              :      * Copy the modified query out of the temporary memory context, and clean
    5966              :      * up.
    5967              :      */
    5968          205 :     MemoryContextSwitchTo(oldcxt);
    5969              : 
    5970          205 :     querytree = copyObject(querytree);
    5971              : 
    5972          205 :     MemoryContextDelete(mycxt);
    5973          205 :     error_context_stack = sqlerrcontext.previous;
    5974          205 :     ReleaseSysCache(func_tuple);
    5975              : 
    5976              :     /*
    5977              :      * We don't have to fix collations here because the upper query is already
    5978              :      * parsed, ie, the collations in the RTE are what count.
    5979              :      */
    5980              : 
    5981              :     /*
    5982              :      * Since there is now no trace of the function in the plan tree, we must
    5983              :      * explicitly record the plan's dependency on the function.
    5984              :      */
    5985          205 :     record_plan_function_dependency(root, func_oid);
    5986              : 
    5987              :     /*
    5988              :      * We must also notice if the inserted query adds a dependency on the
    5989              :      * calling role due to RLS quals.
    5990              :      */
    5991          205 :     if (querytree->hasRowSecurity)
    5992           60 :         root->glob->dependsOnRole = true;
    5993              : 
    5994          205 :     return querytree;
    5995              : 
    5996              :     /* Here if func is not inlinable: release temp memory and return NULL */
    5997        34274 : fail:
    5998        34274 :     MemoryContextSwitchTo(oldcxt);
    5999        34274 :     MemoryContextDelete(mycxt);
    6000        34274 :     error_context_stack = sqlerrcontext.previous;
    6001        34274 :     ReleaseSysCache(func_tuple);
    6002              : 
    6003        34274 :     return NULL;
    6004              : }
    6005              : 
    6006              : /*
    6007              :  * inline_sql_function_in_from
    6008              :  *
    6009              :  * This implements inline_function_in_from for SQL-language functions.
    6010              :  * Returns NULL if the function couldn't be inlined.
    6011              :  *
    6012              :  * The division of labor between here and inline_function_in_from is based
    6013              :  * on the rule that inline_function_in_from should make all checks that are
    6014              :  * certain to be required in both this case and the support-function case.
    6015              :  * Support functions might also want to make checks analogous to the ones
    6016              :  * made here, but then again they might not, or they might just assume that
    6017              :  * the function they are attached to can validly be inlined.
    6018              :  */
    6019              : static Query *
    6020        34463 : inline_sql_function_in_from(PlannerInfo *root,
    6021              :                             RangeTblFunction *rtfunc,
    6022              :                             FuncExpr *fexpr,
    6023              :                             HeapTuple func_tuple,
    6024              :                             Form_pg_proc funcform,
    6025              :                             const char *src)
    6026              : {
    6027              :     Datum       sqlbody;
    6028              :     bool        isNull;
    6029              :     List       *querytree_list;
    6030              :     Query      *querytree;
    6031              :     TypeFuncClass functypclass;
    6032              :     TupleDesc   rettupdesc;
    6033              : 
    6034              :     /*
    6035              :      * The function must be declared to return a set, else inlining would
    6036              :      * change the results if the contained SELECT didn't return exactly one
    6037              :      * row.
    6038              :      */
    6039        34463 :     if (!fexpr->funcretset)
    6040         5725 :         return NULL;
    6041              : 
    6042              :     /*
    6043              :      * Forget it if the function is not SQL-language or has other showstopper
    6044              :      * properties.  In particular it mustn't be declared STRICT, since we
    6045              :      * couldn't enforce that.  It also mustn't be VOLATILE, because that is
    6046              :      * supposed to cause it to be executed with its own snapshot, rather than
    6047              :      * sharing the snapshot of the calling query.  We also disallow returning
    6048              :      * SETOF VOID, because inlining would result in exposing the actual result
    6049              :      * of the function's last SELECT, which should not happen in that case.
    6050              :      * (Rechecking prokind, proretset, and pronargs is just paranoia.)
    6051              :      */
    6052        28738 :     if (funcform->prolang != SQLlanguageId ||
    6053          768 :         funcform->prokind != PROKIND_FUNCTION ||
    6054          768 :         funcform->proisstrict ||
    6055          718 :         funcform->provolatile == PROVOLATILE_VOLATILE ||
    6056          194 :         funcform->prorettype == VOIDOID ||
    6057          189 :         funcform->prosecdef ||
    6058          189 :         !funcform->proretset ||
    6059          189 :         list_length(fexpr->args) != funcform->pronargs)
    6060        28549 :         return NULL;
    6061              : 
    6062              :     /* If we have prosqlbody, pay attention to that not prosrc */
    6063          189 :     sqlbody = SysCacheGetAttr(PROCOID,
    6064              :                               func_tuple,
    6065              :                               Anum_pg_proc_prosqlbody,
    6066              :                               &isNull);
    6067          189 :     if (!isNull)
    6068              :     {
    6069              :         Node       *n;
    6070              : 
    6071           10 :         n = stringToNode(TextDatumGetCString(sqlbody));
    6072           10 :         if (IsA(n, List))
    6073           10 :             querytree_list = linitial_node(List, castNode(List, n));
    6074              :         else
    6075            0 :             querytree_list = list_make1(n);
    6076           10 :         if (list_length(querytree_list) != 1)
    6077            0 :             return NULL;
    6078           10 :         querytree = linitial(querytree_list);
    6079              : 
    6080              :         /* Acquire necessary locks, then apply rewriter. */
    6081           10 :         AcquireRewriteLocks(querytree, true, false);
    6082           10 :         querytree_list = pg_rewrite_query(querytree);
    6083           10 :         if (list_length(querytree_list) != 1)
    6084            0 :             return NULL;
    6085           10 :         querytree = linitial(querytree_list);
    6086              :     }
    6087              :     else
    6088              :     {
    6089              :         SQLFunctionParseInfoPtr pinfo;
    6090              :         List       *raw_parsetree_list;
    6091              : 
    6092              :         /*
    6093              :          * Set up to handle parameters while parsing the function body.  We
    6094              :          * can use the FuncExpr just created as the input for
    6095              :          * prepare_sql_fn_parse_info.
    6096              :          */
    6097          179 :         pinfo = prepare_sql_fn_parse_info(func_tuple,
    6098              :                                           (Node *) fexpr,
    6099              :                                           fexpr->inputcollid);
    6100              : 
    6101              :         /*
    6102              :          * Parse, analyze, and rewrite (unlike inline_function(), we can't
    6103              :          * skip rewriting here).  We can fail as soon as we find more than one
    6104              :          * query, though.
    6105              :          */
    6106          179 :         raw_parsetree_list = pg_parse_query(src);
    6107          179 :         if (list_length(raw_parsetree_list) != 1)
    6108            0 :             return NULL;
    6109              : 
    6110          179 :         querytree_list = pg_analyze_and_rewrite_withcb(linitial(raw_parsetree_list),
    6111              :                                                        src,
    6112              :                                                        (ParserSetupHook) sql_fn_parser_setup,
    6113              :                                                        pinfo, NULL);
    6114          179 :         if (list_length(querytree_list) != 1)
    6115            0 :             return NULL;
    6116          179 :         querytree = linitial(querytree_list);
    6117              :     }
    6118              : 
    6119              :     /*
    6120              :      * Also resolve the actual function result tupdesc, if composite.  If we
    6121              :      * have a coldeflist, believe that; otherwise use get_expr_result_type.
    6122              :      * (This logic should match ExecInitFunctionScan.)
    6123              :      */
    6124          189 :     if (rtfunc->funccolnames != NIL)
    6125              :     {
    6126           19 :         functypclass = TYPEFUNC_RECORD;
    6127           19 :         rettupdesc = BuildDescFromLists(rtfunc->funccolnames,
    6128           19 :                                         rtfunc->funccoltypes,
    6129           19 :                                         rtfunc->funccoltypmods,
    6130           19 :                                         rtfunc->funccolcollations);
    6131              :     }
    6132              :     else
    6133          170 :         functypclass = get_expr_result_type((Node *) fexpr, NULL, &rettupdesc);
    6134              : 
    6135              :     /*
    6136              :      * The single command must be a plain SELECT.
    6137              :      */
    6138          189 :     if (!IsA(querytree, Query) ||
    6139          189 :         querytree->commandType != CMD_SELECT)
    6140            0 :         return NULL;
    6141              : 
    6142              :     /*
    6143              :      * Make sure the function (still) returns what it's declared to.  This
    6144              :      * will raise an error if wrong, but that's okay since the function would
    6145              :      * fail at runtime anyway.  Note that check_sql_fn_retval will also insert
    6146              :      * coercions if needed to make the tlist expression(s) match the declared
    6147              :      * type of the function.  We also ask it to insert dummy NULL columns for
    6148              :      * any dropped columns in rettupdesc, so that the elements of the modified
    6149              :      * tlist match up to the attribute numbers.
    6150              :      *
    6151              :      * If the function returns a composite type, don't inline unless the check
    6152              :      * shows it's returning a whole tuple result; otherwise what it's
    6153              :      * returning is a single composite column which is not what we need.
    6154              :      */
    6155          189 :     if (!check_sql_fn_retval(list_make1(querytree_list),
    6156              :                              fexpr->funcresulttype, rettupdesc,
    6157          189 :                              funcform->prokind,
    6158           75 :                              true) &&
    6159           75 :         (functypclass == TYPEFUNC_COMPOSITE ||
    6160           75 :          functypclass == TYPEFUNC_COMPOSITE_DOMAIN ||
    6161              :          functypclass == TYPEFUNC_RECORD))
    6162            0 :         return NULL;            /* reject not-whole-tuple-result cases */
    6163              : 
    6164              :     /*
    6165              :      * check_sql_fn_retval might've inserted a projection step, but that's
    6166              :      * fine; just make sure we use the upper Query.
    6167              :      */
    6168          185 :     querytree = linitial_node(Query, querytree_list);
    6169              : 
    6170          185 :     return querytree;
    6171              : }
    6172              : 
    6173              : /*
    6174              :  * Replace Param nodes by appropriate actual parameters
    6175              :  *
    6176              :  * This is just enough different from substitute_actual_parameters()
    6177              :  * that it needs its own code.
    6178              :  */
    6179              : static Query *
    6180          205 : substitute_actual_parameters_in_from(Query *expr, int nargs, List *args)
    6181              : {
    6182              :     substitute_actual_parameters_in_from_context context;
    6183              : 
    6184          205 :     context.nargs = nargs;
    6185          205 :     context.args = args;
    6186          205 :     context.sublevels_up = 1;
    6187              : 
    6188          205 :     return query_tree_mutator(expr,
    6189              :                               substitute_actual_parameters_in_from_mutator,
    6190              :                               &context,
    6191              :                               0);
    6192              : }
    6193              : 
    6194              : static Node *
    6195         7695 : substitute_actual_parameters_in_from_mutator(Node *node,
    6196              :                                              substitute_actual_parameters_in_from_context *context)
    6197              : {
    6198              :     Node       *result;
    6199              : 
    6200         7695 :     if (node == NULL)
    6201         4480 :         return NULL;
    6202         3215 :     if (IsA(node, Query))
    6203              :     {
    6204          125 :         context->sublevels_up++;
    6205          125 :         result = (Node *) query_tree_mutator((Query *) node,
    6206              :                                              substitute_actual_parameters_in_from_mutator,
    6207              :                                              context,
    6208              :                                              0);
    6209          125 :         context->sublevels_up--;
    6210          125 :         return result;
    6211              :     }
    6212         3090 :     if (IsA(node, Param))
    6213              :     {
    6214           95 :         Param      *param = (Param *) node;
    6215              : 
    6216           95 :         if (param->paramkind == PARAM_EXTERN)
    6217              :         {
    6218           95 :             if (param->paramid <= 0 || param->paramid > context->nargs)
    6219            0 :                 elog(ERROR, "invalid paramid: %d", param->paramid);
    6220              : 
    6221              :             /*
    6222              :              * Since the parameter is being inserted into a subquery, we must
    6223              :              * adjust levels.
    6224              :              */
    6225           95 :             result = copyObject(list_nth(context->args, param->paramid - 1));
    6226           95 :             IncrementVarSublevelsUp(result, context->sublevels_up, 0);
    6227           95 :             return result;
    6228              :         }
    6229              :     }
    6230         2995 :     return expression_tree_mutator(node,
    6231              :                                    substitute_actual_parameters_in_from_mutator,
    6232              :                                    context);
    6233              : }
    6234              : 
    6235              : /*
    6236              :  * pull_paramids
    6237              :  *      Returns a Bitmapset containing the paramids of all Params in 'expr'.
    6238              :  */
    6239              : Bitmapset *
    6240         1536 : pull_paramids(Expr *expr)
    6241              : {
    6242         1536 :     Bitmapset  *result = NULL;
    6243              : 
    6244         1536 :     (void) pull_paramids_walker((Node *) expr, &result);
    6245              : 
    6246         1536 :     return result;
    6247              : }
    6248              : 
    6249              : static bool
    6250         3436 : pull_paramids_walker(Node *node, Bitmapset **context)
    6251              : {
    6252         3436 :     if (node == NULL)
    6253           10 :         return false;
    6254         3426 :     if (IsA(node, Param))
    6255              :     {
    6256         1589 :         Param      *param = (Param *) node;
    6257              : 
    6258         1589 :         *context = bms_add_member(*context, param->paramid);
    6259         1589 :         return false;
    6260              :     }
    6261         1837 :     return expression_tree_walker(node, pull_paramids_walker, context);
    6262              : }
    6263              : 
    6264              : /*
    6265              :  * Build ScalarArrayOpExpr on top of 'exprs.' 'haveNonConst' indicates
    6266              :  * whether at least one of the expressions is not Const.  When it's false,
    6267              :  * the array constant is built directly; otherwise, we have to build a child
    6268              :  * ArrayExpr. The 'exprs' list gets freed if not directly used in the output
    6269              :  * expression tree.
    6270              :  */
    6271              : ScalarArrayOpExpr *
    6272         2933 : make_SAOP_expr(Oid oper, Node *leftexpr, Oid coltype, Oid arraycollid,
    6273              :                Oid inputcollid, List *exprs, bool haveNonConst)
    6274              : {
    6275         2933 :     Node       *arrayNode = NULL;
    6276         2933 :     ScalarArrayOpExpr *saopexpr = NULL;
    6277         2933 :     Oid         arraytype = get_array_type(coltype);
    6278              : 
    6279         2933 :     if (!OidIsValid(arraytype))
    6280            0 :         return NULL;
    6281              : 
    6282              :     /*
    6283              :      * Assemble an array from the list of constants.  It seems more profitable
    6284              :      * to build a const array.  But in the presence of other nodes, we don't
    6285              :      * have a specific value here and must employ an ArrayExpr instead.
    6286              :      */
    6287         2933 :     if (haveNonConst)
    6288              :     {
    6289           84 :         ArrayExpr  *arrayExpr = makeNode(ArrayExpr);
    6290              : 
    6291              :         /* array_collid will be set by parse_collate.c */
    6292           84 :         arrayExpr->element_typeid = coltype;
    6293           84 :         arrayExpr->array_typeid = arraytype;
    6294           84 :         arrayExpr->multidims = false;
    6295           84 :         arrayExpr->elements = exprs;
    6296           84 :         arrayExpr->location = -1;
    6297              : 
    6298           84 :         arrayNode = (Node *) arrayExpr;
    6299              :     }
    6300              :     else
    6301              :     {
    6302              :         int16       typlen;
    6303              :         bool        typbyval;
    6304              :         char        typalign;
    6305              :         Datum      *elems;
    6306              :         bool       *nulls;
    6307         2849 :         int         i = 0;
    6308              :         ArrayType  *arrayConst;
    6309         2849 :         int         dims[1] = {list_length(exprs)};
    6310         2849 :         int         lbs[1] = {1};
    6311              : 
    6312         2849 :         get_typlenbyvalalign(coltype, &typlen, &typbyval, &typalign);
    6313              : 
    6314         2849 :         elems = palloc_array(Datum, list_length(exprs));
    6315         2849 :         nulls = palloc_array(bool, list_length(exprs));
    6316        11801 :         foreach_node(Const, value, exprs)
    6317              :         {
    6318         6103 :             elems[i] = value->constvalue;
    6319         6103 :             nulls[i++] = value->constisnull;
    6320              :         }
    6321              : 
    6322         2849 :         arrayConst = construct_md_array(elems, nulls, 1, dims, lbs,
    6323              :                                         coltype, typlen, typbyval, typalign);
    6324         2849 :         arrayNode = (Node *) makeConst(arraytype, -1, arraycollid,
    6325              :                                        -1, PointerGetDatum(arrayConst),
    6326              :                                        false, false);
    6327              : 
    6328         2849 :         pfree(elems);
    6329         2849 :         pfree(nulls);
    6330         2849 :         list_free(exprs);
    6331              :     }
    6332              : 
    6333              :     /* Build the SAOP expression node */
    6334         2933 :     saopexpr = makeNode(ScalarArrayOpExpr);
    6335         2933 :     saopexpr->opno = oper;
    6336         2933 :     saopexpr->opfuncid = get_opcode(oper);
    6337         2933 :     saopexpr->hashfuncid = InvalidOid;
    6338         2933 :     saopexpr->negfuncid = InvalidOid;
    6339         2933 :     saopexpr->useOr = true;
    6340         2933 :     saopexpr->inputcollid = inputcollid;
    6341         2933 :     saopexpr->args = list_make2(leftexpr, arrayNode);
    6342         2933 :     saopexpr->location = -1;
    6343              : 
    6344         2933 :     return saopexpr;
    6345              : }
        

Generated by: LCOV version 2.0-1