Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit85c5428

Browse files
committed
Fix connection string handling in psql's \connect command.
psql's \connect claims to be able to re-use previous connectionparameters, 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 havere-use all other parameters. If such a script is loaded in a psql runthat initially had "-d connstring" with some non-default parameters,those other parameters would be lost, potentially causing connectionfailure. (Thus, this is the same kind of bug addressed in commitsa45bc8a and8e5793a, although the details are much different.)To fix, redesign do_connect() so that it pulls out all propertiesof the old PGconn using PQconninfo(), and then replaces individualproperties in that array. In the case where we don't wish to re-useanything, get libpq's default settings using PQconndefaults() andreplace entries in that, so that we don't need different code pathsfor the two cases.This does result in an additional behavioral change for cases wherethe original connection parameters allowed multiple hosts, say"psql -h host1,host2", and the \connect request allows re-use of thehost setting. Because the previous coding relied on PQhost(), itwould only permit reconnection to the same host originally selected.Although one can think of scenarios where that's a good thing, thereare others where it is not. Moreover, that behavior doesn't seem tomeet the principle of least surprise, nor was it documented; nor isit even clear it was intended, since that coding long pre-dates theaddition of multi-host support to libpq. Hence, this patch is contentto drop it and re-use the host list as given.Per Peter Eisentraut's comments on bug #16604. Back-patch to allsupported branches.Discussion:https://postgr.es/m/16604-933f4b8791227b15@postgresql.org
1 parent831611b commit85c5428

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
@@ -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 attemptfailed (wrong user name, access
919-
denied, etc.), the previous connection willonlybe kept if
920-
<application>psql</application> is in interactive mode.When
930+
If the connection attemptfails (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)

[8]ページ先頭

©2009-2025 Movatter.jp