diff --git a/dbutils.c b/dbutils.c index 28bc622d..63331513 100644 --- a/dbutils.c +++ b/dbutils.c @@ -3249,6 +3249,90 @@ _create_event(PGconn *conn, t_configuration_options *options, int node_id, char } +PGresult * +get_event_records(PGconn *conn, int node_id, const char *node_name, const char *event, bool all, int limit) +{ + PGresult *res; + + PQExpBufferData query; + PQExpBufferData where_clause; + + + initPQExpBuffer(&query); + initPQExpBuffer(&where_clause); + + /* LEFT JOIN used here as a node record may have been removed */ + appendPQExpBuffer(&query, + " SELECT e.node_id, n.node_name, e.event, e.successful, " + " TO_CHAR(e.event_timestamp, 'YYYY-MM-DD HH24:MI:SS') AS timestamp, " + " e.details " + " FROM repmgr.events e " + "LEFT JOIN repmgr.nodes n ON e.node_id = n.node_id "); + + if (node_id != UNKNOWN_NODE_ID) + { + append_where_clause(&where_clause, + "n.node_id=%i", node_id); + } + else if (node_name[0] != '\0') + { + char *escaped = escape_string(conn, node_name); + + if (escaped == NULL) + { + log_error(_("unable to escape value provided for node name")); + log_detail(_("node name is: \"%s\""), node_name); + } + else + { + append_where_clause(&where_clause, + "n.node_name='%s'", + escaped); + pfree(escaped); + } + } + + if (event[0] != '\0') + { + char *escaped = escape_string(conn, event); + + if (escaped == NULL) + { + log_error(_("unable to escape value provided for event")); + log_detail(_("event is: \"%s\""), event); + } + else + { + append_where_clause(&where_clause, + "e.event='%s'", + escaped); + pfree(escaped); + } + } + + appendPQExpBuffer(&query, "\n%s\n", + where_clause.data); + + appendPQExpBuffer(&query, + " ORDER BY e.event_timestamp DESC"); + + if (all == false && limit > 0) + { + appendPQExpBuffer(&query, " LIMIT %i", + limit); + } + + log_debug("do_cluster_event():\n%s", query.data); + res = PQexec(conn, query.data); + + termPQExpBuffer(&query); + termPQExpBuffer(&where_clause); + + + return res; +} + + /* ========================== */ /* replication slot functions */ /* ========================== */ diff --git a/dbutils.h b/dbutils.h index bd815790..37405c3c 100644 --- a/dbutils.h +++ b/dbutils.h @@ -369,10 +369,8 @@ bool check_cluster_schema(PGconn *conn); /* GUC manipulation functions */ bool set_config(PGconn *conn, const char *config_param, const char *config_value); bool set_config_bool(PGconn *conn, const char *config_param, bool state); -int guc_set(PGconn *conn, const char *parameter, const char *op, - const char *value); -int guc_set_typed(PGconn *conn, const char *parameter, const char *op, - const char *value, const char *datatype); +int guc_set(PGconn *conn, const char *parameter, const char *op, const char *value); +int guc_set_typed(PGconn *conn, const char *parameter, const char *op, const char *value, const char *datatype); bool get_pg_setting(PGconn *conn, const char *setting, char *output); /* server information functions */ @@ -437,6 +435,7 @@ void config_file_list_add(t_configfile_list *list, const char *file, const char bool create_event_record(PGconn *conn, t_configuration_options *options, int node_id, char *event, bool successful, char *details); bool create_event_notification(PGconn *conn, t_configuration_options *options, int node_id, char *event, bool successful, char *details); bool create_event_notification_extended(PGconn *conn, t_configuration_options *options, int node_id, char *event, bool successful, char *details, t_event_info *event_info); +PGresult *get_event_records(PGconn *conn, int node_id, const char *node_name, const char *event, bool all, int limit); /* replication slot functions */ bool create_replication_slot(PGconn *conn, char *slot_name, int server_version_num, PQExpBufferData *error_msg); diff --git a/repmgr-action-cluster.c b/repmgr-action-cluster.c index ed5d3a9c..7d196292 100644 --- a/repmgr-action-cluster.c +++ b/repmgr-action-cluster.c @@ -436,83 +436,18 @@ void do_cluster_event(void) { PGconn *conn = NULL; - PQExpBufferData query; - PQExpBufferData where_clause; PGresult *res; int i = 0; int column_count = EVENT_HEADER_COUNT; conn = establish_db_connection(config_file_options.conninfo, true); - initPQExpBuffer(&query); - initPQExpBuffer(&where_clause); - - /* LEFT JOIN used here as a node record may have been removed */ - appendPQExpBuffer( - &query, - " SELECT e.node_id, n.node_name, e.event, e.successful, \n" - " TO_CHAR(e.event_timestamp, 'YYYY-MM-DD HH24:MI:SS') AS timestamp, \n" - " e.details \n" - " FROM repmgr.events e \n" - "LEFT JOIN repmgr.nodes n ON e.node_id = n.node_id "); - - if (runtime_options.node_id != UNKNOWN_NODE_ID) - { - - append_where_clause(&where_clause, - "n.node_id=%i", runtime_options.node_id); - } - else if (runtime_options.node_name[0] != '\0') - { - char *escaped = escape_string(conn, runtime_options.node_name); - - if (escaped == NULL) - { - log_error(_("unable to escape value provided for node name")); - } - else - { - append_where_clause(&where_clause, - "n.node_name='%s'", - escaped); - pfree(escaped); - } - } - - if (runtime_options.event[0] != '\0') - { - char *escaped = escape_string(conn, runtime_options.event); - - if (escaped == NULL) - { - log_error(_("unable to escape value provided for event")); - } - else - { - append_where_clause(&where_clause, - "e.event='%s'", - escaped); - pfree(escaped); - } - } - - appendPQExpBuffer(&query, "\n%s\n", - where_clause.data); - - appendPQExpBuffer(&query, - " ORDER BY e.event_timestamp DESC"); - - if (runtime_options.all == false && runtime_options.limit > 0) - { - appendPQExpBuffer(&query, " LIMIT %i", - runtime_options.limit); - } - - log_debug("do_cluster_event():\n%s", query.data); - res = PQexec(conn, query.data); - - termPQExpBuffer(&query); - termPQExpBuffer(&where_clause); + res = get_event_records(conn, + runtime_options.node_id, + runtime_options.node_name, + runtime_options.event, + runtime_options.all, + runtime_options.limit); if (PQresultStatus(res) != PGRES_TUPLES_OK) {