Skip to content

Commit 7428469

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

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
@@ -361,8 +361,15 @@ explain_ExecutorEnd(QueryDesc *queryDesc)
361361
{
362362
if (queryDesc->totaltime && auto_explain_enabled())
363363
{
364+
MemoryContext oldcxt;
364365
double msec;
365366

367+
/*
368+
* Make sure we operate in the per-query context, so any cruft will be
369+
* discarded later during ExecutorEnd.
370+
*/
371+
oldcxt = MemoryContextSwitchTo(queryDesc->estate->es_query_cxt);
372+
366373
/*
367374
* Make sure stats accumulation is done. (Note: it's okay if several
368375
* levels of hook all do this.)
@@ -413,9 +420,9 @@ explain_ExecutorEnd(QueryDesc *queryDesc)
413420
(errmsg("duration: %.3f ms plan:\n%s",
414421
msec, es->str->data),
415422
errhidestmt(true)));
416-
417-
pfree(es->str->data);
418423
}
424+
425+
MemoryContextSwitchTo(oldcxt);
419426
}
420427

421428
if (prev_ExecutorEnd)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy