Skip to content

Commit 4df767c

Browse files
committed
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 1afe31f. Discussion: https://postgr.es/m/3478884.1718656625@sss.pgh.pa.us
1 parent 458fada commit 4df767c

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/backend/commands/async.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2236,6 +2236,8 @@ asyncQueueAdvanceTail(void)
22362236
static void
22372237
ProcessIncomingNotify(bool flush)
22382238
{
2239+
MemoryContext oldcontext;
2240+
22392241
/* We *must* reset the flag */
22402242
notifyInterruptPending = false;
22412243

@@ -2250,14 +2252,21 @@ ProcessIncomingNotify(bool flush)
22502252

22512253
/*
22522254
* We must run asyncQueueReadAllNotifications inside a transaction, else
2253-
* bad things happen if it gets an error.
2255+
* bad things happen if it gets an error. However, we need to preserve
2256+
* the caller's memory context (typically MessageContext).
22542257
*/
2258+
oldcontext = CurrentMemoryContext;
2259+
22552260
StartTransactionCommand();
22562261

22572262
asyncQueueReadAllNotifications();
22582263

22592264
CommitTransactionCommand();
22602265

2266+
/* Caller's context had better not have been transaction-local */
2267+
Assert(MemoryContextIsValid(oldcontext));
2268+
MemoryContextSwitchTo(oldcontext);
2269+
22612270
/*
22622271
* If this isn't an end-of-command case, we must flush the notify messages
22632272
* to ensure frontend gets them promptly.

src/backend/storage/ipc/sinval.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "access/xact.h"
1818
#include "commands/async.h"
1919
#include "miscadmin.h"
20+
#include "nodes/memnodes.h"
2021
#include "storage/ipc.h"
2122
#include "storage/proc.h"
2223
#include "storage/sinvaladt.h"
@@ -184,6 +185,7 @@ ProcessCatchupInterrupt(void)
184185
* can just call AcceptInvalidationMessages() to do this. If we
185186
* aren't, we start and immediately end a transaction; the call to
186187
* AcceptInvalidationMessages() happens down inside transaction start.
188+
* Be sure to preserve caller's memory context when we do that.
187189
*
188190
* It is awfully tempting to just call AcceptInvalidationMessages()
189191
* without the rest of the xact start/stop overhead, and I think that
@@ -197,9 +199,14 @@ ProcessCatchupInterrupt(void)
197199
}
198200
else
199201
{
202+
MemoryContext oldcontext = CurrentMemoryContext;
203+
200204
elog(DEBUG4, "ProcessCatchupEvent outside transaction");
201205
StartTransactionCommand();
202206
CommitTransactionCommand();
207+
/* Caller's context had better not have been transaction-local */
208+
Assert(MemoryContextIsValid(oldcontext));
209+
MemoryContextSwitchTo(oldcontext);
203210
}
204211
}
205212
}

0 commit comments

Comments
 (0)