Skip to content

fix(adapter-pg): serialize queries on single-connection clients - #29979

Open
gadcam wants to merge 1 commit into
prisma:v7from
gadcam:fix/adapter-pg-serialize-single-connection-queries
Open

fix(adapter-pg): serialize queries on single-connection clients#29979
gadcam wants to merge 1 commit into
prisma:v7from
gadcam:fix/adapter-pg-serialize-single-connection-queries

Conversation

@gadcam

@gadcam gadcam commented Aug 11, 2026

Copy link
Copy Markdown

Problem

@prisma/adapter-pg emits DeprecationWarning: Calling client.query() when the client is already executing a query whenever a query with 2+ sibling relations runs inside a transaction. The query interpreter's join node loads relations concurrently; a pg.Pool tolerates this, but a transaction's single pg.PoolClient does not — and pg@9 will make it a hard error.

Fixes #29407.

Fix

Per @aqrln's review on #29468, serialization is done in the base PgQueryable.performIO rather than in PgTransaction — the constraint is the single connection, not transactions per se. PrismaPgAdapter opts out via a serializeQueries flag: the pool manages its own concurrency, and serializing it would cap the whole application at one query at a time. A small promise-chain lock replaces #29468's async-mutex dependency.

The interpreter is deliberately untouched: join fan-out stays parallel (a regression test now pins this) and adapters own the serialization.

Tests

  • transaction connection never sees overlapping client.query() calls (fails without the fix)
  • pool queries stay parallel — guards against reintroducing a global bottleneck
  • a failed query releases the lock, so an error can't deadlock the rest of the transaction
  • interpreter: join children still load in parallel

Credit

This supersedes #29468 by @matingathani, who identified the issue and proposed the original mutex approach — picking it up as the review feedback has been open since 2026-07-28. Thanks also to @tensordreams for their contributions to that branch.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved transaction query handling so queries execute sequentially and continue correctly after a query failure.
    • Preserved concurrent query execution for connection pools and join-related child queries.
  • Tests

    • Added coverage for transaction serialization, pool concurrency, failure recovery, and concurrent join queries.

A pg.PoolClient is a single connection and does not support concurrent
queries (deprecated in pg@8, an error in pg@9), but the query interpreter
loads sibling relations concurrently, so any query with 2+ relations
inside a transaction triggered the deprecation warning. Serialize
performIO in PgQueryable for single-connection clients, leaving the
pool path parallel.

Supersedes prisma#29468, fixes prisma#29407.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Camille Barneaud <1693643+gadcam@users.noreply.github.com>
@CLAassistant

CLAassistant commented Aug 11, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: c2167e1c-f252-47ad-80b7-9a6854aa7e3a

📥 Commits

Reviewing files that changed from the base of the PR and between 179ba0c and 7139956.

📒 Files selected for processing (3)
  • packages/adapter-pg/src/__tests__/pg.test.ts
  • packages/adapter-pg/src/pg.ts
  • packages/client-engine-runtime/src/interpreter/query-interpreter.test.ts

📝 Walkthrough

Walkthrough

The PostgreSQL adapter now serializes queries for single-client transactions, preserves serialization after failures, and allows pool adapters to remain concurrent. Tests cover transaction ordering, pool concurrency, failure recovery, and concurrent join child queries.

Changes

PostgreSQL query serialization

Layer / File(s) Summary
Serialization policy and failure-safe execution
packages/adapter-pg/src/pg.ts, packages/adapter-pg/src/__tests__/pg.test.ts
PgQueryable queues single-client queries and continues the queue after failures. PrismaPgAdapter disables serialization for pools. Tests cover serialized transactions and concurrent pool queries.
Join child-query concurrency validation
packages/client-engine-runtime/src/interpreter/query-interpreter.test.ts
A regression test verifies that three join child queries execute concurrently.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant QueryInterpreter
  participant PgQueryable
  participant pgClient
  QueryInterpreter->>PgQueryable: performIO(query)
  PgQueryable->>pgClient: execute one queued query
  pgClient-->>PgQueryable: return result or failure
  PgQueryable-->>QueryInterpreter: return result
Loading

Possibly related PRs

  • prisma/prisma#29839: Both changes address PostgreSQL query serialization, failure-safe locking, and pool concurrency.
  • prisma/prisma#29907: Both changes modify and test PostgreSQL transaction and pool query concurrency.

Suggested labels: lgtm

Suggested reviewers: jacek-prisma

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the adapter-pg fix and its primary behavior: serializing queries on single-connection clients.
Linked Issues check ✅ Passed The implementation addresses [#29407] by serializing single-client queries, preserving pool concurrency, and maintaining join execution after failures.
Out of Scope Changes check ✅ Passed The implementation and regression tests directly support the linked issue and stated objectives; no unrelated changes are evident.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants