This is a follow-up to another patch I posted about libpq confusing documentation & psql resulting behavior under host/hostaddr settings.
Although the first mostly documentation patch did not gather much enthousiasm, I still think both issues deserve a fix.
About updating psql's behavior, without this patch:
sh> psql "host=foo hostaddr=127.0.0.1"
psql> \conninfo You are connected to database "fabien" as user "fabien" on host "foo" at port "5432". # NOPE, I'm really connected to localhost, foo does not even exist # Other apparent inconsistencies are possible when hostaddr overrides # "host" which is an socket directory or an IP.
psql> \c template1 could not translate host name "foo" to address: Name or service not known Previous connection kept # hmmm.... what is the meaning of reusing a connection? # this issue was pointed out by Arthur Zakirov
After the patch:
sh> psql "host=foo hostaddr=127.0.0.1"
psql> \conninfo You are connected to database "fabien" as user "fabien" on host "foo" (address "127.0.0.1") at port "5432". # better
psql> \c template1 You are now connected to database "template1" as user "fabien". # thanks
The patch adds a PQhostaddr() function to libpq which reports the "hostaddr" setting or the current server ip. The function is used by psql for \conninfo and when reusing parameters for \connect.
The messages are slightly more verbose because the IP is output. I think that user asking for conninfo should survive to the more precise data. This also comes handy if a host name resolves to several IPs (eg IPv6 and IPv4, or several IPs...).
I checked this patch, and it looks well. The documentation is correct, all tests passed. It does what is proposed.
I think so some redundant messages can be reduced - see function printConnInfo - attached patch
If there are no be a objection, I'll mark this patch as ready for commiters