Skip to content

Add SessionBuilder.Interleave - #25

Merged
winebarrel merged 1 commit into
mainfrom
interleave
Nov 8, 2026
Merged

winebarrel merged 1 commit into
mainfrom
interleave

Conversation

@winebarrel

Copy link
Copy Markdown
Owner

Summary

  • Add SessionBuilder.Interleave, for code that begins a transaction on a *sql.DB and then, before it ends, uses the *sql.DB outside the transaction or begins another transaction on it. Outside a test these run on separate connections; on one session they wait for each other until ErrTimeout.
  • With Interleave, a statement outside an open application transaction runs at once, inside it, in a savepoint of its own: it sees what the transaction wrote, a failure fails only that statement, and what it writes is rolled back with the transaction.
  • A transaction begun while another is open nests inside it. Nested transactions must end in the reverse order they began. If the outer one ends first, PostgreSQL ends the inner one with it: the outer Commit rolls both back and returns an error wrapping the new ErrTxOrder, and the inner one's statements, Commit and Rollback return such an error.
  • Statements still run one at a time. A transaction left open at the end of the test fails the test at once, with the stack of its Begin, instead of waiting for the timeout.
  • Without Interleave, nothing changes.
  • Update docs, README and tests.
db := sandpit.With().Interleave().Session(t).DB()

tx, _ := db.Begin()
tx.Exec(`INSERT INTO orders ...`)
db.QueryRow(`SELECT count(*) FROM orders`) // runs inside tx instead of waiting for it
inner, _ := db.Begin()                      // nests inside tx
inner.Commit()
tx.Commit()

Test plan

  • make vet
  • go test -race -count=1 ./...
  • go test -race -count=20 -run 'TestInterleave' ./...
  • golangci-lint run

Interleave lets statements and transactions run while an application transaction is open, instead of waiting for it: a statement outside the transaction runs inside it in a savepoint of its own, and a transaction begun meanwhile nests inside it. Nested transactions that end out of order fail with ErrTxOrder.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.47619% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.47%. Comparing base (24bf93b) to head (8f73123).

Files with missing lines Patch % Lines
conn.go 87.50% 3 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #25      +/-   ##
==========================================
- Coverage   93.08%   92.47%   -0.62%     
==========================================
  Files           7        7              
  Lines         376      425      +49     
==========================================
+ Hits          350      393      +43     
- Misses         14       17       +3     
- Partials       12       15       +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@winebarrel
winebarrel merged commit 6f4c90c into main Nov 8, 2026
4 of 5 checks passed
@winebarrel
winebarrel deleted the interleave branch November 8, 2026 10:28
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