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