Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f656517

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 a818000 commit f656517

File tree

2 files changed

+217
-126
lines changed

2 files changed

+217
-126
lines changed

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

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

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

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

0 commit comments

Comments
 (0)