aboutsummaryrefslogtreecommitdiff
path: root/src/bin/psql
diff options
context:
space:
mode:
authorMichael Paquier <michael@paquier.xyz>2025-02-21 11:19:59 +0900
committerMichael Paquier <michael@paquier.xyz>2025-02-21 11:19:59 +0900
commit41625ab8ea3d0a2656dd0f067f1f0b61df63af97 (patch)
tree627d9167d7f49899ad72efa635bece58aa5eaa11 /src/bin/psql
parent40af897eb777bc8a6afca14195587e79e57a5c06 (diff)
downloadpostgresql-41625ab8ea3d0a2656dd0f067f1f0b61df63af97.tar.gz
postgresql-41625ab8ea3d0a2656dd0f067f1f0b61df63af97.zip
psql: Add support for pipelines
With \bind, \parse, \bind_named and \close, it is possible to issue queries from psql using the extended protocol. However, it was not possible to send these queries using libpq's pipeline mode. This feature has two advantages: - Testing. Pipeline tests were only possible with pgbench, using TAP tests. It now becomes possible to have more SQL tests that are able to stress the backend with pipelines and extended queries. More tests will be added in a follow-up commit that were discussed on some other threads. Some external projects in the community had to implement their own facility to work around this limitation. - Emulation of custom workloads, with more control over the actions taken by a client with libpq APIs. It is possible to emulate more workload patterns to bottleneck the backend with the extended query protocol. This patch adds six new meta-commands to be able to control pipelines: * \startpipeline starts a new pipeline. All extended queries are queued until the end of the pipeline are reached or a sync request is sent and processed. * \endpipeline ends an existing pipeline. All queued commands are sent to the server and all responses are processed by psql. * \syncpipeline queues a synchronisation request, without flushing the commands to the server, equivalent of PQsendPipelineSync(). * \flush, equivalent of PQflush(). * \flushrequest, equivalent of PQsendFlushRequest() * \getresults reads the server's results for the queries in a pipeline. Unsent data is automatically pushed when \getresults is called. It is possible to control the number of results read in a single meta-command execution with an optional parameter, 0 means that all the results should be read. Author: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com> Reviewed-by: Jelte Fennema-Nio <postgres@jeltef.nl> Reviewed-by: Kirill Reshke <reshkekirill@gmail.com> Discussion: https://postgr.es/m/CAO6_XqroE7JuMEm1sWz55rp9fAYX2JwmcP_3m_v51vnOFdsLiQ@mail.gmail.com
Diffstat (limited to 'src/bin/psql')
-rw-r--r--src/bin/psql/command.c170
-rw-r--r--src/bin/psql/common.c288
-rw-r--r--src/bin/psql/help.c7
-rw-r--r--src/bin/psql/settings.h12
-rw-r--r--src/bin/psql/tab-complete.in.c8
5 files changed, 474 insertions, 11 deletions
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 26dfdde195a..12d99eec5a3 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -90,9 +90,12 @@ static backslashResult exec_command_else(PsqlScanState scan_state, ConditionalSt
PQExpBuffer query_buf);
static backslashResult exec_command_endif(PsqlScanState scan_state, ConditionalStack cstack,
PQExpBuffer query_buf);
+static backslashResult exec_command_endpipeline(PsqlScanState scan_state, bool active_branch);
static backslashResult exec_command_encoding(PsqlScanState scan_state, bool active_branch);
static backslashResult exec_command_errverbose(PsqlScanState scan_state, bool active_branch);
static backslashResult exec_command_f(PsqlScanState scan_state, bool active_branch);
+static backslashResult exec_command_flush(PsqlScanState scan_state, bool active_branch);
+static backslashResult exec_command_flushrequest(PsqlScanState scan_state, bool active_branch);
static backslashResult exec_command_g(PsqlScanState scan_state, bool active_branch,
const char *cmd);
static backslashResult process_command_g_options(char *first_option,
@@ -103,6 +106,7 @@ static backslashResult exec_command_gdesc(PsqlScanState scan_state, bool active_
static backslashResult exec_command_getenv(PsqlScanState scan_state, bool active_branch,
const char *cmd);
static backslashResult exec_command_gexec(PsqlScanState scan_state, bool active_branch);
+static backslashResult exec_command_getresults(PsqlScanState scan_state, bool active_branch);
static backslashResult exec_command_gset(PsqlScanState scan_state, bool active_branch);
static backslashResult exec_command_help(PsqlScanState scan_state, bool active_branch);
static backslashResult exec_command_html(PsqlScanState scan_state, bool active_branch);
@@ -132,6 +136,8 @@ static backslashResult exec_command_setenv(PsqlScanState scan_state, bool active
const char *cmd);
static backslashResult exec_command_sf_sv(PsqlScanState scan_state, bool active_branch,
const char *cmd, bool is_func);
+static backslashResult exec_command_startpipeline(PsqlScanState scan_state, bool active_branch);
+static backslashResult exec_command_syncpipeline(PsqlScanState scan_state, bool active_branch);
static backslashResult exec_command_t(PsqlScanState scan_state, bool active_branch);
static backslashResult exec_command_T(PsqlScanState scan_state, bool active_branch);
static backslashResult exec_command_timing(PsqlScanState scan_state, bool active_branch);
@@ -351,18 +357,26 @@ exec_command(const char *cmd,
status = exec_command_else(scan_state, cstack, query_buf);
else if (strcmp(cmd, "endif") == 0)
status = exec_command_endif(scan_state, cstack, query_buf);
+ else if (strcmp(cmd, "endpipeline") == 0)
+ status = exec_command_endpipeline(scan_state, active_branch);
else if (strcmp(cmd, "encoding") == 0)
status = exec_command_encoding(scan_state, active_branch);
else if (strcmp(cmd, "errverbose") == 0)
status = exec_command_errverbose(scan_state, active_branch);
else if (strcmp(cmd, "f") == 0)
status = exec_command_f(scan_state, active_branch);
+ else if (strcmp(cmd, "flush") == 0)
+ status = exec_command_flush(scan_state, active_branch);
+ else if (strcmp(cmd, "flushrequest") == 0)
+ status = exec_command_flushrequest(scan_state, active_branch);
else if (strcmp(cmd, "g") == 0 || strcmp(cmd, "gx") == 0)
status = exec_command_g(scan_state, active_branch, cmd);
else if (strcmp(cmd, "gdesc") == 0)
status = exec_command_gdesc(scan_state, active_branch);
else if (strcmp(cmd, "getenv") == 0)
status = exec_command_getenv(scan_state, active_branch, cmd);
+ else if (strcmp(cmd, "getresults") == 0)
+ status = exec_command_getresults(scan_state, active_branch);
else if (strcmp(cmd, "gexec") == 0)
status = exec_command_gexec(scan_state, active_branch);
else if (strcmp(cmd, "gset") == 0)
@@ -411,6 +425,10 @@ exec_command(const char *cmd,
status = exec_command_sf_sv(scan_state, active_branch, cmd, true);
else if (strcmp(cmd, "sv") == 0 || strcmp(cmd, "sv+") == 0)
status = exec_command_sf_sv(scan_state, active_branch, cmd, false);
+ else if (strcmp(cmd, "startpipeline") == 0)
+ status = exec_command_startpipeline(scan_state, active_branch);
+ else if (strcmp(cmd, "syncpipeline") == 0)
+ status = exec_command_syncpipeline(scan_state, active_branch);
else if (strcmp(cmd, "t") == 0)
status = exec_command_t(scan_state, active_branch);
else if (strcmp(cmd, "T") == 0)
@@ -1516,6 +1534,44 @@ exec_command_f(PsqlScanState scan_state, bool active_branch)
}
/*
+ * \flush -- call PQflush() on the connection
+ */
+static backslashResult
+exec_command_flush(PsqlScanState scan_state, bool active_branch)
+{
+ backslashResult status = PSQL_CMD_SKIP_LINE;
+
+ if (active_branch)
+ {
+ pset.send_mode = PSQL_SEND_FLUSH;
+ status = PSQL_CMD_SEND;
+ }
+ else
+ ignore_slash_options(scan_state);
+
+ return status;
+}
+
+/*
+ * \flushrequest -- call PQsendFlushRequest() on the connection
+ */
+static backslashResult
+exec_command_flushrequest(PsqlScanState scan_state, bool active_branch)
+{
+ backslashResult status = PSQL_CMD_SKIP_LINE;
+
+ if (active_branch)
+ {
+ pset.send_mode = PSQL_SEND_FLUSH_REQUEST;
+ status = PSQL_CMD_SEND;
+ }
+ else
+ ignore_slash_options(scan_state);
+
+ return status;
+}
+
+/*
* \g [(pset-option[=pset-value] ...)] [filename/shell-command]
* \gx [(pset-option[=pset-value] ...)] [filename/shell-command]
*
@@ -1550,6 +1606,14 @@ exec_command_g(PsqlScanState scan_state, bool active_branch, const char *cmd)
if (status == PSQL_CMD_SKIP_LINE && active_branch)
{
+ if (strcmp(cmd, "gx") == 0 &&
+ PQpipelineStatus(pset.db) != PQ_PIPELINE_OFF)
+ {
+ pg_log_error("\\gx not allowed in pipeline mode");
+ clean_extended_state();
+ return PSQL_CMD_ERROR;
+ }
+
if (!fname)
pset.gfname = NULL;
else
@@ -1704,6 +1768,42 @@ exec_command_getenv(PsqlScanState scan_state, bool active_branch,
}
/*
+ * \getresults -- read results
+ */
+static backslashResult
+exec_command_getresults(PsqlScanState scan_state, bool active_branch)
+{
+ backslashResult status = PSQL_CMD_SKIP_LINE;
+
+ if (active_branch)
+ {
+ char *opt;
+ int num_results;
+
+ pset.send_mode = PSQL_SEND_GET_RESULTS;
+ status = PSQL_CMD_SEND;
+ opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
+
+ pset.requested_results = 0;
+ if (opt != NULL)
+ {
+ num_results = atoi(opt);
+ if (num_results < 0)
+ {
+ pg_log_error("\\getresults: invalid number of requested results");
+ return PSQL_CMD_SKIP_LINE;
+ }
+ pset.requested_results = num_results;
+ }
+ }
+ else
+ ignore_slash_options(scan_state);
+
+ return status;
+}
+
+
+/*
* \gexec -- send query and execute each field of result
*/
static backslashResult
@@ -1713,6 +1813,12 @@ exec_command_gexec(PsqlScanState scan_state, bool active_branch)
if (active_branch)
{
+ if (PQpipelineStatus(pset.db) != PQ_PIPELINE_OFF)
+ {
+ pg_log_error("\\gexec not allowed in pipeline mode");
+ clean_extended_state();
+ return PSQL_CMD_ERROR;
+ }
pset.gexec_flag = true;
status = PSQL_CMD_SEND;
}
@@ -1733,6 +1839,13 @@ exec_command_gset(PsqlScanState scan_state, bool active_branch)
char *prefix = psql_scan_slash_option(scan_state,
OT_NORMAL, NULL, false);
+ if (PQpipelineStatus(pset.db) != PQ_PIPELINE_OFF)
+ {
+ pg_log_error("\\gset not allowed in pipeline mode");
+ clean_extended_state();
+ return PSQL_CMD_ERROR;
+ }
+
if (prefix)
pset.gset_prefix = prefix;
else
@@ -2719,6 +2832,63 @@ exec_command_sf_sv(PsqlScanState scan_state, bool active_branch,
}
/*
+ * \startpipeline -- enter pipeline mode
+ */
+static backslashResult
+exec_command_startpipeline(PsqlScanState scan_state, bool active_branch)
+{
+ backslashResult status = PSQL_CMD_SKIP_LINE;
+
+ if (active_branch)
+ {
+ pset.send_mode = PSQL_SEND_START_PIPELINE_MODE;
+ status = PSQL_CMD_SEND;
+ }
+ else
+ ignore_slash_options(scan_state);
+
+ return status;
+}
+
+/*
+ * \syncpipeline -- send a sync message to an active pipeline
+ */
+static backslashResult
+exec_command_syncpipeline(PsqlScanState scan_state, bool active_branch)
+{
+ backslashResult status = PSQL_CMD_SKIP_LINE;
+
+ if (active_branch)
+ {
+ pset.send_mode = PSQL_SEND_PIPELINE_SYNC;
+ status = PSQL_CMD_SEND;
+ }
+ else
+ ignore_slash_options(scan_state);
+
+ return status;
+}
+
+/*
+ * \endpipeline -- end pipeline mode
+ */
+static backslashResult
+exec_command_endpipeline(PsqlScanState scan_state, bool active_branch)
+{
+ backslashResult status = PSQL_CMD_SKIP_LINE;
+
+ if (active_branch)
+ {
+ pset.send_mode = PSQL_SEND_END_PIPELINE_MODE;
+ status = PSQL_CMD_SEND;
+ }
+ else
+ ignore_slash_options(scan_state);
+
+ return status;
+}
+
+/*
* \t -- turn off table headers and row count
*/
static backslashResult
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index 2dd3739485a..bc8c40898f7 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -122,6 +122,18 @@ CloseGOutput(FILE *gfile_fout, bool is_pipe)
}
/*
+ * Reset pset pipeline state
+ */
+static void
+pipelineReset(void)
+{
+ pset.piped_syncs = 0;
+ pset.piped_commands = 0;
+ pset.available_results = 0;
+ pset.requested_results = 0;
+}
+
+/*
* setQFout
* -- handler for -o command line option and \o command
*
@@ -354,6 +366,7 @@ CheckConnection(void)
fprintf(stderr, _("The connection to the server was lost. Attempting reset: "));
PQreset(pset.db);
+ pipelineReset();
OK = ConnectionUp();
if (!OK)
{
@@ -415,10 +428,12 @@ AcceptResult(const PGresult *result, bool show_error)
case PGRES_EMPTY_QUERY:
case PGRES_COPY_IN:
case PGRES_COPY_OUT:
+ case PGRES_PIPELINE_SYNC:
/* Fine, do nothing */
OK = true;
break;
+ case PGRES_PIPELINE_ABORTED:
case PGRES_BAD_RESPONSE:
case PGRES_NONFATAL_ERROR:
case PGRES_FATAL_ERROR:
@@ -1050,6 +1065,7 @@ PrintQueryResult(PGresult *result, bool last,
success = true;
break;
+ case PGRES_PIPELINE_ABORTED:
case PGRES_BAD_RESPONSE:
case PGRES_NONFATAL_ERROR:
case PGRES_FATAL_ERROR:
@@ -1418,6 +1434,63 @@ DescribeQuery(const char *query, double *elapsed_msec)
return OK;
}
+/*
+ * Read and discard all results in an aborted pipeline.
+ *
+ * If a synchronisation point is found, we can stop discarding results as
+ * the pipeline will switch back to a clean state. If no synchronisation
+ * point is available, we need to stop when ther are no more pending
+ * results, otherwise, calling PQgetResult() would block.
+ */
+static PGresult *
+discardAbortedPipelineResults(void)
+{
+ for (;;)
+ {
+ PGresult *res = PQgetResult(pset.db);
+ ExecStatusType result_status = PQresultStatus(res);
+
+ if (result_status == PGRES_PIPELINE_SYNC)
+ {
+ /*
+ * Found a synchronisation point. The sync counter is decremented
+ * by the caller.
+ */
+ return res;
+ }
+ else if (res == NULL)
+ {
+ /* A query was processed, decrement the counters */
+ Assert(pset.available_results > 0);
+ Assert(pset.requested_results > 0);
+ pset.available_results--;
+ pset.requested_results--;
+ }
+
+ if (pset.requested_results == 0)
+ {
+ /* We have read all the requested results, leave */
+ return res;
+ }
+
+ if (pset.available_results == 0 && pset.piped_syncs == 0)
+ {
+ /*
+ * There are no more results to get and there is no
+ * synchronisation point to stop at. This will leave the pipeline
+ * in an aborted state.
+ */
+ return res;
+ }
+
+ /*
+ * An aborted pipeline will have either NULL results or results in an
+ * PGRES_PIPELINE_ABORTED status.
+ */
+ Assert(res == NULL || result_status == PGRES_PIPELINE_ABORTED);
+ PQclear(res);
+ }
+}
/*
* ExecQueryAndProcessResults: utility function for use by SendQuery()
@@ -1430,7 +1503,7 @@ DescribeQuery(const char *query, double *elapsed_msec)
* input or output stream. In that event, we'll marshal data for the COPY.
*
* For other commands, the results are processed normally, depending on their
- * status.
+ * status and the status of a pipeline.
*
* When invoked from \watch, is_watch is true and min_rows is the value
* of that option, or 0 if it wasn't set.
@@ -1451,6 +1524,7 @@ ExecQueryAndProcessResults(const char *query,
bool timing = pset.timing;
bool success = false;
bool return_early = false;
+ bool end_pipeline = false;
instr_time before,
after;
PGresult *result;
@@ -1466,9 +1540,13 @@ ExecQueryAndProcessResults(const char *query,
{
case PSQL_SEND_EXTENDED_CLOSE:
success = PQsendClosePrepared(pset.db, pset.stmtName);
+ if (success && PQpipelineStatus(pset.db) != PQ_PIPELINE_OFF)
+ pset.piped_commands++;
break;
case PSQL_SEND_EXTENDED_PARSE:
success = PQsendPrepare(pset.db, pset.stmtName, query, 0, NULL);
+ if (success && PQpipelineStatus(pset.db) != PQ_PIPELINE_OFF)
+ pset.piped_commands++;
break;
case PSQL_SEND_EXTENDED_QUERY_PARAMS:
Assert(pset.stmtName == NULL);
@@ -1476,6 +1554,8 @@ ExecQueryAndProcessResults(const char *query,
pset.bind_nparams, NULL,
(const char *const *) pset.bind_params,
NULL, NULL, 0);
+ if (success && PQpipelineStatus(pset.db) != PQ_PIPELINE_OFF)
+ pset.piped_commands++;
break;
case PSQL_SEND_EXTENDED_QUERY_PREPARED:
Assert(pset.stmtName != NULL);
@@ -1483,6 +1563,89 @@ ExecQueryAndProcessResults(const char *query,
pset.bind_nparams,
(const char *const *) pset.bind_params,
NULL, NULL, 0);
+ if (success && PQpipelineStatus(pset.db) != PQ_PIPELINE_OFF)
+ pset.piped_commands++;
+ break;
+ case PSQL_SEND_START_PIPELINE_MODE:
+ success = PQenterPipelineMode(pset.db);
+ break;
+ case PSQL_SEND_END_PIPELINE_MODE:
+ success = PQpipelineSync(pset.db);
+ if (success && PQpipelineStatus(pset.db) != PQ_PIPELINE_OFF)
+ {
+ /*
+ * End of the pipeline, all queued commands need to be
+ * processed.
+ */
+ end_pipeline = true;
+ pset.piped_syncs++;
+
+ /*
+ * The server will send a ReadyForQuery after a Sync is
+ * processed, flushing all the results back to the client.
+ */
+ pset.available_results += pset.piped_commands;
+ pset.piped_commands = 0;
+
+ /* We want to read all results */
+ pset.requested_results = pset.available_results + pset.piped_syncs;
+ }
+ break;
+ case PSQL_SEND_PIPELINE_SYNC:
+ success = PQsendPipelineSync(pset.db);
+ if (success && PQpipelineStatus(pset.db) != PQ_PIPELINE_OFF)
+ {
+ pset.piped_syncs++;
+
+ /*
+ * The server will send a ReadyForQuery after a Sync is
+ * processed, flushing all the results back to the client.
+ */
+ pset.available_results += pset.piped_commands;
+ pset.piped_commands = 0;
+ }
+ break;
+ case PSQL_SEND_FLUSH:
+ success = PQflush(pset.db);
+ break;
+ case PSQL_SEND_FLUSH_REQUEST:
+ success = PQsendFlushRequest(pset.db);
+ if (success && PQpipelineStatus(pset.db) != PQ_PIPELINE_OFF)
+ {
+ /*
+ * With the flush request, all commands in the pipeline are
+ * pushed and the server will flush the results back to the
+ * client, making them available.
+ */
+ pset.available_results += pset.piped_commands;
+ pset.piped_commands = 0;
+ }
+ break;
+ case PSQL_SEND_GET_RESULTS:
+ if (pset.available_results == 0 && pset.piped_syncs == 0)
+ {
+ /*
+ * If no sync or flush request were sent, PQgetResult() would
+ * block as there are no results available. Forbid any
+ * attempt to get pending results should we try to reach this
+ * state.
+ */
+ pg_log_info("No pending results to get");
+ success = false;
+ pset.requested_results = 0;
+ }
+ else
+ {
+ success = true;
+
+ /*
+ * Cap requested_results to the maximum number of known
+ * results.
+ */
+ if (pset.requested_results == 0 ||
+ pset.requested_results > (pset.available_results + pset.piped_syncs))
+ pset.requested_results = pset.available_results + pset.piped_syncs;
+ }
break;
case PSQL_SEND_QUERY:
success = PQsendQuery(pset.db, query);
@@ -1501,6 +1664,16 @@ ExecQueryAndProcessResults(const char *query,
return -1;
}
+ if (pset.requested_results == 0 && !end_pipeline &&
+ PQpipelineStatus(pset.db) != PQ_PIPELINE_OFF)
+ {
+ /*
+ * We are in a pipeline and have not reached the pipeline end, or
+ * there was no request to read pipeline results, exit.
+ */
+ return 1;
+ }
+
/*
* Fetch the result in chunks if FETCH_COUNT is set, except when:
*
@@ -1548,7 +1721,7 @@ ExecQueryAndProcessResults(const char *query,
{
ExecStatusType result_status;
bool is_chunked_result = false;
- PGresult *next_result;
+ PGresult *next_result = NULL;
bool last;
if (!AcceptResult(result, false))
@@ -1571,6 +1744,9 @@ ExecQueryAndProcessResults(const char *query,
ClearOrSaveResult(result);
success = false;
+ if (result_status == PGRES_PIPELINE_ABORTED)
+ pg_log_info("Pipeline aborted, command did not run");
+
/*
* switch to next result
*/
@@ -1586,6 +1762,22 @@ ExecQueryAndProcessResults(const char *query,
*/
result = NULL;
}
+ else if ((end_pipeline || pset.requested_results > 0)
+ && PQpipelineStatus(pset.db) != PQ_PIPELINE_OFF)
+ {
+ /*
+ * Error within a pipeline. All commands are aborted until
+ * the next synchronisation point. We need to consume all the
+ * results until this synchronisation point, or stop when
+ * there are no more result to discard.
+ *
+ * Checking the pipeline status is necessary for the case
+ * where the connection was reset. The new connection is not
+ * in any kind of pipeline state and thus has no result to
+ * discard.
+ */
+ result = discardAbortedPipelineResults();
+ }
else
result = PQgetResult(pset.db);
@@ -1772,12 +1964,66 @@ ExecQueryAndProcessResults(const char *query,
}
}
+ if (result_status == PGRES_PIPELINE_SYNC)
+ {
+ Assert(pset.piped_syncs > 0);
+
+ /*
+ * Sync response, decrease the sync and requested_results
+ * counters.
+ */
+ pset.piped_syncs--;
+ pset.requested_results--;
+
+ /*
+ * After a synchronisation point, reset success state to print
+ * possible successful results that will be processed after this.
+ */
+ success = true;
+
+ /*
+ * If all syncs were processed and pipeline end was requested,
+ * exit pipeline mode.
+ */
+ if (end_pipeline && pset.piped_syncs == 0)
+ success &= PQexitPipelineMode(pset.db);
+ }
+ else if (PQpipelineStatus(pset.db) != PQ_PIPELINE_OFF &&
+ result_status != PGRES_PIPELINE_SYNC)
+ {
+ /*
+ * In a pipeline with a non-sync response? Decrease the result
+ * counters.
+ */
+ pset.available_results--;
+ pset.requested_results--;
+ }
+
/*
* Check PQgetResult() again. In the typical case of a single-command
* string, it will return NULL. Otherwise, we'll have other results
* to process. We need to do that to check whether this is the last.
*/
- next_result = PQgetResult(pset.db);
+ if (PQpipelineStatus(pset.db) == PQ_PIPELINE_OFF)
+ next_result = PQgetResult(pset.db);
+ else
+ {
+ /*
+ * In pipeline mode, a NULL result indicates the end of the
+ * current query being processed. Call PQgetResult() once to
+ * consume this state.
+ */
+ if (result_status != PGRES_PIPELINE_SYNC)
+ {
+ next_result = PQgetResult(pset.db);
+ Assert(next_result == NULL);
+ }
+
+ /* Now, we can get the next result in the pipeline. */
+ if (pset.requested_results > 0)
+ next_result = PQgetResult(pset.db);
+ }
+
last = (next_result == NULL);
/*
@@ -1799,8 +2045,13 @@ ExecQueryAndProcessResults(const char *query,
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
- /* this may or may not print something depending on settings */
- if (result != NULL)
+ /*
+ * This may or may not print something depending on settings.
+ *
+ * A pipeline sync will have a non-NULL result but does not have
+ * anything to print, thus ignore results in this case.
+ */
+ if (result != NULL && result_status != PGRES_PIPELINE_SYNC)
{
/*
* If results need to be printed into the file specified by \g,
@@ -1826,9 +2077,15 @@ ExecQueryAndProcessResults(const char *query,
ClearOrSaveResult(result);
result = next_result;
- if (cancel_pressed)
+ if (cancel_pressed && PQpipelineStatus(pset.db) == PQ_PIPELINE_OFF)
{
- /* drop this next result, as well as any others not yet read */
+ /*
+ * Outside of a pipeline, drop the next result, as well as any
+ * others not yet read.
+ *
+ * Within a pipeline, we can let the outer loop handle this as an
+ * aborted pipeline, which will discard then all the results.
+ */
ClearOrSaveResult(result);
ClearOrSaveAllResults();
break;
@@ -1838,6 +2095,17 @@ ExecQueryAndProcessResults(const char *query,
/* close \g file if we opened it */
CloseGOutput(gfile_fout, gfile_is_pipe);
+ if (end_pipeline)
+ {
+ /* after a pipeline is processed, pipeline piped_syncs should be 0 */
+ Assert(pset.piped_syncs == 0);
+ /* all commands have been processed */
+ Assert(pset.piped_commands == 0);
+ /* all results were read */
+ Assert(pset.available_results == 0);
+ }
+ Assert(pset.requested_results == 0);
+
/* may need this to recover from conn loss during COPY */
if (!CheckConnection())
return -1;
@@ -2298,6 +2566,12 @@ clean_extended_state(void)
pset.bind_params = NULL;
break;
case PSQL_SEND_QUERY:
+ case PSQL_SEND_START_PIPELINE_MODE: /* \startpipeline */
+ case PSQL_SEND_END_PIPELINE_MODE: /* \endpipeline */
+ case PSQL_SEND_PIPELINE_SYNC: /* \syncpipeline */
+ case PSQL_SEND_FLUSH: /* \flush */
+ case PSQL_SEND_GET_RESULTS: /* \getresults */
+ case PSQL_SEND_FLUSH_REQUEST: /* \flushrequest */
break;
}
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index da8e1ade5df..714b8619233 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -167,15 +167,22 @@ slashUsage(unsigned short int pager)
HELP0(" \\close STMT_NAME close an existing prepared statement\n");
HELP0(" \\copyright show PostgreSQL usage and distribution terms\n");
HELP0(" \\crosstabview [COLUMNS] execute query and display result in crosstab\n");
+ HELP0(" \\endpipeline exit pipeline mode\n");
HELP0(" \\errverbose show most recent error message at maximum verbosity\n");
+ HELP0(" \\flush push unsent data to the server\n");
+ HELP0(" \\flushrequest send a flushrequest command\n");
HELP0(" \\g [(OPTIONS)] [FILE] execute query (and send result to file or |pipe);\n"
" \\g with no arguments is equivalent to a semicolon\n");
HELP0(" \\gdesc describe result of query, without executing it\n");
+ HELP0(" \\getresults [NUM_RES] read NUM_RES pending results. All pending results are\n"
+ " read if no argument is provided\n");
HELP0(" \\gexec execute query, then execute each value in its result\n");
HELP0(" \\gset [PREFIX] execute query and store result in psql variables\n");
HELP0(" \\gx [(OPTIONS)] [FILE] as \\g, but forces expanded output mode\n");
HELP0(" \\parse STMT_NAME create a prepared statement\n");
HELP0(" \\q quit psql\n");
+ HELP0(" \\startpipeline enter pipeline mode\n");
+ HELP0(" \\syncpipeline add a synchronisation point to an ongoing pipeline\n");
HELP0(" \\watch [[i=]SEC] [c=N] [m=MIN]\n"
" execute query every SEC seconds, up to N times,\n"
" stop if less than MIN rows are returned\n");
diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h
index 2a8fe12eb55..71f553c22ad 100644
--- a/src/bin/psql/settings.h
+++ b/src/bin/psql/settings.h
@@ -69,6 +69,12 @@ typedef enum
PSQL_SEND_EXTENDED_PARSE,
PSQL_SEND_EXTENDED_QUERY_PARAMS,
PSQL_SEND_EXTENDED_QUERY_PREPARED,
+ PSQL_SEND_PIPELINE_SYNC,
+ PSQL_SEND_START_PIPELINE_MODE,
+ PSQL_SEND_END_PIPELINE_MODE,
+ PSQL_SEND_FLUSH,
+ PSQL_SEND_FLUSH_REQUEST,
+ PSQL_SEND_GET_RESULTS,
} PSQL_SEND_MODE;
typedef enum
@@ -111,6 +117,12 @@ typedef struct _psqlSettings
char **bind_params; /* parameters for extended query protocol call */
char *stmtName; /* prepared statement name used for extended
* query protocol commands */
+ int piped_commands; /* number of piped commands */
+ int piped_syncs; /* number of piped syncs */
+ int available_results; /* number of results available to get */
+ int requested_results; /* number of requested results, including
+ * sync messages. Used to read a limited
+ * subset of the available_results. */
bool crosstab_flag; /* one-shot request to crosstab result */
char *ctv_args[4]; /* \crosstabview arguments */
diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c
index eb8bc128720..8432be641ac 100644
--- a/src/bin/psql/tab-complete.in.c
+++ b/src/bin/psql/tab-complete.in.c
@@ -1885,9 +1885,9 @@ psql_completion(const char *text, int start, int end)
"\\drds", "\\drg", "\\dRs", "\\dRp", "\\ds",
"\\dt", "\\dT", "\\dv", "\\du", "\\dx", "\\dX", "\\dy",
"\\echo", "\\edit", "\\ef", "\\elif", "\\else", "\\encoding",
- "\\endif", "\\errverbose", "\\ev",
- "\\f",
- "\\g", "\\gdesc", "\\getenv", "\\gexec", "\\gset", "\\gx",
+ "\\endif", "\\endpipeline", "\\errverbose", "\\ev",
+ "\\f", "\\flush", "\\flushrequest",
+ "\\g", "\\gdesc", "\\getenv", "\\getresults", "\\gexec", "\\gset", "\\gx",
"\\help", "\\html",
"\\if", "\\include", "\\include_relative", "\\ir",
"\\list", "\\lo_import", "\\lo_export", "\\lo_list", "\\lo_unlink",
@@ -1895,7 +1895,7 @@ psql_completion(const char *text, int start, int end)
"\\parse", "\\password", "\\print", "\\prompt", "\\pset",
"\\qecho", "\\quit",
"\\reset",
- "\\s", "\\set", "\\setenv", "\\sf", "\\sv",
+ "\\s", "\\set", "\\setenv", "\\sf", "\\startpipeline", "\\sv", "\\syncpipeline",
"\\t", "\\T", "\\timing",
"\\unset",
"\\x",