1 /*-------------------------------------------------------------------------
4 * Execution of SQL-language functions
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/executor/functions.c,v 1.137 2009/12/14 02:15:51 tgl Exp $
13 *-------------------------------------------------------------------------
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"
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"
36 * Specialized DestReceiver for collecting query output in a SQL function
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 */
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.
53 F_EXEC_START, F_EXEC_RUN, F_EXEC_DONE
56 typedef struct execution_state
58 struct execution_state *next;
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 */
68 * An SQLFunctionCache record is built during the first call,
69 * and linked to from the fn_extra field of the FmgrInfo struct.
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
77 char *src; /* function body text (for error msgs) */
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 */
89 ParamListInfo paramLI; /* Param list representing current args */
91 Tuplestorestate *tstore; /* where we accumulate result tuples */
93 JunkFilter *junkFilter; /* will be NULL if function returns VOID */
95 /* head of linked list of execution_state records */
96 execution_state *func_state;
99 typedef SQLFunctionCache *SQLFunctionCachePtr;
102 /* non-export function prototypes */
103 static execution_state *init_execution_state(List *queryTree_list,
104 SQLFunctionCachePtr fcache,
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);
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,
130 execution_state *firstes = NULL;
131 execution_state *preves = NULL;
132 execution_state *lasttages = NULL;
135 foreach(qtl_item, queryTree_list)
137 Query *queryTree = (Query *) lfirst(qtl_item);
139 execution_state *newes;
141 Assert(IsA(queryTree, Query));
143 if (queryTree->commandType == CMD_UTILITY)
144 stmt = queryTree->utilityStmt;
146 stmt = (Node *) pg_plan_query(queryTree, 0, NULL);
148 /* Precheck all commands for validity in a function */
149 if (IsA(stmt, TransactionStmt))
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))));
156 if (fcache->readonly_func && !CommandIsReadOnly(stmt))
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))));
163 newes = (execution_state *) palloc(sizeof(execution_state));
165 preves->next = newes;
170 newes->status = F_EXEC_START;
171 newes->setsResult = false; /* might change below */
172 newes->lazyEval = false; /* might change below */
176 if (queryTree->canSetTag)
183 * Mark the last canSetTag query as delivering the function result; then,
184 * if it is a plain SELECT, mark it for lazy evaluation. If it's not a
185 * SELECT we must always run it to completion.
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
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 not
197 if (lasttages && fcache->junkFilter)
199 lasttages->setsResult = true;
201 IsA(lasttages->stmt, PlannedStmt))
203 PlannedStmt *ps = (PlannedStmt *) lasttages->stmt;
205 if (ps->commandType == CMD_SELECT &&
206 ps->utilityStmt == NULL &&
207 ps->intoClause == NULL)
208 fcache->lazyEval = lasttages->lazyEval = true;
215 /* Initialize the SQLFunctionCache for a SQL function */
217 init_sql_fcache(FmgrInfo *finfo, bool lazyEvalOK)
219 Oid foid = finfo->fn_oid;
221 HeapTuple procedureTuple;
222 Form_pg_proc procedureStruct;
223 SQLFunctionCachePtr fcache;
226 List *queryTree_list;
230 fcache = (SQLFunctionCachePtr) palloc0(sizeof(SQLFunctionCache));
233 * get the procedure tuple corresponding to the given function Oid
235 procedureTuple = SearchSysCache(PROCOID,
236 ObjectIdGetDatum(foid),
238 if (!HeapTupleIsValid(procedureTuple))
239 elog(ERROR, "cache lookup failed for function %u", foid);
240 procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
243 * get the result type from the procedure tuple, and check for polymorphic
244 * result type; if so, find out the actual result type.
246 rettype = procedureStruct->prorettype;
248 if (IsPolymorphicType(rettype))
250 rettype = get_fn_expr_rettype(finfo);
251 if (rettype == InvalidOid) /* this probably should not happen */
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))));
258 fcache->rettype = rettype;
260 /* Fetch the typlen and byval info for the result type */
261 get_typlenbyval(rettype, &fcache->typlen, &fcache->typbyval);
263 /* Remember whether we're returning setof something */
264 fcache->returnsSet = procedureStruct->proretset;
266 /* Remember if function is STABLE/IMMUTABLE */
267 fcache->readonly_func =
268 (procedureStruct->provolatile != PROVOLATILE_VOLATILE);
271 * We need the actual argument types to pass to the parser.
273 nargs = procedureStruct->pronargs;
278 argOidVect = (Oid *) palloc(nargs * sizeof(Oid));
280 procedureStruct->proargtypes.values,
281 nargs * sizeof(Oid));
282 /* Resolve any polymorphic argument types */
283 for (argnum = 0; argnum < nargs; argnum++)
285 Oid argtype = argOidVect[argnum];
287 if (IsPolymorphicType(argtype))
289 argtype = get_fn_expr_argtype(finfo, argnum);
290 if (argtype == InvalidOid)
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;
301 fcache->argtypes = argOidVect;
304 * And of course we need the function body text.
306 tmp = SysCacheGetAttr(PROCOID,
311 elog(ERROR, "null prosrc for function %u", foid);
312 fcache->src = TextDatumGetCString(tmp);
315 * Parse and rewrite the queries in the function text.
317 queryTree_list = pg_parse_and_rewrite(fcache->src, argOidVect, nargs);
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.
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 force
330 * lazy eval mode in that case; otherwise we'd need extra code to expand
331 * the rowtype column into multiple columns, since we have no way to
332 * notify the caller that it should do that.)
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).
338 fcache->returnsTuple = check_sql_fn_retval(foid,
342 &fcache->junkFilter);
344 if (fcache->returnsTuple)
346 /* Make sure output rowtype is properly blessed */
347 BlessTupleDesc(fcache->junkFilter->jf_resultSlot->tts_tupleDescriptor);
349 else if (fcache->returnsSet && type_is_rowtype(fcache->rettype))
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.
360 /* Finally, plan the queries */
361 fcache->func_state = init_execution_state(queryTree_list,
365 ReleaseSysCache(procedureTuple);
367 finfo->fn_extra = (void *) fcache;
370 /* Start up execution of one execution_state node */
372 postquel_start(execution_state *es, SQLFunctionCachePtr fcache)
377 Assert(es->qd == NULL);
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
385 if (fcache->readonly_func)
386 snapshot = GetActiveSnapshot();
389 CommandCounterIncrement();
390 snapshot = GetTransactionSnapshot();
394 * If this query produces the function result, send its output to the
395 * tuplestore; else discard any output.
399 DR_sqlfunction *myState;
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;
410 dest = None_Receiver;
412 if (IsA(es->stmt, PlannedStmt))
413 es->qd = CreateQueryDesc((PlannedStmt *) es->stmt,
415 snapshot, InvalidSnapshot,
417 fcache->paramLI, false);
419 es->qd = CreateUtilityQueryDesc(es->stmt,
425 /* We assume we don't need to set up ActiveSnapshot for ExecutorStart */
427 /* Utility commands don't need Executor. */
428 if (es->qd->utilitystmt == NULL)
431 * Only set up to collect queued triggers if it's not a SELECT. This
432 * isn't just an optimization, but is necessary in case a SELECT
433 * returns multiple rows to caller --- we mustn't exit from the
434 * function execution with a stacked AfterTrigger level still active.
436 if (es->qd->operation != CMD_SELECT)
437 AfterTriggerBeginQuery();
438 ExecutorStart(es->qd, 0);
441 es->status = F_EXEC_RUN;
444 /* Run one execution_state; either to completion or to first result row */
445 /* Returns true if we ran to completion */
447 postquel_getnext(execution_state *es, SQLFunctionCachePtr fcache)
451 /* Make our snapshot the active one for any called functions */
452 PushActiveSnapshot(es->qd->snapshot);
454 if (es->qd->utilitystmt)
456 /* ProcessUtility needs the PlannedStmt for DECLARE CURSOR */
457 ProcessUtility((es->qd->plannedstmt ?
458 (Node *) es->qd->plannedstmt :
459 es->qd->utilitystmt),
462 false, /* not top level */
465 result = true; /* never stops early */
469 /* Run regular commands to completion unless lazyEval */
470 long count = (es->lazyEval) ? 1L : 0L;
472 ExecutorRun(es->qd, ForwardScanDirection, count);
475 * If we requested run to completion OR there was no tuple returned,
476 * command must be complete.
478 result = (count == 0L || es->qd->estate->es_processed == 0);
486 /* Shut down execution of one execution_state node */
488 postquel_end(execution_state *es)
490 /* mark status done to ensure we don't do ExecutorEnd twice */
491 es->status = F_EXEC_DONE;
493 /* Utility commands don't need Executor. */
494 if (es->qd->utilitystmt == NULL)
496 /* Make our snapshot the active one for any called functions */
497 PushActiveSnapshot(es->qd->snapshot);
499 if (es->qd->operation != CMD_SELECT)
500 AfterTriggerEndQuery(es->qd->estate);
506 (*es->qd->dest->rDestroy) (es->qd->dest);
508 FreeQueryDesc(es->qd);
512 /* Build ParamListInfo array representing current arguments */
514 postquel_sub_params(SQLFunctionCachePtr fcache,
515 FunctionCallInfo fcinfo)
517 int nargs = fcinfo->nargs;
521 ParamListInfo paramLI;
524 if (fcache->paramLI == NULL)
526 /* sizeof(ParamListInfoData) includes the first array element */
527 paramLI = (ParamListInfo) palloc(sizeof(ParamListInfoData) +
528 (nargs - 1) *sizeof(ParamExternData));
529 /* we have static list of params, so no hooks needed */
530 paramLI->paramFetch = NULL;
531 paramLI->paramFetchArg = NULL;
532 paramLI->parserSetup = NULL;
533 paramLI->parserSetupArg = NULL;
534 paramLI->numParams = nargs;
535 fcache->paramLI = paramLI;
539 paramLI = fcache->paramLI;
540 Assert(paramLI->numParams == nargs);
543 for (i = 0; i < nargs; i++)
545 ParamExternData *prm = ¶mLI->params[i];
547 prm->value = fcinfo->arg[i];
548 prm->isnull = fcinfo->argnull[i];
550 prm->ptype = fcache->argtypes[i];
554 fcache->paramLI = NULL;
558 * Extract the SQL function's value from a single result row. This is used
559 * both for scalar (non-set) functions and for each row of a lazy-eval set
563 postquel_get_single_result(TupleTableSlot *slot,
564 FunctionCallInfo fcinfo,
565 SQLFunctionCachePtr fcache,
566 MemoryContext resultcontext)
569 MemoryContext oldcontext;
572 * Set up to return the function value. For pass-by-reference datatypes,
573 * be sure to allocate the result in resultcontext, not the current memory
574 * context (which has query lifespan). We can't leave the data in the
575 * TupleTableSlot because we intend to clear the slot before returning.
577 oldcontext = MemoryContextSwitchTo(resultcontext);
579 if (fcache->returnsTuple)
581 /* We must return the whole tuple as a Datum. */
582 fcinfo->isnull = false;
583 value = ExecFetchSlotTupleDatum(slot);
584 value = datumCopy(value, fcache->typbyval, fcache->typlen);
589 * Returning a scalar, which we have to extract from the first column
590 * of the SELECT result, and then copy into result context if needed.
592 value = slot_getattr(slot, 1, &(fcinfo->isnull));
595 value = datumCopy(value, fcache->typbyval, fcache->typlen);
598 MemoryContextSwitchTo(oldcontext);
604 * fmgr_sql: function call manager for SQL functions
607 fmgr_sql(PG_FUNCTION_ARGS)
609 MemoryContext oldcontext;
610 SQLFunctionCachePtr fcache;
611 ErrorContextCallback sqlerrcontext;
615 TupleTableSlot *slot;
619 * Switch to context in which the fcache lives. This ensures that
620 * parsetrees, plans, etc, will have sufficient lifetime. The
621 * sub-executor is responsible for deleting per-tuple information.
623 oldcontext = MemoryContextSwitchTo(fcinfo->flinfo->fn_mcxt);
626 * Setup error traceback support for ereport()
628 sqlerrcontext.callback = sql_exec_error_callback;
629 sqlerrcontext.arg = fcinfo->flinfo;
630 sqlerrcontext.previous = error_context_stack;
631 error_context_stack = &sqlerrcontext;
633 /* Check call context */
634 if (fcinfo->flinfo->fn_retset)
636 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
639 * For simplicity, we require callers to support both set eval modes.
640 * There are cases where we must use one or must use the other, and
641 * it's not really worthwhile to postpone the check till we know.
642 * But note we do not require caller to provide an expectedDesc.
644 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
645 (rsi->allowedModes & SFRM_ValuePerCall) == 0 ||
646 (rsi->allowedModes & SFRM_Materialize) == 0)
648 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
649 errmsg("set-valued function called in context that cannot accept a set")));
650 randomAccess = rsi->allowedModes & SFRM_Materialize_Random;
651 lazyEvalOK = !(rsi->allowedModes & SFRM_Materialize_Preferred);
655 randomAccess = false;
660 * Initialize fcache (build plans) if first time through.
662 fcache = (SQLFunctionCachePtr) fcinfo->flinfo->fn_extra;
665 init_sql_fcache(fcinfo->flinfo, lazyEvalOK);
666 fcache = (SQLFunctionCachePtr) fcinfo->flinfo->fn_extra;
668 es = fcache->func_state;
671 * Convert params to appropriate format if starting a fresh execution. (If
672 * continuing execution, we can re-use prior params.)
674 if (es && es->status == F_EXEC_START)
675 postquel_sub_params(fcache, fcinfo);
678 * Build tuplestore to hold results, if we don't have one already. Note
679 * it's in the query-lifespan context.
682 fcache->tstore = tuplestore_begin_heap(randomAccess, false, work_mem);
685 * Find first unfinished query in function.
687 while (es && es->status == F_EXEC_DONE)
691 * Execute each command in the function one after another until we either
692 * run out of commands or get a result row from a lazily-evaluated SELECT.
698 if (es->status == F_EXEC_START)
699 postquel_start(es, fcache);
701 completed = postquel_getnext(es, fcache);
704 * If we ran the command to completion, we can shut it down now. Any
705 * row(s) we need to return are safely stashed in the tuplestore, and
706 * we want to be sure that, for example, AFTER triggers get fired
707 * before we return anything. Also, if the function doesn't return
708 * set, we can shut it down anyway because it must be a SELECT and we
709 * don't care about fetching any more result rows.
711 if (completed || !fcache->returnsSet)
715 * Break from loop if we didn't shut down (implying we got a
716 * lazily-evaluated row). Otherwise we'll press on till the whole
717 * function is done, relying on the tuplestore to keep hold of the
718 * data to eventually be returned. This is necessary since an
719 * INSERT/UPDATE/DELETE RETURNING that sets the result might be
720 * followed by additional rule-inserted commands, and we want to
721 * finish doing all those commands before we return anything.
723 if (es->status != F_EXEC_DONE)
729 * The tuplestore now contains whatever row(s) we are supposed to return.
731 if (fcache->returnsSet)
733 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
738 * If we stopped short of being done, we must have a lazy-eval
741 Assert(es->lazyEval);
742 /* Re-use the junkfilter's output slot to fetch back the tuple */
743 Assert(fcache->junkFilter);
744 slot = fcache->junkFilter->jf_resultSlot;
745 if (!tuplestore_gettupleslot(fcache->tstore, true, false, slot))
746 elog(ERROR, "failed to fetch lazy-eval tuple");
747 /* Extract the result as a datum, and copy out from the slot */
748 result = postquel_get_single_result(slot, fcinfo,
750 /* Clear the tuplestore, but keep it for next time */
751 /* NB: this might delete the slot's content, but we don't care */
752 tuplestore_clear(fcache->tstore);
755 * Let caller know we're not finished.
757 rsi->isDone = ExprMultipleResult;
760 * Ensure we will get shut down cleanly if the exprcontext is not
763 if (!fcache->shutdown_reg)
765 RegisterExprContextCallback(rsi->econtext,
767 PointerGetDatum(fcache));
768 fcache->shutdown_reg = true;
771 else if (fcache->lazyEval)
774 * We are done with a lazy evaluation. Clean up.
776 tuplestore_clear(fcache->tstore);
779 * Let caller know we're finished.
781 rsi->isDone = ExprEndResult;
783 fcinfo->isnull = true;
786 /* Deregister shutdown callback, if we made one */
787 if (fcache->shutdown_reg)
789 UnregisterExprContextCallback(rsi->econtext,
791 PointerGetDatum(fcache));
792 fcache->shutdown_reg = false;
798 * We are done with a non-lazy evaluation. Return whatever is in
799 * the tuplestore. (It is now caller's responsibility to free the
800 * tuplestore when done.)
802 rsi->returnMode = SFRM_Materialize;
803 rsi->setResult = fcache->tstore;
804 fcache->tstore = NULL;
805 /* must copy desc because execQual will free it */
806 if (fcache->junkFilter)
807 rsi->setDesc = CreateTupleDescCopy(fcache->junkFilter->jf_cleanTupType);
809 fcinfo->isnull = true;
812 /* Deregister shutdown callback, if we made one */
813 if (fcache->shutdown_reg)
815 UnregisterExprContextCallback(rsi->econtext,
817 PointerGetDatum(fcache));
818 fcache->shutdown_reg = false;
825 * Non-set function. If we got a row, return it; else return NULL.
827 if (fcache->junkFilter)
829 /* Re-use the junkfilter's output slot to fetch back the tuple */
830 slot = fcache->junkFilter->jf_resultSlot;
831 if (tuplestore_gettupleslot(fcache->tstore, true, false, slot))
832 result = postquel_get_single_result(slot, fcinfo,
836 fcinfo->isnull = true;
842 /* Should only get here for VOID functions */
843 Assert(fcache->rettype == VOIDOID);
844 fcinfo->isnull = true;
848 /* Clear the tuplestore, but keep it for next time */
849 tuplestore_clear(fcache->tstore);
853 * If we've gone through every command in the function, we are done. Reset
854 * the execution states to start over again on next call.
858 es = fcache->func_state;
861 es->status = F_EXEC_START;
866 error_context_stack = sqlerrcontext.previous;
868 MemoryContextSwitchTo(oldcontext);
875 * error context callback to let us supply a call-stack traceback
878 sql_exec_error_callback(void *arg)
880 FmgrInfo *flinfo = (FmgrInfo *) arg;
881 SQLFunctionCachePtr fcache = (SQLFunctionCachePtr) flinfo->fn_extra;
882 HeapTuple func_tuple;
883 Form_pg_proc functup;
885 int syntaxerrposition;
887 /* Need access to function's pg_proc tuple */
888 func_tuple = SearchSysCache(PROCOID,
889 ObjectIdGetDatum(flinfo->fn_oid),
891 if (!HeapTupleIsValid(func_tuple))
892 return; /* shouldn't happen */
893 functup = (Form_pg_proc) GETSTRUCT(func_tuple);
894 fn_name = NameStr(functup->proname);
897 * If there is a syntax error position, convert to internal syntax error
899 syntaxerrposition = geterrposition();
900 if (syntaxerrposition > 0)
906 tmp = SysCacheGetAttr(PROCOID, func_tuple, Anum_pg_proc_prosrc,
909 elog(ERROR, "null prosrc");
910 prosrc = TextDatumGetCString(tmp);
912 internalerrposition(syntaxerrposition);
913 internalerrquery(prosrc);
918 * Try to determine where in the function we failed. If there is a query
919 * with non-null QueryDesc, finger it. (We check this rather than looking
920 * for F_EXEC_RUN state, so that errors during ExecutorStart or
921 * ExecutorEnd are blamed on the appropriate query; see postquel_start and
929 es = fcache->func_state;
935 errcontext("SQL function \"%s\" statement %d",
945 * couldn't identify a running query; might be function entry,
946 * function exit, or between queries.
948 errcontext("SQL function \"%s\"", fn_name);
953 /* must have failed during init_sql_fcache() */
954 errcontext("SQL function \"%s\" during startup", fn_name);
957 ReleaseSysCache(func_tuple);
962 * callback function in case a function-returning-set needs to be shut down
963 * before it has been run to completion
966 ShutdownSQLFunction(Datum arg)
968 SQLFunctionCachePtr fcache = (SQLFunctionCachePtr) DatumGetPointer(arg);
969 execution_state *es = fcache->func_state;
973 /* Shut down anything still running */
974 if (es->status == F_EXEC_RUN)
976 /* Reset states to START in case we're called again */
977 es->status = F_EXEC_START;
981 /* Release tuplestore if we have one */
983 tuplestore_end(fcache->tstore);
984 fcache->tstore = NULL;
986 /* execUtils will deregister the callback... */
987 fcache->shutdown_reg = false;
992 * check_sql_fn_retval() -- check return value of a list of sql parse trees.
994 * The return value of a sql function is the value returned by the last
995 * canSetTag query in the function. We do some ad-hoc type checking here
996 * to be sure that the user is returning the type he claims. There are
997 * also a couple of strange-looking features to assist callers in dealing
998 * with allowed special cases, such as binary-compatible result types.
1000 * For a polymorphic function the passed rettype must be the actual resolved
1001 * output type of the function; we should never see a polymorphic pseudotype
1002 * such as ANYELEMENT as rettype. (This means we can't check the type during
1003 * function definition of a polymorphic function.)
1005 * This function returns true if the sql function returns the entire tuple
1006 * result of its final statement, or false if it returns just the first column
1007 * result of that statement. It throws an error if the final statement doesn't
1008 * return the right type at all.
1010 * Note that because we allow "SELECT rowtype_expression", the result can be
1011 * false even when the declared function return type is a rowtype.
1013 * If modifyTargetList isn't NULL, the function will modify the final
1014 * statement's targetlist in two cases:
1015 * (1) if the tlist returns values that are binary-coercible to the expected
1016 * type rather than being exactly the expected type. RelabelType nodes will
1017 * be inserted to make the result types match exactly.
1018 * (2) if there are dropped columns in the declared result rowtype. NULL
1019 * output columns will be inserted in the tlist to match them.
1020 * (Obviously the caller must pass a parsetree that is okay to modify when
1021 * using this flag.) Note that this flag does not affect whether the tlist is
1022 * considered to be a legal match to the result type, only how we react to
1023 * allowed not-exact-match cases. *modifyTargetList will be set true iff
1024 * we had to make any "dangerous" changes that could modify the semantics of
1025 * the statement. If it is set true, the caller should not use the modified
1026 * statement, but for simplicity we apply the changes anyway.
1028 * If junkFilter isn't NULL, then *junkFilter is set to a JunkFilter defined
1029 * to convert the function's tuple result to the correct output tuple type.
1030 * Exception: if the function is defined to return VOID then *junkFilter is
1034 check_sql_fn_retval(Oid func_id, Oid rettype, List *queryTreeList,
1035 bool *modifyTargetList,
1036 JunkFilter **junkFilter)
1046 AssertArg(!IsPolymorphicType(rettype));
1048 if (modifyTargetList)
1049 *modifyTargetList = false; /* initialize for no change */
1051 *junkFilter = NULL; /* initialize in case of VOID result */
1054 * Find the last canSetTag query in the list. This isn't necessarily the
1055 * last parsetree, because rule rewriting can insert queries after what
1059 foreach(lc, queryTreeList)
1061 Query *q = (Query *) lfirst(lc);
1068 * If it's a plain SELECT, it returns whatever the targetlist says.
1069 * Otherwise, if it's INSERT/UPDATE/DELETE with RETURNING, it returns
1070 * that. Otherwise, the function return type must be VOID.
1072 * Note: eventually replace this test with QueryReturnsTuples? We'd need
1073 * a more general method of determining the output type, though. Also, it
1074 * seems too dangerous to consider FETCH or EXECUTE as returning a
1075 * determinable rowtype, since they depend on relatively short-lived
1079 parse->commandType == CMD_SELECT &&
1080 parse->utilityStmt == NULL &&
1081 parse->intoClause == NULL)
1083 tlist_ptr = &parse->targetList;
1084 tlist = parse->targetList;
1087 (parse->commandType == CMD_INSERT ||
1088 parse->commandType == CMD_UPDATE ||
1089 parse->commandType == CMD_DELETE) &&
1090 parse->returningList)
1092 tlist_ptr = &parse->returningList;
1093 tlist = parse->returningList;
1097 /* Empty function body, or last statement is a utility command */
1098 if (rettype != VOIDOID)
1100 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1101 errmsg("return type mismatch in function declared to return %s",
1102 format_type_be(rettype)),
1103 errdetail("Function's final statement must be SELECT or INSERT/UPDATE/DELETE RETURNING.")));
1108 * OK, check that the targetlist returns something matching the declared
1109 * type. (We used to insist that the declared type not be VOID in this
1110 * case, but that makes it hard to write a void function that exits after
1111 * calling another void function. Instead, we insist that the tlist
1112 * return void ... so void is treated as if it were a scalar type below.)
1116 * Count the non-junk entries in the result targetlist.
1118 tlistlen = ExecCleanTargetListLength(tlist);
1120 fn_typtype = get_typtype(rettype);
1122 if (fn_typtype == TYPTYPE_BASE ||
1123 fn_typtype == TYPTYPE_DOMAIN ||
1124 fn_typtype == TYPTYPE_ENUM ||
1128 * For scalar-type returns, the target list must have exactly one
1129 * non-junk entry, and its type must agree with what the user
1130 * declared; except we allow binary-compatible types too.
1136 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1137 errmsg("return type mismatch in function declared to return %s",
1138 format_type_be(rettype)),
1139 errdetail("Final statement must return exactly one column.")));
1141 /* We assume here that non-junk TLEs must come first in tlists */
1142 tle = (TargetEntry *) linitial(tlist);
1143 Assert(!tle->resjunk);
1145 restype = exprType((Node *) tle->expr);
1146 if (!IsBinaryCoercible(restype, rettype))
1148 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1149 errmsg("return type mismatch in function declared to return %s",
1150 format_type_be(rettype)),
1151 errdetail("Actual return type is %s.",
1152 format_type_be(restype))));
1153 if (modifyTargetList && restype != rettype)
1155 tle->expr = (Expr *) makeRelabelType(tle->expr,
1159 /* Relabel is dangerous if TLE is a sort/group or setop column */
1160 if (tle->ressortgroupref != 0 || parse->setOperations)
1161 *modifyTargetList = true;
1164 /* Set up junk filter if needed */
1166 *junkFilter = ExecInitJunkFilter(tlist, false, NULL);
1168 else if (fn_typtype == TYPTYPE_COMPOSITE || rettype == RECORDOID)
1170 /* Returns a rowtype */
1172 int tupnatts; /* physical number of columns in tuple */
1173 int tuplogcols; /* # of nondeleted columns in tuple */
1174 int colindex; /* physical column index */
1175 List *newtlist; /* new non-junk tlist entries */
1176 List *junkattrs; /* new junk tlist entries */
1179 * If the target list is of length 1, and the type of the varnode in
1180 * the target list matches the declared return type, this is okay.
1181 * This can happen, for example, where the body of the function is
1182 * 'SELECT func2()', where func2 has the same composite return type as
1183 * the function that's calling it.
1187 TargetEntry *tle = (TargetEntry *) linitial(tlist);
1189 Assert(!tle->resjunk);
1190 restype = exprType((Node *) tle->expr);
1191 if (IsBinaryCoercible(restype, rettype))
1193 if (modifyTargetList && restype != rettype)
1195 tle->expr = (Expr *) makeRelabelType(tle->expr,
1199 /* Relabel is dangerous if sort/group or setop column */
1200 if (tle->ressortgroupref != 0 || parse->setOperations)
1201 *modifyTargetList = true;
1203 /* Set up junk filter if needed */
1205 *junkFilter = ExecInitJunkFilter(tlist, false, NULL);
1206 return false; /* NOT returning whole tuple */
1210 /* Is the rowtype fixed, or determined only at runtime? */
1211 if (get_func_result_type(func_id, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1214 * Assume we are returning the whole tuple. Crosschecking against
1215 * what the caller expects will happen at runtime.
1218 *junkFilter = ExecInitJunkFilter(tlist, false, NULL);
1224 * Verify that the targetlist matches the return tuple type. We scan
1225 * the non-deleted attributes to ensure that they match the datatypes
1226 * of the non-resjunk columns. For deleted attributes, insert NULL
1227 * result columns if the caller asked for that.
1229 tupnatts = tupdesc->natts;
1230 tuplogcols = 0; /* we'll count nondeleted cols as we go */
1232 newtlist = NIL; /* these are only used if modifyTargetList */
1237 TargetEntry *tle = (TargetEntry *) lfirst(lc);
1238 Form_pg_attribute attr;
1244 if (modifyTargetList)
1245 junkattrs = lappend(junkattrs, tle);
1252 if (colindex > tupnatts)
1254 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1255 errmsg("return type mismatch in function declared to return %s",
1256 format_type_be(rettype)),
1257 errdetail("Final statement returns too many columns.")));
1258 attr = tupdesc->attrs[colindex - 1];
1259 if (attr->attisdropped && modifyTargetList)
1263 /* The type of the null we insert isn't important */
1264 null_expr = (Expr *) makeConst(INT4OID,
1270 newtlist = lappend(newtlist,
1271 makeTargetEntry(null_expr,
1275 /* NULL insertion is dangerous in a setop */
1276 if (parse->setOperations)
1277 *modifyTargetList = true;
1279 } while (attr->attisdropped);
1282 tletype = exprType((Node *) tle->expr);
1283 atttype = attr->atttypid;
1284 if (!IsBinaryCoercible(tletype, atttype))
1286 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1287 errmsg("return type mismatch in function declared to return %s",
1288 format_type_be(rettype)),
1289 errdetail("Final statement returns %s instead of %s at column %d.",
1290 format_type_be(tletype),
1291 format_type_be(atttype),
1293 if (modifyTargetList)
1295 if (tletype != atttype)
1297 tle->expr = (Expr *) makeRelabelType(tle->expr,
1301 /* Relabel is dangerous if sort/group or setop column */
1302 if (tle->ressortgroupref != 0 || parse->setOperations)
1303 *modifyTargetList = true;
1305 tle->resno = colindex;
1306 newtlist = lappend(newtlist, tle);
1310 /* remaining columns in tupdesc had better all be dropped */
1311 for (colindex++; colindex <= tupnatts; colindex++)
1313 if (!tupdesc->attrs[colindex - 1]->attisdropped)
1315 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1316 errmsg("return type mismatch in function declared to return %s",
1317 format_type_be(rettype)),
1318 errdetail("Final statement returns too few columns.")));
1319 if (modifyTargetList)
1323 /* The type of the null we insert isn't important */
1324 null_expr = (Expr *) makeConst(INT4OID,
1330 newtlist = lappend(newtlist,
1331 makeTargetEntry(null_expr,
1335 /* NULL insertion is dangerous in a setop */
1336 if (parse->setOperations)
1337 *modifyTargetList = true;
1341 if (modifyTargetList)
1343 /* ensure resjunk columns are numbered correctly */
1344 foreach(lc, junkattrs)
1346 TargetEntry *tle = (TargetEntry *) lfirst(lc);
1348 tle->resno = colindex++;
1350 /* replace the tlist with the modified one */
1351 *tlist_ptr = list_concat(newtlist, junkattrs);
1354 /* Set up junk filter if needed */
1356 *junkFilter = ExecInitJunkFilterConversion(tlist,
1357 CreateTupleDescCopy(tupdesc),
1360 /* Report that we are returning entire tuple result */
1365 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1366 errmsg("return type %s is not supported for SQL functions",
1367 format_type_be(rettype))));
1374 * CreateSQLFunctionDestReceiver -- create a suitable DestReceiver object
1377 CreateSQLFunctionDestReceiver(void)
1379 DR_sqlfunction *self = (DR_sqlfunction *) palloc0(sizeof(DR_sqlfunction));
1381 self->pub.receiveSlot = sqlfunction_receive;
1382 self->pub.rStartup = sqlfunction_startup;
1383 self->pub.rShutdown = sqlfunction_shutdown;
1384 self->pub.rDestroy = sqlfunction_destroy;
1385 self->pub.mydest = DestSQLFunction;
1387 /* private fields will be set by postquel_start */
1389 return (DestReceiver *) self;
1393 * sqlfunction_startup --- executor startup
1396 sqlfunction_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
1402 * sqlfunction_receive --- receive one tuple
1405 sqlfunction_receive(TupleTableSlot *slot, DestReceiver *self)
1407 DR_sqlfunction *myState = (DR_sqlfunction *) self;
1408 MemoryContext oldcxt;
1410 /* Filter tuple as needed */
1411 slot = ExecFilterJunk(myState->filter, slot);
1413 /* Store the filtered tuple into the tuplestore */
1414 oldcxt = MemoryContextSwitchTo(myState->cxt);
1415 tuplestore_puttupleslot(myState->tstore, slot);
1416 MemoryContextSwitchTo(oldcxt);
1420 * sqlfunction_shutdown --- executor end
1423 sqlfunction_shutdown(DestReceiver *self)
1429 * sqlfunction_destroy --- release DestReceiver object
1432 sqlfunction_destroy(DestReceiver *self)