diff options
author | Michael Paquier | 2025-02-25 01:07:24 +0000 |
---|---|---|
committer | Michael Paquier | 2025-02-25 01:07:24 +0000 |
commit | 3ce357584e795762aa6dc45fafc21dccea7e5ec6 (patch) | |
tree | 97dc2a4cb677b4a9d8d909aaf9794a1c656721a0 /src/bin/psql | |
parent | cbb9086c9ef64b020cb9036f50afc14644bbf734 (diff) |
psql: Add pipeline status to prompt and some state variables
This commit adds %P to psql prompts, able to report the status of a
pipeline depending on PQpipelineStatus(): on, off or abort.
The following variables are added to report the state of an ongoing
pipeline:
- PIPELINE_SYNC_COUNT: reports the number of piped syncs.
- PIPELINE_COMMAND_COUNT: reports the number of piped commands, a
command being either \bind, \bind_named, \close or \parse.
- PIPELINE_RESULT_COUNT: reports the results available to read with
\getresults.
These variables can be used with \echo or in a prompt, using "%:name:"
in PROMPT1, PROMPT2 or PROMPT3. Some basic regression tests are added
for these. The suggestion to use variables to show the details about
the status counters comes from me. The original patch proposed was less
extensible, hardcoding the output in the prompt.
Author: Anthonin Bonnefoy <[email protected]>
Discussion: https://postgr.es/m/CAO6_XqroE7JuMEm1sWz55rp9fAYX2JwmcP_3m_v51vnOFdsLiQ@mail.gmail.com
Diffstat (limited to 'src/bin/psql')
-rw-r--r-- | src/bin/psql/common.c | 27 | ||||
-rw-r--r-- | src/bin/psql/prompt.c | 14 | ||||
-rw-r--r-- | src/bin/psql/startup.c | 5 |
3 files changed, 45 insertions, 1 deletions
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index bc8c40898f7..ed340a466f9 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -525,6 +525,26 @@ SetShellResultVariables(int wait_result) /* + * Set special pipeline variables + * - PIPELINE_SYNC_COUNT: The number of piped syncs + * - PIPELINE_COMMAND_COUNT: The number of piped commands + * - PIPELINE_RESULT_COUNT: The number of results available to read + */ +static void +SetPipelineVariables(void) +{ + char buf[32]; + + snprintf(buf, sizeof(buf), "%d", pset.piped_syncs); + SetVariable(pset.vars, "PIPELINE_SYNC_COUNT", buf); + snprintf(buf, sizeof(buf), "%d", pset.piped_commands); + SetVariable(pset.vars, "PIPELINE_COMMAND_COUNT", buf); + snprintf(buf, sizeof(buf), "%d", pset.available_results); + SetVariable(pset.vars, "PIPELINE_RESULT_COUNT", buf); +} + + +/* * ClearOrSaveResult * * If the result represents an error, remember it for possible display by @@ -1661,6 +1681,8 @@ ExecQueryAndProcessResults(const char *query, CheckConnection(); + SetPipelineVariables(); + return -1; } @@ -1669,8 +1691,10 @@ ExecQueryAndProcessResults(const char *query, { /* * We are in a pipeline and have not reached the pipeline end, or - * there was no request to read pipeline results, exit. + * there was no request to read pipeline results. Update the psql + * variables tracking the pipeline activity and exit. */ + SetPipelineVariables(); return 1; } @@ -2105,6 +2129,7 @@ ExecQueryAndProcessResults(const char *query, Assert(pset.available_results == 0); } Assert(pset.requested_results == 0); + SetPipelineVariables(); /* may need this to recover from conn loss during COPY */ if (!CheckConnection()) diff --git a/src/bin/psql/prompt.c b/src/bin/psql/prompt.c index 08a14feb3c3..3aa7d2d06c8 100644 --- a/src/bin/psql/prompt.c +++ b/src/bin/psql/prompt.c @@ -31,6 +31,7 @@ * sockets, "[local:/dir/name]" if not default * %m - like %M, but hostname only (before first dot), or always "[local]" * %p - backend pid + * %P - pipeline status: on, off or abort * %> - database server port number * %n - database user name * %s - service @@ -181,6 +182,19 @@ get_prompt(promptStatus_t status, ConditionalStack cstack) snprintf(buf, sizeof(buf), "%d", pid); } break; + /* pipeline status */ + case 'P': + { + PGpipelineStatus status = PQpipelineStatus(pset.db); + + if (status == PQ_PIPELINE_ON) + strlcpy(buf, "on", sizeof(buf)); + else if (status == PQ_PIPELINE_ABORTED) + strlcpy(buf, "abort", sizeof(buf)); + else + strlcpy(buf, "off", sizeof(buf)); + break; + } case '0': case '1': diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 703f3f582c1..5018eedf1e5 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -205,6 +205,11 @@ main(int argc, char *argv[]) SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3); SetVariableBool(pset.vars, "SHOW_ALL_RESULTS"); + /* Initialize pipeline variables */ + SetVariable(pset.vars, "PIPELINE_SYNC_COUNT", "0"); + SetVariable(pset.vars, "PIPELINE_COMMAND_COUNT", "0"); + SetVariable(pset.vars, "PIPELINE_RESULT_COUNT", "0"); + parse_psql_options(argc, argv, &options); /* |