> On Mar 2, 2026, at 07:05, Michael Paquier <[email protected]> wrote:
>
> On Fri, Feb 27, 2026 at 04:33:24PM +0900, Michael Paquier wrote:
>> On Fri, Feb 27, 2026 at 02:51:40PM +0800, Chao Li wrote:
>>> Here stats itself is destroyed, but memory pointed by
>>> stats[0]~stats[i-1] are not free-ed, those memory are returned from
>>> examine_attribute() by palloc0_object().
>>
>> I am aware of that. This is not done on simplicity ground, keeping
>> the cleanup of the memory context to ANALYZE in this case.
>
> About this one, something worth noting is the beginning of
> do_analyze_rel(), which does the following:
> /*
> * Set up a working context so that we can easily free whatever junk gets
> * created.
> */
> anl_context = AllocSetContextCreate(CurrentMemoryContext,
> "Analyze",
> ALLOCSET_DEFAULT_SIZES);
> caller_context = MemoryContextSwitchTo(anl_context);
>
> So these extra allocations would just be freed under this memory
> context umbrella once we are done processing a single relation. This
> works even if we begin to repeat ANALYZE commands that fail to build
> some of the stats in a repeated fashion in a single transaction block.
> Code simplicity and readability is just a better choice for this path.
> --
> Michael
Yeah, memory context is a great mechanism.
But why do we still explicitly free the stats array? My concern is mostly about
“partial free”: either free everything, or free nothing and let the memory
context clean it up. “Partial free” tends to confuse code readers, and future
readers may keep running into the same question.
This feels similar to what we discussed in [1]. If we don’t free the container
at all, that’s fine too. But freeing the container while leaving its members
behind is also a kind of “partial free”, and it’s hard to reason about.
[1]
https://www.postgresql.org/message-id/5DED17B4-D948-4C0B-9DE1-A915D0BFAA54%40gmail.com
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/