aboutsummaryrefslogtreecommitdiff
path: root/src/backend/commands/async.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2024-07-01 12:21:07 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2024-07-01 12:21:07 -0400
commit1608902fc3a73b598dbc09922e877aee5da55cf5 (patch)
tree026b003af110bcb4ea406988000dc178589a622b /src/backend/commands/async.c
parentf31c1e34bc801b57bbc3108725f13b793d7c4a4c (diff)
downloadpostgresql-1608902fc3a73b598dbc09922e877aee5da55cf5.tar.gz
postgresql-1608902fc3a73b598dbc09922e877aee5da55cf5.zip
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/commands/async.c')
-rw-r--r--src/backend/commands/async.c11
1 files changed, 10 insertions, 1 deletions
diff --git a/src/backend/commands/async.c b/src/backend/commands/async.c
index 409fece1e06..fff1aad83ed 100644
--- a/src/backend/commands/async.c
+++ b/src/backend/commands/async.c
@@ -2250,6 +2250,8 @@ asyncQueueAdvanceTail(void)
static void
ProcessIncomingNotify(bool flush)
{
+ MemoryContext oldcontext;
+
/* We *must* reset the flag */
notifyInterruptPending = false;
@@ -2264,14 +2266,21 @@ ProcessIncomingNotify(bool flush)
/*
* We must run asyncQueueReadAllNotifications inside a transaction, else
- * bad things happen if it gets an error.
+ * bad things happen if it gets an error. However, we need to preserve
+ * the caller's memory context (typically MessageContext).
*/
+ oldcontext = CurrentMemoryContext;
+
StartTransactionCommand();
asyncQueueReadAllNotifications();
CommitTransactionCommand();
+ /* Caller's context had better not have been transaction-local */
+ Assert(MemoryContextIsValid(oldcontext));
+ MemoryContextSwitchTo(oldcontext);
+
/*
* If this isn't an end-of-command case, we must flush the notify messages
* to ensure frontend gets them promptly.