Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit fdf9d00

Browse files
committed
Report the true database name on connection errors
When reporting connection errors, we might show a database name in the message that's not the one we actually tried to connect to, if the database was taken from libpq defaults instead of from user parameters. Fix such error messages to use PQdb(), which reports the correct name. (But, per commit 2930c05, make sure not to try to print NULL.) Apply to branches 9.5 through 13. Branch master has already been changed differently by commit 58cd8dc. Reported-by: Robert Haas <robertmhaas@gmail.com> Discussion: https://postgr.es/m/CA+TgmobssJ6rS22dspWnu-oDxXevGmhMD8VcRBjmj-b9UDqRjw@mail.gmail.com
1 parent 82f97d3 commit fdf9d00

File tree

4 files changed

+6
-5
lines changed

4 files changed

+6
-5
lines changed

contrib/oid2name/oid2name.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ sql_conn(struct options *my_opts)
340340
if (PQstatus(conn) == CONNECTION_BAD)
341341
{
342342
fprintf(stderr, "%s: could not connect to database %s: %s",
343-
"oid2name", my_opts->dbname, PQerrorMessage(conn));
343+
"oid2name", PQdb(conn) ? PQdb(conn) : "", PQerrorMessage(conn));
344344
PQfinish(conn);
345345
exit(1);
346346
}

contrib/vacuumlo/vacuumlo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ vacuumlo(const char *database, const struct _param *param)
130130
if (PQstatus(conn) == CONNECTION_BAD)
131131
{
132132
fprintf(stderr, "Connection to database \"%s\" failed:\n%s",
133-
database, PQerrorMessage(conn));
133+
PQdb(conn) ? PQdb(conn) : "", PQerrorMessage(conn));
134134
PQfinish(conn);
135135
return -1;
136136
}

src/bin/pg_dump/pg_dumpall.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1773,7 +1773,7 @@ connectDatabase(const char *dbname, const char *connection_string,
17731773
if (fail_on_error)
17741774
{
17751775
pg_log_error("could not connect to database \"%s\": %s",
1776-
dbname, PQerrorMessage(conn));
1776+
PQdb(conn) ? PQdb(conn) : "", PQerrorMessage(conn));
17771777
exit_nicely(1);
17781778
}
17791779
else

src/bin/pgbench/pgbench.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,7 @@ doConnect(void)
12061206
if (PQstatus(conn) == CONNECTION_BAD)
12071207
{
12081208
fprintf(stderr, "connection to database \"%s\" failed:\n%s",
1209-
dbName, PQerrorMessage(conn));
1209+
PQdb(conn), PQerrorMessage(conn));
12101210
PQfinish(conn);
12111211
return NULL;
12121212
}
@@ -5678,7 +5678,8 @@ main(int argc, char **argv)
56785678

56795679
if (PQstatus(con) == CONNECTION_BAD)
56805680
{
5681-
fprintf(stderr, "connection to database \"%s\" failed\n", dbName);
5681+
fprintf(stderr, "connection to database \"%s\" failed\n",
5682+
PQdb(con) ? PQdb(con) : "");
56825683
fprintf(stderr, "%s", PQerrorMessage(con));
56835684
exit(1);
56845685
}

0 commit comments

Comments
 (0)