UUID Use Case

UUID as a Primary Key

A UUID primary key lets any node mint a unique row ID without a central counter — ideal for distributed systems and offline-first apps.

Distributed-safeMerge-safePrefer v7

Generated locally with the Web Crypto API — nothing leaves your browser.

What Is UUID as a Primary Key?

How to generate

  1. Pick v7 so new rows sort by creation time.
  2. Default the column to a generator (gen_random_uuid() equivalent or app-side v7).
  3. Index it; consider a BRIN index for very large time-ordered tables.

Use Cases

Code Examples

PostgreSQL
CREATE TABLE orders (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v7(),
  total numeric
);
JavaScript
import { v7 as uuidv7 } from 'uuid';
const id = uuidv7(); // time-ordered

Frequently Asked Questions

UUID or BIGSERIAL for a primary key?
UUID scales across nodes and hides volume; BIGSERIAL is smaller and faster but centralised and guessable. Choose UUID when you need distribution or opaque IDs.
Why v7 over v4 for keys?
v7's time prefix makes inserts sequential, avoiding the page splits and fragmentation that random v4 causes in B-tree indexes.

Related tools