Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5f86cd7

Browse files
committed
Preserve CurrentMemoryContext across notify and sinval interrupts.
ProcessIncomingNotify is called from the main processing loop thatnormally runs in MessageContext. That outer-loop code assumes thatwhatever it allocates will be cleaned up when we're done processingthe current client message --- but if we service a notify interrupt,then whatever gets allocated before the next switch intoMessageContext will be permanently leaked in TopMemoryContext,because CommitTransactionCommand sets CurrentMemoryContext toTopMemoryContext. There are observable leaks associated with(at least) encoding conversion of incoming queries and parametersattached to Bind messages.sinval catchup interrupts have a similar problem. There might beothers, but I've not identified any other clear cases.To fix, take care to save and restore CurrentMemoryContext acrossthe 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 thoroughgoingapproach in commit1afe31f.Discussion:https://postgr.es/m/3478884.1718656625@sss.pgh.pa.us
1 parentca30e8b commit5f86cd7

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
@@ -2255,6 +2255,8 @@ asyncQueueAdvanceTail(void)
22552255
staticvoid
22562256
ProcessIncomingNotify(boolflush)
22572257
{
2258+
MemoryContextoldcontext;
2259+
22582260
/* We *must* reset the flag */
22592261
notifyInterruptPending= false;
22602262

@@ -2269,14 +2271,21 @@ ProcessIncomingNotify(bool flush)
22692271

22702272
/*
22712273
* We must run asyncQueueReadAllNotifications inside a transaction, else
2272-
* bad things happen if it gets an error.
2274+
* bad things happen if it gets an error. However, we need to preserve
2275+
* the caller's memory context (typically MessageContext).
22732276
*/
2277+
oldcontext=CurrentMemoryContext;
2278+
22742279
StartTransactionCommand();
22752280

22762281
asyncQueueReadAllNotifications();
22772282

22782283
CommitTransactionCommand();
22792284

2285+
/* Caller's context had better not have been transaction-local */
2286+
Assert(MemoryContextIsValid(oldcontext));
2287+
MemoryContextSwitchTo(oldcontext);
2288+
22802289
/*
22812290
* If this isn't an end-of-command case, we must flush the notify messages
22822291
* 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+
MemoryContextoldcontext=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)

[8]ページ先頭

©2009-2025 Movatter.jp