4 pgbench — run a benchmark test on PostgreSQL
8 pgbench -i [option...] [dbname]
10 pgbench [option...] [dbname]
14 pgbench is a simple program for running benchmark tests on PostgreSQL.
15 It runs the same sequence of SQL commands over and over, possibly in
16 multiple concurrent database sessions, and then calculates the average
17 transaction rate (transactions per second). By default, pgbench tests a
18 scenario that is loosely based on TPC-B, involving five SELECT, UPDATE,
19 and INSERT commands per transaction. However, it is easy to test other
20 cases by writing your own transaction script files.
22 Typical output from pgbench looks like:
23 transaction type: <builtin: TPC-B (sort of)>
28 maximum number of tries: 1
29 number of transactions per client: 1000
30 number of transactions actually processed: 10000/10000
31 number of failed transactions: 0 (0.000%)
32 latency average = 11.013 ms
33 latency stddev = 7.351 ms
34 initial connection time = 45.758 ms
35 tps = 896.967014 (without initial connection time)
37 The first seven lines report some of the most important parameter
38 settings. The sixth line reports the maximum number of tries for
39 transactions with serialization or deadlock errors (see Failures and
40 Serialization/Deadlock Retries for more information). The eighth line
41 reports the number of transactions completed and intended (the latter
42 being just the product of number of clients and number of transactions
43 per client); these will be equal unless the run failed before
44 completion or some SQL command(s) failed. (In -T mode, only the actual
45 number of transactions is printed.) The next line reports the number of
46 failed transactions due to serialization or deadlock errors (see
47 Failures and Serialization/Deadlock Retries for more information). The
48 last line reports the number of transactions per second.
50 The default TPC-B-like transaction test requires specific tables to be
51 set up beforehand. pgbench should be invoked with the -i (initialize)
52 option to create and populate these tables. (When you are testing a
53 custom script, you don't need this step, but will instead need to do
54 whatever setup your test needs.) Initialization looks like:
55 pgbench -i [ other-options ] dbname
57 where dbname is the name of the already-created database to test in.
58 (You may also need -h, -p, and/or -U options to specify how to connect
59 to the database server.)
63 pgbench -i creates four tables pgbench_accounts, pgbench_branches,
64 pgbench_history, and pgbench_tellers, destroying any existing tables of
65 these names. Be very careful to use another database if you have tables
68 At the default “scale factor” of 1, the tables initially contain this
71 ---------------------------------
74 pgbench_accounts 100000
77 You can (and, for most purposes, probably should) increase the number
78 of rows by using the -s (scale factor) option. The -F (fillfactor)
79 option might also be used at this point.
81 Once you have done the necessary setup, you can run your benchmark with
82 a command that doesn't include -i, that is
83 pgbench [ options ] dbname
85 In nearly all cases, you'll need some options to make a useful test.
86 The most important options are -c (number of clients), -t (number of
87 transactions), -T (time limit), and -f (specify a custom script file).
88 See below for a full list.
92 The following is divided into three subsections. Different options are
93 used during database initialization and while running benchmarks, but
94 some options are useful in both cases.
96 Initialization Options
98 pgbench accepts the following command-line initialization arguments:
102 Specifies the name of the database to test in. If this is not
103 specified, the environment variable PGDATABASE is used. If that
104 is not set, the user name specified for the connection is used.
108 Required to invoke initialization mode.
111 --init-steps=init_steps #
112 Perform just a selected set of the normal initialization steps.
113 init_steps specifies the initialization steps to be performed,
114 using one character per step. Each step is invoked in the
115 specified order. The default is dtgvp. The available steps are:
118 Drop any existing pgbench tables.
121 Create the tables used by the standard pgbench scenario,
122 namely pgbench_accounts, pgbench_branches,
123 pgbench_history, and pgbench_tellers.
125 g or G (Generate data, client-side or server-side) #
126 Generate data and load it into the standard tables,
127 replacing any data already present.
129 With g (client-side data generation), data is generated in
130 pgbench client and then sent to the server. This uses the
131 client/server bandwidth extensively through a COPY.
132 pgbench uses the FREEZE option to load data into ordinary
133 (non-partition) tables with version 14 or later of
134 PostgreSQL to speed up subsequent VACUUM. Using g causes
135 logging to print one message every 100,000 rows while
136 generating data for all tables.
138 With G (server-side data generation), only small queries
139 are sent from the pgbench client and then data is actually
140 generated in the server. No significant bandwidth is
141 required for this variant, but the server will do more
142 work. Using G causes logging not to print any progress
143 message while generating data.
145 The default initialization behavior uses client-side data
146 generation (equivalent to g).
149 Invoke VACUUM on the standard tables.
151 p (create Primary keys) #
152 Create primary key indexes on the standard tables.
154 f (create Foreign keys) #
155 Create foreign key constraints between the standard
156 tables. (Note that this step is not performed by default.)
159 --fillfactor=fillfactor #
160 Create the pgbench_accounts, pgbench_tellers and
161 pgbench_branches tables with the given fillfactor. Default is
166 Perform no vacuuming during initialization. (This option
167 suppresses the v initialization step, even if it was specified
172 Switch logging to quiet mode, producing only one progress
173 message per 5 seconds. The default logging prints one message
174 each 100,000 rows, which often outputs many lines per second
175 (especially on good hardware).
177 This setting has no effect if G is specified in -I.
180 --scale=scale_factor #
181 Multiply the number of rows generated by the scale factor. For
182 example, -s 100 will create 10,000,000 rows in the
183 pgbench_accounts table. Default is 1. When the scale is 20,000
184 or larger, the columns used to hold account identifiers (aid
185 columns) will switch to using larger integers (bigint), in order
186 to be big enough to hold the range of account identifiers.
189 Create foreign key constraints between the standard tables.
190 (This option adds the f step to the initialization step
191 sequence, if it is not already present.)
193 --index-tablespace=index_tablespace #
194 Create indexes in the specified tablespace, rather than the
197 --partition-method=NAME #
198 Create a partitioned pgbench_accounts table with NAME method.
199 Expected values are range or hash. This option requires that
200 --partitions is set to non-zero. If unspecified, default is
204 Create a partitioned pgbench_accounts table with NUM partitions
205 of nearly equal size for the scaled number of accounts. Default
206 is 0, meaning no partitioning.
208 --tablespace=tablespace #
209 Create tables in the specified tablespace, rather than the
213 Create all tables as unlogged tables, rather than permanent
218 pgbench accepts the following command-line benchmarking arguments:
220 -b scriptname[@weight]
221 --builtin=scriptname[@weight] #
222 Add the specified built-in script to the list of scripts to be
223 executed. Available built-in scripts are: tpcb-like,
224 simple-update and select-only. Unambiguous prefixes of built-in
225 names are accepted. With the special name list, show the list of
226 built-in scripts and exit immediately.
228 Optionally, write an integer weight after @ to adjust the
229 probability of selecting this script versus other ones. The
230 default weight is 1. See below for details.
234 Number of clients simulated, that is, number of concurrent
235 database sessions. Default is 1.
239 Establish a new connection for each transaction, rather than
240 doing it just once per client session. This is useful to measure
241 the connection overhead.
244 --define=varname=value #
245 Define a variable for use by a custom script (see below).
246 Multiple -D options are allowed.
249 --file=filename[@weight] #
250 Add a transaction script read from filename to the list of
251 scripts to be executed.
253 Optionally, write an integer weight after @ to adjust the
254 probability of selecting this script versus other ones. The
255 default weight is 1. (To use a script file name that includes an
256 @ character, append a weight so that there is no ambiguity, for
257 example filen@me@1.) See below for details.
261 Number of worker threads within pgbench. Using more than one
262 thread can be helpful on multi-CPU machines. Clients are
263 distributed as evenly as possible among available threads.
268 Write information about each transaction to a log file. See
272 --latency-limit=limit #
273 Transactions that last more than limit milliseconds are counted
274 and reported separately, as late.
276 When throttling is used (--rate=...), transactions that lag
277 behind schedule by more than limit ms, and thus have no hope of
278 meeting the latency limit, are not sent to the server at all.
279 They are counted and reported separately as skipped.
281 When the --max-tries option is used, a transaction which fails
282 due to a serialization anomaly or from a deadlock will not be
283 retried if the total time of all its tries is greater than limit
284 ms. To limit only the time of tries and not their number, use
285 --max-tries=0. By default, the option --max-tries is set to 1
286 and transactions with serialization/deadlock errors are not
287 retried. See Failures and Serialization/Deadlock Retries for
288 more information about retrying such transactions.
291 --protocol=querymode #
292 Protocol to use for submitting queries to the server:
294 + simple: use simple query protocol.
295 + extended: use extended query protocol.
296 + prepared: use extended query protocol with prepared
299 In the prepared mode, pgbench reuses the parse analysis result
300 starting from the second query iteration, so pgbench runs faster
303 The default is simple query protocol. (See Chapter 54 for more
308 Perform no vacuuming before running the test. This option is
309 necessary if you are running a custom test scenario that does
310 not include the standard tables pgbench_accounts,
311 pgbench_branches, pgbench_history, and pgbench_tellers.
314 --skip-some-updates #
315 Run built-in simple-update script. Shorthand for -b
320 Show progress report every sec seconds. The report includes the
321 time since the beginning of the run, the TPS since the last
322 report, and the transaction latency average, standard deviation,
323 and the number of failed transactions since the last report.
324 Under throttling (-R), the latency is computed with respect to
325 the transaction scheduled start time, not the actual transaction
326 beginning time, thus it also includes the average schedule lag
327 time. When --max-tries is used to enable transaction retries
328 after serialization/deadlock errors, the report includes the
329 number of retried transactions and the sum of all retries.
332 --report-per-command #
333 Report the following statistics for each command after the
334 benchmark finishes: the average per-statement latency (execution
335 time from the perspective of the client), the number of
336 failures, and the number of retries after serialization or
337 deadlock errors in this command. The report displays retry
338 statistics only if the --max-tries option is not equal to 1.
342 Execute transactions targeting the specified rate instead of
343 running as fast as possible (the default). The rate is given in
344 transactions per second. If the targeted rate is above the
345 maximum possible rate, the rate limit won't impact the results.
347 The rate is targeted by starting transactions along a
348 Poisson-distributed schedule time line. The expected start time
349 schedule moves forward based on when the client first started,
350 not when the previous transaction ended. That approach means
351 that when transactions go past their original scheduled end
352 time, it is possible for later ones to catch up again.
354 When throttling is active, the transaction latency reported at
355 the end of the run is calculated from the scheduled start times,
356 so it includes the time each transaction had to wait for the
357 previous transaction to finish. The wait time is called the
358 schedule lag time, and its average and maximum are also reported
359 separately. The transaction latency with respect to the actual
360 transaction start time, i.e., the time spent executing the
361 transaction in the database, can be computed by subtracting the
362 schedule lag time from the reported latency.
364 If --latency-limit is used together with --rate, a transaction
365 can lag behind so much that it is already over the latency limit
366 when the previous transaction ends, because the latency is
367 calculated from the scheduled start time. Such transactions are
368 not sent to the server, but are skipped altogether and counted
371 A high schedule lag time is an indication that the system cannot
372 process transactions at the specified rate, with the chosen
373 number of clients and threads. When the average transaction
374 execution time is longer than the scheduled interval between
375 each transaction, each successive transaction will fall further
376 behind, and the schedule lag time will keep increasing the
377 longer the test run is. When that happens, you will have to
378 reduce the specified transaction rate.
381 --scale=scale_factor #
382 Report the specified scale factor in pgbench's output. With the
383 built-in tests, this is not necessary; the correct scale factor
384 will be detected by counting the number of rows in the
385 pgbench_branches table. However, when testing only custom
386 benchmarks (-f option), the scale factor will be reported as 1
387 unless this option is used.
391 Run built-in select-only script. Shorthand for -b select-only.
394 --transactions=transactions #
395 Number of transactions each client runs. Default is 10.
399 Run the test for this many seconds, rather than a fixed number
400 of transactions per client. -t and -T are mutually exclusive.
404 Vacuum all four standard tables before running the test. With
405 neither -n nor -v, pgbench will vacuum the pgbench_tellers and
406 pgbench_branches tables, and will truncate pgbench_history.
408 --aggregate-interval=seconds #
409 Length of aggregation interval (in seconds). May be used only
410 with -l option. With this option, the log contains per-interval
411 summary data, as described below.
414 Exit immediately when any client is aborted due to some error.
415 Without this option, even when a client is aborted, other
416 clients could continue their run as specified by -t or -T
417 option, and pgbench will print an incomplete results in this
420 Note that serialization failures or deadlock failures do not
421 abort the client, so they are not affected by this option. See
422 Failures and Serialization/Deadlock Retries for more
425 --failures-detailed #
426 Report failures in per-transaction and aggregation logs, as well
427 as in the main and per-script reports, grouped by the following
430 + serialization failures;
433 See Failures and Serialization/Deadlock Retries for more
436 --log-prefix=prefix #
437 Set the filename prefix for the log files created by --log. The
438 default is pgbench_log.
440 --max-tries=number_of_tries #
441 Enable retries for transactions with serialization/deadlock
442 errors and set the maximum number of these tries. This option
443 can be combined with the --latency-limit option which limits the
444 total time of all transaction tries; moreover, you cannot use an
445 unlimited number of tries (--max-tries=0) without
446 --latency-limit or --time. The default value is 1 and
447 transactions with serialization/deadlock errors are not retried.
448 See Failures and Serialization/Deadlock Retries for more
449 information about retrying such transactions.
451 --progress-timestamp #
452 When showing progress (option -P), use a timestamp (Unix epoch)
453 instead of the number of seconds since the beginning of the run.
454 The unit is in seconds, with millisecond precision after the
455 dot. This helps compare logs generated by various tools.
458 Set random generator seed. Seeds the system random number
459 generator, which then produces a sequence of initial generator
460 states, one for each thread. Values for seed may be: time (the
461 default, the seed is based on the current time), rand (use a
462 strong random source, failing if none is available), or an
463 unsigned decimal integer value. The random generator is invoked
464 explicitly from a pgbench script (random... functions) or
465 implicitly (for instance option --rate uses it to schedule
466 transactions). When explicitly set, the value used for seeding
467 is shown on the terminal. Any value allowed for seed may also be
468 provided through the environment variable PGBENCH_RANDOM_SEED.
469 To ensure that the provided seed impacts all possible uses, put
470 this option first or use the environment variable.
472 Setting the seed explicitly allows to reproduce a pgbench run
473 exactly, as far as random numbers are concerned. As the random
474 state is managed per thread, this means the exact same pgbench
475 run for an identical invocation if there is one client per
476 thread and there are no external or data dependencies. From a
477 statistical viewpoint reproducing runs exactly is a bad idea
478 because it can hide the performance variability or improve
479 performance unduly, e.g., by hitting the same pages as a
480 previous run. However, it may also be of great help for
481 debugging, for instance re-running a tricky case which leads to
482 an error. Use wisely.
484 --sampling-rate=rate #
485 Sampling rate, used when writing data into the log, to reduce
486 the amount of log generated. If this option is given, only the
487 specified fraction of transactions are logged. 1.0 means all
488 transactions will be logged, 0.05 means only 5% of the
489 transactions will be logged.
491 Remember to take the sampling rate into account when processing
492 the log file. For example, when computing TPS values, you need
493 to multiply the numbers accordingly (e.g., with 0.01 sample
494 rate, you'll only get 1/100 of the actual TPS).
496 --show-script=scriptname #
497 Show the actual code of builtin script scriptname on stderr, and
501 Print messages about all errors and failures (errors without
502 retrying) including which limit for retries was exceeded and how
503 far it was exceeded for the serialization/deadlock failures.
504 (Note that in this case the output can be significantly
505 increased.) See Failures and Serialization/Deadlock Retries for
510 pgbench also accepts the following common command-line arguments for
511 connection parameters and other common settings:
514 Print debugging output.
518 The database server's host name
522 The database server's port number
526 The user name to connect as
530 Print the pgbench version and exit.
534 Show help about pgbench command line arguments, and exit.
538 A successful run will exit with status 0. Exit status 1 indicates
539 static problems such as invalid command-line options or internal errors
540 which are supposed to never occur. Early errors that occur when
541 starting benchmark such as initial connection failures also exit with
542 status 1. Errors during the run such as database errors or problems in
543 the script will result in exit status 2. In the latter case, pgbench
544 will print partial results if --exit-on-abort option is not specified.
552 Default connection parameters.
554 This utility, like most other PostgreSQL utilities, uses the
555 environment variables supported by libpq (see Section 32.15).
557 The environment variable PG_COLOR specifies whether to use color in
558 diagnostic messages. Possible values are always, auto and never.
562 What Is the “Transaction” Actually Performed in pgbench?
564 pgbench executes test scripts chosen randomly from a specified list.
565 The scripts may include built-in scripts specified with -b and
566 user-provided scripts specified with -f. Each script may be given a
567 relative weight specified after an @ so as to change its selection
568 probability. The default weight is 1. Scripts with a weight of 0 are
571 The default built-in transaction script (also invoked with -b
572 tpcb-like) issues seven commands per transaction over randomly chosen
573 aid, tid, bid and delta. The scenario is inspired by the TPC-B
574 benchmark, but is not actually TPC-B, hence the name.
576 2. UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid
578 3. SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
579 4. UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid =
581 5. UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid
583 6. INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES
584 (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
587 If you select the simple-update built-in (also -N), steps 4 and 5
588 aren't included in the transaction. This will avoid update contention
589 on these tables, but it makes the test case even less like TPC-B.
591 If you select the select-only built-in (also -S), only the SELECT is
596 pgbench has support for running custom benchmark scenarios by replacing
597 the default transaction script (described above) with a transaction
598 script read from a file (-f option). In this case a “transaction”
599 counts as one execution of a script file.
601 A script file contains one or more SQL commands terminated by
602 semicolons. Empty lines and lines beginning with -- are ignored. Script
603 files can also contain “meta commands”, which are interpreted by
604 pgbench itself, as described below.
608 Before PostgreSQL 9.6, SQL commands in script files were terminated by
609 newlines, and so they could not be continued across lines. Now a
610 semicolon is required to separate consecutive SQL commands (though an
611 SQL command does not need one if it is followed by a meta command). If
612 you need to create a script file that works with both old and new
613 versions of pgbench, be sure to write each SQL command on a single line
614 ending with a semicolon.
616 It is assumed that pgbench scripts do not contain incomplete blocks of
617 SQL transactions. If at runtime the client reaches the end of the
618 script without completing the last transaction block, it will be
621 There is a simple variable-substitution facility for script files.
622 Variable names must consist of letters (including non-Latin letters),
623 digits, and underscores, with the first character not being a digit.
624 Variables can be set by the command-line -D option, explained above, or
625 by the meta commands explained below. In addition to any variables
626 preset by -D command-line options, there are a few variables that are
627 preset automatically, listed in Table 301. A value specified for these
628 variables using -D takes precedence over the automatic presets. Once
629 set, a variable's value can be inserted into an SQL command by writing
630 :variablename. When running more than one client session, each session
631 has its own set of variables. pgbench supports up to 255 variable uses
634 Table 301. pgbench Automatic Variables
636 client_id unique number identifying the client session (starts from
638 default_seed seed used in hash and pseudorandom permutation functions
640 random_seed random generator seed (unless overwritten with -D)
641 scale current scale factor
643 Script file meta commands begin with a backslash (\) and normally
644 extend to the end of the line, although they can be continued to
645 additional lines by writing backslash-return. Arguments to a meta
646 command are separated by white space. These meta commands are
649 \gset [prefix] \aset [prefix] #
650 These commands may be used to end SQL queries, taking the place
651 of the terminating semicolon (;).
653 When the \gset command is used, the preceding SQL query is
654 expected to return one row, the columns of which are stored into
655 variables named after column names, and prefixed with prefix if
658 When the \aset command is used, all combined SQL queries
659 (separated by \;) have their columns stored into variables named
660 after column names, and prefixed with prefix if provided. If a
661 query returns no row, no assignment is made and the variable can
662 be tested for existence to detect this. If a query returns more
663 than one row, the last value is kept.
665 \gset and \aset cannot be used in pipeline mode, since the query
666 results are not yet available by the time the commands would
669 The following example puts the final account balance from the
670 first query into variable abalance, and fills variables p_two
671 and p_three with integers from the third query. The result of
672 the second query is discarded. The result of the two last
673 combined queries are stored in variables four and five.
675 UPDATE pgbench_accounts
676 SET abalance = abalance + :delta
678 RETURNING abalance \gset
679 -- compound of two queries
681 SELECT 2 AS two, 3 AS three \gset p_
682 SELECT 4 AS four \; SELECT 5 AS five \aset
688 This group of commands implements nestable conditional blocks,
689 similarly to psql's \if expression. Conditional expressions are
690 identical to those with \set, with non-zero values interpreted
693 \set varname expression #
694 Sets variable varname to a value calculated from expression. The
695 expression may contain the NULL constant, Boolean constants TRUE
696 and FALSE, integer constants such as 5432, double constants such
697 as 3.14159, references to variables :variablename, operators
698 with their usual SQL precedence and associativity, function
699 calls, SQL CASE generic conditional expressions and parentheses.
701 Functions and most operators return NULL on NULL input.
703 For conditional purposes, non zero numerical values are TRUE,
704 zero numerical values and NULL are FALSE.
706 Too large or small integer and double constants, as well as
707 integer arithmetic operators (+, -, * and /) raise errors on
710 When no final ELSE clause is provided to a CASE, the default
715 \set ntellers 10 * :scale
716 \set aid (1021 * random(1, 100000 * :scale)) % \
717 (100000 * :scale) + 1
718 \set divx CASE WHEN :x <> 0 THEN :y/:x ELSE NULL END
720 \sleep number [ us | ms | s ] #
721 Causes script execution to sleep for the specified duration in
722 microseconds (us), milliseconds (ms) or seconds (s). If the unit
723 is omitted then seconds are the default. number can be either an
724 integer constant or a :variablename reference to a variable
725 having an integer value.
731 \setshell varname command [ argument ... ] #
732 Sets variable varname to the result of the shell command command
733 with the given argument(s). The command must return an integer
734 value through its standard output.
736 command and each argument can be either a text constant or a
737 :variablename reference to a variable. If you want to use an
738 argument starting with a colon, write an additional colon at the
739 beginning of argument.
743 \setshell variable_to_be_assigned command literal_argument :variable ::literal_s
746 \shell command [ argument ... ] #
747 Same as \setshell, but the result of the command is discarded.
751 \shell command literal_argument :variable ::literal_starting_with_colon
756 This group of commands implements pipelining of SQL statements.
757 A pipeline must begin with a \startpipeline and end with an
758 \endpipeline. In between there may be any number of
759 \syncpipeline commands, which sends a sync message without
760 ending the ongoing pipeline and flushing the send buffer. In
761 pipeline mode, statements are sent to the server without waiting
762 for the results of previous statements. See Section 32.5 for
763 more details. Pipeline mode requires the use of extended query
768 The arithmetic, bitwise, comparison and logical operators listed in
769 Table 302 are built into pgbench and may be used in expressions
770 appearing in \set. The operators are listed in increasing precedence
771 order. Except as noted, operators taking two numeric inputs will
772 produce a double value if either input is double, otherwise they
773 produce an integer result.
775 Table 302. pgbench Operators
783 boolean OR boolean → boolean
789 boolean AND boolean → boolean
795 NOT boolean → boolean
801 boolean IS [NOT] (NULL|TRUE|FALSE) → boolean
807 value ISNULL|NOTNULL → boolean
813 number = number → boolean
819 number <> number → boolean
825 number != number → boolean
831 number < number → boolean
837 number <= number → boolean
839 Less than or equal to
843 number > number → boolean
849 number >= number → boolean
851 Greater than or equal to
855 integer | integer → integer
861 integer # integer → integer
867 integer & integer → integer
879 integer << integer → integer
885 integer >> integer → integer
891 number + number → number
897 number - number → number
903 number * number → number
909 number / number → number
911 Division (truncates the result towards zero if both inputs are
916 integer % integer → integer
930 The functions listed in Table 303 are built into pgbench and may be
931 used in expressions appearing in \set.
933 Table 303. pgbench Functions
941 abs ( number ) → same type as input
947 debug ( number ) → same type as input
949 Prints the argument to stderr, and returns the argument.
951 debug(5432.1) → 5432.1
953 double ( number ) → double
957 double(5432) → 5432.0
959 exp ( number ) → double
961 Exponential (e raised to the given power)
963 exp(1.0) → 2.718281828459045
965 greatest ( number [, ... ] ) → double if any argument is double, else
968 Selects the largest value among the arguments.
970 greatest(5, 4, 3, 2) → 5
972 hash ( value [, seed ] ) → integer
974 This is an alias for hash_murmur2.
976 hash(10, 5432) → -5817877081768721676
978 hash_fnv1a ( value [, seed ] ) → integer
980 Computes FNV-1a hash.
982 hash_fnv1a(10, 5432) → -7793829335365542153
984 hash_murmur2 ( value [, seed ] ) → integer
986 Computes MurmurHash2 hash.
988 hash_murmur2(10, 5432) → -5817877081768721676
990 int ( number ) → integer
996 least ( number [, ... ] ) → double if any argument is double, else
999 Selects the smallest value among the arguments.
1001 least(5, 4, 3, 2.1) → 2.1
1003 ln ( number ) → double
1007 ln(2.718281828459045) → 1.0
1009 mod ( integer, integer ) → integer
1015 permute ( i, size [, seed ] ) → integer
1017 Permuted value of i, in the range [0, size). This is the new position
1018 of i (modulo size) in a pseudorandom permutation of the integers
1019 0...size-1, parameterized by seed, see below.
1021 permute(0, 4) → an integer between 0 and 3
1025 Approximate value of π
1027 pi() → 3.14159265358979323846
1029 pow ( x, y ) → double
1031 power ( x, y ) → double
1033 x raised to the power of y
1035 pow(2.0, 10) → 1024.0
1037 random ( lb, ub ) → integer
1039 Computes a uniformly-distributed random integer in [lb, ub].
1041 random(1, 10) → an integer between 1 and 10
1043 random_exponential ( lb, ub, parameter ) → integer
1045 Computes an exponentially-distributed random integer in [lb, ub], see
1048 random_exponential(1, 10, 3.0) → an integer between 1 and 10
1050 random_gaussian ( lb, ub, parameter ) → integer
1052 Computes a Gaussian-distributed random integer in [lb, ub], see below.
1054 random_gaussian(1, 10, 2.5) → an integer between 1 and 10
1056 random_zipfian ( lb, ub, parameter ) → integer
1058 Computes a Zipfian-distributed random integer in [lb, ub], see below.
1060 random_zipfian(1, 10, 1.5) → an integer between 1 and 10
1062 sqrt ( number ) → double
1066 sqrt(2.0) → 1.414213562
1068 The random function generates values using a uniform distribution, that
1069 is all the values are drawn within the specified range with equal
1070 probability. The random_exponential, random_gaussian and random_zipfian
1071 functions require an additional double parameter which determines the
1072 precise shape of the distribution.
1073 * For an exponential distribution, parameter controls the
1074 distribution by truncating a quickly-decreasing exponential
1075 distribution at parameter, and then projecting onto integers
1076 between the bounds. To be precise, with
1077 f(x) = exp(-parameter * (x - min) / (max - min + 1)) / (1 - exp(-pa
1079 Then value i between min and max inclusive is drawn with
1080 probability: f(i) - f(i + 1).
1081 Intuitively, the larger the parameter, the more frequently values
1082 close to min are accessed, and the less frequently values close to
1083 max are accessed. The closer to 0 parameter is, the flatter (more
1084 uniform) the access distribution. A crude approximation of the
1085 distribution is that the most frequent 1% values in the range,
1086 close to min, are drawn parameter% of the time. The parameter value
1087 must be strictly positive.
1088 * For a Gaussian distribution, the interval is mapped onto a standard
1089 normal distribution (the classical bell-shaped Gaussian curve)
1090 truncated at -parameter on the left and +parameter on the right.
1091 Values in the middle of the interval are more likely to be drawn.
1092 To be precise, if PHI(x) is the cumulative distribution function of
1093 the standard normal distribution, with mean mu defined as (max +
1095 f(x) = PHI(2.0 * parameter * (x - mu) / (max - min + 1)) /
1096 (2.0 * PHI(parameter) - 1)
1097 then value i between min and max inclusive is drawn with
1098 probability: f(i + 0.5) - f(i - 0.5). Intuitively, the larger the
1099 parameter, the more frequently values close to the middle of the
1100 interval are drawn, and the less frequently values close to the min
1101 and max bounds. About 67% of values are drawn from the middle 1.0 /
1102 parameter, that is a relative 0.5 / parameter around the mean, and
1103 95% in the middle 2.0 / parameter, that is a relative 1.0 /
1104 parameter around the mean; for instance, if parameter is 4.0, 67%
1105 of values are drawn from the middle quarter (1.0 / 4.0) of the
1106 interval (i.e., from 3.0 / 8.0 to 5.0 / 8.0) and 95% from the
1107 middle half (2.0 / 4.0) of the interval (second and third
1108 quartiles). The minimum allowed parameter value is 2.0.
1109 * random_zipfian generates a bounded Zipfian distribution. parameter
1110 defines how skewed the distribution is. The larger the parameter,
1111 the more frequently values closer to the beginning of the interval
1112 are drawn. The distribution is such that, assuming the range starts
1113 from 1, the ratio of the probability of drawing k versus drawing
1114 k+1 is ((k+1)/k)**parameter. For example, random_zipfian(1, ...,
1115 2.5) produces the value 1 about (2/1)**2.5 = 5.66 times more
1116 frequently than 2, which itself is produced (3/2)**2.5 = 2.76 times
1117 more frequently than 3, and so on.
1118 pgbench's implementation is based on "Non-Uniform Random Variate
1119 Generation", Luc Devroye, p. 550-551, Springer 1986. Due to
1120 limitations of that algorithm, the parameter value is restricted to
1121 the range [1.001, 1000].
1125 When designing a benchmark which selects rows non-uniformly, be aware
1126 that the rows chosen may be correlated with other data such as IDs from
1127 a sequence or the physical row ordering, which may skew performance
1130 To avoid this, you may wish to use the permute function, or some other
1131 additional step with similar effect, to shuffle the selected rows and
1132 remove such correlations.
1134 Hash functions hash, hash_murmur2 and hash_fnv1a accept an input value
1135 and an optional seed parameter. In case the seed isn't provided the
1136 value of :default_seed is used, which is initialized randomly unless
1137 set by the command-line -D option.
1139 permute accepts an input value, a size, and an optional seed parameter.
1140 It generates a pseudorandom permutation of integers in the range [0,
1141 size), and returns the index of the input value in the permuted values.
1142 The permutation chosen is parameterized by the seed, which defaults to
1143 :default_seed, if not specified. Unlike the hash functions, permute
1144 ensures that there are no collisions or holes in the output values.
1145 Input values outside the interval are interpreted modulo the size. The
1146 function raises an error if the size is not positive. permute can be
1147 used to scatter the distribution of non-uniform random functions such
1148 as random_zipfian or random_exponential so that values drawn more often
1149 are not trivially correlated. For instance, the following pgbench
1150 script simulates a possible real world workload typical for social
1151 media and blogging platforms where a few accounts generate excessive
1154 \set r random_zipfian(1, :size, 1.07)
1155 \set k 1 + permute(:r, :size)
1157 In some cases several distinct distributions are needed which don't
1158 correlate with each other and this is when the optional seed parameter
1160 \set k1 1 + permute(:r, :size, :default_seed + 123)
1161 \set k2 1 + permute(:r, :size, :default_seed + 321)
1163 A similar behavior can also be approximated with hash:
1165 \set r random_zipfian(1, 100 * :size, 1.07)
1166 \set k 1 + abs(hash(:r)) % :size
1168 However, since hash generates collisions, some values will not be
1169 reachable and others will be more frequent than expected from the
1170 original distribution.
1172 As an example, the full definition of the built-in TPC-B-like
1174 \set aid random(1, 100000 * :scale)
1175 \set bid random(1, 1 * :scale)
1176 \set tid random(1, 10 * :scale)
1177 \set delta random(-5000, 5000)
1179 UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;
1180 SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
1181 UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;
1182 UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;
1183 INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :a
1184 id, :delta, CURRENT_TIMESTAMP);
1187 This script allows each iteration of the transaction to reference
1188 different, randomly-chosen rows. (This example also shows why it's
1189 important for each client session to have its own variables — otherwise
1190 they'd not be independently touching different rows.)
1192 Per-Transaction Logging
1194 With the -l option (but without the --aggregate-interval option),
1195 pgbench writes information about each transaction to a log file. The
1196 log file will be named prefix.nnn, where prefix defaults to
1197 pgbench_log, and nnn is the PID of the pgbench process. The prefix can
1198 be changed by using the --log-prefix option. If the -j option is 2 or
1199 higher, so that there are multiple worker threads, each will have its
1200 own log file. The first worker will use the same name for its log file
1201 as in the standard single worker case. The additional log files for the
1202 other workers will be named prefix.nnn.mmm, where mmm is a sequential
1203 number for each worker starting with 1.
1205 Each line in a log file describes one transaction. It contains the
1206 following space-separated fields:
1209 identifies the client session that ran the transaction
1212 counts how many transactions have been run by that session
1215 transaction's elapsed time, in microseconds
1218 identifies the script file that was used for the transaction
1219 (useful when multiple scripts are specified with -f or -b)
1222 transaction's completion time, as a Unix-epoch time stamp
1225 fractional-second part of transaction's completion time, in
1229 transaction start delay, that is the difference between the
1230 transaction's scheduled start time and the time it actually
1231 started, in microseconds (present only if --rate is specified)
1234 count of retries after serialization or deadlock errors during
1235 the transaction (present only if --max-tries is not equal to
1238 When both --rate and --latency-limit are used, the time for a skipped
1239 transaction will be reported as skipped. If the transaction ends with a
1240 failure, its time will be reported as failed. If you use the
1241 --failures-detailed option, the time of the failed transaction will be
1242 reported as serialization or deadlock depending on the type of failure
1243 (see Failures and Serialization/Deadlock Retries for more information).
1245 Here is a snippet of a log file generated in a single-client run:
1246 0 199 2241 0 1175850568 995598
1247 0 200 2465 0 1175850568 998079
1248 0 201 2513 0 1175850569 608
1249 0 202 2038 0 1175850569 2663
1251 Another example with --rate=100 and --latency-limit=5 (note the
1252 additional schedule_lag column):
1253 0 81 4621 0 1412881037 912698 3005
1254 0 82 6173 0 1412881037 914578 4304
1255 0 83 skipped 0 1412881037 914578 5217
1256 0 83 skipped 0 1412881037 914578 5099
1257 0 83 4722 0 1412881037 916203 3108
1258 0 84 4142 0 1412881037 918023 2333
1259 0 85 2465 0 1412881037 919759 740
1261 In this example, transaction 82 was late, because its latency (6.173
1262 ms) was over the 5 ms limit. The next two transactions were skipped,
1263 because they were already late before they were even started.
1265 The following example shows a snippet of a log file with failures and
1266 retries, with the maximum number of tries set to 10 (note the
1267 additional retries column):
1268 3 0 47423 0 1499414498 34501 3
1269 3 1 8333 0 1499414498 42848 0
1270 3 2 8358 0 1499414498 51219 0
1271 4 0 72345 0 1499414498 59433 6
1272 1 3 41718 0 1499414498 67879 4
1273 1 4 8416 0 1499414498 76311 0
1274 3 3 33235 0 1499414498 84469 3
1275 0 0 failed 0 1499414498 84905 9
1276 2 0 failed 0 1499414498 86248 9
1277 3 4 8307 0 1499414498 92788 0
1279 If the --failures-detailed option is used, the type of failure is
1280 reported in the time like this:
1281 3 0 47423 0 1499414498 34501 3
1282 3 1 8333 0 1499414498 42848 0
1283 3 2 8358 0 1499414498 51219 0
1284 4 0 72345 0 1499414498 59433 6
1285 1 3 41718 0 1499414498 67879 4
1286 1 4 8416 0 1499414498 76311 0
1287 3 3 33235 0 1499414498 84469 3
1288 0 0 serialization 0 1499414498 84905 9
1289 2 0 serialization 0 1499414498 86248 9
1290 3 4 8307 0 1499414498 92788 0
1292 When running a long test on hardware that can handle a lot of
1293 transactions, the log files can become very large. The --sampling-rate
1294 option can be used to log only a random sample of transactions.
1298 With the --aggregate-interval option, a different format is used for
1299 the log files. Each log line describes one aggregation interval. It
1300 contains the following space-separated fields:
1303 start time of the interval, as a Unix-epoch time stamp
1306 number of transactions within the interval
1309 sum of transaction latencies
1312 sum of squares of transaction latencies
1315 minimum transaction latency
1318 maximum transaction latency
1321 sum of transaction start delays (zero unless --rate is
1325 sum of squares of transaction start delays (zero unless --rate
1329 minimum transaction start delay (zero unless --rate is
1333 maximum transaction start delay (zero unless --rate is
1337 number of transactions skipped because they would have started
1338 too late (zero unless --rate and --latency-limit are specified)
1341 number of retried transactions (zero unless --max-tries is not
1345 number of retries after serialization or deadlock errors (zero
1346 unless --max-tries is not equal to one)
1348 serialization_failures
1349 number of transactions that got a serialization error and were
1350 not retried afterwards (zero unless --failures-detailed is
1354 number of transactions that got a deadlock error and were not
1355 retried afterwards (zero unless --failures-detailed is
1358 Here is some example output generated with this option:
1359 pgbench --aggregate-interval=10 --time=20 --client=10 --log --rate=1000 --latenc
1360 y-limit=10 --failures-detailed --max-tries=10 test
1362 1650260552 5178 26171317 177284491527 1136 44462 2647617 7321113867 0 9866 64 75
1364 1650260562 4808 25573984 220121792172 1171 62083 3037380 9666800914 0 9998 598 7
1367 Notice that while the plain (unaggregated) log format shows which
1368 script was used for each transaction, the aggregated format does not.
1369 Therefore if you need per-script data, you need to aggregate the data
1372 Per-Statement Report
1374 With the -r option, pgbench collects the following statistics for each
1376 * latency — elapsed transaction time for each statement. pgbench
1377 reports an average value of all successful runs of the statement.
1378 * The number of failures in this statement. See Failures and
1379 Serialization/Deadlock Retries for more information.
1380 * The number of retries after a serialization or a deadlock error in
1381 this statement. See Failures and Serialization/Deadlock Retries for
1384 The report displays retry statistics only if the --max-tries option is
1387 All values are computed for each statement executed by every client and
1388 are reported after the benchmark has finished.
1390 For the default script, the output will look similar to this:
1391 starting vacuum...end.
1392 transaction type: <builtin: TPC-B (sort of)>
1395 number of clients: 10
1396 number of threads: 1
1397 maximum number of tries: 1
1398 number of transactions per client: 1000
1399 number of transactions actually processed: 10000/10000
1400 number of failed transactions: 0 (0.000%)
1401 number of transactions above the 50.0 ms latency limit: 1311/10000 (13.110 %)
1402 latency average = 28.488 ms
1403 latency stddev = 21.009 ms
1404 initial connection time = 69.068 ms
1405 tps = 346.224794 (without initial connection time)
1406 statement latencies in milliseconds and failures:
1407 0.012 0 \set aid random(1, 100000 * :scale)
1408 0.002 0 \set bid random(1, 1 * :scale)
1409 0.002 0 \set tid random(1, 10 * :scale)
1410 0.002 0 \set delta random(-5000, 5000)
1412 0.834 0 UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid
1414 0.641 0 SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
1415 11.126 0 UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid =
1417 12.961 0 UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid
1419 0.634 0 INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:
1420 tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
1423 Another example of output for the default script using serializable
1424 default transaction isolation level (PGOPTIONS='-c
1425 default_transaction_isolation=serializable' pgbench ...):
1426 starting vacuum...end.
1427 transaction type: <builtin: TPC-B (sort of)>
1430 number of clients: 10
1431 number of threads: 1
1432 maximum number of tries: 10
1433 number of transactions per client: 1000
1434 number of transactions actually processed: 6317/10000
1435 number of failed transactions: 3683 (36.830%)
1436 number of transactions retried: 7667 (76.670%)
1437 total number of retries: 45339
1438 number of transactions above the 50.0 ms latency limit: 106/6317 (1.678 %)
1439 latency average = 17.016 ms
1440 latency stddev = 13.283 ms
1441 initial connection time = 45.017 ms
1442 tps = 186.792667 (without initial connection time)
1443 statement latencies in milliseconds, failures and retries:
1444 0.006 0 0 \set aid random(1, 100000 * :scale)
1445 0.001 0 0 \set bid random(1, 1 * :scale)
1446 0.001 0 0 \set tid random(1, 10 * :scale)
1447 0.001 0 0 \set delta random(-5000, 5000)
1449 0.773 0 1 UPDATE pgbench_accounts SET abalance = abalance + :delta W
1451 0.624 0 0 SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
1452 1.098 320 3762 UPDATE pgbench_tellers SET tbalance = tbalance + :delta WH
1454 0.582 3363 41576 UPDATE pgbench_branches SET bbalance = bbalance + :delta W
1456 0.465 0 0 INSERT INTO pgbench_history (tid, bid, aid, delta, mtime)
1457 VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
1460 If multiple script files are specified, all statistics are reported
1461 separately for each script file.
1463 Note that collecting the additional timing information needed for
1464 per-statement latency computation adds some overhead. This will slow
1465 average execution speed and lower the computed TPS. The amount of
1466 slowdown varies significantly depending on platform and hardware.
1467 Comparing average TPS values with and without latency reporting enabled
1468 is a good way to measure if the timing overhead is significant.
1470 Failures and Serialization/Deadlock Retries
1472 When executing pgbench, there are three main types of errors:
1473 * Errors of the main program. They are the most serious and always
1474 result in an immediate exit from pgbench with the corresponding
1475 error message. They include:
1476 + errors at the beginning of pgbench (e.g. an invalid option
1478 + errors in the initialization mode (e.g. the query to create
1479 tables for built-in scripts fails);
1480 + errors before starting threads (e.g. could not connect to the
1481 database server, syntax error in the meta command, thread
1483 + internal pgbench errors (which are supposed to never
1485 * Errors when the thread manages its clients (e.g. the client could
1486 not start a connection to the database server / the socket for
1487 connecting the client to the database server has become invalid).
1488 In such cases all clients of this thread stop while other threads
1489 continue to work. However, --exit-on-abort is specified, all of the
1490 threads stop immediately in this case.
1491 * Direct client errors. They lead to immediate exit from pgbench with
1492 the corresponding error message in the case of an internal pgbench
1493 error (which are supposed to never occur...) or when
1494 --exit-on-abort is specified. Otherwise in the worst case they only
1495 lead to the abortion of the failed client while other clients
1496 continue their run (but some client errors are handled without an
1497 abortion of the client and reported separately, see below). Later
1498 in this section it is assumed that the discussed errors are only
1499 the direct client errors and they are not internal pgbench errors.
1501 A client's run is aborted in case of a serious error; for example, the
1502 connection with the database server was lost or the end of script was
1503 reached without completing the last transaction. In addition, if
1504 execution of an SQL or meta command fails for reasons other than
1505 serialization or deadlock errors, the client is aborted. Otherwise, if
1506 an SQL command fails with serialization or deadlock errors, the client
1507 is not aborted. In such cases, the current transaction is rolled back,
1508 which also includes setting the client variables as they were before
1509 the run of this transaction (it is assumed that one transaction script
1510 contains only one transaction; see What Is the "Transaction" Actually
1511 Performed in pgbench? for more information). Transactions with
1512 serialization or deadlock errors are repeated after rollbacks until
1513 they complete successfully or reach the maximum number of tries
1514 (specified by the --max-tries option) / the maximum time of retries
1515 (specified by the --latency-limit option) / the end of benchmark
1516 (specified by the --time option). If the last trial run fails, this
1517 transaction will be reported as failed but the client is not aborted
1518 and continues to work.
1522 Without specifying the --max-tries option, a transaction will never be
1523 retried after a serialization or deadlock error because its default
1524 value is 1. Use an unlimited number of tries (--max-tries=0) and the
1525 --latency-limit option to limit only the maximum time of tries. You can
1526 also use the --time option to limit the benchmark duration under an
1527 unlimited number of tries.
1529 Be careful when repeating scripts that contain multiple transactions:
1530 the script is always retried completely, so successful transactions can
1531 be performed several times.
1533 Be careful when repeating transactions with shell commands. Unlike the
1534 results of SQL commands, the results of shell commands are not rolled
1535 back, except for the variable value of the \setshell command.
1537 The latency of a successful transaction includes the entire time of
1538 transaction execution with rollbacks and retries. The latency is
1539 measured only for successful transactions and commands but not for
1540 failed transactions or commands.
1542 The main report contains the number of failed transactions. If the
1543 --max-tries option is not equal to 1, the main report also contains
1544 statistics related to retries: the total number of retried transactions
1545 and total number of retries. The per-script report inherits all these
1546 fields from the main report. The per-statement report displays retry
1547 statistics only if the --max-tries option is not equal to 1.
1549 If you want to group failures by basic types in per-transaction and
1550 aggregation logs, as well as in the main and per-script reports, use
1551 the --failures-detailed option. If you also want to distinguish all
1552 errors and failures (errors without retrying) by type including which
1553 limit for retries was exceeded and how much it was exceeded by for the
1554 serialization/deadlock failures, use the --verbose-errors option.
1556 Table Access Methods
1558 You may specify the Table Access Method for the pgbench tables. The
1559 environment variable PGOPTIONS specifies database configuration options
1560 that are passed to PostgreSQL via the command line (See
1561 Section 19.1.4). For example, a hypothetical default Table Access
1562 Method for the tables that pgbench creates called wuzza can be
1564 PGOPTIONS='-c default_table_access_method=wuzza'
1568 It is very easy to use pgbench to produce completely meaningless
1569 numbers. Here are some guidelines to help you get useful results.
1571 In the first place, never believe any test that runs for only a few
1572 seconds. Use the -t or -T option to make the run last at least a few
1573 minutes, so as to average out noise. In some cases you could need hours
1574 to get numbers that are reproducible. It's a good idea to try the test
1575 run a few times, to find out if your numbers are reproducible or not.
1577 For the default TPC-B-like test scenario, the initialization scale
1578 factor (-s) should be at least as large as the largest number of
1579 clients you intend to test (-c); else you'll mostly be measuring update
1580 contention. There are only -s rows in the pgbench_branches table, and
1581 every transaction wants to update one of them, so -c values in excess
1582 of -s will undoubtedly result in lots of transactions blocked waiting
1583 for other transactions.
1585 The default test scenario is also quite sensitive to how long it's been
1586 since the tables were initialized: accumulation of dead rows and dead
1587 space in the tables changes the results. To understand the results you
1588 must keep track of the total number of updates and when vacuuming
1589 happens. If autovacuum is enabled it can result in unpredictable
1590 changes in measured performance.
1592 A limitation of pgbench is that it can itself become the bottleneck
1593 when trying to test a large number of client sessions. This can be
1594 alleviated by running pgbench on a different machine from the database
1595 server, although low network latency will be essential. It might even
1596 be useful to run several pgbench instances concurrently, on several
1597 client machines, against the same database server.
1601 If untrusted users have access to a database that has not adopted a
1602 secure schema usage pattern, do not run pgbench in that database.
1603 pgbench uses unqualified names and does not manipulate the search path.