diff --git a/doc/src/sgml/ref/pgbench.sgml b/doc/src/sgml/ref/pgbench.sgml
index a808546..2517a3a 100644
--- a/doc/src/sgml/ref/pgbench.sgml
+++ b/doc/src/sgml/ref/pgbench.sgml
@@ -326,8 +326,7 @@ pgbench options> dbname>
Number of worker threads within pgbench.
Using more than one thread can be helpful on multi-CPU machines.
- The number of clients must be a multiple of the number of threads,
- since each thread is given the same number of client sessions to manage.
+ Clients are distributed as evenly as possible among available threads.
Default is 1.
diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
index 06a4dfd..5343837 100644
--- a/src/bin/pgbench/pgbench.c
+++ b/src/bin/pgbench/pgbench.c
@@ -2762,6 +2762,7 @@ main(int argc, char **argv)
int c;
int nclients = 1; /* default number of simulated clients */
int nthreads = 1; /* default number of threads */
+ int nclients_dealt; /* clients distributed between threads */
int is_init_mode = 0; /* initialize mode? */
int is_no_vacuum = 0; /* no vacuum at all before testing? */
int do_vacuum_accounts = 0; /* do vacuum accounts before testing? */
@@ -3122,12 +3123,6 @@ main(int argc, char **argv)
if (nxacts <= 0 && duration <= 0)
nxacts = DEFAULT_NXACTS;
- if (nclients % nthreads != 0)
- {
- fprintf(stderr, "number of clients (%d) must be a multiple of number of threads (%d)\n", nclients, nthreads);
- exit(1);
- }
-
/* --sampling-rate may be used only with -l */
if (sample_rate > 0.0 && !use_log)
{
@@ -3328,19 +3323,24 @@ main(int argc, char **argv)
/* set up thread data structures */
threads = (TState *) pg_malloc(sizeof(TState) * nthreads);
+ nclients_dealt = 0;
+
for (i = 0; i < nthreads; i++)
{
TState *thread = &threads[i];
thread->tid = i;
- thread->state = &state[nclients / nthreads * i];
- thread->nstate = nclients / nthreads;
+ thread->state = &state[nclients_dealt];
+ thread->nstate =
+ (nclients - nclients_dealt + nthreads - i - 1) / (nthreads - i);
thread->random_state[0] = random();
thread->random_state[1] = random();
thread->random_state[2] = random();
thread->throttle_latency_skipped = 0;
thread->latency_late = 0;
+ nclients_dealt += thread->nstate;
+
if (is_latencies)
{
/* Reserve memory for the thread to store per-command latencies */
@@ -3364,6 +3364,9 @@ main(int argc, char **argv)
}
}
+ /* all clients must be assigned to a thread */
+ Assert(nclients_dealt == nclients);
+
/* get start up time */
INSTR_TIME_SET_CURRENT(start_time);