【问题标题】:postgres - find out sql query from transaction idpostgres - 从事务 id 中找出 sql 查询
【发布时间】:2013-03-29 01:10:11
【问题描述】:

我在跑步

select * from pg_lock;

获取具有排他锁的事务。

一旦我知道了具有排他锁的事务 id,我怎样才能找出与该事务关联的 sql 查询。

select * from pg_stat_activity;

给我 sql 查询,但不给我事务 ID。

有人可以帮我吗?

【问题讨论】:

  • 它是select * from pg_locks;。最后的s 很重要。

标签: hibernate postgresql transactions psql


【解决方案1】:

你需要加入 pid。

-- For PostgreSQL 9.1
select l.pid, l.mode, sa.procpid, sa.current_query
from pg_locks l
inner join pg_stat_activity sa
        on l.pid = sa.procpid
where l.mode like '%xclusive%';

-- For PostgreSQL 9.2
select l.pid, l.mode, sa.pid, sa.query
from pg_locks l
inner join pg_stat_activity sa
        on l.pid = sa.pid
where l.mode like '%xclusive%';

pid 列可以连接到 pg_stat_activity 视图的 pid 列,以获取有关会话持有或等待持有每个锁的更多信息。此外,如果您使用准备好的事务,可以将事务列连接到 pg_prepared_xacts 视图的事务列,以获取有关持有锁的准备事务的更多信息。 (准备好的事务永远不会等待锁,但它会继续持有它在运行时获得的锁。)

Source

可用的列和列名在 9.1 和 9.2 版本之间略有不同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 2019-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多