Connection pool
Пул соединений
A connection pool keeps connections to a database or other service open so an application can reuse them. The application borrows a connection when it needs one and returns it when the work is done.
Also known as: pgbouncer
Why keep connections open?
A database connection has a setup cost: the client opens a network connection, authenticates, and may negotiate TLS before it can send its first query. A pool lets later requests skip that setup by reusing connections. It also caps how many connections it opens, so a burst of incoming requests doesn't automatically become a burst of new database sessions.
What happens when you borrow a connection?
The pool hands out an idle connection or opens one if it has room. Your code uses it and releases it when finished. With many pooled APIs, calling close() on the borrowed handle returns the underlying connection to the pool rather than closing the network socket. Use a context manager or a finally block so the connection is returned even if the operation fails. If you're running a transaction, keep the same connection until you commit or roll back.
Suppose a pool has a maximum of 10 connections and 15 request handlers each need one at the same time. At most 10 can hold a connection; the others typically wait for one to become available or fail when their acquisition timeout expires. That timeout measures how long a caller can wait to get a connection. A query or statement timeout controls a different part of the request.
Application pools and PgBouncer
A driver or ORM often manages a pool inside each application process. PgBouncer is a separate PostgreSQL proxy that pools connections on the database side; it's one way to implement pooling, not another name for the concept. In session pooling mode, one PostgreSQL connection is assigned to a client for the lifetime of its session. In transaction pooling mode, PgBouncer returns that connection after the transaction ends so another client can use it. A client's next transaction may run on a different PostgreSQL connection, so don't assume session-scoped state, such as settings made with ordinary SET, will carry over.
Sizing and interview questions
A typical interview scenario: requests are timing out while waiting for a connection. Would you raise the pool limit? First look at how long callers wait to acquire a connection, how long they hold it, and how busy the database is. A handler might hold a connection while waiting on an unrelated HTTP call, or a transaction might spend most of its time blocked on a lock. Increasing the pool won't fix either cause and can make an overloaded database slower.
Count connections across all application instances, not just one. Eight instances with one local pool each, capped at 10 and connected directly to the same database, can open up to 80 connections. Leave headroom for other services, background jobs, and administration. Use a load test to check whether changing the limit improves throughput or simply moves the wait from the application to the database.