lightdb for pg查看锁

select d.datname,c.relname,c.reltype,a.* from pg_locks a, pg_catalog.pg_database d,pg_catalog.pg_class c where d.oid = a.database 
and c.oid = a.relation ;

  该语句可以查出当前数据库中的所有。注意是当前数据库,不是整个pg实例。虽然pg_locks本身是实例级的,但是pg_class是数据库级的,所以关联之后,其他数据库的锁会查询不到。

复制代码
locktype     |database|relation|page|tuple|virtualxid|transactionid|classid|objid|objsubid|virtualtransaction|pid  |mode            |granted|fastpath|
-------------+--------+--------+----+-----+----------+-------------+-------+-----+--------+------------------+-----+----------------+-------+--------+
relation     |   57888|   12141|    |     |          |             |       |     |        |17/504            |15487|AccessShareLock |true   |true    |
virtualxid   |        |        |    |     |17/504    |             |       |     |        |17/504            |15487|ExclusiveLock   |true   |true    |
relation     |  107265|  335884|    |     |          |             |       |     |        |16/118            | 8451|AccessShareLock |true   |true    |
relation     |  107265|  335884|    |     |          |             |       |     |        |16/118            | 8451|RowExclusiveLock|true   |true    |
relation     |  107265|  335878|    |     |          |             |       |     |        |16/118            | 8451|AccessShareLock |true   |true    |
relation     |  107265|  335878|    |     |          |             |       |     |        |16/118            | 8451|RowExclusiveLock|true   |true    |
virtualxid   |        |        |    |     |16/118    |             |       |     |        |16/118            | 8451|ExclusiveLock   |true   |true    |
page         |  107265|  335884|   0|     |          |             |       |     |        |16/118            | 8451|ExclusiveLock   |true   |false   |
transactionid|        |        |    |     |          |12755336     |       |     |        |16/118            | 8451|ExclusiveLock   |true   |false   |
复制代码
select * from pg_locks;
select * from pg_catalog.pg_database ;
select * from pg_catalog.pg_class where oid=335878; -- 查询不到

   对于长时间的锁监控,可以查看postgresql.log,里面记录了获取锁超过参数设置的事务。

  也可以查询一下看看当前正在执行的查询是否已经获取锁了:

复制代码
SELECT
  S.pid,
  age(clock_timestamp(), query_start),
  usename,
  query,
  L.mode,
  L.locktype,
  L.granted
FROM pg_stat_activity S
inner join pg_locks L on S.pid = L.pid 
order by L.granted, L.pid DESC;
复制代码
复制代码
  pid  |       age       | usename |                  query                  |      mode       |  locktype  | granted 
-------+-----------------+---------+-----------------------------------------+-----------------+------------+---------
 20325 | 00:00:04.68855  | lightdb | select * from big_table for update;     | RowShareLock    | relation   | f
 20325 | 00:00:04.688562 | lightdb | select * from big_table for update;     | ExclusiveLock   | virtualxid | t
 19801 | 00:00:32.412    | lightdb | lock table big_table in exclusive mode; | ExclusiveLock   | relation   | t
 19801 | 00:00:32.412001 | lightdb | lock table big_table in exclusive mode; | ExclusiveLock   | virtualxid | t
  1054 | 00:00:00.002006 | lightdb | SELECT                                 +| ExclusiveLock   | virtualxid | t
       |                 |         |   S.pid,                               +|                 |            | 
       |                 |         |   age(clock_timestamp(), query_start), +|                 |            | 
       |                 |         |   usename,                             +|                 |            | 
       |                 |         |   query,                               +|                 |            | 
       |                 |         |   L.mode,                              +|                 |            | 
       |                 |         |   L.locktype,                          +|                 |            | 
       |                 |         |   L.granted                            +|                 |            | 
       |                 |         | FROM pg_stat_activity S                +|                 |            | 
       |                 |         | inner join pg_locks L on S.pid = L.pid +|                 |            | 
       |                 |         | order by L.granted, L.pid DESC;         |                 |            | 
复制代码

  锁的兼容性如下:

  所有的lock table table_name lock_mode都是表锁而不是行锁。只有select ... for update才能行锁。

  各种语句对应的锁可参见13.3. Explicit Locking (light-pg.com)

   pgrowlocks扩展可以用来查看某个表上的行锁信息。如下:

postgres=#  create extension pgrowlocks;
CREATE EXTENSION
postgres=# SELECT * FROM pgrowlocks('big_table');
 locked_row | locker | multi | xids | modes | pids 
------------+--------+-------+------+-------+------
(0 rows)

 

posted @   zhjh256  阅读(225)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2019-09-06 关于js.map以及css.map
点击右上角即可分享
微信分享提示