summaryrefslogtreecommitdiff
path: root/contrib/tablefunc/tablefunc.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/tablefunc/tablefunc.c')
-rw-r--r--contrib/tablefunc/tablefunc.c22
1 files changed, 8 insertions, 14 deletions
diff --git a/contrib/tablefunc/tablefunc.c b/contrib/tablefunc/tablefunc.c
index f5f751e72f..7ab363972e 100644
--- a/contrib/tablefunc/tablefunc.c
+++ b/contrib/tablefunc/tablefunc.c
@@ -934,19 +934,16 @@ get_crosstab_tuplestore(char *sql,
*/
if (lastrowid != NULL)
{
- /*
- * switch to appropriate context while storing the tuple
- */
- SPIcontext = MemoryContextSwitchTo(per_query_ctx);
-
/* 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]);
-
- /* now reset the context */
- MemoryContextSwitchTo(SPIcontext);
}
values[0] = rowid;
@@ -970,16 +967,13 @@ get_crosstab_tuplestore(char *sql,
lastrowid = pstrdup(rowid);
}
- /* switch to appropriate context while storing the tuple */
- SPIcontext = MemoryContextSwitchTo(per_query_ctx);
-
/* flush the last output row */
tuple = BuildTupleFromCStrings(attinmeta, values);
- tuplestore_puttuple(tupstore, tuple);
- /* now reset the context */
+ /* 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)