Re: Fix stale allocChunkLimit when recycling AllocSet

From: David Rowley <dgrowleyml(at)gmail(dot)com>
To: Chao Li <li(dot)evan(dot)chao(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 06:51:36
Message-ID: CAApHDvrkOWzHt4iqE2Q3bJA7D+TorUepZ=8sNQMYcyoU+vVEBg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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.

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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Richard Guo 2026-10-19 06:56:04 "variable not found in subplan target lists" with partitionwise join
Previous Message Peter Smith 2026-10-19 06:20:19 Re: Publication of same parent table with ONLY-ness mismatch should error