summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlvaro Herrera <alvherre@alvh.no-ip.org>2021-01-26 16:42:13 -0300
committerAlvaro Herrera <alvherre@alvh.no-ip.org>2021-01-26 16:42:13 -0300
commitf2dc962360b329c811c5e08eb0de4fb82462cdaf (patch)
tree367c07c4bdc03ecc1093a8875138fa9645b46ae5
parent131825cd50a898b91e43e82cdd971032ef32b3b3 (diff)
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 2930c05634bc, make sure not to try to print NULL.) Apply to branches 9.5 through 13. Branch master has already been changed differently by commit 58cd8dca3de0. Reported-by: Robert Haas <robertmhaas@gmail.com> Discussion: https://postgr.es/m/CA+TgmobssJ6rS22dspWnu-oDxXevGmhMD8VcRBjmj-b9UDqRjw@mail.gmail.com
-rw-r--r--contrib/oid2name/oid2name.c2
-rw-r--r--contrib/vacuumlo/vacuumlo.c2
-rw-r--r--src/bin/pg_dump/pg_dumpall.c2
-rw-r--r--src/bin/pgbench/pgbench.c5
4 files changed, 6 insertions, 5 deletions
diff --git a/contrib/oid2name/oid2name.c b/contrib/oid2name/oid2name.c
index 91da40352b4..69671129c2f 100644
--- a/contrib/oid2name/oid2name.c
+++ b/contrib/oid2name/oid2name.c
@@ -319,7 +319,7 @@ sql_conn(struct options * my_opts)
if (PQstatus(conn) == CONNECTION_BAD)
{
fprintf(stderr, "%s: could not connect to database %s: %s",
- "oid2name", my_opts->dbname, PQerrorMessage(conn));
+ "oid2name", PQdb(conn) ? PQdb(conn) : "", PQerrorMessage(conn));
PQfinish(conn);
exit(1);
}
diff --git a/contrib/vacuumlo/vacuumlo.c b/contrib/vacuumlo/vacuumlo.c
index 0f79b139cfb..5788c17674c 100644
--- a/contrib/vacuumlo/vacuumlo.c
+++ b/contrib/vacuumlo/vacuumlo.c
@@ -124,7 +124,7 @@ vacuumlo(const char *database, const struct _param * param)
if (PQstatus(conn) == CONNECTION_BAD)
{
fprintf(stderr, "Connection to database \"%s\" failed:\n%s",
- database, PQerrorMessage(conn));
+ PQdb(conn) ? PQdb(conn) : "", PQerrorMessage(conn));
PQfinish(conn);
return -1;
}
diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c
index e30bb85b3b4..20cef1b4753 100644
--- a/src/bin/pg_dump/pg_dumpall.c
+++ b/src/bin/pg_dump/pg_dumpall.c
@@ -1954,7 +1954,7 @@ connectDatabase(const char *dbname, const char *connection_string,
{
fprintf(stderr,
_("%s: could not connect to database \"%s\": %s"),
- progname, dbname, PQerrorMessage(conn));
+ progname, PQdb(conn) ? PQdb(conn) : "", PQerrorMessage(conn));
exit_nicely(1);
}
else
diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
index b08121c905e..875b764fa9e 100644
--- a/src/bin/pgbench/pgbench.c
+++ b/src/bin/pgbench/pgbench.c
@@ -686,7 +686,7 @@ doConnect(void)
if (PQstatus(conn) == CONNECTION_BAD)
{
fprintf(stderr, "connection to database \"%s\" failed:\n%s",
- dbName, PQerrorMessage(conn));
+ PQdb(conn), PQerrorMessage(conn));
PQfinish(conn);
return NULL;
}
@@ -3312,7 +3312,8 @@ main(int argc, char **argv)
if (PQstatus(con) == CONNECTION_BAD)
{
- fprintf(stderr, "connection to database \"%s\" failed\n", dbName);
+ fprintf(stderr, "connection to database \"%s\" failed\n",
+ PQdb(con) ? PQdb(con) : "");
fprintf(stderr, "%s", PQerrorMessage(con));
exit(1);
}