Skip to content

Commit 77a7e99

Browse files
committed
Change memory-space accounting mechanism in tuplesort.c and tuplestore.c
to make a reasonable attempt at accounting for palloc overhead, not just the requested size of each memory chunk. Since in many scenarios this will make for a significant reduction in the amount of space acquired, partially compensate by doubling the default value of SORT_MEM to 1Mb. Per discussion in pgsql-general around 9-Jun-2002..
1 parent e44beef commit 77a7e99

File tree

10 files changed

+139
-133
lines changed

10 files changed

+139
-133
lines changed

doc/src/sgml/runtime.sgml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/runtime.sgml,v 1.123 2002/08/09 22:52:04 petere Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/runtime.sgml,v 1.124 2002/08/12 00:36:11 tgl Exp $
33
-->
44

55
<Chapter Id="runtime">
@@ -1640,8 +1640,8 @@ dynamic_library_path = '/usr/local/lib/postgresql:/home/my_project/lib:$libdir'
16401640
<para>
16411641
Specifies the amount of memory to be used by internal sorts and
16421642
hashes before switching to temporary disk files. The value is
1643-
specified in kilobytes, and defaults to 512 kilobytes. Note that
1644-
for a complex query, several sorts and/or hashes might be
1643+
specified in kilobytes, and defaults to 1024 kilobytes (1MB).
1644+
Note that for a complex query, several sorts and/or hashes might be
16451645
running in parallel, and each one will be allowed to use as much
16461646
memory as this value specifies before it starts to put data into
16471647
temporary files. Also, each running backend could be doing one

src/backend/utils/init/globals.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/init/globals.c,v 1.65 2002/06/20 20:29:40 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/init/globals.c,v 1.66 2002/08/12 00:36:11 tgl Exp $
1212
*
1313
* NOTES
1414
* Globals used all over the place should be declared here and not
@@ -71,6 +71,6 @@ char FloatFormat[20] = "%f";
7171

7272
bool enableFsync = true;
7373
bool allowSystemTableMods = false;
74-
int SortMem = 512;
74+
int SortMem = 1024;
7575
int VacuumMem = 8192;
7676
int NBuffers = DEF_NBUFFERS;

src/backend/utils/misc/guc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* command, configuration file, and command line options.
66
* See src/backend/utils/misc/README for more information.
77
*
8-
* $Header: /cvsroot/pgsql/src/backend/utils/misc/guc.c,v 1.78 2002/08/07 17:26:24 tgl Exp $
8+
* $Header: /cvsroot/pgsql/src/backend/utils/misc/guc.c,v 1.79 2002/08/12 00:36:11 tgl Exp $
99
*
1010
* Copyright 2000 by PostgreSQL Global Development Group
1111
* Written by Peter Eisentraut <peter_e@gmx.net>.
@@ -556,7 +556,7 @@ static struct config_int
556556

557557
{
558558
{ "sort_mem", PGC_USERSET }, &SortMem,
559-
512, 4 * BLCKSZ / 1024, INT_MAX, NULL, NULL
559+
1024, 8 * BLCKSZ / 1024, INT_MAX, NULL, NULL
560560
},
561561

