Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 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
-- ----------------------------------------------------------------------
-- QDB-1513: batched read path (advisory maxcombine + span pread).
--
-- The physical layout of .cfp records follows the order pages are flushed
-- to the sidecars, so the parts below pick scenarios whose order is
-- deterministic:
--
-- Part 1: right after a bulk INSERT the heap pages mostly still live only
-- in shared_buffers (zero-extended, no records yet).  Whatever mix of
-- holes and already-flushed records the timing of the bgwriter leaves,
-- the batched reader must agree with the per-block path byte-for-byte on
-- every page of the relation.
--
-- Part 2: a btree build writes its pages synchronously through
-- bulk_write in block order, so the freshly built CFS index (composite
-- key, no deduplication) stores logical neighbours back-to-back:
-- cfs_map_contiguous_run() must see full runs (capped by the probe's
-- 16-block window) and cfs_read_pages_batch() must reproduce every page
-- byte-for-byte (poison-filled batch buffers are compared against the
-- per-block cfs_read_page_cached() reads through a full private-map
-- handle).
--
-- Part 3: a corrupted payload byte of a record inside the contiguous run
-- must fail the batched range read with CFS_STORAGE_CORRUPT naming
-- exactly that block, and a restored byte must read cleanly again.
--
-- Part 4: a non-HOT update dirties a couple of index leaf pages, which
-- the checkpoint re-appends at the tail of .cfp; the requested ranges now
-- contain offset gaps, and the batched reader must fall back per-block
-- around them and still reproduce every page exactly.
--
-- Part 5: compaction of the heap plus a real seqscan of the evicted
-- relation (lazy pool-backed handle, the smgr readv path bufmgr drives
-- with io_combine_limit) must return the same data as a plain
-- uncompressed table loaded with the same rows.
-- ----------------------------------------------------------------------
-- The corrupt test case intentionally triggers WARNINGs from
-- CfsDecompressPage(); suppress client warnings so the regression output
-- stays stable (same approach as cfs_codec.sql).  The CREATE EXTENSION
-- notice is hidden the same way: in the full schedule cfs_codec.sql has
-- already installed the extension.
SET client_min_messages = warning;
CREATE EXTENSION IF NOT EXISTS test_cfs;
RESET client_min_messages;
CREATE EXTENSION pg_buffercache;
CREATE TABLE rb_lz4 (id int, payload text) WITH (algorithm = 'lz4');
INSERT INTO rb_lz4 SELECT g, repeat('rb ', 60) FROM generate_series(1, 5000) g;
-- Part 1: holes (and whatever the bgwriter already flushed) must degrade
-- per-block and still compare equal.
SELECT batch_ok AND pages_checked > 20 AS heap_holes_degrade_correctly
  FROM cfs_test_read_batch('rb_lz4');
 heap_holes_degrade_correctly 
------------------------------
 t
(1 row)
 
-- Part 2: the index build flushes pages synchronously in block order.
CREATE INDEX rb_lz4_payload_idx ON rb_lz4 (payload, id);
-- Block 0 is the btree metapage, which the sort-build writes last: its
-- record lands at the tail, so the run starting at block 0 is short by
-- construction while the leaf/internal span is fully contiguous.
SELECT run0 = 1 AND run_max = 16 AS leaves_contiguous,
       pages_checked > 100 AS whole_index_rechecked,
       batch_ok AS batch_matches_perblock
  FROM cfs_test_read_batch('rb_lz4_payload_idx');
 leaves_contiguous | whole_index_rechecked | batch_matches_perblock 
-------------------+-----------------------+------------------------
 t                 | t                     | t
(1 row)
 
-- Part 3: corrupt one record of the contiguous run, then restore it.
-- (Warnings from CfsDecompressPage() are expected and hidden: the CRC
-- values printed depend on the compressed bytes of the record.)
SET client_min_messages = error;
SELECT cfs_test_read_batch_corrupt('rb_lz4_payload_idx', 3) AS corrupt_mid_run;
 corrupt_mid_run 
-----------------
 ok
(1 row)
 
RESET client_min_messages;
-- The failed span read already served (and per-page counted) the records
-- before the corrupt one; the partial batch must keep the counter invariant
-- intact even on this error path.
SELECT read_count = read_batched_pages + read_fallback_pages
         AS invariant_after_corrupt
  FROM pg_stat_cfs WHERE relid = 'rb_lz4_payload_idx'::regclass;
 invariant_after_corrupt 
-------------------------
 t
(1 row)
 
