SQLNirvana by Pyro

Blog › mysql · postgresql · schema

AUTO_INCREMENT and sequences running out: spot it before inserts fail

An INT primary key tops out at about 2.1 billion. When it runs out, every insert fails. How to check how close you are in MySQL and PostgreSQL, and how to fix it.

It's one of the few database problems that goes from "fine" to "outage" in one second. A table's ID column reaches the largest value its type can hold, and from that moment every INSERT fails. It tends to happen to old, busy tables that were created years ago with INT.

How big is big enough?

TypeSigned maximumUnsigned maximum
TINYINT127255
SMALLINT32,76765,535
MEDIUMINT8,388,60716,777,215
INT2,147,483,6474,294,967,295
BIGINT9.2 × 10^181.8 × 10^19

IDs also get used up faster than rows suggest. Deleted rows don't give their IDs back, failed inserts and INSERT ... ON DUPLICATE KEY UPDATE can consume values, and bulk loads can reserve ranges.

Checking in MySQL

The next value for each table is in information_schema.tables.auto_increment. Join it to the column type to see how much is used:

SELECT c.table_schema, c.table_name, c.column_name, c.column_type, t.auto_increment
FROM information_schema.columns c
JOIN information_schema.tables t ON t.table_schema = c.table_schema AND t.table_name = c.table_name
WHERE c.extra LIKE '%auto_increment%'
  AND c.table_schema NOT IN ('mysql','sys','performance_schema','information_schema')
ORDER BY t.auto_increment DESC;

Compare auto_increment with the maximum for the column type above. In MySQL 8, information_schema.tables can show cached values; ANALYZE TABLE refreshes them, or set information_schema_stats_expiry = 0 for your session.

Checking in PostgreSQL

Identity and serial columns are backed by sequences:

SELECT schemaname, sequencename, data_type, last_value, max_value,
       round(100.0 * last_value / max_value, 1) AS pct_used
FROM pg_sequences
WHERE last_value IS NOT NULL
ORDER BY last_value::float / max_value DESC;

last_value shows as NULL if your user can't read the sequence. A monitoring role needs SELECT on it, which doesn't allow changing it. When a sequence runs out, inserts fail with nextval: reached maximum value of sequence.

When to act

Fixing it in MySQL

Change the column to BIGINT, keeping the signedness and AUTO_INCREMENT:

ALTER TABLE orders MODIFY id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT;

This rebuilds the table and on a large table it takes a long time. Use an online schema change tool such as gh-ost or pt-online-schema-change, which copy the table in the background and swap it in at the end. Remember to change foreign key columns in child tables to the same type.

Fixing it in PostgreSQL

Widen the sequence first, which is instant:

ALTER SEQUENCE orders_id_seq AS bigint;

Then the column itself, which rewrites the table and holds a lock for the duration:

ALTER TABLE orders ALTER COLUMN id TYPE bigint;

For big tables, a common approach is to add a new bigint column, backfill it in batches, keep it in sync with a trigger, and swap it in during a short window.

Prevent it

Use BIGINT for every new ID column. The extra 4 bytes per row are almost never what limits you, and the change later is painful.

SQLNirvana checks every auto-increment column and sequence once a minute and warns at 70% (high) and 85% (critical). The free MySQL health check and PostgreSQL check include it too.

Let SQLNirvana watch it for you

Health, tuning, security and upgrade checks every minute. Free for one server.

Start free