The GEN_RANDOM_UUID function is used to generate a random universally unique identifier (UUID). This topic describes how to use the GEN_RANDOM_UUID function in Hologres.

Limits

Before you use the GEN_RANDOM_UUID function, execute the CREATE EXTENSION statement to install an extension. The extension is installed at the database level. You need to install the extension only once for each database. If you create a database, you must execute the CREATE EXTENSION statement again.
-- Install an extension.
CREATE EXTENSION pgcrypto;

-- Delete the extension.
DROP EXTENSION pgcrypto;

Syntax

The GEN_RANDOM_UUID function is used to generate a UUID of version 4. The UUID consists of random hexadecimal integers.
Note A UUID has a fixed length of 128 bits. Its value ranges from 00000000-0000-0000-0000-000000000000 to ffffffff-ffff-ffff-ffff-ffffffffffff. For more information about UUID data types, see Data types.
GEN_RANDOM_UUID()

Examples

  • Generate a random UUID.
    Example:
    SELECT GEN_RANDOM_UUID();
    Execution result:
    gen_random_uuid
    ------------------------------------
    3a5401f1-0f0c-4380-8611-78e654efd86d
    
  • Generate a random UUID when data is written.
    Example:
    -- Create a data source table and write data to it. You can also use other data sources such as MaxCompute source tables.
    CREATE TABLE t_source (a INT);
    INSERT INTO t_source SELECT * FROM generate_series(1, 5);
    
    -- Create a destination table.
    CREATE TABLE t_result (a INT, b UUID);
    
    -- Write data to the destination table and generate a random UUID.
    INSERT INTO t_result SELECT *, gen_random_uuid() FROM t_source;
    
    -- View the write result.
    SELECT * FROM t_result;
    Execution result:
     a |                  b
    ---+--------------------------------------
     1 | 27477537-abc4-4c17-9cf3-91c856a3b298
     2 | 2522b1ce-fdf3-4b14-a3c3-78f4baac5186
     3 | c69959a3-ad40-424f-9eb1-3271d0c6a8d4
     4 | 5493e087-b1b8-47e2-8117-adea27aaa676
     5 | f0e55a29-e72e-42e6-99aa-486db4f8b624
    (5 rows)