What is running in my DB right now?

When you need to know what is executing in your database right now, these are the queries to execute.

Oracle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
-- rac
SELECT s.inst_id
, s.SID
, s.SERIAL#
, s.username
, s.osuser
, s.machine
, s.program
, s.logon_time
, s.status
, c.sql_fulltext current_sql
FROM gv$session s
JOIN gv$sqlarea c ON c.inst_id = s.inst_id AND c.sql_id = s.sql_id
WHERE s.USERNAME IS NOT NULL
AND s.STATUS = 'ACTIVE'
AND ( s.SQL_ID <> sys_context( 'userenv', 'sid' ) OR s.INST_ID <> USERENV( 'Instance' ) )
;

-- single node
SELECT s.SID
, s.SERIAL#
, s.username
, s.osuser
, s.machine
, s.program
, s.logon_time
, s.status
, c.sql_fulltext current_sql
FROM v$session s
JOIN v$sqlarea c ON c.sql_id = s.sql_id
WHERE s.USERNAME IS NOT NULL
AND s.STATUS = 'ACTIVE'
AND s.sid <> sys_context( 'userenv', 'sid' )
;

PostgreSQL

1
2
3
4
SELECT *
FROM pg_stat_activity psa
WHERE state <> 'idle'
AND pid <> pg_backend_pid( ) ;

MySQL

1
2
3
4
SELECT *
FROM information_schema.PROCESSLIST P
WHERE INFO IS NOT NULL
AND ID <> connection_id( ) ;

SQL Server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
SELECT db_name(st.dbid) as db
, object_name(st.objectid, st.dbid) as [object]
, st.text as query_text
, qp.query_plan
, req.session_id
, req.blocking_session_id
, req.cpu_time
, req.total_elapsed_time
, req.estimated_completion_time
, req.percent_complete
, req.wait_type
, req.wait_time
, req.last_wait_type
, req.wait_resource
, req.reads
, req.logical_reads
, req.row_count
, s.login_time
, s.host_name
, s.program_name
, s.host_process_id
, s.client_interface_name
, s.login_name
, s.nt_domain
, s.nt_user_name
, req.sql_handle
, req.plan_handle
FROM sys.dm_exec_requests req
JOIN sys.dm_exec_sessions s ON s.session_id = req.session_id
CROSS APPLY sys.dm_exec_sql_text(req.sql_handle) st
CROSS APPLY sys.dm_exec_query_plan(req.plan_handle) qp
WHERE req.session_id <> @@spid ;