bool merge = (btspool2 != NULL);
IndexTuple itup,
itup2 = NULL;
- bool should_free,
- should_free2,
- load1;
+ bool load1;
TupleDesc tupdes = RelationGetDescr(wstate->index);
int i,
keysz = RelationGetNumberOfAttributes(wstate->index);
*/
/* the preparation of merge */
- itup = tuplesort_getindextuple(btspool->sortstate,
- true, &should_free);
- itup2 = tuplesort_getindextuple(btspool2->sortstate,
- true, &should_free2);
+ itup = tuplesort_getindextuple(btspool->sortstate, true);
+ itup2 = tuplesort_getindextuple(btspool2->sortstate, true);
indexScanKey = _bt_mkscankey_nodata(wstate->index);
/* Prepare SortSupport data for each column */
if (load1)
{
_bt_buildadd(wstate, state, itup);
- if (should_free)
- pfree(itup);
- itup = tuplesort_getindextuple(btspool->sortstate,
- true, &should_free);
+ itup = tuplesort_getindextuple(btspool->sortstate, true);
}
else
{
_bt_buildadd(wstate, state, itup2);
- if (should_free2)
- pfree(itup2);
- itup2 = tuplesort_getindextuple(btspool2->sortstate,
- true, &should_free2);
+ itup2 = tuplesort_getindextuple(btspool2->sortstate, true);
}
}
pfree(sortKeys);
{
/* merge is unnecessary */
while ((itup = tuplesort_getindextuple(btspool->sortstate,
- true, &should_free)) != NULL)
+ true)) != NULL)
{
/* When we see first tuple, create first index page */
if (state == NULL)
state = _bt_pagestate(wstate, 0);
_bt_buildadd(wstate, state, itup);
- if (should_free)
- pfree(itup);
}
}
/*
* Internal routine to fetch the next tuple in either forward or back
* direction into *stup. Returns FALSE if no more tuples.
- * If *should_free is set, the caller must pfree stup.tuple when done with it.
- * Otherwise, caller should not use tuple following next call here.
+ * Returned tuple belongs to tuplesort memory context, and must not be freed
+ * by caller. Caller should not use tuple following next call here.
*/
static bool
tuplesort_gettuple_common(Tuplesortstate *state, bool forward,
- SortTuple *stup, bool *should_free)
+ SortTuple *stup)
{
unsigned int tuplen;
case TSS_SORTEDINMEM:
Assert(forward || state->randomAccess);
Assert(!state->slabAllocatorUsed);
- *should_free = false;
if (forward)
{
if (state->current < state->memtupcount)
*/
state->lastReturnedTuple = stup->tuple;
- *should_free = false;
return true;
}
else
*/
state->lastReturnedTuple = stup->tuple;
- *should_free = false;
return true;
case TSS_FINALMERGE:
Assert(forward);
/* We are managing memory ourselves, with the slab allocator. */
Assert(state->slabAllocatorUsed);
- *should_free = false;
/*
* The slab slot holding the tuple that we returned in previous
{
MemoryContext oldcontext = MemoryContextSwitchTo(state->sortcontext);
SortTuple stup;
- bool should_free;
- if (!tuplesort_gettuple_common(state, forward, &stup, &should_free))
+ if (!tuplesort_gettuple_common(state, forward, &stup))
stup.tuple = NULL;
MemoryContextSwitchTo(oldcontext);
if (state->sortKeys->abbrev_converter && abbrev)
*abbrev = stup.datum1;
- if (!should_free)
- {
- stup.tuple = heap_copy_minimal_tuple((MinimalTuple) stup.tuple);
- should_free = true;
- }
- ExecStoreMinimalTuple((MinimalTuple) stup.tuple, slot, should_free);
+ stup.tuple = heap_copy_minimal_tuple((MinimalTuple) stup.tuple);
+ ExecStoreMinimalTuple((MinimalTuple) stup.tuple, slot, true);
return true;
}
else
/*
* Fetch the next tuple in either forward or back direction.
- * Returns NULL if no more tuples. If *should_free is set, the
- * caller must pfree the returned tuple when done with it.
- * If it is not set, caller should not use tuple following next
- * call here.
+ * Returns NULL if no more tuples. Returned tuple belongs to tuplesort memory
+ * context, and must not be freed by caller. Caller should not use tuple
+ * following next call here.
*/
HeapTuple
-tuplesort_getheaptuple(Tuplesortstate *state, bool forward, bool *should_free)
+tuplesort_getheaptuple(Tuplesortstate *state, bool forward)
{
MemoryContext oldcontext = MemoryContextSwitchTo(state->sortcontext);
SortTuple stup;
- if (!tuplesort_gettuple_common(state, forward, &stup, should_free))
+ if (!tuplesort_gettuple_common(state, forward, &stup))
stup.tuple = NULL;
MemoryContextSwitchTo(oldcontext);
/*
* Fetch the next index tuple in either forward or back direction.
- * Returns NULL if no more tuples. If *should_free is set, the
- * caller must pfree the returned tuple when done with it.
- * If it is not set, caller should not use tuple following next
- * call here.
+ * Returns NULL if no more tuples. Returned tuple belongs to tuplesort memory
+ * context, and must not be freed by caller. Caller should not use tuple
+ * following next call here.
*/
IndexTuple
-tuplesort_getindextuple(Tuplesortstate *state, bool forward,
- bool *should_free)
+tuplesort_getindextuple(Tuplesortstate *state, bool forward)
{
MemoryContext oldcontext = MemoryContextSwitchTo(state->sortcontext);
SortTuple stup;
- if (!tuplesort_gettuple_common(state, forward, &stup, should_free))
+ if (!tuplesort_gettuple_common(state, forward, &stup))
stup.tuple = NULL;
MemoryContextSwitchTo(oldcontext);
* Returns FALSE if no more datums.
*
* If the Datum is pass-by-ref type, the returned value is freshly palloc'd
- * and is now owned by the caller.
+ * and is now owned by the caller (this differs from similar routines for
+ * other types of tuplesorts).
*
* Caller may optionally be passed back abbreviated value (on TRUE return
* value) when abbreviation was used, which can be used to cheaply avoid
{
MemoryContext oldcontext = MemoryContextSwitchTo(state->sortcontext);
SortTuple stup;
- bool should_free;
- if (!tuplesort_gettuple_common(state, forward, &stup, &should_free))
+ if (!tuplesort_gettuple_common(state, forward, &stup))
{
MemoryContextSwitchTo(oldcontext);
return false;
else
{
/* use stup.tuple because stup.datum1 may be an abbreviation */
-
- if (should_free)
- *val = PointerGetDatum(stup.tuple);
- else
- *val = datumCopy(PointerGetDatum(stup.tuple), false, state->datumTypeLen);
+ *val = datumCopy(PointerGetDatum(stup.tuple), false, state->datumTypeLen);
*isNull = false;
}
while (ntuples-- > 0)
{
SortTuple stup;
- bool should_free;
- if (!tuplesort_gettuple_common(state, forward,
- &stup, &should_free))
+ if (!tuplesort_gettuple_common(state, forward, &stup))
{
MemoryContextSwitchTo(oldcontext);
return false;
}
- if (should_free && stup.tuple)
- pfree(stup.tuple);
CHECK_FOR_INTERRUPTS();
}
MemoryContextSwitchTo(oldcontext);