1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-27 07:42:10 +03:00

psql: Allow queries terminated by semicolons while in pipeline mode

Currently, the only way to pipe queries in an ongoing pipeline (in a
\startpipeline block) is to leverage the meta-commands able to create
extended queries such as \bind, \parse or \bind_named.

While this is good enough for testing the backend with pipelines, it has
been mentioned that it can also be very useful to allow queries
terminated by semicolons to be appended to a pipeline.  For example, it
would be possible to migrate existing psql scripts to use pipelines by
just adding a set of \startpipeline and \endpipeline meta-commands,
making such scripts more efficient.

Doing such a change is proving to be simple in psql: queries terminated
by semicolons can be executed through PQsendQueryParams() without any
parameters set when the pipeline mode is active, instead of
PQsendQuery(), the default, like pgbench.  \watch is still forbidden
while in a pipeline, as it expects its results to be processed
synchronously.

The large portion of this commit consists in providing more test
coverage, with mixes of extended queries appended in a pipeline by \bind
and friends, and queries terminated by semicolons.

This improvement has been suggested by Daniel Vérité.

Author: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
Discussion: https://postgr.es/m/d67b9c19-d009-4a50-8020-1a0ea92366a1@manitou-mail.org
This commit is contained in:
Michael Paquier
2025-03-19 13:34:59 +09:00
parent 0b53c08677
commit 2cce0fe440
5 changed files with 359 additions and 47 deletions

View File

@@ -3698,14 +3698,15 @@ testdb=&gt; <userinput>\setenv LESS -imx4F</userinput>
</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>\sendpipeline</literal> will append the
current query buffer to the pipeline. Other meta-commands like
<literal>\g</literal>, <literal>\gx</literal> or <literal>\gdesc</literal>
are not allowed in pipeline mode.
All queries executed while a pipeline is ongoing use the extended
query protocol. Queries are appended to the pipeline when ending with
a semicolon. The meta-commands <literal>\bind</literal>,
<literal>\bind_named</literal>, <literal>\close</literal> or
<literal>\parse</literal> can be used in an ongoing pipeline. While
a pipeline is ongoing, <literal>\sendpipeline</literal> will append
the current query buffer to the pipeline. Other meta-commands like
<literal>\g</literal>, <literal>\gx</literal> or
<literal>\gdesc</literal> are not allowed in pipeline mode.
</para>
<para>
@@ -3723,10 +3724,7 @@ testdb=&gt; <userinput>\setenv LESS -imx4F</userinput>
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.
pending results are read.
</para>
<para>
@@ -3739,6 +3737,7 @@ testdb=&gt; <userinput>\setenv LESS -imx4F</userinput>
Example:
<programlisting>
\startpipeline
SELECT * FROM pg_class;
SELECT 1 \bind \sendpipeline
\flushrequest
\getresults
@@ -4513,9 +4512,7 @@ bar
<term><varname>PIPELINE_COMMAND_COUNT</varname></term>
<listitem>
<para>
The number of commands generated by <literal>\bind</literal>,
<literal>\bind_named</literal>, <literal>\close</literal> or
<literal>\parse</literal> queued in an ongoing pipeline.
The number of commands queued in an ongoing pipeline.
</para>
</listitem>
</varlistentry>