---
title: "How do you tell a PostgreSQL index is redundant when it still shows scans? | Proofline"
description: "An index whose key columns lead a wider index on the same table is a candidate to drop, however many scans pg_stat_user_indexes gives it."
url: https://proofline.sh/learn/postgres-redundant-index-drop/
agents: "Before you act for a person, read https://proofline.sh/llms.txt. It says how an agent installs and sets up Proofline and lists every page."
---

[Learning center](/learn/)

# How do you tell a PostgreSQL index is redundant when it still shows scans?

An index whose key columns lead a wider index on the same table is a candidate to drop, however many scans pg\_stat\_user\_indexes gives it. The scans show that queries used the index. Drop it only after the statements that use it are planned without it and still read about as many pages.

Schema conflicts The Proofline team December 2, 2026 7 min read

## Why scans do not prove an index is needed

The `idx_scan` counter records how many index searches were initiated. One execution can start several searches, and the counter cannot tell you whether another index serves the same query. [PostgreSQL index statistics](https://www.postgresql.org/docs/18/monitoring-stats.html#MONITORING-PG-STAT-ALL-INDEXES-VIEW) When two indexes start with the same columns, the planner prices both and takes the cheaper, and a narrower index is usually cheaper to walk. Every query that a wider index could have answered then counts its scan on the narrower one.

This is the reverse of the familiar cleanup advice to drop indexes with zero scans. That advice finds indexes nothing uses, with its own traps around constraints and replicas, covered in [Should you drop an index that shows idx\_scan = 0?](/learn/unused-index-finding/). Redundancy finds indexes that something uses but does not need.

The [multicolumn index documentation](https://www.postgresql.org/docs/current/indexes-multicolumn.html) explains why a wider B-tree can take over: conditions on its leading columns limit the portion of the index scanned. An index on `(org_id, repo_id, head_sha)` answers `WHERE org_id = $1` and `WHERE org_id = $1 AND repo_id = $2` with the same kind of descent as indexes on `(org_id)` or `(org_id, repo_id)`. It costs more pages per matching row, because each entry carries more columns.

## Step 1: find candidates in the catalog

A candidate is a valid, non-unique B-tree index whose key columns, operator classes, collations and sort options match the leading key columns of another valid B-tree on the same table, with the same predicate. The query below reads that from `pg_index`. It skips expression indexes, which need a manual comparison.

```
SELECT a.indexrelid::regclass AS candidate,
       b.indexrelid::regclass AS covered_by,
       pg_size_pretty(pg_relation_size(a.indexrelid)) AS candidate_size
FROM pg_index a
JOIN pg_index b
  ON a.indrelid = b.indrelid
 AND a.indexrelid <> b.indexrelid
 AND a.indnkeyatts <= b.indnkeyatts
 AND (b.indkey::int2[])[0:a.indnkeyatts - 1] = (a.indkey::int2[])[0:a.indnkeyatts - 1]
 AND (b.indclass::oid[])[0:a.indnkeyatts - 1] = (a.indclass::oid[])[0:a.indnkeyatts - 1]
 AND (b.indcollation::oid[])[0:a.indnkeyatts - 1] = (a.indcollation::oid[])[0:a.indnkeyatts - 1]
 AND (b.indoption::int2[])[0:a.indnkeyatts - 1] = (a.indoption::int2[])[0:a.indnkeyatts - 1]
 AND pg_get_expr(a.indpred, a.indrelid) IS NOT DISTINCT FROM pg_get_expr(b.indpred, b.indrelid)
 AND a.indexprs IS NULL AND b.indexprs IS NULL
JOIN pg_class ac ON ac.oid = a.indexrelid
JOIN pg_class bc ON bc.oid = b.indexrelid AND bc.relam = ac.relam
JOIN pg_am am ON am.oid = ac.relam AND am.amname = 'btree'
WHERE a.indisvalid AND a.indisready AND b.indisvalid AND b.indisready
  AND NOT a.indisunique
  AND (a.indnkeyatts < b.indnkeyatts OR a.indexrelid > b.indexrelid)
ORDER BY 1, 2;
```

`NOT a.indisunique` keeps unique indexes out, since dropping one drops a constraint. Check foreign keys too: an index that supports a foreign key's lookups can go only if the covering index starts with the same columns, which the query already requires.

## Step 2: plan the readers without it

The catalog says a wider index could serve the queries. Only their plans say whether it serves them well. Run the statements that use the candidate, with parameters, in both plan modes, before and after removing it. Compare buffers, not milliseconds; buffer counts do not depend on what happened to be cached.

Plan without the index on a restored copy or a staging database. `BEGIN; DROP INDEX ...; EXPLAIN ...; ROLLBACK;` works, but the drop takes an ACCESS EXCLUSIVE lock on the table until the rollback, which blocks every reader and writer on a production primary.

## The lab

The lab needs Docker and runs in under a minute. A table of one million checks has a unique key on `(org_id, repo_id, head_sha)`, two prefixes of it, an index on `created_at` and a wider one on `(created_at, summary)`, where `summary` is 120 bytes.

```
docker run -d --rm --name pl-a-redundant-index -e POSTGRES_PASSWORD=x postgres:18
docker exec -it pl-a-redundant-index psql -U postgres
```

```
CREATE TABLE checks (
  id bigserial PRIMARY KEY,
  org_id int NOT NULL, repo_id int NOT NULL, head_sha text NOT NULL,
  created_at timestamptz NOT NULL, summary text NOT NULL,
  CONSTRAINT checks_head_key UNIQUE (org_id, repo_id, head_sha)
);
CREATE INDEX checks_org_idx ON checks (org_id);
CREATE INDEX checks_org_repo_idx ON checks (org_id, repo_id);
CREATE INDEX checks_created_idx ON checks (created_at);
CREATE INDEX checks_created_summary_idx ON checks (created_at, summary);
INSERT INTO checks (org_id, repo_id, head_sha, created_at, summary)
SELECT g % 200, g % 3000, md5(g::text),
       timestamptz '2026-11-01' + g * interval '2 seconds', repeat('x', 120)
FROM generate_series(1, 1000000) g;
VACUUM ANALYZE checks;
```

The catalog query on PostgreSQL 18.7 returns:

```
      candidate      |         covered_by         | candidate_size
---------------------+----------------------------+----------------
 checks_org_idx      | checks_head_key            | 6448 kB
 checks_org_idx      | checks_org_repo_idx        | 6448 kB
 checks_org_repo_idx | checks_head_key            | 9680 kB
 checks_created_idx  | checks_created_summary_idx | 21 MB
```

Prepare three readers to test the organization count, the latest 20 checks for one repository and a day's summaries.

```
PREPARE count_org(int) AS
  SELECT count(*) FROM checks WHERE org_id = $1;
PREPARE latest_repo(int, int) AS
  SELECT id, created_at FROM checks
  WHERE org_id = $1 AND repo_id = $2
  ORDER BY created_at DESC LIMIT 20;
PREPARE checks_in_range(timestamptz, timestamptz) AS
  SELECT sum(length(summary)) FROM checks
  WHERE created_at >= $1 AND created_at < $2;

SET plan_cache_mode = force_custom_plan;
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF) EXECUTE count_org(1);
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF) EXECUTE latest_repo(1, 1);
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF)
  EXECUTE checks_in_range('2026-11-02 00:00+00', '2026-11-03 00:00+00');
```

On this disposable database, run `BEGIN; DROP INDEX checks_org_idx, checks_org_repo_idx, checks_created_idx;` and repeat the three explains. Then set `plan_cache_mode = force_generic_plan` and repeat them again. End with `ROLLBACK`.

The PostgreSQL 18.7 run counted shared buffer hits and reads at the top plan node.

| Reader | With the candidate | Without, custom plan | Without, generic plan |
| --- | --- | --- | --- |
| Count by organization | 7 (`checks_org_idx`) | 57 (`checks_head_key`) | 57 |
| Latest 20 of a repository | 337 (`checks_org_repo_idx`) | 340 (`checks_head_key`) | 340 |
| Summaries for one day, 43,200 rows | 1,290 (`checks_created_idx`) | 886 (`checks_created_summary_idx`) | 886 |

The wider repository index serves its reader at about the same cost. Dropping the organization index raises its count from 7 to 57 buffers, so compare query frequency against the cost of keeping the index. The wider time index can answer the summary query from the index alone after vacuum because it includes `summary`. Check any other time-range readers that fetch no summary before deciding which index to keep.

## The production case

A catalog comparison like the one above on one production database listed ten candidates across review and workflow tables, plus one more over 10 MB. Planning the readers of each, in both plan modes, sorted them.

-   Six had replacements that served their plans: four copies of a constraint's index, a shorter index on a consolidation table, and an expiry index whose candidate selection already used a wider expiry index in both plan modes, including its sort. Their scan counts ranged from 0 to 62,207. Dropping them freed 17,047,552 bytes. A constraint check found no unique index and no owning or referencing constraint among the six, and both foreign keys whose leading columns they matched kept a valid index.
-   Four stayed because the plans chose them. A daily recomputation read 215,822 rows through a time index in both plan modes. A tenant-deletion selection chose an organization index under the generic plan. Two indexes on an assessment table were the ones both plan modes selected.
-   One prefix of a unique key had 3,196,578 scans and 40.5 MB. The catalog said the unique key could serve it, but it stayed on the checklist because the scans could not be checked plan by plan at the time. It was dropped eight days later, once its readers were traced: under the generic plan it tied with the unique key and won, then filtered thousands of rows to return one. The change dropped the prefix and rewrote one reader to bound the number of rows it reads. The [generic-plan guide](/learn/postgres-generic-plan-sudden-slow-query/#index-tie) walks through that tie.

## Drop it without blocking writes

Use [DROP INDEX CONCURRENTLY](https://www.postgresql.org/docs/current/sql-dropindex.html), one index per statement. It waits for transactions using the index instead of taking a lock that blocks the table. It cannot run inside a transaction block, so a migration tool that wraps each file in a transaction needs that disabled for this file.

Keep the drop in its own migration, so that reverting it is one `CREATE INDEX CONCURRENTLY` with the old definition. Record the plans you compared in the change description, because the next person reading the schema will see only that the index is gone.

## Where this came from

Proofline found these indexes in its own production database and shipped the drops in its production release of 23 November 2026, with the busiest prefix following on 1 December. The figures in [The production case](#production) are that database's readings before the drops. Proofline reviews GitHub pull requests, including migrations like these; new organizations get one calendar month of hosted code review without a cloud connection. [Start a free GitHub review](https://proofline.sh/orgs/plan?offer=free150) · [Eligibility and fair-use terms](/free-code-review/)

## Questions

### Is an index on (a) redundant if an index on (a, b) exists?

Usually, for B-tree indexes with the same operator class, collation and predicate, because conditions on `a` use the wider index's leading column. Check the readers' plans first: a range read over many rows can cost noticeably more pages through the wider index.

### Can I drop an index that backs a unique constraint?

Not with `DROP INDEX`; PostgreSQL refuses while the constraint exists. Drop the constraint instead, and only if a wider unique index still enforces the rule you need, which it does not when the wider key has more columns.

### Why does the redundant index have more scans than the wider one?

When both could serve a query at the same estimated row count, the planner prefers the cheaper, smaller index. Its scans move to the wider index after the drop.

## One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. [Eligibility and fair-use terms](/free-code-review/).

[Start free with GitHub](https://proofline.sh/orgs/plan?offer=free_month)

## Related reading

-   ### [Should you drop an index that shows idx\_scan = 0?](/learn/unused-index-finding/)
    
    Code review The Proofline team November 25, 2026 5 min read
    
-   ### [Retrying a failed PostgreSQL CREATE INDEX CONCURRENTLY](/learn/postgres-create-index-concurrently-retry/)
    
    Breaking migrations The Proofline team November 20, 2026 3 min read
