Database

PostgreSQL: Como saber quais as queries/locks que estão sendo executadas neste momento?

RT.DB.FAQ-8463
Executar a seguinte query:

PostgreSQL 10

SELECT pid
, query_start
, datname
, client_addr
, coalesce(wait_event_type||'/'||wait_event,'') as wait
, query
, state
FROM pg_stat_activity a
WHERE pid <> pg_backend_pid()
AND state <> 'idle'
ORDER BY query_start desc nulls last

PostgreSQL 9.5

SELECT datname
, pid
, query
, client_addr
, query_start
FROM pg_stat_activity  a
WHERE pid <> pg_backend_pid() -- Esconder o proprio processo 
AND query <> '<IDLE>'
ORDER BY query_start desc nulls last;

PostgreSQL 8.4

SELECT datname
, procpid
, current_query
, client_addr
, query_start
, waiting
FROM pg_stat_activity a
WHERE procpid <> pg_backend_pid() -- Esconder o proprio processo
AND current_query <> '<IDLE>'
ORDER BY query_start desc nulls last;


Locks

select t.relname
, l.locktype
, page
, virtualtransaction
, pid
, mode
, granted
, a.current_query
from pg_locks l
join pg_stat_all_tables t on l.relation = t.relid
join pg_stat_activity a on a.procpid = l.pid
where t.relname not in ('pg_class','pg_index','pg_namespace', 'pg_authid', 'pg_database')
order by relation asc;

-- Postgres 9.5
select t.relname
, l.locktype
, page
, virtualtransaction
, l.pid
, mode
, granted
, a.query
from pg_locks l
join pg_stat_all_tables t on l.relation = t.relid
join pg_stat_activity a on a.pid = l.pid
where t.relname not in ('pg_class','pg_index','pg_namespace', 'pg_authid', 'pg_database')
order by relation asc;


Ver:
http://wiki.postgresql.org/wiki/Lock_Monitoring