mirror of
https://github.com/EnterpriseDB/repmgr.git
synced 2026-03-23 07:06:30 +00:00
Fix schema quoting
There was a lot of duplicated/unused related to handling the schema name; consolidated and rationalised.
This commit is contained in:
64
dbutils.c
64
dbutils.c
@@ -25,6 +25,9 @@
|
||||
#include "strutil.h"
|
||||
#include "log.h"
|
||||
|
||||
char repmgr_schema[MAXLEN] = "";
|
||||
char repmgr_schema_quoted[MAXLEN] = "";
|
||||
|
||||
PGconn *
|
||||
establish_db_connection(const char *conninfo, const bool exit_on_error)
|
||||
{
|
||||
@@ -96,16 +99,22 @@ is_standby(PGconn *conn)
|
||||
}
|
||||
|
||||
|
||||
|
||||
int
|
||||
is_witness(PGconn *conn, char *schema, char *cluster, int node_id)
|
||||
is_witness(PGconn *conn, char *cluster, int node_id)
|
||||
{
|
||||
PGresult *res;
|
||||
int result = 0;
|
||||
char sqlquery[QUERY_STR_LEN];
|
||||
|
||||
sqlquery_snprintf(sqlquery, "SELECT witness from %s.repl_nodes where cluster = '%s' and id = %d",
|
||||
schema, cluster, node_id);
|
||||
sqlquery_snprintf(sqlquery,
|
||||
"SELECT witness "
|
||||
" FROM %s.repl_nodes "
|
||||
" WHERE cluster = '%s' "
|
||||
" AND id = %d ",
|
||||
get_repmgr_schema_quoted(conn),
|
||||
cluster,
|
||||
node_id);
|
||||
|
||||
res = PQexec(conn, sqlquery);
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
@@ -353,7 +362,7 @@ get_pg_setting(PGconn *conn, const char *setting, char *output)
|
||||
* connection string is placed there.
|
||||
*/
|
||||
PGconn *
|
||||
get_master_connection(PGconn *standby_conn, char *schema, char *cluster,
|
||||
get_master_connection(PGconn *standby_conn, char *cluster,
|
||||
int *master_id, char *master_conninfo_out)
|
||||
{
|
||||
PGconn *master_conn = NULL;
|
||||
@@ -362,7 +371,6 @@ get_master_connection(PGconn *standby_conn, char *schema, char *cluster,
|
||||
char sqlquery[QUERY_STR_LEN];
|
||||
char master_conninfo_stack[MAXCONNINFO];
|
||||
char *master_conninfo = &*master_conninfo_stack;
|
||||
char schema_quoted[MAXLEN];
|
||||
|
||||
int i;
|
||||
|
||||
@@ -373,26 +381,18 @@ get_master_connection(PGconn *standby_conn, char *schema, char *cluster,
|
||||
if (master_conninfo_out != NULL)
|
||||
master_conninfo = master_conninfo_out;
|
||||
|
||||
/*
|
||||
* XXX: This is copied in at least two other procedures
|
||||
*
|
||||
* Assemble the unquoted schema name
|
||||
*/
|
||||
{
|
||||
char *identifier = PQescapeIdentifier(standby_conn, schema,
|
||||
strlen(schema));
|
||||
|
||||
maxlen_snprintf(schema_quoted, "%s", identifier);
|
||||
PQfreemem(identifier);
|
||||
}
|
||||
|
||||
/* find all nodes belonging to this cluster */
|
||||
log_info(_("finding node list for cluster '%s'\n"),
|
||||
cluster);
|
||||
|
||||
sqlquery_snprintf(sqlquery, "SELECT id, conninfo FROM %s.repl_nodes "
|
||||
" WHERE cluster = '%s' and not witness",
|
||||
schema_quoted, cluster);
|
||||
sqlquery_snprintf(sqlquery,
|
||||
"SELECT id, conninfo "
|
||||
" FROM %s.repl_nodes "
|
||||
" WHERE cluster = '%s' "
|
||||
" AND NOT witness ",
|
||||
get_repmgr_schema_quoted(standby_conn),
|
||||
cluster);
|
||||
|
||||
res1 = PQexec(standby_conn, sqlquery);
|
||||
if (PQresultStatus(res1) != PGRES_TUPLES_OK)
|
||||
@@ -560,3 +560,25 @@ cancel_query(PGconn *conn, int timeout)
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
char *
|
||||
get_repmgr_schema(void)
|
||||
{
|
||||
return repmgr_schema;
|
||||
}
|
||||
|
||||
|
||||
char *
|
||||
get_repmgr_schema_quoted(PGconn *conn)
|
||||
{
|
||||
if(strcmp(repmgr_schema_quoted, "") == 0)
|
||||
{
|
||||
char *identifier = PQescapeIdentifier(conn, repmgr_schema,
|
||||
strlen(repmgr_schema));
|
||||
|
||||
maxlen_snprintf(repmgr_schema_quoted, "%s", identifier);
|
||||
PQfreemem(identifier);
|
||||
}
|
||||
|
||||
return repmgr_schema_quoted;
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ PGconn *establish_db_connection_by_params(const char *keywords[],
|
||||
const char *values[],
|
||||
const bool exit_on_error);
|
||||
int is_standby(PGconn *conn);
|
||||
int is_witness(PGconn *conn, char *schema, char *cluster, int node_id);
|
||||
int is_witness(PGconn *conn,char *cluster, int node_id);
|
||||
bool is_pgup(PGconn *conn, int timeout);
|
||||
int get_server_version(PGconn *conn, char *server_version);
|
||||
bool get_cluster_size(PGconn *conn, char *size);
|
||||
@@ -40,10 +40,12 @@ int guc_set_typed(PGconn *conn, const char *parameter, const char *op,
|
||||
const char *value, const char *datatype);
|
||||
|
||||
|
||||
PGconn *get_master_connection(PGconn *standby_conn, char *schema, char *cluster,
|
||||
PGconn *get_master_connection(PGconn *standby_conn, char *cluster,
|
||||
int *master_id, char *master_conninfo_out);
|
||||
|
||||
int wait_connection_availability(PGconn *conn, long long timeout);
|
||||
bool cancel_query(PGconn *conn, int timeout);
|
||||
char *get_repmgr_schema(void);
|
||||
char *get_repmgr_schema_quoted(PGconn *conn);
|
||||
|
||||
#endif
|
||||
|
||||
109
repmgr.c
109
repmgr.c
@@ -84,7 +84,6 @@ static void help(const char *progname);
|
||||
static const char *progname;
|
||||
static const char *keywords[6];
|
||||
static const char *values[6];
|
||||
char repmgr_schema[MAXLEN];
|
||||
bool need_a_node = true;
|
||||
|
||||
/* XXX This should be mapped into a command line option */
|
||||
@@ -387,7 +386,7 @@ main(int argc, char **argv)
|
||||
}
|
||||
}
|
||||
|
||||
/* Prepare the repmgr schema variable */
|
||||
/* Initialise the repmgr schema name */
|
||||
maxlen_snprintf(repmgr_schema, "%s%s", DEFAULT_REPMGR_SCHEMA_PREFIX,
|
||||
options.cluster_name);
|
||||
|
||||
@@ -440,8 +439,9 @@ do_cluster_show(void)
|
||||
conn = establish_db_connection(options.conninfo, true);
|
||||
|
||||
sqlquery_snprintf(sqlquery,
|
||||
"SELECT conninfo, witness FROM %s.repl_nodes ",
|
||||
repmgr_schema);
|
||||
"SELECT conninfo, witness "
|
||||
" FROM %s.repl_nodes ",
|
||||
get_repmgr_schema_quoted(conn));
|
||||
res = PQexec(conn, sqlquery);
|
||||
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
@@ -491,7 +491,7 @@ do_cluster_cleanup(void)
|
||||
|
||||
/* check if there is a master in this cluster */
|
||||
log_info(_("%s connecting to master database\n"), progname);
|
||||
master_conn = get_master_connection(conn, repmgr_schema, options.cluster_name,
|
||||
master_conn = get_master_connection(conn, options.cluster_name,
|
||||
&master_id, NULL);
|
||||
if (!master_conn)
|
||||
{
|
||||
@@ -506,14 +506,14 @@ do_cluster_cleanup(void)
|
||||
sqlquery_snprintf(sqlquery,
|
||||
"DELETE FROM %s.repl_monitor "
|
||||
" WHERE age(now(), last_monitor_time) >= '%d days'::interval ",
|
||||
repmgr_schema,
|
||||
get_repmgr_schema_quoted(master_conn),
|
||||
runtime_options.keep_history);
|
||||
}
|
||||
else
|
||||
{
|
||||
sqlquery_snprintf(sqlquery,
|
||||
"TRUNCATE TABLE %s.repl_monitor",
|
||||
repmgr_schema);
|
||||
get_repmgr_schema_quoted(master_conn));
|
||||
}
|
||||
res = PQexec(master_conn, sqlquery);
|
||||
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||
@@ -530,7 +530,7 @@ do_cluster_cleanup(void)
|
||||
* Let's VACUUM the table to avoid autovacuum to be launched in an
|
||||
* unexpected hour
|
||||
*/
|
||||
sqlquery_snprintf(sqlquery, "VACUUM %s.repl_monitor", repmgr_schema);
|
||||
sqlquery_snprintf(sqlquery, "VACUUM %s.repl_monitor", get_repmgr_schema_quoted(master_conn));
|
||||
res = PQexec(master_conn, sqlquery);
|
||||
|
||||
/* XXX There is any need to check this VACUUM happens without problems? */
|
||||
@@ -548,7 +548,6 @@ do_master_register(void)
|
||||
char sqlquery[QUERY_STR_LEN];
|
||||
|
||||
bool schema_exists = false;
|
||||
char schema_quoted[MAXLEN];
|
||||
int ret;
|
||||
|
||||
bool node_record_created;
|
||||
@@ -572,24 +571,12 @@ do_master_register(void)
|
||||
exit(ERR_BAD_CONFIG);
|
||||
}
|
||||
|
||||
/*
|
||||
* Assemble a quoted schema name XXX This is not currently used due to a
|
||||
* merge conflict, but probably should be
|
||||
*/
|
||||
if (false)
|
||||
{
|
||||
char *identifier = PQescapeIdentifier(conn, repmgr_schema,
|
||||
strlen(repmgr_schema));
|
||||
|
||||
maxlen_snprintf(schema_quoted, "%s", identifier);
|
||||
PQfreemem(identifier);
|
||||
}
|
||||
|
||||
/* Check if there is a schema for this cluster */
|
||||
sqlquery_snprintf(sqlquery,
|
||||
"SELECT 1 FROM pg_namespace "
|
||||
"WHERE nspname = '%s' ",
|
||||
repmgr_schema);
|
||||
get_repmgr_schema());
|
||||
log_debug(_("master register: %s\n"), sqlquery);
|
||||
res = PQexec(conn, sqlquery);
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
@@ -604,7 +591,7 @@ do_master_register(void)
|
||||
{
|
||||
if (!runtime_options.force) /* and we are not forcing so error */
|
||||
{
|
||||
log_notice(_("Schema %s already exists.\n"), repmgr_schema);
|
||||
log_notice(_("Schema %s already exists.\n"), get_repmgr_schema());
|
||||
PQclear(res);
|
||||
PQfinish(conn);
|
||||
exit(ERR_BAD_CONFIG);
|
||||
@@ -616,7 +603,7 @@ do_master_register(void)
|
||||
if (!schema_exists)
|
||||
{
|
||||
log_info(_("master register: creating database objects inside the %s schema\n"),
|
||||
repmgr_schema);
|
||||
get_repmgr_schema());
|
||||
|
||||
/* ok, create the schema */
|
||||
if (!create_schema(conn))
|
||||
@@ -632,7 +619,7 @@ do_master_register(void)
|
||||
sqlquery_snprintf(sqlquery,
|
||||
"DELETE FROM %s.repl_nodes "
|
||||
" WHERE id = %d ",
|
||||
repmgr_schema,
|
||||
get_repmgr_schema_quoted(conn),
|
||||
options.node);
|
||||
log_debug(_("master register: %s\n"), sqlquery);
|
||||
|
||||
@@ -648,7 +635,7 @@ do_master_register(void)
|
||||
}
|
||||
|
||||
/* Ensure there isn't any other master already registered */
|
||||
master_conn = get_master_connection(conn, repmgr_schema,
|
||||
master_conn = get_master_connection(conn,
|
||||
options.cluster_name, &id, NULL);
|
||||
if (master_conn != NULL)
|
||||
{
|
||||
@@ -690,7 +677,6 @@ do_standby_register(void)
|
||||
|
||||
PGresult *res;
|
||||
char sqlquery[QUERY_STR_LEN];
|
||||
char schema_quoted[MAXLEN];
|
||||
|
||||
char master_version[MAXVERSIONSTR];
|
||||
int master_version_num = 0;
|
||||
@@ -719,23 +705,10 @@ do_standby_register(void)
|
||||
exit(ERR_BAD_CONFIG);
|
||||
}
|
||||
|
||||
/*
|
||||
* Assemble a quoted schema name XXX This is not currently used due to a
|
||||
* merge conflict, but probably should be
|
||||
*/
|
||||
if (false)
|
||||
{
|
||||
char *identifier = PQescapeIdentifier(conn, repmgr_schema,
|
||||
strlen(repmgr_schema));
|
||||
|
||||
maxlen_snprintf(schema_quoted, "%s", identifier);
|
||||
PQfreemem(identifier);
|
||||
}
|
||||
|
||||
/* Check if there is a schema for this cluster */
|
||||
sqlquery_snprintf(sqlquery,
|
||||
"SELECT 1 FROM pg_namespace WHERE nspname = '%s'",
|
||||
repmgr_schema);
|
||||
get_repmgr_schema());
|
||||
log_debug(_("standby register: %s\n"), sqlquery);
|
||||
res = PQexec(conn, sqlquery);
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
@@ -751,7 +724,7 @@ do_standby_register(void)
|
||||
if (PQntuples(res) == 0)
|
||||
{
|
||||
/* schema doesn't exist */
|
||||
log_err(_("Schema %s doesn't exist.\n"), repmgr_schema);
|
||||
log_err(_("Schema %s doesn't exist.\n"), get_repmgr_schema());
|
||||
PQclear(res);
|
||||
PQfinish(conn);
|
||||
exit(ERR_BAD_CONFIG);
|
||||
@@ -760,7 +733,7 @@ do_standby_register(void)
|
||||
|
||||
/* check if there is a master in this cluster */
|
||||
log_info(_("%s connecting to master database\n"), progname);
|
||||
master_conn = get_master_connection(conn, repmgr_schema, options.cluster_name,
|
||||
master_conn = get_master_connection(conn, options.cluster_name,
|
||||
&master_id, NULL);
|
||||
if (!master_conn)
|
||||
{
|
||||
@@ -795,7 +768,7 @@ do_standby_register(void)
|
||||
sqlquery_snprintf(sqlquery,
|
||||
"DELETE FROM %s.repl_nodes "
|
||||
" WHERE id = %d",
|
||||
repmgr_schema,
|
||||
get_repmgr_schema_quoted(master_conn),
|
||||
options.node);
|
||||
|
||||
log_debug(_("standby register: %s\n"), sqlquery);
|
||||
@@ -1197,8 +1170,8 @@ do_standby_promote(void)
|
||||
}
|
||||
|
||||
/* we also need to check if there isn't any master already */
|
||||
old_master_conn = get_master_connection(conn, repmgr_schema,
|
||||
options.cluster_name, &old_master_id, NULL);
|
||||
old_master_conn = get_master_connection(conn,
|
||||
options.cluster_name, &old_master_id, NULL);
|
||||
if (old_master_conn != NULL)
|
||||
{
|
||||
log_err(_("This cluster already has an active master server\n"));
|
||||
@@ -1326,7 +1299,7 @@ do_standby_follow(void)
|
||||
conn = establish_db_connection(options.conninfo, true);
|
||||
}
|
||||
|
||||
master_conn = get_master_connection(conn, repmgr_schema,
|
||||
master_conn = get_master_connection(conn,
|
||||
options.cluster_name, &master_id, (char *) &master_conninfo);
|
||||
}
|
||||
while (master_conn == NULL && runtime_options.wait_for_master);
|
||||
@@ -2049,13 +2022,13 @@ create_schema(PGconn *conn)
|
||||
char sqlquery[QUERY_STR_LEN];
|
||||
PGresult *res;
|
||||
|
||||
sqlquery_snprintf(sqlquery, "CREATE SCHEMA %s", repmgr_schema);
|
||||
sqlquery_snprintf(sqlquery, "CREATE SCHEMA %s", get_repmgr_schema_quoted(conn));
|
||||
log_debug(_("master register: %s\n"), sqlquery);
|
||||
res = PQexec(conn, sqlquery);
|
||||
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||
{
|
||||
log_err(_("Cannot create the schema %s: %s\n"),
|
||||
repmgr_schema, PQerrorMessage(conn));
|
||||
get_repmgr_schema(), PQerrorMessage(conn));
|
||||
PQfinish(conn);
|
||||
exit(ERR_BAD_CONFIG);
|
||||
}
|
||||
@@ -2070,7 +2043,7 @@ create_schema(PGconn *conn)
|
||||
" RETURNS TIMESTAMP WITH TIME ZONE "
|
||||
" AS '$libdir/repmgr_funcs', 'repmgr_update_last_updated' "
|
||||
" LANGUAGE C STRICT ",
|
||||
repmgr_schema);
|
||||
get_repmgr_schema_quoted(conn));
|
||||
|
||||
res = PQexec(conn, sqlquery);
|
||||
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||
@@ -2087,7 +2060,7 @@ create_schema(PGconn *conn)
|
||||
" RETURNS TIMESTAMP WITH TIME ZONE "
|
||||
" AS '$libdir/repmgr_funcs', 'repmgr_get_last_updated' "
|
||||
" LANGUAGE C STRICT ",
|
||||
repmgr_schema);
|
||||
get_repmgr_schema_quoted(conn));
|
||||
|
||||
res = PQexec(conn, sqlquery);
|
||||
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||
@@ -2108,14 +2081,14 @@ create_schema(PGconn *conn)
|
||||
" conninfo TEXT NOT NULL, "
|
||||
" priority INTEGER NOT NULL, "
|
||||
" witness BOOLEAN NOT NULL DEFAULT FALSE) ",
|
||||
repmgr_schema);
|
||||
get_repmgr_schema_quoted(conn));
|
||||
|
||||
log_debug(_("master register: %s\n"), sqlquery);
|
||||
res = PQexec(conn, sqlquery);
|
||||
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||
{
|
||||
log_err(_("Cannot create the table %s.repl_nodes: %s\n"),
|
||||
repmgr_schema, PQerrorMessage(conn));
|
||||
get_repmgr_schema_quoted(conn), PQerrorMessage(conn));
|
||||
PQfinish(conn);
|
||||
exit(ERR_BAD_CONFIG);
|
||||
}
|
||||
@@ -2131,13 +2104,13 @@ create_schema(PGconn *conn)
|
||||
" last_wal_standby_location TEXT, "
|
||||
" replication_lag BIGINT NOT NULL, "
|
||||
" apply_lag BIGINT NOT NULL) ",
|
||||
repmgr_schema);
|
||||
get_repmgr_schema_quoted(conn));
|
||||
log_debug(_("master register: %s\n"), sqlquery);
|
||||
res = PQexec(conn, sqlquery);
|
||||
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||
{
|
||||
log_err(_("Cannot create the table %s.repl_monitor: %s\n"),
|
||||
repmgr_schema, PQerrorMessage(conn));
|
||||
get_repmgr_schema_quoted(conn), PQerrorMessage(conn));
|
||||
PQfinish(conn);
|
||||
exit(ERR_BAD_CONFIG);
|
||||
}
|
||||
@@ -2158,15 +2131,18 @@ create_schema(PGconn *conn)
|
||||
" SELECT standby_node, MAX(last_monitor_time) "
|
||||
" FROM %s.repl_monitor GROUP BY 1 "
|
||||
" )",
|
||||
repmgr_schema, repmgr_schema, repmgr_schema,
|
||||
repmgr_schema, repmgr_schema);
|
||||
get_repmgr_schema_quoted(conn),
|
||||
get_repmgr_schema_quoted(conn),
|
||||
get_repmgr_schema_quoted(conn),
|
||||
get_repmgr_schema_quoted(conn),
|
||||
get_repmgr_schema_quoted(conn));
|
||||
log_debug(_("master register: %s\n"), sqlquery);
|
||||
|
||||
res = PQexec(conn, sqlquery);
|
||||
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||
{
|
||||
log_err(_("Cannot create the view %s.repl_status: %s\n"),
|
||||
repmgr_schema, PQerrorMessage(conn));
|
||||
get_repmgr_schema_quoted(conn), PQerrorMessage(conn));
|
||||
PQfinish(conn);
|
||||
exit(ERR_BAD_CONFIG);
|
||||
}
|
||||
@@ -2176,14 +2152,14 @@ create_schema(PGconn *conn)
|
||||
sqlquery_snprintf(sqlquery,
|
||||
"CREATE INDEX idx_repl_status_sort "
|
||||
" ON %s.repl_monitor (last_monitor_time, standby_node) ",
|
||||
repmgr_schema);
|
||||
get_repmgr_schema_quoted(conn));
|
||||
|
||||
log_debug(_("master register: %s\n"), sqlquery);
|
||||
res = PQexec(conn, sqlquery);
|
||||
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||
{
|
||||
log_err(_("Can't index table %s.repl_monitor: %s\n"),
|
||||
repmgr_schema, PQerrorMessage(conn));
|
||||
get_repmgr_schema_quoted(conn), PQerrorMessage(conn));
|
||||
PQfinish(conn);
|
||||
exit(ERR_BAD_CONFIG);
|
||||
}
|
||||
@@ -2194,11 +2170,11 @@ create_schema(PGconn *conn)
|
||||
* here
|
||||
*/
|
||||
sqlquery_snprintf(sqlquery,
|
||||
"CREATE OR REPLACE FUNCTION %s.repmgr_update_standby_location(text) "
|
||||
"CREATE OR REPLACE FUNCTION %s.re_pmgr_update_standby_location(text) "
|
||||
" RETURNS boolean "
|
||||
" AS '$libdir/repmgr_funcs', 'repmgr_update_standby_location' "
|
||||
" LANGUAGE C STRICT ",
|
||||
repmgr_schema);
|
||||
get_repmgr_schema_quoted(conn));
|
||||
|
||||
res = PQexec(conn, sqlquery);
|
||||
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||
@@ -2214,7 +2190,7 @@ create_schema(PGconn *conn)
|
||||
" RETURNS text "
|
||||
" AS '$libdir/repmgr_funcs', 'repmgr_get_last_standby_location' "
|
||||
" LANGUAGE C STRICT ",
|
||||
repmgr_schema);
|
||||
get_repmgr_schema_quoted(conn));
|
||||
|
||||
res = PQexec(conn, sqlquery);
|
||||
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||
@@ -2240,7 +2216,7 @@ copy_configuration(PGconn *masterconn, PGconn *witnessconn)
|
||||
PGresult *res;
|
||||
int i;
|
||||
|
||||
sqlquery_snprintf(sqlquery, "TRUNCATE TABLE %s.repl_nodes", repmgr_schema);
|
||||
sqlquery_snprintf(sqlquery, "TRUNCATE TABLE %s.repl_nodes", get_repmgr_schema_quoted(witnessconn));
|
||||
log_debug("copy_configuration: %s\n", sqlquery);
|
||||
res = PQexec(witnessconn, sqlquery);
|
||||
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||
@@ -2252,7 +2228,7 @@ copy_configuration(PGconn *masterconn, PGconn *witnessconn)
|
||||
|
||||
sqlquery_snprintf(sqlquery,
|
||||
"SELECT id, name, conninfo, priority, witness FROM %s.repl_nodes",
|
||||
repmgr_schema);
|
||||
get_repmgr_schema_quoted(masterconn));
|
||||
res = PQexec(masterconn, sqlquery);
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
@@ -2546,7 +2522,7 @@ create_node_record(PGconn *conn, char *action, int node, char *cluster_name, cha
|
||||
"INSERT INTO %s.repl_nodes "
|
||||
" (id, cluster, name, conninfo, priority, witness) "
|
||||
"VALUES (%d, '%s', '%s', '%s', %d, %s) ",
|
||||
repmgr_schema,
|
||||
get_repmgr_schema_quoted(conn),
|
||||
node,
|
||||
cluster_name,
|
||||
node_name,
|
||||
@@ -2571,3 +2547,4 @@ create_node_record(PGconn *conn, char *action, int node, char *cluster_name, cha
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
2
repmgr.h
2
repmgr.h
@@ -27,6 +27,7 @@
|
||||
#include "strutil.h"
|
||||
#include "dbutils.h"
|
||||
#include "errcode.h"
|
||||
#include "config.h"
|
||||
|
||||
#define PRIMARY_MODE 0
|
||||
#define STANDBY_MODE 1
|
||||
@@ -81,4 +82,5 @@ typedef struct
|
||||
|
||||
#define T_RUNTIME_OPTIONS_INITIALIZER { "", "", "", "", "", "", "", DEFAULT_WAL_KEEP_SEGMENTS, false, false, false, false, false, "", "", 0, "" }
|
||||
|
||||
extern char repmgr_schema[MAXLEN];
|
||||
#endif
|
||||
|
||||
53
repmgrd.c
53
repmgrd.c
@@ -80,7 +80,6 @@ const char *progname;
|
||||
char *config_file = DEFAULT_CONFIG_FILE;
|
||||
bool verbose = false;
|
||||
bool monitoring_history = false;
|
||||
char repmgr_schema[MAXLEN];
|
||||
|
||||
bool failover_done = false;
|
||||
|
||||
@@ -253,7 +252,8 @@ main(int argc, char **argv)
|
||||
}
|
||||
}
|
||||
|
||||
xsnprintf(repmgr_schema, MAXLEN, "%s%s", DEFAULT_REPMGR_SCHEMA_PREFIX,
|
||||
/* Initialise the repmgr schema name */
|
||||
maxlen_snprintf(repmgr_schema, "%s%s", DEFAULT_REPMGR_SCHEMA_PREFIX,
|
||||
local_options.cluster_name);
|
||||
|
||||
log_info(_("%s Connecting to database '%s'\n"), progname,
|
||||
@@ -286,7 +286,7 @@ main(int argc, char **argv)
|
||||
* Set my server mode, establish a connection to primary and start
|
||||
* monitor
|
||||
*/
|
||||
ret = is_witness(my_local_conn, repmgr_schema,
|
||||
ret = is_witness(my_local_conn,
|
||||
local_options.cluster_name, local_options.node);
|
||||
|
||||
if (ret == 1)
|
||||
@@ -396,9 +396,9 @@ main(int argc, char **argv)
|
||||
/* I need the id of the primary as well as a connection to it */
|
||||
log_info(_("%s Connecting to primary for cluster '%s'\n"),
|
||||
progname, local_options.cluster_name);
|
||||
primary_conn = get_master_connection(my_local_conn, repmgr_schema,
|
||||
local_options.cluster_name,
|
||||
&primary_options.node, NULL);
|
||||
primary_conn = get_master_connection(my_local_conn,
|
||||
local_options.cluster_name,
|
||||
&primary_options.node, NULL);
|
||||
if (primary_conn == NULL)
|
||||
{
|
||||
terminate(ERR_BAD_CONFIG);
|
||||
@@ -537,9 +537,11 @@ witness_monitor(void)
|
||||
sqlquery_snprintf(sqlquery,
|
||||
"INSERT INTO %s.repl_monitor "
|
||||
"VALUES(%d, %d, '%s'::timestamp with time zone, "
|
||||
" null, pg_current_xlog_location(), null, "
|
||||
" NULL, pg_current_xlog_location(), NULL, "
|
||||
" 0, 0) ",
|
||||
repmgr_schema, primary_options.node, local_options.node,
|
||||
get_repmgr_schema_quoted(my_local_conn),
|
||||
primary_options.node,
|
||||
local_options.node,
|
||||
monitor_witness_timestamp);
|
||||
|
||||
/*
|
||||
@@ -603,7 +605,7 @@ standby_monitor(void)
|
||||
log_err(_("We couldn't reconnect to master. Now checking if another node has been promoted.\n"));
|
||||
for (connection_retries = 0; connection_retries < 6; connection_retries++)
|
||||
{
|
||||
primary_conn = get_master_connection(my_local_conn, repmgr_schema,
|
||||
primary_conn = get_master_connection(my_local_conn,
|
||||
local_options.cluster_name, &primary_options.node, NULL);
|
||||
if (PQstatus(primary_conn) == CONNECTION_OK)
|
||||
{
|
||||
@@ -736,7 +738,7 @@ standby_monitor(void)
|
||||
"VALUES(%d, %d, '%s'::timestamp with time zone, "
|
||||
"'%s'::timestamp with time zone, '%s', '%s', "
|
||||
"%lld, %lld) ",
|
||||
repmgr_schema,
|
||||
get_repmgr_schema_quoted(primary_conn),
|
||||
primary_options.node, local_options.node, monitor_standby_timestamp,
|
||||
last_wal_standby_applied_timestamp,
|
||||
last_wal_primary_location,
|
||||
@@ -794,7 +796,7 @@ do_failover(void)
|
||||
" WHERE cluster = '%s' "
|
||||
" ORDER BY priority, id "
|
||||
" LIMIT %i ",
|
||||
repmgr_schema,
|
||||
get_repmgr_schema_quoted(my_local_conn),
|
||||
local_options.cluster_name,
|
||||
FAILOVER_NODES_MAX_CHECK);
|
||||
|
||||
@@ -991,8 +993,9 @@ do_failover(void)
|
||||
uxlogid = 0;
|
||||
uxrecoff = 0;
|
||||
|
||||
sqlquery_snprintf(sqlquery, "SELECT %s.repmgr_get_last_standby_location()",
|
||||
repmgr_schema);
|
||||
sqlquery_snprintf(sqlquery,
|
||||
"SELECT %s.repmgr_get_last_standby_location()",
|
||||
get_repmgr_schema_quoted(node_conn));
|
||||
res = PQexec(node_conn, sqlquery);
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
@@ -1212,11 +1215,11 @@ check_cluster_configuration(PGconn *conn)
|
||||
char sqlquery[QUERY_STR_LEN];
|
||||
|
||||
log_info(_("%s Checking cluster configuration with schema '%s'\n"),
|
||||
progname, repmgr_schema);
|
||||
progname, get_repmgr_schema());
|
||||
sqlquery_snprintf(sqlquery,
|
||||
"SELECT oid FROM pg_class "
|
||||
" WHERE oid = '%s.repl_nodes'::regclass ",
|
||||
repmgr_schema);
|
||||
get_repmgr_schema());
|
||||
res = PQexec(conn, sqlquery);
|
||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||
{
|
||||
@@ -1254,9 +1257,12 @@ check_node_configuration(void)
|
||||
log_info(_("%s Checking node %d in cluster '%s'\n"),
|
||||
progname, local_options.node, local_options.cluster_name);
|
||||
sqlquery_snprintf(sqlquery,
|
||||
"SELECT * FROM %s.repl_nodes "
|
||||
" WHERE id = %d AND cluster = '%s' ",
|
||||
repmgr_schema, local_options.node,
|
||||
"SELECT * "
|
||||
" FROM %s.repl_nodes "
|
||||
" WHERE id = %d "
|
||||
" AND cluster = '%s' ",
|
||||
get_repmgr_schema_quoted(my_local_conn),
|
||||
local_options.node,
|
||||
local_options.cluster_name);
|
||||
|
||||
res = PQexec(my_local_conn, sqlquery);
|
||||
@@ -1288,7 +1294,8 @@ check_node_configuration(void)
|
||||
sqlquery_snprintf(sqlquery,
|
||||
"INSERT INTO %s.repl_nodes "
|
||||
"VALUES (%d, '%s', '%s', '%s', 0, 'f') ",
|
||||
repmgr_schema, local_options.node,
|
||||
get_repmgr_schema_quoted(primary_conn),
|
||||
local_options.node,
|
||||
local_options.cluster_name,
|
||||
local_options.node_name,
|
||||
local_options.conninfo);
|
||||
@@ -1393,8 +1400,10 @@ update_shared_memory(char *last_wal_standby_applied)
|
||||
PGresult *res;
|
||||
char sqlquery[QUERY_STR_LEN];
|
||||
|
||||
sprintf(sqlquery, "SELECT %s.repmgr_update_standby_location('%s')",
|
||||
repmgr_schema, last_wal_standby_applied);
|
||||
sprintf(sqlquery,
|
||||
"SELECT %s.repmgr_update_standby_location('%s')",
|
||||
get_repmgr_schema_quoted(my_local_conn),
|
||||
last_wal_standby_applied);
|
||||
|
||||
/* If an error happens, just inform about that and continue */
|
||||
res = PQexec(my_local_conn, sqlquery);
|
||||
@@ -1425,7 +1434,7 @@ update_registration(void)
|
||||
" SET conninfo = '%s', "
|
||||
" priority = %d "
|
||||
" WHERE id = %d ",
|
||||
repmgr_schema,
|
||||
get_repmgr_schema_quoted(primary_conn),
|
||||
local_options.conninfo,
|
||||
local_options.priority,
|
||||
local_options.node);
|
||||
|
||||
Reference in New Issue
Block a user