Repository navigation
Description
Once the pool is at max, handler() in src/index.js commits each new query to a specific busy connection instead of the pool queue. The order is: an idle connection, then opening a closed slot, then pipelining onto a busy connection (up to max_pipeline, default 100), and only then queries. node-postgres instead keeps the request in the pool queue and gives it to whichever client frees up first.
Committing early has costs:
- Head-of-line blocking. A fast query pipelined behind a slow one waits for it, even if another connection frees up immediately. Mixed workloads get poor tail latency. Expected repro, not yet run:
max: 2, warm both connections, startpg_sleep(5)andpg_sleep(0.1), then aselect 1. If it lands behind the 5 s query, it takes about 5 s while the other connection has been idle since 0.1 s. - It is invisible. Waiting work sits in a connection's
sentlist, not the pool queue. The first version ofsql.stats()reportedwaitingCount: 0with 10 of 12 queries stuck behind others (max: 2, 12 ×pg_sleep(0.3)). - Timeouts and cancels. A query timeout (Add a query timeout for silently dead sockets #9) on a pipelined query starts counting while the query is stuck behind someone else's work.
- Failures attach to the wrong query. Once several queries share one connection's ordered list, a mistake in that bookkeeping hits a neighbour. Unsettled transaction pipeline promise on parameter validation error porsager/postgres#1082 and Error swap between completing and failing query in build step (with max connections = 1) porsager/postgres#729 ([MOVED, NOT FIXED] Upstream reports the fork tracks: each now has its own issue on the Roadmap project #5) are this class. A reproduction during the
sql.stats()review went further than [MOVED, NOT FIXED] Upstream reports the fork tracks: each now has its own issue on the Roadmap project #5 records. Withmax: 1, an activepg_sleep, thensql.unsafe('select 1', <70000 params>): the sleep query rejected withMAX_PARAMETERS_EXCEEDED, the 70000-param query hung, and the nextselect 2 as cresolved with[](the hung query's response) instead of[{ c: 2 }]. - Poolers. Deadlocked/hung pool when connected to Postgres through Supavisor porsager/postgres#970 (Supavisor hangs, with
max_pipeline: 0as the workaround), Queries get stuck when running static queries in concurrent cron jobs porsager/postgres#1033 and arbitrary stuck queries porsager/postgres#827 ("stuck queries") may be the same mechanism. Not confirmed.
Pipelining still has value. It saves round trips for short, uniform queries, and with max: 1, common on serverless, the pool queue offers no alternative anyway. The question is the default policy, not pipelining itself.
Measure first
Use sql.stats() (waitingCount, executingCount) and latency percentiles with mixed fast and slow workloads at several max values, against direct PostgreSQL and a transaction-mode pooler (Supavisor, PgBouncer). Include max: 1.
Options to compare
- At
max, queue in the pool and hand each query to the first connection that becomes ready, so no early commitment. - Keep pipelining onto busy connections, but with a shallow default depth.
- Pipeline only on reserved handles and inside transactions, where the caller chose the connection.
- Leave the default and document
max_pipelinetuning (status quo plus docs).
Constraints
- A behaviour change. It belongs to the connection reliability work alongside Add a query timeout for silently dead sockets #9 and Abort reserve() and begin() leases with an AbortSignal #10, and needs measured numbers before a default changes.
- The Unsettled transaction pipeline promise on parameter validation error porsager/postgres#1082 misattribution is a correctness bug to fix regardless of the policy chosen.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsLater
Activity
okomarov commentedon Oct 11, 2026
Measurement criteria for choosing the dispatch policy.
Metrics. Report the median, p95 and p99 latency per query, and per request where a request runs several queries. Not the mean: a single 30 s wait reads as a broken app, while an average saving of 30 ms is not felt.
Workloads. Run each against direct PostgreSQL and a transaction-mode pooler, with added network latency (about a 20 ms round trip), because the round-trip gap only matters with a distant database:
max: 1.Expected trade-offs, to confirm or reject:
max: 1: sending ahead costs nothing, since a query waits as long in the pool as in the connection's line, and it saves one round trip per query.max> 1, all connections busy, mixed workload: a query sent behind a long one waits for its full duration, usually on another user's request. In the pool queue it would wait only for the first free connection. Sending ahead should hurt p95 and p99 here.max> 1, all connections busy, short queries, distant database: without sending ahead, each connection sits idle for most of every round trip. The pool queue grows and waits climb, so sending ahead may help p95 and p99 here.Leading hypothesis for the default, to test and not yet decided:
max> 1, keep queries in the pool queue until a connection is free.max: 1, and insidebegin()andreserve()where the caller chose the connection.okomarov commentedon Oct 11, 2026
Two separate questions sit in this issue:
Strategies to measure
The criteria are in the comment above: median, p95 and p99, never the mean.
max_pipeline: 100max_pipeline: 1max_pipeline: 0(also turns off sending ahead insidebegin()/reserve()and atmax: 1)postgres()instance; documentation onlyS1 to S3 need no library change. A sent query can't be withdrawn, and a cancel stops whatever the connection is running. So rescuing a waiting query (S4) means running it twice, which is only safe for reads; it also reports how often a query runs twice.
max_pipelineis in neither the README nor the types. That needs fixing whatever wins.