diff options
| author | Tom Lane | 2020-11-11 03:51:18 +0000 |
|---|---|---|
| committer | Tom Lane | 2020-11-11 03:51:54 +0000 |
| commit | ec29427ce2a451e7fef7a22de6db8147d8a80994 (patch) | |
| tree | 9be339cd239d97d3ae9358ab6eedb5a6c4d2dad1 /src/backend/replication | |
| parent | b8b6a0124b21edd4aed24217a8e5ecf621ccd661 (diff) | |
Fix and simplify some usages of TimestampDifference().
Introduce TimestampDifferenceMilliseconds() to simplify callers
that would rather have the difference in milliseconds, instead of
the select()-oriented seconds-and-microseconds format. This gets
rid of at least one integer division per call, and it eliminates
some apparently-easy-to-mess-up arithmetic.
Two of these call sites were in fact wrong:
* pg_prewarm's autoprewarm_main() forgot to multiply the seconds
by 1000, thus ending up with a delay 1000X shorter than intended.
That doesn't quite make it a busy-wait, but close.
* postgres_fdw's pgfdw_get_cleanup_result() thought it needed to compute
microseconds not milliseconds, thus ending up with a delay 1000X longer
than intended. Somebody along the way had noticed this problem but
misdiagnosed the cause, and imposed an ad-hoc 60-second limit rather
than fixing the units. This was relatively harmless in context, because
we don't care that much about exactly how long this delay is; still,
it's wrong.
There are a few more callers of TimestampDifference() that don't
have a direct need for seconds-and-microseconds, but can't use
TimestampDifferenceMilliseconds() either because they do need
microsecond precision or because they might possibly deal with
intervals long enough to overflow 32-bit milliseconds. It might be
worth inventing another API to improve that, but that seems outside
the scope of this patch; so those callers are untouched here.
Given the fact that we are fixing some bugs, and the likelihood
that future patches might want to back-patch code that uses this
new API, back-patch to all supported branches.
Alexey Kondratov and Tom Lane
Discussion: https://postgr.es/m/[email protected]
Diffstat (limited to 'src/backend/replication')
| -rw-r--r-- | src/backend/replication/walreceiverfuncs.c | 25 | ||||
| -rw-r--r-- | src/backend/replication/walsender.c | 8 |
2 files changed, 5 insertions, 28 deletions
diff --git a/src/backend/replication/walreceiverfuncs.c b/src/backend/replication/walreceiverfuncs.c index e6757573010..c3e317df9ff 100644 --- a/src/backend/replication/walreceiverfuncs.c +++ b/src/backend/replication/walreceiverfuncs.c @@ -350,10 +350,6 @@ GetReplicationApplyDelay(void) WalRcvData *walrcv = WalRcv; XLogRecPtr receivePtr; XLogRecPtr replayPtr; - - long secs; - int usecs; - TimestampTz chunkReplayStartTime; SpinLockAcquire(&walrcv->mutex); @@ -370,11 +366,8 @@ GetReplicationApplyDelay(void) if (chunkReplayStartTime == 0) return -1; - TimestampDifference(chunkReplayStartTime, - GetCurrentTimestamp(), - &secs, &usecs); - - return (((int) secs * 1000) + (usecs / 1000)); + return TimestampDifferenceMilliseconds(chunkReplayStartTime, + GetCurrentTimestamp()); } /* @@ -385,24 +378,14 @@ int GetReplicationTransferLatency(void) { WalRcvData *walrcv = WalRcv; - TimestampTz lastMsgSendTime; TimestampTz lastMsgReceiptTime; - long secs = 0; - int usecs = 0; - int ms; - SpinLockAcquire(&walrcv->mutex); lastMsgSendTime = walrcv->lastMsgSendTime; lastMsgReceiptTime = walrcv->lastMsgReceiptTime; SpinLockRelease(&walrcv->mutex); - TimestampDifference(lastMsgSendTime, - lastMsgReceiptTime, - &secs, &usecs); - - ms = ((int) secs * 1000) + (usecs / 1000); - - return ms; + return TimestampDifferenceMilliseconds(lastMsgSendTime, + lastMsgReceiptTime); } diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c index df27e847617..1a25be92c29 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -2194,8 +2194,6 @@ WalSndComputeSleeptime(TimestampTz now) if (wal_sender_timeout > 0 && last_reply_timestamp > 0) { TimestampTz wakeup_time; - long sec_to_timeout; - int microsec_to_timeout; /* * At the latest stop sleeping once wal_sender_timeout has been @@ -2214,11 +2212,7 @@ WalSndComputeSleeptime(TimestampTz now) wal_sender_timeout / 2); /* Compute relative time until wakeup. */ - TimestampDifference(now, wakeup_time, - &sec_to_timeout, µsec_to_timeout); - - sleeptime = sec_to_timeout * 1000 + - microsec_to_timeout / 1000; + sleeptime = TimestampDifferenceMilliseconds(now, wakeup_time); } return sleeptime; |
