Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6f81386

Browse files
committed
amcheck: Distinguish interrupted page deletion from corruption.
This prevents false-positive reports about "the first child of leftmosttarget page is not leftmost of its level", "block %u is not leftmost"and "left link/right link pair". They appeared if amcheck ran beforeVACUUM cleaned things, after a cluster exited recovery between thefirst-stage and second-stage WAL records of a deletion. Back-patch tov11 (all supported versions).Reviewed by Peter Geoghegan.Discussion:https://postgr.es/m/20231005025232.c7.nmisch@google.com
1 parent5f06918 commit6f81386

File tree

3 files changed

+163
-4
lines changed

3 files changed

+163
-4
lines changed

‎contrib/amcheck/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ PGFILEDESC = "amcheck - function for verifying relation integrity"
1212

1313
REGRESS = check check_btree check_heap
1414

15+
EXTRA_INSTALL = contrib/pg_walinspect
1516
TAP_TESTS = 1
1617

1718
ifdefUSE_PGXS

‎contrib/amcheck/t/005_pitr.pl

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Copyright (c) 2021-2023, PostgreSQL Global Development Group
2+
3+
# Test integrity of intermediate states by PITR to those states
4+
use strict;
5+
use warnings;
6+
use PostgreSQL::Test::Cluster;
7+
use PostgreSQL::Test::Utils;
8+
use Test::More;
9+
10+
# origin node: generate WAL records of interest.
11+
my$origin = PostgreSQL::Test::Cluster->new('origin');
12+
$origin->init(has_archiving=> 1,allows_streaming=> 1);
13+
$origin->append_conf('postgresql.conf','autovacuum = off');
14+
$origin->start;
15+
$origin->backup('my_backup');
16+
# Create a table with each of 6 PK values spanning 1/4 of a block. Delete the
17+
# first four, so one index leaf is eligible for deletion. Make a replication
18+
# slot just so pg_walinspect will always have access to later WAL.
19+
my$setup =<<EOSQL;
20+
BEGIN;
21+
CREATE EXTENSION amcheck;
22+
CREATE EXTENSION pg_walinspect;
23+
CREATE TABLE not_leftmost (c text);
24+
ALTER TABLE not_leftmost ALTER c SET STORAGE PLAIN;
25+
INSERT INTO not_leftmost
26+
SELECT repeat(n::text, database_block_size / 4)
27+
FROM generate_series(1,6) t(n), pg_control_init();
28+
ALTER TABLE not_leftmost ADD CONSTRAINT not_leftmost_pk PRIMARY KEY (c);
29+
DELETE FROM not_leftmost WHERE c ~ '^[1-4]';
30+
SELECT pg_create_physical_replication_slot('for_walinspect', true, false);
31+
COMMIT;
32+
EOSQL
33+
$origin->safe_psql('postgres',$setup);
34+
my$before_vacuum_lsn =
35+
$origin->safe_psql('postgres',"SELECT pg_current_wal_lsn()");
36+
# VACUUM to delete the aforementioned leaf page. Force an XLogFlush() by
37+
# dropping a permanent table. That way, the XLogReader infrastructure can
38+
# always see VACUUM's records, even under synchronous_commit=off. Finally,
39+
# find the LSN of that VACUUM's last UNLINK_PAGE record.
40+
my$vacuum =<<EOSQL;
41+
SET synchronous_commit = off;
42+
VACUUM (VERBOSE, INDEX_CLEANUP ON) not_leftmost;
43+
CREATE TABLE XLogFlush ();
44+
DROP TABLE XLogFlush;
45+
SELECT max(start_lsn)
46+
FROM pg_get_wal_records_info('$before_vacuum_lsn', pg_current_wal_flush_lsn())
47+
WHERE resource_manager = 'Btree' AND record_type = 'UNLINK_PAGE';
48+
EOSQL
49+
my$unlink_lsn =$origin->safe_psql('postgres',$vacuum);
50+
$origin->stop;
51+
die"did not find UNLINK_PAGE record"unless$unlink_lsn;
52+
53+
# replica node: amcheck at notable points in the WAL stream
54+
my$replica = PostgreSQL::Test::Cluster->new('replica');
55+
$replica->init_from_backup($origin,'my_backup',has_restoring=> 1);
56+
$replica->append_conf('postgresql.conf',
57+
"recovery_target_lsn = '$unlink_lsn'");
58+
$replica->append_conf('postgresql.conf','recovery_target_inclusive = off');
59+
$replica->append_conf('postgresql.conf','recovery_target_action = promote');
60+
$replica->start;
61+
$replica->poll_query_until('postgres',"SELECT pg_is_in_recovery() = 'f';")
62+
ordie"Timed out while waiting for PITR promotion";
63+
# recovery done; run amcheck
64+
my$debug ="SET client_min_messages = 'debug1'";
65+
my ($rc,$stderr);
66+
$rc =$replica->psql(
67+
'postgres',
68+
"$debug; SELECT bt_index_parent_check('not_leftmost_pk', true)",
69+
stderr=> \$stderr);
70+
printSTDERR$stderr,"\n";
71+
is($rc, 0,"bt_index_parent_check passes");
72+
like(
73+
$stderr,
74+
qr/interrupted page deletion detected/,
75+
"bt_index_parent_check: interrupted page deletion detected");
76+
$rc =$replica->psql(
77+
'postgres',
78+
"$debug; SELECT bt_index_check('not_leftmost_pk', true)",
79+
stderr=> \$stderr);
80+
printSTDERR$stderr,"\n";
81+
is($rc, 0,"bt_index_check passes");
82+
83+
done_testing();

‎contrib/amcheck/verify_nbtree.c

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ static void bt_check_every_level(Relation rel, Relation heaprel,
147147
boolrootdescend);
148148
staticBtreeLevelbt_check_level_from_leftmost(BtreeCheckState*state,
149149
BtreeLevellevel);
150+
staticboolbt_leftmost_ignoring_half_dead(BtreeCheckState*state,
151+
BlockNumberstart,
152+
BTPageOpaquestart_opaque);
150153
staticvoidbt_recheck_sibling_links(BtreeCheckState*state,
151154
BlockNumberbtpo_prev_from_target,
152155
BlockNumberleftcurrent);
@@ -775,7 +778,7 @@ bt_check_level_from_leftmost(BtreeCheckState *state, BtreeLevel level)
775778
*/
776779
if (state->readonly)
777780
{
778-
if (!P_LEFTMOST(opaque))
781+
if (!bt_leftmost_ignoring_half_dead(state,current,opaque))
779782
ereport(ERROR,
780783
(errcode(ERRCODE_INDEX_CORRUPTED),
781784
errmsg("block %u is not leftmost in index \"%s\"",
@@ -829,8 +832,16 @@ bt_check_level_from_leftmost(BtreeCheckState *state, BtreeLevel level)
829832
*/
830833
}
831834

832-
/* Sibling links should be in mutual agreement */
833-
if (opaque->btpo_prev!=leftcurrent)
835+
/*
836+
* Sibling links should be in mutual agreement. There arises
837+
* leftcurrent == P_NONE && btpo_prev != P_NONE when the left sibling
838+
* of the parent's low-key downlink is half-dead. (A half-dead page
839+
* has no downlink from its parent.) Under heavyweight locking, the
840+
* last bt_leftmost_ignoring_half_dead() validated this btpo_prev.
841+
* Without heavyweight locking, validation of the P_NONE case remains
842+
* unimplemented.
843+
*/
844+
if (opaque->btpo_prev!=leftcurrent&&leftcurrent!=P_NONE)
834845
bt_recheck_sibling_links(state,opaque->btpo_prev,leftcurrent);
835846

836847
/* Check level */
@@ -911,6 +922,66 @@ bt_check_level_from_leftmost(BtreeCheckState *state, BtreeLevel level)
911922
returnnextleveldown;
912923
}
913924

925+
/*
926+
* Like P_LEFTMOST(start_opaque), but accept an arbitrarily-long chain of
927+
* half-dead, sibling-linked pages to the left. If a half-dead page appears
928+
* under state->readonly, the database exited recovery between the first-stage
929+
* and second-stage WAL records of a deletion.
930+
*/
931+
staticbool
932+
bt_leftmost_ignoring_half_dead(BtreeCheckState*state,
933+
BlockNumberstart,
934+
BTPageOpaquestart_opaque)
935+
{
936+
BlockNumberreached=start_opaque->btpo_prev,
937+
reached_from=start;
938+
boolall_half_dead= true;
939+
940+
/*
941+
* To handle the !readonly case, we'd need to accept BTP_DELETED pages and
942+
* potentially observe nbtree/README "Page deletion and backwards scans".
943+
*/
944+
Assert(state->readonly);
945+
946+
while (reached!=P_NONE&&all_half_dead)
947+
{
948+
Pagepage=palloc_btree_page(state,reached);
949+
BTPageOpaquereached_opaque=BTPageGetOpaque(page);
950+
951+
CHECK_FOR_INTERRUPTS();
952+
953+
/*
954+
* Try to detect btpo_prev circular links. _bt_unlink_halfdead_page()
955+
* writes that side-links will continue to point to the siblings.
956+
* Check btpo_next for that property.
957+
*/
958+
all_half_dead=P_ISHALFDEAD(reached_opaque)&&
959+
reached!=start&&
960+
reached!=reached_from&&
961+
reached_opaque->btpo_next==reached_from;
962+
if (all_half_dead)
963+
{
964+
XLogRecPtrpagelsn=PageGetLSN(page);
965+
966+
/* pagelsn should point to an XLOG_BTREE_MARK_PAGE_HALFDEAD */
967+
ereport(DEBUG1,
968+
(errcode(ERRCODE_NO_DATA),
969+
errmsg_internal("harmless interrupted page deletion detected in index \"%s\"",
970+
RelationGetRelationName(state->rel)),
971+
errdetail_internal("Block=%u right block=%u page lsn=%X/%X.",
972+
reached,reached_from,
973+
LSN_FORMAT_ARGS(pagelsn))));
974+
975+
reached_from=reached;
976+
reached=reached_opaque->btpo_prev;
977+
}
978+
979+
pfree(page);
980+
}
981+
982+
returnall_half_dead;
983+
}
984+
914985
/*
915986
* Raise an error when target page's left link does not point back to the
916987
* previous target page, called leftcurrent here. The leftcurrent page's
@@ -951,6 +1022,9 @@ bt_recheck_sibling_links(BtreeCheckState *state,
9511022
BlockNumberbtpo_prev_from_target,
9521023
BlockNumberleftcurrent)
9531024
{
1025+
/* passing metapage to BTPageGetOpaque() would give irrelevant findings */
1026+
Assert(leftcurrent!=P_NONE);
1027+
9541028
if (!state->readonly)
9551029
{
9561030
Bufferlbuf;
@@ -1934,7 +2008,8 @@ bt_child_highkey_check(BtreeCheckState *state,
19342008
opaque=BTPageGetOpaque(page);
19352009

19362010
/* The first page we visit at the level should be leftmost */
1937-
if (first&& !BlockNumberIsValid(state->prevrightlink)&& !P_LEFTMOST(opaque))
2011+
if (first&& !BlockNumberIsValid(state->prevrightlink)&&
2012+
!bt_leftmost_ignoring_half_dead(state,blkno,opaque))
19382013
ereport(ERROR,
19392014
(errcode(ERRCODE_INDEX_CORRUPTED),
19402015
errmsg("the first child of leftmost target page is not leftmost of its level in index \"%s\"",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp