← Back to PostgreSQL, page 3

postgresql-0057 · Medium mid

Why does ON CONFLICT fail with 'no unique or exclusion constraint matching' when the unique index is partial?

ON CONFLICT (cols) must name columns that match a unique index or constraint the planner can infer. For a partial unique index, the conflict target must repeat the index predicate: ON CONFLICT (email) WHERE deleted_at IS NULL. Without the WHERE, no arbiter index matches and PostgreSQL raises an error.

Full answer

Tables and rows used here:

sql
CREATE TABLE members (id int GENERATED ALWAYS AS IDENTITY, email text, name text, deleted_at date);
CREATE UNIQUE INDEX members_active_email ON members (email) WHERE deleted_at IS NULL;
INSERT INTO members (email, name, deleted_at) VALUES ('a@x.in', 'Old Asha', '2026-01-01'), ('a@x.in', 'Asha', NULL);
sql
INSERT INTO members (email, name) VALUES ('a@x.in', 'Asha R') ON CONFLICT (email) DO UPDATE SET name = EXCLUDED.name;

Output:

text
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
sql
INSERT INTO members (email, name) VALUES ('a@x.in', 'Asha R') ON CONFLICT (email) WHERE deleted_at IS NULL DO UPDATE SET name = EXCLUDED.name; SELECT email, name, deleted_at FROM members ORDER BY id;

PostgreSQL returns:

text
 email  |   name   | deleted_at
--------+----------+------------
 a@x.in | Old Asha | 2026-01-01
 a@x.in | Asha R   | NULL
(2 rows)

The partial index allows one active row per email plus any number of deleted ones, which is why the setup holds two rows for a@x.in. The first upsert fails with 'there is no unique or exclusion constraint matching the ON CONFLICT specification'. Repeating the predicate lets PostgreSQL use the partial index as the arbiter, so the active row is renamed while the deleted row is untouched. ON CONFLICT ON CONSTRAINT cannot name a partial index, because it is an index and not a constraint.

sql
CREATE TABLE members (id int GENERATED ALWAYS AS IDENTITY, email text, name text, deleted_at date);
CREATE UNIQUE INDEX members_active_email ON members (email) WHERE deleted_at IS NULL;
INSERT INTO members (email, name, deleted_at) VALUES ('a@x.in', 'Old Asha', '2026-01-01'), ('a@x.in', 'Asha', NULL);

INSERT INTO members (email, name) VALUES ('a@x.in', 'Asha R') ON CONFLICT (email) DO UPDATE SET name = EXCLUDED.name;

INSERT INTO members (email, name) VALUES ('a@x.in', 'Asha R') ON CONFLICT (email) WHERE deleted_at IS NULL DO UPDATE SET name = EXCLUDED.name; SELECT email, name, deleted_at FROM members ORDER BY id;

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • Conflict targets must match a unique index
  • Partial indexes need the predicate repeated
  • EXCLUDED holds the proposed row
  • ON CONSTRAINT works only for constraints
  • Soft-delete designs need partial uniqueness

Common mistakes

  • Omitting the WHERE for partial unique indexes
  • Trying ON CONSTRAINT with an index name

Follow-ups

  • How would you resurrect a soft-deleted member instead?
  • What happens with ON CONFLICT DO NOTHING without a target?

Tips

  • Repeat the predicate