Repository navigation
Description
Postgres.js matches each reply to a query by its position in the connection's line. That is only correct if nothing is written while the server is partway through a multi-step exchange: a describe-first round trip, an open cursor, or a COPY.
submit() returns whether the connection can take another query. The decision, though, is held by its callers: handler(), go() and onopen() in the pool, and send() and l.next() for begin()/reserve(). They act on the answer from the last submit. Two paths make that answer stale. Both were reproduced on main (032ea2b) during the #20 review, against PostgreSQL 17.
1. Retry writes into an open Describe
retry() (src/connection.js) re-executes a prepared query whose cached plan was invalidated. The rebuilt query is describe-first (Parse, Describe, Flush, then wait for the parameter types), so submit() returns false. retry() ignores that. The pool still lists the connection as busy and sends another query into the open exchange. The retried query receives the other query's rows, and the other query fails with a TypeError.
Reproduction: cache a prepared statement, alter table so its result type changes, then submit another query while the retry's Describe is outstanding.
2. Queries written into COPY mode
copy … from stdin is a simple query, so submit() returns true and the pool keeps sending queries behind it. The COPY_IN_PROGRESS guard only applies after CopyInResponse arrives. Queries written before that land in copy mode, and the connection resets.
Proposal
The connection owns admission. It knows when it is mid-exchange:
- a describe-first query until its parameter types are handled;
- a cursor with its portal open;
- a retry;
- a query that may turn into COPY.
The pool and leases should read that state, and get a signal when the connection can accept again, instead of acting on a stored boolean. Callers stop writing early, so the connection never has to refuse bytes already sent.
For COPY, the client can't know a simple query is a COPY until the reply arrives. Options to weigh during implementation:
- don't send ahead behind simple-protocol queries;
- recognise COPY from the statement;
- hold writes inside the connection while the outcome is unknown.
Tests
- The two reproductions above.
- Check Concurrent queries in sql.begin() after cached-plan invalidation (0A000) leave the connection 'idle in transaction (aborted)' and never release it porsager/postgres#1234: concurrent queries in
sql.begin()after a cached-plan invalidation leave the connection idle in an aborted transaction. It looks like the retry path inside a transaction, but that isn't verified.
Relations
- Prerequisite for Pool dispatch commits queries to busy connections (head-of-line blocking) #18: whatever dispatch policy wins needs a trustworthy answer to "can this connection take more".
- Helps Add a query timeout for silently dead sockets #9: timeouts need to know what is actually running.
- Nearby but distinct: Failed COPY FROM STDIN never settles the writable, skips ROLLBACK in sql.begin(), and permanently leaks the connection as 'idle in transaction (aborted)' porsager/postgres#1173 (a failed COPY leaks the connection).
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsShipped
Activity
chore: release 4.4.0
[-]A connection accepts queries while it is mid-exchange (retry, COPY)[/-][+][FIXED in 4.4.0] A connection accepts queries while it is mid-exchange (retry, COPY)[/+]