Document Service Populate Concurrency#
Every Document Service operation (findMany, findOne, findFirst, etc.) is wrapped in a knex transaction via wrapInTransaction. When a query includes a populate parameter, the query builder's execute() method picks up the ambient transaction from transactionCtx and binds all subsequent queries to the same underlying pg.Client connection.
The populate fan-out ā handled in applyPopulate ā then dispatches one sub-query per relation attribute via Promise.all. With three or more relations, multiple queries are in-flight on that single connection simultaneously, which triggers a node-postgres deprecation warning:
DeprecationWarning: Calling client.query() when the client is already
executing a query is deprecated and will be removed in pg@9.0.
node-postgres has merged the removal of its internal query queue for pg@9, meaning this pattern will throw a hard error (not just warn) once pg@9 is in use.
Root Cause#
The call chain is:
wrapInTransactionopens a knex transaction and stores it inAsyncLocalStorageviatransactionCtx.run().QueryBuilder.execute()reads the ambient transaction withtransactionCtx.get()and calls.transacting(transaction)on every knex query it builds.applyPopulate()then callsawait Promise.all(Object.keys(populate).map(populateAttribute)), which issues all relation sub-queries concurrently on the same connection.
The same concurrent pattern also exists inside the morphToMany branch, which fans out one query per morph type .
Fix: Transaction-aware Populate Execution (PR #27644)#
PR #27644 fixes this by introducing a forEachPopulate helper that detects whether a transaction is active at runtime:
- Inside a transaction (
transactionCtx.get()returns a value): relations are populated sequentially usingfor...of+await. - Outside a transaction: the original parallel
Promise.allfan-out is preserved.
The key change in apply.ts replaces the bottom-of-function Promise.all call and the morphToMany inner Promise.all with forEachPopulate.
Performance impact: none. The pg driver was already serializing concurrent queries on the single transaction connection internally ā the fix just makes the serialization explicit at the application layer, eliminating the deprecated queue. Benchmarks on a production app running 20 transactional findMany calls with 5 relations each showed no throughput difference (262ā331 ms in both cases).
Key Files#
| File | Role |
|---|---|
packages/core/database/src/query/helpers/populate/apply.ts | Fan-out populate logic; site of both the bug (Promise.all) and the fix (forEachPopulate) |
packages/core/database/src/query/query-builder.ts | execute() picks up the ambient transaction and applies it to every knex query |
packages/core/database/src/transaction-context.ts | AsyncLocalStorage-backed store that propagates the active transaction through async call chains |
packages/core/core/src/services/document-service/repository.ts | Wraps all Document Service reads/writes in wrapInTransaction |
Detecting the Problem#
The deprecation warning surfaces in Node.js process output (not in Strapi logs) whenever a Document Service read with 3+ populated relations is executed. Grep for:
DeprecationWarning: Calling client.query() when the client is already executing a query
A minimal reproduction is available that prints a counter of warnings emitted.
Related References#
- PR #27644 ā fix and test coverage (validates max concurrency = 1 inside a transaction vs. 3 outside)
- Issue #27645 ā full stack trace and reproduction steps
- node-postgres #3603 ā upstream decision to make the concurrent-query pattern a hard error in pg@9