sys.gen_random_uuid()
Generate a random
UUID
value (version 4).
SYS.GEN_RANDOM_UUID()
This function does not have any parameters. The parentheses are required. Include
sys.
before the function name if the sys
schema is not
in your search path. For information about the UUID
data type, see UUID.
For
example:
premdb=# select sys.gen_random_uuid();
gen_random_uuid
--------------------------------------
f280eb64-a88f-40d8-9fc0-f345fc4d2726
(1 row)
Add a
UUID
column to a table called newmatchstats
, then
generate unique values for
it:premdb=# alter table newmatchstats add column matchid uuid;
ALTER TABLE
premdb=# \d newmatchstats;
Table "public.newmatchstats"
Column | Type | Modifiers
----------+----------------------+-----------
seasonid | smallint |
matchday | date |
htid | smallint |
atid | smallint |
moment | character varying(5) |
matchid | uuid |
Distribution: Hash (seasonid)
premdb=# update newmatchstats set matchid=sys.gen_random_uuid();
UPDATE 524966
premdb=# select count(distinct matchid) from newmatchstats;
count
--------
524966
(1 row)
The COUNT(DISTINCT)
query verifies the uniqueness of the generated
UUID
values.