Skip to content

Commit 85c5428

Browse files
committed
Fix connection string handling in psql's \connect command.
psql's \connect claims to be able to re-use previous connection parameters, but in fact it only re-uses the database name, user name, host name (and possibly hostaddr, depending on version), and port. This is problematic for assorted use cases. Notably, pg_dump[all] emits "\connect databasename" commands which we would like to have re-use all other parameters. If such a script is loaded in a psql run that initially had "-d connstring" with some non-default parameters, those other parameters would be lost, potentially causing connection failure. (Thus, this is the same kind of bug addressed in commits a45bc8a and 8e5793a, although the details are much different.) To fix, redesign do_connect() so that it pulls out all properties of the old PGconn using PQconninfo(), and then replaces individual properties in that array. In the case where we don't wish to re-use anything, get libpq's default settings using PQconndefaults() and replace entries in that, so that we don't need different code paths for the two cases. This does result in an additional behavioral change for cases where the original connection parameters allowed multiple hosts, say "psql -h host1,host2", and the \connect request allows re-use of the host setting. Because the previous coding relied on PQhost(), it would only permit reconnection to the same host originally selected. Although one can think of scenarios where that's a good thing, there are others where it is not. Moreover, that behavior doesn't seem to meet the principle of least surprise, nor was it documented; nor is it even clear it was intended, since that coding long pre-dates the addition of multi-host support to libpq. Hence, this patch is content to drop it and re-use the host list as given. Per Peter Eisentraut's comments on bug #16604. Back-patch to all supported branches. Discussion: https://postgr.es/m/16604-933f4b8791227b15@postgresql.org
1 parent 831611b commit 85c5428

File tree

2 files changed

+217
-126
lines changed

2 files changed

+217
-126
lines changed

doc/src/sgml/ref/psql-ref.sgml

+29-16
Original file line numberDiff line numberDiff line change
@@ -889,35 +889,47 @@ testdb=>
889889
<para>
890890
Establishes a new connection to a <productname>PostgreSQL</productname>
891891
server. The connection parameters to use can be specified either
892-
using a positional syntax, or using <replaceable>conninfo</replaceable> connection
893-
strings as detailed in <xref linkend="libpq-connstring"/>.
892+
using a positional syntax (one or more of database name, user,
893+
host, and port), or using a <replaceable>conninfo</replaceable>
894+
connection string as detailed in
895+
<xref linkend="libpq-connstring"/>. If no arguments are given, a
896+
new connection is made using the same parameters as before.
894897
</para>
895898

896899
<para>
897-
Where the command omits database name, user, host, or port, the new
898-
connection can reuse values from the previous connection. By default,
899-
values from the previous connection are reused except when processing
900-
a <replaceable>conninfo</replaceable> string. Passing a first argument
901-
of <literal>-reuse-previous=on</literal>
902-
or <literal>-reuse-previous=off</literal> overrides that default.
903-
When the command neither specifies nor reuses a particular parameter,
904-
the <application>libpq</application> default is used. Specifying any
900+
Specifying any
905901
of <replaceable class="parameter">dbname</replaceable>,
906902
<replaceable class="parameter">username</replaceable>,
907903
<replaceable class="parameter">host</replaceable> or
908904
<replaceable class="parameter">port</replaceable>
909905
as <literal>-</literal> is equivalent to omitting that parameter.
910-
If <literal>hostaddr</literal> was specified in the original
911-
connection's <structname>conninfo</structname>, that address is reused
912-
for the new connection (disregarding any other host specification).
906+
</para>
907+
908+
<para>
909+
The new connection can re-use connection parameters from the previous
910+
connection; not only database name, user, host, and port, but other
911+
settings such as <replaceable>sslmode</replaceable>. By default,
912+
parameters are re-used in the positional syntax, but not when
913+
a <replaceable>conninfo</replaceable> string is given. Passing a
914+
first argument of <literal>-reuse-previous=on</literal>
915+
or <literal>-reuse-previous=off</literal> overrides that default. If
916+
parameters are re-used, then any parameter not explicitly specified as
917+
a positional parameter or in the <replaceable>conninfo</replaceable>
918+
string is taken from the existing connection's parameters. An
919+
exception is that if the <replaceable>host</replaceable> setting
920+
is changed from its previous value using the positional syntax,
921+
any <replaceable>hostaddr</replaceable> setting present in the
922+
existing connection's parameters is dropped.
923+
When the command neither specifies nor reuses a particular parameter,
924+
the <application>libpq</application> default is used.
913925
</para>
914926

915927
<para>
916928
If the new connection is successfully made, the previous
917929
connection is closed.
918-
If the connection attempt failed (wrong user name, access
919-
denied, etc.), the previous connection will only be kept if
920-
<application>psql</application> is in interactive mode. When
930+
If the connection attempt fails (wrong user name, access
931+
denied, etc.), the previous connection will be kept if
932+
<application>psql</application> is in interactive mode. But when
921933
executing a non-interactive script, processing will
922934
immediately stop with an error. This distinction was chosen as
923935
a user convenience against typos on the one hand, and a safety
@@ -932,6 +944,7 @@ testdb=&gt;
932944
=&gt; \c mydb myuser host.dom 6432
933945
=&gt; \c service=foo
934946
=&gt; \c "host=localhost port=5432 dbname=mydb connect_timeout=10 sslmode=disable"
947+
=&gt; \c -reuse-previous=on sslmode=require -- changes only sslmode
935948
=&gt; \c postgresql://tom@localhost/mydb?application_name=myapp
936949
</programlisting>
937950
</listitem>

0 commit comments

Comments
 (0)