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

From: 李海洋(陌痕) <mohen(dot)lhy(at)alibaba-inc(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "feichanghong" <feichanghong(at)qq(dot)com>
Cc: "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: 回复:BUG #19040: Memory leak in hashed subplan node due to missing hashtempcxt reset
Date: 2025-09-05 05:13:20
Message-ID: c765ad39-39eb-4a39-8e67-91bcb9ee65e0.mohen.lhy@alibaba-inc.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On 2025-09-03 23:35 Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> writes:
> I wonder why ExecInitSubPlan is making a separate context for this at all,
> rather than using some surrounding short-lived context.
This behavior was introduced by commit 133924e to fix one bug. AFAICS, the
tempcxt is only used by hashfunc evaluation. We should reset tempcxt after per
hashtable find if tempcxt is a separate context.
> If we do keep a separate context, I agree with the idea of one
> MemoryContextReset in the exit of ExecHashSubPlan, but the proposed
> patch seems like a mess. I think what we ought to do is refactor
> ExecHashSubPlan so that there's exactly one "ExecClearTuple(slot)”
> down at the bottom, and then we can add a MemoryContextReset after it.
Based on this thought, I have implemented a new patch in attachment.
It’s worth noting that a similar issue also exists in buildSubPlanHash. The leak
occurs while building the hash table from the subplan’s result set. If the test SQL
in [1] is modified to
```
EXPLAIN ANALYZE
SELECT * FROM test1
WHERE a NOT IN
(SELECT a FROM test1 limit 100000);
```
(and work_mem is adjusted so that the plan uses a hashed subplan), you can
confirm it. To address this, I have also added a MemoryContextReset tempcxt
after each hash table probe in buildSubPlanHash.
Additional, the patch does not include a test case.
Looking forward to your feedback.

Thanks
Haiyang Li
[1] https://www.postgresql.org/message-id/19040-c9b6073ef814f48c%40postgresql.org <https://www.postgresql.org/message-id/19040-c9b6073ef814f48c%40postgresql.org >

Attachment Content-Type Size
v02_fix_memory_leak_in_hashed_subplan_node.patch application/octet-stream 2.4 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2025-09-05 06:28:15 BUG #19043: jdbc connection url is not case insensitive
Previous Message Alexander Lakhin 2025-09-05 04:00:00 Re: BUG #19037: Planner fails on estimating array length with "no relation entry" error