Исходный код СУБД 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
-- ----------------------------------------------------------------------
-- pageinspect on CFS hash pages, adapted from contrib/pageinspect/sql/hash.sql.
--
-- get_raw_page() on a CFS hash index returns decompressed pages; pageinspect
-- must parse them identically to a plain hash on the same data.  This is the
-- byte-correctness oracle for decompressed CFS hash pages.
-- ----------------------------------------------------------------------
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 + hash, plain mirror, identical data (enough for a couple of
-- bucket/overflow pages).
CREATE TABLE pihash_cfs (a int, b text) WITH (algorithm = 'lz4');
CREATE TABLE pihash_plain (a int, b text);
INSERT INTO pihash_cfs SELECT g % 100, g::text FROM generate_series(1, 5000) g;
INSERT INTO pihash_plain SELECT g % 100, g::text FROM generate_series(1, 5000) g;
CREATE INDEX pihash_cfs_idx ON pihash_cfs USING hash (a);
CREATE INDEX pihash_plain_idx ON pihash_plain USING hash (a);
 
SELECT cfs_relation_policy('pihash_cfs_idx'::regclass) AS policy,
       cfs_relation_has_sidecars('pihash_cfs_idx'::regclass) AS has_sidecars;
 
-- Evict the CFS hash index so get_raw_page reads through decompression.
CHECKPOINT;
SELECT cfs_evict('pihash_cfs_idx'::regclass);
 
-- The metapage (page 0) is a valid 8KB page and parses.
SELECT octet_length(get_raw_page('pihash_cfs_idx', 0)) AS page0_len;
SELECT hash_page_type(get_raw_page('pihash_cfs_idx', 0)) AS page0_type;
SELECT hash_page_type(get_raw_page('pihash_cfs_idx', 1)) AS page1_type;
 
-- Metapage key fields must match the plain mirror.
SELECT (magic, version, bsize, bmsize, maxbucket) FROM hash_metapage_info(
  get_raw_page('pihash_cfs_idx', 0));
 
-- Bucket page (page 1) item counts must match the plain mirror.
SELECT (SELECT live_items FROM hash_page_stats(get_raw_page('pihash_cfs_idx', 1)))
     = (SELECT live_items FROM hash_page_stats(get_raw_page('pihash_plain_idx', 1)))
       AS bucket1_live_items_match;
 
DROP TABLE pihash_cfs, pihash_plain;
DROP EXTENSION pg_buffercache;
DROP EXTENSION pageinspect;