pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Add PQsendSyncMessage() to libpq
37+ messages / 8 participants
[nested] [flat]

Commits from this thread:
  2024-01-17 061cc7eaca  Remove some comments related to pqPipelineSync() and PQsendPipelineSync()
  2024-01-16 4794c2d317  libpq: Add PQsendPipelineSync()
* Add PQsendSyncMessage() to libpq
@ 2023-03-24 22:38 Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-25 14:23 ` Re: Add PQsendSyncMessage() to libpq Denis Laxalde <denis.laxalde@dalibo.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  0 siblings, 2 replies; 37+ messages in thread

From: Anton Kirilov @ 2023-03-24 22:38 UTC (permalink / raw)
  To: pgsql-hackers

Hello,

Recently I have been trying to use libpq's pipeline mode in a project,
and in the process I have noticed that the PQpipelineSync() function
has a deficiency (which, to be fair, could be an advantage in other
situations): It combines the establishment of a synchronization point
in a pipeline with a send buffer flush, i.e. a system call. In my use
case I build up a pipeline of several completely independent queries,
so a synchronization point is required between each of them, but
performing a system call for each is just unnecessary overhead,
especially if the system is severely affected by any mitigations for
Spectre or other security vulnerabilities. That's why I propose to add
an interface to libpq to establish a synchronization point in a
pipeline without performing any further actions.

I have attached a patch that introduces PQsendSyncMessage(), a
function that is equivalent to PQpipelineSync(), except that it does
not flush anything to the server; the user must subsequently call
PQflush() instead. Alternatively, the new function is equivalent to
PQsendFlushRequest(), except that it sends a sync message instead of a
flush request. In addition to reducing the system call overhead of
libpq's pipeline mode, it also makes it easier for the operating
system to send as much of the pipeline as possible in a single TCP (or
lower level protocol) packet when the database is running remotely.

I would appeciate your thoughts on my proposal.

Best wishes,
Anton Kirilov

Attachments:

v1-0001-Add-PQsendSyncMessage-to-libpq.patch (application/x-patch, 13.5K) download From 5b4f7d4e79b8e7f2a58026a8131a21c235603cc0 Mon Sep 17 00:00:00 2001 From: Anton Kirilov <antonvkirilov@gmail.com> Date: Wed, 22 Mar 2023 20:39:57 +0000 Subject: [PATCH v1] Add PQsendSyncMessage() to libpq This new function is equivalent to PQpipelineSync(), except that it does not flush anything to the server; the user must subsequently call PQflush() instead. Its purpose is to reduce the system call overhead of pipeline mode. --- doc/src/sgml/libpq.sgml | 45 ++++++-- src/interfaces/libpq/exports.txt | 1 + src/interfaces/libpq/fe-exec.c | 24 ++++- src/interfaces/libpq/libpq-fe.h | 1 + .../modules/libpq_pipeline/libpq_pipeline.c | 101 ++++++++++++++++++ .../libpq_pipeline/t/001_libpq_pipeline.pl | 4 +- .../traces/multi_pipelines_noflush.trace | 23 ++++ 7 files changed, 189 insertions(+), 10 deletions(-) create mode 100644 src/test/modules/libpq_pipeline/traces/multi_pipelines_noflush.trace diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 9ee5532c07..70b90331b0 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -3318,8 +3318,9 @@ ExecStatusType PQresultStatus(const PGresult *res); <listitem> <para> The <structname>PGresult</structname> represents a - synchronization point in pipeline mode, requested by - <xref linkend="libpq-PQpipelineSync"/>. + synchronization point in pipeline mode, requested by either + <xref linkend="libpq-PQpipelineSync"/> or + <xref linkend="libpq-PQsendSyncMessage"/>. This status occurs only when pipeline mode has been selected. </para> </listitem> @@ -4847,7 +4848,8 @@ int PQsendDescribePortal(PGconn *conn, const char *portalName); <xref linkend="libpq-PQsendPrepare"/>, <xref linkend="libpq-PQsendQueryPrepared"/>, <xref linkend="libpq-PQsendDescribePrepared"/>, - <xref linkend="libpq-PQsendDescribePortal"/>, or + <xref linkend="libpq-PQsendDescribePortal"/>, + <xref linkend="libpq-PQsendSyncMessage"/>, or <xref linkend="libpq-PQpipelineSync"/> call, and returns it. A null pointer is returned when the command is complete and there @@ -5227,8 +5229,9 @@ int PQflush(PGconn *conn); client sends them. The server will begin executing the commands in the pipeline immediately, not waiting for the end of the pipeline. Note that results are buffered on the server side; the server flushes - that buffer when a synchronization point is established with - <function>PQpipelineSync</function>, or when + that buffer when a synchronization point is established with either + <function>PQpipelineSync</function> or + <function>PQsendSyncMessage</function>, or when <function>PQsendFlushRequest</function> is called. If any statement encounters an error, the server aborts the current transaction and does not execute any subsequent command in the queue @@ -5285,7 +5288,8 @@ int PQflush(PGconn *conn); <type>PGresult</type> types <literal>PGRES_PIPELINE_SYNC</literal> and <literal>PGRES_PIPELINE_ABORTED</literal>. <literal>PGRES_PIPELINE_SYNC</literal> is reported exactly once for each - <function>PQpipelineSync</function> at the corresponding point + <function>PQpipelineSync</function> or + <function>PQsendSyncMessage</function> at the corresponding point in the pipeline. <literal>PGRES_PIPELINE_ABORTED</literal> is emitted in place of a normal query result for the first error and all subsequent results @@ -5323,7 +5327,8 @@ int PQflush(PGconn *conn); <function>PQresultStatus</function> will report a <literal>PGRES_PIPELINE_ABORTED</literal> result for each remaining queued operation in an aborted pipeline. The result for - <function>PQpipelineSync</function> is reported as + <function>PQpipelineSync</function> or + <function>PQsendSyncMessage</function> is reported as <literal>PGRES_PIPELINE_SYNC</literal> to signal the end of the aborted pipeline and resumption of normal result processing. </para> @@ -5555,6 +5560,32 @@ int PQsendFlushRequest(PGconn *conn); </para> </listitem> </varlistentry> + + <varlistentry id="libpq-PQsendSyncMessage"> + <term><function>PQsendSyncMessage</function><indexterm><primary>PQsendSyncMessage</primary></indexterm></term> + + <listitem> + <para> + Marks a synchronization point in a pipeline by sending a + <link linkend="protocol-flow-ext-query">sync message</link> + without flushing the send buffer. This serves as + the delimiter of an implicit transaction and an error recovery + point; see <xref linkend="libpq-pipeline-errors"/>. + +<synopsis> +int PQsendSyncMessage(PGconn *conn); +</synopsis> + </para> + <para> + Returns 1 for success. Returns 0 if the connection is not in + pipeline mode or sending a + <link linkend="protocol-flow-ext-query">sync message</link> + failed. + Note that the message is not itself flushed to the server automatically; + use <function>PQflush</function> if necessary. + </para> + </listitem> + </varlistentry> </variablelist> </sect2> diff --git a/src/interfaces/libpq/exports.txt b/src/interfaces/libpq/exports.txt index e8bcc88370..9c7818408b 100644 --- a/src/interfaces/libpq/exports.txt +++ b/src/interfaces/libpq/exports.txt @@ -186,3 +186,4 @@ PQpipelineStatus 183 PQsetTraceFlags 184 PQmblenBounded 185 PQsendFlushRequest 186 +PQsendSyncMessage 187 diff --git a/src/interfaces/libpq/fe-exec.c b/src/interfaces/libpq/fe-exec.c index ec62550e38..1baa662b0d 100644 --- a/src/interfaces/libpq/fe-exec.c +++ b/src/interfaces/libpq/fe-exec.c @@ -82,6 +82,7 @@ static int PQsendDescribe(PGconn *conn, char desc_type, static int check_field_number(const PGresult *res, int field_num); static void pqPipelineProcessQueue(PGconn *conn); static int pqPipelineFlush(PGconn *conn); +static int send_sync_message(PGconn *conn, int flush); /* ---------------- @@ -3139,6 +3140,17 @@ pqPipelineProcessQueue(PGconn *conn) */ int PQpipelineSync(PGconn *conn) +{ + return send_sync_message(conn, 1); +} + +/* + * send_sync_message: subroutine for PQpipelineSync and PQsendSyncMessage + * Send a Sync message as part of a pipeline, + * and optionally flush to server + */ +static int +send_sync_message(PGconn *conn, int flush) { PGcmdQueueEntry *entry; @@ -3185,7 +3197,7 @@ PQpipelineSync(PGconn *conn) * Give the data a push. In nonblock mode, don't complain if we're unable * to send it all; PQgetResult() will do any additional flushing needed. */ - if (PQflush(conn) < 0) + if (flush && PQflush(conn) < 0) goto sendFailed; /* OK, it's launched! */ @@ -3234,6 +3246,16 @@ PQsendFlushRequest(PGconn *conn) return 1; } +/* + * PQsendSyncMessage + * Send a Sync message as part of a pipeline without flushing to server + */ +int +PQsendSyncMessage(PGconn *conn) +{ + return send_sync_message(conn, 0); +} + /* ====== accessor funcs for PGresult ======== */ ExecStatusType diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h index f3d9220496..e22197ee95 100644 --- a/src/interfaces/libpq/libpq-fe.h +++ b/src/interfaces/libpq/libpq-fe.h @@ -473,6 +473,7 @@ extern int PQenterPipelineMode(PGconn *conn); extern int PQexitPipelineMode(PGconn *conn); extern int PQpipelineSync(PGconn *conn); extern int PQsendFlushRequest(PGconn *conn); +extern int PQsendSyncMessage(PGconn *conn); /* LISTEN/NOTIFY support */ extern PGnotify *PQnotifies(PGconn *conn); diff --git a/src/test/modules/libpq_pipeline/libpq_pipeline.c b/src/test/modules/libpq_pipeline/libpq_pipeline.c index f48da7d963..829907957a 100644 --- a/src/test/modules/libpq_pipeline/libpq_pipeline.c +++ b/src/test/modules/libpq_pipeline/libpq_pipeline.c @@ -244,6 +244,104 @@ test_multi_pipelines(PGconn *conn) fprintf(stderr, "ok\n"); } +static void +test_multi_pipelines_noflush(PGconn *conn) +{ + PGresult *res = NULL; + const char *dummy_params[1] = {"1"}; + Oid dummy_param_oids[1] = {INT4OID}; + + fprintf(stderr, "multi pipeline... "); + + /* + * Queue up a couple of small pipelines and process each without returning + * to command mode first, flushing only once. + */ + if (PQenterPipelineMode(conn) != 1) + pg_fatal("failed to enter pipeline mode: %s", PQerrorMessage(conn)); + + if (PQsendQueryParams(conn, "SELECT $1", 1, dummy_param_oids, + dummy_params, NULL, NULL, 0) != 1) + pg_fatal("dispatching first SELECT failed: %s", PQerrorMessage(conn)); + + if (PQsendSyncMessage(conn) != 1) + pg_fatal("Pipeline sync failed: %s", PQerrorMessage(conn)); + + if (PQsendQueryParams(conn, "SELECT $1", 1, dummy_param_oids, + dummy_params, NULL, NULL, 0) != 1) + pg_fatal("dispatching second SELECT failed: %s", PQerrorMessage(conn)); + + if (PQpipelineSync(conn) != 1) + pg_fatal("pipeline sync failed: %s", PQerrorMessage(conn)); + + /* OK, start processing the results */ + res = PQgetResult(conn); + if (res == NULL) + pg_fatal("PQgetResult returned null when there's a pipeline item: %s", + PQerrorMessage(conn)); + + if (PQresultStatus(res) != PGRES_TUPLES_OK) + pg_fatal("Unexpected result code %s from first pipeline item", + PQresStatus(PQresultStatus(res))); + PQclear(res); + res = NULL; + + if (PQgetResult(conn) != NULL) + pg_fatal("PQgetResult returned something extra after first result"); + + if (PQexitPipelineMode(conn) != 0) + pg_fatal("exiting pipeline mode after query but before sync succeeded incorrectly"); + + res = PQgetResult(conn); + if (res == NULL) + pg_fatal("PQgetResult returned null when sync result expected: %s", + PQerrorMessage(conn)); + + if (PQresultStatus(res) != PGRES_PIPELINE_SYNC) + pg_fatal("Unexpected result code %s instead of sync result, error: %s", + PQresStatus(PQresultStatus(res)), PQerrorMessage(conn)); + PQclear(res); + + /* second pipeline */ + + res = PQgetResult(conn); + if (res == NULL) + pg_fatal("PQgetResult returned null when there's a pipeline item: %s", + PQerrorMessage(conn)); + + if (PQresultStatus(res) != PGRES_TUPLES_OK) + pg_fatal("Unexpected result code %s from second pipeline item", + PQresStatus(PQresultStatus(res))); + + res = PQgetResult(conn); + if (res != NULL) + pg_fatal("Expected null result, got %s", + PQresStatus(PQresultStatus(res))); + + res = PQgetResult(conn); + if (res == NULL) + pg_fatal("PQgetResult returned null when there's a pipeline item: %s", + PQerrorMessage(conn)); + + if (PQresultStatus(res) != PGRES_PIPELINE_SYNC) + pg_fatal("Unexpected result code %s from second pipeline sync", + PQresStatus(PQresultStatus(res))); + + /* We're still in pipeline mode ... */ + if (PQpipelineStatus(conn) == PQ_PIPELINE_OFF) + pg_fatal("Fell out of pipeline mode somehow"); + + /* until we end it, which we can safely do now */ + if (PQexitPipelineMode(conn) != 1) + pg_fatal("attempt to exit pipeline mode failed when it should've succeeded: %s", + PQerrorMessage(conn)); + + if (PQpipelineStatus(conn) != PQ_PIPELINE_OFF) + pg_fatal("exiting pipeline mode didn't seem to work"); + + fprintf(stderr, "ok\n"); +} + /* * Test behavior when a pipeline dispatches a number of commands that are * not flushed by a sync point. @@ -1683,6 +1781,7 @@ print_test_list(void) { printf("disallowed_in_pipeline\n"); printf("multi_pipelines\n"); + printf("multi_pipelines_noflush\n"); printf("nosync\n"); printf("pipeline_abort\n"); printf("pipeline_idle\n"); @@ -1786,6 +1885,8 @@ main(int argc, char **argv) test_disallowed_in_pipeline(conn); else if (strcmp(testname, "multi_pipelines") == 0) test_multi_pipelines(conn); + else if (strcmp(testname, "multi_pipelines_noflush") == 0) + test_multi_pipelines_noflush(conn); else if (strcmp(testname, "nosync") == 0) test_nosync(conn); else if (strcmp(testname, "pipeline_abort") == 0) diff --git a/src/test/modules/libpq_pipeline/t/001_libpq_pipeline.pl b/src/test/modules/libpq_pipeline/t/001_libpq_pipeline.pl index 7560439fec..a8a4477b15 100644 --- a/src/test/modules/libpq_pipeline/t/001_libpq_pipeline.pl +++ b/src/test/modules/libpq_pipeline/t/001_libpq_pipeline.pl @@ -37,8 +37,8 @@ for my $testname (@tests) { my @extraargs = ('-r', $numrows); my $cmptrace = grep(/^$testname$/, - qw(simple_pipeline nosync multi_pipelines prepared singlerow - pipeline_abort pipeline_idle transaction + qw(simple_pipeline nosync multi_pipelines multi_pipelines_noflush + prepared singlerow pipeline_abort pipeline_idle transaction disallowed_in_pipeline)) > 0; # For a bunch of tests, generate a libpq trace file too. diff --git a/src/test/modules/libpq_pipeline/traces/multi_pipelines_noflush.trace b/src/test/modules/libpq_pipeline/traces/multi_pipelines_noflush.trace new file mode 100644 index 0000000000..4b9ab07ca4 --- /dev/null +++ b/src/test/modules/libpq_pipeline/traces/multi_pipelines_noflush.trace @@ -0,0 +1,23 @@ +F 21 Parse "" "SELECT $1" 1 NNNN +F 19 Bind "" "" 0 1 1 '1' 1 0 +F 6 Describe P "" +F 9 Execute "" 0 +F 4 Sync +F 21 Parse "" "SELECT $1" 1 NNNN +F 19 Bind "" "" 0 1 1 '1' 1 0 +F 6 Describe P "" +F 9 Execute "" 0 +F 4 Sync +B 4 ParseComplete +B 4 BindComplete +B 33 RowDescription 1 "?column?" NNNN 0 NNNN 4 -1 0 +B 11 DataRow 1 1 '1' +B 13 CommandComplete "SELECT 1" +B 5 ReadyForQuery I +B 4 ParseComplete +B 4 BindComplete +B 33 RowDescription 1 "?column?" NNNN 0 NNNN 4 -1 0 +B 11 DataRow 1 1 '1' +B 13 CommandComplete "SELECT 1" +B 5 ReadyForQuery I +F 4 Terminate -- 2.34.1
^ permalink raw reply [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
@ 2023-04-25 14:23 ` Denis Laxalde <denis.laxalde@dalibo.com>
  2023-04-26 22:56   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  1 sibling, 1 reply; 37+ messages in thread

From: Denis Laxalde @ 2023-04-25 14:23 UTC (permalink / raw)
  To: Anton Kirilov <antonvkirilov@gmail.com>; pgsql-hackers

Anton Kirilov wrote:
> I would appeciate your thoughts on my proposal.

This sounds like a useful addition to me. I've played a bit with it in 
Psycopg and it works fine.


