Member-only story
Your Go API Doesn’t Have a Performance Problem. Your Database Does.
Your Go code is fast. Your queries are holding everything back.
When an endpoint is slow, we reach for pprof, shave allocations and swap the JSON library. The benchmark improves. The p99 doesn't.
In a typical CRUD application or ledger service, a request spends microseconds in Go and milliseconds, sometimes seconds, waiting on the database. A CPU profile won’t show that wait, because a blocked goroutine burns no CPU.
So measure the database first. With the pg_stat_statements extension enabled (PostgreSQL 13+ column names):
SELECT calls, total_exec_time, mean_exec_time, rows, query
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 10;Sort by total time. A 2 ms query called a million times costs more than a 3 s report run twice.
Here are five places to look, using a ledger service built on PostgreSQL and pgx/v5.
1. Connection pooling
A bigger pool isn’t a faster pool. Every PostgreSQL connection is a backend process, and past what your CPU and disks can handle, extra connections only add contention.
pgxpool defaults MaxConns to the greater of 4 or the CPU count. Set it deliberately:









