Skip to content

Queries with values take two round trips when parameter types are already known #41

Open

Description

@okomarov

Every query with values that is not already prepared on its connection asks the server for its parameter types first (Parse, Describe, Flush), waits for the answer, then sends Bind, Execute, Sync. That is two round trips.

  • With prepare: false, which PgBouncer in transaction mode, Supavisor and Hyperdrive require, it happens on every query with values, every time.
  • With prepare: true, it happens on each connection's first run of a statement. On serverless, connections are often fresh.
  • A query waiting for its types also holds back the queries queued behind it on that connection.

porsager#1225 measured select ${'x'}::text at 290 ms against 145 ms for select 1, with 70 ms each way.

Why "skip it for plain values" is not safe here

node-postgres never asks, because it encodes every value the same way whatever the server's type. Postgres.js does not: Bind picks each value's serializer from the type the server reported, and strings and numbers infer type 0. Skipping the request would change what is stored:

  • a string bound to json/jsonb is JSON-encoded today; sent raw, '{"a":1}' would store an object instead of a string;
  • a string or number bound to a date or timestamp column goes through new Date(x).toISOString() today; sent raw, the server parses it in the session time zone;
  • a number bound to boolean encodes as false unless it is true; sent raw, 1 would be true;
  • a string bound to bytea is hex-encoded today; custom types with a from list run their serializer on the reported type.

Proposal

  1. Skip the request when every parameter already has a non-zero type (booleans, bigints, Dates, Uint8Arrays, sql.typed). Nothing changes.
  2. With prepare: false, cache the parameter types the server reported, per pool, keyed by the inferred types plus the SQL text, with a size bound. The first run asks; later runs send Parse with the cached types in one round trip, so values are encoded exactly as today. Forget an entry when a type error follows a schema change.
  3. With prepare: true, the same cache serves a connection's first run of a statement another connection already described.
  4. Sending values raw like node-postgres changes stored values, so it would only come as an opt-in option or in a major version.

Measurement

Noise (Vercel Fluid, Supabase transaction pooler, prepare: false) logs per-query durations from 2026-10-12. Several days on 4.4.1 are the baseline; the switch to the build with this change will be the only change deployed. Compared by median, p95 and p99 per query.

Related: porsager#736 removed the extra Flush so Hyperdrive could cache queries. It was closed because caching needs the Describe step skipped, which is the design question here.

Activity

  1. added
    enhancement
    on Oct 12, 2026
  2. okomarov commented on Oct 13, 2026

    @okomarov
    Author

    Rescoped on 2026-10-13: no type cache. Sharing remembered answers across a pool needs heuristics to stay correct, such as text-matching DDL and search_path. Each heuristic misses cases by construction, and a miss stores a wrong value silently.

    New scope:

    1. Default, no behaviour change: skip the type request when every parameter already has a known type (booleans, bigints, Dates, Uint8Arrays, sql.typed).
    2. Opt-in client-side encoding, planned as the 5.0 default. One principle: if the JS value is already the column's kind, the driver encodes it; otherwise it sends the text and PostgreSQL parses it.
    3. A warning mode that encodes each value both ways and logs any query where the new encoding would store something different, so users can check before switching.
  3. moved this to In progress in Roadmapon Oct 15, 2026
  4. okomarov commented on Oct 15, 2026

    @okomarov
    Author

    Available in 4.5.0-rc1 with parameter_types: 'client' (npm install @steadybrew/postgresjs@next). parameter_types: 'warn' reports which of your queries 'client' would send differently; see Parameter types. This stays open until 'client' becomes the default in 5.0.

  5. added
    area: performance
    upstream
    on Oct 16, 2026
  6. okomarov commented on Oct 17, 2026

    @okomarov
    Author

    Production result for parameter_types: 'client', from one app with prepare: false behind the Supabase pooler: a day on 'warn' (2026-10-15 15:02 → 2026-10-16 10:55 UTC, 4.5.0-rc1) against a day on 'client' (2026-10-16 10:56 → 2026-10-17 10:48 UTC). 'warn' takes the same two round trips as the default 'server', and had reported no value that 'client' would send differently.

    'warn' 'client'
    Per query, median / p95 / p99 5.3 / 16.8 / 82.4 ms 3.1 / 13.5 / 83.4 ms
    Per request (sum of its queries), median / p95 / p99 72.1 / 476 / 589 ms 60.1 / 318 / 451 ms
    Errors 0 0
    • Of 49 query shapes with at least 200 runs on both days, 43 have a lower median on 'client' and 1 a higher one. The median change is −2.4 ms, one round trip to the pooler.
    • The most frequent select went from 4.4 / 6.6 / 13.2 ms to 2.4 / 4.0 / 10.1 ms.
    • The p99 is unchanged because the app's slowest statements (update … attempts + 1, median about 106 ms) spend their time in the server; those shapes did not move.

    Limits: one app, one day per mode. This is the evidence for making 'client' the 5.0 default; this issue stays open until then.

  7. moved this from In progress to Later in Roadmapon Oct 24, 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: performanceenhancementupstream

    Type

    No type

    Projects

    • Status
      Later

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions