Book Image

PostgreSQL 9 Administration Cookbook - Second Edition

Book Image

PostgreSQL 9 Administration Cookbook - Second Edition

Overview of this book

Table of Contents (19 chapters)
PostgreSQL 9 Administration Cookbook Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Checking which queries are active or blocked


Here, we will show you how to know whether a query is actually running or it is waiting for another query.

Getting ready

Again, log in as a superuser.

How to do it…

Run this query:

SELECT datname, usename, query
       FROM pg_stat_activity
       WHERE waiting = true;

You will get a list of queries that are waiting on other backends. The following query will run on PostgreSQL versions older than 9.2:

SELECT datname, usename, current_query
       FROM pg_stat_activity
       WHERE waiting = true;

How it works…

The pg_stat_activity system view has a Boolean field named waiting. This field indicates that a certain backend is waiting on a system lock.

The preceding query uses it to filter out only those queries that are waiting.

There's more…

Some more explanations about the preceding queries are appropriate here.

No need for the = true part

As the waiting column is already Boolean, you can safely omit the = true part from the query and simply write the following...