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

Commit93106d7

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 parentaeb7811 commit93106d7

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

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

219220
staticvoidltsWriteBlock(LogicalTapeSet*lts,longblocknum,void*buffer);
220221
staticvoidltsReadBlock(LogicalTapeSet*lts,longblocknum,void*buffer);
222+
staticlongltsGetBlock(LogicalTapeSet*lts,LogicalTape*lt);
221223
staticlongltsGetFreeBlock(LogicalTapeSet*lts);
222224
staticlongltsGetPreallocBlock(LogicalTapeSet*lts,LogicalTape*lt);
223225
staticvoidltsReleaseBlock(LogicalTapeSet*lts,longblocknum);
@@ -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+
staticlong
375+
ltsGetBlock(LogicalTapeSet*lts,LogicalTape*lt)
376+
{
377+
if (lts->enable_prealloc)
378+
returnltsGetPreallocBlock(lts,lt);
379+
else
380+
returnltsGetFreeBlock(lts);
381+
}
382+
383+
/*
384+
* Select the lowest currently unused block from the tape set's global free
385+
* list min heap.
376386
*/
377387
staticlong
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
staticlong
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(intntapes,TapeShare*shared,SharedFileSet*fileset,
673-
intworker)
683+
LogicalTapeSetCreate(intntapes,boolpreallocate,TapeShare*shared,
684+
SharedFileSet*fileset,intworker)
674685
{
675686
LogicalTapeSet*lts;
676687
inti;
@@ -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-
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