From 1476c27256b5da1e823445ff649ff3b71ca70dc0 Mon Sep 17 00:00:00 2001
From: Jacob Champion <jacob.champion@enterprisedb.com>
Date: Fri, 21 Nov 2025 16:42:22 -0800
Subject: [PATCH v4 3/5] squash! Add test for libpq its default protocol
 version

- explain why we're setting max_protocol_version in-place now
- condense the logic so that we *always* set max_protocol_version
  in-place
---
 .../modules/libpq_pipeline/libpq_pipeline.c   | 35 ++++++++-----------
 1 file changed, 15 insertions(+), 20 deletions(-)

diff --git a/src/test/modules/libpq_pipeline/libpq_pipeline.c b/src/test/modules/libpq_pipeline/libpq_pipeline.c
index dd99fcb3667..b819bcc273c 100644
--- a/src/test/modules/libpq_pipeline/libpq_pipeline.c
+++ b/src/test/modules/libpq_pipeline/libpq_pipeline.c
@@ -1331,14 +1331,10 @@ test_protocol_version(PGconn *conn)
 	int			max_protocol_version_index = -1;
 	int			i;
 
-	/*
-	 * Prepare keywords/vals arrays, copied from the existing connection, with
-	 * an extra slot for 'max_protocol_version'.
-	 */
+	/* Prepare keywords/vals arrays, copied from the existing connection. */
 	nopts = 0;
 	for (PQconninfoOption *opt = opts; opt->keyword != NULL; ++opt)
 		nopts++;
-	nopts++;					/* max_protocol_version */
 	nopts++;					/* NULL terminator */
 
 	keywords = pg_malloc0(sizeof(char *) * nopts);
@@ -1347,26 +1343,25 @@ test_protocol_version(PGconn *conn)
 	i = 0;
 	for (PQconninfoOption *opt = opts; opt->keyword != NULL; ++opt)
 	{
-		if (opt->val)
-		{
-			keywords[i] = opt->keyword;
-			vals[i] = opt->val;
-			if (strcmp(opt->keyword, "max_protocol_version") == 0)
-			{
-				max_protocol_version_index = i;
-			}
+		/*
+		 * If the test already specified max_protocol_version, we want to
+		 * replace it rather than attempting to override it. This matters when
+		 * testing defaults, because empty option values at the end of the
+		 * connection string won't replace earlier settings.
+		 */
+		if (strcmp(opt->keyword, "max_protocol_version") == 0)
+			max_protocol_version_index = i;
+		else if (!opt->val)
+			continue;
 
-			i++;
-		}
-	}
+		keywords[i] = opt->keyword;
+		vals[i] = opt->val;
 
-	if (max_protocol_version_index == -1)
-	{
-		max_protocol_version_index = i;
-		keywords[i] = "max_protocol_version";	/* value is filled in below */
 		i++;
 	}
 
+	Assert(max_protocol_version_index >= 0);
+
 	/*
 	 * Test default protocol_version (GREASE - should negotiate down to 3.2)
 	 */
-- 
2.34.1

