Upserting 50K records into big table (app. 350 columns) in postgresql
Some suggestions:

Use prepared statements. That will speed up execution, because the statement doesn't have to be planned every time.

Use savepoints. That way, you can have a single big transaction and still recover from errors:

Set a savepoint every 100 rows or so.

If a row fails, rollback to the savepoint and redo everything up to the failing row, then skip that and start a new savepoint.


If you have many errors, perhaps fewer than 100 rows would be preferable, so that you don't have to redo so much work. But setting a savepoint for every single row will also not be good for performance.

COPY the data to a temporary table and then
INSERT INTO finaltable
SELECT * FROM temptable
ON CONFLICT ...;