Skip to content

fix(databases): chunk batch duplicate lookup by max query values - #15017

Merged
ChiragAgg5k merged 1 commit into
mainfrom
fix/batch-duplicate-query-values
Oct 27, 2026
Merged

ChiragAgg5k merged 1 commit into
mainfrom
fix/batch-duplicate-query-values

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

What

A batch create (or bulk upsert) that hits a duplicate ID looks up which ID is already stored so the error can name it. That lookup put every ID in the batch into one Query::equal('$id', ...), and when a batch had more than 500 IDs the query validator refused it. The caller got a 500 (Invalid query: Query on attribute has greater than 500 values: $id) instead of the 409.

getBatchDuplicateException() now looks the IDs up in chunks of $dbForDatabases->getMaxQueryValues() and stops at the first chunk that finds a stored ID, so the caller gets row_already_exists / document_already_exists naming that ID.

Test

testBulkCreatePastTheQueryValuesLimitNamesTheExistingId batch-creates APP_DATABASE_QUERY_MAX_VALUES + 1 rows with an existing ID last, so it sits in the second chunk, and expects a 409 naming that ID. It runs for every API that uses the ErrorMessages trait.

The test stack's .env sets _APP_LIMIT_DATABASE_BATCH=1000 because the self-hosted default of 100 rejects the batch before it reaches the database. Cloud plans already allow larger batches through databasesBatchSize.

Fixes CLOUD-3RBE

Related: appwrite-labs/cloud#5938

A batch write over 500 IDs that hit a duplicate asked findOne for every ID at once, so the query validator refused it and the caller got a 500 instead of the already-exists error naming the stored ID.
@luxor-code-review

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

Copy link
Copy Markdown

Review complete. 🟡 1 medium

💬 Inline comments (1)


The change makes the bulk-write duplicate lookup split ID lists into chunks sized by the database adapter query-value limit instead of issuing one oversized IN query. The e2e error-message suite gains a case covering a batch past the query-values limit, and .env raises the batch limit so the new test can exercise the code path.

Files Change
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php Chunks the duplicate-ID findOne lookup by getMaxQueryValues() (with a max(1, ...) guard) and keeps the authorization skip scope per iteration.
tests/e2e/Services/Databases/ErrorMessages.php Adds testBulkCreatePastTheQueryValuesLimitNamesTheExistingId, sending APP_DATABASE_QUERY_MAX_VALUES + 1 records.
.env Sets _APP_LIMIT_DATABASE_BATCH=1000 so the new test 501-record batch is accepted.

Reviewed commit: 4485cc4

@ghost

ghost commented Oct 27, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The duplicate-ID lookup now splits submitted IDs into chunks sized to the database query-value limit and returns the first stored duplicate found. The E2E suite adds coverage for an existing ID beyond the first chunk, and the local test environment raises its batch limit to allow that request size.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 3
File Change
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php Chunks duplicate-ID lookups using the database query-value limit.
tests/e2e/Services/Databases/ErrorMessages.php Covers a bulk create whose existing ID falls in the second lookup chunk.
.env Raises the local database batch limit for the larger E2E request.

Reviewed 4485cc4 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@ChiragAgg5k ChiragAgg5k self-assigned this Oct 27, 2026

@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

@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

6 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (3)
  • php.appwrite.secret-compare-timing (1)
  • 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.

@luxor-code-review luxor-code-review Bot 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.

This PR chunks the duplicate-ID lookup for bulk document writes to stay under per-adapter query value limits, with a matching e2e error-message test.

Key findings

}

public function testBulkCreatePastTheQueryValuesLimitNamesTheExistingId(): void
{

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 test · medium

E2E limit test needs batch env above 501

testBulkCreatePastTheQueryValuesLimitNamesTheExistingId sends APP_DATABASE_QUERY_MAX_VALUES + 1 (501) records, which only reaches the duplicate check when _APP_LIMIT_DATABASE_BATCH >= 501; with the code default of 100 in app/init/constants.php the request fails ArrayList validation with 400 before the code under test runs.

@github-actions

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/batch-duplicate-query-values (after).

Metric Before After Change
🚀 Requests/sec 220.87 223.28 ⚪ +1.1%
⏱️ Latency P50 77 ms 78.83 ms ⚪ +2.4%
⏱️ Latency P95 187.84 ms 177.02 ms 🟢 -5.8%
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 78.83 177.02 13,680 223.28 -10.82
Account 144.79 261.04 720 12.43 -30.58
TablesDB 77.89 146.99 7,440 123.73 +8.49
Storage 68.62 158.48 3,600 61.61 -8.31
Functions 106.83 197.06 1,920 33.63 -33.91

Top API waits (after)

API request Max wait (ms)
functions.create 425.58
tablesdb.rows.create 414.54
account.name.update 410.28
functions.variables.create 357.23
account.prefs.update 335.06

@ChiragAgg5k
ChiragAgg5k merged commit 519bafa into main Oct 27, 2026
70 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/batch-duplicate-query-values branch October 27, 2026 14:41
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.

1 participant