Skip to content

Commit 118c9bb

Browse files
committed
Fix bogus "out of memory" reports in tuplestore.c.
The tuplesort/tuplestore memory management logic assumed that the chunk allocation overhead for its memtuples array could not increase when increasing the array size. This is and always was true for tuplesort, but we (I, I think) blindly copied that logic into tuplestore.c without noticing that the assumption failed to hold for the much smaller array elements used by tuplestore. Given rather small work_mem, this could result in an improper complaint about "unexpected out-of-memory situation", as reported by Brent DeSpain in bug #13530. The easiest way to fix this is just to increase tuplestore's initial array size so that the assumption holds. Rather than relying on magic constants, though, let's export a #define from aset.c that represents the safe allocation threshold, and make tuplestore's calculation depend on that. Do the same in tuplesort.c to keep the logic looking parallel, even though tuplesort.c isn't actually at risk at present. This will keep us from breaking it if we ever muck with the allocation parameters in aset.c. Back-patch to all supported versions. The error message doesn't occur pre-9.3, not so much because the problem can't happen as because the pre-9.3 tuplestore code neglected to check for it. (The chance of trouble is a great deal larger as of 9.3, though, due to changes in the array-size-increasing strategy.) However, allowing LACKMEM() to become true unexpectedly could still result in less-than-desirable behavior, so let's patch it all the way back.
1 parent b58e8ca commit 118c9bb

File tree

4 files changed

+42
-15
lines changed

4 files changed

+42
-15
lines changed

src/backend/utils/mmgr/aset.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@
112112
*
113113
* With the current parameters, request sizes up to 8K are treated as chunks,
114114
* larger requests go into dedicated blocks. Change ALLOCSET_NUM_FREELISTS
115-
* to adjust the boundary point. (But in contexts with small maxBlockSize,
116-
* we may set the allocChunkLimit to less than 8K, so as to avoid space
117-
* wastage.)
115+
* to adjust the boundary point; and adjust ALLOCSET_SEPARATE_THRESHOLD in
116+
* memutils.h to agree. (Note: in contexts with small maxBlockSize, we may
117+
* set the allocChunkLimit to less than 8K, so as to avoid space wastage.)
118118
*--------------------
119119
*/
120120

@@ -476,7 +476,12 @@ AllocSetContextCreate(MemoryContext parent,
476476
* We have to have allocChunkLimit a power of two, because the requested
477477
* and actually-allocated sizes of any chunk must be on the same side of
478478
* the limit, else we get confused about whether the chunk is "big".
479+
*
480+
* Also, allocChunkLimit must not exceed ALLOCSET_SEPARATE_THRESHOLD.
479481
*/
482+
StaticAssertStmt(ALLOC_CHUNK_LIMIT == ALLOCSET_SEPARATE_THRESHOLD,
483+
"ALLOC_CHUNK_LIMIT != ALLOCSET_SEPARATE_THRESHOLD");
484+
480485
context->allocChunkLimit = ALLOC_CHUNK_LIMIT;
481486
while ((Size) (context->allocChunkLimit + ALLOC_CHUNKHDRSZ) >
482487
(Size) ((maxBlockSize - ALLOC_BLOCKHDRSZ) / ALLOC_CHUNK_FRACTION))

src/backend/utils/sort/tuplesort.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,14 @@ tuplesort_begin_common(int workMem, bool randomAccess)
571571
state->tapeset = NULL;
572572

573573
state->memtupcount = 0;
574-
state->memtupsize = 1024; /* initial guess */
574+
575+
/*
576+
* Initial size of array must be more than ALLOCSET_SEPARATE_THRESHOLD;
577+
* see comments in grow_memtuples().
578+
*/
579+
state->memtupsize = Max(1024,
580+
ALLOCSET_SEPARATE_THRESHOLD / sizeof(SortTuple) + 1);
581+
575582
state->growmemtuples = true;
576583
state->memtuples = (SortTuple *) palloc(state->memtupsize * sizeof(SortTuple));
577584

@@ -1064,10 +1071,10 @@ grow_memtuples(Tuplesortstate *state)
10641071
* never generate a dangerous request, but to be safe, check explicitly
10651072
* that the array growth fits within availMem. (We could still cause
10661073
* LACKMEM if the memory chunk overhead associated with the memtuples
1067-
* array were to increase. That shouldn't happen with any sane value of
1068-
* allowedMem, because at any array size large enough to risk LACKMEM,
1069-
* palloc would be treating both old and new arrays as separate chunks.
1070-
* But we'll check LACKMEM explicitly below just in case.)
1074+
* array were to increase. That shouldn't happen because we chose the
1075+
* initial array size large enough to ensure that palloc will be treating
1076+
* both old and new arrays as separate chunks. But we'll check LACKMEM
1077+
* explicitly below just in case.)
10711078
*/
10721079
if (state->availMem < (int64) ((newmemtupsize - memtupsize) * sizeof(SortTuple)))
10731080
goto noalloc;
@@ -1080,7 +1087,7 @@ grow_memtuples(Tuplesortstate *state)
10801087
state->memtupsize * sizeof(SortTuple));
10811088
USEMEM(state, GetMemoryChunkSpace(state->memtuples));
10821089
if (LACKMEM(state))
1083-
elog(ERROR, "unexpected out-of-memory situation during sort");
1090+
elog(ERROR, "unexpected out-of-memory situation in tuplesort");
10841091
return true;
10851092

