# Copyright (c) 2026, QDB Development Group
|
#
|
# src/test/modules/test_cfs/t/009_cfs_stats_capacity.pl
|
#
|
# QDB-1516 T2 + GUC + T4: the configurable statistics capacity and
|
# concurrent extension under metadata-slot eviction pressure.
|
# * cfs_stats_slots_per_partition = 4 (256 relations total, the GUC
|
# minimum) and cfs_map_cache_rels_per_partition = 4 (256 metadata
|
# slots) exercise eviction in both slot domains;
|
# * 300 relations beyond both capacities evict pg_stat_cfs rows,
|
# and a 200-relation working set within the capacity must stay fully
|
# visible (stable working set, no row lost);
|
# * T4: two concurrent bulk inserters extend one CFS relation while the
|
# main session churns metadata slots under them: no
|
# "unexpected data beyond EOF", no lost or duplicated rows;
|
# * a restart re-creates the shared-memory layout (T3-lite) and the
|
# counts survive.
|
|
use strict;
|
use warnings FATAL => 'all';
|
|
use PostgreSQL::Test::Cluster;
|
use PostgreSQL::Test::Utils;
|
use Test::More;
|
|
my $node = PostgreSQL::Test::Cluster->new('cfs_stats_cap');
|
$node->init;
|
# bgwriter_lru_maxpages = 0: the seqscan's hint bits must stay in
|
# shared_buffers. A bgwriter flush of a hint-dirtied page would run it
|
# through the CFS write path, dirty the relation's metadata slot, and
|
# starve the next member of the partition.
|
#
|
# cfs_map_cache_rels_per_partition = 4 (256 metadata slots): still below
|
# the 300 relations of the load below, so metadata eviction keeps
|
# happening, but wide enough for the 200-relation working set: a touch of
|
# a churned-out relation republishes its size through
|
# cfs_map_cache_set_meta(), which marks the slot dirty for the next
|
# checkpoint, so at most capacity members of a partition can be touched
|
# before a checkpoint must clear the dirty marks.
|
$node->append_conf(
|
'postgresql.conf',
|
"autovacuum = off\n"
|
. "checkpoint_timeout = 30min\n"
|
. "log_min_messages = warning\n"
|
. "bgwriter_lru_maxpages = 0\n"
|
. "cfs_stats_slots_per_partition = 4\n"
|
. "cfs_map_cache_rels_per_partition = 4");
|
$node->start;
|
|
if (!check_pg_config("#define USE_LZ4"))
|
{
|
plan skip_all => 'LZ4 support not built, skipping CFS capacity test';
|
}
|
|
$node->safe_psql('postgres', 'CREATE EXTENSION test_cfs');
|
|
# ---------------------------------------------------------------------------
|
# 1. The GUC is visible with the configured value.
|
# ---------------------------------------------------------------------------
|
is($node->safe_psql('postgres', 'SHOW cfs_stats_slots_per_partition'),
|
'4', 'cfs_stats_slots_per_partition is set to 4');
|
is($node->safe_psql('postgres', 'SHOW cfs_map_cache_rels_per_partition'),
|
'4', 'cfs_map_cache_rels_per_partition is set to 4');
|
|
# ---------------------------------------------------------------------------
|
# 2. T2: 300 relations beyond the 256-row statistics capacity.
|
#
|
# The tables are created in checkpointed chunks: with only two metadata
|
# slots per partition a chunk of dirty (not yet checkpointed) relations
|
# could exhaust a partition, and the checkpoint clears dirty flags between
|
# the chunks.
|
# ---------------------------------------------------------------------------
|
for my $round (0 .. 5)
|
{
|
my $lo = $round * 50 + 1;
|
my $sql = '';
|
for my $g ($lo .. $lo + 49)
|
{
|
$sql .= "CREATE TABLE cap_t$g (i int) WITH (algorithm = 'lz4');\n"
|
. "INSERT INTO cap_t$g VALUES ($g);\n";
|
}
|
$sql .= "CHECKPOINT;\n";
|
$node->safe_psql('postgres', $sql);
|
}
|
|
# pg_stat_cfs emits a row for every CFS relation; a relation without a
|
# statistics slot shows zeroed counters. Every cap_t table had exactly one
|
# page written through CFS at its chunk checkpoint, so a live slot always
|
# shows write_count > 0, and an evicted relation shows 0: the live rows are
|
# the capacity observable (bounded by 64 partitions x 4 slots = 256, and
|
# 300 - 256 = 44 relations must have lost their counters at least).
|
my $live = $node->safe_psql('postgres',
|
"SELECT count(*) FILTER (WHERE write_count > 0) FROM pg_stat_cfs"
|
. " WHERE relname ~ '^cap_t[0-9]+\$'");
|
cmp_ok($live, '<=', 256, "at most 256 live statistics rows (got $live)");
|
cmp_ok($live, '>', 0, 'live statistics rows exist beyond the load');
|
cmp_ok(300 - $live, '>=', 44,
|
'eviction reset the counters of the relations beyond the capacity');
|
|
# ---------------------------------------------------------------------------
|
# 3. T2: a working set within the capacity must stay fully visible (every
|
# member keeps its slot with accumulated counters).
|
#
|
# The LRU granularity is per partition, so the working set is only stable
|
# when no partition holds more members than it has slots (4). The members
|
# are chosen with a Perl mirror of cfs_cache_hash_rel()
|
# (cfs_shmem_internal.h): reltablespace 0 means the database default
|
# tablespace, and the backend of a permanent relation is
|
# INVALID_PROC_NUMBER (-1 as uint32).
|
# ---------------------------------------------------------------------------
|
sub cfs_partition
|
{
|
my ($spc, $db, $relnum) = @_;
|
my $h = $spc;
|
$h = ($h * 31 + $db) & 0xFFFFFFFF;
|
$h = ($h * 31 + $relnum) & 0xFFFFFFFF;
|
$h = ($h * 31 + 0xFFFFFFFF) & 0xFFFFFFFF;
|
$h = ($h * 31) & 0xFFFFFFFF; # + forknum (MAIN_FORKNUM = 0)
|
$h = 1 if $h == 0;
|
return $h & 63;
|
}
|
|
my ($dboid, $dbtbs) = split qr/\|/, $node->safe_psql('postgres',
|
"SELECT oid, dattablespace FROM pg_database"
|
. " WHERE datname = current_database()");
|
|
my %filenode = map { split qr/\|/ } split qr/\n/, $node->safe_psql(
|
'postgres',
|
"SELECT relname, pg_relation_filenode(oid) FROM pg_class"
|
. " WHERE relname ~ '^cap_t[0-9]+\$'");
|
|
my %part_load = ();
|
my @workset = ();
|
for my $g (sort { $a <=> $b } map { s/^cap_t//r } keys %filenode)
|
{
|
my $part = cfs_partition($dbtbs, $dboid, $filenode{"cap_t$g"});
|
next if ($part_load{$part} // 0) >= 4;
|
$part_load{$part}++;
|
push @workset, "cap_t$g";
|
last if @workset == 200;
|
}
|
is(scalar @workset, 200, 'a 200-relation working set fits 4-per-partition');
|
|
# A seqscan of a buffer-evicted relation is served through the CFS read
|
# path, so each scan re-creates or re-touches the relation's statistics
|
# row. Every relation is touched in its own session (a single transaction
|
# pinning hundreds of metadata slots exhausts the small metadata
|
# partitions), and each relation is evicted exactly once: a second
|
# eviction would flush the seqscan's hint bits through the CFS write path
|
# and dirty the relation's metadata slot, which at 2 slots per partition
|
# can starve the next member of the partition. With at most 4 members
|
# per partition no member can evict another, so one pass is deterministic.
|
# Consecutive sessions may briefly overlap while their backends release
|
# the metadata-slot pins; a short retry absorbs that window.
|
$node->safe_psql('postgres', 'CREATE EXTENSION pg_buffercache');
|
for my $rel (@workset)
|
{
|
my $sql = "SELECT count(*) FROM pg_buffercache_evict_relation('$rel');"
|
. "SELECT count(*) FROM $rel;";
|
my ($ret, $stdout, $stderr);
|
|
for my $attempt (1 .. 20)
|
{
|
($ret, $stdout, $stderr) = $node->psql('postgres', $sql);
|
last if $ret == 0;
|
BAIL_OUT("touch of $rel failed: $stderr")
|
if $stderr !~ /out of relation metadata slots/;
|
select undef, undef, undef, 0.5;
|
}
|
is($ret, 0, "touched $rel through the CFS read path")
|
if $ret != 0;
|
}
|
|
my $stable = $node->safe_psql('postgres',
|
"SELECT count(*) FROM pg_stat_cfs WHERE read_count > 0 AND relname = ANY"
|
. " (ARRAY['" . join("','", @workset) . "']::name[])");
|
is($stable, '200', 'the 200-relation working set keeps live counters');
|
|
# Clear the dirty marks the republished sizes left behind, so the churn
|
# phase below starts from evictable metadata slots.
|
$node->safe_psql('postgres', 'CHECKPOINT');
|
|
# ---------------------------------------------------------------------------
|
# 4. T4: two concurrent bulk inserters extend one relation while the main
|
# session churns metadata slots (256 in total) under them.
|
# ---------------------------------------------------------------------------
|
$node->safe_psql('postgres',
|
"CREATE TABLE t_ext (id int, payload text) WITH (algorithm = 'lz4')");
|
|
my $w1 = $node->background_psql('postgres', timeout => 300);
|
$w1->query_until(qr/W1_READY/,
|
"\\echo W1_READY\n"
|
. "INSERT INTO t_ext SELECT g, repeat('w1', 100)"
|
. " FROM generate_series(1, 20000) g;\n"
|
. "\\echo W1_DONE\n");
|
my $w2 = $node->background_psql('postgres', timeout => 300);
|
$w2->query_until(qr/W2_READY/,
|
"\\echo W2_READY\n"
|
. "INSERT INTO t_ext SELECT 20000 + g, repeat('w2', 100)"
|
. " FROM generate_series(1, 20000) g;\n"
|
. "\\echo W2_DONE\n");
|
|
# Churn metadata slots while both inserters are extending: every CREATE
|
# publishes a fresh policy slot and evicts older ones of its partition.
|
for my $round (1 .. 8)
|
{
|
$node->safe_psql('postgres',
|
"CREATE TABLE churn_t$round (i int) WITH (algorithm = 'lz4');\n"
|
. "INSERT INTO churn_t$round SELECT g FROM generate_series(1, 500) g;\n"
|
. "SELECT count(*) FROM churn_t$round;\n");
|
}
|
|
$w1->query_until(qr/W1_DONE/, "SELECT 'W1_DONE';\n");
|
$w2->query_until(qr/W2_DONE/, "SELECT 'W2_DONE';\n");
|
$w1->quit;
|
$w2->quit;
|
|
is($node->safe_psql('postgres',
|
'SELECT count(*), count(DISTINCT id) FROM t_ext'),
|
'40000|40000', 'both concurrent inserters landed every row exactly once');
|
cmp_ok($node->safe_psql('postgres', "SELECT pg_relation_size('t_ext')"),
|
'>', 0, 'the extended relation has a size');
|
|
my $log = PostgreSQL::Test::Utils::slurp_file($node->logfile);
|
ok($log !~ /unexpected data beyond EOF/,
|
'no "unexpected data beyond EOF" during concurrent extension');
|
|
# ---------------------------------------------------------------------------
|
# 5. T3-lite: a restart re-creates the shared-memory layout; the data and
|
# the configuration survive.
|
# ---------------------------------------------------------------------------
|
$node->restart;
|
|
is($node->safe_psql('postgres', 'SELECT count(*) FROM t_ext'),
|
'40000', 'rows survive the restart with the new shmem layout');
|
is($node->safe_psql('postgres', 'SHOW cfs_stats_slots_per_partition'),
|
'4', 'cfs_stats_slots_per_partition survives the restart');
|
cmp_ok($node->safe_psql('postgres',
|
"SELECT write_count FROM pg_stat_cfs WHERE relname = 't_ext'"),
|
'>', 0, 'the statistics row of t_ext survived the restart');
|
|
$node->stop('fast');
|
|
done_testing();
|