Skip to content

Commit 1608902

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 f31c1e3 commit 1608902

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
@@ -2250,6 +2250,8 @@ asyncQueueAdvanceTail(void)
22502250
static void
22512251
ProcessIncomingNotify(bool flush)
22522252
{
2253+
MemoryContext oldcontext;
2254+
22532255
/* We *must* reset the flag */
22542256
notifyInterruptPending = false;
22552257

@@ -2264,14 +2266,21 @@ ProcessIncomingNotify(bool flush)
22642266

22652267
/*
22662268
* We must run asyncQueueReadAllNotifications inside a transaction, else
2267-
* bad things happen if it gets an error.
2269+
* bad things happen if it gets an error. However, we need to preserve
2270+
* the caller's memory context (typically MessageContext).
22682271
*/
2272+
oldcontext = CurrentMemoryContext;
2273+
22692274
StartTransactionCommand();
22702275

22712276
asyncQueueReadAllNotifications();
22722277

22732278
CommitTransactionCommand();
22742279

2280+
/* Caller's context had better not have been transaction-local */
2281+
Assert(MemoryContextIsValid(oldcontext));
2282+
MemoryContextSwitchTo(oldcontext);
2283+
22752284
/*
22762285
* If this isn't an end-of-command case, we must flush the notify messages
22772286
* 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)