# Copyright (c) 2026, Kirillov Vyacheslav by AI, QDB Development Group # # pg_verifybackup corruption detection for CFS sidecar files. # # The backup manifest covers the CFS sidecars (.cfp/.cfsmap/.cfsjrn) # like any other file, so mutilating them must be detected: content # replacement (checksum mismatch), append/truncate (size mismatch), # deletion (missing file) and an extra sidecar-like file (not in the # manifest). The core's mutilate_* helpers are file-agnostic; here the # same matrix is pointed at the sidecar files of a CFS table. use strict; use warnings FATAL => 'all'; use File::Path qw(rmtree); use PostgreSQL::Test::Cluster; use PostgreSQL::Test::Utils; use Test::More; my $primary = PostgreSQL::Test::Cluster->new('primary'); $primary->init(allows_streaming => 1); $primary->start; # A CFS table with real compressed content. $primary->safe_psql( 'postgres', q[CREATE TABLE cfs_t (id int, payload text) WITH (algorithm = 'lz4'); INSERT INTO cfs_t SELECT g, md5(g::text) FROM generate_series(1, 300) g; CHECKPOINT;]); my $rel_path = $primary->safe_psql('postgres', "SELECT pg_relation_filepath('cfs_t')"); my @scenario = ( { 'name' => 'replace_cfp', 'target_suffix' => '.cfp', 'mutilate' => 'replace', 'fails_like' => qr/checksum mismatch for file/, }, { 'name' => 'append_cfsjrn', 'target_suffix' => '.cfsjrn', 'mutilate' => 'append', 'fails_like' => qr/has size \d+ (on disk|in archive "[^"]+") but size \d+ in the manifest/, }, { 'name' => 'truncate_cfsmap', 'target_suffix' => '.cfsmap', 'mutilate' => 'truncate', 'fails_like' => qr/has size 0 (on disk|in archive "[^"]+") but size \d+ in the manifest/, }, { 'name' => 'missing_cfsmap', 'target_suffix' => '.cfsmap', 'mutilate' => 'missing', 'fails_like' => qr/present in the manifest but not (on disk|in archive "[^"]+")/, }, { 'name' => 'extra_cfp_segment', 'target_suffix' => '.cfp.99', 'mutilate' => 'extra', 'fails_like' => qr/present (on disk|in archive "[^"]+") but not in the manifest/, },); for my $scenario (@scenario) { my $name = $scenario->{'name'}; my $target = $rel_path . $scenario->{'target_suffix'}; # Take a backup and check that it verifies OK. my $backup_path = $primary->backup_dir . '/' . $name; $primary->command_ok( [ 'pg_basebackup', '--pgdata' => $backup_path, '--no-sync', '--checkpoint' => 'fast', ], "base backup ok for $name"); command_ok( [ 'pg_verifybackup', $backup_path ], "intact backup verified for $name"); # Mutilate the sidecar file in the backup. my $file = "$backup_path/$target"; if ($scenario->{'mutilate'} eq 'replace') { # Corrupt the contents without changing the size. open(my $fh, '+<:raw', $file) or die "open $file: $!"; my $buf; read($fh, $buf, 1024); substr($buf, 0, 1) = chr(ord(substr($buf, 0, 1)) ^ 0xFF); seek($fh, 0, 0); print $fh $buf; close($fh); } elsif ($scenario->{'mutilate'} eq 'append') { PostgreSQL::Test::Utils::append_to_file($file, "junk" x 40); } elsif ($scenario->{'mutilate'} eq 'truncate') { open(my $fh, '>', $file) or die "open $file: $!"; close($fh); } elsif ($scenario->{'mutilate'} eq 'missing') { unlink($file) or die "unlink $file: $!"; } elsif ($scenario->{'mutilate'} eq 'extra') { PostgreSQL::Test::Utils::append_to_file($file, "extra segment\n"); } # Now check that the backup no longer verifies. command_fails_like( [ 'pg_verifybackup', $backup_path ], $scenario->{'fails_like'}, "corrupt backup fails verification: $name"); # Use rmtree to reclaim space. rmtree($backup_path); } done_testing();