From c407ea9d7352a108088d3319202a8488b2b9ba7b Mon Sep 17 00:00:00 2001 From: Thomas Munro Date: Sun, 20 Dec 2020 13:16:21 +1300 Subject: [PATCH v2 2/2] Use vectored I/O to zero WAL segments. Instead of making 2048 block-sized write() calls to fill a 16MB WAL file with zeroes, make a much smaller number of pwritev() calls. The actual number depends on the OS's IOV_MAX, but it works out as 2 pwritev() calls on Linux and FreeBSD. Discussion: https://postgr.es/m/CA%2BhUKGJA%2Bu-220VONeoREBXJ9P3S94Y7J%2BkqCnTYmahvZJwM%3Dg%40mail.gmail.com --- src/backend/access/transam/xlog.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index b1e5d2dbff..e27ccddec1 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -48,6 +48,7 @@ #include "pg_trace.h" #include "pgstat.h" #include "port/atomics.h" +#include "port/pg_uio.h" #include "postmaster/bgwriter.h" #include "postmaster/startup.h" #include "postmaster/walwriter.h" @@ -3270,7 +3271,6 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock) XLogSegNo installed_segno; XLogSegNo max_segno; int fd; - int nbytes; int save_errno; XLogFilePath(path, ThisTimeLineID, logsegno, wal_segment_size); @@ -3317,6 +3317,9 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock) save_errno = 0; if (wal_init_zero) { + struct iovec iov[Min(IOV_MAX, 1024)]; /* cap stack space */ + int blocks; + /* * Zero-fill the file. With this setting, we do this the hard way to * ensure that all the file space has really been allocated. On @@ -3326,15 +3329,31 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock) * indirect blocks are down on disk. Therefore, fdatasync(2) or * O_DSYNC will be sufficient to sync future writes to the log file. */ - for (nbytes = 0; nbytes < wal_segment_size; nbytes += XLOG_BLCKSZ) + + /* Prepare to write out a lot of copies of our zero buffer at once. */ + for (int i = 0; i < lengthof(iov); ++i) + { + iov[i].iov_base = zbuffer.data; + iov[i].iov_len = XLOG_BLCKSZ; + } + + /* Loop, writing as many blocks as we can for each system call. */ + blocks = wal_segment_size / XLOG_BLCKSZ; + for (int i = 0; i < blocks;) { + int iovcnt = Min(blocks - i, lengthof(iov)); + size_t size = iovcnt * XLOG_BLCKSZ; + off_t offset = i * XLOG_BLCKSZ; + errno = 0; - if (write(fd, zbuffer.data, XLOG_BLCKSZ) != XLOG_BLCKSZ) + if (pg_pwritev(fd, iov, iovcnt, offset) != size) { /* if write didn't set errno, assume no disk space */ save_errno = errno ? errno : ENOSPC; break; } + + i += iovcnt; } } else -- 2.20.1