Debian Codegraph @

src/bin/pg_combinebackup/t/011_ib_truncation.pl

raw
1 # Copyright (c) 2025-2026, PostgreSQL Global Development Group
2 #
3 # This test aims to validate two things: (1) that the calculated truncation
4 # block never exceeds the segment size and (2) that the correct limit block
5 # length is calculated for the VM fork.
6
7 use strict;
8 use warnings FATAL => 'all';
9 use PostgreSQL::Test::Cluster;
10 use PostgreSQL::Test::Utils;
11 use Test::More;
12
13 # Initialize primary node
14 my $primary = PostgreSQL::Test::Cluster->new('primary');
15 $primary->init(has_archiving => 1, allows_streaming => 1);
16 $primary->append_conf('postgresql.conf', 'summarize_wal = on');
17 $primary->start;
18
19 # Backup locations
20 my $backup_path = $primary->backup_dir;
21 my $full_backup = "$backup_path/full";
22
23 # To avoid using up lots of disk space in the CI/buildfarm environment, this
24 # test will only find the issue when run with a small RELSEG_SIZE. As of this
25 # writing, one of the CI runs is configured using --with-segsize-blocks=6, and
26 # we aim to have this test check for the issue only in that configuration.
27 my $target_blocks = 6;
28 my $block_size = $primary->safe_psql('postgres',
29 "SELECT current_setting('block_size')::int;");
30
31 # We'll have two blocks more than the target number of blocks (one will
32 # survive the subsequent truncation).
33 my $target_rows = int($target_blocks + 2);
34 my $rows_after_truncation = int($target_rows - 1);
35
36 # Create a test table. STORAGE PLAIN prevents compression and TOASTing of
37 # repetitive data, ensuring predictable row sizes.
38 $primary->safe_psql(
39 'postgres', q{
40 CREATE TABLE t (
41 id int,
42 data text STORAGE PLAIN
43 ) WITH (autovacuum_enabled = false);
44 });
45
46 # The tuple size should be enough to prevent two tuples from being on the same
47 # page. Since the template string has a length of 32 bytes, it's enough to
48 # repeat it (block_size / (2*32)) times.
49 $primary->safe_psql(
50 'postgres',
51 "INSERT INTO t
52 SELECT i,
53 repeat('0123456789ABCDEF0123456789ABCDEF', ($block_size / (2*32)))
54 FROM generate_series(1, $target_rows) i;"
55 );
56
57 # Make sure hint bits are set.
58 $primary->safe_psql('postgres', 'VACUUM t;');
59
60 # Verify that the relation is as large as was desired.
61 my $t_blocks = $primary->safe_psql('postgres',
62 "SELECT pg_relation_size('t') / current_setting('block_size')::int;");
63 cmp_ok($t_blocks, '>', $target_blocks, 'target block size exceeded');
64
65 # Take a full base backup
66 $primary->backup('full');
67
68 # Delete rows at the logical end of the table, creating removable pages.
69 $primary->safe_psql('postgres',
70 "DELETE FROM t WHERE id > ($rows_after_truncation);");
71
72 # VACUUM the table. TRUNCATE is enabled by default, and is just mentioned here
73 # for emphasis.
74 $primary->safe_psql('postgres', 'VACUUM (TRUNCATE) t;');
75
76 # Verify expected length after truncation.
77 $t_blocks = $primary->safe_psql('postgres',
78 "SELECT pg_relation_size('t') / current_setting('block_size')::int;");
79 is($t_blocks, $rows_after_truncation, 'post-truncation row count as expected');
80 cmp_ok($t_blocks, '>', $target_blocks,
81 'post-truncation block count as expected');
82
83 # Take an incremental backup based on the full backup manifest
84 $primary->backup('incr',
85 backup_options => [ '--incremental', "$full_backup/backup_manifest" ]);
86
87 # We used to have a bug where the wrong limit block was calculated for the
88 # VM fork, so verify that the WAL summary records the correct VM fork
89 # truncation limit. We can't just check whether the restored VM fork is
90 # the right size on disk, because it's so small that the incremental backup
91 # code will send the entire file.
92 my $relfilenode = $primary->safe_psql('postgres',
93 "SELECT pg_relation_filenode('t');");
94 my $vm_limits = $primary->safe_psql('postgres',
95 "SELECT string_agg(relblocknumber::text, ',')
96 FROM pg_available_wal_summaries() s,
97 pg_wal_summary_contents(s.tli, s.start_lsn, s.end_lsn) c
98 WHERE c.relfilenode = $relfilenode
99 AND c.relforknumber = 2
100 AND c.is_limit_block;");
101 is($vm_limits, '1',
102 'WAL summary has correct VM fork truncation limit');
103
104 # Combine full and incremental backups. Before the fix, this failed because
105 # the INCREMENTAL file header contained an incorrect truncation_block value.
106 my $restored = PostgreSQL::Test::Cluster->new('node2');
107 $restored->init_from_backup($primary, 'incr', combine_with_prior => ['full']);
108 $restored->start();
109
110 # Check that the restored table contains the correct number of rows
111 my $restored_count =
112 $restored->safe_psql('postgres', "SELECT count(*) FROM t;");
113 is($restored_count, $rows_after_truncation,
114 'Restored backup has correct row count');
115
116 $primary->stop;
117 $restored->stop;
118
119 done_testing();