Skip to content

🐛 Bug Report: Pool adapter checks out a connection for every capability getter #14083

Open

Description

@JoshiJoshiJoshi

In short: For simple questions like "does this database support X?" or "what is the host name?", whose answers never change, the database layer borrows a connection from the pool and resets all its settings, many times per request.

Impact:

  • About 10 % of the API's CPU time goes into this bookkeeping.
  • Together with a related fix that is already upstream, cached reads gained +16 to +18 % under full load.

👟 Reproduction steps

  1. Run Appwrite 2.3.0 (any SQL adapter; measured with Postgres).
  2. Put load on a cheap endpoint, e.g. cached GET /v1/tablesdb/{db}/tables/{table}/rows/{rowId}.
  3. Profile the API workers (we used the excimer extension, CPU sampling per Swoole worker).
  4. Utopia\Database\Adapter\Pool::delegate accounts for 21–31 % of API CPU. About 9–12 % of total API CPU is spent in delegate calls for getters such as getSupportForAttributes, getSupportForCasting, getSupportForHostname, getHostname, getMaxUIDLength, getMinDateTime. Each of these checks out a pooled connection.

👍 Expected behavior

Getters that return a constant for a given adapter and DSN are answered without checking out a connection or re-applying the adapter state.

👎 Actual Behavior

Summary

Adapter\Pool forwards every adapter method through delegate() (Pool.php#L59-L96). That includes getters whose result never changes for a given adapter, for example:

  • getSupportForHostname() (L650), getSupportForAttributes() (L520) and the other getSupportFor* methods
  • getHostname() (L241), getMaxUIDLength() (L505), getMinDateTime(), getIdAttributeType()

Each of these calls goes through the full path:

  • $this->pool->use(…) checks out a connection (lock, channel, telemetry)
  • setDatabase, setNamespace, setSharedTables, setTenant, setAuthorization, syncTimeouts, debug and metadata are applied again
  • the getter is called, and the connection is returned

These getters sit on the hot path. Database::getCacheBaseKeys() calls getSupportForHostname() and getHostname() on every getDocument() (Database.php#L10114-L10120). Casting, validation and key handling ask further capabilities per document.

Measurements

CPU profile of the Appwrite API process (excimer, 30 s at 80 concurrent connections), share of API CPU time:

Cached row read List rows Create row
Adapter\Pool::delegate in total 23.9 % 30.6 % 21.3 %
… of that for constant capability getters ≈ 12 % ≈ 11 % ≈ 9 %

A test patch memoized the results of these getters per pool object, without a checkout. We measured it only together with a second change, the cache-key filter hashing that utopia-php/database#980 already addresses upstream. The two effects cannot be separated from our data:

Request Concurrency req/s before → after (both changes) p50 / p99 before → after (ms)
Cached row read 20 2,073 → 2,076 9.5 / 19.3 → 9.4 / 28.1
Cached row read 80 2,286 → 2,700 (+18 %) 30.4 / 138.0 → 25.2 / 95.3
Cached row read 160 2,024 → 2,350 (+16 %) 74.1 / 244.3 → 66.8 / 225.6
List rows 80 1,654 → 1,757 (+6 %) 44.5 / 154.2 → 40.7 / 132.1
List rows 160 1,460 → 1,624 (+11 %) 81.3 / 353.1 → 95.5 / 262.1
Create row 80 1,602 → 1,618 (±1 %) 43.7 / 159.0 → 44.8 / 142.8

The changes save pure CPU, so they show up only when the API nodes are CPU-bound (from 80 concurrent connections in our setup). At 20 concurrent there is no difference in throughput, but API CPU dropped from 85–90 % to 77 %.

Suggested fix

  • Answer constant capability getters without a checkout. Options:
    • resolve them once per pool, from the first adapter instance (the adapter class and DSN do not change within a pool) and cache them on the Pool object
    • or make them static on the adapter class where they really are constants
  • getHostname() depends on the DSN, which is fixed per pool, so it can be cached the same way.
  • Keep delegate() with full state sync for everything that talks to the server or depends on per-call state.

Since when / regression?

Version numbers refer to utopia-php/database unless an Appwrite version is named.

Pool::delegate() has existed since 0.64.0. All Appwrite releases with the Postgres adapter (2.0.0 onwards) are affected, as are MariaDB installs that use the pool adapter. It is not a regression.

Risks / things to consider for the fix

  • Only cache getters that are really constant for the adapter class and DSN. Not every getSupportFor* is: setSupportForAttributes() exists on the adapter and the pool (SQL.php#L3583, Pool.php#L850), so getSupportForAttributes() can change at runtime. Our test patch cached it anyway; a real fix must exclude it or invalidate on the setter.
  • Values that depend on per-handle state (tenant, shared tables, namespace) must not be cached per pool.
  • getHostname() is constant per DSN. If a pool can reconnect to a different host (failover), cache it per connection instead.

Related: utopia-php/database#980 (other per-request work on the getDocument hot path), #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 / self-hosted
    product / storage
    on Oct 3, 2026
  2. changed the title [-]🐛 Bug Report: Database pool adapter checks out a connection and re-applies the full adapter state for every capability getter – ≈ 10 % of API CPU[/-] [+]🐛 Bug Report: Pool adapter checks out a connection for every capability getter[/+] on Oct 4, 2026
  3. removed
    product / storage
    product / functions
    product / self-hosted
    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