Skip to content

fix: a query that fails before it is sent fails the transaction it is in - #177

Merged
okomarov merged 3 commits into
mainfrom
fix/client-failure-aborts-transaction
Oct 22, 2026
Merged

okomarov merged 3 commits into
mainfrom
fix/client-failure-aborts-transaction

Conversation

@okomarov

Copy link
Copy Markdown

Closes #70.

A query that fails in the client (an undefined value, too many parameters, a serializer that throws, a helper that fails to build) was rejected alone and never reached the server, so an open transaction carried on and a later COMMIT committed the other statements without it. The connection now sends a statement the server refuses in the failed query's place, so the transaction fails as it would after a server error:

  • in sql.begin(), a failure that lands after the callback returned makes COMMIT answer ROLLBACK, and sql.begin() rejects with TRANSACTION_ROLLED_BACK whose cause is the client error;
  • on a reserved connection or with max: 1, the statements after it reject with 25P02 and COMMIT answers ROLLBACK, as after a server error; a savepoint recovers.

The failed query still rejects with its own error, and outside a transaction the queries around it get their own results. Based on porsager#1236 and porsager#1238.

The first commit adds the tests alone, so CI shows them failing on main's code.

@okomarov
okomarov force-pushed the fix/client-failure-aborts-transaction branch 4 times, most recently from f9e87bd to 3029340 Compare October 21, 2026 22:56
@okomarov
okomarov force-pushed the fix/client-failure-aborts-transaction branch 11 times, most recently from 939b9e1 to 0a41adc Compare October 22, 2026 12:27
The section listed constraints and exceptions without saying what to
configure, and read as if Cloudflare's advice against prepare: false
conflicted with the example. It now opens with the three settings
(direct connection, prepare left on, max and fetch_types), explains them
after, and says Hyperdrive in front of Supavisor's transaction mode fails
with either prepare setting (runs 39234865111 and 39236978516). It also
drops "not at another pooler's transaction mode", which contradicted CI
passing in front of Neon's pooled endpoint.
…it is in

Reproduces issue #70 on reserved connections (an undefined value, too
many parameters, and a value whose serializer throws after describe,
with prepare on and off), with a savepoint, and in sql.begin when the
failing query runs after the callback has returned. Also covers the
same failures outside a transaction and behind a cursor, which must
keep rejecting alone.

The Workers edge scenarios run the same cases through Neon, the
Supabase pooler and Hyperdrive, with prepare on and off where the
target supports prepared statements. Hyperdrive with prepare on closes
the connection on a new described statement after an error inside a
transaction (cloudflare/workers-sdk#16421), so there the scenario checks
that nothing commits rather than for 25P02 and ROLLBACK.
A query that failed in the client (an undefined value, too many
parameters, a serializer that throws, a helper that fails to build) was
rejected alone and never reached the server, so an open transaction
carried on and a later COMMIT committed the other statements without
it. The connection now sends a statement the server refuses in the
failed query's place, with its own Sync, and keeps the query in the
line with its own error to report, so answers stay aligned and the
transaction fails as after a server error: sql.begin() rejects with
TRANSACTION_ROLLED_BACK whose cause is the client error, and a
transaction run by hand gets 25P02 until a savepoint rolls it back.
The same applies when a value fails to serialize after the server
described the statement. Outside a transaction only the failing query
rejects, as before.

The upgrade note leads with the half-committed transaction this
prevents and says upgrading leaves data written that way in place.

Closes #70. Based on porsager#1236 and porsager#1238.
@okomarov
okomarov force-pushed the fix/client-failure-aborts-transaction branch from 4159604 to cd1f85c Compare October 22, 2026 12:57
@okomarov
okomarov merged commit cd1f85c into main Oct 22, 2026
@okomarov
okomarov deleted the fix/client-failure-aborts-transaction branch October 22, 2026 13:15
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.7.0] A statement that fails to serialize does not abort its transaction, so COMMIT succeeds without it

1 participant