pgbench for PostgreSQL: Why a TPS Number Needs Parameters

Updated

PostgreSQL elephant logo

A pgbench TPS figure is meaningless on its own. Publish it with the four things that produced it — scale factor, client count, duration, and whether pgbench ran on the database host. pgbench prints all four; quote the whole block, not the last line:

$ pgbench -c 10 -j 2 -T 60 hw_w13_pgbench
pgbench (17.10 (Homebrew))
starting vacuum...end.
transaction type: <builtin: TPC-B (sort of)>
scaling factor: 10
query mode: simple
number of clients: 10
number of threads: 2
maximum number of tries: 1
duration: 60 s
number of transactions actually processed: 903643
number of failed transactions: 0 (0.000%)
latency average = 0.664 ms
initial connection time = 19.565 ms
tps = 15064.093032 (without initial connection time)

15,064 tps. On this laptop, with pgbench and the server sharing its CPUs, at scale 10, for 60 seconds, with the machine’s 1-minute load average at 19. Every clause there moves the number.

First: -i destroys the pgbench tables that are already there

The first initialization step of pgbench -i -s 10 mytestdb is documented in pgbench --help: -I, --init-steps=[dtgGvpf]+ (default "dtgvp"), where d is “drop any existing pgbench tables”. Marking a row, then re-initializing at a different scale:

rows after -i -s 10 : 1000000
sentinel present    : REAL CUSTOMER DATA

$ pgbench -i -s 1
dropping old tables...
creating tables...
generating data (client-side)...
done in 0.10 s (drop tables 0.01 s, create tables 0.00 s, ...)

rows now                  : 100000
sentinel now              : ''
non-pgbench table survived: application table, nothing to do with pgbench

Your own tables are safe; anything named pgbench_accounts, pgbench_branches, pgbench_history or pgbench_tellers is not. Use a purpose-made database. (-s 10 is exactly 1,000,000 rows in pgbench_accounts — 100,000 accounts per scale unit.)

The scale factor is part of the result, not a setup detail

Two runs, identical in every other respect — same host, 10 clients, 2 threads, 30 seconds, minutes apart:

scaling factor: 1     latency average = 1.340 ms   tps = 7460.135419
scaling factor: 100   latency average = 0.840 ms   tps = 11904.188387

A 60% difference from one flag. The man page says why: “the initialization scale factor (-s) should be at least as large as the largest number of clients you intend to test (-c); else you’ll mostly be measuring update contention. There are only -s rows in the pgbench_branches table, and every transaction wants to update one of them.” At -s 1, ten clients queue for one branch row — that run measured lock waiting, and the lock views show it happening.

-c and -j are not the same knob

-c is database sessions; -j is threads inside pgbench driving them. Same 32 clients, same 30 seconds, scale 100:

number of threads: 1   latency average = 3.577 ms   tps = 8945.202114
number of threads: 8   latency average = 3.299 ms   tps = 9698.559292

8% of the throughput was the benchmark client, not the database: “A limitation of pgbench is that it can itself become the bottleneck when trying to test a large number of client sessions. This can be alleviated by running pgbench on a different machine from the database server.” Every run here has pgbench and PostgreSQL on the same host, which is why none of these numbers should be read as this machine’s PostgreSQL capacity. Asking for more threads than clients is quietly ignored — -c 2 -j 4 reports number of threads: 2.

The default script is not your workload

The built-in transaction is five statements — three updates, an insert and a select, all by primary key — labelled by pgbench itself <builtin: TPC-B (sort of)>. Swap it for the read-only built-in with -S and change nothing else:

read-write (default) : tps = 15064.093032   latency average = 0.664 ms
read-only (-S)       : tps = 118731.604617  latency average = 0.084 ms

Same host, same 10 clients, same 60 seconds, same scale 10: 7.9× apart. Any claim of the form “PostgreSQL does N tps” is mostly a statement about which script someone ran. For your own workload put the SQL in a file and use -f; for the query-level question underneath, EXPLAIN ANALYZE answers it and pgbench does not.

The same command does not give the same number

The identical command, three times in a row, on the same machine:

run 1  load=23.08  tps = 6218.895102
run 2  load=26.00  tps = 4999.257744
run 3  load=28.41  tps = 5693.620998

A 24% spread with nothing changed — and the same parameters earlier in the session, at a load average near 14, returned 11,904 tps. The variable was the rest of the machine. Hence the man page guidance: “It is very easy to use pgbench to produce completely meaningless numbers… never believe any test that runs for only a few seconds. Use the -t or -T option to make the run last at least a few minutes… It’s a good idea to try the test run a few times, to find out if your numbers are reproducible or not.”

By that standard the runs here are too short, which is why they are published with their load averages. Reproduce them and expect different figures: the parameters transfer, the numbers do not. What transfers is what pgbench-for-performance-testing.sh checks: the parameters pgbench reports (scale, clients, threads, duration) and what -i does to a marked row, both in a scratch database that it leaves for you to drop, plus the man-page lines quoted here — never a tps value.

Apache Bench cannot benchmark PostgreSQL

Apache Bench is not a PostgreSQL benchmarking tool. It cannot connect to PostgreSQL. Its entire usage line is ab [options] [http[s]://]hostname[:port]/path, and its help output mentions “postgres”, “sql” and “jdbc” zero times — checked against ApacheBench 2.3 here. It speaks HTTP and nothing else.

What was run here

PostgreSQL 17.10 (Homebrew) on 127.0.0.1:55432 with shared_buffers = 256MB, max_connections = 100, fsync = on, synchronous_commit = on, wal_level = replica — an untuned default cluster, not a tuned benchmark host. pgbench 17.10, macOS 26.6 arm64, Apple M3 Max, 14 cores, 36 GiB, client and server on the same machine, host load average between 14 and 28 during the runs. Scratch database hw_w13_pgbench; no existing database was initialized or dropped. Nothing here was run on Linux, on a separate client machine, on a tuned configuration, or for the “at least a few minutes” the documentation asks for — so these figures characterise this laptop under load, and nothing else. Treat any performance figure without its parameters, including on this page, as unusable.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.