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