From 0b31533674adb90644298c638cc7dfe066ab5bd6 Mon Sep 17 00:00:00 2001 From: David Rowley Date: Sun, 12 Feb 2023 16:45:15 +1300 Subject: [PATCH v1 2/2] Add more usages of initStringInfoFromString This slightly reduces some memcpy work in walreceiver.c --- src/backend/replication/walreceiver.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c index f6446da2d6..366b91f923 100644 --- a/src/backend/replication/walreceiver.c +++ b/src/backend/replication/walreceiver.c @@ -132,7 +132,6 @@ typedef enum WalRcvWakeupReason static TimestampTz wakeup[NUM_WALRCV_WAKEUPS]; static StringInfoData reply_message; -static StringInfoData incoming_message; /* Prototypes for private functions */ static void WalRcvFetchTimeLineHistoryFiles(TimeLineID first, TimeLineID last); @@ -422,10 +421,12 @@ WalReceiverMain(void) LSN_FORMAT_ARGS(startpoint), startpointTLI))); first_stream = false; - /* Initialize LogstreamResult and buffers for processing messages */ + /* + * Initialize LogstreamResult and reply buffer for processing + * messages + */ LogstreamResult.Write = LogstreamResult.Flush = GetXLogReplayRecPtr(NULL); initStringInfo(&reply_message); - initStringInfo(&incoming_message); /* Initialize nap wakeup times. */ now = GetCurrentTimestamp(); @@ -843,19 +844,19 @@ XLogWalRcvProcessMsg(unsigned char type, char *buf, Size len, TimeLineID tli) TimestampTz sendTime; bool replyRequested; - resetStringInfo(&incoming_message); - switch (type) { case 'w': /* WAL records */ { - /* copy message to StringInfo */ + StringInfoData incoming_message; + + /* set the message in the incoming_message StringInfo */ hdrlen = sizeof(int64) + sizeof(int64) + sizeof(int64); if (len < hdrlen) ereport(ERROR, (errcode(ERRCODE_PROTOCOL_VIOLATION), errmsg_internal("invalid WAL message received from primary"))); - appendBinaryStringInfo(&incoming_message, buf, hdrlen); + initStringInfoFromString(&incoming_message, buf, hdrlen); /* read the fields */ dataStart = pq_getmsgint64(&incoming_message); @@ -870,13 +871,15 @@ XLogWalRcvProcessMsg(unsigned char type, char *buf, Size len, TimeLineID tli) } case 'k': /* Keepalive */ { - /* copy message to StringInfo */ + StringInfoData incoming_message; + + /* set the message in the incoming_message StringInfo */ hdrlen = sizeof(int64) + sizeof(int64) + sizeof(char); if (len != hdrlen) ereport(ERROR, (errcode(ERRCODE_PROTOCOL_VIOLATION), errmsg_internal("invalid keepalive message received from primary"))); - appendBinaryStringInfo(&incoming_message, buf, hdrlen); + initStringInfoFromString(&incoming_message, buf, hdrlen); /* read the fields */ walEnd = pq_getmsgint64(&incoming_message); -- 2.37.2