Crude plumbing to allow 'REINDEX INDEX pgbench_accounts' to use sb_alloc.
authorRobert Haas <rhaas@postgresql.org>
Mon, 5 May 2014 13:48:24 +0000 (09:48 -0400)
committerRobert Haas <rhaas@postgresql.org>
Mon, 5 May 2014 13:48:24 +0000 (09:48 -0400)
src/backend/access/nbtree/nbtsort.c
src/backend/utils/sort/tuplesort.c

index 048b215118642837c76e8cc2d06b1b6cb6ebc52e..1fe1a9bed58c268233f6558670fd580ea48dacdc 100644 (file)
@@ -72,6 +72,7 @@
 #include "storage/smgr.h"
 #include "tcop/tcopprot.h"
 #include "utils/rel.h"
+#include "utils/sb_alloc.h"
 #include "utils/tuplesort.h"
 
 
@@ -769,7 +770,7 @@ _bt_load(BTWriteState *wstate, BTSpool *btspool, BTSpool *btspool2)
                        {
                                _bt_buildadd(wstate, state, itup);
                                if (should_free)
-                                       pfree(itup);
+                                       sb_free(itup);
                                itup = tuplesort_getindextuple(btspool->sortstate,
                                                                                           true, &should_free);
                        }
@@ -777,7 +778,7 @@ _bt_load(BTWriteState *wstate, BTSpool *btspool, BTSpool *btspool2)
                        {
                                _bt_buildadd(wstate, state, itup2);
                                if (should_free2)
-                                       pfree(itup2);
+                                       sb_free(itup2);
                                itup2 = tuplesort_getindextuple(btspool2->sortstate,
                                                                                                true, &should_free2);
                        }
@@ -796,7 +797,7 @@ _bt_load(BTWriteState *wstate, BTSpool *btspool, BTSpool *btspool2)
 
                        _bt_buildadd(wstate, state, itup);
                        if (should_free)
-                               pfree(itup);
+                               sb_free(itup);
                }
        }
 
index 426a64af8673e70a28196fa5773cef4f3c6daa1f..39d6552737856da1cf65e08601a90a702a85547a 100644 (file)
 #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"
 
@@ -216,6 +217,7 @@ struct Tuplesortstate
        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 */
 
        /*
@@ -728,6 +730,7 @@ tuplesort_begin_index_btree(Relation heapRel,
 {
        Tuplesortstate *state = tuplesort_begin_common(workMem, randomAccess);
        MemoryContext oldcontext;
+       state->sortallocator = sb_create_private_allocator();
 
        oldcontext = MemoryContextSwitchTo(state->sortcontext);
 
@@ -1848,8 +1851,11 @@ inittapes(Tuplesortstate *state)
 
 #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
 
        /*
@@ -3344,9 +3350,9 @@ copytup_index(Tuplesortstate *state, SortTuple *stup, void *tup)
        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,
@@ -3370,8 +3376,8 @@ writetup_index(Tuplesortstate *state, int tapenum, SortTuple *stup)
                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
@@ -3379,9 +3385,9 @@ readtup_index(Tuplesortstate *state, SortTuple *stup,
                          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? */