Hands-on Lab·PostgreSQL
PostgreSQL Statistics Views
Cache hit ratio, sequential scans, dead tuples and unused indexes - and a reset that left the table reporting zero rows while holding four hundred thousand.
Monitoring and Operations Guide 41 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
Since PostgreSQL 15 these counters live in shared memory rather than in a separate collector process, and survive a clean shutdown. stats_fetch_consistency controls how they behave inside a transaction.
| 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 you need to know how the database is actually being used. This matters because four views answer four different questions - and a reset does not clear everything you would expect it to.
- reading the whole-database counters from
pg_stat_database - comparing sequential against index scans, per table
- finding the indexes that are earning nothing
- splitting block-level I/O into heap and index reads
- watching a counter move, then resetting and finding what a reset does not touch
Before you start
- The
appdbdatabase with theeventstable and its indexes.
-
The whole-database counters
-
How each table is being read
-
Find the indexes earning nothing
-
Block-level I/O per table
-
Watch a counter move
-
Reset the counters, and find out what a reset does not touch