Skip to content

Commit

Permalink
Reindex corrupted discovery indexes (#6865)
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondjacobson authored and theoilie committed Dec 6, 2023
1 parent 95f6286 commit 8785ac9
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/discovery-provider/ddl/migrations/0041_repair_indexes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CREATE EXTENSION IF NOT EXISTS amcheck;

DO $$
DECLARE
rec RECORD;
BEGIN
FOR rec IN SELECT indexrelid::regclass AS index_name FROM pg_index WHERE indisvalid LOOP
RAISE NOTICE 'Checking index: %', rec.index_name;
BEGIN
-- Attempt to check the index
PERFORM bt_index_check(rec.index_name);
EXCEPTION WHEN OTHERS THEN
-- If an error occurs (indicating a potentially invalid index), reindex it
RAISE NOTICE 'Reindexing due to error in index: %', rec.index_name;
EXECUTE 'REINDEX INDEX ' || rec.index_name;
-- Log that reindexing is complete
RAISE NOTICE 'Reindexing completed for index: %', rec.index_name;
END;
END LOOP;
END;
$$ LANGUAGE plpgsql;

0 comments on commit 8785ac9

Please sign in to comment.