Unprepared queries with parameters take two round trips (`describeFirst`) — one would do for plain values
With prepare: false (needed behind poolers such as PgBouncer transaction mode or Cloudflare Hyperdrive), every query that has parameters is sent in two round trips: Parse/Describe + Flush, a wait for ParameterDescription, then Bind/Execute. Queries without parameters go in one flush (unnamed()).
The line is in src/connection.js build():
q.describeFirst = q.onlyDescribe || (parameters.length && !q.prepared)On a high-latency link this doubles the cost of nearly every query. Measured through a proxy adding 70 ms each way (v3.4.9, prepare: false, fetch_types: false):
| Query | Time |
|---|---|
select 1 |
145 ms |
select ${'x'}::text |
290 ms |
| 3 × parameterised, in parallel | 295 ms |
As far as I can tell, the describe is there so each value is serialised with the serializer for the server's parameter type. But for strings, numbers, booleans, bigints, Dates and Uint8Arrays, the text sent is the same whether the type comes from the server or from inferType. Only arrays and plain objects need the server's type.
A minimal change that keeps the describe for those and skips it otherwise:
q.describeFirst = q.onlyDescribe || (parameters.length && !q.prepared && (q.prepare || !parameters.every(plainParameter)))
function plainParameter(x) {
return x === null || typeof x === 'string' || typeof x === 'number' || typeof x === 'boolean' ||
typeof x === 'bigint' || x instanceof Date || x instanceof Uint8Array
}With it, the parameterised queries above take 145 ms. We've run it in production (Cloudflare Workers + Hyperdrive + Drizzle) with a test suite of ~2,300 tests, including round-trip checks for every plain value type against a real database.
I'm happy to open a PR with this and tests if the approach is acceptable, or to adjust it if I've missed a case where the server's type changes how a plain value is sent.
Source: porsager/postgres