LCOV - differential code coverage report
Current view: top level - src/backend/executor - functions.c (source / functions) Coverage Total Hit UNC UBC GBC GNC CBC DUB DCB
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 90.9 % 605 550 55 2 6 542 2 5
Current Date: 2024-04-14 14:21:10 Functions: 95.8 % 24 23 1 3 20 1
Baseline: 16@8cea358b128 Branches: 76.0 % 492 374 2 116 2 4 368
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed [..60] days: 100.0 % 6 6 5 1
(240..) days: 90.8 % 599 544 55 2 1 541
Function coverage date bins:
(240..) days: 95.8 % 24 23 1 3 20
Branch coverage date bins:
[..60] days: 80.0 % 10 8 2 4 4
(240..) days: 75.9 % 482 366 116 2 364

 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-2024, 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/lsyscache.h"
                                 35                 :                : #include "utils/memutils.h"
                                 36                 :                : #include "utils/snapmgr.h"
                                 37                 :                : #include "utils/syscache.h"
                                 38                 :                : 
                                 39                 :                : 
                                 40                 :                : /*
                                 41                 :                :  * Specialized DestReceiver for collecting query output in a SQL function
                                 42                 :                :  */
                                 43                 :                : typedef struct
                                 44                 :                : {
                                 45                 :                :     DestReceiver pub;           /* publicly-known function pointers */
                                 46                 :                :     Tuplestorestate *tstore;    /* where to put result tuples */
                                 47                 :                :     MemoryContext cxt;          /* context containing tstore */
                                 48                 :                :     JunkFilter *filter;         /* filter to convert tuple type */
                                 49                 :                : } DR_sqlfunction;
                                 50                 :                : 
                                 51                 :                : /*
                                 52                 :                :  * We have an execution_state record for each query in a function.  Each
                                 53                 :                :  * record contains a plantree for its query.  If the query is currently in
                                 54                 :                :  * F_EXEC_RUN state then there's a QueryDesc too.
                                 55                 :                :  *
                                 56                 :                :  * The "next" fields chain together all the execution_state records generated
                                 57                 :                :  * from a single original parsetree.  (There will only be more than one in
                                 58                 :                :  * case of rule expansion of the original parsetree.)
                                 59                 :                :  */
                                 60                 :                : typedef enum
                                 61                 :                : {
                                 62                 :                :     F_EXEC_START, F_EXEC_RUN, F_EXEC_DONE,
                                 63                 :                : } ExecStatus;
                                 64                 :                : 
                                 65                 :                : typedef struct execution_state
                                 66                 :                : {
                                 67                 :                :     struct execution_state *next;
                                 68                 :                :     ExecStatus  status;
                                 69                 :                :     bool        setsResult;     /* true if this query produces func's result */
                                 70                 :                :     bool        lazyEval;       /* true if should fetch one row at a time */
                                 71                 :                :     PlannedStmt *stmt;          /* plan for this query */
                                 72                 :                :     QueryDesc  *qd;             /* null unless status == RUN */
                                 73                 :                : } execution_state;
                                 74                 :                : 
                                 75                 :                : 
                                 76                 :                : /*
                                 77                 :                :  * An SQLFunctionCache record is built during the first call,
                                 78                 :                :  * and linked to from the fn_extra field of the FmgrInfo struct.
                                 79                 :                :  *
                                 80                 :                :  * Note that currently this has only the lifespan of the calling query.
                                 81                 :                :  * Someday we should rewrite this code to use plancache.c to save parse/plan
                                 82                 :                :  * results for longer than that.
                                 83                 :                :  *
                                 84                 :                :  * Physically, though, the data has the lifespan of the FmgrInfo that's used
                                 85                 :                :  * to call the function, and there are cases (particularly with indexes)
                                 86                 :                :  * where the FmgrInfo might survive across transactions.  We cannot assume
                                 87                 :                :  * that the parse/plan trees are good for longer than the (sub)transaction in
                                 88                 :                :  * which parsing was done, so we must mark the record with the LXID/subxid of
                                 89                 :                :  * its creation time, and regenerate everything if that's obsolete.  To avoid
                                 90                 :                :  * memory leakage when we do have to regenerate things, all the data is kept
                                 91                 :                :  * in a sub-context of the FmgrInfo's fn_mcxt.
                                 92                 :                :  */
                                 93                 :                : typedef struct
                                 94                 :                : {
                                 95                 :                :     char       *fname;          /* function name (for error msgs) */
                                 96                 :                :     char       *src;            /* function body text (for error msgs) */
                                 97                 :                : 
                                 98                 :                :     SQLFunctionParseInfoPtr pinfo;  /* data for parser callback hooks */
                                 99                 :                : 
                                100                 :                :     Oid         rettype;        /* actual return type */
                                101                 :                :     int16       typlen;         /* length of the return type */
                                102                 :                :     bool        typbyval;       /* true if return type is pass by value */
                                103                 :                :     bool        returnsSet;     /* true if returning multiple rows */
                                104                 :                :     bool        returnsTuple;   /* true if returning whole tuple result */
                                105                 :                :     bool        shutdown_reg;   /* true if registered shutdown callback */
                                106                 :                :     bool        readonly_func;  /* true to run in "read only" mode */
                                107                 :                :     bool        lazyEval;       /* true if using lazyEval for result query */
                                108                 :                : 
                                109                 :                :     ParamListInfo paramLI;      /* Param list representing current args */
                                110                 :                : 
                                111                 :                :     Tuplestorestate *tstore;    /* where we accumulate result tuples */
                                112                 :                : 
                                113                 :                :     JunkFilter *junkFilter;     /* will be NULL if function returns VOID */
                                114                 :                : 
                                115                 :                :     /*
                                116                 :                :      * func_state is a List of execution_state records, each of which is the
                                117                 :                :      * first for its original parsetree, with any additional records chained
                                118                 :                :      * to it via the "next" fields.  This sublist structure is needed to keep
                                119                 :                :      * track of where the original query boundaries are.
                                120                 :                :      */
                                121                 :                :     List       *func_state;
                                122                 :                : 
                                123                 :                :     MemoryContext fcontext;     /* memory context holding this struct and all
                                124                 :                :                                  * subsidiary data */
                                125                 :                : 
                                126                 :                :     LocalTransactionId lxid;    /* lxid in which cache was made */
                                127                 :                :     SubTransactionId subxid;    /* subxid in which cache was made */
                                128                 :                : } SQLFunctionCache;
                                129                 :                : 
                                130                 :                : typedef SQLFunctionCache *SQLFunctionCachePtr;
                                131                 :                : 
                                132                 :                : 
                                133                 :                : /* non-export function prototypes */
                                134                 :                : static Node *sql_fn_param_ref(ParseState *pstate, ParamRef *pref);
                                135                 :                : static Node *sql_fn_post_column_ref(ParseState *pstate,
                                136                 :                :                                     ColumnRef *cref, Node *var);
                                137                 :                : static Node *sql_fn_make_param(SQLFunctionParseInfoPtr pinfo,
                                138                 :                :                                int paramno, int location);
                                139                 :                : static Node *sql_fn_resolve_param_name(SQLFunctionParseInfoPtr pinfo,
                                140                 :                :                                        const char *paramname, int location);
                                141                 :                : static List *init_execution_state(List *queryTree_list,
                                142                 :                :                                   SQLFunctionCachePtr fcache,
                                143                 :                :                                   bool lazyEvalOK);
                                144                 :                : static void init_sql_fcache(FunctionCallInfo fcinfo, Oid collation, bool lazyEvalOK);
                                145                 :                : static void postquel_start(execution_state *es, SQLFunctionCachePtr fcache);
                                146                 :                : static bool postquel_getnext(execution_state *es, SQLFunctionCachePtr fcache);
                                147                 :                : static void postquel_end(execution_state *es);
                                148                 :                : static void postquel_sub_params(SQLFunctionCachePtr fcache,
                                149                 :                :                                 FunctionCallInfo fcinfo);
                                150                 :                : static Datum postquel_get_single_result(TupleTableSlot *slot,
                                151                 :                :                                         FunctionCallInfo fcinfo,
                                152                 :                :                                         SQLFunctionCachePtr fcache,
                                153                 :                :                                         MemoryContext resultcontext);
                                154                 :                : static void sql_exec_error_callback(void *arg);
                                155                 :                : static void ShutdownSQLFunction(Datum arg);
                                156                 :                : static bool coerce_fn_result_column(TargetEntry *src_tle,
                                157                 :                :                                     Oid res_type, int32 res_typmod,
                                158                 :                :                                     bool tlist_is_modifiable,
                                159                 :                :                                     List **upper_tlist,
                                160                 :                :                                     bool *upper_tlist_nontrivial);
                                161                 :                : static void sqlfunction_startup(DestReceiver *self, int operation, TupleDesc typeinfo);
                                162                 :                : static bool sqlfunction_receive(TupleTableSlot *slot, DestReceiver *self);
                                163                 :                : static void sqlfunction_shutdown(DestReceiver *self);
                                164                 :                : static void sqlfunction_destroy(DestReceiver *self);
                                165                 :                : 
                                166                 :                : 
                                167                 :                : /*
                                168                 :                :  * Prepare the SQLFunctionParseInfo struct for parsing a SQL function body
                                169                 :                :  *
                                170                 :                :  * This includes resolving actual types of polymorphic arguments.
                                171                 :                :  *
                                172                 :                :  * call_expr can be passed as NULL, but then we will fail if there are any
                                173                 :                :  * polymorphic arguments.
                                174                 :                :  */
                                175                 :                : SQLFunctionParseInfoPtr
 4770 tgl@sss.pgh.pa.us         176                 :CBC       28661 : prepare_sql_fn_parse_info(HeapTuple procedureTuple,
                                177                 :                :                           Node *call_expr,
                                178                 :                :                           Oid inputCollation)
                                179                 :                : {
                                180                 :                :     SQLFunctionParseInfoPtr pinfo;
                                181                 :          28661 :     Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
                                182                 :                :     int         nargs;
                                183                 :                : 
                                184                 :          28661 :     pinfo = (SQLFunctionParseInfoPtr) palloc0(sizeof(SQLFunctionParseInfo));
                                185                 :                : 
                                186                 :                :     /* Function's name (only) can be used to qualify argument names */
 4453                           187                 :          28661 :     pinfo->fname = pstrdup(NameStr(procedureStruct->proname));
                                188                 :                : 
                                189                 :                :     /* Save the function's input collation */
 4770                           190                 :          28661 :     pinfo->collation = inputCollation;
                                191                 :                : 
                                192                 :                :     /*
                                193                 :                :      * Copy input argument types from the pg_proc entry, then resolve any
                                194                 :                :      * polymorphic types.
                                195                 :                :      */
                                196                 :          28661 :     pinfo->nargs = nargs = procedureStruct->pronargs;
                                197         [ +  + ]:          28661 :     if (nargs > 0)
                                198                 :                :     {
                                199                 :                :         Oid        *argOidVect;
                                200                 :                :         int         argnum;
                                201                 :                : 
                                202                 :          18510 :         argOidVect = (Oid *) palloc(nargs * sizeof(Oid));
                                203                 :          18510 :         memcpy(argOidVect,
                                204                 :          18510 :                procedureStruct->proargtypes.values,
                                205                 :                :                nargs * sizeof(Oid));
                                206                 :                : 
                                207         [ +  + ]:          54241 :         for (argnum = 0; argnum < nargs; argnum++)
                                208                 :                :         {
                                209                 :          35731 :             Oid         argtype = argOidVect[argnum];
                                210                 :                : 
                                211   [ +  +  +  +  :          35731 :             if (IsPolymorphicType(argtype))
                                     +  +  +  -  +  
                                     +  +  +  +  +  
                                     +  +  +  +  +  
                                           +  +  + ]
                                212                 :                :             {
                                213                 :           1430 :                 argtype = get_call_expr_argtype(call_expr, argnum);
                                214         [ -  + ]:           1430 :                 if (argtype == InvalidOid)
 4770 tgl@sss.pgh.pa.us         215         [ #  # ]:UBC           0 :                     ereport(ERROR,
                                216                 :                :                             (errcode(ERRCODE_DATATYPE_MISMATCH),
                                217                 :                :                              errmsg("could not determine actual type of argument declared %s",
                                218                 :                :                                     format_type_be(argOidVect[argnum]))));
 4770 tgl@sss.pgh.pa.us         219                 :CBC        1430 :                 argOidVect[argnum] = argtype;
                                220                 :                :             }
                                221                 :                :         }
                                222                 :                : 
                                223                 :          18510 :         pinfo->argtypes = argOidVect;
                                224                 :                :     }
                                225                 :                : 
                                226                 :                :     /*
                                227                 :                :      * Collect names of arguments, too, if any
                                228                 :                :      */
 4453                           229         [ +  + ]:          28661 :     if (nargs > 0)
                                230                 :                :     {
                                231                 :                :         Datum       proargnames;
                                232                 :                :         Datum       proargmodes;
                                233                 :                :         int         n_arg_names;
                                234                 :                :         bool        isNull;
                                235                 :                : 
                                236                 :          18510 :         proargnames = SysCacheGetAttr(PROCNAMEARGSNSP, procedureTuple,
                                237                 :                :                                       Anum_pg_proc_proargnames,
                                238                 :                :                                       &isNull);
                                239         [ +  + ]:          18510 :         if (isNull)
 2489                           240                 :          15155 :             proargnames = PointerGetDatum(NULL);    /* just to be sure */
                                241                 :                : 
 4453                           242                 :          18510 :         proargmodes = SysCacheGetAttr(PROCNAMEARGSNSP, procedureTuple,
                                243                 :                :                                       Anum_pg_proc_proargmodes,
                                244                 :                :                                       &isNull);
                                245         [ +  + ]:          18510 :         if (isNull)
 2489                           246                 :          18080 :             proargmodes = PointerGetDatum(NULL);    /* just to be sure */
                                247                 :                : 
 1039                           248                 :          18510 :         n_arg_names = get_func_input_arg_names(proargnames, proargmodes,
                                249                 :                :                                                &pinfo->argnames);
                                250                 :                : 
                                251                 :                :         /* Paranoia: ignore the result if too few array entries */
 4453                           252         [ +  + ]:          18510 :         if (n_arg_names < nargs)
                                253                 :          15155 :             pinfo->argnames = NULL;
                                254                 :                :     }
                                255                 :                :     else
                                256                 :          10151 :         pinfo->argnames = NULL;
                                257                 :                : 
 4770                           258                 :          28661 :     return pinfo;
                                259                 :                : }
                                260                 :                : 
                                261                 :                : /*
                                262                 :                :  * Parser setup hook for parsing a SQL function body.
                                263                 :                :  */
                                264                 :                : void
                                265                 :          29724 : sql_fn_parser_setup(struct ParseState *pstate, SQLFunctionParseInfoPtr pinfo)
                                266                 :                : {
                                267                 :          29724 :     pstate->p_pre_columnref_hook = NULL;
 4453                           268                 :          29724 :     pstate->p_post_columnref_hook = sql_fn_post_column_ref;
 4770                           269                 :          29724 :     pstate->p_paramref_hook = sql_fn_param_ref;
                                270                 :                :     /* no need to use p_coerce_param_hook */
                                271                 :          29724 :     pstate->p_ref_hook_state = (void *) pinfo;
                                272                 :          29724 : }
                                273                 :                : 
                                274                 :                : /*
                                275                 :                :  * sql_fn_post_column_ref       parser callback for ColumnRefs
                                276                 :                :  */
                                277                 :                : static Node *
 4453                           278                 :           5786 : sql_fn_post_column_ref(ParseState *pstate, ColumnRef *cref, Node *var)
                                279                 :                : {
                                280                 :           5786 :     SQLFunctionParseInfoPtr pinfo = (SQLFunctionParseInfoPtr) pstate->p_ref_hook_state;
                                281                 :                :     int         nnames;
                                282                 :                :     Node       *field1;
                                283                 :           5786 :     Node       *subfield = NULL;
                                284                 :                :     const char *name1;
                                285                 :           5786 :     const char *name2 = NULL;
                                286                 :                :     Node       *param;
                                287                 :                : 
                                288                 :                :     /*
                                289                 :                :      * Never override a table-column reference.  This corresponds to
                                290                 :                :      * considering the parameter names to appear in a scope outside the
                                291                 :                :      * individual SQL commands, which is what we want.
                                292                 :                :      */
                                293         [ +  + ]:           5786 :     if (var != NULL)
                                294                 :           4425 :         return NULL;
                                295                 :                : 
                                296                 :                :     /*----------
                                297                 :                :      * The allowed syntaxes are:
                                298                 :                :      *
                                299                 :                :      * A        A = parameter name
                                300                 :                :      * A.B      A = function name, B = parameter name
                                301                 :                :      *          OR: A = record-typed parameter name, B = field name
                                302                 :                :      *          (the first possibility takes precedence)
                                303                 :                :      * A.B.C    A = function name, B = record-typed parameter name,
                                304                 :                :      *          C = field name
                                305                 :                :      * A.*      Whole-row reference to composite parameter A.
                                306                 :                :      * A.B.*    Same, with A = function name, B = parameter name
                                307                 :                :      *
                                308                 :                :      * Here, it's sufficient to ignore the "*" in the last two cases --- the
                                309                 :                :      * main parser will take care of expanding the whole-row reference.
                                310                 :                :      *----------
                                311                 :                :      */
                                312                 :           1361 :     nnames = list_length(cref->fields);
                                313                 :                : 
                                314         [ -  + ]:           1361 :     if (nnames > 3)
 4453 tgl@sss.pgh.pa.us         315                 :UBC           0 :         return NULL;
                                316                 :                : 
 3723 tgl@sss.pgh.pa.us         317         [ +  + ]:CBC        1361 :     if (IsA(llast(cref->fields), A_Star))
                                318                 :             27 :         nnames--;
                                319                 :                : 
 4453                           320                 :           1361 :     field1 = (Node *) linitial(cref->fields);
                                321                 :           1361 :     name1 = strVal(field1);
                                322         [ +  + ]:           1361 :     if (nnames > 1)
                                323                 :                :     {
                                324                 :             78 :         subfield = (Node *) lsecond(cref->fields);
                                325                 :             78 :         name2 = strVal(subfield);
                                326                 :                :     }
                                327                 :                : 
                                328         [ +  + ]:           1361 :     if (nnames == 3)
                                329                 :                :     {
                                330                 :                :         /*
                                331                 :                :          * Three-part name: if the first part doesn't match the function name,
                                332                 :                :          * we can fail immediately. Otherwise, look up the second part, and
                                333                 :                :          * take the third part to be a field reference.
                                334                 :                :          */
                                335         [ -  + ]:             12 :         if (strcmp(name1, pinfo->fname) != 0)
 4453 tgl@sss.pgh.pa.us         336                 :UBC           0 :             return NULL;
                                337                 :                : 
 4453 tgl@sss.pgh.pa.us         338                 :CBC          12 :         param = sql_fn_resolve_param_name(pinfo, name2, cref->location);
                                339                 :                : 
                                340                 :             12 :         subfield = (Node *) lthird(cref->fields);
                                341         [ -  + ]:             12 :         Assert(IsA(subfield, String));
                                342                 :                :     }
                                343   [ +  +  +  + ]:           1349 :     else if (nnames == 2 && strcmp(name1, pinfo->fname) == 0)
                                344                 :                :     {
                                345                 :                :         /*
                                346                 :                :          * Two-part name with first part matching function name: first see if
                                347                 :                :          * second part matches any parameter name.
                                348                 :                :          */
                                349                 :             12 :         param = sql_fn_resolve_param_name(pinfo, name2, cref->location);
                                350                 :                : 
                                351         [ +  - ]:             12 :         if (param)
                                352                 :                :         {
                                353                 :                :             /* Yes, so this is a parameter reference, no subfield */
                                354                 :             12 :             subfield = NULL;
                                355                 :                :         }
                                356                 :                :         else
                                357                 :                :         {
                                358                 :                :             /* No, so try to match as parameter name and subfield */
 4453 tgl@sss.pgh.pa.us         359                 :UBC           0 :             param = sql_fn_resolve_param_name(pinfo, name1, cref->location);
                                360                 :                :         }
                                361                 :                :     }
                                362                 :                :     else
                                363                 :                :     {
                                364                 :                :         /* Single name, or parameter name followed by subfield */
 4453 tgl@sss.pgh.pa.us         365                 :CBC        1337 :         param = sql_fn_resolve_param_name(pinfo, name1, cref->location);
                                366                 :                :     }
                                367                 :                : 
                                368         [ -  + ]:           1361 :     if (!param)
 4453 tgl@sss.pgh.pa.us         369                 :UBC           0 :         return NULL;            /* No match */
                                370                 :                : 
 4453 tgl@sss.pgh.pa.us         371         [ +  + ]:CBC        1361 :     if (subfield)
                                372                 :                :     {
                                373                 :                :         /*
                                374                 :                :          * Must be a reference to a field of a composite parameter; otherwise
                                375                 :                :          * ParseFuncOrColumn will return NULL, and we'll fail back at the
                                376                 :                :          * caller.
                                377                 :                :          */
                                378                 :             66 :         param = ParseFuncOrColumn(pstate,
                                379                 :             66 :                                   list_make1(subfield),
                                380                 :             66 :                                   list_make1(param),
                                381                 :                :                                   pstate->p_last_srf,
                                382                 :                :                                   NULL,
                                383                 :                :                                   false,
                                384                 :                :                                   cref->location);
                                385                 :                :     }
                                386                 :                : 
                                387                 :           1361 :     return param;
                                388                 :                : }
                                389                 :                : 
                                390                 :                : /*
                                391                 :                :  * sql_fn_param_ref     parser callback for ParamRefs ($n symbols)
                                392                 :                :  */
                                393                 :                : static Node *
 4770                           394                 :          61553 : sql_fn_param_ref(ParseState *pstate, ParamRef *pref)
                                395                 :                : {
                                396                 :          61553 :     SQLFunctionParseInfoPtr pinfo = (SQLFunctionParseInfoPtr) pstate->p_ref_hook_state;
                                397                 :          61553 :     int         paramno = pref->number;
                                398                 :                : 
                                399                 :                :     /* Check parameter number is valid */
                                400   [ +  -  +  + ]:          61553 :     if (paramno <= 0 || paramno > pinfo->nargs)
                                401                 :              3 :         return NULL;            /* unknown parameter number */
                                402                 :                : 
 4453                           403                 :          61550 :     return sql_fn_make_param(pinfo, paramno, pref->location);
                                404                 :                : }
                                405                 :                : 
                                406                 :                : /*
                                407                 :                :  * sql_fn_make_param        construct a Param node for the given paramno
                                408                 :                :  */
                                409                 :                : static Node *
                                410                 :          62911 : sql_fn_make_param(SQLFunctionParseInfoPtr pinfo,
                                411                 :                :                   int paramno, int location)
                                412                 :                : {
                                413                 :                :     Param      *param;
                                414                 :                : 
 4770                           415                 :          62911 :     param = makeNode(Param);
                                416                 :          62911 :     param->paramkind = PARAM_EXTERN;
                                417                 :          62911 :     param->paramid = paramno;
                                418                 :          62911 :     param->paramtype = pinfo->argtypes[paramno - 1];
                                419                 :          62911 :     param->paramtypmod = -1;
                                420                 :          62911 :     param->paramcollid = get_typcollation(param->paramtype);
 4453                           421                 :          62911 :     param->location = location;
                                422                 :                : 
                                423                 :                :     /*
                                424                 :                :      * If we have a function input collation, allow it to override the
                                425                 :                :      * type-derived collation for parameter symbols.  (XXX perhaps this should
                                426                 :                :      * not happen if the type collation is not default?)
                                427                 :                :      */
 4770                           428   [ +  +  +  + ]:          62911 :     if (OidIsValid(pinfo->collation) && OidIsValid(param->paramcollid))
                                429                 :           1432 :         param->paramcollid = pinfo->collation;
                                430                 :                : 
                                431                 :          62911 :     return (Node *) param;
                                432                 :                : }
                                433                 :                : 
                                434                 :                : /*
                                435                 :                :  * Search for a function parameter of the given name; if there is one,
                                436                 :                :  * construct and return a Param node for it.  If not, return NULL.
                                437                 :                :  * Helper function for sql_fn_post_column_ref.
                                438                 :                :  */
                                439                 :                : static Node *
 4453                           440                 :           1361 : sql_fn_resolve_param_name(SQLFunctionParseInfoPtr pinfo,
                                441                 :                :                           const char *paramname, int location)
                                442                 :                : {
                                443                 :                :     int         i;
                                444                 :                : 
                                445         [ -  + ]:           1361 :     if (pinfo->argnames == NULL)
 4453 tgl@sss.pgh.pa.us         446                 :UBC           0 :         return NULL;
                                447                 :                : 
 4453 tgl@sss.pgh.pa.us         448         [ +  - ]:CBC        1942 :     for (i = 0; i < pinfo->nargs; i++)
                                449                 :                :     {
                                450   [ +  -  +  + ]:           1942 :         if (pinfo->argnames[i] && strcmp(pinfo->argnames[i], paramname) == 0)
                                451                 :           1361 :             return sql_fn_make_param(pinfo, i + 1, location);
                                452                 :                :     }
                                453                 :                : 
 4453 tgl@sss.pgh.pa.us         454                 :UBC           0 :     return NULL;
                                455                 :                : }
                                456                 :                : 
                                457                 :                : /*
                                458                 :                :  * Set up the per-query execution_state records for a SQL function.
                                459                 :                :  *
                                460                 :                :  * The input is a List of Lists of parsed and rewritten, but not planned,
                                461                 :                :  * querytrees.  The sublist structure denotes the original query boundaries.
                                462                 :                :  */
                                463                 :                : static List *
 5644 tgl@sss.pgh.pa.us         464                 :CBC       18019 : init_execution_state(List *queryTree_list,
                                465                 :                :                      SQLFunctionCachePtr fcache,
                                466                 :                :                      bool lazyEvalOK)
                                467                 :                : {
 4794                           468                 :          18019 :     List       *eslist = NIL;
 5644                           469                 :          18019 :     execution_state *lasttages = NULL;
                                470                 :                :     ListCell   *lc1;
                                471                 :                : 
 4794                           472   [ +  +  +  +  :          36096 :     foreach(lc1, queryTree_list)
                                              +  + ]
                                473                 :                :     {
 2561                           474                 :          18080 :         List       *qtlist = lfirst_node(List, lc1);
 4794                           475                 :          18080 :         execution_state *firstes = NULL;
                                476                 :          18080 :         execution_state *preves = NULL;
                                477                 :                :         ListCell   *lc2;
                                478                 :                : 
                                479   [ +  -  +  +  :          36160 :         foreach(lc2, qtlist)
                                              +  + ]
                                480                 :                :         {
 2561                           481                 :          18083 :             Query      *queryTree = lfirst_node(Query, lc2);
                                482                 :                :             PlannedStmt *stmt;
                                483                 :                :             execution_state *newes;
                                484                 :                : 
                                485                 :                :             /* Plan the query if needed */
 4794                           486         [ +  + ]:          18083 :             if (queryTree->commandType == CMD_UTILITY)
                                487                 :                :             {
                                488                 :                :                 /* Utility commands require no planning. */
 2647                           489                 :             78 :                 stmt = makeNode(PlannedStmt);
                                490                 :             78 :                 stmt->commandType = CMD_UTILITY;
                                491                 :             78 :                 stmt->canSetTag = queryTree->canSetTag;
                                492                 :             78 :                 stmt->utilityStmt = queryTree->utilityStmt;
                                493                 :             78 :                 stmt->stmt_location = queryTree->stmt_location;
                                494                 :             78 :                 stmt->stmt_len = queryTree->stmt_len;
                                495                 :                :             }
                                496                 :                :             else
                                497                 :          18005 :                 stmt = pg_plan_query(queryTree,
 1476 fujii@postgresql.org      498                 :          18005 :                                      fcache->src,
                                499                 :                :                                      CURSOR_OPT_PARALLEL_OK,
                                500                 :                :                                      NULL);
                                501                 :                : 
                                502                 :                :             /*
                                503                 :                :              * Precheck all commands for validity in a function.  This should
                                504                 :                :              * generally match the restrictions spi.c applies.
                                505                 :                :              */
 2647 tgl@sss.pgh.pa.us         506         [ +  + ]:          18080 :             if (stmt->commandType == CMD_UTILITY)
                                507                 :                :             {
                                508         [ -  + ]:             78 :                 if (IsA(stmt->utilityStmt, CopyStmt) &&
 2647 tgl@sss.pgh.pa.us         509         [ #  # ]:UBC           0 :                     ((CopyStmt *) stmt->utilityStmt)->filename == NULL)
                                510         [ #  # ]:              0 :                     ereport(ERROR,
                                511                 :                :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                512                 :                :                              errmsg("cannot COPY to/from client in an SQL function")));
                                513                 :                : 
 2647 tgl@sss.pgh.pa.us         514         [ -  + ]:CBC          78 :                 if (IsA(stmt->utilityStmt, TransactionStmt))
 2647 tgl@sss.pgh.pa.us         515         [ #  # ]:UBC           0 :                     ereport(ERROR,
                                516                 :                :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                517                 :                :                     /* translator: %s is a SQL statement name */
                                518                 :                :                              errmsg("%s is not allowed in an SQL function",
                                519                 :                :                                     CreateCommandName(stmt->utilityStmt))));
                                520                 :                :             }
                                521                 :                : 
 4794 tgl@sss.pgh.pa.us         522   [ +  +  -  + ]:CBC       18080 :             if (fcache->readonly_func && !CommandIsReadOnly(stmt))
 4794 tgl@sss.pgh.pa.us         523         [ #  # ]:UBC           0 :                 ereport(ERROR,
                                524                 :                :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                525                 :                :                 /* translator: %s is a SQL statement name */
                                526                 :                :                          errmsg("%s is not allowed in a non-volatile function",
                                527                 :                :                                 CreateCommandName((Node *) stmt))));
                                528                 :                : 
                                529                 :                :             /* OK, build the execution_state for this query */
 4794 tgl@sss.pgh.pa.us         530                 :CBC       18080 :             newes = (execution_state *) palloc(sizeof(execution_state));
                                531         [ +  + ]:          18080 :             if (preves)
                                532                 :              3 :                 preves->next = newes;
                                533                 :                :             else
                                534                 :          18077 :                 firstes = newes;
                                535                 :                : 
                                536                 :          18080 :             newes->next = NULL;
                                537                 :          18080 :             newes->status = F_EXEC_START;
 4753 bruce@momjian.us          538                 :          18080 :             newes->setsResult = false;   /* might change below */
                                539                 :          18080 :             newes->lazyEval = false; /* might change below */
 4794 tgl@sss.pgh.pa.us         540                 :          18080 :             newes->stmt = stmt;
                                541                 :          18080 :             newes->qd = NULL;
                                542                 :                : 
                                543         [ +  + ]:          18080 :             if (queryTree->canSetTag)
                                544                 :          18077 :                 lasttages = newes;
                                545                 :                : 
                                546                 :          18080 :             preves = newes;
                                547                 :                :         }
                                548                 :                : 
                                549                 :          18077 :         eslist = lappend(eslist, firstes);
                                550                 :                :     }
                                551                 :                : 
                                552                 :                :     /*
                                553                 :                :      * Mark the last canSetTag query as delivering the function result; then,
                                554                 :                :      * if it is a plain SELECT, mark it for lazy evaluation. If it's not a
                                555                 :                :      * SELECT we must always run it to completion.
                                556                 :                :      *
                                557                 :                :      * Note: at some point we might add additional criteria for whether to use
                                558                 :                :      * lazy eval.  However, we should prefer to use it whenever the function
                                559                 :                :      * doesn't return set, since fetching more than one row is useless in that
                                560                 :                :      * case.
                                561                 :                :      *
                                562                 :                :      * Note: don't set setsResult if the function returns VOID, as evidenced
                                563                 :                :      * by not having made a junkfilter.  This ensures we'll throw away any
                                564                 :                :      * output from the last statement in such a function.
                                565                 :                :      */
 5644                           566   [ +  +  +  + ]:          18016 :     if (lasttages && fcache->junkFilter)
                                567                 :                :     {
                                568                 :          17874 :         lasttages->setsResult = true;
                                569         [ +  + ]:          17874 :         if (lazyEvalOK &&
 2647                           570         [ +  + ]:          17550 :             lasttages->stmt->commandType == CMD_SELECT &&
                                571         [ +  - ]:          17517 :             !lasttages->stmt->hasModifyingCTE)
                                572                 :          17517 :             fcache->lazyEval = lasttages->lazyEval = true;
                                573                 :                :     }
                                574                 :                : 
 4794                           575                 :          18016 :     return eslist;
                                576                 :                : }
                                577                 :                : 
                                578                 :                : /*
                                579                 :                :  * Initialize the SQLFunctionCache for a SQL function
                                580                 :                :  */
                                581                 :                : static void
 1558                           582                 :          18023 : init_sql_fcache(FunctionCallInfo fcinfo, Oid collation, bool lazyEvalOK)
                                583                 :                : {
                                584                 :          18023 :     FmgrInfo   *finfo = fcinfo->flinfo;
 8634                           585                 :          18023 :     Oid         foid = finfo->fn_oid;
                                586                 :                :     MemoryContext fcontext;
                                587                 :                :     MemoryContext oldcontext;
                                588                 :                :     Oid         rettype;
                                589                 :                :     TupleDesc   rettupdesc;
                                590                 :                :     HeapTuple   procedureTuple;
                                591                 :                :     Form_pg_proc procedureStruct;
                                592                 :                :     SQLFunctionCachePtr fcache;
                                593                 :                :     List       *queryTree_list;
                                594                 :                :     List       *resulttlist;
                                595                 :                :     ListCell   *lc;
                                596                 :                :     Datum       tmp;
                                597                 :                :     bool        isNull;
                                598                 :                : 
                                599                 :                :     /*
                                600                 :                :      * Create memory context that holds all the SQLFunctionCache data.  It
                                601                 :                :      * must be a child of whatever context holds the FmgrInfo.
                                602                 :                :      */
 4060                           603                 :          18023 :     fcontext = AllocSetContextCreate(finfo->fn_mcxt,
                                604                 :                :                                      "SQL function",
                                605                 :                :                                      ALLOCSET_DEFAULT_SIZES);
                                606                 :                : 
                                607                 :          18023 :     oldcontext = MemoryContextSwitchTo(fcontext);
                                608                 :                : 
                                609                 :                :     /*
                                610                 :                :      * Create the struct proper, link it to fcontext and fn_extra.  Once this
                                611                 :                :      * is done, we'll be able to recover the memory after failure, even if the
                                612                 :                :      * FmgrInfo is long-lived.
                                613                 :                :      */
 7593                           614                 :          18023 :     fcache = (SQLFunctionCachePtr) palloc0(sizeof(SQLFunctionCache));
 4060                           615                 :          18023 :     fcache->fcontext = fcontext;
 5140                           616                 :          18023 :     finfo->fn_extra = (void *) fcache;
                                617                 :                : 
                                618                 :                :     /*
                                619                 :                :      * get the procedure tuple corresponding to the given function Oid
                                620                 :                :      */
 5173 rhaas@postgresql.org      621                 :          18023 :     procedureTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(foid));
 8634 tgl@sss.pgh.pa.us         622         [ -  + ]:          18023 :     if (!HeapTupleIsValid(procedureTuple))
 7573 tgl@sss.pgh.pa.us         623         [ #  # ]:UBC           0 :         elog(ERROR, "cache lookup failed for function %u", foid);
 8634 tgl@sss.pgh.pa.us         624                 :CBC       18023 :     procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
                                625                 :                : 
                                626                 :                :     /*
                                627                 :                :      * copy function name immediately for use by error reporting callback, and
                                628                 :                :      * for use as memory context identifier
                                629                 :                :      */
 5140                           630                 :          18023 :     fcache->fname = pstrdup(NameStr(procedureStruct->proname));
 2210                           631                 :          18023 :     MemoryContextSetIdentifier(fcontext, fcache->fname);
                                632                 :                : 
                                633                 :                :     /*
                                634                 :                :      * Resolve any polymorphism, obtaining the actual result type, and the
                                635                 :                :      * corresponding tupdesc if it's a rowtype.
                                636                 :                :      */
 1558                           637                 :          18023 :     (void) get_call_result_type(fcinfo, &rettype, &rettupdesc);
                                638                 :                : 
 7213                           639                 :          18023 :     fcache->rettype = rettype;
                                640                 :                : 
                                641                 :                :     /* Fetch the typlen and byval info for the result type */
 2235 peter_e@gmx.net           642                 :          18023 :     get_typlenbyval(rettype, &fcache->typlen, &fcache->typbyval);
                                643                 :                : 
                                644                 :                :     /* Remember whether we're returning setof something */
 5644 tgl@sss.pgh.pa.us         645                 :          18023 :     fcache->returnsSet = procedureStruct->proretset;
                                646                 :                : 
                                647                 :                :     /* Remember if function is STABLE/IMMUTABLE */
 7153                           648                 :          18023 :     fcache->readonly_func =
                                649                 :          18023 :         (procedureStruct->provolatile != PROVOLATILE_VOLATILE);
                                650                 :                : 
                                651                 :                :     /*
                                652                 :                :      * We need the actual argument types to pass to the parser.  Also make
                                653                 :                :      * sure that parameter symbols are considered to have the function's
                                654                 :                :      * resolved input collation.
                                655                 :                :      */
 4770                           656                 :          36046 :     fcache->pinfo = prepare_sql_fn_parse_info(procedureTuple,
                                657                 :          18023 :                                               finfo->fn_expr,
                                658                 :                :                                               collation);
                                659                 :                : 
                                660                 :                :     /*
                                661                 :                :      * And of course we need the function body text.
                                662                 :                :      */
  386 dgustafsson@postgres      663                 :          18023 :     tmp = SysCacheGetAttrNotNull(PROCOID, procedureTuple, Anum_pg_proc_prosrc);
 1095 tgl@sss.pgh.pa.us         664                 :          18023 :     fcache->src = TextDatumGetCString(tmp);
                                665                 :                : 
                                666                 :                :     /* If we have prosqlbody, pay attention to that not prosrc. */
                                667                 :          18023 :     tmp = SysCacheGetAttr(PROCOID,
                                668                 :                :                           procedureTuple,
                                669                 :                :                           Anum_pg_proc_prosqlbody,
                                670                 :                :                           &isNull);
                                671                 :                : 
                                672                 :                :     /*
                                673                 :                :      * Parse and rewrite the queries in the function text.  Use sublists to
                                674                 :                :      * keep track of the original query boundaries.
                                675                 :                :      *
                                676                 :                :      * Note: since parsing and planning is done in fcontext, we will generate
                                677                 :                :      * a lot of cruft that lives as long as the fcache does.  This is annoying
                                678                 :                :      * but we'll not worry about it until the module is rewritten to use
                                679                 :                :      * plancache.c.
                                680                 :                :      */
 4794                           681                 :          18023 :     queryTree_list = NIL;
 1095                           682         [ +  + ]:          18023 :     if (!isNull)
                                683                 :                :     {
                                684                 :                :         Node       *n;
                                685                 :                :         List       *stored_query_list;
                                686                 :                : 
 1103 peter@eisentraut.org      687                 :           1170 :         n = stringToNode(TextDatumGetCString(tmp));
                                688         [ +  + ]:           1170 :         if (IsA(n, List))
                                689                 :            964 :             stored_query_list = linitial_node(List, castNode(List, n));
                                690                 :                :         else
                                691                 :            206 :             stored_query_list = list_make1(n);
                                692                 :                : 
                                693   [ +  +  +  +  :           2340 :         foreach(lc, stored_query_list)
                                              +  + ]
                                694                 :                :         {
                                695                 :           1170 :             Query      *parsetree = lfirst_node(Query, lc);
                                696                 :                :             List       *queryTree_sublist;
                                697                 :                : 
                                698                 :           1170 :             AcquireRewriteLocks(parsetree, true, false);
                                699                 :           1170 :             queryTree_sublist = pg_rewrite_query(parsetree);
                                700                 :           1170 :             queryTree_list = lappend(queryTree_list, queryTree_sublist);
                                701                 :                :         }
                                702                 :                :     }
                                703                 :                :     else
                                704                 :                :     {
                                705                 :                :         List       *raw_parsetree_list;
                                706                 :                : 
                                707                 :          16853 :         raw_parsetree_list = pg_parse_query(fcache->src);
                                708                 :                : 
                                709   [ +  -  +  +  :          33766 :         foreach(lc, raw_parsetree_list)
                                              +  + ]
                                710                 :                :         {
                                711                 :          16914 :             RawStmt    *parsetree = lfirst_node(RawStmt, lc);
                                712                 :                :             List       *queryTree_sublist;
                                713                 :                : 
  772                           714                 :          16914 :             queryTree_sublist = pg_analyze_and_rewrite_withcb(parsetree,
 1103                           715                 :          16914 :                                                               fcache->src,
                                716                 :                :                                                               (ParserSetupHook) sql_fn_parser_setup,
                                717                 :          16914 :                                                               fcache->pinfo,
                                718                 :                :                                                               NULL);
                                719                 :          16913 :             queryTree_list = lappend(queryTree_list, queryTree_sublist);
                                720                 :                :         }
                                721                 :                :     }
                                722                 :                : 
                                723                 :                :     /*
                                724                 :                :      * Check that there are no statements we don't want to allow.
                                725                 :                :      */
 1273 tgl@sss.pgh.pa.us         726                 :          18022 :     check_sql_fn_statements(queryTree_list);
                                727                 :                : 
                                728                 :                :     /*
                                729                 :                :      * Check that the function returns the type it claims to.  Although in
                                730                 :                :      * simple cases this was already done when the function was defined, we
                                731                 :                :      * have to recheck because database objects used in the function's queries
                                732                 :                :      * might have changed type.  We'd have to recheck anyway if the function
                                733                 :                :      * had any polymorphic arguments.  Moreover, check_sql_fn_retval takes
                                734                 :                :      * care of injecting any required column type coercions.  (But we don't
                                735                 :                :      * ask it to insert nulls for dropped columns; the junkfilter handles
                                736                 :                :      * that.)
                                737                 :                :      *
                                738                 :                :      * Note: we set fcache->returnsTuple according to whether we are returning
                                739                 :                :      * the whole tuple result or just a single column.  In the latter case we
                                740                 :                :      * clear returnsTuple because we need not act different from the scalar
                                741                 :                :      * result case, even if it's a rowtype column.  (However, we have to force
                                742                 :                :      * lazy eval mode in that case; otherwise we'd need extra code to expand
                                743                 :                :      * the rowtype column into multiple columns, since we have no way to
                                744                 :                :      * notify the caller that it should do that.)
                                745                 :                :      */
 1273 tgl@sss.pgh.pa.us         746                 :GNC       36041 :     fcache->returnsTuple = check_sql_fn_retval(queryTree_list,
                                747                 :                :                                                rettype,
                                748                 :                :                                                rettupdesc,
   33                           749                 :          18022 :                                                procedureStruct->prokind,
                                750                 :                :                                                false,
                                751                 :                :                                                &resulttlist);
                                752                 :                : 
                                753                 :                :     /*
                                754                 :                :      * Construct a JunkFilter we can use to coerce the returned rowtype to the
                                755                 :                :      * desired form, unless the result type is VOID, in which case there's
                                756                 :                :      * nothing to coerce to.  (XXX Frequently, the JunkFilter isn't doing
                                757                 :                :      * anything very interesting, but much of this module expects it to be
                                758                 :                :      * there anyway.)
                                759                 :                :      */
 1558 tgl@sss.pgh.pa.us         760         [ +  + ]:CBC       18019 :     if (rettype != VOIDOID)
                                761                 :                :     {
                                762                 :          17877 :         TupleTableSlot *slot = MakeSingleTupleTableSlot(NULL,
                                763                 :                :                                                         &TTSOpsMinimalTuple);
                                764                 :                : 
                                765                 :                :         /*
                                766                 :                :          * If the result is composite, *and* we are returning the whole tuple
                                767                 :                :          * result, we need to insert nulls for any dropped columns.  In the
                                768                 :                :          * single-column-result case, there might be dropped columns within
                                769                 :                :          * the composite column value, but it's not our problem here.  There
                                770                 :                :          * should be no resjunk entries in resulttlist, so in the second case
                                771                 :                :          * the JunkFilter is certainly a no-op.
                                772                 :                :          */
                                773   [ +  +  +  + ]:          17877 :         if (rettupdesc && fcache->returnsTuple)
                                774                 :            618 :             fcache->junkFilter = ExecInitJunkFilterConversion(resulttlist,
                                775                 :                :                                                               rettupdesc,
                                776                 :                :                                                               slot);
                                777                 :                :         else
                                778                 :          17259 :             fcache->junkFilter = ExecInitJunkFilter(resulttlist, slot);
                                779                 :                :     }
                                780                 :                : 
 5644                           781         [ +  + ]:          18019 :     if (fcache->returnsTuple)
                                782                 :                :     {
                                783                 :                :         /* Make sure output rowtype is properly blessed */
                                784                 :            630 :         BlessTupleDesc(fcache->junkFilter->jf_resultSlot->tts_tupleDescriptor);
                                785                 :                :     }
                                786   [ +  +  -  + ]:          17389 :     else if (fcache->returnsSet && type_is_rowtype(fcache->rettype))
                                787                 :                :     {
                                788                 :                :         /*
                                789                 :                :          * Returning rowtype as if it were scalar --- materialize won't work.
                                790                 :                :          * Right now it's sufficient to override any caller preference for
                                791                 :                :          * materialize mode, but to add more smarts in init_execution_state
                                792                 :                :          * about this, we'd probably need a three-way flag instead of bool.
                                793                 :                :          */
 5644 tgl@sss.pgh.pa.us         794                 :UBC           0 :         lazyEvalOK = true;
                                795                 :                :     }
                                796                 :                : 
                                797                 :                :     /* Finally, plan the queries */
 7153 tgl@sss.pgh.pa.us         798                 :CBC       18019 :     fcache->func_state = init_execution_state(queryTree_list,
                                799                 :                :                                               fcache,
                                800                 :                :                                               lazyEvalOK);
                                801                 :                : 
                                802                 :                :     /* Mark fcache with time of creation to show it's valid */
   42 heikki.linnakangas@i      803                 :GNC       18016 :     fcache->lxid = MyProc->vxid.lxid;
 4060 tgl@sss.pgh.pa.us         804                 :CBC       18016 :     fcache->subxid = GetCurrentSubTransactionId();
                                805                 :                : 
 8550                           806                 :          18016 :     ReleaseSysCache(procedureTuple);
                                807                 :                : 
 4060                           808                 :          18016 :     MemoryContextSwitchTo(oldcontext);
 8634                           809                 :          18016 : }
                                810                 :                : 
                                811                 :                : /* Start up execution of one execution_state node */
                                812                 :                : static void
 7801                           813                 :          82690 : postquel_start(execution_state *es, SQLFunctionCachePtr fcache)
                                814                 :                : {
                                815                 :                :     DestReceiver *dest;
                                816                 :                : 
                                817         [ -  + ]:          82690 :     Assert(es->qd == NULL);
                                818                 :                : 
                                819                 :                :     /* Caller should have ensured a suitable snapshot is active */
 4794                           820         [ -  + ]:          82690 :     Assert(ActiveSnapshotSet());
                                821                 :                : 
                                822                 :                :     /*
                                823                 :                :      * If this query produces the function result, send its output to the
                                824                 :                :      * tuplestore; else discard any output.
                                825                 :                :      */
 5644                           826         [ +  + ]:          82690 :     if (es->setsResult)
                                827                 :                :     {
                                828                 :                :         DR_sqlfunction *myState;
                                829                 :                : 
 5614                           830                 :          62489 :         dest = CreateDestReceiver(DestSQLFunction);
                                831                 :                :         /* pass down the needed info to the dest receiver routines */
 5644                           832                 :          62489 :         myState = (DR_sqlfunction *) dest;
                                833         [ -  + ]:          62489 :         Assert(myState->pub.mydest == DestSQLFunction);
                                834                 :          62489 :         myState->tstore = fcache->tstore;
                                835                 :          62489 :         myState->cxt = CurrentMemoryContext;
                                836                 :          62489 :         myState->filter = fcache->junkFilter;
                                837                 :                :     }
                                838                 :                :     else
                                839                 :          20201 :         dest = None_Receiver;
                                840                 :                : 
 2647 tgl@sss.pgh.pa.us         841                 :UBC           0 :     es->qd = CreateQueryDesc(es->stmt,
 2647 tgl@sss.pgh.pa.us         842                 :CBC       82690 :                              fcache->src,
                                843                 :                :                              GetActiveSnapshot(),
                                844                 :                :                              InvalidSnapshot,
                                845                 :                :                              dest,
                                846                 :                :                              fcache->paramLI,
 2571 kgrittn@postgresql.o      847         [ -  + ]:          82690 :                              es->qd ? es->qd->queryEnv : NULL,
                                848                 :                :                              0);
                                849                 :                : 
                                850                 :                :     /* Utility commands don't need Executor. */
 2647 tgl@sss.pgh.pa.us         851         [ +  + ]:          82690 :     if (es->qd->operation != CMD_UTILITY)
                                852                 :                :     {
                                853                 :                :         /*
                                854                 :                :          * In lazyEval mode, do not let the executor set up an AfterTrigger
                                855                 :                :          * context.  This is necessary not just an optimization, because we
                                856                 :                :          * mustn't exit from the function execution with a stacked
                                857                 :                :          * AfterTrigger level still active.  We are careful not to select
                                858                 :                :          * lazyEval mode for any statement that could possibly queue triggers.
                                859                 :                :          */
                                860                 :                :         int         eflags;
                                861                 :                : 
 4795                           862         [ +  + ]:          82612 :         if (es->lazyEval)
                                863                 :          62033 :             eflags = EXEC_FLAG_SKIP_TRIGGERS;
                                864                 :                :         else
                                865                 :          20579 :             eflags = 0;         /* default run-to-completion flags */
                                866                 :          82612 :         ExecutorStart(es->qd, eflags);
                                867                 :                :     }
                                868                 :                : 
 7801                           869                 :          82690 :     es->status = F_EXEC_RUN;
10141 scrappy@hub.org           870                 :          82690 : }
                                871                 :                : 
                                872                 :                : /* Run one execution_state; either to completion or to first result row */
                                873                 :                : /* Returns true if we ran to completion */
                                874                 :                : static bool
 6242 tgl@sss.pgh.pa.us         875                 :          83170 : postquel_getnext(execution_state *es, SQLFunctionCachePtr fcache)
                                876                 :                : {
                                877                 :                :     bool        result;
                                878                 :                : 
 2647                           879         [ +  + ]:          83170 :     if (es->qd->operation == CMD_UTILITY)
                                880                 :                :     {
                                881                 :             78 :         ProcessUtility(es->qd->plannedstmt,
 5816 alvherre@alvh.no-ip.      882                 :             78 :                        fcache->src,
                                883                 :                :                        true,    /* protect function cache's parsetree */
                                884                 :                :                        PROCESS_UTILITY_QUERY,
                                885                 :             78 :                        es->qd->params,
 2571 kgrittn@postgresql.o      886                 :             78 :                        es->qd->queryEnv,
 5816 alvherre@alvh.no-ip.      887                 :             78 :                        es->qd->dest,
                                888                 :                :                        NULL);
 5644 tgl@sss.pgh.pa.us         889                 :             33 :         result = true;          /* never stops early */
                                890                 :                :     }
                                891                 :                :     else
                                892                 :                :     {
                                893                 :                :         /* Run regular commands to completion unless lazyEval */
 2955                           894                 :          83092 :         uint64      count = (es->lazyEval) ? 1 : 0;
                                895                 :                : 
 2579 rhaas@postgresql.org      896   [ +  +  +  + ]:          83092 :         ExecutorRun(es->qd, ForwardScanDirection, count, !fcache->returnsSet || !es->lazyEval);
                                897                 :                : 
                                898                 :                :         /*
                                899                 :                :          * If we requested run to completion OR there was no tuple returned,
                                900                 :                :          * command must be complete.
                                901                 :                :          */
 2955 tgl@sss.pgh.pa.us         902   [ +  +  +  + ]:          78809 :         result = (count == 0 || es->qd->estate->es_processed == 0);
                                903                 :                :     }
                                904                 :                : 
 7153                           905                 :          78842 :     return result;
                                906                 :                : }
                                907                 :                : 
                                908                 :                : /* Shut down execution of one execution_state node */
                                909                 :                : static void
 6944                           910                 :          78362 : postquel_end(execution_state *es)
                                911                 :                : {
                                912                 :                :     /* mark status done to ensure we don't do ExecutorEnd twice */
 7566                           913                 :          78362 :     es->status = F_EXEC_DONE;
                                914                 :                : 
                                915                 :                :     /* Utility commands don't need Executor. */
 2647                           916         [ +  + ]:          78362 :     if (es->qd->operation != CMD_UTILITY)
                                917                 :                :     {
 4795                           918                 :          78329 :         ExecutorFinish(es->qd);
 5816 alvherre@alvh.no-ip.      919                 :          78320 :         ExecutorEnd(es->qd);
                                920                 :                :     }
                                921                 :                : 
 2411 peter_e@gmx.net           922                 :          78353 :     es->qd->dest->rDestroy(es->qd->dest);
                                923                 :                : 
 7791 tgl@sss.pgh.pa.us         924                 :          78353 :     FreeQueryDesc(es->qd);
 7801                           925                 :          78353 :     es->qd = NULL;
10141 scrappy@hub.org           926                 :          78353 : }
                                927                 :                : 
                                928                 :                : /* Build ParamListInfo array representing current arguments */
                                929                 :                : static void
 7801 tgl@sss.pgh.pa.us         930                 :          62643 : postquel_sub_params(SQLFunctionCachePtr fcache,
                                931                 :                :                     FunctionCallInfo fcinfo)
                                932                 :                : {
                                933                 :          62643 :     int         nargs = fcinfo->nargs;
                                934                 :                : 
                                935         [ +  + ]:          62643 :     if (nargs > 0)
                                936                 :                :     {
                                937                 :                :         ParamListInfo paramLI;
  613                           938                 :          57107 :         Oid        *argtypes = fcache->pinfo->argtypes;
                                939                 :                : 
 5644                           940         [ +  + ]:          57107 :         if (fcache->paramLI == NULL)
                                941                 :                :         {
 1858 peter@eisentraut.org      942                 :          12495 :             paramLI = makeParamList(nargs);
 5644 tgl@sss.pgh.pa.us         943                 :          12495 :             fcache->paramLI = paramLI;
                                944                 :                :         }
                                945                 :                :         else
                                946                 :                :         {
                                947                 :          44612 :             paramLI = fcache->paramLI;
                                948         [ -  + ]:          44612 :             Assert(paramLI->numParams == nargs);
                                949                 :                :         }
                                950                 :                : 
 1858 peter@eisentraut.org      951         [ +  + ]:         169332 :         for (int i = 0; i < nargs; i++)
                                952                 :                :         {
 6567 tgl@sss.pgh.pa.us         953                 :         112225 :             ParamExternData *prm = &paramLI->params[i];
                                954                 :                : 
                                955                 :                :             /*
                                956                 :                :              * If an incoming parameter value is a R/W expanded datum, we
                                957                 :                :              * force it to R/O.  We'd be perfectly entitled to scribble on it,
                                958                 :                :              * but the problem is that if the parameter is referenced more
                                959                 :                :              * than once in the function, earlier references might mutate the
                                960                 :                :              * value seen by later references, which won't do at all.  We
                                961                 :                :              * could do better if we could be sure of the number of Param
                                962                 :                :              * nodes in the function's plans; but we might not have planned
                                963                 :                :              * all the statements yet, nor do we have plan tree walker
                                964                 :                :              * infrastructure.  (Examining the parse trees is not good enough,
                                965                 :                :              * because of possible function inlining during planning.)
                                966                 :                :              */
 1905 andres@anarazel.de        967                 :         112225 :             prm->isnull = fcinfo->args[i].isnull;
  613 tgl@sss.pgh.pa.us         968   [ +  +  +  + ]:         112225 :             prm->value = MakeExpandedObjectReadOnly(fcinfo->args[i].value,
                                969                 :                :                                                     prm->isnull,
                                970                 :                :                                                     get_typlen(argtypes[i]));
 6430                           971                 :         112225 :             prm->pflags = 0;
  613                           972                 :         112225 :             prm->ptype = argtypes[i];
                                973                 :                :         }
                                974                 :                :     }
                                975                 :                :     else
 5644                           976                 :           5536 :         fcache->paramLI = NULL;
10141 scrappy@hub.org           977                 :          62643 : }
                                978                 :                : 
                                979                 :                : /*
                                980                 :                :  * Extract the SQL function's value from a single result row.  This is used
                                981                 :                :  * both for scalar (non-set) functions and for each row of a lazy-eval set
                                982                 :                :  * result.
                                983                 :                :  */
                                984                 :                : static Datum
 5644 tgl@sss.pgh.pa.us         985                 :          54738 : postquel_get_single_result(TupleTableSlot *slot,
                                986                 :                :                            FunctionCallInfo fcinfo,
                                987                 :                :                            SQLFunctionCachePtr fcache,
                                988                 :                :                            MemoryContext resultcontext)
                                989                 :                : {
                                990                 :                :     Datum       value;
                                991                 :                :     MemoryContext oldcontext;
                                992                 :                : 
                                993                 :                :     /*
                                994                 :                :      * Set up to return the function value.  For pass-by-reference datatypes,
                                995                 :                :      * be sure to allocate the result in resultcontext, not the current memory
                                996                 :                :      * context (which has query lifespan).  We can't leave the data in the
                                997                 :                :      * TupleTableSlot because we intend to clear the slot before returning.
                                998                 :                :      */
 6944                           999                 :          54738 :     oldcontext = MemoryContextSwitchTo(resultcontext);
                               1000                 :                : 
 7153                          1001         [ +  + ]:          54738 :     if (fcache->returnsTuple)
                               1002                 :                :     {
                               1003                 :                :         /* We must return the whole tuple as a Datum. */
                               1004                 :            717 :         fcinfo->isnull = false;
 1977 andres@anarazel.de       1005                 :            717 :         value = ExecFetchSlotHeapTupleDatum(slot);
                               1006                 :                :     }
                               1007                 :                :     else
                               1008                 :                :     {
                               1009                 :                :         /*
                               1010                 :                :          * Returning a scalar, which we have to extract from the first column
                               1011                 :                :          * of the SELECT result, and then copy into result context if needed.
                               1012                 :                :          */
 6969 tgl@sss.pgh.pa.us        1013                 :          54021 :         value = slot_getattr(slot, 1, &(fcinfo->isnull));
                               1014                 :                : 
 7153                          1015         [ +  + ]:          54021 :         if (!fcinfo->isnull)
                               1016                 :          53848 :             value = datumCopy(value, fcache->typbyval, fcache->typlen);
                               1017                 :                :     }
                               1018                 :                : 
 6944                          1019                 :          54738 :     MemoryContextSwitchTo(oldcontext);
                               1020                 :                : 
 7153                          1021                 :          54738 :     return value;
                               1022                 :                : }
                               1023                 :                : 
                               1024                 :                : /*
                               1025                 :                :  * fmgr_sql: function call manager for SQL functions
                               1026                 :                :  */
                               1027                 :                : Datum
 8634                          1028                 :          63133 : fmgr_sql(PG_FUNCTION_ARGS)
                               1029                 :                : {
                               1030                 :                :     SQLFunctionCachePtr fcache;
                               1031                 :                :     ErrorContextCallback sqlerrcontext;
                               1032                 :                :     MemoryContext oldcontext;
                               1033                 :                :     bool        randomAccess;
                               1034                 :                :     bool        lazyEvalOK;
                               1035                 :                :     bool        is_first;
                               1036                 :                :     bool        pushed_snapshot;
                               1037                 :                :     execution_state *es;
                               1038                 :                :     TupleTableSlot *slot;
                               1039                 :                :     Datum       result;
                               1040                 :                :     List       *eslist;
                               1041                 :                :     ListCell   *eslc;
                               1042                 :                : 
                               1043                 :                :     /*
                               1044                 :                :      * Setup error traceback support for ereport()
                               1045                 :                :      */
 7566                          1046                 :          63133 :     sqlerrcontext.callback = sql_exec_error_callback;
                               1047                 :          63133 :     sqlerrcontext.arg = fcinfo->flinfo;
                               1048                 :          63133 :     sqlerrcontext.previous = error_context_stack;
                               1049                 :          63133 :     error_context_stack = &sqlerrcontext;
                               1050                 :                : 
                               1051                 :                :     /* Check call context */
 5644                          1052         [ +  + ]:          63133 :     if (fcinfo->flinfo->fn_retset)
                               1053                 :                :     {
                               1054                 :           2501 :         ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
                               1055                 :                : 
                               1056                 :                :         /*
                               1057                 :                :          * For simplicity, we require callers to support both set eval modes.
                               1058                 :                :          * There are cases where we must use one or must use the other, and
                               1059                 :                :          * it's not really worthwhile to postpone the check till we know. But
                               1060                 :                :          * note we do not require caller to provide an expectedDesc.
                               1061                 :                :          */
                               1062   [ +  -  +  - ]:           2501 :         if (!rsi || !IsA(rsi, ReturnSetInfo) ||
                               1063         [ +  - ]:           2501 :             (rsi->allowedModes & SFRM_ValuePerCall) == 0 ||
 5421                          1064         [ -  + ]:           2501 :             (rsi->allowedModes & SFRM_Materialize) == 0)
 5644 tgl@sss.pgh.pa.us        1065         [ #  # ]:UBC           0 :             ereport(ERROR,
                               1066                 :                :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               1067                 :                :                      errmsg("set-valued function called in context that cannot accept a set")));
 5644 tgl@sss.pgh.pa.us        1068                 :CBC        2501 :         randomAccess = rsi->allowedModes & SFRM_Materialize_Random;
                               1069                 :           2501 :         lazyEvalOK = !(rsi->allowedModes & SFRM_Materialize_Preferred);
                               1070                 :                :     }
                               1071                 :                :     else
                               1072                 :                :     {
                               1073                 :          60632 :         randomAccess = false;
                               1074                 :          60632 :         lazyEvalOK = true;
                               1075                 :                :     }
                               1076                 :                : 
                               1077                 :                :     /*
                               1078                 :                :      * Initialize fcache (build plans) if first time through; or re-initialize
                               1079                 :                :      * if the cache is stale.
                               1080                 :                :      */
 8634                          1081                 :          63133 :     fcache = (SQLFunctionCachePtr) fcinfo->flinfo->fn_extra;
                               1082                 :                : 
 4060                          1083         [ +  + ]:          63133 :     if (fcache != NULL)
                               1084                 :                :     {
   42 heikki.linnakangas@i     1085         [ +  + ]:GNC       45131 :         if (fcache->lxid != MyProc->vxid.lxid ||
 4060 tgl@sss.pgh.pa.us        1086         [ -  + ]:CBC       45110 :             !SubTransactionIsActive(fcache->subxid))
                               1087                 :                :         {
                               1088                 :                :             /* It's stale; unlink and delete */
                               1089                 :             21 :             fcinfo->flinfo->fn_extra = NULL;
                               1090                 :             21 :             MemoryContextDelete(fcache->fcontext);
                               1091                 :             21 :             fcache = NULL;
                               1092                 :                :         }
                               1093                 :                :     }
                               1094                 :                : 
 8634                          1095         [ +  + ]:          63133 :     if (fcache == NULL)
                               1096                 :                :     {
 1558                          1097                 :          18023 :         init_sql_fcache(fcinfo, PG_GET_COLLATION(), lazyEvalOK);
 8634                          1098                 :          18016 :         fcache = (SQLFunctionCachePtr) fcinfo->flinfo->fn_extra;
                               1099                 :                :     }
                               1100                 :                : 
                               1101                 :                :     /*
                               1102                 :                :      * Switch to context in which the fcache lives.  This ensures that our
                               1103                 :                :      * tuplestore etc will have sufficient lifetime.  The sub-executor is
                               1104                 :                :      * responsible for deleting per-tuple information.  (XXX in the case of a
                               1105                 :                :      * long-lived FmgrInfo, this policy represents more memory leakage, but
                               1106                 :                :      * it's not entirely clear where to keep stuff instead.)
                               1107                 :                :      */
 4060                          1108                 :          63126 :     oldcontext = MemoryContextSwitchTo(fcache->fcontext);
                               1109                 :                : 
                               1110                 :                :     /*
                               1111                 :                :      * Find first unfinished query in function, and note whether it's the
                               1112                 :                :      * first query.
                               1113                 :                :      */
                               1114                 :          63126 :     eslist = fcache->func_state;
 4794                          1115                 :          63126 :     es = NULL;
                               1116                 :          63126 :     is_first = true;
                               1117   [ +  +  +  -  :          63126 :     foreach(eslc, eslist)
                                              +  + ]
                               1118                 :                :     {
                               1119                 :          63123 :         es = (execution_state *) lfirst(eslc);
                               1120                 :                : 
                               1121   [ +  -  -  + ]:          63123 :         while (es && es->status == F_EXEC_DONE)
                               1122                 :                :         {
 4794 tgl@sss.pgh.pa.us        1123                 :UBC           0 :             is_first = false;
                               1124                 :              0 :             es = es->next;
                               1125                 :                :         }
                               1126                 :                : 
 4794 tgl@sss.pgh.pa.us        1127         [ +  - ]:CBC       63123 :         if (es)
                               1128                 :          63123 :             break;
                               1129                 :                :     }
                               1130                 :                : 
                               1131                 :                :     /*
                               1132                 :                :      * Convert params to appropriate format if starting a fresh execution. (If
                               1133                 :                :      * continuing execution, we can re-use prior params.)
                               1134                 :                :      */
                               1135   [ +  -  +  +  :          63126 :     if (is_first && es && es->status == F_EXEC_START)
                                              +  + ]
 7801                          1136                 :          62643 :         postquel_sub_params(fcache, fcinfo);
                               1137                 :                : 
                               1138                 :                :     /*
                               1139                 :                :      * Build tuplestore to hold results, if we don't have one already. Note
                               1140                 :                :      * it's in the query-lifespan context.
                               1141                 :                :      */
 5644                          1142         [ +  + ]:          63126 :     if (!fcache->tstore)
                               1143                 :          18109 :         fcache->tstore = tuplestore_begin_heap(randomAccess, false, work_mem);
                               1144                 :                : 
                               1145                 :                :     /*
                               1146                 :                :      * Execute each command in the function one after another until we either
                               1147                 :                :      * run out of commands or get a result row from a lazily-evaluated SELECT.
                               1148                 :                :      *
                               1149                 :                :      * Notes about snapshot management:
                               1150                 :                :      *
                               1151                 :                :      * In a read-only function, we just use the surrounding query's snapshot.
                               1152                 :                :      *
                               1153                 :                :      * In a non-read-only function, we rely on the fact that we'll never
                               1154                 :                :      * suspend execution between queries of the function: the only reason to
                               1155                 :                :      * suspend execution before completion is if we are returning a row from a
                               1156                 :                :      * lazily-evaluated SELECT.  So, when first entering this loop, we'll
                               1157                 :                :      * either start a new query (and push a fresh snapshot) or re-establish
                               1158                 :                :      * the active snapshot from the existing query descriptor.  If we need to
                               1159                 :                :      * start a new query in a subsequent execution of the loop, either we need
                               1160                 :                :      * a fresh snapshot (and pushed_snapshot is false) or the existing
                               1161                 :                :      * snapshot is on the active stack and we can just bump its command ID.
                               1162                 :                :      */
 4794                          1163                 :          63126 :     pushed_snapshot = false;
 7905                          1164         [ +  + ]:         141479 :     while (es)
                               1165                 :                :     {
                               1166                 :                :         bool        completed;
                               1167                 :                : 
 5644                          1168         [ +  + ]:          83170 :         if (es->status == F_EXEC_START)
                               1169                 :                :         {
                               1170                 :                :             /*
                               1171                 :                :              * If not read-only, be sure to advance the command counter for
                               1172                 :                :              * each command, so that all work to date in this transaction is
                               1173                 :                :              * visible.  Take a new snapshot if we don't have one yet,
                               1174                 :                :              * otherwise just bump the command ID in the existing snapshot.
                               1175                 :                :              */
 4794                          1176         [ +  + ]:          82690 :             if (!fcache->readonly_func)
                               1177                 :                :             {
                               1178                 :          75992 :                 CommandCounterIncrement();
                               1179         [ +  + ]:          75992 :                 if (!pushed_snapshot)
                               1180                 :                :                 {
                               1181                 :          75989 :                     PushActiveSnapshot(GetTransactionSnapshot());
                               1182                 :          75989 :                     pushed_snapshot = true;
                               1183                 :                :                 }
                               1184                 :                :                 else
                               1185                 :              3 :                     UpdateActiveSnapshotCommandId();
                               1186                 :                :             }
                               1187                 :                : 
 5644                          1188                 :          82690 :             postquel_start(es, fcache);
                               1189                 :                :         }
 4794                          1190   [ +  +  +  - ]:            480 :         else if (!fcache->readonly_func && !pushed_snapshot)
                               1191                 :                :         {
                               1192                 :                :             /* Re-establish active snapshot when re-entering function */
                               1193                 :            306 :             PushActiveSnapshot(es->qd->snapshot);
                               1194                 :            306 :             pushed_snapshot = true;
                               1195                 :                :         }
                               1196                 :                : 
 5644                          1197                 :          83170 :         completed = postquel_getnext(es, fcache);
                               1198                 :                : 
                               1199                 :                :         /*
                               1200                 :                :          * If we ran the command to completion, we can shut it down now. Any
                               1201                 :                :          * row(s) we need to return are safely stashed in the tuplestore, and
                               1202                 :                :          * we want to be sure that, for example, AFTER triggers get fired
                               1203                 :                :          * before we return anything.  Also, if the function doesn't return
                               1204                 :                :          * set, we can shut it down anyway because it must be a SELECT and we
                               1205                 :                :          * don't care about fetching any more result rows.
                               1206                 :                :          */
                               1207   [ +  +  +  + ]:          78842 :         if (completed || !fcache->returnsSet)
                               1208                 :          78362 :             postquel_end(es);
                               1209                 :                : 
                               1210                 :                :         /*
                               1211                 :                :          * Break from loop if we didn't shut down (implying we got a
                               1212                 :                :          * lazily-evaluated row).  Otherwise we'll press on till the whole
                               1213                 :                :          * function is done, relying on the tuplestore to keep hold of the
                               1214                 :                :          * data to eventually be returned.  This is necessary since an
                               1215                 :                :          * INSERT/UPDATE/DELETE RETURNING that sets the result might be
                               1216                 :                :          * followed by additional rule-inserted commands, and we want to
                               1217                 :                :          * finish doing all those commands before we return anything.
                               1218                 :                :          */
 9716 bruce@momjian.us         1219         [ +  + ]:          78833 :         if (es->status != F_EXEC_DONE)
                               1220                 :            480 :             break;
                               1221                 :                : 
                               1222                 :                :         /*
                               1223                 :                :          * Advance to next execution_state, which might be in the next list.
                               1224                 :                :          */
                               1225                 :          78353 :         es = es->next;
 4794 tgl@sss.pgh.pa.us        1226         [ +  + ]:          98397 :         while (!es)
                               1227                 :                :         {
 1735                          1228                 :          78350 :             eslc = lnext(eslist, eslc);
 4794                          1229         [ +  + ]:          78350 :             if (!eslc)
                               1230                 :          58306 :                 break;          /* end of function */
                               1231                 :                : 
                               1232                 :          20044 :             es = (execution_state *) lfirst(eslc);
                               1233                 :                : 
                               1234                 :                :             /*
                               1235                 :                :              * Flush the current snapshot so that we will take a new one for
                               1236                 :                :              * the new query list.  This ensures that new snaps are taken at
                               1237                 :                :              * original-query boundaries, matching the behavior of interactive
                               1238                 :                :              * execution.
                               1239                 :                :              */
                               1240         [ +  - ]:          20044 :             if (pushed_snapshot)
                               1241                 :                :             {
                               1242                 :          20044 :                 PopActiveSnapshot();
                               1243                 :          20044 :                 pushed_snapshot = false;
                               1244                 :                :             }
                               1245                 :                :         }
                               1246                 :                :     }
                               1247                 :                : 
                               1248                 :                :     /*
                               1249                 :                :      * The tuplestore now contains whatever row(s) we are supposed to return.
                               1250                 :                :      */
 5644                          1251         [ +  + ]:          58789 :     if (fcache->returnsSet)
                               1252                 :                :     {
                               1253                 :           2498 :         ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
                               1254                 :                : 
                               1255         [ +  + ]:           2498 :         if (es)
                               1256                 :                :         {
                               1257                 :                :             /*
                               1258                 :                :              * If we stopped short of being done, we must have a lazy-eval
                               1259                 :                :              * row.
                               1260                 :                :              */
                               1261         [ -  + ]:            480 :             Assert(es->lazyEval);
                               1262                 :                :             /* Re-use the junkfilter's output slot to fetch back the tuple */
                               1263         [ -  + ]:            480 :             Assert(fcache->junkFilter);
                               1264                 :            480 :             slot = fcache->junkFilter->jf_resultSlot;
 5497                          1265         [ -  + ]:            480 :             if (!tuplestore_gettupleslot(fcache->tstore, true, false, slot))
 5644 tgl@sss.pgh.pa.us        1266         [ #  # ]:UBC           0 :                 elog(ERROR, "failed to fetch lazy-eval tuple");
                               1267                 :                :             /* Extract the result as a datum, and copy out from the slot */
 5644 tgl@sss.pgh.pa.us        1268                 :CBC         480 :             result = postquel_get_single_result(slot, fcinfo,
                               1269                 :                :                                                 fcache, oldcontext);
                               1270                 :                :             /* Clear the tuplestore, but keep it for next time */
                               1271                 :                :             /* NB: this might delete the slot's content, but we don't care */
                               1272                 :            480 :             tuplestore_clear(fcache->tstore);
                               1273                 :                : 
                               1274                 :                :             /*
                               1275                 :                :              * Let caller know we're not finished.
                               1276                 :                :              */
                               1277                 :            480 :             rsi->isDone = ExprMultipleResult;
                               1278                 :                : 
                               1279                 :                :             /*
                               1280                 :                :              * Ensure we will get shut down cleanly if the exprcontext is not
                               1281                 :                :              * run to completion.
                               1282                 :                :              */
                               1283         [ +  + ]:            480 :             if (!fcache->shutdown_reg)
                               1284                 :                :             {
                               1285                 :            358 :                 RegisterExprContextCallback(rsi->econtext,
                               1286                 :                :                                             ShutdownSQLFunction,
                               1287                 :                :                                             PointerGetDatum(fcache));
                               1288                 :            358 :                 fcache->shutdown_reg = true;
                               1289                 :                :             }
                               1290                 :                :         }
                               1291         [ +  + ]:           2018 :         else if (fcache->lazyEval)
                               1292                 :                :         {
                               1293                 :                :             /*
                               1294                 :                :              * We are done with a lazy evaluation.  Clean up.
                               1295                 :                :              */
                               1296                 :           1583 :             tuplestore_clear(fcache->tstore);
                               1297                 :                : 
                               1298                 :                :             /*
                               1299                 :                :              * Let caller know we're finished.
                               1300                 :                :              */
                               1301                 :           1583 :             rsi->isDone = ExprEndResult;
                               1302                 :                : 
 8634                          1303                 :           1583 :             fcinfo->isnull = true;
                               1304                 :           1583 :             result = (Datum) 0;
                               1305                 :                : 
                               1306                 :                :             /* Deregister shutdown callback, if we made one */
 8008                          1307         [ +  + ]:           1583 :             if (fcache->shutdown_reg)
                               1308                 :                :             {
                               1309                 :            358 :                 UnregisterExprContextCallback(rsi->econtext,
                               1310                 :                :                                               ShutdownSQLFunction,
                               1311                 :                :                                               PointerGetDatum(fcache));
                               1312                 :            358 :                 fcache->shutdown_reg = false;
                               1313                 :                :             }
                               1314                 :                :         }
                               1315                 :                :         else
                               1316                 :                :         {
                               1317                 :                :             /*
                               1318                 :                :              * We are done with a non-lazy evaluation.  Return whatever is in
                               1319                 :                :              * the tuplestore.  (It is now caller's responsibility to free the
                               1320                 :                :              * tuplestore when done.)
                               1321                 :                :              */
 5644                          1322                 :            435 :             rsi->returnMode = SFRM_Materialize;
                               1323                 :            435 :             rsi->setResult = fcache->tstore;
                               1324                 :            435 :             fcache->tstore = NULL;
                               1325                 :                :             /* must copy desc because execSRF.c will free it */
                               1326         [ +  + ]:            435 :             if (fcache->junkFilter)
                               1327                 :            432 :                 rsi->setDesc = CreateTupleDescCopy(fcache->junkFilter->jf_cleanTupType);
                               1328                 :                : 
                               1329                 :            435 :             fcinfo->isnull = true;
                               1330                 :            435 :             result = (Datum) 0;
                               1331                 :                : 
                               1332                 :                :             /* Deregister shutdown callback, if we made one */
                               1333         [ -  + ]:            435 :             if (fcache->shutdown_reg)
                               1334                 :                :             {
 5644 tgl@sss.pgh.pa.us        1335                 :UBC           0 :                 UnregisterExprContextCallback(rsi->econtext,
                               1336                 :                :                                               ShutdownSQLFunction,
                               1337                 :                :                                               PointerGetDatum(fcache));
                               1338                 :              0 :                 fcache->shutdown_reg = false;
                               1339                 :                :             }
                               1340                 :                :         }
                               1341                 :                :     }
                               1342                 :                :     else
                               1343                 :                :     {
                               1344                 :                :         /*
                               1345                 :                :          * Non-set function.  If we got a row, return it; else return NULL.
                               1346                 :                :          */
 5644 tgl@sss.pgh.pa.us        1347         [ +  + ]:CBC       56291 :         if (fcache->junkFilter)
                               1348                 :                :         {
                               1349                 :                :             /* Re-use the junkfilter's output slot to fetch back the tuple */
                               1350                 :          56192 :             slot = fcache->junkFilter->jf_resultSlot;
 5497                          1351         [ +  + ]:          56192 :             if (tuplestore_gettupleslot(fcache->tstore, true, false, slot))
 5644                          1352                 :          54258 :                 result = postquel_get_single_result(slot, fcinfo,
                               1353                 :                :                                                     fcache, oldcontext);
                               1354                 :                :             else
                               1355                 :                :             {
                               1356                 :           1934 :                 fcinfo->isnull = true;
                               1357                 :           1934 :                 result = (Datum) 0;
                               1358                 :                :             }
                               1359                 :                :         }
                               1360                 :                :         else
                               1361                 :                :         {
                               1362                 :                :             /* Should only get here for VOID functions and procedures */
 2235 peter_e@gmx.net          1363         [ -  + ]:             99 :             Assert(fcache->rettype == VOIDOID);
 5644 tgl@sss.pgh.pa.us        1364                 :             99 :             fcinfo->isnull = true;
                               1365                 :             99 :             result = (Datum) 0;
                               1366                 :                :         }
                               1367                 :                : 
                               1368                 :                :         /* Clear the tuplestore, but keep it for next time */
                               1369                 :          56291 :         tuplestore_clear(fcache->tstore);
                               1370                 :                :     }
                               1371                 :                : 
                               1372                 :                :     /* Pop snapshot if we have pushed one */
 4794                          1373         [ +  + ]:          58789 :     if (pushed_snapshot)
                               1374                 :          51942 :         PopActiveSnapshot();
                               1375                 :                : 
                               1376                 :                :     /*
                               1377                 :                :      * If we've gone through every command in the function, we are done. Reset
                               1378                 :                :      * the execution states to start over again on next call.
                               1379                 :                :      */
 5644                          1380         [ +  + ]:          58789 :     if (es == NULL)
                               1381                 :                :     {
 4794                          1382   [ +  +  +  +  :         136659 :         foreach(eslc, fcache->func_state)
                                              +  + ]
                               1383                 :                :         {
                               1384                 :          78350 :             es = (execution_state *) lfirst(eslc);
                               1385         [ +  + ]:         156703 :             while (es)
                               1386                 :                :             {
                               1387                 :          78353 :                 es->status = F_EXEC_START;
                               1388                 :          78353 :                 es = es->next;
                               1389                 :                :             }
                               1390                 :                :         }
                               1391                 :                :     }
                               1392                 :                : 
 7566                          1393                 :          58789 :     error_context_stack = sqlerrcontext.previous;
                               1394                 :                : 
 8677                          1395                 :          58789 :     MemoryContextSwitchTo(oldcontext);
                               1396                 :                : 
 9716 bruce@momjian.us         1397                 :          58789 :     return result;
                               1398                 :                : }
                               1399                 :                : 
                               1400                 :                : 
                               1401                 :                : /*
                               1402                 :                :  * error context callback to let us supply a call-stack traceback
                               1403                 :                :  */
                               1404                 :                : static void
 7566 tgl@sss.pgh.pa.us        1405                 :           4369 : sql_exec_error_callback(void *arg)
                               1406                 :                : {
                               1407                 :           4369 :     FmgrInfo   *flinfo = (FmgrInfo *) arg;
                               1408                 :           4369 :     SQLFunctionCachePtr fcache = (SQLFunctionCachePtr) flinfo->fn_extra;
                               1409                 :                :     int         syntaxerrposition;
                               1410                 :                : 
                               1411                 :                :     /*
                               1412                 :                :      * We can do nothing useful if init_sql_fcache() didn't get as far as
                               1413                 :                :      * saving the function name
                               1414                 :                :      */
 5140                          1415   [ +  -  -  + ]:           4369 :     if (fcache == NULL || fcache->fname == NULL)
 5140 tgl@sss.pgh.pa.us        1416                 :UBC           0 :         return;
                               1417                 :                : 
                               1418                 :                :     /*
                               1419                 :                :      * If there is a syntax error position, convert to internal syntax error
                               1420                 :                :      */
 7329 tgl@sss.pgh.pa.us        1421                 :CBC        4369 :     syntaxerrposition = geterrposition();
 5140                          1422   [ +  +  +  - ]:           4369 :     if (syntaxerrposition > 0 && fcache->src != NULL)
                               1423                 :                :     {
 7329                          1424                 :              1 :         errposition(0);
                               1425                 :              1 :         internalerrposition(syntaxerrposition);
 5140                          1426                 :              1 :         internalerrquery(fcache->src);
                               1427                 :                :     }
                               1428                 :                : 
                               1429                 :                :     /*
                               1430                 :                :      * Try to determine where in the function we failed.  If there is a query
                               1431                 :                :      * with non-null QueryDesc, finger it.  (We check this rather than looking
                               1432                 :                :      * for F_EXEC_RUN state, so that errors during ExecutorStart or
                               1433                 :                :      * ExecutorEnd are blamed on the appropriate query; see postquel_start and
                               1434                 :                :      * postquel_end.)
                               1435                 :                :      */
                               1436         [ +  + ]:           4369 :     if (fcache->func_state)
                               1437                 :                :     {
                               1438                 :                :         execution_state *es;
                               1439                 :                :         int         query_num;
                               1440                 :                :         ListCell   *lc;
                               1441                 :                : 
 4794                          1442                 :           4362 :         es = NULL;
 7566                          1443                 :           4362 :         query_num = 1;
 4794                          1444   [ +  -  +  -  :           4362 :         foreach(lc, fcache->func_state)
                                              +  - ]
                               1445                 :                :         {
                               1446                 :           4362 :             es = (execution_state *) lfirst(lc);
                               1447         [ +  - ]:           4362 :             while (es)
                               1448                 :                :             {
                               1449         [ +  - ]:           4362 :                 if (es->qd)
                               1450                 :                :                 {
                               1451                 :           4362 :                     errcontext("SQL function \"%s\" statement %d",
                               1452                 :                :                                fcache->fname, query_num);
                               1453                 :           4362 :                     break;
                               1454                 :                :                 }
 4794 tgl@sss.pgh.pa.us        1455                 :UBC           0 :                 es = es->next;
                               1456                 :                :             }
 4794 tgl@sss.pgh.pa.us        1457         [ +  - ]:CBC        4362 :             if (es)
                               1458                 :           4362 :                 break;
 7566 tgl@sss.pgh.pa.us        1459                 :UBC           0 :             query_num++;
                               1460                 :                :         }
 7566 tgl@sss.pgh.pa.us        1461         [ -  + ]:CBC        4362 :         if (es == NULL)
                               1462                 :                :         {
                               1463                 :                :             /*
                               1464                 :                :              * couldn't identify a running query; might be function entry,
                               1465                 :                :              * function exit, or between queries.
                               1466                 :                :              */
 5140 tgl@sss.pgh.pa.us        1467                 :UBC           0 :             errcontext("SQL function \"%s\"", fcache->fname);
                               1468                 :                :         }
                               1469                 :                :     }
                               1470                 :                :     else
                               1471                 :                :     {
                               1472                 :                :         /*
                               1473                 :                :          * Assume we failed during init_sql_fcache().  (It's possible that the
                               1474                 :                :          * function actually has an empty body, but in that case we may as
                               1475                 :                :          * well report all errors as being "during startup".)
                               1476                 :                :          */
 5140 tgl@sss.pgh.pa.us        1477                 :CBC           7 :         errcontext("SQL function \"%s\" during startup", fcache->fname);
                               1478                 :                :     }
                               1479                 :                : }
                               1480                 :                : 
                               1481                 :                : 
                               1482                 :                : /*
                               1483                 :                :  * callback function in case a function-returning-set needs to be shut down
                               1484                 :                :  * before it has been run to completion
                               1485                 :                :  */
                               1486                 :                : static void
 8008 tgl@sss.pgh.pa.us        1487                 :UBC           0 : ShutdownSQLFunction(Datum arg)
                               1488                 :                : {
                               1489                 :              0 :     SQLFunctionCachePtr fcache = (SQLFunctionCachePtr) DatumGetPointer(arg);
                               1490                 :                :     execution_state *es;
                               1491                 :                :     ListCell   *lc;
                               1492                 :                : 
 4794                          1493   [ #  #  #  #  :              0 :     foreach(lc, fcache->func_state)
                                              #  # ]
                               1494                 :                :     {
                               1495                 :              0 :         es = (execution_state *) lfirst(lc);
                               1496         [ #  # ]:              0 :         while (es)
                               1497                 :                :         {
                               1498                 :                :             /* Shut down anything still running */
                               1499         [ #  # ]:              0 :             if (es->status == F_EXEC_RUN)
                               1500                 :                :             {
                               1501                 :                :                 /* Re-establish active snapshot for any called functions */
                               1502         [ #  # ]:              0 :                 if (!fcache->readonly_func)
                               1503                 :              0 :                     PushActiveSnapshot(es->qd->snapshot);
                               1504                 :                : 
                               1505                 :              0 :                 postquel_end(es);
                               1506                 :                : 
                               1507         [ #  # ]:              0 :                 if (!fcache->readonly_func)
                               1508                 :              0 :                     PopActiveSnapshot();
                               1509                 :                :             }
                               1510                 :                : 
                               1511                 :                :             /* Reset states to START in case we're called again */
                               1512                 :              0 :             es->status = F_EXEC_START;
                               1513                 :              0 :             es = es->next;
                               1514                 :                :         }
                               1515                 :                :     }
                               1516                 :                : 
                               1517                 :                :     /* Release tuplestore if we have one */
 5644                          1518         [ #  # ]:              0 :     if (fcache->tstore)
                               1519                 :              0 :         tuplestore_end(fcache->tstore);
                               1520                 :              0 :     fcache->tstore = NULL;
                               1521                 :                : 
                               1522                 :                :     /* execUtils will deregister the callback... */
 8008                          1523                 :              0 :     fcache->shutdown_reg = false;
                               1524                 :              0 : }
                               1525                 :                : 
                               1526                 :                : /*
                               1527                 :                :  * check_sql_fn_statements
                               1528                 :                :  *
                               1529                 :                :  * Check statements in an SQL function.  Error out if there is anything that
                               1530                 :                :  * is not acceptable.
                               1531                 :                :  */
                               1532                 :                : void
 1273 tgl@sss.pgh.pa.us        1533                 :CBC       20973 : check_sql_fn_statements(List *queryTreeLists)
                               1534                 :                : {
                               1535                 :                :     ListCell   *lc;
                               1536                 :                : 
                               1537                 :                :     /* We are given a list of sublists of Queries */
                               1538   [ +  +  +  +  :          42063 :     foreach(lc, queryTreeLists)
                                              +  + ]
                               1539                 :                :     {
                               1540                 :          21093 :         List       *sublist = lfirst_node(List, lc);
                               1541                 :                :         ListCell   *lc2;
                               1542                 :                : 
                               1543   [ +  -  +  +  :          42186 :         foreach(lc2, sublist)
                                              +  + ]
                               1544                 :                :         {
                               1545                 :          21096 :             Query      *query = lfirst_node(Query, lc2);
                               1546                 :                : 
                               1547                 :                :             /*
                               1548                 :                :              * Disallow calling procedures with output arguments.  The current
                               1549                 :                :              * implementation would just throw the output values away, unless
                               1550                 :                :              * the statement is the last one.  Per SQL standard, we should
                               1551                 :                :              * assign the output values by name.  By disallowing this here, we
                               1552                 :                :              * preserve an opportunity for future improvement.
                               1553                 :                :              */
                               1554         [ +  + ]:          21096 :             if (query->commandType == CMD_UTILITY &&
                               1555         [ +  + ]:            109 :                 IsA(query->utilityStmt, CallStmt))
                               1556                 :                :             {
 1039                          1557                 :             15 :                 CallStmt   *stmt = (CallStmt *) query->utilityStmt;
                               1558                 :                : 
                               1559         [ +  + ]:             15 :                 if (stmt->outargs != NIL)
                               1560         [ +  - ]:              3 :                     ereport(ERROR,
                               1561                 :                :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               1562                 :                :                              errmsg("calling procedures with output arguments is not supported in SQL functions")));
                               1563                 :                :             }
                               1564                 :                :         }
                               1565                 :                :     }
 2223 peter_e@gmx.net          1566                 :          20970 : }
                               1567                 :                : 
                               1568                 :                : /*
                               1569                 :                :  * check_sql_fn_retval()
                               1570                 :                :  *      Check return value of a list of lists of sql parse trees.
                               1571                 :                :  *
                               1572                 :                :  * The return value of a sql function is the value returned by the last
                               1573                 :                :  * canSetTag query in the function.  We do some ad-hoc type checking and
                               1574                 :                :  * coercion here to ensure that the function returns what it's supposed to.
                               1575                 :                :  * Note that we may actually modify the last query to make it match!
                               1576                 :                :  *
                               1577                 :                :  * This function returns true if the sql function returns the entire tuple
                               1578                 :                :  * result of its final statement, or false if it returns just the first column
                               1579                 :                :  * result of that statement.  It throws an error if the final statement doesn't
                               1580                 :                :  * return the right type at all.
                               1581                 :                :  *
                               1582                 :                :  * Note that because we allow "SELECT rowtype_expression", the result can be
                               1583                 :                :  * false even when the declared function return type is a rowtype.
                               1584                 :                :  *
                               1585                 :                :  * For a polymorphic function the passed rettype must be the actual resolved
                               1586                 :                :  * output type of the function.  (This means we can't check the type during
                               1587                 :                :  * function definition of a polymorphic function.)  If we do see a polymorphic
                               1588                 :                :  * rettype we'll throw an error, saying it is not a supported rettype.
                               1589                 :                :  *
                               1590                 :                :  * If the function returns composite, the passed rettupdesc should describe
                               1591                 :                :  * the expected output.  If rettupdesc is NULL, we can't verify that the
                               1592                 :                :  * output matches; that should only happen in fmgr_sql_validator(), or when
                               1593                 :                :  * the function returns RECORD and the caller doesn't actually care which
                               1594                 :                :  * composite type it is.
                               1595                 :                :  *
                               1596                 :                :  * (Typically, rettype and rettupdesc are computed by get_call_result_type
                               1597                 :                :  * or a sibling function.)
                               1598                 :                :  *
                               1599                 :                :  * In addition to coercing individual output columns, we can modify the
                               1600                 :                :  * output to include dummy NULL columns for any dropped columns appearing
                               1601                 :                :  * in rettupdesc.  This is done only if the caller asks for it.
                               1602                 :                :  *
                               1603                 :                :  * If resultTargetList isn't NULL, then *resultTargetList is set to the
                               1604                 :                :  * targetlist that defines the final statement's result.  Exception: if the
                               1605                 :                :  * function is defined to return VOID then *resultTargetList is set to NIL.
                               1606                 :                :  */
                               1607                 :                : bool
 1273 tgl@sss.pgh.pa.us        1608                 :GBC       30930 : check_sql_fn_retval(List *queryTreeLists,
                               1609                 :                :                     Oid rettype, TupleDesc rettupdesc,
                               1610                 :                :                     char prokind,
                               1611                 :                :                     bool insertDroppedCols,
                               1612                 :                :                     List **resultTargetList)
                               1613                 :                : {
 1558 tgl@sss.pgh.pa.us        1614                 :CBC       30930 :     bool        is_tuple_result = false;
                               1615                 :                :     Query      *parse;
                               1616                 :                :     ListCell   *parse_cell;
                               1617                 :                :     List       *tlist;
                               1618                 :                :     int         tlistlen;
                               1619                 :                :     bool        tlist_is_modifiable;
                               1620                 :                :     char        fn_typtype;
                               1621                 :          30930 :     List       *upper_tlist = NIL;
                               1622                 :          30930 :     bool        upper_tlist_nontrivial = false;
                               1623                 :                :     ListCell   *lc;
                               1624                 :                : 
                               1625         [ +  + ]:          30930 :     if (resultTargetList)
                               1626                 :          18022 :         *resultTargetList = NIL;    /* initialize in case of VOID result */
                               1627                 :                : 
                               1628                 :                :     /*
                               1629                 :                :      * If it's declared to return VOID, we don't care what's in the function.
                               1630                 :                :      * (This takes care of procedures with no output parameters, as well.)
                               1631                 :                :      */
 2235 peter_e@gmx.net          1632         [ +  + ]:          30930 :     if (rettype == VOIDOID)
 2327                          1633                 :            346 :         return false;
                               1634                 :                : 
                               1635                 :                :     /*
                               1636                 :                :      * Find the last canSetTag query in the function body (which is presented
                               1637                 :                :      * to us as a list of sublists of Query nodes).  This isn't necessarily
                               1638                 :                :      * the last parsetree, because rule rewriting can insert queries after
                               1639                 :                :      * what the user wrote.  Note that it might not even be in the last
                               1640                 :                :      * sublist, for example if the last query rewrites to DO INSTEAD NOTHING.
                               1641                 :                :      * (It might not be unreasonable to throw an error in such a case, but
                               1642                 :                :      * this is the historical behavior and it doesn't seem worth changing.)
                               1643                 :                :      */
 5644 tgl@sss.pgh.pa.us        1644                 :          30584 :     parse = NULL;
 1558                          1645                 :          30584 :     parse_cell = NULL;
 1273                          1646   [ +  -  +  +  :          61279 :     foreach(lc, queryTreeLists)
                                              +  + ]
                               1647                 :                :     {
                               1648                 :          30695 :         List       *sublist = lfirst_node(List, lc);
                               1649                 :                :         ListCell   *lc2;
                               1650                 :                : 
                               1651   [ +  -  +  +  :          61393 :         foreach(lc2, sublist)
                                              +  + ]
                               1652                 :                :         {
                               1653                 :          30698 :             Query      *q = lfirst_node(Query, lc2);
                               1654                 :                : 
                               1655         [ +  + ]:          30698 :             if (q->canSetTag)
                               1656                 :                :             {
                               1657                 :          30695 :                 parse = q;
                               1658                 :          30695 :                 parse_cell = lc2;
                               1659                 :                :             }
                               1660                 :                :         }
                               1661                 :                :     }
                               1662                 :                : 
                               1663                 :                :     /*
                               1664                 :                :      * If it's a plain SELECT, it returns whatever the targetlist says.
                               1665                 :                :      * Otherwise, if it's INSERT/UPDATE/DELETE/MERGE with RETURNING, it
                               1666                 :                :      * returns that. Otherwise, the function return type must be VOID.
                               1667                 :                :      *
                               1668                 :                :      * Note: eventually replace this test with QueryReturnsTuples?  We'd need
                               1669                 :                :      * a more general method of determining the output type, though.  Also, it
                               1670                 :                :      * seems too dangerous to consider FETCH or EXECUTE as returning a
                               1671                 :                :      * determinable rowtype, since they depend on relatively short-lived
                               1672                 :                :      * entities.
                               1673                 :                :      */
 5644                          1674         [ +  - ]:          30584 :     if (parse &&
 2647                          1675         [ +  + ]:          30584 :         parse->commandType == CMD_SELECT)
                               1676                 :                :     {
 5644                          1677                 :          30518 :         tlist = parse->targetList;
                               1678                 :                :         /* tlist is modifiable unless it's a dummy in a setop query */
 1558                          1679                 :          30518 :         tlist_is_modifiable = (parse->setOperations == NULL);
                               1680                 :                :     }
 5644                          1681         [ +  - ]:             66 :     else if (parse &&
                               1682         [ +  + ]:             66 :              (parse->commandType == CMD_INSERT ||
 5644 tgl@sss.pgh.pa.us        1683         [ +  - ]:GBC          15 :               parse->commandType == CMD_UPDATE ||
   28 dean.a.rasheed@gmail     1684         [ +  - ]:GNC          15 :               parse->commandType == CMD_DELETE ||
                               1685         [ +  - ]:             15 :               parse->commandType == CMD_MERGE) &&
 5644 tgl@sss.pgh.pa.us        1686         [ +  - ]:CBC          66 :              parse->returningList)
                               1687                 :                :     {
                               1688                 :             66 :         tlist = parse->returningList;
                               1689                 :                :         /* returningList can always be modified */
 1558                          1690                 :             66 :         tlist_is_modifiable = true;
                               1691                 :                :     }
                               1692                 :                :     else
                               1693                 :                :     {
                               1694                 :                :         /* Empty function body, or last statement is a utility command */
 2235 peter_e@gmx.net          1695         [ #  # ]:UBC           0 :         ereport(ERROR,
                               1696                 :                :                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                               1697                 :                :                  errmsg("return type mismatch in function declared to return %s",
                               1698                 :                :                         format_type_be(rettype)),
                               1699                 :                :                  errdetail("Function's final statement must be SELECT or INSERT/UPDATE/DELETE/MERGE RETURNING.")));
                               1700                 :                :         return false;           /* keep compiler quiet */
                               1701                 :                :     }
                               1702                 :                : 
                               1703                 :                :     /*
                               1704                 :                :      * OK, check that the targetlist returns something matching the declared
                               1705                 :                :      * type, and modify it if necessary.  If possible, we insert any coercion
                               1706                 :                :      * steps right into the final statement's targetlist.  However, that might
                               1707                 :                :      * risk changes in the statement's semantics --- we can't safely change
                               1708                 :                :      * the output type of a grouping column, for instance.  In such cases we
                               1709                 :                :      * handle coercions by inserting an extra level of Query that effectively
                               1710                 :                :      * just does a projection.
                               1711                 :                :      */
                               1712                 :                : 
                               1713                 :                :     /*
                               1714                 :                :      * Count the non-junk entries in the result targetlist.
                               1715                 :                :      */
 7129 tgl@sss.pgh.pa.us        1716                 :CBC       30584 :     tlistlen = ExecCleanTargetListLength(tlist);
                               1717                 :                : 
 6954                          1718                 :          30584 :     fn_typtype = get_typtype(rettype);
                               1719                 :                : 
 6222                          1720   [ +  +  +  + ]:          30584 :     if (fn_typtype == TYPTYPE_BASE ||
                               1721         [ +  + ]:            975 :         fn_typtype == TYPTYPE_DOMAIN ||
                               1722         [ +  + ]:            972 :         fn_typtype == TYPTYPE_ENUM ||
 1211 akorotkov@postgresql     1723         [ +  + ]:            954 :         fn_typtype == TYPTYPE_RANGE ||
                               1724                 :                :         fn_typtype == TYPTYPE_MULTIRANGE)
 7129 tgl@sss.pgh.pa.us        1725                 :          29642 :     {
                               1726                 :                :         /*
                               1727                 :                :          * For scalar-type returns, the target list must have exactly one
                               1728                 :                :          * non-junk entry, and its type must be coercible to rettype.
                               1729                 :                :          */
                               1730                 :                :         TargetEntry *tle;
                               1731                 :                : 
                               1732         [ +  + ]:          29648 :         if (tlistlen != 1)
                               1733         [ +  - ]:              3 :             ereport(ERROR,
                               1734                 :                :                     (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                               1735                 :                :                      errmsg("return type mismatch in function declared to return %s",
                               1736                 :                :                             format_type_be(rettype)),
                               1737                 :                :                      errdetail("Final statement must return exactly one column.")));
                               1738                 :                : 
                               1739                 :                :         /* We assume here that non-junk TLEs must come first in tlists */
 5871                          1740                 :          29645 :         tle = (TargetEntry *) linitial(tlist);
                               1741         [ -  + ]:          29645 :         Assert(!tle->resjunk);
                               1742                 :                : 
 1558                          1743         [ +  + ]:          29645 :         if (!coerce_fn_result_column(tle, rettype, -1,
                               1744                 :                :                                      tlist_is_modifiable,
                               1745                 :                :                                      &upper_tlist,
                               1746                 :                :                                      &upper_tlist_nontrivial))
 7129                          1747         [ +  - ]:              3 :             ereport(ERROR,
                               1748                 :                :                     (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                               1749                 :                :                      errmsg("return type mismatch in function declared to return %s",
                               1750                 :                :                             format_type_be(rettype)),
                               1751                 :                :                      errdetail("Actual return type is %s.",
                               1752                 :                :                                format_type_be(exprType((Node *) tle->expr)))));
                               1753                 :                :     }
 6222                          1754   [ +  +  +  + ]:            936 :     else if (fn_typtype == TYPTYPE_COMPOSITE || rettype == RECORDOID)
 7129                          1755                 :            867 :     {
                               1756                 :                :         /*
                               1757                 :                :          * Returns a rowtype.
                               1758                 :                :          *
                               1759                 :                :          * Note that we will not consider a domain over composite to be a
                               1760                 :                :          * "rowtype" return type; it goes through the scalar case above.  This
                               1761                 :                :          * is because we only provide column-by-column implicit casting, and
                               1762                 :                :          * will not cast the complete record result.  So the only way to
                               1763                 :                :          * produce a domain-over-composite result is to compute it as an
                               1764                 :                :          * explicit single-column result.  The single-composite-column code
                               1765                 :                :          * path just below could handle such cases, but it won't be reached.
                               1766                 :                :          */
                               1767                 :                :         int         tupnatts;   /* physical number of columns in tuple */
                               1768                 :                :         int         tuplogcols; /* # of nondeleted columns in tuple */
                               1769                 :                :         int         colindex;   /* physical column index */
                               1770                 :                : 
                               1771                 :                :         /*
                               1772                 :                :          * If the target list has one non-junk entry, and that expression has
                               1773                 :                :          * or can be coerced to the declared return type, take it as the
                               1774                 :                :          * result.  This allows, for example, 'SELECT func2()', where func2
                               1775                 :                :          * has the same composite return type as the function that's calling
                               1776                 :                :          * it.  This provision creates some ambiguity --- maybe the expression
                               1777                 :                :          * was meant to be the lone field of the composite result --- but it
                               1778                 :                :          * works well enough as long as we don't get too enthusiastic about
                               1779                 :                :          * inventing coercions from scalar to composite types.
                               1780                 :                :          *
                               1781                 :                :          * XXX Note that if rettype is RECORD and the expression is of a named
                               1782                 :                :          * composite type, or vice versa, this coercion will succeed, whether
                               1783                 :                :          * or not the record type really matches.  For the moment we rely on
                               1784                 :                :          * runtime type checking to catch any discrepancy, but it'd be nice to
                               1785                 :                :          * do better at parse time.
                               1786                 :                :          *
                               1787                 :                :          * We must *not* do this for a procedure, however.  Procedures with
                               1788                 :                :          * output parameter(s) have rettype RECORD, and the CALL code expects
                               1789                 :                :          * to get results corresponding to the list of output parameters, even
                               1790                 :                :          * when there's just one parameter that's composite.
                               1791                 :                :          */
   33                          1792   [ +  +  +  + ]:            933 :         if (tlistlen == 1 && prokind != PROKIND_PROCEDURE)
                               1793                 :                :         {
 5871                          1794                 :             63 :             TargetEntry *tle = (TargetEntry *) linitial(tlist);
                               1795                 :                : 
                               1796         [ -  + ]:             63 :             Assert(!tle->resjunk);
 1558                          1797         [ +  + ]:             63 :             if (coerce_fn_result_column(tle, rettype, -1,
                               1798                 :                :                                         tlist_is_modifiable,
                               1799                 :                :                                         &upper_tlist,
                               1800                 :                :                                         &upper_tlist_nontrivial))
                               1801                 :                :             {
                               1802                 :                :                 /* Note that we're NOT setting is_tuple_result */
                               1803                 :             36 :                 goto tlist_coercion_finished;
                               1804                 :                :             }
                               1805                 :                :         }
                               1806                 :                : 
                               1807                 :                :         /*
                               1808                 :                :          * If the caller didn't provide an expected tupdesc, we can't do any
                               1809                 :                :          * further checking.  Assume we're returning the whole tuple.
                               1810                 :                :          */
                               1811         [ +  + ]:            897 :         if (rettupdesc == NULL)
                               1812                 :                :         {
                               1813                 :                :             /* Return tlist if requested */
                               1814         [ +  + ]:             24 :             if (resultTargetList)
                               1815                 :             12 :                 *resultTargetList = tlist;
 6954                          1816                 :             24 :             return true;
                               1817                 :                :         }
                               1818                 :                : 
                               1819                 :                :         /*
                               1820                 :                :          * Verify that the targetlist matches the return tuple type.  We scan
                               1821                 :                :          * the non-resjunk columns, and coerce them if necessary to match the
                               1822                 :                :          * datatypes of the non-deleted attributes.  For deleted attributes,
                               1823                 :                :          * insert NULL result columns if the caller asked for that.
                               1824                 :                :          */
 1558                          1825                 :            873 :         tupnatts = rettupdesc->natts;
 6954                          1826                 :            873 :         tuplogcols = 0;         /* we'll count nondeleted cols as we go */
 7129                          1827                 :            873 :         colindex = 0;
                               1828                 :                : 
 5644                          1829   [ +  -  +  +  :           3306 :         foreach(lc, tlist)
                                              +  + ]
                               1830                 :                :         {
                               1831                 :           2439 :             TargetEntry *tle = (TargetEntry *) lfirst(lc);
                               1832                 :                :             Form_pg_attribute attr;
                               1833                 :                : 
                               1834                 :                :             /* resjunk columns can simply be ignored */
 6948                          1835         [ -  + ]:           2439 :             if (tle->resjunk)
 7129 tgl@sss.pgh.pa.us        1836                 :UBC           0 :                 continue;
                               1837                 :                : 
                               1838                 :                :             do
                               1839                 :                :             {
 7129 tgl@sss.pgh.pa.us        1840                 :CBC        2481 :                 colindex++;
 6954                          1841         [ -  + ]:           2481 :                 if (colindex > tupnatts)
 7129 tgl@sss.pgh.pa.us        1842         [ #  # ]:UBC           0 :                     ereport(ERROR,
                               1843                 :                :                             (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                               1844                 :                :                              errmsg("return type mismatch in function declared to return %s",
                               1845                 :                :                                     format_type_be(rettype)),
                               1846                 :                :                              errdetail("Final statement returns too many columns.")));
 1558 tgl@sss.pgh.pa.us        1847                 :CBC        2481 :                 attr = TupleDescAttr(rettupdesc, colindex - 1);
                               1848   [ +  +  +  + ]:           2481 :                 if (attr->attisdropped && insertDroppedCols)
                               1849                 :                :                 {
                               1850                 :                :                     Expr       *null_expr;
                               1851                 :                : 
                               1852                 :                :                     /* The type of the null we insert isn't important */
 5235                          1853                 :              3 :                     null_expr = (Expr *) makeConst(INT4OID,
                               1854                 :                :                                                    -1,
                               1855                 :                :                                                    InvalidOid,
                               1856                 :                :                                                    sizeof(int32),
                               1857                 :                :                                                    (Datum) 0,
                               1858                 :                :                                                    true,    /* isnull */
                               1859                 :                :                                                    true /* byval */ );
 1558                          1860                 :              3 :                     upper_tlist = lappend(upper_tlist,
                               1861                 :              3 :                                           makeTargetEntry(null_expr,
                               1862                 :              3 :                                                           list_length(upper_tlist) + 1,
                               1863                 :                :                                                           NULL,
                               1864                 :                :                                                           false));
                               1865                 :              3 :                     upper_tlist_nontrivial = true;
                               1866                 :                :                 }
 7129                          1867         [ +  + ]:           2481 :             } while (attr->attisdropped);
 6954                          1868                 :           2439 :             tuplogcols++;
                               1869                 :                : 
 1558                          1870         [ +  + ]:           2439 :             if (!coerce_fn_result_column(tle,
                               1871                 :                :                                          attr->atttypid, attr->atttypmod,
                               1872                 :                :                                          tlist_is_modifiable,
                               1873                 :                :                                          &upper_tlist,
                               1874                 :                :                                          &upper_tlist_nontrivial))
 7129                          1875         [ +  - ]:              6 :                 ereport(ERROR,
                               1876                 :                :                         (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                               1877                 :                :                          errmsg("return type mismatch in function declared to return %s",
                               1878                 :                :                                 format_type_be(rettype)),
                               1879                 :                :                          errdetail("Final statement returns %s instead of %s at column %d.",
                               1880                 :                :                                    format_type_be(exprType((Node *) tle->expr)),
                               1881                 :                :                                    format_type_be(attr->atttypid),
                               1882                 :                :                                    tuplogcols)));
                               1883                 :                :         }
                               1884                 :                : 
                               1885                 :                :         /* remaining columns in rettupdesc had better all be dropped */
 5235                          1886         [ -  + ]:            867 :         for (colindex++; colindex <= tupnatts; colindex++)
                               1887                 :                :         {
 1558 tgl@sss.pgh.pa.us        1888         [ #  # ]:UBC           0 :             if (!TupleDescAttr(rettupdesc, colindex - 1)->attisdropped)
 5235                          1889         [ #  # ]:              0 :                 ereport(ERROR,
                               1890                 :                :                         (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                               1891                 :                :                          errmsg("return type mismatch in function declared to return %s",
                               1892                 :                :                                 format_type_be(rettype)),
                               1893                 :                :                          errdetail("Final statement returns too few columns.")));
 1558                          1894         [ #  # ]:              0 :             if (insertDroppedCols)
                               1895                 :                :             {
                               1896                 :                :                 Expr       *null_expr;
                               1897                 :                : 
                               1898                 :                :                 /* The type of the null we insert isn't important */
 5235                          1899                 :              0 :                 null_expr = (Expr *) makeConst(INT4OID,
                               1900                 :                :                                                -1,
                               1901                 :                :                                                InvalidOid,
                               1902                 :                :                                                sizeof(int32),
                               1903                 :                :                                                (Datum) 0,
                               1904                 :                :                                                true,    /* isnull */
                               1905                 :                :                                                true /* byval */ );
 1558                          1906                 :              0 :                 upper_tlist = lappend(upper_tlist,
                               1907                 :              0 :                                       makeTargetEntry(null_expr,
                               1908                 :              0 :                                                       list_length(upper_tlist) + 1,
                               1909                 :                :                                                       NULL,
                               1910                 :                :                                                       false));
                               1911                 :              0 :                 upper_tlist_nontrivial = true;
                               1912                 :                :             }
                               1913                 :                :         }
                               1914                 :                : 
                               1915                 :                :         /* Report that we are returning entire tuple result */
 1558 tgl@sss.pgh.pa.us        1916                 :CBC         867 :         is_tuple_result = true;
                               1917                 :                :     }
                               1918                 :                :     else
 7129                          1919         [ +  - ]:              3 :         ereport(ERROR,
                               1920                 :                :                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
                               1921                 :                :                  errmsg("return type %s is not supported for SQL functions",
                               1922                 :                :                         format_type_be(rettype))));
                               1923                 :                : 
 1558                          1924                 :          30545 : tlist_coercion_finished:
                               1925                 :                : 
                               1926                 :                :     /*
                               1927                 :                :      * If necessary, modify the final Query by injecting an extra Query level
                               1928                 :                :      * that just performs a projection.  (It'd be dubious to do this to a
                               1929                 :                :      * non-SELECT query, but we never have to; RETURNING lists can always be
                               1930                 :                :      * modified in-place.)
                               1931                 :                :      */
                               1932         [ +  + ]:          30545 :     if (upper_tlist_nontrivial)
                               1933                 :                :     {
                               1934                 :                :         Query      *newquery;
                               1935                 :                :         List       *colnames;
                               1936                 :                :         RangeTblEntry *rte;
                               1937                 :                :         RangeTblRef *rtr;
                               1938                 :                : 
                               1939         [ -  + ]:             33 :         Assert(parse->commandType == CMD_SELECT);
                               1940                 :                : 
                               1941                 :                :         /* Most of the upper Query struct can be left as zeroes/nulls */
                               1942                 :             33 :         newquery = makeNode(Query);
                               1943                 :             33 :         newquery->commandType = CMD_SELECT;
                               1944                 :             33 :         newquery->querySource = parse->querySource;
                               1945                 :             33 :         newquery->canSetTag = true;
                               1946                 :             33 :         newquery->targetList = upper_tlist;
                               1947                 :                : 
                               1948                 :                :         /* We need a moderately realistic colnames list for the subquery RTE */
                               1949                 :             33 :         colnames = NIL;
                               1950   [ +  -  +  +  :             90 :         foreach(lc, parse->targetList)
                                              +  + ]
                               1951                 :                :         {
                               1952                 :             57 :             TargetEntry *tle = (TargetEntry *) lfirst(lc);
                               1953                 :                : 
                               1954         [ -  + ]:             57 :             if (tle->resjunk)
 1558 tgl@sss.pgh.pa.us        1955                 :UBC           0 :                 continue;
 1558 tgl@sss.pgh.pa.us        1956                 :CBC          57 :             colnames = lappend(colnames,
                               1957         [ +  - ]:             57 :                                makeString(tle->resname ? tle->resname : ""));
                               1958                 :                :         }
                               1959                 :                : 
                               1960                 :                :         /* Build a suitable RTE for the subquery */
                               1961                 :             33 :         rte = makeNode(RangeTblEntry);
                               1962                 :             33 :         rte->rtekind = RTE_SUBQUERY;
                               1963                 :             33 :         rte->subquery = parse;
                               1964                 :             33 :         rte->eref = rte->alias = makeAlias("*SELECT*", colnames);
                               1965                 :             33 :         rte->lateral = false;
                               1966                 :             33 :         rte->inh = false;
                               1967                 :             33 :         rte->inFromCl = true;
                               1968                 :             33 :         newquery->rtable = list_make1(rte);
                               1969                 :                : 
                               1970                 :             33 :         rtr = makeNode(RangeTblRef);
                               1971                 :             33 :         rtr->rtindex = 1;
                               1972                 :             33 :         newquery->jointree = makeFromExpr(list_make1(rtr), NULL);
                               1973                 :                : 
                               1974                 :                :         /* Replace original query in the correct element of the query list */
                               1975                 :             33 :         lfirst(parse_cell) = newquery;
                               1976                 :                :     }
                               1977                 :                : 
                               1978                 :                :     /* Return tlist (possibly modified) if requested */
                               1979         [ +  + ]:          30545 :     if (resultTargetList)
                               1980                 :          17865 :         *resultTargetList = upper_tlist;
                               1981                 :                : 
                               1982                 :          30545 :     return is_tuple_result;
                               1983                 :                : }
                               1984                 :                : 
                               1985                 :                : /*
                               1986                 :                :  * Process one function result column for check_sql_fn_retval
                               1987                 :                :  *
                               1988                 :                :  * Coerce the output value to the required type/typmod, and add a column
                               1989                 :                :  * to *upper_tlist for it.  Set *upper_tlist_nontrivial to true if we
                               1990                 :                :  * add an upper tlist item that's not just a Var.
                               1991                 :                :  *
                               1992                 :                :  * Returns true if OK, false if could not coerce to required type
                               1993                 :                :  * (in which case, no changes have been made)
                               1994                 :                :  */
                               1995                 :                : static bool
                               1996                 :          32147 : coerce_fn_result_column(TargetEntry *src_tle,
                               1997                 :                :                         Oid res_type,
                               1998                 :                :                         int32 res_typmod,
                               1999                 :                :                         bool tlist_is_modifiable,
                               2000                 :                :                         List **upper_tlist,
                               2001                 :                :                         bool *upper_tlist_nontrivial)
                               2002                 :                : {
                               2003                 :                :     TargetEntry *new_tle;
                               2004                 :                :     Expr       *new_tle_expr;
                               2005                 :                :     Node       *cast_result;
                               2006                 :                : 
                               2007                 :                :     /*
                               2008                 :                :      * If the TLE has a sortgroupref marking, don't change it, as it probably
                               2009                 :                :      * is referenced by ORDER BY, DISTINCT, etc, and changing its type would
                               2010                 :                :      * break query semantics.  Otherwise, it's safe to modify in-place unless
                               2011                 :                :      * the query as a whole has issues with that.
                               2012                 :                :      */
                               2013   [ +  +  +  + ]:          32147 :     if (tlist_is_modifiable && src_tle->ressortgroupref == 0)
                               2014                 :                :     {
                               2015                 :                :         /* OK to modify src_tle in place, if necessary */
                               2016                 :          63854 :         cast_result = coerce_to_target_type(NULL,
                               2017                 :          31927 :                                             (Node *) src_tle->expr,
                               2018                 :          31927 :                                             exprType((Node *) src_tle->expr),
                               2019                 :                :                                             res_type, res_typmod,
                               2020                 :                :                                             COERCION_ASSIGNMENT,
                               2021                 :                :                                             COERCE_IMPLICIT_CAST,
                               2022                 :                :                                             -1);
                               2023         [ +  + ]:          31927 :         if (cast_result == NULL)
                               2024                 :             36 :             return false;
 1461                          2025                 :          31891 :         assign_expr_collations(NULL, cast_result);
 1558                          2026                 :          31891 :         src_tle->expr = (Expr *) cast_result;
                               2027                 :                :         /* Make a Var referencing the possibly-modified TLE */
                               2028                 :          31891 :         new_tle_expr = (Expr *) makeVarFromTargetEntry(1, src_tle);
                               2029                 :                :     }
                               2030                 :                :     else
                               2031                 :                :     {
                               2032                 :                :         /* Any casting must happen in the upper tlist */
                               2033                 :            220 :         Var        *var = makeVarFromTargetEntry(1, src_tle);
                               2034                 :                : 
                               2035                 :            220 :         cast_result = coerce_to_target_type(NULL,
                               2036                 :                :                                             (Node *) var,
                               2037                 :                :                                             var->vartype,
                               2038                 :                :                                             res_type, res_typmod,
                               2039                 :                :                                             COERCION_ASSIGNMENT,
                               2040                 :                :                                             COERCE_IMPLICIT_CAST,
                               2041                 :                :                                             -1);
                               2042         [ -  + ]:            220 :         if (cast_result == NULL)
 1558 tgl@sss.pgh.pa.us        2043                 :UBC           0 :             return false;
 1461 tgl@sss.pgh.pa.us        2044                 :CBC         220 :         assign_expr_collations(NULL, cast_result);
                               2045                 :                :         /* Did the coercion actually do anything? */
 1558                          2046         [ +  + ]:            220 :         if (cast_result != (Node *) var)
                               2047                 :             42 :             *upper_tlist_nontrivial = true;
                               2048                 :            220 :         new_tle_expr = (Expr *) cast_result;
                               2049                 :                :     }
                               2050                 :          64222 :     new_tle = makeTargetEntry(new_tle_expr,
                               2051                 :          32111 :                               list_length(*upper_tlist) + 1,
                               2052                 :                :                               src_tle->resname, false);
                               2053                 :          32111 :     *upper_tlist = lappend(*upper_tlist, new_tle);
                               2054                 :          32111 :     return true;
                               2055                 :                : }
                               2056                 :                : 
                               2057                 :                : 
                               2058                 :                : /*
                               2059                 :                :  * CreateSQLFunctionDestReceiver -- create a suitable DestReceiver object
                               2060                 :                :  */
                               2061                 :                : DestReceiver *
 5644                          2062                 :          62489 : CreateSQLFunctionDestReceiver(void)
                               2063                 :                : {
                               2064                 :          62489 :     DR_sqlfunction *self = (DR_sqlfunction *) palloc0(sizeof(DR_sqlfunction));
                               2065                 :                : 
                               2066                 :          62489 :     self->pub.receiveSlot = sqlfunction_receive;
                               2067                 :          62489 :     self->pub.rStartup = sqlfunction_startup;
                               2068                 :          62489 :     self->pub.rShutdown = sqlfunction_shutdown;
                               2069                 :          62489 :     self->pub.rDestroy = sqlfunction_destroy;
                               2070                 :          62489 :     self->pub.mydest = DestSQLFunction;
                               2071                 :                : 
                               2072                 :                :     /* private fields will be set by postquel_start */
                               2073                 :                : 
                               2074                 :          62489 :     return (DestReceiver *) self;
                               2075                 :                : }
                               2076                 :                : 
                               2077                 :                : /*
                               2078                 :                :  * sqlfunction_startup --- executor startup
                               2079                 :                :  */
                               2080                 :                : static void
                               2081                 :          62969 : sqlfunction_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
                               2082                 :                : {
                               2083                 :                :     /* no-op */
                               2084                 :          62969 : }
                               2085                 :                : 
                               2086                 :                : /*
                               2087                 :                :  * sqlfunction_receive --- receive one tuple
                               2088                 :                :  */
                               2089                 :                : static bool
                               2090                 :         115913 : sqlfunction_receive(TupleTableSlot *slot, DestReceiver *self)
                               2091                 :                : {
                               2092                 :         115913 :     DR_sqlfunction *myState = (DR_sqlfunction *) self;
                               2093                 :                : 
                               2094                 :                :     /* Filter tuple as needed */
                               2095                 :         115913 :     slot = ExecFilterJunk(myState->filter, slot);
                               2096                 :                : 
                               2097                 :                :     /* Store the filtered tuple into the tuplestore */
                               2098                 :         115913 :     tuplestore_puttupleslot(myState->tstore, slot);
                               2099                 :                : 
 2869 rhaas@postgresql.org     2100                 :         115913 :     return true;
                               2101                 :                : }
                               2102                 :                : 
                               2103                 :                : /*
                               2104                 :                :  * sqlfunction_shutdown --- executor end
                               2105                 :                :  */
                               2106                 :                : static void
 5644 tgl@sss.pgh.pa.us        2107                 :          58687 : sqlfunction_shutdown(DestReceiver *self)
                               2108                 :                : {
                               2109                 :                :     /* no-op */
                               2110                 :          58687 : }
                               2111                 :                : 
                               2112                 :                : /*
                               2113                 :                :  * sqlfunction_destroy --- release DestReceiver object
                               2114                 :                :  */
                               2115                 :                : static void
                               2116                 :          58207 : sqlfunction_destroy(DestReceiver *self)
                               2117                 :                : {
                               2118                 :          58207 :     pfree(self);
                               2119                 :          58207 : }
        

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