Sub topic 3
Choosing a Conflict Target
Assume you know basic SELECT, INSERT, and UPDATE statements, primary keys, unique constraints, and how to run PostgreSQL statements from psql or another client.
A conflict target tells PostgreSQL which uniqueness rule should detect a conflict. It is the part between ON CONFLICT and DO NOTHING or DO UPDATE. PostgreSQL checks that rule when the proposed row is inserted.
Column-list targets
Use a column list when the conflict is defined by a primary key or a unique index. The column list identifies the columns whose values must be unique.
CREATE TABLE product_stock (
sku text PRIMARY KEY,
quantity integer NOT NULL,
updated_at timestamptz NOT NULL DEFAULT now()
);
INSERT INTO product_stock (sku, quantity)
VALUES ('A-100', 5)
ON CONFLICT (sku) DO UPDATE
SET quantity = EXCLUDED.quantity;Here, (sku) is the conflict target. PostgreSQL can infer the primary key index from that column list. The same form works with a named unique constraint:
CREATE TABLE customer_account (
customer_id bigint PRIMARY KEY,
email text NOT NULL UNIQUE
);
INSERT INTO customer_account (customer_id, email)
VALUES (42, '[email protected]')
ON CONFLICT (email) DO NOTHING;The target can contain multiple columns. PostgreSQL treats the combination as unique, so a conflict occurs only when all targeted column values match.
CREATE TABLE membership (
team_id bigint NOT NULL,
user_id bigint NOT NULL,
role text NOT NULL,
UNIQUE (team_id, user_id)
);
INSERT INTO membership (team_id, user_id, role)
VALUES (10, 7, 'editor')
ON CONFLICT (team_id, user_id) DO UPDATE
SET role = EXCLUDED.role;| Conflict target | Typical use |
|---|---|
| `(team_id, user_id)` | A composite unique key |
| `(email)` | A unique email index or constraint |
| `ON CONSTRAINT membership_team_id_user_id_key` | A named unique constraint |
Constraint-name targets
You can name a primary key or unique constraint directly:
INSERT INTO membership (team_id, user_id, role)
VALUES (10, 7, 'viewer')
ON CONFLICT ON CONSTRAINT membership_team_id_user_id_key
DO UPDATE
SET role = EXCLUDED.role;This is useful when the table has several uniqueness rules and the intended rule is clearer by name. It also avoids repeating a long column list.
ON CONSTRAINT accepts a constraint name, such as a primary key or unique constraint. It does not accept the name of an ordinary unique index. If uniqueness was created with CREATE UNIQUE INDEX, use its indexed columns instead.
Partial unique indexes
A partial unique index enforces uniqueness only for rows that satisfy a predicate. A predicate is a Boolean condition, such as deleted_at IS NULL.
CREATE TABLE app_user (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email text NOT NULL,
name text NOT NULL,
deleted_at timestamptz
);
CREATE UNIQUE INDEX app_user_email_active_idx
ON app_user (email)
WHERE deleted_at IS NULL;
INSERT INTO app_user (email, name)
VALUES ('[email protected]', 'Ada')
ON CONFLICT (email) WHERE deleted_at IS NULL
DO UPDATE
SET name = EXCLUDED.name;The WHERE clause in the conflict target repeats the index predicate. Without it, PostgreSQL may not be able to infer the partial index, and the statement can fail with an error saying that no matching unique or exclusion constraint exists.
This design permits one active account for an email while allowing older, soft-deleted rows with the same email. The upsert targets only active rows.
How to choose
Use this rule of thumb:
- Use a column list for a simple primary key or unique index.
- Use
ON CONSTRAINTwhen a named primary key or unique constraint expresses the rule clearly. - Repeat the predicate when targeting a partial unique index.
- Confirm that the target matches an existing unique or exclusion rule. A nonunique index cannot be used for conflict detection.
When several unique rules exist, choose the rule that represents the identity of the incoming row. For example, an order line might be identified by (order_id, product_id), while a user might be identified by an active email address.
Exercise
A table has a partial unique index on (email) WHERE deleted_at IS NULL. Which conflict target should an upsert use?
Show answer
ON CONFLICT (email) WHERE deleted_at IS NULL, because the target must identify the partial unique index and repeat its predicate.