LCOV - code coverage report
Current view: top level - src/backend/executor - functions.c (source / functions) Hit Total Coverage
Test: PostgreSQL 12beta2 Lines: 492 565 87.1 %
Date: 2019-06-19 14:06:47 Functions: 22 23 95.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13