]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/functions-uuid.html
PG 18 docs from https://ftp.postgresql.org/pub/source/v18.0/postgresql-18.0-docs...
[ai-pg] / full-docs / src / sgml / html / functions-uuid.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>9.14. UUID Functions</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="functions-textsearch.html" title="9.13. Text Search Functions and Operators" /><link rel="next" href="functions-xml.html" title="9.15. XML Functions" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">9.14. UUID Functions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="functions-textsearch.html" title="9.13. Text Search Functions and Operators">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="functions.html" title="Chapter 9. Functions and Operators">Up</a></td><th width="60%" align="center">Chapter 9. Functions and Operators</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="functions-xml.html" title="9.15. XML Functions">Next</a></td></tr></table><hr /></div><div class="sect1" id="FUNCTIONS-UUID"><div class="titlepage"><div><div><h2 class="title" style="clear: both">9.14. UUID Functions <a href="#FUNCTIONS-UUID" class="id_link">#</a></h2></div></div></div><a id="id-1.5.8.20.2" class="indexterm"></a><a id="id-1.5.8.20.3" class="indexterm"></a><a id="id-1.5.8.20.4" class="indexterm"></a><a id="id-1.5.8.20.5" class="indexterm"></a><a id="id-1.5.8.20.6" class="indexterm"></a><a id="id-1.5.8.20.7" class="indexterm"></a><p>
3    <a class="xref" href="functions-uuid.html#FUNC_UUID_GEN_TABLE" title="Table 9.45. UUID Generation Functions">Table 9.45</a> shows the <span class="productname">PostgreSQL</span>
4    functions that can be used to generate UUIDs.
5   </p><div class="table" id="FUNC_UUID_GEN_TABLE"><p class="title"><strong>Table 9.45. <acronym class="acronym">UUID</acronym> Generation Functions</strong></p><div class="table-contents"><table class="table" summary="UUID Generation Functions" border="1"><colgroup><col /></colgroup><thead><tr><th class="func_table_entry">
6        <p class="func_signature">
7         Function
8        </p>
9        <p>
10         Description
11         </p>
12        <p>
13         Example(s)
14        </p>
15        </th></tr></thead><tbody><tr><td class="func_table_entry">
16        <p class="func_signature">
17         <code class="type">gen_random_uuid</code>
18         → <code class="returnvalue">uuid</code>
19        </p>
20        <p class="func_signature">
21         <code class="type">uuidv4</code>
22         → <code class="returnvalue">uuid</code>
23        </p>
24        <p>
25          Generate a version 4 (random) UUID.
26        </p>
27        <p>
28         <code class="literal">gen_random_uuid()</code>
29         → <code class="returnvalue">5b30857f-0bfa-48b5-ac0b-5c64e28078d1</code>
30        </p>
31        <p>
32         <code class="literal">uuidv4()</code>
33         → <code class="returnvalue">b42410ee-132f-42ee-9e4f-09a6485c95b8</code>
34        </p>
35       </td></tr><tr><td class="func_table_entry">
36        <p class="func_signature">
37         <code class="type">uuidv7</code>
38         ( [<span class="optional"> <em class="parameter"><code>shift</code></em> <code class="type">interval</code> </span>] )
39         → <code class="returnvalue">uuid</code>
40        </p>
41        <p>
42         Generate a version 7 (time-ordered) UUID. The timestamp is computed using UNIX timestamp
43         with millisecond precision + sub-millisecond timestamp + random. The optional parameter
44         <em class="parameter"><code>shift</code></em> will shift the computed timestamp by the given <code class="type">interval</code>.
45        </p>
46        <p>
47         <code class="literal">uuidv7()</code>
48         → <code class="returnvalue">019535d9-3df7-79fb-b466-fa907fa17f9e</code>
49        </p>
50       </td></tr></tbody></table></div></div><br class="table-break" /><div class="note"><h3 class="title">Note</h3><p>
51     The <a class="xref" href="uuid-ossp.html" title="F.49. uuid-ossp — a UUID generator">uuid-ossp</a> module provides additional functions that
52     implement other standard algorithms for generating UUIDs.
53    </p></div><p>
54    <a class="xref" href="functions-uuid.html#FUNC_UUID_EXTRACT_TABLE" title="Table 9.46. UUID Extraction Functions">Table 9.46</a> shows the <span class="productname">PostgreSQL</span>
55    functions that can be used to extract information from UUIDs.
56   </p><div class="table" id="FUNC_UUID_EXTRACT_TABLE"><p class="title"><strong>Table 9.46. <acronym class="acronym">UUID</acronym> Extraction Functions</strong></p><div class="table-contents"><table class="table" summary="UUID Extraction Functions" border="1"><colgroup><col /></colgroup><thead><tr><th class="func_table_entry">
57        <p class="func_signature">
58         Function
59        </p>
60        <p>
61         Description
62        </p>
63        <p>
64         Example(s)
65        </p>
66       </th></tr></thead><tbody><tr><td class="func_table_entry">
67        <p class="func_signature">
68         <code class="type">uuid_extract_timestamp</code>
69         ( <code class="type">uuid</code> )
70         → <code class="returnvalue">timestamp with time zone</code>
71        </p>
72        <p>
73         Extracts a <code class="type">timestamp with time zone</code> from UUID
74         version 1 and 7.  For other versions, this function returns null.  Note that
75         the extracted timestamp is not necessarily exactly equal to the time the
76         UUID was generated; this depends on the implementation that generated the
77         UUID.
78        </p>
79        <p>
80         <code class="literal">uuid_extract_timestamp('019535d9-3df7-79fb-b466-​fa907fa17f9e'::uuid)</code>
81          → <code class="returnvalue">2025-02-23 21:46:24.503-05</code>
82        </p>
83       </td></tr><tr><td class="func_table_entry">
84        <p class="func_signature">
85         <code class="type">uuid_extract_version</code>
86         ( <code class="type">uuid</code> )
87         → <code class="returnvalue">smallint</code>
88        </p>
89        <p>
90         Extracts the version from a UUID of the variant described by
91         <a class="ulink" href="https://datatracker.ietf.org/doc/html/rfc9562" target="_top">RFC 9562</a>.  For
92         other variants, this function returns null.  For example, for a UUID
93         generated by <code class="function">gen_random_uuid</code>, this function will
94         return 4.
95        </p>
96        <p>
97         <code class="literal">uuid_extract_version('41db1265-8bc1-4ab3-992f-​885799a4af1d'::uuid)</code>
98         → <code class="returnvalue">4</code>
99        </p>
100        <p>
101         <code class="literal">uuid_extract_version('019535d9-3df7-79fb-b466-​fa907fa17f9e'::uuid)</code>
102         → <code class="returnvalue">7</code>
103        </p>
104       </td></tr></tbody></table></div></div><br class="table-break" /><p>
105    <span class="productname">PostgreSQL</span> also provides the usual comparison
106    operators shown in <a class="xref" href="functions-comparison.html#FUNCTIONS-COMPARISON-OP-TABLE" title="Table 9.1. Comparison Operators">Table 9.1</a> for
107    UUIDs.
108   </p><p>
109    See <a class="xref" href="datatype-uuid.html" title="8.12. UUID Type">Section 8.12</a> for details on the data type
110    <code class="type">uuid</code> in <span class="productname">PostgreSQL</span>.
111   </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="functions-textsearch.html" title="9.13. Text Search Functions and Operators">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="functions.html" title="Chapter 9. Functions and Operators">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="functions-xml.html" title="9.15. XML Functions">Next</a></td></tr><tr><td width="40%" align="left" valign="top">9.13. Text Search Functions and Operators </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 9.15. XML Functions</td></tr></table></div></body></html>