1PGBENCH(1) PostgreSQL 9.2.24 Documentation PGBENCH(1)
2
3
4
6 pgbench - run a benchmark test on PostgreSQL
7
9 pgbench -i [option...] [dbname]
10
11 pgbench [option...] [dbname]
12
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.
21
22 Typical output from pgbench looks like:
23
24 transaction type: TPC-B (sort of)
25 scaling factor: 10
26 query mode: simple
27 number of clients: 10
28 number of threads: 1
29 number of transactions per client: 1000
30 number of transactions actually processed: 10000/10000
31 tps = 85.184871 (including connections establishing)
32 tps = 85.296346 (excluding connections establishing)
33
34 The first six lines report some of the most important parameter
35 settings. The next line reports the number of transactions completed
36 and intended (the latter being just the product of number of clients
37 and number of transactions per client); these will be equal unless the
38 run failed before completion. (In -T mode, only the actual number of
39 transactions is printed.) The last two lines report the number of
40 transactions per second, figured with and without counting the time to
41 start database sessions.
42
43 The default TPC-B-like transaction test requires specific tables to be
44 set up beforehand. pgbench should be invoked with the -i (initialize)
45 option to create and populate these tables. (When you are testing a
46 custom script, you don't need this step, but will instead need to do
47 whatever setup your test needs.) Initialization looks like:
48
49 pgbench -i [ other-options ] dbname
50
51 where dbname is the name of the already-created database to test in.
52 (You may also need -h, -p, and/or -U options to specify how to connect
53 to the database server.)
54
55 Caution
56 pgbench -i creates four tables pgbench_accounts, pgbench_branches,
57 pgbench_history, and pgbench_tellers, destroying any existing
58 tables of these names. Be very careful to use another database if
59 you have tables having these names!
60
61 At the default “scale factor” of 1, the tables initially contain this
62 many rows:
63
64 table # of rows
65 ---------------------------------
66 pgbench_branches 1
67 pgbench_tellers 10
68 pgbench_accounts 100000
69 pgbench_history 0
70
71 You can (and, for most purposes, probably should) increase the number
72 of rows by using the -s (scale factor) option. The -F (fillfactor)
73 option might also be used at this point.
74
75 Once you have done the necessary setup, you can run your benchmark with
76 a command that doesn't include -i, that is
77
78 pgbench [ options ] dbname
79
80 In nearly all cases, you'll need some options to make a useful test.
81 The most important options are -c (number of clients), -t (number of
82 transactions), -T (time limit), and -f (specify a custom script file).
83 See below for a full list.
84
86 The following is divided into three subsections: Different options are
87 used during database initialization and while running benchmarks, some
88 options are useful in both cases.
89
90 Initialization Options
91 pgbench accepts the following command-line initialization arguments:
92
93 -i
94 Required to invoke initialization mode.
95
96 -F fillfactor
97 Create the pgbench_accounts, pgbench_tellers and pgbench_branches
98 tables with the given fillfactor. Default is 100.
99
100 -s scale_factor
101 Multiply the number of rows generated by the scale factor. For
102 example, -s 100 will create 10,000,000 rows in the pgbench_accounts
103 table. Default is 1.
104
105 --index-tablespace=index_tablespace
106 Create indexes in the specified tablespace, rather than the default
107 tablespace.
108
109 --tablespace=tablespace
110 Create tables in the specified tablespace, rather than the default
111 tablespace.
112
113 --unlogged-tables
114 Create all tables as unlogged tables, rather than permanent tables.
115
116 Benchmarking Options
117 pgbench accepts the following command-line benchmarking arguments:
118
119 -c clients
120 Number of clients simulated, that is, number of concurrent database
121 sessions. Default is 1.
122
123 -C
124 Establish a new connection for each transaction, rather than doing
125 it just once per client session. This is useful to measure the
126 connection overhead.
127
128 -d
129 Print debugging output.
130
131 -D varname=value
132 Define a variable for use by a custom script (see below). Multiple
133 -D options are allowed.
134
135 -f filename
136 Read transaction script from filename. See below for details. -N,
137 -S, and -f are mutually exclusive.
138
139 -j threads
140 Number of worker threads within pgbench. Using more than one thread
141 can be helpful on multi-CPU machines. The number of clients must be
142 a multiple of the number of threads, since each thread is given the
143 same number of client sessions to manage. Default is 1.
144
145 -l
146 Write the time taken by each transaction to a log file. See below
147 for details.
148
149 -M querymode
150 Protocol to use for submitting queries to the server:
151
152 · simple: use simple query protocol.
153
154 · extended: use extended query protocol.
155
156 · prepared: use extended query protocol with prepared statements.
157
158 The default is simple query protocol. (See Chapter 46,
159 Frontend/Backend Protocol, in the documentation for more
160 information.)
161
162 -n
163 Perform no vacuuming before running the test. This option is
164 necessary if you are running a custom test scenario that does not
165 include the standard tables pgbench_accounts, pgbench_branches,
166 pgbench_history, and pgbench_tellers.
167
168 -N
169 Do not update pgbench_tellers and pgbench_branches. This will avoid
170 update contention on these tables, but it makes the test case even
171 less like TPC-B.
172
173 -r
174 Report the average per-statement latency (execution time from the
175 perspective of the client) of each command after the benchmark
176 finishes. See below for details.
177
178 -s scale_factor
179 Report the specified scale factor in pgbench's output. With the
180 built-in tests, this is not necessary; the correct scale factor
181 will be detected by counting the number of rows in the
182 pgbench_branches table. However, when testing custom benchmarks (-f
183 option), the scale factor will be reported as 1 unless this option
184 is used.
185
186 -S
187 Perform select-only transactions instead of TPC-B-like test.
188
189 -t transactions
190 Number of transactions each client runs. Default is 10.
191
192 -T seconds
193 Run the test for this many seconds, rather than a fixed number of
194 transactions per client. -t and -T are mutually exclusive.
195
196 -v
197 Vacuum all four standard tables before running the test. With
198 neither -n nor -v, pgbench will vacuum the pgbench_tellers and
199 pgbench_branches tables, and will truncate pgbench_history.
200
201 Common Options
202 pgbench accepts the following command-line common arguments:
203
204 -h hostname
205 The database server's host name
206
207 -p port
208 The database server's port number
209
210 -U login
211 The user name to connect as
212
213 -V, --version
214 Print the pgbench version and exit.
215
216 -?, --help
217 Show help about pgbench command line arguments, and exit.
218
220 What is the “Transaction” Actually Performed in pgbench?
221 The default transaction script issues seven commands per transaction:
222
223 1. BEGIN;
224
225 2. UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid
226 = :aid;
227
228 3. SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
229
230 4. UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid =
231 :tid;
232
233 5. UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid
234 = :bid;
235
236 6. INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES
237 (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
238
239 7. END;
240
241 If you specify -N, steps 4 and 5 aren't included in the transaction. If
242 you specify -S, only the SELECT is issued.
243
244 Custom Scripts
245 pgbench has support for running custom benchmark scenarios by replacing
246 the default transaction script (described above) with a transaction
247 script read from a file (-f option). In this case a “transaction”
248 counts as one execution of a script file. You can even specify multiple
249 scripts (multiple -f options), in which case a random one of the
250 scripts is chosen each time a client session starts a new transaction.
251
252 The format of a script file is one SQL command per line; multiline SQL
253 commands are not supported. Empty lines and lines beginning with -- are
254 ignored. Script file lines can also be “meta commands”, which are
255 interpreted by pgbench itself, as described below.
256
257 There is a simple variable-substitution facility for script files.
258 Variables can be set by the command-line -D option, explained above, or
259 by the meta commands explained below. In addition to any variables
260 preset by -D command-line options, the variable scale is preset to the
261 current scale factor. Once set, a variable's value can be inserted into
262 a SQL command by writing :variablename. When running more than one
263 client session, each session has its own set of variables.
264
265 Script file meta commands begin with a backslash (\). Arguments to a
266 meta command are separated by white space. These meta commands are
267 supported:
268
269 \set varname operand1 [ operator operand2 ]
270 Sets variable varname to a calculated integer value. Each operand
271 is either an integer constant or a :variablename reference to a
272 variable having an integer value. The operator can be +, -, *, or
273 /.
274
275 Example:
276
277 \set ntellers 10 * :scale
278
279 \setrandom varname min max
280 Sets variable varname to a random integer value between the limits
281 min and max inclusive. Each limit can be either an integer constant
282 or a :variablename reference to a variable having an integer value.
283
284 Example:
285
286 \setrandom aid 1 :naccounts
287
288 \sleep number [ us | ms | s ]
289 Causes script execution to sleep for the specified duration in
290 microseconds (us), milliseconds (ms) or seconds (s). If the unit is
291 omitted then seconds are the default. number can be either an
292 integer constant or a :variablename reference to a variable having
293 an integer value.
294
295 Example:
296
297 \sleep 10 ms
298
299 \setshell varname command [ argument ... ]
300 Sets variable varname to the result of the shell command command.
301 The command must return an integer value through its standard
302 output.
303
304 argument can be either a text constant or a :variablename reference
305 to a variable of any types. If you want to use argument starting
306 with colons, you need to add an additional colon at the beginning
307 of argument.
308
309 Example:
310
311 \setshell variable_to_be_assigned command literal_argument :variable ::literal_starting_with_colon
312
313 \shell command [ argument ... ]
314 Same as \setshell, but the result is ignored.
315
316 Example:
317
318 \shell command literal_argument :variable ::literal_starting_with_colon
319
320 As an example, the full definition of the built-in TPC-B-like
321 transaction is:
322
323 \set nbranches :scale
324 \set ntellers 10 * :scale
325 \set naccounts 100000 * :scale
326 \setrandom aid 1 :naccounts
327 \setrandom bid 1 :nbranches
328 \setrandom tid 1 :ntellers
329 \setrandom delta -5000 5000
330 BEGIN;
331 UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;
332 SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
333 UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;
334 UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;
335 INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
336 END;
337
338 This script allows each iteration of the transaction to reference
339 different, randomly-chosen rows. (This example also shows why it's
340 important for each client session to have its own variables — otherwise
341 they'd not be independently touching different rows.)
342
343 Per-Transaction Logging
344 With the -l option, pgbench writes the time taken by each transaction
345 to a log file. The log file will be named pgbench_log.nnn, where nnn is
346 the PID of the pgbench process. If the -j option is 2 or higher,
347 creating multiple worker threads, each will have its own log file. The
348 first worker will use the same name for its log file as in the standard
349 single worker case. The additional log files for the other workers will
350 be named pgbench_log.nnn.mmm, where mmm is a sequential number for each
351 worker starting with 1.
352
353 The format of the log is:
354
355 client_id transaction_no time file_no time_epoch time_us
356
357 where time is the total elapsed transaction time in microseconds,
358 file_no identifies which script file was used (useful when multiple
359 scripts were specified with -f), and time_epoch/time_us are a UNIX
360 epoch format timestamp and an offset in microseconds (suitable for
361 creating an ISO 8601 timestamp with fractional seconds) showing when
362 the transaction completed.
363
364 Here are example outputs:
365
366 0 199 2241 0 1175850568 995598
367 0 200 2465 0 1175850568 998079
368 0 201 2513 0 1175850569 608
369 0 202 2038 0 1175850569 2663
370
371 Per-Statement Latencies
372 With the -r option, pgbench collects the elapsed transaction time of
373 each statement executed by every client. It then reports an average of
374 those values, referred to as the latency for each statement, after the
375 benchmark has finished.
376
377 For the default script, the output will look similar to this:
378
379 starting vacuum...end.
380 transaction type: TPC-B (sort of)
381 scaling factor: 1
382 query mode: simple
383 number of clients: 10
384 number of threads: 1
385 number of transactions per client: 1000
386 number of transactions actually processed: 10000/10000
387 tps = 618.764555 (including connections establishing)
388 tps = 622.977698 (excluding connections establishing)
389 statement latencies in milliseconds:
390 0.004386 \set nbranches 1 * :scale
391 0.001343 \set ntellers 10 * :scale
392 0.001212 \set naccounts 100000 * :scale
393 0.001310 \setrandom aid 1 :naccounts
394 0.001073 \setrandom bid 1 :nbranches
395 0.001005 \setrandom tid 1 :ntellers
396 0.001078 \setrandom delta -5000 5000
397 0.326152 BEGIN;
398 0.603376 UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;
399 0.454643 SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
400 5.528491 UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;
401 7.335435 UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;
402 0.371851 INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
403 1.212976 END;
404
405 If multiple script files are specified, the averages are reported
406 separately for each script file.
407
408 Note that collecting the additional timing information needed for
409 per-statement latency computation adds some overhead. This will slow
410 average execution speed and lower the computed TPS. The amount of
411 slowdown varies significantly depending on platform and hardware.
412 Comparing average TPS values with and without latency reporting enabled
413 is a good way to measure if the timing overhead is significant.
414
415 Good Practices
416 It is very easy to use pgbench to produce completely meaningless
417 numbers. Here are some guidelines to help you get useful results.
418
419 In the first place, never believe any test that runs for only a few
420 seconds. Use the -t or -T option to make the run last at least a few
421 minutes, so as to average out noise. In some cases you could need hours
422 to get numbers that are reproducible. It's a good idea to try the test
423 run a few times, to find out if your numbers are reproducible or not.
424
425 For the default TPC-B-like test scenario, the initialization scale
426 factor (-s) should be at least as large as the largest number of
427 clients you intend to test (-c); else you'll mostly be measuring update
428 contention. There are only -s rows in the pgbench_branches table, and
429 every transaction wants to update one of them, so -c values in excess
430 of -s will undoubtedly result in lots of transactions blocked waiting
431 for other transactions.
432
433 The default test scenario is also quite sensitive to how long it's been
434 since the tables were initialized: accumulation of dead rows and dead
435 space in the tables changes the results. To understand the results you
436 must keep track of the total number of updates and when vacuuming
437 happens. If autovacuum is enabled it can result in unpredictable
438 changes in measured performance.
439
440 A limitation of pgbench is that it can itself become the bottleneck
441 when trying to test a large number of client sessions. This can be
442 alleviated by running pgbench on a different machine from the database
443 server, although low network latency will be essential. It might even
444 be useful to run several pgbench instances concurrently, on several
445 client machines, against the same database server.
446
447
448
449PostgreSQL 9.2.24 2017-11-06 PGBENCH(1)