Исходный код СУБД 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# Copyright (c) 2026, Kirillov Vyacheslav by AI, QDB Development Group
#
# QDB-1513 P4-2: the offloaded CFS read path (io_method=worker plus the
# shared read scratch arena).  Server-level behaviour that needs a custom
# configuration and therefore cannot live in the regression schedule:
#
# 1. cfs_read_scratch_mb is an observable postmaster GUC, and 0 keeps the
#    server fully functional on the inline path.
# 2. Bit-identical data: the same cold table read through the offload
#    configuration and through the inline one produces the same md5, and
#    the offload configuration really batched (pg_stat_cfs counters).
# 3. A worker dying right after the reopen (the aio-worker-after-reopen
#    injection) fails the in-flight read through the AIO result path
#    ("could not read block"), the server stays up, and a retry after the
#    detach succeeds — the transient-error contract of the compaction
#    race handling.  Requires --enable-injection-points; skipped here
#    otherwise (the verify stand runs it).
# 4. A truncated .cfp tail must surface as a corruption ERROR through
#    cfs_report_corruption() ("checksum or format corruption"), not as an
#    internal IO error — the EOF error currency of the inline span path.
 
use strict;
use warnings FATAL => 'all';
 
use PostgreSQL::Test::Cluster;
use Test::More;
 
# Check the live processes, not just the configured worker limits.
sub check_io_workers
{
    my ($node) = @_;
    ok($node->poll_query_until('postgres',
        q{SELECT count(*) = 2 FROM pg_stat_activity
          WHERE backend_type = 'io worker'}),
        'two I/O workers are running');
}
 
my $node = PostgreSQL::Test::Cluster->new('cfs_scratch');
$node->init;
$node->append_conf('postgresql.conf', qq{
io_method = worker
io_min_workers = 2
io_max_workers = 2
cfs_read_scratch_mb = 32
autovacuum = off
});
$node->start;
check_io_workers($node);
 
is($node->safe_psql('postgres', 'SHOW io_min_workers'), '2', 'minimum I/O workers');
is($node->safe_psql('postgres', 'SHOW io_max_workers'), '2', 'maximum I/O workers');
 
my $setting = $node->safe_psql('postgres',
    "SELECT setting FROM pg_settings WHERE name = 'cfs_read_scratch_mb'");
is($setting, '32', 'arena enabled at server start');
 
# One compact lz4 heap: a bulk INSERT appends records back-to-back, so a
# cold seqscan meets full runs and the read stream batches them.
$node->safe_psql('postgres',
    "CREATE TABLE sc_lz4 (id int, payload text) WITH (algorithm = 'lz4')");
$node->safe_psql('postgres',
    "INSERT INTO sc_lz4 SELECT g, repeat('sc ', 60) FROM generate_series(1, 5000) g");
$node->safe_psql('postgres', 'CHECKPOINT');
my $md5_expected = $node->safe_psql('postgres',
    "SELECT md5(string_agg(payload, '' ORDER BY id)) FROM sc_lz4");
 
# The offloaded read: cold restart so every page comes from the sidecars
# through read_stream -> smgrstartreadv -> the scratch arena.
$node->restart;
check_io_workers($node);
my $md5_offload = $node->safe_psql('postgres',
    "SELECT md5(string_agg(payload, '' ORDER BY id)) FROM sc_lz4");
is($md5_offload, $md5_expected, 'offloaded read is bit-identical');
 
