Patch
Keep checksums launcher registered while it resets state
- Branch
- master
- Risk
- 30
- Base commit
- 458c77d74d2bab2014e97f01df19dc805dc7b42a
To apply it to a checkout of master at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a2331c-9815-71f3-96ed-28822d55f56e | jq -r .body | git am
From 3c9b750b5bcde0c8c5e40694854d032b93f8edba Mon Sep 17 00:00:00 2001
From: Claude <noreply@anthropic.com>
Date: Tue, 1 Dec 2026 11:36:28 -0800
Subject: [PATCH] Keep checksums launcher registered while it resets state
When enabling data checksums was cancelled, the launcher returned
from DataChecksumsWorkerLauncherMain() with the cluster still in
inprogress-on, having already cleared launcher_running in shared
memory. The state was only set back to off by the launcher_exit()
callback, which takes two checkpoints and waits on two procsignal
barriers. A pg_enable_data_checksums() call arriving in that window
therefore started a second launcher, and the two then changed the
data checksum state concurrently. The new launcher could take the
cluster to on while the old one, having chosen its path before that,
wrote off straight over it. That transition is not a valid one, so
every process which had absorbed on raised "incorrect data checksum
state" over and over, the exiting launcher waited forever on its
barrier, and the cluster ended up with checksums off even though
enabling had been reported as successful.
Fix by resetting the state in DataChecksumsWorkerLauncherMain(),
before launcher_running is cleared, such that a new launcher cannot
be started until the old one has finished changing the state. Also
make launcher_exit() do nothing unless the process still is the
registered launcher. After a normal return there is nothing left
for it to clean up, and whatever state it finds may by then belong
to a launcher started later.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
---
src/backend/postmaster/datachecksum_state.c | 43 ++++++++----
.../modules/test_checksums/t/005_injection.pl | 67 +++++++++++++++++++
2 files changed, 97 insertions(+), 13 deletions(-)
diff --git a/src/backend/postmaster/datachecksum_state.c b/src/backend/postmaster/datachecksum_state.c
index e2b673a96b5..e032d96b816 100644
--- a/src/backend/postmaster/datachecksum_state.c
+++ b/src/backend/postmaster/datachecksum_state.c
@@ -1129,35 +1129,42 @@ done:
* 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).
+ *
+ * Shared state is only ours to clean up for as long as this process is the
+ * registered launcher. If DataChecksumsWorkerLauncherMain has returned
+ * normally then it has already finished its work and given up that role, and
+ * another launcher might have been started since, so nothing must be touched
+ * here in that case.
*/
static void
launcher_exit(int code, Datum arg)
{
abort_requested = false;
- if (launcher_running)
+ if (!launcher_running)
+ return;
+
+ LWLockAcquire(DataChecksumsWorkerLock, LW_EXCLUSIVE);
+ if (DataChecksumState->worker_pid != InvalidPid)
{
- LWLockAcquire(DataChecksumsWorkerLock, LW_EXCLUSIVE);
- if (DataChecksumState->worker_pid != InvalidPid)
- {
- ereport(LOG,
- errmsg("data checksums launcher exiting while worker is still running, signalling worker"));
- kill(DataChecksumState->worker_pid, SIGTERM);
- DataChecksumState->worker_pid = InvalidPid;
- }
- LWLockRelease(DataChecksumsWorkerLock);
+ ereport(LOG,
+ errmsg("data checksums launcher exiting while worker is still running, signalling worker"));
+ kill(DataChecksumState->worker_pid, SIGTERM);
+ DataChecksumState->worker_pid = InvalidPid;
}
+ LWLockRelease(DataChecksumsWorkerLock);
/*
* If the launcher is exiting before data checksums are enabled then set
- * the state to off since processing cannot be resumed.
+ * the state to off since processing cannot be resumed. The shared
+ * launcher_running flag must remain set until this is done, to keep a new
+ * launcher from changing the data checksum state at the same time.
*/
if (DataChecksumsInProgressOn())
SetDataChecksumsOff();
LWLockAcquire(DataChecksumsWorkerLock, LW_EXCLUSIVE);
- if (launcher_running)
- DataChecksumState->launcher_running = false;
+ DataChecksumState->launcher_running = false;
launcher_running = false;
LWLockRelease(DataChecksumsWorkerLock);
}
@@ -1370,6 +1377,16 @@ again:
done:
+ /*
+ * If enabling data checksums was aborted, the cluster is still in the
+ * inprogress-on state. Processing cannot be resumed, so set the state
+ * back to off. This must be done before the launcher_running flag is
+ * cleared below, since a new launcher could otherwise be started and
+ * change the data checksum state concurrently with us.
+ */
+ if (DataChecksumsInProgressOn())
+ SetDataChecksumsOff();
+
/*
* This state will only be displayed for a fleeting moment, but for the
* sake of correctness it is still added before ending the command.
diff --git a/src/test/modules/test_checksums/t/005_injection.pl b/src/test/modules/test_checksums/t/005_injection.pl
index 8dcbe971466..c3a8b86f5d4 100644
--- a/src/test/modules/test_checksums/t/005_injection.pl
+++ b/src/test/modules/test_checksums/t/005_injection.pl
@@ -205,6 +205,73 @@ like(
qr/create database strategy "file_copy" not allowed/m,
'file_copy error message in log');
+# ---------------------------------------------------------------------------
+# Test that a new request cannot start a second launcher while a cancelled
+# launcher is still busy resetting the data checksum state
+#
+
+disable_data_checksums($node, wait => 1);
+$node_loglocation = -s $node->logfile;
+
+# Block the worker on a temporary table, such that the launcher can be
+# cancelled while the cluster is in inprogress-on.
+my $blocker = $node->background_psql('postgres');
+$blocker->query_safe('CREATE TEMPORARY TABLE blocker_tt (a integer);');
+enable_data_checksums($node, wait => 'inprogress-on');
+$node->poll_query_until(
+ 'postgres',
+ "SELECT wait_event FROM pg_catalog.pg_stat_activity "
+ . "WHERE backend_type = 'datachecksums worker';",
+ 'ChecksumEnableTemptableWait'
+) or die "timed out waiting for worker to wait for temporary tables";
+
+# Hold the next checkpoint, which makes the cancelled launcher stall when
+# it sets the data checksum state back to off.
+$node->safe_psql('postgres',
+ "SELECT injection_points_attach('create-checkpoint-initial','wait');");
+my $launcher_pid = $node->safe_psql('postgres',
+ "SELECT pid FROM pg_catalog.pg_stat_activity "
+ . "WHERE backend_type = 'datachecksums launcher';");
+$node->safe_psql('postgres', "SELECT pg_cancel_backend($launcher_pid);");
+$node->wait_for_event('checkpointer', 'create-checkpoint-initial');
+wait_for_checksum_state($node, 'inprogress-off');
+
+# With the temporary table gone a new launcher could run to completion. The
+# request must not start one though, since the cancelled launcher has yet to
+# finish.
+$blocker->quit;
+enable_data_checksums($node);
+$result = $node->safe_psql('postgres',
+ "SELECT pid FROM pg_catalog.pg_stat_activity "
+ . "WHERE backend_type = 'datachecksums launcher';");
+is($result, $launcher_pid,
+ 'cancelled launcher is still running while resetting state');
+
+# Let the checkpoint proceed and make sure that the cancelled launcher is the
+# only process which has altered the data checksum state.
+$node->safe_psql('postgres',
+ "SELECT injection_points_detach('create-checkpoint-initial');");
+$node->safe_psql('postgres',
+ "SELECT injection_points_wakeup('create-checkpoint-initial');");
+wait_for_checksum_state($node, 'off');
+$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";
+test_checksum_state($node, 'off');
+
+$log = PostgreSQL::Test::Utils::slurp_file($node->logfile, $node_loglocation);
+my @launches = $log =~ /enabling data checksums requested/g;
+is(scalar(@launches), 1, 'no second launcher was started');
+unlike(
+ $log,
+ qr/incorrect data checksum state/,
+ 'no invalid data checksum state transition');
+
+# Enabling data checksums must work once the cancelled launcher has exited.
+enable_data_checksums($node, wait => 'on');
+
# ---------------------------------------------------------------------------
# Test teardown
#
base-commit: 458c77d74d2bab2014e97f01df19dc805dc7b42a
--
2.53.0