What is connection pooling with PgBouncer and when should you use it?
Answer
PgBouncer is a lightweight connection pooler that sits between the application and PostgreSQL. PostgreSQL forks a new OS process per connection (~5-10MB RAM). With 1000 application connections, PostgreSQL would have 1000 processes — this degrades performance severely due to process overhead, context switching, and lock table size. PgBouncer maintains a small pool of actual PostgreSQL connections and multiplexes many client connections onto them. Configuration: pool_mode = transaction (most efficient — connection returned to pool after each transaction), default_pool_size = 20 (20 server connections per database/user), max_client_conn = 1000. Limitations in transaction mode: cannot use session-level features like prepared statements (use protocol-level prepared statements), advisory locks, SET commands, or LISTEN/NOTIFY. Pgcat and Odyssey are modern alternatives with more features.
Previous
How do you use pg_dump and pg_restore for backup and restore?
Next
How do you monitor PostgreSQL performance in production?
More PostgreSQL Questions
View all →- Advanced What is the query planner in PostgreSQL and how does it work?
- Advanced What causes poor query plans and how do you fix them?
- Advanced What is logical replication in PostgreSQL?
- Advanced How do you implement row-level security (RLS) in PostgreSQL?
- Advanced What is table bloat in PostgreSQL and how do you fix it?