Re: [HACKERS] PATCH: Batch/pipelining support for libpq
Thanks a lot for the merge. I did some tests and the master branch runs up to 15% faster than the last patch I tried (v22). Amazing! Cheers, Matthieu Garrigues On Tue, Mar 16, 2021 at 9:00 PM Andres Freund wrote: > > Hi, > > On 2021-03-05 21:35:59 -0300, Alvaro Herrera wrote: > > I'll take the weekend to think about the issue with conn->last_query and > > conn->queryclass that I mentioned yesterday; other than that detail my > > feeling is that this is committable, so I'll be looking at getting this > > pushed early next weeks, barring opinions from others. > > It is *very* exciting to see this being merged. Thanks for all the work > to all that contributed! > > Greetings, > > Andres Freund
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
Hi, On 2021-03-05 21:35:59 -0300, Alvaro Herrera wrote: > I'll take the weekend to think about the issue with conn->last_query and > conn->queryclass that I mentioned yesterday; other than that detail my > feeling is that this is committable, so I'll be looking at getting this > pushed early next weeks, barring opinions from others. It is *very* exciting to see this being merged. Thanks for all the work to all that contributed! Greetings, Andres Freund
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
On 2021-Mar-15, Justin Pryzby wrote: > Are you going to update the assertion ? > > +#if 0 > > > Assert((meta == META_NONE && varprefix == NULL) || > > >((meta == META_GSET || meta == META_ASET) && varprefix != > NULL)); > > +#endif > > Yeah, caught that just after sending. Here's a notpatch. -- Álvaro Herrera39°49'30"S 73°17'W "La virtud es el justo medio entre dos defectos" (Aristóteles) diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c index ba7b35d83c..e69d43b26b 100644 --- a/src/bin/pgbench/pgbench.c +++ b/src/bin/pgbench/pgbench.c @@ -2823,13 +2823,12 @@ readCommandResponse(CState *st, MetaCommand meta, char *varprefix) int qrynum = 0; /* -* varprefix should be set only with \gset or \aset, and SQL commands do -* not need it. +* varprefix should be set only with \gset or \aset, and \endpipeline and +* SQL commands do not need it. */ -#if 0 Assert((meta == META_NONE && varprefix == NULL) || + ((meta == META_ENDPIPELINE) && varprefix == NULL) || ((meta == META_GSET || meta == META_ASET) && varprefix != NULL)); -#endif res = PQgetResult(st->con);
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
Are you going to update the assertion ? +#if 0 Assert((meta == META_NONE && varprefix == NULL) || ((meta == META_GSET || meta == META_ASET) && varprefix != NULL)); +#endif
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
Here's what seems a final version of the patch. I renamed one more function: PQsendPipeline is now PQpipelineSync. I also reworded the docs in a couple of places, added a few tests to the pgbench patch, and made it work. Note the pgbench results in pipeline mode: ./pgbench -r -Mextended -n -f /home/alvherre/Code/pgsql-build/pipeline/src/bin/pgbench/tmp_check/t_001_pgbench_with_server_main_data/001_pgbench_pipeline -c 100 -t1 pgbench (PostgreSQL) 14.0 transaction type: /home/alvherre/Code/pgsql-build/pipeline/src/bin/pgbench/tmp_check/t_001_pgbench_with_server_main_data/001_pgbench_pipeline scaling factor: 1 query mode: extended number of clients: 100 number of threads: 1 number of transactions per client: 1 number of transactions actually processed: 100/100 latency average = 2.316 ms initial connection time = 113.859 ms tps = 43182.438635 (without initial connection time) statement latencies in milliseconds: 0.000 \startpipeline 0.000 select 1; 0.000 select 1; 0.000 select 1; 0.000 select 1; 0.000 select 1; 0.000 select 1; 0.000 select 1; 0.000 select 1; 0.000 select 1; 0.000 select 1; 1.624 \endpipeline If I just replace the \startpipeline and \endpipeline lines with BEGIN and COMMIT respectively, I get this: tps = 10220.259051 (without initial connection time) 0.830 begin; 0.765 select 1; 0.752 select 1; 0.753 select 1; 0.755 select 1; 0.754 select 1; 0.755 select 1; 0.757 select 1; 0.756 select 1; 0.756 select 1; 0.756 select 1; 0.750 commit; Yes, you could say that this is a ltle bit unfair -- but it seems quite impressive nonetheless. -- Álvaro Herrera39°49'30"S 73°17'W >From 5e4fdd5246d559caf0d75ad74001f09a48ec4c0e Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Mon, 15 Mar 2021 15:05:22 -0300 Subject: [PATCH v37 1/2] Implement pipeline mode in libpq MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pipeline mode in libpq lets an application avoid the Sync messages in the FE/BE protocol that are implicit in the old libpq API after each query. The application can then insert Sync at its leisure with a new libpq function PQpipelineSync. This can lead to substantial reductions in query latency. Co-authored-by: Craig Ringer Co-authored-by: Matthieu Garrigues Co-authored-by: Álvaro Herrera Reviewed-by: Andres Freund Reviewed-by: Aya Iwata Reviewed-by: Daniel Vérité Reviewed-by: David G. Johnston Reviewed-by: Justin Pryzby Reviewed-by: Kirk Jamison Reviewed-by: Michael Paquier Reviewed-by: Nikhil Sontakke Reviewed-by: Vaishnavi Prabakaran Reviewed-by: Zhihong Yu Discussion: https://postgr.es/m/CAMsr+YFUjJytRyV4J-16bEoiZyH=4nj+sQ7JP9ajwz=b4dm...@mail.gmail.com Discussion: https://postgr.es/m/cajkzx4t5e-2cqe3dtv2r78dyfvz+in8py7a8marvlhs_pg7...@mail.gmail.com --- doc/src/sgml/libpq.sgml | 522 ++- doc/src/sgml/lobj.sgml|4 + doc/src/sgml/ref/pgbench.sgml | 21 + .../libpqwalreceiver/libpqwalreceiver.c |6 + src/bin/pg_amcheck/pg_amcheck.c |2 + src/interfaces/libpq/exports.txt |4 + src/interfaces/libpq/fe-connect.c | 37 +- src/interfaces/libpq/fe-exec.c| 717 - src/interfaces/libpq/fe-protocol3.c | 77 +- src/interfaces/libpq/libpq-fe.h | 21 +- src/interfaces/libpq/libpq-int.h | 60 +- src/test/modules/Makefile |1 + src/test/modules/libpq_pipeline/.gitignore|5 + src/test/modules/libpq_pipeline/Makefile | 20 + src/test/modules/libpq_pipeline/README|1 + .../modules/libpq_pipeline/libpq_pipeline.c | 1303 + .../libpq_pipeline/t/001_libpq_pipeline.pl| 28 + src/tools/msvc/Mkvcbuild.pm |9 +- src/tools/pgindent/typedefs.list |2 + 19 files changed, 2727 insertions(+), 113 deletions(-) create mode 100644 src/test/modules/libpq_pipeline/.gitignore create mode 100644 src/test/modules/libpq_pipeline/Makefile create mode 100644 src/test/modules/libpq_pipeline/README create mode 100644 src/test/modules/libpq_pipeline/libpq_pipeline.c create mode 100644 src/test/modules/libpq_pipeline/t/001_libpq_pipeline.pl diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 910e9a81ea..be674fbaa9 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -3180,6 +3180,33 @@ ExecStatusType PQresultStatus(const PGresult *res); + + + PGRES_PIPELINE_SYNC + + +The PGresult represents a +synchronization point in pipeline mode, requested
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
On 2021-Mar-11, Tom Lane wrote: > I think the changes in pqParseInput3() are broken. You should have > kept the else-structure as-is and inserted the check for "not really > idle" inside the else-clause that reports an error. As it stands, > after successfully processing an asynchronously-received error or > ParameterStatus message, the added code will cause us to return without > advancing inStart, creating an infinite loop of reprocessing that message. > > It's possible that we should redefine the way things happen so that if > we're waiting for another pipeline event, we should hold off processing > of async error & ParameterStatus; but in that case you should have added > the pre-emptive return ahead of that if/else structure, where the existing > "If not IDLE state, just wait ..." test is. I think I agree that holding off 'E' and 'S' messages when in between processing results for different queries in a pipeline, so keeping the original if/else structure is correct. An error would be correctly dealt with in the BUSY state immediately afterwards; and the fact that we pass 'inError' false at that point causes the wrong reaction (namely that the pipeline is not put in aborted state). I made a number of other changes: documentation adjustments per comments from David Johnston, some function renaming as previously noted, and added test code for PQsendDescribePrepared, PQsendDescribePortal. Also rebased on latest changes. I also absorbed one change that I already had when I submitted v35, but hadn't done "git add" on (which caused a compile failure for CF bot). -- Álvaro Herrera Valdivia, Chile diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 910e9a81ea..7b938c106c 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -3180,6 +3180,33 @@ ExecStatusType PQresultStatus(const PGresult *res); + + + PGRES_PIPELINE_SYNC + + +The PGresult represents a +synchronization point in pipeline mode, requested by +. +This status occurs only when pipeline mode has been selected. + + + + + + PGRES_PIPELINE_ABORTED + + +The PGresult represents a pipeline that has +received an error from the server. PQgetResult +must be called repeatedly, and each time it will return this status code +until the end of the current pipeline, at which point it will return +PGRES_PIPELINE_SYNC and normal processing can +resume. + + + + If the result status is PGRES_TUPLES_OK or @@ -4926,6 +4953,479 @@ int PQflush(PGconn *conn); + + Pipeline Mode + + + libpq + pipeline mode + + + + pipelining + in libpq + + + + batch mode + in libpq + + + + libpq pipeline mode allows applications to + send a query without having to read the result of the previously + sent query. Taking advantage of the pipeline mode, a client will wait + less for the server, since multiple queries/results can be + sent/received in a single network transaction. + + + + While pipeline mode provides a significant performance boost, writing + clients using the pipeline mode is more complex because it involves + managing a queue of pending queries and finding which result + corresponds to which query in the queue. + + + + Pipeline mode also generally consumes more memory on both the client and server, + though careful and aggressive management of the send/receive queue can mitigate + this. This applies whether or not the connection is in blocking or non-blocking + mode. + + + + While the pipeline API was introduced in + PostgreSQL 14, it is a client-side feature + which doesn't require special server support, and works on any server + that supports the v3 extended query protocol. + + + + Using Pipeline Mode + + +To issue pipelines, the application must switch the connection +into pipeline mode, +which is done with . + can be used +to test whether pipeline mode is active. +In pipeline mode, only asynchronous operations +are permitted, and COPY is disallowed. +Using synchronous command execution functions +such as PQfn, +PQexec, +PQexecParams, +PQprepare, +PQexecPrepared, +PQdescribePrepared, +PQdescribePortal, +is an error condition. +Once all dispatched commands have had their results processed, and +the end pipeline result has been consumed, the application may return +to non-pipelined mode with . + + + + + It is best to use pipeline mode with libpq in + non-blocking mode. If used + in blocking mode it is possible for a client/server deadlock to occur. + + +The client will block trying to send queries to the server,
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
Alvaro Herrera writes: > [ v35-libpq-pipeline.patch ] I think the changes in pqParseInput3() are broken. You should have kept the else-structure as-is and inserted the check for "not really idle" inside the else-clause that reports an error. As it stands, after successfully processing an asynchronously-received error or ParameterStatus message, the added code will cause us to return without advancing inStart, creating an infinite loop of reprocessing that message. It's possible that we should redefine the way things happen so that if we're waiting for another pipeline event, we should hold off processing of async error & ParameterStatus; but in that case you should have added the pre-emptive return ahead of that if/else structure, where the existing "If not IDLE state, just wait ..." test is. My guess though is that we do need to process error messages in that state, so that the correct patch looks more like else { +/* + * We're notionally not-IDLE when in pipeline mode we have + * completed processing the results of one query and are waiting + * for the next one in the pipeline. In this case, as above, just + * wait. + */ +if (conn->asyncStatus == PGASYNC_IDLE && +conn->pipelineStatus != PQ_PIPELINE_OFF && +conn->cmd_queue_head != NULL) +return; + pqInternalNotice(>noticeHooks, "message type 0x%02x arrived from server while idle", id); /* Discard the unexpected message */ conn->inCursor += msgLength; } It'd be appropriate to do more than nothing to the comment block above this if/else chain, too, because really that one ought to explain why we should consume ERROR when in pipeline state. (I've not looked at the rest of this patch, just scanned what you did in fe-protocol3.c, because I wondered if there would be any interaction with the where-to-advance-inStart changes I'm about to commit. Looks okay modulo this issue.) regards, tom lane
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
On 2021-Mar-05, Alvaro Herrera wrote: > I'll take the weekend to think about the issue with conn->last_query and > conn->queryclass that I mentioned yesterday; other than that detail my > feeling is that this is committable, so I'll be looking at getting this > pushed early next weeks, barring opinions from others. It took longer than I expected, but it works well now. conn->last_query is gone; all commands, both in pipeline mode and in no-pipeline mode, go via the command queue. This is cleaner all around; we don't have to have the pipeline code "cheat" so that it looks like each command is "last" at each point. I have not absorbed David Johnston's latest doc suggestions yet. I'm going to give the code a last renaming pass, on the idea that the command queue is no longer exclusively for the pipeline mode, so some things need less exclusionary names. But functionality wise AFAICS this patch has the shape it ought to have. -- Álvaro Herrera39°49'30"S 73°17'W diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 910e9a81ea..0bffb92462 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -3180,6 +3180,33 @@ ExecStatusType PQresultStatus(const PGresult *res); + + + PGRES_PIPELINE_SYNC + + +The PGresult represents a +synchronization point in pipeline mode, requested by +. +This status occurs only when pipeline mode has been selected. + + + + + + PGRES_PIPELINE_ABORTED + + +The PGresult represents a pipeline that has +received an error from the server. PQgetResult +must be called repeatedly, and each time it will return this status code +until the end of the current pipeline, at which point it will return +PGRES_PIPELINE_SYNC and normal processing can +resume. + + + + If the result status is PGRES_TUPLES_OK or @@ -4926,6 +4953,473 @@ int PQflush(PGconn *conn); + + Pipeline Mode + + + libpq + pipeline mode + + + + pipelining + in libpq + + + + batch mode + in libpq + + + + libpq pipeline mode allows applications to + send a query without having to read the result of the previously + sent query. Taking advantage of the pipeline mode, a client will wait + less for the server, since multiple queries/results can be + sent/received in a single network transaction. + + + + While pipeline mode provides a significant performance boost, writing + clients using the pipeline mode is more complex because it involves + managing a queue of pending queries and finding which result + corresponds to which query in the queue. + + + + Pipeline mode also generally consumes more memory on both the client and server, + though careful and aggressive management of the send/receive queue can mitigate + this. This applies whether or not the connection is in blocking or non-blocking + mode. + + + + Using Pipeline Mode + + +To issue pipelines, the application must switch the connection +into pipeline mode, +which is done with . + can be used +to test whether pipeline mode is active. +In pipeline mode, only asynchronous operations +are permitted, and COPY is disallowed. +Using synchronous command execution functions +such as PQfn, +PQexec, +PQexecParams, +PQprepare, +PQexecPrepared, +PQdescribePrepared, +PQdescribePortal, +is an error condition. +Once all dispatched commands have had their results processed, and +the end pipeline result has been consumed, the application may return +to non-pipelined mode with . + + + + + It is best to use pipeline mode with libpq in + non-blocking mode. If used + in blocking mode it is possible for a client/server deadlock to occur. + + +The client will block trying to send queries to the server, but the +server will block trying to send results to the client from queries +it has already processed. This only occurs when the client sends +enough queries to fill both its output buffer and the server's receive +buffer before it switches to processing input from the server, +but it's hard to predict exactly when that will happen. + + + + + + +Issuing Queries + + + After entering pipeline mode, the application dispatches requests using + , + , + or its prepared-query sibling + . + These requests are queued on the client-side until flushed to the server; + this occurs when is used to + establish a synchronization point in the pipeline, + or when is called. + The functions , + , and + also work in pipeline mode. +
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
v33 was indeed marked a pass by cfbot. However, it only *builds* the test program, it does not *run* it. I guess we'll have to wait for the buildfarm to tell us more. In the meantime, I implemented PQsendQuery() as callable in pipeline mode; it does that by using the extended-query protocol directly rather than sending 'Q' as in non-pipeline mode. I also adjusted the docs a little bit more. That's what you see here as v34. I'll take the weekend to think about the issue with conn->last_query and conn->queryclass that I mentioned yesterday; other than that detail my feeling is that this is committable, so I'll be looking at getting this pushed early next weeks, barring opinions from others. -- Álvaro Herrera Valdivia, Chile "El sabio habla porque tiene algo que decir; el tonto, porque tiene que decir algo" (Platon). diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 0553279314..e3cd5c377b 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -3173,6 +3173,33 @@ ExecStatusType PQresultStatus(const PGresult *res); + + + PGRES_PIPELINE_SYNC + + +The PGresult represents a +synchronization point in pipeline mode, requested by +. +This status occurs only when pipeline mode has been selected. + + + + + + PGRES_PIPELINE_ABORTED + + +The PGresult represents a pipeline that has +received an error from the server. PQgetResult +must be called repeatedly, and each time it will return this status code +until the end of the current pipeline, at which point it will return +PGRES_PIPELINE_SYNC and normal processing can +resume. + + + + If the result status is PGRES_TUPLES_OK or @@ -4919,6 +4946,473 @@ int PQflush(PGconn *conn); + + Pipeline Mode + + + libpq + pipeline mode + + + + pipelining + in libpq + + + + batch mode + in libpq + + + + libpq pipeline mode allows applications to + send a query without having to read the result of the previously + sent query. Taking advantage of the pipeline mode, a client will wait + less for the server, since multiple queries/results can be + sent/received in a single network transaction. + + + + While pipeline mode provides a significant performance boost, writing + clients using the pipeline mode is more complex because it involves + managing a queue of pending queries and finding which result + corresponds to which query in the queue. + + + + Pipeline mode also generally consumes more memory on both the client and server, + though careful and aggressive management of the send/receive queue can mitigate + this. This applies whether or not the connection is in blocking or non-blocking + mode. + + + + Using Pipeline Mode + + +To issue pipelines, the application must switch the connection +into pipeline mode, +which is done with . + can be used +to test whether pipeline mode is active. +In pipeline mode, only asynchronous operations +are permitted, and COPY is disallowed. +Using synchronous command execution functions +such as PQfn, +PQexec, +PQexecParams, +PQprepare, +PQexecPrepared, +PQdescribePrepared, +PQdescribePortal, +is an error condition. +Once all dispatched commands have had their results processed, and +the end pipeline result has been consumed, the application may return +to non-pipelined mode with . + + + + + It is best to use pipeline mode with libpq in + non-blocking mode. If used + in blocking mode it is possible for a client/server deadlock to occur. + + +The client will block trying to send queries to the server, but the +server will block trying to send results to the client from queries +it has already processed. This only occurs when the client sends +enough queries to fill both its output buffer and the server's receive +buffer before it switches to processing input from the server, +but it's hard to predict exactly when that will happen. + + + + + + +Issuing Queries + + + After entering pipeline mode, the application dispatches requests using + , + , + or its prepared-query sibling + . + These requests are queued on the client-side until flushed to the server; + this occurs when is used to + establish a synchronization point in the pipeline, + or when is called. + The functions , + , and + also work in pipeline mode. + Result processing is described below. + + + + The server executes statements, and returns results, in the order the + client sends them. The server will
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
Hi, + \gset and \aset cannot be used + pipeline mode, since query results are not immediately 'used pipeline mode' -> 'used in pipeline mode' --- /dev/null +++ b/src/test/modules/libpq_pipeline/libpq_pipeline.c @@ -0,0 +1,1144 @@ +/* + * src/test/modules/libpq_pipeline/libpq_pipeline.c Looks like license information is missing from the header. Cheers On Thu, Mar 4, 2021 at 1:40 PM Alvaro Herrera wrote: > On 2021-Mar-04, Alvaro Herrera wrote: > > > I don't know where do __WSAFDIsSet and __imp_select come from or what to > > do about them. Let's see if adding pgport and pgcommon fixes things. > > Indeed all those other problems were fixed and these remain. New > failure is: > > "C:\projects\postgresql\pgsql.sln" (default target) (1) -> > 6007"C:\projects\postgresql\libpq_pipeline.vcxproj" (default target) (55) > -> > 6008(Link target) -> > 6009 libpq_pipeline.obj : error LNK2019: unresolved external symbol > __WSAFDIsSet referenced in function test_pipelined_insert > [C:\projects\postgresql\libpq_pipeline.vcxproj] > 6010 libpq_pipeline.obj : error LNK2019: unresolved external symbol > __imp_select referenced in function test_pipelined_insert > [C:\projects\postgresql\libpq_pipeline.vcxproj] > 6011 .\Release\libpq_pipeline\libpq_pipeline.exe : fatal error LNK1120: 2 > unresolved externals [C:\projects\postgresql\libpq_pipeline.vcxproj] > > I did notice that isolationtester.c is using select(), and one > difference is that it includes which libpq_pipeline.c > does not -- and it also pulls in ws2_32.lib. Let's see if those two > changes fix things. > > -- > Álvaro Herrera Valdivia, Chile >
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
On 2021-Mar-04, Alvaro Herrera wrote: > I don't know where do __WSAFDIsSet and __imp_select come from or what to > do about them. Let's see if adding pgport and pgcommon fixes things. Indeed all those other problems were fixed and these remain. New failure is: "C:\projects\postgresql\pgsql.sln" (default target) (1) -> 6007"C:\projects\postgresql\libpq_pipeline.vcxproj" (default target) (55) -> 6008(Link target) -> 6009 libpq_pipeline.obj : error LNK2019: unresolved external symbol __WSAFDIsSet referenced in function test_pipelined_insert [C:\projects\postgresql\libpq_pipeline.vcxproj] 6010 libpq_pipeline.obj : error LNK2019: unresolved external symbol __imp_select referenced in function test_pipelined_insert [C:\projects\postgresql\libpq_pipeline.vcxproj] 6011 .\Release\libpq_pipeline\libpq_pipeline.exe : fatal error LNK1120: 2 unresolved externals [C:\projects\postgresql\libpq_pipeline.vcxproj] I did notice that isolationtester.c is using select(), and one difference is that it includes which libpq_pipeline.c does not -- and it also pulls in ws2_32.lib. Let's see if those two changes fix things. -- Álvaro Herrera Valdivia, Chile diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 0553279314..c87b0ce911 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -3173,6 +3173,33 @@ ExecStatusType PQresultStatus(const PGresult *res); + + + PGRES_PIPELINE_SYNC + + +The PGresult represents a +synchronization point in pipeline mode, requested by +. +This status occurs only when pipeline mode has been selected. + + + + + + PGRES_PIPELINE_ABORTED + + +The PGresult represents a pipeline that has +received an error from the server. PQgetResult +must be called repeatedly, and each time it will return this status code +until the end of the current pipeline, at which point it will return +PGRES_PIPELINE_SYNC and normal processing can +resume. + + + + If the result status is PGRES_TUPLES_OK or @@ -4919,6 +4946,498 @@ int PQflush(PGconn *conn); + + Pipeline Mode + + + libpq + pipeline mode + + + + pipelining + in libpq + + + + batch mode + in libpq + + + + libpq pipeline mode allows applications to + send a query without having to read the result of the previously + sent query. Taking advantage of the pipeline mode, a client will wait + less for the server, since multiple queries/results can be sent/ + received in a single network transaction. + + + + While pipeline mode provides a significant performance boost, writing + clients using the pipeline mode is more complex because it involves + managing a queue of pending queries and finding which result + corresponds to which query in the queue. + + + + Pipeline mode also generally consumes more memory on both the client and server, + though careful and aggressive management of the send/receive queue can mitigate + this. This applies whether or not the connection is in blocking or non-blocking + mode. + + + + Using Pipeline Mode + + +To issue pipelines, the application must switch a connection into pipeline mode. +Enter pipeline mode with +or test whether pipeline mode is active with +. +In pipeline mode, only asynchronous operations +are permitted, and COPY is disallowed. +Using any synchronous command execution functions, +such as PQfn, or PQexec +and its sibling functions, is an error condition. + + + + + It is best to use pipeline mode with libpq in + non-blocking mode. If used + in blocking mode it is possible for a client/server deadlock to occur. + + +The client will block trying to send queries to the server, but the +server will block trying to send results to the client from queries +it has already processed. This only occurs when the client sends +enough queries to fill both its output buffer and the server's receive +buffer before it switches to processing input from the server, +but it's hard to predict exactly when that will happen. + + + + + + +Issuing Queries + + + After entering pipeline mode, the application dispatches requests using + , + or its prepared-query sibling + . + These requests are queued on the client-side until flushed to the server; + this occurs when is used to + establish a synchronization point in the pipeline, + or when is called. + The functions , + , and + also work in pipeline mode. + Result processing is described below. + + + + The server executes statements, and returns
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
On 2021-Mar-04, Alvaro Herrera wrote: > v31. Got this: libpq_pipeline.obj : error LNK2019: unresolved external symbol __WSAFDIsSet referenced in function test_pipelined_insert [C:\projects\postgresql\libpq_pipeline.vcxproj] 5019libpq_pipeline.obj : error LNK2019: unresolved external symbol __imp_select referenced in function test_pipelined_insert [C:\projects\postgresql\libpq_pipeline.vcxproj] 5020libpq_pipeline.obj : error LNK2019: unresolved external symbol pg_snprintf referenced in function test_pipelined_insert [C:\projects\postgresql\libpq_pipeline.vcxproj] 5021libpq_pipeline.obj : error LNK2019: unresolved external symbol pg_vfprintf referenced in function pg_fatal_impl [C:\projects\postgresql\libpq_pipeline.vcxproj] 5022libpq_pipeline.obj : error LNK2019: unresolved external symbol pg_fprintf referenced in function pg_fatal_impl [C:\projects\postgresql\libpq_pipeline.vcxproj] 5023libpq_pipeline.obj : error LNK2019: unresolved external symbol pg_printf referenced in function pg_fatal_impl [C:\projects\postgresql\libpq_pipeline.vcxproj] 5024libpq_pipeline.obj : error LNK2019: unresolved external symbol pg_strerror referenced in function test_pipelined_insert [C:\projects\postgresql\libpq_pipeline.vcxproj] 5025libpq_pipeline.obj : error LNK2019: unresolved external symbol pg_strdup referenced in function main [C:\projects\postgresql\libpq_pipeline.vcxproj] 5026libpq_pipeline.obj : error LNK2019: unresolved external symbol pfree referenced in function test_singlerowmode [C:\projects\postgresql\libpq_pipeline.vcxproj] 5027libpq_pipeline.obj : error LNK2019: unresolved external symbol psprintf referenced in function test_singlerowmode [C:\projects\postgresql\libpq_pipeline.vcxproj] pg_snprintf, pg_vfprintf, pg_fprintf, pg_printf, pg_strerror are in pgport. pg_strdup and pfree, psprintf are in pgcommon. I don't know where do __WSAFDIsSet and __imp_select come from or what to do about them. Let's see if adding pgport and pgcommon fixes things. -- Álvaro Herrera39°49'30"S 73°17'W diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 0553279314..c87b0ce911 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -3173,6 +3173,33 @@ ExecStatusType PQresultStatus(const PGresult *res); + + + PGRES_PIPELINE_SYNC + + +The PGresult represents a +synchronization point in pipeline mode, requested by +. +This status occurs only when pipeline mode has been selected. + + + + + + PGRES_PIPELINE_ABORTED + + +The PGresult represents a pipeline that has +received an error from the server. PQgetResult +must be called repeatedly, and each time it will return this status code +until the end of the current pipeline, at which point it will return +PGRES_PIPELINE_SYNC and normal processing can +resume. + + + + If the result status is PGRES_TUPLES_OK or @@ -4919,6 +4946,498 @@ int PQflush(PGconn *conn); + + Pipeline Mode + + + libpq + pipeline mode + + + + pipelining + in libpq + + + + batch mode + in libpq + + + + libpq pipeline mode allows applications to + send a query without having to read the result of the previously + sent query. Taking advantage of the pipeline mode, a client will wait + less for the server, since multiple queries/results can be sent/ + received in a single network transaction. + + + + While pipeline mode provides a significant performance boost, writing + clients using the pipeline mode is more complex because it involves + managing a queue of pending queries and finding which result + corresponds to which query in the queue. + + + + Pipeline mode also generally consumes more memory on both the client and server, + though careful and aggressive management of the send/receive queue can mitigate + this. This applies whether or not the connection is in blocking or non-blocking + mode. + + + + Using Pipeline Mode + + +To issue pipelines, the application must switch a connection into pipeline mode. +Enter pipeline mode with +or test whether pipeline mode is active with +. +In pipeline mode, only asynchronous operations +are permitted, and COPY is disallowed. +Using any synchronous command execution functions, +such as PQfn, or PQexec +and its sibling functions, is an error condition. + + + + + It is best to use pipeline mode with libpq in + non-blocking mode. If used + in blocking mode it is possible for a client/server deadlock to occur. + + +The client will block trying to send queries to the server, but the +server will block trying to send results
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
On 2021-Mar-04, Alvaro Herrera wrote: > I think the problem is that the project is called pipeline and not > test_libpq, so there's no match in the name. I'm going to rename the > whole thing to src/test/modules/libpq_pipeline/ and see if the msvc > tooling likes that better. v31. -- Álvaro Herrera Valdivia, Chile "I'm impressed how quickly you are fixing this obscure issue. I came from MS SQL and it would be hard for me to put into words how much of a better job you all are doing on [PostgreSQL]." Steve Midgley, http://archives.postgresql.org/pgsql-sql/2008-08/msg0.php diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 0553279314..c87b0ce911 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -3173,6 +3173,33 @@ ExecStatusType PQresultStatus(const PGresult *res); + + + PGRES_PIPELINE_SYNC + + +The PGresult represents a +synchronization point in pipeline mode, requested by +. +This status occurs only when pipeline mode has been selected. + + + + + + PGRES_PIPELINE_ABORTED + + +The PGresult represents a pipeline that has +received an error from the server. PQgetResult +must be called repeatedly, and each time it will return this status code +until the end of the current pipeline, at which point it will return +PGRES_PIPELINE_SYNC and normal processing can +resume. + + + + If the result status is PGRES_TUPLES_OK or @@ -4919,6 +4946,498 @@ int PQflush(PGconn *conn); + + Pipeline Mode + + + libpq + pipeline mode + + + + pipelining + in libpq + + + + batch mode + in libpq + + + + libpq pipeline mode allows applications to + send a query without having to read the result of the previously + sent query. Taking advantage of the pipeline mode, a client will wait + less for the server, since multiple queries/results can be sent/ + received in a single network transaction. + + + + While pipeline mode provides a significant performance boost, writing + clients using the pipeline mode is more complex because it involves + managing a queue of pending queries and finding which result + corresponds to which query in the queue. + + + + Pipeline mode also generally consumes more memory on both the client and server, + though careful and aggressive management of the send/receive queue can mitigate + this. This applies whether or not the connection is in blocking or non-blocking + mode. + + + + Using Pipeline Mode + + +To issue pipelines, the application must switch a connection into pipeline mode. +Enter pipeline mode with +or test whether pipeline mode is active with +. +In pipeline mode, only asynchronous operations +are permitted, and COPY is disallowed. +Using any synchronous command execution functions, +such as PQfn, or PQexec +and its sibling functions, is an error condition. + + + + + It is best to use pipeline mode with libpq in + non-blocking mode. If used + in blocking mode it is possible for a client/server deadlock to occur. + + +The client will block trying to send queries to the server, but the +server will block trying to send results to the client from queries +it has already processed. This only occurs when the client sends +enough queries to fill both its output buffer and the server's receive +buffer before it switches to processing input from the server, +but it's hard to predict exactly when that will happen. + + + + + + +Issuing Queries + + + After entering pipeline mode, the application dispatches requests using + , + or its prepared-query sibling + . + These requests are queued on the client-side until flushed to the server; + this occurs when is used to + establish a synchronization point in the pipeline, + or when is called. + The functions , + , and + also work in pipeline mode. + Result processing is described below. + + + + The server executes statements, and returns results, in the order the + client sends them. The server will begin executing the commands in the + pipeline immediately, not waiting for the end of the pipeline. + If any statement encounters an error, the server aborts the current + transaction and skips processing commands in the pipeline until the + next synchronization point established by PQsendPipeline. + (This remains true even if the commands in the pipeline would rollback + the transaction.) + Query processing resumes after the synchronization point. + + + + It's fine for
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
On 2021-Mar-04, Alvaro Herrera wrote: > v30 contains changes to hopefully make it build on MSVC. Hm, that didn't work -- appveyor still says: Project "C:\projects\postgresql\pgsql.sln" (1) is building "C:\projects\postgresql\pipeline.vcxproj" (75) on node 1 (default targets). PrepareForBuild: Creating directory ".\Release\pipeline\". Creating directory ".\Release\pipeline\pipeline.tlog\". InitializeBuildStatus: Creating ".\Release\pipeline\pipeline.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified. ClCompile: C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\x86_amd64\CL.exe /c /Isrc/include /Isrc/include/port/win32 /Isrc/include/port/win32_msvc /Zi /nologo /W3 /WX- /Ox /D WIN32 /D _WINDOWS /D __WINDOWS__ /D __WIN32__ /D WIN32_STACK_RLIMIT=4194304 /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _MBCS /GF /Gm- /EHsc /MD /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo".\Release\pipeline\\" /Fd".\Release\pipeline\vc120.pdb" /Gd /TC /wd4018 /wd4244 /wd4273 /wd4102 /wd4090 /wd4267 /errorReport:queue /MP src/test/modules/test_libpq/pipeline.c pipeline.c src/test/modules/test_libpq/pipeline.c(11): fatal error C1083: Cannot open include file: 'libpq-fe.h': No such file or directory [C:\projects\postgresql\pipeline.vcxproj] Done Building Project "C:\projects\postgresql\pipeline.vcxproj" (default targets) -- FAILED. Project "C:\projects\postgresql\pgsql.sln" (1) is building "C:\projects\postgresql\test_parser.vcxproj" (76) on node 1 (default targets). I think the problem is that the project is called pipeline and not test_libpq, so there's no match in the name. I'm going to rename the whole thing to src/test/modules/libpq_pipeline/ and see if the msvc tooling likes that better. -- Álvaro Herrera39°49'30"S 73°17'W
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
v30 contains changes to hopefully make it build on MSVC. -- Álvaro Herrera Valdivia, Chile diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 0553279314..c16befa314 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -3173,6 +3173,33 @@ ExecStatusType PQresultStatus(const PGresult *res); + + + PGRES_PIPELINE_SYNC + + +The PGresult represents a +synchronization point in pipeline mode, requested by +. +This status occurs only when pipeline mode has been selected. + + + + + + PGRES_PIPELINE_ABORTED + + +The PGresult represents a pipeline that has +received an error from the server. PQgetResult +must be called repeatedly, and each time it will return this status code +until the end of the current pipeline, at which point it will return +PGRES_PIPELINE_SYNC and normal processing can +resume. + + + + If the result status is PGRES_TUPLES_OK or @@ -4919,6 +4946,498 @@ int PQflush(PGconn *conn); + + Pipeline Mode + + + libpq + pipeline mode + + + + pipelining + in libpq + + + + batch mode + in libpq + + + + libpq pipeline mode allows applications to + send a query without having to read the result of the previously + sent query. Taking advantage of the pipeline mode, a client will wait + less for the server, since multiple queries/results can be sent/ + received in a single network transaction. + + + + While pipeline mode provides a significant performance boost, writing + clients using the pipeline mode is more complex because it involves + managing a queue of pending queries and finding which result + corresponds to which query in the queue. + + + + Pipeline mode also generally consumes more memory on both the client and server, + though careful and aggressive management of the send/receive queue can mitigate + this. This applies whether or not the connection is in blocking or non-blocking + mode. + + + + Using Pipeline Mode + + +To issue pipelines, the application must switch a connection into pipeline mode. +Enter pipeline mode with +or test whether pipeline mode is active with +. +In pipeline mode, only asynchronous operations +are permitted, and COPY is disallowed. +Using any synchronous command execution functions, +such as PQfn, or PQexec +and its sibling functions, is an error condition. + + + + + It is best to use pipeline mode with libpq in + non-blocking mode. If used + in blocking mode it is possible for a client/server deadlock to occur. + + +The client will block trying to send queries to the server, but the +server will block trying to send results to the client from queries +it has already processed. This only occurs when the client sends +enough queries to fill both its output buffer and the server's receive +buffer before it switches to processing input from the server, +but it's hard to predict exactly when that will happen. + + + + + + +Issuing Queries + + + After entering pipeline mode, the application dispatches requests using + , + or its prepared-query sibling + . + These requests are queued on the client-side until flushed to the server; + this occurs when is used to + establish a synchronization point in the pipeline, + or when is called. + The functions , + , and + also work in pipeline mode. + Result processing is described below. + + + + The server executes statements, and returns results, in the order the + client sends them. The server will begin executing the commands in the + pipeline immediately, not waiting for the end of the pipeline. + If any statement encounters an error, the server aborts the current + transaction and skips processing commands in the pipeline until the + next synchronization point established by PQsendPipeline. + (This remains true even if the commands in the pipeline would rollback + the transaction.) + Query processing resumes after the synchronization point. + + + + It's fine for one operation to depend on the results of a + prior one; for example, one query may define a table that the next + query in the same pipeline uses. Similarly, an application may + create a named prepared statement and execute it with later + statements in the same pipeline. + + + + +Processing Results + + + To process the result of one query in a pipeline, the application calls + PQgetResult repeatedly and handles each result + until PQgetResult
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
> I think it's just because you forgot the patch. > https://www.postgresql.org/message-id/20210304142627.GA5978%40alvherre.pgsql -- Álvaro Herrera39°49'30"S 73°17'W diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 0553279314..c16befa314 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -3173,6 +3173,33 @@ ExecStatusType PQresultStatus(const PGresult *res); + + + PGRES_PIPELINE_SYNC + + +The PGresult represents a +synchronization point in pipeline mode, requested by +. +This status occurs only when pipeline mode has been selected. + + + + + + PGRES_PIPELINE_ABORTED + + +The PGresult represents a pipeline that has +received an error from the server. PQgetResult +must be called repeatedly, and each time it will return this status code +until the end of the current pipeline, at which point it will return +PGRES_PIPELINE_SYNC and normal processing can +resume. + + + + If the result status is PGRES_TUPLES_OK or @@ -4919,6 +4946,498 @@ int PQflush(PGconn *conn); + + Pipeline Mode + + + libpq + pipeline mode + + + + pipelining + in libpq + + + + batch mode + in libpq + + + + libpq pipeline mode allows applications to + send a query without having to read the result of the previously + sent query. Taking advantage of the pipeline mode, a client will wait + less for the server, since multiple queries/results can be sent/ + received in a single network transaction. + + + + While pipeline mode provides a significant performance boost, writing + clients using the pipeline mode is more complex because it involves + managing a queue of pending queries and finding which result + corresponds to which query in the queue. + + + + Pipeline mode also generally consumes more memory on both the client and server, + though careful and aggressive management of the send/receive queue can mitigate + this. This applies whether or not the connection is in blocking or non-blocking + mode. + + + + Using Pipeline Mode + + +To issue pipelines, the application must switch a connection into pipeline mode. +Enter pipeline mode with +or test whether pipeline mode is active with +. +In pipeline mode, only asynchronous operations +are permitted, and COPY is disallowed. +Using any synchronous command execution functions, +such as PQfn, or PQexec +and its sibling functions, is an error condition. + + + + + It is best to use pipeline mode with libpq in + non-blocking mode. If used + in blocking mode it is possible for a client/server deadlock to occur. + + +The client will block trying to send queries to the server, but the +server will block trying to send results to the client from queries +it has already processed. This only occurs when the client sends +enough queries to fill both its output buffer and the server's receive +buffer before it switches to processing input from the server, +but it's hard to predict exactly when that will happen. + + + + + + +Issuing Queries + + + After entering pipeline mode, the application dispatches requests using + , + or its prepared-query sibling + . + These requests are queued on the client-side until flushed to the server; + this occurs when is used to + establish a synchronization point in the pipeline, + or when is called. + The functions , + , and + also work in pipeline mode. + Result processing is described below. + + + + The server executes statements, and returns results, in the order the + client sends them. The server will begin executing the commands in the + pipeline immediately, not waiting for the end of the pipeline. + If any statement encounters an error, the server aborts the current + transaction and skips processing commands in the pipeline until the + next synchronization point established by PQsendPipeline. + (This remains true even if the commands in the pipeline would rollback + the transaction.) + Query processing resumes after the synchronization point. + + + + It's fine for one operation to depend on the results of a + prior one; for example, one query may define a table that the next + query in the same pipeline uses. Similarly, an application may + create a named prepared statement and execute it with later + statements in the same pipeline. + + + + +Processing Results + + + To process the result of one query in a pipeline, the
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
On Thu, Mar 04, 2021 at 12:01:37PM -0300, Alvaro Herrera wrote: > Apparently, the archives system or the commitfest system is not picking > up new messages to the thread, so the CF app is trying to apply a > very old patch version. I'm not sure what's up with that. Thomas, any > clues on where to look? I think it's just because you forgot the patch. https://www.postgresql.org/message-id/20210304142627.GA5978%40alvherre.pgsql -- Justin
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
On Thu, Mar 4, 2021 at 8:01 PM Alvaro Herrera wrote: > Apparently, the archives system or the commitfest system is not picking > up new messages to the thread, so the CF app is trying to apply a > very old patch version. I'm not sure what's up with that. Thomas, any > clues on where to look? > > -- > Álvaro Herrera Valdivia, Chile > "Oh, great altar of passive entertainment, bestow upon me thy discordant > images > at such speed as to render linear thought impossible" (Calvin a la TV) > Hi Alvaro, The thread splits and CF app still has the previous thread. The old thread has the v18 as the latest patch which is failing. The new thread which ( https://www.postgresql.org/message-id/CAJkzx4T5E-2cQe3dtv2R78dYFvz%2Bin8PY7A8MArvLhs_pg75gg%40mail.gmail.com ) has the new patch. -- Ibrar Ahmed
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
Apparently, the archives system or the commitfest system is not picking up new messages to the thread, so the CF app is trying to apply a very old patch version. I'm not sure what's up with that. Thomas, any clues on where to look? -- Álvaro Herrera Valdivia, Chile "Oh, great altar of passive entertainment, bestow upon me thy discordant images at such speed as to render linear thought impossible" (Calvin a la TV)
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
On 2021-Mar-04, Ibrar Ahmed wrote: > The build is failing for this patch, can you please take a look at this? > > https://cirrus-ci.com/task/4568547922804736 > https://ci.appveyor.com/project/postgresql-cfbot/postgresql/build/1.0.129221 > > > I am marking the patch "Waiting on Author" I don't know why you chose to respond to such an old message, but here's a rebase which also includes the workaround I suggested last night for the problem of the outdated query printed by error messages, as well as Justin's suggested fixes. (I also made a few tweaks to the TAP test). -- Álvaro Herrera Valdivia, Chile "No renuncies a nada. No te aferres a nada."
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
On Wed, Jul 15, 2020 at 12:18 AM Andres Freund wrote: > Hi, > > On 2020-07-10 19:01:49 -0400, Alvaro Herrera wrote: > > Totally unasked for, here's a rebase of this patch series. I didn't do > > anything other than rebasing to current master, solving a couple of very > > trivial conflicts, fixing some whitespace complaints by git apply, and > > running tests to verify everthing works. > > > > I don't foresee working on this at all, so if anyone is interested in > > seeing this feature in, I encourage them to read and address > > Horiguchi-san's feedback. > > Nor am I planning to do so, but I do think its a pretty important > improvement. > > > > > +/* > > + * PQrecyclePipelinedCommand > > + * Push a command queue entry onto the freelist. It must be a > dangling entry > > + * with null next pointer and not referenced by any other entry's > next pointer. > > + */ > > Dangling sounds a bit like it's already freed. > > > > > +/* > > + * PQbatchSendQueue > > + * End a batch submission by sending a protocol sync. The connection > will > > + * remain in batch mode and unavailable for new synchronous command > execution > > + * functions until all results from the batch are processed by the > client. > > I feel like the reference to the protocol sync is a bit too low level > for an external API. It should first document what the function does > from a user's POV. > > I think it'd also be good to document whether / whether not queries can > already have been sent before PQbatchSendQueue is called or not. > > > > +/* > > + * PQbatchProcessQueue > > + *In batch mode, start processing the next query in the queue. > > + * > > + * Returns 1 if the next query was popped from the queue and can > > + * be processed by PQconsumeInput, PQgetResult, etc. > > + * > > + * Returns 0 if the current query isn't done yet, the connection > > + * is not in a batch, or there are no more queries to process. > > + */ > > +int > > +PQbatchProcessQueue(PGconn *conn) > > +{ > > + PGcommandQueueEntry *next_query; > > + > > + if (!conn) > > + return 0; > > + > > + if (conn->batch_status == PQBATCH_MODE_OFF) > > + return 0; > > + > > + switch (conn->asyncStatus) > > + { > > + case PGASYNC_COPY_IN: > > + case PGASYNC_COPY_OUT: > > + case PGASYNC_COPY_BOTH: > > + printfPQExpBuffer(>errorMessage, > > +libpq_gettext_noop("internal error, > COPY in batch mode")); > > + break; > > Shouldn't there be a return 0 here? > > > > > + if (conn->batch_status == PQBATCH_MODE_ABORTED && conn->queryclass > != PGQUERY_SYNC) > > + { > > + /* > > + * In an aborted batch we don't get anything from the > server for each > > + * result; we're just discarding input until we get to the > next sync > > + * from the server. The client needs to know its queries > got aborted > > + * so we create a fake PGresult to return immediately from > > + * PQgetResult. > > + */ > > + conn->result = PQmakeEmptyPGresult(conn, > > + > PGRES_BATCH_ABORTED); > > + if (!conn->result) > > + { > > + printfPQExpBuffer(>errorMessage, > > + > libpq_gettext("out of memory")); > > + pqSaveErrorResult(conn); > > + return 0; > > Is there any way an application can recover at this point? ISTM we'd be > stuck in the previous asyncStatus, no? > > > > +/* pqBatchFlush > > + * In batch mode, data will be flushed only when the out buffer reaches > the threshold value. > > + * In non-batch mode, data will be flushed all the time. > > + */ > > +static int > > +pqBatchFlush(PGconn *conn) > > +{ > > + if ((conn->batch_status == PQBATCH_MODE_OFF)||(conn->outCount >= > OUTBUFFER_THRESHOLD)) > > + return(pqFlush(conn)); > > + return 0; /* Just to keep compiler quiet */ > > +} > > unnecessarily long line. > > > > +/* > > + * Connection's outbuffer threshold is set to 64k as it is safe > > + * in Windows as per comments in pqSendSome() API. > > + */ > > +#define OUTBUFFER_THRESHOLD 65536 > > I don't think the comment explains much. It's fine to send more than 64k > with pqSendSome(), they'll just be send with separate pgsecure_write() > invocations. And only on windows. > > It clearly makes sense to start sending out data at a certain > granularity to avoid needing unnecessary amounts of memory, and to make > more efficient use of latency / serer side compute. > > It's not implausible that 64k is the right amount for that, I just don't > think the explanation above is good. > > > diff --git a/src/test/modules/test_libpq/testlibpqbatch.c > b/src/test/modules/test_libpq/testlibpqbatch.c > > new file mode 100644 > > index 00..4d6ba266e5 > > --- /dev/null > > +++ b/src/test/modules/test_libpq/testlibpqbatch.c > > @@
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
Hi, On 2020-07-10 19:01:49 -0400, Alvaro Herrera wrote: > Totally unasked for, here's a rebase of this patch series. I didn't do > anything other than rebasing to current master, solving a couple of very > trivial conflicts, fixing some whitespace complaints by git apply, and > running tests to verify everthing works. > > I don't foresee working on this at all, so if anyone is interested in > seeing this feature in, I encourage them to read and address > Horiguchi-san's feedback. Nor am I planning to do so, but I do think its a pretty important improvement. > +/* > + * PQrecyclePipelinedCommand > + * Push a command queue entry onto the freelist. It must be a dangling > entry > + * with null next pointer and not referenced by any other entry's next > pointer. > + */ Dangling sounds a bit like it's already freed. > +/* > + * PQbatchSendQueue > + * End a batch submission by sending a protocol sync. The connection will > + * remain in batch mode and unavailable for new synchronous command > execution > + * functions until all results from the batch are processed by the client. I feel like the reference to the protocol sync is a bit too low level for an external API. It should first document what the function does from a user's POV. I think it'd also be good to document whether / whether not queries can already have been sent before PQbatchSendQueue is called or not. > +/* > + * PQbatchProcessQueue > + *In batch mode, start processing the next query in the queue. > + * > + * Returns 1 if the next query was popped from the queue and can > + * be processed by PQconsumeInput, PQgetResult, etc. > + * > + * Returns 0 if the current query isn't done yet, the connection > + * is not in a batch, or there are no more queries to process. > + */ > +int > +PQbatchProcessQueue(PGconn *conn) > +{ > + PGcommandQueueEntry *next_query; > + > + if (!conn) > + return 0; > + > + if (conn->batch_status == PQBATCH_MODE_OFF) > + return 0; > + > + switch (conn->asyncStatus) > + { > + case PGASYNC_COPY_IN: > + case PGASYNC_COPY_OUT: > + case PGASYNC_COPY_BOTH: > + printfPQExpBuffer(>errorMessage, > +libpq_gettext_noop("internal error, COPY in > batch mode")); > + break; Shouldn't there be a return 0 here? > + if (conn->batch_status == PQBATCH_MODE_ABORTED && conn->queryclass != > PGQUERY_SYNC) > + { > + /* > + * In an aborted batch we don't get anything from the server > for each > + * result; we're just discarding input until we get to the next > sync > + * from the server. The client needs to know its queries got > aborted > + * so we create a fake PGresult to return immediately from > + * PQgetResult. > + */ > + conn->result = PQmakeEmptyPGresult(conn, > + >PGRES_BATCH_ABORTED); > + if (!conn->result) > + { > + printfPQExpBuffer(>errorMessage, > + libpq_gettext("out of > memory")); > + pqSaveErrorResult(conn); > + return 0; Is there any way an application can recover at this point? ISTM we'd be stuck in the previous asyncStatus, no? > +/* pqBatchFlush > + * In batch mode, data will be flushed only when the out buffer reaches the > threshold value. > + * In non-batch mode, data will be flushed all the time. > + */ > +static int > +pqBatchFlush(PGconn *conn) > +{ > + if ((conn->batch_status == PQBATCH_MODE_OFF)||(conn->outCount >= > OUTBUFFER_THRESHOLD)) > + return(pqFlush(conn)); > + return 0; /* Just to keep compiler quiet */ > +} unnecessarily long line. > +/* > + * Connection's outbuffer threshold is set to 64k as it is safe > + * in Windows as per comments in pqSendSome() API. > + */ > +#define OUTBUFFER_THRESHOLD 65536 I don't think the comment explains much. It's fine to send more than 64k with pqSendSome(), they'll just be send with separate pgsecure_write() invocations. And only on windows. It clearly makes sense to start sending out data at a certain granularity to avoid needing unnecessary amounts of memory, and to make more efficient use of latency / serer side compute. It's not implausible that 64k is the right amount for that, I just don't think the explanation above is good. > diff --git a/src/test/modules/test_libpq/testlibpqbatch.c > b/src/test/modules/test_libpq/testlibpqbatch.c > new file mode 100644 > index 00..4d6ba266e5 > --- /dev/null > +++ b/src/test/modules/test_libpq/testlibpqbatch.c > @@ -0,0 +1,1456 @@ > +/* > + * src/test/modules/test_libpq/testlibpqbatch.c > + * > + * > + * testlibpqbatch.c > + * Test of batch execution functionality > + */
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
On 2019-Sep-09, Amit Kapila wrote: > Thanks for picking up this. However, I noticed that previously > Horiguchi-San has given some comments on this patch [1] which doesn't > seem to be addressed or at least not all of them are addressed. It is > possible that you would have already addressed those, but in that > case, it would be good if you respond to his email as well. If those > are not addressed, then it will be good to address those. Totally unasked for, here's a rebase of this patch series. I didn't do anything other than rebasing to current master, solving a couple of very trivial conflicts, fixing some whitespace complaints by git apply, and running tests to verify everthing works. I don't foresee working on this at all, so if anyone is interested in seeing this feature in, I encourage them to read and address Horiguchi-san's feedback. -- Álvaro Herrerahttps://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services >From c2de4e2a2cbc301b48a7946a9ee0b9acceae7233 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Fri, 10 Jul 2020 11:29:11 -0400 Subject: [PATCH v18 1/2] libpq batch support --- doc/src/sgml/libpq.sgml | 502 +++ doc/src/sgml/lobj.sgml| 4 + .../libpqwalreceiver/libpqwalreceiver.c | 3 + src/interfaces/libpq/exports.txt | 5 + src/interfaces/libpq/fe-connect.c | 27 + src/interfaces/libpq/fe-exec.c| 597 -- src/interfaces/libpq/fe-protocol2.c | 6 + src/interfaces/libpq/fe-protocol3.c | 15 +- src/interfaces/libpq/libpq-fe.h | 24 +- src/interfaces/libpq/libpq-int.h | 47 +- 10 files changed, 1186 insertions(+), 44 deletions(-) diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index d1ccaa775a..ad168727c7 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -4826,6 +4826,500 @@ int PQflush(PGconn *conn); + + Batch mode and query pipelining + + + libpq + batch mode + + + + libpq + pipelining + + + + libpq supports queueing up queries into + a pipeline to be executed as a batch on the server. Batching queries allows + applications to avoid a client/server round-trip after each query to get + the results before issuing the next query. + + + + When to use batching + + +Much like asynchronous query mode, there is no performance disadvantage to +using batching and pipelining. It increases client application complexity +and extra caution is required to prevent client/server deadlocks but +can sometimes offer considerable performance improvements. + + + +Batching is most useful when the server is distant, i.e. network latency +(ping time) is high, and when many small operations are being performed in +rapid sequence. There is usually less benefit in using batches when each +query takes many multiples of the client/server round-trip time to execute. +A 100-statement operation run on a server 300ms round-trip-time away would take +30 seconds in network latency alone without batching; with batching it may spend +as little as 0.3s waiting for results from the server. + + + +Use batches when your application does lots of small +INSERT, UPDATE and +DELETE operations that can't easily be transformed into +operations on sets or into a +COPY operation. + + + +Batching is not useful when information from one operation is required by the +client before it knows enough to send the next operation. The client must +introduce a synchronisation point and wait for a full client/server +round-trip to get the results it needs. However, it's often possible to +adjust the client design to exchange the required information server-side. +Read-modify-write cycles are especially good candidates; for example: + + BEGIN; + SELECT x FROM mytable WHERE id = 42 FOR UPDATE; + -- result: x=2 + -- client adds 1 to x: + UPDATE mytable SET x = 3 WHERE id = 42; + COMMIT; + +could be much more efficiently done with: + + UPDATE mytable SET x = x + 1 WHERE id = 42; + + + + + + The batch API was introduced in PostgreSQL 10.0, but clients using PostgresSQL 10.0 version of libpq can + use batches on server versions 7.4 and newer. Batching works on any server + that supports the v3 extended query protocol. + + + + + + + Using batch mode + + +To issue batches the application must switch +a connection into batch mode. Enter batch mode with PQenterBatchMode(conn) or test +whether batch mode is active with PQbatchStatus(conn). In batch mode only asynchronous operations are permitted, and +COPY is not recommended as it most likely will trigger failure in batch processing. +Using any synchronous command execution
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
On Fri, Aug 30, 2019 at 7:06 PM Nikhil Sontakke wrote: > > Hi, > > > > This patch has been around for some time now, the last version fails to > > > apply cleanly and in-depth reviews have happened. I am moving that to > > > the next CF, waiting on its author. > > > > Unfortunately, nothing was changed since then, so there is already some > > amount > > of unaddressed review feedback. I'll move this patch to "Returned with > > feedback". > > > > Craig Ringer mentioned about this thread to me recently. > > This effort has seen decent reviews from Craig, Andres and Michael > already. So, I thought of refreshing it to work against latest master > HEAD. > Thanks for picking up this. However, I noticed that previously Horiguchi-San has given some comments on this patch [1] which doesn't seem to be addressed or at least not all of them are addressed. It is possible that you would have already addressed those, but in that case, it would be good if you respond to his email as well. If those are not addressed, then it will be good to address those. [1] - https://www.postgresql.org/message-id/20180322.211148.187821341.horiguchi.kyotaro%40lab.ntt.co.jp -- With Regards, Amit Kapila. EnterpriseDB: http://www.enterprisedb.com
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
Hi, > > This patch has been around for some time now, the last version fails to > > apply cleanly and in-depth reviews have happened. I am moving that to > > the next CF, waiting on its author. > > Unfortunately, nothing was changed since then, so there is already some amount > of unaddressed review feedback. I'll move this patch to "Returned with > feedback". > Craig Ringer mentioned about this thread to me recently. This effort has seen decent reviews from Craig, Andres and Michael already. So, I thought of refreshing it to work against latest master HEAD. PFA, main patch as well as the test patch (I named the test patch v17 to be consistent with the main patch). The major grouse with the test patch AFAICS was the use of non-Windows compliant timersub() function. I have now used INSTR_TIME_SET_CURRENT/INSTR_TIME_SUBTRACT family of portable macros for the same. Please let me know on what we think of the above. Regards, Nikhil -- Nikhil Sontakke 2ndQuadrant - PostgreSQL Solutions for the Enterprise https://www.2ndQuadrant.com/ 0002-libpq_batch_tests_community_master.v17.patch Description: Binary data 0001-libpq_batch_support_commmunity_master.v17.patch Description: Binary data
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
> On Mon, Oct 1, 2018 at 8:34 AM Michael Paquier wrote: > > On Fri, Mar 23, 2018 at 02:18:09PM +0100, Daniel Verite wrote: > > There's a batch mode for pgbench in a patch posted in [1], > > with \beginbatch and \endbatch commands, but nothing > > for psql AFAICS. > > psql is more complicated because currently it uses a > > blocking PQexec() call at its core. Craig mentioned psql > > integration in [2] and [3]. > > This patch has been around for some time now, the last version fails to > apply cleanly and in-depth reviews have happened. I am moving that to > the next CF, waiting on its author. Unfortunately, nothing was changed since then, so there is already some amount of unaddressed review feedback. I'll move this patch to "Returned with feedback".
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
On Fri, Mar 23, 2018 at 02:18:09PM +0100, Daniel Verite wrote: > There's a batch mode for pgbench in a patch posted in [1], > with \beginbatch and \endbatch commands, but nothing > for psql AFAICS. > psql is more complicated because currently it uses a > blocking PQexec() call at its core. Craig mentioned psql > integration in [2] and [3]. This patch has been around for some time now, the last version fails to apply cleanly and in-depth reviews have happened. I am moving that to the next CF, waiting on its author. -- Michael signature.asc Description: PGP signature
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
Kyotaro HORIGUCHI wrote: > A disucssion on psql batch mode was held in another branch of > this thread. How do we treat that? There's a batch mode for pgbench in a patch posted in [1], with \beginbatch and \endbatch commands, but nothing for psql AFAICS. psql is more complicated because currently it uses a blocking PQexec() call at its core. Craig mentioned psql integration in [2] and [3]. Also a script can have inter-query dependencies such as in insert into table(...) returning id \gset update othertable set col= :id where ...; which is a problem in batch mode, as we don't want to send the update before the right value for :id is known. Whether we want to support these dependencies and how needs discussion. For instance we might not support them at all, or create a synchronization command that collects all results of queries sent so far, or do it implicitly when a variable is injected into a query... This looks like substantial work that might be best done separately from the libpq patch. [1] https://www.postgresql.org/message-id/b4e34135-2bd9-4b8a-94ca-27d760da2...@manitou-mail.org [2] https://www.postgresql.org/message-id/CAMsr+YGLhaDkjymLuNVQy4MrSKQoA=F1vO=aN8XQf30N=aq...@mail.gmail.com [3] https://www.postgresql.org/message-id/CAMsr+YE6BK4iAaQz=ny3xdnblhnnz_4tp-ptjqbnnpszmgo...@mail.gmail.com Best regards, -- Daniel Vérité PostgreSQL-powered mailer: http://www.manitou-mail.org Twitter: @DanielVerite
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
On Fri, Jan 5, 2018 at 4:55 PM, Vaishnavi Prabakaran < vaishnaviprabaka...@gmail.com> wrote: > > > On Tue, Nov 28, 2017 at 12:57 PM, Michael Paquier < > michael.paqu...@gmail.com> wrote: > >> On Thu, Oct 5, 2017 at 9:58 AM, Vaishnavi Prabakaran >>wrote: >> > Thanks for the suggestion and, OK I will create a new patch in upcoming >> > commitfest with attached patch addressing above review comments. >> >> The patch still applies and there has been no updates for the last >> month, as well as no reviews. I am bumping it to next CF. > > > Thank you, I see the patch generates a compilation error due to usage of > "FALSE" with latest postgres code, Hence attaching the patch with > correction. > Corrected compilation error in documentation portion of patch with latest postgres code. Attached the corrected patch. Thanks & Regards, Vaishnavi, Fujitsu Australia. 0001-Pipelining-batch-support-for-libpq-code-v16.patch Description: Binary data
Re: [HACKERS] PATCH: Batch/pipelining support for libpq
On Tue, Nov 28, 2017 at 12:57 PM, Michael Paquierwrote: > On Thu, Oct 5, 2017 at 9:58 AM, Vaishnavi Prabakaran > wrote: > > Thanks for the suggestion and, OK I will create a new patch in upcoming > > commitfest with attached patch addressing above review comments. > > The patch still applies and there has been no updates for the last > month, as well as no reviews. I am bumping it to next CF. Thank you, I see the patch generates a compilation error due to usage of "FALSE" with latest postgres code, Hence attaching the patch with correction. Thanks & Regards, Vaishnavi, Fujitsu Australia. 0001-Pipelining-batch-support-for-libpq-code-v15.patch Description: Binary data