Skip to content

fix(db): restore elastic postgres pool - #583

Closed
izadoesdev wants to merge 2 commits into
mainfrom
codex/fix-production-postgres-pool
Closed

fix(db): restore elastic postgres pool#583
izadoesdev wants to merge 2 commits into
mainfrom
codex/fix-production-postgres-pool

Conversation

@izadoesdev

@izadoesdev izadoesdev commented Aug 1, 2026

Copy link
Copy Markdown
Member

Summary

  • Restore the elastic pg.Pool in the shared PostgreSQL client.
  • Keep the pool lazy/dynamic (min remains 0), cap each replica at 50 connections, close idle clients after 30 seconds, and bound acquisition at 10 seconds.
  • Preserve PgBouncer-compatible URL normalization, query timing, error handling, warmup, and graceful shutdown.

Production evidence

  • PostgreSQL/PgBouncer itself is not saturated: PgBouncer was 23/400, direct PostgreSQL 10/50, and database CPU was 8%.
  • The current production runtime emits Calling client.query() when the client is already executing a query, proving concurrent requests are sharing one pg.Client.
  • A 39-request /links/create burst from one API key produced p95 6.49s latency and 28 cache-command timeouts; every slow request had one PostgreSQL query and 0 HTTP 5xx.
  • Staging already runs this exact pool implementation with clean health checks and no PostgreSQL acquisition errors.

Validation

  • bun run lint
  • bun run check-types (34/34 tasks)
  • bun run test (25/25 tasks; 3,905 AI-package tests passed, 0 failed)
  • Pre-push test hook passed
  • Independent focused review requested before merge

Rollout and rollback

  • After merge, verify production health plus Axiom error rate, PostgreSQL timing, and /links/create p95 under the next real burst.
  • Rollback is limited to reverting these two commits if pool acquisition or connection counts regress.

Issue

Emergency maintainer production-incident hotfix; no public issue contains the operational details.

AI disclosure

OpenAI Codex assisted with incident investigation, the minimal cherry-pick, validation, and PR preparation. The implementation was already exercised in staging and was verified with the full local suite.


Summary by cubic

Restore an elastic pg.Pool for the shared PostgreSQL client to fix concurrent query contention and reduce timeouts. The pool is lazy (min 0), defaults to 50 max connections per replica, closes idle after 30s, and bounds acquisition to 10s.

  • Bug Fixes
    • Replaced pg.Client with pg.Pool and moved query timing to the pool.
    • Preserved PgBouncer-compatible URL normalization, warmup, error reporting, and graceful shutdown.
    • Added DB_POOL_MAX env support to override the pool ceiling.

Written for commit 6d65b6f. Summary will update on new commits.

Review in cubic

@cursor

cursor Bot commented Aug 1, 2026

Copy link
Copy Markdown

Bugbot is not enabled for your account, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

@vercel

vercel Bot commented Aug 1, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
dashboard Ready Ready Preview Aug 1, 2026 6:10pm
databuddy-status Ready Ready Preview Aug 1, 2026 6:10pm
documentation Ready Ready Preview Aug 1, 2026 6:10pm

@coderabbitai

coderabbitai Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 10f22c09-52f8-4661-bad4-91d105cb9ea4

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@greptile-apps

greptile-apps Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR restores a lazily initialized, bounded PostgreSQL pool in place of the shared single client while retaining query instrumentation, error handling, warmup, and shutdown behavior.

  • Configures up to 50 pooled connections with 30-second idle eviction and a 10-second acquisition timeout.
  • Preserves PostgreSQL URL normalization and application naming.
  • Updates warmup and graceful shutdown to acquire, release, and close pooled clients.

Confidence Score: 5/5

The PR appears safe to merge, with no concrete changed-code failure identified in the restored pool implementation.

The shared database path now uses the Promise-based pg pool API expected by Drizzle, while the configured acquisition bounds and acquire-release/end lifecycle preserve the existing public behavior.

Important Files Changed

Filename Overview
packages/db/src/client.ts Replaces the process-wide PostgreSQL Client with a bounded lazy Pool and adapts query timing, warmup, error handling, and shutdown without an accepted changed-code defect.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  Caller[Application query] --> Proxy[Lazy db proxy]
  Proxy --> Drizzle[Drizzle ORM]
  Drizzle --> Pool[Shared pg.Pool]
  Pool --> PgBouncer[PgBouncer / PostgreSQL]
  Warmup[warmPostgres] --> Acquire[Acquire and release client]
  Acquire --> Pool
  Shutdown[shutdownPostgres] --> End[pool.end]
  End --> Pool
Loading

Reviews (1): Last reviewed commit: "fix(db): raise postgres pool ceiling" | Re-trigger Greptile

@izadoesdev

Copy link
Copy Markdown
Member Author

Closing because this PR was mistakenly opened against main. Project PRs target staging, and the two PostgreSQL pool changes are already present on staging (0fe9cf895, 24ed27490). Retargeting this divergent hotfix branch would include unrelated changes, so there is no valid staging diff to review here.

@izadoesdev izadoesdev closed this Aug 1, 2026
@izadoesdev
izadoesdev deleted the codex/fix-production-postgres-pool branch August 2, 2026 14:38
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