Исходный код СУБД 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
--
-- one_c_temp_memory_catalog_xact
--
 
SET client_min_messages = warning;
SET enable_temp_memory_catalog = on;
 
BEGIN;
SAVEPOINT tmc_xact_sp;
CREATE TEMP TABLE tmc_xact_sp_table(id integer);
INSERT INTO pg_temp.tmc_xact_sp_table VALUES (1);
ROLLBACK TO SAVEPOINT tmc_xact_sp;
RELEASE SAVEPOINT tmc_xact_sp;
COMMIT;
 
SELECT to_regclass('pg_temp.tmc_xact_sp_table') IS NULL AS sp_table_missing;
 
BEGIN;
SAVEPOINT tmc_xact_create_outer;
SAVEPOINT tmc_xact_create_inner;
CREATE TEMP TABLE tmc_xact_create_rb(id integer, payload integer);
INSERT INTO pg_temp.tmc_xact_create_rb VALUES (10, 20);
RELEASE SAVEPOINT tmc_xact_create_inner;
SELECT id, payload
FROM pg_temp.tmc_xact_create_rb;
ROLLBACK TO SAVEPOINT tmc_xact_create_outer;
RELEASE SAVEPOINT tmc_xact_create_outer;
COMMIT;
 
SELECT to_regclass('pg_temp.tmc_xact_create_rb') IS NULL AS create_rb_missing;
 
BEGIN;
SAVEPOINT tmc_xact_create_commit_outer;
SAVEPOINT tmc_xact_create_commit_inner;
CREATE TEMP TABLE tmc_xact_create_commit(id integer, payload integer);
INSERT INTO pg_temp.tmc_xact_create_commit VALUES (11, 22);
RELEASE SAVEPOINT tmc_xact_create_commit_inner;
RELEASE SAVEPOINT tmc_xact_create_commit_outer;
COMMIT;
 
SELECT id, payload
FROM pg_temp.tmc_xact_create_commit;
 
DROP TABLE pg_temp.tmc_xact_create_commit;
 
SET enable_temp_memory_catalog = on;
CREATE TEMP TABLE tmc_xact_update(
    a integer,
    keep integer
);
INSERT INTO pg_temp.tmc_xact_update VALUES (1, 101);
SET enable_temp_memory_catalog = off;
 
BEGIN;
SAVEPOINT tmc_xact_update_outer;
ALTER TABLE pg_temp.tmc_xact_update
RENAME COLUMN a TO a_outer;
SAVEPOINT tmc_xact_update_inner;
ALTER TABLE pg_temp.tmc_xact_update
RENAME COLUMN a_outer TO a_inner;
RELEASE SAVEPOINT tmc_xact_update_inner;
SELECT a_inner, keep
FROM pg_temp.tmc_xact_update;
ROLLBACK TO SAVEPOINT tmc_xact_update_outer;
RELEASE SAVEPOINT tmc_xact_update_outer;
COMMIT;
 
DO $$
DECLARE
    logical_columns text[];
BEGIN
    IF to_regclass('pg_temp.tmc_xact_update') IS NULL THEN
        RAISE EXCEPTION 'tmc_xact_update is missing after nested rollback';
    END IF;
 
    SELECT array_agg(keys.key ORDER BY keys.key)
    INTO logical_columns
    FROM pg_temp.tmc_xact_update AS t
    CROSS JOIN LATERAL
        jsonb_object_keys(to_jsonb(t)) AS keys(key);
 
    IF logical_columns IS DISTINCT FROM ARRAY['a', 'keep']::text[] THEN
        RAISE EXCEPTION 'unexpected nested rollback logical columns: %',
            logical_columns;
    END IF;
END;
$$;
 
SELECT a, keep
FROM pg_temp.tmc_xact_update;
 
DROP TABLE pg_temp.tmc_xact_update;
 
SET enable_temp_memory_catalog = on;
CREATE TEMP TABLE tmc_xact_update_commit(
    a integer,
    keep integer
);
INSERT INTO pg_temp.tmc_xact_update_commit VALUES (2, 202);
SET enable_temp_memory_catalog = off;
 
BEGIN;
SAVEPOINT tmc_xact_update_commit_outer;
ALTER TABLE pg_temp.tmc_xact_update_commit
RENAME COLUMN a TO a_outer;
SAVEPOINT tmc_xact_update_commit_inner;
ALTER TABLE pg_temp.tmc_xact_update_commit
RENAME COLUMN a_outer TO a_inner;
RELEASE SAVEPOINT tmc_xact_update_commit_inner;
RELEASE SAVEPOINT tmc_xact_update_commit_outer;
COMMIT;
 
