Исходный код СУБД 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# Copyright (c) 2025-2026, PostgreSQL Global Development Group
 
use strict;
use warnings FATAL => 'all';
 
use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;
 
use FindBin;
use lib $FindBin::RealBin;
 
use TestAio;
 
 
my $node = PostgreSQL::Test::Cluster->new('test');
$node->init();
 
TestAio::configure($node);
 
$node->append_conf(
    'postgresql.conf', qq(
max_connections=8
io_method=worker
));
 
$node->start();
test_setup($node);
$node->stop();
 
 
foreach my $method (TestAio::supported_io_methods())
{
    $node->adjust_conf('postgresql.conf', 'io_method', $method);
    $node->start();
    test_io_method($method, $node);
    $node->stop();
}
 
done_testing();
 
 
sub test_setup
{
    my $node = shift;
 
    $node->safe_psql(
        'postgres', qq(
CREATE EXTENSION test_aio;
 
CREATE TABLE largeish(k int not null) WITH (FILLFACTOR=10);
INSERT INTO largeish(k) SELECT generate_series(1, 10000);
));
    ok(1, "setup");
}
 
 
sub test_repeated_blocks
{
    my $io_method = shift;
    my $node = shift;
 
    my $psql = $node->background_psql('postgres', on_error_stop => 0);
 
    # Preventing larger reads makes testing easier
    $psql->query_safe(qq/SET io_combine_limit = 1/);
 
    # test miss of the same block twice in a row
    $psql->query_safe(qq/SELECT evict_rel('largeish');/);
 
    # block 0 grows the distance enough that the stream will look ahead and try
    # to start a pending read for block 2 (and later block 4) twice before
    # returning any buffers.
    $psql->query_safe(
        qq/SELECT * FROM read_stream_for_blocks('largeish',
           ARRAY[0, 2, 2, 4, 4]);/);
 
    ok(1, "$io_method: stream missing the same block repeatedly");
 
    $psql->query_safe(
        qq/SELECT * FROM read_stream_for_blocks('largeish',
           ARRAY[0, 2, 2, 4, 4]);/);
    ok(1, "$io_method: stream hitting the same block repeatedly");
 
    # test hit of the same block twice in a row
    $psql->query_safe(qq/SELECT evict_rel('largeish');/);
    $psql->query_safe(
        qq/SELECT * FROM read_stream_for_blocks('largeish',
           ARRAY[0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1, 0]);/);
    ok(1, "$io_method: stream accessing same block");
 
    # Test repeated blocks with a temp table, using invalidate_rel_block()
    # to evict individual local buffers.
    $psql->query_safe(
        qq/CREATE TEMP TABLE largeish_temp(k int not null) WITH (FILLFACTOR=10);
           INSERT INTO largeish_temp(k) SELECT generate_series(1, 200);/);
 
    # Evict the specific blocks we'll request to force misses
    $psql->query_safe(qq/SELECT invalidate_rel_block('largeish_temp', 0);/);
    $psql->query_safe(qq/SELECT invalidate_rel_block('largeish_temp', 2);/);
    $psql->query_safe(qq/SELECT invalidate_rel_block('largeish_temp', 4);/);
 
    $psql->query_safe(
        qq/SELECT * FROM read_stream_for_blocks('largeish_temp',
           ARRAY[0, 2, 2, 4, 4]);/);
    ok(1, "$io_method: temp stream missing the same block repeatedly");
 
    # Now the blocks are cached, so repeated access should be hits
    $psql->query_safe(
        qq/SELECT * FROM read_stream_for_blocks('largeish_temp',
           ARRAY[0, 2, 2, 4, 4]);/);
    ok(1, "$io_method: temp stream hitting the same block repeatedly");
 
    $psql->quit();
}
 
 
sub test_inject_foreign
{
    my $io_method = shift;
    my $node = shift;
 
    my $psql_a = $node->background_psql('postgres', on_error_stop => 0);
    my $psql_b = $node->background_psql('postgres', on_error_stop => 0);
 
    my $pid_a = $psql_a->query_safe(qq/SELECT pg_backend_pid();/);
 
 
    ###
    # Test read stream encountering buffers undergoing IO in another backend,
    # with the other backend's reads succeeding.
    ###
    $psql_a->query_safe(qq/SELECT evict_rel('largeish');/);
 
    $psql_b->query_safe(
        qq/SELECT inj_io_completion_wait(pid=>pg_backend_pid(),
           relfilenode=>pg_relation_filenode('largeish'));/);
 
    $psql_b->{stdin} .= qq/SELECT read_rel_block_ll('largeish',
        blockno=>5, nblocks=>1);\n/;
    $psql_b->{run}->pump_nb();
 
    $node->poll_query_until(
        'postgres', qq/SELECT wait_event FROM pg_stat_activity
            WHERE wait_event = 'completion_wait';/,
        'completion_wait');
 
    # Block 5 is undergoing IO in session b, so session a will move on to start
    # a new IO for block 7.
    $psql_a->{stdin} .= qq/SELECT array_agg(blocknum) FROM
        read_stream_for_blocks('largeish', ARRAY[0, 2, 5, 7]);\n/;
    $psql_a->{run}->pump_nb();
 
    $node->poll_query_until('postgres',
        qq(SELECT wait_event FROM pg_stat_activity WHERE pid = $pid_a),
        'AioIoCompletion');
 
    $node->safe_psql('postgres', qq/SELECT inj_io_completion_continue()/);
 
    pump_until(
        $psql_a->{run}, $psql_a->{timeout},
        \$psql_a->{stdout}, qr/\{0,2,5,7\}/);
    $psql_a->{stdout} = '';
 
    ok(1,
        qq/$io_method: read stream encounters succeeding IO by another backend/
    );
 
    ###
    # Test read stream encountering buffers undergoing IO in another backend,
    # with the other backend's reads failing.
    ###
    $psql_a->query_safe(qq/SELECT evict_rel('largeish');/);
 
    $psql_b->query_safe(
        qq/SELECT inj_io_completion_wait(pid=>pg_backend_pid(),
           relfilenode=>pg_relation_filenode('largeish'));/);
 
    $psql_b->query_safe(
        qq/SELECT inj_io_short_read_attach(-errno_from_string('EIO'),
           pid=>pg_backend_pid(),
           relfilenode=>pg_relation_filenode('largeish'));/);
 
    $psql_b->{stdin} .= qq/SELECT read_rel_block_ll('largeish',
        blockno=>5, nblocks=>1);\n/;
    $psql_b->{run}->pump_nb();
 
    $node->poll_query_until(
        'postgres',
        qq/SELECT wait_event FROM pg_stat_activity
           WHERE wait_event = 'completion_wait';/,
        'completion_wait');
 
    $psql_a->{stdin} .= qq/SELECT array_agg(blocknum) FROM
        read_stream_for_blocks('largeish', ARRAY[0, 2, 5, 7]);\n/;
    $psql_a->{run}->pump_nb();
 
    $node->poll_query_until('postgres',
        qq(SELECT wait_event FROM pg_stat_activity WHERE pid = $pid_a),
        'AioIoCompletion');
 
    $node->safe_psql('postgres', qq/SELECT inj_io_completion_continue()/);
 
    pump_until(
        $psql_a->{run}, $psql_a->{timeout},
        \$psql_a->{stdout}, qr/\{0,2,5,7\}/);
    $psql_a->{stdout} = '';
 
    pump_until($psql_b->{run}, $psql_b->{timeout}, \$psql_b->{stderr},
        qr/ERROR.*could not read blocks 5\.\.5/);
    ok(1, "$io_method: injected error occurred");
    $psql_b->{stderr} = '';
    $psql_b->query_safe(qq/SELECT inj_io_short_read_detach();/);
 
    ok(1,
        qq/$io_method: read stream encounters failing IO by another backend/);
 
 
    ###
    # Test read stream encountering two buffers that are undergoing the same
    # IO, started by another backend.
    ###
    $psql_a->query_safe(qq/SELECT evict_rel('largeish');/);
 
    $psql_b->query_safe(
        qq/SELECT inj_io_completion_wait(pid=>pg_backend_pid(),
           relfilenode=>pg_relation_filenode('largeish'));/);
 
    $psql_b->{stdin} .= qq/SELECT read_rel_block_ll('largeish',
        blockno=>2, nblocks=>3);\n/;
    $psql_b->{run}->pump_nb();
 
    $node->poll_query_until(
        'postgres',
        qq/SELECT wait_event FROM pg_stat_activity
            WHERE wait_event = 'completion_wait';/,
        'completion_wait');
 
    # Blocks 2 and 4 are undergoing IO initiated by session b
    $psql_a->{stdin} .= qq/SELECT array_agg(blocknum) FROM
        read_stream_for_blocks('largeish', ARRAY[0, 2, 4]);\n/;
    $psql_a->{run}->pump_nb();
 
    $node->poll_query_until('postgres',
        qq(SELECT wait_event FROM pg_stat_activity WHERE pid = $pid_a),
        'AioIoCompletion');
 
    $node->safe_psql('postgres', qq/SELECT inj_io_completion_continue()/);
 
    pump_until(
        $psql_a->{run}, $psql_a->{timeout},
        \$psql_a->{stdout}, qr/\{0,2,4\}/);
    $psql_a->{stdout} = '';
 
    ok(1, qq/$io_method: read stream encounters two buffer read in one IO/);
 
    $psql_a->quit();
    $psql_b->quit();
}
 
 
sub test_io_method
{
    my $io_method = shift;
    my $node = shift;
 
    is($node->safe_psql('postgres', 'SHOW io_method'),
        $io_method, "$io_method: io_method set correctly");
 
    test_repeated_blocks($io_method, $node);
 
  SKIP:
    {
        skip 'Injection points not supported by this build', 1
          unless $ENV{enable_injection_points} eq 'yes';
        test_inject_foreign($io_method, $node);
    }
}