# QDB-1507 (U2): cfs_bg_period_ms changed via reload must apply to the # CURRENT wait of the compaction launcher, not only after the first full # period elapses. # # Scenario: a node starts with the default period of one hour; a fragmented # CFS table is created; the period is lowered to 1000 ms via ALTER SYSTEM + # pg_reload_conf(). The launcher must run a compaction pass within about a # second (pg_cfs_bgworker_status().last_cycle_started_at becomes non-NULL) # and the pass must compact the fragmented relation (last_compaction set). # Before the fix the absolute next_run schedule kept sleeping out the # remainder of the old hour and the pass never started. use strict; use warnings FATAL => 'all'; use PostgreSQL::Test::Cluster; use Test::More; plan tests => 3; my $node = PostgreSQL::Test::Cluster->new('cfs_bg_reload'); # Threshold/min-interval are per-pass parameters: make every pass eligible # and allow back-to-back passes. The period itself stays at the default # (3600000 ms) until the reload below. $node->init; $node->append_conf( 'postgresql.conf', qq{ cfs_bg_threshold = 1 cfs_bg_min_interval_s = 0 }); $node->start; $node->safe_psql( 'postgres', qq{ CREATE TABLE bg_reload_t (id int, v text) WITH (algorithm='lz4'); INSERT INTO bg_reload_t SELECT g, repeat('r' || g, 50) FROM generate_series(1, 20000) g; CHECKPOINT; UPDATE bg_reload_t SET v = v || 'u' WHERE id % 2 = 0; CHECKPOINT; }); # Sanity: the launcher is up and idle on the default hour-long schedule. is($node->safe_psql('postgres', q{SELECT last_cycle_started_at IS NULL FROM pg_cfs_bgworker_status()}), 't', 'no cycle before the period is lowered'); # Reload the shortened period (PGC_SIGHUP): the current wait must be # re-armed to the new period. Poll up to 15 s, a comfortable margin over # the expected ~1 s. $node->safe_psql('postgres', 'ALTER SYSTEM SET cfs_bg_period_ms = 1000'); $node->reload; ok($node->poll_query_until('postgres', q{SELECT last_cycle_started_at IS NOT NULL FROM pg_cfs_bgworker_status()}, 't', 15), 'compaction pass starts within ~15 s of the reload'); # The pass must actually process the fragmented relation: the dead ratio is # far above the threshold of 1 percent. ok($node->poll_query_until('postgres', q{SELECT COALESCE((SELECT last_compaction IS NOT NULL FROM pg_stat_cfs WHERE relname = 'bg_reload_t'), false)}, 't', 25), 'fragmented relation compacted after the reload'); $node->stop; done_testing();