@@ -1511,8 +1511,8 @@ class ToolMetric(Base):
15111511 __tablename__ = "tool_metrics"
15121512
15131513 id : Mapped [int ] = mapped_column (primary_key = True )
1514- tool_id : Mapped [str ] = mapped_column (String (36 ), ForeignKey ("tools.id" ), nullable = False )
1515- timestamp : Mapped [datetime ] = mapped_column (DateTime (timezone = True ), default = utc_now )
1514+ tool_id : Mapped [str ] = mapped_column (String (36 ), ForeignKey ("tools.id" ), nullable = False , index = True )
1515+ timestamp : Mapped [datetime ] = mapped_column (DateTime (timezone = True ), default = utc_now , index = True )
15161516 response_time : Mapped [float ] = mapped_column (Float , nullable = False )
15171517 is_success : Mapped [bool ] = mapped_column (Boolean , nullable = False )
15181518 error_message : Mapped [Optional [str ]] = mapped_column (Text , nullable = True )
@@ -1537,8 +1537,8 @@ class ResourceMetric(Base):
15371537 __tablename__ = "resource_metrics"
15381538
15391539 id : Mapped [int ] = mapped_column (primary_key = True )
1540- resource_id : Mapped [str ] = mapped_column (String (36 ), ForeignKey ("resources.id" ), nullable = False )
1541- timestamp : Mapped [datetime ] = mapped_column (DateTime (timezone = True ), default = utc_now )
1540+ resource_id : Mapped [str ] = mapped_column (String (36 ), ForeignKey ("resources.id" ), nullable = False , index = True )
1541+ timestamp : Mapped [datetime ] = mapped_column (DateTime (timezone = True ), default = utc_now , index = True )
15421542 response_time : Mapped [float ] = mapped_column (Float , nullable = False )
15431543 is_success : Mapped [bool ] = mapped_column (Boolean , nullable = False )
15441544 error_message : Mapped [Optional [str ]] = mapped_column (Text , nullable = True )
@@ -1563,8 +1563,8 @@ class ServerMetric(Base):
15631563 __tablename__ = "server_metrics"
15641564
15651565 id : Mapped [int ] = mapped_column (primary_key = True )
1566- server_id : Mapped [str ] = mapped_column (String (36 ), ForeignKey ("servers.id" ), nullable = False )
1567- timestamp : Mapped [datetime ] = mapped_column (DateTime (timezone = True ), default = utc_now )
1566+ server_id : Mapped [str ] = mapped_column (String (36 ), ForeignKey ("servers.id" ), nullable = False , index = True )
1567+ timestamp : Mapped [datetime ] = mapped_column (DateTime (timezone = True ), default = utc_now , index = True )
15681568 response_time : Mapped [float ] = mapped_column (Float , nullable = False )
15691569 is_success : Mapped [bool ] = mapped_column (Boolean , nullable = False )
15701570 error_message : Mapped [Optional [str ]] = mapped_column (Text , nullable = True )
@@ -1589,8 +1589,8 @@ class PromptMetric(Base):
15891589 __tablename__ = "prompt_metrics"
15901590
15911591 id : Mapped [int ] = mapped_column (primary_key = True )
1592- prompt_id : Mapped [str ] = mapped_column (String (36 ), ForeignKey ("prompts.id" ), nullable = False )
1593- timestamp : Mapped [datetime ] = mapped_column (DateTime (timezone = True ), default = utc_now )
1592+ prompt_id : Mapped [str ] = mapped_column (String (36 ), ForeignKey ("prompts.id" ), nullable = False , index = True )
1593+ timestamp : Mapped [datetime ] = mapped_column (DateTime (timezone = True ), default = utc_now , index = True )
15941594 response_time : Mapped [float ] = mapped_column (Float , nullable = False )
15951595 is_success : Mapped [bool ] = mapped_column (Boolean , nullable = False )
15961596 error_message : Mapped [Optional [str ]] = mapped_column (Text , nullable = True )
@@ -1616,8 +1616,8 @@ class A2AAgentMetric(Base):
16161616 __tablename__ = "a2a_agent_metrics"
16171617
16181618 id : Mapped [int ] = mapped_column (primary_key = True )
1619- a2a_agent_id : Mapped [str ] = mapped_column (String (36 ), ForeignKey ("a2a_agents.id" ), nullable = False )
1620- timestamp : Mapped [datetime ] = mapped_column (DateTime (timezone = True ), default = utc_now )
1619+ a2a_agent_id : Mapped [str ] = mapped_column (String (36 ), ForeignKey ("a2a_agents.id" ), nullable = False , index = True )
1620+ timestamp : Mapped [datetime ] = mapped_column (DateTime (timezone = True ), default = utc_now , index = True )
16211621 response_time : Mapped [float ] = mapped_column (Float , nullable = False )
16221622 is_success : Mapped [bool ] = mapped_column (Boolean , nullable = False )
16231623 error_message : Mapped [Optional [str ]] = mapped_column (Text , nullable = True )
@@ -3898,8 +3898,12 @@ def fresh_db_session() -> Generator[Session, Any, None]:
38983898 such as for metrics recording after releasing the main session.
38993899
39003900 This is a synchronous context manager that creates a new database session
3901- from the SessionLocal factory. The session is automatically closed when
3902- the context manager exits, regardless of whether an exception occurred.
3901+ from the SessionLocal factory. The session is automatically committed on
3902+ successful exit or rolled back on exception, then closed.
3903+
3904+ Note: Prior to this fix, sessions were closed without commit, causing
3905+ PostgreSQL to implicitly rollback all transactions (even read-only SELECTs).
3906+ This was causing ~40% rollback rate under load.
39033907
39043908 Yields:
39053909 Session: A fresh SQLAlchemy database session.
@@ -3913,6 +3917,10 @@ def fresh_db_session() -> Generator[Session, Any, None]:
39133917 db = SessionLocal ()
39143918 try :
39153919 yield db
3920+ db .commit () # Commit on successful exit (even for read-only operations)
3921+ except Exception :
3922+ db .rollback () # Explicit rollback on exception
3923+ raise
39163924 finally :
39173925 db .close ()
39183926
0 commit comments