Find the longest-running queries right now in PostgreSQL
What is running right now, ranked by duration, with the exact text and wait state. The snapshot you need mid-incident.
The task
During a slowdown you need to know what is executing at this instant. pg_stat_activity reports one row per server session with its current or last query, when it started, and what it is waiting on. wait_event_type and wait_event name the lock, I/O or IPC wait; both are NULL when the backend is running on CPU.
The snippet
Written for PostgreSQL. Versions for SQL Server are under Variations.
SELECT pid,
usename, datname, application_name, client_addr,
state,
wait_event_type, wait_event,
now() - query_start AS running_for,
now() - xact_start AS xact_age,
LEFT(query, 140) AS query
FROM pg_stat_activity
WHERE state = 'active'
AND pid <> pg_backend_pid()
ORDER BY query_start
LIMIT 25;
Fonctionnement
Each pg_stat_activity row carries the session's state (active, idle, idle in transaction), the query text, and two clocks: query_start for when the current statement began and xact_start for when its transaction began. A transaction can hold locks far longer than any single statement runs. When xact_age is much larger than running_for, the session has run earlier statements in the same transaction and may be holding their locks. wait_event_type = 'Lock' means the query is blocked by another session, and the who-blocks-whom recipe names the blocker. The pg_backend_pid() exclusion keeps this query out of its own results.
Variations
Also show idle in transaction sessions
SELECT pid, usename, datname, state,
now() - xact_start AS xact_age,
now() - state_change AS idle_for,
LEFT(query, 100) AS last_query
FROM pg_stat_activity
WHERE state IN ('active', 'idle in transaction', 'idle in transaction (aborted)')
AND pid <> pg_backend_pid()
ORDER BY COALESCE(xact_start, state_change);
SQL Server: currently running requests
SQL ServerSELECT r.session_id, r.blocking_session_id, r.wait_type, r.wait_time,
r.start_time, r.status,
LEFT(t.text, 140) AS query
FROM sys.dm_exec_requests r
JOIN sys.dm_exec_sessions s ON s.session_id = r.session_id
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) t
WHERE s.is_user_process = 1
AND r.session_id <> @@SPID
ORDER BY r.start_time;
Questions that come up
A query has been running for hours. Should I kill it?
Check what it is doing first. If wait_event_type is Lock, it is waiting on someone else's lock and cancelling it frees nothing; find the blocker instead. If it is doing real work, find out who owns it, because cancelling a four-hour batch at hour three throws the work away. pg_cancel_backend(pid) stops the statement and keeps the connection; the kill-a-query recipe has the checklist.
Why does idle in transaction show up here but state is not active?
The main query only shows state = 'active'. A session can be inside an open transaction without running anything: the application ran BEGIN and a few statements and then went quiet. Those sessions hold their locks and their snapshot, which is why the variant lists them alongside active queries.
Keep reading
Run these with autocomplete that knows your tables
SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.