CREATE SEQUENCE (Create Sequence)
Last updated: 2026-11-04
CREATE SEQUENCE (Create Sequence)
CREATE SEQUENCE creates a standalone generator of integers, read with nextval(). A sequence controls its start value, increment, bounds, cache, and whether it cycles. Using nextval() as a column default is equivalent to a SERIAL or IDENTITY column, but a standalone sequence is more flexible: it can exist apart from any table, be shared by several tables, and be retuned on its own after a data repair.
| Kind | command |
|---|---|
| Example Language | SQL |
📝 Syntax
CREATE SEQUENCE [IF NOT EXISTS] <sequence name>
[AS <data type>]
[INCREMENT [BY] <step>]
[MINVALUE <min> | NO MINVALUE]
[MAXVALUE <max> | NO MAXVALUE]
[START [WITH] <start>]
[CACHE <cache size>]
[CYCLE | NO CYCLE]
[OWNED BY <table>.<column> | NONE]
⚙️ Parameters
| INCREMENT BY | How much the value advances per call, 1 by default |
|---|---|
| START WITH | The first value handed out |
| MINVALUE / MAXVALUE | Lower and upper bounds |
| CACHE | How many values are pre-fetched into the session |
| CYCLE | Wraps around to the minimum after the maximum |
| OWNED BY | Binds the sequence to a column |
Returns:No return value. Once it succeeds the sequence can be read with nextval().
▶ 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 3
id | title
---+-------
10 | First
15 | Second
20 | Third
📌 Key Points
- Values handed out by nextval() are not rolled back with the transaction — sequences deliberately do not roll back, which avoids numbering clashes under concurrency.
- CACHE N pre-fetches N values into the session, trading a gap in the numbering for throughput.
- With CYCLE the sequence returns to MINVALUE after MAXVALUE; without it, a call past the maximum raises an error.
- OWNED BY ties the sequence to a column, so dropping the table drops the sequence too.
- A sequence is an independent object: several tables may share one, and it need not be attached to a table at all.
- AS integer limits the sequence to 32 bits, saving space compared with the bigint default.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QCan a sequence hand out the same value twice?
ANot for a monotonic sequence. Even after a rollback, a value already handed out is never issued again — which is exactly why sequences do not roll back.
QShould I use SERIAL or CREATE SEQUENCE?
ASERIAL/IDENTITY is enough for a plain auto-increment column; use a standalone sequence when the numbering must be shared across tables, needs a custom step, or must exist apart from any table.