Исходный код СУБД 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
-- ----------------------------------------------------------------------
-- QDB-1569: pages delivered by the CFS CRC chain (header, payload and
-- original-image CRCs, all three verified in CfsDecompressPage) are not
-- hashed a second time by the cluster page-checksum when
-- cfs_skip_page_checksum is on (the default); PageIsVerified() keeps
-- its header sanity checks.  With the GUC off the double check is
-- restored, and corruption detection itself never weakens: it lives in
-- the CFS CRC chain, which the GUC does not touch.
--
-- Part 1: a plain read of a CFS lz4 relation succeeds with the default
-- GUC (the redundant page-checksum is skipped after the CFS CRCs).
-- Part 2: the same read succeeds with the GUC off (second hash back).
-- Part 3: a corrupted payload byte of one record must still fail the
-- CFS read with the GUC on and off alike, and a restored record must
-- read cleanly again through the smgr read path.
-- ----------------------------------------------------------------------
-- The corrupt test case intentionally triggers WARNINGs from
-- CfsDecompressPage(); suppress client warnings so the regression output
-- stays stable (same approach as cfs_read_batch.sql).  The CREATE
-- EXTENSION notices are hidden the same way: in the full schedule
-- cfs_codec.sql and cfs_read_batch.sql have already installed the
-- extensions.
SET client_min_messages = warning;
CREATE EXTENSION IF NOT EXISTS test_cfs;
CREATE EXTENSION IF NOT EXISTS pg_buffercache;
RESET client_min_messages;
 
SHOW cfs_skip_page_checksum;
 
-- Part 1: rows on disk, buffers evicted, read through the CFS read path.
CREATE TABLE pc_lz4 (id int, payload text) WITH (algorithm = 'lz4');
INSERT INTO pc_lz4 SELECT g, repeat('pc ', 60) FROM generate_series(1, 500) g;
CHECKPOINT;
 
-- Force the scan through the CFS read path: evict the relation's
-- buffers so the count below is served by smgr reads.
SELECT buffers_evicted > 0 AS pc_evicted
  FROM pg_buffercache_evict_relation('pc_lz4');
SELECT count(*) AS pc_rows FROM pc_lz4;
 
-- Part 2: the same read with the redundant page-checksum restored.
SET cfs_skip_page_checksum = off;
 
SELECT buffers_evicted > 0 AS pc_evicted
  FROM pg_buffercache_evict_relation('pc_lz4');
SELECT count(*) AS pc_rows FROM pc_lz4;
 
SET cfs_skip_page_checksum = on;
 
-- Part 3: corrupt one payload byte, then restore it; the CFS CRC chain
-- must catch the corruption with the GUC on and off alike.
-- (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('pc_lz4', 3) AS corrupt_mid_run;
RESET client_min_messages;
 
SET cfs_skip_page_checksum = off;
SET client_min_messages = error;
SELECT cfs_test_read_batch_corrupt('pc_lz4', 3) AS corrupt_mid_run;
RESET client_min_messages;
SET cfs_skip_page_checksum = on;
 
-- The restored record must read cleanly again through the smgr path.
SELECT buffers_evicted > 0 AS pc_evicted
  FROM pg_buffercache_evict_relation('pc_lz4');
SELECT count(*) AS pc_rows FROM pc_lz4;
 
DROP TABLE pc_lz4;