PostgreSQL Enterprise (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
What is Connection Pooling and why is PgBouncer essential for PostgreSQL?
senior LevelSystem DesignDatabases & SQLPostgreSQL
In Simple Words: Because PostgreSQL forks an entire operating system process per connection, each connection consumes ~10 MB of RAM, and 500 connections cause massive CPU context switching:
- PgBouncer: Ultra-lightweight connection pooler that sits between clients and PostgreSQL.
- Pooling Modes:
- Session Pooling: Connection assigned to client for entire session lifetime.
- Transaction Pooling (Best Practice): Connection assigned to client strictly for the duration of a single transaction (
BEGINtoCOMMIT), and released immediately afterward. Allows 10,000 web clients to share 50 PostgreSQL connections! - Statement Pooling: Reassigned after every single SQL statement.