LCOV - differential code coverage report
Current view: top level - src/backend/optimizer/util - clauses.c (source / functions) Coverage Total Hit UNC LBC UIC UBC GBC GIC GNC CBC EUB ECB DUB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 86.7 % 1704 1477 12 56 138 21 56 942 38 441 147 959 3 21
Current Date: 2023-04-08 17:13:01 Functions: 100.0 % 71 71 71 71
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 [..60] days: 65.2 % 23 15 8 15 1 1
Legend: Lines: hit not hit (60,120] days: 100.0 % 2 2 2
(120,180] days: 81.0 % 21 17 4 17
(180,240] days: 100.0 % 4 4 4 2
(240..) days: 87.0 % 1654 1439 56 138 21 56 942 441 145 931
Function coverage date bins:
(240..) days: 50.7 % 140 71 71 69

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

Generated by: LCOV version v1.16-55-g56c0a2a