How do you monitor PostgreSQL performance in production?
Answer
Key monitoring areas and tools: (1) Query performance: enable pg_stat_statements extension — tracks cumulative statistics per query. Query slowest queries: SELECT query, mean_exec_time, calls FROM pg_stat_statements ORDER BY mean_exec_time DESC LIMIT 20;. Set log_min_duration_statement = 1000 to log queries over 1 second. (2) Table/index health: pg_stat_user_tables (seq scans, dead tuples), pg_stat_user_indexes (index usage). (3) Bloat: pgstattuple. (4) Replication lag: pg_stat_replication. (5) Locks: pg_locks + pg_stat_activity. (6) External monitoring: Prometheus + postgres_exporter + Grafana dashboards (e.g., pganalyze, Datadog, New Relic). Set up alerting on: replication lag, long-running transactions, high dead tuple counts, low cache hit ratio (blks_hit / (blks_hit + blks_read) should be > 99%).
More PostgreSQL Questions
View all →- Advanced What is the query planner in PostgreSQL and how does it work?
- Advanced What causes poor query plans and how do you fix them?
- Advanced What is logical replication in PostgreSQL?
- Advanced How do you implement row-level security (RLS) in PostgreSQL?
- Advanced What is table bloat in PostgreSQL and how do you fix it?