| 1 |
# Copyright (c) 2026, PostgreSQL Global Development Group |
| 2 |
# |
| 3 |
# Test that heap operations clearing visibility map bits (INSERT, UPDATE, |
| 4 |
# DELETE, SELECT FOR UPDATE, COPY) correctly register visibility map buffers, |
| 5 |
# since incremental backups rely on the WAL summarizer, which only tracks |
| 6 |
# registered buffers. |
| 7 |
|
| 8 |
use strict; |
| 9 |
use warnings FATAL => 'all'; |
| 10 |
use PostgreSQL::Test::Cluster; |
| 11 |
use PostgreSQL::Test::Utils; |
| 12 |
use Test::More; |
| 13 |
|
| 14 |
my $tempdir = PostgreSQL::Test::Utils::tempdir_short(); |
| 15 |
my $mode = $ENV{PG_TEST_PG_COMBINEBACKUP_MODE} || '--copy'; |
| 16 |
|
| 17 |
# Set up primary with WAL summarization enabled. |
| 18 |
my $primary = PostgreSQL::Test::Cluster->new('primary'); |
| 19 |
$primary->init(allows_streaming => 1); |
| 20 |
$primary->append_conf('postgresql.conf', <<EOF); |
| 21 |
summarize_wal = on |
| 22 |
autovacuum = off |
| 23 |
EOF |
| 24 |
$primary->start; |
| 25 |
|
| 26 |
$primary->safe_psql('postgres', q{CREATE EXTENSION pg_visibility}); |
| 27 |
|
| 28 |
my @tests = ( |
| 29 |
{ |
| 30 |
label => 'INSERT', |
| 31 |
table => 'vm_insert_test', |
| 32 |
setup => q{CREATE TABLE vm_insert_test (id int); |
| 33 |
INSERT INTO vm_insert_test DEFAULT VALUES;}, |
| 34 |
modify => q{INSERT INTO vm_insert_test VALUES (1)}, |
| 35 |
visible_op => '<', |
| 36 |
frozen_op => '<', |
| 37 |
}, |
| 38 |
{ |
| 39 |
label => 'DELETE', |
| 40 |
table => 'vm_delete_test', |
| 41 |
setup => q{CREATE TABLE vm_delete_test (id int); |
| 42 |
INSERT INTO vm_delete_test VALUES (1), (2);}, |
| 43 |
modify => q{DELETE FROM vm_delete_test WHERE id = 1}, |
| 44 |
visible_op => '<', |
| 45 |
frozen_op => '<', |
| 46 |
}, |
| 47 |
{ |
| 48 |
label => 'UPDATE', |
| 49 |
table => 'vm_update_test', |
| 50 |
# Include both same-page and cross-page updates. val is stored PLAIN |
| 51 |
# so the large update stays inline. |
| 52 |
setup => q{CREATE TABLE vm_update_test (id INT, val TEXT); |
| 53 |
ALTER TABLE vm_update_test ALTER COLUMN val SET STORAGE PLAIN; |
| 54 |
INSERT INTO vm_update_test VALUES (1, 'same page'), (2, 'cross page'); |
| 55 |
INSERT INTO vm_update_test SELECT i, repeat('a', 200) |
| 56 |
FROM generate_series(3, 70) i;}, |
| 57 |
modify => q{UPDATE vm_update_test SET id = 0 WHERE id = 1; |
| 58 |
UPDATE vm_update_test SET val = repeat('b', 4000) WHERE id = 2;}, |
| 59 |
# Confirm the small update stays on the same heap page while the large |
| 60 |
# update relocates the tuple to a different heap page. |
| 61 |
ctid_checks => [ |
| 62 |
{ |
| 63 |
before => 'id = 1', |
| 64 |
after => 'id = 0', |
| 65 |
same => 1, |
| 66 |
desc => 'small update stays on the same heap page', |
| 67 |
}, |
| 68 |
{ |
| 69 |
before => 'id = 2', |
| 70 |
after => 'id = 2', |
| 71 |
same => 0, |
| 72 |
desc => 'large update moves the tuple to a different heap page', |
| 73 |
}, |
| 74 |
], |
| 75 |
visible_op => '<', |
| 76 |
frozen_op => '<', |
| 77 |
}, |
| 78 |
{ |
| 79 |
label => 'LOCK', |
| 80 |
table => 'vm_lock_test', |
| 81 |
setup => q{CREATE TABLE vm_lock_test (id int); |
| 82 |
INSERT INTO vm_lock_test VALUES (1), (2);}, |
| 83 |
modify => q{SELECT * FROM vm_lock_test WHERE id = 1 FOR UPDATE}, |
| 84 |
visible_op => '==', |
| 85 |
frozen_op => '<', |
| 86 |
}, |
| 87 |
{ |
| 88 |
label => 'COPY', |
| 89 |
table => 'vm_copy_test', |
| 90 |
setup => q{CREATE TABLE vm_copy_test (id int); |
| 91 |
INSERT INTO vm_copy_test DEFAULT VALUES;}, |
| 92 |
modify => q{COPY vm_copy_test FROM PROGRAM 'echo 42'}, |
| 93 |
visible_op => '<', |
| 94 |
frozen_op => '<', |
| 95 |
}, |
| 96 |
); |
| 97 |
|
| 98 |
sub get_vm_summary |
| 99 |
{ |
| 100 |
my ($node, $table) = @_; |
| 101 |
my $result = $node->safe_psql('postgres', |
| 102 |
"SELECT all_visible, all_frozen FROM pg_visibility_map_summary('$table')"); |
| 103 |
my @vals = split(/\|/, $result); |
| 104 |
return @vals; |
| 105 |
} |
| 106 |
|
| 107 |
# Return the heap block number of the (single) row matching $where. The ctid |
| 108 |
# is "(block,offset)"; casting it through point lets us pull out the block. |
| 109 |
sub heap_block |
| 110 |
{ |
| 111 |
my ($node, $table, $where) = @_; |
| 112 |
return $node->safe_psql('postgres', |
| 113 |
"SELECT (ctid::text::point)[0]::int FROM $table WHERE $where"); |
| 114 |
} |
| 115 |
|
| 116 |
# Confirm VACUUM (FREEZE) set VM bits before testing whether later heap |
| 117 |
# modifications clear those bits and are captured by incremental backup. We |
| 118 |
# could perhaps be more exact than > 0, but the coarseness attempts to avoid |
| 119 |
# test flakes. |
| 120 |
sub check_vacuumed_vm |
| 121 |
{ |
| 122 |
my ($node, $test) = @_; |
| 123 |
my ($all_visible, $all_frozen) = get_vm_summary($node, $test->{table}); |
| 124 |
|
| 125 |
cmp_ok($all_visible, '>', 0, |
| 126 |
"$test->{label} test: pages are all-visible after vacuum"); |
| 127 |
cmp_ok($all_frozen, '>', 0, |
| 128 |
"$test->{label} test: pages are all-frozen after vacuum"); |
| 129 |
|
| 130 |
return ($all_visible, $all_frozen); |
| 131 |
} |
| 132 |
|
| 133 |
# Check the VM bit counts after a heap modification against the post-vacuum |
| 134 |
# baseline. Most operations clear both bits; tuple locking clears all-frozen |
| 135 |
# without clearing all-visible. |
| 136 |
sub check_modified_vm |
| 137 |
{ |
| 138 |
my ($node, $test) = @_; |
| 139 |
my ($post_visible, $post_frozen) = get_vm_summary($node, $test->{table}); |
| 140 |
|
| 141 |
cmp_ok($post_visible, $test->{visible_op}, $test->{pre_visible}, |
| 142 |
"$test->{label} test: all-visible state after modification"); |
| 143 |
cmp_ok($post_frozen, $test->{frozen_op}, $test->{pre_frozen}, |
| 144 |
"$test->{label} test: all-frozen state after modification"); |
| 145 |
|
| 146 |
return ($post_visible, $post_frozen); |
| 147 |
} |
| 148 |
|
| 149 |
# Verify the combined backup restored VM state exactly as it exists on the |
| 150 |
# primary, and ask pg_visibility to check that visible tuples are consistent. |
| 151 |
sub validate_restored_vm |
| 152 |
{ |
| 153 |
my ($restored, $test) = @_; |
| 154 |
|
| 155 |
my ($primary_visible, $primary_frozen) = |
| 156 |
get_vm_summary($primary, $test->{table}); |
| 157 |
my ($restored_visible, $restored_frozen) = |
| 158 |
get_vm_summary($restored, $test->{table}); |
| 159 |
|
| 160 |
is($restored_visible, $primary_visible, |
| 161 |
"$test->{label} test: restored all_visible count matches primary"); |
| 162 |
is($restored_frozen, $primary_frozen, |
| 163 |
"$test->{label} test: restored all_frozen count matches primary"); |
| 164 |
|
| 165 |
my $corrupt_tids = $restored->safe_psql('postgres', |
| 166 |
"SELECT count(*) FROM pg_check_visible('$test->{table}')"); |
| 167 |
is($corrupt_tids, '0', |
| 168 |
"$test->{label} test: no VM corruption detected by pg_check_visible"); |
| 169 |
} |
| 170 |
|
| 171 |
# Create and populate the tables, then vacuum freeze them to set the VM bits |
| 172 |
foreach my $test (@tests) |
| 173 |
{ |
| 174 |
$primary->safe_psql('postgres', $test->{setup}); |
| 175 |
$primary->safe_psql('postgres', "VACUUM (FREEZE) $test->{table}"); |
| 176 |
($test->{pre_visible}, $test->{pre_frozen}) = |
| 177 |
check_vacuumed_vm($primary, $test); |
| 178 |
} |
| 179 |
|
| 180 |
# Take a full backup |
| 181 |
my $full_name = 'full'; |
| 182 |
my $full_path = $primary->backup_dir . "/$full_name"; |
| 183 |
$primary->command_ok( |
| 184 |
[ |
| 185 |
'pg_basebackup', '--no-sync', |
| 186 |
'--pgdata' => $full_path, |
| 187 |
'--checkpoint' => 'fast', |
| 188 |
], |
| 189 |
'full backup'); |
| 190 |
|
| 191 |
# Modify the tables and check that the VM bits are as expected for that test |
| 192 |
# after the specified modification. |
| 193 |
foreach my $test (@tests) |
| 194 |
{ |
| 195 |
# Record the heap block of any rows whose same-/cross-page movement we |
| 196 |
# want to verify, before the modification relocates them. |
| 197 |
foreach my $check (@{ $test->{ctid_checks} // [] }) |
| 198 |
{ |
| 199 |
$check->{before_blk} = |
| 200 |
heap_block($primary, $test->{table}, $check->{before}); |
| 201 |
} |
| 202 |
|
| 203 |
$primary->safe_psql('postgres', $test->{modify}); |
| 204 |
($test->{post_visible}, $test->{post_frozen}) = |
| 205 |
check_modified_vm($primary, $test); |
| 206 |
|
| 207 |
# Confirm each checked row did (or did not) move to a different heap page. |
| 208 |
foreach my $check (@{ $test->{ctid_checks} // [] }) |
| 209 |
{ |
| 210 |
my $after_blk = heap_block($primary, $test->{table}, $check->{after}); |
| 211 |
if ($check->{same}) |
| 212 |
{ |
| 213 |
is($after_blk, $check->{before_blk}, |
| 214 |
"$test->{label} test: $check->{desc}"); |
| 215 |
} |
| 216 |
else |
| 217 |
{ |
| 218 |
isnt($after_blk, $check->{before_blk}, |
| 219 |
"$test->{label} test: $check->{desc}"); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
# Take an incremental backup. This will have the changes made in the |
| 225 |
# modification step. |
| 226 |
my $incr_name = 'incr'; |
| 227 |
my $incr_path = $primary->backup_dir . "/$incr_name"; |
| 228 |
$primary->command_ok( |
| 229 |
[ |
| 230 |
'pg_basebackup', '--no-sync', |
| 231 |
'--pgdata' => $incr_path, |
| 232 |
'--checkpoint' => 'fast', |
| 233 |
'--incremental' => $full_path . '/backup_manifest', |
| 234 |
], |
| 235 |
'incremental backup'); |
| 236 |
|
| 237 |
# Start a server from a combined backup composed of the incremental and full |
| 238 |
# backup. |
| 239 |
my $restored = PostgreSQL::Test::Cluster->new('restored'); |
| 240 |
$restored->init_from_backup($primary, $incr_name, |
| 241 |
combine_with_prior => [$full_name], |
| 242 |
combine_mode => $mode); |
| 243 |
$restored->append_conf('postgresql.conf', <<EOF); |
| 244 |
autovacuum = off |
| 245 |
EOF |
| 246 |
$restored->start; |
| 247 |
$restored->safe_psql('postgres', q{CREATE EXTENSION IF NOT EXISTS pg_visibility}); |
| 248 |
|
| 249 |
# Confirm that the restored server's visibility map matches the original server |
| 250 |
foreach my $test (@tests) |
| 251 |
{ |
| 252 |
validate_restored_vm($restored, $test); |
| 253 |
} |
| 254 |
|
| 255 |
$restored->stop; |
| 256 |
$primary->stop; |
| 257 |
|
| 258 |
done_testing(); |