Skip to content

fix(databases): return the committed row from get after a transaction - #14201

Merged
HarshMN2345 merged 8 commits into
mainfrom
cursor/fix-transaction-getrow-cache-a18a
Oct 12, 2026
Merged

HarshMN2345 merged 8 commits into
mainfrom
cursor/fix-transaction-getrow-cache-a18a

Conversation

@HarshMN2345

@HarshMN2345 HarshMN2345 commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

After a transaction commit, getRow could keep serving the pre-commit row (or a 404 for a row the commit created) until the next write to that row or the 24h document-cache TTL.

The commit endpoint runs every staged write inside one outer withTransaction(), so each write's cache purge runs inside a savepoint, before the real COMMIT. A read in that window misses the cache, reads the still-committed old row, and caches it.

The commit now runs under Cache::recordPurges(), which records every key purged while it runs, and purges those keys again once the outer transaction returns. This covers every write path the database purges, including relationship writes it runs silently. A cache error is logged and does not fail a commit that already landed.

utopia-php/database 8.x purges after the outermost commit itself, so this can go when Appwrite moves to it.

Test Plan

testGetAfterCommitWithConcurrentReads stages an update plus 90 filler creates and keeps 8 readers polling getRow for the whole commit. It fails on main and passes with this change.

Local repro (PostgreSQL, Redis), 10 rounds per write type:

Staged write main this PR
update, delete, create, increment, bulkUpdate, bulkDelete 10/10 stale each 0/10

Legacy transactions server, client and ACID e2e suites pass.

Related PRs and Issues

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

🤖 Generated with Claude Code

A staged commit runs each write inside an outer transaction, so
updateDocument() purges the document cache when its savepoint
releases, before COMMIT. A get in that window caches the pre-commit
row and serves it until the next write or the cache TTL.

Purge every row the commit wrote once that outer transaction returns.

Fixes #14173
@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

32 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (17)
  • php.appwrite.permissive-write-permission (8)
  • php.appwrite.secret-compare-timing (5)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → cursor/fix-transaction-getrow-cache-a18a (after).

Metric Before After Change
🚀 Requests/sec 221.23 228.1 ⚪ +3.1%
⏱️ Latency P50 79.17 ms 74.87 ms 🟢 -5.4%
⏱️ Latency P95 182.39 ms 178.52 ms ⚪ -2.1%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 74.87 178.52 14,250 228.1 -3.87
Account 152.84 271.81 750 12.43 +2.28
TablesDB 72.93 131.24 7,750 126.23 -13.97
Storage 67.42 141.76 3,750 62.89 -13.49
Functions 116.74 227.29 2,000 34.32 +22.23

Top API waits (after)

API request Max wait (ms)
account.name.update 521.77
account.prefs.update 419.83
functions.variables.update 390.02
functions.variables.delete 347.45
functions.create 340.3

HarshMN2345 and others added 3 commits October 7, 2026 09:27
PHPStan does not resolve Utopia\Console\Console. Log a failed purge
through Utopia\Console, matching the other database actions, so a
cache error still cannot fail a commit that already landed.
…t state

applyBulkDeleteToState() matches the delete's queries in memory and ignores
limit, cursor and filters it cannot evaluate, so it can unset rows from $state
that the database kept. Those rows then missed the post-commit purge, and a
read in the savepoint-to-COMMIT window left a stale entry (a 404 for a row
that exists). Record every row still in $state for the collection before that
call.

Also trim the purge bookkeeping: $state keys are always either an operation's
row id, which is already recorded, or the row's own id, so collect only the
row ids. Drop the no-op casts and empty-id guards, and name the Database
parameter $dbForDatabases like the other handlers.
A single sequential client never reads between the in-savepoint purge and
COMMIT, so the test passes with or without the post-commit purge.
testUpdateDocument already covers a get after commit with a warmed cache.
@HarshMN2345
HarshMN2345 marked this pull request as ready for review October 7, 2026 09:51
@luxor-code-review

luxor-code-review Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Review complete. No issues found — approved ✅.


This change updates the transactions Update endpoint to track documents written during a transaction and purge them from cache once the transaction commits.

Files Change
src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php Tracks written/deleted documents per collection during commit or rollback and purges their cache entries afterward.

Reviewed commit: ff08776

Copy link
Copy Markdown
Member Author

@hansi-codes review

@ghost

ghost commented Oct 7, 2026 •

Copy link
Copy Markdown

🔵 Tier A · Mergeable after minor fixes

The implementation change is covered for direct updates, but the revised suite drops coverage for nested updates and cascade deletes.

Database transaction commits now record cache purges made during their writes and replay them after the outer transaction commits. An E2E regression test polls a row while a staged update and filler creates are committed.

Latest changes: The latest commits replace explicit row and relationship ID collection with recording all cache purges during the transaction and replaying them after COMMIT, and simplify the concurrency regression to one update-plus-filler test.

Verdict New comments Fixed Still open
✅ Approved 1 1 0
Finding Where
🟡 Keep coverage for implicit relationship and cascade purges tests/e2e/Services/Databases/Transactions/TransactionsBase.php:2135
Fix with agent prompt
### Issue 1
tests/e2e/Services/Databases/Transactions/TransactionsBase.php:2135-2141
**Keep coverage for implicit relationship and cascade purges**

