Skip to content

πŸ› Bug Report: PostgreSQL adapter never uses the _uid index – every create, get and update by row ID scans the whole tableΒ #14075

Open

Description

@JoshiJoshiJoshi

In short: Appwrite's Postgres tables have an index on the row ID, but the queries are written in a way that Postgres cannot use it. So every lookup by ID reads the whole table instead of jumping to the row.

Impact:

  • Creating, reading (uncached) and updating rows gets slower the bigger the table: on 1 M rows a single create takes β‰ˆ 0.5 s instead of β‰ˆ 25 ms (76 instead of 1,400 creates/s).
  • It also hits internal tables (users, sessions, teams, keys, transactions), so busy projects slow down over time without any obvious reason.
  • Small tables hide it, which is why it is easy to miss in development.

πŸ‘Ÿ Reproduction steps

  1. Run Appwrite 2.3.0 self-hosted with _APP_DB_ADAPTER=postgresql.

  2. Create a table, e.g. loadtest/writes with a few string/integer columns.

  3. Fill it with 1,000,000 rows (bulk create, 1,000 rows per request).

  4. Measure any of these requests:

    • POST /v1/tablesdb/{db}/tables/{table}/rows (create a single row)
    • GET /v1/tablesdb/{db}/tables/{table}/rows/{rowId} for a row that is not in the cache yet
    • PATCH /v1/tablesdb/{db}/tables/{table}/rows/{rowId} with new permissions on a table with row security
  5. Check the query plan in the appwrite-postgresql container, using the query exactly as Appwrite sends it:

    EXPLAIN ANALYZE SELECT "main".* FROM "appwrite"."_1_database_1_collection_2" AS "main" WHERE "main"."_uid" = '<rowId>';

    β†’ Parallel Seq Scan, all rows filtered. With SET enable_seqscan = off; the plan stays a (disabled) sequential scan.

  6. The same query with the collation of the index uses the index:

    EXPLAIN ANALYZE SELECT "main".* FROM "appwrite"."_1_database_1_collection_2" AS "main" WHERE "main"."_uid" COLLATE utf8_ci_ai = '<rowId>';

    β†’ Index Scan using _1_database_1_collection_2_uid, 0.03 ms instead of 30–300 ms.

Minimal reproduction in plain SQL (no Appwrite needed, ~1 minute)

Same collation and index definitions as utopia-php/database:

CREATE COLLATION IF NOT EXISTS utf8_ci_ai (provider = icu, locale = 'und-u-ks-level1', deterministic = false);

CREATE TABLE docs (_id bigserial PRIMARY KEY, _uid varchar(255) NOT NULL, name text);
CREATE UNIQUE INDEX docs_uid ON docs (_uid COLLATE utf8_ci_ai);

CREATE TABLE docs_perms (_id bigserial PRIMARY KEY, _type varchar(12) NOT NULL,
                         _permission varchar(255) NOT NULL, _document varchar(255) NOT NULL);
CREATE UNIQUE INDEX docs_ukey ON docs_perms (_document COLLATE utf8_ci_ai, _type, _permission);

INSERT INTO docs (_uid, name) SELECT md5(i::text), 'row ' || i FROM generate_series(1, 1000000) AS i;
INSERT INTO docs_perms (_document, _type, _permission)
SELECT md5(i::text), 'read', 'user:' || (i % 500) FROM generate_series(1, 1000000) AS i;
ANALYZE docs; ANALYZE docs_perms;

-- as issued by the adapter: sequential scans
EXPLAIN (ANALYZE, COSTS OFF) SELECT * FROM docs AS main WHERE main._uid = md5('123456');
EXPLAIN (ANALYZE, COSTS OFF) SELECT _uid, _id FROM docs WHERE _uid IN (md5('123456'));
BEGIN; EXPLAIN (ANALYZE, COSTS OFF) DELETE FROM docs_perms WHERE _document = md5('123456'); ROLLBACK;

-- index cannot be used even when sequential scans are disabled
SET enable_seqscan = off;
EXPLAIN (COSTS OFF) SELECT * FROM docs AS main WHERE main._uid = md5('123456');
RESET enable_seqscan;

-- with the index collation: index scans
EXPLAIN (ANALYZE, COSTS OFF) SELECT * FROM docs AS main WHERE main._uid COLLATE utf8_ci_ai = md5('123456');
BEGIN; EXPLAIN (ANALYZE, COSTS OFF) DELETE FROM docs_perms WHERE _document COLLATE utf8_ci_ai = md5('123456'); ROLLBACK;

Result on PostgreSQL 18.3: _uid = … 26 ms (Parallel Seq Scan), _uid IN (…) 24 ms (Parallel Seq Scan), DELETE … _document = … 52 ms (Seq Scan); with COLLATE utf8_ci_ai 0.13 ms and 0.06 ms (Index Scan).

