How To Fix Deadlock Error When Reindexing All In Magento 2?

how to fix Deadlock error occurred while reindexing all in magento 2 tutorial_

What Triggers This Error

After the developer tried to re-indexing, the exception.log file was generated including the following deadlock error in this file. Meanwhile, the server was stuck and needed restarting. Then, the site can run as usual.

“PDOException: SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction”

Magento Version: 2
Mode: Production
Indexing Mode: Update on save

Total Products: 18k
Total Customers: 54k
Total Orders: 18k

So, how to fix this error, and what is the best way to re-index manually?

Solution For Magento Deadlock Error When Reindexing All

There are 3 simple steps to handle this Magento 2 deadlock error:

  • Step 1: Open your database on local then run SQL:
DROP TABLE `catalog_category_product_index`;
DROP TABLE `catalog_category_product_index_tmp`;


CREATE TABLE `catalog_category_product_index` (
  `category_id` int(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'Category ID',
  `product_id` int(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'Product ID',
  `position` int(11) DEFAULT NULL COMMENT 'Position',
  `is_parent` smallint(5) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'Is Parent',
  `store_id` smallint(5) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'Store ID',
  `visibility` smallint(5) UNSIGNED NOT NULL COMMENT 'Visibility'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Catalog Category Product Index';
ALTER TABLE `catalog_category_product_index`
  ADD PRIMARY KEY (`category_id`,`product_id`,`store_id`),
  ADD KEY `CAT_CTGR_PRD_IDX_PRD_ID_STORE_ID_CTGR_ID_VISIBILITY` (`product_id`,`store_id`,`category_id`,`visibility`),
  ADD KEY `CAT_CTGR_PRD_IDX_STORE_ID_CTGR_ID_VISIBILITY_IS_PARENT_POSITION` (`store_id`,`category_id`,`visibility`,`is_parent`,`position`);
CREATE TABLE `catalog_category_product_index_tmp` (
  `category_id` int(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'Category ID',
  `product_id` int(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'Product ID',
  `position` int(11) NOT NULL DEFAULT '0' COMMENT 'Position',
  `is_parent` smallint(5) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'Is Parent',
  `store_id` smallint(5) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'Store ID',
  `visibility` smallint(5) UNSIGNED NOT NULL COMMENT 'Visibility'
) ENGINE=MEMORY DEFAULT CHARSET=utf8 COMMENT='Catalog Category Product Indexer Temp Table';
ALTER TABLE `catalog_category_product_index_tmp`
  ADD KEY `CAT_CTGR_PRD_IDX_TMP_PRD_ID_CTGR_ID_STORE_ID` (`product_id`,`category_id`,`store_id`);
  • Step 2: Try to re-index on local
  • Step 3: If you can re-index on the local then re-index on the server

Wrapping Up

We have shown you 3 steps to fix the deadlock error that occurs when re-indexing in Magento. If you have any problems when following the instructions, please leave a comment below.

2 thoughts on “How To Fix Deadlock Error When Reindexing All In Magento 2?

  1. Alkrenawi zid says:

    Hi CHARLIE,
    Does the solution (query) above is dropping and create the table as is or what exactly?

    Thanks,
    Zid

    • Mark Mac says:

      Hi Zidal, feel free to email us your question, as it would be quite lengthy to answer your question here. All the best!

Leave a Reply

Your email address will not be published. Required fields are marked *