fix(sql-pg): Fail transactions that PostgreSQL rolls back at COMMIT - #8558
Merged
tim-smart merged 5 commits intoSep 27, 2026
Merged
Conversation
🦋 Changeset detectedLatest commit: 2091937 The changes in this PR will be included in the next version bump. This PR includes changesets to release 31 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
Bundle Size AnalysisGenerated from PR build output; treat the content below as untrusted.
|
This branch is waiting to be deployed
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.
Problem
When a statement inside a transaction fails, PostgreSQL aborts the transaction. If the caller catches that error and the transaction body then succeeds,
withTransactionsendsCOMMIT. PostgreSQL does not answer thatCOMMITwith an error. It rolls the transaction back and returns the command tagROLLBACK.makeWithTransactionruns the commit asEffect.orDie(options.commit(conn))and never reads the result. SowithTransactionreports success, and every write in the transaction is gone.Reproduction
effect@4.0.0-rc.117and@effect/sql-pg@4.0.0-rc.117, Postgres 17:The same body with the error inside a nested
withTransactioncommits correctly, becauseROLLBACK TO SAVEPOINTrepairs the transaction beforeCOMMIT.Fix
SqlClient.makeaccepts an effect forcommitas well as a string. This follows theonCommitFailureoption added in Clean up SQLite transactions after failed commits #8529, and it is additive.@effect/sql-pgpasses acommitthat reads the command tag, whichPgConnection.queryalready returns. OnROLLBACKit fails with aSqlErrorwhose reason isUnknownErrorwithoperation: "commit".makeWithTransactionturns that failure into a defect, the same way it handles any failedCOMMIT.I chose
UnknownErrorbecause no existing reason describes a rolled-back commit. I can add a dedicated reason instead if you prefer.Validation
pnpm lint-fixEFFECT_INTEGRATION_TESTS=1 pnpm test --run --project @effect/sql-pg: 16 files, 414 tests pass. The new test inClient.integration.test.tsfails before the fix.pnpm test --run packages/effect/test/sql: 54 tests pass.pnpm checkCloses EFF-1608