arkhipov / temporal_tables

Temporal Tables PostgreSQL Extension
BSD 2-Clause "Simplified" License
937 stars 47 forks source link

Generate history row on UPDATE only if OLD is DISTINCT from NEW #32

Open tmcdos opened 7 years ago

tmcdos commented 7 years ago

First, thank you for this excellent extension! I have noticed that the versioning trigger creates new history rows even if they are the same as the previous one. I mean, if I execute UPDATE myTable SET column = 2 WHERE id = 1 5 times - I will have 5 rows in the history differing only in the sys_period column. I tried to wrap the versioning() function with an SQL trigger but I faced the errors function versioning(text, text, text) does not exist and function "versioning" was not called by trigger manager. I am not familiar enough with PostgreSQL extensions API and can not modify the source code myself. Maybe someone can help with this ? UPDATE: I created 2 triggers instead of one - first trigger handles INSERT and DELETE, second trigger handles UPDATE WHEN old.* IS DISTINCT FROM new.* but this does not solve the problem completely - for example if the history table has fewer columns than the original table and you update a column which is not present in the history table then you get extra rows in the history.

mlt commented 7 years ago

I think this is by design. If you don't want new row and thus a history entry, use where restriction to check.

tmcdos commented 7 years ago

I see. It seems cheaper to have extra history entries than doing checks on each update.