Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
 
# Copyright (c) 2026, PostgreSQL Global Development Group
 
# Test INSERT ON CONFLICT DO UPDATE behavior concurrent with
# CREATE INDEX CONCURRENTLY and REINDEX CONCURRENTLY.
#
# These tests verify the fix for "duplicate key value violates unique
# constraint" errors that occurred when infer_arbiter_indexes() only considered
# indisvalid indexes, causing different transactions to use different arbiter
# indexes.
 
use strict;
use warnings FATAL => 'all';
 
use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;
use Time::HiRes qw(usleep);
 
plan skip_all => 'Injection points not supported by this build'
  unless $ENV{enable_injection_points} eq 'yes';
 
# Node initialization
my $node = PostgreSQL::Test::Cluster->new('node');
$node->init();
$node->start();
 
# Check if the extension injection_points is available
plan skip_all => 'Extension injection_points not installed'
  unless $node->check_extension('injection_points');
 
$node->safe_psql('postgres', 'CREATE EXTENSION injection_points;');
 
$node->safe_psql(
    'postgres', q[
CREATE SCHEMA test;
CREATE UNLOGGED TABLE test.tblpk (i int PRIMARY KEY, updated_at timestamp);
ALTER TABLE test.tblpk SET (parallel_workers=0);
 
CREATE TABLE test.tblparted(i int primary key, updated_at timestamp) PARTITION BY RANGE (i);
CREATE TABLE test.tbl_partition PARTITION OF test.tblparted
    FOR VALUES FROM (0) TO (10000)
    WITH (parallel_workers = 0);
 
CREATE UNLOGGED TABLE test.tblexpr(i int, updated_at timestamp);
CREATE UNIQUE INDEX tbl_pkey_special ON test.tblexpr(abs(i)) WHERE i < 1000;
ALTER TABLE test.tblexpr SET (parallel_workers=0);
 
]);
 
############################################################################
note('Test: REINDEX CONCURRENTLY + UPSERT (wakeup at set-dead phase)');
 
# Create sessions with on_error_stop => 0 so psql doesn't exit on SQL errors.
# This allows us to collect stderr and detect errors after the test completes.
my $s1 = $node->background_psql('postgres', on_error_stop => 0);
my $s2 = $node->background_psql('postgres', on_error_stop => 0);
my $s3 = $node->background_psql('postgres', on_error_stop => 0);
 
# Setup injection points for each session
$s1->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('check-exclusion-or-unique-constraint-no-conflict', 'wait');
]);
 
$s2->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('exec-insert-before-insert-speculative', 'wait');
]);
 
$s3->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('reindex-relation-concurrently-before-set-dead', 'wait');
]);
 
# s3 starts REINDEX (will block on reindex-relation-concurrently-before-set-dead)
$s3->query_until(
    qr/starting_reindex/, q[
\echo starting_reindex
REINDEX INDEX CONCURRENTLY test.tblpk_pkey;
]);
 
# Wait for s3 to hit injection point
ok_injection_point($node, 'reindex-relation-concurrently-before-set-dead');
 
# s1 starts UPSERT (will block on check-exclusion-or-unique-constraint-no-conflict)
$s1->query_until(
    qr/starting_upsert_s1/, q[
\echo starting_upsert_s1
INSERT INTO test.tblpk VALUES (13,now()) ON CONFLICT (i) DO UPDATE SET updated_at = now();
]);
 
# Wait for s1 to hit injection point
ok_injection_point($node, 'check-exclusion-or-unique-constraint-no-conflict');
 
# Wakeup s3 to continue (reindex-relation-concurrently-before-set-dead)
wakeup_injection_point($node,
    'reindex-relation-concurrently-before-set-dead');
 
# s2 starts UPSERT (will block on exec-insert-before-insert-speculative)
$s2->query_until(
    qr/starting_upsert_s2/, q[
\echo starting_upsert_s2
INSERT INTO test.tblpk VALUES (13,now()) ON CONFLICT (i) DO UPDATE SET updated_at = now();
]);
 
# Wait for s2 to hit injection point
ok_injection_point($node, 'exec-insert-before-insert-speculative');
 
# Wakeup s1 (check-exclusion-or-unique-constraint-no-conflict)
wakeup_injection_point($node,
    'check-exclusion-or-unique-constraint-no-conflict');
 
# Wakeup s2 (exec-insert-before-insert-speculative)
wakeup_injection_point($node, 'exec-insert-before-insert-speculative');
 
clean_safe_quit_ok($s1, $s2, $s3);
 
# Cleanup test 1
$node->safe_psql('postgres', 'TRUNCATE TABLE test.tblpk');
 
############################################################################
note('Test: REINDEX CONCURRENTLY + UPSERT (wakeup at swap phase)');
 
$s1 = $node->background_psql('postgres', on_error_stop => 0);
$s2 = $node->background_psql('postgres', on_error_stop => 0);
$s3 = $node->background_psql('postgres', on_error_stop => 0);
 
$s1->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('check-exclusion-or-unique-constraint-no-conflict', 'wait');
]);
 
$s2->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('exec-insert-before-insert-speculative', 'wait');
]);
 
$s3->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('reindex-relation-concurrently-before-swap', 'wait');
]);
 
$s3->query_until(
    qr/starting_reindex/, q[
\echo starting_reindex
REINDEX INDEX CONCURRENTLY test.tblpk_pkey;
]);
 
ok_injection_point($node, 'reindex-relation-concurrently-before-swap');
 
$s1->query_until(
    qr/starting_upsert_s1/, q[
\echo starting_upsert_s1
INSERT INTO test.tblpk VALUES (13,now()) ON CONFLICT (i) DO UPDATE SET updated_at = now();
]);
 
ok_injection_point($node, 'check-exclusion-or-unique-constraint-no-conflict');
 
wakeup_injection_point($node, 'reindex-relation-concurrently-before-swap');
 
$s2->query_until(
    qr/starting_upsert_s2/, q[
\echo starting_upsert_s2
INSERT INTO test.tblpk VALUES (13,now()) ON CONFLICT (i) DO UPDATE SET updated_at = now();
]);
 
ok_injection_point($node, 'exec-insert-before-insert-speculative');
 
wakeup_injection_point($node, 'exec-insert-before-insert-speculative');
wakeup_injection_point($node,
    'check-exclusion-or-unique-constraint-no-conflict');
 
clean_safe_quit_ok($s1, $s2, $s3);
 
$node->safe_psql('postgres', 'TRUNCATE TABLE test.tblpk');
 
############################################################################
note('Test: REINDEX CONCURRENTLY + UPSERT (s1 wakes before reindex)');
 
$s1 = $node->background_psql('postgres', on_error_stop => 0);
$s2 = $node->background_psql('postgres', on_error_stop => 0);
$s3 = $node->background_psql('postgres', on_error_stop => 0);
 
$s1->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('check-exclusion-or-unique-constraint-no-conflict', 'wait');
]);
 
$s2->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('exec-insert-before-insert-speculative', 'wait');
]);
 
$s3->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('reindex-relation-concurrently-before-set-dead', 'wait');
]);
 
$s3->query_until(
    qr/starting_reindex/, q[
\echo starting_reindex
REINDEX INDEX CONCURRENTLY test.tblpk_pkey;
]);
 
ok_injection_point($node, 'reindex-relation-concurrently-before-set-dead');
 
$s1->query_until(
    qr/starting_upsert_s1/, q[
\echo starting_upsert_s1
INSERT INTO test.tblpk VALUES (13,now()) ON CONFLICT (i) DO UPDATE SET updated_at = now();
]);
 
ok_injection_point($node, 'check-exclusion-or-unique-constraint-no-conflict');
 
# Start s2 BEFORE waking reindex (key difference from permutation 1)
$s2->query_until(
    qr/starting_upsert_s2/, q[
\echo starting_upsert_s2
INSERT INTO test.tblpk VALUES (13,now()) ON CONFLICT (i) DO UPDATE SET updated_at = now();
]);
 
ok_injection_point($node, 'exec-insert-before-insert-speculative');
 
# Wake s1 first, then reindex, then s2
wakeup_injection_point($node,
    'check-exclusion-or-unique-constraint-no-conflict');
wakeup_injection_point($node,
    'reindex-relation-concurrently-before-set-dead');
wakeup_injection_point($node, 'exec-insert-before-insert-speculative');
 
clean_safe_quit_ok($s1, $s2, $s3);
 
$node->safe_psql('postgres', 'TRUNCATE TABLE test.tblpk');
 
############################################################################
note('Test: REINDEX + UPSERT ON CONSTRAINT (set-dead phase)');
 
$s1 = $node->background_psql('postgres', on_error_stop => 0);
$s2 = $node->background_psql('postgres', on_error_stop => 0);
$s3 = $node->background_psql('postgres', on_error_stop => 0);
 
$s1->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('check-exclusion-or-unique-constraint-no-conflict', 'wait');
]);
 
$s2->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('exec-insert-before-insert-speculative', 'wait');
]);
 
$s3->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('reindex-relation-concurrently-before-set-dead', 'wait');
]);
 
$s3->query_until(
    qr/starting_reindex/, q[
\echo starting_reindex
REINDEX INDEX CONCURRENTLY test.tblpk_pkey;
]);
 
ok_injection_point($node, 'reindex-relation-concurrently-before-set-dead');
 
$s1->query_until(
    qr/starting_upsert_s1/, q[
\echo starting_upsert_s1
INSERT INTO test.tblpk VALUES (13, now()) ON CONFLICT ON CONSTRAINT tblpk_pkey DO UPDATE SET updated_at = now();
]);
 
ok_injection_point($node, 'check-exclusion-or-unique-constraint-no-conflict');
 
wakeup_injection_point($node,
    'reindex-relation-concurrently-before-set-dead');
 
$s2->query_until(
    qr/starting_upsert_s2/, q[
\echo starting_upsert_s2
INSERT INTO test.tblpk VALUES (13, now()) ON CONFLICT ON CONSTRAINT tblpk_pkey DO UPDATE SET updated_at = now();
]);
 
ok_injection_point($node, 'exec-insert-before-insert-speculative');
 
wakeup_injection_point($node,
    'check-exclusion-or-unique-constraint-no-conflict');
wakeup_injection_point($node, 'exec-insert-before-insert-speculative');
 
clean_safe_quit_ok($s1, $s2, $s3);
 
$node->safe_psql('postgres', 'TRUNCATE TABLE test.tblpk');
 
############################################################################
note('Test: REINDEX + UPSERT ON CONSTRAINT (swap phase)');
 
$s1 = $node->background_psql('postgres', on_error_stop => 0);
$s2 = $node->background_psql('postgres', on_error_stop => 0);
$s3 = $node->background_psql('postgres', on_error_stop => 0);
 
$s1->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('check-exclusion-or-unique-constraint-no-conflict', 'wait');
]);
 
$s2->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('exec-insert-before-insert-speculative', 'wait');
]);
 
$s3->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('reindex-relation-concurrently-before-swap', 'wait');
]);
 
$s3->query_until(
    qr/starting_reindex/, q[
\echo starting_reindex
REINDEX INDEX CONCURRENTLY test.tblpk_pkey;
]);
 
ok_injection_point($node, 'reindex-relation-concurrently-before-swap');
 
$s1->query_until(
    qr/starting_upsert_s1/, q[
\echo starting_upsert_s1
INSERT INTO test.tblpk VALUES (13, now()) ON CONFLICT ON CONSTRAINT tblpk_pkey DO UPDATE SET updated_at = now();
]);
 
ok_injection_point($node, 'check-exclusion-or-unique-constraint-no-conflict');
 
wakeup_injection_point($node, 'reindex-relation-concurrently-before-swap');
 
$s2->query_until(
    qr/starting_upsert_s2/, q[
\echo starting_upsert_s2
INSERT INTO test.tblpk VALUES (13, now()) ON CONFLICT ON CONSTRAINT tblpk_pkey DO UPDATE SET updated_at = now();
]);
 
ok_injection_point($node, 'exec-insert-before-insert-speculative');
 
wakeup_injection_point($node, 'exec-insert-before-insert-speculative');
wakeup_injection_point($node,
    'check-exclusion-or-unique-constraint-no-conflict');
 
clean_safe_quit_ok($s1, $s2, $s3);
 
$node->safe_psql('postgres', 'TRUNCATE TABLE test.tblpk');
 
############################################################################
note('Test: REINDEX + UPSERT ON CONSTRAINT (s1 wakes before reindex)');
 
$s1 = $node->background_psql('postgres', on_error_stop => 0);
$s2 = $node->background_psql('postgres', on_error_stop => 0);
$s3 = $node->background_psql('postgres', on_error_stop => 0);
 
$s1->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('check-exclusion-or-unique-constraint-no-conflict', 'wait');
]);
 
$s2->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('exec-insert-before-insert-speculative', 'wait');
]);
 
$s3->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('reindex-relation-concurrently-before-set-dead', 'wait');
]);
 
$s3->query_until(
    qr/starting_reindex/, q[
\echo starting_reindex
REINDEX INDEX CONCURRENTLY test.tblpk_pkey;
]);
 
ok_injection_point($node, 'reindex-relation-concurrently-before-set-dead');
 
$s1->query_until(
    qr/starting_upsert_s1/, q[
\echo starting_upsert_s1
INSERT INTO test.tblpk VALUES (13, now()) ON CONFLICT ON CONSTRAINT tblpk_pkey DO UPDATE SET updated_at = now();
]);
 
ok_injection_point($node, 'check-exclusion-or-unique-constraint-no-conflict');
 
# Start s2 BEFORE waking reindex
$s2->query_until(
    qr/starting_upsert_s2/, q[
\echo starting_upsert_s2
INSERT INTO test.tblpk VALUES (13, now()) ON CONFLICT ON CONSTRAINT tblpk_pkey DO UPDATE SET updated_at = now();
]);
 
ok_injection_point($node, 'exec-insert-before-insert-speculative');
 
# Wake s1 first, then reindex, then s2
wakeup_injection_point($node,
    'check-exclusion-or-unique-constraint-no-conflict');
wakeup_injection_point($node,
    'reindex-relation-concurrently-before-set-dead');
wakeup_injection_point($node, 'exec-insert-before-insert-speculative');
 
clean_safe_quit_ok($s1, $s2, $s3);
 
$node->safe_psql('postgres', 'TRUNCATE TABLE test.tblpk');
 
############################################################################
note('Test: REINDEX on partitioned table (set-dead phase)');
 
$s1 = $node->background_psql('postgres', on_error_stop => 0);
$s2 = $node->background_psql('postgres', on_error_stop => 0);
$s3 = $node->background_psql('postgres', on_error_stop => 0);
 
$s1->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('check-exclusion-or-unique-constraint-no-conflict', 'wait');
]);
 
$s2->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('exec-insert-before-insert-speculative', 'wait');
]);
 
$s3->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('reindex-relation-concurrently-before-set-dead', 'wait');
]);
 
$s3->query_until(
    qr/starting_reindex/, q[
\echo starting_reindex
REINDEX INDEX CONCURRENTLY test.tbl_partition_pkey;
]);
 
ok_injection_point($node, 'reindex-relation-concurrently-before-set-dead');
 
$s1->query_until(
    qr/starting_upsert_s1/, q[
\echo starting_upsert_s1
INSERT INTO test.tblparted VALUES (13, now()) ON CONFLICT (i) DO UPDATE SET updated_at = now();
]);
 
ok_injection_point($node, 'check-exclusion-or-unique-constraint-no-conflict');
 
wakeup_injection_point($node,
    'reindex-relation-concurrently-before-set-dead');
 
$s2->query_until(
    qr/starting_upsert_s2/, q[
\echo starting_upsert_s2
INSERT INTO test.tblparted VALUES (13, now()) ON CONFLICT (i) DO UPDATE SET updated_at = now();
]);
 
ok_injection_point($node, 'exec-insert-before-insert-speculative');
 
wakeup_injection_point($node,
    'check-exclusion-or-unique-constraint-no-conflict');
wakeup_injection_point($node, 'exec-insert-before-insert-speculative');
 
clean_safe_quit_ok($s1, $s2, $s3);
 
$node->safe_psql('postgres', 'TRUNCATE TABLE test.tblparted');
 
############################################################################
note('Test: REINDEX on partitioned table (swap phase)');
 
$s1 = $node->background_psql('postgres', on_error_stop => 0);
$s2 = $node->background_psql('postgres', on_error_stop => 0);
$s3 = $node->background_psql('postgres', on_error_stop => 0);
 
$s1->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('check-exclusion-or-unique-constraint-no-conflict', 'wait');
]);
 
$s2->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('exec-insert-before-insert-speculative', 'wait');
]);
 
$s3->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('reindex-relation-concurrently-before-swap', 'wait');
]);
 
$s3->query_until(
    qr/starting_reindex/, q[
\echo starting_reindex
REINDEX INDEX CONCURRENTLY test.tbl_partition_pkey;
]);
 
ok_injection_point($node, 'reindex-relation-concurrently-before-swap');
 
$s1->query_until(
    qr/starting_upsert_s1/, q[
\echo starting_upsert_s1
INSERT INTO test.tblparted VALUES (13, now()) ON CONFLICT (i) DO UPDATE SET updated_at = now();
]);
 
ok_injection_point($node, 'check-exclusion-or-unique-constraint-no-conflict');
 
wakeup_injection_point($node, 'reindex-relation-concurrently-before-swap');
 
$s2->query_until(
    qr/starting_upsert_s2/, q[
\echo starting_upsert_s2
INSERT INTO test.tblparted VALUES (13, now()) ON CONFLICT (i) DO UPDATE SET updated_at = now();
]);
 
ok_injection_point($node, 'exec-insert-before-insert-speculative');
 
wakeup_injection_point($node, 'exec-insert-before-insert-speculative');
wakeup_injection_point($node,
    'check-exclusion-or-unique-constraint-no-conflict');
 
clean_safe_quit_ok($s1, $s2, $s3);
 
$node->safe_psql('postgres', 'TRUNCATE TABLE test.tblparted');
 
############################################################################
note('Test: REINDEX on partitioned table (s1 wakes before reindex)');
 
$s1 = $node->background_psql('postgres', on_error_stop => 0);
$s2 = $node->background_psql('postgres', on_error_stop => 0);
$s3 = $node->background_psql('postgres', on_error_stop => 0);
 
$s1->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('check-exclusion-or-unique-constraint-no-conflict', 'wait');
]);
 
$s2->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('exec-insert-before-insert-speculative', 'wait');
]);
 
$s3->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('reindex-relation-concurrently-before-set-dead', 'wait');
]);
 
$s3->query_until(
    qr/starting_reindex/, q[
\echo starting_reindex
REINDEX INDEX CONCURRENTLY test.tbl_partition_pkey;
]);
 
ok_injection_point($node, 'reindex-relation-concurrently-before-set-dead');
 
$s1->query_until(
    qr/starting_upsert_s1/, q[
\echo starting_upsert_s1
INSERT INTO test.tblparted VALUES (13, now()) ON CONFLICT (i) DO UPDATE SET updated_at = now();
]);
 
ok_injection_point($node, 'check-exclusion-or-unique-constraint-no-conflict');
 
# Start s2 BEFORE waking reindex
$s2->query_until(
    qr/starting_upsert_s2/, q[
\echo starting_upsert_s2
INSERT INTO test.tblparted VALUES (13, now()) ON CONFLICT (i) DO UPDATE SET updated_at = now();
]);
 
ok_injection_point($node, 'exec-insert-before-insert-speculative');
 
# Wake s1 first, then reindex, then s2
wakeup_injection_point($node,
    'check-exclusion-or-unique-constraint-no-conflict');
wakeup_injection_point($node,
    'reindex-relation-concurrently-before-set-dead');
wakeup_injection_point($node, 'exec-insert-before-insert-speculative');
 
clean_safe_quit_ok($s1, $s2, $s3);
 
$node->safe_psql('postgres', 'TRUNCATE TABLE test.tblparted');
 
############################################################################
note(
    'Test: REINDEX on partitioned table, cache inval between two get_partition_ancestors'
);
 
$s1 = $node->background_psql('postgres', on_error_stop => 0);
$s2 = $node->background_psql('postgres', on_error_stop => 0);
$s3 = $node->background_psql('postgres', on_error_stop => 0);
 
$s1->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('exec-init-partition-after-get-partition-ancestors', 'wait');
]);
 
$s2->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('reindex-relation-concurrently-before-swap', 'wait');
]);
 
$s2->query_until(
    qr/starting_reindex/, q[
\echo starting_reindex
REINDEX INDEX CONCURRENTLY test.tbl_partition_pkey;
]);
 
ok_injection_point($node, 'reindex-relation-concurrently-before-swap');
 
$s1->query_until(
    qr/starting_upsert_s1/, q[
\echo starting_upsert_s1
INSERT INTO test.tblparted VALUES (13, now()) ON CONFLICT (i) DO UPDATE SET updated_at = now();
]);
 
ok_injection_point($node,
    'exec-init-partition-after-get-partition-ancestors');
 
wakeup_injection_point($node, 'reindex-relation-concurrently-before-swap');
 
wakeup_injection_point($node,
    'exec-init-partition-after-get-partition-ancestors');
 
clean_safe_quit_ok($s1, $s2, $s3);
 
$node->safe_psql('postgres', 'TRUNCATE TABLE test.tblparted');
 
############################################################################
note('Test: CREATE INDEX CONCURRENTLY + UPSERT');
# Uses invalidate-catalog-snapshot-end to test catalog invalidation
# during UPSERT
 
$s1 = $node->background_psql('postgres', on_error_stop => 0);
$s2 = $node->background_psql('postgres', on_error_stop => 0);
$s3 = $node->background_psql('postgres', on_error_stop => 0);
 
my $s1_pid = $s1->query_safe('SELECT pg_backend_pid()');
 
# s1 attaches BOTH injection points - the unique constraint check AND catalog snapshot
$s1->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('check-exclusion-or-unique-constraint-no-conflict', 'wait');
]);
 
$s1->query_until(
    qr/attaching_injection_point/, q[
\echo attaching_injection_point
SELECT injection_points_attach('invalidate-catalog-snapshot-end', 'wait');
]);
 
# In cases of cache clobbering, s1 may hit the injection point during attach.
# Wait for that session to become idle (attach completed), or wake it up if
# it becomes stuck on injection point.
if (!wait_for_idle($node, $s1_pid))
{
    ok_injection_point(
        $node,
        'invalidate-catalog-snapshot-end',
        's1 hit injection point during attach (cache clobbering mode)');
    $node->safe_psql(
        'postgres', q[
        SELECT injection_points_wakeup('invalidate-catalog-snapshot-end');
    ]);
}
 
$s2->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('exec-insert-before-insert-speculative', 'wait');
]);
 
$s3->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('define-index-before-set-valid', 'wait');
]);
 
# s3: Start CREATE INDEX CONCURRENTLY (blocks on define-index-before-set-valid)
$s3->query_until(
    qr/starting_create_index/, q[
\echo starting_create_index
CREATE UNIQUE INDEX CONCURRENTLY tbl_pkey_duplicate ON test.tblpk(i);
]);
 
ok_injection_point($node, 'define-index-before-set-valid');
 
# s1: Start UPSERT (blocks on invalidate-catalog-snapshot-end)
$s1->query_until(
    qr/starting_upsert_s1/, q[
\echo starting_upsert_s1
INSERT INTO test.tblpk VALUES (13,now()) ON CONFLICT (i) DO UPDATE SET updated_at = now();
]);
 
ok_injection_point($node, 'invalidate-catalog-snapshot-end');
 
# Wakeup s3 (CREATE INDEX continues, triggers catalog invalidation)
wakeup_injection_point($node, 'define-index-before-set-valid');
 
# s2: Start UPSERT (blocks on exec-insert-before-insert-speculative)
$s2->query_until(
    qr/starting_upsert_s2/, q[
\echo starting_upsert_s2
INSERT INTO test.tblpk VALUES (13,now()) ON CONFLICT (i) DO UPDATE SET updated_at = now();
]);
 
ok_injection_point($node, 'exec-insert-before-insert-speculative');
 
wakeup_injection_point($node, 'invalidate-catalog-snapshot-end');
 
ok_injection_point($node, 'check-exclusion-or-unique-constraint-no-conflict');
 
wakeup_injection_point($node, 'exec-insert-before-insert-speculative');
 
wakeup_injection_point($node,
    'check-exclusion-or-unique-constraint-no-conflict');
 
clean_safe_quit_ok($s1, $s2, $s3);
 
$node->safe_psql('postgres', 'TRUNCATE TABLE test.tblparted');
 
############################################################################
note('Test: CREATE INDEX CONCURRENTLY on partial index + UPSERT');
# Uses invalidate-catalog-snapshot-end to test catalog invalidation during UPSERT
 
$s1 = $node->background_psql('postgres', on_error_stop => 0);
$s2 = $node->background_psql('postgres', on_error_stop => 0);
$s3 = $node->background_psql('postgres', on_error_stop => 0);
 
$s1_pid = $s1->query_safe('SELECT pg_backend_pid()');
 
# s1 attaches BOTH injection points - the unique constraint check AND catalog snapshot
$s1->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('check-exclusion-or-unique-constraint-no-conflict', 'wait');
]);
 
$s1->query_until(
    qr/attaching_injection_point/, q[
\echo attaching_injection_point
SELECT injection_points_attach('invalidate-catalog-snapshot-end', 'wait');
]);
 
# In cases of cache clobbering, s1 may hit the injection point during attach.
# Wait for that session to become idle (attach completed), or wake it up if
# it becomes stuck on injection point.
if (!wait_for_idle($node, $s1_pid))
{
    ok_injection_point(
        $node,
        'invalidate-catalog-snapshot-end',
        's1 hit injection point during attach (cache clobbering mode)');
    $node->safe_psql(
        'postgres', q[
        SELECT injection_points_wakeup('invalidate-catalog-snapshot-end');
    ]);
}
 
$s2->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('exec-insert-before-insert-speculative', 'wait');
]);
 
$s3->query_safe(
    q[
SELECT injection_points_set_local();
SELECT injection_points_attach('define-index-before-set-valid', 'wait');
]);
 
# s3: Start CREATE INDEX CONCURRENTLY (blocks on define-index-before-set-valid)
$s3->query_until(
    qr/starting_create_index/, q[
\echo starting_create_index
CREATE UNIQUE INDEX CONCURRENTLY tbl_pkey_special_duplicate ON test.tblexpr(abs(i)) WHERE i < 10000;
]);
 
ok_injection_point($node, 'define-index-before-set-valid');
 
# s1: Start UPSERT (blocks on invalidate-catalog-snapshot-end)
$s1->query_until(
    qr/starting_upsert_s1/, q[
\echo starting_upsert_s1
INSERT INTO test.tblexpr VALUES(13,now()) ON CONFLICT (abs(i)) WHERE i < 100 DO UPDATE SET updated_at = now();
]);
 
ok_injection_point($node, 'invalidate-catalog-snapshot-end');
 
# Wakeup s3 (CREATE INDEX continues, triggers catalog invalidation)
wakeup_injection_point($node, 'define-index-before-set-valid');
 
# s2: Start UPSERT (blocks on exec-insert-before-insert-speculative)
$s2->query_until(
    qr/starting_upsert_s2/, q[
\echo starting_upsert_s2
INSERT INTO test.tblexpr VALUES(13,now()) ON CONFLICT (abs(i)) WHERE i < 100 DO UPDATE SET updated_at = now();
]);
 
ok_injection_point($node, 'exec-insert-before-insert-speculative');
wakeup_injection_point($node, 'invalidate-catalog-snapshot-end');
ok_injection_point($node, 'check-exclusion-or-unique-constraint-no-conflict');
wakeup_injection_point($node, 'exec-insert-before-insert-speculative');
wakeup_injection_point($node,
    'check-exclusion-or-unique-constraint-no-conflict');
 
