Commit 90857b48 authored by Tom Lane's avatar Tom Lane

Preserve caller's memory context in ProcessCompletedNotifies().

This is necessary to avoid long-term memory leakage, because the main loop
in PostgresMain expects to be executing in MessageContext, and hence is a
bit sloppy about freeing stuff that is only needed for the duration of
processing the current client message.  The known case of an actual leak
is when encoding conversion has to be done on the incoming command string,
but there might be others.  Per report from Per-Olov Esgard.

Back-patch to 9.0, where the bug was introduced by the LISTEN/NOTIFY
rewrite.
parent 336db7e3
...@@ -1090,6 +1090,7 @@ Exec_UnlistenAllCommit(void) ...@@ -1090,6 +1090,7 @@ Exec_UnlistenAllCommit(void)
void void
ProcessCompletedNotifies(void) ProcessCompletedNotifies(void)
{ {
MemoryContext caller_context;
bool signalled; bool signalled;
/* Nothing to do if we didn't send any notifications */ /* Nothing to do if we didn't send any notifications */
...@@ -1103,6 +1104,12 @@ ProcessCompletedNotifies(void) ...@@ -1103,6 +1104,12 @@ ProcessCompletedNotifies(void)
*/ */
backendHasSentNotifications = false; backendHasSentNotifications = false;
/*
* We must preserve the caller's memory context (probably MessageContext)
* across the transaction we do here.
*/
caller_context = CurrentMemoryContext;
if (Trace_notify) if (Trace_notify)
elog(DEBUG1, "ProcessCompletedNotifies"); elog(DEBUG1, "ProcessCompletedNotifies");
...@@ -1135,6 +1142,8 @@ ProcessCompletedNotifies(void) ...@@ -1135,6 +1142,8 @@ ProcessCompletedNotifies(void)
CommitTransactionCommand(); CommitTransactionCommand();
MemoryContextSwitchTo(caller_context);
/* We don't need pq_flush() here since postgres.c will do one shortly */ /* We don't need pq_flush() here since postgres.c will do one shortly */
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment