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() ).…