clean_safe_quit_ok($s1, $s2, $s3);
 
$node->safe_psql('postgres', 'TRUNCATE TABLE test.tblexpr');
 
done_testing();
 
############################################################################
# Helper functions
#
############################################################################
 
# Helper: Wait for a session to hit an injection point.
# Optional second argument is timeout in seconds.
# Returns true if found, false if timeout.
# On timeout, logs diagnostic information about all active queries.
sub wait_for_injection_point
{
    my ($node, $point_name, $timeout) = @_;
    $timeout //= $PostgreSQL::Test::Utils::timeout_default / 2;
 
    for (my $elapsed = 0; $elapsed < $timeout * 10; $elapsed++)
    {
        my $pid = $node->safe_psql(
            'postgres', qq[
            SELECT pid FROM pg_stat_activity
            WHERE wait_event_type = 'InjectionPoint'
              AND wait_event = '$point_name'
            LIMIT 1;
        ]);
        return 1 if $pid ne '';
        usleep(100_000);
    }
 
    # Timeout - report diagnostic information
    my $activity = $node->safe_psql(
        'postgres', q[
        SELECT format('pid=%s, state=%s, wait_event_type=%s, wait_event=%s, backend_xmin=%s, backend_xid=%s, query=%s',
            pid, state, wait_event_type, wait_event, backend_xmin, backend_xid, left(query, 100))
        FROM pg_stat_activity
        ORDER BY pid;
    ]);
    diag(   "wait_for_injection_point timeout waiting for: $point_name\n"
          . "Current queries in pg_stat_activity:\n$activity");
 
    return 0;
}
 
# Test helper: ok() a wait for the given injection point
# Third argument is an optional test name.
sub ok_injection_point
{
    my ($node, $injection_point, $testname) = @_;
    $testname //= "hit injection point $injection_point";
 
    ok(wait_for_injection_point($node, $injection_point), $testname);
}
 
# Helper: Wait for a specific backend to become idle.
# Returns true if idle, false if waiting for injection point or timeout.
sub wait_for_idle
{
    my ($node, $pid, $timeout) = @_;
    $timeout //= $PostgreSQL::Test::Utils::timeout_default / 2;
 
    for (my $elapsed = 0; $elapsed < $timeout * 10; $elapsed++)
    {
        my $result = $node->safe_psql(
            'postgres', qq[
            SELECT state, wait_event_type FROM pg_stat_activity WHERE pid = $pid;
        ]);
        my ($state, $wait_event_type) = split(/\|/, $result, 2);
        $state           //= '';
        $wait_event_type //= '';
        return 1 if $state eq 'idle';
        return 0 if $wait_event_type eq 'InjectionPoint';
 
        usleep(100_000);
    }
    return 0;
}
 
# Helper: Detach and wakeup an injection point
sub wakeup_injection_point
{
    my ($node, $point_name) = @_;
    $node->safe_psql(
        'postgres', qq[
SELECT injection_points_detach('$point_name');
SELECT injection_points_wakeup('$point_name');
]);
}
 
# Wait for any pending query to complete, capture stderr, and close the session.
# Returns the stderr output (excluding internal markers).
sub safe_quit
{
    my ($session) = @_;
 
    # Send a marker and wait for it to ensure any pending query completes
    my $banner = "safe_quit_marker";
    my $banner_match = qr/(^|\n)$banner\r?\n/;
 
    $session->{stdin} .= "\\echo $banner\n\\warn $banner\n";
 
    pump_until(
        $session->{run}, $session->{timeout},
        \$session->{stdout}, $banner_match);
    pump_until(
        $session->{run}, $session->{timeout},
        \$session->{stderr}, $banner_match);
 
    # Capture stderr (excluding the banner)
    my $stderr = $session->{stderr};
    $stderr =~ s/$banner_match//;
 
    # Close the session
    $session->quit;
 
    return $stderr;
}
 
# Helper function: verify that the given sessions exit cleanly.
sub clean_safe_quit_ok
{
    my $i = 1;
    foreach my $session (@_)
    {
        is(safe_quit($session), '', "session " . $i++ . " quit cleanly");
    }
}