Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0758964

Browse files
committed
logtape.c: do not preallocate for tapes when sorting
The preallocation logic is only useful for HashAgg, so disable it whensorting.Also, adjust an out-of-date comment.Reviewed-by: Peter GeogheganDiscussion:https://postgr.es/m/CAH2-Wzn_o7tE2+hRVvwSFghRb75AJ5g-nqGzDUqLYMexjOAe=g@mail.gmail.comBackpatch-through: 13
1 parent7634bd4 commit0758964

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
intinit_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
longnFreeBlocks;/* # of currently free blocks */
214214
SizefreeBlocksLen;/* current allocated length of freeBlocks[] */
215+
boolenable_prealloc;/* preallocate write blocks? */
215216

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

221222
staticvoidltsWriteBlock(LogicalTapeSet*lts,longblocknum,void*buffer);
222223
staticvoidltsReadBlock(LogicalTapeSet*lts,longblocknum,void*buffer);
224+
staticlongltsGetBlock(LogicalTapeSet*lts,LogicalTape*lt);
223225
staticlongltsGetFreeBlock(LogicalTapeSet*lts);
224226
staticlongltsGetPreallocBlock(LogicalTapeSet*lts,LogicalTape*lt);
225227
staticvoidltsReleaseBlock(LogicalTapeSet*lts,longblocknum);
@@ -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+
staticlong
377+
ltsGetBlock(LogicalTapeSet*lts,LogicalTape*lt)
378+
{
379+
if (lts->enable_prealloc)
380+
returnltsGetPreallocBlock(lts,lt);
381+
else
382+
returnltsGetFreeBlock(lts);
383+
}
384+
385+
/*
386+
* Select the lowest currently unused block from the tape set's global free
387+
* list min heap.
378388
*/
379389
staticlong
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
staticlong
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(intntapes,TapeShare*shared,SharedFileSet*fileset,
675-
intworker)
685+
LogicalTapeSetCreate(intntapes,boolpreallocate,TapeShare*shared,
686+
SharedFileSet*fileset,intworker)
676687
{
677688
LogicalTapeSet*lts;
678689
inti;
@@ -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-
externLogicalTapeSet*LogicalTapeSetCreate(intntapes,TapeShare*shared,
57+
externLogicalTapeSet*LogicalTapeSetCreate(intntapes,boolpreallocate,
58+
TapeShare*shared,
5859
SharedFileSet*fileset,intworker);
5960
externvoidLogicalTapeSetClose(LogicalTapeSet*lts);
6061
externvoidLogicalTapeSetForgetFreeSpace(LogicalTapeSet*lts);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp