Fix stale allocChunkLimit when recycling AllocSet

Started by Chao Liabout 1 month ago5 messageshackers
Jump to latest
#1Chao Li
li.evan.chao@gmail.com

Hi,

While reading the memory context code, I found an issue with AllocSet’s recycling mechanism.

Looking at the code:
```
/*
* If a suitable freelist entry exists, just recycle that context.
*/
if (freeListIndex >= 0)
{
AllocSetFreeList *freelist = &context_freelists[freeListIndex];

if (freelist->first_free != NULL)
{
/* Remove entry from freelist */
set = freelist->first_free;
freelist->first_free = (AllocSet) set->header.nextchild;
freelist->num_free--;

/* Update its maxBlockSize; everything else should be OK */
set->maxBlockSize = maxBlockSize;

/* Reinitialize its header, installing correct name and parent */
MemoryContextCreate((MemoryContext) set,
T_AllocSetContext,
MCTX_ASET_ID,
parent,
name);
```

When recycling a context from the freelist, it only resets set->maxBlockSize without recalculating set->allocChunkLimit, while set->allocChunkLimit is derived from set->maxBlockSize.

Does this have a real impact? I verified it this way: I added a log to compare the retained allocChunkLimit with the expected allocChunkLimit, then ran “make check”. See the attached repro1.diff for this debug change, and repro1_result.txt, which shows a lot of hits at runtime, for example:
```
2026-10-16 13:20:12.174 CST client backend[66213] pg_regress/oidjoins LOG: EVAN: recycling AllocSet context "CachedPlanSource": maxBlockSize 8192 -> 8388608, allocChunkLimit retained 1024, expected 8192
```

AFAIK, when an allocation requests a size larger than set->allocChunkLimit, it goes to the system malloc(). Thus, a stale smaller allocChunkLimit may cause unintended malloc()/free() calls, which breaks the intended allocation policy.

Again, do some allocations really go to malloc() unintentionally? I added more logs and ran “make check”. See the attached repro2.diff and repro2_result.txt. There are also a lot of hits, for example:
```
2026-10-16 13:21:10.951 CST client backend[69075] pg_regress/event_trigger LOG: EVAN: AllocSet context "CachedPlanSource": allocation size 1328 wrongly takes malloc path; allocChunkLimit retained 1024, expected 8192
```

In the other direction, when the retained set->allocChunkLimit is larger than expected, allocations that should use dedicated malloc() blocks will be pooled, which leads to unexpected memory retention after pfree().

Once again, are there real hits? See the attached repro3.diff and repro3_result.txt. There are also a lot of hits, for example:
```
2026-10-16 13:40:37.168 CST client backend[73568] pg_regress/fast_default LOG: EVAN: AllocSet context "PortalContext": allocation size 2244 wrongly takes pooled chunk path; allocChunkLimit retained 8192, expected 1024
```

In summary, this bug doesn’t lead to memory leaks, but it breaks AllocSet’s intended allocation behavior: sometimes causing unexpected malloc()/free() calls, and sometimes unexpected pooling and memory retention.

The fix is straightforward. I extracted the allocChunkLimit computation into a shared helper and use it in both the creation and recycling paths. See the attached patch for details. Since this code is very old, the fix might be worth backpatching to all supported branches.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

