Skip to content

🐛 Bug Report: Postgres: fulltext search never uses an index #14081

Open

Description

@JoshiJoshiJoshi

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.search scans 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

  1. Run Appwrite 2.3.0 with _APP_DB_ADAPTER=postgresql.

  2. Create a string column note and a fulltext index on it through the API, on a table with ≈ 1 M rows.

  3. In Postgres, \d "appwrite"."_1_database_1_collection_2" shows a plain btree ("note") index.

  4. listRows with Query.search('note', 'word') is slow; EXPLAIN ANALYZE of the generated query shows a sequential scan that evaluates to_tsvector(...) for every row.

  5. 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"):

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 IMMUTABLE

Every 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

  1. Use an explicit, immutable configuration in both the query and the index. simple is 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')));
  2. 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.

  3. 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 search expression to_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_config applies (english in the official image): English stemming and stop words. With simple, rooms no longer matches room, 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 CONCURRENTLY to 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):

🎲 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?

Activity

  1. added
    product / databases
    product / functions
    product / migrations
    product / self-hosted
    product / sites
    product / storage
    on Oct 3, 2026
  2. JoshiJoshiJoshi commented on Oct 4, 2026

    @JoshiJoshiJoshi
    Author

    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 search attribute of users (ID, email, name, phone, labels) has a fulltext index, which the Postgres adapter creates as CREATE INDEX _1__users__key_search ON _1_users USING btree (search) (same for teams, _console_users, _console_teams). A B-tree entry is limited to 2 704 bytes, so PUT /v1/users/{userId}/labels fails with 500 general_unknown:

    PDOException: SQLSTATE[54000]: Program limit exceeded: 7 ERROR:  index row size 3304 exceeds btree version 4 maximum 2704 for index "_1__users__key_search"
    

    Limit found by bisection on 2.3.0 (Postgres 18), user with short ID/email/name:

    Labels First count that fails
    random, 36 characters (the allowed maximum length) 63
    random, 10 characters 216
    l0001, l0002, … (5 characters) 815

    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 tsvector expression instead of a B-tree on the raw column) would fix this too, since a GIN index has no such entry size limit.

  3. changed the title [-]🐛 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[/+] on Oct 4, 2026
  4. removed
    product / storage
    product / functions
    product / self-hosted
    product / migrations
    product / sites
    on Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    product / databases

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions