O'Reilly logo

PostgreSQL 9.0 High Performance by Gregory Smith

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

Connections and activity

pg_stat_activity provides a way to get a snapshot of what every client on the server is currently doing. Because it includes a process ID, on UNIX-like systems pg_stat_activity is also useful to line up with information collected at the operating system level, by utilities such as top or ps.

The simplest thing to do with this view is to count how many client backends are currently active:

pgbench=# SELECT count(*) FROM pg_stat_activity WHERE NOT procpid=pg_backend_pid();
 count 
-------
     4

As the query itself will normally appear in the results, that's filtered out by looking up its process ID and excluding it. This is a good practice to get into queries against this view. This total gives you an idea how close you are ...

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