ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

锁查询及处理

2022-01-18 17:32:17  阅读:140  来源: 互联网

标签:03 17 05 处理 查询 waiting other 2021


目录
环境
症状
问题原因
解决方案

环境
系统平台:N/A
版本:6.0
症状
数据库锁表后,对锁住的表无法进行改动,严重时会影响业务正常运转。

问题原因
多个会话在同一时间段对相同的数据做了改动,且第一个会话没有立即释放。

解决方案
1、创建测试表

highgo=# select * from hgjob_test033;

 id | name  |         timestart          | count 

----+-------+----------------------------+-------

  1 | job02 | 2021-03-05 17:44:40.885468 |     5

  2 | job02 | 2021-03-05 17:55:30.621806 |     5

  3 | job02 | 2021-03-05 17:59:24.859304 |     5

  4 | job02 | 2021-03-05 17:59:24.859304 |     5

  5 | job02 | 2021-03-05 17:59:24.859304 |     5

  6 | job02 | 2021-03-05 17:59:24.859304 |     5

  7 | job02 | 2021-03-05 17:59:24.859304 |     5

(7 rows)

2、模拟锁情况
窗口一:

highgo=# begin;

BEGIN

highgo=# update hgjob_test033 set name = 'newjob3' where id=2;

UPDATE 1

窗口二:

highgo=# begin ;

BEGIN

highgo=# update hgjob_test033 set name = 'newjob4' where id=2;

【此时会一直卡住不动】

窗口三:

创建锁状态视图

create view viewlocks as

SELECT

    waiting.locktype           AS waiting_locktype, --可锁定对象的类型:relation, extend, page, tuple,transactionid, virtualxid,object, userlock, advisory

    waiting.relation::regclass AS waiting_table,        --等待表

    waiting_stm.query          AS waiting_query,        --等待查询

    waiting.mode               AS waiting_mode,            --这个进程持有的或者是期望持有的锁模式

    waiting.pid                AS waiting_pid,            --持有或者等待这个锁的服务器进程的进程ID ,如果锁是被一个预备事务持有的,那么为空

    other.locktype             AS previous_locktype,        --当前锁的上层锁

    other.relation::regclass   AS previous_table,

    other_stm.query            AS previous_query,

    other_stm.state               AS previous_state,

    other.mode                 AS Previous_mode,

    other.pid                  AS previous_pid,            --等待该pid完成(kill)

    other.GRANTED              AS previous_granted            --如果持有锁,为真,如果等待锁,为假

FROM

    pg_catalog.pg_locks AS waiting

JOIN

    pg_catalog.pg_stat_activity AS waiting_stm

    ON (

        waiting_stm.pid = waiting.pid

    )

JOIN

    pg_catalog.pg_locks AS other

    ON (

        (

            waiting."database" = other."database"

        AND waiting.relation  = other.relation

        )

        OR waiting.transactionid = other.transactionid

    )

JOIN

    pg_catalog.pg_stat_activity AS other_stm

    ON (

        other_stm.pid = other.pid

    )

WHERE

    NOT waiting.GRANTED

AND

    waiting.pid <> other.pid;

3、查询锁状态

highgo=# select * from viewlocks;

 waiting_locktype | waiting_table |                     waiting_query                     | waiting_mode | waiting_pid | previous_locktype | previous_table |    

                previous_query                     |   previous_state    | previous_mode | previous_pid | previous_granted 

------------------+---------------+-------------------------------------------------------+--------------+-------------+-------------------+----------------+----

---------------------------------------------------+---------------------+---------------+--------------+------------------

 transactionid    |               | update hgjob_test033 set name = 'newjob4' where id=2; | ShareLock    |        4384 | transactionid     |                | upd

ate hgjob_test033 set name = 'newjob3' where id=2; | idle in transaction | ExclusiveLock |         4207 | t

(1 row)

4、取消对应操作进程

highgo=# select  pg_cancel_backend(4384);

 pg_cancel_backend 

-------------------

 t

(1 row)



日志打印:

ERROR:  canceling statement due to user request

CONTEXT:  while updating tuple (0,2) in relation "hgjob_test033"

数据状态:

highgo=# select * from hgjob_test033;

 id |  name   |         timestart          | count 

----+---------+----------------------------+-------

  1 | job02   | 2021-03-05 17:44:40.885468 |     5

  3 | job02   | 2021-03-05 17:59:24.859304 |     5

  4 | job02   | 2021-03-05 17:59:24.859304 |     5

  5 | job02   | 2021-03-05 17:59:24.859304 |     5

  6 | job02   | 2021-03-05 17:59:24.859304 |     5

  7 | job02   | 2021-03-05 17:59:24.859304 |     5

  2 | newjob3 | 2021-03-05 17:55:30.621806 |     5

(7 rows)

标签:03,17,05,处理,查询,waiting,other,2021
来源: https://blog.csdn.net/pg_hgdb/article/details/122564319

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有