Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uvfaX-00H4fr-AV for pgsql-bugs@arkaria.postgresql.org; Mon, 08 Sep 2025 17:23:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1uvfaW-006SeC-BJ for pgsql-bugs@arkaria.postgresql.org; Mon, 08 Sep 2025 17:23:32 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uvfaW-006Sdi-30 for pgsql-bugs@lists.postgresql.org; Mon, 08 Sep 2025 17:23:32 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uvfaU-001C8n-1K for pgsql-bugs@lists.postgresql.org; Mon, 08 Sep 2025 17:23:31 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 588HNCLx507584; Mon, 8 Sep 2025 13:23:14 -0400 From: Tom Lane To: feichanghong cc: =?utf-8?B?Iuadjua1t+a0iyjpmYznl5UpIg==?= , ocean_li_996 , "pgsql-bugs@lists.postgresql.org" Subject: Re: BUG #19040: Memory leak in hashed subplan node due to missing hashtempcxt reset In-reply-to: References: <19040-c9b6073ef814f48c@postgresql.org> <58062871.112.1990d3e06ea.Coremail.ocean_li_996@163.com> <808767.1756913726@sss.pgh.pa.us> <160523.1757190713@sss.pgh.pa.us> Comments: In-reply-to feichanghong message dated "Sun, 07 Sep 2025 17:02:23 +0800" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <507582.1757352192.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 08 Sep 2025 13:23:12 -0400 Message-ID: <507583.1757352192@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk feichanghong writes: >> On Sep 7, 2025, at 16:24, =E6=9D=8E=E6=B5=B7=E6=B4=8B(=E9=99=8C=E7=97=95= ) wrote: >> On 2025-09-06 20:31:53 Tom Lane writes: >>> After contemplating things for awhile, I think that feichanghong=E2=80= =99s >>> 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. >> I have considered this approach as well, but my concern is that "tempcx= t" >> is not always an independent memory context. In some cases, it referenc= es >> another context =E2=80=94 for example, in nodeSetOp.c=E2=80=99s "build_= hash_table", =E2=80=9Ctempcxt" >> points to "setopstate->ps.ps_ExprContext->ecxt_per_tuple_memory". There= is >> similar usage in nodeAgg.c as well. I=E2=80=99m 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 =E2=80=9C= tempcxt=E2=80=9D. > Yes, I agree with that. Yeah, that is a fair point. The existing API is that the caller is responsible for resetting tempcxt sufficiently often, and it looks like nodeSubplan.c is the only place that gets this wrong. Let's just fix nodeSubplan.c, add a comment documenting this requirement, and call it good. >> Should we make tempcxt a completely independent memory context? > It looks fine. Perhaps we don't need to pass tempcxt to BuildTupleHashTa= ble, > but rather create a new one within it. After each switch to tempcxt, we = should > clean it up using MemoryContextReset. I thought about that too, but that would result in two short-lived contexts and two reset operations per tuple cycle where only one is needed. I'm rather tempted to fix nodeSubplan.c by making it use innerecontext->ecxt_per_tuple_memory instead of a separate hash tmpcontext. That context is getting reset already, at least in buildSubPlanHash(). That's probably too risky for the back branches but we could do it in HEAD. regards, tom lane