-- Текущие активные подключения
SELECT 
    pid,
    datname,
    usename,
    application_name,
    client_addr,
    state,
    query,
    query_start,
    state_change
FROM pg_stat_activity 
WHERE state = 'active'
ORDER BY query_start;

-- Количество подключений по клиентам
SELECT 
    client_addr,
    application_name,
    count(*) as connection_count,
    max(state) as state
FROM pg_stat_activity 
GROUP BY client_addr, application_name
ORDER BY connection_count DESC;

-- Долго выполняющиеся запросы
SELECT 
    pid,
    now() - query_start as duration,
    query,
    state
FROM pg_stat_activity 
WHERE state = 'active' 
AND now() - query_start > interval '30 seconds'
ORDER BY duration DESC;


-- Лимиты подключений
SHOW max_connections;
SHOW superuser_reserved_connections;

-- Таймауты
SHOW idle_in_transaction_session_timeout;
SHOW tcp_keepalives_idle;
SHOW tcp_keepalives_interval;


-- Проверим, что делает DBeaver
SELECT 
    pid,
    query,
    state,
    now() - query_start as running_for
FROM pg_stat_activity 
WHERE application_name LIKE '%DBeaver%'
AND state = 'active';

