1 CREATE EXTENSION "uuid-ossp";
4 --------------------------------------
5 00000000-0000-0000-0000-000000000000
10 --------------------------------------
11 6ba7b810-9dad-11d1-80b4-00c04fd430c8
16 --------------------------------------
17 6ba7b811-9dad-11d1-80b4-00c04fd430c8
22 --------------------------------------
23 6ba7b812-9dad-11d1-80b4-00c04fd430c8
26 SELECT uuid_ns_x500();
28 --------------------------------------
29 6ba7b814-9dad-11d1-80b4-00c04fd430c8
32 -- some quick and dirty field extraction functions
33 -- this is actually timestamp concatenated with clock sequence, per RFC 4122
34 CREATE FUNCTION uuid_timestamp_bits(uuid) RETURNS varbit AS
35 $$ SELECT ('x' || substr($1::text, 15, 4) || substr($1::text, 10, 4) ||
36 substr($1::text, 1, 8) || substr($1::text, 20, 4))::bit(80)
37 & x'0FFFFFFFFFFFFFFF3FFF' $$
38 LANGUAGE SQL STRICT IMMUTABLE;
39 CREATE FUNCTION uuid_version_bits(uuid) RETURNS varbit AS
40 $$ SELECT ('x' || substr($1::text, 15, 2))::bit(8) & '11110000' $$
41 LANGUAGE SQL STRICT IMMUTABLE;
42 CREATE FUNCTION uuid_reserved_bits(uuid) RETURNS varbit AS
43 $$ SELECT ('x' || substr($1::text, 20, 2))::bit(8) & '11000000' $$
44 LANGUAGE SQL STRICT IMMUTABLE;
45 CREATE FUNCTION uuid_multicast_bit(uuid) RETURNS bool AS
46 $$ SELECT (('x' || substr($1::text, 25, 2))::bit(8) & '00000001') != '00000000' $$
47 LANGUAGE SQL STRICT IMMUTABLE;
48 CREATE FUNCTION uuid_local_admin_bit(uuid) RETURNS bool AS
49 $$ SELECT (('x' || substr($1::text, 25, 2))::bit(8) & '00000010') != '00000000' $$
50 LANGUAGE SQL STRICT IMMUTABLE;
51 CREATE FUNCTION uuid_node(uuid) RETURNS text AS
52 $$ SELECT substr($1::text, 25) $$
53 LANGUAGE SQL STRICT IMMUTABLE;
54 -- Ideally, the multicast bit would never be set in V1 output, but the
55 -- UUID library may fall back to MC if it can't get the system MAC address.
56 -- Also, the local-admin bit might be set (if so, we're probably inside a VM).
57 -- So we can't test either bit here.
58 SELECT uuid_version_bits(uuid_generate_v1()),
59 uuid_reserved_bits(uuid_generate_v1());
60 uuid_version_bits | uuid_reserved_bits
61 -------------------+--------------------
65 -- Although RFC 4122 only requires the multicast bit to be set in V1MC style
66 -- UUIDs, our implementation always sets the local-admin bit as well.
67 SELECT uuid_version_bits(uuid_generate_v1mc()),
68 uuid_reserved_bits(uuid_generate_v1mc()),
69 uuid_multicast_bit(uuid_generate_v1mc()),
70 uuid_local_admin_bit(uuid_generate_v1mc());
71 uuid_version_bits | uuid_reserved_bits | uuid_multicast_bit | uuid_local_admin_bit
72 -------------------+--------------------+--------------------+----------------------
73 00010000 | 10000000 | t | t
76 -- timestamp+clock sequence should be monotonic increasing in v1
77 SELECT uuid_timestamp_bits(uuid_generate_v1()) < uuid_timestamp_bits(uuid_generate_v1());
83 SELECT uuid_timestamp_bits(uuid_generate_v1mc()) < uuid_timestamp_bits(uuid_generate_v1mc());
89 -- Ideally, the node value is stable in V1 addresses, but OSSP UUID
90 -- falls back to V1MC behavior if it can't get the system MAC address.
91 SELECT CASE WHEN uuid_multicast_bit(uuid_generate_v1()) AND
92 uuid_local_admin_bit(uuid_generate_v1()) THEN
95 uuid_node(uuid_generate_v1()) = uuid_node(uuid_generate_v1())
102 -- In any case, V1MC node addresses should be random.
103 SELECT uuid_node(uuid_generate_v1()) <> uuid_node(uuid_generate_v1mc());
109 SELECT uuid_node(uuid_generate_v1mc()) <> uuid_node(uuid_generate_v1mc());
115 SELECT uuid_generate_v3(uuid_ns_dns(), 'www.widgets.com');
117 --------------------------------------
118 3d813cbb-47fb-32ba-91df-831e1593ac29
121 SELECT uuid_generate_v5(uuid_ns_dns(), 'www.widgets.com');
123 --------------------------------------
124 21f7f8de-8051-5b89-8680-0195ef798b6a
127 SELECT uuid_version_bits(uuid_generate_v4()),
128 uuid_reserved_bits(uuid_generate_v4());
129 uuid_version_bits | uuid_reserved_bits
130 -------------------+--------------------
134 SELECT uuid_generate_v4() <> uuid_generate_v4();