1 CREATE TABLE bttest_a(id int8);
2 CREATE TABLE bttest_b(id int8);
3 CREATE TABLE bttest_multi(id int8, data int8);
4 CREATE TABLE delete_test_table (a bigint, b bigint, c bigint, d bigint);
6 ALTER TABLE bttest_a SET (autovacuum_enabled = false);
7 ALTER TABLE bttest_b SET (autovacuum_enabled = false);
8 ALTER TABLE bttest_multi SET (autovacuum_enabled = false);
9 ALTER TABLE delete_test_table SET (autovacuum_enabled = false);
10 INSERT INTO bttest_a SELECT * FROM generate_series(1, 100000);
11 INSERT INTO bttest_b SELECT * FROM generate_series(100000, 1, -1);
12 INSERT INTO bttest_multi SELECT i, i%2 FROM generate_series(1, 100000) as i;
13 CREATE INDEX bttest_a_idx ON bttest_a USING btree (id);
14 CREATE INDEX bttest_b_idx ON bttest_b USING btree (id);
15 CREATE UNIQUE INDEX bttest_multi_idx ON bttest_multi
16 USING btree (id) INCLUDE (data);
17 CREATE ROLE bttest_role;
18 -- verify permissions are checked (error due to function not callable)
20 SELECT bt_index_check('bttest_a_idx'::regclass);
21 ERROR: permission denied for function bt_index_check
22 SELECT bt_index_parent_check('bttest_a_idx'::regclass);
23 ERROR: permission denied for function bt_index_parent_check
25 -- we, intentionally, don't check relation permissions - it's useful
26 -- to run this cluster-wide with a restricted account, and as tested
27 -- above explicit permission has to be granted for that.
28 GRANT EXECUTE ON FUNCTION bt_index_check(regclass) TO bttest_role;
29 GRANT EXECUTE ON FUNCTION bt_index_parent_check(regclass) TO bttest_role;
30 GRANT EXECUTE ON FUNCTION bt_index_check(regclass, boolean) TO bttest_role;
31 GRANT EXECUTE ON FUNCTION bt_index_parent_check(regclass, boolean) TO bttest_role;
33 SELECT bt_index_check('bttest_a_idx');
39 SELECT bt_index_parent_check('bttest_a_idx');
41 -----------------------
46 -- verify plain tables are rejected (error)
47 SELECT bt_index_check('bttest_a');
48 ERROR: "bttest_a" is not an index
49 SELECT bt_index_parent_check('bttest_a');
50 ERROR: "bttest_a" is not an index
51 -- verify non-existing indexes are rejected (error)
52 SELECT bt_index_check(17);
53 ERROR: could not open relation with OID 17
54 SELECT bt_index_parent_check(17);
55 ERROR: could not open relation with OID 17
56 -- verify wrong index types are rejected (error)
58 CREATE INDEX bttest_a_brin_idx ON bttest_a USING brin(id);
59 SELECT bt_index_parent_check('bttest_a_brin_idx');
60 ERROR: only B-Tree indexes are supported as targets for verification
61 DETAIL: Relation "bttest_a_brin_idx" is not a B-Tree index.
63 -- normal check outside of xact
64 SELECT bt_index_check('bttest_a_idx');
70 -- more expansive tests
71 SELECT bt_index_check('bttest_a_idx', true);
77 SELECT bt_index_parent_check('bttest_b_idx', true);
79 -----------------------
84 SELECT bt_index_check('bttest_a_idx');
90 SELECT bt_index_parent_check('bttest_b_idx');
92 -----------------------
96 -- make sure we don't have any leftover locks
97 SELECT * FROM pg_locks
98 WHERE relation = ANY(ARRAY['bttest_a', 'bttest_a_idx', 'bttest_b', 'bttest_b_idx']::regclass[])
99 AND pid = pg_backend_pid();
100 locktype | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction | pid | mode | granted | fastpath
101 ----------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+-----+------+---------+----------
105 -- normal check outside of xact for index with included columns
106 SELECT bt_index_check('bttest_multi_idx');
112 -- more expansive test for index with included columns
113 SELECT bt_index_parent_check('bttest_multi_idx', true);
114 bt_index_parent_check
115 -----------------------
119 -- repeat expansive test for index built using insertions
120 TRUNCATE bttest_multi;
121 INSERT INTO bttest_multi SELECT i, i%2 FROM generate_series(1, 100000) as i;
122 SELECT bt_index_parent_check('bttest_multi_idx', true);
123 bt_index_parent_check
124 -----------------------
129 -- Test for multilevel page deletion/downlink present checks
131 INSERT INTO delete_test_table SELECT i, 1, 2, 3 FROM generate_series(1,80000) i;
132 ALTER TABLE delete_test_table ADD PRIMARY KEY (a,b,c,d);
133 -- Delete many entries, and vacuum. This causes page deletions.
134 DELETE FROM delete_test_table WHERE a > 40000;
135 VACUUM delete_test_table;
136 -- Delete most entries, and vacuum, deleting internal pages and creating "fast
138 DELETE FROM delete_test_table WHERE a < 79990;
139 VACUUM delete_test_table;
140 SELECT bt_index_parent_check('delete_test_table_pkey', true);
141 bt_index_parent_check
142 -----------------------
147 -- BUG #15597: must not assume consistent input toasting state when forming
148 -- tuple. Bloom filter must fingerprint normalized index tuple representation.
150 CREATE TABLE toast_bug(buggy text);
151 ALTER TABLE toast_bug ALTER COLUMN buggy SET STORAGE plain;
152 -- pg_attribute entry for toasty.buggy will have plain storage:
153 CREATE INDEX toasty ON toast_bug(buggy);
154 -- Whereas pg_attribute entry for toast_bug.buggy now has extended storage:
155 ALTER TABLE toast_bug ALTER COLUMN buggy SET STORAGE extended;
156 -- Insert compressible heap tuple (comfortably exceeds TOAST_TUPLE_THRESHOLD):
157 INSERT INTO toast_bug SELECT repeat('a', 2200);
158 -- Should not get false positive report of corruption:
159 SELECT bt_index_check('toasty', true);
168 DROP TABLE bttest_multi;
169 DROP TABLE delete_test_table;
170 DROP TABLE toast_bug;
171 DROP OWNED BY bttest_role; -- permissions
172 DROP ROLE bttest_role;