76

I'm investigating an issue with DB connections being left open indefinitely, causing problems on the DB server. How do I see currently open connections to a PostgreSQL server, particularly those using a specific database? Ideally I'd like to see what command is executing there as well. Basically, I'm looking for something equivalent to the "Current Activity" view in MSSQL.

  • from PostgreSQL 9.0 on you will even see the name of the application causing the problem. maybe this is helpful for future checks. – postgresql007 Apr 5 '10 at 18:19
103

OK, got it from someone else. This query does the trick:

select *
from pg_stat_activity
where datname = 'mydatabasename'
19

See also pg-top, which acts like top except it shows postgres activity.

  • Install pg-top (in Debian, the package is called "ptop").
  • Become the postgres user (e.g. sudo su postgres)
  • Run pg_top
  • Can it work without root access on the machine? – EMP Nov 29 '10 at 22:02
  • 1
    @Evgeny, As far as I know, you have to be logged in as the postgres user. That should be possible without root access. – Wayne Conrad Nov 30 '10 at 14:06
10

Reference taken from this article.

SELECT 
    pid
    ,datname
    ,usename
    ,application_name
    ,client_hostname
    ,client_port
    ,backend_start
    ,query_start
    ,query  
FROM pg_stat_activity
WHERE state <> 'idle'
AND pid<>pg_backend_pid();
  • query and pid were named current_query and procpid, and state is not available in pg_stat_activity prior to PostgreSQL 9.2. – cpburnz May 3 '16 at 3:27
1

PostgreSQL ASH Viewer (PASH Viewer) provides graphical view of active session history data. https://github.com/dbacvetkov/PASH-Viewer It's free and open source.

ScreenShot

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.