1
0
mirror of https://github.com/postgres/postgres.git synced 2025-10-13 18:28:01 +03:00

chomp PQerrorMessage() in backend uses

PQerrorMessage() returns an error message with a trailing newline, but
in backend use (dblink, postgres_fdw, libpqwalreceiver), we want to have
the error message without that for emitting via ereport().  To simplify
that, add a function pchomp() that returns a pstrdup'ed string with the
trailing newline characters removed.
This commit is contained in:
Peter Eisentraut
2017-02-27 08:30:06 -05:00
parent 9fab40ad32
commit 2ed193c904
6 changed files with 40 additions and 36 deletions

View File

@@ -166,7 +166,7 @@ typedef struct remoteConnHashEnt
#define DBLINK_RES_INTERNALERROR(p2) \
do { \
msg = pstrdup(PQerrorMessage(conn)); \
msg = pchomp(PQerrorMessage(conn)); \
if (res) \
PQclear(res); \
elog(ERROR, "%s: %s", p2, msg); \
@@ -204,7 +204,7 @@ typedef struct remoteConnHashEnt
conn = PQconnectdb(connstr); \
if (PQstatus(conn) == CONNECTION_BAD) \
{ \
msg = pstrdup(PQerrorMessage(conn)); \
msg = pchomp(PQerrorMessage(conn)); \
PQfinish(conn); \
ereport(ERROR, \
(errcode(ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION), \
@@ -278,7 +278,7 @@ dblink_connect(PG_FUNCTION_ARGS)
if (PQstatus(conn) == CONNECTION_BAD)
{
msg = pstrdup(PQerrorMessage(conn));
msg = pchomp(PQerrorMessage(conn));
PQfinish(conn);
if (rconn)
pfree(rconn);
@@ -651,7 +651,7 @@ dblink_send_query(PG_FUNCTION_ARGS)
/* async query send */
retval = PQsendQuery(conn, sql);
if (retval != 1)
elog(NOTICE, "could not send query: %s", PQerrorMessage(conn));
elog(NOTICE, "could not send query: %s", pchomp(PQerrorMessage(conn)));
PG_RETURN_INT32(retval);
}
@@ -1087,7 +1087,7 @@ storeQueryResult(volatile storeInfo *sinfo, PGconn *conn, const char *sql)
PGresult *res;
if (!PQsendQuery(conn, sql))
elog(ERROR, "could not send query: %s", PQerrorMessage(conn));
elog(ERROR, "could not send query: %s", pchomp(PQerrorMessage(conn)));
if (!PQsetSingleRowMode(conn)) /* shouldn't fail */
elog(ERROR, "failed to set single-row mode for dblink query");
@@ -1370,7 +1370,7 @@ dblink_error_message(PG_FUNCTION_ARGS)
if (msg == NULL || msg[0] == '\0')
PG_RETURN_TEXT_P(cstring_to_text("OK"));
else
PG_RETURN_TEXT_P(cstring_to_text(msg));
PG_RETURN_TEXT_P(cstring_to_text(pchomp(msg)));
}
/*
@@ -2709,7 +2709,7 @@ dblink_res_error(PGconn *conn, const char *conname, PGresult *res,
* return NULL, not a PGresult at all.
*/
if (message_primary == NULL)
message_primary = PQerrorMessage(conn);
message_primary = pchomp(PQerrorMessage(conn));
if (res)
PQclear(res);

View File

@@ -377,7 +377,6 @@ FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[])
WHERE t.a > 7;
ERROR: could not establish connection
DETAIL: missing "=" after "myconn" in connection info string
-- create a named persistent connection
SELECT dblink_connect('myconn',connection_parameters());
dblink_connect
@@ -604,7 +603,6 @@ FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[])
WHERE t.a > 7;
ERROR: could not establish connection
DETAIL: missing "=" after "myconn" in connection info string
-- create a named persistent connection
SELECT dblink_connect('myconn',connection_parameters());
dblink_connect