On Wed, 11 Nov 2026 at 08:33, shihao zhong <[email protected]> wrote:
>
> Hi Greg,
>
> > One point I'd like a reviewer to look at in 0001:
> >
> > redo now reads a block the record does not register.
>
> Could that be a torn page?
>
> > The page is not modified, so it needs no full-page image and no LSN
> > update; the old code registered it with REGBUF_NO_IMAGE, so redo already
> > worked from the on-disk copy.
>
> I think if we have a torn bucket page and an XLOG_HASH_DELETE for one
> of its overflow pages before the FPI, we may hit an issue?
>
> We do not register an FPI for it in hashbucketcleanup().
>
>     if (!xlrec.is_primary_bucket_page)
>     {
>         uint8 flags = REGBUF_STANDARD | REGBUF_NO_IMAGE |
>             REGBUF_NO_CHANGE;
>
>         XLogRegisterBuffer(0, bucket_buf, flags);
>     }
>
>     XLogRegisterBuffer(1, buf, REGBUF_STANDARD);
>
> Then hash_xlog_delete() reads it from disk in the else branch, and
> that read fails.
>
>     if (xldata->is_primary_bucket_page)
>         action = XLogReadBufferForRedoExtended(record, 1, RBM_NORMAL,
>                                                true, &deletebuf);
>     else
>     {
>         (void) XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL,
>                                              true, &bucketbuf);
>
>         action = XLogReadBufferForRedo(record, 1, &deletebuf);
>     }
>
> I don't think this is caused by your patch. This looks like an
> existing bug in master.

Thanks for the report. I was able to reproduce this on HEAD.
The attached test
(src/test/recovery/t/068a_hash_torn_primary_page_delete_first.pl) does
the following, with data checksums enabled: a) Builds a hash index
with all keys in one bucket, consisting of a primary page P and an
overflow page, and takes a checkpoint. b) Runs VACUUM to remove dead
entries from the overflow page only. This writes an XLOG_HASH_DELETE
record for the overflow page without modifying P.  c) Runs a second
VACUUM to remove dead entries from P. This is the first modification
to P since the checkpoint, so the corresponding WAL record includes a
full-page image (FPI) of P.  d) Crashes the server, overwrites the
second half of P in the data file with random bytes to simulate a torn
write, and restarts the server.

Crash recovery then fails while replaying the first record (the DELETE
for the overflow page), before reaching the record containing the FPI
of P:
FATAL:  invalid page in block 7 of relation "base/5/16387"
CONTEXT:  WAL redo at 0/017745E0 for Hash/DELETE: ... is_primary F;
blkref #1: ... blk 10 FPW

During redo, we read P only to acquire the cleanup lock, using
RBM_NORMAL. The read therefore fails checksum verification. The test
fails in the same way without the patch, where P is accessed through
block 0, and with the patch, where it is accessed through
bucket_blkno.
Since we only need P to acquire the cleanup lock and don't actually
need its contents, I think we should read it using RBM_ZERO_ON_ERROR
in hash_xlog_lock_bucket(). This would zero the buffer instead of
raising an error when a torn page is encountered, allowing the FPI
later in the WAL to restore the correct contents.

Do you think RBM_ZERO_ON_ERROR is the right approach here, or is there
a better solution to fix this?

Regards,
Vignesh
# Copyright (c) 2026, PostgreSQL Global Development Group

# Test crash recovery when the primary bucket page of a hash bucket is torn,
# and the full-page image that repairs it comes after the record that reads it.
#

use strict;
use warnings FATAL => 'all';

use IPC::Run;
use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;

my $node = PostgreSQL::Test::Cluster->new('primary');
$node->init;
$node->append_conf(
	'postgresql.conf', qq{
autovacuum = off
fsync = off
full_page_writes = on
checkpoint_timeout = 1h
});
$node->start;

is($node->safe_psql('postgres', 'SHOW data_checksums'),
	'on', 'premise: data checksums are on');

