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
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.
- Rust (stable)
- PostgreSQL with
pg_stat_statementsenabled - Access to
pg_stat_statements,pg_stat_activity, andpg_locks
cargo buildRelease:
cargo build --releaseRelease binary path:
target/release/pgdoctor-core
First-time setup:
cargo run -- initRun a one-shot diagnostic:
cargo run -- checkSee agent and queue status:
cargo run -- statusIf you use the built binary directly:
pgdoctor-core init
pgdoctor-core check
pgdoctor-core statusThink of the CLI in 4 groups: setup, diagnostics, Pro export, and background agent.
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.
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.
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
manualor 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
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>
pgdoctor-core init
pgdoctor-core check
pgdoctor-core config edit
pgdoctor-core check --livepgdoctor-core check --enqueue
pgdoctor-core status
pgdoctor-core flushForeground:
pgdoctor-core agent run --mode continuousBackground:
pgdoctor-core agent start --mode continuous
pgdoctor-core status
pgdoctor-core agent stopoff
- No queue, no push.
manual
- Queue is used.
- Push happens only when you run
flushorcheck --push.
scheduled
- Queue is used.
- Background push worker runs on interval.
continuous
- Queue is used.
- Continuous collection and automatic push run together.
In postgresql.conf:
shared_preload_libraries = 'pg_stat_statements'Then restart PostgreSQL and run:
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;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_stringslow_query_threshold_msthroughput_min_callspro_api_keypro_export_modepro_consent_acknowledged
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:5001In PowerShell:
$env:PGDOCTOR_ENV="dev"
$env:PGDOCTOR_PRO_ENDPOINT="http://localhost:5001"status shows which endpoint policy is active.
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_versionpayload_signaturesection_signatureschange_flagsfull_detail_refreshcollected_at_utc
- Pro export requires:
- a configured
pro_api_key - user consent
- successful consent sync to the server
- a configured
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
- Prefer a dedicated monitoring role for PgDoctor.
- Avoid using superuser or application credentials in
connection_string. - Protect
~/.pgdoctor/config.jsonwith 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"
}The score is built from penalties:
- Slow-query penalty
- Heavy-query penalty
- Lock waiter penalty
The report prints:
Health ScorePenalty Breakdown
cargo check
cargo test