Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
--
-- one_c_temp_memory_catalog_toast
--
 
SET client_min_messages = warning;
 
SET enable_temp_memory_catalog = off;
 
CREATE TEMP TABLE tmc_toast_phys_parent(
    id integer
);
 
DO $$
DECLARE
    parent_oid oid;
    toast_nsp text;
    toast_oid oid;
    toast_index_oid oid;
    class_rows bigint;
    attr_rows bigint;
    pg_index_rows bigint;
    depend_rows bigint;
BEGIN
    parent_oid := to_regclass('pg_temp.tmc_toast_phys_parent');
 
    IF parent_oid IS NULL THEN
        RAISE EXCEPTION 'tmc_toast_phys_parent is missing';
    END IF;
END;
$$;
 
SET enable_temp_memory_catalog = on;
 
ALTER TABLE pg_temp.tmc_toast_phys_parent
ADD COLUMN payload text;
 
ALTER TABLE pg_temp.tmc_toast_phys_parent
ALTER COLUMN payload SET STORAGE EXTERNAL;
 
DO $$
DECLARE
    parent_oid oid;
    temp_nsp text;
    toast_nsp text;
    toast_oid oid;
    toast_index_oid oid;
    class_rows bigint;
    attr_rows bigint;
    pg_index_rows bigint;
    depend_rows bigint;
BEGIN
    parent_oid := to_regclass('pg_temp.tmc_toast_phys_parent');
 
    IF parent_oid IS NULL THEN
        RAISE EXCEPTION 'tmc_toast_phys_parent disappeared before TOAST checks';
    END IF;
 
    SELECT nspname INTO temp_nsp
    FROM pg_catalog.pg_namespace
    WHERE oid = pg_my_temp_schema();
 
    toast_nsp := replace(temp_nsp, 'pg_temp_', 'pg_toast_temp_');
 
    toast_oid := to_regclass(format('%I.%I', toast_nsp, format('pg_toast_%s', parent_oid)));
    toast_index_oid := to_regclass(format('%I.%I', toast_nsp, format('pg_toast_%s_index', parent_oid)));
 
    IF toast_oid IS NULL OR toast_index_oid IS NULL THEN
        RAISE EXCEPTION
            'physical control TOAST objects failed to resolve';
    END IF;
 
    SELECT count(*) INTO class_rows
    FROM pg_catalog.pg_class
    WHERE oid = toast_oid;
 
    IF class_rows <> 1 THEN
        RAISE EXCEPTION 'unexpected pg_class rows for physical TOAST relation: %',
            class_rows;
    END IF;
 
    SELECT count(*) INTO class_rows
    FROM pg_catalog.pg_class
    WHERE oid = toast_index_oid;
 
    IF class_rows <> 1 THEN
        RAISE EXCEPTION 'unexpected pg_class rows for physical TOAST index: %',
            class_rows;
    END IF;
 
    SELECT count(*) INTO attr_rows
    FROM pg_catalog.pg_attribute
    WHERE attrelid = toast_oid
      AND attnum > 0;
 
    IF attr_rows <= 0 THEN
        RAISE EXCEPTION 'unexpected pg_attribute rows for physical TOAST relation: %',
            attr_rows;
    END IF;
 
    SELECT count(*) INTO attr_rows
    FROM pg_catalog.pg_attribute
    WHERE attrelid = toast_index_oid
      AND attnum > 0;
 
    IF attr_rows <= 0 THEN
        RAISE EXCEPTION 'unexpected pg_attribute rows for physical TOAST index: %',
            attr_rows;
    END IF;
 
    SELECT count(*) INTO pg_index_rows
    FROM pg_catalog.pg_index
    WHERE indexrelid = toast_index_oid;
 
    IF pg_index_rows <> 1 THEN
        RAISE EXCEPTION 'unexpected pg_index rows for physical TOAST index: %',
            pg_index_rows;
    END IF;
 
    SELECT count(*) INTO depend_rows
    FROM pg_catalog.pg_depend
    WHERE classid = 'pg_class'::regclass
      AND objid = toast_oid
      AND refclassid = 'pg_class'::regclass
      AND refobjid = parent_oid
      AND deptype = 'i';
 
    IF depend_rows <> 1 THEN
        RAISE EXCEPTION 'unexpected physical dependency rows for TOAST relation: %',
            depend_rows;
    END IF;
