mirror of
https://github.com/postgres/postgres.git
synced 2025-04-22 23:02:54 +03:00
pgbench: Remove \setrandom.
You can now do the same thing via \set using the appropriate function, either random(), random_gaussian(), or random_exponential(), depending on the desired distribution. This is not backward-compatible, but per discussion, it's worth it to avoid having the old syntax hang around forever. Fabien Coelho, reviewed by Michael Paquier, and adjusted by me.
This commit is contained in:
parent
7abc157165
commit
ad9566470b
@ -836,60 +836,6 @@ pgbench <optional> <replaceable>options</> </optional> <replaceable>dbname</>
|
|||||||
</listitem>
|
</listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
|
||||||
<term>
|
|
||||||
<literal>\setrandom <replaceable>varname</> <replaceable>min</> <replaceable>max</> [ uniform | { gaussian | exponential } <replaceable>parameter</> ]</literal>
|
|
||||||
</term>
|
|
||||||
|
|
||||||
<listitem>
|
|
||||||
<para>
|
|
||||||
Sets variable <replaceable>varname</> to a random integer value
|
|
||||||
between the limits <replaceable>min</> and <replaceable>max</> inclusive.
|
|
||||||
Each limit can be either an integer constant or a
|
|
||||||
<literal>:</><replaceable>variablename</> reference to a variable
|
|
||||||
having an integer value.
|
|
||||||
</para>
|
|
||||||
|
|
||||||
<para>
|
|
||||||
<itemizedlist>
|
|
||||||
<listitem>
|
|
||||||
<para>
|
|
||||||
<literal>\setrandom n 1 10</> or <literal>\setrandom n 1 10 uniform</>
|
|
||||||
is equivalent to <literal>\set n random(1, 10)</> and uses a uniform
|
|
||||||
distribution.
|
|
||||||
</para>
|
|
||||||
</listitem>
|
|
||||||
|
|
||||||
<listitem>
|
|
||||||
<para>
|
|
||||||
<literal>\setrandom n 1 10 exponential 3.0</> is equivalent to
|
|
||||||
<literal>\set n random_exponential(1, 10, 3.0)</> and uses an
|
|
||||||
exponential distribution.
|
|
||||||
</para>
|
|
||||||
</listitem>
|
|
||||||
|
|
||||||
<listitem>
|
|
||||||
<para>
|
|
||||||
<literal>\setrandom n 1 10 gaussian 2.0</> is equivalent to
|
|
||||||
<literal>\set n random_gaussian(1, 10, 2.0)</>, and uses a gaussian
|
|
||||||
distribution.
|
|
||||||
</para>
|
|
||||||
</listitem>
|
|
||||||
</itemizedlist>
|
|
||||||
|
|
||||||
See the documentation of these functions below for further information
|
|
||||||
about the precise shape of these distributions, depending on the value
|
|
||||||
of the parameter.
|
|
||||||
</para>
|
|
||||||
|
|
||||||
<para>
|
|
||||||
Example:
|
|
||||||
<programlisting>
|
|
||||||
\setrandom aid 1 :naccounts gaussian 5.0
|
|
||||||
</programlisting></para>
|
|
||||||
</listitem>
|
|
||||||
</varlistentry>
|
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term>
|
<term>
|
||||||
<literal>\sleep <replaceable>number</> [ us | ms | s ]</literal>
|
<literal>\sleep <replaceable>number</> [ us | ms | s ]</literal>
|
||||||
|
@ -1941,148 +1941,7 @@ top:
|
|||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
if (pg_strcasecmp(argv[0], "set") == 0)
|
||||||
* Note: this section could be removed, as the same functionnality
|
|
||||||
* is available through \set xxx random_gaussian(...)
|
|
||||||
*/
|
|
||||||
if (pg_strcasecmp(argv[0], "setrandom") == 0)
|
|
||||||
{
|
|
||||||
char *var;
|
|
||||||
int64 min,
|
|
||||||
max;
|
|
||||||
double parameter = 0;
|
|
||||||
char res[64];
|
|
||||||
|
|
||||||
if (*argv[2] == ':')
|
|
||||||
{
|
|
||||||
if ((var = getVariable(st, argv[2] + 1)) == NULL)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "%s: undefined variable \"%s\"\n",
|
|
||||||
argv[0], argv[2]);
|
|
||||||
st->ecnt++;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
min = strtoint64(var);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
min = strtoint64(argv[2]);
|
|
||||||
|
|
||||||
if (*argv[3] == ':')
|
|
||||||
{
|
|
||||||
if ((var = getVariable(st, argv[3] + 1)) == NULL)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "%s: undefined variable \"%s\"\n",
|
|
||||||
argv[0], argv[3]);
|
|
||||||
st->ecnt++;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
max = strtoint64(var);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
max = strtoint64(argv[3]);
|
|
||||||
|
|
||||||
if (max < min)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "%s: \\setrandom maximum is less than minimum\n",
|
|
||||||
argv[0]);
|
|
||||||
st->ecnt++;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Generate random number functions need to be able to subtract
|
|
||||||
* max from min and add one to the result without overflowing.
|
|
||||||
* Since we know max > min, we can detect overflow just by
|
|
||||||
* checking for a negative result. But we must check both that the
|
|
||||||
* subtraction doesn't overflow, and that adding one to the result
|
|
||||||
* doesn't overflow either.
|
|
||||||
*/
|
|
||||||
if (max - min < 0 || (max - min) + 1 < 0)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "%s: \\setrandom range is too large\n",
|
|
||||||
argv[0]);
|
|
||||||
st->ecnt++;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (argc == 4 || /* uniform without or with "uniform" keyword */
|
|
||||||
(argc == 5 && pg_strcasecmp(argv[4], "uniform") == 0))
|
|
||||||
{
|
|
||||||
#ifdef DEBUG
|
|
||||||
printf("min: " INT64_FORMAT " max: " INT64_FORMAT " random: " INT64_FORMAT "\n", min, max, getrand(thread, min, max));
|
|
||||||
#endif
|
|
||||||
snprintf(res, sizeof(res), INT64_FORMAT, getrand(thread, min, max));
|
|
||||||
}
|
|
||||||
else if (argc == 6 &&
|
|
||||||
((pg_strcasecmp(argv[4], "gaussian") == 0) ||
|
|
||||||
(pg_strcasecmp(argv[4], "exponential") == 0)))
|
|
||||||
{
|
|
||||||
if (*argv[5] == ':')
|
|
||||||
{
|
|
||||||
if ((var = getVariable(st, argv[5] + 1)) == NULL)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "%s: invalid parameter: \"%s\"\n",
|
|
||||||
argv[0], argv[5]);
|
|
||||||
st->ecnt++;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
parameter = strtod(var, NULL);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
parameter = strtod(argv[5], NULL);
|
|
||||||
|
|
||||||
if (pg_strcasecmp(argv[4], "gaussian") == 0)
|
|
||||||
{
|
|
||||||
if (parameter < MIN_GAUSSIAN_PARAM)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "gaussian parameter must be at least %f (not \"%s\")\n", MIN_GAUSSIAN_PARAM, argv[5]);
|
|
||||||
st->ecnt++;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
#ifdef DEBUG
|
|
||||||
printf("min: " INT64_FORMAT " max: " INT64_FORMAT " random: " INT64_FORMAT "\n",
|
|
||||||
min, max,
|
|
||||||
getGaussianRand(thread, min, max, parameter));
|
|
||||||
#endif
|
|
||||||
snprintf(res, sizeof(res), INT64_FORMAT,
|
|
||||||
getGaussianRand(thread, min, max, parameter));
|
|
||||||
}
|
|
||||||
else if (pg_strcasecmp(argv[4], "exponential") == 0)
|
|
||||||
{
|
|
||||||
if (parameter <= 0.0)
|
|
||||||
{
|
|
||||||
fprintf(stderr,
|
|
||||||
"exponential parameter must be greater than zero (not \"%s\")\n",
|
|
||||||
argv[5]);
|
|
||||||
st->ecnt++;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
#ifdef DEBUG
|
|
||||||
printf("min: " INT64_FORMAT " max: " INT64_FORMAT " random: " INT64_FORMAT "\n",
|
|
||||||
min, max,
|
|
||||||
getExponentialRand(thread, min, max, parameter));
|
|
||||||
#endif
|
|
||||||
snprintf(res, sizeof(res), INT64_FORMAT,
|
|
||||||
getExponentialRand(thread, min, max, parameter));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else /* this means an error somewhere in the parsing phase... */
|
|
||||||
{
|
|
||||||
fprintf(stderr, "%s: invalid arguments for \\setrandom\n",
|
|
||||||
argv[0]);
|
|
||||||
st->ecnt++;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!putVariable(st, argv[0], argv[1], res))
|
|
||||||
{
|
|
||||||
st->ecnt++;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
st->listen = true;
|
|
||||||
}
|
|
||||||
else if (pg_strcasecmp(argv[0], "set") == 0)
|
|
||||||
{
|
{
|
||||||
char res[64];
|
char res[64];
|
||||||
PgBenchExpr *expr = commands[st->state]->expr;
|
PgBenchExpr *expr = commands[st->state]->expr;
|
||||||
@ -2880,43 +2739,7 @@ process_backslash_command(PsqlScanState sstate, const char *source)
|
|||||||
start_offset,
|
start_offset,
|
||||||
end_offset);
|
end_offset);
|
||||||
|
|
||||||
if (pg_strcasecmp(my_command->argv[0], "setrandom") == 0)
|
if (pg_strcasecmp(my_command->argv[0], "sleep") == 0)
|
||||||
{
|
|
||||||
/*--------
|
|
||||||
* parsing:
|
|
||||||
* \setrandom variable min max [uniform]
|
|
||||||
* \setrandom variable min max (gaussian|exponential) parameter
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (my_command->argc < 4)
|
|
||||||
syntax_error(source, lineno, my_command->line, my_command->argv[0],
|
|
||||||
"missing arguments", NULL, -1);
|
|
||||||
|
|
||||||
if (my_command->argc == 4 || /* uniform without/with "uniform"
|
|
||||||
* keyword */
|
|
||||||
(my_command->argc == 5 &&
|
|
||||||
pg_strcasecmp(my_command->argv[4], "uniform") == 0))
|
|
||||||
{
|
|
||||||
/* nothing to do */
|
|
||||||
}
|
|
||||||
else if ( /* argc >= 5 */
|
|
||||||
(pg_strcasecmp(my_command->argv[4], "gaussian") == 0) ||
|
|
||||||
(pg_strcasecmp(my_command->argv[4], "exponential") == 0))
|
|
||||||
{
|
|
||||||
if (my_command->argc < 6)
|
|
||||||
syntax_error(source, lineno, my_command->line, my_command->argv[0],
|
|
||||||
"missing parameter", NULL, -1);
|
|
||||||
else if (my_command->argc > 6)
|
|
||||||
syntax_error(source, lineno, my_command->line, my_command->argv[0],
|
|
||||||
"too many arguments", NULL,
|
|
||||||
offsets[6] - start_offset);
|
|
||||||
}
|
|
||||||
else /* unrecognized distribution argument */
|
|
||||||
syntax_error(source, lineno, my_command->line, my_command->argv[0],
|
|
||||||
"unexpected argument", my_command->argv[4],
|
|
||||||
offsets[4] - start_offset);
|
|
||||||
}
|
|
||||||
else if (pg_strcasecmp(my_command->argv[0], "sleep") == 0)
|
|
||||||
{
|
{
|
||||||
if (my_command->argc < 2)
|
if (my_command->argc < 2)
|
||||||
syntax_error(source, lineno, my_command->line, my_command->argv[0],
|
syntax_error(source, lineno, my_command->line, my_command->argv[0],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user