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

Commit3a205c9

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 parentf7cd7b2 commit3a205c9

File tree

2 files changed

+160
-4
lines changed

2 files changed

+160
-4
lines changed

‎contrib/amcheck/t/005_pitr.pl

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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_waldump will always have access to later WAL.
19+
my$setup =<<EOSQL;
20+
BEGIN;
21+
CREATE EXTENSION amcheck;
22+
CREATE TABLE not_leftmost (c text);
23+
ALTER TABLE not_leftmost ALTER c SET STORAGE PLAIN;
24+
INSERT INTO not_leftmost
25+
SELECT repeat(n::text, database_block_size / 4)
26+
FROM generate_series(1,6) t(n), pg_control_init();
27+
ALTER TABLE not_leftmost ADD CONSTRAINT not_leftmost_pk PRIMARY KEY (c);
28+
DELETE FROM not_leftmost WHERE c ~ '^[1-4]';
29+
SELECT pg_create_physical_replication_slot('for_waldump', true, false);
30+
COMMIT;
31+
EOSQL
32+
$origin->safe_psql('postgres',$setup);
33+
my$before_vacuum_walfile =
34+
$origin->safe_psql('postgres',"SELECT pg_walfile_name(pg_current_wal_lsn())");
35+
# VACUUM to delete the aforementioned leaf page. Force an XLogFlush() by
36+
# dropping a permanent table. That way, the XLogReader infrastructure can
37+
# always see VACUUM's records, even under synchronous_commit=off. Finally,
38+
# find the LSN of that VACUUM's last UNLINK_PAGE record.
39+
my$vacuum =<<EOSQL;
40+
SET synchronous_commit = off;
41+
VACUUM (VERBOSE, INDEX_CLEANUP ON) not_leftmost;
42+
CREATE TABLE XLogFlush ();
43+
DROP TABLE XLogFlush;
44+
SELECT pg_walfile_name(pg_current_wal_flush_lsn());
45+
EOSQL
46+
my$after_unlink_walfile =$origin->safe_psql('postgres',$vacuum);
47+
$origin->stop;
48+
my$unlink_lsn =do {
49+
my$stdout;
50+
run_log(['pg_waldump','-p',$origin->data_dir .'/pg_wal',
51+
$before_vacuum_walfile,$after_unlink_walfile],
52+
'>', \$stdout);
53+
$stdout =~m|^rmgr: Btree .*, lsn: ([/0-9A-F]+), .*, desc: UNLINK_PAGE left|m;
54+
$1;
55+
};
56+
die"did not find UNLINK_PAGE record"unless$unlink_lsn;
57+
58+
# replica node: amcheck at notable points in the WAL stream
59+
my$replica = PostgreSQL::Test::Cluster->new('replica');
60+
$replica->init_from_backup($origin,'my_backup',has_restoring=> 1);
61+
$replica->append_conf('postgresql.conf',
62+
"recovery_target_lsn = '$unlink_lsn'");
63+
$replica->append_conf('postgresql.conf','recovery_target_inclusive = off');
64+
$replica->append_conf('postgresql.conf','recovery_target_action = promote');
65+
$replica->start;
66+
$replica->poll_query_until('postgres',"SELECT pg_is_in_recovery() = 'f';")
67+
ordie"Timed out while waiting for PITR promotion";
68+
# recovery done; run amcheck
69+
my$debug ="SET client_min_messages = 'debug1'";
70+
my ($rc,$stderr);
71+
$rc =$replica->psql(
72+
'postgres',
73+
"$debug; SELECT bt_index_parent_check('not_leftmost_pk', true)",
74+
stderr=> \$stderr);
75+
printSTDERR$stderr,"\n";
76+
is($rc, 0,"bt_index_parent_check passes");
77+
like(
78+
$stderr,
79+
qr/interrupted page deletion detected/,
80+
"bt_index_parent_check: interrupted page deletion detected");
81+
$rc =$replica->psql(
82+
'postgres',
83+
"$debug; SELECT bt_index_check('not_leftmost_pk', true)",
84+
stderr=> \$stderr);
85+
printSTDERR$stderr,"\n";
86+
is($rc, 0,"bt_index_check passes");
87+
88+
done_testing();

