Do you even lift?

As well as the built-in scenarios, pgbench allows you to run a custom “transaction script”; making it a viable alternative (for some scenarios) to driving load testing via your application, e.g. using a locust swarm.

It’s not a full blown programming language (or even a DSL really), but you can for example: insert a row, and then update it:

\set uid random(1, 100000 * :scale)

BEGIN;
INSERT INTO foo (user_id, ...) VALUES (:uid, ...) RETURNING id \gset g_
COMMIT;

\sleep 1s

BEGIN;
UPDATE foo SET bar = 1 WHERE id = :g_id;
COMMIT;

You can then point pgbench at your DB, and this script, with say 20 clients for 5 mins:

docker run -it --rm --network=host -v $PWD:/app -w /app -e PGPASSWORD=postgres postgres:15 pgbench -h ... -U ... -d ... -f foo.sql  -c 20 -T 600 -j 8
...

...
number of transactions actually processed: 81752
number of failed transactions: 0 (0.000%)
latency average = 146.670 ms
initial connection time = 560.452 ms
tps = 136.360106 (without initial connection time)

The transaction count here is actually the number of times the script ran (if you have multiple transactions inside it).

Leave a comment