In this recipe, we are going to learn how to check for active sessions in a database.
We are going to query the pg_stat_activity
table to check for active sessions in a database. The query used in this recipe works in PostgreSQL version 9.2 onwards.
We can use the following SQL query to find the active sessions in the hrdb
database:
SELECT pid , usename, application_name, client_addr, client_hostname, query, state from pg_stat_activity where datname='dvdrental';
We use the preceding query to find all of the client connections made to the hrdb
database. Here is an explanation of the columns in the pg_stat_activity
table to find information regarding active sessions in the hrdb
database: