Skip to content

SQL Monitoring in PostgreSQL (3) – pg_activity

The last posts looked at how the logging system and the pg_stat_statements extension can be used to monitor sql statements in PostgreSQL. This post will introduce pg_activity which is very similar to htop.

There are some dependencies which need to be installed before we can start installing pg_activity. The first one is python. As I am on a redhat based distribution this is quite easy:

[root@oel7 ~] yum install -y python

Then we need to install psycopg which is the database adapter for PostgreSQL for the python language (Note: if you have installed PostgreSQL not in the default location edit the setup.cfg script and provide the path to pg_config. Otherwise the install will fail):

postgres@oel7:/var/tmp/ [dummy] tar -axf psycopg2-2.6.1.tar.gz
postgres@oel7:/var/tmp/ [dummy] cd psycopg2-2.6.1
postgres@oel7:/var/tmp/psycopg2-2.6.1/ [dummy] python setup.py build
postgres@oel7:/var/tmp/psycopg2-2.6.1/ [dummy] sudo python setup.py install

The next (and last) thing we need to have available is psutils, a python library for querying os statistics:

postgres@oel7:/var/tmp/ [dummy] tar -axf psutil-3.3.0.tar.gz
postgres@oel7:/var/tmp/ [dummy] cd psutil-3.3.0
postgres@oel7:/var/tmp/ [dummy] sudo python setup.py install

That’s it. Now we can install pg_activity:

postgres@oel7:/var/tmp/ [dummy] unzip pg_activity-master.zip
postgres@oel7:/var/tmp/ [dummy] cd pg_activity-master
postgres@oel7:/var/tmp/ [dummy] sudo python setup.py install

Quite easy. Lets see what we can do with it. If you are locally on the server where your PostgreSQL instance runs you can just start pg_activity (I fired a sql statement so that you can see at least one sql in the screenshot):

postgres@oel7:/home/postgres/ [PG3] pg_activity

pg_activity_1

There is a nice summary on the top like in top/htop. The different statements which are currently executing are displayed below.

Hitting “h” for help shows you the various options:
pg_activity_2

The “F1/2/3” switches are very nice when you want to display blocking queries only, running queries only or waiting queries only. Another great features is that you do not need to install pg_activity on the server where PostgreSQL is running. The same connections options as in e.g. psql are there so that you can connect to any remote PostgreSQL instance you have access to:

postgres@oel7:/home/postgres/ [PG3] pg_activity --help
Usage: pg_activity [options]

htop like application for PostgreSQL server activity monitoring.

Options:
  --version             show program's version number and exit
  -U USERNAME, --username=USERNAME
                        Database user name (default: "postgres").
  -p PORT, --port=PORT  Database server port (default: "5432").
  -h HOSTNAME, --host=HOSTNAME
                        Database server host or socket directory
                        (default: "localhost").
  -d DBNAME, --dbname=DBNAME
                        Database name to connect to (default: "postgres").
  -C, --no-color        Disable color usage.
  --blocksize=BLOCKSIZE
                        Filesystem blocksize (default: 4096)
  --rds                 Enable support for AWS RDS
  --help                Show this help message and exit.
  --debug               Enable debug mode for traceback tracking.

  Display Options, you can exclude some columns by using them :
    --no-database       Disable DATABASE.
    --no-user           Disable USER.
    --no-client         Disable CLIENT.
    --no-cpu            Disable CPU%.
    --no-mem            Disable MEM%.
    --no-read           Disable READ/s.
    --no-write          Disable WRITE/s.
    --no-time           Disable TIME+.
    --no-wait           Disable W.

Conclusion: pg_activity is small but very useful tool for monitoring an PostgreSQL instance. In the next posts I’ll look into some more feature rich monitoring solutions that are around for monitoring PostgreSQL instances.

Let’s work together

Ready to operate at
the next level?

Tell us about your challenge. We’ll bring the depth, the expertise, and the Swiss precision.

Open source Team

Open source Team

Posts published under the Open source Team account are collective contributions by the dbi services open infrastructure consultants. The practice covers open-source databases and platforms, PostgreSQL first of all, along with MySQL, MariaDB, and Linux, from architecture, replication, and high availability to performance tuning and operations under service level agreements, and contributes actively to the PostgreSQL community.View Author posts

721 posts since September 2011

Most read in Database

The Database posts our readers open most: the all-time favourites, and what is trending right now.

Leave a Comment

Your email address will not be published. Required fields are marked *