Learn Labs
Monitoring & Performance

Load Testing with pgbench

Finding the cluster's limit, the bottleneck nobody expected, what PgBouncer changes, and how far replicas scale reads

labs/loadtest.py drives the cluster with pgbench, which ships with Postgres. Its default transaction is TPC-B-like: three UPDATEs, a SELECT and an INSERT against accounts, tellers and branches, on scale 20 (2 million accounts). Each run lasts 20 seconds. Two choices make the numbers trustworthy:

  • Percentiles, not the average. pgbench prints an average latency; the lab writes every transaction's latency to a log (--log) and computes p50 and p99 from it. The average hides exactly the slow requests users notice.
  • CPU of every hop, sampled mid-run. HAProxy, PgBouncer, the primary and the pgbench client itself. A throughput number without knowing what was saturated is a number about nothing.
docker compose -f compose_prod.yaml run --rm labs python loadtest.py

The first run measured HAProxy

== haproxy:5001 -S        (read-only, 32 clients)
haproxy 52.97%     pg-1 65.41%     pg-3 69.91%
== pgbouncer:6432        (read/write, 32 clients)
haproxy 49.94%     pg-2 100.54%    pgbouncer 47.07%

The first run said reads on two replicas were no faster than on the primary (24,476 vs 24,635 transactions a second), and PgBouncer topped out at 2,600. Sampling docker stats during the runs showed why: HAProxy sat at 50% CPU in every test, which was exactly its limit (cpus: "0.5" in compose_prod.yaml). Every query passes through it, and the replicas had headroom. The test wasn't measuring Postgres at all.

With HAProxy at 2 CPUs, the same tests:

1. Clients vs throughput and latency
   8 clients, direct          2,906 tps  p50    2.33 ms  p99    7.28 ms  | CPU cores: haproxy 0.5 pgbouncer 0.0 primary 1.2
   8 clients, pgbouncer       2,129 tps  p50    3.02 ms  p99   15.83 ms  | CPU cores: haproxy 0.4 pgbouncer 0.4 primary 0.9
  32 clients, direct          5,745 tps  p50    4.18 ms  p99   24.60 ms  | CPU cores: haproxy 0.8 pgbouncer 0.0 primary 2.0
  32 clients, pgbouncer       4,056 tps  p50    7.22 ms  p99   17.94 ms  | CPU cores: haproxy 0.8 pgbouncer 0.7 primary 1.7
  64 clients, direct          4,036 tps  p50    7.88 ms  p99  123.10 ms
  64 clients, pgbouncer       4,044 tps  p50   15.04 ms  p99   26.70 ms
 128 clients, direct          3,611 tps  p50   16.01 ms  p99  275.29 ms
 128 clients, pgbouncer       3,945 tps  p50   30.85 ms  p99   58.20 ms
3. 300 clients (max_connections = 200)
 300 clients, direct          FAILED: FATAL:  sorry, too many clients already
 300 clients, pgbouncer       3,975 tps  p50   73.39 ms  p99  126.30 ms
4. Read-only (-S), 32 clients
 select-only, primary        30,698 tps  p50    0.22 ms  p99    2.11 ms  | primary 2.0
 select-only, replicas (2)   68,006 tps  p50    0.15 ms  p99    0.47 ms  | haproxy 1.8
8 clients, direct2,9068, PgBouncer2,12932, direct5,745 (primary at 2.0 cores)32, PgBouncer4,056128, direct3,611128, PgBouncer3,945300, directfails: too many clients300, PgBouncer3,975
32 clients, direct24.6 ms32, PgBouncer17.9 ms64, direct123 ms64, PgBouncer26.7 ms128, direct275 ms128, PgBouncer58.2 ms

Reading the results

The limit is the primary's CPU. Direct connections peaked at 5,745 transactions a second with 32 clients, with the primary at 2.0 cores, its container limit. More clients made it worse, not better: 4,036 at 64, 3,611 at 128. Past saturation, extra connections only add contention (most likely row locks on the 20 pgbench_branches rows that every transaction updates, plus context switching), and p99 went from 25 ms to 275 ms.

PgBouncer trades peak for stability. With 20 server connections it can't reach the direct peak at 32 clients (4,056: some clients wait for a connection), but its throughput stays flat at about 4,000 from 32 to 300 clients and its p99 stays low: 58 ms at 128 clients where direct was 275. It queues clients instead of letting them all fight inside Postgres. And at 300 clients, direct connections fail outright (max_connections is 200) while PgBouncer serves them all.

Replicas scale reads. Read-only through HAProxy to two replicas: 68,006 transactions a second, 2.2 times the primary alone, with lower latency. At that rate HAProxy was using 1.8 cores, near its new 2-core limit, so the next bottleneck is already visible.

A connection per transaction

2. A new connection per transaction (-C), 32 clients
 -C, direct          431 tps  p50 43.06 ms  p99 245.33 ms  | CPU cores: primary 1.8 client 3.4
 -C, pgbouncer       542 tps  p50 42.42 ms  p99 105.71 ms  | CPU cores: primary 0.4 client 5.7

Opening a connection for every transaction (PHP, serverless functions, a missing pool) cut throughput by more than 10 times. The expected story is "PgBouncer saves the cost of Postgres forking a backend", and it did: the primary dropped from 1.8 cores to 0.4. But throughput barely moved, because the pgbench client was the bottleneck at 3.4 to 5.7 cores. Each new connection costs a TLS handshake and a SCRAM exchange, and SCRAM is deliberately slow (4,096 rounds of PBKDF2) on both sides. A pooler on the database side can't fix a client that reconnects for every query; a pool in the application can.

On this page