#postgresql #monitoring #performance #cli

app dbwarrior

CLI to detect slow and high-row queries in PostgreSQL

1 unstable release

Uses new Rust 2024

new 0.1.0 Apr 8, 2025

#1209 in Database interfaces

MIT license

21KB
152 lines

dbwarrior ๐Ÿง 

Real-time Postgres query monitoring CLI tool that detects long-running or high-row queries, checks indexing, alerts via Slack/email, logs to file or JSON, and integrates with your workflow.


๐Ÿš€ Features

  • Detect long-running queries in pg_stat_activity
  • Analyze WHERE clause and check if indexes exist
  • Slack alerts + optional email fallback
  • Log alerts to file and structured JSON
  • Fingerprint queries to prevent alert spam
  • GitHub Actions CI pipeline
  • Easy Makefile for build/run/test/lint/publish

๐Ÿ”ง Installation

Option 1: Cargo

cargo install dbwarrior

Option 2: Clone & Build

git clone https://github.com/sanctusgee/dbwarrior.git
cd dbwarrior
make build

Run with:

make run

โš™๏ธ Configuration

Create a config.toml in your project root (or pass --config your_file.toml):

db_url = "postgresql://user:password@localhost/yourdb"
query_time_threshold_secs = 30
row_estimate_threshold = 1000000
slack_webhook_url = "https://hooks.slack.com/services/..."
email_fallback = "alerts@yourcompany.com"
log_file_path = "dbwarrior.log"
json_output_path = "dbwarrior.json"

โœ… Example Output

โš ๏ธ  Long-running query detected (PID 7483)
Duration: 45s
User: redash
Query:
  SELECT * FROM events WHERE user_id IS NOT NULL
Table: events
No index on events(user_id) โŒ

Suggestion:
  โ†’ Add index on events(user_id)

๐Ÿ“ฆ Makefile Targets

make build       # Compile in release mode
make run         # Run using config.toml
make test        # Run all tests
make lint        # Run Clippy linter
make fmt         # Format code
make watch       # Rebuild on file changes
make publish     # Publish to crates.io

๐Ÿงช GitHub Actions CI

Enabled for every push to main and new release tags:

  • ๐Ÿงฑ Builds the app
  • โœ… Runs all tests
  • ๐Ÿท๏ธ Tags release versions automatically

๐Ÿ“ฌ JSON Output

Each alert is written as a line of JSON:

{
  "pid": 7483,
  "duration_secs": 45,
  "user": "redash",
  "query": "SELECT * FROM events WHERE user_id IS NOT NULL",
  "table": "events",
  "column": "user_id",
  "index_exists": false,
  "suggestion": "Add index on events(user_id)"
}

๐Ÿ™Œ Contributing

See CONTRIBUTING.md for setup, linting, and PR guidelines.


๐Ÿ›ก License

MIT ยฉ sanctusgee


โญ Star this repo if dbwarrior helps you keep queries in check!

Dependencies

~15โ€“29MB
~456K SLoC