Under load, the app starts throwing connection errors because PostgreSQL's max_connections limit has been reached, usually from unclosed connections or missing pooling in a serverless environment.

Add a connection pooler like PgBouncer in front of Postgres, and make sure the app closes or properly reuses connections instead of opening a new one per request.

Step-by-Step Guide

1

Check current connection count: SELECT count(*) FROM pg_stat_activity;

2

Review max_connections in postgresql.conf before raising it blindly

3

Use a pooling library appropriate to your stack (pg-pool, Prisma's connection_limit)

4

Set an idle_timeout so unused connections close automatically

5

Add PgBouncer for transaction-level pooling in front of Postgres

6

In serverless functions, ensure the ORM client closes connections after each invocation

7

Monitor pg_stat_activity for idle-in-transaction sessions that never commit

8

Restart app instances to clear any already-leaked connections

Found an issue with this solution?

Related Topics

pgbouncer setup guideprisma connection limit serverlesspostgres idle in transaction