Исходный код СУБД 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
# D1 shared .cfp tail: two backends alternately appending pages of one
# CFS relation through the real locked write path.
#
# Every cfs_test_tail_write() call runs in its OWN backend (a fresh psql
# invocation): the handle, its cached append cursor and the compression
# scratch are private to that process, so the only state carried between
# the alternating writers is the shared metadata slot.  With the
# authoritative tail in the slot, each writer must resume exactly where
# its peer stopped; the returned .cfp offsets are therefore strictly
# increasing.  A backend that appended at a stale per-handle cursor would
# reuse (and overwrite) a peer's record; cfs_test_tail_verify() detects
# that as duplicate offsets (-2) or corrupted read-back data (-1) and
# returns the verified block count otherwise.
 
use strict;
use warnings FATAL => 'all';
 
use PostgreSQL::Test::Cluster;
use Test::More;
 
my $node = PostgreSQL::Test::Cluster->new('cfs_tail_two_backends');
$node->init;
$node->start;
 
$node->safe_psql('postgres', 'CREATE EXTENSION test_cfs');
 
my $rel = 910501;
 
# (session, block): perfect alternation, three pages per backend.
my @steps = (
    [1, 1], [2, 2], [1, 3], [2, 4], [1, 5], [2, 6]);
 
my $prev_off = 0;
for my $step (@steps)
{
    my ($session, $block) = @$step;
 
    my $off = $node->safe_psql('postgres',
        "SELECT cfs_test_tail_write($rel, $block)");
    like($off, qr/^\d+$/,
        "session $session wrote block $block at a numeric offset");
    cmp_ok($off, '>', $prev_off,
        "session $session block $block offset $off is past peer's $prev_off");
    $prev_off = $off;
}
 
my $verified = $node->safe_psql('postgres',
    "SELECT cfs_test_tail_verify($rel, 6)");
is($verified, '6',
    'all six pages read back intact with strictly increasing offsets');
 
$node->safe_psql('postgres', "SELECT cfs_test_tail_cleanup($rel)");
 
$node->stop('immediate');
 
done_testing();