#include "storage/smgr.h"
#include "tcop/tcopprot.h"
#include "utils/rel.h"
+#include "utils/sb_alloc.h"
#include "utils/tuplesort.h"
{
_bt_buildadd(wstate, state, itup);
if (should_free)
- pfree(itup);
+ sb_free(itup);
itup = tuplesort_getindextuple(btspool->sortstate,
true, &should_free);
}
{
_bt_buildadd(wstate, state, itup2);
if (should_free2)
- pfree(itup2);
+ sb_free(itup2);
itup2 = tuplesort_getindextuple(btspool2->sortstate,
true, &should_free2);
}
_bt_buildadd(wstate, state, itup);
if (should_free)
- pfree(itup);
+ sb_free(itup);
}
}
#include "utils/memutils.h"
#include "utils/pg_rusage.h"
#include "utils/rel.h"
+#include "utils/sb_alloc.h"
#include "utils/sortsupport.h"
#include "utils/tuplesort.h"
int maxTapes; /* number of tapes (Knuth's T) */
int tapeRange; /* maxTapes-1 (Knuth's P) */
MemoryContext sortcontext; /* memory context holding all sort data */
+ sb_allocator *sortallocator; /* superblock allocator holding sort data */
LogicalTapeSet *tapeset; /* logtape.c object for tapes in a temp file */
/*
{
Tuplesortstate *state = tuplesort_begin_common(workMem, randomAccess);
MemoryContext oldcontext;
+ state->sortallocator = sb_create_private_allocator();
oldcontext = MemoryContextSwitchTo(state->sortcontext);
#ifdef TRACE_SORT
if (trace_sort)
- elog(LOG, "switching to external sort with %d tapes: %s",
- maxTapes, pg_rusage_show(&state->ru_start));
+ {
+ int64 spaceUsed = (state->allowedMem - state->availMem + 1023) / 1024;
+ elog(LOG, "switching to external sort with %d tapes (%ld KB used): %s",
+ maxTapes, spaceUsed, pg_rusage_show(&state->ru_start));
+ }
#endif
/*
IndexTuple newtuple;
/* copy the tuple into sort storage */
- newtuple = (IndexTuple) palloc(tuplen);
+ newtuple = (IndexTuple) sb_alloc(state->sortallocator, tuplen, 0);
memcpy(newtuple, tuple, tuplen);
- USEMEM(state, GetMemoryChunkSpace(newtuple));
+ USEMEM(state, sb_chunk_space(newtuple));
stup->tuple = (void *) newtuple;
/* set up first-column key value */
stup->datum1 = index_getattr(newtuple,
LogicalTapeWrite(state->tapeset, tapenum,
(void *) &tuplen, sizeof(tuplen));
- FREEMEM(state, GetMemoryChunkSpace(tuple));
- pfree(tuple);
+ FREEMEM(state, sb_chunk_space(tuple));
+ sb_free(tuple);
}
static void
int tapenum, unsigned int len)
{
unsigned int tuplen = len - sizeof(unsigned int);
- IndexTuple tuple = (IndexTuple) palloc(tuplen);
+ IndexTuple tuple = (IndexTuple) sb_alloc(state->sortallocator, tuplen, 0);
- USEMEM(state, GetMemoryChunkSpace(tuple));
+ USEMEM(state, sb_chunk_space(tuple));
LogicalTapeReadExact(state->tapeset, tapenum,
tuple, tuplen);
if (state->randomAccess) /* need trailing length word? */