-- ---------------------------------------------------------------------- -- TRUNCATE on CFS relations, adapted from -- src/test/regress/sql/truncate.sql. -- -- TRUNCATE swaps the relation to a fresh, empty relfilenode. For a CFS -- table that means the sidecar trio (.cfp/.cfsmap/.cfsjrn) must be reset -- together with the main fork, on commit as well as on rollback: -- -- * committed TRUNCATE: new relfilenode with fresh empty sidecars, old -- relfilenode's files removed, table reusable and readable through -- decompression afterwards; -- -- * rolled-back TRUNCATE: data and sidecars of the original -- relfilenode intact, the transient relfilenode's files removed at -- abort; -- -- * TRUNCATE of an empty CFS table: still a valid no-op storage swap. -- -- 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; $$; CREATE FUNCTION cfs_base_relation_files(p_node oid) RETURNS bigint LANGUAGE sql AS $$ SELECT count(*) FROM pg_ls_dir('base/' || (SELECT oid FROM pg_database WHERE datname = current_database())) AS f WHERE f ~ ('^' || p_node::text || '(\..*)?$'); $$; -- Committed TRUNCATE ------------------------------------------------------- CREATE TABLE tr_cfs (id int, payload text) WITH (algorithm = 'lz4'); INSERT INTO tr_cfs SELECT g, md5(g::text) FROM generate_series(1, 300) g; SELECT relfilenode AS old_node FROM pg_class WHERE oid = 'tr_cfs'::regclass \gset TRUNCATE tr_cfs; -- A fresh relfilenode with fresh sidecars, no rows. SELECT relfilenode <> :old_node AS node_changed FROM pg_class WHERE oid = 'tr_cfs'::regclass; CHECKPOINT; SELECT cfs_relation_has_sidecars('tr_cfs') AS sidecars, (SELECT count(*) FROM tr_cfs) AS rows; SELECT cfs_base_relation_files(:old_node) AS old_node_leftovers; -- The policy survives the truncate. SELECT * FROM cfs_relation_policy('tr_cfs'); -- The table is writable and readable (through decompression) again. INSERT INTO tr_cfs SELECT g, md5(g::text) FROM generate_series(1001, 1100) g; CHECKPOINT; SELECT cfs_evict('tr_cfs'); SELECT count(*) AS rows, md5(string_agg(payload, '' ORDER BY id)) = (SELECT md5(string_agg(md5(g::text), '' ORDER BY g)) FROM generate_series(1001, 1100) g) AS data_ok FROM tr_cfs; -- Rolled-back TRUNCATE ----------------------------------------------------- SELECT relfilenode AS stable_node FROM pg_class WHERE oid = 'tr_cfs'::regclass \gset BEGIN; TRUNCATE tr_cfs; -- Inside the transaction the transient relfilenode is visible. SELECT relfilenode <> :stable_node AS node_differs FROM pg_class WHERE oid = 'tr_cfs'::regclass; SELECT relfilenode AS transient_node FROM pg_class WHERE oid = 'tr_cfs'::regclass \gset ROLLBACK; -- After the abort the original relfilenode is back, its data and -- sidecars are intact, and the transient relfilenode left no files. SELECT relfilenode = :stable_node AS node_restored FROM pg_class WHERE oid = 'tr_cfs'::regclass; CHECKPOINT; SELECT cfs_relation_has_sidecars('tr_cfs') AS sidecars; SELECT cfs_base_relation_files(:transient_node) AS transient_leftovers; SELECT cfs_evict('tr_cfs'); SELECT count(*) AS rows, md5(string_agg(payload, '' ORDER BY id)) = (SELECT md5(string_agg(md5(g::text), '' ORDER BY g)) FROM generate_series(1001, 1100) g) AS data_ok FROM tr_cfs; -- TRUNCATE of an empty CFS table ------------------------------------------- CREATE TABLE tr_empty (id int) WITH (algorithm = 'zstd'); TRUNCATE tr_empty; CHECKPOINT; SELECT cfs_relation_has_sidecars('tr_empty') AS sidecars, (SELECT count(*) FROM tr_empty) AS rows; -- TRUNCATE of a CFS table created in the same transaction ------------------ -- -- heap_truncate_one_rel() truncates in place through the smgrtruncate() -- bridge, which runs inside a critical section; a table created and -- truncated in the same transaction has never been written through CFS. -- The sidecars must already exist at CREATE time (the policy publication -- materializes them, like mdcreate() materializes the base file), so the -- bridge opens existing files instead of creating them in the critical -- section. BEGIN; CREATE TABLE tr_same_txn (id int) WITH (algorithm = 'lz4'); SELECT cfs_relation_has_sidecars('tr_same_txn') AS sidecars; TRUNCATE tr_same_txn; TRUNCATE tr_same_txn; SELECT cfs_relation_has_sidecars('tr_same_txn') AS sidecars, (SELECT count(*) FROM tr_same_txn) AS rows; COMMIT; SELECT cfs_relation_has_sidecars('tr_same_txn') AS sidecars, (SELECT count(*) FROM tr_same_txn) AS rows; DROP TABLE tr_same_txn; -- A committed table truncated twice inside one transaction: the second -- TRUNCATE runs the in-place path on the fresh relfilenode the first one -- assigned, whose sidecars must likewise exist since the relfilenode swap. CREATE TABLE tr_twice (id int) WITH (algorithm = 'zstd'); BEGIN; TRUNCATE tr_twice; TRUNCATE tr_twice; SELECT cfs_relation_has_sidecars('tr_twice') AS sidecars, (SELECT count(*) FROM tr_twice) AS rows; COMMIT; DROP TABLE tr_twice; DROP TABLE tr_cfs, tr_empty; DROP FUNCTION cfs_evict(regclass); DROP FUNCTION cfs_base_relation_files(oid); DROP EXTENSION pg_buffercache;