Add Conn.TxFromCurrentTransaction and TxOptions.BeginSQL - #2637
Open
timofurrer wants to merge 1 commit into
Open
Add Conn.TxFromCurrentTransaction and TxOptions.BeginSQL#2637timofurrer wants to merge 1 commit into
timofurrer wants to merge 1 commit into
Conversation
Allows a caller to begin a transaction as part of a Batch or a pgconn pipeline and still get a Tx for the rest of the unit of work, saving the round trip that Conn.BeginTx spends on its own begin query. TxOptions.BeginSQL exports the statement Conn.BeginTx would send, so callers queueing it themselves do not have to hand-roll it. Refs jackc#1667
timofurrer
marked this pull request as ready for review
August 22, 2026 10:30
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.
Implements the primitive suggested in #1667:
Design discussion and measurements are in #1667 (comment). Opening as a draft because naming and shape are still open.
What this adds
A caller can already queue
beginas the first query of aBatchor a pgconn pipeline, which saves the round tripConn.BeginTxspends on its ownExec. What is missing is a way to get aTxback afterwards, so the rest of the unit of work keeps pgx's transaction bookkeeping (ErrTxClosed,Rollbackbeing safe afterCommit,ErrTxCommitRollback, savepoint nesting) instead of reimplementing it outside pgx.BeginSQLis exported so callers queueing the begin themselves use the statement pgx would produce, rather than hand-rolling one that silently drifts frombeginSQL().Notes on the implementation
TxOptions.CommitQueryis read. The isolation level, access mode and deferrable mode must already have been established by the begin query the caller sent, and this is documented on the method.'T'and'E'. A batch that failed partway leaves the connection in a failed transaction, and the caller still needs aTxto roll it back.dbTxthatConn.BeginTxreturns, so behavior after construction is identical. No existing code path changes.Testing
New tests in
tx_test.go: table-driven coverage ofBeginSQL, plus commit, rollback, not-in-a-transaction, failed-transaction andCommitQuerycases forTxFromCurrentTransaction. The commit test also asserts the isolation level from the batched begin is in effect inside the resultingTx.Full
go testfor the root package passes against PostgreSQL 17.Not addressed
You raised on #1667 that
COMMITis the symmetric problem. It is, and this does not solve it: a caller can queuecommitin the final batch, but nothing can tell theTxit is already closed, sodefer tx.Rollback()spends the round trip that was just saved. That seems to want its own affordance and I did not want to conflate the two.AI disclosure
Per CONTRIBUTING.md: this change was developed with AI assistance (Claude Code, Opus 5), including the prototype, the round-trip measurements and the tests. I have reviewed it and can answer questions about it.