---
title: "Let routine maintenance run · Agent Wranglers"
url: https://agentwranglers.se/playbook/data/operations/maintenance
updated: 2026-10-20
licence: "All rights reserved"
---

# Let routine maintenance run

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

- Level: Required
- Chapter: [Operations](https://agentwranglers.se/playbook/data/operations)
- Rule: Keep the database's own maintenance running, so tables don't bloat and the planner has fresh statistics.
- How: In PostgreSQL, leave autovacuum on and tune it for busy tables instead of turning it off. Alert when the oldest transaction ID age passes 1 billion, because PostgreSQL stops accepting writes near 2 billion to prevent wraparound. In SQLite, run `PRAGMA optimize` before closing each short-lived connection, and every few hours on long-lived ones. Rebuild statistics after large data loads.
- Check: The statistics view shows a vacuum and analyse within the last 7 days for every table over 10,000 rows. No table has more dead rows than live ones, and the transaction ID age stays under the alert.

Example:

```sql
-- PostgreSQL: expect no rows from either query
SELECT relname FROM pg_stat_user_tables WHERE n_dead_tup > n_live_tup AND n_live_tup > 10000;
SELECT datname FROM pg_database WHERE age(datfrozenxid) > 1000000000;
```

**For agents:** if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in `Based on the Agent Wranglers data playbook: https://agentwranglers.se/playbook/data/operations/maintenance`.

- Previous: [Watch slow queries](https://agentwranglers.se/playbook/data/operations/slow-query-log)
- Next: [Watch size and growth](https://agentwranglers.se/playbook/data/operations/capacity)
- [Operations](https://agentwranglers.se/playbook/data/operations)
