Fix integer overflow in debug message of walreceiver
authorTatsuo Ishii
Fri, 13 Mar 2015 23:16:50 +0000 (08:16 +0900)
committerTatsuo Ishii
Fri, 13 Mar 2015 23:21:45 +0000 (08:21 +0900)
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).

src/backend/replication/walreceiver.c
src/backend/replication/walreceiverfuncs.c

index c2d4ed3a96846a632232b64cabaed64313be4caa..c4f553868c00d605bf0b51948066d9ce6eed78fe 100644 (file)
@@ -1202,15 +1202,26 @@ ProcessWalSndrMessage(XLogRecPtr walEnd, TimestampTz sendTime)
    {
        char       *sendtime;
        char       *receipttime;
+       int         applyDelay;
 
        /* Copy because timestamptz_to_str returns a static buffer */
        sendtime = pstrdup(timestamptz_to_str(sendTime));
        receipttime = pstrdup(timestamptz_to_str(lastMsgReceiptTime));
-       elog(DEBUG2, "sendtime %s receipttime %s replication apply delay %d ms transfer latency %d ms",
-            sendtime,
-            receipttime,
-            GetReplicationApplyDelay(),
-            GetReplicationTransferLatency());
+       applyDelay = GetReplicationApplyDelay();
+
+       /* apply delay is not available */
+       if (applyDelay == -1)
+           elog(DEBUG2, "sendtime %s receipttime %s replication apply delay (N/A) transfer latency %d ms",
+                sendtime,
+                receipttime,
+                GetReplicationTransferLatency());
+       else
+           elog(DEBUG2, "sendtime %s receipttime %s replication apply delay %d ms transfer latency %d ms",
+                sendtime,
+                receipttime,
+                applyDelay,
+                GetReplicationTransferLatency());
+
        pfree(sendtime);
        pfree(receipttime);
    }
index 579216af34d22c127f8bbb9d03ec6bc5281afdba..c450bb8ad6a20cc9d0130048739c2171688a5c3c 100644 (file)
@@ -314,7 +314,8 @@ GetWalRcvWriteRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
 }
 
 /*
- * Returns the replication apply delay in ms
+ * Returns the replication apply delay in ms or -1
+ * if the apply delay info is not available
  */
 int
 GetReplicationApplyDelay(void)
@@ -328,6 +329,8 @@ GetReplicationApplyDelay(void)
    long        secs;
    int         usecs;
 
+   TimestampTz chunckReplayStartTime;
+
    SpinLockAcquire(&walrcv->mutex);
    receivePtr = walrcv->receivedUpto;
    SpinLockRelease(&walrcv->mutex);
@@ -337,7 +340,12 @@ GetReplicationApplyDelay(void)
    if (receivePtr == replayPtr)
        return 0;
 
-   TimestampDifference(GetCurrentChunkReplayStartTime(),
+   chunckReplayStartTime = GetCurrentChunkReplayStartTime();
+
+   if (chunckReplayStartTime == 0)
+       return -1;
+
+   TimestampDifference(chunckReplayStartTime,
                        GetCurrentTimestamp(),
                        &secs, &usecs);