Skip to content

feat(schemadiff): name the index a REINDEX CONCURRENTLY copy belongs to - #734

Merged
aparajon merged 3 commits into
mainfrom
armand/invalid-index-reindex-of
Dec 4, 2026
Merged

aparajon merged 3 commits into
mainfrom
armand/invalid-index-reindex-of

Conversation

@aparajon

@aparajon aparajon commented Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator

Why

A REINDEX CONCURRENTLY that is cancelled or fails leaves its copy of the index invalid beside the index it rebuilt. The copy only duplicates a valid index, so dropping it is the whole fix. That advice is different from the advice for an abandoned build. InvalidIndex did not say which index a copy belongs to, so a caller had to match the server's copy names itself and read the catalog again to confirm the original exists.

What

  • InvalidIndex.ReindexOf names the valid index on the same table that an invalid index is a REINDEX CONCURRENTLY copy of.
  • The server names a copy after its index, with a _ccnew or _ccold suffix, a number when that name is taken, and the index's name truncated to fit. The listing matches the valid index whose name is the copy's name without the suffix. When the copy's name is at the identifier limit, it matches the valid index whose name starts with that.
  • ReindexOf is empty for any other invalid index, for an index that only has a copy's suffix, and when more than one valid index fits.

Before and after

orders_region_idx is valid. A REINDEX INDEX CONCURRENTLY orders_region_idx was cancelled and left orders_region_idx_ccnew invalid.

Before                                  After

ListInvalidIndexes                      ListInvalidIndexes
┌──────────────────────────────────┐    ┌──────────────────────────────────┐
│ Name: orders_region_idx_ccnew    │    │ Name: orders_region_idx_ccnew    │
│ (no link to orders_region_idx)   │ ✗  │ ReindexOf: orders_region_idx     │
└────────────────┬─────────────────┘    └────────────────┬─────────────────┘
                 │ caller matches the                    │
                 ▼ name and reads again                  ▼
┌──────────────────────────────────┐    ┌──────────────────────────────────┐
│ caller's own regex + catalog     │    │ "drop the copy": no second read  │
│ read                             │    │                                  │
└──────────────────────────────────┘    └──────────────────────────────────┘

How

A correlated subquery in the existing listing finds the one valid index on the copy's table that the name rule matches. The listing never reads anything beyond its single catalog query, and every catalog name and operator stays pg_catalog-qualified. The test leaves each copy invalid with a failed unique build under the copy's name. That is the same catalog state a cancelled reindex leaves, without holding a reindex that would wait on every older transaction in a shared test database. One copy carries the truncated name the server gives a copy of a 63-byte index name, checked against real cancelled reindexes. The test also lists a failed build whose name only looks like a copy's.

Risk

The listing adds one field and changes no existing one. Only the name links a copy to its index, so an ambiguous match reports no index rather than a guess. Upholds CO-9: the new subquery qualifies every catalog name and operator.

Upgrade notes

None.

🤖 Generated with Claude Code

InvalidIndex.ReindexOf names the valid index on the same table that an invalid index is a REINDEX CONCURRENTLY copy of, matched on the server's copy names, including a name truncated to fit the suffix. It is empty for any other invalid index and when more than one valid index fits.

Signed-off-by: Armand Parajon <armand@squareup.com>
…held reindex

A held REINDEX CONCURRENTLY waits on every older transaction in the database, which a shared test server's other tests read as a lock wait on their own holder, so one cancelled its run early. A failed unique build under a copy's name leaves the same catalog state without waiting on anyone.

Signed-off-by: Armand Parajon <armand@squareup.com>
@aparajon
aparajon marked this pull request as ready for review December 4, 2026 10:10
@Kiran01bm

Copy link
Copy Markdown
Collaborator

🤖 Review findings - created by Kiran's code review agent - for pg-sprite/pull/734, 5fd562a.

Verdict: 5 findings — 3 non-blocking (name-only match, running multi-index reindex, test coverage), 2 suggestions.

Non-blocking

1. A name-only match labels an index with a different definition as a reindex copy, and the test pins that as correct. The subquery checks only the table and the name (invalid_indexes.go:81-89). The fixture builds CREATE UNIQUE INDEX CONCURRENTLY pads_pad_idx_ccnew beside a non-unique pads_pad_idx and expects "pads_pad_idx" back (invalid_indexes_integration_test.go:295-315). A real copy always matches its original's definition, so the field doc's promise that "dropping the copy is the whole fix" (invalid_indexes.go:41-49) is wrong here: a consumer would tell the operator to drop a failed unique build that was never a duplicate. Comparing indisunique, indkey, indclass, indcollation, indoption, indexprs, indpred and relam before matching would rule this out cheaply.

2. While a multi-index REINDEX ... CONCURRENTLY runs, its other copies look like abandoned leftovers (PLAUSIBLE). BuilderPID matches only the single index_relid in the progress row (invalid_indexes.go:90-95), and the server reports one index at a time. Scenario: REINDEX TABLE CONCURRENTLY orders with three indexes is validating the second one. The first and third _ccnew copies show BuilderPID 0, BuilderObservable true and a non-empty ReindexOf, which is exactly what a cancelled reindex looks like. The behaviour predates this PR, but ReindexOf now makes that state read as "safe to drop". At minimum, say in the field doc that ReindexOf is set for a reindex that is still running. A full fix would treat any REINDEX CONCURRENTLY progress row on t.oid as the builder of a _cc* copy.

