skip to content

identity columns and uuids

why serial is legacy, GENERATED AS IDENTITY, and uuidv7 keys that sort by time.

address
everything.types.identity_and_uuids
part
part 6, types and values
chapter
chapter 7 of 11
reading time
7 min

an identity column (GENERATED ALWAYS AS IDENTITY) is the standard way to number rows: a sequence behind the column, owned by it. serial is the older postgres shorthand for the same thing, with looser rules.

a uuid is a 128-bit id you can make anywhere, no sequence needed. random ones (version 4) scatter inserts across an index; uuidv7(), new in postgres 18, starts with a timestamp, so new keys sort last.

the picture

you know how a deli hands out paper tickets from a dispenser? the dispenser is a sequence: it gives out the next number, never the same one twice, never takes one back. that's an identity column. a uuid is everyone printing their own ticket, with a number so long that two people never print the same one.

the difference shows up in the index. tickets from a dispenser always go at the end of the line. self-printed random tickets go wherever their number falls, which is anywhere. uuidv7 puts the time first, so self-printed tickets line up too.

uuid =
uuid_extract_version
7
first 12 hex digits
1,761,664,597,792
uuid_extract_timestamp
2025-10-28 15:16:37.792+00

the first 12 hex digits are a millisecond count since 1970, and postgres turns them back into a time. the rest is random (postgres also packs extra sub-millisecond precision in), so two made in a row still sort in the order they were made.

unix time, millisecondsversion and variantrandom
real output, postgres 18.6 (PGlite 0.5.12) · identity-and-uuids
two uuids, as postgres reads them. pick one. both carry a version digit and variant bits. a version 7 spends its first 48 bits on unix time in milliseconds, which is why it sorts by when it was made.

identity columns

INSERT already showed the three kinds of id column side by side and the gap a rollback leaves; this is the rest. an identity column is backed by an ordinary sequence, which postgres creates, names and ties to the column:

sqlempty db
CREATE TABLE teas (id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY, name text NOT NULL);
SELECT pg_get_serial_sequence('teas', 'id') AS sequence;
SELECT data_type, start_value, increment_by, max_value FROM pg_sequences WHERE sequencename = 'teas_id_seq';
output, from postgres 18.6
#1 · CREATE TABLE
#2 · SELECT 1
sequencetext
public.teas_id_seq
(1 row)
#3 · SELECT 1
data_typeregtypestart_valueint8increment_byint8max_valueint8
integer112147483647
(1 row)

ALWAYS means postgres refuses a hand-typed id, on INSERT and on UPDATE:

sqlempty db
CREATE TABLE teas (id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY, name text NOT NULL);
INSERT INTO teas (id, name) VALUES (10, 'oolong');
output, from postgres 18.6
#1 · CREATE TABLE
#2 · ERROR 428C9
ERROR 428C9: cannot insert a non-DEFAULT value into column "id"
DETAIL: Column "id" is an identity column defined as GENERATED ALWAYS.
HINT: Use OVERRIDING SYSTEM VALUE to override.

the escape hatch is OVERRIDING SYSTEM VALUE, for migrations and restores. it gets the row in, and leaves the sequence behind:

after =
teas.sql
  1. 1CREATE TABLE teas (id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY, name text NOT NULL);
  2. 2INSERT INTO teas (name) VALUES ('assam'), ('sencha');
  3. 3SELECT id, name, (SELECT last_value FROM teas_id_seq) AS seq_last_value FROM teas ORDER BY id;
the result for two inserts
idint4nametextseq_last_valueint8
1assam2
2sencha2
(2 rows)

an identity column, two rows. the sequence behind it has handed out 1 and 2.

real output, postgres 18.6 (PGlite 0.5.12) · identity-and-uuids
five statements on one table, the sequence's last_value beside every row. step through: the hand-typed 3 goes in, the sequence still says 2, the next normal insert collides, and setval puts things right.

so every bulk load with explicit ids ends with a setval(pg_get_serial_sequence(...), max(id)). (GENERATED BY DEFAULT accepts the typed id without the OVERRIDING clause, and has exactly the same collision waiting.)

