From 31c88772c80cf3863ac538d96187640d41f18884 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Tue, 29 Dec 2009 17:41:26 +0000 Subject: 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. --- src/backend/commands/prepare.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) (limited to 'src/backend/commands/prepare.c') diff --git a/src/backend/commands/prepare.c b/src/backend/commands/prepare.c index d5ec457732a..03035cf7b47 100644 --- a/src/backend/commands/prepare.c +++ b/src/backend/commands/prepare.c @@ -10,7 +10,7 @@ * Copyright (c) 2002-2006, PostgreSQL Global Development Group * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/commands/prepare.c,v 1.66.2.1 2007/04/26 23:24:56 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/commands/prepare.c,v 1.66.2.2 2009/12/29 17:41:25 heikki Exp $ * *------------------------------------------------------------------------- */ @@ -717,6 +717,9 @@ pg_prepared_statement(PG_FUNCTION_ARGS) */ tupstore = tuplestore_begin_heap(true, false, work_mem); + /* generate junk in short-term context */ + MemoryContextSwitchTo(oldcontext); + /* hash table might be uninitialized */ if (prepared_queries) { @@ -730,9 +733,6 @@ pg_prepared_statement(PG_FUNCTION_ARGS) Datum values[5]; bool nulls[5]; - /* generate junk in short-term context */ - MemoryContextSwitchTo(oldcontext); - MemSet(nulls, 0, sizeof(nulls)); values[0] = DirectFunctionCall1(textin, @@ -749,9 +749,6 @@ pg_prepared_statement(PG_FUNCTION_ARGS) values[4] = BoolGetDatum(prep_stmt->from_sql); tuple = heap_form_tuple(tupdesc, values, nulls); - - /* switch to appropriate context while storing the tuple */ - MemoryContextSwitchTo(per_query_ctx); tuplestore_puttuple(tupstore, tuple); } } @@ -759,8 +756,6 @@ pg_prepared_statement(PG_FUNCTION_ARGS) /* clean up and return the tuplestore */ tuplestore_donestoring(tupstore); - MemoryContextSwitchTo(oldcontext); - rsinfo->returnMode = SFRM_Materialize; rsinfo->setResult = tupstore; rsinfo->setDesc = tupdesc; -- cgit v1.2.3