-- ---------------------------------------------------------------------- -- Shared-memory map cache, statistics, eviction, cached I/O -- ---------------------------------------------------------------------- -- The CRC-invalidation case deliberately corrupts a .cfp record; silence -- the resulting WARNINGs so the expected output stays deterministic. SET client_min_messages = error; SELECT count(*) AS shmem_failures FROM cfs_run_shmem_tests() WHERE result <> 'ok'; SELECT test_name, result, detail FROM cfs_run_shmem_tests() ORDER BY test_name; -- ---------------------------------------------------------------------- -- Deferred policy publication: an ALTER changing only the tuning -- (level) queues the new policy for the end of the transaction instead of -- publishing it into shared memory immediately. A committed transaction -- publishes the new level; an aborted one leaves the previous level in the -- shared cache. -- ---------------------------------------------------------------------- CREATE TABLE cfs_defer (i int) WITH (algorithm = 'lz4', level = 3); -- Touch the relation through the smgr layer so its frozen policy (level 3) -- is published into the shared cache. INSERT INTO cfs_defer VALUES (1); -- The shared cache now holds the table's frozen policy (level 3). SELECT cfs_test_shared_level('cfs_defer') AS shared_level; -- Defer a level change inside a transaction: the shared cache must still -- show the old level, the pending flag must be set, and COMMIT publishes 9. BEGIN; SELECT cfs_test_defer_publish('cfs_defer', 9); SELECT cfs_test_publish_pending('cfs_defer') AS pending; SELECT cfs_test_shared_level('cfs_defer') AS shared_level_before_commit; COMMIT; SELECT cfs_test_shared_level('cfs_defer') AS shared_level_after_commit; -- A deferred change that is rolled back must not reach the shared cache. BEGIN; SELECT cfs_test_defer_publish('cfs_defer', 11); SELECT cfs_test_shared_level('cfs_defer') AS shared_level_before_rollback; ROLLBACK; SELECT cfs_test_shared_level('cfs_defer') AS shared_level_after_rollback; -- A repeated defer within one transaction keeps only the last version. BEGIN; SELECT cfs_test_defer_publish('cfs_defer', 5); SELECT cfs_test_defer_publish('cfs_defer', 6); COMMIT; SELECT cfs_test_shared_level('cfs_defer') AS shared_level_after_double_defer; DROP TABLE cfs_defer; -- ---------------------------------------------------------------------- -- The same semantics through the real DDL path (ALTER TABLE), including -- subtransaction rollback and turning CFS off in the same -- transaction: the deferred list must follow the transaction tree, -- publishing exactly what the committed catalog says. -- ---------------------------------------------------------------------- CREATE TABLE cfs_defer_ddl (i int) WITH (algorithm = 'lz4', level = 3); -- Publish the frozen policy (level 3) through the smgr layer. INSERT INTO cfs_defer_ddl VALUES (1); -- An autocommitted tuning ALTER publishes the new level after COMMIT. ALTER TABLE cfs_defer_ddl SET (level = 4); SELECT cfs_test_shared_level('cfs_defer_ddl') AS shared_after_real_alter; -- A top-level ROLLBACK of a real tuning ALTER leaves the shared policy -- untouched as well: the publication is deferred to PRE_COMMIT, which an -- aborted transaction never reaches. Subsequent writes through the smgr -- layer keep resolving the old level. BEGIN; ALTER TABLE cfs_defer_ddl SET (level = 10); SELECT cfs_test_publish_pending('cfs_defer_ddl') AS pending_inside_alter; ROLLBACK; SELECT cfs_test_shared_level('cfs_defer_ddl') AS shared_after_top_rollback; INSERT INTO cfs_defer_ddl VALUES (2); SELECT cfs_test_shared_level('cfs_defer_ddl') AS shared_after_rollback_write; -- An ALTER rolled back to a savepoint must not leave a pending -- publication, and the commit must publish the surviving catalog level. BEGIN; SAVEPOINT sp; ALTER TABLE cfs_defer_ddl SET (level = 9); SELECT cfs_test_publish_pending('cfs_defer_ddl') AS pending_inside_savepoint; ROLLBACK TO SAVEPOINT sp; SELECT cfs_test_publish_pending('cfs_defer_ddl') AS pending_after_sub_rollback; COMMIT; SELECT cfs_test_shared_level('cfs_defer_ddl') AS shared_after_sub_rollback; -- A RESET (algorithm) rolled back to a savepoint must not eat the pending -- publication of the outer level: a transition to non-CFS queues nothing -- (the entries of the old node publish harmlessly at commit and the unlink -- cleans up), so the outer entry survives to the commit untouched. SELECT pg_relation_filenode('cfs_defer_ddl') AS nnf2_node \gset BEGIN; ALTER TABLE cfs_defer_ddl SET (level = 7); SAVEPOINT nnf2_sp; ALTER TABLE cfs_defer_ddl RESET (algorithm); SELECT cfs_test_publish_pending_at(:nnf2_node) AS pending_inside_reset; ROLLBACK TO SAVEPOINT nnf2_sp; COMMIT; SELECT cfs_test_shared_level_at(:nnf2_node) AS shared_after_reset_rollback; -- A RESET (algorithm) whose subtransaction is RELEASEd (committed) leaves -- the outer entry queued for the replaced node: it publishes harmlessly at -- commit — the vanilla double-entry answer — because the replaced node is -- unlinked at the same commit and its slot is forgotten with the sidecars. SELECT pg_relation_filenode('cfs_defer_ddl') AS nnf3_node \gset BEGIN; ALTER TABLE cfs_defer_ddl SET (level = 6); SAVEPOINT nnf3_sp; ALTER TABLE cfs_defer_ddl RESET (algorithm); SELECT cfs_test_publish_pending_at(:nnf3_node) AS pending_inside_release; RELEASE SAVEPOINT nnf3_sp; SELECT cfs_test_publish_pending_at(:nnf3_node) AS pending_after_release; COMMIT; -- The old node is replaced and unlinked at commit; its slot is forgotten -- with the sidecar files, so no CFS policy remains for it. SELECT cfs_test_shared_level_at(:nnf3_node) AS shared_after_release_commit; -- Restore the CFS state (algorithm and level) for the sections below; the -- non-CFS -> CFS transition rewrites the relation into a new storage node. ALTER TABLE cfs_defer_ddl SET (algorithm = 'lz4', level = 3); -- Turning CFS off in the same transaction leaves the earlier entry queued -- for the old storage node (the transition always rewrites into a new -- one): the entry publishes harmlessly at commit, and the unlink of the -- replaced node forgets the slot. SELECT pg_relation_filenode('cfs_defer_ddl') AS old_node \gset BEGIN; ALTER TABLE cfs_defer_ddl SET (level = 5); SELECT cfs_test_publish_pending_at(:old_node) AS pending_before_disable; ALTER TABLE cfs_defer_ddl RESET (algorithm); SELECT cfs_test_publish_pending_at(:old_node) AS pending_after_disable; COMMIT; -- The old node is gone at commit; no CFS policy may remain for it. SELECT cfs_test_shared_level_at(:old_node) AS shared_old_node_after_commit; -- Regression guard: the unlink of CFS sidecars must be decided physically, -- from the files on disk, never from the policy cache — even a slot lying -- is_cfs=false for a node whose sidecar files still exist must not leak -- them (the mdunlink() principle — files are removed unconditionally). The -- lie is planted after the last catalog phase of the DROP, in the same -- transaction: the window where the deferred-publication commit callbacks -- run before smgrDoPendingDeletes(). CREATE TABLE cfs_unlink_guard (a int) WITH (algorithm = 'lz4'); INSERT INTO cfs_unlink_guard SELECT g FROM generate_series(1, 1000) g; SELECT cfs_relation_has_sidecars('cfs_unlink_guard') AS guard_sidecars; SELECT pg_relation_filenode('cfs_unlink_guard') AS guard_node \gset BEGIN; DROP TABLE cfs_unlink_guard; SELECT cfs_test_publish_false_at(:guard_node); COMMIT; CHECKPOINT; SELECT cfs_test_sidecars_at(:guard_node) AS leaked_sidecars; -- Regression guard, part two: whatever the physical predicate says, the -- unlink must always forget the cached policy slot of the unlinked node — -- a dropped node must not leave a stale slot behind even when it never had -- sidecar files. A plain non-CFS table with a planted lying slot (and no -- sidecars on disk) discriminates the old early-return behavior, which -- skipped cfs_forget_options() together with the sidecar removal. CREATE TABLE cfs_unlink_slot_guard (a int); SELECT pg_relation_filenode('cfs_unlink_slot_guard') AS slot_guard_node \gset BEGIN; SELECT cfs_test_publish_false_at(:slot_guard_node); SELECT cfs_test_slot_present_at(:slot_guard_node) AS slot_planted; DROP TABLE cfs_unlink_slot_guard; COMMIT; SELECT cfs_test_slot_present_at(:slot_guard_node) AS stale_slot_left; DROP TABLE cfs_defer_ddl;