UUID_GENERATE_V4

Generate a new random UUID (version 4).

Category: miscReturns: STRINGDialect: PostgreSql

Syntax

UUID_GENERATE_V4()

Description

## Overview Generates a new random UUID conforming to version 4 of RFC 4122. This function exists as a compatibility alias for PG-compat code that expects the `uuid_generate_v4()` name from the `uuid-ossp` extension. UUID_GENERATE_V4 is semantically identical to UUID and GEN_RANDOM_UUID. ## 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 for UUID and GEN_RANDOM_UUID.

Examples

-- Generate a single UUID
SELECT UUID_GENERATE_V4();
-- Use as a primary key value on insert
INSERT INTO crm.catalog.contacts (id, name)
VALUES (UUID_GENERATE_V4(), 'Acme Corp');
-- Each call produces a distinct value
SELECT UUID_GENERATE_V4() AS id_1, UUID_GENERATE_V4() AS id_2;
-- Freeze a UUID for reuse across references in the same query
WITH id AS (SELECT UUID_GENERATE_V4() AS v) SELECT id.v FROM id JOIN id i2 ON id.v = i2.v;
-- Round-trip validation
SELECT IS_UUID(UUID_GENERATE_V4()) AS ok;  -- true

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →