Skip to content

Benchmark time-based UUIDv7 vs random UUIDv4 Postgres PRIMARY KEYs🐘🔑

License

Notifications You must be signed in to change notification settings

mikeblum/pg-uuidv7-benchmark

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pg-uuid7

Overview

With the upcoming acceptance of time-based UUIDs (v7) I wanted to validate using v4 (random) vs v7 (time-based) as primary keys under Postgres 15.

In theory we should see faster insert and lookup times for UUIDv7 primary keys compared to random UUIDv4 keys which exibit poor index locality:

CYBERTEC - Unexpected downsides of UUID keys in PostgreSQL

Normally I'd prefer the default google/uuid package but it doesn't support v7 UUIDs just yet.

Sources

UUIDv4 - RFC 4122

UUIDv7 Proposal

Timescale - generate_series

Setup

  • golang 1.21.x
  • postgres 15.x
  • gnu make for build tooling
  • goose for schema migrations
  • sqlc for SQL DDL

SELECT version();

PostgreSQL 15.4, compiled by Visual C++ build 1914, 64-bit

Bootstrap SQL DDL

make build

Bootstrap Postgres Schema

Execute as the postgres user:

CREATE USER app WITH PASSWORD '????';

GRANT CONNECT ON DATABASE postgres TO app;

CREATE SCHEMA IF NOT EXISTS app AUTHORIZATION app;

GRANT USAGE ON SCHEMA app TO app;

GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA app TO app;

Run

export DATABASE_URL="postgres:https://app:????@localhost:5432/postgres?search_path=app"
make run

About

Benchmark time-based UUIDv7 vs random UUIDv4 Postgres PRIMARY KEYs🐘🔑

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages