Skip to content
emremeneksePublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

15 Commits

Folders and files

Repository files navigation

PgDoctor

PostgreSQL Performance Doctor CLI and agent.

  ____     ____   ____     ___     ____   _____    ___    ____
 |  _ \   / ___| |  _ \   / _ \   / ___| |_   _|  / _ \  |  _ \
 | |_) | | |  _  | | | | | | | | | |       | |   | | | | | |_) |
 |  __/  | |_| | | |_| | | |_| | | |___    | |   | |_| | |  _ <
 |_|      \____| |____/   \___/   \____|   |_|    \___/  |_| \_\

PgDoctor analyzes a PostgreSQL instance and reports:

  • Slow queries
  • High-load queries
  • Lock contention and live connection warnings
  • Root-cause signals for wait events, WAL/checkpoint pressure, and I/O hotspots
  • A weighted health score

What It Looks Like

PgDoctor Diagnostic Report | Server: my-db-host
Health Score: 64/100 [WARN]
Performance Degradation Detected: 8 slow queries + 0 heavy queries + 1 lock waiter(s).
Penalty Breakdown: slow=26, heavy=0, lock=10, total=36.
ROOT CAUSE SIGNALS
Query behavior: shared read=412 shared hit=9231 temp read=0 temp written=0 total exec time=14822.2ms wal_bytes=0 plan_time=0.0ms jit_supported=false.
Wait events: waiting_sessions=2 lock_waiters=1 top_wait=Lock:transactionid (1).
WAL/checkpoint: timed=12 requested=1 backend_buffers=0 backend_fsync=0.
I/O visibility: hottest_table=public.orders read=2048 hit_ratio=98.7% hottest_index=orders.ix_orders_customer_id read=1440 hit_ratio=99.1% pg_stat_io_supported=false.

Requirements

  • Rust (stable)
  • PostgreSQL with pg_stat_statements enabled
  • Access to pg_stat_statements, pg_stat_activity, and pg_locks

Build

cargo build

Release:

cargo build --release

Release binary path:

  • target/release/pgdoctor-core

Quick Start

First-time setup:

cargo run -- init

Run a one-shot diagnostic:

cargo run -- check

See agent and queue status:

cargo run -- status

If you use the built binary directly:

pgdoctor-core init
pgdoctor-core check
pgdoctor-core status

Command Guide

Think of the CLI in 4 groups: setup, diagnostics, Pro export, and background agent.

1. Setup And Config

init

  • First-time setup.
  • Tests the PostgreSQL connection and writes local config.
  • Flags:
    • --connection-string <VALUE>
    • --non-interactive
    • --skip-analysis

config show

  • Prints the current config.

config edit

  • Opens the interactive config editor.
  • Use this for thresholds, Pro mode, API key, and consent.

config reset

  • Deletes local config after confirmation.

2. Diagnostics

check

  • Runs one diagnostic pass.
  • Best command for day-to-day troubleshooting.
  • Includes a compact root-cause summary for:
    • query behavior
    • wait events / blockers
    • WAL/checkpoint pressure
    • I/O hotspots

check --live

  • Runs diagnostics every 30 seconds in the foreground.
  • Best when you want to watch the database while reproducing an issue.

3. Pro Export

These commands are only relevant if you are using Pro export.

check --enqueue

  • Runs one diagnostic pass and writes the snapshot into the local SQLite queue.
  • Does not force an immediate push by itself.

check --push

  • Runs one diagnostic pass.
  • Default behavior is queue-first:
    • snapshot goes into the local queue
    • then PgDoctor tries to flush the queue

check --push --direct-push

  • Legacy direct HTTP push path.
  • Bypasses queue-first flow.
  • Mostly useful for debugging the HTTP API.

flush

  • Tries to send queued Pro snapshots immediately.
  • Useful when mode is manual or when you want to retry after fixing endpoint or consent issues.
  • Flag:
    • --batch-size <NUMBER>

status

  • Shows queue and agent visibility:
    • queue depth
    • pending / failed / sent counts
    • last error
    • effective endpoint host
    • whether endpoint override is active

4. Background Agent

agent run

  • Runs the collector loop in the foreground.
  • Collects snapshots on an interval.
  • Depending on mode, it can also enqueue and push automatically.

agent start

  • Starts the same agent loop in the background.
  • Useful for long-running monitoring on a workstation or server.

agent stop

  • Stops the background agent.
  • Flag:
    • --force

Common agent flags:

  • --interval-seconds <NUMBER>
  • --push-interval-seconds <NUMBER>
  • --mode <off|manual|scheduled|continuous>

Typical Workflows

Local Troubleshooting

pgdoctor-core init
pgdoctor-core check
pgdoctor-core config edit
pgdoctor-core check --live

Queue-First Pro Export

pgdoctor-core check --enqueue
pgdoctor-core status
pgdoctor-core flush

Continuous Agent

Foreground:

pgdoctor-core agent run --mode continuous

Background:

pgdoctor-core agent start --mode continuous
pgdoctor-core status
pgdoctor-core agent stop

Export Modes

off

  • No queue, no push.

manual

  • Queue is used.
  • Push happens only when you run flush or check --push.

scheduled

  • Queue is used.
  • Background push worker runs on interval.

continuous

  • Queue is used.
  • Continuous collection and automatic push run together.

PostgreSQL Setup

In postgresql.conf:

shared_preload_libraries = 'pg_stat_statements'

Then restart PostgreSQL and run:

CREATE EXTENSION IF NOT EXISTS pg_stat_statements;

Configuration

Config file:

  • Windows: C:\Users\<user>\.pgdoctor\config.json
  • Linux/macOS: ~/.pgdoctor/config.json

Example:

{
  "connection_string": "postgresql://postgres:postgres@localhost:5432/postgres",
  "connection_timeout_seconds": 30,
  "max_retry_attempts": 3,
  "retry_delay_milliseconds": 1000,
  "slow_query_threshold_ms": 100.0,
  "throughput_min_calls": 100,
  "pro_export_enabled": false,
  "pro_api_key": "",
  "pro_export_mode": "off",
  "pro_collection_interval_seconds": 30,
  "pro_push_interval_seconds": 30,
  "pro_batch_size": 50,
  "pro_max_batches_per_flush": 20,
  "pro_max_retry_attempts": 5,
  "pro_retention_days": 7,
  "pro_max_push_requests_per_minute": 30,
  "pro_circuit_breaker_fail_threshold": 5,
  "pro_circuit_breaker_cooldown_seconds": 60,
  "pro_consent_acknowledged": false
}

Important fields:

  • connection_string
  • slow_query_threshold_ms
  • throughput_min_calls
  • pro_api_key
  • pro_export_mode
  • pro_consent_acknowledged

Endpoint Policy

Snapshot push target:

  • Production default:
    • https://api.pgdoctor.com/api/Snapshots/Create

Development override:

  • Set PGDOCTOR_ENV=dev
  • Set PGDOCTOR_PRO_ENDPOINT=http://localhost:5001

Example:

export PGDOCTOR_ENV=dev
export PGDOCTOR_PRO_ENDPOINT=http://localhost:5001

In PowerShell:

$env:PGDOCTOR_ENV="dev"
$env:PGDOCTOR_PRO_ENDPOINT="http://localhost:5001"

status shows which endpoint policy is active.

Consent And Privacy

Pro export is opt-in.

Rules:

  • Raw SQL must not leave the machine.
  • Exported query text is sanitized.
  • Export payloads now include request-level dedupe metadata:
    • snapshot_schema_version
    • payload_signature
    • section_signatures
    • change_flags
    • full_detail_refresh
    • collected_at_utc
  • Pro export requires:
    • a configured pro_api_key
    • user consent
    • successful consent sync to the server

If consent is missing or not synced, PgDoctor blocks export before sending snapshots.

Unchanged snapshots are not enqueued indefinitely:

  • client-side payload signatures suppress duplicate queue writes
  • section signatures and change flags are attached to exported payloads
  • a forced full-detail refresh is still allowed periodically

Security Notes

  • Prefer a dedicated monitoring role for PgDoctor.
  • Avoid using superuser or application credentials in connection_string.
  • Protect ~/.pgdoctor/config.json with OS file permissions.

Create a dedicated read-only monitoring role:

CREATE ROLE pgdoctor_monitor WITH LOGIN PASSWORD 'change-this-strong-password';
GRANT pg_monitor TO pgdoctor_monitor;

Then use it in config:

{
  "connection_string": "postgresql://pgdoctor_monitor:change-this-strong-password@localhost:5432/postgres"
}

Health Score Summary

The score is built from penalties:

  • Slow-query penalty
  • Heavy-query penalty
  • Lock waiter penalty

The report prints:

  • Health Score
  • Penalty Breakdown

Development

cargo check
cargo test

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages