Document Service Relation Queries#
Relation queries in the Document Service occur on two paths ā reads (populate fan-out) and writes (ID mapping + entity validation). Both paths share a single underlying PostgreSQL connection when a transaction is active, which creates a concurrency hazard that triggers a node-postgres deprecation warning and will become a hard error in pg@....
Transaction Context#
Every Document Service operation (findMany, findOne, create, update, etc.) is wrapped in a knex transaction via wrapInTransaction. The active transaction is stored in AsyncLocalStorage via transactionCtx.run() and then picked up by QueryBuilder.execute(), which binds all subsequent queries to the same pg.Client connection. Any concurrent queries on that connection trigger the warning:
DeprecationWarning: Calling client.query() when the client is already executing a query
Read Path: Populate Fan-out#
When a query includes populate, applyPopulate() dispatches one sub-query per relation attribute. The original implementation used Promise.all, so three or more populated relations issued concurrent queries on the shared connection.
PR #27644 (fix(database): populate relations sequentially inside a transaction) addresses this by introducing a forEachPopulate helper in apply.ts:
- Inside a transaction ā relations are populated sequentially (
for...of+await). - Outside a transaction ā the original parallel
Promise.allfan-out is preserved.
The same fix applies to the morphToMany branch, which previously fanned out one query per morph type concurrently. There is no measured throughput impact because the pg driver was already serializing concurrent queries internally on the shared connection.
Write Path: ID Map Loading and Relation Validation#
On saves, two additional query fan-outs occur inside the same transaction:
1. ID map loading ā id-map.ts translates documentId values to numeric entity IDs before the write. It batches lookups by uid + locale, then calls async.map to run findMany queries per group. Inside a transaction these grouped queries run concurrently on the shared connection.
2. Entity validation ā entity-validator/index.ts runs checkRelationsExist on every save to confirm that referenced relation targets actually exist. It iterates relationsStore ā one entry per target content type ā and fires all existence count queries via Promise.all. This means every distinct relation target (e.g., api::article.article, plugin::upload.file) gets its own concurrent count query on the shared connection.
The relationsStore is built by buildRelationsStore, which traverses the input payload (including nested components and dynamic zones) and groups IDs by target UID.
PR #27945 proposes the same transaction-aware fix for both write-path fan-outs:
checkRelationsExistswitches to sequentialawaitinside a transaction (with early-exit on the first missing relation), retainingPromise.alloutside.id-map.tslikewise serializes its groupedfindManycalls when a transaction is active.
Key Files#
| File | Role |
|---|---|
packages/core/database/src/query/helpers/populate/apply.ts | Read-path relation fan-out; fixed by PR #27644 |
packages/core/database/src/transaction-context.ts | AsyncLocalStorage-backed transaction propagation |
packages/core/database/src/query/query-builder.ts | Binds ambient transaction to every knex query |
packages/core/core/src/services/document-service/repository.ts | Wraps all Document Service operations in a transaction |
packages/core/core/src/services/document-service/transform/id-map.ts | Batches documentId ā entity ID lookups; write-path fan-out (PR #27945) |
packages/core/core/src/services/entity-validator/index.ts | Per-target-type count validation on save; write-path fan-out (PR #27945) |
Related References#
- PR #27644 ā fix(database): populate relations sequentially inside a transaction
- PR #27945 ā proposed fix for write-path (
id-map+checkRelationsExist) fan-outs - Background doc ā Document Service Populate Concurrency (internal)
- node-postgres #3603 ā upstream decision making concurrent queries on a single connection a hard error in pg@...