Repository navigation
Close a cursor and roll back its transaction when a batch fails - #38
Merged
Merged
Conversation
A server error while fetching a later cursor batch left the cursor's own transaction aborted, so every later statement on the connection failed with 25P02 until the caller closed the cursor, and close! then threw "expected COMMIT but PostgreSQL completed ROLLBACK". A cursor consumed by Tables.columntable or collect could not be closed at all. A decode error in a later batch left the cursor open, so reading on skipped that batch's rows. A failed batch now ends the cursor the way a failed first batch already does: the portal is closed and a transaction the cursor opened is rolled back. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A decode error loses the batch's rows without aborting the transaction; only a server error does both. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Merged
quinnj
referenced
this pull request
Oct 30, 2026
Co-authored-by: Jacob Quinn <quinn.jacobd@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Postgres.cursormishandled a batch that fails after the first one:25P02 current transaction is aborted.DBInterface.close!(cursor)then threwexpected COMMIT but PostgreSQL completed ROLLBACK. A cursor consumed byTables.columntable(...),collect(...)orDataFrame(...)can't be closed at all, so onlyPostgres.rollback(conn)recovered the connection.infinitydate in a later batch): the cursor stayed open, so a caller that kept iterating silently skipped the failed batch's rows. A failure on row 3 withfetchsize=1gave rows[1, 2, 4, 5].A failed first batch already closed the cursor and rolled back its transaction.
fetch_portal!now does the same for later batches: it closes the portal and the cursor's statement, and rolls back a transaction the cursor opened. A transaction the caller opened is left to the caller.Found by a differential check of Postgres.jl against psycopg 3 on PostgreSQL 16 (streaming vs buffered reads).
Verification
Postgres.transaction: the block still rolls back and the connection is usable afterwards.BEGIN: the caller's transaction stays open until their ownROLLBACK.reconnect=trueconnection reconnects. Before, it refused to reconnect until the cursor was closed.fetchsize1, 7, 1000 and 100000.Pkg.test()(Julia 1.12.6, Docker PostgreSQL 16): 9250/9250 passed.Review notes
I re-read the diff as a reviewer.
fetch_portal!has one caller (iterate); the first batch is read in the constructor, which already handles errors.close_cursor!takes the connection lock afterfetch_portal!has released it. A failure during cleanup is swallowed, so the batch error is what the caller sees;rethrow()after the nestedtrykeeps the original exception and backtrace. Iterating the cursor after the failure returnsnothingrather than reading past the lost batch.🤖 Generated with Claude Code