These changes were made with assistance from @Amrit-Hub's comment about the upsert logic. Existing logic assumes that if there is a matching primary key and match columns have changed then update, else insert. This assumes we won't pass an existing PK with unchanged values in match columns, which could lead to duplicate records being inserted each run of the upsert. By altering the logic to only consider the PK as the match condition we can push the logic for updating further down and avoid unintentionally re-inserting records with same PK/match-column combination that were unchanged from last upsert attempt.
These changes were made with assistance from @Amrit-Hub's comment about the upsert logic. Existing logic assumes that if there is a matching primary key and match columns have changed then update, else insert. This assumes we won't pass an existing PK with unchanged values in match columns, which could lead to duplicate records being inserted each run of the upsert. By altering the logic to only consider the PK as the match condition we can push the logic for updating further down and avoid unintentionally re-inserting records with same PK/match-column combination that were unchanged from last upsert attempt.