|
| 1 | +-- Corruption: quick index check — btree + GIN (PG18+). Safe for production, fast. |
| 2 | +-- Requires: CREATE EXTENSION amcheck |
| 3 | +-- All checks use AccessShareLock only — no write blocking. |
| 4 | +-- Checks page-level consistency of btree indexes (bt_index_check). |
| 5 | +-- On PG18+, also checks GIN indexes (gin_index_check). |
| 6 | + |
| 7 | +do $$ |
| 8 | +declare |
| 9 | + rec record; |
| 10 | + idx_count int := 0; |
| 11 | + err_count int := 0; |
| 12 | + skip_count int := 0; |
| 13 | + gin_count int := 0; |
| 14 | + gin_err_count int := 0; |
| 15 | + gin_skip_count int := 0; |
| 16 | + pg_version int; |
| 17 | +begin |
| 18 | + if not exists (select 1 from pg_extension where extname = 'amcheck') then |
| 19 | + raise notice '❌ amcheck extension is not installed. Run: CREATE EXTENSION amcheck;'; |
| 20 | + return; |
| 21 | + end if; |
| 22 | + |
| 23 | + select current_setting('server_version_num')::int into pg_version; |
| 24 | + |
| 25 | + -- === B-tree indexes === |
| 26 | + raise notice ''; |
| 27 | + raise notice '=== B-tree index check (bt_index_check, AccessShareLock) ==='; |
| 28 | + raise notice ''; |
| 29 | + |
| 30 | + for rec in |
| 31 | + select |
| 32 | + n.nspname as schema_name, |
| 33 | + c.relname as index_name, |
| 34 | + t.relname as table_name, |
| 35 | + c.oid as index_oid |
| 36 | + from pg_index i |
| 37 | + join pg_class c on c.oid = i.indexrelid |
| 38 | + join pg_class t on t.oid = i.indrelid |
| 39 | + join pg_namespace n on n.oid = c.relnamespace |
| 40 | + join pg_am a on a.oid = c.relam |
| 41 | + where a.amname = 'btree' |
| 42 | + and c.relpersistence != 't' |
| 43 | + and i.indisvalid |
| 44 | + order by n.nspname, t.relname, c.relname |
| 45 | + loop |
| 46 | + begin |
| 47 | + perform bt_index_check(rec.index_oid); |
| 48 | + idx_count := idx_count + 1; |
| 49 | + exception |
| 50 | + when insufficient_privilege then |
| 51 | + skip_count := skip_count + 1; |
| 52 | + when others then |
| 53 | + raise warning '❌ CORRUPTION in %.%: %', |
| 54 | + rec.schema_name, rec.index_name, sqlerrm; |
| 55 | + err_count := err_count + 1; |
| 56 | + end; |
| 57 | + end loop; |
| 58 | + |
| 59 | + if err_count = 0 and skip_count = 0 then |
| 60 | + raise notice '✅ All % btree indexes OK.', idx_count; |
| 61 | + elsif err_count = 0 then |
| 62 | + raise notice '✅ % btree indexes OK, % skipped (insufficient privileges).', idx_count, skip_count; |
| 63 | + else |
| 64 | + raise warning '❌ % of % btree indexes have corruption!', err_count, idx_count + err_count + skip_count; |
| 65 | + end if; |
| 66 | + |
| 67 | + -- === GIN indexes (PG18+) === |
| 68 | + if pg_version >= 180000 then |
| 69 | + raise notice ''; |
| 70 | + raise notice '=== GIN index check (gin_index_check, AccessShareLock) ==='; |
| 71 | + raise notice ''; |
| 72 | + |
| 73 | + for rec in |
| 74 | + select |
| 75 | + n.nspname as schema_name, |
| 76 | + c.relname as index_name, |
| 77 | + t.relname as table_name, |
| 78 | + c.oid as index_oid |
| 79 | + from pg_index i |
| 80 | + join pg_class c on c.oid = i.indexrelid |
| 81 | + join pg_class t on t.oid = i.indrelid |
| 82 | + join pg_namespace n on n.oid = c.relnamespace |
| 83 | + join pg_am a on a.oid = c.relam |
| 84 | + where a.amname = 'gin' |
| 85 | + and c.relpersistence != 't' |
| 86 | + and i.indisvalid |
| 87 | + order by n.nspname, t.relname, c.relname |
| 88 | + loop |
| 89 | + begin |
| 90 | + perform gin_index_check(rec.index_oid); |
| 91 | + gin_count := gin_count + 1; |
| 92 | + exception |
| 93 | + when insufficient_privilege then |
| 94 | + gin_skip_count := gin_skip_count + 1; |
| 95 | + when others then |
| 96 | + raise warning '❌ CORRUPTION in %.%: %', |
| 97 | + rec.schema_name, rec.index_name, sqlerrm; |
| 98 | + gin_err_count := gin_err_count + 1; |
| 99 | + end; |
| 100 | + end loop; |
| 101 | + |
| 102 | + if gin_count + gin_err_count + gin_skip_count = 0 then |
| 103 | + raise notice 'No GIN indexes found.'; |
| 104 | + elsif gin_err_count = 0 and gin_skip_count = 0 then |
| 105 | + raise notice '✅ All % GIN indexes OK.', gin_count; |
| 106 | + elsif gin_err_count = 0 then |
| 107 | + raise notice '✅ % GIN indexes OK, % skipped (insufficient privileges).', gin_count, gin_skip_count; |
| 108 | + else |
| 109 | + raise warning '❌ % of % GIN indexes have corruption!', gin_err_count, gin_count + gin_err_count + gin_skip_count; |
| 110 | + end if; |
| 111 | + else |
| 112 | + raise notice ''; |
| 113 | + raise notice 'ℹ️ GIN index checking requires PostgreSQL 18+. Skipped.'; |
| 114 | + end if; |
| 115 | +end; |
| 116 | +$$; |
0 commit comments