Исходный код СУБД 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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
--
-- one_c_planner_or_sort
--
 
SET client_min_messages = warning;
 
DROP SCHEMA IF EXISTS one_c_or_sort CASCADE;
CREATE SCHEMA one_c_or_sort;
SET search_path = one_c_or_sort, public;
 
CREATE FUNCTION explain_root(query_text text)
RETURNS jsonb
LANGUAGE plpgsql
AS $$
DECLARE
    plan_doc jsonb;
BEGIN
    EXECUTE 'EXPLAIN (FORMAT JSON) ' || query_text INTO plan_doc;
    RETURN plan_doc -> 0 -> 'Plan';
END;
$$;
 
CREATE FUNCTION explain_analyze_root(query_text text)
RETURNS jsonb
LANGUAGE plpgsql
AS $$
DECLARE
    plan_doc jsonb;
BEGIN
    EXECUTE 'EXPLAIN (ANALYZE, COSTS ON, TIMING OFF, SUMMARY OFF, FORMAT JSON) '
        || query_text
        INTO plan_doc;
    RETURN plan_doc -> 0 -> 'Plan';
END;
$$;
 
CREATE FUNCTION count_node_type(plan jsonb, wanted text)
RETURNS integer
LANGUAGE sql
AS $$
WITH RECURSIVE nodes(node) AS (
    SELECT plan
    UNION ALL
    SELECT child.node
    FROM nodes,
         LATERAL jsonb_array_elements(COALESCE(nodes.node -> 'Plans',
                                              '[]'::jsonb)) AS child(node)
)
SELECT count(*)::integer
FROM nodes
WHERE node ->> 'Node Type' = wanted;
$$;
 
CREATE FUNCTION first_node(plan jsonb, wanted text)
RETURNS jsonb
LANGUAGE sql
AS $$
WITH RECURSIVE nodes(node) AS (
    SELECT plan
    UNION ALL
    SELECT child.node
    FROM nodes,
         LATERAL jsonb_array_elements(COALESCE(nodes.node -> 'Plans',
                                              '[]'::jsonb)) AS child(node)
)
SELECT node
FROM nodes
WHERE node ->> 'Node Type' = wanted
LIMIT 1;
$$;
 
CREATE FUNCTION nth_child(plan jsonb, idx integer)
RETURNS jsonb
LANGUAGE sql
IMMUTABLE
AS $$
SELECT plan -> 'Plans' -> (idx - 1);
$$;
 
SET max_parallel_workers_per_gather = 0;
 
CREATE TABLE ordered_or_t(
    x integer,
    y integer,
    payload text
)
WITH (autovacuum_enabled = false);
 
INSERT INTO ordered_or_t
SELECT
    g % 100,
    g % 1000,
    md5(g::text)
FROM generate_series(1, 200000) AS g;
 
CREATE INDEX ordered_or_xy_idx
ON ordered_or_t(x, y);
 
ANALYZE ordered_or_t;
 
SET enable_bitmapscan = off;
SET enable_seqscan = off;
 
DO $$
DECLARE
    actual_count integer;
    asc_result_count integer := 0;
    desc_result_count integer := 0;
    range_asc_result_count integer := 0;
    range_desc_result_count integer := 0;
    distinct_count integer;
    asc_plan jsonb;
    desc_plan jsonb;
    range_asc_plan jsonb;
    range_desc_plan jsonb;
    overlap_plan jsonb;
    child1 jsonb;
    child2 jsonb;
    prev_x integer;
    prev_y integer;
    row_record record;
