Skip to content

Commit 67b4fcf

Browse files
committed
Allocate local buffers in a context of their own, rather than dumping them
into TopMemoryContext. This makes no functional difference, but makes it easier to see what the space is being used for in MemoryContextStats dumps. Per a recent example in which I was surprised by the size of TopMemoryContext.
1 parent 780a7dc commit 67b4fcf

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/backend/storage/buffer/localbuf.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/storage/buffer/localbuf.c,v 1.89 2010/01/02 16:57:51 momjian Exp $
12+
* $PostgreSQL: pgsql/src/backend/storage/buffer/localbuf.c,v 1.89.6.1 2010/08/19 16:16:27 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -397,6 +397,7 @@ GetLocalBufferStorage(void)
397397
static int next_buf_in_block = 0;
398398
static int num_bufs_in_block = 0;
399399
static int total_bufs_allocated = 0;
400+
static MemoryContext LocalBufferContext = NULL;
400401

401402
char *this_buf;
402403

@@ -407,15 +408,27 @@ GetLocalBufferStorage(void)
407408
/* Need to make a new request to memmgr */
408409
int num_bufs;
409410

411+
/*
412+
* We allocate local buffers in a context of their own, so that the
413+
* space eaten for them is easily recognizable in MemoryContextStats
414+
* output. Create the context on first use.
415+
*/
416+
if (LocalBufferContext == NULL)
417+
LocalBufferContext =
418+
AllocSetContextCreate(TopMemoryContext,
419+
"LocalBufferContext",
420+
ALLOCSET_DEFAULT_MINSIZE,
421+
ALLOCSET_DEFAULT_INITSIZE,
422+
ALLOCSET_DEFAULT_MAXSIZE);
423+
410424
/* Start with a 16-buffer request; subsequent ones double each time */
411425
num_bufs = Max(num_bufs_in_block * 2, 16);
412426
/* But not more than what we need for all remaining local bufs */
413427
num_bufs = Min(num_bufs, NLocBuffer - total_bufs_allocated);
414428
/* And don't overflow MaxAllocSize, either */
415429
num_bufs = Min(num_bufs, MaxAllocSize / BLCKSZ);
416430

417-
/* Allocate space from TopMemoryContext so it never goes away */
418-
cur_block = (char *) MemoryContextAlloc(TopMemoryContext,
431+
cur_block = (char *) MemoryContextAlloc(LocalBufferContext,
419432
num_bufs * BLCKSZ);
420433
next_buf_in_block = 0;
421434
num_bufs_in_block = num_bufs;

0 commit comments

Comments
 (0)