Interview Prep/Monitoring & Observability/Prometheus and PromQL at Scale

How do you monitor a PostgreSQL primary and replicas?

IntermediateScenarioPrometheus and PromQL at Scale
Answer

Situation read → Use postgres_exporter and watch for saturation, replication health and query behavior.

What I'd do →

  1. Run postgres_exporter with a dedicated read-only role in the pg_monitor group.
  2. Key metrics and alerts:
    • Connections: pg_stat_activity_count versus pg_settings_max_connections (alert above 80%).
    • Replication lag: pg_replication_lag_seconds or pg_stat_replication LSN differences.
    • Transactions: rate(pg_stat_database_xact_rollback[5m]), deadlocks.
    • Cache hit ratio, long-running transactions, and pg_stat_database_tup_*.
    • Disk, and pg_wal growth (a stuck replication slot fills the disk).
  3. Add pg_stat_statements for slow-query visibility.

Why → Connection exhaustion, replication lag and WAL retention are the common ways Postgres takes down a service.

Interview tip → Mention that an inactive replication slot retaining WAL is a classic disk-full cause.

All Monitoring & Observability questions Roadmap: Monitoring & alerting

More on Prometheus and PromQL at Scale