Skip to content

Commit 0758964

Browse files
committed
logtape.c: do not preallocate for tapes when sorting
The preallocation logic is only useful for HashAgg, so disable it when sorting. Also, adjust an out-of-date comment. Reviewed-by: Peter Geoghegan Discussion: https://postgr.es/m/CAH2-Wzn_o7tE2+hRVvwSFghRb75AJ5g-nqGzDUqLYMexjOAe=g@mail.gmail.com Backpatch-through: 13
1 parent 7634bd4 commit 0758964

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

src/backend/executor/nodeAgg.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2882,7 +2882,7 @@ hashagg_tapeinfo_init(AggState *aggstate)
28822882
HashTapeInfo *tapeinfo = palloc(sizeof(HashTapeInfo));
28832883
int init_tapes = 16; /* expanded dynamically */
28842884

2885-
tapeinfo->tapeset = LogicalTapeSetCreate(init_tapes, NULL, NULL, -1);
2885+
tapeinfo->tapeset = LogicalTapeSetCreate(init_tapes, true, NULL, NULL, -1);
28862886
tapeinfo->ntapes = init_tapes;
28872887
tapeinfo->nfreetapes = init_tapes;
28882888
tapeinfo->freetapes_alloc = init_tapes;

src/backend/utils/sort/logtape.c

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ struct LogicalTapeSet
212212
long *freeBlocks; /* resizable array holding minheap */
213213
long nFreeBlocks; /* # of currently free blocks */
214214
Size freeBlocksLen; /* current allocated length of freeBlocks[] */
215+
bool enable_prealloc; /* preallocate write blocks? */
215216

216217
/* The array of logical tapes. */
217218
int nTapes; /* # of logical tapes in set */
@@ -220,6 +221,7 @@ struct LogicalTapeSet
220221

221222
static void ltsWriteBlock(LogicalTapeSet *lts, long blocknum, void *buffer);
222223
static void ltsReadBlock(LogicalTapeSet *lts, long blocknum, void *buffer);
224+
static long ltsGetBlock(LogicalTapeSet *lts, LogicalTape *lt);
223225
static long ltsGetFreeBlock(LogicalTapeSet *lts);
224226
static long ltsGetPreallocBlock(LogicalTapeSet *lts, LogicalTape *lt);
225227
static void ltsReleaseBlock(LogicalTapeSet *lts, long blocknum);
@@ -242,12 +244,8 @@ ltsWriteBlock(LogicalTapeSet *lts, long blocknum, void *buffer)
242244
* that's past the current end of file, fill the space between the current
243245
* end of file and the target block with zeros.
244246
*
245-
* This should happen rarely, otherwise you are not writing very
246-
* sequentially. In current use, this only happens when the sort ends
247-
* writing a run, and switches to another tape. The last block of the
248-
* previous tape isn't flushed to disk until the end of the sort, so you
249-
* get one-block hole, where the last block of the previous tape will
250-
* later go.
247+
* This can happen either when tapes preallocate blocks; or for the last
248+
* block of a tape which might not have been flushed.
251249
*
252250
* Note that BufFile concatenation can leave "holes" in BufFile between
253251
* worker-owned block ranges. These are tracked for reporting purposes
@@ -373,8 +371,20 @@ parent_offset(unsigned long i)
373371
}
374372

375373
/*
376-
* Select the lowest currently unused block by taking the first element from
377-
* the freelist min heap.
374+
* Get the next block for writing.
375+
*/
376+
static long
377+
ltsGetBlock(LogicalTapeSet *lts, LogicalTape *lt)
378+
{
379+
if (lts->enable_prealloc)
380+
return ltsGetPreallocBlock(lts, lt);
381+
else
382+
return ltsGetFreeBlock(lts);
383+
}
384+
385+
/*
386+
* Select the lowest currently unused block from the tape set's global free
387+
* list min heap.
378388
*/
379389
static long
380390
ltsGetFreeBlock(LogicalTapeSet *lts)
@@ -430,7 +440,8 @@ ltsGetFreeBlock(LogicalTapeSet *lts)
430440

431441
/*
432442
* Return the lowest free block number from the tape's preallocation list.
433-
* Refill the preallocation list if necessary.
443+
* Refill the preallocation list with blocks from the tape set's free list if
444+
* necessary.
434445
*/
435446
static long
436447
ltsGetPreallocBlock(LogicalTapeSet *lts, LogicalTape *lt)
@@ -671,8 +682,8 @@ ltsInitReadBuffer(LogicalTapeSet *lts, LogicalTape *lt)
671682
* infrastructure that may be lifted in the future.
672683
*/
673684
LogicalTapeSet *
674-
LogicalTapeSetCreate(int ntapes, TapeShare *shared, SharedFileSet *fileset,
675-
int worker)
685+
LogicalTapeSetCreate(int ntapes, bool preallocate, TapeShare *shared,
686+
SharedFileSet *fileset, int worker)
676687
{
677688
LogicalTapeSet *lts;
678689
int i;
@@ -689,6 +700,7 @@ LogicalTapeSetCreate(int ntapes, TapeShare *shared, SharedFileSet *fileset,
689700
lts->freeBlocksLen = 32; /* reasonable initial guess */
690701
lts->freeBlocks = (long *) palloc(lts->freeBlocksLen * sizeof(long));
691702
lts->nFreeBlocks = 0;
703+
lts->enable_prealloc = preallocate;
692704
lts->nTapes = ntapes;
693705
lts->tapes = (LogicalTape *) palloc(ntapes * sizeof(LogicalTape));
694706

@@ -782,7 +794,7 @@ LogicalTapeWrite(LogicalTapeSet *lts, int tapenum,
782794
Assert(lt->firstBlockNumber == -1);
783795
Assert(lt->pos == 0);
784796

785-
lt->curBlockNumber = ltsGetPreallocBlock(lts, lt);
797+
lt->curBlockNumber = ltsGetBlock(lts, lt);
786798
lt->firstBlockNumber = lt->curBlockNumber;
787799

788800
TapeBlockGetTrailer(lt->buffer)->prev = -1L;
@@ -806,7 +818,7 @@ LogicalTapeWrite(LogicalTapeSet *lts, int tapenum,
806818
* First allocate the next block, so that we can store it in the
807819
* 'next' pointer of this block.
808820
*/
809-
nextBlockNumber = ltsGetPreallocBlock(lts, lt);
821+
nextBlockNumber = ltsGetBlock(lts, lt);
810822

811823
/* set the next-pointer and dump the current block. */
812824
TapeBlockGetTrailer(lt->buffer)->next = nextBlockNumber;

src/backend/utils/sort/tuplesort.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2591,7 +2591,7 @@ inittapes(Tuplesortstate *state, bool mergeruns)
25912591
/* Create the tape set and allocate the per-tape data arrays */
25922592
inittapestate(state, maxTapes);
25932593
state->tapeset =
2594-
LogicalTapeSetCreate(maxTapes, NULL,
2594+
LogicalTapeSetCreate(maxTapes, false, NULL,
25952595
state->shared ? &state->shared->fileset : NULL,
25962596
state->worker);
25972597

@@ -4657,8 +4657,9 @@ leader_takeover_tapes(Tuplesortstate *state)
46574657
* randomAccess is disallowed for parallel sorts.
46584658
*/
46594659
inittapestate(state, nParticipants + 1);
4660-
state->tapeset = LogicalTapeSetCreate(nParticipants + 1, shared->tapes,
4661-
&shared->fileset, state->worker);
4660+
state->tapeset = LogicalTapeSetCreate(nParticipants + 1, false,
4661+
shared->tapes, &shared->fileset,
4662+
state->worker);
46624663

46634664
/* mergeruns() relies on currentRun for # of runs (in one-pass cases) */
46644665
state->currentRun = nParticipants;

src/include/utils/logtape.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ typedef struct TapeShare
5454
* prototypes for functions in logtape.c
5555
*/
5656

57-
extern LogicalTapeSet *LogicalTapeSetCreate(int ntapes, TapeShare *shared,
57+
extern LogicalTapeSet *LogicalTapeSetCreate(int ntapes, bool preallocate,
58+
TapeShare *shared,
5859
SharedFileSet *fileset, int worker);
5960
extern void LogicalTapeSetClose(LogicalTapeSet *lts);
6061
extern void LogicalTapeSetForgetFreeSpace(LogicalTapeSet *lts);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy