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

Commit166f943

Browse files
committed
Clarify the logic in a few places in the new balanced merge code.
In selectnewtape(), use 'nOutputTapes' rather than 'nOutputRuns' in thecheck for whether to start a new tape or to append a new run to anexisting tape. Until 'maxTapes' is reached, nOutputTapes is always equalto nOutputRuns, so it doesn't change the logic, but it seems more logicalto compare # of tapes with # of tapes. Also, currently maxTapes is nevermodified after the merging begins, but written this way, the code wouldstill work if it was. (Although the nOutputRuns == nOutputTapes assertionwould need to be removed and using nOutputRuns % nOutputTapes todistribute the runs evenly across the tapes wouldn't do a good jobanymore).Similarly in mergeruns(), change to USEMEM(state->tape_buffer_mem) toaccount for the memory used for tape buffers. It's equal to availMemcurrently, but tape_buffer_mem is more direct and future-proof. Forexample, if we changed the logic to only allocate half of the remainingmemory to tape buffers, USEMEM(state->tape_buffer_mem) would still becorrect.Coverity complained about these. Hopefully this patch helps it tounderstand the logic better. Thanks to Tom Lane for initial analysis.
1 parentb4ada4e commit166f943

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

‎src/backend/utils/sort/tuplesort.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2773,13 +2773,19 @@ inittapestate(Tuplesortstate *state, int maxTapes)
27732773
staticvoid
27742774
selectnewtape(Tuplesortstate*state)
27752775
{
2776-
if (state->nOutputRuns<state->maxTapes)
2776+
/*
2777+
* At the beginning of each merge pass, nOutputTapes and nOutputRuns are
2778+
* both zero. On each call, we create a new output tape to hold the next
2779+
* run, until maxTapes is reached. After that, we assign new runs to the
2780+
* existing tapes in a round robin fashion.
2781+
*/
2782+
if (state->nOutputTapes<state->maxTapes)
27772783
{
27782784
/* Create a new tape to hold the next run */
27792785
Assert(state->outputTapes[state->nOutputRuns]==NULL);
27802786
Assert(state->nOutputRuns==state->nOutputTapes);
27812787
state->destTape=LogicalTapeCreate(state->tapeset);
2782-
state->outputTapes[state->nOutputRuns]=state->destTape;
2788+
state->outputTapes[state->nOutputTapes]=state->destTape;
27832789
state->nOutputTapes++;
27842790
state->nOutputRuns++;
27852791
}
@@ -2908,7 +2914,7 @@ mergeruns(Tuplesortstate *state)
29082914
* divide this memory between the input and output tapes in the pass.
29092915
*/
29102916
state->tape_buffer_mem=state->availMem;
2911-
USEMEM(state,state->availMem);
2917+
USEMEM(state,state->tape_buffer_mem);
29122918
#ifdefTRACE_SORT
29132919
if (trace_sort)
29142920
elog(LOG,"worker %d using %zu KB of memory for tape buffers",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp