PG Bloat Detective logo

PG Bloat Detective

Read-only PostgreSQL bloat analysis with blocker evidence, index checks, timelines, reports, and MCP tools.

Install PG Bloat Detective

Pick your client, then add it in one click or paste the config.

STDIOPython
~/Library/Application Support/Claude/claude_desktop_config.json
claude_desktop_config.json
{
  "mcpServers": {
    "pg-bloat-detective": {
      "command": "uvx",
      "args": [
        "pg-bloat-detective"
      ]
    }
  }
}

Settings → Developer → Edit Config, merge this into mcpServers, then fully quit and reopen Claude Desktop.

Claude Code command
claude mcp add --transport stdio pg-bloat-detective -- uvx pg-bloat-detective

Run in your terminal. Add --scope project to share it with your team through .mcp.json.

Install not yet sandbox-tested— we haven't run this command through our automated sandbox yet. Not a red flag; we're working through the catalog.

Overview

PG Bloat Detective MCP server analyzes read-only PostgreSQL snapshots to explain table bloat, identify sessions or slots blocking VACUUM, and detect index bloat or unused indexes. It stores each collection in a SQLite timeline and produces verdicts with dead-tuple data, blocker PIDs, query text, and suggested remediation. The MCP interface provides five read-only tools, including `bloat_check` and a live diagnostic that uses a temporary snapshot. Reach for it when a PostgreSQL database needs evidence-based vacuum or REINDEX investigation without allowing the diagnostic process to write to production.

Use cases

  • Investigate why autovacuum is falling behind on a PostgreSQL table
  • Identify the session or slot preventing VACUUM from reclaiming dead tuples
  • Find indexes that need REINDEX instead of another VACUUM
  • Track bloat findings across repeated PostgreSQL snapshots
  • Expose index bloat and scan metrics to Prometheus and Grafana

Key features

  • Read-only PostgreSQL collection with a five-second statement timeout
  • SQLite timeline storage for repeated snapshots
  • Table and index verdicts with PID and query evidence
  • Index bloat checks guarded by a configurable size limit
  • MCP stdio server with live and timeline-based diagnostics
  • Prometheus metrics and HTML, JSON, and dashboard reporting

Security & health

What it can reach and how reliably it runs — from our own checks, not the maintainer’s claims.

ActiveLast checked

Recent health check succeeded.

13/13 checks healthy over the last 52d

Supply-chain signal

No high-severity advisories surfaced by our automated scan.

Critical 0High 0Medium 0Low 0

Scanned 16h ago via OSV.dev · pg-bloat-detective (PyPI)

Adoption & maintenance

Factual signals from GitHub, npm, and our automated checks — not a rating.

Last commit14d agoMost recent push to the default branch.
Directory activity1 viewsConfig copies, upvotes, and views on AllMCPs.

How PG Bloat Detective works

What PG Bloat Detective does

PG Bloat Detective examines PostgreSQL table and index health without modifying the source database. Its findings distinguish ordinary steady-state activity from tables that are losing the autovacuum race, tables whose cleanup is blocked, heaps that need a rewrite, bloated indexes, and indexes with no observed scans.

The analysis combines dead-tuple history from pg_stat_user_tables with approximate tuple measurements from pgstattuple_approx. For blockers, it records the oldest relevant backend_xmin holder and associates the finding with a process ID, transaction age, query text, replication slot, prepared transaction, or active transaction where applicable. Index checks use pgstatindex and calculate bloat from average leaf density.

Verdicts are intended to guide the next operation: leave normal tables alone, run or tune VACUUM for vacuum-starved tables, resolve the named blocker before vacuuming, use a rewrite operation for severe heap bloat, and schedule REINDEX for index bloat. An index reported as unused is a drop candidate that should be checked against broader query statistics first.

How it works

The collector opens a read-only PostgreSQL transaction and applies a five-second statement timeout. Each snapshot is written to one SQLite database, allowing several observations to be compared over time rather than relying on a single sample. The analyze command emits one verdict per table or index, while report can produce a static report or JSON output.

Index inspection is protected by a one-gigabyte cost guard by default. --max-bytes changes that threshold and --allow-large bypasses it. Index bloat above 30 percent becomes index-bloated; zero scans across the collected snapshots becomes index-unused. The live MCP diagnostic instead takes a throwaway snapshot, so it does not alter the stored timeline.

Setup and configuration

Install the package with pip:

Terminal
pip install pg-bloat-detective

Start the MCP process over stdio with:

bash
bloat-mcp

The package also provides a Docker image and a compose demonstration database. The command-line workflow uses collect, analyze, report, and serve; serve exposes Prometheus metrics on port 9187. A database path can be supplied with --db; the image uses timeline.db as its default relative path. The BLOAT_DB setting is used by the server and can point it at the SQLite timeline file.

For development, install the development extras and run the test suite with pytest. The demonstrated PostgreSQL connection uses a local database on port 5433, while the collector can target PostgreSQL 12 through 17 deployments, including hosted or self-managed instances.

Tools and capabilities

  • Collect read-only PostgreSQL snapshots into a SQLite timeline.
  • Report table and index verdicts with supporting evidence and suggested fixes.
  • Identify idle transactions, replication slots, prepared transactions, and active transactions holding back cleanup.
  • Measure index bloat with pgstatindex and track index scan counts.
  • Expose Prometheus metrics including pgbloat_index_bloat_pct and pgbloat_index_scans.
  • Use the MCP tools bloat_check and bloat_live_diagnose from an MCP client.

The MCP server exposes five read-only tools in total. bloat_check reads findings from the timeline, while bloat_live_diagnose checks a database through a temporary snapshot. The stored findings can also feed the included HTML report and dashboard or be graphed with Grafana.

Limitations and notes

The collector is diagnostic and read-only; it does not run VACUUM, REINDEX, DROP, or rewrite operations. Index inspection can be expensive, which is why the size guard exists. A reported unused index is based on zero scans across the collected snapshots and should be validated with pg_stat_statements before removal.

PostgreSQL statistics can lag behind approximate tuple measurements. In that situation, the analyzer can retain a normal verdict and identify the statistics as stale rather than treating the lag as confirmed bloat. A useful timeline therefore depends on collecting more than one snapshot when investigating blockers or index usage.

Written by AllMCPs with AI assistance from the project’s README and listing data. How we build listings

Reviews

No reviews yet — be the first to share how it worked for you.

Frequently Asked Questions about PG Bloat Detective

PG Bloat Detective is an MCP server that connects AI clients to PostgreSQL bloat diagnostics. Its main tools inspect stored read-only snapshots or take a temporary live snapshot, then return table and index verdicts, VACUUM blockers, PIDs, query evidence, and remediation guidance without changing the database.

Install it with `pip install pg-bloat-detective`, then start the stdio server with `bloat-mcp`. Docker is also supported, including an image invocation with `docker run -i pg-bloat-detective`.

Yes, PG Bloat Detective works with Cursor through its stdio MCP server. The package also documents Claude Desktop usage and is intended for MCP clients that can launch a stdio server.

Yes, it identifies the oldest relevant `backend_xmin` holder and reports blocker evidence such as the PID, transaction age, and query text. It also checks stale replication slots, prepared transactions, idle transactions, and active transactions.

No, VACUUM does not fix the reported index-bloat condition. PG Bloat Detective measures average leaf density with `pgstatindex`; indexes over 30 percent bloat receive an `index-bloated` verdict and should be considered for REINDEX.

Related MCP servers

Add a badge to your READMEShow it’s listed — live SVG, Markdown or HTML.

AllMCPs Directory Badge

Full Badge Customizer

Showcase your server listing on GitHub or your project documentation. Embed this dynamic SVG badge to highlight official listing status and live engagement.

Badge Style:
Live Dynamic SVG PreviewPG Bloat Detective AllMCPs Directory Badge
Markdown (GitHub README)
[![AllMCPs](https://allmcps.com/api/badge/pg-bloat-detective?style=directory)](https://allmcps.com/mcp/pg-bloat-detective)
HTML Embed
<a href="https://allmcps.com/mcp/pg-bloat-detective"><img src="https://allmcps.com/api/badge/pg-bloat-detective?style=directory" alt="PG Bloat Detective on AllMCPs" /></a>