OSDN Git Service

Prevent sorting from requesting a SortTuple array that exceeds MaxAllocSize;
authorTom Lane <tgl@sss.pgh.pa.us>
Sat, 4 Mar 2006 19:05:06 +0000 (19:05 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Sat, 4 Mar 2006 19:05:06 +0000 (19:05 +0000)
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

index 0f680d0..aa8660f 100644 (file)
@@ -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 *)