diff --git a/contrib/pgbench/pgbench.c b/contrib/pgbench/pgbench.c
index 24dab1f..66d4023 100644
--- a/contrib/pgbench/pgbench.c
+++ b/contrib/pgbench/pgbench.c
@@ -2108,6 +2108,7 @@ main(int argc, char **argv)
 	instr_time	total_time;
 	instr_time	conn_total_time;
 	int			total_xacts;
+	char	   *clientid;
 
 	int			i;
 
@@ -2423,6 +2424,14 @@ main(int argc, char **argv)
 		exit(1);
 	}
 #endif
+	/*
+	 * Define a client_number variable that is unique per connection. But don't
+	 * override an explicit -D switch.
+	 */
+	if (getVariable(&state[0], "client_number") == NULL)
+		clientid = xmalloc(11); /* INT_MAX can be 10 digits long at most .*/
+	else
+		clientid = NULL
 
 	/*
 	 * save main process id in the global variable because process id will be
@@ -2446,9 +2455,25 @@ main(int argc, char **argv)
 				if (!putVariable(&state[i], "startup", state[0].variables[j].name, state[0].variables[j].value))
 					exit(1);
 			}
+
+			/* Define the client-id for this client */
+		if (clientid != NULL)
+			{
+				sprintf(clientid, "%d", i);
+				if (!putVariable(&state[i], "startup", "client_number", clientid))
+					exit(1);
+			}
 		}
 	}
 
+	/* Define the client-id for the first client */
+	if (clientid != NULL)
+	{
+		sprintf(clientid, "%d", 0);
+		if (!putVariable(&state[0], "startup", "client_number", clientid))
+			exit(1);
+	}
+
 	if (debug)
 	{
 		if (duration <= 0)
diff --git a/doc/src/sgml/pgbench.sgml b/doc/src/sgml/pgbench.sgml
index e9900d3..c495600 100644
--- a/doc/src/sgml/pgbench.sgml
+++ b/doc/src/sgml/pgbench.sgml
@@ -600,7 +600,10 @@ 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.
+   the variable <literal>scale</> is automatically preset to the current scale factor,
+   and <literal>client_number</> is preset to a unique number per client; 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
