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 tests for index with included columns
113 SELECT bt_index_parent_check('bttest_multi_idx', true, true);
114 bt_index_parent_check
115 -----------------------
119 -- repeat expansive tests 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, true);
123 bt_index_parent_check
124 -----------------------
129 -- Test for multilevel page deletion/downlink present checks, and rootdescend
132 INSERT INTO delete_test_table SELECT i, 1, 2, 3 FROM generate_series(1,80000) i;
133 ALTER TABLE delete_test_table ADD PRIMARY KEY (a,b,c,d);
134 -- Delete most entries, and vacuum, deleting internal pages and creating "fast
136 DELETE FROM delete_test_table WHERE a < 79990;
137 VACUUM delete_test_table;
138 SELECT bt_index_parent_check('delete_test_table_pkey', true);
139 bt_index_parent_check
140 -----------------------
145 -- BUG #15597: must not assume consistent input toasting state when forming
146 -- tuple. Bloom filter must fingerprint normalized index tuple representation.
148 CREATE TABLE toast_bug(buggy text);
149 ALTER TABLE toast_bug ALTER COLUMN buggy SET STORAGE plain;
150 -- pg_attribute entry for toasty.buggy will have plain storage:
151 CREATE INDEX toasty ON toast_bug(buggy);
152 -- Whereas pg_attribute entry for toast_bug.buggy now has extended storage:
153 ALTER TABLE toast_bug ALTER COLUMN buggy SET STORAGE extended;
154 -- Insert compressible heap tuple (comfortably exceeds TOAST_TUPLE_THRESHOLD):
155 INSERT INTO toast_bug SELECT repeat('a', 2200);
156 -- Should not get false positive report of corruption:
157 SELECT bt_index_check('toasty', true);
166 DROP TABLE bttest_multi;
167 DROP TABLE delete_test_table;
168 DROP TABLE toast_bug;
169 DROP OWNED BY bttest_role; -- permissions
170 DROP ROLE bttest_role;