1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-18 02:02:55 +03:00

Previous fix for temporary file management broke returning a set from

PL/pgSQL function within an exception handler. Make sure we use the right
resource owner when we create the tuplestore to hold returned tuples.

Simplify tuplestore API so that the caller doesn't need to be in the right
memory context when calling tuplestore_put* functions. tuplestore.c
automatically switches to the memory context used when the tuplestore was
created. Tuplesort was already modified like this earlier. This patch also
removes the now useless MemoryContextSwitch calls from callers.

Report by Aleksei on pgsql-bugs on Dec 22 2009. Backpatch to 8.1, like
the previous patch that broke this.
This commit is contained in:
Heikki Linnakangas
2009-12-29 17:41:26 +00:00
parent 6b922abf60
commit 31c88772c8
10 changed files with 64 additions and 66 deletions

View File

@@ -1,7 +1,7 @@
/**********************************************************************
* plperl.c - perl as a procedural language for PostgreSQL
*
* $PostgreSQL: pgsql/src/pl/plperl/plperl.c,v 1.123.2.9 2009/11/29 21:02:34 tgl Exp $
* $PostgreSQL: pgsql/src/pl/plperl/plperl.c,v 1.123.2.10 2009/12/29 17:41:25 heikki Exp $
*
**********************************************************************/
@@ -1984,11 +1984,9 @@ plperl_return_next(SV *sv)
tuple = heap_form_tuple(current_call_data->ret_tdesc, &ret, &isNull);
}
/* Make sure to store the tuple in a long-lived memory context */
MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory);
tuplestore_puttuple(current_call_data->tuple_store, tuple);
MemoryContextSwitchTo(old_cxt);
MemoryContextSwitchTo(old_cxt);
MemoryContextReset(current_call_data->tmp_cxt);
}

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.180.2.10 2009/04/02 01:16:25 tgl Exp $
* $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.180.2.11 2009/12/29 17:41:25 heikki Exp $
*
*-------------------------------------------------------------------------
*/
@@ -2058,11 +2058,7 @@ exec_stmt_return_next(PLpgSQL_execstate *estate,
if (HeapTupleIsValid(tuple))
{
MemoryContext oldcxt;
oldcxt = MemoryContextSwitchTo(estate->tuple_store_cxt);
tuplestore_puttuple(estate->tuple_store, tuple);
MemoryContextSwitchTo(oldcxt);
if (free_tuple)
heap_freetuple(tuple);
@@ -2076,6 +2072,7 @@ exec_init_tuple_store(PLpgSQL_execstate *estate)
{
ReturnSetInfo *rsi = estate->rsi;
MemoryContext oldcxt;
ResourceOwner oldowner;
/*
* Check caller can handle a set result in the way we want
@@ -2087,10 +2084,20 @@ exec_init_tuple_store(PLpgSQL_execstate *estate)
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("set-valued function called in context that cannot accept a set")));
estate->tuple_store_cxt = rsi->econtext->ecxt_per_query_memory;
/*
* Switch to the right memory context and resource owner for storing
* the tuplestore for return set. If we're within a subtransaction opened
* for an exception-block, for example, we must still create the
* tuplestore in the resource owner that was active when this function was
* entered, and not in the subtransaction resource owner.
*/
oldcxt = MemoryContextSwitchTo(estate->tuple_store_cxt);
oldowner = CurrentResourceOwner;
CurrentResourceOwner = estate->tuple_store_owner;
estate->tuple_store = tuplestore_begin_heap(true, false, work_mem);
CurrentResourceOwner = oldowner;
MemoryContextSwitchTo(oldcxt);
estate->rettupdesc = rsi->expectedDesc;
@@ -2202,7 +2209,16 @@ plpgsql_estate_setup(PLpgSQL_execstate *estate,
estate->exitlabel = NULL;
estate->tuple_store = NULL;
estate->tuple_store_cxt = NULL;
if (rsi)
{
estate->tuple_store_cxt = rsi->econtext->ecxt_per_query_memory;
estate->tuple_store_owner = CurrentResourceOwner;
}
else
{
estate->tuple_store_cxt = NULL;
estate->tuple_store_owner = NULL;
}
estate->rsi = rsi;
estate->trig_nargs = 0;

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/pl/plpgsql/src/plpgsql.h,v 1.81.2.3 2008/10/09 16:35:19 tgl Exp $
* $PostgreSQL: pgsql/src/pl/plpgsql/src/plpgsql.h,v 1.81.2.4 2009/12/29 17:41:26 heikki Exp $
*
*-------------------------------------------------------------------------
*/
@@ -607,6 +607,7 @@ typedef struct
Tuplestorestate *tuple_store; /* SRFs accumulate results here */
MemoryContext tuple_store_cxt;
ResourceOwner tuple_store_owner;
ReturnSetInfo *rsi;
int trig_nargs;