@@ -122,8 +122,8 @@ typedef enum WalRcvWakeupReason
122
122
WALRCV_WAKEUP_TERMINATE ,
123
123
WALRCV_WAKEUP_PING ,
124
124
WALRCV_WAKEUP_REPLY ,
125
- WALRCV_WAKEUP_HSFEEDBACK ,
126
- NUM_WALRCV_WAKEUPS
125
+ WALRCV_WAKEUP_HSFEEDBACK
126
+ #define NUM_WALRCV_WAKEUPS ( WALRCV_WAKEUP_HSFEEDBACK + 1 )
127
127
} WalRcvWakeupReason ;
128
128
129
129
/*
@@ -206,8 +206,6 @@ WalReceiverMain(void)
206
206
*/
207
207
Assert (walrcv != NULL );
208
208
209
- now = GetCurrentTimestamp ();
210
-
211
209
/*
212
210
* Mark walreceiver as running in shared memory.
213
211
*
@@ -261,6 +259,7 @@ WalReceiverMain(void)
261
259
Assert (!is_temp_slot || (slotname [0 ] == '\0' ));
262
260
263
261
/* Initialise to a sanish value */
262
+ now = GetCurrentTimestamp ();
264
263
walrcv -> lastMsgSendTime =
265
264
walrcv -> lastMsgReceiptTime = walrcv -> latestWalEndTime = now ;
266
265
@@ -464,6 +463,7 @@ WalReceiverMain(void)
464
463
{
465
464
ConfigReloadPending = false;
466
465
ProcessConfigFile (PGC_SIGHUP );
466
+ /* recompute wakeup times */
467
467
now = GetCurrentTimestamp ();
468
468
for (int i = 0 ; i < NUM_WALRCV_WAKEUPS ; ++ i )
469
469
WalRcvComputeNextWakeup (i , now );
@@ -472,7 +472,6 @@ WalReceiverMain(void)
472
472
473
473
/* See if we can read data immediately */
474
474
len = walrcv_receive (wrconn , & buf , & wait_fd );
475
- now = GetCurrentTimestamp ();
476
475
if (len != 0 )
477
476
{
478
477
/*
@@ -487,6 +486,7 @@ WalReceiverMain(void)
487
486
* Something was received from primary, so adjust
488
487
* the ping and terminate wakeup times.
489
488
*/
489
+ now = GetCurrentTimestamp ();
490
490
WalRcvComputeNextWakeup (WALRCV_WAKEUP_TERMINATE ,
491
491
now );
492
492
WalRcvComputeNextWakeup (WALRCV_WAKEUP_PING , now );
@@ -506,7 +506,6 @@ WalReceiverMain(void)
506
506
break ;
507
507
}
508
508
len = walrcv_receive (wrconn , & buf , & wait_fd );
509
- now = GetCurrentTimestamp ();
510
509
}
511
510
512
511
/* Let the primary know that we received some data. */
@@ -525,7 +524,7 @@ WalReceiverMain(void)
525
524
break ;
526
525
527
526
/* Find the soonest wakeup time, to limit our nap. */
528
- nextWakeup = PG_INT64_MAX ;
527
+ nextWakeup = TIMESTAMP_INFINITY ;
529
528
for (int i = 0 ; i < NUM_WALRCV_WAKEUPS ; ++ i )
530
529
nextWakeup = Min (wakeup [i ], nextWakeup );
531
530
@@ -536,6 +535,7 @@ WalReceiverMain(void)
536
535
* millisecond to avoid waking up too early and spinning until
537
536
* one of the wakeup times.
538
537
*/
538
+ now = GetCurrentTimestamp ();
539
539
nap = (int ) Min (INT_MAX , Max (0 , (nextWakeup - now + 999 ) / 1000 ));
540
540
541
541
/*
@@ -556,7 +556,6 @@ WalReceiverMain(void)
556
556
wait_fd ,
557
557
nap ,
558
558
WAIT_EVENT_WAL_RECEIVER_MAIN );
559
- now = GetCurrentTimestamp ();
560
559
if (rc & WL_LATCH_SET )
561
560
{
562
561
ResetLatch (MyLatch );
@@ -592,19 +591,20 @@ WalReceiverMain(void)
592
591
* Check if time since last receive from primary has
593
592
* reached the configured limit.
594
593
*/
594
+ now = GetCurrentTimestamp ();
595
595
if (now >= wakeup [WALRCV_WAKEUP_TERMINATE ])
596
596
ereport (ERROR ,
597
597
(errcode (ERRCODE_CONNECTION_FAILURE ),
598
598
errmsg ("terminating walreceiver due to timeout" )));
599
599
600
600
/*
601
- * We didn't receive anything new, for half of receiver
602
- * replication timeout. Ping the server.
601
+ * If we didn't receive anything new for half of receiver
602
+ * replication timeout, then ping the server.
603
603
*/
604
604
if (now >= wakeup [WALRCV_WAKEUP_PING ])
605
605
{
606
606
requestReply = true;
607
- wakeup [WALRCV_WAKEUP_PING ] = PG_INT64_MAX ;
607
+ wakeup [WALRCV_WAKEUP_PING ] = TIMESTAMP_INFINITY ;
608
608
}
609
609
610
610
XLogWalRcvSendReply (requestReply , requestReply );
@@ -1266,7 +1266,6 @@ static void
1266
1266
ProcessWalSndrMessage (XLogRecPtr walEnd , TimestampTz sendTime )
1267
1267
{
1268
1268
WalRcvData * walrcv = WalRcv ;
1269
-
1270
1269
TimestampTz lastMsgReceiptTime = GetCurrentTimestamp ();
1271
1270
1272
1271
/* Update shared-memory status */
@@ -1310,7 +1309,10 @@ ProcessWalSndrMessage(XLogRecPtr walEnd, TimestampTz sendTime)
1310
1309
/*
1311
1310
* Compute the next wakeup time for a given wakeup reason. Can be called to
1312
1311
* initialize a wakeup time, to adjust it for the next wakeup, or to
1313
- * reinitialize it when GUCs have changed.
1312
+ * reinitialize it when GUCs have changed. We ask the caller to pass in the
1313
+ * value of "now" because this frequently avoids multiple calls of
1314
+ * GetCurrentTimestamp(). It had better be a reasonably up-to-date value
1315
+ * though.
1314
1316
*/
1315
1317
static void
1316
1318
WalRcvComputeNextWakeup (WalRcvWakeupReason reason , TimestampTz now )
@@ -1319,30 +1321,29 @@ WalRcvComputeNextWakeup(WalRcvWakeupReason reason, TimestampTz now)
1319
1321
{
1320
1322
case WALRCV_WAKEUP_TERMINATE :
1321
1323
if (wal_receiver_timeout <= 0 )
1322
- wakeup [reason ] = PG_INT64_MAX ;
1324
+ wakeup [reason ] = TIMESTAMP_INFINITY ;
1323
1325
else
1324
- wakeup [reason ] = now + wal_receiver_timeout * INT64CONST ( 1000 );
1326
+ wakeup [reason ] = TimestampTzPlusMilliseconds ( now , wal_receiver_timeout );
1325
1327
break ;
1326
1328
case WALRCV_WAKEUP_PING :
1327
1329
if (wal_receiver_timeout <= 0 )
1328
- wakeup [reason ] = PG_INT64_MAX ;
1330
+ wakeup [reason ] = TIMESTAMP_INFINITY ;
1329
1331
else
1330
- wakeup [reason ] = now + ( wal_receiver_timeout / 2 ) * INT64CONST ( 1000 );
1332
+ wakeup [reason ] = TimestampTzPlusMilliseconds ( now , wal_receiver_timeout / 2 );
1331
1333
break ;
1332
1334
case WALRCV_WAKEUP_HSFEEDBACK :
1333
1335
if (!hot_standby_feedback || wal_receiver_status_interval <= 0 )
1334
- wakeup [reason ] = PG_INT64_MAX ;
1336
+ wakeup [reason ] = TIMESTAMP_INFINITY ;
1335
1337
else
1336
- wakeup [reason ] = now + wal_receiver_status_interval * INT64CONST ( 1000000 );
1338
+ wakeup [reason ] = TimestampTzPlusSeconds ( now , wal_receiver_status_interval );
1337
1339
break ;
1338
1340
case WALRCV_WAKEUP_REPLY :
1339
1341
if (wal_receiver_status_interval <= 0 )
1340
- wakeup [reason ] = PG_INT64_MAX ;
1342
+ wakeup [reason ] = TIMESTAMP_INFINITY ;
1341
1343
else
1342
- wakeup [reason ] = now + wal_receiver_status_interval * INT64CONST (1000000 );
1343
- break ;
1344
- default :
1344
+ wakeup [reason ] = TimestampTzPlusSeconds (now , wal_receiver_status_interval );
1345
1345
break ;
1346
+ /* there's intentionally no default: here */
1346
1347
}
1347
1348
}
1348
1349
0 commit comments