Repository navigation
fix(drizzle): limit atomic updates with a locking subquery - #19214
Conversation
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
📦 esbuild Bundle Analysis for payloadThis analysis was generated by esbuild-bundle-analyzer. 🤖 |
AlessioGr
left a comment
There was a problem hiding this comment.
AI Review:
1. FOR UPDATE takes a stronger lock than the UPDATE needs
packages/drizzle/src/updateOne.ts:86
On main, the atomic path is a plain UPDATE … WHERE <where>. Postgres takes FOR NO KEY UPDATE row locks for that. The new candidate subquery uses .for('update'), which also conflicts with the FOR KEY SHARE lock that a foreign-key check takes when a row referencing the locked row is inserted.
This can hang a request in forgotPassword. The reservation runs on a separate autocommit connection (reservationReq.transactionID = undefined), while req keeps its own transaction open. If that transaction has already inserted a row with an FK to the user (from a beforeOperation hook, an outer caller transaction, a *_rels row, payload_preferences_rels, etc.), the reservation's SELECT … FOR UPDATE waits on req, and req waits on the reservation. Both are on the same Node request, so Postgres can't detect the deadlock and the request hangs.
In confirmOrder the stronger lock is held until the settlement commits. Concurrent inserts that reference the ecommerce transaction row block for that whole time.
Fix: match the lock the UPDATE takes anyway:
this.name === 'postgres' ? matchingRowQuery.for('no key update') : matchingRowQuery2. The where condition is sent twice
packages/drizzle/src/updateOne.ts:93
.where(and(eq(table.id, matchingRow), where))where is now in both the candidate subquery and the outer UPDATE. Every bound parameter is sent twice, and the database evaluates the condition twice.
On D1 (100 bound-parameter limit) and other SQLite builds with low limits, a condition with roughly 50+ parameters (e.g. an in list on a non-id column) fit on main but now fails.
The outer where looks redundant:
- Postgres (READ COMMITTED):
FOR UPDATE/FOR NO KEY UPDATEre-checkswhereagainst the latest row version before it locks the row, so the locked row already matches. - SQLite: writers are serialized and this is one statement, so the subquery and the UPDATE see the same data.
Fix: drop the outer where:
.where(eq(table.id, matchingRow))|
🚀 This is included in version v4.0.0-beta.9 |
Written with AI
Description
Atomic
updateOnepredicates can update every matching SQL row while returning only the first. Limit the write to one matching primary key using a subquery inside theUPDATE, retaining the original predicate on the outer write.Supersedes #19156 and addresses the subquery review suggestion. This replaces the separate candidate lookup and application retry loop with one SQL statement. PostgreSQL locks the candidate with
FOR UPDATE, allowing a waiting caller to continue to another matching row if a competing write makes the first candidate ineligible. SQLite uses the same subquery without a locking clause because it serializes writers.Changes
options.atomic === true, preserving main-table write validation and rejecting predicates requiring joins.nullwhen no document qualifies.This remains an independent prerequisite extracted from #19101. Branch-specific physical row-ID resolution and visibility remain in the content branching stack.
Test Plan
git diff --checkpassed.