A single statement that inserts or, on unique contradiction, updates instead.

INSERT ... ON CONFLICT turns a race into one statement

INSERT … ON CONFLICT lets PostgreSQL resolve an insert collision inside the statement that performs the write. It replaces a fragile “check whether the key exists, then insert” sequence with an explicit conflict policy backed by an eligible index or constraint. It does not turn every insertion error into a successful update or define the application’s merge policy for you. ...

March 25, 2027 · 5 min · XB Field Notes