Исходный код СУБД 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
-- ----------------------------------------------------------------------
-- amcheck gin_index_check on a CFS-compressed GIN index.
--
-- gin_index_check walks the GIN entry tree and posting trees through the
-- buffer manager, so on a CFS GIN (inherited from a CFS heap) it reads
-- decompressed pages.  A clean run (no corruption) validates that every
-- decompressed CFS GIN page is structurally correct.
--
-- Based on contrib/amcheck/sql/check_gin.sql, reduced to the bulk-load and
-- insert paths.
-- ----------------------------------------------------------------------
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 $$;
 
SELECT setseed(1);
 
-- CFS heap with posting trees (frequent entries) and posting leaves (sparse).
CREATE TABLE cfsgin_chk ("col" int[]) WITH (algorithm = 'lz4');
INSERT INTO cfsgin_chk
  SELECT array_agg(round(random() * 255))
    FROM generate_series(1, 100000) i GROUP BY i % 10000;
INSERT INTO cfsgin_chk
  SELECT array_agg(255 + round(random() * 100))
    FROM generate_series(1, 100) i GROUP BY i % 100;
 
CREATE INDEX cfsgin_chk_idx ON cfsgin_chk USING gin ("col");
 
SELECT cfs_relation_policy('cfsgin_chk_idx'::regclass) AS policy,
       cfs_relation_has_sidecars('cfsgin_chk_idx'::regclass) AS has_sidecars;
 
-- Evict the CFS GIN pages so gin_index_check reads them through decompression.
CHECKPOINT;
SELECT cfs_evict('cfsgin_chk_idx'::regclass);
 
-- gin_index_check must report no corruption (empty = clean).
SELECT gin_index_check('cfsgin_chk_idx') AS cfs_gin_check;
 
-- Repeat with the insert path (fastupdate off) rather than bulk load.
DROP TABLE cfsgin_chk;
SELECT setseed(1);
CREATE TABLE cfsgin_chk ("col" int[]) WITH (algorithm = 'lz4');
CREATE INDEX cfsgin_chk_idx ON cfsgin_chk USING gin ("col");
ALTER INDEX cfsgin_chk_idx SET (fastupdate = false);
INSERT INTO cfsgin_chk
  SELECT array_agg(round(random() * 255))
    FROM generate_series(1, 100000) i GROUP BY i % 10000;
 
CHECKPOINT;
SELECT cfs_evict('cfsgin_chk_idx'::regclass);
SELECT gin_index_check('cfsgin_chk_idx') AS cfs_gin_insert_check;
 
DROP TABLE cfsgin_chk;
DROP EXTENSION pg_buffercache;
DROP EXTENSION amcheck;