PostgreSQL 10:有了这个 upsert 函数,如何仅在列值不同时更新?

PostgreSQL 10: With this upsert function, how to update only if column value is different?

我希望更新 apr 仅当它不同时,现在,无论它是不同还是相同,它似乎都会更新:

   INSERT INTO live_mytable (id, loan_type, apr, term, oldestyear) 
   SELECT id, loan_type, apr, term, oldestyear
   FROM   imp_mytable 
   ON CONFLICT (id,loan_type,term,oldestyear) DO update
   set apr = excluded.apr;

如何将此查询更改为仅在值不同时更新?

您可以在更新中使用 WHERE 子句:

 INSERT INTO live_mytable (id, loan_type, apr, term, oldestyear) 
   SELECT id, loan_type, apr, term, oldestyear
   FROM   imp_mytable 
 ON CONFLICT (id,loan_type,term,oldestyear) 
 DO update
   set apr = excluded.apr
 WHERE 
      live_mytable.apr IS DISTINCT FROM 
      EXCLUDED.apr;