Исходный код СУБД 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
# Copyright (c) 2026, QDB Development Group
#
# src/test/modules/test_cfs/t/004_cfs_journal_header.pl
#
# The .cfsjrn header carries the monotone generation watermark.
# Every journal reset - the checkpoint truncate, the compaction swap - must
# stamp it, and it must never regress.
 
use strict;
use warnings FATAL => 'all';
 
use PostgreSQL::Test::Utils;
use PostgreSQL::Test::Cluster;
use Test::More;
 
my $node = PostgreSQL::Test::Cluster->new('cfs_jrn_header');
$node->init;
$node->start;
 
if (!check_pg_config("#define USE_LZ4"))
{
    plan skip_all =>
      'LZ4 support not built, skipping CFS journal header test';
}
 
$node->safe_psql('postgres', 'CREATE EXTENSION test_cfs');
 
# A table with mixed generations: fresh rows, rewritten rows and holes.
$node->safe_psql(
    'postgres', q{
    CREATE TABLE jt (g int, payload text) WITH (algorithm = 'lz4');
    INSERT INTO jt
        SELECT g, repeat(md5(g::text), 4)
        FROM generate_series(1, 200) g;
    UPDATE jt SET payload = repeat(md5(g::text || 'u'), 4)
        WHERE g <= 20;
    DELETE FROM jt WHERE g > 180;
});
$node->safe_psql('postgres', 'CHECKPOINT');
 
my $filenode =
  $node->safe_psql('postgres', "SELECT pg_relation_filenode('jt')");
 
sub journal_header
{
    my $row = $node->safe_psql('postgres',
            "SELECT incarnation, max_generation, map_max_generation"
          . " FROM cfs_test_journal_header($filenode)");
    my @f = split qr/\|/, $row;
    return @f;
}
 
my ($inc, $wm, $map_wm) = journal_header();
 
cmp_ok($wm, '>=', $map_wm,
    'checkpoint watermark covers every map row generation');
 
