]> begriffs open source - ai-pg/blob - full-docs/txt/functions-uuid.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / functions-uuid.txt
1
2 9.14. UUID Functions #
3
4    Table 9.45 shows the PostgreSQL functions that can be used to generate
5    UUIDs.
6
7    Table 9.45. UUID Generation Functions
8
9    Function
10
11    Description
12
13    Example(s)
14
15    gen_random_uuid → uuid
16
17    uuidv4 → uuid
18
19    Generate a version 4 (random) UUID.
20
21    gen_random_uuid() → 5b30857f-0bfa-48b5-ac0b-5c64e28078d1
22
23    uuidv4() → b42410ee-132f-42ee-9e4f-09a6485c95b8
24
25    uuidv7 ( [ shift interval ] ) → uuid
26
27    Generate a version 7 (time-ordered) UUID. The timestamp is computed
28    using UNIX timestamp with millisecond precision + sub-millisecond
29    timestamp + random. The optional parameter shift will shift the
30    computed timestamp by the given interval.
31
32    uuidv7() → 019535d9-3df7-79fb-b466-fa907fa17f9e
33
34 Note
35
36    The uuid-ossp module provides additional functions that implement other
37    standard algorithms for generating UUIDs.
38
39    Table 9.46 shows the PostgreSQL functions that can be used to extract
40    information from UUIDs.
41
42    Table 9.46. UUID Extraction Functions
43
44    Function
45
46    Description
47
48    Example(s)
49
50    uuid_extract_timestamp ( uuid ) → timestamp with time zone
51
52    Extracts a timestamp with time zone from UUID version 1 and 7. For
53    other versions, this function returns null. Note that the extracted
54    timestamp is not necessarily exactly equal to the time the UUID was
55    generated; this depends on the implementation that generated the UUID.
56
57    uuid_extract_timestamp('019535d9-3df7-79fb-b466-​fa907fa17f9e'::uuid) →
58    2025-02-23 21:46:24.503-05
59
60    uuid_extract_version ( uuid ) → smallint
61
62    Extracts the version from a UUID of the variant described by RFC 9562.
63    For other variants, this function returns null. For example, for a UUID
64    generated by gen_random_uuid, this function will return 4.
65
66    uuid_extract_version('41db1265-8bc1-4ab3-992f-​885799a4af1d'::uuid) → 4
67
68    uuid_extract_version('019535d9-3df7-79fb-b466-​fa907fa17f9e'::uuid) → 7
69
70    PostgreSQL also provides the usual comparison operators shown in
71    Table 9.1 for UUIDs.
72
73    See Section 8.12 for details on the data type uuid in PostgreSQL.