diff options
author | Robert Haas <rhaas@postgresql.org> | 2021-02-05 13:33:38 -0500 |
---|---|---|
committer | Robert Haas <rhaas@postgresql.org> | 2021-02-05 13:33:38 -0500 |
commit | e955bd4b6c2bcdbd253837f6cf4c7520b98e69d4 (patch) | |
tree | bc8e70ed9fbe9bee458299dc6f76b20311185cf7 /src/fe_utils/query_utils.c | |
parent | c444472af5c202067a9ecb0ff8df7370fb1ea8f4 (diff) | |
download | postgresql-e955bd4b6c2bcdbd253837f6cf4c7520b98e69d4.tar.gz postgresql-e955bd4b6c2bcdbd253837f6cf4c7520b98e69d4.zip |
Move some code from src/bin/scripts to src/fe_utils to permit reuse.
The parallel slots infrastructure (which implements client-side
multiplexing of server connections doing similar things, not
threading or multiple processes or anything like that) are moved from
src/bin/scripts/scripts_parallel.c to src/fe_utils/parallel_slot.c.
The functions consumeQueryResult() and processQueryResult() which were
previously part of src/bin/scripts/common.c are now moved into that
file as well, becoming static helper functions. This might need to be
changed in the future, but currently they're not used for anything
else.
Some other functions from src/bin/scripts/common.c are moved to to
src/fe_utils and are split up among several files. connectDatabase(),
connectMaintenanceDatabase(), and disconnectDatabase() are moved to
connect_utils.c. executeQuery(), executeCommand(), and
executeMaintenanceCommand() are move to query_utils.c.
handle_help_version_opts() is moved to option_utils.c.
Mark Dilger, reviewed by me. The larger patch series of which this is
a part has also had review from Peter Geoghegan, Andres Freund, Álvaro
Herrera, Michael Paquier, and Amul Sul, but I don't know whether any
of them have reviewed this bit specifically.
Discussion: http://postgr.es/m/12ED3DA8-25F0-4B68-937D-D907CFBF08E7@enterprisedb.com
Discussion: http://postgr.es/m/5F743835-3399-419C-8324-2D424237E999@enterprisedb.com
Discussion: http://postgr.es/m/70655DF3-33CE-4527-9A4D-DDEB582B6BA0@enterprisedb.com
Diffstat (limited to 'src/fe_utils/query_utils.c')
-rw-r--r-- | src/fe_utils/query_utils.c | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/fe_utils/query_utils.c b/src/fe_utils/query_utils.c new file mode 100644 index 00000000000..d5ffe56fd68 --- /dev/null +++ b/src/fe_utils/query_utils.c @@ -0,0 +1,92 @@ +/*------------------------------------------------------------------------- + * + * Facilities for frontend code to query a databases. + * + * Portions Copyright (c) 1996-2021, 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_info("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_info("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; +} |