Исходный код СУБД 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
--
-- one_c_temp_memory_catalog_index
--
 
SET client_min_messages = warning;
 
SET enable_temp_memory_catalog = off;
 
CREATE TEMP TABLE tmc_index_phys_parent(
    id integer,
    payload integer
);
 
INSERT INTO pg_temp.tmc_index_phys_parent
SELECT g, g * 10
FROM generate_series(1, 8) AS g;
 
SET enable_temp_memory_catalog = on;
 
CREATE INDEX tmc_index_phys_idx
ON pg_temp.tmc_index_phys_parent USING btree (id);
 
DO $$
DECLARE
    index_oid oid;
    class_rows bigint;
    attr_rows bigint;
    pg_index_rows bigint;
BEGIN
    index_oid := to_regclass('pg_temp.tmc_index_phys_idx');
 
    IF index_oid IS NULL THEN
        RAISE EXCEPTION 'tmc_index_phys_idx is missing';
    END IF;
 
    SELECT count(*) INTO class_rows
    FROM pg_catalog.pg_class
    WHERE oid = index_oid;
 
    SELECT count(*) INTO attr_rows
    FROM pg_catalog.pg_attribute
    WHERE attrelid = index_oid
      AND attnum > 0;
 
    SELECT count(*) INTO pg_index_rows
    FROM pg_catalog.pg_index
    WHERE indexrelid = index_oid;
 
    IF class_rows <> 1 OR attr_rows <= 0 OR pg_index_rows <> 1 THEN
        RAISE EXCEPTION
            'unexpected physical control rows for tmc_index_phys_idx: class %, attr %, pg_index %',
            class_rows, attr_rows, pg_index_rows;
    END IF;
END;
$$;
 
DROP INDEX pg_temp.tmc_index_phys_idx;
DROP TABLE pg_temp.tmc_index_phys_parent;
 
SET enable_temp_memory_catalog = on;
 
CREATE TEMP TABLE tmc_index_mem_parent(
    id integer,
    payload integer
);
 
INSERT INTO pg_temp.tmc_index_mem_parent
SELECT g, g * 100
FROM generate_series(1, 16) AS g;
 
DO $$
DECLARE
    table_oid oid;
    class_rows bigint;
BEGIN
    table_oid := to_regclass('pg_temp.tmc_index_mem_parent');
 
    IF table_oid IS NULL THEN
        RAISE EXCEPTION 'tmc_index_mem_parent is missing';
    END IF;
 
    SELECT count(*) INTO class_rows
    FROM pg_catalog.pg_class
    WHERE oid = table_oid;
 
    IF class_rows <> 0 THEN
        RAISE EXCEPTION
            'unexpected physical pg_class rowcount for tmc_index_mem_parent: got %, expected 0',
            class_rows;
    END IF;
END;
$$;
 
SET enable_temp_memory_catalog = off;
 
CREATE INDEX tmc_index_mem_idx
ON pg_temp.tmc_index_mem_parent USING btree (id);
 
SELECT to_regclass('pg_temp.tmc_index_mem_idx') IS NOT NULL AS mem_idx_visible;
 
DO $$
DECLARE
    index_oid oid;
    index_def text;
    plan json;
    node_type text;
    index_name text;
