Skip to content

Close a cursor when its last row is read - #31

Merged
quinnj merged 1 commit into
mainfrom
claude/close-exhausted-cursor
Oct 27, 2026
Merged

quinnj merged 1 commit into
mainfrom
claude/close-exhausted-cursor

Conversation

@quinnj

@quinnj quinnj commented Oct 22, 2026

Copy link
Copy Markdown
Member

A cursor opened outside a transaction starts its own (BEGIN), committed only by DBInterface.close!(cursor). Iterating a cursor to the end left that transaction open, so every later statement on the connection ran inside it, reported success, and was rolled back when the connection closed:

for row in Postgres.cursor(conn, "SELECT x FROM t"; fetchsize=100) end   # or Tables.columntable(cursor)
Postgres.in_transaction(conn)                     # true
DBInterface.execute(conn, "INSERT INTO t VALUES (-1)")   # "succeeds"
DBInterface.close!(conn)                           # the INSERT is gone

Reading past the last row now closes the cursor: the portal is released, the cursor's own transaction is committed (what close! already did), and an owned prepared statement is closed. A later close! is a no-op, and a transaction the caller opened stays open. Rows already returned stay valid (each ResultRow owns its values).

Not covered: a cursor abandoned with break keeps its transaction open until close!, as documented. The docstring now says that every statement runs inside the cursor's transaction until then.

Verification: "Cursor Streaming" now checks that a full read and Tables.columntable(cursor) leave no transaction open and that a later INSERT autocommits, and that the named-portal test sees the portal released at the last row with the caller's transaction still open (9 assertions fail on main); full Pkg.test() passes locally (8854/8854).

🤖 Generated with Claude Code

A cursor opened outside a transaction starts its own, committed on close.
Iterating to the end left it open, so every later statement on the
connection ran inside that transaction and was rolled back when the
connection closed: `for row in Postgres.cursor(conn, sql) ... end` or
`Tables.columntable(cursor)` followed by an INSERT silently lost the
INSERT. Reading past the last row now closes the cursor (portal released,
own transaction committed); `close!` afterwards is a no-op, and a
caller-owned transaction stays open.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@quinnj
quinnj merged commit a0ecea2 into main Oct 27, 2026
16 checks passed
quinnj referenced this pull request Oct 27, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant