1 /*-------------------------------------------------------------------------
4 * Execution of SQL-language functions
6 * Portions Copyright (c) 1996-2018, 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 "storage/proc.h"
29 #include "tcop/utility.h"
30 #include "utils/builtins.h"
31 #include "utils/datum.h"
32 #include "utils/lsyscache.h"
33 #include "utils/memutils.h"
34 #include "utils/snapmgr.h"
35 #include "utils/syscache.h"
39 * Specialized DestReceiver for collecting query output in a SQL function
43 DestReceiver pub; /* publicly-known function pointers */
44 Tuplestorestate *tstore; /* where to put result tuples */
45 MemoryContext cxt; /* context containing tstore */
46 JunkFilter *filter; /* filter to convert tuple type */
50 * We have an execution_state record for each query in a function. Each
51 * record contains a plantree for its query. If the query is currently in
52 * F_EXEC_RUN state then there's a QueryDesc too.
54 * The "next" fields chain together all the execution_state records generated
55 * from a single original parsetree. (There will only be more than one in
56 * case of rule expansion of the original parsetree.)
60 F_EXEC_START, F_EXEC_RUN, F_EXEC_DONE
63 typedef struct execution_state
65 struct execution_state *next;
67 bool setsResult; /* true if this query produces func's result */
68 bool lazyEval; /* true if should fetch one row at a time */
69 PlannedStmt *stmt; /* plan for this query */
70 QueryDesc *qd; /* null unless status == RUN */
75 * An SQLFunctionCache record is built during the first call,
76 * and linked to from the fn_extra field of the FmgrInfo struct.
78 * Note that currently this has only the lifespan of the calling query.
79 * Someday we should rewrite this code to use plancache.c to save parse/plan
80 * results for longer than that.
82 * Physically, though, the data has the lifespan of the FmgrInfo that's used
83 * to call the function, and there are cases (particularly with indexes)
84 * where the FmgrInfo might survive across transactions. We cannot assume
85 * that the parse/plan trees are good for longer than the (sub)transaction in
86 * which parsing was done, so we must mark the record with the LXID/subxid of
87 * its creation time, and regenerate everything if that's obsolete. To avoid
88 * memory leakage when we do have to regenerate things, all the data is kept
89 * in a sub-context of the FmgrInfo's fn_mcxt.
93 char *fname; /* function name (for error msgs) */
94 char *src; /* function body text (for error msgs) */
96 SQLFunctionParseInfoPtr pinfo; /* data for parser callback hooks */
98 Oid rettype; /* actual return type */
99 int16 typlen; /* length of the return type */
100 bool typbyval; /* true if return type is pass by value */
101 bool returnsSet; /* true if returning multiple rows */
102 bool returnsTuple; /* true if returning whole tuple result */
103 bool shutdown_reg; /* true if registered shutdown callback */
104 bool readonly_func; /* true to run in "read only" mode */
105 bool lazyEval; /* true if using lazyEval for result query */
107 ParamListInfo paramLI; /* Param list representing current args */
109 Tuplestorestate *tstore; /* where we accumulate result tuples */
111 JunkFilter *junkFilter; /* will be NULL if function returns VOID */
114 * func_state is a List of execution_state records, each of which is the
115 * first for its original parsetree, with any additional records chained
116 * to it via the "next" fields. This sublist structure is needed to keep
117 * track of where the original query boundaries are.
121 MemoryContext fcontext; /* memory context holding this struct and all
124 LocalTransactionId lxid; /* lxid in which cache was made */
125 SubTransactionId subxid; /* subxid in which cache was made */
128 typedef SQLFunctionCache *SQLFunctionCachePtr;
131 * Data structure needed by the parser callback hooks to resolve parameter
132 * references during parsing of a SQL function's body. This is separate from
133 * SQLFunctionCache since we sometimes do parsing separately from execution.
135 typedef struct SQLFunctionParseInfo
137 char *fname; /* function's name */
138 int nargs; /* number of input arguments */
139 Oid *argtypes; /* resolved types of input arguments */
140 char **argnames; /* names of input arguments; NULL if none */
141 /* Note that argnames[i] can be NULL, if some args are unnamed */
142 Oid collation; /* function's input collation, if known */
143 } SQLFunctionParseInfo;
146 /* non-export function prototypes */
147 static Node *sql_fn_param_ref(ParseState *pstate, ParamRef *pref);
148 static Node *sql_fn_post_column_ref(ParseState *pstate,
149 ColumnRef *cref, Node *var);
150 static Node *sql_fn_make_param(SQLFunctionParseInfoPtr pinfo,
151 int paramno, int location);
152 static Node *sql_fn_resolve_param_name(SQLFunctionParseInfoPtr pinfo,
153 const char *paramname, int location);
154 static List *init_execution_state(List *queryTree_list,
155 SQLFunctionCachePtr fcache,
157 static void init_sql_fcache(FmgrInfo *finfo, Oid collation, bool lazyEvalOK);
158 static void postquel_start(execution_state *es, SQLFunctionCachePtr fcache);
159 static bool postquel_getnext(execution_state *es, SQLFunctionCachePtr fcache);
160 static void postquel_end(execution_state *es);
161 static void postquel_sub_params(SQLFunctionCachePtr fcache,
162 FunctionCallInfo fcinfo);
163 static Datum postquel_get_single_result(TupleTableSlot *slot,
164 FunctionCallInfo fcinfo,
165 SQLFunctionCachePtr fcache,
166 MemoryContext resultcontext);
167 static void sql_exec_error_callback(void *arg);
168 static void ShutdownSQLFunction(Datum arg);
169 static void sqlfunction_startup(DestReceiver *self, int operation, TupleDesc typeinfo);
170 static bool sqlfunction_receive(TupleTableSlot *slot, DestReceiver *self);
171 static void sqlfunction_shutdown(DestReceiver *self);
172 static void sqlfunction_destroy(DestReceiver *self);
176 * Prepare the SQLFunctionParseInfo struct for parsing a SQL function body
178 * This includes resolving actual types of polymorphic arguments.
180 * call_expr can be passed as NULL, but then we will fail if there are any
181 * polymorphic arguments.
183 SQLFunctionParseInfoPtr
184 prepare_sql_fn_parse_info(HeapTuple procedureTuple,
188 SQLFunctionParseInfoPtr pinfo;
189 Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
192 pinfo = (SQLFunctionParseInfoPtr) palloc0(sizeof(SQLFunctionParseInfo));
194 /* Function's name (only) can be used to qualify argument names */
195 pinfo->fname = pstrdup(NameStr(procedureStruct->proname));
197 /* Save the function's input collation */
198 pinfo->collation = inputCollation;
201 * Copy input argument types from the pg_proc entry, then resolve any
204 pinfo->nargs = nargs = procedureStruct->pronargs;
210 argOidVect = (Oid *) palloc(nargs * sizeof(Oid));
212 procedureStruct->proargtypes.values,
213 nargs * sizeof(Oid));
215 for (argnum = 0; argnum < nargs; argnum++)
217 Oid argtype = argOidVect[argnum];
219 if (IsPolymorphicType(argtype))
221 argtype = get_call_expr_argtype(call_expr, argnum);
222 if (argtype == InvalidOid)
224 (errcode(ERRCODE_DATATYPE_MISMATCH),
225 errmsg("could not determine actual type of argument declared %s",
226 format_type_be(argOidVect[argnum]))));
227 argOidVect[argnum] = argtype;
231 pinfo->argtypes = argOidVect;
235 * Collect names of arguments, too, if any
244 proargnames = SysCacheGetAttr(PROCNAMEARGSNSP, procedureTuple,
245 Anum_pg_proc_proargnames,
248 proargnames = PointerGetDatum(NULL); /* just to be sure */
250 proargmodes = SysCacheGetAttr(PROCNAMEARGSNSP, procedureTuple,
251 Anum_pg_proc_proargmodes,
254 proargmodes = PointerGetDatum(NULL); /* just to be sure */
256 n_arg_names = get_func_input_arg_names(proargnames, proargmodes,
259 /* Paranoia: ignore the result if too few array entries */
260 if (n_arg_names < nargs)
261 pinfo->argnames = NULL;
264 pinfo->argnames = NULL;
270 * Parser setup hook for parsing a SQL function body.
273 sql_fn_parser_setup(struct ParseState *pstate, SQLFunctionParseInfoPtr pinfo)
275 pstate->p_pre_columnref_hook = NULL;
276 pstate->p_post_columnref_hook = sql_fn_post_column_ref;
277 pstate->p_paramref_hook = sql_fn_param_ref;
278 /* no need to use p_coerce_param_hook */
279 pstate->p_ref_hook_state = (void *) pinfo;
283 * sql_fn_post_column_ref parser callback for ColumnRefs
286 sql_fn_post_column_ref(ParseState *pstate, ColumnRef *cref, Node *var)
288 SQLFunctionParseInfoPtr pinfo = (SQLFunctionParseInfoPtr) pstate->p_ref_hook_state;
291 Node *subfield = NULL;
293 const char *name2 = NULL;
297 * Never override a table-column reference. This corresponds to
298 * considering the parameter names to appear in a scope outside the
299 * individual SQL commands, which is what we want.
305 * The allowed syntaxes are:
307 * A A = parameter name
308 * A.B A = function name, B = parameter name
309 * OR: A = record-typed parameter name, B = field name
310 * (the first possibility takes precedence)
311 * A.B.C A = function name, B = record-typed parameter name,
313 * A.* Whole-row reference to composite parameter A.
314 * A.B.* Same, with A = function name, B = parameter name
316 * Here, it's sufficient to ignore the "*" in the last two cases --- the
317 * main parser will take care of expanding the whole-row reference.
320 nnames = list_length(cref->fields);
325 if (IsA(llast(cref->fields), A_Star))
328 field1 = (Node *) linitial(cref->fields);
329 Assert(IsA(field1, String));
330 name1 = strVal(field1);
333 subfield = (Node *) lsecond(cref->fields);
334 Assert(IsA(subfield, String));
335 name2 = strVal(subfield);
341 * Three-part name: if the first part doesn't match the function name,
342 * we can fail immediately. Otherwise, look up the second part, and
343 * take the third part to be a field reference.
345 if (strcmp(name1, pinfo->fname) != 0)
348 param = sql_fn_resolve_param_name(pinfo, name2, cref->location);
350 subfield = (Node *) lthird(cref->fields);
351 Assert(IsA(subfield, String));
353 else if (nnames == 2 && strcmp(name1, pinfo->fname) == 0)
356 * Two-part name with first part matching function name: first see if
357 * second part matches any parameter name.
359 param = sql_fn_resolve_param_name(pinfo, name2, cref->location);
363 /* Yes, so this is a parameter reference, no subfield */
368 /* No, so try to match as parameter name and subfield */
369 param = sql_fn_resolve_param_name(pinfo, name1, cref->location);
374 /* Single name, or parameter name followed by subfield */
375 param = sql_fn_resolve_param_name(pinfo, name1, cref->location);
379 return NULL; /* No match */
384 * Must be a reference to a field of a composite parameter; otherwise
385 * ParseFuncOrColumn will return NULL, and we'll fail back at the
388 param = ParseFuncOrColumn(pstate,
389 list_make1(subfield),
401 * sql_fn_param_ref parser callback for ParamRefs ($n symbols)
404 sql_fn_param_ref(ParseState *pstate, ParamRef *pref)
406 SQLFunctionParseInfoPtr pinfo = (SQLFunctionParseInfoPtr) pstate->p_ref_hook_state;
407 int paramno = pref->number;
409 /* Check parameter number is valid */
410 if (paramno <= 0 || paramno > pinfo->nargs)
411 return NULL; /* unknown parameter number */
413 return sql_fn_make_param(pinfo, paramno, pref->location);
417 * sql_fn_make_param construct a Param node for the given paramno
420 sql_fn_make_param(SQLFunctionParseInfoPtr pinfo,
421 int paramno, int location)
425 param = makeNode(Param);
426 param->paramkind = PARAM_EXTERN;
427 param->paramid = paramno;
428 param->paramtype = pinfo->argtypes[paramno - 1];
429 param->paramtypmod = -1;
430 param->paramcollid = get_typcollation(param->paramtype);
431 param->location = location;
434 * If we have a function input collation, allow it to override the
435 * type-derived collation for parameter symbols. (XXX perhaps this should
436 * not happen if the type collation is not default?)
438 if (OidIsValid(pinfo->collation) && OidIsValid(param->paramcollid))
439 param->paramcollid = pinfo->collation;
441 return (Node *) param;
445 * Search for a function parameter of the given name; if there is one,
446 * construct and return a Param node for it. If not, return NULL.
447 * Helper function for sql_fn_post_column_ref.
450 sql_fn_resolve_param_name(SQLFunctionParseInfoPtr pinfo,
451 const char *paramname, int location)
455 if (pinfo->argnames == NULL)
458 for (i = 0; i < pinfo->nargs; i++)
460 if (pinfo->argnames[i] && strcmp(pinfo->argnames[i], paramname) == 0)
461 return sql_fn_make_param(pinfo, i + 1, location);
468 * Set up the per-query execution_state records for a SQL function.
470 * The input is a List of Lists of parsed and rewritten, but not planned,
471 * querytrees. The sublist structure denotes the original query boundaries.
474 init_execution_state(List *queryTree_list,
475 SQLFunctionCachePtr fcache,
479 execution_state *lasttages = NULL;
482 foreach(lc1, queryTree_list)
484 List *qtlist = lfirst_node(List, lc1);
485 execution_state *firstes = NULL;
486 execution_state *preves = NULL;
491 Query *queryTree = lfirst_node(Query, lc2);
493 execution_state *newes;
495 /* Plan the query if needed */
496 if (queryTree->commandType == CMD_UTILITY)
498 /* Utility commands require no planning. */
499 stmt = makeNode(PlannedStmt);
500 stmt->commandType = CMD_UTILITY;
501 stmt->canSetTag = queryTree->canSetTag;
502 stmt->utilityStmt = queryTree->utilityStmt;
503 stmt->stmt_location = queryTree->stmt_location;
504 stmt->stmt_len = queryTree->stmt_len;
507 stmt = pg_plan_query(queryTree,
508 CURSOR_OPT_PARALLEL_OK,
512 * Precheck all commands for validity in a function. This should
513 * generally match the restrictions spi.c applies.
515 if (stmt->commandType == CMD_UTILITY)
517 if (IsA(stmt->utilityStmt, CopyStmt) &&
518 ((CopyStmt *) stmt->utilityStmt)->filename == NULL)
520 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
521 errmsg("cannot COPY to/from client in a SQL function")));
523 if (IsA(stmt->utilityStmt, TransactionStmt))
525 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
526 /* translator: %s is a SQL statement name */
527 errmsg("%s is not allowed in a SQL function",
528 CreateCommandTag(stmt->utilityStmt))));
531 if (fcache->readonly_func && !CommandIsReadOnly(stmt))
533 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
534 /* translator: %s is a SQL statement name */
535 errmsg("%s is not allowed in a non-volatile function",
536 CreateCommandTag((Node *) stmt))));
538 if (IsInParallelMode() && !CommandIsReadOnly(stmt))
539 PreventCommandIfParallelMode(CreateCommandTag((Node *) stmt));
541 /* OK, build the execution_state for this query */
542 newes = (execution_state *) palloc(sizeof(execution_state));
544 preves->next = newes;
549 newes->status = F_EXEC_START;
550 newes->setsResult = false; /* might change below */
551 newes->lazyEval = false; /* might change below */
555 if (queryTree->canSetTag)
561 eslist = lappend(eslist, firstes);
565 * Mark the last canSetTag query as delivering the function result; then,
566 * if it is a plain SELECT, mark it for lazy evaluation. If it's not a
567 * SELECT we must always run it to completion.
569 * Note: at some point we might add additional criteria for whether to use
570 * lazy eval. However, we should prefer to use it whenever the function
571 * doesn't return set, since fetching more than one row is useless in that
574 * Note: don't set setsResult if the function returns VOID, as evidenced
575 * by not having made a junkfilter. This ensures we'll throw away any
576 * output from the last statement in such a function.
578 if (lasttages && fcache->junkFilter)
580 lasttages->setsResult = true;
582 lasttages->stmt->commandType == CMD_SELECT &&
583 !lasttages->stmt->hasModifyingCTE)
584 fcache->lazyEval = lasttages->lazyEval = true;
591 * Initialize the SQLFunctionCache for a SQL function
594 init_sql_fcache(FmgrInfo *finfo, Oid collation, bool lazyEvalOK)
596 Oid foid = finfo->fn_oid;
597 MemoryContext fcontext;
598 MemoryContext oldcontext;
600 HeapTuple procedureTuple;
601 Form_pg_proc procedureStruct;
602 SQLFunctionCachePtr fcache;
603 List *raw_parsetree_list;
604 List *queryTree_list;
605 List *flat_query_list;
611 * Create memory context that holds all the SQLFunctionCache data. It
612 * must be a child of whatever context holds the FmgrInfo.
614 fcontext = AllocSetContextCreate(finfo->fn_mcxt,
616 ALLOCSET_DEFAULT_SIZES);
618 oldcontext = MemoryContextSwitchTo(fcontext);
621 * Create the struct proper, link it to fcontext and fn_extra. Once this
622 * is done, we'll be able to recover the memory after failure, even if the
623 * FmgrInfo is long-lived.
625 fcache = (SQLFunctionCachePtr) palloc0(sizeof(SQLFunctionCache));
626 fcache->fcontext = fcontext;
627 finfo->fn_extra = (void *) fcache;
630 * get the procedure tuple corresponding to the given function Oid
632 procedureTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(foid));
633 if (!HeapTupleIsValid(procedureTuple))
634 elog(ERROR, "cache lookup failed for function %u", foid);
635 procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
638 * copy function name immediately for use by error reporting callback, and
639 * for use as memory context identifier
641 fcache->fname = pstrdup(NameStr(procedureStruct->proname));
642 MemoryContextSetIdentifier(fcontext, fcache->fname);
645 * get the result type from the procedure tuple, and check for polymorphic
646 * result type; if so, find out the actual result type.
648 rettype = procedureStruct->prorettype;
650 if (IsPolymorphicType(rettype))
652 rettype = get_fn_expr_rettype(finfo);
653 if (rettype == InvalidOid) /* this probably should not happen */
655 (errcode(ERRCODE_DATATYPE_MISMATCH),
656 errmsg("could not determine actual result type for function declared to return type %s",
657 format_type_be(procedureStruct->prorettype))));
660 fcache->rettype = rettype;
662 /* Fetch the typlen and byval info for the result type */
663 get_typlenbyval(rettype, &fcache->typlen, &fcache->typbyval);
665 /* Remember whether we're returning setof something */
666 fcache->returnsSet = procedureStruct->proretset;
668 /* Remember if function is STABLE/IMMUTABLE */
669 fcache->readonly_func =
670 (procedureStruct->provolatile != PROVOLATILE_VOLATILE);
673 * We need the actual argument types to pass to the parser. Also make
674 * sure that parameter symbols are considered to have the function's
675 * resolved input collation.
677 fcache->pinfo = prepare_sql_fn_parse_info(procedureTuple,
682 * And of course we need the function body text.
684 tmp = SysCacheGetAttr(PROCOID,
689 elog(ERROR, "null prosrc for function %u", foid);
690 fcache->src = TextDatumGetCString(tmp);
693 * Parse and rewrite the queries in the function text. Use sublists to
694 * keep track of the original query boundaries. But we also build a
695 * "flat" list of the rewritten queries to pass to check_sql_fn_retval.
696 * This is because the last canSetTag query determines the result type
697 * independently of query boundaries --- and it might not be in the last
698 * sublist, for example if the last query rewrites to DO INSTEAD NOTHING.
699 * (It might not be unreasonable to throw an error in such a case, but
700 * this is the historical behavior and it doesn't seem worth changing.)
702 * Note: since parsing and planning is done in fcontext, we will generate
703 * a lot of cruft that lives as long as the fcache does. This is annoying
704 * but we'll not worry about it until the module is rewritten to use
707 raw_parsetree_list = pg_parse_query(fcache->src);
709 queryTree_list = NIL;
710 flat_query_list = NIL;
711 foreach(lc, raw_parsetree_list)
713 RawStmt *parsetree = lfirst_node(RawStmt, lc);
714 List *queryTree_sublist;
716 queryTree_sublist = pg_analyze_and_rewrite_params(parsetree,
718 (ParserSetupHook) sql_fn_parser_setup,
721 queryTree_list = lappend(queryTree_list, queryTree_sublist);
722 flat_query_list = list_concat(flat_query_list,
723 list_copy(queryTree_sublist));
726 check_sql_fn_statements(flat_query_list);
729 * Check that the function returns the type it claims to. Although in
730 * simple cases this was already done when the function was defined, we
731 * have to recheck because database objects used in the function's queries
732 * might have changed type. We'd have to do it anyway if the function had
733 * any polymorphic arguments.
735 * Note: we set fcache->returnsTuple according to whether we are returning
736 * the whole tuple result or just a single column. In the latter case we
737 * clear returnsTuple because we need not act different from the scalar
738 * result case, even if it's a rowtype column. (However, we have to force
739 * lazy eval mode in that case; otherwise we'd need extra code to expand
740 * the rowtype column into multiple columns, since we have no way to
741 * notify the caller that it should do that.)
743 * check_sql_fn_retval will also construct a JunkFilter we can use to
744 * coerce the returned rowtype to the desired form (unless the result type
745 * is VOID, in which case there's nothing to coerce to).
747 fcache->returnsTuple = check_sql_fn_retval(foid,
751 &fcache->junkFilter);
753 if (fcache->returnsTuple)
755 /* Make sure output rowtype is properly blessed */
756 BlessTupleDesc(fcache->junkFilter->jf_resultSlot->tts_tupleDescriptor);
758 else if (fcache->returnsSet && type_is_rowtype(fcache->rettype))
761 * Returning rowtype as if it were scalar --- materialize won't work.
762 * Right now it's sufficient to override any caller preference for
763 * materialize mode, but to add more smarts in init_execution_state
764 * about this, we'd probably need a three-way flag instead of bool.
769 /* Finally, plan the queries */
770 fcache->func_state = init_execution_state(queryTree_list,
774 /* Mark fcache with time of creation to show it's valid */
775 fcache->lxid = MyProc->lxid;
776 fcache->subxid = GetCurrentSubTransactionId();
778 ReleaseSysCache(procedureTuple);
780 MemoryContextSwitchTo(oldcontext);
783 /* Start up execution of one execution_state node */
785 postquel_start(execution_state *es, SQLFunctionCachePtr fcache)
789 Assert(es->qd == NULL);
791 /* Caller should have ensured a suitable snapshot is active */
792 Assert(ActiveSnapshotSet());
795 * If this query produces the function result, send its output to the
796 * tuplestore; else discard any output.
800 DR_sqlfunction *myState;
802 dest = CreateDestReceiver(DestSQLFunction);
803 /* pass down the needed info to the dest receiver routines */
804 myState = (DR_sqlfunction *) dest;
805 Assert(myState->pub.mydest == DestSQLFunction);
806 myState->tstore = fcache->tstore;
807 myState->cxt = CurrentMemoryContext;
808 myState->filter = fcache->junkFilter;
811 dest = None_Receiver;
813 es->qd = CreateQueryDesc(es->stmt,
819 es->qd ? es->qd->queryEnv : NULL,
822 /* Utility commands don't need Executor. */
823 if (es->qd->operation != CMD_UTILITY)
826 * In lazyEval mode, do not let the executor set up an AfterTrigger
827 * context. This is necessary not just an optimization, because we
828 * mustn't exit from the function execution with a stacked
829 * AfterTrigger level still active. We are careful not to select
830 * lazyEval mode for any statement that could possibly queue triggers.
835 eflags = EXEC_FLAG_SKIP_TRIGGERS;
837 eflags = 0; /* default run-to-completion flags */
838 ExecutorStart(es->qd, eflags);
841 es->status = F_EXEC_RUN;
844 /* Run one execution_state; either to completion or to first result row */
845 /* Returns true if we ran to completion */
847 postquel_getnext(execution_state *es, SQLFunctionCachePtr fcache)
851 if (es->qd->operation == CMD_UTILITY)
853 ProcessUtility(es->qd->plannedstmt,
855 PROCESS_UTILITY_QUERY,
860 result = true; /* never stops early */
864 /* Run regular commands to completion unless lazyEval */
865 uint64 count = (es->lazyEval) ? 1 : 0;
867 ExecutorRun(es->qd, ForwardScanDirection, count, !fcache->returnsSet || !es->lazyEval);
870 * If we requested run to completion OR there was no tuple returned,
871 * command must be complete.
873 result = (count == 0 || es->qd->estate->es_processed == 0);
879 /* Shut down execution of one execution_state node */
881 postquel_end(execution_state *es)
883 /* mark status done to ensure we don't do ExecutorEnd twice */
884 es->status = F_EXEC_DONE;
886 /* Utility commands don't need Executor. */
887 if (es->qd->operation != CMD_UTILITY)
889 ExecutorFinish(es->qd);
893 es->qd->dest->rDestroy(es->qd->dest);
895 FreeQueryDesc(es->qd);
899 /* Build ParamListInfo array representing current arguments */
901 postquel_sub_params(SQLFunctionCachePtr fcache,
902 FunctionCallInfo fcinfo)
904 int nargs = fcinfo->nargs;
908 ParamListInfo paramLI;
911 if (fcache->paramLI == NULL)
913 paramLI = (ParamListInfo)
914 palloc(offsetof(ParamListInfoData, params) +
915 nargs * sizeof(ParamExternData));
916 /* we have static list of params, so no hooks needed */
917 paramLI->paramFetch = NULL;
918 paramLI->paramFetchArg = NULL;
919 paramLI->paramCompile = NULL;
920 paramLI->paramCompileArg = NULL;
921 paramLI->parserSetup = NULL;
922 paramLI->parserSetupArg = NULL;
923 paramLI->numParams = nargs;
924 fcache->paramLI = paramLI;
928 paramLI = fcache->paramLI;
929 Assert(paramLI->numParams == nargs);
932 for (i = 0; i < nargs; i++)
934 ParamExternData *prm = ¶mLI->params[i];
936 prm->value = fcinfo->arg[i];
937 prm->isnull = fcinfo->argnull[i];
939 prm->ptype = fcache->pinfo->argtypes[i];
943 fcache->paramLI = NULL;
947 * Extract the SQL function's value from a single result row. This is used
948 * both for scalar (non-set) functions and for each row of a lazy-eval set
952 postquel_get_single_result(TupleTableSlot *slot,
953 FunctionCallInfo fcinfo,
954 SQLFunctionCachePtr fcache,
955 MemoryContext resultcontext)
958 MemoryContext oldcontext;
961 * Set up to return the function value. For pass-by-reference datatypes,
962 * be sure to allocate the result in resultcontext, not the current memory
963 * context (which has query lifespan). We can't leave the data in the
964 * TupleTableSlot because we intend to clear the slot before returning.
966 oldcontext = MemoryContextSwitchTo(resultcontext);
968 if (fcache->returnsTuple)
970 /* We must return the whole tuple as a Datum. */
971 fcinfo->isnull = false;
972 value = ExecFetchSlotTupleDatum(slot);
977 * Returning a scalar, which we have to extract from the first column
978 * of the SELECT result, and then copy into result context if needed.
980 value = slot_getattr(slot, 1, &(fcinfo->isnull));
983 value = datumCopy(value, fcache->typbyval, fcache->typlen);
986 MemoryContextSwitchTo(oldcontext);
992 * fmgr_sql: function call manager for SQL functions
995 fmgr_sql(PG_FUNCTION_ARGS)
997 SQLFunctionCachePtr fcache;
998 ErrorContextCallback sqlerrcontext;
999 MemoryContext oldcontext;
1003 bool pushed_snapshot;
1004 execution_state *es;
1005 TupleTableSlot *slot;
1011 * Setup error traceback support for ereport()
1013 sqlerrcontext.callback = sql_exec_error_callback;
1014 sqlerrcontext.arg = fcinfo->flinfo;
1015 sqlerrcontext.previous = error_context_stack;
1016 error_context_stack = &sqlerrcontext;
1018 /* Check call context */
1019 if (fcinfo->flinfo->fn_retset)
1021 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
1024 * For simplicity, we require callers to support both set eval modes.
1025 * There are cases where we must use one or must use the other, and
1026 * it's not really worthwhile to postpone the check till we know. But
1027 * note we do not require caller to provide an expectedDesc.
1029 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
1030 (rsi->allowedModes & SFRM_ValuePerCall) == 0 ||
1031 (rsi->allowedModes & SFRM_Materialize) == 0)
1033 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1034 errmsg("set-valued function called in context that cannot accept a set")));
1035 randomAccess = rsi->allowedModes & SFRM_Materialize_Random;
1036 lazyEvalOK = !(rsi->allowedModes & SFRM_Materialize_Preferred);
1040 randomAccess = false;
1045 * Initialize fcache (build plans) if first time through; or re-initialize
1046 * if the cache is stale.
1048 fcache = (SQLFunctionCachePtr) fcinfo->flinfo->fn_extra;
1052 if (fcache->lxid != MyProc->lxid ||
1053 !SubTransactionIsActive(fcache->subxid))
1055 /* It's stale; unlink and delete */
1056 fcinfo->flinfo->fn_extra = NULL;
1057 MemoryContextDelete(fcache->fcontext);
1064 init_sql_fcache(fcinfo->flinfo, PG_GET_COLLATION(), lazyEvalOK);
1065 fcache = (SQLFunctionCachePtr) fcinfo->flinfo->fn_extra;
1069 * Switch to context in which the fcache lives. This ensures that our
1070 * tuplestore etc will have sufficient lifetime. The sub-executor is
1071 * responsible for deleting per-tuple information. (XXX in the case of a
1072 * long-lived FmgrInfo, this policy represents more memory leakage, but
1073 * it's not entirely clear where to keep stuff instead.)
1075 oldcontext = MemoryContextSwitchTo(fcache->fcontext);
1078 * Find first unfinished query in function, and note whether it's the
1081 eslist = fcache->func_state;
1084 foreach(eslc, eslist)
1086 es = (execution_state *) lfirst(eslc);
1088 while (es && es->status == F_EXEC_DONE)
1099 * Convert params to appropriate format if starting a fresh execution. (If
1100 * continuing execution, we can re-use prior params.)
1102 if (is_first && es && es->status == F_EXEC_START)
1103 postquel_sub_params(fcache, fcinfo);
1106 * Build tuplestore to hold results, if we don't have one already. Note
1107 * it's in the query-lifespan context.
1109 if (!fcache->tstore)
1110 fcache->tstore = tuplestore_begin_heap(randomAccess, false, work_mem);
1113 * Execute each command in the function one after another until we either
1114 * run out of commands or get a result row from a lazily-evaluated SELECT.
1116 * Notes about snapshot management:
1118 * In a read-only function, we just use the surrounding query's snapshot.
1120 * In a non-read-only function, we rely on the fact that we'll never
1121 * suspend execution between queries of the function: the only reason to
1122 * suspend execution before completion is if we are returning a row from a
1123 * lazily-evaluated SELECT. So, when first entering this loop, we'll
1124 * either start a new query (and push a fresh snapshot) or re-establish
1125 * the active snapshot from the existing query descriptor. If we need to
1126 * start a new query in a subsequent execution of the loop, either we need
1127 * a fresh snapshot (and pushed_snapshot is false) or the existing
1128 * snapshot is on the active stack and we can just bump its command ID.
1130 pushed_snapshot = false;
1135 if (es->status == F_EXEC_START)
1138 * If not read-only, be sure to advance the command counter for
1139 * each command, so that all work to date in this transaction is
1140 * visible. Take a new snapshot if we don't have one yet,
1141 * otherwise just bump the command ID in the existing snapshot.
1143 if (!fcache->readonly_func)
1145 CommandCounterIncrement();
1146 if (!pushed_snapshot)
1148 PushActiveSnapshot(GetTransactionSnapshot());
1149 pushed_snapshot = true;
1152 UpdateActiveSnapshotCommandId();
1155 postquel_start(es, fcache);
1157 else if (!fcache->readonly_func && !pushed_snapshot)
1159 /* Re-establish active snapshot when re-entering function */
1160 PushActiveSnapshot(es->qd->snapshot);
1161 pushed_snapshot = true;
1164 completed = postquel_getnext(es, fcache);
1167 * If we ran the command to completion, we can shut it down now. Any
1168 * row(s) we need to return are safely stashed in the tuplestore, and
1169 * we want to be sure that, for example, AFTER triggers get fired
1170 * before we return anything. Also, if the function doesn't return
1171 * set, we can shut it down anyway because it must be a SELECT and we
1172 * don't care about fetching any more result rows.
1174 if (completed || !fcache->returnsSet)
1178 * Break from loop if we didn't shut down (implying we got a
1179 * lazily-evaluated row). Otherwise we'll press on till the whole
1180 * function is done, relying on the tuplestore to keep hold of the
1181 * data to eventually be returned. This is necessary since an
1182 * INSERT/UPDATE/DELETE RETURNING that sets the result might be
1183 * followed by additional rule-inserted commands, and we want to
1184 * finish doing all those commands before we return anything.
1186 if (es->status != F_EXEC_DONE)
1190 * Advance to next execution_state, which might be in the next list.
1197 break; /* end of function */
1199 es = (execution_state *) lfirst(eslc);
1202 * Flush the current snapshot so that we will take a new one for
1203 * the new query list. This ensures that new snaps are taken at
1204 * original-query boundaries, matching the behavior of interactive
1207 if (pushed_snapshot)
1209 PopActiveSnapshot();
1210 pushed_snapshot = false;
1216 * The tuplestore now contains whatever row(s) we are supposed to return.
1218 if (fcache->returnsSet)
1220 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
1225 * If we stopped short of being done, we must have a lazy-eval
1228 Assert(es->lazyEval);
1229 /* Re-use the junkfilter's output slot to fetch back the tuple */
1230 Assert(fcache->junkFilter);
1231 slot = fcache->junkFilter->jf_resultSlot;
1232 if (!tuplestore_gettupleslot(fcache->tstore, true, false, slot))
1233 elog(ERROR, "failed to fetch lazy-eval tuple");
1234 /* Extract the result as a datum, and copy out from the slot */
1235 result = postquel_get_single_result(slot, fcinfo,
1236 fcache, oldcontext);
1237 /* Clear the tuplestore, but keep it for next time */
1238 /* NB: this might delete the slot's content, but we don't care */
1239 tuplestore_clear(fcache->tstore);
1242 * Let caller know we're not finished.
1244 rsi->isDone = ExprMultipleResult;
1247 * Ensure we will get shut down cleanly if the exprcontext is not
1248 * run to completion.
1250 if (!fcache->shutdown_reg)
1252 RegisterExprContextCallback(rsi->econtext,
1253 ShutdownSQLFunction,
1254 PointerGetDatum(fcache));
1255 fcache->shutdown_reg = true;
1258 else if (fcache->lazyEval)
1261 * We are done with a lazy evaluation. Clean up.
1263 tuplestore_clear(fcache->tstore);
1266 * Let caller know we're finished.
1268 rsi->isDone = ExprEndResult;
1270 fcinfo->isnull = true;
1273 /* Deregister shutdown callback, if we made one */
1274 if (fcache->shutdown_reg)
1276 UnregisterExprContextCallback(rsi->econtext,
1277 ShutdownSQLFunction,
1278 PointerGetDatum(fcache));
1279 fcache->shutdown_reg = false;
1285 * We are done with a non-lazy evaluation. Return whatever is in
1286 * the tuplestore. (It is now caller's responsibility to free the
1287 * tuplestore when done.)
1289 rsi->returnMode = SFRM_Materialize;
1290 rsi->setResult = fcache->tstore;
1291 fcache->tstore = NULL;
1292 /* must copy desc because execSRF.c will free it */
1293 if (fcache->junkFilter)
1294 rsi->setDesc = CreateTupleDescCopy(fcache->junkFilter->jf_cleanTupType);
1296 fcinfo->isnull = true;
1299 /* Deregister shutdown callback, if we made one */
1300 if (fcache->shutdown_reg)
1302 UnregisterExprContextCallback(rsi->econtext,
1303 ShutdownSQLFunction,
1304 PointerGetDatum(fcache));
1305 fcache->shutdown_reg = false;
1312 * Non-set function. If we got a row, return it; else return NULL.
1314 if (fcache->junkFilter)
1316 /* Re-use the junkfilter's output slot to fetch back the tuple */
1317 slot = fcache->junkFilter->jf_resultSlot;
1318 if (tuplestore_gettupleslot(fcache->tstore, true, false, slot))
1319 result = postquel_get_single_result(slot, fcinfo,
1320 fcache, oldcontext);
1323 fcinfo->isnull = true;
1329 /* Should only get here for VOID functions and procedures */
1330 Assert(fcache->rettype == VOIDOID);
1331 fcinfo->isnull = true;
1335 /* Clear the tuplestore, but keep it for next time */
1336 tuplestore_clear(fcache->tstore);
1339 /* Pop snapshot if we have pushed one */
1340 if (pushed_snapshot)
1341 PopActiveSnapshot();
1344 * If we've gone through every command in the function, we are done. Reset
1345 * the execution states to start over again on next call.
1349 foreach(eslc, fcache->func_state)
1351 es = (execution_state *) lfirst(eslc);
1354 es->status = F_EXEC_START;
1360 error_context_stack = sqlerrcontext.previous;
1362 MemoryContextSwitchTo(oldcontext);
1369 * error context callback to let us supply a call-stack traceback
1372 sql_exec_error_callback(void *arg)
1374 FmgrInfo *flinfo = (FmgrInfo *) arg;
1375 SQLFunctionCachePtr fcache = (SQLFunctionCachePtr) flinfo->fn_extra;
1376 int syntaxerrposition;
1379 * We can do nothing useful if init_sql_fcache() didn't get as far as
1380 * saving the function name
1382 if (fcache == NULL || fcache->fname == NULL)
1386 * If there is a syntax error position, convert to internal syntax error
1388 syntaxerrposition = geterrposition();
1389 if (syntaxerrposition > 0 && fcache->src != NULL)
1392 internalerrposition(syntaxerrposition);
1393 internalerrquery(fcache->src);
1397 * Try to determine where in the function we failed. If there is a query
1398 * with non-null QueryDesc, finger it. (We check this rather than looking
1399 * for F_EXEC_RUN state, so that errors during ExecutorStart or
1400 * ExecutorEnd are blamed on the appropriate query; see postquel_start and
1403 if (fcache->func_state)
1405 execution_state *es;
1411 foreach(lc, fcache->func_state)
1413 es = (execution_state *) lfirst(lc);
1418 errcontext("SQL function \"%s\" statement %d",
1419 fcache->fname, query_num);
1431 * couldn't identify a running query; might be function entry,
1432 * function exit, or between queries.
1434 errcontext("SQL function \"%s\"", fcache->fname);
1440 * Assume we failed during init_sql_fcache(). (It's possible that the
1441 * function actually has an empty body, but in that case we may as
1442 * well report all errors as being "during startup".)
1444 errcontext("SQL function \"%s\" during startup", fcache->fname);
1450 * callback function in case a function-returning-set needs to be shut down
1451 * before it has been run to completion
1454 ShutdownSQLFunction(Datum arg)
1456 SQLFunctionCachePtr fcache = (SQLFunctionCachePtr) DatumGetPointer(arg);
1457 execution_state *es;
1460 foreach(lc, fcache->func_state)
1462 es = (execution_state *) lfirst(lc);
1465 /* Shut down anything still running */
1466 if (es->status == F_EXEC_RUN)
1468 /* Re-establish active snapshot for any called functions */
1469 if (!fcache->readonly_func)
1470 PushActiveSnapshot(es->qd->snapshot);
1474 if (!fcache->readonly_func)
1475 PopActiveSnapshot();
1478 /* Reset states to START in case we're called again */
1479 es->status = F_EXEC_START;
1484 /* Release tuplestore if we have one */
1486 tuplestore_end(fcache->tstore);
1487 fcache->tstore = NULL;
1489 /* execUtils will deregister the callback... */
1490 fcache->shutdown_reg = false;
1494 * check_sql_fn_statements
1496 * Check statements in an SQL function. Error out if there is anything that
1497 * is not acceptable.
1500 check_sql_fn_statements(List *queryTreeList)
1504 foreach(lc, queryTreeList)
1506 Query *query = lfirst_node(Query, lc);
1509 * Disallow procedures with output arguments. The current
1510 * implementation would just throw the output values away, unless the
1511 * statement is the last one. Per SQL standard, we should assign the
1512 * output values by name. By disallowing this here, we preserve an
1513 * opportunity for future improvement.
1515 if (query->commandType == CMD_UTILITY &&
1516 IsA(query->utilityStmt, CallStmt))
1518 CallStmt *stmt = castNode(CallStmt, query->utilityStmt);
1526 tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(stmt->funcexpr->funcid));
1527 if (!HeapTupleIsValid(tuple))
1528 elog(ERROR, "cache lookup failed for function %u", stmt->funcexpr->funcid);
1529 numargs = get_func_arg_info(tuple, &argtypes, &argnames, &argmodes);
1530 ReleaseSysCache(tuple);
1532 for (i = 0; i < numargs; i++)
1534 if (argmodes && (argmodes[i] == PROARGMODE_INOUT || argmodes[i] == PROARGMODE_OUT))
1536 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1537 errmsg("calling procedures with output arguments is not supported in SQL functions")));
1544 * check_sql_fn_retval() -- check return value of a list of sql parse trees.
1546 * The return value of a sql function is the value returned by the last
1547 * canSetTag query in the function. We do some ad-hoc type checking here
1548 * to be sure that the user is returning the type he claims. There are
1549 * also a couple of strange-looking features to assist callers in dealing
1550 * with allowed special cases, such as binary-compatible result types.
1552 * For a polymorphic function the passed rettype must be the actual resolved
1553 * output type of the function; we should never see a polymorphic pseudotype
1554 * such as ANYELEMENT as rettype. (This means we can't check the type during
1555 * function definition of a polymorphic function.)
1557 * This function returns true if the sql function returns the entire tuple
1558 * result of its final statement, or false if it returns just the first column
1559 * result of that statement. It throws an error if the final statement doesn't
1560 * return the right type at all.
1562 * Note that because we allow "SELECT rowtype_expression", the result can be
1563 * false even when the declared function return type is a rowtype.
1565 * If modifyTargetList isn't NULL, the function will modify the final
1566 * statement's targetlist in two cases:
1567 * (1) if the tlist returns values that are binary-coercible to the expected
1568 * type rather than being exactly the expected type. RelabelType nodes will
1569 * be inserted to make the result types match exactly.
1570 * (2) if there are dropped columns in the declared result rowtype. NULL
1571 * output columns will be inserted in the tlist to match them.
1572 * (Obviously the caller must pass a parsetree that is okay to modify when
1573 * using this flag.) Note that this flag does not affect whether the tlist is
1574 * considered to be a legal match to the result type, only how we react to
1575 * allowed not-exact-match cases. *modifyTargetList will be set true iff
1576 * we had to make any "dangerous" changes that could modify the semantics of
1577 * the statement. If it is set true, the caller should not use the modified
1578 * statement, but for simplicity we apply the changes anyway.
1580 * If junkFilter isn't NULL, then *junkFilter is set to a JunkFilter defined
1581 * to convert the function's tuple result to the correct output tuple type.
1582 * Exception: if the function is defined to return VOID then *junkFilter is
1586 check_sql_fn_retval(Oid func_id, Oid rettype, List *queryTreeList,
1587 bool *modifyTargetList,
1588 JunkFilter **junkFilter)
1598 AssertArg(!IsPolymorphicType(rettype));
1600 if (modifyTargetList)
1601 *modifyTargetList = false; /* initialize for no change */
1603 *junkFilter = NULL; /* initialize in case of VOID result */
1606 * If it's declared to return VOID, we don't care what's in the function.
1607 * (This takes care of the procedure case, as well.)
1609 if (rettype == VOIDOID)
1613 * Find the last canSetTag query in the list. This isn't necessarily the
1614 * last parsetree, because rule rewriting can insert queries after what
1618 foreach(lc, queryTreeList)
1620 Query *q = lfirst_node(Query, lc);
1627 * If it's a plain SELECT, it returns whatever the targetlist says.
1628 * Otherwise, if it's INSERT/UPDATE/DELETE with RETURNING, it returns
1629 * that. Otherwise, the function return type must be VOID.
1631 * Note: eventually replace this test with QueryReturnsTuples? We'd need
1632 * a more general method of determining the output type, though. Also, it
1633 * seems too dangerous to consider FETCH or EXECUTE as returning a
1634 * determinable rowtype, since they depend on relatively short-lived
1638 parse->commandType == CMD_SELECT)
1640 tlist_ptr = &parse->targetList;
1641 tlist = parse->targetList;
1644 (parse->commandType == CMD_INSERT ||
1645 parse->commandType == CMD_UPDATE ||
1646 parse->commandType == CMD_DELETE) &&
1647 parse->returningList)
1649 tlist_ptr = &parse->returningList;
1650 tlist = parse->returningList;
1654 /* Empty function body, or last statement is a utility command */
1656 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1657 errmsg("return type mismatch in function declared to return %s",
1658 format_type_be(rettype)),
1659 errdetail("Function's final statement must be SELECT or INSERT/UPDATE/DELETE RETURNING.")));
1660 return false; /* keep compiler quiet */
1664 * OK, check that the targetlist returns something matching the declared
1669 * Count the non-junk entries in the result targetlist.
1671 tlistlen = ExecCleanTargetListLength(tlist);
1673 fn_typtype = get_typtype(rettype);
1675 if (fn_typtype == TYPTYPE_BASE ||
1676 fn_typtype == TYPTYPE_DOMAIN ||
1677 fn_typtype == TYPTYPE_ENUM ||
1678 fn_typtype == TYPTYPE_RANGE)
1681 * For scalar-type returns, the target list must have exactly one
1682 * non-junk entry, and its type must agree with what the user
1683 * declared; except we allow binary-compatible types too.
1689 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1690 errmsg("return type mismatch in function declared to return %s",
1691 format_type_be(rettype)),
1692 errdetail("Final statement must return exactly one column.")));
1694 /* We assume here that non-junk TLEs must come first in tlists */
1695 tle = (TargetEntry *) linitial(tlist);
1696 Assert(!tle->resjunk);
1698 restype = exprType((Node *) tle->expr);
1699 if (!IsBinaryCoercible(restype, rettype))
1701 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1702 errmsg("return type mismatch in function declared to return %s",
1703 format_type_be(rettype)),
1704 errdetail("Actual return type is %s.",
1705 format_type_be(restype))));
1706 if (modifyTargetList && restype != rettype)
1708 tle->expr = (Expr *) makeRelabelType(tle->expr,
1711 get_typcollation(rettype),
1712 COERCE_IMPLICIT_CAST);
1713 /* Relabel is dangerous if TLE is a sort/group or setop column */
1714 if (tle->ressortgroupref != 0 || parse->setOperations)
1715 *modifyTargetList = true;
1718 /* Set up junk filter if needed */
1720 *junkFilter = ExecInitJunkFilter(tlist, false, NULL);
1722 else if (fn_typtype == TYPTYPE_COMPOSITE || rettype == RECORDOID)
1725 * Returns a rowtype.
1727 * Note that we will not consider a domain over composite to be a
1728 * "rowtype" return type; it goes through the scalar case above. This
1729 * is because SQL functions don't provide any implicit casting to the
1730 * result type, so there is no way to produce a domain-over-composite
1731 * result except by computing it as an explicit single-column result.
1734 int tupnatts; /* physical number of columns in tuple */
1735 int tuplogcols; /* # of nondeleted columns in tuple */
1736 int colindex; /* physical column index */
1737 List *newtlist; /* new non-junk tlist entries */
1738 List *junkattrs; /* new junk tlist entries */
1741 * If the target list is of length 1, and the type of the varnode in
1742 * the target list matches the declared return type, this is okay.
1743 * This can happen, for example, where the body of the function is
1744 * 'SELECT func2()', where func2 has the same composite return type as
1745 * the function that's calling it.
1747 * XXX Note that if rettype is RECORD, the IsBinaryCoercible check
1748 * will succeed for any composite restype. For the moment we rely on
1749 * runtime type checking to catch any discrepancy, but it'd be nice to
1750 * do better at parse time.
1754 TargetEntry *tle = (TargetEntry *) linitial(tlist);
1756 Assert(!tle->resjunk);
1757 restype = exprType((Node *) tle->expr);
1758 if (IsBinaryCoercible(restype, rettype))
1760 if (modifyTargetList && restype != rettype)
1762 tle->expr = (Expr *) makeRelabelType(tle->expr,
1765 get_typcollation(rettype),
1766 COERCE_IMPLICIT_CAST);
1767 /* Relabel is dangerous if sort/group or setop column */
1768 if (tle->ressortgroupref != 0 || parse->setOperations)
1769 *modifyTargetList = true;
1771 /* Set up junk filter if needed */
1773 *junkFilter = ExecInitJunkFilter(tlist, false, NULL);
1774 return false; /* NOT returning whole tuple */
1779 * Is the rowtype fixed, or determined only at runtime? (Note we
1780 * cannot see TYPEFUNC_COMPOSITE_DOMAIN here.)
1782 if (get_func_result_type(func_id, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1785 * Assume we are returning the whole tuple. Crosschecking against
1786 * what the caller expects will happen at runtime.
1789 *junkFilter = ExecInitJunkFilter(tlist, false, NULL);
1795 * Verify that the targetlist matches the return tuple type. We scan
1796 * the non-deleted attributes to ensure that they match the datatypes
1797 * of the non-resjunk columns. For deleted attributes, insert NULL
1798 * result columns if the caller asked for that.
1800 tupnatts = tupdesc->natts;
1801 tuplogcols = 0; /* we'll count nondeleted cols as we go */
1803 newtlist = NIL; /* these are only used if modifyTargetList */
1808 TargetEntry *tle = (TargetEntry *) lfirst(lc);
1809 Form_pg_attribute attr;
1815 if (modifyTargetList)
1816 junkattrs = lappend(junkattrs, tle);
1823 if (colindex > tupnatts)
1825 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1826 errmsg("return type mismatch in function declared to return %s",
1827 format_type_be(rettype)),
1828 errdetail("Final statement returns too many columns.")));
1829 attr = TupleDescAttr(tupdesc, colindex - 1);
1830 if (attr->attisdropped && modifyTargetList)
1834 /* The type of the null we insert isn't important */
1835 null_expr = (Expr *) makeConst(INT4OID,
1842 newtlist = lappend(newtlist,
1843 makeTargetEntry(null_expr,
1847 /* NULL insertion is dangerous in a setop */
1848 if (parse->setOperations)
1849 *modifyTargetList = true;
1851 } while (attr->attisdropped);
1854 tletype = exprType((Node *) tle->expr);
1855 atttype = attr->atttypid;
1856 if (!IsBinaryCoercible(tletype, atttype))
1858 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1859 errmsg("return type mismatch in function declared to return %s",
1860 format_type_be(rettype)),
1861 errdetail("Final statement returns %s instead of %s at column %d.",
1862 format_type_be(tletype),
1863 format_type_be(atttype),
1865 if (modifyTargetList)
1867 if (tletype != atttype)
1869 tle->expr = (Expr *) makeRelabelType(tle->expr,
1872 get_typcollation(atttype),
1873 COERCE_IMPLICIT_CAST);
1874 /* Relabel is dangerous if sort/group or setop column */
1875 if (tle->ressortgroupref != 0 || parse->setOperations)
1876 *modifyTargetList = true;
1878 tle->resno = colindex;
1879 newtlist = lappend(newtlist, tle);
1883 /* remaining columns in tupdesc had better all be dropped */
1884 for (colindex++; colindex <= tupnatts; colindex++)
1886 if (!TupleDescAttr(tupdesc, colindex - 1)->attisdropped)
1888 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1889 errmsg("return type mismatch in function declared to return %s",
1890 format_type_be(rettype)),
1891 errdetail("Final statement returns too few columns.")));
1892 if (modifyTargetList)
1896 /* The type of the null we insert isn't important */
1897 null_expr = (Expr *) makeConst(INT4OID,
1904 newtlist = lappend(newtlist,
1905 makeTargetEntry(null_expr,
1909 /* NULL insertion is dangerous in a setop */
1910 if (parse->setOperations)
1911 *modifyTargetList = true;
1915 if (modifyTargetList)
1917 /* ensure resjunk columns are numbered correctly */
1918 foreach(lc, junkattrs)
1920 TargetEntry *tle = (TargetEntry *) lfirst(lc);
1922 tle->resno = colindex++;
1924 /* replace the tlist with the modified one */
1925 *tlist_ptr = list_concat(newtlist, junkattrs);
1928 /* Set up junk filter if needed */
1930 *junkFilter = ExecInitJunkFilterConversion(tlist,
1931 CreateTupleDescCopy(tupdesc),
1934 /* Report that we are returning entire tuple result */
1939 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1940 errmsg("return type %s is not supported for SQL functions",
1941 format_type_be(rettype))));
1948 * CreateSQLFunctionDestReceiver -- create a suitable DestReceiver object
1951 CreateSQLFunctionDestReceiver(void)
1953 DR_sqlfunction *self = (DR_sqlfunction *) palloc0(sizeof(DR_sqlfunction));
1955 self->pub.receiveSlot = sqlfunction_receive;
1956 self->pub.rStartup = sqlfunction_startup;
1957 self->pub.rShutdown = sqlfunction_shutdown;
1958 self->pub.rDestroy = sqlfunction_destroy;
1959 self->pub.mydest = DestSQLFunction;
1961 /* private fields will be set by postquel_start */
1963 return (DestReceiver *) self;
1967 * sqlfunction_startup --- executor startup
1970 sqlfunction_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
1976 * sqlfunction_receive --- receive one tuple
1979 sqlfunction_receive(TupleTableSlot *slot, DestReceiver *self)
1981 DR_sqlfunction *myState = (DR_sqlfunction *) self;
1983 /* Filter tuple as needed */
1984 slot = ExecFilterJunk(myState->filter, slot);
1986 /* Store the filtered tuple into the tuplestore */
1987 tuplestore_puttupleslot(myState->tstore, slot);
1993 * sqlfunction_shutdown --- executor end
1996 sqlfunction_shutdown(DestReceiver *self)
2002 * sqlfunction_destroy --- release DestReceiver object
2005 sqlfunction_destroy(DestReceiver *self)