fix(db): Set db statement timeout of 90 seconds by TheodoreSpeaks · Pull Request #4276 · simstudioai/sim · GitHub
Skip to content

fix(db): Set db statement timeout of 90 seconds#4276

Merged
TheodoreSpeaks merged 2 commits intostagingfrom
fix/billing-lock-hold
Apr 23, 2026
Merged

fix(db): Set db statement timeout of 90 seconds#4276
TheodoreSpeaks merged 2 commits intostagingfrom
fix/billing-lock-hold

Conversation

@TheodoreSpeaks
Copy link
Copy Markdown
Collaborator

Summary

We've seen db queries with locks of over 70 minutes. We should set a maximum transaction limit to prevent hogging db resources.

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Other: ___________

Testing

  • Db setting only, shouldn't affect any behavior unless things are real wrong.

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

Screenshots/Videos

@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 23, 2026

@TheodoreSpeaks
Copy link
Copy Markdown
Collaborator Author

@BugBot review

@cursor
Copy link
Copy Markdown

cursor Bot commented Apr 23, 2026

PR Summary

Medium Risk
Applies server-side Postgres timeouts to all drizzle postgres clients, which could surface as new failures for legitimately long-running queries or transactions.

Overview
Adds a server-side safety cap on database usage by configuring Postgres connection options to enforce statement_timeout=90000 and idle_in_transaction_session_timeout=90000.

This is applied to both the shared packages/db client and the socket server’s DB client (apps/sim/socket/database/operations.ts), with comments noting that scripts needing longer limits must use a separately configured client.

Reviewed by Cursor Bugbot for commit f764497. Bugbot is set up for automated code reviews on this repo. Configure here.

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit f764497. Configure here.

@TheodoreSpeaks
Copy link
Copy Markdown
Collaborator Author

@BugBot review

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit f764497. Configure here.

@TheodoreSpeaks TheodoreSpeaks changed the title Set statement timeout of 90 seconds fix(db): Set db statement timeout of 90 seconds Apr 23, 2026
@TheodoreSpeaks TheodoreSpeaks marked this pull request as ready for review April 23, 2026 22:18
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 23, 2026

Greptile Summary

This PR adds PostgreSQL session-level statement_timeout (90 s) and idle_in_transaction_session_timeout (90 s) to both the primary DB client (packages/db/index.ts) and the realtime socket DB client (apps/realtime/src/database/operations.ts), as a last-resort guard against the runaway 70-minute lock-holding queries observed in production. The implementation is correct: 90000 is in milliseconds per PostgreSQL spec, the connection.options field in postgres.js is the right mechanism for setting server-side GUCs per session, and the code comment clearly notes that migrations needing longer limits must construct their own client.

Confidence Score: 5/5

Safe to merge — change is narrowly scoped, values are correct, and existing error handling propagates timeout errors appropriately.

Both timeout values (90000 ms = 90 s) are correct per PostgreSQL spec, the postgres.js connection.options API is used correctly, and the inline comment already calls out the migration override requirement. No logic changes; all remaining observations are P2 or lower.

No files require special attention.

Important Files Changed

Filename Overview
packages/db/index.ts Adds statement_timeout=90000 and idle_in_transaction_session_timeout=90000 via connection.options; values and API usage are correct.
apps/realtime/src/database/operations.ts Mirrors the same session timeout settings for the socket DB connection; comment references the rationale in packages/db/index.ts.

Sequence Diagram

sequenceDiagram
    participant App as Application Code
    participant Client as postgres.js Client
    participant PG as PostgreSQL

    App->>Client: new postgres(connectionString, options)
    Client->>PG: Startup with statement_timeout=90000ms and idle_in_transaction_session_timeout=90000ms
    PG-->>Client: Session GUCs set

    App->>PG: BEGIN transaction
    alt Statement runs longer than 90s
        PG-->>App: ERROR 57014 query_canceled (statement_timeout)
    else Transaction idle longer than 90s
        PG-->>App: ERROR 57P05 idle_in_transaction_session_timeout
    else Normal execution
        PG-->>App: Result set
        App->>PG: COMMIT
    end
Loading

Reviews (1): Last reviewed commit: "Merge remote-tracking branch 'origin/sta..." | Re-trigger Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant