-- ----------------------------------------------------------------------
|
-- Serial vs parallel BRIN build on a CFS heap, adapted from
|
-- contrib/pageinspect/sql/brin.sql.
|
--
|
-- The core test proves that a parallel BRIN build produces the same
|
-- index as a serial build. Replayed with a CFS heap (the BRIN indexes
|
-- inherit the table policy, so their pages are CFS-compressed too), it
|
-- additionally proves that the CFS write path is deterministic across
|
-- the two build strategies: the summary tuples read back through
|
-- decompression must be identical.
|
--
|
-- The dataset is copied from the core test: it has very few distinct
|
-- values per range, so the minmax-multi summaries need no merging and
|
-- the comparison stays deterministic.
|
--
|
-- Before the comparisons both indexes are evicted from shared_buffers
|
-- via pg_buffercache_evict_relation(), so get_raw_page() goes through
|
-- smgrread()/CFS decompression (see cfs_create_table.sql).
|
-- ----------------------------------------------------------------------
|
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;
|
$$;
|
|
CREATE TABLE brin_cfs_test (a int, b text, c bigint)
|
WITH (fillfactor = 40, algorithm = 'lz4');
|
|
INSERT INTO brin_cfs_test
|
SELECT (CASE WHEN (mod(i,231) = 0) OR (i BETWEEN 3500 AND 4000) THEN NULL ELSE i END),
|
(CASE WHEN (mod(i,233) = 0) OR (i BETWEEN 3750 AND 4250) THEN NULL ELSE encode(sha256(i::text::bytea), 'hex') END),
|
(CASE WHEN (mod(i,233) = 0) OR (i BETWEEN 3850 AND 4500) THEN NULL ELSE (i/100) + mod(i,8) END)
|
FROM generate_series(1,5000) S(i);
|
|
-- Build an index without parallelism.
|
|
SET max_parallel_maintenance_workers = 0;
|
|
CREATE INDEX brin_cfs_serial_idx ON brin_cfs_test
|
USING brin (a int4_minmax_ops, a int4_bloom_ops, b, c int8_minmax_multi_ops)
|
WITH (pages_per_range = 7)
|
WHERE NOT (a BETWEEN 1000 AND 1500);
|
|
-- Build the same index using parallelism. The GUCs below force a
|
-- parallel build for this small table (see the core test).
|
|
SET min_parallel_table_scan_size = 0;
|
|
SET max_parallel_maintenance_workers = 4;
|
|
SET maintenance_work_mem = '128MB';
|
|
CREATE INDEX brin_cfs_parallel_idx ON brin_cfs_test
|
USING brin (a int4_minmax_ops, a int4_bloom_ops, b, c int8_minmax_multi_ops)
|
WITH (pages_per_range = 7)
|
WHERE NOT (a BETWEEN 1000 AND 1500);
|
|
-- Both indexes inherited the table's CFS policy.
|
|
SELECT is_cfs, algorithm FROM cfs_relation_policy('brin_cfs_serial_idx');
|
|
SELECT is_cfs, algorithm FROM cfs_relation_policy('brin_cfs_parallel_idx');
|
|
-- Same page count, as in the core test.
|
|
SELECT relname, relpages
|
FROM pg_class
|
WHERE relname IN ('brin_cfs_serial_idx', 'brin_cfs_parallel_idx')
|
ORDER BY relname;
|
|
-- Force the summary pages back through CFS decompression, then check
|
-- that (A except B) and (B except A) are both empty over every summary
|
-- page.
|
|
CHECKPOINT;
|
|
SELECT cfs_evict('brin_cfs_serial_idx');
|
|
SELECT cfs_evict('brin_cfs_parallel_idx');
|
|
WITH parallel_items AS (
|
SELECT (brin_page_items(get_raw_page('brin_cfs_parallel_idx', p),
|
'brin_cfs_parallel_idx')).*
|
FROM generate_series(
|
(SELECT lastrevmappage + 1
|
FROM brin_metapage_info(get_raw_page('brin_cfs_parallel_idx', 0))),
|
(SELECT relpages - 1 FROM pg_class
|
WHERE relname = 'brin_cfs_parallel_idx')) AS p
|
),
|
serial_items AS (
|
SELECT (brin_page_items(get_raw_page('brin_cfs_serial_idx', p),
|
'brin_cfs_serial_idx')).*
|
FROM generate_series(
|
(SELECT lastrevmappage + 1
|
FROM brin_metapage_info(get_raw_page('brin_cfs_serial_idx', 0))),
|
(SELECT relpages - 1 FROM pg_class
|
WHERE relname = 'brin_cfs_serial_idx')) AS p
|
)
|
SELECT count(*) AS differing_items
|
FROM ((SELECT * FROM parallel_items EXCEPT SELECT * FROM serial_items)
|
UNION ALL
|
(SELECT * FROM serial_items EXCEPT SELECT * FROM parallel_items)) AS d;
|
|
-- Structural integrity of both indexes' sidecars.
|
|
SELECT ok FROM pg_cfs_check('brin_cfs_serial_idx'::regclass, true);
|
|
SELECT ok FROM pg_cfs_check('brin_cfs_parallel_idx'::regclass, true);
|
|
DROP TABLE brin_cfs_test;
|
|
DROP FUNCTION cfs_evict(regclass);
|
|
DROP EXTENSION pageinspect;
|
|
DROP EXTENSION pg_buffercache;
|