Sub topic 11
Inserting or Updating with ON CONFLICT
This chapter assumes PostgreSQL 12 or newer, basic SQL, and the books table from the earlier schema. That table has a unique isbn13 column. A unique constraint means that two rows cannot store the same non-null ISBN.
An upsert is an operation that inserts a row when it does not exist and updates the existing row when it does. PostgreSQL provides upserts with INSERT ... ON CONFLICT. This is useful when the same book may arrive repeatedly from an import, API, or synchronization job.
Insert when no conflict exists
A normal insert fails if another row already has the same ISBN:
INSERT INTO books (title, isbn13, published_year, page_count)
VALUES ('Example Title', '9780000000001', 2001, 320);If the ISBN is already present, PostgreSQL raises a unique-constraint error. You can instead ignore the new row:
INSERT INTO books (title, isbn13, published_year, page_count)
VALUES ('Example Title', '9780000000001', 3200, 320)
ON CONFLICT (isbn13) DO NOTHING;The conflict target, (isbn13), tells PostgreSQL which unique rule matters. If a row with that ISBN exists, DO NOTHING leaves the existing row unchanged. This is useful when the first stored version should win.
Update the conflicting row
Often the incoming data should replace selected values. Use DO UPDATE for that case:
INSERT INTO books (title, isbn13, published_year, page_count)
VALUES ('Example Title: Revised', '9780000000001', 2001, 340)
ON CONFLICT (isbn13) DO UPDATE
SET title = EXCLUDED.title,
published_year = EXCLUDED.published_year,
page_count = EXCLUDED.page_count
RETURNING book_id, title, isbn13, page_count;EXCLUDED is a special name for the row you tried to insert. The unqualified column on the left belongs to the existing row. Therefore, page_count = EXCLUDED.page_count copies the incoming page count into the stored row.
RETURNING reports the row produced by either path. It makes the statement useful to application code that needs the book's identity key.
A single upsert can be run repeatedly:
INSERT INTO books (title, isbn13, page_count)
VALUES ('Example Title', '9780000000001', 340)
ON CONFLICT (isbn13) DO UPDATE
SET title = EXCLUDED.title,
page_count = EXCLUDED.page_count;After the first run, later runs update the same book instead of creating duplicates. This property is called idempotence: repeating the same operation produces the same stored result.
Choosing what to overwrite
Do not overwrite trusted data automatically just because an import contains a value. You can preserve an existing value when the incoming value is null:
INSERT INTO books (title, isbn13, published_year, page_count)
VALUES ('Example Title', '9780000000001', NULL, 340)
ON CONFLICT (isbn13) DO UPDATE
SET title = EXCLUDED.title,
published_year = COALESCE(EXCLUDED.published_year, books.published_year),
page_count = COALESCE(EXCLUDED.page_count, books.page_count);COALESCE returns the first non-null value. Here, a missing incoming year or page count does not erase the stored value.
The database checks the unique constraint and performs the chosen action as one statement. This avoids the race condition that can occur when an application separately checks for a row and then inserts or updates it.
Exercise
What does EXCLUDED.page_count mean in an ON CONFLICT DO UPDATE clause?
Show answer
It means the page_count value from the row that the statement tried to insert, not the value currently stored in the conflicting row.