> For AI agents: the complete documentation index is available at https://docs.hod.sh/llms.txt, the full documentation bundle is available at https://docs.hod.sh/llms-full.txt.

# Database

Hod needs Postgres 18 or newer, and refuses to start on an older one. Every server shares the same database.

- **Connections.** Each server uses up to `HOD_DATABASE_MAX_CONNECTIONS`, plus one for each background loop holding its lock (at most 7) and one that listens for notifications. Set Postgres's `max_connections` to cover every server.
- **Poolers.** Hod listens for notifications (`LISTEN`) and holds session advisory locks, so PgBouncer must run in session mode, or the server must connect to Postgres directly. Transaction pooling breaks both.
- **Timeouts.** Every connection has `statement_timeout`, `lock_timeout` and a one-minute `idle_in_transaction_session_timeout`. Migrations run without them. Connections are named `hod-server` in `pg_stat_activity`.
- **Health.** Two queries show whether vacuum keeps up. The tables Hod updates often should show mostly HOT updates and few dead rows:

  ```sql
  SELECT relname, n_tup_upd, n_tup_hot_upd, n_dead_tup, last_autovacuum, total_autovacuum_time
  FROM pg_stat_user_tables
  WHERE relname IN ('jobs', 'job_attempts', 'job_leases', 'log_chunks', 'runners')
  ORDER BY n_dead_tup DESC;
  ```

  A `job_leases` with `n_tup_hot_upd` well below `n_tup_upd`, or `n_dead_tup` growing between runs of autovacuum, points at a long-running transaction holding vacuum back (`SELECT pid, xact_start, state, query FROM pg_stat_activity WHERE xact_start < now() - interval '5 minutes'`).