‎contrib/amcheck/verify_nbtree.c

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ static void bt_check_every_level(Relation rel, Relation heaprel,
135135
boolrootdescend);
136136
staticBtreeLevelbt_check_level_from_leftmost(BtreeCheckState*state,
137137
BtreeLevellevel);
138+
staticboolbt_leftmost_ignoring_half_dead(BtreeCheckState*state,
139+
BlockNumberstart,
140+
BTPageOpaquestart_opaque);
138141
staticvoidbt_target_page_check(BtreeCheckState*state);
139142
staticBTScanInsertbt_right_page_check_scankey(BtreeCheckState*state);
140143
staticvoidbt_downlink_check(BtreeCheckState*state,BTScanInserttargetkey,
@@ -752,7 +755,7 @@ bt_check_level_from_leftmost(BtreeCheckState *state, BtreeLevel level)
752755
*/
753756
if (state->readonly)
754757
{
755-
if (!P_LEFTMOST(opaque))
758+
if (!bt_leftmost_ignoring_half_dead(state,current,opaque))
756759
ereport(ERROR,
757760
(errcode(ERRCODE_INDEX_CORRUPTED),
758761
errmsg("block %u is not leftmost in index \"%s\"",
@@ -807,10 +810,14 @@ bt_check_level_from_leftmost(BtreeCheckState *state, BtreeLevel level)
807810
}
808811

809812
/*
810-
* readonly mode can only ever land on live pages and half-dead pages,
811-
* so sibling pointers should always be in mutual agreement
813+
* Sibling links should be in mutual agreement. There arises
814+
* leftcurrent == P_NONE && btpo_prev != P_NONE when the left sibling
815+
* of the parent's low-key downlink is half-dead. (A half-dead page
816+
* has no downlink from its parent.) Under heavyweight locking, the
817+
* last bt_leftmost_ignoring_half_dead() validated this btpo_prev.
812818
*/
813-
if (state->readonly&&opaque->btpo_prev!=leftcurrent)
819+
if (state->readonly&&
820+
opaque->btpo_prev!=leftcurrent&&leftcurrent!=P_NONE)
814821
ereport(ERROR,
815822
(errcode(ERRCODE_INDEX_CORRUPTED),
816823
errmsg("left link/right link pair in index \"%s\" not in agreement",
@@ -860,6 +867,67 @@ bt_check_level_from_leftmost(BtreeCheckState *state, BtreeLevel level)
860867
returnnextleveldown;
861868
}
862869

870+
/*
871+
* Like P_LEFTMOST(start_opaque), but accept an arbitrarily-long chain of
872+
* half-dead, sibling-linked pages to the left. If a half-dead page appears
873+
* under state->readonly, the database exited recovery between the first-stage
874+
* and second-stage WAL records of a deletion.
875+
*/
876+
staticbool
877+
bt_leftmost_ignoring_half_dead(BtreeCheckState*state,
878+
BlockNumberstart,
879+
BTPageOpaquestart_opaque)
880+
{
881+
BlockNumberreached=start_opaque->btpo_prev,
882+
reached_from=start;
883+
boolall_half_dead= true;
884+
885+
/*
886+
* To handle the !readonly case, we'd need to accept BTP_DELETED pages and
887+
* potentially observe nbtree/README "Page deletion and backwards scans".
888+
*/
889+
Assert(state->readonly);
890+
891+
while (reached!=P_NONE&&all_half_dead)
892+
{
893+
Pagepage=palloc_btree_page(state,reached);
894+
BTPageOpaquereached_opaque= (BTPageOpaque)PageGetSpecialPointer(page);
895+
896+
CHECK_FOR_INTERRUPTS();
897+
898+
/*
899+
* Try to detect btpo_prev circular links. _bt_unlink_halfdead_page()
900+
* writes that side-links will continue to point to the siblings.
901+
* Check btpo_next for that property.
902+
*/
903+
all_half_dead=P_ISHALFDEAD(reached_opaque)&&
904+
reached!=start&&
905+
reached!=reached_from&&
906+
reached_opaque->btpo_next==reached_from;
907+
if (all_half_dead)
908+
{
909+
XLogRecPtrpagelsn=PageGetLSN(page);
910+
911+
/* pagelsn should point to an XLOG_BTREE_MARK_PAGE_HALFDEAD */
912+
ereport(DEBUG1,
913+
(errcode(ERRCODE_NO_DATA),
914+
errmsg_internal("harmless interrupted page deletion detected in index \"%s\"",
915+
RelationGetRelationName(state->rel)),
916+
errdetail_internal("Block=%u right block=%u page lsn=%X/%X.",
917+
reached,reached_from,
918+
(uint32) (pagelsn >>32),
919+
(uint32)pagelsn)));
920+
921+
reached_from=reached;
922+
reached=reached_opaque->btpo_prev;
923+
}
924+
925+
pfree(page);
926+
}
927+
928+
returnall_half_dead;
929+
}
930+
863931
/*
864932
* Function performs the following checks on target page, or pages ancillary to
865933
* target page:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp