]> granicus.if.org Git - postgresql/blob - src/backend/executor/functions.c
Clean up the API for DestReceiver objects by eliminating the assumption
[postgresql] / src / backend / executor / functions.c
1 /*-------------------------------------------------------------------------
2  *
3  * functions.c
4  *        Execution of SQL-language functions
5  *
6  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/executor/functions.c,v 1.130 2008/11/30 20:51:25 tgl Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include "access/xact.h"
18 #include "catalog/pg_proc.h"
19 #include "catalog/pg_type.h"
20 #include "commands/trigger.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 "tcop/utility.h"
28 #include "utils/builtins.h"
29 #include "utils/datum.h"
30 #include "utils/lsyscache.h"
31 #include "utils/snapmgr.h"
32 #include "utils/syscache.h"
33
34
35 /*
36  * Specialized DestReceiver for collecting query output in a SQL function
37  */
38 typedef struct
39 {
40         DestReceiver pub;                       /* publicly-known function pointers */
41         Tuplestorestate *tstore;        /* where to put result tuples */
42         MemoryContext cxt;                      /* context containing tstore */
43         JunkFilter *filter;                     /* filter to convert tuple type */
44 } DR_sqlfunction;
45
46 /*
47  * We have an execution_state record for each query in a function.      Each
48  * record contains a plantree for its query.  If the query is currently in
49  * F_EXEC_RUN state then there's a QueryDesc too.
50  */
51 typedef enum
52 {
53         F_EXEC_START, F_EXEC_RUN, F_EXEC_DONE
54 } ExecStatus;
55
56 typedef struct execution_state
57 {
58         struct execution_state *next;
59         ExecStatus      status;
60         bool            setsResult;             /* true if this query produces func's result */
61         bool            lazyEval;               /* true if should fetch one row at a time */
62         Node       *stmt;                       /* PlannedStmt or utility statement */
63         QueryDesc  *qd;                         /* null unless status == RUN */
64 } execution_state;
65
66
67 /*
68  * An SQLFunctionCache record is built during the first call,
69  * and linked to from the fn_extra field of the FmgrInfo struct.
70  *
71  * Note that currently this has only the lifespan of the calling query.
72  * Someday we might want to consider caching the parse/plan results longer
73  * than that.
74  */
75 typedef struct
76 {
77         char       *src;                        /* function body text (for error msgs) */
78
79         Oid                *argtypes;           /* resolved types of arguments */
80         Oid                     rettype;                /* actual return type */
81         int16           typlen;                 /* length of the return type */
82         bool            typbyval;               /* true if return type is pass by value */
83         bool            returnsSet;             /* true if returning multiple rows */
84         bool            returnsTuple;   /* true if returning whole tuple result */
85         bool            shutdown_reg;   /* true if registered shutdown callback */
86         bool            readonly_func;  /* true to run in "read only" mode */
87         bool            lazyEval;               /* true if using lazyEval for result query */
88
89         ParamListInfo paramLI;          /* Param list representing current args */
90
91         Tuplestorestate *tstore;        /* where we accumulate result tuples */
92
93         JunkFilter *junkFilter;         /* will be NULL if function returns VOID */
94
95         /* head of linked list of execution_state records */
96         execution_state *func_state;
97 } SQLFunctionCache;
98
99 typedef SQLFunctionCache *SQLFunctionCachePtr;
100
101
102 /* non-export function prototypes */
103 static execution_state *init_execution_state(List *queryTree_list,
104                                                                                          SQLFunctionCachePtr fcache,
105                                                                                          bool lazyEvalOK);
106 static void init_sql_fcache(FmgrInfo *finfo, bool lazyEvalOK);
107 static void postquel_start(execution_state *es, SQLFunctionCachePtr fcache);
108 static bool postquel_getnext(execution_state *es, SQLFunctionCachePtr fcache);
109 static void postquel_end(execution_state *es);
110 static void postquel_sub_params(SQLFunctionCachePtr fcache,
111                                         FunctionCallInfo fcinfo);
112 static Datum postquel_get_single_result(TupleTableSlot *slot,
113                                                    FunctionCallInfo fcinfo,
114                                                    SQLFunctionCachePtr fcache,
115                                                    MemoryContext resultcontext);
116 static void sql_exec_error_callback(void *arg);
117 static void ShutdownSQLFunction(Datum arg);
118 static void sqlfunction_startup(DestReceiver *self, int operation, TupleDesc typeinfo);
119 static void sqlfunction_receive(TupleTableSlot *slot, DestReceiver *self);
120 static void sqlfunction_shutdown(DestReceiver *self);
121 static void sqlfunction_destroy(DestReceiver *self);
122
123
124 /* Set up the list of per-query execution_state records for a SQL function */
125 static execution_state *
126 init_execution_state(List *queryTree_list,
127                                          SQLFunctionCachePtr fcache,
128                                          bool lazyEvalOK)
129 {
130         execution_state *firstes = NULL;
131         execution_state *preves = NULL;
132         execution_state *lasttages = NULL;
133         ListCell   *qtl_item;
134
135         foreach(qtl_item, queryTree_list)
136         {
137                 Query      *queryTree = (Query *) lfirst(qtl_item);
138                 Node       *stmt;
139                 execution_state *newes;
140
141                 Assert(IsA(queryTree, Query));
142
143                 if (queryTree->commandType == CMD_UTILITY)
144                         stmt = queryTree->utilityStmt;
145                 else
146                         stmt = (Node *) pg_plan_query(queryTree, 0, NULL);
147
148                 /* Precheck all commands for validity in a function */
149                 if (IsA(stmt, TransactionStmt))
150                         ereport(ERROR,
151                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
152                         /* translator: %s is a SQL statement name */
153                                          errmsg("%s is not allowed in a SQL function",
154                                                         CreateCommandTag(stmt))));
155
156                 if (fcache->readonly_func && !CommandIsReadOnly(stmt))
157                         ereport(ERROR,
158                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
159                         /* translator: %s is a SQL statement name */
160                                          errmsg("%s is not allowed in a non-volatile function",
161                                                         CreateCommandTag(stmt))));
162
163                 newes = (execution_state *) palloc(sizeof(execution_state));
164                 if (preves)
165                         preves->next = newes;
166                 else
167                         firstes = newes;
168
169                 newes->next = NULL;
170                 newes->status = F_EXEC_START;
171                 newes->setsResult = false;                      /* might change below */
172                 newes->lazyEval = false;                        /* might change below */
173                 newes->stmt = stmt;
174                 newes->qd = NULL;
175
176                 if (queryTree->canSetTag)
177                         lasttages = newes;
178
179                 preves = newes;
180         }
181
182         /*
183          * Mark the last canSetTag query as delivering the function result;
184          * then, if it is a plain SELECT, mark it for lazy evaluation.
185          * If it's not a SELECT we must always run it to completion.
186          *
187          * Note: at some point we might add additional criteria for whether to use
188          * lazy eval.  However, we should prefer to use it whenever the function
189          * doesn't return set, since fetching more than one row is useless in that
190          * case.
191          *
192          * Note: don't set setsResult if the function returns VOID, as evidenced
193          * by not having made a junkfilter.  This ensures we'll throw away any
194          * output from a utility statement that check_sql_fn_retval deemed to
195          * not have output.
196          */
197         if (lasttages && fcache->junkFilter)
198         {
199                 lasttages->setsResult = true;
200                 if (lazyEvalOK &&
201                         IsA(lasttages->stmt, PlannedStmt))
202                 {
203                         PlannedStmt *ps = (PlannedStmt *) lasttages->stmt;
204
205                         if (ps->commandType == CMD_SELECT &&
206                                 ps->utilityStmt == NULL &&
207                                 ps->intoClause == NULL)
208                                 fcache->lazyEval = lasttages->lazyEval = true;
209                 }
210         }
211
212         return firstes;
213 }
214
215 /* Initialize the SQLFunctionCache for a SQL function */
216 static void
217 init_sql_fcache(FmgrInfo *finfo, bool lazyEvalOK)
218 {
219         Oid                     foid = finfo->fn_oid;
220         Oid                     rettype;
221         HeapTuple       procedureTuple;
222         Form_pg_proc procedureStruct;
223         SQLFunctionCachePtr fcache;
224         Oid                *argOidVect;
225         int                     nargs;
226         List       *queryTree_list;
227         Datum           tmp;
228         bool            isNull;
229
230         fcache = (SQLFunctionCachePtr) palloc0(sizeof(SQLFunctionCache));
231
232         /*
233          * get the procedure tuple corresponding to the given function Oid
234          */
235         procedureTuple = SearchSysCache(PROCOID,
236                                                                         ObjectIdGetDatum(foid),
237                                                                         0, 0, 0);
238         if (!HeapTupleIsValid(procedureTuple))
239                 elog(ERROR, "cache lookup failed for function %u", foid);
240         procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
241
242         /*
243          * get the result type from the procedure tuple, and check for polymorphic
244          * result type; if so, find out the actual result type.
245          */
246         rettype = procedureStruct->prorettype;
247
248         if (IsPolymorphicType(rettype))
249         {
250                 rettype = get_fn_expr_rettype(finfo);
251                 if (rettype == InvalidOid)              /* this probably should not happen */
252                         ereport(ERROR,
253                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
254                                          errmsg("could not determine actual result type for function declared to return type %s",
255                                                         format_type_be(procedureStruct->prorettype))));
256         }
257
258         fcache->rettype = rettype;
259
260         /* Fetch the typlen and byval info for the result type */
261         get_typlenbyval(rettype, &fcache->typlen, &fcache->typbyval);
262
263         /* Remember whether we're returning setof something */
264         fcache->returnsSet = procedureStruct->proretset;
265
266         /* Remember if function is STABLE/IMMUTABLE */
267         fcache->readonly_func =
268                 (procedureStruct->provolatile != PROVOLATILE_VOLATILE);
269
270         /*
271          * We need the actual argument types to pass to the parser.
272          */
273         nargs = procedureStruct->pronargs;
274         if (nargs > 0)
275         {
276                 int                     argnum;
277
278                 argOidVect = (Oid *) palloc(nargs * sizeof(Oid));
279                 memcpy(argOidVect,
280                            procedureStruct->proargtypes.values,
281                            nargs * sizeof(Oid));
282                 /* Resolve any polymorphic argument types */
283                 for (argnum = 0; argnum < nargs; argnum++)
284                 {
285                         Oid                     argtype = argOidVect[argnum];
286
287                         if (IsPolymorphicType(argtype))
288                         {
289                                 argtype = get_fn_expr_argtype(finfo, argnum);
290                                 if (argtype == InvalidOid)
291                                         ereport(ERROR,
292                                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
293                                                          errmsg("could not determine actual type of argument declared %s",
294                                                                         format_type_be(argOidVect[argnum]))));
295                                 argOidVect[argnum] = argtype;
296                         }
297                 }
298         }
299         else
300                 argOidVect = NULL;
301         fcache->argtypes = argOidVect;
302
303         /*
304          * And of course we need the function body text.
305          */
306         tmp = SysCacheGetAttr(PROCOID,
307                                                   procedureTuple,
308                                                   Anum_pg_proc_prosrc,
309                                                   &isNull);
310         if (isNull)
311                 elog(ERROR, "null prosrc for function %u", foid);
312         fcache->src = TextDatumGetCString(tmp);
313
314         /*
315          * Parse and rewrite the queries in the function text.
316          */
317         queryTree_list = pg_parse_and_rewrite(fcache->src, argOidVect, nargs);
318
319         /*
320          * Check that the function returns the type it claims to.  Although in
321          * simple cases this was already done when the function was defined, we
322          * have to recheck because database objects used in the function's queries
323          * might have changed type.  We'd have to do it anyway if the function had
324          * any polymorphic arguments.
325          *
326          * Note: we set fcache->returnsTuple according to whether we are returning
327          * the whole tuple result or just a single column.      In the latter case we
328          * clear returnsTuple because we need not act different from the scalar
329          * result case, even if it's a rowtype column.  (However, we have to
330          * force lazy eval mode in that case; otherwise we'd need extra code to
331          * expand the rowtype column into multiple columns, since we have no
332          * way to notify the caller that it should do that.)
333          *
334          * check_sql_fn_retval will also construct a JunkFilter we can use to
335          * coerce the returned rowtype to the desired form (unless the result type
336          * is VOID, in which case there's nothing to coerce to).
337          */
338         fcache->returnsTuple = check_sql_fn_retval(foid,
339                                                                                            rettype,
340                                                                                            queryTree_list,
341                                                                                            false,
342                                                                                            &fcache->junkFilter);
343
344         if (fcache->returnsTuple)
345         {
346                 /* Make sure output rowtype is properly blessed */
347                 BlessTupleDesc(fcache->junkFilter->jf_resultSlot->tts_tupleDescriptor);
348         }
349         else if (fcache->returnsSet && type_is_rowtype(fcache->rettype))
350         {
351                 /*
352                  * Returning rowtype as if it were scalar --- materialize won't work.
353                  * Right now it's sufficient to override any caller preference for
354                  * materialize mode, but to add more smarts in init_execution_state
355                  * about this, we'd probably need a three-way flag instead of bool.
356                  */
357                 lazyEvalOK = true;
358         }
359
360         /* Finally, plan the queries */
361         fcache->func_state = init_execution_state(queryTree_list,
362                                                                                           fcache,
363                                                                                           lazyEvalOK);
364
365         ReleaseSysCache(procedureTuple);
366
367         finfo->fn_extra = (void *) fcache;
368 }
369
370 /* Start up execution of one execution_state node */
371 static void
372 postquel_start(execution_state *es, SQLFunctionCachePtr fcache)
373 {
374         Snapshot        snapshot;
375         DestReceiver *dest;
376
377         Assert(es->qd == NULL);
378
379         /*
380          * In a read-only function, use the surrounding query's snapshot;
381          * otherwise take a new snapshot for each query.  The snapshot should
382          * include a fresh command ID so that all work to date in this transaction
383          * is visible.
384          */
385         if (fcache->readonly_func)
386                 snapshot = GetActiveSnapshot();
387         else
388         {
389                 CommandCounterIncrement();
390                 snapshot = GetTransactionSnapshot();
391         }
392
393         /*
394          * If this query produces the function result, send its output to the
395          * tuplestore; else discard any output.
396          */
397         if (es->setsResult)
398         {
399                 DR_sqlfunction *myState;
400
401                 dest = CreateDestReceiver(DestSQLFunction);
402                 /* pass down the needed info to the dest receiver routines */
403                 myState = (DR_sqlfunction *) dest;
404                 Assert(myState->pub.mydest == DestSQLFunction);
405                 myState->tstore = fcache->tstore;
406                 myState->cxt = CurrentMemoryContext;
407                 myState->filter = fcache->junkFilter;
408         }
409         else
410                 dest = None_Receiver;
411
412         if (IsA(es->stmt, PlannedStmt))
413                 es->qd = CreateQueryDesc((PlannedStmt *) es->stmt,
414                                                                  snapshot, InvalidSnapshot,
415                                                                  dest,
416                                                                  fcache->paramLI, false);
417         else
418                 es->qd = CreateUtilityQueryDesc(es->stmt,
419                                                                                 snapshot,
420                                                                                 dest,
421                                                                                 fcache->paramLI);
422
423         /* We assume we don't need to set up ActiveSnapshot for ExecutorStart */
424
425         /* Utility commands don't need Executor. */
426         if (es->qd->utilitystmt == NULL)
427         {
428                 /*
429                  * Only set up to collect queued triggers if it's not a SELECT. This
430                  * isn't just an optimization, but is necessary in case a SELECT
431                  * returns multiple rows to caller --- we mustn't exit from the
432                  * function execution with a stacked AfterTrigger level still active.
433                  */
434                 if (es->qd->operation != CMD_SELECT)
435                         AfterTriggerBeginQuery();
436                 ExecutorStart(es->qd, 0);
437         }
438
439         es->status = F_EXEC_RUN;
440 }
441
442 /* Run one execution_state; either to completion or to first result row */
443 /* Returns true if we ran to completion */
444 static bool
445 postquel_getnext(execution_state *es, SQLFunctionCachePtr fcache)
446 {
447         bool            result;
448
449         /* Make our snapshot the active one for any called functions */
450         PushActiveSnapshot(es->qd->snapshot);
451
452         if (es->qd->utilitystmt)
453         {
454                 /* ProcessUtility needs the PlannedStmt for DECLARE CURSOR */
455                 ProcessUtility((es->qd->plannedstmt ?
456                                                 (Node *) es->qd->plannedstmt :
457                                                 es->qd->utilitystmt),
458                                            fcache->src,
459                                            es->qd->params,
460                                            false,               /* not top level */
461                                            es->qd->dest,
462                                            NULL);
463                 result = true;                  /* never stops early */
464         }
465         else
466         {
467                 /* Run regular commands to completion unless lazyEval */
468                 long            count = (es->lazyEval) ? 1L : 0L;
469
470                 ExecutorRun(es->qd, ForwardScanDirection, count);
471
472                 /*
473                  * If we requested run to completion OR there was no tuple returned,
474                  * command must be complete.
475                  */
476                 result = (count == 0L || es->qd->estate->es_processed == 0);
477         }
478
479         PopActiveSnapshot();
480
481         return result;
482 }
483
484 /* Shut down execution of one execution_state node */
485 static void
486 postquel_end(execution_state *es)
487 {
488         /* mark status done to ensure we don't do ExecutorEnd twice */
489         es->status = F_EXEC_DONE;
490
491         /* Utility commands don't need Executor. */
492         if (es->qd->utilitystmt == NULL)
493         {
494                 /* Make our snapshot the active one for any called functions */
495                 PushActiveSnapshot(es->qd->snapshot);
496
497                 if (es->qd->operation != CMD_SELECT)
498                         AfterTriggerEndQuery(es->qd->estate);
499                 ExecutorEnd(es->qd);
500
501                 PopActiveSnapshot();
502         }
503
504         (*es->qd->dest->rDestroy) (es->qd->dest);
505
506         FreeQueryDesc(es->qd);
507         es->qd = NULL;
508 }
509
510 /* Build ParamListInfo array representing current arguments */
511 static void
512 postquel_sub_params(SQLFunctionCachePtr fcache,
513                                         FunctionCallInfo fcinfo)
514 {
515         int                     nargs = fcinfo->nargs;
516
517         if (nargs > 0)
518         {
519                 ParamListInfo paramLI;
520                 int                     i;
521
522                 if (fcache->paramLI == NULL)
523                 {
524                         /* sizeof(ParamListInfoData) includes the first array element */
525                         paramLI = (ParamListInfo) palloc(sizeof(ParamListInfoData) +
526                                                                            (nargs - 1) *sizeof(ParamExternData));
527                         paramLI->numParams = nargs;
528                         fcache->paramLI = paramLI;
529                 }
530                 else
531                 {
532                         paramLI = fcache->paramLI;
533                         Assert(paramLI->numParams == nargs);
534                 }
535
536                 for (i = 0; i < nargs; i++)
537                 {
538                         ParamExternData *prm = &paramLI->params[i];
539
540                         prm->value = fcinfo->arg[i];
541                         prm->isnull = fcinfo->argnull[i];
542                         prm->pflags = 0;
543                         prm->ptype = fcache->argtypes[i];
544                 }
545         }
546         else
547                 fcache->paramLI = NULL;
548 }
549
550 /*
551  * Extract the SQL function's value from a single result row.  This is used
552  * both for scalar (non-set) functions and for each row of a lazy-eval set
553  * result.
554  */
555 static Datum
556 postquel_get_single_result(TupleTableSlot *slot,
557                                                    FunctionCallInfo fcinfo,
558                                                    SQLFunctionCachePtr fcache,
559                                                    MemoryContext resultcontext)
560 {
561         Datum           value;
562         MemoryContext oldcontext;
563
564         /*
565          * Set up to return the function value.  For pass-by-reference datatypes,
566          * be sure to allocate the result in resultcontext, not the current memory
567          * context (which has query lifespan).  We can't leave the data in the
568          * TupleTableSlot because we intend to clear the slot before returning.
569          */
570         oldcontext = MemoryContextSwitchTo(resultcontext);
571
572         if (fcache->returnsTuple)
573         {
574                 /* We must return the whole tuple as a Datum. */
575                 fcinfo->isnull = false;
576                 value = ExecFetchSlotTupleDatum(slot);
577                 value = datumCopy(value, fcache->typbyval, fcache->typlen);
578         }
579         else
580         {
581                 /*
582                  * Returning a scalar, which we have to extract from the first column
583                  * of the SELECT result, and then copy into result context if needed.
584                  */
585                 value = slot_getattr(slot, 1, &(fcinfo->isnull));
586
587                 if (!fcinfo->isnull)
588                         value = datumCopy(value, fcache->typbyval, fcache->typlen);
589         }
590
591         MemoryContextSwitchTo(oldcontext);
592
593         return value;
594 }
595
596 /*
597  * fmgr_sql: function call manager for SQL functions
598  */
599 Datum
600 fmgr_sql(PG_FUNCTION_ARGS)
601 {
602         MemoryContext oldcontext;
603         SQLFunctionCachePtr fcache;
604         ErrorContextCallback sqlerrcontext;
605         bool            randomAccess;
606         bool            lazyEvalOK;
607         execution_state *es;
608         TupleTableSlot *slot;
609         Datum           result;
610
611         /*
612          * Switch to context in which the fcache lives.  This ensures that
613          * parsetrees, plans, etc, will have sufficient lifetime.  The
614          * sub-executor is responsible for deleting per-tuple information.
615          */
616         oldcontext = MemoryContextSwitchTo(fcinfo->flinfo->fn_mcxt);
617
618         /*
619          * Setup error traceback support for ereport()
620          */
621         sqlerrcontext.callback = sql_exec_error_callback;
622         sqlerrcontext.arg = fcinfo->flinfo;
623         sqlerrcontext.previous = error_context_stack;
624         error_context_stack = &sqlerrcontext;
625
626         /* Check call context */
627         if (fcinfo->flinfo->fn_retset)
628         {
629                 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
630
631                 /*
632                  * For simplicity, we require callers to support both set eval modes.
633                  * There are cases where we must use one or must use the other, and
634                  * it's not really worthwhile to postpone the check till we know.
635                  */
636                 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
637                         (rsi->allowedModes & SFRM_ValuePerCall) == 0 ||
638                         (rsi->allowedModes & SFRM_Materialize) == 0 ||
639                         rsi->expectedDesc == NULL)
640                         ereport(ERROR,
641                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
642                                          errmsg("set-valued function called in context that cannot accept a set")));
643                 randomAccess = rsi->allowedModes & SFRM_Materialize_Random;
644                 lazyEvalOK = !(rsi->allowedModes & SFRM_Materialize_Preferred);
645         }
646         else
647         {
648                 randomAccess = false;
649                 lazyEvalOK = true;
650         }
651
652         /*
653          * Initialize fcache (build plans) if first time through.
654          */
655         fcache = (SQLFunctionCachePtr) fcinfo->flinfo->fn_extra;
656         if (fcache == NULL)
657         {
658                 init_sql_fcache(fcinfo->flinfo, lazyEvalOK);
659                 fcache = (SQLFunctionCachePtr) fcinfo->flinfo->fn_extra;
660         }
661         es = fcache->func_state;
662
663         /*
664          * Convert params to appropriate format if starting a fresh execution. (If
665          * continuing execution, we can re-use prior params.)
666          */
667         if (es && es->status == F_EXEC_START)
668                 postquel_sub_params(fcache, fcinfo);
669
670         /*
671          * Build tuplestore to hold results, if we don't have one already.
672          * Note it's in the query-lifespan context.
673          */
674         if (!fcache->tstore)
675                 fcache->tstore = tuplestore_begin_heap(randomAccess, false, work_mem);
676
677         /*
678          * Find first unfinished query in function.
679          */
680         while (es && es->status == F_EXEC_DONE)
681                 es = es->next;
682
683         /*
684          * Execute each command in the function one after another until we either
685          * run out of commands or get a result row from a lazily-evaluated SELECT.
686          */
687         while (es)
688         {
689                 bool    completed;
690
691                 if (es->status == F_EXEC_START)
692                         postquel_start(es, fcache);
693
694                 completed = postquel_getnext(es, fcache);
695
696                 /*
697                  * If we ran the command to completion, we can shut it down now.
698                  * Any row(s) we need to return are safely stashed in the tuplestore,
699                  * and we want to be sure that, for example, AFTER triggers get fired
700                  * before we return anything.  Also, if the function doesn't return
701                  * set, we can shut it down anyway because it must be a SELECT and
702                  * we don't care about fetching any more result rows.
703                  */
704                 if (completed || !fcache->returnsSet)
705                         postquel_end(es);
706
707                 /*
708                  * Break from loop if we didn't shut down (implying we got a
709                  * lazily-evaluated row).  Otherwise we'll press on till the
710                  * whole function is done, relying on the tuplestore to keep hold
711                  * of the data to eventually be returned.  This is necessary since
712                  * an INSERT/UPDATE/DELETE RETURNING that sets the result might be
713                  * followed by additional rule-inserted commands, and we want to
714                  * finish doing all those commands before we return anything.
715                  */
716                 if (es->status != F_EXEC_DONE)
717                         break;
718                 es = es->next;
719         }
720
721         /*
722          * The tuplestore now contains whatever row(s) we are supposed to return.
723          */
724         if (fcache->returnsSet)
725         {
726                 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
727
728                 if (es)
729                 {
730                         /*
731                          * If we stopped short of being done, we must have a lazy-eval row.
732                          */
733                         Assert(es->lazyEval);
734                         /* Re-use the junkfilter's output slot to fetch back the tuple */
735                         Assert(fcache->junkFilter);
736                         slot = fcache->junkFilter->jf_resultSlot;
737                         if (!tuplestore_gettupleslot(fcache->tstore, true, slot))
738                                 elog(ERROR, "failed to fetch lazy-eval tuple");
739                         /* Extract the result as a datum, and copy out from the slot */
740                         result = postquel_get_single_result(slot, fcinfo,
741                                                                                                 fcache, oldcontext);
742                         /* Clear the tuplestore, but keep it for next time */
743                         /* NB: this might delete the slot's content, but we don't care */
744                         tuplestore_clear(fcache->tstore);
745
746                         /*
747                          * Let caller know we're not finished.
748                          */
749                         rsi->isDone = ExprMultipleResult;
750
751                         /*
752                          * Ensure we will get shut down cleanly if the exprcontext is not
753                          * run to completion.
754                          */
755                         if (!fcache->shutdown_reg)
756                         {
757                                 RegisterExprContextCallback(rsi->econtext,
758                                                                                         ShutdownSQLFunction,
759                                                                                         PointerGetDatum(fcache));
760                                 fcache->shutdown_reg = true;
761                         }
762                 }
763                 else if (fcache->lazyEval)
764                 {
765                         /*
766                          * We are done with a lazy evaluation.  Clean up.
767                          */
768                         tuplestore_clear(fcache->tstore);
769
770                         /*
771                          * Let caller know we're finished.
772                          */
773                         rsi->isDone = ExprEndResult;
774
775                         fcinfo->isnull = true;
776                         result = (Datum) 0;
777
778                         /* Deregister shutdown callback, if we made one */
779                         if (fcache->shutdown_reg)
780                         {
781                                 UnregisterExprContextCallback(rsi->econtext,
782                                                                                           ShutdownSQLFunction,
783                                                                                           PointerGetDatum(fcache));
784                                 fcache->shutdown_reg = false;
785                         }
786                 }
787                 else
788                 {
789                         /*
790                          * We are done with a non-lazy evaluation.  Return whatever is
791                          * in the tuplestore.  (It is now caller's responsibility to
792                          * free the tuplestore when done.)
793                          */
794                         rsi->returnMode = SFRM_Materialize;
795                         rsi->setResult = fcache->tstore;
796                         fcache->tstore = NULL;
797                         /* must copy desc because execQual will free it */
798                         if (fcache->junkFilter)
799                                 rsi->setDesc = CreateTupleDescCopy(fcache->junkFilter->jf_cleanTupType);
800
801                         fcinfo->isnull = true;
802                         result = (Datum) 0;
803
804                         /* Deregister shutdown callback, if we made one */
805                         if (fcache->shutdown_reg)
806                         {
807                                 UnregisterExprContextCallback(rsi->econtext,
808                                                                                           ShutdownSQLFunction,
809                                                                                           PointerGetDatum(fcache));
810                                 fcache->shutdown_reg = false;
811                         }
812                 }
813         }
814         else
815         {
816                 /*
817                  * Non-set function.  If we got a row, return it; else return NULL.
818                  */
819                 if (fcache->junkFilter)
820                 {
821                         /* Re-use the junkfilter's output slot to fetch back the tuple */
822                         slot = fcache->junkFilter->jf_resultSlot;
823                         if (tuplestore_gettupleslot(fcache->tstore, true, slot))
824                                 result = postquel_get_single_result(slot, fcinfo,
825                                                                                                         fcache, oldcontext);
826                         else
827                         {
828                                 fcinfo->isnull = true;
829                                 result = (Datum) 0;
830                         }
831                 }
832                 else
833                 {
834                         /* Should only get here for VOID functions */
835                         Assert(fcache->rettype == VOIDOID);
836                         fcinfo->isnull = true;
837                         result = (Datum) 0;
838                 }
839
840                 /* Clear the tuplestore, but keep it for next time */
841                 tuplestore_clear(fcache->tstore);
842         }
843
844         /*
845          * If we've gone through every command in the function, we are done.
846          * Reset the execution states to start over again on next call.
847          */
848         if (es == NULL)
849         {
850                 es = fcache->func_state;
851                 while (es)
852                 {
853                         es->status = F_EXEC_START;
854                         es = es->next;
855                 }
856         }
857
858         error_context_stack = sqlerrcontext.previous;
859
860         MemoryContextSwitchTo(oldcontext);
861
862         return result;
863 }
864
865
866 /*
867  * error context callback to let us supply a call-stack traceback
868  */
869 static void
870 sql_exec_error_callback(void *arg)
871 {
872         FmgrInfo   *flinfo = (FmgrInfo *) arg;
873         SQLFunctionCachePtr fcache = (SQLFunctionCachePtr) flinfo->fn_extra;
874         HeapTuple       func_tuple;
875         Form_pg_proc functup;
876         char       *fn_name;
877         int                     syntaxerrposition;
878
879         /* Need access to function's pg_proc tuple */
880         func_tuple = SearchSysCache(PROCOID,
881                                                                 ObjectIdGetDatum(flinfo->fn_oid),
882                                                                 0, 0, 0);
883         if (!HeapTupleIsValid(func_tuple))
884                 return;                                 /* shouldn't happen */
885         functup = (Form_pg_proc) GETSTRUCT(func_tuple);
886         fn_name = NameStr(functup->proname);
887
888         /*
889          * If there is a syntax error position, convert to internal syntax error
890          */
891         syntaxerrposition = geterrposition();
892         if (syntaxerrposition > 0)
893         {
894                 bool            isnull;
895                 Datum           tmp;
896                 char       *prosrc;
897
898                 tmp = SysCacheGetAttr(PROCOID, func_tuple, Anum_pg_proc_prosrc,
899                                                           &isnull);
900                 if (isnull)
901                         elog(ERROR, "null prosrc");
902                 prosrc = TextDatumGetCString(tmp);
903                 errposition(0);
904                 internalerrposition(syntaxerrposition);
905                 internalerrquery(prosrc);
906                 pfree(prosrc);
907         }
908
909         /*
910          * Try to determine where in the function we failed.  If there is a query
911          * with non-null QueryDesc, finger it.  (We check this rather than looking
912          * for F_EXEC_RUN state, so that errors during ExecutorStart or
913          * ExecutorEnd are blamed on the appropriate query; see postquel_start and
914          * postquel_end.)
915          */
916         if (fcache)
917         {
918                 execution_state *es;
919                 int                     query_num;
920
921                 es = fcache->func_state;
922                 query_num = 1;
923                 while (es)
924                 {
925                         if (es->qd)
926                         {
927                                 errcontext("SQL function \"%s\" statement %d",
928                                                    fn_name, query_num);
929                                 break;
930                         }
931                         es = es->next;
932                         query_num++;
933                 }
934                 if (es == NULL)
935                 {
936                         /*
937                          * couldn't identify a running query; might be function entry,
938                          * function exit, or between queries.
939                          */
940                         errcontext("SQL function \"%s\"", fn_name);
941                 }
942         }
943         else
944         {
945                 /* must have failed during init_sql_fcache() */
946                 errcontext("SQL function \"%s\" during startup", fn_name);
947         }
948
949         ReleaseSysCache(func_tuple);
950 }
951
952
953 /*
954  * callback function in case a function-returning-set needs to be shut down
955  * before it has been run to completion
956  */
957 static void
958 ShutdownSQLFunction(Datum arg)
959 {
960         SQLFunctionCachePtr fcache = (SQLFunctionCachePtr) DatumGetPointer(arg);
961         execution_state *es = fcache->func_state;
962
963         while (es != NULL)
964         {
965                 /* Shut down anything still running */
966                 if (es->status == F_EXEC_RUN)
967                         postquel_end(es);
968                 /* Reset states to START in case we're called again */
969                 es->status = F_EXEC_START;
970                 es = es->next;
971         }
972
973         /* Release tuplestore if we have one */
974         if (fcache->tstore)
975                 tuplestore_end(fcache->tstore);
976         fcache->tstore = NULL;
977
978         /* execUtils will deregister the callback... */
979         fcache->shutdown_reg = false;
980 }
981
982
983 /*
984  * check_sql_fn_retval() -- check return value of a list of sql parse trees.
985  *
986  * The return value of a sql function is the value returned by the last
987  * canSetTag query in the function.  We do some ad-hoc type checking here
988  * to be sure that the user is returning the type he claims.  There are
989  * also a couple of strange-looking features to assist callers in dealing
990  * with allowed special cases, such as binary-compatible result types.
991  *
992  * For a polymorphic function the passed rettype must be the actual resolved
993  * output type of the function; we should never see a polymorphic pseudotype
994  * such as ANYELEMENT as rettype.  (This means we can't check the type during
995  * function definition of a polymorphic function.)
996  *
997  * This function returns true if the sql function returns the entire tuple
998  * result of its final statement, and false otherwise.  Note that because we
999  * allow "SELECT rowtype_expression", this may be false even when the declared
1000  * function return type is a rowtype.
1001  *
1002  * If insertRelabels is true, then binary-compatible cases are dealt with
1003  * by actually inserting RelabelType nodes into the output targetlist;
1004  * obviously the caller must pass a parsetree that it's okay to modify in this
1005  * case.
1006  *
1007  * If junkFilter isn't NULL, then *junkFilter is set to a JunkFilter defined
1008  * to convert the function's tuple result to the correct output tuple type.
1009  * Exception: if the function is defined to return VOID then *junkFilter is
1010  * set to NULL.
1011  */
1012 bool
1013 check_sql_fn_retval(Oid func_id, Oid rettype, List *queryTreeList,
1014                                         bool insertRelabels,
1015                                         JunkFilter **junkFilter)
1016 {
1017         Query      *parse;
1018         List       *tlist;
1019         int                     tlistlen;
1020         char            fn_typtype;
1021         Oid                     restype;
1022         ListCell   *lc;
1023
1024         AssertArg(!IsPolymorphicType(rettype));
1025
1026         if (junkFilter)
1027                 *junkFilter = NULL;             /* initialize in case of VOID result */
1028
1029         /*
1030          * Find the last canSetTag query in the list.  This isn't necessarily
1031          * the last parsetree, because rule rewriting can insert queries after
1032          * what the user wrote.
1033          */
1034         parse = NULL;
1035         foreach(lc, queryTreeList)
1036         {
1037                 Query  *q = (Query *) lfirst(lc);
1038
1039                 if (q->canSetTag)
1040                         parse = q;
1041         }
1042
1043         /*
1044          * If it's a plain SELECT, it returns whatever the targetlist says.
1045          * Otherwise, if it's INSERT/UPDATE/DELETE with RETURNING, it returns that.
1046          * Otherwise, the function return type must be VOID.
1047          *
1048          * Note: eventually replace this test with QueryReturnsTuples?  We'd need
1049          * a more general method of determining the output type, though.  Also,
1050          * it seems too dangerous to consider FETCH or EXECUTE as returning a
1051          * determinable rowtype, since they depend on relatively short-lived
1052          * entities.
1053          */
1054         if (parse &&
1055                 parse->commandType == CMD_SELECT &&
1056                 parse->utilityStmt == NULL &&
1057                 parse->intoClause == NULL)
1058         {
1059                 tlist = parse->targetList;
1060         }
1061         else if (parse &&
1062                          (parse->commandType == CMD_INSERT ||
1063                           parse->commandType == CMD_UPDATE ||
1064                           parse->commandType == CMD_DELETE) &&
1065                          parse->returningList)
1066         {
1067                 tlist = parse->returningList;
1068         }
1069         else
1070         {
1071                 /* Empty function body, or last statement is a utility command */
1072                 if (rettype != VOIDOID)
1073                         ereport(ERROR,
1074                                         (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1075                          errmsg("return type mismatch in function declared to return %s",
1076                                         format_type_be(rettype)),
1077                                  errdetail("Function's final statement must be SELECT or INSERT/UPDATE/DELETE RETURNING.")));
1078                 return false;
1079         }
1080
1081         /*
1082          * OK, check that the targetlist returns something matching the declared
1083          * type.  (We used to insist that the declared type not be VOID in this
1084          * case, but that makes it hard to write a void function that exits after
1085          * calling another void function.  Instead, we insist that the tlist
1086          * return void ... so void is treated as if it were a scalar type below.)
1087          */
1088
1089         /*
1090          * Count the non-junk entries in the result targetlist.
1091          */
1092         tlistlen = ExecCleanTargetListLength(tlist);
1093
1094         fn_typtype = get_typtype(rettype);
1095
1096         if (fn_typtype == TYPTYPE_BASE ||
1097                 fn_typtype == TYPTYPE_DOMAIN ||
1098                 fn_typtype == TYPTYPE_ENUM ||
1099                 rettype == VOIDOID)
1100         {
1101                 /*
1102                  * For scalar-type returns, the target list must have exactly one
1103                  * non-junk entry, and its type must agree with what the user
1104                  * declared; except we allow binary-compatible types too.
1105                  */
1106                 TargetEntry *tle;
1107
1108                 if (tlistlen != 1)
1109                         ereport(ERROR,
1110                                         (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1111                          errmsg("return type mismatch in function declared to return %s",
1112                                         format_type_be(rettype)),
1113                                  errdetail("Final statement must return exactly one column.")));
1114
1115                 /* We assume here that non-junk TLEs must come first in tlists */
1116                 tle = (TargetEntry *) linitial(tlist);
1117                 Assert(!tle->resjunk);
1118
1119                 restype = exprType((Node *) tle->expr);
1120                 if (!IsBinaryCoercible(restype, rettype))
1121                         ereport(ERROR,
1122                                         (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1123                          errmsg("return type mismatch in function declared to return %s",
1124                                         format_type_be(rettype)),
1125                                          errdetail("Actual return type is %s.",
1126                                                            format_type_be(restype))));
1127                 if (insertRelabels && restype != rettype)
1128                         tle->expr = (Expr *) makeRelabelType(tle->expr,
1129                                                                                                  rettype,
1130                                                                                                  -1,
1131                                                                                                  COERCE_DONTCARE);
1132
1133                 /* Set up junk filter if needed */
1134                 if (junkFilter)
1135                         *junkFilter = ExecInitJunkFilter(tlist, false, NULL);
1136         }
1137         else if (fn_typtype == TYPTYPE_COMPOSITE || rettype == RECORDOID)
1138         {
1139                 /* Returns a rowtype */
1140                 TupleDesc       tupdesc;
1141                 int                     tupnatts;       /* physical number of columns in tuple */
1142                 int                     tuplogcols; /* # of nondeleted columns in tuple */
1143                 int                     colindex;       /* physical column index */
1144
1145                 /*
1146                  * If the target list is of length 1, and the type of the varnode in
1147                  * the target list matches the declared return type, this is okay.
1148                  * This can happen, for example, where the body of the function is
1149                  * 'SELECT func2()', where func2 has the same composite return type
1150                  * as the function that's calling it.
1151                  */
1152                 if (tlistlen == 1)
1153                 {
1154                         TargetEntry *tle = (TargetEntry *) linitial(tlist);
1155
1156                         Assert(!tle->resjunk);
1157                         restype = exprType((Node *) tle->expr);
1158                         if (IsBinaryCoercible(restype, rettype))
1159                         {
1160                                 if (insertRelabels && restype != rettype)
1161                                         tle->expr = (Expr *) makeRelabelType(tle->expr,
1162                                                                                                                  rettype,
1163                                                                                                                  -1,
1164                                                                                                                  COERCE_DONTCARE);
1165                                 /* Set up junk filter if needed */
1166                                 if (junkFilter)
1167                                         *junkFilter = ExecInitJunkFilter(tlist, false, NULL);
1168                                 return false;   /* NOT returning whole tuple */
1169                         }
1170                 }
1171
1172                 /* Is the rowtype fixed, or determined only at runtime? */
1173                 if (get_func_result_type(func_id, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1174                 {
1175                         /*
1176                          * Assume we are returning the whole tuple. Crosschecking against
1177                          * what the caller expects will happen at runtime.
1178                          */
1179                         if (junkFilter)
1180                                 *junkFilter = ExecInitJunkFilter(tlist, false, NULL);
1181                         return true;
1182                 }
1183                 Assert(tupdesc);
1184
1185                 /*
1186                  * Verify that the targetlist matches the return tuple type. We scan
1187                  * the non-deleted attributes to ensure that they match the datatypes
1188                  * of the non-resjunk columns.
1189                  */
1190                 tupnatts = tupdesc->natts;
1191                 tuplogcols = 0;                 /* we'll count nondeleted cols as we go */
1192                 colindex = 0;
1193
1194                 foreach(lc, tlist)
1195                 {
1196                         TargetEntry *tle = (TargetEntry *) lfirst(lc);
1197                         Form_pg_attribute attr;
1198                         Oid                     tletype;
1199                         Oid                     atttype;
1200
1201                         if (tle->resjunk)
1202                                 continue;
1203
1204                         do
1205                         {
1206                                 colindex++;
1207                                 if (colindex > tupnatts)
1208                                         ereport(ERROR,
1209                                                         (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1210                                                          errmsg("return type mismatch in function declared to return %s",
1211                                                                         format_type_be(rettype)),
1212                                            errdetail("Final statement returns too many columns.")));
1213                                 attr = tupdesc->attrs[colindex - 1];
1214                         } while (attr->attisdropped);
1215                         tuplogcols++;
1216
1217                         tletype = exprType((Node *) tle->expr);
1218                         atttype = attr->atttypid;
1219                         if (!IsBinaryCoercible(tletype, atttype))
1220                                 ereport(ERROR,
1221                                                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1222                                                  errmsg("return type mismatch in function declared to return %s",
1223                                                                 format_type_be(rettype)),
1224                                                  errdetail("Final statement returns %s instead of %s at column %d.",
1225                                                                    format_type_be(tletype),
1226                                                                    format_type_be(atttype),
1227                                                                    tuplogcols)));
1228                         if (insertRelabels && tletype != atttype)
1229                                 tle->expr = (Expr *) makeRelabelType(tle->expr,
1230                                                                                                          atttype,
1231                                                                                                          -1,
1232                                                                                                          COERCE_DONTCARE);
1233                 }
1234
1235                 for (;;)
1236                 {
1237                         colindex++;
1238                         if (colindex > tupnatts)
1239                                 break;
1240                         if (!tupdesc->attrs[colindex - 1]->attisdropped)
1241                                 tuplogcols++;
1242                 }
1243
1244                 if (tlistlen != tuplogcols)
1245                         ereport(ERROR,
1246                                         (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1247                          errmsg("return type mismatch in function declared to return %s",
1248                                         format_type_be(rettype)),
1249                                          errdetail("Final statement returns too few columns.")));
1250
1251                 /* Set up junk filter if needed */
1252                 if (junkFilter)
1253                         *junkFilter = ExecInitJunkFilterConversion(tlist,
1254                                                                                                 CreateTupleDescCopy(tupdesc),
1255                                                                                                            NULL);
1256
1257                 /* Report that we are returning entire tuple result */
1258                 return true;
1259         }
1260         else
1261                 ereport(ERROR,
1262                                 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1263                                  errmsg("return type %s is not supported for SQL functions",
1264                                                 format_type_be(rettype))));
1265
1266         return false;
1267 }
1268
1269
1270 /*
1271  * CreateSQLFunctionDestReceiver -- create a suitable DestReceiver object
1272  */
1273 DestReceiver *
1274 CreateSQLFunctionDestReceiver(void)
1275 {
1276         DR_sqlfunction *self = (DR_sqlfunction *) palloc0(sizeof(DR_sqlfunction));
1277
1278         self->pub.receiveSlot = sqlfunction_receive;
1279         self->pub.rStartup = sqlfunction_startup;
1280         self->pub.rShutdown = sqlfunction_shutdown;
1281         self->pub.rDestroy = sqlfunction_destroy;
1282         self->pub.mydest = DestSQLFunction;
1283
1284         /* private fields will be set by postquel_start */
1285
1286         return (DestReceiver *) self;
1287 }
1288
1289 /*
1290  * sqlfunction_startup --- executor startup
1291  */
1292 static void
1293 sqlfunction_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
1294 {
1295         /* no-op */
1296 }
1297
1298 /*
1299  * sqlfunction_receive --- receive one tuple
1300  */
1301 static void
1302 sqlfunction_receive(TupleTableSlot *slot, DestReceiver *self)
1303 {
1304         DR_sqlfunction *myState = (DR_sqlfunction *) self;
1305         MemoryContext oldcxt;
1306
1307         /* Filter tuple as needed */
1308         slot = ExecFilterJunk(myState->filter, slot);
1309
1310         /* Store the filtered tuple into the tuplestore */
1311         oldcxt = MemoryContextSwitchTo(myState->cxt);
1312         tuplestore_puttupleslot(myState->tstore, slot);
1313         MemoryContextSwitchTo(oldcxt);
1314 }
1315
1316 /*
1317  * sqlfunction_shutdown --- executor end
1318  */
1319 static void
1320 sqlfunction_shutdown(DestReceiver *self)
1321 {
1322         /* no-op */
1323 }
1324
1325 /*
1326  * sqlfunction_destroy --- release DestReceiver object
1327  */
1328 static void
1329 sqlfunction_destroy(DestReceiver *self)
1330 {
1331         pfree(self);
1332 }