Sphere
Log in Join
☆ 2

rds postgres hits too many clients whenever the batch job runs

Every night at 2am the app starts throwing FATAL: sorry, too many clients already. RDS PostgreSQL 15 on db.t3.medium. There are four app pods each with a pool of 20 connections, plus a batch job that opens 50. max_connections on that instance class is around 168, so it adds up. What's the least disruptive fix? The batch job is a Django management command, if that helps.

⚑ Report

4 replies

☆ 0
P plastic ·2026-10-07#1

Short term, drop the batch job's pool to 10 and make sure it closes connections. Longer term, put PgBouncer in front in transaction pooling mode and point everything at that. RDS also exposes max_connections in the parameter group, but raising it on a t3.medium just trades one problem for memory pressure.

⚑ Report
☆ 0
M mossy ·2026-10-09#2

Lowered the batch pool and the errors stopped. But we're growing and I don't want to keep playing whack-a-mole with pool sizes. Is PgBouncer transaction mode safe with prepared statements? Our ORM uses them by default, so I need to know before rolling anything out.

⚑ Report
☆ 0
T tls ·2026-10-12#3

Not with named prepared statements, no. Transaction mode can break them because the server connection is reused across clients. Use session pooling instead, which is safer but doesn't multiply connections as much. Check your ORM's prepared statement setting before you decide.

⚑ Report
☆ 0
S sd ·2026-10-21#4

We used session pooling with a max of 80 server connections and it solved the nightly spike without any app changes. Also set idle_in_transaction_session_timeout to 60s in the parameter group. We found a dozen leaked transactions from a bug that were holding connections open all day.

⚑ Report
Log in to reply.