-
Notifications
You must be signed in to change notification settings - Fork 3.3k
fix(db): enable database connection pooling in production #1564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Greptile Overview
Summary
This PR enables database connection pooling in production environments by removing a conditional check that previously prevented the global database instance from being cached in production. The change modifies `packages/db/index.ts` to ensure the singleton pattern for database connections is applied consistently across all environments.The fix addresses a performance issue where production environments were creating new database connections for every request instead of reusing the existing connection pool. The database configuration already includes proper pooling settings (max: 80 connections, idle_timeout: 20s), but these weren't being utilized effectively in production due to the missing global caching. By removing the NODE_ENV !== 'production' condition around globalThis.database = db, the change aligns production behavior with development and staging environments, allowing the connection pool to function as intended.
This modification fits naturally into the existing database setup architecture, which uses Drizzle ORM with PostgreSQL and already has connection pooling configured. The change simply ensures that the singleton pattern works consistently across all deployment environments.
Important Files Changed
Changed Files
| Filename | Score | Overview |
|---|---|---|
| packages/db/index.ts | 4/5 | Removes NODE_ENV check to enable database connection pooling in production |
Confidence score: 4/5
- This PR is safe to merge with minimal risk as it fixes a clear performance issue by enabling proper connection pooling
- Score reflects a straightforward fix that aligns production with other environments using existing, well-tested pooling configuration
- No files require special attention as the change is simple and well-targeted
Sequence Diagram
sequenceDiagram
participant App as "Application"
participant DB as "Database Module"
participant Postgres as "PostgreSQL Client"
participant Pool as "Connection Pool"
participant Database as "PostgreSQL Database"
App->>DB: "Import database module"
DB->>DB: "Read DATABASE_URL environment variable"
alt DATABASE_URL missing
DB->>App: "Throw Error: Missing DATABASE_URL"
else DATABASE_URL exists
DB->>Postgres: "Create postgres client with pooling config"
Note over Postgres: "Config: prepare=false, idle_timeout=20,<br/>connect_timeout=30, max=80"
Postgres->>Pool: "Initialize connection pool"
Pool->>Database: "Establish initial connections"
Database-->>Pool: "Connection established"
Pool-->>Postgres: "Pool ready"
Postgres-->>DB: "Client ready"
DB->>DB: "Create drizzle client with postgres client"
DB->>DB: "Check if global database exists"
alt Global database exists
DB->>DB: "Use existing global database"
else No global database
DB->>DB: "Set drizzleClient as global database"
end
DB->>App: "Export db instance"
end
1 file reviewed, no comments
Added two diagnostic endpoints: - /api/debug/env: Shows NODE_ENV and globalThis.database status - /api/debug/db-test: Measures database query performance 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
724fedd to
1c189ac
Compare
Summary
enable database connection pooling in production, was already enabled in other environments
Type of Change
Testing
Manually
Checklist