Skip to content

@prisma/adapter-pg does not map PostgreSQL deadlock SQLSTATE 40P01 to P2034 #29716

Closed

Description

@neliro

Bug description

PostgreSQL uses SQLSTATE 40P01 for deadlock_detected. @prisma/adapter-pg
maps 40001 to TransactionWriteConflict, but does not map 40P01.

In an isolated PostgreSQL 16 reproduction using @prisma/adapter-pg,
@prisma/client, and Prisma CLI 6.19.2, an ordinary model mutation whose
database trigger deterministically raises 40P01 does not surface
PrismaClientKnownRequestError with code P2034. The same behavior occurs
inside an interactive transaction.

Without P2034, callers cannot safely apply application retry logic keyed to
Prisma's structured deadlock code without parsing provider text or retrying
unrelated failures.

Severity: Major. This breaks the structured retry contract for PostgreSQL
deadlocks.

Reproduction

Self-contained reproduction:
https://gist.github.com/neliro/b659bb6d1bf388422129e36a9f54b213

The reproduction uses a disposable loopback-only PostgreSQL 16 database and a
trigger to raise the exact SQLSTATE deterministically. On the exercised path,
output is limited to the error classification, attempt count, outcome, and row
count; all created database objects are removed.

Expected vs. actual behavior

Expected:

{
  "clientVersion": "6.19.2",
  "directClassification": "known:P2034",
  "transactionClassification": "known:P2034",
  "attempts": 2,
  "outcome": "inserted",
  "rowCount": 1
}

Actual with the unmodified 6.19.2 adapter:

{
  "clientVersion": "6.19.2",
  "directClassification": "unknown_request",
  "transactionClassification": "unknown_request",
  "attempts": 1,
  "outcome": "not_retryable",
  "rowCount": 0
}

At Prisma main commit
cda80a4488b7b551c36bf09ca2e8303ef9509da4,
packages/adapter-pg/src/errors.ts
maps 40001 to TransactionWriteConflict, while 40P01 falls through to the
generic PostgreSQL error path. Prisma translates TransactionWriteConflict to
P2034, whose message covers write conflicts and deadlocks.

The minimal source change is to give 40P01 the same mapping as 40001 and
extend the existing adapter unit test to cover both codes.

Frequency

Consistently reproducible for PostgreSQL 40P01 through @prisma/adapter-pg.

Development or production

Both. This is a Prisma Client runtime error-classification path.

Regression

Unknown. I have not confirmed a previously working adapter version.

Workaround

There is no safe application-level workaround. Parsing provider messages or
retrying every generic failure would broaden retries beyond deadlocks. A local
package patch that maps 40P01 to TransactionWriteConflict restores P2034.

Prisma schema and query

The complete schema and executable query are in the reproduction gist. The
relevant operation is an ordinary model mutation inside an interactive
transaction:

await prisma.$transaction((tx) =>
  tx.probe.create({ data: { value: 'transaction' } }),
)

The trigger raises 40P01 only on the first attempt so a correctly classified
retry commits exactly one row.

Prisma config

Not used. The Prisma 6 reproduction uses the datasource in schema.prisma and
passes the connection string to PrismaPg.

Environment and setup

  • OS: macOS
  • Database: PostgreSQL 16
  • Node.js: 25.7.0
  • Prisma CLI: 6.19.2
  • Prisma Client: 6.19.2
  • @prisma/adapter-pg: 6.19.2

No credentials, provider logs, or raw provider messages are included.

Activity

  1. Huaian666 commented on Jul 13, 2026

    @Huaian666

    ngl this is a pretty significant gap — 40P01 is the standard PostgreSQL deadlock SQLSTATE, and without it mapping to P2034 there is no clean way to retry on deadlocks without parsing raw error strings. the fix itself looks trivial (just give 40P01 the same mapping as 40001 in errors.ts), hopefully gets prioritized since structured retry is kind of the whole point of having these error codes

  2. added a commit that references this issue on Aug 9, 2026
    32f3d17
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions