Menu

Random UUID primary keys tripled the WAL every insert wrote (558 bytes vs 186)
📰

Random UUID primary keys tripled the WAL every insert wrote (558 bytes vs 186)

DEV Community: database·Muhammad Adnan·9 days ago
#QaF13kfj
#devtodatabase#devto#dev#uuid#index
Long article (987 words) — no AI summary yet.
Reading 0:00
15s threshold

id uuid PRIMARY KEY DEFAULT gen_random_uuid() is in a lot of new schemas, mine included. IDs that don't leak row counts and can be made anywhere are nice. I measured what they cost on the write path against a plain bigint identity and a time-ordered UUID (the version 7 layout).

The setup

PostgreSQL 16, 4 vCPU, local SSD, default settings ( shared_buffers = 128 MB). Three tables that differ only in the primary key:

CREATE TABLE t_bigint ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY , payload text NOT NULL ) ; CREATE TABLE t_uuid4 ( id uuid PRIMARY KEY DEFAULT gen_random_uuid ( ) , payload text NOT NULL ) ; CREATE TABLE t_uuid7 ( id uuid PRIMARY KEY DEFAULT uuid_v7 ( ) , payload text NOT NULL ) ;

Enter fullscreen mode

Exit fullscreen mode

PostgreSQL 16 has no built-in version 7 generator, so uuid_v7() is a small SQL function that puts the current Unix time in milliseconds in the first 48 bits and random bits after it (PostgreSQL 18 ships uuidv7() ).…

Continue reading — create a free account

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