Skip to content

🐛 Bug Report: Postgres: startsWith/contains/endsWith always scan the full table #14088

Closed

Description

@JoshiJoshiJoshi

In short: On Postgres, startsWith, endsWith and contains become ILIKE, and none of the index types the API can create can serve ILIKE. 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: startsWith 7 req/s (p50 8.5 s), a selective contains 5 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

  1. Run Appwrite 2.3.0 self-hosted with _APP_DB_ADAPTER=postgresql.
  2. Create a table with string columns name and note, a key index on each, and ≈ 3 M rows.
  3. List rows with Query.startsWith('name', 'Zimmer 99') or Query.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 startsWith and ×60–75 for the selective contains. pg_stat_user_indexes.idx_scan of 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

  1. Allow Database::INDEX_TRIGRAM as an index type in the TablesDB/Databases index API (and Console) when the adapter supports it, for string columns. This makes startsWith, endsWith and contains indexable for any pattern of three or more characters.
  2. Alternatively, create a trigram index automatically next to the B-tree for key indexes on string columns on Postgres, or as an option of the key index.
  3. 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_trgm must be installed. The official image ships it; external PostgreSQL installations may need CREATE 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?

🏢 Have you read the Code of Conduct?

  • I have read the Code of Conduct

Activity

  1. added
    product / databases
    product / self-hosted
    product / storage
    on Oct 4, 2026
  2. changed the title [-]🐛 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[/+] on Oct 4, 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 / databasesproduct / self-hostedproduct / storage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions