Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-29 · container image postgres:17
ON CONFLICT (columns) does not just name columns to compare. PostgreSQL looks for a unique constraint or unique index whose columns (and expression or WHERE condition) match exactly, and uses it as the arbiter. If none matches, the INSERT fails with 42P10 before any row is checked — even when there is no duplicate at all.
01The ON CONFLICT column has no unique constraint
users.email is a plain text column. The upsert ON CONFLICT (email) DO UPDATE has nothing to arbitrate on.
Setup
CREATE TABLE users (id serial PRIMARY KEY, email text, name text);
INSERT INTO users (email, name) VALUES ('kim@example.com', 'Kim');CREATE TABLE
INSERT 0 1INSERT INTO users (email, name) VALUES ('kim@example.com', 'Kim Minsu')
ON CONFLICT (email) DO UPDATE SET name = EXCLUDED.name;ERROR: 42P10: there is no unique or exclusion constraint matching the ON CONFLICT specificationALTER TABLE users ADD CONSTRAINT users_email_key UNIQUE (email);ALTER TABLEAdd UNIQUE (email). The same upsert then updates the existing row: Kim becomes Kim Minsu and there is still only one row. If the table already contains duplicate emails, the ALTER fails, so clean them up first.
INSERT INTO users (email, name) VALUES ('kim@example.com', 'Kim Minsu')
ON CONFLICT (email) DO UPDATE SET name = EXCLUDED.name;
SELECT id, email, name FROM users;INSERT 0 1
id | email | name
----+-----------------+-----------
1 | kim@example.com | Kim Minsu
(1 row)02The unique key has several columns but ON CONFLICT lists only some
The primary key is (warehouse_id, item_code). ON CONFLICT (item_code) alone does not match it — a subset of a composite key is not unique.
Setup
CREATE TABLE stock (warehouse_id int, item_code text, qty int, PRIMARY KEY (warehouse_id, item_code));
INSERT INTO stock VALUES (1, 'A100', 5);CREATE TABLE
INSERT 0 1INSERT INTO stock VALUES (1, 'A100', 3)
ON CONFLICT (item_code) DO UPDATE SET qty = stock.qty + EXCLUDED.qty;ERROR: 42P10: there is no unique or exclusion constraint matching the ON CONFLICT specificationSELECT conname, pg_get_constraintdef(oid) FROM pg_constraint WHERE conrelid = 'stock'::regclass; conname | pg_get_constraintdef
------------+---------------------------------------
stock_pkey | PRIMARY KEY (warehouse_id, item_code)
(1 row)List every column of the key: ON CONFLICT (warehouse_id, item_code). The quantity becomes 5 + 3 = 8. pg_get_constraintdef shows the exact column list.
INSERT INTO stock VALUES (1, 'A100', 3)
ON CONFLICT (warehouse_id, item_code) DO UPDATE SET qty = stock.qty + EXCLUDED.qty;
SELECT * FROM stock;INSERT 0 1
warehouse_id | item_code | qty
--------------+-----------+-----
1 | A100 | 8
(1 row)03The unique index is partial (WHERE deleted_at IS NULL)
Email is unique only among non-deleted members. ON CONFLICT (email) does not match a partial index unless you repeat its condition.
Setup
CREATE TABLE members (id serial PRIMARY KEY, email text, deleted_at timestamp);
CREATE UNIQUE INDEX members_email_active ON members (email) WHERE deleted_at IS NULL;
INSERT INTO members (email) VALUES ('lee@example.com');CREATE TABLE
CREATE INDEX
INSERT 0 1INSERT INTO members (email) VALUES ('lee@example.com')
ON CONFLICT (email) DO NOTHING;ERROR: 42P10: there is no unique or exclusion constraint matching the ON CONFLICT specificationSELECT indexdef FROM pg_indexes WHERE tablename = 'members' AND indexname = 'members_email_active'; indexdef
----------------------------------------------------------------------------------------------------------
CREATE UNIQUE INDEX members_email_active ON lab_a.members USING btree (email) WHERE (deleted_at IS NULL)
(1 row)Add the index predicate: ON CONFLICT (email) WHERE deleted_at IS NULL DO NOTHING. The duplicate is skipped (INSERT 0 0) and only one row remains.
INSERT INTO members (email) VALUES ('lee@example.com')
ON CONFLICT (email) WHERE deleted_at IS NULL DO NOTHING;
SELECT id, email FROM members;INSERT 0 0
id | email
----+-----------------
1 | lee@example.com
(1 row)04The unique index is on an expression such as lower(email)
The index is on lower(email), not on email, so ON CONFLICT (email) does not match it.
Setup
CREATE TABLE accounts (id serial PRIMARY KEY, email text, name text);
CREATE UNIQUE INDEX accounts_email_lower ON accounts (lower(email));
INSERT INTO accounts (email, name) VALUES ('Park@Example.com', 'Park');CREATE TABLE
CREATE INDEX
INSERT 0 1INSERT INTO accounts (email, name) VALUES ('park@example.com', 'Park J')
ON CONFLICT (email) DO UPDATE SET name = EXCLUDED.name;ERROR: 42P10: there is no unique or exclusion constraint matching the ON CONFLICT specificationSELECT indexdef FROM pg_indexes WHERE indexname = 'accounts_email_lower'; indexdef
---------------------------------------------------------------------------------------
CREATE UNIQUE INDEX accounts_email_lower ON lab_a.accounts USING btree (lower(email))
(1 row)Write the same expression: ON CONFLICT (lower(email)). park@example.com now conflicts with the stored Park@Example.com and the name is updated to Park J; the stored email keeps its original case.
INSERT INTO accounts (email, name) VALUES ('park@example.com', 'Park J')
ON CONFLICT (lower(email)) DO UPDATE SET name = EXCLUDED.name;
SELECT id, email, name FROM accounts;INSERT 0 1
id | email | name
----+------------------+--------
1 | Park@Example.com | Park J
(1 row)FAQ
How do I fix there is no unique or exclusion constraint matching the ON CONFLICT specification?
Make the ON CONFLICT target match a unique constraint or index exactly: same columns, same expression, and the same WHERE condition for a partial index. If there is none, add a UNIQUE constraint.
Can I use ON CONFLICT with a column that is not unique?
No. The conflict target must be backed by a unique constraint, a unique index or an exclusion constraint.
How do I use ON CONFLICT with a composite primary key?
List all key columns in the same set, for example ON CONFLICT (warehouse_id, item_code).
How do I use ON CONFLICT with a partial unique index?
Repeat the index condition after the column list: ON CONFLICT (email) WHERE deleted_at IS NULL.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12