aurora-wait-diagnostics-and-blue-green-check.sql
-- 1. Aggregate cumulative Aurora backend wait events (DataFileRead, XactSync, BufferPin)
SELECT
type_name AS wait_category,
event_name AS wait_event,
waits AS total_wait_count,
round((wait_time / 1000.0)::numeric, 2) AS total_wait_ms,
round((wait_time::numeric / NULLIF(waits, 0) / 1000.0), 3) AS avg_wait_ms
FROM aurora_stat_backend_waits()
WHERE event_name IN ('DataFileRead', 'XactSync', 'BufferPin', 'BufferContent', 'WALWrite')
ORDER BY wait_time DESC;
-- 2. Identify live queries waiting on Aurora storage reads (IO:DataFileRead) or commit sync
SELECT
pid,
usename,
application_name,
wait_event_type,
wait_event,
round(extract(epoch FROM (now() - query_start))::numeric, 2) AS runtime_sec,
left(query, 90) AS sql_snippet
FROM pg_stat_activity
WHERE state = 'active'
AND wait_event_type IN ('IO', 'LWLock', 'BufferPin')
ORDER BY query_start ASC;
-- 3. Find top queries generating physical storage block reads (Aurora Standard I/O drivers)
SELECT
queryid,
calls,
shared_blks_read,
round(100.0 * shared_blks_hit / NULLIF(shared_blks_hit + shared_blks_read, 0), 2) AS hit_pct,
round(mean_exec_time::numeric, 2) AS mean_ms,
left(query, 85) AS query_sample
FROM pg_stat_statements
WHERE shared_blks_read > 10000
ORDER BY shared_blks_read DESC
LIMIT 10;
-- 4. Verify Aurora cluster writer/reader LSN durability and lag before Blue/Green switchover
SELECT
server_id,
CASE WHEN session_id = 'MASTER_SESSION_ID' THEN 'WRITER' ELSE 'READER' END AS role,
highest_lsn_written,
durable_lsn,
round(replica_lag_in_msec::numeric, 2) AS replica_lag_ms
FROM aurora_replica_status()
ORDER BY role DESC, server_id;