Skip to content

fix(databases): allow trigram index type for Postgres string matching - #14108

Merged
HarshMN2345 merged 6 commits into
mainfrom
fix/postgres-trigram-index
Oct 13, 2026
Merged

HarshMN2345 merged 6 commits into
mainfrom
fix/postgres-trigram-index

Conversation

@HarshMN2345

Copy link
Copy Markdown
Member

Fixes #14088

On Postgres, startsWith, contains and endsWith compile to ILIKE, which none of the index types the API allows can serve, so these queries always scan the whole table. utopia-php/database already supports Database::INDEX_TRIGRAM (GIN with gin_trgm_ops). This change adds trigram to the index type whitelist (and SDK enum) of the Databases and TablesDB index create endpoints and to the inline index validator used when creating a collection or table.

Adapter support and column type are still checked by the library index validator that already runs in these endpoints. It uses getSupportForTrigramIndex(), which is true only on Postgres, and allows string attributes only, without orders or lengths. On MariaDB/MySQL and Mongo the request is rejected with "Trigram indexes are not supported". The worker already passes the type through to createIndex(). DocumentsDB (Mongo) is unchanged.

Verification, 300k rows on appwrite/postgres:

  • Before: the type param rejects trigram (allowed: key, fulltext, unique, spatial). With only the B-tree key index, name ILIKE 'Zimmer 99%' and note ILIKE '%Schimmel%' run as a Parallel Seq Scan (~95-110 ms).
  • After: trigram is accepted. The library validator accepts it on a string attribute and rejects it on $createdAt. The index it creates (USING gin (name gin_trgm_ops)) turns both queries into a Bitmap Index Scan (~1 ms).

@ghost

ghost commented Oct 5, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

Adds trigram as an index type for Databases and TablesDB direct and inline index creation, exposes it in the SDK enums, and relies on the database library validator for adapter, attribute, and option restrictions. Documents the PostgreSQL-only string-matching use case and adds E2E coverage for successful creation and rejection cases. The latest changes also expose adapter trigram support through Console Variables and use that capability in the E2E test instead of checking the configured adapter name.

Latest changes: The newest commits add supportForTrigramIndex to Console Variables and make the E2E test branch on the adapter's reported capability rather than its configured adapter string.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 11
File Change
docs/references/databases/create-index.md Documents trigram as a PostgreSQL-only option for string matching indexes.
docs/references/tablesdb/create-index.md Documents trigram as a PostgreSQL-only option for string matching indexes.
src/Appwrite/Platform/Modules/Console/Http/Variables/Get.php Includes adapter trigram-index support in Console Variables.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Create.php Allows trigram in inline collection index documentation and validation.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Indexes/Create.php Accepts trigram for direct index creation and adds it to the Databases SDK enum.
src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Create.php Allows trigram in inline table index documentation and validation.
src/Appwrite/Platform/Modules/Databases/Http/TablesDB/Tables/Indexes/Create.php Accepts trigram for direct index creation and adds it to the TablesDB SDK enum.
src/Appwrite/Utopia/Database/Validator/Indexes.php Allows trigram in inline index validation.
src/Appwrite/Utopia/Response/Model/ConsoleVariables.php Defines the supportForTrigramIndex response field.
tests/e2e/Scopes/Scope.php Adds a helper to read trigram support from Console Variables.
tests/e2e/Services/Databases/DatabasesBase.php Covers trigram index creation and validation outcomes based on adapter capability.

Merged main (d277b8a); no changes to this PR's code since a1aea58 · 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

@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/postgres-trigram-index (after).

Metric Before After Change
🚀 Requests/sec 205.88 219.34 🟢 +6.5%
⏱️ Latency P50 82.4 ms 80.76 ms ⚪ -2%
⏱️ Latency P95 204.58 ms 177.15 ms 🟢 -13.4%
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 80.76 177.15 13,680 219.34 -27.43
Account 144.06 262.66 720 12.05 -67.62
TablesDB 78.32 136.04 7,440 120.84 -11.42
Storage 75.65 160.43 3,600 60.71 -11.18
Functions 114.25 213.19 1,920 33.14 -37.01

Top API waits (after)

API request Max wait (ms)
functions.variables.update 417.31
account.prefs.update 409.96
account.name.update 399.93
storage.files.create 379.18
functions.variables.create 359.7

PostgreSQL builds a trigram index through the index endpoint and inline
on create, and refuses one on a non-string attribute or with orders.
Other adapters reject the type as unsupported on both paths.
@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

19 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (12)
  • 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.

The create-index references for Databases and TablesDB now list every
type the endpoints accept, and note that trigram is PostgreSQL only and
serves contains, startsWith and endsWith on string attributes.

@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

HarshMN2345 and others added 2 commits October 13, 2026 11:54
Expose supportForTrigramIndex in the console variables and read it
through a getSupportForTrigramIndex() scope getter instead of checking
_APP_DB_ADAPTER directly.
@HarshMN2345
HarshMN2345 merged commit 889e9f2 into main Oct 13, 2026
12 checks passed
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: Postgres: startsWith/contains/endsWith always scan the full table

2 participants