| 1 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 2 |
|
| 3 |
# Check that pg_check_visible() and pg_check_frozen() report correct TIDs for |
| 4 |
# corruption. |
| 5 |
use strict; |
| 6 |
use warnings FATAL => 'all'; |
| 7 |
use File::Copy; |
| 8 |
use PostgreSQL::Test::Cluster; |
| 9 |
use PostgreSQL::Test::Utils; |
| 10 |
use Test::More; |
| 11 |
|
| 12 |
my $node = PostgreSQL::Test::Cluster->new('main'); |
| 13 |
$node->init; |
| 14 |
# Anything holding a snapshot, including auto-analyze of pg_proc, could stop |
| 15 |
# VACUUM from updating the visibility map. |
| 16 |
$node->append_conf('postgresql.conf', 'autovacuum=off'); |
| 17 |
$node->start; |
| 18 |
|
| 19 |
my $blck_size = $node->safe_psql("postgres", "SHOW block_size;"); |
| 20 |
|
| 21 |
# Create a sample table with at least 10 pages and then run VACUUM. 10 is |
| 22 |
# selected manually as it is big enough to select 5 random tuples from the |
| 23 |
# relation. |
| 24 |
$node->safe_psql( |
| 25 |
'postgres', qq( |
| 26 |
CREATE EXTENSION pg_visibility; |
| 27 |
CREATE TABLE corruption_test |
| 28 |
WITH (autovacuum_enabled = false) AS |
| 29 |
SELECT |
| 30 |
i, |
| 31 |
repeat('a', 10) AS data |
| 32 |
FROM |
| 33 |
generate_series(1, $blck_size) i; |
| 34 |
VACUUM (FREEZE, DISABLE_PAGE_SKIPPING) corruption_test; |
| 35 |
)); |
| 36 |
|
| 37 |
# VACUUM is run, it is safe to get the number of pages. |
| 38 |
my $npages = $node->safe_psql( |
| 39 |
"postgres", |
| 40 |
"SELECT relpages FROM pg_class |
| 41 |
WHERE relname = 'corruption_test';" |
| 42 |
); |
| 43 |
ok($npages >= 10, 'table has at least 10 pages'); |
| 44 |
|
| 45 |
my $file = $node->safe_psql("postgres", |
| 46 |
"SELECT pg_relation_filepath('corruption_test');"); |
| 47 |
|
| 48 |
# Delete the first block to make sure that it will be skipped as it is |
| 49 |
# not visible nor frozen. |
| 50 |
$node->safe_psql( |
| 51 |
"postgres", |
| 52 |
"DELETE FROM corruption_test |
| 53 |
WHERE (ctid::text::point)[0] = 0;" |
| 54 |
); |
| 55 |
|
| 56 |
# Copy visibility map. |
| 57 |
$node->stop; |
| 58 |
my $vm_file = $node->data_dir . '/' . $file . '_vm'; |
| 59 |
copy("$vm_file", "${vm_file}_temp"); |
| 60 |
$node->start; |
| 61 |
|
| 62 |
# Select 5 random tuples that are starting from the second block of the |
| 63 |
# relation. The first block is skipped because it is deleted above. |
| 64 |
my $tuples = $node->safe_psql( |
| 65 |
"postgres", |
| 66 |
"SELECT ctid FROM ( |
| 67 |
SELECT ctid FROM corruption_test |
| 68 |
WHERE (ctid::text::point)[0] != 0 |
| 69 |
ORDER BY random() LIMIT 5) |
| 70 |
ORDER BY ctid ASC;" |
| 71 |
); |
| 72 |
|
| 73 |
# Do the changes below to use tuples in the query. |
| 74 |
# "\n" -> "," |
| 75 |
# "(" -> "'(" |
| 76 |
# ")" -> ")'" |
| 77 |
(my $tuples_query = $tuples) =~ s/\n/,/g; |
| 78 |
$tuples_query =~ s/\(/\'\(/g; |
| 79 |
$tuples_query =~ s/\)/\)\'/g; |
| 80 |
|
| 81 |
$node->safe_psql( |
| 82 |
"postgres", |
| 83 |
"DELETE FROM corruption_test |
| 84 |
WHERE ctid in ($tuples_query);" |
| 85 |
); |
| 86 |
|
| 87 |
# Overwrite visibility map with the old one. |
| 88 |
$node->stop; |
| 89 |
move("${vm_file}_temp", "$vm_file"); |
| 90 |
$node->start; |
| 91 |
|
| 92 |
my $result = $node->safe_psql( |
| 93 |
"postgres", |
| 94 |
"SELECT DISTINCT t_ctid |
| 95 |
FROM pg_check_visible('corruption_test') |
| 96 |
ORDER BY t_ctid ASC;" |
| 97 |
); |
| 98 |
is($result, $tuples, 'pg_check_visible must report tuples as corrupted'); |
| 99 |
|
| 100 |
$result = $node->safe_psql( |
| 101 |
"postgres", |
| 102 |
"SELECT DISTINCT t_ctid |
| 103 |
FROM pg_check_frozen('corruption_test') |
| 104 |
ORDER BY t_ctid ASC;" |
| 105 |
); |
| 106 |
is($result, $tuples, 'pg_check_frozen must report tuples as corrupted'); |
| 107 |
|
| 108 |
$node->stop; |
| 109 |
done_testing(); |