Sub topic 5
GENERATED ALWAYS vs GENERATED BY DEFAULT
Assume you know basic CREATE TABLE, INSERT, and SELECT statements, primary keys, and the basic idea of a transaction. This chapter focuses on how PostgreSQL handles values supplied for an identity column during INSERT.
An identity column gets its value from a sequence when the INSERT leaves the column out or uses the DEFAULT keyword. The generation mode controls what happens when the statement supplies an explicit value.
The two generation modes
GENERATED ALWAYS is the strict mode. PostgreSQL normally requires the database to generate the value. An explicit value causes an error unless the statement clearly says that it is intentionally overriding the rule.
GENERATED BY DEFAULT is the flexible mode. PostgreSQL generates a value when no value is supplied, but accepts an explicit value when one is supplied.
| Mode | No value supplied | Explicit value supplied |
|---|---|---|
| `GENERATED ALWAYS` | PostgreSQL generates a value | Rejected unless overridden |
| `GENERATED BY DEFAULT` | PostgreSQL generates a value | Accepted |
Both modes use the same sequence mechanism. The difference is the protection around manually supplied values.
CREATE TABLE always_orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer text NOT NULL
);
CREATE TABLE default_orders (
id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
customer text NOT NULL
);
INSERT INTO always_orders (customer)
VALUES ('Ada')
RETURNING id;
INSERT INTO default_orders (customer)
VALUES ('Grace')
RETURNING id;The first insert into each table asks PostgreSQL to generate the ID. The RETURNING clause gives the generated value back to the application.
When to choose each mode
Use GENERATED ALWAYS when application code should never choose IDs during normal operation. This is a good default for new tables because it prevents accidental collisions and makes ownership clear: the database owns the ID.
Use GENERATED BY DEFAULT when you sometimes load or preserve known IDs. Common cases include importing data from another system, restoring selected rows, or running a migration that must keep existing identifiers.
Overriding the default behavior
With GENERATED ALWAYS, an explicit value is rejected:
INSERT INTO always_orders (id, customer)
VALUES (100, 'Lin');
-- ERROR: cannot insert a non-DEFAULT value into column "id"To deliberately insert that value, use OVERRIDING SYSTEM VALUE:
INSERT INTO always_orders (id, customer)
OVERRIDING SYSTEM VALUE
VALUES (100, 'Lin');OVERRIDING SYSTEM VALUE tells PostgreSQL to accept the value supplied by the statement instead of the value that the identity sequence would generate.
You can also force PostgreSQL to ignore supplied identity values. OVERRIDING USER VALUE is useful when a shared import statement includes an ID column, but the destination database must assign its own IDs:
INSERT INTO default_orders (id, customer)
OVERRIDING USER VALUE
VALUES (500, 'Maya');This inserts the customer while generating a new ID. The supplied 500 is ignored.
Choose ALWAYS for ordinary application inserts and BY DEFAULT when controlled imports are part of the table's lifecycle. In either case, keep a primary key or another uniqueness constraint if IDs must be unique.
Exercise
Which mode should you choose if an application must never provide its own ID, but an administrator occasionally needs to import rows with preserved IDs?
Show answer
Use GENERATED ALWAYS for normal protection, and use OVERRIDING SYSTEM VALUE in the administrator's explicit import statement.