Skip to content

Commit a25bae0

Browse files
author
Commitfest Bot
committed
[CF 5655] v03 - Allow ';' to push queries in an ongoing psql pipeline
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/5655 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/CAO6_Xqo=fNh0KCRZG7T18OZfjiabeL-t4C6mkXsz-3dAZ1YwTA@mail.gmail.com Author(s): Anthonin Bonnefoy
2 parents 122a9af + 6ef4918 commit a25bae0

File tree

5 files changed

+420
-31
lines changed

5 files changed

+420
-31
lines changed

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3698,14 +3698,19 @@ testdb=&gt; <userinput>\setenv LESS -imx4F</userinput>
36983698
</para>
36993699

37003700
<para>
3701-
Pipeline mode requires the use of the extended query protocol. All
3702-
queries need to be sent using the meta-commands
3703-
<literal>\bind</literal>, <literal>\bind_named</literal>,
3704-
<literal>\close</literal> or <literal>\parse</literal>. While a
3705-
pipeline is ongoing, <literal>\sendpipeline</literal> will append the
3706-
current query buffer to the pipeline. Other meta-commands like
3707-
<literal>\g</literal>, <literal>\gx</literal> or <literal>\gdesc</literal>
3708-
are not allowed in pipeline mode.
3701+
Pipeline mode requires the use of the extended query protocol. Queries
3702+
can be sent using the meta-commands <literal>\bind</literal>,
3703+
<literal>\bind_named</literal>, <literal>\close</literal> or
3704+
<literal>\parse</literal>. While a pipeline is ongoing,
3705+
<literal>\sendpipeline</literal> will append the current query
3706+
buffer to the pipeline. Other meta-commands like <literal>\g</literal>,
3707+
<literal>\gx</literal> or <literal>\gdesc</literal> are not allowed
3708+
in pipeline mode.
3709+
</para>
3710+
3711+
<para>
3712+
Queries can also be sent using <literal>;</literal>. While a pipeline is
3713+
ongoing, they will automatically be sent using extended query protocol.
37093714
</para>
37103715

37113716
<para>

