From db07be32063c17b6f69148b669f170674cba9c75 Mon Sep 17 00:00:00 2001
From: Zsolt Parragi <zsolt.parragi@percona.com>
Date: Tue, 1 Dec 2026 22:47:52 +0000
Subject: [PATCH v2 2/3] 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.

That alone doesn't help a worker that is waiting for a lock, e.g. on a
relation another session holds ACCESS EXCLUSIVE on, as it doesn't get
to check the invocation number.  So also terminate the worker with
TerminateBackgroundWorker() on its handle, like the SIGINT path already
does.  That works whether or not the worker has started yet, but needs
the postmaster, while the bump also works if the postmaster is gone.
This replaces the kill() in launcher_exit(), so the worker_pid field is
no longer needed, remove it.

Author: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Author: Zsolt Parragi <zsolt.parragi@percona.com>
Discussion: https://www.postgresql.org/message-id/3ed1ca68-7409-4170-8a99-32e1704ff4aa@iki.fi
---
 src/backend/postmaster/datachecksum_state.c | 42 +++++++++++++--------
 1 file changed, 27 insertions(+), 15 deletions(-)

diff --git a/src/backend/postmaster/datachecksum_state.c b/src/backend/postmaster/datachecksum_state.c
index bac84843d24..b6d2cf30208 100644
--- a/src/backend/postmaster/datachecksum_state.c
+++ b/src/backend/postmaster/datachecksum_state.c
@@ -344,7 +344,6 @@ typedef struct DataChecksumsStateStruct
 	 */
 	uint64		worker_invocation;	/* unique invocation number */
 	Oid			database_oid;	/* database it's processing */
-	pid_t		worker_pid;		/* worker process's PID */
 
 	/*
 	 * These fields indicate the target state that the worker is currently
@@ -388,6 +387,9 @@ static volatile sig_atomic_t abort_requested = false;
 
 static uint64 worker_invocation;
 
+/* Handle of the last worker registered by this launcher */
+static BackgroundWorkerHandle *worker_handle = NULL;
+
 /*
  * Have we set the DataChecksumsStateStruct->launcher_running flag?
  * If we have, we need to clear it before exiting!
@@ -984,7 +986,6 @@ ProcessDatabase(DataChecksumsWorkerDatabase *db)
 	 * if it completes successfully.
 	 */
 	DataChecksumState->worker_result = DATACHECKSUMSWORKER_FAILED;
-	DataChecksumState->worker_pid = InvalidPid;
 
 	invocation = ++DataChecksumState->worker_invocation_counter;
 	DataChecksumState->worker_invocation = invocation;
@@ -1017,6 +1018,7 @@ ProcessDatabase(DataChecksumsWorkerDatabase *db)
 				errhint("The \"%s\" setting might be too low.", "max_worker_processes"));
 		return DATACHECKSUMSWORKER_FAILED;
 	}
+	worker_handle = bgw_handle;
 
 	/*
 	 * While this expects to wait for BGWORKER_STARTUP it may return _STOPPED
@@ -1079,12 +1081,6 @@ ProcessDatabase(DataChecksumsWorkerDatabase *db)
 			errmsg("initiating data checksum processing in database \"%s\"",
 				   db->dbname));
 
-	/* Save the pid of the worker so we can signal it later */
-	LWLockAcquire(DataChecksumsWorkerLock, LW_EXCLUSIVE);
-	Assert(DataChecksumState->worker_invocation == invocation);
-	DataChecksumState->worker_pid = pid;
-	LWLockRelease(DataChecksumsWorkerLock);
-
 	snprintf(activity, sizeof(activity) - 1,
 			 "Waiting for worker in database %s (pid %ld)", db->dbname, (long) pid);
 	pgstat_report_activity(STATE_RUNNING, activity);
@@ -1101,7 +1097,6 @@ ProcessDatabase(DataChecksumsWorkerDatabase *db)
 	LWLockAcquire(DataChecksumsWorkerLock, LW_EXCLUSIVE);
 	Assert(DataChecksumState->worker_invocation == invocation);
 	result = DataChecksumState->worker_result;
-	DataChecksumState->worker_pid = InvalidPid;
 	LWLockRelease(DataChecksumsWorkerLock);
 
 	/*
@@ -1148,14 +1143,31 @@ launcher_exit(int code, Datum arg)
 	if (launcher_running)
 	{
 		LWLockAcquire(DataChecksumsWorkerLock, LW_EXCLUSIVE);
-		if (DataChecksumState->worker_pid != InvalidPid)
+
+		/*
+		 * Increment the invocation number so that if a worker is running, it
+		 * will see that the current invocation aborted and bail out quickly.
+		 * That doesn't help a worker that is waiting for a lock, so we also
+		 * terminate it.  Using the handle rather than the worker's PID covers
+		 * the case that the worker was launched, but we haven't seen it start
+		 * yet.  Terminating needs the postmaster, though, while the bump also
+		 * works if the postmaster is gone.
+		 */
+		DataChecksumState->worker_invocation =
+			++DataChecksumState->worker_invocation_counter;
+		LWLockRelease(DataChecksumsWorkerLock);
+
+		if (worker_handle != NULL)
 		{
-			ereport(LOG,
-					errmsg("data checksums launcher exiting while worker is still running, signalling worker"));
-			kill(DataChecksumState->worker_pid, SIGTERM);
-			DataChecksumState->worker_pid = InvalidPid;
+			pid_t		pid;
+
+			if (GetBackgroundWorkerPid(worker_handle, &pid) != BGWH_STOPPED)
+			{
+				ereport(LOG,
+						errmsg("data checksums launcher exiting while worker is still running, signalling worker"));
+				TerminateBackgroundWorker(worker_handle);
+			}
 		}
-		LWLockRelease(DataChecksumsWorkerLock);
 	}
 
 	/*
-- 
2.56.1

