Sub topic 10
Tuning start, increment and limits
This chapter assumes basic CREATE TABLE, INSERT, and primary-key syntax. You should also know that an identity column uses a PostgreSQL sequence to choose its next value.
An identity column does not have to start at 1 or increase by 1. You can configure the sequence when you create the table.
Sequence options
The most useful options are:
| Option | Meaning |
|---|---|
| `START WITH` | The first value produced by the sequence |
| `INCREMENT BY` | The amount added after each generated value |
| `MINVALUE` | The lowest value the sequence may produce |
| `MAXVALUE` | The highest value the sequence may produce |
| `CACHE` | How many values PostgreSQL reserves in advance |
| `CYCLE` | Whether the sequence starts again at its minimum after reaching its maximum |
The start value affects the initial position. The increment affects every later value. For example, a sequence starting at 1000 with an increment of 10 produces 1000, 1010, 1020, and so on.
CREATE TABLE tickets (
id integer GENERATED BY DEFAULT AS IDENTITY
(START WITH 1000
INCREMENT BY 10
MINVALUE 1000
MAXVALUE 2147483647
CACHE 20
NO CYCLE)
PRIMARY KEY,
title text NOT NULL
);
INSERT INTO tickets (title)
VALUES ('first'), ('second')
RETURNING id;The result contains IDs 1000 and 1010.
The MAXVALUE must fit the column type. An integer can store values from -2147483648 through 2147483647. A sequence can be configured with a larger maximum, but inserting that value into an integer column will fail. Use bigint when the table may grow beyond the integer range.
How the sequence advances
The sequence is separate from the table rows. PostgreSQL reserves the next sequence value when an insert requests one. A transaction can later roll back, but the reserved value is not returned to the sequence.
flowchart TD
A[INSERT without an ID] --> B[Identity sequence chooses next value]
B --> C[Value is stored in the row]
C --> D[Next value is incremented]
B --> E[Transaction rolls back]
E --> F[Reserved value is not reused]This means that sequence values are identifiers, not a gapless business counter. Deletes, failed inserts, rollbacks, and cached values can all produce gaps.
Choosing the settings
Use the default START WITH 1 and INCREMENT BY 1 for most surrogate keys. A surrogate key is a generated identifier whose value has no business meaning.
Use a different start value when IDs must avoid a reserved range. Use a larger increment when separate systems generate IDs in distinct numeric ranges. For example, one system might use IDs beginning at 1 and increment by 2, while another uses 2 and increments by 2.
Set MINVALUE and MAXVALUE to document and enforce the intended range. Avoid a small type merely to save storage. A smallint reaches its limit quickly, while bigint provides a much larger range for a small storage cost compared with the data in most rows.
Exercise
A table needs generated IDs beginning at 5000 and increasing by 5. It must stop rather than wrap around. Which identity options express this requirement?
Show answer
Use (START WITH 5000 INCREMENT BY 5 NO CYCLE), and choose a MAXVALUE that fits the column type and the application's allowed range.