Исходный код СУБД 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
-- ----------------------------------------------------------------------
-- pageinspect on CFS btree pages, adapted from contrib/pageinspect/sql/btree.sql.
--
-- get_raw_page() reads a page through smgr by relation+block, so on a CFS
-- index it returns a decompressed page.  After evicting the index, pageinspect
-- must parse that decompressed page identically to a plain (uncompressed)
-- btree built on the same data: same metapage stats, same leaf item counts,
-- same item keys.  This is the byte-correctness oracle for decompressed CFS
-- btree pages.
--
-- LSN-dependent and relation-specific fields are excluded from the
-- comparison (they legitimately differ between two tables).
-- ----------------------------------------------------------------------
CREATE EXTENSION pageinspect;
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 $$;
 
-- CFS heap + btree, plain mirror, identical data.
CREATE TABLE pibt_cfs (a int8, b int4range) WITH (algorithm = 'lz4');
CREATE TABLE pibt_plain (a int8, b int4range);
INSERT INTO pibt_cfs SELECT g, int4range(g, g+1) FROM generate_series(1, 1000) g;
INSERT INTO pibt_plain SELECT g, int4range(g, g+1) FROM generate_series(1, 1000) g;
CREATE INDEX pibt_cfs_idx ON pibt_cfs USING btree (a);
CREATE INDEX pibt_plain_idx ON pibt_plain USING btree (a);
 
SELECT cfs_relation_policy('pibt_cfs_idx'::regclass) AS policy,
       cfs_relation_has_sidecars('pibt_cfs_idx'::regclass) AS has_sidecars;
 
-- Evict the CFS btree so get_raw_page reads through decompression.
CHECKPOINT;
SELECT cfs_evict('pibt_cfs_idx'::regclass);
 
-- get_raw_page returns a valid 8KB page from the compressed index.
SELECT octet_length(get_raw_page('pibt_cfs_idx', 0)) AS page0_len,
       octet_length(get_raw_page('pibt_cfs_idx', 1)) AS page1_len;
 
-- The metapage (page 0) and a leaf page (page 1) must parse and match the
-- plain mirror on item counts and key data.
SELECT (SELECT live_items FROM bt_page_stats('pibt_cfs_idx', 1))
     = (SELECT live_items FROM bt_page_stats('pibt_plain_idx', 1))
       AS leaf1_live_items_match;
 
-- Item keys on leaf page 1 must be identical (offset/ctid/len/data); the
-- decompressed CFS page must carry the exact same index tuples.
SELECT count(*) AS differing_items FROM (
  (SELECT itemoffset, ctid, itemlen, data
     FROM bt_page_items('pibt_cfs_idx', 1)
   EXCEPT
   SELECT itemoffset, ctid, itemlen, data
     FROM bt_page_items('pibt_plain_idx', 1))
  UNION ALL
  (SELECT itemoffset, ctid, itemlen, data
     FROM bt_page_items('pibt_plain_idx', 1)
   EXCEPT
   SELECT itemoffset, ctid, itemlen, data
     FROM bt_page_items('pibt_cfs_idx', 1))
) d;
 
DROP TABLE pibt_cfs, pibt_plain;
DROP EXTENSION pg_buffercache;
DROP EXTENSION pageinspect;