Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix query which creates significant deadlock potential. #103

Merged
merged 1 commit into from
Oct 6, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions app/code/core/Mage/CatalogInventory/Model/Resource/Stock.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,24 @@ public function getProductsStock($stock, $productIds, $lockRows = false)
$productTable = $this->getTable('catalog/product');
$select = $this->_getWriteAdapter()->select()
->from(array('si' => $itemTable))
->join(array('p' => $productTable), 'p.entity_id=si.product_id', array('type_id'))
->where('stock_id=?', $stock->getId())
->where('product_id IN(?)', $productIds)
->forUpdate($lockRows);
return $this->_getWriteAdapter()->fetchAll($select);
$rows = $this->_getWriteAdapter()->fetchAll($select);

// Add type_id to result using separate select without FOR UPDATE instead
// of a join which causes only an S lock on catalog_product_entity rather
// than an X lock. An X lock on a table causes an S lock on all foreign keys
// so using a separate query here significantly reduces the number of
// unnecessarily locked rows in other tables, thereby avoiding deadlocks.
$select = $this->_getWriteAdapter()->select()
->from($productTable, ['entity_id', 'type_id'])
->where('entity_id IN(?)', $productIds);
$typeIds = $this->_getWriteAdapter()->fetchPairs($select);
foreach ($rows as $row) {
$row['type_id'] = $typeIds[$row['product_id']];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this does not do what was intended. Product type is not sent on $rows array.

Eg. this could work, or alternatively loop by reference.

foreach ($rows as $k => $row) { $rows[$k]['type_id'] = $typeIds[$row['product_id']]; }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right but it was fixed a few days later on 7804594.

}
return $rows;
}

/**
Expand Down