# Plain writes append journal records but must not touch the header: the
# watermark is rewritten by resets only.
$node->safe_psql(
    'postgres',
    "INSERT INTO jt SELECT g, repeat(md5(g::text), 4)
     FROM generate_series(201, 260) g");
my ($inc_w, $wm_w) = journal_header();
 
is($inc_w, $inc, 'writes without a checkpoint leave the incarnation alone');
is($wm_w, $wm, 'writes without a checkpoint leave the watermark alone');
 
# The next checkpoint folds the new writes in: the watermark does not
# regress.
$node->safe_psql('postgres', 'CHECKPOINT');
my ($inc2, $wm2, $map_wm2) = journal_header();
 
cmp_ok($wm2, '>=', $wm, 'watermark is monotone across checkpoints');
 
# Compaction swaps the (.cfp, .cfsmap) pair and resets the journal: the
# fresh header must keep the monotone watermark.
$node->safe_psql('postgres',
    "UPDATE jt SET payload = repeat(md5(g::text || 'v'), 4)");
$node->safe_psql('postgres', 'CHECKPOINT');
$node->safe_psql('postgres', "SELECT pg_cfs_compact('jt')");
 
my ($inc3, $wm3, $map_wm3) = journal_header();
 
cmp_ok($wm3, '>=', $wm2, 'compaction preserves the generation watermark');
 
# The header watermark survives a restart, and the next checkpoint keeps
# it monotone.
$node->restart;
my ($inc_r, $wm_r, $map_wm_r) = journal_header();
is($wm_r, $wm3, 'the header watermark survives a restart');
$node->safe_psql('postgres', 'CHECKPOINT');
my ($inc4, $wm4, $map_wm4) = journal_header();
cmp_ok($wm4, '>=', $wm3, 'the watermark is monotone across the restart');
 
# Dead-space counters are checkpoint-exact: the write path accounts
# nothing between checkpoints and the fold counts every superseded row.
# dead_bytes is an advisory phase-1 snapshot, so it is checked for
# reflecting the rewrite, not for an exact byte total.  The exact total
# (cfp_size - base - live) would need the live-byte count the format no
# longer stores; deriving it here would mean parsing the binary map rows
# in Perl, which couples the test to the on-disk layout for no extra
# coverage.  dead_pages below is the exact check.  The folded-row count
# depends on hint bits / full-page writes, so the expectation is a range
# bounded by what the journal actually carried: at least one folded record,
# and never more than the records that existed before the checkpoint (no
# row is folded twice).
$node->safe_psql(
    'postgres', q{
    CREATE TABLE dead_t (g int, payload text) WITH (algorithm = 'lz4');
    INSERT INTO dead_t
        SELECT g, repeat(md5(g::text), 4)
        FROM generate_series(1, 400) g;
    CHECKPOINT;
});
 
my $rows0 = $node->safe_psql('postgres',
        "SELECT checkpoint_map_rows_written FROM pg_stat_cfs"
      . " WHERE relname = 'dead_t'");
my $dead_path =
  $node->safe_psql('postgres', "SELECT pg_relation_filepath('dead_t')");
$node->safe_psql(
    'postgres',
    "UPDATE dead_t SET payload = repeat(md5(g::text || 'd'), 4)
     WHERE g = 1");
 
# The journal of the rewritten relation: the assertions below prove the
# checkpoint consumed it completely.
my $dead_jrn = $node->data_dir . "/$dead_path.cfsjrn";
 
# The logical size before the rewrite; the truncation subtest below
# compares against it.
my $nblocks_dead = int(
    $node->safe_psql('postgres',
        "SELECT logical_size FROM pg_cfs_relation_size('dead_t')") / 8192);
 
$node->safe_psql('postgres', 'CHECKPOINT');
 
my $dead_filenode =
  $node->safe_psql('postgres', "SELECT pg_relation_filenode('dead_t')");
my ($dead_pages, $dead_bytes, $rows_written) = split qr/\|/,
  $node->safe_psql(
    'postgres',
    "SELECT dead_pages, dead_bytes, checkpoint_map_rows_written"
      . " FROM pg_stat_cfs WHERE relname = 'dead_t'");
 
cmp_ok($rows_written - $rows0,
    '>=', 1, 'the checkpoint folded the records the journal carried');
# Records whose pages the checkpoint itself flushed (hint bits) land in
# the journal after the sample above, so a pre-sampled count cannot bound
# the fold.  The exact bound is the reset: every record is consumed
# exactly once and the journal comes back as a bare header.
is(-s $dead_jrn, 16,
    'the checkpoint consumed the whole journal and reset it');
is( $dead_pages,
    $rows_written - $rows0,
    'dead_pages counts exactly the superseded rows');
cmp_ok($dead_bytes, '>', 0, 'dead_bytes reflects the rewritten pages');
 
# An immediate sync is a full-snapshot writer too: its
# journal reset must bump the incarnation and stamp the header from the
# map state it just persisted.
my $inc_before = $node->safe_psql('postgres',
    "SELECT incarnation FROM cfs_test_journal_header($dead_filenode)");
my $wm_before = $node->safe_psql('postgres',
    "SELECT max_generation FROM cfs_test_journal_header($dead_filenode)");
$node->safe_psql('postgres', "SELECT cfs_test_immedsync('dead_t')");
my ($inc_after, $wm_after) = split qr/\|/,
  $node->safe_psql('postgres',
        "SELECT incarnation, max_generation"
      . " FROM cfs_test_journal_header($dead_filenode)");
cmp_ok($inc_after, '>', $inc_before,
    'the immedsync reset bumps the incarnation');
cmp_ok($wm_after, '>=', $wm_before,
    'the immedsync preserves the generation watermark');
 
# The immedsync replaced the .cfsmap inode without bumping the shared
# generation.  A backend that already holds a cached handle must notice the
# swap before it journals a RESIZE: ftruncating the replaced inode would
# leave the new map file carrying its pre-truncation size.
#
# The truncation is driven through cfs_test_smgr_truncate() rather than
# VACUUM: SQL TRUNCATE assigns a new relfilenode (and new sidecars), while
# VACUUM reads every page on its way to RelationTruncate(), and that page
# resolution reopens the handle through the segment-pool swap check.  The
# wrapper reaches smgrtruncate() with session A's cached handle intact,
# which is exactly what cfs_smgr_truncate() must defend against.
my $bg = $node->background_psql('postgres', timeout => 60);
$bg->query_safe("SELECT count(*) FROM dead_t");
$node->safe_psql('postgres', "SELECT cfs_test_immedsync('dead_t')");
$bg->query_safe("SELECT cfs_test_smgr_truncate('dead_t', 1)");
 
my $nblocks = int(
    $node->safe_psql('postgres',
        "SELECT logical_size FROM pg_cfs_relation_size('dead_t')") / 8192);
cmp_ok($nblocks, '<', $nblocks_dead,
    "the truncation shrank the relation: $nblocks_dead -> $nblocks blocks");
# The truncation only journals the RESIZE: the physical truncation is the
# checkpoint's job (cfs_truncate runs inside RelationTruncate()'s critical
# section), so the map file keeps its dead tail for now.
my $map_size = -s $node->data_dir . "/$dead_path.cfsmap";
is( $map_size,
    8 + $nblocks_dead * 24,
    'the truncation keeps the physical map file until the checkpoint');
# The immedsync left a bare 16-byte header and nothing wrote to the journal
# in between, so the truncation must have appended exactly one 48-byte
# RESIZE record to the NEW journal.
is(-s $node->data_dir . "/$dead_path.cfsjrn",
    16 + 48, 'the RESIZE landed in the new journal (one 48-byte record)');
 
# Dead-page counter before the truncating checkpoint: nothing was folded
# since the immedsync reset, so every row the RESIZE is about to drop must
# surface as the dead_pages delta of this checkpoint.  The delta may also
# carry a few superseded rows from records that landed in the journal
# after the sample (hint bits), like every fold delta here - the
# truncation rows are the floor, not the exact total.
my $dead_pages_pre = $node->safe_psql('postgres',
    "SELECT dead_pages FROM pg_stat_cfs WHERE relname = 'dead_t'");
 
# The checkpoint applies the deferred RESIZE: the dead tail is charged and
# physically truncated, and the journal is consumed down to its bare
# header.
$node->safe_psql('postgres', 'CHECKPOINT');
is( -s $node->data_dir . "/$dead_path.cfsmap",
    8 + $nblocks * 24,
    'the checkpoint applies the deferred map truncation');
is(-s $node->data_dir . "/$dead_path.cfsjrn",
    16, 'the checkpoint consumed the RESIZE and reset the journal');
cmp_ok(
    $node->safe_psql('postgres',
        "SELECT dead_pages FROM pg_stat_cfs WHERE relname = 'dead_t'") -
      $dead_pages_pre,
    '>=',
    $nblocks_dead - $nblocks,
    'dead_pages counts the rows the truncating RESIZE dropped');
is($node->safe_psql('postgres', "SELECT ok FROM pg_cfs_check('dead_t')"),
    't', 'fsck is clean');
 
$bg->quit;
 
$node->stop('fast');
 
done_testing();