PostgreSQL upsert: do nothing if fields don't change

3.7k views Asked by At

Is it possible to express an upsert query, where nothing happens if the inserted data doesn't have any changes compared to what is already in the database?

Currently I have:

insert into feeds (link, title, category)
values (...)
on conflict (link)
do update set (title, category, _updated)
= (..., now()::timestamp)
1

There are 1 answers

3
AudioBubble On BEST ANSWER

You can add a where clause to the update part:

insert into feeds (link, title, category)
values (...)
on conflict (link)
do update 
   set (title, category, _updated) = (..., now()::timestamp)
where (feeds.title, feeds.category) is distinct from (excluded.title, excluded.category)