Skip to content

Abort a transaction when a parameter fails to serialize after its describe - #1238

Closed
chrbala wants to merge 1 commit into
porsager:masterfrom
chrbala:fix-serialize-error-after-describe
Closed

chrbala wants to merge 1 commit into
porsager:masterfrom
chrbala:fix-serialize-error-after-describe

Conversation

@chrbala

@chrbala chrbala commented Sep 30, 2026

Copy link
Copy Markdown

A query with parameters that is new to a connection is sent as Parse and Describe first, and only bound once the server has described its parameter types. If a parameter then fails to serialize (for example new Date(NaN), whose serializer throws a RangeError), the query is rejected and a bare Sync is sent. The server answers that Sync without an error, so a transaction the query was pipelined in goes on and commits without it:

const reserved = await sql.reserve()
await Promise.all([
  reserved`begin`,
  reserved`insert into test values (1)`,
  reserved`select ${ new Date(NaN) }::timestamptz`, // rejects
  reserved`insert into test values (2)`,
  reserved`commit`                                  // commits 1 and 2
])

Bind the query with no parameters instead. The server refuses it, since the query takes at least one parameter (or it would not have been described first). That aborts the transaction, the refusal answers in the query's place, and the query is rejected with its own error, as a retried query already is. It is no longer a cursor either, whose error would send a Sync of its own and shift the next answer.

…cribe

A query with parameters that is new to a connection is sent as Parse and
Describe first, and only bound once the server has described its
parameter types. If a parameter then fails to serialize (for example
`new Date(NaN)`, whose serializer throws a RangeError), the query is
rejected and a bare Sync is sent. The server answers that Sync without an
error, so a transaction the query was pipelined in goes on and commits
without it:

    const reserved = await sql.reserve()
    await Promise.all([
      reserved`begin`,
      reserved`insert into test values (1)`,
      reserved`select ${ new Date(NaN) }::timestamptz`, // rejects
      reserved`insert into test values (2)`,
      reserved`commit`                                  // commits 1 and 2
    ])

Bind the query with no parameters instead. The server refuses it, since
the query takes at least one parameter (or it would not have been
described first). That aborts the transaction, the refusal answers in the
query's place, and the query is rejected with its own error, as a retried
query already is. It is no longer a cursor either, whose error would send
a Sync of its own and shift the next answer.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@chrbala

chrbala commented Sep 30, 2026

Copy link
Copy Markdown
Author

I made a few PRs from Claude which fix some problems I was having. Can you take a look?

okomarov added a commit to steadybrew/postgresjs that referenced this pull request Oct 21, 2026
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.

Closes #70. Based on porsager#1236 and porsager#1238.
okomarov added a commit to steadybrew/postgresjs that referenced this pull request Oct 21, 2026
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.

Closes #70. Based on porsager#1236 and porsager#1238.
okomarov added a commit to steadybrew/postgresjs that referenced this pull request Oct 21, 2026
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.

Closes #70. Based on porsager#1236 and porsager#1238.
okomarov added a commit to steadybrew/postgresjs that referenced this pull request Oct 22, 2026
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 added a commit to steadybrew/postgresjs that referenced this pull request Oct 23, 2026
A query that fails before it reaches the server now fails the
transaction it is in (issue #70). The reserved-connection pipeline tests
for an undefined value and too many parameters reproduce
porsager#1236, and the serialize-after-describe tests with
prepare on and off reproduce porsager#1238. On 3.4.9 each
commits both inserts without the failed statement or hangs on the
shifted answers; on the fork the transaction rolls back.
okomarov added a commit to steadybrew/postgresjs that referenced this pull request Oct 24, 2026
A query that fails before it reaches the server now fails the
transaction it is in (issue #70). The reserved-connection pipeline tests
for an undefined value and too many parameters reproduce
porsager#1236, and the serialize-after-describe tests with
prepare on and off reproduce porsager#1238. On 3.4.9 each
commits both inserts without the failed statement or hangs on the
shifted answers; on the fork the transaction rolls back.
@porsager

Copy link
Copy Markdown
Owner

Thanks! This is part of v4 (06cf284), so I am closing it here. The changelog has the details: https://github.com/porsager/postgres/blob/master/CHANGELOG.md

@porsager porsager closed this Oct 29, 2026
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.

2 participants