Skip to content

Sequences past 80% of their range

An integer sequence that reaches its maximum stops the inserts on that table. This catches it months earlier.

PostgreSQL · SQL
SELECT schemaname || '.' || sequencename AS sequence_name,       last_value,       max_value,       ROUND(100.0 * last_value / max_value, 2) AS used_pctFROM pg_sequencesWHERE last_value IS NOT NULL  AND last_value > max_value * 0.8ORDER BY used_pct DESC

This query is part of the "PostgreSQL health" pack: connect the database in DataRunner and install the whole pack at once, with queries and dashboard.

What this query shows

A serial or integer identity column draws its values from a sequence that ends at 2,147,483,647. When it gets there, every INSERT on the table fails with "nextval: reached maximum value of sequence". On a busy table that day arrives with no warning, and the fix under pressure is painful.

This query reads pg_sequences and returns every sequence that is already above 80% of its maximum, most used first. The healthy result is empty. Run it long before the limit, because the fix takes planning.

How to read each column

  • sequence_name: schema and sequence.
  • last_value: the last value handed out by the sequence.
  • max_value: the highest value the sequence can return. For an integer sequence it is 2147483647, for bigint 9223372036854775807.
  • used_pct: last_value divided by max_value, as a percentage.

The rows are only those above 80%, so one row at 83% means you have about a fifth of the range left. Work out your daily insert rate to turn that into a date.

Healthy and worrying numbers

Empty is healthy. The 80% line is a rule of thumb that leaves time to act, not a PostgreSQL limit. At 80% start the plan. At 90% do it this week. Sequences on bigint columns will not show up in your lifetime at any realistic rate, since the range is about 9.2 quintillion.

A table that burns ids fast, such as an event log or a table with many failed inserts, can be near the limit even with few rows, because sequence values are not returned when a transaction rolls back.

What to do about it

Change the column to bigint and the sequence to match. Altering the column type rewrites the table and takes an ACCESS EXCLUSIVE lock, so plan it for a quiet window, or use a copy and swap approach on a very large table.

ALTER TABLE public.events ALTER COLUMN id TYPE bigint;
ALTER SEQUENCE public.events_id_seq AS bigint;

Check the result, and do not forget foreign keys. Any column that references this id must become bigint too.

SELECT sequencename, data_type, max_valueFROM pg_sequencesWHERE sequencename = 'events_id_seq';

Limits of this query

  • last_value is NULL when the sequence has not been used yet or your role lacks the privilege to read it, and those rows are filtered out.
  • It reads the sequence, not the column. A column that is integer but fed by a bigint sequence will still overflow at 2147483647 and this query will not flag it.
  • Run it on the primary. A standby can show an older last_value.
  • Sequences set to CYCLE start over instead of failing. That can cause duplicate key errors instead.

Run it on a schedule in DataRunner

Connect the database to DataRunner, paste this query or start from the template, approve the SQL, then pick a schedule and a channel. Daily or weekly is plenty, since a sequence moves slowly. Each run executes the query on your database and delivers the result as CSV, Excel, PDF or inline HTML by e-mail, Slack, Telegram, Discord, WhatsApp (beta), a signed webhook or Google Sheets.

This query returns no rows when everything is fine. A run with no rows can be skipped, so a message only arrives when there is something to look at.

If the database sits in a private network, the bridge agent opens the connection from the inside out, so you do not need to open an inbound port.

Frequently asked questions

What happens when a PostgreSQL sequence reaches its maximum?
The next call to nextval raises the error "reached maximum value of sequence" and the INSERT fails. For a serial column that is 2,147,483,647. Nothing is lost, but the application cannot insert until you widen the sequence and the column.
How do I change a PostgreSQL sequence to bigint?
Run ALTER SEQUENCE name AS bigint, and ALTER TABLE ... ALTER COLUMN id TYPE bigint on the column that uses it. The column change rewrites the table, so plan for the lock. Do the same for foreign key columns.
How do I check the current value of a sequence?
Query pg_sequences, which shows last_value and max_value for each sequence you can access. Avoid calling nextval to look, since that consumes a value.
Why does my sequence have gaps?
Values are handed out even when the transaction rolls back or a conflicting INSERT fails, and sessions can cache values. Gaps are normal and do not matter, but they burn the range faster than the row count suggests.