dataiotech Labs
Scripts Library/postgresql/#20
Script #20
postgresql
Monitoringread-onlyMin Version: 10

Check PostgreSQL Sequences for Overflow Risk

Shows how close each sequence is to its data type limit. A sequence that hits the integer limit causes a production outage.

#sequence#overflow#integer
postgresql · sql
SELECT schemaname, sequencename, data_type, last_value, max_value,
       round(100.0 * last_value / max_value, 2) AS pct_used
FROM pg_sequences
WHERE last_value IS NOT NULL
ORDER BY pct_used DESC
LIMIT 20;
Operational Notes & Remediation Guidance

Alert when a sequence reaches 80% of its maximum value. Migrate `integer` primary keys to `bigint` before wraparound occurs.

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: 10).
  • 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.