Re: BUG #16604: pg_dump with --jobs breaks SSL connections - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #16604: pg_dump with --jobs breaks SSL connections
Date
Msg-id 145080.1602975503@sss.pgh.pa.us
Whole thread Raw
In response to Re: BUG #16604: pg_dump with --jobs breaks SSL connections  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-bugs
I wrote:
> A bit of refactoring later, I have the attached, which seems to
> clean up all cases.  I got rid of some bits of dead code along
> the way.

I pushed that at commit a45bc8a4f.  However...

In some off-list discussion, Peter Eisentraut pointed out that
pg_dump/pg_restore are not the only programs with this problem.

Several of the src/bin/scripts/ programs, such as vacuumdb
and reindexdb, perform reconnections when told to use parallel
processing.  These reconnections use methods similar to parallel
pg_restore and have the same bug.  We can fix this straightforwardly
using much the same methods as in a45bc8a4f, as per 0001 below.
(0001 also fixes the documentation oversight that we didn't say that
these programs' --maintenance-db option could be a connstring.)

pg_dumpall, as well as "pg_dump -C" with text output, have a
variant of the issue: they emit psql scripts containing commands
such as "\connect databasename", and psql's \connect processing
isn't very good about re-using all connection options either.
In fact, it'll *only* re-use username, host name (and hostaddr if
given), and port.  So if you do "psql -d '...some connection options'
and then try to run a dump script, the reconnections will fail
if there were other critical options in the -d connstring, much
like what was reported here.  0002 below is a draft attempt at
addressing this, but it's probably more controversial than 0001,
because there's a larger behavioral change involved.

An important thing to understand is that except for the \connect
case, all these programs perform a reconnection by re-using the
textual parameters given originally.  Thus for example, if you do
"vacuumdb -j 4 -h host1,host2 ...", it's unclear which of host1 and
host2 will be connected to, either by the initial connection or any
of the parallel worker processes.  I don't regard that as a bug
particularly.  Perhaps some users would wish that once vacuumdb
made an initial connection, the workers would guarantee to reconnect
to that same host; but ISTM the real answer is "don't use that kind
of host spec for this purpose".  (Note that there are other ways
to shoot yourself in the foot here, for example if your DNS can
resolve several different IPs for the same server host name.  All
these cases seem more like pilot error than things for us to fix.)

\connect, however, is way out in left field.  In the first place,
while these other programs are just interested in re-using all the
connection parameters except possibly the database name, \connect
intends to allow substitution of new values for some but not
necessarily all of the parameters.  In the second place, \connect
makes some effort to force reconnection to occur to the very same
server if you don't specify a new host.  Rather than re-using the
original parameter strings, it pulls out PQhost() and PQport()
values from the previous connection, and also PQhostaddr() if you
specified hostaddr previously.  These values will reflect just the
server actually connected to, not the multi-host values you might
have given originally.

I'm inclined to think that this second behavior is simply wrong,
or at least a failure to adhere to the principle of least surprise.
An example of why it might be thought buggy is

postgres=# \connect "dbname=postgres host=sss1,sss1 hostaddr=127.0.0.1,::1"
You are now connected to database "postgres" as user "postgres".
postgres=# \connect -reuse-previous=on "dbname=postgres host=sss1,sss1"
could not match 2 host names to 1 hostaddr values
Previous connection kept

This happens because what -reuse-previous injects is not the previous
hostaddr connection parameter value ("127.0.0.1,::1"), but only the
actual connection address ("127.0.0.1"), resulting in a surprising
mismatch.

Moreover, psql isn't even being self-consistent by doing this.
If you get a connection loss, it will invoke PQreset() which just
re-uses the connection parameters previously given, and therefore
is perfectly content to connect to any of the hosts you named before.
It's quite unclear why \connect should be pickier than that case.

Therefore, my proposal in 0002 below is to drop all that logic.
The patch's method for re-using connection parameters is to fetch
the parameters of the old connection with PQconninfo(), which will
provide the textual values for each parameter slot, and then just
overwrite the specific slots that you provide new values for.
This seems to me to have considerably less surprise factor than
what happens now.  But there's no denying that it changes behavior
in multi-host cases to a greater degree than is minimally necessary
to fix the bug complained of here.

There are some things adjacent to this that I'd kind of like to fix.
For example, I noted that createuser and dropuser lack any way to use
a connstring, as they have neither -d nor --maintenance-db options.
That seems pretty bogus, but it also seems like a missing feature not
a bug, so I didn't do anything about it here.  (To be clear, I'm
proposing these changes for back-patch, as a45bc8a4f was.)

Comments?

            regards, tom lane

diff --git a/doc/src/sgml/ref/clusterdb.sgml b/doc/src/sgml/ref/clusterdb.sgml
index c53bacf864..c838b22c44 100644
--- a/doc/src/sgml/ref/clusterdb.sgml
+++ b/doc/src/sgml/ref/clusterdb.sgml
@@ -90,9 +90,9 @@ PostgreSQL documentation
       <term><option><optional>--dbname=</optional><replaceable class="parameter">dbname</replaceable></option></term>
       <listitem>
        <para>
-        Specifies the name of the database to be clustered.
-        If this is not specified and <option>-a</option> (or
-        <option>--all</option>) is not used, the database name is read
+        Specifies the name of the database to be clustered,
+        when <option>-a</option>/<option>--all</option> is not used.
+        If this is not specified, the database name is read
         from the environment variable <envar>PGDATABASE</envar>.  If
         that is not set, the user name specified for the connection is
         used.  The <replaceable>dbname</replaceable> can be a <link
@@ -249,10 +249,16 @@ PostgreSQL documentation
       <term><option>--maintenance-db=<replaceable class="parameter">dbname</replaceable></option></term>
       <listitem>
        <para>
-         Specifies the name of the database to connect to discover what other
-         databases should be clustered. If not specified, the
-         <literal>postgres</literal> database will be used,
-         and if that does not exist, <literal>template1</literal> will be used.
+        Specifies the name of the database to connect to to discover which
+        databases should be clustered,
+        when <option>-a</option>/<option>--all</option> is used.
+        If not specified, the <literal>postgres</literal> database will be used,
+        or if that does not exist, <literal>template1</literal> will be used.
+        This can be a <link linkend="libpq-connstring">connection
+        string</link>.  If so, connection string parameters will override any
+        conflicting command line options.  Also, connection string parameters
+        other than the database name itself will be re-used when connecting
+        to other databases.
        </para>
       </listitem>
      </varlistentry>
diff --git a/doc/src/sgml/ref/createdb.sgml b/doc/src/sgml/ref/createdb.sgml
index 95cc82dc88..86473455c9 100644
--- a/doc/src/sgml/ref/createdb.sgml
+++ b/doc/src/sgml/ref/createdb.sgml
@@ -284,6 +284,9 @@ PostgreSQL documentation
          database will be used; if that does not exist (or if it is the name
          of the new database being created), <literal>template1</literal> will
          be used.
+         This can be a <link linkend="libpq-connstring">connection
+         string</link>.  If so, connection string parameters will override any
+         conflicting command line options.
        </para>
       </listitem>
      </varlistentry>
diff --git a/doc/src/sgml/ref/dropdb.sgml b/doc/src/sgml/ref/dropdb.sgml
index fe523a2ee1..d36aed38c5 100644
--- a/doc/src/sgml/ref/dropdb.sgml
+++ b/doc/src/sgml/ref/dropdb.sgml
@@ -217,6 +217,9 @@ PostgreSQL documentation
          target database. If not specified, the <literal>postgres</literal>
          database will be used; if that does not exist (or is the database
          being dropped), <literal>template1</literal> will be used.
+         This can be a <link linkend="libpq-connstring">connection
+         string</link>.  If so, connection string parameters will override any
+         conflicting command line options.
        </para>
       </listitem>
      </varlistentry>
