monitoring: ensure that invalid replication_lag value is not inserted.

Per Github #189.
This commit is contained in:
Ian Barwick
2016-06-20 10:55:25 +09:00
parent 3d6c349d88
commit 5d8b1a3a31

View File

@@ -706,6 +706,9 @@ standby_monitor(void)
XLogRecPtr lsn_last_xlog_receive_location; XLogRecPtr lsn_last_xlog_receive_location;
XLogRecPtr lsn_last_xlog_replay_location; XLogRecPtr lsn_last_xlog_replay_location;
long long unsigned int replication_lag;
long long unsigned int apply_lag;
int connection_retries, int connection_retries,
ret; ret;
bool did_retry = false; bool did_retry = false;
@@ -1051,24 +1054,39 @@ standby_monitor(void)
strncpy(last_wal_primary_location, PQgetvalue(res, 0, 0), MAXLEN); strncpy(last_wal_primary_location, PQgetvalue(res, 0, 0), MAXLEN);
PQclear(res); PQclear(res);
/* Calculate the lag */
lsn_master_current_xlog_location = lsn_to_xlogrecptr(last_wal_primary_location, NULL); lsn_master_current_xlog_location = lsn_to_xlogrecptr(last_wal_primary_location, NULL);
lsn_last_xlog_replay_location = lsn_to_xlogrecptr(last_xlog_replay_location, NULL); lsn_last_xlog_replay_location = lsn_to_xlogrecptr(last_xlog_replay_location, NULL);
lsn_last_xlog_receive_location = lsn_to_xlogrecptr(last_xlog_receive_location, NULL);
/* Calculate apply lag */
if (last_xlog_receive_location_gte_replayed == false) if (last_xlog_receive_location_gte_replayed == false)
{ {
/* /*
* We're not receiving streaming WAL - in this case the receive location * We're not receiving streaming WAL - in this case the receive location
* equals the last replayed location * equals the last replayed location
*/ */
apply_lag = 0;
lsn_last_xlog_receive_location = lsn_last_xlog_replay_location;
strncpy(last_xlog_receive_location, last_xlog_replay_location, MAXLEN); strncpy(last_xlog_receive_location, last_xlog_replay_location, MAXLEN);
} }
else else
{ {
lsn_last_xlog_receive_location = lsn_to_xlogrecptr(last_xlog_receive_location, NULL); apply_lag = (long long unsigned int)lsn_last_xlog_receive_location - lsn_last_xlog_replay_location;
}
/* Calculate replication lag */
if (lsn_master_current_xlog_location >= lsn_last_xlog_receive_location)
{
replication_lag = (long long unsigned int)(lsn_master_current_xlog_location - lsn_last_xlog_receive_location);
}
else
{
/* This should never happen, but in case it does set lag to zero */
log_warning("Master xlog (%s) location appears less than standby receive location (%s)\n",
last_wal_primary_location,
last_xlog_receive_location);
replication_lag = 0;
} }
/* /*
@@ -1099,8 +1117,8 @@ standby_monitor(void)
last_xact_replay_timestamp, last_xact_replay_timestamp,
last_wal_primary_location, last_wal_primary_location,
last_xlog_receive_location, last_xlog_receive_location,
(long long unsigned int)(lsn_master_current_xlog_location - lsn_last_xlog_receive_location), replication_lag,
(long long unsigned int)(lsn_last_xlog_receive_location - lsn_last_xlog_replay_location)); apply_lag);
/* /*
* Execute the query asynchronously, but don't check for a result. We will * Execute the query asynchronously, but don't check for a result. We will