summaryrefslogtreecommitdiff
path: root/src/backend
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend')
-rw-r--r--src/backend/commands/prepare.c13
-rw-r--r--src/backend/executor/execQual.c5
-rw-r--r--src/backend/executor/tstoreReceiver.c5
-rw-r--r--src/backend/utils/mmgr/portalmem.c12
-rw-r--r--src/backend/utils/sort/tuplestore.c30
5 files changed, 37 insertions, 28 deletions
diff --git a/src/backend/commands/prepare.c b/src/backend/commands/prepare.c
index 7dcd3cb5a85..f0914096789 100644
--- a/src/backend/commands/prepare.c
+++ b/src/backend/commands/prepare.c
@@ -10,7 +10,7 @@
* Copyright (c) 2002-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/commands/prepare.c,v 1.80.2.1 2008/04/02 18:32:00 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/commands/prepare.c,v 1.80.2.2 2009/12/29 17:41:18 heikki Exp $
*
*-------------------------------------------------------------------------
*/
@@ -759,6 +759,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)
{
@@ -772,9 +775,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,
@@ -792,9 +792,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);
}
}
@@ -802,8 +799,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;
diff --git a/src/backend/executor/execQual.c b/src/backend/executor/execQual.c
index 27e764c19a9..f26a4437da3 100644
--- a/src/backend/executor/execQual.c
+++ b/src/backend/executor/execQual.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/executor/execQual.c,v 1.226.2.1 2008/11/15 20:53:40 petere Exp $
+ * $PostgreSQL: pgsql/src/backend/executor/execQual.c,v 1.226.2.2 2009/12/29 17:41:18 heikki Exp $
*
*-------------------------------------------------------------------------
*/
@@ -1656,9 +1656,7 @@ ExecMakeTableFunctionResult(ExprState *funcexpr,
tuple = heap_form_tuple(tupdesc, &result, &fcinfo.isnull);
}
- oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
tuplestore_puttuple(tupstore, tuple);
- MemoryContextSwitchTo(oldcontext);
/*
* Are we done?
@@ -1710,6 +1708,7 @@ no_function_result:
memset(nullflags, true, natts * sizeof(bool));
tuple = heap_form_tuple(expectedDesc, nulldatums, nullflags);
MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
+
tuplestore_puttuple(tupstore, tuple);
}
}
diff --git a/src/backend/executor/tstoreReceiver.c b/src/backend/executor/tstoreReceiver.c
index 197aa3c0774..9645365ac1f 100644
--- a/src/backend/executor/tstoreReceiver.c
+++ b/src/backend/executor/tstoreReceiver.c
@@ -13,7 +13,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/executor/tstoreReceiver.c,v 1.19.2.1 2008/12/01 17:06:27 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/executor/tstoreReceiver.c,v 1.19.2.2 2009/12/29 17:41:18 heikki Exp $
*
*-------------------------------------------------------------------------
*/
@@ -95,11 +95,8 @@ static void
tstoreReceiveSlot_notoast(TupleTableSlot *slot, DestReceiver *self)
{
TStoreState *myState = (TStoreState *) self;
- MemoryContext oldcxt = MemoryContextSwitchTo(myState->cxt);
tuplestore_puttupleslot(myState->tstore, slot);
-
- MemoryContextSwitchTo(oldcxt);
}
/*
diff --git a/src/backend/utils/mmgr/portalmem.c b/src/backend/utils/mmgr/portalmem.c
index b977396fb42..217c6b1234b 100644
--- a/src/backend/utils/mmgr/portalmem.c
+++ b/src/backend/utils/mmgr/portalmem.c
@@ -12,7 +12,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/mmgr/portalmem.c,v 1.106.2.1 2008/04/02 18:32:00 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/mmgr/portalmem.c,v 1.106.2.2 2009/12/29 17:41:18 heikki Exp $
*
*-------------------------------------------------------------------------
*/
@@ -911,6 +911,9 @@ pg_cursor(PG_FUNCTION_ARGS)
*/
tupstore = tuplestore_begin_heap(true, false, work_mem);
+ /* generate junk in short-term context */
+ MemoryContextSwitchTo(oldcontext);
+
hash_seq_init(&hash_seq, PortalHashTable);
while ((hentry = hash_seq_search(&hash_seq)) != NULL)
{
@@ -923,9 +926,6 @@ pg_cursor(PG_FUNCTION_ARGS)
if (!portal->visible)
continue;
- /* generate junk in short-term context */
- MemoryContextSwitchTo(oldcontext);
-
MemSet(nulls, 0, sizeof(nulls));
values[0] = DirectFunctionCall1(textin, CStringGetDatum(portal->name));
@@ -941,16 +941,12 @@ pg_cursor(PG_FUNCTION_ARGS)
tuple = heap_form_tuple(tupdesc, values, nulls);
- /* switch to appropriate context while storing the tuple */
- MemoryContextSwitchTo(per_query_ctx);
tuplestore_puttuple(tupstore, tuple);
}
/* clean up and return the tuplestore */
tuplestore_donestoring(tupstore);
- MemoryContextSwitchTo(oldcontext);
-
rsinfo->returnMode = SFRM_Materialize;
rsinfo->setResult = tupstore;
rsinfo->setDesc = tupdesc;
diff --git a/src/backend/utils/sort/tuplestore.c b/src/backend/utils/sort/tuplestore.c
index e297579674a..81106e513a4 100644
--- a/src/backend/utils/sort/tuplestore.c
+++ b/src/backend/utils/sort/tuplestore.c
@@ -38,7 +38,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/sort/tuplestore.c,v 1.36 2008/01/01 19:45:55 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/sort/tuplestore.c,v 1.36.2.1 2009/12/29 17:41:18 heikki Exp $
*
*-------------------------------------------------------------------------
*/
@@ -50,6 +50,7 @@
#include "executor/executor.h"
#include "storage/buffile.h"
#include "utils/memutils.h"
+#include "utils/resowner.h"
#include "utils/tuplestore.h"
@@ -74,6 +75,8 @@ struct Tuplestorestate
bool interXact; /* keep open through transactions? */
long availMem; /* remaining memory available, in bytes */
BufFile *myfile; /* underlying file, or NULL if none */
+ MemoryContext context; /* memory context for holding tuples */
+ ResourceOwner resowner; /* resowner for holding temp files */
/*
* These function pointers decouple the routines that must know what kind
@@ -225,6 +228,8 @@ tuplestore_begin_common(int eflags, bool interXact, int maxKBytes)
state->interXact = interXact;
state->availMem = maxKBytes * 1024L;
state->myfile = NULL;
+ state->context = CurrentMemoryContext;
+ state->resowner = CurrentResourceOwner;
state->memtupcount = 0;
state->memtupsize = 1024; /* initial guess */
@@ -250,9 +255,9 @@ tuplestore_begin_common(int eflags, bool interXact, int maxKBytes)
*
* interXact: if true, the files used for on-disk storage persist beyond the
* end of the current transaction. NOTE: It's the caller's responsibility to
- * create such a tuplestore in a memory context that will also survive
- * transaction boundaries, and to ensure the tuplestore is closed when it's
- * no longer wanted.
+ * create such a tuplestore in a memory context and resource owner that will
+ * also survive transaction boundaries, and to ensure the tuplestore is closed
+ * when it's no longer wanted.
*
* maxKBytes: how much data to store in memory (any data beyond this
* amount is paged to disk). When in doubt, use work_mem.
@@ -353,6 +358,7 @@ tuplestore_puttupleslot(Tuplestorestate *state,
TupleTableSlot *slot)
{
MinimalTuple tuple;
+ MemoryContext oldcxt = MemoryContextSwitchTo(state->context);
/*
* Form a MinimalTuple in working memory
@@ -361,6 +367,8 @@ tuplestore_puttupleslot(Tuplestorestate *state,
USEMEM(state, GetMemoryChunkSpace(tuple));
tuplestore_puttuple_common(state, (void *) tuple);
+
+ MemoryContextSwitchTo(oldcxt);
}
/*
@@ -372,17 +380,23 @@ tuplestore_puttupleslot(Tuplestorestate *state,
void
tuplestore_puttuple(Tuplestorestate *state, HeapTuple tuple)
{
+ MemoryContext oldcxt = MemoryContextSwitchTo(state->context);
+
/*
* Copy the tuple. (Must do this even in WRITEFILE case.)
*/
tuple = COPYTUP(state, tuple);
tuplestore_puttuple_common(state, (void *) tuple);
+
+ MemoryContextSwitchTo(oldcxt);
}
static void
tuplestore_puttuple_common(Tuplestorestate *state, void *tuple)
{
+ ResourceOwner oldowner;
+
switch (state->status)
{
case TSS_INMEM:
@@ -429,7 +443,15 @@ tuplestore_puttuple_common(Tuplestorestate *state, void *tuple)
* the temp file(s) are created in suitable temp tablespaces.
*/
PrepareTempTablespaces();
+
+ /* associate the file with the store's resource owner */
+ oldowner = CurrentResourceOwner;
+ CurrentResourceOwner = state->resowner;
+
state->myfile = BufFileCreateTemp(state->interXact);
+
+ CurrentResourceOwner = oldowner;
+
state->status = TSS_WRITEFILE;
dumptuples(state);
break;