Repository navigation
Description
In short: On Postgres, a "fulltext" index created through Appwrite is a normal index that full-text search cannot use, and the search query is written in a way that can never use an index.
Impact:
- Every
Query.searchscans the whole table: 4 requests/s on a 1 M-row table, database at 100 % CPU.- With a proper full-text index the same search takes 0.18 ms instead of 611 ms.
- Users who created fulltext indexes believe search is indexed, but it is not.
👟 Reproduction steps
-
Run Appwrite 2.3.0 with
_APP_DB_ADAPTER=postgresql. -
Create a string column
noteand a fulltext index on it through the API, on a table with ≈ 1 M rows. -
In Postgres,
\d "appwrite"."_1_database_1_collection_2"shows a plainbtree ("note")index. -
listRowswithQuery.search('note', 'word')is slow;EXPLAIN ANALYZEof the generated query shows a sequential scan that evaluatesto_tsvector(...)for every row. -
An index on the expression the query uses cannot be created:
CREATE INDEX x ON "appwrite"."_1_database_1_collection_2" USING gin (to_tsvector(regexp_replace(note, '[^\w]+', ' ', 'g'))); -- ERROR: functions in index expression must be marked IMMUTABLE
👍 Expected behavior
A fulltext index created through Appwrite is usable by Query::search.
👎 Actual Behavior
Summary
On Postgres, a Database::INDEX_FULLTEXT index is created as an ordinary B-tree on the raw column, e.g. CREATE INDEX … ("note"):
Postgres.php#L885-L894mapsINDEX_FULLTEXTtoINDEX.L920uses the default B-tree.
Query::search is translated to this expression (Postgres.php#L1679-L1685):
to_tsvector(regexp_replace("note", '[^\w]+', ' ', 'g')) @@ websearch_to_tsquery(:value)A B-tree on note cannot answer this. Worse, the expression cannot be indexed at all: to_tsvector(text) without a text search configuration depends on default_text_search_config, so it is not IMMUTABLE:
CREATE INDEX x ON t USING gin (to_tsvector(regexp_replace(note, '[^\w]+',' ','g')));
-- ERROR: functions in index expression must be marked IMMUTABLEEvery search query is therefore a full table scan that computes to_tsvector for every row. That happens twice per Appwrite list request: once for the rows and once for total. The language configuration is also implicit (english by default), so the stemming of non-English text depends on a server setting.
Measurements
Table with ≈ 1 M rows; column note has a fulltext index created through the Appwrite API. Search for a word contained in 1,029 rows:
| Time | |
|---|---|
EXPLAIN ANALYZE of the generated query (25 rows, ORDER BY _id) |
611 ms, Parallel Seq Scan / Index Scan on pkey with filter over all rows |
Same search with to_tsvector('simple', …) and a GIN expression index |
0.18 ms (Bitmap Index Scan) |
| Through the API, 1 request (rows + total) | 740–754 ms |
| Through the API, 40 concurrent | 4 req/s, Postgres node at 100 % CPU |
Suggested fix
-
Use an explicit, immutable configuration in both the query and the index.
simpleis language-neutral; it could also be made configurable per index:-- query to_tsvector('simple', regexp_replace("note", '[^\w]+', ' ', 'g')) @@ websearch_to_tsquery('simple', :value) -- index for INDEX_FULLTEXT CREATE INDEX … USING gin (to_tsvector('simple', regexp_replace("note", '[^\w]+', ' ', 'g')));
-
For multi-column fulltext indexes, index the concatenation that the query uses (
coalesce(a,'') || ' ' || coalesce(b,'')), or create one expression per column and OR them in the query. -
Existing fulltext indexes need a migration (drop the B-tree, create the GIN expression index). A B-tree on long text can also fail on values above ≈ 2.7 KB (B-tree tuple size limit), which a GIN index does not.
Since when / regression?
Version numbers refer to utopia-php/database unless an Appwrite version is named.
- The
searchexpressionto_tsvector(regexp_replace(…))without a configuration has been in the adapter since 0.30.0. - All Appwrite releases with the Postgres adapter (2.0.0 onwards) are affected. It is not a regression.
Risks / things to consider for the fix
- Search results change. Today the server default
default_text_search_configapplies (englishin the official image): English stemming and stop words. Withsimple,roomsno longer matchesroom, and English stop words become searchable. Make the configuration explicit and ideally configurable per index, and document the change. - Migration on large tables: replacing B-tree "fulltext" indexes with GIN expression indexes means a rebuild. It should use
CREATE INDEX CONCURRENTLYto avoid write locks. - For multi-column indexes, the indexed expression and the query expression must match exactly, or the index is ignored.
Related: utopia-php/database#870 (SQLite got real FTS5-backed fulltext indexes), #14075 (same test setup).
Related issues from the same analysis (Appwrite 2.3.0 with Postgres, same test setup):
- 🐛 Bug Report: Two uncached team queries on every API request #14077 – every request runs two uncached queries for the project team
- 🐛 Bug Report: Postgres insert reads back
_idwith an extra query #14078 –_idread back after every insert instead ofINSERT … RETURNING - 🐛 Bug Report: Postgres adapter sets and resets
statement_timeoutaround every query #14079 –SET/RESET statement_timeoutaround every query - 🐛 Bug Report: Postgres adapter sends
ROLLBACKbefore everyBEGIN#14080 –ROLLBACKbefore everyBEGIN, Postgres log flood - 🐛 Bug Report:
total=falseon list endpoints is ignored #14082 –total=falseon list endpoints is ignored - 🐛 Bug Report: Pool adapter checks out a connection for every capability getter #14083 – pool adapter checks out a connection for constant capability getters
- 🐛 Bug Report: Usage queue backlog evicts the cache from Redis #14084 – usage queue backlog evicts the cache in the shared Redis
- 🐛 Bug Report: PostgreSQL adapter never uses the
_uidindex – every create, get and update by row ID scans the whole table #14075 – Postgres adapter never uses the_uidindex (collation)
🎲 Appwrite version
Version 2.3.0 (self-hosted)
💻 Operating system
Linux (Debian 12, Docker 29.8)
🧱 Your Environment
Self-hosted Appwrite 2.3.0 with _APP_DB_ADAPTER=postgresql, PostgreSQL 18.3 (appwrite/postgres:0.1.0), utopia-php/database 7.3.11. Test setup: Postgres, Redis and ClickHouse on one 4 vCPU node, the API on two separate 4 vCPU nodes, load from oha. The code in question lives in utopia-php/database; filed here because it ships with Appwrite and affects every Appwrite install that uses Postgres. Related test setup: #14075.
👀 Have you spent some time to check if this issue has been raised before?
- I checked and didn't find similar issue
🏢 Have you read the Code of Conduct?
- I have read the Code of Conduct
Activity
JoshiJoshiJoshi commentedon Oct 4, 2026
One more consequence of fulltext indexes being plain B-trees on Postgres, and this one is a hard error, not just slowness: users with more than a few dozen labels can't be saved.
The
searchattribute ofusers(ID, email, name, phone, labels) has a fulltext index, which the Postgres adapter creates asCREATE INDEX _1__users__key_search ON _1_users USING btree (search)(same forteams,_console_users,_console_teams). A B-tree entry is limited to 2 704 bytes, soPUT /v1/users/{userId}/labelsfails with500 general_unknown:Limit found by bisection on 2.3.0 (Postgres 18), user with short ID/email/name:
l0001,l0002, … (5 characters)The API allows up to 1 000 labels (
APP_LIMIT_ARRAY_LABELS_SIZE). Postgres compresses index entries, so repetitive labels get further; realistic labels don't. The request is rejected and the user keeps the old labels. We didn't find an existing issue for it.The suggested fix in this issue (a GIN index on an immutable
tsvectorexpression instead of a B-tree on the raw column) would fix this too, since a GIN index has no such entry size limit.[-]🐛 Bug Report: On Postgres, fulltext indexes are plain B-trees and `Query::search` can never use an index – every search scans the whole table[/-][+]🐛 Bug Report: Postgres: fulltext search never uses an index[/+]