From 6354b0c7e7eeb3b923c75fafaf00bafce6238b9f Mon Sep 17 00:00:00 2001
From: Kevin Pullin <kevin.pullin@gmail.com>
Date: Thu, 24 Dec 2020 07:21:40 -0800
Subject: [PATCH] Add query for 'pg_replication_slots' (#465)

The existing 'pg_stat_replication' data does not
include stats for inactive replication slots. This
commit adds a minimal amount of metrics from
'pg_replication_slots' to know if a slot is
active and its lag.

This is helpful to detect if an inactive slot
is causing the server to run out of storage due
to an inactive slot blocking WAL flushing.
---
 cmd/postgres_exporter/postgres_exporter.go | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/cmd/postgres_exporter/postgres_exporter.go b/cmd/postgres_exporter/postgres_exporter.go
index 37795aac..a115876b 100644
--- a/cmd/postgres_exporter/postgres_exporter.go
+++ b/cmd/postgres_exporter/postgres_exporter.go
@@ -315,6 +315,16 @@ var builtinMetricMaps = map[string]intermediateMetricMap{
 		true,
 		0,
 	},
+	"pg_replication_slots": {
+		map[string]ColumnMapping{
+			"slot_name":       {LABEL, "Name of the replication slot", nil, nil},
+			"database":        {LABEL, "Name of the database", nil, nil},
+			"active":          {GAUGE, "Flag indicating if the slot is active", nil, nil},
+			"pg_wal_lsn_diff": {GAUGE, "Replication lag in bytes", nil, nil},
+		},
+		true,
+		0,
+	},
 	"pg_stat_archiver": {
 		map[string]ColumnMapping{
 			"archived_count":     {COUNTER, "Number of WAL files that have been successfully archived", nil, nil},
@@ -407,6 +417,16 @@ var queryOverrides = map[string][]OverrideQuery{
 		},
 	},
 
+	"pg_replication_slots": {
+		{
+			semver.MustParseRange(">=9.4.0"),
+			`
+			SELECT slot_name, database, active, pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)
+			FROM pg_replication_slots
+			`,
+		},
+	},
+
 	"pg_stat_archiver": {
 		{
 			semver.MustParseRange(">=0.0.0"),