[MDEV-31858] NEXTVAL on nocache sequence encounter "ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction" in Galera Created: 2023-08-06  Updated: 2023-09-11  Resolved: 2023-09-11

Status: Closed
Project: MariaDB Server
Component/s: Galera, Server
Affects Version/s: 10.6.14
Fix Version/s: N/A

Type: Bug Priority: Major
Reporter: William Wong Assignee: Jan Lindström
Resolution: Not a Bug Votes: 0
Labels: None
Environment:

redhat 7 on VMware



 Description   

One of our DB is using 2 DB nodes + 1 aribtrator architecture with HAProxy on top. Found nextval error when sequence object is using nocache. Increment is 0 as Galera requirement.
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction

DB nodes are using MariaDB version 10.6.14 and Galera 26.4.12.

Below is the test case. One node will encounter error easily.

# create sequence with nocache
drop   sequence s ;
create sequence s increment=0 cache=0 ;
show create sequence s ;
 
 
# create procedure for the test
DROP PROCEDURE IF EXISTS seq_test;
 
DELIMITER $$
CREATE PROCEDURE seq_test(IN num_of_turn int, IN slp float)
BEGIN
    DECLARE finished  char(1);
	DECLARE cnt int;
	
	SET cnt=0;
	
    WHILE cnt <= num_of_turn DO
        SELECT NEXTVAL(s);
 
        SET cnt=cnt+1;
 
        SELECT SLEEP(slp);
    END WHILE;
END $$
 
DELIMITER ;
 
 
# node 1
call seq_test(1000, 0.3);
 
 
# node 2
call seq_test(1000, 0.2);



 Comments   
Comment by Jan Lindström [ 2023-09-11 ]

I could reproduce this issue but it is not a bug. This is because sequence value is stored in special table and SELECT NEXTVAL is basically update of latest used value. Concurrent execution could cause both lock wait as well as deadlock errors especially if you have multi-master setup.

Generated at Thu Feb 08 10:27:00 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.