|
| 1 | +-- minimal test, basically just verifying that amcheck |
| 2 | +CREATE TABLE bttest_a(id int8); |
| 3 | +CREATE TABLE bttest_b(id int8); |
| 4 | +INSERT INTO bttest_a SELECT * FROM generate_series(1, 100000); |
| 5 | +INSERT INTO bttest_b SELECT * FROM generate_series(100000, 1, -1); |
| 6 | +CREATE INDEX bttest_a_idx ON bttest_a USING btree (id); |
| 7 | +CREATE INDEX bttest_b_idx ON bttest_b USING btree (id); |
| 8 | +CREATE ROLE bttest_role; |
| 9 | +-- verify permissions are checked (error due to function not callable) |
| 10 | +SET ROLE bttest_role; |
| 11 | +SELECT bt_index_check('bttest_a_idx'::regclass); |
| 12 | +ERROR: permission denied for function bt_index_check |
| 13 | +SELECT bt_index_parent_check('bttest_a_idx'::regclass); |
| 14 | +ERROR: permission denied for function bt_index_parent_check |
| 15 | +RESET ROLE; |
| 16 | +-- we, intentionally, don't check relation permissions - it's useful |
| 17 | +-- to run this cluster-wide with a restricted account, and as tested |
| 18 | +-- above explicit permission has to be granted for that. |
| 19 | +GRANT EXECUTE ON FUNCTION bt_index_check(regclass) TO bttest_role; |
| 20 | +GRANT EXECUTE ON FUNCTION bt_index_parent_check(regclass) TO bttest_role; |
| 21 | +SET ROLE bttest_role; |
| 22 | +SELECT bt_index_check('bttest_a_idx'); |
| 23 | + bt_index_check |
| 24 | +---------------- |
| 25 | + |
| 26 | +(1 row) |
| 27 | + |
| 28 | +SELECT bt_index_parent_check('bttest_a_idx'); |
| 29 | + bt_index_parent_check |
| 30 | +----------------------- |
| 31 | + |
| 32 | +(1 row) |
| 33 | + |
| 34 | +RESET ROLE; |
| 35 | +-- verify plain tables are rejected (error) |
| 36 | +SELECT bt_index_check('bttest_a'); |
| 37 | +ERROR: "bttest_a" is not an index |
| 38 | +SELECT bt_index_parent_check('bttest_a'); |
| 39 | +ERROR: "bttest_a" is not an index |
| 40 | +-- verify non-existing indexes are rejected (error) |
| 41 | +SELECT bt_index_check(17); |
| 42 | +ERROR: could not open relation with OID 17 |
| 43 | +SELECT bt_index_parent_check(17); |
| 44 | +ERROR: could not open relation with OID 17 |
| 45 | +-- verify wrong index types are rejected (error) |
| 46 | +BEGIN; |
| 47 | +CREATE INDEX bttest_a_brin_idx ON bttest_a USING brin(id); |
| 48 | +SELECT bt_index_parent_check('bttest_a_brin_idx'); |
| 49 | +ERROR: only B-Tree indexes are supported as targets for verification |
| 50 | +DETAIL: Relation "bttest_a_brin_idx" is not a B-Tree index. |
| 51 | +ROLLBACK; |
| 52 | +-- normal check outside of xact |
| 53 | +SELECT bt_index_check('bttest_a_idx'); |
| 54 | + bt_index_check |
| 55 | +---------------- |
| 56 | + |
| 57 | +(1 row) |
| 58 | + |
| 59 | +-- more expansive test |
| 60 | +SELECT bt_index_parent_check('bttest_b_idx'); |
| 61 | + bt_index_parent_check |
| 62 | +----------------------- |
| 63 | + |
| 64 | +(1 row) |
| 65 | + |
| 66 | +BEGIN; |
| 67 | +SELECT bt_index_check('bttest_a_idx'); |
| 68 | + bt_index_check |
| 69 | +---------------- |
| 70 | + |
| 71 | +(1 row) |
| 72 | + |
| 73 | +SELECT bt_index_parent_check('bttest_b_idx'); |
| 74 | + bt_index_parent_check |
| 75 | +----------------------- |
| 76 | + |
| 77 | +(1 row) |
| 78 | + |
| 79 | +-- make sure we don't have any leftover locks |
| 80 | +SELECT * FROM pg_locks WHERE relation IN ('bttest_a_idx'::regclass, 'bttest_b_idx'::regclass); |
| 81 | + locktype | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction | pid | mode | granted | fastpath |
| 82 | +----------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+-----+------+---------+---------- |
| 83 | +(0 rows) |
| 84 | + |
| 85 | +COMMIT; |
| 86 | +-- cleanup |
| 87 | +DROP TABLE bttest_a; |
| 88 | +DROP TABLE bttest_b; |
| 89 | +DROP OWNED BY bttest_role; -- permissions |
| 90 | +DROP ROLE bttest_role; |
0 commit comments