Find last query executed by specific session in Redshift

Redshift, not like PostgreSQL, doesn't display info about last executed query in pg_stat_activity so here is another way to do this.

Query

select s.process,
       s.user_name,
       s.db_name,
       q.querytxt,
       q.starttime,
       q.endtime 
from stv_sessions s
left join stl_query q on s.process = q.pid
                      and s.db_name = q.database
where s.process = 'chosen session'
order by q.starttime desc
limit 1;

Columns

  • process - Process ID of the session
  • user_name - user name connected to session
  • db_name - database name
  • querytxt - most recent query
  • starttime - query start time
  • endtime - query end time

Rows

Query returns just one row.

Sample results

We can see that last query issued by pid 322 was "select * from event"

sample results