Commit 3856cf96 authored by Robert Haas's avatar Robert Haas

Remove should_free arguments to tuplesort routines.

Since commit e94568ec, the answer is
always "false", and we do not need to complicate the API by arranging
to return a constant value.

Peter Geoghegan

Discussion: http://postgr.es/m/CAM3SWZQWZZ_N=DmmL7tKy_OUjGH_5mN=N=A6h7kHyyDvEhg2DA@mail.gmail.com
parent 9b3d02c2
...@@ -104,15 +104,13 @@ void ...@@ -104,15 +104,13 @@ void
_h_indexbuild(HSpool *hspool) _h_indexbuild(HSpool *hspool)
{ {
IndexTuple itup; IndexTuple itup;
bool should_free;
#ifdef USE_ASSERT_CHECKING #ifdef USE_ASSERT_CHECKING
uint32 hashkey = 0; uint32 hashkey = 0;
#endif #endif
tuplesort_performsort(hspool->sortstate); tuplesort_performsort(hspool->sortstate);
while ((itup = tuplesort_getindextuple(hspool->sortstate, while ((itup = tuplesort_getindextuple(hspool->sortstate, true)) != NULL)
true, &should_free)) != NULL)
{ {
/* /*
* Technically, it isn't critical that hash keys be found in sorted * Technically, it isn't critical that hash keys be found in sorted
...@@ -129,7 +127,5 @@ _h_indexbuild(HSpool *hspool) ...@@ -129,7 +127,5 @@ _h_indexbuild(HSpool *hspool)
#endif #endif
_hash_doinsert(hspool->index, itup); _hash_doinsert(hspool->index, itup);
if (should_free)
pfree(itup);
} }
} }
...@@ -680,9 +680,7 @@ _bt_load(BTWriteState *wstate, BTSpool *btspool, BTSpool *btspool2) ...@@ -680,9 +680,7 @@ _bt_load(BTWriteState *wstate, BTSpool *btspool, BTSpool *btspool2)
bool merge = (btspool2 != NULL); bool merge = (btspool2 != NULL);
IndexTuple itup, IndexTuple itup,
itup2 = NULL; itup2 = NULL;
bool should_free, bool load1;
should_free2,
load1;
TupleDesc tupdes = RelationGetDescr(wstate->index); TupleDesc tupdes = RelationGetDescr(wstate->index);
int i, int i,
keysz = RelationGetNumberOfAttributes(wstate->index); keysz = RelationGetNumberOfAttributes(wstate->index);
...@@ -697,10 +695,8 @@ _bt_load(BTWriteState *wstate, BTSpool *btspool, BTSpool *btspool2) ...@@ -697,10 +695,8 @@ _bt_load(BTWriteState *wstate, BTSpool *btspool, BTSpool *btspool2)
*/ */
/* the preparation of merge */ /* the preparation of merge */
itup = tuplesort_getindextuple(btspool->sortstate, itup = tuplesort_getindextuple(btspool->sortstate, true);
true, &should_free); itup2 = tuplesort_getindextuple(btspool2->sortstate, true);
itup2 = tuplesort_getindextuple(btspool2->sortstate,
true, &should_free2);
indexScanKey = _bt_mkscankey_nodata(wstate->index); indexScanKey = _bt_mkscankey_nodata(wstate->index);
/* Prepare SortSupport data for each column */ /* Prepare SortSupport data for each column */
...@@ -775,18 +771,12 @@ _bt_load(BTWriteState *wstate, BTSpool *btspool, BTSpool *btspool2) ...@@ -775,18 +771,12 @@ _bt_load(BTWriteState *wstate, BTSpool *btspool, BTSpool *btspool2)
if (load1) if (load1)
{ {
_bt_buildadd(wstate, state, itup); _bt_buildadd(wstate, state, itup);
if (should_free) itup = tuplesort_getindextuple(btspool->sortstate, true);
pfree(itup);
itup = tuplesort_getindextuple(btspool->sortstate,
true, &should_free);
} }
else else
{ {
_bt_buildadd(wstate, state, itup2); _bt_buildadd(wstate, state, itup2);
if (should_free2) itup2 = tuplesort_getindextuple(btspool2->sortstate, true);
pfree(itup2);
itup2 = tuplesort_getindextuple(btspool2->sortstate,
true, &should_free2);
} }
} }
pfree(sortKeys); pfree(sortKeys);
...@@ -795,15 +785,13 @@ _bt_load(BTWriteState *wstate, BTSpool *btspool, BTSpool *btspool2) ...@@ -795,15 +785,13 @@ _bt_load(BTWriteState *wstate, BTSpool *btspool, BTSpool *btspool2)
{ {
/* merge is unnecessary */ /* merge is unnecessary */
while ((itup = tuplesort_getindextuple(btspool->sortstate, while ((itup = tuplesort_getindextuple(btspool->sortstate,
true, &should_free)) != NULL) true)) != NULL)
{ {
/* When we see first tuple, create first index page */ /* When we see first tuple, create first index page */
if (state == NULL) if (state == NULL)
state = _bt_pagestate(wstate, 0); state = _bt_pagestate(wstate, 0);
_bt_buildadd(wstate, state, itup); _bt_buildadd(wstate, state, itup);
if (should_free)
pfree(itup);
} }
} }
......
...@@ -1057,11 +1057,10 @@ copy_heap_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose, ...@@ -1057,11 +1057,10 @@ copy_heap_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose,
for (;;) for (;;)
{ {
HeapTuple tuple; HeapTuple tuple;
bool shouldfree;
CHECK_FOR_INTERRUPTS(); CHECK_FOR_INTERRUPTS();
tuple = tuplesort_getheaptuple(tuplesort, true, &shouldfree); tuple = tuplesort_getheaptuple(tuplesort, true);
if (tuple == NULL) if (tuple == NULL)
break; break;
...@@ -1069,9 +1068,6 @@ copy_heap_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose, ...@@ -1069,9 +1068,6 @@ copy_heap_data(Oid OIDNewHeap, Oid OIDOldHeap, Oid OIDOldIndex, bool verbose,
oldTupDesc, newTupDesc, oldTupDesc, newTupDesc,
values, isnull, values, isnull,
NewHeap->rd_rel->relhasoids, rwstate); NewHeap->rd_rel->relhasoids, rwstate);
if (shouldfree)
heap_freetuple(tuple);
} }
tuplesort_end(tuplesort); tuplesort_end(tuplesort);
......
...@@ -1841,12 +1841,12 @@ tuplesort_performsort(Tuplesortstate *state) ...@@ -1841,12 +1841,12 @@ tuplesort_performsort(Tuplesortstate *state)
/* /*
* Internal routine to fetch the next tuple in either forward or back * Internal routine to fetch the next tuple in either forward or back
* direction into *stup. Returns FALSE if no more tuples. * direction into *stup. Returns FALSE if no more tuples.
* If *should_free is set, the caller must pfree stup.tuple when done with it. * Returned tuple belongs to tuplesort memory context, and must not be freed
* Otherwise, caller should not use tuple following next call here. * by caller. Caller should not use tuple following next call here.
*/ */
static bool static bool
tuplesort_gettuple_common(Tuplesortstate *state, bool forward, tuplesort_gettuple_common(Tuplesortstate *state, bool forward,
SortTuple *stup, bool *should_free) SortTuple *stup)
{ {
unsigned int tuplen; unsigned int tuplen;
...@@ -1855,7 +1855,6 @@ tuplesort_gettuple_common(Tuplesortstate *state, bool forward, ...@@ -1855,7 +1855,6 @@ tuplesort_gettuple_common(Tuplesortstate *state, bool forward,
case TSS_SORTEDINMEM: case TSS_SORTEDINMEM:
Assert(forward || state->randomAccess); Assert(forward || state->randomAccess);
Assert(!state->slabAllocatorUsed); Assert(!state->slabAllocatorUsed);
*should_free = false;
if (forward) if (forward)
{ {
if (state->current < state->memtupcount) if (state->current < state->memtupcount)
...@@ -1927,7 +1926,6 @@ tuplesort_gettuple_common(Tuplesortstate *state, bool forward, ...@@ -1927,7 +1926,6 @@ tuplesort_gettuple_common(Tuplesortstate *state, bool forward,
*/ */
state->lastReturnedTuple = stup->tuple; state->lastReturnedTuple = stup->tuple;
*should_free = false;
return true; return true;
} }
else else
...@@ -2008,14 +2006,12 @@ tuplesort_gettuple_common(Tuplesortstate *state, bool forward, ...@@ -2008,14 +2006,12 @@ tuplesort_gettuple_common(Tuplesortstate *state, bool forward,
*/ */
state->lastReturnedTuple = stup->tuple; state->lastReturnedTuple = stup->tuple;
*should_free = false;
return true; return true;
case TSS_FINALMERGE: case TSS_FINALMERGE:
Assert(forward); Assert(forward);
/* We are managing memory ourselves, with the slab allocator. */ /* We are managing memory ourselves, with the slab allocator. */
Assert(state->slabAllocatorUsed); Assert(state->slabAllocatorUsed);
*should_free = false;
/* /*
* The slab slot holding the tuple that we returned in previous * The slab slot holding the tuple that we returned in previous
...@@ -2097,9 +2093,8 @@ tuplesort_gettupleslot(Tuplesortstate *state, bool forward, ...@@ -2097,9 +2093,8 @@ tuplesort_gettupleslot(Tuplesortstate *state, bool forward,
{ {
MemoryContext oldcontext = MemoryContextSwitchTo(state->sortcontext); MemoryContext oldcontext = MemoryContextSwitchTo(state->sortcontext);
SortTuple stup; SortTuple stup;
bool should_free;
if (!tuplesort_gettuple_common(state, forward, &stup, &should_free)) if (!tuplesort_gettuple_common(state, forward, &stup))
stup.tuple = NULL; stup.tuple = NULL;
MemoryContextSwitchTo(oldcontext); MemoryContextSwitchTo(oldcontext);
...@@ -2110,12 +2105,8 @@ tuplesort_gettupleslot(Tuplesortstate *state, bool forward, ...@@ -2110,12 +2105,8 @@ tuplesort_gettupleslot(Tuplesortstate *state, bool forward,
if (state->sortKeys->abbrev_converter && abbrev) if (state->sortKeys->abbrev_converter && abbrev)
*abbrev = stup.datum1; *abbrev = stup.datum1;
if (!should_free) stup.tuple = heap_copy_minimal_tuple((MinimalTuple) stup.tuple);
{ ExecStoreMinimalTuple((MinimalTuple) stup.tuple, slot, true);
stup.tuple = heap_copy_minimal_tuple((MinimalTuple) stup.tuple);
should_free = true;
}
ExecStoreMinimalTuple((MinimalTuple) stup.tuple, slot, should_free);
return true; return true;
} }
else else
...@@ -2127,18 +2118,17 @@ tuplesort_gettupleslot(Tuplesortstate *state, bool forward, ...@@ -2127,18 +2118,17 @@ tuplesort_gettupleslot(Tuplesortstate *state, bool forward,
/* /*
* Fetch the next tuple in either forward or back direction. * Fetch the next tuple in either forward or back direction.
* Returns NULL if no more tuples. If *should_free is set, the * Returns NULL if no more tuples. Returned tuple belongs to tuplesort memory
* caller must pfree the returned tuple when done with it. * context, and must not be freed by caller. Caller should not use tuple
* If it is not set, caller should not use tuple following next * following next call here.
* call here.
*/ */
HeapTuple HeapTuple
tuplesort_getheaptuple(Tuplesortstate *state, bool forward, bool *should_free) tuplesort_getheaptuple(Tuplesortstate *state, bool forward)
{ {
MemoryContext oldcontext = MemoryContextSwitchTo(state->sortcontext); MemoryContext oldcontext = MemoryContextSwitchTo(state->sortcontext);
SortTuple stup; SortTuple stup;
if (!tuplesort_gettuple_common(state, forward, &stup, should_free)) if (!tuplesort_gettuple_common(state, forward, &stup))
stup.tuple = NULL; stup.tuple = NULL;
MemoryContextSwitchTo(oldcontext); MemoryContextSwitchTo(oldcontext);
...@@ -2148,19 +2138,17 @@ tuplesort_getheaptuple(Tuplesortstate *state, bool forward, bool *should_free) ...@@ -2148,19 +2138,17 @@ tuplesort_getheaptuple(Tuplesortstate *state, bool forward, bool *should_free)
/* /*
* Fetch the next index tuple in either forward or back direction. * Fetch the next index tuple in either forward or back direction.
* Returns NULL if no more tuples. If *should_free is set, the * Returns NULL if no more tuples. Returned tuple belongs to tuplesort memory
* caller must pfree the returned tuple when done with it. * context, and must not be freed by caller. Caller should not use tuple
* If it is not set, caller should not use tuple following next * following next call here.
* call here.
*/ */
IndexTuple IndexTuple
tuplesort_getindextuple(Tuplesortstate *state, bool forward, tuplesort_getindextuple(Tuplesortstate *state, bool forward)
bool *should_free)
{ {
MemoryContext oldcontext = MemoryContextSwitchTo(state->sortcontext); MemoryContext oldcontext = MemoryContextSwitchTo(state->sortcontext);
SortTuple stup; SortTuple stup;
if (!tuplesort_gettuple_common(state, forward, &stup, should_free)) if (!tuplesort_gettuple_common(state, forward, &stup))
stup.tuple = NULL; stup.tuple = NULL;
MemoryContextSwitchTo(oldcontext); MemoryContextSwitchTo(oldcontext);
...@@ -2173,7 +2161,8 @@ tuplesort_getindextuple(Tuplesortstate *state, bool forward, ...@@ -2173,7 +2161,8 @@ tuplesort_getindextuple(Tuplesortstate *state, bool forward,
* Returns FALSE if no more datums. * Returns FALSE if no more datums.
* *
* If the Datum is pass-by-ref type, the returned value is freshly palloc'd * 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 * Caller may optionally be passed back abbreviated value (on TRUE return
* value) when abbreviation was used, which can be used to cheaply avoid * value) when abbreviation was used, which can be used to cheaply avoid
...@@ -2188,9 +2177,8 @@ tuplesort_getdatum(Tuplesortstate *state, bool forward, ...@@ -2188,9 +2177,8 @@ tuplesort_getdatum(Tuplesortstate *state, bool forward,
{ {
MemoryContext oldcontext = MemoryContextSwitchTo(state->sortcontext); MemoryContext oldcontext = MemoryContextSwitchTo(state->sortcontext);
SortTuple stup; SortTuple stup;
bool should_free;
if (!tuplesort_gettuple_common(state, forward, &stup, &should_free)) if (!tuplesort_gettuple_common(state, forward, &stup))
{ {
MemoryContextSwitchTo(oldcontext); MemoryContextSwitchTo(oldcontext);
return false; return false;
...@@ -2208,11 +2196,7 @@ tuplesort_getdatum(Tuplesortstate *state, bool forward, ...@@ -2208,11 +2196,7 @@ tuplesort_getdatum(Tuplesortstate *state, bool forward,
else else
{ {
/* use stup.tuple because stup.datum1 may be an abbreviation */ /* use stup.tuple because stup.datum1 may be an abbreviation */
*val = datumCopy(PointerGetDatum(stup.tuple), false, state->datumTypeLen);
if (should_free)
*val = PointerGetDatum(stup.tuple);
else
*val = datumCopy(PointerGetDatum(stup.tuple), false, state->datumTypeLen);
*isNull = false; *isNull = false;
} }
...@@ -2270,16 +2254,12 @@ tuplesort_skiptuples(Tuplesortstate *state, int64 ntuples, bool forward) ...@@ -2270,16 +2254,12 @@ tuplesort_skiptuples(Tuplesortstate *state, int64 ntuples, bool forward)
while (ntuples-- > 0) while (ntuples-- > 0)
{ {
SortTuple stup; SortTuple stup;
bool should_free;
if (!tuplesort_gettuple_common(state, forward, if (!tuplesort_gettuple_common(state, forward, &stup))
&stup, &should_free))
{ {
MemoryContextSwitchTo(oldcontext); MemoryContextSwitchTo(oldcontext);
return false; return false;
} }
if (should_free && stup.tuple)
pfree(stup.tuple);
CHECK_FOR_INTERRUPTS(); CHECK_FOR_INTERRUPTS();
} }
MemoryContextSwitchTo(oldcontext); MemoryContextSwitchTo(oldcontext);
......
...@@ -94,10 +94,8 @@ extern void tuplesort_performsort(Tuplesortstate *state); ...@@ -94,10 +94,8 @@ extern void tuplesort_performsort(Tuplesortstate *state);
extern bool tuplesort_gettupleslot(Tuplesortstate *state, bool forward, extern bool tuplesort_gettupleslot(Tuplesortstate *state, bool forward,
TupleTableSlot *slot, Datum *abbrev); TupleTableSlot *slot, Datum *abbrev);
extern HeapTuple tuplesort_getheaptuple(Tuplesortstate *state, bool forward, extern HeapTuple tuplesort_getheaptuple(Tuplesortstate *state, bool forward);
bool *should_free); extern IndexTuple tuplesort_getindextuple(Tuplesortstate *state, bool forward);
extern IndexTuple tuplesort_getindextuple(Tuplesortstate *state, bool forward,
bool *should_free);
extern bool tuplesort_getdatum(Tuplesortstate *state, bool forward, extern bool tuplesort_getdatum(Tuplesortstate *state, bool forward,
Datum *val, bool *isNull, Datum *abbrev); Datum *val, bool *isNull, Datum *abbrev);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment