Cookbook recipe

Watch wait events in pg_stat_activity

Applies to PostgreSQL 13–17 Last reviewed May 2026 Grounded in source
Estimated investigation4 min

Scenario

The database is busy but you cannot tell on what. Wait events show whether sessions wait on I/O, locks, or CPU. Diagnose it Aggregate current wait events: SELECT wait_event_type, wait_event, count(*) FROM pg_stat_activity WHERE state='active' GROUP…

Investigation Path

The database is busy but you cannot tell on what. Wait events show whether sessions wait on I/O, locks, or CPU.

Diagnose it

Aggregate current wait events:

SELECT wait_event_type, wait_event, count(*)
FROM pg_stat_activity
WHERE state='active'
GROUP BY 1,2 ORDER BY 3 DESC;

Why it happens

Each active backend reports what it is waiting on. A flood of IO/DataFileRead means disk-bound; Lock means contention; LWLock points at internal contention; mostly NULL means CPU-bound.

This is a Pro lesson

Get every Learning Pathway and cookbook recipe — grounded in PostgreSQL source code, with diagnostics, fixes, and prevention for each topic.

Continue this lesson to learn:

  • How to fix it
  • Prevent it next time
  • Related & next steps
  • All 36 Learning Pathway lessons
  • 170+ cookbook recipes
  • Source-grounded diagnostics & fixes

Secure checkout Cancel anytime Source-grounded

Career Impact

This scenario builds production judgment and operational confidence under pressure.

Open Career Dashboard →

Keep going

Related & next steps

Was this helpful?

← All cookbook recipes