Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ac7d6f5

Browse files
committed
Make use of initReadOnlyStringInfo() in more places
f0efa5a introduced the concept of "read-only" StringInfos which makes use of an existing, possibly not NUL terminated, buffer. Here we adjust two places that make use of StringInfos to receive data to avoid using appendBinaryStringInfo() in cases where a NUL termination character is not required. This saves a possible palloc() and saves having to needlessly memcpy() from one buffer to another. Here we adjust two places which were using appendBinaryStringInfo(). Neither of these cases seem particularly performance-critical. In the case of XLogWalRcvProcessMsg(), the appendBinaryStringInfo() was only appending 24 bytes. The change made here does mean that we can get rid of the incoming_message global variable and make that local instead. The apply_spooled_messages() case applies in logical decoding when applying (possibly large) changes which have been serialized to a file. Reviewed-by: Amit Kapila Discussion: https://postgr.es/m/CAApHDvoxYUDHwqPf-ShvchsERf1RzmkGoLwg63JNvHCkDCuyKQ@mail.gmail.com
1 parent 18b5851 commit ac7d6f5

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/backend/replication/logical/worker.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,7 +2019,6 @@ void
20192019
apply_spooled_messages(FileSet *stream_fileset, TransactionId xid,
20202020
XLogRecPtr lsn)
20212021
{
2022-
StringInfoData s2;
20232022
int nchanges;
20242023
char path[MAXPGPATH];
20252024
char *buffer = NULL;
@@ -2057,7 +2056,6 @@ apply_spooled_messages(FileSet *stream_fileset, TransactionId xid,
20572056
CurrentResourceOwner = oldowner;
20582057

20592058
buffer = palloc(BLCKSZ);
2060-
initStringInfo(&s2);
20612059

20622060
MemoryContextSwitchTo(oldcxt);
20632061

@@ -2079,6 +2077,7 @@ apply_spooled_messages(FileSet *stream_fileset, TransactionId xid,
20792077
nchanges = 0;
20802078
while (true)
20812079
{
2080+
StringInfoData s2;
20822081
size_t nbytes;
20832082
int len;
20842083

@@ -2104,9 +2103,8 @@ apply_spooled_messages(FileSet *stream_fileset, TransactionId xid,
21042103

21052104
BufFileTell(stream_fd, &fileno, &offset);
21062105

2107-
/* copy the buffer to the stringinfo and call apply_dispatch */
2108-
resetStringInfo(&s2);
2109-
appendBinaryStringInfo(&s2, buffer, len);
2106+
/* init a stringinfo using the buffer and call apply_dispatch */
2107+
initReadOnlyStringInfo(&s2, buffer, len);
21102108

21112109
/* Ensure we are reading the data into our memory context. */
21122110
oldcxt = MemoryContextSwitchTo(ApplyMessageContext);

src/backend/replication/walreceiver.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ typedef enum WalRcvWakeupReason
132132
static TimestampTz wakeup[NUM_WALRCV_WAKEUPS];
133133

134134
static StringInfoData reply_message;
135-
static StringInfoData incoming_message;
136135

137136
/* Prototypes for private functions */
138137
static void WalRcvFetchTimeLineHistoryFiles(TimeLineID first, TimeLineID last);
@@ -425,7 +424,6 @@ WalReceiverMain(void)
425424
/* Initialize LogstreamResult and buffers for processing messages */
426425
LogstreamResult.Write = LogstreamResult.Flush = GetXLogReplayRecPtr(NULL);
427426
initStringInfo(&reply_message);
428-
initStringInfo(&incoming_message);
429427

430428
/* Initialize nap wakeup times. */
431429
now = GetCurrentTimestamp();
@@ -843,19 +841,20 @@ XLogWalRcvProcessMsg(unsigned char type, char *buf, Size len, TimeLineID tli)
843841
TimestampTz sendTime;
844842
bool replyRequested;
845843

846-
resetStringInfo(&incoming_message);
847-
848844
switch (type)
849845
{
850846
case 'w': /* WAL records */
851847
{
852-
/* copy message to StringInfo */
848+
StringInfoData incoming_message;
849+
853850
hdrlen = sizeof(int64) + sizeof(int64) + sizeof(int64);
854851
if (len < hdrlen)
855852
ereport(ERROR,
856853
(errcode(ERRCODE_PROTOCOL_VIOLATION),
857854
errmsg_internal("invalid WAL message received from primary")));
858-
appendBinaryStringInfo(&incoming_message, buf, hdrlen);
855+
856+
/* initialize a StringInfo with the given buffer */
857+
initReadOnlyStringInfo(&incoming_message, buf, hdrlen);
859858

860859
/* read the fields */
861860
dataStart = pq_getmsgint64(&incoming_message);
@@ -870,13 +869,16 @@ XLogWalRcvProcessMsg(unsigned char type, char *buf, Size len, TimeLineID tli)
870869
}
871870
case 'k': /* Keepalive */
872871
{
873-
/* copy message to StringInfo */
872+
StringInfoData incoming_message;
873+
874874
hdrlen = sizeof(int64) + sizeof(int64) + sizeof(char);
875875
if (len != hdrlen)
876876
ereport(ERROR,
877877
(errcode(ERRCODE_PROTOCOL_VIOLATION),
878878
errmsg_internal("invalid keepalive message received from primary")));
879-
appendBinaryStringInfo(&incoming_message, buf, hdrlen);
879+
880+
/* initialize a StringInfo with the given buffer */
881+
initReadOnlyStringInfo(&incoming_message, buf, hdrlen);
880882

881883
/* read the fields */
882884
walEnd = pq_getmsgint64(&incoming_message);

0 commit comments

Comments
 (0)