PostgreSQL Health Check Scripts
PostgreSQL Health Check Scripts
Instance Health, Database Growth, Connections, VACUUM, Dead Tuples, Replication, XID Age & DBA Checks
Introduction
A PostgreSQL health check should examine more than whether the database service is running. DBAs need visibility into database availability, uptime, connection pressure, storage growth, dead tuples, VACUUM and ANALYZE activity, transaction age, replication, indexes, and long-running transactions.
The following scripts provide practical checks that can be incorporated into daily DBA reviews, troubleshooting activities, operational runbooks, and environment assessments.
Contents
1. PostgreSQL Version 2. PostgreSQL Uptime 3. Database Size Check 4. Connection Health 5. Dead Tuple Check 6. VACUUM and ANALYZE History 7. Tables Needing VACUUM Attention 8. Invalid Indexes 9. Transaction ID Age 10. Long-Running Transactions 11. Replication Health 12. Replication Slot Health 13. Important Configuration Settings1. PostgreSQL Version
Start a health check by confirming the exact PostgreSQL server version. This is important when reviewing compatibility, patching, features, and support status.
2. PostgreSQL Uptime
Database uptime can help identify unexpected restarts and establish when the current PostgreSQL server process started.
3. Database Size Check
Monitor database size regularly so unexpected growth can be identified before it becomes a filesystem or storage-capacity issue.
4. Connection Health
This query compares current backend usage with the configured
max_connections value.
max_connections automatically when utilization becomes high.
Investigate application connection pooling, leaked sessions, idle connections, workload
concurrency, and memory implications first.
5. Dead Tuple Check
PostgreSQL's MVCC architecture can leave old row versions after UPDATE and DELETE operations. VACUUM is responsible for making reusable space available and maintaining database health.
n_live_tup and n_dead_tup are estimates.
Use them to identify candidates requiring investigation rather than treating the
values as exact row counts.
6. VACUUM and ANALYZE History
Review when user tables were last vacuumed or analyzed by manual operations or autovacuum.
7. Tables Needing VACUUM Attention
The following script provides a practical list of tables with significant dead tuple counts. The appropriate threshold depends on table size and workload.
10,000 dead tuples is only an example.
For large tables, evaluate the dead-tuple ratio, update/delete rate, autovacuum
settings, table size, transaction behavior, and available maintenance capacity.
8. Invalid Indexes
Invalid indexes may remain after certain interrupted or failed index operations and should be investigated by the DBA.
9. Transaction ID Age
Transaction ID wraparound protection is a critical PostgreSQL maintenance responsibility. Monitor database transaction age so autovacuum has sufficient opportunity to freeze old tuples.
Compare with Autovacuum Freeze Configuration
10. Long-Running Transactions
Long-running transactions can retain older row versions and interfere with normal VACUUM cleanup.
11. Replication Health
Run this query on a primary PostgreSQL server to review connected streaming replication clients.
Check Whether the Current Server Is in Recovery
12. Replication Slot Health
Replication slots can retain WAL required by consumers. Inactive or abandoned slots can therefore contribute to excessive WAL retention.
Estimate Retained WAL
13. Important Configuration Settings
This query provides a quick view of several settings commonly reviewed during PostgreSQL health and configuration assessments.
Daily PostgreSQL Health Check Summary
| Area | Check | Investigate When |
|---|---|---|
| Availability | Uptime and restart history | Restart was unexpected |
| Connections | Current and active sessions | Usage deviates materially from the normal baseline or approaches capacity |
| Storage | Database and filesystem growth | Growth becomes abnormal or free space approaches operational thresholds |
| VACUUM | Dead tuples and autovacuum history | Dead tuples accumulate or autovacuum cannot keep pace |
| XID | Transaction ID age | Age rises unexpectedly toward configured freeze limits |
| Indexes | Validity and readiness | Invalid or not-ready indexes appear |
| Replication | Standby state and lag | Standby disconnects or lag exceeds business tolerance |
| Slots | Slot activity and retained WAL | Inactive slots retain unexpected amounts of WAL |
Production Health Check Checklist
- Confirm PostgreSQL instance availability and version.
- Review uptime and investigate unexpected restarts.
- Monitor database and filesystem growth.
- Review connection utilization and connection-pool behavior.
- Identify long-running and idle-in-transaction sessions.
- Review dead tuples and autovacuum activity.
- Review ANALYZE activity and optimizer-statistics freshness.
- Monitor transaction ID age and freeze activity.
- Investigate invalid indexes.
- Monitor replication state and lag.
- Review inactive replication slots and WAL retention.
- Verify backup and WAL-archive health separately.
- Review PostgreSQL logs for errors, warnings, deadlocks, and failed operations.
- Compare important configuration settings with the approved production baseline.
- Correlate database findings with CPU, memory, disk latency, filesystem space, network, and application metrics.
Key Takeaways
- PostgreSQL health checks should cover availability, capacity, maintenance, transactions, indexes, and replication.
- Database uptime helps identify unexpected restart or failover events.
- Connection pressure should be investigated before simply increasing
max_connections. - Dead tuple statistics help identify tables that may require VACUUM investigation.
- VACUUM and ANALYZE history provides visibility into PostgreSQL maintenance activity.
- Transaction ID age is a critical PostgreSQL health metric.
- Long-running transactions can prevent normal removal of old row versions.
- Invalid indexes should be investigated rather than automatically dropped.
- Inactive replication slots can retain significant amounts of WAL.
- Health-check values should always be interpreted against workload-specific baselines.
Conclusion
A PostgreSQL health check should provide a structured view of database availability, capacity, connection usage, MVCC maintenance, transaction age, index state, replication, WAL retention, and important configuration settings.
The greatest value comes from performing these checks consistently and comparing the results against historical baselines. A single snapshot may show the current state, but monitoring trends helps a DBA identify gradual deterioration before it becomes a production incident.
For critical systems, these scripts should be combined with automated monitoring, alerting, backup validation, restore testing, infrastructure telemetry, and a documented operational runbook.
Comments
Post a Comment