Connection pooling for serverless Postgres: a field note

Serverless functions open far more Postgres connections than a long-running server, so a pooler in transaction mode usually decides whether the database stays up under load.

A traditional web server keeps a small, fixed pool of database connections and reuses them for every request. A serverless platform does the opposite: each cold instance starts with no connections, opens its own, and may hold it until the instance is frozen. Under a burst of traffic, a hundred instances can open a hundred connections in a few seconds, and Postgres, which forks a process per connection, starts refusing new ones once it reaches max_connections.

The usual fix is a pooler such as PgBouncer between the functions and the database. In transaction mode the pooler lends a server connection to a client only for the length of one transaction, so many short-lived function instances share a few real connections. The trade-off is that session state does not survive between transactions: prepared statements, advisory locks held across statements, SET commands and temporary tables can all behave unexpectedly, and some drivers need prepared statements turned off.

Three checks help before launch. First, set the pool size from the database's real limit, leaving headroom for migrations and admin sessions. Second, make every function open its connection lazily and close it, or return it to the pool, before the response is sent. Third, load-test with the same concurrency the platform allows, because the failure only appears when many instances start at once.

Written 2026-11-26.