-- ---------------------------------------------------------------------- -- GIN index through the CFS storage manager. -- -- A GIN index on a CFS heap inherits the heap's compression policy, so the -- pending list, entry tree, and posting trees all live in sidecar-compressed -- storage. This covers the paths that interact with CFS smgr the most: -- fastupdate flush (gin_clean_pending_list / VACUUM), posting-tree vacuum -- after deletes, and the recompression churn that happens with fastupdate off. -- A plain (uncompressed) mirror table is the scan oracle: CFS and plain must -- return identical results. Both tables receive identical operations. -- -- Based on regress/sql/gin.sql, reduced to the build/flush/vacuum/scan paths. -- ---------------------------------------------------------------------- CREATE EXTENSION pg_buffercache; -- CFS heap + plain mirror, both GIN-indexed with fastupdate on. Every -- operation below is applied identically to both so the data stays in sync. CREATE TABLE cfsgin_cfs (i int4[]) WITH (algorithm = 'lz4', autovacuum_enabled = off); CREATE TABLE cfsgin_plain (i int4[]) WITH (autovacuum_enabled = off); CREATE INDEX cfsgin_cfs_idx ON cfsgin_cfs USING gin (i) WITH (fastupdate = on, gin_pending_list_limit = 4096); CREATE INDEX cfsgin_plain_idx ON cfsgin_plain USING gin (i) WITH (fastupdate = on, gin_pending_list_limit = 4096); INSERT INTO cfsgin_cfs SELECT array[1, 2, g] FROM generate_series(1, 5000) g; INSERT INTO cfsgin_cfs SELECT array[1, 3, g] FROM generate_series(1, 5000) g; INSERT INTO cfsgin_plain SELECT array[1, 2, g] FROM generate_series(1, 5000) g; INSERT INTO cfsgin_plain SELECT array[1, 3, g] FROM generate_series(1, 5000) g; SELECT cfs_relation_policy('cfsgin_cfs_idx'::regclass) AS policy, cfs_relation_has_sidecars('cfsgin_cfs_idx'::regclass) AS has_sidecars; -- Flush the fastupdate pending list through CFS smgr. SELECT gin_clean_pending_list('cfsgin_cfs_idx') > 10 AS many_flushed; SELECT gin_clean_pending_list('cfsgin_plain_idx') > 10 AS many_flushed; -- Posting-tree vacuum: delete all entries containing key 2, then vacuum. DELETE FROM cfsgin_cfs WHERE i @> array[2]; DELETE FROM cfsgin_plain WHERE i @> array[2]; VACUUM cfsgin_cfs; VACUUM cfsgin_plain; -- Turn fastupdate off and do more inserts to churn the GIN leaf pages -- (exercises the recompression codepath). ALTER INDEX cfsgin_cfs_idx SET (fastupdate = off); ALTER INDEX cfsgin_plain_idx SET (fastupdate = off); INSERT INTO cfsgin_cfs SELECT array[1, 2, g] FROM generate_series(1, 1000) g; INSERT INTO cfsgin_cfs SELECT array[1, 3, g] FROM generate_series(1, 1000) g; INSERT INTO cfsgin_plain SELECT array[1, 2, g] FROM generate_series(1, 1000) g; INSERT INTO cfsgin_plain SELECT array[1, 3, g] FROM generate_series(1, 1000) g; DELETE FROM cfsgin_cfs WHERE i @> array[2]; DELETE FROM cfsgin_plain WHERE i @> array[2]; VACUUM cfsgin_cfs; VACUUM cfsgin_plain; -- Evict the compressed GIN pages so scans read through CFS decompression. CHECKPOINT; SELECT buffers_evicted > 0 AS evicted_idx FROM pg_buffercache_evict_relation('cfsgin_cfs_idx'::regclass); SELECT buffers_evicted > 0 AS evicted_tbl FROM pg_buffercache_evict_relation('cfsgin_cfs'); -- Row counts must be identical (data in sync). SELECT count(*) AS cfs_rows FROM cfsgin_cfs; SELECT count(*) AS plain_rows FROM cfsgin_plain; -- A rare-key containment search exercises the entry tree + posting tree. SET enable_seqscan TO off; SELECT count(*) AS cfs_rare FROM cfsgin_cfs WHERE i @> array[1, 4999]; RESET enable_seqscan; SELECT count(*) AS plain_rare FROM cfsgin_plain WHERE i @> array[1, 4999]; -- A frequent-key search exercises many posting lists. SET enable_seqscan TO off; SELECT count(*) AS cfs_freq FROM cfsgin_cfs WHERE i @> array[1]; RESET enable_seqscan; SELECT count(*) AS plain_freq FROM cfsgin_plain WHERE i @> array[1]; DROP TABLE cfsgin_cfs, cfsgin_plain; DROP EXTENSION pg_buffercache;