UUID

Generate a new random UUID (version 4).

Category: miscReturns: STRINGDialect: Standard

Syntax

UUID()

Description

## Overview Generates a new random UUID (Universally Unique Identifier) in the standard 8-4-4-4-12 hexadecimal format. Each call returns a fresh, independent value. Use this function to create surrogate primary keys, correlation IDs, idempotency tokens, or any situation that needs a globally unique identifier without coordination. UUID is a dialect-neutral alias for GEN_RANDOM_UUID and UUID_GENERATE_V4 and produces the same value format and randomness guarantees. ## Behavior - Non-deterministic: every call returns a different value, even within the same row or the same statement. - Uses a cryptographically secure random source. - Returns a 36-character lowercase hyphenated string (version 4, variant 10xx). - Never returns NULL. - Side effect free (does not consult a sequence, file, or external service). ## Compatibility - Dialect-neutral name. PG-compat callers may prefer GEN_RANDOM_UUID; the function UUID_GENERATE_V4 is available for code ported from engines that use that name.

Examples

-- Generate a single UUID
SELECT UUID();
-- Stamp a request log with a correlation id
SELECT UUID() AS request_id, 'process_start' AS event;
-- Freeze a UUID in a CTE so the same value is reused in multiple projections
WITH id AS (SELECT UUID() AS v) SELECT id.v, id.v FROM id;
-- Validate freshly generated UUIDs round-trip through IS_UUID
SELECT IS_UUID(UUID()) AS ok;  -- true
-- Use as primary key on insert
INSERT INTO ops.audit.events (event_id, actor, occurred_at)
VALUES (UUID(), CURRENT_USER(), CURRENT_TIMESTAMP);

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →