Ten requests leaked a connection in my Node service, and request number 11 never got an answer. No error, no log line, no timeout. I reproduced it with pg 8.23.1, Node 24.22.1 and PostgreSQL 17.12: after 8 seconds the query was still waiting, and I could have waited all day. The second failure, sorry, too many clients already, took 12 app instances with the default pool to trigger. Both are one-line fixes once you know what to look at.
Failure 1: the query that waits forever
pg.Pool defaults to max: 10 and connectionTimeoutMillis: 0. Zero means "wait without limit". So if ten checked-out clients never come back, the eleventh caller sits in a queue and nothing tells you.
Here is the handler pattern I had. It throws between connect() and release():
async function handler() { const c = await pool.connect(); await c.query('select 1'); throw new Error('boom'); // release() never runs } I ran it 10 times, then asked for one more connection. Results, one process per setup:
| setup | request 11 |
|---|---|
| leak, default pool | no answer after 8000 ms, pool.waitingCount = 1 |
leak, connectionTimeoutMillis: 2000 | timeout exceeded when trying to connect after 2000 ms |
try/finally with release() | ok in 0 to 1 ms, pool total stays at 1 |
The timeout does not fix the leak. It turns a silent hang into an error you can see, alert on and trace to the handler. The finally is the actual fix:
const { Pool } = require('pg'); const pool = new Pool({ max: 10, connectionTimeoutMillis: 2000, // fail loudly instead of queueing forever }); async function handler() { const c = await pool.connect(); try { await c.query('select 1'); // ...your work } finally { c.release(); // runs on return and on throw } } If you can, use pool.query() for single statements. It checks the client out and releases it for you, so there is nothing to forget.
Failure 2: sorry, too many clients already
Postgres defaults to max_connections = 100, and 3 of those are reserved for superusers (superuser_reserved_connections). Your app role gets 97. Each Node process has its own pool, so the math is instances x pool max, not pool max.
I started N processes, each with a pool of max and each opening all of its connections at once, using a normal non-superuser role:
| instances x pool max | wanted | connected | failed | error |
|---|---|---|---|---|
| 5 x 10 | 50 | 50 | 0 | none |
| 10 x 10 | 100 | 97 | 3 | remaining connection slots are reserved for roles with the SUPERUSER attribute |
| 12 x 10 | 120 | 97 | 23 | the same message, plus sorry, too many clients already |
| 10 x 9 | 90 | 90 | 0 | none |
Two things surprised me. First, 10 instances of a default pool already break, because of the 3 reserved slots. Second, the first error message you see is not "too many clients". It is the "reserved for SUPERUSER" one, and it sends people looking at their role instead of their pool size.
A caveat on my own run: I first connected as postgres (a superuser) and got 100 of 100 connections. Superusers can use the reserved slots. If you test pool limits as your admin user, you will think everything is fine.
The sizing rule I use now
pool max = floor((max_connections - 3 - headroom) / instances) Headroom is for migrations, psql sessions and a deploy where old and new instances overlap. With max_connections = 100, 10 instances and 10 spare slots, that is floor(87 / 10) = 8 per instance. Rolling deploys count too: if you run 10 and start 10 more before the old ones stop, size for 20.
If you cannot shrink the pool enough, put PgBouncer in transaction mode in front. I did not test it here, so treat that as a pointer and not a result.
See it before it hurts
Log the pool counters on an interval, and ask Postgres who is holding the slots:
setInterval(() => { console.log({ total: pool.totalCount, idle: pool.idleCount, waiting: pool.waitingCount }); }, 10_000).unref(); select usename, state, count(*) from pg_stat_activity group by 1, 2 order by 3 desc; A leak looks like idle: 0 with total stuck at your max while traffic is low. Many idle in transaction rows point at a handler that opened a transaction and never finished it.
Checklist
- Set
connectionTimeoutMillison every pool. Zero is never what you want in production. - Every manual
pool.connect()needs atry/finallywithrelease(). Search your code for.connect()and read each one. - Watch
pool.totalCount,pool.idleCountandpool.waitingCount. AwaitingCountabove zero for more than a moment is your early warning. - Do the instances x max math against
show max_connectionson the real server, and leave headroom. - Test as the app role, not as a superuser.
What I did not test
One pg version (8.23.1), one Node version (24.22.1), one Postgres build (17.12, the embedded-postgres binary, not a managed service). Managed Postgres often has a different max_connections tied to instance size, so read yours. I did not test ORMs or pool wrappers that set their own timeouts, and I did not test PgBouncer. The lab scripts are small enough to copy: one file for the leak, one worker for the instance math.
When your Node service runs out of Postgres connections, what is it that you find first: a hang, a timeout, or the "too many clients" error?