src/bin/psql/command.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3282,6 +3282,13 @@ exec_command_watch(PsqlScanState scan_state, bool active_branch,
32823282
int iter = 0;
32833283
int min_rows = 0;
32843284

3285+
if (PQpipelineStatus(pset.db) != PQ_PIPELINE_OFF)
3286+
{
3287+
pg_log_error("\\watch not allowed in pipeline mode");
3288+
clean_extended_state();
3289+
success = false;
3290+
}
3291+
32853292
/*
32863293
* Parse arguments. We allow either an unlabeled interval or
32873294
* "name=value", where name is from the set ('i', 'interval', 'c',

src/bin/psql/common.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,15 @@ ExecQueryAndProcessResults(const char *query,
16681668
}
16691669
break;
16701670
case PSQL_SEND_QUERY:
1671-
success = PQsendQuery(pset.db, query);
1671+
if (PQpipelineStatus(pset.db) != PQ_PIPELINE_OFF)
1672+
{
1673+
success = PQsendQueryParams(pset.db, query,
1674+
0, NULL, NULL, NULL, NULL, 0);
1675+
if (success)
1676+
pset.piped_commands++;
1677+
}
1678+
else
1679+
success = PQsendQuery(pset.db, query);
16721680
break;
16731681
}
16741682

src/test/regress/expected/psql_pipeline.out

Lines changed: 265 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -388,23 +388,267 @@ SELECT $1 \bind 3 \sendpipeline
388388

389389
\endpipeline
390390
--
391-
-- Pipeline errors
391+
-- Tests pipelining queries with ';'
392392
--
393-
-- \endpipeline outside of pipeline should fail
393+
-- Single query sent with ';'
394+
\startpipeline
395+
SELECT 1;
394396
\endpipeline
395-
cannot send pipeline when not in pipeline mode
396-
-- Query using simple protocol should not be sent and should leave the
397-
-- pipeline usable.
397+
?column?
398+
----------
399+
1
400+
(1 row)
401+
402+
-- Multiple queries sent with ';'
403+
\startpipeline
404+
SELECT 1;
405+
SELECT 2;
406+
SELECT 3;
407+
\endpipeline
408+
?column?
409+
----------
410+
1
411+
(1 row)
412+
413+
?column?
414+
----------
415+
2
416+
(1 row)
417+
418+
?column?
419+
----------
420+
3
421+
(1 row)
422+
423+
-- Multiple queries on the same line can be piped with ';'
424+
\startpipeline
425+
SELECT 1; SELECT 2; SELECT 3
426+
;
427+
\endpipeline
428+
?column?
429+
----------
430+
1
431+
(1 row)
432+
433+
?column?
434+
----------
435+
2
436+
(1 row)
437+
438+
?column?
439+
----------
440+
3
441+
(1 row)
442+
443+
-- Test \flush with queries piped with ';'
444+
\startpipeline
445+
\flush
446+
SELECT 1;
447+
\flush
448+
SELECT 2;
449+
SELECT 3;
450+
\endpipeline
451+
?column?
452+
----------
453+
1
454+
(1 row)
455+
456+
?column?
457+
----------
458+
2
459+
(1 row)
460+
461+
?column?
462+
----------
463+
3
464+
(1 row)
465+
466+
-- Send multiple syncs with queries piped with ';'
467+
\startpipeline
468+
\echo :PIPELINE_COMMAND_COUNT
469+
0
470+
\echo :PIPELINE_SYNC_COUNT
471+
0
472+
\echo :PIPELINE_RESULT_COUNT
473+
0
474+
SELECT 1;
475+
\syncpipeline
476+
\syncpipeline
477+
SELECT 2;
478+
\syncpipeline
479+
SELECT 3;
480+
\echo :PIPELINE_COMMAND_COUNT
481+
1
482+
\echo :PIPELINE_SYNC_COUNT
483+
3
484+
\echo :PIPELINE_RESULT_COUNT
485+
2
486+
\endpipeline
487+
?column?
488+
----------
489+
1
490+
(1 row)
491+
492+
?column?
493+
----------
494+
2
495+
(1 row)
496+
497+
?column?
498+
----------
499+
3
500+
(1 row)
501+
502+
-- Mix queries piped with ';' and \sendpipeline
398503
\startpipeline
399504
SELECT 1;
400-
PQsendQuery not allowed in pipeline mode
401505
SELECT $1 \bind 'val1' \sendpipeline
506+
SELECT $1, $2 \bind 'val2' 'val3' \sendpipeline
507+
SELECT 2;
508+
\endpipeline
509+
?column?
510+
----------
511+
1
512+
(1 row)
513+
514+
?column?
515+
----------
516+
val1
517+
(1 row)
518+
519+
?column? | ?column?
520+
----------+----------
521+
val2 | val3
522+
(1 row)
523+
524+
?column?
525+
----------
526+
2
527+
(1 row)
528+
529+
-- Piping a query with ';' will replace the unnamed prepared statement
530+
\startpipeline
531+
SELECT $1 \parse ''
532+
SELECT 1;
533+
\bind_named ''
534+
\endpipeline
535+
?column?
536+
----------
537+
1
538+
(1 row)
539+
540+
-- An extended query can be piped by a ';' after a newline
541+
\startpipeline
542+
SELECT $1 \bind 1
543+
;
544+
SELECT 2;
545+
\endpipeline
546+
?column?
547+
----------
548+
1
549+
(1 row)
550+
551+
?column?
552+
----------
553+
2
554+
(1 row)
555+
556+
-- COPY FROM STDIN, using ';'
557+
\startpipeline
558+
SELECT 'val1';
559+
COPY psql_pipeline FROM STDIN;
560+
\endpipeline
561+
?column?
562+
----------
563+
val1
564+
(1 row)
565+
566+
-- COPY FROM STDIN with \flushrequest + \getresults, using ';'
567+
\startpipeline
568+
SELECT 'val1';
569+
COPY psql_pipeline FROM STDIN;
570+
\flushrequest
571+
\getresults
572+
?column?
573+
----------
574+
val1
575+
(1 row)
576+
577+
message type 0x5a arrived from server while idle
578+
\endpipeline
579+
-- COPY FROM STDIN with \syncpipeline + \getresults, using ';'
580+
\startpipeline
581+
SELECT 'val1';
582+
COPY psql_pipeline FROM STDIN;
583+
\syncpipeline
584+
\getresults
585+
?column?
586+
----------
587+
val1
588+
(1 row)
589+
402590
\endpipeline
591+
-- COPY TO STDOUT, using ';'
592+
\startpipeline
593+
SELECT 'val1';
594+
copy psql_pipeline TO STDOUT;
595+
\endpipeline
596+
?column?
597+
----------
598+
val1
599+
(1 row)
600+
601+
1 \N
602+
2 test2
603+
3 test3
604+
4 test4
605+
20 test2
606+
30 test3
607+
40 test4
608+
-- COPY TO STDOUT with \flushrequest + \getresults, using ';'
609+
\startpipeline
610+
SELECT 'val1';
611+
copy psql_pipeline TO STDOUT;
612+
\flushrequest
613+
\getresults
403614
?column?
404615
----------
405616
val1
406617
(1 row)
407618

619+
1 \N
620+
2 test2
621+
3 test3
622+
4 test4
623+
20 test2
624+
30 test3
625+
40 test4
626+
\endpipeline
627+
-- COPY TO STDOUT with \syncpipeline + \getresults, using ';'
628+
\startpipeline
629+
SELECT 'val1';
630+
copy psql_pipeline TO STDOUT;
631+
\syncpipeline
632+
\getresults
633+
?column?
634+
----------
635+
val1
636+
(1 row)
637+
638+
1 \N
639+
2 test2
640+
3 test3
641+
4 test4
642+
20 test2
643+
30 test3
644+
40 test4
645+
\endpipeline
646+
--
647+
-- Pipeline errors
648+
--
649+
-- \endpipeline outside of pipeline should fail
650+
\endpipeline
651+
cannot send pipeline when not in pipeline mode
408652
-- After an aborted pipeline, commands after a \syncpipeline should be
409653
-- displayed.
410654
\startpipeline
@@ -425,6 +669,13 @@ SELECT \bind 'val1' \sendpipeline
425669
SELECT $1 \bind 'val1' \sendpipeline
426670
\endpipeline
427671
ERROR: bind message supplies 1 parameters, but prepared statement "" requires 0
672+
-- Using ';' with a parameter will trigger an incorrect parameter error and
673+
-- abort the pipeline
674+
\startpipeline
675+
SELECT $1;
676+
SELECT 1;
677+
\endpipeline
678+
ERROR: bind message supplies 0 parameters, but prepared statement "" requires 1
428679
-- An explicit transaction with an error needs to be rollbacked after
429680
-- the pipeline.
430681
\startpipeline
@@ -435,12 +686,11 @@ ROLLBACK \bind \sendpipeline
435686
ERROR: duplicate key value violates unique constraint "psql_pipeline_pkey"
436687
DETAIL: Key (a)=(1) already exists.
437688
ROLLBACK;
438-
-- \watch sends a simple query, something not allowed within a pipeline.
689+
-- \watch is not allowed within a pipeline.
439690
\startpipeline
440691
SELECT \bind \sendpipeline
441692
\watch 1
442-
PQsendQuery not allowed in pipeline mode
443-
693+
\watch not allowed in pipeline mode
444694
\endpipeline
445695
--
446696
(1 row)
@@ -530,7 +780,7 @@ SELECT COUNT(*) FROM psql_pipeline \bind \sendpipeline
530780

531781
count
532782
-------
533-
4
783+
7
534784
(1 row)
535785

536786
-- After an error, pipeline is aborted and requires \syncpipeline to be
@@ -617,11 +867,11 @@ select 1;
617867
-- Error messages accumulate and are repeated.
618868
\startpipeline
619869
SELECT 1 \bind \sendpipeline
620-
SELECT 1;
621-
PQsendQuery not allowed in pipeline mode
622-
SELECT 1;
623-
PQsendQuery not allowed in pipeline mode
624-
PQsendQuery not allowed in pipeline mode
870+
\gdesc
871+
synchronous command execution functions are not allowed in pipeline mode
872+
\gdesc
873+
synchronous command execution functions are not allowed in pipeline mode
874+
synchronous command execution functions are not allowed in pipeline mode
625875
\endpipeline
626876
?column?
627877
----------

0 commit comments

Comments
 (0)