PostgreSQL SQL: updating summaries
This should be faster:

UPDATE master 
  SET "count" = t.cnt
FROM (
   SELECT master_id, COUNT(*) as cnt 
   FROM detail 
   group by master_id
) as t
where t.detail.master_id = master.id
  and t.cnt &lt;&gt; master."count";


This assumes that master.id is unique.