From 30cd08bac78448afa18630b3c479b2630b4279ba Mon Sep 17 00:00:00 2001
From: Yugo Nagata <nagata@sraoss.co.jp>
Date: Fri, 26 Sep 2025 10:41:34 +0900
Subject: [PATCH v16 1/3] pgbench: Use PQresultErrorMessage() instead of
 PQerrorMessage()

Previously, readCommandResponse() used PQerrorMessage() to get the error
message after calling another PQgetResult() to peek at the next result
in order to determine whether the current one was the last.

This caused the error message to be lost in pipeline mode.
Although this issue has never been observed in non-pipeline mode,
referencing an error message using PQerrorMessage() after another
PQgetResult() call does not seem like a good idea in general.

Fix this by using PQresultErrorMessage() instead.
---
 src/bin/pgbench/pgbench.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
index cc03af05447..36c6469149e 100644
--- a/src/bin/pgbench/pgbench.c
+++ b/src/bin/pgbench/pgbench.c
@@ -3356,7 +3356,7 @@ readCommandResponse(CState *st, MetaCommand meta, char *varprefix)
 				st->num_syncs--;
 				if (st->num_syncs == 0 && PQexitPipelineMode(st->con) != 1)
 					pg_log_error("client %d failed to exit pipeline mode: %s", st->id,
-								 PQerrorMessage(st->con));
+								 PQresultErrorMessage(res));
 				break;
 
 			case PGRES_NONFATAL_ERROR:
@@ -3366,7 +3366,7 @@ readCommandResponse(CState *st, MetaCommand meta, char *varprefix)
 				if (canRetryError(st->estatus))
 				{
 					if (verbose_errors)
-						commandError(st, PQerrorMessage(st->con));
+						commandError(st, PQresultErrorMessage(res));
 					goto error;
 				}
 				/* fall through */
@@ -3375,7 +3375,7 @@ readCommandResponse(CState *st, MetaCommand meta, char *varprefix)
 				/* anything else is unexpected */
 				pg_log_error("client %d script %d aborted in command %d query %d: %s",
 							 st->id, st->use_file, st->command, qrynum,
-							 PQerrorMessage(st->con));
+							 PQresultErrorMessage(res));
 				goto error;
 		}
 
-- 
2.43.0

