Memory leak in standby master (gpsyncagent process)
@@ -6136,6 +6136,23 @@ SyncAgentMain(int argc, char *argv[], const char *username)
if (whereToSendOutput == DestDebug)
printf("\nPostgreSQL stand-alone backend %s\n", PG_VERSION);
+ /*
+ * Create the memory context we will use in the main loop.
+ *
+ * MessageContext is reset once per iteration of the main loop, ie, upon
+ * completion of processing of each command message from the client.
+ *
+ * Fix memory leak: HAWQ-1594
+ * SyncAgent doesn't allocate memory through the memory context
+ * other than input buffer, but for the consistency with PostgresMain,
+ * we define MessageContext and switch to it at the beginning of loop,
+ * so that we can create another context in case we need one in the future.
+ */
+ MessageContext = AllocSetContextCreate(TopMemoryContext,
+ "MessageContext",
+ ALLOCSET_DEFAULT_MINSIZE,
+ ALLOCSET_DEFAULT_INITSIZE,
+ ALLOCSET_DEFAULT_MAXSIZE);
/*
* POSTGRES main processing loop begins here
*
@@ -6236,6 +6253,14 @@ SyncAgentMain(int argc, char *argv[], const char *username)
*/
doing_extended_query_message = false;
+ /*
+ * Fix memory leak: HAWQ-1594
+ * Release storage left over from prior query cycle, and create a new
+ * query input buffer in the cleared MessageContext.
+ */
+ MemoryContextSwitchTo(MessageContext);
+ MemoryContextResetAndDeleteChildren(MessageContext);
+
initStringInfo(&input_message);
/*
