]> granicus.if.org Git - postgresql/commitdiff
Add :client_id automatic variable for custom pgbench scripts.
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Fri, 14 Jun 2013 20:31:44 +0000 (23:31 +0300)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Fri, 14 Jun 2013 20:31:44 +0000 (23:31 +0300)
This makes it easier to write custom scripts that have different logic for
each client.

Gurjeet Singh, with some changes by me.

contrib/pgbench/pgbench.c
doc/src/sgml/pgbench.sgml

index 8c202bf8702d26f2f1efe4b529b1ebd6a814068b..1303217086e7878291dc3c96630733d959a683a0 100644 (file)
@@ -2544,6 +2544,20 @@ main(int argc, char **argv)
                }
        }
 
+       /*
+        * Define a :client_id variable that is unique per connection. But don't
+        * override an explicit -D switch.
+        */
+       if (getVariable(&state[0], "client_id") == NULL)
+       {
+               for (i = 0; i < nclients; i++)
+               {
+                       snprintf(val, sizeof(val), "%d", i);
+                       if (!putVariable(&state[i], "startup", "client_id", val))
+                               exit(1);
+               }
+       }
+
        if (!is_no_vacuum)
        {
                fprintf(stderr, "starting vacuum...");
index e9900d326701173f5ee979db9e18cb9b3c537d62..8775606affaeb2a20c9eda2b1cafa3a0e38f1681 100644 (file)
@@ -600,13 +600,39 @@ pgbench <optional> <replaceable>options</> </optional> <replaceable>dbname</>
    Variables can be set by the command-line <option>-D</> option,
    explained above, or by the meta commands explained below.
    In addition to any variables preset by <option>-D</> command-line options,
-   the variable <literal>scale</> is preset to the current scale factor.
+   there are a few variables that are preset automatically, listed in
+   <xref linkend="pgbench-automatic-variables">. A value specified for these
+   variables using <option>-D</> takes precedence over the automatic presets.
    Once set, a variable's
    value can be inserted into a SQL command by writing
    <literal>:</><replaceable>variablename</>.  When running more than
    one client session, each session has its own set of variables.
   </para>
 
+   <table id="pgbench-automatic-variables">
+    <title>Automatic variables</title>
+    <tgroup cols="2">
+     <thead>
+      <row>
+       <entry>Variable</entry>
+       <entry>Description</entry>
+      </row>
+     </thead>
+
+     <tbody>
+      <row>
+       <entry> <literal>scale</literal> </entry>
+       <entry>current scale factor</entry>
+      </row>
+
+      <row>
+       <entry> <literal>client_id</literal> </entry>
+       <entry>unique number identifying the client session (starts from zero)</entry>
+      </row>
+     </tbody>
+    </tgroup>
+   </table>
+
   <para>
    Script file meta commands begin with a backslash (<literal>\</>).
    Arguments to a meta command are separated by white space.