~ubuntu-branches/ubuntu/precise/postgresql-9.1/precise-security

« back to all changes in this revision

Viewing changes to src/backend/executor/functions.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * functions.c
 
4
 *        Execution of SQL-language functions
 
5
 *
 
6
 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
 
7
 * Portions Copyright (c) 1994, Regents of the University of California
 
8
 *
 
9
 *
 
10
 * IDENTIFICATION
 
11
 *        src/backend/executor/functions.c
 
12
 *
 
13
 *-------------------------------------------------------------------------
 
14
 */
 
15
#include "postgres.h"
 
16
 
 
17
#include "access/xact.h"
 
18
#include "catalog/pg_proc.h"
 
19
#include "catalog/pg_type.h"
 
20
#include "commands/trigger.h"
 
21
#include "executor/functions.h"
 
22
#include "funcapi.h"
 
23
#include "miscadmin.h"
 
24
#include "nodes/makefuncs.h"
 
25
#include "nodes/nodeFuncs.h"
 
26
#include "parser/parse_coerce.h"
 
27
#include "tcop/utility.h"
 
28
#include "utils/builtins.h"
 
29
#include "utils/datum.h"
 
30
#include "utils/lsyscache.h"
 
31
#include "utils/snapmgr.h"
 
32
#include "utils/syscache.h"
 
33
 
 
34
 
 
35
/*
 
36
 * Specialized DestReceiver for collecting query output in a SQL function
 
37
 */
 
38
typedef struct
 
39
{
 
40
        DestReceiver pub;                       /* publicly-known function pointers */
 
41
        Tuplestorestate *tstore;        /* where to put result tuples */
 
42
        MemoryContext cxt;                      /* context containing tstore */
 
43
        JunkFilter *filter;                     /* filter to convert tuple type */
 
44
} DR_sqlfunction;
 
45
 
 
46
/*
 
47
 * We have an execution_state record for each query in a function.      Each
 
48
 * record contains a plantree for its query.  If the query is currently in
 
49
 * F_EXEC_RUN state then there's a QueryDesc too.
 
50
 *
 
51
 * The "next" fields chain together all the execution_state records generated
 
52
 * from a single original parsetree.  (There will only be more than one in
 
53
 * case of rule expansion of the original parsetree.)
 
54
 */
 
55
typedef enum
 
56
{
 
57
        F_EXEC_START, F_EXEC_RUN, F_EXEC_DONE
 
58
} ExecStatus;
 
59
 
 
60
typedef struct execution_state
 
61
{
 
62
        struct execution_state *next;
 
63
        ExecStatus      status;
 
64
        bool            setsResult;             /* true if this query produces func's result */
 
65
        bool            lazyEval;               /* true if should fetch one row at a time */
 
66
        Node       *stmt;                       /* PlannedStmt or utility statement */
 
67
        QueryDesc  *qd;                         /* null unless status == RUN */
 
68
} execution_state;
 
69
 
 
70
 
 
71
/*
 
72
 * An SQLFunctionCache record is built during the first call,
 
73
 * and linked to from the fn_extra field of the FmgrInfo struct.
 
74
 *
 
75
 * Note that currently this has only the lifespan of the calling query.
 
76
 * Someday we might want to consider caching the parse/plan results longer
 
77
 * than that.
 
78
 */
 
79
typedef struct
 
80
{
 
81
        char       *fname;                      /* function name (for error msgs) */
 
82
        char       *src;                        /* function body text (for error msgs) */
 
83
 
 
84
        SQLFunctionParseInfoPtr pinfo;          /* data for parser callback hooks */
 
85
 
 
86
        Oid                     rettype;                /* actual return type */
 
87
        int16           typlen;                 /* length of the return type */
 
88
        bool            typbyval;               /* true if return type is pass by value */
 
89
        bool            returnsSet;             /* true if returning multiple rows */
 
90
        bool            returnsTuple;   /* true if returning whole tuple result */
 
91
        bool            shutdown_reg;   /* true if registered shutdown callback */
 
92
        bool            readonly_func;  /* true to run in "read only" mode */
 
93
        bool            lazyEval;               /* true if using lazyEval for result query */
 
94
 
 
95
        ParamListInfo paramLI;          /* Param list representing current args */
 
96
 
 
97
        Tuplestorestate *tstore;        /* where we accumulate result tuples */
 
98
 
 
99
        JunkFilter *junkFilter;         /* will be NULL if function returns VOID */
 
100
 
 
101
        /*
 
102
         * func_state is a List of execution_state records, each of which is the
 
103
         * first for its original parsetree, with any additional records chained
 
104
         * to it via the "next" fields.  This sublist structure is needed to keep
 
105
         * track of where the original query boundaries are.
 
106
         */
 
107
        List       *func_state;
 
108
} SQLFunctionCache;
 
109
 
 
110
typedef SQLFunctionCache *SQLFunctionCachePtr;
 
111
 
 
112
/*
 
113
 * Data structure needed by the parser callback hooks to resolve parameter
 
114
 * references during parsing of a SQL function's body.  This is separate from
 
115
 * SQLFunctionCache since we sometimes do parsing separately from execution.
 
116
 */
 
117
typedef struct SQLFunctionParseInfo
 
118
{
 
119
        Oid                *argtypes;           /* resolved types of input arguments */
 
120
        int                     nargs;                  /* number of input arguments */
 
121
        Oid                     collation;              /* function's input collation, if known */
 
122
}       SQLFunctionParseInfo;
 
123
 
 
124
 
 
125
/* non-export function prototypes */
 
126
static Node *sql_fn_param_ref(ParseState *pstate, ParamRef *pref);
 
127
static List *init_execution_state(List *queryTree_list,
 
128
                                         SQLFunctionCachePtr fcache,
 
129
                                         bool lazyEvalOK);
 
130
static void init_sql_fcache(FmgrInfo *finfo, Oid collation, bool lazyEvalOK);
 
131
static void postquel_start(execution_state *es, SQLFunctionCachePtr fcache);
 
132
static bool postquel_getnext(execution_state *es, SQLFunctionCachePtr fcache);
 
133
static void postquel_end(execution_state *es);
 
134
static void postquel_sub_params(SQLFunctionCachePtr fcache,
 
135
                                        FunctionCallInfo fcinfo);
 
136
static Datum postquel_get_single_result(TupleTableSlot *slot,
 
137
                                                   FunctionCallInfo fcinfo,
 
138
                                                   SQLFunctionCachePtr fcache,
 
139
                                                   MemoryContext resultcontext);
 
140
static void sql_exec_error_callback(void *arg);
 
141
static void ShutdownSQLFunction(Datum arg);
 
142
static void sqlfunction_startup(DestReceiver *self, int operation, TupleDesc typeinfo);
 
143
static void sqlfunction_receive(TupleTableSlot *slot, DestReceiver *self);
 
144
static void sqlfunction_shutdown(DestReceiver *self);
 
145
static void sqlfunction_destroy(DestReceiver *self);
 
146
 
 
147
 
 
148
/*
 
149
 * Prepare the SQLFunctionParseInfo struct for parsing a SQL function body
 
150
 *
 
151
 * This includes resolving actual types of polymorphic arguments.
 
152
 *
 
153
 * call_expr can be passed as NULL, but then we will fail if there are any
 
154
 * polymorphic arguments.
 
155
 */
 
156
SQLFunctionParseInfoPtr
 
157
prepare_sql_fn_parse_info(HeapTuple procedureTuple,
 
158
                                                  Node *call_expr,
 
159
                                                  Oid inputCollation)
 
160
{
 
161
        SQLFunctionParseInfoPtr pinfo;
 
162
        Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
 
163
        int                     nargs;
 
164
 
 
165
        pinfo = (SQLFunctionParseInfoPtr) palloc0(sizeof(SQLFunctionParseInfo));
 
166
 
 
167
        /* Save the function's input collation */
 
168
        pinfo->collation = inputCollation;
 
169
 
 
170
        /*
 
171
         * Copy input argument types from the pg_proc entry, then resolve any
 
172
         * polymorphic types.
 
173
         */
 
174
        pinfo->nargs = nargs = procedureStruct->pronargs;
 
175
        if (nargs > 0)
 
176
        {
 
177
                Oid                *argOidVect;
 
178
                int                     argnum;
 
179
 
 
180
                argOidVect = (Oid *) palloc(nargs * sizeof(Oid));
 
181
                memcpy(argOidVect,
 
182
                           procedureStruct->proargtypes.values,
 
183
                           nargs * sizeof(Oid));
 
184
 
 
185
                for (argnum = 0; argnum < nargs; argnum++)
 
186
                {
 
187
                        Oid                     argtype = argOidVect[argnum];
 
188
 
 
189
                        if (IsPolymorphicType(argtype))
 
190
                        {
 
191
                                argtype = get_call_expr_argtype(call_expr, argnum);
 
192
                                if (argtype == InvalidOid)
 
193
                                        ereport(ERROR,
 
194
                                                        (errcode(ERRCODE_DATATYPE_MISMATCH),
 
195
                                                         errmsg("could not determine actual type of argument declared %s",
 
196
                                                                        format_type_be(argOidVect[argnum]))));
 
197
                                argOidVect[argnum] = argtype;
 
198
                        }
 
199
                }
 
200
 
 
201
                pinfo->argtypes = argOidVect;
 
202
        }
 
203
 
 
204
        return pinfo;
 
205
}
 
