PostgreSQL Source Code  git master
query_utils.h File Reference
#include "postgres_fe.h"
#include "libpq-fe.h"
Include dependency graph for query_utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

PGresultexecuteQuery (PGconn *conn, const char *query, bool echo)
 
void executeCommand (PGconn *conn, const char *query, bool echo)
 
bool executeMaintenanceCommand (PGconn *conn, const char *query, bool echo)
 

Function Documentation

◆ executeCommand()

void executeCommand ( PGconn conn,
const char *  query,
bool  echo 
)

Definition at line 47 of file query_utils.c.

References pg_log_error, pg_log_info, PGRES_COMMAND_OK, PQclear(), PQerrorMessage(), PQexec(), PQfinish(), PQresultStatus(), and printf.

48 {
49  PGresult *res;
50 
51  if (echo)
52  printf("%s\n", query);
53 
54  res = PQexec(conn, query);
55  if (!res ||
57  {
58  pg_log_error("query failed: %s", PQerrorMessage(conn));
59  pg_log_info("query was: %s", query);
60  PQfinish(conn);
61  exit(1);
62  }
63 
64  PQclear(res);
65 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6735
#define pg_log_error(...)
Definition: logging.h:80
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4229
#define printf(...)
Definition: port.h:222
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3097
void PQclear(PGresult *res)
Definition: fe-exec.c:680
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2142
#define pg_log_info(...)
Definition: logging.h:88

◆ executeMaintenanceCommand()

bool executeMaintenanceCommand ( PGconn conn,
const char *  query,
bool  echo 
)

Definition at line 74 of file query_utils.c.

References PGRES_COMMAND_OK, PQclear(), PQexec(), PQresultStatus(), printf, ResetCancelConn(), and SetCancelConn().

Referenced by cluster_one_database(), and run_reindex_command().

75 {
76  PGresult *res;
77  bool r;
78 
79  if (echo)
80  printf("%s\n", query);
81 
82  SetCancelConn(conn);
83  res = PQexec(conn, query);
85 
86  r = (res && PQresultStatus(res) == PGRES_COMMAND_OK);
87 
88  if (res)
89  PQclear(res);
90 
91  return r;
92 }
void ResetCancelConn(void)
Definition: cancel.c:100
#define printf(...)
Definition: port.h:222
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3097
void SetCancelConn(PGconn *conn)
Definition: cancel.c:70
void PQclear(PGresult *res)
Definition: fe-exec.c:680
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2142

◆ executeQuery()

PGresult* executeQuery ( PGconn conn,
const char *  query,
bool  echo 
)

Definition at line 22 of file query_utils.c.

References pg_log_error, pg_log_info, PGRES_TUPLES_OK, PQerrorMessage(), PQexec(), PQfinish(), PQresultStatus(), and printf.

23 {
24  PGresult *res;
25 
26  if (echo)
27  printf("%s\n", query);
28 
29  res = PQexec(conn, query);
30  if (!res ||
32  {
33  pg_log_error("query failed: %s", PQerrorMessage(conn));
34  pg_log_info("query was: %s", query);
35  PQfinish(conn);
36  exit(1);
37  }
38 
39  return res;
40 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6735
#define pg_log_error(...)
Definition: logging.h:80
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4229
#define printf(...)
Definition: port.h:222
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3097
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2142
#define pg_log_info(...)
Definition: logging.h:88