Sub topic 3
The Older `serial` Approach
This chapter assumes you know basic CREATE TABLE, INSERT, and SELECT statements, understand primary keys, and have a basic idea of transactions. It focuses on PostgreSQL's older serial syntax so that you can recognize existing schemas and maintain them safely.
What `serial` means
serial is not a true data type. It is shorthand that PostgreSQL expands into several objects:
- An integer column.
- A sequence that produces the next number.
- A default expression that calls
nextvalon that sequence.
For example, this table uses the serial pseudo-type:
CREATE TABLE customers (
id serial PRIMARY KEY,
name text NOT NULL
);
INSERT INTO customers (name) VALUES ('Ada'), ('Grace');
SELECT * FROM customers;The id column receives values from a sequence when an INSERT does not provide an ID. PRIMARY KEY adds a uniqueness constraint and prevents null values. The serial declaration itself does not guarantee uniqueness.
PostgreSQL provides three common forms:
| Declaration | Underlying integer type | Approximate range |
|---|---|---|
| `smallserial` | `smallint` | -32,768 to 32,767 |
| `serial` | `integer` | -2,147,483,648 to 2,147,483,647 |
| `bigserial` | `bigint` | -9.22 quintillion to 9.22 quintillion |
Choose the form based on the largest value the table may need. Changing from serial to bigserial later requires planning because the column and sequence types must both support the larger range.
Objects created behind the syntax
For a declaration such as id serial, PostgreSQL effectively creates a sequence and assigns a default similar to this:
CREATE SEQUENCE customers_id_seq
AS integer;
CREATE TABLE customers (
id integer NOT NULL DEFAULT nextval('customers_id_seq'::regclass),
name text NOT NULL,
PRIMARY KEY (id)
);
ALTER SEQUENCE customers_id_seq
OWNED BY customers.id;The OWNED BY relationship tells PostgreSQL that the sequence belongs to the column. Dropping the column or table can then remove the associated sequence. The exact generated names and catalog details can vary, so inspect the database rather than relying only on a guessed sequence name.
You can find the sequence associated with a serial column with pg_get_serial_sequence:
SELECT pg_get_serial_sequence('customers', 'id');The sequence has its own state. Calling nextval advances it and returns a value:
SELECT nextval(pg_get_serial_sequence('customers', 'id'));Calling nextval directly consumes a value, even if you do not insert a row with that value.
Serial compared with identity
Identity columns are the newer SQL-standard syntax. They make the generation rule part of the column definition and expose whether explicit values are normally accepted.
A serial column is an ordinary integer column with a sequence-backed default. Because it has a default, an explicit value is normally accepted:
INSERT INTO customers (id, name)
VALUES (100, 'Grace');An identity column can instead be declared GENERATED ALWAYS or GENERATED BY DEFAULT. That distinction gives PostgreSQL clearer rules for explicit values and makes the schema's generation behavior easier to inspect.
For new PostgreSQL tables, prefer identity columns. You will still encounter serial in older applications, migrations, and database dumps. Treat those columns as sequence-backed defaults, inspect their sequences before changing them, and remember that sequence values can have gaps.
Exercise
Does serial PRIMARY KEY create a special serial data type that guarantees unique values?
Show answer
No. serial expands to an integer column, a sequence, and a default using nextval. The primary key constraint, not serial, enforces uniqueness.