Re: BUG #19040: Memory leak in hashed subplan node due to missing hashtempcxt reset

From: feichanghong <feichanghong(at)qq(dot)com>
To: "李海洋(陌痕)" <mohen(dot)lhy(at)alibaba-inc(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, ocean_li_996 <ocean_li_996(at)163(dot)com>, "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #19040: Memory leak in hashed subplan node due to missing hashtempcxt reset
Date: 2025-09-07 09:02:23
Message-ID: tencent_0C8392F1FB41065427D6C7AC8919073F8206@qq.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

> On Sep 7, 2025, at 16:24, 李海洋(陌痕) <mohen(dot)lhy(at)alibaba-inc(dot)com> wrote:
>
> On 2025-09-06 20:31:53 Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> writes:
>
> > After contemplating things for awhile, I think that feichanghong’s
> > idea is the right one after all: in each of the functions that switch
> > into hashtable->tempcxt, let's do a reset on the way out, as attached.
> > That's straightforward and visibly related to the required data
> > lifespan.
>
> I have considered this approach as well, but my concern is that "tempcxt"
> is not always an independent memory context. In some cases, it references
> another context — for example, in nodeSetOp.c’s "build_hash_table", “tempcxt"
> points to "setopstate->ps.ps_ExprContext->ecxt_per_tuple_memory". There is
> similar usage in nodeAgg.c as well. I’m not entirely sure that this approach would
> not discard data we still need, because the lifespan of
> "ps_ExprContext->ecxt_per_tuple_memory" seems to be longer than “tempcxt”.

Yes, I agree with that.

> Should we make tempcxt a completely independent memory context?

It looks fine. Perhaps we don't need to pass tempcxt to BuildTupleHashTable,
but rather create a new one within it. After each switch to tempcxt, we should
clean it up using MemoryContextReset.

Best Regards,
Fei Changhong

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Katja Henke 2025-09-07 15:34:46 Re: BUG #19034: Recursive function with sql_body can replace an existing function but can not be created on it's own
Previous Message 李海洋 (陌痕) 2025-09-07 08:24:05 回复:回复:BUG #19040: Memory leak in hashed subplan node due to missing hashtempcxt reset