Skip to content

[FIXED in 4.4.0] A connection accepts queries while it is mid-exchange (retry, COPY) #21

Closed

Description

@okomarov

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

Relations

Activity

  1. added a commit that references this issue on Oct 11, 2026
  2. moved this to Shipped in Roadmapon Oct 15, 2026
  3. added
    area: connections
    bug
    on Oct 16, 2026
  4. changed the title [-]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)[/+] on Oct 17, 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: connectionsbug

    Type

    No type

    Projects

    • Status
      Shipped

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions