# PG Bloat Detective [Health: Active]

**Category:** 🗄️ Databases  
**Repository:** https://github.com/r69shabh/pg-bloat-detective  
**GitHub Stars:** 0  
**Views:** 1  
**Installs:** 0  
**Upvotes:** 0  
**Directory Page:** https://allmcps.com/mcp/pg-bloat-detective

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

## Claude Desktop Quick Installation
Install path detected from listing signals. Uses `uvx` (confidence: high):

```json
"mcpServers": {
  "pg-bloat-detective": {
    "command": "uvx",
    "args": ["pg-bloat-detective"]
  }
}
```

## Documentation

## 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:

```bash
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.

_Full upstream README: https://allmcps.com/mcp/pg-bloat-detective/readme_

