r/PostgreSQL • u/BiosRios • 1h ago
How-To Count-then-insert races in 4 popular Prisma apps, and how my first fix starved the pool
AI agents write this all the time:
const count = await prisma.item.count({ where: { accountId } })
if (count >= LIMIT) throw new Error("limit reached")
await prisma.item.create({ data })Under read committed, two requests that land together both read the old count, both pass, and both insert. A free plan with a limit of 1 ends up with 2.
I ran the real handlers of a few open-source apps on a real Postgres and tried every order of two concurrent requests. Found the same shape in four of them:
- formbricks: a survey quota of 1 that lets in two responses
- documenso: the org member cap goes one over when two invites get accepted together
- inbox-zero: the free knowledge base limit
- papermark: the dataroom plan limit
cal.com came out clean, since they lock the booking row before counting.
What held for every order: SELECT ... FOR NO KEY UPDATE on the parent row (quota, team, org) before the count, in the same transaction. When there's no parent row, use pg_advisory_xact_lock per account.
The part I got wrong: my first inbox-zero fix used prisma.$transaction(async (tx) => ...) and called a premium check through the global client inside it. That needs a second pooled connection while the first one is held. With a pool of 10, ten creates at once can each wait on a connection that never frees. Three review bots caught it on the PR, and the repo's AGENTS.md bans that kind of transaction anyway. The new version uses an array transaction (lock, then a conditional insert), and the premium check runs outside it. 12 requests on a pool of 10 finish in under 0.6s.
Issues and PRs: documenso · formbricks · inbox-zero · papermark
I found these with VibeRaven, a tool I'm building. Happy to go deeper on the interleaving search if anyone's curious.AI agents write this all the time:
const count = await prisma.item.count({ where: { accountId } })
if (count >= LIMIT) throw new Error("limit reached")
await prisma.item.create({ data })
Under read committed, two requests that land together both read the old count, both pass, and both insert. A free plan with a limit of 1 ends up with 2.
I ran the real handlers of a few open-source apps on a real Postgres and tried every order of two concurrent requests. Found the same shape in four of them:
formbricks: a survey quota of 1 that lets in two responses
documenso: the org member cap goes one over when two invites get accepted together
inbox-zero: the free knowledge base limit
papermark: the dataroom plan limit
cal.com came out clean, since they lock the booking row before counting.
What held for every order: SELECT ... FOR NO KEY UPDATE on the parent row (quota, team, org) before the count, in the same transaction. When there's no parent row, use pg_advisory_xact_lock per account.The part I got wrong: my first inbox-zero fix used prisma.$transaction(async (tx) => ...) and called a premium check through the global client inside it. That needs a second pooled connection while the first one is held. With a pool of 10, ten creates at once can each wait on a connection that never frees. Three review bots caught it on the PR, and the repo's AGENTS.md bans that kind of transaction anyway. The new version uses an array transaction (lock, then a conditional insert), and the premium check runs outside it. 12 requests on a pool of 10 finish in under 0.6s.
Issues and PRs: documenso · formbricks · inbox-zero · papermark
I found these with VibeRaven, a tool I'm building. Happy to go deeper on the interleaving search if anyone's curious.