-- ---------------------------------------------------------------------- -- amcheck bt_index_check on a CFS-compressed btree. -- -- bt_index_check / bt_index_parent_check walk every page of a btree index -- through the buffer manager, so on a CFS btree (inherited from a CFS heap) -- they read decompressed pages. A clean amcheck run (no corruption) is a -- strong oracle that every decompressed CFS btree page is structurally -- correct -- downlinks, keys, high keys, dedup TIDs, the lot. -- -- The oracle is a plain (uncompressed) mirror: amcheck must pass on both. -- -- Based on contrib/amcheck/sql/check_btree.sql, reduced to the index-build -- and dedup paths that exercise the CFS smgr read path. -- ---------------------------------------------------------------------- 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 $$; -- CFS heap + plain mirror, both with btree indexes (dedup on). CREATE TABLE cfsbt_cfs (id int8, data int8) WITH (algorithm = 'lz4'); CREATE TABLE cfsbt_plain (id int8, data int8); INSERT INTO cfsbt_cfs SELECT i, i % 2 FROM generate_series(1, 100000) i; INSERT INTO cfsbt_plain SELECT i, i % 2 FROM generate_series(1, 100000) i; CREATE INDEX cfsbt_cfs_idx ON cfsbt_cfs USING btree (id) WITH (deduplicate_items = ON); CREATE INDEX cfsbt_plain_idx ON cfsbt_plain USING btree (id) WITH (deduplicate_items = ON); -- A covering unique index (different internal-page layout). CREATE UNIQUE INDEX cfsbt_cfs_multi ON cfsbt_cfs USING btree (id) INCLUDE (data); SELECT cfs_relation_policy('cfsbt_cfs_idx'::regclass) AS policy, cfs_relation_has_sidecars('cfsbt_cfs_idx'::regclass) AS has_sidecars; -- Evict the CFS btree pages so amcheck reads them through decompression. CHECKPOINT; SELECT cfs_evict('cfsbt_cfs_idx'::regclass); SELECT cfs_evict('cfsbt_cfs_multi'::regclass); -- amcheck must report no corruption on the CFS btree (empty = clean) and -- on the plain mirror. SELECT bt_index_check('cfsbt_cfs_idx') AS cfs_check; SELECT bt_index_check('cfsbt_plain_idx') AS plain_check; SELECT bt_index_check('cfsbt_cfs_idx', true) AS cfs_check_full; SELECT bt_index_parent_check('cfsbt_cfs_idx', true) AS cfs_parent_check; SELECT bt_index_parent_check('cfsbt_cfs_multi', true, true) AS cfs_multi_check; -- Dedup stress: many duplicate keys collapse into posting-list TIDs that -- amcheck must re-expand from the decompressed pages. Drop the unique -- covering index first so the all-42 inserts are allowed. DROP INDEX cfsbt_cfs_multi; TRUNCATE cfsbt_cfs; TRUNCATE cfsbt_plain; INSERT INTO cfsbt_cfs SELECT 42, 1 FROM generate_series(1, 2000); INSERT INTO cfsbt_plain SELECT 42, 1 FROM generate_series(1, 2000); SELECT bt_index_check('cfsbt_cfs_idx', true) AS cfs_dedup_check; SELECT bt_index_check('cfsbt_plain_idx', true) AS plain_dedup_check; DROP TABLE cfsbt_cfs, cfsbt_plain; DROP EXTENSION pg_buffercache; DROP EXTENSION amcheck;