Skip to content

feat: enhance PgBouncer transaction pooling mode for high-performance clients#51

Merged
paudley merged 3 commits intomainfrom
pg_bouncer_improvements
Nov 30, 2025
Merged

feat: enhance PgBouncer transaction pooling mode for high-performance clients#51
paudley merged 3 commits intomainfrom
pg_bouncer_improvements

Conversation

@paudley
Copy link
Owner

@paudley paudley commented Nov 30, 2025

Summary

  • Upgrade PgBouncer to 1.25+ (PGDG repository) enabling max_prepared_statements for full ORM/driver compatibility in transaction pooling mode
  • Add comprehensive configuration options for connection lifecycle, timeout protection, and DNS failover
  • Add PostgreSQL-side optimizations for transaction pooling (plan caching, JIT, parallel queries, TCP keepalives)
  • Add documentation guide for transaction pooling with client library examples

Key Changes

PgBouncer Enhancements

  • Prepared Statements: max_prepared_statements=1024 - enables automatic prepared statement tracking in transaction mode
  • Driver Compatibility: Extended ignore_startup_parameters for JDBC, Go lib/pq, Node.js, psql
  • Connection Lifecycle: server_lifetime=1800, server_idle_timeout=300 for connection recycling
  • Timeout Protection: query_wait_timeout=30, client_idle_timeout=3600 for fast-fail and cleanup
  • DNS Failover: dns_max_ttl=30, dns_nxdomain_ttl=5 for quick failover
  • Debugging: application_name_add_host=1 appends client IP:port to application_name

PostgreSQL Optimizations

  • plan_cache_mode=auto - generic plan caching after 5 executions
  • jit=on with jit_above_cost=100000 - JIT for complex queries
  • max_parallel_workers_per_gather=4 - parallel query support
  • tcp_keepalives_idle=60 - fast dead connection detection

Documentation

  • Comprehensive guide: docs/PGBOUNCER_TRANSACTION_POOLING.md
  • Client library examples: Python (psycopg2/3, SQLAlchemy, asyncpg), Node.js (pg, Prisma), Go (pgx), Java (JDBC)
  • Troubleshooting guide with common errors and solutions
  • Diagnostic commands reference

Files Changed

  • docker/pgbouncer/Dockerfile - Add PGDG repository for PgBouncer 1.25+
  • pgbouncer/entrypoint.sh - Add env vars and config generation
  • postgres/conf/postgresql.conf.tpl - Add transaction pooling optimizations
  • docker-compose.yml - Pass new env vars to both services
  • .env.example - Document all new configuration options
  • docs/PGBOUNCER_TRANSACTION_POOLING.md - New comprehensive guide

Test plan

  • Build PgBouncer image and verify version is 1.25+: docker compose build pgbouncer && docker compose run --rm pgbouncer pgbouncer --version
  • Start stack with pgbouncer profile: docker compose --profile pgbouncer up -d
  • Verify PgBouncer config shows new parameters: docker compose exec pgbouncer cat /tmp/pgbouncer.ini
  • Test prepared statement support through PgBouncer
  • Verify PostgreSQL settings: SHOW jit; SHOW plan_cache_mode; SHOW tcp_keepalives_idle;

… clients

Upgrade PgBouncer to 1.25+ (PGDG) enabling max_prepared_statements support
for full ORM/driver compatibility in transaction pooling mode.

