Skip to content

Database Optimization 🟑 BETA

Measure a slow query with EXPLAIN ANALYZE, then change a migration or an index rather than guessing at a setting.

Admin guide Β· database tuning runbook

When to use Before you start Steps Verify Roll back
A query is slow, a table grows too fast Snapshot and dump, DB access Measure, inspect schema, add index or partition Plan improves, correctness unchanged Revert the migration via restore

When to use

Use this runbook when latency comes from the database, not from the model provider. Measure first; an index that is not used costs writes for nothing.

Before you start

  • table! and joinable! macros live only in the botschema crate. A schema change is a migration followed by diesel print-schema and an update to botschema; never copy a table into a feature crate.
  • Take a dump before touching a schema or a large table: pg_dump -U postgres -F c -f /tmp/backup.dump <dbname>.
  • Enum-like values are defined by the schema and migrations; read those files instead of a copied value table.

Steps

# 1. Measure the query as the server runs it.
psql -h localhost -U postgres -d botserver -c \
  "EXPLAIN ANALYZE SELECT ...;"

# 2. Check whether the existing indexes are used.
psql -h localhost -U postgres -d botserver -c \
  "SELECT relname, idx_scan, indexrelname FROM pg_stat_user_indexes ORDER BY idx_scan;"

# 3. Inspect bloat on the high-churn table.
psql -h localhost -U postgres -d botserver -c \
  "SELECT relname, n_dead_tup FROM pg_stat_user_tables ORDER BY n_dead_tup DESC LIMIT 10;"

# 4. Change the schema through a migration, then refresh botschema:
#    diesel migration run
#    diesel print-schema
#    update the matching module in botschema, then commit both.

Schema Design Principles

The schema has one home (botschema). A feature crate consumes it with pub use botschema::<table>; and must not declare its own table!.

Indexing Strategies

Index type Use when
Composite queries filter on a stable pair such as tenant and time
Partial only active rows are queried
BRIN very large append-only time-series tables

Confirm the index is scanned in pg_stat_user_indexes before keeping it.

Table Partitioning

Partition a high-volume table by time only when retention and query patterns justify the operational cost. Manage partitions from a migration, not from a one-off command.

Connection Pooling

Configure the pool in the server's configuration; read the current keys from the source. A larger pool does not fix a missing index.

Query Optimization

Prefer one set-based query over N+1 row lookups. Verify with EXPLAIN ANALYZE, not with a stopwatch on a warm cache.

Vacuum and Maintenance

Aggressive autovacuum suits high-churn tables. Rotate logs and prune only operational counters; product data is never a filler for disk space.

Monitoring Queries

Check Query surface
Slow queries EXPLAIN ANALYZE and the slow-query log
Index usage pg_stat_user_indexes
Table bloat pg_stat_user_tables
Connection count pg_stat_activity

Verify

Check Command / surface Pass condition
Plan EXPLAIN ANALYZE sequential scan replaced by the intended index
Correctness test read and write same result as before
Index use idx_scan index is actually scanned
Schema botschema module matches diesel print-schema
Health /health 200

Roll back

πŸ”΄ Restore the pre-change dump before reverting a partially applied migration, then follow the restore runbook. An index-only change is reverted by dropping that index.