Sub topic 7
Inserting explicit values with `OVERRIDING`
Assume you know CREATE TABLE, INSERT, primary keys, integer types, and basic transactions. This chapter focuses on how PostgreSQL handles an explicitly supplied value for an identity column.
An identity column normally gets its value from an associated sequence. The two generation modes decide whether an explicit value is accepted:
| Identity definition | Explicit value in a normal `INSERT` |
|---|---|
| `GENERATED ALWAYS` | Rejected |
| `GENERATED BY DEFAULT` | Accepted |
Why explicit IDs are sometimes needed
Most application inserts should let PostgreSQL generate the ID. However, explicit IDs are useful when you:
- Load reference data with stable IDs.
- Restore rows from a backup.
- Copy data between databases.
- Preserve identifiers from an older system.
- Insert a row whose ID is specified by an import format.
The OVERRIDING clause makes this choice clear in the INSERT statement.
Overriding `GENERATED ALWAYS`
GENERATED ALWAYS is the stricter mode. PostgreSQL rejects a non-DEFAULT value unless the statement includes OVERRIDING SYSTEM VALUE.
CREATE TABLE orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer text NOT NULL
);
INSERT INTO orders (customer)
VALUES ('Ada')
RETURNING id;
INSERT INTO orders (id, customer)
VALUES (100, 'Grace');
-- ERROR: cannot insert a non-DEFAULT value into column "id"
INSERT INTO orders (id, customer)
OVERRIDING SYSTEM VALUE
VALUES (100, 'Grace');The final statement explicitly says that the supplied value is allowed to override the system-generated value. The row receives ID 100.
OVERRIDING SYSTEM VALUE affects the identity-generation rule. It does not disable the primary key constraint. If ID 100 already exists, the insert still fails with a duplicate-key error.
Forcing generated values
OVERRIDING USER VALUE does the opposite. It tells PostgreSQL to ignore values supplied by the statement and generate identity values instead.
INSERT INTO orders (id, customer)
OVERRIDING USER VALUE
VALUES (500, 'Lin');The inserted row does not use 500. PostgreSQL obtains the ID from the identity sequence.
This clause is useful when an import includes an ID column, but the destination database must assign its own identifiers.
Comparing the clauses
GENERATED BY DEFAULT accepts explicit values without an override clause. The clauses can still document or enforce the intended behavior:
CREATE TABLE tickets (
id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
title text NOT NULL
);
INSERT INTO tickets (id, title)
VALUES (900, 'Imported ticket');
INSERT INTO tickets (id, title)
OVERRIDING USER VALUE
VALUES (901, 'Locally generated ticket');The first row uses 900. The second row ignores 901 and receives the next sequence value.
Use OVERRIDING SYSTEM VALUE sparingly. After inserting explicit high IDs, check that the identity sequence is ahead of the largest stored ID before allowing normal application inserts.
Exercise
Which clause should you use when an import supplies IDs but the destination database must generate its own IDs?
Show answer
Use OVERRIDING USER VALUE.