BEGIN
    index_oid := to_regclass('pg_temp.tmc_index_mem_idx');
 
    IF index_oid IS NULL THEN
        RAISE EXCEPTION 'tmc_index_mem_idx is missing';
    END IF;
 
    index_def := pg_get_indexdef(index_oid);
    IF position('CREATE INDEX tmc_index_mem_idx ON ' IN index_def) <> 1
       OR position(' USING btree (id)' IN index_def) = 0 THEN
        RAISE EXCEPTION 'unexpected index definition for tmc_index_mem_idx: %',
            index_def;
    END IF;
 
    PERFORM set_config('enable_seqscan', 'off', true);
    PERFORM set_config('enable_bitmapscan', 'off', true);
 
    EXECUTE
        'EXPLAIN (FORMAT JSON) SELECT payload FROM pg_temp.tmc_index_mem_parent WHERE id = 7'
        INTO plan;
 
    node_type := plan->0->'Plan'->>'Node Type';
    index_name := plan->0->'Plan'->>'Index Name';
 
    IF node_type NOT IN ('Index Scan', 'Index Only Scan') THEN
        RAISE EXCEPTION 'unexpected node type for tmc_index_mem_idx plan: %',
            node_type;
    END IF;
 
    IF index_name <> 'tmc_index_mem_idx' THEN
        RAISE EXCEPTION 'unexpected index name for tmc_index_mem_idx plan: %',
            index_name;
    END IF;
END;
$$;
 
SELECT payload
FROM pg_temp.tmc_index_mem_parent
WHERE id = 7;
 
RESET enable_seqscan;
RESET enable_bitmapscan;
 
DO $$
DECLARE
    index_oid oid;
    class_rows bigint;
    attr_rows bigint;
    pg_index_rows bigint;
    depend_rows bigint;
BEGIN
    index_oid := to_regclass('pg_temp.tmc_index_mem_idx');
 
    IF index_oid IS NULL THEN
        RAISE EXCEPTION 'tmc_index_mem_idx disappeared before spill checks';
    END IF;
 
    SELECT count(*) INTO class_rows
    FROM pg_catalog.pg_class
    WHERE oid = index_oid;
 
    SELECT count(*) INTO attr_rows
    FROM pg_catalog.pg_attribute
    WHERE attrelid = index_oid
      AND attnum > 0;
 
    SELECT count(*) INTO pg_index_rows
    FROM pg_catalog.pg_index
    WHERE indexrelid = index_oid;
 
    SELECT count(*) INTO depend_rows
    FROM pg_catalog.pg_depend
    WHERE classid = 'pg_class'::regclass
      AND objid = index_oid;
 
    IF class_rows <> 0 OR attr_rows <> 0 OR pg_index_rows <> 0 OR depend_rows <> 0 THEN
        RAISE EXCEPTION
            'unexpected physical spill for tmc_index_mem_idx: class %, attr %, pg_index %, depend %',
            class_rows, attr_rows, pg_index_rows, depend_rows;
    END IF;
END;
$$;
 
CREATE INDEX tmc_index_mem_multi
ON pg_temp.tmc_index_mem_parent USING btree (id, payload);
 
SELECT to_regtype('pg_temp.tmc_index_mem_multi') IS NOT NULL AS mem_multi_type_visible;
 
DO $$
DECLARE
    index_oid oid;
    type_oid oid;
    type_rows bigint;
    class_rows bigint;
    attr_rows bigint;
    pg_index_rows bigint;
BEGIN
    index_oid := to_regclass('pg_temp.tmc_index_mem_multi');
    type_oid := to_regtype('pg_temp.tmc_index_mem_multi');
 
    IF index_oid IS NULL THEN
        RAISE EXCEPTION 'tmc_index_mem_multi is missing';
    END IF;
 
    IF type_oid IS NULL THEN
        RAISE EXCEPTION 'tmc_index_mem_multi type is missing';
    END IF;
 
    SELECT count(*) INTO type_rows
    FROM pg_catalog.pg_type
    WHERE oid = type_oid;
 
    SELECT count(*) INTO class_rows
    FROM pg_catalog.pg_class
    WHERE oid = index_oid;
 
    SELECT count(*) INTO attr_rows
    FROM pg_catalog.pg_attribute
    WHERE attrelid = index_oid
      AND attnum > 0;
 
    SELECT count(*) INTO pg_index_rows
    FROM pg_catalog.pg_index
    WHERE indexrelid = index_oid;
 
    IF type_rows <> 0 OR class_rows <> 0 OR attr_rows <> 0 OR pg_index_rows <> 0 THEN
        RAISE EXCEPTION
            'unexpected physical spill for tmc_index_mem_multi: type %, class %, attr %, pg_index %',
            type_rows, class_rows, attr_rows, pg_index_rows;
    END IF;
