UUID vs auto-increment for primary keys, which do you prefer?

nightowl87

New Member
Messages
60
Reaction score
0
Location
Portland, OR

So I'm building this little side project (a todo app, nothing fancy) and I started with auto-increment IDs because it was easy. But now I'm reading about UUIDs and how they're better for distributed systems. I'm not doing anything distributed yet, but I don't want to migrate later. What do you all use? I've seen some people say UUIDs are a pain for indexes. Is that true? Also, if I do use UUIDs, should I use v4 or v7? I'm kinda lost. Using Postgres btw, if that matters. Been at this for like 3 months so still learning.

danielp77

New Member
Messages
51
Reaction score
0
Location
Melbourne, AU

Dude. UUIDv4 as a primary key in Postgres is genuinely painful, and the people warning you about indexes are not exaggerating. Random values mean every insert lands somewhere different in the B-tree, so you get page splits, cache misses, and your index bloats way faster than it should. For a todo app with 500 rows you'll never notice. For 50 million rows you'll notice a LOT.

But here's the thing, v7 basically fixed this. It's time-ordered, so inserts append to the right side of the index like a bigint would, but you still get the global uniqueness. If you're on Postgres 18 you literally get uuidv7() built in now, no extension needed. If you're on older versions, pg_uuidv7 or just generating it in your app layer works fine.

Honestly though? For a todo app, the "I don't want to migrate later" instinct is a trap. Migrating a PK from bigint to uuid is one migration and a couple hours of annoyance, and you'd probably rewrite the schema anyway once you actually learn what you need. But since you're asking, and since you're already on Postgres, I'd just go v7 from the start. It's a one-line change now vs. a real project later, and you don't lose the index performance.

What are you using for the app layer? If it's something like Prisma or Drizzle, they handle the generation for you and it's basically free.

You must log in or register to reply here.