Concept Flow - Conditional INSERT with ON CONFLICT
Start INSERT
Check if conflict on unique key
Run ON CONFLICT action
End
The database tries to insert a row. If a conflict on a unique key happens, it runs the ON CONFLICT action instead of erroring.
INSERT INTO users(id, name, age) VALUES (1, 'Alice', 30) ON CONFLICT (id) DO UPDATE SET age = EXCLUDED.age WHERE users.age < EXCLUDED.age;
| Step | Action | Conflict Check | Condition (WHERE) | Result | Table State |
|---|---|---|---|---|---|
| 1 | Try insert (1, 'Alice', 30) | No existing id=1 | N/A | Insert succeeds | [{id:1, name:'Alice', age:30}] |
| 2 | Try insert (1, 'Alice', 35) | Conflict on id=1 | users.age(30) < EXCLUDED.age(35) is True | Update age to 35 | [{id:1, name:'Alice', age:35}] |
| 3 | Try insert (1, 'Alice', 25) | Conflict on id=1 | users.age(35) < EXCLUDED.age(25) is False | Do nothing | [{id:1, name:'Alice', age:35}] |
| 4 | Try insert (2, 'Bob', 40) | No existing id=2 | N/A | Insert succeeds | [{id:1, name:'Alice', age:35}, {id:2, name:'Bob', age:40}] |
| Table State | Start | After Step 1 | After Step 2 | After Step 3 | After Step 4 |
|---|---|---|---|---|---|
| users | [] | [{id:1, name:'Alice', age:30}] | [{id:1, name:'Alice', age:35}] | [{id:1, name:'Alice', age:35}] | [{id:1, name:'Alice', age:35}, {id:2, name:'Bob', age:40}] |
INSERT INTO table (columns) VALUES (values) ON CONFLICT (conflict_target) DO UPDATE SET column = value WHERE condition; - Tries to insert row. - If conflict on unique key, runs DO UPDATE. - WHERE filters if update happens. - Else inserts new row normally.
ON CONFLICT DO NOTHING clause do in a PostgreSQL INSERT statement?ON CONFLICT DO NOTHING clause tells PostgreSQL to ignore the insert if a duplicate key conflict happens.DO UPDATE which modifies existing rows, DO NOTHING simply skips the insert without error.id in PostgreSQL?ON CONFLICT (column) DO UPDATE SET to update on conflict.EXCLUDED.val to refer to the new value and specifies the conflict column.users(id SERIAL PRIMARY KEY, username TEXT UNIQUE, score INT), what will be the result of this query?INSERT INTO users (username, score) VALUES ('alice', 10) ON CONFLICT (username) DO UPDATE SET score = users.score + EXCLUDED.score RETURNING *;products(id INT UNIQUE, name TEXT UNIQUE), identify the error in this PostgreSQL query:INSERT INTO products (id, name) VALUES (1, 'Pen') ON CONFLICT DO UPDATE SET name = EXCLUDED.name;
DO UPDATE, PostgreSQL requires specifying the conflict target column or constraint.(id) or constraint name after ON CONFLICT, causing syntax error.email and last_login into a table users(email TEXT UNIQUE, last_login TIMESTAMP). If the email already exists, update last_login only if the new timestamp is more recent. Which query correctly implements this conditional update?WHERE clause in DO UPDATE to conditionally apply the update.last_login only if the new timestamp is greater than the existing one, using WHERE EXCLUDED.last_login > users.last_login.Pass the test and this pattern moves unknown