PostgreSQL upsert:如果字段没有改变则什么也不做

PostgreSQL upsert: do nothing if fields don't change

是否可以表达一个 upsert 查询,如果插入的数据与数据库中已有的数据相比没有任何变化,则什么也不会发生?

目前我有:

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

您可以在 update 部分添加一个 where 子句:

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)