To ensure your PostgreSQL tables are analyzed and vacuumed weekly, follow these steps:
1. Verify Autovacuum Settings (Optional but Recommended)
Check if autovacuum is enabled (default is on). While this won't force weekly vacuums, it's good to confirm:
SELECT name, setting FROM pg_settings WHERE name = 'autovacuum';
If disabled, enable it in postgresql.conf and restart PostgreSQL.
2. Create a Maintenance Script
Use vacuumdb to vacuum/analyze all databases. Create a script (e.g., /usr/local/bin/vacuum_analyze.sh):
#!/bin/bash
# Vacuum and analyze all databases
vacuumdb -a --vacuum --analyze -v
Make it executable:
chmod +x /usr/local/bin/vacuum_analyze.sh
3. Schedule with Cron
Schedule the script to run weekly (e.g., every Sunday at 2 AM). Edit the cron jobs for the postgres user:
sudo crontab -u postgres -e
Add this line:
0 2 * * 0 /usr/local/bin/vacuum_analyze.sh >> /var/log/vacuum_analyze.log 2>&1
0 2 * * 0: Runs at 2:00 AM every Sunday.
- Log output to
/var/log/vacuum_analyze.log.
4. Verify Permissions
Ensure the postgres user has access to the script and PostgreSQL. Test the script manually:
sudo -u postgres /usr/local/bin/vacuum_analyze.sh
5. Check CheckMK Alerts
After the first run, verify the CheckMK alert clears. Check table statistics with:
SELECT relname, last_vacuum, last_analyze
FROM pg_stat_all_tables
WHERE schemaname = 'public';
Notes:
- Large Tables: Schedule during off-peak hours to avoid performance impact.
- Remote Databases: Add connection flags to
vacuumdb (e.g., -h hostname -U username).
- Selective Vacuuming: To target specific databases/tables, adjust
vacuumdb options (e.g., -d dbname).
This approach ensures all tables are maintained weekly, resolving the CheckMK alerts.