Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 31f8d62

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 6d2ac55 commit 31f8d62

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
@@ -2182,6 +2182,8 @@ asyncQueueAdvanceTail(void)
21822182
static void
21832183
ProcessIncomingNotify(bool flush)
21842184
{
2185+
MemoryContext oldcontext;
2186+
21852187
/* We *must* reset the flag */
21862188
notifyInterruptPending = false;
21872189

@@ -2196,14 +2198,21 @@ ProcessIncomingNotify(bool flush)
21962198

21972199
/*
21982200
* We must run asyncQueueReadAllNotifications inside a transaction, else
2199-
* bad things happen if it gets an error.
2201+
* bad things happen if it gets an error. However, we need to preserve
2202+
* the caller's memory context (typically MessageContext).
22002203
*/
2204+
oldcontext = CurrentMemoryContext;
2205+
22012206
StartTransactionCommand();
22022207

22032208
asyncQueueReadAllNotifications();
22042209

22052210
CommitTransactionCommand();
22062211

2212+
/* Caller's context had better not have been transaction-local */
2213+
Assert(MemoryContextIsValid(oldcontext));
2214+
MemoryContextSwitchTo(oldcontext);
2215+
22072216
/*
22082217
* If this isn't an end-of-command case, we must flush the notify messages
22092218
* 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
@@ -16,6 +16,7 @@
1616

1717
#include "access/xact.h"
1818
#include "miscadmin.h"
19+
#include "nodes/memnodes.h"
1920
#include "storage/latch.h"
2021
#include "storage/sinvaladt.h"
2122
#include "utils/inval.h"
@@ -182,6 +183,7 @@ ProcessCatchupInterrupt(void)
182183
* can just call AcceptInvalidationMessages() to do this. If we
183184
* aren't, we start and immediately end a transaction; the call to
184185
* AcceptInvalidationMessages() happens down inside transaction start.
186+
* Be sure to preserve caller's memory context when we do that.
185187
*
186188
* It is awfully tempting to just call AcceptInvalidationMessages()
187189
* without the rest of the xact start/stop overhead, and I think that
@@ -195,9 +197,14 @@ ProcessCatchupInterrupt(void)
195197
}
196198
else
197199
{
200+
MemoryContext oldcontext = CurrentMemoryContext;
201+
198202
elog(DEBUG4, "ProcessCatchupEvent outside transaction");
199203
StartTransactionCommand();
200204
CommitTransactionCommand();
205+
/* Caller's context had better not have been transaction-local */
206+
Assert(MemoryContextIsValid(oldcontext));
207+
MemoryContextSwitchTo(oldcontext);
201208
}
202209
}
203210
}

0 commit comments

Comments
 (0)