UUID in PostgreSQL — Tutorial Video Walkthrough

PostgreSQL 18 ships uuidv7() as a built-in — set it as a column default and every insert path (ORM, raw SQL, migrations) gets UUID v7 automatically. For PG 14–17, use the pg_uuidv7 extension. The native uuid column type stores 16 bytes; never use VARCHAR(36). See PostgreSQL 18 Ships Native UUID v7.

Time-range queries: WHERE id > uuidv7_sub_interval(uuidv7(), '1 hour') hits the primary key index — no separate created_at column needed.

Read more: UUID in Databases · Zero-Downtime Migration · Lexicographical Order

Generate a UUID v7 · UUID Decoder

Frequently asked questions

Do videos on UUID database performance actually explain B-tree fragmentation?

Good UUID database performance videos explain that UUID v4 causes B-tree page splits because each new value inserts at a random position, fragmenting the index. UUID v7 solves this — its timestamp prefix means consecutive inserts cluster at the rightmost leaf, the same behaviour as auto-increment. See UUID Database Primary Keys video walkthrough and the full UUID in Databases guide.

Does PostgreSQL 18 support UUID v7 natively?

Yes. PostgreSQL 18 ships a built-in uuidv7() function that generates RFC 9562-compliant UUID v7 values directly in SQL — no extensions or application-level generation required. The values store in the existing uuid column type unchanged.

Do UUIDs hurt database index performance?

UUID v4 does — its randomness causes every insert to land at a different leaf page, leading to page splits and poor cache locality. UUID v7 embeds a millisecond timestamp so consecutive inserts cluster together, behaving like an auto-increment integer for B-tree purposes.