Find last query executed by specific session in PostgreSQL database

PostgreSQL enables you to quick check last query executed by each session. To do this you need only one query.


select pid,
       usename as username,
       datname as database_name,
from pg_stat_activity
where pid = 'chosen_session';


  • pid - process ID of chosen session
  • username - name of the user logged into this backend
  • database_name - name of the database this backend is connected to
  • query - most recent query
  • application_name - name of the application that is connected to this backend
  • backend_start - time when this process was started. For client backends, this is the time the client connected to the server.
  • state - current overall state of this backend. Possible values are:
    • active
    • idle
    • idle in transaction
    • idle in transaction (aborted)
    • fastpath function call
    • disabled
  • state_change - time when the state was last changed


  • Query returns just one row

Sample results

sample results

Comments are only visible when the visitor has consented to statistics cookies. To see and add comments please accept statistics cookies.
There are no comments. Click here to write the first comment.