Rows Blocking a Unique Index

A migration is about to run CREATE UNIQUE INDEX ON account (email) and it fails. Find the rows that stop it.

Return one row per blocking value:

  • email — the duplicated value, exactly as stored
  • copies — how many rows hold it
  • keep_id — the lowest id among them, the row the cleanup will keep

Order by email.

Two things the index does not consider duplicates. NULL values — a unique index permits any number of them — and values differing in case, because comparison is case-sensitive. Report neither.

Expected output


email    | copies | keep_id
---------+--------+--------
a@x.test | 3      | 1
b@x.test | 2      | 4
PostgreSQL

Press Run to start the SQL engine.

SensAI
Beta
Tap the mic to stop voice mode