Finding out what the users are currently running

Using this recipe, we will get all the current running SQL commands from the PostgreSQL instance.

Getting ready

In the previous recipe, we discussed how to get all the active sessions information, and now we are going to get all the SQL statements that the active sessions are executing.

How to do it...

  1. Initiate pgbench as aforementioned.
  2. Connect to the PostgreSQL database as either a superuser or database owner and execute the following SQL statement:
     $ psql -h localhost -U postgres postgres=# SELECT datname, usename, application_name, now()-backend_start AS "Session duration", pid, query FROM pg_stat_activity WHERE state='active'; -[ RECORD 1 ]----+------------------------------- datname | postgres ...

Get PostgreSQL High Performance Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.