pg_basebackup, pg_receivewal: fix failure to find password in ~/.pgpass.
authorTom Lane <[email protected]>
Mon, 4 Nov 2024 19:36:04 +0000 (14:36 -0500)
committerTom Lane <[email protected]>
Mon, 4 Nov 2024 19:36:11 +0000 (14:36 -0500)
Sloppy refactoring in commit cca97ce6a caused these programs
to pass dbname = NULL to libpq if there was no "--dbname" switch
on the command line, where before "replication" would be passed.
This didn't break things completely, because the source server doesn't
care about the dbname specified for a physical replication connection.
However, it did cause libpq to fail to match a ~/.pgpass entry that
has "replication" in the dbname field.  Restore the previous behavior
of passing "replication".

Also, closer inspection shows that if you do specify a dbname
in the connection string, that is what will be matched to ~/.pgpass,
not "replication".  This was the pre-existing behavior so we should
not change it, but the SGML docs were pretty misleading about it.
Improve that.

Per bug #18685 from Toshi Harada.  Back- to v17 where the
error crept in.

Discussion: https://postgr.es/m/18685-fee2dd142b9688f1@postgresql.org
Discussion: https://postgr.es/m/2702546.1730740456@sss.pgh.pa.us

doc/src/sgml/ref/pg_basebackup.sgml
doc/src/sgml/ref/pg_receivewal.sgml
src/bin/pg_basebackup/streamutil.c

index 4f99340c1dd8f323f39392678d9b5a85a44536f7..c2d721208b1d0c0393a28549b3dd357960738691 100644 (file)
@@ -807,14 +807,18 @@ PostgreSQL documentation
         will override any conflicting command line options.
        </para>
        <para>
-        The option is called <literal>--dbname</literal> for consistency with other
+        This option is called <literal>--dbname</literal> for consistency with other
         client applications, but because <application>pg_basebackup</application>
         doesn't connect to any particular database in the cluster, any database
-        name in the connection string will be ignored
-        by <productname>PostgreSQL</productname>. Middleware, or proxies, used in
-        connecting to <productname>PostgreSQL</productname> might however
-        utilize the value. The database name specified in connection string can
-        also be used by <link linkend="logicaldecoding-replication-slots-synchronization">
+        name included in the connection string will be ignored by the server.
+        However, a database name supplied that way overrides the default
+        database name (<literal>replication</literal>) for purposes of
+        looking up the replication connection's password
+        in <filename>~/.pgpass</filename>.  Similarly, middleware or proxies
+        used in connecting to <productname>PostgreSQL</productname> might
+        utilize the name for purposes such as connection routing.  The
+        database name can also be used
+        by <link linkend="logicaldecoding-replication-slots-synchronization">
         logical replication slot synchronization</link>.
        </para>
       </listitem>
index 8944eee2a36499e0944f36bd0e2aa57ed007cf40..9e353a068e5a3e150049af45bbc096fdde5135c4 100644 (file)
@@ -315,13 +315,16 @@ PostgreSQL documentation
         will override any conflicting command line options.
        </para>
        <para>
-        The option is called <literal>--dbname</literal> for consistency with other
+        This option is called <literal>--dbname</literal> for consistency with other
         client applications, but because <application>pg_receivewal</application>
         doesn't connect to any particular database in the cluster, any database
-        name in the connection string will be ignored by
-        <productname>PostgreSQL</productname>. Middleware, or proxies, used in
-        connecting to <productname>PostgreSQL</productname> might however
-        utilize the value.
+        name included in the connection string will be ignored by the server.
+        However, a database name supplied that way overrides the default
+        database name (<literal>replication</literal>) for purposes of
+        looking up the replication connection's password
+        in <filename>~/.pgpass</filename>.  Similarly, middleware or proxies
+        used in connecting to <productname>PostgreSQL</productname> might
+        utilize the name for purposes such as connection routing.
        </para>
       </listitem>
      </varlistentry>
index 30b3d9a377e73ae057dff4072248e09f588e9453..9b864825462e1c2e3bedff69a67e00d7ae0a7cbc 100644 (file)
@@ -74,7 +74,10 @@ GetConnection(void)
    PQconninfoOption *conn_opt;
    char       *err_msg = NULL;
 
-   /* pg_recvlogical uses dbname only; others use connection_string only. */
+   /*
+    * pg_recvlogical uses dbname only; others use connection_string only.
+    * (Note: both variables will be NULL if there's no command line options.)
+    */
    Assert(dbname == NULL || connection_string == NULL);
 
    /*
@@ -120,12 +123,12 @@ GetConnection(void)
        keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
        values = pg_malloc0((argcount + 1) * sizeof(*values));
        keywords[i] = "dbname";
-       values[i] = dbname;
+       values[i] = (dbname == NULL) ? "replication" : dbname;
        i++;
    }
 
    keywords[i] = "replication";
-   values[i] = dbname == NULL ? "true" : "database";
+   values[i] = (dbname == NULL) ? "true" : "database";
    i++;
    keywords[i] = "fallback_application_name";
    values[i] = progname;