Skip to content

Commit

Permalink
Collect more columns from pg_replication_slots.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdevan committed Oct 3, 2021
1 parent b7e167b commit 546ce2e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
23 changes: 17 additions & 6 deletions collector/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -1705,14 +1705,25 @@ func (c *collector) getReplicationSlotsv94() {

q := `SELECT slot_name, COALESCE(plugin, ''), slot_type,
COALESCE(database, ''), active, xmin, catalog_xmin,
restart_lsn, confirmed_flush_lsn, temporary
restart_lsn, confirmed_flush_lsn, temporary,
@wal_status@, @safe_wal_size@, two_phase
FROM pg_replication_slots
ORDER BY slot_name ASC`
if c.version < pgv96 { // confirmed_flush_lsn only in v9.6+
q = strings.Replace(q, "confirmed_flush_lsn", "''", 1)
if c.version < pgv96 { // confirmed_flush_lsn only in pg >= 9.6
q = strings.Replace(q, "confirmed_flush_lsn", `''`, 1)
}
if c.version < pgv10 { // temporary only in v10+
q = strings.Replace(q, "temporary", "FALSE", 1)
if c.version < pgv10 { // temporary only in pg >= 10
q = strings.Replace(q, "temporary", `FALSE`, 1)
}
if c.version < pgv13 { // wal_status and safe_wal_size only in pg >= 13
q = strings.Replace(q, "@wal_status@", `''`, 1)
q = strings.Replace(q, "@safe_wal_size@", `0::bigint`, 1)
} else {
q = strings.Replace(q, "@wal_status@", `COALESCE(wal_status, '')`, 1)
q = strings.Replace(q, "@safe_wal_size@", `COALESCE(safe_wal_size, 0)::bigint`, 1)
}
if c.version < pgv14 { // two_phase only in pg >= 14
q = strings.Replace(q, "two_phase", `FALSE`, 1)
}
rows, err := c.db.QueryContext(ctx, q)
if err != nil {
Expand All @@ -1727,7 +1738,7 @@ func (c *collector) getReplicationSlotsv94() {
var rlsn, cflsn sql.NullString
if err := rows.Scan(&rs.SlotName, &rs.Plugin, &rs.SlotType,
&rs.DBName, &rs.Active, &xmin, &cXmin, &rlsn, &cflsn,
&rs.Temporary); err != nil {
&rs.Temporary, &rs.WALStatus, &rs.SafeWALSize, &rs.TwoPhase); err != nil {
log.Fatalf("pg_replication_slots query failed: %v", err)
}
rs.Xmin = int(xmin.Int64)
Expand Down
4 changes: 4 additions & 0 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ type ReplicationSlot struct {
RestartLSN string `json:"restart_lsn"`
ConfirmedFlushLSN string `json:"confirmed_flush_lsn"`
Temporary bool `json:"temporary"`
// following fields present only in schema 1.11 and later
WALStatus string `json:"wal_status,omitempty"` // >= pg13
SafeWALSize int64 `json:"safe_wal_size,omitempty"` // >= pg13
TwoPhase bool `json:"two_phase,omitempty"` // >= pg14
}

type Role struct {
Expand Down

0 comments on commit 546ce2e

Please sign in to comment.