206
 
 
207
/*
 
208
 * Parser setup hook for parsing a SQL function body.
 
209
 */
 
210
void
 
211
sql_fn_parser_setup(struct ParseState *pstate, SQLFunctionParseInfoPtr pinfo)
 
212
{
 
213
        /* Later we might use these hooks to support parameter names */
 
214
        pstate->p_pre_columnref_hook = NULL;
 
215
        pstate->p_post_columnref_hook = NULL;
 
216
        pstate->p_paramref_hook = sql_fn_param_ref;
 
217
        /* no need to use p_coerce_param_hook */
 
218
        pstate->p_ref_hook_state = (void *) pinfo;
 
219
}
 
220
 
 
221
/*
 
222
 * sql_fn_param_ref             parser callback for ParamRefs ($n symbols)
 
223
 */
 
224
static Node *
 
225
sql_fn_param_ref(ParseState *pstate, ParamRef *pref)
 
226
{
 
227
        SQLFunctionParseInfoPtr pinfo = (SQLFunctionParseInfoPtr) pstate->p_ref_hook_state;
 
228
        int                     paramno = pref->number;
 
229
        Param      *param;
 
230
 
 
231
        /* Check parameter number is valid */
 
232
        if (paramno <= 0 || paramno > pinfo->nargs)
 
233
                return NULL;                    /* unknown parameter number */
 
234
 
 
235
        param = makeNode(Param);
 
236
        param->paramkind = PARAM_EXTERN;
 
237
        param->paramid = paramno;
 
238
        param->paramtype = pinfo->argtypes[paramno - 1];
 
239
        param->paramtypmod = -1;
 
240
        param->paramcollid = get_typcollation(param->paramtype);
 
241
        param->location = pref->location;
 
242
 
 
243
        /*
 
244
         * If we have a function input collation, allow it to override the
 
245
         * type-derived collation for parameter symbols.  (XXX perhaps this should
 
246
         * not happen if the type collation is not default?)
 
247
         */
 
248
        if (OidIsValid(pinfo->collation) && OidIsValid(param->paramcollid))
 
249
                param->paramcollid = pinfo->collation;
 
250
 
 
251
        return (Node *) param;
 
252
}
 
253
 
 
254
/*
 
255
 * Set up the per-query execution_state records for a SQL function.
 
256
 *
 
257
 * The input is a List of Lists of parsed and rewritten, but not planned,
 
258
 * querytrees.  The sublist structure denotes the original query boundaries.
 
259
 */
 
260
static List *
 
261
init_execution_state(List *queryTree_list,
 
262
                                         SQLFunctionCachePtr fcache,
 
263
                                         bool lazyEvalOK)
 
264
{
 
265
        List       *eslist = NIL;
 
266
        execution_state *lasttages = NULL;
 
267
        ListCell   *lc1;
 
268
 
 
269
        foreach(lc1, queryTree_list)
 
270
        {
 
271
                List       *qtlist = (List *) lfirst(lc1);
 
272
                execution_state *firstes = NULL;
 
273
                execution_state *preves = NULL;
 
274
                ListCell   *lc2;
 
275
 
 
276
                foreach(lc2, qtlist)
 
277
                {
 
278
                        Query      *queryTree = (Query *) lfirst(lc2);
 
279
                        Node       *stmt;
 
280
                        execution_state *newes;
 
281
 
 
282
                        Assert(IsA(queryTree, Query));
 
283
 
 
284
                        /* Plan the query if needed */
 
285
                        if (queryTree->commandType == CMD_UTILITY)
 
286
                                stmt = queryTree->utilityStmt;
 
287
                        else
 
288
                                stmt = (Node *) pg_plan_query(queryTree, 0, NULL);
 
289
 
 
290
                        /* Precheck all commands for validity in a function */
 
291
                        if (IsA(stmt, TransactionStmt))
 
292
                                ereport(ERROR,
 
293
                                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 
294
                                /* translator: %s is a SQL statement name */
 
295
                                                 errmsg("%s is not allowed in a SQL function",
 
296
                                                                CreateCommandTag(stmt))));
 
297
 
 
298
                        if (fcache->readonly_func && !CommandIsReadOnly(stmt))
 
299
                                ereport(ERROR,
 
300
                                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 
301
                                /* translator: %s is a SQL statement name */
 
302
                                           errmsg("%s is not allowed in a non-volatile function",
 
303
                                                          CreateCommandTag(stmt))));
 
304
 
 
305
                        /* OK, build the execution_state for this query */
 
306
                        newes = (execution_state *) palloc(sizeof(execution_state));
 
307
                        if (preves)
 
308
                                preves->next = newes;
 
309
                        else
 
310
                                firstes = newes;
 
311
 
 
312
                        newes->next = NULL;
 
313
                        newes->status = F_EXEC_START;
 
314
                        newes->setsResult = false;      /* might change below */
 
315
                        newes->lazyEval = false;        /* might change below */
 
316
                        newes->stmt = stmt;
 
317
                        newes->qd = NULL;
 
318
 
 
319
                        if (queryTree->canSetTag)
 
320
                                lasttages = newes;
 
321
 
 
322
                        preves = newes;
 
323
                }
 
324
 
 
325
                eslist = lappend(eslist, firstes);
 
326
        }
 
327
 
 
328
        /*
 
329
         * Mark the last canSetTag query as delivering the function result; then,
 
330
         * if it is a plain SELECT, mark it for lazy evaluation. If it's not a
 
331
         * SELECT we must always run it to completion.
 
332
         *
 
333
         * Note: at some point we might add additional criteria for whether to use
 
334
         * lazy eval.  However, we should prefer to use it whenever the function
 
335
         * doesn't return set, since fetching more than one row is useless in that
 
336
         * case.
 
337
         *
 
338
         * Note: don't set setsResult if the function returns VOID, as evidenced
 
339
         * by not having made a junkfilter.  This ensures we'll throw away any
 
340
         * output from a utility statement that check_sql_fn_retval deemed to not
 
341
         * have output.
 
342
         */
 
343
        if (lasttages && fcache->junkFilter)
 
344
        {
 
345
                lasttages->setsResult = true;
 
346
                if (lazyEvalOK &&
 
347
                        IsA(lasttages->stmt, PlannedStmt))
 
348
                {
 
349
                        PlannedStmt *ps = (PlannedStmt *) lasttages->stmt;
 
350
 
 
351
                        if (ps->commandType == CMD_SELECT &&
 
352
                                ps->utilityStmt == NULL &&
 
353
                                ps->intoClause == NULL &&
 
354
                                !ps->hasModifyingCTE)
 
355
                                fcache->lazyEval = lasttages->lazyEval = true;
 
356
                }
 
357
        }
 
358
 
 
359
        return eslist;
 
360
}
 
361
 
 
362
/*
 
363
 * Initialize the SQLFunctionCache for a SQL function
 
364
 */
 
365
static void
 
366
init_sql_fcache(FmgrInfo *finfo, Oid collation, bool lazyEvalOK)
 
367
{
 
368
        Oid                     foid = finfo->fn_oid;
 
369
        Oid                     rettype;
 
370
        HeapTuple       procedureTuple;
 
371
        Form_pg_proc procedureStruct;
 
372
        SQLFunctionCachePtr fcache;
 
373
        List       *raw_parsetree_list;
 
374
        List       *queryTree_list;
 
375
        List       *flat_query_list;
 
376
        ListCell   *lc;
 
377
        Datum           tmp;
 
378
        bool            isNull;
 
379
 
 
380
        fcache = (SQLFunctionCachePtr) palloc0(sizeof(SQLFunctionCache));
 
381
        finfo->fn_extra = (void *) fcache;
 
382
 
 
383
        /*
 
384
         * get the procedure tuple corresponding to the given function Oid
 
385
         */
 
386
        procedureTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(foid));
 
387
        if (!HeapTupleIsValid(procedureTuple))
 
388
                elog(ERROR, "cache lookup failed for function %u", foid);
 
389
        procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
 
390
 
 
391
        /*
 
392
         * copy function name immediately for use by error reporting callback
 
393
         */
 
394
        fcache->fname = pstrdup(NameStr(procedureStruct->proname));
 
395
 
 
396
        /*
 
397
         * get the result type from the procedure tuple, and check for polymorphic
 
398
         * result type; if so, find out the actual result type.
 
399
         */
 
400
        rettype = procedureStruct->prorettype;
 
401
 
 
402
        if (IsPolymorphicType(rettype))
 
403
        {
 
404
                rettype = get_fn_expr_rettype(finfo);
 
405
                if (rettype == InvalidOid)              /* this probably should not happen */
 
406
                        ereport(ERROR,
 
407
                                        (errcode(ERRCODE_DATATYPE_MISMATCH),
 
408
                                         errmsg("could not determine actual result type for function declared to return type %s",
 
409
                                                        format_type_be(procedureStruct->prorettype))));
 
410
        }
 
411
 
 
412
        fcache->rettype = rettype;
 
413
 
 
414
        /* Fetch the typlen and byval info for the result type */
 
415
        get_typlenbyval(rettype, &fcache->typlen, &fcache->typbyval);
 
416
 
 
417
        /* Remember whether we're returning setof something */
 
418
        fcache->returnsSet = procedureStruct->proretset;
 
419
 
 
420
        /* Remember if function is STABLE/IMMUTABLE */
 
421
        fcache->readonly_func =
 
422
                (procedureStruct->provolatile != PROVOLATILE_VOLATILE);
 
423
 
 
424
        /*
 
425
         * We need the actual argument types to pass to the parser.  Also make
 
426
         * sure that parameter symbols are considered to have the function's
 
427
         * resolved input collation.
 
428
         */
 
429
        fcache->pinfo = prepare_sql_fn_parse_info(procedureTuple,
 
430
                                                                                          finfo->fn_expr,
 
431
                                                                                          collation);
 
432
 
 
433
        /*
 
434
         * And of course we need the function body text.
 
435
         */
 
436
        tmp = SysCacheGetAttr(PROCOID,
 
437
                                                  procedureTuple,
 
438
                                                  Anum_pg_proc_prosrc,
 
439
                                                  &isNull);
 
440
        if (isNull)
 
441
                elog(ERROR, "null prosrc for function %u", foid);
 
442
        fcache->src = TextDatumGetCString(tmp);
 
443
 
 
444
        /*
 
445
         * Parse and rewrite the queries in the function text.  Use sublists to
 
446
         * keep track of the original query boundaries.  But we also build a
 
447
         * "flat" list of the rewritten queries to pass to check_sql_fn_retval.
 
448
         * This is because the last canSetTag query determines the result type
 
449
         * independently of query boundaries --- and it might not be in the last
 
450
         * sublist, for example if the last query rewrites to DO INSTEAD NOTHING.
 
451
         * (It might not be unreasonable to throw an error in such a case, but
 
452
         * this is the historical behavior and it doesn't seem worth changing.)
 
453
         */
 
454
        raw_parsetree_list = pg_parse_query(fcache->src);
 
455
 
 
456
        queryTree_list = NIL;
 
457
        flat_query_list = NIL;
 
458
        foreach(lc, raw_parsetree_list)
 
459
        {
 
460
                Node       *parsetree = (Node *) lfirst(lc);
 
461
                List       *queryTree_sublist;
 
462
 
 
463
                queryTree_sublist = pg_analyze_and_rewrite_params(parsetree,
 
464
                                                                                                                  fcache->src,
 
465
                                                                           (ParserSetupHook) sql_fn_parser_setup,
 
466
                                                                                                                  fcache->pinfo);
 
467
                queryTree_list = lappend(queryTree_list, queryTree_sublist);
 
468
                flat_query_list = list_concat(flat_query_list,
 
469
                                                                          list_copy(queryTree_sublist));
 
470
        }
 
471
 
 
472
        /*
 
473
         * Check that the function returns the type it claims to.  Although in
 
474
         * simple cases this was already done when the function was defined, we
 
475
         * have to recheck because database objects used in the function's queries
 
476
         * might have changed type.  We'd have to do it anyway if the function had
 
477
         * any polymorphic arguments.
 
478
         *
 
479
         * Note: we set fcache->returnsTuple according to whether we are returning
 
480
         * the whole tuple result or just a single column.      In the latter case we
 
481
         * clear returnsTuple because we need not act different from the scalar
 
482
         * result case, even if it's a rowtype column.  (However, we have to force
 
483
         * lazy eval mode in that case; otherwise we'd need extra code to expand
 
484
         * the rowtype column into multiple columns, since we have no way to
 
485
         * notify the caller that it should do that.)
 
486
         *
 
487
         * check_sql_fn_retval will also construct a JunkFilter we can use to
 
488
         * coerce the returned rowtype to the desired form (unless the result type
 
489
         * is VOID, in which case there's nothing to coerce to).
 
490
         */
 
491
        fcache->returnsTuple = check_sql_fn_retval(foid,
 
492
                                                                                           rettype,
 
493
                                                                                           flat_query_list,
 
494
                                                                                           NULL,
 
495
                                                                                           &fcache->junkFilter);
 
496
 
 
497
        if (fcache->returnsTuple)
 
498
        {
 
499
                /* Make sure output rowtype is properly blessed */
 
500
                BlessTupleDesc(fcache->junkFilter->jf_resultSlot->tts_tupleDescriptor);
 
501
        }
 
502
        else if (fcache->returnsSet && type_is_rowtype(fcache->rettype))
 
503
        {
 
504
                /*
 
505
                 * Returning rowtype as if it were scalar --- materialize won't work.
 
506
                 * Right now it's sufficient to override any caller preference for
 
507
                 * materialize mode, but to add more smarts in init_execution_state
 
508
                 * about this, we'd probably need a three-way flag instead of bool.
 
509
                 */
 
510
                lazyEvalOK = true;
 
511
        }
 
512
 
 
513
        /* Finally, plan the queries */
 
514
        fcache->func_state = init_execution_state(queryTree_list,
 
515
                                                                                          fcache,
 
516
                                                                                          lazyEvalOK);
 
517
 
 
518
        ReleaseSysCache(procedureTuple);
 
519
}
 
520
 
 
521
/* Start up execution of one execution_state node */
 
522
static void
 
523
postquel_start(execution_state *es, SQLFunctionCachePtr fcache)
 
524
{
 
525
        DestReceiver *dest;
 
526
 
 
527
        Assert(es->qd == NULL);
 
528
 
 
529
        /* Caller should have ensured a suitable snapshot is active */
 
530
        Assert(ActiveSnapshotSet());
 
531
 
 
532
        /*
 
533
         * If this query produces the function result, send its output to the
 
534
         * tuplestore; else discard any output.
 
535
         */
 
536
        if (es->setsResult)
 
537
        {
 
538
                DR_sqlfunction *myState;
 
539
 
 
540
                dest = CreateDestReceiver(DestSQLFunction);
 
541
                /* pass down the needed info to the dest receiver routines */
 
542
                myState = (DR_sqlfunction *) dest;
 
543
                Assert(myState->pub.mydest == DestSQLFunction);
 
544
                myState->tstore = fcache->tstore;
 
545
                myState->cxt = CurrentMemoryContext;
 
546
                myState->filter = fcache->junkFilter;
 
547
        }
 
548
        else
 
549
                dest = None_Receiver;
 
550
 
 
551
        if (IsA(es->stmt, PlannedStmt))
 
552
                es->qd = CreateQueryDesc((PlannedStmt *) es->stmt,
 
553
                                                                 fcache->src,
 
554
                                                                 GetActiveSnapshot(),
 
555
                                                                 InvalidSnapshot,
 
556
                                                                 dest,
 
557
                                                                 fcache->paramLI, 0);
 
558
        else
 
559
                es->qd = CreateUtilityQueryDesc(es->stmt,
 
560
                                                                                fcache->src,
 
561
                                                                                GetActiveSnapshot(),
 
562
                                                                                dest,
 
563
                                                                                fcache->paramLI);
 
564
 
 
565
        /* Utility commands don't need Executor. */
 
566
        if (es->qd->utilitystmt == NULL)
 
567
        {
 
568
                /*
 
569
                 * In lazyEval mode, do not let the executor set up an AfterTrigger
 
570
                 * context.  This is necessary not just an optimization, because we
 
571
                 * mustn't exit from the function execution with a stacked
 
572
                 * AfterTrigger level still active.  We are careful not to select
 
573
                 * lazyEval mode for any statement that could possibly queue triggers.
 
574
                 */
 
575
                int                     eflags;
 
576
 
 
577
                if (es->lazyEval)
 
578
                        eflags = EXEC_FLAG_SKIP_TRIGGERS;
 
579
                else
 
580
                        eflags = 0;                     /* default run-to-completion flags */
 
581
                ExecutorStart(es->qd, eflags);
 
582
        }
 
583
 
 
584
        es->status = F_EXEC_RUN;
 
585
}
 
586
 
 
587
/* Run one execution_state; either to completion or to first result row */
 
588
/* Returns true if we ran to completion */
 
589
static bool
 
590
postquel_getnext(execution_state *es, SQLFunctionCachePtr fcache)
 
591
{
 
592
        bool            result;
 
593
 
 
594
        if (es->qd->utilitystmt)
 
595
        {
 
596
                /* ProcessUtility needs the PlannedStmt for DECLARE CURSOR */
 
597
                ProcessUtility((es->qd->plannedstmt ?
 
598
                                                (Node *) es->qd->plannedstmt :
 
599
                                                es->qd->utilitystmt),
 
600
                                           fcache->src,
 
601
                                           es->qd->params,
 
602
                                           false,       /* not top level */
 
603
                                           es->qd->dest,
 
604
                                           NULL);
 
605
                result = true;                  /* never stops early */
 
606
        }
 
607
        else
 
608
        {
 
609
                /* Run regular commands to completion unless lazyEval */
 
610
                long            count = (es->lazyEval) ? 1L : 0L;
 
611
 
 
612
                ExecutorRun(es->qd, ForwardScanDirection, count);
 
613
 
 
614
                /*
 
615
                 * If we requested run to completion OR there was no tuple returned,
 
616
                 * command must be complete.
 
617
                 */
 
618
                result = (count == 0L || es->qd->estate->es_processed == 0);
 
619
        }
 
620
 
 
621
        return result;
 
622
}
 
