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.

feat: add composite message index and reduce pool timeout (#5156)

Sarah Wooders300c32456e1d
diff --git a/alembic/versions/f9ad1c25fd2b_add_query_optimizing_runs_listing.py b/alembic/versions/f9ad1c25fd2b_add_query_optimizing_runs_listing.py
new file mode 100644
index 000000000..d4a265b62
--- /dev/null
+++ b/alembic/versions/f9ad1c25fd2b_add_query_optimizing_runs_listing.py
@@ -0,0 +1,31 @@
+"""add query optimizing runs listing
+
+Revision ID: f9ad1c25fd2b
+Revises: 3bc3c031fbe4
+Create Date: 2025-10-04 00:44:06.663817
+
+"""
+
+from typing import Sequence, Union
+
+import sqlalchemy as sa
+
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision: str = "f9ad1c25fd2b"
+down_revision: Union[str, None] = "3bc3c031fbe4"
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def upgrade() -> None:
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.create_index("ix_messages_run_err_sequence", "messages", ["run_id", "is_err", "sequence_id"], unique=False)
+ # ### end Alembic commands ###
+
+
+def downgrade() -> None:
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.drop_index("ix_messages_run_err_sequence", table_name="messages")
+ # ### end Alembic commands ###
diff --git a/letta/orm/message.py b/letta/orm/message.py
index e7d57eacf..c76696462 100644
--- a/letta/orm/message.py
+++ b/letta/orm/message.py
@@ -22,6 +22,8 @@ class Message(SqlalchemyBase, OrganizationMixin, AgentMixin):
Index("ix_messages_agent_sequence", "agent_id", "sequence_id"),
Index("ix_messages_org_agent", "organization_id", "agent_id"),
Index("ix_messages_run_id", "run_id"),
+ # Composite index for optimizing the frequently-run query:
+ Index("ix_messages_run_err_sequence", "run_id", "is_err", "sequence_id"),
)
__pydantic_model__ = PydanticMessage
diff --git a/letta/settings.py b/letta/settings.py
index 867331397..28882ac1e 100644
--- a/letta/settings.py
+++ b/letta/settings.py
@@ -236,7 +236,7 @@ class Settings(BaseSettings):
pg_uri: Optional[str] = default_pg_uri # option to specify full uri
pg_pool_size: int = 25 # Concurrent connections
pg_max_overflow: int = 10 # Overflow limit
- pg_pool_timeout: int = 90 # Seconds to wait for a connection
+ pg_pool_timeout: int = 30 # Seconds to wait for a connection
pg_pool_recycle: int = 1800 # When to recycle connections
pg_echo: bool = False # Logging
pool_pre_ping: bool = True # Pre ping to check for dead connections