* Darko Prenosil <Darko.Prenosil@finteh.hr>
* Shridhar Daithankar <shridhar_daithankar@persistent.co.in>
*
- * $PostgreSQL: pgsql/contrib/dblink/dblink.c,v 1.84 2009/09/12 23:20:52 joe Exp $
+ * $PostgreSQL: pgsql/contrib/dblink/dblink.c,v 1.85 2009/12/29 17:40:59 heikki Exp $
* Copyright (c) 2001-2009, PostgreSQL Global Development Group
* ALL RIGHTS RESERVED;
*
else
nulls[2] = true;
- /* switch to appropriate context while storing the tuple */
- MemoryContextSwitchTo(per_query_ctx);
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
- MemoryContextSwitchTo(oldcontext);
PQfreemem(notify);
PQconsumeInput(conn);
* Copyright (c) 2008-2009, PostgreSQL Global Development Group
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/contrib/pg_stat_statements/pg_stat_statements.c,v 1.9 2009/12/15 20:04:49 tgl Exp $
+ * $PostgreSQL: pgsql/contrib/pg_stat_statements/pg_stat_statements.c,v 1.10 2009/12/29 17:40:59 heikki Exp $
*
*-------------------------------------------------------------------------
*/
rsinfo->setResult = tupstore;
rsinfo->setDesc = tupdesc;
+ MemoryContextSwitchTo(oldcontext);
+
LWLockAcquire(pgss->lock, LW_SHARED);
hash_seq_init(&hash_seq, pgss_hash);
int i = 0;
Counters tmp;
- /* generate junk in short-term context */
- MemoryContextSwitchTo(oldcontext);
-
memset(values, 0, sizeof(values));
memset(nulls, 0, sizeof(nulls));
Assert(i == PG_STAT_STATEMENTS_COLS);
- /* switch to appropriate context while storing the tuple */
- MemoryContextSwitchTo(per_query_ctx);
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
}
/* clean up and return the tuplestore */
tuplestore_donestoring(tupstore);
- MemoryContextSwitchTo(oldcontext);
-
return (Datum) 0;
}
/*
- * $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
{
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);
}
HeapTuple tuple;
int ret;
int proc;
- MemoryContext SPIcontext;
/* initialize our tuplestore (while still in query context!) */
tupstore = tuplestore_begin_heap(randomAccess, false, work_mem);
/* 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]);
/* 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)
Tuplestorestate *tupstore)
{
TupleDesc tupdesc = attinmeta->tupdesc;
- MemoryContext oldcontext;
int ret;
int proc;
int serial_column;
/* 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++;
}
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 */
/*
- * $PostgreSQL: pgsql/contrib/xml2/xpath.c,v 1.23 2009/06/11 14:48:53 momjian Exp $
+ * $PostgreSQL: pgsql/contrib/xml2/xpath.c,v 1.24 2009/12/29 17:40:59 heikki Exp $
*
* Parser interface for DOM-based parser (libxml) rather than
* stream-based SAX-type parser
{
/* not well-formed, so output all-NULL tuple */
ret_tuple = BuildTupleFromCStrings(attinmeta, values);
- oldcontext = MemoryContextSwitchTo(per_query_ctx);
tuplestore_puttuple(tupstore, ret_tuple);
- MemoryContextSwitchTo(oldcontext);
heap_freetuple(ret_tuple);
}
else
if (had_values)
{
ret_tuple = BuildTupleFromCStrings(attinmeta, values);
- oldcontext = MemoryContextSwitchTo(per_query_ctx);
tuplestore_puttuple(tupstore, ret_tuple);
- MemoryContextSwitchTo(oldcontext);
heap_freetuple(ret_tuple);
}
* Copyright (c) 2002-2009, PostgreSQL Global Development Group
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/commands/prepare.c,v 1.100 2009/11/04 22:26:05 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/commands/prepare.c,v 1.101 2009/12/29 17:40:59 heikki Exp $
*
*-------------------------------------------------------------------------
*/
tuplestore_begin_heap(rsinfo->allowedModes & SFRM_Materialize_Random,
false, work_mem);
+ /* generate junk in short-term context */
+ MemoryContextSwitchTo(oldcontext);
+
/* hash table might be uninitialized */
if (prepared_queries)
{
Datum values[5];
bool nulls[5];
- /* generate junk in short-term context */
- MemoryContextSwitchTo(oldcontext);
-
MemSet(nulls, 0, sizeof(nulls));
values[0] = CStringGetTextDatum(prep_stmt->stmt_name);
prep_stmt->plansource->num_params);
values[4] = BoolGetDatum(prep_stmt->from_sql);
- /* switch to appropriate context while storing the tuple */
- MemoryContextSwitchTo(per_query_ctx);
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
}
}
/* clean up and return the tuplestore */
tuplestore_donestoring(tupstore);
- MemoryContextSwitchTo(oldcontext);
-
rsinfo->returnMode = SFRM_Materialize;
rsinfo->setResult = tupstore;
rsinfo->setDesc = tupdesc;
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/executor/execQual.c,v 1.256 2009/12/14 02:15:49 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/executor/execQual.c,v 1.257 2009/12/29 17:40:59 heikki Exp $
*
*-------------------------------------------------------------------------
*/
tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
tmptup.t_data = td;
- oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
tuplestore_puttuple(tupstore, &tmptup);
}
else
- {
- oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
tuplestore_putvalues(tupstore, tupdesc, &result, &fcinfo.isnull);
- }
- MemoryContextSwitchTo(oldcontext);
/*
* Are we done?
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/executor/functions.c,v 1.138 2009/12/15 04:57:47 rhaas Exp $
+ * $PostgreSQL: pgsql/src/backend/executor/functions.c,v 1.139 2009/12/29 17:40:59 heikki Exp $
*
*-------------------------------------------------------------------------
*/
sqlfunction_receive(TupleTableSlot *slot, DestReceiver *self)
{
DR_sqlfunction *myState = (DR_sqlfunction *) self;
- MemoryContext oldcxt;
/* Filter tuple as needed */
slot = ExecFilterJunk(myState->filter, slot);
/* Store the filtered tuple into the tuplestore */
- oldcxt = MemoryContextSwitchTo(myState->cxt);
tuplestore_puttupleslot(myState->tstore, slot);
- MemoryContextSwitchTo(oldcxt);
}
/*
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/executor/nodeWindowAgg.c,v 1.7 2009/09/27 21:10:53 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/executor/nodeWindowAgg.c,v 1.8 2009/12/29 17:40:59 heikki Exp $
*
*-------------------------------------------------------------------------
*/
outerPlan = outerPlanState(winstate);
- /* Must be in query context to call outerplan or touch tuplestore */
+ /* Must be in query context to call outerplan */
oldcontext = MemoryContextSwitchTo(winstate->ss.ps.ps_ExprContext->ecxt_per_query_memory);
while (winstate->spooled_rows <= pos || pos == -1)
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/executor/tstoreReceiver.c,v 1.23 2009/06/11 14:48:57 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/executor/tstoreReceiver.c,v 1.24 2009/12/29 17:40:59 heikki Exp $
*
*-------------------------------------------------------------------------
*/
tstoreReceiveSlot_notoast(TupleTableSlot *slot, DestReceiver *self)
{
TStoreState *myState = (TStoreState *) self;
- MemoryContext oldcxt = MemoryContextSwitchTo(myState->cxt);
tuplestore_puttupleslot(myState->tstore, slot);
-
- MemoryContextSwitchTo(oldcxt);
}
/*
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/mmgr/portalmem.c,v 1.113 2009/01/01 17:23:53 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/mmgr/portalmem.c,v 1.114 2009/12/29 17:40:59 heikki Exp $
*
*-------------------------------------------------------------------------
*/
tuplestore_begin_heap(rsinfo->allowedModes & SFRM_Materialize_Random,
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)
{
if (!portal->visible)
continue;
- /* generate junk in short-term context */
- MemoryContextSwitchTo(oldcontext);
-
MemSet(nulls, 0, sizeof(nulls));
values[0] = CStringGetTextDatum(portal->name);
values[4] = BoolGetDatum(portal->cursorOptions & CURSOR_OPT_SCROLL);
values[5] = TimestampTzGetDatum(portal->creation_time);
- /* switch to appropriate context while storing the tuple */
- MemoryContextSwitchTo(per_query_ctx);
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
}
/* clean up and return the tuplestore */
tuplestore_donestoring(tupstore);
- MemoryContextSwitchTo(oldcontext);
-
rsinfo->returnMode = SFRM_Materialize;
rsinfo->setResult = tupstore;
rsinfo->setDesc = tupdesc;
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/sort/tuplestore.c,v 1.48 2009/06/11 14:49:06 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/sort/tuplestore.c,v 1.49 2009/12/29 17:40:59 heikki Exp $
*
*-------------------------------------------------------------------------
*/
#include "executor/executor.h"
#include "storage/buffile.h"
#include "utils/memutils.h"
+#include "utils/resowner.h"
#include "utils/tuplestore.h"
bool truncated; /* tuplestore_trim has removed tuples? */
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
state->truncated = false;
state->availMem = maxKBytes * 1024L;
state->myfile = NULL;
+ state->context = CurrentMemoryContext;
+ state->resowner = CurrentResourceOwner;
state->memtupcount = 0;
state->memtupsize = 1024; /* initial guess */
*
* 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.
TupleTableSlot *slot)
{
MinimalTuple tuple;
+ MemoryContext oldcxt = MemoryContextSwitchTo(state->context);
/*
* Form a MinimalTuple in working memory
USEMEM(state, GetMemoryChunkSpace(tuple));
tuplestore_puttuple_common(state, (void *) tuple);
+
+ MemoryContextSwitchTo(oldcxt);
}
/*
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);
}
/*
Datum *values, bool *isnull)
{
MinimalTuple tuple;
+ MemoryContext oldcxt = MemoryContextSwitchTo(state->context);
tuple = heap_form_minimal_tuple(tdesc, values, isnull);
tuplestore_puttuple_common(state, (void *) tuple);
+
+ MemoryContextSwitchTo(oldcxt);
}
static void
{
TSReadPointer *readptr;
int i;
+ ResourceOwner oldowner;
switch (state->status)
{
* 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;
+
/*
* Freeze the decision about whether trailing length words will be
* used. We can't change this choice once data is on tape, even
/**********************************************************************
* plperl.c - perl as a procedural language for PostgreSQL
*
- * $PostgreSQL: pgsql/src/pl/plperl/plperl.c,v 1.155 2009/11/29 21:02:16 tgl Exp $
+ * $PostgreSQL: pgsql/src/pl/plperl/plperl.c,v 1.156 2009/12/29 17:40:59 heikki Exp $
*
**********************************************************************/
tuple = plperl_build_tuple_result((HV *) SvRV(sv),
current_call_data->attinmeta);
-
- /* Make sure to store the tuple in a long-lived memory context */
- MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory);
tuplestore_puttuple(current_call_data->tuple_store, tuple);
- MemoryContextSwitchTo(old_cxt);
}
else
{
isNull = true;
}
- /* Make sure to store the tuple in a long-lived memory context */
- MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory);
tuplestore_putvalues(current_call_data->tuple_store,
current_call_data->ret_tdesc,
&ret, &isNull);
- MemoryContextSwitchTo(old_cxt);
}
+ MemoryContextSwitchTo(old_cxt);
MemoryContextReset(current_call_data->tmp_cxt);
}
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.251 2009/11/09 00:26:55 tgl Exp $
+ * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.252 2009/12/29 17:40:59 heikki Exp $
*
*-------------------------------------------------------------------------
*/
{
TupleDesc tupdesc;
int natts;
- MemoryContext oldcxt;
HeapTuple tuple = NULL;
bool free_tuple = false;
tupdesc->attrs[0]->atttypmod,
isNull);
- oldcxt = MemoryContextSwitchTo(estate->tuple_store_cxt);
tuplestore_putvalues(estate->tuple_store, tupdesc,
&retval, &isNull);
- MemoryContextSwitchTo(oldcxt);
}
break;
tupdesc->attrs[0]->atttypmod,
isNull);
- oldcxt = MemoryContextSwitchTo(estate->tuple_store_cxt);
tuplestore_putvalues(estate->tuple_store, tupdesc,
&retval, &isNull);
- MemoryContextSwitchTo(oldcxt);
exec_eval_cleanup(estate);
}
if (HeapTupleIsValid(tuple))
{
- oldcxt = MemoryContextSwitchTo(estate->tuple_store_cxt);
tuplestore_puttuple(estate->tuple_store, tuple);
- MemoryContextSwitchTo(oldcxt);
if (free_tuple)
heap_freetuple(tuple);
while (true)
{
- MemoryContext old_cxt;
int i;
SPI_cursor_fetch(portal, true, 50);
if (SPI_processed == 0)
break;
- old_cxt = MemoryContextSwitchTo(estate->tuple_store_cxt);
for (i = 0; i < SPI_processed; i++)
{
HeapTuple tuple = SPI_tuptable->vals[i];
heap_freetuple(tuple);
processed++;
}
- MemoryContextSwitchTo(old_cxt);
SPI_freetuptable(SPI_tuptable);
}
{
ReturnSetInfo *rsi = estate->rsi;
MemoryContext oldcxt;
+ ResourceOwner oldowner;
/*
* Check caller can handle a set result in the way we want
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("set-valued function called in context that cannot accept a set")));
- estate->tuple_store_cxt = rsi->econtext->ecxt_per_query_memory;
-
+ /*
+ * Switch to the right memory context and resource owner for storing
+ * the tuplestore for return set. If we're within a subtransaction opened
+ * for an exception-block, for example, we must still create the
+ * tuplestore in the resource owner that was active when this function was
+ * entered, and not in the subtransaction resource owner.
+ */
oldcxt = MemoryContextSwitchTo(estate->tuple_store_cxt);
+ oldowner = CurrentResourceOwner;
+ CurrentResourceOwner = estate->tuple_store_owner;
+
estate->tuple_store =
tuplestore_begin_heap(rsi->allowedModes & SFRM_Materialize_Random,
false, work_mem);
+
+ CurrentResourceOwner = oldowner;
MemoryContextSwitchTo(oldcxt);
estate->rettupdesc = rsi->expectedDesc;
estate->exitlabel = NULL;
estate->tuple_store = NULL;
- estate->tuple_store_cxt = NULL;
+ if (rsi)
+ {
+ estate->tuple_store_cxt = rsi->econtext->ecxt_per_query_memory;
+ estate->tuple_store_owner = CurrentResourceOwner;
+ }
+ else
+ {
+ estate->tuple_store_cxt = NULL;
+ estate->tuple_store_owner = NULL;
+ }
estate->rsi = rsi;
estate->found_varno = func->found_varno;
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/pl/plpgsql/src/plpgsql.h,v 1.125 2009/11/13 22:43:42 tgl Exp $
+ * $PostgreSQL: pgsql/src/pl/plpgsql/src/plpgsql.h,v 1.126 2009/12/29 17:40:59 heikki Exp $
*
*-------------------------------------------------------------------------
*/
Tuplestorestate *tuple_store; /* SRFs accumulate results here */
MemoryContext tuple_store_cxt;
+ ResourceOwner tuple_store_owner;
ReturnSetInfo *rsi;
int found_varno;