This revision removes the nested-update and cascade-delete concurrency tests, leaving only a direct-row update case. Since the new approach relies on the database layer issuing purge calls for implicit relationship and cascade writes, a missed purge in either path would now pass this suite; retaining coverage for those cases would protect the broader behavior.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
📂 Walkthrough · 3
File Change
packages/cache/src/Cache.php Add an API to record cache purge keys during a callback, including overlapping recordings.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php Record purges during the outer transaction and replay them after commit, logging thrown cache errors.
tests/e2e/Services/Databases/Transactions/TransactionsBase.php Exercise concurrent reads during a staged update commit with filler creates.
✅ Fixed since the last review · 1
  • Discover cascade targets without read-permission filtering · src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php:881

Merged main (6da03d1); no changes to this PR's code since 12e1087 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

A nested child written with its parent is purged inside the savepoint,
so a concurrent get can cache the pre-commit child. Record those related
rows with the parent, and overlap readers with the commit so the
following get must return the committed value.

Fixes #14173
@cursor

cursor Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Nested related rows are now included in the post-commit purge. A staged create, update, or upsert records those child ids from the operation payload and from the row written when the nested document had no id yet, then purges them after the outer transaction returns.

testConcurrentGetAfterCommit overlaps readers with the commit and checks the following get. testConcurrentGetOfRelatedRowAfterCommit does the same for a one-way nested child. The in-memory bulk-delete purge is unchanged.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

Copy link
Copy Markdown
Member Author

@hansi-codes review

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

Comment on lines +654 to +655
if (!\in_array($action, ['create', 'update', 'upsert'], true)) {
return;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include related rows affected by staged deletes

A staged delete with onDelete: cascade also deletes related rows, but this early return excludes them, and neither $written nor $state records those child IDs. A concurrent child read can therefore re-cache the pre-delete row before COMMIT and keep returning it afterwards; please capture and purge the affected related IDs for delete operations too.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php
Line: 654-655

Comment:
**Include related rows affected by staged deletes**

A staged delete with `onDelete: cascade` also deletes related rows, but this early return excludes them, and neither `$written` nor `$state` records those child IDs. A concurrent child read can therefore re-cache the pre-delete row before COMMIT and keep returning it afterwards; please capture and purge the affected related IDs for delete operations too.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

🟡 Minor · bug · Reply if this doesn't apply.

A staged delete with onDelete cascade removes related rows inside the savepoint, so a concurrent get can cache the pre-delete child. Record those ids before the delete and purge them after the outer commit.

Fixes #14173.
@cursor

cursor Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Staged deletes with onDelete: cascade now record the related rows (including junction rows) before deleteDocument, while those rows still exist. The post-commit purge drops them, so a child read that overlaps the commit cannot keep the pre-delete row.

testConcurrentGetOfCascadedRowAfterCommit overlaps readers with that commit and expects the cascaded child GET to 404.

Copy link
Copy Markdown
Member Author

@hansi-codes review

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · See the inline comments. Summary

@ghost

ghost commented Oct 7, 2026

Copy link
Copy Markdown

Already reviewed 28e637f. No new commits since then, so I didn't post another review.

Record the cache keys purged while the commit's outer transaction runs and
purge them again once it returns, instead of rebuilding the written row set
from operations, state and relationship metadata. This covers every write
path the database layer purges, including relationship writes it runs
silently.

Rows in collections with relationship attributes are never cached, so the
nested and cascade discovery is dropped along with its tests. The remaining
test polls getRow for the whole commit and fails without the fix.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

Comment on lines +2135 to +2141
$operations = [[
'action' => 'update',
'databaseId' => $databaseId,
$this->getContainerIdParam() => $collectionId,
$this->getRecordIdParam() => $documentId,
'data' => ['name' => 'done'],
]];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep coverage for implicit relationship and cascade purges

This revision removes the nested-update and cascade-delete concurrency tests, leaving only a direct-row update case. Since the new approach relies on the database layer issuing purge calls for implicit relationship and cascade writes, a missed purge in either path would now pass this suite; retaining coverage for those cases would protect the broader behavior.

Prompt To Fix With AI
This is a comment left during a code review.
Path: tests/e2e/Services/Databases/Transactions/TransactionsBase.php
Line: 2135-2141

Comment:
**Keep coverage for implicit relationship and cascade purges**

This revision removes the nested-update and cascade-delete concurrency tests, leaving only a direct-row update case. Since the new approach relies on the database layer issuing purge calls for implicit relationship and cascade writes, a missed purge in either path would now pass this suite; retaining coverage for those cases would protect the broader behavior.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

🟡 Minor · testing · Reply if this doesn't apply.

@HarshMN2345
HarshMN2345 merged commit 14af5b8 into main Oct 12, 2026
20 checks passed
abnegate added a commit that referenced this pull request Oct 12, 2026
Brings in main's post-commit cache purges for transactions (#14201).
It merges cleanly onto the database 8.0 API.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Bug Report: getRow returns the pre-commit row after a transaction

2 participants