aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2011-05-27 12:10:32 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2011-05-27 12:10:32 -0400
commit90857b48e1f69dbca52f498bd444190d36dbd73f (patch)
treefffdeea9752230f56a77c0639f259edb3c7b9e66 /src
parent336db7e3474508b46a0e6bdb013a8bee5aac9adf (diff)
downloadpostgresql-90857b48e1f69dbca52f498bd444190d36dbd73f.tar.gz
postgresql-90857b48e1f69dbca52f498bd444190d36dbd73f.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/backend/commands/async.c9
1 files changed, 9 insertions, 0 deletions
diff --git a/src/backend/commands/async.c b/src/backend/commands/async.c
index 588e9f2b85d..02f8f9cd635 100644
--- a/src/backend/commands/async.c
+++ b/src/backend/commands/async.c
@@ -1090,6 +1090,7 @@ Exec_UnlistenAllCommit(void)
void
ProcessCompletedNotifies(void)
{
+ MemoryContext caller_context;
bool signalled;
/* Nothing to do if we didn't send any notifications */
@@ -1103,6 +1104,12 @@ ProcessCompletedNotifies(void)
*/
backendHasSentNotifications = false;
+ /*
+ * We must preserve the caller's memory context (probably MessageContext)
+ * across the transaction we do here.
+ */
+ caller_context = CurrentMemoryContext;
+
if (Trace_notify)
elog(DEBUG1, "ProcessCompletedNotifies");
@@ -1135,6 +1142,8 @@ ProcessCompletedNotifies(void)
CommitTransactionCommand();
+ MemoryContextSwitchTo(caller_context);
+
/* We don't need pq_flush() here since postgres.c will do one shortly */
}