Postgresql 9.5 alter table 在没有其他长 运行 查询的情况下获得锁定超时
Postgresql 9.5 alter table gets lock timeout without other long running queries
我正在尝试执行 alter table mydb.r_group drop constraint test;
但是它花了 2 分钟多才获得独占锁。 table 正忙 table 有许多进程在查询它。 (进程使用 jdbc 访问数据库)
我试图使用以下查询语句找到一个长 运行 查询,但除了一些具有 idle
状态的查询外找不到任何其他查询。
SELECT
pid,
now() - pg_stat_activity.query_start AS duration,
query,
state
FROM pg_stat_activity
WHERE (now() - pg_stat_activity.query_start) > interval '5 minutes';
我还尝试使用以下查询语句找到阻止更改 table 查询的查询,但此查询也被阻止,因此无法获取数据。
SELECT blocked_locks.pid AS blocked_pid,
blocked_activity.usename AS blocked_user,
blocking_locks.pid AS blocking_pid,
blocking_activity.usename AS blocking_user,
blocked_activity.query AS blocked_statement,
blocking_activity.query AS current_statement_in_blocking_process
FROM pg_catalog.pg_locks blocked_locks
JOIN pg_catalog.pg_stat_activity blocked_activity ON blocked_activity.pid = blocked_locks.pid
JOIN pg_catalog.pg_locks blocking_locks
ON blocking_locks.locktype = blocked_locks.locktype
AND blocking_locks.DATABASE IS NOT DISTINCT FROM blocked_locks.DATABASE
AND blocking_locks.relation IS NOT DISTINCT FROM blocked_locks.relation
AND blocking_locks.page IS NOT DISTINCT FROM blocked_locks.page
AND blocking_locks.tuple IS NOT DISTINCT FROM blocked_locks.tuple
AND blocking_locks.virtualxid IS NOT DISTINCT FROM blocked_locks.virtualxid
AND blocking_locks.transactionid IS NOT DISTINCT FROM blocked_locks.transactionid
AND blocking_locks.classid IS NOT DISTINCT FROM blocked_locks.classid
AND blocking_locks.objid IS NOT DISTINCT FROM blocked_locks.objid
AND blocking_locks.objsubid IS NOT DISTINCT FROM blocked_locks.objsubid
AND blocking_locks.pid != blocked_locks.pid
JOIN pg_catalog.pg_stat_activity blocking_activity ON blocking_activity.pid = blocking_locks.pid
WHERE NOT blocked_locks.GRANTED;
有谁知道我可以采取哪些其他步骤来找到根本原因?
首先,尝试找到有问题的 table 的对象 ID:
SELECT tableoid
FROM mydb.r_group
LIMIT 1;
然后找到这个table上的所有锁:
SELECT pid, mode, granted
FROM pg_locks
WHERE database = current_database()
AND locktype = 'relation'
AND relation = <the number from above>;
然后你有一个所有会阻止 ALTER TABLE
的会话的列表。
在 pg_stat_activity
中寻找这些 pid
,看看他们在做什么。特别有趣的是 xact_start
,活动事务开始的时间。
如果您在 pg_stat_activity
中找不到阻塞进程,它也可能是后台进程,很可能是 anti-wraparound autovacuum worker。在 shell.
上使用 ps
查找阻塞进程 ID
我正在尝试执行 alter table mydb.r_group drop constraint test;
但是它花了 2 分钟多才获得独占锁。 table 正忙 table 有许多进程在查询它。 (进程使用 jdbc 访问数据库)
我试图使用以下查询语句找到一个长 运行 查询,但除了一些具有 idle
状态的查询外找不到任何其他查询。
SELECT
pid,
now() - pg_stat_activity.query_start AS duration,
query,
state
FROM pg_stat_activity
WHERE (now() - pg_stat_activity.query_start) > interval '5 minutes';
我还尝试使用以下查询语句找到阻止更改 table 查询的查询,但此查询也被阻止,因此无法获取数据。
SELECT blocked_locks.pid AS blocked_pid,
blocked_activity.usename AS blocked_user,
blocking_locks.pid AS blocking_pid,
blocking_activity.usename AS blocking_user,
blocked_activity.query AS blocked_statement,
blocking_activity.query AS current_statement_in_blocking_process
FROM pg_catalog.pg_locks blocked_locks
JOIN pg_catalog.pg_stat_activity blocked_activity ON blocked_activity.pid = blocked_locks.pid
JOIN pg_catalog.pg_locks blocking_locks
ON blocking_locks.locktype = blocked_locks.locktype
AND blocking_locks.DATABASE IS NOT DISTINCT FROM blocked_locks.DATABASE
AND blocking_locks.relation IS NOT DISTINCT FROM blocked_locks.relation
AND blocking_locks.page IS NOT DISTINCT FROM blocked_locks.page
AND blocking_locks.tuple IS NOT DISTINCT FROM blocked_locks.tuple
AND blocking_locks.virtualxid IS NOT DISTINCT FROM blocked_locks.virtualxid
AND blocking_locks.transactionid IS NOT DISTINCT FROM blocked_locks.transactionid
AND blocking_locks.classid IS NOT DISTINCT FROM blocked_locks.classid
AND blocking_locks.objid IS NOT DISTINCT FROM blocked_locks.objid
AND blocking_locks.objsubid IS NOT DISTINCT FROM blocked_locks.objsubid
AND blocking_locks.pid != blocked_locks.pid
JOIN pg_catalog.pg_stat_activity blocking_activity ON blocking_activity.pid = blocking_locks.pid
WHERE NOT blocked_locks.GRANTED;
有谁知道我可以采取哪些其他步骤来找到根本原因?
首先,尝试找到有问题的 table 的对象 ID:
SELECT tableoid
FROM mydb.r_group
LIMIT 1;
然后找到这个table上的所有锁:
SELECT pid, mode, granted
FROM pg_locks
WHERE database = current_database()
AND locktype = 'relation'
AND relation = <the number from above>;
然后你有一个所有会阻止 ALTER TABLE
的会话的列表。
在 pg_stat_activity
中寻找这些 pid
,看看他们在做什么。特别有趣的是 xact_start
,活动事务开始的时间。
如果您在 pg_stat_activity
中找不到阻塞进程,它也可能是后台进程,很可能是 anti-wraparound autovacuum worker。在 shell.
ps
查找阻塞进程 ID