diff --git a/doc/src/sgml/ref/reindexdb.sgml b/doc/src/sgml/ref/reindexdb.sgml
index a6d93693c5..5741445333 100644
--- a/doc/src/sgml/ref/reindexdb.sgml
+++ b/doc/src/sgml/ref/reindexdb.sgml
@@ -134,9 +134,9 @@ PostgreSQL documentation
       <term><option><optional>--dbname=</optional><replaceable class="parameter">dbname</replaceable></option></term>
       <listitem>
        <para>
-        Specifies the name of the database to be reindexed.
-        If this is not specified and <option>-a</option> (or
-        <option>--all</option>) is not used, the database name is read
+        Specifies the name of the database to be reindexed,
+        when <option>-a</option>/<option>--all</option> is not used.
+        If this is not specified, the database name is read
         from the environment variable <envar>PGDATABASE</envar>.  If
         that is not set, the user name specified for the connection is
         used.  The <replaceable>dbname</replaceable> can be a <link
@@ -351,10 +351,16 @@ PostgreSQL documentation
       <term><option>--maintenance-db=<replaceable class="parameter">dbname</replaceable></option></term>
       <listitem>
        <para>
-         Specifies the name of the database to connect to discover what other
-         databases should be reindexed. If not specified, the
-         <literal>postgres</literal> database will be used,
-         and if that does not exist, <literal>template1</literal> will be used.
+        Specifies the name of the database to connect to to discover which
+        databases should be reindexed,
+        when <option>-a</option>/<option>--all</option> is used.
+        If not specified, the <literal>postgres</literal> database will be used,
+        or if that does not exist, <literal>template1</literal> will be used.
+        This can be a <link linkend="libpq-connstring">connection
+        string</link>.  If so, connection string parameters will override any
+        conflicting command line options.  Also, connection string parameters
+        other than the database name itself will be re-used when connecting
+        to other databases.
        </para>
       </listitem>
      </varlistentry>
diff --git a/doc/src/sgml/ref/vacuumdb.sgml b/doc/src/sgml/ref/vacuumdb.sgml
index 6dcdab9caf..a90fc9322f 100644
--- a/doc/src/sgml/ref/vacuumdb.sgml
+++ b/doc/src/sgml/ref/vacuumdb.sgml
@@ -92,9 +92,9 @@ PostgreSQL documentation
       <term><option><optional>--dbname=</optional><replaceable class="parameter">dbname</replaceable></option></term>
       <listitem>
        <para>
-        Specifies the name of the database to be cleaned or analyzed.
-        If this is not specified and <option>-a</option> (or
-        <option>--all</option>) is not used, the database name is read
+        Specifies the name of the database to be cleaned or analyzed,
+        when <option>-a</option>/<option>--all</option> is not used.
+        If this is not specified, the database name is read
         from the environment variable <envar>PGDATABASE</envar>.  If
         that is not set, the user name specified for the connection is
         used.  The <replaceable>dbname</replaceable> can be a <link
@@ -474,10 +474,16 @@ PostgreSQL documentation
       <term><option>--maintenance-db=<replaceable class="parameter">dbname</replaceable></option></term>
       <listitem>
        <para>
-         Specifies the name of the database to connect to discover what other
-         databases should be vacuumed. If not specified, the
-         <literal>postgres</literal> database will be used,
-         and if that does not exist, <literal>template1</literal> will be used.
+        Specifies the name of the database to connect to to discover which
+        databases should be vacuumed,
+        when <option>-a</option>/<option>--all</option> is used.
+        If not specified, the <literal>postgres</literal> database will be used,
+        or if that does not exist, <literal>template1</literal> will be used.
+        This can be a <link linkend="libpq-connstring">connection
+        string</link>.  If so, connection string parameters will override any
+        conflicting command line options.  Also, connection string parameters
+        other than the database name itself will be re-used when connecting
+        to other databases.
        </para>
       </listitem>
      </varlistentry>
diff --git a/src/bin/scripts/clusterdb.c b/src/bin/scripts/clusterdb.c
index 12972de0e9..2f786e6103 100644
--- a/src/bin/scripts/clusterdb.c
+++ b/src/bin/scripts/clusterdb.c
@@ -17,15 +17,10 @@
 #include "fe_utils/string_utils.h"


-static void cluster_one_database(const char *dbname, bool verbose, const char *table,
-                                 const char *host, const char *port,
-                                 const char *username, enum trivalue prompt_password,
-                                 const char *progname, bool echo);
-static void cluster_all_databases(bool verbose, const char *maintenance_db,
-                                  const char *host, const char *port,
-                                  const char *username, enum trivalue prompt_password,
-                                  const char *progname, bool echo, bool quiet);
-
+static void cluster_one_database(const ConnParams *cparams, const char *table,
+                                 const char *progname, bool verbose, bool echo);
+static void cluster_all_databases(ConnParams *cparams, const char *progname,
+                                  bool verbose, bool echo, bool quiet);
 static void help(const char *progname);


@@ -58,6 +53,7 @@ main(int argc, char *argv[])
     char       *port = NULL;
     char       *username = NULL;
     enum trivalue prompt_password = TRI_DEFAULT;
+    ConnParams    cparams;
     bool        echo = false;
     bool        quiet = false;
     bool        alldb = false;
@@ -134,6 +130,13 @@ main(int argc, char *argv[])
         exit(1);
     }

+    /* fill cparams except for dbname, which is set below */
+    cparams.pghost = host;
+    cparams.pgport = port;
+    cparams.pguser = username;
+    cparams.prompt_password = prompt_password;
+    cparams.override_dbname = NULL;
+
     setup_cancel_handler(NULL);

     if (alldb)
@@ -150,8 +153,9 @@ main(int argc, char *argv[])
             exit(1);
         }

-        cluster_all_databases(verbose, maintenance_db, host, port, username, prompt_password,
-                              progname, echo, quiet);
+        cparams.dbname = maintenance_db;
+
+        cluster_all_databases(&cparams, progname, verbose, echo, quiet);
     }
     else
     {
@@ -165,21 +169,21 @@ main(int argc, char *argv[])
                 dbname = get_user_name_or_exit(progname);
         }

+        cparams.dbname = dbname;
+
         if (tables.head != NULL)
         {
             SimpleStringListCell *cell;

             for (cell = tables.head; cell; cell = cell->next)
             {
-                cluster_one_database(dbname, verbose, cell->val,
-                                     host, port, username, prompt_password,
-                                     progname, echo);
+                cluster_one_database(&cparams, cell->val,
+                                     progname, verbose, echo);
             }
         }
         else
-            cluster_one_database(dbname, verbose, NULL,
-                                 host, port, username, prompt_password,
-                                 progname, echo);
+            cluster_one_database(&cparams, NULL,
+                                 progname, verbose, echo);
     }

     exit(0);
@@ -187,17 +191,14 @@ main(int argc, char *argv[])


 static void
-cluster_one_database(const char *dbname, bool verbose, const char *table,
-                     const char *host, const char *port,
-                     const char *username, enum trivalue prompt_password,
-                     const char *progname, bool echo)
+cluster_one_database(const ConnParams *cparams, const char *table,
+                     const char *progname, bool verbose, bool echo)
 {
     PQExpBufferData sql;

     PGconn       *conn;

-    conn = connectDatabase(dbname, host, port, username, prompt_password,
-                           progname, echo, false, false);
+    conn = connectDatabase(cparams, progname, echo, false, false);

     initPQExpBuffer(&sql);

@@ -228,22 +229,17 @@ cluster_one_database(const char *dbname, bool verbose, const char *table,


 static void
-cluster_all_databases(bool verbose, const char *maintenance_db,
-                      const char *host, const char *port,
-                      const char *username, enum trivalue prompt_password,
-                      const char *progname, bool echo, bool quiet)
+cluster_all_databases(ConnParams *cparams, const char *progname,
+                      bool verbose, bool echo, bool quiet)
 {
     PGconn       *conn;
     PGresult   *result;
-    PQExpBufferData connstr;
     int            i;

-    conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
-                                      prompt_password, progname, echo);
+    conn = connectMaintenanceDatabase(cparams, progname, echo);
     result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", echo);
     PQfinish(conn);

-    initPQExpBuffer(&connstr);
     for (i = 0; i < PQntuples(result); i++)
     {
         char       *dbname = PQgetvalue(result, i, 0);
@@ -254,15 +250,10 @@ cluster_all_databases(bool verbose, const char *maintenance_db,
             fflush(stdout);
         }

-        resetPQExpBuffer(&connstr);
-        appendPQExpBufferStr(&connstr, "dbname=");
-        appendConnStrVal(&connstr, dbname);
+        cparams->override_dbname = dbname;

-        cluster_one_database(connstr.data, verbose, NULL,
-                             host, port, username, prompt_password,
-                             progname, echo);
+        cluster_one_database(cparams, NULL, progname, verbose, echo);
     }
-    termPQExpBuffer(&connstr);

     PQclear(result);
 }
diff --git a/src/bin/scripts/common.c b/src/bin/scripts/common.c
index e987eef234..3362221a31 100644
--- a/src/bin/scripts/common.c
+++ b/src/bin/scripts/common.c
@@ -54,7 +54,7 @@ handle_help_version_opts(int argc, char *argv[],
  * Make a database connection with the given parameters.
  *
  * An interactive password prompt is automatically issued if needed and
- * allowed by prompt_password.
+ * allowed by cparams->prompt_password.
  *
  * If allow_password_reuse is true, we will try to re-use any password
  * given during previous calls to this routine.  (Callers should not pass
@@ -62,22 +62,23 @@ handle_help_version_opts(int argc, char *argv[],
  * as before, else we might create password exposure hazards.)
  */
 PGconn *
-connectDatabase(const char *dbname, const char *pghost,
-                const char *pgport, const char *pguser,
-                enum trivalue prompt_password, const char *progname,
+connectDatabase(const ConnParams *cparams, const char *progname,
                 bool echo, bool fail_ok, bool allow_password_reuse)
 {
     PGconn       *conn;
     bool        new_pass;
     static char *password = NULL;

+    /* Callers must supply at least dbname; other params can be NULL */
+    Assert(cparams->dbname);
+
     if (!allow_password_reuse && password)
     {
         free(password);
         password = NULL;
     }

-    if (!password && prompt_password == TRI_YES)
+    if (cparams->prompt_password == TRI_YES && password == NULL)
         password = simple_prompt("Password: ", false);

     /*
@@ -86,23 +87,35 @@ connectDatabase(const char *dbname, const char *pghost,
      */
     do
     {
-        const char *keywords[7];
-        const char *values[7];
-
-        keywords[0] = "host";
-        values[0] = pghost;
-        keywords[1] = "port";
-        values[1] = pgport;
-        keywords[2] = "user";
-        values[2] = pguser;
-        keywords[3] = "password";
-        values[3] = password;
-        keywords[4] = "dbname";
-        values[4] = dbname;
-        keywords[5] = "fallback_application_name";
-        values[5] = progname;
-        keywords[6] = NULL;
-        values[6] = NULL;
+        const char *keywords[8];
+        const char *values[8];
+        int            i = 0;
+
+        /*
+         * If dbname is a connstring, its entries can override the other
+         * values obtained from cparams; but in turn, override_dbname can
+         * override the dbname component of it.
+         */
+        keywords[i] = "host";
+        values[i++] = cparams->pghost;
+        keywords[i] = "port";
+        values[i++] = cparams->pgport;
+        keywords[i] = "user";
+        values[i++] = cparams->pguser;
+        keywords[i] = "password";
+        values[i++] = password;
+        keywords[i] = "dbname";
+        values[i++] = cparams->dbname;
+        if (cparams->override_dbname)
+        {
+            keywords[i] = "dbname";
+            values[i++] = cparams->override_dbname;
+        }
+        keywords[i] = "fallback_application_name";
+        values[i++] = progname;
+        keywords[i] = NULL;
+        values[i++] = NULL;
+        Assert(i <= lengthof(keywords));

         new_pass = false;
         conn = PQconnectdbParams(keywords, values, true);
@@ -110,7 +123,7 @@ connectDatabase(const char *dbname, const char *pghost,
         if (!conn)
         {
             pg_log_error("could not connect to database %s: out of memory",
-                         dbname);
+                         cparams->dbname);
             exit(1);
         }

@@ -119,7 +132,7 @@ connectDatabase(const char *dbname, const char *pghost,
          */
         if (PQstatus(conn) == CONNECTION_BAD &&
             PQconnectionNeedsPassword(conn) &&
-            prompt_password != TRI_NO)
+            cparams->prompt_password != TRI_NO)
         {
             PQfinish(conn);
             if (password)
@@ -138,10 +151,11 @@ connectDatabase(const char *dbname, const char *pghost,
             return NULL;
         }
         pg_log_error("could not connect to database %s: %s",
-                     dbname, PQerrorMessage(conn));
+                     cparams->dbname, PQerrorMessage(conn));
         exit(1);
     }

+    /* Start strict; callers may override this. */
     PQclear(executeQuery(conn, ALWAYS_SECURE_SEARCH_PATH_SQL, echo));

     return conn;
@@ -149,27 +163,30 @@ connectDatabase(const char *dbname, const char *pghost,

 /*
  * Try to connect to the appropriate maintenance database.
+ *
+ * This differs from connectDatabase only in that it has a rule for
+ * inserting a default "dbname" if none was given (which is why cparams
+ * is not const).  Note that cparams->dbname should typically come from
+ * a --maintenance-db command line parameter.
  */
 PGconn *
-connectMaintenanceDatabase(const char *maintenance_db,
-                           const char *pghost, const char *pgport,
-                           const char *pguser, enum trivalue prompt_password,
+connectMaintenanceDatabase(ConnParams *cparams,
                            const char *progname, bool echo)
 {
     PGconn       *conn;

     /* If a maintenance database name was specified, just connect to it. */
-    if (maintenance_db)
-        return connectDatabase(maintenance_db, pghost, pgport, pguser,
-                               prompt_password, progname, echo, false, false);
+    if (cparams->dbname)
+        return connectDatabase(cparams, progname, echo, false, false);

     /* Otherwise, try postgres first and then template1. */
-    conn = connectDatabase("postgres", pghost, pgport, pguser, prompt_password,
-                           progname, echo, true, false);
+    cparams->dbname = "postgres";
+    conn = connectDatabase(cparams, progname, echo, true, false);
     if (!conn)
-        conn = connectDatabase("template1", pghost, pgport, pguser,
-                               prompt_password, progname, echo, false, false);
-
+    {
+        cparams->dbname = "template1";
+        conn = connectDatabase(cparams, progname, echo, false, false);
+    }
     return conn;
 }

diff --git a/src/bin/scripts/common.h b/src/bin/scripts/common.h
index ddf6320b47..9ec57cdd87 100644
--- a/src/bin/scripts/common.h
+++ b/src/bin/scripts/common.h
@@ -21,20 +21,32 @@ enum trivalue
     TRI_YES
 };

+/* Parameters needed by connectDatabase/connectMaintenanceDatabase */
+typedef struct _connParams
+{
+    /* These fields record the actual command line parameters */
+    const char *dbname;            /* this may be a connstring! */
+    const char *pghost;
+    const char *pgport;
+    const char *pguser;
+    enum trivalue prompt_password;
+    /* If not NULL, this overrides the dbname obtained from command line */
+    /* (but *only* the DB name, not anything else in the connstring) */
+    const char *override_dbname;
+} ConnParams;
+
 typedef void (*help_handler) (const char *progname);

 extern void handle_help_version_opts(int argc, char *argv[],
                                      const char *fixed_progname,
                                      help_handler hlp);

-extern PGconn *connectDatabase(const char *dbname, const char *pghost,
-                               const char *pgport, const char *pguser,
-                               enum trivalue prompt_password, const char *progname,
-                               bool echo, bool fail_ok, bool allow_password_reuse);
+extern PGconn *connectDatabase(const ConnParams *cparams,
+                               const char *progname,
+                               bool echo, bool fail_ok,
+                               bool allow_password_reuse);

-extern PGconn *connectMaintenanceDatabase(const char *maintenance_db,
-                                          const char *pghost, const char *pgport,
-                                          const char *pguser, enum trivalue prompt_password,
+extern PGconn *connectMaintenanceDatabase(ConnParams *cparams,
                                           const char *progname, bool echo);

 extern void disconnectDatabase(PGconn *conn);
diff --git a/src/bin/scripts/createdb.c b/src/bin/scripts/createdb.c
index 1353af97c4..91e6e2194b 100644
--- a/src/bin/scripts/createdb.c
+++ b/src/bin/scripts/createdb.c
@@ -51,6 +51,7 @@ main(int argc, char *argv[])
     char       *port = NULL;
     char       *username = NULL;
     enum trivalue prompt_password = TRI_DEFAULT;
+    ConnParams    cparams;
     bool        echo = false;
     char       *owner = NULL;
     char       *tablespace = NULL;
@@ -180,8 +181,14 @@ main(int argc, char *argv[])
     if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
         maintenance_db = "template1";

-    conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
-                                      prompt_password, progname, echo);
+    cparams.dbname = maintenance_db;
+    cparams.pghost = host;
+    cparams.pgport = port;
+    cparams.pguser = username;
+    cparams.prompt_password = prompt_password;
+    cparams.override_dbname = NULL;
+
+    conn = connectMaintenanceDatabase(&cparams, progname, echo);

     initPQExpBuffer(&sql);

diff --git a/src/bin/scripts/createuser.c b/src/bin/scripts/createuser.c
index 6179199563..d6b56f15c3 100644
--- a/src/bin/scripts/createuser.c
+++ b/src/bin/scripts/createuser.c
@@ -59,6 +59,7 @@ main(int argc, char *argv[])
     char       *username = NULL;
     SimpleStringList roles = {NULL, NULL};
     enum trivalue prompt_password = TRI_DEFAULT;
+    ConnParams    cparams;
     bool        echo = false;
     bool        interactive = false;
     int            conn_limit = -2;    /* less than minimum valid value */
@@ -252,8 +253,14 @@ main(int argc, char *argv[])
     if (login == 0)
         login = TRI_YES;

-    conn = connectDatabase("postgres", host, port, username, prompt_password,
-                           progname, echo, false, false);
+    cparams.dbname = NULL;        /* this program lacks any dbname option... */
+    cparams.pghost = host;
+    cparams.pgport = port;
+    cparams.pguser = username;
+    cparams.prompt_password = prompt_password;
+    cparams.override_dbname = NULL;
+
+    conn = connectMaintenanceDatabase(&cparams, progname, echo);

     initPQExpBuffer(&sql);

diff --git a/src/bin/scripts/dropdb.c b/src/bin/scripts/dropdb.c
index 581c7749c8..ccbf78e91a 100644
--- a/src/bin/scripts/dropdb.c
+++ b/src/bin/scripts/dropdb.c
@@ -48,6 +48,7 @@ main(int argc, char *argv[])
     char       *port = NULL;
     char       *username = NULL;
     enum trivalue prompt_password = TRI_DEFAULT;
+    ConnParams    cparams;
     bool        echo = false;
     bool        interactive = false;
     bool        force = false;
@@ -137,9 +138,14 @@ main(int argc, char *argv[])
     if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
         maintenance_db = "template1";

-    conn = connectMaintenanceDatabase(maintenance_db,
-                                      host, port, username, prompt_password,
-                                      progname, echo);
+    cparams.dbname = maintenance_db;
+    cparams.pghost = host;
+    cparams.pgport = port;
+    cparams.pguser = username;
+    cparams.prompt_password = prompt_password;
+    cparams.override_dbname = NULL;
+
+    conn = connectMaintenanceDatabase(&cparams, progname, echo);

     if (echo)
         printf("%s\n", sql.data);
diff --git a/src/bin/scripts/dropuser.c b/src/bin/scripts/dropuser.c
index f7ddd1402d..73d7328a88 100644
--- a/src/bin/scripts/dropuser.c
+++ b/src/bin/scripts/dropuser.c
@@ -46,6 +46,7 @@ main(int argc, char *argv[])
     char       *port = NULL;
     char       *username = NULL;
     enum trivalue prompt_password = TRI_DEFAULT;
+    ConnParams    cparams;
     bool        echo = false;
     bool        interactive = false;

@@ -129,13 +130,19 @@ main(int argc, char *argv[])
             exit(0);
     }

+    cparams.dbname = NULL;        /* this program lacks any dbname option... */
+    cparams.pghost = host;
+    cparams.pgport = port;
+    cparams.pguser = username;
+    cparams.prompt_password = prompt_password;
+    cparams.override_dbname = NULL;
+
+    conn = connectMaintenanceDatabase(&cparams, progname, echo);
+
     initPQExpBuffer(&sql);
     appendPQExpBuffer(&sql, "DROP ROLE %s%s;",
                       (if_exists ? "IF EXISTS " : ""), fmtId(dropuser));

-    conn = connectDatabase("postgres", host, port, username, prompt_password,
-                           progname, echo, false, false);
-
     if (echo)
         printf("%s\n", sql.data);
     result = PQexec(conn, sql.data);
diff --git a/src/bin/scripts/reindexdb.c b/src/bin/scripts/reindexdb.c
index 40dcbc9283..40003380ae 100644
--- a/src/bin/scripts/reindexdb.c
+++ b/src/bin/scripts/reindexdb.c
@@ -34,15 +34,12 @@ static SimpleStringList *get_parallel_object_list(PGconn *conn,
                                                   ReindexType type,
                                                   SimpleStringList *user_list,
                                                   bool echo);
-static void reindex_one_database(const char *dbname, ReindexType type,
-                                 SimpleStringList *user_list, const char *host,
-                                 const char *port, const char *username,
-                                 enum trivalue prompt_password, const char *progname,
+static void reindex_one_database(const ConnParams *cparams, ReindexType type,
+                                 SimpleStringList *user_list,
+                                 const char *progname,
                                  bool echo, bool verbose, bool concurrently,
                                  int concurrentCons);
-static void reindex_all_databases(const char *maintenance_db,
-                                  const char *host, const char *port,
-                                  const char *username, enum trivalue prompt_password,
+static void reindex_all_databases(ConnParams *cparams,
                                   const char *progname, bool echo,
                                   bool quiet, bool verbose, bool concurrently,
                                   int concurrentCons);
@@ -86,6 +83,7 @@ main(int argc, char *argv[])
     const char *port = NULL;
     const char *username = NULL;
     enum trivalue prompt_password = TRI_DEFAULT;
+    ConnParams    cparams;
     bool        syscatalog = false;
     bool        alldb = false;
     bool        echo = false;
@@ -188,6 +186,13 @@ main(int argc, char *argv[])
         exit(1);
     }

+    /* fill cparams except for dbname, which is set below */
+    cparams.pghost = host;
+    cparams.pgport = port;
+    cparams.pguser = username;
+    cparams.prompt_password = prompt_password;
+    cparams.override_dbname = NULL;
+
     setup_cancel_handler(NULL);

     if (alldb)
@@ -218,8 +223,9 @@ main(int argc, char *argv[])
             exit(1);
         }

-        reindex_all_databases(maintenance_db, host, port, username,
-                              prompt_password, progname, echo, quiet, verbose,
+        cparams.dbname = maintenance_db;
+
+        reindex_all_databases(&cparams, progname, echo, quiet, verbose,
                               concurrently, concurrentCons);
     }
     else if (syscatalog)
@@ -256,9 +262,11 @@ main(int argc, char *argv[])
                 dbname = get_user_name_or_exit(progname);
         }

-        reindex_one_database(dbname, REINDEX_SYSTEM, NULL, host,
-                             port, username, prompt_password, progname,
-                             echo, verbose, concurrently, 1);
+        cparams.dbname = dbname;
+
+        reindex_one_database(&cparams, REINDEX_SYSTEM, NULL,
+                             progname, echo, verbose,
+                             concurrently, 1);
     }
     else
     {
@@ -283,40 +291,40 @@ main(int argc, char *argv[])
                 dbname = get_user_name_or_exit(progname);
         }

+        cparams.dbname = dbname;
+
         if (schemas.head != NULL)
-            reindex_one_database(dbname, REINDEX_SCHEMA, &schemas, host,
-                                 port, username, prompt_password, progname,
-                                 echo, verbose, concurrently, concurrentCons);
+            reindex_one_database(&cparams, REINDEX_SCHEMA, &schemas,
+                                 progname, echo, verbose,
+                                 concurrently, concurrentCons);

         if (indexes.head != NULL)
-            reindex_one_database(dbname, REINDEX_INDEX, &indexes, host,
-                                 port, username, prompt_password, progname,
-                                 echo, verbose, concurrently, 1);
+            reindex_one_database(&cparams, REINDEX_INDEX, &indexes,
+                                 progname, echo, verbose,
+                                 concurrently, 1);

         if (tables.head != NULL)
-            reindex_one_database(dbname, REINDEX_TABLE, &tables, host,
-                                 port, username, prompt_password, progname,
-                                 echo, verbose, concurrently,
-                                 concurrentCons);
+            reindex_one_database(&cparams, REINDEX_TABLE, &tables,
+                                 progname, echo, verbose,
+                                 concurrently, concurrentCons);

         /*
          * reindex database only if neither index nor table nor schema is
          * specified
          */
         if (indexes.head == NULL && tables.head == NULL && schemas.head == NULL)
-            reindex_one_database(dbname, REINDEX_DATABASE, NULL, host,
-                                 port, username, prompt_password, progname,
-                                 echo, verbose, concurrently, concurrentCons);
+            reindex_one_database(&cparams, REINDEX_DATABASE, NULL,
+                                 progname, echo, verbose,
+                                 concurrently, concurrentCons);
     }

     exit(0);
 }

 static void
-reindex_one_database(const char *dbname, ReindexType type,
-                     SimpleStringList *user_list, const char *host,
-                     const char *port, const char *username,
-                     enum trivalue prompt_password, const char *progname, bool echo,
+reindex_one_database(const ConnParams *cparams, ReindexType type,
+                     SimpleStringList *user_list,
+                     const char *progname, bool echo,
                      bool verbose, bool concurrently, int concurrentCons)
 {
     PGconn       *conn;
@@ -328,8 +336,7 @@ reindex_one_database(const char *dbname, ReindexType type,
     bool        failed = false;
     int            items_count = 0;

-    conn = connectDatabase(dbname, host, port, username, prompt_password,
-                           progname, echo, false, false);
+    conn = connectDatabase(cparams, progname, echo, false, false);

     if (concurrently && PQserverVersion(conn) < 120000)
     {
@@ -436,8 +443,7 @@ reindex_one_database(const char *dbname, ReindexType type,

     Assert(process_list != NULL);

-    slots = ParallelSlotsSetup(dbname, host, port, username, prompt_password,
-                               progname, echo, conn, concurrentCons);
+    slots = ParallelSlotsSetup(cparams, progname, echo, conn, concurrentCons);

     cell = process_list->head;
     do
@@ -705,23 +711,18 @@ get_parallel_object_list(PGconn *conn, ReindexType type,
 }

 static void
-reindex_all_databases(const char *maintenance_db,
-                      const char *host, const char *port,
-                      const char *username, enum trivalue prompt_password,
+reindex_all_databases(ConnParams *cparams,
                       const char *progname, bool echo, bool quiet, bool verbose,
                       bool concurrently, int concurrentCons)
 {
     PGconn       *conn;
     PGresult   *result;
-    PQExpBufferData connstr;
     int            i;

-    conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
-                                      prompt_password, progname, echo);
+    conn = connectMaintenanceDatabase(cparams, progname, echo);
     result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", echo);
     PQfinish(conn);

-    initPQExpBuffer(&connstr);
     for (i = 0; i < PQntuples(result); i++)
     {
         char       *dbname = PQgetvalue(result, i, 0);
@@ -732,16 +733,12 @@ reindex_all_databases(const char *maintenance_db,
             fflush(stdout);
         }

-        resetPQExpBuffer(&connstr);
-        appendPQExpBufferStr(&connstr, "dbname=");
-        appendConnStrVal(&connstr, dbname);
+        cparams->override_dbname = dbname;

-        reindex_one_database(connstr.data, REINDEX_DATABASE, NULL, host,
-                             port, username, prompt_password,
+        reindex_one_database(cparams, REINDEX_DATABASE, NULL,
                              progname, echo, verbose, concurrently,
                              concurrentCons);
     }
-    termPQExpBuffer(&connstr);

     PQclear(result);
 }
diff --git a/src/bin/scripts/scripts_parallel.c b/src/bin/scripts/scripts_parallel.c
index 01bc6dfeff..ec264a269a 100644
--- a/src/bin/scripts/scripts_parallel.c
+++ b/src/bin/scripts/scripts_parallel.c
@@ -205,8 +205,7 @@ ParallelSlotsGetIdle(ParallelSlot *slots, int numslots)
  * set.
  */
 ParallelSlot *
-ParallelSlotsSetup(const char *dbname, const char *host, const char *port,
-                   const char *username, bool prompt_password,
+ParallelSlotsSetup(const ConnParams *cparams,
                    const char *progname, bool echo,
                    PGconn *conn, int numslots)
 {
@@ -221,8 +220,7 @@ ParallelSlotsSetup(const char *dbname, const char *host, const char *port,
     {
         for (i = 1; i < numslots; i++)
         {
-            conn = connectDatabase(dbname, host, port, username, prompt_password,
-                                   progname, echo, false, true);
+            conn = connectDatabase(cparams, progname, echo, false, true);

             /*
              * Fail and exit immediately if trying to use a socket in an
diff --git a/src/bin/scripts/scripts_parallel.h b/src/bin/scripts/scripts_parallel.h
index cf20449ce3..c9d9f0623e 100644
--- a/src/bin/scripts/scripts_parallel.h
+++ b/src/bin/scripts/scripts_parallel.h
@@ -12,6 +12,7 @@
 #ifndef SCRIPTS_PARALLEL_H
 #define SCRIPTS_PARALLEL_H

+#include "common.h"
 #include "libpq-fe.h"


@@ -23,10 +24,7 @@ typedef struct ParallelSlot

 extern ParallelSlot *ParallelSlotsGetIdle(ParallelSlot *slots, int numslots);

-extern ParallelSlot *ParallelSlotsSetup(const char *dbname, const char *host,
-                                        const char *port,
-                                        const char *username,
-                                        bool prompt_password,
+extern ParallelSlot *ParallelSlotsSetup(const ConnParams *cparams,
                                         const char *progname, bool echo,
                                         PGconn *conn, int numslots);

diff --git a/src/bin/scripts/vacuumdb.c b/src/bin/scripts/vacuumdb.c
index 2a1247a1b0..8c2eade1d5 100644
--- a/src/bin/scripts/vacuumdb.c
+++ b/src/bin/scripts/vacuumdb.c
@@ -42,19 +42,16 @@ typedef struct vacuumingOptions
 } vacuumingOptions;


-static void vacuum_one_database(const char *dbname, vacuumingOptions *vacopts,
+static void vacuum_one_database(const ConnParams *cparams,
+                                vacuumingOptions *vacopts,
                                 int stage,
                                 SimpleStringList *tables,
-                                const char *host, const char *port,
-                                const char *username, enum trivalue prompt_password,
                                 int concurrentCons,
                                 const char *progname, bool echo, bool quiet);

-static void vacuum_all_databases(vacuumingOptions *vacopts,
+static void vacuum_all_databases(ConnParams *cparams,
+                                 vacuumingOptions *vacopts,
                                  bool analyze_in_stages,
-                                 const char *maintenance_db,
-                                 const char *host, const char *port,
-                                 const char *username, enum trivalue prompt_password,
                                  int concurrentCons,
                                  const char *progname, bool echo, bool quiet);

@@ -112,6 +109,7 @@ main(int argc, char *argv[])
     char       *port = NULL;
     char       *username = NULL;
     enum trivalue prompt_password = TRI_DEFAULT;
+    ConnParams    cparams;
     bool        echo = false;
     bool        quiet = false;
     vacuumingOptions vacopts;
@@ -311,6 +309,13 @@ main(int argc, char *argv[])
         }
     }

+    /* fill cparams except for dbname, which is set below */
+    cparams.pghost = host;
+    cparams.pgport = port;
+    cparams.pguser = username;
+    cparams.prompt_password = prompt_password;
+    cparams.override_dbname = NULL;
+
     setup_cancel_handler(NULL);

     /* Avoid opening extra connections. */
@@ -330,10 +335,10 @@ main(int argc, char *argv[])
             exit(1);
         }

-        vacuum_all_databases(&vacopts,
+        cparams.dbname = maintenance_db;
+
+        vacuum_all_databases(&cparams, &vacopts,
                              analyze_in_stages,
-                             maintenance_db,
-                             host, port, username, prompt_password,
                              concurrentCons,
                              progname, echo, quiet);
     }
@@ -349,25 +354,25 @@ main(int argc, char *argv[])
                 dbname = get_user_name_or_exit(progname);
         }

+        cparams.dbname = dbname;
+
         if (analyze_in_stages)
         {
             int            stage;

             for (stage = 0; stage < ANALYZE_NUM_STAGES; stage++)
             {
-                vacuum_one_database(dbname, &vacopts,
+                vacuum_one_database(&cparams, &vacopts,
                                     stage,
                                     &tables,
-                                    host, port, username, prompt_password,
                                     concurrentCons,
                                     progname, echo, quiet);
             }
         }
         else
-            vacuum_one_database(dbname, &vacopts,
+            vacuum_one_database(&cparams, &vacopts,
                                 ANALYZE_NO_STAGE,
                                 &tables,
-                                host, port, username, prompt_password,
                                 concurrentCons,
                                 progname, echo, quiet);
     }
@@ -389,11 +394,10 @@ main(int argc, char *argv[])
  * a list of tables from the database.
  */
 static void
-vacuum_one_database(const char *dbname, vacuumingOptions *vacopts,
+vacuum_one_database(const ConnParams *cparams,
+                    vacuumingOptions *vacopts,
                     int stage,
                     SimpleStringList *tables,
-                    const char *host, const char *port,
-                    const char *username, enum trivalue prompt_password,
                     int concurrentCons,
                     const char *progname, bool echo, bool quiet)
 {
@@ -424,8 +428,7 @@ vacuum_one_database(const char *dbname, vacuumingOptions *vacopts,
     Assert(stage == ANALYZE_NO_STAGE ||
            (stage >= 0 && stage < ANALYZE_NUM_STAGES));

-    conn = connectDatabase(dbname, host, port, username, prompt_password,
-                           progname, echo, false, true);
+    conn = connectDatabase(cparams, progname, echo, false, true);

     if (vacopts->disable_page_skipping && PQserverVersion(conn) < 90600)
     {
@@ -663,8 +666,7 @@ vacuum_one_database(const char *dbname, vacuumingOptions *vacopts,
      * for the first slot.  If not in parallel mode, the first slot in the
      * array contains the connection.
      */
-    slots = ParallelSlotsSetup(dbname, host, port, username, prompt_password,
-                               progname, echo, conn, concurrentCons);
+    slots = ParallelSlotsSetup(cparams, progname, echo, conn, concurrentCons);

     /*
      * Prepare all the connections to run the appropriate analyze stage, if
@@ -736,28 +738,23 @@ finish:
  * quickly everywhere before generating more detailed ones.
  */
 static void
-vacuum_all_databases(vacuumingOptions *vacopts,
+vacuum_all_databases(ConnParams *cparams,
+                     vacuumingOptions *vacopts,
                      bool analyze_in_stages,
-                     const char *maintenance_db, const char *host,
-                     const char *port, const char *username,
-                     enum trivalue prompt_password,
                      int concurrentCons,
                      const char *progname, bool echo, bool quiet)
 {
     PGconn       *conn;
     PGresult   *result;
-    PQExpBufferData connstr;
     int            stage;
     int            i;

-    conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
-                                      prompt_password, progname, echo);
+    conn = connectMaintenanceDatabase(cparams, progname, echo);
     result = executeQuery(conn,
                           "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;",
                           echo);
     PQfinish(conn);

-    initPQExpBuffer(&connstr);
     if (analyze_in_stages)
     {
         /*
@@ -772,14 +769,11 @@ vacuum_all_databases(vacuumingOptions *vacopts,
         {
             for (i = 0; i < PQntuples(result); i++)
             {
-                resetPQExpBuffer(&connstr);
-                appendPQExpBufferStr(&connstr, "dbname=");
-                appendConnStrVal(&connstr, PQgetvalue(result, i, 0));
+                cparams->override_dbname = PQgetvalue(result, i, 0);

-                vacuum_one_database(connstr.data, vacopts,
+                vacuum_one_database(cparams, vacopts,
                                     stage,
                                     NULL,
-                                    host, port, username, prompt_password,
                                     concurrentCons,
                                     progname, echo, quiet);
             }
@@ -789,19 +783,15 @@ vacuum_all_databases(vacuumingOptions *vacopts,
     {
         for (i = 0; i < PQntuples(result); i++)
         {
-            resetPQExpBuffer(&connstr);
-            appendPQExpBufferStr(&connstr, "dbname=");
-            appendConnStrVal(&connstr, PQgetvalue(result, i, 0));
+            cparams->override_dbname = PQgetvalue(result, i, 0);

-            vacuum_one_database(connstr.data, vacopts,
+            vacuum_one_database(cparams, vacopts,
                                 ANALYZE_NO_STAGE,
                                 NULL,
-                                host, port, username, prompt_password,
                                 concurrentCons,
                                 progname, echo, quiet);
         }
     }
-    termPQExpBuffer(&connstr);

     PQclear(result);
 }
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index ee3fc09577..77eae07106 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -889,27 +889,39 @@ testdb=>
         <para>
         Establishes a new connection to a <productname>PostgreSQL</productname>
         server.  The connection parameters to use can be specified either
-        using a positional syntax, or using <replaceable>conninfo</replaceable> connection
-        strings as detailed in <xref linkend="libpq-connstring"/>.
+        using a positional syntax (one or more of database name, user,
+        host, and port), or using a <replaceable>conninfo</replaceable>
+        connection string as detailed in
+        <xref linkend="libpq-connstring"/>.  If no arguments are given, a
+        new connection is made using the same parameters as before.
         </para>

         <para>
-        Where the command omits database name, user, host, or port, the new
-        connection can reuse values from the previous connection.  By default,
-        values from the previous connection are reused except when processing
-        a <replaceable>conninfo</replaceable> string.  Passing a first argument
-        of <literal>-reuse-previous=on</literal>
-        or <literal>-reuse-previous=off</literal> overrides that default.
-        When the command neither specifies nor reuses a particular parameter,
-        the <application>libpq</application> default is used.  Specifying any
+        Specifying any
         of <replaceable class="parameter">dbname</replaceable>,
         <replaceable class="parameter">username</replaceable>,
         <replaceable class="parameter">host</replaceable> or
         <replaceable class="parameter">port</replaceable>
         as <literal>-</literal> is equivalent to omitting that parameter.
-        If <literal>hostaddr</literal> was specified in the original
-        connection's <structname>conninfo</structname>, that address is reused
-        for the new connection (disregarding any other host specification).
+        </para>
+
+        <para>
+        The new connection can re-use connection parameters from the previous
+        connection; not only database name, user, host, and port, but other
+        settings such as <replaceable>sslmode</replaceable>.  By default,
+        parameters are re-used in the positional syntax, but not when
+        a <replaceable>conninfo</replaceable> string is given.  Passing a
+        first argument of <literal>-reuse-previous=on</literal>
+        or <literal>-reuse-previous=off</literal> overrides that default.  If
+        parameters are re-used, then any parameter not explicitly specified as
+        a positional parameter or in the <replaceable>conninfo</replaceable>
+        string is taken from the existing connection's parameters.  An
+        exception is that if the <replaceable>host</replaceable> setting
+        is changed from its previous value using the positional syntax,
+        any <replaceable>hostaddr</replaceable> setting present in the
+        existing connection's parameters is dropped.
+        When the command neither specifies nor reuses a particular parameter,
+        the <application>libpq</application> default is used.
         </para>

         <para>
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index d4aa0976b5..ac272d0588 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -3011,26 +3011,6 @@ param_is_newly_set(const char *old_val, const char *new_val)
     return false;
 }

-/* return whether the connection has 'hostaddr' in its conninfo */
-static bool
-has_hostaddr(PGconn *conn)
-{
-    bool        used = false;
-    PQconninfoOption *ciopt = PQconninfo(conn);
-
-    for (PQconninfoOption *p = ciopt; p->keyword != NULL; p++)
-    {
-        if (strcmp(p->keyword, "hostaddr") == 0 && p->val != NULL)
-        {
-            used = true;
-            break;
-        }
-    }
-
-    PQconninfoFree(ciopt);
-    return used;
-}
-
 /*
  * do_connect -- handler for \connect
  *
@@ -3048,13 +3028,15 @@ do_connect(enum trivalue reuse_previous_specification,
            char *dbname, char *user, char *host, char *port)
 {
     PGconn       *o_conn = pset.db,
-               *n_conn;
+               *n_conn = NULL;
+    PQconninfoOption *cinfo;
+    int            nconnopts = 0;
+    bool        same_host = false;
     char       *password = NULL;
-    char       *hostaddr = NULL;
-    bool        keep_password;
+    bool        success = true;
+    bool        keep_password = true;
     bool        has_connection_string;
     bool        reuse_previous;
-    PQExpBufferData connstr;

     if (!o_conn && (!dbname || !user || !host || !port))
     {
@@ -3096,55 +3078,125 @@ do_connect(enum trivalue reuse_previous_specification,
     }

     /*
-     * Grab missing values from the old connection.  If we grab host (or host
-     * is the same as before) and hostaddr was set, grab that too.
+     * If we intend to re-use connection parameters, collect them out of the
+     * old connection, then replace individual values as necessary. Otherwise,
+     * obtain a PQconninfoOption array containing libpq's defaults, and modify
+     * that.  Note this function assumes that PQconninfo, PQconndefaults, and
+     * PQconninfoParse will all produce arrays containing the same options in
+     * the same order.
      */
     if (reuse_previous)
+        cinfo = PQconninfo(o_conn);
+    else
+        cinfo = PQconndefaults();
+
+    if (cinfo)
     {
-        if (!user)
-            user = PQuser(o_conn);
-        if (host && strcmp(host, PQhost(o_conn)) == 0 &&
-            has_hostaddr(o_conn))
+        if (has_connection_string)
         {
-            hostaddr = PQhostaddr(o_conn);
+            /* Parse the connstring and insert values into cinfo */
+            PQconninfoOption *replcinfo;
+            char       *errmsg;
+
+            replcinfo = PQconninfoParse(dbname, &errmsg);
+            if (replcinfo)
+            {
+                PQconninfoOption *ci;
+                PQconninfoOption *replci;
+
+                for (ci = cinfo, replci = replcinfo;
+                     ci->keyword && replci->keyword;
+                     ci++, replci++)
+                {
+                    Assert(strcmp(ci->keyword, replci->keyword) == 0);
+                    /* Insert value from connstring if one was provided */
+                    if (replci->val)
+                    {
+                        /*
+                         * We know that both val strings were allocated by
+                         * libpq, so the least messy way to avoid memory leaks
+                         * is to swap them.
+                         */
+                        char       *swap = replci->val;
+
+                        replci->val = ci->val;
+                        ci->val = swap;
+                    }
+                }
+                Assert(ci->keyword == NULL && replci->keyword == NULL);
+
+                /* While here, determine how many option slots there are */
+                nconnopts = ci - cinfo;
+
+                PQconninfoFree(replcinfo);
+
+                /* We never re-use a password with a conninfo string. */
+                keep_password = false;
+
+                /* Don't let code below try to inject dbname into params. */
+                dbname = NULL;
+            }
+            else
+            {
+                /* PQconninfoParse failed */
+                if (errmsg)
+                {
+                    pg_log_error("%s", errmsg);
+                    PQfreemem(errmsg);
+                }
+                else
+                    pg_log_error("out of memory");
+                success = false;
+            }
         }
-        if (!host)
+        else
         {
-            host = PQhost(o_conn);
-            if (has_hostaddr(o_conn))
-                hostaddr = PQhostaddr(o_conn);
+            /*
+             * If dbname isn't a connection string, then we'll inject it and
+             * the other parameters into the keyword array below.  (We can't
+             * easily insert them into the cinfo array because of memory
+             * management issues: PQconninfoFree would misbehave on Windows.)
+             * However, to avoid dependencies on the order in which parameters
+             * appear in the array, make a preliminary scan to set
+             * keep_password and same_host correctly.
+             *
+             * While any change in user, host, or port causes us to ignore the
+             * old connection's password, we don't force that for dbname,
+             * since passwords aren't database-specific.
+             */
+            PQconninfoOption *ci;
+
+            for (ci = cinfo; ci->keyword; ci++)
+            {
+                if (user && strcmp(ci->keyword, "user") == 0)
+                {
+                    if (!(ci->val && strcmp(user, ci->val) == 0))
+                        keep_password = false;
+                }
+                else if (host && strcmp(ci->keyword, "host") == 0)
+                {
+                    if (ci->val && strcmp(host, ci->val) == 0)
+                        same_host = true;
+                    else
+                        keep_password = false;
+                }
+                else if (port && strcmp(ci->keyword, "port") == 0)
+                {
+                    if (!(ci->val && strcmp(port, ci->val) == 0))
+                        keep_password = false;
+                }
+            }
+
+            /* While here, determine how many option slots there are */
+            nconnopts = ci - cinfo;
         }
-        if (!port)
-            port = PQport(o_conn);
     }
-
-    /*
-     * Any change in the parameters read above makes us discard the password.
-     * We also discard it if we're to use a conninfo rather than the
-     * positional syntax.
-     */
-    if (has_connection_string)
-        keep_password = false;
     else
-        keep_password =
-            (user && PQuser(o_conn) && strcmp(user, PQuser(o_conn)) == 0) &&
-            (host && PQhost(o_conn) && strcmp(host, PQhost(o_conn)) == 0) &&
-            (port && PQport(o_conn) && strcmp(port, PQport(o_conn)) == 0);
-
-    /*
-     * Grab missing dbname from old connection.  No password discard if this
-     * changes: passwords aren't (usually) database-specific.
-     */
-    if (!dbname && reuse_previous)
     {
-        initPQExpBuffer(&connstr);
-        appendPQExpBufferStr(&connstr, "dbname=");
-        appendConnStrVal(&connstr, PQdb(o_conn));
-        dbname = connstr.data;
-        /* has_connection_string=true would be a dead store */
+        /* We failed to create the cinfo structure */
+        pg_log_error("out of memory");
+        success = false;
     }
-    else
-        connstr.data = NULL;

     /*
      * If the user asked to be prompted for a password, ask for one now. If
@@ -3156,7 +3208,7 @@ do_connect(enum trivalue reuse_previous_specification,
      * the postmaster's log.  But libpq offers no API that would let us obtain
      * a password and then continue with the first connection attempt.
      */
-    if (pset.getPassword == TRI_YES)
+    if (pset.getPassword == TRI_YES && success)
     {
         /*
          * If a connstring or URI is provided, we can't be sure we know which
@@ -3176,57 +3228,62 @@ do_connect(enum trivalue reuse_previous_specification,
             password = NULL;
     }

-    while (true)
+    /* Loop till we have a connection or fail, which we might've already */
+    while (success)
     {
-#define PARAMS_ARRAY_SIZE    9
-        const char **keywords = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
-        const char **values = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
-        int            paramnum = -1;
-
-        keywords[++paramnum] = "host";
-        values[paramnum] = host;
-        if (hostaddr && *hostaddr)
-        {
-            keywords[++paramnum] = "hostaddr";
-            values[paramnum] = hostaddr;
-        }
-        keywords[++paramnum] = "port";
-        values[paramnum] = port;
-        keywords[++paramnum] = "user";
-        values[paramnum] = user;
+        const char **keywords = pg_malloc((nconnopts + 1) * sizeof(*keywords));
+        const char **values = pg_malloc((nconnopts + 1) * sizeof(*values));
+        int            paramnum = 0;
+        PQconninfoOption *ci;

         /*
-         * Position in the array matters when the dbname is a connection
-         * string, because settings in a connection string override earlier
-         * array entries only.  Thus, user= in the connection string always
-         * takes effect, but client_encoding= often will not.
+         * Copy non-default settings into the PQconnectdbParams parameter
+         * arrays; but override any values specified old-style, as well as the
+         * password and a couple of fields we want to set forcibly.
+         *
+         * XXX: need to update this comment (and maybe main() too?)
          *
          * If you change this code, also change the initial-connection code in
          * main().  For no good reason, a connection string password= takes
          * precedence in main() but not here.
          */
-        keywords[++paramnum] = "dbname";
-        values[paramnum] = dbname;
-        keywords[++paramnum] = "password";
-        values[paramnum] = password;
-        keywords[++paramnum] = "fallback_application_name";
-        values[paramnum] = pset.progname;
-        keywords[++paramnum] = "client_encoding";
-        values[paramnum] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
-
+        for (ci = cinfo; ci->keyword; ci++)
+        {
+            keywords[paramnum] = ci->keyword;
+
+            if (dbname && strcmp(ci->keyword, "dbname") == 0)
+                values[paramnum++] = dbname;
+            else if (user && strcmp(ci->keyword, "user") == 0)
+                values[paramnum++] = user;
+            else if (host && strcmp(ci->keyword, "host") == 0)
+                values[paramnum++] = host;
+            else if (host && !same_host && strcmp(ci->keyword, "hostaddr") == 0)
+            {
+                /* If we're changing the host value, drop any old hostaddr */
+                values[paramnum++] = NULL;
+            }
+            else if (port && strcmp(ci->keyword, "port") == 0)
+                values[paramnum++] = port;
+            else if (strcmp(ci->keyword, "password") == 0)
+                values[paramnum++] = password;
+            else if (strcmp(ci->keyword, "fallback_application_name") == 0)
+                values[paramnum++] = pset.progname;
+            else if (strcmp(ci->keyword, "client_encoding") == 0)
+                values[paramnum++] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
+            else if (ci->val)
+                values[paramnum++] = ci->val;
+            /* else, don't bother making libpq parse this keyword */
+        }
         /* add array terminator */
-        keywords[++paramnum] = NULL;
+        keywords[paramnum] = NULL;
         values[paramnum] = NULL;

-        n_conn = PQconnectdbParams(keywords, values, true);
+        /* Note we do not want libpq to re-expand the dbname parameter */
+        n_conn = PQconnectdbParams(keywords, values, false);

         pg_free(keywords);
         pg_free(values);

-        /* We can immediately discard the password -- no longer needed */
-        if (password)
-            pg_free(password);
-
         if (PQstatus(n_conn) == CONNECTION_OK)
             break;

@@ -3242,9 +3299,28 @@ do_connect(enum trivalue reuse_previous_specification,
              */
             password = prompt_for_password(PQuser(n_conn));
             PQfinish(n_conn);
+            n_conn = NULL;
             continue;
         }

+        /*
+         * We'll report the error below ... unless n_conn is NULL, indicating
+         * that libpq didn't have enough memory to make a PGconn.
+         */
+        if (n_conn == NULL)
+            pg_log_error("out of memory");
+
+        success = false;
+    }                            /* end retry loop */
+
+    /* Release locally allocated data, whether we succeeded or not */
+    if (password)
+        pg_free(password);
+    if (cinfo)
+        PQconninfoFree(cinfo);
+
+    if (!success)
+    {
         /*
          * Failed to connect to the database. In interactive mode, keep the
          * previous connection to the DB; in scripting mode, close our
@@ -3252,7 +3328,11 @@ do_connect(enum trivalue reuse_previous_specification,
          */
         if (pset.cur_cmd_interactive)
         {
-            pg_log_info("%s", PQerrorMessage(n_conn));
+            if (n_conn)
+            {
+                pg_log_info("%s", PQerrorMessage(n_conn));
+                PQfinish(n_conn);
+            }

             /* pset.db is left unmodified */
             if (o_conn)
@@ -3260,7 +3340,12 @@ do_connect(enum trivalue reuse_previous_specification,
         }
         else
         {
-            pg_log_error("\\connect: %s", PQerrorMessage(n_conn));
+            if (n_conn)
+            {
+                pg_log_error("\\connect: %s", PQerrorMessage(n_conn));
+                PQfinish(n_conn);
+            }
+
             if (o_conn)
             {
                 /*
@@ -3274,13 +3359,8 @@ do_connect(enum trivalue reuse_previous_specification,
             }
         }

-        PQfinish(n_conn);
-        if (connstr.data)
-            termPQExpBuffer(&connstr);
         return false;
     }
-    if (connstr.data)
-        termPQExpBuffer(&connstr);

     /*
      * Replace the old connection with the new one, and update

pgsql-bugs by date:

Previous
From: Tom Lane
Date:
Subject: Re: date/time special values incorrectly cached as constant in plpgsql
Next
From: Alexander Zubkov
Date:
Subject: Re: BUG #16659: postgresql leaks memory or do not limit its usage