From bdbcaf7f8ba970e947590d86e6a19d4cb0087d70 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Tue, 1 Oct 2024 06:06:57 +1300
Subject: [PATCH] Don't write changing WAL buffers with direct I/O.

Filesystems that have checksums for user data sometimes don't like it if
you modify the data in memory while it is being written to disk.
XLogWrite() allowed that with trailing partially filled WAL pages,
because it always writes the whole WAL buffer, and yet other backends
are allowed to fill in the rest of the buffer at the same time.

Defend against corruption by taking a temporary copy of any trailing
partial page.  Use a vectored write if necessary to avoid an extra
system call.

Unfortunately on Windows pg_pwritev() is implemented with a loop and the
default wal_sync_method will therefore sometimes wait for synchronous
I/O twice, but that is mitigated by the fact that we don't activate the
new behavior for buffered WAL writes, still the default.  (It'd be
possible to WriteFileGather() but that'd take more infrastructural
changes do deal with overlapped I/O.)

XXX Zeroes in unstable region instead?

Per observation from Andres Freund that the WAL must have the same
problem that we'd already diagnosed for relation data with concurrently
modified hint bits.

Discussion: https://postgr.es/m/CA%2BhUKGL-sZrfwcdme8jERPbn%2BsGbY13sRCvq8b9Hp%3DhaWpC6fw%40mail.gmail.com
---
 src/backend/access/transam/xlog.c | 43 ++++++++++++++++++++++++++++++-
 1 file changed, 42 insertions(+), 1 deletion(-)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 6f58412bcab..c8994ad8aad 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2426,6 +2426,16 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			Size		nleft;
 			ssize_t		written;
 			instr_time	start;
+			bool		copy_tail;
+
+			/*
+			 * Do we need to treat the tail page specially to avoid writing
+			 * data out while the buffer is still potentially changing?  We
+			 * enable this only for direct I/O currently, where that is known
+			 * to cause checksum failures on some file systems.
+			 */
+			copy_tail = (io_direct_flags & IO_DIRECT_WAL) != 0 &&
+				ispartialpage;
 
 			/* OK to write the page(s) */
 			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
@@ -2433,6 +2443,37 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 			nleft = nbytes;
 			do
 			{
+				char		tail_page[XLOG_BLCKSZ];
+				struct iovec iov[2];
+				int			iovcnt = 0;
+
+				if (!copy_tail || nleft > XLOG_BLCKSZ)
+				{
+					/*
+					 * Write as many pages as possible directly from WAL
+					 * buffers.
+					 */
+					iov[iovcnt].iov_base = from;
+					iov[iovcnt].iov_len = nleft - (copy_tail ? XLOG_BLCKSZ : 0);
+					++iovcnt;
+				}
+
+				if (copy_tail)
+				{
+					size_t		tail_nleft = Min(nleft, XLOG_BLCKSZ);
+
+					/*
+					 * Copy the tail page into a temporary buffer and write
+					 * that part from there instead, because the WAL buffer is
+					 * potentially still changing underneath our feet and our
+					 * defense against that is enabled.
+					 */
+					memcpy(tail_page, from + nleft - tail_nleft, tail_nleft);
+					iov[iovcnt].iov_base = tail_page;
+					iov[iovcnt].iov_len = tail_nleft;
+					++iovcnt;
+				}
+
 				errno = 0;
 
 				/* Measure I/O timing to write WAL data */
@@ -2442,7 +2483,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
 					INSTR_TIME_SET_ZERO(start);
 
 				pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
-				written = pg_pwrite(openLogFile, from, nleft, startoffset);
+				written = pg_pwritev(openLogFile, iov, iovcnt, startoffset);
 				pgstat_report_wait_end();
 
 				/*
-- 
2.47.0

