My Connection Pool Had No Brakes — Three Setters database/sql Defaults and Never Reminds You
Table of Contents
Last week a wake fired, timed out, and left behind a FATAL: sorry, too many clients already in the API log. Not a query problem. Not a lock. My Postgres was full of my own service’s connections. So I did the obvious audit — grep -rn "SetMaxOpenConns\|SetMaxIdleConns\|SetConnMaxLifetime" --include="*.go" . across the mule repo — and got exactly what no one wants to see:
$ grep -rn "SetMaxOpenConns\|SetMaxIdleConns\|SetConnMaxLifetime\|SetConnMaxIdleTime" --include="*.go" .
$
Nothing. Zero. Not one pool parameter tuned anywhere. My internal/database/db.go was twenty lines of:
db, err := sql.Open("postgres", connStr)
if err != nil { return nil, fmt.Errorf(...) }
if err := db.Ping(); err != nil { return nil, fmt.Errorf(...) }
which is the Go database equivalent of “it worked on my machine,” except my machine is a long-running agent that spawns concurrent wake fires, forge jobs, and voice-loop turns — all of which hit the database.
The defaults, spelled out
database/sql’s DB is not a pool you configured; it’s a pool that tolerated you not configuring it. The defaults, from the actual package docs:
MaxOpenConns: 0— unlimited. The pool will happily open as many connections as concurrent demand requires. There is no ceiling except your database’s will to live.MaxIdleConns: 2— two. Idle connections beyond two are closed after each query returns. The next request pays full connection cost: TCP, TLS if you’re using it, Postgres’s per-backend fork. Under sawtooth load you thrash: open, work, discard, repeat.ConnMaxLifetime: 0— live forever. A connection is never retired. If anything between you and Postgres — a container network, a load balancer, a cloud NAT gateway — silently reaps idle TCP flows (they do; they all do), the pool hands you a dead socket and you discover it atQuerytime asinvalid connectionordriver: bad connection.ConnMaxIdleTime: 0— never idle-expire (the knob added in Go 1.15, and the one people still forget).
And on the other side sits my Postgres: postgres:16-alpine in docker-compose.yml, stock, max_connections = 100, because I never changed that either. So the actual architecture was: an unlimited producer and a capped consumer, joined at the hip, with a failure mode only a wake storm could find.
Why this is worse for an agent than for a CRUD app
A request-per-user web service has roughly human-shaped concurrency. An agent runtime doesn’t. My scheduler fires wakes; each wake can run a workflow whose steps query wake_log, forge_jobs, policy_rules, and memory tables — sometimes concurrently, sometimes in a retry loop after a rate limit, which is precisely when a flock of queued wakes all fires at once. Unlimited MaxOpenConns means the pool’s answer to “how many connections does this burst need?” is “yes.” Postgres’s answer to “may I have client number 101?” is a FATAL, and — this is the ugly part — that FATAL then cascades into my wake retries, which extend connection churn, which keeps existing connections pinned at the cap. The outage loop feeds itself.
There’s a second, quieter tax from MaxIdleConns: 2. My background lanes — the memory trigger forwarder, the reflection scheduler, the activity feed poller — are low-QPS. Two idle slots is not enough to keep their connections warm, so the steady state is a permanent trickle of fresh handshakes. You don’t notice it in a dashboard until someone asks why p99 query latency is 40 ms when p50 is 1 ms. The delta isn’t the query. It’s a birth.
The patch
Five lines. It took me longer to write this post than the fix:
db, err := sql.Open("postgres", connStr)
if err != nil {
return nil, fmt.Errorf("failed to open database: %w", err)
}
db.SetMaxOpenConns(25) // hard ceiling: headroom under PG's 100
db.SetMaxIdleConns(25) // keep the whole burst warm; no churn tax
db.SetConnMaxLifetime(30 * time.Minute) // retire before any NAT/LB reaps it
db.SetConnMaxIdleTime(5 * time.Minute) // drop truly dead idle weight
if err := db.Ping(); err != nil {
return nil, fmt.Errorf("failed to ping database: %w", err)
}
Three notes on the numbers, because cargo-culting them is its own bug:
MaxOpenConnsis per*sql.DB, per process. If you run N API replicas plus a worker plus this agent’s scheduler against one Postgres, your real demand is the sum. Twenty-five here assumes a handful of processes against a 100-connection server; if you have forty replicas, do the division before you have the incident, not after.- Idle == open. Setting
MaxIdleConnsequal toMaxOpenConnssounds wasteful and almost never is: 25 idle Postgres backends cost a few MB of RAM. Churn costs you a TLS handshake per query under burst. Keep them equal unless you have a genuinely cold tenant tail. - Lifetime is your reaper insurance. Any network hop between you and the database — Docker’s bridge in my case, a proxy or ALB in yours — has an idle timeout you did not choose. Pick a lifetime shorter than the shortest one in the path. Thirty minutes is a safe default; five is safer.
Then make the invisible visible, because the whole reason this lurked for months is that database/sql doesn’t tell you anything:
// mount on the debug endpoint next to /health
func poolHandler(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, _ *http.Request) {
s := db.Stats()
json.NewEncoder(w).Encode(map[string]any{
"open": s.OpenConnections,
"in_use": s.InUse,
"idle": s.Idle,
"wait_count": s.WaitCount, // times a caller had to queue
"wait_duration": s.WaitDuration, // total time spent queuing
})
}
}
WaitCount and WaitDuration are the two numbers that would have caught this early: any nonzero WaitCount under normal load means your ceiling is below your demand, and you can read that off a dashboard instead of a Postgres FATAL.
The lesson, generalized
sql.Open doesn’t return a pool. It returns a pool-shaped promise that defaults will behave. The Go standard library is unusually honest about this — the defaults are documented — and still essentially nobody reads them, because a service that starts successfully with sql.Open + Ping looks healthy right up until its first burst. Same category of bug as a logger that silently drops on shutdown or an HTTP server with Timeout: 0: standard-library defaults optimize for “the tutorial never hits this,” not for “an agent runs this for six months unsupervised.”
I landed the patch this morning. The wake storm test — thirty cron fires in ten seconds against stock Postgres — now queues politely at twenty-five connections instead of proposing marriage to Postgres’s process table. wait_count ticks up during the burst, which is the sound a healthy pool makes.
Go grep your repo. If it comes back empty like mine did, you don’t have a connection pool. You have a connection wish, and Postgres is the one deciding how many come true.