# Copyright (c) 2026, Kirillov Vyacheslav by AI, QDB Development Group # # src/test/recovery/t/509_cfs_dump_restore.pl # # Logical backup and restore of a database with CFS-compressed relations. # # Vanilla pg_dump dumps a TOAST relation's reloptions (the frozen copy of # the owning table's CFS policy triple) back into the table's WITH clause # as toast.* options, so the server must accept that output on restore. # This test dumps a database containing a CFS table with toastable columns, # a second CFS table with a different algorithm, a CFS materialized view, # a plain (non-CFS) table with a TOAST column, and an index on the CFS # table; restores it into a fresh database; and verifies that the row # contents, the effective CFS policy of every relation, the TOAST mirror # and structural integrity all match. use strict; use warnings FATAL => 'all'; use PostgreSQL::Test::Cluster; use PostgreSQL::Test::Utils; use Test::More; my $node = PostgreSQL::Test::Cluster->new('cfs_dump_restore'); $node->init; $node->append_conf('postgresql.conf', "checkpoint_timeout = 30s\n"); $node->start; # The scenario needs at least one CFS codec; use zstd when available and # fall back to lz4. my $configure = $node->safe_psql('postgres', "SELECT setting FROM pg_config WHERE name = 'CONFIGURE'"); my $has_zstd = ($configure =~ /--with-zstd/) ? 1 : 0; my $has_lz4 = ($configure =~ /--with-lz4/) ? 1 : 0; if (!$has_zstd && !$has_lz4) { plan skip_all => 'no CFS compression codec built, skipping dump/restore test'; } my $algo = $has_zstd ? 'zstd' : 'lz4'; my $other_algo = $has_lz4 && $algo ne 'lz4' ? 'lz4' : undef; # Return the md5 of the concatenation of all payload values, ordered by id. sub table_checksum { my ($tbl) = @_; return $node->safe_psql('postgres', "SELECT md5(string_agg(payload, '' ORDER BY id)) FROM $tbl"); } # ---------------------------------------------------------------------- # Populate the source database. # ---------------------------------------------------------------------- # A CFS table with a toastable column and long, compressible strings: the # payload is large enough to be toasted, and repeated so compression kicks # in for real. $node->safe_psql('postgres', "CREATE TABLE cfs_main (id int, payload text) " . "WITH (algorithm = '$algo', level = 3, min_gain_bytes = 512)"); $node->safe_psql('postgres', "INSERT INTO cfs_main " . "SELECT g, repeat(md5(g::text), 200) " . "FROM generate_series(1, 200) g"); # A second CFS table with a different algorithm, when available. my $second_algo = defined($other_algo) ? $other_algo : undef; if (defined($second_algo)) { $node->safe_psql('postgres', "CREATE TABLE cfs_other (id int, payload text) " . "WITH (algorithm = '$second_algo', level = 3, min_gain_bytes = 512)" ); $node->safe_psql('postgres', "INSERT INTO cfs_other " . "SELECT g, repeat(md5('o' || g), 200) " . "FROM generate_series(1, 100) g"); } # An index on the CFS table. $node->safe_psql('postgres', "CREATE INDEX cfs_main_idx ON cfs_main (id)"); # A CFS materialized view. $node->safe_psql('postgres', "CREATE MATERIALIZED VIEW cfs_mv WITH (algorithm = '$algo') " . "AS SELECT g AS id, repeat('mv-' || g, 100) AS payload " . "FROM generate_series(1, 50) g"); # A plain table with a toastable column, as a control that a non-CFS # relation still round-trips unchanged. $node->safe_psql('postgres', "CREATE TABLE plain_t (id int, payload text)"); $node->safe_psql('postgres', "INSERT INTO plain_t " . "SELECT g, repeat('plain-' || g, 200) " . "FROM generate_series(1, 50) g"); $node->safe_psql('postgres', "CHECKPOINT"); # Record the source policy of each CFS relation. my %src_policy; for my $rel ('cfs_main', 'cfs_other', 'cfs_mv', 'cfs_main_idx') { next if $rel eq 'cfs_other' && !defined($second_algo); $src_policy{$rel} = $node->safe_psql('postgres', "SELECT is_cfs || '|' || coalesce(algorithm, '') || '|' || " . "coalesce(level::text, '') || '|' || " . "coalesce(min_gain_bytes::text, '') " . "FROM pg_cfs_relation_info('$rel'::regclass)"); } # Record the toast mirror of the CFS table and of the plain table. my $src_toast_mirror = $node->safe_psql('postgres', "SELECT string_agg(option_name || '=' || option_value, ',' ORDER BY option_name) " . "FROM pg_options_to_table((SELECT reloptions FROM pg_class " . "WHERE oid = (SELECT reltoastrelid FROM pg_class " . "WHERE oid = 'cfs_main'::regclass)))"); # Record source checksums and row counts. my %src_checksum; my %src_count; for my $rel ('cfs_main', 'cfs_other', 'cfs_mv', 'plain_t') { next if $rel eq 'cfs_other' && !defined($second_algo); $src_count{$rel} = $node->safe_psql('postgres', "SELECT count(*) FROM $rel"); $src_checksum{$rel} = table_checksum($rel); } # ---------------------------------------------------------------------- # Dump and restore into a fresh database. # ---------------------------------------------------------------------- my $dump_file = "$PostgreSQL::Test::Utils::tmp_check/cfs_dump.dump"; $node->command_ok( [ 'pg_dump', '--format=custom', '--no-sync', '--file' => $dump_file, '--dbname' => $node->connstr('postgres'), ], 'pg_dump -Fc of the CFS database'); $node->safe_psql('postgres', "CREATE DATABASE restore_db"); $node->command_ok( [ 'pg_restore', '--exit-on-error', '--dbname' => $node->connstr('restore_db'), $dump_file, ], 'pg_restore into restore_db'); # ---------------------------------------------------------------------- # Verify the restored database. # ---------------------------------------------------------------------- # Row counts and contents match. for my $rel ('cfs_main', 'cfs_other', 'cfs_mv', 'plain_t') { next if $rel eq 'cfs_other' && !defined($second_algo); my $count = $node->safe_psql('restore_db', "SELECT count(*) FROM $rel"); is($count, $src_count{$rel}, "$rel row count matches after restore"); my $checksum = $node->safe_psql('restore_db', "SELECT md5(string_agg(payload, '' ORDER BY id)) FROM $rel"); is($checksum, $src_checksum{$rel}, "$rel contents match after restore"); } # Effective policy matches for every CFS relation. for my $rel ('cfs_main', 'cfs_other', 'cfs_mv', 'cfs_main_idx') { next if $rel eq 'cfs_other' && !defined($second_algo); my $policy = $node->safe_psql('restore_db', "SELECT is_cfs || '|' || coalesce(algorithm, '') || '|' || " . "coalesce(level::text, '') || '|' || " . "coalesce(min_gain_bytes::text, '') " . "FROM pg_cfs_relation_info('$rel'::regclass)"); is($policy, $src_policy{$rel}, "$rel policy matches after restore"); } # The TOAST mirror is restored verbatim. my $restored_toast_mirror = $node->safe_psql('restore_db', "SELECT string_agg(option_name || '=' || option_value, ',' ORDER BY option_name) " . "FROM pg_options_to_table((SELECT reloptions FROM pg_class " . "WHERE oid = (SELECT reltoastrelid FROM pg_class " . "WHERE oid = 'cfs_main'::regclass)))"); is($restored_toast_mirror, $src_toast_mirror, 'TOAST reloptions mirror restored verbatim'); # Structural integrity holds. my $check = $node->safe_psql('restore_db', "SELECT ok, errors FROM pg_cfs_check('cfs_main'::regclass, true)"); is($check, 't|0', 'pg_cfs_check passes on the restored CFS table'); # The plain table is still non-CFS. my $plain_is_cfs = $node->safe_psql('restore_db', "SELECT is_cfs FROM pg_cfs_relation_info('plain_t'::regclass)"); is($plain_is_cfs, 'f', 'plain table stays non-CFS after restore'); done_testing();