Sub topic 8
Returning with ON CONFLICT: What Comes Back and What Does Not
This chapter assumes you know basic INSERT, UPDATE, and DELETE, primary keys, unique constraints, and how to run SQL in psql or another PostgreSQL client.
INSERT ... ON CONFLICT is PostgreSQL's upsert syntax. It tries to insert a row. If a unique or exclusion constraint reports a conflict, PostgreSQL can update the existing row or do nothing. Adding RETURNING lets the statement report the row produced by either path.
The two conflict actions
Consider this table:
CREATE TABLE tasks (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
title text NOT NULL UNIQUE,
status text NOT NULL DEFAULT 'open',
updated_at timestamptz NOT NULL DEFAULT now()
);An upsert that updates an existing task can return the final row:
INSERT INTO tasks (title, status)
VALUES ('Review PR', 'done')
ON CONFLICT (title) DO UPDATE
SET status = EXCLUDED.status,
updated_at = now()
RETURNING id, title, status, updated_at;EXCLUDED means the row that PostgreSQL attempted to insert. The unqualified target columns refer to the row already stored in tasks. The RETURNING result contains the row after the insert or update action completes.
This statement returns one row in both cases:
- If
Review PRdoes not exist, it returns the newly inserted row. - If
Review PRalready exists, it returns the updated row.
The identity value and timestamp are available immediately. The application does not need a second SELECT.
`DO NOTHING` can return no row
DO NOTHING handles a conflict without changing the existing row:
INSERT INTO tasks (title)
VALUES ('Review PR')
ON CONFLICT (title) DO NOTHING
RETURNING id, title, status;There are two possible results:
- A new task is inserted, so
RETURNINGproduces one row. - A task with that title already exists, so PostgreSQL does nothing and produces zero rows.
DO NOTHING does not return the conflicting row. This is a common mistake. RETURNING reports rows affected by the statement; it is not a general-purpose lookup of rows that prevented the insert.
If the application needs the existing row, it can handle an empty result with a separate SELECT, or use DO UPDATE when an update is acceptable:
INSERT INTO tasks (title)
VALUES ('Review PR')
ON CONFLICT (title) DO UPDATE
SET updated_at = now()
RETURNING id, title, status, updated_at;This changes updated_at, so it returns the existing row through the update path.
Conditional updates and aliases
A conflict action can contain a WHERE condition:
INSERT INTO tasks (title, status)
VALUES ('Review PR', 'open')
ON CONFLICT (title) DO UPDATE
SET status = EXCLUDED.status,
updated_at = now()
WHERE tasks.status <> 'done'
RETURNING id, title, status;If the existing task is already done, the condition is false. PostgreSQL locks the conflicting row but does not update or return it. The client receives zero rows, just as it would when no row matches an ordinary UPDATE.
Use a table alias when needed, but remember that an alias hides the original table name inside the statement:
INSERT INTO tasks AS t (title, status)
VALUES ('Review PR', 'done')
ON CONFLICT (title) DO UPDATE
SET status = EXCLUDED.status
RETURNING t.id, t.title, t.status;The EXCLUDED name is available in the conflict action, not as a separate returned result set. If the client must know whether the insert or update path ran, return identifying values and apply an explicit convention, or issue a separate query when that distinction matters.
Reading the result in client code
Treat RETURNING as a result set. A successful statement may return one row, many rows, or zero rows depending on the input and conflict action. For a single-row upsert, application code should check whether a row was returned before reading its fields.
The statement is atomic: the insert, conflict handling, and returned values belong to the same database operation.
Exercise
What does RETURNING produce when an ON CONFLICT DO NOTHING clause encounters an existing conflicting row?
Show answer
It produces zero rows because the existing row was neither inserted nor changed.