From 000c74ff83bbc2486e49468bd2272710f3dfdab9 Mon Sep 17 00:00:00 2001
From: Hari Babu <kommi.haribabu@gmail.com>
Date: Sat, 24 Mar 2018 01:16:19 +1100
Subject: [PATCH] PQhost to return connected host and hostaddr details

Earlier PQhost doesn't return the connected host details
when the connection type is CHT_HOST_ADDRESS instead it
returns the provided connection host parameter or the default
host details, this can lead to confusion.

It is better to provide the host or hostaddr details of
the connected host irrespective of the connection type.
The Default host addresses details are also available
in the connhost structure, so no need of returning default
hosts.
---
 src/interfaces/libpq/fe-connect.c | 19 ++++++-------------
 1 file changed, 6 insertions(+), 13 deletions(-)

diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 39c19998c2..f61de76e60 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -6015,21 +6015,14 @@ PQpass(const PGconn *conn)
 char *
 PQhost(const PGconn *conn)
 {
-	if (!conn)
+	if (!conn || !conn->connhost)
 		return NULL;
-	if (conn->connhost != NULL &&
-		conn->connhost[conn->whichhost].type != CHT_HOST_ADDRESS)
+	if (conn->connhost[conn->whichhost].host != NULL &&
+		conn->connhost[conn->whichhost].host[0] != '\0')
 		return conn->connhost[conn->whichhost].host;
-	else if (conn->pghost != NULL && conn->pghost[0] != '\0')
-		return conn->pghost;
-	else
-	{
-#ifdef HAVE_UNIX_SOCKETS
-		return DEFAULT_PGSOCKET_DIR;
-#else
-		return DefaultHost;
-#endif
-	}
+	else if (conn->connhost[conn->whichhost].hostaddr != NULL &&
+			 conn->connhost[conn->whichhost].hostaddr[0] != '\0')
+		return conn->connhost[conn->whichhost].hostaddr;
 }
 
 char *
-- 
2.16.1.windows.4

