Proposal : Use bump memory context for temp buffers
Hi,
Commit [1]29f6a959cfd8ffa7d6db2c0629439c5329e2853e introduced a new memory context suitable for situations when we
should allocate a large amount of memory with no need to free or reallocate it.
I think that it will be useful for temp buffers :
1) We allocate them lazily and never try to free them.
2) Some users are very active in working with temporary tables, and as
a result,
they set large values for the "temp_buffers" parameter (several gigabytes).
Thus, the use case for temp buffers seems to perfectly fit for bump
memory context.
What do you think?
[1]: 29f6a959cfd8ffa7d6db2c0629439c5329e2853e
--
Best regards,
Daniil Davydov
Attachments:
0001-Use-bump-memcxt-for-temp-buffers.patchtext/x-patch; charset=US-ASCII; name=0001-Use-bump-memcxt-for-temp-buffers.patchDownload
From 046f86e79110005d783b00fe0a73af7ad2a0d3cf Mon Sep 17 00:00:00 2001
From: Daniil Davidov <d.davydov@postgrespro.ru>
Date: Tue, 16 Dec 2025 14:45:22 +0700
Subject: [PATCH] Use bump memcxt for temp buffers
---
src/backend/storage/buffer/localbuf.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/src/backend/storage/buffer/localbuf.c b/src/backend/storage/buffer/localbuf.c
index 15aac7d1c9f..cb2d7141f9d 100644
--- a/src/backend/storage/buffer/localbuf.c
+++ b/src/backend/storage/buffer/localbuf.c
@@ -922,9 +922,9 @@ GetLocalBufferStorage(void)
*/
if (LocalBufferContext == NULL)
LocalBufferContext =
- AllocSetContextCreate(TopMemoryContext,
- "LocalBufferContext",
- ALLOCSET_DEFAULT_SIZES);
+ BumpContextCreate(TopMemoryContext,
+ "LocalBufferContext",
+ ALLOCSET_DEFAULT_SIZES);
/* Start with a 16-buffer request; subsequent ones double each time */
num_bufs = Max(num_bufs_in_block * 2, 16);
--
2.43.0
On 16/12/2025 15:05, Daniil Davydov wrote:
Hi,
Commit [1] introduced a new memory context suitable for situations when we
should allocate a large amount of memory with no need to free or reallocate it.I think that it will be useful for temp buffers :
1) We allocate them lazily and never try to free them.
2) Some users are very active in working with temporary tables, and as
a result,
they set large values for the "temp_buffers" parameter (several gigabytes).Thus, the use case for temp buffers seems to perfectly fit for bump
memory context.
What do you think?
It makes no difference. The bump memory context is useful if you perform
a lot of small allocations, because it skips the overhead of the chunk
headers. In LocalBufferContext, we only ever make one allocation.
- Heikki
Hi,
On Tue, Dec 16, 2025 at 8:51 PM Heikki Linnakangas <hlinnaka@iki.fi> wrote:
On 16/12/2025 15:05, Daniil Davydov wrote:
I think that it will be useful for temp buffers :
1) We allocate them lazily and never try to free them.
2) Some users are very active in working with temporary tables, and as
a result,
they set large values for the "temp_buffers" parameter (several gigabytes).It makes no difference. The bump memory context is useful if you perform
a lot of small allocations, because it skips the overhead of the chunk
headers. In LocalBufferContext, we only ever make one allocation.
Thanks for the explanation!
Actually, we allocate temp buffers gradually by memory chunks with
increasing size. So if temp buffers are pretty big, we must to do about 20
allocations.
I 100% agree that the amount of saved memory will be very-very low
compared to the size of the temp buffers. When I wrote "perfectly fit for bump
memory context" I meant design rather than the performance increasing.
--
Best regards,
Daniil Davydov