Learning navigation

Code with Animation

RETURNING & Upsert

Get the written rows back with RETURNING, and handle duplicate keys with ON CONFLICT instead of an error.

SQL6 min readConcept 31 of 44

Two small additions that save a lot of code

RETURNING goes at the end of INSERT, UPDATE or DELETE and makes the statement answer with rows, like a SELECT. For INSERT you get the new row. For UPDATE you get the new content of each changed row. For DELETE you get the row that was removed.

ON CONFLICT goes after VALUES and says what to do when a row would break a unique key, instead of raising an error. DO NOTHING skips the row. DO UPDATE SET ... updates the row that is already there.

No second query, no race

After inserting a row you almost always need its id. Asking with a second query is slower and can pick the wrong row if someone else inserts at the same moment. RETURNING id gives the right answer in the same step.

"Insert it, or update it if it already exists" is a very common need. Doing it in your own code takes two statements and can fail when two people run it at once. One ON CONFLICT statement does it in one step.

Insert, return and upsert

INSERT INTO members (name, email)
VALUES ('Dara', 'dara@mail.test')
RETURNING id, name;

The statement returns the row Dara got: id 4. Now the upsert:

INSERT INTO members (id, name, email)
VALUES (2, 'Ben2', 'b2@mail.test')
ON CONFLICT (id) DO UPDATE
  SET name = EXCLUDED.name, email = EXCLUDED.email
RETURNING id, name, email;

Id 2 already exists, so instead of an error, that row is updated. EXCLUDED is the name for the row you tried to insert. With DO NOTHING the row is skipped, the tag says INSERT 0 0, and RETURNING gives back nothing, because no row was written.

Return rows, then upsert

Run INSERT ... RETURNING and watch the RETURNING table. Then run both ON CONFLICT versions on id 2, and the last one that touches the same key twice.

INSERT INTO members (name, email)
VALUES ('Dara', 'dara@mail.test')
RETURNING id, name;
members (3 rows, next id: 4)
idintegernametextemailtext
1Ashaasha@mail.test
2Benben@mail.test
3ChenNULL

Run INSERT ... RETURNING first, then try the two ON CONFLICT versions on id 2. Press Reset table to start again.

Check yourself

Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.

  1. 1What does `RETURNING *` give back at the end of a `DELETE`?
  2. 2`INSERT ... ON CONFLICT (id) DO NOTHING RETURNING id` runs with an id that already exists. What does it return?
  3. 3In `DO UPDATE SET name = EXCLUDED.name`, what is EXCLUDED?

Remember this

  • RETURNING gives back the rows an INSERT, UPDATE or DELETE wrote: new row, new content, deleted row.
  • ON CONFLICT DO NOTHING skips a duplicate. ON CONFLICT (key) DO UPDATE updates the existing row using EXCLUDED.
  • DO UPDATE needs a conflict target, and one statement may not hit the same row twice.
  • Use RETURNING id instead of a second query, and ON CONFLICT instead of check-then-insert.

Done with this concept?

Mark it complete to track your progress. No login needed.