---
title: "Should you drop an index that shows idx_scan = 0? | Proofline"
description: "Drop it only if it backs no constraint, no server in the cluster has scanned it, and its counters cover a full business cycle."
url: https://proofline.sh/learn/unused-index-finding/
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/)

# Should you drop an index that shows idx\_scan = 0?

Drop it only if it backs no constraint, no server in the cluster has scanned it, and its counters cover a full business cycle. A unique or primary key index enforces its constraint without counting a scan, counters restart after pg\_stat\_reset() or a crash, and each replica keeps its own. Check all three before you drop anything.

Code review The Proofline team November 25, 2026 5 min read

## What the advisor counted

Advisors that report unused PostgreSQL indexes read `idx_scan` from `pg_stat_user_indexes`, the cumulative statistics view with one row per index. PostgreSQL 16 added `last_idx_scan`, documented as "The time of the last scan on this index, based on the most recent transaction stop time" ([Monitoring Database Activity, REL\_16\_15](https://github.com/postgres/postgres/blob/REL_16_15/doc/src/sgml/monitoring.sgml)). Both describe one server since its last reset, and that scope is what the finding leaves out.

The fixture below is constructed: a PostgreSQL 16.15 primary on port 55432 and a streaming replica on port 55433, built with `pg_basebackup`. `accounts` has 200,000 rows, a primary key, a unique constraint on `email`, and two ordinary indexes, on `created_at` and `region`. The application inserted three rows on the primary, one of which was rejected as a duplicate email. A monthly report ran two date-range queries on the replica. The advisor's query on the primary then returned:

```
=> SELECT indexrelname, idx_scan, last_idx_scan, pg_size_pretty(pg_relation_size(indexrelid)) AS size
   FROM pg_stat_user_indexes WHERE relname = 'accounts' ORDER BY indexrelname;
      indexrelname       | idx_scan | last_idx_scan |  size
-------------------------+----------+---------------+---------
 accounts_created_at_idx |        0 |               | 4408 kB
 accounts_email_key      |        0 |               | 14 MB
 accounts_pkey           |        0 |               | 4408 kB
 accounts_region_idx     |        0 |               | 1368 kB
```

Every index reads zero, including the 14 MB unique index that had just rejected a duplicate. Only one of the four is unused.

## Constraint indexes enforce without scanning

The duplicate insert failed on the unique index while its counter stayed at zero:

```
ERROR:  duplicate key value violates unique constraint "accounts_email_key"
DETAIL:  Key (email)=(user7@example.com) already exists.
```

The PostgreSQL manual states that the index created for a unique constraint or primary key "is the mechanism that enforces the constraint" ([Unique Indexes, REL\_16\_15](https://github.com/postgres/postgres/blob/REL_16_15/doc/src/sgml/indices.sgml)). Every insert, and every update that changes the key, consults it, and none of those checks shows up as a scan. Join the advisor's list to the catalogs before reading anything else:

```
=> SELECT s.indexrelname, s.idx_scan, i.indisunique, i.indisprimary, c.conname, c.contype
   FROM pg_stat_user_indexes s
   JOIN pg_index i ON i.indexrelid = s.indexrelid
   LEFT JOIN pg_constraint c ON c.conindid = s.indexrelid
   WHERE s.relname = 'accounts' AND s.idx_scan = 0 ORDER BY s.indexrelname;
      indexrelname       | idx_scan | indisunique | indisprimary |      conname       | contype
-------------------------+----------+-------------+--------------+--------------------+---------
 accounts_created_at_idx |        0 | f           | f            |                    |
 accounts_email_key      |        0 | t           | f            | accounts_email_key | u
 accounts_pkey           |        0 | t           | t            | accounts_pkey      | p
 accounts_region_idx     |        0 | f           | f            |                    |
```

Remove every row with a constraint or with `indisunique` true. The second test matters because a `CREATE UNIQUE INDEX` with no constraint still rejects duplicates and has no `pg_constraint` row. PostgreSQL refuses the drop for the constraint case, which is a useful check if a migration tries it:

```
=> DROP INDEX accounts_email_key;
ERROR:  cannot drop index accounts_email_key because constraint accounts_email_key on table accounts requires it
HINT:  You can drop constraint accounts_email_key on table accounts instead.
```

## Counters only cover the time since the last reset

`idx_scan = 0` means "no scans since the counters started". Two events start them again. The first is a reset by an administrator or a monitoring tool. After a query used `accounts_created_at_idx` on the primary (`idx_scan` 1), `SELECT pg_stat_reset()` returned it to zero, and the database row recorded when:

```
=> SELECT d.stats_reset, pg_postmaster_start_time() AS server_started, now()
   FROM pg_stat_database d WHERE d.datname = current_database();
          stats_reset          |        server_started         |              now
-------------------------------+-------------------------------+-------------------------------
 2026-11-25 08:36:37.177481+00 | 2026-11-25 08:36:28.676479+00 | 2026-11-25 08:36:37.283373+00
```

The second leaves less trace. The manual says that "when starting from an unclean shutdown (e.g., after an immediate shutdown, a server crash, starting from a base backup, and point-in-time recovery), all statistics counters are reset." In the fixture, `accounts_created_at_idx` showed `idx_scan` 1 before `pg_ctl -m immediate stop` and 0 after the restart, while `stats_reset` stayed empty. The server log carried the evidence instead: `redo starts at 0/2000028` and `checkpoint starting: end-of-recovery immediate wait`.

So the window the counter covers starts at the latest of `stats_reset`, a crash recovery or restore in the server log, and the server's creation. `pg_postmaster_start_time()` gives a conservative start when you cannot read the log. If that window is shorter than the slowest legitimate user of the index (a month-end report, a quarterly export, a yearly audit), the zero does not support a drop.

## Each server counts its own scans

The same query on the replica returned a different answer:

```
      indexrelname       | idx_scan |        last_idx_scan         |  size
-------------------------+----------+------------------------------+---------
 accounts_created_at_idx |        2 | 2026-11-25 08:36:01.01976+00 | 4408 kB
 accounts_email_key      |        0 |                              | 14 MB
 accounts_pkey           |        0 |                              | 4408 kB
 accounts_region_idx     |        0 |                              | 1368 kB
```

The manual's hot standby chapter states it: "All scans, reads, blocks, index usage, etc., will be recorded normally on the standby." ([Hot Standby, REL\_16\_15](https://github.com/postgres/postgres/blob/REL_16_15/doc/src/sgml/high-availability.sgml)) Those scans never reach the primary's counters. A physical replica cannot hold an index of its own, though, so dropping `accounts_created_at_idx` on the primary removes it from the replica too, and the report would lose it. Run the advisor's query on every server that serves reads, and add the counts.

## The disconfirmation: an index nobody uses

`accounts_region_idx` passes every test. It backs no constraint and is not unique, it reads zero on the primary and on the replica, and the replica's counters have run since it was built. In this fixture the counters span minutes, which would not be enough. In production, accept the finding only when the window above covers a full business cycle. Save the definition, then drop it without blocking writes:

```
$ psql -p 55432 -Atc "SELECT pg_get_indexdef('accounts_region_idx'::regclass)"
CREATE INDEX accounts_region_idx ON public.accounts USING btree (region)
$ psql -p 55432 -c "DROP INDEX CONCURRENTLY accounts_region_idx"
```

The drop returned without error, and the replica's index list then showed `accounts_created_at_idx`, `accounts_email_key` and `accounts_pkey` only. `CONCURRENTLY` matters on a busy table: without it, "A normal DROP INDEX acquires an ACCESS EXCLUSIVE lock on the table, blocking other accesses until the index drop can be completed" ([DROP INDEX, REL\_16\_15](https://github.com/postgres/postgres/blob/REL_16_15/doc/src/sgml/ref/drop_index.sgml)). The saved definition lets you recreate the index if a reader turns up later.

## Decide with the window and every server

| What the finding says | Evidence to collect | Decision |
| --- | --- | --- |
| `idx_scan = 0` on a primary key or unique index | `pg_index.indisunique`, `pg_constraint.conindid` | Dismiss; it enforces the constraint |
| `idx_scan = 0`, counters recently reset | `pg_stat_database.stats_reset`, server log for crash recovery or restore, `pg_postmaster_start_time()` | Collect again after a full business cycle |
| `idx_scan = 0` on the primary only | The same query on every replica that serves reads | Keep the index if any server scans it |
| `idx_scan = 0` everywhere, no constraint, window covers a full cycle | The index definition from `pg_get_indexdef` | Drop with `DROP INDEX CONCURRENTLY`; record the window and the definition |

When an advisor reports the opposite, a scan that an index might have avoided, the [guide to sequential scan findings](/learn/sequential-scan-finding/) covers the plan evidence. Dropping an index is a schema change, and the [guide to PostgreSQL migration review](/learn/database-migration-review/) covers how to ship it alongside the application versions still running.

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

-   ### [Does a sequential scan finding mean an index is missing?](/learn/sequential-scan-finding/)
    
    Code review The Proofline team November 25, 2026 6 min read
    
-   ### [PostgreSQL migration review for rolling deployments](/learn/database-migration-review/)
    
    Breaking migrations The Proofline team October 7, 2026 7 min read
