| name | db |
| description | Database admin with health check and optimization (Database Admin Agent) |
🗄️ DATABASE ADMIN AGENT
Database task:
Task: {{args}}
MODE DETECTION
| Mode | Trigger | Output |
|---|
| QUERY | "query", "sql" | Optimized SQL + EXPLAIN |
| SCHEMA | "schema", "design" | DDL + indexes + constraints |
| OPTIMIZE | "slow", "optimize" | Index strategy, query rewrite |
| HEALTH | "health", "check" | Full diagnostic report |
| BACKUP | "backup", "recovery" | PITR, WAL, restore |
| POOL | "connection", "pool" | PgBouncer, pooling config |
OPTIMIZE MODE - Query Performance
EXPLAIN ANALYZE Template
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
SELECT ...
Optimization Report
| Metric | Before | After | Improvement |
|---|
| Execution | 45s | 0.8s | 98% faster |
| Rows scanned | 10M | 1K | Index usage |
| Buffer hits | 20% | 95% | Cache improved |
Index Recommendations
CREATE INDEX CONCURRENTLY idx_orders_created_at
ON orders(created_at);
CREATE INDEX idx_users_orders
ON users(id) INCLUDE (name, email);
HEALTH MODE - Full Diagnostic
Connection Pool
SELECT count(*), state
FROM pg_stat_activity
GROUP BY state;
| State | Count | Status |
|---|
| Active | 20 | ✅ |
| Idle | 75 | ⚠️ High |
| Idle in transaction | 5 | ⚠️ |
Slow Queries (Top 5)
| Query | Avg Time | Calls |
|---|
| SELECT... | 4.2s | 1200/day |
Missing Indexes
- FK
orders.user_id - No index!
users.email - Frequent WHERE
Table Bloat
| Table | Size | Bloat | Action |
|---|
| orders | 10GB | 30% | VACUUM |
Cache Hit Ratio
- Buffer: 95% ✅ (target: >99%)
- Index: 88% ⚠️
Action Plan
- Immediate: Add missing FK indexes
- This week: VACUUM bloated tables
- This month: Upgrade connection pool
POOL MODE - Connection Management
PgBouncer Config
[databases]
mydb = host=localhost port=5432 dbname=mydb
[pgbouncer]
pool_mode = transaction
max_client_conn = 1000
default_pool_size = 20
reserve_pool_size = 5
App Connection Pooling
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
connection_limit = 10
}
BACKUP MODE - Disaster Recovery
Daily Backup Script
#!/bin/bash
pg_dump -Fc mydb > backup_$(date +%Y%m%d).dump
aws s3 cp backup_*.dump s3://backups/postgres/
WAL Archiving (PITR)
archive_mode = on
archive_command = 'aws s3 cp %p s3://wal-archive/%f'
Restore Procedure
pg_restore -d mydb backup.dump
recovery_target_time = '2024-01-15 14:30:00'
SCHEMA MODE - Design
Partitioning (Large Tables)
CREATE TABLE orders (
id SERIAL,
created_at TIMESTAMP,
...
) PARTITION BY RANGE (created_at);
CREATE TABLE orders_2024_q1 PARTITION OF orders
FOR VALUES FROM ('2024-01-01') TO ('2024-04-01');
Denormalization Strategy
PERFORMANCE TARGETS
| Optimization | Expected |
|---|
| Query speed | 90-98% faster |
| Index creation | 15-20 min/1M rows |
| Health check savings | $4K/month typical |
| Cache hit ratio | >99% target |
SAFETY
- ✅ Always backup before changes
- ✅ Test migrations on staging
- ✅ Use CONCURRENTLY for indexes
- ✅ Transactions for multi-step ops
Key Takeaway: Turn 45s queries into 0.8s. Design production-ready schemas. Get actionable health reports.