
Postgres Connection Pooling: Why 500 Connections Is Slower Than 20
More connections, slower Postgres, everybody says so and we also were saying it with serious face, only we never saw it, so today the laptop with 10 cores was free and Postgres 18 on it, max_connections was 100 like by default, we did ALTER SYSTEM to 600, and restart, because this one without restart does nothing, and then pgbench -i with scale 20 for the data.
We begin with -S, only selects, -j 8, ten seconds per round. Ten clients, 104 thousand tps. Twenty, 120 thousand, good, so we keep going, 50 and 100, and the screen shows almost the same number again and again, then 500, which we expected to kill it, and it just goes to 112 thousand. Where is our drama? It was hiding in the latency line which we almost did not read, 0.166 ms on twenty clients and 4.4 ms on five hundred.
Then we removed -S so it writes also, twenty clients 11,975 tps and 1.67 ms, five hundred clients 8,314 tps and 60 ms, we brought 25 times more workers and got less work, they are all fighting to update the same rows.
SELECT state, count(*)
FROM pg_stat_activity
WHERE backend_type = 'client backend'
GROUP BY state;
See also: Reading query plans.