From 300c32456e1de71b50ce612d1a13ea0bd75e280b Mon Sep 17 00:00:00 2001 From: Sarah Wooders Date: Sat, 4 Oct 2025 00:51:17 -0700 Subject: [PATCH] feat: add composite message index and reduce pool timeout (#5156) --- ...5fd2b_add_query_optimizing_runs_listing.py | 31 +++++++++++++++++++ letta/orm/message.py | 2 ++ letta/settings.py | 2 +- 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 alembic/versions/f9ad1c25fd2b_add_query_optimizing_runs_listing.py 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 00000000..d4a265b6 --- /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 e7d57eac..c7669646 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 86733139..28882ac1 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