Repository navigation
Description
Describe the Bug
When Postgres refuses a COMMIT (a deferred constraint, a serialization failure, or the connection dropping during the commit), payload.db.commitTransaction(id) resolves normally. Nothing is thrown and nothing is logged. The rows are rolled back, but every caller continues as if they were saved: payload.create returns the document, hooks that run after the operation fire, and application code sends its mail or advances its cursor.
Why
packages/drizzle/src/transactions/beginTransaction.ts attaches a .catch() to the promise returned by this.drizzle.transaction(...):
const done = this.drizzle.transaction(async (tx) => { ... }, options).catch((err) => {
// Connection failed before callback ran - reject instead of hanging forever
transactionFailed(err)
})That .catch() was added for a connection failure before the callback runs, but it also means done can never reject. resolve() returns done, and commitTransaction does:
try {
await session.resolve()
} catch (_) {
await session.reject()
}so the try can never throw, and even if it could, the error would be swallowed by the empty catch. Once the callback has run, transactionFailed(err) is a no-op on an already settled promise, so the commit error goes nowhere.
Expected
commitTransaction rejects with the commit error, so payload.create and friends reject and callers can react.
Suggested fix
Keep the error when the transaction promise rejects after resolve() was called, and rethrow it:
let committing = false
let commitFailure: { error: unknown } | undefined
// ...
resolve = () => {
committing = true
res()
return done.then(() => {
if (commitFailure) throw commitFailure.error
})
}
// ...
}, options).catch((err) => {
transactionFailed(err)
if (committing) commitFailure = { error: err ?? new Error('The transaction could not be committed') }
})and in commitTransaction:
try {
await session.resolve()
} catch (err) {
await session.reject()
throw err
}We run this as a local patch with a regression test (the reproduction below, plus a successful commit and a rollback to show those are unchanged).
Possibly related: #17294 (an update that is rolled back while the request still answers 200).
Link to the code that reproduces this issue
Reproduction Steps
The linked repository is one script, with no Payload project and no collections. It opens a transaction through payload.db and commits it.
- Start a Postgres:
docker run -d --rm --name payload-repro-pg -e POSTGRES_PASSWORD=repro -p 127.0.0.1:55432:5432 postgres:17 git clone https://github.com/yousefamar/payload-commit-failure-repro && cd payload-commit-failure-repro && npm installnode repro.mjs(ornode repro.mjs <connection string>for another database)
The part that matters:
await payload.db.drizzle.execute(sql`create table probe (n int, constraint probe_u unique (n) deferrable initially deferred)`)
const id = await payload.db.beginTransaction()
const tx = payload.db.sessions[id].db
await tx.execute(sql`insert into probe (n) values (1)`)
await tx.execute(sql`insert into probe (n) values (1)`)
await payload.db.commitTransaction(id) // resolves; expected: rejects with 23505
// select count(*) from probe -> 0Actual output:
control, valid commit: resolved, rows = 1
refused COMMIT: commitTransaction RESOLVED with no error (expected: rejects with 23505)
rows in probe afterwards = 0 (0 = the transaction was rolled back)
unhandled rejections: 0
Expected: the second commitTransaction rejects with the commit error.
Which area(s) are affected?
db: postgres
Environment Info
payload: 3.90.2
@payloadcms/db-postgres: 3.90.2
@payloadcms/drizzle: 3.90.2 (latest on npm; the same code is on main today, and in 3.87.0)
drizzle-orm: 0.45.2
pg: 8.20.0
Node.js: 22.20.0
Next.js: not involved (the reproduction is a Local API script)
Postgres: 17.11 (stock postgres:17 container); also seen on Postgres 17 hosted by Neon
Activity
SebastianSchuetze commentedon Oct 14, 2026
I ran into the same issue in a small public test repo:
https://github.com/findmydoc-platform/payload-test-repo/tree/ae9d2f69a9be14914813003e59a84f37f3cce109
It uses Payload 3.88.0 with PostgreSQL and triggers a deferred constraint error during
COMMIT.commitTransaction()still resolves, even though PostgreSQL rolls the transaction back and the inserted row is not persisted. The reproduction is intentionally left unpatched.ibrahimalzubi commentedon Oct 18, 2026
Still present in 3.91.0 (latest) and 4.0.0-beta.2:
packages/drizzle/src/transactions/{begin,commit,rollback}Transaction.tshave the same git blobs atv3.90.2,v3.91.0,3.xandmain, and the publisheddist/transactions/*.jsfiles are byte-identical in 3.90.2, 3.91.0 and 4.0.0-beta.2.Some additions that may help triage:
It happens on ordinary Local API writes, not only with
payload.db. I reproduced it with a plain Node script (payload and @payloadcms/db-postgres 3.90.2, pg 8.20.0, PostgreSQL 17.4, Node 24.18): onepostscollection, plus aDEFERRABLE INITIALLY DEFERREDconstraint trigger that raises on every UPDATE ofposts.payload.update()resolves with the new value while the row keeps the old one:PostgreSQL logs
ERROR: deferred check rejected posts row 1withSTATEMENT: commiton Payload's connection, thenWARNING: there is no transaction in progressfor drizzle'srollback. So drizzle-orm did see the failure and rethrew it. Two controls behave as expected: withdisableTransaction: truethe samepayload.update()rejects withDrizzleQueryError(causeP0001), and a plain node-postgresBEGIN … COMMITrejects at COMMIT. In the failing case nothing is logged and there is no unhandled rejection.Not a regression from #16220. The handler on
donewas already a plain.catch(() => {})when #4582 (2023) maderesolve()returndone; #16220 only changed what the handler does. COMMIT errors have never reached the caller in the drizzle-based adapters.Reach, from reading the 3.91.0 source. Every operation that opens and commits its own transaction goes through this path: collection create/update/delete and their variants, global update, the auth operations,
updateJobsand the migration runner. That applies ondb-postgresanddb-vercel-postgresby default, and ondb-sqliteanddb-d1-sqlitewhentransactionOptionsis set. Some consequences once a COMMIT fails:loginreturns a token whose session row was rolled back, soauth/strategies/jwt.ts:180-184treats it as no user.logoutreturnstruewhile the session removal was rolled back, so the token keeps working.migrateneither logsError running migration …nor exits with code 1 (packages/drizzle/src/migrate.ts:110-116; onmainit would rethrow,:120-127); it moves on to the next migration, although the current one and itspayload-migrationsrow were rolled back (theMigrated:line at:100is printed before the commit either way).main, payload'scommitTransactionhelper (utilities/commitTransaction.ts:15-17) also runscommitFileOperations("Called only after the owning database transaction commits") after a failed COMMIT.Fixes: #17726 (into
3.x), #17727 and #18664 (intomain) are open for this. One related case that none of them can catch: if an earlier SQL statement in the transaction failed and its error was caught, PostgreSQL answersCOMMITwithROLLBACKand no error (the situation in #15517).Reproduction script (
repro.mjs, plain Node, no Next.js)Dependencies:
[email protected],@payloadcms/[email protected]and[email protected], in a package with"type": "module". SetREPRO_DATABASE_URLto the URL of an empty database and runnode repro.mjswithNODE_ENVunset, because it relies on the development schema push. The bug reproduced if the output includes a line starting withRESULT: REPRODUCED; exit code 1 alone can also mean a setup failure.SamOdum commentedon Oct 26, 2026
Confirming on 3.90.1 and checked against 3.92.0's published
@payloadcms/drizzle: a refused COMMIT still resolvescommitTransaction, sopayload.createreturns a document that was never saved.A minimal reproduction with no mocks: a
DEFERRABLE INITIALLY DEFERREDconstraint, which Postgres checks only at COMMIT.Insert
(1), (1)insideinitTransaction(req), thencommitTransaction(req)resolves, and no rows exist. A constraint trigger withRAISE EXCEPTIONon a collection's table shows the same throughpayload.create.We're running the change from #18664 (and #17726) as a
pnpm patchin production code. With it, both cases reject and our full suite passes. A review of either PR would let us drop the patch.DanRibbens commentedon Oct 29, 2026
This issue and the above linked PRs may have overlap with #19159 being worked on. I will review and coordinate what gets merged and closed.
chore(drizzle): propagate transaction commit failures
3li7alaki commentedon Nov 15, 2026
Confirming on @payloadcms/drizzle 3.93.0, the current release: its published
dist/transactions/beginTransaction.jsstill has the.catchthat settlesdone, so a refused COMMIT (deferred FK, real Postgres, no mocks) still resolvescommitTransactionwhile the rows are rolled back.#19159 merged on 2026-11-12, after 3.93.0 (published 2026-11-04), so no release carries the fix yet. Is it planned for the next 3.x release? We are running a backport of #19159 as a local patch until then.