Skip to content

Commit 6260cc5

Browse files
committed
pgbench: add \cset and \gset commands
These commands allow assignment of values produced by queries to pgbench variables, where they can be used by further commands. \gset terminates a command sequence (just like a bare semicolon); \cset separates multiple queries in a compound command, like an escaped semicolon (\;). A prefix can be provided to the \-command and is prepended to the name of each output column to produce the final variable name. This feature allows pgbench scripts to react meaningfully to the actual database contents, allowing more powerful benchmarks to be written. Authors: Fabien Coelho, Álvaro Herrera Reviewed-by: Amit Langote <[email protected]> Reviewed-by: Stephen Frost <[email protected]> Reviewed-by: Pavel Stehule <[email protected]> Reviewed-by: Tom Lane <[email protected]> Reviewed-by: Tatsuo Ishii <[email protected]> Reviewed-by: Rafia Sabih <[email protected]> Discussion: https://postgr.es/m/alpine.DEB.2.20.1607091005330.3412@sto
1 parent e1c1d54 commit 6260cc5

File tree

6 files changed

+602
-137
lines changed

6 files changed

+602
-137
lines changed

doc/src/sgml/ref/pgbench.sgml

+85
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,91 @@ pgbench <optional> <replaceable>options</replaceable> </optional> <replaceable>d
954954
</para>
955955

956956
<variablelist>
957+
<varlistentry id='pgbench-metacommand-cset'>
958+
<term>
959+
<literal>\cset [<replaceable>prefix</replaceable>]</literal>
960+
</term>
961+
962+
<listitem>
963+
<para>
964+
This command may be used to end SQL queries, replacing an embedded
965+
semicolon (<literal>\;</literal>) within a compound SQL command.
966+
</para>
967+
968+
<para>
969+
When this command is used, the preceding SQL query is expected to
970+
return one row, the columns of which are stored into variables named after
971+
column names, and prefixed with <replaceable>prefix</replaceable> if provided.
972+
</para>
973+
974+
<para>
975+
The following example sends four queries as one compound SQL command,
976+
inducing one message sent at the protocol level.
977+
The result of the first query is stored into variable <replaceable>one</replaceable>,
978+
the results of the third query are stored into variables <replaceable>z_three</replaceable>
979+
and <replaceable>z_four</replaceable>,
980+
whereas the results of the other queries are discarded.
981+
<programlisting>
982+
-- compound of four queries
983+
SELECT 1 AS one \cset
984+
SELECT 2 AS two \;
985+
SELECT 3 AS three, 4 AS four \cset z_
986+
SELECT 5;
987+
</programlisting>
988+
</para>
989+
990+
<note>
991+
<para>
992+
<literal>\cset</literal> does not work when empty SQL queries appear
993+
within a compound SQL command.
994+
</para>
995+
</note>
996+
</listitem>
997+
</varlistentry>
998+
999+
<varlistentry id='pgbench-metacommand-gset'>
1000+
<term>
1001+
<literal>\gset [<replaceable>prefix</replaceable>]</literal>
1002+
</term>
1003+
1004+
<listitem>
1005+
<para>
1006+
This command may be used to end SQL queries, replacing a final semicolon
1007+
(<literal>;</literal>).
1008+
</para>
1009+
1010+
<para>
1011+
When this command is used, the preceding SQL query is expected to
1012+
return one row, the columns of which are stored into variables named after
1013+
column names, and prefixed with <replaceable>prefix</replaceable> if provided.
1014+
</para>
1015+
1016+
<para>
1017+
The following example puts the final account balance from the first query
1018+
into variable <replaceable>abalance</replaceable>, and fills variables
1019+
<replaceable>p_two</replaceable> and <replaceable>p_three</replaceable>
1020+
with integers from the last query.
1021+
The result of the second query is discarded.
1022+
<programlisting>
1023+
UPDATE pgbench_accounts
1024+
SET abalance = abalance + :delta
1025+
WHERE aid = :aid
1026+
RETURNING abalance \gset
1027+
-- compound of two queries
1028+
SELECT 1 \;
1029+
SELECT 2 AS two, 3 AS three \gset p_
1030+
</programlisting>
1031+
</para>
1032+
1033+
<note>
1034+
<para>
1035+
<literal>\gset</literal> does not work when empty SQL queries appear
1036+
within a compound SQL command.
1037+
</para>
1038+
</note>
1039+
</listitem>
1040+
</varlistentry>
1041+
9571042
<varlistentry>
9581043
<term><literal>\if</literal> <replaceable class="parameter">expression</replaceable></term>
9591044
<term><literal>\elif</literal> <replaceable class="parameter">expression</replaceable></term>

0 commit comments

Comments
 (0)