download

Why did UNIQUE(email, deleted_at) still allow duplicate active users?

SQL~10 minReviewed 3 Dec 2026Free · No sign-in

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).

  1. Why does check-then-insert (SELECT then INSERT) race under concurrent signups, and why does the starter's UNIQUE (email, deleted_at) still allow (3, '[email protected]', NULL)?
  2. Write SQL to enforce one active row per email while keeping soft-deleted history, insert (3, '[email protected]', NULL) and (4, '[email protected]', NULL) with ON CONFLICT DO NOTHING, then select all rows ordered by id.
"CODE · SQL"
CREATE TABLE users (
  id INTEGER PRIMARY KEY,
  email TEXT NOT NULL,
  deleted_at TEXT,
  UNIQUE (email, deleted_at)
);
Show hints (3)
  1. "NULL IS DISTINCT"

    Standard SQL unique constraints treat every NULL as distinct from every other NULL. Two ('[email protected]', NULL) rows do not conflict under UNIQUE (email, deleted_at).

  2. "INDEX ACTIVE ROWS"

    Create a partial unique index on users (email) with WHERE deleted_at IS NULL so only active accounts must be unique.

  3. "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.

"SOLUTION · SQL"
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;
"OUTPUT"
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 designAllows soft-deleted history?Blocks two active [email protected] rows?Index size
UNIQUE (email)No (blocks re-signup)YesAll rows
UNIQUE (email, deleted_at)Yes (distinct timestamps)No (NULL != NULL)All rows
UNIQUE INDEX (email) WHERE deleted_at IS NULLYesYesActive rows only
UNIQUE NULLS NOT DISTINCT (PostgreSQL 15+)Yes (distinct timestamps)YesAll rows

Common mistakes

  • Running SELECT before INSERT without a unique index. Two concurrent signups both pass the check and create duplicate accounts.
  • Adding UNIQUE (email, deleted_at) and assuming NULL matches NULL. Both SQLite and PostgreSQL by default accept multiple ('[email protected]', NULL) active rows.
  • Omitting WHERE deleted_at IS NULL from ON 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) treats NULL values 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 (1 for inserted, 0 for ignored) or append RETURNING id. When DO NOTHING skips a conflicting row, RETURNING returns 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 →

Original OfferVibe question. SQLite Partial Indexes · SQLite UNIQUE Constraints · SQLite UPSERT · PostgreSQL Unique Indexes · PostgreSQL Partial Indexes.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.