Skip to content

fix: reuse one database pool per Database - #146

Merged
prasadlohakpure merged 1 commit into
mainfrom
fix/reuse-db-connection-pool
Sep 29, 2026
Merged

prasadlohakpure merged 1 commit into
mainfrom
fix/reuse-db-connection-pool

Conversation

@prasadlohakpure

@prasadlohakpure prasadlohakpure commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Sessions were calling sql.Open and Close on every database call, so each session owned its own pool and Postgres ran out of connections (janitor.worker.error.new_session).
  • One *sql.DB is now opened per Database and reused. Session close only rolls back an uncommitted transaction.
  • Queries run on the session transaction when one is open, so FOR UPDATE locks stay held until commit. Pool size comes from max_open_conns and max_idle_conns (defaults 25 and 10).

Testing

  1. Before testing started
image
  1. When trying to bombard with 10 concurrent requests, the idle connection never grew beyond 10 (as per maxIdleConn config)
image

Test plan

  • go test ./internal/pkg/database/
  • Deploy with database.max_open_conns and database.max_idle_conns set, and confirm janitor new_session errors stop
  • Confirm async job claim and janitor stale-job cleanup still commit (status updates and active_jobs deletes)
  • Confirm cancel and command/cluster status updates still report rows affected

Copilot AI lite review requested due to automatic review settings September 28, 2026 18:44
@wiz-55ccc8b716

wiz-55ccc8b716 Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Wiz Scan Summary

Scanner Findings
Vulnerability Finding Vulnerabilities -
Data Finding Sensitive Data -
Secret Finding Secrets 1 Low
IaC Misconfiguration IaC Misconfigurations -
SAST Finding SAST Findings -
Software Management Finding Software Management Findings -
Total 1 Low

View scan details in Wiz

To detect these findings earlier in the dev lifecycle, try the Wiz Code extension for VS Code, JetBrains, or Visual Studio.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Opening a new pool per session exhausted Postgres. One *sql.DB is shared per Database, and Query and QueryRow use the open transaction so FOR UPDATE locks last until commit.
@prasadlohakpure
prasadlohakpure force-pushed the fix/reuse-db-connection-pool branch from 5589f3c to 7e69cd9 Compare September 29, 2026 04:27
@prasadlohakpure

Copy link
Copy Markdown
Collaborator Author

Wiz check is failing due to changes in config file which has test creds. This check is false positive and can be ignored.

@prasadlohakpure
prasadlohakpure merged commit 05dd99d into main Sep 29, 2026
6 of 7 checks passed
@prasadlohakpure
prasadlohakpure deleted the fix/reuse-db-connection-pool branch September 29, 2026 04:44
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.

3 participants