Skip to content

Python · performance

no-gen-random-uuid-in-sql

python:no-gen-random-uuid-in-sql

Embedded SQL calls gen_random_uuid() instead of uuidv7().

Code
SARJ053
Default
error
Fix
none
Languages
python

Why

Random UUIDv4 primary keys scatter inserts across B-tree pages; UUIDv7 keys preserve time ordering.

Fix

Use uuidv7() where the supported PostgreSQL version provides it.

Before / after

Executed by this rule’s unit tests.

Before

Table defaults to a random UUID

app/store.py · focus
SQL = "CREATE TABLE call (id UUID PRIMARY KEY DEFAULT gen_random_uuid())"

After

Table defaults to UUIDv7

app/store.py · focus
SQL = "CREATE TABLE call (id UUID PRIMARY KEY DEFAULT uuidv7())"

Limits

  • Detection covers SQL-shaped Python string literals after masking SQL comments and quoted values.
  • Known UUIDv7 compatibility implementations that internally call gen_random_uuid() are excluded.