# One bucket receives every key: a primary page holding about 407 entries and
# an overflow page with the rest.
$node->safe_psql(
	'postgres', q{
CREATE TABLE t (id int, k int) WITH (autovacuum_enabled = false);
CREATE INDEX t_i ON t USING hash (k);
INSERT INTO t SELECT i, 1 FROM generate_series(1, 600) i;
});
$node->safe_psql('postgres', 'CHECKPOINT');
my $redo = $node->safe_psql('postgres',
	'SELECT redo_lsn FROM pg_control_checkpoint()');

# The entries of ids 451-600 are on the overflow page, those of ids 1-50 on the
# primary page.  The DELETE for the overflow page comes first; the VACUUM of
# the primary page then changes it for the first time since the checkpoint, so
# its record carries the full-page image of the page.
$node->safe_psql('postgres', 'DELETE FROM t WHERE id > 450');
$node->safe_psql('postgres', 'VACUUM t');
$node->safe_psql('postgres', 'DELETE FROM t WHERE id <= 50');
$node->safe_psql('postgres', 'VACUUM t');

my $relpath = $node->safe_psql('postgres',
	q{SELECT pg_relation_filepath('t_i')});
my $bs = $node->safe_psql('postgres', 'SHOW block_size');
$node->safe_psql('postgres', 'SELECT pg_switch_wal()');

# Read the Hash records after the checkpoint.
my ($stdout, $stderr);
IPC::Run::run(
	[
		'pg_waldump',
		'--path' => $node->data_dir . '/pg_wal',
		'--start' => $redo,
		'--rmgr' => 'Hash',
	],
	'>' => \$stdout,
	'2>' => \$stderr);
my @records = grep { /^rmgr: Hash/ } split(/^(?=rmgr: )/m, $stdout);

# The primary page: named by the first DELETE for an overflow page.
my ($idx_delete) =
  grep { $records[$_] =~ /desc: DELETE .*is_primary F/ } 0 .. $#records;
ok(defined $idx_delete,
	'premise: VACUUM wrote a DELETE for an overflow page')
  or diag $stdout;
my $pblk;
if (defined $idx_delete)
{
	($pblk) = $records[$idx_delete] =~ /bucket_blkno (\d+)/;
	($pblk) = $records[$idx_delete] =~ /blkref #0: rel \S+ blk (\d+)/
	  unless defined $pblk;
}
ok(defined $pblk, 'premise: found the primary bucket page')
  or diag $stdout;

# Where the first full-page image of the primary page is, relative to the
# DELETE for the overflow page.
my ($idx_fpi) =
  defined $pblk
  ? grep { $records[$_] =~ /blkref #\d+: rel \S+ blk $pblk FPW/ }
  0 .. $#records
  : ();
ok(defined $idx_fpi,
	'premise: the primary page got a full-page image after the checkpoint')
  or diag $stdout;
if (defined $idx_delete && defined $idx_fpi)
{
	cmp_ok($idx_delete, '<', $idx_fpi,
		'premise: the DELETE comes before the image of the primary page');
}

$node->stop('immediate');

# Simulate a torn write of the primary page: keep the first half of its block
# and overwrite the second half.
if (defined $pblk)
{
	my $path = $node->data_dir . '/' . $relpath;
	open(my $fh, '+<:raw', $path) or die "could not open $path: $!";
	sysseek($fh, $pblk * $bs + $bs / 2, 0) or die "seek: $!";
	my $junk = join('', map { chr(int(rand(256))) } 1 .. $bs / 2);
	syswrite($fh, $junk) == $bs / 2 or die "write: $!";
	close($fh);
}

my $started = $node->start(fail_ok => 1);
ok($started, 'crash recovery succeeds');
if ($started)
{
	is( $node->safe_psql(
			'postgres',
			q{SET enable_seqscan = off; SET enable_bitmapscan = off;
			  SELECT count(*) FROM t WHERE k = 1}),
		'400',
		'the hash index gives the right result after recovery');
	$node->stop;
}
else
{
	my $log = slurp_file($node->logfile);
	diag("the server did not start; the log has:\n"
		  . join('', grep { /invalid page|FATAL|WAL redo/ } split(/^/m, $log)));
}

done_testing();

Reply via email to