PG Bloat Detective

v0.1.6
io.github.r69shabh/pg-bloat-detective Checked 5 hours ago Published 1 month ago

Read-only Postgres bloat verdicts: blockers, timelines, REINDEX proof.

These tools were reported by the server itself during our smoke test (tools/list), not copied from the README.

bloat_check

Diagnose PostgreSQL bloat from a collected timeline. Read-only: only reads the local SQLite timeline file, never touches Postgres. Args: db: Path to the SQLite timeline file (default: BLOAT_DB env, 'timeline.db'). Collect one first with bloat_collect, or pass any timeline.db path. Returns: findings: list of {table, verdict, evidence, action} — verdict is one of normal | vacuum-starved | blocked-by-idle-xact | blocked-by-slot | blocked-by-prepared | blocked-by-active-xact | needs-rewrite | index-bloated | index-unused. Evidence carries dead/live counts, pgstat…

ParameterTypeDescription
dbstring

bloat_collect

Take a read-only Postgres snapshot into the timeline, then return fresh verdicts. Safe on prod: read-only transaction, 5s statement timeout, needs only pg_stat_* views + optional pgstattuple extension. No writes, no agents. Args: dsn: Postgres DSN, e.g. 'dbname=app user=readonly host=db.internal'. Default: BLOAT_DSN env. Never logs the password. Returns: {snapshot: 'saved', db: path, findings: [...with action per table/index]}.

ParameterTypeDescription
dsnstring

bloat_timeline

Dead-tuple + approx + index-bloat time series: when the bloat started. Read-only on the local timeline file. Args: table: Table name to filter (e.g. 'churn'). Empty = all tables. db: Path to the SQLite timeline file (default: BLOAT_DB env). Returns: {table: {dead: [{ts, live, dead}], approx_pct: [{ts, dead_pct}]}, index_bloat: {index: [{ts, bloat_pct}]}}. Rising dead = vacuum-starved; spike-then-zero = was blocked, blocker left.

ParameterTypeDescription
dbstring
tablestring

bloat_blockers

Who is pinning VACUUM: blocker pid + query + xmin age + slot. Read-only on the local timeline file. Priority: prepared-xact > stale-slot > idle-in-transaction. Fix: end the named session, then VACUUM the table. Args: db: Path to the SQLite timeline file (default: BLOAT_DB env). Returns: {blockers: [{ts, kind, pid, age, query, slot}] — newest first, max 20. Use pid with SELECT pg_terminate_backend(pid) after verification.

ParameterTypeDescription
dbstring

bloat_live_diagnose

One-shot live diagnosis without touching your timeline file. Snapshots into a throwaway temp DB, diagnoses, discards it. Read-only on Postgres (read-only tx, 5s timeout). Safest option for prod DSNs. Args: dsn: Postgres DSN (default: BLOAT_DSN env). Password never logged. Returns: {findings: [...with action], blockers: [...]} — same verdict set as bloat_check, plus the current xmin-holder evidence.

ParameterTypeDescription
dsnstring