DEV Community

Cover image for Random UUID primary keys tripled the WAL every insert wrote (558 bytes vs 186)
Muhammad Adnan
Muhammad Adnan

Posted on

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

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()). The scripts and full output are in the measurement folder for this post.

Two tests:

  1. Bulk load: 10,000,000 rows into each table with one INSERT ... SELECT.
  2. Steady traffic: then, with each table at 10M rows and its index larger than shared_buffers, 8 clients doing single-row INSERTs, one per transaction, for 60 seconds per table. That's what an app does all day.

Steady traffic: the WAL per insert tripled

Primary key Inserts in 60 s WAL written WAL per insert
bigint identity 1,898,643 338 MB 186 bytes
UUID v4 (gen_random_uuid()) 1,756,807 934 MB 558 bytes
UUID v7 (time-ordered) 1,447,834 270 MB 196 bytes

Throughput barely moved on this machine (29,000 vs 31,600 inserts a second; the v7 run is slower because its generator is a SQL function). The difference is in what every insert writes: three times as much WAL with random UUIDs.

WAL is what goes to disk before a commit returns, to every replica, and into every backup and archive. Tripling it means three times the replication traffic, larger backups, and more disk I/O, on every insert, forever.

Why random keys write more

The cause is how Postgres protects against torn pages. After every checkpoint, the first change to any page writes a full copy of that 8 KB page into the WAL (full_page_writes, on by default). Later changes to the same page, until the next checkpoint, only log the small change.

  • With bigint or UUID v7, new keys are always the largest so far. Every insert lands on the same rightmost leaf page of the index. That page gets one full-page image per checkpoint, then tiny records.
  • With UUID v4, every insert lands on a random leaf page among tens of thousands. Most of them haven't been touched since the last checkpoint, so most inserts pay for a full 8 KB page image in the index on top of the row itself.

The bigger the index, the worse it gets: more pages, so fewer inserts land on a page that's already been copied since the last checkpoint.

Bulk load: 3.8 times slower

Primary key 10M-row INSERT ... SELECT WAL written Index size Leaf pages full
bigint identity 15.4 s 1,428 MB 214 MB 90%
UUID v4 58.6 s 2,423 MB 391 MB 69%
UUID v7 29.8 s 1,641 MB 408 MB 66%

Random keys split pages all over the index, and half-empty pages are why the v4 index ends up 69% full instead of 90%. Part of the size difference is simply that a UUID is 16 bytes and a bigint 8.

Two honest caveats about the v7 row. Its time includes the SQL function, which is slower than a built-in. And its index came out as loose as v4's in this test because a bulk load writes hundreds of rows per millisecond, and within one millisecond the v7 bits are random. At normal app rates, where few rows share a millisecond, the steady-traffic test above is the one that applies, and there v7 behaved like bigint.

What to do

  • New tables: if you want UUIDs, use time-ordered ones. On PostgreSQL 18, DEFAULT uuidv7(). On older versions, generate UUID v7 in the app (most UUID libraries have it now) or use a SQL function like the one in the measurement folder.
  • Or keep bigint as the primary key and add a separate random uuid column with a unique index for public IDs, if you need IDs that don't reveal order. The random index still pays the cost, but only one index instead of the primary key and every foreign key that references it.
  • Existing tables with v4 keys: measure before changing anything. Compare pg_current_wal_lsn() before and after a known number of inserts, like the script does. If WAL volume, replica lag or backup size isn't a problem for you, it may not be worth a migration.
  • Spreading checkpoints further apart (checkpoint_timeout, max_wal_size) reduces full-page images for every key type, at the cost of longer crash recovery.

Checklist

  • New primary keys are bigint identity or time-ordered UUIDs, not gen_random_uuid().
  • Foreign keys use the same type as the key they reference.
  • WAL volume per day is a number you track, next to database size.
  • Anyone changing ID types measures WAL per insert at production table sizes, not on an empty table.

More measurements like this

I measure one Postgres behavior like this every few days. My Claude Code skills pack for TypeScript + GraphQL + Postgres backends turns lessons like these into rules Claude follows while it writes code: safe migrations, slow-query profiling, cursor pagination, resolver auth and more, plus a starter backend tested in CI against a real Postgres.

Top comments (0)