623
 
 
624
/* Shut down execution of one execution_state node */
 
625
static void
 
626
postquel_end(execution_state *es)
 
627
{
 
628
        /* mark status done to ensure we don't do ExecutorEnd twice */
 
629
        es->status = F_EXEC_DONE;
 
630
 
 
631
        /* Utility commands don't need Executor. */
 
632
        if (es->qd->utilitystmt == NULL)
 
633
        {
 
634
                ExecutorFinish(es->qd);
 
635
                ExecutorEnd(es->qd);
 
636
        }
 
637
 
 
638
        (*es->qd->dest->rDestroy) (es->qd->dest);
 
639
 
 
640
        FreeQueryDesc(es->qd);
 
641
        es->qd = NULL;
 
642
}
 
643
 
 
644
/* Build ParamListInfo array representing current arguments */
 
645
static void
 
646
postquel_sub_params(SQLFunctionCachePtr fcache,
 
647
                                        FunctionCallInfo fcinfo)
 
648
{
 
649
        int                     nargs = fcinfo->nargs;
 
650
 
 
651
        if (nargs > 0)
 
652
        {
 
653
                ParamListInfo paramLI;
 
654
                int                     i;
 
655
 
 
656
                if (fcache->paramLI == NULL)
 
657
                {
 
658
                        /* sizeof(ParamListInfoData) includes the first array element */
 
659
                        paramLI = (ParamListInfo) palloc(sizeof(ParamListInfoData) +
 
660
                                                                          (nargs - 1) * sizeof(ParamExternData));
 
661
                        /* we have static list of params, so no hooks needed */
 
662
                        paramLI->paramFetch = NULL;
 
663
                        paramLI->paramFetchArg = NULL;
 
664
                        paramLI->parserSetup = NULL;
 
665
                        paramLI->parserSetupArg = NULL;
 
666
                        paramLI->numParams = nargs;
 
667
                        fcache->paramLI = paramLI;
 
668
                }
 
669
                else
 
670
                {
 
671
                        paramLI = fcache->paramLI;
 
672
                        Assert(paramLI->numParams == nargs);
 
673
                }
 
674
 
 
675
                for (i = 0; i < nargs; i++)
 
676
                {
 
677
                        ParamExternData *prm = &paramLI->params[i];
 
678
 
 
679
                        prm->value = fcinfo->arg[i];
 
680
                        prm->isnull = fcinfo->argnull[i];
 
681
                        prm->pflags = 0;
 
682
                        prm->ptype = fcache->pinfo->argtypes[i];
 
683
                }
 
684
        }
 
685
        else
 
686
                fcache->paramLI = NULL;
 
687
}
 
688
 
 
689
/*
 
690
 * Extract the SQL function's value from a single result row.  This is used
 
691
 * both for scalar (non-set) functions and for each row of a lazy-eval set
 
692
 * result.
 
693
 */
 
694
static Datum
 
695
postquel_get_single_result(TupleTableSlot *slot,
 
696
                                                   FunctionCallInfo fcinfo,
 
697
                                                   SQLFunctionCachePtr fcache,
 
698
                                                   MemoryContext resultcontext)
 
699
{
 
700
        Datum           value;
 
701
        MemoryContext oldcontext;
 
702
 
 
703
        /*
 
704
         * Set up to return the function value.  For pass-by-reference datatypes,
 
705
         * be sure to allocate the result in resultcontext, not the current memory
 
706
         * context (which has query lifespan).  We can't leave the data in the
 
707
         * TupleTableSlot because we intend to clear the slot before returning.
 
708
         */
 
709
        oldcontext = MemoryContextSwitchTo(resultcontext);
 
710
 
 
711
        if (fcache->returnsTuple)
 
712
        {
 
713
                /* We must return the whole tuple as a Datum. */
 
714
                fcinfo->isnull = false;
 
715
                value = ExecFetchSlotTupleDatum(slot);
 
716
                value = datumCopy(value, fcache->typbyval, fcache->typlen);
 
717
        }
 
718
        else
 
719
        {
 
720
                /*
 
721
                 * Returning a scalar, which we have to extract from the first column
 
722
                 * of the SELECT result, and then copy into result context if needed.
 
723
                 */
 
724
                value = slot_getattr(slot, 1, &(fcinfo->isnull));
 
725
 
 
726
                if (!fcinfo->isnull)
 
727
                        value = datumCopy(value, fcache->typbyval, fcache->typlen);
 
728
        }
 
729
 
 
730
        MemoryContextSwitchTo(oldcontext);
 
731
 
 
732
        return value;
 
733
}
 
734
 
 
735
/*
 
736
 * fmgr_sql: function call manager for SQL functions
 
737
 */
 
738
Datum
 
739
fmgr_sql(PG_FUNCTION_ARGS)
 
