django-postgres-extra icon indicating copy to clipboard operation
django-postgres-extra copied to clipboard

Exclude conflict_target fields from update fields

Open mgu opened this issue 4 years ago • 2 comments

_get_upsert_fields should not add fields in self.conflict_target in the update_fields

If we are "on conflict" then we know that the conflict_target columns already contains the value we wanted to insert, there is no need to update them with the same value

mgu avatar May 08 '20 19:05 mgu

FYI, from a database point of view, this is not a silly optimization : conflict_target will always contain indexed fields, and doing an update on these fields will thus prevent an HOT update from happening. Not having this optimization will force the use of the built-in suppress_redundant_updates_trigger https://www.postgresql.org/docs/10/functions-trigger.html on the DB side.

pinaraf avatar May 09 '20 10:05 pinaraf

At the face of this sounds very simple, but if it was I guess someone would have solved it already. Is there some hidden complexity that means update_fields = [field for field in update_fields if field not in conflict_targets] (pseudo-ish-code, the types don't match up directly) doesn't work?

henribru avatar Oct 07 '21 14:10 henribru