Fix event logging when cloning from another standby

We can only write to the primary, which we'll need to find seperately
when cloning from a standby.
This commit is contained in:
Ian Barwick
2015-03-06 18:39:36 +09:00
parent ff63954c68
commit 2339adba6c
3 changed files with 118 additions and 4 deletions

View File

@@ -409,6 +409,88 @@ get_pg_setting(PGconn *conn, const char *setting, char *output)
}
/*
* get_primary_connection()
*
* Returns connection to cluster's primary node
*
* This assumes that the primary node information in `repl_nodes`
* is correct. See get_master_connection(), which polls all known
* servers to find out which one is currently primary.
*/
PGconn *
get_primary_connection(PGconn *standby_conn, char *cluster,
int *primary_node_id_ptr, char *primary_conninfo_out)
{
PGconn *primary_conn = NULL;
PGresult *res;
char sqlquery[QUERY_STR_LEN];
char primary_conninfo_stack[MAXCONNINFO];
char *primary_conninfo = &*primary_conninfo_stack;
/*
* If the caller wanted to get a copy of the connection info string, sub
* out the local stack pointer for the pointer passed by the caller.
*/
if (primary_conninfo_out != NULL)
primary_conninfo = primary_conninfo_out;
sqlquery_snprintf(sqlquery,
" SELECT n.conninfo, n.name, n.id "
" FROM %s.repl_nodes n "
" WHERE n.cluster = '%s' "
" AND n.type = 'primary' ",
get_repmgr_schema_quoted(standby_conn),
cluster);
log_debug("get_primary_connection(): %s\n", sqlquery);
res = PQexec(standby_conn, sqlquery);
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
log_err(_("Unable to get conninfo for primary server: %s\n"),
PQerrorMessage(standby_conn));
PQclear(res);
return NULL;
}
if(!PQntuples(res))
{
log_notice(_("No primary server record found"));
PQclear(res);
return NULL;
}
strncpy(primary_conninfo, PQgetvalue(res, 0, 0), MAXCONNINFO);
if(primary_node_id_ptr != NULL)
*primary_node_id_ptr = atoi(PQgetvalue(res, 0, 1));
PQclear(res);
log_debug("conninfo is: '%s'\n", primary_conninfo);
primary_conn = establish_db_connection(primary_conninfo, false);
if (PQstatus(primary_conn) != CONNECTION_OK)
{
log_err(_("Unable to connect to primary node: %s\n"),
PQerrorMessage(primary_conn));
return NULL;
}
return primary_conn;
}
/*
* get_upstream_connection()
*
* Returns connection to node's upstream node
*
* NOTE: will attempt to connect even if node is marked as inactive
*/
PGconn *
get_upstream_connection(PGconn *standby_conn, char *cluster, int node_id,
int *upstream_node_id_ptr, char *upstream_conninfo_out)
@@ -485,6 +567,9 @@ get_upstream_connection(PGconn *standby_conn, char *cluster, int node_id,
* NB: If master_conninfo_out may be NULL. If it is non-null, it is assumed to
* point to allocated memory of MAXCONNINFO in length, and the master server
* connection string is placed there.
*
* To get the primary node from the metadata (i.e. without polling all servers),
* use `get_primary_connection()`.
*/
PGconn *