🐘 PostgreSQL Intermediate

How does connection pooling work with PostgreSQL?

Why Interviewers Ask This

Candidates at the intermediate level are expected to not only know this concept but explain the trade-offs involved. Interviewers use this question to see if you can reason about design decisions, not just recall facts.

Answer

PostgreSQL creates a new OS process per connection — each connection consumes ~5-10MB of RAM and CPU. High connection counts (hundreds or thousands) degrade performance significantly. Connection pooling maintains a pool of database connections that are reused across application requests. PgBouncer is the most popular pooler. Modes: Session mode (connection held for entire client session), Transaction mode (connection returned to pool after each transaction — highest efficiency), Statement mode (returned after each statement — limited use). Configure: max_client_conn (clients), default_pool_size (server connections per database/user). PgBouncer sits between the app and PostgreSQL, often reducing server connections from thousands to tens. pgpool-II also provides load balancing and replication.

Common Mistake

Candidates often give textbook answers here. Interviewers are more impressed when you relate the concept to a specific problem you solved in a real PostgreSQL project.