# Copyright (c) 2026, Kirillov Vyacheslav by AI, QDB Development Group # # QDB-1513 P4-2: the offloaded CFS read path (io_method=worker plus the # shared read scratch arena). Server-level behaviour that needs a custom # configuration and therefore cannot live in the regression schedule: # # 1. cfs_read_scratch_mb is an observable postmaster GUC, and 0 keeps the # server fully functional on the inline path. # 2. Bit-identical data: the same cold table read through the offload # configuration and through the inline one produces the same md5, and # the offload configuration really batched (pg_stat_cfs counters). # 3. A worker dying right after the reopen (the aio-worker-after-reopen # injection) fails the in-flight read through the AIO result path # ("could not read block"), the server stays up, and a retry after the # detach succeeds — the transient-error contract of the compaction # race handling. Requires --enable-injection-points; skipped here # otherwise (the verify stand runs it). # 4. A truncated .cfp tail must surface as a corruption ERROR through # cfs_report_corruption() ("checksum or format corruption"), not as an # internal IO error — the EOF error currency of the inline span path. use strict; use warnings FATAL => 'all'; use PostgreSQL::Test::Cluster; use Test::More; # Check the live processes, not just the configured worker limits. sub check_io_workers { my ($node) = @_; ok($node->poll_query_until('postgres', q{SELECT count(*) = 2 FROM pg_stat_activity WHERE backend_type = 'io worker'}), 'two I/O workers are running'); } my $node = PostgreSQL::Test::Cluster->new('cfs_scratch'); $node->init; $node->append_conf('postgresql.conf', qq{ io_method = worker io_min_workers = 2 io_max_workers = 2 cfs_read_scratch_mb = 32 autovacuum = off }); $node->start; check_io_workers($node); is($node->safe_psql('postgres', 'SHOW io_min_workers'), '2', 'minimum I/O workers'); is($node->safe_psql('postgres', 'SHOW io_max_workers'), '2', 'maximum I/O workers'); my $setting = $node->safe_psql('postgres', "SELECT setting FROM pg_settings WHERE name = 'cfs_read_scratch_mb'"); is($setting, '32', 'arena enabled at server start'); # One compact lz4 heap: a bulk INSERT appends records back-to-back, so a # cold seqscan meets full runs and the read stream batches them. $node->safe_psql('postgres', "CREATE TABLE sc_lz4 (id int, payload text) WITH (algorithm = 'lz4')"); $node->safe_psql('postgres', "INSERT INTO sc_lz4 SELECT g, repeat('sc ', 60) FROM generate_series(1, 5000) g"); $node->safe_psql('postgres', 'CHECKPOINT'); my $md5_expected = $node->safe_psql('postgres', "SELECT md5(string_agg(payload, '' ORDER BY id)) FROM sc_lz4"); # The offloaded read: cold restart so every page comes from the sidecars # through read_stream -> smgrstartreadv -> the scratch arena. $node->restart; check_io_workers($node); my $md5_offload = $node->safe_psql('postgres', "SELECT md5(string_agg(payload, '' ORDER BY id)) FROM sc_lz4"); is($md5_offload, $md5_expected, 'offloaded read is bit-identical'); my $batched = $node->safe_psql('postgres', "SELECT read_batched_pages > 0 AND read_count = read_batched_pages + read_fallback_pages FROM pg_stat_cfs WHERE relid = 'sc_lz4'::regclass"); diag("offload stats: " . $node->safe_psql('postgres', "SELECT read_count || ' read, ' || read_batched_pages || ' batched, ' || read_fallback_pages || ' fallback, io_method=' || (SELECT setting FROM pg_settings WHERE name = 'io_method') FROM pg_stat_cfs WHERE relid = 'sc_lz4'::regclass")); is($batched, 't', 'offload configuration batched the reads'); # Worker dies right after reopening the segment: the read must fail # through the AIO result path with a retryable error, not crash. subtest 'worker reopen failure is a transient read error' => sub { plan skip_all => 'Injection points not supported by this build' if $ENV{enable_injection_points} ne 'yes'; if (!$node->check_extension('injection_points')) { plan skip_all => 'Extension injection_points not installed'; } $node->safe_psql('postgres', 'CREATE EXTENSION IF NOT EXISTS injection_points'); # Cold again, so the scan actually goes through the workers. $node->restart; check_io_workers($node); $node->safe_psql('postgres', "SELECT injection_points_attach('aio-worker-after-reopen', 'error')"); my ($ret, $out, $stderr) = $node->psql('postgres', 'SELECT count(*) FROM sc_lz4'); isnt($ret, 0, 'injected reopen failure fails the scan'); like($stderr, qr/could not read block/, 'reopen failure reports through the AIO result path'); $node->safe_psql('postgres', "SELECT injection_points_detach('aio-worker-after-reopen')"); $node->restart; check_io_workers($node); my $md5_retry = $node->safe_psql('postgres', "SELECT md5(string_agg(payload, '' ORDER BY id)) FROM sc_lz4"); is($md5_retry, $md5_expected, 'retry after the failure is bit-identical'); }; # The inline comparison: same data, arena off. The restart both switches # the mode and makes the buffers cold. $node->stop; $node->append_conf('postgresql.conf', 'cfs_read_scratch_mb = 0'); $node->start; check_io_workers($node); $setting = $node->safe_psql('postgres', "SELECT setting FROM pg_settings WHERE name = 'cfs_read_scratch_mb'"); is($setting, '0', 'arena disabled at 0 MB'); my $md5_inline = $node->safe_psql('postgres', "SELECT md5(string_agg(payload, '' ORDER BY id)) FROM sc_lz4"); is($md5_inline, $md5_expected, 'inline read matches the offloaded one'); # A truncated .cfp tail (a partial copy, a crashed grow) shortens the # span preadv: that is corruption, not an IO error. my $relpath = $node->safe_psql('postgres', q{SELECT pg_relation_filepath('sc_lz4')}); $node->stop; # The truncated-tail part works directly on the data files: this fork's # test cluster keeps the path in data_dir(), not in a pgdata field. my @segs = sort glob($node->data_dir . '/' . $relpath . '.cfp*'); ok(@segs > 0, 'sidecar segments found') or BAIL_OUT('no .cfp sidecars'); # The chain's last segment carries the highest suffix (a bare .cfp sorts # first); truncating its tail cuts records the map still references. my $last = $segs[-1]; my $size = -s $last; ok(truncate($last, $size - 8192), 'truncated the segment tail'); # Back to the offload configuration for the short-read path. $node->append_conf('postgresql.conf', 'cfs_read_scratch_mb = 32'); $node->start; check_io_workers($node); my ($ret, $out, $stderr) = $node->psql('postgres', 'SELECT count(*) FROM sc_lz4'); isnt($ret, 0, 'truncated tail fails the scan'); like($stderr, qr/checksum or format corruption/, 'short span read reports corruption, not an IO error'); # The failed offload span must run the same pool bookkeeping as the # inline path: the crc failure is counted, so the counters observable # in pg_stat_cfs stay identical between the two paths. my $crc = $node->safe_psql('postgres', "SELECT crc_failures > 0 FROM pg_stat_cfs WHERE relid = 'sc_lz4'::regclass"); is($crc, 't', 'short span read counts the crc failure'); $node->stop; done_testing();