The smallest managed Postgres plan on DigitalOcean, 1 vCPU and 1 GB, has max_connections set to 25. Three of those are reserved for superusers, which leaves 22 for you. That's plenty for one app server with a sensible pool. It's not plenty for a fleet of workers, a serverless function that scales out, or a Node app where every process opens its own ten connections.
DigitalOcean's answer is connection pools, which are PgBouncer running in front of the cluster, created with one command. I wanted to see what that actually changes when far more clients show up than the database can take, and what breaks in the pool's transaction mode, which is the one everybody tells you to use.
The setup
One Postgres 18 cluster on the smallest plan in Frankfurt, and a $6 Droplet in the same region as the client, talking to the cluster over the private network. The client was a Python script with psycopg 3 that opened N connections at once and had each one run select pg_sleep(0.02) in a loop for 20 seconds. The query is a 20 ms wait, so the database has almost nothing to do and the only thing being tested is connections.
I made two pools:
doctl databases pool create <cluster-id> tx --db defaultdb --mode transaction --size 10 --user doadmin doctl databases pool create <cluster-id> sess --db defaultdb --mode session --size 10 --user doadmin My first try was size 20 for each, and the second one came back with 412 pool size exceeds available server connections. The pool sizes come out of the same 22 connections. After 10 and 10, a third pool of 2 was accepted and one of 3 wasn't. So a pool doesn't give you more database connections. It decides who gets the ones you have.
200 clients, straight at the database
With 20 clients connecting directly, everything was fine: about 790 queries a second, p50 22.7 ms, which is the 20 ms sleep plus a bit.
With 200, twenty clients got in and the other 180 were turned away at connect time with one of two errors:
FATAL: remaining connection slots are reserved for roles with the SUPERUSER attribute FATAL: sorry, too many clients already The twenty that got in ran at the same speed as before. Postgres doesn't slow down here, it just refuses. For an app that means 90% of workers crash on startup, or keep retrying.
The same 200 through the transaction pool
Same script, port 25061 and database name tx instead of the cluster's own. All 200 connected, all 200 ran queries, and there were zero errors in 20 seconds.
It's not free. Ten server connections were now shared by 200 clients, so throughput dropped to 414 queries a second, which is close to the limit of 10 connections running a 20 ms query back to back. Each client waited its turn: p50 went from 22 ms to 421 ms. That's the real trade. Instead of 180 failures you get 200 slower clients, and if your queries are short, the waiting is short too.
Connecting got slower as well, with a p50 of 2.7 seconds to open a connection. Most of that was 200 TLS handshakes at once on a 1 vCPU client, which is my side of the wire, not the pool.
The same 200 through the session pool
Session mode gives each client a server connection for as long as the client stays connected. With ten server connections, ten clients got in and ran normally at 22 ms. The other 190 connected fine, PgBouncer accepted them in 30 ms, but their first query just sat there.
To see how long, I held ten connections open and sent an eleventh. Its query failed after exactly 120.2 seconds with query_wait_timeout. No error at connect time, just two minutes of nothing. If your app uses long-lived connections, session mode at this size behaves almost like a direct connection, just with a slower, quieter failure.
It's useful for a different reason, which comes up below.
Clients that connect for every request
Plenty of code opens a connection, runs one query and closes it again, like PHP, functions, or scripts. I ran 15 clients doing that in a loop.
Through the transaction pool they managed 54 requests a second with a p50 of 273 ms. Direct, 33 a second with a p50 of 415 ms. The full cycle was dominated by the TLS handshake on my small client, so I wouldn't read much into the exact numbers, but the pool came out ahead every time, because the server side of the connection already exists and doesn't need a new Postgres process.
Direct also had errors in that run, even with only 15 clients. The reason turned out to be the most useful thing I learned.
The pool keeps its connections
PgBouncer keeps server connections open after it's done with them, so the next client doesn't have to wait for a new one. After my tests, pg_stat_activity showed ten idle connections from the pool that stayed open for several minutes after the last client left.
Those connections count against the same 22. With the transaction pool warm, only twelve slots were left for anything connecting directly, which is why my direct test with 15 clients failed. Running a migration tool or psql directly at the same time can hit the same wall.
The fix is to pick one road. If you create pools, point everything at a pool, and keep your pool sizes a few below the limit so there is room for an admin session.
What transaction mode breaks
In transaction mode a client only holds a server connection for one transaction. With autocommit on, that's one statement. The next statement can run on a different server connection, and anything that lives on the connection rather than in the transaction stops working. I checked each of these with 12 other clients keeping the pool busy, and ran the same checks directly as a control. Direct passed all of them.
SET. One client ran SET statement_timeout = '7s', then read it back 50 times. It saw its own setting 5 times out of 50. A second client that had never run SET saw 7s 4 times out of 50. The setting stayed on whichever server connection ran the SET, and that connection then served other clients.
I found this by accident. An earlier version of the test set a 7 second timeout and moved on, and a later, unrelated query in another client was cancelled with canceling statement due to statement timeout. Brand new psql sessions through the pool were still showing 7s afterwards. Inside an explicit transaction, SET LOCAL worked 50 times out of 50, because the transaction keeps the same server connection until it ends.
Temporary tables. CREATE TEMP TABLE followed by an insert into it in the next statement worked 0 times out of 20. The table was created, on some other connection.
Advisory locks. pg_try_advisory_lock(k) then pg_advisory_unlock(k) as two statements: the unlock found its lock once out of 20. The other 19 locks stayed held on connections my client no longer had anything to do with. Anyone else waiting on those keys would wait until the server connection closes. Session-level advisory locks, the kind many migration tools use, need session mode or a direct connection.
LISTEN. Five notifications sent, none received. The LISTEN was registered on a server connection the listening client didn't keep.
Prepared statements. I expected these to break too, because for years they did with PgBouncer in transaction mode. They didn't. psycopg with prepare_threshold=0, which prepares every query on the server, ran 50 out of 50. asyncpg with its default statement cache, which tends to cause the most trouble, ran 50 out of 50, also with seven different statements mixed together. Recent PgBouncer versions track protocol-level prepared statements across server connections, and DigitalOcean's pools appear to have that turned on. That removes the most common reason people give up on transaction mode.
Session mode cleans up after itself
I ran the same checks through the session pool. SET, temp tables, advisory locks and LISTEN all behaved like a direct connection. A client in session mode that set statement_timeout and disconnected left nothing behind: three new clients got the same server process back and all of them saw the default. So session mode isn't useless at all. It's what you want for a migration tool, a worker that relies on advisory locks, or a LISTEN connection, while the rest of the app goes through transaction mode.
What I'd take from it
The pool did what it's for. 200 clients on a database that allows 22, zero errors, and nothing to install or run myself. It took two doctl commands and a port change.
If you set it up:
- Use transaction mode for app traffic. Prepared statements work, so most ORMs and drivers are fine as they are.
- Don't use plain
SET. UseSET LOCALinside a transaction, or put settings on the role withALTER ROLE ... SET. - Give migrations, advisory locks and LISTEN their own small session pool or a direct connection.
- Remember the pool sizes and direct connections share one budget, and the pool keeps its connections after the clients go.
- Watch for
query_wait_timeout. It shows up two minutes after the problem started, not when it starts.
Caveats
One cluster, the smallest size, one region, and a test query that does nothing but wait. With real queries the waiting in transaction mode depends on how long your transactions are. Most of the connect-time numbers are limited by a 1 vCPU client doing TLS handshakes. The 120 second wait is what I measured, not a documented setting, and the cleanup in session mode I only checked for SET.
