1 /*-------------------------------------------------------------------------
4 * Execution of SQL-language functions
6 * Portions Copyright (c) 1996-2017, 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 Node *stmt; /* PlannedStmt or utility statement */
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),
399 * sql_fn_param_ref parser callback for ParamRefs ($n symbols)
402 sql_fn_param_ref(ParseState *pstate, ParamRef *pref)
404 SQLFunctionParseInfoPtr pinfo = (SQLFunctionParseInfoPtr) pstate->p_ref_hook_state;
405 int paramno = pref->number;
407 /* Check parameter number is valid */
408 if (paramno <= 0 || paramno > pinfo->nargs)
409 return NULL; /* unknown parameter number */
411 return sql_fn_make_param(pinfo, paramno, pref->location);
415 * sql_fn_make_param construct a Param node for the given paramno
418 sql_fn_make_param(SQLFunctionParseInfoPtr pinfo,
419 int paramno, int location)
423 param = makeNode(Param);
424 param->paramkind = PARAM_EXTERN;
425 param->paramid = paramno;
426 param->paramtype = pinfo->argtypes[paramno - 1];
427 param->paramtypmod = -1;
428 param->paramcollid = get_typcollation(param->paramtype);
429 param->location = location;
432 * If we have a function input collation, allow it to override the
433 * type-derived collation for parameter symbols. (XXX perhaps this should
434 * not happen if the type collation is not default?)
436 if (OidIsValid(pinfo->collation) && OidIsValid(param->paramcollid))
437 param->paramcollid = pinfo->collation;
439 return (Node *) param;
443 * Search for a function parameter of the given name; if there is one,
444 * construct and return a Param node for it. If not, return NULL.
445 * Helper function for sql_fn_post_column_ref.
448 sql_fn_resolve_param_name(SQLFunctionParseInfoPtr pinfo,
449 const char *paramname, int location)
453 if (pinfo->argnames == NULL)
456 for (i = 0; i < pinfo->nargs; i++)
458 if (pinfo->argnames[i] && strcmp(pinfo->argnames[i], paramname) == 0)
459 return sql_fn_make_param(pinfo, i + 1, location);
466 * Set up the per-query execution_state records for a SQL function.
468 * The input is a List of Lists of parsed and rewritten, but not planned,
469 * querytrees. The sublist structure denotes the original query boundaries.
472 init_execution_state(List *queryTree_list,
473 SQLFunctionCachePtr fcache,
477 execution_state *lasttages = NULL;
480 foreach(lc1, queryTree_list)
482 List *qtlist = (List *) lfirst(lc1);
483 execution_state *firstes = NULL;
484 execution_state *preves = NULL;
489 Query *queryTree = (Query *) lfirst(lc2);
491 execution_state *newes;
493 Assert(IsA(queryTree, Query));
495 /* Plan the query if needed */
496 if (queryTree->commandType == CMD_UTILITY)
497 stmt = queryTree->utilityStmt;
499 stmt = (Node *) pg_plan_query(queryTree,
500 fcache->readonly_func ? CURSOR_OPT_PARALLEL_OK : 0,
504 * Precheck all commands for validity in a function. This should
505 * generally match the restrictions spi.c applies.
507 if (IsA(stmt, CopyStmt) &&
508 ((CopyStmt *) stmt)->filename == NULL)
510 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
511 errmsg("cannot COPY to/from client in a SQL function")));
513 if (IsA(stmt, TransactionStmt))
515 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
516 /* translator: %s is a SQL statement name */
517 errmsg("%s is not allowed in a SQL function",
518 CreateCommandTag(stmt))));
520 if (fcache->readonly_func && !CommandIsReadOnly(stmt))
522 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
523 /* translator: %s is a SQL statement name */
524 errmsg("%s is not allowed in a non-volatile function",
525 CreateCommandTag(stmt))));
527 if (IsInParallelMode() && !CommandIsReadOnly(stmt))
528 PreventCommandIfParallelMode(CreateCommandTag(stmt));
530 /* OK, build the execution_state for this query */
531 newes = (execution_state *) palloc(sizeof(execution_state));
533 preves->next = newes;
538 newes->status = F_EXEC_START;
539 newes->setsResult = false; /* might change below */
540 newes->lazyEval = false; /* might change below */
544 if (queryTree->canSetTag)
550 eslist = lappend(eslist, firstes);
554 * Mark the last canSetTag query as delivering the function result; then,
555 * if it is a plain SELECT, mark it for lazy evaluation. If it's not a
556 * SELECT we must always run it to completion.
558 * Note: at some point we might add additional criteria for whether to use
559 * lazy eval. However, we should prefer to use it whenever the function
560 * doesn't return set, since fetching more than one row is useless in that
563 * Note: don't set setsResult if the function returns VOID, as evidenced
564 * by not having made a junkfilter. This ensures we'll throw away any
565 * output from a utility statement that check_sql_fn_retval deemed to not
568 if (lasttages && fcache->junkFilter)
570 lasttages->setsResult = true;
572 IsA(lasttages->stmt, PlannedStmt))
574 PlannedStmt *ps = (PlannedStmt *) lasttages->stmt;
576 if (ps->commandType == CMD_SELECT &&
577 ps->utilityStmt == NULL &&
578 !ps->hasModifyingCTE)
579 fcache->lazyEval = lasttages->lazyEval = true;
587 * Initialize the SQLFunctionCache for a SQL function
590 init_sql_fcache(FmgrInfo *finfo, Oid collation, bool lazyEvalOK)
592 Oid foid = finfo->fn_oid;
593 MemoryContext fcontext;
594 MemoryContext oldcontext;
596 HeapTuple procedureTuple;
597 Form_pg_proc procedureStruct;
598 SQLFunctionCachePtr fcache;
599 List *raw_parsetree_list;
600 List *queryTree_list;
601 List *flat_query_list;
607 * Create memory context that holds all the SQLFunctionCache data. It
608 * must be a child of whatever context holds the FmgrInfo.
610 fcontext = AllocSetContextCreate(finfo->fn_mcxt,
612 ALLOCSET_DEFAULT_SIZES);
614 oldcontext = MemoryContextSwitchTo(fcontext);
617 * Create the struct proper, link it to fcontext and fn_extra. Once this
618 * is done, we'll be able to recover the memory after failure, even if the
619 * FmgrInfo is long-lived.
621 fcache = (SQLFunctionCachePtr) palloc0(sizeof(SQLFunctionCache));
622 fcache->fcontext = fcontext;
623 finfo->fn_extra = (void *) fcache;
626 * get the procedure tuple corresponding to the given function Oid
628 procedureTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(foid));
629 if (!HeapTupleIsValid(procedureTuple))
630 elog(ERROR, "cache lookup failed for function %u", foid);
631 procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
634 * copy function name immediately for use by error reporting callback
636 fcache->fname = pstrdup(NameStr(procedureStruct->proname));
639 * get the result type from the procedure tuple, and check for polymorphic
640 * result type; if so, find out the actual result type.
642 rettype = procedureStruct->prorettype;
644 if (IsPolymorphicType(rettype))
646 rettype = get_fn_expr_rettype(finfo);
647 if (rettype == InvalidOid) /* this probably should not happen */
649 (errcode(ERRCODE_DATATYPE_MISMATCH),
650 errmsg("could not determine actual result type for function declared to return type %s",
651 format_type_be(procedureStruct->prorettype))));
654 fcache->rettype = rettype;
656 /* Fetch the typlen and byval info for the result type */
657 get_typlenbyval(rettype, &fcache->typlen, &fcache->typbyval);
659 /* Remember whether we're returning setof something */
660 fcache->returnsSet = procedureStruct->proretset;
662 /* Remember if function is STABLE/IMMUTABLE */
663 fcache->readonly_func =
664 (procedureStruct->provolatile != PROVOLATILE_VOLATILE);
667 * We need the actual argument types to pass to the parser. Also make
668 * sure that parameter symbols are considered to have the function's
669 * resolved input collation.
671 fcache->pinfo = prepare_sql_fn_parse_info(procedureTuple,
676 * And of course we need the function body text.
678 tmp = SysCacheGetAttr(PROCOID,
683 elog(ERROR, "null prosrc for function %u", foid);
684 fcache->src = TextDatumGetCString(tmp);
687 * Parse and rewrite the queries in the function text. Use sublists to
688 * keep track of the original query boundaries. But we also build a
689 * "flat" list of the rewritten queries to pass to check_sql_fn_retval.
690 * This is because the last canSetTag query determines the result type
691 * independently of query boundaries --- and it might not be in the last
692 * sublist, for example if the last query rewrites to DO INSTEAD NOTHING.
693 * (It might not be unreasonable to throw an error in such a case, but
694 * this is the historical behavior and it doesn't seem worth changing.)
696 * Note: since parsing and planning is done in fcontext, we will generate
697 * a lot of cruft that lives as long as the fcache does. This is annoying
698 * but we'll not worry about it until the module is rewritten to use
701 raw_parsetree_list = pg_parse_query(fcache->src);
703 queryTree_list = NIL;
704 flat_query_list = NIL;
705 foreach(lc, raw_parsetree_list)
707 Node *parsetree = (Node *) lfirst(lc);
708 List *queryTree_sublist;
710 queryTree_sublist = pg_analyze_and_rewrite_params(parsetree,
712 (ParserSetupHook) sql_fn_parser_setup,
714 queryTree_list = lappend(queryTree_list, queryTree_sublist);
715 flat_query_list = list_concat(flat_query_list,
716 list_copy(queryTree_sublist));
720 * Check that the function returns the type it claims to. Although in
721 * simple cases this was already done when the function was defined, we
722 * have to recheck because database objects used in the function's queries
723 * might have changed type. We'd have to do it anyway if the function had
724 * any polymorphic arguments.
726 * Note: we set fcache->returnsTuple according to whether we are returning
727 * the whole tuple result or just a single column. In the latter case we
728 * clear returnsTuple because we need not act different from the scalar
729 * result case, even if it's a rowtype column. (However, we have to force
730 * lazy eval mode in that case; otherwise we'd need extra code to expand
731 * the rowtype column into multiple columns, since we have no way to
732 * notify the caller that it should do that.)
734 * check_sql_fn_retval will also construct a JunkFilter we can use to
735 * coerce the returned rowtype to the desired form (unless the result type
736 * is VOID, in which case there's nothing to coerce to).
738 fcache->returnsTuple = check_sql_fn_retval(foid,
742 &fcache->junkFilter);
744 if (fcache->returnsTuple)
746 /* Make sure output rowtype is properly blessed */
747 BlessTupleDesc(fcache->junkFilter->jf_resultSlot->tts_tupleDescriptor);
749 else if (fcache->returnsSet && type_is_rowtype(fcache->rettype))
752 * Returning rowtype as if it were scalar --- materialize won't work.
753 * Right now it's sufficient to override any caller preference for
754 * materialize mode, but to add more smarts in init_execution_state
755 * about this, we'd probably need a three-way flag instead of bool.
760 /* Finally, plan the queries */
761 fcache->func_state = init_execution_state(queryTree_list,
765 /* Mark fcache with time of creation to show it's valid */
766 fcache->lxid = MyProc->lxid;
767 fcache->subxid = GetCurrentSubTransactionId();
769 ReleaseSysCache(procedureTuple);
771 MemoryContextSwitchTo(oldcontext);
774 /* Start up execution of one execution_state node */
776 postquel_start(execution_state *es, SQLFunctionCachePtr fcache)
780 Assert(es->qd == NULL);
782 /* Caller should have ensured a suitable snapshot is active */
783 Assert(ActiveSnapshotSet());
786 * If this query produces the function result, send its output to the
787 * tuplestore; else discard any output.
791 DR_sqlfunction *myState;
793 dest = CreateDestReceiver(DestSQLFunction);
794 /* pass down the needed info to the dest receiver routines */
795 myState = (DR_sqlfunction *) dest;
796 Assert(myState->pub.mydest == DestSQLFunction);
797 myState->tstore = fcache->tstore;
798 myState->cxt = CurrentMemoryContext;
799 myState->filter = fcache->junkFilter;
802 dest = None_Receiver;
804 if (IsA(es->stmt, PlannedStmt))
805 es->qd = CreateQueryDesc((PlannedStmt *) es->stmt,
812 es->qd = CreateUtilityQueryDesc(es->stmt,
818 /* Utility commands don't need Executor. */
819 if (es->qd->utilitystmt == NULL)
822 * In lazyEval mode, do not let the executor set up an AfterTrigger
823 * context. This is necessary not just an optimization, because we
824 * mustn't exit from the function execution with a stacked
825 * AfterTrigger level still active. We are careful not to select
826 * lazyEval mode for any statement that could possibly queue triggers.
831 eflags = EXEC_FLAG_SKIP_TRIGGERS;
833 eflags = 0; /* default run-to-completion flags */
834 ExecutorStart(es->qd, eflags);
837 es->status = F_EXEC_RUN;
840 /* Run one execution_state; either to completion or to first result row */
841 /* Returns true if we ran to completion */
843 postquel_getnext(execution_state *es, SQLFunctionCachePtr fcache)
847 if (es->qd->utilitystmt)
849 /* ProcessUtility needs the PlannedStmt for DECLARE CURSOR */
850 ProcessUtility((es->qd->plannedstmt ?
851 (Node *) es->qd->plannedstmt :
852 es->qd->utilitystmt),
854 PROCESS_UTILITY_QUERY,
858 result = true; /* never stops early */
862 /* Run regular commands to completion unless lazyEval */
863 uint64 count = (es->lazyEval) ? 1 : 0;
865 ExecutorRun(es->qd, ForwardScanDirection, count);
868 * If we requested run to completion OR there was no tuple returned,
869 * command must be complete.
871 result = (count == 0 || es->qd->estate->es_processed == 0);
877 /* Shut down execution of one execution_state node */
879 postquel_end(execution_state *es)
881 /* mark status done to ensure we don't do ExecutorEnd twice */
882 es->status = F_EXEC_DONE;
884 /* Utility commands don't need Executor. */
885 if (es->qd->utilitystmt == NULL)
887 ExecutorFinish(es->qd);
891 (*es->qd->dest->rDestroy) (es->qd->dest);
893 FreeQueryDesc(es->qd);
897 /* Build ParamListInfo array representing current arguments */
899 postquel_sub_params(SQLFunctionCachePtr fcache,
900 FunctionCallInfo fcinfo)
902 int nargs = fcinfo->nargs;
906 ParamListInfo paramLI;
909 if (fcache->paramLI == NULL)
911 paramLI = (ParamListInfo)
912 palloc(offsetof(ParamListInfoData, params) +
913 nargs * sizeof(ParamExternData));
914 /* we have static list of params, so no hooks needed */
915 paramLI->paramFetch = NULL;
916 paramLI->paramFetchArg = NULL;
917 paramLI->parserSetup = NULL;
918 paramLI->parserSetupArg = NULL;
919 paramLI->numParams = nargs;
920 paramLI->paramMask = NULL;
921 fcache->paramLI = paramLI;
925 paramLI = fcache->paramLI;
926 Assert(paramLI->numParams == nargs);
929 for (i = 0; i < nargs; i++)
931 ParamExternData *prm = ¶mLI->params[i];
933 prm->value = fcinfo->arg[i];
934 prm->isnull = fcinfo->argnull[i];
936 prm->ptype = fcache->pinfo->argtypes[i];
940 fcache->paramLI = NULL;
944 * Extract the SQL function's value from a single result row. This is used
945 * both for scalar (non-set) functions and for each row of a lazy-eval set
949 postquel_get_single_result(TupleTableSlot *slot,
950 FunctionCallInfo fcinfo,
951 SQLFunctionCachePtr fcache,
952 MemoryContext resultcontext)
955 MemoryContext oldcontext;
958 * Set up to return the function value. For pass-by-reference datatypes,
959 * be sure to allocate the result in resultcontext, not the current memory
960 * context (which has query lifespan). We can't leave the data in the
961 * TupleTableSlot because we intend to clear the slot before returning.
963 oldcontext = MemoryContextSwitchTo(resultcontext);
965 if (fcache->returnsTuple)
967 /* We must return the whole tuple as a Datum. */
968 fcinfo->isnull = false;
969 value = ExecFetchSlotTupleDatum(slot);
974 * Returning a scalar, which we have to extract from the first column
975 * of the SELECT result, and then copy into result context if needed.
977 value = slot_getattr(slot, 1, &(fcinfo->isnull));
980 value = datumCopy(value, fcache->typbyval, fcache->typlen);
983 MemoryContextSwitchTo(oldcontext);
989 * fmgr_sql: function call manager for SQL functions
992 fmgr_sql(PG_FUNCTION_ARGS)
994 SQLFunctionCachePtr fcache;
995 ErrorContextCallback sqlerrcontext;
996 MemoryContext oldcontext;
1000 bool pushed_snapshot;
1001 execution_state *es;
1002 TupleTableSlot *slot;
1008 * Setup error traceback support for ereport()
1010 sqlerrcontext.callback = sql_exec_error_callback;
1011 sqlerrcontext.arg = fcinfo->flinfo;
1012 sqlerrcontext.previous = error_context_stack;
1013 error_context_stack = &sqlerrcontext;
1015 /* Check call context */
1016 if (fcinfo->flinfo->fn_retset)
1018 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
1021 * For simplicity, we require callers to support both set eval modes.
1022 * There are cases where we must use one or must use the other, and
1023 * it's not really worthwhile to postpone the check till we know. But
1024 * note we do not require caller to provide an expectedDesc.
1026 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
1027 (rsi->allowedModes & SFRM_ValuePerCall) == 0 ||
1028 (rsi->allowedModes & SFRM_Materialize) == 0)
1030 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1031 errmsg("set-valued function called in context that cannot accept a set")));
1032 randomAccess = rsi->allowedModes & SFRM_Materialize_Random;
1033 lazyEvalOK = !(rsi->allowedModes & SFRM_Materialize_Preferred);
1037 randomAccess = false;
1042 * Initialize fcache (build plans) if first time through; or re-initialize
1043 * if the cache is stale.
1045 fcache = (SQLFunctionCachePtr) fcinfo->flinfo->fn_extra;
1049 if (fcache->lxid != MyProc->lxid ||
1050 !SubTransactionIsActive(fcache->subxid))
1052 /* It's stale; unlink and delete */
1053 fcinfo->flinfo->fn_extra = NULL;
1054 MemoryContextDelete(fcache->fcontext);
1061 init_sql_fcache(fcinfo->flinfo, PG_GET_COLLATION(), lazyEvalOK);
1062 fcache = (SQLFunctionCachePtr) fcinfo->flinfo->fn_extra;
1066 * Switch to context in which the fcache lives. This ensures that our
1067 * tuplestore etc will have sufficient lifetime. The sub-executor is
1068 * responsible for deleting per-tuple information. (XXX in the case of a
1069 * long-lived FmgrInfo, this policy represents more memory leakage, but
1070 * it's not entirely clear where to keep stuff instead.)
1072 oldcontext = MemoryContextSwitchTo(fcache->fcontext);
1075 * Find first unfinished query in function, and note whether it's the
1078 eslist = fcache->func_state;
1081 foreach(eslc, eslist)
1083 es = (execution_state *) lfirst(eslc);
1085 while (es && es->status == F_EXEC_DONE)
1096 * Convert params to appropriate format if starting a fresh execution. (If
1097 * continuing execution, we can re-use prior params.)
1099 if (is_first && es && es->status == F_EXEC_START)
1100 postquel_sub_params(fcache, fcinfo);
1103 * Build tuplestore to hold results, if we don't have one already. Note
1104 * it's in the query-lifespan context.
1106 if (!fcache->tstore)
1107 fcache->tstore = tuplestore_begin_heap(randomAccess, false, work_mem);
1110 * Execute each command in the function one after another until we either
1111 * run out of commands or get a result row from a lazily-evaluated SELECT.
1113 * Notes about snapshot management:
1115 * In a read-only function, we just use the surrounding query's snapshot.
1117 * In a non-read-only function, we rely on the fact that we'll never
1118 * suspend execution between queries of the function: the only reason to
1119 * suspend execution before completion is if we are returning a row from a
1120 * lazily-evaluated SELECT. So, when first entering this loop, we'll
1121 * either start a new query (and push a fresh snapshot) or re-establish
1122 * the active snapshot from the existing query descriptor. If we need to
1123 * start a new query in a subsequent execution of the loop, either we need
1124 * a fresh snapshot (and pushed_snapshot is false) or the existing
1125 * snapshot is on the active stack and we can just bump its command ID.
1127 pushed_snapshot = false;
1132 if (es->status == F_EXEC_START)
1135 * If not read-only, be sure to advance the command counter for
1136 * each command, so that all work to date in this transaction is
1137 * visible. Take a new snapshot if we don't have one yet,
1138 * otherwise just bump the command ID in the existing snapshot.
1140 if (!fcache->readonly_func)
1142 CommandCounterIncrement();
1143 if (!pushed_snapshot)
1145 PushActiveSnapshot(GetTransactionSnapshot());
1146 pushed_snapshot = true;
1149 UpdateActiveSnapshotCommandId();
1152 postquel_start(es, fcache);
1154 else if (!fcache->readonly_func && !pushed_snapshot)
1156 /* Re-establish active snapshot when re-entering function */
1157 PushActiveSnapshot(es->qd->snapshot);
1158 pushed_snapshot = true;
1161 completed = postquel_getnext(es, fcache);
1164 * If we ran the command to completion, we can shut it down now. Any
1165 * row(s) we need to return are safely stashed in the tuplestore, and
1166 * we want to be sure that, for example, AFTER triggers get fired
1167 * before we return anything. Also, if the function doesn't return
1168 * set, we can shut it down anyway because it must be a SELECT and we
1169 * don't care about fetching any more result rows.
1171 if (completed || !fcache->returnsSet)
1175 * Break from loop if we didn't shut down (implying we got a
1176 * lazily-evaluated row). Otherwise we'll press on till the whole
1177 * function is done, relying on the tuplestore to keep hold of the
1178 * data to eventually be returned. This is necessary since an
1179 * INSERT/UPDATE/DELETE RETURNING that sets the result might be
1180 * followed by additional rule-inserted commands, and we want to
1181 * finish doing all those commands before we return anything.
1183 if (es->status != F_EXEC_DONE)
1187 * Advance to next execution_state, which might be in the next list.
1194 break; /* end of function */
1196 es = (execution_state *) lfirst(eslc);
1199 * Flush the current snapshot so that we will take a new one for
1200 * the new query list. This ensures that new snaps are taken at
1201 * original-query boundaries, matching the behavior of interactive
1204 if (pushed_snapshot)
1206 PopActiveSnapshot();
1207 pushed_snapshot = false;
1213 * The tuplestore now contains whatever row(s) we are supposed to return.
1215 if (fcache->returnsSet)
1217 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
1222 * If we stopped short of being done, we must have a lazy-eval
1225 Assert(es->lazyEval);
1226 /* Re-use the junkfilter's output slot to fetch back the tuple */
1227 Assert(fcache->junkFilter);
1228 slot = fcache->junkFilter->jf_resultSlot;
1229 if (!tuplestore_gettupleslot(fcache->tstore, true, false, slot))
1230 elog(ERROR, "failed to fetch lazy-eval tuple");
1231 /* Extract the result as a datum, and copy out from the slot */
1232 result = postquel_get_single_result(slot, fcinfo,
1233 fcache, oldcontext);
1234 /* Clear the tuplestore, but keep it for next time */
1235 /* NB: this might delete the slot's content, but we don't care */
1236 tuplestore_clear(fcache->tstore);
1239 * Let caller know we're not finished.
1241 rsi->isDone = ExprMultipleResult;
1244 * Ensure we will get shut down cleanly if the exprcontext is not
1245 * run to completion.
1247 if (!fcache->shutdown_reg)
1249 RegisterExprContextCallback(rsi->econtext,
1250 ShutdownSQLFunction,
1251 PointerGetDatum(fcache));
1252 fcache->shutdown_reg = true;
1255 else if (fcache->lazyEval)
1258 * We are done with a lazy evaluation. Clean up.
1260 tuplestore_clear(fcache->tstore);
1263 * Let caller know we're finished.
1265 rsi->isDone = ExprEndResult;
1267 fcinfo->isnull = true;
1270 /* Deregister shutdown callback, if we made one */
1271 if (fcache->shutdown_reg)
1273 UnregisterExprContextCallback(rsi->econtext,
1274 ShutdownSQLFunction,
1275 PointerGetDatum(fcache));
1276 fcache->shutdown_reg = false;
1282 * We are done with a non-lazy evaluation. Return whatever is in
1283 * the tuplestore. (It is now caller's responsibility to free the
1284 * tuplestore when done.)
1286 rsi->returnMode = SFRM_Materialize;
1287 rsi->setResult = fcache->tstore;
1288 fcache->tstore = NULL;
1289 /* must copy desc because execQual will free it */
1290 if (fcache->junkFilter)
1291 rsi->setDesc = CreateTupleDescCopy(fcache->junkFilter->jf_cleanTupType);
1293 fcinfo->isnull = true;
1296 /* Deregister shutdown callback, if we made one */
1297 if (fcache->shutdown_reg)
1299 UnregisterExprContextCallback(rsi->econtext,
1300 ShutdownSQLFunction,
1301 PointerGetDatum(fcache));
1302 fcache->shutdown_reg = false;
1309 * Non-set function. If we got a row, return it; else return NULL.
1311 if (fcache->junkFilter)
1313 /* Re-use the junkfilter's output slot to fetch back the tuple */
1314 slot = fcache->junkFilter->jf_resultSlot;
1315 if (tuplestore_gettupleslot(fcache->tstore, true, false, slot))
1316 result = postquel_get_single_result(slot, fcinfo,
1317 fcache, oldcontext);
1320 fcinfo->isnull = true;
1326 /* Should only get here for VOID functions */
1327 Assert(fcache->rettype == VOIDOID);
1328 fcinfo->isnull = true;
1332 /* Clear the tuplestore, but keep it for next time */
1333 tuplestore_clear(fcache->tstore);
1336 /* Pop snapshot if we have pushed one */
1337 if (pushed_snapshot)
1338 PopActiveSnapshot();
1341 * If we've gone through every command in the function, we are done. Reset
1342 * the execution states to start over again on next call.
1346 foreach(eslc, fcache->func_state)
1348 es = (execution_state *) lfirst(eslc);
1351 es->status = F_EXEC_START;
1357 error_context_stack = sqlerrcontext.previous;
1359 MemoryContextSwitchTo(oldcontext);
1366 * error context callback to let us supply a call-stack traceback
1369 sql_exec_error_callback(void *arg)
1371 FmgrInfo *flinfo = (FmgrInfo *) arg;
1372 SQLFunctionCachePtr fcache = (SQLFunctionCachePtr) flinfo->fn_extra;
1373 int syntaxerrposition;
1376 * We can do nothing useful if init_sql_fcache() didn't get as far as
1377 * saving the function name
1379 if (fcache == NULL || fcache->fname == NULL)
1383 * If there is a syntax error position, convert to internal syntax error
1385 syntaxerrposition = geterrposition();
1386 if (syntaxerrposition > 0 && fcache->src != NULL)
1389 internalerrposition(syntaxerrposition);
1390 internalerrquery(fcache->src);
1394 * Try to determine where in the function we failed. If there is a query
1395 * with non-null QueryDesc, finger it. (We check this rather than looking
1396 * for F_EXEC_RUN state, so that errors during ExecutorStart or
1397 * ExecutorEnd are blamed on the appropriate query; see postquel_start and
1400 if (fcache->func_state)
1402 execution_state *es;
1408 foreach(lc, fcache->func_state)
1410 es = (execution_state *) lfirst(lc);
1415 errcontext("SQL function \"%s\" statement %d",
1416 fcache->fname, query_num);
1428 * couldn't identify a running query; might be function entry,
1429 * function exit, or between queries.
1431 errcontext("SQL function \"%s\"", fcache->fname);
1437 * Assume we failed during init_sql_fcache(). (It's possible that the
1438 * function actually has an empty body, but in that case we may as
1439 * well report all errors as being "during startup".)
1441 errcontext("SQL function \"%s\" during startup", fcache->fname);
1447 * callback function in case a function-returning-set needs to be shut down
1448 * before it has been run to completion
1451 ShutdownSQLFunction(Datum arg)
1453 SQLFunctionCachePtr fcache = (SQLFunctionCachePtr) DatumGetPointer(arg);
1454 execution_state *es;
1457 foreach(lc, fcache->func_state)
1459 es = (execution_state *) lfirst(lc);
1462 /* Shut down anything still running */
1463 if (es->status == F_EXEC_RUN)
1465 /* Re-establish active snapshot for any called functions */
1466 if (!fcache->readonly_func)
1467 PushActiveSnapshot(es->qd->snapshot);
1471 if (!fcache->readonly_func)
1472 PopActiveSnapshot();
1475 /* Reset states to START in case we're called again */
1476 es->status = F_EXEC_START;
1481 /* Release tuplestore if we have one */
1483 tuplestore_end(fcache->tstore);
1484 fcache->tstore = NULL;
1486 /* execUtils will deregister the callback... */
1487 fcache->shutdown_reg = false;
1492 * check_sql_fn_retval() -- check return value of a list of sql parse trees.
1494 * The return value of a sql function is the value returned by the last
1495 * canSetTag query in the function. We do some ad-hoc type checking here
1496 * to be sure that the user is returning the type he claims. There are
1497 * also a couple of strange-looking features to assist callers in dealing
1498 * with allowed special cases, such as binary-compatible result types.
1500 * For a polymorphic function the passed rettype must be the actual resolved
1501 * output type of the function; we should never see a polymorphic pseudotype
1502 * such as ANYELEMENT as rettype. (This means we can't check the type during
1503 * function definition of a polymorphic function.)
1505 * This function returns true if the sql function returns the entire tuple
1506 * result of its final statement, or false if it returns just the first column
1507 * result of that statement. It throws an error if the final statement doesn't
1508 * return the right type at all.
1510 * Note that because we allow "SELECT rowtype_expression", the result can be
1511 * false even when the declared function return type is a rowtype.
1513 * If modifyTargetList isn't NULL, the function will modify the final
1514 * statement's targetlist in two cases:
1515 * (1) if the tlist returns values that are binary-coercible to the expected
1516 * type rather than being exactly the expected type. RelabelType nodes will
1517 * be inserted to make the result types match exactly.
1518 * (2) if there are dropped columns in the declared result rowtype. NULL
1519 * output columns will be inserted in the tlist to match them.
1520 * (Obviously the caller must pass a parsetree that is okay to modify when
1521 * using this flag.) Note that this flag does not affect whether the tlist is
1522 * considered to be a legal match to the result type, only how we react to
1523 * allowed not-exact-match cases. *modifyTargetList will be set true iff
1524 * we had to make any "dangerous" changes that could modify the semantics of
1525 * the statement. If it is set true, the caller should not use the modified
1526 * statement, but for simplicity we apply the changes anyway.
1528 * If junkFilter isn't NULL, then *junkFilter is set to a JunkFilter defined
1529 * to convert the function's tuple result to the correct output tuple type.
1530 * Exception: if the function is defined to return VOID then *junkFilter is
1534 check_sql_fn_retval(Oid func_id, Oid rettype, List *queryTreeList,
1535 bool *modifyTargetList,
1536 JunkFilter **junkFilter)
1546 AssertArg(!IsPolymorphicType(rettype));
1548 if (modifyTargetList)
1549 *modifyTargetList = false; /* initialize for no change */
1551 *junkFilter = NULL; /* initialize in case of VOID result */
1554 * Find the last canSetTag query in the list. This isn't necessarily the
1555 * last parsetree, because rule rewriting can insert queries after what
1559 foreach(lc, queryTreeList)
1561 Query *q = (Query *) lfirst(lc);
1568 * If it's a plain SELECT, it returns whatever the targetlist says.
1569 * Otherwise, if it's INSERT/UPDATE/DELETE with RETURNING, it returns
1570 * that. Otherwise, the function return type must be VOID.
1572 * Note: eventually replace this test with QueryReturnsTuples? We'd need
1573 * a more general method of determining the output type, though. Also, it
1574 * seems too dangerous to consider FETCH or EXECUTE as returning a
1575 * determinable rowtype, since they depend on relatively short-lived
1579 parse->commandType == CMD_SELECT &&
1580 parse->utilityStmt == NULL)
1582 tlist_ptr = &parse->targetList;
1583 tlist = parse->targetList;
1586 (parse->commandType == CMD_INSERT ||
1587 parse->commandType == CMD_UPDATE ||
1588 parse->commandType == CMD_DELETE) &&
1589 parse->returningList)
1591 tlist_ptr = &parse->returningList;
1592 tlist = parse->returningList;
1596 /* Empty function body, or last statement is a utility command */
1597 if (rettype != VOIDOID)
1599 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1600 errmsg("return type mismatch in function declared to return %s",
1601 format_type_be(rettype)),
1602 errdetail("Function's final statement must be SELECT or INSERT/UPDATE/DELETE RETURNING.")));
1607 * OK, check that the targetlist returns something matching the declared
1608 * type. (We used to insist that the declared type not be VOID in this
1609 * case, but that makes it hard to write a void function that exits after
1610 * calling another void function. Instead, we insist that the tlist
1611 * return void ... so void is treated as if it were a scalar type below.)
1615 * Count the non-junk entries in the result targetlist.
1617 tlistlen = ExecCleanTargetListLength(tlist);
1619 fn_typtype = get_typtype(rettype);
1621 if (fn_typtype == TYPTYPE_BASE ||
1622 fn_typtype == TYPTYPE_DOMAIN ||
1623 fn_typtype == TYPTYPE_ENUM ||
1624 fn_typtype == TYPTYPE_RANGE ||
1628 * For scalar-type returns, the target list must have exactly one
1629 * non-junk entry, and its type must agree with what the user
1630 * declared; except we allow binary-compatible types too.
1636 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1637 errmsg("return type mismatch in function declared to return %s",
1638 format_type_be(rettype)),
1639 errdetail("Final statement must return exactly one column.")));
1641 /* We assume here that non-junk TLEs must come first in tlists */
1642 tle = (TargetEntry *) linitial(tlist);
1643 Assert(!tle->resjunk);
1645 restype = exprType((Node *) tle->expr);
1646 if (!IsBinaryCoercible(restype, rettype))
1648 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1649 errmsg("return type mismatch in function declared to return %s",
1650 format_type_be(rettype)),
1651 errdetail("Actual return type is %s.",
1652 format_type_be(restype))));
1653 if (modifyTargetList && restype != rettype)
1655 tle->expr = (Expr *) makeRelabelType(tle->expr,
1658 get_typcollation(rettype),
1659 COERCE_IMPLICIT_CAST);
1660 /* Relabel is dangerous if TLE is a sort/group or setop column */
1661 if (tle->ressortgroupref != 0 || parse->setOperations)
1662 *modifyTargetList = true;
1665 /* Set up junk filter if needed */
1667 *junkFilter = ExecInitJunkFilter(tlist, false, NULL);
1669 else if (fn_typtype == TYPTYPE_COMPOSITE || rettype == RECORDOID)
1671 /* Returns a rowtype */
1673 int tupnatts; /* physical number of columns in tuple */
1674 int tuplogcols; /* # of nondeleted columns in tuple */
1675 int colindex; /* physical column index */
1676 List *newtlist; /* new non-junk tlist entries */
1677 List *junkattrs; /* new junk tlist entries */
1680 * If the target list is of length 1, and the type of the varnode in
1681 * the target list matches the declared return type, this is okay.
1682 * This can happen, for example, where the body of the function is
1683 * 'SELECT func2()', where func2 has the same composite return type as
1684 * the function that's calling it.
1686 * XXX Note that if rettype is RECORD, the IsBinaryCoercible check
1687 * will succeed for any composite restype. For the moment we rely on
1688 * runtime type checking to catch any discrepancy, but it'd be nice to
1689 * do better at parse time.
1693 TargetEntry *tle = (TargetEntry *) linitial(tlist);
1695 Assert(!tle->resjunk);
1696 restype = exprType((Node *) tle->expr);
1697 if (IsBinaryCoercible(restype, rettype))
1699 if (modifyTargetList && restype != rettype)
1701 tle->expr = (Expr *) makeRelabelType(tle->expr,
1704 get_typcollation(rettype),
1705 COERCE_IMPLICIT_CAST);
1706 /* Relabel is dangerous if sort/group or setop column */
1707 if (tle->ressortgroupref != 0 || parse->setOperations)
1708 *modifyTargetList = true;
1710 /* Set up junk filter if needed */
1712 *junkFilter = ExecInitJunkFilter(tlist, false, NULL);
1713 return false; /* NOT returning whole tuple */
1717 /* Is the rowtype fixed, or determined only at runtime? */
1718 if (get_func_result_type(func_id, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1721 * Assume we are returning the whole tuple. Crosschecking against
1722 * what the caller expects will happen at runtime.
1725 *junkFilter = ExecInitJunkFilter(tlist, false, NULL);
1731 * Verify that the targetlist matches the return tuple type. We scan
1732 * the non-deleted attributes to ensure that they match the datatypes
1733 * of the non-resjunk columns. For deleted attributes, insert NULL
1734 * result columns if the caller asked for that.
1736 tupnatts = tupdesc->natts;
1737 tuplogcols = 0; /* we'll count nondeleted cols as we go */
1739 newtlist = NIL; /* these are only used if modifyTargetList */
1744 TargetEntry *tle = (TargetEntry *) lfirst(lc);
1745 Form_pg_attribute attr;
1751 if (modifyTargetList)
1752 junkattrs = lappend(junkattrs, tle);
1759 if (colindex > tupnatts)
1761 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1762 errmsg("return type mismatch in function declared to return %s",
1763 format_type_be(rettype)),
1764 errdetail("Final statement returns too many columns.")));
1765 attr = tupdesc->attrs[colindex - 1];
1766 if (attr->attisdropped && modifyTargetList)
1770 /* The type of the null we insert isn't important */
1771 null_expr = (Expr *) makeConst(INT4OID,
1778 newtlist = lappend(newtlist,
1779 makeTargetEntry(null_expr,
1783 /* NULL insertion is dangerous in a setop */
1784 if (parse->setOperations)
1785 *modifyTargetList = true;
1787 } while (attr->attisdropped);
1790 tletype = exprType((Node *) tle->expr);
1791 atttype = attr->atttypid;
1792 if (!IsBinaryCoercible(tletype, atttype))
1794 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1795 errmsg("return type mismatch in function declared to return %s",
1796 format_type_be(rettype)),
1797 errdetail("Final statement returns %s instead of %s at column %d.",
1798 format_type_be(tletype),
1799 format_type_be(atttype),
1801 if (modifyTargetList)
1803 if (tletype != atttype)
1805 tle->expr = (Expr *) makeRelabelType(tle->expr,
1808 get_typcollation(atttype),
1809 COERCE_IMPLICIT_CAST);
1810 /* Relabel is dangerous if sort/group or setop column */
1811 if (tle->ressortgroupref != 0 || parse->setOperations)
1812 *modifyTargetList = true;
1814 tle->resno = colindex;
1815 newtlist = lappend(newtlist, tle);
1819 /* remaining columns in tupdesc had better all be dropped */
1820 for (colindex++; colindex <= tupnatts; colindex++)
1822 if (!tupdesc->attrs[colindex - 1]->attisdropped)
1824 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1825 errmsg("return type mismatch in function declared to return %s",
1826 format_type_be(rettype)),
1827 errdetail("Final statement returns too few columns.")));
1828 if (modifyTargetList)
1832 /* The type of the null we insert isn't important */
1833 null_expr = (Expr *) makeConst(INT4OID,
1840 newtlist = lappend(newtlist,
1841 makeTargetEntry(null_expr,
1845 /* NULL insertion is dangerous in a setop */
1846 if (parse->setOperations)
1847 *modifyTargetList = true;
1851 if (modifyTargetList)
1853 /* ensure resjunk columns are numbered correctly */
1854 foreach(lc, junkattrs)
1856 TargetEntry *tle = (TargetEntry *) lfirst(lc);
1858 tle->resno = colindex++;
1860 /* replace the tlist with the modified one */
1861 *tlist_ptr = list_concat(newtlist, junkattrs);
1864 /* Set up junk filter if needed */
1866 *junkFilter = ExecInitJunkFilterConversion(tlist,
1867 CreateTupleDescCopy(tupdesc),
1870 /* Report that we are returning entire tuple result */
1875 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1876 errmsg("return type %s is not supported for SQL functions",
1877 format_type_be(rettype))));
1884 * CreateSQLFunctionDestReceiver -- create a suitable DestReceiver object
1887 CreateSQLFunctionDestReceiver(void)
1889 DR_sqlfunction *self = (DR_sqlfunction *) palloc0(sizeof(DR_sqlfunction));
1891 self->pub.receiveSlot = sqlfunction_receive;
1892 self->pub.rStartup = sqlfunction_startup;
1893 self->pub.rShutdown = sqlfunction_shutdown;
1894 self->pub.rDestroy = sqlfunction_destroy;
1895 self->pub.mydest = DestSQLFunction;
1897 /* private fields will be set by postquel_start */
1899 return (DestReceiver *) self;
1903 * sqlfunction_startup --- executor startup
1906 sqlfunction_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
1912 * sqlfunction_receive --- receive one tuple
1915 sqlfunction_receive(TupleTableSlot *slot, DestReceiver *self)
1917 DR_sqlfunction *myState = (DR_sqlfunction *) self;
1919 /* Filter tuple as needed */
1920 slot = ExecFilterJunk(myState->filter, slot);
1922 /* Store the filtered tuple into the tuplestore */
1923 tuplestore_puttupleslot(myState->tstore, slot);
1929 * sqlfunction_shutdown --- executor end
1932 sqlfunction_shutdown(DestReceiver *self)
1938 * sqlfunction_destroy --- release DestReceiver object
1941 sqlfunction_destroy(DestReceiver *self)