GEN_RANDOM_UUID | Databend

1 min read Original article ↗

Generates a random UUID based on version 7, starting from version 1.2.658. Previously, this function generated UUIDs based on version 4.

In an application where events are logged, maintaining the correct sequence of actions is essential. UUID v7 ensures that each event is time-ordered, making it easy to track actions chronologically.

-- Log a user logging in
SELECT GEN_RANDOM_UUID(), 'User logged in' AS event, CURRENT_TIMESTAMP AS event_time;

-- Log a user making a purchase
SELECT GEN_RANDOM_UUID(), 'User made a purchase' AS event, CURRENT_TIMESTAMP AS event_time;

Notice that the gen_random_uuid() values are generated in the order that the events occurred, making it easy to maintain chronological order.