pgbench — run a benchmark test on PostgreSQL
pgbench -i [option...] [dbname]
pgbench [option...] [dbname]
pgbench is a simple program for running benchmark tests on PostgreSQL. It runs the same sequence of SQL commands over and over, possibly in multiple concurrent database sessions, and then calculates the average transaction rate (transactions per second). By default, pgbench tests a scenario that is loosely based on TPC-B, involving five SELECT, UPDATE, and INSERT commands per transaction. However, it is easy to test other cases by writing your own transaction script files.
Typical output from pgbench looks like:
transaction type: <builtin: TPC-B (sort of)> scaling factor: 10 query mode: simple number of clients: 10 number of threads: 1 number of transactions per client: 1000 number of transactions actually processed: 10000/10000 latency average = 11.013 ms latency stddev = 7.351 ms initial connection time = 45.758 ms tps = 896.967014 (without initial connection time)
The first six lines report some of the most important parameter settings. The next line reports the number of transactions completed and intended (the latter being just the product of number of clients and number of transactions per client); these will be equal unless the run failed before completion. (In -T mode, only the actual number of transactions is printed.) The last line reports the number of transactions per second.
The default TPC-B-like transaction test requires specific tables to be set up beforehand. pgbench should be invoked with the -i (initialize) option to create and populate these tables. (When you are testing a custom script, you don't need this step, but will instead need to do whatever setup your test needs.) Initialization looks like:
pgbench -i [other-options]dbname
where dbname is the name of the already-created database to test in. (You may also need -h, -p, and/or -U options to specify how to connect to the database server.)
pgbench -i creates four tables pgbench_accounts, pgbench_branches, pgbench_history, and pgbench_tellers, destroying any existing tables of these names. Be very careful to use another database if you have tables having these names!
At the default “scale factor” of 1, the tables initially contain this many rows:
table # of rows --------------------------------- pgbench_branches 1 pgbench_tellers 10 pgbench_accounts 100000 pgbench_history 0
You can (and, for most purposes, probably should) increase the number of rows by using the -s (scale factor) option. The -F (fillfactor) option might also be used at this point.
Once you have done the necessary setup, you can run your benchmark with a command that doesn't include -i, that is
pgbench [options]dbname
In nearly all cases, you'll need some options to make a useful test. The most important options are -c (number of clients), -t (number of transactions), -T (time limit), and -f (specify a custom script file). See below for a full list.
The following is divided into three subsections. Different options are used during database initialization and while running benchmarks, but some options are useful in both cases.
pgbench accepts the following command-line initialization arguments:
dbnameSpecifies the name of the database to test in. If this is not specified, the environment variable PGDATABASE is used. If that is not set, the user name specified for the connection is used.
-i--initializeRequired to invoke initialization mode.
-I init_steps--init-steps=init_stepsPerform just a selected set of the normal initialization steps. init_steps specifies the initialization steps to be performed, using one character per step. Each step is invoked in the specified order. The default is dtgvp. The available steps are:
d (Drop)Drop any existing pgbench tables.
t (create Tables)Create the tables used by the standard pgbench scenario, namely pgbench_accounts, pgbench_branches, pgbench_history, and pgbench_tellers.
g or G (Generate data, client-side or server-side)Generate data and load it into the standard tables, replacing any data already present.
With g (client-side data generation), data is generated in pgbench client and then sent to the server. This uses the client/server bandwidth extensively through a COPY. Using g causes logging to print one message every 100,000 rows while generating data for the pgbench_accounts table.
With G (server-side data generation), only small queries are sent from the pgbench client and then data is actually generated in the server. No significant bandwidth is required for this variant, but the server will do more work. Using G causes logging not to print any progress message while generating data.
The default initialization behavior uses client-side data generation (equivalent to g).
v (Vacuum)Invoke VACUUM on the standard tables.
p (create Primary keys)Create primary key indexes on the standard tables.
f (create Foreign keys)Create foreign key constraints between the standard tables. (Note that this step is not performed by default.)
-F fillfactor--fillfactor=fillfactorCreate the pgbench_accounts, pgbench_tellers and pgbench_branches tables with the given fillfactor. Default is 100.
-n--no-vacuumPerform no vacuuming during initialization. (This option suppresses the v initialization step, even if it was specified in -I.)
-q--quietSwitch logging to quiet mode, producing only one progress message per 5 seconds. The default logging prints one message each 100,000 rows, which often outputs many lines per second (especially on good hardware).
This setting has no effect if G is specified in -I.
-s scale_factor--scale=scale_factorMultiply the number of rows generated by the scale factor. For example, -s 100 will create 10,000,000 rows in the pgbench_accounts table. Default is 1. When the scale is 20,000 or larger, the columns used to hold account identifiers (aid columns) will switch to using larger integers (bigint), in order to be big enough to hold the range of account identifiers.
--foreign-keysCreate foreign key constraints between the standard tables. (This option adds the f step to the initialization step sequence, if it is not already present.)
--index-tablespace=index_tablespaceCreate indexes in the specified tablespace, rather than the default tablespace.
--partition-method=NAMECreate a partitioned pgbench_accounts table with NAME method. Expected values are range or hash. This option requires that --partitions is set to non-zero. If unspecified, default is range.
--partitions=NUMCreate a partitioned pgbench_accounts table with NUM partitions of nearly equal size for the scaled number of accounts. Default is 0, meaning no partitioning.
--tablespace=tablespaceCreate tables in the specified tablespace, rather than the default tablespace.
--unlogged-tablesCreate all tables as unlogged tables, rather than permanent tables.
pgbench accepts the following command-line benchmarking arguments:
-b scriptname[@weight]--builtin=scriptname[@weight]Add the specified built-in script to the list of scripts to be executed. Available built-in scripts are: tpcb-like, simple-update and select-only. Unambiguous prefixes of built-in names are accepted. With the special name list, show the list of built-in scripts and exit immediately.
Optionally, write an integer weight after @ to adjust the probability of selecting this script versus other ones. The default weight is 1. See below for details.
-c clients--client=clientsNumber of clients simulated, that is, number of concurrent database sessions. Default is 1.
-C--connectEstablish a new connection for each transaction, rather than doing it just once per client session. This is useful to measure the connection overhead.
-d--debugPrint debugging output.
-D varname=value--define=varname=valueDefine a variable for use by a custom script (see below). Multiple -D options are allowed.
-f filename[@weight]--file=filename[@weight]Add a transaction script read from filename to the list of scripts to be executed.
Optionally, write an integer weight after @ to adjust the probability of selecting this script versus other ones. The default weight is 1. (To use a script file name that includes an @ character, append a weight so that there is no ambiguity, for example filen@me@1.) See below for details.
-j threads--jobs=threadsNumber of worker threads within pgbench. Using more than one thread can be helpful on multi-CPU machines. Clients are distributed as evenly as possible among available threads. Default is 1.
-l--logWrite information about each transaction to a log file. See below for details.
-L limit--latency-limit=limitTransactions that last more than limit milliseconds are counted and reported separately, as late.
When throttling is used (--rate=...), transactions that lag behind schedule by more than limit ms, and thus have no hope of meeting the latency limit, are not sent to the server at all. They are counted and reported separately as skipped.
-M querymode--protocol=querymodeProtocol to use for submitting queries to the server:
simple: use simple query protocol.
extended: use extended query protocol.
prepared: use extended query protocol with prepared statements.
In the prepared mode, pgbench reuses the parse analysis result starting from the second query iteration, so pgbench runs faster than in other modes.
The default is simple query protocol. (See Chapter 53 for more information.)
-n--no-vacuumPerform no vacuuming before running the test. This option is necessary if you are running a custom test scenario that does not include the standard tables pgbench_accounts, pgbench_branches, pgbench_history, and pgbench_tellers.
-N--skip-some-updatesRun built-in simple-update script. Shorthand for -b simple-update.
-P sec--progress=secShow progress report every sec seconds. The report includes the time since the beginning of the run, the TPS since the last report, and the transaction latency average and standard deviation since the last report. Under throttling (-R), the latency is computed with respect to the transaction scheduled start time, not the actual transaction beginning time, thus it also includes the average schedule lag time.
-r--report-latenciesReport the average per-statement latency (execution time from the perspective of the client) of each command after the benchmark finishes. See below for details.
-R rate--rate=rateExecute transactions targeting the specified rate instead of running as fast as possible (the default). The rate is given in transactions per second. If the targeted rate is above the maximum possible rate, the rate limit won't impact the results.
The rate is targeted by starting transactions along a Poisson-distributed schedule time line. The expected start time schedule moves forward based on when the client first started, not when the previous transaction ended. That approach means that when transactions go past their original scheduled end time, it is possible for later ones to catch up again.
When throttling is active, the transaction latency reported at the end of the run is calculated from the scheduled start times, so it includes the time each transaction had to wait for the previous transaction to finish. The wait time is called the schedule lag time, and its average and maximum are also reported separately. The transaction latency with respect to the actual transaction start time, i.e., the time spent executing the transaction in the database, can be computed by subtracting the schedule lag time from the reported latency.
If --latency-limit is used together with --rate, a transaction can lag behind so much that it is already over the latency limit when the previous transaction ends, because the latency is calculated from the scheduled start time. Such transactions are not sent to the server, but are skipped altogether and counted separately.
A high schedule lag time is an indication that the system cannot process transactions at the specified rate, with the chosen number of clients and threads. When the average transaction execution time is longer than the scheduled interval between each transaction, each successive transaction will fall further behind, and the schedule lag time will keep increasing the longer the test run is. When that happens, you will have to reduce the specified transaction rate.
-s scale_factor--scale=scale_factorReport the specified scale factor in pgbench's output. With the built-in tests, this is not necessary; the correct scale factor will be detected by counting the number of rows in the pgbench_branches table. However, when testing only custom benchmarks (-f option), the scale factor will be reported as 1 unless this option is used.
-S--select-onlyRun built-in select-only script. Shorthand for -b select-only.
-t transactions--transactions=transactionsNumber of transactions each client runs. Default is 10.
-T seconds--time=secondsRun the test for this many seconds, rather than a fixed number of transactions per client. -t and -T are mutually exclusive.
-v--vacuum-allVacuum all four standard tables before running the test. With neither -n nor -v, pgbench will vacuum the pgbench_tellers and pgbench_branches tables, and will truncate pgbench_history.