aboutsummaryrefslogtreecommitdiff
path: root/contrib/tablefunc/tablefunc.c
diff options
context:
space:
mode:
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>2009-12-29 17:40:59 +0000
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>2009-12-29 17:40:59 +0000
commit84d723b6cefcf25b8c800f8aa6cf3c9538a546b4 (patch)
treefa91ab7b2bc60f3d602a330bc68b78ae8beab69b /contrib/tablefunc/tablefunc.c
parent50ef9f7b0612c755097bbddd7f2985237030c31a (diff)
downloadpostgresql-84d723b6cefcf25b8c800f8aa6cf3c9538a546b4.tar.gz
postgresql-84d723b6cefcf25b8c800f8aa6cf3c9538a546b4.zip
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.
Diffstat (limited to 'contrib/tablefunc/tablefunc.c')
-rw-r--r--contrib/tablefunc/tablefunc.c29
1 files changed, 2 insertions, 27 deletions
diff --git a/contrib/tablefunc/tablefunc.c b/contrib/tablefunc/tablefunc.c
index 48512bd9349..272b1b43777 100644
--- a/contrib/tablefunc/tablefunc.c
+++ b/contrib/tablefunc/tablefunc.c
@@ -1,5 +1,5 @@
/*
- * $PostgreSQL: pgsql/contrib/tablefunc/tablefunc.c,v 1.60 2009/06/11 14:48:52 momjian Exp $
+ * $PostgreSQL: pgsql/contrib/tablefunc/tablefunc.c,v 1.61 2009/12/29 17:40:59 heikki Exp $
*
*
* tablefunc
@@ -567,14 +567,9 @@ crosstab(PG_FUNCTION_ARGS)
{
HeapTuple tuple;
- /* build the tuple */
+ /* build the tuple and store it */
tuple = BuildTupleFromCStrings(attinmeta, values);
-
- /* switch to appropriate context while storing the tuple */
- oldcontext = MemoryContextSwitchTo(per_query_ctx);
tuplestore_puttuple(tupstore, tuple);
- MemoryContextSwitchTo(oldcontext);
-
heap_freetuple(tuple);
}
@@ -807,7 +802,6 @@ get_crosstab_tuplestore(char *sql,
HeapTuple tuple;
int ret;
int proc;
- MemoryContext SPIcontext;
/* initialize our tuplestore (while still in query context!) */
tupstore = tuplestore_begin_heap(randomAccess, false, work_mem);
@@ -907,10 +901,7 @@ get_crosstab_tuplestore(char *sql,
/* rowid changed, flush the previous output row */
tuple = BuildTupleFromCStrings(attinmeta, values);
- /* switch to appropriate context while storing the tuple */
- SPIcontext = MemoryContextSwitchTo(per_query_ctx);
tuplestore_puttuple(tupstore, tuple);
- MemoryContextSwitchTo(SPIcontext);
for (j = 0; j < result_ncols; j++)
xpfree(values[j]);
@@ -943,10 +934,7 @@ get_crosstab_tuplestore(char *sql,
/* flush the last output row */
tuple = BuildTupleFromCStrings(attinmeta, values);
- /* switch to appropriate context while storing the tuple */
- SPIcontext = MemoryContextSwitchTo(per_query_ctx);
tuplestore_puttuple(tupstore, tuple);
- MemoryContextSwitchTo(SPIcontext);
}
if (SPI_finish() != SPI_OK_FINISH)
@@ -1232,7 +1220,6 @@ build_tuplestore_recursively(char *key_fld,
Tuplestorestate *tupstore)
{
TupleDesc tupdesc = attinmeta->tupdesc;
- MemoryContext oldcontext;
int ret;
int proc;
int serial_column;
@@ -1310,15 +1297,9 @@ build_tuplestore_recursively(char *key_fld,
/* construct the tuple */
tuple = BuildTupleFromCStrings(attinmeta, values);
- /* switch to long lived context while storing the tuple */
- oldcontext = MemoryContextSwitchTo(per_query_ctx);
-
/* now store it */
tuplestore_puttuple(tupstore, tuple);
- /* now reset the context */
- MemoryContextSwitchTo(oldcontext);
-
/* increment level */
level++;
}
@@ -1404,15 +1385,9 @@ build_tuplestore_recursively(char *key_fld,
xpfree(current_key);
xpfree(current_key_parent);
- /* switch to long lived context while storing the tuple */
- oldcontext = MemoryContextSwitchTo(per_query_ctx);
-
/* store the tuple for later use */
tuplestore_puttuple(tupstore, tuple);
- /* now reset the context */
- MemoryContextSwitchTo(oldcontext);
-
heap_freetuple(tuple);
/* recurse using current_key_parent as the new start_with */