Interview frequency: …
Short answer
A pool manages a set of pre-opened connections and shares them among requests; exhausting it queues requests and increases response times and timeouts.
Explanation
Creating a new database connection (TCP Handshake + TLS + Authentication) is expensive and takes tens of milliseconds. A Pool (such as pg.Pool) provides a fixed number of connections (for example, 20), which are borrowed with client = await pool.connect() and returned with client.release(). If queries take too long or connections leak because release is forgotten, the server waits for an available connection.
Code example
const client = await pool.connect();
try {
const res = await client.query("SELECT * FROM users WHERE id = $1", [id]);
} finally {
client.release(); // Always required in finally
}Common mistakes
- Forgetting to call client.release() in a finally block, quickly exhausting the pool and taking down the server.