Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~5 min readSQL
#PostgreSQL#SQL#Upserts#Constraints
A Postgres upsert crate refused at a cyan conflict gate, an amber unique constraint card missing beside it

Introduction & Quick Summary

An upsert fails before writing anything:

code
ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification SQL state: 42P10

ON 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 CONFLICT clause 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 name and 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; -- 42P10

The target forms differ in how strictly they must match:

FormRequiresCommon failure
ON CONFLICT (col)unique index on that columnno index, or a partial one
ON CONFLICT (a, b)unique index on exactly those columnsindex exists in another order or with extra columns
ON CONFLICT (col) WHERE ...partial unique index with same predicatepredicate missing or different
ON CONFLICT ON CONSTRAINT cnamed unique or exclusion constraintconstraint is deferrable or is an index only
ON CONFLICT (lower(col))unique index on that expressionexpression spelled differently

Key Comparison Table

ArbiterEnforces uniquenessWorks with ON CONFLICTNotes
Unique constraintyesyesconstraint name usable
Unique indexyesyesmust not be partial unless predicate matches
Partial unique indexonly for matching rowsyes, with matching WHEREideal for soft deletes
Expression unique indexyesyes, with the same expressionlower(email)
Non-unique indexnono42P10
Exclusion constraintyesyesneeds WITH = or similar
Deferrable unique constraintyesnocannot 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.

sql
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 partition

For 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 CONCURRENTLY and left invalid. pg_index.indisvalid = false means the constraint is not enforced. Rebuild it before relying on upserts.
  • Assuming DO NOTHING skips the check. It requires the same arbiter, so it fails with 42P10 exactly like DO UPDATE.
  • ORM-generated upserts. Prisma, Drizzle, and Supabase clients generate ON CONFLICT targets 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.
SymptomCauseFix
index exists, still 42P10partial index without predicateadd WHERE to the target
works in staging, fails in prodmissing migrationapply the unique index
duplicate rows instead of updatesnullable keyNULLS NOT DISTINCT
fails only on the partitioned tablepartition key missinginclude 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 documentation

The SQL documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit SQL work — they run entirely in this browser tab, so nothing you paste leaves your device.

All SQL guides

More in SQL

All SQL guides

The guides filed next to this one in SQL, in library order.