Skip to content

Commit 93106d7

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 aeb7811 commit 93106d7

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
@@ -210,6 +210,7 @@ struct LogicalTapeSet
210210
long *freeBlocks; /* resizable array holding minheap */
211211
long nFreeBlocks; /* # of currently free blocks */
212212
Size freeBlocksLen; /* current allocated length of freeBlocks[] */
213+
bool enable_prealloc; /* preallocate write blocks? */
213214

214215
/* The array of logical tapes. */
215216
int nTapes; /* # of logical tapes in set */
@@ -218,6 +219,7 @@ struct LogicalTapeSet
218219

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

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

429439
/*
430440
* Return the lowest free block number from the tape's preallocation list.
431-
* Refill the preallocation list if necessary.
441+
* Refill the preallocation list with blocks from the tape set's free list if
442+
* necessary.
432443
*/
433444
static long
434445
ltsGetPreallocBlock(LogicalTapeSet *lts, LogicalTape *lt)
@@ -669,8 +680,8 @@ ltsInitReadBuffer(LogicalTapeSet *lts, LogicalTape *lt)
669680
* infrastructure that may be lifted in the future.
670681
*/
671682
LogicalTapeSet *
672-
LogicalTapeSetCreate(int ntapes, TapeShare *shared, SharedFileSet *fileset,
673-
int worker)
683+
LogicalTapeSetCreate(int ntapes, bool preallocate, TapeShare *shared,
684+
SharedFileSet *fileset, int worker)
674685
{
675686
LogicalTapeSet *lts;
676687
int i;
@@ -687,6 +698,7 @@ LogicalTapeSetCreate(int ntapes, TapeShare *shared, SharedFileSet *fileset,
687698
lts->freeBlocksLen = 32; /* reasonable initial guess */
688699
lts->freeBlocks = (long *) palloc(lts->freeBlocksLen * sizeof(long));
689700
lts->nFreeBlocks = 0;
701+
lts->enable_prealloc = preallocate;
690702
lts->nTapes = ntapes;
691703
lts->tapes = (LogicalTape *) palloc(ntapes * sizeof(LogicalTape));
692704

@@ -780,7 +792,7 @@ LogicalTapeWrite(LogicalTapeSet *lts, int tapenum,
780792
Assert(lt->firstBlockNumber == -1);
781793
Assert(lt->pos == 0);
782794

783-
lt->curBlockNumber = ltsGetPreallocBlock(lts, lt);
795+
lt->curBlockNumber = ltsGetBlock(lts, lt);
784796
lt->firstBlockNumber = lt->curBlockNumber;
785797

786798
TapeBlockGetTrailer(lt->buffer)->prev = -1L;
@@ -804,7 +816,7 @@ LogicalTapeWrite(LogicalTapeSet *lts, int tapenum,
804816
* First allocate the next block, so that we can store it in the
805817
* 'next' pointer of this block.
806818
*/
807-
nextBlockNumber = ltsGetPreallocBlock(lts, lt);
819+
nextBlockNumber = ltsGetBlock(lts, lt);
808820

809821
/* set the next-pointer and dump the current block. */
810822
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