Skip to content

fix: reject only the query that fails to build and keep pipelined responses in order - #22

Merged
okomarov merged 1 commit into
mainfrom
fix/build-error-pipeline
Oct 11, 2026
Merged

okomarov merged 1 commit into
mainfrom
fix/build-error-pipeline

Conversation

@okomarov

Copy link
Copy Markdown

Fixes #20: a query that fails while it is being built no longer takes another query's error or response. Addresses porsager#1082 (unsettled transaction pipeline promise on a parameter validation error) and porsager#729 (error swap between a completing and a failing query).

submit() puts a query in the connection's line before building it. When building threw, the old catch had three effects:

  • it rejected the running query instead of the failing one;
  • it left the failing query in the line with nothing sent for it;
  • it wrote a Sync only when nothing was pipelined.

Later replies were then matched to the wrong queries. Triggers are an undefined value, more than 65534 parameters, or a throwing debug callback.

The catch is now:

sent.remove(q) || write(Sync)
queryError(q, error)
return sent.length < max_pipeline
  • A failing query that was queued is taken out of the line. Nothing was sent for it, so nothing waits for a reply.
  • A Sync is written only when the failing query was alone on the connection, where it provides that query's ReadyForQuery. No Sync is ever written while another exchange is open.
  • The return value now respects max_pipeline.

The review rejected always writing a Sync for the failed query. PostgreSQL ignores Sync during COPY FROM STDIN, so later queries got shifted results and then hung. Inside a retried describe-first query, it closed that exchange early.

The review also found two existing paths where a connection accepts queries mid-exchange: a retry, and COPY. They are out of scope here and tracked in #21.

Local validation (Docker, Node 24.21, PostgreSQL 17.11):

  • ESM (312 tests) and CJS integration suites pass. Lint passes.
  • 8 of the 10 new tests fail on main, including upstream's own Unsettled transaction pipeline promise on parameter validation error porsager/postgres#1082 reproduction (sql.begin with ${undefined}), which hangs there. The other two (a throwing debug on an idle connection, and stats back to idle) pass on main and stay as regression guards.
  • The COPY test also fails on the first version of this fix, which always wrote a Sync.

…ponses in order

submit() registers a query as active or pipelined before it builds
it. When the build threw (an undefined parameter value, which raises
UNDEFINED_VALUE, too many parameters, or a throwing debug callback)
the catch rejected the active query instead of the one that failed,
and wrote a Sync only when nothing was pipelined. With another query
running, the running query received the build error, the failing query
was never written and never settled, and every later response was
matched to the query before it, so later queries resolved with each
other's results.

The catch now rejects the failing query itself. If it was queued
behind another query it is dropped from the pipeline, so nothing waits
for a reply that will never come, and no Sync is written. A Sync is
written only when the failing query is the active query on an idle
connection, where it gives that query its ReadyForQuery. Because no
Sync is sent while another sequence is open, it cannot close the
extended-protocol sequence of a retried describeFirst query or be
ignored in copy mode. submit() now returns whether the pipeline still
has room, instead of always true.

Rejected: always writing a Sync for a placeholder. PostgreSQL ignores
Sync during COPY FROM STDIN, so a placeholder queued behind a copy
never gets its ReadyForQuery and later queries get shifted results and
then hang. Inside a retried describeFirst sequence, which has no Sync
of its own, the placeholder's Sync closes that sequence early. Also
rejected: building before registering the query. On an idle
connection nothing would then produce a ReadyForQuery, and the pool
would keep the connection busy forever.

A throwing debug callback on an idle connection already worked, since
the failing query was the active one and its Sync gave it the
ReadyForQuery. That test and the idle stats test pass on main and are
regression guards. Behind a running query it hung before this change
and now rejects only that query. The microtask rethrow for the other
callbacks from #13 is still needed.

Closes #20. Fixes porsager#1082 and porsager#729.
Tests: build failure behind a running query, in the middle of a pipeline, on a reserved handle, behind copy from stdin, undefined value in a transaction and behind a running query, throwing debug idle, pipelined and on retry, stats back to idle.
@okomarov
okomarov merged commit 2a83834 into main Oct 11, 2026
10 checks passed
okomarov added a commit that referenced this pull request Oct 11, 2026
Ships sql.stats() and the fix for a query that fails to build taking
another query's error or response (#20). Promoted straight to latest
without a release candidate: CI on PR #22 passed on Node 24 and 26
against PostgreSQL 15 to 18, Cloudflare workerd and Deno.
@okomarov
okomarov deleted the fix/build-error-pipeline branch October 11, 2026 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FIXED in 4.3.0] A query that fails to serialize gets another query's error and response

1 participant