# PostgreSQL Performance (read-only) [Health: Active]

**Category:** 🗄️ Databases  
**Repository:** https://github.com/gouranshul/pg-perf-mcp  
**GitHub Stars:** 0  
**Views:** 1  
**Installs:** 0  
**Upvotes:** 0  
**Directory Page:** https://allmcps.com/mcp/postgresql-performance-read-only

## Description
Read-only PostgreSQL performance diagnostics: slow queries, EXPLAIN, index advice, locks, functions

## Tools
Capabilities this server exposes over MCP:

- **top_slow_queries** — Most expensive statements from `pg_stat_statements`: normalized text, calls, total/mean/max ms, rows, cache hit ratio, share of total time. Order by `total_time`, `mean_time`, `calls` or `rows`.
- **explain_query** — Summarized plan: 5 costliest nodes, seq scans on large tables, 10x row misestimates, disk sorts/hash spills, nested loops with many iterations. `analyze=true` adds real timings. `$1` placeholders get a generic plan.
- **suggest_indexes** — CREATE INDEX CONCURRENTLY` candidates from filtered seq scans, join keys and `ORDER BY ... LIMIT`, each with a reason; validated with hypothetical indexes when `hypopg` is installed.
- **unused_indexes** — Indexes with `idx_scan = 0`, largest first, excluding primary key, unique and constraint indexes; includes size, definition and a `DROP INDEX CONCURRENTLY` to review.
- **table_health** — Live/dead tuples, dead ratio, last (auto)vacuum/analyze, rows changed since analyze, seq vs index scans, sizes, with plain-language warnings.
- **blocking_sessions** — Blocked/blocking pairs via `pg_blocking_pids`, truncated query texts, wait time, lock type/mode/table, blocker state, root blockers.
- **slow_functions** — User-defined functions (PL/pgSQL, SQL, ...) ranked by total, self or mean time or calls, from `pg_stat_user_functions`, with language and volatility.
- **explain_function** — Looks **inside** the functions a query calls: time per function, every statement executed inside them with calls, time and its own analyzed plan, and findings such as a seq scan inside a function, a function called once per row, or a read-only function left `VOLATILE`. Includes the function source.

## Claude Desktop Quick Installation
No generated config for this server: it installs through its own setup steps.

The project's own setup steps (from https://github.com/gouranshul/pg-perf-mcp), copied verbatim. Follow them in order; they are not an mcpServers config.

1. Start the server:

```sh
docker compose up
```

2. Install:

```sh
docker pull ghcr.io/gouranshul/pg-perf-mcp:0.1.0
```

3. Install:

```sh
cat > pg-perf.env <<'EOF'
PGPERF_DB_URL=jdbc:postgresql://host.docker.internal:5432/mydb
PGPERF_DB_USER=mcp_readonly
PGPERF_DB_PASSWORD=<the role's password>
MCP_API_KEY=<32+ random characters>
EOF
```

4. Start the server:

```sh
docker run --rm --env-file pg-perf.env -p 127.0.0.1:8080:8080 ghcr.io/gouranshul/pg-perf-mcp:0.1.0
```

5. Install:

```sh
git clone https://github.com/gouranshul/pg-perf-mcp && cd pg-perf-mcp
```

6. Configure:

```sh
cp .env.example .env        # then replace the placeholder secrets
```

## Documentation

## What PostgreSQL Performance (read-only) does

PostgreSQL Performance (read-only) MCP server gives AI assistants a controlled interface for investigating PostgreSQL performance. It gathers data from `pg_stat_statements`, PostgreSQL catalogs, execution plans, lock views, and function statistics. The server is intended for diagnosis rather than remediation: it can recommend an index or report a blocking session, but it does not apply changes, drop indexes, cancel sessions, or terminate backends.

The server also exposes two schema resources. `pg://schema/overview` reports table estimates, sizes, and indexes, while `pg://schema/{table}` returns columns, indexes with scan counts, and foreign keys that lack indexes. The `diagnose_slow_database` prompt provides a staged investigation covering queries, plans, indexes, table condition, functions, and a prioritized conclusion.

## How it works

Requests arrive over MCP using streamable HTTP and bearer-key authorization. Tool execution passes through an SQL guard, an audited runner, and a read-only PostgreSQL executor. The guard accepts one plain `SELECT` and rejects writes, DDL, multiple statements, data-changing CTEs, locking clauses, dangerous functions, and other unsafe constructs.

Each operation runs in a read-only transaction with statement and lock timeouts, then rolls back. Results are limited to a default maximum of 200 rows. The connection role is expected to have monitoring and statistics access, including `pg_read_all_stats` or `pg_monitor` as described by the project. Audit records store a hash and short preview of SQL rather than the complete statement, and errors omit stack traces and connection details.

For function analysis, the server can enable `auto_explain` only within the current transaction. It combines transaction-level function statistics with statement statistics to show time spent inside nested function statements and identify patterns such as a function executed once per result row.

## Tools and capabilities

The PostgreSQL Performance (read-only) MCP server provides these tools:

- `top_slow_queries` ranks statements by total time, mean time, calls, or rows and includes execution, cache, and workload-share metrics.
- `explain_query` summarizes costly plan nodes, large sequential scans, row-estimate errors, disk spills, and expensive nested loops. Optional analysis adds measured timings.
- `suggest_indexes` proposes index candidates from scans, joins, and ordering patterns. When `hypopg` is available, hypothetical indexes can be used to compare planner estimates; no index is created.
- `unused_indexes` lists zero-scan indexes while excluding primary, unique, and constraint indexes, and supplies a drop statement for review only.
- `table_health` reports tuple condition, vacuum and analyze history, table sizes, and sequential versus index scans.
- `blocking_sessions` identifies blocked and blocking pairs, wait information, lock details, blocker state, and root blockers.
- `slow_functions` ranks user-defined functions using PostgreSQL function statistics and includes language and volatility.
- `explain_function` inspects statements executed within called functions, their plans, timing, and related findings.

## Setup and configuration

The documented architecture connects the MCP service to PostgreSQL through a read-only role and expects PostgreSQL statistics features such as `pg_stat_statements`. Function diagnostics require `track_functions`. Hypothetical index validation requires the optional `hypopg` extension. The project identifies PostgreSQL 18, Java 25, and Spring Boot 4.1 in its repository badges.

The service is shown communicating with clients through streamable HTTP and a bearer API key.

## Limitations and notes

Results depend on the statistics and extensions available in the target database. `explain_query` uses generic plans for `$1` placeholders, and analyzed plans run under the same timeout and rollback protections. Function analysis cannot successfully inspect functions that write because execution remains read-only. Recommendations are review material: index creation, index removal, vacuum changes, and session termination must be performed separately by an authorized operator.

_Full upstream README: https://allmcps.com/mcp/postgresql-performance-read-only/readme_

