Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

perf(db): index autopilot task recovery lookup (#8247)

Multica Evecd49d2b471c0
diff --git a/server/cmd/migrate/main.go b/server/cmd/migrate/main.go
index 0b51f6590d..1a2ffcc2e6 100644
--- a/server/cmd/migrate/main.go
+++ b/server/cmd/migrate/main.go
@@ -301,6 +301,7 @@ var concurrentIndexCleanups = map[string]string{
"452_agent_task_pending_thread_unique": "idx_one_pending_task_per_issue_agent_thread",
"458_activity_log_member_assignee_frequency_index": "idx_activity_log_member_assignee_frequency",
"459_chat_message_assistant_task_index": "idx_chat_message_assistant_task",
+ "460_agent_task_queue_autopilot_run_created_at_index": "idx_agent_task_queue_autopilot_run_created_at",
}
// concurrentDownIndexCleanups covers every migration whose down direction
diff --git a/server/migrations/460_agent_task_queue_autopilot_run_created_at_index.down.sql b/server/migrations/460_agent_task_queue_autopilot_run_created_at_index.down.sql
new file mode 100644
index 0000000000..78c2609f1c
--- /dev/null
+++ b/server/migrations/460_agent_task_queue_autopilot_run_created_at_index.down.sql
@@ -0,0 +1 @@
+DROP INDEX CONCURRENTLY IF EXISTS idx_agent_task_queue_autopilot_run_created_at;
diff --git a/server/migrations/460_agent_task_queue_autopilot_run_created_at_index.up.sql b/server/migrations/460_agent_task_queue_autopilot_run_created_at_index.up.sql
new file mode 100644
index 0000000000..2347c89ae5
--- /dev/null
+++ b/server/migrations/460_agent_task_queue_autopilot_run_created_at_index.up.sql
@@ -0,0 +1,11 @@
+-- Single statement: CREATE INDEX CONCURRENTLY cannot run inside a transaction
+-- or share a multi-command migration file.
+--
+-- GetAutopilotTaskByRun recovers run_only dispatches whose task INSERT committed
+-- before autopilot_run.task_id was linked. Without an autopilot_run_id-leading
+-- index, finding the earliest task scans and sorts the entire task queue.
+-- Automatic retries inherit the same autopilot_run_id, so this index must remain
+-- non-unique. The partial predicate avoids indexing ordinary non-Autopilot tasks.
+CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_agent_task_queue_autopilot_run_created_at
+ ON agent_task_queue (autopilot_run_id, created_at)
+ WHERE autopilot_run_id IS NOT NULL;