πŸ‘ Expected behavior

Lookups by row ID ($id) use the unique index on _uid, and permission lookups use the index on _perms._document. Create, get and update by ID should cost about the same on a table with 1,000 rows as on a table with 1,000,000 rows.

πŸ‘Ž Actual Behavior

Every lookup by ID is a full sequential scan, so its cost grows linearly with the table size.

Cause. utopia-php/database (Postgres adapter, introduced in utopia-php/database#739) creates the indexes with the non-deterministic ICU collation utf8_ci_ai:

CREATE UNIQUE INDEX "…_uid"  ON … ("_uid" COLLATE utf8_ci_ai);
CREATE UNIQUE INDEX "…_ukey" ON …_perms (_document COLLATE utf8_ci_ai, _type, _permission);

The queries compare without COLLATE (column default collation), so PostgreSQL cannot use these indexes at all:

Links point to the current main / 7.4.0; Appwrite 2.3.0 ships 7.3.11 with the same code. This affects every table, including the internal ones: in our test instance (one project) all 69 _uid indexes and all 69 _perms._document indexes carry utf8_ci_ai, and lookups on _console_users, _console_keys, _console_teams, _1_users, _1_sessions, _1_teams, _1_memberships and _1_transactions show the same plan.

Measured through the API (Postgres on its own 4 vCPU node, two API nodes with 4 vCPU each, load from oha/k6). "Plain index" is a manually added CREATE INDEX … (_uid) or … _perms (_document) without collation, as a comparison:

Scenario As shipped With plain index
Create row, table with 1M rows, 40 concurrent 76 rows/s, p50 550 ms (reading back _id after the insert: 495 ms per call) 1,406 rows/s, p50 26 ms
Get row by ID, not cached, 1M rows, 20 concurrent 65 req/s, p50 312 ms (lookup 287 ms per call) 1,330 req/s, p50 11.5 ms (lookup 0.031 ms)
Update row with new permissions, 100k rows / 200k permission entries, 40 concurrent 383 req/s, p50 97 ms (DELETE FROM _perms 54.7 ms per call) 1,137 req/s, p50 33 ms (0.049 ms per call)
Share of Postgres time spent in these lookups on small tables (0–30k rows): create / bulk create (100 rows) / update / transaction commit 75 % / 91 % / 89 % / 89 % Postgres time per written row βˆ’86 % to βˆ’91 %

Transactions (/v1/tablesdb/transactions) are hit twice: every staged row is read back by _uid on commit, and _1_transactions is locked by _uid (WHERE _uid = $1 FOR UPDATE) – so commits get slower the more transactions have ever been created.

Suggested fix: compare with the index collation wherever _uid or _document is matched ("_uid" COLLATE utf8_ci_ai = :_uid, "_uid" COLLATE utf8_ci_ai IN (…), _document COLLATE utf8_ci_ai = :_uid). This keeps the case-insensitive uniqueness from utopia-php/database#739 and makes the existing indexes usable. Alternatively, declare the columns themselves with COLLATE utf8_ci_ai (needs a migration).

Workaround until then: an additional plain index per table (CREATE INDEX CONCURRENTLY … (_uid) and … _perms (_document)). It works, but it is unknown to Appwrite and has to be repeated for every new table.

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 from the official compose file), utopia-php/database 7.3.11.
  • Customizations in the test setup: Postgres moved to a separate node, max_connections raised, _APP_WORKER_PER_CORE varied between 6 and 30. None of these affect the result: the plain SQL reproduction above shows the same plans on a stock PostgreSQL 18 with only the adapter's collation and index definitions.
  • Requests sent with an API key (bulk and transactions require one); reads were anonymous on a table with read("any").

πŸ‘€ 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 / auth
    product / console
    product / databases
    product / locale
    product / messaging
    product / migrations
    product / self-hosted
    product / storage
    on Oct 3, 2026
  2. JoshiJoshiJoshi commented on Oct 4, 2026

    @JoshiJoshiJoshi
    Author

    End-to-end check on the same cluster (2 API nodes Γ— 4 vCPU, Postgres/Redis/ClickHouse on a third node, table with 3.3 M rows, 80 concurrent requests), stock 2.3.0 settings, before vs. with this fix plus #14078:

    before with the fixes
    Create row 16 req/s (p50 5.0 s) 1,527 req/s (p50 55 ms)
    Transactions with 10 creates each 2/s (37 s each) 162/s (0.24 s each)
    Writes during mixed read/write load 6 req/s (p50 4.1 s) 534 req/s (p50 70 ms)

    Before, each insert read β‰ˆ 2.5 M rows; afterwards a handful. Same hardware and settings in both runs, only the fixes differ.

  3. removed
    product / storage
    product / auth
    product / console
    product / self-hosted
    product / messaging
    product / migrations
    product / locale
    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