Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit bc7fa0c

Browse files
committed
Improve scripting language in pgbench
Added: - variable now might contain integer, double, boolean and null values - functions ln, exp - logical AND/OR/NOT - bitwise AND/OR/NOT/XOR - bit right/left shift - comparison operators - IS [NOT] (NULL|TRUE|FALSE) - conditional choice (in form of when/case/then) New operations and functions allow to implement more complicated test scenario. Author: Fabien Coelho with minor editorization by me Reviewed-By: Pavel Stehule, Jeevan Ladhe, me Discussion: https://www.postgresql.org/message-id/flat/alpine.DEB.2.10.1604030742390.31618@sto
1 parent 63008b1 commit bc7fa0c

File tree

6 files changed

+1026
-126
lines changed

6 files changed

+1026
-126
lines changed

doc/src/sgml/ref/pgbench.sgml

+217-6
Original file line numberDiff line numberDiff line change
@@ -904,14 +904,32 @@ pgbench <optional> <replaceable>options</replaceable> </optional> <replaceable>d
904904
<para>
905905
Sets variable <replaceable>varname</replaceable> to a value calculated
906906
from <replaceable>expression</replaceable>.
907-
The expression may contain integer constants such as <literal>5432</literal>,
907+
The expression may contain the <literal>NULL</literal> constant,
908+
boolean constants <literal>TRUE</literal> and <literal>FALSE</literal>,
909+
integer constants such as <literal>5432</literal>,
908910
double constants such as <literal>3.14159</literal>,
909911
references to variables <literal>:</literal><replaceable>variablename</replaceable>,
910-
unary operators (<literal>+</literal>, <literal>-</literal>) and binary operators
911-
(<literal>+</literal>, <literal>-</literal>, <literal>*</literal>, <literal>/</literal>,
912-
<literal>%</literal>) with their usual precedence and associativity,
913-
<link linkend="pgbench-builtin-functions">function calls</link>, and
914-
parentheses.
912+
<link linkend="pgbench-builtin-operators">operators</link>
913+
with their usual SQL precedence and associativity,
914+
<link linkend="pgbench-builtin-functions">function calls</link>,
915+
SQL <link linkend="functions-case"><token>CASE</token> generic conditional
916+
expressions</link> and parentheses.
917+
</para>
918+
919+
<para>
920+
Functions and most operators return <literal>NULL</literal> on
921+
<literal>NULL</literal> input.
922+
</para>
923+
924+
<para>
925+
For conditional purposes, non zero numerical values are
926+
<literal>TRUE</literal>, zero numerical values and <literal>NULL</literal>
927+
are <literal>FALSE</literal>.
928+
</para>
929+
930+
<para>
931+
When no final <token>ELSE</token> clause is provided to a
932+
<token>CASE</token>, the default value is <literal>NULL</literal>.
915933
</para>
916934

917935
<para>
@@ -920,6 +938,7 @@ pgbench <optional> <replaceable>options</replaceable> </optional> <replaceable>d
920938
\set ntellers 10 * :scale
921939
\set aid (1021 * random(1, 100000 * :scale)) % \
922940
(100000 * :scale) + 1
941+
\set divx CASE WHEN :x &lt;&gt; 0 THEN :y/:x ELSE NULL END
923942
</programlisting></para>
924943
</listitem>
925944
</varlistentry>
@@ -996,6 +1015,177 @@ pgbench <optional> <replaceable>options</replaceable> </optional> <replaceable>d
9961015
</variablelist>
9971016
</refsect2>
9981017

1018+
<refsect2 id="pgbench-builtin-operators">
1019+
<title>Built-In Operators</title>
1020+
1021+
<para>
1022+
The arithmetic, bitwise, comparison and logical operators listed in
1023+
<xref linkend="pgbench-operators"/> are built into <application>pgbench</application>
1024+
and may be used in expressions appearing in
1025+
<link linkend="pgbench-metacommand-set"><literal>\set</literal></link>.
1026+
</para>
1027+
1028+
<table id="pgbench-operators">
1029+
<title>pgbench Operators by increasing precedence</title>
1030+
<tgroup cols="4">
1031+
<thead>
1032+
<row>
1033+
<entry>Operator</entry>
1034+
<entry>Description</entry>
1035+
<entry>Example</entry>
1036+
<entry>Result</entry>
1037+
</row>
1038+
</thead>
1039+
<tbody>
1040+
<row>
1041+
<entry><literal>OR</literal></entry>
1042+
<entry>logical or</entry>
1043+
<entry><literal>5 or 0</literal></entry>
1044+
<entry><literal>TRUE</literal></entry>
1045+
</row>
1046+
<row>
1047+
<entry><literal>AND</literal></entry>
1048+
<entry>logical and</entry>
1049+
<entry><literal>3 and 0</literal></entry>
1050+
<entry><literal>FALSE</literal></entry>
1051+
</row>
1052+
<row>
1053+
<entry><literal>NOT</literal></entry>
1054+
<entry>logical not</entry>
1055+
<entry><literal>not false</literal></entry>
1056+
<entry><literal>TRUE</literal></entry>
1057+
</row>
1058+
<row>
1059+
<entry><literal>IS [NOT] (NULL|TRUE|FALSE)</literal></entry>
1060+
<entry>value tests</entry>
1061+
<entry><literal>1 is null</literal></entry>
1062+
<entry><literal>FALSE</literal></entry>
1063+
</row>
1064+
<row>
1065+
<entry><literal>ISNULL|NOTNULL</literal></entry>
1066+
<entry>null tests</entry>
1067+
<entry><literal>1 notnull</literal></entry>
1068+
<entry><literal>TRUE</literal></entry>
1069+
</row>
1070+
<row>
1071+
<entry><literal>=</literal></entry>
1072+
<entry>is equal</entry>
1073+
<entry><literal>5 = 4</literal></entry>
1074+
<entry><literal>FALSE</literal></entry>
1075+
</row>
1076+
<row>
1077+
<entry><literal>&lt;&gt;</literal></entry>
1078+
<entry>is not equal</entry>
1079+
<entry><literal>5 &lt;&gt; 4</literal></entry>
1080+
<entry><literal>TRUE</literal></entry>
1081+
</row>
1082+
<row>
1083+
<entry><literal>!=</literal></entry>
1084+
<entry>is not equal</entry>
1085+
<entry><literal>5 != 5</literal></entry>
1086+
<entry><literal>FALSE</literal></entry>
1087+
</row>
1088+
<row>
1089+
<entry><literal>&lt;</literal></entry>
1090+
<entry>lower than</entry>
1091+
<entry><literal>5 &lt; 4</literal></entry>
1092+
<entry><literal>FALSE</literal></entry>
1093+
</row>
1094+
<row>
1095+
<entry><literal>&lt;=</literal></entry>
1096+
<entry>lower or equal</entry>
1097+
<entry><literal>5 &lt;= 4</literal></entry>
1098+
<entry><literal>FALSE</literal></entry>
1099+
</row>
1100+
<row>
1101+
<entry><literal>&gt;</literal></entry>
1102+
<entry>greater than</entry>
1103+
<entry><literal>5 &gt; 4</literal></entry>
1104+
<entry><literal>TRUE</literal></entry>
1105+
</row>
1106+
<row>
1107+
<entry><literal>&gt;=</literal></entry>
1108+
<entry>greater or equal</entry>
1109+
<entry><literal>5 &gt;= 4</literal></entry>
1110+
<entry><literal>TRUE</literal></entry>
1111+
</row>
1112+
<row>
1113+
<entry><literal>|</literal></entry>
1114+
<entry>integer bitwise OR</entry>
1115+
<entry><literal>1 | 2</literal></entry>
1116+
<entry><literal>3</literal></entry>
1117+
</row>
1118+
<row>
1119+
<entry><literal>#</literal></entry>
1120+
<entry>integer bitwise XOR</entry>
1121+
<entry><literal>1 # 3</literal></entry>
1122+
<entry><literal>2</literal></entry>
1123+
</row>
1124+
<row>
1125+
<entry><literal>&amp;</literal></entry>
1126+
<entry>integer bitwise AND</entry>
1127+
<entry><literal>1 &amp; 3</literal></entry>
1128+
<entry><literal>1</literal></entry>
1129+
</row>
1130+
<row>
1131+
<entry><literal>~</literal></entry>
1132+
<entry>integer bitwise NOT</entry>
1133+
<entry><literal>~ 1</literal></entry>
1134+
<entry><literal>-2</literal></entry>
1135+
</row>
1136+
<row>
1137+
<entry><literal>&lt;&lt;</literal></entry>
1138+
<entry>integer bitwise shift left</entry>
1139+
<entry><literal>1 &lt;&lt; 2</literal></entry>
1140+
<entry><literal>4</literal></entry>
1141+
</row>
1142+
<row>
1143+
<entry><literal>&gt;&gt;</literal></entry>
1144+
<entry>integer bitwise shift right</entry>
1145+
<entry><literal>8 &gt;&gt; 2</literal></entry>
1146+
<entry><literal>2</literal></entry>
1147+
</row>
1148+
<row>
1149+
<entry><literal>+</literal></entry>
1150+
<entry>addition</entry>
1151+
<entry><literal>5 + 4</literal></entry>
1152+
<entry><literal>9</literal></entry>
1153+
</row>
1154+
<row>
1155+
<entry><literal>-</literal></entry>
1156+
<entry>substraction</entry>
1157+
<entry><literal>3 - 2.0</literal></entry>
1158+
<entry><literal>1.0</literal></entry>
1159+
</row>
1160+
<row>
1161+
<entry><literal>*</literal></entry>
1162+
<entry>multiplication</entry>
1163+
<entry><literal>5 * 4</literal></entry>
1164+
<entry><literal>20</literal></entry>
1165+
</row>
1166+
<row>
1167+
<entry><literal>/</literal></entry>
1168+
<entry>division (integer truncates the results)</entry>
1169+
<entry><literal>5 / 3</literal></entry>
1170+
<entry><literal>1</literal></entry>
1171+
</row>
1172+
<row>
1173+
<entry><literal>%</literal></entry>
1174+
<entry>modulo</entry>
1175+
<entry><literal>3 % 2</literal></entry>
1176+
<entry><literal>1</literal></entry>
1177+
</row>
1178+
<row>
1179+
<entry><literal>-</literal></entry>
1180+
<entry>opposite</entry>
1181+
<entry><literal>- 2.0</literal></entry>
1182+
<entry><literal>-2.0</literal></entry>
1183+
</row>
1184+
</tbody>
1185+
</tgroup>
1186+
</table>
1187+
</refsect2>
1188+
9991189
<refsect2 id="pgbench-builtin-functions">
10001190
<title>Built-In Functions</title>
10011191

@@ -1041,6 +1231,13 @@ pgbench <optional> <replaceable>options</replaceable> </optional> <replaceable>d
10411231
<entry><literal>double(5432)</literal></entry>
10421232
<entry><literal>5432.0</literal></entry>
10431233
</row>
1234+
<row>
1235+
<entry><literal><function>exp(<replaceable>x</replaceable>)</function></literal></entry>
1236+
<entry>double</entry>
1237+
<entry>exponential</entry>
1238+
<entry><literal>exp(1.0)</literal></entry>
1239+
<entry><literal>2.718281828459045</literal></entry>
1240+
</row>
10441241
<row>
10451242
<entry><literal><function>greatest(<replaceable>a</replaceable> [, <replaceable>...</replaceable> ] )</function></literal></entry>
10461243
<entry>double if any <replaceable>a</replaceable> is double, else integer</entry>
@@ -1062,6 +1259,20 @@ pgbench <optional> <replaceable>options</replaceable> </optional> <replaceable>d
10621259
<entry><literal>least(5, 4, 3, 2.1)</literal></entry>
10631260
<entry><literal>2.1</literal></entry>
10641261
</row>
1262+
<row>
1263+
<entry><literal><function>ln(<replaceable>x</replaceable>)</function></literal></entry>
1264+
<entry>double</entry>
1265+
<entry>natural logarithm</entry>
1266+
<entry><literal>ln(2.718281828459045)</literal></entry>
1267+
<entry><literal>1.0</literal></entry>
1268+
</row>
1269+
<row>
1270+
<entry><literal><function>mod(<replaceable>i</replaceable>, <replaceable>bj</replaceable>)</function></literal></entry>
1271+
<entry>integer</entry>
1272+
<entry>modulo</entry>
1273+
<entry><literal>mod(54, 32)</literal></entry>
1274+
<entry><literal>22</literal></entry>
1275+
</row>
10651276
<row>
10661277
<entry><literal><function>pi()</function></literal></entry>
10671278
<entry>double</entry>

0 commit comments

Comments
 (0)