Skip to content

🐛 Bug Report: Operators in a transaction bulkUpdate are applied twice #14217

Closed

Description

@JoshiJoshiJoshi

👟 Reproduction steps

  1. Table with an integer column n and a varchar column grp; create rows r0…r2 with n = 1, grp = "g" (with createRows, outside a transaction).
  2. 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\"]}"]}}
    ]
  3. Commit → 200, status committed. 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

  1. added
    product / databases
    product / vcs
    sdk / cli
    on Oct 7, 2026
  2. added a commit that references this issue on Oct 12, 2026
    f1dd85b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    product / databasesproduct / vcssdk / cli

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions