fix psql \conninfo & \connect when using hostaddr - Mailing list pgsql-hackers

From Fabien COELHO
Subject fix psql \conninfo & \connect when using hostaddr
Date
Msg-id alpine.DEB.2.21.1810261532380.27686@lancre
Whole thread Raw
Responses Re: fix psql \conninfo & \connect when using hostaddr
Re: fix psql \conninfo & \connect when using hostaddr
Re: fix psql \conninfo & \connect when using hostaddr
List pgsql-hackers
Hello,

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...).

-- 
Fabien.
Attachment

pgsql-hackers by date:

Previous
From: Jean-Christophe Arnu
Date:
Subject: wal_dump output on CREATE DATABASE
Next
From: Tom Lane
Date:
Subject: Re: [PATCH] Tab complete EXECUTE FUNCTION for CREATE (EVENT) TRIGGER