Skip renaming inbox indexes missing from the workspace schema in the 2.66 upgrade

Merged
CH
2d ago
+4-9

What

RenameAgentChatObjectsToInboxCommand (2.66) renames the indexes of the agent chat objects. It now runs ALTER INDEX IF EXISTS … RENAME TO …, so an index that is missing from the workspace schema is skipped and only its metadata is renamed. There is no catalog query.

Why

The 2.66 upgrade is failing on prod for about 1 workspace in 60 (25 so far across the two upgrade runners) with:

ALTER INDEX "workspace_…"."IDX_86b44904be02499c3ea946bf986" RENAME TO "IDX_8808890151ec78fd7ccbd984154"
relation "workspace_….IDX_86b44904be02499c3ea946bf986" does not exist (42P01)

In these workspaces the index metadata exists, but the physical index was never created under either name. The case I checked is an index on agentChatThreadTarget.targetInsurerQuote, a custom relation. The renames run in a single transaction, so the plain ALTER INDEX error aborts it, the workspace fails, and the upgrade run aborts at the end before the next instance step. A try/catch alone would leave the transaction aborted, so IF EXISTS is used instead.

flowchart LR
  A[index rename from metadata] --> B[ALTER INDEX IF EXISTS … RENAME TO]
  B -- index present --> C[renamed]
  B -- index missing --> D[NOTICE, skipped]
  C --> E[update indexMetadata.name]
  D --> E
BeforeAfter
Index presentrenamed in schema and metadataunchanged
Index missing in schema42P01, transaction aborted, workspace upgrade failsmetadata renamed, workspace upgrade continues

Notes for reviewers

  • The change stays in the 2-66 version folder. The shared renameIndexWithoutRebuild is also used by the 2.18 command, so it is untouched.
  • A workspace with a missing index ends up in the same state as before, just with the new name in the metadata. Recreating the missing indexes is out of scope.
  • Checked in a rolled-back transaction on Postgres: a missing index only raises a NOTICE and the transaction continues, and an existing index is renamed. There is no automated test for this path.
  • This needs a 2.66 patch release, so the PR needs the ci:allow-previous-version-upgrade-mutation label. After it ships, the failed workspaces get rerun with upgrade -w <id>.