-- ----------------------------------------------------------------------
|
-- 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';
|
shmem_failures
|
----------------
|
0
|
(1 row)
|
|
SELECT test_name, result, detail
|
FROM cfs_run_shmem_tests()
|
ORDER BY test_name;
|
test_name | result | detail
|
------------------------------------+--------+-------------------------------------------------------
|
bg_pause_expiry | ok | pause/expiry/status ok
|
read_cached_entry_corruption | ok | corrupt cached entry rejected and invalidated
|
shared_map_api | ok | segment pool API ok
|
shared_map_clock_sweep | ok | clock sweep kept the hot segment
|
shared_map_drop_database | ok | drop database kept other databases
|
shared_map_ensure | ok | segment load ok
|
shared_map_eviction | ok | eviction ok
|
shared_map_eviction_pinned | ok | pinned eviction ok
|
shared_map_fallback_read | ok | fallback_reads +1, data ok
|
shared_map_generation_gate | ok | generation gate ok
|
shared_map_invalidate_index | ok | relation index scoped the invalidation
|
shared_map_journal_aba | ok | journal truncation vs segment sync ok
|
shared_map_nblocks_gate | ok | nblocks gate ok
|
shared_map_pool_exhaustion | ok | pinned admission ok
|
shared_map_replay_corruption | ok | mid-journal corruption rejected
|
shared_map_resident_store_no_evict | ok | resident store evictions 0
|
shared_map_short_read | ok | torn tail ignored, healed from journal (healed)
|
shared_map_truncate_resurrect | ok | truncate resurrection ok
|
shared_map_unhealed_corruption | ok | block quarantined: auth 1/1 pool 1/1 again 1 lookup 1
|
shared_map_write_no_load | ok | write loaded 0 segments, reads ok
|
shmem_cached_io | ok | cached write/read reads=2 writes=1
|
shmem_chains | ok | 64 colliding keys, chains keep exact slots
|
shmem_crc_point_invalidate | ok | CRC error dropped one entry; meta slot survived
|
shmem_crc_survives_invalidate | ok | crc_failures survives invalidate, reset clears
|
shmem_generation_check | ok | stale-generation hit rejected
|
shmem_invalidate | ok | invalidate ok
|
shmem_meta_overflow | ok | excess relation rejected with ERROR
|
shmem_meta_pin | ok | meta/pin/release ok
|
shmem_pin_reaper | ok | pin reaper ok
|
shmem_publish_postcommit_overflow | ok | post-commit overflow degraded to a WARNING and a skip
|
shmem_stale_handle_hit | ok | stale handle reopened on hit ok
|
shmem_stats | ok | counters reads=0 writes=0 after reset reads=0
|
shmem_truncate_meta | ok | truncate shrank meta, invalidate dropped it
|
(33 rows)
|
|
-- ----------------------------------------------------------------------
|
-- 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;
|
shared_level
|
--------------
|
3
|
(1 row)
|
|
-- 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);
|
cfs_test_defer_publish
|
------------------------
|
|
(1 row)
|
|
SELECT cfs_test_publish_pending('cfs_defer') AS pending;
|
pending
|
---------
|
t
|
(1 row)
|
|
SELECT cfs_test_shared_level('cfs_defer') AS shared_level_before_commit;
|
shared_level_before_commit
|
----------------------------
|
3
|
(1 row)
|
|
COMMIT;
|
SELECT cfs_test_shared_level('cfs_defer') AS shared_level_after_commit;
|
shared_level_after_commit
|
---------------------------
|
9
|
(1 row)
|
|
-- A deferred change that is rolled back must not reach the shared cache.
|
BEGIN;
|
SELECT cfs_test_defer_publish('cfs_defer', 11);
|
cfs_test_defer_publish
|
------------------------
|
|
(1 row)
|
|
SELECT cfs_test_shared_level('cfs_defer') AS shared_level_before_rollback;
|
shared_level_before_rollback
|
------------------------------
|
9
|
(1 row)
|
|
ROLLBACK;
|
SELECT cfs_test_shared_level('cfs_defer') AS shared_level_after_rollback;
|
shared_level_after_rollback
|
-----------------------------
|
9
|
(1 row)
|
|
-- A repeated defer within one transaction keeps only the last version.
|
BEGIN;
|
SELECT cfs_test_defer_publish('cfs_defer', 5);
|
cfs_test_defer_publish
|
------------------------
|
|
(1 row)
|
|
SELECT cfs_test_defer_publish('cfs_defer', 6);
|
cfs_test_defer_publish
|
------------------------
|
|
(1 row)
|
|
COMMIT;
|
SELECT cfs_test_shared_level('cfs_defer') AS shared_level_after_double_defer;
|
shared_level_after_double_defer
|
---------------------------------
|
6
|
(1 row)
|
|
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;
|
shared_after_real_alter
|
-------------------------
|
4
|
(1 row)
|
|
-- 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;
|
pending_inside_alter
|
----------------------
|
t
|
(1 row)
|
|
ROLLBACK;
|
SELECT cfs_test_shared_level('cfs_defer_ddl') AS shared_after_top_rollback;
|
shared_after_top_rollback
|
---------------------------
|
4
|
(1 row)
|
|
INSERT INTO cfs_defer_ddl VALUES (2);
|
SELECT cfs_test_shared_level('cfs_defer_ddl') AS shared_after_rollback_write;
|
shared_after_rollback_write
|
-----------------------------
|
4
|
(1 row)
|
|
-- 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;
|
pending_inside_savepoint
|
--------------------------
|
t
|
(1 row)
|
|
ROLLBACK TO SAVEPOINT sp;
|
SELECT cfs_test_publish_pending('cfs_defer_ddl') AS pending_after_sub_rollback;
|
pending_after_sub_rollback
|
----------------------------
|
f
|
(1 row)
|
|
COMMIT;
|
SELECT cfs_test_shared_level('cfs_defer_ddl') AS shared_after_sub_rollback;
|
shared_after_sub_rollback
|
---------------------------
|
4
|
(1 row)
|
|
-- 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;
|
pending_inside_reset
|
----------------------
|
t
|
(1 row)
|
|
ROLLBACK TO SAVEPOINT nnf2_sp;
|
COMMIT;
|
SELECT cfs_test_shared_level_at(:nnf2_node) AS shared_after_reset_rollback;
|
shared_after_reset_rollback
|
-----------------------------
|
7
|
(1 row)
|
|
-- 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;
|
pending_inside_release
|
------------------------
|
t
|
(1 row)
|
|
RELEASE SAVEPOINT nnf3_sp;
|
SELECT cfs_test_publish_pending_at(:nnf3_node) AS pending_after_release;
|
pending_after_release
|
-----------------------
|
t
|
(1 row)
|
|
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;
|
shared_after_release_commit
|
-----------------------------
|
|
(1 row)
|
|
-- 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;
|
pending_before_disable
|
------------------------
|
t
|
(1 row)
|
|
ALTER TABLE cfs_defer_ddl RESET (algorithm);
|
SELECT cfs_test_publish_pending_at(:old_node) AS pending_after_disable;
|
pending_after_disable
|
-----------------------
|
t
|
(1 row)
|
|
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;
|
shared_old_node_after_commit
|
------------------------------
|
|
(1 row)
|
|
-- 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;
|
guard_sidecars
|
----------------
|
t
|
(1 row)
|
|
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);
|
cfs_test_publish_false_at
|
---------------------------
|
|
(1 row)
|
|
COMMIT;
|
CHECKPOINT;
|
SELECT cfs_test_sidecars_at(:guard_node) AS leaked_sidecars;
|
leaked_sidecars
|
-----------------
|
f
|
(1 row)
|
|
-- 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);
|
cfs_test_publish_false_at
|
---------------------------
|
|
(1 row)
|
|
SELECT cfs_test_slot_present_at(:slot_guard_node) AS slot_planted;
|
slot_planted
|
--------------
|
t
|
(1 row)
|
|
DROP TABLE cfs_unlink_slot_guard;
|
COMMIT;
|
SELECT cfs_test_slot_present_at(:slot_guard_node) AS stale_slot_left;
|
stale_slot_left
|
-----------------
|
f
|
(1 row)
|
|
DROP TABLE cfs_defer_ddl;
|