740
{
 
741
        MemoryContext oldcontext;
 
742
        SQLFunctionCachePtr fcache;
 
743
        ErrorContextCallback sqlerrcontext;
 
744
        bool            randomAccess;
 
745
        bool            lazyEvalOK;
 
746
        bool            is_first;
 
747
        bool            pushed_snapshot;
 
748
        execution_state *es;
 
749
        TupleTableSlot *slot;
 
750
        Datum           result;
 
751
        List       *eslist;
 
752
        ListCell   *eslc;
 
753
 
 
754
        /*
 
755
         * Switch to context in which the fcache lives.  This ensures that
 
756
         * parsetrees, plans, etc, will have sufficient lifetime.  The
 
757
         * sub-executor is responsible for deleting per-tuple information.
 
758
         */
 
759
        oldcontext = MemoryContextSwitchTo(fcinfo->flinfo->fn_mcxt);
 
760
 
 
761
        /*
 
762
         * Setup error traceback support for ereport()
 
763
         */
 
764
        sqlerrcontext.callback = sql_exec_error_callback;
 
765
        sqlerrcontext.arg = fcinfo->flinfo;
 
766
        sqlerrcontext.previous = error_context_stack;
 
767
        error_context_stack = &sqlerrcontext;
 
768
 
 
769
        /* Check call context */
 
770
        if (fcinfo->flinfo->fn_retset)
 
771
        {
 
772
                ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
 
773
 
 
774
                /*
 
775
                 * For simplicity, we require callers to support both set eval modes.
 
776
                 * There are cases where we must use one or must use the other, and
 
777
                 * it's not really worthwhile to postpone the check till we know. But
 
778
                 * note we do not require caller to provide an expectedDesc.
 
779
                 */
 
780
                if (!rsi || !IsA(rsi, ReturnSetInfo) ||
 
781
                        (rsi->allowedModes & SFRM_ValuePerCall) == 0 ||
 
782
                        (rsi->allowedModes & SFRM_Materialize) == 0)
 
783
                        ereport(ERROR,
 
784
                                        (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 
785
                                         errmsg("set-valued function called in context that cannot accept a set")));
 
786
                randomAccess = rsi->allowedModes & SFRM_Materialize_Random;
 
787
                lazyEvalOK = !(rsi->allowedModes & SFRM_Materialize_Preferred);
 
788
        }
 
789
        else
 
790
        {
 
791
                randomAccess = false;
 
792
                lazyEvalOK = true;
 
793
        }
 
794
 
 
795
        /*
 
796
         * Initialize fcache (build plans) if first time through.
 
797
         */
 
798
        fcache = (SQLFunctionCachePtr) fcinfo->flinfo->fn_extra;
 
799
        if (fcache == NULL)
 
800
        {
 
801
                init_sql_fcache(fcinfo->flinfo, PG_GET_COLLATION(), lazyEvalOK);
 
802
                fcache = (SQLFunctionCachePtr) fcinfo->flinfo->fn_extra;
 
803
        }
 
804
        eslist = fcache->func_state;
 
805
 
 
806
        /*
 
807
         * Find first unfinished query in function, and note whether it's the
 
808
         * first query.
 
809
         */
 
810
        es = NULL;
 
811
        is_first = true;
 
812
        foreach(eslc, eslist)
 
813
        {
 
814
                es = (execution_state *) lfirst(eslc);
 
815
 
 
816
                while (es && es->status == F_EXEC_DONE)
 
817
                {
 
818
                        is_first = false;
 
819
                        es = es->next;
 
820
                }
 
821
 
 
822
                if (es)
 
823
                        break;
 
824
        }
 
825
 
 
826
        /*
 
827
         * Convert params to appropriate format if starting a fresh execution. (If
 
828
         * continuing execution, we can re-use prior params.)
 
829
         */
 
830
        if (is_first && es && es->status == F_EXEC_START)
 
831
                postquel_sub_params(fcache, fcinfo);
 
832
 
 
833
        /*
 
834
         * Build tuplestore to hold results, if we don't have one already. Note
 
835
         * it's in the query-lifespan context.
 
836
         */
 
837
        if (!fcache->tstore)
 
838
                fcache->tstore = tuplestore_begin_heap(randomAccess, false, work_mem);
 
839
 
 
840
        /*
 
841
         * Execute each command in the function one after another until we either
 
842
         * run out of commands or get a result row from a lazily-evaluated SELECT.
 
843
         *
 
844
         * Notes about snapshot management:
 
845
         *
 
846
         * In a read-only function, we just use the surrounding query's snapshot.
 
847
         *
 
848
         * In a non-read-only function, we rely on the fact that we'll never
 
849
         * suspend execution between queries of the function: the only reason to
 
850
         * suspend execution before completion is if we are returning a row from a
 
851
         * lazily-evaluated SELECT.  So, when first entering this loop, we'll
 
852
         * either start a new query (and push a fresh snapshot) or re-establish
 
853
         * the active snapshot from the existing query descriptor.      If we need to
 
854
         * start a new query in a subsequent execution of the loop, either we need
 
855
         * a fresh snapshot (and pushed_snapshot is false) or the existing
 
856
         * snapshot is on the active stack and we can just bump its command ID.
 
857
         */
 
858
        pushed_snapshot = false;
 
859
        while (es)
 
860
        {
 
861
                bool            completed;
 
862
 
 
863
                if (es->status == F_EXEC_START)
 
864
                {
 
865
                        /*
 
866
                         * If not read-only, be sure to advance the command counter for
 
867
                         * each command, so that all work to date in this transaction is
 
868
                         * visible.  Take a new snapshot if we don't have one yet,
 
869
                         * otherwise just bump the command ID in the existing snapshot.
 
870
                         */
 
871
                        if (!fcache->readonly_func)
 
872
                        {
 
873
                                CommandCounterIncrement();
 
874
                                if (!pushed_snapshot)
 
875
                                {
 
876
                                        PushActiveSnapshot(GetTransactionSnapshot());
 
877
                                        pushed_snapshot = true;
 
878
                                }
 
879
                                else
 
880
                                        UpdateActiveSnapshotCommandId();
 
881
                        }
 
882
 
 
883
                        postquel_start(es, fcache);
 
884
                }
 
885
                else if (!fcache->readonly_func && !pushed_snapshot)
 
886
                {
 
887
                        /* Re-establish active snapshot when re-entering function */
 
888
                        PushActiveSnapshot(es->qd->snapshot);
 
889
                        pushed_snapshot = true;
 
890
                }
 
891
 
 
892
                completed = postquel_getnext(es, fcache);
 
893
 
 
894
                /*
 
895
                 * If we ran the command to completion, we can shut it down now. Any
 
896
                 * row(s) we need to return are safely stashed in the tuplestore, and
 
897
                 * we want to be sure that, for example, AFTER triggers get fired
 
898
                 * before we return anything.  Also, if the function doesn't return
 
899
                 * set, we can shut it down anyway because it must be a SELECT and we
 
900
                 * don't care about fetching any more result rows.
 
901
                 */
 
902
                if (completed || !fcache->returnsSet)
 
903
                        postquel_end(es);
 
904
 
 
905
                /*
 
906
                 * Break from loop if we didn't shut down (implying we got a
 
907
                 * lazily-evaluated row).  Otherwise we'll press on till the whole
 
908
                 * function is done, relying on the tuplestore to keep hold of the
 
909
                 * data to eventually be returned.      This is necessary since an
 
910
                 * INSERT/UPDATE/DELETE RETURNING that sets the result might be
 
911
                 * followed by additional rule-inserted commands, and we want to
 
912
                 * finish doing all those commands before we return anything.
 
913
                 */
 
914
                if (es->status != F_EXEC_DONE)
 
915
                        break;
 
916
 
 
917
                /*
 
918
                 * Advance to next execution_state, which might be in the next list.
 
919
                 */
 
920
                es = es->next;
 
921
                while (!es)
 
922
                {
 
923
                        eslc = lnext(eslc);
 
924
                        if (!eslc)
 
925
                                break;                  /* end of function */
 
926
 
 
927
                        es = (execution_state *) lfirst(eslc);
 
928
 
 
929
                        /*
 
930
                         * Flush the current snapshot so that we will take a new one for
 
931
                         * the new query list.  This ensures that new snaps are taken at
 
932
                         * original-query boundaries, matching the behavior of interactive
 
933
                         * execution.
 
934
                         */
 
935
                        if (pushed_snapshot)
 
936
                        {
 
937
                                PopActiveSnapshot();
 
938
                                pushed_snapshot = false;
 
939
                        }
 
940
                }
 
941
        }
 
942
 
 
943
        /*
 
944
         * The tuplestore now contains whatever row(s) we are supposed to return.
 
945
         */
 
946
        if (fcache->returnsSet)
 
947
        {
 
948
                ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
 
949
 
 
950
                if (es)
 
951
                {
 
952
                        /*
 
953
                         * If we stopped short of being done, we must have a lazy-eval
 
954
                         * row.
 
955
                         */
 
956
                        Assert(es->lazyEval);
 
957
                        /* Re-use the junkfilter's output slot to fetch back the tuple */
 
958
                        Assert(fcache->junkFilter);
 
959
                        slot = fcache->junkFilter->jf_resultSlot;
 
960
                        if (!tuplestore_gettupleslot(fcache->tstore, true, false, slot))
 
961
                                elog(ERROR, "failed to fetch lazy-eval tuple");
 
962
                        /* Extract the result as a datum, and copy out from the slot */
 
963
                        result = postquel_get_single_result(slot, fcinfo,
 
964
                                                                                                fcache, oldcontext);
 
965
                        /* Clear the tuplestore, but keep it for next time */
 
966
                        /* NB: this might delete the slot's content, but we don't care */
 
967
                        tuplestore_clear(fcache->tstore);
 
968
 
 
969
                        /*
 
970
                         * Let caller know we're not finished.
 
971
                         */
 
972
                        rsi->isDone = ExprMultipleResult;
 
973
 
 
974
                        /*
 
975
                         * Ensure we will get shut down cleanly if the exprcontext is not
 
976
                         * run to completion.
 
977
                         */
 
978
                        if (!fcache->shutdown_reg)
 
979
                        {
 
980
                                RegisterExprContextCallback(rsi->econtext,
 
981
                                                                                        ShutdownSQLFunction,
 
982
                                                                                        PointerGetDatum(fcache));
 
983
                                fcache->shutdown_reg = true;
 
984
                        }
 
985
                }
 
986
                else if (fcache->lazyEval)
 
987
                {
 
988
                        /*
 
989
                         * We are done with a lazy evaluation.  Clean up.
 
990
                         */
 
991
                        tuplestore_clear(fcache->tstore);
 
992
 
 
993
                        /*
 
994
                         * Let caller know we're finished.
 
995
                         */
 
996
                        rsi->isDone = ExprEndResult;
 
997
 
 
998
                        fcinfo->isnull = true;
 
999
                        result = (Datum) 0;
 
1000
 
 
1001
                        /* Deregister shutdown callback, if we made one */
 
1002
                        if (fcache->shutdown_reg)
 
1003
                        {
 
1004
                                UnregisterExprContextCallback(rsi->econtext,
 
1005
                                                                                          ShutdownSQLFunction,
 
1006
                                                                                          PointerGetDatum(fcache));
 
1007
                                fcache->shutdown_reg = false;
 
1008
                        }
 
1009
                }
 
1010
                else
 
1011
                {
 
1012
                        /*
 
1013
                         * We are done with a non-lazy evaluation.      Return whatever is in
 
1014
                         * the tuplestore.      (It is now caller's responsibility to free the
 
1015
                         * tuplestore when done.)
 
1016
                         */
 
1017
                        rsi->returnMode = SFRM_Materialize;
 
1018
                        rsi->setResult = fcache->tstore;
 
1019
                        fcache->tstore = NULL;
 
1020
                        /* must copy desc because execQual will free it */
 
1021
                        if (fcache->junkFilter)
 
1022
                                rsi->setDesc = CreateTupleDescCopy(fcache->junkFilter->jf_cleanTupType);
 
1023
 
 
1024
                        fcinfo->isnull = true;
 
1025
                        result = (Datum) 0;
 
1026
 
 
1027
                        /* Deregister shutdown callback, if we made one */
 
1028
                        if (fcache->shutdown_reg)
 
1029
                        {
 
1030
                                UnregisterExprContextCallback(rsi->econtext,
 
1031
                                                                                          ShutdownSQLFunction,
 
1032
                                                                                          PointerGetDatum(fcache));
 
1033
                                fcache->shutdown_reg = false;
 
1034
                        }
 
1035
                }
 
1036
        }
 
1037
        else
 
1038
        {
 
1039
                /*
 
1040
                 * Non-set function.  If we got a row, return it; else return NULL.
 
1041
                 */
 
1042
                if (fcache->junkFilter)
 
1043
                {
 
1044
                        /* Re-use the junkfilter's output slot to fetch back the tuple */
 
1045
                        slot = fcache->junkFilter->jf_resultSlot;
 
1046
                        if (tuplestore_gettupleslot(fcache->tstore, true, false, slot))
 
1047
                                result = postquel_get_single_result(slot, fcinfo,
 
1048
                                                                                                        fcache, oldcontext);
 
1049
                        else
 
1050
                        {
 
1051
                                fcinfo->isnull = true;
 
1052
                                result = (Datum) 0;
 
1053
                        }
 
1054
                }
 
1055
                else
 
1056
                {
 
1057
                        /* Should only get here for VOID functions */
 
1058
                        Assert(fcache->rettype == VOIDOID);
 
1059
                        fcinfo->isnull = true;
 
1060
                        result = (Datum) 0;
 
1061
                }
 
1062
 
 
1063
                /* Clear the tuplestore, but keep it for next time */
 
1064
                tuplestore_clear(fcache->tstore);
 
1065
        }
 
1066
 
 
1067
        /* Pop snapshot if we have pushed one */
 
1068
        if (pushed_snapshot)
 
1069
                PopActiveSnapshot();
 
1070
 
 
1071
        /*
 
1072
         * If we've gone through every command in the function, we are done. Reset
 
1073
         * the execution states to start over again on next call.
 
1074
         */
 
1075
        if (es == NULL)
 
1076
        {
 
1077
                foreach(eslc, fcache->func_state)
 
1078
                {
 
1079
                        es = (execution_state *) lfirst(eslc);
 
1080
                        while (es)
 
1081
                        {
 
1082
                                es->status = F_EXEC_START;
 
1083
                                es = es->next;
 
1084
                        }
 
1085
                }
 
1086
        }
 
1087
 
 
1088
        error_context_stack = sqlerrcontext.previous;
 
1089
 
 
1090
        MemoryContextSwitchTo(oldcontext);
 
1091
 
 
1092
        return result;
 
1093
}
 
