From 4d3d56f19dd4673daa7510cfd0d463802f9d77f2 Mon Sep 17 00:00:00 2001
From: Robert Haas <rhaas@postgresql.org>
Date: Fri, 27 Oct 2017 14:21:16 +0200
Subject: [PATCH 2/2] libpq test framework

---
 src/interfaces/libpq/fe-connect.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)

diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 6bcf60a712..2c859d7617 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -1798,11 +1798,11 @@ connectDBStart(PGconn *conn)
 #endif
 
 	/*
-	 * Set up to try to connect, with protocol 3.0 as the first attempt.
+	 * Set up to try to connect, with protocol 3.1 as the first attempt.
 	 */
 	conn->whichhost = 0;
 	conn->addr_cur = conn->connhost[0].addrlist;
-	conn->pversion = PG_PROTOCOL(3, 0);
+	conn->pversion = PG_PROTOCOL(3, 1);
 	conn->send_appname = true;
 	conn->status = CONNECTION_NEEDED;
 
@@ -2653,13 +2653,13 @@ keep_going:						/* We will come back to here until there is
 				 * request or an error here.  Anything else probably means
 				 * it's not Postgres on the other end at all.
 				 */
-				if (!(beresp == 'R' || beresp == 'E'))
+				if (!(beresp == 'R' || beresp == 'E' || beresp == 'v'))
 				{
 					appendPQExpBuffer(&conn->errorMessage,
 									  libpq_gettext(
 													"expected authentication request from "
-													"server, but received %c\n"),
-									  beresp);
+													"server, but received %d\n"),
+									  (int) beresp);
 					goto error_return;
 				}
 
@@ -2813,6 +2813,34 @@ keep_going:						/* We will come back to here until there is
 					goto error_return;
 				}
 
+				/*
+				 * Parse and discard any NegotiateProtocolVersion message.
+				 */
+				if (beresp == 'v')
+				{
+					int			server_max_version;
+					int			num_unrecognized_parameters;
+
+					if (pqGetInt(&server_max_version, 4, conn))
+						return PGRES_POLLING_READING;
+					if (pqGetInt(&num_unrecognized_parameters, 4, conn))
+						return PGRES_POLLING_READING;
+					if (num_unrecognized_parameters > 0)
+					{
+						PQExpBufferData buf;
+						int			i;
+
+						initPQExpBuffer(&buf);
+						for (i = 0; i < num_unrecognized_parameters; ++i)
+							(void) pqGets(&buf, conn);
+						termPQExpBuffer(&buf);
+					}
+
+					conn->inStart = conn->inCursor;
+
+					goto keep_going;
+				}
+
 				/* It is an authentication request. */
 				conn->auth_req_received = true;
 
-- 
2.13.5 (Apple Git-94)

