From 3f5aa6788283ef08da9d4516f5dd6149ac8eb40d Mon Sep 17 00:00:00 2001
From: Hari Babu <kommi.haribabu@gmail.com>
Date: Mon, 26 Mar 2018 17:38:21 +1100
Subject: [PATCH] PQhost to return active 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 all connection host parameter values
or the default host details, this can lead to confusion.

It is better to provide the active host or hostaddr details of
the connected server host irrespective of the connection type.
when both host and hostaddr are specified in the connection
string, active host parameter value is returned.

PQhost function returns NULL when the connection is not established,
or returns an empty string when the connection status is not
CONNECTION_OK.

Similarly PQPort is also changed to return the active connection
port or NULL or an empty string.
---
 doc/src/sgml/libpq.sgml           | 23 +++++++++++++++++++++--
 src/interfaces/libpq/fe-connect.c | 33 +++++++++++++++++++++------------
 2 files changed, 42 insertions(+), 14 deletions(-)

diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml
index 1fd5dd9fca..853af2d4f0 100644
--- a/doc/src/sgml/libpq.sgml
+++ b/doc/src/sgml/libpq.sgml
@@ -1692,7 +1692,7 @@ char *PQpass(const PGconn *conn);
 
      <listitem>
       <para>
-       Returns the server host name of the connection.
+       Returns the server host name or host address of the active connection.
        This can be a host name, an IP address, or a directory path if the
        connection is via Unix socket.  (The path case can be distinguished
        because it will always be an absolute path, beginning
@@ -1701,6 +1701,20 @@ char *PQpass(const PGconn *conn);
 char *PQhost(const PGconn *conn);
 </synopsis>
       </para>
+
+      <para>
+       The <function>PQhost</function> function returns NULL when the
+       connection is not established, or returns an empty string when status
+       of the connection is not <literal>CONNECTION_OK</literal>.
+      </para>
+
+      <note>
+       <para>
+        when both <literal>host</literal> and <literal>hostaddr</literal>
+        parameters are specified in the connection string, the connection
+        <literal>host</literal> parameter is returned.
+       </para>
+      </note>
      </listitem>
     </varlistentry>
 
@@ -1714,11 +1728,16 @@ char *PQhost(const PGconn *conn);
 
      <listitem>
       <para>
-       Returns the port of the connection.
+       Returns the port of the active connection.
 
 <synopsis>
 char *PQport(const PGconn *conn);
 </synopsis>
+      </para>
+
+	  <para>
+       The <function>PQport</function> function returns NULL when the connection is not established,
+       or returns an empty string when status of the connection is not <literal>CONNECTION_OK</literal>.
       </para>
      </listitem>
     </varlistentry>
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 39c19998c2..5b593d5a2a 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -6017,18 +6017,25 @@ PQhost(const PGconn *conn)
 {
 	if (!conn)
 		return NULL;
-	if (conn->connhost != NULL &&
-		conn->connhost[conn->whichhost].type != CHT_HOST_ADDRESS)
+
+	if (!conn->connhost || conn->status != CONNECTION_OK)
+		return "";
+
+	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
+		/*
+		 * conn structure should have at least one "host" or "hostaddr"
+		 * defined.
+		 */
+		Assert(conn->connhost[conn->whichhost].hostaddr != NULL);
+		Assert(conn->connhost[conn->whichhost].hostaddr[0] != '\0');
+
+		return conn->connhost[conn->whichhost].hostaddr;
 	}
 }
 
@@ -6037,9 +6044,11 @@ PQport(const PGconn *conn)
 {
 	if (!conn)
 		return NULL;
-	if (conn->connhost != NULL)
-		return conn->connhost[conn->whichhost].port;
-	return conn->pgport;
+
+	if (!conn->connhost || conn->status != CONNECTION_OK)
+		return "";
+
+	return conn->connhost[conn->whichhost].port;
 }
 
 char *
-- 
2.16.1.windows.4

