diff --git a/contrib/pgbench/pgbench.c b/contrib/pgbench/pgbench.c
index 8c202bf870..1303217086 100644
--- a/contrib/pgbench/pgbench.c
+++ b/contrib/pgbench/pgbench.c
@@ -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...");
diff --git a/doc/src/sgml/pgbench.sgml b/doc/src/sgml/pgbench.sgml
index e9900d3267..8775606aff 100644
--- a/doc/src/sgml/pgbench.sgml
+++ b/doc/src/sgml/pgbench.sgml
@@ -600,13 +600,39 @@ pgbench options> dbname>
Variables can be set by the command-line