Backfill a status row for work items that have none
What does this MR do and why?
Most work items have no row in work_item_current_statuses. When that happens, the status is worked out at read time: an open item counts as the lifecycle's default open status, a closed item as the default closed status, and a closed item marked duplicate as the default duplicate status.
This causes a problem. When a board or list filters by one of those default statuses, for example "Start" or "New" in gitlab-org, the query has to add an OR branch for "items with no row." No index can serve that branch, so the query reads every item in the group. This is the main cause of the timeouts described in https://gitlab.com/gitlab-org/gitlab/-/work_items/602041.
This MR adds a batched background migration, BackfillMissingWorkItemCurrentStatuses, over the issues table. It runs in batches of 10,000 with sub-batches of 100. For every item that has no status row, and whose top-level group has a custom lifecycle for that item's type, it inserts a row pointing at the lifecycle's default status for the item's state. updated_at is set to the item's created_at, the same value the read-time fallback uses today. Items in groups without a custom lifecycle are skipped. The insert uses ON CONFLICT (work_item_id) DO NOTHING, so the migration can run again safely and will not fight with users who are changing statuses.
This matters because, after this migration and !255857 (merged) (which moves rows still on built-in status ids to the custom ones), every item in a converted group will have exactly one row, on the indexed custom_status_id column. A follow-up change can then drop the "no row" branch and the "built-in id" branch from the filter query and use the index directly.
That follow-up is a small change, behind a feature flag plus a check that this migration has finished. Some statuses are still very dense, with tens of thousands of items in one status at the group level, so an exact count stays expensive there. The follow-up will also cap counts at the group level above a threshold chosen from production data. Project-level counts already render fine and need no cap.
This migration covers whatever groups have a custom lifecycle when it runs. Required stops are 19.5 and 19.8. It is enqueued in 19.5 and finalizable in 19.6.
Query plan
One sub-batch of the migration, 100 issue ids in gitlab-org, 22 rows inserted: 375 ms, about 2,300 buffers. Plan
INSERT INTO work_item_current_statuses (work_item_id, namespace_id, custom_status_id, updated_at)
SELECT issues.id,
issues.namespace_id,
CASE WHEN issues.state_id = 1 THEN lifecycles.default_open_status_id
WHEN issues.duplicated_to_id IS NOT NULL THEN lifecycles.default_duplicate_status_id
ELSE lifecycles.default_closed_status_id
END,
issues.created_at
FROM issues
JOIN namespaces ON namespaces.id = issues.namespace_id
JOIN work_item_type_custom_lifecycles AS type_lifecycles
ON type_lifecycles.namespace_id = namespaces.traversal_ids[1]
AND type_lifecycles.work_item_type_id = issues.work_item_type_id
JOIN work_item_custom_lifecycles AS lifecycles ON lifecycles.id = type_lifecycles.lifecycle_id
LEFT JOIN work_item_current_statuses AS existing ON existing.work_item_id = issues.id
WHERE issues.id IN (...sub-batch...)
AND existing.id IS NULL
ON CONFLICT (work_item_id) DO NOTHING;Effect on the status filter
gitlab-org, before and after the backfill (the after plans drop the two OR branches):
| Filter | Before | After |
|---|---|---|
| "Start" status count | 386,826 buffers, 70 s (plan) | 8,738 buffers, 1.2 s (plan) |
| "New" status count | 2.41M buffers, 4.6 min (plan) | 1.30M buffers, 32 s cold, about 2.5 s warm (plan) |
"New" is one of the dense statuses. It is why the follow-up caps group-level counts.
References
- Closes #629280 (closed)
- Epic: &23612 (closed)
- Related: !255857 (merged) (rows still on a built-in id), !257785 (merged) (conversion-time rewrite)
Screenshots or screen recordings
Not applicable. Database migration only.
How to set up and validate locally
- Run the specs:
spec/migrations/20261005100000_queue_backfill_missing_work_item_current_statuses_spec.rbandee/spec/lib/ee/gitlab/background_migration/backfill_missing_work_item_current_statuses_spec.rb. - In
rails console, pick a group that has a custom lifecycle and an open issue in it with no status row:group = Group.find_by_full_path('<path>') issue = WorkItem.where('namespace_traversal_ids[1] = ?', group.id).without_current_status.first - Run the job for that id:
Gitlab::BackgroundMigration::BackfillMissingWorkItemCurrentStatuses.new( start_cursor: [issue.id], end_cursor: [issue.id], batch_table: :issues, batch_column: :id, sub_batch_size: 100, pause_ms: 0, connection: ApplicationRecord.connection ).perform - Confirm
issue.reload.current_statusnow points at the lifecycle's default open status and thatupdated_atequals the issue'screated_at. The status shown in the UI is unchanged.