aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/sort/tuplestore.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/utils/sort/tuplestore.c')
-rw-r--r--src/backend/utils/sort/tuplestore.c19
1 files changed, 13 insertions, 6 deletions
diff --git a/src/backend/utils/sort/tuplestore.c b/src/backend/utils/sort/tuplestore.c
index 627b281f67b..51f474d9c46 100644
--- a/src/backend/utils/sort/tuplestore.c
+++ b/src/backend/utils/sort/tuplestore.c
@@ -267,7 +267,14 @@ tuplestore_begin_common(int eflags, bool interXact, int maxKBytes)
state->memtupdeleted = 0;
state->memtupcount = 0;
- state->memtupsize = 1024; /* initial guess */
+
+ /*
+ * Initial size of array must be more than ALLOCSET_SEPARATE_THRESHOLD;
+ * see comments in grow_memtuples().
+ */
+ state->memtupsize = Max(16384 / sizeof(void *),
+ ALLOCSET_SEPARATE_THRESHOLD / sizeof(void *) + 1);
+
state->growmemtuples = true;
state->memtuples = (void **) palloc(state->memtupsize * sizeof(void *));
@@ -641,10 +648,10 @@ grow_memtuples(Tuplestorestate *state)
* never generate a dangerous request, but to be safe, check explicitly
* that the array growth fits within availMem. (We could still cause
* LACKMEM if the memory chunk overhead associated with the memtuples
- * array were to increase. That shouldn't happen with any sane value of
- * allowedMem, because at any array size large enough to risk LACKMEM,
- * palloc would be treating both old and new arrays as separate chunks.
- * But we'll check LACKMEM explicitly below just in case.)
+ * array were to increase. That shouldn't happen because we chose the
+ * initial array size large enough to ensure that palloc will be treating
+ * both old and new arrays as separate chunks. But we'll check LACKMEM
+ * explicitly below just in case.)
*/
if (state->availMem < (int64) ((newmemtupsize - memtupsize) * sizeof(void *)))
goto noalloc;
@@ -657,7 +664,7 @@ grow_memtuples(Tuplestorestate *state)
state->memtupsize * sizeof(void *));
USEMEM(state, GetMemoryChunkSpace(state->memtuples));
if (LACKMEM(state))
- elog(ERROR, "unexpected out-of-memory situation during sort");
+ elog(ERROR, "unexpected out-of-memory situation in tuplestore");
return true;
noalloc: