Before commit
6b65a7fe62e129d5c2b85cd74d6a91d8f7564608, tqueue.c could
perform tuple remapping and thus leak memory, which is why commit
af33039317ddc4a0e38a02e2255c2bf453115fd2 made TupleQueueReaderNext
run in a short-lived context. Now, however, tqueue.c has been reduced
to a shadow of its former self, and there shouldn't be any chance of
leaks any more. Accordingly, remove some tuple copying and memory
context manipulation to speed up processing.
Patch by me, reviewed by Amit Kapila. Some testing by Rafia Sabih.
Discussion: http://postgr.es/m/CAA4eK1LSDydwrNjmYSNkfJ3ZivGSWH9SVswh6QpNzsMdj_oOQA@mail.gmail.com
ExecGather(PlanState *pstate)
{
GatherState *node = castNode(GatherState, pstate);
ExecGather(PlanState *pstate)
{
GatherState *node = castNode(GatherState, pstate);
- TupleTableSlot *fslot = node->funnel_slot;
TupleTableSlot *slot;
ExprContext *econtext;
TupleTableSlot *slot;
ExprContext *econtext;
/*
* Reset per-tuple memory context to free any expression evaluation
/*
* Reset per-tuple memory context to free any expression evaluation
- * storage allocated in the previous tuple cycle. This will also clear
- * any previous tuple returned by a TupleQueueReader; to make sure we
- * don't leave a dangling pointer around, clear the working slot first.
+ * storage allocated in the previous tuple cycle.
econtext = node->ps.ps_ExprContext;
ResetExprContext(econtext);
econtext = node->ps.ps_ExprContext;
ResetExprContext(econtext);
PlanState *outerPlan = outerPlanState(gatherstate);
TupleTableSlot *outerTupleSlot;
TupleTableSlot *fslot = gatherstate->funnel_slot;
PlanState *outerPlan = outerPlanState(gatherstate);
TupleTableSlot *outerTupleSlot;
TupleTableSlot *fslot = gatherstate->funnel_slot;
- MemoryContext tupleContext = gatherstate->ps.ps_ExprContext->ecxt_per_tuple_memory;
HeapTuple tup;
while (gatherstate->nreaders > 0 || gatherstate->need_to_scan_locally)
HeapTuple tup;
while (gatherstate->nreaders > 0 || gatherstate->need_to_scan_locally)
if (gatherstate->nreaders > 0)
{
if (gatherstate->nreaders > 0)
{
- MemoryContext oldContext;
-
- /* Run TupleQueueReaders in per-tuple context */
- oldContext = MemoryContextSwitchTo(tupleContext);
tup = gather_readnext(gatherstate);
tup = gather_readnext(gatherstate);
- MemoryContextSwitchTo(oldContext);
if (HeapTupleIsValid(tup))
{
if (HeapTupleIsValid(tup))
{
fslot, /* slot in which to store the tuple */
InvalidBuffer, /* buffer associated with this
* tuple */
fslot, /* slot in which to store the tuple */
InvalidBuffer, /* buffer associated with this
* tuple */
- false); /* slot should not pfree tuple */
+ true); /* pfree tuple when done with it */
&tuple_buffer->done);
if (!HeapTupleIsValid(tuple))
break;
&tuple_buffer->done);
if (!HeapTupleIsValid(tuple))
break;
- tuple_buffer->tuple[i] = heap_copytuple(tuple);
+ tuple_buffer->tuple[i] = tuple;
tuple_buffer->nTuples++;
}
}
tuple_buffer->nTuples++;
}
}
&tuple_buffer->done);
if (!HeapTupleIsValid(tup))
return false;
&tuple_buffer->done);
if (!HeapTupleIsValid(tup))
return false;
- tup = heap_copytuple(tup);
/*
* Attempt to read more tuples in nowait mode and store them in the
/*
* Attempt to read more tuples in nowait mode and store them in the
{
TupleQueueReader *reader;
HeapTuple tup;
{
TupleQueueReader *reader;
HeapTuple tup;
- MemoryContext oldContext;
- MemoryContext tupleContext;
/* Check for async events, particularly messages from workers. */
CHECK_FOR_INTERRUPTS();
/* Attempt to read a tuple. */
reader = gm_state->reader[nreader - 1];
/* Check for async events, particularly messages from workers. */
CHECK_FOR_INTERRUPTS();
/* Attempt to read a tuple. */
reader = gm_state->reader[nreader - 1];
-
- /* Run TupleQueueReaders in per-tuple context */
- tupleContext = gm_state->ps.ps_ExprContext->ecxt_per_tuple_memory;
- oldContext = MemoryContextSwitchTo(tupleContext);
tup = TupleQueueReaderNext(reader, nowait, done);
tup = TupleQueueReaderNext(reader, nowait, done);
- MemoryContextSwitchTo(oldContext);
* is set to true when there are no remaining tuples and otherwise to false.
*
* The returned tuple, if any, is allocated in CurrentMemoryContext.
* is set to true when there are no remaining tuples and otherwise to false.
*
* The returned tuple, if any, is allocated in CurrentMemoryContext.
+ * Note that this routine must not leak memory! (We used to allow that,
+ * but not any more.)
*
* Even when shm_mq_receive() returns SHM_MQ_WOULD_BLOCK, this can still
* accumulate bytes from a partially-read message, so it's useful to call
*
* Even when shm_mq_receive() returns SHM_MQ_WOULD_BLOCK, this can still
* accumulate bytes from a partially-read message, so it's useful to call