END;
$$;
 
INSERT INTO pg_temp.tmc_toast_phys_parent
VALUES (
    1,
    'phys:' || repeat('P', 36000) || ':tail'
);
 
SELECT length(payload) AS phys_payload_len,
       left(payload, 5) AS phys_prefix,
       right(payload, 5) AS phys_suffix
FROM pg_temp.tmc_toast_phys_parent;
 
DROP TABLE pg_temp.tmc_toast_phys_parent;
 
SET enable_temp_memory_catalog = on;
 
CREATE TEMP TABLE tmc_toast_mem_parent(
    id integer
);
 
DO $$
DECLARE
    parent_oid oid;
    class_rows bigint;
BEGIN
    parent_oid := to_regclass('pg_temp.tmc_toast_mem_parent');
 
    IF parent_oid IS NULL THEN
        RAISE EXCEPTION 'tmc_toast_mem_parent is missing';
    END IF;
 
    SELECT count(*) INTO class_rows
    FROM pg_catalog.pg_class
    WHERE oid = parent_oid;
 
    IF class_rows <> 0 THEN
        RAISE EXCEPTION
            'unexpected physical pg_class rowcount for tmc_toast_mem_parent: got %, expected 0',
            class_rows;
    END IF;
END;
$$;
 
SET enable_temp_memory_catalog = off;
 
CREATE TEMP TABLE tmc_toast_state(
    parent_oid oid,
    toast_oid oid,
    toast_index_oid oid
);
 
ALTER TABLE pg_temp.tmc_toast_mem_parent
ADD COLUMN payload text;
 
ALTER TABLE pg_temp.tmc_toast_mem_parent
ALTER COLUMN payload SET STORAGE EXTERNAL;
 
SELECT to_regclass('pg_temp.tmc_toast_mem_parent') IS NOT NULL AS mem_parent_visible;
 
DO $$
DECLARE
    parent_oid oid;
    temp_nsp text;
    toast_nsp text;
    toast_oid oid;
    toast_index_oid oid;
    index_def text;
    class_rows bigint;
    attr_rows bigint;
    pg_index_rows bigint;
    depend_rows bigint;
BEGIN
    parent_oid := to_regclass('pg_temp.tmc_toast_mem_parent');
 
    IF parent_oid IS NULL THEN
        RAISE EXCEPTION 'tmc_toast_mem_parent disappeared before TOAST checks';
    END IF;
 
    SELECT nspname INTO temp_nsp
    FROM pg_catalog.pg_namespace
    WHERE oid = pg_my_temp_schema();
 
    toast_nsp := replace(temp_nsp, 'pg_temp_', 'pg_toast_temp_');
 
    toast_oid := to_regclass(format('%I.%I', toast_nsp, format('pg_toast_%s', parent_oid)));
    toast_index_oid := to_regclass(format('%I.%I', toast_nsp, format('pg_toast_%s_index', parent_oid)));
 
    IF toast_oid IS NULL OR toast_index_oid IS NULL THEN
        RAISE EXCEPTION 'memory-backed TOAST objects failed to resolve';
    END IF;
 
    index_def := pg_get_indexdef(toast_index_oid);
    IF index_def IS NULL
       OR position('UNIQUE INDEX ' IN index_def) = 0
       OR position(' USING btree (chunk_id, chunk_seq)' IN index_def) = 0 THEN
        RAISE EXCEPTION 'unexpected TOAST index definition visibility';
    END IF;
 
    SELECT count(*) INTO class_rows
    FROM pg_catalog.pg_class
    WHERE oid = toast_oid;
 
    SELECT count(*) INTO attr_rows
    FROM pg_catalog.pg_attribute
    WHERE attrelid = toast_oid
      AND attnum > 0;
 
    SELECT count(*) INTO pg_index_rows
    FROM pg_catalog.pg_index
    WHERE indexrelid = toast_index_oid;
 
    SELECT count(*) INTO depend_rows
    FROM pg_catalog.pg_depend
    WHERE classid = 'pg_class'::regclass
      AND objid = toast_oid
      AND refclassid = 'pg_class'::regclass
      AND refobjid = parent_oid
      AND deptype = 'i';
 
    IF class_rows <> 0 OR attr_rows <> 0 OR pg_index_rows <> 0 OR depend_rows <> 0 THEN
        RAISE EXCEPTION
            'unexpected physical spill for memory TOAST relation: class %, attr %, pg_index %, depend %',
            class_rows, attr_rows, pg_index_rows, depend_rows;
    END IF;
 
    SELECT count(*) INTO class_rows
    FROM pg_catalog.pg_class
    WHERE oid = toast_index_oid;
 
    SELECT count(*) INTO attr_rows
    FROM pg_catalog.pg_attribute
    WHERE attrelid = toast_index_oid
      AND attnum > 0;
 
    IF class_rows <> 0 OR attr_rows <> 0 THEN
        RAISE EXCEPTION
            'unexpected physical spill for memory TOAST index: class %, attr %',
            class_rows, attr_rows;
    END IF;
 
    INSERT INTO pg_temp.tmc_toast_state
    VALUES (parent_oid, toast_oid, toast_index_oid);
 
    SELECT count(*) INTO class_rows
    FROM pg_temp.tmc_toast_state AS s
    WHERE s.parent_oid IS NOT NULL
      AND s.toast_oid IS NOT NULL
      AND s.toast_index_oid IS NOT NULL;
 
    IF class_rows <> 1 THEN
        RAISE EXCEPTION 'known-good TOAST state capture failed';
    END IF;
END;
$$;
 
INSERT INTO pg_temp.tmc_toast_mem_parent
VALUES (
    1,
    'mem:' || repeat('M', 36000) || ':tail'
);
 
SELECT length(payload) AS mem_payload_len,
       left(payload, 4) AS mem_prefix,
       right(payload, 5) AS mem_suffix
FROM pg_temp.tmc_toast_mem_parent;
 
UPDATE pg_temp.tmc_toast_mem_parent
SET payload = 'upd:' || repeat('U', 37000) || ':done'
WHERE id = 1;
 
SELECT length(payload) AS mem_updated_payload_len,
       left(payload, 4) AS mem_updated_prefix,
       right(payload, 5) AS mem_updated_suffix
FROM pg_temp.tmc_toast_mem_parent;
 
SET enable_temp_memory_catalog = on;
 
CREATE TEMP TABLE tmc_toast_rb_parent(
    id integer
);
 
INSERT INTO pg_temp.tmc_toast_rb_parent VALUES (1);
 
SET enable_temp_memory_catalog = off;
 
BEGIN;
SAVEPOINT tmc_toast_rb_s;
 
ALTER TABLE pg_temp.tmc_toast_rb_parent
ADD COLUMN payload text;
 
DO $$
DECLARE
    parent_oid oid;
    temp_nsp text;
    toast_nsp text;
    toast_oid oid;
    toast_index_oid oid;
BEGIN
    parent_oid := to_regclass('pg_temp.tmc_toast_rb_parent');
 
    IF parent_oid IS NULL THEN
        RAISE EXCEPTION 'tmc_toast_rb_parent is missing inside rollback test';
    END IF;
 
    SELECT nspname INTO temp_nsp
    FROM pg_catalog.pg_namespace
    WHERE oid = pg_my_temp_schema();
 
    toast_nsp := replace(temp_nsp, 'pg_temp_', 'pg_toast_temp_');
 
    toast_oid := to_regclass(format('%I.%I', toast_nsp, format('pg_toast_%s', parent_oid)));
    toast_index_oid := to_regclass(format('%I.%I', toast_nsp, format('pg_toast_%s_index', parent_oid)));
 
    IF toast_oid IS NULL OR toast_index_oid IS NULL THEN
        RAISE EXCEPTION 'rollback TOAST objects failed to resolve inside savepoint';
    END IF;
