v1-0001-Assert-that-ControlFileLock-is-held-exclusively-i.patch

application/octet-stream

Filename: v1-0001-Assert-that-ControlFileLock-is-held-exclusively-i.patch
Type: application/octet-stream
Part: 0
Message: Re: race condition when writing pg_control

Patch

Format: format-patch
Series: patch v1-0001
Subject: Assert that ControlFileLock is held exclusively in UpdateControlFile().
File+
src/backend/access/transam/xlog.c 12 1
From 99341ba250aae7186e3a974d2d5fa035ac0dc625 Mon Sep 17 00:00:00 2001
From: Nathan Bossart <bossartn@amazon.com>
Date: Tue, 26 May 2020 19:26:45 +0000
Subject: [PATCH v1 1/1] Assert that ControlFileLock is held exclusively in
 UpdateControlFile().

---
 src/backend/access/transam/xlog.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index ca09d81b08..f71f6a287b 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -4900,6 +4900,7 @@ ReadControlFile(void)
 void
 UpdateControlFile(void)
 {
+	Assert(LWLockHeldByMeInMode(ControlFileLock, LW_EXCLUSIVE));
 	update_controlfile(DataDir, ControlFile, true);
 }
 
@@ -6955,8 +6956,14 @@ StartupXLOG(void)
 			}
 		}
 		ControlFile->time = (pg_time_t) time(NULL);
-		/* No need to hold ControlFileLock yet, we aren't up far enough */
+
+		/*
+		 * We aren't up far enough yet to need the ControlFileLock, but we take
+		 * it anyway to satisfy the assertion in UpdateControlFile().
+		 */
+		LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
 		UpdateControlFile();
+		LWLockRelease(ControlFileLock);
 
 		/*
 		 * Initialize our local copy of minRecoveryPoint.  When doing crash
@@ -9743,6 +9750,8 @@ XLogReportParameters(void)
 			XLogFlush(recptr);
 		}
 
+		LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
+
 		ControlFile->MaxConnections = MaxConnections;
 		ControlFile->max_worker_processes = max_worker_processes;
 		ControlFile->max_wal_senders = max_wal_senders;
@@ -9752,6 +9761,8 @@ XLogReportParameters(void)
 		ControlFile->wal_log_hints = wal_log_hints;
 		ControlFile->track_commit_timestamp = track_commit_timestamp;
 		UpdateControlFile();
+
+		LWLockRelease(ControlFileLock);
 	}
 }
 
-- 
2.16.6