1094
 
 
1095
 
 
1096
/*
 
1097
 * error context callback to let us supply a call-stack traceback
 
1098
 */
 
1099
static void
 
1100
sql_exec_error_callback(void *arg)
 
1101
{
 
1102
        FmgrInfo   *flinfo = (FmgrInfo *) arg;
 
1103
        SQLFunctionCachePtr fcache = (SQLFunctionCachePtr) flinfo->fn_extra;
 
1104
        int                     syntaxerrposition;
 
1105
 
 
1106
        /*
 
1107
         * We can do nothing useful if init_sql_fcache() didn't get as far as
 
1108
         * saving the function name
 
1109
         */
 
1110
        if (fcache == NULL || fcache->fname == NULL)
 
1111
                return;
 
1112
 
 
1113
        /*
 
1114
         * If there is a syntax error position, convert to internal syntax error
 
1115
         */
 
1116
        syntaxerrposition = geterrposition();
 
1117
        if (syntaxerrposition > 0 && fcache->src != NULL)
 
1118
        {
 
1119
                errposition(0);
 
1120
                internalerrposition(syntaxerrposition);
 
1121
                internalerrquery(fcache->src);
 
1122
        }
 
1123
 
 
1124
        /*
 
1125
         * Try to determine where in the function we failed.  If there is a query
 
1126
         * with non-null QueryDesc, finger it.  (We check this rather than looking
 
1127
         * for F_EXEC_RUN state, so that errors during ExecutorStart or
 
1128
         * ExecutorEnd are blamed on the appropriate query; see postquel_start and
 
1129
         * postquel_end.)
 
1130
         */
 
1131
        if (fcache->func_state)
 
1132
        {
 
1133
                execution_state *es;
 
1134
                int                     query_num;
 
1135
                ListCell   *lc;
 
1136
 
 
1137
                es = NULL;
 
1138
                query_num = 1;
 
1139
                foreach(lc, fcache->func_state)
 
1140
                {
 
1141
                        es = (execution_state *) lfirst(lc);
 
1142
                        while (es)
 
1143
                        {
 
1144
                                if (es->qd)
 
1145
                                {
 
1146
                                        errcontext("SQL function \"%s\" statement %d",
 
1147
                                                           fcache->fname, query_num);
 
1148
                                        break;
 
1149
                                }
 
1150
                                es = es->next;
 
1151
                        }
 
1152
                        if (es)
 
1153
                                break;
 
1154
                        query_num++;
 
1155
                }
 
1156
                if (es == NULL)
 
1157
                {
 
1158
                        /*
 
1159
                         * couldn't identify a running query; might be function entry,
 
1160
                         * function exit, or between queries.
 
1161
                         */
 
1162
                        errcontext("SQL function \"%s\"", fcache->fname);
 
1163
                }
 
1164
        }
 
1165
        else
 
1166
        {
 
1167
                /*
 
1168
                 * Assume we failed during init_sql_fcache().  (It's possible that the
 
1169
                 * function actually has an empty body, but in that case we may as
 
1170
                 * well report all errors as being "during startup".)
 
1171
                 */
 
1172
                errcontext("SQL function \"%s\" during startup", fcache->fname);
 
1173
        }
 
1174
}
 
1175
 
 
1176
 
 
1177
/*
 
1178
 * callback function in case a function-returning-set needs to be shut down
 
1179
 * before it has been run to completion
 
1180
 */
 
1181
static void
 
1182
ShutdownSQLFunction(Datum arg)
 
1183
{
 
1184
        SQLFunctionCachePtr fcache = (SQLFunctionCachePtr) DatumGetPointer(arg);
 
1185
        execution_state *es;
 
1186
        ListCell   *lc;
 
1187
 
 
1188
        foreach(lc, fcache->func_state)
 
1189
        {
 
1190
                es = (execution_state *) lfirst(lc);
 
1191
                while (es)
 
1192
                {
 
1193
                        /* Shut down anything still running */
 
1194
                        if (es->status == F_EXEC_RUN)
 
1195
                        {
 
1196
                                /* Re-establish active snapshot for any called functions */
 
1197
                                if (!fcache->readonly_func)
 
1198
                                        PushActiveSnapshot(es->qd->snapshot);
 
1199
 
 
1200
                                postquel_end(es);
 
1201
 
 
1202
                                if (!fcache->readonly_func)
 
1203
                                        PopActiveSnapshot();
 
1204
                        }
 
1205
 
 
1206
                        /* Reset states to START in case we're called again */
 
1207
                        es->status = F_EXEC_START;
 
1208
                        es = es->next;
 
1209
                }
 
1210
        }
 
1211
 
 
1212
        /* Release tuplestore if we have one */
 
1213
        if (fcache->tstore)
 
1214
                tuplestore_end(fcache->tstore);
 
1215
        fcache->tstore = NULL;
 
1216
 
 
1217
        /* execUtils will deregister the callback... */
 
1218
        fcache->shutdown_reg = false;
 
1219
}
 
1220
 
 
1221
 
 
1222
/*
 
1223
 * check_sql_fn_retval() -- check return value of a list of sql parse trees.
 
1224
 *
 
1225
 * The return value of a sql function is the value returned by the last
 
1226
 * canSetTag query in the function.  We do some ad-hoc type checking here
 
1227
 * to be sure that the user is returning the type he claims.  There are
 
1228
 * also a couple of strange-looking features to assist callers in dealing
 
1229
 * with allowed special cases, such as binary-compatible result types.
 
1230
 *
 
1231
 * For a polymorphic function the passed rettype must be the actual resolved
 
1232
 * output type of the function; we should never see a polymorphic pseudotype
 
1233
 * such as ANYELEMENT as rettype.  (This means we can't check the type during
 
1234
 * function definition of a polymorphic function.)
 
1235
 *
 
1236
 * This function returns true if the sql function returns the entire tuple
 
1237
 * result of its final statement, or false if it returns just the first column
 
1238
 * result of that statement.  It throws an error if the final statement doesn't
 
1239
 * return the right type at all.
 
1240
 *
 
1241
 * Note that because we allow "SELECT rowtype_expression", the result can be
 
1242
 * false even when the declared function return type is a rowtype.
 
1243
 *
 
1244
 * If modifyTargetList isn't NULL, the function will modify the final
 
1245
 * statement's targetlist in two cases:
 
1246
 * (1) if the tlist returns values that are binary-coercible to the expected
 
1247
 * type rather than being exactly the expected type.  RelabelType nodes will
 
1248
 * be inserted to make the result types match exactly.
 
1249
 * (2) if there are dropped columns in the declared result rowtype.  NULL
 
1250
 * output columns will be inserted in the tlist to match them.
 
1251
 * (Obviously the caller must pass a parsetree that is okay to modify when
 
1252
 * using this flag.)  Note that this flag does not affect whether the tlist is
 
1253
 * considered to be a legal match to the result type, only how we react to
 
1254
 * allowed not-exact-match cases.  *modifyTargetList will be set true iff
 
1255
 * we had to make any "dangerous" changes that could modify the semantics of
 
1256
 * the statement.  If it is set true, the caller should not use the modified
 
1257
 * statement, but for simplicity we apply the changes anyway.
 
1258
 *
 
1259
 * If junkFilter isn't NULL, then *junkFilter is set to a JunkFilter defined
 
1260
 * to convert the function's tuple result to the correct output tuple type.
 
1261
 * Exception: if the function is defined to return VOID then *junkFilter is
 
1262
 * set to NULL.
 
1263
 */
 
1264
bool
 
1265
check_sql_fn_retval(Oid func_id, Oid rettype, List *queryTreeList,
 
1266
                                        bool *modifyTargetList,
 
1267
                                        JunkFilter **junkFilter)
 
