PostgreSQL Redundant Indexes: Is an Index on (a) Redundant When You Have (a, b)?
DEV Community

PostgreSQL Redundant Indexes: Is an Index on (a) Redundant When You Have (a, b)?

Disclosure: I build Schemity, a desktop ERD tool - this post is from our blog and uses it for the examples.

TL;DR

A plain b-tree index on (a) is redundant when another b-tree index starts with a, such as (a, b): PostgreSQL 18.3 served the same lookup from the wider index with the same number of heap reads.

Do not drop it on that rule if it is UNIQUE, uses a different operator class or has INCLUDE columns the wider one lacks, and judge a partial or hash index on its own. Usage counters will not tell you which one to drop.

You can usually drop a PostgreSQL index on (a) once a b-tree index on (a, b) exists, because the wider index finds the same rows for every lookup on a.

The exceptions matter more than the rule: several kinds of index share a leading column with a wider one and cannot be judged from the column list, and dropping the wrong one breaks a query or a constraint.

Everything below ran on PostgreSQL 18.3 in a throwaway container, on an orders table with 1,000,000 rows (89 MB) and these indexes:

CREATE TABLE orders (
  id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  customer_id bigint NOT NULL REFERENCES customers (id),
  status text NOT NULL,
  email text NOT NULL,
  total numeric (12, 2) NOT NULL,
  created_at timestamptz NOT NULL
);
CREATE INDEX orders_customer_id_idx ON orders (customer_id);
CREATE INDEX orders_customer_id_created_at_idx ON orders (customer_id, created_at);
CREATE UNIQUE INDEX orders_email_key ON orders (email);
CREATE INDEX orders_email_status_idx ON orders (email, status);
CREATE INDEX orders_status_open_idx ON orders (status) WHERE status = 'open';
CREATE INDEX orders_status_created_at_idx ON orders (status, created_at);
CREATE INDEX orders_email_pattern_idx ON orders (email text_pattern_ops);
CREATE INDEX orders_customer_id_hash_idx ON orders USING hash (customer_id);
CREATE INDEX orders_customer_id_total_idx ON orders (customer_id) INCLUDE (total);
CREATE INDEX orders_id_idx ON orders (id);
CREATE INDEX orders_created_at_desc_idx ON orders (created_at DESC);
CREATE INDEX orders_created_at_id_idx ON orders (created_at, id);

Postgres redundant index: is (a) redundant when (a, b) exists?

Yes, for two plain b-tree indexes: the wider one found the same rows with the same heap reads.

With only orders_customer_id_idx available, WHERE customer_id = 4242 returned its 20 rows through a bitmap scan that read 20 heap blocks. With only orders_customer_id_created_at_idx available, the plan was the same shape with the same 20 heap blocks.

A b-tree is sorted by its first column first, so a search on that column descends either index the same way.

With only these two present, the planner picked the wider one for the plain lookup, and it is the only one of the two that can also return a customer's orders already sorted:

EXPLAIN SELECT * FROM orders WHERE customer_id = 4242 ORDER BY created_at DESC LIMIT 5;
-- Index Scan Backward using orders_customer_id_created_at_idx

The narrow index is smaller, 7.4 MB against 30 MB, because b-tree deduplication packs the repeated customer_id values and cannot pack the wider keys, which are all distinct.

That is the one thing it has going for it, and it shows in one kind of query: a count over 10,000 customers read 199 buffers from the narrow index and 773 from the wide one. It is rarely enough to keep a second index that every write has to maintain.

Three indexes on this table are redundant in that sense:

  • orders_customer_id_idx (customer_id) - covered by orders_customer_id_created_at_idx - Leading column of a wider index
  • orders_id_idx (id) - covered by orders_pkey - Same column as the primary key's index
  • orders_created_at_desc_idx (created_at DESC) - covered by orders_created_at_id_idx - A one-column index reads in either direction

