~ubuntu-branches/ubuntu/trusty/postgresql-8.4/trusty

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-11 16:59:35 UTC
  • mfrom: (5.1.1 karmic)
  • Revision ID: james.westby@ubuntu.com-20090711165935-jfwin6gfrxf0gfsi
Tags: 8.4.0-2
* debian/libpq-dev.install: Ship catalog/genbki.h. (Closes: #536139)
* debian/rules: Drop --enable-cassert for final release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
 *
9
9
 *
10
10
 * IDENTIFICATION
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 $
12
12
 *
13
13
 *-------------------------------------------------------------------------
14
14
 */
101
101
 
102
102
/* non-export function prototypes */
103
103
static execution_state *init_execution_state(List *queryTree_list,
104
 
                                                                                         SQLFunctionCachePtr fcache,
105
 
                                                                                         bool lazyEvalOK);
 
104
                                         SQLFunctionCachePtr fcache,
 
105
                                         bool lazyEvalOK);
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);
168
168
 
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;
175
175
 
180
180
        }
181
181
 
182
182
        /*
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.
186
186
         *
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
191
191
         *
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
195
 
         * not have output.
 
194
         * output from a utility statement that check_sql_fn_retval deemed to not
 
195
         * have output.
196
196
         */
197
197
        if (lasttages && fcache->junkFilter)
198
198
        {
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.)
333
333
         *
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
459
459
                                                es->qd->utilitystmt),
460
460
                                           fcache->src,
461
461
                                           es->qd->params,
462
 
                                           false,               /* not top level */
 
462
                                           false,       /* not top level */
463
463
                                           es->qd->dest,
464
464
                                           NULL);
465
465
                result = true;                  /* never stops early */
566
566
        /*
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.
571
571
         */
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.
637
638
                 */
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)
642
642
                        ereport(ERROR,
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);
671
671
 
672
672
        /*
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.
675
675
         */
676
676
        if (!fcache->tstore)
677
677
                fcache->tstore = tuplestore_begin_heap(randomAccess, false, work_mem);
688
688
         */
689
689
        while (es)
690
690
        {
691
 
                bool    completed;
 
691
                bool            completed;
692
692
 
693
693
                if (es->status == F_EXEC_START)
694
694
                        postquel_start(es, fcache);
696
696
                completed = postquel_getnext(es, fcache);
697
697
 
698
698
                /*
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.
705
705
                 */
706
706
                if (completed || !fcache->returnsSet)
707
707
                        postquel_end(es);
708
708
 
709
709
                /*
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.
717
717
                 */
730
730
                if (es)
731
731
                {
732
732
                        /*
733
 
                         * If we stopped short of being done, we must have a lazy-eval row.
 
733
                         * If we stopped short of being done, we must have a lazy-eval
 
734
                         * row.
734
735
                         */
735
736
                        Assert(es->lazyEval);
736
737
                        /* Re-use the junkfilter's output slot to fetch back the tuple */
765
766
                else if (fcache->lazyEval)
766
767
                {
767
768
                        /*
768
 
                         * We are done with a lazy evaluation.  Clean up.
 
769
                         * We are done with a lazy evaluation.  Clean up.
769
770
                         */
770
771
                        tuplestore_clear(fcache->tstore);
771
772
 
789
790
                else
790
791
                {
791
792
                        /*
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.)
795
796
                         */
796
797
                        rsi->returnMode = SFRM_Materialize;
797
798
                        rsi->setResult = fcache->tstore;
844
845
        }
845
846
 
846
847
        /*
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.
849
850
         */
850
851
        if (es == NULL)
851
852
        {
997
998
 * function definition of a polymorphic function.)
998
999
 *
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.
1003
1004
 *
1029
1030
                *junkFilter = NULL;             /* initialize in case of VOID result */
1030
1031
 
1031
1032
        /*
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
 
1035
         * the user wrote.
1035
1036
         */
1036
1037
        parse = NULL;
1037
1038
        foreach(lc, queryTreeList)
1038
1039
        {
1039
 
                Query  *q = (Query *) lfirst(lc);
 
1040
                Query      *q = (Query *) lfirst(lc);
1040
1041
 
1041
1042
                if (q->canSetTag)
1042
1043
                        parse = q;
1044
1045
 
1045
1046
        /*
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.
1049
1050
         *
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
1054
1055
         * entities.
1055
1056
         */
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.")));
1080
1081
                return false;
1081
1082
        }
1082
1083
 
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.")));
1116
1117
 
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.
1153
1154
                 */
1154
1155
                if (tlistlen == 1)
1155
1156
                {
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);
1217
1218
                        tuplogcols++;