Skip to content

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

Go

MUL-7433: index high-volume lookup paths (#8479)

Multica Eveb0d44c7cfa42
diff --git a/server/cmd/migrate/main.go b/server/cmd/migrate/main.go
index e6a576e874..cfd5ea14c9 100644
--- a/server/cmd/migrate/main.go
+++ b/server/cmd/migrate/main.go
@@ -140,6 +140,10 @@ var pgBigmOperatorClass = extensionOperatorClass{
// they are still pending: a fresh self-hosted install, which is exactly where an
// interrupted build would otherwise leave a permanently unusable index.
var concurrentIndexCleanups = map[string]string{
+ "495_issue_to_label_label_id_index": "issue_to_label_label_idx",
+ "496_chat_session_agent_id_index": "idx_chat_session_agent_id",
+ "497_agent_task_queue_delegated_failure_evidence_index": "idx_agent_task_queue_delegated_failure_evidence",
+ "498_chat_session_runtime_id_index": "idx_chat_session_runtime_id",
"486_maintenance_job_id_index": "idx_maintenance_job_id",
"487_maintenance_job_idempotency_index": "idx_maintenance_job_idempotency",
"488_maintenance_job_active_index": "idx_maintenance_job_active",
diff --git a/server/migrations/495_issue_to_label_label_id_index.down.sql b/server/migrations/495_issue_to_label_label_id_index.down.sql
new file mode 100644
index 0000000000..6d1e52ae5d
--- /dev/null
+++ b/server/migrations/495_issue_to_label_label_id_index.down.sql
@@ -0,0 +1 @@
+DROP INDEX CONCURRENTLY IF EXISTS issue_to_label_label_idx;
diff --git a/server/migrations/495_issue_to_label_label_id_index.up.sql b/server/migrations/495_issue_to_label_label_id_index.up.sql
new file mode 100644
index 0000000000..cea15e2526
--- /dev/null
+++ b/server/migrations/495_issue_to_label_label_id_index.up.sql
@@ -0,0 +1,5 @@
+-- ListLabels counts assignments once per issue label, and label deletion also
+-- looks up rows by label_id. The primary key starts with issue_id, so it cannot
+-- selectively serve either path as the junction table grows.
+CREATE INDEX CONCURRENTLY IF NOT EXISTS issue_to_label_label_idx
+ ON issue_to_label (label_id);
diff --git a/server/migrations/496_chat_session_agent_id_index.down.sql b/server/migrations/496_chat_session_agent_id_index.down.sql
new file mode 100644
index 0000000000..6c7ada62b9
--- /dev/null
+++ b/server/migrations/496_chat_session_agent_id_index.down.sql
@@ -0,0 +1 @@
+DROP INDEX CONCURRENTLY IF EXISTS idx_chat_session_agent_id;
diff --git a/server/migrations/496_chat_session_agent_id_index.up.sql b/server/migrations/496_chat_session_agent_id_index.up.sql
new file mode 100644
index 0000000000..cf1fdea26b
--- /dev/null
+++ b/server/migrations/496_chat_session_agent_id_index.up.sql
@@ -0,0 +1,6 @@
+-- Runtime teardown finds and locks every chat session owned by a system agent
+-- before deleting that agent. The same column backs the ON DELETE CASCADE
+-- foreign key, so an unindexed lookup extends the teardown transaction and its
+-- chat-session lock window with a full-table scan.
+CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_chat_session_agent_id
+ ON chat_session (agent_id);
diff --git a/server/migrations/497_agent_task_queue_delegated_failure_evidence_index.down.sql b/server/migrations/497_agent_task_queue_delegated_failure_evidence_index.down.sql
new file mode 100644
index 0000000000..592fbbaeb2
--- /dev/null
+++ b/server/migrations/497_agent_task_queue_delegated_failure_evidence_index.down.sql
@@ -0,0 +1 @@
+DROP INDEX CONCURRENTLY IF EXISTS idx_agent_task_queue_delegated_failure_evidence;
diff --git a/server/migrations/497_agent_task_queue_delegated_failure_evidence_index.up.sql b/server/migrations/497_agent_task_queue_delegated_failure_evidence_index.up.sql
new file mode 100644
index 0000000000..ce0d3176cd
--- /dev/null
+++ b/server/migrations/497_agent_task_queue_delegated_failure_evidence_index.up.sql
@@ -0,0 +1,6 @@
+-- Delegated-failure recovery repeatedly counts attempts for one failed task and
+-- selects its newest attempt by created_at/id. Keep this partial because
+-- agent_task_queue is write-heavy and only recovery tasks need the lookup.
+CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_agent_task_queue_delegated_failure_evidence
+ ON agent_task_queue (trigger_evidence_ref_id, created_at DESC, id DESC)
+ WHERE trigger_evidence_kind = 'delegated_failure';
diff --git a/server/migrations/498_chat_session_runtime_id_index.down.sql b/server/migrations/498_chat_session_runtime_id_index.down.sql
new file mode 100644
index 0000000000..66dce2e589
--- /dev/null
+++ b/server/migrations/498_chat_session_runtime_id_index.down.sql
@@ -0,0 +1 @@
+DROP INDEX CONCURRENTLY IF EXISTS idx_chat_session_runtime_id;
diff --git a/server/migrations/498_chat_session_runtime_id_index.up.sql b/server/migrations/498_chat_session_runtime_id_index.up.sql
new file mode 100644
index 0000000000..ea41290aa9
--- /dev/null
+++ b/server/migrations/498_chat_session_runtime_id_index.up.sql
@@ -0,0 +1,6 @@
+-- Deleting a runtime clears chat-session resume pointers through the runtime_id
+-- foreign key. Equality to the deleted non-NULL runtime implies this predicate,
+-- so the partial index serves the FK action without indexing unbound sessions.
+CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_chat_session_runtime_id
+ ON chat_session (runtime_id)
+ WHERE runtime_id IS NOT NULL;