ALTER SEQUENCE (Alter Sequence)
Last updated: 2026-11-04
ALTER SEQUENCE (Alter Sequence)
ALTER SEQUENCE retunes an existing sequence: changing its step, bounds, or cache, restarting its counter, or binding it to a column. It is the tool for resetting numbering after a data repair, or for leaving gaps between batches of ids.
| Kind | command |
|---|---|
| Example Language | SQL |
📝 Syntax
ALTER SEQUENCE [IF EXISTS] <sequence name>
[AS <data type>]
[INCREMENT [BY] <step>]
[MINVALUE <min> | NO MINVALUE]
[MAXVALUE <max> | NO MAXVALUE]
[START [WITH] <start>]
[RESTART [WITH] <restart value>]
[CACHE <cache size>]
[CYCLE | NO CYCLE]
[OWNED BY <table>.<column> | NONE]
⚙️ Parameters
| RESTART WITH | Sets the next value to be issued |
|---|---|
| INCREMENT BY | Changes the increment step |
| MINVALUE / MAXVALUE | Adjusts the value range |
| OWNED BY | Binds the sequence to a column, or NONE to unbind |
Returns:No return value. Changes take effect for subsequent nextval() calls right away.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
DROP TABLE
DROP SEQUENCE
CREATE SEQUENCE
CREATE TABLE
INSERT 0 1
ALTER SEQUENCE
ALTER SEQUENCE
INSERT 0 1
id | title
----+-------------
1 | before alter
100 | after alter
📌 Key Points
- ALTER SEQUENCE ... RESTART WITH n sets the next value to be issued; values already handed out are unaffected.
- After INCREMENT BY changes the step, the next value follows the new step.
- When changing MINVALUE or MAXVALUE the current value must fall inside the new range, otherwise the statement fails.
- OWNED BY NONE detaches the sequence from its column so it can live on its own.
- Sequence parameter changes do not roll back either.
- Values already taken via nextval() — including any cached ones — are never returned by a parameter change.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QHow do I reset a sequence back to 1?
ARun ALTER SEQUENCE seq_name RESTART WITH 1; — the next value issued is 1, while previously issued numbers are not reused.
QDoes changing the increment scramble existing numbers?
ANo. Numbers already issued stay as they are; only values issued from that point on follow the new step.