Repository navigation
Description
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
- Run Appwrite 2.3.0 (any SQL adapter; measured with Postgres).
- Put load on a cheap endpoint, e.g. cached
GET /v1/tablesdb/{db}/tables/{table}/rows/{rowId}. - Profile the API workers (we used the
excimerextension, CPU sampling per Swoole worker). Utopia\Database\Adapter\Pool::delegateaccounts for 21–31 % of API CPU. About 9–12 % of total API CPU is spent indelegatecalls for getters such asgetSupportForAttributes,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 othergetSupportFor*methodsgetHostname()(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
Poolobject - or make them static on the adapter class where they really are constants
- 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
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), sogetSupportForAttributes()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):
- 🐛 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: Postgres: fulltext search never uses an index #14081 – fulltext indexes on Postgres are plain B-trees,
searchcannot use them - 🐛 Bug Report:
total=falseon list endpoints is ignored #14082 –total=falseon list endpoints is ignored - 🐛 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
[-]🐛 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[/+]