From 80cadb303ce035a291a1b7441adb59073a9529c0 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sat, 4 Mar 2006 19:05:06 +0000 Subject: [PATCH] Prevent sorting from requesting a SortTuple array that exceeds MaxAllocSize; we'll go over to disk-based sort if we reach that limit. This fixes Stefan Kaltenbrunner's observation that sorting can suffer an 'invalid memory alloc request size' failure when sort_mem is set large enough. It's unfortunately not so easy to fix in 8.1 ... --- src/backend/utils/sort/tuplesort.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/backend/utils/sort/tuplesort.c b/src/backend/utils/sort/tuplesort.c index 0f680d0e2c..aa8660ff18 100644 --- a/src/backend/utils/sort/tuplesort.c +++ b/src/backend/utils/sort/tuplesort.c @@ -91,7 +91,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/sort/tuplesort.c,v 1.60 2006/02/26 22:58:12 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/sort/tuplesort.c,v 1.61 2006/03/04 19:05:06 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -741,6 +741,13 @@ grow_memtuples(Tuplesortstate *state) */ if (state->availMem <= (long) (state->memtupsize * sizeof(SortTuple))) return false; + /* + * On a 64-bit machine, allowedMem could be high enough to get us into + * trouble with MaxAllocSize, too. + */ + if ((Size) (state->memtupsize * 2) >= MaxAllocSize / sizeof(SortTuple)) + return false; + FREEMEM(state, GetMemoryChunkSpace(state->memtuples)); state->memtupsize *= 2; state->memtuples = (SortTuple *) -- 2.11.0