]> begriffs open source - ai-pg/blob - full-docs/html/pgbench.html
Include latest toc output
[ai-pg] / full-docs / html / pgbench.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>pgbench</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="app-pgbasebackup.html" title="pg_basebackup" /><link rel="next" href="app-pgcombinebackup.html" title="pg_combinebackup" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center"><span class="application">pgbench</span></th></tr><tr><td width="10%" align="left"><a accesskey="p" href="app-pgbasebackup.html" title="pg_basebackup">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="reference-client.html" title="PostgreSQL Client Applications">Up</a></td><th width="60%" align="center">PostgreSQL Client Applications</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="app-pgcombinebackup.html" title="pg_combinebackup">Next</a></td></tr></table><hr /></div><div class="refentry" id="PGBENCH"><div class="titlepage"></div><a id="id-1.9.4.11.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle"><span class="application">pgbench</span></span></h2><p>pgbench — run a benchmark test on <span class="productname">PostgreSQL</span></p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><div class="cmdsynopsis"><p id="id-1.9.4.11.4.1"><code class="command">pgbench</code>  <code class="option">-i</code>  [<em class="replaceable"><code>option</code></em>...] [<em class="replaceable"><code>dbname</code></em>]</p></div><div class="cmdsynopsis"><p id="id-1.9.4.11.4.2"><code class="command">pgbench</code> [<em class="replaceable"><code>option</code></em>...] [<em class="replaceable"><code>dbname</code></em>]</p></div></div><div class="refsect1" id="id-1.9.4.11.5"><h2>Description</h2><p>
3   <span class="application">pgbench</span> is a simple program for running benchmark
4   tests on <span class="productname">PostgreSQL</span>.  It runs the same sequence of SQL
5   commands over and over, possibly in multiple concurrent database sessions,
6   and then calculates the average transaction rate (transactions per second).
7   By default, <span class="application">pgbench</span> tests a scenario that is
8   loosely based on TPC-B, involving five <code class="command">SELECT</code>,
9   <code class="command">UPDATE</code>, and <code class="command">INSERT</code> commands per transaction.
10   However, it is easy to test other cases by writing your own transaction
11   script files.
12  </p><p>
13   Typical output from <span class="application">pgbench</span> looks like:
14
15 </p><pre class="screen">
16 transaction type: &lt;builtin: TPC-B (sort of)&gt;
17 scaling factor: 10
18 query mode: simple
19 number of clients: 10
20 number of threads: 1
21 maximum number of tries: 1
22 number of transactions per client: 1000
23 number of transactions actually processed: 10000/10000
24 number of failed transactions: 0 (0.000%)
25 latency average = 11.013 ms
26 latency stddev = 7.351 ms
27 initial connection time = 45.758 ms
28 tps = 896.967014 (without initial connection time)
29 </pre><p>
30
31   The first seven lines report some of the most important parameter
32   settings.
33   The sixth line reports the maximum number of tries for transactions with
34   serialization or deadlock errors (see <a class="xref" href="pgbench.html#FAILURES-AND-RETRIES" title="Failures and Serialization/Deadlock Retries">Failures and Serialization/Deadlock Retries</a>
35   for more information).
36   The eighth line reports the number of transactions completed
37   and intended (the latter being just the product of number of clients
38   and number of transactions per client); these will be equal unless the run
39   failed before completion or some SQL command(s) failed.  (In
40   <code class="option">-T</code> mode, only the actual number of transactions is printed.)
41   The next line reports the number of failed transactions due to
42   serialization or deadlock errors (see <a class="xref" href="pgbench.html#FAILURES-AND-RETRIES" title="Failures and Serialization/Deadlock Retries">Failures and Serialization/Deadlock Retries</a>
43   for more information).
44   The last line reports the number of transactions per second.
45  </p><p>
46    The default TPC-B-like transaction test requires specific tables to be
47    set up beforehand.  <span class="application">pgbench</span> should be invoked with
48    the <code class="option">-i</code> (initialize) option to create and populate these
49    tables.  (When you are testing a custom script, you don't need this
50    step, but will instead need to do whatever setup your test needs.)
51    Initialization looks like:
52
53 </p><pre class="programlisting">
54 pgbench -i [<span class="optional"> <em class="replaceable"><code>other-options</code></em> </span>] <em class="replaceable"><code>dbname</code></em>
55 </pre><p>
56
57    where <em class="replaceable"><code>dbname</code></em> is the name of the already-created
58    database to test in.  (You may also need <code class="option">-h</code>,
59    <code class="option">-p</code>, and/or <code class="option">-U</code> options to specify how to
60    connect to the database server.)
61   </p><div class="caution"><h3 class="title">Caution</h3><p>
62     <code class="literal">pgbench -i</code> creates four tables <code class="structname">pgbench_accounts</code>,
63     <code class="structname">pgbench_branches</code>, <code class="structname">pgbench_history</code>, and
64     <code class="structname">pgbench_tellers</code>,
65     destroying any existing tables of these names.
66     Be very careful to use another database if you have tables having these
67     names!
68    </p></div><p>
69    At the default <span class="quote">“<span class="quote">scale factor</span>”</span> of 1, the tables initially
70    contain this many rows:
71 </p><pre class="screen">
72 table                   # of rows
73 ---------------------------------
74 pgbench_branches        1
75 pgbench_tellers         10
76 pgbench_accounts        100000
77 pgbench_history         0
78 </pre><p>
79    You can (and, for most purposes, probably should) increase the number
80    of rows by using the <code class="option">-s</code> (scale factor) option.  The
81    <code class="option">-F</code> (fillfactor) option might also be used at this point.
82   </p><p>
83    Once you have done the necessary setup, you can run your benchmark
84    with a command that doesn't include <code class="option">-i</code>, that is
85
86 </p><pre class="programlisting">
87 pgbench [<span class="optional"> <em class="replaceable"><code>options</code></em> </span>] <em class="replaceable"><code>dbname</code></em>
88 </pre><p>
89
90    In nearly all cases, you'll need some options to make a useful test.
91    The most important options are <code class="option">-c</code> (number of clients),
92    <code class="option">-t</code> (number of transactions), <code class="option">-T</code> (time limit),
93    and <code class="option">-f</code> (specify a custom script file).
94    See below for a full list.
95   </p></div><div class="refsect1" id="id-1.9.4.11.6"><h2>Options</h2><p>
96    The following is divided into three subsections.  Different options are
97    used during database initialization and while running benchmarks, but some
98    options are useful in both cases.
99   </p><div class="refsect2" id="PGBENCH-INIT-OPTIONS"><h3>Initialization Options</h3><p>
100     <span class="application">pgbench</span> accepts the following command-line
101     initialization arguments:
102
103     </p><div class="variablelist"><dl class="variablelist"><dt id="PGBENCH-OPTION-DBNAME"><span class="term"><code class="option">[<span class="optional">-d</span>] <em class="replaceable"><code>dbname</code></em></code><br /></span><span class="term"><code class="option">[<span class="optional">--dbname=</span>]<em class="replaceable"><code>dbname</code></em></code></span> <a href="#PGBENCH-OPTION-DBNAME" class="id_link">#</a></dt><dd><p>
104         Specifies the name of the database to test in. If this is
105         not specified, the environment variable
106         <code class="envar">PGDATABASE</code> is used. If that is not set, the
107         user name specified for the connection is used.
108        </p></dd><dt id="PGBENCH-OPTION-INITIALIZE"><span class="term"><code class="option">-i</code><br /></span><span class="term"><code class="option">--initialize</code></span> <a href="#PGBENCH-OPTION-INITIALIZE" class="id_link">#</a></dt><dd><p>
109         Required to invoke initialization mode.
110        </p></dd><dt id="PGBENCH-OPTION-INIT-STEPS"><span class="term"><code class="option">-I <em class="replaceable"><code>init_steps</code></em></code><br /></span><span class="term"><code class="option">--init-steps=<em class="replaceable"><code>init_steps</code></em></code></span> <a href="#PGBENCH-OPTION-INIT-STEPS" class="id_link">#</a></dt><dd><p>
111         Perform just a selected set of the normal initialization steps.
112         <em class="replaceable"><code>init_steps</code></em> specifies the
113         initialization steps to be performed, using one character per step.
114         Each step is invoked in the specified order.
115         The default is <code class="literal">dtgvp</code>.
116         The available steps are:
117
118         </p><div class="variablelist"><dl class="variablelist"><dt id="PGBENCH-OPTION-INIT-STEPS-D"><span class="term"><code class="literal">d</code> (Drop)</span> <a href="#PGBENCH-OPTION-INIT-STEPS-D" class="id_link">#</a></dt><dd><p>
119             Drop any existing <span class="application">pgbench</span> tables.
120            </p></dd><dt id="PGBENCH-OPTION-INIT-STEPS-T"><span class="term"><code class="literal">t</code> (create Tables)</span> <a href="#PGBENCH-OPTION-INIT-STEPS-T" class="id_link">#</a></dt><dd><p>
121             Create the tables used by the
122             standard <span class="application">pgbench</span> scenario, namely
123             <code class="structname">pgbench_accounts</code>,
124             <code class="structname">pgbench_branches</code>,
125             <code class="structname">pgbench_history</code>, and
126             <code class="structname">pgbench_tellers</code>.
127            </p></dd><dt id="PGBENCH-OPTION-INIT-STEPS-G"><span class="term"><code class="literal">g</code> or <code class="literal">G</code> (Generate data, client-side or server-side)</span> <a href="#PGBENCH-OPTION-INIT-STEPS-G" class="id_link">#</a></dt><dd><p>
128             Generate data and load it into the standard tables,
129             replacing any data already present.
130            </p><p>
131             With <code class="literal">g</code> (client-side data generation),
132             data is generated in <code class="command">pgbench</code> client and then
133             sent to the server. This uses the client/server bandwidth
134             extensively through a <code class="command">COPY</code>.
135             <code class="command">pgbench</code> uses the <code class="option">FREEZE</code> option
136             to load data into ordinary (non-partition) tables with version 14
137             or later of <span class="productname">PostgreSQL</span> to speed up
138             subsequent <code class="command">VACUUM</code>.
139             Using <code class="literal">g</code> causes logging to
140             print one message every 100,000 rows while generating data for all
141             tables.
142            </p><p>
143             With <code class="literal">G</code> (server-side data generation),
144             only small queries are sent from the <code class="command">pgbench</code>
145             client and then data is actually generated in the server.
146             No significant bandwidth is required for this variant, but
147             the server will do more work.
148             Using <code class="literal">G</code> causes logging not to print any progress
149             message while generating data.
150            </p><p>
151             The default initialization behavior uses client-side data
152             generation (equivalent to <code class="literal">g</code>).
153            </p></dd><dt id="PGBENCH-OPTION-INIT-STEPS-V"><span class="term"><code class="literal">v</code> (Vacuum)</span> <a href="#PGBENCH-OPTION-INIT-STEPS-V" class="id_link">#</a></dt><dd><p>
154             Invoke <code class="command">VACUUM</code> on the standard tables.
155            </p></dd><dt id="PGBENCH-OPTION-INIT-STEPS-P"><span class="term"><code class="literal">p</code> (create Primary keys)</span> <a href="#PGBENCH-OPTION-INIT-STEPS-P" class="id_link">#</a></dt><dd><p>
156             Create primary key indexes on the standard tables.
157            </p></dd><dt id="PGBENCH-OPTION-INIT-STEPS-F"><span class="term"><code class="literal">f</code> (create Foreign keys)</span> <a href="#PGBENCH-OPTION-INIT-STEPS-F" class="id_link">#</a></dt><dd><p>
158             Create foreign key constraints between the standard tables.
159             (Note that this step is not performed by default.)
160            </p></dd></dl></div></dd><dt id="PGBENCH-OPTION-FILLFACTOR"><span class="term"><code class="option">-F</code> <em class="replaceable"><code>fillfactor</code></em><br /></span><span class="term"><code class="option">--fillfactor=</code><em class="replaceable"><code>fillfactor</code></em></span> <a href="#PGBENCH-OPTION-FILLFACTOR" class="id_link">#</a></dt><dd><p>
161         Create the <code class="structname">pgbench_accounts</code>,
162         <code class="structname">pgbench_tellers</code> and
163         <code class="structname">pgbench_branches</code> tables with the given fillfactor.
164         Default is 100.
165        </p></dd><dt id="PGBENCH-OPTION-NO-VACUUM-INIT"><span class="term"><code class="option">-n</code><br /></span><span class="term"><code class="option">--no-vacuum</code></span> <a href="#PGBENCH-OPTION-NO-VACUUM-INIT" class="id_link">#</a></dt><dd><p>
166         Perform no vacuuming during initialization.
167         (This option suppresses the <code class="literal">v</code> initialization step,
168         even if it was specified in <code class="option">-I</code>.)
169        </p></dd><dt id="PGBENCH-OPTION-QUIET"><span class="term"><code class="option">-q</code><br /></span><span class="term"><code class="option">--quiet</code></span> <a href="#PGBENCH-OPTION-QUIET" class="id_link">#</a></dt><dd><p>
170         Switch logging to quiet mode, producing only one progress message per 5
171         seconds. The default logging prints one message each 100,000 rows, which
172         often outputs many lines per second (especially on good hardware).
173        </p><p>
174         This setting has no effect if <code class="literal">G</code> is specified
175         in <code class="option">-I</code>.
176        </p></dd><dt id="PGBENCH-OPTION-SCALE-INIT"><span class="term"><code class="option">-s</code> <em class="replaceable"><code>scale_factor</code></em><br /></span><span class="term"><code class="option">--scale=</code><em class="replaceable"><code>scale_factor</code></em></span> <a href="#PGBENCH-OPTION-SCALE-INIT" class="id_link">#</a></dt><dd><p>
177         Multiply the number of rows generated by the scale factor.
178         For example, <code class="literal">-s 100</code> will create 10,000,000 rows
179         in the <code class="structname">pgbench_accounts</code> table. Default is 1.
180         When the scale is 20,000 or larger, the columns used to
181         hold account identifiers (<code class="structfield">aid</code> columns)
182         will switch to using larger integers (<code class="type">bigint</code>),
183         in order to be big enough to hold the range of account
184         identifiers.
185        </p></dd><dt id="PGBENCH-OPTION-FOREIGN-KEYS"><span class="term"><code class="option">--foreign-keys</code></span> <a href="#PGBENCH-OPTION-FOREIGN-KEYS" class="id_link">#</a></dt><dd><p>
186         Create foreign key constraints between the standard tables.
187         (This option adds the <code class="literal">f</code> step to the initialization
188         step sequence, if it is not already present.)
189        </p></dd><dt id="PGBENCH-OPTION-INDEX-TABLESPACE"><span class="term"><code class="option">--index-tablespace=<em class="replaceable"><code>index_tablespace</code></em></code></span> <a href="#PGBENCH-OPTION-INDEX-TABLESPACE" class="id_link">#</a></dt><dd><p>
190         Create indexes in the specified tablespace, rather than the default
191         tablespace.
192        </p></dd><dt id="PGBENCH-OPTION-PARTITION-METHOD"><span class="term"><code class="option">--partition-method=<em class="replaceable"><code>NAME</code></em></code></span> <a href="#PGBENCH-OPTION-PARTITION-METHOD" class="id_link">#</a></dt><dd><p>
193         Create a partitioned <code class="literal">pgbench_accounts</code> table with
194         <em class="replaceable"><code>NAME</code></em> method.
195         Expected values are <code class="literal">range</code> or <code class="literal">hash</code>.
196         This option requires that <code class="option">--partitions</code> is set to non-zero.
197         If unspecified, default is <code class="literal">range</code>.
198        </p></dd><dt id="PGBENCH-OPTION-PARTITIONS"><span class="term"><code class="option">--partitions=<em class="replaceable"><code>NUM</code></em></code></span> <a href="#PGBENCH-OPTION-PARTITIONS" class="id_link">#</a></dt><dd><p>
199         Create a partitioned <code class="literal">pgbench_accounts</code> table with
200         <em class="replaceable"><code>NUM</code></em> partitions of nearly equal size for
201         the scaled number of accounts.
202         Default is <code class="literal">0</code>, meaning no partitioning.
203        </p></dd><dt id="PGBENCH-OPTION-TABLESPACE"><span class="term"><code class="option">--tablespace=<em class="replaceable"><code>tablespace</code></em></code></span> <a href="#PGBENCH-OPTION-TABLESPACE" class="id_link">#</a></dt><dd><p>
204         Create tables in the specified tablespace, rather than the default
205         tablespace.
206        </p></dd><dt id="PGBENCH-OPTION-UNLOGGED-TABLES"><span class="term"><code class="option">--unlogged-tables</code></span> <a href="#PGBENCH-OPTION-UNLOGGED-TABLES" class="id_link">#</a></dt><dd><p>
207         Create all tables as unlogged tables, rather than permanent tables.
208        </p></dd></dl></div><p>
209    </p></div><div class="refsect2" id="PGBENCH-RUN-OPTIONS"><h3>Benchmarking Options</h3><p>
210     <span class="application">pgbench</span> accepts the following command-line
211     benchmarking arguments:
212
213     </p><div class="variablelist"><dl class="variablelist"><dt id="PGBENCH-OPTION-BUILTIN"><span class="term"><code class="option">-b</code> <em class="replaceable"><code>scriptname[@weight]</code></em><br /></span><span class="term"><code class="option">--builtin</code>=<em class="replaceable"><code>scriptname[@weight]</code></em></span> <a href="#PGBENCH-OPTION-BUILTIN" class="id_link">#</a></dt><dd><p>
214         Add the specified built-in script to the list of scripts to be executed.
215         Available built-in scripts are: <code class="literal">tpcb-like</code>,
216         <code class="literal">simple-update</code> and <code class="literal">select-only</code>.
217         Unambiguous prefixes of built-in names are accepted.
218         With the special name <code class="literal">list</code>, show the list of built-in scripts
219         and exit immediately.
220        </p><p>
221         Optionally, write an integer weight after <code class="literal">@</code> to
222         adjust the probability of selecting this script versus other ones.
223         The default weight is 1.
224         See below for details.
225        </p></dd><dt id="PGBENCH-OPTION-CLIENT"><span class="term"><code class="option">-c</code> <em class="replaceable"><code>clients</code></em><br /></span><span class="term"><code class="option">--client=</code><em class="replaceable"><code>clients</code></em></span> <a href="#PGBENCH-OPTION-CLIENT" class="id_link">#</a></dt><dd><p>
226         Number of clients simulated, that is, number of concurrent database
227         sessions.  Default is 1.
228        </p></dd><dt id="PGBENCH-OPTION-CONNECT"><span class="term"><code class="option">-C</code><br /></span><span class="term"><code class="option">--connect</code></span> <a href="#PGBENCH-OPTION-CONNECT" class="id_link">#</a></dt><dd><p>
229         Establish a new connection for each transaction, rather than
230         doing it just once per client session.
231         This is useful to measure the connection overhead.
232        </p></dd><dt id="PGBENCH-OPTION-DEFINE"><span class="term"><code class="option">-D</code> <em class="replaceable"><code>varname</code></em><code class="literal">=</code><em class="replaceable"><code>value</code></em><br /></span><span class="term"><code class="option">--define=</code><em class="replaceable"><code>varname</code></em><code class="literal">=</code><em class="replaceable"><code>value</code></em></span> <a href="#PGBENCH-OPTION-DEFINE" class="id_link">#</a></dt><dd><p>
233         Define a variable for use by a custom script (see below).
234         Multiple <code class="option">-D</code> options are allowed.
235        </p></dd><dt id="PGBENCH-OPTION-FILE"><span class="term"><code class="option">-f</code> <em class="replaceable"><code>filename[@weight]</code></em><br /></span><span class="term"><code class="option">--file=</code><em class="replaceable"><code>filename[@weight]</code></em></span> <a href="#PGBENCH-OPTION-FILE" class="id_link">#</a></dt><dd><p>
236         Add a transaction script read from <em class="replaceable"><code>filename</code></em>
237         to the list of scripts to be executed.
238        </p><p>
239         Optionally, write an integer weight after <code class="literal">@</code> to
240         adjust the probability of selecting this script versus other ones.
241         The default weight is 1.
242         (To use a script file name that includes an <code class="literal">@</code>
243         character, append a weight so that there is no ambiguity, for
244         example <code class="literal">filen@me@1</code>.)
245         See below for details.
246        </p></dd><dt id="PGBENCH-OPTION-JOBS"><span class="term"><code class="option">-j</code> <em class="replaceable"><code>threads</code></em><br /></span><span class="term"><code class="option">--jobs=</code><em class="replaceable"><code>threads</code></em></span> <a href="#PGBENCH-OPTION-JOBS" class="id_link">#</a></dt><dd><p>
247         Number of worker threads within <span class="application">pgbench</span>.
248         Using more than one thread can be helpful on multi-CPU machines.
249         Clients are distributed as evenly as possible among available threads.
250         Default is 1.
251        </p></dd><dt id="PGBENCH-OPTION-LOG"><span class="term"><code class="option">-l</code><br /></span><span class="term"><code class="option">--log</code></span> <a href="#PGBENCH-OPTION-LOG" class="id_link">#</a></dt><dd><p>
252         Write information about each transaction to a log file.
253         See below for details.
254        </p></dd><dt id="PGBENCH-OPTION-LATENCY-LIMIT"><span class="term"><code class="option">-L</code> <em class="replaceable"><code>limit</code></em><br /></span><span class="term"><code class="option">--latency-limit=</code><em class="replaceable"><code>limit</code></em></span> <a href="#PGBENCH-OPTION-LATENCY-LIMIT" class="id_link">#</a></dt><dd><p>
255         Transactions that last more than <em class="replaceable"><code>limit</code></em> milliseconds
256         are counted and reported separately, as <em class="firstterm">late</em>.
257        </p><p>
258         When throttling is used (<code class="option">--rate=...</code>), transactions that
259         lag behind schedule by more than <em class="replaceable"><code>limit</code></em> ms, and thus
260         have no hope of meeting the latency limit, are not sent to the server
261         at all. They are counted and reported separately as
262         <em class="firstterm">skipped</em>.
263        </p><p>
264         When the <code class="option">--max-tries</code> option is used, a transaction
265         which fails due to a serialization anomaly or from a deadlock will not
266         be retried if the total time of all its tries is greater than
267         <em class="replaceable"><code>limit</code></em> ms. To limit only the time of tries
268         and not their number, use <code class="literal">--max-tries=0</code>. By
269         default, the option <code class="option">--max-tries</code> is set to 1 and
270         transactions with serialization/deadlock errors are not retried. See
271         <a class="xref" href="pgbench.html#FAILURES-AND-RETRIES" title="Failures and Serialization/Deadlock Retries">Failures and Serialization/Deadlock Retries</a> for more information about
272         retrying such transactions.
273        </p></dd><dt id="PGBENCH-OPTION-PROTOCOL"><span class="term"><code class="option">-M</code> <em class="replaceable"><code>querymode</code></em><br /></span><span class="term"><code class="option">--protocol=</code><em class="replaceable"><code>querymode</code></em></span> <a href="#PGBENCH-OPTION-PROTOCOL" class="id_link">#</a></dt><dd><p>
274         Protocol to use for submitting queries to the server:
275           </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><code class="literal">simple</code>: use simple query protocol.</p></li><li class="listitem"><p><code class="literal">extended</code>: use extended query protocol.</p></li><li class="listitem"><p><code class="literal">prepared</code>: use extended query protocol with prepared statements.</p></li></ul></div><p>
276
277         In the <code class="literal">prepared</code> mode, <span class="application">pgbench</span>
278         reuses the parse analysis result starting from the second query
279         iteration, so <span class="application">pgbench</span> runs faster
280         than in other modes.
281        </p><p>
282         The default is simple query protocol.  (See <a class="xref" href="protocol.html" title="Chapter 54. Frontend/Backend Protocol">Chapter 54</a>
283         for more information.)
284        </p></dd><dt id="PGBENCH-OPTION-NO-VACUUM-RUN"><span class="term"><code class="option">-n</code><br /></span><span class="term"><code class="option">--no-vacuum</code></span> <a href="#PGBENCH-OPTION-NO-VACUUM-RUN" class="id_link">#</a></dt><dd><p>
285         Perform no vacuuming before running the test.
286         This option is <span class="emphasis"><em>necessary</em></span>
287         if you are running a custom test scenario that does not include
288         the standard tables <code class="structname">pgbench_accounts</code>,
289         <code class="structname">pgbench_branches</code>, <code class="structname">pgbench_history</code>, and
290         <code class="structname">pgbench_tellers</code>.
291        </p></dd><dt id="PGBENCH-OPTION-SKIP-SOME-UPDATES"><span class="term"><code class="option">-N</code><br /></span><span class="term"><code class="option">--skip-some-updates</code></span> <a href="#PGBENCH-OPTION-SKIP-SOME-UPDATES" class="id_link">#</a></dt><dd><p>
292         Run built-in simple-update script.
293         Shorthand for <code class="option">-b simple-update</code>.
294        </p></dd><dt id="PGBENCH-OPTION-PROGRESS"><span class="term"><code class="option">-P</code> <em class="replaceable"><code>sec</code></em><br /></span><span class="term"><code class="option">--progress=</code><em class="replaceable"><code>sec</code></em></span> <a href="#PGBENCH-OPTION-PROGRESS" class="id_link">#</a></dt><dd><p>
295         Show progress report every <em class="replaceable"><code>sec</code></em> seconds.  The report
296         includes the time since the beginning of the run, the TPS since the
297         last report, and the transaction latency average, standard deviation,
298         and the number of failed transactions since the last report. Under
299         throttling (<code class="option">-R</code>), the latency is computed with respect
300         to the transaction scheduled start time, not the actual transaction
301         beginning time, thus it also includes the average schedule lag time.
302         When <code class="option">--max-tries</code> is used to enable transaction retries
303         after serialization/deadlock errors, the report includes the number of
304         retried transactions and the sum of all retries.
305        </p></dd><dt id="PGBENCH-OPTION-REPORT-LATENCIES"><span class="term"><code class="option">-r</code><br /></span><span class="term"><code class="option">--report-per-command</code></span> <a href="#PGBENCH-OPTION-REPORT-LATENCIES" class="id_link">#</a></dt><dd><p>
306         Report the following statistics for each command after the benchmark
307         finishes: the average per-statement latency (execution time from the
308         perspective of the client), the number of failures, and the number of
309         retries after serialization or deadlock errors in this command.  The
310         report displays retry statistics only if the
311         <code class="option">--max-tries</code> option is not equal to 1.
312        </p></dd><dt id="PGBENCH-OPTION-RATE"><span class="term"><code class="option">-R</code> <em class="replaceable"><code>rate</code></em><br /></span><span class="term"><code class="option">--rate=</code><em class="replaceable"><code>rate</code></em></span> <a href="#PGBENCH-OPTION-RATE" class="id_link">#</a></dt><dd><p>
313         Execute transactions targeting the specified rate instead of running
314         as fast as possible (the default).  The rate is given in transactions
315         per second.  If the targeted rate is above the maximum possible rate,
316         the rate limit won't impact the results.
317        </p><p>
318         The rate is targeted by starting transactions along a
319         Poisson-distributed schedule time line.  The expected start time
320         schedule moves forward based on when the client first started, not
321         when the previous transaction ended.  That approach means that when
322         transactions go past their original scheduled end time, it is
323         possible for later ones to catch up again.
324        </p><p>
325         When throttling is active, the transaction latency reported at the
326         end of the run is calculated from the scheduled start times, so it
327         includes the time each transaction had to wait for the previous
328         transaction to finish. The wait time is called the schedule lag time,
329         and its average and maximum are also reported separately. The
330         transaction latency with respect to the actual transaction start time,
331         i.e., the time spent executing the transaction in the database, can be
332         computed by subtracting the schedule lag time from the reported
333         latency.
334        </p><p>
335         If <code class="option">--latency-limit</code> is used together with <code class="option">--rate</code>,
336         a transaction can lag behind so much that it is already over the
337         latency limit when the previous transaction ends, because the latency
338         is calculated from the scheduled start time. Such transactions are
339         not sent to the server, but are skipped altogether and counted
340         separately.
341        </p><p>
342         A high schedule lag time is an indication that the system cannot
343         process transactions at the specified rate, with the chosen number of
344         clients and threads. When the average transaction execution time is
345         longer than the scheduled interval between each transaction, each
346         successive transaction will fall further behind, and the schedule lag
347         time will keep increasing the longer the test run is. When that
348         happens, you will have to reduce the specified transaction rate.
349        </p></dd><dt id="PGBENCH-OPTION-SCALE-RUN"><span class="term"><code class="option">-s</code> <em class="replaceable"><code>scale_factor</code></em><br /></span><span class="term"><code class="option">--scale=</code><em class="replaceable"><code>scale_factor</code></em></span> <a href="#PGBENCH-OPTION-SCALE-RUN" class="id_link">#</a></dt><dd><p>
350         Report the specified scale factor in <span class="application">pgbench</span>'s
351         output.  With the built-in tests, this is not necessary; the
352         correct scale factor will be detected by counting the number of
353         rows in the <code class="structname">pgbench_branches</code> table.
354         However, when testing only custom benchmarks (<code class="option">-f</code> option),
355         the scale factor will be reported as 1 unless this option is used.
356        </p></dd><dt id="PGBENCH-OPTION-SELECT-ONLY"><span class="term"><code class="option">-S</code><br /></span><span class="term"><code class="option">--select-only</code></span> <a href="#PGBENCH-OPTION-SELECT-ONLY" class="id_link">#</a></dt><dd><p>
357         Run built-in select-only script.
358         Shorthand for <code class="option">-b select-only</code>.
359        </p></dd><dt id="PGBENCH-OPTION-TRANSACTIONS"><span class="term"><code class="option">-t</code> <em class="replaceable"><code>transactions</code></em><br /></span><span class="term"><code class="option">--transactions=</code><em class="replaceable"><code>transactions</code></em></span> <a href="#PGBENCH-OPTION-TRANSACTIONS" class="id_link">#</a></dt><dd><p>
360         Number of transactions each client runs.  Default is 10.
361        </p></dd><dt id="PGBENCH-OPTION-TIME"><span class="term"><code class="option">-T</code> <em class="replaceable"><code>seconds</code></em><br /></span><span class="term"><code class="option">--time=</code><em class="replaceable"><code>seconds</code></em></span> <a href="#PGBENCH-OPTION-TIME" class="id_link">#</a></dt><dd><p>
362         Run the test for this many seconds, rather than a fixed number of
363         transactions per client. <code class="option">-t</code> and
364         <code class="option">-T</code> are mutually exclusive.
365        </p></dd><dt id="PGBENCH-OPTION-VACUUM-ALL"><span class="term"><code class="option">-v</code><br /></span><span class="term"><code class="option">--vacuum-all</code></span> <a href="#PGBENCH-OPTION-VACUUM-ALL" class="id_link">#</a></dt><dd><p>
366         Vacuum all four standard tables before running the test.
367         With neither <code class="option">-n</code> nor <code class="option">-v</code>, <span class="application">pgbench</span> will vacuum the
368         <code class="structname">pgbench_tellers</code> and <code class="structname">pgbench_branches</code>
369         tables, and will truncate <code class="structname">pgbench_history</code>.
370        </p></dd><dt id="PGBENCH-OPTION-AGGREGATE-INTERVAL"><span class="term"><code class="option">--aggregate-interval=<em class="replaceable"><code>seconds</code></em></code></span> <a href="#PGBENCH-OPTION-AGGREGATE-INTERVAL" class="id_link">#</a></dt><dd><p>
371         Length of aggregation interval (in seconds).  May be used only
372         with <code class="option">-l</code> option.  With this option, the log contains
373         per-interval summary data, as described below.
374        </p></dd><dt id="PGBENCH-OPTION-EXIT-ON-ABORT"><span class="term"><code class="option">--exit-on-abort</code></span> <a href="#PGBENCH-OPTION-EXIT-ON-ABORT" class="id_link">#</a></dt><dd><p>
375         Exit immediately when any client is aborted due to some error. Without
376         this option, even when a client is aborted, other clients could
377         continue their run as specified by <code class="option">-t</code>
378         or <code class="option">-T</code> option, and <span class="application">pgbench</span>
379         will print an incomplete results in this case.
380        </p><p>
381         Note that serialization failures or deadlock failures do not abort the
382         client, so they are not affected by this option.
383         See <a class="xref" href="pgbench.html#FAILURES-AND-RETRIES" title="Failures and Serialization/Deadlock Retries">Failures and Serialization/Deadlock Retries</a> for more information.
384        </p></dd><dt id="PGBENCH-OPTION-FAILURES-DETAILED"><span class="term"><code class="option">--failures-detailed</code></span> <a href="#PGBENCH-OPTION-FAILURES-DETAILED" class="id_link">#</a></dt><dd><p>
385         Report failures in per-transaction and aggregation logs, as well as in
386         the main and per-script reports, grouped by the following types:
387         </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>serialization failures;</p></li><li class="listitem"><p>deadlock failures;</p></li></ul></div><p>
388         See <a class="xref" href="pgbench.html#FAILURES-AND-RETRIES" title="Failures and Serialization/Deadlock Retries">Failures and Serialization/Deadlock Retries</a> for more information.
389        </p></dd><dt id="PGBENCH-OPTION-LOG-PREFIX"><span class="term"><code class="option">--log-prefix=<em class="replaceable"><code>prefix</code></em></code></span> <a href="#PGBENCH-OPTION-LOG-PREFIX" class="id_link">#</a></dt><dd><p>
390         Set the filename prefix for the log files created by
391         <code class="option">--log</code>.  The default is <code class="literal">pgbench_log</code>.
392        </p></dd><dt id="PGBENCH-OPTION-MAX-TRIES"><span class="term"><code class="option">--max-tries=<em class="replaceable"><code>number_of_tries</code></em></code></span> <a href="#PGBENCH-OPTION-MAX-TRIES" class="id_link">#</a></dt><dd><p>
393         Enable retries for transactions with serialization/deadlock errors and
394         set the maximum number of these tries. This option can be combined with
395         the <code class="option">--latency-limit</code> option which limits the total time
396         of all transaction tries; moreover, you cannot use an unlimited number
397         of tries (<code class="literal">--max-tries=0</code>) without
398         <code class="option">--latency-limit</code> or <code class="option">--time</code>.
399         The default value is 1 and transactions with serialization/deadlock
400         errors are not retried. See <a class="xref" href="pgbench.html#FAILURES-AND-RETRIES" title="Failures and Serialization/Deadlock Retries">Failures and Serialization/Deadlock Retries</a>
401         for more information about retrying such transactions.
402        </p></dd><dt id="PGBENCH-OPTION-PROGRESS-TIMESTAMP"><span class="term"><code class="option">--progress-timestamp</code></span> <a href="#PGBENCH-OPTION-PROGRESS-TIMESTAMP" class="id_link">#</a></dt><dd><p>
403         When showing progress (option <code class="option">-P</code>), use a timestamp
404         (Unix epoch) instead of the number of seconds since the
405         beginning of the run.  The unit is in seconds, with millisecond
406         precision after the dot.
407         This helps compare logs generated by various tools.
408        </p></dd><dt id="PGBENCH-OPTION-RANDOM-SEED"><span class="term"><code class="option">--random-seed=</code><em class="replaceable"><code>seed</code></em></span> <a href="#PGBENCH-OPTION-RANDOM-SEED" class="id_link">#</a></dt><dd><p>
409         Set random generator seed.  Seeds the system random number generator,
410         which then produces a sequence of initial generator states, one for
411         each thread.
412         Values for <em class="replaceable"><code>seed</code></em> may be:
413         <code class="literal">time</code> (the default, the seed is based on the current time),
414         <code class="literal">rand</code> (use a strong random source, failing if none
415         is available), or an unsigned decimal integer value.
416         The random generator is invoked explicitly from a pgbench script
417         (<code class="literal">random...</code> functions) or implicitly (for instance option
418         <code class="option">--rate</code> uses it to schedule transactions).
419         When explicitly set, the value used for seeding is shown on the terminal.
420         Any value allowed for <em class="replaceable"><code>seed</code></em> may also be
421         provided through the environment variable
422         <code class="literal">PGBENCH_RANDOM_SEED</code>.
423         To ensure that the provided seed impacts all possible uses, put this option
424         first or use the environment variable.
425       </p><p>
426         Setting the seed explicitly allows to reproduce a <code class="command">pgbench</code>
427         run exactly, as far as random numbers are concerned.
428         As the random state is managed per thread, this means the exact same
429         <code class="command">pgbench</code> run for an identical invocation if there is one
430         client per thread and there are no external or data dependencies.
431         From a statistical viewpoint reproducing runs exactly is a bad idea because
432         it can hide the performance variability or improve performance unduly,
433         e.g., by hitting the same pages as a previous run.
434         However, it may also be of great help for debugging, for instance
435         re-running a tricky case which leads to an error.
436         Use wisely.
437        </p></dd><dt id="PGBENCH-OPTION-SAMPLING-RATE"><span class="term"><code class="option">--sampling-rate=<em class="replaceable"><code>rate</code></em></code></span> <a href="#PGBENCH-OPTION-SAMPLING-RATE" class="id_link">#</a></dt><dd><p>
438         Sampling rate, used when writing data into the log, to reduce the
439         amount of log generated. If this option is given, only the specified
440         fraction of transactions are logged. 1.0 means all transactions will
441         be logged, 0.05 means only 5% of the transactions will be logged.
442        </p><p>
443         Remember to take the sampling rate into account when processing the
444         log file. For example, when computing TPS values, you need to multiply
445         the numbers accordingly (e.g., with 0.01 sample rate, you'll only get
446         1/100 of the actual TPS).
447        </p></dd><dt id="PGBENCH-OPTION-SHOW-SCRIPT"><span class="term"><code class="option">--show-script=</code><em class="replaceable"><code>scriptname</code></em></span> <a href="#PGBENCH-OPTION-SHOW-SCRIPT" class="id_link">#</a></dt><dd><p>
448         Show the actual code of builtin script <em class="replaceable"><code>scriptname</code></em>
449         on stderr, and exit immediately.
450        </p></dd><dt id="PGBENCH-OPTION-VERBOSE-ERRORS"><span class="term"><code class="option">--verbose-errors</code></span> <a href="#PGBENCH-OPTION-VERBOSE-ERRORS" class="id_link">#</a></dt><dd><p>
451         Print messages about all errors and failures (errors without retrying)
452         including which limit for retries was exceeded and how far it was
453         exceeded for the serialization/deadlock failures. (Note that in this
454         case the output can be significantly increased.)
455         See <a class="xref" href="pgbench.html#FAILURES-AND-RETRIES" title="Failures and Serialization/Deadlock Retries">Failures and Serialization/Deadlock Retries</a> for more information.
456        </p></dd></dl></div><p>
457    </p></div><div class="refsect2" id="PGBENCH-COMMON-OPTIONS"><h3>Common Options</h3><p>
458     <span class="application">pgbench</span> also accepts the following common command-line
459     arguments for connection parameters and other common settings:
460
461     </p><div class="variablelist"><dl class="variablelist"><dt id="PGBENCH-OPTION-DEBUG"><span class="term"><code class="option">--debug</code></span> <a href="#PGBENCH-OPTION-DEBUG" class="id_link">#</a></dt><dd><p>
462         Print debugging output.
463        </p></dd><dt id="PGBENCH-OPTION-HOST"><span class="term"><code class="option">-h</code> <em class="replaceable"><code>hostname</code></em><br /></span><span class="term"><code class="option">--host=</code><em class="replaceable"><code>hostname</code></em></span> <a href="#PGBENCH-OPTION-HOST" class="id_link">#</a></dt><dd><p>
464         The database server's host name
465        </p></dd><dt id="PGBENCH-OPTION-PORT"><span class="term"><code class="option">-p</code> <em class="replaceable"><code>port</code></em><br /></span><span class="term"><code class="option">--port=</code><em class="replaceable"><code>port</code></em></span> <a href="#PGBENCH-OPTION-PORT" class="id_link">#</a></dt><dd><p>
466         The database server's port number
467        </p></dd><dt id="PGBENCH-OPTION-USERNAME"><span class="term"><code class="option">-U</code> <em class="replaceable"><code>login</code></em><br /></span><span class="term"><code class="option">--username=</code><em class="replaceable"><code>login</code></em></span> <a href="#PGBENCH-OPTION-USERNAME" class="id_link">#</a></dt><dd><p>
468         The user name to connect as
469        </p></dd><dt id="PGBENCH-OPTION-VERSION"><span class="term"><code class="option">-V</code><br /></span><span class="term"><code class="option">--version</code></span> <a href="#PGBENCH-OPTION-VERSION" class="id_link">#</a></dt><dd><p>
470         Print the <span class="application">pgbench</span> version and exit.
471        </p></dd><dt id="PGBENCH-OPTION-HELP"><span class="term"><code class="option">-?</code><br /></span><span class="term"><code class="option">--help</code></span> <a href="#PGBENCH-OPTION-HELP" class="id_link">#</a></dt><dd><p>
472         Show help about <span class="application">pgbench</span> command line
473         arguments, and exit.
474        </p></dd></dl></div><p>
475    </p></div></div><div class="refsect1" id="id-1.9.4.11.7"><h2>Exit Status</h2><p>
476    A successful run will exit with status 0.  Exit status 1 indicates static
477    problems such as invalid command-line options or internal errors which
478    are supposed to never occur.  Early errors that occur when starting
479    benchmark such as initial connection failures also exit with status 1.
480    Errors during the run such as database errors or problems in the script
481    will result in exit status 2. In the latter case,
482    <span class="application">pgbench</span> will print partial results if
483    <code class="option">--exit-on-abort</code> option is not specified.
484   </p></div><div class="refsect1" id="id-1.9.4.11.8"><h2>Environment</h2><div class="variablelist"><dl class="variablelist"><dt id="PGBENCH-ENVIRONMENT-PGDATABASE"><span class="term"><code class="envar">PGDATABASE</code><br /></span><span class="term"><code class="envar">PGHOST</code><br /></span><span class="term"><code class="envar">PGPORT</code><br /></span><span class="term"><code class="envar">PGUSER</code></span> <a href="#PGBENCH-ENVIRONMENT-PGDATABASE" class="id_link">#</a></dt><dd><p>
485       Default connection parameters.
486      </p></dd></dl></div><p>
487    This utility, like most other <span class="productname">PostgreSQL</span> utilities,
488    uses the environment variables supported by <span class="application">libpq</span>
489    (see <a class="xref" href="libpq-envars.html" title="32.15. Environment Variables">Section 32.15</a>).
490   </p><p>
491    The environment variable <code class="envar">PG_COLOR</code> specifies whether to use
492    color in diagnostic messages. Possible values are
493    <code class="literal">always</code>, <code class="literal">auto</code> and
494    <code class="literal">never</code>.
495   </p></div><div class="refsect1" id="id-1.9.4.11.9"><h2>Notes</h2><div class="refsect2" id="TRANSACTIONS-AND-SCRIPTS"><h3>What Is the <span class="quote">“<span class="quote">Transaction</span>”</span> Actually Performed in <span class="application">pgbench</span>?</h3><p>
496    <span class="application">pgbench</span> executes test scripts chosen randomly
497    from a specified list.
498    The scripts may include built-in scripts specified with <code class="option">-b</code>
499    and user-provided scripts specified with <code class="option">-f</code>.
500    Each script may be given a relative weight specified after an
501    <code class="literal">@</code> so as to change its selection probability.
502    The default weight is <code class="literal">1</code>.
503    Scripts with a weight of <code class="literal">0</code> are ignored.
504  </p><p>
505    The default built-in transaction script (also invoked with <code class="option">-b tpcb-like</code>)
506    issues seven commands per transaction over randomly chosen <code class="literal">aid</code>,
507    <code class="literal">tid</code>, <code class="literal">bid</code> and <code class="literal">delta</code>.
508    The scenario is inspired by the TPC-B benchmark, but is not actually TPC-B,
509    hence the name.
510   </p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p><code class="literal">BEGIN;</code></p></li><li class="listitem"><p><code class="literal">UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;</code></p></li><li class="listitem"><p><code class="literal">SELECT abalance FROM pgbench_accounts WHERE aid = :aid;</code></p></li><li class="listitem"><p><code class="literal">UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;</code></p></li><li class="listitem"><p><code class="literal">UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;</code></p></li><li class="listitem"><p><code class="literal">INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);</code></p></li><li class="listitem"><p><code class="literal">END;</code></p></li></ol></div><p>
511    If you select the <code class="literal">simple-update</code> built-in (also <code class="option">-N</code>),
512    steps 4 and 5 aren't included in the transaction.
513    This will avoid update contention on these tables, but
514    it makes the test case even less like TPC-B.
515   </p><p>
516    If you select the <code class="literal">select-only</code> built-in (also <code class="option">-S</code>),
517    only the <code class="command">SELECT</code> is issued.
518   </p></div><div class="refsect2" id="id-1.9.4.11.9.3"><h3>Custom Scripts</h3><p>
519    <span class="application">pgbench</span> has support for running custom
520    benchmark scenarios by replacing the default transaction script
521    (described above) with a transaction script read from a file
522    (<code class="option">-f</code> option).  In this case a <span class="quote">“<span class="quote">transaction</span>”</span>
523    counts as one execution of a script file.
524   </p><p>
525    A script file contains one or more SQL commands terminated by
526    semicolons.  Empty lines and lines beginning with
527    <code class="literal">--</code> are ignored.  Script files can also contain
528    <span class="quote">“<span class="quote">meta commands</span>”</span>, which are interpreted by <span class="application">pgbench</span>
529    itself, as described below.
530   </p><div class="note"><h3 class="title">Note</h3><p>
531     Before <span class="productname">PostgreSQL</span> 9.6, SQL commands in script files
532     were terminated by newlines, and so they could not be continued across
533     lines.  Now a semicolon is <span class="emphasis"><em>required</em></span> to separate consecutive
534     SQL commands (though an SQL command does not need one if it is followed
535     by a meta command).  If you need to create a script file that works with
536     both old and new versions of <span class="application">pgbench</span>, be sure to write
537     each SQL command on a single line ending with a semicolon.
538    </p><p>
539     It is assumed that <span class="application">pgbench</span> scripts do not contain
540     incomplete blocks of SQL
541     transactions. If at runtime the client reaches the end of the script without
542     completing the last transaction block, it will be aborted.
543    </p></div><p>
544    There is a simple variable-substitution facility for script files.
545    Variable names must consist of letters (including non-Latin letters),
546    digits, and underscores, with the first character not being a digit.
547    Variables can be set by the command-line <code class="option">-D</code> option,
548    explained above, or by the meta commands explained below.
549    In addition to any variables preset by <code class="option">-D</code> command-line options,
550    there are a few variables that are preset automatically, listed in
551    <a class="xref" href="pgbench.html#PGBENCH-AUTOMATIC-VARIABLES" title="Table 301. pgbench Automatic Variables">Table 301</a>. A value specified for these
552    variables using <code class="option">-D</code> takes precedence over the automatic presets.
553    Once set, a variable's
554    value can be inserted into an SQL command by writing
555    <code class="literal">:</code><em class="replaceable"><code>variablename</code></em>.  When running more than
556    one client session, each session has its own set of variables.
557    <span class="application">pgbench</span> supports up to 255 variable uses in one
558    statement.
559   </p><div class="table" id="PGBENCH-AUTOMATIC-VARIABLES"><p class="title"><strong>Table 301. pgbench Automatic Variables</strong></p><div class="table-contents"><table class="table" summary="pgbench Automatic Variables" border="1"><colgroup><col class="col1" /><col class="col2" /></colgroup><thead><tr><th>Variable</th><th>Description</th></tr></thead><tbody><tr><td> <code class="literal">client_id</code> </td><td>unique number identifying the client session (starts from zero)</td></tr><tr><td> <code class="literal">default_seed</code> </td><td>seed used in hash and pseudorandom permutation functions by default</td></tr><tr><td> <code class="literal">random_seed</code> </td><td>random generator seed (unless overwritten with <code class="option">-D</code>)</td></tr><tr><td> <code class="literal">scale</code> </td><td>current scale factor</td></tr></tbody></table></div></div><br class="table-break" /><p>
560    Script file meta commands begin with a backslash (<code class="literal">\</code>) and
561    normally extend to the end of the line, although they can be continued
562    to additional lines by writing backslash-return.
563    Arguments to a meta command are separated by white space.
564    These meta commands are supported:
565   </p><div class="variablelist"><dl class="variablelist"><dt id="PGBENCH-METACOMMAND-GSET"><span class="term">
566      <code class="literal">\gset [<em class="replaceable"><code>prefix</code></em>]</code>
567      <code class="literal">\aset [<em class="replaceable"><code>prefix</code></em>]</code>
568     </span> <a href="#PGBENCH-METACOMMAND-GSET" class="id_link">#</a></dt><dd><p>
569       These commands may be used to end SQL queries, taking the place of the
570       terminating semicolon (<code class="literal">;</code>).
571      </p><p>
572       When the <code class="literal">\gset</code> command is used, the preceding SQL query is
573       expected to return one row, the columns of which are stored into variables
574       named after column names, and prefixed with <em class="replaceable"><code>prefix</code></em>
575       if provided.
576      </p><p>
577       When the <code class="literal">\aset</code> command is used, all combined SQL queries
578       (separated by <code class="literal">\;</code>) have their columns stored into variables
579       named after column names, and prefixed with <em class="replaceable"><code>prefix</code></em>
580       if provided. If a query returns no row, no assignment is made and the variable
581       can be tested for existence to detect this. If a query returns more than one
582       row, the last value is kept.
583      </p><p>
584       <code class="literal">\gset</code> and <code class="literal">\aset</code> cannot be used in
585       pipeline mode, since the query results are not yet available by the time
586       the commands would need them.
587      </p><p>
588       The following example puts the final account balance from the first query
589       into variable <em class="replaceable"><code>abalance</code></em>, and fills variables
590       <em class="replaceable"><code>p_two</code></em> and <em class="replaceable"><code>p_three</code></em>
591       with integers from the third query.
592       The result of the second query is discarded.
593       The result of the two last combined queries are stored in variables
594       <em class="replaceable"><code>four</code></em> and <em class="replaceable"><code>five</code></em>.
595 </p><pre class="programlisting">
596 UPDATE pgbench_accounts
597   SET abalance = abalance + :delta
598   WHERE aid = :aid
599   RETURNING abalance \gset
600 -- compound of two queries
601 SELECT 1 \;
602 SELECT 2 AS two, 3 AS three \gset p_
603 SELECT 4 AS four \; SELECT 5 AS five \aset
604 </pre></dd><dt id="PGBENCH-METACOMMAND-IF-ELSE"><span class="term"><code class="literal">\if</code> <em class="replaceable"><code>expression</code></em><br /></span><span class="term"><code class="literal">\elif</code> <em class="replaceable"><code>expression</code></em><br /></span><span class="term"><code class="literal">\else</code><br /></span><span class="term"><code class="literal">\endif</code></span> <a href="#PGBENCH-METACOMMAND-IF-ELSE" class="id_link">#</a></dt><dd><p>
605       This group of commands implements nestable conditional blocks,
606       similarly to <code class="literal">psql</code>'s <a class="xref" href="app-psql.html#PSQL-METACOMMAND-IF"><code class="literal">\if</code> <em class="replaceable"><code>expression</code></em></a>.
607       Conditional expressions are identical to those with <code class="literal">\set</code>,
608       with non-zero values interpreted as true.
609      </p></dd><dt id="PGBENCH-METACOMMAND-SET"><span class="term">
610      <code class="literal">\set <em class="replaceable"><code>varname</code></em> <em class="replaceable"><code>expression</code></em></code>
611     </span> <a href="#PGBENCH-METACOMMAND-SET" class="id_link">#</a></dt><dd><p>
612       Sets variable <em class="replaceable"><code>varname</code></em> to a value calculated
613       from <em class="replaceable"><code>expression</code></em>.
614       The expression may contain the <code class="literal">NULL</code> constant,
615       Boolean constants <code class="literal">TRUE</code> and <code class="literal">FALSE</code>,
616       integer constants such as <code class="literal">5432</code>,
617       double constants such as <code class="literal">3.14159</code>,
618       references to variables <code class="literal">:</code><em class="replaceable"><code>variablename</code></em>,
619       <a class="link" href="pgbench.html#PGBENCH-BUILTIN-OPERATORS" title="Built-in Operators">operators</a>
620       with their usual SQL precedence and associativity,
621       <a class="link" href="pgbench.html#PGBENCH-BUILTIN-FUNCTIONS" title="Built-In Functions">function calls</a>,
622       SQL <a class="link" href="functions-conditional.html#FUNCTIONS-CASE" title="9.18.1. CASE"><code class="token">CASE</code> generic conditional
623       expressions</a> and parentheses.
624      </p><p>
625       Functions and most operators return <code class="literal">NULL</code> on
626       <code class="literal">NULL</code> input.
627      </p><p>
628       For conditional purposes, non zero numerical values are
629       <code class="literal">TRUE</code>, zero numerical values and <code class="literal">NULL</code>
630       are <code class="literal">FALSE</code>.
631      </p><p>
632       Too large or small integer and double constants, as well as
633       integer arithmetic operators (<code class="literal">+</code>,
634       <code class="literal">-</code>, <code class="literal">*</code> and <code class="literal">/</code>)
635       raise errors on overflows.
636      </p><p>
637       When no final <code class="token">ELSE</code> clause is provided to a
638       <code class="token">CASE</code>, the default value is <code class="literal">NULL</code>.
639      </p><p>
640       Examples:
641 </p><pre class="programlisting">
642 \set ntellers 10 * :scale
643 \set aid (1021 * random(1, 100000 * :scale)) % \
644            (100000 * :scale) + 1
645 \set divx CASE WHEN :x &lt;&gt; 0 THEN :y/:x ELSE NULL END
646 </pre></dd><dt id="PGBENCH-METACOMMAND-SLEEP"><span class="term">
647      <code class="literal">\sleep <em class="replaceable"><code>number</code></em> [ us | ms | s ]</code>
648     </span> <a href="#PGBENCH-METACOMMAND-SLEEP" class="id_link">#</a></dt><dd><p>
649       Causes script execution to sleep for the specified duration in
650       microseconds (<code class="literal">us</code>), milliseconds (<code class="literal">ms</code>) or seconds
651       (<code class="literal">s</code>).  If the unit is omitted then seconds are the default.
652       <em class="replaceable"><code>number</code></em> can be either an integer constant or a
653       <code class="literal">:</code><em class="replaceable"><code>variablename</code></em> reference to a variable
654       having an integer value.
655      </p><p>
656       Example:
657 </p><pre class="programlisting">
658 \sleep 10 ms
659 </pre></dd><dt id="PGBENCH-METACOMMAND-SETSHELL"><span class="term">
660      <code class="literal">\setshell <em class="replaceable"><code>varname</code></em> <em class="replaceable"><code>command</code></em> [ <em class="replaceable"><code>argument</code></em> ... ]</code>
661     </span> <a href="#PGBENCH-METACOMMAND-SETSHELL" class="id_link">#</a></dt><dd><p>
662       Sets variable <em class="replaceable"><code>varname</code></em> to the result of the shell command
663       <em class="replaceable"><code>command</code></em> with the given <em class="replaceable"><code>argument</code></em>(s).
664       The command must return an integer value through its standard output.
665      </p><p>
666       <em class="replaceable"><code>command</code></em> and each <em class="replaceable"><code>argument</code></em> can be either
667       a text constant or a <code class="literal">:</code><em class="replaceable"><code>variablename</code></em> reference
668       to a variable. If you want to use an <em class="replaceable"><code>argument</code></em> starting
669       with a colon, write an additional colon at the beginning of
670       <em class="replaceable"><code>argument</code></em>.
671      </p><p>
672       Example:
673 </p><pre class="programlisting">
674 \setshell variable_to_be_assigned command literal_argument :variable ::literal_starting_with_colon
675 </pre></dd><dt id="PGBENCH-METACOMMAND-SHELL"><span class="term">
676      <code class="literal">\shell <em class="replaceable"><code>command</code></em> [ <em class="replaceable"><code>argument</code></em> ... ]</code>
677     </span> <a href="#PGBENCH-METACOMMAND-SHELL" class="id_link">#</a></dt><dd><p>
678       Same as <code class="literal">\setshell</code>, but the result of the command
679       is discarded.
680      </p><p>
681       Example:
682 </p><pre class="programlisting">
683 \shell command literal_argument :variable ::literal_starting_with_colon
684 </pre></dd><dt id="PGBENCH-METACOMMAND-PIPELINE"><span class="term"><code class="literal">\startpipeline</code><br /></span><span class="term"><code class="literal">\syncpipeline</code><br /></span><span class="term"><code class="literal">\endpipeline</code></span> <a href="#PGBENCH-METACOMMAND-PIPELINE" class="id_link">#</a></dt><dd><p>
685         This group of commands implements pipelining of SQL statements.
686         A pipeline must begin with a <code class="command">\startpipeline</code>
687         and end with an <code class="command">\endpipeline</code>. In between there
688         may be any number of <code class="command">\syncpipeline</code> commands,
689         which sends a <a class="link" href="protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY" title="54.2.3. Extended Query">sync message</a>
690         without ending the ongoing pipeline and flushing the send buffer.
691         In pipeline mode, statements are sent to the server without waiting
692         for the results of previous statements.  See
693         <a class="xref" href="libpq-pipeline-mode.html" title="32.5. Pipeline Mode">Section 32.5</a> for more details.
694         Pipeline mode requires the use of extended query protocol.
695      </p></dd></dl></div></div><div class="refsect2" id="PGBENCH-BUILTIN-OPERATORS"><h3>Built-in Operators</h3><p>
696    The arithmetic, bitwise, comparison and logical operators listed in
697    <a class="xref" href="pgbench.html#PGBENCH-OPERATORS" title="Table 302. pgbench Operators">Table 302</a> are built into <span class="application">pgbench</span>
698    and may be used in expressions appearing in
699    <a class="link" href="pgbench.html#PGBENCH-METACOMMAND-SET"><code class="literal">\set</code></a>.
700    The operators are listed in increasing precedence order.
701    Except as noted, operators taking two numeric inputs will produce
702    a double value if either input is double, otherwise they produce
703    an integer result.
704   </p><div class="table" id="PGBENCH-OPERATORS"><p class="title"><strong>Table 302. pgbench Operators</strong></p><div class="table-contents"><table class="table" summary="pgbench Operators" border="1"><colgroup><col /></colgroup><thead><tr><th class="func_table_entry"><p class="func_signature">
705         Operator
706        </p>
707        <p>
708         Description
709        </p>
710        <p>
711         Example(s)
712        </p></th></tr></thead><tbody><tr><td class="func_table_entry"><p class="func_signature">
713         <em class="replaceable"><code>boolean</code></em> <code class="literal">OR</code> <em class="replaceable"><code>boolean</code></em>
714         → <code class="returnvalue"><em class="replaceable"><code>boolean</code></em></code>
715        </p>
716        <p>
717         Logical OR
718        </p>
719        <p>
720         <code class="literal">5 or 0</code>
721         → <code class="returnvalue">TRUE</code>
722        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
723         <em class="replaceable"><code>boolean</code></em> <code class="literal">AND</code> <em class="replaceable"><code>boolean</code></em>
724         → <code class="returnvalue"><em class="replaceable"><code>boolean</code></em></code>
725        </p>
726        <p>
727         Logical AND
728        </p>
729        <p>
730         <code class="literal">3 and 0</code>
731         → <code class="returnvalue">FALSE</code>
732        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
733         <code class="literal">NOT</code> <em class="replaceable"><code>boolean</code></em>
734         → <code class="returnvalue"><em class="replaceable"><code>boolean</code></em></code>
735        </p>
736        <p>
737         Logical NOT
738        </p>
739        <p>
740         <code class="literal">not false</code>
741         → <code class="returnvalue">TRUE</code>
742        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
743         <em class="replaceable"><code>boolean</code></em> <code class="literal">IS [NOT] (NULL|TRUE|FALSE)</code>
744         → <code class="returnvalue"><em class="replaceable"><code>boolean</code></em></code>
745        </p>
746        <p>
747         Boolean value tests
748        </p>
749        <p>
750         <code class="literal">1 is null</code>
751         → <code class="returnvalue">FALSE</code>
752        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
753         <em class="replaceable"><code>value</code></em> <code class="literal">ISNULL|NOTNULL</code>
754         → <code class="returnvalue"><em class="replaceable"><code>boolean</code></em></code>
755        </p>
756        <p>
757         Nullness tests
758        </p>
759        <p>
760         <code class="literal">1 notnull</code>
761         → <code class="returnvalue">TRUE</code>
762        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
763         <em class="replaceable"><code>number</code></em> <code class="literal">=</code> <em class="replaceable"><code>number</code></em>
764         → <code class="returnvalue"><em class="replaceable"><code>boolean</code></em></code>
765        </p>
766        <p>
767         Equal
768        </p>
769        <p>
770         <code class="literal">5 = 4</code>
771         → <code class="returnvalue">FALSE</code>
772        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
773         <em class="replaceable"><code>number</code></em> <code class="literal">&lt;&gt;</code> <em class="replaceable"><code>number</code></em>
774         → <code class="returnvalue"><em class="replaceable"><code>boolean</code></em></code>
775        </p>
776        <p>
777         Not equal
778        </p>
779        <p>
780         <code class="literal">5 &lt;&gt; 4</code>
781         → <code class="returnvalue">TRUE</code>
782        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
783         <em class="replaceable"><code>number</code></em> <code class="literal">!=</code> <em class="replaceable"><code>number</code></em>
784         → <code class="returnvalue"><em class="replaceable"><code>boolean</code></em></code>
785        </p>
786        <p>
787         Not equal
788        </p>
789        <p>
790         <code class="literal">5 != 5</code>
791         → <code class="returnvalue">FALSE</code>
792        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
793         <em class="replaceable"><code>number</code></em> <code class="literal">&lt;</code> <em class="replaceable"><code>number</code></em>
794         → <code class="returnvalue"><em class="replaceable"><code>boolean</code></em></code>
795        </p>
796        <p>
797         Less than
798        </p>
799        <p>
800         <code class="literal">5 &lt; 4</code>
801         → <code class="returnvalue">FALSE</code>
802        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
803         <em class="replaceable"><code>number</code></em> <code class="literal">&lt;=</code> <em class="replaceable"><code>number</code></em>
804         → <code class="returnvalue"><em class="replaceable"><code>boolean</code></em></code>
805        </p>
806        <p>
807         Less than or equal to
808        </p>
809        <p>
810         <code class="literal">5 &lt;= 4</code>
811         → <code class="returnvalue">FALSE</code>
812        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
813         <em class="replaceable"><code>number</code></em> <code class="literal">&gt;</code> <em class="replaceable"><code>number</code></em>
814         → <code class="returnvalue"><em class="replaceable"><code>boolean</code></em></code>
815        </p>
816        <p>
817         Greater than
818        </p>
819        <p>
820         <code class="literal">5 &gt; 4</code>
821         → <code class="returnvalue">TRUE</code>
822        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
823         <em class="replaceable"><code>number</code></em> <code class="literal">&gt;=</code> <em class="replaceable"><code>number</code></em>
824         → <code class="returnvalue"><em class="replaceable"><code>boolean</code></em></code>
825        </p>
826        <p>
827         Greater than or equal to
828        </p>
829        <p>
830         <code class="literal">5 &gt;= 4</code>
831         → <code class="returnvalue">TRUE</code>
832        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
833         <em class="replaceable"><code>integer</code></em> <code class="literal">|</code> <em class="replaceable"><code>integer</code></em>
834         → <code class="returnvalue"><em class="replaceable"><code>integer</code></em></code>
835        </p>
836        <p>
837         Bitwise OR
838        </p>
839        <p>
840         <code class="literal">1 | 2</code>
841         → <code class="returnvalue">3</code>
842        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
843         <em class="replaceable"><code>integer</code></em> <code class="literal">#</code> <em class="replaceable"><code>integer</code></em>
844         → <code class="returnvalue"><em class="replaceable"><code>integer</code></em></code>
845        </p>
846        <p>
847         Bitwise XOR
848        </p>
849        <p>
850         <code class="literal">1 # 3</code>
851         → <code class="returnvalue">2</code>
852        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
853         <em class="replaceable"><code>integer</code></em> <code class="literal">&amp;</code> <em class="replaceable"><code>integer</code></em>
854         → <code class="returnvalue"><em class="replaceable"><code>integer</code></em></code>
855        </p>
856        <p>
857         Bitwise AND
858        </p>
859        <p>
860         <code class="literal">1 &amp; 3</code>
861         → <code class="returnvalue">1</code>
862        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
863         <code class="literal">~</code> <em class="replaceable"><code>integer</code></em>
864         → <code class="returnvalue"><em class="replaceable"><code>integer</code></em></code>
865        </p>
866        <p>
867         Bitwise NOT
868        </p>
869        <p>
870         <code class="literal">~ 1</code>
871         → <code class="returnvalue">-2</code>
872        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
873         <em class="replaceable"><code>integer</code></em> <code class="literal">&lt;&lt;</code> <em class="replaceable"><code>integer</code></em>
874         → <code class="returnvalue"><em class="replaceable"><code>integer</code></em></code>
875        </p>
876        <p>
877         Bitwise shift left
878        </p>
879        <p>
880         <code class="literal">1 &lt;&lt; 2</code>
881         → <code class="returnvalue">4</code>
882        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
883         <em class="replaceable"><code>integer</code></em> <code class="literal">&gt;&gt;</code> <em class="replaceable"><code>integer</code></em>
884         → <code class="returnvalue"><em class="replaceable"><code>integer</code></em></code>
885        </p>
886        <p>
887         Bitwise shift right
888        </p>
889        <p>
890         <code class="literal">8 &gt;&gt; 2</code>
891         → <code class="returnvalue">2</code>
892        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
893         <em class="replaceable"><code>number</code></em> <code class="literal">+</code> <em class="replaceable"><code>number</code></em>
894         → <code class="returnvalue"><em class="replaceable"><code>number</code></em></code>
895        </p>
896        <p>
897         Addition
898        </p>
899        <p>
900         <code class="literal">5 + 4</code>
901         → <code class="returnvalue">9</code>
902        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
903         <em class="replaceable"><code>number</code></em> <code class="literal">-</code> <em class="replaceable"><code>number</code></em>
904         → <code class="returnvalue"><em class="replaceable"><code>number</code></em></code>
905        </p>
906        <p>
907         Subtraction
908        </p>
909        <p>
910         <code class="literal">3 - 2.0</code>
911         → <code class="returnvalue">1.0</code>
912        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
913         <em class="replaceable"><code>number</code></em> <code class="literal">*</code> <em class="replaceable"><code>number</code></em>
914         → <code class="returnvalue"><em class="replaceable"><code>number</code></em></code>
915        </p>
916        <p>
917         Multiplication
918        </p>
919        <p>
920         <code class="literal">5 * 4</code>
921         → <code class="returnvalue">20</code>
922        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
923         <em class="replaceable"><code>number</code></em> <code class="literal">/</code> <em class="replaceable"><code>number</code></em>
924         → <code class="returnvalue"><em class="replaceable"><code>number</code></em></code>
925        </p>
926        <p>
927         Division (truncates the result towards zero if both inputs are integers)
928        </p>
929        <p>
930         <code class="literal">5 / 3</code>
931         → <code class="returnvalue">1</code>
932        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
933         <em class="replaceable"><code>integer</code></em> <code class="literal">%</code> <em class="replaceable"><code>integer</code></em>
934         → <code class="returnvalue"><em class="replaceable"><code>integer</code></em></code>
935        </p>
936        <p>
937         Modulo (remainder)
938        </p>
939        <p>
940         <code class="literal">3 % 2</code>
941         → <code class="returnvalue">1</code>
942        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
943         <code class="literal">-</code> <em class="replaceable"><code>number</code></em>
944         → <code class="returnvalue"><em class="replaceable"><code>number</code></em></code>
945        </p>
946        <p>
947         Negation
948        </p>
949        <p>
950         <code class="literal">- 2.0</code>
951         → <code class="returnvalue">-2.0</code>
952        </p></td></tr></tbody></table></div></div><br class="table-break" /></div><div class="refsect2" id="PGBENCH-BUILTIN-FUNCTIONS"><h3>Built-In Functions</h3><p>
953    The functions listed in <a class="xref" href="pgbench.html#PGBENCH-FUNCTIONS" title="Table 303. pgbench Functions">Table 303</a> are built
954    into <span class="application">pgbench</span> and may be used in expressions appearing in
955    <a class="link" href="pgbench.html#PGBENCH-METACOMMAND-SET"><code class="literal">\set</code></a>.
956   </p><div class="table" id="PGBENCH-FUNCTIONS"><p class="title"><strong>Table 303. pgbench Functions</strong></p><div class="table-contents"><table class="table" summary="pgbench Functions" border="1"><colgroup><col /></colgroup><thead><tr><th class="func_table_entry"><p class="func_signature">
957         Function
958        </p>
959        <p>
960         Description
961        </p>
962        <p>
963         Example(s)
964        </p></th></tr></thead><tbody><tr><td class="func_table_entry"><p class="func_signature">
965         <code class="function">abs</code> ( <em class="replaceable"><code>number</code></em> )
966         → <code class="returnvalue"></code> same type as input
967        </p>
968        <p>
969         Absolute value
970        </p>
971        <p>
972         <code class="literal">abs(-17)</code>
973         → <code class="returnvalue">17</code>
974        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
975         <code class="function">debug</code> ( <em class="replaceable"><code>number</code></em> )
976         → <code class="returnvalue"></code> same type as input
977        </p>
978        <p>
979         Prints the argument to <span class="systemitem">stderr</span>,
980         and returns the argument.
981        </p>
982        <p>
983         <code class="literal">debug(5432.1)</code>
984         → <code class="returnvalue">5432.1</code>
985        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
986         <code class="function">double</code> ( <em class="replaceable"><code>number</code></em> )
987         → <code class="returnvalue">double</code>
988        </p>
989        <p>
990         Casts to double.
991        </p>
992        <p>
993         <code class="literal">double(5432)</code>
994         → <code class="returnvalue">5432.0</code>
995        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
996         <code class="function">exp</code> ( <em class="replaceable"><code>number</code></em> )
997         → <code class="returnvalue">double</code>
998        </p>
999        <p>
1000         Exponential (<code class="literal">e</code> raised to the given power)
1001        </p>
1002        <p>
1003         <code class="literal">exp(1.0)</code>
1004         → <code class="returnvalue">2.718281828459045</code>
1005        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
1006         <code class="function">greatest</code> ( <em class="replaceable"><code>number</code></em> [<span class="optional">, <code class="literal">...</code> </span>] )
1007         → <code class="returnvalue"></code> <code class="type">double</code> if any argument is double, else <code class="type">integer</code>
1008        </p>
1009        <p>
1010         Selects the largest value among the arguments.
1011        </p>
1012        <p>
1013         <code class="literal">greatest(5, 4, 3, 2)</code>
1014         → <code class="returnvalue">5</code>
1015        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
1016         <code class="function">hash</code> ( <em class="parameter"><code>value</code></em> [<span class="optional">, <em class="parameter"><code>seed</code></em> </span>] )
1017         → <code class="returnvalue">integer</code>
1018        </p>
1019        <p>
1020         This is an alias for <code class="function">hash_murmur2</code>.
1021        </p>
1022        <p>
1023         <code class="literal">hash(10, 5432)</code>
1024         → <code class="returnvalue">-5817877081768721676</code>
1025        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
1026         <code class="function">hash_fnv1a</code> ( <em class="parameter"><code>value</code></em> [<span class="optional">, <em class="parameter"><code>seed</code></em> </span>] )
1027         → <code class="returnvalue">integer</code>
1028        </p>
1029        <p>
1030         Computes <a class="ulink" href="https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function" target="_top">FNV-1a hash</a>.
1031        </p>
1032        <p>
1033         <code class="literal">hash_fnv1a(10, 5432)</code>
1034         → <code class="returnvalue">-7793829335365542153</code>
1035        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
1036         <code class="function">hash_murmur2</code> ( <em class="parameter"><code>value</code></em> [<span class="optional">, <em class="parameter"><code>seed</code></em> </span>] )
1037         → <code class="returnvalue">integer</code>
1038        </p>
1039        <p>
1040         Computes <a class="ulink" href="https://en.wikipedia.org/wiki/MurmurHash" target="_top">MurmurHash2 hash</a>.
1041        </p>
1042        <p>
1043         <code class="literal">hash_murmur2(10, 5432)</code>
1044         → <code class="returnvalue">-5817877081768721676</code>
1045        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
1046         <code class="function">int</code> ( <em class="replaceable"><code>number</code></em> )
1047         → <code class="returnvalue">integer</code>
1048        </p>
1049        <p>
1050         Casts to integer.
1051        </p>
1052        <p>
1053         <code class="literal">int(5.4 + 3.8)</code>
1054         → <code class="returnvalue">9</code>
1055        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
1056         <code class="function">least</code> ( <em class="replaceable"><code>number</code></em> [<span class="optional">, <code class="literal">...</code> </span>] )
1057         → <code class="returnvalue"></code> <code class="type">double</code> if any argument is double, else <code class="type">integer</code>
1058        </p>
1059        <p>
1060         Selects the smallest value among the arguments.
1061        </p>
1062        <p>
1063         <code class="literal">least(5, 4, 3, 2.1)</code>
1064         → <code class="returnvalue">2.1</code>
1065        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
1066         <code class="function">ln</code> ( <em class="replaceable"><code>number</code></em> )
1067         → <code class="returnvalue">double</code>
1068        </p>
1069        <p>
1070         Natural logarithm
1071        </p>
1072        <p>
1073         <code class="literal">ln(2.718281828459045)</code>
1074         → <code class="returnvalue">1.0</code>
1075        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
1076 <code class="function">mod</code> ( <em class="replaceable"><code>integer</code></em>, <em class="replaceable"><code>integer</code></em> )
1077         → <code class="returnvalue">integer</code>
1078        </p>
1079        <p>
1080         Modulo (remainder)
1081        </p>
1082        <p>
1083         <code class="literal">mod(54, 32)</code>
1084         → <code class="returnvalue">22</code>
1085        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
1086         <code class="function">permute</code> ( <em class="parameter"><code>i</code></em>, <em class="parameter"><code>size</code></em> [, <em class="parameter"><code>seed</code></em> ] )
1087         → <code class="returnvalue">integer</code>
1088        </p>
1089        <p>
1090         Permuted value of <em class="parameter"><code>i</code></em>, in the range
1091         <code class="literal">[0, size)</code>.  This is the new position of
1092         <em class="parameter"><code>i</code></em> (modulo <em class="parameter"><code>size</code></em>) in a
1093         pseudorandom permutation of the integers <code class="literal">0...size-1</code>,
1094         parameterized by <em class="parameter"><code>seed</code></em>, see below.
1095        </p>
1096        <p>
1097         <code class="literal">permute(0, 4)</code>
1098         → <code class="returnvalue">an integer between 0 and 3</code>
1099        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
1100         <code class="function">pi</code> ()
1101         → <code class="returnvalue">double</code>
1102        </p>
1103        <p>
1104         Approximate value of <span class="symbol_font">π</span>
1105        </p>
1106        <p>
1107         <code class="literal">pi()</code>
1108         → <code class="returnvalue">3.14159265358979323846</code>
1109        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
1110         <code class="function">pow</code> ( <em class="parameter"><code>x</code></em>, <em class="parameter"><code>y</code></em> )
1111         → <code class="returnvalue">double</code>
1112        </p>
1113        <p class="func_signature">
1114         <code class="function">power</code> ( <em class="parameter"><code>x</code></em>, <em class="parameter"><code>y</code></em> )
1115         → <code class="returnvalue">double</code>
1116        </p>
1117        <p>
1118         <em class="parameter"><code>x</code></em> raised to the power of <em class="parameter"><code>y</code></em>
1119        </p>
1120        <p>
1121         <code class="literal">pow(2.0, 10)</code>
1122         → <code class="returnvalue">1024.0</code>
1123        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
1124         <code class="function">random</code> ( <em class="parameter"><code>lb</code></em>, <em class="parameter"><code>ub</code></em> )
1125         → <code class="returnvalue">integer</code>
1126        </p>
1127        <p>
1128         Computes a uniformly-distributed random integer in <code class="literal">[lb,
1129         ub]</code>.
1130        </p>
1131        <p>
1132         <code class="literal">random(1, 10)</code>
1133         → <code class="returnvalue">an integer between 1 and 10</code>
1134        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
1135         <code class="function">random_exponential</code> ( <em class="parameter"><code>lb</code></em>, <em class="parameter"><code>ub</code></em>, <em class="parameter"><code>parameter</code></em> )
1136         → <code class="returnvalue">integer</code>
1137        </p>
1138        <p>
1139         Computes an exponentially-distributed random integer in <code class="literal">[lb,
1140         ub]</code>, see below.
1141        </p>
1142        <p>
1143         <code class="literal">random_exponential(1, 10, 3.0)</code>
1144         → <code class="returnvalue">an integer between 1 and 10</code>
1145        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
1146         <code class="function">random_gaussian</code> ( <em class="parameter"><code>lb</code></em>, <em class="parameter"><code>ub</code></em>, <em class="parameter"><code>parameter</code></em> )
1147         → <code class="returnvalue">integer</code>
1148        </p>
1149        <p>
1150         Computes a Gaussian-distributed random integer in <code class="literal">[lb,
1151         ub]</code>, see below.
1152        </p>
1153        <p>
1154         <code class="literal">random_gaussian(1, 10, 2.5)</code>
1155         → <code class="returnvalue">an integer between 1 and 10</code>
1156        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
1157         <code class="function">random_zipfian</code> ( <em class="parameter"><code>lb</code></em>, <em class="parameter"><code>ub</code></em>, <em class="parameter"><code>parameter</code></em> )
1158         → <code class="returnvalue">integer</code>
1159        </p>
1160        <p>
1161         Computes a Zipfian-distributed random integer in <code class="literal">[lb,
1162         ub]</code>, see below.
1163        </p>
1164        <p>
1165         <code class="literal">random_zipfian(1, 10, 1.5)</code>
1166         → <code class="returnvalue">an integer between 1 and 10</code>
1167        </p></td></tr><tr><td class="func_table_entry"><p class="func_signature">
1168         <code class="function">sqrt</code> ( <em class="replaceable"><code>number</code></em> )
1169         → <code class="returnvalue">double</code>
1170        </p>
1171        <p>
1172         Square root
1173        </p>
1174        <p>
1175         <code class="literal">sqrt(2.0)</code>
1176         → <code class="returnvalue">1.414213562</code>
1177        </p></td></tr></tbody></table></div></div><br class="table-break" /><p>
1178     The <code class="literal">random</code> function generates values using a uniform
1179     distribution, that is all the values are drawn within the specified
1180     range with equal probability. The <code class="literal">random_exponential</code>,
1181     <code class="literal">random_gaussian</code> and <code class="literal">random_zipfian</code>
1182     functions require an additional double parameter which determines the precise
1183     shape of the distribution.
1184    </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
1185       For an exponential distribution, <em class="replaceable"><code>parameter</code></em>
1186       controls the distribution by truncating a quickly-decreasing
1187       exponential distribution at <em class="replaceable"><code>parameter</code></em>, and then
1188       projecting onto integers between the bounds.
1189       To be precise, with
1190 </p><div class="literallayout"><p><br />
1191 f(x) = exp(-parameter * (x - min) / (max - min + 1)) / (1 - exp(-parameter))<br />
1192 </p></div><p>
1193       Then value <em class="replaceable"><code>i</code></em> between <em class="replaceable"><code>min</code></em> and
1194       <em class="replaceable"><code>max</code></em> inclusive is drawn with probability:
1195       <code class="literal">f(i) - f(i + 1)</code>.
1196      </p><p>
1197       Intuitively, the larger the <em class="replaceable"><code>parameter</code></em>, the more
1198       frequently values close to <em class="replaceable"><code>min</code></em> are accessed, and the
1199       less frequently values close to <em class="replaceable"><code>max</code></em> are accessed.
1200       The closer to 0 <em class="replaceable"><code>parameter</code></em> is, the flatter (more
1201       uniform) the access distribution.
1202       A crude approximation of the distribution is that the most frequent 1%
1203       values in the range, close to <em class="replaceable"><code>min</code></em>, are drawn
1204       <em class="replaceable"><code>parameter</code></em>% of the time.
1205       The <em class="replaceable"><code>parameter</code></em> value must be strictly positive.
1206      </p></li><li class="listitem"><p>
1207       For a Gaussian distribution, the interval is mapped onto a standard
1208       normal distribution (the classical bell-shaped Gaussian curve) truncated
1209       at <code class="literal">-parameter</code> on the left and <code class="literal">+parameter</code>
1210       on the right.
1211       Values in the middle of the interval are more likely to be drawn.
1212       To be precise, if <code class="literal">PHI(x)</code> is the cumulative distribution
1213       function of the standard normal distribution, with mean <code class="literal">mu</code>
1214       defined as <code class="literal">(max + min) / 2.0</code>, with
1215 </p><div class="literallayout"><p><br />
1216 f(x) = PHI(2.0 * parameter * (x - mu) / (max - min + 1)) /<br />
1217        (2.0 * PHI(parameter) - 1)<br />
1218 </p></div><p>
1219       then value <em class="replaceable"><code>i</code></em> between <em class="replaceable"><code>min</code></em> and
1220       <em class="replaceable"><code>max</code></em> inclusive is drawn with probability:
1221       <code class="literal">f(i + 0.5) - f(i - 0.5)</code>.
1222       Intuitively, the larger the <em class="replaceable"><code>parameter</code></em>, the more
1223       frequently values close to the middle of the interval are drawn, and the
1224       less frequently values close to the <em class="replaceable"><code>min</code></em> and
1225       <em class="replaceable"><code>max</code></em> bounds. About 67% of values are drawn from the
1226       middle <code class="literal">1.0 / parameter</code>, that is a relative
1227       <code class="literal">0.5 / parameter</code> around the mean, and 95% in the middle
1228       <code class="literal">2.0 / parameter</code>, that is a relative
1229       <code class="literal">1.0 / parameter</code> around the mean; for instance, if
1230       <em class="replaceable"><code>parameter</code></em> is 4.0, 67% of values are drawn from the
1231       middle quarter (1.0 / 4.0) of the interval (i.e., from
1232       <code class="literal">3.0 / 8.0</code> to <code class="literal">5.0 / 8.0</code>) and 95% from
1233       the middle half (<code class="literal">2.0 / 4.0</code>) of the interval (second and third
1234       quartiles). The minimum allowed <em class="replaceable"><code>parameter</code></em>
1235       value is 2.0.
1236      </p></li><li class="listitem"><p>
1237       <code class="literal">random_zipfian</code> generates a bounded Zipfian
1238       distribution.
1239       <em class="replaceable"><code>parameter</code></em> defines how skewed the distribution
1240       is. The larger the <em class="replaceable"><code>parameter</code></em>, the more
1241       frequently values closer to the beginning of the interval are drawn.
1242       The distribution is such that, assuming the range starts from 1,
1243       the ratio of the probability of drawing <em class="replaceable"><code>k</code></em>
1244       versus drawing <em class="replaceable"><code>k+1</code></em> is
1245       <code class="literal">((<em class="replaceable"><code>k</code></em>+1)/<em class="replaceable"><code>k</code></em>)**<em class="replaceable"><code>parameter</code></em></code>.
1246       For example, <code class="literal">random_zipfian(1, ..., 2.5)</code> produces
1247       the value <code class="literal">1</code> about <code class="literal">(2/1)**2.5 =
1248       5.66</code> times more frequently than <code class="literal">2</code>, which
1249       itself is produced <code class="literal">(3/2)**2.5 = 2.76</code> times more
1250       frequently than <code class="literal">3</code>, and so on.
1251      </p><p>
1252       <span class="application">pgbench</span>'s implementation is based on
1253       "Non-Uniform Random Variate Generation", Luc Devroye, p. 550-551,
1254       Springer 1986.  Due to limitations of that algorithm,
1255       the <em class="replaceable"><code>parameter</code></em> value is restricted to
1256       the range [1.001, 1000].
1257      </p></li></ul></div><div class="note"><h3 class="title">Note</h3><p>
1258       When designing a benchmark which selects rows non-uniformly, be aware
1259       that the rows chosen may be correlated with other data such as IDs from
1260       a sequence or the physical row ordering, which may skew performance
1261       measurements.
1262     </p><p>
1263       To avoid this, you may wish to use the <code class="function">permute</code>
1264       function, or some other additional step with similar effect, to shuffle
1265       the selected rows and remove such correlations.
1266     </p></div><p>
1267     Hash functions <code class="literal">hash</code>, <code class="literal">hash_murmur2</code> and
1268     <code class="literal">hash_fnv1a</code> accept an input value and an optional seed parameter.
1269     In case the seed isn't provided the value of <code class="literal">:default_seed</code>
1270     is used, which is initialized randomly unless set by the command-line
1271     <code class="literal">-D</code> option.
1272   </p><p>
1273     <code class="literal">permute</code> accepts an input value, a size, and an optional
1274     seed parameter.  It generates a pseudorandom permutation of integers in
1275     the range <code class="literal">[0, size)</code>, and returns the index of the input
1276     value in the permuted values.  The permutation chosen is parameterized by
1277     the seed, which defaults to <code class="literal">:default_seed</code>, if not
1278     specified.  Unlike the hash functions, <code class="literal">permute</code> ensures
1279     that there are no collisions or holes in the output values.  Input values
1280     outside the interval are interpreted modulo the size.  The function raises
1281     an error if the size is not positive.  <code class="function">permute</code> can be
1282     used to scatter the distribution of non-uniform random functions such as
1283     <code class="literal">random_zipfian</code> or <code class="literal">random_exponential</code>
1284     so that values drawn more often are not trivially correlated.  For
1285     instance, the following <span class="application">pgbench</span> script
1286     simulates a possible real world workload typical for social media and
1287     blogging platforms where a few accounts generate excessive load:
1288
1289 </p><pre class="programlisting">
1290 \set size 1000000
1291 \set r random_zipfian(1, :size, 1.07)
1292 \set k 1 + permute(:r, :size)
1293 </pre><p>
1294
1295     In some cases several distinct distributions are needed which don't correlate
1296     with each other and this is when the optional seed parameter comes in handy:
1297
1298 </p><pre class="programlisting">
1299 \set k1 1 + permute(:r, :size, :default_seed + 123)
1300 \set k2 1 + permute(:r, :size, :default_seed + 321)
1301 </pre><p>
1302
1303     A similar behavior can also be approximated with <code class="function">hash</code>:
1304
1305 </p><pre class="programlisting">
1306 \set size 1000000
1307 \set r random_zipfian(1, 100 * :size, 1.07)
1308 \set k 1 + abs(hash(:r)) % :size
1309 </pre><p>
1310
1311     However, since <code class="function">hash</code> generates collisions, some values
1312     will not be reachable and others will be more frequent than expected from
1313     the original distribution.
1314   </p><p>
1315    As an example, the full definition of the built-in TPC-B-like
1316    transaction is:
1317
1318 </p><pre class="programlisting">
1319 \set aid random(1, 100000 * :scale)
1320 \set bid random(1, 1 * :scale)
1321 \set tid random(1, 10 * :scale)
1322 \set delta random(-5000, 5000)
1323 BEGIN;
1324 UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;
1325 SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
1326 UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;
1327 UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;
1328 INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
1329 END;
1330 </pre><p>
1331
1332    This script allows each iteration of the transaction to reference
1333    different, randomly-chosen rows.  (This example also shows why it's
1334    important for each client session to have its own variables —
1335    otherwise they'd not be independently touching different rows.)
1336   </p></div><div class="refsect2" id="id-1.9.4.11.9.6"><h3>Per-Transaction Logging</h3><p>
1337    With the <code class="option">-l</code> option (but without
1338    the <code class="option">--aggregate-interval</code> option),
1339    <span class="application">pgbench</span> writes information about each transaction
1340    to a log file.  The log file will be named
1341    <code class="filename"><em class="replaceable"><code>prefix</code></em>.<em class="replaceable"><code>nnn</code></em></code>,
1342    where <em class="replaceable"><code>prefix</code></em> defaults to <code class="literal">pgbench_log</code>, and
1343    <em class="replaceable"><code>nnn</code></em> is the PID of the
1344    <span class="application">pgbench</span> process.
1345    The prefix can be changed by using the <code class="option">--log-prefix</code> option.
1346    If the <code class="option">-j</code> option is 2 or higher, so that there are multiple
1347    worker threads, each will have its own log file. The first worker will
1348    use the same name for its log file as in the standard single worker case.
1349    The additional log files for the other workers will be named
1350    <code class="filename"><em class="replaceable"><code>prefix</code></em>.<em class="replaceable"><code>nnn</code></em>.<em class="replaceable"><code>mmm</code></em></code>,
1351    where <em class="replaceable"><code>mmm</code></em> is a sequential number for each worker starting
1352    with 1.
1353   </p><p>
1354    Each line in a log file describes one transaction.
1355    It contains the following space-separated fields:
1356
1357    </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>client_id</code></em></span></dt><dd><p>
1358        identifies the client session that ran the transaction
1359       </p></dd><dt><span class="term"><em class="replaceable"><code>transaction_no</code></em></span></dt><dd><p>
1360        counts how many transactions have been run by that session
1361       </p></dd><dt><span class="term"><em class="replaceable"><code>time</code></em></span></dt><dd><p>
1362        transaction's elapsed time, in microseconds
1363       </p></dd><dt><span class="term"><em class="replaceable"><code>script_no</code></em></span></dt><dd><p>
1364        identifies the script file that was used for the transaction
1365        (useful when multiple scripts are specified
1366        with <code class="option">-f</code> or <code class="option">-b</code>)
1367       </p></dd><dt><span class="term"><em class="replaceable"><code>time_epoch</code></em></span></dt><dd><p>
1368        transaction's completion time, as a Unix-epoch time stamp
1369       </p></dd><dt><span class="term"><em class="replaceable"><code>time_us</code></em></span></dt><dd><p>
1370        fractional-second part of transaction's completion time, in
1371        microseconds
1372       </p></dd><dt><span class="term"><em class="replaceable"><code>schedule_lag</code></em></span></dt><dd><p>
1373        transaction start delay, that is the difference between the
1374        transaction's scheduled start time and the time it actually
1375        started, in microseconds
1376        (present only if <code class="option">--rate</code> is specified)
1377       </p></dd><dt><span class="term"><em class="replaceable"><code>retries</code></em></span></dt><dd><p>
1378        count of retries after serialization or deadlock errors during the
1379        transaction
1380        (present only if <code class="option">--max-tries</code> is not equal to one)
1381       </p></dd></dl></div><p>
1382   </p><p>
1383    When both <code class="option">--rate</code> and <code class="option">--latency-limit</code> are used,
1384    the <em class="replaceable"><code>time</code></em> for a skipped transaction will be reported as
1385    <code class="literal">skipped</code>.
1386    If the transaction ends with a failure, its <em class="replaceable"><code>time</code></em>
1387    will be reported as <code class="literal">failed</code>. If you use the
1388    <code class="option">--failures-detailed</code> option, the
1389    <em class="replaceable"><code>time</code></em> of the failed transaction will be reported as
1390    <code class="literal">serialization</code> or
1391    <code class="literal">deadlock</code> depending on the type of failure (see
1392    <a class="xref" href="pgbench.html#FAILURES-AND-RETRIES" title="Failures and Serialization/Deadlock Retries">Failures and Serialization/Deadlock Retries</a> for more information).
1393   </p><p>
1394    Here is a snippet of a log file generated in a single-client run:
1395 </p><pre class="screen">
1396 0 199 2241 0 1175850568 995598
1397 0 200 2465 0 1175850568 998079
1398 0 201 2513 0 1175850569 608
1399 0 202 2038 0 1175850569 2663
1400 </pre><p>
1401
1402    Another example with <code class="literal">--rate=100</code>
1403    and <code class="literal">--latency-limit=5</code> (note the additional
1404    <em class="replaceable"><code>schedule_lag</code></em> column):
1405 </p><pre class="screen">
1406 0 81 4621 0 1412881037 912698 3005
1407 0 82 6173 0 1412881037 914578 4304
1408 0 83 skipped 0 1412881037 914578 5217
1409 0 83 skipped 0 1412881037 914578 5099
1410 0 83 4722 0 1412881037 916203 3108
1411 0 84 4142 0 1412881037 918023 2333
1412 0 85 2465 0 1412881037 919759 740
1413 </pre><p>
1414    In this example, transaction 82 was late, because its latency (6.173 ms) was
1415    over the 5 ms limit. The next two transactions were skipped, because they
1416    were already late before they were even started.
1417   </p><p>
1418    The following example shows a snippet of a log file with failures and
1419    retries, with the maximum number of tries set to 10 (note the additional
1420    <em class="replaceable"><code>retries</code></em> column):
1421 </p><pre class="screen">
1422 3 0 47423 0 1499414498 34501 3
1423 3 1 8333 0 1499414498 42848 0
1424 3 2 8358 0 1499414498 51219 0
1425 4 0 72345 0 1499414498 59433 6
1426 1 3 41718 0 1499414498 67879 4
1427 1 4 8416 0 1499414498 76311 0
1428 3 3 33235 0 1499414498 84469 3
1429 0 0 failed 0 1499414498 84905 9
1430 2 0 failed 0 1499414498 86248 9
1431 3 4 8307 0 1499414498 92788 0
1432 </pre><p>
1433   </p><p>
1434    If the <code class="option">--failures-detailed</code> option is used, the type of
1435    failure is reported in the <em class="replaceable"><code>time</code></em> like this:
1436 </p><pre class="screen">
1437 3 0 47423 0 1499414498 34501 3
1438 3 1 8333 0 1499414498 42848 0
1439 3 2 8358 0 1499414498 51219 0
1440 4 0 72345 0 1499414498 59433 6
1441 1 3 41718 0 1499414498 67879 4
1442 1 4 8416 0 1499414498 76311 0
1443 3 3 33235 0 1499414498 84469 3
1444 0 0 serialization 0 1499414498 84905 9
1445 2 0 serialization 0 1499414498 86248 9
1446 3 4 8307 0 1499414498 92788 0
1447 </pre><p>
1448   </p><p>
1449    When running a long test on hardware that can handle a lot of transactions,
1450    the log files can become very large.  The <code class="option">--sampling-rate</code> option
1451    can be used to log only a random sample of transactions.
1452   </p></div><div class="refsect2" id="id-1.9.4.11.9.7"><h3>Aggregated Logging</h3><p>
1453    With the <code class="option">--aggregate-interval</code> option, a different
1454    format is used for the log files.  Each log line describes one
1455    aggregation interval.  It contains the following space-separated
1456    fields:
1457
1458    </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>interval_start</code></em></span></dt><dd><p>
1459        start time of the interval, as a Unix-epoch time stamp
1460       </p></dd><dt><span class="term"><em class="replaceable"><code>num_transactions</code></em></span></dt><dd><p>
1461        number of transactions within the interval
1462       </p></dd><dt><span class="term"><em class="replaceable"><code>sum_latency</code></em></span></dt><dd><p>
1463        sum of transaction latencies
1464       </p></dd><dt><span class="term"><em class="replaceable"><code>sum_latency_2</code></em></span></dt><dd><p>
1465        sum of squares of transaction latencies
1466       </p></dd><dt><span class="term"><em class="replaceable"><code>min_latency</code></em></span></dt><dd><p>
1467        minimum transaction latency
1468       </p></dd><dt><span class="term"><em class="replaceable"><code>max_latency</code></em></span></dt><dd><p>
1469        maximum transaction latency
1470       </p></dd><dt><span class="term"><em class="replaceable"><code>sum_lag</code></em></span></dt><dd><p>
1471        sum of transaction start delays
1472        (zero unless <code class="option">--rate</code> is specified)
1473       </p></dd><dt><span class="term"><em class="replaceable"><code>sum_lag_2</code></em></span></dt><dd><p>
1474        sum of squares of transaction start delays
1475        (zero unless <code class="option">--rate</code> is specified)
1476       </p></dd><dt><span class="term"><em class="replaceable"><code>min_lag</code></em></span></dt><dd><p>
1477        minimum transaction start delay
1478        (zero unless <code class="option">--rate</code> is specified)
1479       </p></dd><dt><span class="term"><em class="replaceable"><code>max_lag</code></em></span></dt><dd><p>
1480        maximum transaction start delay
1481        (zero unless <code class="option">--rate</code> is specified)
1482       </p></dd><dt><span class="term"><em class="replaceable"><code>skipped</code></em></span></dt><dd><p>
1483        number of transactions skipped because they would have started too late
1484        (zero unless <code class="option">--rate</code>
1485        and <code class="option">--latency-limit</code> are specified)
1486       </p></dd><dt><span class="term"><em class="replaceable"><code>retried</code></em></span></dt><dd><p>
1487        number of retried transactions
1488        (zero unless <code class="option">--max-tries</code> is not equal to one)
1489       </p></dd><dt><span class="term"><em class="replaceable"><code>retries</code></em></span></dt><dd><p>
1490        number of retries after serialization or deadlock errors
1491        (zero unless <code class="option">--max-tries</code> is not equal to one)
1492       </p></dd><dt><span class="term"><em class="replaceable"><code>serialization_failures</code></em></span></dt><dd><p>
1493        number of transactions that got a serialization error and were not
1494        retried afterwards
1495        (zero unless <code class="option">--failures-detailed</code> is specified)
1496       </p></dd><dt><span class="term"><em class="replaceable"><code>deadlock_failures</code></em></span></dt><dd><p>
1497        number of transactions that got a deadlock error and were not
1498        retried afterwards
1499        (zero unless <code class="option">--failures-detailed</code> is specified)
1500       </p></dd></dl></div><p>
1501   </p><p>
1502    Here is some example output generated with this option:
1503 </p><pre class="screen">
1504 <strong class="userinput"><code>pgbench --aggregate-interval=10 --time=20 --client=10 --log --rate=1000 --latency-limit=10 --failures-detailed --max-tries=10 test</code></strong>
1505
1506 1650260552 5178 26171317 177284491527 1136 44462 2647617 7321113867 0 9866 64 7564 28340 4148 0
1507 1650260562 4808 25573984 220121792172 1171 62083 3037380 9666800914 0 9998 598 7392 26621 4527 0
1508 </pre><p>
1509   </p><p>
1510    Notice that while the plain (unaggregated) log format shows which script
1511    was used for each transaction, the aggregated format does not. Therefore if
1512    you need per-script data, you need to aggregate the data on your own.
1513   </p></div><div class="refsect2" id="id-1.9.4.11.9.8"><h3>Per-Statement Report</h3><p>
1514    With the <code class="option">-r</code> option, <span class="application">pgbench</span>
1515    collects the following statistics for each statement:
1516    </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
1517          <code class="literal">latency</code> — elapsed transaction time for each
1518          statement. <span class="application">pgbench</span> reports an average value
1519          of all successful runs of the statement.
1520        </p></li><li class="listitem"><p>
1521          The number of failures in this statement. See
1522          <a class="xref" href="pgbench.html#FAILURES-AND-RETRIES" title="Failures and Serialization/Deadlock Retries">Failures and Serialization/Deadlock Retries</a> for more information.
1523        </p></li><li class="listitem"><p>
1524          The number of retries after a serialization or a deadlock error in this
1525          statement. See <a class="xref" href="pgbench.html#FAILURES-AND-RETRIES" title="Failures and Serialization/Deadlock Retries">Failures and Serialization/Deadlock Retries</a> for more information.
1526        </p></li></ul></div><p>
1527   </p><p>
1528    The report displays retry statistics only if the <code class="option">--max-tries</code>
1529    option is not equal to 1.
1530   </p><p>
1531    All values are computed for each statement executed by every client and are
1532    reported after the benchmark has finished.
1533   </p><p>
1534    For the default script, the output will look similar to this:
1535 </p><pre class="screen">
1536 starting vacuum...end.
1537 transaction type: &lt;builtin: TPC-B (sort of)&gt;
1538 scaling factor: 1
1539 query mode: simple
1540 number of clients: 10
1541 number of threads: 1
1542 maximum number of tries: 1
1543 number of transactions per client: 1000
1544 number of transactions actually processed: 10000/10000
1545 number of failed transactions: 0 (0.000%)
1546 number of transactions above the 50.0 ms latency limit: 1311/10000 (13.110 %)
1547 latency average = 28.488 ms
1548 latency stddev = 21.009 ms
1549 initial connection time = 69.068 ms
1550 tps = 346.224794 (without initial connection time)
1551 statement latencies in milliseconds and failures:
1552    0.012  0  \set aid random(1, 100000 * :scale)
1553    0.002  0  \set bid random(1, 1 * :scale)
1554    0.002  0  \set tid random(1, 10 * :scale)
1555    0.002  0  \set delta random(-5000, 5000)
1556    0.319  0  BEGIN;
1557    0.834  0  UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;
1558    0.641  0  SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
1559   11.126  0  UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;
1560   12.961  0  UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;
1561    0.634  0  INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
1562    1.957  0  END;
1563 </pre><p>
1564
1565    Another example of output for the default script using serializable default
1566    transaction isolation level (<code class="command">PGOPTIONS='-c
1567    default_transaction_isolation=serializable' pgbench ...</code>):
1568 </p><pre class="screen">
1569 starting vacuum...end.
1570 transaction type: &lt;builtin: TPC-B (sort of)&gt;
1571 scaling factor: 1
1572 query mode: simple
1573 number of clients: 10
1574 number of threads: 1
1575 maximum number of tries: 10
1576 number of transactions per client: 1000
1577 number of transactions actually processed: 6317/10000
1578 number of failed transactions: 3683 (36.830%)
1579 number of transactions retried: 7667 (76.670%)
1580 total number of retries: 45339
1581 number of transactions above the 50.0 ms latency limit: 106/6317 (1.678 %)
1582 latency average = 17.016 ms
1583 latency stddev = 13.283 ms
1584 initial connection time = 45.017 ms
1585 tps = 186.792667 (without initial connection time)
1586 statement latencies in milliseconds, failures and retries:
1587   0.006     0      0  \set aid random(1, 100000 * :scale)
1588   0.001     0      0  \set bid random(1, 1 * :scale)
1589   0.001     0      0  \set tid random(1, 10 * :scale)
1590   0.001     0      0  \set delta random(-5000, 5000)
1591   0.385     0      0  BEGIN;
1592   0.773     0      1  UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;
1593   0.624     0      0  SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
1594   1.098   320   3762  UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;
1595   0.582  3363  41576  UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;
1596   0.465     0      0  INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
1597   1.933     0      0  END;
1598 </pre><p>
1599    If multiple script files are specified, all statistics are reported
1600    separately for each script file.
1601   </p><p>
1602    Note that collecting the additional timing information needed for
1603    per-statement latency computation adds some overhead.  This will slow
1604    average execution speed and lower the computed TPS.  The amount
1605    of slowdown varies significantly depending on platform and hardware.
1606    Comparing average TPS values with and without latency reporting enabled
1607    is a good way to measure if the timing overhead is significant.
1608   </p></div><div class="refsect2" id="FAILURES-AND-RETRIES"><h3>Failures and Serialization/Deadlock Retries</h3><p>
1609    When executing <span class="application">pgbench</span>, there are three main types
1610    of errors:
1611    </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
1612          Errors of the main program. They are the most serious and always result
1613          in an immediate exit from <span class="application">pgbench</span> with the
1614          corresponding error message. They include:
1615          </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>
1616                errors at the beginning of <span class="application">pgbench</span>
1617                (e.g. an invalid option value);
1618              </p></li><li class="listitem"><p>
1619                errors in the initialization mode (e.g. the query to create
1620                tables for built-in scripts fails);
1621              </p></li><li class="listitem"><p>
1622                errors before starting threads (e.g. could not connect to the
1623                database server, syntax error in the meta command, thread
1624                creation failure);
1625              </p></li><li class="listitem"><p>
1626                internal <span class="application">pgbench</span> errors (which are
1627                supposed to never occur...).
1628              </p></li></ul></div></li><li class="listitem"><p>
1629          Errors when the thread manages its clients (e.g. the client could not
1630          start a connection to the database server / the socket for connecting
1631          the client to the database server has become invalid). In such cases
1632          all clients of this thread stop while other threads continue to work.
1633          However, <code class="option">--exit-on-abort</code> is specified, all of the
1634          threads stop immediately in this case.
1635        </p></li><li class="listitem"><p>
1636          Direct client errors. They lead to immediate exit from
1637          <span class="application">pgbench</span> with the corresponding error message
1638          in the case of an internal <span class="application">pgbench</span>
1639          error (which are supposed to never occur...) or when
1640          <code class="option">--exit-on-abort</code> is specified. Otherwise in the worst
1641          case they only lead to the abortion of the failed client while other
1642          clients continue their run (but some client errors are handled without
1643          an abortion of the client and reported separately, see below). Later in
1644          this section it is assumed that the discussed errors are only the
1645          direct client errors and they are not internal
1646          <span class="application">pgbench</span> errors.
1647        </p></li></ul></div><p>
1648   </p><p>
1649    A client's run is aborted in case of a serious error; for example, the
1650    connection with the database server was lost or the end of script was reached
1651    without completing the last transaction. In addition, if execution of an SQL
1652    or meta command fails for reasons other than serialization or deadlock errors,
1653    the client is aborted. Otherwise, if an SQL command fails with serialization or
1654    deadlock errors, the client is not aborted. In such cases, the current
1655    transaction is rolled back, which also includes setting the client variables
1656    as they were before the run of this transaction (it is assumed that one
1657    transaction script contains only one transaction; see
1658    <a class="xref" href="pgbench.html#TRANSACTIONS-AND-SCRIPTS" title="What Is the “Transaction” Actually Performed in pgbench?">What Is the "Transaction" Actually Performed in pgbench?</a> for more information).
1659    Transactions with serialization or deadlock errors are repeated after
1660    rollbacks until they complete successfully or reach the maximum
1661    number of tries (specified by the <code class="option">--max-tries</code> option) / the maximum
1662    time of retries (specified by the <code class="option">--latency-limit</code> option) / the end
1663    of benchmark (specified by the <code class="option">--time</code> option). If
1664    the last trial run fails, this transaction will be reported as failed but
1665    the client is not aborted and continues to work.
1666   </p><div class="note"><h3 class="title">Note</h3><p>
1667     Without specifying the <code class="option">--max-tries</code> option, a transaction will
1668     never be retried after a serialization or deadlock error because its default
1669     value is 1. Use an unlimited number of tries (<code class="literal">--max-tries=0</code>)
1670     and the <code class="option">--latency-limit</code> option to limit only the maximum time
1671     of tries. You can also use the <code class="option">--time</code> option to limit the
1672     benchmark duration under an unlimited number of tries.
1673    </p><p>
1674     Be careful when repeating scripts that contain multiple transactions: the
1675     script is always retried completely, so successful transactions can be
1676     performed several times.
1677    </p><p>
1678     Be careful when repeating transactions with shell commands. Unlike the
1679     results of SQL commands, the results of shell commands are not rolled back,
1680     except for the variable value of the <code class="command">\setshell</code> command.
1681    </p></div><p>
1682    The latency of a successful transaction includes the entire time of
1683    transaction execution with rollbacks and retries. The latency is measured
1684    only for successful transactions and commands but not for failed transactions
1685    or commands.
1686   </p><p>
1687    The main report contains the number of failed transactions. If the
1688    <code class="option">--max-tries</code> option is not equal to 1, the main report also
1689    contains statistics related to retries: the total number of retried
1690    transactions and total number of retries. The per-script report inherits all
1691    these fields from the main report. The per-statement report displays retry
1692    statistics only if the <code class="option">--max-tries</code> option is not equal to 1.
1693   </p><p>
1694    If you want to group failures by basic types in per-transaction and
1695    aggregation logs, as well as in the main and per-script reports, use the
1696    <code class="option">--failures-detailed</code> option. If you also want to distinguish
1697    all errors and failures (errors without retrying) by type including which
1698    limit for retries was exceeded and how much it was exceeded by for the
1699    serialization/deadlock failures, use the <code class="option">--verbose-errors</code>
1700    option.
1701   </p></div><div class="refsect2" id="id-1.9.4.11.9.10"><h3>Table Access Methods</h3><p>
1702    You may specify the <a class="link" href="tableam.html" title="Chapter 62. Table Access Method Interface Definition">Table Access Method</a>
1703    for the pgbench tables.  The environment variable <code class="envar">PGOPTIONS</code>
1704    specifies database configuration options that are passed to PostgreSQL via
1705    the command line (See <a class="xref" href="config-setting.html#CONFIG-SETTING-SHELL" title="19.1.4. Parameter Interaction via the Shell">Section 19.1.4</a>).
1706    For example, a hypothetical default Table Access Method for the tables that
1707    pgbench creates called <code class="literal">wuzza</code> can be specified with:
1708 </p><pre class="programlisting">
1709 PGOPTIONS='-c default_table_access_method=wuzza'
1710 </pre><p>
1711   </p></div><div class="refsect2" id="id-1.9.4.11.9.11"><h3>Good Practices</h3><p>
1712    It is very easy to use <span class="application">pgbench</span> to produce completely
1713    meaningless numbers.  Here are some guidelines to help you get useful
1714    results.
1715   </p><p>
1716    In the first place, <span class="emphasis"><em>never</em></span> believe any test that runs
1717    for only a few seconds.  Use the <code class="option">-t</code> or <code class="option">-T</code> option
1718    to make the run last at least a few minutes, so as to average out noise.
1719    In some cases you could need hours to get numbers that are reproducible.
1720    It's a good idea to try the test run a few times, to find out if your
1721    numbers are reproducible or not.
1722   </p><p>
1723    For the default TPC-B-like test scenario, the initialization scale factor
1724    (<code class="option">-s</code>) should be at least as large as the largest number of
1725    clients you intend to test (<code class="option">-c</code>); else you'll mostly be
1726    measuring update contention.  There are only <code class="option">-s</code> rows in
1727    the <code class="structname">pgbench_branches</code> table, and every transaction wants to
1728    update one of them, so <code class="option">-c</code> values in excess of <code class="option">-s</code>
1729    will undoubtedly result in lots of transactions blocked waiting for
1730    other transactions.
1731   </p><p>
1732    The default test scenario is also quite sensitive to how long it's been
1733    since the tables were initialized: accumulation of dead rows and dead space
1734    in the tables changes the results.  To understand the results you must keep
1735    track of the total number of updates and when vacuuming happens.  If
1736    autovacuum is enabled it can result in unpredictable changes in measured
1737    performance.
1738   </p><p>
1739    A limitation of <span class="application">pgbench</span> is that it can itself become
1740    the bottleneck when trying to test a large number of client sessions.
1741    This can be alleviated by running <span class="application">pgbench</span> on a different
1742    machine from the database server, although low network latency will be
1743    essential.  It might even be useful to run several <span class="application">pgbench</span>
1744    instances concurrently, on several client machines, against the same
1745    database server.
1746   </p></div><div class="refsect2" id="id-1.9.4.11.9.12"><h3>Security</h3><p>
1747     If untrusted users have access to a database that has not adopted a
1748     <a class="link" href="ddl-schemas.html#DDL-SCHEMAS-PATTERNS" title="5.10.6. Usage Patterns">secure schema usage pattern</a>,
1749     do not run <span class="application">pgbench</span> in that
1750     database.  <span class="application">pgbench</span> uses unqualified names and
1751     does not manipulate the search path.
1752   </p></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="app-pgbasebackup.html" title="pg_basebackup">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="reference-client.html" title="PostgreSQL Client Applications">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="app-pgcombinebackup.html" title="pg_combinebackup">Next</a></td></tr><tr><td width="40%" align="left" valign="top"><span class="application">pg_basebackup</span> </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> <span class="application">pg_combinebackup</span></td></tr></table></div></body></html>