ArticleAdvanced2 min read

When Database Connection Pools Become the Bottleneck

How to distinguish pool wait from query time, size connections with Little’s law, and evaluate PgBouncer's trade-offs.

Author
Pradeep Gajendra
Published
Updated

Scope of this article

Versions
PostgreSQL 16; node-postgres 8.x; PgBouncer configuration docs
Environment
Capacity example is illustrative, not a benchmark. Check managed service limits and PgBouncer version behavior.

Most "the database is slow" incidents in small services are not slow queries. They are requests waiting for a connection that never reached PostgreSQL at all. This article shows how to tell the difference, how to size a pool from first principles, and when a pooler like PgBouncer actually helps.

What a pool actually bounds#

A client-side pool caps concurrent checked-out connections per process. PostgreSQL separately caps server connections with max_connections, which defaults to 100 [1]. Every process multiplies the client-side number.

Twelve workers, five pool connections all busy, seven requests waiting in the pool queue

Original illustration: when every connection is checked out, latency comes from the queue, not the query. Credit: StackTalk
Sequence: a request asks the pool for a client; if none is idle it waits until timeout, then queries PostgreSQL and releases the client.

Diagram

Sizing with Little's law#

The number of connections a workload needs is roughly arrival rate times time-in-database:

L=λ⋅W⇒400 req/s×0.012 s=4.8 connectionsL = \lambda \cdot W \quad\Rightarrow\quad 400\,\text{req/s} \times 0.012\,\text{s} = 4.8\ \text{connections}

The HikariCP project's sizing notes argue that small pools often outperform large ones on the same hardware [2]; treat that as a hypothesis to test on your own workload, not a rule.

A bounded pool in node-postgres#

src/db/pool.tstypescript
import { Pool } from "pg";

export const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
  max: 10, // per process
  idleTimeoutMillis: 30_000,
  connectionTimeoutMillis: 2_000, // fail fast instead of queueing forever
});

node-postgres queues connect() calls once max clients are checked out [3]. A short connectionTimeoutMillis turns silent queueing into a visible error you can alert on.

Seeing the queue from the server side#

Terminal
psql "$DATABASE_URL" -c "select state, count(*) from pg_stat_activity group by state;"

When a pooler helps#

PgBouncer pool modes and their constraints (from the PgBouncer configuration docs)

Mode

Server connection released

Breaks

session

When the client disconnects

Little multiplexing benefit; connections remain pinned

transaction

After each transaction

Session-level state: SET, advisory locks, LISTEN

statement

After each statement

Multi-statement transactions

Transaction pooling lets many idle client connections share few server connections [4], which matters when you run many processes or serverless instances.

Checklist#

  • Sum max pool size across every process and compare it with max_connections minus reserved slots.

  • Alert on pool wait time and connection timeouts, not only on query latency.

  • Keep transactions short; never await external I/O inside one.

  • Introduce PgBouncer in transaction mode when process count, not query cost, drives connection count.

Sources

  1. [1]
    PostgreSQL Documentation: 19.3 Connections and Authentication (max_connections)

    PostgreSQL Global Development Group · publication date unknown · PostgreSQL 16 · retrieved 2026-11-02

  2. [2]
    HikariCP wiki: About Pool Sizing

    HikariCP project · publication date unknown · retrieved 2026-11-02

  3. [3]
    node-postgres: Pooling

    node-postgres · publication date unknown · pg 8.x · retrieved 2026-11-02

  4. [4]
    PgBouncer: Configuration (pool_mode)

    PgBouncer project · publication date unknown · PgBouncer 1.2x · retrieved 2026-11-02

Spotted an error? Use “Suggest a correction” in the discussion, or read our corrections policy.

Discussion

Sign in to join the discussion. Reading is always open.

Loading discussion…

Report comment