| 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); |
| 5 |
|
| 6 |
-- Stabilize tests |
| 7 |
ALTER TABLE bttest_a SET (autovacuum_enabled = false); |
| 8 |
ALTER TABLE bttest_b SET (autovacuum_enabled = false); |
| 9 |
ALTER TABLE bttest_multi SET (autovacuum_enabled = false); |
| 10 |
ALTER TABLE delete_test_table SET (autovacuum_enabled = false); |
| 11 |
|
| 12 |
INSERT INTO bttest_a SELECT * FROM generate_series(1, 100000); |
| 13 |
INSERT INTO bttest_b SELECT * FROM generate_series(100000, 1, -1); |
| 14 |
INSERT INTO bttest_multi SELECT i, i%2 FROM generate_series(1, 100000) as i; |
| 15 |
|
| 16 |
CREATE INDEX bttest_a_idx ON bttest_a USING btree (id) WITH (deduplicate_items = ON); |
| 17 |
CREATE INDEX bttest_b_idx ON bttest_b USING btree (id); |
| 18 |
CREATE UNIQUE INDEX bttest_multi_idx ON bttest_multi |
| 19 |
USING btree (id) INCLUDE (data); |
| 20 |
|
| 21 |
CREATE ROLE regress_bttest_role; |
| 22 |
|
| 23 |
-- verify permissions are checked (error due to function not callable) |
| 24 |
SET ROLE regress_bttest_role; |
| 25 |
SELECT bt_index_check('bttest_a_idx'::regclass); |
| 26 |
SELECT bt_index_parent_check('bttest_a_idx'::regclass); |
| 27 |
RESET ROLE; |
| 28 |
|
| 29 |
-- we, intentionally, don't check relation permissions - it's useful |
| 30 |
-- to run this cluster-wide with a restricted account, and as tested |
| 31 |
-- above explicit permission has to be granted for that. |
| 32 |
GRANT EXECUTE ON FUNCTION bt_index_check(regclass) TO regress_bttest_role; |
| 33 |
GRANT EXECUTE ON FUNCTION bt_index_parent_check(regclass) TO regress_bttest_role; |
| 34 |
GRANT EXECUTE ON FUNCTION bt_index_check(regclass, boolean) TO regress_bttest_role; |
| 35 |
GRANT EXECUTE ON FUNCTION bt_index_parent_check(regclass, boolean) TO regress_bttest_role; |
| 36 |
SET ROLE regress_bttest_role; |
| 37 |
SELECT bt_index_check('bttest_a_idx'); |
| 38 |
SELECT bt_index_parent_check('bttest_a_idx'); |
| 39 |
RESET ROLE; |
| 40 |
|
| 41 |
-- verify plain tables are rejected (error) |
| 42 |
SELECT bt_index_check('bttest_a'); |
| 43 |
SELECT bt_index_parent_check('bttest_a'); |
| 44 |
|
| 45 |
-- verify non-existing indexes are rejected (error) |
| 46 |
SELECT bt_index_check(17); |
| 47 |
SELECT bt_index_parent_check(17); |
| 48 |
|
| 49 |
-- verify wrong index types are rejected (error) |
| 50 |
BEGIN; |
| 51 |
CREATE INDEX bttest_a_brin_idx ON bttest_a USING brin(id); |
| 52 |
SELECT bt_index_parent_check('bttest_a_brin_idx'); |
| 53 |
ROLLBACK; |
| 54 |
|
| 55 |
-- normal check outside of xact |
| 56 |
SELECT bt_index_check('bttest_a_idx'); |
| 57 |
-- more expansive tests |
| 58 |
SELECT bt_index_check('bttest_a_idx', true); |
| 59 |
SELECT bt_index_parent_check('bttest_b_idx', true); |
| 60 |
|
| 61 |
BEGIN; |
| 62 |
SELECT bt_index_check('bttest_a_idx'); |
| 63 |
SELECT bt_index_parent_check('bttest_b_idx'); |
| 64 |
-- make sure we don't have any leftover locks |
| 65 |
SELECT * FROM pg_locks |
| 66 |
WHERE relation = ANY(ARRAY['bttest_a', 'bttest_a_idx', 'bttest_b', 'bttest_b_idx']::regclass[]) |
| 67 |
AND pid = pg_backend_pid(); |
| 68 |
COMMIT; |
| 69 |
|
| 70 |
-- Deduplication |
| 71 |
TRUNCATE bttest_a; |
| 72 |
INSERT INTO bttest_a SELECT 42 FROM generate_series(1, 2000); |
| 73 |
SELECT bt_index_check('bttest_a_idx', true); |
| 74 |
|
| 75 |
-- normal check outside of xact for index with included columns |
| 76 |
SELECT bt_index_check('bttest_multi_idx'); |
| 77 |
-- more expansive tests for index with included columns |
| 78 |
SELECT bt_index_parent_check('bttest_multi_idx', true, true); |
| 79 |
|
| 80 |
-- repeat expansive tests for index built using insertions |
| 81 |
TRUNCATE bttest_multi; |
| 82 |
INSERT INTO bttest_multi SELECT i, i%2 FROM generate_series(1, 100000) as i; |
| 83 |
SELECT bt_index_parent_check('bttest_multi_idx', true, true); |
| 84 |
|
| 85 |
-- |
| 86 |
-- Test for multilevel page deletion/downlink present checks, and rootdescend |
| 87 |
-- checks |
| 88 |
-- |
| 89 |
INSERT INTO delete_test_table SELECT i, 1, 2, 3 FROM generate_series(1,80000) i; |
| 90 |
ALTER TABLE delete_test_table ADD PRIMARY KEY (a,b,c,d); |
| 91 |
-- Delete most entries, and vacuum, deleting internal pages and creating "fast |
| 92 |
-- root" |
| 93 |
DELETE FROM delete_test_table WHERE a < 79990; |
| 94 |
VACUUM delete_test_table; |
| 95 |
SELECT bt_index_parent_check('delete_test_table_pkey', true); |
| 96 |
|
| 97 |
-- |
| 98 |
-- BUG #15597: must not assume consistent input toasting state when forming |
| 99 |
-- tuple. Bloom filter must fingerprint normalized index tuple representation. |
| 100 |
-- |
| 101 |
CREATE TABLE toast_bug(buggy text); |
| 102 |
ALTER TABLE toast_bug ALTER COLUMN buggy SET STORAGE extended; |
| 103 |
CREATE INDEX toasty ON toast_bug(buggy); |
| 104 |
|
| 105 |
-- pg_attribute entry for toasty.buggy (the index) will have plain storage: |
| 106 |
UPDATE pg_attribute SET attstorage = 'p' |
| 107 |
WHERE attrelid = 'toasty'::regclass AND attname = 'buggy'; |
| 108 |
|
| 109 |
-- Whereas pg_attribute entry for toast_bug.buggy (the table) still has extended storage: |
| 110 |
SELECT attstorage FROM pg_attribute |
| 111 |
WHERE attrelid = 'toast_bug'::regclass AND attname = 'buggy'; |
| 112 |
|
| 113 |
-- Insert compressible heap tuple (comfortably exceeds TOAST_TUPLE_THRESHOLD): |
| 114 |
INSERT INTO toast_bug SELECT repeat('a', 2200); |
| 115 |
-- Should not get false positive report of corruption: |
| 116 |
SELECT bt_index_check('toasty', true); |
| 117 |
|
| 118 |
-- |
| 119 |
-- Check that index expressions and predicates are run as the table's owner, |
| 120 |
-- with empty search_path |
| 121 |
-- |
| 122 |
TRUNCATE bttest_a; |
| 123 |
INSERT INTO bttest_a SELECT * FROM generate_series(1, 1000); |
| 124 |
ALTER TABLE bttest_a OWNER TO regress_bttest_role; |
| 125 |
-- A dummy index function checking current_user |
| 126 |
CREATE FUNCTION ifun(int8) RETURNS int8 AS $$ |
| 127 |
BEGIN |
| 128 |
ASSERT current_setting('search_path') = 'pg_catalog, pg_temp', |
| 129 |
format('ifun(%s) called with current_schemas %s, search_path %s', |
| 130 |
$1, current_schemas(true), current_setting('search_path')); |
| 131 |
ASSERT "current_user"() = 'regress_bttest_role', |
| 132 |
format('ifun(%s) called by %s', $1, current_user); |
| 133 |
RETURN $1; |
| 134 |
END; |
| 135 |
$$ LANGUAGE plpgsql IMMUTABLE; |
| 136 |
|
| 137 |
CREATE INDEX bttest_a_expr_idx ON bttest_a ((ifun(id) + ifun(0))) |
| 138 |
WHERE ifun(id + 10) > ifun(10); |
| 139 |
BEGIN; |
| 140 |
SET LOCAL check_function_bodies = off; |
| 141 |
CREATE SCHEMA preempt; |
| 142 |
GRANT USAGE ON SCHEMA preempt TO regress_bttest_role; |
| 143 |
SET LOCAL search_path = preempt, pg_catalog, public; |
| 144 |
CREATE FUNCTION "current_user"() RETURNS name AS $$ |
| 145 |
broken |
| 146 |
$$ LANGUAGE sql STABLE PARALLEL SAFE STRICT; |
| 147 |
SELECT bt_index_check('bttest_a_expr_idx', true); |
| 148 |
ROLLBACK; |
| 149 |
|
| 150 |
-- UNIQUE constraint check |
| 151 |
SELECT bt_index_check('bttest_a_idx', heapallindexed => true, checkunique => true); |
| 152 |
SELECT bt_index_check('bttest_b_idx', heapallindexed => false, checkunique => true); |
| 153 |
SELECT bt_index_parent_check('bttest_a_idx', heapallindexed => true, rootdescend => true, checkunique => true); |
| 154 |
SELECT bt_index_parent_check('bttest_b_idx', heapallindexed => true, rootdescend => false, checkunique => true); |
| 155 |
|
| 156 |
-- Check that null values in an unique index are not treated as equal |
| 157 |
CREATE TABLE bttest_unique_nulls (a serial, b int, c int UNIQUE); |
| 158 |
INSERT INTO bttest_unique_nulls VALUES (generate_series(1, 10000), 2, default); |
| 159 |
SELECT bt_index_check('bttest_unique_nulls_c_key', heapallindexed => true, checkunique => true); |
| 160 |
CREATE INDEX on bttest_unique_nulls (b,c); |
| 161 |
SELECT bt_index_check('bttest_unique_nulls_b_c_idx', heapallindexed => true, checkunique => true); |
| 162 |
|
| 163 |
-- Check support of both 1B and 4B header sizes of short varlena datum |
| 164 |
CREATE TABLE varlena_bug (v text); |
| 165 |
ALTER TABLE varlena_bug ALTER column v SET storage plain; |
| 166 |
INSERT INTO varlena_bug VALUES ('x'); |
| 167 |
COPY varlena_bug from stdin; |
| 168 |
x |
| 169 |
\. |
| 170 |
CREATE INDEX varlena_bug_idx on varlena_bug(v); |
| 171 |
SELECT bt_index_check('varlena_bug_idx', true); |
| 172 |
|
| 173 |
-- Also check that we compress varlena values, which were previously stored |
| 174 |
-- uncompressed in index. |
| 175 |
INSERT INTO varlena_bug VALUES (repeat('Test', 250)); |
| 176 |
ALTER TABLE varlena_bug ALTER COLUMN v SET STORAGE extended; |
| 177 |
SELECT bt_index_check('varlena_bug_idx', true); |
| 178 |
|
| 179 |
-- cleanup |
| 180 |
DROP TABLE bttest_a; |
| 181 |
DROP TABLE bttest_b; |
| 182 |
DROP TABLE bttest_multi; |
| 183 |
DROP TABLE delete_test_table; |
| 184 |
DROP TABLE toast_bug; |
| 185 |
DROP FUNCTION ifun(int8); |
| 186 |
DROP TABLE bttest_unique_nulls; |
| 187 |
DROP OWNED BY regress_bttest_role; -- permissions |
| 188 |
DROP ROLE regress_bttest_role; |
| 189 |
DROP TABLE varlena_bug; |