Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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();