The last one surprises people. ORDER BY created_at DESC LIMIT 10 ran as Index Scan Backward using orders_created_at_id_idx once the DESC index was gone.

Direction only matters in a multi-column index whose columns sort in different directions, or when an index puts NULLs in the non-default place (DESC NULLS LAST, ASC NULLS FIRST).

PostgreSQL also accepted two identical CREATE INDEX statements under different names with no warning, so exact duplicates are easy to collect.

Which indexes look redundant and are not?

Five on this table share a leading column with a wider index and cannot be called redundant from the column list: three do something the wider one cannot, and two are a judgment call.

  • orders_email_key, UNIQUE (email) - Looks covered by (email, status) - Refuses a second row with the same email
  • orders_status_open_idx, partial on status = 'open' - Looks covered by (status, created_at) - Judgment call: serves nothing new, but is 1.7 MB against 32 MB
  • orders_email_pattern_idx, text_pattern_ops - Looks covered by (email, status) - Serves LIKE 'user1234%'
  • orders_customer_id_total_idx, INCLUDE (total) - Looks covered by (customer_id, created_at) - Answers sum(total) from the index alone
  • orders_customer_id_hash_idx, hash - Looks covered by (customer_id, created_at) - Judgment call: another access method, not comparable by columns; here it serves nothing the b-tree does not

The unique index

With orders_email_key dropped, an insert of an email that already existed went through and the table held two rows for it. (email, status) allows that, because the pair differs. Here the wider index is the one to question: with email unique, a lookup by email finds at most one row either way.

The pattern index

This database uses the en_US.utf8 collation, where an ordinary b-tree cannot serve a LIKE prefix. WHERE email LIKE 'user1234%' used orders_email_pattern_idx, and with that index dropped it became a parallel sequential scan of the whole table.

The INCLUDE index

SELECT sum(total) FROM orders WHERE customer_id = 4242 ran as an index-only scan that read 5 buffers. Without the index it read 24, because it had to visit the table for total.

The hash index

With all twelve indexes present, the planner sent the plain customer_id = 4242 lookup to the hash index. It is 31 MB, larger than either b-tree on the column, and the b-trees answer the same lookup, so it is the one of the five most likely to go.

Does an index on the second column help, and what about skip scan?

An index on (b) is not made redundant by (a, b), though PostgreSQL 18 can now use the wider index for it at a higher cost.

Before PostgreSQL 18, a lookup by created_at alone could not use orders_status_created_at_idx (status, created_at) efficiently. PostgreSQL 18 added the b-tree skip scan (release notes: "Allow skip scans of btree indexes"), which searches the index roughly once per distinct value of the leading column, plus the probes that find each next value.

status has four values here, and with both created_at indexes dropped the lookup took nine searches:

Index Scan using orders_status_created_at_idx on orders
Index Cond: (created_at = '2026-01-05 00:00:00+00'::timestamp with time zone)
Index Searches: 9
Buffers: shared hit=14 read=19

That is 33 buffers. The dedicated index did the same lookup in one search and 4 buffers.

So the skip scan makes a missing index survivable when the leading column has a handful of values; it is not a reason to call an index on the second column redundant, and with a high-cardinality leading column the planner does not choose it.

What does a redundant index cost?

Disk space and time on writes, and how much time depends on the order the rows arrive in. I inserted 200,000 rows three times with all twelve indexes and three times with the three redundant ones dropped:

  • Load with rows in increasing order of id and time:
    • All twelve indexes: 7.2, 7.4, 7.5 s
    • Without the three redundant: 7.0, 7.0, 8.3 s
  • Load with random customer_id and created_at:
    • All twelve indexes: 14.6, 16.1, 17.1 s
    • Without the three redundant: 11.5, 12.4, 13.0 s

On the append-only load the difference is inside the noise. On the random load the three indexes cost 3 to 4 seconds every time, about a fifth of the load. Cutting the table to four indexes brought the first load down to 4.1 s.

