Sub topic 15
Dashboard and Statistics tabs
This chapter assumes you can register a server in pgAdmin 4, browse its object tree, and run queries in the Query Tool. It also assumes you know basic SQL. You will learn what the Dashboard and Statistics tabs show, which PostgreSQL views feed them, and how to read the same numbers with SQL, so you can check them in either tool.
Where the numbers come from
pgAdmin does not measure anything itself. Both tabs run queries against PostgreSQL's cumulative statistics views, which belong to the pg_catalog schema and are filled in by the server. Two points follow from this:
- The counters are kept by the server, not by pgAdmin. A number you see in pgAdmin matches what
psqlshows for the same view. - Most counters are cumulative since the last statistics reset or server restart. A single value tells you little. A change between two readings tells you more.
The main views behind the tabs are listed below.
| What you see | Underlying view | Scope |
|---|---|---|
| Sessions graph, session list | `pg_stat_activity` | Server |
| Transactions per second, tuples in and out | `pg_stat_database` | One database |
| Block I/O graph | `pg_stat_database` | One database |
| Locks list | `pg_locks` | Server |
| Per-table statistics | `pg_stat_user_tables`, `pg_statio_user_tables` | Tables |
| Per-index statistics | `pg_stat_user_indexes`, `pg_statio_user_indexes` | Indexes |
The Dashboard tab
Click a server or a database in the object browser and open the Dashboard tab. The dashboard is scoped to what you selected. A server-level dashboard reports on the whole server. A database-level dashboard filters the database-scoped graphs to that database.
Graphs
The top of the page shows time-series graphs that refresh every few seconds. The refresh interval is configurable in the dashboard preferences.
- Server sessions shows total, active and idle connections. A rising "total" line that approaches
max_connectionsmeans new connections will soon fail. - Transactions per second shows commits, rollbacks and total. A high rollback rate often points to application errors or failed statements.
- Tuples in and Tuples out show rows inserted, updated and deleted, and rows fetched, returned and read.
- Block I/O shows blocks read from disk and blocks found in shared buffers (hits).
The transaction and tuple graphs are rates. pgAdmin samples the cumulative counters and plots the difference between samples.
Activity panels
Below the graphs are tabbed panels:
- Sessions lists rows from
pg_stat_activity: process ID, user, application, client address, state, the current or last query, and how long it has been running. - Locks lists rows from
pg_locks, with the lock type, mode and whether it was granted. - Prepared Transactions lists transactions left in the two-phase commit state.
- Configuration shows the current server settings.
The Sessions panel lets you cancel a query or terminate a session. These actions call pg_cancel_backend and pg_terminate_backend.
Reproducing the Sessions panel in SQL
You can get the same information in psql or the Query Tool:
SELECT pid,
usename,
application_name,
state,
now() - query_start AS running_for,
left(query, 60) AS query
FROM pg_stat_activity
WHERE datname = current_database()
AND pid <> pg_backend_pid()
ORDER BY query_start;The filter pid <> pg_backend_pid() removes your own session. The state column takes values such as active, idle and idle in transaction. A session that stays idle in transaction holds locks and can block other work, so it deserves attention.
The Statistics tab
Select a table, index or database in the object browser and open the Statistics tab. It shows a grid of counters for the selected object kind. When you select a database, it lists values for that database. When you select a table node, it lists values for that table.
For a table, the useful columns come from pg_stat_user_tables and pg_statio_user_tables:
| Column | Meaning |
|---|---|
| Sequential scans | Times the whole table was read row by row |
| Sequential tuples read | Rows read by those scans |
| Index scans | Times an index was used |
| Tuples inserted, updated, deleted | Row changes since the last reset |
| Live tuples, dead tuples | Estimated visible rows and rows awaiting cleanup |
| Last vacuum, last autovacuum | When cleanup last ran |
| Last analyze, last autoanalyze | When planner statistics were last refreshed |
| Heap blocks read, heap blocks hit | Disk reads and shared-buffer hits for the table |
Reading the table statistics
Look for these patterns:
- Many sequential scans on a large table with few index scans. A query may lack a useful index. Confirm with
EXPLAIN, as shown in the previous chapter. - A high dead tuple count. Updates and deletes leave old row versions behind until vacuum removes them. If autovacuum is not keeping up, tables and indexes grow (bloat).
- A stale last analyze time on a table that changes often. The planner may choose poor plans because its row estimates are out of date.
- A low hit ratio. If heap blocks read greatly exceed hits, the working set may not fit in memory.
The same data in SQL
SELECT relname,
seq_scan,
idx_scan,
n_live_tup,
n_dead_tup,
last_autovacuum,
last_autoanalyze
FROM pg_stat_user_tables
ORDER BY n_dead_tup DESC
LIMIT 10;To compute a cache hit ratio for the current database:
SELECT datname,
blks_hit,
blks_read,
round(100.0 * blks_hit / nullif(blks_hit + blks_read, 0), 2) AS hit_pct
FROM pg_stat_database
WHERE datname = current_database();The nullif call avoids a division by zero on a database with no reads yet. Remember that blks_hit counts hits in PostgreSQL's shared buffers only. A block counted as a read may still come from the operating system's file cache, so a lower hit ratio does not always mean slow disk access.
To find indexes that have never been used since the last reset:
SELECT schemaname, relname, indexrelname, idx_scan
FROM pg_stat_user_indexes
WHERE idx_scan = 0
ORDER BY relname;An unused index costs write time and disk space. Before dropping one, check that it does not enforce a unique or primary key constraint, and that the statistics cover a long enough period, including rare jobs such as month-end reports.
Common errors and gotchas
- Empty or zero statistics after a restart or crash. After a crash recovery, counters can be reset. Run
ANALYZEand give the workload time to produce new numbers. - Missing graphs or panels. Some panels need permissions. A role without
pg_monitor(or superuser rights) sees only its own sessions inpg_stat_activity, with the query text hidden for other users. Grantpg_monitorto a monitoring role instead of using a superuser. - Estimated counts.
n_live_tupandn_dead_tupare estimates. UseSELECT count(*)when you need an exact row count. - Stats reset.
pg_stat_reset()clears counters for the current database. Do not run it on a shared server without telling others, since it also erases the history that helped you find problems. - Dashboard overhead. Each open dashboard polls the server. A short refresh interval on many open tabs adds load. Lengthen the interval on busy servers.
Exercises
Exercise
The Statistics tab for the table orders shows 80,000 sequential scans, 12 index scans, and 450,000 dead tuples against 500,000 live tuples. Name two things you would check next, and one SQL query for each.
Show answer
First, check why queries scan the table sequentially: run EXPLAIN (ANALYZE, BUFFERS) on the common query, such as SELECT * FROM orders WHERE customer_id = 42; inside a transaction that you roll back, and look for a "Seq Scan" node. Second, check autovacuum: run SELECT relname, n_dead_tup, last_autovacuum FROM pg_stat_user_tables WHERE relname = 'orders'; to see when cleanup last ran. A very high dead tuple ratio suggests autovacuum is not keeping up or is blocked by a long-running transaction. You can find such transactions with the pg_stat_activity query above, looking for idle in transaction sessions.
Exercise
Why is a single reading of the "Tuples out" graph or a counter such as idx_scan hard to interpret, and what should you do instead?
Show answer
These counters are cumulative since the last statistics reset or restart, so one value mixes all past activity. Take two readings some time apart and compare the difference, or watch the graph, which plots the rate between samples. For a long-lived index check, make sure the observation period covers all regular workloads.