mirror of
https://github.com/postgres/postgres.git
synced 2025-07-02 09:02:37 +03:00
Improve frontend error logging style.
Get rid of the separate "FATAL" log level, as it was applied so inconsistently as to be meaningless. This mostly involves s/pg_log_fatal/pg_log_error/g. Create a macro pg_fatal() to handle the common use-case of pg_log_error() immediately followed by exit(1). Various modules had already invented either this or equivalent macros; standardize on pg_fatal() and apply it where possible. Invent the ability to add "detail" and "hint" messages to a frontend message, much as we have long had in the backend. Except where rewording was needed to convert existing coding to detail/hint style, I have (mostly) resisted the temptation to change existing message wording. Patch by me. Design and patch reviewed at various stages by Robert Haas, Kyotaro Horiguchi, Peter Eisentraut and Daniel Gustafsson. Discussion: https://postgr.es/m/1363732.1636496441@sss.pgh.pa.us
This commit is contained in:
@ -49,10 +49,7 @@ RestoreArchivedFile(const char *path, const char *xlogfname,
|
||||
xlogRestoreCmd = BuildRestoreCommand(restoreCommand, xlogpath,
|
||||
xlogfname, NULL);
|
||||
if (xlogRestoreCmd == NULL)
|
||||
{
|
||||
pg_log_fatal("cannot use restore_command with %%r placeholder");
|
||||
exit(1);
|
||||
}
|
||||
pg_fatal("cannot use restore_command with %%r placeholder");
|
||||
|
||||
/*
|
||||
* Execute restore_command, which should copy the missing file from
|
||||
@ -70,22 +67,16 @@ RestoreArchivedFile(const char *path, const char *xlogfname,
|
||||
if (stat(xlogpath, &stat_buf) == 0)
|
||||
{
|
||||
if (expectedSize > 0 && stat_buf.st_size != expectedSize)
|
||||
{
|
||||
pg_log_fatal("unexpected file size for \"%s\": %lld instead of %lld",
|
||||
xlogfname, (long long int) stat_buf.st_size,
|
||||
(long long int) expectedSize);
|
||||
exit(1);
|
||||
}
|
||||
pg_fatal("unexpected file size for \"%s\": %lld instead of %lld",
|
||||
xlogfname, (long long int) stat_buf.st_size,
|
||||
(long long int) expectedSize);
|
||||
else
|
||||
{
|
||||
int xlogfd = open(xlogpath, O_RDONLY | PG_BINARY, 0);
|
||||
|
||||
if (xlogfd < 0)
|
||||
{
|
||||
pg_log_fatal("could not open file \"%s\" restored from archive: %m",
|
||||
xlogpath);
|
||||
exit(1);
|
||||
}
|
||||
pg_fatal("could not open file \"%s\" restored from archive: %m",
|
||||
xlogpath);
|
||||
else
|
||||
return xlogfd;
|
||||
}
|
||||
@ -93,11 +84,8 @@ RestoreArchivedFile(const char *path, const char *xlogfname,
|
||||
else
|
||||
{
|
||||
if (errno != ENOENT)
|
||||
{
|
||||
pg_log_fatal("could not stat file \"%s\": %m",
|
||||
xlogpath);
|
||||
exit(1);
|
||||
}
|
||||
pg_fatal("could not stat file \"%s\": %m",
|
||||
xlogpath);
|
||||
}
|
||||
}
|
||||
|
||||
@ -108,11 +96,8 @@ RestoreArchivedFile(const char *path, const char *xlogfname,
|
||||
* fatal too.
|
||||
*/
|
||||
if (wait_result_is_any_signal(rc, true))
|
||||
{
|
||||
pg_log_fatal("restore_command failed: %s",
|
||||
wait_result_to_str(rc));
|
||||
exit(1);
|
||||
}
|
||||
pg_fatal("restore_command failed: %s",
|
||||
wait_result_to_str(rc));
|
||||
|
||||
/*
|
||||
* The file is not available, so just let the caller decide what to do
|
||||
|
@ -88,11 +88,8 @@ connectDatabase(const ConnParams *cparams, const char *progname,
|
||||
conn = PQconnectdbParams(keywords, values, true);
|
||||
|
||||
if (!conn)
|
||||
{
|
||||
pg_log_error("could not connect to database %s: out of memory",
|
||||
cparams->dbname);
|
||||
exit(1);
|
||||
}
|
||||
pg_fatal("could not connect to database %s: out of memory",
|
||||
cparams->dbname);
|
||||
|
||||
/*
|
||||
* No luck? Trying asking (again) for a password.
|
||||
@ -117,8 +114,7 @@ connectDatabase(const ConnParams *cparams, const char *progname,
|
||||
PQfinish(conn);
|
||||
return NULL;
|
||||
}
|
||||
pg_log_error("%s", PQerrorMessage(conn));
|
||||
exit(1);
|
||||
pg_fatal("%s", PQerrorMessage(conn));
|
||||
}
|
||||
|
||||
/* Start strict; callers may override this. */
|
||||
|
@ -298,10 +298,7 @@ connect_slot(ParallelSlotArray *sa, int slotno, const char *dbname)
|
||||
sa->cparams->override_dbname = old_override;
|
||||
|
||||
if (PQsocket(slot->connection) >= FD_SETSIZE)
|
||||
{
|
||||
pg_log_fatal("too many jobs for this platform");
|
||||
exit(1);
|
||||
}
|
||||
pg_fatal("too many jobs for this platform");
|
||||
|
||||
/* Setup the connection using the supplied command, if any. */
|
||||
if (sa->initcmd)
|
||||
|
@ -31,7 +31,7 @@ executeQuery(PGconn *conn, const char *query, bool echo)
|
||||
PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
pg_log_error("query failed: %s", PQerrorMessage(conn));
|
||||
pg_log_info("query was: %s", query);
|
||||
pg_log_error_detail("Query was: %s", query);
|
||||
PQfinish(conn);
|
||||
exit(1);
|
||||
}
|
||||
@ -56,7 +56,7 @@ executeCommand(PGconn *conn, const char *query, bool echo)
|
||||
PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||
{
|
||||
pg_log_error("query failed: %s", PQerrorMessage(conn));
|
||||
pg_log_info("query was: %s", query);
|
||||
pg_log_error_detail("Query was: %s", query);
|
||||
PQfinish(conn);
|
||||
exit(1);
|
||||
}
|
||||
|
@ -31,10 +31,7 @@ GenerateRecoveryConfig(PGconn *pgconn, char *replication_slot)
|
||||
|
||||
contents = createPQExpBuffer();
|
||||
if (!contents)
|
||||
{
|
||||
pg_log_error("out of memory");
|
||||
exit(1);
|
||||
}
|
||||
pg_fatal("out of memory");
|
||||
|
||||
/*
|
||||
* In PostgreSQL 12 and newer versions, standby_mode is gone, replaced by
|
||||
@ -45,10 +42,7 @@ GenerateRecoveryConfig(PGconn *pgconn, char *replication_slot)
|
||||
|
||||
connOptions = PQconninfo(pgconn);
|
||||
if (connOptions == NULL)
|
||||
{
|
||||
pg_log_error("out of memory");
|
||||
exit(1);
|
||||
}
|
||||
pg_fatal("out of memory");
|
||||
|
||||
initPQExpBuffer(&conninfo_buf);
|
||||
for (PQconninfoOption *opt = connOptions; opt && opt->keyword; opt++)
|
||||
@ -73,10 +67,7 @@ GenerateRecoveryConfig(PGconn *pgconn, char *replication_slot)
|
||||
appendConnStrVal(&conninfo_buf, opt->val);
|
||||
}
|
||||
if (PQExpBufferDataBroken(conninfo_buf))
|
||||
{
|
||||
pg_log_error("out of memory");
|
||||
exit(1);
|
||||
}
|
||||
pg_fatal("out of memory");
|
||||
|
||||
/*
|
||||
* Escape the connection string, so that it can be put in the config file.
|
||||
@ -96,10 +87,7 @@ GenerateRecoveryConfig(PGconn *pgconn, char *replication_slot)
|
||||
}
|
||||
|
||||
if (PQExpBufferBroken(contents))
|
||||
{
|
||||
pg_log_error("out of memory");
|
||||
exit(1);
|
||||
}
|
||||
pg_fatal("out of memory");
|
||||
|
||||
PQconninfoFree(connOptions);
|
||||
|
||||
@ -130,16 +118,10 @@ WriteRecoveryConfig(PGconn *pgconn, char *target_dir, PQExpBuffer contents)
|
||||
|
||||
cf = fopen(filename, use_recovery_conf ? "w" : "a");
|
||||
if (cf == NULL)
|
||||
{
|
||||
pg_log_error("could not open file \"%s\": %m", filename);
|
||||
exit(1);
|
||||
}
|
||||
pg_fatal("could not open file \"%s\": %m", filename);
|
||||
|
||||
if (fwrite(contents->data, contents->len, 1, cf) != 1)
|
||||
{
|
||||
pg_log_error("could not write to file \"%s\": %m", filename);
|
||||
exit(1);
|
||||
}
|
||||
pg_fatal("could not write to file \"%s\": %m", filename);
|
||||
|
||||
fclose(cf);
|
||||
|
||||
@ -148,10 +130,7 @@ WriteRecoveryConfig(PGconn *pgconn, char *target_dir, PQExpBuffer contents)
|
||||
snprintf(filename, MAXPGPATH, "%s/%s", target_dir, "standby.signal");
|
||||
cf = fopen(filename, "w");
|
||||
if (cf == NULL)
|
||||
{
|
||||
pg_log_error("could not create file \"%s\": %m", filename);
|
||||
exit(1);
|
||||
}
|
||||
pg_fatal("could not create file \"%s\": %m", filename);
|
||||
|
||||
fclose(cf);
|
||||
}
|
||||
@ -167,9 +146,6 @@ escape_quotes(const char *src)
|
||||
char *result = escape_single_quotes_ascii(src);
|
||||
|
||||
if (!result)
|
||||
{
|
||||
pg_log_error("out of memory");
|
||||
exit(1);
|
||||
}
|
||||
pg_fatal("out of memory");
|
||||
return result;
|
||||
}
|
||||
|
Reference in New Issue
Block a user