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. #6810

Closed
Closed
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/Magento/CatalogInventory/Model/ResourceModel/Stock.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,24 @@ public function lockProductsStock($productIds, $websiteId)
$itemTable = $this->getTable('cataloginventory_stock_item');
$productTable = $this->getTable('catalog_product_entity');
$select = $this->getConnection()->select()->from(['si' => $itemTable])
->join(['p' => $productTable], 'p.entity_id=si.product_id', ['type_id'])
->where('website_id=?', $websiteId)
->where('product_id IN(?)', $productIds)
->forUpdate(true);
return $this->getConnection()->fetchAll($select);
$rows = $this->getConnection()->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->getConnection()->select()
->from($productTable, ['entity_id', 'type_id'])
->where('entity_id IN(?)', $productIds);
$typeIds = $this->getConnection()->fetchPairs($select);
foreach ($rows as &$row) {
$row['type_id'] = $typeIds[$row['product_id']];
}
return $rows;
}

/**
Expand Down