Skip to content

Commit e79bcab

Browse files
authored
Db index (IBM#1716)
* DB Indexing Signed-off-by: Mihai Criveti <crivetimihai@gmail.com> * DB Indexing Signed-off-by: Mihai Criveti <crivetimihai@gmail.com> --------- Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
1 parent 259b5e3 commit e79bcab

2 files changed

Lines changed: 43 additions & 12 deletions

File tree

docker-compose.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,29 @@ services:
265265
- "max_parallel_workers_per_gather=2"
266266
- "-c"
267267
- "max_parallel_workers=4"
268+
# === ROLLBACK DEBUGGING ===
269+
- "-c"
270+
- "log_min_error_statement=error"
271+
- "-c"
272+
- "log_min_messages=warning"
273+
- "-c"
274+
- "log_error_verbosity=verbose"
275+
- "-c"
276+
- "log_line_prefix=%t [%p]: user=%u,db=%d,app=%a,client=%h "
277+
- "-c"
278+
- "log_lock_waits=on"
279+
- "-c"
280+
- "deadlock_timeout=1s"
281+
- "-c"
282+
- "log_temp_files=0"
283+
- "-c"
284+
- "log_checkpoints=on"
285+
- "-c"
286+
- "log_connections=on"
287+
- "-c"
288+
- "log_disconnections=on"
289+
- "-c"
290+
- "idle_in_transaction_session_timeout=60s"
268291
environment:
269292
- POSTGRES_USER=postgres
270293
- POSTGRES_PASSWORD=mysecretpassword

mcpgateway/db.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)