Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit86c43f4

Browse files
committed
pgbench: Support double constants and functions.
The new functions are pi(), random(), random_exponential(),random_gaussian(), and sqrt(). I was worried that this would beslower than before, but, if anything, it actually turns out to beslightly faster, because we now express the built-in pgbench scriptsusing fewer lines; each \setrandom can be merged into a subsequent\set.Fabien Coelho
1 parent9bd6131 commit86c43f4

File tree

5 files changed

+624
-192
lines changed

5 files changed

+624
-192
lines changed

‎doc/src/sgml/ref/pgbench.sgml

Lines changed: 185 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -815,9 +815,10 @@ pgbench <optional> <replaceable>options</> </optional> <replaceable>dbname</>
815815

816816
<listitem>
817817
<para>
818-
Sets variable <replaceable>varname</> toan integer value calculated
818+
Sets variable <replaceable>varname</> toa value calculated
819819
from <replaceable>expression</>.
820820
The expression may contain integer constants such as <literal>5432</>,
821+
double constants such as <literal>3.14159</>,
821822
references to variables <literal>:</><replaceable>variablename</>,
822823
unary operators (<literal>+</>, <literal>-</>) and binary operators
823824
(<literal>+</>, <literal>-</>, <literal>*</>, <literal>/</>,
@@ -830,7 +831,7 @@ pgbench <optional> <replaceable>options</> </optional> <replaceable>dbname</>
830831
Examples:
831832
<programlisting>
832833
\set ntellers 10 * :scale
833-
\set aid (1021 *:aid) % (100000 * :scale) + 1
834+
\set aid (1021 *random(1, 100000 * :scale)) % (100000 * :scale) + 1
834835
</programlisting></para>
835836
</listitem>
836837
</varlistentry>
@@ -850,66 +851,35 @@ pgbench <optional> <replaceable>options</> </optional> <replaceable>dbname</>
850851
</para>
851852

852853
<para>
853-
By default, or when <literal>uniform</> is specified, all values in the
854-
range are drawn with equal probability. Specifying <literal>gaussian</>
855-
or <literal>exponential</> options modifies this behavior; each
856-
requires a mandatory parameter which determines the precise shape of the
857-
distribution.
858-
</para>
854+
<itemizedlist>
855+
<listitem>
856+
<para>
857+
<literal>\setrandom n 1 10</> or <literal>\setrandom n 1 10 uniform</>
858+
is equivalent to <literal>\set n random(1, 10)</> and uses a uniform
859+
distribution.
860+
</para>
861+
</listitem>
859862

860-
<para>
861-
For a Gaussian distribution, the interval is mapped onto a standard
862-
normal distribution (the classical bell-shaped Gaussian curve) truncated
863-
at <literal>-parameter</> on the left and <literal>+parameter</>
864-
on the right.
865-
Values in the middle of the interval are more likely to be drawn.
866-
To be precise, if <literal>PHI(x)</> is the cumulative distribution
867-
function of the standard normal distribution, with mean <literal>mu</>
868-
defined as <literal>(max + min) / 2.0</>, with
869-
<literallayout>
870-
f(x) = PHI(2.0 * parameter * (x - mu) / (max - min + 1)) /
871-
(2.0 * PHI(parameter) - 1.0)
872-
</literallayout>
873-
then value <replaceable>i</> between <replaceable>min</> and
874-
<replaceable>max</> inclusive is drawn with probability:
875-
<literal>f(i + 0.5) - f(i - 0.5)</>.
876-
Intuitively, the larger <replaceable>parameter</>, the more
877-
frequently values close to the middle of the interval are drawn, and the
878-
less frequently values close to the <replaceable>min</> and
879-
<replaceable>max</> bounds. About 67% of values are drawn from the
880-
middle <literal>1.0 / parameter</>, that is a relative
881-
<literal>0.5 / parameter</> around the mean, and 95% in the middle
882-
<literal>2.0 / parameter</>, that is a relative
883-
<literal>1.0 / parameter</> around the mean; for instance, if
884-
<replaceable>parameter</> is 4.0, 67% of values are drawn from the
885-
middle quarter (1.0 / 4.0) of the interval (i.e. from
886-
<literal>3.0 / 8.0</> to <literal>5.0 / 8.0</>) and 95% from
887-
the middle half (<literal>2.0 / 4.0</>) of the interval (second and
888-
third quartiles). The minimum <replaceable>parameter</> is 2.0 for
889-
performance of the Box-Muller transform.
890-
</para>
863+
<listitem>
864+
<para>
865+
<literal>\setrandom n 1 10 exponential 3.0</> is equivalent to
866+
<literal>\set n random_exponential(1, 10, 3.0)</> and uses an
867+
exponential distribution.
868+
</para>
869+
</listitem>
891870

892-
<para>
893-
For an exponential distribution, <replaceable>parameter</>
894-
controls the distribution by truncating a quickly-decreasing
895-
exponential distribution at <replaceable>parameter</>, and then
896-
projecting onto integers between the bounds.
897-
To be precise, with
898-
<literallayout>
899-
f(x) = exp(-parameter * (x - min) / (max - min + 1)) / (1.0 - exp(-parameter))
900-
</literallayout>
901-
Then value <replaceable>i</> between <replaceable>min</> and
902-
<replaceable>max</> inclusive is drawn with probability:
903-
<literal>f(x) - f(x + 1)</>.
904-
Intuitively, the larger <replaceable>parameter</>, the more
905-
frequently values close to <replaceable>min</> are accessed, and the
906-
less frequently values close to <replaceable>max</> are accessed.
907-
The closer to 0 <replaceable>parameter</>, the flatter (more uniform)
908-
the access distribution.
909-
A crude approximation of the distribution is that the most frequent 1%
910-
values in the range, close to <replaceable>min</>, are drawn
911-
<replaceable>parameter</>% of the time.
912-
<replaceable>parameter</> value must be strictly positive.
871+
<listitem>
872+
<para>
873+
<literal>\setrandom n 1 10 gaussian 2.0</> is equivalent to
874+
<literal>\set n random_gaussian(1, 10, 2.0)</>, and uses a gaussian
875+
distribution.
876+
</para>
877+
</listitem>
878+
</itemizedlist>
879+
880+
See the documentation of these functions below for further information
881+
about the precise shape of these distributions, depending on the value
882+
of the parameter.
913883
</para>
914884

915885
<para>
@@ -990,34 +960,6 @@ f(x) = exp(-parameter * (x - min) / (max - min + 1)) / (1.0 - exp(-parameter))
990960
</listitem>
991961
</varlistentry>
992962
</variablelist>
993-
994-
<para>
995-
As an example, the full definition of the built-in TPC-B-like
996-
transaction is:
997-
998-
<programlisting>
999-
\set nbranches :scale
1000-
\set ntellers 10 * :scale
1001-
\set naccounts 100000 * :scale
1002-
\setrandom aid 1 :naccounts
1003-
\setrandom bid 1 :nbranches
1004-
\setrandom tid 1 :ntellers
1005-
\setrandom delta -5000 5000
1006-
BEGIN;
1007-
UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;
1008-
SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
1009-
UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;
1010-
UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;
1011-
INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
1012-
END;
1013-
</programlisting>
1014-
1015-
This script allows each iteration of the transaction to reference
1016-
different, randomly-chosen rows. (This example also shows why it's
1017-
important for each client session to have its own variables &mdash;
1018-
otherwise they'd not be independently touching different rows.)
1019-
</para>
1020-
1021963
</refsect2>
1022964

1023965
<refsect2 id="pgbench-builtin-functions">
@@ -1046,16 +988,30 @@ END;
1046988
<row>
1047989
<entry><literal><function>abs(<replaceable>a</>)</></></>
1048990
<entry>same as <replaceable>a</></>
1049-
<entry>integer value</>
991+
<entry>integeror double absolutevalue</>
1050992
<entry><literal>abs(-17)</></>
1051993
<entry><literal>17</></>
1052994
</row>
1053995
<row>
1054996
<entry><literal><function>debug(<replaceable>a</>)</></></>
1055997
<entry>same as <replaceable>a</> </>
1056998
<entry>print to <systemitem>stderr</systemitem> the given argument</>
1057-
<entry><literal>debug(5432)</></>
1058-
<entry><literal>5432</></>
999+
<entry><literal>debug(5432.1)</></>
1000+
<entry><literal>5432.1</></>
1001+
</row>
1002+
<row>
1003+
<entry><literal><function>double(<replaceable>i</>)</></></>
1004+
<entry>double</>
1005+
<entry>cast to double</>
1006+
<entry><literal>double(5432)</></>
1007+
<entry><literal>5432.0</></>
1008+
</row>
1009+
<row>
1010+
<entry><literal><function>int(<replaceable>x</>)</></></>
1011+
<entry>integer</>
1012+
<entry>cast to int</>
1013+
<entry><literal>int(5.4 + 3.8)</></>
1014+
<entry><literal>9</></>
10591015
</row>
10601016
<row>
10611017
<entry><literal><function>max(<replaceable>i</> [, <replaceable>...</> ] )</></></>
@@ -1071,9 +1027,143 @@ END;
10711027
<entry><literal>min(5, 4, 3, 2)</></>
10721028
<entry><literal>2</></>
10731029
</row>
1030+
<row>
1031+
<entry><literal><function>pi()</></></>
1032+
<entry>double</>
1033+
<entry>value of the PI constant</>
1034+
<entry><literal>pi()</></>
1035+
<entry><literal>3.14159265358979323846</></>
1036+
</row>
1037+
<row>
1038+
<entry><literal><function>random(<replaceable>lb</>, <replaceable>ub</>)</></></>
1039+
<entry>integer</>
1040+
<entry>uniformly-distributed random integer in <literal>[lb, ub]</></>
1041+
<entry><literal>random(1, 10)</></>
1042+
<entry>an integer between <literal>1</> and <literal>10</></>
1043+
</row>
1044+
<row>
1045+
<entry><literal><function>random_exponential(<replaceable>lb</>, <replaceable>ub</>, <replaceable>parameter</>)</></></>
1046+
<entry>integer</>
1047+
<entry>exponentially-distributed random integer in <literal>[lb, ub]</>,
1048+
see below</>
1049+
<entry><literal>random_exponential(1, 10, 3.0)</></>
1050+
<entry>an integer between <literal>1</> and <literal>10</></>
1051+
</row>
1052+
<row>
1053+
<entry><literal><function>random_gaussian(<replaceable>lb</>, <replaceable>ub</>, <replaceable>parameter</>)</></></>
1054+
<entry>integer</>
1055+
<entry>gaussian-distributed random integer in <literal>[lb, ub]</>,
1056+
see below</>
1057+
<entry><literal>random_gaussian(1, 10, 2.5)</></>
1058+
<entry>an integer between <literal>1</> and <literal>10</></>
1059+
</row>
1060+
<row>
1061+
<entry><literal><function>sqrt(<replaceable>x</>)</></></>
1062+
<entry>double</>
1063+
<entry>square root</>
1064+
<entry><literal>sqrt(2.0)</></>
1065+
<entry><literal>1.414213562</></>
1066+
</row>
10741067
</tbody>
10751068
</tgroup>
10761069
</table>
1070+
1071+
<para>
1072+
The <literal>random</> function generates values using a uniform
1073+
distribution, that is all the values are drawn within the specified
1074+
range with equal probability. The <literal>random_exponential</> and
1075+
<literal>random_gaussian</> functions require an additional double
1076+
parameter which determines the precise shape of the distribution.
1077+
</para>
1078+
1079+
<itemizedlist>
1080+
<listitem>
1081+
<para>
1082+
For an exponential distribution, <replaceable>parameter</>
1083+
controls the distribution by truncating a quickly-decreasing
1084+
exponential distribution at <replaceable>parameter</>, and then
1085+
projecting onto integers between the bounds.
1086+
To be precise, with
1087+
<literallayout>
1088+
f(x) = exp(-parameter * (x - min) / (max - min + 1)) / (1 - exp(-parameter))
1089+
</literallayout>
1090+
Then value <replaceable>i</> between <replaceable>min</> and
1091+
<replaceable>max</> inclusive is drawn with probability:
1092+
<literal>f(x) - f(x + 1)</>.
1093+
</para>
1094+
1095+
<para>
1096+
Intuitively, the larger the <replaceable>parameter</>, the more
1097+
frequently values close to <replaceable>min</> are accessed, and the
1098+
less frequently values close to <replaceable>max</> are accessed.
1099+
The closer to 0 <replaceable>parameter</> is, the flatter (more
1100+
uniform) the access distribution.
1101+
A crude approximation of the distribution is that the most frequent 1%
1102+
values in the range, close to <replaceable>min</>, are drawn
1103+
<replaceable>parameter</>% of the time.
1104+
The <replaceable>parameter</> value must be strictly positive.
1105+
</para>
1106+
</listitem>
1107+
1108+
<listitem>
1109+
<para>
1110+
For a Gaussian distribution, the interval is mapped onto a standard
1111+
normal distribution (the classical bell-shaped Gaussian curve) truncated
1112+
at <literal>-parameter</> on the left and <literal>+parameter</>
1113+
on the right.
1114+
Values in the middle of the interval are more likely to be drawn.
1115+
To be precise, if <literal>PHI(x)</> is the cumulative distribution
1116+
function of the standard normal distribution, with mean <literal>mu</>
1117+
defined as <literal>(max + min) / 2.0</>, with
1118+
<literallayout>
1119+
f(x) = PHI(2.0 * parameter * (x - mu) / (max - min + 1)) /
1120+
(2.0 * PHI(parameter) - 1)
1121+
</literallayout>
1122+
then value <replaceable>i</> between <replaceable>min</> and
1123+
<replaceable>max</> inclusive is drawn with probability:
1124+
<literal>f(i + 0.5) - f(i - 0.5)</>.
1125+
Intuitively, the larger the <replaceable>parameter</>, the more
1126+
frequently values close to the middle of the interval are drawn, and the
1127+
less frequently values close to the <replaceable>min</> and
1128+
<replaceable>max</> bounds. About 67% of values are drawn from the
1129+
middle <literal>1.0 / parameter</>, that is a relative
1130+
<literal>0.5 / parameter</> around the mean, and 95% in the middle
1131+
<literal>2.0 / parameter</>, that is a relative
1132+
<literal>1.0 / parameter</> around the mean; for instance, if
1133+
<replaceable>parameter</> is 4.0, 67% of values are drawn from the
1134+
middle quarter (1.0 / 4.0) of the interval (i.e. from
1135+
<literal>3.0 / 8.0</> to <literal>5.0 / 8.0</>) and 95% from
1136+
the middle half (<literal>2.0 / 4.0</>) of the interval (second and third
1137+
quartiles). The minimum <replaceable>parameter</> is 2.0 for performance
1138+
of the Box-Muller transform.
1139+
</para>
1140+
</listitem>
1141+
</itemizedlist>
1142+
1143+
<para>
1144+
As an example, the full definition of the built-in TPC-B-like
1145+
transaction is:
1146+
1147+
<programlisting>
1148+
\set aid random(1, 100000 * :scale)
1149+
\set bid random(1, 1 * :scale)
1150+
\set tid random(1, 10 * :scale)
1151+
\set delta random(-5000, 5000)
1152+
BEGIN;
1153+
UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;
1154+
SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
1155+
UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;
1156+
UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;
1157+
INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
1158+
END;
1159+
</programlisting>
1160+
1161+
This script allows each iteration of the transaction to reference
1162+
different, randomly-chosen rows. (This example also shows why it's
1163+
important for each client session to have its own variables &mdash;
1164+
otherwise they'd not be independently touching different rows.)
1165+
</para>
1166+
10771167
</refsect2>
10781168

10791169
<refsect2>
@@ -1223,13 +1313,10 @@ tps = 618.764555 (including connections establishing)
12231313
tps = 622.977698 (excluding connections establishing)
12241314
script statistics:
12251315
- statement latencies in milliseconds:
1226-
0.004386 \set nbranches 1 * :scale
1227-
0.001343 \set ntellers 10 * :scale
1228-
0.001212 \set naccounts 100000 * :scale
1229-
0.001310 \setrandom aid 1 :naccounts
1230-
0.001073 \setrandom bid 1 :nbranches
1231-
0.001005 \setrandom tid 1 :ntellers
1232-
0.001078 \setrandom delta -5000 5000
1316+
0.002522 \set aid random(1, 100000 * :scale)
1317+
0.005459 \set bid random(1, 1 * :scale)
1318+
0.002348 \set tid random(1, 10 * :scale)
1319+
0.001078 \set delta random(-5000, 5000)
12331320
0.326152 BEGIN;
12341321
0.603376 UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;
12351322
0.454643 SELECT abalance FROM pgbench_accounts WHERE aid = :aid;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp