From d56cb09458a5fbee539601c084fc7e72e5e9cfb5 Mon Sep 17 00:00:00 2001
From: Daniel Gustafsson <dgustafsson@postgresql.org>
Date: Fri, 14 Mar 2025 15:00:51 +0100
Subject: [PATCH v20250315 2/4] Reviewfixups

---
 src/backend/access/transam/xlog.c            | 26 +++++++++++++++++++-
 src/backend/postmaster/datachecksumsworker.c |  2 +-
 src/backend/utils/init/postinit.c            |  4 +--
 src/include/postmaster/datachecksumsworker.h |  2 +-
 src/test/checksum/Makefile                   |  2 +-
 5 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 61da6d583cd..e4c72f985e4 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -660,6 +660,16 @@ static bool updateMinRecoveryPoint = true;
  */
 static uint32 LocalDataChecksumVersion = 0;
 
+/*
+ * Flag to remember if the procsignalbarrier being absorbed for enabling
+ * checksums is the first one or not. The first procsignalbarrier can in rare
+ * circumstances cause a transition from 'on' to 'on' when a new process is
+ * spawned between the update of XLogCtl->data_checksum_version and the
+ * barrier being emitted.  This can only happen on the very first barrier so
+ * mark that with this flag.
+ */
+static bool InitialDataChecksumTransition = true;
+
 /*
  * Variable backing the GUC, keep it in sync with LocalDataChecksumVersion.
  * See SetLocalDataChecksumVersion().
@@ -4935,7 +4945,20 @@ AbsorbChecksumsOnInProgressBarrier(void)
 bool
 AbsorbChecksumsOnBarrier(void)
 {
-	Assert(LocalDataChecksumVersion == PG_DATA_CHECKSUM_INPROGRESS_ON_VERSION);
+	/*
+	 * If the process was spawned between updating XLogCtl and emitting the
+	 * barrier it will have seen the updated value, so for the first barrier
+	 * we accept both "on" and "inprogress-on".
+	 */
+	if (InitialDataChecksumTransition)
+	{
+		Assert((LocalDataChecksumVersion == PG_DATA_CHECKSUM_INPROGRESS_ON_VERSION) ||
+			   (LocalDataChecksumVersion == PG_DATA_CHECKSUM_VERSION));
+		InitialDataChecksumTransition = false;
+	}
+	else
+		Assert(LocalDataChecksumVersion == PG_DATA_CHECKSUM_INPROGRESS_ON_VERSION);
+
 	SetLocalDataChecksumVersion(PG_DATA_CHECKSUM_VERSION);
 	return true;
 }
@@ -5319,6 +5342,7 @@ LocalProcessControlFile(bool reset)
 	Assert(reset || ControlFile == NULL);
 	ControlFile = palloc(sizeof(ControlFileData));
 	ReadControlFile();
+	SetLocalDataChecksumVersion(ControlFile->data_checksum_version);
 }
 
 /*
diff --git a/src/backend/postmaster/datachecksumsworker.c b/src/backend/postmaster/datachecksumsworker.c
index 6a201dca8de..81be2808895 100644
--- a/src/backend/postmaster/datachecksumsworker.c
+++ b/src/backend/postmaster/datachecksumsworker.c
@@ -150,7 +150,7 @@
  *     online operation).
  *
  *
- * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  *
diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c
index bae18b449aa..692570eb0f1 100644
--- a/src/backend/utils/init/postinit.c
+++ b/src/backend/utils/init/postinit.c
@@ -746,13 +746,13 @@ InitPostgres(const char *in_dbname, Oid dboid,
 	 */
 	SharedInvalBackendInit(false);
 
+	ProcSignalInit(MyCancelKeyValid, MyCancelKey);
+
 	/*
 	 * Set up backend local cache of Controldata values.
 	 */
 	InitLocalControldata();
 
-	ProcSignalInit(MyCancelKeyValid, MyCancelKey);
-
 	/*
 	 * Also set up timeout handlers needed for backend operation.  We need
 	 * these in every case except bootstrap.
diff --git a/src/include/postmaster/datachecksumsworker.h b/src/include/postmaster/datachecksumsworker.h
index 59c9000d646..0649232723d 100644
--- a/src/include/postmaster/datachecksumsworker.h
+++ b/src/include/postmaster/datachecksumsworker.h
@@ -4,7 +4,7 @@
  *	  header file for checksum helper background worker
  *
  *
- * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * src/include/postmaster/datachecksumsworker.h
diff --git a/src/test/checksum/Makefile b/src/test/checksum/Makefile
index fd03bf73df4..f287001301e 100644
--- a/src/test/checksum/Makefile
+++ b/src/test/checksum/Makefile
@@ -2,7 +2,7 @@
 #
 # Makefile for src/test/checksum
 #
-# Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
+# Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
 # Portions Copyright (c) 1994, Regents of the University of California
 #
 # src/test/checksum/Makefile
-- 
2.48.1

