Skip to content

fix(db): raise SQLAlchemy connection pool size to match real concurrency - #77

Open
mateusbellozupko wants to merge 2 commits into
evolution-foundation:mainfrom
mateusbellozupko:fix/db-pool-exhaustion
Open

mateusbellozupko wants to merge 2 commits into
evolution-foundation:mainfrom
mateusbellozupko:fix/db-pool-exhaustion

Conversation

@mateusbellozupko

@mateusbellozupko mateusbellozupko commented Sep 27, 2026 •

Copy link
Copy Markdown

Summary

  • create_engine(POSTGRES_CONNECTION_STRING, ...) never set pool_size/max_overflow, so it ran on SQLAlchemy's bare defaults (5 + 10 overflow = 15 total connections).
  • Under real load — many stage-inactivity AI calls held open for 30-90s each, plus live chat traffic — that pool exhausts and stays exhausted: every new request times out after 30s with QueuePool limit of size 5 overflow 10 reached, with no recovery until the process is restarted. Observed in production for 14+ hours, blocking every AI-generated automated message account-wide.
  • Makes pool_size/max_overflow configurable via DB_POOL_SIZE/DB_MAX_OVERFLOW, defaulting higher (20/40) than the library defaults that exhausted.

Test plan

  • Added tests/unit/test_database_pool_config.py: the engine's pool size/overflow are configurable via env vars, and default higher than SQLAlchemy's bare defaults.

🤖 Generated with Claude Code

Summary by Sourcery

Configure a larger, environment-controlled database connection pool to sustain concurrent AI and chat workloads without exhausting available connections.

Bug Fixes:

  • Increase database connection pool capacity to prevent pool exhaustion under production concurrency.

Enhancements:

  • Make SQLAlchemy pool size and overflow configurable through environment variables, with higher service-specific defaults and guidance for multi-process connection limits.

Tests:

  • Add unit coverage for configurable pool settings and defaults above SQLAlchemy's bare limits.

engine = create_engine(POSTGRES_CONNECTION_STRING, ...) never set
pool_size/max_overflow, so it ran on SQLAlchemy's bare defaults
(5 + 10 overflow = 15 total connections). Under normal load — many
stage-inactivity AI calls held open for 30-90s each, plus live chat
traffic — that pool exhausts and stays exhausted: every new request
times out after 30s with "QueuePool limit of size 5 overflow 10
reached", with no recovery until the process is restarted. Observed
in production for 14+ hours, blocking every AI-generated automated
message account-wide.

Make pool_size/max_overflow configurable via DB_POOL_SIZE/
DB_MAX_OVERFLOW, defaulting higher (20/40) than the library defaults
that exhausted.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@sourcery-ai

sourcery-ai Bot commented Sep 27, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

The PR addresses production pool exhaustion by configuring SQLAlchemy with larger, environment-controlled pool and overflow limits, defaulting to 20 and 40 instead of 5 and 10, and adds tests covering both overrides and safer defaults.

File-Level Changes

Change Details Files
Increase the database connection pool capacity and expose both limits through environment-backed settings.
  • Add DB_POOL_SIZE and DB_MAX_OVERFLOW settings with defaults of 20 and 40.
  • Pass the configured values to SQLAlchemy's engine creation alongside existing pool health settings.
src/config/database.py
src/config/settings.py
Add regression coverage for configurable and non-default pool limits.
  • Reload settings and the database engine under controlled environment variables.
  • Assert explicit values are applied and defaults exceed SQLAlchemy's bare limits.
tests/unit/test_database_pool_config.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/config/database.py" line_range="50-51" />
<code_context>
     POSTGRES_CONNECTION_STRING,
     pool_pre_ping=True,
     pool_recycle=1800,
+    pool_size=settings.DB_POOL_SIZE,
+    max_overflow=settings.DB_MAX_OVERFLOW,
     connect_args={
         "keepalives": 1,
</code_context>
<issue_to_address>
**issue (broader_impact):** The default pool configuration permits up to 60 PostgreSQL connections per process (20 pooled connections plus 40 overflow connections). With multiple Uvicorn workers or service replicas, the aggregate limit exceeds PostgreSQL's default `max_connections` and causes connection attempts to fail once the database-wide limit is reached.

**Triggers:** When the service runs with multiple workers/replicas or shares PostgreSQL with other services.

**Suggested fix:** Set a deployment-wide connection budget and size each process's pool accordingly, or explicitly raise/configure PostgreSQL's connection limit alongside these settings.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 1 finding to address first, and the new defaults change how many database connections the service can open, potentially overloading Postgres or causing connection exhaustion and an outage under real concurrency. Reverting restores the previous limits, but any outage or failed requests that already occurred cannot be undone.

Blocking findings: src/config/database.py:51


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src/config/database.py
…ployments (Sourcery review)

Sourcery's review on the public PR flagged that DB_POOL_SIZE/
DB_MAX_OVERFLOW are per-process: with multiple uvicorn workers or
service replicas, the aggregate connection count can exceed
Postgres's own max_connections shared across every service on that
instance. The defaults (20/40) are correct for this deployment's
single-process topology (no --workers flag) and were chosen from a
real production incident, so they aren't being lowered defensively
without evidence -- documenting the tradeoff so it's sized correctly
before anyone scales workers/replicas.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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