Исходный код СУБД 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
-- ----------------------------------------------------------------------
-- amcheck verify_heapam on a CFS heap, and SET TABLESPACE preserving the
-- CFS policy.
--
-- verify_heapam scans every heap page through the buffer manager (a
-- BAS_BULKREAD strategy), so on a CFS heap it reads decompressed pages and
-- validates tuple structure, visibility, and XID/MXFF states.  A clean run
-- is an oracle that decompressed CFS heap pages are correct.
--
-- The matrix note for this case: SET TABLESPACE must preserve compression.
-- Moving a CFS table to another tablespace rewrites it through the CFS smgr
-- write path; the destination table must still be CFS, with sidecars, and
-- verify_heapam-clean.
--
-- Based on contrib/amcheck/sql/check_heap.sql.
-- ----------------------------------------------------------------------
CREATE EXTENSION amcheck;
CREATE EXTENSION pg_buffercache;
CREATE FUNCTION cfs_evict(p_rel regclass) RETURNS void
  LANGUAGE plpgsql AS $$ BEGIN
  PERFORM pg_buffercache_evict_relation(p_rel);
END $$;
 
CREATE TABLE cfsheap (a integer, b text) WITH (algorithm = 'lz4');
INSERT INTO cfsheap (a, b)
  SELECT gs, repeat('x', gs) FROM generate_series(1, 50) gs;
 
SELECT cfs_relation_policy('cfsheap') AS policy_before,
       cfs_relation_has_sidecars('cfsheap') AS sidecars_before;
 
-- Evict so verify_heapam reads through CFS decompression.
CHECKPOINT;
SELECT cfs_evict('cfsheap');
 
-- verify_heapam must report no corruption on the CFS heap.
SELECT count(*) AS corrupt FROM verify_heapam(relation := 'cfsheap', skip := 'none');
 
-- SET TABLESPACE rewrites the table; the destination must still be CFS.
SET allow_in_place_tablespaces = true;
CREATE TABLESPACE cfsheap_tblspc LOCATION '';
BEGIN;
ALTER TABLE cfsheap SET TABLESPACE cfsheap_tblspc;
COMMIT;
 
SELECT cfs_relation_policy('cfsheap') AS policy_after_tsp,
       cfs_relation_has_sidecars('cfsheap') AS sidecars_after_tsp;
 
CHECKPOINT;
SELECT cfs_evict('cfsheap');
 
-- After the rewrite the moved table must still verify clean.
SELECT count(*) AS corrupt_after_move FROM verify_heapam(relation := 'cfsheap', skip := 'none');
 
-- FREEZE changes the visibility/XID state that verify_heapam inspects; the
-- frozen CFS pages must still be clean.
VACUUM (FREEZE, DISABLE_PAGE_SKIPPING) cfsheap;
SELECT count(*) AS corrupt_after_freeze FROM verify_heapam(relation := 'cfsheap', skip := 'none');
 
DROP TABLE cfsheap;
DROP TABLESPACE cfsheap_tblspc;
DROP EXTENSION pg_buffercache;
DROP EXTENSION amcheck;