dataiotech Labs
Scripts Library/postgresql/#10
Script #10
postgresql
Performanceread-onlyMin Version: 9.x

PostgreSQL Tables with Heavy Sequential Scans

Finds tables that read many rows through sequential scans. A first step in identifying missing-index candidates.

#seq-scan#missing-index#pg_stat_user_tables
postgresql · sql
SELECT schemaname, relname, seq_scan, seq_tup_read,
       idx_scan, n_live_tup,
       round(seq_tup_read::numeric / NULLIF(seq_scan, 0)) AS avg_rows_per_seq_scan
FROM pg_stat_user_tables
WHERE seq_scan > 0 AND n_live_tup > 10000
ORDER BY seq_tup_read DESC
LIMIT 20;
Operational Notes & Remediation Guidance

Sequential scans on small tables are normal. Before adding an index, verify query execution plans using `EXPLAIN (ANALYZE, BUFFERS)`.

Production Safety & Execution Standards
  • This script is designed for read-only diagnostics; it does not alter schema or modify data directly.
  • Prior to executing on production systems under high load, verify the target database version (Min. Version: 9.x).
  • Exercise caution with mutating actions (e.g., terminating sessions or dropping indexes) referenced in the notes section.

Engineering Triad: Related Runbooks & Incident Response

Which production incidents and emergency drills execute this script as a diagnosis step?

Runbook Library (63)
Proactive Maintenance & Health Check: This script is not tied to an active incident alert; it is intended for scheduled maintenance, capacity planning, and routine DBA audits.