aboutsummaryrefslogtreecommitdiff
path: root/doc/src/sgml/pgbench.sgml
blob: 0e9dcfab442535daa9a5bb02199122eea7f8fd05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
<sect1 id="pgbench">
 <title>pgbench</title>
 
 <indexterm zone="pgbench">
  <primary>pgbench</primary>
 </indexterm>

 <para>
  <literal>pgbench</literal> is a simple program to run a benchmark test. 
  <literal>pgbench</literal> is a client application of PostgreSQL and runs 
  with PostgreSQL only. It performs lots of small and simple transactions 
  including SELECT/UPDATE/INSERT operations then calculates number of
  transactions successfully completed within a second (transactions
  per second, tps). Targeting data includes a table with at least 100k
  tuples.
 </para>
 <para>
  Example outputs from pgbench look like:
 </para>
 <programlisting>
number of clients: 4
number of transactions per client: 100
number of processed transactions: 400/400
tps = 19.875015(including connections establishing)
tps = 20.098827(excluding connections establishing)
 </programlisting>
 <para> Similar program called "JDBCBench" already exists, but it requires
  Java that may not be available on every platform. Moreover some
  people concerned about the overhead of Java that might lead
  inaccurate results. So I decided to write in pure C, and named
  it "pgbench."
 </para>

 <para>
  Features of pgbench:
 </para>
 <itemizedlist>
  <listitem>
   <para>
    pgbench is written in C using libpq only. So it is very portable
    and easy to install.
   </para>
  </listitem>
  <listitem>
   <para>
    pgbench can simulate concurrent connections using asynchronous
    capability of libpq. No threading is required.
   </para>
  </listitem>
 </itemizedlist>

 <sect2>
  <title>Overview</title>
  <orderedlist>
   <listitem>
    <para>(optional)Initialize database by:</para>
    <programlisting>
pgbench -i &lt;dbname&gt;
    </programlisting>
    <para>
     where &lt;dbname&gt; is the name of database. pgbench uses four tables
     accounts, branches, history and tellers. These tables will be
     destroyed. Be very careful if you have tables having same
     names. Default test data contains:
    </para>
    <programlisting>
table           # of tuples
-------------------------
branches        1
tellers         10
accounts        100000
history         0
    </programlisting>
    <para>
     You can increase the number of tuples by using -s option. branches,
     tellers and accounts tables are created with a fillfactor which is
     set using -F option. See below.
    </para>
   </listitem>
   <listitem>
    <para>Run the benchmark test</para>
    <programlisting>
pgbench &lt;dbname&gt;
    </programlisting>
    <para>
     The default configuration is:
    </para>
    <programlisting>
     number of clients: 1
     number of transactions per client: 10
    </programlisting>
   </listitem>
  </orderedlist>

  <table>
   <title><literal>pgbench</literal> options</title>
   <tgroup cols="2">
    <thead>
     <row>
      <entry>Parameter</entry>
      <entry>Description</entry>
     </row>
    </thead>
    <tbody>
     <row>
      <entry><literal>-h hostname</literal></entry>
      <entry>
       <para>
        hostname where the backend is running. If this option
        is omitted, pgbench will connect to the localhost via
        Unix domain socket.
       </para>
      </entry>
     </row>
     <row>
      <entry><literal>-p port</literal></entry>
      <entry>
       <para>
        the port number that the backend is accepting. default is
        libpq's default, usually 5432.
       </para>
      </entry>
     </row>
     <row>
      <entry><literal>-c number_of_clients</literal></entry>
      <entry>
       <para>
        Number of clients simulated. default is 1.
       </para>
      </entry>
     </row>
     <row>
      <entry><literal>-t number_of_transactions</literal></entry>
      <entry>
       <para>
        Number of transactions each client runs. default is 10.
       </para>
      </entry>
     </row>
     <row>
      <entry><literal>-s scaling_factor</literal></entry>
      <entry>
       <para>
        this should be used with -i (initialize) option.
        number of tuples generated will be multiple of the
        scaling factor. For example, -s 100 will imply 10M
        (10,000,000) tuples in the accounts table.
        default is 1.  
       </para>
       <para>
        NOTE: scaling factor should be at least
        as large as the largest number of clients you intend
        to test; else you'll mostly be measuring update contention.
        Regular (not initializing) runs using one of the
        built-in tests will detect scale based on the number of
        branches in the database.  For custom (-f) runs it can
        be manually specified with this parameter.
       </para>
      </entry>
     </row>
     <row>
      <entry><literal>-D varname=value</literal></entry>
      <entry>
       <para>
        Define a variable. It can be refered to by a script
        provided by using -f option. Multiple -D options are allowed.
       </para>
      </entry>
     </row>
     <row>
      <entry><literal>-U login</literal></entry>
      <entry>
       <para>
        Specify db user's login name if it is different from
        the Unix login name.
       </para>
      </entry>
     </row>
     <row>
      <entry><literal>-P password</literal></entry>
      <entry>
       <para>
        Specify the db password. CAUTION: using this option
        might be a security hole since ps command will
        show the password. Use this for TESTING PURPOSE ONLY.
       </para>
      </entry>
     </row>
     <row>
      <entry><literal>-n</literal></entry>
      <entry>
       <para>
        No vacuuming and cleaning the history table prior to the
        test is performed.
       </para>
      </entry>
     </row>
     <row>
      <entry><literal>-v</literal></entry>
      <entry>
       <para>
        Do vacuuming before testing. This will take some time.
        With neither -n nor -v, pgbench will vacuum tellers and
        branches tables only.
       </para>
      </entry>
     </row>
     <row>
      <entry><literal>-S</literal></entry>
      <entry>
       <para>
        Perform select only transactions instead of TPC-B.
       </para>
      </entry>
     </row>
     <row>
      <entry><literal>-N</literal></entry>
      <entry>
       <para>
        Do not update "branches" and "tellers". This will
        avoid heavy update contention on branches and tellers,
        while it will not make pgbench supporting TPC-B like
        transactions.
       </para>
      </entry>
     </row>
     <row>
      <entry><literal>-f filename</literal></entry>
      <entry>
       <para>
        Read transaction script from file. Detailed
        explanation will appear later.
       </para>
      </entry>
     </row>
     <row>
      <entry><literal>-C</literal></entry>
      <entry>
       <para>
        Establish connection for each transaction, rather than
        doing it just once at beginning of pgbench in the normal
        mode. This is useful to measure the connection overhead.
       </para>
      </entry>
     </row>
     <row>
      <entry><literal>-l</literal></entry>
      <entry>
       <para>
        Write the time taken by each transaction to a logfile,
        with the name "pgbench_log.xxx", where xxx is the PID
        of the pgbench process. The format of the log is:
       </para>
       <programlisting>
        client_id transaction_no time file_no time-epoch time-us
       </programlisting>
       <para>
        where time is measured in microseconds, , the file_no is
        which test file was used (useful when multiple were
        specified with -f), and time-epoch/time-us are a
        UNIX epoch format timestamp followed by an offset
        in microseconds (suitable for creating a ISO 8601
        timestamp with a fraction of a second) of when
        the transaction completed.
       </para>
       <para>
        Here are example outputs:
       </para>
       <programlisting>
 0 199 2241 0 1175850568 995598
 0 200 2465 0 1175850568 998079
 0 201 2513 0 1175850569 608
 0 202 2038 0 1175850569 2663
       </programlisting>
      </entry>
     </row>
     <row>
      <entry><literal>-F fillfactor</literal></entry>
      <entry>
       <para>
        Create tables(accounts, tellers and branches) with the given
        fillfactor. Default is 100.  This should be used with -i
        (initialize) option.
       </para>
      </entry>
     </row>
     <row>
      <entry><literal>-d</literal></entry>
      <entry>
       <para>
        debug option.
       </para>
      </entry>
     </row>
    </tbody>
   </tgroup>
  </table>
 </sect2>

 <sect2>
  <title>What is the "transaction" actually performed in pgbench?</title>
  <orderedlist>
   <listitem><para><literal>begin;</literal></para></listitem>

   <listitem><para><literal>update accounts set abalance = abalance + :delta where aid = :aid;</literal></para></listitem>

   <listitem><para><literal>select abalance from accounts where aid = :aid;</literal></para></listitem>

   <listitem><para><literal>update tellers set tbalance = tbalance + :delta where tid = :tid;</literal></para></listitem>

   <listitem><para><literal>update branches set bbalance = bbalance + :delta where bid = :bid;</literal></para></listitem>

   <listitem><para><literal>insert into history(tid,bid,aid,delta) values(:tid,:bid,:aid,:delta);</literal></para></listitem>

   <listitem><para><literal>end;</literal></para></listitem>
  </orderedlist>
  <para>
   If you specify -N, (4) and (5) aren't included in the transaction.
  </para>
 </sect2>

 <sect2>
  <title>Script file</title>
  <para>
   <literal>pgbench</literal> has support for reading a transaction script 
   from a specified file (<literal>-f</literal> option). This file should 
   include SQL commands in each line. SQL command consists of multiple lines 
   are not supported. Empty lines and lines begging with "--" will be ignored.
  </para>
  <para>
   Multiple <literal>-f</literal> options are allowed. In this case each 
   transaction is assigned randomly chosen script.
  </para>
  <para>
   SQL commands can include "meta command" which begins with "\" (back
   slash). A meta command takes some arguments separted by white
   spaces. Currently following meta command is supported:
  </para>

  <itemizedlist>
   <listitem>
    <para>
     <literal>\set name operand1 [ operator operand2 ]</literal>
     - Sets the calculated value using "operand1" "operator"
     "operand2" to variable "name". If "operator" and "operand2"
     are omitted, the value of operand1 is set to variable "name". 
    </para>
    <para>
     Example:
    </para>
    <programlisting>
