Agent Wranglers

DAT-39Recommended

Let routine maintenance run

Keep the database's own maintenance running, so tables don't bloat and the planner has fresh statistics.

How to do it

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. On long-lived ones, run PRAGMA optimize=0x10002 when the connection opens, then PRAGMA optimize every few hours. Rebuild statistics after large data loads. Track bloat as Watch size and growth says.

Examples

SQL example

-- 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;

How to 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.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/data/operations/maintenance">Agent Wranglers data and databases playbook</a>. In code you write, add a comment next to it with the same link.