Avoid redundantly prefixing PQerrorMessage for a connection failure.
authorTom Lane <[email protected]>
Fri, 22 Jan 2021 21:52:31 +0000 (16:52 -0500)
committerTom Lane <[email protected]>
Fri, 22 Jan 2021 21:52:31 +0000 (16:52 -0500)
libpq's error messages for connection failures pretty well stand on
their own, especially since commits 52a10224e/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/1094524.1611266589@sss.pgh.pa.us

21 files changed:
contrib/oid2name/oid2name.c
contrib/vacuumlo/vacuumlo.c
doc/src/sgml/libpq.sgml
doc/src/sgml/lobj.sgml
src/bin/pg_dump/pg_backup_db.c
src/bin/pg_dump/pg_dumpall.c
src/bin/pg_dump/t/002_pg_dump.pl
src/bin/pg_upgrade/server.c
src/bin/pgbench/pgbench.c
src/bin/pgbench/t/001_pgbench_with_server.pl
src/bin/scripts/common.c
src/interfaces/ecpg/ecpglib/connect.c
src/interfaces/ecpg/test/expected/connect-test5.stderr
src/test/examples/testlibpq.c
src/test/examples/testlibpq2.c
src/test/examples/testlibpq3.c
src/test/examples/testlibpq4.c
src/test/examples/testlo.c
src/test/examples/testlo64.c
src/test/isolation/isolationtester.c
src/tools/findoidjoins/findoidjoins.c

index 5a884e29049f8fbe14d877fb5582bb9f76f675b4..65cce4999366b26750902b85eaddf1b297795b4b 100644 (file)
@@ -347,8 +347,7 @@ sql_conn(struct options *my_opts)
    /* check to see that the backend connection was successfully made */
    if (PQstatus(conn) == CONNECTION_BAD)
    {
-       pg_log_error("could not connect to database %s: %s",
-                    my_opts->dbname, PQerrorMessage(conn));
+       pg_log_error("%s", PQerrorMessage(conn));
        PQfinish(conn);
        exit(1);
    }
index cdc2c02b8e825abe53d54f30ebe4f10a24dd4896..dcb95c432047088e6f07c90fc2b0b472ed051349 100644 (file)
@@ -124,8 +124,7 @@ vacuumlo(const char *database, const struct _param *param)
    /* check to see that the backend connection was successfully made */
    if (PQstatus(conn) == CONNECTION_BAD)
    {
-       pg_log_error("connection to database \"%s\" failed: %s",
-                    database, PQerrorMessage(conn));
+       pg_log_error("%s", PQerrorMessage(conn));
        PQfinish(conn);
        return -1;
    }
index b22cb38ca3b295a3a1ea36a6f923ef10fad0a78e..b7a82453f0da16437be711ce331f24cc42c78381 100644 (file)
@@ -6837,8 +6837,8 @@ main(void)
 
     if (PQstatus(conn) != CONNECTION_OK)
     {
-        fprintf(stderr, "Connection to database failed: %s",
-                PQerrorMessage(conn));
+        /* PQerrorMessage's result includes a trailing newline */
+        fprintf(stderr, "%s", PQerrorMessage(conn));
         PQfinish(conn);
         return 1;
     }
@@ -8296,8 +8296,7 @@ main(int argc, char **argv)
     /* Check to see that the backend connection was successfully made */
     if (PQstatus(conn) != CONNECTION_OK)
     {
-        fprintf(stderr, "Connection to database failed: %s",
-                PQerrorMessage(conn));
+        fprintf(stderr, "%s", PQerrorMessage(conn));
         exit_nicely(conn);
     }
 
@@ -8466,8 +8465,7 @@ main(int argc, char **argv)
     /* Check to see that the backend connection was successfully made */
     if (PQstatus(conn) != CONNECTION_OK)
     {
-        fprintf(stderr, "Connection to database failed: %s",
-                PQerrorMessage(conn));
+        fprintf(stderr, "%s", PQerrorMessage(conn));
         exit_nicely(conn);
     }
 
