From ed076d5ccc03dbf47218b201f8c6c52df51e8027 Mon Sep 17 00:00:00 2001 From: Petr Jelinek Date: Fri, 20 Jan 2017 22:22:53 +0100 Subject: [PATCH 3/3] Always initialize stringinfo buffers in walsender --- src/backend/replication/walsender.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c index f3082c3..a6e2655 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -149,6 +149,9 @@ static StringInfoData output_message; static StringInfoData reply_message; static StringInfoData tmpbuf; +/* Did we init above buffers? */ +static bool initialized_buffers = false; + /* * Timestamp of the last receipt of the reply from the standby. Set to 0 if * wal_sender_timeout doesn't need to be active. @@ -816,8 +819,6 @@ CreateReplicationSlot(CreateReplicationSlotCmd *cmd) cmd->temporary ? RS_TEMPORARY : RS_EPHEMERAL); } - initStringInfo(&output_message); - if (cmd->kind == REPLICATION_KIND_LOGICAL) { LogicalDecodingContext *ctx; @@ -1310,6 +1311,18 @@ exec_replication_command(const char *cmd_string) (errmsg("received replication command: %s", cmd_string))); /* + * Allocate buffers that will be used for each outgoing and incoming + * message. Only do this once to avoid leaking memory. + */ + if (!initialized_buffers) + { + initStringInfo(&output_message); + initStringInfo(&reply_message); + initStringInfo(&tmpbuf); + initialized_buffers = true; + } + + /* * CREATE_REPLICATION_SLOT ... LOGICAL exports a snapshot until the next * command arrives. Clean up the old stuff if there's anything. */ @@ -1802,14 +1815,6 @@ static void WalSndLoop(WalSndSendDataCallback send_data) { /* - * Allocate buffers that will be used for each outgoing and incoming - * message. We do this just once to reduce palloc overhead. - */ - initStringInfo(&output_message); - initStringInfo(&reply_message); - initStringInfo(&tmpbuf); - - /* * Initialize the last reply timestamp. That enables timeout processing * from hereon. */ -- 2.7.4