From 84da465c6039a9fd8f4ab6ff18998d59c07eff41 Mon Sep 17 00:00:00 2001 From: Richard Mathot Date: Wed, 16 Sep 2020 13:08:21 +0000 Subject: [PATCH 1/2] [FIX] mrp: installation on very large inventories Before this commit, installation of MRP in a database with 4.8M stock.move + 4.8M stock.move.line was not possible at all. The process lasted for a very long time (>90 min in our tests) then eventually failed after using the whole RAM of your server. This commit allows installing MRP in databases with large stock.move / stock.move.line tables by bootstrapping the slow parts in SQL. A more general fix regarding the time complexity of some parts of the prefetching algorithm is coming later in 13.0-init-computed-fields-rco opw-2281585 X-original-commit: 549de5601efdad3ebdb03feb8beb1e898f80113a --- addons/mrp/__init__.py | 12 ++++++++++++ addons/mrp/__manifest__.py | 1 + 2 files changed, 13 insertions(+) diff --git a/addons/mrp/__init__.py b/addons/mrp/__init__.py index 07f99e38042..db4bea45d37 100644 --- a/addons/mrp/__init__.py +++ b/addons/mrp/__init__.py @@ -8,6 +8,18 @@ from . import controller from odoo import api, SUPERUSER_ID + +def _pre_init_mrp(cr): + """ Allow installing MRP in databases with large stock.move table (>1M records) + - Creating the computed+stored field stock_move.is_done is terribly slow with the ORM and + leads to "Out of Memory" crashes + """ + cr.execute("""ALTER TABLE "stock_move" ADD COLUMN "is_done" bool;""") + cr.execute("""UPDATE stock_move + SET is_done=TRUE + WHERE STATE='done' + OR STATE='cancel';""") + def _create_warehouse_data(cr, registry): """ This hook is used to add a default manufacture_pull_id, manufacture picking_type on every warehouse. It is necessary if the mrp module is diff --git a/addons/mrp/__manifest__.py b/addons/mrp/__manifest__.py index fc38329c1be..2be559a112e 100644 --- a/addons/mrp/__manifest__.py +++ b/addons/mrp/__manifest__.py @@ -52,6 +52,7 @@ ], 'test': [], 'application': True, + 'pre_init_hook': '_pre_init_mrp', 'post_init_hook': '_create_warehouse_data', 'uninstall_hook': 'uninstall_hook', } From e666694e2af6c48db8989f76c80a4b6efb20955c Mon Sep 17 00:00:00 2001 From: Richard Mathot Date: Wed, 16 Sep 2020 13:35:02 +0000 Subject: [PATCH 2/2] [FIX] mrp: more Voodoo magic It appears that if you update a record more than once in the same PostgreSQL transaction, PostgreSQL is going to re-check all the constraints on the table. ** THIS IS THE PART WHERE THE BLACK MAGIC HAPPENS ** We combine the setting of the default value of stock.move.unit_factor in the same UPDATE query to avoid the constraint re-check; it saves approximately 60% of the SQL running time in my case. ** ... AND NOW YOU CAN SAY: WHAT THE F...?! ** Thanks to @rco-odoo for the tip; he knows it from the wise @nseinlet. X-original-commit: 8e47e2f605309c8ed0d5509f7bb070528d8f32c4 --- addons/mrp/__init__.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/addons/mrp/__init__.py b/addons/mrp/__init__.py index db4bea45d37..01c44c4b198 100644 --- a/addons/mrp/__init__.py +++ b/addons/mrp/__init__.py @@ -16,9 +16,7 @@ def _pre_init_mrp(cr): """ cr.execute("""ALTER TABLE "stock_move" ADD COLUMN "is_done" bool;""") cr.execute("""UPDATE stock_move - SET is_done=TRUE - WHERE STATE='done' - OR STATE='cancel';""") + SET is_done=COALESCE(state in ('done', 'cancel'), FALSE);""") def _create_warehouse_data(cr, registry): """ This hook is used to add a default manufacture_pull_id, manufacture