LCOV - differential code coverage report
Current view: top level - src/backend/executor - functions.c (source / functions) Coverage Total Hit UBC CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 94.9 % 750 712 38 712
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 33 33 33
Baseline: lcov-20250906-005545-baseline Branches: 79.2 % 530 420 110 420
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 8 8 8
(30,360] days: 96.3 % 353 340 13 340
(360..) days: 93.6 % 389 364 25 364
Function coverage date bins:
(30,360] days: 100.0 % 13 13 13
(360..) days: 100.0 % 20 20 20
Branch coverage date bins:
(7,30] days: 100.0 % 2 2 2
(30,360] days: 77.4 % 234 181 53 181
(360..) days: 80.6 % 294 237 57 237

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * functions.c
                                  4                 :                :  *    Execution of SQL-language functions
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
                                  7                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  8                 :                :  *
                                  9                 :                :  *
                                 10                 :                :  * IDENTIFICATION
                                 11                 :                :  *    src/backend/executor/functions.c
                                 12                 :                :  *
                                 13                 :                :  *-------------------------------------------------------------------------
                                 14                 :                :  */
                                 15                 :                : #include "postgres.h"
                                 16                 :                : 
                                 17                 :                : #include "access/htup_details.h"
                                 18                 :                : #include "access/xact.h"
                                 19                 :                : #include "catalog/pg_proc.h"
                                 20                 :                : #include "catalog/pg_type.h"
                                 21                 :                : #include "executor/functions.h"
                                 22                 :                : #include "funcapi.h"
                                 23                 :                : #include "miscadmin.h"
                                 24                 :                : #include "nodes/makefuncs.h"
                                 25                 :                : #include "nodes/nodeFuncs.h"
                                 26                 :                : #include "parser/parse_coerce.h"
                                 27                 :                : #include "parser/parse_collate.h"
                                 28                 :                : #include "parser/parse_func.h"
                                 29                 :                : #include "rewrite/rewriteHandler.h"
                                 30                 :                : #include "storage/proc.h"
                                 31                 :                : #include "tcop/utility.h"
                                 32                 :                : #include "utils/builtins.h"
                                 33                 :                : #include "utils/datum.h"
                                 34                 :                : #include "utils/funccache.h"
                                 35                 :                : #include "utils/lsyscache.h"
                                 36                 :                : #include "utils/memutils.h"
                                 37                 :                : #include "utils/plancache.h"
                                 38                 :                : #include "utils/snapmgr.h"
                                 39                 :                : #include "utils/syscache.h"
                                 40                 :                : 
                                 41                 :                : 
                                 42                 :                : /*
                                 43                 :                :  * Specialized DestReceiver for collecting query output in a SQL function
                                 44                 :                :  */
                                 45                 :                : typedef struct
                                 46                 :                : {
                                 47                 :                :     DestReceiver pub;           /* publicly-known function pointers */
                                 48                 :                :     Tuplestorestate *tstore;    /* where to put result tuples, or NULL */
                                 49                 :                :     JunkFilter *filter;         /* filter to convert tuple type */
                                 50                 :                : } DR_sqlfunction;
                                 51                 :                : 
                                 52                 :                : /*
                                 53                 :                :  * We have an execution_state record for each query in a function.  Each
                                 54                 :                :  * record references a plantree for its query.  If the query is currently in
                                 55                 :                :  * F_EXEC_RUN state then there's a QueryDesc too.
                                 56                 :                :  *
                                 57                 :                :  * The "next" fields chain together all the execution_state records generated
                                 58                 :                :  * from a single original parsetree.  (There will only be more than one in
                                 59                 :                :  * case of rule expansion of the original parsetree.)  The chain structure is
                                 60                 :                :  * quite vestigial at this point, because we allocate the records in an array
                                 61                 :                :  * for ease of memory management.  But we'll get rid of it some other day.
                                 62                 :                :  */
                                 63                 :                : typedef enum
                                 64                 :                : {
                                 65                 :                :     F_EXEC_START, F_EXEC_RUN, F_EXEC_DONE,
                                 66                 :                : } ExecStatus;
                                 67                 :                : 
                                 68                 :                : typedef struct execution_state
                                 69                 :                : {
                                 70                 :                :     struct execution_state *next;
                                 71                 :                :     ExecStatus  status;
                                 72                 :                :     bool        setsResult;     /* true if this query produces func's result */
                                 73                 :                :     bool        lazyEval;       /* true if should fetch one row at a time */
                                 74                 :                :     PlannedStmt *stmt;          /* plan for this query */
                                 75                 :                :     QueryDesc  *qd;             /* null unless status == RUN */
                                 76                 :                : } execution_state;
                                 77                 :                : 
                                 78                 :                : 
                                 79                 :                : /*
                                 80                 :                :  * Data associated with a SQL-language function is kept in two main
                                 81                 :                :  * data structures:
                                 82                 :                :  *
                                 83                 :                :  * 1. SQLFunctionHashEntry is a long-lived (potentially session-lifespan)
                                 84                 :                :  * struct that holds all the info we need out of the function's pg_proc row.
                                 85                 :                :  * In addition it holds pointers to CachedPlanSource(s) that manage creation
                                 86                 :                :  * of plans for the query(s) within the function.  A SQLFunctionHashEntry is
                                 87                 :                :  * potentially shared across multiple concurrent executions of the function,
                                 88                 :                :  * so it must contain no execution-specific state; but its use_count must
                                 89                 :                :  * reflect the number of SQLFunctionCache structs pointing at it.
                                 90                 :                :  * If the function's pg_proc row is updated, we throw away and regenerate
                                 91                 :                :  * the SQLFunctionHashEntry and subsidiary data.  (Also note that if the
                                 92                 :                :  * function is polymorphic or used as a trigger, there is a separate
                                 93                 :                :  * SQLFunctionHashEntry for each usage, so that we need consider only one
                                 94                 :                :  * set of relevant data types.)  The struct itself is in memory managed by
                                 95                 :                :  * funccache.c, and its subsidiary data is kept in one of two contexts:
                                 96                 :                :  *  * pcontext ("parse context") holds the raw parse trees or Query trees
                                 97                 :                :  *    that we read from the pg_proc row.  These will be converted to
                                 98                 :                :  *    CachedPlanSources as they are needed.  Once the last one is converted,
                                 99                 :                :  *    pcontext can be freed.
                                100                 :                :  *  * hcontext ("hash context") holds everything else belonging to the
                                101                 :                :  *    SQLFunctionHashEntry.
                                102                 :                :  *
                                103                 :                :  * 2. SQLFunctionCache is subsidiary data for a single FmgrInfo struct.
                                104                 :                :  * It is pointed to by the fn_extra field of the FmgrInfo struct, and is
                                105                 :                :  * always allocated in the FmgrInfo's fn_mcxt.  It holds a reference to
                                106                 :                :  * the CachedPlan for the current query, and other execution-specific data.
                                107                 :                :  * A few subsidiary items such as the ParamListInfo object are also kept
                                108                 :                :  * directly in fn_mcxt (which is also called fcontext here).  But most
                                109                 :                :  * subsidiary data is in jfcontext or subcontext.
                                110                 :                :  */
                                111                 :                : 
                                112                 :                : typedef struct SQLFunctionHashEntry
                                113                 :                : {
                                114                 :                :     CachedFunction cfunc;       /* fields managed by funccache.c */
                                115                 :                : 
                                116                 :                :     char       *fname;          /* function name (for error msgs) */
                                117                 :                :     char       *src;            /* function body text (for error msgs) */
                                118                 :                : 
                                119                 :                :     SQLFunctionParseInfoPtr pinfo;  /* data for parser callback hooks */
                                120                 :                :     int16      *argtyplen;      /* lengths of the input argument types */
                                121                 :                : 
                                122                 :                :     Oid         rettype;        /* actual return type */
                                123                 :                :     int16       typlen;         /* length of the return type */
                                124                 :                :     bool        typbyval;       /* true if return type is pass by value */
                                125                 :                :     bool        returnsSet;     /* true if returning multiple rows */
                                126                 :                :     bool        returnsTuple;   /* true if returning whole tuple result */
                                127                 :                :     bool        readonly_func;  /* true to run in "read only" mode */
                                128                 :                :     char        prokind;        /* prokind from pg_proc row */
                                129                 :                : 
                                130                 :                :     TupleDesc   rettupdesc;     /* result tuple descriptor */
                                131                 :                : 
                                132                 :                :     List       *source_list;    /* RawStmts or Queries read from pg_proc */
                                133                 :                :     int         num_queries;    /* original length of source_list */
                                134                 :                :     bool        raw_source;     /* true if source_list contains RawStmts */
                                135                 :                : 
                                136                 :                :     List       *plansource_list;    /* CachedPlanSources for fn's queries */
                                137                 :                : 
                                138                 :                :     MemoryContext pcontext;     /* memory context holding source_list */
                                139                 :                :     MemoryContext hcontext;     /* memory context holding all else */
                                140                 :                : } SQLFunctionHashEntry;
                                141                 :                : 
                                142                 :                : typedef struct SQLFunctionCache
                                143                 :                : {
                                144                 :                :     SQLFunctionHashEntry *func; /* associated SQLFunctionHashEntry */
                                145                 :                : 
                                146                 :                :     bool        active;         /* are we executing this cache entry? */
                                147                 :                :     bool        lazyEvalOK;     /* true if lazyEval is safe */
                                148                 :                :     bool        shutdown_reg;   /* true if registered shutdown callback */
                                149                 :                :     bool        lazyEval;       /* true if using lazyEval for result query */
                                150                 :                :     bool        randomAccess;   /* true if tstore needs random access */
                                151                 :                :     bool        ownSubcontext;  /* is subcontext really a separate context? */
                                152                 :                : 
                                153                 :                :     ParamListInfo paramLI;      /* Param list representing current args */
                                154                 :                : 
                                155                 :                :     Tuplestorestate *tstore;    /* where we accumulate result for a SRF */
                                156                 :                :     MemoryContext tscontext;    /* memory context that tstore should be in */
                                157                 :                : 
                                158                 :                :     JunkFilter *junkFilter;     /* will be NULL if function returns VOID */
                                159                 :                :     int         jf_generation;  /* tracks whether junkFilter is up-to-date */
                                160                 :                : 
                                161                 :                :     /*
                                162                 :                :      * While executing a particular query within the function, cplan is the
                                163                 :                :      * CachedPlan we've obtained for that query, and eslist is a chain of
                                164                 :                :      * execution_state records for the individual plans within the CachedPlan.
                                165                 :                :      * If eslist is not NULL at entry to fmgr_sql, then we are resuming
                                166                 :                :      * execution of a lazyEval-mode set-returning function.
                                167                 :                :      *
                                168                 :                :      * next_query_index is the 0-based index of the next CachedPlanSource to
                                169                 :                :      * get a CachedPlan from.
                                170                 :                :      */
                                171                 :                :     CachedPlan *cplan;          /* Plan for current query, if any */
                                172                 :                :     ResourceOwner cowner;       /* CachedPlan is registered with this owner */
                                173                 :                :     int         next_query_index;   /* index of next CachedPlanSource to run */
                                174                 :                : 
                                175                 :                :     execution_state *eslist;    /* chain of execution_state records */
                                176                 :                :     execution_state *esarray;   /* storage for eslist */
                                177                 :                :     int         esarray_len;    /* allocated length of esarray[] */
                                178                 :                : 
                                179                 :                :     /* if positive, this is the 1-based index of the query we're processing */
                                180                 :                :     int         error_query_index;
                                181                 :                : 
                                182                 :                :     MemoryContext fcontext;     /* memory context holding this struct and all
                                183                 :                :                                  * subsidiary data */
                                184                 :                :     MemoryContext jfcontext;    /* subsidiary memory context holding
                                185                 :                :                                  * junkFilter, result slot, and related data */
                                186                 :                :     MemoryContext subcontext;   /* subsidiary memory context for sub-executor */
                                187                 :                : 
                                188                 :                :     /* Callback to release our use-count on the SQLFunctionHashEntry */
                                189                 :                :     MemoryContextCallback mcb;
                                190                 :                : } SQLFunctionCache;
                                191                 :                : 
                                192                 :                : typedef SQLFunctionCache *SQLFunctionCachePtr;
                                193                 :                : 
                                194                 :                : 
                                195                 :                : /* non-export function prototypes */
                                196                 :                : static Node *sql_fn_param_ref(ParseState *pstate, ParamRef *pref);
                                197                 :                : static Node *sql_fn_post_column_ref(ParseState *pstate,
                                198                 :                :                                     ColumnRef *cref, Node *var);
                                199                 :                : static Node *sql_fn_make_param(SQLFunctionParseInfoPtr pinfo,
                                200                 :                :                                int paramno, int location);
                                201                 :                : static Node *sql_fn_resolve_param_name(SQLFunctionParseInfoPtr pinfo,
                                202                 :                :                                        const char *paramname, int location);
                                203                 :                : static SQLFunctionCache *init_sql_fcache(FunctionCallInfo fcinfo,
                                204                 :                :                                          bool lazyEvalOK);
                                205                 :                : static bool init_execution_state(SQLFunctionCachePtr fcache);
                                206                 :                : static void prepare_next_query(SQLFunctionHashEntry *func);
                                207                 :                : static void sql_compile_callback(FunctionCallInfo fcinfo,
                                208                 :                :                                  HeapTuple procedureTuple,
                                209                 :                :                                  const CachedFunctionHashKey *hashkey,
                                210                 :                :                                  CachedFunction *cfunc,
                                211                 :                :                                  bool forValidator);
                                212                 :                : static void sql_delete_callback(CachedFunction *cfunc);
                                213                 :                : static void sql_postrewrite_callback(List *querytree_list, void *arg);
                                214                 :                : static void postquel_start(execution_state *es, SQLFunctionCachePtr fcache);
                                215                 :                : static bool postquel_getnext(execution_state *es, SQLFunctionCachePtr fcache);
                                216                 :                : static void postquel_end(execution_state *es, SQLFunctionCachePtr fcache);
                                217                 :                : static void postquel_sub_params(SQLFunctionCachePtr fcache,
                                218                 :                :                                 FunctionCallInfo fcinfo);
                                219                 :                : static Datum postquel_get_single_result(TupleTableSlot *slot,
                                220                 :                :                                         FunctionCallInfo fcinfo,
                                221                 :                :                                         SQLFunctionCachePtr fcache);
                                222                 :                : static void sql_compile_error_callback(void *arg);
                                223                 :                : static void sql_exec_error_callback(void *arg);
                                224                 :                : static void ShutdownSQLFunction(Datum arg);
                                225                 :                : static void RemoveSQLFunctionCache(void *arg);
                                226                 :                : static void check_sql_fn_statement(List *queryTreeList);
                                227                 :                : static bool check_sql_stmt_retval(List *queryTreeList,
                                228                 :                :                                   Oid rettype, TupleDesc rettupdesc,
                                229                 :                :                                   char prokind, bool insertDroppedCols);
                                230                 :                : static bool coerce_fn_result_column(TargetEntry *src_tle,
                                231                 :                :                                     Oid res_type, int32 res_typmod,
                                232                 :                :                                     bool tlist_is_modifiable,
                                233                 :                :                                     List **upper_tlist,
                                234                 :                :                                     bool *upper_tlist_nontrivial);
                                235                 :                : static List *get_sql_fn_result_tlist(List *queryTreeList);
                                236                 :                : static void sqlfunction_startup(DestReceiver *self, int operation, TupleDesc typeinfo);
                                237                 :                : static bool sqlfunction_receive(TupleTableSlot *slot, DestReceiver *self);
                                238                 :                : static void sqlfunction_shutdown(DestReceiver *self);
                                239                 :                : static void sqlfunction_destroy(DestReceiver *self);
                                240                 :                : 
                                241                 :                : 
                                242                 :                : /*
                                243                 :                :  * Prepare the SQLFunctionParseInfo struct for parsing a SQL function body
                                244                 :                :  *
                                245                 :                :  * This includes resolving actual types of polymorphic arguments.
                                246                 :                :  *
                                247                 :                :  * call_expr can be passed as NULL, but then we will fail if there are any
                                248                 :                :  * polymorphic arguments.
                                249                 :                :  */
                                250                 :                : SQLFunctionParseInfoPtr
 5280 tgl@sss.pgh.pa.us         251                 :CBC        3503 : prepare_sql_fn_parse_info(HeapTuple procedureTuple,
                                252                 :                :                           Node *call_expr,
                                253                 :                :                           Oid inputCollation)
                                254                 :                : {
                                255                 :                :     SQLFunctionParseInfoPtr pinfo;
                                256                 :           3503 :     Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
                                257                 :                :     int         nargs;
                                258                 :                : 
                                259                 :           3503 :     pinfo = (SQLFunctionParseInfoPtr) palloc0(sizeof(SQLFunctionParseInfo));
                                260                 :                : 
                                261                 :                :     /* Function's name (only) can be used to qualify argument names */
 4963                           262                 :           3503 :     pinfo->fname = pstrdup(NameStr(procedureStruct->proname));
                                263                 :                : 
                                264                 :                :     /* Save the function's input collation */
 5280                           265                 :           3503 :     pinfo->collation = inputCollation;
                                266                 :                : 
                                267                 :                :     /*
                                268                 :                :      * Copy input argument types from the pg_proc entry, then resolve any
                                269                 :                :      * polymorphic types.
                                270                 :                :      */
                                271                 :           3503 :     pinfo->nargs = nargs = procedureStruct->pronargs;
                                272         [ +  + ]:           3503 :     if (nargs > 0)
                                273                 :                :     {
                                274                 :                :         Oid        *argOidVect;
                                275                 :                :         int         argnum;
                                276                 :                : 
                                277                 :           2877 :         argOidVect = (Oid *) palloc(nargs * sizeof(Oid));
                                278                 :           2877 :         memcpy(argOidVect,
                                279                 :           2877 :                procedureStruct->proargtypes.values,
                                280                 :                :                nargs * sizeof(Oid));
                                281                 :                : 
                                282         [ +  + ]:           7643 :         for (argnum = 0; argnum < nargs; argnum++)
                                283                 :                :         {
                                284                 :           4766 :             Oid         argtype = argOidVect[argnum];
                                285                 :                : 
                                286   [ +  +  +  +  :           4766 :             if (IsPolymorphicType(argtype))
                                     +  +  +  -  +  
                                     +  +  +  +  +  
                                     +  +  +  +  +  
                                           +  +  + ]
                                287                 :                :             {
                                288                 :           1284 :                 argtype = get_call_expr_argtype(call_expr, argnum);
                                289         [ -  + ]:           1284 :                 if (argtype == InvalidOid)
 5280 tgl@sss.pgh.pa.us         290         [ #  # ]:UBC           0 :                     ereport(ERROR,
                                291                 :                :                             (errcode(ERRCODE_DATATYPE_MISMATCH),
                                292                 :                :                              errmsg("could not determine actual type of argument declared %s",
                                293                 :                :                                     format_type_be(argOidVect[argnum]))));
 5280 tgl@sss.pgh.pa.us         294                 :CBC        1284 :                 argOidVect[argnum] = argtype;
                                295                 :                :             }
                                296                 :                :         }
                                297                 :                : 
                                298                 :           2877 :         pinfo->argtypes = argOidVect;
                                299                 :                :     }
                                300                 :                : 
                                301                 :                :     /*
                                302                 :                :      * Collect names of arguments, too, if any
                                303                 :                :      */
 4963                           304         [ +  + ]:           3503 :     if (nargs > 0)
                                305                 :                :     {
                                306                 :                :         Datum       proargnames;
                                307                 :                :         Datum       proargmodes;
                                308                 :                :         int         n_arg_names;
                                309                 :                :         bool        isNull;
                                310                 :                : 
                                311                 :           2877 :         proargnames = SysCacheGetAttr(PROCNAMEARGSNSP, procedureTuple,
                                312                 :                :                                       Anum_pg_proc_proargnames,
                                313                 :                :                                       &isNull);
                                314         [ +  + ]:           2877 :         if (isNull)
 2999                           315                 :           1923 :             proargnames = PointerGetDatum(NULL);    /* just to be sure */
                                316                 :                : 
 4963                           317                 :           2877 :         proargmodes = SysCacheGetAttr(PROCNAMEARGSNSP, procedureTuple,
                                318                 :                :                                       Anum_pg_proc_proargmodes,
                                319                 :                :                                       &isNull);
                                320         [ +  + ]:           2877 :         if (isNull)
 2999                           321                 :           2557 :             proargmodes = PointerGetDatum(NULL);    /* just to be sure */
                                322                 :                : 
 1549                           323                 :           2877 :         n_arg_names = get_func_input_arg_names(proargnames, proargmodes,
                                324                 :                :                                                &pinfo->argnames);
                                325                 :                : 
                                326                 :                :         /* Paranoia: ignore the result if too few array entries */
 4963                           327         [ +  + ]:           2877 :         if (n_arg_names < nargs)
                                328                 :           1923 :             pinfo->argnames = NULL;
                                329                 :                :     }
                                330                 :                :     else
                                331                 :            626 :         pinfo->argnames = NULL;
                                332                 :                : 
 5280                           333                 :           3503 :     return pinfo;
                                334                 :                : }
                                335                 :                : 
                                336                 :                : /*
                                337                 :                :  * Parser setup hook for parsing a SQL function body.
                                338                 :                :  */
                                339                 :                : void
                                340                 :           6032 : sql_fn_parser_setup(struct ParseState *pstate, SQLFunctionParseInfoPtr pinfo)
                                341                 :                : {
                                342                 :           6032 :     pstate->p_pre_columnref_hook = NULL;
 4963                           343                 :           6032 :     pstate->p_post_columnref_hook = sql_fn_post_column_ref;
 5280                           344                 :           6032 :     pstate->p_paramref_hook = sql_fn_param_ref;
                                345                 :                :     /* no need to use p_coerce_param_hook */
  282 peter@eisentraut.org      346                 :           6032 :     pstate->p_ref_hook_state = pinfo;
 5280 tgl@sss.pgh.pa.us         347                 :           6032 : }
                                348                 :                : 
                                349                 :                : /*
                                350                 :                :  * sql_fn_post_column_ref       parser callback for ColumnRefs
                                351                 :                :  */
                                352                 :                : static Node *
 4963                           353                 :           4489 : sql_fn_post_column_ref(ParseState *pstate, ColumnRef *cref, Node *var)
                                354                 :                : {
                                355                 :           4489 :     SQLFunctionParseInfoPtr pinfo = (SQLFunctionParseInfoPtr) pstate->p_ref_hook_state;
                                356                 :                :     int         nnames;
                                357                 :                :     Node       *field1;
                                358                 :           4489 :     Node       *subfield = NULL;
                                359                 :                :     const char *name1;
                                360                 :           4489 :     const char *name2 = NULL;
                                361                 :                :     Node       *param;
                                362                 :                : 
                                363                 :                :     /*
                                364                 :                :      * Never override a table-column reference.  This corresponds to
                                365                 :                :      * considering the parameter names to appear in a scope outside the
                                366                 :                :      * individual SQL commands, which is what we want.
                                367                 :                :      */
                                368         [ +  + ]:           4489 :     if (var != NULL)
                                369                 :           3762 :         return NULL;
                                370                 :                : 
                                371                 :                :     /*----------
                                372                 :                :      * The allowed syntaxes are:
                                373                 :                :      *
                                374                 :                :      * A        A = parameter name
                                375                 :                :      * A.B      A = function name, B = parameter name
                                376                 :                :      *          OR: A = record-typed parameter name, B = field name
                                377                 :                :      *          (the first possibility takes precedence)
                                378                 :                :      * A.B.C    A = function name, B = record-typed parameter name,
                                379                 :                :      *          C = field name
                                380                 :                :      * A.*      Whole-row reference to composite parameter A.
                                381                 :                :      * A.B.*    Same, with A = function name, B = parameter name
                                382                 :                :      *
                                383                 :                :      * Here, it's sufficient to ignore the "*" in the last two cases --- the
                                384                 :                :      * main parser will take care of expanding the whole-row reference.
                                385                 :                :      *----------
                                386                 :                :      */
                                387                 :            727 :     nnames = list_length(cref->fields);
                                388                 :                : 
                                389         [ -  + ]:            727 :     if (nnames > 3)
 4963 tgl@sss.pgh.pa.us         390                 :UBC           0 :         return NULL;
                                391                 :                : 
 4233 tgl@sss.pgh.pa.us         392         [ +  + ]:CBC         727 :     if (IsA(llast(cref->fields), A_Star))
                                393                 :             21 :         nnames--;
                                394                 :                : 
 4963                           395                 :            727 :     field1 = (Node *) linitial(cref->fields);
                                396                 :            727 :     name1 = strVal(field1);
                                397         [ +  + ]:            727 :     if (nnames > 1)
                                398                 :                :     {
                                399                 :             78 :         subfield = (Node *) lsecond(cref->fields);
                                400                 :             78 :         name2 = strVal(subfield);
                                401                 :                :     }
                                402                 :                : 
                                403         [ +  + ]:            727 :     if (nnames == 3)
                                404                 :                :     {
                                405                 :                :         /*
                                406                 :                :          * Three-part name: if the first part doesn't match the function name,
                                407                 :                :          * we can fail immediately. Otherwise, look up the second part, and
                                408                 :                :          * take the third part to be a field reference.
                                409                 :                :          */
                                410         [ -  + ]:             12 :         if (strcmp(name1, pinfo->fname) != 0)
 4963 tgl@sss.pgh.pa.us         411                 :UBC           0 :             return NULL;
                                412                 :                : 
 4963 tgl@sss.pgh.pa.us         413                 :CBC          12 :         param = sql_fn_resolve_param_name(pinfo, name2, cref->location);
                                414                 :                : 
                                415                 :             12 :         subfield = (Node *) lthird(cref->fields);
                                416         [ -  + ]:             12 :         Assert(IsA(subfield, String));
                                417                 :                :     }
                                418   [ +  +  +  + ]:            715 :     else if (nnames == 2 && strcmp(name1, pinfo->fname) == 0)
                                419                 :                :     {
                                420                 :                :         /*
                                421                 :                :          * Two-part name with first part matching function name: first see if
                                422                 :                :          * second part matches any parameter name.
                                423                 :                :          */
                                424                 :             12 :         param = sql_fn_resolve_param_name(pinfo, name2, cref->location);
                                425                 :                : 
                                426         [ +  - ]:             12 :         if (param)
                                427                 :                :         {
                                428                 :                :             /* Yes, so this is a parameter reference, no subfield */
                                429                 :             12 :             subfield = NULL;
                                430                 :                :         }
                                431                 :                :         else
                                432                 :                :         {
                                433                 :                :             /* No, so try to match as parameter name and subfield */
 4963 tgl@sss.pgh.pa.us         434                 :UBC           0 :             param = sql_fn_resolve_param_name(pinfo, name1, cref->location);
                                435                 :                :         }
                                436                 :                :     }
                                437                 :                :     else
                                438                 :                :     {
                                439                 :                :         /* Single name, or parameter name followed by subfield */
 4963 tgl@sss.pgh.pa.us         440                 :CBC         703 :         param = sql_fn_resolve_param_name(pinfo, name1, cref->location);
                                441                 :                :     }
                                442                 :                : 
                                443         [ -  + ]:            727 :     if (!param)
 4963 tgl@sss.pgh.pa.us         444                 :UBC           0 :         return NULL;            /* No match */
                                445                 :                : 
 4963 tgl@sss.pgh.pa.us         446         [ +  + ]:CBC         727 :     if (subfield)
                                447                 :                :     {
                                448                 :                :         /*
                                449                 :                :          * Must be a reference to a field of a composite parameter; otherwise
                                450                 :                :          * ParseFuncOrColumn will return NULL, and we'll fail back at the
                                451                 :                :          * caller.
                                452                 :                :          */
                                453                 :             66 :         param = ParseFuncOrColumn(pstate,
                                454                 :             66 :                                   list_make1(subfield),
                                455                 :             66 :                                   list_make1(param),
                                456                 :                :                                   pstate->p_last_srf,
                                457                 :                :                                   NULL,
                                458                 :                :                                   false,
                                459                 :                :                                   cref->location);
                                460                 :                :     }
                                461                 :                : 
                                462                 :            727 :     return param;
                                463                 :                : }
                                464                 :                : 
                                465                 :                : /*
                                466                 :                :  * sql_fn_param_ref     parser callback for ParamRefs ($n symbols)
                                467                 :                :  */
                                468                 :                : static Node *
 5280                           469                 :          10862 : sql_fn_param_ref(ParseState *pstate, ParamRef *pref)
                                470                 :                : {
                                471                 :          10862 :     SQLFunctionParseInfoPtr pinfo = (SQLFunctionParseInfoPtr) pstate->p_ref_hook_state;
                                472                 :          10862 :     int         paramno = pref->number;
                                473                 :                : 
                                474                 :                :     /* Check parameter number is valid */
                                475   [ +  -  +  + ]:          10862 :     if (paramno <= 0 || paramno > pinfo->nargs)
                                476                 :              3 :         return NULL;            /* unknown parameter number */
                                477                 :                : 
 4963                           478                 :          10859 :     return sql_fn_make_param(pinfo, paramno, pref->location);
                                479                 :                : }
                                480                 :                : 
                                481                 :                : /*
                                482                 :                :  * sql_fn_make_param        construct a Param node for the given paramno
                                483                 :                :  */
                                484                 :                : static Node *
                                485                 :          11586 : sql_fn_make_param(SQLFunctionParseInfoPtr pinfo,
                                486                 :                :                   int paramno, int location)
                                487                 :                : {
                                488                 :                :     Param      *param;
                                489                 :                : 
 5280                           490                 :          11586 :     param = makeNode(Param);
                                491                 :          11586 :     param->paramkind = PARAM_EXTERN;
                                492                 :          11586 :     param->paramid = paramno;
                                493                 :          11586 :     param->paramtype = pinfo->argtypes[paramno - 1];
                                494                 :          11586 :     param->paramtypmod = -1;
                                495                 :          11586 :     param->paramcollid = get_typcollation(param->paramtype);
 4963                           496                 :          11586 :     param->location = location;
                                497                 :                : 
                                498                 :                :     /*
                                499                 :                :      * If we have a function input collation, allow it to override the
                                500                 :                :      * type-derived collation for parameter symbols.  (XXX perhaps this should
                                501                 :                :      * not happen if the type collation is not default?)
                                502                 :                :      */
 5280                           503   [ +  +  +  + ]:          11586 :     if (OidIsValid(pinfo->collation) && OidIsValid(param->paramcollid))
                                504                 :            984 :         param->paramcollid = pinfo->collation;
                                505                 :                : 
                                506                 :          11586 :     return (Node *) param;
                                507                 :                : }
                                508                 :                : 
                                509                 :                : /*
                                510                 :                :  * Search for a function parameter of the given name; if there is one,
                                511                 :                :  * construct and return a Param node for it.  If not, return NULL.
                                512                 :                :  * Helper function for sql_fn_post_column_ref.
                                513                 :                :  */
                                514                 :                : static Node *
 4963                           515                 :            727 : sql_fn_resolve_param_name(SQLFunctionParseInfoPtr pinfo,
                                516                 :                :                           const char *paramname, int location)
                                517                 :                : {
                                518                 :                :     int         i;
                                519                 :                : 
                                520         [ -  + ]:            727 :     if (pinfo->argnames == NULL)
 4963 tgl@sss.pgh.pa.us         521                 :UBC           0 :         return NULL;
                                522                 :                : 
 4963 tgl@sss.pgh.pa.us         523         [ +  - ]:CBC         999 :     for (i = 0; i < pinfo->nargs; i++)
                                524                 :                :     {
                                525   [ +  -  +  + ]:            999 :         if (pinfo->argnames[i] && strcmp(pinfo->argnames[i], paramname) == 0)
                                526                 :            727 :             return sql_fn_make_param(pinfo, i + 1, location);
                                527                 :                :     }
                                528                 :                : 
 4963 tgl@sss.pgh.pa.us         529                 :UBC           0 :     return NULL;
                                530                 :                : }
                                531                 :                : 
                                532                 :                : /*
                                533                 :                :  * Initialize the SQLFunctionCache for a SQL function
                                534                 :                :  */
                                535                 :                : static SQLFunctionCache *
  157 tgl@sss.pgh.pa.us         536                 :CBC       42701 : init_sql_fcache(FunctionCallInfo fcinfo, bool lazyEvalOK)
                                537                 :                : {
                                538                 :          42701 :     FmgrInfo   *finfo = fcinfo->flinfo;
                                539                 :                :     SQLFunctionHashEntry *func;
                                540                 :                :     SQLFunctionCache *fcache;
                                541                 :                : 
                                542                 :                :     /*
                                543                 :                :      * If this is the first execution for this FmgrInfo, set up a cache struct
                                544                 :                :      * (initially containing null pointers).  The cache must live as long as
                                545                 :                :      * the FmgrInfo, so it goes in fn_mcxt.  Also set up a memory context
                                546                 :                :      * callback that will be invoked when fn_mcxt is deleted.
                                547                 :                :      */
  142                           548                 :          42701 :     fcache = finfo->fn_extra;
                                549         [ +  + ]:          42701 :     if (fcache == NULL)
                                550                 :                :     {
                                551                 :                :         fcache = (SQLFunctionCache *)
                                552                 :          16144 :             MemoryContextAllocZero(finfo->fn_mcxt, sizeof(SQLFunctionCache));
                                553                 :          16144 :         fcache->fcontext = finfo->fn_mcxt;
                                554                 :          16144 :         fcache->mcb.func = RemoveSQLFunctionCache;
                                555                 :          16144 :         fcache->mcb.arg = fcache;
                                556                 :          16144 :         MemoryContextRegisterResetCallback(finfo->fn_mcxt, &fcache->mcb);
                                557                 :          16144 :         finfo->fn_extra = fcache;
                                558                 :                :     }
                                559                 :                : 
                                560                 :                :     /*
                                561                 :                :      * If the SQLFunctionCache is marked as active, we must have errored out
                                562                 :                :      * of a prior execution.  Reset state.  (It might seem that we could also
                                563                 :                :      * reach this during recursive invocation of a SQL function, but we won't
                                564                 :                :      * because that case won't involve re-use of the same FmgrInfo.)
                                565                 :                :      */
   17                           566         [ +  + ]:          42701 :     if (fcache->active)
                                567                 :                :     {
                                568                 :                :         /*
                                569                 :                :          * In general, this stanza should clear all the same fields that
                                570                 :                :          * ShutdownSQLFunction would.  Note we must clear fcache->cplan
                                571                 :                :          * without doing ReleaseCachedPlan, because error cleanup from the
                                572                 :                :          * prior execution would have taken care of releasing that plan.
                                573                 :                :          * Likewise, if tstore is still set then it is pointing at garbage.
                                574                 :                :          */
                                575                 :              3 :         fcache->cplan = NULL;
                                576                 :              3 :         fcache->eslist = NULL;
                                577                 :              3 :         fcache->tstore = NULL;
                                578                 :              3 :         fcache->shutdown_reg = false;
                                579                 :              3 :         fcache->active = false;
                                580                 :                :     }
                                581                 :                : 
                                582                 :                :     /*
                                583                 :                :      * If we are resuming execution of a set-returning function, just keep
                                584                 :                :      * using the same cache.  We do not ask funccache.c to re-validate the
                                585                 :                :      * SQLFunctionHashEntry: we want to run to completion using the function's
                                586                 :                :      * initial definition.
                                587                 :                :      */
  142                           588         [ +  + ]:          42701 :     if (fcache->eslist != NULL)
                                589                 :                :     {
                                590         [ -  + ]:            494 :         Assert(fcache->func != NULL);
                                591                 :            494 :         return fcache;
                                592                 :                :     }
                                593                 :                : 
                                594                 :                :     /*
                                595                 :                :      * Look up, or re-validate, the long-lived hash entry.  Make the hash key
                                596                 :                :      * depend on the result of get_call_result_type() when that's composite,
                                597                 :                :      * so that we can safely assume that we'll build a new hash entry if the
                                598                 :                :      * composite rowtype changes.
                                599                 :                :      */
                                600                 :                :     func = (SQLFunctionHashEntry *)
  157                           601                 :          42207 :         cached_function_compile(fcinfo,
  142                           602                 :          42207 :                                 (CachedFunction *) fcache->func,
                                603                 :                :                                 sql_compile_callback,
                                604                 :                :                                 sql_delete_callback,
                                605                 :                :                                 sizeof(SQLFunctionHashEntry),
                                606                 :                :                                 true,
                                607                 :                :                                 false);
                                608                 :                : 
                                609                 :                :     /*
                                610                 :                :      * Install the hash pointer in the SQLFunctionCache, and increment its use
                                611                 :                :      * count to reflect that.  If cached_function_compile gave us back a
                                612                 :                :      * different hash entry than we were using before, we must decrement that
                                613                 :                :      * one's use count.
                                614                 :                :      */
                                615         [ +  + ]:          42204 :     if (func != fcache->func)
                                616                 :                :     {
                                617         [ -  + ]:          16141 :         if (fcache->func != NULL)
                                618                 :                :         {
  142 tgl@sss.pgh.pa.us         619         [ #  # ]:UBC           0 :             Assert(fcache->func->cfunc.use_count > 0);
                                620                 :              0 :             fcache->func->cfunc.use_count--;
                                621                 :                :         }
  142 tgl@sss.pgh.pa.us         622                 :CBC       16141 :         fcache->func = func;
  157                           623                 :          16141 :         func->cfunc.use_count++;
                                624                 :                :         /* Assume we need to rebuild the junkFilter */
  142                           625                 :          16141 :         fcache->junkFilter = NULL;
                                626                 :                :     }
                                627                 :                : 
                                628                 :                :     /*
                                629                 :                :      * We're beginning a new execution of the function, so convert params to
                                630                 :                :      * appropriate format.
                                631                 :                :      */
  157                           632                 :          42204 :     postquel_sub_params(fcache, fcinfo);
                                633                 :                : 
                                634                 :                :     /* Also reset lazyEval state for the new execution. */
  142                           635                 :          42204 :     fcache->lazyEvalOK = lazyEvalOK;
                                636                 :          42204 :     fcache->lazyEval = false;
                                637                 :                : 
                                638                 :                :     /* Also reset data about where we are in the function. */
                                639                 :          42204 :     fcache->eslist = NULL;
                                640                 :          42204 :     fcache->next_query_index = 0;
                                641                 :          42204 :     fcache->error_query_index = 0;
                                642                 :                : 
  157                           643                 :          42204 :     return fcache;
                                644                 :                : }
                                645                 :                : 
                                646                 :                : /*
                                647                 :                :  * Set up the per-query execution_state records for the next query within
                                648                 :                :  * the SQL function.
                                649                 :                :  *
                                650                 :                :  * Returns true if successful, false if there are no more queries.
                                651                 :                :  */
                                652                 :                : static bool
                                653                 :          80392 : init_execution_state(SQLFunctionCachePtr fcache)
                                654                 :                : {
                                655                 :                :     CachedPlanSource *plansource;
                                656                 :          80392 :     execution_state *preves = NULL;
 6154                           657                 :          80392 :     execution_state *lasttages = NULL;
                                658                 :                :     int         nstmts;
                                659                 :                :     ListCell   *lc;
                                660                 :                : 
                                661                 :                :     /*
                                662                 :                :      * Clean up after previous query, if there was one.
                                663                 :                :      */
  157                           664         [ +  + ]:          80392 :     if (fcache->cplan)
                                665                 :                :     {
                                666                 :          38188 :         ReleaseCachedPlan(fcache->cplan, fcache->cowner);
                                667                 :          38188 :         fcache->cplan = NULL;
                                668                 :                :     }
                                669                 :          80392 :     fcache->eslist = NULL;
                                670                 :                : 
                                671                 :                :     /*
                                672                 :                :      * Get the next CachedPlanSource, or stop if there are no more.  We might
                                673                 :                :      * need to create the next CachedPlanSource; if so, advance
                                674                 :                :      * error_query_index first, so that errors detected in prepare_next_query
                                675                 :                :      * are blamed on the right statement.
                                676                 :                :      */
                                677         [ +  + ]:          80392 :     if (fcache->next_query_index >= list_length(fcache->func->plansource_list))
                                678                 :                :     {
                                679         [ +  + ]:          39194 :         if (fcache->next_query_index >= fcache->func->num_queries)
                                680                 :          38143 :             return false;
                                681                 :           1051 :         fcache->error_query_index++;
                                682                 :           1051 :         prepare_next_query(fcache->func);
                                683                 :                :     }
                                684                 :                :     else
                                685                 :          41198 :         fcache->error_query_index++;
                                686                 :                : 
                                687                 :          42246 :     plansource = (CachedPlanSource *) list_nth(fcache->func->plansource_list,
                                688                 :                :                                                fcache->next_query_index);
                                689                 :          42246 :     fcache->next_query_index++;
                                690                 :                : 
                                691                 :                :     /*
                                692                 :                :      * Generate plans for the query or queries within this CachedPlanSource.
                                693                 :                :      * Register the CachedPlan with the current resource owner.  (Saving
                                694                 :                :      * cowner here is mostly paranoia, but this way we needn't assume that
                                695                 :                :      * CurrentResourceOwner will be the same when ShutdownSQLFunction runs.)
                                696                 :                :      */
                                697                 :          42246 :     fcache->cowner = CurrentResourceOwner;
                                698                 :          42246 :     fcache->cplan = GetCachedPlan(plansource,
                                699                 :                :                                   fcache->paramLI,
                                700                 :                :                                   fcache->cowner,
                                701                 :                :                                   NULL);
                                702                 :                : 
                                703                 :                :     /*
                                704                 :                :      * If necessary, make esarray[] bigger to hold the needed state.
                                705                 :                :      */
  142                           706                 :          42221 :     nstmts = list_length(fcache->cplan->stmt_list);
                                707         [ +  + ]:          42221 :     if (nstmts > fcache->esarray_len)
                                708                 :                :     {
                                709         [ +  - ]:          16116 :         if (fcache->esarray == NULL)
                                710                 :          16116 :             fcache->esarray = (execution_state *)
                                711                 :          16116 :                 MemoryContextAlloc(fcache->fcontext,
                                712                 :                :                                    sizeof(execution_state) * nstmts);
                                713                 :                :         else
  142 tgl@sss.pgh.pa.us         714                 :UBC           0 :             fcache->esarray = repalloc_array(fcache->esarray,
                                715                 :                :                                              execution_state, nstmts);
  142 tgl@sss.pgh.pa.us         716                 :CBC       16116 :         fcache->esarray_len = nstmts;
                                717                 :                :     }
                                718                 :                : 
                                719                 :                :     /*
                                720                 :                :      * Build execution_state list to match the number of contained plans.
                                721                 :                :      */
  157                           722   [ +  -  +  +  :          84445 :     foreach(lc, fcache->cplan->stmt_list)
                                              +  + ]
                                723                 :                :     {
                                724                 :          42224 :         PlannedStmt *stmt = lfirst_node(PlannedStmt, lc);
                                725                 :                :         execution_state *newes;
                                726                 :                : 
                                727                 :                :         /*
                                728                 :                :          * Precheck all commands for validity in a function.  This should
                                729                 :                :          * generally match the restrictions spi.c applies.
                                730                 :                :          */
                                731         [ +  + ]:          42224 :         if (stmt->commandType == CMD_UTILITY)
                                732                 :                :         {
                                733         [ -  + ]:             90 :             if (IsA(stmt->utilityStmt, CopyStmt) &&
  157 tgl@sss.pgh.pa.us         734         [ #  # ]:UBC           0 :                 ((CopyStmt *) stmt->utilityStmt)->filename == NULL)
                                735         [ #  # ]:              0 :                 ereport(ERROR,
                                736                 :                :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                737                 :                :                          errmsg("cannot COPY to/from client in an SQL function")));
                                738                 :                : 
  157 tgl@sss.pgh.pa.us         739         [ -  + ]:CBC          90 :             if (IsA(stmt->utilityStmt, TransactionStmt))
 5304 tgl@sss.pgh.pa.us         740         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                741                 :                :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                742                 :                :                 /* translator: %s is a SQL statement name */
                                743                 :                :                          errmsg("%s is not allowed in an SQL function",
                                744                 :                :                                 CreateCommandName(stmt->utilityStmt))));
                                745                 :                :         }
                                746                 :                : 
  157 tgl@sss.pgh.pa.us         747   [ +  +  -  + ]:CBC       42224 :         if (fcache->func->readonly_func && !CommandIsReadOnly(stmt))
  157 tgl@sss.pgh.pa.us         748         [ #  # ]:UBC           0 :             ereport(ERROR,
                                749                 :                :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                750                 :                :             /* translator: %s is a SQL statement name */
                                751                 :                :                      errmsg("%s is not allowed in a non-volatile function",
                                752                 :                :                             CreateCommandName((Node *) stmt))));
                                753                 :                : 
                                754                 :                :         /* OK, build the execution_state for this query */
  142 tgl@sss.pgh.pa.us         755                 :CBC       42224 :         newes = &fcache->esarray[foreach_current_index(lc)];
  157                           756         [ +  + ]:          42224 :         if (preves)
                                757                 :              3 :             preves->next = newes;
                                758                 :                :         else
                                759                 :          42221 :             fcache->eslist = newes;
                                760                 :                : 
                                761                 :          42224 :         newes->next = NULL;
                                762                 :          42224 :         newes->status = F_EXEC_START;
                                763                 :          42224 :         newes->setsResult = false;   /* might change below */
                                764                 :          42224 :         newes->lazyEval = false; /* might change below */
                                765                 :          42224 :         newes->stmt = stmt;
                                766                 :          42224 :         newes->qd = NULL;
                                767                 :                : 
                                768         [ +  + ]:          42224 :         if (stmt->canSetTag)
                                769                 :          42221 :             lasttages = newes;
                                770                 :                : 
                                771                 :          42224 :         preves = newes;
                                772                 :                :     }
                                773                 :                : 
                                774                 :                :     /*
                                775                 :                :      * If this isn't the last CachedPlanSource, we're done here.  Otherwise,
                                776                 :                :      * we need to prepare information about how to return the results.
                                777                 :                :      */
                                778         [ +  + ]:          42221 :     if (fcache->next_query_index < fcache->func->num_queries)
                                779                 :             63 :         return true;
                                780                 :                : 
                                781                 :                :     /*
                                782                 :                :      * Construct a JunkFilter we can use to coerce the returned rowtype to the
                                783                 :                :      * desired form, unless the result type is VOID, in which case there's
                                784                 :                :      * nothing to coerce to.  (XXX Frequently, the JunkFilter isn't doing
                                785                 :                :      * anything very interesting, but much of this module expects it to be
                                786                 :                :      * there anyway.)
                                787                 :                :      *
                                788                 :                :      * Normally we can re-use the JunkFilter across executions, but if the
                                789                 :                :      * plan for the last CachedPlanSource changed, we'd better rebuild it.
                                790                 :                :      *
                                791                 :                :      * The JunkFilter, its result slot, and its tupledesc are kept in a
                                792                 :                :      * subsidiary memory context so that we can free them easily when needed.
                                793                 :                :      */
  142                           794         [ +  + ]:          42158 :     if (fcache->func->rettype != VOIDOID &&
                                795         [ +  + ]:          42011 :         (fcache->junkFilter == NULL ||
                                796         [ +  + ]:          26057 :          fcache->jf_generation != fcache->cplan->generation))
                                797                 :                :     {
                                798                 :                :         TupleTableSlot *slot;
                                799                 :                :         List       *resulttlist;
                                800                 :                :         MemoryContext oldcontext;
                                801                 :                : 
                                802                 :                :         /* Create or reset the jfcontext */
                                803         [ +  + ]:          16993 :         if (fcache->jfcontext == NULL)
                                804                 :          15954 :             fcache->jfcontext = AllocSetContextCreate(fcache->fcontext,
                                805                 :                :                                                       "SQL function junkfilter",
                                806                 :                :                                                       ALLOCSET_SMALL_SIZES);
                                807                 :                :         else
                                808                 :           1039 :             MemoryContextReset(fcache->jfcontext);
                                809                 :          16993 :         oldcontext = MemoryContextSwitchTo(fcache->jfcontext);
                                810                 :                : 
                                811                 :          16993 :         slot = MakeSingleTupleTableSlot(NULL, &TTSOpsMinimalTuple);
                                812                 :                : 
                                813                 :                :         /*
                                814                 :                :          * Re-fetch the (possibly modified) output tlist of the final
                                815                 :                :          * statement.  By this point, we should have thrown an error if there
                                816                 :                :          * is not one.
                                817                 :                :          */
  157                           818                 :          16993 :         resulttlist = get_sql_fn_result_tlist(plansource->query_list);
                                819                 :                : 
                                820                 :                :         /*
                                821                 :                :          * If the result is composite, *and* we are returning the whole tuple
                                822                 :                :          * result, we need to insert nulls for any dropped columns.  In the
                                823                 :                :          * single-column-result case, there might be dropped columns within
                                824                 :                :          * the composite column value, but it's not our problem here.  There
                                825                 :                :          * should be no resjunk entries in resulttlist, so in the second case
                                826                 :                :          * the JunkFilter is certainly a no-op.
                                827                 :                :          */
                                828   [ +  +  +  + ]:          16993 :         if (fcache->func->rettupdesc && fcache->func->returnsTuple)
                                829                 :            802 :             fcache->junkFilter = ExecInitJunkFilterConversion(resulttlist,
                                830                 :            802 :                                                               fcache->func->rettupdesc,
                                831                 :                :                                                               slot);
                                832                 :                :         else
                                833                 :          16191 :             fcache->junkFilter = ExecInitJunkFilter(resulttlist, slot);
                                834                 :                : 
                                835                 :                :         /*
                                836                 :                :          * The resulttlist tree belongs to the plancache and might disappear
                                837                 :                :          * underneath us due to plancache invalidation.  While we could
                                838                 :                :          * forestall that by copying it, that'd just be a waste of cycles,
                                839                 :                :          * because the junkfilter doesn't need it anymore.  (It'd only be used
                                840                 :                :          * by ExecFindJunkAttribute(), which we don't use here.)  To ensure
                                841                 :                :          * there's not a dangling pointer laying about, clear the junkFilter's
                                842                 :                :          * pointer.
                                843                 :                :          */
  142                           844                 :          16993 :         fcache->junkFilter->jf_targetList = NIL;
                                845                 :                : 
                                846                 :                :         /* Make sure output rowtype is properly blessed */
                                847         [ +  + ]:          16993 :         if (fcache->func->returnsTuple)
                                848                 :            814 :             BlessTupleDesc(fcache->junkFilter->jf_resultSlot->tts_tupleDescriptor);
                                849                 :                : 
                                850                 :                :         /* Mark the JunkFilter as up-to-date */
                                851                 :          16993 :         fcache->jf_generation = fcache->cplan->generation;
                                852                 :                : 
                                853                 :          16993 :         MemoryContextSwitchTo(oldcontext);
                                854                 :                :     }
                                855                 :                : 
                                856         [ +  + ]:          42158 :     if (fcache->func->returnsSet &&
                                857   [ +  +  -  + ]:           2120 :         !fcache->func->returnsTuple &&
                                858                 :             97 :         type_is_rowtype(fcache->func->rettype))
                                859                 :                :     {
                                860                 :                :         /*
                                861                 :                :          * Returning rowtype as if it were scalar --- materialize won't work.
                                862                 :                :          * Right now it's sufficient to override any caller preference for
                                863                 :                :          * materialize mode, but this might need more work in future.
                                864                 :                :          */
  157 tgl@sss.pgh.pa.us         865                 :UBC           0 :         fcache->lazyEvalOK = true;
                                866                 :                :     }
                                867                 :                : 
                                868                 :                :     /*
                                869                 :                :      * Mark the last canSetTag query as delivering the function result; then,
                                870                 :                :      * if it is a plain SELECT, mark it for lazy evaluation. If it's not a
                                871                 :                :      * SELECT we must always run it to completion.
                                872                 :                :      *
                                873                 :                :      * Note: at some point we might add additional criteria for whether to use
                                874                 :                :      * lazy eval.  However, we should prefer to use it whenever the function
                                875                 :                :      * doesn't return set, since fetching more than one row is useless in that
                                876                 :                :      * case.
                                877                 :                :      *
                                878                 :                :      * Note: don't set setsResult if the function returns VOID, as evidenced
                                879                 :                :      * by not having made a junkfilter.  This ensures we'll throw away any
                                880                 :                :      * output from the last statement in such a function.
                                881                 :                :      */
 6154 tgl@sss.pgh.pa.us         882   [ +  -  +  + ]:CBC       42158 :     if (lasttages && fcache->junkFilter)
                                883                 :                :     {
                                884                 :          42011 :         lasttages->setsResult = true;
  157                           885         [ +  + ]:          42011 :         if (fcache->lazyEvalOK &&
 3157                           886         [ +  + ]:          41594 :             lasttages->stmt->commandType == CMD_SELECT &&
                                887         [ +  - ]:          41555 :             !lasttages->stmt->hasModifyingCTE)
                                888                 :          41555 :             fcache->lazyEval = lasttages->lazyEval = true;
                                889                 :                :     }
                                890                 :                : 
  157                           891                 :          42158 :     return true;
                                892                 :                : }
                                893                 :                : 
                                894                 :                : /*
                                895                 :                :  * Convert the SQL function's next query from source form (RawStmt or Query)
                                896                 :                :  * into a CachedPlanSource.  If it's the last query, also determine whether
                                897                 :                :  * the function returnsTuple.
                                898                 :                :  */
                                899                 :                : static void
                                900                 :           1051 : prepare_next_query(SQLFunctionHashEntry *func)
                                901                 :                : {
                                902                 :                :     int         qindex;
                                903                 :                :     bool        islast;
                                904                 :                :     CachedPlanSource *plansource;
                                905                 :                :     List       *queryTree_list;
                                906                 :                :     MemoryContext oldcontext;
                                907                 :                : 
                                908                 :                :     /* Which query should we process? */
                                909                 :           1051 :     qindex = list_length(func->plansource_list);
                                910         [ -  + ]:           1051 :     Assert(qindex < func->num_queries); /* else caller error */
                                911                 :           1051 :     islast = (qindex + 1 >= func->num_queries);
                                912                 :                : 
                                913                 :                :     /*
                                914                 :                :      * Parse and/or rewrite the query, creating a CachedPlanSource that holds
                                915                 :                :      * a copy of the original parsetree.  Note fine point: we make a copy of
                                916                 :                :      * each original parsetree to ensure that the source_list in pcontext
                                917                 :                :      * remains unmodified during parse analysis and rewrite.  This is normally
                                918                 :                :      * unnecessary, but we have to do it in case an error is raised during
                                919                 :                :      * parse analysis.  Otherwise, a fresh attempt to execute the function
                                920                 :                :      * will arrive back here and try to work from a corrupted source_list.
                                921                 :                :      */
                                922         [ +  + ]:           1051 :     if (!func->raw_source)
                                923                 :                :     {
                                924                 :                :         /* Source queries are already parse-analyzed */
                                925                 :            391 :         Query      *parsetree = list_nth_node(Query, func->source_list, qindex);
                                926                 :                : 
  155                           927                 :            391 :         parsetree = copyObject(parsetree);
  157                           928                 :            391 :         plansource = CreateCachedPlanForQuery(parsetree,
                                929                 :            391 :                                               func->src,
                                930                 :                :                                               CreateCommandTag((Node *) parsetree));
                                931                 :            391 :         AcquireRewriteLocks(parsetree, true, false);
                                932                 :            391 :         queryTree_list = pg_rewrite_query(parsetree);
                                933                 :                :     }
                                934                 :                :     else
                                935                 :                :     {
                                936                 :                :         /* Source queries are raw parsetrees */
                                937                 :            660 :         RawStmt    *parsetree = list_nth_node(RawStmt, func->source_list, qindex);
                                938                 :                : 
  155                           939                 :            660 :         parsetree = copyObject(parsetree);
  157                           940                 :            660 :         plansource = CreateCachedPlan(parsetree,
                                941                 :            660 :                                       func->src,
                                942                 :                :                                       CreateCommandTag(parsetree->stmt));
                                943                 :            660 :         queryTree_list = pg_analyze_and_rewrite_withcb(parsetree,
                                944                 :            660 :                                                        func->src,
                                945                 :                :                                                        (ParserSetupHook) sql_fn_parser_setup,
                                946                 :            660 :                                                        func->pinfo,
                                947                 :                :                                                        NULL);
                                948                 :                :     }
                                949                 :                : 
                                950                 :                :     /*
                                951                 :                :      * Check that there are no statements we don't want to allow.
                                952                 :                :      */
                                953                 :           1051 :     check_sql_fn_statement(queryTree_list);
                                954                 :                : 
                                955                 :                :     /*
                                956                 :                :      * If this is the last query, check that the function returns the type it
                                957                 :                :      * claims to.  Although in simple cases this was already done when the
                                958                 :                :      * function was defined, we have to recheck because database objects used
                                959                 :                :      * in the function's queries might have changed type.  We'd have to
                                960                 :                :      * recheck anyway if the function had any polymorphic arguments. Moreover,
                                961                 :                :      * check_sql_stmt_retval takes care of injecting any required column type
                                962                 :                :      * coercions.  (But we don't ask it to insert nulls for dropped columns;
                                963                 :                :      * the junkfilter handles that.)
                                964                 :                :      *
                                965                 :                :      * Note: we set func->returnsTuple according to whether we are returning
                                966                 :                :      * the whole tuple result or just a single column.  In the latter case we
                                967                 :                :      * clear returnsTuple because we need not act different from the scalar
                                968                 :                :      * result case, even if it's a rowtype column.  (However, we have to force
                                969                 :                :      * lazy eval mode in that case; otherwise we'd need extra code to expand
                                970                 :                :      * the rowtype column into multiple columns, since we have no way to
                                971                 :                :      * notify the caller that it should do that.)
                                972                 :                :      */
                                973         [ +  + ]:           1051 :     if (islast)
                                974                 :           1013 :         func->returnsTuple = check_sql_stmt_retval(queryTree_list,
                                975                 :                :                                                    func->rettype,
                                976                 :                :                                                    func->rettupdesc,
                                977                 :           1016 :                                                    func->prokind,
                                978                 :                :                                                    false);
                                979                 :                : 
                                980                 :                :     /*
                                981                 :                :      * Now that check_sql_stmt_retval has done its thing, we can complete plan
                                982                 :                :      * cache entry creation.
                                983                 :                :      */
                                984                 :           1048 :     CompleteCachedPlan(plansource,
                                985                 :                :                        queryTree_list,
                                986                 :                :                        NULL,
                                987                 :                :                        NULL,
                                988                 :                :                        0,
                                989                 :                :                        (ParserSetupHook) sql_fn_parser_setup,
                                990                 :           1048 :                        func->pinfo,
                                991                 :                :                        CURSOR_OPT_PARALLEL_OK | CURSOR_OPT_NO_SCROLL,
                                992                 :                :                        false);
                                993                 :                : 
                                994                 :                :     /*
                                995                 :                :      * Install post-rewrite hook.  Its arg is the hash entry if this is the
                                996                 :                :      * last statement, else NULL.
                                997                 :                :      */
                                998         [ +  + ]:           1048 :     SetPostRewriteHook(plansource,
                                999                 :                :                        sql_postrewrite_callback,
                               1000                 :                :                        islast ? func : NULL);
                               1001                 :                : 
                               1002                 :                :     /*
                               1003                 :                :      * While the CachedPlanSources can take care of themselves, our List
                               1004                 :                :      * pointing to them had better be in the hcontext.
                               1005                 :                :      */
                               1006                 :           1048 :     oldcontext = MemoryContextSwitchTo(func->hcontext);
                               1007                 :           1048 :     func->plansource_list = lappend(func->plansource_list, plansource);
                               1008                 :           1048 :     MemoryContextSwitchTo(oldcontext);
                               1009                 :                : 
                               1010                 :                :     /*
                               1011                 :                :      * As soon as we've linked the CachedPlanSource into the list, mark it as
                               1012                 :                :      * "saved".
                               1013                 :                :      */
                               1014                 :           1048 :     SaveCachedPlan(plansource);
                               1015                 :                : 
                               1016                 :                :     /*
                               1017                 :                :      * Finally, if this was the last statement, we can flush the pcontext with
                               1018                 :                :      * the original query trees; they're all safely copied into
                               1019                 :                :      * CachedPlanSources now.
                               1020                 :                :      */
                               1021         [ +  + ]:           1048 :     if (islast)
                               1022                 :                :     {
                               1023                 :           1013 :         func->source_list = NIL; /* avoid dangling pointer */
                               1024                 :           1013 :         MemoryContextDelete(func->pcontext);
                               1025                 :           1013 :         func->pcontext = NULL;
                               1026                 :                :     }
                               1027                 :           1048 : }
                               1028                 :                : 
                               1029                 :                : /*
                               1030                 :                :  * Fill a new SQLFunctionHashEntry.
                               1031                 :                :  *
                               1032                 :                :  * The passed-in "cfunc" struct is expected to be zeroes, except
                               1033                 :                :  * for the CachedFunction fields, which we don't touch here.
                               1034                 :                :  *
                               1035                 :                :  * We expect to be called in a short-lived memory context (typically a
                               1036                 :                :  * query's per-tuple context).  Data that is to be part of the hash entry
                               1037                 :                :  * must be copied into the hcontext or pcontext as appropriate.
                               1038                 :                :  */
                               1039                 :                : static void
                               1040                 :           1034 : sql_compile_callback(FunctionCallInfo fcinfo,
                               1041                 :                :                      HeapTuple procedureTuple,
                               1042                 :                :                      const CachedFunctionHashKey *hashkey,
                               1043                 :                :                      CachedFunction *cfunc,
                               1044                 :                :                      bool forValidator)
                               1045                 :                : {
                               1046                 :           1034 :     SQLFunctionHashEntry *func = (SQLFunctionHashEntry *) cfunc;
                               1047                 :           1034 :     Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
                               1048                 :                :     ErrorContextCallback comperrcontext;
                               1049                 :                :     MemoryContext hcontext;
                               1050                 :                :     MemoryContext pcontext;
                               1051                 :           1034 :     MemoryContext oldcontext = CurrentMemoryContext;
                               1052                 :                :     Oid         rettype;
                               1053                 :                :     TupleDesc   rettupdesc;
                               1054                 :                :     Datum       tmp;
                               1055                 :                :     bool        isNull;
                               1056                 :                :     List       *source_list;
                               1057                 :                : 
                               1058                 :                :     /*
                               1059                 :                :      * Setup error traceback support for ereport() during compile.  (This is
                               1060                 :                :      * mainly useful for reporting parse errors from pg_parse_query.)
                               1061                 :                :      */
                               1062                 :           1034 :     comperrcontext.callback = sql_compile_error_callback;
                               1063                 :           1034 :     comperrcontext.arg = func;
                               1064                 :           1034 :     comperrcontext.previous = error_context_stack;
                               1065                 :           1034 :     error_context_stack = &comperrcontext;
                               1066                 :                : 
                               1067                 :                :     /*
                               1068                 :                :      * Create the hash entry's memory context.  For now it's a child of the
                               1069                 :                :      * caller's context, so that it will go away if we fail partway through.
                               1070                 :                :      */
                               1071                 :           1034 :     hcontext = AllocSetContextCreate(CurrentMemoryContext,
                               1072                 :                :                                      "SQL function",
                               1073                 :                :                                      ALLOCSET_SMALL_SIZES);
                               1074                 :                : 
                               1075                 :                :     /*
                               1076                 :                :      * Create the not-as-long-lived pcontext.  We make this a child of
                               1077                 :                :      * hcontext so that it doesn't require separate deletion.
                               1078                 :                :      */
                               1079                 :           1034 :     pcontext = AllocSetContextCreate(hcontext,
                               1080                 :                :                                      "SQL function parse trees",
                               1081                 :                :                                      ALLOCSET_SMALL_SIZES);
                               1082                 :           1034 :     func->pcontext = pcontext;
                               1083                 :                : 
                               1084                 :                :     /*
                               1085                 :                :      * copy function name immediately for use by error reporting callback, and
                               1086                 :                :      * for use as memory context identifier
                               1087                 :                :      */
                               1088                 :           2068 :     func->fname = MemoryContextStrdup(hcontext,
                               1089                 :           1034 :                                       NameStr(procedureStruct->proname));
                               1090                 :           1034 :     MemoryContextSetIdentifier(hcontext, func->fname);
                               1091                 :                : 
                               1092                 :                :     /*
                               1093                 :                :      * Resolve any polymorphism, obtaining the actual result type, and the
                               1094                 :                :      * corresponding tupdesc if it's a rowtype.
                               1095                 :                :      */
 2068                          1096                 :           1034 :     (void) get_call_result_type(fcinfo, &rettype, &rettupdesc);
                               1097                 :                : 
  157                          1098                 :           1034 :     func->rettype = rettype;
                               1099         [ +  + ]:           1034 :     if (rettupdesc)
                               1100                 :                :     {
                               1101                 :            265 :         MemoryContextSwitchTo(hcontext);
                               1102                 :            265 :         func->rettupdesc = CreateTupleDescCopy(rettupdesc);
                               1103                 :            265 :         MemoryContextSwitchTo(oldcontext);
                               1104                 :                :     }
                               1105                 :                : 
                               1106                 :                :     /* Fetch the typlen and byval info for the result type */
                               1107                 :           1034 :     get_typlenbyval(rettype, &func->typlen, &func->typbyval);
                               1108                 :                : 
                               1109                 :                :     /* Remember whether we're returning setof something */
                               1110                 :           1034 :     func->returnsSet = procedureStruct->proretset;
                               1111                 :                : 
                               1112                 :                :     /* Remember if function is STABLE/IMMUTABLE */
                               1113                 :           1034 :     func->readonly_func =
 7663                          1114                 :           1034 :         (procedureStruct->provolatile != PROVOLATILE_VOLATILE);
                               1115                 :                : 
                               1116                 :                :     /* Remember routine kind */
  157                          1117                 :           1034 :     func->prokind = procedureStruct->prokind;
                               1118                 :                : 
                               1119                 :                :     /*
                               1120                 :                :      * We need the actual argument types to pass to the parser.  Also make
                               1121                 :                :      * sure that parameter symbols are considered to have the function's
                               1122                 :                :      * resolved input collation.
                               1123                 :                :      */
                               1124                 :           1034 :     MemoryContextSwitchTo(hcontext);
                               1125                 :           2068 :     func->pinfo = prepare_sql_fn_parse_info(procedureTuple,
                               1126                 :           1034 :                                             fcinfo->flinfo->fn_expr,
                               1127                 :                :                                             PG_GET_COLLATION());
                               1128                 :           1034 :     MemoryContextSwitchTo(oldcontext);
                               1129                 :                : 
                               1130                 :                :     /*
                               1131                 :                :      * Now that we have the resolved argument types, collect their typlens for
                               1132                 :                :      * use in postquel_sub_params.
                               1133                 :                :      */
  142                          1134                 :           1034 :     func->argtyplen = (int16 *)
                               1135                 :           1034 :         MemoryContextAlloc(hcontext, func->pinfo->nargs * sizeof(int16));
                               1136         [ +  + ]:           2495 :     for (int i = 0; i < func->pinfo->nargs; i++)
                               1137                 :           1461 :         func->argtyplen[i] = get_typlen(func->pinfo->argtypes[i]);
                               1138                 :                : 
                               1139                 :                :     /*
                               1140                 :                :      * And of course we need the function body text.
                               1141                 :                :      */
  896 dgustafsson@postgres     1142                 :           1034 :     tmp = SysCacheGetAttrNotNull(PROCOID, procedureTuple, Anum_pg_proc_prosrc);
  157 tgl@sss.pgh.pa.us        1143                 :           1034 :     func->src = MemoryContextStrdup(hcontext,
                               1144                 :           1034 :                                     TextDatumGetCString(tmp));
                               1145                 :                : 
                               1146                 :                :     /* If we have prosqlbody, pay attention to that not prosrc. */
 1605                          1147                 :           1034 :     tmp = SysCacheGetAttr(PROCOID,
                               1148                 :                :                           procedureTuple,
                               1149                 :                :                           Anum_pg_proc_prosqlbody,
                               1150                 :                :                           &isNull);
                               1151         [ +  + ]:           1034 :     if (!isNull)
                               1152                 :                :     {
                               1153                 :                :         /* Source queries are already parse-analyzed */
                               1154                 :                :         Node       *n;
                               1155                 :                : 
 1613 peter@eisentraut.org     1156                 :            391 :         n = stringToNode(TextDatumGetCString(tmp));
                               1157         [ +  + ]:            391 :         if (IsA(n, List))
  157 tgl@sss.pgh.pa.us        1158                 :            276 :             source_list = linitial_node(List, castNode(List, n));
                               1159                 :                :         else
                               1160                 :            115 :             source_list = list_make1(n);
                               1161                 :            391 :         func->raw_source = false;
                               1162                 :                :     }
                               1163                 :                :     else
                               1164                 :                :     {
                               1165                 :                :         /* Source queries are raw parsetrees */
                               1166                 :            643 :         source_list = pg_parse_query(func->src);
                               1167                 :            643 :         func->raw_source = true;
                               1168                 :                :     }
                               1169                 :                : 
                               1170                 :                :     /*
                               1171                 :                :      * Note: we must save the number of queries so that we'll still remember
                               1172                 :                :      * how many there are after we discard source_list.
                               1173                 :                :      */
                               1174                 :           1034 :     func->num_queries = list_length(source_list);
                               1175                 :                : 
                               1176                 :                :     /*
                               1177                 :                :      * Edge case: empty function body is OK only if it returns VOID.  Normally
                               1178                 :                :      * we validate that the last statement returns the right thing in
                               1179                 :                :      * check_sql_stmt_retval, but we'll never reach that if there's no last
                               1180                 :                :      * statement.
                               1181                 :                :      */
  156                          1182   [ +  +  +  + ]:           1034 :     if (func->num_queries == 0 && rettype != VOIDOID)
                               1183         [ +  - ]:              3 :         ereport(ERROR,
                               1184                 :                :                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                               1185                 :                :                  errmsg("return type mismatch in function declared to return %s",
                               1186                 :                :                         format_type_be(rettype)),
                               1187                 :                :                  errdetail("Function's final statement must be SELECT or INSERT/UPDATE/DELETE/MERGE RETURNING.")));
                               1188                 :                : 
                               1189                 :                :     /* Save the source trees in pcontext for now. */
  157                          1190                 :           1031 :     MemoryContextSwitchTo(pcontext);
                               1191                 :           1031 :     func->source_list = copyObject(source_list);
                               1192                 :           1031 :     MemoryContextSwitchTo(oldcontext);
                               1193                 :                : 
                               1194                 :                :     /*
                               1195                 :                :      * We now have a fully valid hash entry, so reparent hcontext under
                               1196                 :                :      * CacheMemoryContext to make all the subsidiary data long-lived, and only
                               1197                 :                :      * then install the hcontext link so that sql_delete_callback will know to
                               1198                 :                :      * delete it.
                               1199                 :                :      */
                               1200                 :           1031 :     MemoryContextSetParent(hcontext, CacheMemoryContext);
                               1201                 :           1031 :     func->hcontext = hcontext;
                               1202                 :                : 
                               1203                 :           1031 :     error_context_stack = comperrcontext.previous;
                               1204                 :           1031 : }
                               1205                 :                : 
                               1206                 :                : /*
                               1207                 :                :  * Deletion callback used by funccache.c.
                               1208                 :                :  *
                               1209                 :                :  * Free any free-able subsidiary data of cfunc, but not the
                               1210                 :                :  * struct CachedFunction itself.
                               1211                 :                :  */
                               1212                 :                : static void
                               1213                 :             33 : sql_delete_callback(CachedFunction *cfunc)
                               1214                 :                : {
                               1215                 :             33 :     SQLFunctionHashEntry *func = (SQLFunctionHashEntry *) cfunc;
                               1216                 :                :     ListCell   *lc;
                               1217                 :                : 
                               1218                 :                :     /* Release the CachedPlanSources */
                               1219   [ +  -  +  +  :             66 :     foreach(lc, func->plansource_list)
                                              +  + ]
                               1220                 :                :     {
                               1221                 :             33 :         CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
                               1222                 :                : 
                               1223                 :             33 :         DropCachedPlan(plansource);
                               1224                 :                :     }
                               1225                 :             33 :     func->plansource_list = NIL;
                               1226                 :                : 
                               1227                 :                :     /*
                               1228                 :                :      * If we have an hcontext, free it, thereby getting rid of all subsidiary
                               1229                 :                :      * data.  (If we still have a pcontext, this gets rid of that too.)
                               1230                 :                :      */
                               1231         [ +  - ]:             33 :     if (func->hcontext)
                               1232                 :             33 :         MemoryContextDelete(func->hcontext);
                               1233                 :             33 :     func->hcontext = NULL;
                               1234                 :             33 : }
                               1235                 :                : 
                               1236                 :                : /*
                               1237                 :                :  * Post-rewrite callback used by plancache.c.
                               1238                 :                :  *
                               1239                 :                :  * This must match the processing that prepare_next_query() does between
                               1240                 :                :  * rewriting and calling CompleteCachedPlan().
                               1241                 :                :  */
                               1242                 :                : static void
                               1243                 :            177 : sql_postrewrite_callback(List *querytree_list, void *arg)
                               1244                 :                : {
                               1245                 :                :     /*
                               1246                 :                :      * Check that there are no statements we don't want to allow.  (Presently,
                               1247                 :                :      * there's no real point in this because the result can't change from what
                               1248                 :                :      * we saw originally.  But it's cheap and maybe someday it will matter.)
                               1249                 :                :      */
                               1250                 :            177 :     check_sql_fn_statement(querytree_list);
                               1251                 :                : 
                               1252                 :                :     /*
                               1253                 :                :      * If this is the last query, we must re-do what check_sql_stmt_retval did
                               1254                 :                :      * to its targetlist.  Also check that returnsTuple didn't change (it
                               1255                 :                :      * probably cannot, but be cautious).
                               1256                 :                :      */
                               1257         [ +  - ]:            177 :     if (arg != NULL)
                               1258                 :                :     {
                               1259                 :            177 :         SQLFunctionHashEntry *func = (SQLFunctionHashEntry *) arg;
                               1260                 :                :         bool        returnsTuple;
                               1261                 :                : 
                               1262                 :            177 :         returnsTuple = check_sql_stmt_retval(querytree_list,
                               1263                 :                :                                              func->rettype,
                               1264                 :                :                                              func->rettupdesc,
                               1265                 :            177 :                                              func->prokind,
                               1266                 :                :                                              false);
                               1267         [ -  + ]:            177 :         if (returnsTuple != func->returnsTuple)
  157 tgl@sss.pgh.pa.us        1268         [ #  # ]:UBC           0 :             ereport(ERROR,
                               1269                 :                :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               1270                 :                :                      errmsg("cached plan must not change result type")));
                               1271                 :                :     }
 9144 tgl@sss.pgh.pa.us        1272                 :CBC         177 : }
                               1273                 :                : 
                               1274                 :                : /* Start up execution of one execution_state node */
                               1275                 :                : static void
 8311                          1276                 :          42224 : postquel_start(execution_state *es, SQLFunctionCachePtr fcache)
                               1277                 :                : {
                               1278                 :                :     DestReceiver *dest;
  127                          1279                 :          42224 :     MemoryContext oldcontext = CurrentMemoryContext;
                               1280                 :                : 
 8311                          1281         [ -  + ]:          42224 :     Assert(es->qd == NULL);
                               1282                 :                : 
                               1283                 :                :     /* Caller should have ensured a suitable snapshot is active */
 5304                          1284         [ -  + ]:          42224 :     Assert(ActiveSnapshotSet());
                               1285                 :                : 
                               1286                 :                :     /*
                               1287                 :                :      * In lazyEval mode for a SRF, we must run the sub-executor in a child of
                               1288                 :                :      * fcontext, so that it can survive across multiple calls to fmgr_sql.
                               1289                 :                :      * (XXX in the case of a long-lived FmgrInfo, this policy potentially
                               1290                 :                :      * causes memory leakage, but it's not very clear where we could keep
                               1291                 :                :      * stuff instead.  Fortunately, there are few if any cases where
                               1292                 :                :      * set-returning functions are invoked via FmgrInfos that would outlive
                               1293                 :                :      * the calling query.)  Otherwise, we're going to run it to completion
                               1294                 :                :      * before exiting fmgr_sql, so it can perfectly well run in the caller's
                               1295                 :                :      * context.
                               1296                 :                :      */
  142                          1297   [ +  +  +  + ]:          42224 :     if (es->lazyEval && fcache->func->returnsSet)
                               1298                 :                :     {
                               1299                 :           1588 :         fcache->subcontext = AllocSetContextCreate(fcache->fcontext,
                               1300                 :                :                                                    "SQL function execution",
                               1301                 :                :                                                    ALLOCSET_DEFAULT_SIZES);
                               1302                 :           1588 :         fcache->ownSubcontext = true;
                               1303                 :                :     }
                               1304         [ +  + ]:          40636 :     else if (es->stmt->commandType == CMD_UTILITY)
                               1305                 :                :     {
                               1306                 :                :         /*
                               1307                 :                :          * The code path using a sub-executor is pretty good about cleaning up
                               1308                 :                :          * cruft, since the executor will make its own sub-context.  We don't
                               1309                 :                :          * really need an additional layer of sub-context in that case.
                               1310                 :                :          * However, if this is a utility statement, it won't make its own
                               1311                 :                :          * sub-context, so it seems advisable to make one that we can free on
                               1312                 :                :          * completion.
                               1313                 :                :          */
                               1314                 :             90 :         fcache->subcontext = AllocSetContextCreate(CurrentMemoryContext,
                               1315                 :                :                                                    "SQL function execution",
                               1316                 :                :                                                    ALLOCSET_DEFAULT_SIZES);
                               1317                 :             90 :         fcache->ownSubcontext = true;
                               1318                 :                :     }
                               1319                 :                :     else
                               1320                 :                :     {
                               1321                 :          40546 :         fcache->subcontext = CurrentMemoryContext;
                               1322                 :          40546 :         fcache->ownSubcontext = false;
                               1323                 :                :     }
                               1324                 :                : 
                               1325                 :                :     /*
                               1326                 :                :      * Build a tuplestore if needed, that is if it's a set-returning function
                               1327                 :                :      * and we're producing the function result without using lazyEval mode.
                               1328                 :                :      */
  127                          1329         [ +  + ]:          42224 :     if (es->setsResult)
                               1330                 :                :     {
                               1331         [ -  + ]:          42011 :         Assert(fcache->tstore == NULL);
                               1332   [ +  +  +  + ]:          42011 :         if (fcache->func->returnsSet && !es->lazyEval)
                               1333                 :                :         {
                               1334                 :            432 :             MemoryContextSwitchTo(fcache->tscontext);
                               1335                 :            432 :             fcache->tstore = tuplestore_begin_heap(fcache->randomAccess,
                               1336                 :                :                                                    false, work_mem);
                               1337                 :                :         }
                               1338                 :                :     }
                               1339                 :                : 
                               1340                 :                :     /* Switch into the selected subcontext (might be a no-op) */
                               1341                 :          42224 :     MemoryContextSwitchTo(fcache->subcontext);
                               1342                 :                : 
                               1343                 :                :     /*
                               1344                 :                :      * If this query produces the function result, collect its output using
                               1345                 :                :      * our custom DestReceiver; else discard any output.
                               1346                 :                :      */
 6154                          1347         [ +  + ]:          42224 :     if (es->setsResult)
                               1348                 :                :     {
                               1349                 :                :         DR_sqlfunction *myState;
                               1350                 :                : 
 6124                          1351                 :          42011 :         dest = CreateDestReceiver(DestSQLFunction);
                               1352                 :                :         /* pass down the needed info to the dest receiver routines */
 6154                          1353                 :          42011 :         myState = (DR_sqlfunction *) dest;
                               1354         [ -  + ]:          42011 :         Assert(myState->pub.mydest == DestSQLFunction);
  127                          1355                 :          42011 :         myState->tstore = fcache->tstore; /* might be NULL */
 6154                          1356                 :          42011 :         myState->filter = fcache->junkFilter;
                               1357                 :                : 
                               1358                 :                :         /* Make very sure the junkfilter's result slot is empty */
  127                          1359                 :          42011 :         ExecClearTuple(fcache->junkFilter->jf_resultSlot);
                               1360                 :                :     }
                               1361                 :                :     else
 6154                          1362                 :            213 :         dest = None_Receiver;
                               1363                 :                : 
 3157                          1364                 :          42224 :     es->qd = CreateQueryDesc(es->stmt,
  157                          1365                 :          42224 :                              fcache->func->src,
                               1366                 :                :                              GetActiveSnapshot(),
                               1367                 :                :                              InvalidSnapshot,
                               1368                 :                :                              dest,
                               1369                 :                :                              fcache->paramLI,
 3081 kgrittn@postgresql.o     1370         [ -  + ]:          42224 :                              es->qd ? es->qd->queryEnv : NULL,
                               1371                 :                :                              0);
                               1372                 :                : 
                               1373                 :                :     /* Utility commands don't need Executor. */
 3157 tgl@sss.pgh.pa.us        1374         [ +  + ]:          42224 :     if (es->qd->operation != CMD_UTILITY)
                               1375                 :                :     {
                               1376                 :                :         /*
                               1377                 :                :          * In lazyEval mode, do not let the executor set up an AfterTrigger
                               1378                 :                :          * context.  This is necessary not just an optimization, because we
                               1379                 :                :          * mustn't exit from the function execution with a stacked
                               1380                 :                :          * AfterTrigger level still active.  We are careful not to select
                               1381                 :                :          * lazyEval mode for any statement that could possibly queue triggers.
                               1382                 :                :          */
                               1383                 :                :         int         eflags;
                               1384                 :                : 
 5305                          1385         [ +  + ]:          42134 :         if (es->lazyEval)
                               1386                 :          41555 :             eflags = EXEC_FLAG_SKIP_TRIGGERS;
                               1387                 :                :         else
                               1388                 :            579 :             eflags = 0;         /* default run-to-completion flags */
  107 amitlan@postgresql.o     1389                 :          42134 :         ExecutorStart(es->qd, eflags);
                               1390                 :                :     }
                               1391                 :                : 
 8311 tgl@sss.pgh.pa.us        1392                 :          42221 :     es->status = F_EXEC_RUN;
                               1393                 :                : 
  142                          1394                 :          42221 :     MemoryContextSwitchTo(oldcontext);
10651 scrappy@hub.org          1395                 :          42221 : }
                               1396                 :                : 
                               1397                 :                : /* Run one execution_state; either to completion or to first result row */
                               1398                 :                : /* Returns true if we ran to completion */
                               1399                 :                : static bool
 6752 tgl@sss.pgh.pa.us        1400                 :          42715 : postquel_getnext(execution_state *es, SQLFunctionCachePtr fcache)
                               1401                 :                : {
                               1402                 :                :     bool        result;
                               1403                 :                :     MemoryContext oldcontext;
                               1404                 :                : 
                               1405                 :                :     /* Run the sub-executor in subcontext */
  142                          1406                 :          42715 :     oldcontext = MemoryContextSwitchTo(fcache->subcontext);
                               1407                 :                : 
 3157                          1408         [ +  + ]:          42715 :     if (es->qd->operation == CMD_UTILITY)
                               1409                 :                :     {
                               1410                 :             90 :         ProcessUtility(es->qd->plannedstmt,
  157                          1411                 :             90 :                        fcache->func->src,
                               1412                 :                :                        true,    /* protect function cache's parsetree */
                               1413                 :                :                        PROCESS_UTILITY_QUERY,
 6326 alvherre@alvh.no-ip.     1414                 :             90 :                        es->qd->params,
 3081 kgrittn@postgresql.o     1415                 :             90 :                        es->qd->queryEnv,
 6326 alvherre@alvh.no-ip.     1416                 :             90 :                        es->qd->dest,
                               1417                 :                :                        NULL);
 6154 tgl@sss.pgh.pa.us        1418                 :             45 :         result = true;          /* never stops early */
                               1419                 :                :     }
                               1420                 :                :     else
                               1421                 :                :     {
                               1422                 :                :         /* Run regular commands to completion unless lazyEval */
 3465                          1423                 :          42625 :         uint64      count = (es->lazyEval) ? 1 : 0;
                               1424                 :                : 
  271                          1425                 :          42625 :         ExecutorRun(es->qd, ForwardScanDirection, count);
                               1426                 :                : 
                               1427                 :                :         /*
                               1428                 :                :          * If we requested run to completion OR there was no tuple returned,
                               1429                 :                :          * command must be complete.
                               1430                 :                :          */
 3465                          1431   [ +  +  +  + ]:          38652 :         result = (count == 0 || es->qd->estate->es_processed == 0);
                               1432                 :                :     }
                               1433                 :                : 
  142                          1434                 :          38697 :     MemoryContextSwitchTo(oldcontext);
                               1435                 :                : 
 7663                          1436                 :          38697 :     return result;
                               1437                 :                : }
                               1438                 :                : 
                               1439                 :                : /* Shut down execution of one execution_state node */
                               1440                 :                : static void
  142                          1441                 :          38203 : postquel_end(execution_state *es, SQLFunctionCachePtr fcache)
                               1442                 :                : {
                               1443                 :                :     MemoryContext oldcontext;
                               1444                 :                : 
                               1445                 :                :     /* Run the sub-executor in subcontext */
                               1446                 :          38203 :     oldcontext = MemoryContextSwitchTo(fcache->subcontext);
                               1447                 :                : 
                               1448                 :                :     /* mark status done to ensure we don't do ExecutorEnd twice */
 8076                          1449                 :          38203 :     es->status = F_EXEC_DONE;
                               1450                 :                : 
                               1451                 :                :     /* Utility commands don't need Executor. */
 3157                          1452         [ +  + ]:          38203 :     if (es->qd->operation != CMD_UTILITY)
                               1453                 :                :     {
 5305                          1454                 :          38158 :         ExecutorFinish(es->qd);
 6326 alvherre@alvh.no-ip.     1455                 :          38149 :         ExecutorEnd(es->qd);
                               1456                 :                :     }
                               1457                 :                : 
 2921 peter_e@gmx.net          1458                 :          38194 :     es->qd->dest->rDestroy(es->qd->dest);
                               1459                 :                : 
 8301 tgl@sss.pgh.pa.us        1460                 :          38194 :     FreeQueryDesc(es->qd);
 8311                          1461                 :          38194 :     es->qd = NULL;
                               1462                 :                : 
  142                          1463                 :          38194 :     MemoryContextSwitchTo(oldcontext);
                               1464                 :                : 
                               1465                 :                :     /* Delete the subcontext, if it's actually a separate context */
                               1466         [ +  + ]:          38194 :     if (fcache->ownSubcontext)
                               1467                 :           1633 :         MemoryContextDelete(fcache->subcontext);
                               1468                 :          38194 :     fcache->subcontext = NULL;
10651 scrappy@hub.org          1469                 :          38194 : }
                               1470                 :                : 
                               1471                 :                : /* Build ParamListInfo array representing current arguments */
                               1472                 :                : static void
 8311 tgl@sss.pgh.pa.us        1473                 :          42204 : postquel_sub_params(SQLFunctionCachePtr fcache,
                               1474                 :                :                     FunctionCallInfo fcinfo)
                               1475                 :                : {
                               1476                 :          42204 :     int         nargs = fcinfo->nargs;
                               1477                 :                : 
                               1478         [ +  + ]:          42204 :     if (nargs > 0)
                               1479                 :                :     {
                               1480                 :                :         ParamListInfo paramLI;
  157                          1481                 :          37818 :         Oid        *argtypes = fcache->func->pinfo->argtypes;
  142                          1482                 :          37818 :         int16      *argtyplen = fcache->func->argtyplen;
                               1483                 :                : 
 6154                          1484         [ +  + ]:          37818 :         if (fcache->paramLI == NULL)
                               1485                 :                :         {
                               1486                 :                :             /* First time through: build a persistent ParamListInfo struct */
                               1487                 :                :             MemoryContext oldcontext;
                               1488                 :                : 
  142                          1489                 :          11773 :             oldcontext = MemoryContextSwitchTo(fcache->fcontext);
 2368 peter@eisentraut.org     1490                 :          11773 :             paramLI = makeParamList(nargs);
 6154 tgl@sss.pgh.pa.us        1491                 :          11773 :             fcache->paramLI = paramLI;
  142                          1492                 :          11773 :             MemoryContextSwitchTo(oldcontext);
                               1493                 :                :         }
                               1494                 :                :         else
                               1495                 :                :         {
 6154                          1496                 :          26045 :             paramLI = fcache->paramLI;
                               1497         [ -  + ]:          26045 :             Assert(paramLI->numParams == nargs);
                               1498                 :                :         }
                               1499                 :                : 
 2368 peter@eisentraut.org     1500         [ +  + ]:         111402 :         for (int i = 0; i < nargs; i++)
                               1501                 :                :         {
 7077 tgl@sss.pgh.pa.us        1502                 :          73584 :             ParamExternData *prm = &paramLI->params[i];
                               1503                 :                : 
                               1504                 :                :             /*
                               1505                 :                :              * If an incoming parameter value is a R/W expanded datum, we
                               1506                 :                :              * force it to R/O.  We'd be perfectly entitled to scribble on it,
                               1507                 :                :              * but the problem is that if the parameter is referenced more
                               1508                 :                :              * than once in the function, earlier references might mutate the
                               1509                 :                :              * value seen by later references, which won't do at all.  We
                               1510                 :                :              * could do better if we could be sure of the number of Param
                               1511                 :                :              * nodes in the function's plans; but we might not have planned
                               1512                 :                :              * all the statements yet, nor do we have plan tree walker
                               1513                 :                :              * infrastructure.  (Examining the parse trees is not good enough,
                               1514                 :                :              * because of possible function inlining during planning.)
                               1515                 :                :              */
 2415 andres@anarazel.de       1516                 :          73584 :             prm->isnull = fcinfo->args[i].isnull;
 1123 tgl@sss.pgh.pa.us        1517   [ +  +  +  + ]:          73584 :             prm->value = MakeExpandedObjectReadOnly(fcinfo->args[i].value,
                               1518                 :                :                                                     prm->isnull,
                               1519                 :                :                                                     argtyplen[i]);
                               1520                 :                :             /* Allow the value to be substituted into custom plans */
  157                          1521                 :          73584 :             prm->pflags = PARAM_FLAG_CONST;
 1123                          1522                 :          73584 :             prm->ptype = argtypes[i];
                               1523                 :                :         }
                               1524                 :                :     }
                               1525                 :                :     else
 6154                          1526                 :           4386 :         fcache->paramLI = NULL;
10651 scrappy@hub.org          1527                 :          42204 : }
                               1528                 :                : 
                               1529                 :                : /*
                               1530                 :                :  * Extract the SQL function's value from a single result row.  This is used
                               1531                 :                :  * both for scalar (non-set) functions and for each row of a lazy-eval set
                               1532                 :                :  * result.  We expect the current memory context to be that of the caller
                               1533                 :                :  * of fmgr_sql.
                               1534                 :                :  */
                               1535                 :                : static Datum
 6154 tgl@sss.pgh.pa.us        1536                 :          34247 : postquel_get_single_result(TupleTableSlot *slot,
                               1537                 :                :                            FunctionCallInfo fcinfo,
                               1538                 :                :                            SQLFunctionCachePtr fcache)
                               1539                 :                : {
                               1540                 :                :     Datum       value;
                               1541                 :                : 
                               1542                 :                :     /*
                               1543                 :                :      * Set up to return the function value.  For pass-by-reference datatypes,
                               1544                 :                :      * be sure to copy the result into the current context.  We can't leave
                               1545                 :                :      * the data in the TupleTableSlot because we must clear the slot before
                               1546                 :                :      * returning.
                               1547                 :                :      */
  157                          1548         [ +  + ]:          34247 :     if (fcache->func->returnsTuple)
                               1549                 :                :     {
                               1550                 :                :         /* We must return the whole tuple as a Datum. */
 7663                          1551                 :            735 :         fcinfo->isnull = false;
 2487 andres@anarazel.de       1552                 :            735 :         value = ExecFetchSlotHeapTupleDatum(slot);
                               1553                 :                :     }
                               1554                 :                :     else
                               1555                 :                :     {
                               1556                 :                :         /*
                               1557                 :                :          * Returning a scalar, which we have to extract from the first column
                               1558                 :                :          * of the SELECT result, and then copy into current context if needed.
                               1559                 :                :          */
 7479 tgl@sss.pgh.pa.us        1560                 :          33512 :         value = slot_getattr(slot, 1, &(fcinfo->isnull));
                               1561                 :                : 
 7663                          1562         [ +  + ]:          33512 :         if (!fcinfo->isnull)
  157                          1563                 :          33340 :             value = datumCopy(value, fcache->func->typbyval, fcache->func->typlen);
                               1564                 :                :     }
                               1565                 :                : 
                               1566                 :                :     /* Clear the slot for next time */
  127                          1567                 :          34247 :     ExecClearTuple(slot);
                               1568                 :                : 
 7663                          1569                 :          34247 :     return value;
                               1570                 :                : }
                               1571                 :                : 
                               1572                 :                : /*
                               1573                 :                :  * fmgr_sql: function call manager for SQL functions
                               1574                 :                :  */
                               1575                 :                : Datum
 9144                          1576                 :          42701 : fmgr_sql(PG_FUNCTION_ARGS)
                               1577                 :                : {
                               1578                 :                :     SQLFunctionCachePtr fcache;
                               1579                 :                :     ErrorContextCallback sqlerrcontext;
                               1580                 :                :     MemoryContext tscontext;
                               1581                 :                :     bool        randomAccess;
                               1582                 :                :     bool        lazyEvalOK;
                               1583                 :                :     bool        pushed_snapshot;
                               1584                 :                :     execution_state *es;
                               1585                 :                :     TupleTableSlot *slot;
                               1586                 :                :     Datum       result;
                               1587                 :                : 
                               1588                 :                :     /* Check call context */
 6154                          1589         [ +  + ]:          42701 :     if (fcinfo->flinfo->fn_retset)
                               1590                 :                :     {
                               1591                 :           2520 :         ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
                               1592                 :                : 
                               1593                 :                :         /*
                               1594                 :                :          * For simplicity, we require callers to support both set eval modes.
                               1595                 :                :          * There are cases where we must use one or must use the other, and
                               1596                 :                :          * it's not really worthwhile to postpone the check till we know. But
                               1597                 :                :          * note we do not require caller to provide an expectedDesc.
                               1598                 :                :          */
                               1599   [ +  -  +  - ]:           2520 :         if (!rsi || !IsA(rsi, ReturnSetInfo) ||
                               1600         [ +  - ]:           2520 :             (rsi->allowedModes & SFRM_ValuePerCall) == 0 ||
 5931                          1601         [ -  + ]:           2520 :             (rsi->allowedModes & SFRM_Materialize) == 0)
 6154 tgl@sss.pgh.pa.us        1602         [ #  # ]:UBC           0 :             ereport(ERROR,
                               1603                 :                :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               1604                 :                :                      errmsg("set-valued function called in context that cannot accept a set")));
 6154 tgl@sss.pgh.pa.us        1605                 :CBC        2520 :         randomAccess = rsi->allowedModes & SFRM_Materialize_Random;
                               1606                 :           2520 :         lazyEvalOK = !(rsi->allowedModes & SFRM_Materialize_Preferred);
                               1607                 :                :         /* tuplestore, if used, must have query lifespan */
  127                          1608                 :           2520 :         tscontext = rsi->econtext->ecxt_per_query_memory;
                               1609                 :                :     }
                               1610                 :                :     else
                               1611                 :                :     {
 6154                          1612                 :          40181 :         randomAccess = false;
                               1613                 :          40181 :         lazyEvalOK = true;
                               1614                 :                :         /* we won't need a tuplestore */
  127                          1615                 :          40181 :         tscontext = NULL;
                               1616                 :                :     }
                               1617                 :                : 
                               1618                 :                :     /*
                               1619                 :                :      * Initialize fcache if starting a fresh execution.
                               1620                 :                :      */
  157                          1621                 :          42701 :     fcache = init_sql_fcache(fcinfo, lazyEvalOK);
                               1622                 :                : 
                               1623                 :                :     /* Mark fcache as active */
   17                          1624                 :          42698 :     fcache->active = true;
                               1625                 :                : 
                               1626                 :                :     /* Remember info that we might need later to construct tuplestore */
  127                          1627                 :          42698 :     fcache->tscontext = tscontext;
                               1628                 :          42698 :     fcache->randomAccess = randomAccess;
                               1629                 :                : 
                               1630                 :                :     /*
                               1631                 :                :      * Now we can set up error traceback support for ereport()
                               1632                 :                :      */
  157                          1633                 :          42698 :     sqlerrcontext.callback = sql_exec_error_callback;
                               1634                 :          42698 :     sqlerrcontext.arg = fcache;
                               1635                 :          42698 :     sqlerrcontext.previous = error_context_stack;
                               1636                 :          42698 :     error_context_stack = &sqlerrcontext;
                               1637                 :                : 
                               1638                 :                :     /*
                               1639                 :                :      * Find first unfinished execution_state.  If none, advance to the next
                               1640                 :                :      * query in function.
                               1641                 :                :      */
                               1642                 :                :     do
                               1643                 :                :     {
                               1644                 :          84871 :         es = fcache->eslist;
 5304                          1645   [ +  +  -  + ]:          84871 :         while (es && es->status == F_EXEC_DONE)
 5304 tgl@sss.pgh.pa.us        1646                 :UBC           0 :             es = es->next;
 5304 tgl@sss.pgh.pa.us        1647         [ +  + ]:CBC       84871 :         if (es)
                               1648                 :          42667 :             break;
  157                          1649         [ +  + ]:          42204 :     } while (init_execution_state(fcache));
                               1650                 :                : 
                               1651                 :                :     /*
                               1652                 :                :      * Execute each command in the function one after another until we either
                               1653                 :                :      * run out of commands or get a result row from a lazily-evaluated SELECT.
                               1654                 :                :      *
                               1655                 :                :      * Notes about snapshot management:
                               1656                 :                :      *
                               1657                 :                :      * In a read-only function, we just use the surrounding query's snapshot.
                               1658                 :                :      *
                               1659                 :                :      * In a non-read-only function, we rely on the fact that we'll never
                               1660                 :                :      * suspend execution between queries of the function: the only reason to
                               1661                 :                :      * suspend execution before completion is if we are returning a row from a
                               1662                 :                :      * lazily-evaluated SELECT.  So, when first entering this loop, we'll
                               1663                 :                :      * either start a new query (and push a fresh snapshot) or re-establish
                               1664                 :                :      * the active snapshot from the existing query descriptor.  If we need to
                               1665                 :                :      * start a new query in a subsequent execution of the loop, either we need
                               1666                 :                :      * a fresh snapshot (and pushed_snapshot is false) or the existing
                               1667                 :                :      * snapshot is on the active stack and we can just bump its command ID.
                               1668                 :                :      */
 5304                          1669                 :          42670 :     pushed_snapshot = false;
 8415                          1670         [ +  + ]:          80861 :     while (es)
                               1671                 :                :     {
                               1672                 :                :         bool        completed;
                               1673                 :                : 
 6154                          1674         [ +  + ]:          42718 :         if (es->status == F_EXEC_START)
                               1675                 :                :         {
                               1676                 :                :             /*
                               1677                 :                :              * If not read-only, be sure to advance the command counter for
                               1678                 :                :              * each command, so that all work to date in this transaction is
                               1679                 :                :              * visible.  Take a new snapshot if we don't have one yet,
                               1680                 :                :              * otherwise just bump the command ID in the existing snapshot.
                               1681                 :                :              */
  157                          1682         [ +  + ]:          42224 :             if (!fcache->func->readonly_func)
                               1683                 :                :             {
 5304                          1684                 :          36977 :                 CommandCounterIncrement();
                               1685         [ +  + ]:          36977 :                 if (!pushed_snapshot)
                               1686                 :                :                 {
                               1687                 :          36974 :                     PushActiveSnapshot(GetTransactionSnapshot());
                               1688                 :          36974 :                     pushed_snapshot = true;
                               1689                 :                :                 }
                               1690                 :                :                 else
                               1691                 :              3 :                     UpdateActiveSnapshotCommandId();
                               1692                 :                :             }
                               1693                 :                : 
 6154                          1694                 :          42224 :             postquel_start(es, fcache);
                               1695                 :                :         }
  157                          1696   [ +  +  +  - ]:            494 :         else if (!fcache->func->readonly_func && !pushed_snapshot)
                               1697                 :                :         {
                               1698                 :                :             /* Re-establish active snapshot when re-entering function */
 5304                          1699                 :            320 :             PushActiveSnapshot(es->qd->snapshot);
                               1700                 :            320 :             pushed_snapshot = true;
                               1701                 :                :         }
                               1702                 :                : 
 6154                          1703                 :          42715 :         completed = postquel_getnext(es, fcache);
                               1704                 :                : 
                               1705                 :                :         /*
                               1706                 :                :          * If we ran the command to completion, we can shut it down now. Any
                               1707                 :                :          * row(s) we need to return are safely stashed in the result slot or
                               1708                 :                :          * tuplestore, and we want to be sure that, for example, AFTER
                               1709                 :                :          * triggers get fired before we return anything.  Also, if the
                               1710                 :                :          * function doesn't return set, we can shut it down anyway because it
                               1711                 :                :          * must be a SELECT and we don't care about fetching any more result
                               1712                 :                :          * rows.
                               1713                 :                :          */
  157                          1714   [ +  +  +  + ]:          38697 :         if (completed || !fcache->func->returnsSet)
  142                          1715                 :          38200 :             postquel_end(es, fcache);
                               1716                 :                : 
                               1717                 :                :         /*
                               1718                 :                :          * Break from loop if we didn't shut down (implying we got a
                               1719                 :                :          * lazily-evaluated row).  Otherwise we'll press on till the whole
                               1720                 :                :          * function is done, relying on the tuplestore to keep hold of the
                               1721                 :                :          * data to eventually be returned.  This is necessary since an
                               1722                 :                :          * INSERT/UPDATE/DELETE RETURNING that sets the result might be
                               1723                 :                :          * followed by additional rule-inserted commands, and we want to
                               1724                 :                :          * finish doing all those commands before we return anything.
                               1725                 :                :          */
10226 bruce@momjian.us         1726         [ +  + ]:          38688 :         if (es->status != F_EXEC_DONE)
                               1727                 :            497 :             break;
                               1728                 :                : 
                               1729                 :                :         /*
                               1730                 :                :          * Advance to next execution_state, and perhaps next query.
                               1731                 :                :          */
                               1732                 :          38191 :         es = es->next;
 5304 tgl@sss.pgh.pa.us        1733         [ +  + ]:          38239 :         while (!es)
                               1734                 :                :         {
                               1735                 :                :             /*
                               1736                 :                :              * Flush the current snapshot so that we will take a new one for
                               1737                 :                :              * the new query list.  This ensures that new snaps are taken at
                               1738                 :                :              * original-query boundaries, matching the behavior of interactive
                               1739                 :                :              * execution.
                               1740                 :                :              */
                               1741         [ +  + ]:          38188 :             if (pushed_snapshot)
                               1742                 :                :             {
                               1743                 :          32966 :                 PopActiveSnapshot();
                               1744                 :          32966 :                 pushed_snapshot = false;
                               1745                 :                :             }
                               1746                 :                : 
  157                          1747         [ +  + ]:          38188 :             if (!init_execution_state(fcache))
                               1748                 :          38140 :                 break;          /* end of function */
                               1749                 :                : 
                               1750                 :             48 :             es = fcache->eslist;
                               1751                 :                :         }
                               1752                 :                :     }
                               1753                 :                : 
                               1754                 :                :     /*
                               1755                 :                :      * The result slot or tuplestore now contains whatever row(s) we are
                               1756                 :                :      * supposed to return.
                               1757                 :                :      */
                               1758         [ +  + ]:          38640 :     if (fcache->func->returnsSet)
                               1759                 :                :     {
 6154                          1760                 :           2517 :         ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
                               1761                 :                : 
                               1762         [ +  + ]:           2517 :         if (es)
                               1763                 :                :         {
                               1764                 :                :             /*
                               1765                 :                :              * If we stopped short of being done, we must have a lazy-eval
                               1766                 :                :              * row.
                               1767                 :                :              */
                               1768         [ -  + ]:            497 :             Assert(es->lazyEval);
                               1769                 :                :             /* The junkfilter's result slot contains the query result tuple */
                               1770         [ -  + ]:            497 :             Assert(fcache->junkFilter);
                               1771                 :            497 :             slot = fcache->junkFilter->jf_resultSlot;
  127                          1772         [ -  + ]:            497 :             Assert(!TTS_EMPTY(slot));
                               1773                 :                :             /* Extract the result as a datum, and copy out from the slot */
  142                          1774                 :            497 :             result = postquel_get_single_result(slot, fcinfo, fcache);
                               1775                 :                : 
                               1776                 :                :             /*
                               1777                 :                :              * Let caller know we're not finished.
                               1778                 :                :              */
 6154                          1779                 :            497 :             rsi->isDone = ExprMultipleResult;
                               1780                 :                : 
                               1781                 :                :             /*
                               1782                 :                :              * Ensure we will get shut down cleanly if the exprcontext is not
                               1783                 :                :              * run to completion.
                               1784                 :                :              */
                               1785         [ +  + ]:            497 :             if (!fcache->shutdown_reg)
                               1786                 :                :             {
                               1787                 :            363 :                 RegisterExprContextCallback(rsi->econtext,
                               1788                 :                :                                             ShutdownSQLFunction,
                               1789                 :                :                                             PointerGetDatum(fcache));
                               1790                 :            363 :                 fcache->shutdown_reg = true;
                               1791                 :                :             }
                               1792                 :                :         }
                               1793         [ +  + ]:           2020 :         else if (fcache->lazyEval)
                               1794                 :                :         {
                               1795                 :                :             /*
                               1796                 :                :              * We are done with a lazy evaluation.  Let caller know we're
                               1797                 :                :              * finished.
                               1798                 :                :              */
                               1799                 :           1585 :             rsi->isDone = ExprEndResult;
                               1800                 :                : 
 9144                          1801                 :           1585 :             fcinfo->isnull = true;
                               1802                 :           1585 :             result = (Datum) 0;
                               1803                 :                : 
                               1804                 :                :             /* Deregister shutdown callback, if we made one */
 8518                          1805         [ +  + ]:           1585 :             if (fcache->shutdown_reg)
                               1806                 :                :             {
                               1807                 :            360 :                 UnregisterExprContextCallback(rsi->econtext,
                               1808                 :                :                                               ShutdownSQLFunction,
                               1809                 :                :                                               PointerGetDatum(fcache));
                               1810                 :            360 :                 fcache->shutdown_reg = false;
                               1811                 :                :             }
                               1812                 :                :         }
                               1813                 :                :         else
                               1814                 :                :         {
                               1815                 :                :             /*
                               1816                 :                :              * We are done with a non-lazy evaluation.  Return whatever is in
                               1817                 :                :              * the tuplestore.  (It is now caller's responsibility to free the
                               1818                 :                :              * tuplestore when done.)
                               1819                 :                :              *
                               1820                 :                :              * Note an edge case: we could get here without having made a
                               1821                 :                :              * tuplestore if the function is declared to return SETOF VOID.
                               1822                 :                :              * ExecMakeTableFunctionResult will cope with null setResult.
                               1823                 :                :              */
  127                          1824   [ +  +  -  + ]:            435 :             Assert(fcache->tstore || fcache->func->rettype == VOIDOID);
 6154                          1825                 :            435 :             rsi->returnMode = SFRM_Materialize;
                               1826                 :            435 :             rsi->setResult = fcache->tstore;
                               1827                 :            435 :             fcache->tstore = NULL;
                               1828                 :                :             /* must copy desc because execSRF.c will free it */
                               1829         [ +  + ]:            435 :             if (fcache->junkFilter)
                               1830                 :            432 :                 rsi->setDesc = CreateTupleDescCopy(fcache->junkFilter->jf_cleanTupType);
                               1831                 :                : 
                               1832                 :            435 :             fcinfo->isnull = true;
                               1833                 :            435 :             result = (Datum) 0;
                               1834                 :                : 
                               1835                 :                :             /* Deregister shutdown callback, if we made one */
                               1836         [ -  + ]:            435 :             if (fcache->shutdown_reg)
                               1837                 :                :             {
 6154 tgl@sss.pgh.pa.us        1838                 :UBC           0 :                 UnregisterExprContextCallback(rsi->econtext,
                               1839                 :                :                                               ShutdownSQLFunction,
                               1840                 :                :                                               PointerGetDatum(fcache));
                               1841                 :              0 :                 fcache->shutdown_reg = false;
                               1842                 :                :             }
                               1843                 :                :         }
                               1844                 :                :     }
                               1845                 :                :     else
                               1846                 :                :     {
                               1847                 :                :         /*
                               1848                 :                :          * Non-set function.  If we got a row, return it; else return NULL.
                               1849                 :                :          */
 6154 tgl@sss.pgh.pa.us        1850         [ +  + ]:CBC       36123 :         if (fcache->junkFilter)
                               1851                 :                :         {
                               1852                 :                :             /* The junkfilter's result slot contains the query result tuple */
                               1853                 :          36016 :             slot = fcache->junkFilter->jf_resultSlot;
  127                          1854         [ +  + ]:          36016 :             if (!TTS_EMPTY(slot))
  142                          1855                 :          33750 :                 result = postquel_get_single_result(slot, fcinfo, fcache);
                               1856                 :                :             else
                               1857                 :                :             {
 6154                          1858                 :           2266 :                 fcinfo->isnull = true;
                               1859                 :           2266 :                 result = (Datum) 0;
                               1860                 :                :             }
                               1861                 :                :         }
                               1862                 :                :         else
                               1863                 :                :         {
                               1864                 :                :             /* Should only get here for VOID functions and procedures */
  157                          1865         [ -  + ]:            107 :             Assert(fcache->func->rettype == VOIDOID);
 6154                          1866                 :            107 :             fcinfo->isnull = true;
                               1867                 :            107 :             result = (Datum) 0;
                               1868                 :                :         }
                               1869                 :                :     }
                               1870                 :                : 
                               1871                 :                :     /* Pop snapshot if we have pushed one */
 5304                          1872         [ +  + ]:          38640 :     if (pushed_snapshot)
                               1873                 :            323 :         PopActiveSnapshot();
                               1874                 :                : 
                               1875                 :                :     /*
                               1876                 :                :      * If we've gone through every command in the function, we are done. Reset
                               1877                 :                :      * state to start over again on next call.
                               1878                 :                :      */
 6154                          1879         [ +  + ]:          38640 :     if (es == NULL)
  142                          1880                 :          38143 :         fcache->eslist = NULL;
                               1881                 :                : 
                               1882                 :                :     /* Mark fcache as inactive */
   17                          1883                 :          38640 :     fcache->active = false;
                               1884                 :                : 
 8076                          1885                 :          38640 :     error_context_stack = sqlerrcontext.previous;
                               1886                 :                : 
