# Представление активных запросов
SELECT * FROM pg_stat_activity;
SELECT query, state, waiting, pid
FROM pg_stat_activity
WHERE datname = 'DatabASeName'
AND NOT (state = 'idle' OR pid = pg_backend_pid());
# Документация по представлению блокировок # https://postgrespro.ru/docs/postgrespro/10/view-pg-locks
SELECT * FROM pg_locks;
# Список запросов, которые заблокировали друг-друга
SELECT
pg_ca.pid AS blocked_pid,
pg_ca.query AS blocked_query,
pg_sa.pid AS blocking_pid,
pg_sa.query AS blocking_query
FROM pg_catalog.pg_locks pg_cl
JOIN pg_stat_activity pg_ca ON pg_ca.pid = pg_cl.pid
JOIN pg_catalog.pg_locks pg_lo ON pg_lo.pid != pg_cl.pid
AND pg_lo.transactionid = pg_cl.transactionid
JOIN pg_stat_activity pg_sa ON pg_sa.pid = pg_lo.pid
WHERE NOT pg_cl.granted
AND pg_sa.datname = 'DatabASeName';
# Вывести блокирующие запросы из базы # wait_duration - время выполнения запроса
SELECT pid, query, now() - query_start AS wait_duration
FROM pg_catalog.pg_stat_activity
WHERE datname = 'DatabASeName'
AND waiting;
# Вывести что именно болкировано
SELECT
pg_ca.relation::regclass,
pg_sa.pid AS blocked_pid,
pg_sa.query AS blocked_query,
pg_cl.mode AS blocked_mode,
pg_sa.pid AS blocking_pid,
pg_sa.query AS blocking_query,
pg_ca.mode AS blocking_mode
FROM pg_catalog.pg_locks pg_cl
JOIN pg_stat_activity pg_sa
ON pg_sa.pid = pg_cl.pid
JOIN pg_catalog.pg_locks pg_ca
ON pg_cl.pid != pg_ca.pid
AND pg_ca.relation = pg_cl.relation
AND pg_ca.locktype = pg_cl.locktype
JOIN pg_stat_activity pg_sa
ON pg_sa.pid = pg_ca.pid
WHERE not pg_cl.granted
AND pg_sa.datname = 'DatabASeName';
Комментарии пользователей
Эту новость ещё не комментировалиНаписать комментарий
Анонимам нельзя оставоять комментарии, зарегистрируйтесь!