# Copyright (c) 2026, QDB Development Group # # src/test/modules/test_cfs/t/008_cfs_compact_online.pl # # Online compaction (QDB-1514): pg_cfs_compact() runs under # ShareUpdateExclusiveLock, so while the copy phase (stretched with # cfs_debug_compact_delay_ms) is in flight: # * concurrent reads and writes of the relation complete; # * DDL that needs a stronger lock is rejected under a short lock_timeout # (the lock conflict is real, the level is not AccessExclusive); # * the catch-up phase carries the concurrently appended records over # (mixed UPDATE/INSERT/DELETE workload), and the compacted pair passes # pg_cfs_check() with the dead counters reset; # * the offline fallback (cfs_compact_online = off) still compacts; # * the background worker reaches a relation that is being read the whole # time — under the historical AccessExclusiveLock regime its conditional # lock could never be taken on a busy relation. use strict; use warnings FATAL => 'all'; use PostgreSQL::Test::Cluster; use PostgreSQL::Test::Utils; use Test::More; my $node = PostgreSQL::Test::Cluster->new('cfs_compact_online'); $node->init; $node->append_conf('postgresql.conf', "autovacuum = off\ncheckpoint_timeout = 30min\nlog_min_messages = warning"); $node->start; $node->safe_psql('postgres', 'CREATE EXTENSION test_cfs'); # --------------------------------------------------------------------------- # 1. Online compaction under a mixed concurrent workload. # --------------------------------------------------------------------------- $node->safe_psql( 'postgres', q{ CREATE TABLE co_t (id int, payload text) WITH (algorithm = 'lz4'); INSERT INTO co_t SELECT g, repeat('a' || g, 2000) FROM generate_series(1, 2000) g; CHECKPOINT; UPDATE co_t SET payload = repeat('b' || id, 2000); CHECKPOINT; }); my $dead_before = $node->safe_psql('postgres', "SELECT dead_bytes FROM pg_stat_cfs WHERE relname = 'co_t'"); ok($dead_before > 0, "table has dead space before compaction ($dead_before)"); # A concurrent writer: one long statement of mixed operations that keeps # appending to the relation while the compaction copies. The final state # is deterministic: +400 inserted, -400 deleted, the rest updated. my $writer = $node->background_psql('postgres', timeout => 180); $writer->query_until(qr/WRITE_LOOP_READY/, "\\echo WRITE_LOOP_READY\n" . "DO \$\$ DECLARE i int; BEGIN\n" . " FOR i IN 1..250 LOOP\n" . " UPDATE co_t SET payload = payload || 'w' || i WHERE id = i;\n" . " INSERT INTO co_t VALUES (10000 + i, repeat('c' || i, 2000));\n" . " DELETE FROM co_t WHERE id = 100 + i;\n" . " END LOOP;\n" . "END \$\$;\n" . "\\echo WRITE_LOOP_DONE\n"); # The compaction, stretched so the copy is still running while the writer # and the probe queries below are in flight. my $compactor = $node->background_psql('postgres', timeout => 180); $compactor->query_until(qr/COMPACT_READY/, "\\echo COMPACT_READY\n" . "SET cfs_debug_compact_delay_ms = 100;\n" . "SELECT pg_cfs_compact('co_t');\n" . "\\echo COMPACT_DONE\n"); # Give the copy phase a moment to get going, then prove that reads and # writes proceed: the reader SELECT completes while the compaction runs # (ShareUpdateExclusiveLock does not conflict with SELECT's # AccessShareLock), and the writer loop above is still appending. sleep(1); is($node->safe_psql('postgres', 'SELECT count(*) FROM co_t'), '2000', 'reads run while the online compaction copies'); # DDL must conflict: a short lock_timeout turns the would-be wait into an # immediate, identifiable failure while the compaction holds the relation # lock. (No lock conflict at all would mean the compaction holds nothing; # no conflict with plain reads above means the level is not exclusive.) my $ddl; { # local statement_timeout/lock_timeout for one probe $ddl = $node->psql('postgres', "SET lock_timeout = '2s'; ALTER TABLE co_t ADD COLUMN probe int;", stderr => \$my::stderr); } isnt($ddl, 0, 'DDL conflicts with the running online compaction'); like($my::stderr, qr/lock_timeout|canceling statement/, 'the DDL probe failed on the lock wait, not on anything else'); # The writer must get through the whole mixed workload while the copy # phase is still running: its completion while the progress view still # shows 'copying live blocks' is the non-blocking proof for WRITES (the # reads were proven by the SELECT above). $writer->query_until(qr/WRITE_LOOP_DONE/, "SELECT 'WRITE_LOOP_DONE';\n"); $writer->quit; is($node->safe_psql('postgres', "SELECT phase FROM pg_stat_progress_cfs_compact WHERE relid = 'co_t'::regclass"), 'copying live blocks', 'the mixed write workload finished inside the copy phase'); $compactor->query_until(qr/COMPACT_DONE/, "SELECT 'COMPACT_DONE';\n"); $compactor->quit; # The catch-up phase carried the concurrent appends over: every row that # survived the workload is readable and the sidecar pair is consistent. is($node->safe_psql('postgres', 'SELECT count(*) FROM co_t'), '2000', 'the workload arithmetic survived the compaction'); is($node->safe_psql('postgres', "SELECT count(*) FROM co_t WHERE id BETWEEN 1 AND 99 AND payload LIKE '%w%'"), '99', 'rows updated during the copy read their new versions'); is($node->safe_psql('postgres', "SELECT count(*) FROM co_t WHERE id > 10000 AND payload = repeat('c' || id - 10000, 2000)"), '250', 'rows inserted during the copy are intact'); is($node->safe_psql('postgres', "SELECT ok FROM pg_cfs_check('co_t')"), 't', 'pg_cfs_check passes after the online compaction'); is($node->safe_psql('postgres', "SELECT dead_bytes FROM pg_stat_cfs WHERE relname = 'co_t'"), '0', 'dead counters are reset after the online compaction'); # --------------------------------------------------------------------------- # 2. Offline fallback: cfs_compact_online = off keeps compacting. # --------------------------------------------------------------------------- $node->safe_psql('postgres', "ALTER SYSTEM SET cfs_compact_online = off"); $node->safe_psql('postgres', 'SELECT pg_reload_conf()'); # A fresh session picks the reload up at start. $node->safe_psql('postgres', "UPDATE co_t SET payload = payload || 'x'"); $node->safe_psql('postgres', 'CHECKPOINT'); my $offline = $node->safe_psql('postgres', "SELECT pg_cfs_compact('co_t') LIKE 'compacted:%'"); is($offline, 't', 'offline mode compacts'); is($node->safe_psql('postgres', "SELECT ok FROM pg_cfs_check('co_t')"), 't', 'pg_cfs_check passes after the offline compaction'); $node->safe_psql('postgres', "ALTER SYSTEM RESET cfs_compact_online"); $node->safe_psql('postgres', 'SELECT pg_reload_conf()'); # --------------------------------------------------------------------------- # 3. The background worker reaches a busy relation. # A reader loop holds AccessShareLock continuously; under the old # AccessExclusiveLock regime the worker's conditional acquire could # never succeed, and the dead space stayed. Now it must compact. # The worker knobs are set before start: a reloaded period does not # pull the already-scheduled next pass earlier (the fixed-schedule # behaviour documented with the launcher loop). # --------------------------------------------------------------------------- my $bgn = PostgreSQL::Test::Cluster->new('cfs_compact_bg_busy'); $bgn->init; $bgn->append_conf('postgresql.conf', "autovacuum = off\ncheckpoint_timeout = 30min\n" . "cfs_bg_period_ms = 1000\ncfs_bg_threshold = 1\n" . "cfs_bg_min_interval_s = 0\ncfs_bg_max_relations = 5"); $bgn->start; $bgn->safe_psql( 'postgres', q{ CREATE TABLE co_busy (id int, payload text) WITH (algorithm = 'lz4'); INSERT INTO co_busy SELECT g, repeat('d' || g, 100) FROM generate_series(1, 1000) g; CHECKPOINT; UPDATE co_busy SET payload = repeat('e' || id, 100); CHECKPOINT; }); my $reader = $bgn->background_psql('postgres', timeout => 180); $reader->query_until(qr/READ_LOOP_READY/, "\\echo READ_LOOP_READY\n" . "DO \$\$ DECLARE i int; BEGIN\n" . " FOR i IN 1..600 LOOP\n" . " PERFORM count(*) FROM co_busy;\n" . " PERFORM pg_sleep(0.05);\n" . " END LOOP;\n" . "END \$\$;\n" . "\\echo READ_LOOP_DONE\n"); my $found = 0; for my $i (1 .. 60) { # last_compaction is the per-relation compaction timestamp: only a # real (successful) compaction of THIS relation sets it. my $done = $bgn->safe_psql('postgres', "SELECT last_compaction IS NOT NULL AND dead_bytes = 0 " . "FROM pg_stat_cfs WHERE relname = 'co_busy'"); if (defined $done && $done eq 't') { $found = 1; last; } sleep(1); } ok($found, 'the background worker compacted the busy relation'); is($bgn->safe_psql('postgres', "SELECT ok FROM pg_cfs_check('co_busy')"), 't', 'pg_cfs_check passes for the busy relation'); $reader->query_until(qr/READ_LOOP_DONE/, "SELECT 'READ_LOOP_DONE';\n"); $reader->quit; $reader->query_until(qr/READ_LOOP_DONE/, "SELECT 'READ_LOOP_DONE';\n"); $reader->quit; $bgn->stop('fast'); $node->stop('fast'); done_testing();