diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2024-07-01 12:21:07 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2024-07-01 12:21:07 -0400 |
commit | 31f8d620b287258a34a35c25868896a46deea14f (patch) | |
tree | 4d859897b1b52a06ede977c60b9bfb3a85b9e82e /src/backend/storage/ipc/sinval.c | |
parent | 6d2ac554911d874e4d0609f5b2c5f34a1226ee0c (diff) |
Preserve CurrentMemoryContext across notify and sinval interrupts.
ProcessIncomingNotify is called from the main processing loop that
normally runs in MessageContext. That outer-loop code assumes that
whatever it allocates will be cleaned up when we're done processing
the current client message --- but if we service a notify interrupt,
then whatever gets allocated before the next switch into
MessageContext will be permanently leaked in TopMemoryContext,
because CommitTransactionCommand sets CurrentMemoryContext to
TopMemoryContext. There are observable leaks associated with
(at least) encoding conversion of incoming queries and parameters
attached to Bind messages.
sinval catchup interrupts have a similar problem. There might be
others, but I've not identified any other clear cases.
To fix, take care to save and restore CurrentMemoryContext across
the Start/CommitTransactionCommand calls in these functions.
Per bug #18512 from wizardbrony. Commit to back branches only;
in HEAD, this was dealt with by the riskier but more thoroughgoing
approach in commit 1afe31f03.
Discussion: https://postgr.es/m/3478884.1718656625@sss.pgh.pa.us
Diffstat (limited to 'src/backend/storage/ipc/sinval.c')
-rw-r--r-- | src/backend/storage/ipc/sinval.c | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/src/backend/storage/ipc/sinval.c b/src/backend/storage/ipc/sinval.c index d9b16f84d19..4e69015fe4d 100644 --- a/src/backend/storage/ipc/sinval.c +++ b/src/backend/storage/ipc/sinval.c @@ -16,6 +16,7 @@ #include "access/xact.h" #include "miscadmin.h" +#include "nodes/memnodes.h" #include "storage/latch.h" #include "storage/sinvaladt.h" #include "utils/inval.h" @@ -182,6 +183,7 @@ ProcessCatchupInterrupt(void) * can just call AcceptInvalidationMessages() to do this. If we * aren't, we start and immediately end a transaction; the call to * AcceptInvalidationMessages() happens down inside transaction start. + * Be sure to preserve caller's memory context when we do that. * * It is awfully tempting to just call AcceptInvalidationMessages() * without the rest of the xact start/stop overhead, and I think that @@ -195,9 +197,14 @@ ProcessCatchupInterrupt(void) } else { + MemoryContext oldcontext = CurrentMemoryContext; + elog(DEBUG4, "ProcessCatchupEvent outside transaction"); StartTransactionCommand(); CommitTransactionCommand(); + /* Caller's context had better not have been transaction-local */ + Assert(MemoryContextIsValid(oldcontext)); + MemoryContextSwitchTo(oldcontext); } } } |