PgBouncer changes:
- Upgrade Dockerfile to use PGDG repository (1.25+ vs Debian's 1.18)
- Add max_prepared_statements=1024 for prepared statement caching
- Add ignore_startup_parameters for JDBC/Go/Node compatibility
- Add connection lifecycle controls (server_lifetime, server_idle_timeout)
- Add client timeout protection (query_wait_timeout, client_idle_timeout)
- Add DNS failover settings (dns_max_ttl, dns_nxdomain_ttl)
- Add application_name_add_host for connection debugging

PostgreSQL changes:
- Add plan_cache_mode=auto for generic plan caching
- Enable JIT compilation (jit=on, jit_above_cost=100000)
- Add parallel query settings (max_parallel_workers_per_gather=4)
- Add TCP keepalive settings for fast dead connection detection
- Add idle_session_timeout and temp_file_limit settings

Documentation:
- Add comprehensive transaction pooling guide with client library examples
- Document limitations and solutions for session state, temp tables, etc.
- Include troubleshooting guide and diagnostic commands
Copilot AI review requested due to automatic review settings November 30, 2025 00:24
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @paudley, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the transaction pooling capabilities by upgrading PgBouncer to a version that supports prepared statements, introducing a suite of new configuration options for robust connection management and failover, and optimizing PostgreSQL settings for high-performance pooled environments. A detailed guide is also included to assist users in configuring their applications and understanding the nuances of transaction pooling.

Highlights

  • PgBouncer Upgrade and Prepared Statements: PgBouncer has been upgraded to version 1.25+ by integrating the PGDG repository, enabling the critical max_prepared_statements feature for full ORM/driver compatibility in transaction pooling mode.
  • Comprehensive PgBouncer Configuration: Extensive new configuration options have been added for PgBouncer, covering connection lifecycle management (e.g., server_lifetime, server_idle_timeout), robust timeout protection (e.g., query_wait_timeout, client_idle_timeout), and improved DNS failover capabilities (e.g., dns_max_ttl, dns_nxdomain_ttl).
  • PostgreSQL Optimizations for Transaction Pooling: PostgreSQL-side optimizations have been introduced to enhance performance in a transaction-pooled environment, including plan_cache_mode=auto, JIT compilation settings, parallel query support, and TCP keepalives for faster detection of dead connections.
  • New Documentation Guide: A comprehensive new documentation guide (docs/PGBOUNCER_TRANSACTION_POOLING.md) has been added, detailing transaction pooling concepts, its limitations and solutions, client library configurations for various languages (Python, Node.js, Go, Java), troubleshooting tips, and a configuration reference.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a comprehensive set of enhancements for using PgBouncer in transaction pooling mode. The changes are well-structured and include upgrades to PgBouncer, extensive configuration options for both PgBouncer and PostgreSQL, and excellent documentation to guide users.

The implementation is solid, with new configurations clearly exposed through environment variables and reflected in the docker-compose.yml and startup scripts. The new PGBOUNCER_TRANSACTION_POOLING.md guide is particularly noteworthy for its detail and practical client library examples.

I have a couple of suggestions for improvement:

  • In the pgbouncer Dockerfile, adding apt-get clean would help to further optimize the image size.
  • The node-postgres example in the new documentation uses an insecure SSL configuration that should be corrected to promote better security practices.

Overall, this is a great contribution that significantly improves the transaction pooling capabilities and usability of the project.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR enhances the PgBouncer configuration to support transaction pooling mode with full ORM and driver compatibility by upgrading to PgBouncer 1.21+ from the PGDG repository. The enhancement centers around enabling max_prepared_statements support, which allows prepared statements to work correctly in transaction pooling mode by automatically tracking and re-preparing them across connection reassignments. Additionally, the PR adds comprehensive timeout protection, connection lifecycle management, DNS failover settings, and PostgreSQL-side optimizations for transaction pooling performance.

Key changes:

  • Added PGDG repository to Dockerfile for PgBouncer 1.21+ with max_prepared_statements support
  • Extended PgBouncer configuration with transaction mode compatibility settings, connection lifecycle management, and timeout protection
  • Added PostgreSQL optimizations for plan caching, JIT compilation, parallel queries, and TCP keepalives
  • Created comprehensive documentation guide with client library examples and troubleshooting guidance

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
docker/pgbouncer/Dockerfile Adds PGDG repository to install PgBouncer 1.21+ with prepared statement support
pgbouncer/entrypoint.sh Adds environment variables and configuration generation for transaction pooling features
postgres/conf/postgresql.conf.tpl Adds PostgreSQL configuration optimizations for transaction pooling (plan cache, JIT, TCP keepalives)
docker-compose.yml Passes new environment variables to both PostgreSQL and PgBouncer services
.env.example Documents all new configuration options with detailed comments
docs/PGBOUNCER_TRANSACTION_POOLING.md New comprehensive guide for transaction pooling with examples and troubleshooting

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Add apt-get clean before rm -rf for smaller image size (Gemini)
- Fix inconsistent version reference: 1.25+ → 1.21+ (Copilot)
  max_prepared_statements requires PgBouncer 1.21+, not 1.25+
Address Copilot review: Go examples now show proper error handling
with if err != nil checks instead of ignoring errors with _
@paudley paudley merged commit 2cbf12d into main Nov 30, 2025
16 checks passed
@paudley paudley deleted the pg_bouncer_improvements branch November 30, 2025 00:48
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