From 6a20315b9d25ddc9f77b96d2e8318d9853b105eb Mon Sep 17 00:00:00 2001
From: Yugo Nagata <nagata@sraoss.co.jp>
Date: Tue, 11 Nov 2025 10:14:30 +0900
Subject: [PATCH] Make sure discardUntilSync() discards until the last sync
 point

---
 src/bin/pgbench/pgbench.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
index d8764ba6fe0..c31dd30672b 100644
--- a/src/bin/pgbench/pgbench.c
+++ b/src/bin/pgbench/pgbench.c
@@ -3563,14 +3563,14 @@ doRetry(CState *st, pg_time_usec_t *now)
 }
 
 /*
- * Read results and discard it until a sync point.
+ * Read and discard results until the last sync point.
  */
 static int
 discardUntilSync(CState *st)
 {
 	bool		received_sync = false;
 
-	/* send a sync */
+	/* Send a sync since all PGRES_PIPELINE_SYNC may be already received. */
 	if (!PQpipelineSync(st->con))
 	{
 		pg_log_error("client %d aborted: failed to send a pipeline sync",
@@ -3588,10 +3588,15 @@ discardUntilSync(CState *st)
 		else if (received_sync)
 		{
 			/*
-			 * PGRES_PIPELINE_SYNC must be followed by another
-			 * PGRES_PIPELINE_SYNC or NULL; otherwise, assert failure.
+			 * If a PGRES_PIPELINE_SYNC is followed by something other than
+			 * PGRES_PIPELINE_SYNC or NULL, another PGRES_PIPELINE_SYNC will
+			 * eventually follow.
 			 */
-			Assert(res == NULL);
+			if (res)
+			{
+				received_sync = false;
+				continue;
+			}
 
 			/*
 			 * Reset ongoing sync count to 0 since all PGRES_PIPELINE_SYNC
-- 
2.43.0

