Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

Which database metrics would you put on the first dashboard for a PostgreSQL database behind a Spring Boot service, and why?

The first dashboard should answer two questions fast: is the database the reason the service is slow, and is it about to run out of something. So we show top query latency and throughput from pg_stat_statements, HikariCP pool metrics from Micrometer, connections against max_connections and sessions idle in transaction, cache hit ratio, disk I/O and free storage, replication lag and WAL rate, and locks, deadlocks, autovacuum and dead tuples.

Why this question is asked

When a Spring Boot service gets slow, the first argument in the incident channel is usually whether the database is to blame. A good first dashboard ends that argument in a minute, and a bad one shows forty graphs of things that never change. Interviewers ask this question to see whether we understand which numbers actually move during real problems, and why. In this answer, we will go through the dashboard row by row: the application's view through HikariCP, the queries, connections and sessions, resources and storage, replication and WAL, and finally locks and vacuum, explaining for each why it earns its place.

1Start With the Application's View: the Connection Pool

A Spring Boot service talks to PostgreSQL through a connection pool, by default HikariCP. A pool keeps a fixed number of open database connections and lends them to request threads. Spring Boot publishes the pool's state through Micrometer, the metrics library it uses, under names such as hikaricp.connections.active, hikaricp.connections.idle, hikaricp.connections.pending and hikaricp.connections.acquire.

These four numbers are the first row of the dashboard because they show the database from the service's point of view. Active is how many connections are lent out right now, idle is how many are waiting to be used, pending is how many threads are waiting for a connection, and acquire time is how long they waited. If pending is above zero and acquire time climbs, requests are queueing for the database even before any SQL runs. Notice that this can happen while the database itself is quiet, for example when code holds connections during slow HTTP calls.

YAML
# application.yml: expose metrics for Prometheus
management:
endpoints.web.exposure.include: health,prometheus
metrics.tags.application: orders-service
# Example series on /actuator/prometheus
# hikaricp_connections_active{pool="HikariPool-1"} 8.0
# hikaricp_connections_pending{pool="HikariPool-1"} 0.0
# hikaricp_connections_acquire_seconds_max{pool="HikariPool-1"} 0.002

2Top Queries From pg_stat_statements

pg_stat_statements is a PostgreSQL extension that records, for every distinct query shape, how many times it ran, the total and mean execution time, and the rows returned. It must be listed in shared_preload_libraries and created with CREATE EXTENSION. On most managed services it is available and often enabled by default.

The dashboard shows throughput (calls per second) and latency (mean time) for the top ten queries by total time. Total time matters more than mean time: a 2 ms query that runs 5,000 times per second costs far more than a 2 s report that runs once an hour. When latency rises, this panel tells us whether one query got slower (often a changed plan or a missing index) or everything got slower (often a resource problem).

SQL
SELECT left(query, 60) AS query, calls,
round(mean_exec_time::numeric, 2) AS mean_ms,
round(total_exec_time::numeric) AS total_ms
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 5;
-- query | calls | mean_ms | total_ms
-- SELECT * FROM orders WHERE customer_ | 9120334 | 1.84 | 16781414
-- UPDATE stock SET qty = qty - $1 WHER | 411220 | 12.40 | 5099128

3Connections and Sessions From pg_stat_activity

PostgreSQL has a hard limit, max_connections, and each connection is a separate server process using memory. If several services each have a pool of 20 and run 10 instances, they can try to open 200 connections or more. So the dashboard shows connections in use against max_connections, grouped by state.

The state that deserves its own panel is idle in transaction. It means a session has started a transaction, run some statements, and is now doing nothing while the transaction stays open. Such a session holds its locks and its pool connection, and it stops vacuum from cleaning up rows newer than its snapshot. A count above a few, or any such session older than a minute, usually points to application code that does slow work inside @Transactional methods. We also show the age of the oldest running query and the oldest transaction, because one forgotten transaction can cause trouble across the whole database.

SQL
SELECT state, count(*), max(now() - xact_start) AS oldest_xact
FROM pg_stat_activity
WHERE backend_type = 'client backend'
GROUP BY state;
-- state | count | oldest_xact
-- active | 6 | 00:00:00.41
-- idle | 74 |
-- idle in transaction | 3 | 00:04:12.9 <- worth investigating

4Resources and Storage

The next row is about the machine. The cache hit ratio is the share of block reads served from PostgreSQL's shared buffers rather than from disk, computed from blks_hit and blks_read in pg_stat_database. For an OLTP database it usually sits above 99 percent; a drop means the working set no longer fits in memory and disk reads will rise. Next to it we show disk I/O (read and write operations per second, and latency), CPU, and free storage.

Free storage needs a trend line and an alert well before it runs out, because a PostgreSQL server that fills its disk stops accepting writes. On a managed service we also watch any burst credit or IOPS limit, since running out of I/O credits looks like a sudden slowdown with no change in the application.

5Replication, WAL, Locks and Vacuum

WAL, the write-ahead log, is the stream of changes PostgreSQL writes before changing data files; replicas replay it to stay in sync. Replication lag, measured in bytes or seconds on each replica, tells us how stale reads from replicas can be and how much data we could lose on failover. The WAL generation rate shows how much the database is writing; a sudden jump often comes from a bulk update or a migration and explains growing lag and backup size.

The last row covers contention and cleanup. We show waiting locks and the deadlock counter from pg_stat_database, which tell us when sessions block each other. Then autovacuum activity and dead tuples on the largest tables from pg_stat_user_tables. Dead tuples are old row versions left behind by updates and deletes; autovacuum removes them. If it falls behind, tables and indexes bloat and queries slow down gradually, which is the kind of problem that looks mysterious without this panel.

The first dashboard, row by row
RowPanelsQuestion it answers
Pool (Micrometer)active, idle, pending, acquire timeAre requests waiting for a connection?
Queriestop 10 by total time, calls/s, mean msDid one query get slower, or all of them?
Sessionsconnections vs max, idle in transaction, oldest xactAre sessions piling up or held open?
Resourcescache hit ratio, CPU, disk IOPS and latency, free spaceIs the machine the limit?
Replicationlag per replica, WAL bytes/sAre replicas stale, is write volume unusual?
Contentionwaiting locks, deadlocks, dead tuples, autovacuumAre sessions blocking, is cleanup behind?

6Follow-up questions

What does 'idle in transaction' indicate?

It means the session opened a transaction and is not running any statement right now, but has not committed or rolled back. In a Spring application this usually means a @Transactional method is doing something slow that is not SQL, such as calling another service, or a transaction was left open by a bug. While it waits, the session keeps its locks, holds a pool connection and prevents vacuum from removing row versions newer than its snapshot. Setting idle_in_transaction_session_timeout puts a limit on the damage, but the real fix is to keep transactions short.

Why show total time rather than mean time for queries?

Because total time is what the database actually spends. A cheap query that runs constantly can dominate CPU, while a slow query that runs rarely may not matter. Mean time is still useful next to it, to see whether a query's plan got worse.

Is a 99 percent cache hit ratio always good?

It is a reasonable sign for OLTP, but it can hide problems: a database can have a high ratio and still be slow because of locks or a bad plan. It is one panel among several, useful mainly when it drops compared with its own history.

7Conclusion

In this answer, we built the first dashboard for PostgreSQL behind a Spring Boot service. We started with HikariCP's active, idle, pending and acquire time from Micrometer, then the top queries from pg_stat_statements, connections against max_connections and idle in transaction sessions from pg_stat_activity, cache hit ratio, disk I/O and free storage, replication lag and WAL rate, and finally locks, deadlocks, dead tuples and autovacuum. Each row answers a question an on-call engineer actually asks.

What to say in the interviewAbout 70 seconds aloud

Open with

I would build it so the on-call engineer can answer two questions in a minute: is the database why the service is slow, and is it running out of anything.

Then, if they want more

The first row is the HikariCP pool from Micrometer: active, idle, pending threads and connection acquire time, because pending above zero means requests are queueing for a connection even before SQL runs.

Next, the top ten queries by total time from pg_stat_statements with calls per second and mean latency, so I can tell whether one query regressed or everything slowed down. Then connections used against max_connections and sessions by state from pg_stat_activity, especially idle in transaction, which means application code is holding a transaction open, keeping locks and blocking vacuum.

Then resources: cache hit ratio, CPU, disk IOPS and latency, and free storage with a trend, because a full disk stops writes. Then replication lag per replica and WAL generation rate. And finally waiting locks, deadlocks, dead tuples on the big tables and autovacuum activity, which explain gradual slowdowns. I would keep it to those panels so it stays readable.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Which database metrics would you put on the first dashboard for a PostgreSQL database...": https://scrivio-interview-library.netlify.app/databases/database-operations/6b45796d70/