postgres.git / summary / log / commit / refs
commit fd9bb365db5b83249b97b788df4af4e7877fc1e0
Author: Fujii Masao <fujii@postgresql.org>
Date: Fri Aug 21 03:36:01 2026 +0000
First released in: 18.7, 19.0
psql: Do not let invalid \getresults affect the next query
In pipeline mode, an invalid \getresults argument could previously
affect the next SQL command in the same pipeline. For example, after
reporting an error for \getresults -1, psql could treat the following
SQL command as a request to read pending pipeline results instead of
sending it to the server, making the command appear to be skipped or
causing missing results.
This happened because psql marked \getresults as a request to read
pipeline results before validating its optional argument. When
validation failed, psql reported the error without running the normal
cleanup path that clears the request.
Fix this by validating the \getresults argument before marking the
command as a request to read pipeline results. After an invalid
argument, psql now reports the error and sends the following SQL command
normally.
Backpatch to v18, where psql pipeline meta-commands were introduced.
Author: Fujii Masao <masao.fujii@gmail.com>
Reviewed-by: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
Discussion: https://postgr.es/m/CAHGQGwGkgM2HKZeig5hobEgUCjn7MMJux4UCcN=OfOzOEvRT0A@mail.gmail.com
Backpatch-through: 18
src/bin/psql/command.c | 9 +++++----
src/test/regress/expected/psql_pipeline.out | 21 +++++++++++++++++++++
src/test/regress/sql/psql_pipeline.sql | 11 +++++++++++
3 files changed, 37 insertions(+), 4 deletions(-)
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 72023d111fc..b3d65ca5e31 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -1935,10 +1935,8 @@ exec_command_getresults(PsqlScanState scan_state, bool active_branch)
if (active_branch)
{
char *opt;
- int num_results;
+ int num_results = 0;
- 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;
@@ -1951,8 +1949,11 @@ exec_command_getresults(PsqlScanState scan_state, bool active_branch)
pg_log_error("\\getresults: invalid number of requested results");
return PSQL_CMD_ERROR;
}
- pset.requested_results = num_results;
}
+
+ pset.requested_results = num_results;
+ pset.send_mode = PSQL_SEND_GET_RESULTS;
+ status = PSQL_CMD_SEND;
}
else
ignore_slash_options(scan_state);
diff --git a/src/test/regress/expected/psql_pipeline.out b/src/test/regress/expected/psql_pipeline.out
index a931d63cafe..6fc5aea49d9 100644
--- a/src/test/regress/expected/psql_pipeline.out
+++ b/src/test/regress/expected/psql_pipeline.out
@@ -627,6 +627,27 @@ Pipeline aborted, command did not run
\startpipeline
\getresults -1
\getresults: invalid number of requested results
+\endpipeline
+-- After an invalid \getresults argument, the next SQL command in the
+-- pipeline should still be sent and returned normally.
+\startpipeline
+SELECT 1;
+\flushrequest
+\getresults -1
+\getresults: invalid number of requested results
+SELECT 99;
+\flushrequest
+\getresults
+ ?column?
+----------
+ 1
+(1 row)
+
+ ?column?
+----------
+ 99
+(1 row)
+
\endpipeline
-- \getresults when there is no result should not impact the next
-- query executed.
diff --git a/src/test/regress/sql/psql_pipeline.sql b/src/test/regress/sql/psql_pipeline.sql
index 468ef1d090b..b6bd917c90b 100644
--- a/src/test/regress/sql/psql_pipeline.sql
+++ b/src/test/regress/sql/psql_pipeline.sql
@@ -354,6 +354,17 @@ SELECT $1 \bind \sendpipeline
\getresults -1
\endpipeline
+-- After an invalid \getresults argument, the next SQL command in the
+-- pipeline should still be sent and returned normally.
+\startpipeline
+SELECT 1;
+\flushrequest
+\getresults -1
+SELECT 99;
+\flushrequest
+\getresults
+\endpipeline
+
-- \getresults when there is no result should not impact the next
-- query executed.
\getresults 1
[parent: 88d2c0e3a64c]