Skip to content
Advertisement

In PostgreSQL, I’m performing an UPSERT on 1000 rows, I’m using ON CONFLICT … DO NOTHING, if 1 row fails, do the other 999 get inserted?

I’m trying to understand how the ON CONFLICT clause works. What I want, is if an insert fails for any reason, I want it to do nothing with the row, but continue processing the other items in the bulk insert.

Here’s my scenario. I’m building a bulk insert query, with 1000 rows to be inserted. Currently, my query has:

What I want to know is can I do this:

This would effectively take any conflict inserting a row (in my case a null constraint on a column) and just not insert it. I need to ensure that if there is 1 conflict for one row, that the other 999 rows will still be inserted.

UPDATE: here’s a demo showing the issue I’m encountering: https://dbfiddle.uk/?rdbms=postgres_9.5&fiddle=8947dbcd38e9136531b907d3c745c0f5

UPDATE 2: It looks like when you pass null values, it fails the entire bulk insert process.

FINAL UPDATE: Accepted the first answer because of that cool demo link – which helped me realize (and in the comments) that null isn’t captured, so I just need to change my table structure and rely on the primary key… so when duplicate nulls are added, the pkey constraint gets triggered. Thanks everyone!

Advertisement

Answer

All rows for which there is no conflict will be inserted, and the conflicting row will be ignored.

Consider this demo:

id | info
-: | :---
 1 | old 
 2 | new 
 3 | new 
 4 | new 
 5 | new 
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement