Skip to content

@payloadcms/db-postgres — concurrent client.query() during batch update #16333

Open

Description

@birdzai

Describe the Bug

When batch-editing records in the Payload admin, @payloadcms/db-postgres calls client.query() on a pg client that is already executing a query. This triggers the following warning:

DeprecationWarning: Calling client.query() when the client is already executing a query
is deprecated and will be removed in [email protected]. Use async/await or an external async flow
control mechanism instead.

This warning is currently masked in Payload's own project templates via NODE_OPTIONS=--no-deprecation in the default dev and start scripts. When that flag is removed (as in any real production deployment that doesn't carry it forward), the warning surfaces.

In [email protected] this will stop warning and start throwing, breaking batch updates entirely.

Link to the code that reproduces this issue

https://github.com/birdzai/payload-postgres-batch-update-repro

Reproduction Steps

As documented in README.md in linked repo:

1. Start the database

docker compose up -d

2. Install dependencies and start the dev server

pnpm install && pnpm dev

On first run, onInit seeds the database with:

3. Build for production

pnpm build

4. Start without deprecation suppression

node --trace-deprecation node_modules/next/dist/bin/next start

Note: the default pnpm start script passes NODE_OPTIONS=--no-deprecation, which masks the warning. The --trace-deprecation flag surfaces it with a full stack trace.

5. Trigger the bug

  1. Open http://localhost:3000/admin and log in as [email protected] / password
  2. Navigate to the Dummy collection
  3. Select all 3 records using the checkbox in the list header
  4. Click Edit in the bulk action toolbar
  5. Set checked to true and click Save

6. Observe the warning in the terminal

DeprecationWarning: Calling client.query() when the client is already executing a query
is deprecated and will be removed in [email protected]. Use async/await or an external async flow
control mechanism instead.
    at Client.query (.../node_modules/pg/lib/client.js:691:7)
    at .next/server/chunks/ssr/....js (o.queryWithCache)
    at .next/server/chunks/ssr/....js (Object.startActiveSpan)
    at .next/server/chunks/ssr/....js (o.execute)
    at Object.startActiveSpan

The trace originates in Drizzle's queryWithCache → execute, called via OpenTelemetry's startActiveSpan, ultimately issuing a second pg Client.query() call while the client is already mid-query.

Which area(s) are affected?

db: postgres

Environment Info

Binaries:
  Node: 25.1.0
  npm: 11.6.2
  Yarn: N/A
  pnpm: 10.33.0
Relevant Packages:
  payload: 3.83.0
  next: 16.2.3
  @payloadcms/db-postgres: 3.83.0
  @payloadcms/drizzle: 3.83.0
  @payloadcms/graphql: 3.83.0
  @payloadcms/next/utilities: 3.83.0
  @payloadcms/richtext-lexical: 3.83.0
  @payloadcms/translations: 3.83.0
  @payloadcms/ui/shared: 3.83.0
  react: 19.2.4
  react-dom: 19.2.4
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 25.4.0: Thu Mar 19 19:30:44 PDT 2026; root:xnu-12377.101.15~1/RELEASE_ARM64_T6000
  Available memory (MB): 32768
  Available CPU cores: 10

Activity

  1. added
    status: needs-triage
    validate-reproduction
    on Apr 21, 2026
  2. added
    db: postgres
    and removed
    validate-reproduction
    on Apr 21, 2026
  3. palber commented on Jun 7, 2026

    @palber

    Hit the same bug, but with localized fields the consequences are much worse — data is permanently deleted.

    When running "Edit many" on a collection with localized fields, *_locales rows of random documents get wiped on every bulk edit. In our case ~44 of 100 product records lost their localized titles per run. On the next edit those required localized fields fail validation — which is when the data loss becomes visible as "X of 100 failed".

    The reason it's destructive here: upsertRow takes a DELETE + reinsert path for localized fields. Under the concurrent-query race on the shared transaction client, the reinserts get lost silently.

    I suspect the root cause is that update.js opens one shared transaction (= one pg client) for the entire bulk op at the top, then fires all documents concurrently via docs.map(async …). The existing bulkOperationsSingleTransaction flag looks like it was meant to fix this, but it has no effect because the request-level transaction is already open before that flag is consulted.

    Workaround, with some drawbacks, that fully resolves it for us:

    db: postgresAdapter({
      pool: { connectionString: process.env.DATABASE_URI },
      transactionOptions: false,
    })
  4. emmpauldev commented on Oct 28, 2026

    @emmpauldev

    We hit this on a production project (Payload 3.90.2, @payloadcms/db-postgres 3.90.2, pg 8.20.0, drizzle-orm 0.45.2) and measured how widespread it is. The batch update in the original report is only one of several paths. Here are our measurements in case they help scope a fix.

    How we counted. pg prints the warning once per process, so it hides the volume. We preloaded a small script (node --require) that wraps pg's Client.prototype.query. It records the stack each time a query starts while that client already has one active or queued (this._queryQueue.length > 0 or an active query). It also keeps the stacks taken at the entry of each Local API call, because drizzle loses the caller's async frames. At exit, it groups the calls by the innermost application frame and by the Payload frame that launched the parallel branch.

    Payload paths that fan out on the shared transaction client (line numbers in Payload 3.90.2 sources):

    1. Relationship validation during a write. fields/hooks/beforeChange/traverseFields.ts:129 validates a document's fields in parallel. Each relationship field's validation runs a find (collections/operations/find.ts:213), all on the same transaction client. This fires on any single-document create/update with several relationship fields, not just bulk operations. On our demo seed, it accounts for 90 of the remaining concurrent queries.
    2. Bulk delete({ where }). collections/operations/delete.ts deletes each matching document in parallel. Each document's beforeDelete/afterDelete hooks, plus the per-row deleteOne, run concurrently on one client. Any hook that writes (an audit row, for example) adds to the race.
    3. Bulk update({ where }). collections/operations/update.ts:462 is the path in the original report. With user hooks in place, each hook's reads and writes join the race. A single Local API update({ where }) over three documents with beforeValidate/afterChange hooks, the same path as the admin's bulk edit, produced 60 concurrent queries.

    Counts. These are concurrent queries per run, before and after we removed the sources in our own code. We did three things: (a) one read per save instead of one read per link in a custom validate, (b) a Promise.all turned into sequential awaits, and (c) cascades that delete their children one by one instead of with one bulk delete({ where }).

    Run Before After What remains
    Demo seed (blank DB) 420 90 all path 1
    Profiles suite 474 5 path 1
    Orders suite 4,019 3,045 path 2 from bulk deletes in test teardown (2,977), path 3 (60), path 1 (8)
    Members suite 222 98 path 2, test teardown
    Access-control suite 69 18 path 1

    After our changes, the application code we control triggers zero concurrent queries. The rest goes through Payload's own fan-out. We can't sequence it from userland short of avoiding bulk operations entirely, and path 1 can't be avoided at all.

    A note for anyone relying on bulk delete in hooks. Replacing delete({ where }) with delete({ id }) per child doesn't work as a drop-in inside a parent's transaction. A refused child (an APIError thrown by its hook) rolls back the whole shared transaction and clears req.transactionID before the caller can catch it. We kept a bulk call but scoped it to one row (where: { id: { equals } }), which returns errors instead of throwing, and we stop at the first error.

    Happy to share the preload script if it's useful for reproducing.

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

    Bugdb: postgresengagement: mediumstatus: needs-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions