LCOV - differential code coverage report
Current view: top level - src/backend/optimizer/util - clauses.c (source / functions) Coverage Total Hit UNC LBC UBC GBC GIC GNC CBC DCB
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 86.8 % 1733 1505 2 1 225 3 3 13 1486 5
Current Date: 2024-04-14 14:21:10 Functions: 100.0 % 73 73 3 70
Baseline: 16@8cea358b128 Branches: 69.1 % 1329 919 4 2 404 4 12 903
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed [..60] days: 87.5 % 16 14 2 10 4
(120,180] days: 100.0 % 10 10 10
(240..) days: 86.8 % 1707 1481 1 225 3 3 3 1472
Function coverage date bins:
(120,180] days: 100.0 % 2 2 2
(240..) days: 100.0 % 71 71 3 68
Branch coverage date bins:
[..60] days: 75.0 % 12 9 3 7 2
(240..) days: 69.1 % 1317 910 1 2 404 4 5 901

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

Generated by: LCOV version 2.1-beta2-3-g6141622