Recompute flat entity maps before skipping a deferred operation
Problem
Since deferred operations became the default (#29081, 2026-10-28), some relation indexes exist in core."indexMetadata" but not in the workspace schema. On prod, 2,553 indexes are missing across 181 workspaces, and new ones appear every day. They sit on the morph target columns of noteTarget, taskTarget, attachment, timelineActivity and agentChatThreadTarget. The same fields also keep NOT VALID foreign keys. This drift makes the 2.66 RenameAgentChatObjectsToInboxCommand fail (worked around in #32278).
A migration commits its build_index / validate_foreignKey rows before it invalidates the flat entity maps cache. The worker job reads the PENDING rows from the database, then the maps from the cache. When the job runs in between, the new index or field is not in the cache yet. The handlers treated "not in maps" as "already deleted" and returned, so the row was deleted as a success. No index was built, and there was no error and no FAILED row.
It mostly happens when objects are created in parallel (create_many_object_metadata runs createOneObject through Promise.all). On prod, an object created within 2 s of another loses its indexes 38% of the time, against 1.1% when isolated.
Fix
Each handler returns payloadEntityNotFound when the entity named in its payload (indexMetadataId, fieldMetadataId, objectMetadataId) is not in the maps. When that happens, or when a nested lookup throws ENTITY_NOT_FOUND, the job runner recomputes the maps once from the database and runs the operation again. After the recompute, a missing payload entity means it was really deleted and the operation is skipped. A nested lookup that still misses throws and fails the operation as before. The recompute happens at most once per job run, and only on a miss.
flowchart TD
A[Job reads<br/>PENDING rows] --> B[Load maps<br/>from cache]
B --> C[Run handler]
C -->|done| D[Delete row]
C -->|entity not<br/>in maps| E[Recompute maps<br/>from DB]:::new
E --> F[Run handler<br/>again]:::new
F -->|done| D
F -->|payload entity<br/>still missing| G[Skip: entity<br/>deleted]:::new
G --> D
F -->|nested entity<br/>still missing| H[Fail and<br/>retry]
classDef new fill:#d4f7d4,stroke:#2a7a2a
| Case | Before | After |
|---|---|---|
| Entity committed but not cached yet | Row deleted, no index or FK validation | Maps recomputed, index built, FK validated |
| Entity deleted before the job ran | Row deleted, skipped | Row deleted, skipped after one recompute |
| Entity cached | Runs | Runs |
Same change for validate_foreignKey, build_recordPermissionIndexes and rebuild_object, which had the same early return.
Repair
upgrade:2-67:repair-skipped-deferred-relation-operations is a workspace command that repairs the workspaces this bug already hit. It takes the non-unique, no-WHERE indexes whose fields are all many-to-one relations, plus the join columns of those relations. Then it queues:
build_indexfor every index thatto_regclasscannot findvalidate_foreignKeyfor everyNOT VALIDforeign key on those join columns
The worker builds and validates them off the upgrade path, with CONCURRENTLY.
Per workspace, the command runs two catalog lookups, scoped by name: to_regclass per index, and pg_constraint by conrelid. It never scans the catalog by schema. It skips workspaces that still have schema operations in flight; rerun it by name for those. On prod it should queue about 2,550 index builds and about 2,300 FK validations across 181 workspaces.
Not in this PR
- Making
create_many_object_metadatacreate objects one after another.