END;
$$;
 
BEGIN;
SAVEPOINT tmc_index_create_s;
 
CREATE INDEX tmc_index_rb_idx
ON pg_temp.tmc_index_mem_parent USING btree (payload);
 
SELECT to_regclass('pg_temp.tmc_index_rb_idx') IS NOT NULL AS rb_created;
 
ROLLBACK TO SAVEPOINT tmc_index_create_s;
RELEASE SAVEPOINT tmc_index_create_s;
COMMIT;
 
SELECT to_regclass('pg_temp.tmc_index_rb_idx') IS NULL AS rb_missing_after_rollback;
 
DO $$
DECLARE
    class_rows bigint;
    attr_rows bigint;
    pg_index_rows bigint;
    depend_rows bigint;
BEGIN
    SELECT count(*) INTO class_rows
    FROM pg_catalog.pg_class
    WHERE relname = 'tmc_index_rb_idx';
 
    SELECT count(*) INTO attr_rows
    FROM pg_catalog.pg_attribute
    WHERE attrelid IN (
        SELECT oid
        FROM pg_catalog.pg_class
        WHERE relname = 'tmc_index_rb_idx'
    )
      AND attnum > 0;
 
    SELECT count(*) INTO pg_index_rows
    FROM pg_catalog.pg_index
    WHERE indexrelid IN (
        SELECT oid
        FROM pg_catalog.pg_class
        WHERE relname = 'tmc_index_rb_idx'
    );
 
    SELECT count(*) INTO depend_rows
    FROM pg_catalog.pg_depend
    WHERE classid = 'pg_class'::regclass
      AND objid IN (
        SELECT oid
        FROM pg_catalog.pg_class
        WHERE relname = 'tmc_index_rb_idx'
    );
 
    IF class_rows <> 0 OR attr_rows <> 0 OR pg_index_rows <> 0 OR depend_rows <> 0 THEN
        RAISE EXCEPTION
            'unexpected physical spill for rolled back tmc_index_rb_idx: class %, attr %, pg_index %, depend %',
            class_rows, attr_rows, pg_index_rows, depend_rows;
    END IF;
END;
$$;
 
CREATE INDEX tmc_index_drop_rb_idx
ON pg_temp.tmc_index_mem_parent USING btree (payload);
 
BEGIN;
SAVEPOINT tmc_index_drop_s;
 
DROP INDEX pg_temp.tmc_index_drop_rb_idx;
 
SELECT to_regclass('pg_temp.tmc_index_drop_rb_idx') IS NULL AS drop_missing_inside;
 
ROLLBACK TO SAVEPOINT tmc_index_drop_s;
RELEASE SAVEPOINT tmc_index_drop_s;
COMMIT;
 
SELECT to_regclass('pg_temp.tmc_index_drop_rb_idx') IS NOT NULL AS drop_restored;
 
DO $$
DECLARE
    index_oid oid;
    index_def text;
    plan json;
    node_type text;
    index_name text;