DO $$
DECLARE
    logical_columns text[];
BEGIN
    IF to_regclass('pg_temp.tmc_xact_update_commit') IS NULL THEN
        RAISE EXCEPTION 'tmc_xact_update_commit is missing after nested commit';
    END IF;
 
    SELECT array_agg(keys.key ORDER BY keys.key)
    INTO logical_columns
    FROM pg_temp.tmc_xact_update_commit AS t
    CROSS JOIN LATERAL
        jsonb_object_keys(to_jsonb(t)) AS keys(key);
 
    IF logical_columns IS DISTINCT FROM ARRAY['a_inner', 'keep']::text[] THEN
        RAISE EXCEPTION 'unexpected nested commit logical columns: %',
            logical_columns;
    END IF;
END;
$$;
 
SELECT a_inner, keep
FROM pg_temp.tmc_xact_update_commit;
 
DROP TABLE pg_temp.tmc_xact_update_commit;
 
SET enable_temp_memory_catalog = on;
CREATE TEMP TABLE tmc_xact_drop_rb(id integer, payload integer);
INSERT INTO pg_temp.tmc_xact_drop_rb VALUES (3, 303);
SET enable_temp_memory_catalog = off;
 
BEGIN;
SAVEPOINT tmc_xact_drop_outer;
SAVEPOINT tmc_xact_drop_inner;
DROP TABLE pg_temp.tmc_xact_drop_rb;
RELEASE SAVEPOINT tmc_xact_drop_inner;
SELECT to_regclass('pg_temp.tmc_xact_drop_rb') IS NULL AS drop_rb_missing_inside;
ROLLBACK TO SAVEPOINT tmc_xact_drop_outer;
RELEASE SAVEPOINT tmc_xact_drop_outer;
COMMIT;
 
SELECT id, payload
FROM pg_temp.tmc_xact_drop_rb;
 
DROP TABLE pg_temp.tmc_xact_drop_rb;
 
SET enable_temp_memory_catalog = on;
 
BEGIN;
SAVEPOINT tmc_xact_create_drop_s;
CREATE TEMP TABLE tmc_xact_create_drop(id integer, payload integer);
INSERT INTO pg_temp.tmc_xact_create_drop VALUES (4, 404);
DROP TABLE pg_temp.tmc_xact_create_drop;
ROLLBACK TO SAVEPOINT tmc_xact_create_drop_s;
RELEASE SAVEPOINT tmc_xact_create_drop_s;
COMMIT;
 
SELECT to_regclass('pg_temp.tmc_xact_create_drop') IS NULL AS create_drop_missing;
 
SET enable_temp_memory_catalog = on;
CREATE TEMP TABLE tmc_xact_top_update(
    original integer,
    keep integer
);
INSERT INTO pg_temp.tmc_xact_top_update VALUES (5, 505);
SET enable_temp_memory_catalog = off;
 
BEGIN;
ALTER TABLE pg_temp.tmc_xact_top_update
RENAME COLUMN original TO temporary_name;
ROLLBACK;
 
SELECT original, keep
FROM pg_temp.tmc_xact_top_update;
 
DROP TABLE pg_temp.tmc_xact_top_update;
 
SET enable_temp_memory_catalog = on;
CREATE TEMP TABLE tmc_xact_top_drop(id integer, payload integer);
INSERT INTO pg_temp.tmc_xact_top_drop VALUES (6, 606);
SET enable_temp_memory_catalog = off;
 
BEGIN;
DROP TABLE pg_temp.tmc_xact_top_drop;
ROLLBACK;
 
SELECT id, payload
FROM pg_temp.tmc_xact_top_drop;
 
DROP TABLE pg_temp.tmc_xact_top_drop;
 
SET enable_temp_memory_catalog = on;
 
DO $$
DECLARE
    aborted_oid oid;
    class_rows bigint;
    attr_rows bigint;
    type_rows bigint;
BEGIN
    BEGIN
        EXECUTE 'CREATE TEMP TABLE tmc_xact_pl_create(id integer, payload integer)';
        EXECUTE 'INSERT INTO pg_temp.tmc_xact_pl_create VALUES (7, 707)';
        aborted_oid := to_regclass('pg_temp.tmc_xact_pl_create');
 
        IF aborted_oid IS NULL THEN
            RAISE EXCEPTION 'tmc_xact_pl_create is missing before forced rollback';
        END IF;
 
        RAISE EXCEPTION USING ERRCODE = 'P9001', MESSAGE = 'forced rollback';
    EXCEPTION
        WHEN SQLSTATE 'P9001' THEN
            NULL;
    END;
 
    IF to_regclass('pg_temp.tmc_xact_pl_create') IS NOT NULL THEN
        RAISE EXCEPTION 'tmc_xact_pl_create survived handled exception';
    END IF;
 
    SELECT count(*) INTO class_rows
    FROM pg_catalog.pg_class
    WHERE oid = aborted_oid;
 
    SELECT count(*) INTO attr_rows
    FROM pg_catalog.pg_attribute
    WHERE attrelid = aborted_oid;
 
    SELECT count(*) INTO type_rows
    FROM pg_catalog.pg_type
    WHERE typrelid = aborted_oid;
 
    IF class_rows <> 0 OR attr_rows <> 0 OR type_rows <> 0 THEN
        RAISE EXCEPTION 'aborted CREATE left physical catalog rows: class %, attr %, type %',
            class_rows, attr_rows, type_rows;
    END IF;
END;
$$;
 
DO $$
DECLARE
    i integer;
BEGIN
    FOR i IN 1..20 LOOP
        BEGIN
            BEGIN
                EXECUTE 'CREATE TEMP TABLE tmc_xact_pl_stress(id integer)';
                EXECUTE format('INSERT INTO pg_temp.tmc_xact_pl_stress VALUES (%s)', i);
                RAISE EXCEPTION USING ERRCODE = 'P9001', MESSAGE = 'forced rollback';
            EXCEPTION
                WHEN SQLSTATE 'P9001' THEN
                    NULL;
            END;
 
            IF to_regclass('pg_temp.tmc_xact_pl_stress') IS NOT NULL THEN
                RAISE EXCEPTION 'tmc_xact_pl_stress survived iteration %', i;
            END IF;
        END;
    END LOOP;
END;
$$;
 
SET enable_temp_memory_catalog = on;
CREATE TEMP TABLE tmc_xact_update_stress(
    value integer,
    keep integer
);
INSERT INTO pg_temp.tmc_xact_update_stress VALUES (8, 808);
SET enable_temp_memory_catalog = off;
 
DO $$
DECLARE
    i integer;
    observed_value integer;
    observed_keep integer;
BEGIN
    FOR i IN 1..20 LOOP
        BEGIN
            BEGIN
                EXECUTE 'ALTER TABLE pg_temp.tmc_xact_update_stress RENAME COLUMN value TO value_tmp';
                RAISE EXCEPTION USING ERRCODE = 'P9001', MESSAGE = 'forced rollback';
            EXCEPTION
                WHEN SQLSTATE 'P9001' THEN
                    NULL;
            END;
 
            SELECT value, keep
            INTO observed_value, observed_keep
            FROM pg_temp.tmc_xact_update_stress;
 
            IF observed_value <> 8 OR observed_keep <> 808 THEN
                RAISE EXCEPTION 'unexpected update stress row after iteration %: value %, keep %',
                    i, observed_value, observed_keep;
            END IF;
        END;
    END LOOP;
END;
$$;
 
SELECT value, keep
FROM pg_temp.tmc_xact_update_stress;
 
DROP TABLE pg_temp.tmc_xact_update_stress;
 
SET enable_temp_memory_catalog = on;
CREATE TEMP TABLE tmc_xact_drop_stress(id integer, payload integer);
INSERT INTO pg_temp.tmc_xact_drop_stress VALUES (9, 909);
SET enable_temp_memory_catalog = off;
 
DO $$
DECLARE
    i integer;
    observed_count integer;
    observed_payload integer;
BEGIN
    FOR i IN 1..10 LOOP
        BEGIN
            BEGIN
                EXECUTE 'DROP TABLE pg_temp.tmc_xact_drop_stress';
                RAISE EXCEPTION USING ERRCODE = 'P9001', MESSAGE = 'forced rollback';
            EXCEPTION
                WHEN SQLSTATE 'P9001' THEN
                    NULL;
            END;
 
            SELECT count(*), sum(payload)::integer
            INTO observed_count, observed_payload
            FROM pg_temp.tmc_xact_drop_stress;
 
            IF observed_count <> 1 OR observed_payload <> 909 THEN
                RAISE EXCEPTION 'unexpected drop stress state after iteration %: count %, payload %',
                    i, observed_count, observed_payload;
            END IF;
        END;
    END LOOP;
