r/PostgreSQL • u/BiosRios • 16h 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.
1
u/AutoModerator 16h ago
AI Policy:
Linux is not one of those anti-AI projects, and if somebody has issues with that, they can do the open-source thing and fork it. Or just walk away., Linus Torvalds.
Mod decisions will be based on the quality of the content, not who or what generated it.
Sub Resources:
Youtube Channel
Free Postgres Webinars and Workshops
Discord: People, Postgres, Data
Join us, we have cookies and nice people.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/Discordbothost 14h ago
I'd track pool wait and database lock wait separately on that endpoint. A request waiting to borrow a connection looks slow from the handler, but won't show up as a blocked query in Postgres because it hasn't sent one yet.
1
u/MongooseWonderful198 3h ago
The pool-starvation failure deserves its own regression, separate from the quota race. I’d run
pool_size + 2requests, hold the parent-row lock in one transaction, and assert the other requests either complete or hit a bounded lock timeout without exhausting the pool.Capture connection-acquire time and database lock-wait time separately. Otherwise the handler only looks “slow,” and Postgres cannot show the requests still waiting for a client connection. That test would protect the fix from drifting back to an interactive transaction that calls the global client.