Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
-- regression test for the uuid datatype
-- creating test tables
CREATE TABLE guid1
(
    guid_field UUID,
    text_field TEXT DEFAULT(now())
);
CREATE TABLE guid2
(
    guid_field UUID,
    text_field TEXT DEFAULT(now())
);
CREATE TABLE guid3
(
    id SERIAL,
    guid_field UUID,
    guid_encoded text GENERATED ALWAYS AS (encode(guid_field::bytea, 'base32hex')) STORED
);
-- inserting invalid data tests
-- too long
INSERT INTO guid1(guid_field) VALUES('11111111-1111-1111-1111-111111111111F');
ERROR:  invalid input syntax for type uuid: "11111111-1111-1111-1111-111111111111F"
LINE 1: INSERT INTO guid1(guid_field) VALUES('11111111-1111-1111-111...
                                             ^
-- too short
INSERT INTO guid1(guid_field) VALUES('{11111111-1111-1111-1111-11111111111}');
ERROR:  invalid input syntax for type uuid: "{11111111-1111-1111-1111-11111111111}"
LINE 1: INSERT INTO guid1(guid_field) VALUES('{11111111-1111-1111-11...
                                             ^
-- valid data but invalid format
INSERT INTO guid1(guid_field) VALUES('111-11111-1111-1111-1111-111111111111');
ERROR:  invalid input syntax for type uuid: "111-11111-1111-1111-1111-111111111111"
LINE 1: INSERT INTO guid1(guid_field) VALUES('111-11111-1111-1111-11...
                                             ^
INSERT INTO guid1(guid_field) VALUES('{22222222-2222-2222-2222-222222222222 ');
ERROR:  invalid input syntax for type uuid: "{22222222-2222-2222-2222-222222222222 "
LINE 1: INSERT INTO guid1(guid_field) VALUES('{22222222-2222-2222-22...
                                             ^
-- invalid data
INSERT INTO guid1(guid_field) VALUES('11111111-1111-1111-G111-111111111111');
ERROR:  invalid input syntax for type uuid: "11111111-1111-1111-G111-111111111111"
LINE 1: INSERT INTO guid1(guid_field) VALUES('11111111-1111-1111-G11...
                                             ^
INSERT INTO guid1(guid_field) VALUES('11+11111-1111-1111-1111-111111111111');
ERROR:  invalid input syntax for type uuid: "11+11111-1111-1111-1111-111111111111"
LINE 1: INSERT INTO guid1(guid_field) VALUES('11+11111-1111-1111-111...
                                             ^
-- test non-error-throwing API
SELECT pg_input_is_valid('11', 'uuid');
 pg_input_is_valid 
-------------------
 f
(1 row)
 
SELECT * FROM pg_input_error_info('11', 'uuid');
                 message                  | detail | hint | sql_error_code 
------------------------------------------+--------+------+----------------
 invalid input syntax for type uuid: "11" |        |      | 22P02
(1 row)
 
--inserting three input formats
INSERT INTO guid1(guid_field) VALUES('11111111-1111-1111-1111-111111111111');
INSERT INTO guid1(guid_field) VALUES('{22222222-2222-2222-2222-222222222222}');
INSERT INTO guid1(guid_field) VALUES('3f3e3c3b3a3039383736353433a2313e');
-- retrieving the inserted data
SELECT guid_field FROM guid1;
              guid_field              
--------------------------------------
 11111111-1111-1111-1111-111111111111
 22222222-2222-2222-2222-222222222222
 3f3e3c3b-3a30-3938-3736-353433a2313e
(3 rows)
 
-- ordering test
SELECT guid_field FROM guid1 ORDER BY guid_field ASC;
              guid_field              
--------------------------------------
 11111111-1111-1111-1111-111111111111
 22222222-2222-2222-2222-222222222222
 3f3e3c3b-3a30-3938-3736-353433a2313e
(3 rows)
 
SELECT guid_field FROM guid1 ORDER BY guid_field DESC;
              guid_field              
--------------------------------------
 3f3e3c3b-3a30-3938-3736-353433a2313e
 22222222-2222-2222-2222-222222222222
 11111111-1111-1111-1111-111111111111
(3 rows)
 
-- = operator test
SELECT COUNT(*) FROM guid1 WHERE guid_field = '3f3e3c3b-3a30-3938-3736-353433a2313e';
 count 
-------
     1
(1 row)
 
-- <> operator test
SELECT COUNT(*) FROM guid1 WHERE guid_field <> '11111111111111111111111111111111';
 count 
-------
     2
(1 row)
 
-- < operator test
SELECT COUNT(*) FROM guid1 WHERE guid_field < '22222222-2222-2222-2222-222222222222';
 count 
-------
     1
(1 row)
 
-- <= operator test
SELECT COUNT(*) FROM guid1 WHERE guid_field <= '22222222-2222-2222-2222-222222222222';
 count 
-------
     2
(1 row)
 
-- > operator test
SELECT COUNT(*) FROM guid1 WHERE guid_field > '22222222-2222-2222-2222-222222222222';
 count 
-------
     1
(1 row)
 
-- >= operator test
SELECT COUNT(*) FROM guid1 WHERE guid_field >= '22222222-2222-2222-2222-222222222222';
 count 
-------
     2
(1 row)
 
-- btree and hash index creation test
CREATE INDEX guid1_btree ON guid1 USING BTREE (guid_field);
CREATE INDEX guid1_hash  ON guid1 USING HASH  (guid_field);
-- unique index test
CREATE UNIQUE INDEX guid1_unique_BTREE ON guid1 USING BTREE (guid_field);
EXPLAIN (COSTS OFF)
SELECT COUNT(*) FROM guid1 WHERE guid_field <> '11111111111111111111111111111111' OR
                            guid_field <> '3f3e3c3b-3a30-3938-3736-353433a2313e';
                                                                   QUERY PLAN                                                                   
------------------------------------------------------------------------------------------------------------------------------------------------
 Aggregate
   ->  Seq Scan on guid1
         Filter: ((guid_field <> '11111111-1111-1111-1111-111111111111'::uuid) OR (guid_field <> '3f3e3c3b-3a30-3938-3736-353433a2313e'::uuid))
(3 rows)
 
EXPLAIN (COSTS OFF)
SELECT COUNT(*) FROM guid1 WHERE guid_field <= '22222222-2222-2222-2222-222222222222' OR
                                    guid_field <= '11111111111111111111111111111111' OR
                                    guid_field <= '3f3e3c3b-3a30-3938-3736-353433a2313e';
                                                                                                   QUERY PLAN                                                                                                   
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Aggregate
   ->  Seq Scan on guid1
         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))
(3 rows)
 
EXPLAIN (COSTS OFF)
SELECT COUNT(*) FROM guid1 WHERE guid_field = '3f3e3c3b-3a30-3938-3736-353433a2313e' OR
                            guid_field = '11111111111111111111111111111111';
                                                                  QUERY PLAN                                                                  
----------------------------------------------------------------------------------------------------------------------------------------------
 Aggregate
   ->  Seq Scan on guid1
         Filter: ((guid_field = '3f3e3c3b-3a30-3938-3736-353433a2313e'::uuid) OR (guid_field = '11111111-1111-1111-1111-111111111111'::uuid))
(3 rows)
 
-- should fail
INSERT INTO guid1(guid_field) VALUES('11111111-1111-1111-1111-111111111111');
ERROR:  duplicate key value violates unique constraint "guid1_unique_btree"
DETAIL:  Key (guid_field)=(11111111-1111-1111-1111-111111111111) already exists.
-- check to see whether the new indexes are actually there
SELECT count(*) FROM pg_class WHERE relkind='i' AND relname LIKE 'guid%';
 count 
-------
     3
(1 row)
 
-- populating the test tables with additional records
INSERT INTO guid1(guid_field) VALUES('44444444-4444-4444-4444-444444444444');
INSERT INTO guid2(guid_field) VALUES('11111111-1111-1111-1111-111111111111');
INSERT INTO guid2(guid_field) VALUES('{22222222-2222-2222-2222-222222222222}');
INSERT INTO guid2(guid_field) VALUES('3f3e3c3b3a3039383736353433a2313e');
-- join test
SELECT COUNT(*) FROM guid1 g1 INNER JOIN guid2 g2 ON g1.guid_field = g2.guid_field;
 count 
-------
     3
(1 row)
 
SELECT COUNT(*) FROM guid1 g1 LEFT JOIN guid2 g2 ON g1.guid_field = g2.guid_field WHERE g2.guid_field IS NULL;
 count 
-------
     1
(1 row)
 
-- generation test
TRUNCATE guid1;
INSERT INTO guid1 (guid_field) VALUES (gen_random_uuid());
INSERT INTO guid1 (guid_field) VALUES (gen_random_uuid());
SELECT count(DISTINCT guid_field) FROM guid1;
 count 
-------
     2
(1 row)
 
-- test of uuidv4() alias
TRUNCATE guid1;
INSERT INTO guid1 (guid_field) VALUES (uuidv4());
INSERT INTO guid1 (guid_field) VALUES (uuidv4());
SELECT count(DISTINCT guid_field) FROM guid1;
 count 
-------
     2
(1 row)
 
-- generation test for v7
TRUNCATE guid1;
INSERT INTO guid1 (guid_field) VALUES (uuidv7());
INSERT INTO guid1 (guid_field) VALUES (uuidv7());
INSERT INTO guid1 (guid_field) VALUES (uuidv7(INTERVAL '1 day'));
SELECT count(DISTINCT guid_field) FROM guid1;
 count 
-------
     3
(1 row)
 
-- test sortability of v7
INSERT INTO guid3 (guid_field) VALUES ('00000000-0000-0000-0000-000000000000'::uuid);
INSERT INTO guid3 (guid_field) SELECT uuidv7() FROM generate_series(1, 10);
INSERT INTO guid3 (guid_field) VALUES ('ffffffff-ffff-ffff-ffff-ffffffffffff'::uuid);
SELECT array_agg(id ORDER BY guid_field) FROM guid3;
          array_agg           
------------------------------
 {1,2,3,4,5,6,7,8,9,10,11,12}
(1 row)
 
-- Test base32hex encoding of UUIDs and its lexicographical sorting property.
-- COLLATE "C" is required to prevent buildfarm failures in non-C locales
-- where natural language collations (such as cs_CZ) would break strict
-- byte-wise ordering.
SELECT array_agg(id ORDER BY guid_encoded COLLATE "C") FROM guid3;
          array_agg           
------------------------------
 {1,2,3,4,5,6,7,8,9,10,11,12}
(1 row)
 
-- Check the timestamp offsets for v7.
--
-- generate UUIDv7 values with timestamps ranging from 1970 (the Unix epoch year)
-- to 10888 (one year before the maximum possible year), and then verify that
-- the extracted timestamps from these UUIDv7 values have not overflowed.
WITH uuidts AS (
    SELECT y, ts as ts, lag(ts) OVER (ORDER BY y) AS prev_ts
    FROM (SELECT y, uuid_extract_timestamp(uuidv7((y || ' years')::interval)) AS ts
        FROM generate_series(1970 - extract(year from now())::int, 10888 - extract(year from now())::int) y)
)
SELECT y, ts, prev_ts FROM uuidts WHERE ts < prev_ts;
 y | ts | prev_ts 
---+----+---------
(0 rows)
 
-- uuidv7: infinite intervals are rejected
SELECT uuidv7('infinity'::interval);
ERROR:  interval out of range for UUID version 7
DETAIL:  UUID version 7 does not support infinite intervals.
SELECT uuidv7('-infinity'::interval);
ERROR:  interval out of range for UUID version 7
DETAIL:  UUID version 7 does not support infinite intervals.
-- uuidv7: timestamps before Unix epoch are rejected
SELECT uuidv7('-1000 years'::interval);
ERROR:  timestamp out of range for UUID version 7
DETAIL:  UUID version 7 supports timestamps from 1970-01-01 to approximately year 10889.
-- uuidv7: timestamps beyond 48-bit ms field (~year 10889) are rejected
SELECT uuidv7('9000 years'::interval);
ERROR:  timestamp out of range for UUID version 7
DETAIL:  UUID version 7 supports timestamps from 1970-01-01 to approximately year 10889.
-- uuidv7: a large but in-range forward shift is accepted
SELECT uuid_extract_timestamp(uuidv7('1000 years'::interval)) > now() + '999 years'::interval;
 ?column? 
----------
 t
(1 row)
 
-- extract functions
-- version
SELECT uuid_extract_version('11111111-1111-5111-8111-111111111111');  -- 5
 uuid_extract_version 
----------------------
                    5
(1 row)
 
SELECT uuid_extract_version(gen_random_uuid());  -- 4
 uuid_extract_version 
----------------------
                    4
(1 row)
 
SELECT uuid_extract_version('11111111-1111-1111-1111-111111111111');  -- null
 uuid_extract_version 
----------------------
                     
(1 row)
 
SELECT uuid_extract_version(uuidv4());  -- 4
 uuid_extract_version 
----------------------
                    4
(1 row)
 
SELECT uuid_extract_version(uuidv7());  -- 7
 uuid_extract_version 
----------------------
                    7
(1 row)
 
-- timestamp
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
 ?column? 
----------
 t
(1 row)
 
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
 ?column? 
----------
 t
(1 row)
 
SELECT uuid_extract_timestamp(gen_random_uuid());  -- null
 uuid_extract_timestamp 
------------------------
 
(1 row)
 
SELECT uuid_extract_timestamp('11111111-1111-1111-1111-111111111111');  -- null
 uuid_extract_timestamp 
------------------------
 
(1 row)
 
-- casts
SELECT '5b35380a-7143-4912-9b55-f322699c6770'::uuid::bytea;
               bytea                
------------------------------------
 \x5b35380a714349129b55f322699c6770
(1 row)
 
SELECT '\x019a2f859ced7225b99d9c55044a2563'::bytea::uuid;
                 uuid                 
--------------------------------------
 019a2f85-9ced-7225-b99d-9c55044a2563
(1 row)
 
SELECT '\x1234567890abcdef'::bytea::uuid; -- error
ERROR:  invalid input length for type uuid
DETAIL:  Expected 16 bytes, got 8.
SELECT v = v::bytea::uuid as matched FROM gen_random_uuid() v;
 matched 
---------
 t
(1 row)
 
-- clean up
DROP TABLE guid1, guid2, guid3 CASCADE;