From 7676617ec45f8aa08c6f8ef7c5fc889ade4a9994 Mon Sep 17 00:00:00 2001
From: Bernd Helmle <Bernd Helmle mailings@oopsware.de>
Date: Thu, 10 Jul 2025 18:23:51 +0200
Subject: [PATCH 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 4350f6abe3a..53890c3e726 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -671,6 +671,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().
@@ -5049,7 +5059,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;
 }
@@ -5435,6 +5458,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 c5202a8a8db..785b8d4b04f 100644
--- a/src/backend/utils/init/postinit.c
+++ b/src/backend/utils/init/postinit.c
@@ -749,13 +749,13 @@ InitPostgres(const char *in_dbname, Oid dboid,
 	 */
 	SharedInvalBackendInit(false);
 
+	ProcSignalInit(MyCancelKey, MyCancelKeyLength);
+
 	/*
 	 * Set up backend local cache of Controldata values.
 	 */
 	InitLocalControldata();
 
-	ProcSignalInit(MyCancelKey, MyCancelKeyLength);
-
 	/*
 	 * 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.50.0

