I threw Claude at this, and it pointed out that: > That's pretty harmless, but there's also a simple fix: increment the > "invocation number" when the launcher exits. That way, the worker will > quickly see that the invocation number doesn't match and exit, even if > the launcher fails to signal it.
This leaves a gap: a worker that is blocked in a lock when the launcher exits still outlives it, as it doesn't get any abort checks. With the injection point in 0002 hoolding the lancher before it learns the PID, and another session holding ACCESS EXCLUSIVE on pg_shdescription the worker is still waiting for the lock after the launcher is already gone and data_checksums is already off. v2-0002 is your fix plus a TerminateBackgroundWorker call and a few related changes. It also found that a very unlikely launcher failure in InitPostgres can cause launcher_exit to kill a duplicate/next launcher's worker, resulting in an additional error on master, or also an assertion with v1-0001. This is handled in v2-0001 with a static flag, which is not the nicest solution, but everything else that I thought would be better ended up causing other issues. This is an even more unlikely issue than the previous one, but I included the patch for it in case we want to include it. v2-0003 is your original test modified to reproduce the first issue. > It's pretty artificial, and the > consequence is very minor so the test doesn't seem worth committing. +1, but it also doesn't take much time to run.
From 9eb53ab99734431ca5382ec801c6609a3b8f7942 Mon Sep 17 00:00:00 2001 From: Zsolt Parragi <[email protected]> Date: Tue, 1 Dec 2026 22:47:32 +0000 Subject: [PATCH v2 1/3] Don't clean up after a data checksums launcher that never started launcher_exit() cleans up after the data checksums launcher: it signals the worker, turns data checksums off if enabling did not finish, and clears the shared launcher_running flag. It is registered, and the local launcher_running flag was set, before the launcher connects to the database, while the shared flag is set only after that. If connection setup failed, shmem_exit() released DataChecksumsWorkerLock before running the exit callbacks, so a duplicate launcher waiting for the lock could find the shared flag unset and take over. launcher_exit() in the failed launcher then acted as if it owned the operation: it could signal the new launcher's worker, turn data checksums off underneath it, and clear its shared flag. To fix, set the local flag together with the shared one, and add a launcher_started flag that makes launcher_exit() do nothing in a launcher that never got that far. The callback has to stay registered before connecting: launcher_exit() may wait for a ProcSignalBarrier, which only works if our own ProcSignal slot is already gone by then, as interrupts are held during exit. Add a comment about that. This needs an error in InitPostgres(), so it's unlikely in practice. Discussion: https://www.postgresql.org/message-id/[email protected] --- src/backend/postmaster/datachecksum_state.c | 30 ++++++++++++++++----- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/backend/postmaster/datachecksum_state.c b/src/backend/postmaster/datachecksum_state.c index e2b673a96b5..bac84843d24 100644 --- a/src/backend/postmaster/datachecksum_state.c +++ b/src/backend/postmaster/datachecksum_state.c @@ -394,6 +394,12 @@ static uint64 worker_invocation; */ static volatile sig_atomic_t launcher_running = false; +/* + * Did this process ever take over as the launcher? Unlike launcher_running, + * this stays set after we have given up the role. + */ +static bool launcher_started = false; + /* Are we enabling data checksums, or disabling them? */ static DataChecksumsWorkerOperation operation; @@ -1124,17 +1130,21 @@ done: * launcher_exit * * Internal routine for cleaning up state when a launcher process which has - * performed checksum operations exits. A launcher process which is exiting due - * to a duplicate started launcher does not need to perform any cleanup and - * this function should not be called. Otherwise, we need to clean up the abort - * flag to ensure that processing can be started again if it was previously - * aborted (note: started again, *not* restarted from where it left off). + * performed checksum operations exits. A launcher process which never took + * over the launcher role, because another launcher was already running or + * because it failed during startup, must not perform any cleanup. Otherwise, + * we need to clean up the abort flag to ensure that processing can be started + * again if it was previously aborted (note: started again, *not* restarted + * from where it left off). */ static void launcher_exit(int code, Datum arg) { abort_requested = false; + if (!launcher_started) + return; + if (launcher_running) { LWLockAcquire(DataChecksumsWorkerLock, LW_EXCLUSIVE); @@ -1282,13 +1292,21 @@ DataChecksumsWorkerLauncherMain(Datum arg) return; } + /* + * Register the exit callback before connecting, so that it runs after our + * ProcSignal slot has been released. launcher_exit() may have to wait + * for a ProcSignalBarrier, which we could not absorb ourselves with + * interrupts held during exit. The callback does nothing until + * launcher_started is set below. + */ on_shmem_exit(launcher_exit, 0); - launcher_running = true; /* Initialize a connection to shared catalogs only */ BackgroundWorkerInitializeConnectionByOid(InvalidOid, InvalidOid, 0); operation = DataChecksumState->launch_operation; + launcher_started = true; + launcher_running = true; DataChecksumState->launcher_running = true; DataChecksumState->operation = operation; DataChecksumState->cost_delay = DataChecksumState->launch_cost_delay; -- 2.56.1
From db07be32063c17b6f69148b669f170674cba9c75 Mon Sep 17 00:00:00 2001 From: Zsolt Parragi <[email protected]> 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 <[email protected]> Author: Zsolt Parragi <[email protected]> Discussion: https://www.postgresql.org/message-id/[email protected] --- 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
From 681a800da5a30645209c36aa7a8181a0a5dabd0a Mon Sep 17 00:00:00 2001 From: Zsolt Parragi <[email protected]> Date: Tue, 1 Dec 2026 22:23:36 +0000 Subject: [PATCH v2 3/3] Add test for data checksums worker outliving the launcher Add an injection point in the data checksums launcher right after it has registered a worker, before it has learned the worker's PID, and use it to test that the worker exits if the launcher exits at that point, even while the worker is waiting for a lock. Author: Heikki Linnakangas <[email protected]> Author: Zsolt Parragi <[email protected]> Discussion: https://www.postgresql.org/message-id/[email protected] --- src/backend/postmaster/datachecksum_state.c | 2 + .../modules/test_checksums/t/005_injection.pl | 52 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/backend/postmaster/datachecksum_state.c b/src/backend/postmaster/datachecksum_state.c index b6d2cf30208..e73070be3a6 100644 --- a/src/backend/postmaster/datachecksum_state.c +++ b/src/backend/postmaster/datachecksum_state.c @@ -1020,6 +1020,8 @@ ProcessDatabase(DataChecksumsWorkerDatabase *db) } worker_handle = bgw_handle; + INJECTION_POINT("datachecksumsworker-launcher-worker-registered", NULL); + /* * While this expects to wait for BGWORKER_STARTUP it may return _STOPPED * if the worker was terminated in the meantime so we must check status. diff --git a/src/test/modules/test_checksums/t/005_injection.pl b/src/test/modules/test_checksums/t/005_injection.pl index 8dcbe971466..40a7da2ef22 100644 --- a/src/test/modules/test_checksums/t/005_injection.pl +++ b/src/test/modules/test_checksums/t/005_injection.pl @@ -205,6 +205,58 @@ like( qr/create database strategy "file_copy" not allowed/m, 'file_copy error message in log'); +# --------------------------------------------------------------------------- +# Test that the worker is terminated if the launcher exits before it has +# learned the worker's PID, even when the worker is waiting for a lock and +# does not get to check for an abort request. +# + +disable_data_checksums($node, wait => 1); + +# The first worker processes the shared catalogs, block it on one of them. +# With wal_level=minimal the lock doesn't assign an XID, so it doesn't hold up +# the launcher's wait for running transactions. +my $locker = $node->background_psql('postgres'); +$locker->query_safe( + 'BEGIN; LOCK TABLE pg_shdescription IN ACCESS EXCLUSIVE MODE;'); + +$node->safe_psql('postgres', + "SELECT injection_points_attach('datachecksumsworker-launcher-worker-registered','wait');" +); +enable_data_checksums($node); +$node->wait_for_event('datachecksums launcher', + 'datachecksumsworker-launcher-worker-registered'); +$node->poll_query_until('postgres', + "SELECT count(*) = 1 FROM pg_catalog.pg_stat_activity " + . "WHERE backend_type = 'datachecksums worker' " + . "AND wait_event_type = 'Lock';") + or die "timed out waiting for datachecksums worker to block"; + +$node->safe_psql('postgres', + "SELECT pg_terminate_backend(pid) FROM pg_catalog.pg_stat_activity " + . "WHERE backend_type = 'datachecksums launcher';"); +$node->poll_query_until('postgres', + "SELECT count(*) = 0 FROM pg_catalog.pg_stat_activity " + . "WHERE backend_type = 'datachecksums launcher';") + or die "timed out waiting for datachecksums launcher to exit"; +$node->safe_psql('postgres', + "SELECT injection_points_detach('datachecksumsworker-launcher-worker-registered');" +); + +# The worker must go away while the lock is still held. +ok( $node->poll_query_until( + 'postgres', + "SELECT count(*) = 0 FROM pg_catalog.pg_stat_activity " + . "WHERE backend_type = 'datachecksums worker';"), + 'blocked worker exits after the launcher exited'); + +$locker->query_safe('COMMIT;'); +$locker->quit; + +# The launcher disappears from pg_stat_activity before its exit callback has +# turned data checksums off, so wait for that. +wait_for_checksum_state($node, 'off'); + # --------------------------------------------------------------------------- # Test teardown # -- 2.56.1
