Skip to main content
Glama
YawLabs

@yawlabs/postgres-mcp

by YawLabs

Find unused indexes

pg_unused_indexes
Read-onlyIdempotent

Identify unused or rarely used indexes that incur write overhead and disk cost, using scan counts and reset age to avoid false conclusions.

Instructions

Indexes that have never been scanned or have very low usage, largest first. Each unused index costs write amplification (every INSERT/UPDATE maintains it) and disk space, so before adding a new index, check whether the fix is to drop a dead one. Returns {rows, stats_reset, stats_reset_age_seconds}. READ THIS BEFORE RECOMMENDING A DROP: scans is a counter, not a verdict. It only counts since the last statistics reset, which is why the top-level stats_reset and stats_reset_age_seconds are part of the answer. If the counters were reset an hour ago, EVERY index looks unused; if stats_reset is null, the start of the window is unknown and the counts prove nothing. This list is only trustworthy once the reset age comfortably exceeds the slowest cycle that could use the index - a monthly report, a quarterly close, a yearly job, a failover-only query path. PRIMARY KEY and UNIQUE indexes are already excluded from these results: they enforce a constraint and stay load-bearing at zero scans, so they never appear here and their absence is not evidence of anything. On PostgreSQL 16+ each row also carries last_idx_scan, the timestamp of the most recent scan (null = never scanned since the reset). 'Not scanned since 2026-02-14' is a far better basis for a decision than a bare count. On PostgreSQL 18+, do not fall back on the old 'the leading column is never filtered, so this index is dead weight' reasoning. Skip scan lets the planner use a multi-column btree whose leading column is unconstrained, so such an index can now be doing real work.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
limitNoMax rows to return (default 50).
schemaNoLimit to one schema. If omitted, all user schemas are included.
maxScansNoInclude indexes with scan count <= this (default 10). Use 0 for 'never scanned'.

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
rowsYes
_warningsNo
stats_resetYesEvery counter in `rows` is cumulative SINCE this point. Null = start of the window unknown.
stats_reset_age_secondsYesSeconds since `stats_reset`; null whenever that is null.

Schema Changelog

Changes observed during successful MCP inspections.

  1. Changed6 schema fields changedv0.12.1
    • removedOutput schema / properties / rows / items / properties / last_idx_scan / anyOf
      Removed value: -[
      -  {
      -    "type": "string"
      -  },
      -  {
      -    "type": "null"
      -  }
      -]
    • addedOutput schema / properties / rows / items / properties / last_idx_scan / type
      Added value: +[
      +  "string",
      +  "null"
      +]
    • removedOutput schema / properties / stats_reset / anyOf
      Removed value: -[
      -  {
      -    "type": "string"
      -  },
      -  {
      -    "type": "null"
      -  }
      -]
    • addedOutput schema / properties / stats_reset / type
      Added value: +[
      +  "string",
      +  "null"
      +]
    • removedOutput schema / properties / stats_reset_age_seconds / anyOf
      Removed value: -[
      -  {
      -    "type": "number"
      -  },
      -  {
      -    "type": "null"
      -  }
      -]
    • addedOutput schema / properties / stats_reset_age_seconds / type
      Added value: +[
      +  "number",
      +  "null"
      +]
  2. Changed2 schema fields changedv0.12.0
    • changedInput schema / $schema
      Previous value: -"http://json-schema.org/draft-07/schema#"New value: +"https://json-schema.org/draft/2020-12/schema"
    • changedOutput schema / (root)
      Previous value: -nullNew value: +{
      +  "$schema": "https://json-schema.org/draft/2020-12/schema",
      +  "additionalProperties": false,
      +  "properties": {
      +    "_warnings": {
      +      "items": {
      +        "type": "string"
      +      },
      +      "type": "array"
      +    },
      +    "rows": {
      +      "items": {
      +        "additionalProperties": false,
      +        "properties": {
      +          "definition": {
      +            "type": "string"
      +          },
      +          "index": {
      +            "type": "string"
      +          },
      +          "last_idx_scan": {
      +            "anyOf": [
      +              {
      +                "type": "string"
      +              },
      +              {
      +                "type": "null"
      +              }
      +            ],
      +            "description": "PostgreSQL 16+ only, absent below that. Null = never scanned since the reset. Far better grounds for a drop decision than the bare count."
      +          },
      +          "scans": {
      +            "description": "Bigint as a decimal string. A COUNTER, not a verdict -- read stats_reset first.",
      +            "type": "string"
      +          },
      +          "schema": {
      +            "type": "string"
      +          },
      +          "size_bytes": {
      +            "type": "string"
      +          },
      +          "size_pretty": {
      +            "type": "string"
      +          },
      +          "table": {
      +            "type": "string"
      +          }
      +        },
      +        "required": [
      +          "schema",
      +          "table",
      +          "index",
      +          "scans",
      +          "size_pretty",
      +          "size_bytes",
      +          "definition"
      +        ],
      +        "type": "object"
      +      },
      +      "type": "array"
      +    },
      +    "stats_reset": {
      +      "anyOf": [
      +        {
      +          "type": "string"
      +        },
      +        {
      +          "type": "null"
      +        }
      +      ],
      +      "description": "Every counter in `rows` is cumulative SINCE this point. Null = start of the window unknown."
      +    },
      +    "stats_reset_age_seconds": {
      +      "anyOf": [
      +        {
      +          "type": "number"
      +        },
      +        {
      +          "type": "null"
      +        }
      +      ],
      +      "description": "Seconds since `stats_reset`; null whenever that is null."
      +    }
      +  },
      +  "required": [
      +    "rows",
      +    "stats_reset",
      +    "stats_reset_age_seconds"
      +  ],
      +  "type": "object"
      +}
  3. First observedv0.7.0

TDQS

A4.6/5.0
Behavior5/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations already mark the tool read-only and idempotent, and the description adds substantial non-obvious behavioral detail: scan counts are window-dependent counters, stats_reset semantics can invalidate conclusions, PK/UNIQUE indexes are filtered out, and PG16+/PG18+ version differences change how results should be interpreted. This goes well beyond the structured annotations.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is longer than typical, but the extra length is largely justified because misusing this tool can lead to harmful DROP INDEX recommendations. The core result and the critical stats_reset warning are front-loaded, and the version-specific notes earn their place. It could be slightly tightened but is not padded.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a high-stakes diagnostic tool, the description covers the output shape, the main trust caveat, excluded index types, version-specific behavior, and how to reason about scan counts. With the output schema covering field-level details, nothing essential is missing for an agent to use the tool correctly.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema already documents all three parameters, so the baseline is 3. The description adds real semantic value by explaining that maxScans must be interpreted relative to the stats_reset window and that a low count is not a verdict by itself. It also clarifies meaningfully what a value like 0 implies about the reset window.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

States exactly what is returned: indexes that have never been scanned or have very low usage, ordered largest first. It also names the return payload and gives concrete selection criteria, making the tool's resource and behavior unambiguous and distinct from siblings like pg_index_advisor or pg_seq_scan_tables.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Provides explicit guidance on when the output is trustworthy: stats_reset age must exceed the slowest relevant cycle, and null stats_reset means counts prove nothing. It also warns that PK/UNIQUE indexes are deliberately excluded. It does not explicitly name alternative sibling tools, but the contextual guidance is strong enough for correct selection.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.