Search code examples
mysqllockingmariadbinnodb

MariaDB: SELECT in stored procedure locks rows


I have a problem with SELECT in stored procedure.

Isolation level is READ-COMMITED, engine is InnoDB so all select should be nonlocking consistent read: https://dev.mysql.com/doc/refman/8.0/en/innodb-consistent-read.html

Database is MariaDB version 10.1:

MariaDB [(none)]> select @@version;
+-----------------+
| @@version       |
+-----------------+
| 10.1.24-MariaDB |
+-----------------+
1 row in set (0.02 sec)

Following select in stored procedure locks acquires lock on table agent:

IF 
    v_pack_agent_id IS NULL OR 
    v_pack_agent_id not in                  
    (
        select ID from agent where LINKED_AGENTS_GUID = (select LINKED_AGENTS_GUID from agent where ID = a_agent_id) union all 
        select a_agent_id
    )
THEN
    SET a_sign = SIGN_NOT_OWN_BLOCK;
    LEAVE l_proc;
END IF;

Table agent definition:

MariaDB [db_mercury]> show create table agent\G
*************************** 1. row ***************************
Table: agent
Create Table: CREATE TABLE `agent` (
`ID` smallint(5) unsigned NOT NULL AUTO_INCREMENT COMMENT 'PK',
 ...
`LINKED_AGENTS_GUID` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
 PRIMARY KEY (`ID`),
 UNIQUE KEY `UK$ORA_ID` (`ORA_ID`),
 KEY `IDX_LINKED_AGENTS_GUID` (`LINKED_AGENTS_GUID`)
 ) ENGINE=InnoDB AUTO_INCREMENT=6198 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

Problem is because select nested in IF block, if I move select outside of IF like below, then no locks are acquired.

This locks nothing:

select ID 
from agent 
where LINKED_AGENTS_GUID = (select LINKED_AGENTS_GUID 
                            from agent 
                            where ID = a_agent_id) 
union all 
select a_agent_id

For me it seems like a bug.

Any ideas why select is locking?


Solution

  • I reported a bug to MariaDB and they confirmed it: https://jira.mariadb.org/browse/MDEV-18044