postgres.git / summary / log / commit / refs
commit abfd5b9d95a05be64bf2b390afaf304f9e147051
Author: Melanie Plageman <melanieplageman@gmail.com>
Date: Thu Nov 19 22:19:01 2026 +0000
Forget invalid VM pages during truncation replay
Commit ed62d26caca made heap records that clear visibility map bits
register VM blocks, causing redo to read those blocks with RBM_NORMAL.
If such a record is replayed after the VM fork was already truncated on
disk, recovery records a missing-page reference. Replaying the
truncation only forgot missing pages from the main fork, so recovery
later PANICs with unresolved invalid pages.
Call XLogTruncateRelation() for the VM fork when replaying a VM
truncation.
Reported-by: Jacky Nguyen <nktpro@gmail.com>
Reviewed-by: Kirill Reshke <reshkekirill@gmail.com>
Reviewed-by: Shihao Zhong <zhong950419@gmail.com>
Reviewed-by: Rahul Yadav <rahul@rhyadav.dev>
Discussion: https://postgr.es/m/CAL4mQLAp562c1rCgg2Dqx6TBSdOk6vOvaLFjwFG9E6k4uwvJJw%40mail.gmail.com
Backpatch-through: 17
.../pg_visibility/t/001_concurrent_transaction.pl | 62 ++++++++++++++++++++--
src/backend/catalog/storage.c | 12 ++++-
2 files changed, 69 insertions(+), 5 deletions(-)
diff --git a/contrib/pg_visibility/t/001_concurrent_transaction.pl b/contrib/pg_visibility/t/001_concurrent_transaction.pl
index c31d041757d..ec353ac2f55 100644
--- a/contrib/pg_visibility/t/001_concurrent_transaction.pl
+++ b/contrib/pg_visibility/t/001_concurrent_transaction.pl
@@ -3,6 +3,9 @@
# Check that a concurrent transaction doesn't cause false negatives in
# pg_check_visible() function
+#
+# Also check that a standby can restart after replaying a VM clear followed by
+# a VM truncation.
use strict;
use warnings FATAL => 'all';
use PostgreSQL::Test::Cluster;
@@ -11,8 +14,12 @@ use Test::More;
my $node = PostgreSQL::Test::Cluster->new('main');
-
-$node->init;
+$node->init(allows_streaming => 1);
+$node->append_conf(
+ 'postgresql.conf', qq{
+autovacuum = off
+checkpoint_timeout = 1h
+});
$node->start;
# Setup another database
@@ -39,9 +46,58 @@ my $result = $node->safe_psql("postgres",
# There should be no false negatives
ok($result eq "", "pg_check_visible() detects no errors");
-# Shutdown
+# End the concurrent transaction
$bsession->query_safe("COMMIT;");
$bsession->quit;
+
+# Initialize a streaming standby for the recovery test.
+my $backup_name = 'my_backup';
+$node->backup($backup_name);
+my $standby = PostgreSQL::Test::Cluster->new('standby');
+$standby->init_from_backup($node, $backup_name, has_streaming => 1);
+$standby->start;
+
+# A WAL record clearing a VM bit registers the VM block. Make sure a standby
+# that crashes after the VM fork was truncated, and so replays the clear again
+# against a missing VM block, doesn't leave an unresolved invalid-page
+# reference behind: the VM truncation record must forget it.
+$node->safe_psql(
+ "postgres",
+ "CREATE TABLE vm_truncate_test AS SELECT generate_series(1, 500) i;\n"
+ . "VACUUM (FREEZE) vm_truncate_test;");
+
+# Move the standby's restart point past the VM set but before the VM clear.
+$node->safe_psql("postgres", "CHECKPOINT;");
+$node->wait_for_catchup($standby);
+$standby->safe_psql("postgres", "CHECKPOINT;");
+
+# Without full page images, the VM clear is replayed as a plain delta.
+$node->append_conf('postgresql.conf', "full_page_writes = off");
+$node->reload;
+$node->poll_query_until(
+ "postgres",
+ q{SELECT current_setting('full_page_writes') = 'off';});
+
+$node->safe_psql(
+ "postgres",
+ "UPDATE vm_truncate_test SET i = 0 WHERE i = 1;\n"
+ . "SELECT pg_truncate_visibility_map('vm_truncate_test');");
+$node->wait_for_catchup($standby);
+
+is($standby->safe_psql(
+ "postgres", "SELECT pg_relation_size('vm_truncate_test', 'vm');"),
+ '0',
+ "VM fork truncated on standby");
+
+# Crash the standby; redo restarts before the VM clear.
+$standby->stop('immediate');
+my $log_offset = -s $standby->logfile;
+ok($standby->start(fail_ok => 1), "standby restarts after VM truncation");
+ok(!$standby->log_contains(
+ qr/WAL contains references to invalid pages/, $log_offset),
+ "no invalid-page PANIC during recovery");
+
$node->stop;
+$standby->stop;
done_testing();
diff --git a/src/backend/catalog/storage.c b/src/backend/catalog/storage.c
index a3e554e372e..bef7da35247 100644
--- a/src/backend/catalog/storage.c
+++ b/src/backend/catalog/storage.c
@@ -1043,10 +1043,18 @@ smgr_redo(XLogReaderState *record)
need_fsm_vacuum = true;
}
}
- if ((xlrec->flags & SMGR_TRUNCATE_VM) != 0 &&
- smgrexists(reln, VISIBILITYMAP_FORKNUM))
+ if ((xlrec->flags & SMGR_TRUNCATE_VM) != 0)
{
+ /*
+ * Also tell xlogutils.c about it. Do this even if the VM fork is
+ * already short enough (e.g. replaying again after a crash), as
+ * earlier records may have logged invalid pages for it.
+ */
+ XLogTruncateRelation(xlrec->rlocator, VISIBILITYMAP_FORKNUM,
+ visibilitymap_truncation_length(xlrec->blkno));
+
blocks[nforks] = visibilitymap_prepare_truncate(rel, xlrec->blkno);
+
if (BlockNumberIsValid(blocks[nforks]))
{
forks[nforks] = VISIBILITYMAP_FORKNUM;
[parent: 1b51ce7c9ded]