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

Commitf7d54f4

Browse files
committed
Fix accounting of memory needed for merge heap.
We allegedly allocated all remaining memory for the read buffers of thesort tapes, but we allocated the merge heap only after that. That meansthat the allocation of the merge heap was guaranteed to go over the memorylimit. Fix by allocating the merge heap first. This makes little differencein practice, because the merge heap is tiny, but let's tidy.While we're at it, add a safeguard for the case that we are already overthe limit when allocating the read buffers. That shouldn't happen, butbetter safe than sorry.The memory accounting error was reported off-list by Peter Geoghegan.
1 parentcd5d3af commitf7d54f4

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2637,8 +2637,16 @@ mergeruns(Tuplesortstate *state)
26372637
}
26382638

26392639
/*
2640-
* Use all the spare memory we have available for read buffers among the
2641-
* input tapes.
2640+
* Allocate a new 'memtuples' array, for the heap. It will hold one tuple
2641+
* from each input tape.
2642+
*/
2643+
state->memtupsize=numInputTapes;
2644+
state->memtuples= (SortTuple*)palloc(numInputTapes*sizeof(SortTuple));
2645+
USEMEM(state,GetMemoryChunkSpace(state->memtuples));
2646+
2647+
/*
2648+
* Use all the remaining memory we have available for read buffers among
2649+
* the input tapes.
26422650
*
26432651
* We do this only after checking for the case that we produced only one
26442652
* initial run, because there is no need to use a large read buffer when
@@ -2661,17 +2669,9 @@ mergeruns(Tuplesortstate *state)
26612669
(state->availMem) /1024,numInputTapes);
26622670
#endif
26632671

2664-
state->read_buffer_size=state->availMem /numInputTapes;
2672+
state->read_buffer_size=Min(state->availMem /numInputTapes,0);
26652673
USEMEM(state,state->availMem);
26662674

2667-
/*
2668-
* Allocate a new 'memtuples' array, for the heap. It will hold one tuple
2669-
* from each input tape.
2670-
*/
2671-
state->memtupsize=numInputTapes;
2672-
state->memtuples= (SortTuple*)palloc(numInputTapes*sizeof(SortTuple));
2673-
USEMEM(state,GetMemoryChunkSpace(state->memtuples));
2674-
26752675
/* End of step D2: rewind all output tapes to prepare for merging */
26762676
for (tapenum=0;tapenum<state->tapeRange;tapenum++)
26772677
LogicalTapeRewindForRead(state->tapeset,tapenum,state->read_buffer_size);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp