DB2 set current schema也能导致锁升级(lock escalation)?

最近遇到锁升级的问题,发现db2diag.log里出现set current schema语句导致锁升级,印象中只有SQL语句会导致锁升级。于是模拟了一下,模拟的重点在于如何把locklist用满,并且保持满的状态。


重现过程:

重现过程比较简单,将锁内存调小,然后session 1往一张大表里不提的方式插入1条数据,锁住表。
Session 2以RS隔离级别查询这个大表,这时候session 2会试图在每一条记录上都加锁,讯速占满locklist,并尝试锁升级(由于锁等,一会HANG)。
Session 3中基本上随便执行一条语句都会锁升级,包括 set current schema .


首先将数据库的下面3个参数调整一下,调整为如下值
LOCKLIST 80 MAXLOCKS 100 LOCKTIMEOUT -1

Session 1
Session 1创建一张表,并load数据
$ seq 1 10000000 > t1.del
$ db2 "create table t1(id int)"
$ db2 "load from t1.del of del insert into t1 nonrecoverable"
$ db2 +c "insert into t1 values(100)"

Session 2

Sesion 2以RS隔离级别查询t1所有记录
$ db2 "select * from t1 with rs"

此时Session 2已经持有了很多行锁,导致需要锁升级,但是由于锁等,锁升级不会成功(也未失败,一直hang着),此时locklist是满的。

Session 3
Session 3里, 执行 set current schema 就会在db2diag.log里出现锁升级的日志
$ db2 set current schema pera
DB20000I  The SQL command completed successfully.

db2diag.log如下

2020-07-22-09.06.49.678887-240 E166757E966           LEVEL: Info
PID     : 16819                TID : 140523427849984 PROC : db2sysc 0
INSTANCE: db2inst1             NODE : 000            DB   : SAMPLE
APPHDL  : 0-22                 APPID: *LOCAL.db2inst1.200722130639
AUTHID  : DB2INST1             HOSTNAME: node01
EDUID   : 22                   EDUNAME: db2agent (SAMPLE) 0
FUNCTION: DB2 UDB, data management, sqldEscalateLocks, probe:1
MESSAGE : ADM5501I  DB2 is performing lock escalation. The affected application 
          is named "db2bp", and is associated with the workload name 
          "SYSDEFAULTUSERWORKLOAD" and application ID 
          "*LOCAL.db2inst1.200722130639"  at member "0". The total number of 
          locks currently held is "4", and the target number of locks to hold 
          is "2". The current statement being executed is "set current schema 
          pera". Reason code: "2"
DATA #1 : Hex integer, 8 bytes
0x830000000004003B

2020-07-22-09.06.49.679409-240 E167724E560           LEVEL: Warning
PID     : 16819                TID : 140523427849984 PROC : db2sysc 0
INSTANCE: db2inst1             NODE : 000            DB   : SAMPLE
APPHDL  : 0-22                 APPID: *LOCAL.db2inst1.200722130639
AUTHID  : DB2INST1             HOSTNAME: node01
EDUID   : 22                   EDUNAME: db2agent (SAMPLE) 0
FUNCTION: DB2 UDB, data management, sqldEscalateLocks, probe:3
DATA #1 : <preformatted>
Client Information for lock escalation not available because it was not set for this agent 


通过上面的模拟,我们也能了解为什么有的系统一旦有锁升级,db2diag.log里会出现N多的SQL语句都锁升级了,逻辑如下:
应用A可能在某张表上加了表锁或者行锁,这时候应用B因为SQL不好的原因占用了太多行锁,需要锁升级,会把整个locklist占满。 这时候其他应用基本上执行啥SQL都会锁升级,因为 locklist被应用B占满了。
只有B锁升级成功后,locklist才能降下来。

扫描二维码关注公众号,回复: 11401542 查看本文章

猜你喜欢

转载自blog.csdn.net/qingsong3333/article/details/107524150