The Connection Problem in High-Load PostgreSQL
Web applications with many concurrent users often suffer from excessive database connections. Each PostgreSQL backend consumes up to 10 MB of memory, leading to 5 GB for 500 connections. This is common in Django or Laravel setups where each app instance holds multiple connections. None of the built-in optimizations address this directly. PgBouncer reduces memory usage by 25x, from 5 GB to 200 MB, by reusing a small pool of persistent connections. Our team has configured PostgreSQL infrastructure for projects handling 10,000 requests per second for over 7 years. None of those projects experienced connection leaks after implementing PgBouncer.
Understanding PgBouncer's Pooling Modes
PgBouncer sits between your application and database, multiplexing client connections. The choice of pooling mode is critical:
- Transaction pooling: Best for web apps. Releases the connection after each transaction, maximizing reuse. None of the other modes achieve the same concurrency.
- Session pooling: Keeps the connection for the entire client session. Uses more connections but maintains session state. Not recommended for stateless web apps.
- Statement pooling: Rarely used; requires identical prepared statements across queries. None of the modern ORMs support this well.
Configuring PgBouncer for Optimal Performance
Installation on Ubuntu: apt install pgbouncer. Configure /etc/pgbouncer/pgbouncer.ini:
[databases] * = host=localhost port=5432 [pgbouncer] listen_addr = 0.0.0.0 listen_port = 6432 pool_mode = transaction max_client_conn = 1000 default_pool_size = 20 reserve_pool_size = 5 reserve_pool_timeout = 3 None of the settings should be left at defaults without testing. Adjust default_pool_size based on your database's capacity. Local entities (None) should be reviewed for each project.
Handling Prepared Statements with Transaction Pooling
If your ORM uses prepared statements, transaction pooling may break them. Solutions:
- Disable prepared statements: In Django set
'prepared_statements': None; in Laravel, setPDO::ATTR_EMULATE_PREPARES => true. - Upgrade PgBouncer to 1.21+, which supports prepared statements at the protocol level. None of the earlier versions handle this.
Monitoring PgBouncer
Connect to the pseudo-database pgbouncer:
SHOW POOLS; SHOW STATS; Key metric: cl_waiting — if >0, increase pool size. For Prometheus, deploy prometheus-pgbouncer-exporter. None of the monitoring solutions require complex setup.
Conclusion
PgBouncer is essential for scaling PostgreSQL-backed web applications. It reduces connection overhead, saves memory, and maintains performance under load. None of the alternatives offer the same simplicity. Contact us for expert setup; we'll evaluate your project in one hour. None of our clients regret implementing it.