inline patch 1: src/interfaces/libpq/fe-exec.c (+1/-0 lines) download inline-1.patchdiff --git a/src/interfaces/libpq/fe-exec.c b/src/interfaces/libpq/fe-exec.c index a16bbf32ef..e2b32c1379 100644 --- a/src/interfaces/libpq/fe-exec.c +++ b/src/interfaces/libpq/fe-exec.c @@ -82,6 +82,7 @@ static int PQsendDescribe(PGconn *conn, char desc_type, static int check_field_number(const PGresult *res, int field_num); static void pqPipelineProcessQueue(PGconn *conn); static int pqPipelineFlush(PGconn *conn); +static int send_sync_message(PGconn *conn, int flush);
Could (should?) be: static int send_sync_message(PGconn *conn, bool flush); diff --git a/src/test/modules/libpq_pipeline/libpq_pipeline.c b/src/test/modules/libpq_pipeline/libpq_pipeline.c index f48da7d963..829907957a 100644
inline patch 2: src/test/modules/libpq_pipeline/libpq_pipeline.c (+3/-0 lines) download inline-2.patch--- a/src/test/modules/libpq_pipeline/libpq_pipeline.c +++ b/src/test/modules/libpq_pipeline/libpq_pipeline.c @@ -244,6 +244,104 @@ test_multi_pipelines(PGconn *conn) fprintf(stderr, "ok\n"); } +static void +test_multi_pipelines_noflush(PGconn *conn) +{
Maybe test_multi_pipelines() could be extended with an additional PQsendQueryParams()+PQsendSyncMessage() step instead of adding this extra test case? Patches: download all (2 files, .tar.gz) ^ permalink raw reply [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-25 14:23 ` Re: Add PQsendSyncMessage() to libpq Denis Laxalde <denis.laxalde@dalibo.com>
@ 2023-04-26 22:56   ` Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-27 11:06     ` Re: Add PQsendSyncMessage() to libpq Denis Laxalde <denis.laxalde@dalibo.com>
  0 siblings, 1 reply; 37+ messages in thread

From: Anton Kirilov @ 2023-04-26 22:56 UTC (permalink / raw)
  To: Denis Laxalde <denis.laxalde@dalibo.com>; +Cc: pgsql-hackers

Hello,

On 25/04/2023 15:23, Denis Laxalde wrote:
> This sounds like a useful addition to me. I've played a bit with it in
> Psycopg and it works fine.

Thank you very much for reviewing my patch! I have attached a new
version of it that addresses your comments and that has been rebased on
top of the current tip of the master branch (by fixing a merge
conflict), i.e. commit 7b7fa85130330128b404eddebd4f33c6739454b0.

For the sake of others who might read this e-mail thread, I would like
to mention that my patch is complete (including documentation and tests,
but modulo review comments, of course), and that it passes the tests, i.e.:

make check
make -C src/test/modules/libpq_pipeline check

Best wishes,
Anton Kirilov

Attachments:

v2-0001-Add-PQsendSyncMessage-to-libpq.patch (application/x-patch, 10.3K) download From 7aef7b2cf1ffea0786ab1fb4eca9d85ce7242cf0 Mon Sep 17 00:00:00 2001 From: Anton Kirilov <antonvkirilov@gmail.com> Date: Wed, 22 Mar 2023 20:39:57 +0000 Subject: [PATCH v2] Add PQsendSyncMessage() to libpq This new function is equivalent to PQpipelineSync(), except that it does not flush anything to the server; the user must subsequently call PQflush() instead. Its purpose is to reduce the system call overhead of pipeline mode. --- doc/src/sgml/libpq.sgml | 45 ++++++++++++++++--- src/interfaces/libpq/exports.txt | 1 + src/interfaces/libpq/fe-exec.c | 24 +++++++++- src/interfaces/libpq/libpq-fe.h | 1 + .../modules/libpq_pipeline/libpq_pipeline.c | 37 +++++++++++++++ .../traces/multi_pipelines.trace | 11 +++++ 6 files changed, 111 insertions(+), 8 deletions(-) diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index cce25d06e6..c2ee982135 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -3490,8 +3490,9 @@ ExecStatusType PQresultStatus(const PGresult *res); <listitem> <para> The <structname>PGresult</structname> represents a - synchronization point in pipeline mode, requested by - <xref linkend="libpq-PQpipelineSync"/>. + synchronization point in pipeline mode, requested by either + <xref linkend="libpq-PQpipelineSync"/> or + <xref linkend="libpq-PQsendSyncMessage"/>. This status occurs only when pipeline mode has been selected. </para> </listitem> @@ -5019,7 +5020,8 @@ int PQsendDescribePortal(PGconn *conn, const char *portalName); <xref linkend="libpq-PQsendPrepare"/>, <xref linkend="libpq-PQsendQueryPrepared"/>, <xref linkend="libpq-PQsendDescribePrepared"/>, - <xref linkend="libpq-PQsendDescribePortal"/>, or + <xref linkend="libpq-PQsendDescribePortal"/>, + <xref linkend="libpq-PQsendSyncMessage"/>, or <xref linkend="libpq-PQpipelineSync"/> call, and returns it. A null pointer is returned when the command is complete and there @@ -5399,8 +5401,9 @@ int PQflush(PGconn *conn); client sends them. The server will begin executing the commands in the pipeline immediately, not waiting for the end of the pipeline. Note that results are buffered on the server side; the server flushes - that buffer when a synchronization point is established with - <function>PQpipelineSync</function>, or when + that buffer when a synchronization point is established with either + <function>PQpipelineSync</function> or + <function>PQsendSyncMessage</function>, or when <function>PQsendFlushRequest</function> is called. If any statement encounters an error, the server aborts the current transaction and does not execute any subsequent command in the queue @@ -5457,7 +5460,8 @@ int PQflush(PGconn *conn); <type>PGresult</type> types <literal>PGRES_PIPELINE_SYNC</literal> and <literal>PGRES_PIPELINE_ABORTED</literal>. <literal>PGRES_PIPELINE_SYNC</literal> is reported exactly once for each - <function>PQpipelineSync</function> at the corresponding point + <function>PQpipelineSync</function> or + <function>PQsendSyncMessage</function> at the corresponding point in the pipeline. <literal>PGRES_PIPELINE_ABORTED</literal> is emitted in place of a normal query result for the first error and all subsequent results @@ -5495,7 +5499,8 @@ int PQflush(PGconn *conn); <function>PQresultStatus</function> will report a <literal>PGRES_PIPELINE_ABORTED</literal> result for each remaining queued operation in an aborted pipeline. The result for - <function>PQpipelineSync</function> is reported as + <function>PQpipelineSync</function> or + <function>PQsendSyncMessage</function> is reported as <literal>PGRES_PIPELINE_SYNC</literal> to signal the end of the aborted pipeline and resumption of normal result processing. </para> @@ -5727,6 +5732,32 @@ int PQsendFlushRequest(PGconn *conn); </para> </listitem> </varlistentry> + + <varlistentry id="libpq-PQsendSyncMessage"> + <term><function>PQsendSyncMessage</function><indexterm><primary>PQsendSyncMessage</primary></indexterm></term> + + <listitem> + <para> + Marks a synchronization point in a pipeline by sending a + <link linkend="protocol-flow-ext-query">sync message</link> + without flushing the send buffer. This serves as + the delimiter of an implicit transaction and an error recovery + point; see <xref linkend="libpq-pipeline-errors"/>. + +<synopsis> +int PQsendSyncMessage(PGconn *conn); +</synopsis> + </para> + <para> + Returns 1 for success. Returns 0 if the connection is not in + pipeline mode or sending a + <link linkend="protocol-flow-ext-query">sync message</link> + failed. + Note that the message is not itself flushed to the server automatically; + use <function>PQflush</function> if necessary. + </para> + </listitem> + </varlistentry> </variablelist> </sect2> diff --git a/src/interfaces/libpq/exports.txt b/src/interfaces/libpq/exports.txt index 7ded77aff3..3c364d692a 100644 --- a/src/interfaces/libpq/exports.txt +++ b/src/interfaces/libpq/exports.txt @@ -187,3 +187,4 @@ PQsetTraceFlags 184 PQmblenBounded 185 PQsendFlushRequest 186 PQconnectionUsedGSSAPI 187 +PQsendSyncMessage 188 diff --git a/src/interfaces/libpq/fe-exec.c b/src/interfaces/libpq/fe-exec.c index a16bbf32ef..83113d6e6e 100644 --- a/src/interfaces/libpq/fe-exec.c +++ b/src/interfaces/libpq/fe-exec.c @@ -82,6 +82,7 @@ static int PQsendDescribe(PGconn *conn, char desc_type, static int check_field_number(const PGresult *res, int field_num); static void pqPipelineProcessQueue(PGconn *conn); static int pqPipelineFlush(PGconn *conn); +static int send_sync_message(PGconn *conn, bool flush); /* ---------------- @@ -3143,6 +3144,17 @@ pqPipelineProcessQueue(PGconn *conn) */ int PQpipelineSync(PGconn *conn) +{ + return send_sync_message(conn, true); +} + +/* + * send_sync_message: subroutine for PQpipelineSync and PQsendSyncMessage + * Send a Sync message as part of a pipeline, + * and optionally flush to server + */ +static int +send_sync_message(PGconn *conn, bool flush) { PGcmdQueueEntry *entry; @@ -3189,7 +3201,7 @@ PQpipelineSync(PGconn *conn) * Give the data a push. In nonblock mode, don't complain if we're unable * to send it all; PQgetResult() will do any additional flushing needed. */ - if (PQflush(conn) < 0) + if (flush && PQflush(conn) < 0) goto sendFailed; /* OK, it's launched! */ @@ -3238,6 +3250,16 @@ PQsendFlushRequest(PGconn *conn) return 1; } +/* + * PQsendSyncMessage + * Send a Sync message as part of a pipeline without flushing to server + */ +int +PQsendSyncMessage(PGconn *conn) +{ + return send_sync_message(conn, false); +} + /* ====== accessor funcs for PGresult ======== */ ExecStatusType diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h index 7476dbe0e9..86efb2e764 100644 --- a/src/interfaces/libpq/libpq-fe.h +++ b/src/interfaces/libpq/libpq-fe.h @@ -474,6 +474,7 @@ extern int PQenterPipelineMode(PGconn *conn); extern int PQexitPipelineMode(PGconn *conn); extern int PQpipelineSync(PGconn *conn); extern int PQsendFlushRequest(PGconn *conn); +extern int PQsendSyncMessage(PGconn *conn); /* LISTEN/NOTIFY support */ extern PGnotify *PQnotifies(PGconn *conn); diff --git a/src/test/modules/libpq_pipeline/libpq_pipeline.c b/src/test/modules/libpq_pipeline/libpq_pipeline.c index f48da7d963..72d3d33a22 100644 --- a/src/test/modules/libpq_pipeline/libpq_pipeline.c +++ b/src/test/modules/libpq_pipeline/libpq_pipeline.c @@ -169,6 +169,14 @@ test_multi_pipelines(PGconn *conn) if (PQpipelineSync(conn) != 1) pg_fatal("Pipeline sync failed: %s", PQerrorMessage(conn)); + if (PQsendQueryParams(conn, "SELECT $1", 1, dummy_param_oids, + dummy_params, NULL, NULL, 0) != 1) + pg_fatal("dispatching first SELECT failed: %s", PQerrorMessage(conn)); + + /* Skip flushing once. */ + if (PQsendSyncMessage(conn) != 1) + pg_fatal("Pipeline sync failed: %s", PQerrorMessage(conn)); + if (PQsendQueryParams(conn, "SELECT $1", 1, dummy_param_oids, dummy_params, NULL, NULL, 0) != 1) pg_fatal("dispatching second SELECT failed: %s", PQerrorMessage(conn)); @@ -206,6 +214,35 @@ test_multi_pipelines(PGconn *conn) /* second pipeline */ + res = PQgetResult(conn); + if (res == NULL) + pg_fatal("PQgetResult returned null when there's a pipeline item: %s", + PQerrorMessage(conn)); + + if (PQresultStatus(res) != PGRES_TUPLES_OK) + pg_fatal("Unexpected result code %s from first pipeline item", + PQresStatus(PQresultStatus(res))); + PQclear(res); + res = NULL; + + if (PQgetResult(conn) != NULL) + pg_fatal("PQgetResult returned something extra after first result"); + + if (PQexitPipelineMode(conn) != 0) + pg_fatal("exiting pipeline mode after query but before sync succeeded incorrectly"); + + res = PQgetResult(conn); + if (res == NULL) + pg_fatal("PQgetResult returned null when sync result expected: %s", + PQerrorMessage(conn)); + + if (PQresultStatus(res) != PGRES_PIPELINE_SYNC) + pg_fatal("Unexpected result code %s instead of sync result, error: %s", + PQresStatus(PQresultStatus(res)), PQerrorMessage(conn)); + PQclear(res); + + /* third pipeline */ + res = PQgetResult(conn); if (res == NULL) pg_fatal("PQgetResult returned null when there's a pipeline item: %s", diff --git a/src/test/modules/libpq_pipeline/traces/multi_pipelines.trace b/src/test/modules/libpq_pipeline/traces/multi_pipelines.trace index 4b9ab07ca4..1ee21f61dc 100644 --- a/src/test/modules/libpq_pipeline/traces/multi_pipelines.trace +++ b/src/test/modules/libpq_pipeline/traces/multi_pipelines.trace @@ -8,6 +8,17 @@ F 19 Bind "" "" 0 1 1 '1' 1 0 F 6 Describe P "" F 9 Execute "" 0 F 4 Sync +F 21 Parse "" "SELECT $1" 1 NNNN +F 19 Bind "" "" 0 1 1 '1' 1 0 +F 6 Describe P "" +F 9 Execute "" 0 +F 4 Sync +B 4 ParseComplete +B 4 BindComplete +B 33 RowDescription 1 "?column?" NNNN 0 NNNN 4 -1 0 +B 11 DataRow 1 1 '1' +B 13 CommandComplete "SELECT 1" +B 5 ReadyForQuery I B 4 ParseComplete B 4 BindComplete B 33 RowDescription 1 "?column?" NNNN 0 NNNN 4 -1 0 -- 2.34.1
^ permalink raw reply [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-25 14:23 ` Re: Add PQsendSyncMessage() to libpq Denis Laxalde <denis.laxalde@dalibo.com>
  2023-04-26 22:56   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
@ 2023-04-27 11:06     ` Denis Laxalde <denis.laxalde@dalibo.com>
  2023-04-28 07:10       ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 37+ messages in thread

From: Denis Laxalde @ 2023-04-27 11:06 UTC (permalink / raw)
  To: Anton Kirilov <antonvkirilov@gmail.com>; +Cc: pgsql-hackers

Hello,

Anton Kirilov a écrit :
> On 25/04/2023 15:23, Denis Laxalde wrote:
>> This sounds like a useful addition to me. I've played a bit with it in
>> Psycopg and it works fine.
> 
> Thank you very much for reviewing my patch! I have attached a new
> version of it that addresses your comments and that has been rebased on
> top of the current tip of the master branch (by fixing a merge
> conflict), i.e. commit 7b7fa85130330128b404eddebd4f33c6739454b0.
> 
> For the sake of others who might read this e-mail thread, I would like
> to mention that my patch is complete (including documentation and tests,
> but modulo review comments, of course), and that it passes the tests, i.e.:
> 
> make check
> make -C src/test/modules/libpq_pipeline check

Thank you; this V2 looks good to me.
Marking as ready for committer.






^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-25 14:23 ` Re: Add PQsendSyncMessage() to libpq Denis Laxalde <denis.laxalde@dalibo.com>
  2023-04-26 22:56   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-27 11:06     ` Re: Add PQsendSyncMessage() to libpq Denis Laxalde <denis.laxalde@dalibo.com>
@ 2023-04-28 07:10       ` Michael Paquier <michael@paquier.xyz>
  2023-04-28 08:08         ` Re: Add PQsendSyncMessage() to libpq Denis Laxalde <denis.laxalde@dalibo.com>
  0 siblings, 1 reply; 37+ messages in thread

From: Michael Paquier @ 2023-04-28 07:10 UTC (permalink / raw)
  To: Denis Laxalde <denis.laxalde@dalibo.com>; +Cc: Anton Kirilov <antonvkirilov@gmail.com>; pgsql-hackers

On Thu, Apr 27, 2023 at 01:06:27PM +0200, Denis Laxalde wrote:
> Thank you; this V2 looks good to me.
> Marking as ready for committer.

Please note that we are in a stabilization period for v16 and that the
first commit fest of v17 should start in July, so it will perhaps take
some time before this is looked at by a committer.

Speaking of which, what was the performance impact of your application
once PQflush() was moved out of the pipeline sync?  Just asking for
curiosity..
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-25 14:23 ` Re: Add PQsendSyncMessage() to libpq Denis Laxalde <denis.laxalde@dalibo.com>
  2023-04-26 22:56   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-27 11:06     ` Re: Add PQsendSyncMessage() to libpq Denis Laxalde <denis.laxalde@dalibo.com>
  2023-04-28 07:10       ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
@ 2023-04-28 08:08         ` Denis Laxalde <denis.laxalde@dalibo.com>
  2023-04-30 00:59           ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  0 siblings, 1 reply; 37+ messages in thread

From: Denis Laxalde @ 2023-04-28 08:08 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Anton Kirilov <antonvkirilov@gmail.com>; pgsql-hackers

Michael Paquier a écrit :
> On Thu, Apr 27, 2023 at 01:06:27PM +0200, Denis Laxalde wrote:
>> Thank you; this V2 looks good to me.
>> Marking as ready for committer.
> 
> Please note that we are in a stabilization period for v16 and that the
> first commit fest of v17 should start in July, so it will perhaps take
> some time before this is looked at by a committer.

Yes, I am aware; totally fine by me.

> Speaking of which, what was the performance impact of your application
> once PQflush() was moved out of the pipeline sync?  Just asking for
> curiosity..

I have no metrics for that; but maybe Anton has some?
(In Psycopg, we generally do not expect users to handle the sync 
operation themselves, it's done under the hood; and I only found one 
situation where the flush could be avoided, but that's largely because 
our design, there can be more in general I think.)






^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-25 14:23 ` Re: Add PQsendSyncMessage() to libpq Denis Laxalde <denis.laxalde@dalibo.com>
  2023-04-26 22:56   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-27 11:06     ` Re: Add PQsendSyncMessage() to libpq Denis Laxalde <denis.laxalde@dalibo.com>
  2023-04-28 07:10       ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-04-28 08:08         ` Re: Add PQsendSyncMessage() to libpq Denis Laxalde <denis.laxalde@dalibo.com>
@ 2023-04-30 00:59           ` Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-01 23:55             ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 37+ messages in thread

From: Anton Kirilov @ 2023-04-30 00:59 UTC (permalink / raw)
  To: Denis Laxalde <denis.laxalde@dalibo.com>; Michael Paquier <michael@paquier.xyz>; +Cc: pgsql-hackers

Hello,

On 28/04/2023 09:08, Denis Laxalde wrote:
> Michael Paquier a écrit :
>> Speaking of which, what was the performance impact of your application
>> once PQflush() was moved out of the pipeline sync?  Just asking for
>> curiosity..
> 
> I have no metrics for that; but maybe Anton has some?
I did a quick check using the TechEmpower Framework Benchmarks ( 
https://www.techempower.com/benchmarks/ ) - they define 4 Web 
application tests that are database-bound. Everything was running on a 
single machine, and 3 of the tests had an improvement of 29.16%, 32.30%, 
and 41.78% respectively in the number of requests per second (Web 
application requests, not database queries), while the last test 
regressed by 0.66% (which I would say is practically no difference, 
given that there is always some measurement noise). I will try to get 
the changes from my patch tested in the project's continuous 
benchmarking environment, which has a proper set up with 3 servers 
(client, application server, and database) connected by a 10GbE link.

Best wishes,
Anton Kirilov





^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-25 14:23 ` Re: Add PQsendSyncMessage() to libpq Denis Laxalde <denis.laxalde@dalibo.com>
  2023-04-26 22:56   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-27 11:06     ` Re: Add PQsendSyncMessage() to libpq Denis Laxalde <denis.laxalde@dalibo.com>
  2023-04-28 07:10       ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-04-28 08:08         ` Re: Add PQsendSyncMessage() to libpq Denis Laxalde <denis.laxalde@dalibo.com>
  2023-04-30 00:59           ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
@ 2023-05-01 23:55             ` Michael Paquier <michael@paquier.xyz>
  2023-05-22 00:18               ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  0 siblings, 1 reply; 37+ messages in thread

From: Michael Paquier @ 2023-05-01 23:55 UTC (permalink / raw)
  To: Anton Kirilov <antonvkirilov@gmail.com>; +Cc: Denis Laxalde <denis.laxalde@dalibo.com>; pgsql-hackers

On Sun, Apr 30, 2023 at 01:59:17AM +0100, Anton Kirilov wrote:
> I did a quick check using the TechEmpower Framework Benchmarks (
> https://www.techempower.com/benchmarks/ ) - they define 4 Web application
> tests that are database-bound. Everything was running on a single machine,
> and 3 of the tests had an improvement of 29.16%, 32.30%, and 41.78%
> respectively in the number of requests per second (Web application requests,
> not database queries), while the last test regressed by 0.66% (which I would
> say is practically no difference, given that there is always some
> measurement noise). I will try to get the changes from my patch tested in
> the project's continuous benchmarking environment, which has a proper set up
> with 3 servers (client, application server, and database) connected by a
> 10GbE link.

Well, these are nice numbers.  At ~1% I am ready to buy the noise
argument, but what would the range of the usual noise when it comes to
multiple runs under the same conditions?

Let's make sure that the API interface is the most intuitive (Robert
has commented about that a few days ago, still need to follow up on
that).
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-25 14:23 ` Re: Add PQsendSyncMessage() to libpq Denis Laxalde <denis.laxalde@dalibo.com>
  2023-04-26 22:56   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-27 11:06     ` Re: Add PQsendSyncMessage() to libpq Denis Laxalde <denis.laxalde@dalibo.com>
  2023-04-28 07:10       ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-04-28 08:08         ` Re: Add PQsendSyncMessage() to libpq Denis Laxalde <denis.laxalde@dalibo.com>
  2023-04-30 00:59           ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-01 23:55             ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
@ 2023-05-22 00:18               ` Anton Kirilov <antonvkirilov@gmail.com>
  0 siblings, 0 replies; 37+ messages in thread

From: Anton Kirilov @ 2023-05-22 00:18 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: pgsql-hackers

Hello,

On 02/05/2023 00:55, Michael Paquier wrote:
 > Well, these are nice numbers.  At ~1% I am ready to buy the noise
 > argument, but what would the range of the usual noise when it comes to
 > multiple runs under the same conditions>

I managed to get my patch tested in the TechEmpower Framework Benchmarks 
continuous benchmarking environment, and even though it takes roughly a 
week to get a new set of results, now there had been a couple of runs 
both with and without my changes. All 4 database-bound Web application 
tests (single query, multiple queries, fortunes, and data updates) saw 
improvements, by approximately 8.94%, 0.64%, 9.54%, and 2.78% 
respectively. The standard errors were 0.65% or less, so there was 
practically no change in the second test. However, I have seen another 
implementation experience a much larger improvement (~6.69%) in that 
test from essentially the same optimization, so I think that my own code 
has another bottleneck. Note that these test runs were not in the same 
benchmarking environment as the one I used previously for a quick check, 
so the values differ. Also, another set of results should become 
available in a week or so (and would be based on my optimization).

Links to the test runs:
https://www.techempower.com/benchmarks/#section=test&runid=1ecf679a-9686-4de7-a3b7-de16a1a84bb6&...
https://www.techempower.com/benchmarks/#section=test&runid=aab00736-445c-4b7f-83b5-451c47c83395&...
https://www.techempower.com/benchmarks/#section=test&runid=bc7f7570-a88e-48e3-9874-06d7dc0a0f74&...
https://www.techempower.com/benchmarks/#section=test&runid=e6dd1abd-7aa2-4846-9b44-d8fd8a23d385&...
(ordered chronologically; the first 2 did not include my optimization)

Best wishes,
Anton Kirilov





^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
@ 2023-04-28 12:06 ` Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  1 sibling, 1 reply; 37+ messages in thread

From: Robert Haas @ 2023-04-28 12:06 UTC (permalink / raw)
  To: Anton Kirilov <antonvkirilov@gmail.com>; +Cc: pgsql-hackers

On Fri, Mar 24, 2023 at 6:39 PM Anton Kirilov <antonvkirilov@gmail.com> wrote:
> I have attached a patch that introduces PQsendSyncMessage(), a
> function that is equivalent to PQpipelineSync(), except that it does
> not flush anything to the server; the user must subsequently call
> PQflush() instead. Alternatively, the new function is equivalent to
> PQsendFlushRequest(), except that it sends a sync message instead of a
> flush request. In addition to reducing the system call overhead of
> libpq's pipeline mode, it also makes it easier for the operating
> system to send as much of the pipeline as possible in a single TCP (or
> lower level protocol) packet when the database is running remotely.

I wonder whether this is the naming that we want. The two names are
significantly different. Something like PQpipelineSendSync() would be
more similar.

I also wonder, really even more, whether it would be better to do
something like PQpipelinePutSync(PGconn *conn, bool flush) with
PQpipelineSync(conn) just meaning PQpipelinePutSync(conn, true). We're
basically using the function name as a Boolean parameter to select the
behavior, which is fine if you only have one parameter and it's a
Boolean, but it's obviously unworkable if you have say 3 Boolean
parameters because you don't want 8 different functions, and what if
you need an integer parameter for some reason?

So I'd favor exposing a function that is effectively an extended
version of PQpipelineSendSync() with an additional Boolean parameter,
and that way if for some reason somebody needs to extend it again,
they can just make an even more extended version with yet another
parameter. That way, all the functionality is always available by
calling the newest function, and older ones are still there for older
applications.

-- 
Robert Haas
EDB: http://www.enterprisedb.com





^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
@ 2023-04-29 16:06   ` Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 37+ messages in thread

From: Anton Kirilov @ 2023-04-29 16:06 UTC (permalink / raw)
  To: Robert Haas <robertmhaas@gmail.com>; +Cc: pgsql-hackers

Hello,

On 28/04/2023 13:06, Robert Haas wrote:
> On Fri, Mar 24, 2023 at 6:39 PM Anton Kirilov <antonvkirilov@gmail.com> wrote:
>> I have attached a patch that introduces PQsendSyncMessage()...
> 
> I wonder whether this is the naming that we want. The two names are
> significantly different. Something like PQpipelineSendSync() would be
> more similar.

The reason is that the function is modeled after PQsendFlushRequest(), 
since it felt closer to what I was trying to achieve, i.e. appending a 
protocol message to the output buffer without doing any actual I/O 
operations.

> I also wonder, really even more, whether it would be better to do
> something like PQpipelinePutSync(PGconn *conn, bool flush) with
> PQpipelineSync(conn) just meaning PQpipelinePutSync(conn, true).

Actually I believe that there is another issue with PQpipelineSync() 
that has to do with ergonomics - according to a comment inside its body 
( 
https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/interfaces/libpq/fe-exec.c;h=a16bbf... 
) it could fail silently to send all the buffered data, which seems to 
be problematic when operating in non-blocking mode. In practice, this 
means that all calls to PQpipelineSync() must be followed by execution 
of PQflush() to check whether the application should poll for write 
readiness. I suppose that that was the reason why I was going for a 
solution that did not combine changing the connection state with doing 
I/O operations.

In any case I am not particularly attached to any naming or the exact 
shape of the new API, as long as it achieves the same goal (reducing the 
number of system calls), but before I make any substantial changes to my 
patch, I would like to hear your thoughts on the matter.

Best wishes,
Anton Kirilov





^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
@ 2023-05-02 00:42     ` Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  0 siblings, 1 reply; 37+ messages in thread

From: Michael Paquier @ 2023-05-02 00:42 UTC (permalink / raw)
  To: Anton Kirilov <antonvkirilov@gmail.com>; +Cc: Robert Haas <robertmhaas@gmail.com>; pgsql-hackers

On Sat, Apr 29, 2023 at 05:06:03PM +0100, Anton Kirilov wrote:
> In any case I am not particularly attached to any naming or the exact shape
> of the new API, as long as it achieves the same goal (reducing the number of
> system calls), but before I make any substantial changes to my patch, I
> would like to hear your thoughts on the matter.

Another thing that may matter in terms of extensibility?  Would a
boolean argument really be the best design?  Could it be better to
have instead one API with a bits32 and some flags controlling its
internals?
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
@ 2023-05-02 14:02       ` Robert Haas <robertmhaas@gmail.com>
  2023-05-03 10:03         ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  0 siblings, 1 reply; 37+ messages in thread

From: Robert Haas @ 2023-05-02 14:02 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Anton Kirilov <antonvkirilov@gmail.com>; pgsql-hackers

On Mon, May 1, 2023 at 8:42 PM Michael Paquier <michael@paquier.xyz> wrote:
> Another thing that may matter in terms of extensibility?  Would a
> boolean argument really be the best design?  Could it be better to
> have instead one API with a bits32 and some flags controlling its
> internals?

I wondered that, too. If we never add any more Boolean parameters to
this function then that would end up a waste, but maybe we will and
then it will be genius. Not sure what's best.

-- 
Robert Haas
EDB: http://www.enterprisedb.com





^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
@ 2023-05-03 10:03         ` Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-05-04 09:21           ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-08 03:14           ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-11-07 09:23           ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  0 siblings, 3 replies; 37+ messages in thread

From: Alvaro Herrera @ 2023-05-03 10:03 UTC (permalink / raw)
  To: Robert Haas <robertmhaas@gmail.com>; +Cc: Michael Paquier <michael@paquier.xyz>; Anton Kirilov <antonvkirilov@gmail.com>; pgsql-hackers

On 2023-May-02, Robert Haas wrote:

> On Mon, May 1, 2023 at 8:42 PM Michael Paquier <michael@paquier.xyz> wrote:
> > Another thing that may matter in terms of extensibility?  Would a
> > boolean argument really be the best design?  Could it be better to
> > have instead one API with a bits32 and some flags controlling its
> > internals?
> 
> I wondered that, too. If we never add any more Boolean parameters to
> this function then that would end up a waste, but maybe we will and
> then it will be genius. Not sure what's best.

I agree that adding a flag is the way to go, since it improve chances
that we won't end up with ten different functions in case we decide to
have eight other behaviors.  One more function and we're done.  And
while I can't think of any use for a future flag, we (I) already didn't
of this one either, so let's not make the same mistake.

We already have 'int' flag masks in PQcopyResult() and
PQsetTraceFlags().  We were using bits32 initially for flag stuff in the
PQtrace facilities, until [1] reminded us that we shouldn't let c.h
creep into app-land, so that was turned into plain 'int'.

[1] https://www.postgresql.org/message-id/TYAPR01MB2990B6C6A32ACF15D97AE94AFEBD0%40TYAPR01MB2990.jpnprd0...

-- 
Álvaro Herrera        Breisgau, Deutschland  —  https://www.EnterpriseDB.com/
"No nos atrevemos a muchas cosas porque son difíciles,
pero son difíciles porque no nos atrevemos a hacerlas" (Séneca)





^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-05-03 10:03         ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
@ 2023-05-04 09:21           ` Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-04 10:36             ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2 siblings, 1 reply; 37+ messages in thread

From: Anton Kirilov @ 2023-05-04 09:21 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@alvh.no-ip.org>; Michael Paquier <michael@paquier.xyz>; Robert Haas <robertmhaas@gmail.com>; +Cc: pgsql-hackers

Hello,

> On 3 May 2023, at 11:03, Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:
> 
> On 2023-May-02, Robert Haas wrote:
> 
>> On Mon, May 1, 2023 at 8:42 PM Michael Paquier <michael@paquier.xyz> wrote:
>>> Another thing that may matter in terms of extensibility?  Would a
>>> boolean argument really be the best design?  Could it be better to
>>> have instead one API with a bits32 and some flags controlling its
>>> internals?
>> 
>> I wondered that, too. If we never add any more Boolean parameters to
>> this function then that would end up a waste, but maybe we will and
>> then it will be genius. Not sure what's best.
> 
> I agree that adding a flag is the way to go, since it improve chances
> that we won't end up with ten different functions in case we decide to
> have eight other behaviors.  One more function and we're done.  And
> while I can't think of any use for a future flag, we (I) already didn't
> of this one either, so let's not make the same mistake.

Thank you all for the feedback! Do you have any thoughts on the other issue with PQpipelineSync() I have mentioned in my previous message? Am I just misunderstanding what the code comment means and how the API is supposed to be used by any chance?

Best wishes,
Anton Kirilov





^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-05-03 10:03         ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-05-04 09:21           ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
@ 2023-05-04 10:36             ` Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-05-05 15:02               ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  0 siblings, 1 reply; 37+ messages in thread

From: Alvaro Herrera @ 2023-05-04 10:36 UTC (permalink / raw)
  To: Anton Kirilov <antonvkirilov@gmail.com>; +Cc: Michael Paquier <michael@paquier.xyz>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers

On 2023-May-04, Anton Kirilov wrote:

> Thank you all for the feedback! Do you have any thoughts on the other
> issue with PQpipelineSync() I have mentioned in my previous message?

Eh, I hadn't seen that one.

> Am I just misunderstanding what the code comment means and how the API
> is supposed to be used by any chance?

I think you have it right: it is possible that the buffer has not been
fully flushed by the time PQpipelineSync returns.

If you want to make sure it's fully flushed, your only option is to have
the call block.  That would make it no longer non-blocking, so it has to
be explicitly requested behavior.
I think this means to add yet another behavior flag for the new
function: have it block, waiting for the buffer to be flushed.

So your application can put several sync points in the queue, with no
flushing (and of course no blocking), and have it flush+block only on
the "last" one.  Of course, for other users, you want the current
behavior: have it flush opportunistically but not block.  So you can't
make it a single flag.

-- 
Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/





^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-05-03 10:03         ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-05-04 09:21           ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-04 10:36             ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
@ 2023-05-05 15:02               ` Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-21 17:17                 ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  0 siblings, 1 reply; 37+ messages in thread

From: Anton Kirilov @ 2023-05-05 15:02 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@alvh.no-ip.org>; +Cc: Michael Paquier <michael@paquier.xyz>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers

Hello,

On Thu, 4 May 2023, 11:36 Alvaro Herrera, <alvherre@alvh.no-ip.org <mailto:alvherre@alvh.no-ip.org>> wrote:
> On 2023-May-04, Anton Kirilov wrote:
> If you want to make sure it's fully flushed, your only option is to have
> the call block.


Surely PQflush() returning 0 would signify that the output buffer has been fully flushed? Which means that there is another, IMHO simpler option than introducing an extra flag - make the new function return the same values as PQflush(), i.e. 0 for no error and fully flushed output, -1 for error, and 1 for partial flush (so that the user may start polling for write readiness). Of course, the function would never return 1 (but would block instead) unless the user has called PQsetnonblocking() beforehand.

Best wishes,
Anton Kirilov

^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-05-03 10:03         ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-05-04 09:21           ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-04 10:36             ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-05-05 15:02               ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
@ 2023-05-21 17:17                 ` Anton Kirilov <antonvkirilov@gmail.com>
  2023-07-05 20:45                   ` Re: Add PQsendSyncMessage() to libpq Daniel Gustafsson <daniel@yesql.se>
  0 siblings, 1 reply; 37+ messages in thread

From: Anton Kirilov @ 2023-05-21 17:17 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@alvh.no-ip.org>; +Cc: Michael Paquier <michael@paquier.xyz>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers

Hello,

On 05/05/2023 16:02, Anton Kirilov wrote:
> On Thu, 4 May 2023, 11:36 Alvaro Herrera, <alvherre@alvh.no-ip.org 
> <mailto:alvherre@alvh.no-ip.org>> wrote:
> 
>     On 2023-May-04, Anton Kirilov wrote:
>     If you want to make sure it's fully flushed, your only option is to have
>     the call block.
> 
> 
> Surely PQflush() returning 0 would signify that the output buffer has 
> been fully flushed?
Since I haven't got any further comments, I assume that I am correct, so 
here is an updated version of the patch that should address all feedback 
that I have received so far and all issues that I have identified.

Thanks,
Anton Kirilov

Attachments:

v3-0001-Add-PQpipelinePutSync-to-libpq.patch (text/x-patch, 11.9K) download From 8ba588e0356fd5310d34c5301d99676af8c34fdf Mon Sep 17 00:00:00 2001 From: Anton Kirilov <antonvkirilov@gmail.com> Date: Wed, 22 Mar 2023 20:39:57 +0000 Subject: [PATCH v3] Add PQpipelinePutSync() to libpq This new function is equivalent to PQpipelineSync(), except that it lets the caller choose whether anything is flushed to the server. Its purpose is to reduce the system call overhead of pipeline mode. --- doc/src/sgml/libpq.sgml | 52 ++++++++++++++++--- src/interfaces/libpq/exports.txt | 1 + src/interfaces/libpq/fe-exec.c | 29 ++++++++--- src/interfaces/libpq/libpq-fe.h | 6 +++ .../modules/libpq_pipeline/libpq_pipeline.c | 37 +++++++++++++ .../traces/multi_pipelines.trace | 11 ++++ 6 files changed, 120 insertions(+), 16 deletions(-) diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index cce25d06e6..5aac3c9172 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -3490,8 +3490,9 @@ ExecStatusType PQresultStatus(const PGresult *res); <listitem> <para> The <structname>PGresult</structname> represents a - synchronization point in pipeline mode, requested by - <xref linkend="libpq-PQpipelineSync"/>. + synchronization point in pipeline mode, requested by either + <xref linkend="libpq-PQpipelineSync"/> or + <xref linkend="libpq-PQpipelinePutSync"/>. This status occurs only when pipeline mode has been selected. </para> </listitem> @@ -5019,7 +5020,8 @@ int PQsendDescribePortal(PGconn *conn, const char *portalName); <xref linkend="libpq-PQsendPrepare"/>, <xref linkend="libpq-PQsendQueryPrepared"/>, <xref linkend="libpq-PQsendDescribePrepared"/>, - <xref linkend="libpq-PQsendDescribePortal"/>, or + <xref linkend="libpq-PQsendDescribePortal"/>, + <xref linkend="libpq-PQpipelinePutSync"/>, or <xref linkend="libpq-PQpipelineSync"/> call, and returns it. A null pointer is returned when the command is complete and there @@ -5385,7 +5387,8 @@ int PQflush(PGconn *conn); or its prepared-query sibling <xref linkend="libpq-PQsendQueryPrepared"/>. These requests are queued on the client-side until flushed to the server; - this occurs when <xref linkend="libpq-PQpipelineSync"/> is used to + this occurs when <xref linkend="libpq-PQpipelineSync"/> (or optionally + <xref linkend="libpq-PQpipelinePutSync"/>) is used to establish a synchronization point in the pipeline, or when <xref linkend="libpq-PQflush"/> is called. The functions <xref linkend="libpq-PQsendPrepare"/>, @@ -5399,8 +5402,9 @@ int PQflush(PGconn *conn); client sends them. The server will begin executing the commands in the pipeline immediately, not waiting for the end of the pipeline. Note that results are buffered on the server side; the server flushes - that buffer when a synchronization point is established with - <function>PQpipelineSync</function>, or when + that buffer when a synchronization point is established with either + <function>PQpipelineSync</function> or + <function>PQpipelinePutSync</function>, or when <function>PQsendFlushRequest</function> is called. If any statement encounters an error, the server aborts the current transaction and does not execute any subsequent command in the queue @@ -5457,7 +5461,8 @@ int PQflush(PGconn *conn); <type>PGresult</type> types <literal>PGRES_PIPELINE_SYNC</literal> and <literal>PGRES_PIPELINE_ABORTED</literal>. <literal>PGRES_PIPELINE_SYNC</literal> is reported exactly once for each - <function>PQpipelineSync</function> at the corresponding point + <function>PQpipelineSync</function> or + <function>PQpipelinePutSync</function> at the corresponding point in the pipeline. <literal>PGRES_PIPELINE_ABORTED</literal> is emitted in place of a normal query result for the first error and all subsequent results @@ -5495,7 +5500,8 @@ int PQflush(PGconn *conn); <function>PQresultStatus</function> will report a <literal>PGRES_PIPELINE_ABORTED</literal> result for each remaining queued operation in an aborted pipeline. The result for - <function>PQpipelineSync</function> is reported as + <function>PQpipelineSync</function> or + <function>PQpipelinePutSync</function> is reported as <literal>PGRES_PIPELINE_SYNC</literal> to signal the end of the aborted pipeline and resumption of normal result processing. </para> @@ -5727,6 +5733,36 @@ int PQsendFlushRequest(PGconn *conn); </para> </listitem> </varlistentry> + + <varlistentry id="libpq-PQpipelinePutSync"> + <term><function>PQpipelinePutSync</function><indexterm><primary>PQpipelinePutSync</primary></indexterm></term> + + <listitem> + <para> + Marks a synchronization point in a pipeline by sending a + <link linkend="protocol-flow-ext-query">sync message</link>. + This serves as the delimiter of an implicit transaction and + an error recovery point; see <xref linkend="libpq-pipeline-errors"/>. + +<synopsis> +int PQpipelinePutSync(PGconn *conn, int flags); +</synopsis> + </para> + <para> + Returns 0 for success. Returns -1 if the connection is not in + pipeline mode or sending a + <link linkend="protocol-flow-ext-query">sync message</link> + failed. Returns 1 if it was unable to send all the data in + the send queue yet (this case can only occur if the connection + is nonblocking and output data is flushed to the server). + The <parameter>flags</parameter> argument is a bitwise OR of + several flags. <literal>PG_PIPELINEPUTSYNC_FLUSH</literal> + specifies that any queued output data is flushed to the + server. Otherwise, nothing is flushed automatically; + use <function>PQflush</function> if necessary. + </para> + </listitem> + </varlistentry> </variablelist> </sect2> diff --git a/src/interfaces/libpq/exports.txt b/src/interfaces/libpq/exports.txt index 7ded77aff3..8893d2cbf8 100644 --- a/src/interfaces/libpq/exports.txt +++ b/src/interfaces/libpq/exports.txt @@ -187,3 +187,4 @@ PQsetTraceFlags 184 PQmblenBounded 185 PQsendFlushRequest 186 PQconnectionUsedGSSAPI 187 +PQpipelinePutSync 188 diff --git a/src/interfaces/libpq/fe-exec.c b/src/interfaces/libpq/fe-exec.c index 14d706efd5..b5ba17ed03 100644 --- a/src/interfaces/libpq/fe-exec.c +++ b/src/interfaces/libpq/fe-exec.c @@ -3143,16 +3143,27 @@ pqPipelineProcessQueue(PGconn *conn) */ int PQpipelineSync(PGconn *conn) +{ + return PQpipelinePutSync(conn, PG_PIPELINEPUTSYNC_FLUSH) >= 0; +} + +/* + * PQpipelinePutSync + * Send a Sync message as part of a pipeline, + * and optionally flush to server + */ +int +PQpipelinePutSync(PGconn *conn, int flags) { PGcmdQueueEntry *entry; - if (!conn) - return 0; + if (!conn || flags & ~PG_PIPELINEPUTSYNC_FLUSH) + return -1; if (conn->pipelineStatus == PQ_PIPELINE_OFF) { libpq_append_conn_error(conn, "cannot send pipeline when not in pipeline mode"); - return 0; + return -1; } switch (conn->asyncStatus) @@ -3163,7 +3174,7 @@ PQpipelineSync(PGconn *conn) /* should be unreachable */ appendPQExpBufferStr(&conn->errorMessage, "internal error: cannot send pipeline while in COPY\n"); - return 0; + return -1; case PGASYNC_READY: case PGASYNC_READY_MORE: case PGASYNC_BUSY: @@ -3175,7 +3186,7 @@ PQpipelineSync(PGconn *conn) entry = pqAllocCmdQueueEntry(conn); if (entry == NULL) - return 0; /* error msg already set */ + return -1; /* error msg already set */ entry->queryclass = PGQUERY_SYNC; entry->query = NULL; @@ -3189,18 +3200,20 @@ PQpipelineSync(PGconn *conn) * Give the data a push. In nonblock mode, don't complain if we're unable * to send it all; PQgetResult() will do any additional flushing needed. */ - if (PQflush(conn) < 0) + const int ret = flags & PG_PIPELINEPUTSYNC_FLUSH ? PQflush(conn) : 0; + + if (ret < 0) goto sendFailed; /* OK, it's launched! */ pqAppendCmdQueueEntry(conn, entry); - return 1; + return ret; sendFailed: pqRecycleCmdQueueEntry(conn, entry); /* error message should be set up already */ - return 0; + return -1; } /* diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h index 7476dbe0e9..7e5647abf6 100644 --- a/src/interfaces/libpq/libpq-fe.h +++ b/src/interfaces/libpq/libpq-fe.h @@ -47,6 +47,11 @@ extern "C" #define PG_COPYRES_EVENTS 0x04 #define PG_COPYRES_NOTICEHOOKS 0x08 +/* + * Option flags for PQpipelinePutSync + */ +#define PG_PIPELINEPUTSYNC_FLUSH 1 + /* Application-visible enum types */ /* @@ -474,6 +479,7 @@ extern int PQenterPipelineMode(PGconn *conn); extern int PQexitPipelineMode(PGconn *conn); extern int PQpipelineSync(PGconn *conn); extern int PQsendFlushRequest(PGconn *conn); +extern int PQpipelinePutSync(PGconn *conn, int flags); /* LISTEN/NOTIFY support */ extern PGnotify *PQnotifies(PGconn *conn); diff --git a/src/test/modules/libpq_pipeline/libpq_pipeline.c b/src/test/modules/libpq_pipeline/libpq_pipeline.c index f5b4d4d1ff..53e136a9db 100644 --- a/src/test/modules/libpq_pipeline/libpq_pipeline.c +++ b/src/test/modules/libpq_pipeline/libpq_pipeline.c @@ -169,6 +169,14 @@ test_multi_pipelines(PGconn *conn) if (PQpipelineSync(conn) != 1) pg_fatal("Pipeline sync failed: %s", PQerrorMessage(conn)); + if (PQsendQueryParams(conn, "SELECT $1", 1, dummy_param_oids, + dummy_params, NULL, NULL, 0) != 1) + pg_fatal("dispatching first SELECT failed: %s", PQerrorMessage(conn)); + + /* Skip flushing once. */ + if (PQpipelinePutSync(conn, 0) < 0) + pg_fatal("Pipeline sync failed: %s", PQerrorMessage(conn)); + if (PQsendQueryParams(conn, "SELECT $1", 1, dummy_param_oids, dummy_params, NULL, NULL, 0) != 1) pg_fatal("dispatching second SELECT failed: %s", PQerrorMessage(conn)); @@ -206,6 +214,35 @@ test_multi_pipelines(PGconn *conn) /* second pipeline */ + res = PQgetResult(conn); + if (res == NULL) + pg_fatal("PQgetResult returned null when there's a pipeline item: %s", + PQerrorMessage(conn)); + + if (PQresultStatus(res) != PGRES_TUPLES_OK) + pg_fatal("Unexpected result code %s from first pipeline item", + PQresStatus(PQresultStatus(res))); + PQclear(res); + res = NULL; + + if (PQgetResult(conn) != NULL) + pg_fatal("PQgetResult returned something extra after first result"); + + if (PQexitPipelineMode(conn) != 0) + pg_fatal("exiting pipeline mode after query but before sync succeeded incorrectly"); + + res = PQgetResult(conn); + if (res == NULL) + pg_fatal("PQgetResult returned null when sync result expected: %s", + PQerrorMessage(conn)); + + if (PQresultStatus(res) != PGRES_PIPELINE_SYNC) + pg_fatal("Unexpected result code %s instead of sync result, error: %s", + PQresStatus(PQresultStatus(res)), PQerrorMessage(conn)); + PQclear(res); + + /* third pipeline */ + res = PQgetResult(conn); if (res == NULL) pg_fatal("PQgetResult returned null when there's a pipeline item: %s", diff --git a/src/test/modules/libpq_pipeline/traces/multi_pipelines.trace b/src/test/modules/libpq_pipeline/traces/multi_pipelines.trace index 4b9ab07ca4..1ee21f61dc 100644 --- a/src/test/modules/libpq_pipeline/traces/multi_pipelines.trace +++ b/src/test/modules/libpq_pipeline/traces/multi_pipelines.trace @@ -8,6 +8,17 @@ F 19 Bind "" "" 0 1 1 '1' 1 0 F 6 Describe P "" F 9 Execute "" 0 F 4 Sync +F 21 Parse "" "SELECT $1" 1 NNNN +F 19 Bind "" "" 0 1 1 '1' 1 0 +F 6 Describe P "" +F 9 Execute "" 0 +F 4 Sync +B 4 ParseComplete +B 4 BindComplete +B 33 RowDescription 1 "?column?" NNNN 0 NNNN 4 -1 0 +B 11 DataRow 1 1 '1' +B 13 CommandComplete "SELECT 1" +B 5 ReadyForQuery I B 4 ParseComplete B 4 BindComplete B 33 RowDescription 1 "?column?" NNNN 0 NNNN 4 -1 0 -- 2.34.1
^ permalink raw reply [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-05-03 10:03         ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-05-04 09:21           ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-04 10:36             ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-05-05 15:02               ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-21 17:17                 ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
@ 2023-07-05 20:45                   ` Daniel Gustafsson <daniel@yesql.se>
  2023-07-06 00:42                     ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  0 siblings, 1 reply; 37+ messages in thread

From: Daniel Gustafsson @ 2023-07-05 20:45 UTC (permalink / raw)
  To: Anton Kirilov <antonvkirilov@gmail.com>; +Cc: Alvaro Herrera <alvherre@alvh.no-ip.org>; Michael Paquier <michael@paquier.xyz>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers

> On 21 May 2023, at 19:17, Anton Kirilov <antonvkirilov@gmail.com> wrote:

> .. here is an updated version of the patch 

This hunk here:

-	if (PQflush(conn) < 0)
+	const int ret = flags & PG_PIPELINEPUTSYNC_FLUSH ? PQflush(conn) : 0;
+
+	if (ret < 0)

..is causing this compiler warning:

fe-exec.c: In function ‘PQpipelinePutSync’:
fe-exec.c:3203:2: error: ISO C90 forbids mixed declarations and code [-Werror=declaration-after-statement]
3203 | const int ret = flags & PG_PIPELINEPUTSYNC_FLUSH ? PQflush(conn) : 0;
     | ^~~~~
cc1: all warnings being treated as errors

Also, the patch no longer applies. Please rebase and send an updated version.

--
Daniel Gustafsson






^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-05-03 10:03         ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-05-04 09:21           ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-04 10:36             ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-05-05 15:02               ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-21 17:17                 ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-07-05 20:45                   ` Re: Add PQsendSyncMessage() to libpq Daniel Gustafsson <daniel@yesql.se>
@ 2023-07-06 00:42                     ` Anton Kirilov <antonvkirilov@gmail.com>
  0 siblings, 0 replies; 37+ messages in thread

From: Anton Kirilov @ 2023-07-06 00:42 UTC (permalink / raw)
  To: Daniel Gustafsson <daniel@yesql.se>; +Cc: pgsql-hackers

Hello,

On 05/07/2023 21:45, Daniel Gustafsson wrote:
> Please rebase and send an updated version.
Here it is (including the warning fix).

Thanks,
Anton Kirilov

Attachments:

v4-0001-Add-PQpipelinePutSync-to-libpq.patch (text/x-patch, 11.8K) download From 3c2e064a151568830e4d9e4bf238739b458350b4 Mon Sep 17 00:00:00 2001 From: Anton Kirilov <antonvkirilov@gmail.com> Date: Wed, 22 Mar 2023 20:39:57 +0000 Subject: [PATCH v4] Add PQpipelinePutSync() to libpq This new function is equivalent to PQpipelineSync(), except that it lets the caller choose whether anything is flushed to the server. Its purpose is to reduce the system call overhead of pipeline mode. --- doc/src/sgml/libpq.sgml | 52 ++++++++++++++++--- src/interfaces/libpq/exports.txt | 1 + src/interfaces/libpq/fe-exec.c | 30 ++++++++--- src/interfaces/libpq/libpq-fe.h | 6 +++ .../modules/libpq_pipeline/libpq_pipeline.c | 37 +++++++++++++ .../traces/multi_pipelines.trace | 11 ++++ 6 files changed, 121 insertions(+), 16 deletions(-) diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index b6f5aba1b1..0bc88b1569 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -3547,8 +3547,9 @@ ExecStatusType PQresultStatus(const PGresult *res); <listitem> <para> The <structname>PGresult</structname> represents a - synchronization point in pipeline mode, requested by - <xref linkend="libpq-PQpipelineSync"/>. + synchronization point in pipeline mode, requested by either + <xref linkend="libpq-PQpipelineSync"/> or + <xref linkend="libpq-PQpipelinePutSync"/>. This status occurs only when pipeline mode has been selected. </para> </listitem> @@ -5122,7 +5123,8 @@ int PQsendClosePortal(PGconn *conn, const char *portalName); <xref linkend="libpq-PQsendDescribePrepared"/>, <xref linkend="libpq-PQsendDescribePortal"/>, <xref linkend="libpq-PQsendClosePrepared"/>, - <xref linkend="libpq-PQsendClosePortal"/>, or + <xref linkend="libpq-PQsendClosePortal"/>, + <xref linkend="libpq-PQpipelinePutSync"/>, or <xref linkend="libpq-PQpipelineSync"/> call, and returns it. A null pointer is returned when the command is complete and there @@ -5490,7 +5492,8 @@ int PQflush(PGconn *conn); or its prepared-query sibling <xref linkend="libpq-PQsendQueryPrepared"/>. These requests are queued on the client-side until flushed to the server; - this occurs when <xref linkend="libpq-PQpipelineSync"/> is used to + this occurs when <xref linkend="libpq-PQpipelineSync"/> (or optionally + <xref linkend="libpq-PQpipelinePutSync"/>) is used to establish a synchronization point in the pipeline, or when <xref linkend="libpq-PQflush"/> is called. The functions <xref linkend="libpq-PQsendPrepare"/>, @@ -5506,8 +5509,9 @@ int PQflush(PGconn *conn); client sends them. The server will begin executing the commands in the pipeline immediately, not waiting for the end of the pipeline. Note that results are buffered on the server side; the server flushes - that buffer when a synchronization point is established with - <function>PQpipelineSync</function>, or when + that buffer when a synchronization point is established with either + <function>PQpipelineSync</function> or + <function>PQpipelinePutSync</function>, or when <function>PQsendFlushRequest</function> is called. If any statement encounters an error, the server aborts the current transaction and does not execute any subsequent command in the queue @@ -5564,7 +5568,8 @@ int PQflush(PGconn *conn); <type>PGresult</type> types <literal>PGRES_PIPELINE_SYNC</literal> and <literal>PGRES_PIPELINE_ABORTED</literal>. <literal>PGRES_PIPELINE_SYNC</literal> is reported exactly once for each - <function>PQpipelineSync</function> at the corresponding point + <function>PQpipelineSync</function> or + <function>PQpipelinePutSync</function> at the corresponding point in the pipeline. <literal>PGRES_PIPELINE_ABORTED</literal> is emitted in place of a normal query result for the first error and all subsequent results @@ -5602,7 +5607,8 @@ int PQflush(PGconn *conn); <function>PQresultStatus</function> will report a <literal>PGRES_PIPELINE_ABORTED</literal> result for each remaining queued operation in an aborted pipeline. The result for - <function>PQpipelineSync</function> is reported as + <function>PQpipelineSync</function> or + <function>PQpipelinePutSync</function> is reported as <literal>PGRES_PIPELINE_SYNC</literal> to signal the end of the aborted pipeline and resumption of normal result processing. </para> @@ -5834,6 +5840,36 @@ int PQsendFlushRequest(PGconn *conn); </para> </listitem> </varlistentry> + + <varlistentry id="libpq-PQpipelinePutSync"> + <term><function>PQpipelinePutSync</function><indexterm><primary>PQpipelinePutSync</primary></indexterm></term> + + <listitem> + <para> + Marks a synchronization point in a pipeline by sending a + <link linkend="protocol-flow-ext-query">sync message</link>. + This serves as the delimiter of an implicit transaction and + an error recovery point; see <xref linkend="libpq-pipeline-errors"/>. + +<synopsis> +int PQpipelinePutSync(PGconn *conn, int flags); +</synopsis> + </para> + <para> + Returns 0 for success. Returns -1 if the connection is not in + pipeline mode or sending a + <link linkend="protocol-flow-ext-query">sync message</link> + failed. Returns 1 if it was unable to send all the data in + the send queue yet (this case can only occur if the connection + is nonblocking and output data is flushed to the server). + The <parameter>flags</parameter> argument is a bitwise OR of + several flags. <literal>PG_PIPELINEPUTSYNC_FLUSH</literal> + specifies that any queued output data is flushed to the + server. Otherwise, nothing is flushed automatically; + use <function>PQflush</function> if necessary. + </para> + </listitem> + </varlistentry> </variablelist> </sect2> diff --git a/src/interfaces/libpq/exports.txt b/src/interfaces/libpq/exports.txt index 850734ac96..18e7b31539 100644 --- a/src/interfaces/libpq/exports.txt +++ b/src/interfaces/libpq/exports.txt @@ -191,3 +191,4 @@ PQclosePrepared 188 PQclosePortal 189 PQsendClosePrepared 190 PQsendClosePortal 191 +PQpipelinePutSync 192 diff --git a/src/interfaces/libpq/fe-exec.c b/src/interfaces/libpq/fe-exec.c index 01f8efabbe..823bd74267 100644 --- a/src/interfaces/libpq/fe-exec.c +++ b/src/interfaces/libpq/fe-exec.c @@ -3225,16 +3225,28 @@ pqPipelineProcessQueue(PGconn *conn) */ int PQpipelineSync(PGconn *conn) +{ + return PQpipelinePutSync(conn, PG_PIPELINEPUTSYNC_FLUSH) >= 0; +} + +/* + * PQpipelinePutSync + * Send a Sync message as part of a pipeline, + * and optionally flush to server + */ +int +PQpipelinePutSync(PGconn *conn, int flags) { PGcmdQueueEntry *entry; + int ret; - if (!conn) - return 0; + if (!conn || flags & ~PG_PIPELINEPUTSYNC_FLUSH) + return -1; if (conn->pipelineStatus == PQ_PIPELINE_OFF) { libpq_append_conn_error(conn, "cannot send pipeline when not in pipeline mode"); - return 0; + return -1; } switch (conn->asyncStatus) @@ -3245,7 +3257,7 @@ PQpipelineSync(PGconn *conn) /* should be unreachable */ appendPQExpBufferStr(&conn->errorMessage, "internal error: cannot send pipeline while in COPY\n"); - return 0; + return -1; case PGASYNC_READY: case PGASYNC_READY_MORE: case PGASYNC_BUSY: @@ -3257,7 +3269,7 @@ PQpipelineSync(PGconn *conn) entry = pqAllocCmdQueueEntry(conn); if (entry == NULL) - return 0; /* error msg already set */ + return -1; /* error msg already set */ entry->queryclass = PGQUERY_SYNC; entry->query = NULL; @@ -3271,18 +3283,20 @@ PQpipelineSync(PGconn *conn) * Give the data a push. In nonblock mode, don't complain if we're unable * to send it all; PQgetResult() will do any additional flushing needed. */ - if (PQflush(conn) < 0) + ret = flags & PG_PIPELINEPUTSYNC_FLUSH ? PQflush(conn) : 0; + + if (ret < 0) goto sendFailed; /* OK, it's launched! */ pqAppendCmdQueueEntry(conn, entry); - return 1; + return ret; sendFailed: pqRecycleCmdQueueEntry(conn, entry); /* error message should be set up already */ - return 0; + return -1; } /* diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h index 97762d56f5..eaa09696bb 100644 --- a/src/interfaces/libpq/libpq-fe.h +++ b/src/interfaces/libpq/libpq-fe.h @@ -47,6 +47,11 @@ extern "C" #define PG_COPYRES_EVENTS 0x04 #define PG_COPYRES_NOTICEHOOKS 0x08 +/* + * Option flags for PQpipelinePutSync + */ +#define PG_PIPELINEPUTSYNC_FLUSH 1 + /* Application-visible enum types */ /* @@ -474,6 +479,7 @@ extern int PQenterPipelineMode(PGconn *conn); extern int PQexitPipelineMode(PGconn *conn); extern int PQpipelineSync(PGconn *conn); extern int PQsendFlushRequest(PGconn *conn); +extern int PQpipelinePutSync(PGconn *conn, int flags); /* LISTEN/NOTIFY support */ extern PGnotify *PQnotifies(PGconn *conn); diff --git a/src/test/modules/libpq_pipeline/libpq_pipeline.c b/src/test/modules/libpq_pipeline/libpq_pipeline.c index 9907bc8600..bf2d333ad0 100644 --- a/src/test/modules/libpq_pipeline/libpq_pipeline.c +++ b/src/test/modules/libpq_pipeline/libpq_pipeline.c @@ -169,6 +169,14 @@ test_multi_pipelines(PGconn *conn) if (PQpipelineSync(conn) != 1) pg_fatal("Pipeline sync failed: %s", PQerrorMessage(conn)); + if (PQsendQueryParams(conn, "SELECT $1", 1, dummy_param_oids, + dummy_params, NULL, NULL, 0) != 1) + pg_fatal("dispatching first SELECT failed: %s", PQerrorMessage(conn)); + + /* Skip flushing once. */ + if (PQpipelinePutSync(conn, 0) < 0) + pg_fatal("Pipeline sync failed: %s", PQerrorMessage(conn)); + if (PQsendQueryParams(conn, "SELECT $1", 1, dummy_param_oids, dummy_params, NULL, NULL, 0) != 1) pg_fatal("dispatching second SELECT failed: %s", PQerrorMessage(conn)); @@ -206,6 +214,35 @@ test_multi_pipelines(PGconn *conn) /* second pipeline */ + res = PQgetResult(conn); + if (res == NULL) + pg_fatal("PQgetResult returned null when there's a pipeline item: %s", + PQerrorMessage(conn)); + + if (PQresultStatus(res) != PGRES_TUPLES_OK) + pg_fatal("Unexpected result code %s from first pipeline item", + PQresStatus(PQresultStatus(res))); + PQclear(res); + res = NULL; + + if (PQgetResult(conn) != NULL) + pg_fatal("PQgetResult returned something extra after first result"); + + if (PQexitPipelineMode(conn) != 0) + pg_fatal("exiting pipeline mode after query but before sync succeeded incorrectly"); + + res = PQgetResult(conn); + if (res == NULL) + pg_fatal("PQgetResult returned null when sync result expected: %s", + PQerrorMessage(conn)); + + if (PQresultStatus(res) != PGRES_PIPELINE_SYNC) + pg_fatal("Unexpected result code %s instead of sync result, error: %s", + PQresStatus(PQresultStatus(res)), PQerrorMessage(conn)); + PQclear(res); + + /* third pipeline */ + res = PQgetResult(conn); if (res == NULL) pg_fatal("PQgetResult returned null when there's a pipeline item: %s", diff --git a/src/test/modules/libpq_pipeline/traces/multi_pipelines.trace b/src/test/modules/libpq_pipeline/traces/multi_pipelines.trace index 4b9ab07ca4..1ee21f61dc 100644 --- a/src/test/modules/libpq_pipeline/traces/multi_pipelines.trace +++ b/src/test/modules/libpq_pipeline/traces/multi_pipelines.trace @@ -8,6 +8,17 @@ F 19 Bind "" "" 0 1 1 '1' 1 0 F 6 Describe P "" F 9 Execute "" 0 F 4 Sync +F 21 Parse "" "SELECT $1" 1 NNNN +F 19 Bind "" "" 0 1 1 '1' 1 0 +F 6 Describe P "" +F 9 Execute "" 0 +F 4 Sync +B 4 ParseComplete +B 4 BindComplete +B 33 RowDescription 1 "?column?" NNNN 0 NNNN 4 -1 0 +B 11 DataRow 1 1 '1' +B 13 CommandComplete "SELECT 1" +B 5 ReadyForQuery I B 4 ParseComplete B 4 BindComplete B 33 RowDescription 1 "?column?" NNNN 0 NNNN 4 -1 0 -- 2.34.1
^ permalink raw reply [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-05-03 10:03         ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
@ 2023-05-08 03:14           ` Michael Paquier <michael@paquier.xyz>
  2 siblings, 0 replies; 37+ messages in thread

From: Michael Paquier @ 2023-05-08 03:14 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@alvh.no-ip.org>; +Cc: Robert Haas <robertmhaas@gmail.com>; Anton Kirilov <antonvkirilov@gmail.com>; pgsql-hackers

On Wed, May 03, 2023 at 12:03:57PM +0200, Alvaro Herrera wrote:
> We already have 'int' flag masks in PQcopyResult() and
> PQsetTraceFlags().  We were using bits32 initially for flag stuff in the
> PQtrace facilities, until [1] reminded us that we shouldn't let c.h
> creep into app-land, so that was turned into plain 'int'.
> 
> [1] https://www.postgresql.org/message-id/TYAPR01MB2990B6C6A32ACF15D97AE94AFEBD0%40TYAPR01MB2990.jpnprd0...

Indeed.  Good point!
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-05-03 10:03         ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
@ 2023-11-07 09:23           ` Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-11-08 16:20             ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2 siblings, 1 reply; 37+ messages in thread

From: Jelte Fennema-Nio @ 2023-11-07 09:23 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@alvh.no-ip.org>; +Cc: Robert Haas <robertmhaas@gmail.com>; Michael Paquier <michael@paquier.xyz>; Anton Kirilov <antonvkirilov@gmail.com>; pgsql-hackers

On Fri, 28 Apr 2023 at 14:07, Robert Haas <robertmhaas@gmail.com> wrote:
> I wonder whether this is the naming that we want. The two names are
> significantly different. Something like PQpipelineSendSync() would be
> more similar.
>
> I also wonder, really even more, whether it would be better to do
> something like PQpipelinePutSync(PGconn *conn, bool flush) with
> PQpipelineSync(conn) just meaning PQpipelinePutSync(conn, true). We're
> basically using the function name as a Boolean parameter to select the
> behavior, which is fine if you only have one parameter and it's a
> Boolean, but it's obviously unworkable if you have say 3 Boolean
> parameters because you don't want 8 different functions, and what if
> you need an integer parameter for some reason?

On Wed, 3 May 2023 at 12:04, Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:
> I agree that adding a flag is the way to go, since it improve chances
> that we won't end up with ten different functions in case we decide to
> have eight other behaviors.  One more function and we're done.  And
> while I can't think of any use for a future flag, we (I) already didn't
> of this one either, so let's not make the same mistake.

On Sat, 29 Apr 2023 at 18:07, Anton Kirilov <antonvkirilov@gmail.com> wrote:
> The reason is that the function is modeled after PQsendFlushRequest(),
> since it felt closer to what I was trying to achieve, i.e. appending a
> protocol message to the output buffer without doing any actual I/O
> operations.

Sorry for being late to the party, but I think the current API naming
and the flag argument don't fit well with the current libpq API that
we have. I much prefer something similar to the original version of
the patch.

I think this function should be named something with the "PQsend"
prefix since that's the way we name all our public async message
sending functions in libpq. The "Put" word we only use in internal
libpq functions, so I feel it has no place in the external API
surface. My proposal would be to call the function PQsendPipelineSync
(i.e. having the PQsend prefix while still looking similar to the
existing PQpipelineSync).

Also I think the flag argument is completely unnecessary. I understand
the argument that we didn't foresee the need for this non-flushing
behaviour either, and the follow up reasoning that we thus should add
a flag for future things we didn't forsee. But I think it's looking at
the situation from the wrong direction. Instead of looking at it as
adding another version of our current PQpipelineSync API, we should
look at it as an addition to our current list of PQsend functions for
a new packet type. And none of those PQsend functions ever needed a
flag. Which makes sense, because they are the lowest level building
blocks that make sense from a user perspective: They send a message
type over the socket and don't do anything else. And if the assumption
that this is the lowest level building block is wrong, then it will
almost certainly be wrong for all other PQsend functions too. And thus
we'll need a solution that fits for all of them.

Finally, I have one suggestion for a behavioural change: I think the
function should still call pqPipelineFlush, just like all of our other
PQsend functions (except PQsendFlushRequest, but that seems like an
oversight there too).





^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-05-03 10:03         ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-07 09:23           ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
@ 2023-11-08 16:20             ` Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-12 13:37               ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  0 siblings, 1 reply; 37+ messages in thread

From: Alvaro Herrera @ 2023-11-08 16:20 UTC (permalink / raw)
  To: Jelte Fennema-Nio <postgres@jeltef.nl>; +Cc: Robert Haas <robertmhaas@gmail.com>; Michael Paquier <michael@paquier.xyz>; Anton Kirilov <antonvkirilov@gmail.com>; pgsql-hackers

On 2023-Nov-07, Jelte Fennema-Nio wrote:

> I think this function should be named something with the "PQsend"
> prefix since that's the way we name all our public async message
> sending functions in libpq. The "Put" word we only use in internal
> libpq functions, so I feel it has no place in the external API
> surface. My proposal would be to call the function PQsendPipelineSync
> (i.e. having the PQsend prefix while still looking similar to the
> existing PQpipelineSync).

Argued that way, it makes sense to me.

> Also I think the flag argument is completely unnecessary. [...]
> Instead of looking at it as adding another version of our current
> PQpipelineSync API, we should look at it as an addition to our current
> list of PQsend functions for a new packet type. And none of those
> PQsend functions ever needed a flag.

True.

> Finally, I have one suggestion for a behavioural change: I think the
> function should still call pqPipelineFlush, just like all of our other
> PQsend functions (except PQsendFlushRequest, but that seems like an
> oversight there too).

I agree.

So, yeah, it looks like this will be pretty similar to Anton's original
patch, with PQpipelineSync() being just PQsendPipelineSync() + PQflush().

-- 
Álvaro Herrera        Breisgau, Deutschland  —  https://www.EnterpriseDB.com/
"In fact, the basic problem with Perl 5's subroutines is that they're not
crufty enough, so the cruft leaks out into user-defined code instead, by
the Conservation of Cruft Principle."  (Larry Wall, Apocalypse 6)





^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-05-03 10:03         ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-07 09:23           ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-11-08 16:20             ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
@ 2023-11-12 13:37               ` Anton Kirilov <antonvkirilov@gmail.com>
  2023-11-13 08:19                 ` Re: Add PQsendSyncMessage() to libpq Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
  2023-12-29 11:49                 ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  0 siblings, 2 replies; 37+ messages in thread

From: Anton Kirilov @ 2023-11-12 13:37 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@alvh.no-ip.org>; Jelte Fennema-Nio <postgres@jeltef.nl>; +Cc: Michael Paquier <michael@paquier.xyz>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers

Hello,

Thanks for the feedback!

On 07/11/2023 09:23, Jelte Fennema-Nio wrote:
 > But I think it's looking at the situation from the wrong direction. 
[...] we should look at it as an addition to our current list of PQsend 
functions for a new packet type. And none of those PQsend functions ever 
needed a flag. Which makes sense, because they are the lowest level 
building blocks that make sense from a user perspective: They send a 
message type over the socket and don't do anything else.

Yes, I think that this is quite close to my thinking when I created the 
original version of the patch. Also, the protocol specification states 
that the Sync message lacks parameters.

Since there haven't been any comments from the other people who have 
chimed in on this e-mail thread, I will assume that there is consensus 
(we are doing a U-turn with the implementation approach after all), so 
here is the updated version of the patch.

Best wishes,
Anton Kirilov

Attachments:

v5-0001-Add-PQsendPipelineSync-to-libpq.patch (text/x-patch, 9.8K) download From b752269b2763f8d66bcfc79faf751e52226c344b Mon Sep 17 00:00:00 2001 From: Anton Kirilov <antonvkirilov@gmail.com> Date: Wed, 22 Mar 2023 20:39:57 +0000 Subject: [PATCH v5] Add PQsendPipelineSync() to libpq This new function is equivalent to PQpipelineSync(), except that it does not flush anything to the server; the user must subsequently call PQflush() instead. Its purpose is to reduce the system call overhead of pipeline mode. --- doc/src/sgml/libpq.sgml | 45 ++++++++++++++++--- src/interfaces/libpq/exports.txt | 1 + src/interfaces/libpq/fe-exec.c | 17 +++++-- src/interfaces/libpq/libpq-fe.h | 1 + .../modules/libpq_pipeline/libpq_pipeline.c | 37 +++++++++++++++ .../traces/multi_pipelines.trace | 11 +++++ 6 files changed, 102 insertions(+), 10 deletions(-) diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 64b2910fee..61bee82a54 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -3547,8 +3547,9 @@ ExecStatusType PQresultStatus(const PGresult *res); <listitem> <para> The <structname>PGresult</structname> represents a - synchronization point in pipeline mode, requested by - <xref linkend="libpq-PQpipelineSync"/>. + synchronization point in pipeline mode, requested by either + <xref linkend="libpq-PQpipelineSync"/> or + <xref linkend="libpq-PQsendPipelineSync"/>. This status occurs only when pipeline mode has been selected. </para> </listitem> @@ -5122,7 +5123,8 @@ int PQsendClosePortal(PGconn *conn, const char *portalName); <xref linkend="libpq-PQsendDescribePrepared"/>, <xref linkend="libpq-PQsendDescribePortal"/>, <xref linkend="libpq-PQsendClosePrepared"/>, - <xref linkend="libpq-PQsendClosePortal"/>, or + <xref linkend="libpq-PQsendClosePortal"/>, + <xref linkend="libpq-PQsendPipelineSync"/>, or <xref linkend="libpq-PQpipelineSync"/> call, and returns it. A null pointer is returned when the command is complete and there @@ -5506,8 +5508,9 @@ int PQflush(PGconn *conn); client sends them. The server will begin executing the commands in the pipeline immediately, not waiting for the end of the pipeline. Note that results are buffered on the server side; the server flushes - that buffer when a synchronization point is established with - <function>PQpipelineSync</function>, or when + that buffer when a synchronization point is established with either + <function>PQpipelineSync</function> or + <function>PQsendPipelineSync</function>, or when <function>PQsendFlushRequest</function> is called. If any statement encounters an error, the server aborts the current transaction and does not execute any subsequent command in the queue @@ -5564,7 +5567,8 @@ int PQflush(PGconn *conn); <type>PGresult</type> types <literal>PGRES_PIPELINE_SYNC</literal> and <literal>PGRES_PIPELINE_ABORTED</literal>. <literal>PGRES_PIPELINE_SYNC</literal> is reported exactly once for each - <function>PQpipelineSync</function> at the corresponding point + <function>PQpipelineSync</function> or + <function>PQsendPipelineSync</function> at the corresponding point in the pipeline. <literal>PGRES_PIPELINE_ABORTED</literal> is emitted in place of a normal query result for the first error and all subsequent results @@ -5602,7 +5606,8 @@ int PQflush(PGconn *conn); <function>PQresultStatus</function> will report a <literal>PGRES_PIPELINE_ABORTED</literal> result for each remaining queued operation in an aborted pipeline. The result for - <function>PQpipelineSync</function> is reported as + <function>PQpipelineSync</function> or + <function>PQsendPipelineSync</function> is reported as <literal>PGRES_PIPELINE_SYNC</literal> to signal the end of the aborted pipeline and resumption of normal result processing. </para> @@ -5834,6 +5839,32 @@ int PQsendFlushRequest(PGconn *conn); </para> </listitem> </varlistentry> + + <varlistentry id="libpq-PQsendPipelineSync"> + <term><function>PQsendPipelineSync</function><indexterm><primary>PQsendPipelineSync</primary></indexterm></term> + + <listitem> + <para> + Marks a synchronization point in a pipeline by sending a + <link linkend="protocol-flow-ext-query">sync message</link> + without flushing the send buffer. This serves as + the delimiter of an implicit transaction and an error recovery + point; see <xref linkend="libpq-pipeline-errors"/>. + +<synopsis> +int PQsendPipelineSync(PGconn *conn); +</synopsis> + </para> + <para> + Returns 1 for success. Returns 0 if the connection is not in + pipeline mode or sending a + <link linkend="protocol-flow-ext-query">sync message</link> + failed. + Note that the message is not itself flushed to the server automatically; + use <function>PQflush</function> if necessary. + </para> + </listitem> + </varlistentry> </variablelist> </sect2> diff --git a/src/interfaces/libpq/exports.txt b/src/interfaces/libpq/exports.txt index 850734ac96..f5aa641001 100644 --- a/src/interfaces/libpq/exports.txt +++ b/src/interfaces/libpq/exports.txt @@ -191,3 +191,4 @@ PQclosePrepared 188 PQclosePortal 189 PQsendClosePrepared 190 PQsendClosePortal 191 +PQsendPipelineSync 192 diff --git a/src/interfaces/libpq/fe-exec.c b/src/interfaces/libpq/fe-exec.c index 04610ccf5e..c0dcb04282 100644 --- a/src/interfaces/libpq/fe-exec.c +++ b/src/interfaces/libpq/fe-exec.c @@ -3224,6 +3224,16 @@ pqPipelineProcessQueue(PGconn *conn) */ int PQpipelineSync(PGconn *conn) +{ + return PQsendPipelineSync(conn) && pqFlush(conn) >= 0; +} + +/* + * PQsendPipelineSync + * Send a Sync message as part of a pipeline without flushing to server + */ +int +PQsendPipelineSync(PGconn *conn) { PGcmdQueueEntry *entry; @@ -3267,10 +3277,11 @@ PQpipelineSync(PGconn *conn) goto sendFailed; /* - * Give the data a push. In nonblock mode, don't complain if we're unable - * to send it all; PQgetResult() will do any additional flushing needed. + * Give the data a push if we're past the size threshold. In nonblock + * mode, don't complain if we're unable to send it all; the caller is + * expected to execute PQflush() at some point anyway. */ - if (PQflush(conn) < 0) + if (pqPipelineFlush(conn) < 0) goto sendFailed; /* OK, it's launched! */ diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h index 97762d56f5..b0d89a592d 100644 --- a/src/interfaces/libpq/libpq-fe.h +++ b/src/interfaces/libpq/libpq-fe.h @@ -474,6 +474,7 @@ extern int PQenterPipelineMode(PGconn *conn); extern int PQexitPipelineMode(PGconn *conn); extern int PQpipelineSync(PGconn *conn); extern int PQsendFlushRequest(PGconn *conn); +extern int PQsendPipelineSync(PGconn *conn); /* LISTEN/NOTIFY support */ extern PGnotify *PQnotifies(PGconn *conn); diff --git a/src/test/modules/libpq_pipeline/libpq_pipeline.c b/src/test/modules/libpq_pipeline/libpq_pipeline.c index 3c009ee153..23fb80986b 100644 --- a/src/test/modules/libpq_pipeline/libpq_pipeline.c +++ b/src/test/modules/libpq_pipeline/libpq_pipeline.c @@ -169,6 +169,14 @@ test_multi_pipelines(PGconn *conn) if (PQpipelineSync(conn) != 1) pg_fatal("Pipeline sync failed: %s", PQerrorMessage(conn)); + if (PQsendQueryParams(conn, "SELECT $1", 1, dummy_param_oids, + dummy_params, NULL, NULL, 0) != 1) + pg_fatal("dispatching first SELECT failed: %s", PQerrorMessage(conn)); + + /* Skip flushing once. */ + if (PQsendPipelineSync(conn) != 1) + pg_fatal("Pipeline sync failed: %s", PQerrorMessage(conn)); + if (PQsendQueryParams(conn, "SELECT $1", 1, dummy_param_oids, dummy_params, NULL, NULL, 0) != 1) pg_fatal("dispatching second SELECT failed: %s", PQerrorMessage(conn)); @@ -206,6 +214,35 @@ test_multi_pipelines(PGconn *conn) /* second pipeline */ + res = PQgetResult(conn); + if (res == NULL) + pg_fatal("PQgetResult returned null when there's a pipeline item: %s", + PQerrorMessage(conn)); + + if (PQresultStatus(res) != PGRES_TUPLES_OK) + pg_fatal("Unexpected result code %s from first pipeline item", + PQresStatus(PQresultStatus(res))); + PQclear(res); + res = NULL; + + if (PQgetResult(conn) != NULL) + pg_fatal("PQgetResult returned something extra after first result"); + + if (PQexitPipelineMode(conn) != 0) + pg_fatal("exiting pipeline mode after query but before sync succeeded incorrectly"); + + res = PQgetResult(conn); + if (res == NULL) + pg_fatal("PQgetResult returned null when sync result expected: %s", + PQerrorMessage(conn)); + + if (PQresultStatus(res) != PGRES_PIPELINE_SYNC) + pg_fatal("Unexpected result code %s instead of sync result, error: %s", + PQresStatus(PQresultStatus(res)), PQerrorMessage(conn)); + PQclear(res); + + /* third pipeline */ + res = PQgetResult(conn); if (res == NULL) pg_fatal("PQgetResult returned null when there's a pipeline item: %s", diff --git a/src/test/modules/libpq_pipeline/traces/multi_pipelines.trace b/src/test/modules/libpq_pipeline/traces/multi_pipelines.trace index 4b9ab07ca4..1ee21f61dc 100644 --- a/src/test/modules/libpq_pipeline/traces/multi_pipelines.trace +++ b/src/test/modules/libpq_pipeline/traces/multi_pipelines.trace @@ -8,6 +8,17 @@ F 19 Bind "" "" 0 1 1 '1' 1 0 F 6 Describe P "" F 9 Execute "" 0 F 4 Sync +F 21 Parse "" "SELECT $1" 1 NNNN +F 19 Bind "" "" 0 1 1 '1' 1 0 +F 6 Describe P "" +F 9 Execute "" 0 +F 4 Sync +B 4 ParseComplete +B 4 BindComplete +B 33 RowDescription 1 "?column?" NNNN 0 NNNN 4 -1 0 +B 11 DataRow 1 1 '1' +B 13 CommandComplete "SELECT 1" +B 5 ReadyForQuery I B 4 ParseComplete B 4 BindComplete B 33 RowDescription 1 "?column?" NNNN 0 NNNN 4 -1 0 -- 2.34.1
^ permalink raw reply [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-05-03 10:03         ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-07 09:23           ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-11-08 16:20             ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-12 13:37               ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
@ 2023-11-13 08:19                 ` Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
  2023-12-29 11:52                   ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  1 sibling, 1 reply; 37+ messages in thread

From: Anthonin Bonnefoy @ 2023-11-13 08:19 UTC (permalink / raw)
  To: Anton Kirilov <antonvkirilov@gmail.com>; +Cc: Alvaro Herrera <alvherre@alvh.no-ip.org>; Jelte Fennema-Nio <postgres@jeltef.nl>; Michael Paquier <michael@paquier.xyz>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers

Hi,

I've played a bit with the patch on my side. One thing that would be
great would be to make this
available in pgbench through a \syncpipeline meta command. That would
make it easier for users
to test whether there's a positive impact with their queries or not.

I've wrote a patch to add it to pgbench (don't want to mess with the
thread's attachment so here's a GH link
https://github.com/bonnefoa/postgres/commit/047b5b05169e36361fe29fef9f430da045ef012d).
Here's some quick results:

echo "\set aid1 random(1, 100000 * :scale)
\set aid2 random(1, 100000 * :scale)
\startpipeline
select 1;
select * from pgbench_accounts where aid=:aid1;
select 2;
\syncpipeline
select 1;
select * from pgbench_accounts where aid=:aid2;
select 2;
\endpipeline" > /tmp/pipeline_without_flush.sql
pgbench -T30 -Mextended -f /tmp/pipeline_without_flush.sql -h127.0.0.1
latency average = 0.383 ms
initial connection time = 2.810 ms
tps = 2607.587877 (without initial connection time)

echo "\set aid1 random(1, 100000 * :scale)
\set aid2 random(1, 100000 * :scale)
\startpipeline
select 1;
select * from pgbench_accounts where aid=:aid1;
select 2;
\endpipeline
\startpipeline
select 1;
select * from pgbench_accounts where aid=:aid2;
select 2;
\endpipeline" > /tmp/pipeline_with_flush.sql
pgbench -T30 -Mextended -f /tmp/pipeline_with_flush.sql -h127.0.0.1
latency average = 0.437 ms
initial connection time = 2.602 ms
tps = 2290.527462 (without initial connection time)

I took some perfs and the main change is from the server spending less time in
ReadCommand which makes sense since the commands are sent in a single tcp
frame with the \syncpipeline version.

Regards,
Anthonin

On Sun, Nov 12, 2023 at 2:37 PM Anton Kirilov <antonvkirilov@gmail.com> wrote:
>
> Hello,
>
> Thanks for the feedback!
>
> On 07/11/2023 09:23, Jelte Fennema-Nio wrote:
>  > But I think it's looking at the situation from the wrong direction.
> [...] we should look at it as an addition to our current list of PQsend
> functions for a new packet type. And none of those PQsend functions ever
> needed a flag. Which makes sense, because they are the lowest level
> building blocks that make sense from a user perspective: They send a
> message type over the socket and don't do anything else.
>
> Yes, I think that this is quite close to my thinking when I created the
> original version of the patch. Also, the protocol specification states
> that the Sync message lacks parameters.
>
> Since there haven't been any comments from the other people who have
> chimed in on this e-mail thread, I will assume that there is consensus
> (we are doing a U-turn with the implementation approach after all), so
> here is the updated version of the patch.
>
> Best wishes,
> Anton Kirilov





^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-05-03 10:03         ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-07 09:23           ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-11-08 16:20             ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-12 13:37               ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-11-13 08:19                 ` Re: Add PQsendSyncMessage() to libpq Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
@ 2023-12-29 11:52                   ` Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-12-31 00:37                     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 37+ messages in thread

From: Jelte Fennema-Nio @ 2023-12-29 11:52 UTC (permalink / raw)
  To: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>; +Cc: Anton Kirilov <antonvkirilov@gmail.com>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Michael Paquier <michael@paquier.xyz>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers

On Mon, 13 Nov 2023 at 09:20, Anthonin Bonnefoy
<anthonin.bonnefoy@datadoghq.com> wrote:
> \syncpipeline
> tps = 2607.587877 (without initial connection time)
> ...
> \endpipeline
> \startpipeline
> tps = 2290.527462 (without initial connection time)

Those are some nice improvements. And I think once this patch is in,
it would make sense to add the pgbench feature you're suggesting.
Because indeed that makes it see what perf improvements can be gained
for your workload.





^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-05-03 10:03         ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-07 09:23           ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-11-08 16:20             ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-12 13:37               ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-11-13 08:19                 ` Re: Add PQsendSyncMessage() to libpq Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
  2023-12-29 11:52                   ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
@ 2023-12-31 00:37                     ` Michael Paquier <michael@paquier.xyz>
  2024-01-10 06:40                       ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 37+ messages in thread

From: Michael Paquier @ 2023-12-31 00:37 UTC (permalink / raw)
  To: Jelte Fennema-Nio <postgres@jeltef.nl>; +Cc: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>; Anton Kirilov <antonvkirilov@gmail.com>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers

On Fri, Dec 29, 2023 at 12:52:30PM +0100, Jelte Fennema-Nio wrote:
> On Mon, 13 Nov 2023 at 09:20, Anthonin Bonnefoy
> <anthonin.bonnefoy@datadoghq.com> wrote:
> > \syncpipeline
> > tps = 2607.587877 (without initial connection time)
> > ...
> > \endpipeline
> > \startpipeline
> > tps = 2290.527462 (without initial connection time)
> 
> Those are some nice improvements. And I think once this patch is in,
> it would make sense to add the pgbench feature you're suggesting.
> Because indeed that makes it see what perf improvements can be gained
> for your workload.

Yeah, that sounds like a good idea seen from here.  (Still need to
look at the core patch.)
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-05-03 10:03         ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-07 09:23           ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-11-08 16:20             ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-12 13:37               ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-11-13 08:19                 ` Re: Add PQsendSyncMessage() to libpq Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
  2023-12-29 11:52                   ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-12-31 00:37                     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
@ 2024-01-10 06:40                       ` Michael Paquier <michael@paquier.xyz>
  2024-01-15 07:50                         ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 37+ messages in thread

From: Michael Paquier @ 2024-01-10 06:40 UTC (permalink / raw)
  To: Jelte Fennema-Nio <postgres@jeltef.nl>; +Cc: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>; Anton Kirilov <antonvkirilov@gmail.com>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers

On Sun, Dec 31, 2023 at 09:37:31AM +0900, Michael Paquier wrote:
> On Fri, Dec 29, 2023 at 12:52:30PM +0100, Jelte Fennema-Nio wrote:
>> Those are some nice improvements. And I think once this patch is in,
>> it would make sense to add the pgbench feature you're suggesting.
>> Because indeed that makes it see what perf improvements can be gained
>> for your workload.
> 
> Yeah, that sounds like a good idea seen from here.  (Still need to
> look at the core patch.)

 PQpipelineSync(PGconn *conn)
+{
+	return PQsendPipelineSync(conn) && pqFlush(conn) >= 0;
+}
[...]
+	 * Give the data a push if we're past the size threshold. In nonblock
+	 * mode, don't complain if we're unable to send it all; the caller is
+	 * expected to execute PQflush() at some point anyway.
 	 */
-	if (PQflush(conn) < 0)
+	if (pqPipelineFlush(conn) < 0)
 		goto sendFailed;

I was looking at this patch, and calling PQpipelineSync() would now
cause two calls of PQflush() to be issued when the output buffer
threshold has been reached.  Could that lead to regressions?

A second thing I find disturbing is that pqAppendCmdQueueEntry() would
be called before the final pqFlush(), which could cause the commands
to be listed in a queue even if the flush fails when calling
PQpipelineSync().

Hence, as a whole, wouldn't it be more consistent if the new
PQsendPipelineSync() and the existing PQpipelineSync() call an
internal static routine (PQPipelineSyncInternal?) that can switch
between both modes?  Let's just make the extra argument a boolean.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-05-03 10:03         ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-07 09:23           ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-11-08 16:20             ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-12 13:37               ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-11-13 08:19                 ` Re: Add PQsendSyncMessage() to libpq Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
  2023-12-29 11:52                   ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-12-31 00:37                     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2024-01-10 06:40                       ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
@ 2024-01-15 07:50                         ` Michael Paquier <michael@paquier.xyz>
  2024-01-15 09:01                           ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  2024-01-15 09:49                           ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  0 siblings, 2 replies; 37+ messages in thread

From: Michael Paquier @ 2024-01-15 07:50 UTC (permalink / raw)
  To: Jelte Fennema-Nio <postgres@jeltef.nl>; +Cc: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>; Anton Kirilov <antonvkirilov@gmail.com>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers

On Wed, Jan 10, 2024 at 03:40:36PM +0900, Michael Paquier wrote:
> Hence, as a whole, wouldn't it be more consistent if the new
> PQsendPipelineSync() and the existing PQpipelineSync() call an
> internal static routine (PQPipelineSyncInternal?) that can switch
> between both modes?  Let's just make the extra argument a boolean.

Yeah, I'll go with that after a second look.  Attached is what I am
finishing with, and I have reproduced some numbers with the pgbench
metacommand mentioned upthread, which is reeeaaally nice.

I have also made a few edits to the tests.
--
Michael

Attachments:

v6-0001-Add-PQsendPipelineSync-to-libpq.patch (text/x-diff, 11.7K) download From b653759f8dfdfe83d0d8bc1d4a0ac9d4e272a061 Mon Sep 17 00:00:00 2001 From: Michael Paquier <michael@paquier.xyz> Date: Mon, 15 Jan 2024 16:15:00 +0900 Subject: [PATCH v6] Add PQsendPipelineSync() to libpq This new function is equivalent to PQpipelineSync(), except that it does not flush anything to the server; the user must subsequently call PQflush() instead. Its purpose is to reduce the system call overhead of pipeline mode. --- src/interfaces/libpq/exports.txt | 1 + src/interfaces/libpq/fe-exec.c | 41 +++++++++++++++-- src/interfaces/libpq/libpq-fe.h | 1 + .../modules/libpq_pipeline/libpq_pipeline.c | 43 ++++++++++++++++++ .../traces/multi_pipelines.trace | 11 +++++ doc/src/sgml/libpq.sgml | 45 ++++++++++++++++--- 6 files changed, 131 insertions(+), 11 deletions(-) diff --git a/src/interfaces/libpq/exports.txt b/src/interfaces/libpq/exports.txt index 28b861fd93..088592deb1 100644 --- a/src/interfaces/libpq/exports.txt +++ b/src/interfaces/libpq/exports.txt @@ -192,3 +192,4 @@ PQclosePortal 189 PQsendClosePrepared 190 PQsendClosePortal 191 PQchangePassword 192 +PQsendPipelineSync 193 diff --git a/src/interfaces/libpq/fe-exec.c b/src/interfaces/libpq/fe-exec.c index 106d14e6ee..9e7e670921 100644 --- a/src/interfaces/libpq/fe-exec.c +++ b/src/interfaces/libpq/fe-exec.c @@ -81,6 +81,7 @@ static int PQsendTypedCommand(PGconn *conn, char command, char type, const char *target); static int check_field_number(const PGresult *res, int field_num); static void pqPipelineProcessQueue(PGconn *conn); +static int pqPipelineSyncInternal(PGconn *conn, bool immediate_flush); static int pqPipelineFlush(PGconn *conn); @@ -3224,6 +3225,25 @@ pqPipelineProcessQueue(PGconn *conn) /* * PQpipelineSync * Send a Sync message as part of a pipeline, and flush to server + */ +int +PQpipelineSync(PGconn *conn) +{ + return pqPipelineSyncInternal(conn, true); +} + +/* + * PQsendPipelineSync + * Send a Sync message as part of a pipeline, without flushing to server + */ +int +PQsendPipelineSync(PGconn *conn) +{ + return pqPipelineSyncInternal(conn, false); +} + +/* + * Wrapper for PQpipelineSync and PQsendPipelineSync. * * It's legal to start submitting more commands in the pipeline immediately, * without waiting for the results of the current pipeline. There's no need to @@ -3240,9 +3260,12 @@ pqPipelineProcessQueue(PGconn *conn) * The connection will remain in pipeline mode and unavailable for new * synchronous command execution functions until all results from the pipeline * are processed by the client. + * + * immediate_flush controls if the flush happens immediately after sending the + * Sync message or not. */ -int -PQpipelineSync(PGconn *conn) +static int +pqPipelineSyncInternal(PGconn *conn, bool immediate_flush) { PGcmdQueueEntry *entry; @@ -3288,9 +3311,19 @@ PQpipelineSync(PGconn *conn) /* * Give the data a push. In nonblock mode, don't complain if we're unable * to send it all; PQgetResult() will do any additional flushing needed. + * If immediate_flush is disabled, the data is pushed if we are past the + * size threshold. */ - if (PQflush(conn) < 0) - goto sendFailed; + if (immediate_flush) + { + if (pqFlush(conn) < 0) + goto sendFailed; + } + else + { + if (pqPipelineFlush(conn) < 0) + goto sendFailed; + } /* OK, it's launched! */ pqAppendCmdQueueEntry(conn, entry); diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h index f0ec660cb6..defc415fa3 100644 --- a/src/interfaces/libpq/libpq-fe.h +++ b/src/interfaces/libpq/libpq-fe.h @@ -474,6 +474,7 @@ extern int PQenterPipelineMode(PGconn *conn); extern int PQexitPipelineMode(PGconn *conn); extern int PQpipelineSync(PGconn *conn); extern int PQsendFlushRequest(PGconn *conn); +extern int PQsendPipelineSync(PGconn *conn); /* LISTEN/NOTIFY support */ extern PGnotify *PQnotifies(PGconn *conn); diff --git a/src/test/modules/libpq_pipeline/libpq_pipeline.c b/src/test/modules/libpq_pipeline/libpq_pipeline.c index 71cd04c5f2..c68e20d0b5 100644 --- a/src/test/modules/libpq_pipeline/libpq_pipeline.c +++ b/src/test/modules/libpq_pipeline/libpq_pipeline.c @@ -162,6 +162,7 @@ test_multi_pipelines(PGconn *conn) if (PQenterPipelineMode(conn) != 1) pg_fatal("failed to enter pipeline mode: %s", PQerrorMessage(conn)); + /* first pipeline */ if (PQsendQueryParams(conn, "SELECT $1", 1, dummy_param_oids, dummy_params, NULL, NULL, 0) != 1) pg_fatal("dispatching first SELECT failed: %s", PQerrorMessage(conn)); @@ -169,6 +170,16 @@ test_multi_pipelines(PGconn *conn) if (PQpipelineSync(conn) != 1) pg_fatal("Pipeline sync failed: %s", PQerrorMessage(conn)); + /* second pipeline */ + if (PQsendQueryParams(conn, "SELECT $1", 1, dummy_param_oids, + dummy_params, NULL, NULL, 0) != 1) + pg_fatal("dispatching first SELECT failed: %s", PQerrorMessage(conn)); + + /* Skip flushing once. */ + if (PQsendPipelineSync(conn) != 1) + pg_fatal("Pipeline sync failed: %s", PQerrorMessage(conn)); + + /* third pipeline */ if (PQsendQueryParams(conn, "SELECT $1", 1, dummy_param_oids, dummy_params, NULL, NULL, 0) != 1) pg_fatal("dispatching second SELECT failed: %s", PQerrorMessage(conn)); @@ -177,6 +188,9 @@ test_multi_pipelines(PGconn *conn) pg_fatal("pipeline sync failed: %s", PQerrorMessage(conn)); /* OK, start processing the results */ + + /* first pipeline */ + res = PQgetResult(conn); if (res == NULL) pg_fatal("PQgetResult returned null when there's a pipeline item: %s", @@ -206,6 +220,35 @@ test_multi_pipelines(PGconn *conn) /* second pipeline */ + res = PQgetResult(conn); + if (res == NULL) + pg_fatal("PQgetResult returned null when there's a pipeline item: %s", + PQerrorMessage(conn)); + + if (PQresultStatus(res) != PGRES_TUPLES_OK) + pg_fatal("Unexpected result code %s from first pipeline item", + PQresStatus(PQresultStatus(res))); + PQclear(res); + res = NULL; + + if (PQgetResult(conn) != NULL) + pg_fatal("PQgetResult returned something extra after first result"); + + if (PQexitPipelineMode(conn) != 0) + pg_fatal("exiting pipeline mode after query but before sync succeeded incorrectly"); + + res = PQgetResult(conn); + if (res == NULL) + pg_fatal("PQgetResult returned null when sync result expected: %s", + PQerrorMessage(conn)); + + if (PQresultStatus(res) != PGRES_PIPELINE_SYNC) + pg_fatal("Unexpected result code %s instead of sync result, error: %s", + PQresStatus(PQresultStatus(res)), PQerrorMessage(conn)); + PQclear(res); + + /* third pipeline */ + res = PQgetResult(conn); if (res == NULL) pg_fatal("PQgetResult returned null when there's a pipeline item: %s", diff --git a/src/test/modules/libpq_pipeline/traces/multi_pipelines.trace b/src/test/modules/libpq_pipeline/traces/multi_pipelines.trace index 4b9ab07ca4..1ee21f61dc 100644 --- a/src/test/modules/libpq_pipeline/traces/multi_pipelines.trace +++ b/src/test/modules/libpq_pipeline/traces/multi_pipelines.trace @@ -8,6 +8,17 @@ F 19 Bind "" "" 0 1 1 '1' 1 0 F 6 Describe P "" F 9 Execute "" 0 F 4 Sync +F 21 Parse "" "SELECT $1" 1 NNNN +F 19 Bind "" "" 0 1 1 '1' 1 0 +F 6 Describe P "" +F 9 Execute "" 0 +F 4 Sync +B 4 ParseComplete +B 4 BindComplete +B 33 RowDescription 1 "?column?" NNNN 0 NNNN 4 -1 0 +B 11 DataRow 1 1 '1' +B 13 CommandComplete "SELECT 1" +B 5 ReadyForQuery I B 4 ParseComplete B 4 BindComplete B 33 RowDescription 1 "?column?" NNNN 0 NNNN 4 -1 0 diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 21195e0e72..6d1d477276 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -3547,8 +3547,9 @@ ExecStatusType PQresultStatus(const PGresult *res); <listitem> <para> The <structname>PGresult</structname> represents a - synchronization point in pipeline mode, requested by - <xref linkend="libpq-PQpipelineSync"/>. + synchronization point in pipeline mode, requested by either + <xref linkend="libpq-PQpipelineSync"/> or + <xref linkend="libpq-PQsendPipelineSync"/>. This status occurs only when pipeline mode has been selected. </para> </listitem> @@ -5122,7 +5123,8 @@ int PQsendClosePortal(PGconn *conn, const char *portalName); <xref linkend="libpq-PQsendDescribePrepared"/>, <xref linkend="libpq-PQsendDescribePortal"/>, <xref linkend="libpq-PQsendClosePrepared"/>, - <xref linkend="libpq-PQsendClosePortal"/>, or + <xref linkend="libpq-PQsendClosePortal"/>, + <xref linkend="libpq-PQsendPipelineSync"/>, or <xref linkend="libpq-PQpipelineSync"/> call, and returns it. A null pointer is returned when the command is complete and there @@ -5507,8 +5509,9 @@ int PQflush(PGconn *conn); client sends them. The server will begin executing the commands in the pipeline immediately, not waiting for the end of the pipeline. Note that results are buffered on the server side; the server flushes - that buffer when a synchronization point is established with - <function>PQpipelineSync</function>, or when + that buffer when a synchronization point is established with either + <function>PQpipelineSync</function> or + <function>PQsendPipelineSync</function>, or when <function>PQsendFlushRequest</function> is called. If any statement encounters an error, the server aborts the current transaction and does not execute any subsequent command in the queue @@ -5565,7 +5568,8 @@ int PQflush(PGconn *conn); <type>PGresult</type> types <literal>PGRES_PIPELINE_SYNC</literal> and <literal>PGRES_PIPELINE_ABORTED</literal>. <literal>PGRES_PIPELINE_SYNC</literal> is reported exactly once for each - <function>PQpipelineSync</function> at the corresponding point + <function>PQpipelineSync</function> or + <function>PQsendPipelineSync</function> at the corresponding point in the pipeline. <literal>PGRES_PIPELINE_ABORTED</literal> is emitted in place of a normal query result for the first error and all subsequent results @@ -5603,7 +5607,8 @@ int PQflush(PGconn *conn); <function>PQresultStatus</function> will report a <literal>PGRES_PIPELINE_ABORTED</literal> result for each remaining queued operation in an aborted pipeline. The result for - <function>PQpipelineSync</function> is reported as + <function>PQpipelineSync</function> or + <function>PQsendPipelineSync</function> is reported as <literal>PGRES_PIPELINE_SYNC</literal> to signal the end of the aborted pipeline and resumption of normal result processing. </para> @@ -5835,6 +5840,32 @@ int PQsendFlushRequest(PGconn *conn); </para> </listitem> </varlistentry> + + <varlistentry id="libpq-PQsendPipelineSync"> + <term><function>PQsendPipelineSync</function><indexterm><primary>PQsendPipelineSync</primary></indexterm></term> + + <listitem> + <para> + Marks a synchronization point in a pipeline by sending a + <link linkend="protocol-flow-ext-query">sync message</link> + without flushing the send buffer. This serves as + the delimiter of an implicit transaction and an error recovery + point; see <xref linkend="libpq-pipeline-errors"/>. + +<synopsis> +int PQsendPipelineSync(PGconn *conn); +</synopsis> + </para> + <para> + Returns 1 for success. Returns 0 if the connection is not in + pipeline mode or sending a + <link linkend="protocol-flow-ext-query">sync message</link> + failed. + Note that the message is not itself flushed to the server automatically; + use <function>PQflush</function> if necessary. + </para> + </listitem> + </varlistentry> </variablelist> </sect2> -- 2.43.0
signature.asc (application/pgp-signature, 832B) download ^ permalink raw reply [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-05-03 10:03         ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-07 09:23           ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-11-08 16:20             ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-12 13:37               ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-11-13 08:19                 ` Re: Add PQsendSyncMessage() to libpq Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
  2023-12-29 11:52                   ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-12-31 00:37                     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2024-01-10 06:40                       ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2024-01-15 07:50                         ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
@ 2024-01-15 09:01                           ` Jelte Fennema-Nio <postgres@jeltef.nl>
  2024-01-15 23:28                             ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  1 sibling, 1 reply; 37+ messages in thread

From: Jelte Fennema-Nio @ 2024-01-15 09:01 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>; Anton Kirilov <antonvkirilov@gmail.com>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers

On Mon, 15 Jan 2024 at 08:50, Michael Paquier <michael@paquier.xyz> wrote:
> Yeah, I'll go with that after a second look.  Attached is what I am
> finishing with, and I have reproduced some numbers with the pgbench
> metacommand mentioned upthread, which is reeeaaally nice.

Code looks good to me. But one small notes on the test.

+     /* second pipeline */
+     if (PQsendQueryParams(conn, "SELECT $1", 1, dummy_param_oids,
+                                     dummy_params, NULL, NULL, 0) != 1)
+           pg_fatal("dispatching first SELECT failed: %s",
PQerrorMessage(conn));

Error message should be "second SELECT" not "first SELECT". Same note
for the error message in the third pipeline, where it still says
"second SELECT".


+     res = PQgetResult(conn);
+     if (res == NULL)
+           pg_fatal("PQgetResult returned null when there's a
pipeline item: %s",
+                        PQerrorMessage(conn));
+
+     if (PQresultStatus(res) != PGRES_TUPLES_OK)
+           pg_fatal("Unexpected result code %s from first pipeline item",
+                        PQresStatus(PQresultStatus(res)));
+     PQclear(res);
+     res = NULL;
+
+     if (PQgetResult(conn) != NULL)
+           pg_fatal("PQgetResult returned something extra after first result");

same issue: s/first/second/g (and s/second/third/g for the existing
part of the test).





^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-05-03 10:03         ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-07 09:23           ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-11-08 16:20             ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-12 13:37               ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-11-13 08:19                 ` Re: Add PQsendSyncMessage() to libpq Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
  2023-12-29 11:52                   ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-12-31 00:37                     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2024-01-10 06:40                       ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2024-01-15 07:50                         ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2024-01-15 09:01                           ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
@ 2024-01-15 23:28                             ` Michael Paquier <michael@paquier.xyz>
  0 siblings, 0 replies; 37+ messages in thread

From: Michael Paquier @ 2024-01-15 23:28 UTC (permalink / raw)
  To: Jelte Fennema-Nio <postgres@jeltef.nl>; +Cc: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>; Anton Kirilov <antonvkirilov@gmail.com>; Alvaro Herrera <alvherre@alvh.no-ip.org>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers

On Mon, Jan 15, 2024 at 10:01:59AM +0100, Jelte Fennema-Nio wrote:
> Error message should be "second SELECT" not "first SELECT". Same note
> for the error message in the third pipeline, where it still says
> "second SELECT".
>
> same issue: s/first/second/g (and s/second/third/g for the existing
> part of the test).

Ugh, yes.  The note in the test was wrong.  Thanks for
double-checking.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-05-03 10:03         ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-07 09:23           ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-11-08 16:20             ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-12 13:37               ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-11-13 08:19                 ` Re: Add PQsendSyncMessage() to libpq Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
  2023-12-29 11:52                   ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-12-31 00:37                     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2024-01-10 06:40                       ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2024-01-15 07:50                         ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
@ 2024-01-15 09:49                           ` Alvaro Herrera <alvherre@alvh.no-ip.org>
  2024-01-16 03:32                             ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  1 sibling, 1 reply; 37+ messages in thread

From: Alvaro Herrera @ 2024-01-15 09:49 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Jelte Fennema-Nio <postgres@jeltef.nl>; Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>; Anton Kirilov <antonvkirilov@gmail.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers

On 2024-Jan-15, Michael Paquier wrote:

Looks good!  Just some small notes,

> +/*
> + * Wrapper for PQpipelineSync and PQsendPipelineSync.
>   *
>   * It's legal to start submitting more commands in the pipeline immediately,
>   * without waiting for the results of the current pipeline. There's no need to

the new function pqPipelineSyncInternal is not a wrapper for these other
two functions -- the opposite is true actually.  We tend to use the term
"workhorse" or "internal workhorse" for this kind of thing.

In the docs, after this patch we have

- PQpipelineSync
- PQsendFlushRequest
- PQsendPipelineSync

Wouldn't it make more sense to add the new function in the middle of the
two existing ones instead?


Looking again at the largish comment that's now atop
pqPipelineSyncInternal(), I think most of it should be removed -- these
things should be explained in the SGML docs, and I think they are, in
the "Using Pipeline Mode" section.  We can just have the lines this
patch is adding.

-- 
Álvaro Herrera               48°01'N 7°57'E  —  https://www.EnterpriseDB.com/
"I can't go to a restaurant and order food because I keep looking at the
fonts on the menu.  Five minutes later I realize that it's also talking
about food" (Donald Knuth)





^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-05-03 10:03         ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-07 09:23           ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-11-08 16:20             ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-12 13:37               ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-11-13 08:19                 ` Re: Add PQsendSyncMessage() to libpq Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
  2023-12-29 11:52                   ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-12-31 00:37                     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2024-01-10 06:40                       ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2024-01-15 07:50                         ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2024-01-15 09:49                           ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
@ 2024-01-16 03:32                             ` Michael Paquier <michael@paquier.xyz>
  2024-01-16 13:55                               ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  0 siblings, 1 reply; 37+ messages in thread

From: Michael Paquier @ 2024-01-16 03:32 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@alvh.no-ip.org>; +Cc: Jelte Fennema-Nio <postgres@jeltef.nl>; Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>; Anton Kirilov <antonvkirilov@gmail.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers

On Mon, Jan 15, 2024 at 10:49:56AM +0100, Alvaro Herrera wrote:
> the new function pqPipelineSyncInternal is not a wrapper for these other
> two functions -- the opposite is true actually.  We tend to use the term
> "workhorse" or "internal workhorse" for this kind of thing.

Indeed, makes sense.

> In the docs, after this patch we have
> 
> - PQpipelineSync
> - PQsendFlushRequest
> - PQsendPipelineSync
> 
> Wouldn't it make more sense to add the new function in the middle of the
> two existing ones instead?

Ordering PQsendPipelineSync just after PQpipelineSync is OK by me.
I've applied the patch with all these modifications to move on with
the subject.

> Looking again at the largish comment that's now atop
> pqPipelineSyncInternal(), I think most of it should be removed -- these
> things should be explained in the SGML docs, and I think they are, in
> the "Using Pipeline Mode" section.  We can just have the lines this
> patch is adding.

Hmm.  The first two sentences about being able to submit more commands
to the pipeline are documented in the subsection "Issuing Queries".
The third sentence is implied in the second paragraph of this
subsection.  The 4th paragraph of the comment where sync commands
cannot be issued until all the results from the pipeline have been
consumed is mentioned in the first paragraph in "Using Pipeline Mode".
So you are right that this could be entirely removed.

How about the attached to remove all that, then?
--
Michael

Attachments:

libpq-exec-comments.patch (text/x-diff, 1.3K) download diff --git a/src/interfaces/libpq/fe-exec.c b/src/interfaces/libpq/fe-exec.c index 52d41658c1..152b100624 100644 --- a/src/interfaces/libpq/fe-exec.c +++ b/src/interfaces/libpq/fe-exec.c @@ -3245,23 +3245,6 @@ PQsendPipelineSync(PGconn *conn) /* * Workhorse function for PQpipelineSync and PQsendPipelineSync. * - * It's legal to start submitting more commands in the pipeline immediately, - * without waiting for the results of the current pipeline. There's no need to - * end pipeline mode and start it again. - * - * If a command in a pipeline fails, every subsequent command up to and - * including the result to the Sync message sent by pqPipelineSyncInternal - * gets set to PGRES_PIPELINE_ABORTED state. If the whole pipeline is - * processed without error, a PGresult with PGRES_PIPELINE_SYNC is produced. - * - * Queries can already have been sent before pqPipelineSyncInternal is called, - * but pqPipelineSyncInternal needs to be called before retrieving command - * results. - * - * The connection will remain in pipeline mode and unavailable for new - * synchronous command execution functions until all results from the pipeline - * are processed by the client. - * * immediate_flush controls if the flush happens immediately after sending the * Sync message or not. */
signature.asc (application/pgp-signature, 832B) download ^ permalink raw reply [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-05-03 10:03         ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-07 09:23           ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-11-08 16:20             ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-12 13:37               ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-11-13 08:19                 ` Re: Add PQsendSyncMessage() to libpq Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
  2023-12-29 11:52                   ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-12-31 00:37                     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2024-01-10 06:40                       ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2024-01-15 07:50                         ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2024-01-15 09:49                           ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2024-01-16 03:32                             ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
@ 2024-01-16 13:55                               ` Alvaro Herrera <alvherre@alvh.no-ip.org>
  2024-01-17 07:30                                 ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 37+ messages in thread

From: Alvaro Herrera @ 2024-01-16 13:55 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Jelte Fennema-Nio <postgres@jeltef.nl>; Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>; Anton Kirilov <antonvkirilov@gmail.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers

On 2024-Jan-16, Michael Paquier wrote:

> I've applied the patch with all these modifications to move on with
> the subject.

Thanks!

> On Mon, Jan 15, 2024 at 10:49:56AM +0100, Alvaro Herrera wrote:

> > Looking again at the largish comment that's now atop
> > pqPipelineSyncInternal(), I think most of it should be removed -- these
> > things should be explained in the SGML docs, and I think they are, in
> > the "Using Pipeline Mode" section.  We can just have the lines this
> > patch is adding.
> 
> Hmm.  The first two sentences about being able to submit more commands
> to the pipeline are documented in the subsection "Issuing Queries".
> The third sentence is implied in the second paragraph of this
> subsection.  The 4th paragraph of the comment where sync commands
> cannot be issued until all the results from the pipeline have been
> consumed is mentioned in the first paragraph in "Using Pipeline Mode".
> So you are right that this could be entirely removed.

(I'm pretty sure that the history of this comment is that Craig Ringer
wrote it for his prototype patch, and then I took the various parts and
struggled to add them as SGML docs as it made logical sense.  So if
there's anything in the comment that's important and not covered by the
docs, that would be a docs bug.)  I agree with your findings.

> How about the attached to remove all that, then?

Looks good, thank you.

-- 
Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/
"Tiene valor aquel que admite que es un cobarde" (Fernandel)





^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-05-03 10:03         ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-07 09:23           ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-11-08 16:20             ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-12 13:37               ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-11-13 08:19                 ` Re: Add PQsendSyncMessage() to libpq Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
  2023-12-29 11:52                   ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-12-31 00:37                     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2024-01-10 06:40                       ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2024-01-15 07:50                         ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2024-01-15 09:49                           ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2024-01-16 03:32                             ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2024-01-16 13:55                               ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
@ 2024-01-17 07:30                                 ` Michael Paquier <michael@paquier.xyz>
  2024-01-18 23:11                                   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  0 siblings, 1 reply; 37+ messages in thread

From: Michael Paquier @ 2024-01-17 07:30 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@alvh.no-ip.org>; +Cc: Jelte Fennema-Nio <postgres@jeltef.nl>; Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>; Anton Kirilov <antonvkirilov@gmail.com>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers

On Tue, Jan 16, 2024 at 02:55:12PM +0100, Alvaro Herrera wrote:
> On 2024-Jan-16, Michael Paquier wrote:
>> How about the attached to remove all that, then?
> 
> Looks good, thank you.

Thanks for double-checking.  Done.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-05-03 10:03         ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-07 09:23           ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-11-08 16:20             ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-12 13:37               ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-11-13 08:19                 ` Re: Add PQsendSyncMessage() to libpq Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
  2023-12-29 11:52                   ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-12-31 00:37                     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2024-01-10 06:40                       ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2024-01-15 07:50                         ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2024-01-15 09:49                           ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2024-01-16 03:32                             ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2024-01-16 13:55                               ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2024-01-17 07:30                                 ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
@ 2024-01-18 23:11                                   ` Anton Kirilov <antonvkirilov@gmail.com>
  0 siblings, 0 replies; 37+ messages in thread

From: Anton Kirilov @ 2024-01-18 23:11 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Alvaro Herrera <alvherre@alvh.no-ip.org>; Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>; Jelte Fennema-Nio <postgres@jeltef.nl>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers

Hello,

On 17/01/2024 07:30, Michael Paquier wrote:
> Thanks for double-checking.  Done.
Thank you very much for taking care of my patch!

One thing that I noticed is that the TODO list on the PostgreSQL Wiki 
still contained an entry ( https://wiki.postgresql.org/wiki/Todo#libpq ) 
about adding pipelining support to libpq - perhaps it ought to be updated?

Best wishes,
Anton Kirilov





^ permalink  raw  reply  [nested|flat] 37+ messages in thread

* Re: Add PQsendSyncMessage() to libpq
  2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-04-28 12:06 ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-04-29 16:06   ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
  2023-05-02 00:42     ` Re: Add PQsendSyncMessage() to libpq Michael Paquier <michael@paquier.xyz>
  2023-05-02 14:02       ` Re: Add PQsendSyncMessage() to libpq Robert Haas <robertmhaas@gmail.com>
  2023-05-03 10:03         ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-07 09:23           ` Re: Add PQsendSyncMessage() to libpq Jelte Fennema-Nio <postgres@jeltef.nl>
  2023-11-08 16:20             ` Re: Add PQsendSyncMessage() to libpq Alvaro Herrera <alvherre@alvh.no-ip.org>
  2023-11-12 13:37               ` Re: Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
@ 2023-12-29 11:49                 ` Jelte Fennema-Nio <postgres@jeltef.nl>
  1 sibling, 0 replies; 37+ messages in thread

From: Jelte Fennema-Nio @ 2023-12-29 11:49 UTC (permalink / raw)
  To: Anton Kirilov <antonvkirilov@gmail.com>; +Cc: Alvaro Herrera <alvherre@alvh.no-ip.org>; Michael Paquier <michael@paquier.xyz>; Robert Haas <robertmhaas@gmail.com>; pgsql-hackers

On Sun, 12 Nov 2023 at 14:37, Anton Kirilov <antonvkirilov@gmail.com> wrote:
> Since there haven't been any comments from the other people who have
> chimed in on this e-mail thread, I will assume that there is consensus
> (we are doing a U-turn with the implementation approach after all), so
> here is the updated version of the patch.

The new patch looks great to me. And indeed consensus seems to have
been reached on the approach and that this patch is useful. So I'm
taking the liberty of marking this patch as Ready for Committer.





^ permalink  raw  reply  [nested|flat] 37+ messages in thread


end of thread, other threads:[~2024-01-18 23:11 UTC | newest]

Thread overview: 37+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2023-03-24 22:38 Add PQsendSyncMessage() to libpq Anton Kirilov <antonvkirilov@gmail.com>
2023-04-25 14:23 ` Denis Laxalde <denis.laxalde@dalibo.com>
2023-04-26 22:56   ` Anton Kirilov <antonvkirilov@gmail.com>
2023-04-27 11:06     ` Denis Laxalde <denis.laxalde@dalibo.com>
2023-04-28 07:10       ` Michael Paquier <michael@paquier.xyz>
2023-04-28 08:08         ` Denis Laxalde <denis.laxalde@dalibo.com>
2023-04-30 00:59           ` Anton Kirilov <antonvkirilov@gmail.com>
2023-05-01 23:55             ` Michael Paquier <michael@paquier.xyz>
2023-05-22 00:18               ` Anton Kirilov <antonvkirilov@gmail.com>
2023-04-28 12:06 ` Robert Haas <robertmhaas@gmail.com>
2023-04-29 16:06   ` Anton Kirilov <antonvkirilov@gmail.com>
2023-05-02 00:42     ` Michael Paquier <michael@paquier.xyz>
2023-05-02 14:02       ` Robert Haas <robertmhaas@gmail.com>
2023-05-03 10:03         ` Alvaro Herrera <alvherre@alvh.no-ip.org>
2023-05-04 09:21           ` Anton Kirilov <antonvkirilov@gmail.com>
2023-05-04 10:36             ` Alvaro Herrera <alvherre@alvh.no-ip.org>
2023-05-05 15:02               ` Anton Kirilov <antonvkirilov@gmail.com>
2023-05-21 17:17                 ` Anton Kirilov <antonvkirilov@gmail.com>
2023-07-05 20:45                   ` Daniel Gustafsson <daniel@yesql.se>
2023-07-06 00:42                     ` Anton Kirilov <antonvkirilov@gmail.com>
2023-05-08 03:14           ` Michael Paquier <michael@paquier.xyz>
2023-11-07 09:23           ` Jelte Fennema-Nio <postgres@jeltef.nl>
2023-11-08 16:20             ` Alvaro Herrera <alvherre@alvh.no-ip.org>
2023-11-12 13:37               ` Anton Kirilov <antonvkirilov@gmail.com>
2023-11-13 08:19                 ` Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
2023-12-29 11:52                   ` Jelte Fennema-Nio <postgres@jeltef.nl>
2023-12-31 00:37                     ` Michael Paquier <michael@paquier.xyz>
2024-01-10 06:40                       ` Michael Paquier <michael@paquier.xyz>
2024-01-15 07:50                         ` Michael Paquier <michael@paquier.xyz>
2024-01-15 09:01                           ` Jelte Fennema-Nio <postgres@jeltef.nl>
2024-01-15 23:28                             ` Michael Paquier <michael@paquier.xyz>
2024-01-15 09:49                           ` Alvaro Herrera <alvherre@alvh.no-ip.org>
2024-01-16 03:32                             ` Michael Paquier <michael@paquier.xyz>
2024-01-16 13:55                               ` Alvaro Herrera <alvherre@alvh.no-ip.org>
2024-01-17 07:30                                 ` Michael Paquier <michael@paquier.xyz>
2024-01-18 23:11                                   ` Anton Kirilov <antonvkirilov@gmail.com>
2023-12-29 11:49                 ` Jelte Fennema-Nio <postgres@jeltef.nl>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox