From e9e2b506b957ec9b49f63196e72feddd972ed1d4 Mon Sep 17 00:00:00 2001 From: Sai Pranav Date: Sun, 22 Nov 2026 13:56:58 +0000 Subject: [PATCH v2 2/2] pg_combinebackup: Accept a final backup taken during a checksum transition check_control_files() refused a chain whenever the final backup's data checksum state was anything but "off" and an earlier backup's state differed. That also refused a final backup taken while checksums were still being enabled or disabled online. Such a chain is safe: if recovery of the combined backup ends before the transition completes, the state goes back to "off" and nothing is verified, and if it replays the rest of the transition, every page is written again with a full-page image and a valid checksum. Only refuse the chain when the final backup has checksums fully enabled, and add a test for a final backup taken while checksums are being enabled. Suggested-by: shihao zhong --- doc/src/sgml/ref/pg_combinebackup.sgml | 4 +- src/bin/pg_combinebackup/pg_combinebackup.c | 7 ++- .../t/024_combinebackup_mixed.pl | 58 ++++++++++++++++++- 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/doc/src/sgml/ref/pg_combinebackup.sgml b/doc/src/sgml/ref/pg_combinebackup.sgml index ddc28a3bd9..c962f7126d 100644 --- a/doc/src/sgml/ref/pg_combinebackup.sgml +++ b/doc/src/sgml/ref/pg_combinebackup.sgml @@ -313,7 +313,9 @@ PostgreSQL documentation would fail checksum verification as soon as it reads them. Take a new full backup once data checksums have been enabled, whether offline with or online (see - ). + ). A chain whose final + backup was taken while checksums were still being enabled or disabled is + accepted. diff --git a/src/bin/pg_combinebackup/pg_combinebackup.c b/src/bin/pg_combinebackup/pg_combinebackup.c index 589b5c8e79..56009dffbd 100644 --- a/src/bin/pg_combinebackup/pg_combinebackup.c +++ b/src/bin/pg_combinebackup/pg_combinebackup.c @@ -650,11 +650,14 @@ check_control_files(int n_backups, char **backup_dirs) /* * Detect checksum mismatches, but only if the last backup in the - * chain has checksums enabled. + * chain has checksums enabled. If checksums were still being + * enabled or disabled in the last backup, recovery either ends with + * them off, or replays the rest of the transition, which writes every + * page with a full-page image and a checksum. */ if (i == n_backups - 1) data_checksum_version = control_file->data_checksum_version; - else if (data_checksum_version != PG_DATA_CHECKSUM_OFF && + else if (data_checksum_version == PG_DATA_CHECKSUM_VERSION && data_checksum_version != control_file->data_checksum_version) data_checksum_mismatch = true; diff --git a/src/test/modules/test_checksums/t/024_combinebackup_mixed.pl b/src/test/modules/test_checksums/t/024_combinebackup_mixed.pl index 2f51c7fd9d..bb94e0119b 100644 --- a/src/test/modules/test_checksums/t/024_combinebackup_mixed.pl +++ b/src/test/modules/test_checksums/t/024_combinebackup_mixed.pl @@ -141,6 +141,62 @@ unlike( 'no checksum verification failures in the combined backup'); $restored->stop('immediate'); -$node->stop; + +# An incremental backup taken while checksums are still being enabled online +# is accepted: when recovery ends before the enable completes, checksums go +# back to "off", so the blocks inherited from the full backup are never +# verified. +$node->backup('full3'); + +# Slow enough that the backup below is taken in the middle of it. +enable_data_checksums( + $node, + cost_delay => 100, + cost_limit => 1, + wait => 'inprogress-on'); + +$node->safe_psql('postgres', + "CREATE TABLE t4 AS SELECT generate_series(1,1000) AS a;"); + +$node->command_ok( + [ + 'pg_basebackup', + '--pgdata' => $node->backup_dir . '/incr3', + '--dbname' => $node->connstr('postgres'), + '--no-sync', + '--checkpoint' => 'fast', + '--incremental' => $node->backup_dir . '/full3/backup_manifest', + ], + 'incremental backup while checksums are being enabled'); + +my $restored2 = PostgreSQL::Test::Cluster->new('restored2'); +$restored2->init_from_backup( + $node, 'incr3', + combine_with_prior => ['full3'], + has_restoring => 1, + standby => 0); +# The node "restored" above may have archived a newer timeline into the +# shared archive; stay on the timeline of the backup. +$restored2->append_conf('postgresql.conf', + "recovery_target_timeline = 'current'"); +$restored2->start; + +# The state goes back to "off" only at the end of recovery. +$restored2->poll_query_until('postgres', 'SELECT NOT pg_is_in_recovery();') + or die "timed out waiting for the end of recovery"; + +($rc, $stdout, $stderr) = $restored2->psql('postgres', + "SELECT setting FROM pg_settings WHERE name = 'data_checksums';"); +is($rc, 0, 'combined in-progress backup accepts connections') + or diag("stderr: $stderr"); +is($stdout, 'off', 'interrupted enable leaves the combined backup off'); + +($rc, $stdout, $stderr) = $restored2->psql('postgres', + 'SELECT (SELECT count(*) FROM t1) + (SELECT count(*) FROM t4);'); +is($stdout, '201000', 'all blocks of the combined in-progress backup are readable') + or diag("stderr: $stderr"); + +$restored2->stop('immediate'); +$node->stop('immediate'); done_testing(); -- 2.50.1 (Apple Git-155)