fix psql \conninfo & \connect when using hostaddr

From: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>
To: PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Arthur Zakirov <a(dot)zakirov(at)postgrespro(dot)ru>, Robert Haas <robertmhaas(at)gmail(dot)com>
Subject: fix psql \conninfo & \connect when using hostaddr
Date: 2018-10-26 13:54:51
Message-ID: alpine.DEB.2.21.1810261532380.27686@lancre
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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 Content-Type Size
libpq-conn-1.patch text/x-diff 11.1 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Alvaro Herrera 2018-10-26 14:09:27 Re: Should pg 11 use a lot more memory building an spgist index?
Previous Message Jean-Christophe Arnu 2018-10-26 13:53:01 wal_dump output on CREATE DATABASE