Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

perf: add concurrent indexes for messages listing query (#9766)

Kian Jones3527e962efb7
diff --git a/alembic/versions/45402909a46b_add_concurrent_indexes_for_messages_.py b/alembic/versions/45402909a46b_add_concurrent_indexes_for_messages_.py
new file mode 100644
index 000000000..00651bafc
--- /dev/null
+++ b/alembic/versions/45402909a46b_add_concurrent_indexes_for_messages_.py
@@ -0,0 +1,43 @@
+"""add concurrent indexes for messages listing
+
+Revision ID: 45402909a46b
+Revises: b2c3d4e5f6a8
+Create Date: 2026-03-03 21:23:50.802684
+
+"""
+
+from typing import Sequence, Union
+
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision: str = "45402909a46b"
+down_revision: Union[str, None] = "b2c3d4e5f6a8"
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def upgrade() -> None:
+ connection = op.get_bind()
+ connection.commit()
+ autocommit_connection = connection.execution_options(isolation_level="AUTOCOMMIT")
+ autocommit_connection.exec_driver_sql(
+ """
+ CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_messages_on_updated_at
+ ON messages USING btree (updated_at)
+ """
+ )
+ autocommit_connection.exec_driver_sql(
+ """
+ CREATE INDEX CONCURRENTLY IF NOT EXISTS ix_messages_agent_conversation_sequence
+ ON messages USING btree (agent_id, conversation_id, sequence_id)
+ """
+ )
+
+
+def downgrade() -> None:
+ connection = op.get_bind()
+ connection.commit()
+ autocommit_connection = connection.execution_options(isolation_level="AUTOCOMMIT")
+ autocommit_connection.exec_driver_sql("DROP INDEX CONCURRENTLY IF EXISTS ix_messages_agent_conversation_sequence")
+ autocommit_connection.exec_driver_sql("DROP INDEX CONCURRENTLY IF EXISTS idx_messages_on_updated_at")
diff --git a/letta/orm/message.py b/letta/orm/message.py
index 10b6b562b..cfcf04814 100644
--- a/letta/orm/message.py
+++ b/letta/orm/message.py
@@ -25,7 +25,9 @@ class Message(SqlalchemyBase, OrganizationMixin, AgentMixin):
__tablename__ = "messages"
__table_args__ = (
+ Index("idx_messages_on_updated_at", "updated_at"),
Index("ix_messages_agent_created_at", "agent_id", "created_at"),
+ Index("ix_messages_agent_conversation_sequence", "agent_id", "conversation_id", "sequence_id"),
Index("ix_messages_created_at", "created_at", "id"),
Index("ix_messages_agent_sequence", "agent_id", "sequence_id"),
Index("ix_messages_org_agent", "organization_id", "agent_id"),