-- Part 4: move a couple of index leaf pages to the tail of .cfp.
UPDATE rb_lz4 SET id = 5000 + id WHERE id = 2500;
CHECKPOINT;
-- Two index leaf pages (the old and the new position of the updated
-- key) are re-appended at the tail, splitting the span; the 16-block
-- probe window still finds full contiguous runs between the gaps, and
-- the batched reader must degrade per-block exactly around them.
SELECT run_max = 16 AS long_runs_survive,
       pages_checked > 100 AS whole_index_rechecked,
       batch_ok AS batch_matches_perblock
  FROM cfs_test_read_batch('rb_lz4_payload_idx');
 long_runs_survive | whole_index_rechecked | batch_matches_perblock 
-------------------+-----------------------+------------------------
 t                 | t                     | t
(1 row)
 
-- Part 5: compaction plus a real seqscan through the smgr readv path.
CREATE TABLE rb_plain (id int, payload text);
INSERT INTO rb_plain SELECT id, payload FROM rb_lz4;
SELECT pg_cfs_compact('rb_lz4') ~ 'reclaimed \d+' AS compacted;
 compacted 
-----------
 t
(1 row)
 
SELECT batch_ok AND pages_checked > 100 AS heap_batched_after_compact
  FROM cfs_test_read_batch('rb_lz4');
 heap_batched_after_compact 
----------------------------
 t
(1 row)
 
CHECKPOINT;
SELECT buffers_evicted > 0 AS evicted
  FROM pg_buffercache_evict_relation('rb_lz4');
 evicted 
---------
 t
(1 row)
 
SELECT (SELECT md5(string_agg(payload, '' ORDER BY id)) FROM rb_lz4)
       =
       (SELECT md5(string_agg(payload, '' ORDER BY id)) FROM rb_plain)
       AS seqscan_matches_plain;
 seqscan_matches_plain 
-----------------------
 t
(1 row)
 
-- Part 6: batch-read observability (QDB-1513 evidence base).  The wait
-- event split must expose decompression separately from the raw read, and
-- the pg_stat_cfs counters must attribute every served page either to a
-- batched span or to the per-block fallback (default shared-map mode).
SELECT count(*) = 1 AS decompress_wait_event
  FROM pg_wait_events WHERE name = 'CfsDecompress';
 decompress_wait_event 
-----------------------
 t
(1 row)
 
SET enable_indexscan = off;
SET enable_indexonlyscan = off;
SET enable_bitmapscan = off;
-- The compacted heap is physically contiguous: a cold seqscan is served by
-- span reads.
DO $$ BEGIN PERFORM pg_cfs_reset_stats('rb_lz4'); END $$;
SELECT buffers_evicted > 0 AS evicted
  FROM pg_buffercache_evict_relation('rb_lz4');
 evicted 
---------
 t
(1 row)
 
SELECT count(*) > 0 AS seqscan_ran FROM rb_lz4;
 seqscan_ran 
-------------
 t
(1 row)
 
SELECT read_batch_count > 0 AS spans_served,
       read_batched_pages > 0 AS pages_batched,
       read_count = read_batched_pages + read_fallback_pages
         AS invariant_holds
  FROM pg_stat_cfs WHERE relid = 'rb_lz4'::regclass;
 spans_served | pages_batched | invariant_holds 
--------------+---------------+-----------------
 t            | t             | t
(1 row)
 
-- Updated pages are re-appended at the tail of .cfp, so their old offsets
-- become gaps inside runs: those blocks must degrade per-block (fallback)
-- and the invariant must survive.
UPDATE rb_lz4 SET payload = payload || 'x' WHERE id % 500 = 0;
CHECKPOINT;
DO $$ BEGIN PERFORM pg_cfs_reset_stats('rb_lz4'); END $$;
SELECT buffers_evicted > 0 AS evicted
  FROM pg_buffercache_evict_relation('rb_lz4');
 evicted 
---------
 t
(1 row)
 
SELECT count(*) > 0 AS seqscan_ran FROM rb_lz4;
 seqscan_ran 
-------------
 t
(1 row)
 
SELECT read_fallback_pages > 0 AS fallback_used,
       read_count = read_batched_pages + read_fallback_pages
         AS invariant_holds
  FROM pg_stat_cfs WHERE relid = 'rb_lz4'::regclass;
 fallback_used | invariant_holds 
---------------+-----------------
 t             | t
(1 row)
 
RESET enable_indexscan;
RESET enable_indexonlyscan;
RESET enable_bitmapscan;
DROP TABLE rb_lz4, rb_plain;