diff options
Diffstat (limited to 'src/backend/utils')
-rw-r--r-- | src/backend/utils/mmgr/portalmem.c | 12 | ||||
-rw-r--r-- | src/backend/utils/sort/tuplestore.c | 30 |
2 files changed, 29 insertions, 13 deletions
diff --git a/src/backend/utils/mmgr/portalmem.c b/src/backend/utils/mmgr/portalmem.c index 3e3505caf05..57ff508a91e 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.97.2.1 2007/04/26 23:24:57 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/mmgr/portalmem.c,v 1.97.2.2 2009/12/29 17:41:25 heikki Exp $ * *------------------------------------------------------------------------- */ @@ -854,6 +854,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) { @@ -866,9 +869,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)); @@ -884,16 +884,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 ccc6580c1c7..bc2178d474f 100644 --- a/src/backend/utils/sort/tuplestore.c +++ b/src/backend/utils/sort/tuplestore.c @@ -36,7 +36,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/sort/tuplestore.c,v 1.29.2.1 2007/08/02 17:48:54 neilc Exp $ + * $PostgreSQL: pgsql/src/backend/utils/sort/tuplestore.c,v 1.29.2.2 2009/12/29 17:41:25 heikki Exp $ * *------------------------------------------------------------------------- */ @@ -46,6 +46,7 @@ #include "access/heapam.h" #include "storage/buffile.h" #include "utils/memutils.h" +#include "utils/resowner.h" #include "utils/tuplestore.h" @@ -70,6 +71,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 @@ -220,6 +223,8 @@ tuplestore_begin_common(bool randomAccess, 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 */ @@ -245,9 +250,9 @@ tuplestore_begin_common(bool randomAccess, 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. @@ -315,6 +320,7 @@ tuplestore_puttupleslot(Tuplestorestate *state, TupleTableSlot *slot) { MinimalTuple tuple; + MemoryContext oldcxt = MemoryContextSwitchTo(state->context); /* * Form a MinimalTuple in working memory @@ -323,6 +329,8 @@ tuplestore_puttupleslot(Tuplestorestate *state, USEMEM(state, GetMemoryChunkSpace(tuple)); tuplestore_puttuple_common(state, (void *) tuple); + + MemoryContextSwitchTo(oldcxt); } /* @@ -334,17 +342,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: @@ -389,7 +403,13 @@ tuplestore_puttuple_common(Tuplestorestate *state, void *tuple) /* * Nope; time to switch to tape-based operation. */ - state->myfile = BufFileCreateTemp(state->interXact); + + /* 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; |