Your Node pg query_timeout doesn't stop the query in Postgres
My Node service gave up on a slow query after 1 second. Postgres kept running it for about 9 more, holding a row lock the whole time, and the next request that touched the same row waited 9,010 ms. I measured it with pg
My Node service gave up on a slow query after 1 second. Postgres kept running it for about 9 more, holding a row lock the whole time, and the next request that touched the same row waited 9,010 ms.
I measured it with pg 8.23.1 (node-postgres), Node 24.22.1 and PostgreSQL 17.12, three runs per setting. The client option query_timeout: 1000 rejects your promise with Query read timeout at about 1,016 ms. It does not cancel anything on the server.
The setup
One row, and one slow statement that takes the row lock and then sleeps:
const pool = new Pool({ max: 5, query_timeout: 1000 });
await pool.query(
"update acct set v = v + 1 where id = 1 returning (select pg_sleep(10))"
);
Right after the error, a second connection runs a plain update acct set v = v + 1 where id = 1, and a watcher polls pg_stat_activity every 250 ms or so. Times are from the start of the slow query.
| Setting | App gets the error at | Slow statement last seen active | Second update waited (own start) |
|---|---|---|---|
query_timeout: 1000 |
1,016 to 1,020 ms | 9,841 to 9,850 ms | 9,009 to 9,011 ms |
statement_timeout: 1000 |
1,016 to 1,017 ms | already gone at the first poll | 6 to 8 ms |
query_timeout + client_connection_check_interval=1000
|
1,016 to 1,019 ms | 1,783 to 1,785 ms | 999 to 1,000 ms |
Same error timing from the app's point of view in all three rows. Very different lives on the server.
Your pool max stops meaning anything
This is the part that changed how I read pool metrics. After a query_timeout, pg throws that client away (pool.totalCount dropped to 0) and the next request gets a brand new connection. The old backend is still sleeping with its lock.
I fired 20 slow queries, one every 200 ms, through a Pool with max: 5:
- With
query_timeout: 1000, the pool never showed more than 4 clients. Postgres showed 20 active backends for that app, and still 20 after all 20 promises had rejected. - With
statement_timeout: 1000, the peak was 4 active backends and 0 after.
So under a slow query storm, a client-only timeout makes your app retry on top of work Postgres is still doing, while every pool gauge looks healthy. That is a quiet road to sorry, too many clients already without any single pool ever going over its max.
What I changed
statement_timeout is enforced by Postgres, so the cancel happens where the work and the locks are:
const pool = new Pool({
max: 5,
statement_timeout: 5000, // Postgres cancels the statement
query_timeout: 6000, // backstop if the network itself is gone
});
Or once, on the role, so every client and every script gets it:
alter role app set statement_timeout = '5s';
I still keep query_timeout a bit above it. If the network dies, the server cannot send you the cancel error, and you want the promise to settle anyway.
On Postgres 14 and later there is also client_connection_check_interval. With it set to 1 s, the server noticed the dropped client during the sleep and ended the statement about 1 s after the disconnect instead of about 9 s. It is a safety net for clients that vanish, not a replacement for a real statement timeout.
One caveat from the lab: pg_sleep stands in for a slow query here. Without client_connection_check_interval, Postgres does not look for a dead client while a statement runs, so a real slow query also runs to the end. The whole lab is three small files (a Postgres starter, the timeout script and the pileup script), and the numbers above come straight from its run logs.
The question
Where do you set statement_timeout in production: on the database role, in the pool config, or per query with SET LOCAL for the few slow reports you allow to run long?
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.