Incident brief
Sequence reached its maximum value
nextval() fails once a non-cycling sequence hits its MAXVALUE; a sequence capped at 2 raised the limit error on the third call.
What lands in your log
ERROR: nextval: reached maximum value of sequence "order_seq" (2)
In 10 seconds
- What triggers it
- Create or reach a sequence with a low MAXVALUE and no CYCLE.
- Fix
- Raise the sequence's MAXVALUE (or move the column and sequence to bigint) so it has room to grow.
- Proof
- Reproduced on PostgreSQL 18.4 → The reproduction exhausts a MAXVALUE-2 sequence: nextval returns 1 then 2, and the third call raises SQLSTATE 2200H.
Fix
What to do right now
Application-level steps for this error.
- Raise the sequence's MAXVALUE (or move the column and sequence to bigint) so it has room to grow.
- Add CYCLE only if wrapping is acceptable and duplicate keys are impossible.
-- Raise the ceiling so the sequence can advance again.
ALTER SEQUENCE order_seq MAXVALUE 1000000;
SELECT nextval('order_seq');For this error
See this error live on the server
Run these against the affected instance to confirm the diagnosis before you act.
Inspect sequence bounds and current state before raising MAXVALUE or migrating the owning column type.
Sequences at or near their maximum
Compare last_value with max_value/increment; cycle=false sequences stop at the ceiling.
SELECT schemaname, sequencename, data_type,
last_value, increment_by, max_value, cycle,
max_value - last_value AS remaining_values
FROM pg_sequences
WHERE last_value IS NOT NULL
ORDER BY remaining_values
LIMIT 50;Why it happens
What PostgreSQL is telling you
The mechanism behind the error, grounded in the official manual, not paraphrased.
PostgreSQL 18 Documentation, Appendix A. PostgreSQL Error Codes (Table A.1, Class 22, Data Exception)
2200H → sequence_generator_limit_exceededRead the full section on postgresql.org →
Calling nextval past the sequence maximum
The sequence was capped at 2 with no CYCLE, so the third nextval raises 'nextval: reached maximum value of sequence order_seq (2)'.The session continues normally
Exhausting the sequence failed as a single statement, so session B's next query proceeds without any transactional cleanup.Reproduce & verify
A real, single-session PostgreSQL reproduction
A literal transcript of SQL run against a live PostgreSQL instance in an isolated lab. The commands below are exactly what was executed.
- 1Create or reach a sequence with a low MAXVALUE and no CYCLE.
- 2Call nextval() until it returns the maximum value (here 2).
- 3The next nextval() aborts with SQLSTATE 2200H: 'reached maximum value of sequence'.
An id sequence backed by a too-small domain hit its ceiling in production and every insert began failing.
DROP SEQUENCE IF EXISTS order_seq;
CREATE SEQUENCE order_seq START 1 MAXVALUE 2;SELECT nextval('order_seq');
SELECT nextval('order_seq');
SELECT nextval('order_seq');
SELECT 'ok' AS session_after_error;SELECT 'ok' AS session_after_error;What PostgreSQL actually returned
CREATE SEQUENCE nextval
---------
1
(1 row)
nextval
---------
2
(1 row)
ERROR: nextval: reached maximum value of sequence "order_seq" (2) session_after_error
---------------------
ok
(1 row)The sequence advances again once its ceiling is raised.
Without this
Before: the sequence is stuck at its maximum
With this, tested
After: a higher MAXVALUE lets nextval advance
- A second operational test: exact SQL, raw output, measured result, and engineer notes
Card required. Cancel before day 7 and you are not charged.
Runbook to fix this
The runbook for this incident
Full step-by-step fixes for the condition behind this error: the diagnosis, the exact SQL, and output captured in the lab.
Connected
Everything this error touches
Every page this SQLSTATE connects to: the concept that explains it, the runbooks that fix it, the parameters you tune to prevent it, and the sibling errors it travels with. All real cross-references. Jump straight in, or open the full interactive map.
Fix it — runbooks
Related errors
Verification
- Last verified
- 2026-07-24 (isolated lab, PostgreSQL 18.4)
- Verification scope
- Verified against PostgreSQL 18.4 in an isolated lab environment
- Audit status
- reviewed
Went further?
Pro unlocks the second lab proof
Free page stops the bleeding. Pro adds the operational test, SQLSTATE audit, and deeper evidence, same error, more certainty.