my $batched = $node->safe_psql('postgres',
    "SELECT read_batched_pages > 0 AND read_count = read_batched_pages + read_fallback_pages
     FROM pg_stat_cfs WHERE relid = 'sc_lz4'::regclass");
diag("offload stats: " . $node->safe_psql('postgres',
    "SELECT read_count || ' read, ' || read_batched_pages || ' batched, ' ||
            read_fallback_pages || ' fallback, io_method=' ||
            (SELECT setting FROM pg_settings WHERE name = 'io_method')
     FROM pg_stat_cfs WHERE relid = 'sc_lz4'::regclass"));
is($batched, 't', 'offload configuration batched the reads');
 
# Worker dies right after reopening the segment: the read must fail
# through the AIO result path with a retryable error, not crash.
subtest 'worker reopen failure is a transient read error' => sub {
    plan skip_all => 'Injection points not supported by this build'
        if $ENV{enable_injection_points} ne 'yes';
 
    if (!$node->check_extension('injection_points'))
    {
        plan skip_all => 'Extension injection_points not installed';
    }
 
    $node->safe_psql('postgres',
        'CREATE EXTENSION IF NOT EXISTS injection_points');
 
    # Cold again, so the scan actually goes through the workers.
    $node->restart;
    check_io_workers($node);
    $node->safe_psql('postgres',
        "SELECT injection_points_attach('aio-worker-after-reopen', 'error')");
 
    my ($ret, $out, $stderr) = $node->psql('postgres',
        'SELECT count(*) FROM sc_lz4');
    isnt($ret, 0, 'injected reopen failure fails the scan');
    like($stderr, qr/could not read block/,
        'reopen failure reports through the AIO result path');
 
    $node->safe_psql('postgres',
        "SELECT injection_points_detach('aio-worker-after-reopen')");
    $node->restart;
    check_io_workers($node);
 
    my $md5_retry = $node->safe_psql('postgres',
        "SELECT md5(string_agg(payload, '' ORDER BY id)) FROM sc_lz4");
    is($md5_retry, $md5_expected, 'retry after the failure is bit-identical');
};
 
# The inline comparison: same data, arena off.  The restart both switches
# the mode and makes the buffers cold.
$node->stop;
$node->append_conf('postgresql.conf', 'cfs_read_scratch_mb = 0');
$node->start;
check_io_workers($node);
$setting = $node->safe_psql('postgres',
    "SELECT setting FROM pg_settings WHERE name = 'cfs_read_scratch_mb'");
is($setting, '0', 'arena disabled at 0 MB');
my $md5_inline = $node->safe_psql('postgres',
    "SELECT md5(string_agg(payload, '' ORDER BY id)) FROM sc_lz4");
is($md5_inline, $md5_expected, 'inline read matches the offloaded one');
 
# A truncated .cfp tail (a partial copy, a crashed grow) shortens the
# span preadv: that is corruption, not an IO error.
my $relpath = $node->safe_psql('postgres',
    q{SELECT pg_relation_filepath('sc_lz4')});
$node->stop;
 
# The truncated-tail part works directly on the data files: this fork's
# test cluster keeps the path in data_dir(), not in a pgdata field.
my @segs = sort glob($node->data_dir . '/' . $relpath . '.cfp*');
ok(@segs > 0, 'sidecar segments found') or BAIL_OUT('no .cfp sidecars');
# The chain's last segment carries the highest suffix (a bare .cfp sorts
# first); truncating its tail cuts records the map still references.
my $last = $segs[-1];
my $size = -s $last;
ok(truncate($last, $size - 8192), 'truncated the segment tail');
 
# Back to the offload configuration for the short-read path.
$node->append_conf('postgresql.conf', 'cfs_read_scratch_mb = 32');
$node->start;
check_io_workers($node);
 
my ($ret, $out, $stderr) = $node->psql('postgres',
    'SELECT count(*) FROM sc_lz4');
isnt($ret, 0, 'truncated tail fails the scan');
like($stderr, qr/checksum or format corruption/,
    'short span read reports corruption, not an IO error');
 
# The failed offload span must run the same pool bookkeeping as the
# inline path: the crc failure is counted, so the counters observable
# in pg_stat_cfs stay identical between the two paths.
my $crc = $node->safe_psql('postgres',
    "SELECT crc_failures > 0 FROM pg_stat_cfs WHERE relid = 'sc_lz4'::regclass");
is($crc, 't', 'short span read counts the crc failure');
 
$node->stop;
done_testing();