562562
{

src/backend/utils/misc/postgresql.conf.sample

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
#
5858
# Non-shared Memory Sizes
5959
#
60-
#sort_mem = 512 # min 32
60+
#sort_mem = 1024 # min 64
6161
#vacuum_mem = 8192 # min 1024
6262

6363

src/backend/utils/mmgr/aset.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Portions Copyright (c) 1994, Regents of the University of California
1212
*
1313
* IDENTIFICATION
14-
* $Header: /cvsroot/pgsql/src/backend/utils/mmgr/aset.c,v 1.46 2002/06/20 20:29:40 momjian Exp $
14+
* $Header: /cvsroot/pgsql/src/backend/utils/mmgr/aset.c,v 1.47 2002/08/12 00:36:12 tgl Exp $
1515
*
1616
* NOTE:
1717
* This is a new (Feb. 05, 1999) implementation of the allocation set
@@ -204,11 +204,11 @@ static void *AllocSetRealloc(MemoryContext context, void *pointer, Size size);
204204
static void AllocSetInit(MemoryContext context);
205205
static void AllocSetReset(MemoryContext context);
206206
static void AllocSetDelete(MemoryContext context);
207-
207+
static Size AllocSetGetChunkSpace(MemoryContext context, void *pointer);
208+
static void AllocSetStats(MemoryContext context);
208209
#ifdef MEMORY_CONTEXT_CHECKING
209210
static void AllocSetCheck(MemoryContext context);
210211
#endif
211-
static void AllocSetStats(MemoryContext context);
212212

213213
/*
214214
* This is the virtual function table for AllocSet contexts.
@@ -220,10 +220,11 @@ static MemoryContextMethods AllocSetMethods = {
220220
AllocSetInit,
221221
AllocSetReset,
222222
AllocSetDelete,
223+
AllocSetGetChunkSpace,
224+
AllocSetStats
223225
#ifdef MEMORY_CONTEXT_CHECKING
224-
AllocSetCheck,
226+
, AllocSetCheck
225227
#endif
226-
AllocSetStats
227228
};
228229

229230

@@ -953,6 +954,19 @@ AllocSetRealloc(MemoryContext context, void *pointer, Size size)
953954
}
954955
}
955956

957+
/*
958+
* AllocSetGetChunkSpace
959+
* Given a currently-allocated chunk, determine the total space
960+
* it occupies (including all memory-allocation overhead).
961+
*/
962+
static Size
963+
AllocSetGetChunkSpace(MemoryContext context, void *pointer)
964+
{
965+
AllocChunk chunk = AllocPointerGetChunk(pointer);
966+
967+
return chunk->size + ALLOC_CHUNKHDRSZ;
968+
}
969+
956970
/*
957971
* AllocSetStats
958972
* Displays stats about memory consumption of an allocset.

src/backend/utils/mmgr/mcxt.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
*
1616
* IDENTIFICATION
17-
* $Header: /cvsroot/pgsql/src/backend/utils/mmgr/mcxt.c,v 1.31 2002/08/10 20:29:18 momjian Exp $
17+
* $Header: /cvsroot/pgsql/src/backend/utils/mmgr/mcxt.c,v 1.32 2002/08/12 00:36:12 tgl Exp $
1818
*
1919
*-------------------------------------------------------------------------
2020
*/
@@ -224,6 +224,39 @@ MemoryContextResetAndDeleteChildren(MemoryContext context)
224224
(*context->methods->reset) (context);
225225
}
226226

227+
/*
228+
* GetMemoryChunkSpace
229+
* Given a currently-allocated chunk, determine the total space
230+
* it occupies (including all memory-allocation overhead).
231+
*
232+
* This is useful for measuring the total space occupied by a set of
233+
* allocated chunks.
234+
*/
235+
Size
236+
GetMemoryChunkSpace(void *pointer)
237+
{
238+
StandardChunkHeader *header;
239+
240+
/*
241+
* Try to detect bogus pointers handed to us, poorly though we can.
242+
* Presumably, a pointer that isn't MAXALIGNED isn't pointing at an
243+
* allocated chunk.
244+
*/
245+
Assert(pointer != NULL);
246+
Assert(pointer == (void *) MAXALIGN(pointer));
247+
248+
/*
249+
* OK, it's probably safe to look at the chunk header.
250+
*/
251+
header = (StandardChunkHeader *)
252+
((char *) pointer - STANDARDCHUNKHEADERSIZE);
253+
254+
AssertArg(MemoryContextIsValid(header->context));
255+
256+
return (*header->context->methods->get_chunk_space) (header->context,
257+
pointer);
258+
}
259+
227260
/*
228261
* MemoryContextStats
229262
* Print statistics about the named context and all its descendants.

0 commit comments

Comments
 (0)