mirror of
https://github.com/postgres/postgres.git
synced 2025-07-28 23:42:10 +03:00
Add amcheck extension to contrib.
This is the beginning of a collection of SQL-callable functions to verify the integrity of data files. For now it only contains code to verify B-Tree indexes. This adds two SQL-callable functions, validating B-Tree consistency to a varying degree. Check the, extensive, docs for details. The goal is to later extend the coverage of the module to further access methods, possibly including the heap. Once checks for additional access methods exist, we'll likely add some "dispatch" functions that cover multiple access methods. Author: Peter Geoghegan, editorialized by Andres Freund Reviewed-By: Andres Freund, Tomas Vondra, Thomas Munro, Anastasia Lubennikova, Robert Haas, Amit Langote Discussion: CAM3SWZQzLMhMwmBqjzK+pRKXrNUZ4w90wYMUWfkeV8mZ3Debvw@mail.gmail.com
This commit is contained in:
21
contrib/amcheck/Makefile
Normal file
21
contrib/amcheck/Makefile
Normal file
@ -0,0 +1,21 @@
|
||||
# contrib/amcheck/Makefile
|
||||
|
||||
MODULE_big = amcheck
|
||||
OBJS = verify_nbtree.o $(WIN32RES)
|
||||
|
||||
EXTENSION = amcheck
|
||||
DATA = amcheck--1.0.sql
|
||||
PGFILEDESC = "amcheck - function for verifying relation integrity"
|
||||
|
||||
REGRESS = check check_btree
|
||||
|
||||
ifdef USE_PGXS
|
||||
PG_CONFIG = pg_config
|
||||
PGXS := $(shell $(PG_CONFIG) --pgxs)
|
||||
include $(PGXS)
|
||||
else
|
||||
subdir = contrib/amcheck
|
||||
top_builddir = ../..
|
||||
include $(top_builddir)/src/Makefile.global
|
||||
include $(top_srcdir)/contrib/contrib-global.mk
|
||||
endif
|
24
contrib/amcheck/amcheck--1.0.sql
Normal file
24
contrib/amcheck/amcheck--1.0.sql
Normal file
@ -0,0 +1,24 @@
|
||||
/* contrib/amcheck/amcheck--1.0.sql */
|
||||
|
||||
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
|
||||
\echo Use "CREATE EXTENSION amcheck" to load this file. \quit
|
||||
|
||||
--
|
||||
-- bt_index_check()
|
||||
--
|
||||
CREATE FUNCTION bt_index_check(index regclass)
|
||||
RETURNS VOID
|
||||
AS 'MODULE_PATHNAME', 'bt_index_check'
|
||||
LANGUAGE C STRICT PARALLEL RESTRICTED;
|
||||
|
||||
--
|
||||
-- bt_index_parent_check()
|
||||
--
|
||||
CREATE FUNCTION bt_index_parent_check(index regclass)
|
||||
RETURNS VOID
|
||||
AS 'MODULE_PATHNAME', 'bt_index_parent_check'
|
||||
LANGUAGE C STRICT PARALLEL RESTRICTED;
|
||||
|
||||
-- Don't want these to be available to public
|
||||
REVOKE ALL ON FUNCTION bt_index_check(regclass) FROM PUBLIC;
|
||||
REVOKE ALL ON FUNCTION bt_index_parent_check(regclass) FROM PUBLIC;
|
5
contrib/amcheck/amcheck.control
Normal file
5
contrib/amcheck/amcheck.control
Normal file
@ -0,0 +1,5 @@
|
||||
# amcheck extension
|
||||
comment = 'functions for verifying relation integrity'
|
||||
default_version = '1.0'
|
||||
module_pathname = '$libdir/amcheck'
|
||||
relocatable = true
|
1
contrib/amcheck/expected/check.out
Normal file
1
contrib/amcheck/expected/check.out
Normal file
@ -0,0 +1 @@
|
||||
CREATE EXTENSION amcheck;
|
90
contrib/amcheck/expected/check_btree.out
Normal file
90
contrib/amcheck/expected/check_btree.out
Normal file
@ -0,0 +1,90 @@
|
||||
-- minimal test, basically just verifying that amcheck
|
||||
CREATE TABLE bttest_a(id int8);
|
||||
CREATE TABLE bttest_b(id int8);
|
||||
INSERT INTO bttest_a SELECT * FROM generate_series(1, 100000);
|
||||
INSERT INTO bttest_b SELECT * FROM generate_series(100000, 1, -1);
|
||||
CREATE INDEX bttest_a_idx ON bttest_a USING btree (id);
|
||||
CREATE INDEX bttest_b_idx ON bttest_b USING btree (id);
|
||||
CREATE ROLE bttest_role;
|
||||
-- verify permissions are checked (error due to function not callable)
|
||||
SET ROLE bttest_role;
|
||||
SELECT bt_index_check('bttest_a_idx'::regclass);
|
||||
ERROR: permission denied for function bt_index_check
|
||||
SELECT bt_index_parent_check('bttest_a_idx'::regclass);
|
||||
ERROR: permission denied for function bt_index_parent_check
|
||||
RESET ROLE;
|
||||
-- we, intentionally, don't check relation permissions - it's useful
|
||||
-- to run this cluster-wide with a restricted account, and as tested
|
||||
-- above explicit permission has to be granted for that.
|
||||
GRANT EXECUTE ON FUNCTION bt_index_check(regclass) TO bttest_role;
|
||||
GRANT EXECUTE ON FUNCTION bt_index_parent_check(regclass) TO bttest_role;
|
||||
SET ROLE bttest_role;
|
||||
SELECT bt_index_check('bttest_a_idx');
|
||||
bt_index_check
|
||||
----------------
|
||||
|
||||
(1 row)
|
||||
|
||||
SELECT bt_index_parent_check('bttest_a_idx');
|
||||
bt_index_parent_check
|
||||
-----------------------
|
||||
|
||||
(1 row)
|
||||
|
||||
RESET ROLE;
|
||||
-- verify plain tables are rejected (error)
|
||||
SELECT bt_index_check('bttest_a');
|
||||
ERROR: "bttest_a" is not an index
|
||||
SELECT bt_index_parent_check('bttest_a');
|
||||
ERROR: "bttest_a" is not an index
|
||||
-- verify non-existing indexes are rejected (error)
|
||||
SELECT bt_index_check(17);
|
||||
ERROR: could not open relation with OID 17
|
||||
SELECT bt_index_parent_check(17);
|
||||
ERROR: could not open relation with OID 17
|
||||
-- verify wrong index types are rejected (error)
|
||||
BEGIN;
|
||||
CREATE INDEX bttest_a_brin_idx ON bttest_a USING brin(id);
|
||||
SELECT bt_index_parent_check('bttest_a_brin_idx');
|
||||
ERROR: only B-Tree indexes are supported as targets for verification
|
||||
DETAIL: Relation "bttest_a_brin_idx" is not a B-Tree index.
|
||||
ROLLBACK;
|
||||
-- normal check outside of xact
|
||||
SELECT bt_index_check('bttest_a_idx');
|
||||
bt_index_check
|
||||
----------------
|
||||
|
||||
(1 row)
|
||||
|
||||
-- more expansive test
|
||||
SELECT bt_index_parent_check('bttest_b_idx');
|
||||
bt_index_parent_check
|
||||
-----------------------
|
||||
|
||||
(1 row)
|
||||
|
||||
BEGIN;
|
||||
SELECT bt_index_check('bttest_a_idx');
|
||||
bt_index_check
|
||||
----------------
|
||||
|
||||
(1 row)
|
||||
|
||||
SELECT bt_index_parent_check('bttest_b_idx');
|
||||
bt_index_parent_check
|
||||
-----------------------
|
||||
|
||||
(1 row)
|
||||
|
||||
-- make sure we don't have any leftover locks
|
||||
SELECT * FROM pg_locks WHERE relation IN ('bttest_a_idx'::regclass, 'bttest_b_idx'::regclass);
|
||||
locktype | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction | pid | mode | granted | fastpath
|
||||
----------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+-----+------+---------+----------
|
||||
(0 rows)
|
||||
|
||||
COMMIT;
|
||||
-- cleanup
|
||||
DROP TABLE bttest_a;
|
||||
DROP TABLE bttest_b;
|
||||
DROP OWNED BY bttest_role; -- permissions
|
||||
DROP ROLE bttest_role;
|
1
contrib/amcheck/sql/check.sql
Normal file
1
contrib/amcheck/sql/check.sql
Normal file
@ -0,0 +1 @@
|
||||
CREATE EXTENSION amcheck;
|
59
contrib/amcheck/sql/check_btree.sql
Normal file
59
contrib/amcheck/sql/check_btree.sql
Normal file
@ -0,0 +1,59 @@
|
||||
-- minimal test, basically just verifying that amcheck
|
||||
CREATE TABLE bttest_a(id int8);
|
||||
CREATE TABLE bttest_b(id int8);
|
||||
|
||||
INSERT INTO bttest_a SELECT * FROM generate_series(1, 100000);
|
||||
INSERT INTO bttest_b SELECT * FROM generate_series(100000, 1, -1);
|
||||
|
||||
CREATE INDEX bttest_a_idx ON bttest_a USING btree (id);
|
||||
CREATE INDEX bttest_b_idx ON bttest_b USING btree (id);
|
||||
|
||||
CREATE ROLE bttest_role;
|
||||
|
||||
-- verify permissions are checked (error due to function not callable)
|
||||
SET ROLE bttest_role;
|
||||
SELECT bt_index_check('bttest_a_idx'::regclass);
|
||||
SELECT bt_index_parent_check('bttest_a_idx'::regclass);
|
||||
RESET ROLE;
|
||||
|
||||
-- we, intentionally, don't check relation permissions - it's useful
|
||||
-- to run this cluster-wide with a restricted account, and as tested
|
||||
-- above explicit permission has to be granted for that.
|
||||
GRANT EXECUTE ON FUNCTION bt_index_check(regclass) TO bttest_role;
|
||||
GRANT EXECUTE ON FUNCTION bt_index_parent_check(regclass) TO bttest_role;
|
||||
SET ROLE bttest_role;
|
||||
SELECT bt_index_check('bttest_a_idx');
|
||||
SELECT bt_index_parent_check('bttest_a_idx');
|
||||
RESET ROLE;
|
||||
|
||||
-- verify plain tables are rejected (error)
|
||||
SELECT bt_index_check('bttest_a');
|
||||
SELECT bt_index_parent_check('bttest_a');
|
||||
|
||||
-- verify non-existing indexes are rejected (error)
|
||||
SELECT bt_index_check(17);
|
||||
SELECT bt_index_parent_check(17);
|
||||
|
||||
-- verify wrong index types are rejected (error)
|
||||
BEGIN;
|
||||
CREATE INDEX bttest_a_brin_idx ON bttest_a USING brin(id);
|
||||
SELECT bt_index_parent_check('bttest_a_brin_idx');
|
||||
ROLLBACK;
|
||||
|
||||
-- normal check outside of xact
|
||||
SELECT bt_index_check('bttest_a_idx');
|
||||
-- more expansive test
|
||||
SELECT bt_index_parent_check('bttest_b_idx');
|
||||
|
||||
BEGIN;
|
||||
SELECT bt_index_check('bttest_a_idx');
|
||||
SELECT bt_index_parent_check('bttest_b_idx');
|
||||
-- make sure we don't have any leftover locks
|
||||
SELECT * FROM pg_locks WHERE relation IN ('bttest_a_idx'::regclass, 'bttest_b_idx'::regclass);
|
||||
COMMIT;
|
||||
|
||||
-- cleanup
|
||||
DROP TABLE bttest_a;
|
||||
DROP TABLE bttest_b;
|
||||
DROP OWNED BY bttest_role; -- permissions
|
||||
DROP ROLE bttest_role;
|
1249
contrib/amcheck/verify_nbtree.c
Normal file
1249
contrib/amcheck/verify_nbtree.c
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user