PostgreSQL Monitoring Scripts
PostgreSQL Monitoring Scripts
Sessions, Connections, Blocking, Locks, Long Queries, Database Size, Replication & Activity Monitoring
Introduction
Effective PostgreSQL monitoring helps DBAs identify abnormal workload patterns, long-running SQL statements, blocking sessions, connection pressure, storage growth, idle transactions, and replication problems before they become major incidents.
PostgreSQL exposes detailed operational information through catalog and statistics
views including pg_stat_activity, pg_locks,
pg_stat_database, pg_stat_user_tables, and
pg_stat_replication.
Contents
1. Monitor Database Connections 2. Active Sessions 3. Long-Running Queries 4. Idle-in-Transaction Sessions 5. Blocking Sessions 6. Current Locks 7. Database Sizes 8. Largest Tables 9. Connection Utilization 10. Database Activity Statistics 11. Replication Status 12. Replication Lag1. Monitor Database Connections
This query displays the number of current PostgreSQL sessions grouped by database.
2. Active Sessions
Use this query to review sessions currently executing SQL statements.
3. Long-Running Queries
The following example identifies active SQL statements running for more than five minutes. Adjust the threshold according to the expected workload.
4. Idle-in-Transaction Sessions
Sessions left idle inside an open transaction can retain locks and prevent cleanup of older row versions.
5. Blocking Sessions
PostgreSQL provides pg_blocking_pids() to identify sessions preventing
another backend from acquiring a required lock.
6. Current Locks
7. Database Sizes
8. Largest Tables
This query uses total relation size so the result includes the table and associated indexes and TOAST storage.
9. Connection Utilization
max_connections. Review connection leaks, application pooling,
session duration, workload concurrency, and tools such as PgBouncer.
10. Database Activity Statistics
11. Replication Status
On a primary server, pg_stat_replication displays information about
connected WAL sender processes and standby replication state.
12. Replication Lag in Bytes
Production Monitoring Checklist
- Monitor active and total database connections.
- Alert before connection capacity reaches critical levels.
- Identify unexpectedly long-running SQL statements.
- Monitor idle-in-transaction sessions.
- Monitor blocking and lock waits.
- Track database and table growth.
- Review transaction commits and rollbacks.
- Monitor temporary-file generation and deadlocks.
- Monitor primary and standby replication state.
- Alert on abnormal replication lag.
- Capture historical metrics rather than relying only on point-in-time queries.
- Correlate PostgreSQL metrics with CPU, memory, storage latency, filesystem capacity, and application telemetry.
Key Takeaways
pg_stat_activityis a primary source for session and query monitoring.pg_blocking_pids()helps identify sessions responsible for blocking.pg_locksprovides detailed lock-state information.- Idle transactions deserve particular attention because they can retain locks and old row versions.
- Database and relation-size functions help track capacity growth.
pg_stat_databaseprovides database-level workload statistics.pg_stat_replicationprovides primary-side replication visibility.- Connection pressure should be investigated before increasing
max_connections. - A production monitoring strategy should combine PostgreSQL statistics with infrastructure and application telemetry.
Conclusion
PostgreSQL monitoring should provide visibility across sessions, transactions, SQL activity, locks, connections, storage growth, database statistics, and replication. No single query provides a complete picture of database health.
For production systems, these DBA scripts should complement persistent monitoring and alerting so abnormal behavior can be compared against historical baselines. Effective monitoring allows DBAs to identify developing problems before they become application outages or recovery incidents.
Comments
Post a Comment