Sub topic 4
Creating an Identity Column
This chapter assumes that you know basic CREATE TABLE, INSERT, and SELECT statements, and that you understand primary keys and PostgreSQL integer types. You will create a table whose numeric ID PostgreSQL generates automatically.
The identity-column model
An identity column is a table column whose value PostgreSQL generates from an internal sequence. A sequence is a database object that produces numbers, usually one number at a time.
You normally omit the identity column from an INSERT. PostgreSQL obtains the next sequence value and stores it in the row.
The standard syntax places GENERATED ... AS IDENTITY after the column type:
CREATE TABLE orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer text NOT NULL
);
INSERT INTO orders (customer)
VALUES ('Ada');
SELECT id, customer
FROM orders;The first row receives a generated id, such as 1. The exact value depends on the sequence's current state. PRIMARY KEY makes the column both non-null and unique. The identity property alone does not guarantee uniqueness.
Choosing the generation mode
PostgreSQL supports two identity modes:
| Definition | Normal insert with an explicit ID | Typical use |
|---|---|---|
| `GENERATED ALWAYS AS IDENTITY` | Rejected | Application-controlled generated IDs |
| `GENERATED BY DEFAULT AS IDENTITY` | Accepted | Imports, migrations, or occasional assigned IDs |
ALWAYS gives PostgreSQL control unless an insert explicitly overrides that rule. BY DEFAULT uses the sequence when no value is supplied, but accepts a supplied value when one is present.
For most application tables, start with ALWAYS:
CREATE TABLE events (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
event_name text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
INSERT INTO events (event_name)
VALUES ('account-created')
RETURNING id, event_name, created_at;RETURNING sends generated column values back as part of the INSERT statement. This avoids a separate query that might select the wrong row, especially when several clients insert data at the same time.
Common errors
Do not insert NULL or a placeholder such as 0 just to trigger generation. Omit the identity column, or use the DEFAULT keyword:
INSERT INTO events (id, event_name)
VALUES (DEFAULT, 'email-sent')
RETURNING id;Do not assume that identity values form a gapless business number. Sequence values can be consumed by failed or rolled-back transactions, and concurrent inserts can receive values in an order different from the order in which transactions commit.
Choose the column type for the expected lifetime of the table. bigint provides much more space than integer and is a common safe default for high-volume tables. It does not make the values unique by itself, so keep the primary key or another unique constraint.
Exercise
Write an INSERT statement that adds an order for customer "Grace" and returns the generated ID, assuming the table has an identity column named id and a required text column named customer.
Show answer
INSERT INTO orders (customer) VALUES ('Grace') RETURNING id;