Исходный код СУБД 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
--
-- one_c_executor_guaranteed_empty
--
 
SET client_min_messages = warning;
 
DROP SCHEMA IF EXISTS one_c_executor CASCADE;
CREATE SCHEMA one_c_executor;
SET search_path = one_c_executor, public;
 
CREATE FUNCTION explain_analyze_root(query_text text)
RETURNS jsonb
LANGUAGE plpgsql
AS $$
DECLARE
    plan_doc jsonb;
BEGIN
    EXECUTE
        'EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF, FORMAT JSON) ' ||
        query_text
        INTO plan_doc;
    RETURN plan_doc -> 0 -> 'Plan';
END;
$$;
 
CREATE FUNCTION find_plan_node(plan_doc jsonb,
                               wanted_node_type text,
                               wanted_relation_name text DEFAULT NULL)
RETURNS jsonb
LANGUAGE plpgsql
AS $$
DECLARE
    child_plan jsonb;
    found_plan jsonb;
BEGIN
    IF plan_doc IS NULL THEN
        RETURN NULL;
    END IF;
 
    IF plan_doc ->> 'Node Type' = wanted_node_type AND
       (wanted_relation_name IS NULL OR
        plan_doc ->> 'Relation Name' = wanted_relation_name) THEN
        RETURN plan_doc;
    END IF;
 
    IF jsonb_typeof(plan_doc -> 'Plans') = 'array' THEN
        FOR child_plan IN
            SELECT value
            FROM jsonb_array_elements(plan_doc -> 'Plans')
        LOOP
            found_plan := one_c_executor.find_plan_node(child_plan,
                                                       wanted_node_type,
                                                       wanted_relation_name);
            IF found_plan IS NOT NULL THEN
                RETURN found_plan;
            END IF;
        END LOOP;
    END IF;
 
    RETURN NULL;
END;
$$;
 
SET max_parallel_workers_per_gather = 0;
SET enable_hashjoin = off;
SET enable_mergejoin = off;
SET enable_nestloop = on;
SET enable_material = on;
SET jit = off;
 
CREATE TABLE ge_outer(id integer)
WITH (autovacuum_enabled = false);
 
CREATE TABLE ge_inner(id integer)
WITH (autovacuum_enabled = false);
 
INSERT INTO ge_outer
SELECT g
FROM generate_series(1, 100000) AS g;
 
INSERT INTO ge_inner
SELECT g
FROM generate_series(1, 1000) AS g;
 
ANALYZE ge_outer;
ANALYZE ge_inner;
 
DELETE FROM ge_inner;
 
DO $$
DECLARE
    actual_rows bigint;
    estimated_rows integer;
    query_text text;
    plan_root jsonb;
    nested_loop jsonb;
    materialize_node jsonb;
    outer_scan jsonb;
BEGIN
    SELECT count(*)
      INTO actual_rows
      FROM one_c_executor.ge_inner;
 
    IF actual_rows <> 0 THEN
        RAISE EXCEPTION 'unexpected ge_inner rowcount: got %, expected 0',
            actual_rows;
    END IF;
 
    SELECT reltuples::integer
      INTO estimated_rows
      FROM pg_catalog.pg_class
     WHERE oid = 'one_c_executor.ge_inner'::regclass;
 
    IF estimated_rows <> 1000 THEN
        RAISE EXCEPTION 'unexpected ge_inner reltuples: got %, expected 1000',
            estimated_rows;
    END IF;
 
    query_text := $sql$
        SELECT count(*)
        FROM one_c_executor.ge_outer o
        WHERE EXISTS (
            SELECT 1
            FROM one_c_executor.ge_inner i
            WHERE i.id = o.id
        )
    $sql$;
 
    SELECT count(*)
      INTO actual_rows
      FROM one_c_executor.ge_outer o
     WHERE EXISTS (
            SELECT 1
            FROM one_c_executor.ge_inner i
            WHERE i.id = o.id
        );
 
    IF actual_rows <> 0 THEN
        RAISE EXCEPTION 'unexpected semi-join result: got %, expected 0',
            actual_rows;
    END IF;
 
    plan_root := one_c_executor.explain_analyze_root(query_text);
    nested_loop := one_c_executor.find_plan_node(plan_root, 'Nested Loop');
    materialize_node := one_c_executor.find_plan_node(plan_root, 'Materialize');
    outer_scan := one_c_executor.find_plan_node(plan_root, 'Seq Scan', 'ge_outer');
 
    IF nested_loop IS NULL THEN
        RAISE EXCEPTION 'Nested Loop node not found in semi-join plan';
    END IF;
 
    IF materialize_node IS NULL THEN
        RAISE EXCEPTION 'Materialize node not found in semi-join plan';
    END IF;
 
    IF (materialize_node ->> 'Actual Rows')::numeric <> 0 THEN
        RAISE EXCEPTION 'unexpected Materialize Actual Rows: got %, expected 0',
            materialize_node ->> 'Actual Rows';
    END IF;
 
    IF outer_scan IS NULL THEN
        RAISE EXCEPTION 'Seq Scan on ge_outer not found in semi-join plan';
    END IF;
 
    IF (outer_scan ->> 'Actual Rows')::numeric <> 1 THEN
        RAISE EXCEPTION 'unexpected ge_outer Actual Rows: got %, expected 1',
            outer_scan ->> 'Actual Rows';
    END IF;
END;
$$;
 
DO $$
DECLARE
    actual_rows bigint;
BEGIN
    SELECT count(*)
      INTO actual_rows
      FROM one_c_executor.ge_outer o
      LEFT JOIN one_c_executor.ge_inner i
        ON i.id = o.id;
 
    IF actual_rows <> 100000 THEN
        RAISE EXCEPTION 'unexpected LEFT JOIN count: got %, expected 100000',
            actual_rows;
    END IF;
 
    SELECT count(*)
      INTO actual_rows
      FROM one_c_executor.ge_outer o
     WHERE NOT EXISTS (
            SELECT 1
            FROM one_c_executor.ge_inner i
            WHERE i.id = o.id
        );
 
    IF actual_rows <> 100000 THEN
        RAISE EXCEPTION 'unexpected anti-semantics count: got %, expected 100000',
            actual_rows;
    END IF;
END;
$$;
 
CREATE TABLE expr_outer(id integer)
WITH (autovacuum_enabled = false);
 
CREATE TABLE expr_a(id integer)
WITH (autovacuum_enabled = false);
 
CREATE TABLE expr_b(id integer)
WITH (autovacuum_enabled = false);
 
INSERT INTO expr_outer
SELECT g
FROM generate_series(1, 100000) AS g;
 
INSERT INTO expr_a
SELECT g
FROM generate_series(1, 1000) AS g;
 
INSERT INTO expr_b
SELECT g
FROM generate_series(1, 1000) AS g;
 
ANALYZE expr_outer;
ANALYZE expr_a;
ANALYZE expr_b;
 
DELETE FROM expr_a;
DELETE FROM expr_b;
 
DO $$
DECLARE
    actual_rows bigint;
    query_text text;
    plan_root jsonb;
    outer_scan jsonb;
    rows_removed numeric;
BEGIN
    query_text := $sql$
        SELECT count(*)
        FROM one_c_executor.expr_outer o
        WHERE
              (
                  o.id IN (SELECT a.id FROM one_c_executor.expr_a a)
                  AND o.id > 0
              )
           OR (
                  o.id IN (SELECT b.id FROM one_c_executor.expr_b b)
                  AND o.id > 0
              )
    $sql$;
 
    SELECT count(*)
      INTO actual_rows
      FROM one_c_executor.expr_outer o
     WHERE (
               o.id IN (SELECT a.id FROM one_c_executor.expr_a a)
               AND o.id > 0
           )
        OR (
               o.id IN (SELECT b.id FROM one_c_executor.expr_b b)
               AND o.id > 0
           );
 
    IF actual_rows <> 0 THEN
        RAISE EXCEPTION 'unexpected OR/SubPlan result: got %, expected 0',
            actual_rows;
    END IF;
 
    plan_root := one_c_executor.explain_analyze_root(query_text);
    outer_scan := one_c_executor.find_plan_node(plan_root, 'Seq Scan', 'expr_outer');
 
    IF outer_scan IS NULL THEN
        RAISE EXCEPTION 'Seq Scan on expr_outer not found in OR/SubPlan plan';
    END IF;
 
    IF (outer_scan ->> 'Actual Rows')::numeric <> 0 THEN
        RAISE EXCEPTION 'unexpected expr_outer Actual Rows: got %, expected 0',
            outer_scan ->> 'Actual Rows';
    END IF;
 
    rows_removed := COALESCE((outer_scan ->> 'Rows Removed by Filter')::numeric, 0);
    IF rows_removed > 1 THEN
        RAISE EXCEPTION 'too many rows removed by filter: got %, expected <= 1',
            rows_removed;
    END IF;
 
    IF position('"Subplan Name"' IN plan_root::text) = 0 OR
       position('"Parent Relationship": "SubPlan"' IN plan_root::text) = 0 THEN
        RAISE EXCEPTION 'expected SubPlan metadata not found in OR/SubPlan plan';
    END IF;
END;
$$;
 
DO $$
DECLARE
    query_text text := $sql$
        SELECT count(*)
        FROM one_c_executor.expr_outer o
        WHERE
              (
                  o.id IN (SELECT a.id FROM one_c_executor.expr_a a)
                  AND o.id > 0
              )
           OR (
                  o.id IN (SELECT b.id FROM one_c_executor.expr_b b)
                  AND o.id > 0
              )
    $sql$;
    actual_rows bigint;
    plan_root jsonb;
    outer_scan jsonb;
    rows_removed numeric;
BEGIN
    IF pg_catalog.pg_jit_available() THEN
        PERFORM pg_catalog.set_config('jit', 'on', true);
        PERFORM pg_catalog.set_config('jit_above_cost', '0', true);
 
        SELECT count(*)
          INTO actual_rows
          FROM one_c_executor.expr_outer o
         WHERE (
                   o.id IN (SELECT a.id FROM one_c_executor.expr_a a)
                   AND o.id > 0
               )
            OR (
                   o.id IN (SELECT b.id FROM one_c_executor.expr_b b)
                   AND o.id > 0
               );
 
        IF actual_rows <> 0 THEN
            RAISE EXCEPTION 'unexpected JIT OR/SubPlan result: got %, expected 0',
                actual_rows;
        END IF;
 
        plan_root := one_c_executor.explain_analyze_root(query_text);
        outer_scan := one_c_executor.find_plan_node(plan_root, 'Seq Scan', 'expr_outer');
 
        IF outer_scan IS NULL THEN
            RAISE EXCEPTION 'Seq Scan on expr_outer not found in JIT OR/SubPlan plan';
        END IF;
 
        rows_removed := COALESCE((outer_scan ->> 'Rows Removed by Filter')::numeric, 0);
        IF rows_removed > 1 THEN
            RAISE EXCEPTION 'too many JIT rows removed by filter: got %, expected <= 1',
                rows_removed;
        END IF;
    END IF;
END;
$$;
 
DO $$
DECLARE
    actual_rows bigint;
    query_text text;
    plan_root jsonb;
    outer_scan jsonb;
BEGIN
    query_text := $sql$
        SELECT count(*)
        FROM one_c_executor.expr_outer o
        WHERE
              o.id IN (
                  SELECT a.id
                  FROM one_c_executor.expr_a a
              )
           OR o.id < 0
    $sql$;
 
    SELECT count(*)
      INTO actual_rows
      FROM one_c_executor.expr_outer o
     WHERE
           o.id IN (
               SELECT a.id
               FROM one_c_executor.expr_a a
           )
        OR o.id < 0;
 
    IF actual_rows <> 0 THEN
        RAISE EXCEPTION 'unexpected mixed OR result: got %, expected 0',
            actual_rows;
    END IF;
 
    plan_root := one_c_executor.explain_analyze_root(query_text);
    outer_scan := one_c_executor.find_plan_node(plan_root, 'Seq Scan', 'expr_outer');
 
    IF outer_scan IS NULL THEN
        RAISE EXCEPTION 'Seq Scan on expr_outer not found in mixed OR plan';
    END IF;
 
    IF (outer_scan ->> 'Actual Rows')::numeric <> 0 THEN
        RAISE EXCEPTION 'unexpected mixed OR Actual Rows: got %, expected 0',
            outer_scan ->> 'Actual Rows';
    END IF;
 
    IF COALESCE((outer_scan ->> 'Rows Removed by Filter')::numeric, 0) <> 100000 THEN
        RAISE EXCEPTION 'unexpected mixed OR Rows Removed by Filter: got %, expected 100000',
            COALESCE(outer_scan ->> 'Rows Removed by Filter', 'NULL');
    END IF;
 
    IF position('"Subplan Name"' IN plan_root::text) = 0 OR
       position('"Parent Relationship": "SubPlan"' IN plan_root::text) = 0 THEN
        RAISE EXCEPTION 'expected SubPlan metadata not found in mixed OR plan';
    END IF;
END;
$$;
 
DO $$
DECLARE
    actual_rows bigint;
BEGIN
    SELECT count(*)
      INTO actual_rows
      FROM one_c_executor.expr_outer o
     WHERE NOT (
            o.id IN (
                SELECT a.id
                FROM one_c_executor.expr_a a
            )
        );
 
    IF actual_rows <> 100000 THEN
        RAISE EXCEPTION 'unexpected NOT/SubPlan result: got %, expected 100000',
            actual_rows;
    END IF;
END;
$$;
 
DO $$
DECLARE
    actual_rows bigint;
    query_text text;
    plan_root jsonb;
    outer_scan jsonb;
    rows_removed numeric;
BEGIN
    query_text := $sql$
        SELECT count(*)
        FROM one_c_executor.expr_outer o
        WHERE
            (
                   o.id IN (
                       SELECT a.id
                       FROM one_c_executor.expr_a a
                   )
                OR o.id IN (
                       SELECT b.id
                       FROM one_c_executor.expr_b b
                   )
            )
            AND o.id > 0
    $sql$;
 
    SELECT count(*)
      INTO actual_rows
      FROM one_c_executor.expr_outer o
     WHERE
         (
                o.id IN (
                    SELECT a.id
                    FROM one_c_executor.expr_a a
                )
             OR o.id IN (
                    SELECT b.id
                    FROM one_c_executor.expr_b b
                )
         )
         AND o.id > 0;
 
    IF actual_rows <> 0 THEN
        RAISE EXCEPTION 'unexpected top-level AND result: got %, expected 0',
            actual_rows;
    END IF;
 
    plan_root := one_c_executor.explain_analyze_root(query_text);
    outer_scan := one_c_executor.find_plan_node(plan_root, 'Seq Scan', 'expr_outer');
 
    IF outer_scan IS NULL THEN
        RAISE EXCEPTION 'Seq Scan on expr_outer not found in top-level AND plan';
    END IF;
 
    IF (outer_scan ->> 'Actual Rows')::numeric <> 0 THEN
        RAISE EXCEPTION 'unexpected top-level AND Actual Rows: got %, expected 0',
            outer_scan ->> 'Actual Rows';
    END IF;
 
    rows_removed := COALESCE((outer_scan ->> 'Rows Removed by Filter')::numeric, 0);
    IF rows_removed > 1 THEN
        RAISE EXCEPTION 'too many top-level AND rows removed by filter: got %, expected <= 1',
            rows_removed;
    END IF;
 
    IF position('"Subplan Name"' IN plan_root::text) = 0 OR
       position('"Parent Relationship": "SubPlan"' IN plan_root::text) = 0 THEN
        RAISE EXCEPTION 'expected SubPlan metadata not found in top-level AND plan';
    END IF;
END;
$$;
 
CREATE TABLE expr_nonempty(id integer)
WITH (autovacuum_enabled = false);
 
INSERT INTO expr_nonempty VALUES (100000);
 
ANALYZE expr_nonempty;
 
DO $$
DECLARE
    actual_rows bigint;
    query_text text;
    plan_root jsonb;
BEGIN
    query_text := $sql$
        SELECT count(*)
        FROM one_c_executor.expr_outer o
        WHERE
              o.id IN (
                  SELECT n.id
                  FROM one_c_executor.expr_nonempty n
              )
           OR o.id < 0
    $sql$;
 
    SELECT count(*)
      INTO actual_rows
      FROM one_c_executor.expr_outer o
     WHERE
           o.id IN (
               SELECT n.id
               FROM one_c_executor.expr_nonempty n
           )
        OR o.id < 0;
 
    IF actual_rows <> 1 THEN
        RAISE EXCEPTION 'unexpected nonempty OR result: got %, expected 1',
            actual_rows;
    END IF;
 
    plan_root := one_c_executor.explain_analyze_root(query_text);
 
    IF position('"Subplan Name"' IN plan_root::text) = 0 OR
       position('"Parent Relationship": "SubPlan"' IN plan_root::text) = 0 THEN
        RAISE EXCEPTION 'expected SubPlan metadata not found in nonempty OR plan';
    END IF;
END;
$$;
 
RESET jit_above_cost;
RESET jit;
RESET enable_material;
RESET enable_nestloop;
RESET enable_mergejoin;
RESET enable_hashjoin;
RESET max_parallel_workers_per_gather;
RESET search_path;
 
DROP SCHEMA one_c_executor CASCADE;