Repository navigation
Description
In short: On Postgres,
startsWith,endsWithandcontainsbecomeILIKE, and none of the index types the API can create can serveILIKE. Every such query reads the whole table. Postgres has an index type that fits (trigram,pg_trgm), and Appwrite's database library already supports it, but the API does not offer it.Impact:
- On 3.3 M rows:
startsWith7 req/s (p50 8.5 s), a selectivecontains5 req/s (p50 4.4 s), with the database at 100 % CPU.- With a trigram index created by hand: 130–154 and 308–374 req/s (×20–75), p50 ≈ 0.1–0.3 s.
- Fix: allow the trigram index type in the index API on Postgres.
👟 Reproduction steps
- Run Appwrite 2.3.0 self-hosted with
_APP_DB_ADAPTER=postgresql. - Create a table with string columns
nameandnote, akeyindex on each, and ≈ 3 M rows. - List rows with
Query.startsWith('name', 'Zimmer 99')orQuery.contains('note', 'Schimmel')(few matches), e.g. 40 concurrent requests with an API key.
👍 Expected behavior
A query that matches 0.03–0.3 % of the rows uses an index and returns in milliseconds, like other filtered queries on indexed columns.
👎 Actual Behavior
The Postgres adapter turns string matching into ILIKE (getLikeOperator(), patterns at L1740):
| Query | SQL |
|---|---|
startsWith('name', 'Zimmer 99') |
name ILIKE 'Zimmer 99%' |
contains('note', 'Schimmel') |
note ILIKE '%Schimmel%' |
The index types the API offers (TablesDB/Tables/Indexes/Create.php#L66: key, fulltext, unique, spatial) all create B-tree indexes for string columns. A B-tree cannot serve ILIKE, not even for a prefix. So every such query reads the whole table, together with the count for total.
The adapter already knows the right index type: Database::INDEX_TRIGRAM creates a GIN index with gin_trgm_ops (L893, L915), and the appwrite/postgres image ships pg_trgm. The API just doesn't allow it.
Measured. Table with 3.3 M rows (468 MB), API on two nodes with 4 vCPU each, 40 concurrent requests with total, 10 s per step. The trigram indexes were created directly in Postgres. Order: with index → without → with (re-created).
| Query (matches) | with trigram index | without (today) | with trigram index again |
|---|---|---|---|
startsWith(name, 'Zimmer 99') (11 000, 0.3 %) |
130 req/s, p50 317 ms | 7 req/s, p50 8.5 s | 154 req/s, p50 268 ms |
contains(note, 'Schimmel') (1 029, 0.03 %) |
308 req/s, p50 128 ms | 5 req/s, p50 4.4 s | 374 req/s, p50 109 ms |
contains(note, 'Minibar') (2.28 M, 70 %) |
16 req/s | 16 req/s | 16 req/s |
- With the trigram index: ×19–22 for
startsWithand ×60–75 for the selectivecontains.pg_stat_user_indexes.idx_scanof the trigram indexes rises in every run with the index, so Postgres uses it. - Without it, the Postgres node is at 100 % CPU while the API nodes idle at 1–3 %. A few such queries slow down every other query on the same database.
- When most rows match (70 %), no index helps: the count alone touches ≈ 800 000 rows per request.
- Index size: 155 MB (
note) and 59 MB (name) for a 468 MB table; creating both took 10 s.
🎲 Appwrite version
2.3.0
💻 Operating system
Linux (Debian 12), Docker
🧱 Your Environment
Self-hosted, _APP_DB_ADAPTER=postgresql (PostgreSQL 18, appwrite/postgres image). Postgres, Redis and ClickHouse on one node, the API on two nodes with 4 vCPU each (_APP_WORKER_PER_CORE=2, _APP_WORKER_MAX_COROUTINES=16).
💡 Suggested fix
- Allow
Database::INDEX_TRIGRAMas an index type in the TablesDB/Databases index API (and Console) when the adapter supports it, for string columns. This makesstartsWith,endsWithandcontainsindexable for any pattern of three or more characters. - Alternatively, create a trigram index automatically next to the B-tree for
keyindexes on string columns on Postgres, or as an option of thekeyindex. - Document that on Postgres, string matching queries scan the whole table unless such an index exists.
Since when / regression?
Not a regression. ILIKE has been the like-operator of the Postgres adapter since it exists, and INDEX_TRIGRAM has been in the adapter since utopia-php/database 4.5.0 but was never exposed by the Appwrite API. All Appwrite releases with the Postgres adapter are affected.
Risks / things to consider for the fix
- Trigram GIN indexes are large (here about a third of the table) and slow down writes to the indexed columns.
- Patterns shorter than three characters cannot use a trigram index.
pg_trgmmust be installed. The official image ships it; external PostgreSQL installations may needCREATE EXTENSION pg_trgm.- MariaDB/MySQL and MongoDB need their own answer (or the index type stays Postgres-only).
👀 Have you spent some time to check if this issue has been raised before?
- I checked and didn't find a similar issue (related: 🐛 Bug Report: Postgres: fulltext search never uses an index #14081, 🐛 Bug Report:
total=falseon list endpoints is ignored #14082)
🏢 Have you read the Code of Conduct?
- I have read the Code of Conduct
Activity
[-]🐛 Bug Report: On Postgres, `startsWith` / `contains` / `endsWith` always scan the whole table – no index type the API can create helps; a trigram index makes them 20–75× faster[/-][+]🐛 Bug Report: Postgres: `startsWith`/`contains`/`endsWith` always scan the full table[/+]