Skip to content

Commit 8175da6

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 35b4b7d commit 8175da6

File tree

2 files changed

+230
-86
lines changed

2 files changed

+230
-86
lines changed

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

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -872,34 +872,49 @@ testdb=>
872872
<term><literal>\c</literal> or <literal>\connect [ -reuse-previous=<replaceable class="parameter">on|off</replaceable> ] [ <replaceable class="parameter">dbname</replaceable> [ <replaceable class="parameter">username</replaceable> ] [ <replaceable class="parameter">host</replaceable> ] [ <replaceable class="parameter">port</replaceable> ] | <replaceable class="parameter">conninfo</replaceable> ]</literal></term>
873873
<listitem>
874874
<para>
875-
Establishes a new connection to a <productname>PostgreSQL</>
875+
Establishes a new connection to a <productname>PostgreSQL</productname>
876876
server. The connection parameters to use can be specified either
877-
using a positional syntax, or using <replaceable>conninfo</> connection
878-
strings as detailed in <xref linkend="libpq-connstring">.
877+
using a positional syntax (one or more of database name, user,
878+
host, and port), or using a <replaceable>conninfo</replaceable>
879+
connection string as detailed in
880+
<xref linkend="libpq-connstring">. If no arguments are given, a
881+
new connection is made using the same parameters as before.
879882
</para>
880883

881884
<para>
882-
Where the command omits database name, user, host, or port, the new
883-
connection can reuse values from the previous connection. By default,
884-
values from the previous connection are reused except when processing
885-
a <replaceable>conninfo</> string. Passing a first argument
886-
of <literal>-reuse-previous=on</>
887-
or <literal>-reuse-previous=off</literal> overrides that default.
888-
When the command neither specifies nor reuses a particular parameter,
889-
the <application>libpq</application> default is used. Specifying any
885+
Specifying any
890886
of <replaceable class="parameter">dbname</replaceable>,
891887
<replaceable class="parameter">username</replaceable>,
892888
<replaceable class="parameter">host</replaceable> or
893889
<replaceable class="parameter">port</replaceable>
894890
as <literal>-</literal> is equivalent to omitting that parameter.
895891
</para>
896892

893+
<para>
894+
The new connection can re-use connection parameters from the previous
895+
connection; not only database name, user, host, and port, but other
896+
settings such as <replaceable>sslmode</replaceable>. By default,
897+
parameters are re-used in the positional syntax, but not when
898+
a <replaceable>conninfo</replaceable> string is given. Passing a
899+
first argument of <literal>-reuse-previous=on</literal>
900+
or <literal>-reuse-previous=off</literal> overrides that default. If
901+
parameters are re-used, then any parameter not explicitly specified as
902+
a positional parameter or in the <replaceable>conninfo</replaceable>
903+
string is taken from the existing connection's parameters. An
904+
exception is that if the <replaceable>host</replaceable> setting
905+
is changed from its previous value using the positional syntax,
906+
any <replaceable>hostaddr</replaceable> setting present in the
907+
existing connection's parameters is dropped.
908+
When the command neither specifies nor reuses a particular parameter,
909+
the <application>libpq</application> default is used.
910+
</para>
911+
897912
<para>
898913
If the new connection is successfully made, the previous
899914
connection is closed.
900-
If the connection attempt failed (wrong user name, access
901-
denied, etc.), the previous connection will only be kept if
902-
<application>psql</application> is in interactive mode. When
915+
If the connection attempt fails (wrong user name, access
916+
denied, etc.), the previous connection will be kept if
917+
<application>psql</application> is in interactive mode. But when
903918
executing a non-interactive script, processing will
904919
immediately stop with an error. This distinction was chosen as
905920
a user convenience against typos on the one hand, and a safety
@@ -914,6 +929,7 @@ testdb=&gt;
914929
=&gt; \c mydb myuser host.dom 6432
915930
=&gt; \c service=foo
916931
=&gt; \c "host=localhost port=5432 dbname=mydb connect_timeout=10 sslmode=disable"
932+
=&gt; \c -reuse-previous=on sslmode=require -- changes only sslmode
917933
=&gt; \c postgresql://tom@localhost/mydb?application_name=myapp
918934
</programlisting>
919935
</listitem>

0 commit comments

Comments
 (0)