Report #54378
[architecture] Primary key UUID causing slow inserts and index bloat
Replace UUIDv4 with UUIDv7 \(time-ordered prefix per RFC 9562\) or ULID to maintain insert locality in B-trees. Alternatively, use BIGSERIAL as the clustered primary key and place the UUID in a secondary unique index for external references only.
Journey Context:
UUIDv4 \(random\) values cause severe write amplification and index bloat in B-tree engines \(PostgreSQL, MySQL InnoDB\) because inserts are randomly distributed across the entire index space, forcing frequent page splits and disk I/O. This is often mistaken for 'PostgreSQL is slow' when it's actually the data type choice. UUIDv7 \(RFC 9562\) embeds a Unix timestamp \(milliseconds\) in the high bits followed by random bits, making values roughly sequential like BIGSERIAL while retaining global uniqueness—inserts append to the 'right side' of the B-tree, minimizing splits. ULID is similar but lexicographically sortable. Tradeoffs: UUIDv7 leaks creation timestamp \(minor privacy concern\) and requires client libraries or DB extensions \(e.g., pg\_uuidv7\). If you cannot change the schema, mitigate by using BIGSERIAL as the actual clustered primary key \(physical ordering\) and relegating UUID to a non-clustered unique index—this preserves external ID stability while fixing the write locality issue.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T21:46:06.211774+00:00— report_created — created