-- ---------------------------------------------------------------------- -- CLUSTER on CFS relations, adapted from src/test/regress/sql/cluster.sql. -- -- CLUSTER is a full table rewrite into a new relfilenode — the same -- kind of atomic swap the CFS compactor performs. The checks: -- -- * a shuffled CFS table clustered on an index: rows come back in -- index order, the sidecars are rebuilt for the new relfilenode and -- the old relfilenode's files are gone; -- -- * toasted values are copied correctly by the rewrite; -- -- * MVCC safety: dead and rolled-back row versions must not leak into -- the rewritten storage; -- -- * edge cases: CLUSTER on a table never clustered before (error) and -- CLUSTER of an empty CFS table. -- -- In this fork the files of a replaced relfilenode are unlinked at the -- next checkpoint, not at commit, so file-count checks run after an -- explicit CHECKPOINT (see cfs_alter_table.sql). -- -- Read-back checks evict the relation from shared_buffers via -- pg_buffercache_evict_relation(), so the verifying SELECTs go through -- smgrread()/CFS decompression (see cfs_create_table.sql). -- ---------------------------------------------------------------------- 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; $$; -- Basic CLUSTER with relfilenode swap ------------------------------------- -- Insert rows in reverse key order so clustering has visible work to do. CREATE TABLE cl_cfs (id int, payload text) WITH (algorithm = 'lz4'); INSERT INTO cl_cfs SELECT 301 - g, md5(g::text) FROM generate_series(1, 300) g; CREATE INDEX cl_cfs_idx ON cl_cfs (id); SELECT relfilenode AS old_node FROM pg_class WHERE oid = 'cl_cfs'::regclass \gset CLUSTER cl_cfs USING cl_cfs_idx; -- CLUSTER rewrote the table and marked the index as clustered. SELECT relfilenode <> :old_node AS node_changed FROM pg_class WHERE oid = 'cl_cfs'::regclass; SELECT indisclustered FROM pg_index WHERE indexrelid = 'cl_cfs_idx'::regclass; CHECKPOINT; SELECT cfs_relation_has_sidecars('cl_cfs') AS sidecars_rebuilt; -- Old relfilenode's files (main fork and sidecars) are gone. SELECT count(*) AS old_node_leftovers FROM pg_ls_dir('base/' || (SELECT oid FROM pg_database WHERE datname = current_database())) AS f WHERE f ~ ('^' || :old_node::text || '(\..*)?$'); -- Physical order now follows the index: with index scans disabled, the -- sequential scan returns the rows in clustered order, read back through -- decompression. SELECT cfs_evict('cl_cfs'); SET enable_indexscan = off; SET enable_bitmapscan = off; SELECT id = 1 AS first_is_one, payload = md5('300') AS payload_ok FROM cl_cfs LIMIT 1; SELECT count(*) AS rows, md5(string_agg(payload, '' ORDER BY id)) = (SELECT md5(string_agg(md5((301 - g)::text), '' ORDER BY g)) FROM generate_series(1, 300) g) AS data_ok FROM cl_cfs; RESET enable_indexscan; RESET enable_bitmapscan; -- TOAST values survive the rewrite ---------------------------------------- -- The big column is built from distinct md5 hashes so that PGLZ cannot -- squeeze it inline and it really lands in the toast relation. CREATE TABLE cl_toast (id int, big text) WITH (algorithm = 'zstd'); INSERT INTO cl_toast SELECT g, (SELECT string_agg(md5((g * 1000 + i)::text), '') FROM generate_series(1, 150) i) FROM generate_series(1, 10) g; CREATE INDEX cl_toast_idx ON cl_toast (id); CLUSTER cl_toast USING cl_toast_idx; CHECKPOINT; SELECT cfs_evict('cl_toast'); SELECT cfs_evict((SELECT reltoastrelid FROM pg_class WHERE oid = 'cl_toast'::regclass)); SELECT count(*) AS rows, md5(string_agg(big, '' ORDER BY id)) = (SELECT md5(string_agg((SELECT string_agg(md5((g * 1000 + i)::text), '') FROM generate_series(1, 150) i), '' ORDER BY g)) FROM generate_series(1, 10) g) AS toast_ok FROM cl_toast; -- MVCC safety ------------------------------------------------------------- -- Deleted and rolled-back row versions must not reappear in the -- rewritten CFS storage. CREATE TABLE cl_mvcc (id int, payload text) WITH (algorithm = 'lz4'); INSERT INTO cl_mvcc SELECT g, md5(g::text) FROM generate_series(1, 100) g; CREATE INDEX cl_mvcc_idx ON cl_mvcc (id); DELETE FROM cl_mvcc WHERE id > 50; BEGIN; UPDATE cl_mvcc SET payload = 'rolled-back' WHERE id <= 25; ABORT; CLUSTER cl_mvcc USING cl_mvcc_idx; SELECT cfs_evict('cl_mvcc'); SELECT count(*) AS rows, count(*) FILTER (WHERE payload = 'rolled-back') AS rolled_back_rows, md5(string_agg(payload, '' ORDER BY id)) = (SELECT md5(string_agg(md5(g::text), '' ORDER BY g)) FROM generate_series(1, 50) g) AS data_ok FROM cl_mvcc; -- Edge cases --------------------------------------------------------------- -- A table never clustered cannot be re-clustered without an index name. CREATE TABLE cl_edge (id int) WITH (algorithm = 'lz4'); CLUSTER cl_edge; -- An empty CFS table can be clustered; it ends up with valid sidecars -- and no rows. CREATE INDEX cl_edge_idx ON cl_edge (id); CLUSTER cl_edge USING cl_edge_idx; CHECKPOINT; SELECT cfs_relation_has_sidecars('cl_edge') AS sidecars, (SELECT count(*) FROM cl_edge) AS rows; DROP TABLE cl_cfs, cl_toast, cl_mvcc, cl_edge; DROP FUNCTION cfs_evict(regclass); DROP EXTENSION pg_buffercache;