Re: BUG #19040: Memory leak in hashed subplan node due to missing hashtempcxt reset
От | Tom Lane |
---|---|
Тема | Re: BUG #19040: Memory leak in hashed subplan node due to missing hashtempcxt reset |
Дата | |
Msg-id | 507583.1757352192@sss.pgh.pa.us обсуждение исходный текст |
Ответ на | Re: BUG #19040: Memory leak in hashed subplan node due to missing hashtempcxt reset (feichanghong <feichanghong@qq.com>) |
Ответы |
Re: BUG #19040: Memory leak in hashed subplan node due to missing hashtempcxt reset
|
Список | pgsql-bugs |
feichanghong <feichanghong@qq.com> writes: >> On Sep 7, 2025, at 16:24, 李海洋(陌痕) <mohen.lhy@alibaba-inc.com> wrote: >> On 2025-09-06 20:31:53 Tom Lane <tgl@sss.pgh.pa.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. >> 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. 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 BuildTupleHashTable, > 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
В списке pgsql-bugs по дате отправления: