48
48
#include "pg_trace.h"
49
49
#include "pgstat.h"
50
50
#include "port/atomics.h"
51
+ #include "port/pg_iovec.h"
51
52
#include "postmaster/bgwriter.h"
52
53
#include "postmaster/startup.h"
53
54
#include "postmaster/walwriter.h"
@@ -3270,7 +3271,6 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
3270
3271
XLogSegNo installed_segno ;
3271
3272
XLogSegNo max_segno ;
3272
3273
int fd ;
3273
- int nbytes ;
3274
3274
int save_errno ;
3275
3275
3276
3276
XLogFilePath (path , ThisTimeLineID , logsegno , wal_segment_size );
@@ -3317,6 +3317,9 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
3317
3317
save_errno = 0 ;
3318
3318
if (wal_init_zero )
3319
3319
{
3320
+ struct iovec iov [PG_IOV_MAX ];
3321
+ int blocks ;
3322
+
3320
3323
/*
3321
3324
* Zero-fill the file. With this setting, we do this the hard way to
3322
3325
* ensure that all the file space has really been allocated. On
@@ -3326,15 +3329,28 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
3326
3329
* indirect blocks are down on disk. Therefore, fdatasync(2) or
3327
3330
* O_DSYNC will be sufficient to sync future writes to the log file.
3328
3331
*/
3329
- for (nbytes = 0 ; nbytes < wal_segment_size ; nbytes += XLOG_BLCKSZ )
3332
+
3333
+ /* Prepare to write out a lot of copies of our zero buffer at once. */
3334
+ for (int i = 0 ; i < lengthof (iov ); ++ i )
3330
3335
{
3331
- errno = 0 ;
3332
- if (write (fd , zbuffer .data , XLOG_BLCKSZ ) != XLOG_BLCKSZ )
3336
+ iov [i ].iov_base = zbuffer .data ;
3337
+ iov [i ].iov_len = XLOG_BLCKSZ ;
3338
+ }
3339
+
3340
+ /* Loop, writing as many blocks as we can for each system call. */
3341
+ blocks = wal_segment_size / XLOG_BLCKSZ ;
3342
+ for (int i = 0 ; i < blocks ;)
3343
+ {
3344
+ int iovcnt = Min (blocks - i , lengthof (iov ));
3345
+ off_t offset = i * XLOG_BLCKSZ ;
3346
+
3347
+ if (pg_pwritev_with_retry (fd , iov , iovcnt , offset ) < 0 )
3333
3348
{
3334
- /* if write didn't set errno, assume no disk space */
3335
- save_errno = errno ? errno : ENOSPC ;
3349
+ save_errno = errno ;
3336
3350
break ;
3337
3351
}
3352
+
3353
+ i += iovcnt ;
3338
3354
}
3339
3355
}
3340
3356
else
0 commit comments