Skip to content

Commit 7f563c0

Browse files
committed
Add amcheck verification of heap relations belonging to btree indexes.
Add a new, optional, capability to bt_index_check() and bt_index_parent_check(): check that each heap tuple that should have an index entry does in fact have one. The extra checking is performed at the end of the existing nbtree checks. This is implemented by using a Bloom filter data structure. The implementation performs set membership tests within a callback (the same type of callback that each index AM registers for CREATE INDEX). The Bloom filter is populated during the initial index verification scan. Reusing the CREATE INDEX infrastructure allows the new verification option to automatically benefit from the heap consistency checks that CREATE INDEX already performs. CREATE INDEX does thorough sanity checking of HOT chains, so the new check actually manages to detect problems in heap-only tuples. Author: Peter Geoghegan Reviewed-By: Pavan Deolasee, Andres Freund Discussion: https://postgr.es/m/CAH2-Wzm5VmG7cu1N-H=nnS57wZThoSDQU+F5dewx3o84M+jY=g@mail.gmail.com
1 parent 51bc271 commit 7f563c0

File tree

7 files changed

+458
-63
lines changed

7 files changed

+458
-63
lines changed

contrib/amcheck/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ MODULE_big = amcheck
44
OBJS = verify_nbtree.o $(WIN32RES)
55

66
EXTENSION = amcheck
7-
DATA = amcheck--1.0.sql
7+
DATA = amcheck--1.0--1.1.sql amcheck--1.0.sql
88
PGFILEDESC = "amcheck - function for verifying relation integrity"
99

1010
REGRESS = check check_btree

contrib/amcheck/amcheck--1.0--1.1.sql

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* contrib/amcheck/amcheck--1.0--1.1.sql */
2+
3+
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
4+
\echo Use "ALTER EXTENSION amcheck UPDATE TO '1.1'" to load this file. \quit
5+
6+
-- In order to avoid issues with dependencies when updating amcheck to 1.1,
7+
-- create new, overloaded versions of the 1.0 functions
8+
9+
--
10+
-- bt_index_check()
11+
--
12+
CREATE FUNCTION bt_index_check(index regclass,
13+
heapallindexed boolean)
14+
RETURNS VOID
15+
AS 'MODULE_PATHNAME', 'bt_index_check'
16+
LANGUAGE C STRICT PARALLEL RESTRICTED;
17+
18+
--
19+
-- bt_index_parent_check()
20+
--
21+
CREATE FUNCTION bt_index_parent_check(index regclass,
22+
heapallindexed boolean)
23+
RETURNS VOID
24+
AS 'MODULE_PATHNAME', 'bt_index_parent_check'
25+
LANGUAGE C STRICT PARALLEL RESTRICTED;
26+
27+
-- Don't want these to be available to public
28+
REVOKE ALL ON FUNCTION bt_index_check(regclass, boolean) FROM PUBLIC;
29+
REVOKE ALL ON FUNCTION bt_index_parent_check(regclass, boolean) FROM PUBLIC;

contrib/amcheck/amcheck.control

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# amcheck extension
22
comment = 'functions for verifying relation integrity'
3-
default_version = '1.0'
3+
default_version = '1.1'
44
module_pathname = '$libdir/amcheck'
55
relocatable = true

contrib/amcheck/expected/check_btree.out

+10-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ RESET ROLE;
1818
-- above explicit permission has to be granted for that.
1919
GRANT EXECUTE ON FUNCTION bt_index_check(regclass) TO bttest_role;
2020
GRANT EXECUTE ON FUNCTION bt_index_parent_check(regclass) TO bttest_role;
21+
GRANT EXECUTE ON FUNCTION bt_index_check(regclass, boolean) TO bttest_role;
22+
GRANT EXECUTE ON FUNCTION bt_index_parent_check(regclass, boolean) TO bttest_role;
2123
SET ROLE bttest_role;
2224
SELECT bt_index_check('bttest_a_idx');
2325
bt_index_check
@@ -56,8 +58,14 @@ SELECT bt_index_check('bttest_a_idx');
5658

5759
(1 row)
5860

59-
-- more expansive test
60-
SELECT bt_index_parent_check('bttest_b_idx');
61+
-- more expansive tests
62+
SELECT bt_index_check('bttest_a_idx', true);
63+
bt_index_check
64+
----------------
65+
66+
(1 row)
67+
68+
SELECT bt_index_parent_check('bttest_b_idx', true);
6169
bt_index_parent_check
6270
-----------------------
6371

contrib/amcheck/sql/check_btree.sql

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ RESET ROLE;
2121
-- above explicit permission has to be granted for that.
2222
GRANT EXECUTE ON FUNCTION bt_index_check(regclass) TO bttest_role;
2323
GRANT EXECUTE ON FUNCTION bt_index_parent_check(regclass) TO bttest_role;
24+
GRANT EXECUTE ON FUNCTION bt_index_check(regclass, boolean) TO bttest_role;
25+
GRANT EXECUTE ON FUNCTION bt_index_parent_check(regclass, boolean) TO bttest_role;
2426
SET ROLE bttest_role;
2527
SELECT bt_index_check('bttest_a_idx');
2628
SELECT bt_index_parent_check('bttest_a_idx');
@@ -42,8 +44,9 @@ ROLLBACK;
4244

4345
-- normal check outside of xact
4446
SELECT bt_index_check('bttest_a_idx');
45-
-- more expansive test
46-
SELECT bt_index_parent_check('bttest_b_idx');
47+
-- more expansive tests
48+
SELECT bt_index_check('bttest_a_idx', true);
49+
SELECT bt_index_parent_check('bttest_b_idx', true);
4750

4851
BEGIN;
4952
SELECT bt_index_check('bttest_a_idx');

0 commit comments

Comments
 (0)