Skip to content

Commit

Permalink
Collect pg_stat_replication.reply_time.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdevan committed Oct 2, 2021
1 parent 186b479 commit db97362
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 8 additions & 1 deletion collector/collect.go
Expand Up @@ -684,9 +684,16 @@ func (c *collector) getReplicationv10() {
COALESCE(EXTRACT(EPOCH FROM replay_lag)::bigint, 0),
COALESCE(sync_priority, -1),
COALESCE(sync_state, ''),
@reply_time@,
pid
FROM pg_stat_replication
ORDER BY pid ASC`
if c.version >= 120000 {
// for pg v12+ also collect reply_time
q = strings.Replace(q, "@reply_time@", `COALESCE(EXTRACT(EPOCH FROM reply_time)::bigint, 0)`, 1)
} else {
q = strings.Replace(q, "@reply_time@", `0`, 1)
}
rows, err := c.db.QueryContext(ctx, q)
if err != nil {
log.Printf("warning: pg_stat_replication query failed: %v", err)
Expand All @@ -700,7 +707,7 @@ func (c *collector) getReplicationv10() {
if err := rows.Scan(&r.RoleName, &r.ApplicationName, &r.ClientAddr,
&r.BackendStart, &backendXmin, &r.State, &r.SentLSN, &r.WriteLSN,
&r.FlushLSN, &r.ReplayLSN, &r.WriteLag, &r.FlushLag, &r.ReplayLag,
&r.SyncPriority, &r.SyncState, &r.PID); err != nil {
&r.SyncPriority, &r.SyncState, &r.ReplyTime, &r.PID); err != nil {
log.Fatalf("pg_stat_replication query failed: %v", err)
}
r.BackendXmin = int(backendXmin.Int64)
Expand Down
2 changes: 2 additions & 0 deletions model.go
Expand Up @@ -481,6 +481,8 @@ type ReplicationOut struct {
SyncState string `json:"sync_state"`
// following fields present only in schema 1.5 and later
PID int `json:"pid,omitempty"`
// following fields present only in schema 1.11 and later
ReplyTime int64 `json:"reply_time,omitempty"` // >= pg12
}

type ReplicationIn struct {
Expand Down

0 comments on commit db97362

Please sign in to comment.