Re: Add Pipelining support in psql
Michael Paquier <michael@paquier.xyz>
On Tue, Jan 14, 2025 at 09:49:02AM +0100, Anthonin Bonnefoy wrote:
> During my tests, I've noticed I didn't handle query cancellation, this
> is now fixed. I've also added additional comments related to
> available_results to make it clearer that it depends on what the
> server has flushed to the client.
This is a pretty cool patch. I like the structure you have used for
the integration with the tracking of the number of commands, the
number of syncs (like pgbench) put in a pipeline, the number of
results requested and the number of results available. That makes the
whole easier to look at with a state in pset.
+ PSQL_SEND_PIPELINE_SYNC,
+ PSQL_START_PIPELINE_MODE,
+ PSQL_END_PIPELINE_MODE,
+ PSQL_FLUSH,
+ PSQL_SEND_FLUSH_REQUEST,
+ PSQL_GET_RESULTS,
These new values are inconsistent, let's use some more PSQL_SEND_*
here. That makes the whole set of values more greppable.
The tests in psql.sql are becoming really long. Perhaps it would be
better to split that into its own file, say psql_pipeline.sql? The
input file is already 2k lines, you are adding 15% more lines to that.
+ * otherwise, calling PQgetResults will block.
Likely PQgetResults => PQgetResult().
Wondering if the cancellation in ExecQueryAndProcessResults() is
sound, I've not been able to break it, still..
I can also get behind the additions of \flush and \flushrequest to
query different parts of libpq.
+ if (pset.requested_results == 0)
+ /* We've read all requested results, exit */
+ return res;
Set of nits with the style of the code, but I'd suggest to use
braces {} here, to outline that the comment is in a block. There's a
second, larger one in discardAbortedPipelineResults().
+ 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;
+ }
What is the reasoning here behind this restriction? \gx is a wrapper
of \g with expanded mode on, but it is also possible to call \g with
expanded=on, bypassing this restriction.
Let's split the prompt patch with the support of %P into its own
patch.
-#define DEFAULT_PROMPT1 "%/%R%x%# "
-#define DEFAULT_PROMPT2 "%/%R%x%# "
+#define DEFAULT_PROMPT1 "%/%R%P%x%# "
+#define DEFAULT_PROMPT2 "%/%R%P%x%# "
#define DEFAULT_PROMPT3 ">> "
I don't think that changing this default is a good idea. Everybody
can do that in their own .psqlrc (spoiler: I do).
The idea to use three fields with a hardcoded format does not look
like a good idea to me. I think that this should be done in a
different and more extensible way:
- Use %P to track if we are in pipeline mode on, off or abort.
- Define three new variables that behave like ROW_COUNT, but for the
fields you want to track here. These could then be passed down to a
PROMPT with %:name:, grammar already supported.
That would make the whole much more flexible. At it seems to me that
we could also add requested_results to this set? These could be named
with the same prefix, like PIPELINE_SYNC_COUNT, etc.
--
Michael
Commits
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
psql: Fix assertion failures with pipeline mode
- 3631612eae9c 18.0 landed
-
psql: Rework TAP routine psql_fails_like() to define WAL sender context
- 0ff95e0a5be1 18.0 landed
-
psql: Split extended query protocol meta-commands in --help=commands
- 78231baaf967 18.0 landed
-
psql: Improve descriptions of \\flush[request] in --help
- 5743d122fcf3 18.0 landed
-
psql: Fix incorrect status code returned by \getresults
- 5ee7bd944ee8 18.0 landed
-
psql: Allow queries terminated by semicolons while in pipeline mode
- 2cce0fe440fb 18.0 landed
-
psql: Add \sendpipeline to send query buffers while in a pipeline
- 17caf6644546 18.0 landed
-
psql: Fix memory leak with \gx used within a pipeline
- 54d23601b978 18.0 landed
-
psql: Add pipeline status to prompt and some state variables
- 3ce357584e79 18.0 landed
-
Add more tests for utility commands in pipelines
- a4e986ef5a46 18.0 landed
-
psql: Add support for pipelines
- 41625ab8ea3d 18.0 landed
-
Add braces for if block with large comment in psql's common.c
- 40af897eb777 18.0 landed