This repository contains a minimal, reproducible example demonstrating an issue with high RSS memory usage during queries involving PostgreSQL JSON columns when using Prisma ORM. The seeded data provided mimics real business data where the issue was encountered. In the application, records with large JSON columns are fetched, manipulated, and updated, causing significant RAM consumption under certain circumstances.
When executing specific queries, the application's RSS memory usage spikes, sometimes reaching up to 1GB. This behavior has been observed consistently and can be reproduced using the code provided in this repository.
To demonstrate that the issue is related to Prisma, we have benchmarked the same operations using the Kysely ORM framework. Additionally, a workaround was implemented where a text field was used instead of a JSON column. While this reduces the problem, it is not a perfect solution.
To reproduce the issue locally, follow these steps:
- Set up the environment using Docker Compose:
docker-compose up -d
- Install dependencies:
pnpm install
- Run Prisma migrations:
pnpm run migrate
- Seed the database:
pnpm run seed
- Build the project:
pnpm run build
- Start the server with prisma:
pnpm run run prisma
or with kysely
pnpm run run kysely
Now, you can access the application endpoints:
-
Fetching and updating data (JSON column): curl http://localhost:3001/
-
Fetching and updating data (Text column replacing JSON): curl http://localhost:3001/without-json
Performance analysis can be conducted using Clinic.js, a performance profiling tool. Use the following npm scripts along with Clinic.js:
-
Prisma with JSON column: pnpm run analysis:prisma:json
-
Prisma without JSON column: pnpm run analysis:prisma:no-json
-
Kysely with JSON column: pnpm run analysis:kysely:json
-
Kysely without JSON column: npm run analysis:kysely:no-json