why not serial? it's postgres's older shorthand, not in the standard: it creates the same kind of sequence and sets the column's DEFAULT nextval(...). so it behaves like BY DEFAULT (nothing stops a typed id), the sequence is a loose object you can see in the column default and change by accident, and dropping or copying the default doesn't drag the sequence along. identity is standard SQL SQL:2003, and the sequence belongs to the column itself. use bigint for the column unless you're sure: an integer sequence stops at 2,147,483,647 (integers).

uuids

a uuid is 16 bytes, printed as 36 characters. store it as uuid, not text, which is more than twice the size:

sqlempty db
SELECT pg_column_size('019a2b64-7f20-7b3c-9a1d-4e5f60718293'::uuid) AS as_uuid,
       pg_column_size('019a2b64-7f20-7b3c-9a1d-4e5f60718293'::text) AS as_text;
output, from postgres 18.6
SELECT 1
as_uuidint4as_textint4
1640
(1 row)

two ways to make one in postgres 18. gen_random_uuid() (or uuidv4()) makes a random version 4, and uuidv7() pg 18+ makes a time-ordered version 7. their values change every run, so this page compares their order, not their digits:

sqlempty db
WITH made AS (
  SELECT i, uuidv7() AS v7, gen_random_uuid() AS v4
  FROM generate_series(1, 1000) AS i
)
SELECT bool_and(v7 > prev_v7) AS v7_always_increasing,
       bool_and(v4 > prev_v4) AS v4_always_increasing
FROM (
  SELECT v7, v4, lag(v7) OVER (ORDER BY i) AS prev_v7, lag(v4) OVER (ORDER BY i) AS prev_v4
  FROM made
) AS pairs
WHERE prev_v7 IS NOT NULL;
output, from postgres 18.6
SELECT 1
v7_always_increasingboolv4_always_increasingbool
tf
(1 row)

all thousand uuidv7 values are bigger than the one before; the random ones aren't (each pair is a coin toss). a uuidv7 also tells you when it was made, which a v4 can't:

sqlempty db
SET TimeZone = 'UTC';
SELECT uuid_extract_version(uuidv7()) AS v7, uuid_extract_version(gen_random_uuid()) AS v4,
       uuid_extract_timestamp(uuidv7()) > now() - interval '1 minute' AS v7_time_is_now,
       uuid_extract_timestamp(gen_random_uuid()) IS NULL AS v4_has_no_time;
output, from postgres 18.6
#1 · SET
#2 · SELECT 1
v7int2v4int2v7_time_is_nowboolv4_has_no_timebool
74tt
(1 row)
predict the output
sql
-- 20,000 rows each, keyed by random uuids, by uuidv7() and by a bigint.
-- how many leaf pages of each primary key hold one of the last 1,000 rows?

here's what the leaf pages looked like, measured with pageinspect:

random uuid92 leaf pages
uuidv777 leaf pages
bigint55 leaf pages
random uuid92 leaves touched
uuidv75 leaves touched
bigint4 leaves touched
leaf pages of each primary key after 20,000 inserts (top), and how many of them hold one of the last 1,000 rows (bottom). random keys spread recent inserts over every leaf and leave pages half empty after splits; time-ordered keys keep them together.

92 leaf pages for random keys against 77 for uuidv7: a random insert splits a page in the middle, leaving two half-full pages, where an append fills the last page and starts a new one. and the last 1,000 random inserts touched all 92 leaves, against 5 for uuidv7 and 4 for bigint. on a table bigger than memory, "every leaf" means reads from disk and more WAL per insert. (the uuid tree has more pages than bigint's 55 because each key is 16 bytes, not 8.) b-trees, page by page and page splits show the mechanics.

keylines

  • GENERATED ALWAYS AS IDENTITY is the standard id column; serial is the older, looser shorthand.
  • OVERRIDING SYSTEM VALUE leaves the sequence behind: setval afterwards.
  • pg_get_serial_sequence names the sequence behind an identity or serial column.
  • store uuids as uuid (16 bytes), never as text.
  • random uuids scatter inserts across the index; uuidv7 keys sort by time and append.