1 -- regression test for the uuid datatype
2 -- creating test tables
6 text_field TEXT DEFAULT(now())
11 text_field TEXT DEFAULT(now())
18 -- inserting invalid data tests
20 INSERT INTO guid1(guid_field) VALUES('11111111-1111-1111-1111-111111111111F');
21 ERROR: invalid input syntax for type uuid: "11111111-1111-1111-1111-111111111111F"
22 LINE 1: INSERT INTO guid1(guid_field) VALUES('11111111-1111-1111-111...
25 INSERT INTO guid1(guid_field) VALUES('{11111111-1111-1111-1111-11111111111}');
26 ERROR: invalid input syntax for type uuid: "{11111111-1111-1111-1111-11111111111}"
27 LINE 1: INSERT INTO guid1(guid_field) VALUES('{11111111-1111-1111-11...
29 -- valid data but invalid format
30 INSERT INTO guid1(guid_field) VALUES('111-11111-1111-1111-1111-111111111111');
31 ERROR: invalid input syntax for type uuid: "111-11111-1111-1111-1111-111111111111"
32 LINE 1: INSERT INTO guid1(guid_field) VALUES('111-11111-1111-1111-11...
34 INSERT INTO guid1(guid_field) VALUES('{22222222-2222-2222-2222-222222222222 ');
35 ERROR: invalid input syntax for type uuid: "{22222222-2222-2222-2222-222222222222 "
36 LINE 1: INSERT INTO guid1(guid_field) VALUES('{22222222-2222-2222-22...
39 INSERT INTO guid1(guid_field) VALUES('11111111-1111-1111-G111-111111111111');
40 ERROR: invalid input syntax for type uuid: "11111111-1111-1111-G111-111111111111"
41 LINE 1: INSERT INTO guid1(guid_field) VALUES('11111111-1111-1111-G11...
43 INSERT INTO guid1(guid_field) VALUES('11+11111-1111-1111-1111-111111111111');
44 ERROR: invalid input syntax for type uuid: "11+11111-1111-1111-1111-111111111111"
45 LINE 1: INSERT INTO guid1(guid_field) VALUES('11+11111-1111-1111-111...
47 -- test non-error-throwing API
48 SELECT pg_input_is_valid('11', 'uuid');
54 SELECT * FROM pg_input_error_info('11', 'uuid');
55 message | detail | hint | sql_error_code
56 ------------------------------------------+--------+------+----------------
57 invalid input syntax for type uuid: "11" | | | 22P02
60 --inserting three input formats
61 INSERT INTO guid1(guid_field) VALUES('11111111-1111-1111-1111-111111111111');
62 INSERT INTO guid1(guid_field) VALUES('{22222222-2222-2222-2222-222222222222}');
63 INSERT INTO guid1(guid_field) VALUES('3f3e3c3b3a3039383736353433a2313e');
64 -- retrieving the inserted data
65 SELECT guid_field FROM guid1;
67 --------------------------------------
68 11111111-1111-1111-1111-111111111111
69 22222222-2222-2222-2222-222222222222
70 3f3e3c3b-3a30-3938-3736-353433a2313e
74 SELECT guid_field FROM guid1 ORDER BY guid_field ASC;
76 --------------------------------------
77 11111111-1111-1111-1111-111111111111
78 22222222-2222-2222-2222-222222222222
79 3f3e3c3b-3a30-3938-3736-353433a2313e
82 SELECT guid_field FROM guid1 ORDER BY guid_field DESC;
84 --------------------------------------
85 3f3e3c3b-3a30-3938-3736-353433a2313e
86 22222222-2222-2222-2222-222222222222
87 11111111-1111-1111-1111-111111111111
91 SELECT COUNT(*) FROM guid1 WHERE guid_field = '3f3e3c3b-3a30-3938-3736-353433a2313e';
98 SELECT COUNT(*) FROM guid1 WHERE guid_field <> '11111111111111111111111111111111';
105 SELECT COUNT(*) FROM guid1 WHERE guid_field < '22222222-2222-2222-2222-222222222222';
112 SELECT COUNT(*) FROM guid1 WHERE guid_field <= '22222222-2222-2222-2222-222222222222';
119 SELECT COUNT(*) FROM guid1 WHERE guid_field > '22222222-2222-2222-2222-222222222222';
126 SELECT COUNT(*) FROM guid1 WHERE guid_field >= '22222222-2222-2222-2222-222222222222';
132 -- btree and hash index creation test
133 CREATE INDEX guid1_btree ON guid1 USING BTREE (guid_field);
134 CREATE INDEX guid1_hash ON guid1 USING HASH (guid_field);
136 CREATE UNIQUE INDEX guid1_unique_BTREE ON guid1 USING BTREE (guid_field);
138 SELECT COUNT(*) FROM guid1 WHERE guid_field <> '11111111111111111111111111111111' OR
139 guid_field <> '3f3e3c3b-3a30-3938-3736-353433a2313e';
141 ------------------------------------------------------------------------------------------------------------------------------------------------
144 Filter: ((guid_field <> '11111111-1111-1111-1111-111111111111'::uuid) OR (guid_field <> '3f3e3c3b-3a30-3938-3736-353433a2313e'::uuid))
148 SELECT COUNT(*) FROM guid1 WHERE guid_field <= '22222222-2222-2222-2222-222222222222' OR
149 guid_field <= '11111111111111111111111111111111' OR
150 guid_field <= '3f3e3c3b-3a30-3938-3736-353433a2313e';
152 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
155 Filter: ((guid_field <= '22222222-2222-2222-2222-222222222222'::uuid) OR (guid_field <= '11111111-1111-1111-1111-111111111111'::uuid) OR (guid_field <= '3f3e3c3b-3a30-3938-3736-353433a2313e'::uuid))
159 SELECT COUNT(*) FROM guid1 WHERE guid_field = '3f3e3c3b-3a30-3938-3736-353433a2313e' OR
160 guid_field = '11111111111111111111111111111111';
162 ----------------------------------------------------------------------------------------------------------------------------------------------
165 Filter: ((guid_field = '3f3e3c3b-3a30-3938-3736-353433a2313e'::uuid) OR (guid_field = '11111111-1111-1111-1111-111111111111'::uuid))
169 INSERT INTO guid1(guid_field) VALUES('11111111-1111-1111-1111-111111111111');
170 ERROR: duplicate key value violates unique constraint "guid1_unique_btree"
171 DETAIL: Key (guid_field)=(11111111-1111-1111-1111-111111111111) already exists.
172 -- check to see whether the new indexes are actually there
173 SELECT count(*) FROM pg_class WHERE relkind='i' AND relname LIKE 'guid%';
179 -- populating the test tables with additional records
180 INSERT INTO guid1(guid_field) VALUES('44444444-4444-4444-4444-444444444444');
181 INSERT INTO guid2(guid_field) VALUES('11111111-1111-1111-1111-111111111111');
182 INSERT INTO guid2(guid_field) VALUES('{22222222-2222-2222-2222-222222222222}');
183 INSERT INTO guid2(guid_field) VALUES('3f3e3c3b3a3039383736353433a2313e');
185 SELECT COUNT(*) FROM guid1 g1 INNER JOIN guid2 g2 ON g1.guid_field = g2.guid_field;
191 SELECT COUNT(*) FROM guid1 g1 LEFT JOIN guid2 g2 ON g1.guid_field = g2.guid_field WHERE g2.guid_field IS NULL;
199 INSERT INTO guid1 (guid_field) VALUES (gen_random_uuid());
200 INSERT INTO guid1 (guid_field) VALUES (gen_random_uuid());
201 SELECT count(DISTINCT guid_field) FROM guid1;
207 -- test of uuidv4() alias
209 INSERT INTO guid1 (guid_field) VALUES (uuidv4());
210 INSERT INTO guid1 (guid_field) VALUES (uuidv4());
211 SELECT count(DISTINCT guid_field) FROM guid1;
217 -- generation test for v7
219 INSERT INTO guid1 (guid_field) VALUES (uuidv7());
220 INSERT INTO guid1 (guid_field) VALUES (uuidv7());
221 INSERT INTO guid1 (guid_field) VALUES (uuidv7(INTERVAL '1 day'));
222 SELECT count(DISTINCT guid_field) FROM guid1;
228 -- test sortability of v7
229 INSERT INTO guid3 (guid_field) SELECT uuidv7() FROM generate_series(1, 10);
230 SELECT array_agg(id ORDER BY guid_field) FROM guid3;
232 ------------------------
233 {1,2,3,4,5,6,7,8,9,10}
238 SELECT uuid_extract_version('11111111-1111-5111-8111-111111111111'); -- 5
240 ----------------------
244 SELECT uuid_extract_version(gen_random_uuid()); -- 4
246 ----------------------
250 SELECT uuid_extract_version('11111111-1111-1111-1111-111111111111'); -- null
252 ----------------------
256 SELECT uuid_extract_version(uuidv4()); -- 4
258 ----------------------
262 SELECT uuid_extract_version(uuidv7()); -- 7
264 ----------------------
269 SELECT uuid_extract_timestamp('C232AB00-9414-11EC-B3C8-9F6BDECED846') = 'Tuesday, February 22, 2022 2:22:22.00 PM GMT+05:00'; -- RFC 9562 test vector for v1
275 SELECT uuid_extract_timestamp('017F22E2-79B0-7CC3-98C4-DC0C0C07398F') = 'Tuesday, February 22, 2022 2:22:22.00 PM GMT+05:00'; -- RFC 9562 test vector for v7
281 SELECT uuid_extract_timestamp(gen_random_uuid()); -- null
282 uuid_extract_timestamp
283 ------------------------
287 SELECT uuid_extract_timestamp('11111111-1111-1111-1111-111111111111'); -- null
288 uuid_extract_timestamp
289 ------------------------
294 DROP TABLE guid1, guid2, guid3 CASCADE;