pg_activity - Live Monitoring des EDB Postgres Advanced Server
In diesem Blogbeitrag möchte ich die per Github frei erhältliche Befehlszeilen Monitoring Anwendung pg_activity von dalibo vorstellen. Es handelt sich dabei, ähnlich dem unter Linux bekannten “htop”, um eine Anwendung zum Überwachen von PostgreSQL. Dabei werden sowohl Speicherverbrauch bzw. -nutzung als auch SQL Query spezifische Informationen übersichtlich dargestellt. Mit Hilfe dieser Informationen lassen sich Rückschlüsse auf die aktuelle Auslastung der PostgreSQL Instanz ziehen.
sudo yum install git python3 python3-psycopg2 psutils python36-psutil
Danach wird das pg_activity Git Repository geklont und in das erstellte Verzeichnis gewechselt.
git clone github.com/dalibo/pg_activity.git
Cloning into 'pg_activity'...
remote: Enumerating objects: 18, done.
remote: Counting objects: 100% (18/18), done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 1274 (delta 6), reused 9 (delta 3), pack-reused 1256
Receiving objects: 100% (1274/1274), 1.22 MiB | 384.00 KiB/s, done.
Resolving deltas: 100% (587/587), done.
cd pg_activity/
Die Installation von pg_activity geht schnell vonstatten. Es kann dabei gewählt werden, ob auch keine man pages mit installiert werden sollen.
sudo python3 setup.py install
Oder mit man pages … wie ich es gewählt habe.
sudo python setup.py install --with-man
running install
running bdist_egg
running egg_info
creating pg_activity.egg-info
writing pg_activity.egg-info/PKG-INFO
writing dependency_links to pg_activity.egg-info/dependency_links.txt
writing requirements to pg_activity.egg-info/requires.txt
writing top-level names to pg_activity.egg-info/top_level.txt
writing manifest file 'pg_activity.egg-info/SOURCES.txt'
reading manifest file 'pg_activity.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: manifest_maker: MANIFEST.in, line 1: 'recursive-include' expects <dir> <pattern1> <pattern2> ...
writing manifest file 'pg_activity.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
creating build
creating build/lib
creating build/lib/pgactivity
copying pgactivity/Data.py -> build/lib/pgactivity
copying pgactivity/Process.py -> build/lib/pgactivity
copying pgactivity/UI.py -> build/lib/pgactivity
copying pgactivity/__init__.py -> build/lib/pgactivity
creating build/bdist.linux-x86_64
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/pgactivity
copying build/lib/pgactivity/Data.py -> build/bdist.linux-x86_64/egg/pgactivity
copying build/lib/pgactivity/Process.py -> build/bdist.linux-x86_64/egg/pgactivity
copying build/lib/pgactivity/UI.py -> build/bdist.linux-x86_64/egg/pgactivity
copying build/lib/pgactivity/__init__.py -> build/bdist.linux-x86_64/egg/pgactivity
byte-compiling build/bdist.linux-x86_64/egg/pgactivity/Data.py to Data.cpython-36.pyc
byte-compiling build/bdist.linux-x86_64/egg/pgactivity/Process.py to Process.cpython-36.pyc
byte-compiling build/bdist.linux-x86_64/egg/pgactivity/UI.py to UI.cpython-36.pyc
byte-compiling build/bdist.linux-x86_64/egg/pgactivity/__init__.py to __init__.cpython-36.pyc
installing package data to build/bdist.linux-x86_64/egg
running install_data
copying docs/man/pg_activity.1 -> /usr/share/man/man1
creating build/bdist.linux-x86_64/egg/EGG-INFO
installing scripts to build/bdist.linux-x86_64/egg/EGG-INFO/scripts
running install_scripts
running build_scripts
creating build/scripts-3.6
copying and adjusting pg_activity -> build/scripts-3.6
changing mode of build/scripts-3.6/pg_activity from 644 to 755
creating build/bdist.linux-x86_64/egg/EGG-INFO/scripts
copying build/scripts-3.6/pg_activity -> build/bdist.linux-x86_64/egg/EGG-INFO/scripts
changing mode of build/bdist.linux-x86_64/egg/EGG-INFO/scripts/pg_activity to 755
copying pg_activity.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying pg_activity.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying pg_activity.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying pg_activity.egg-info/requires.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying pg_activity.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist/pg_activity-1.6.1-py3.6.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing pg_activity-1.6.1-py3.6.egg
Copying pg_activity-1.6.1-py3.6.egg to /usr/local/lib/python3.6/site-packages
Adding pg-activity 1.6.1 to easy-install.pth file
Installing pg_activity script to /usr/local/bin
Installed /usr/local/lib/python3.6/site-packages/pg_activity-1.6.1-py3.6.egg
Processing dependencies for pg-activity==1.6.1
Searching for psutil==5.6.7
Best match: psutil 5.6.7
Adding psutil 5.6.7 to easy-install.pth file
Using /usr/lib64/python3.6/site-packages
Finished processing dependencies for pg-activity==1.6.1
pg_activity kann Lokal und Remote ausgeführt werden. Beim lokalen Ausführen sollte der User auch der Postgres User, in diesem Falle enterprisedb, sein. Ansonsten werden nur eingeschränkt Informationen angezeigt. Das Gleiche gilt auch für den Datenbank Superuser.
sudo su - enterprisedb
Beim Ausführen von pg_activity können per Parameter die von Postgres bekannten Verbindungsdetails sowie einige weitere Parameter, z.B. zum Anpassen der Ausgabe, übergeben werden.
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
--output=FILEPATH Store running queries as CSV.
--help Show this help message and exit.
--debug Enable debug mode for traceback tracking.
--no-db-size Skip total size of DB.
--verbose-mode=VERBOSE_MODE
Queries display mode. Values: 1-TRUNCATED,
2-FULL(default), 3-INDENTED
--duration-mode=DURATION_MODE
Duration mode. Values: 1-QUERY(default),
2-TRANSACTION, 3-BACKEND
--min-duration=SECONDS
Don't display queries with smaller than specified
duration (in seconds).
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.
--no-app-name Disable App.
In meiner Demo nutze ich nun pgbench, welches wir bereits vorgestellt hatten, um etwas Last auf der Datenbank Instanz zu erzeugen und einen besseren Eindruck von pg_activity zu vermitteln. Dazu öffne ich ein zweites Terminal, erstelle eine Datenbank ‘pgbench’ mit einer Größe von ca. 756 MB. Zum Schluss starte ich ein Benchmarklauf mit 10 Clients in 2 Threads über eine Dauer von 10 Minuten.
psql -U enterprisedb -c "CREATE DATABASE pgbench;"
pgbench -U enterprisedb -i s 50 pgbench
pgbench -U enterprisedb -c 10 -j 2 -T 600 pgbench
In der ersten Shell kann ich nun pg_activity aufrufen.
pg_activity

Abbildung: pg_activity Monitoring
Solange die Oberfläche aktiviert ist, können per Tastendruck diverse Features aktiviert/deaktiviert, nach Spalten sortiert oder auch die Ansicht geändert werden. Beendet wird pg_activity mit der Taste “q”.