Skip to content

Pool dispatch commits queries to busy connections (head-of-line blocking) #18

Open

Description

@okomarov

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:

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

  1. At max, queue in the pool and hand each query to the first connection that becomes ready, so no early commitment.
  2. Keep pipelining onto busy connections, but with a shallow default depth.
  3. Pipeline only on reserved handles and inside transactions, where the caller chose the connection.
  4. Leave the default and document max_pipeline tuning (status quo plus docs).

Constraints

Activity

  1. added
    enhancement
    on Oct 9, 2026
  2. okomarov commented on Oct 11, 2026

    @okomarov
    Author

    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:

    1. Short queries only, under sustained load.
    2. Short queries mixed with occasional long ones, for example 1 in 100 running 5 to 30 s.
    3. 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.
    • Sending at most one query ahead caps how many queries a long query holds up, not how long they wait.

    Leading hypothesis for the default, to test and not yet decided:

    • At max > 1, keep queries in the pool queue until a connection is free.
    • Make sending ahead across unrelated callers opt-in.
    • Keep sending ahead at max: 1, and inside begin() and reserve() where the caller chose the connection.
  3. okomarov commented on Oct 11, 2026

    @okomarov
    Author

    Two separate questions sit in this issue:

    • Admission (correctness): can this connection take another query right now? Only the connection knows. Today its callers decide from a stale answer, and two reproduced bugs come from that. Now tracked in [FIXED in 4.4.0] A connection accepts queries while it is mid-exchange (retry, COPY) #21, which is a prerequisite here.
    • Policy (latency): among connections that can take more, should the pool send ahead or keep queries waiting in the pool? This issue.

    Strategies to measure

    The criteria are in the comment above: median, p95 and p99, never the mean.

    Strategy How to run it
    S1 Send ahead up to 100 per connection (current default) max_pipeline: 100
    S2 Send at most one ahead max_pipeline: 1
    S3 Late binding: wait in the pool until a connection is free max_pipeline: 0 (also turns off sending ahead inside begin()/reserve() and at max: 1)
    S4 Send ahead, then rescue: when another connection goes idle while a query is still stuck behind a long one, run a copy there; first result wins and the leftover copy is discarded. Read-only, opt-in needs a prototype
    S5 A separate pool for known-long queries a second postgres() instance; documentation only

    S1 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_pipeline is in neither the README nor the types. That needs fixing whatever wins.

  4. moved this to Later in Roadmapon Oct 15, 2026
  5. added
    area: connections
    area: performance
    upstream
    on Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: connectionsarea: performanceenhancementupstream

    Type

    No type

    Projects

    • Status
      Later

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions