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