10861093
noalloc:

src/backend/utils/sort/tuplestore.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,14 @@ tuplestore_begin_common(int eflags, bool interXact, int maxKBytes)
265265

266266
state->memtupdeleted = 0;
267267
state->memtupcount = 0;
268-
state->memtupsize = 1024; /* initial guess */
268+
269+
/*
270+
* Initial size of array must be more than ALLOCSET_SEPARATE_THRESHOLD;
271+
* see comments in grow_memtuples().
272+
*/
273+
state->memtupsize = Max(16384 / sizeof(void *),
274+
ALLOCSET_SEPARATE_THRESHOLD / sizeof(void *) + 1);
275+
269276
state->growmemtuples = true;
270277
state->memtuples = (void **) palloc(state->memtupsize * sizeof(void *));
271278

@@ -639,10 +646,10 @@ grow_memtuples(Tuplestorestate *state)
639646
* never generate a dangerous request, but to be safe, check explicitly
640647
* that the array growth fits within availMem. (We could still cause
641648
* LACKMEM if the memory chunk overhead associated with the memtuples
642-
* array were to increase. That shouldn't happen with any sane value of
643-
* allowedMem, because at any array size large enough to risk LACKMEM,
644-
* palloc would be treating both old and new arrays as separate chunks.
645-
* But we'll check LACKMEM explicitly below just in case.)
649+
* array were to increase. That shouldn't happen because we chose the
650+
* initial array size large enough to ensure that palloc will be treating
651+
* both old and new arrays as separate chunks. But we'll check LACKMEM
652+
* explicitly below just in case.)
646653
*/
647654
if (state->availMem < (int64) ((newmemtupsize - memtupsize) * sizeof(void *)))
648655
goto noalloc;
@@ -655,7 +662,7 @@ grow_memtuples(Tuplestorestate *state)
655662
state->memtupsize * sizeof(void *));
656663
USEMEM(state, GetMemoryChunkSpace(state->memtuples));
657664
if (LACKMEM(state))
658-
elog(ERROR, "unexpected out-of-memory situation during sort");
665+
elog(ERROR, "unexpected out-of-memory situation in tuplestore");
659666
return true;
660667

661668
noalloc:

src/include/utils/memutils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,12 @@ extern MemoryContext AllocSetContextCreate(MemoryContext parent,
145145
#define ALLOCSET_SMALL_INITSIZE (1 * 1024)
146146
#define ALLOCSET_SMALL_MAXSIZE (8 * 1024)
147147

148+
/*
149+
* Threshold above which a request in an AllocSet context is certain to be
150+
* allocated separately (and thereby have constant allocation overhead).
151+
* Few callers should be interested in this, but tuplesort/tuplestore need
152+
* to know it.
153+
*/
154+
#define ALLOCSET_SEPARATE_THRESHOLD 8192
155+
148156
#endif /* MEMUTILS_H */

0 commit comments

Comments
 (0)