mirror of
https://github.com/postgres/postgres.git
synced 2025-07-30 11:03:19 +03:00
psql: Add support for pipelines
With \bind, \parse, \bind_named and \close, it is possible to issue queries from psql using the extended protocol. However, it was not possible to send these queries using libpq's pipeline mode. This feature has two advantages: - Testing. Pipeline tests were only possible with pgbench, using TAP tests. It now becomes possible to have more SQL tests that are able to stress the backend with pipelines and extended queries. More tests will be added in a follow-up commit that were discussed on some other threads. Some external projects in the community had to implement their own facility to work around this limitation. - Emulation of custom workloads, with more control over the actions taken by a client with libpq APIs. It is possible to emulate more workload patterns to bottleneck the backend with the extended query protocol. This patch adds six new meta-commands to be able to control pipelines: * \startpipeline starts a new pipeline. All extended queries are queued until the end of the pipeline are reached or a sync request is sent and processed. * \endpipeline ends an existing pipeline. All queued commands are sent to the server and all responses are processed by psql. * \syncpipeline queues a synchronisation request, without flushing the commands to the server, equivalent of PQsendPipelineSync(). * \flush, equivalent of PQflush(). * \flushrequest, equivalent of PQsendFlushRequest() * \getresults reads the server's results for the queries in a pipeline. Unsent data is automatically pushed when \getresults is called. It is possible to control the number of results read in a single meta-command execution with an optional parameter, 0 means that all the results should be read. Author: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com> Reviewed-by: Jelte Fennema-Nio <postgres@jeltef.nl> Reviewed-by: Kirill Reshke <reshkekirill@gmail.com> Discussion: https://postgr.es/m/CAO6_XqroE7JuMEm1sWz55rp9fAYX2JwmcP_3m_v51vnOFdsLiQ@mail.gmail.com
This commit is contained in:
@ -3674,6 +3674,73 @@ testdb=> <userinput>\setenv LESS -imx4F</userinput>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry id="app-psql-meta-command-pipeline">
|
||||
<term><literal>\startpipeline</literal></term>
|
||||
<term><literal>\syncpipeline</literal></term>
|
||||
<term><literal>\endpipeline</literal></term>
|
||||
<term><literal>\flushrequest</literal></term>
|
||||
<term><literal>\flush</literal></term>
|
||||
<term><literal>\getresults [ <replaceable class="parameter">number_results</replaceable> ]</literal></term>
|
||||
|
||||
<listitem>
|
||||
<para>
|
||||
This group of commands implements pipelining of SQL statements.
|
||||
A pipeline must begin with a <command>\startpipeline</command>
|
||||
and end with an <command>\endpipeline</command>. In between there
|
||||
may be any number of <command>\syncpipeline</command> commands,
|
||||
which sends a <link linkend="protocol-flow-ext-query">sync message</link>
|
||||
without ending the ongoing pipeline and flushing the send buffer.
|
||||
In pipeline mode, statements are sent to the server without waiting
|
||||
for the results of previous statements.
|
||||
See <xref linkend="libpq-pipeline-mode"/> for more details.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Pipeline mode requires the use of the extended query protocol. All
|
||||
queries need to be sent using the meta-commands
|
||||
<literal>\bind</literal>, <literal>\bind_named</literal>,
|
||||
<literal>\close</literal> or <literal>\parse</literal>. While a
|
||||
pipeline is ongoing, <literal>\g</literal> will append the current
|
||||
query buffer to the pipeline. Other meta-commands like
|
||||
<literal>\gx</literal> or <literal>\gdesc</literal> are not allowed
|
||||
in pipeline mode.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
<command>\flushrequest</command> appends a flush command to the
|
||||
pipeline, allowing to read results with
|
||||
<command>\getresults</command> without issuing a sync or ending the
|
||||
pipeline. <command>\getresults</command> will automatically push
|
||||
unsent data to the server. <command>\flush</command> can be used to
|
||||
manually push unsent data.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
<command>\getresults</command> accepts an optional
|
||||
<replaceable class="parameter">number_results</replaceable> parameter.
|
||||
If provided, only the first
|
||||
<replaceable class="parameter">number_results</replaceable> pending
|
||||
results will be read. If not provided or <literal>0</literal>, all
|
||||
pending results are read. The commands <literal>\bind</literal>,
|
||||
<literal>\bind_named</literal>, <literal>\close</literal>,
|
||||
<literal>\parse</literal> and <literal>\syncpipeline</literal>
|
||||
generate one result to get.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Example:
|
||||
<programlisting>
|
||||
\startpipeline
|
||||
SELECT 1 \bind \g
|
||||
\flushrequest
|
||||
\getresults
|
||||
\endpipeline
|
||||
</programlisting>
|
||||
</para>
|
||||
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
|
||||
<varlistentry id="app-psql-meta-command-t-lc">
|
||||
<term><literal>\t</literal></term>
|
||||
|
Reference in New Issue
Block a user