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

+1-2
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

+1-2
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

+5-8
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

+1-2
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

+2-4
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

+1-2
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

+1-1
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

+4-4
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

+1-2
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

+1-1
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');

src/bin/scripts/common.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ connectDatabase(const ConnParams *cparams, const char *progname,
150150
PQfinish(conn);
151151
return NULL;
152152
}
153-
pg_log_error("could not connect to database %s: %s",
154-
cparams->dbname, PQerrorMessage(conn));
153+
pg_log_error("%s", PQerrorMessage(conn));
155154
exit(1);
156155
}
157156

src/interfaces/ecpg/ecpglib/connect.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,8 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
652652
const char *errmsg = PQerrorMessage(this->connection);
653653
const char *db = realname ? realname : ecpg_gettext("<DEFAULT>");
654654

655-
ecpg_log("ECPGconnect: could not open database: %s\n", errmsg);
655+
/* PQerrorMessage's result already has a trailing newline */
656+
ecpg_log("ECPGconnect: %s", errmsg);
656657

657658
ecpg_finish(this);
658659
#ifdef ENABLE_THREAD_SAFETY

src/interfaces/ecpg/test/expected/connect-test5.stderr

+2-4
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
[NO_PID]: sqlca: code: 0, state: 00000
3737
[NO_PID]: ECPGconnect: opening database <DEFAULT> on <DEFAULT> port <DEFAULT> for user regress_ecpg_user2
3838
[NO_PID]: sqlca: code: 0, state: 00000
39-
[NO_PID]: ECPGconnect: could not open database: connection to server failed: FATAL: database "regress_ecpg_user2" does not exist
40-
39+
[NO_PID]: ECPGconnect: connection to server failed: FATAL: database "regress_ecpg_user2" does not exist
4140
[NO_PID]: sqlca: code: 0, state: 00000
4241
[NO_PID]: ecpg_finish: connection main closed
4342
[NO_PID]: sqlca: code: 0, state: 00000
@@ -73,8 +72,7 @@
7372
[NO_PID]: sqlca: code: -220, state: 08003
7473
[NO_PID]: ECPGconnect: opening database <DEFAULT> on <DEFAULT> port <DEFAULT> for user regress_ecpg_user2
7574
[NO_PID]: sqlca: code: 0, state: 00000
76-
[NO_PID]: ECPGconnect: could not open database: connection to server failed: FATAL: database "regress_ecpg_user2" does not exist
77-
75+
[NO_PID]: ECPGconnect: connection to server failed: FATAL: database "regress_ecpg_user2" does not exist
7876
[NO_PID]: sqlca: code: 0, state: 00000
7977
[NO_PID]: ecpg_finish: connection main closed
8078
[NO_PID]: sqlca: code: 0, state: 00000

src/test/examples/testlibpq.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ main(int argc, char **argv)
4343
/* Check to see that the backend connection was successfully made */
4444
if (PQstatus(conn) != CONNECTION_OK)
4545
{
46-
fprintf(stderr, "Connection to database failed: %s",
47-
PQerrorMessage(conn));
46+
fprintf(stderr, "%s", PQerrorMessage(conn));
4847
exit_nicely(conn);
4948
}
5049

src/test/examples/testlibpq2.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ main(int argc, char **argv)
7272
/* Check to see that the backend connection was successfully made */
7373
if (PQstatus(conn) != CONNECTION_OK)
7474
{
75-
fprintf(stderr, "Connection to database failed: %s",
76-
PQerrorMessage(conn));
75+
fprintf(stderr, "%s", PQerrorMessage(conn));
7776
exit_nicely(conn);
7877
}
7978

src/test/examples/testlibpq3.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ main(int argc, char **argv)
138138
/* Check to see that the backend connection was successfully made */
139139
if (PQstatus(conn) != CONNECTION_OK)
140140
{
141-
fprintf(stderr, "Connection to database failed: %s",
142-
PQerrorMessage(conn));
141+
fprintf(stderr, "%s", PQerrorMessage(conn));
143142
exit_nicely(conn);
144143
}
145144

src/test/examples/testlibpq4.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ check_prepare_conn(PGconn *conn, const char *dbName)
2929
/* check to see that the backend connection was successfully made */
3030
if (PQstatus(conn) != CONNECTION_OK)
3131
{
32-
fprintf(stderr, "Connection to database \"%s\" failed: %s",
33-
dbName, PQerrorMessage(conn));
32+
fprintf(stderr, "%s", PQerrorMessage(conn));
3433
exit(1);
3534
}
3635

src/test/examples/testlo.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,7 @@ main(int argc, char **argv)
225225
/* check to see that the backend connection was successfully made */
226226
if (PQstatus(conn) != CONNECTION_OK)
227227
{
228-
fprintf(stderr, "Connection to database failed: %s",
229-
PQerrorMessage(conn));
228+
fprintf(stderr, "%s", PQerrorMessage(conn));
230229
exit_nicely(conn);
231230
}
232231

src/test/examples/testlo64.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,7 @@ main(int argc, char **argv)
249249
/* check to see that the backend connection was successfully made */
250250
if (PQstatus(conn) != CONNECTION_OK)
251251
{
252-
fprintf(stderr, "Connection to database failed: %s",
253-
PQerrorMessage(conn));
252+
fprintf(stderr, "%s", PQerrorMessage(conn));
254253
exit_nicely(conn);
255254
}
256255

src/test/isolation/isolationtester.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ main(int argc, char **argv)
167167
conns[i] = PQconnectdb(conninfo);
168168
if (PQstatus(conns[i]) != CONNECTION_OK)
169169
{
170-
fprintf(stderr, "Connection %d to database failed: %s",
170+
fprintf(stderr, "Connection %d failed: %s",
171171
i, PQerrorMessage(conns[i]));
172172
exit(1);
173173
}

src/tools/findoidjoins/findoidjoins.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ main(int argc, char **argv)
4444
conn = PQconnectdb(sql.data);
4545
if (PQstatus(conn) == CONNECTION_BAD)
4646
{
47-
fprintf(stderr, "connection error: %s\n", PQerrorMessage(conn));
47+
fprintf(stderr, "%s", PQerrorMessage(conn));
4848
exit(EXIT_FAILURE);
4949
}
5050

0 commit comments

Comments
 (0)