10226 bruce@momjian.us         1887                 :          38640 :     return result;
                               1888                 :                : }
                               1889                 :                : 
                               1890                 :                : 
                               1891                 :                : /*
                               1892                 :                :  * error context callback to let us supply a traceback during compile
                               1893                 :                :  */
                               1894                 :                : static void
  157 tgl@sss.pgh.pa.us        1895                 :              3 : sql_compile_error_callback(void *arg)
                               1896                 :                : {
                               1897                 :              3 :     SQLFunctionHashEntry *func = (SQLFunctionHashEntry *) arg;
                               1898                 :                :     int         syntaxerrposition;
                               1899                 :                : 
                               1900                 :                :     /*
                               1901                 :                :      * We can do nothing useful if sql_compile_callback() didn't get as far as
                               1902                 :                :      * copying the function name
                               1903                 :                :      */
                               1904         [ -  + ]:              3 :     if (func->fname == NULL)
 5650 tgl@sss.pgh.pa.us        1905                 :UBC           0 :         return;
                               1906                 :                : 
                               1907                 :                :     /*
                               1908                 :                :      * If there is a syntax error position, convert to internal syntax error
                               1909                 :                :      */
 7839 tgl@sss.pgh.pa.us        1910                 :CBC           3 :     syntaxerrposition = geterrposition();
  157                          1911   [ -  +  -  - ]:              3 :     if (syntaxerrposition > 0 && func->src != NULL)
                               1912                 :                :     {
 7839 tgl@sss.pgh.pa.us        1913                 :UBC           0 :         errposition(0);
                               1914                 :              0 :         internalerrposition(syntaxerrposition);
  157                          1915                 :              0 :         internalerrquery(func->src);
                               1916                 :                :     }
                               1917                 :                : 
                               1918                 :                :     /*
                               1919                 :                :      * sql_compile_callback() doesn't do any per-query processing, so just
                               1920                 :                :      * report the context as "during startup".
                               1921                 :                :      */
  157 tgl@sss.pgh.pa.us        1922                 :CBC           3 :     errcontext("SQL function \"%s\" during startup", func->fname);
                               1923                 :                : }
                               1924                 :                : 
                               1925                 :                : /*
                               1926                 :                :  * error context callback to let us supply a call-stack traceback at runtime
                               1927                 :                :  */
                               1928                 :                : static void
                               1929                 :           4077 : sql_exec_error_callback(void *arg)
                               1930                 :                : {
                               1931                 :           4077 :     SQLFunctionCachePtr fcache = (SQLFunctionCachePtr) arg;
                               1932                 :                :     int         syntaxerrposition;
                               1933                 :                : 
                               1934                 :                :     /*
                               1935                 :                :      * If there is a syntax error position, convert to internal syntax error
                               1936                 :                :      */
                               1937                 :           4077 :     syntaxerrposition = geterrposition();
                               1938   [ +  +  +  - ]:           4077 :     if (syntaxerrposition > 0 && fcache->func->src != NULL)
                               1939                 :                :     {
                               1940                 :              1 :         errposition(0);
                               1941                 :              1 :         internalerrposition(syntaxerrposition);
                               1942                 :              1 :         internalerrquery(fcache->func->src);
                               1943                 :                :     }
                               1944                 :                : 
                               1945                 :                :     /*
                               1946                 :                :      * If we failed while executing an identifiable query within the function,
                               1947                 :                :      * report that.  Otherwise say it was "during startup".
                               1948                 :                :      */
                               1949         [ +  - ]:           4077 :     if (fcache->error_query_index > 0)
                               1950                 :           4077 :         errcontext("SQL function \"%s\" statement %d",
                               1951                 :           4077 :                    fcache->func->fname, fcache->error_query_index);
                               1952                 :                :     else
  157 tgl@sss.pgh.pa.us        1953                 :UBC           0 :         errcontext("SQL function \"%s\" during startup", fcache->func->fname);
 8076 tgl@sss.pgh.pa.us        1954                 :CBC        4077 : }
                               1955                 :                : 
                               1956                 :                : 
                               1957                 :                : /*
                               1958                 :                :  * ExprContext callback function
                               1959                 :                :  *
                               1960                 :                :  * We register this in the active ExprContext while a set-returning SQL
                               1961                 :                :  * function is running, in case the function needs to be shut down before it
                               1962                 :                :  * has been run to completion.  Note that this will not be called during an
                               1963                 :                :  * error abort, but we don't need it because transaction abort will take care
                               1964                 :                :  * of releasing executor resources.
                               1965                 :                :  */
                               1966                 :                : static void
 8518                          1967                 :              3 : ShutdownSQLFunction(Datum arg)
                               1968                 :                : {
  142                          1969                 :              3 :     SQLFunctionCachePtr fcache = (SQLFunctionCachePtr) DatumGetPointer(arg);
                               1970                 :                :     execution_state *es;
                               1971                 :                : 
                               1972                 :              3 :     es = fcache->eslist;
                               1973         [ +  + ]:              6 :     while (es)
                               1974                 :                :     {
                               1975                 :                :         /* Shut down anything still running */
                               1976         [ +  - ]:              3 :         if (es->status == F_EXEC_RUN)
                               1977                 :                :         {
                               1978                 :                :             /* Re-establish active snapshot for any called functions */
                               1979         [ +  - ]:              3 :             if (!fcache->func->readonly_func)
                               1980                 :              3 :                 PushActiveSnapshot(es->qd->snapshot);
                               1981                 :                : 
                               1982                 :              3 :             postquel_end(es, fcache);
                               1983                 :                : 
                               1984         [ +  - ]:              3 :             if (!fcache->func->readonly_func)
                               1985                 :              3 :                 PopActiveSnapshot();
                               1986                 :                :         }
                               1987                 :              3 :         es = es->next;
                               1988                 :                :     }
                               1989                 :              3 :     fcache->eslist = NULL;
                               1990                 :                : 
                               1991                 :                :     /* Release tuplestore if we have one */
                               1992         [ -  + ]:              3 :     if (fcache->tstore)
  142 tgl@sss.pgh.pa.us        1993                 :UBC           0 :         tuplestore_end(fcache->tstore);
  142 tgl@sss.pgh.pa.us        1994                 :CBC           3 :     fcache->tstore = NULL;
                               1995                 :                : 
                               1996                 :                :     /* Release CachedPlan if we have one */
                               1997         [ +  - ]:              3 :     if (fcache->cplan)
                               1998                 :              3 :         ReleaseCachedPlan(fcache->cplan, fcache->cowner);
                               1999                 :              3 :     fcache->cplan = NULL;
                               2000                 :                : 
                               2001                 :                :     /* execUtils will deregister the callback... */
                               2002                 :              3 :     fcache->shutdown_reg = false;
  157                          2003                 :              3 : }
                               2004                 :                : 
                               2005                 :                : /*
                               2006                 :                :  * MemoryContext callback function
                               2007                 :                :  *
                               2008                 :                :  * We register this in the memory context that contains a SQLFunctionCache
                               2009                 :                :  * struct.  When the memory context is reset or deleted, we release the
                               2010                 :                :  * reference count (if any) that the cache holds on the long-lived hash entry.
                               2011                 :                :  * Note that this will happen even during error aborts.
                               2012                 :                :  */
                               2013                 :                : static void
  142                          2014                 :          16144 : RemoveSQLFunctionCache(void *arg)
                               2015                 :                : {
                               2016                 :          16144 :     SQLFunctionCache *fcache = (SQLFunctionCache *) arg;
                               2017                 :                : 
                               2018                 :                :     /* Release reference count on SQLFunctionHashEntry */
                               2019         [ +  + ]:          16144 :     if (fcache->func != NULL)
                               2020                 :                :     {
                               2021         [ -  + ]:          16141 :         Assert(fcache->func->cfunc.use_count > 0);
                               2022                 :          16141 :         fcache->func->cfunc.use_count--;
                               2023                 :                :         /* This should be unnecessary, but let's just be sure: */
                               2024                 :          16141 :         fcache->func = NULL;
                               2025                 :                :     }
 8518                          2026                 :          16144 : }
                               2027                 :                : 
                               2028                 :                : /*
                               2029                 :                :  * check_sql_fn_statements
                               2030                 :                :  *
                               2031                 :                :  * Check statements in an SQL function.  Error out if there is anything that
                               2032                 :                :  * is not acceptable.
                               2033                 :                :  */
                               2034                 :                : void
 1783                          2035                 :           3634 : check_sql_fn_statements(List *queryTreeLists)
                               2036                 :                : {
                               2037                 :                :     ListCell   *lc;
                               2038                 :                : 
                               2039                 :                :     /* We are given a list of sublists of Queries */
                               2040   [ +  +  +  +  :           7323 :     foreach(lc, queryTreeLists)
                                              +  + ]
                               2041                 :                :     {
                               2042                 :           3692 :         List       *sublist = lfirst_node(List, lc);
                               2043                 :                : 
  157                          2044                 :           3692 :         check_sql_fn_statement(sublist);
                               2045                 :                :     }
                               2046                 :           3631 : }
                               2047                 :                : 
                               2048                 :                : /*
                               2049                 :                :  * As above, for a single sublist of Queries.
                               2050                 :                :  */
                               2051                 :                : static void
                               2052                 :           4920 : check_sql_fn_statement(List *queryTreeList)
                               2053                 :                : {
                               2054                 :                :     ListCell   *lc;
                               2055                 :                : 
                               2056   [ +  -  +  +  :           9840 :     foreach(lc, queryTreeList)
                                              +  + ]
                               2057                 :                :     {
                               2058                 :           4923 :         Query      *query = lfirst_node(Query, lc);
                               2059                 :                : 
                               2060                 :                :         /*
                               2061                 :                :          * Disallow calling procedures with output arguments.  The current
                               2062                 :                :          * implementation would just throw the output values away, unless the
                               2063                 :                :          * statement is the last one.  Per SQL standard, we should assign the
                               2064                 :                :          * output values by name.  By disallowing this here, we preserve an
                               2065                 :                :          * opportunity for future improvement.
                               2066                 :                :          */
                               2067         [ +  + ]:           4923 :         if (query->commandType == CMD_UTILITY &&
                               2068         [ +  + ]:             77 :             IsA(query->utilityStmt, CallStmt))
                               2069                 :                :         {
                               2070                 :             15 :             CallStmt   *stmt = (CallStmt *) query->utilityStmt;
                               2071                 :                : 
                               2072         [ +  + ]:             15 :             if (stmt->outargs != NIL)
                               2073         [ +  - ]:              3 :                 ereport(ERROR,
                               2074                 :                :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               2075                 :                :                          errmsg("calling procedures with output arguments is not supported in SQL functions")));
                               2076                 :                :         }
                               2077                 :                :     }
 2733 peter_e@gmx.net          2078                 :           4917 : }
                               2079                 :                : 
                               2080                 :                : /*
                               2081                 :                :  * check_sql_fn_retval()
                               2082                 :                :  *      Check return value of a list of lists of sql parse trees.
                               2083                 :                :  *
                               2084                 :                :  * The return value of a sql function is the value returned by the last
                               2085                 :                :  * canSetTag query in the function.  We do some ad-hoc type checking and
                               2086                 :                :  * coercion here to ensure that the function returns what it's supposed to.
                               2087                 :                :  * Note that we may actually modify the last query to make it match!
                               2088                 :                :  *
                               2089                 :                :  * This function returns true if the sql function returns the entire tuple
                               2090                 :                :  * result of its final statement, or false if it returns just the first column
                               2091                 :                :  * result of that statement.  It throws an error if the final statement doesn't
                               2092                 :                :  * return the right type at all.
                               2093                 :                :  *
                               2094                 :                :  * Note that because we allow "SELECT rowtype_expression", the result can be
                               2095                 :                :  * false even when the declared function return type is a rowtype.
                               2096                 :                :  *
                               2097                 :                :  * For a polymorphic function the passed rettype must be the actual resolved
                               2098                 :                :  * output type of the function.  (This means we can't check the type during
                               2099                 :                :  * function definition of a polymorphic function.)  If we do see a polymorphic
                               2100                 :                :  * rettype we'll throw an error, saying it is not a supported rettype.
                               2101                 :                :  *
                               2102                 :                :  * If the function returns composite, the passed rettupdesc should describe
                               2103                 :                :  * the expected output.  If rettupdesc is NULL, we can't verify that the
                               2104                 :                :  * output matches; that should only happen in fmgr_sql_validator(), or when
                               2105                 :                :  * the function returns RECORD and the caller doesn't actually care which
                               2106                 :                :  * composite type it is.
                               2107                 :                :  *
                               2108                 :                :  * (Typically, rettype and rettupdesc are computed by get_call_result_type
                               2109                 :                :  * or a sibling function.)
                               2110                 :                :  *
                               2111                 :                :  * In addition to coercing individual output columns, we can modify the
                               2112                 :                :  * output to include dummy NULL columns for any dropped columns appearing
                               2113                 :                :  * in rettupdesc.  This is done only if the caller asks for it.
                               2114                 :                :  */
                               2115                 :                : bool
 1783 tgl@sss.pgh.pa.us        2116                 :           5398 : check_sql_fn_retval(List *queryTreeLists,
                               2117                 :                :                     Oid rettype, TupleDesc rettupdesc,
                               2118                 :                :                     char prokind,
                               2119                 :                :                     bool insertDroppedCols)
                               2120                 :                : {
                               2121                 :                :     List       *queryTreeList;
                               2122                 :                : 
                               2123                 :                :     /*
                               2124                 :                :      * We consider only the last sublist of Query nodes, so that only the last
                               2125                 :                :      * original statement is a candidate to produce the result.  This is a
                               2126                 :                :      * change from pre-v18 versions, which would back up to the last statement
                               2127                 :                :      * that includes a canSetTag query, thus ignoring any ending statement(s)
                               2128                 :                :      * that rewrite to DO INSTEAD NOTHING.  That behavior was undocumented and
                               2129                 :                :      * there seems no good reason for it, except that it was an artifact of
                               2130                 :                :      * the original coding.
                               2131                 :                :      *
                               2132                 :                :      * If the function body is completely empty, handle that the same as if
                               2133                 :                :      * the last query had rewritten to nothing.
                               2134                 :                :      */
  157                          2135         [ +  + ]:           5398 :     if (queryTreeLists != NIL)
                               2136                 :           5386 :         queryTreeList = llast_node(List, queryTreeLists);
                               2137                 :                :     else
                               2138                 :             12 :         queryTreeList = NIL;
                               2139                 :                : 
                               2140                 :           5398 :     return check_sql_stmt_retval(queryTreeList,
                               2141                 :                :                                  rettype, rettupdesc,
                               2142                 :                :                                  prokind, insertDroppedCols);
                               2143                 :                : }
                               2144                 :                : 
                               2145                 :                : /*
                               2146                 :                :  * As for check_sql_fn_retval, but we are given just the last query's
                               2147                 :                :  * rewritten-queries list.
                               2148                 :                :  */
                               2149                 :                : static bool
                               2150                 :           6591 : check_sql_stmt_retval(List *queryTreeList,
                               2151                 :                :                       Oid rettype, TupleDesc rettupdesc,
                               2152                 :                :                       char prokind, bool insertDroppedCols)
                               2153                 :                : {
 2068                          2154                 :           6591 :     bool        is_tuple_result = false;
                               2155                 :                :     Query      *parse;
                               2156                 :                :     ListCell   *parse_cell;
                               2157                 :                :     List       *tlist;
                               2158                 :                :     int         tlistlen;
                               2159                 :                :     bool        tlist_is_modifiable;
                               2160                 :                :     char        fn_typtype;
                               2161                 :           6591 :     List       *upper_tlist = NIL;
                               2162                 :           6591 :     bool        upper_tlist_nontrivial = false;
                               2163                 :                :     ListCell   *lc;
                               2164                 :                : 
                               2165                 :                :     /*
                               2166                 :                :      * If it's declared to return VOID, we don't care what's in the function.
                               2167                 :                :      * (This takes care of procedures with no output parameters, as well.)
                               2168                 :                :      */
 2745 peter_e@gmx.net          2169         [ +  + ]:           6591 :     if (rettype == VOIDOID)
 2837                          2170                 :            313 :         return false;
                               2171                 :                : 
                               2172                 :                :     /*
                               2173                 :                :      * Find the last canSetTag query in the list of Query nodes.  This isn't
                               2174                 :                :      * necessarily the last parsetree, because rule rewriting can insert
                               2175                 :                :      * queries after what the user wrote.
                               2176                 :                :      */
 6154 tgl@sss.pgh.pa.us        2177                 :           6278 :     parse = NULL;
 2068                          2178                 :           6278 :     parse_cell = NULL;
  157                          2179   [ +  +  +  +  :          12556 :     foreach(lc, queryTreeList)
                                              +  + ]
                               2180                 :                :     {
                               2181                 :           6278 :         Query      *q = lfirst_node(Query, lc);
                               2182                 :                : 
                               2183         [ +  + ]:           6278 :         if (q->canSetTag)
                               2184                 :                :         {
                               2185                 :           6275 :             parse = q;
                               2186                 :           6275 :             parse_cell = lc;
                               2187                 :                :         }
                               2188                 :                :     }
                               2189                 :                : 
                               2190                 :                :     /*
                               2191                 :                :      * If it's a plain SELECT, it returns whatever the targetlist says.
                               2192                 :                :      * Otherwise, if it's INSERT/UPDATE/DELETE/MERGE with RETURNING, it
                               2193                 :                :      * returns that. Otherwise, the function return type must be VOID.
                               2194                 :                :      *
                               2195                 :                :      * Note: eventually replace this test with QueryReturnsTuples?  We'd need
                               2196                 :                :      * a more general method of determining the output type, though.  Also, it
                               2197                 :                :      * seems too dangerous to consider FETCH or EXECUTE as returning a
                               2198                 :                :      * determinable rowtype, since they depend on relatively short-lived
                               2199                 :                :      * entities.
                               2200                 :                :      */
 6154                          2201         [ +  + ]:           6278 :     if (parse &&
 3157                          2202         [ +  + ]:           6275 :         parse->commandType == CMD_SELECT)
                               2203                 :                :     {
 6154                          2204                 :           6218 :         tlist = parse->targetList;
                               2205                 :                :         /* tlist is modifiable unless it's a dummy in a setop query */
 2068                          2206                 :           6218 :         tlist_is_modifiable = (parse->setOperations == NULL);
                               2207                 :                :     }
 6154                          2208         [ +  + ]:             60 :     else if (parse &&
                               2209         [ +  + ]:             57 :              (parse->commandType == CMD_INSERT ||
                               2210         [ +  - ]:             15 :               parse->commandType == CMD_UPDATE ||
  538 dean.a.rasheed@gmail     2211         [ +  - ]:             15 :               parse->commandType == CMD_DELETE ||
                               2212         [ +  - ]:             15 :               parse->commandType == CMD_MERGE) &&
 6154 tgl@sss.pgh.pa.us        2213         [ +  - ]:             57 :              parse->returningList)
                               2214                 :                :     {
                               2215                 :             57 :         tlist = parse->returningList;
                               2216                 :                :         /* returningList can always be modified */
 2068                          2217                 :             57 :         tlist_is_modifiable = true;
                               2218                 :                :     }
                               2219                 :                :     else
                               2220                 :                :     {
                               2221                 :                :         /* Last statement is a utility command, or it rewrote to nothing */
 2745 peter_e@gmx.net          2222         [ +  - ]:              3 :         ereport(ERROR,
                               2223                 :                :                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                               2224                 :                :                  errmsg("return type mismatch in function declared to return %s",
                               2225                 :                :                         format_type_be(rettype)),
                               2226                 :                :                  errdetail("Function's final statement must be SELECT or INSERT/UPDATE/DELETE/MERGE RETURNING.")));
                               2227                 :                :         return false;           /* keep compiler quiet */
                               2228                 :                :     }
                               2229                 :                : 
                               2230                 :                :     /*
                               2231                 :                :      * OK, check that the targetlist returns something matching the declared
                               2232                 :                :      * type, and modify it if necessary.  If possible, we insert any coercion
                               2233                 :                :      * steps right into the final statement's targetlist.  However, that might
                               2234                 :                :      * risk changes in the statement's semantics --- we can't safely change
                               2235                 :                :      * the output type of a grouping column, for instance.  In such cases we
                               2236                 :                :      * handle coercions by inserting an extra level of Query that effectively
                               2237                 :                :      * just does a projection.
                               2238                 :                :      */
                               2239                 :                : 
                               2240                 :                :     /*
                               2241                 :                :      * Count the non-junk entries in the result targetlist.
                               2242                 :                :      */
 7639 tgl@sss.pgh.pa.us        2243                 :           6275 :     tlistlen = ExecCleanTargetListLength(tlist);
                               2244                 :                : 
 7464                          2245                 :           6275 :     fn_typtype = get_typtype(rettype);
                               2246                 :                : 
 6732                          2247   [ +  +  +  + ]:           6275 :     if (fn_typtype == TYPTYPE_BASE ||
                               2248         [ +  + ]:            700 :         fn_typtype == TYPTYPE_DOMAIN ||
                               2249         [ +  + ]:            697 :         fn_typtype == TYPTYPE_ENUM ||
 1721 akorotkov@postgresql     2250         [ +  + ]:            679 :         fn_typtype == TYPTYPE_RANGE ||
                               2251                 :                :         fn_typtype == TYPTYPE_MULTIRANGE)
 7639 tgl@sss.pgh.pa.us        2252                 :           5608 :     {
                               2253                 :                :         /*
                               2254                 :                :          * For scalar-type returns, the target list must have exactly one
                               2255                 :                :          * non-junk entry, and its type must be coercible to rettype.
                               2256                 :                :          */
                               2257                 :                :         TargetEntry *tle;
                               2258                 :                : 
                               2259         [ +  + ]:           5614 :         if (tlistlen != 1)
                               2260         [ +  - ]:              3 :             ereport(ERROR,
                               2261                 :                :                     (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                               2262                 :                :                      errmsg("return type mismatch in function declared to return %s",
                               2263                 :                :                             format_type_be(rettype)),
                               2264                 :                :                      errdetail("Final statement must return exactly one column.")));
                               2265                 :                : 
                               2266                 :                :         /* We assume here that non-junk TLEs must come first in tlists */
 6381                          2267                 :           5611 :         tle = (TargetEntry *) linitial(tlist);
                               2268         [ -  + ]:           5611 :         Assert(!tle->resjunk);
                               2269                 :                : 
 2068                          2270         [ +  + ]:           5611 :         if (!coerce_fn_result_column(tle, rettype, -1,
                               2271                 :                :                                      tlist_is_modifiable,
                               2272                 :                :                                      &upper_tlist,
                               2273                 :                :                                      &upper_tlist_nontrivial))
 7639                          2274         [ +  - ]:              3 :             ereport(ERROR,
                               2275                 :                :                     (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                               2276                 :                :                      errmsg("return type mismatch in function declared to return %s",
                               2277                 :                :                             format_type_be(rettype)),
                               2278                 :                :                      errdetail("Actual return type is %s.",
                               2279                 :                :                                format_type_be(exprType((Node *) tle->expr)))));
                               2280                 :                :     }
 6732                          2281   [ +  +  +  + ]:            661 :     else if (fn_typtype == TYPTYPE_COMPOSITE || rettype == RECORDOID)
 7639                          2282                 :            592 :     {
                               2283                 :                :         /*
                               2284                 :                :          * Returns a rowtype.
                               2285                 :                :          *
                               2286                 :                :          * Note that we will not consider a domain over composite to be a
                               2287                 :                :          * "rowtype" return type; it goes through the scalar case above.  This
                               2288                 :                :          * is because we only provide column-by-column implicit casting, and
                               2289                 :                :          * will not cast the complete record result.  So the only way to
                               2290                 :                :          * produce a domain-over-composite result is to compute it as an
                               2291                 :                :          * explicit single-column result.  The single-composite-column code
                               2292                 :                :          * path just below could handle such cases, but it won't be reached.
                               2293                 :                :          */
                               2294                 :                :         int         tupnatts;   /* physical number of columns in tuple */
                               2295                 :                :         int         tuplogcols; /* # of nondeleted columns in tuple */
                               2296                 :                :         int         colindex;   /* physical column index */
                               2297                 :                : 
                               2298                 :                :         /*
                               2299                 :                :          * If the target list has one non-junk entry, and that expression has
                               2300                 :                :          * or can be coerced to the declared return type, take it as the
                               2301                 :                :          * result.  This allows, for example, 'SELECT func2()', where func2
                               2302                 :                :          * has the same composite return type as the function that's calling
                               2303                 :                :          * it.  This provision creates some ambiguity --- maybe the expression
                               2304                 :                :          * was meant to be the lone field of the composite result --- but it
                               2305                 :                :          * works well enough as long as we don't get too enthusiastic about
                               2306                 :                :          * inventing coercions from scalar to composite types.
                               2307                 :                :          *
                               2308                 :                :          * XXX Note that if rettype is RECORD and the expression is of a named
                               2309                 :                :          * composite type, or vice versa, this coercion will succeed, whether
                               2310                 :                :          * or not the record type really matches.  For the moment we rely on
                               2311                 :                :          * runtime type checking to catch any discrepancy, but it'd be nice to
                               2312                 :                :          * do better at parse time.
                               2313                 :                :          *
                               2314                 :                :          * We must *not* do this for a procedure, however.  Procedures with
                               2315                 :                :          * output parameter(s) have rettype RECORD, and the CALL code expects
                               2316                 :                :          * to get results corresponding to the list of output parameters, even
                               2317                 :                :          * when there's just one parameter that's composite.
                               2318                 :                :          */
  543                          2319   [ +  +  +  + ]:            658 :         if (tlistlen == 1 && prokind != PROKIND_PROCEDURE)
                               2320                 :                :         {
 6381                          2321                 :            108 :             TargetEntry *tle = (TargetEntry *) linitial(tlist);
                               2322                 :                : 
                               2323         [ -  + ]:            108 :             Assert(!tle->resjunk);
 2068                          2324         [ +  + ]:            108 :             if (coerce_fn_result_column(tle, rettype, -1,
                               2325                 :                :                                         tlist_is_modifiable,
                               2326                 :                :                                         &upper_tlist,
                               2327                 :                :                                         &upper_tlist_nontrivial))
                               2328                 :                :             {
                               2329                 :                :                 /* Note that we're NOT setting is_tuple_result */
                               2330                 :             36 :                 goto tlist_coercion_finished;
                               2331                 :                :             }
                               2332                 :                :         }
                               2333                 :                : 
                               2334                 :                :         /*
                               2335                 :                :          * If the caller didn't provide an expected tupdesc, we can't do any
                               2336                 :                :          * further checking.  Assume we're returning the whole tuple.
                               2337                 :                :          */
                               2338         [ +  + ]:            622 :         if (rettupdesc == NULL)
 7464                          2339                 :             24 :             return true;
                               2340                 :                : 
                               2341                 :                :         /*
                               2342                 :                :          * Verify that the targetlist matches the return tuple type.  We scan
                               2343                 :                :          * the non-resjunk columns, and coerce them if necessary to match the
                               2344                 :                :          * datatypes of the non-deleted attributes.  For deleted attributes,
                               2345                 :                :          * insert NULL result columns if the caller asked for that.
                               2346                 :                :          */
 2068                          2347                 :            598 :         tupnatts = rettupdesc->natts;
 7464                          2348                 :            598 :         tuplogcols = 0;         /* we'll count nondeleted cols as we go */
 7639                          2349                 :            598 :         colindex = 0;
                               2350                 :                : 
 6154                          2351   [ +  -  +  +  :           2306 :         foreach(lc, tlist)
                                              +  + ]
                               2352                 :                :         {
                               2353                 :           1714 :             TargetEntry *tle = (TargetEntry *) lfirst(lc);
                               2354                 :                :             Form_pg_attribute attr;
                               2355                 :                : 
                               2356                 :                :             /* resjunk columns can simply be ignored */
 7458                          2357         [ -  + ]:           1714 :             if (tle->resjunk)
 7639 tgl@sss.pgh.pa.us        2358                 :UBC           0 :                 continue;
                               2359                 :                : 
                               2360                 :                :             do
                               2361                 :                :             {
 7639 tgl@sss.pgh.pa.us        2362                 :CBC        1741 :                 colindex++;
 7464                          2363         [ -  + ]:           1741 :                 if (colindex > tupnatts)
 7639 tgl@sss.pgh.pa.us        2364         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               2365                 :                :                             (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                               2366                 :                :                              errmsg("return type mismatch in function declared to return %s",
                               2367                 :                :                                     format_type_be(rettype)),
                               2368                 :                :                              errdetail("Final statement returns too many columns.")));
 2068 tgl@sss.pgh.pa.us        2369                 :CBC        1741 :                 attr = TupleDescAttr(rettupdesc, colindex - 1);
                               2370   [ +  +  +  + ]:           1741 :                 if (attr->attisdropped && insertDroppedCols)
                               2371                 :                :                 {
                               2372                 :                :                     Expr       *null_expr;
                               2373                 :                : 
                               2374                 :                :                     /* The type of the null we insert isn't important */
 5745                          2375                 :              3 :                     null_expr = (Expr *) makeConst(INT4OID,
                               2376                 :                :                                                    -1,
                               2377                 :                :                                                    InvalidOid,
                               2378                 :                :                                                    sizeof(int32),
                               2379                 :                :                                                    (Datum) 0,
                               2380                 :                :                                                    true,    /* isnull */
                               2381                 :                :                                                    true /* byval */ );
 2068                          2382                 :              3 :                     upper_tlist = lappend(upper_tlist,
                               2383                 :              3 :                                           makeTargetEntry(null_expr,
                               2384                 :              3 :                                                           list_length(upper_tlist) + 1,
                               2385                 :                :                                                           NULL,
                               2386                 :                :                                                           false));
                               2387                 :              3 :                     upper_tlist_nontrivial = true;
                               2388                 :                :                 }
 7639                          2389         [ +  + ]:           1741 :             } while (attr->attisdropped);
 7464                          2390                 :           1714 :             tuplogcols++;
                               2391                 :                : 
 2068                          2392         [ +  + ]:           1714 :             if (!coerce_fn_result_column(tle,
                               2393                 :                :                                          attr->atttypid, attr->atttypmod,
                               2394                 :                :                                          tlist_is_modifiable,
                               2395                 :                :                                          &upper_tlist,
                               2396                 :                :                                          &upper_tlist_nontrivial))
 7639                          2397         [ +  - ]:              6 :                 ereport(ERROR,
                               2398                 :                :                         (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                               2399                 :                :                          errmsg("return type mismatch in function declared to return %s",
                               2400                 :                :                                 format_type_be(rettype)),
                               2401                 :                :                          errdetail("Final statement returns %s instead of %s at column %d.",
                               2402                 :                :                                    format_type_be(exprType((Node *) tle->expr)),
                               2403                 :                :                                    format_type_be(attr->atttypid),
                               2404                 :                :                                    tuplogcols)));
                               2405                 :                :         }
                               2406                 :                : 
                               2407                 :                :         /* remaining columns in rettupdesc had better all be dropped */
 5745                          2408         [ -  + ]:            592 :         for (colindex++; colindex <= tupnatts; colindex++)
                               2409                 :                :         {
  260 drowley@postgresql.o     2410         [ #  # ]:UBC           0 :             if (!TupleDescCompactAttr(rettupdesc, colindex - 1)->attisdropped)
 5745 tgl@sss.pgh.pa.us        2411         [ #  # ]:              0 :                 ereport(ERROR,
                               2412                 :                :                         (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                               2413                 :                :                          errmsg("return type mismatch in function declared to return %s",
                               2414                 :                :                                 format_type_be(rettype)),
                               2415                 :                :                          errdetail("Final statement returns too few columns.")));
 2068                          2416         [ #  # ]:              0 :             if (insertDroppedCols)
                               2417                 :                :             {
                               2418                 :                :                 Expr       *null_expr;
                               2419                 :                : 
                               2420                 :                :                 /* The type of the null we insert isn't important */
 5745                          2421                 :              0 :                 null_expr = (Expr *) makeConst(INT4OID,
                               2422                 :                :                                                -1,
                               2423                 :                :                                                InvalidOid,
                               2424                 :                :                                                sizeof(int32),
                               2425                 :                :                                                (Datum) 0,
                               2426                 :                :                                                true,    /* isnull */
                               2427                 :                :                                                true /* byval */ );
 2068                          2428                 :              0 :                 upper_tlist = lappend(upper_tlist,
                               2429                 :              0 :                                       makeTargetEntry(null_expr,
                               2430                 :              0 :                                                       list_length(upper_tlist) + 1,
                               2431                 :                :                                                       NULL,
                               2432                 :                :                                                       false));
                               2433                 :              0 :                 upper_tlist_nontrivial = true;
                               2434                 :                :             }
                               2435                 :                :         }
                               2436                 :                : 
                               2437                 :                :         /* Report that we are returning entire tuple result */
 2068 tgl@sss.pgh.pa.us        2438                 :CBC         592 :         is_tuple_result = true;
                               2439                 :                :     }
                               2440                 :                :     else
 7639                          2441         [ +  - ]:              3 :         ereport(ERROR,
                               2442                 :                :                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                               2443                 :                :                  errmsg("return type %s is not supported for SQL functions",
                               2444                 :                :                         format_type_be(rettype))));
                               2445                 :                : 
 2068                          2446                 :           6236 : tlist_coercion_finished:
                               2447                 :                : 
                               2448                 :                :     /*
                               2449                 :                :      * If necessary, modify the final Query by injecting an extra Query level
                               2450                 :                :      * that just performs a projection.  (It'd be dubious to do this to a
                               2451                 :                :      * non-SELECT query, but we never have to; RETURNING lists can always be
                               2452                 :                :      * modified in-place.)
                               2453                 :                :      */
                               2454         [ +  + ]:           6236 :     if (upper_tlist_nontrivial)
                               2455                 :                :     {
                               2456                 :                :         Query      *newquery;
                               2457                 :                :         List       *colnames;
                               2458                 :                :         RangeTblEntry *rte;
                               2459                 :                :         RangeTblRef *rtr;
                               2460                 :                : 
                               2461         [ -  + ]:             42 :         Assert(parse->commandType == CMD_SELECT);
                               2462                 :                : 
                               2463                 :                :         /* Most of the upper Query struct can be left as zeroes/nulls */
                               2464                 :             42 :         newquery = makeNode(Query);
                               2465                 :             42 :         newquery->commandType = CMD_SELECT;
                               2466                 :             42 :         newquery->querySource = parse->querySource;
                               2467                 :             42 :         newquery->canSetTag = true;
                               2468                 :             42 :         newquery->targetList = upper_tlist;
                               2469                 :                : 
                               2470                 :                :         /* We need a moderately realistic colnames list for the subquery RTE */
                               2471                 :             42 :         colnames = NIL;
                               2472   [ +  -  +  +  :            108 :         foreach(lc, parse->targetList)
                                              +  + ]
                               2473                 :                :         {
                               2474                 :             66 :             TargetEntry *tle = (TargetEntry *) lfirst(lc);
                               2475                 :                : 
                               2476         [ -  + ]:             66 :             if (tle->resjunk)
 2068 tgl@sss.pgh.pa.us        2477                 :UBC           0 :                 continue;
 2068 tgl@sss.pgh.pa.us        2478                 :CBC          66 :             colnames = lappend(colnames,
                               2479         [ +  - ]:             66 :                                makeString(tle->resname ? tle->resname : ""));
                               2480                 :                :         }
                               2481                 :                : 
                               2482                 :                :         /* Build a suitable RTE for the subquery */
                               2483                 :             42 :         rte = makeNode(RangeTblEntry);
                               2484                 :             42 :         rte->rtekind = RTE_SUBQUERY;
                               2485                 :             42 :         rte->subquery = parse;
                               2486                 :             42 :         rte->eref = rte->alias = makeAlias("*SELECT*", colnames);
                               2487                 :             42 :         rte->lateral = false;
                               2488                 :             42 :         rte->inh = false;
                               2489                 :             42 :         rte->inFromCl = true;
                               2490                 :             42 :         newquery->rtable = list_make1(rte);
                               2491                 :                : 
                               2492                 :             42 :         rtr = makeNode(RangeTblRef);
                               2493                 :             42 :         rtr->rtindex = 1;
                               2494                 :             42 :         newquery->jointree = makeFromExpr(list_make1(rtr), NULL);
                               2495                 :                : 
                               2496                 :                :         /*
                               2497                 :                :          * Make sure the new query is marked as having row security if the
                               2498                 :                :          * original one does.
                               2499                 :                :          */
  299 nathan@postgresql.or     2500                 :             42 :         newquery->hasRowSecurity = parse->hasRowSecurity;
                               2501                 :                : 
                               2502                 :                :         /* Replace original query in the correct element of the query list */
 2068 tgl@sss.pgh.pa.us        2503                 :             42 :         lfirst(parse_cell) = newquery;
                               2504                 :                :     }
                               2505                 :                : 
                               2506                 :           6236 :     return is_tuple_result;
                               2507                 :                : }
                               2508                 :                : 
                               2509                 :                : /*
                               2510                 :                :  * Process one function result column for check_sql_fn_retval
                               2511                 :                :  *
                               2512                 :                :  * Coerce the output value to the required type/typmod, and add a column
                               2513                 :                :  * to *upper_tlist for it.  Set *upper_tlist_nontrivial to true if we
                               2514                 :                :  * add an upper tlist item that's not just a Var.
                               2515                 :                :  *
                               2516                 :                :  * Returns true if OK, false if could not coerce to required type
                               2517                 :                :  * (in which case, no changes have been made)
                               2518                 :                :  */
                               2519                 :                : static bool
                               2520                 :           7433 : coerce_fn_result_column(TargetEntry *src_tle,
                               2521                 :                :                         Oid res_type,
                               2522                 :                :                         int32 res_typmod,
                               2523                 :                :                         bool tlist_is_modifiable,
                               2524                 :                :                         List **upper_tlist,
                               2525                 :                :                         bool *upper_tlist_nontrivial)
                               2526                 :                : {
                               2527                 :                :     TargetEntry *new_tle;
                               2528                 :                :     Expr       *new_tle_expr;
                               2529                 :                :     Node       *cast_result;
                               2530                 :                : 
                               2531                 :                :     /*
                               2532                 :                :      * If the TLE has a sortgroupref marking, don't change it, as it probably
                               2533                 :                :      * is referenced by ORDER BY, DISTINCT, etc, and changing its type would
                               2534                 :                :      * break query semantics.  Otherwise, it's safe to modify in-place unless
                               2535                 :                :      * the query as a whole has issues with that.
                               2536                 :                :      */
                               2537   [ +  +  +  + ]:           7433 :     if (tlist_is_modifiable && src_tle->ressortgroupref == 0)
                               2538                 :                :     {
                               2539                 :                :         /* OK to modify src_tle in place, if necessary */
                               2540                 :          14644 :         cast_result = coerce_to_target_type(NULL,
                               2541                 :           7322 :                                             (Node *) src_tle->expr,
                               2542                 :           7322 :                                             exprType((Node *) src_tle->expr),
                               2543                 :                :                                             res_type, res_typmod,
                               2544                 :                :                                             COERCION_ASSIGNMENT,
                               2545                 :                :                                             COERCE_IMPLICIT_CAST,
                               2546                 :                :                                             -1);
                               2547         [ +  + ]:           7322 :         if (cast_result == NULL)
                               2548                 :             72 :             return false;
 1971                          2549                 :           7250 :         assign_expr_collations(NULL, cast_result);
 2068                          2550                 :           7250 :         src_tle->expr = (Expr *) cast_result;
                               2551                 :                :         /* Make a Var referencing the possibly-modified TLE */
                               2552                 :           7250 :         new_tle_expr = (Expr *) makeVarFromTargetEntry(1, src_tle);
                               2553                 :                :     }
                               2554                 :                :     else
                               2555                 :                :     {
                               2556                 :                :         /* Any casting must happen in the upper tlist */
                               2557                 :            111 :         Var        *var = makeVarFromTargetEntry(1, src_tle);
                               2558                 :                : 
                               2559                 :            111 :         cast_result = coerce_to_target_type(NULL,
                               2560                 :                :                                             (Node *) var,
                               2561                 :                :                                             var->vartype,
                               2562                 :                :                                             res_type, res_typmod,
                               2563                 :                :                                             COERCION_ASSIGNMENT,
                               2564                 :                :                                             COERCE_IMPLICIT_CAST,
                               2565                 :                :                                             -1);
                               2566         [ +  + ]:            111 :         if (cast_result == NULL)
                               2567                 :              9 :             return false;
 1971                          2568                 :            102 :         assign_expr_collations(NULL, cast_result);
                               2569                 :                :         /* Did the coercion actually do anything? */
 2068                          2570         [ +  + ]:            102 :         if (cast_result != (Node *) var)
                               2571                 :             51 :             *upper_tlist_nontrivial = true;
                               2572                 :            102 :         new_tle_expr = (Expr *) cast_result;
                               2573                 :                :     }
                               2574                 :          14704 :     new_tle = makeTargetEntry(new_tle_expr,
                               2575                 :           7352 :                               list_length(*upper_tlist) + 1,
                               2576                 :                :                               src_tle->resname, false);
                               2577                 :           7352 :     *upper_tlist = lappend(*upper_tlist, new_tle);
                               2578                 :           7352 :     return true;
                               2579                 :                : }
                               2580                 :                : 
                               2581                 :                : /*
                               2582                 :                :  * Extract the targetlist of the last canSetTag query in the given list
                               2583                 :                :  * of parsed-and-rewritten Queries.  Returns NIL if there is none.
                               2584                 :                :  */
                               2585                 :                : static List *
  157                          2586                 :          16993 : get_sql_fn_result_tlist(List *queryTreeList)
                               2587                 :                : {
                               2588                 :          16993 :     Query      *parse = NULL;
                               2589                 :                :     ListCell   *lc;
                               2590                 :                : 
                               2591   [ +  -  +  +  :          33989 :     foreach(lc, queryTreeList)
                                              +  + ]
                               2592                 :                :     {
                               2593                 :          16996 :         Query      *q = lfirst_node(Query, lc);
                               2594                 :                : 
                               2595         [ +  + ]:          16996 :         if (q->canSetTag)
                               2596                 :          16993 :             parse = q;
                               2597                 :                :     }
                               2598         [ +  - ]:          16993 :     if (parse &&
                               2599         [ +  + ]:          16993 :         parse->commandType == CMD_SELECT)
                               2600                 :          16945 :         return parse->targetList;
                               2601         [ +  - ]:             48 :     else if (parse &&
                               2602         [ +  + ]:             48 :              (parse->commandType == CMD_INSERT ||
                               2603         [ +  - ]:             12 :               parse->commandType == CMD_UPDATE ||
                               2604         [ +  - ]:             12 :               parse->commandType == CMD_DELETE ||
                               2605         [ +  - ]:             12 :               parse->commandType == CMD_MERGE) &&
                               2606         [ +  - ]:             48 :              parse->returningList)
                               2607                 :             48 :         return parse->returningList;
                               2608                 :                :     else
  157 tgl@sss.pgh.pa.us        2609                 :UBC           0 :         return NIL;
                               2610                 :                : }
                               2611                 :                : 
                               2612                 :                : 
                               2613                 :                : /*
                               2614                 :                :  * CreateSQLFunctionDestReceiver -- create a suitable DestReceiver object
                               2615                 :                :  */
                               2616                 :                : DestReceiver *
 6154 tgl@sss.pgh.pa.us        2617                 :CBC       42011 : CreateSQLFunctionDestReceiver(void)
                               2618                 :                : {
                               2619                 :          42011 :     DR_sqlfunction *self = (DR_sqlfunction *) palloc0(sizeof(DR_sqlfunction));
                               2620                 :                : 
                               2621                 :          42011 :     self->pub.receiveSlot = sqlfunction_receive;
                               2622                 :          42011 :     self->pub.rStartup = sqlfunction_startup;
                               2623                 :          42011 :     self->pub.rShutdown = sqlfunction_shutdown;
                               2624                 :          42011 :     self->pub.rDestroy = sqlfunction_destroy;
                               2625                 :          42011 :     self->pub.mydest = DestSQLFunction;
                               2626                 :                : 
                               2627                 :                :     /* private fields will be set by postquel_start */
                               2628                 :                : 
                               2629                 :          42011 :     return (DestReceiver *) self;
                               2630                 :                : }
                               2631                 :                : 
                               2632                 :                : /*
                               2633                 :                :  * sqlfunction_startup --- executor startup
                               2634                 :                :  */
                               2635                 :                : static void
                               2636                 :          42502 : sqlfunction_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
                               2637                 :                : {
                               2638                 :                :     /* no-op */
                               2639                 :          42502 : }
                               2640                 :                : 
                               2641                 :                : /*
                               2642                 :                :  * sqlfunction_receive --- receive one tuple
                               2643                 :                :  */
                               2644                 :                : static bool
                               2645                 :          95422 : sqlfunction_receive(TupleTableSlot *slot, DestReceiver *self)
                               2646                 :                : {
                               2647                 :          95422 :     DR_sqlfunction *myState = (DR_sqlfunction *) self;
                               2648                 :                : 
  127                          2649         [ +  + ]:          95422 :     if (myState->tstore)
                               2650                 :                :     {
                               2651                 :                :         /* We are collecting all of a set result into the tuplestore */
                               2652                 :                : 
                               2653                 :                :         /* Filter tuple as needed */
                               2654                 :          61172 :         slot = ExecFilterJunk(myState->filter, slot);
                               2655                 :                : 
                               2656                 :                :         /* Store the filtered tuple into the tuplestore */
                               2657                 :          61172 :         tuplestore_puttupleslot(myState->tstore, slot);
                               2658                 :                :     }
                               2659                 :                :     else
                               2660                 :                :     {
                               2661                 :                :         /*
                               2662                 :                :          * We only want the first tuple, which we'll save in the junkfilter's
                               2663                 :                :          * result slot.  Ignore any additional tuples passed.
                               2664                 :                :          */
                               2665         [ +  + ]:          34250 :         if (TTS_EMPTY(myState->filter->jf_resultSlot))
                               2666                 :                :         {
                               2667                 :                :             /* Filter tuple as needed */
                               2668                 :          34247 :             slot = ExecFilterJunk(myState->filter, slot);
                               2669         [ -  + ]:          34247 :             Assert(slot == myState->filter->jf_resultSlot);
                               2670                 :                : 
                               2671                 :                :             /* Materialize the slot so it preserves pass-by-ref values */
                               2672                 :          34247 :             ExecMaterializeSlot(slot);
                               2673                 :                :         }
                               2674                 :                :     }
                               2675                 :                : 
 3379 rhaas@postgresql.org     2676                 :          95422 :     return true;
                               2677                 :                : }
                               2678                 :                : 
                               2679                 :                : /*
                               2680                 :                :  * sqlfunction_shutdown --- executor end
                               2681                 :                :  */
                               2682                 :                : static void
 6154 tgl@sss.pgh.pa.us        2683                 :          38530 : sqlfunction_shutdown(DestReceiver *self)
                               2684                 :                : {
                               2685                 :                :     /* no-op */
                               2686                 :          38530 : }
                               2687                 :                : 
                               2688                 :                : /*
                               2689                 :                :  * sqlfunction_destroy --- release DestReceiver object
                               2690                 :                :  */
                               2691                 :                : static void
                               2692                 :          38036 : sqlfunction_destroy(DestReceiver *self)
                               2693                 :                : {
                               2694                 :          38036 :     pfree(self);
                               2695                 :          38036 : }
        

Generated by: LCOV version 2.4-beta