END;
$$;
 
SELECT id, payload
FROM pg_temp.tmc_xact_drop_stress;
 
DROP TABLE pg_temp.tmc_xact_drop_stress;
 
SET enable_temp_memory_catalog = on;
CREATE TEMP TABLE tmc_xact_inplace(id integer, payload integer);
INSERT INTO pg_temp.tmc_xact_inplace
SELECT g, g * 10
FROM generate_series(1, 32) AS g;
SET enable_temp_memory_catalog = off;
 
DO $$
BEGIN
    BEGIN
        EXECUTE 'ANALYZE pg_temp.tmc_xact_inplace';
        RAISE EXCEPTION USING ERRCODE = 'P9001', MESSAGE = 'forced rollback';
    EXCEPTION
        WHEN SQLSTATE 'P9001' THEN
            NULL;
    END;
END;
$$;
 
ALTER TABLE pg_temp.tmc_xact_inplace
RENAME TO tmc_xact_inplace_renamed;
 
DO $$
DECLARE
    plan json;
BEGIN
    EXECUTE 'EXPLAIN (FORMAT JSON) SELECT * FROM pg_temp.tmc_xact_inplace_renamed'
        INTO plan;
 
    IF (plan->0->'Plan'->>'Plan Rows')::integer <> 32 THEN
        RAISE EXCEPTION 'unexpected inplace rollback plan rows: got %, expected 32',
            plan->0->'Plan'->>'Plan Rows';
    END IF;
END;
$$;
 
DROP TABLE pg_temp.tmc_xact_inplace_renamed;
 
SET enable_temp_memory_catalog = on;
 
BEGIN;
CREATE TEMP TABLE tmc_xact_on_commit_drop(
    id integer,
    payload integer
) ON COMMIT DROP;
INSERT INTO pg_temp.tmc_xact_on_commit_drop VALUES (10, 1000);
COMMIT;
 
SELECT to_regclass('pg_temp.tmc_xact_on_commit_drop') IS NULL AS on_commit_drop_missing;
 
BEGIN;
CREATE TEMP TABLE tmc_xact_on_commit_delete(
    id integer,
    payload integer
) ON COMMIT DELETE ROWS;
INSERT INTO pg_temp.tmc_xact_on_commit_delete VALUES (1, 10), (2, 20), (3, 30);
COMMIT;
 
SELECT count(*) AS on_commit_delete_count
FROM pg_temp.tmc_xact_on_commit_delete;
 
DO $$
DECLARE
    rel_oid oid;
    class_rows bigint;
    attr_rows bigint;
BEGIN
    rel_oid := to_regclass('pg_temp.tmc_xact_on_commit_delete');
    IF rel_oid IS NULL THEN
        RAISE EXCEPTION 'tmc_xact_on_commit_delete is missing after commit';
    END IF;
 
    SELECT count(*) INTO class_rows
    FROM pg_catalog.pg_class
    WHERE oid = rel_oid;
 
    SELECT count(*) INTO attr_rows
    FROM pg_catalog.pg_attribute
    WHERE attrelid = rel_oid
      AND attnum > 0
      AND NOT attisdropped;
 
    IF class_rows <> 0 OR attr_rows <> 0 THEN
        RAISE EXCEPTION 'unexpected physical catalog rows for surviving memory table: class %, attr %',
            class_rows, attr_rows;
    END IF;
END;
$$;
 
DROP TABLE pg_temp.tmc_xact_on_commit_delete;
 
DO $$
DECLARE
    relname text;
BEGIN
    FOREACH relname IN ARRAY ARRAY[
        'tmc_xact_sp_table',
        'tmc_xact_create_rb',
        'tmc_xact_create_commit',
        'tmc_xact_update',
        'tmc_xact_update_commit',
        'tmc_xact_drop_rb',
        'tmc_xact_create_drop',
        'tmc_xact_top_update',
        'tmc_xact_top_drop',
        'tmc_xact_pl_create',
        'tmc_xact_pl_stress',
        'tmc_xact_update_stress',
        'tmc_xact_drop_stress',
        'tmc_xact_inplace',
        'tmc_xact_inplace_renamed',
        'tmc_xact_on_commit_drop',
        'tmc_xact_on_commit_delete'
    ] LOOP
        IF to_regclass(format('pg_temp.%s', relname)) IS NOT NULL THEN
            RAISE EXCEPTION 'leftover temp relation: %', relname;
        END IF;
    END LOOP;
END;
$$;
 
RESET enable_temp_memory_catalog;