# QDB-1507 (U8): structural corruption of the .cfsmap prologue and of the
|
# .cfsjrn header read by a backend (not by the startup scan - that path
|
# keeps its recovery semantics) must be routed through
|
# cfs_report_corruption(), so the cfs_checksum_failure GUC decides the
|
# severity. With the GUC set to 'panic', a backend read of a truncated
|
# page map snapshot and of a journal with a corrupted header must PANIC
|
# the server; before the fix both sites raised a plain ERROR regardless
|
# of the GUC (the journal-header site did so from the full replay path
|
# even after the segment path was routed).
|
#
|
# The corruption is applied while the server runs and exercised through
|
# pg_cfs_compact(), which opens a fresh storage handle (and thus the map
|
# snapshot and the journal) in the calling backend - a restart would
|
# instead hit the startup fold, whose FATAL semantics stay unchanged.
|
|
use strict;
|
use warnings FATAL => 'all';
|
use PostgreSQL::Test::Cluster;
|
use PostgreSQL::Test::Utils;
|
use Test::More;
|
plan tests => 6;
|
{
|
my $node = PostgreSQL::Test::Cluster->new('cfs_map_panic');
|
$node->init;
|
$node->append_conf('postgresql.conf', 'cfs_checksum_failure = panic');
|
$node->append_conf('postgresql.conf', 'restart_after_crash = off');
|
$node->start;
|
|
$node->safe_psql(
|
'postgres', qq{
|
CREATE TABLE mc_t (id int, v text) WITH (algorithm='lz4');
|
CREATE TABLE mc_j (id int, v text) WITH (algorithm='lz4');
|
INSERT INTO mc_t
|
SELECT g, repeat('m' || g, 40) FROM generate_series(1, 500) g;
|
INSERT INTO mc_j
|
SELECT g, repeat('j' || g, 40) FROM generate_series(1, 500) g;
|
CHECKPOINT;
|
});
|
my $fn_t = $node->safe_psql('postgres',
|
q{SELECT pg_relation_filenode('mc_t')});
|
my $fn_j = $node->safe_psql('postgres',
|
q{SELECT pg_relation_filenode('mc_j')});
|
my $dboid = $node->safe_psql('postgres',
|
q{SELECT oid FROM pg_database WHERE datname = 'postgres'});
|
|
# Truncate the page map snapshot below its 8-byte prologue while the
|
# server is up: only a backend read (below) can hit it now.
|
my $map_path = $node->data_dir . "/base/$dboid/$fn_t.cfsmap";
|
ok(-f $map_path, 'page map snapshot exists');
|
open(my $fh, '>', $map_path) or die "could not open $map_path: $!";
|
print $fh 'xxxx';
|
close($fh);
|
|
|
my $off = -s $node->logfile;
|
my ($ret, $stdout, $stderr) = $node->psql('postgres', "SELECT pg_cfs_compact('mc_t')");
|
isnt($ret, 0, 'map: compact fails');
|
$node->log_check('map: new specific PANIC', $off, log_like => [qr/PANIC:.*checksum or format corruption[^\n]*\n[^\n]*truncated header/s]);
|
$node->wait_for_log(qr/database system is shut down/, $off);
|
|
}
|
{
|
my $node = PostgreSQL::Test::Cluster->new('cfs_journal_panic');
|
$node->init;
|
$node->append_conf('postgresql.conf', 'cfs_checksum_failure = panic');
|
$node->append_conf('postgresql.conf', 'restart_after_crash = off');
|
$node->start;
|
|
$node->safe_psql(
|
'postgres', qq{
|
CREATE TABLE mc_t (id int, v text) WITH (algorithm='lz4');
|
CREATE TABLE mc_j (id int, v text) WITH (algorithm='lz4');
|
INSERT INTO mc_t
|
SELECT g, repeat('m' || g, 40) FROM generate_series(1, 500) g;
|
INSERT INTO mc_j
|
SELECT g, repeat('j' || g, 40) FROM generate_series(1, 500) g;
|
CHECKPOINT;
|
});
|
my $fn_t = $node->safe_psql('postgres',
|
q{SELECT pg_relation_filenode('mc_t')});
|
my $fn_j = $node->safe_psql('postgres',
|
q{SELECT pg_relation_filenode('mc_j')});
|
my $dboid = $node->safe_psql('postgres',
|
q{SELECT oid FROM pg_database WHERE datname = 'postgres'});
|
|
# Corrupt the journal header magic in place (runtime, same reason).
|
my $jrn_path = $node->data_dir . "/base/$dboid/$fn_j.cfsjrn";
|
ok(-f $jrn_path, 'journal file exists');
|
open(my $jf, '+<', $jrn_path) or die "could not open $jrn_path: $!";
|
binmode($jf);
|
read($jf, my $hdr, 8);
|
seek($jf, 0, 0);
|
print $jf pack('C8', map { 0xFF } 1 .. 8);
|
close($jf);
|
|
|
my $off = -s $node->logfile;
|
my ($ret, $stdout, $stderr) = $node->psql('postgres', "SELECT pg_cfs_compact('mc_j')");
|
isnt($ret, 0, 'journal: compact fails');
|
$node->log_check('journal: new specific PANIC', $off, log_like => [qr/PANIC:.*checksum or format corruption[^\n]*\n[^\n]*invalid \.cfsjrn header/s]);
|
$node->wait_for_log(qr/database system is shut down/, $off);
|
|
}
|
|
done_testing();
|