BEGIN
    index_oid := to_regclass('pg_temp.tmc_index_drop_rb_idx');
 
    IF index_oid IS NULL THEN
        RAISE EXCEPTION 'tmc_index_drop_rb_idx is missing after rollback';
    END IF;
 
    index_def := pg_get_indexdef(index_oid);
    IF position('CREATE INDEX tmc_index_drop_rb_idx ON ' IN index_def) <> 1
       OR position(' USING btree (payload)' IN index_def) = 0 THEN
        RAISE EXCEPTION 'unexpected index definition for tmc_index_drop_rb_idx: %',
            index_def;
    END IF;
 
    PERFORM set_config('enable_seqscan', 'off', true);
    PERFORM set_config('enable_bitmapscan', 'off', true);
 
    EXECUTE
        'EXPLAIN (FORMAT JSON) SELECT payload FROM pg_temp.tmc_index_mem_parent WHERE payload = 800'
        INTO plan;
 
    node_type := plan->0->'Plan'->>'Node Type';
    index_name := plan->0->'Plan'->>'Index Name';
 
    IF node_type NOT IN ('Index Scan', 'Index Only Scan') THEN
        RAISE EXCEPTION 'unexpected node type for tmc_index_drop_rb_idx plan: %',
            node_type;
    END IF;
 
    IF index_name <> 'tmc_index_drop_rb_idx' THEN
        RAISE EXCEPTION 'unexpected index name for tmc_index_drop_rb_idx plan: %',
            index_name;
    END IF;
END;
$$;
 
SELECT payload
FROM pg_temp.tmc_index_mem_parent
WHERE payload = 800;
 
RESET enable_seqscan;
RESET enable_bitmapscan;
 
ALTER INDEX pg_temp.tmc_index_drop_rb_idx
RENAME TO tmc_index_drop_rb_idx_renamed;
 
SELECT to_regclass('pg_temp.tmc_index_drop_rb_idx') IS NULL AS old_drop_name_missing;
SELECT to_regclass('pg_temp.tmc_index_drop_rb_idx_renamed') IS NOT NULL AS new_drop_name_present;
 
DO $$
DECLARE
    index_oid oid;
    index_def text;
    plan json;
    node_type text;
    index_name text;
BEGIN
    index_oid := to_regclass('pg_temp.tmc_index_drop_rb_idx_renamed');
 
    IF index_oid IS NULL THEN
        RAISE EXCEPTION 'tmc_index_drop_rb_idx_renamed is missing';
    END IF;
 
    index_def := pg_get_indexdef(index_oid);
    IF position('CREATE INDEX tmc_index_drop_rb_idx_renamed ON ' IN index_def) <> 1
       OR position(' USING btree (payload)' IN index_def) = 0 THEN
        RAISE EXCEPTION 'unexpected index definition for tmc_index_drop_rb_idx_renamed: %',
            index_def;
    END IF;
 
    PERFORM set_config('enable_seqscan', 'off', true);
    PERFORM set_config('enable_bitmapscan', 'off', true);
 
    EXECUTE
        'EXPLAIN (FORMAT JSON) SELECT payload FROM pg_temp.tmc_index_mem_parent WHERE payload = 800'
        INTO plan;
 
    node_type := plan->0->'Plan'->>'Node Type';
    index_name := plan->0->'Plan'->>'Index Name';
 
    IF node_type NOT IN ('Index Scan', 'Index Only Scan') THEN
        RAISE EXCEPTION 'unexpected node type for tmc_index_drop_rb_idx_renamed plan: %',
            node_type;
    END IF;
 
    IF index_name <> 'tmc_index_drop_rb_idx_renamed' THEN
        RAISE EXCEPTION 'unexpected index name for tmc_index_drop_rb_idx_renamed plan: %',
            index_name;
    END IF;
END;
$$;
 
SELECT payload
FROM pg_temp.tmc_index_mem_parent
WHERE payload = 800;
 
RESET enable_seqscan;
RESET enable_bitmapscan;
 
DROP INDEX pg_temp.tmc_index_mem_idx;
DROP INDEX pg_temp.tmc_index_mem_multi;
DROP INDEX pg_temp.tmc_index_drop_rb_idx_renamed;
 
SELECT to_regclass('pg_temp.tmc_index_mem_idx') IS NULL
       AND to_regclass('pg_temp.tmc_index_mem_multi') IS NULL
       AND to_regclass('pg_temp.tmc_index_drop_rb_idx_renamed') IS NULL
       AS all_indexes_dropped;
 
DROP TABLE pg_temp.tmc_index_mem_parent;
 
RESET enable_temp_memory_catalog;