Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Index room_agents on agent_id (CHOO-3667)

Abel Dantas6c7ccab9a9e6
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8b9791612..4779b0e5f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -224,6 +224,9 @@ version of their own to them without also giving them a release of their own.
group whose subgroup would move up next to a namesake is refused too, and
the dashboard now shows why. Names can no longer contain `/` or be blank.
Existing duplicates are left as they are and can be reached by id.
+- **Reading an agent's rooms uses an index.** `room_agents` is indexed on
+ `agent_id`; reads by agent scanned the whole table, 2.9 ms each at 10,000
+ agents.
#### Fixed
- **On Slack, the 👀 and the activity card no longer arrive after the agent's
diff --git a/core/switch_core/db/models.py b/core/switch_core/db/models.py
index 70dff1e2c..807b1e746 100644
--- a/core/switch_core/db/models.py
+++ b/core/switch_core/db/models.py
@@ -886,6 +886,9 @@ room_agents = Table(
name="fk_room_agents_agent",
),
Index("ix_room_agents_tenant_id", "tenant_id"),
+ # The primary key leads with the room; reads of an agent's rooms, and the
+ # foreign key's check when an agent is deleted, go by the agent.
+ Index("ix_room_agents_agent_id", "agent_id"),
)
room_skills = Table(
diff --git a/core/switch_core/migrations/versions/063652bcb450_index_room_agents_agent_id.py b/core/switch_core/migrations/versions/063652bcb450_index_room_agents_agent_id.py
new file mode 100644
index 000000000..14d4c3a61
--- /dev/null
+++ b/core/switch_core/migrations/versions/063652bcb450_index_room_agents_agent_id.py
@@ -0,0 +1,44 @@
+"""index room_agents on agent_id
+
+Reading the rooms an agent is in (`RoomStore.get_rooms_for_agent` and six
+other reads by agent), and the check of `fk_room_agents_agent` when an agent
+is deleted, filter `room_agents` by agent. The primary key is (room_id,
+agent_id), whose leading column is the room, so none could use it and each
+was a sequential scan: 2.9 ms at 10,000 agents, and reading the rooms of a
+controller's agents, which every process does when a controller connects,
+ran it for each of them.
+
+Not `CONCURRENTLY`, for the reason `b8f2d0c41e57` gives: revisions run in
+a transaction, and the table is small on every deployment today. A deployment
+where it is not can build the index by hand first, and this becomes a no-op:
+
+ CREATE INDEX CONCURRENTLY ix_room_agents_agent_id ON room_agents (agent_id);
+
+Expand only: a process from before this revision reads the table as it did.
+
+Revision ID: 063652bcb450
+Revises: b7e1d4c9a2f6
+Create Date: 2026-11-01 00:00:00.000000
+
+"""
+
+from collections.abc import Sequence
+
+from alembic import op
+
+revision: str = "063652bcb450"
+down_revision: str | None = "b7e1d4c9a2f6"
+branch_labels: str | Sequence[str] | None = None
+depends_on: str | Sequence[str] | None = None
+expand_only: bool = True
+
+_INDEX = "ix_room_agents_agent_id"
+
+
+def upgrade() -> None:
+ # `IF NOT EXISTS` so an index built by hand ahead of the upgrade is kept.
+ op.execute(f"CREATE INDEX IF NOT EXISTS {_INDEX} ON room_agents (agent_id)")
+
+
+def downgrade() -> None:
+ op.execute(f"DROP INDEX IF EXISTS {_INDEX}")