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 →
- Run
postgres_exporterwith a dedicated read-only role in thepg_monitorgroup. - Key metrics and alerts:
- Connections:
pg_stat_activity_countversuspg_settings_max_connections(alert above 80%). - Replication lag:
pg_replication_lag_secondsorpg_stat_replicationLSN differences. - Transactions:
rate(pg_stat_database_xact_rollback[5m]), deadlocks. - Cache hit ratio, long-running transactions, and
pg_stat_database_tup_*. - Disk, and
pg_walgrowth (a stuck replication slot fills the disk).
- Connections:
- Add
pg_stat_statementsfor 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.
More on Prometheus and PromQL at Scale
Prometheus memory has grown to 60 GB and keeps climbing. How do you find the cause?IntermediateA developer added `user_id` as a metric label. What is the problem and what do you advise?BeginnerYour dashboard shows the average of per-pod p99 latencies. Why is it wrong, and how do you compute a correct fleet-wide p99?IntermediateChoose histogram bucket boundaries for a service with a 300 ms latency SLO.AdvancedYou have 500 short-lived batch jobs that finish before Prometheus can scrape them. How do you capture their metrics?IntermediateYour EC2 fleet autoscales constantly. How do you keep Prometheus targets up to date?Intermediate