Skip to content

Commit 4909cb5

Browse files
committed
Fix integer overflow in debug message of walreceiver
The message tries to tell the replication apply delay which fails if the first WAL record is not applied yet. Fix is, instead of telling overflowed minus numeric, showing "N/A" which indicates that the delay data is not yet available. Problem reported by me and patch by Fabrízio de Royes Mello. Back patched to 9.4, 9.3 and 9.2 stable branches (9.1 and 9.0 do not have the debug message).
1 parent 590fc5d commit 4909cb5

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/backend/replication/walreceiver.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -793,15 +793,26 @@ ProcessWalSndrMessage(XLogRecPtr walEnd, TimestampTz sendTime)
793793
{
794794
char *sendtime;
795795
char *receipttime;
796+
int applyDelay;
796797

797798
/* Copy because timestamptz_to_str returns a static buffer */
798799
sendtime = pstrdup(timestamptz_to_str(sendTime));
799800
receipttime = pstrdup(timestamptz_to_str(lastMsgReceiptTime));
800-
elog(DEBUG2, "sendtime %s receipttime %s replication apply delay %d ms transfer latency %d ms",
801-
sendtime,
802-
receipttime,
803-
GetReplicationApplyDelay(),
804-
GetReplicationTransferLatency());
801+
applyDelay = GetReplicationApplyDelay();
802+
803+
/* apply delay is not available */
804+
if (applyDelay == -1)
805+
elog(DEBUG2, "sendtime %s receipttime %s replication apply delay (N/A) transfer latency %d ms",
806+
sendtime,
807+
receipttime,
808+
GetReplicationTransferLatency());
809+
else
810+
elog(DEBUG2, "sendtime %s receipttime %s replication apply delay %d ms transfer latency %d ms",
811+
sendtime,
812+
receipttime,
813+
applyDelay,
814+
GetReplicationTransferLatency());
815+
805816
pfree(sendtime);
806817
pfree(receipttime);
807818
}

src/backend/replication/walreceiverfuncs.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ GetWalRcvWriteRecPtr(XLogRecPtr *latestChunkStart)
241241
}
242242

243243
/*
244-
* Returns the replication apply delay in ms
244+
* Returns the replication apply delay in ms or -1
245+
* if the apply delay info is not available
245246
*/
246247
int
247248
GetReplicationApplyDelay(void)
@@ -255,6 +256,8 @@ GetReplicationApplyDelay(void)
255256
long secs;
256257
int usecs;
257258

259+
TimestampTz chunckReplayStartTime;
260+
258261
SpinLockAcquire(&walrcv->mutex);
259262
receivePtr = walrcv->receivedUpto;
260263
SpinLockRelease(&walrcv->mutex);
@@ -264,7 +267,12 @@ GetReplicationApplyDelay(void)
264267
if (XLByteEQ(receivePtr, replayPtr))
265268
return 0;
266269

267-
TimestampDifference(GetCurrentChunkReplayStartTime(),
270+
chunckReplayStartTime = GetCurrentChunkReplayStartTime();
271+
272+
if (chunckReplayStartTime == 0)
273+
return -1;
274+
275+
TimestampDifference(chunckReplayStartTime,
268276
GetCurrentTimestamp(),
269277
&secs, &usecs);
270278

0 commit comments

Comments
 (0)