1 /*-------------------------------------------------------------------------
4 * Execution of SQL-language functions
6 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/executor/functions.c
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.
51 * The "next" fields chain together all the execution_state records generated
52 * from a single original parsetree. (There will only be more than one in
53 * case of rule expansion of the original parsetree.)
57 F_EXEC_START, F_EXEC_RUN, F_EXEC_DONE
60 typedef struct execution_state
62 struct execution_state *next;
64 bool setsResult; /* true if this query produces func's result */
65 bool lazyEval; /* true if should fetch one row at a time */
66 Node *stmt; /* PlannedStmt or utility statement */
67 QueryDesc *qd; /* null unless status == RUN */
72 * An SQLFunctionCache record is built during the first call,
73 * and linked to from the fn_extra field of the FmgrInfo struct.
75 * Note that currently this has only the lifespan of the calling query.
76 * Someday we might want to consider caching the parse/plan results longer
81 char *fname; /* function name (for error msgs) */
82 char *src; /* function body text (for error msgs) */
84 SQLFunctionParseInfoPtr pinfo; /* data for parser callback hooks */
86 Oid rettype; /* actual return type */
87 int16 typlen; /* length of the return type */
88 bool typbyval; /* true if return type is pass by value */
89 bool returnsSet; /* true if returning multiple rows */
90 bool returnsTuple; /* true if returning whole tuple result */
91 bool shutdown_reg; /* true if registered shutdown callback */
92 bool readonly_func; /* true to run in "read only" mode */
93 bool lazyEval; /* true if using lazyEval for result query */
95 ParamListInfo paramLI; /* Param list representing current args */
97 Tuplestorestate *tstore; /* where we accumulate result tuples */
99 JunkFilter *junkFilter; /* will be NULL if function returns VOID */
102 * func_state is a List of execution_state records, each of which is the
103 * first for its original parsetree, with any additional records chained
104 * to it via the "next" fields. This sublist structure is needed to keep
105 * track of where the original query boundaries are.
110 typedef SQLFunctionCache *SQLFunctionCachePtr;
113 * Data structure needed by the parser callback hooks to resolve parameter
114 * references during parsing of a SQL function's body. This is separate from
115 * SQLFunctionCache since we sometimes do parsing separately from execution.
117 typedef struct SQLFunctionParseInfo
119 Oid *argtypes; /* resolved types of input arguments */
120 int nargs; /* number of input arguments */
121 Oid collation; /* function's input collation, if known */
122 } SQLFunctionParseInfo;
125 /* non-export function prototypes */
126 static Node *sql_fn_param_ref(ParseState *pstate, ParamRef *pref);
127 static List *init_execution_state(List *queryTree_list,
128 SQLFunctionCachePtr fcache,
130 static void init_sql_fcache(FmgrInfo *finfo, bool lazyEvalOK);
131 static void postquel_start(execution_state *es, SQLFunctionCachePtr fcache);
132 static bool postquel_getnext(execution_state *es, SQLFunctionCachePtr fcache);
133 static void postquel_end(execution_state *es);
134 static void postquel_sub_params(SQLFunctionCachePtr fcache,
135 FunctionCallInfo fcinfo);
136 static Datum postquel_get_single_result(TupleTableSlot *slot,
137 FunctionCallInfo fcinfo,
138 SQLFunctionCachePtr fcache,
139 MemoryContext resultcontext);
140 static void sql_exec_error_callback(void *arg);
141 static void ShutdownSQLFunction(Datum arg);
142 static void sqlfunction_startup(DestReceiver *self, int operation, TupleDesc typeinfo);
143 static void sqlfunction_receive(TupleTableSlot *slot, DestReceiver *self);
144 static void sqlfunction_shutdown(DestReceiver *self);
145 static void sqlfunction_destroy(DestReceiver *self);
149 * Prepare the SQLFunctionParseInfo struct for parsing a SQL function body
151 * This includes resolving actual types of polymorphic arguments.
153 * call_expr can be passed as NULL, but then we will fail if there are any
154 * polymorphic arguments.
156 SQLFunctionParseInfoPtr
157 prepare_sql_fn_parse_info(HeapTuple procedureTuple,
161 SQLFunctionParseInfoPtr pinfo;
162 Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
165 pinfo = (SQLFunctionParseInfoPtr) palloc0(sizeof(SQLFunctionParseInfo));
167 /* Save the function's input collation */
168 pinfo->collation = inputCollation;
171 * Copy input argument types from the pg_proc entry, then resolve any
174 pinfo->nargs = nargs = procedureStruct->pronargs;
180 argOidVect = (Oid *) palloc(nargs * sizeof(Oid));
182 procedureStruct->proargtypes.values,
183 nargs * sizeof(Oid));
185 for (argnum = 0; argnum < nargs; argnum++)
187 Oid argtype = argOidVect[argnum];
189 if (IsPolymorphicType(argtype))
191 argtype = get_call_expr_argtype(call_expr, argnum);
192 if (argtype == InvalidOid)
194 (errcode(ERRCODE_DATATYPE_MISMATCH),
195 errmsg("could not determine actual type of argument declared %s",
196 format_type_be(argOidVect[argnum]))));
197 argOidVect[argnum] = argtype;
201 pinfo->argtypes = argOidVect;
208 * Parser setup hook for parsing a SQL function body.
211 sql_fn_parser_setup(struct ParseState *pstate, SQLFunctionParseInfoPtr pinfo)
213 /* Later we might use these hooks to support parameter names */
214 pstate->p_pre_columnref_hook = NULL;
215 pstate->p_post_columnref_hook = NULL;
216 pstate->p_paramref_hook = sql_fn_param_ref;
217 /* no need to use p_coerce_param_hook */
218 pstate->p_ref_hook_state = (void *) pinfo;
222 * sql_fn_param_ref parser callback for ParamRefs ($n symbols)
225 sql_fn_param_ref(ParseState *pstate, ParamRef *pref)
227 SQLFunctionParseInfoPtr pinfo = (SQLFunctionParseInfoPtr) pstate->p_ref_hook_state;
228 int paramno = pref->number;
231 /* Check parameter number is valid */
232 if (paramno <= 0 || paramno > pinfo->nargs)
233 return NULL; /* unknown parameter number */
235 param = makeNode(Param);
236 param->paramkind = PARAM_EXTERN;
237 param->paramid = paramno;
238 param->paramtype = pinfo->argtypes[paramno - 1];
239 param->paramtypmod = -1;
240 param->paramcollid = get_typcollation(param->paramtype);
241 param->location = pref->location;
244 * If we have a function input collation, allow it to override the
245 * type-derived collation for parameter symbols. (XXX perhaps this should
246 * not happen if the type collation is not default?)
248 if (OidIsValid(pinfo->collation) && OidIsValid(param->paramcollid))
249 param->paramcollid = pinfo->collation;
251 return (Node *) param;
255 * Set up the per-query execution_state records for a SQL function.
257 * The input is a List of Lists of parsed and rewritten, but not planned,
258 * querytrees. The sublist structure denotes the original query boundaries.
261 init_execution_state(List *queryTree_list,
262 SQLFunctionCachePtr fcache,
266 execution_state *lasttages = NULL;
269 foreach(lc1, queryTree_list)
271 List *qtlist = (List *) lfirst(lc1);
272 execution_state *firstes = NULL;
273 execution_state *preves = NULL;
278 Query *queryTree = (Query *) lfirst(lc2);
280 execution_state *newes;
282 Assert(IsA(queryTree, Query));
284 /* Plan the query if needed */
285 if (queryTree->commandType == CMD_UTILITY)
286 stmt = queryTree->utilityStmt;
288 stmt = (Node *) pg_plan_query(queryTree, 0, NULL);
290 /* Precheck all commands for validity in a function */
291 if (IsA(stmt, TransactionStmt))
293 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
294 /* translator: %s is a SQL statement name */
295 errmsg("%s is not allowed in a SQL function",
296 CreateCommandTag(stmt))));
298 if (fcache->readonly_func && !CommandIsReadOnly(stmt))
300 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
301 /* translator: %s is a SQL statement name */
302 errmsg("%s is not allowed in a non-volatile function",
303 CreateCommandTag(stmt))));
305 /* OK, build the execution_state for this query */
306 newes = (execution_state *) palloc(sizeof(execution_state));
308 preves->next = newes;
313 newes->status = F_EXEC_START;
314 newes->setsResult = false; /* might change below */
315 newes->lazyEval = false; /* might change below */
319 if (queryTree->canSetTag)
325 eslist = lappend(eslist, firstes);
329 * Mark the last canSetTag query as delivering the function result; then,
330 * if it is a plain SELECT, mark it for lazy evaluation. If it's not a
331 * SELECT we must always run it to completion.
333 * Note: at some point we might add additional criteria for whether to use
334 * lazy eval. However, we should prefer to use it whenever the function
335 * doesn't return set, since fetching more than one row is useless in that
338 * Note: don't set setsResult if the function returns VOID, as evidenced
339 * by not having made a junkfilter. This ensures we'll throw away any
340 * output from a utility statement that check_sql_fn_retval deemed to not
343 if (lasttages && fcache->junkFilter)
345 lasttages->setsResult = true;
347 IsA(lasttages->stmt, PlannedStmt))
349 PlannedStmt *ps = (PlannedStmt *) lasttages->stmt;
351 if (ps->commandType == CMD_SELECT &&
352 ps->utilityStmt == NULL &&
353 ps->intoClause == NULL &&
354 !ps->hasModifyingCTE)
355 fcache->lazyEval = lasttages->lazyEval = true;
363 * Initialize the SQLFunctionCache for a SQL function
366 init_sql_fcache(FmgrInfo *finfo, bool lazyEvalOK)
368 Oid foid = finfo->fn_oid;
370 HeapTuple procedureTuple;
371 Form_pg_proc procedureStruct;
372 SQLFunctionCachePtr fcache;
373 List *raw_parsetree_list;
374 List *queryTree_list;
375 List *flat_query_list;
380 fcache = (SQLFunctionCachePtr) palloc0(sizeof(SQLFunctionCache));
381 finfo->fn_extra = (void *) fcache;
384 * get the procedure tuple corresponding to the given function Oid
386 procedureTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(foid));
387 if (!HeapTupleIsValid(procedureTuple))
388 elog(ERROR, "cache lookup failed for function %u", foid);
389 procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
392 * copy function name immediately for use by error reporting callback
394 fcache->fname = pstrdup(NameStr(procedureStruct->proname));
397 * get the result type from the procedure tuple, and check for polymorphic
398 * result type; if so, find out the actual result type.
400 rettype = procedureStruct->prorettype;
402 if (IsPolymorphicType(rettype))
404 rettype = get_fn_expr_rettype(finfo);
405 if (rettype == InvalidOid) /* this probably should not happen */
407 (errcode(ERRCODE_DATATYPE_MISMATCH),
408 errmsg("could not determine actual result type for function declared to return type %s",
409 format_type_be(procedureStruct->prorettype))));
412 fcache->rettype = rettype;
414 /* Fetch the typlen and byval info for the result type */
415 get_typlenbyval(rettype, &fcache->typlen, &fcache->typbyval);
417 /* Remember whether we're returning setof something */
418 fcache->returnsSet = procedureStruct->proretset;
420 /* Remember if function is STABLE/IMMUTABLE */
421 fcache->readonly_func =
422 (procedureStruct->provolatile != PROVOLATILE_VOLATILE);
425 * We need the actual argument types to pass to the parser. Also make
426 * sure that parameter symbols are considered to have the function's
427 * resolved input collation.
429 fcache->pinfo = prepare_sql_fn_parse_info(procedureTuple,
431 finfo->fn_collation);
434 * And of course we need the function body text.
436 tmp = SysCacheGetAttr(PROCOID,
441 elog(ERROR, "null prosrc for function %u", foid);
442 fcache->src = TextDatumGetCString(tmp);
445 * Parse and rewrite the queries in the function text. Use sublists to
446 * keep track of the original query boundaries. But we also build a
447 * "flat" list of the rewritten queries to pass to check_sql_fn_retval.
448 * This is because the last canSetTag query determines the result type
449 * independently of query boundaries --- and it might not be in the last
450 * sublist, for example if the last query rewrites to DO INSTEAD NOTHING.
451 * (It might not be unreasonable to throw an error in such a case, but
452 * this is the historical behavior and it doesn't seem worth changing.)
454 raw_parsetree_list = pg_parse_query(fcache->src);
456 queryTree_list = NIL;
457 flat_query_list = NIL;
458 foreach(lc, raw_parsetree_list)
460 Node *parsetree = (Node *) lfirst(lc);
461 List *queryTree_sublist;
463 queryTree_sublist = pg_analyze_and_rewrite_params(parsetree,
465 (ParserSetupHook) sql_fn_parser_setup,
467 queryTree_list = lappend(queryTree_list, queryTree_sublist);
468 flat_query_list = list_concat(flat_query_list,
469 list_copy(queryTree_sublist));
473 * Check that the function returns the type it claims to. Although in
474 * simple cases this was already done when the function was defined, we
475 * have to recheck because database objects used in the function's queries
476 * might have changed type. We'd have to do it anyway if the function had
477 * any polymorphic arguments.
479 * Note: we set fcache->returnsTuple according to whether we are returning
480 * the whole tuple result or just a single column. In the latter case we
481 * clear returnsTuple because we need not act different from the scalar
482 * result case, even if it's a rowtype column. (However, we have to force
483 * lazy eval mode in that case; otherwise we'd need extra code to expand
484 * the rowtype column into multiple columns, since we have no way to
485 * notify the caller that it should do that.)
487 * check_sql_fn_retval will also construct a JunkFilter we can use to
488 * coerce the returned rowtype to the desired form (unless the result type
489 * is VOID, in which case there's nothing to coerce to).
491 fcache->returnsTuple = check_sql_fn_retval(foid,
495 &fcache->junkFilter);
497 if (fcache->returnsTuple)
499 /* Make sure output rowtype is properly blessed */
500 BlessTupleDesc(fcache->junkFilter->jf_resultSlot->tts_tupleDescriptor);
502 else if (fcache->returnsSet && type_is_rowtype(fcache->rettype))
505 * Returning rowtype as if it were scalar --- materialize won't work.
506 * Right now it's sufficient to override any caller preference for
507 * materialize mode, but to add more smarts in init_execution_state
508 * about this, we'd probably need a three-way flag instead of bool.
513 /* Finally, plan the queries */
514 fcache->func_state = init_execution_state(queryTree_list,
518 ReleaseSysCache(procedureTuple);
521 /* Start up execution of one execution_state node */
523 postquel_start(execution_state *es, SQLFunctionCachePtr fcache)
527 Assert(es->qd == NULL);
529 /* Caller should have ensured a suitable snapshot is active */
530 Assert(ActiveSnapshotSet());
533 * If this query produces the function result, send its output to the
534 * tuplestore; else discard any output.
538 DR_sqlfunction *myState;
540 dest = CreateDestReceiver(DestSQLFunction);
541 /* pass down the needed info to the dest receiver routines */
542 myState = (DR_sqlfunction *) dest;
543 Assert(myState->pub.mydest == DestSQLFunction);
544 myState->tstore = fcache->tstore;
545 myState->cxt = CurrentMemoryContext;
546 myState->filter = fcache->junkFilter;
549 dest = None_Receiver;
551 if (IsA(es->stmt, PlannedStmt))
552 es->qd = CreateQueryDesc((PlannedStmt *) es->stmt,
559 es->qd = CreateUtilityQueryDesc(es->stmt,
565 /* Utility commands don't need Executor. */
566 if (es->qd->utilitystmt == NULL)
569 * In lazyEval mode, do not let the executor set up an AfterTrigger
570 * context. This is necessary not just an optimization, because we
571 * mustn't exit from the function execution with a stacked
572 * AfterTrigger level still active. We are careful not to select
573 * lazyEval mode for any statement that could possibly queue triggers.
578 eflags = EXEC_FLAG_SKIP_TRIGGERS;
580 eflags = 0; /* default run-to-completion flags */
581 ExecutorStart(es->qd, eflags);
584 es->status = F_EXEC_RUN;
587 /* Run one execution_state; either to completion or to first result row */
588 /* Returns true if we ran to completion */
590 postquel_getnext(execution_state *es, SQLFunctionCachePtr fcache)
594 if (es->qd->utilitystmt)
596 /* ProcessUtility needs the PlannedStmt for DECLARE CURSOR */
597 ProcessUtility((es->qd->plannedstmt ?
598 (Node *) es->qd->plannedstmt :
599 es->qd->utilitystmt),
602 false, /* not top level */
605 result = true; /* never stops early */
609 /* Run regular commands to completion unless lazyEval */
610 long count = (es->lazyEval) ? 1L : 0L;
612 ExecutorRun(es->qd, ForwardScanDirection, count);
615 * If we requested run to completion OR there was no tuple returned,
616 * command must be complete.
618 result = (count == 0L || es->qd->estate->es_processed == 0);
624 /* Shut down execution of one execution_state node */
626 postquel_end(execution_state *es)
628 /* mark status done to ensure we don't do ExecutorEnd twice */
629 es->status = F_EXEC_DONE;
631 /* Utility commands don't need Executor. */
632 if (es->qd->utilitystmt == NULL)
634 ExecutorFinish(es->qd);
638 (*es->qd->dest->rDestroy) (es->qd->dest);
640 FreeQueryDesc(es->qd);
644 /* Build ParamListInfo array representing current arguments */
646 postquel_sub_params(SQLFunctionCachePtr fcache,
647 FunctionCallInfo fcinfo)
649 int nargs = fcinfo->nargs;
653 ParamListInfo paramLI;
656 if (fcache->paramLI == NULL)
658 /* sizeof(ParamListInfoData) includes the first array element */
659 paramLI = (ParamListInfo) palloc(sizeof(ParamListInfoData) +
660 (nargs - 1) *sizeof(ParamExternData));
661 /* we have static list of params, so no hooks needed */
662 paramLI->paramFetch = NULL;
663 paramLI->paramFetchArg = NULL;
664 paramLI->parserSetup = NULL;
665 paramLI->parserSetupArg = NULL;
666 paramLI->numParams = nargs;
667 fcache->paramLI = paramLI;
671 paramLI = fcache->paramLI;
672 Assert(paramLI->numParams == nargs);
675 for (i = 0; i < nargs; i++)
677 ParamExternData *prm = ¶mLI->params[i];
679 prm->value = fcinfo->arg[i];
680 prm->isnull = fcinfo->argnull[i];
682 prm->ptype = fcache->pinfo->argtypes[i];
686 fcache->paramLI = NULL;
690 * Extract the SQL function's value from a single result row. This is used
691 * both for scalar (non-set) functions and for each row of a lazy-eval set
695 postquel_get_single_result(TupleTableSlot *slot,
696 FunctionCallInfo fcinfo,
697 SQLFunctionCachePtr fcache,
698 MemoryContext resultcontext)
701 MemoryContext oldcontext;
704 * Set up to return the function value. For pass-by-reference datatypes,
705 * be sure to allocate the result in resultcontext, not the current memory
706 * context (which has query lifespan). We can't leave the data in the
707 * TupleTableSlot because we intend to clear the slot before returning.
709 oldcontext = MemoryContextSwitchTo(resultcontext);
711 if (fcache->returnsTuple)
713 /* We must return the whole tuple as a Datum. */
714 fcinfo->isnull = false;
715 value = ExecFetchSlotTupleDatum(slot);
716 value = datumCopy(value, fcache->typbyval, fcache->typlen);
721 * Returning a scalar, which we have to extract from the first column
722 * of the SELECT result, and then copy into result context if needed.
724 value = slot_getattr(slot, 1, &(fcinfo->isnull));
727 value = datumCopy(value, fcache->typbyval, fcache->typlen);
730 MemoryContextSwitchTo(oldcontext);
736 * fmgr_sql: function call manager for SQL functions
739 fmgr_sql(PG_FUNCTION_ARGS)
741 MemoryContext oldcontext;
742 SQLFunctionCachePtr fcache;
743 ErrorContextCallback sqlerrcontext;
747 bool pushed_snapshot;
749 TupleTableSlot *slot;
755 * Switch to context in which the fcache lives. This ensures that
756 * parsetrees, plans, etc, will have sufficient lifetime. The
757 * sub-executor is responsible for deleting per-tuple information.
759 oldcontext = MemoryContextSwitchTo(fcinfo->flinfo->fn_mcxt);
762 * Setup error traceback support for ereport()
764 sqlerrcontext.callback = sql_exec_error_callback;
765 sqlerrcontext.arg = fcinfo->flinfo;
766 sqlerrcontext.previous = error_context_stack;
767 error_context_stack = &sqlerrcontext;
769 /* Check call context */
770 if (fcinfo->flinfo->fn_retset)
772 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
775 * For simplicity, we require callers to support both set eval modes.
776 * There are cases where we must use one or must use the other, and
777 * it's not really worthwhile to postpone the check till we know. But
778 * note we do not require caller to provide an expectedDesc.
780 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
781 (rsi->allowedModes & SFRM_ValuePerCall) == 0 ||
782 (rsi->allowedModes & SFRM_Materialize) == 0)
784 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
785 errmsg("set-valued function called in context that cannot accept a set")));
786 randomAccess = rsi->allowedModes & SFRM_Materialize_Random;
787 lazyEvalOK = !(rsi->allowedModes & SFRM_Materialize_Preferred);
791 randomAccess = false;
796 * Initialize fcache (build plans) if first time through.
798 fcache = (SQLFunctionCachePtr) fcinfo->flinfo->fn_extra;
801 init_sql_fcache(fcinfo->flinfo, lazyEvalOK);
802 fcache = (SQLFunctionCachePtr) fcinfo->flinfo->fn_extra;
804 eslist = fcache->func_state;
807 * Find first unfinished query in function, and note whether it's the
812 foreach(eslc, eslist)
814 es = (execution_state *) lfirst(eslc);
816 while (es && es->status == F_EXEC_DONE)
827 * Convert params to appropriate format if starting a fresh execution. (If
828 * continuing execution, we can re-use prior params.)
830 if (is_first && es && es->status == F_EXEC_START)
831 postquel_sub_params(fcache, fcinfo);
834 * Build tuplestore to hold results, if we don't have one already. Note
835 * it's in the query-lifespan context.
838 fcache->tstore = tuplestore_begin_heap(randomAccess, false, work_mem);
841 * Execute each command in the function one after another until we either
842 * run out of commands or get a result row from a lazily-evaluated SELECT.
844 * Notes about snapshot management:
846 * In a read-only function, we just use the surrounding query's snapshot.
848 * In a non-read-only function, we rely on the fact that we'll never
849 * suspend execution between queries of the function: the only reason to
850 * suspend execution before completion is if we are returning a row from
851 * a lazily-evaluated SELECT. So, when first entering this loop, we'll
852 * either start a new query (and push a fresh snapshot) or re-establish
853 * the active snapshot from the existing query descriptor. If we need to
854 * start a new query in a subsequent execution of the loop, either we need
855 * a fresh snapshot (and pushed_snapshot is false) or the existing
856 * snapshot is on the active stack and we can just bump its command ID.
858 pushed_snapshot = false;
863 if (es->status == F_EXEC_START)
866 * If not read-only, be sure to advance the command counter for
867 * each command, so that all work to date in this transaction is
868 * visible. Take a new snapshot if we don't have one yet,
869 * otherwise just bump the command ID in the existing snapshot.
871 if (!fcache->readonly_func)
873 CommandCounterIncrement();
874 if (!pushed_snapshot)
876 PushActiveSnapshot(GetTransactionSnapshot());
877 pushed_snapshot = true;
880 UpdateActiveSnapshotCommandId();
883 postquel_start(es, fcache);
885 else if (!fcache->readonly_func && !pushed_snapshot)
887 /* Re-establish active snapshot when re-entering function */
888 PushActiveSnapshot(es->qd->snapshot);
889 pushed_snapshot = true;
892 completed = postquel_getnext(es, fcache);
895 * If we ran the command to completion, we can shut it down now. Any
896 * row(s) we need to return are safely stashed in the tuplestore, and
897 * we want to be sure that, for example, AFTER triggers get fired
898 * before we return anything. Also, if the function doesn't return
899 * set, we can shut it down anyway because it must be a SELECT and we
900 * don't care about fetching any more result rows.
902 if (completed || !fcache->returnsSet)
906 * Break from loop if we didn't shut down (implying we got a
907 * lazily-evaluated row). Otherwise we'll press on till the whole
908 * function is done, relying on the tuplestore to keep hold of the
909 * data to eventually be returned. This is necessary since an
910 * INSERT/UPDATE/DELETE RETURNING that sets the result might be
911 * followed by additional rule-inserted commands, and we want to
912 * finish doing all those commands before we return anything.
914 if (es->status != F_EXEC_DONE)
918 * Advance to next execution_state, which might be in the next list.
925 break; /* end of function */
927 es = (execution_state *) lfirst(eslc);
930 * Flush the current snapshot so that we will take a new one
931 * for the new query list. This ensures that new snaps are
932 * taken at original-query boundaries, matching the behavior
933 * of interactive execution.
938 pushed_snapshot = false;
944 * The tuplestore now contains whatever row(s) we are supposed to return.
946 if (fcache->returnsSet)
948 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
953 * If we stopped short of being done, we must have a lazy-eval
956 Assert(es->lazyEval);
957 /* Re-use the junkfilter's output slot to fetch back the tuple */
958 Assert(fcache->junkFilter);
959 slot = fcache->junkFilter->jf_resultSlot;
960 if (!tuplestore_gettupleslot(fcache->tstore, true, false, slot))
961 elog(ERROR, "failed to fetch lazy-eval tuple");
962 /* Extract the result as a datum, and copy out from the slot */
963 result = postquel_get_single_result(slot, fcinfo,
965 /* Clear the tuplestore, but keep it for next time */
966 /* NB: this might delete the slot's content, but we don't care */
967 tuplestore_clear(fcache->tstore);
970 * Let caller know we're not finished.
972 rsi->isDone = ExprMultipleResult;
975 * Ensure we will get shut down cleanly if the exprcontext is not
978 if (!fcache->shutdown_reg)
980 RegisterExprContextCallback(rsi->econtext,
982 PointerGetDatum(fcache));
983 fcache->shutdown_reg = true;
986 else if (fcache->lazyEval)
989 * We are done with a lazy evaluation. Clean up.
991 tuplestore_clear(fcache->tstore);
994 * Let caller know we're finished.
996 rsi->isDone = ExprEndResult;
998 fcinfo->isnull = true;
1001 /* Deregister shutdown callback, if we made one */
1002 if (fcache->shutdown_reg)
1004 UnregisterExprContextCallback(rsi->econtext,
1005 ShutdownSQLFunction,
1006 PointerGetDatum(fcache));
1007 fcache->shutdown_reg = false;
1013 * We are done with a non-lazy evaluation. Return whatever is in
1014 * the tuplestore. (It is now caller's responsibility to free the
1015 * tuplestore when done.)
1017 rsi->returnMode = SFRM_Materialize;
1018 rsi->setResult = fcache->tstore;
1019 fcache->tstore = NULL;
1020 /* must copy desc because execQual will free it */
1021 if (fcache->junkFilter)
1022 rsi->setDesc = CreateTupleDescCopy(fcache->junkFilter->jf_cleanTupType);
1024 fcinfo->isnull = true;
1027 /* Deregister shutdown callback, if we made one */
1028 if (fcache->shutdown_reg)
1030 UnregisterExprContextCallback(rsi->econtext,
1031 ShutdownSQLFunction,
1032 PointerGetDatum(fcache));
1033 fcache->shutdown_reg = false;
1040 * Non-set function. If we got a row, return it; else return NULL.
1042 if (fcache->junkFilter)
1044 /* Re-use the junkfilter's output slot to fetch back the tuple */
1045 slot = fcache->junkFilter->jf_resultSlot;
1046 if (tuplestore_gettupleslot(fcache->tstore, true, false, slot))
1047 result = postquel_get_single_result(slot, fcinfo,
1048 fcache, oldcontext);
1051 fcinfo->isnull = true;
1057 /* Should only get here for VOID functions */
1058 Assert(fcache->rettype == VOIDOID);
1059 fcinfo->isnull = true;
1063 /* Clear the tuplestore, but keep it for next time */
1064 tuplestore_clear(fcache->tstore);
1067 /* Pop snapshot if we have pushed one */
1068 if (pushed_snapshot)
1069 PopActiveSnapshot();
1072 * If we've gone through every command in the function, we are done. Reset
1073 * the execution states to start over again on next call.
1077 foreach(eslc, fcache->func_state)
1079 es = (execution_state *) lfirst(eslc);
1082 es->status = F_EXEC_START;
1088 error_context_stack = sqlerrcontext.previous;
1090 MemoryContextSwitchTo(oldcontext);
1097 * error context callback to let us supply a call-stack traceback
1100 sql_exec_error_callback(void *arg)
1102 FmgrInfo *flinfo = (FmgrInfo *) arg;
1103 SQLFunctionCachePtr fcache = (SQLFunctionCachePtr) flinfo->fn_extra;
1104 int syntaxerrposition;
1107 * We can do nothing useful if init_sql_fcache() didn't get as far as
1108 * saving the function name
1110 if (fcache == NULL || fcache->fname == NULL)
1114 * If there is a syntax error position, convert to internal syntax error
1116 syntaxerrposition = geterrposition();
1117 if (syntaxerrposition > 0 && fcache->src != NULL)
1120 internalerrposition(syntaxerrposition);
1121 internalerrquery(fcache->src);
1125 * Try to determine where in the function we failed. If there is a query
1126 * with non-null QueryDesc, finger it. (We check this rather than looking
1127 * for F_EXEC_RUN state, so that errors during ExecutorStart or
1128 * ExecutorEnd are blamed on the appropriate query; see postquel_start and
1131 if (fcache->func_state)
1133 execution_state *es;
1139 foreach(lc, fcache->func_state)
1141 es = (execution_state *) lfirst(lc);
1146 errcontext("SQL function \"%s\" statement %d",
1147 fcache->fname, query_num);
1159 * couldn't identify a running query; might be function entry,
1160 * function exit, or between queries.
1162 errcontext("SQL function \"%s\"", fcache->fname);
1168 * Assume we failed during init_sql_fcache(). (It's possible that the
1169 * function actually has an empty body, but in that case we may as
1170 * well report all errors as being "during startup".)
1172 errcontext("SQL function \"%s\" during startup", fcache->fname);
1178 * callback function in case a function-returning-set needs to be shut down
1179 * before it has been run to completion
1182 ShutdownSQLFunction(Datum arg)
1184 SQLFunctionCachePtr fcache = (SQLFunctionCachePtr) DatumGetPointer(arg);
1185 execution_state *es;
1188 foreach(lc, fcache->func_state)
1190 es = (execution_state *) lfirst(lc);
1193 /* Shut down anything still running */
1194 if (es->status == F_EXEC_RUN)
1196 /* Re-establish active snapshot for any called functions */
1197 if (!fcache->readonly_func)
1198 PushActiveSnapshot(es->qd->snapshot);
1202 if (!fcache->readonly_func)
1203 PopActiveSnapshot();
1206 /* Reset states to START in case we're called again */
1207 es->status = F_EXEC_START;
1212 /* Release tuplestore if we have one */
1214 tuplestore_end(fcache->tstore);
1215 fcache->tstore = NULL;
1217 /* execUtils will deregister the callback... */
1218 fcache->shutdown_reg = false;
1223 * check_sql_fn_retval() -- check return value of a list of sql parse trees.
1225 * The return value of a sql function is the value returned by the last
1226 * canSetTag query in the function. We do some ad-hoc type checking here
1227 * to be sure that the user is returning the type he claims. There are
1228 * also a couple of strange-looking features to assist callers in dealing
1229 * with allowed special cases, such as binary-compatible result types.
1231 * For a polymorphic function the passed rettype must be the actual resolved
1232 * output type of the function; we should never see a polymorphic pseudotype
1233 * such as ANYELEMENT as rettype. (This means we can't check the type during
1234 * function definition of a polymorphic function.)
1236 * This function returns true if the sql function returns the entire tuple
1237 * result of its final statement, or false if it returns just the first column
1238 * result of that statement. It throws an error if the final statement doesn't
1239 * return the right type at all.
1241 * Note that because we allow "SELECT rowtype_expression", the result can be
1242 * false even when the declared function return type is a rowtype.
1244 * If modifyTargetList isn't NULL, the function will modify the final
1245 * statement's targetlist in two cases:
1246 * (1) if the tlist returns values that are binary-coercible to the expected
1247 * type rather than being exactly the expected type. RelabelType nodes will
1248 * be inserted to make the result types match exactly.
1249 * (2) if there are dropped columns in the declared result rowtype. NULL
1250 * output columns will be inserted in the tlist to match them.
1251 * (Obviously the caller must pass a parsetree that is okay to modify when
1252 * using this flag.) Note that this flag does not affect whether the tlist is
1253 * considered to be a legal match to the result type, only how we react to
1254 * allowed not-exact-match cases. *modifyTargetList will be set true iff
1255 * we had to make any "dangerous" changes that could modify the semantics of
1256 * the statement. If it is set true, the caller should not use the modified
1257 * statement, but for simplicity we apply the changes anyway.
1259 * If junkFilter isn't NULL, then *junkFilter is set to a JunkFilter defined
1260 * to convert the function's tuple result to the correct output tuple type.
1261 * Exception: if the function is defined to return VOID then *junkFilter is
1265 check_sql_fn_retval(Oid func_id, Oid rettype, List *queryTreeList,
1266 bool *modifyTargetList,
1267 JunkFilter **junkFilter)
1277 AssertArg(!IsPolymorphicType(rettype));
1279 if (modifyTargetList)
1280 *modifyTargetList = false; /* initialize for no change */
1282 *junkFilter = NULL; /* initialize in case of VOID result */
1285 * Find the last canSetTag query in the list. This isn't necessarily the
1286 * last parsetree, because rule rewriting can insert queries after what
1290 foreach(lc, queryTreeList)
1292 Query *q = (Query *) lfirst(lc);
1299 * If it's a plain SELECT, it returns whatever the targetlist says.
1300 * Otherwise, if it's INSERT/UPDATE/DELETE with RETURNING, it returns
1301 * that. Otherwise, the function return type must be VOID.
1303 * Note: eventually replace this test with QueryReturnsTuples? We'd need
1304 * a more general method of determining the output type, though. Also, it
1305 * seems too dangerous to consider FETCH or EXECUTE as returning a
1306 * determinable rowtype, since they depend on relatively short-lived
1310 parse->commandType == CMD_SELECT &&
1311 parse->utilityStmt == NULL &&
1312 parse->intoClause == NULL)
1314 tlist_ptr = &parse->targetList;
1315 tlist = parse->targetList;
1318 (parse->commandType == CMD_INSERT ||
1319 parse->commandType == CMD_UPDATE ||
1320 parse->commandType == CMD_DELETE) &&
1321 parse->returningList)
1323 tlist_ptr = &parse->returningList;
1324 tlist = parse->returningList;
1328 /* Empty function body, or last statement is a utility command */
1329 if (rettype != VOIDOID)
1331 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1332 errmsg("return type mismatch in function declared to return %s",
1333 format_type_be(rettype)),
1334 errdetail("Function's final statement must be SELECT or INSERT/UPDATE/DELETE RETURNING.")));
1339 * OK, check that the targetlist returns something matching the declared
1340 * type. (We used to insist that the declared type not be VOID in this
1341 * case, but that makes it hard to write a void function that exits after
1342 * calling another void function. Instead, we insist that the tlist
1343 * return void ... so void is treated as if it were a scalar type below.)
1347 * Count the non-junk entries in the result targetlist.
1349 tlistlen = ExecCleanTargetListLength(tlist);
1351 fn_typtype = get_typtype(rettype);
1353 if (fn_typtype == TYPTYPE_BASE ||
1354 fn_typtype == TYPTYPE_DOMAIN ||
1355 fn_typtype == TYPTYPE_ENUM ||
1359 * For scalar-type returns, the target list must have exactly one
1360 * non-junk entry, and its type must agree with what the user
1361 * declared; except we allow binary-compatible types too.
1367 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1368 errmsg("return type mismatch in function declared to return %s",
1369 format_type_be(rettype)),
1370 errdetail("Final statement must return exactly one column.")));
1372 /* We assume here that non-junk TLEs must come first in tlists */
1373 tle = (TargetEntry *) linitial(tlist);
1374 Assert(!tle->resjunk);
1376 restype = exprType((Node *) tle->expr);
1377 if (!IsBinaryCoercible(restype, rettype))
1379 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1380 errmsg("return type mismatch in function declared to return %s",
1381 format_type_be(rettype)),
1382 errdetail("Actual return type is %s.",
1383 format_type_be(restype))));
1384 if (modifyTargetList && restype != rettype)
1386 tle->expr = (Expr *) makeRelabelType(tle->expr,
1389 get_typcollation(rettype),
1391 /* Relabel is dangerous if TLE is a sort/group or setop column */
1392 if (tle->ressortgroupref != 0 || parse->setOperations)
1393 *modifyTargetList = true;
1396 /* Set up junk filter if needed */
1398 *junkFilter = ExecInitJunkFilter(tlist, false, NULL);
1400 else if (fn_typtype == TYPTYPE_COMPOSITE || rettype == RECORDOID)
1402 /* Returns a rowtype */
1404 int tupnatts; /* physical number of columns in tuple */
1405 int tuplogcols; /* # of nondeleted columns in tuple */
1406 int colindex; /* physical column index */
1407 List *newtlist; /* new non-junk tlist entries */
1408 List *junkattrs; /* new junk tlist entries */
1411 * If the target list is of length 1, and the type of the varnode in
1412 * the target list matches the declared return type, this is okay.
1413 * This can happen, for example, where the body of the function is
1414 * 'SELECT func2()', where func2 has the same composite return type as
1415 * the function that's calling it.
1417 * XXX Note that if rettype is RECORD, the IsBinaryCoercible check
1418 * will succeed for any composite restype. For the moment we rely on
1419 * runtime type checking to catch any discrepancy, but it'd be nice to
1420 * do better at parse time.
1424 TargetEntry *tle = (TargetEntry *) linitial(tlist);
1426 Assert(!tle->resjunk);
1427 restype = exprType((Node *) tle->expr);
1428 if (IsBinaryCoercible(restype, rettype))
1430 if (modifyTargetList && restype != rettype)
1432 tle->expr = (Expr *) makeRelabelType(tle->expr,
1435 get_typcollation(rettype),
1437 /* Relabel is dangerous if sort/group or setop column */
1438 if (tle->ressortgroupref != 0 || parse->setOperations)
1439 *modifyTargetList = true;
1441 /* Set up junk filter if needed */
1443 *junkFilter = ExecInitJunkFilter(tlist, false, NULL);
1444 return false; /* NOT returning whole tuple */
1448 /* Is the rowtype fixed, or determined only at runtime? */
1449 if (get_func_result_type(func_id, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1452 * Assume we are returning the whole tuple. Crosschecking against
1453 * what the caller expects will happen at runtime.
1456 *junkFilter = ExecInitJunkFilter(tlist, false, NULL);
1462 * Verify that the targetlist matches the return tuple type. We scan
1463 * the non-deleted attributes to ensure that they match the datatypes
1464 * of the non-resjunk columns. For deleted attributes, insert NULL
1465 * result columns if the caller asked for that.
1467 tupnatts = tupdesc->natts;
1468 tuplogcols = 0; /* we'll count nondeleted cols as we go */
1470 newtlist = NIL; /* these are only used if modifyTargetList */
1475 TargetEntry *tle = (TargetEntry *) lfirst(lc);
1476 Form_pg_attribute attr;
1482 if (modifyTargetList)
1483 junkattrs = lappend(junkattrs, tle);
1490 if (colindex > tupnatts)
1492 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1493 errmsg("return type mismatch in function declared to return %s",
1494 format_type_be(rettype)),
1495 errdetail("Final statement returns too many columns.")));
1496 attr = tupdesc->attrs[colindex - 1];
1497 if (attr->attisdropped && modifyTargetList)
1501 /* The type of the null we insert isn't important */
1502 null_expr = (Expr *) makeConst(INT4OID,
1508 newtlist = lappend(newtlist,
1509 makeTargetEntry(null_expr,
1513 /* NULL insertion is dangerous in a setop */
1514 if (parse->setOperations)
1515 *modifyTargetList = true;
1517 } while (attr->attisdropped);
1520 tletype = exprType((Node *) tle->expr);
1521 atttype = attr->atttypid;
1522 if (!IsBinaryCoercible(tletype, atttype))
1524 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1525 errmsg("return type mismatch in function declared to return %s",
1526 format_type_be(rettype)),
1527 errdetail("Final statement returns %s instead of %s at column %d.",
1528 format_type_be(tletype),
1529 format_type_be(atttype),
1531 if (modifyTargetList)
1533 if (tletype != atttype)
1535 tle->expr = (Expr *) makeRelabelType(tle->expr,
1538 get_typcollation(atttype),
1540 /* Relabel is dangerous if sort/group or setop column */
1541 if (tle->ressortgroupref != 0 || parse->setOperations)
1542 *modifyTargetList = true;
1544 tle->resno = colindex;
1545 newtlist = lappend(newtlist, tle);
1549 /* remaining columns in tupdesc had better all be dropped */
1550 for (colindex++; colindex <= tupnatts; colindex++)
1552 if (!tupdesc->attrs[colindex - 1]->attisdropped)
1554 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1555 errmsg("return type mismatch in function declared to return %s",
1556 format_type_be(rettype)),
1557 errdetail("Final statement returns too few columns.")));
1558 if (modifyTargetList)
1562 /* The type of the null we insert isn't important */
1563 null_expr = (Expr *) makeConst(INT4OID,
1569 newtlist = lappend(newtlist,
1570 makeTargetEntry(null_expr,
1574 /* NULL insertion is dangerous in a setop */
1575 if (parse->setOperations)
1576 *modifyTargetList = true;
1580 if (modifyTargetList)
1582 /* ensure resjunk columns are numbered correctly */
1583 foreach(lc, junkattrs)
1585 TargetEntry *tle = (TargetEntry *) lfirst(lc);
1587 tle->resno = colindex++;
1589 /* replace the tlist with the modified one */
1590 *tlist_ptr = list_concat(newtlist, junkattrs);
1593 /* Set up junk filter if needed */
1595 *junkFilter = ExecInitJunkFilterConversion(tlist,
1596 CreateTupleDescCopy(tupdesc),
1599 /* Report that we are returning entire tuple result */
1604 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1605 errmsg("return type %s is not supported for SQL functions",
1606 format_type_be(rettype))));
1613 * CreateSQLFunctionDestReceiver -- create a suitable DestReceiver object
1616 CreateSQLFunctionDestReceiver(void)
1618 DR_sqlfunction *self = (DR_sqlfunction *) palloc0(sizeof(DR_sqlfunction));
1620 self->pub.receiveSlot = sqlfunction_receive;
1621 self->pub.rStartup = sqlfunction_startup;
1622 self->pub.rShutdown = sqlfunction_shutdown;
1623 self->pub.rDestroy = sqlfunction_destroy;
1624 self->pub.mydest = DestSQLFunction;
1626 /* private fields will be set by postquel_start */
1628 return (DestReceiver *) self;
1632 * sqlfunction_startup --- executor startup
1635 sqlfunction_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
1641 * sqlfunction_receive --- receive one tuple
1644 sqlfunction_receive(TupleTableSlot *slot, DestReceiver *self)
1646 DR_sqlfunction *myState = (DR_sqlfunction *) self;
1648 /* Filter tuple as needed */
1649 slot = ExecFilterJunk(myState->filter, slot);
1651 /* Store the filtered tuple into the tuplestore */
1652 tuplestore_puttupleslot(myState->tstore, slot);
1656 * sqlfunction_shutdown --- executor end
1659 sqlfunction_shutdown(DestReceiver *self)
1665 * sqlfunction_destroy --- release DestReceiver object
1668 sqlfunction_destroy(DestReceiver *self)