11
* $PostgreSQL: pgsql/src/backend/executor/functions.c,v 1.133 2009/03/27 18:30:21 tgl Exp $
11
* $PostgreSQL: pgsql/src/backend/executor/functions.c,v 1.135 2009/06/11 17:25:38 tgl Exp $
13
13
*-------------------------------------------------------------------------
102
102
/* non-export function prototypes */
103
103
static execution_state *init_execution_state(List *queryTree_list,
104
SQLFunctionCachePtr fcache,
104
SQLFunctionCachePtr fcache,
106
106
static void init_sql_fcache(FmgrInfo *finfo, bool lazyEvalOK);
107
107
static void postquel_start(execution_state *es, SQLFunctionCachePtr fcache);
108
108
static bool postquel_getnext(execution_state *es, SQLFunctionCachePtr fcache);
169
169
newes->next = NULL;
170
170
newes->status = F_EXEC_START;
171
newes->setsResult = false; /* might change below */
172
newes->lazyEval = false; /* might change below */
171
newes->setsResult = false; /* might change below */
172
newes->lazyEval = false; /* might change below */
173
173
newes->stmt = stmt;
174
174
newes->qd = NULL;
183
* Mark the last canSetTag query as delivering the function result;
184
* then, if it is a plain SELECT, mark it for lazy evaluation.
185
* If it's not a SELECT we must always run it to completion.
183
* Mark the last canSetTag query as delivering the function result; then,
184
* if it is a plain SELECT, mark it for lazy evaluation. If it's not a
185
* SELECT we must always run it to completion.
187
187
* Note: at some point we might add additional criteria for whether to use
188
188
* lazy eval. However, we should prefer to use it whenever the function
192
192
* Note: don't set setsResult if the function returns VOID, as evidenced
193
193
* by not having made a junkfilter. This ensures we'll throw away any
194
* output from a utility statement that check_sql_fn_retval deemed to
194
* output from a utility statement that check_sql_fn_retval deemed to not
197
197
if (lasttages && fcache->junkFilter)
326
326
* Note: we set fcache->returnsTuple according to whether we are returning
327
327
* the whole tuple result or just a single column. In the latter case we
328
328
* clear returnsTuple because we need not act different from the scalar
329
* result case, even if it's a rowtype column. (However, we have to
330
* force lazy eval mode in that case; otherwise we'd need extra code to
331
* expand the rowtype column into multiple columns, since we have no
332
* way to notify the caller that it should do that.)
329
* result case, even if it's a rowtype column. (However, we have to force
330
* lazy eval mode in that case; otherwise we'd need extra code to expand
331
* the rowtype column into multiple columns, since we have no way to
332
* notify the caller that it should do that.)
334
334
* check_sql_fn_retval will also construct a JunkFilter we can use to
335
335
* coerce the returned rowtype to the desired form (unless the result type
567
567
* Set up to return the function value. For pass-by-reference datatypes,
568
568
* be sure to allocate the result in resultcontext, not the current memory
569
* context (which has query lifespan). We can't leave the data in the
569
* context (which has query lifespan). We can't leave the data in the
570
570
* TupleTableSlot because we intend to clear the slot before returning.
572
572
oldcontext = MemoryContextSwitchTo(resultcontext);
634
634
* For simplicity, we require callers to support both set eval modes.
635
635
* There are cases where we must use one or must use the other, and
636
636
* it's not really worthwhile to postpone the check till we know.
637
* But note we do not require caller to provide an expectedDesc.
638
639
if (!rsi || !IsA(rsi, ReturnSetInfo) ||
639
640
(rsi->allowedModes & SFRM_ValuePerCall) == 0 ||
640
(rsi->allowedModes & SFRM_Materialize) == 0 ||
641
rsi->expectedDesc == NULL)
641
(rsi->allowedModes & SFRM_Materialize) == 0)
643
643
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
644
644
errmsg("set-valued function called in context that cannot accept a set")));
670
670
postquel_sub_params(fcache, fcinfo);
673
* Build tuplestore to hold results, if we don't have one already.
674
* Note it's in the query-lifespan context.
673
* Build tuplestore to hold results, if we don't have one already. Note
674
* it's in the query-lifespan context.
676
676
if (!fcache->tstore)
677
677
fcache->tstore = tuplestore_begin_heap(randomAccess, false, work_mem);
696
696
completed = postquel_getnext(es, fcache);
699
* If we ran the command to completion, we can shut it down now.
700
* Any row(s) we need to return are safely stashed in the tuplestore,
701
* and we want to be sure that, for example, AFTER triggers get fired
699
* If we ran the command to completion, we can shut it down now. Any
700
* row(s) we need to return are safely stashed in the tuplestore, and
701
* we want to be sure that, for example, AFTER triggers get fired
702
702
* before we return anything. Also, if the function doesn't return
703
* set, we can shut it down anyway because it must be a SELECT and
704
* we don't care about fetching any more result rows.
703
* set, we can shut it down anyway because it must be a SELECT and we
704
* don't care about fetching any more result rows.
706
706
if (completed || !fcache->returnsSet)
707
707
postquel_end(es);
710
710
* Break from loop if we didn't shut down (implying we got a
711
* lazily-evaluated row). Otherwise we'll press on till the
712
* whole function is done, relying on the tuplestore to keep hold
713
* of the data to eventually be returned. This is necessary since
714
* an INSERT/UPDATE/DELETE RETURNING that sets the result might be
711
* lazily-evaluated row). Otherwise we'll press on till the whole
712
* function is done, relying on the tuplestore to keep hold of the
713
* data to eventually be returned. This is necessary since an
714
* INSERT/UPDATE/DELETE RETURNING that sets the result might be
715
715
* followed by additional rule-inserted commands, and we want to
716
716
* finish doing all those commands before we return anything.
792
* We are done with a non-lazy evaluation. Return whatever is
793
* in the tuplestore. (It is now caller's responsibility to
794
* free the tuplestore when done.)
793
* We are done with a non-lazy evaluation. Return whatever is in
794
* the tuplestore. (It is now caller's responsibility to free the
795
* tuplestore when done.)
796
797
rsi->returnMode = SFRM_Materialize;
797
798
rsi->setResult = fcache->tstore;
847
* If we've gone through every command in the function, we are done.
848
* Reset the execution states to start over again on next call.
848
* If we've gone through every command in the function, we are done. Reset
849
* the execution states to start over again on next call.
997
998
* function definition of a polymorphic function.)
999
1000
* This function returns true if the sql function returns the entire tuple
1000
* result of its final statement, and false otherwise. Note that because we
1001
* result of its final statement, and false otherwise. Note that because we
1001
1002
* allow "SELECT rowtype_expression", this may be false even when the declared
1002
1003
* function return type is a rowtype.
1029
1030
*junkFilter = NULL; /* initialize in case of VOID result */
1032
* Find the last canSetTag query in the list. This isn't necessarily
1033
* the last parsetree, because rule rewriting can insert queries after
1034
* what the user wrote.
1033
* Find the last canSetTag query in the list. This isn't necessarily the
1034
* last parsetree, because rule rewriting can insert queries after what
1037
1038
foreach(lc, queryTreeList)
1039
Query *q = (Query *) lfirst(lc);
1040
Query *q = (Query *) lfirst(lc);
1041
1042
if (q->canSetTag)
1046
1047
* If it's a plain SELECT, it returns whatever the targetlist says.
1047
* Otherwise, if it's INSERT/UPDATE/DELETE with RETURNING, it returns that.
1048
* Otherwise, the function return type must be VOID.
1048
* Otherwise, if it's INSERT/UPDATE/DELETE with RETURNING, it returns
1049
* that. Otherwise, the function return type must be VOID.
1050
1051
* Note: eventually replace this test with QueryReturnsTuples? We'd need
1051
* a more general method of determining the output type, though. Also,
1052
* it seems too dangerous to consider FETCH or EXECUTE as returning a
1052
* a more general method of determining the output type, though. Also, it
1053
* seems too dangerous to consider FETCH or EXECUTE as returning a
1053
1054
* determinable rowtype, since they depend on relatively short-lived
1076
1077
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1077
1078
errmsg("return type mismatch in function declared to return %s",
1078
1079
format_type_be(rettype)),
1079
errdetail("Function's final statement must be SELECT or INSERT/UPDATE/DELETE RETURNING.")));
1080
errdetail("Function's final statement must be SELECT or INSERT/UPDATE/DELETE RETURNING.")));
1112
1113
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1113
1114
errmsg("return type mismatch in function declared to return %s",
1114
1115
format_type_be(rettype)),
1115
errdetail("Final statement must return exactly one column.")));
1116
errdetail("Final statement must return exactly one column.")));
1117
1118
/* We assume here that non-junk TLEs must come first in tlists */
1118
1119
tle = (TargetEntry *) linitial(tlist);
1148
1149
* If the target list is of length 1, and the type of the varnode in
1149
1150
* the target list matches the declared return type, this is okay.
1150
1151
* This can happen, for example, where the body of the function is
1151
* 'SELECT func2()', where func2 has the same composite return type
1152
* as the function that's calling it.
1152
* 'SELECT func2()', where func2 has the same composite return type as
1153
* the function that's calling it.
1154
1155
if (tlistlen == 1)
1211
1212
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1212
1213
errmsg("return type mismatch in function declared to return %s",
1213
1214
format_type_be(rettype)),
1214
errdetail("Final statement returns too many columns.")));
1215
errdetail("Final statement returns too many columns.")));
1215
1216
attr = tupdesc->attrs[colindex - 1];
1216
1217
} while (attr->attisdropped);