Re: Fix stale allocChunkLimit when recycling AllocSet

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: David Rowley <dgrowleyml(at)gmail(dot)com>
Cc: Donghang Lin <donghanglin(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Fix stale allocChunkLimit when recycling AllocSet
Date: 2026-10-19 07:30:45
Message-ID: D27F70C4-4167-4870-9949-B8F2EA42742B@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On Oct 19, 2026, at 14:51, David Rowley <dgrowleyml(at)gmail(dot)com> wrote:
>
> On Mon, 19 Oct 2026 at 19:04, Chao Li <li(dot)evan(dot)chao(at)gmail(dot)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/

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Chao Li 2026-10-19 08:12:06 Re: DOCS - CREATE PUBLICATION wording re 'ONLY' for partitioned tables.
Previous Message Vaibhav Dalvi 2026-10-19 07:20:54 Re: walreceiver: stale hot_standby_feedback xmin on primary when wal_receiver_status_interval = 0 (PG16 and later)