diff --git a/httpbl.module b/httpbl.module
index 9be6b62..c183bed 100644
--- a/httpbl.module
+++ b/httpbl.module
@@ -732,11 +732,9 @@ function _httpbl_whitelisted($ip) {
  * Write status value into cache table
  */
 function _httpbl_cache_set($ip, $status, $offset = 0) {
-  // DELETE and then INSERT leads to race conditions [#723358].
-  // INSERT IGNORE could work as well, but neither have PostgreSQL alternatives.
-  //  db_query("DELETE FROM {httpbl} WHERE hostname = '%s'", $ip);
- //  db_query("INSERT INTO {httpbl} (hostname, status, expire) VALUES ('%s', %d, %d)", $ip, $status, time() + $offset);
-  db_query("REPLACE {httpbl} (hostname, status, expire) VALUES ('%s', %d, %d)", $ip, $status, time() + $offset);
+  // http://stackoverflow.com/questions/1109061/insert-on-duplicate-update-postgresql/6527838#6527838
+  db_query("UPDATE {httpbl} SET hostname= '%s', status= %d, expire= %d WHERE hostname= '%s'", $ip, $status, time() + $offset);
+  db_query("INSERT INTO {httpbl} (hostname, status, expire) SELECT '%s', %d, %d FROM (SELECT 1) AS foo WHERE NOT EXISTS (SELECT 1 FROM {httpbl} WHERE hostname = '%s')", $ip, $status, time() + $offset, $ip);
 
   if ($status == HTTPBL_LIST_BLACK && variable_get('httpbl_cache', HTTPBL_CACHE_DBDRUPAL)) {
     db_query("INSERT INTO {access} (mask, type, status) VALUES ('%s', '%s', %d)", $ip, 'host', 0);
