rewrite safe update query