|
| 1 | +-- migration-safe: Metadata-only storage option; existing and new workers retain the same index and query semantics. |
| 2 | +ALTER INDEX "public"."user_table_rows_tenant_data_gin_idx" SET (fastupdate = off);--> statement-breakpoint |
| 3 | +-- Release the DDL lock before maintenance and index builds. Everything below is safe to replay after a partial migration. |
| 4 | +COMMIT;--> statement-breakpoint |
| 5 | +-- Disabling fastupdate does not flush existing pending entries. Drain them once without rebuilding the index. |
| 6 | +SET statement_timeout = '5min';--> statement-breakpoint |
| 7 | +SELECT pg_catalog.gin_clean_pending_list('"public"."user_table_rows_tenant_data_gin_idx"'::regclass);--> statement-breakpoint |
| 8 | +SET statement_timeout = 0;--> statement-breakpoint |
| 9 | +-- Same key columns as workflow_execution_logs_workspace_activity_idx plus a trailing `level`, so it serves every scan the old index did and keeps the logs stats read index-only. |
| 10 | +SET lock_timeout = '5s';--> statement-breakpoint |
| 11 | +-- A replay keeps a valid build of the new index, since the drop below may already have removed the one it supersedes. Only an invalid build left by an interrupted run is set aside and rebuilt. |
| 12 | +DO $$ |
| 13 | +BEGIN |
| 14 | + IF EXISTS ( |
| 15 | + SELECT 1 FROM pg_index |
| 16 | + WHERE indexrelid = to_regclass('"public"."workflow_execution_logs_workspace_activity_level_idx_failed"') |
| 17 | + AND (indisvalid OR indrelid <> '"public"."workflow_execution_logs"'::regclass) |
| 18 | + ) THEN |
| 19 | + RAISE EXCEPTION 'Refusing to drop unexpected index workflow_execution_logs_workspace_activity_level_idx_failed'; |
| 20 | + END IF; |
| 21 | + IF EXISTS ( |
| 22 | + SELECT 1 FROM pg_index |
| 23 | + WHERE indexrelid = to_regclass('"public"."workflow_execution_logs_workspace_activity_level_idx"') |
| 24 | + AND indrelid = '"public"."workflow_execution_logs"'::regclass |
| 25 | + AND NOT indisvalid |
| 26 | + ) THEN |
| 27 | + ALTER INDEX "public"."workflow_execution_logs_workspace_activity_level_idx" RENAME TO "workflow_execution_logs_workspace_activity_level_idx_failed"; |
| 28 | + END IF; |
| 29 | +END $$;--> statement-breakpoint |
| 30 | +SET lock_timeout = 0;--> statement-breakpoint |
| 31 | +-- migration-safe: drops only the invalid interrupted build set aside above; a valid replacement and the index it supersedes are never touched here. |
| 32 | +DROP INDEX CONCURRENTLY IF EXISTS "public"."workflow_execution_logs_workspace_activity_level_idx_failed";--> statement-breakpoint |
| 33 | +CREATE INDEX CONCURRENTLY IF NOT EXISTS "workflow_execution_logs_workspace_activity_level_idx" ON "workflow_execution_logs" USING btree ("workspace_id","started_at","status","total_duration_ms","workflow_id","trigger","level");--> statement-breakpoint |
| 34 | +-- migration-safe: superseded by workflow_execution_logs_workspace_activity_level_idx (a key-prefix superset, built above); no code references index names. |
| 35 | +DROP INDEX CONCURRENTLY IF EXISTS "workflow_execution_logs_workspace_activity_idx";--> statement-breakpoint |
| 36 | +SET lock_timeout = '5s'; |
0 commit comments