\set ntellers 10 * :scale
    </programlisting>
   </listitem>
   <listitem>
    <para>
     <literal>\setrandom name min max</literal>
     - Assigns random integer to name between min and max
    </para>
    <para>
     Example:
    </para>
    <programlisting>
\setrandom aid 1 100000
    </programlisting>
   </listitem>
   <listitem>
    <para>
     Variables can be referred to in SQL comands by adding ":" in front
     of the varible name.
    </para>
    <para>
     Example:
    </para>
    <programlisting>
SELECT abalance FROM accounts WHERE aid = :aid
    </programlisting>
    <para>
     Variables can also be defined by using -D option.
    </para>
   </listitem>
   <listitem>
    <para>
     <literal>\sleep num [us|ms|s]</> - Causes script execution to sleep for the
     specified duration of microseconds (us), milliseconds (ms) or the default
     seconds (s).
    </para>
    <para>
     Example:
    </para>
    <programlisting>
\setrandom millisec 1000 2500
\sleep : millisec ms
    </programlisting>
   </listitem>
  </itemizedlist>
 </sect2>

 <sect2>
  <title>Examples</title>
  <para>
   Example, TPC-B like benchmark can be defined as follows(scaling
   factor = 1):
  </para>
  <programlisting>
\set nbranches :scale
\set ntellers 10 * :scale
\set naccounts 100000 * :scale
\setrandom aid 1 :naccounts
\setrandom bid 1 :nbranches
\setrandom tid 1 :ntellers
\setrandom delta 1 10000
BEGIN
UPDATE accounts SET abalance = abalance + :delta WHERE aid = :aid
SELECT abalance FROM accounts WHERE aid = :aid
UPDATE tellers SET tbalance = tbalance + :delta WHERE tid = :tid
UPDATE branches SET bbalance = bbalance + :delta WHERE bid = :bid
INSERT INTO history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, 'now')
END
  </programlisting>
  <para>
   If you want to automatically set the scaling factor from the number of
   tuples in branches table, use -s option and shell command like this:
  </para>
  <programlisting>
pgbench -s $(psql -At -c "SELECT count(*) FROM branches") -f tpc_b.sql
  </programlisting>
  <para>
   Notice that -f option does not execute vacuum and clearing history
   table before starting benchmark.
  </para>
 </sect2>

</sect1>