Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf0f9ed6

Browse files
committed
Fix ancient memory leak in contrib/auto_explain.
The ExecutorEnd hook is invoked in a context that could be quitelong-lived, not the executor's own per-query context as I thinkwe were sort of assuming. Thus, any cruft generated while producingthe EXPLAIN output could accumulate over multiple queries. This canresult in spectacular leakage if log_nested_statements is on, andeven without that I'm surprised nobody complained before.To fix, just switch into the executor's context so that anything weallocate will be released when standard_ExecutorEnd frees the executorstate. We might as well nuke the code's retail pfree of the explainoutput string, too; that's laughably inadequate to the need.Japin Li, per report from Jeff Janes. This bug is old, soback-patch to all supported branches.Discussion:https://postgr.es/m/CAMkU=1wCVtbeRn0s9gt12KwQ7PLXovbpM8eg25SYocKW3BT4hg@mail.gmail.com
1 parent77a0e14 commitf0f9ed6

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

‎contrib/auto_explain/auto_explain.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,15 @@ explain_ExecutorEnd(QueryDesc *queryDesc)
282282
{
283283
if (queryDesc->totaltime&&auto_explain_enabled())
284284
{
285+
MemoryContextoldcxt;
285286
doublemsec;
286287

288+
/*
289+
* Make sure we operate in the per-query context, so any cruft will be
290+
* discarded later during ExecutorEnd.
291+
*/
292+
oldcxt=MemoryContextSwitchTo(queryDesc->estate->es_query_cxt);
293+
287294
/*
288295
* Make sure stats accumulation is done. (Note: it's okay if several
289296
* levels of hook all do this.)
@@ -331,9 +338,9 @@ explain_ExecutorEnd(QueryDesc *queryDesc)
331338
(errmsg("duration: %.3f ms plan:\n%s",
332339
msec,es->str->data),
333340
errhidestmt(true)));
334-
335-
pfree(es->str->data);
336341
}
342+
343+
MemoryContextSwitchTo(oldcxt);
337344
}
338345

339346
if (prev_ExecutorEnd)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp