Adjust some comments about structure properties in pg_stat.h
[pgsql.git] / src / fe_utils / query_utils.c
blob4970c4575da856b495959832b17236247ee02ead
1 /*-------------------------------------------------------------------------
3 * Facilities for frontend code to query a databases.
5 * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
6 * Portions Copyright (c) 1994, Regents of the University of California
8 * src/fe_utils/query_utils.c
10 *-------------------------------------------------------------------------
12 #include "postgres_fe.h"
14 #include "common/logging.h"
15 #include "fe_utils/cancel.h"
16 #include "fe_utils/query_utils.h"
19 * Run a query, return the results, exit program on failure.
21 PGresult *
22 executeQuery(PGconn *conn, const char *query, bool echo)
24 PGresult *res;
26 if (echo)
27 printf("%s\n", query);
29 res = PQexec(conn, query);
30 if (!res ||
31 PQresultStatus(res) != PGRES_TUPLES_OK)
33 pg_log_error("query failed: %s", PQerrorMessage(conn));
34 pg_log_error_detail("Query was: %s", query);
35 PQfinish(conn);
36 exit(1);
39 return res;
44 * As above for a SQL command (which returns nothing).
46 void
47 executeCommand(PGconn *conn, const char *query, bool echo)
49 PGresult *res;
51 if (echo)
52 printf("%s\n", query);
54 res = PQexec(conn, query);
55 if (!res ||
56 PQresultStatus(res) != PGRES_COMMAND_OK)
58 pg_log_error("query failed: %s", PQerrorMessage(conn));
59 pg_log_error_detail("Query was: %s", query);
60 PQfinish(conn);
61 exit(1);
64 PQclear(res);
69 * As above for a SQL maintenance command (returns command success).
70 * Command is executed with a cancel handler set, so Ctrl-C can
71 * interrupt it.
73 bool
74 executeMaintenanceCommand(PGconn *conn, const char *query, bool echo)
76 PGresult *res;
77 bool r;
79 if (echo)
80 printf("%s\n", query);
82 SetCancelConn(conn);
83 res = PQexec(conn, query);
84 ResetCancelConn();
86 r = (res && PQresultStatus(res) == PGRES_COMMAND_OK);
88 PQclear(res);
90 return r;