Исходный код СУБД 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
# Deferred CFS policy publication across prepared transactions:
# a prepared ALTER queues its policy into the two-phase state file, so
# PREPARE itself publishes nothing, ROLLBACK PREPARED discards the policy,
# and COMMIT PREPARED publishes it after the durable commit — even after a
# restart that restored the prepared transaction from the on-disk state
# file.  The probes deliberately read the shared policy cache by
# relfilenode, so they never open the relation and never trigger the lazy
# publication an ordinary open would cause.
 
use strict;
use warnings FATAL => 'all';
 
use PostgreSQL::Test::Cluster;
use Test::More;
 
my $node = PostgreSQL::Test::Cluster->new('cfs_2pc_main');
$node->init;
$node->append_conf('postgresql.conf', 'max_prepared_transactions = 1');
$node->start;
 
$node->safe_psql('postgres', 'CREATE EXTENSION test_cfs');
 
$node->safe_psql('postgres',
    "CREATE TABLE cfs_2pc (i int) WITH (algorithm = 'lz4', level = 3)");
$node->safe_psql('postgres', 'INSERT INTO cfs_2pc VALUES (1)');
 
my $filenode =
  $node->safe_psql('postgres', "SELECT pg_relation_filenode('cfs_2pc')");
my $shared_level = sub {
    return $node->safe_psql('postgres',
        "SELECT cfs_test_shared_level_at($filenode)");
};
 
is($shared_level->(), '3', 'initial policy published at level 3');
 
# PREPARE must not publish anything; ROLLBACK PREPARED discards the policy.
$node->safe_psql('postgres',
        "BEGIN; ALTER TABLE cfs_2pc SET (level = 8); "
      . "PREPARE TRANSACTION 'cfs_2pc_rollback'");
is($shared_level->(), '3', 'PREPARE TRANSACTION does not publish');
$node->safe_psql('postgres', "ROLLBACK PREPARED 'cfs_2pc_rollback'");
is($shared_level->(), '3', 'ROLLBACK PREPARED keeps the old policy');
 
# COMMIT PREPARED publishes the policy recorded in the state file, without
# any relation reopen in between.
$node->safe_psql('postgres',
        "BEGIN; ALTER TABLE cfs_2pc SET (level = 8); "
      . "PREPARE TRANSACTION 'cfs_2pc_commit'");
is($shared_level->(), '3', 'still level 3 before COMMIT PREPARED');
$node->safe_psql('postgres', "COMMIT PREPARED 'cfs_2pc_commit'");
is($shared_level->(), '8', 'COMMIT PREPARED publishes the new policy');
 
# The state file survives a restart: the prepared transaction (and its
# queued policy) is restored from disk, and COMMIT PREPARED publishes it.
$node->safe_psql('postgres',
        "BEGIN; ALTER TABLE cfs_2pc SET (level = 9); "
      . "PREPARE TRANSACTION 'cfs_2pc_restart'");
$node->restart;
is($shared_level->(), '',
    'shared cache is empty after restart; prepared policy not published');
$node->safe_psql('postgres', "COMMIT PREPARED 'cfs_2pc_restart'");
is($shared_level->(), '9',
    'COMMIT PREPARED after restart publishes the restored policy');
 
# NNF-9: a deferred publication queued for a storage node that the same
# prepared transaction replaces must not be replayed by COMMIT PREPARED.
# FinishPreparedTransaction() drops the replaced node's files before the
# postcommit callbacks run (the reverse of the CommitTransaction() order),
# so a replayed entry would republish a stale policy for the dead node and
# the next checkpoint would lazily recreate its sidecar files.
$node->safe_psql('postgres',
        "BEGIN; ALTER TABLE cfs_2pc SET (level = 6); "
      . "ALTER TABLE cfs_2pc RESET (algorithm); "
      . "PREPARE TRANSACTION 'cfs_2pc_rewrite'");
is( $node->safe_psql(
        'postgres', "SELECT cfs_test_slot_present_at($filenode)"),
    't',
    'replaced node slot still cached before COMMIT PREPARED');
$node->safe_psql('postgres', "COMMIT PREPARED 'cfs_2pc_rewrite'");
is( $node->safe_psql(
        'postgres', "SELECT cfs_test_slot_present_at($filenode)"),
    'f',
    'no stale slot republished for the replaced node');
$node->safe_psql('postgres', 'CHECKPOINT');
is($node->safe_psql('postgres', "SELECT cfs_test_sidecars_at($filenode)"),
    'f', 'no sidecar files recreated for the replaced node');
is($node->safe_psql('postgres', 'SELECT count(*) FROM cfs_2pc'),
    '1', 'the rewritten table is readable');
 
$node->stop('fast');
done_testing();