Skip to content

Commit e2a9129

Browse files
committed
pg_basebackup, pg_receivewal: fix failure to find password in ~/.pgpass.
Sloppy refactoring in commit cca97ce 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-patch 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
1 parent e367114 commit e2a9129

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

doc/src/sgml/ref/pg_basebackup.sgml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -807,14 +807,18 @@ PostgreSQL documentation
807807
will override any conflicting command line options.
808808
</para>
809809
<para>
810-
The option is called <literal>--dbname</literal> for consistency with other
810+
This option is called <literal>--dbname</literal> for consistency with other
811811
client applications, but because <application>pg_basebackup</application>
812812
doesn't connect to any particular database in the cluster, any database
813-
name in the connection string will be ignored
814-
by <productname>PostgreSQL</productname>. Middleware, or proxies, used in
815-
connecting to <productname>PostgreSQL</productname> might however
816-
utilize the value. The database name specified in connection string can
817-
also be used by <link linkend="logicaldecoding-replication-slots-synchronization">
813+
name included in the connection string will be ignored by the server.
814+
However, a database name supplied that way overrides the default
815+
database name (<literal>replication</literal>) for purposes of
816+
looking up the replication connection's password
817+
in <filename>~/.pgpass</filename>. Similarly, middleware or proxies
818+
used in connecting to <productname>PostgreSQL</productname> might
819+
utilize the name for purposes such as connection routing. The
820+
database name can also be used
821+
by <link linkend="logicaldecoding-replication-slots-synchronization">
818822
logical replication slot synchronization</link>.
819823
</para>
820824
</listitem>

doc/src/sgml/ref/pg_receivewal.sgml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,16 @@ PostgreSQL documentation
315315
will override any conflicting command line options.
316316
</para>
317317
<para>
318-
The option is called <literal>--dbname</literal> for consistency with other
318+
This option is called <literal>--dbname</literal> for consistency with other
319319
client applications, but because <application>pg_receivewal</application>
320320
doesn't connect to any particular database in the cluster, any database
321-
name in the connection string will be ignored by
322-
<productname>PostgreSQL</productname>. Middleware, or proxies, used in
323-
connecting to <productname>PostgreSQL</productname> might however
324-
utilize the value.
321+
name included in the connection string will be ignored by the server.
322+
However, a database name supplied that way overrides the default
323+
database name (<literal>replication</literal>) for purposes of
324+
looking up the replication connection's password
325+
in <filename>~/.pgpass</filename>. Similarly, middleware or proxies
326+
used in connecting to <productname>PostgreSQL</productname> might
327+
utilize the name for purposes such as connection routing.
325328
</para>
326329
</listitem>
327330
</varlistentry>

src/bin/pg_basebackup/streamutil.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ GetConnection(void)
7474
PQconninfoOption *conn_opt;
7575
char *err_msg = NULL;
7676

77-
/* pg_recvlogical uses dbname only; others use connection_string only. */
77+
/*
78+
* pg_recvlogical uses dbname only; others use connection_string only.
79+
* (Note: both variables will be NULL if there's no command line options.)
80+
*/
7881
Assert(dbname == NULL || connection_string == NULL);
7982

8083
/*
@@ -120,12 +123,12 @@ GetConnection(void)
120123
keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
121124
values = pg_malloc0((argcount + 1) * sizeof(*values));
122125
keywords[i] = "dbname";
123-
values[i] = dbname;
126+
values[i] = (dbname == NULL) ? "replication" : dbname;
124127
i++;
125128
}
126129

127130
keywords[i] = "replication";
128-
values[i] = dbname == NULL ? "true" : "database";
131+
values[i] = (dbname == NULL) ? "true" : "database";
129132
i++;
130133
keywords[i] = "fallback_application_name";
131134
values[i] = progname;

0 commit comments

Comments
 (0)