# Copyright (c) 2026, Kirillov Vyacheslav by AI, QDB Development Group # # The shared block-map pool is a fixed shared-memory reservation sized by # cfs_map_memory. Exercise server start at the documented edges (pool off, # minimum, default, 1 GB and 4 GB) so CfsShmemSize() accounts for every # object CfsShmemInit() creates (segment hash, reservation hash, epochs). use strict; use warnings FATAL => 'all'; use PostgreSQL::Test::Cluster; use Test::More; sub available_kb { open(my $fh, '<', '/proc/meminfo') or return undef; while (my $line = <$fh>) { return $1 if $line =~ /^MemAvailable:\s+(\d+)\s+kB/; } return undef; } my @sizes = (0, 1, 128, 1024, 4096); for my $mb (@sizes) { if ($mb == 4096) { my $avail = available_kb(); if (defined $avail && $avail < 6 * 1024 * 1024) { pass("skip cfs_map_memory=4096: only $avail kB available"); next; } } my $node = PostgreSQL::Test::Cluster->new("cfs_map_$mb"); $node->init; $node->append_conf('postgresql.conf', "shared_buffers = 1MB\n"); $node->append_conf('postgresql.conf', "cfs_map_memory = $mb\n"); $node->start; my $setting = $node->safe_psql('postgres', "SELECT setting FROM pg_settings WHERE name = 'cfs_map_memory'"); is($setting, $mb, "cfs_map_memory starts at $mb MB"); my $segments = $node->safe_psql('postgres', 'SELECT pool_segments FROM pg_cfs_map_pool()'); if ($mb == 0) { is($segments, '0', 'pool is disabled at 0 MB'); } else { cmp_ok($segments, '>', 0, "pool reserves segments at $mb MB"); } # The size path never builds a map of its own anymore (QDB-1516): it # measures stat(.cfsmap) plus the journal's last RESIZE, so no throwaway # full map may open at any pool size. With the pool off the persistent # handle owns a full map either way; with the pool on the lazy handle # stays lazy and the measurement reads the sidecar pair directly. $node->safe_psql('postgres', 'CREATE EXTENSION test_cfs'); # Metadata slots (and their pins) exist for every pool size, so the # pin-owner registry that reaps pins of dead processes must exist even # with the pool off. my $pin_registry = $node->safe_psql('postgres', 'SELECT cfs_test_map_pin_registry()'); is($pin_registry, 't', "pin-owner registry exists at $mb MB"); $node->safe_psql('postgres', "CREATE TABLE size_probe (i int) WITH (algorithm = 'lz4')"); $node->safe_psql('postgres', 'INSERT INTO size_probe VALUES (1)'); my $opens = $node->safe_psql('postgres', "SELECT cfs_test_smgr_size_full_opens('size_probe')"); is( $opens, '0', "size path opened $opens full map(s) at $mb MB"); $node->stop; } done_testing();