Hi, I reported in other thread that PQhost() has three problems.
http://www.postgresql.org/message-id/cahgqgwe77akyabywde5+8bjldopthp7cnswao_syedjogyv...@mail.gmail.com ------------------------ (1) PQhost() can return Unix-domain socket directory path even in the platform that doesn't support Unix-domain socket. (2) In the platform that doesn't support Unix-domain socket, when neither host nor hostaddr are specified, the default host 'localhost' is used to connect to the server and PQhost() must return that, but it doesn't. (3) PQhost() cannot return the hostaddr. As the result of these problems, you can see the incorrect result of \conninfo, for example. $ psql -d "hostaddr=127.0.0.1" =# \conninfo You are connected to database "postgres" as user "postgres" via socket in "/tmp" at port "5432". Obviously "/tmp" should be "127.0.0.1". ------------------------ Attached patch fixes these problems. We can fix the problem (3) by changing PQhost() so that it also returns the hostaddr. But this change might break the existing application using PQhost(). So, I added new libpq function PQhostaddr() which returns the hostaddr, and changed \conninfo so that it reports correct connection information by using both PQhost() and PQhostaddr(). These problems exist in v9.3 or before. Basically we should backport the patch into those versions. But obviously it's too late to add new libpq function PQhostaddr() into those versions. Then, I'm thinking to backport only the change for (1) and (2) because we can fix them without adding new libpq function. BTW, I think that the patch also fixes the problem of \conninfo that MauMau reported in other thread. http://www.postgresql.org/message-id/8913B51B3B534F878D54B89E1EB964C6@maumau Regards, -- Fujii Masao
*** a/doc/src/sgml/libpq.sgml --- b/doc/src/sgml/libpq.sgml *************** *** 1464,1469 **** char *PQhost(const PGconn *conn); --- 1464,1487 ---- </listitem> </varlistentry> + <varlistentry id="libpq-pqhostaddr"> + <term> + <function>PQhostaddr</function> + <indexterm> + <primary>PQhostaddr</primary> + </indexterm> + </term> + + <listitem> + <para> + Returns the server numeric IP address or host name of the connection. + <synopsis> + char *PQhostaddr(const PGconn *conn); + </synopsis> + </para> + </listitem> + </varlistentry> + <varlistentry id="libpq-pqport"> <term> <function>PQport</function> *** a/src/bin/psql/command.c --- b/src/bin/psql/command.c *************** *** 300,306 **** exec_command(const char *cmd, else if (strcmp(cmd, "conninfo") == 0) { char *db = PQdb(pset.db); ! char *host = PQhost(pset.db); if (db == NULL) printf(_("You are currently not connected to a database.\n")); --- 300,306 ---- else if (strcmp(cmd, "conninfo") == 0) { char *db = PQdb(pset.db); ! char *host = (PQhostaddr(pset.db) != NULL) ? PQhostaddr(pset.db) : PQhost(pset.db); if (db == NULL) printf(_("You are currently not connected to a database.\n")); *** a/src/interfaces/libpq/exports.txt --- b/src/interfaces/libpq/exports.txt *************** *** 165,167 **** lo_lseek64 162 --- 165,168 ---- lo_tell64 163 lo_truncate64 164 PQconninfo 165 + PQhostaddr 166 *** a/src/interfaces/libpq/fe-connect.c --- b/src/interfaces/libpq/fe-connect.c *************** *** 5188,5194 **** PQhost(const PGconn *conn) { if (!conn) return NULL; ! return conn->pghost ? conn->pghost : conn->pgunixsocket; } char * --- 5188,5211 ---- { if (!conn) return NULL; ! if (conn->pghost != NULL && conn->pghost[0] != '\0') ! return conn->pghost; ! else ! { ! #ifdef HAVE_UNIX_SOCKETS ! return conn->pgunixsocket; ! #else ! return DefaultHost; ! #endif ! } ! } ! ! char * ! PQhostaddr(const PGconn *conn) ! { ! if (!conn) ! return NULL; ! return conn->pghostaddr; } char * *** a/src/interfaces/libpq/libpq-fe.h --- b/src/interfaces/libpq/libpq-fe.h *************** *** 301,306 **** extern char *PQdb(const PGconn *conn); --- 301,307 ---- extern char *PQuser(const PGconn *conn); extern char *PQpass(const PGconn *conn); extern char *PQhost(const PGconn *conn); + extern char *PQhostaddr(const PGconn *conn); extern char *PQport(const PGconn *conn); extern char *PQtty(const PGconn *conn); extern char *PQoptions(const PGconn *conn);
-- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers