1
0
mirror of https://github.com/postgres/postgres.git synced 2025-09-05 02:22:28 +03:00
Files
postgres/src/fe_utils/query_utils.c
Tom Lane 9a374b77fb 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
2022-04-08 14:55:14 -04:00

93 lines
1.8 KiB
C

/*-------------------------------------------------------------------------
*
* Facilities for frontend code to query a databases.
*
* Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* src/fe_utils/query_utils.c
*
*-------------------------------------------------------------------------
*/
#include "postgres_fe.h"
#include "common/logging.h"
#include "fe_utils/cancel.h"
#include "fe_utils/query_utils.h"
/*
* Run a query, return the results, exit program on failure.
*/
PGresult *
executeQuery(PGconn *conn, const char *query, bool echo)
{
PGresult *res;
if (echo)
printf("%s\n", query);
res = PQexec(conn, query);
if (!res ||
PQresultStatus(res) != PGRES_TUPLES_OK)
{
pg_log_error("query failed: %s", PQerrorMessage(conn));
pg_log_error_detail("Query was: %s", query);
PQfinish(conn);
exit(1);
}
return res;
}
/*
* As above for a SQL command (which returns nothing).
*/
void
executeCommand(PGconn *conn, const char *query, bool echo)
{
PGresult *res;
if (echo)
printf("%s\n", query);
res = PQexec(conn, query);
if (!res ||
PQresultStatus(res) != PGRES_COMMAND_OK)
{
pg_log_error("query failed: %s", PQerrorMessage(conn));
pg_log_error_detail("Query was: %s", query);
PQfinish(conn);
exit(1);
}
PQclear(res);
}
/*
* As above for a SQL maintenance command (returns command success).
* Command is executed with a cancel handler set, so Ctrl-C can
* interrupt it.
*/
bool
executeMaintenanceCommand(PGconn *conn, const char *query, bool echo)
{
PGresult *res;
bool r;
if (echo)
printf("%s\n", query);
SetCancelConn(conn);
res = PQexec(conn, query);
ResetCancelConn();
r = (res && PQresultStatus(res) == PGRES_COMMAND_OK);
if (res)
PQclear(res);
return r;
}