1268
{
 
1269
        Query      *parse;
 
1270
        List      **tlist_ptr;
 
1271
        List       *tlist;
 
1272
        int                     tlistlen;
 
1273
        char            fn_typtype;
 
1274
        Oid                     restype;
 
1275
        ListCell   *lc;
 
1276
 
 
1277
        AssertArg(!IsPolymorphicType(rettype));
 
1278
 
 
1279
        if (modifyTargetList)
 
1280
                *modifyTargetList = false;              /* initialize for no change */
 
1281
        if (junkFilter)
 
1282
                *junkFilter = NULL;             /* initialize in case of VOID result */
 
1283
 
 
1284
        /*
 
1285
         * Find the last canSetTag query in the list.  This isn't necessarily the
 
1286
         * last parsetree, because rule rewriting can insert queries after what
 
1287
         * the user wrote.
 
1288
         */
 
1289
        parse = NULL;
 
1290
        foreach(lc, queryTreeList)
 
1291
        {
 
1292
                Query      *q = (Query *) lfirst(lc);
 
1293
 
 
1294
                if (q->canSetTag)
 
1295
                        parse = q;
 
1296
        }
 
1297
 
 
1298
        /*
 
1299
         * If it's a plain SELECT, it returns whatever the targetlist says.
 
1300
         * Otherwise, if it's INSERT/UPDATE/DELETE with RETURNING, it returns
 
1301
         * that. Otherwise, the function return type must be VOID.
 
1302
         *
 
1303
         * Note: eventually replace this test with QueryReturnsTuples?  We'd need
 
1304
         * a more general method of determining the output type, though.  Also, it
 
1305
         * seems too dangerous to consider FETCH or EXECUTE as returning a
 
1306
         * determinable rowtype, since they depend on relatively short-lived
 
1307
         * entities.
 
1308
         */
 
1309
        if (parse &&
 
1310
                parse->commandType == CMD_SELECT &&
 
1311
                parse->utilityStmt == NULL &&
 
1312
                parse->intoClause == NULL)
 
1313
        {
 
1314
                tlist_ptr = &parse->targetList;
 
1315
                tlist = parse->targetList;
 
1316
        }
 
1317
        else if (parse &&
 
1318
                         (parse->commandType == CMD_INSERT ||
 
1319
                          parse->commandType == CMD_UPDATE ||
 
1320
                          parse->commandType == CMD_DELETE) &&
 
1321
                         parse->returningList)
 
1322
        {
 
1323
                tlist_ptr = &parse->returningList;
 
1324
                tlist = parse->returningList;
 
1325
        }
 
1326
        else
 
1327
        {
 
1328
                /* Empty function body, or last statement is a utility command */
 
1329
                if (rettype != VOIDOID)
 
1330
                        ereport(ERROR,
 
1331
                                        (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
 
1332
                         errmsg("return type mismatch in function declared to return %s",
 
1333
                                        format_type_be(rettype)),
 
1334
                                         errdetail("Function's final statement must be SELECT or INSERT/UPDATE/DELETE RETURNING.")));
 
1335
                return false;
 
1336
        }
 
1337
 
 
1338
        /*
 
1339
         * OK, check that the targetlist returns something matching the declared
 
1340
         * type.  (We used to insist that the declared type not be VOID in this
 
1341
         * case, but that makes it hard to write a void function that exits after
 
1342
         * calling another void function.  Instead, we insist that the tlist
 
1343
         * return void ... so void is treated as if it were a scalar type below.)
 
1344
         */
 
1345
 
 
1346
        /*
 
1347
         * Count the non-junk entries in the result targetlist.
 
1348
         */
 
1349
        tlistlen = ExecCleanTargetListLength(tlist);
 
1350
 
 
1351
        fn_typtype = get_typtype(rettype);
 
1352
 
 
1353
        if (fn_typtype == TYPTYPE_BASE ||
 
1354
                fn_typtype == TYPTYPE_DOMAIN ||
 
1355
                fn_typtype == TYPTYPE_ENUM ||
 
1356
                rettype == VOIDOID)
 
1357
        {
 
1358
                /*
 
1359
                 * For scalar-type returns, the target list must have exactly one
 
1360
                 * non-junk entry, and its type must agree with what the user
 
1361
                 * declared; except we allow binary-compatible types too.
 
1362
                 */
 
1363
                TargetEntry *tle;
 
1364
 
 
1365
                if (tlistlen != 1)
 
1366
                        ereport(ERROR,
 
1367
                                        (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
 
1368
                         errmsg("return type mismatch in function declared to return %s",
 
1369
                                        format_type_be(rettype)),
 
1370
                          errdetail("Final statement must return exactly one column.")));
 
1371
 
 
1372
                /* We assume here that non-junk TLEs must come first in tlists */
 
1373
                tle = (TargetEntry *) linitial(tlist);
 
1374
                Assert(!tle->resjunk);
 
1375
 
 
1376
                restype = exprType((Node *) tle->expr);
 
1377
                if (!IsBinaryCoercible(restype, rettype))
 
1378
                        ereport(ERROR,
 
1379
                                        (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
 
1380
                         errmsg("return type mismatch in function declared to return %s",
 
1381
                                        format_type_be(rettype)),
 
1382
                                         errdetail("Actual return type is %s.",
 
1383
                                                           format_type_be(restype))));
 
1384
                if (modifyTargetList && restype != rettype)
 
1385
                {
 
1386
                        tle->expr = (Expr *) makeRelabelType(tle->expr,
 
1387
                                                                                                 rettype,
 
1388
                                                                                                 -1,
 
1389
                                                                                                 get_typcollation(rettype),
 
1390
                                                                                                 COERCE_DONTCARE);
 
1391
                        /* Relabel is dangerous if TLE is a sort/group or setop column */
 
1392
                        if (tle->ressortgroupref != 0 || parse->setOperations)
 
1393
                                *modifyTargetList = true;
 
1394
                }
 
1395
 
 
1396
                /* Set up junk filter if needed */
 
1397
                if (junkFilter)
 
1398
                        *junkFilter = ExecInitJunkFilter(tlist, false, NULL);
 
1399
        }
 
1400
        else if (fn_typtype == TYPTYPE_COMPOSITE || rettype == RECORDOID)
 
1401
        {
 
1402
                /* Returns a rowtype */
 
1403
                TupleDesc       tupdesc;
 
1404
                int                     tupnatts;       /* physical number of columns in tuple */
 
1405
                int                     tuplogcols; /* # of nondeleted columns in tuple */
 
1406
                int                     colindex;       /* physical column index */
 
1407
                List       *newtlist;   /* new non-junk tlist entries */
 
1408
                List       *junkattrs;  /* new junk tlist entries */
 
1409
 
 
1410
                /*
 
1411
                 * If the target list is of length 1, and the type of the varnode in
 
1412
                 * the target list matches the declared return type, this is okay.
 
1413
                 * This can happen, for example, where the body of the function is
 
1414
                 * 'SELECT func2()', where func2 has the same composite return type as
 
1415
                 * the function that's calling it.
 
1416
                 *
 
1417
                 * XXX Note that if rettype is RECORD, the IsBinaryCoercible check
 
1418
                 * will succeed for any composite restype.      For the moment we rely on
 
1419
                 * runtime type checking to catch any discrepancy, but it'd be nice to
 
1420
                 * do better at parse time.
 
1421
                 */
 
1422
                if (tlistlen == 1)
 
1423
                {
 
1424
                        TargetEntry *tle = (TargetEntry *) linitial(tlist);
 
1425
 
 
1426
                        Assert(!tle->resjunk);
 
1427
                        restype = exprType((Node *) tle->expr);
 
1428
                        if (IsBinaryCoercible(restype, rettype))
 
1429
                        {
 
1430
                                if (modifyTargetList && restype != rettype)
 
1431
                                {
 
1432
                                        tle->expr = (Expr *) makeRelabelType(tle->expr,
 
1433
                                                                                                                 rettype,
 
1434
                                                                                                                 -1,
 
1435
                                                                                                   get_typcollation(rettype),
 
1436
                                                                                                                 COERCE_DONTCARE);
 
1437
                                        /* Relabel is dangerous if sort/group or setop column */
 
1438
                                        if (tle->ressortgroupref != 0 || parse->setOperations)
 
1439
                                                *modifyTargetList = true;
 
1440
                                }
 
1441
                                /* Set up junk filter if needed */
 
1442
                                if (junkFilter)
 
1443
                                        *junkFilter = ExecInitJunkFilter(tlist, false, NULL);
 
1444
                                return false;   /* NOT returning whole tuple */
 
1445
                        }
 
1446
                }
 
1447
 
 
1448
                /* Is the rowtype fixed, or determined only at runtime? */
 
1449
                if (get_func_result_type(func_id, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
 
1450
                {
 
1451
                        /*
 
1452
                         * Assume we are returning the whole tuple. Crosschecking against
 
1453
                         * what the caller expects will happen at runtime.
 
1454
                         */
 
1455
                        if (junkFilter)
 
1456
                                *junkFilter = ExecInitJunkFilter(tlist, false, NULL);
 
1457
                        return true;
 
1458
                }
 
1459
                Assert(tupdesc);
 
1460
 
 
1461
                /*
 
1462
                 * Verify that the targetlist matches the return tuple type. We scan
 
1463
                 * the non-deleted attributes to ensure that they match the datatypes
 
1464
                 * of the non-resjunk columns.  For deleted attributes, insert NULL
 
1465
                 * result columns if the caller asked for that.
 
1466
                 */
 
1467
                tupnatts = tupdesc->natts;
 
1468
                tuplogcols = 0;                 /* we'll count nondeleted cols as we go */
 
1469
                colindex = 0;
 
1470
                newtlist = NIL;                 /* these are only used if modifyTargetList */
 
1471
                junkattrs = NIL;
 
1472
 
 
1473
                foreach(lc, tlist)
 
1474
                {
 
1475
                        TargetEntry *tle = (TargetEntry *) lfirst(lc);
 
1476
                        Form_pg_attribute attr;
 
1477
                        Oid                     tletype;
 
1478
                        Oid                     atttype;
 
1479
 
 
1480
                        if (tle->resjunk)
 
1481
                        {
 
1482
                                if (modifyTargetList)
 
1483
                                        junkattrs = lappend(junkattrs, tle);
 
1484
                                continue;
 
1485
                        }
 
1486
 
 
1487
                        do
 
1488
                        {
 
1489
                                colindex++;
 
1490
                                if (colindex > tupnatts)
 
1491
                                        ereport(ERROR,
 
1492
                                                        (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
 
1493
                                                         errmsg("return type mismatch in function declared to return %s",
 
1494
                                                                        format_type_be(rettype)),
 
1495
                                        errdetail("Final statement returns too many columns.")));
 
1496
                                attr = tupdesc->attrs[colindex - 1];
 
1497
                                if (attr->attisdropped && modifyTargetList)
 
1498
                                {
 
1499
                                        Expr       *null_expr;
 
1500
 
 
1501
                                        /* The type of the null we insert isn't important */
 
1502
                                        null_expr = (Expr *) makeConst(INT4OID,
 
1503
                                                                                                   -1,
 
1504
                                                                                                   InvalidOid,
 
1505
                                                                                                   sizeof(int32),
 
1506
                                                                                                   (Datum) 0,
 
1507
                                                                                                   true,                /* isnull */
 
1508
                                                                                                   true /* byval */ );
 
1509
                                        newtlist = lappend(newtlist,
 
1510
                                                                           makeTargetEntry(null_expr,
 
1511
                                                                                                           colindex,
 
1512
                                                                                                           NULL,
 
1513
                                                                                                           false));
 
1514
                                        /* NULL insertion is dangerous in a setop */
 
1515
                                        if (parse->setOperations)
 
1516
                                                *modifyTargetList = true;
 
1517
                                }
 
1518
                        } while (attr->attisdropped);
 
1519
                        tuplogcols++;
 
1520
 
 
1521
                        tletype = exprType((Node *) tle->expr);
 
1522
                        atttype = attr->atttypid;
 
1523
                        if (!IsBinaryCoercible(tletype, atttype))
 
1524
                                ereport(ERROR,
 
1525
                                                (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
 
1526
                                                 errmsg("return type mismatch in function declared to return %s",
 
1527
                                                                format_type_be(rettype)),
 
1528
                                                 errdetail("Final statement returns %s instead of %s at column %d.",
 
1529
                                                                   format_type_be(tletype),
 
1530
                                                                   format_type_be(atttype),
 
1531
                                                                   tuplogcols)));
 
1532
                        if (modifyTargetList)
 
1533
                        {
 
1534
                                if (tletype != atttype)
 
1535
                                {
 
1536
                                        tle->expr = (Expr *) makeRelabelType(tle->expr,
 
1537
                                                                                                                 atttype,
 
1538
                                                                                                                 -1,
 
1539
                                                                                                   get_typcollation(atttype),
 
1540
                                                                                                                 COERCE_DONTCARE);
 
1541
                                        /* Relabel is dangerous if sort/group or setop column */
 
1542
                                        if (tle->ressortgroupref != 0 || parse->setOperations)
 
1543
                                                *modifyTargetList = true;
 
1544
                                }
 
1545
                                tle->resno = colindex;
 
1546
                                newtlist = lappend(newtlist, tle);
 
1547
                        }
 
1548
                }
 
1549
 
 
1550
                /* remaining columns in tupdesc had better all be dropped */
 
1551
                for (colindex++; colindex <= tupnatts; colindex++)
 
1552
                {
 
1553
                        if (!tupdesc->attrs[colindex - 1]->attisdropped)
 
1554
                                ereport(ERROR,
 
1555
                                                (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
 
1556
                                                 errmsg("return type mismatch in function declared to return %s",
 
1557
                                                                format_type_be(rettype)),
 
1558
                                         errdetail("Final statement returns too few columns.")));
 
1559
                        if (modifyTargetList)
 
1560
                        {
 
1561
                                Expr       *null_expr;
 
1562
 
 
1563
                                /* The type of the null we insert isn't important */
 
1564
                                null_expr = (Expr *) makeConst(INT4OID,
 
1565
                                                                                           -1,
 
1566
                                                                                           InvalidOid,
 
1567
                                                                                           sizeof(int32),
 
1568
                                                                                           (Datum) 0,
 
1569
                                                                                           true,        /* isnull */
 
1570
                                                                                           true /* byval */ );
 
1571
                                newtlist = lappend(newtlist,
 
1572
                                                                   makeTargetEntry(null_expr,
 
1573
                                                                                                   colindex,
 
1574
                                                                                                   NULL,
 
1575
                                                                                                   false));
 
1576
                                /* NULL insertion is dangerous in a setop */
 
1577
                                if (parse->setOperations)
 
1578
                                        *modifyTargetList = true;
 
1579
                        }
 
1580
                }
 
1581
 
 
1582
                if (modifyTargetList)
 
1583
                {
 
1584
                        /* ensure resjunk columns are numbered correctly */
 
1585
                        foreach(lc, junkattrs)
 
1586
                        {
 
1587
                                TargetEntry *tle = (TargetEntry *) lfirst(lc);
 
1588
 
 
1589
                                tle->resno = colindex++;
 
1590
                        }
 
1591
                        /* replace the tlist with the modified one */
 
1592
                        *tlist_ptr = list_concat(newtlist, junkattrs);
 
1593
                }
 
1594
 
 
1595
                /* Set up junk filter if needed */
 
1596
                if (junkFilter)
 
1597
                        *junkFilter = ExecInitJunkFilterConversion(tlist,
 
1598
                                                                                                CreateTupleDescCopy(tupdesc),
 
1599
                                                                                                           NULL);
 
1600
 
 
1601
                /* Report that we are returning entire tuple result */
 
1602
                return true;
 
1603
        }
 
1604
        else
 
1605
                ereport(ERROR,
 
1606
                                (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
 
1607
                                 errmsg("return type %s is not supported for SQL functions",
 
1608
                                                format_type_be(rettype))));
 
1609
 
 
1610
        return false;
 
1611
}
 
1612
 
 
1613
 
 
1614
/*
 
1615
 * CreateSQLFunctionDestReceiver -- create a suitable DestReceiver object
 
1616
 */
 
1617
DestReceiver *
 
1618
CreateSQLFunctionDestReceiver(void)
 
1619
{
 
1620
        DR_sqlfunction *self = (DR_sqlfunction *) palloc0(sizeof(DR_sqlfunction));
 
1621
 
 
1622
        self->pub.receiveSlot = sqlfunction_receive;
 
1623
        self->pub.rStartup = sqlfunction_startup;
 
1624
        self->pub.rShutdown = sqlfunction_shutdown;
 
1625
        self->pub.rDestroy = sqlfunction_destroy;
 
1626
        self->pub.mydest = DestSQLFunction;
 
1627
 
 
1628
        /* private fields will be set by postquel_start */
 
1629
 
 
1630
        return (DestReceiver *) self;
 
1631
}
 
1632
 
 
1633
/*
 
1634
 * sqlfunction_startup --- executor startup
 
1635
 */
 
1636
static void
 
1637
sqlfunction_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
 
1638
{
 
1639
        /* no-op */
 
1640
}
 
1641
 
 
1642
/*
 
1643
 * sqlfunction_receive --- receive one tuple
 
1644
 */
 
1645
static void
 
1646
sqlfunction_receive(TupleTableSlot *slot, DestReceiver *self)
 
1647
{
 
1648
        DR_sqlfunction *myState = (DR_sqlfunction *) self;
 
1649
 
 
1650
        /* Filter tuple as needed */
 
1651
        slot = ExecFilterJunk(myState->filter, slot);
 
1652
 
 
1653
        /* Store the filtered tuple into the tuplestore */
 
1654
        tuplestore_puttupleslot(myState->tstore, slot);
 
1655
}
 
1656
 
 
1657
/*
 
1658
 * sqlfunction_shutdown --- executor end
 
1659
 */
 
1660
static void
 
1661
sqlfunction_shutdown(DestReceiver *self)
 
1662
{
 
1663
        /* no-op */
 
1664
}
 
1665
 
 
1666
/*
 
1667
 * sqlfunction_destroy --- release DestReceiver object
 
1668
 */
 
1669
static void
 
1670
sqlfunction_destroy(DestReceiver *self)
 
1671
{
 
1672
        pfree(self);
 
1673
}