3. The test never runs a real REINDEX, and the _ccold and numbered-suffix branches are untested. All three fixtures are hand-named failed unique builds (invalid_indexes_integration_test.go:298-305), so the long[:57] truncation is the test's own model of the server, not something the server produced. Nothing covers _ccold, _ccnew1, or a multibyte name near 63 bytes. A real failed reindex does not need a held transaction: build an expression index on an immutable function, redefine the function to raise, and run REINDEX INDEX CONCURRENTLY. The build phase waits only on the table's lockers, and the server leaves a genuine _ccnew copy behind.

General suggestions

4. A prefix match can cancel an exact match. When the copy's name is 60 bytes or longer, the OR adds starts_with matches beside the exact one, so count(*) = 2 returns NULL even though an exact-name index exists (invalid_indexes.go:81, invalid_indexes.go:87-89). Example: a 54-byte index x, a valid x_more, and an invalid x_ccnew. Preferring the exact match and using the prefix only as a fallback avoids it.

5. The subquery is the dense compound SQL that AGENTS.md asks to avoid. The suffix regex appears three times, inside a nested OR/AND (invalid_indexes.go:86-89). A LATERAL that computes the base name and the truncated flag once would read more clearly in an incident.

The one thing that could have broken, verified

Truncation matching. The server's makeObjectName cuts the index's name to 63 - len("_ccnew") = 57 bytes, or 56 for a numbered _ccnewN. pg_mbcliplen can drop up to 3 more bytes on a multibyte boundary, so a truncated copy name is always 60 bytes or longer. That makes the octet_length >= 60 gate plus starts_with complete for every truncated copy, and the test's long[:57] + "_ccnew" matches the server's rule for ASCII names.

Verified correct

  • The match is scoped to the same table (vi.indrelid = t.oid), so a partition's copy matches only that partition's index, and an inherited child never matches its parent's index.
  • Only valid indexes are candidates (vi.indisvalid), so the copy never matches itself or another invalid copy.
  • The $-anchored regex strips only the last suffix, so a reindex of foo_ccnew maps foo_ccnew_ccnew back to foo_ccnew.
  • The new column is scanned in the right position, and NULL maps to an empty ReindexOf.
  • No consumer reads ReindexOf yet (internal/cli/pull.go leftOutFix is unchanged), so the field is added ahead of use and no plan or advice changes in this PR.
  • Every new function, operator and cast is pg_catalog-qualified, and the existing CO-9 test covers the query.
  • CI: 18/18 checks pass at 5fd562ab.

This review was generated by Claude Code (claude-opus-5-5).

@Kiran01bm Kiran01bm left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Approved on Kiran's (@kmuddukrishna) behalf by the scheduled review agent — no blocking findings at 5fd562a. See the review comment above; non-blocking findings and suggestions, if any, are not merge gates.

… its table reindexes

ReindexOf now names an index only when it has the copy's definition, and an exact name wins over a truncated one. A REINDEX CONCURRENTLY of the copy's table counts as its builder, since the progress view names only the index it is on now. The test runs real failed reindexes for the truncated, numbered and multibyte names, and covers _ccold.

Signed-off-by: Armand Parajon <armand@squareup.com>
@aparajon

aparajon commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator Author

🤖 Addressed every finding from the review of 5fd562a in f8d3608:

  1. Name-only match. A valid index now counts as the copy's original only if its definition matches too: pg_get_indexdef must read the same for both once the copy's name is swapped in. So a failed unique build beside a non-unique index of the same name gets no ReindexOf. The test pins that case as empty.
  2. Multi-index reindex. A copy with a reindex suffix now counts any REINDEX CONCURRENTLY progress row on its table as its builder, so the other copies of a running multi-index reindex show its PID, not 0. The ReindexOf doc says it is also set while a reindex is still running, and that the copy is safe to drop only once BuilderPID is zero.
  3. Coverage. The test now runs real failing REINDEX INDEX CONCURRENTLY commands (an expression index on a function that is then redefined to raise), so the server names each copy itself. It covers a truncated 63-byte name, the numbered _ccnew1 from a second failure, a multibyte name the server cuts to the last whole character, and _ccold.
  4. Prefix match beside an exact match. An exact-name match now wins. The prefix match is used only when there is no exact one. The test covers your 54-byte x / x_more / x_ccnew example.
  5. Dense SQL. A LATERAL subquery computes the copy check, the base name and the may-be-truncated flag once, and the match reads from a small CTE.

The catalog-qualification test (CO-9) and the schemadiff integration tests pass.

— Claude Code (Claude Opus 5.5)

@aparajon
aparajon merged commit 5e15eec into main Dec 4, 2026
18 checks passed
@aparajon
aparajon deleted the armand/invalid-index-reindex-of branch December 4, 2026 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants