> On Mon, May 27, 2019 at 10:38 PM Noah Misch <noah@leadboat.com> wrote:
>
> On Mon, Nov 19, 2018 at 12:53:15PM -0300, Alvaro Herrera wrote:
> > commit 6e5f8d4
> > Commit: Alvaro Herrera <alvherre@alvh.no-ip.org>
> > CommitDate: Mon Nov 19 14:34:12 2018 -0300
> >
> > psql: Show IP address in \conninfo
>
> > Discussion: https://postgr.es/m/alpine.DEB.2.21.1810261532380.27686@lancre
> > https://postgr.es/m/alpine.DEB.2.21.1808201323020.13832@lancre
>
> > --- a/src/bin/psql/command.c
> > +++ b/src/bin/psql/command.c
> > @@ -2894,12 +2911,27 @@ do_connect(enum trivalue reuse_previous_specification,
> > }
> >
> > /* grab missing values from the old connection */
> > - if (!user && reuse_previous)
> > - user = PQuser(o_conn);
> > - if (!host && reuse_previous)
> > - host = PQhost(o_conn);
> > - if (!port && reuse_previous)
> > - port = PQport(o_conn);
> > + if (reuse_previous)
> > + {
> > + if (!user)
> > + user = PQuser(o_conn);
> > + if (host && strcmp(host, PQhost(o_conn)) == 0)
> > + {
> > + /*
> > + * if we are targetting the same host, reuse its hostaddr for
> > + * consistency
> > + */
> > + hostaddr = PQhostaddr(o_conn);
> > + }
> > + if (!host)
> > + {
> > + host = PQhost(o_conn);
> > + /* also set hostaddr for consistency */
> > + hostaddr = PQhostaddr(o_conn);
> > + }
> > + if (!port)
> > + port = PQport(o_conn);
> > + }
> >
> > /*
> > * Any change in the parameters read above makes us discard the password.
>
> The "hostaddr = PQhostaddr(o_conn)" branches make \connect use the same IP
> address as the existing connection. I like that when the existing connection
> uses a hostaddr= parameter, but I doubt it's the right thing otherwise. If
> the server IP changes, \connect should find the server at its new IP. If the
> server has multiple IPs, \connect should have the opportunity to try them all,
> just like the original connection attempt could have.
>
> Other than that, the \connect behavior change makes sense to me. However,
> nothing updated \connect documentation. (Even the commit log message said
> nothing about \connect.)
Given that it's an open item for PostgreSQL 12, I've decided to take a look.
Indeed, looks like 6e5f8d4 introduced a subtle behaviour change, when hostaddr
changes are not picked up for subsequent \connect's, and I don't see any
mentions of it in the documentation. Although I guess it can be avoided by
`-reuse-previous=off`, probably it makese sense to update the docs.