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

Commit 3ce3575

Browse files
committed
psql: Add pipeline status to prompt and some state variables
This commit adds %P to psql prompts, able to report the status of a pipeline depending on PQpipelineStatus(): on, off or abort. The following variables are added to report the state of an ongoing pipeline: - PIPELINE_SYNC_COUNT: reports the number of piped syncs. - PIPELINE_COMMAND_COUNT: reports the number of piped commands, a command being either \bind, \bind_named, \close or \parse. - PIPELINE_RESULT_COUNT: reports the results available to read with \getresults. These variables can be used with \echo or in a prompt, using "%:name:" in PROMPT1, PROMPT2 or PROMPT3. Some basic regression tests are added for these. The suggestion to use variables to show the details about the status counters comes from me. The original patch proposed was less extensible, hardcoding the output in the prompt. Author: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com> Discussion: https://postgr.es/m/CAO6_XqroE7JuMEm1sWz55rp9fAYX2JwmcP_3m_v51vnOFdsLiQ@mail.gmail.com
1 parent cbb9086 commit 3ce3575

File tree

6 files changed

+128
-1
lines changed

6 files changed

+128
-1
lines changed

doc/src/sgml/ref/psql-ref.sgml

+50
Original file line numberDiff line numberDiff line change
@@ -3728,6 +3728,12 @@ testdb=&gt; <userinput>\setenv LESS -imx4F</userinput>
37283728
generate one result to get.
37293729
</para>
37303730

3731+
<para>
3732+
When pipeline mode is active, a dedicated prompt variable is available
3733+
to report the pipeline status.
3734+
See <xref linkend="app-psql-prompting-p-uc"/> for more details
3735+
</para>
3736+
37313737
<para>
37323738
Example:
37333739
<programlisting>
@@ -4502,6 +4508,39 @@ bar
45024508
</listitem>
45034509
</varlistentry>
45044510

4511+
<varlistentry id="app-psql-variables-pipeline-command-count">
4512+
<term><varname>PIPELINE_COMMAND_COUNT</varname></term>
4513+
<listitem>
4514+
<para>
4515+
The number of commands generated by <literal>\bind</literal>,
4516+
<literal>\bind_named</literal>, <literal>\close</literal> or
4517+
<literal>\parse</literal> queued in an ongoing pipeline.
4518+
</para>
4519+
</listitem>
4520+
</varlistentry>
4521+
4522+
<varlistentry id="app-psql-variables-pipeline-result-count">
4523+
<term><varname>PIPELINE_RESULT_COUNT</varname></term>
4524+
<listitem>
4525+
<para>
4526+
The number of commands of an ongoing pipeline that were followed
4527+
by either a <command>\flushrequest</command> or a
4528+
<command>\syncpipeline</command>, forcing the server to send the
4529+
results. These results can be retrieved with
4530+
<command>\getresults</command>.
4531+
</para>
4532+
</listitem>
4533+
</varlistentry>
4534+
4535+
<varlistentry id="app-psql-variables-pipeline-sync-count">
4536+
<term><varname>PIPELINE_SYNC_COUNT</varname></term>
4537+
<listitem>
4538+
<para>
4539+
The number of sync messages queued in an ongoing pipeline.
4540+
</para>
4541+
</listitem>
4542+
</varlistentry>
4543+
45054544
<varlistentry id="app-psql-variables-port">
45064545
<term><varname>PORT</varname></term>
45074546
<listitem>
@@ -4901,6 +4940,17 @@ testdb=&gt; <userinput>INSERT INTO my_table VALUES (:'content');</userinput>
49014940
</listitem>
49024941
</varlistentry>
49034942

4943+
<varlistentry id="app-psql-prompting-p-uc">
4944+
<term><literal>%P</literal></term>
4945+
<listitem>
4946+
<para>
4947+
Pipeline status: <literal>off</literal> when not in a pipeline,
4948+
<literal>on</literal> when in an ongoing pipeline or
4949+
<literal>abort</literal> when in an aborted pipeline.
4950+
</para>
4951+
</listitem>
4952+
</varlistentry>
4953+
49044954
<varlistentry id="app-psql-prompting-r">
49054955
<term><literal>%R</literal></term>
49064956
<listitem>

src/bin/psql/common.c

+26-1
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,26 @@ SetShellResultVariables(int wait_result)
524524
}
525525

526526

527+
/*
528+
* Set special pipeline variables
529+
* - PIPELINE_SYNC_COUNT: The number of piped syncs
530+
* - PIPELINE_COMMAND_COUNT: The number of piped commands
531+
* - PIPELINE_RESULT_COUNT: The number of results available to read
532+
*/
533+
static void
534+
SetPipelineVariables(void)
535+
{
536+
char buf[32];
537+
538+
snprintf(buf, sizeof(buf), "%d", pset.piped_syncs);
539+
SetVariable(pset.vars, "PIPELINE_SYNC_COUNT", buf);
540+
snprintf(buf, sizeof(buf), "%d", pset.piped_commands);
541+
SetVariable(pset.vars, "PIPELINE_COMMAND_COUNT", buf);
542+
snprintf(buf, sizeof(buf), "%d", pset.available_results);
543+
SetVariable(pset.vars, "PIPELINE_RESULT_COUNT", buf);
544+
}
545+
546+
527547
/*
528548
* ClearOrSaveResult
529549
*
@@ -1661,6 +1681,8 @@ ExecQueryAndProcessResults(const char *query,
16611681

16621682
CheckConnection();
16631683

1684+
SetPipelineVariables();
1685+
16641686
return -1;
16651687
}
16661688

@@ -1669,8 +1691,10 @@ ExecQueryAndProcessResults(const char *query,
16691691
{
16701692
/*
16711693
* We are in a pipeline and have not reached the pipeline end, or
1672-
* there was no request to read pipeline results, exit.
1694+
* there was no request to read pipeline results. Update the psql
1695+
* variables tracking the pipeline activity and exit.
16731696
*/
1697+
SetPipelineVariables();
16741698
return 1;
16751699
}
16761700

@@ -2105,6 +2129,7 @@ ExecQueryAndProcessResults(const char *query,
21052129
Assert(pset.available_results == 0);
21062130
}
21072131
Assert(pset.requested_results == 0);
2132+
SetPipelineVariables();
21082133

21092134
/* may need this to recover from conn loss during COPY */
21102135
if (!CheckConnection())

src/bin/psql/prompt.c

+14
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* sockets, "[local:/dir/name]" if not default
3232
* %m - like %M, but hostname only (before first dot), or always "[local]"
3333
* %p - backend pid
34+
* %P - pipeline status: on, off or abort
3435
* %> - database server port number
3536
* %n - database user name
3637
* %s - service
@@ -181,6 +182,19 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
181182
snprintf(buf, sizeof(buf), "%d", pid);
182183
}
183184
break;
185+
/* pipeline status */
186+
case 'P':
187+
{
188+
PGpipelineStatus status = PQpipelineStatus(pset.db);
189+
190+
if (status == PQ_PIPELINE_ON)
191+
strlcpy(buf, "on", sizeof(buf));
192+
else if (status == PQ_PIPELINE_ABORTED)
193+
strlcpy(buf, "abort", sizeof(buf));
194+
else
195+
strlcpy(buf, "off", sizeof(buf));
196+
break;
197+
}
184198

185199
case '0':
186200
case '1':

src/bin/psql/startup.c

+5
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ main(int argc, char *argv[])
205205
SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3);
206206
SetVariableBool(pset.vars, "SHOW_ALL_RESULTS");
207207

208+
/* Initialize pipeline variables */
209+
SetVariable(pset.vars, "PIPELINE_SYNC_COUNT", "0");
210+
SetVariable(pset.vars, "PIPELINE_COMMAND_COUNT", "0");
211+
SetVariable(pset.vars, "PIPELINE_RESULT_COUNT", "0");
212+
208213
parse_psql_options(argc, argv, &options);
209214

210215
/*

src/test/regress/expected/psql_pipeline.out

+22
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,24 @@ SELECT $1, $2 \bind 'val2' 'val3' \g
5757

5858
-- Send multiple syncs
5959
\startpipeline
60+
\echo :PIPELINE_COMMAND_COUNT
61+
0
62+
\echo :PIPELINE_SYNC_COUNT
63+
0
64+
\echo :PIPELINE_RESULT_COUNT
65+
0
6066
SELECT $1 \bind 'val1' \g
6167
\syncpipeline
6268
\syncpipeline
6369
SELECT $1, $2 \bind 'val2' 'val3' \g
6470
\syncpipeline
6571
SELECT $1, $2 \bind 'val4' 'val5' \g
72+
\echo :PIPELINE_COMMAND_COUNT
73+
1
74+
\echo :PIPELINE_SYNC_COUNT
75+
3
76+
\echo :PIPELINE_RESULT_COUNT
77+
2
6678
\endpipeline
6779
?column?
6880
----------
@@ -303,13 +315,21 @@ SELECT $1 \bind 2 \g
303315
SELECT $1 \bind 1 \g
304316
SELECT $1 \bind 2 \g
305317
SELECT $1 \bind 3 \g
318+
\echo :PIPELINE_SYNC_COUNT
319+
0
306320
\syncpipeline
321+
\echo :PIPELINE_SYNC_COUNT
322+
1
323+
\echo :PIPELINE_RESULT_COUNT
324+
3
307325
\getresults 1
308326
?column?
309327
----------
310328
1
311329
(1 row)
312330

331+
\echo :PIPELINE_RESULT_COUNT
332+
2
313333
SELECT $1 \bind 4 \g
314334
\getresults 3
315335
?column?
@@ -322,6 +342,8 @@ SELECT $1 \bind 4 \g
322342
3
323343
(1 row)
324344

345+
\echo :PIPELINE_RESULT_COUNT
346+
0
325347
\endpipeline
326348
?column?
327349
----------

src/test/regress/sql/psql_pipeline.sql

+11
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@ SELECT $1, $2 \bind 'val2' 'val3' \g
2727

2828
-- Send multiple syncs
2929
\startpipeline
30+
\echo :PIPELINE_COMMAND_COUNT
31+
\echo :PIPELINE_SYNC_COUNT
32+
\echo :PIPELINE_RESULT_COUNT
3033
SELECT $1 \bind 'val1' \g
3134
\syncpipeline
3235
\syncpipeline
3336
SELECT $1, $2 \bind 'val2' 'val3' \g
3437
\syncpipeline
3538
SELECT $1, $2 \bind 'val4' 'val5' \g
39+
\echo :PIPELINE_COMMAND_COUNT
40+
\echo :PIPELINE_SYNC_COUNT
41+
\echo :PIPELINE_RESULT_COUNT
3642
\endpipeline
3743

3844
-- \startpipeline should not have any effect if already in a pipeline.
@@ -174,10 +180,15 @@ SELECT $1 \bind 2 \g
174180
SELECT $1 \bind 1 \g
175181
SELECT $1 \bind 2 \g
176182
SELECT $1 \bind 3 \g
183+
\echo :PIPELINE_SYNC_COUNT
177184
\syncpipeline
185+
\echo :PIPELINE_SYNC_COUNT
186+
\echo :PIPELINE_RESULT_COUNT
178187
\getresults 1
188+
\echo :PIPELINE_RESULT_COUNT
179189
SELECT $1 \bind 4 \g
180190
\getresults 3
191+
\echo :PIPELINE_RESULT_COUNT
181192
\endpipeline
182193

183194
-- \syncpipeline count as one command to fetch for \getresults.

0 commit comments

Comments
 (0)