Maybe, instead,  you can do a upsert for all the records?  Its a MySQL-ism, but it'd work.  PostgreSQL has an equivelent construction but it probably isn't worth customizing for them.  Just do a query, check what has to change, then send the updates, I guess.