@@ -8694,8 +8692,7 @@ main(int argc, char **argv)
     /* Check to see that the backend connection was successfully made */
     if (PQstatus(conn) != CONNECTION_OK)
     {
-        fprintf(stderr, "Connection to database failed: %s",
-                PQerrorMessage(conn));
+        fprintf(stderr, "%s", PQerrorMessage(conn));
         exit_nicely(conn);
     }
 
index 413eda50af370b1026b998642ec47c57694fbfbb..6d46da42e27b164a82c7fcec79317d3039dc8fc5 100644 (file)
@@ -939,8 +939,7 @@ main(int argc, char **argv)
     /* check to see that the backend connection was successfully made */
     if (PQstatus(conn) != CONNECTION_OK)
     {
-        fprintf(stderr, "Connection to database failed: %s",
-                PQerrorMessage(conn));
+        fprintf(stderr, "%s", PQerrorMessage(conn));
         exit_nicely(conn);
     }
 
index 5ba43441f50aa111182ee488bb995efe18f5d617..2856c16e853dba4087061137e33a31912355ddf8 100644 (file)
@@ -188,12 +188,10 @@ ConnectDatabase(Archive *AHX,
    if (PQstatus(AH->connection) == CONNECTION_BAD)
    {
        if (isReconnect)
-           fatal("reconnection to database \"%s\" failed: %s",
-                 PQdb(AH->connection) ? PQdb(AH->connection) : "",
+           fatal("reconnection failed: %s",
                  PQerrorMessage(AH->connection));
        else
-           fatal("connection to database \"%s\" failed: %s",
-                 PQdb(AH->connection) ? PQdb(AH->connection) : "",
+           fatal("%s",
                  PQerrorMessage(AH->connection));
    }
 
index 85d08ad66037662781000663011ae5f55d9752cc..007a3d0f9a3726d904c5e4dc3c0c97198b9f17a4 100644 (file)
@@ -1768,8 +1768,7 @@ connectDatabase(const char *dbname, const char *connection_string,
    {
        if (fail_on_error)
        {
-           pg_log_error("could not connect to database \"%s\": %s",
-                        dbname, PQerrorMessage(conn));
+           pg_log_error("%s", PQerrorMessage(conn));
            exit_nicely(1);
        }
        else
index a9bbb80e63996d4eb9737d42bad654c99b9ced85..798884da36b12f78b87fb8b282b80bb92654af86 100644 (file)
@@ -3460,7 +3460,7 @@ foreach my $db (sort keys %create_sql)
 
 command_fails_like(
    [ 'pg_dump', '-p', "$port", 'qqq' ],
-   qr/pg_dump: error: connection to database "qqq" failed: connection to server .* failed: FATAL:  database "qqq" does not exist/,
+   qr/pg_dump: error: connection to server .* failed: FATAL:  database "qqq" does not exist/,
    'connecting to a non-existent database');
 
 #########################################
index 31b1425202915d8a7499203fc073150293e3cda9..7fed0ae1086dc30edabae41d8372f90a1edbcd44 100644 (file)
@@ -30,8 +30,7 @@ connectToServer(ClusterInfo *cluster, const char *db_name)
 
    if (conn == NULL || PQstatus(conn) != CONNECTION_OK)
    {
-       pg_log(PG_REPORT, "connection to database failed: %s",
-              PQerrorMessage(conn));
+       pg_log(PG_REPORT, "%s", PQerrorMessage(conn));
 
        if (conn)
            PQfinish(conn);
@@ -50,6 +49,8 @@ connectToServer(ClusterInfo *cluster, const char *db_name)
  * get_db_conn()
  *
  * get database connection, using named database + standard params for cluster
+ *
+ * Caller must check for connection failure!
  */
 static PGconn *
 get_db_conn(ClusterInfo *cluster, const char *db_name)
@@ -294,8 +295,7 @@ start_postmaster(ClusterInfo *cluster, bool report_and_exit_on_error)
    if ((conn = get_db_conn(cluster, "template1")) == NULL ||
        PQstatus(conn) != CONNECTION_OK)
    {
-       pg_log(PG_REPORT, "\nconnection to database failed: %s",
-              PQerrorMessage(conn));
+       pg_log(PG_REPORT, "\n%s", PQerrorMessage(conn));
        if (conn)
            PQfinish(conn);
        if (cluster == &old_cluster)
index f7da3e1f62692763eb736c22aa39c02d145dee69..1be1ad3d6d9596839d98f7c3bd2e3643753e49ae 100644 (file)
@@ -1225,8 +1225,7 @@ doConnect(void)
    /* check to see that the backend connection was successfully made */
    if (PQstatus(conn) == CONNECTION_BAD)
    {
-       pg_log_error("connection to database \"%s\" failed: %s",
-                    dbName, PQerrorMessage(conn));
+       pg_log_error("%s", PQerrorMessage(conn));
        PQfinish(conn);
        return NULL;
    }
index 61b671d54fd66a6f9b6c0465f68d3af327b73d10..daffc18e521945cd6a953d6272edcae5b6073f52 100644 (file)
@@ -90,7 +90,7 @@ pgbench(
    1,
    [qr{^$}],
    [
-       qr{connection to database "no-such-database" failed},
+       qr{connection to server .* failed},
        qr{FATAL:  database "no-such-database" does not exist}
    ],
    'no such database');
index 13ac531695064763d8a1576106c0638c727471a3..21ef297e6eb658b55e28ff709b289f8d614815f9 100644 (file)
@@ -150,8 +150,7 @@ connectDatabase(const ConnParams *cparams, const char *progname,
            PQfinish(conn);
            return NULL;
        }
-       pg_log_error("could not connect to database %s: %s",
-                    cparams->dbname, PQerrorMessage(conn));
+       pg_log_error("%s", PQerrorMessage(conn));
        exit(1);
    }
 
index 1cb52116f95d7a6c5a8d2fa82fb7eb9fe1adb8a1..6b0a3067e6c124d1f99016e375c0f2480550cde2 100644 (file)
@@ -652,7 +652,8 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
        const char *errmsg = PQerrorMessage(this->connection);
        const char *db = realname ? realname : ecpg_gettext("<DEFAULT>");
 
-       ecpg_log("ECPGconnect: could not open database: %s\n", errmsg);
+       /* PQerrorMessage's result already has a trailing newline */
+       ecpg_log("ECPGconnect: %s", errmsg);
 
        ecpg_finish(this);
 #ifdef ENABLE_THREAD_SAFETY
index db3cd9c2285db21cd2f94d55f37ffc3c6dc05d1a..a15f3443204f0fa959f3d7ab51ee9320b0b063f3 100644 (file)
@@ -36,8 +36,7 @@
 [NO_PID]: sqlca: code: 0, state: 00000
 [NO_PID]: ECPGconnect: opening database <DEFAULT> on <DEFAULT> port <DEFAULT>  for user regress_ecpg_user2
 [NO_PID]: sqlca: code: 0, state: 00000
-[NO_PID]: ECPGconnect: could not open database: connection to server failed: FATAL:  database "regress_ecpg_user2" does not exist
-
+[NO_PID]: ECPGconnect: connection to server failed: FATAL:  database "regress_ecpg_user2" does not exist
 [NO_PID]: sqlca: code: 0, state: 00000
 [NO_PID]: ecpg_finish: connection main closed
 [NO_PID]: sqlca: code: 0, state: 00000
@@ -73,8 +72,7 @@
 [NO_PID]: sqlca: code: -220, state: 08003
 [NO_PID]: ECPGconnect: opening database <DEFAULT> on <DEFAULT> port <DEFAULT>  for user regress_ecpg_user2
 [NO_PID]: sqlca: code: 0, state: 00000
-[NO_PID]: ECPGconnect: could not open database: connection to server failed: FATAL:  database "regress_ecpg_user2" does not exist
-
+[NO_PID]: ECPGconnect: connection to server failed: FATAL:  database "regress_ecpg_user2" does not exist
 [NO_PID]: sqlca: code: 0, state: 00000
 [NO_PID]: ecpg_finish: connection main closed
 [NO_PID]: sqlca: code: 0, state: 00000
index 18c98083de94ddd90ccc430c849dd5ba6787f503..0372781eaf27b9bebdcd09d2fb5b6c5c0981f256 100644 (file)
@@ -43,8 +43,7 @@ main(int argc, char **argv)
    /* Check to see that the backend connection was successfully made */
    if (PQstatus(conn) != CONNECTION_OK)
    {
-       fprintf(stderr, "Connection to database failed: %s",
-               PQerrorMessage(conn));
+       fprintf(stderr, "%s", PQerrorMessage(conn));
        exit_nicely(conn);
    }
 
index 511246763a6b847e233a614804d5159290769794..6337b315a42996d2610fe4204ba39f4cfc136eab 100644 (file)
@@ -72,8 +72,7 @@ main(int argc, char **argv)
    /* Check to see that the backend connection was successfully made */
    if (PQstatus(conn) != CONNECTION_OK)
    {
-       fprintf(stderr, "Connection to database failed: %s",
-               PQerrorMessage(conn));
+       fprintf(stderr, "%s", PQerrorMessage(conn));
        exit_nicely(conn);
    }
 
index dda45af859ab550b8c259d182a69c8d17ae4f3dd..4f7b79138897563a160e14fd0d72cf89e7a6e6ca 100644 (file)
@@ -138,8 +138,7 @@ main(int argc, char **argv)
    /* Check to see that the backend connection was successfully made */
    if (PQstatus(conn) != CONNECTION_OK)
    {
-       fprintf(stderr, "Connection to database failed: %s",
-               PQerrorMessage(conn));
+       fprintf(stderr, "%s", PQerrorMessage(conn));
        exit_nicely(conn);
    }
 
index df8e454b5dfcf47cb504a91be6bb44a44edc07b2..dd11bbc46dc3d895da5f604f1781705302c2feb5 100644 (file)
@@ -29,8 +29,7 @@ check_prepare_conn(PGconn *conn, const char *dbName)
    /* check to see that the backend connection was successfully made */
    if (PQstatus(conn) != CONNECTION_OK)
    {
-       fprintf(stderr, "Connection to database \"%s\" failed: %s",
-               dbName, PQerrorMessage(conn));
+       fprintf(stderr, "%s", PQerrorMessage(conn));
        exit(1);
    }
 
index fa8da58e1b37933c43dd0e0169940bd6db214543..6d91681bcf8291b1ea2b8dfe9c107df7fa601fed 100644 (file)
@@ -225,8 +225,7 @@ main(int argc, char **argv)
    /* check to see that the backend connection was successfully made */
    if (PQstatus(conn) != CONNECTION_OK)
    {
-       fprintf(stderr, "Connection to database failed: %s",
-               PQerrorMessage(conn));
+       fprintf(stderr, "%s", PQerrorMessage(conn));
        exit_nicely(conn);
    }
 
index 6334171163ae278c5a5bbbebf48d266b4aefbc27..23e910944683fdc1f78695477421cd953af98788 100644 (file)
@@ -249,8 +249,7 @@ main(int argc, char **argv)
    /* check to see that the backend connection was successfully made */
    if (PQstatus(conn) != CONNECTION_OK)
    {
-       fprintf(stderr, "Connection to database failed: %s",
-               PQerrorMessage(conn));
+       fprintf(stderr, "%s", PQerrorMessage(conn));
        exit_nicely(conn);
    }
 
index f80261c022950afd4d9174740783975b7bc81198..0a73d38daeb39a8bbb1908e18c5d53d6754a5166 100644 (file)
@@ -167,7 +167,7 @@ main(int argc, char **argv)
        conns[i] = PQconnectdb(conninfo);
        if (PQstatus(conns[i]) != CONNECTION_OK)
        {
-           fprintf(stderr, "Connection %d to database failed: %s",
+           fprintf(stderr, "Connection %d failed: %s",
                    i, PQerrorMessage(conns[i]));
            exit(1);
        }
index a42c8a34da2fc520c95331d7ac86807bf467aad6..f882c8b0ef244fae009beee99c3bd5b18d24310d 100644 (file)
@@ -44,7 +44,7 @@ main(int argc, char **argv)
    conn = PQconnectdb(sql.data);
    if (PQstatus(conn) == CONNECTION_BAD)
    {
-       fprintf(stderr, "connection error:  %s\n", PQerrorMessage(conn));
+       fprintf(stderr, "%s", PQerrorMessage(conn));
        exit(EXIT_FAILURE);
    }