# Copyright (c) 2026, Kirillov Vyacheslav by AI, QDB Development Group # # pg_checksums on a cluster containing CFS relations. # # pg_checksums scans relation files as raw BLCKSZ pages; the CFS sidecar # files (.cfp/.cfsmap/.cfsjrn) do not fit that format, so the tool skips # them lexically and reports "CFS sidecars skipped: N". The test # verifies that on a cluster with CFS tables: # # * --check passes with no spurious corruption reports and the sidecar # skip counter is non-zero; # * a corrupted byte inside a .cfp file is NOT reported by --check # (sidecars carry their own CRCs and are intentionally out of scope # for page checksums); # * corruption of a plain table's page is still detected, with the # CFS tables present; # * --disable/--enable round-trips work on such a cluster. use strict; use warnings FATAL => 'all'; use PostgreSQL::Test::Cluster; use PostgreSQL::Test::Utils; use Test::More; my $node = PostgreSQL::Test::Cluster->new('main'); $node->init; $node->start; my $pgdata = $node->data_dir; # Run pg_checksums --check on the test cluster and verify the exit code # plus stdout/stderr patterns. sub check_checksums { my ($exit_code, $stdout, $stderr, $desc) = @_; command_checks_all([ 'pg_checksums', '--check', '--pgdata' => $pgdata ], $exit_code, $stdout, $stderr, $desc); } # CFS tables (both algorithms) and a plain table. $node->safe_psql( 'postgres', q[CREATE TABLE cfs_lz4 (id int, payload text) WITH (algorithm = 'lz4'); INSERT INTO cfs_lz4 SELECT g, md5(g::text) FROM generate_series(1, 300) g; CREATE TABLE cfs_zstd (id int, payload text) WITH (algorithm = 'zstd'); INSERT INTO cfs_zstd SELECT g, md5(g::text) FROM generate_series(1, 300) g; CREATE TABLE plain_t AS SELECT g FROM generate_series(1, 10000) g; ALTER TABLE plain_t SET (autovacuum_enabled = false); CHECKPOINT;]); my $cfs_filepath = $node->safe_psql('postgres', "SELECT pg_relation_filepath('cfs_lz4')"); my $plain_filepath = $node->safe_psql('postgres', "SELECT pg_relation_filepath('plain_t')"); $node->stop; # Sanity: sidecar files really exist for the CFS table. ok(-f "$pgdata/$cfs_filepath.cfp", '.cfp exists'); ok(-f "$pgdata/$cfs_filepath.cfsmap", '.cfsmap exists'); ok(-f "$pgdata/$cfs_filepath.cfsjrn", '.cfsjrn exists'); # --check passes, sidecars are skipped, no spurious corruption. check_checksums( 0, [ qr/CFS sidecars skipped: [1-9]/, qr/Bad checksums: +0/ ], [qr/^$/], '--check passes on cluster with CFS relations'); # Flip one byte in the middle of the .cfp file. pg_checksums must keep # ignoring it: sidecars are out of scope for page checksums (they carry # their own CRC32C per record). my $cfp = "$pgdata/$cfs_filepath.cfp"; open(my $fh, '+<:raw', $cfp) or die "cannot open $cfp: $!"; seek($fh, 1024, 0) or die "cannot seek in $cfp: $!"; my $byte; read($fh, $byte, 1) == 1 or die "cannot read $cfp: $!"; seek($fh, 1024, 0) or die "cannot seek in $cfp: $!"; print $fh chr(ord($byte) ^ 0xFF); close($fh); check_checksums(0, [qr/Bad checksums: +0/], [qr/^$/], 'corrupted .cfp byte is not reported by --check'); # Corruption of a plain table's page is still detected with the CFS # tables present. $node->corrupt_page_checksum($plain_filepath, 0); check_checksums( 1, [qr/Bad checksums:.*1/], [qr/checksum verification failed/], 'corrupted plain page is still detected'); # Disable/enable round-trip works (this also rewrites the corrupted # page's checksum). command_ok( [ 'pg_checksums', '--disable', '--pgdata' => $pgdata ], '--disable works on cluster with CFS relations'); command_ok( [ 'pg_checksums', '--enable', '--pgdata' => $pgdata ], '--enable works on cluster with CFS relations'); check_checksums(0, [ qr/CFS sidecars skipped: [1-9]/, qr/Bad checksums: +0/ ], [qr/^$/], '--check passes after enable'); done_testing();