zoukankan      html  css  js  c++  java
  • Postgresql 解决锁表

    转载地址:https://blog.csdn.net/cicon/article/details/68068462
    ##一、postgresql解决锁表
    --查询是否锁表了
    select oid from pg_class where relname='可能锁表了的表'
    select pid from pg_locks where relation='上面查出的oid'
    --如果查询到了结果,表示该表被锁 则需要释放锁定
    select pg_cancel_backend(上面查到的pid)
    12345
    二、查看锁的其他方法
    转载地址:https://yq.aliyun.com/ask/184?order=ctime
    --  用一个函数来将锁转换为数字,
    create or replace function f_lock_level(i_mode text) returns int as
    $$
    declare
    begin
      case i_mode
        when 'INVALID' then return 0;
        when 'AccessShareLock' then return 1;
        when 'RowShareLock' then return 2;
        when 'RowExclusiveLock' then return 3;
        when 'ShareUpdateExclusiveLock' then return 4;
        when 'ShareLock' then return 5;
        when 'ShareRowExclusiveLock' then return 6;
        when 'ExclusiveLock' then return 7;
        when 'AccessExclusiveLock' then return 8;
        else return 0;
      end case;
    end;
    $$
     language plpgsql strict;
    -- 修改查询语句,按锁级别排序:
    with t_wait as                    
    (   select a.mode,a.locktype,a.database,a.relation,a.page,a.tuple,a.classid,a.objid,a.objsubid,
     a.pid,a.virtualtransaction,a.virtualxid,a,transactionid,b.query,b.xact_start,b.query_start,
     b.usename,b.datname
     from pg_locks a,pg_stat_activity b
     where a.pid=b.pid and not a.granted
    ),
    t_run as
    (
     select a.mode,a.locktype,a.database,a.relation,a.page,a.tuple,a.classid,a.objid,a.objsubid,
     a.pid,a.virtualtransaction,a.virtualxid,a,transactionid,b.query,b.xact_start,b.query_start,
     b.usename,b.datname
     from pg_locks a,pg_stat_activity b
     where a.pid=b.pid and a.granted
    )
    select r.locktype,r.mode r_mode,r.usename r_user,r.datname r_db,r.relation::regclass,r.pid r_pid,
    r.page r_page,r.tuple r_tuple,r.xact_start r_xact_start,r.query_start r_query_start,
    now()-r.query_start r_locktime,r.query r_query,w.mode w_mode,w.pid w_pid,w.page w_page,
    w.tuple w_tuple,w.xact_start w_xact_start,w.query_start w_query_start,
    now()-w.query_start w_locktime,w.query w_query 
    from t_wait w,t_run r
    where r.locktype is not distinct from w.locktype
    and r.database is not distinct from w.database
    and r.relation is not distinct from w.relation
    and r.page is not distinct from w.page
    and r.tuple is not distinct from w.tuple
    and r.classid is not distinct from w.classid
    and r.objid is not distinct from w.objid
    and r.objsubid is not distinct from w.objsubid
    and r.transactionid is not distinct from w.transactionid
    and r.pid <> w.pid
    order by f_lock_level(w.mode)+f_lock_level(r.mode) desc,r.xact_start;
    12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
    --现在可以排在前面的就是锁级别高的等待,优先干掉这个。
    -[ RECORD 1 ]-+---------------------------------------------------------------------
    locktype      | relation  -- 冲突类型
    r_mode        | ShareUpdateExclusiveLock  -- 持锁模式
    r_user        | postgres  -- 持锁用户
    r_db          | postgres  -- 持锁数据库
    relation      | tbl  -- 持锁对象
    r_pid         | 25656  -- 持锁进程
    r_xact_start  | 2015-05-10 14:11:16.08318+08  -- 持锁事务开始时间
    r_query_start | 2015-05-10 14:11:16.08318+08  -- 持锁SQL开始时间
    r_locktime    | 00:01:49.460779  -- 持锁时长
    r_query       | vacuum freeze tbl;  --  持锁SQL,注意不一定是这个SQL带来的锁,也有可能是这个事务在之前执行的SQL加的锁
    w_mode        | AccessExclusiveLock  -- 等待锁模式
    w_pid         | 26731  -- 等待锁进程
    w_xact_start  | 2015-05-10 14:11:17.987362+08  --  等待锁事务开始时间
    w_query_start | 2015-05-10 14:11:17.987362+08  --  等待锁SQL开始时间
    w_locktime    | 00:01:47.556597  --  等待锁时长
    w_query       | truncate tbl;  -- 等待锁SQL
    -[ RECORD 2 ]-+---------------------------------------------------------------------
    locktype      | relation
    r_mode        | ShareUpdateExclusiveLock
    r_user        | postgres
    r_db          | postgres
    relation      | tbl
    r_pid         | 25656
    r_xact_start  | 2015-05-10 14:11:16.08318+08
    r_query_start | 2015-05-10 14:11:16.08318+08
    r_locktime    | 00:01:49.460779
    r_query       | vacuum freeze tbl;
    w_mode        | RowExclusiveLock
    w_pid         | 25582
    w_xact_start  | 2015-05-10 14:11:22.845+08
    w_query_start | 2015-05-10 14:11:22.845+08
    w_locktime    | 00:01:42.698959
    w_query       | insert into tbl(crt_time) select now() from generate_series(1,1000);  -- 这个SQL其实等待的是truncate tbl的锁;
    ......

  • 相关阅读:
    SAP問題點(一)
    都是粗心惹的禍
    学习笔记<一>
    灰色心情
    SAP问题点(二)
    AngularJS Unknown provider报错
    Nginx随笔
    Nginx配置限制IP访问
    Windows The task you are trying to do can't be completed because Remote Desktop Services is currently busy. Please try again in a few minutes. Other users should still be able to log on
    在DOS使用NotePad++打开文件
  • 原文地址:https://www.cnblogs.com/xibuhaohao/p/11126033.html
Copyright © 2011-2022 走看看