1 /*-------------------------------------------------------------------------
3 * Facilities for frontend code to query a databases.
5 * Portions Copyright (c) 1996-2021, 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.
22 executeQuery(PGconn
*conn
, const char *query
, bool echo
)
27 printf("%s\n", query
);
29 res
= PQexec(conn
, query
);
31 PQresultStatus(res
) != PGRES_TUPLES_OK
)
33 pg_log_error("query failed: %s", PQerrorMessage(conn
));
34 pg_log_info("query was: %s", query
);
44 * As above for a SQL command (which returns nothing).
47 executeCommand(PGconn
*conn
, const char *query
, bool echo
)
52 printf("%s\n", query
);
54 res
= PQexec(conn
, query
);
56 PQresultStatus(res
) != PGRES_COMMAND_OK
)
58 pg_log_error("query failed: %s", PQerrorMessage(conn
));
59 pg_log_info("query was: %s", query
);
69 * As above for a SQL maintenance command (returns command success).
70 * Command is executed with a cancel handler set, so Ctrl-C can
74 executeMaintenanceCommand(PGconn
*conn
, const char *query
, bool echo
)
80 printf("%s\n", query
);
83 res
= PQexec(conn
, query
);
86 r
= (res
&& PQresultStatus(res
) == PGRES_COMMAND_OK
);