title: Duplicate entry on INSERT after DELETE from table in transaction
body: <p>I'm looking for any ideas to explain (and prevent) the following:</p>
  
  <p>1) We have a Magento reindex process (price or stock) that is failing from time to time with: </p>
  
  <blockquote>
    <p>exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[23000]:
    Integrity constraint violation: 1062 Duplicate entry '512723-1-1' for
    key 'PRIMARY'' in /[omitted]/lib/Zend/Db/Statement/Pdo.php:242</p>
  </blockquote>
  
  <p>Full trace: <a href="https://gist.github.com/werdan/5255362" rel="nofollow">https://gist.github.com/werdan/5255362</a></p>
  
  <p>2) Magento reindex is done in transaction, which can be  sketched as following:</p>
  
  <pre><code>START TRANSACTION;
  DELETE FROM `cataloginventory_stock_status`;
  INSERT INTO cataloginventory_stock_status SELECT * FROM cataloginventory_stock_status_idx;
  COMMIT;
  </code></pre>
  
  <p>3) It should be impossible to break consistency of the table between DELETE and INSERT commands, nevertheless it happens.</p>
  
  <p>We have checked that tables cataloginventory_stock_status and cataloginventory_stock_status_idx have identical keys and structure.</p>
  
  <p>What else can be suggested as an explanation and solution?</p>