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