t254231_1
repro1_result.txttext/plain; name=repro1_result.txt; x-unix-mode=0644Download
repro2_result.txttext/plain; name=repro2_result.txt; x-unix-mode=0644Download
repro3_result.txttext/plain; name=repro3_result.txt; x-unix-mode=0644Download
v1-0001-Recompute-allocChunkLimit-when-recycling-an-Alloc.patchapplication/octet-stream; name=v1-0001-Recompute-allocChunkLimit-when-recycling-an-Alloc.patch; x-unix-mode=0644Download+47-25
nocfbot_repro1.diffapplication/octet-stream; name=nocfbot_repro1.diff; x-unix-mode=0644Download+13-0
nocfbot_repro2.diffapplication/octet-stream; name=nocfbot_repro2.diff; x-unix-mode=0644Download+23-0
nocfbot_repro3.diffapplication/octet-stream; name=nocfbot_repro3.diff; x-unix-mode=0644Download+28-0
#2Donghang Lin
donghanglin@gmail.com
In reply to: Chao Li (#1)
Re: Fix stale allocChunkLimit when recycling AllocSet

Hi Chao,

When recycling a context from the freelist, it only resets set->maxBlockSize without recalculating set->allocChunkLimit, while set->allocChunkLimit is derived from set->maxBlockSize.

Nice find! It's clear that the reuse path diverges from the create
path for setting allocChunkLimit.

In summary, this bug doesn’t lead to memory leaks, but it breaks AllocSet’s intended allocation behavior: sometimes causing unexpected malloc()/free() calls, and sometimes unexpected pooling and memory retention.

Per the comment you moved to the helper function, if allocChunkLimit
doesn't follow the ALLOC_CHUNK_FRACTION formula, it could also lead to
unexpected size of memory waste at a block's tail.

The fix is straightforward. I extracted the allocChunkLimit computation into a shared helper and use it in both the creation and recycling paths. See the attached patch for details. Since this code is very old, the fix might be worth backpatching to all supported branches.

The fix looks good to me.

Regards,
Donghang

#3Chao Li
li.evan.chao@gmail.com
In reply to: Donghang Lin (#2)
Re: Fix stale allocChunkLimit when recycling AllocSet

On Oct 17, 2026, at 08:47, Donghang Lin <donghanglin@gmail.com> wrote:

Hi Chao,

When recycling a context from the freelist, it only resets set->maxBlockSize without recalculating set->allocChunkLimit, while set->allocChunkLimit is derived from set->maxBlockSize.

Nice find! It's clear that the reuse path diverges from the create
path for setting allocChunkLimit.

Thanks a lot for your review.

In summary, this bug doesn’t lead to memory leaks, but it breaks AllocSet’s intended allocation behavior: sometimes causing unexpected malloc()/free() calls, and sometimes unexpected pooling and memory retention.

Per the comment you moved to the helper function, if allocChunkLimit
doesn't follow the ALLOC_CHUNK_FRACTION formula, it could also lead to
unexpected size of memory waste at a block's tail.

Yes, that’s another consequence of stale larger allocChunkLimit.

The fix is straightforward. I extracted the allocChunkLimit computation into a shared helper and use it in both the creation and recycling paths. See the attached patch for details. Since this code is very old, the fix might be worth backpatching to all supported branches.

The fix looks good to me.

Cool!

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

#4David Rowley
dgrowleyml@gmail.com
In reply to: Chao Li (#3)
Re: Fix stale allocChunkLimit when recycling AllocSet

On Mon, 19 Oct 2026 at 19:04, Chao Li <li.evan.chao@gmail.com> wrote:

In summary, this bug doesn’t lead to memory leaks, but it breaks AllocSet’s intended allocation behavior: sometimes causing unexpected malloc()/free() calls, and sometimes unexpected pooling and memory retention.

Per the comment you moved to the helper function, if allocChunkLimit
doesn't follow the ALLOC_CHUNK_FRACTION formula, it could also lead to
unexpected size of memory waste at a block's tail.

Yes, that’s another consequence of stale larger allocChunkLimit.

I don't think this is 100% accurate, as when a non-large allocation is
made and there's not enough remaining space in the block, the
remaining space is divvied up and put onto the freelists. That might
happen to get wasted of those freelist items are never used, but to
say the entire remaining space is wasted isn't strictly true.

I did consider if this was worth a backpatch and I think it is, as
having the performance characteristics depend on the random chance of
what context was last pushed onto the freelist doesn't seem great.

I've backpatched this to v14. I kept the comments prior to the
allocChunkLimit being set the same as they had been in each version.
Annoyingly, that block had changed various times since v14.

I took the liberty and added an Assert(set->nextBlockSize ==
set->initBlockSize); to clarify that we expect that to have been done
the last time the context was reset.

David

#5Chao Li
li.evan.chao@gmail.com
In reply to: David Rowley (#4)
Re: Fix stale allocChunkLimit when recycling AllocSet

On Oct 19, 2026, at 14:51, David Rowley <dgrowleyml@gmail.com> wrote:

On Mon, 19 Oct 2026 at 19:04, Chao Li <li.evan.chao@gmail.com> wrote:

In summary, this bug doesn’t lead to memory leaks, but it breaks AllocSet’s intended allocation behavior: sometimes causing unexpected malloc()/free() calls, and sometimes unexpected pooling and memory retention.

Per the comment you moved to the helper function, if allocChunkLimit
doesn't follow the ALLOC_CHUNK_FRACTION formula, it could also lead to
unexpected size of memory waste at a block's tail.

Yes, that’s another consequence of stale larger allocChunkLimit.

I don't think this is 100% accurate, as when a non-large allocation is
made and there's not enough remaining space in the block, the
remaining space is divvied up and put onto the freelists. That might
happen to get wasted of those freelist items are never used, but to
say the entire remaining space is wasted isn't strictly true.

Thanks for the clarification.

I did consider if this was worth a backpatch and I think it is, as
having the performance characteristics depend on the random chance of
what context was last pushed onto the freelist doesn't seem great.

I've backpatched this to v14. I kept the comments prior to the
allocChunkLimit being set the same as they had been in each version.
Annoyingly, that block had changed various times since v14.

Thanks for pushing and back-patching.

I took the liberty and added an Assert(set->nextBlockSize ==
set->initBlockSize); to clarify that we expect that to have been done
the last time the context was reset.

David

Sounds good.

BTW, I learned to use "make check" for this kind of testing from you, so thanks again.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/