diff --git a/packages/discovery-provider/ddl/migrations/0041_repair_indexes.sql b/packages/discovery-provider/ddl/migrations/0041_repair_indexes.sql new file mode 100644 index 00000000000..ed78739db8f --- /dev/null +++ b/packages/discovery-provider/ddl/migrations/0041_repair_indexes.sql @@ -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;