kaiyin
.
zhong
home
essays
experiments
experience
about
contact
Writing
Essays
Things I have learned. Things I built, broke, and eventually figured out.
all
cryptography (3)
databases (1)
linear algebra (7)
linux (1)
llmops (2)
machine learning (15)
math (18)
postgres (1)
rust (10)
security (3)
statistics (5)
storage (2)
tui (9)
tutorial (2)
zfs (3)
37aea8b9c35317b0f51271a99fba288d
Your primary key knows what time it is
Sep 28, 2025 · 11 min
UUIDv7 puts a 48-bit Unix timestamp in front of 74 random bits, and that one design decision fixes almost everything wrong with random UUIDs in a B-tree. Postgres 18 ships it natively, from the bit layout down to what it does to the rightmost leaf page.
databases
·
postgres
UUIDv7 — 128 bits, timestamp first
read left to right, most-significant bits first
unix_ts_ms
48 bits
ver
4
rand_a
12
var
2
rand_b
62 bits
MSB
= 01998f8ce680
LSB
leading big-endian field:
textual order = time order
→ k-sortable, lands on the
index's rightmost leaf
the trailing 74 bits:
random → uniqueness with
no coordination (v4's
superpower, kept intact)