The three redundant indexes also hold 50 MB between them, more than half the size of the table.

Dropping one does not change which rows any query finds. The only queries that read more afterwards are wide range or index-only scans on the leading column, like the count above.

The cost is easy to miss because it never shows up as a slow query. It shows up as slower writes, more WAL, longer VACUUM and a bigger backup.

How do you find redundant indexes in PostgreSQL?

Compare the leading key columns of every pair of indexes on a table in pg_index, and skip the kinds that are never redundant.

SELECT a.indexrelid::regclass AS redundant, b.indexrelid::regclass AS covered_by
FROM pg_index a
JOIN pg_index b ON b.indrelid = a.indrelid AND b.indexrelid <> a.indexrelid
JOIN pg_class ac ON ac.oid = a.indexrelid
JOIN pg_class bc ON bc.oid = b.indexrelid
WHERE NOT a.indisunique
AND a.indisvalid AND b.indisvalid
AND a.indpred IS NULL AND b.indpred IS NULL
AND a.indexprs IS NULL AND b.indexprs IS NULL
AND ac.relam = bc.relam
AND a.indnkeyatts = a.indnatts
AND a.indnkeyatts <= b.indnkeyatts
AND (a.indkey::int2[])[0:a.indnkeyatts-1] = (b.indkey::int2[])[0:a.indnkeyatts-1]
AND (a.indclass::oid[])[0:a.indnkeyatts-1] = (b.indclass::oid[])[0:a.indnkeyatts-1]
AND (a.indcollation::oid[])[0:a.indnkeyatts-1] = (b.indcollation::oid[])[0:a.indnkeyatts-1]
AND (a.indnkeyatts < b.indnkeyatts OR b.indisunique OR a.indexrelid > b.indexrelid)
ORDER BY 1, 2;

On the example it returns exactly the three redundant indexes and none of the five look-alikes. It ignores sort direction and NULL placement, which is right for a plain one-column index and needs a second look when an index mixes ASC and DESC or sets NULLS FIRST or NULLS LAST.

Do not use scan counts for this. After 1,000 runs of SELECT count(*) FROM orders WHERE customer_id = $1, pg_stat_user_indexes showed:

  • orders_customer_id_total_idx: 1000
  • orders_customer_id_created_at_idx: 0
  • orders_customer_id_idx: 0
  • orders_customer_id_hash_idx: 0

The planner sent every lookup to one index, so the other three read as unused, including the one that the sorted query above needs. A count of 0 says the planner had a better choice for the queries that ran, not that the index does nothing.

Before dropping one, check that nothing names it, such as a query hint from an extension or a CLUSTER. Then drop it with DROP INDEX CONCURRENTLY so writes are not blocked.

How does Schemity find redundant indexes?

Schemity's schema lint has a redundant-index rule that reports an index whose columns are a leading prefix of another index, a unique constraint or the primary key. Schemity is database design software that reads your live database, shows the impact of every schema change before it runs, and keeps the diagram as a file in Git.

Read from this database, schema lint (Schemity Pro) reports two findings on orders:

  • orders_customer_id_idx, "a leading prefix of orders_customer_id_created_at_idx (customer_id, created_at)"
  • orders_id_idx, "the same as orders_pkey"

It leaves the five look-alikes alone: a unique index enforces something, an operator class and an INCLUDE list serve queries the wider index does not, and a partial index or another access method is yours to judge. It also leaves the DESC index alone, so that one takes the query above to find.

Each finding sits on the table in the diagram, can be searched, can be ignored (the choice is saved in the diagram file), and has a Fix button that removes the index as one undoable edit. The drop then goes through the same migration preview as any other change.

That is the useful order for a schema you did not write: understand your schema first, including which of its indexes are only weight.

Redundant indexes are one of the leftovers a long-lived database collects. Another is the sequence nothing uses any more, covered in finding unused sequences in PostgreSQL, and the index that must not be dropped is in cannot drop index needed in a foreign key constraint.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.