Menu

1 / 2

Stop Reaching for UUIDv4 Primary Keys: What Postgres 18's uuidv7 Fixes

DEV Community: backend·Andrii B.·7 days ago
#Cztqzbcv
#devtobackend#devto#dev#index#uuid
Long article (3700 words) — no AI summary yet.
Reading 0:00
15s threshold

Here's a table definition you've probably written, approved in review, or copied from a tutorial:

CREATE TABLE orders ( id uuid PRIMARY KEY DEFAULT gen_random_uuid ( ) , customer_id uuid NOT NULL , total_cents bigint NOT NULL , created_at timestamptz NOT NULL DEFAULT now ( ) ) ;

Enter fullscreen mode

Exit fullscreen mode

Nothing is wrong with it on day one. IDs are unguessable, you can generate them on any service without a round trip, and merging data from two databases never collides. It works fine at ten thousand rows. It works fine at a million.

Then the table outgrows memory, and that DEFAULT gen_random_uuid() starts costing you in a way that doesn't show up in any single query plan. Every insert lands on a random page of the primary key index. Pages split in half and stay half empty. Checkpoints turn into a flood of full-page images in the WAL.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.