1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-30 06:01:21 +03:00

libpq: Add PQsendPipelineSync()

This new function is equivalent to PQpipelineSync(), except that it does
not flush anything to the server except if the size threshold of the
output buffer is reached; the user must subsequently call PQflush()
instead.

Its purpose is to reduce the system call overhead of pipeline mode, by
giving to applications more control over the timing of the flushes when
manipulating commands in pipeline mode.

Author: Anton Kirilov
Reviewed-by: Jelte Fennema-Nio, Robert Haas, Álvaro Herrera, Denis
Laxalde, Michael Paquier
Discussion: https://postgr.es/m/CACV6eE5arHFZEA717=iKEa_OewpVFfWJOmsOdGrqqsr8CJVfWQ@mail.gmail.com
This commit is contained in:
Michael Paquier
2024-01-16 10:13:42 +09:00
parent 83eb244e41
commit 4794c2d317
6 changed files with 138 additions and 17 deletions

View File

@@ -192,3 +192,4 @@ PQclosePortal 189
PQsendClosePrepared 190
PQsendClosePortal 191
PQchangePassword 192
PQsendPipelineSync 193

View File

@@ -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,25 +3225,48 @@ 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);
}
/*
* 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 PQpipelineSync gets set to
* PGRES_PIPELINE_ABORTED state. If the whole pipeline is processed without
* error, a PGresult with PGRES_PIPELINE_SYNC is produced.
* 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 PQpipelineSync is called, but
* PQpipelineSync needs to be called before retrieving command results.
* 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.
*/
int
PQpipelineSync(PGconn *conn)
static int
pqPipelineSyncInternal(PGconn *conn, bool immediate_flush)
{
PGcmdQueueEntry *entry;
@@ -3288,9 +3312,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);

View File

@@ -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);