I managed to replicate the issue with the following data & tables attached download link.
I am trying to insert from bugtest.temp_on_conflict_test into bugtest.history, both tables have a primary key on history_id so there cannot be duplicates
The insert query also uses distinct on (history_id)
INSERT INTO bugtest.history (account_id,appo_id,account_name,resource_owner_name,resource_owner_id,resource_owner_user_id,resource_id,resource_type,preview,resource_date,history_id)
select distinct on (history_id) * from bugtest.temp_on_conflict_test limit 2
ON CONFLICT (history_id) DO UPDATE SET
account_id = excluded.account_id,
resource_owner_name = excluded.resource_owner_name,
resource_owner_user_id = excluded.resource_owner_user_id,
resource_owner_id = excluded.resource_owner_id,
preview = excluded.preview,
resource_date = excluded.resource_date,
account_name = excluded.account_name
RETURNING *