Hands-on Lab·PostgreSQL
PostgreSQL pg_stat_activity and Session Control
The first view anyone opens on a busy server - and the one that quietly lies to you. As a non-superuser it showed ten of eleven sessions as nothing but a pid.
Monitoring and Operations Guide 38 of 47 Intermediate
- OSUbuntu 26.04 LTS
- PostgreSQL18.6-0ubuntu0.26.04.1
- Host2 core / 4 GB, 400,000-row test table
- TimeAbout 17 min
io worker appears in backend_type from PostgreSQL 18, which introduced asynchronous I/O. On 17 and earlier you will see three fewer background processes here.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| db-a01 | 192.168.0.81 | Ubuntu 26.04 LTS | Primary / Source / Replica Set Member 1 | 2 Core | 4 GB | 50 GB |
This guide includes
Use this when a server is busy and you need to know what it is doing. This matters because pg_stat_activity quietly hides most of it from a non-superuser - and cancelling a query is not the same thing as terminating a session.
- looking at every session on the server, one row per backend
- finding out why most of it came back blank
- watching a query while it runs, then cancelling it
- spotting a transaction still holding on after its work is finished
- terminating it, and seeing exactly how that differs from a cancel
Before you start
- A running cluster with the
appdbdatabase. - Shell access as a user who can
sudo -u postgres.
-
Look at every session on the server
-
Find out why most of it was blank
-
Watch a query while it runs
-
Cancel it
-
Spot a transaction holding on
-
Terminate it, and see the difference