Prisma 7: every PrismaClient decodes its own copy of the parameterization schema (~31 MB heap per client)

Open
#30,760 3 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
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
node.js, postgresql, typescript

Research direction

Start in the generated client runtime at runtime/client.js and runtime/client.mjs, then trace the decoder called by the client engine constructor. Check whether the decoded parameterization schema is read-only and whether caching it per input object preserves behavior. Done means clients from the same generated client share the decoded graph and the reproduction shows near-zero extra heap per client.

Written by the indexing model from the issue text.

Description

Bug description

With the query compiler (Prisma 7, driver adapters), each new PrismaClient(...) decodes the generated client's parameterizationSchema into a fresh object graph. For our schema that is about 31 MB of JS heap per client. An app that keeps one client per tenant (a multi-tenant AWS Lambda holding a small LRU of tenant clients) pays that for every client, even though every graph is identical.

On Prisma 6.9 with the same schema, each extra client cost about 7 MB of RSS. The upgrade to 7.10 is what pushed our 512 MB Lambda over its memory limit.

Where it happens

In @prisma/client 7.10.0 runtime/client.js (minified names), the client engine's constructor does:

this.#s = mr.deserialize(t.parameterizationSchema, enumLookup)

mr.deserialize calls a decoder, function Ba(e){return new Li(e).deserialize()}, which turns the base64 graph into {strings, inputNodes, outputNodes, roots}. t.parameterizationSchema is the same object for every client of one generated client. As far as we can tell, the decoded graph is read-only after construction: nothing writes to its arrays or objects.

Reproduction
const { PrismaClient } = require("./generated/client");
const { PrismaPg } = require("@prisma/adapter-pg");
const heap = () => { gc(); gc(); return process.memoryUsage().heapUsed; };
const make = () => new PrismaClient({ adapter: new PrismaPg({ connectionString: "postgresql://u:[email protected]:1/x" }) });
const clients = [make()];
const before = heap();
for (let i = 0; i < 4; i++) clients.push(make());
console.log(((heap() - before) / 4 / 1048576).toFixed(1), "MB per extra client");

Run with node --expose-gc. No database connection is needed: constructing the client is enough. We see 31 MB per extra client.

A possible fix

We tested memoising the decoder per input object (WeakMap), in both runtime/client.js and runtime/client.mjs:

var cache = new WeakMap;
function Ba(e){ let t = cache.get(e); return t || (t = new Li(e).deserialize(), cache.set(e, t)), t }

Measured with that change:

  • Heap per extra client: 31 MB before, 0.15 MB after.
  • Our real Lambda handler, 13 tenants, 5 cached clients: peak RSS 605 MB before, 291 MB after.
  • Our full api, lambda and core test suites pass with it.

We are not patching our installed Prisma. For now we give the Lambda more memory and cache fewer clients, so a fix in Prisma itself would let us undo both.

Expected behavior

Clients created from the same generated client share one decoded parameterization schema, or decode it lazily, so the cost per extra client is close to zero.

Environment
  • prisma and @prisma/client 7.10.0, prisma-client-js generator, @prisma/adapter-pg
  • PostgreSQL
  • Node.js 24 (AWS Lambda nodejs runtime)
Dominant language
TypeScript
Stars
47.8k
Forks
2.6k
Avg merge
1d 21h
Merged PRs (30d)
227

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/orm

All issues in prisma/orm

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.