diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml
index 1fd5dd9fca6..943adfef774 100644
--- a/doc/src/sgml/libpq.sgml
+++ b/doc/src/sgml/libpq.sgml
@@ -1692,7 +1692,7 @@ char *PQpass(const PGconn *conn);
- Returns the server host name of the connection.
+ Returns the server host name 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,30 @@ char *PQpass(const PGconn *conn);
char *PQhost(const PGconn *conn);
+
+
+ If the connection parameters specified both host and
+ hostaddr, then PQhost will
+ return the host information. If only
+ hostaddr was specified, then that is returned.
+ If multiple hosts were specified in the connection parameters,
+ PQhost returns the host actually connected to.
+
+
+
+ PQhost returns NULL if the
+ conn argument is NULL.
+ Otherwise, if there is an error producing the host information (perhaps
+ if the connection has not been fully established or there was an
+ error), it returns an empty string.
+
+
+
+ If multiple hosts were specified in the connection parameters, it is
+ not possible to rely on the result of PQhost until
+ the connection is established. The status of the connection can be
+ checked using the function PQstatus.
+
@@ -1714,12 +1738,32 @@ char *PQhost(const PGconn *conn);
- Returns the port of the connection.
+ Returns the port of the active connection.
char *PQport(const PGconn *conn);
+
+
+ If multiple ports were specified in the connection parameters,
+ PQport returns the port actually connected to.
+
+
+
+ PQport returns NULL if the
+ conn argument is NULL.
+ Otherwise, if there is an error producing the port information (perhaps
+ if the connection has not been fully established or there was an
+ error), it returns an empty string.
+
+
+
+ If multiple ports were specified in the connection parameters, it is
+ not possible to rely on the result of PQport until
+ the connection is established. The status of the connection can be
+ checked using the function PQstatus.
+
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 39c19998c22..f3057e9d6f5 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -6017,19 +6017,18 @@ PQhost(const PGconn *conn)
{
if (!conn)
return NULL;
- if (conn->connhost != NULL &&
- conn->connhost[conn->whichhost].type != CHT_HOST_ADDRESS)
- return conn->connhost[conn->whichhost].host;
- else if (conn->pghost != NULL && conn->pghost[0] != '\0')
- return conn->pghost;
- else
+
+ if (conn->connhost != NULL)
{
-#ifdef HAVE_UNIX_SOCKETS
- return DEFAULT_PGSOCKET_DIR;
-#else
- return DefaultHost;
-#endif
+ if (conn->connhost[conn->whichhost].host != NULL &&
+ conn->connhost[conn->whichhost].host[0] != '\0')
+ return conn->connhost[conn->whichhost].host;
+ else if (conn->connhost[conn->whichhost].hostaddr != NULL &&
+ conn->connhost[conn->whichhost].hostaddr[0] != '\0')
+ return conn->connhost[conn->whichhost].hostaddr;
}
+
+ return "";
}
char *
@@ -6037,9 +6036,11 @@ PQport(const PGconn *conn)
{
if (!conn)
return NULL;
+
if (conn->connhost != NULL)
return conn->connhost[conn->whichhost].port;
- return conn->pgport;
+
+ return "";
}
char *