1 /*-------------------------------------------------------------------------
4 * Execution of SQL-language functions
6 * Portions Copyright (c) 1996-2012, 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/htup_details.h"
18 #include "access/xact.h"
19 #include "catalog/pg_proc.h"
20 #include "catalog/pg_type.h"
21 #include "executor/functions.h"
23 #include "miscadmin.h"
24 #include "nodes/makefuncs.h"
25 #include "nodes/nodeFuncs.h"
26 #include "parser/parse_coerce.h"
27 #include "parser/parse_func.h"
28 #include "tcop/utility.h"
29 #include "utils/builtins.h"
30 #include "utils/datum.h"
31 #include "utils/lsyscache.h"
32 #include "utils/snapmgr.h"
33 #include "utils/syscache.h"
37 * Specialized DestReceiver for collecting query output in a SQL function
41 DestReceiver pub; /* publicly-known function pointers */
42 Tuplestorestate *tstore; /* where to put result tuples */
43 MemoryContext cxt; /* context containing tstore */
44 JunkFilter *filter; /* filter to convert tuple type */
48 * We have an execution_state record for each query in a function. Each
49 * record contains a plantree for its query. If the query is currently in
50 * F_EXEC_RUN state then there's a QueryDesc too.
52 * The "next" fields chain together all the execution_state records generated
53 * from a single original parsetree. (There will only be more than one in
54 * case of rule expansion of the original parsetree.)
58 F_EXEC_START, F_EXEC_RUN, F_EXEC_DONE
61 typedef struct execution_state
63 struct execution_state *next;
65 bool setsResult; /* true if this query produces func's result */
66 bool lazyEval; /* true if should fetch one row at a time */
67 Node *stmt; /* PlannedStmt or utility statement */
68 QueryDesc *qd; /* null unless status == RUN */
73 * An SQLFunctionCache record is built during the first call,
74 * and linked to from the fn_extra field of the FmgrInfo struct.
76 * Note that currently this has only the lifespan of the calling query.
77 * Someday we might want to consider caching the parse/plan results longer
82 char *fname; /* function name (for error msgs) */
83 char *src; /* function body text (for error msgs) */
85 SQLFunctionParseInfoPtr pinfo; /* data for parser callback hooks */
87 Oid rettype; /* actual return type */
88 int16 typlen; /* length of the return type */
89 bool typbyval; /* true if return type is pass by value */
90 bool returnsSet; /* true if returning multiple rows */
91 bool returnsTuple; /* true if returning whole tuple result */
92 bool shutdown_reg; /* true if registered shutdown callback */
93 bool readonly_func; /* true to run in "read only" mode */
94 bool lazyEval; /* true if using lazyEval for result query */
96 ParamListInfo paramLI; /* Param list representing current args */
98 Tuplestorestate *tstore; /* where we accumulate result tuples */
100 JunkFilter *junkFilter; /* will be NULL if function returns VOID */
103 * func_state is a List of execution_state records, each of which is the
104 * first for its original parsetree, with any additional records chained
105 * to it via the "next" fields. This sublist structure is needed to keep
106 * track of where the original query boundaries are.
111 typedef SQLFunctionCache *SQLFunctionCachePtr;
114 * Data structure needed by the parser callback hooks to resolve parameter
115 * references during parsing of a SQL function's body. This is separate from
116 * SQLFunctionCache since we sometimes do parsing separately from execution.
118 typedef struct SQLFunctionParseInfo
120 char *fname; /* function's name */
121 int nargs; /* number of input arguments */
122 Oid *argtypes; /* resolved types of input arguments */
123 char **argnames; /* names of input arguments; NULL if none */
124 /* Note that argnames[i] can be NULL, if some args are unnamed */
125 Oid collation; /* function's input collation, if known */
126 } SQLFunctionParseInfo;
129 /* non-export function prototypes */
130 static Node *sql_fn_param_ref(ParseState *pstate, ParamRef *pref);
131 static Node *sql_fn_post_column_ref(ParseState *pstate,
132 ColumnRef *cref, Node *var);
133 static Node *sql_fn_make_param(SQLFunctionParseInfoPtr pinfo,
134 int paramno, int location);
135 static Node *sql_fn_resolve_param_name(SQLFunctionParseInfoPtr pinfo,
136 const char *paramname, int location);
137 static List *init_execution_state(List *queryTree_list,
138 SQLFunctionCachePtr fcache,
140 static void init_sql_fcache(FmgrInfo *finfo, Oid collation, bool lazyEvalOK);
141 static void postquel_start(execution_state *es, SQLFunctionCachePtr fcache);
142 static bool postquel_getnext(execution_state *es, SQLFunctionCachePtr fcache);
143 static void postquel_end(execution_state *es);
144 static void postquel_sub_params(SQLFunctionCachePtr fcache,
145 FunctionCallInfo fcinfo);
146 static Datum postquel_get_single_result(TupleTableSlot *slot,
147 FunctionCallInfo fcinfo,
148 SQLFunctionCachePtr fcache,
149 MemoryContext resultcontext);
150 static void sql_exec_error_callback(void *arg);
151 static void ShutdownSQLFunction(Datum arg);
152 static void sqlfunction_startup(DestReceiver *self, int operation, TupleDesc typeinfo);
153 static void sqlfunction_receive(TupleTableSlot *slot, DestReceiver *self);
154 static void sqlfunction_shutdown(DestReceiver *self);
155 static void sqlfunction_destroy(DestReceiver *self);
159 * Prepare the SQLFunctionParseInfo struct for parsing a SQL function body
161 * This includes resolving actual types of polymorphic arguments.
163 * call_expr can be passed as NULL, but then we will fail if there are any
164 * polymorphic arguments.
166 SQLFunctionParseInfoPtr
167 prepare_sql_fn_parse_info(HeapTuple procedureTuple,
171 SQLFunctionParseInfoPtr pinfo;
172 Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
175 pinfo = (SQLFunctionParseInfoPtr) palloc0(sizeof(SQLFunctionParseInfo));
177 /* Function's name (only) can be used to qualify argument names */
178 pinfo->fname = pstrdup(NameStr(procedureStruct->proname));
180 /* Save the function's input collation */
181 pinfo->collation = inputCollation;
184 * Copy input argument types from the pg_proc entry, then resolve any
187 pinfo->nargs = nargs = procedureStruct->pronargs;
193 argOidVect = (Oid *) palloc(nargs * sizeof(Oid));
195 procedureStruct->proargtypes.values,
196 nargs * sizeof(Oid));
198 for (argnum = 0; argnum < nargs; argnum++)
200 Oid argtype = argOidVect[argnum];
202 if (IsPolymorphicType(argtype))
204 argtype = get_call_expr_argtype(call_expr, argnum);
205 if (argtype == InvalidOid)
207 (errcode(ERRCODE_DATATYPE_MISMATCH),
208 errmsg("could not determine actual type of argument declared %s",
209 format_type_be(argOidVect[argnum]))));
210 argOidVect[argnum] = argtype;
214 pinfo->argtypes = argOidVect;
218 * Collect names of arguments, too, if any
227 proargnames = SysCacheGetAttr(PROCNAMEARGSNSP, procedureTuple,
228 Anum_pg_proc_proargnames,
231 proargnames = PointerGetDatum(NULL); /* just to be sure */
233 proargmodes = SysCacheGetAttr(PROCNAMEARGSNSP, procedureTuple,
234 Anum_pg_proc_proargmodes,
237 proargmodes = PointerGetDatum(NULL); /* just to be sure */
239 n_arg_names = get_func_input_arg_names(proargnames, proargmodes,
242 /* Paranoia: ignore the result if too few array entries */
243 if (n_arg_names < nargs)
244 pinfo->argnames = NULL;
247 pinfo->argnames = NULL;
253 * Parser setup hook for parsing a SQL function body.
256 sql_fn_parser_setup(struct ParseState *pstate, SQLFunctionParseInfoPtr pinfo)
258 pstate->p_pre_columnref_hook = NULL;
259 pstate->p_post_columnref_hook = sql_fn_post_column_ref;
260 pstate->p_paramref_hook = sql_fn_param_ref;
261 /* no need to use p_coerce_param_hook */
262 pstate->p_ref_hook_state = (void *) pinfo;
266 * sql_fn_post_column_ref parser callback for ColumnRefs
269 sql_fn_post_column_ref(ParseState *pstate, ColumnRef *cref, Node *var)
271 SQLFunctionParseInfoPtr pinfo = (SQLFunctionParseInfoPtr) pstate->p_ref_hook_state;
274 Node *subfield = NULL;
276 const char *name2 = NULL;
280 * Never override a table-column reference. This corresponds to
281 * considering the parameter names to appear in a scope outside the
282 * individual SQL commands, which is what we want.
288 * The allowed syntaxes are:
290 * A A = parameter name
291 * A.B A = function name, B = parameter name
292 * OR: A = record-typed parameter name, B = field name
293 * (the first possibility takes precedence)
294 * A.B.C A = function name, B = record-typed parameter name,
298 nnames = list_length(cref->fields);
303 field1 = (Node *) linitial(cref->fields);
304 Assert(IsA(field1, String));
305 name1 = strVal(field1);
308 subfield = (Node *) lsecond(cref->fields);
309 Assert(IsA(subfield, String));
310 name2 = strVal(subfield);
316 * Three-part name: if the first part doesn't match the function name,
317 * we can fail immediately. Otherwise, look up the second part, and
318 * take the third part to be a field reference.
320 if (strcmp(name1, pinfo->fname) != 0)
323 param = sql_fn_resolve_param_name(pinfo, name2, cref->location);
325 subfield = (Node *) lthird(cref->fields);
326 Assert(IsA(subfield, String));
328 else if (nnames == 2 && strcmp(name1, pinfo->fname) == 0)
331 * Two-part name with first part matching function name: first see if
332 * second part matches any parameter name.
334 param = sql_fn_resolve_param_name(pinfo, name2, cref->location);
338 /* Yes, so this is a parameter reference, no subfield */
343 /* No, so try to match as parameter name and subfield */
344 param = sql_fn_resolve_param_name(pinfo, name1, cref->location);
349 /* Single name, or parameter name followed by subfield */
350 param = sql_fn_resolve_param_name(pinfo, name1, cref->location);
354 return NULL; /* No match */
359 * Must be a reference to a field of a composite parameter; otherwise
360 * ParseFuncOrColumn will return NULL, and we'll fail back at the
363 param = ParseFuncOrColumn(pstate,
364 list_make1(subfield),
366 NIL, false, false, false,
367 NULL, true, cref->location);
374 * sql_fn_param_ref parser callback for ParamRefs ($n symbols)
377 sql_fn_param_ref(ParseState *pstate, ParamRef *pref)
379 SQLFunctionParseInfoPtr pinfo = (SQLFunctionParseInfoPtr) pstate->p_ref_hook_state;
380 int paramno = pref->number;
382 /* Check parameter number is valid */
383 if (paramno <= 0 || paramno > pinfo->nargs)
384 return NULL; /* unknown parameter number */
386 return sql_fn_make_param(pinfo, paramno, pref->location);
390 * sql_fn_make_param construct a Param node for the given paramno
393 sql_fn_make_param(SQLFunctionParseInfoPtr pinfo,
394 int paramno, int location)
398 param = makeNode(Param);
399 param->paramkind = PARAM_EXTERN;
400 param->paramid = paramno;
401 param->paramtype = pinfo->argtypes[paramno - 1];
402 param->paramtypmod = -1;
403 param->paramcollid = get_typcollation(param->paramtype);
404 param->location = location;
407 * If we have a function input collation, allow it to override the
408 * type-derived collation for parameter symbols. (XXX perhaps this should
409 * not happen if the type collation is not default?)
411 if (OidIsValid(pinfo->collation) && OidIsValid(param->paramcollid))
412 param->paramcollid = pinfo->collation;
414 return (Node *) param;
418 * Search for a function parameter of the given name; if there is one,
419 * construct and return a Param node for it. If not, return NULL.
420 * Helper function for sql_fn_post_column_ref.
423 sql_fn_resolve_param_name(SQLFunctionParseInfoPtr pinfo,
424 const char *paramname, int location)
428 if (pinfo->argnames == NULL)
431 for (i = 0; i < pinfo->nargs; i++)
433 if (pinfo->argnames[i] && strcmp(pinfo->argnames[i], paramname) == 0)
434 return sql_fn_make_param(pinfo, i + 1, location);
441 * Set up the per-query execution_state records for a SQL function.
443 * The input is a List of Lists of parsed and rewritten, but not planned,
444 * querytrees. The sublist structure denotes the original query boundaries.
447 init_execution_state(List *queryTree_list,
448 SQLFunctionCachePtr fcache,
452 execution_state *lasttages = NULL;
455 foreach(lc1, queryTree_list)
457 List *qtlist = (List *) lfirst(lc1);
458 execution_state *firstes = NULL;
459 execution_state *preves = NULL;
464 Query *queryTree = (Query *) lfirst(lc2);
466 execution_state *newes;
468 Assert(IsA(queryTree, Query));
470 /* Plan the query if needed */
471 if (queryTree->commandType == CMD_UTILITY)
472 stmt = queryTree->utilityStmt;
474 stmt = (Node *) pg_plan_query(queryTree, 0, NULL);
476 /* Precheck all commands for validity in a function */
477 if (IsA(stmt, TransactionStmt))
479 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
480 /* translator: %s is a SQL statement name */
481 errmsg("%s is not allowed in a SQL function",
482 CreateCommandTag(stmt))));
484 if (fcache->readonly_func && !CommandIsReadOnly(stmt))
486 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
487 /* translator: %s is a SQL statement name */
488 errmsg("%s is not allowed in a non-volatile function",
489 CreateCommandTag(stmt))));
491 /* OK, build the execution_state for this query */
492 newes = (execution_state *) palloc(sizeof(execution_state));
494 preves->next = newes;
499 newes->status = F_EXEC_START;
500 newes->setsResult = false; /* might change below */
501 newes->lazyEval = false; /* might change below */
505 if (queryTree->canSetTag)
511 eslist = lappend(eslist, firstes);
515 * Mark the last canSetTag query as delivering the function result; then,
516 * if it is a plain SELECT, mark it for lazy evaluation. If it's not a
517 * SELECT we must always run it to completion.
519 * Note: at some point we might add additional criteria for whether to use
520 * lazy eval. However, we should prefer to use it whenever the function
521 * doesn't return set, since fetching more than one row is useless in that
524 * Note: don't set setsResult if the function returns VOID, as evidenced
525 * by not having made a junkfilter. This ensures we'll throw away any
526 * output from a utility statement that check_sql_fn_retval deemed to not
529 if (lasttages && fcache->junkFilter)
531 lasttages->setsResult = true;
533 IsA(lasttages->stmt, PlannedStmt))
535 PlannedStmt *ps = (PlannedStmt *) lasttages->stmt;
537 if (ps->commandType == CMD_SELECT &&
538 ps->utilityStmt == NULL &&
539 !ps->hasModifyingCTE)
540 fcache->lazyEval = lasttages->lazyEval = true;
548 * Initialize the SQLFunctionCache for a SQL function
551 init_sql_fcache(FmgrInfo *finfo, Oid collation, bool lazyEvalOK)
553 Oid foid = finfo->fn_oid;
555 HeapTuple procedureTuple;
556 Form_pg_proc procedureStruct;
557 SQLFunctionCachePtr fcache;
558 List *raw_parsetree_list;
559 List *queryTree_list;
560 List *flat_query_list;
565 fcache = (SQLFunctionCachePtr) palloc0(sizeof(SQLFunctionCache));
566 finfo->fn_extra = (void *) fcache;
569 * get the procedure tuple corresponding to the given function Oid
571 procedureTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(foid));
572 if (!HeapTupleIsValid(procedureTuple))
573 elog(ERROR, "cache lookup failed for function %u", foid);
574 procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
577 * copy function name immediately for use by error reporting callback
579 fcache->fname = pstrdup(NameStr(procedureStruct->proname));
582 * get the result type from the procedure tuple, and check for polymorphic
583 * result type; if so, find out the actual result type.
585 rettype = procedureStruct->prorettype;
587 if (IsPolymorphicType(rettype))
589 rettype = get_fn_expr_rettype(finfo);
590 if (rettype == InvalidOid) /* this probably should not happen */
592 (errcode(ERRCODE_DATATYPE_MISMATCH),
593 errmsg("could not determine actual result type for function declared to return type %s",
594 format_type_be(procedureStruct->prorettype))));
597 fcache->rettype = rettype;
599 /* Fetch the typlen and byval info for the result type */
600 get_typlenbyval(rettype, &fcache->typlen, &fcache->typbyval);
602 /* Remember whether we're returning setof something */
603 fcache->returnsSet = procedureStruct->proretset;
605 /* Remember if function is STABLE/IMMUTABLE */
606 fcache->readonly_func =
607 (procedureStruct->provolatile != PROVOLATILE_VOLATILE);
610 * We need the actual argument types to pass to the parser. Also make
611 * sure that parameter symbols are considered to have the function's
612 * resolved input collation.
614 fcache->pinfo = prepare_sql_fn_parse_info(procedureTuple,
619 * And of course we need the function body text.
621 tmp = SysCacheGetAttr(PROCOID,
626 elog(ERROR, "null prosrc for function %u", foid);
627 fcache->src = TextDatumGetCString(tmp);
630 * Parse and rewrite the queries in the function text. Use sublists to
631 * keep track of the original query boundaries. But we also build a
632 * "flat" list of the rewritten queries to pass to check_sql_fn_retval.
633 * This is because the last canSetTag query determines the result type
634 * independently of query boundaries --- and it might not be in the last
635 * sublist, for example if the last query rewrites to DO INSTEAD NOTHING.
636 * (It might not be unreasonable to throw an error in such a case, but
637 * this is the historical behavior and it doesn't seem worth changing.)
639 raw_parsetree_list = pg_parse_query(fcache->src);
641 queryTree_list = NIL;
642 flat_query_list = NIL;
643 foreach(lc, raw_parsetree_list)
645 Node *parsetree = (Node *) lfirst(lc);
646 List *queryTree_sublist;
648 queryTree_sublist = pg_analyze_and_rewrite_params(parsetree,
650 (ParserSetupHook) sql_fn_parser_setup,
652 queryTree_list = lappend(queryTree_list, queryTree_sublist);
653 flat_query_list = list_concat(flat_query_list,
654 list_copy(queryTree_sublist));
658 * Check that the function returns the type it claims to. Although in
659 * simple cases this was already done when the function was defined, we
660 * have to recheck because database objects used in the function's queries
661 * might have changed type. We'd have to do it anyway if the function had
662 * any polymorphic arguments.
664 * Note: we set fcache->returnsTuple according to whether we are returning
665 * the whole tuple result or just a single column. In the latter case we
666 * clear returnsTuple because we need not act different from the scalar
667 * result case, even if it's a rowtype column. (However, we have to force
668 * lazy eval mode in that case; otherwise we'd need extra code to expand
669 * the rowtype column into multiple columns, since we have no way to
670 * notify the caller that it should do that.)
672 * check_sql_fn_retval will also construct a JunkFilter we can use to
673 * coerce the returned rowtype to the desired form (unless the result type
674 * is VOID, in which case there's nothing to coerce to).
676 fcache->returnsTuple = check_sql_fn_retval(foid,
680 &fcache->junkFilter);
682 if (fcache->returnsTuple)
684 /* Make sure output rowtype is properly blessed */
685 BlessTupleDesc(fcache->junkFilter->jf_resultSlot->tts_tupleDescriptor);
687 else if (fcache->returnsSet && type_is_rowtype(fcache->rettype))
690 * Returning rowtype as if it were scalar --- materialize won't work.
691 * Right now it's sufficient to override any caller preference for
692 * materialize mode, but to add more smarts in init_execution_state
693 * about this, we'd probably need a three-way flag instead of bool.
698 /* Finally, plan the queries */
699 fcache->func_state = init_execution_state(queryTree_list,
703 ReleaseSysCache(procedureTuple);
706 /* Start up execution of one execution_state node */
708 postquel_start(execution_state *es, SQLFunctionCachePtr fcache)
712 Assert(es->qd == NULL);
714 /* Caller should have ensured a suitable snapshot is active */
715 Assert(ActiveSnapshotSet());
718 * If this query produces the function result, send its output to the
719 * tuplestore; else discard any output.
723 DR_sqlfunction *myState;
725 dest = CreateDestReceiver(DestSQLFunction);
726 /* pass down the needed info to the dest receiver routines */
727 myState = (DR_sqlfunction *) dest;
728 Assert(myState->pub.mydest == DestSQLFunction);
729 myState->tstore = fcache->tstore;
730 myState->cxt = CurrentMemoryContext;
731 myState->filter = fcache->junkFilter;
734 dest = None_Receiver;
736 if (IsA(es->stmt, PlannedStmt))
737 es->qd = CreateQueryDesc((PlannedStmt *) es->stmt,
744 es->qd = CreateUtilityQueryDesc(es->stmt,
750 /* Utility commands don't need Executor. */
751 if (es->qd->utilitystmt == NULL)
754 * In lazyEval mode, do not let the executor set up an AfterTrigger
755 * context. This is necessary not just an optimization, because we
756 * mustn't exit from the function execution with a stacked
757 * AfterTrigger level still active. We are careful not to select
758 * lazyEval mode for any statement that could possibly queue triggers.
763 eflags = EXEC_FLAG_SKIP_TRIGGERS;
765 eflags = 0; /* default run-to-completion flags */
766 ExecutorStart(es->qd, eflags);
769 es->status = F_EXEC_RUN;
772 /* Run one execution_state; either to completion or to first result row */
773 /* Returns true if we ran to completion */
775 postquel_getnext(execution_state *es, SQLFunctionCachePtr fcache)
779 if (es->qd->utilitystmt)
781 /* ProcessUtility needs the PlannedStmt for DECLARE CURSOR */
782 ProcessUtility((es->qd->plannedstmt ?
783 (Node *) es->qd->plannedstmt :
784 es->qd->utilitystmt),
789 PROCESS_UTILITY_QUERY);
790 result = true; /* never stops early */
794 /* Run regular commands to completion unless lazyEval */
795 long count = (es->lazyEval) ? 1L : 0L;
797 ExecutorRun(es->qd, ForwardScanDirection, count);
800 * If we requested run to completion OR there was no tuple returned,
801 * command must be complete.
803 result = (count == 0L || es->qd->estate->es_processed == 0);
809 /* Shut down execution of one execution_state node */
811 postquel_end(execution_state *es)
813 /* mark status done to ensure we don't do ExecutorEnd twice */
814 es->status = F_EXEC_DONE;
816 /* Utility commands don't need Executor. */
817 if (es->qd->utilitystmt == NULL)
819 ExecutorFinish(es->qd);
823 (*es->qd->dest->rDestroy) (es->qd->dest);
825 FreeQueryDesc(es->qd);
829 /* Build ParamListInfo array representing current arguments */
831 postquel_sub_params(SQLFunctionCachePtr fcache,
832 FunctionCallInfo fcinfo)
834 int nargs = fcinfo->nargs;
838 ParamListInfo paramLI;
841 if (fcache->paramLI == NULL)
843 /* sizeof(ParamListInfoData) includes the first array element */
844 paramLI = (ParamListInfo) palloc(sizeof(ParamListInfoData) +
845 (nargs - 1) * sizeof(ParamExternData));
846 /* we have static list of params, so no hooks needed */
847 paramLI->paramFetch = NULL;
848 paramLI->paramFetchArg = NULL;
849 paramLI->parserSetup = NULL;
850 paramLI->parserSetupArg = NULL;
851 paramLI->numParams = nargs;
852 fcache->paramLI = paramLI;
856 paramLI = fcache->paramLI;
857 Assert(paramLI->numParams == nargs);
860 for (i = 0; i < nargs; i++)
862 ParamExternData *prm = ¶mLI->params[i];
864 prm->value = fcinfo->arg[i];
865 prm->isnull = fcinfo->argnull[i];
867 prm->ptype = fcache->pinfo->argtypes[i];
871 fcache->paramLI = NULL;
875 * Extract the SQL function's value from a single result row. This is used
876 * both for scalar (non-set) functions and for each row of a lazy-eval set
880 postquel_get_single_result(TupleTableSlot *slot,
881 FunctionCallInfo fcinfo,
882 SQLFunctionCachePtr fcache,
883 MemoryContext resultcontext)
886 MemoryContext oldcontext;
889 * Set up to return the function value. For pass-by-reference datatypes,
890 * be sure to allocate the result in resultcontext, not the current memory
891 * context (which has query lifespan). We can't leave the data in the
892 * TupleTableSlot because we intend to clear the slot before returning.
894 oldcontext = MemoryContextSwitchTo(resultcontext);
896 if (fcache->returnsTuple)
898 /* We must return the whole tuple as a Datum. */
899 fcinfo->isnull = false;
900 value = ExecFetchSlotTupleDatum(slot);
901 value = datumCopy(value, fcache->typbyval, fcache->typlen);
906 * Returning a scalar, which we have to extract from the first column
907 * of the SELECT result, and then copy into result context if needed.
909 value = slot_getattr(slot, 1, &(fcinfo->isnull));
912 value = datumCopy(value, fcache->typbyval, fcache->typlen);
915 MemoryContextSwitchTo(oldcontext);
921 * fmgr_sql: function call manager for SQL functions
924 fmgr_sql(PG_FUNCTION_ARGS)
926 MemoryContext oldcontext;
927 SQLFunctionCachePtr fcache;
928 ErrorContextCallback sqlerrcontext;
932 bool pushed_snapshot;
934 TupleTableSlot *slot;
940 * Switch to context in which the fcache lives. This ensures that
941 * parsetrees, plans, etc, will have sufficient lifetime. The
942 * sub-executor is responsible for deleting per-tuple information.
944 oldcontext = MemoryContextSwitchTo(fcinfo->flinfo->fn_mcxt);
947 * Setup error traceback support for ereport()
949 sqlerrcontext.callback = sql_exec_error_callback;
950 sqlerrcontext.arg = fcinfo->flinfo;
951 sqlerrcontext.previous = error_context_stack;
952 error_context_stack = &sqlerrcontext;
954 /* Check call context */
955 if (fcinfo->flinfo->fn_retset)
957 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
960 * For simplicity, we require callers to support both set eval modes.
961 * There are cases where we must use one or must use the other, and
962 * it's not really worthwhile to postpone the check till we know. But
963 * note we do not require caller to provide an expectedDesc.
965 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
966 (rsi->allowedModes & SFRM_ValuePerCall) == 0 ||
967 (rsi->allowedModes & SFRM_Materialize) == 0)
969 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
970 errmsg("set-valued function called in context that cannot accept a set")));
971 randomAccess = rsi->allowedModes & SFRM_Materialize_Random;
972 lazyEvalOK = !(rsi->allowedModes & SFRM_Materialize_Preferred);
976 randomAccess = false;
981 * Initialize fcache (build plans) if first time through.
983 fcache = (SQLFunctionCachePtr) fcinfo->flinfo->fn_extra;
986 init_sql_fcache(fcinfo->flinfo, PG_GET_COLLATION(), lazyEvalOK);
987 fcache = (SQLFunctionCachePtr) fcinfo->flinfo->fn_extra;
989 eslist = fcache->func_state;
992 * Find first unfinished query in function, and note whether it's the
997 foreach(eslc, eslist)
999 es = (execution_state *) lfirst(eslc);
1001 while (es && es->status == F_EXEC_DONE)
1012 * Convert params to appropriate format if starting a fresh execution. (If
1013 * continuing execution, we can re-use prior params.)
1015 if (is_first && es && es->status == F_EXEC_START)
1016 postquel_sub_params(fcache, fcinfo);
1019 * Build tuplestore to hold results, if we don't have one already. Note
1020 * it's in the query-lifespan context.
1022 if (!fcache->tstore)
1023 fcache->tstore = tuplestore_begin_heap(randomAccess, false, work_mem);
1026 * Execute each command in the function one after another until we either
1027 * run out of commands or get a result row from a lazily-evaluated SELECT.
1029 * Notes about snapshot management:
1031 * In a read-only function, we just use the surrounding query's snapshot.
1033 * In a non-read-only function, we rely on the fact that we'll never
1034 * suspend execution between queries of the function: the only reason to
1035 * suspend execution before completion is if we are returning a row from a
1036 * lazily-evaluated SELECT. So, when first entering this loop, we'll
1037 * either start a new query (and push a fresh snapshot) or re-establish
1038 * the active snapshot from the existing query descriptor. If we need to
1039 * start a new query in a subsequent execution of the loop, either we need
1040 * a fresh snapshot (and pushed_snapshot is false) or the existing
1041 * snapshot is on the active stack and we can just bump its command ID.
1043 pushed_snapshot = false;
1048 if (es->status == F_EXEC_START)
1051 * If not read-only, be sure to advance the command counter for
1052 * each command, so that all work to date in this transaction is
1053 * visible. Take a new snapshot if we don't have one yet,
1054 * otherwise just bump the command ID in the existing snapshot.
1056 if (!fcache->readonly_func)
1058 CommandCounterIncrement();
1059 if (!pushed_snapshot)
1061 PushActiveSnapshot(GetTransactionSnapshot());
1062 pushed_snapshot = true;
1065 UpdateActiveSnapshotCommandId();
1068 postquel_start(es, fcache);
1070 else if (!fcache->readonly_func && !pushed_snapshot)
1072 /* Re-establish active snapshot when re-entering function */
1073 PushActiveSnapshot(es->qd->snapshot);
1074 pushed_snapshot = true;
1077 completed = postquel_getnext(es, fcache);
1080 * If we ran the command to completion, we can shut it down now. Any
1081 * row(s) we need to return are safely stashed in the tuplestore, and
1082 * we want to be sure that, for example, AFTER triggers get fired
1083 * before we return anything. Also, if the function doesn't return
1084 * set, we can shut it down anyway because it must be a SELECT and we
1085 * don't care about fetching any more result rows.
1087 if (completed || !fcache->returnsSet)
1091 * Break from loop if we didn't shut down (implying we got a
1092 * lazily-evaluated row). Otherwise we'll press on till the whole
1093 * function is done, relying on the tuplestore to keep hold of the
1094 * data to eventually be returned. This is necessary since an
1095 * INSERT/UPDATE/DELETE RETURNING that sets the result might be
1096 * followed by additional rule-inserted commands, and we want to
1097 * finish doing all those commands before we return anything.
1099 if (es->status != F_EXEC_DONE)
1103 * Advance to next execution_state, which might be in the next list.
1110 break; /* end of function */
1112 es = (execution_state *) lfirst(eslc);
1115 * Flush the current snapshot so that we will take a new one for
1116 * the new query list. This ensures that new snaps are taken at
1117 * original-query boundaries, matching the behavior of interactive
1120 if (pushed_snapshot)
1122 PopActiveSnapshot();
1123 pushed_snapshot = false;
1129 * The tuplestore now contains whatever row(s) we are supposed to return.
1131 if (fcache->returnsSet)
1133 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
1138 * If we stopped short of being done, we must have a lazy-eval
1141 Assert(es->lazyEval);
1142 /* Re-use the junkfilter's output slot to fetch back the tuple */
1143 Assert(fcache->junkFilter);
1144 slot = fcache->junkFilter->jf_resultSlot;
1145 if (!tuplestore_gettupleslot(fcache->tstore, true, false, slot))
1146 elog(ERROR, "failed to fetch lazy-eval tuple");
1147 /* Extract the result as a datum, and copy out from the slot */
1148 result = postquel_get_single_result(slot, fcinfo,
1149 fcache, oldcontext);
1150 /* Clear the tuplestore, but keep it for next time */
1151 /* NB: this might delete the slot's content, but we don't care */
1152 tuplestore_clear(fcache->tstore);
1155 * Let caller know we're not finished.
1157 rsi->isDone = ExprMultipleResult;
1160 * Ensure we will get shut down cleanly if the exprcontext is not
1161 * run to completion.
1163 if (!fcache->shutdown_reg)
1165 RegisterExprContextCallback(rsi->econtext,
1166 ShutdownSQLFunction,
1167 PointerGetDatum(fcache));
1168 fcache->shutdown_reg = true;
1171 else if (fcache->lazyEval)
1174 * We are done with a lazy evaluation. Clean up.
1176 tuplestore_clear(fcache->tstore);
1179 * Let caller know we're finished.
1181 rsi->isDone = ExprEndResult;
1183 fcinfo->isnull = true;
1186 /* Deregister shutdown callback, if we made one */
1187 if (fcache->shutdown_reg)
1189 UnregisterExprContextCallback(rsi->econtext,
1190 ShutdownSQLFunction,
1191 PointerGetDatum(fcache));
1192 fcache->shutdown_reg = false;
1198 * We are done with a non-lazy evaluation. Return whatever is in
1199 * the tuplestore. (It is now caller's responsibility to free the
1200 * tuplestore when done.)
1202 rsi->returnMode = SFRM_Materialize;
1203 rsi->setResult = fcache->tstore;
1204 fcache->tstore = NULL;
1205 /* must copy desc because execQual will free it */
1206 if (fcache->junkFilter)
1207 rsi->setDesc = CreateTupleDescCopy(fcache->junkFilter->jf_cleanTupType);
1209 fcinfo->isnull = true;
1212 /* Deregister shutdown callback, if we made one */
1213 if (fcache->shutdown_reg)
1215 UnregisterExprContextCallback(rsi->econtext,
1216 ShutdownSQLFunction,
1217 PointerGetDatum(fcache));
1218 fcache->shutdown_reg = false;
1225 * Non-set function. If we got a row, return it; else return NULL.
1227 if (fcache->junkFilter)
1229 /* Re-use the junkfilter's output slot to fetch back the tuple */
1230 slot = fcache->junkFilter->jf_resultSlot;
1231 if (tuplestore_gettupleslot(fcache->tstore, true, false, slot))
1232 result = postquel_get_single_result(slot, fcinfo,
1233 fcache, oldcontext);
1236 fcinfo->isnull = true;
1242 /* Should only get here for VOID functions */
1243 Assert(fcache->rettype == VOIDOID);
1244 fcinfo->isnull = true;
1248 /* Clear the tuplestore, but keep it for next time */
1249 tuplestore_clear(fcache->tstore);
1252 /* Pop snapshot if we have pushed one */
1253 if (pushed_snapshot)
1254 PopActiveSnapshot();
1257 * If we've gone through every command in the function, we are done. Reset
1258 * the execution states to start over again on next call.
1262 foreach(eslc, fcache->func_state)
1264 es = (execution_state *) lfirst(eslc);
1267 es->status = F_EXEC_START;
1273 error_context_stack = sqlerrcontext.previous;
1275 MemoryContextSwitchTo(oldcontext);
1282 * error context callback to let us supply a call-stack traceback
1285 sql_exec_error_callback(void *arg)
1287 FmgrInfo *flinfo = (FmgrInfo *) arg;
1288 SQLFunctionCachePtr fcache = (SQLFunctionCachePtr) flinfo->fn_extra;
1289 int syntaxerrposition;
1292 * We can do nothing useful if init_sql_fcache() didn't get as far as
1293 * saving the function name
1295 if (fcache == NULL || fcache->fname == NULL)
1299 * If there is a syntax error position, convert to internal syntax error
1301 syntaxerrposition = geterrposition();
1302 if (syntaxerrposition > 0 && fcache->src != NULL)
1305 internalerrposition(syntaxerrposition);
1306 internalerrquery(fcache->src);
1310 * Try to determine where in the function we failed. If there is a query
1311 * with non-null QueryDesc, finger it. (We check this rather than looking
1312 * for F_EXEC_RUN state, so that errors during ExecutorStart or
1313 * ExecutorEnd are blamed on the appropriate query; see postquel_start and
1316 if (fcache->func_state)
1318 execution_state *es;
1324 foreach(lc, fcache->func_state)
1326 es = (execution_state *) lfirst(lc);
1331 errcontext("SQL function \"%s\" statement %d",
1332 fcache->fname, query_num);
1344 * couldn't identify a running query; might be function entry,
1345 * function exit, or between queries.
1347 errcontext("SQL function \"%s\"", fcache->fname);
1353 * Assume we failed during init_sql_fcache(). (It's possible that the
1354 * function actually has an empty body, but in that case we may as
1355 * well report all errors as being "during startup".)
1357 errcontext("SQL function \"%s\" during startup", fcache->fname);
1363 * callback function in case a function-returning-set needs to be shut down
1364 * before it has been run to completion
1367 ShutdownSQLFunction(Datum arg)
1369 SQLFunctionCachePtr fcache = (SQLFunctionCachePtr) DatumGetPointer(arg);
1370 execution_state *es;
1373 foreach(lc, fcache->func_state)
1375 es = (execution_state *) lfirst(lc);
1378 /* Shut down anything still running */
1379 if (es->status == F_EXEC_RUN)
1381 /* Re-establish active snapshot for any called functions */
1382 if (!fcache->readonly_func)
1383 PushActiveSnapshot(es->qd->snapshot);
1387 if (!fcache->readonly_func)
1388 PopActiveSnapshot();
1391 /* Reset states to START in case we're called again */
1392 es->status = F_EXEC_START;
1397 /* Release tuplestore if we have one */
1399 tuplestore_end(fcache->tstore);
1400 fcache->tstore = NULL;
1402 /* execUtils will deregister the callback... */
1403 fcache->shutdown_reg = false;
1408 * check_sql_fn_retval() -- check return value of a list of sql parse trees.
1410 * The return value of a sql function is the value returned by the last
1411 * canSetTag query in the function. We do some ad-hoc type checking here
1412 * to be sure that the user is returning the type he claims. There are
1413 * also a couple of strange-looking features to assist callers in dealing
1414 * with allowed special cases, such as binary-compatible result types.
1416 * For a polymorphic function the passed rettype must be the actual resolved
1417 * output type of the function; we should never see a polymorphic pseudotype
1418 * such as ANYELEMENT as rettype. (This means we can't check the type during
1419 * function definition of a polymorphic function.)
1421 * This function returns true if the sql function returns the entire tuple
1422 * result of its final statement, or false if it returns just the first column
1423 * result of that statement. It throws an error if the final statement doesn't
1424 * return the right type at all.
1426 * Note that because we allow "SELECT rowtype_expression", the result can be
1427 * false even when the declared function return type is a rowtype.
1429 * If modifyTargetList isn't NULL, the function will modify the final
1430 * statement's targetlist in two cases:
1431 * (1) if the tlist returns values that are binary-coercible to the expected
1432 * type rather than being exactly the expected type. RelabelType nodes will
1433 * be inserted to make the result types match exactly.
1434 * (2) if there are dropped columns in the declared result rowtype. NULL
1435 * output columns will be inserted in the tlist to match them.
1436 * (Obviously the caller must pass a parsetree that is okay to modify when
1437 * using this flag.) Note that this flag does not affect whether the tlist is
1438 * considered to be a legal match to the result type, only how we react to
1439 * allowed not-exact-match cases. *modifyTargetList will be set true iff
1440 * we had to make any "dangerous" changes that could modify the semantics of
1441 * the statement. If it is set true, the caller should not use the modified
1442 * statement, but for simplicity we apply the changes anyway.
1444 * If junkFilter isn't NULL, then *junkFilter is set to a JunkFilter defined
1445 * to convert the function's tuple result to the correct output tuple type.
1446 * Exception: if the function is defined to return VOID then *junkFilter is
1450 check_sql_fn_retval(Oid func_id, Oid rettype, List *queryTreeList,
1451 bool *modifyTargetList,
1452 JunkFilter **junkFilter)
1462 AssertArg(!IsPolymorphicType(rettype));
1464 if (modifyTargetList)
1465 *modifyTargetList = false; /* initialize for no change */
1467 *junkFilter = NULL; /* initialize in case of VOID result */
1470 * Find the last canSetTag query in the list. This isn't necessarily the
1471 * last parsetree, because rule rewriting can insert queries after what
1475 foreach(lc, queryTreeList)
1477 Query *q = (Query *) lfirst(lc);
1484 * If it's a plain SELECT, it returns whatever the targetlist says.
1485 * Otherwise, if it's INSERT/UPDATE/DELETE with RETURNING, it returns
1486 * that. Otherwise, the function return type must be VOID.
1488 * Note: eventually replace this test with QueryReturnsTuples? We'd need
1489 * a more general method of determining the output type, though. Also, it
1490 * seems too dangerous to consider FETCH or EXECUTE as returning a
1491 * determinable rowtype, since they depend on relatively short-lived
1495 parse->commandType == CMD_SELECT &&
1496 parse->utilityStmt == NULL)
1498 tlist_ptr = &parse->targetList;
1499 tlist = parse->targetList;
1502 (parse->commandType == CMD_INSERT ||
1503 parse->commandType == CMD_UPDATE ||
1504 parse->commandType == CMD_DELETE) &&
1505 parse->returningList)
1507 tlist_ptr = &parse->returningList;
1508 tlist = parse->returningList;
1512 /* Empty function body, or last statement is a utility command */
1513 if (rettype != VOIDOID)
1515 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1516 errmsg("return type mismatch in function declared to return %s",
1517 format_type_be(rettype)),
1518 errdetail("Function's final statement must be SELECT or INSERT/UPDATE/DELETE RETURNING.")));
1523 * OK, check that the targetlist returns something matching the declared
1524 * type. (We used to insist that the declared type not be VOID in this
1525 * case, but that makes it hard to write a void function that exits after
1526 * calling another void function. Instead, we insist that the tlist
1527 * return void ... so void is treated as if it were a scalar type below.)
1531 * Count the non-junk entries in the result targetlist.
1533 tlistlen = ExecCleanTargetListLength(tlist);
1535 fn_typtype = get_typtype(rettype);
1537 if (fn_typtype == TYPTYPE_BASE ||
1538 fn_typtype == TYPTYPE_DOMAIN ||
1539 fn_typtype == TYPTYPE_ENUM ||
1540 fn_typtype == TYPTYPE_RANGE ||
1544 * For scalar-type returns, the target list must have exactly one
1545 * non-junk entry, and its type must agree with what the user
1546 * declared; except we allow binary-compatible types too.
1552 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1553 errmsg("return type mismatch in function declared to return %s",
1554 format_type_be(rettype)),
1555 errdetail("Final statement must return exactly one column.")));
1557 /* We assume here that non-junk TLEs must come first in tlists */
1558 tle = (TargetEntry *) linitial(tlist);
1559 Assert(!tle->resjunk);
1561 restype = exprType((Node *) tle->expr);
1562 if (!IsBinaryCoercible(restype, rettype))
1564 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1565 errmsg("return type mismatch in function declared to return %s",
1566 format_type_be(rettype)),
1567 errdetail("Actual return type is %s.",
1568 format_type_be(restype))));
1569 if (modifyTargetList && restype != rettype)
1571 tle->expr = (Expr *) makeRelabelType(tle->expr,
1574 get_typcollation(rettype),
1576 /* Relabel is dangerous if TLE is a sort/group or setop column */
1577 if (tle->ressortgroupref != 0 || parse->setOperations)
1578 *modifyTargetList = true;
1581 /* Set up junk filter if needed */
1583 *junkFilter = ExecInitJunkFilter(tlist, false, NULL);
1585 else if (fn_typtype == TYPTYPE_COMPOSITE || rettype == RECORDOID)
1587 /* Returns a rowtype */
1589 int tupnatts; /* physical number of columns in tuple */
1590 int tuplogcols; /* # of nondeleted columns in tuple */
1591 int colindex; /* physical column index */
1592 List *newtlist; /* new non-junk tlist entries */
1593 List *junkattrs; /* new junk tlist entries */
1596 * If the target list is of length 1, and the type of the varnode in
1597 * the target list matches the declared return type, this is okay.
1598 * This can happen, for example, where the body of the function is
1599 * 'SELECT func2()', where func2 has the same composite return type as
1600 * the function that's calling it.
1602 * XXX Note that if rettype is RECORD, the IsBinaryCoercible check
1603 * will succeed for any composite restype. For the moment we rely on
1604 * runtime type checking to catch any discrepancy, but it'd be nice to
1605 * do better at parse time.
1609 TargetEntry *tle = (TargetEntry *) linitial(tlist);
1611 Assert(!tle->resjunk);
1612 restype = exprType((Node *) tle->expr);
1613 if (IsBinaryCoercible(restype, rettype))
1615 if (modifyTargetList && restype != rettype)
1617 tle->expr = (Expr *) makeRelabelType(tle->expr,
1620 get_typcollation(rettype),
1622 /* Relabel is dangerous if sort/group or setop column */
1623 if (tle->ressortgroupref != 0 || parse->setOperations)
1624 *modifyTargetList = true;
1626 /* Set up junk filter if needed */
1628 *junkFilter = ExecInitJunkFilter(tlist, false, NULL);
1629 return false; /* NOT returning whole tuple */
1633 /* Is the rowtype fixed, or determined only at runtime? */
1634 if (get_func_result_type(func_id, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1637 * Assume we are returning the whole tuple. Crosschecking against
1638 * what the caller expects will happen at runtime.
1641 *junkFilter = ExecInitJunkFilter(tlist, false, NULL);
1647 * Verify that the targetlist matches the return tuple type. We scan
1648 * the non-deleted attributes to ensure that they match the datatypes
1649 * of the non-resjunk columns. For deleted attributes, insert NULL
1650 * result columns if the caller asked for that.
1652 tupnatts = tupdesc->natts;
1653 tuplogcols = 0; /* we'll count nondeleted cols as we go */
1655 newtlist = NIL; /* these are only used if modifyTargetList */
1660 TargetEntry *tle = (TargetEntry *) lfirst(lc);
1661 Form_pg_attribute attr;
1667 if (modifyTargetList)
1668 junkattrs = lappend(junkattrs, tle);
1675 if (colindex > tupnatts)
1677 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1678 errmsg("return type mismatch in function declared to return %s",
1679 format_type_be(rettype)),
1680 errdetail("Final statement returns too many columns.")));
1681 attr = tupdesc->attrs[colindex - 1];
1682 if (attr->attisdropped && modifyTargetList)
1686 /* The type of the null we insert isn't important */
1687 null_expr = (Expr *) makeConst(INT4OID,
1694 newtlist = lappend(newtlist,
1695 makeTargetEntry(null_expr,
1699 /* NULL insertion is dangerous in a setop */
1700 if (parse->setOperations)
1701 *modifyTargetList = true;
1703 } while (attr->attisdropped);
1706 tletype = exprType((Node *) tle->expr);
1707 atttype = attr->atttypid;
1708 if (!IsBinaryCoercible(tletype, atttype))
1710 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1711 errmsg("return type mismatch in function declared to return %s",
1712 format_type_be(rettype)),
1713 errdetail("Final statement returns %s instead of %s at column %d.",
1714 format_type_be(tletype),
1715 format_type_be(atttype),
1717 if (modifyTargetList)
1719 if (tletype != atttype)
1721 tle->expr = (Expr *) makeRelabelType(tle->expr,
1724 get_typcollation(atttype),
1726 /* Relabel is dangerous if sort/group or setop column */
1727 if (tle->ressortgroupref != 0 || parse->setOperations)
1728 *modifyTargetList = true;
1730 tle->resno = colindex;
1731 newtlist = lappend(newtlist, tle);
1735 /* remaining columns in tupdesc had better all be dropped */
1736 for (colindex++; colindex <= tupnatts; colindex++)
1738 if (!tupdesc->attrs[colindex - 1]->attisdropped)
1740 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1741 errmsg("return type mismatch in function declared to return %s",
1742 format_type_be(rettype)),
1743 errdetail("Final statement returns too few columns.")));
1744 if (modifyTargetList)
1748 /* The type of the null we insert isn't important */
1749 null_expr = (Expr *) makeConst(INT4OID,
1756 newtlist = lappend(newtlist,
1757 makeTargetEntry(null_expr,
1761 /* NULL insertion is dangerous in a setop */
1762 if (parse->setOperations)
1763 *modifyTargetList = true;
1767 if (modifyTargetList)
1769 /* ensure resjunk columns are numbered correctly */
1770 foreach(lc, junkattrs)
1772 TargetEntry *tle = (TargetEntry *) lfirst(lc);
1774 tle->resno = colindex++;
1776 /* replace the tlist with the modified one */
1777 *tlist_ptr = list_concat(newtlist, junkattrs);
1780 /* Set up junk filter if needed */
1782 *junkFilter = ExecInitJunkFilterConversion(tlist,
1783 CreateTupleDescCopy(tupdesc),
1786 /* Report that we are returning entire tuple result */
1791 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1792 errmsg("return type %s is not supported for SQL functions",
1793 format_type_be(rettype))));
1800 * CreateSQLFunctionDestReceiver -- create a suitable DestReceiver object
1803 CreateSQLFunctionDestReceiver(void)
1805 DR_sqlfunction *self = (DR_sqlfunction *) palloc0(sizeof(DR_sqlfunction));
1807 self->pub.receiveSlot = sqlfunction_receive;
1808 self->pub.rStartup = sqlfunction_startup;
1809 self->pub.rShutdown = sqlfunction_shutdown;
1810 self->pub.rDestroy = sqlfunction_destroy;
1811 self->pub.mydest = DestSQLFunction;
1813 /* private fields will be set by postquel_start */
1815 return (DestReceiver *) self;
1819 * sqlfunction_startup --- executor startup
1822 sqlfunction_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
1828 * sqlfunction_receive --- receive one tuple
1831 sqlfunction_receive(TupleTableSlot *slot, DestReceiver *self)
1833 DR_sqlfunction *myState = (DR_sqlfunction *) self;
1835 /* Filter tuple as needed */
1836 slot = ExecFilterJunk(myState->filter, slot);
1838 /* Store the filtered tuple into the tuplestore */
1839 tuplestore_puttupleslot(myState->tstore, slot);
1843 * sqlfunction_shutdown --- executor end
1846 sqlfunction_shutdown(DestReceiver *self)
1852 * sqlfunction_destroy --- release DestReceiver object
1855 sqlfunction_destroy(DestReceiver *self)