Skip to main content

Posts

Showing posts with the label Database Architecture

UUIDv4 vs UUIDv7: Fixing B-Tree Fragmentation in PostgreSQL

  If you are using standard   uuid_generate_v4()   or   gen_random_uuid()   as a Primary Key in PostgreSQL, you have likely architected a performance time bomb. It usually detonates when the table size exceeds available RAM. In the early days of an application, with 100,000 rows, your entire index fits in  shared_buffers  (Postgres's page cache). Writes are fast because they are effectively in-memory operations. However, as you scale to 50M+ rows, insert latency stops being linear and hits a "random I/O cliff." This post analyzes why the B-Tree fails with random inputs and how to solve it using UUIDv7 (RFC 9562). The Root Cause: B-Tree Mechanics and Random I/O PostgreSQL uses B-Tree indexes for Primary Keys by default. B-Trees are balanced tree structures optimized for sorted data. They strive to keep the tree balanced to ensure  O(log n)  lookup times. The Problem with Randomness (UUIDv4) A UUIDv4 is 128 bits of pseudo-randomness. When you i...