PG
PRO
2200HERRORTier 1 — Safe✅ HIGH confidence

sequence generator limit exceeded

Category: Data ExceptionVersions: All Postgres versions

🔴 Production Risk Error

High: once the sequence is exhausted every INSERT to the table fails until the sequence is altered or the key type is widened.

What this means

SQLSTATE 2200H is raised when a sequence generator has reached its maximum (or minimum for descending sequences) value and cannot produce another value without cycling, and the sequence was created with NO CYCLE.

Why it happens

  1. 1A SERIAL, BIGSERIAL, or manually created SEQUENCE has exhausted all values in its range
  2. 2nextval() called on a sequence with MAXVALUE already reached and NO CYCLE set

How to reproduce

Sequence exhausted — all values consumed.

trigger — this will ERROR
CREATE SEQUENCE tiny_seq MINVALUE 1 MAXVALUE 3 NO CYCLE;
SELECT nextval('tiny_seq'); -- 1
SELECT nextval('tiny_seq'); -- 2
SELECT nextval('tiny_seq'); -- 3
SELECT nextval('tiny_seq'); -- ERROR: 2200H
ERROR: nextval: reached maximum value of sequence "tiny_seq" (3)

Fix 1: Migrate the primary key column from INTEGER/SERIAL to BIGINT/BIGSERIAL

When an integer PK sequence is nearing exhaustion.

fix
ALTER TABLE orders ALTER COLUMN id TYPE BIGINT;
ALTER SEQUENCE orders_id_seq MAXVALUE 9223372036854775807;

Why this works

BIGINT sequences have a range of ~9.2 quintillion and are practically inexhaustible for typical workloads.

Fix 2: Reset or recycle the sequence with CYCLE

When sequence values are not used as unique identifiers and cycling is acceptable.

fix
ALTER SEQUENCE my_seq CYCLE RESTART;

Why this works

WITH CYCLE causes the sequence to wrap back to MINVALUE after reaching MAXVALUE. Use only when duplicate values are safe.

What not to do

Enable CYCLE on a primary key sequence

Why it's wrong: Cycling causes sequence values to repeat, which violates uniqueness on primary key columns and causes 23505 errors.

Dangerous variant

⚠️ Warning

2200H on a production SERIAL primary key is a live outage — all INSERTs fail

Sources

📚 Official docs: https://www.postgresql.org/docs/current/errcodes-appendix.html

📚 Feature docs: https://www.postgresql.org/docs/current/sql-createsequence.html

🔧 Source ref: Class 22 — Data Exception

📖 Further reading: PostgreSQL Sequences

Confidence assessment

✅ HIGH confidence

Standard SQLSTATE for sequence exhaustion. A well-known production pitfall for integer SERIAL primary keys on high-write tables.

See also

📄 Reference pages

CREATE SEQUENCESERIALBIGSERIALALTER SEQUENCE
⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →