Why did UNIQUE(email, deleted_at) still allow duplicate active users?
Question
Your users table uses soft deletes: active rows have deleted_at IS NULL, and deleted rows store a UTC date. Existing rows are (1, '[email protected]', '2026-10-01') and (2, '[email protected]', NULL).
- Why does check-then-insert (
SELECTthenINSERT) race under concurrent signups, and why does the starter'sUNIQUE (email, deleted_at)still allow(3, '[email protected]', NULL)? - Write SQL to enforce one active row per email while keeping soft-deleted history, insert
(3, '[email protected]', NULL)and(4, '[email protected]', NULL)withON CONFLICT DO NOTHING, then select all rows ordered byid.
CREATE TABLE users ( id INTEGER PRIMARY KEY, email TEXT NOT NULL, deleted_at TEXT, UNIQUE (email, deleted_at) );
Show hints (3)
- "NULL IS DISTINCT"
Standard SQL unique constraints treat every
NULLas distinct from every otherNULL. Two('[email protected]', NULL)rows do not conflict underUNIQUE (email, deleted_at). - "INDEX ACTIVE ROWS"
Create a partial unique index on
users (email)withWHERE deleted_at IS NULLso only active accounts must be unique. - "MATCH CONFLICT TARGET"
With a partial unique index, repeat the matching predicate in
ON CONFLICT (email) WHERE deleted_at IS NULL DO NOTHING.
Show checked explanation
SQL unique constraints treat NULL values as distinct, so UNIQUE (email, deleted_at) permits unlimited ('[email protected]', NULL) rows. Enforce active uniqueness with a partial unique index on (email) WHERE deleted_at IS NULL, and handle duplicate signups atomically with INSERT ... ON CONFLICT (email) WHERE deleted_at IS NULL DO NOTHING.
CREATE UNIQUE INDEX idx_active_email ON users (email) WHERE deleted_at IS NULL; INSERT INTO users (id, email, deleted_at) VALUES (3, '[email protected]', NULL), (4, '[email protected]', NULL) ON CONFLICT (email) WHERE deleted_at IS NULL DO NOTHING; SELECT id, email, deleted_at FROM users ORDER BY id;
id | email | deleted_at 1 | [email protected] | 2026-10-01 2 | [email protected] | NULL 4 | [email protected] | NULL
Author-checked on SQLite 3.49.1 and CPython 3.12.10: proved UNIQUE(email, deleted_at) permits duplicate NULL active rows, ran the partial unique index and ON CONFLICT solution, tested two-thread concurrent signups in a temporary directory, and checked expression index lower(email). PostgreSQL NULLS NOT DISTINCT semantics checked against official documentation. Checked by an editor other than the author.
Walkthrough
Two separate traps break naive signup uniqueness:
1. Application SELECT then INSERT races. When two requests sign up [email protected] at the same millisecond, both run SELECT and see zero active rows. Both then run INSERT. Only a database-level unique index serializes the write atomically.
2. NULL does not equal NULL in UNIQUE constraints. Under standard SQL (SQLite and PostgreSQL by default), every NULL is distinct. UNIQUE (email, deleted_at) happily accepts (2, '[email protected]', NULL) and (3, '[email protected]', NULL) side by side.
Why a partial unique index solves both. CREATE UNIQUE INDEX idx_active_email ON users (email) WHERE deleted_at IS NULL indexes only active rows. Soft-deleted row 1 ('2026-10-01') stays outside the index, so users can delete an account and re-register later. Meanwhile, active row 2 blocks duplicate active row 3.
Matching the partial index in ON CONFLICT. Both SQLite and PostgreSQL require the conflict target to repeat WHERE deleted_at IS NULL so the engine matches the partial index. Use DO NOTHING to ignore duplicate signups cleanly, or DO UPDATE when a conflict should refresh fields on the existing active row.
| Constraint design | Allows soft-deleted history? | Blocks two active [email protected] rows? | Index size |
|---|---|---|---|
UNIQUE (email) | No (blocks re-signup) | Yes | All rows |
UNIQUE (email, deleted_at) | Yes (distinct timestamps) | No (NULL != NULL) | All rows |
UNIQUE INDEX (email) WHERE deleted_at IS NULL | Yes | Yes | Active rows only |
UNIQUE NULLS NOT DISTINCT (PostgreSQL 15+) | Yes (distinct timestamps) | Yes | All rows |
Common mistakes
- Running
SELECTbeforeINSERTwithout a unique index. Two concurrent signups both pass the check and create duplicate accounts. - Adding
UNIQUE (email, deleted_at)and assumingNULLmatchesNULL. Both SQLite and PostgreSQL by default accept multiple('[email protected]', NULL)active rows. - Omitting
WHERE deleted_at IS NULLfromON CONFLICT (email). The engine cannot match a partial unique index without its matching predicate. - Using plain
UNIQUE (email)on a soft-delete table. Anyone who deletes their account can never sign up again with that email.
Follow-up questions
- "How does PostgreSQL 15+ UNIQUE NULLS NOT DISTINCT compare to a partial unique index?"
UNIQUE NULLS NOT DISTINCT (email, deleted_at)treatsNULLvalues as equal, so it also blocks two('[email protected]', NULL)rows. A partial unique index stays smaller when many rows are soft-deleted and allows multiple deleted rows with the same timestamp. - "What if someone signs up as '[email protected]' while '[email protected]' is already active?" Text comparison is case-sensitive by default. Normalize emails to lowercase in the application and enforce it in the database with
CREATE UNIQUE INDEX idx_active_email ON users (lower(email)) WHERE deleted_at IS NULL. - "How does your API know whether ON CONFLICT DO NOTHING inserted a user or ignored a duplicate?" Check the affected row count (
1for inserted,0for ignored) or appendRETURNING id. WhenDO NOTHINGskips a conflicting row,RETURNINGreturns zero rows.
What a strong answer covers
Explains both the SELECT-then-INSERT race and SQL's NULL uniqueness rule, enforces active uniqueness with a partial unique index, matches its WHERE predicate in ON CONFLICT, and compares PostgreSQL 15+ NULLS NOT DISTINCT.
Open in interactive practice deck →