Repository navigation
Description
👟 Reproduction steps
- Table with an integer column
nand a varchar columngrp; create rowsr0…r2withn = 1,grp = "g"(withcreateRows, outside a transaction). - Create a transaction and stage two operations:
[ {"action": "bulkUpdate", "databaseId": "db", "tableId": "t", "data": {"data": {"n": "{\"method\":\"increment\",\"values\":[2]}"}, "queries": ["{\"method\":\"equal\",\"attribute\":\"grp\",\"values\":[\"g\"]}"]}}, {"action": "bulkUpdate", "databaseId": "db", "tableId": "t", "data": {"data": {"n": "{\"method\":\"multiply\",\"values\":[3]}"}, "queries": ["{\"method\":\"equal\",\"attribute\":\"grp\",\"values\":[\"g\"]}"]}} ] - Commit →
200, statuscommitted. Read the rows.
👍 Expected behavior
n = (1 + 2) × 3 = 9 – each staged operation is applied once, as when the same two updateRows calls run outside a transaction (that gives 9).
👎 Actual Behavior
n = 27: the multiply is applied twice. Every bulkUpdate with an operator is applied twice to each row that the transaction already touched (created, updated, or matched by an earlier bulk operation). Rows the transaction has not touched before are updated once.
Staged operations (start n = 1 unless noted) |
Expected | Actual |
|---|---|---|
bulkUpdate increment 2 |
3 | 3 |
bulkUpdate increment 1, bulkUpdate increment 1 |
3 | 4 |
5 × bulkUpdate increment 1 (start 0) |
5 | 9 |
bulkUpdate increment 2, bulkUpdate multiply 3 |
9 | 27 |
bulkUpdate set n = 5, bulkUpdate multiply 3 |
15 | 45 |
bulkCreate (n = 1), bulkUpdate increment 1 |
2 | 3 |
create (n = 1), bulkUpdate increment 1 |
2 | 3 |
update r0 of another column, bulkUpdate increment 1 |
all 2 | r0 = 3, others 2 |
2 × bulkUpdate decrement 3 with min 0 (stock 10) |
4 | 1 |
bulkUpdate arrayAppend "a", bulkUpdate arrayAppend "b" |
["a","b"] |
["a","b","b"] |
bulkUpdate stringConcat "a", bulkUpdate stringConcat "b" (start "x") |
"xab" |
"xabb" |
Every row of the table was reproduced in two runs with identical results. The commit response reports success (operations: 2, status: committed), so the client cannot notice the wrong values. Typical patterns are affected: creating rows and adjusting a counter in the same transaction, or decrementing stock twice in one checkout.
Cause: in handleBulkUpdateOperation() (Transactions/Update.php L888–L944) TransactionState::applyBulkUpdateToState() (L236–L257) copies the update data – including the Operator object – into the state documents of rows already in the transaction state. updateDocuments() then applies the operator in SQL (first application). For these "dependent" rows the documents are afterwards re-written from the state with upsertDocuments() (L924–L940); the state document still contains the Operator, so the upsert applies it a second time. With plain values the re-write is idempotent, which is why only operators are affected. main has the same code.
Related, same handler family: a bulkUpsert operation with any operator always fails at commit with 400 Invalid document structure: Attribute "n" has invalid format (existing rows, new rows, increment and stringConcat, two runs each), while upsertRows with the same operators works outside a transaction.
🎲 Appwrite version
2.3.0
💻 Operating system
Linux (Debian 12), Docker
🧱 Your Environment
Self-hosted Appwrite 2.3.0, Postgres adapter (PostgreSQL 18), two API nodes.
💡 Suggested fix
In applyBulkUpdateToState(), apply operators to the state document (compute the value) instead of storing the Operator object, or – simpler – take the documents returned by updateDocuments() (onNext) as the new state for dependent rows instead of re-writing them from the state.
👀 Have you spent some time to check if this issue has been raised before?
- I checked and didn't find a similar issue
🏢 Have you read the Code of Conduct?
- I have read the Code of Conduct
Activity
fix(databases): apply transaction bulk updates once