Skip to content

feat(database): add pgbouncer support and optimize postgres config #1700

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

guilherme-aguilar
Copy link

@guilherme-aguilar guilherme-aguilar commented Jul 8, 2025

  • Add pgbouncer service to handle connection pooling
  • Update database connection URIs to support direct and pooled connections
  • Optimize postgres configuration with better memory settings
  • Update prisma schema to support directUrl connection

Summary by Sourcery

Add PgBouncer for connection pooling, support both pooled and direct PostgreSQL connections in Prisma, and optimize PostgreSQL memory settings

New Features:

  • Introduce a PgBouncer service in Docker Compose for transaction-level connection pooling
  • Support separate directUrl and pooled connection URIs in the Prisma schema

Enhancements:

  • Tune PostgreSQL configuration with reduced max_connections and adjusted memory parameters
  • Rename POSTGRES_DB environment variable to evolution_db in Docker Compose

- Add pgbouncer service to handle connection pooling
- Update database connection URIs to support direct and pooled connections
- Optimize postgres configuration with better memory settings
- Update prisma schema to support directUrl connection
Copy link
Contributor

sourcery-ai bot commented Jul 8, 2025

Reviewer's Guide

This PR integrates PgBouncer into the Docker setup for transparent connection pooling, tunes Postgres memory and connection settings for better performance, and updates both environment examples and the Prisma schema to support separate direct and pooled connection URLs.

Class diagram for updated Prisma database connection configuration

classDiagram
  class PrismaClient {
    +String url
    +String directUrl
    +connect()
    +disconnect()
  }
  PrismaClient : url = pooled connection (via PgBouncer)
  PrismaClient : directUrl = direct Postgres connection
Loading

File-Level Changes

Change Details Files
Integrate PgBouncer for connection pooling
  • Added a new pgbouncer service in docker-compose
  • Configured pool mode, auth type, client connection limits, and pool size
  • Linked pgbouncer to postgres via depends_on and exposed port 6543
docker-compose.yaml
Optimize Postgres memory and connection settings
  • Reduced max_connections from 1000 to 200
  • Set shared_buffers, effective_cache_size, and work_mem parameters
  • Renamed database environment variable from evolution to evolution_db
docker-compose.yaml
Expand environment example for pooled and direct URIs
  • Updated .env.example with separate placeholders for DATABASE_URL and POOL_DATABASE_URL
  • Reflected new port for pgbouncer in example URI
.env.example
Enhance Prisma schema to support directUrl
  • Added directUrl attribute to the datasource block
  • Pointed url and directUrl at distinct environment variables
prisma/postgresql-schema.prisma

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

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @guilherme-aguilar - I've reviewed your changes - here's some feedback:

  • Extract database credentials (user, password, and database name) into environment variables or a .env file instead of hard-coding them in docker-compose for better flexibility and security.
  • Add healthcheck definitions for both the postgres and pgbouncer services to ensure dependent services wait for the database to be fully ready before starting.
  • After updating the Prisma schema with the new directUrl, remember to regenerate the Prisma client and verify any related migrations are applied.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Extract database credentials (user, password, and database name) into environment variables or a .env file instead of hard-coding them in docker-compose for better flexibility and security.
- Add healthcheck definitions for both the postgres and pgbouncer services to ensure dependent services wait for the database to be fully ready before starting.
- After updating the Prisma schema with the new directUrl, remember to regenerate the Prisma client and verify any related migrations are applied.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

guilherme added 2 commits July 8, 2025 18:09
- Add new psql_bouncer database provider option
- Update database scripts to handle psql_bouncer provider
- Comment out pgbouncer service in docker-compose
- Simplify postgresql schema by removing directUrl
- Add new psql_bouncer-schema.prisma file
- Update .env.example with psql_bouncer configuration
- Modify runWithProvider.js to handle psql_bouncer migrations
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