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 1uw6la-0066Ys-2U for pgsql-bugs@arkaria.postgresql.org; Tue, 09 Sep 2025 22:24:46 +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 1uw6lZ-00FBlI-6F for pgsql-bugs@arkaria.postgresql.org; Tue, 09 Sep 2025 22:24:45 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uw6lY-00FBl8-UI for pgsql-bugs@lists.postgresql.org; Tue, 09 Sep 2025 22:24:45 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uw6lV-001b7Z-2J for pgsql-bugs@lists.postgresql.org; Tue, 09 Sep 2025 22:24:45 +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 589MOM5X1239242; Tue, 9 Sep 2025 18:24:22 -0400 From: Tom Lane To: "=?UTF-8?B?5p2O5rW35rSLKOmZjOeXlSk=?=" cc: "feichanghong" , "ocean_li_996" , "pgsql-bugs@lists.postgresql.org" Subject: =?UTF-8?Q?Re:_=E5=9B=9E=E5=A4=8D=EF=BC=9ABUG_#19040:_Memory_leak_?= =?UTF-8?Q?in_hashed_subplan_node_due_to_missing_hashtempcxt_reset?= In-reply-to: <68ba6c1d-40a8-4aa5-aec1-f0421455bcf5.mohen.lhy@alibaba-inc.com> 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> <507583.1757352192@sss.pgh.pa.us> <721997.1757364370@sss.pgh.pa.us> ,<1073615.1757387109@sss.pgh.pa.us> <68ba6c1d-40a8-4aa5-aec1-f0421455bcf5.mohen.lhy@alibaba-inc.com> Comments: In-reply-to "=?UTF-8?B?5p2O5rW35rSLKOmZjOeXlSk=?=" message dated "Tue, 09 Sep 2025 15:30:48 +0800" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <1239240.1757456662.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 09 Sep 2025 18:24:22 -0400 Message-ID: <1239241.1757456662@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "=3D?UTF-8?B?5p2O5rW35rSLKOmZjOeXlSk=3D?=3D" w= rites: > For v04-0002, I checked the commit history, and before commit 133924e1, > buildSubPlanHash was using ecxt_per_tuple_memory. It seems that the > "Subplan HashTable Temp Context" was introduced in order to fix a certai= n bug. You're quite right that 0002 looks suspiciously like it's reverting 133924e1. However, it doesn't cause the test case added by that commit to fail (even under Valgrind). I think the reason is what you say next: > However, the changed behavior of TupleHashTableMatch introduced by commi= t > bf6c614a (noted in [1]) may make the condition=EF=BC=9A > ``` > However, the hashtable routines feel free to reset their temp context at= any time, = > which'd lead to destroying input data that was still needed. > ``` > no longer holds true. Then, the lifespan of tempcxt in buildHashtable i= s similar > to that of innercontext->ecxt_per_tuple_memory, so it makes sense to mer= ge the two, > I think. Looking around in 133924e1^, the only place in execGrouping.c that would reset the hashtable's tempcxt was execTuplesMatch (which was called by TupleHashTableMatch). But bf6c614a removed execTuplesMatch. The modern execGrouping.c code resets hashtable->tempcxt nowhere. Instead, TupleHashTableMatch applies ExecQualAndReset to hashtable->exprcontext, so that what is reset is the ecxt_per_tuple_memory of that econtext --- but that's manufactured by CreateStandaloneExprContext in BuildTupleHashTable, and has no connection at all to any context that nodeSubplan.c will touch. It is certainly not the same ecxt_per_tuple_memory that pre-133924e1 was resetting. So basically I'm saying that bf6c614a made it okay to revert 133924e1. > BTW, I ran the test case supported in commit 133924e1 on version not con= tained commit > 133924e1 (tag REL8_0_26). I did not find any problems. But i can not fin= d more information > about this issue. = Digging in the archives, I found the discussion leading to 133924e1 at https://www.postgresql.org/message-id/flat/i2jnbo%241lcj%241%40news.hub.or= g As for trying it on 8.0.26, the commit message for 133924e1 says specifically that the problem isn't there pre-8.1. I did try to duplicate your test using 133924e1^, and it didn't fail for me either. But I'm not too excited about that, because building PG versions this old on modern platforms is really hard. I had to compile with -O0 to get a build that worked at all, and that's already a significant difference from the code we would have been testing back in 2010. It may be that the reason for non-reproduction is buried somewhere in that, or in the different compiler toolchain. I'm not sure it's worth a lot of effort to dig deeply into the details. regards, tom lane