]> begriffs open source - ai-pg/blob - full-docs/man1/pgbench.1
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / man1 / pgbench.1
1 '\" t
2 .\"     Title: pgbench
3 .\"    Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
5 .\"      Date: 2025
6 .\"    Manual: PostgreSQL 18.0 Documentation
7 .\"    Source: PostgreSQL 18.0
8 .\"  Language: English
9 .\"
10 .TH "PGBENCH" "1" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 pgbench \- run a benchmark test on PostgreSQL
32 .SH "SYNOPSIS"
33 .HP \w'\fBpgbench\fR\ 'u
34 \fBpgbench\fR \fB\-i\fR [\fIoption\fR...] [\fIdbname\fR]
35 .HP \w'\fBpgbench\fR\ 'u
36 \fBpgbench\fR [\fIoption\fR...] [\fIdbname\fR]
37 .SH "DESCRIPTION"
38 .PP
39 pgbench
40 is a simple program for running benchmark tests on
41 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,
42 pgbench
43 tests a scenario that is loosely based on TPC\-B, involving five
44 \fBSELECT\fR,
45 \fBUPDATE\fR, and
46 \fBINSERT\fR
47 commands per transaction\&. However, it is easy to test other cases by writing your own transaction script files\&.
48 .PP
49 Typical output from
50 pgbench
51 looks like:
52 .sp
53 .if n \{\
54 .RS 4
55 .\}
56 .nf
57 transaction type: <builtin: TPC\-B (sort of)>
58 scaling factor: 10
59 query mode: simple
60 number of clients: 10
61 number of threads: 1
62 maximum number of tries: 1
63 number of transactions per client: 1000
64 number of transactions actually processed: 10000/10000
65 number of failed transactions: 0 (0\&.000%)
66 latency average = 11\&.013 ms
67 latency stddev = 7\&.351 ms
68 initial connection time = 45\&.758 ms
69 tps = 896\&.967014 (without initial connection time)
70 .fi
71 .if n \{\
72 .RE
73 .\}
74 .sp
75 The first seven lines report some of the most important parameter settings\&. The sixth line reports the maximum number of tries for transactions with serialization or deadlock errors (see
76 Failures and Serialization/Deadlock Retries
77 for more information)\&. The eighth 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 or some SQL command(s) failed\&. (In
78 \fB\-T\fR
79 mode, only the actual number of transactions is printed\&.) The next line reports the number of failed transactions due to serialization or deadlock errors (see
80 Failures and Serialization/Deadlock Retries
81 for more information)\&. The last line reports the number of transactions per second\&.
82 .PP
83 The default TPC\-B\-like transaction test requires specific tables to be set up beforehand\&.
84 pgbench
85 should be invoked with the
86 \fB\-i\fR
87 (initialize) option to create and populate these tables\&. (When you are testing a custom script, you don\*(Aqt need this step, but will instead need to do whatever setup your test needs\&.) Initialization looks like:
88 .sp
89 .if n \{\
90 .RS 4
91 .\}
92 .nf
93 pgbench \-i [ \fIother\-options\fR ] \fIdbname\fR
94 .fi
95 .if n \{\
96 .RE
97 .\}
98 .sp
99 where
100 \fIdbname\fR
101 is the name of the already\-created database to test in\&. (You may also need
102 \fB\-h\fR,
103 \fB\-p\fR, and/or
104 \fB\-U\fR
105 options to specify how to connect to the database server\&.)
106 .if n \{\
107 .sp
108 .\}
109 .RS 4
110 .it 1 an-trap
111 .nr an-no-space-flag 1
112 .nr an-break-flag 1
113 .br
114 .ps +1
115 \fBCaution\fR
116 .ps -1
117 .br
118 .PP
119 pgbench \-i
120 creates four tables
121 pgbench_accounts,
122 pgbench_branches,
123 pgbench_history, and
124 pgbench_tellers, destroying any existing tables of these names\&. Be very careful to use another database if you have tables having these names!
125 .sp .5v
126 .RE
127 .PP
128 At the default
129 \(lqscale factor\(rq
130 of 1, the tables initially contain this many rows:
131 .sp
132 .if n \{\
133 .RS 4
134 .\}
135 .nf
136 table                   # of rows
137 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
138 pgbench_branches        1
139 pgbench_tellers         10
140 pgbench_accounts        100000
141 pgbench_history         0
142 .fi
143 .if n \{\
144 .RE
145 .\}
146 .sp
147 You can (and, for most purposes, probably should) increase the number of rows by using the
148 \fB\-s\fR
149 (scale factor) option\&. The
150 \fB\-F\fR
151 (fillfactor) option might also be used at this point\&.
152 .PP
153 Once you have done the necessary setup, you can run your benchmark with a command that doesn\*(Aqt include
154 \fB\-i\fR, that is
155 .sp
156 .if n \{\
157 .RS 4
158 .\}
159 .nf
160 pgbench [ \fIoptions\fR ] \fIdbname\fR
161 .fi
162 .if n \{\
163 .RE
164 .\}
165 .sp
166 In nearly all cases, you\*(Aqll need some options to make a useful test\&. The most important options are
167 \fB\-c\fR
168 (number of clients),
169 \fB\-t\fR
170 (number of transactions),
171 \fB\-T\fR
172 (time limit), and
173 \fB\-f\fR
174 (specify a custom script file)\&. See below for a full list\&.
175 .SH "OPTIONS"
176 .PP
177 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\&.
178 .SS "Initialization Options"
179 .PP
180 pgbench
181 accepts the following command\-line initialization arguments:
182 .PP
183 \fB[\-d]\fR\fB \fR\fB\fIdbname\fR\fR
184 .br
185 \fB[\-\-dbname=]\fR\fB\fIdbname\fR\fR
186 .RS 4
187 Specifies the name of the database to test in\&. If this is not specified, the environment variable
188 \fBPGDATABASE\fR
189 is used\&. If that is not set, the user name specified for the connection is used\&.
190 .RE
191 .PP
192 \fB\-i\fR
193 .br
194 \fB\-\-initialize\fR
195 .RS 4
196 Required to invoke initialization mode\&.
197 .RE
198 .PP
199 \fB\-I \fR\fB\fIinit_steps\fR\fR
200 .br
201 \fB\-\-init\-steps=\fR\fB\fIinit_steps\fR\fR
202 .RS 4
203 Perform just a selected set of the normal initialization steps\&.
204 \fIinit_steps\fR
205 specifies the initialization steps to be performed, using one character per step\&. Each step is invoked in the specified order\&. The default is
206 dtgvp\&. The available steps are:
207 .PP
208 d (Drop)
209 .RS 4
210 Drop any existing
211 pgbench
212 tables\&.
213 .RE
214 .PP
215 t (create Tables)
216 .RS 4
217 Create the tables used by the standard
218 pgbench
219 scenario, namely
220 pgbench_accounts,
221 pgbench_branches,
222 pgbench_history, and
223 pgbench_tellers\&.
224 .RE
225 .PP
226 g or G (Generate data, client\-side or server\-side)
227 .RS 4
228 Generate data and load it into the standard tables, replacing any data already present\&.
229 .sp
230 With
231 g
232 (client\-side data generation), data is generated in
233 \fBpgbench\fR
234 client and then sent to the server\&. This uses the client/server bandwidth extensively through a
235 \fBCOPY\fR\&.
236 \fBpgbench\fR
237 uses the
238 \fBFREEZE\fR
239 option to load data into ordinary (non\-partition) tables with version 14 or later of
240 PostgreSQL
241 to speed up subsequent
242 \fBVACUUM\fR\&. Using
243 g
244 causes logging to print one message every 100,000 rows while generating data for all tables\&.
245 .sp
246 With
247 G
248 (server\-side data generation), only small queries are sent from the
249 \fBpgbench\fR
250 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
251 G
252 causes logging not to print any progress message while generating data\&.
253 .sp
254 The default initialization behavior uses client\-side data generation (equivalent to
255 g)\&.
256 .RE
257 .PP
258 v (Vacuum)
259 .RS 4
260 Invoke
261 \fBVACUUM\fR
262 on the standard tables\&.
263 .RE
264 .PP
265 p (create Primary keys)
266 .RS 4
267 Create primary key indexes on the standard tables\&.
268 .RE
269 .PP
270 f (create Foreign keys)
271 .RS 4
272 Create foreign key constraints between the standard tables\&. (Note that this step is not performed by default\&.)
273 .RE
274 .RE
275 .PP
276 \fB\-F\fR \fIfillfactor\fR
277 .br
278 \fB\-\-fillfactor=\fR\fIfillfactor\fR
279 .RS 4
280 Create the
281 pgbench_accounts,
282 pgbench_tellers
283 and
284 pgbench_branches
285 tables with the given fillfactor\&. Default is 100\&.
286 .RE
287 .PP
288 \fB\-n\fR
289 .br
290 \fB\-\-no\-vacuum\fR
291 .RS 4
292 Perform no vacuuming during initialization\&. (This option suppresses the
293 v
294 initialization step, even if it was specified in
295 \fB\-I\fR\&.)
296 .RE
297 .PP
298 \fB\-q\fR
299 .br
300 \fB\-\-quiet\fR
301 .RS 4
302 Switch 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)\&.
303 .sp
304 This setting has no effect if
305 G
306 is specified in
307 \fB\-I\fR\&.
308 .RE
309 .PP
310 \fB\-s\fR \fIscale_factor\fR
311 .br
312 \fB\-\-scale=\fR\fIscale_factor\fR
313 .RS 4
314 Multiply the number of rows generated by the scale factor\&. For example,
315 \-s 100
316 will create 10,000,000 rows in the
317 pgbench_accounts
318 table\&. Default is 1\&. When the scale is 20,000 or larger, the columns used to hold account identifiers (aid
319 columns) will switch to using larger integers (bigint), in order to be big enough to hold the range of account identifiers\&.
320 .RE
321 .PP
322 \fB\-\-foreign\-keys\fR
323 .RS 4
324 Create foreign key constraints between the standard tables\&. (This option adds the
325 f
326 step to the initialization step sequence, if it is not already present\&.)
327 .RE
328 .PP
329 \fB\-\-index\-tablespace=\fR\fB\fIindex_tablespace\fR\fR
330 .RS 4
331 Create indexes in the specified tablespace, rather than the default tablespace\&.
332 .RE
333 .PP
334 \fB\-\-partition\-method=\fR\fB\fINAME\fR\fR
335 .RS 4
336 Create a partitioned
337 pgbench_accounts
338 table with
339 \fINAME\fR
340 method\&. Expected values are
341 range
342 or
343 hash\&. This option requires that
344 \fB\-\-partitions\fR
345 is set to non\-zero\&. If unspecified, default is
346 range\&.
347 .RE
348 .PP
349 \fB\-\-partitions=\fR\fB\fINUM\fR\fR
350 .RS 4
351 Create a partitioned
352 pgbench_accounts
353 table with
354 \fINUM\fR
355 partitions of nearly equal size for the scaled number of accounts\&. Default is
356 0, meaning no partitioning\&.
357 .RE
358 .PP
359 \fB\-\-tablespace=\fR\fB\fItablespace\fR\fR
360 .RS 4
361 Create tables in the specified tablespace, rather than the default tablespace\&.
362 .RE
363 .PP
364 \fB\-\-unlogged\-tables\fR
365 .RS 4
366 Create all tables as unlogged tables, rather than permanent tables\&.
367 .RE
368 .SS "Benchmarking Options"
369 .PP
370 pgbench
371 accepts the following command\-line benchmarking arguments:
372 .PP
373 \fB\-b\fR \fIscriptname[@weight]\fR
374 .br
375 \fB\-\-builtin\fR=\fIscriptname[@weight]\fR
376 .RS 4
377 Add the specified built\-in script to the list of scripts to be executed\&. Available built\-in scripts are:
378 tpcb\-like,
379 simple\-update
380 and
381 select\-only\&. Unambiguous prefixes of built\-in names are accepted\&. With the special name
382 list, show the list of built\-in scripts and exit immediately\&.
383 .sp
384 Optionally, write an integer weight after
385 @
386 to adjust the probability of selecting this script versus other ones\&. The default weight is 1\&. See below for details\&.
387 .RE
388 .PP
389 \fB\-c\fR \fIclients\fR
390 .br
391 \fB\-\-client=\fR\fIclients\fR
392 .RS 4
393 Number of clients simulated, that is, number of concurrent database sessions\&. Default is 1\&.
394 .RE
395 .PP
396 \fB\-C\fR
397 .br
398 \fB\-\-connect\fR
399 .RS 4
400 Establish a new connection for each transaction, rather than doing it just once per client session\&. This is useful to measure the connection overhead\&.
401 .RE
402 .PP
403 \fB\-D\fR \fIvarname\fR=\fIvalue\fR
404 .br
405 \fB\-\-define=\fR\fIvarname\fR=\fIvalue\fR
406 .RS 4
407 Define a variable for use by a custom script (see below)\&. Multiple
408 \fB\-D\fR
409 options are allowed\&.
410 .RE
411 .PP
412 \fB\-f\fR \fIfilename[@weight]\fR
413 .br
414 \fB\-\-file=\fR\fIfilename[@weight]\fR
415 .RS 4
416 Add a transaction script read from
417 \fIfilename\fR
418 to the list of scripts to be executed\&.
419 .sp
420 Optionally, write an integer weight after
421 @
422 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
423 @
424 character, append a weight so that there is no ambiguity, for example
425 filen@me@1\&.) See below for details\&.
426 .RE
427 .PP
428 \fB\-j\fR \fIthreads\fR
429 .br
430 \fB\-\-jobs=\fR\fIthreads\fR
431 .RS 4
432 Number of worker threads within
433 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\&.
434 .RE
435 .PP
436 \fB\-l\fR
437 .br
438 \fB\-\-log\fR
439 .RS 4
440 Write information about each transaction to a log file\&. See below for details\&.
441 .RE
442 .PP
443 \fB\-L\fR \fIlimit\fR
444 .br
445 \fB\-\-latency\-limit=\fR\fIlimit\fR
446 .RS 4
447 Transactions that last more than
448 \fIlimit\fR
449 milliseconds are counted and reported separately, as
450 late\&.
451 .sp
452 When throttling is used (\fB\-\-rate=\&.\&.\&.\fR), transactions that lag behind schedule by more than
453 \fIlimit\fR
454 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
455 skipped\&.
456 .sp
457 When the
458 \fB\-\-max\-tries\fR
459 option is used, a transaction which fails due to a serialization anomaly or from a deadlock will not be retried if the total time of all its tries is greater than
460 \fIlimit\fR
461 ms\&. To limit only the time of tries and not their number, use
462 \-\-max\-tries=0\&. By default, the option
463 \fB\-\-max\-tries\fR
464 is set to 1 and transactions with serialization/deadlock errors are not retried\&. See
465 Failures and Serialization/Deadlock Retries
466 for more information about retrying such transactions\&.
467 .RE
468 .PP
469 \fB\-M\fR \fIquerymode\fR
470 .br
471 \fB\-\-protocol=\fR\fIquerymode\fR
472 .RS 4
473 Protocol to use for submitting queries to the server:
474 .sp
475 .RS 4
476 .ie n \{\
477 \h'-04'\(bu\h'+03'\c
478 .\}
479 .el \{\
480 .sp -1
481 .IP \(bu 2.3
482 .\}
483 simple: use simple query protocol\&.
484 .RE
485 .sp
486 .RS 4
487 .ie n \{\
488 \h'-04'\(bu\h'+03'\c
489 .\}
490 .el \{\
491 .sp -1
492 .IP \(bu 2.3
493 .\}
494 extended: use extended query protocol\&.
495 .RE
496 .sp
497 .RS 4
498 .ie n \{\
499 \h'-04'\(bu\h'+03'\c
500 .\}
501 .el \{\
502 .sp -1
503 .IP \(bu 2.3
504 .\}
505 prepared: use extended query protocol with prepared statements\&.
506 .RE
507 .sp
508 In the
509 prepared
510 mode,
511 pgbench
512 reuses the parse analysis result starting from the second query iteration, so
513 pgbench
514 runs faster than in other modes\&.
515 .sp
516 The default is simple query protocol\&. (See
517 Chapter\ \&54
518 for more information\&.)
519 .RE
520 .PP
521 \fB\-n\fR
522 .br
523 \fB\-\-no\-vacuum\fR
524 .RS 4
525 Perform no vacuuming before running the test\&. This option is
526 \fInecessary\fR
527 if you are running a custom test scenario that does not include the standard tables
528 pgbench_accounts,
529 pgbench_branches,
530 pgbench_history, and
531 pgbench_tellers\&.
532 .RE
533 .PP
534 \fB\-N\fR
535 .br
536 \fB\-\-skip\-some\-updates\fR
537 .RS 4
538 Run built\-in simple\-update script\&. Shorthand for
539 \fB\-b simple\-update\fR\&.
540 .RE
541 .PP
542 \fB\-P\fR \fIsec\fR
543 .br
544 \fB\-\-progress=\fR\fIsec\fR
545 .RS 4
546 Show progress report every
547 \fIsec\fR
548 seconds\&. The report includes the time since the beginning of the run, the TPS since the last report, and the transaction latency average, standard deviation, and the number of failed transactions since the last report\&. Under throttling (\fB\-R\fR), 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\&. When
549 \fB\-\-max\-tries\fR
550 is used to enable transaction retries after serialization/deadlock errors, the report includes the number of retried transactions and the sum of all retries\&.
551 .RE
552 .PP
553 \fB\-r\fR
554 .br
555 \fB\-\-report\-per\-command\fR
556 .RS 4
557 Report the following statistics for each command after the benchmark finishes: the average per\-statement latency (execution time from the perspective of the client), the number of failures, and the number of retries after serialization or deadlock errors in this command\&. The report displays retry statistics only if the
558 \fB\-\-max\-tries\fR
559 option is not equal to 1\&.
560 .RE
561 .PP
562 \fB\-R\fR \fIrate\fR
563 .br
564 \fB\-\-rate=\fR\fIrate\fR
565 .RS 4
566 Execute 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\*(Aqt impact the results\&.
567 .sp
568 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\&.
569 .sp
570 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\&.
571 .sp
572 If
573 \fB\-\-latency\-limit\fR
574 is used together with
575 \fB\-\-rate\fR, 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\&.
576 .sp
577 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\&.
578 .RE
579 .PP
580 \fB\-s\fR \fIscale_factor\fR
581 .br
582 \fB\-\-scale=\fR\fIscale_factor\fR
583 .RS 4
584 Report the specified scale factor in
585 pgbench\*(Aqs 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
586 pgbench_branches
587 table\&. However, when testing only custom benchmarks (\fB\-f\fR
588 option), the scale factor will be reported as 1 unless this option is used\&.
589 .RE
590 .PP
591 \fB\-S\fR
592 .br
593 \fB\-\-select\-only\fR
594 .RS 4
595 Run built\-in select\-only script\&. Shorthand for
596 \fB\-b select\-only\fR\&.
597 .RE
598 .PP
599 \fB\-t\fR \fItransactions\fR
600 .br
601 \fB\-\-transactions=\fR\fItransactions\fR
602 .RS 4
603 Number of transactions each client runs\&. Default is 10\&.
604 .RE
605 .PP
606 \fB\-T\fR \fIseconds\fR
607 .br
608 \fB\-\-time=\fR\fIseconds\fR
609 .RS 4
610 Run the test for this many seconds, rather than a fixed number of transactions per client\&.
611 \fB\-t\fR
612 and
613 \fB\-T\fR
614 are mutually exclusive\&.
615 .RE
616 .PP
617 \fB\-v\fR
618 .br
619 \fB\-\-vacuum\-all\fR
620 .RS 4
621 Vacuum all four standard tables before running the test\&. With neither
622 \fB\-n\fR
623 nor
624 \fB\-v\fR,
625 pgbench
626 will vacuum the
627 pgbench_tellers
628 and
629 pgbench_branches
630 tables, and will truncate
631 pgbench_history\&.
632 .RE
633 .PP
634 \fB\-\-aggregate\-interval=\fR\fB\fIseconds\fR\fR
635 .RS 4
636 Length of aggregation interval (in seconds)\&. May be used only with
637 \fB\-l\fR
638 option\&. With this option, the log contains per\-interval summary data, as described below\&.
639 .RE
640 .PP
641 \fB\-\-exit\-on\-abort\fR
642 .RS 4
643 Exit immediately when any client is aborted due to some error\&. Without this option, even when a client is aborted, other clients could continue their run as specified by
644 \fB\-t\fR
645 or
646 \fB\-T\fR
647 option, and
648 pgbench
649 will print an incomplete results in this case\&.
650 .sp
651 Note that serialization failures or deadlock failures do not abort the client, so they are not affected by this option\&. See
652 Failures and Serialization/Deadlock Retries
653 for more information\&.
654 .RE
655 .PP
656 \fB\-\-failures\-detailed\fR
657 .RS 4
658 Report failures in per\-transaction and aggregation logs, as well as in the main and per\-script reports, grouped by the following types:
659 .sp
660 .RS 4
661 .ie n \{\
662 \h'-04'\(bu\h'+03'\c
663 .\}
664 .el \{\
665 .sp -1
666 .IP \(bu 2.3
667 .\}
668 serialization failures;
669 .RE
670 .sp
671 .RS 4
672 .ie n \{\
673 \h'-04'\(bu\h'+03'\c
674 .\}
675 .el \{\
676 .sp -1
677 .IP \(bu 2.3
678 .\}
679 deadlock failures;
680 .RE
681 .sp
682 See
683 Failures and Serialization/Deadlock Retries
684 for more information\&.
685 .RE
686 .PP
687 \fB\-\-log\-prefix=\fR\fB\fIprefix\fR\fR
688 .RS 4
689 Set the filename prefix for the log files created by
690 \fB\-\-log\fR\&. The default is
691 pgbench_log\&.
692 .RE
693 .PP
694 \fB\-\-max\-tries=\fR\fB\fInumber_of_tries\fR\fR
695 .RS 4
696 Enable retries for transactions with serialization/deadlock errors and set the maximum number of these tries\&. This option can be combined with the
697 \fB\-\-latency\-limit\fR
698 option which limits the total time of all transaction tries; moreover, you cannot use an unlimited number of tries (\-\-max\-tries=0) without
699 \fB\-\-latency\-limit\fR
700 or
701 \fB\-\-time\fR\&. The default value is 1 and transactions with serialization/deadlock errors are not retried\&. See
702 Failures and Serialization/Deadlock Retries
703 for more information about retrying such transactions\&.
704 .RE
705 .PP
706 \fB\-\-progress\-timestamp\fR
707 .RS 4
708 When showing progress (option
709 \fB\-P\fR), use a timestamp (Unix epoch) instead of the number of seconds since the beginning of the run\&. The unit is in seconds, with millisecond precision after the dot\&. This helps compare logs generated by various tools\&.
710 .RE
711 .PP
712 \fB\-\-random\-seed=\fR\fIseed\fR
713 .RS 4
714 Set random generator seed\&. Seeds the system random number generator, which then produces a sequence of initial generator states, one for each thread\&. Values for
715 \fIseed\fR
716 may be:
717 time
718 (the default, the seed is based on the current time),
719 rand
720 (use a strong random source, failing if none is available), or an unsigned decimal integer value\&. The random generator is invoked explicitly from a pgbench script (random\&.\&.\&.
721 functions) or implicitly (for instance option
722 \fB\-\-rate\fR
723 uses it to schedule transactions)\&. When explicitly set, the value used for seeding is shown on the terminal\&. Any value allowed for
724 \fIseed\fR
725 may also be provided through the environment variable
726 PGBENCH_RANDOM_SEED\&. To ensure that the provided seed impacts all possible uses, put this option first or use the environment variable\&.
727 .sp
728 Setting the seed explicitly allows to reproduce a
729 \fBpgbench\fR
730 run exactly, as far as random numbers are concerned\&. As the random state is managed per thread, this means the exact same
731 \fBpgbench\fR
732 run for an identical invocation if there is one client per thread and there are no external or data dependencies\&. From a statistical viewpoint reproducing runs exactly is a bad idea because it can hide the performance variability or improve performance unduly, e\&.g\&., by hitting the same pages as a previous run\&. However, it may also be of great help for debugging, for instance re\-running a tricky case which leads to an error\&. Use wisely\&.
733 .RE
734 .PP
735 \fB\-\-sampling\-rate=\fR\fB\fIrate\fR\fR
736 .RS 4
737 Sampling rate, used when writing data into the log, to reduce the amount of log generated\&. If this option is given, only the specified fraction of transactions are logged\&. 1\&.0 means all transactions will be logged, 0\&.05 means only 5% of the transactions will be logged\&.
738 .sp
739 Remember to take the sampling rate into account when processing the log file\&. For example, when computing TPS values, you need to multiply the numbers accordingly (e\&.g\&., with 0\&.01 sample rate, you\*(Aqll only get 1/100 of the actual TPS)\&.
740 .RE
741 .PP
742 \fB\-\-show\-script=\fR\fIscriptname\fR
743 .RS 4
744 Show the actual code of builtin script
745 \fIscriptname\fR
746 on stderr, and exit immediately\&.
747 .RE
748 .PP
749 \fB\-\-verbose\-errors\fR
750 .RS 4
751 Print messages about all errors and failures (errors without retrying) including which limit for retries was exceeded and how far it was exceeded for the serialization/deadlock failures\&. (Note that in this case the output can be significantly increased\&.) See
752 Failures and Serialization/Deadlock Retries
753 for more information\&.
754 .RE
755 .SS "Common Options"
756 .PP
757 pgbench
758 also accepts the following common command\-line arguments for connection parameters and other common settings:
759 .PP
760 \fB\-\-debug\fR
761 .RS 4
762 Print debugging output\&.
763 .RE
764 .PP
765 \fB\-h\fR \fIhostname\fR
766 .br
767 \fB\-\-host=\fR\fIhostname\fR
768 .RS 4
769 The database server\*(Aqs host name
770 .RE
771 .PP
772 \fB\-p\fR \fIport\fR
773 .br
774 \fB\-\-port=\fR\fIport\fR
775 .RS 4
776 The database server\*(Aqs port number
777 .RE
778 .PP
779 \fB\-U\fR \fIlogin\fR
780 .br
781 \fB\-\-username=\fR\fIlogin\fR
782 .RS 4
783 The user name to connect as
784 .RE
785 .PP
786 \fB\-V\fR
787 .br
788 \fB\-\-version\fR
789 .RS 4
790 Print the
791 pgbench
792 version and exit\&.
793 .RE
794 .PP
795 \fB\-?\fR
796 .br
797 \fB\-\-help\fR
798 .RS 4
799 Show help about
800 pgbench
801 command line arguments, and exit\&.
802 .RE
803 .SH "EXIT STATUS"
804 .PP
805 A successful run will exit with status 0\&. Exit status 1 indicates static problems such as invalid command\-line options or internal errors which are supposed to never occur\&. Early errors that occur when starting benchmark such as initial connection failures also exit with status 1\&. Errors during the run such as database errors or problems in the script will result in exit status 2\&. In the latter case,
806 pgbench
807 will print partial results if
808 \fB\-\-exit\-on\-abort\fR
809 option is not specified\&.
810 .SH "ENVIRONMENT"
811 .PP
812 \fBPGDATABASE\fR
813 .br
814 \fBPGHOST\fR
815 .br
816 \fBPGPORT\fR
817 .br
818 \fBPGUSER\fR
819 .RS 4
820 Default connection parameters\&.
821 .RE
822 .PP
823 This utility, like most other
824 PostgreSQL
825 utilities, uses the environment variables supported by
826 libpq
827 (see
828 Section\ \&32.15)\&.
829 .PP
830 The environment variable
831 \fBPG_COLOR\fR
832 specifies whether to use color in diagnostic messages\&. Possible values are
833 always,
834 auto
835 and
836 never\&.
837 .SH "NOTES"
838 .SS "What Is the \(lqTransaction\(rq Actually Performed in pgbench?"
839 .PP
840 pgbench
841 executes test scripts chosen randomly from a specified list\&. The scripts may include built\-in scripts specified with
842 \fB\-b\fR
843 and user\-provided scripts specified with
844 \fB\-f\fR\&. Each script may be given a relative weight specified after an
845 @
846 so as to change its selection probability\&. The default weight is
847 1\&. Scripts with a weight of
848 0
849 are ignored\&.
850 .PP
851 The default built\-in transaction script (also invoked with
852 \fB\-b tpcb\-like\fR) issues seven commands per transaction over randomly chosen
853 aid,
854 tid,
855 bid
856 and
857 delta\&. The scenario is inspired by the TPC\-B benchmark, but is not actually TPC\-B, hence the name\&.
858 .sp
859 .RS 4
860 .ie n \{\
861 \h'-04' 1.\h'+01'\c
862 .\}
863 .el \{\
864 .sp -1
865 .IP "  1." 4.2
866 .\}
867 BEGIN;
868 .RE
869 .sp
870 .RS 4
871 .ie n \{\
872 \h'-04' 2.\h'+01'\c
873 .\}
874 .el \{\
875 .sp -1
876 .IP "  2." 4.2
877 .\}
878 UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;
879 .RE
880 .sp
881 .RS 4
882 .ie n \{\
883 \h'-04' 3.\h'+01'\c
884 .\}
885 .el \{\
886 .sp -1
887 .IP "  3." 4.2
888 .\}
889 SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
890 .RE
891 .sp
892 .RS 4
893 .ie n \{\
894 \h'-04' 4.\h'+01'\c
895 .\}
896 .el \{\
897 .sp -1
898 .IP "  4." 4.2
899 .\}
900 UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;
901 .RE
902 .sp
903 .RS 4
904 .ie n \{\
905 \h'-04' 5.\h'+01'\c
906 .\}
907 .el \{\
908 .sp -1
909 .IP "  5." 4.2
910 .\}
911 UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;
912 .RE
913 .sp
914 .RS 4
915 .ie n \{\
916 \h'-04' 6.\h'+01'\c
917 .\}
918 .el \{\
919 .sp -1
920 .IP "  6." 4.2
921 .\}
922 INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
923 .RE
924 .sp
925 .RS 4
926 .ie n \{\
927 \h'-04' 7.\h'+01'\c
928 .\}
929 .el \{\
930 .sp -1
931 .IP "  7." 4.2
932 .\}
933 END;
934 .RE
935 .PP
936 If you select the
937 simple\-update
938 built\-in (also
939 \fB\-N\fR), steps 4 and 5 aren\*(Aqt included in the transaction\&. This will avoid update contention on these tables, but it makes the test case even less like TPC\-B\&.
940 .PP
941 If you select the
942 select\-only
943 built\-in (also
944 \fB\-S\fR), only the
945 \fBSELECT\fR
946 is issued\&.
947 .SS "Custom Scripts"
948 .PP
949 pgbench
950 has support for running custom benchmark scenarios by replacing the default transaction script (described above) with a transaction script read from a file (\fB\-f\fR
951 option)\&. In this case a
952 \(lqtransaction\(rq
953 counts as one execution of a script file\&.
954 .PP
955 A script file contains one or more SQL commands terminated by semicolons\&. Empty lines and lines beginning with
956 \-\-
957 are ignored\&. Script files can also contain
958 \(lqmeta commands\(rq, which are interpreted by
959 pgbench
960 itself, as described below\&.
961 .if n \{\
962 .sp
963 .\}
964 .RS 4
965 .it 1 an-trap
966 .nr an-no-space-flag 1
967 .nr an-break-flag 1
968 .br
969 .ps +1
970 \fBNote\fR
971 .ps -1
972 .br
973 .PP
974 Before
975 PostgreSQL
976 9\&.6, SQL commands in script files were terminated by newlines, and so they could not be continued across lines\&. Now a semicolon is
977 \fIrequired\fR
978 to separate consecutive SQL commands (though an SQL command does not need one if it is followed by a meta command)\&. If you need to create a script file that works with both old and new versions of
979 pgbench, be sure to write each SQL command on a single line ending with a semicolon\&.
980 .PP
981 It is assumed that
982 pgbench
983 scripts do not contain incomplete blocks of SQL transactions\&. If at runtime the client reaches the end of the script without completing the last transaction block, it will be aborted\&.
984 .sp .5v
985 .RE
986 .PP
987 There is a simple variable\-substitution facility for script files\&. Variable names must consist of letters (including non\-Latin letters), digits, and underscores, with the first character not being a digit\&. Variables can be set by the command\-line
988 \fB\-D\fR
989 option, explained above, or by the meta commands explained below\&. In addition to any variables preset by
990 \fB\-D\fR
991 command\-line options, there are a few variables that are preset automatically, listed in
992 Table\ \&301\&. A value specified for these variables using
993 \fB\-D\fR
994 takes precedence over the automatic presets\&. Once set, a variable\*(Aqs value can be inserted into an SQL command by writing
995 :\fIvariablename\fR\&. When running more than one client session, each session has its own set of variables\&.
996 pgbench
997 supports up to 255 variable uses in one statement\&.
998 .sp
999 .it 1 an-trap
1000 .nr an-no-space-flag 1
1001 .nr an-break-flag 1
1002 .br
1003 .B Table\ \&301.\ \&pgbench Automatic Variables
1004 .TS
1005 allbox tab(:);
1006 lB lB.
1007 T{
1008 Variable
1009 T}:T{
1010 Description
1011 T}
1012 .T&
1013 l l
1014 l l
1015 l l
1016 l l.
1017 T{
1018 client_id
1019 T}:T{
1020 unique number identifying the client session (starts from zero)
1021 T}
1022 T{
1023 default_seed
1024 T}:T{
1025 seed used in hash and pseudorandom permutation functions by default
1026 T}
1027 T{
1028 random_seed
1029 T}:T{
1030 random generator seed (unless overwritten with \fB\-D\fR)
1031 T}
1032 T{
1033 scale
1034 T}:T{
1035 current scale factor
1036 T}
1037 .TE
1038 .sp 1
1039 .PP
1040 Script file meta commands begin with a backslash (\e) and normally extend to the end of the line, although they can be continued to additional lines by writing backslash\-return\&. Arguments to a meta command are separated by white space\&. These meta commands are supported:
1041 .PP
1042 \egset [\fIprefix\fR] \easet [\fIprefix\fR]
1043 .RS 4
1044 These commands may be used to end SQL queries, taking the place of the terminating semicolon (;)\&.
1045 .sp
1046 When the
1047 \egset
1048 command is used, the preceding SQL query is expected to return one row, the columns of which are stored into variables named after column names, and prefixed with
1049 \fIprefix\fR
1050 if provided\&.
1051 .sp
1052 When the
1053 \easet
1054 command is used, all combined SQL queries (separated by
1055 \e;) have their columns stored into variables named after column names, and prefixed with
1056 \fIprefix\fR
1057 if provided\&. If a query returns no row, no assignment is made and the variable can be tested for existence to detect this\&. If a query returns more than one row, the last value is kept\&.
1058 .sp
1059 \egset
1060 and
1061 \easet
1062 cannot be used in pipeline mode, since the query results are not yet available by the time the commands would need them\&.
1063 .sp
1064 The following example puts the final account balance from the first query into variable
1065 \fIabalance\fR, and fills variables
1066 \fIp_two\fR
1067 and
1068 \fIp_three\fR
1069 with integers from the third query\&. The result of the second query is discarded\&. The result of the two last combined queries are stored in variables
1070 \fIfour\fR
1071 and
1072 \fIfive\fR\&.
1073 .sp
1074 .if n \{\
1075 .RS 4
1076 .\}
1077 .nf
1078 UPDATE pgbench_accounts
1079   SET abalance = abalance + :delta
1080   WHERE aid = :aid
1081   RETURNING abalance \egset
1082 \-\- compound of two queries
1083 SELECT 1 \e;
1084 SELECT 2 AS two, 3 AS three \egset p_
1085 SELECT 4 AS four \e; SELECT 5 AS five \easet
1086 .fi
1087 .if n \{\
1088 .RE
1089 .\}
1090 .RE
1091 .PP
1092 \eif \fIexpression\fR
1093 .br
1094 \eelif \fIexpression\fR
1095 .br
1096 \eelse
1097 .br
1098 \eendif
1099 .RS 4
1100 This group of commands implements nestable conditional blocks, similarly to
1101 psql\*(Aqs
1102 \eif \fIexpression\fR\&. Conditional expressions are identical to those with
1103 \eset, with non\-zero values interpreted as true\&.
1104 .RE
1105 .PP
1106 \eset \fIvarname\fR \fIexpression\fR
1107 .RS 4
1108 Sets variable
1109 \fIvarname\fR
1110 to a value calculated from
1111 \fIexpression\fR\&. The expression may contain the
1112 NULL
1113 constant, Boolean constants
1114 TRUE
1115 and
1116 FALSE, integer constants such as
1117 5432, double constants such as
1118 3\&.14159, references to variables
1119 :\fIvariablename\fR,
1120 operators
1121 with their usual SQL precedence and associativity,
1122 function calls, SQL
1123 CASE generic conditional expressions
1124 and parentheses\&.
1125 .sp
1126 Functions and most operators return
1127 NULL
1128 on
1129 NULL
1130 input\&.
1131 .sp
1132 For conditional purposes, non zero numerical values are
1133 TRUE, zero numerical values and
1134 NULL
1135 are
1136 FALSE\&.
1137 .sp
1138 Too large or small integer and double constants, as well as integer arithmetic operators (+,
1139 \-,
1140 *
1141 and
1142 /) raise errors on overflows\&.
1143 .sp
1144 When no final
1145 ELSE
1146 clause is provided to a
1147 CASE, the default value is
1148 NULL\&.
1149 .sp
1150 Examples:
1151 .sp
1152 .if n \{\
1153 .RS 4
1154 .\}
1155 .nf
1156 \eset ntellers 10 * :scale
1157 \eset aid (1021 * random(1, 100000 * :scale)) % \e
1158            (100000 * :scale) + 1
1159 \eset divx CASE WHEN :x <> 0 THEN :y/:x ELSE NULL END
1160 .fi
1161 .if n \{\
1162 .RE
1163 .\}
1164 .RE
1165 .PP
1166 \esleep \fInumber\fR [ us | ms | s ]
1167 .RS 4
1168 Causes script execution to sleep for the specified duration in microseconds (us), milliseconds (ms) or seconds (s)\&. If the unit is omitted then seconds are the default\&.
1169 \fInumber\fR
1170 can be either an integer constant or a
1171 :\fIvariablename\fR
1172 reference to a variable having an integer value\&.
1173 .sp
1174 Example:
1175 .sp
1176 .if n \{\
1177 .RS 4
1178 .\}
1179 .nf
1180 \esleep 10 ms
1181 .fi
1182 .if n \{\
1183 .RE
1184 .\}
1185 .RE
1186 .PP
1187 \esetshell \fIvarname\fR \fIcommand\fR [ \fIargument\fR \&.\&.\&. ]
1188 .RS 4
1189 Sets variable
1190 \fIvarname\fR
1191 to the result of the shell command
1192 \fIcommand\fR
1193 with the given
1194 \fIargument\fR(s)\&. The command must return an integer value through its standard output\&.
1195 .sp
1196 \fIcommand\fR
1197 and each
1198 \fIargument\fR
1199 can be either a text constant or a
1200 :\fIvariablename\fR
1201 reference to a variable\&. If you want to use an
1202 \fIargument\fR
1203 starting with a colon, write an additional colon at the beginning of
1204 \fIargument\fR\&.
1205 .sp
1206 Example:
1207 .sp
1208 .if n \{\
1209 .RS 4
1210 .\}
1211 .nf
1212 \esetshell variable_to_be_assigned command literal_argument :variable ::literal_starting_with_colon
1213 .fi
1214 .if n \{\
1215 .RE
1216 .\}
1217 .RE
1218 .PP
1219 \eshell \fIcommand\fR [ \fIargument\fR \&.\&.\&. ]
1220 .RS 4
1221 Same as
1222 \esetshell, but the result of the command is discarded\&.
1223 .sp
1224 Example:
1225 .sp
1226 .if n \{\
1227 .RS 4
1228 .\}
1229 .nf
1230 \eshell command literal_argument :variable ::literal_starting_with_colon
1231 .fi
1232 .if n \{\
1233 .RE
1234 .\}
1235 .RE
1236 .PP
1237 \estartpipeline
1238 .br
1239 \esyncpipeline
1240 .br
1241 \eendpipeline
1242 .RS 4
1243 This group of commands implements pipelining of SQL statements\&. A pipeline must begin with a
1244 \fB\estartpipeline\fR
1245 and end with an
1246 \fB\eendpipeline\fR\&. In between there may be any number of
1247 \fB\esyncpipeline\fR
1248 commands, which sends a
1249 sync message
1250 without ending the ongoing pipeline and flushing the send buffer\&. In pipeline mode, statements are sent to the server without waiting for the results of previous statements\&. See
1251 Section\ \&32.5
1252 for more details\&. Pipeline mode requires the use of extended query protocol\&.
1253 .RE
1254 .SS "Built\-in Operators"
1255 .PP
1256 The arithmetic, bitwise, comparison and logical operators listed in
1257 Table\ \&302
1258 are built into
1259 pgbench
1260 and may be used in expressions appearing in
1261 \eset\&. The operators are listed in increasing precedence order\&. Except as noted, operators taking two numeric inputs will produce a double value if either input is double, otherwise they produce an integer result\&.
1262 .sp
1263 .it 1 an-trap
1264 .nr an-no-space-flag 1
1265 .nr an-break-flag 1
1266 .br
1267 .B Table\ \&302.\ \&pgbench Operators
1268 .TS
1269 allbox tab(:);
1270 lB.
1271 T{
1272 .PP
1273 Operator
1274
1275        .PP
1276 Description
1277
1278        .PP
1279 Example(s)
1280 T}
1281 .T&
1282 l
1283 l
1284 l
1285 l
1286 l
1287 l
1288 l
1289 l
1290 l
1291 l
1292 l
1293 l
1294 l
1295 l
1296 l
1297 l
1298 l
1299 l
1300 l
1301 l
1302 l
1303 l
1304 l
1305 l.
1306 T{
1307 .PP
1308 \fIboolean\fR
1309 OR
1310 \fIboolean\fR
1311 → \fIboolean\fR
1312
1313        .PP
1314 Logical OR
1315
1316        .PP
1317 5 or 0
1318 → TRUE
1319 T}
1320 T{
1321 .PP
1322 \fIboolean\fR
1323 AND
1324 \fIboolean\fR
1325 → \fIboolean\fR
1326
1327        .PP
1328 Logical AND
1329
1330        .PP
1331 3 and 0
1332 → FALSE
1333 T}
1334 T{
1335 .PP
1336 NOT
1337 \fIboolean\fR
1338 → \fIboolean\fR
1339
1340        .PP
1341 Logical NOT
1342
1343        .PP
1344 not false
1345 → TRUE
1346 T}
1347 T{
1348 .PP
1349 \fIboolean\fR
1350 IS [NOT] (NULL|TRUE|FALSE)
1351 → \fIboolean\fR
1352
1353        .PP
1354 Boolean value tests
1355
1356        .PP
1357 1 is null
1358 → FALSE
1359 T}
1360 T{
1361 .PP
1362 \fIvalue\fR
1363 ISNULL|NOTNULL
1364 → \fIboolean\fR
1365
1366        .PP
1367 Nullness tests
1368
1369        .PP
1370 1 notnull
1371 → TRUE
1372 T}
1373 T{
1374 .PP
1375 \fInumber\fR
1376 =
1377 \fInumber\fR
1378 → \fIboolean\fR
1379
1380        .PP
1381 Equal
1382
1383        .PP
1384 5 = 4
1385 → FALSE
1386 T}
1387 T{
1388 .PP
1389 \fInumber\fR
1390 <>
1391 \fInumber\fR
1392 → \fIboolean\fR
1393
1394        .PP
1395 Not equal
1396
1397        .PP
1398 5 <> 4
1399 → TRUE
1400 T}
1401 T{
1402 .PP
1403 \fInumber\fR
1404 !=
1405 \fInumber\fR
1406 → \fIboolean\fR
1407
1408        .PP
1409 Not equal
1410
1411        .PP
1412 5 != 5
1413 → FALSE
1414 T}
1415 T{
1416 .PP
1417 \fInumber\fR
1418 <
1419 \fInumber\fR
1420 → \fIboolean\fR
1421
1422        .PP
1423 Less than
1424
1425        .PP
1426 5 < 4
1427 → FALSE
1428 T}
1429 T{
1430 .PP
1431 \fInumber\fR
1432 <=
1433 \fInumber\fR
1434 → \fIboolean\fR
1435
1436        .PP
1437 Less than or equal to
1438
1439        .PP
1440 5 <= 4
1441 → FALSE
1442 T}
1443 T{
1444 .PP
1445 \fInumber\fR
1446 >
1447 \fInumber\fR
1448 → \fIboolean\fR
1449
1450        .PP
1451 Greater than
1452
1453        .PP
1454 5 > 4
1455 → TRUE
1456 T}
1457 T{
1458 .PP
1459 \fInumber\fR
1460 >=
1461 \fInumber\fR
1462 → \fIboolean\fR
1463
1464        .PP
1465 Greater than or equal to
1466
1467        .PP
1468 5 >= 4
1469 → TRUE
1470 T}
1471 T{
1472 .PP
1473 \fIinteger\fR
1474 |
1475 \fIinteger\fR
1476 → \fIinteger\fR
1477
1478        .PP
1479 Bitwise OR
1480
1481        .PP
1482 1 | 2
1483 → 3
1484 T}
1485 T{
1486 .PP
1487 \fIinteger\fR
1488 #
1489 \fIinteger\fR
1490 → \fIinteger\fR
1491
1492        .PP
1493 Bitwise XOR
1494
1495        .PP
1496 1 # 3
1497 → 2
1498 T}
1499 T{
1500 .PP
1501 \fIinteger\fR
1502 &
1503 \fIinteger\fR
1504 → \fIinteger\fR
1505
1506        .PP
1507 Bitwise AND
1508
1509        .PP
1510 1 & 3
1511 → 1
1512 T}
1513 T{
1514 .PP
1515 ~
1516 \fIinteger\fR
1517 → \fIinteger\fR
1518
1519        .PP
1520 Bitwise NOT
1521
1522        .PP
1523 ~ 1
1524 → \-2
1525 T}
1526 T{
1527 .PP
1528 \fIinteger\fR
1529 <<
1530 \fIinteger\fR
1531 → \fIinteger\fR
1532
1533        .PP
1534 Bitwise shift left
1535
1536        .PP
1537 1 << 2
1538 → 4
1539 T}
1540 T{
1541 .PP
1542 \fIinteger\fR
1543 >>
1544 \fIinteger\fR
1545 → \fIinteger\fR
1546
1547        .PP
1548 Bitwise shift right
1549
1550        .PP
1551 8 >> 2
1552 → 2
1553 T}
1554 T{
1555 .PP
1556 \fInumber\fR
1557 +
1558 \fInumber\fR
1559 → \fInumber\fR
1560
1561        .PP
1562 Addition
1563
1564        .PP
1565 5 + 4
1566 → 9
1567 T}
1568 T{
1569 .PP
1570 \fInumber\fR
1571 \-
1572 \fInumber\fR
1573 → \fInumber\fR
1574
1575        .PP
1576 Subtraction
1577
1578        .PP
1579 3 \- 2\&.0
1580 → 1\&.0
1581 T}
1582 T{
1583 .PP
1584 \fInumber\fR
1585 *
1586 \fInumber\fR
1587 → \fInumber\fR
1588
1589        .PP
1590 Multiplication
1591
1592        .PP
1593 5 * 4
1594 → 20
1595 T}
1596 T{
1597 .PP
1598 \fInumber\fR
1599 /
1600 \fInumber\fR
1601 → \fInumber\fR
1602
1603        .PP
1604 Division (truncates the result towards zero if both inputs are integers)
1605
1606        .PP
1607 5 / 3
1608 → 1
1609 T}
1610 T{
1611 .PP
1612 \fIinteger\fR
1613 %
1614 \fIinteger\fR
1615 → \fIinteger\fR
1616
1617        .PP
1618 Modulo (remainder)
1619
1620        .PP
1621 3 % 2
1622 → 1
1623 T}
1624 T{
1625 .PP
1626 \-
1627 \fInumber\fR
1628 → \fInumber\fR
1629
1630        .PP
1631 Negation
1632
1633        .PP
1634 \- 2\&.0
1635 → \-2\&.0
1636 T}
1637 .TE
1638 .sp 1
1639 .SS "Built\-In Functions"
1640 .PP
1641 The functions listed in
1642 Table\ \&303
1643 are built into
1644 pgbench
1645 and may be used in expressions appearing in
1646 \eset\&.
1647 .sp
1648 .it 1 an-trap
1649 .nr an-no-space-flag 1
1650 .nr an-break-flag 1
1651 .br
1652 .B Table\ \&303.\ \&pgbench Functions
1653 .TS
1654 allbox tab(:);
1655 lB.
1656 T{
1657 .PP
1658 Function
1659
1660        .PP
1661 Description
1662
1663        .PP
1664 Example(s)
1665 T}
1666 .T&
1667 l
1668 l
1669 l
1670 l
1671 l
1672 l
1673 l
1674 l
1675 l
1676 l
1677 l
1678 l
1679 l
1680 l
1681 l
1682 l
1683 l
1684 l
1685 l
1686 l.
1687 T{
1688 .PP
1689 \fBabs\fR
1690 (
1691 \fInumber\fR
1692 )
1693 →
1694 same type as input
1695
1696        .PP
1697 Absolute value
1698
1699        .PP
1700 abs(\-17)
1701 → 17
1702 T}
1703 T{
1704 .PP
1705 \fBdebug\fR
1706 (
1707 \fInumber\fR
1708 )
1709 →
1710 same type as input
1711
1712        .PP
1713 Prints the argument to
1714 stderr, and returns the argument\&.
1715
1716        .PP
1717 debug(5432\&.1)
1718 → 5432\&.1
1719 T}
1720 T{
1721 .PP
1722 \fBdouble\fR
1723 (
1724 \fInumber\fR
1725 )
1726 → double
1727
1728        .PP
1729 Casts to double\&.
1730
1731        .PP
1732 double(5432)
1733 → 5432\&.0
1734 T}
1735 T{
1736 .PP
1737 \fBexp\fR
1738 (
1739 \fInumber\fR
1740 )
1741 → double
1742
1743        .PP
1744 Exponential (e
1745 raised to the given power)
1746
1747        .PP
1748 exp(1\&.0)
1749 → 2\&.718281828459045
1750 T}
1751 T{
1752 .PP
1753 \fBgreatest\fR
1754 (
1755 \fInumber\fR
1756 [, \&.\&.\&. ]
1757 )
1758 →
1759 double
1760 if any argument is double, else
1761 integer
1762
1763        .PP
1764 Selects the largest value among the arguments\&.
1765
1766        .PP
1767 greatest(5, 4, 3, 2)
1768 → 5
1769 T}
1770 T{
1771 .PP
1772 \fBhash\fR
1773 (
1774 \fIvalue\fR
1775 [, \fIseed\fR ]
1776 )
1777 → integer
1778
1779        .PP
1780 This is an alias for
1781 \fBhash_murmur2\fR\&.
1782
1783        .PP
1784 hash(10, 5432)
1785 → \-5817877081768721676
1786 T}
1787 T{
1788 .PP
1789 \fBhash_fnv1a\fR
1790 (
1791 \fIvalue\fR
1792 [, \fIseed\fR ]
1793 )
1794 → integer
1795
1796        .PP
1797 Computes
1798 \m[blue]\fBFNV\-1a hash\fR\m[]\&.
1799
1800        .PP
1801 hash_fnv1a(10, 5432)
1802 → \-7793829335365542153
1803 T}
1804 T{
1805 .PP
1806 \fBhash_murmur2\fR
1807 (
1808 \fIvalue\fR
1809 [, \fIseed\fR ]
1810 )
1811 → integer
1812
1813        .PP
1814 Computes
1815 \m[blue]\fBMurmurHash2 hash\fR\m[]\&.
1816
1817        .PP
1818 hash_murmur2(10, 5432)
1819 → \-5817877081768721676
1820 T}
1821 T{
1822 .PP
1823 \fBint\fR
1824 (
1825 \fInumber\fR
1826 )
1827 → integer
1828
1829        .PP
1830 Casts to integer\&.
1831
1832        .PP
1833 int(5\&.4 + 3\&.8)
1834 → 9
1835 T}
1836 T{
1837 .PP
1838 \fBleast\fR
1839 (
1840 \fInumber\fR
1841 [, \&.\&.\&. ]
1842 )
1843 →
1844 double
1845 if any argument is double, else
1846 integer
1847
1848        .PP
1849 Selects the smallest value among the arguments\&.
1850
1851        .PP
1852 least(5, 4, 3, 2\&.1)
1853 → 2\&.1
1854 T}
1855 T{
1856 .PP
1857 \fBln\fR
1858 (
1859 \fInumber\fR
1860 )
1861 → double
1862
1863        .PP
1864 Natural logarithm
1865
1866        .PP
1867 ln(2\&.718281828459045)
1868 → 1\&.0
1869 T}
1870 T{
1871 .PP
1872 \fBmod\fR
1873 (
1874 \fIinteger\fR,
1875 \fIinteger\fR
1876 )
1877 → integer
1878
1879        .PP
1880 Modulo (remainder)
1881
1882        .PP
1883 mod(54, 32)
1884 → 22
1885 T}
1886 T{
1887 .PP
1888 \fBpermute\fR
1889 (
1890 \fIi\fR,
1891 \fIsize\fR
1892 [,
1893 \fIseed\fR
1894 ] )
1895 → integer
1896
1897        .PP
1898 Permuted value of
1899 \fIi\fR, in the range
1900 [0, size)\&. This is the new position of
1901 \fIi\fR
1902 (modulo
1903 \fIsize\fR) in a pseudorandom permutation of the integers
1904 0\&.\&.\&.size\-1, parameterized by
1905 \fIseed\fR, see below\&.
1906
1907        .PP
1908 permute(0, 4)
1909 → an integer between 0 and 3
1910 T}
1911 T{
1912 .PP
1913 \fBpi\fR
1914 ()
1915 → double
1916
1917        .PP
1918 Approximate value of
1919 π
1920
1921        .PP
1922 pi()
1923 → 3\&.14159265358979323846
1924 T}
1925 T{
1926 .PP
1927 \fBpow\fR
1928 (
1929 \fIx\fR,
1930 \fIy\fR
1931 )
1932 → double
1933
1934        .PP
1935 \fBpower\fR
1936 (
1937 \fIx\fR,
1938 \fIy\fR
1939 )
1940 → double
1941
1942        .PP
1943 \fIx\fR
1944 raised to the power of
1945 \fIy\fR
1946
1947        .PP
1948 pow(2\&.0, 10)
1949 → 1024\&.0
1950 T}
1951 T{
1952 .PP
1953 \fBrandom\fR
1954 (
1955 \fIlb\fR,
1956 \fIub\fR
1957 )
1958 → integer
1959
1960        .PP
1961 Computes a uniformly\-distributed random integer in
1962 [lb, ub]\&.
1963
1964        .PP
1965 random(1, 10)
1966 → an integer between 1 and 10
1967 T}
1968 T{
1969 .PP
1970 \fBrandom_exponential\fR
1971 (
1972 \fIlb\fR,
1973 \fIub\fR,
1974 \fIparameter\fR
1975 )
1976 → integer
1977
1978        .PP
1979 Computes an exponentially\-distributed random integer in
1980 [lb, ub], see below\&.
1981
1982        .PP
1983 random_exponential(1, 10, 3\&.0)
1984 → an integer between 1 and 10
1985 T}
1986 T{
1987 .PP
1988 \fBrandom_gaussian\fR
1989 (
1990 \fIlb\fR,
1991 \fIub\fR,
1992 \fIparameter\fR
1993 )
1994 → integer
1995
1996        .PP
1997 Computes a Gaussian\-distributed random integer in
1998 [lb, ub], see below\&.
1999
2000        .PP
2001 random_gaussian(1, 10, 2\&.5)
2002 → an integer between 1 and 10
2003 T}
2004 T{
2005 .PP
2006 \fBrandom_zipfian\fR
2007 (
2008 \fIlb\fR,
2009 \fIub\fR,
2010 \fIparameter\fR
2011 )
2012 → integer
2013
2014        .PP
2015 Computes a Zipfian\-distributed random integer in
2016 [lb, ub], see below\&.
2017
2018        .PP
2019 random_zipfian(1, 10, 1\&.5)
2020 → an integer between 1 and 10
2021 T}
2022 T{
2023 .PP
2024 \fBsqrt\fR
2025 (
2026 \fInumber\fR
2027 )
2028 → double
2029
2030        .PP
2031 Square root
2032
2033        .PP
2034 sqrt(2\&.0)
2035 → 1\&.414213562
2036 T}
2037 .TE
2038 .sp 1
2039 .PP
2040 The
2041 random
2042 function generates values using a uniform distribution, that is all the values are drawn within the specified range with equal probability\&. The
2043 random_exponential,
2044 random_gaussian
2045 and
2046 random_zipfian
2047 functions require an additional double parameter which determines the precise shape of the distribution\&.
2048 .sp
2049 .RS 4
2050 .ie n \{\
2051 \h'-04'\(bu\h'+03'\c
2052 .\}
2053 .el \{\
2054 .sp -1
2055 .IP \(bu 2.3
2056 .\}
2057 For an exponential distribution,
2058 \fIparameter\fR
2059 controls the distribution by truncating a quickly\-decreasing exponential distribution at
2060 \fIparameter\fR, and then projecting onto integers between the bounds\&. To be precise, with
2061 .sp
2062 .if n \{\
2063 .RS 4
2064 .\}
2065 .nf
2066 f(x) = exp(\-parameter * (x \- min) / (max \- min + 1)) / (1 \- exp(\-parameter))
2067 .fi
2068 .if n \{\
2069 .RE
2070 .\}
2071 .sp
2072 Then value
2073 \fIi\fR
2074 between
2075 \fImin\fR
2076 and
2077 \fImax\fR
2078 inclusive is drawn with probability:
2079 f(i) \- f(i + 1)\&.
2080 .sp
2081 Intuitively, the larger the
2082 \fIparameter\fR, the more frequently values close to
2083 \fImin\fR
2084 are accessed, and the less frequently values close to
2085 \fImax\fR
2086 are accessed\&. The closer to 0
2087 \fIparameter\fR
2088 is, the flatter (more uniform) the access distribution\&. A crude approximation of the distribution is that the most frequent 1% values in the range, close to
2089 \fImin\fR, are drawn
2090 \fIparameter\fR% of the time\&. The
2091 \fIparameter\fR
2092 value must be strictly positive\&.
2093 .RE
2094 .sp
2095 .RS 4
2096 .ie n \{\
2097 \h'-04'\(bu\h'+03'\c
2098 .\}
2099 .el \{\
2100 .sp -1
2101 .IP \(bu 2.3
2102 .\}
2103 For a Gaussian distribution, the interval is mapped onto a standard normal distribution (the classical bell\-shaped Gaussian curve) truncated at
2104 \-parameter
2105 on the left and
2106 +parameter
2107 on the right\&. Values in the middle of the interval are more likely to be drawn\&. To be precise, if
2108 PHI(x)
2109 is the cumulative distribution function of the standard normal distribution, with mean
2110 mu
2111 defined as
2112 (max + min) / 2\&.0, with
2113 .sp
2114 .if n \{\
2115 .RS 4
2116 .\}
2117 .nf
2118 f(x) = PHI(2\&.0 * parameter * (x \- mu) / (max \- min + 1)) /
2119        (2\&.0 * PHI(parameter) \- 1)
2120 .fi
2121 .if n \{\
2122 .RE
2123 .\}
2124 .sp
2125 then value
2126 \fIi\fR
2127 between
2128 \fImin\fR
2129 and
2130 \fImax\fR
2131 inclusive is drawn with probability:
2132 f(i + 0\&.5) \- f(i \- 0\&.5)\&. Intuitively, the larger the
2133 \fIparameter\fR, the more frequently values close to the middle of the interval are drawn, and the less frequently values close to the
2134 \fImin\fR
2135 and
2136 \fImax\fR
2137 bounds\&. About 67% of values are drawn from the middle
2138 1\&.0 / parameter, that is a relative
2139 0\&.5 / parameter
2140 around the mean, and 95% in the middle
2141 2\&.0 / parameter, that is a relative
2142 1\&.0 / parameter
2143 around the mean; for instance, if
2144 \fIparameter\fR
2145 is 4\&.0, 67% of values are drawn from the middle quarter (1\&.0 / 4\&.0) of the interval (i\&.e\&., from
2146 3\&.0 / 8\&.0
2147 to
2148 5\&.0 / 8\&.0) and 95% from the middle half (2\&.0 / 4\&.0) of the interval (second and third quartiles)\&. The minimum allowed
2149 \fIparameter\fR
2150 value is 2\&.0\&.
2151 .RE
2152 .sp
2153 .RS 4
2154 .ie n \{\
2155 \h'-04'\(bu\h'+03'\c
2156 .\}
2157 .el \{\
2158 .sp -1
2159 .IP \(bu 2.3
2160 .\}
2161 random_zipfian
2162 generates a bounded Zipfian distribution\&.
2163 \fIparameter\fR
2164 defines how skewed the distribution is\&. The larger the
2165 \fIparameter\fR, the more frequently values closer to the beginning of the interval are drawn\&. The distribution is such that, assuming the range starts from 1, the ratio of the probability of drawing
2166 \fIk\fR
2167 versus drawing
2168 \fIk+1\fR
2169 is
2170 ((\fIk\fR+1)/\fIk\fR)**\fIparameter\fR\&. For example,
2171 random_zipfian(1, \&.\&.\&., 2\&.5)
2172 produces the value
2173 1
2174 about
2175 (2/1)**2\&.5 = 5\&.66
2176 times more frequently than
2177 2, which itself is produced
2178 (3/2)**2\&.5 = 2\&.76
2179 times more frequently than
2180 3, and so on\&.
2181 .sp
2182 pgbench\*(Aqs implementation is based on "Non\-Uniform Random Variate Generation", Luc Devroye, p\&. 550\-551, Springer 1986\&. Due to limitations of that algorithm, the
2183 \fIparameter\fR
2184 value is restricted to the range [1\&.001, 1000]\&.
2185 .RE
2186 .if n \{\
2187 .sp
2188 .\}
2189 .RS 4
2190 .it 1 an-trap
2191 .nr an-no-space-flag 1
2192 .nr an-break-flag 1
2193 .br
2194 .ps +1
2195 \fBNote\fR
2196 .ps -1
2197 .br
2198 .PP
2199 When designing a benchmark which selects rows non\-uniformly, be aware that the rows chosen may be correlated with other data such as IDs from a sequence or the physical row ordering, which may skew performance measurements\&.
2200 .PP
2201 To avoid this, you may wish to use the
2202 \fBpermute\fR
2203 function, or some other additional step with similar effect, to shuffle the selected rows and remove such correlations\&.
2204 .sp .5v
2205 .RE
2206 .PP
2207 Hash functions
2208 hash,
2209 hash_murmur2
2210 and
2211 hash_fnv1a
2212 accept an input value and an optional seed parameter\&. In case the seed isn\*(Aqt provided the value of
2213 :default_seed
2214 is used, which is initialized randomly unless set by the command\-line
2215 \-D
2216 option\&.
2217 .PP
2218 permute
2219 accepts an input value, a size, and an optional seed parameter\&. It generates a pseudorandom permutation of integers in the range
2220 [0, size), and returns the index of the input value in the permuted values\&. The permutation chosen is parameterized by the seed, which defaults to
2221 :default_seed, if not specified\&. Unlike the hash functions,
2222 permute
2223 ensures that there are no collisions or holes in the output values\&. Input values outside the interval are interpreted modulo the size\&. The function raises an error if the size is not positive\&.
2224 \fBpermute\fR
2225 can be used to scatter the distribution of non\-uniform random functions such as
2226 random_zipfian
2227 or
2228 random_exponential
2229 so that values drawn more often are not trivially correlated\&. For instance, the following
2230 pgbench
2231 script simulates a possible real world workload typical for social media and blogging platforms where a few accounts generate excessive load:
2232 .sp
2233 .if n \{\
2234 .RS 4
2235 .\}
2236 .nf
2237 \eset size 1000000
2238 \eset r random_zipfian(1, :size, 1\&.07)
2239 \eset k 1 + permute(:r, :size)
2240 .fi
2241 .if n \{\
2242 .RE
2243 .\}
2244 .sp
2245 In some cases several distinct distributions are needed which don\*(Aqt correlate with each other and this is when the optional seed parameter comes in handy:
2246 .sp
2247 .if n \{\
2248 .RS 4
2249 .\}
2250 .nf
2251 \eset k1 1 + permute(:r, :size, :default_seed + 123)
2252 \eset k2 1 + permute(:r, :size, :default_seed + 321)
2253 .fi
2254 .if n \{\
2255 .RE
2256 .\}
2257 .sp
2258 A similar behavior can also be approximated with
2259 \fBhash\fR:
2260 .sp
2261 .if n \{\
2262 .RS 4
2263 .\}
2264 .nf
2265 \eset size 1000000
2266 \eset r random_zipfian(1, 100 * :size, 1\&.07)
2267 \eset k 1 + abs(hash(:r)) % :size
2268 .fi
2269 .if n \{\
2270 .RE
2271 .\}
2272 .sp
2273 However, since
2274 \fBhash\fR
2275 generates collisions, some values will not be reachable and others will be more frequent than expected from the original distribution\&.
2276 .PP
2277 As an example, the full definition of the built\-in TPC\-B\-like transaction is:
2278 .sp
2279 .if n \{\
2280 .RS 4
2281 .\}
2282 .nf
2283 \eset aid random(1, 100000 * :scale)
2284 \eset bid random(1, 1 * :scale)
2285 \eset tid random(1, 10 * :scale)
2286 \eset delta random(\-5000, 5000)
2287 BEGIN;
2288 UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;
2289 SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
2290 UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;
2291 UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;
2292 INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
2293 END;
2294 .fi
2295 .if n \{\
2296 .RE
2297 .\}
2298 .sp
2299 This script allows each iteration of the transaction to reference different, randomly\-chosen rows\&. (This example also shows why it\*(Aqs important for each client session to have its own variables \(em otherwise they\*(Aqd not be independently touching different rows\&.)
2300 .SS "Per\-Transaction Logging"
2301 .PP
2302 With the
2303 \fB\-l\fR
2304 option (but without the
2305 \fB\-\-aggregate\-interval\fR
2306 option),
2307 pgbench
2308 writes information about each transaction to a log file\&. The log file will be named
2309 \fIprefix\fR\&.\fInnn\fR, where
2310 \fIprefix\fR
2311 defaults to
2312 pgbench_log, and
2313 \fInnn\fR
2314 is the PID of the
2315 pgbench
2316 process\&. The prefix can be changed by using the
2317 \fB\-\-log\-prefix\fR
2318 option\&. If the
2319 \fB\-j\fR
2320 option is 2 or higher, so that there are multiple worker threads, each will have its own log file\&. The first worker will use the same name for its log file as in the standard single worker case\&. The additional log files for the other workers will be named
2321 \fIprefix\fR\&.\fInnn\fR\&.\fImmm\fR, where
2322 \fImmm\fR
2323 is a sequential number for each worker starting with 1\&.
2324 .PP
2325 Each line in a log file describes one transaction\&. It contains the following space\-separated fields:
2326 .PP
2327 \fIclient_id\fR
2328 .RS 4
2329 identifies the client session that ran the transaction
2330 .RE
2331 .PP
2332 \fItransaction_no\fR
2333 .RS 4
2334 counts how many transactions have been run by that session
2335 .RE
2336 .PP
2337 \fItime\fR
2338 .RS 4
2339 transaction\*(Aqs elapsed time, in microseconds
2340 .RE
2341 .PP
2342 \fIscript_no\fR
2343 .RS 4
2344 identifies the script file that was used for the transaction (useful when multiple scripts are specified with
2345 \fB\-f\fR
2346 or
2347 \fB\-b\fR)
2348 .RE
2349 .PP
2350 \fItime_epoch\fR
2351 .RS 4
2352 transaction\*(Aqs completion time, as a Unix\-epoch time stamp
2353 .RE
2354 .PP
2355 \fItime_us\fR
2356 .RS 4
2357 fractional\-second part of transaction\*(Aqs completion time, in microseconds
2358 .RE
2359 .PP
2360 \fIschedule_lag\fR
2361 .RS 4
2362 transaction start delay, that is the difference between the transaction\*(Aqs scheduled start time and the time it actually started, in microseconds (present only if
2363 \fB\-\-rate\fR
2364 is specified)
2365 .RE
2366 .PP
2367 \fIretries\fR
2368 .RS 4
2369 count of retries after serialization or deadlock errors during the transaction (present only if
2370 \fB\-\-max\-tries\fR
2371 is not equal to one)
2372 .RE
2373 .PP
2374 When both
2375 \fB\-\-rate\fR
2376 and
2377 \fB\-\-latency\-limit\fR
2378 are used, the
2379 \fItime\fR
2380 for a skipped transaction will be reported as
2381 skipped\&. If the transaction ends with a failure, its
2382 \fItime\fR
2383 will be reported as
2384 failed\&. If you use the
2385 \fB\-\-failures\-detailed\fR
2386 option, the
2387 \fItime\fR
2388 of the failed transaction will be reported as
2389 serialization
2390 or
2391 deadlock
2392 depending on the type of failure (see
2393 Failures and Serialization/Deadlock Retries
2394 for more information)\&.
2395 .PP
2396 Here is a snippet of a log file generated in a single\-client run:
2397 .sp
2398 .if n \{\
2399 .RS 4
2400 .\}
2401 .nf
2402 0 199 2241 0 1175850568 995598
2403 0 200 2465 0 1175850568 998079
2404 0 201 2513 0 1175850569 608
2405 0 202 2038 0 1175850569 2663
2406 .fi
2407 .if n \{\
2408 .RE
2409 .\}
2410 .sp
2411 Another example with
2412 \-\-rate=100
2413 and
2414 \-\-latency\-limit=5
2415 (note the additional
2416 \fIschedule_lag\fR
2417 column):
2418 .sp
2419 .if n \{\
2420 .RS 4
2421 .\}
2422 .nf
2423 0 81 4621 0 1412881037 912698 3005
2424 0 82 6173 0 1412881037 914578 4304
2425 0 83 skipped 0 1412881037 914578 5217
2426 0 83 skipped 0 1412881037 914578 5099
2427 0 83 4722 0 1412881037 916203 3108
2428 0 84 4142 0 1412881037 918023 2333
2429 0 85 2465 0 1412881037 919759 740
2430 .fi
2431 .if n \{\
2432 .RE
2433 .\}
2434 .sp
2435 In this example, transaction 82 was late, because its latency (6\&.173 ms) was over the 5 ms limit\&. The next two transactions were skipped, because they were already late before they were even started\&.
2436 .PP
2437 The following example shows a snippet of a log file with failures and retries, with the maximum number of tries set to 10 (note the additional
2438 \fIretries\fR
2439 column):
2440 .sp
2441 .if n \{\
2442 .RS 4
2443 .\}
2444 .nf
2445 3 0 47423 0 1499414498 34501 3
2446 3 1 8333 0 1499414498 42848 0
2447 3 2 8358 0 1499414498 51219 0
2448 4 0 72345 0 1499414498 59433 6
2449 1 3 41718 0 1499414498 67879 4
2450 1 4 8416 0 1499414498 76311 0
2451 3 3 33235 0 1499414498 84469 3
2452 0 0 failed 0 1499414498 84905 9
2453 2 0 failed 0 1499414498 86248 9
2454 3 4 8307 0 1499414498 92788 0
2455 .fi
2456 .if n \{\
2457 .RE
2458 .\}
2459 .PP
2460 If the
2461 \fB\-\-failures\-detailed\fR
2462 option is used, the type of failure is reported in the
2463 \fItime\fR
2464 like this:
2465 .sp
2466 .if n \{\
2467 .RS 4
2468 .\}
2469 .nf
2470 3 0 47423 0 1499414498 34501 3
2471 3 1 8333 0 1499414498 42848 0
2472 3 2 8358 0 1499414498 51219 0
2473 4 0 72345 0 1499414498 59433 6
2474 1 3 41718 0 1499414498 67879 4
2475 1 4 8416 0 1499414498 76311 0
2476 3 3 33235 0 1499414498 84469 3
2477 0 0 serialization 0 1499414498 84905 9
2478 2 0 serialization 0 1499414498 86248 9
2479 3 4 8307 0 1499414498 92788 0
2480 .fi
2481 .if n \{\
2482 .RE
2483 .\}
2484 .PP
2485 When running a long test on hardware that can handle a lot of transactions, the log files can become very large\&. The
2486 \fB\-\-sampling\-rate\fR
2487 option can be used to log only a random sample of transactions\&.
2488 .SS "Aggregated Logging"
2489 .PP
2490 With the
2491 \fB\-\-aggregate\-interval\fR
2492 option, a different format is used for the log files\&. Each log line describes one aggregation interval\&. It contains the following space\-separated fields:
2493 .PP
2494 \fIinterval_start\fR
2495 .RS 4
2496 start time of the interval, as a Unix\-epoch time stamp
2497 .RE
2498 .PP
2499 \fInum_transactions\fR
2500 .RS 4
2501 number of transactions within the interval
2502 .RE
2503 .PP
2504 \fIsum_latency\fR
2505 .RS 4
2506 sum of transaction latencies
2507 .RE
2508 .PP
2509 \fIsum_latency_2\fR
2510 .RS 4
2511 sum of squares of transaction latencies
2512 .RE
2513 .PP
2514 \fImin_latency\fR
2515 .RS 4
2516 minimum transaction latency
2517 .RE
2518 .PP
2519 \fImax_latency\fR
2520 .RS 4
2521 maximum transaction latency
2522 .RE
2523 .PP
2524 \fIsum_lag\fR
2525 .RS 4
2526 sum of transaction start delays (zero unless
2527 \fB\-\-rate\fR
2528 is specified)
2529 .RE
2530 .PP
2531 \fIsum_lag_2\fR
2532 .RS 4
2533 sum of squares of transaction start delays (zero unless
2534 \fB\-\-rate\fR
2535 is specified)
2536 .RE
2537 .PP
2538 \fImin_lag\fR
2539 .RS 4
2540 minimum transaction start delay (zero unless
2541 \fB\-\-rate\fR
2542 is specified)
2543 .RE
2544 .PP
2545 \fImax_lag\fR
2546 .RS 4
2547 maximum transaction start delay (zero unless
2548 \fB\-\-rate\fR
2549 is specified)
2550 .RE
2551 .PP
2552 \fIskipped\fR
2553 .RS 4
2554 number of transactions skipped because they would have started too late (zero unless
2555 \fB\-\-rate\fR
2556 and
2557 \fB\-\-latency\-limit\fR
2558 are specified)
2559 .RE
2560 .PP
2561 \fIretried\fR
2562 .RS 4
2563 number of retried transactions (zero unless
2564 \fB\-\-max\-tries\fR
2565 is not equal to one)
2566 .RE
2567 .PP
2568 \fIretries\fR
2569 .RS 4
2570 number of retries after serialization or deadlock errors (zero unless
2571 \fB\-\-max\-tries\fR
2572 is not equal to one)
2573 .RE
2574 .PP
2575 \fIserialization_failures\fR
2576 .RS 4
2577 number of transactions that got a serialization error and were not retried afterwards (zero unless
2578 \fB\-\-failures\-detailed\fR
2579 is specified)
2580 .RE
2581 .PP
2582 \fIdeadlock_failures\fR
2583 .RS 4
2584 number of transactions that got a deadlock error and were not retried afterwards (zero unless
2585 \fB\-\-failures\-detailed\fR
2586 is specified)
2587 .RE
2588 .PP
2589 Here is some example output generated with this option:
2590 .sp
2591 .if n \{\
2592 .RS 4
2593 .\}
2594 .nf
2595 \fBpgbench \-\-aggregate\-interval=10 \-\-time=20 \-\-client=10 \-\-log \-\-rate=1000 \-\-latency\-limit=10 \-\-failures\-detailed \-\-max\-tries=10 test\fR
2596
2597 1650260552 5178 26171317 177284491527 1136 44462 2647617 7321113867 0 9866 64 7564 28340 4148 0
2598 1650260562 4808 25573984 220121792172 1171 62083 3037380 9666800914 0 9998 598 7392 26621 4527 0
2599 .fi
2600 .if n \{\
2601 .RE
2602 .\}
2603 .PP
2604 Notice that while the plain (unaggregated) log format shows which script was used for each transaction, the aggregated format does not\&. Therefore if you need per\-script data, you need to aggregate the data on your own\&.
2605 .SS "Per\-Statement Report"
2606 .PP
2607 With the
2608 \fB\-r\fR
2609 option,
2610 pgbench
2611 collects the following statistics for each statement:
2612 .sp
2613 .RS 4
2614 .ie n \{\
2615 \h'-04'\(bu\h'+03'\c
2616 .\}
2617 .el \{\
2618 .sp -1
2619 .IP \(bu 2.3
2620 .\}
2621 latency
2622 \(em elapsed transaction time for each statement\&.
2623 pgbench
2624 reports an average value of all successful runs of the statement\&.
2625 .RE
2626 .sp
2627 .RS 4
2628 .ie n \{\
2629 \h'-04'\(bu\h'+03'\c
2630 .\}
2631 .el \{\
2632 .sp -1
2633 .IP \(bu 2.3
2634 .\}
2635 The number of failures in this statement\&. See
2636 Failures and Serialization/Deadlock Retries
2637 for more information\&.
2638 .RE
2639 .sp
2640 .RS 4
2641 .ie n \{\
2642 \h'-04'\(bu\h'+03'\c
2643 .\}
2644 .el \{\
2645 .sp -1
2646 .IP \(bu 2.3
2647 .\}
2648 The number of retries after a serialization or a deadlock error in this statement\&. See
2649 Failures and Serialization/Deadlock Retries
2650 for more information\&.
2651 .RE
2652 .PP
2653 The report displays retry statistics only if the
2654 \fB\-\-max\-tries\fR
2655 option is not equal to 1\&.
2656 .PP
2657 All values are computed for each statement executed by every client and are reported after the benchmark has finished\&.
2658 .PP
2659 For the default script, the output will look similar to this:
2660 .sp
2661 .if n \{\
2662 .RS 4
2663 .\}
2664 .nf
2665 starting vacuum\&.\&.\&.end\&.
2666 transaction type: <builtin: TPC\-B (sort of)>
2667 scaling factor: 1
2668 query mode: simple
2669 number of clients: 10
2670 number of threads: 1
2671 maximum number of tries: 1
2672 number of transactions per client: 1000
2673 number of transactions actually processed: 10000/10000
2674 number of failed transactions: 0 (0\&.000%)
2675 number of transactions above the 50\&.0 ms latency limit: 1311/10000 (13\&.110 %)
2676 latency average = 28\&.488 ms
2677 latency stddev = 21\&.009 ms
2678 initial connection time = 69\&.068 ms
2679 tps = 346\&.224794 (without initial connection time)
2680 statement latencies in milliseconds and failures:
2681    0\&.012  0  \eset aid random(1, 100000 * :scale)
2682    0\&.002  0  \eset bid random(1, 1 * :scale)
2683    0\&.002  0  \eset tid random(1, 10 * :scale)
2684    0\&.002  0  \eset delta random(\-5000, 5000)
2685    0\&.319  0  BEGIN;
2686    0\&.834  0  UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;
2687    0\&.641  0  SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
2688   11\&.126  0  UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;
2689   12\&.961  0  UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;
2690    0\&.634  0  INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
2691    1\&.957  0  END;
2692 .fi
2693 .if n \{\
2694 .RE
2695 .\}
2696 .sp
2697 Another example of output for the default script using serializable default transaction isolation level (\fBPGOPTIONS=\*(Aq\-c default_transaction_isolation=serializable\*(Aq pgbench \&.\&.\&.\fR):
2698 .sp
2699 .if n \{\
2700 .RS 4
2701 .\}
2702 .nf
2703 starting vacuum\&.\&.\&.end\&.
2704 transaction type: <builtin: TPC\-B (sort of)>
2705 scaling factor: 1
2706 query mode: simple
2707 number of clients: 10
2708 number of threads: 1
2709 maximum number of tries: 10
2710 number of transactions per client: 1000
2711 number of transactions actually processed: 6317/10000
2712 number of failed transactions: 3683 (36\&.830%)
2713 number of transactions retried: 7667 (76\&.670%)
2714 total number of retries: 45339
2715 number of transactions above the 50\&.0 ms latency limit: 106/6317 (1\&.678 %)
2716 latency average = 17\&.016 ms
2717 latency stddev = 13\&.283 ms
2718 initial connection time = 45\&.017 ms
2719 tps = 186\&.792667 (without initial connection time)
2720 statement latencies in milliseconds, failures and retries:
2721   0\&.006     0      0  \eset aid random(1, 100000 * :scale)
2722   0\&.001     0      0  \eset bid random(1, 1 * :scale)
2723   0\&.001     0      0  \eset tid random(1, 10 * :scale)
2724   0\&.001     0      0  \eset delta random(\-5000, 5000)
2725   0\&.385     0      0  BEGIN;
2726   0\&.773     0      1  UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;
2727   0\&.624     0      0  SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
2728   1\&.098   320   3762  UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;
2729   0\&.582  3363  41576  UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;
2730   0\&.465     0      0  INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
2731   1\&.933     0      0  END;
2732 .fi
2733 .if n \{\
2734 .RE
2735 .\}
2736 .PP
2737 If multiple script files are specified, all statistics are reported separately for each script file\&.
2738 .PP
2739 Note that collecting the additional timing information needed for per\-statement latency computation adds some overhead\&. This will slow average execution speed and lower the computed TPS\&. The amount of slowdown varies significantly depending on platform and hardware\&. Comparing average TPS values with and without latency reporting enabled is a good way to measure if the timing overhead is significant\&.
2740 .SS "Failures and Serialization/Deadlock Retries"
2741 .PP
2742 When executing
2743 pgbench, there are three main types of errors:
2744 .sp
2745 .RS 4
2746 .ie n \{\
2747 \h'-04'\(bu\h'+03'\c
2748 .\}
2749 .el \{\
2750 .sp -1
2751 .IP \(bu 2.3
2752 .\}
2753 Errors of the main program\&. They are the most serious and always result in an immediate exit from
2754 pgbench
2755 with the corresponding error message\&. They include:
2756 .sp
2757 .RS 4
2758 .ie n \{\
2759 \h'-04'\(bu\h'+03'\c
2760 .\}
2761 .el \{\
2762 .sp -1
2763 .IP \(bu 2.3
2764 .\}
2765 errors at the beginning of
2766 pgbench
2767 (e\&.g\&. an invalid option value);
2768 .RE
2769 .sp
2770 .RS 4
2771 .ie n \{\
2772 \h'-04'\(bu\h'+03'\c
2773 .\}
2774 .el \{\
2775 .sp -1
2776 .IP \(bu 2.3
2777 .\}
2778 errors in the initialization mode (e\&.g\&. the query to create tables for built\-in scripts fails);
2779 .RE
2780 .sp
2781 .RS 4
2782 .ie n \{\
2783 \h'-04'\(bu\h'+03'\c
2784 .\}
2785 .el \{\
2786 .sp -1
2787 .IP \(bu 2.3
2788 .\}
2789 errors before starting threads (e\&.g\&. could not connect to the database server, syntax error in the meta command, thread creation failure);
2790 .RE
2791 .sp
2792 .RS 4
2793 .ie n \{\
2794 \h'-04'\(bu\h'+03'\c
2795 .\}
2796 .el \{\
2797 .sp -1
2798 .IP \(bu 2.3
2799 .\}
2800 internal
2801 pgbench
2802 errors (which are supposed to never occur\&.\&.\&.)\&.
2803 .RE
2804 .RE
2805 .sp
2806 .RS 4
2807 .ie n \{\
2808 \h'-04'\(bu\h'+03'\c
2809 .\}
2810 .el \{\
2811 .sp -1
2812 .IP \(bu 2.3
2813 .\}
2814 Errors when the thread manages its clients (e\&.g\&. the client could not start a connection to the database server / the socket for connecting the client to the database server has become invalid)\&. In such cases all clients of this thread stop while other threads continue to work\&. However,
2815 \fB\-\-exit\-on\-abort\fR
2816 is specified, all of the threads stop immediately in this case\&.
2817 .RE
2818 .sp
2819 .RS 4
2820 .ie n \{\
2821 \h'-04'\(bu\h'+03'\c
2822 .\}
2823 .el \{\
2824 .sp -1
2825 .IP \(bu 2.3
2826 .\}
2827 Direct client errors\&. They lead to immediate exit from
2828 pgbench
2829 with the corresponding error message in the case of an internal
2830 pgbench
2831 error (which are supposed to never occur\&.\&.\&.) or when
2832 \fB\-\-exit\-on\-abort\fR
2833 is specified\&. Otherwise in the worst case they only lead to the abortion of the failed client while other clients continue their run (but some client errors are handled without an abortion of the client and reported separately, see below)\&. Later in this section it is assumed that the discussed errors are only the direct client errors and they are not internal
2834 pgbench
2835 errors\&.
2836 .RE
2837 .PP
2838 A client\*(Aqs run is aborted in case of a serious error; for example, the connection with the database server was lost or the end of script was reached without completing the last transaction\&. In addition, if execution of an SQL or meta command fails for reasons other than serialization or deadlock errors, the client is aborted\&. Otherwise, if an SQL command fails with serialization or deadlock errors, the client is not aborted\&. In such cases, the current transaction is rolled back, which also includes setting the client variables as they were before the run of this transaction (it is assumed that one transaction script contains only one transaction; see
2839 What Is the "Transaction" Actually Performed in pgbench?
2840 for more information)\&. Transactions with serialization or deadlock errors are repeated after rollbacks until they complete successfully or reach the maximum number of tries (specified by the
2841 \fB\-\-max\-tries\fR
2842 option) / the maximum time of retries (specified by the
2843 \fB\-\-latency\-limit\fR
2844 option) / the end of benchmark (specified by the
2845 \fB\-\-time\fR
2846 option)\&. If the last trial run fails, this transaction will be reported as failed but the client is not aborted and continues to work\&.
2847 .if n \{\
2848 .sp
2849 .\}
2850 .RS 4
2851 .it 1 an-trap
2852 .nr an-no-space-flag 1
2853 .nr an-break-flag 1
2854 .br
2855 .ps +1
2856 \fBNote\fR
2857 .ps -1
2858 .br
2859 .PP
2860 Without specifying the
2861 \fB\-\-max\-tries\fR
2862 option, a transaction will never be retried after a serialization or deadlock error because its default value is 1\&. Use an unlimited number of tries (\-\-max\-tries=0) and the
2863 \fB\-\-latency\-limit\fR
2864 option to limit only the maximum time of tries\&. You can also use the
2865 \fB\-\-time\fR
2866 option to limit the benchmark duration under an unlimited number of tries\&.
2867 .PP
2868 Be careful when repeating scripts that contain multiple transactions: the script is always retried completely, so successful transactions can be performed several times\&.
2869 .PP
2870 Be careful when repeating transactions with shell commands\&. Unlike the results of SQL commands, the results of shell commands are not rolled back, except for the variable value of the
2871 \fB\esetshell\fR
2872 command\&.
2873 .sp .5v
2874 .RE
2875 .PP
2876 The latency of a successful transaction includes the entire time of transaction execution with rollbacks and retries\&. The latency is measured only for successful transactions and commands but not for failed transactions or commands\&.
2877 .PP
2878 The main report contains the number of failed transactions\&. If the
2879 \fB\-\-max\-tries\fR
2880 option is not equal to 1, the main report also contains statistics related to retries: the total number of retried transactions and total number of retries\&. The per\-script report inherits all these fields from the main report\&. The per\-statement report displays retry statistics only if the
2881 \fB\-\-max\-tries\fR
2882 option is not equal to 1\&.
2883 .PP
2884 If you want to group failures by basic types in per\-transaction and aggregation logs, as well as in the main and per\-script reports, use the
2885 \fB\-\-failures\-detailed\fR
2886 option\&. If you also want to distinguish all errors and failures (errors without retrying) by type including which limit for retries was exceeded and how much it was exceeded by for the serialization/deadlock failures, use the
2887 \fB\-\-verbose\-errors\fR
2888 option\&.
2889 .SS "Table Access Methods"
2890 .PP
2891 You may specify the
2892 Table Access Method
2893 for the pgbench tables\&. The environment variable
2894 \fBPGOPTIONS\fR
2895 specifies database configuration options that are passed to PostgreSQL via the command line (See
2896 Section\ \&19.1.4)\&. For example, a hypothetical default Table Access Method for the tables that pgbench creates called
2897 wuzza
2898 can be specified with:
2899 .sp
2900 .if n \{\
2901 .RS 4
2902 .\}
2903 .nf
2904 PGOPTIONS=\*(Aq\-c default_table_access_method=wuzza\*(Aq
2905 .fi
2906 .if n \{\
2907 .RE
2908 .\}
2909 .sp
2910 .SS "Good Practices"
2911 .PP
2912 It is very easy to use
2913 pgbench
2914 to produce completely meaningless numbers\&. Here are some guidelines to help you get useful results\&.
2915 .PP
2916 In the first place,
2917 \fInever\fR
2918 believe any test that runs for only a few seconds\&. Use the
2919 \fB\-t\fR
2920 or
2921 \fB\-T\fR
2922 option to make the run last at least a few minutes, so as to average out noise\&. In some cases you could need hours to get numbers that are reproducible\&. It\*(Aqs a good idea to try the test run a few times, to find out if your numbers are reproducible or not\&.
2923 .PP
2924 For the default TPC\-B\-like test scenario, the initialization scale factor (\fB\-s\fR) should be at least as large as the largest number of clients you intend to test (\fB\-c\fR); else you\*(Aqll mostly be measuring update contention\&. There are only
2925 \fB\-s\fR
2926 rows in the
2927 pgbench_branches
2928 table, and every transaction wants to update one of them, so
2929 \fB\-c\fR
2930 values in excess of
2931 \fB\-s\fR
2932 will undoubtedly result in lots of transactions blocked waiting for other transactions\&.
2933 .PP
2934 The default test scenario is also quite sensitive to how long it\*(Aqs been since the tables were initialized: accumulation of dead rows and dead space in the tables changes the results\&. To understand the results you must keep track of the total number of updates and when vacuuming happens\&. If autovacuum is enabled it can result in unpredictable changes in measured performance\&.
2935 .PP
2936 A limitation of
2937 pgbench
2938 is that it can itself become the bottleneck when trying to test a large number of client sessions\&. This can be alleviated by running
2939 pgbench
2940 on a different machine from the database server, although low network latency will be essential\&. It might even be useful to run several
2941 pgbench
2942 instances concurrently, on several client machines, against the same database server\&.
2943 .SS "Security"
2944 .PP
2945 If untrusted users have access to a database that has not adopted a
2946 secure schema usage pattern, do not run
2947 pgbench
2948 in that database\&.
2949 pgbench
2950 uses unqualified names and does not manipulate the search path\&.