# Copyright (c) 2026, QDB Development Group # # src/test/modules/test_cfs/t/005_cfs_mapload_race.pl # # cfs_map_load() reads the map snapshot # and the journal in two preads with no lock held. A concurrent # checkpoint fold+reset landing between them folds the healing journal # records into the map and resets the journal, so the stale snapshot's # corrupt rows look unhealed and the load reported corruption the map # file no longer had (the smgrimmedsync full-map open is the SQL-reachable # victim). The load now samples the journal identity around the two # reads and retries once when unhealed rows meet a changed identity. # # The race window is forced deterministically with the injection point # cfs-map-load-before-journal (src/backend/storage/cfs/cfs_storage.c): the # victim session pauses between its snapshot pread and its journal pread, # the main session completes a whole checkpoint (fold + journal reset) # inside the pause, and the victim must retry on the changed identity. # The attachment is process-local, so only the victim's load waits. use strict; use warnings FATAL => 'all'; use Time::HiRes qw(usleep); use PostgreSQL::Test::Utils; use PostgreSQL::Test::Cluster; use Test::More; if ($ENV{enable_injection_points} ne 'yes') { plan skip_all => 'Injection points not supported by this build'; } my $node = PostgreSQL::Test::Cluster->new('cfs_mapload_race'); $node->init; $node->append_conf('postgresql.conf', "checkpoint_timeout = 30min\nautovacuum = off\nshared_buffers = 1MB"); $node->start; if (!check_pg_config("#define USE_LZ4")) { plan skip_all => 'LZ4 support not built, skipping CFS map load race test'; } if (!$node->check_extension('injection_points')) { plan skip_all => 'Extension injection_points not installed'; } $node->safe_psql('postgres', 'CREATE EXTENSION test_cfs'); $node->safe_psql('postgres', 'CREATE EXTENSION injection_points'); # Rows folded into the map by the first checkpoint; then a block-0 update # whose dirty page is evicted (the 1 MB shared_buffers overflows on the # filler insert, the same eviction trick as the recovery suite's fold # tests), so the journal holds a real UPDATE record covering block 0 by # the time the victim opens its handle. $node->safe_psql( 'postgres', q{ CREATE TABLE race_t (g int, payload text) WITH (algorithm = 'lz4'); INSERT INTO race_t SELECT g, repeat(md5(g::text), 60) FROM generate_series(1, 200) g; CHECKPOINT; UPDATE race_t SET payload = repeat(md5(g::text || 'u'), 60) WHERE g <= 3; CREATE TABLE filler (g int, payload text) WITH (algorithm = 'lz4'); INSERT INTO filler SELECT g, repeat(md5(g::text), 60) FROM generate_series(1, 1200) g; }); my $dd = $node->data_dir; my $path = $node->safe_psql('postgres', "SELECT pg_relation_filepath('race_t')"); my $map = "$dd/$path.cfsmap"; my $jrn = "$dd/$path.cfsjrn"; cmp_ok(-s $jrn, '>', 16, 'journal holds an UPDATE record covering block 0'); # Flip one byte of the block 0 map row: a healable corruption - the # journal record the last UPDATE appended still covers the block, so the # next load (or the checkpoint fold below) heals it. { my $off = 8 + 0 * 24; open my $fh, '+<:raw', $map or die "open $map: $!"; seek($fh, $off, 0) or die "seek: $!"; my $b; read($fh, $b, 1); seek($fh, $off, 0); print $fh chr(ord($b) ^ 0xFF); close($fh); } pass('flipped one byte of the block 0 map row'); # The victim: arm a process-local mid-load pause in ITS session only, then # run the immedsync. Its lock-free full-map open reads the corrupt # snapshot, waits between the two reads, and finally records the outcome in # race_result (an ERROR aborts the INSERT, so no row ever appears - that is # exactly the failure this test pins). $node->safe_psql('postgres', 'CREATE TABLE race_result (ok bool)'); # on_error_stop => 0: the raced load raises a corruption ERROR, and the # victim session must survive it to let the poll below observe that no # result row ever appears. my $victim = $node->background_psql('postgres', timeout => 60, on_error_stop => 0); $victim->query_until(qr/VICTIM_STARTED/, "\\echo VICTIM_STARTED\n" . "SELECT injection_points_set_local();\n" . "SELECT injection_points_attach('cfs-map-load-before-journal', 'wait');\n" . "INSERT INTO race_result SELECT cfs_test_immedsync('race_t');\n"); # Wait until the snapshot read has happened and the victim is parked before # the journal replay, so the checkpoint below provably lands inside the # victim's window. $node->wait_for_event('client backend', 'cfs-map-load-before-journal'); # Complete a whole checkpoint inside the window: the fold rewrites the # corrupt row from its journal record and the reset empties the journal, # changing the journal identity the victim sampled before its snapshot. $node->safe_psql('postgres', 'CHECKPOINT'); is(-s $jrn, 16, 'the foreign checkpoint reset the journal inside the window'); $node->safe_psql('postgres', "SELECT injection_points_wakeup('cfs-map-load-before-journal')"); # The victim resumed on a changed journal: it must retry the load on the # fresh files instead of reporting the stale snapshot's corruption. Poll # for its row. my $result_deadline = time() + 30; while (time() < $result_deadline) { last if $node->safe_psql('postgres', 'SELECT count(*) FROM race_result') eq '1'; usleep(100_000); } $victim->quit; is($node->safe_psql('postgres', 'SELECT ok FROM race_result'), 't', 'the raced immedsync completed without an error'); is($node->safe_psql('postgres', 'SELECT count(*) FROM race_t'), '200', 'the relation reads normally after the raced load'); # The victim's immedsync was itself a full-snapshot writer: its journal # reset leaves a bare header. is(-s $jrn, 16, 'the immedsync left a bare journal header'); $node->stop('fast'); done_testing();