Queries and performance

Upsert with ON CONFLICT

Insert a row or handle a specific uniqueness conflict in one deliberate statement.

8 minute lesson

~~~

Use ON CONFLICT against a real unique constraint:

INSERT INTO settings (user_id, theme)
VALUES (1, 'dark')
ON CONFLICT (user_id)
DO UPDATE SET theme = EXCLUDED.theme;

Name the conflict target. Do not silently swallow every possible error.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →