Исходный код СУБД 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
-- ----------------------------------------------------------------------
-- pageinspect on CFS GiST pages, adapted from contrib/pageinspect/sql/gist.sql.
--
-- The core test uses an UNLOGGED table for predictable LSNs, but CFS does
-- not support UNLOGGED, so this uses a regular CFS table.  get_raw_page() on
-- a CFS GiST index returns decompressed pages; pageinspect must parse them
-- and the leaf item offsets/lengths must match a plain GiST on the same data.
-- ----------------------------------------------------------------------
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 + GiST, plain mirror, identical data.
CREATE TABLE pigist_cfs (p point, t text) WITH (algorithm = 'lz4');
CREATE TABLE pigist_plain (p point, t text);
INSERT INTO pigist_cfs SELECT point(i, i), i::text FROM generate_series(1, 1000) i;
INSERT INTO pigist_plain SELECT point(i, i), i::text FROM generate_series(1, 1000) i;
CREATE INDEX pigist_cfs_idx ON pigist_cfs USING gist (p);
CREATE INDEX pigist_plain_idx ON pigist_plain USING gist (p);
 
SELECT cfs_relation_policy('pigist_cfs_idx'::regclass) AS policy,
       cfs_relation_has_sidecars('pigist_cfs_idx'::regclass) AS has_sidecars;
 
-- Evict the CFS GiST so get_raw_page reads through decompression.
CHECKPOINT;
SELECT cfs_evict('pigist_cfs_idx'::regclass);
 
-- Page 0 (root) and page 1 (leaf) are valid 8KB pages.
SELECT octet_length(get_raw_page('pigist_cfs_idx', 0)) AS root_len,
       octet_length(get_raw_page('pigist_cfs_idx', 1)) AS leaf_len;
 
-- Leaf item offsets/lengths/ctids must match the plain mirror: the
-- decompressed CFS pages carry the exact same index tuples.
SELECT count(*) AS differing_items FROM (
  (SELECT itemoffset, ctid, itemlen
     FROM gist_page_items_bytea(get_raw_page('pigist_cfs_idx', 1))
   EXCEPT
   SELECT itemoffset, ctid, itemlen
     FROM gist_page_items_bytea(get_raw_page('pigist_plain_idx', 1)))
  UNION ALL
  (SELECT itemoffset, ctid, itemlen
     FROM gist_page_items_bytea(get_raw_page('pigist_plain_idx', 1))
   EXCEPT
   SELECT itemoffset, ctid, itemlen
     FROM gist_page_items_bytea(get_raw_page('pigist_cfs_idx', 1)))
) d;
 
DROP TABLE pigist_cfs, pigist_plain;
DROP EXTENSION pg_buffercache;
DROP EXTENSION pageinspect;