Generate a new random UUID (version 4).
GEN_RANDOM_UUID()
## Overview Generates a new random UUID conforming to version 4 of RFC 4122. The value is produced from a cryptographically secure random number generator. Use this function in PG-compat SQL that expects `gen_random_uuid()` to be available. GEN_RANDOM_UUID is a PG-compat alias for UUID and UUID_GENERATE_V4. All three return the same format and randomness guarantees. ## Behavior - Non-deterministic: every call returns a different value. - Uses a cryptographically secure random source. - Returns a 36-character lowercase hyphenated string (version 4, variant 10xx). - Never returns NULL. - Side effect free. ## Compatibility - PG-compat alias. Semantically identical to UUID and UUID_GENERATE_V4.
-- Generate a single UUID
SELECT GEN_RANDOM_UUID();
-- Use as a surrogate primary key
INSERT INTO crm.catalog.contacts (id, name)
VALUES (GEN_RANDOM_UUID(), 'Acme Corp');
-- Stamp a correlation id in a CTE so the same value can be reused
WITH c AS (SELECT GEN_RANDOM_UUID() AS cid) SELECT c.cid FROM c;
-- Generate two distinct values in one SELECT
SELECT GEN_RANDOM_UUID() AS uuid_1, GEN_RANDOM_UUID() AS uuid_2;
-- Validate that a generated UUID passes IS_UUID
SELECT IS_UUID(GEN_RANDOM_UUID()); -- true