From c5c955db7acd94ae4682a1e5040320806ca22056 Mon Sep 17 00:00:00 2001
From: Xavier Morel <xmo@odoo.com>
Date: Tue, 4 Jun 2019 10:55:12 +0000
Subject: [PATCH] [FIX] core: work around ir_logging deadlock

DROP CONSTRAINT (even with IF EXISTS is specified) acquires an ACCESS
EXCLUSIVE lock on the table, preventing e.g. inserts in an other
transaction, so ir_logging would systematically deadlock if configured
to the same database and a warning would be triggered during install
or update (if that ran ir.logging's init).

1. hand-roll the "IF EXISTS" bit, to avoid taking an ACCESS EXCLUSIVE
lock on the table if the problematic constraint does not exist and
thus doesn't need to be dropped (which by now should be the vast
majority of cases).

Replacing DROP CONSTRAINT with DISABLE TRIGGER does not fix the
issue as *that* acquires SHARE ROW EXCLUSIVE. While that's less
constraitning than ACCESS EXCLUSIVE, it still conflicts with an
insert's ROW_EXCLUSIVE.

2. add a timeout to the logging INSERT anyway, the deadlock is still
an issue if we're updating a database which does have the
problematic constraint, and we want to preclude the possible
eventual introduction of new deadlocks in the future.

closes odoo/odoo#34243

Signed-off-by: Xavier Morel (xmo) <xmo@odoo.com>
---
 odoo/addons/base/ir/ir_logging.py | 8 +++++++-
 odoo/netsvc.py                    | 5 +++--
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/odoo/addons/base/ir/ir_logging.py b/odoo/addons/base/ir/ir_logging.py
index 4e3caef0794a..f672a76edff9 100644
--- a/odoo/addons/base/ir/ir_logging.py
+++ b/odoo/addons/base/ir/ir_logging.py
@@ -34,4 +34,10 @@ class IrLogging(models.Model):
     @api.model_cr
     def init(self):
         super(IrLogging, self).init()
-        self._cr.execute("ALTER TABLE ir_logging DROP CONSTRAINT IF EXISTS ir_logging_write_uid_fkey")
+        self._cr.execute("select 1 from information_schema.constraint_column_usage where table_name = 'ir_logging' and constraint_name = 'ir_logging_write_uid_fkey'")
+        if self._cr.rowcount:
+            # DROP CONSTRAINT unconditionally takes an ACCESS EXCLUSIVE lock
+            # on the table, even "IF EXISTS" is set and not matching; disabling
+            # the relevant trigger instead acquires SHARE ROW EXCLUSIVE, which
+            # still conflicts with the ROW EXCLUSIVE needed for an insert
+            self._cr.execute("ALTER TABLE ir_logging DROP CONSTRAINT ir_logging_write_uid_fkey")
diff --git a/odoo/netsvc.py b/odoo/netsvc.py
index 84e9702479b9..dac9b433e8c4 100644
--- a/odoo/netsvc.py
+++ b/odoo/netsvc.py
@@ -54,7 +54,7 @@ def LocalService(name):
 path_prefix = os.path.realpath(os.path.dirname(os.path.dirname(__file__)))
 
 class PostgreSQLHandler(logging.Handler):
-    """ PostgreSQL Loggin Handler will store logs in the database, by default
+    """ PostgreSQL Logging Handler will store logs in the database, by default
     the current database, can be set using --log-db=DBNAME
     """
     def emit(self, record):
@@ -64,7 +64,8 @@ class PostgreSQLHandler(logging.Handler):
         if not dbname:
             return
         with tools.ignore(Exception), tools.mute_logger('odoo.sql_db'), sql_db.db_connect(dbname, allow_uri=True).cursor() as cr:
-            cr.autocommit(True)
+            # preclude risks of deadlocks
+            cr.execute("SET LOCAL statement_timeout = 1000")
             msg = tools.ustr(record.msg)
             if record.args:
                 msg = msg % record.args
-- 
GitLab