O'Reilly logo

PostgreSQL High Performance Cookbook by Dinesh Kumar, Chitij Chauhan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required