fix: repair invalid indexes left by a cancelled CREATE INDEX CONCURRENTLY (#598) - #611
Open
christophostertag wants to merge 1 commit into
Open
christophostertag wants to merge 1 commit into
christophostertag wants to merge 1 commit into
Conversation
Contributor
|
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Index ignore patterns can incorrectly suppress unhealthy managed constraint-backed indexes.
Get a fresh assessment by requesting another Copilot review.
Review effort: Balanced
Findings: 1
What changed in this PR
Adds safe recovery for unhealthy indexes left by interrupted concurrent PostgreSQL index builds.
Changes:
- Records unhealthy index catalog state and fingerprints it.
- Plans isolated
REINDEX INDEX CONCURRENTLYrecovery with safety assertions. - Adds stale-plan checks, documentation, and regression coverage.
| File | Description |
|---|---|
cmd/apply/apply.go |
Revalidates plans and improves recovery errors. |
cmd/issue_598_integration_test.go |
Tests interrupted-build recovery scenarios. |
cmd/plan/plan.go |
Prepends index recovery diffs. |
docs/workflow/online-ddl.mdx |
Documents recovery behavior and limitations. |
internal/diff/index_recovery.go |
Classifies and plans index repairs. |
internal/diff/index_recovery_test.go |
Tests recovery classification. |
internal/fingerprint/index_state_test.go |
Tests health-state fingerprints. |
internal/plan/index_recovery.go |
Generates recovery SQL and assertions. |
internal/plan/index_recovery_test.go |
Tests isolation and quoting. |
internal/plan/rewrite.go |
Dispatches recovery rewrites. |
ir/index_state.go |
Builds operational index health metadata. |
ir/inspector.go |
Integrates index-state inspection. |
ir/ir.go |
Adds unhealthy index state to the IR. |
ir/queries/queries.sql |
Queries unhealthy index catalog state. |
ir/queries/queries.sql.go |
Adds generated query bindings. |
Files not reviewed (1)
- ir/queries/queries.sql.go: Generated file
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+34
to
+36
| if i.ignoreConfig != nil && (i.ignoreConfig.ShouldIgnoreIndex(row.IndexName) || (row.ConstraintName != "" && i.ignoreConfig.ShouldIgnoreConstraint(row.ConstraintName))) { | ||
| continue | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Fixes #598.
Problem
Cancelling a native
CREATE INDEX CONCURRENTLYleaves an invalid index behind. On11678c5, replanning against an unchanged desired schema reports "no changes" because the definition still matches. The invalid index stays in place and never gets repaired.Fix
REINDEX INDEX CONCURRENTLY. PostgreSQL preserves the definition, dependencies and any existing unique protection, so no drop and recreate is needed.pg_stat_progress_create_index, with a table-lock fallback for progress rows hidden by role visibility. Only affected unhealthy indexes are checked. Intentionally invalid partition parents that are not touched do not block unrelated work.Boundaries and limits
Existing behavior and concurrency for healthy migrations are unchanged.
Validation
Reproduced the false no-op on untouched upstream
11678c582923fc1a27ed2edf37f3503d1fc466a8with actual interrupted concurrent builds. The final source candidate passes 45 synthetic native CLI cases on PostgreSQL 14.18, 15.13, 16.9, 17.5 and 18.3 (nine per version): both readiness stages, ordinary/unique indexes, rich definitions, duplicate failure, interrupted replacement and retry, and stale saved plans. No generated SQL was manually repaired.Independent database checks cover exact rows and index definitions, all three validity/readiness/liveness flags, uniqueness before/during/after recovery (including NULLS NOT DISTINCT), materialized views, preserved healthy PK/UNIQUE/FK constraints, hidden-role progress, permission failure, and post-swap interruption cleanup. Intentional partition states and explicit index/constraint/table removal have native regression coverage, including newly active maintenance during saved-plan apply.
Executed local commands include:
The new actual-interruption regression fails on untouched upstream and passes with this change. Existing create-index and online-DDL golden controls also pass; sqlc 1.30.0 regeneration is deterministic. Local checks use Go 1.26.4 on macOS; the complete upstream CI suite passed on Go 1.24.13/Linux. Its two existing pgvector cases were skipped because embedded-postgres lacks that third-party extension; all new #598 tests ran and passed.
Independent design/code reviews and a separate exact-commit binary verification passed. Automated review reported 5/5 confidence with no actionable findings.