The prisma dev local Postgres shares one session across connections, so Bun prepared statements collide after a restart (42P05)

Open
#471 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
postgresql, typescript
Domain
databases

Research direction

Start by reading the reproduction in the issue and tracing startPrismaDevServer and the socket connection handling in @prisma/dev 0.25.2. Reproduce the two-client prepared-statement collision, then investigate how the PGlite session is shared or reset when a connection closes. Done means separate or reset session state prevents 42P05 after a client restart, without requiring prepare: false.

Written by the indexing model from the issue text.

Description

Composer 0.40.0, @prisma/dev 0.25.2.

From Composer's own record; not reproduced here. Composer gotchas.md at v0.40.0, lines 616-646, "prisma dev shares one Postgres session across all connections — a restarted Bun.SQL client crash-loops on 42P05":

  • Line 618: "Filed upstream: not yet". Line 619: product prisma dev / @prisma/dev 0.25.2 (PGlite).
  • Lines 623-632, symptom: the second prisma dev run, or any restart of a service against the same database, crash-loops with PostgresError: prepared statement "P… $0" already exists, errno: "42P05", routine: "StorePreparedStatement", until the emulator holds the service after 5 fast exits.
  • Line 636, cause: @prisma/dev runs PGlite, a single-session Postgres, behind a socket, so every client connection shares one backend session, including pg_prepared_statements, and nothing is reset when a connection closes. Bun names prepared statements P<query text>$<per-connection counter>, so a restarted process's first statement collides with the dead process's. A real Prisma Postgres gives each connection its own session.
  • Line 638, workaround: new SQL({ url, max: 1, idleTimeout: 10, prepare: false }), which Composer's own email, auth and storage stores use.
  • Lines 640-644, reproduction: startPrismaDevServer({ name, persistenceMode: 'stateless' }); open new SQL({ url, max: 1 }), run create table if not exists t (id int), close it; open a second client and run the same statement: 42P05. With prepare: false both succeed.

The prisma.io docs currently carry a workaround for this, to be removed once it is fixed: apps/docs/content/docs/composer/databases.mdx, the paragraph after the rawPostgres() server snippet that starts "Those pool settings matter" and explains prepare: false (on main since October, not changed on this branch). It tells readers to change their client settings because of this prisma dev defect. Remove the prepare: false advice from that paragraph once @prisma/dev gives each connection its own session (or resets session state when a connection closes). Expected: each client connection to the local Postgres gets its own session, as on Prisma Postgres.

Found while writing the Composer docs on prisma.io (prisma/web#8965).

Dominant language
TypeScript
Stars
6
Forks
2
Avg merge
1d 2h
Merged PRs (30d)
33

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from prisma/composer

All issues in prisma/composer

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.