Исходный код СУБД 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
-- ----------------------------------------------------------------------
-- pageinspect on CFS heap pages, adapted from
-- contrib/pageinspect/sql/page.sql.
--
-- get_raw_page() reads a page through smgr by relation and block number,
-- so on a CFS relation it exercises the decompressing smgrread path —
-- making pageinspect the best available oracle that a decompressed CFS
-- page is byte-correct: after evicting shared_buffers, the parsed page
-- contents of a CFS table must match those of a plain twin table loaded
-- with the same rows.
--
-- Only structural fields are compared (lp, lp_off, lp_len, lp_flags,
-- t_data): page LSNs and checksums legitimately differ between the two
-- tables.
--
-- Note: CFS relations cannot be TEMP, so unlike the core test this one
-- uses a regular table; FSM/VM contents are read but not compared
-- byte-for-byte.
-- ----------------------------------------------------------------------
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;
$$;
 
CREATE TABLE pi_cfs (a int, b text) WITH (algorithm = 'lz4');
 
INSERT INTO pi_cfs
  SELECT g, md5(g::text) FROM generate_series(1, 300) g;
 
CREATE TABLE pi_plain (a int, b text);
 
INSERT INTO pi_plain
  SELECT g, md5(g::text) FROM generate_series(1, 300) g;
 
VACUUM (DISABLE_PAGE_SKIPPING) pi_cfs;
 
VACUUM (DISABLE_PAGE_SKIPPING) pi_plain;
 
-- A page can be read from every fork, same as for a plain table.
 
CHECKPOINT;
 
SELECT octet_length(get_raw_page('pi_cfs', 'main', 0)) AS main_0;
 
SELECT octet_length(get_raw_page('pi_cfs', 'fsm', 0)) AS fsm_0;
 
SELECT octet_length(get_raw_page('pi_cfs', 'vm', 0)) AS vm_0;
 
-- Reading past the last block fails, same as vanilla.
 
SELECT octet_length(get_raw_page('pi_cfs', 'main', -1));
 
-- The page header survives decompression (LSN and checksum excluded).
 
SELECT pagesize, version FROM page_header(get_raw_page('pi_cfs', 0));
 
-- The real check: force both tables out of shared_buffers, then compare
-- the parsed items of every main-fork page.  For the CFS table these
-- pages just came through decompression.
 
SELECT cfs_evict('pi_cfs');
 
SELECT cfs_evict('pi_plain');
 
-- The block range of the CFS table comes from its logical size: the
-- main fork of a CFS relation is only a small metadata stub, so
-- pg_relation_size() cannot be used to find the page count.
 
WITH cfs AS (
  SELECT (heap_page_items(get_raw_page('pi_cfs', b))).*
    FROM generate_series(0, (SELECT logical_size / 8192 - 1
                               FROM pg_cfs_relation_size('pi_cfs'))) AS b
),
plain AS (
  SELECT (heap_page_items(get_raw_page('pi_plain', b))).*
    FROM generate_series(0, pg_relation_size('pi_plain') / 8192 - 1) AS b
)
SELECT count(*) AS differing_items
  FROM ((SELECT lp, lp_off, lp_len, lp_flags, t_data FROM cfs
         EXCEPT
         SELECT lp, lp_off, lp_len, lp_flags, t_data FROM plain)
        UNION ALL
        (SELECT lp, lp_off, lp_len, lp_flags, t_data FROM plain
         EXCEPT
         SELECT lp, lp_off, lp_len, lp_flags, t_data FROM cfs)) AS d;
 
-- tuple_data_split decodes the decompressed tuples back to the inserted
-- values (spot-check the first tuple).
 
SELECT tuple_data_split('pi_cfs'::regclass, t_data, t_infomask,
                        t_infomask2, t_bits)
  FROM heap_page_items(get_raw_page('pi_cfs', 0))
 WHERE lp = 1;
 
DROP TABLE pi_cfs, pi_plain;
 
DROP FUNCTION cfs_evict(regclass);
 
DROP EXTENSION pageinspect;
 
DROP EXTENSION pg_buffercache;