Postgres ON CONFLICT: no unique constraint matches
42P10 rejects an upsert whose ON CONFLICT target has no matching unique index. Create a unique constraint on exactly those columns, predicate included.

Introduction & Quick Summary
An upsert fails before writing anything:
ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification
SQL state: 42P10ON CONFLICT (email) is not a hint. It tells Postgres which index to use as the arbiter, and the planner requires a unique or exclusion constraint that covers exactly those columns. If the table has no such index, the statement cannot be planned and nothing runs.
Why the target is rejected:
- No unique index or unique constraint exists on the listed columns.
- The index exists but is partial, and the
ON CONFLICTclause omits the predicate. - The index is on an expression (
lower(email)), and the conflict target lists the expression incorrectly. - The index is on a partitioned table without the partition key, so no global unique index exists.
- The statement uses
ON CONFLICT ON CONSTRAINT nameand the named constraint is not unique or is deferrable.
Quick answer: list the indexes on the table, then make the conflict target match one of them exactly, including any WHERE predicate and expression.
Core Mechanics, Signatures & Reproducible Setup
INSERT ... ON CONFLICT has two target forms: a column list with an optional predicate, or a constraint name. Both are resolved at plan time against the table's unique indexes.
CREATE TABLE subscribers (
id bigserial PRIMARY KEY,
email text NOT NULL,
status text NOT NULL DEFAULT 'active',
deleted_at timestamptz
);
-- works: the primary key and this unique index exist
ALTER TABLE subscribers ADD CONSTRAINT subscribers_email_key UNIQUE (email);
INSERT INTO subscribers (email, status)
VALUES ('a@example.com', 'active')
ON CONFLICT (email) DO UPDATE SET status = EXCLUDED.status;Reproduce the failure with a table that has no unique index on the target:
CREATE TABLE events (
id bigserial PRIMARY KEY,
user_id bigint NOT NULL,
kind text NOT NULL
);
INSERT INTO events (user_id, kind) VALUES (1, 'click')
ON CONFLICT (user_id, kind) DO NOTHING; -- 42P10The target forms differ in how strictly they must match:
| Form | Requires | Common failure |
|---|---|---|
ON CONFLICT (col) | unique index on that column | no index, or a partial one |
ON CONFLICT (a, b) | unique index on exactly those columns | index exists in another order or with extra columns |
ON CONFLICT (col) WHERE ... | partial unique index with same predicate | predicate missing or different |
ON CONFLICT ON CONSTRAINT c | named unique or exclusion constraint | constraint is deferrable or is an index only |
ON CONFLICT (lower(col)) | unique index on that expression | expression spelled differently |
Key Comparison Table
| Arbiter | Enforces uniqueness | Works with ON CONFLICT | Notes |
|---|---|---|---|
| Unique constraint | yes | yes | constraint name usable |
| Unique index | yes | yes | must not be partial unless predicate matches |
| Partial unique index | only for matching rows | yes, with matching WHERE | ideal for soft deletes |
| Expression unique index | yes | yes, with the same expression | lower(email) |
| Non-unique index | no | no | 42P10 |
| Exclusion constraint | yes | yes | needs WITH = or similar |
| Deferrable unique constraint | yes | no | cannot be an arbiter |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
1. List the indexes before editing the query
\d shows indexes and constraints, and the predicate is the part that usually differs.
\d+ subscribers
SELECT indexname, indexdef
FROM pg_indexes
WHERE schemaname = 'public' AND tablename = 'subscribers';Read indexdef literally: column order, expressions, and any trailing WHERE must all appear in the conflict target.
2. Partial unique indexes and soft deletes
The most common real-world case: uniqueness only among active rows.
CREATE UNIQUE INDEX subscribers_active_email_idx
ON subscribers (email)
WHERE deleted_at IS NULL;
-- the predicate must be repeated in the ON CONFLICT clause
INSERT INTO subscribers (email, status)
VALUES ('a@example.com', 'active')
ON CONFLICT (email) WHERE deleted_at IS NULL
DO UPDATE SET status = EXCLUDED.status;Omitting the WHERE produces 42P10 even though the index is visible in \d, because the planner cannot use a partial index as an arbiter for unrestricted rows.
3. Expression indexes
An index on lower(email) requires the same expression in the target. Column names alone do not match.
CREATE UNIQUE INDEX subscribers_email_lower_idx ON subscribers (lower(email));
INSERT INTO subscribers (email) VALUES ('A@Example.com')
ON CONFLICT (lower(email)) DO NOTHING;If the target expression differs even slightly (lower(trim(email))), the planner rejects it. Align the index and the statement, and prefer one canonical expression used by both.
4. Constraint names, deferrable constraints, and ON CONSTRAINT
ON CONFLICT ON CONSTRAINT subscribers_email_key works only when that name refers to a unique or exclusion constraint, not a plain index. Deferrable unique constraints cannot act as arbiter.
ALTER TABLE subscribers
ADD CONSTRAINT subscribers_email_key UNIQUE (email);If the constraint was created as DEFERRABLE INITIALLY DEFERRED for bulk loading, either drop the deferrability for that constraint or use the column-list form where a non-deferrable index exists.
5. Nulls, nullable columns, and NULLS NOT DISTINCT
A unique index allows multiple rows with NULL in the indexed column, so an upsert on a nullable key inserts duplicates instead of conflicting. Postgres 15 added NULLS NOT DISTINCT to change that.
CREATE UNIQUE INDEX sessions_user_device_idx
ON sessions (user_id, device_id) NULLS NOT DISTINCT;Check the index definition before assuming a conflict will fire. Silent duplicate inserts are worse than 42P10, because no error tells you the upsert did not upsert.
6. Partitioned tables and batch inserts
A unique index on a partitioned table must include the partition key, and there is no cross-partition enforcement. An upsert whose target omits the partition key fails with 42P10 on the parent table.
CREATE TABLE events (
id bigint NOT NULL,
day date NOT NULL,
kind text NOT NULL
) PARTITION BY RANGE (day);
CREATE UNIQUE INDEX events_id_day_key ON events (id, day);
-- ON CONFLICT (id) DO NOTHING -> 42P10
-- ON CONFLICT (id, day) DO NOTHING -> works per partitionFor batch upserts from INSERT ... SELECT, the same arbiter rules apply. When the natural key is not unique, MERGE (Postgres 15+) or a CTE with UPDATE then INSERT expresses the intent without inventing a constraint.
Common Compilation/Runtime Mistakes & Version Gotchas
- Adding the index inside the same transaction as the upsert. The index must be committed and valid; a concurrently built index that failed leaves an invalid index that does not arbitrate.
- Index created with
CONCURRENTLYand left invalid.pg_index.indisvalid = falsemeans the constraint is not enforced. Rebuild it before relying on upserts. - Assuming
DO NOTHINGskips the check. It requires the same arbiter, so it fails with 42P10 exactly likeDO UPDATE. - ORM-generated upserts. Prisma, Drizzle, and Supabase clients generate
ON CONFLICTtargets from schema metadata; a schema drift between the migration and the database produces this error at runtime. - Column order in the target.
(a, b)matches an index on(a, b), and Postgres can also use(b, a)for a two-column index in many cases, but partial and expression indexes require exact spelling.
| Symptom | Cause | Fix |
|---|---|---|
| index exists, still 42P10 | partial index without predicate | add WHERE to the target |
| works in staging, fails in prod | missing migration | apply the unique index |
| duplicate rows instead of updates | nullable key | NULLS NOT DISTINCT |
| fails only on the partitioned table | partition key missing | include it in the target |
Write and test the statement against a real schema with the SQL formatter and runner, and check the exact index definition with the SQL schema tool instead of trusting a migration name.
The arbiter rules are specified in INSERT, and the behaviour of unique indexes, including partial and expression forms, is covered in Unique indexes.
Official references
primary documentationThe SQL documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit SQL work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related SQL guides
All SQL guidesSQLite: database is locked (SQLITE_BUSY) under concurrent writes
SQLite allows one writer. Set a busy timeout, switch to WAL, start write transactions as IMMEDIATE, and reduce to a single writer connection.
SQL Window Functions: ROW_NUMBER, RANK, DENSE_RANK, and LEAD/LAG Explained
How the four ranking and offset functions differ on ties, why the default RANGE frame makes running totals repeat, and how LAG builds gap-and-island.
Postgres: column does not exist (SQLSTATE 42703)
42703 means the identifier you wrote is not the one that exists. Check case folding, alias scope, search_path and migration drift.
PostgreSQL: canceling statement due to statement timeout
The statement was canceled by statement_timeout. Find whether the role, the pooler or the client set it, then scope a longer budget to that job.
More in SQL
All SQL guidesThe guides filed next to this one in SQL, in library order.