BEGIN
    SELECT count(*)
      INTO actual_count
      FROM one_c_or_sort.ordered_or_t
     WHERE (x = 5 AND y > 900)
        OR x > 96;
 
    IF actual_count <> 6200 THEN
        RAISE EXCEPTION 'positive ordered-OR result mismatch: got %, expected 6200',
            actual_count;
    END IF;
 
    asc_plan := one_c_or_sort.explain_analyze_root($sql$
        SELECT *
        FROM one_c_or_sort.ordered_or_t
        WHERE (x = 5 AND y > 900)
           OR x > 96
        ORDER BY x, y
    $sql$);
 
    IF asc_plan ->> 'Node Type' <> 'Append' THEN
        RAISE EXCEPTION 'positive ASC root node mismatch: got %, expected Append',
            asc_plan ->> 'Node Type';
    END IF;
 
    IF one_c_or_sort.count_node_type(asc_plan, 'Append') <> 1 THEN
        RAISE EXCEPTION 'positive ASC Append count mismatch';
    END IF;
 
    IF one_c_or_sort.count_node_type(asc_plan, 'Index Scan') <> 2 THEN
        RAISE EXCEPTION 'positive ASC Index Scan count mismatch';
    END IF;
 
    IF one_c_or_sort.count_node_type(asc_plan, 'Sort') <> 0 THEN
        RAISE EXCEPTION 'positive ASC unexpectedly contains Sort';
    END IF;
 
    IF one_c_or_sort.count_node_type(asc_plan, 'Incremental Sort') <> 0 THEN
        RAISE EXCEPTION 'positive ASC unexpectedly contains Incremental Sort';
    END IF;
 
    IF (asc_plan ->> 'Actual Rows')::numeric <> 6200 THEN
        RAISE EXCEPTION 'positive ASC actual rows mismatch: got %, expected 6200',
            (asc_plan ->> 'Actual Rows')::numeric;
    END IF;
 
    child1 := one_c_or_sort.nth_child(asc_plan, 1);
    child2 := one_c_or_sort.nth_child(asc_plan, 2);
 
    IF child1 ->> 'Node Type' <> 'Index Scan'
       OR child2 ->> 'Node Type' <> 'Index Scan' THEN
        RAISE EXCEPTION 'positive ASC child node mismatch: expected two Index Scan nodes';
    END IF;
 
    IF child1 ->> 'Index Name' <> 'ordered_or_xy_idx'
       OR child2 ->> 'Index Name' <> 'ordered_or_xy_idx' THEN
        RAISE EXCEPTION 'positive ASC index name mismatch';
    END IF;
 
    IF child1 ->> 'Scan Direction' <> 'Forward'
       OR child2 ->> 'Scan Direction' <> 'Forward' THEN
        RAISE EXCEPTION 'positive ASC scan direction mismatch';
    END IF;
 
    IF (child1 ->> 'Actual Rows')::numeric <> 200
       OR (child2 ->> 'Actual Rows')::numeric <> 6000 THEN
        RAISE EXCEPTION 'positive ASC child row order mismatch: child1 %, child2 %',
            (child1 ->> 'Actual Rows')::numeric,
            (child2 ->> 'Actual Rows')::numeric;
    END IF;
 
    prev_x := NULL;
    prev_y := NULL;
 
    FOR row_record IN
        SELECT x, y
        FROM one_c_or_sort.ordered_or_t
        WHERE (x = 5 AND y > 900)
           OR x > 96
        ORDER BY x, y
    LOOP
        asc_result_count := asc_result_count + 1;
 
        IF prev_x IS NOT NULL
           AND (prev_x > row_record.x
                OR (prev_x = row_record.x AND prev_y > row_record.y)) THEN
            RAISE EXCEPTION 'positive ASC emitted rows out of order at row %: previous (%,%), current (%,%)',
                asc_result_count, prev_x, prev_y, row_record.x, row_record.y;
        END IF;
 
        prev_x := row_record.x;
        prev_y := row_record.y;
    END LOOP;
 
    IF asc_result_count <> 6200 THEN
        RAISE EXCEPTION 'positive ASC emitted row count mismatch: got %, expected 6200',
            asc_result_count;
    END IF;
 
    desc_plan := one_c_or_sort.explain_analyze_root($sql$
        SELECT *
        FROM one_c_or_sort.ordered_or_t
        WHERE (x = 5 AND y > 900)
           OR x > 96
        ORDER BY x DESC, y DESC
    $sql$);
 
    IF desc_plan ->> 'Node Type' <> 'Append' THEN
        RAISE EXCEPTION 'positive DESC root node mismatch: got %, expected Append',
            desc_plan ->> 'Node Type';
    END IF;
 
    IF one_c_or_sort.count_node_type(desc_plan, 'Append') <> 1 THEN
        RAISE EXCEPTION 'positive DESC Append count mismatch';
    END IF;
 
    IF one_c_or_sort.count_node_type(desc_plan, 'Index Scan') <> 2 THEN
        RAISE EXCEPTION 'positive DESC Index Scan count mismatch';
    END IF;
 
    IF one_c_or_sort.count_node_type(desc_plan, 'Sort') <> 0 THEN
        RAISE EXCEPTION 'positive DESC unexpectedly contains Sort';
    END IF;
 
    IF one_c_or_sort.count_node_type(desc_plan, 'Incremental Sort') <> 0 THEN
        RAISE EXCEPTION 'positive DESC unexpectedly contains Incremental Sort';
    END IF;
 
    IF (desc_plan ->> 'Actual Rows')::numeric <> 6200 THEN
        RAISE EXCEPTION 'positive DESC actual rows mismatch: got %, expected 6200',
            (desc_plan ->> 'Actual Rows')::numeric;
    END IF;
 
    child1 := one_c_or_sort.nth_child(desc_plan, 1);
    child2 := one_c_or_sort.nth_child(desc_plan, 2);
 
    IF child1 ->> 'Node Type' <> 'Index Scan'
       OR child2 ->> 'Node Type' <> 'Index Scan' THEN
        RAISE EXCEPTION 'positive DESC child node mismatch: expected two Index Scan nodes';
    END IF;
 
    IF child1 ->> 'Index Name' <> 'ordered_or_xy_idx'
       OR child2 ->> 'Index Name' <> 'ordered_or_xy_idx' THEN
        RAISE EXCEPTION 'positive DESC index name mismatch';
    END IF;
 
    IF child1 ->> 'Scan Direction' <> 'Backward'
       OR child2 ->> 'Scan Direction' <> 'Backward' THEN
        RAISE EXCEPTION 'positive DESC scan direction mismatch';
    END IF;
 
    IF (child1 ->> 'Actual Rows')::numeric <> 6000
       OR (child2 ->> 'Actual Rows')::numeric <> 200 THEN
        RAISE EXCEPTION 'positive DESC child row order mismatch: child1 %, child2 %',
            (child1 ->> 'Actual Rows')::numeric,
            (child2 ->> 'Actual Rows')::numeric;
    END IF;
 
    prev_x := NULL;
    prev_y := NULL;
 
    FOR row_record IN
        SELECT x, y
        FROM one_c_or_sort.ordered_or_t
        WHERE (x = 5 AND y > 900)
           OR x > 96
        ORDER BY x DESC, y DESC
    LOOP
        desc_result_count := desc_result_count + 1;
 
        IF prev_x IS NOT NULL
           AND (prev_x < row_record.x
                OR (prev_x = row_record.x AND prev_y < row_record.y)) THEN
            RAISE EXCEPTION 'positive DESC emitted rows out of order at row %: previous (%,%), current (%,%)',
                desc_result_count, prev_x, prev_y, row_record.x, row_record.y;
        END IF;
 
        prev_x := row_record.x;
        prev_y := row_record.y;
    END LOOP;
 
    IF desc_result_count <> 6200 THEN
        RAISE EXCEPTION 'positive DESC emitted row count mismatch: got %, expected 6200',
            desc_result_count;
    END IF;
 
    range_asc_plan := one_c_or_sort.explain_analyze_root($sql$
        SELECT *
        FROM one_c_or_sort.ordered_or_t
        WHERE x < 1
           OR x >= 99
        ORDER BY x, y
    $sql$);
 
    IF range_asc_plan ->> 'Node Type' <> 'Append' THEN
        RAISE EXCEPTION 'range ASC root node mismatch: got %, expected Append',
            range_asc_plan ->> 'Node Type';
    END IF;
 
    IF one_c_or_sort.count_node_type(range_asc_plan, 'Index Scan') <> 2 THEN
        RAISE EXCEPTION 'range ASC Index Scan count mismatch';
    END IF;
 
    IF jsonb_array_length(range_asc_plan -> 'Plans') <> 2 THEN
        RAISE EXCEPTION 'range ASC child count mismatch';
    END IF;
 
    IF one_c_or_sort.count_node_type(range_asc_plan, 'Sort') <> 0
       OR one_c_or_sort.count_node_type(range_asc_plan, 'Incremental Sort') <> 0 THEN
        RAISE EXCEPTION 'range ASC unexpectedly contains a Sort';
    END IF;
 
    child1 := one_c_or_sort.nth_child(range_asc_plan, 1);
    child2 := one_c_or_sort.nth_child(range_asc_plan, 2);
 
    IF child1 ->> 'Node Type' <> 'Index Scan'
       OR child2 ->> 'Node Type' <> 'Index Scan'
       OR child1 ->> 'Index Name' <> 'ordered_or_xy_idx'
       OR child2 ->> 'Index Name' <> 'ordered_or_xy_idx'
       OR child1 ->> 'Scan Direction' <> 'Forward'
       OR child2 ->> 'Scan Direction' <> 'Forward' THEN
        RAISE EXCEPTION 'range ASC child node, index, or scan direction mismatch';
    END IF;
 
    IF child1 ->> 'Index Cond' <> '(x < 1)'
       OR child2 ->> 'Index Cond' <> '(x >= 99)' THEN
        RAISE EXCEPTION 'range ASC child order mismatch: child1 %, child2 %',
            child1 ->> 'Index Cond', child2 ->> 'Index Cond';
    END IF;
 
    prev_x := NULL;
    prev_y := NULL;
 
    FOR row_record IN
        SELECT x, y
        FROM one_c_or_sort.ordered_or_t
        WHERE x < 1
           OR x >= 99
        ORDER BY x, y
    LOOP
        range_asc_result_count := range_asc_result_count + 1;
 
        IF prev_x IS NOT NULL
           AND (prev_x > row_record.x
                OR (prev_x = row_record.x AND prev_y > row_record.y)) THEN
            RAISE EXCEPTION 'range ASC emitted rows out of order at row %: previous (%,%), current (%,%)',
                range_asc_result_count, prev_x, prev_y, row_record.x, row_record.y;
        END IF;
 
        prev_x := row_record.x;
        prev_y := row_record.y;
    END LOOP;
 
    IF range_asc_result_count <> 4000 THEN
        RAISE EXCEPTION 'range ASC emitted row count mismatch: got %, expected 4000',
            range_asc_result_count;
    END IF;
 
    range_desc_plan := one_c_or_sort.explain_analyze_root($sql$
        SELECT *
        FROM one_c_or_sort.ordered_or_t
        WHERE 1 > x
           OR 99 <= x
        ORDER BY x DESC, y DESC
    $sql$);
 
    IF range_desc_plan ->> 'Node Type' <> 'Append' THEN
        RAISE EXCEPTION 'range DESC root node mismatch: got %, expected Append',
            range_desc_plan ->> 'Node Type';
    END IF;
 
    IF one_c_or_sort.count_node_type(range_desc_plan, 'Index Scan') <> 2 THEN
        RAISE EXCEPTION 'range DESC Index Scan count mismatch';
    END IF;
 
    IF jsonb_array_length(range_desc_plan -> 'Plans') <> 2 THEN
        RAISE EXCEPTION 'range DESC child count mismatch';
    END IF;
 
    IF one_c_or_sort.count_node_type(range_desc_plan, 'Sort') <> 0
       OR one_c_or_sort.count_node_type(range_desc_plan, 'Incremental Sort') <> 0 THEN
        RAISE EXCEPTION 'range DESC unexpectedly contains a Sort';
    END IF;
 
    child1 := one_c_or_sort.nth_child(range_desc_plan, 1);
    child2 := one_c_or_sort.nth_child(range_desc_plan, 2);
 
    IF child1 ->> 'Node Type' <> 'Index Scan'
       OR child2 ->> 'Node Type' <> 'Index Scan'
       OR child1 ->> 'Index Name' <> 'ordered_or_xy_idx'
       OR child2 ->> 'Index Name' <> 'ordered_or_xy_idx'
       OR child1 ->> 'Scan Direction' <> 'Backward'
       OR child2 ->> 'Scan Direction' <> 'Backward' THEN
        RAISE EXCEPTION 'range DESC child node, index, or scan direction mismatch';
    END IF;
 
    IF child1 ->> 'Index Cond' <> '(x >= 99)'
       OR child2 ->> 'Index Cond' <> '(x < 1)' THEN
        RAISE EXCEPTION 'range DESC child order mismatch: child1 %, child2 %',
            child1 ->> 'Index Cond', child2 ->> 'Index Cond';
    END IF;
 
    prev_x := NULL;
    prev_y := NULL;
 
    FOR row_record IN
        SELECT x, y
        FROM one_c_or_sort.ordered_or_t
        WHERE 1 > x
           OR 99 <= x
        ORDER BY x DESC, y DESC
    LOOP
        range_desc_result_count := range_desc_result_count + 1;
 
        IF prev_x IS NOT NULL
           AND (prev_x < row_record.x
                OR (prev_x = row_record.x AND prev_y < row_record.y)) THEN
            RAISE EXCEPTION 'range DESC emitted rows out of order at row %: previous (%,%), current (%,%)',
                range_desc_result_count, prev_x, prev_y, row_record.x, row_record.y;
        END IF;
 
        prev_x := row_record.x;
        prev_y := row_record.y;
    END LOOP;
 
    IF range_desc_result_count <> 4000 THEN
        RAISE EXCEPTION 'range DESC emitted row count mismatch: got %, expected 4000',
            range_desc_result_count;
    END IF;
 
    overlap_plan := one_c_or_sort.explain_analyze_root($sql$
        SELECT *
        FROM one_c_or_sort.ordered_or_t
        WHERE (x = 5 AND y > 900)
           OR x > 4
        ORDER BY x, y
    $sql$);
 
    IF one_c_or_sort.count_node_type(overlap_plan, 'Append') <> 0 THEN
        RAISE EXCEPTION 'overlap-negative unexpectedly selected special Append';
    END IF;
 
    SELECT count(*)
      INTO actual_count
      FROM one_c_or_sort.ordered_or_t
     WHERE x > 96
        OR x > 98;
 
    SELECT count(*)
      INTO distinct_count
      FROM (
        SELECT DISTINCT x, y, payload
        FROM one_c_or_sort.ordered_or_t
        WHERE x > 96
           OR x > 98
      ) AS deduped;
 
    IF actual_count <> 6000 THEN
        RAISE EXCEPTION 'contained ordered-OR result mismatch: got %, expected 6000',
            actual_count;
    END IF;
 
    IF distinct_count <> actual_count THEN
        RAISE EXCEPTION 'contained ordered-OR produced duplicates: count %, distinct %',
            actual_count, distinct_count;
    END IF;
END;
$$;
 
SET enable_bitmapscan = on;
SET enable_seqscan = on;
SET enable_incremental_sort = off;
SET work_mem = '256MB';
 
CREATE TABLE sort_cost_t(
    unique_integer integer,
    low_cardinality_integer integer,
    wide_text text
)
WITH (autovacuum_enabled = false);
 
INSERT INTO sort_cost_t
SELECT
    g,
    g % 10,
    repeat(md5(g::text), 16)
FROM generate_series(1, 50000) AS g;
 
ANALYZE sort_cost_t;
 
DO $$
DECLARE
    int_sort jsonb;
    text_sort jsonb;
    unique_first_sort jsonb;
    low_first_sort jsonb;
    int_startup numeric;
    text_startup numeric;
    unique_first_startup numeric;
    low_first_startup numeric;
BEGIN
    int_sort := one_c_or_sort.first_node(
        one_c_or_sort.explain_root($sql$
            SELECT unique_integer
            FROM one_c_or_sort.sort_cost_t
            ORDER BY unique_integer
        $sql$),
        'Sort');
 
    text_sort := one_c_or_sort.first_node(
        one_c_or_sort.explain_root($sql$
            SELECT wide_text
            FROM one_c_or_sort.sort_cost_t
            ORDER BY wide_text
        $sql$),
        'Sort');
 
    IF int_sort IS NULL OR text_sort IS NULL THEN
        RAISE EXCEPTION 'expected Sort nodes for single-column sort-cost checks';
    END IF;
 
    int_startup := (int_sort ->> 'Startup Cost')::numeric;
    text_startup := (text_sort ->> 'Startup Cost')::numeric;
 
    IF text_startup <= int_startup THEN
        RAISE EXCEPTION 'wide text sort startup cost mismatch: int %, text %',
            int_startup, text_startup;
    END IF;
 
    IF text_startup / int_startup <= 1.05 THEN
        RAISE EXCEPTION 'wide text sort ratio too small: int %, text %, ratio %',
            int_startup, text_startup, text_startup / int_startup;
    END IF;
 
    unique_first_sort := one_c_or_sort.first_node(
        one_c_or_sort.explain_root($sql$
            SELECT unique_integer, wide_text
            FROM one_c_or_sort.sort_cost_t
            ORDER BY unique_integer, wide_text
        $sql$),
        'Sort');
 
    low_first_sort := one_c_or_sort.first_node(
        one_c_or_sort.explain_root($sql$
            SELECT low_cardinality_integer, wide_text
            FROM one_c_or_sort.sort_cost_t
            ORDER BY low_cardinality_integer, wide_text
        $sql$),
        'Sort');
 
    IF unique_first_sort IS NULL OR low_first_sort IS NULL THEN
        RAISE EXCEPTION 'expected Sort nodes for multi-key sort-cost checks';
    END IF;
 
    unique_first_startup := (unique_first_sort ->> 'Startup Cost')::numeric;
    low_first_startup := (low_first_sort ->> 'Startup Cost')::numeric;
 
    IF low_first_startup <= unique_first_startup THEN
        RAISE EXCEPTION 'comparison-frequency sort startup cost mismatch: unique-first %, low-cardinality-first %',
            unique_first_startup, low_first_startup;
    END IF;
END;
$$;
 
CREATE TABLE merge_left_t(
    k integer,
    v integer
)
WITH (autovacuum_enabled = false);
 
CREATE TABLE merge_right_t(
    k integer,
    v integer
)
WITH (autovacuum_enabled = false);
 
INSERT INTO merge_left_t
SELECT g, g % 11
FROM generate_series(1, 500) AS g;
 
INSERT INTO merge_right_t
SELECT g, g % 13
FROM generate_series(1, 500) AS g;
 
ANALYZE merge_left_t;
ANALYZE merge_right_t;
 
DO $$
DECLARE
    prev_hashjoin text;
    prev_mergejoin text;
    prev_nestloop text;
    prev_seqscan text;
    unsorted_plan jsonb;
    ordered_plan jsonb;
    full_plan jsonb;
BEGIN
    prev_hashjoin := current_setting('enable_hashjoin');
    prev_mergejoin := current_setting('enable_mergejoin');
    prev_nestloop := current_setting('enable_nestloop');
    prev_seqscan := current_setting('enable_seqscan');
 
    PERFORM set_config('enable_nestloop', 'off', false);
    PERFORM set_config('enable_hashjoin', 'on', false);
    PERFORM set_config('enable_mergejoin', 'on', false);
 
    unsorted_plan := one_c_or_sort.explain_root($sql$
        SELECT *
        FROM one_c_or_sort.merge_left_t l
        JOIN one_c_or_sort.merge_right_t r
          ON l.k = r.k
    $sql$);
 
    IF one_c_or_sort.count_node_type(unsorted_plan, 'Hash Join') < 1 THEN
        RAISE EXCEPTION 'unsorted INNER JOIN did not produce Hash Join';
    END IF;
 
    IF one_c_or_sort.count_node_type(unsorted_plan, 'Merge Join') <> 0 THEN
        RAISE EXCEPTION 'unsorted INNER JOIN unexpectedly produced Merge Join';
    END IF;
 
    CREATE INDEX merge_left_k_idx ON one_c_or_sort.merge_left_t(k);
    CREATE INDEX merge_right_k_idx ON one_c_or_sort.merge_right_t(k);
    ANALYZE one_c_or_sort.merge_left_t;
    ANALYZE one_c_or_sort.merge_right_t;
 
    PERFORM set_config('enable_hashjoin', 'off', false);
    PERFORM set_config('enable_nestloop', 'off', false);
    PERFORM set_config('enable_mergejoin', 'on', false);
    PERFORM set_config('enable_seqscan', 'off', false);
 
    ordered_plan := one_c_or_sort.explain_root($sql$
        SELECT *
        FROM one_c_or_sort.merge_left_t l
        JOIN one_c_or_sort.merge_right_t r
          ON l.k = r.k
    $sql$);
 
    IF one_c_or_sort.count_node_type(ordered_plan, 'Merge Join') < 1 THEN
        RAISE EXCEPTION 'ordered INNER JOIN did not produce Merge Join';
    END IF;
 
    IF one_c_or_sort.count_node_type(ordered_plan, 'Sort') <> 0 THEN
        RAISE EXCEPTION 'ordered INNER JOIN unexpectedly contains Sort under Merge Join';
    END IF;
 
    PERFORM set_config('enable_hashjoin', 'off', false);
    PERFORM set_config('enable_nestloop', 'off', false);
    PERFORM set_config('enable_mergejoin', 'on', false);
    PERFORM set_config('enable_seqscan', 'on', false);
 
    full_plan := one_c_or_sort.explain_root($sql$
        SELECT *
        FROM one_c_or_sort.merge_left_t l
        FULL JOIN one_c_or_sort.merge_right_t r
          ON l.v = r.v
    $sql$);
 
    IF one_c_or_sort.count_node_type(full_plan, 'Merge Join') < 1 THEN
        RAISE EXCEPTION 'FULL JOIN did not preserve Merge Join exception';
    END IF;
 
    PERFORM set_config('enable_hashjoin', prev_hashjoin, false);
    PERFORM set_config('enable_mergejoin', prev_mergejoin, false);
    PERFORM set_config('enable_nestloop', prev_nestloop, false);
    PERFORM set_config('enable_seqscan', prev_seqscan, false);
END;
$$;
 
DROP SCHEMA one_c_or_sort CASCADE;