# QDB-1507 (U6): pg_prewarm on a CFS relation routes through # cfs_smgr_prefetch (posix_fadvise WILLNEED over the .cfp ranges resolved # from the block map); 'buffer' mode goes through the regular synchronous # CFS read path. 'prefetch' is advisory, so the observable contract is # the return value (every block counted), a successful buffered prewarm, # and correct decompressed reads after a full buffer eviction. A plain # (non-CFS) relation next to it pins that the md prefetch path is not # affected by the CFS dispatch. use strict; use warnings; use PostgreSQL::Test::Cluster; use PostgreSQL::Test::Utils; use Test::More; plan tests => 6; my $node = PostgreSQL::Test::Cluster->new('cfs_prewarm'); $node->init; $node->start; $node->safe_psql( 'postgres', q{ CREATE EXTENSION pg_prewarm; CREATE EXTENSION pg_buffercache; CREATE TABLE pre_t (id int, payload text) WITH (algorithm = 'lz4'); INSERT INTO pre_t SELECT g, repeat('p' || g, 100) FROM generate_series(1, 2000) g; CHECKPOINT; }); # The logical size the prewarm modes must count. pg_cfs_relation_size() # is the CFS-idiomatic source: the plain pg_relation_size() reads the # main-fork compatibility marker and is not block-aligned for CFS. my $nblocks = $node->safe_psql('postgres', "SELECT (pg_cfs_relation_size('pre_t')).logical_size / " . "current_setting('block_size')::int"); ok($nblocks > 0, "CFS table has blocks to prewarm ($nblocks)"); is($node->safe_psql('postgres', "SELECT pg_prewarm('pre_t', 'prefetch')"), $nblocks, 'pg_prewarm(prefetch) advised every CFS block'); is($node->safe_psql('postgres', "SELECT pg_prewarm('pre_t', 'buffer')"), $nblocks, 'pg_prewarm(buffer) read every CFS block'); # Drop the pages from shared_buffers and go again: prefetch still counts # every block and the sequential scan reads the decompressed data back # correctly through the synchronous CFS read path. $node->safe_psql('postgres', "SELECT pg_buffercache_evict_relation('pre_t')"); is($node->safe_psql('postgres', "SELECT pg_prewarm('pre_t', 'prefetch')"), $nblocks, 'repeated pg_prewarm(prefetch) after eviction'); is($node->safe_psql('postgres', 'SELECT count(*), sum(id) FROM pre_t'), '2000|2001000', 'CFS reads correct after eviction'); # Control: a non-CFS relation beside it takes the md prefetch path # unchanged. $node->safe_psql( 'postgres', q{ CREATE TABLE pre_plain (id int, payload text); INSERT INTO pre_plain SELECT g, repeat('p' || g, 100) FROM generate_series(1, 2000) g; CHECKPOINT; }); my $nblocks_plain = $node->safe_psql('postgres', "SELECT pg_relation_size('pre_plain') / " . "current_setting('block_size')::int"); is($node->safe_psql('postgres', "SELECT pg_prewarm('pre_plain', 'prefetch')"), $nblocks_plain, 'plain table prefetch still advises every block'); $node->stop; done_testing();