@@ -31,14 +31,13 @@ static char current_walfile_name[MAXPGPATH] = "";
31
31
static bool reportFlushPosition = false;
32
32
static XLogRecPtr lastFlushPosition = InvalidXLogRecPtr ;
33
33
34
- static int64 last_fsync = -1 ; /* timestamp of last WAL file flush */
35
34
static bool still_sending = true; /* feedback still needs to be sent? */
36
35
37
36
static PGresult * HandleCopyStream (PGconn * conn , XLogRecPtr startpos ,
38
37
uint32 timeline , char * basedir ,
39
38
stream_stop_callback stream_stop , int standby_message_timeout ,
40
39
char * partial_suffix , XLogRecPtr * stoppos ,
41
- int fsync_interval );
40
+ bool synchronous );
42
41
static int CopyStreamPoll (PGconn * conn , long timeout_ms );
43
42
static int CopyStreamReceive (PGconn * conn , long timeout , char * * buffer );
44
43
static bool ProcessKeepaliveMsg (PGconn * conn , char * copybuf , int len ,
@@ -55,8 +54,7 @@ static bool CheckCopyStreamStop(PGconn *conn, XLogRecPtr blockpos,
55
54
stream_stop_callback stream_stop ,
56
55
char * partial_suffix , XLogRecPtr * stoppos );
57
56
static long CalculateCopyStreamSleeptime (int64 now , int standby_message_timeout ,
58
- int64 last_status , int fsync_interval ,
59
- XLogRecPtr blockpos );
57
+ int64 last_status );
60
58
61
59
static bool ReadEndOfStreamingResult (PGresult * res , XLogRecPtr * startpos ,
62
60
uint32 * timeline );
@@ -209,7 +207,6 @@ close_walfile(char *basedir, char *partial_suffix, XLogRecPtr pos)
209
207
progname , current_walfile_name , partial_suffix );
210
208
211
209
lastFlushPosition = pos ;
212
- last_fsync = feGetCurrentTimestamp ();
213
210
return true;
214
211
}
215
212
@@ -440,8 +437,8 @@ CheckServerVersionForStreaming(PGconn *conn)
440
437
* allows you to tell the difference between partial and completed files,
441
438
* so that you can continue later where you left.
442
439
*
443
- * fsync_interval controls how often we flush to the received WAL file ,
444
- * in milliseconds .
440
+ * If 'synchronous' is true, the received WAL is flushed as soon as written ,
441
+ * otherwise only when the WAL file is closed .
445
442
*
446
443
* Note: The log position *must* be at a log segment start!
447
444
*/
@@ -450,7 +447,7 @@ ReceiveXlogStream(PGconn *conn, XLogRecPtr startpos, uint32 timeline,
450
447
char * sysidentifier , char * basedir ,
451
448
stream_stop_callback stream_stop ,
452
449
int standby_message_timeout , char * partial_suffix ,
453
- int fsync_interval )
450
+ bool synchronous )
454
451
{
455
452
char query [128 ];
456
453
char slotcmd [128 ];
@@ -595,7 +592,7 @@ ReceiveXlogStream(PGconn *conn, XLogRecPtr startpos, uint32 timeline,
595
592
/* Stream the WAL */
596
593
res = HandleCopyStream (conn , startpos , timeline , basedir , stream_stop ,
597
594
standby_message_timeout , partial_suffix ,
598
- & stoppos , fsync_interval );
595
+ & stoppos , synchronous );
599
596
if (res == NULL )
600
597
goto error ;
601
598
@@ -760,7 +757,7 @@ static PGresult *
760
757
HandleCopyStream (PGconn * conn , XLogRecPtr startpos , uint32 timeline ,
761
758
char * basedir , stream_stop_callback stream_stop ,
762
759
int standby_message_timeout , char * partial_suffix ,
763
- XLogRecPtr * stoppos , int fsync_interval )
760
+ XLogRecPtr * stoppos , bool synchronous )
764
761
{
765
762
char * copybuf = NULL ;
766
763
int64 last_status = -1 ;
@@ -784,24 +781,26 @@ HandleCopyStream(PGconn *conn, XLogRecPtr startpos, uint32 timeline,
784
781
now = feGetCurrentTimestamp ();
785
782
786
783
/*
787
- * If fsync_interval has elapsed since last WAL flush and we've written
788
- * some WAL data, flush them to disk .
784
+ * If synchronous option is true, issue sync command as soon as
785
+ * there are WAL data which has not been flushed yet .
789
786
*/
790
- if (lastFlushPosition < blockpos &&
791
- walfile != -1 &&
792
- ((fsync_interval > 0 &&
793
- feTimestampDifferenceExceeds (last_fsync , now , fsync_interval )) ||
794
- fsync_interval < 0 ))
787
+ if (synchronous && lastFlushPosition < blockpos && walfile != -1 )
795
788
{
796
789
if (fsync (walfile ) != 0 )
797
790
{
798
791
fprintf (stderr , _ ("%s: could not fsync file \"%s\": %s\n" ),
799
792
progname , current_walfile_name , strerror (errno ));
800
793
goto error ;
801
794
}
802
-
803
795
lastFlushPosition = blockpos ;
804
- last_fsync = now ;
796
+
797
+ /*
798
+ * Send feedback so that the server sees the latest WAL locations
799
+ * immediately.
800
+ */
801
+ if (!sendFeedback (conn , blockpos , now , false))
802
+ goto error ;
803
+ last_status = now ;
805
804
}
806
805
807
806
/*
@@ -821,7 +820,7 @@ HandleCopyStream(PGconn *conn, XLogRecPtr startpos, uint32 timeline,
821
820
* Calculate how long send/receive loops should sleep
822
821
*/
823
822
sleeptime = CalculateCopyStreamSleeptime (now , standby_message_timeout ,
824
- last_status , fsync_interval , blockpos );
823
+ last_status );
825
824
826
825
r = CopyStreamReceive (conn , sleeptime , & copybuf );
827
826
while (r != 0 )
@@ -1244,34 +1243,22 @@ CheckCopyStreamStop(PGconn *conn, XLogRecPtr blockpos, uint32 timeline,
1244
1243
*/
1245
1244
static long
1246
1245
CalculateCopyStreamSleeptime (int64 now , int standby_message_timeout ,
1247
- int64 last_status , int fsync_interval , XLogRecPtr blockpos )
1246
+ int64 last_status )
1248
1247
{
1249
- int64 targettime = 0 ;
1250
1248
int64 status_targettime = 0 ;
1251
- int64 fsync_targettime = 0 ;
1252
1249
long sleeptime ;
1253
1250
1254
1251
if (standby_message_timeout && still_sending )
1255
1252
status_targettime = last_status +
1256
1253
(standby_message_timeout - 1 ) * ((int64 ) 1000 );
1257
1254
1258
- if (fsync_interval > 0 && lastFlushPosition < blockpos )
1259
- fsync_targettime = last_fsync +
1260
- (fsync_interval - 1 ) * ((int64 ) 1000 );
1261
-
1262
- if ((status_targettime < fsync_targettime && status_targettime > 0 ) ||
1263
- fsync_targettime == 0 )
1264
- targettime = status_targettime ;
1265
- else
1266
- targettime = fsync_targettime ;
1267
-
1268
- if (targettime > 0 )
1255
+ if (status_targettime > 0 )
1269
1256
{
1270
1257
long secs ;
1271
1258
int usecs ;
1272
1259
1273
1260
feTimestampDifference (now ,
1274
- targettime ,
1261
+ status_targettime ,
1275
1262
& secs ,
1276
1263
& usecs );
1277
1264
/* Always sleep at least 1 sec */
0 commit comments