From 1b407eb13c26ed9bb281027e12072fdfbedc525f Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Mon, 30 Nov 2026 23:48:40 +0200
Subject: [PATCH 1/2] Bump data checksums invocation counter when launcher
 exits

When the data checksums launcher exits, launcher_exit() sends SIGTERM
to the worker, but it can only do that if it knows the worker's PID.
The launcher knows it only after WaitForDataChecksumsWorkerState()
reports that the worker has started.  If the launcher exited after
registering the worker but before recording its PID, e.g. because it
received SIGTERM while waiting for it to start up, the worker was not
signalled and would proceed to start up and run.

To fix, increment the current invocation number in launcher_exit().
That way, if a worker has been launched but hasn't fully started up
yet, it will see that the invocation number has changed when it starts
up, and exit.
---
 src/backend/postmaster/datachecksum_state.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/src/backend/postmaster/datachecksum_state.c b/src/backend/postmaster/datachecksum_state.c
index e2b673a96b5..10f76fd09e4 100644
--- a/src/backend/postmaster/datachecksum_state.c
+++ b/src/backend/postmaster/datachecksum_state.c
@@ -1138,6 +1138,17 @@ launcher_exit(int code, Datum arg)
 	if (launcher_running)
 	{
 		LWLockAcquire(DataChecksumsWorkerLock, LW_EXCLUSIVE);
+
+		/*
+		 * Increment the invocation number so that if a worker is running, it
+		 * will see that the current invocation aborted and bail out quickly.
+		 * We also send SIGTERM to the worker, but bumping the invocation
+		 * number covers the case that the worker was launched, but we hadn't
+		 * set DataChecksumState->worker_pid yet.
+		 */
+		DataChecksumState->worker_invocation =
+			++DataChecksumState->worker_invocation_counter;
+
 		if (DataChecksumState->worker_pid != InvalidPid)
 		{
 			ereport(LOG,
-- 
2.47.3