END;
$$;
 
SELECT to_regclass('pg_temp.tmc_toast_rb_parent') IS NOT NULL AS rb_parent_visible_inside;
 
ROLLBACK TO SAVEPOINT tmc_toast_rb_s;
RELEASE SAVEPOINT tmc_toast_rb_s;
COMMIT;
 
DO $$
DECLARE
    saved_parent_oid oid;
    saved_toast_oid oid;
    saved_toast_index_oid oid;
    parent_oid oid;
    temp_nsp text;
    toast_nsp text;
    toast_oid oid;
    toast_index_oid oid;
    logical_columns text[];
    class_rows bigint;
    payload_len integer;
    payload_prefix text;
    payload_suffix text;
BEGIN
    parent_oid := to_regclass('pg_temp.tmc_toast_rb_parent');
 
    IF parent_oid IS NULL THEN
        RAISE EXCEPTION 'tmc_toast_rb_parent is missing after rollback';
    END IF;
 
    SELECT array_agg(keys.key ORDER BY keys.key)
    INTO logical_columns
    FROM pg_temp.tmc_toast_rb_parent AS t
    CROSS JOIN LATERAL
        jsonb_object_keys(to_jsonb(t)) AS keys(key);
 
    IF logical_columns IS DISTINCT FROM ARRAY['id']::text[] THEN
        RAISE EXCEPTION 'unexpected rollback logical columns: %',
            logical_columns;
    END IF;
 
    SELECT nspname INTO temp_nsp
    FROM pg_catalog.pg_namespace
    WHERE oid = pg_my_temp_schema();
 
    toast_nsp := replace(temp_nsp, 'pg_temp_', 'pg_toast_temp_');
 
    toast_oid := to_regclass(format('%I.%I', toast_nsp, format('pg_toast_%s', parent_oid)));
    toast_index_oid := to_regclass(format('%I.%I', toast_nsp, format('pg_toast_%s_index', parent_oid)));
 
    IF toast_oid IS NOT NULL OR toast_index_oid IS NOT NULL THEN
        RAISE EXCEPTION 'rolled back TOAST objects still resolve';
    END IF;
 
    SELECT count(*) INTO class_rows
    FROM pg_catalog.pg_class
    WHERE relname = format('pg_toast_%s', parent_oid);
 
    IF class_rows <> 0 THEN
        RAISE EXCEPTION 'rolled back TOAST relation spilled physically: %',
            class_rows;
    END IF;
 
    SELECT count(*) INTO class_rows
    FROM pg_catalog.pg_class
    WHERE relname = format('pg_toast_%s_index', parent_oid);
 
    IF class_rows <> 0 THEN
        RAISE EXCEPTION 'rolled back TOAST index spilled physically: %',
            class_rows;
    END IF;
 
    SELECT count(*),
           min(s.parent_oid),
           min(s.toast_oid),
           min(s.toast_index_oid)
    INTO class_rows,
         saved_parent_oid,
         saved_toast_oid,
         saved_toast_index_oid
    FROM pg_temp.tmc_toast_state AS s
    WHERE s.parent_oid IS NOT NULL
      AND s.toast_oid IS NOT NULL
      AND s.toast_index_oid IS NOT NULL;
 
    IF class_rows <> 1 THEN
        RAISE EXCEPTION 'committed memory TOAST state row is missing';
    END IF;
 
    toast_oid := to_regclass(
        format('%I.%I', toast_nsp, format('pg_toast_%s', saved_parent_oid)));
    toast_index_oid := to_regclass(
        format('%I.%I', toast_nsp, format('pg_toast_%s_index', saved_parent_oid)));
 
    IF toast_oid IS DISTINCT FROM saved_toast_oid THEN
        RAISE EXCEPTION
            'committed memory TOAST relation was damaged by unrelated subtransaction rollback';
    END IF;
 
    IF toast_index_oid IS DISTINCT FROM saved_toast_index_oid THEN
        RAISE EXCEPTION
            'committed memory TOAST index was damaged by unrelated subtransaction rollback';
    END IF;
 
    SELECT length(payload),
           left(payload, 4),
           right(payload, 5)
    INTO payload_len,
         payload_prefix,
         payload_suffix
    FROM pg_temp.tmc_toast_mem_parent
    WHERE id = 1;
 
    IF payload_len <> 37009
       OR payload_prefix <> 'upd:'
       OR payload_suffix <> ':done' THEN
        RAISE EXCEPTION
            'committed oversized TOAST payload became unusable after unrelated rollback';
    END IF;
END;
$$;
 
SET enable_temp_memory_catalog = off;
 
DROP TABLE pg_temp.tmc_toast_mem_parent;
 
DO $$
DECLARE
    saved_parent_oid oid;
    saved_toast_oid oid;
    saved_toast_index_oid oid;
    temp_nsp text;
    toast_nsp text;
    toast_class_rows bigint;
    toast_attr_rows bigint;
    index_class_rows bigint;
    index_attr_rows bigint;
    pg_index_rows bigint;
BEGIN
    SELECT count(*),
           min(s.parent_oid),
           min(s.toast_oid),
           min(s.toast_index_oid)
    INTO toast_class_rows,
         saved_parent_oid,
         saved_toast_oid,
         saved_toast_index_oid
    FROM pg_temp.tmc_toast_state AS s
    WHERE s.parent_oid IS NOT NULL
      AND s.toast_oid IS NOT NULL
      AND s.toast_index_oid IS NOT NULL;
 
    IF toast_class_rows <> 1 THEN
        RAISE EXCEPTION 'saved TOAST cleanup state is missing';
    END IF;
 
    IF to_regclass('pg_temp.tmc_toast_mem_parent') IS NOT NULL THEN
        RAISE EXCEPTION 'tmc_toast_mem_parent still resolves after drop';
    END IF;
 
    SELECT nspname INTO temp_nsp
    FROM pg_catalog.pg_namespace
    WHERE oid = pg_my_temp_schema();
 
    toast_nsp := replace(temp_nsp, 'pg_temp_', 'pg_toast_temp_');
 
    IF to_regclass(format('%I.%I', toast_nsp, format('pg_toast_%s', saved_parent_oid))) IS NOT NULL
       OR to_regclass(format('%I.%I', toast_nsp, format('pg_toast_%s_index', saved_parent_oid))) IS NOT NULL THEN
        RAISE EXCEPTION 'dropped memory TOAST objects still resolve';
    END IF;
 
    SELECT count(*) INTO toast_class_rows
    FROM pg_catalog.pg_class
    WHERE oid = saved_toast_oid;
 
    SELECT count(*) INTO toast_attr_rows
    FROM pg_catalog.pg_attribute
    WHERE attrelid = saved_toast_oid
      AND attnum > 0;
 
    SELECT count(*) INTO index_class_rows
    FROM pg_catalog.pg_class
    WHERE oid = saved_toast_index_oid;
 
    SELECT count(*) INTO index_attr_rows
    FROM pg_catalog.pg_attribute
    WHERE attrelid = saved_toast_index_oid
      AND attnum > 0;
 
    SELECT count(*) INTO pg_index_rows
    FROM pg_catalog.pg_index
    WHERE indexrelid = saved_toast_index_oid;
 
    IF toast_class_rows <> 0
       OR toast_attr_rows <> 0
       OR index_class_rows <> 0
       OR index_attr_rows <> 0
       OR pg_index_rows <> 0 THEN
        RAISE EXCEPTION
            'dropped memory TOAST metadata spilled physically: toast class %, toast attr %, index class %, index attr %, pg_index %',
            toast_class_rows, toast_attr_rows, index_class_rows, index_attr_rows, pg_index_rows;
    END IF;
END;
$$;
 
SELECT to_regclass('pg_temp.tmc_toast_mem_parent') IS NULL AS mem_parent_dropped;
 
DROP TABLE pg_temp.tmc_toast_rb_parent;
DROP TABLE pg_temp.tmc_toast_state;
 
RESET enable_temp_memory_catalog;