Sub topic 1
Why tables need generated IDs
Prerequisites
This chapter assumes you know basic SQL: CREATE TABLE, INSERT, and SELECT. You should understand what a primary key is and know the integer types smallint, integer, and bigint. The examples run on PostgreSQL 10 or later, because identity columns arrived in that release.
Why tables need generated IDs
Each row needs a value that tells it apart from every other row. A primary key is a column, or set of columns, whose values must be unique. A surrogate key is a primary key with an artificial value, such as 1, 2, 3, that has no business meaning. Surrogate keys stay stable when a customer changes their name or email address. Assigning them by hand is error-prone, so the database should generate them.
Sequences: the mechanism underneath
A sequence is a database object that hands out numbers one at a time. PostgreSQL provides three functions for working with one:
nextval('name')returns the next value and advances the sequence.currval('name')returns the value thatnextvallast returned in the current session.setval('name', n)sets the sequence to a chosen value.
CREATE SEQUENCE demo_seq;
SELECT nextval('demo_seq'); -- returns 1
SELECT nextval('demo_seq'); -- returns 2A sequence is separate from any table. Its values are not rolled back when a transaction fails, so gaps can appear. A later chapter covers gaps in detail.
The older serial approach
Before identity columns, PostgreSQL offered the serial pseudo-type. Writing serial in a column definition creates an integer column, a sequence, and a default value that calls nextval.
CREATE TABLE customers_old (
id serial PRIMARY KEY,
name text NOT NULL
);This works, but serial is not part of the SQL standard. The sequence is a separate object that someone can drop or change by mistake. You will still find serial in older code.
Creating an identity column
An identity column is the standard SQL way to attach a sequence to a column. The database owns the sequence and fills the column automatically.
CREATE TABLE customers (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text NOT NULL
);
INSERT INTO customers (name) VALUES ('Ada') RETURNING id;RETURNING id sends the generated value back in the same statement, so your application does not need a second query.
GENERATED ALWAYS vs GENERATED BY DEFAULT
The two modes differ in how they treat a value you supply yourself.
| Mode | Explicit value in INSERT | Typical use |
|---|---|---|
| GENERATED ALWAYS | Rejected with an error | The database controls every ID |
| GENERATED BY DEFAULT | Accepted, and the sequence is skipped | Data loads that bring their own IDs |
Exercise
A table has id integer GENERATED ALWAYS AS IDENTITY. You run INSERT INTO customers (id, name) VALUES (50, 'Grace'). What happens, and which mode would let this succeed without extra clauses?
Show answer
The insert fails with an error because ALWAYS rejects explicit values. GENERATED BY DEFAULT would accept the value 50 without any extra clause.