Skip to content

Commit 58cd8dc

Browse files
committed
Avoid redundantly prefixing PQerrorMessage for a connection failure.
libpq's error messages for connection failures pretty well stand on their own, especially since commits 52a1022/27a48e5a1. Prefixing them with 'could not connect to database "foo"' or the like is just redundant, and perhaps even misleading if the specific database name isn't relevant to the failure. (When it is, we trust that the backend's error message will include the DB name.) Indeed, psql hasn't used any such prefix in a long time. So, make all our other programs and documentation examples agree with psql's practice. Discussion: https://postgr.es/m/[email protected]
1 parent 7cd9765 commit 58cd8dc

File tree

21 files changed

+31
-49
lines changed

21 files changed

+31
-49
lines changed

contrib/oid2name/oid2name.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,7 @@ sql_conn(struct options *my_opts)
347347
/* check to see that the backend connection was successfully made */
348348
if (PQstatus(conn) == CONNECTION_BAD)
349349
{
350-
pg_log_error("could not connect to database %s: %s",
351-
my_opts->dbname, PQerrorMessage(conn));
350+
pg_log_error("%s", PQerrorMessage(conn));
352351
PQfinish(conn);
353352
exit(1);
354353
}

contrib/vacuumlo/vacuumlo.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ vacuumlo(const char *database, const struct _param *param)
124124
/* check to see that the backend connection was successfully made */
125125
if (PQstatus(conn) == CONNECTION_BAD)
126126
{
127-
pg_log_error("connection to database \"%s\" failed: %s",
128-
database, PQerrorMessage(conn));
127+
pg_log_error("%s", PQerrorMessage(conn));
129128
PQfinish(conn);
130129
return -1;
131130
}

doc/src/sgml/libpq.sgml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6837,8 +6837,8 @@ main(void)
68376837

68386838
if (PQstatus(conn) != CONNECTION_OK)
68396839
{
6840-
fprintf(stderr, "Connection to database failed: %s",
6841-
PQerrorMessage(conn));
6840+
/* PQerrorMessage's result includes a trailing newline */
6841+
fprintf(stderr, "%s", PQerrorMessage(conn));
68426842
PQfinish(conn);
68436843
return 1;
68446844
}
@@ -8296,8 +8296,7 @@ main(int argc, char **argv)
82968296
/* Check to see that the backend connection was successfully made */
82978297
if (PQstatus(conn) != CONNECTION_OK)
82988298
{
8299-
fprintf(stderr, "Connection to database failed: %s",
8300-
PQerrorMessage(conn));
8299+
fprintf(stderr, "%s", PQerrorMessage(conn));
83018300
exit_nicely(conn);
83028301
}
83038302

@@ -8466,8 +8465,7 @@ main(int argc, char **argv)
84668465
/* Check to see that the backend connection was successfully made */
84678466
if (PQstatus(conn) != CONNECTION_OK)
84688467
{
8469-
fprintf(stderr, "Connection to database failed: %s",
8470-
PQerrorMessage(conn));
8468+
fprintf(stderr, "%s", PQerrorMessage(conn));
84718469
exit_nicely(conn);
84728470
}
84738471

@@ -8694,8 +8692,7 @@ main(int argc, char **argv)
86948692
/* Check to see that the backend connection was successfully made */
86958693
if (PQstatus(conn) != CONNECTION_OK)
86968694
{
8697-
fprintf(stderr, "Connection to database failed: %s",
8698-
PQerrorMessage(conn));
8695+
fprintf(stderr, "%s", PQerrorMessage(conn));
86998696
exit_nicely(conn);
87008697
}
87018698

doc/src/sgml/lobj.sgml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -939,8 +939,7 @@ main(int argc, char **argv)
939939
/* check to see that the backend connection was successfully made */
940940
if (PQstatus(conn) != CONNECTION_OK)
941941
{
942-
fprintf(stderr, "Connection to database failed: %s",
943-
PQerrorMessage(conn));
942+
fprintf(stderr, "%s", PQerrorMessage(conn));
944943
exit_nicely(conn);
945944
}
946945

src/bin/pg_dump/pg_backup_db.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,10 @@ ConnectDatabase(Archive *AHX,
188188
if (PQstatus(AH->connection) == CONNECTION_BAD)
189189
{
190190
if (isReconnect)
191-
fatal("reconnection to database \"%s\" failed: %s",
192-
PQdb(AH->connection) ? PQdb(AH->connection) : "",
191+
fatal("reconnection failed: %s",
193192
PQerrorMessage(AH->connection));
194193
else
195-
fatal("connection to database \"%s\" failed: %s",
196-
PQdb(AH->connection) ? PQdb(AH->connection) : "",
194+
fatal("%s",
197195
PQerrorMessage(AH->connection));
198196
}
199197

src/bin/pg_dump/pg_dumpall.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,8 +1768,7 @@ connectDatabase(const char *dbname, const char *connection_string,
17681768
{
17691769
if (fail_on_error)
17701770
{
1771-
pg_log_error("could not connect to database \"%s\": %s",
1772-
dbname, PQerrorMessage(conn));
1771+
pg_log_error("%s", PQerrorMessage(conn));
17731772
exit_nicely(1);
17741773
}
17751774
else

src/bin/pg_dump/t/002_pg_dump.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3460,7 +3460,7 @@
34603460
34613461
command_fails_like(
34623462
[ 'pg_dump', '-p', "$port", 'qqq' ],
3463-
qr/pg_dump: error: connection to database "qqq" failed: connection to server .* failed: FATAL: database "qqq" does not exist/,
3463+
qr/pg_dump: error: connection to server .* failed: FATAL: database "qqq" does not exist/,
34643464
'connecting to a non-existent database');
34653465
34663466
#########################################

src/bin/pg_upgrade/server.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ connectToServer(ClusterInfo *cluster, const char *db_name)
3030

3131
if (conn == NULL || PQstatus(conn) != CONNECTION_OK)
3232
{
33-
pg_log(PG_REPORT, "connection to database failed: %s",
34-
PQerrorMessage(conn));
33+
pg_log(PG_REPORT, "%s", PQerrorMessage(conn));
3534

3635
if (conn)
3736
PQfinish(conn);
@@ -50,6 +49,8 @@ connectToServer(ClusterInfo *cluster, const char *db_name)
5049
* get_db_conn()
5150
*
5251
* get database connection, using named database + standard params for cluster
52+
*
53+
* Caller must check for connection failure!
5354
*/
5455
static PGconn *
5556
get_db_conn(ClusterInfo *cluster, const char *db_name)
@@ -294,8 +295,7 @@ start_postmaster(ClusterInfo *cluster, bool report_and_exit_on_error)
294295
if ((conn = get_db_conn(cluster, "template1")) == NULL ||
295296
PQstatus(conn) != CONNECTION_OK)
296297
{
297-
pg_log(PG_REPORT, "\nconnection to database failed: %s",
298-
PQerrorMessage(conn));
298+
pg_log(PG_REPORT, "\n%s", PQerrorMessage(conn));
299299
if (conn)
300300
PQfinish(conn);
301301
if (cluster == &old_cluster)

src/bin/pgbench/pgbench.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,8 +1225,7 @@ doConnect(void)
12251225
/* check to see that the backend connection was successfully made */
12261226
if (PQstatus(conn) == CONNECTION_BAD)
12271227
{
1228-
pg_log_error("connection to database \"%s\" failed: %s",
1229-
dbName, PQerrorMessage(conn));
1228+
pg_log_error("%s", PQerrorMessage(conn));
12301229
PQfinish(conn);
12311230
return NULL;
12321231
}

src/bin/pgbench/t/001_pgbench_with_server.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ sub pgbench
9090
1,
9191
[qr{^$}],
9292
[
93-
qr{connection to database "no-such-database" failed},
93+
qr{connection to server .* failed},
9494
qr{FATAL: database "no-such-database" does not exist}
9595
],
9696
'no such database');

0 commit comments

Comments
 (0)