Исходный код СУБД 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
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
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
--
-- insert...on conflict do unique index inference
--
create table insertconflicttest(key int4, fruit text);
-- invalid clauses
insert into insertconflicttest values (1) on conflict (key int4_ops (fillfactor=10)) do nothing;
ERROR:  operator class options are not allowed in ON CONFLICT clause
LINE 1: ...t into insertconflicttest values (1) on conflict (key int4_o...
                                                             ^
insert into insertconflicttest values (1) on conflict (key asc) do nothing;
ERROR:  ASC/DESC is not allowed in ON CONFLICT clause
LINE 1: ...t into insertconflicttest values (1) on conflict (key asc) d...
                                                             ^
insert into insertconflicttest values (1) on conflict (key nulls last) do nothing;
ERROR:  NULLS FIRST/LAST is not allowed in ON CONFLICT clause
LINE 1: ...t into insertconflicttest values (1) on conflict (key nulls ...
                                                             ^
-- These things should work through a view, as well
create view insertconflictview as select * from insertconflicttest;
--
-- Test unique index inference with operator class specifications and
-- named collations
--
create unique index op_index_key on insertconflicttest(key, fruit text_pattern_ops);
create unique index collation_index_key on insertconflicttest(key, fruit collate "C");
create unique index both_index_key on insertconflicttest(key, fruit collate "C" text_pattern_ops);
create unique index both_index_expr_key on insertconflicttest(key, lower(fruit) collate "C" text_pattern_ops);
-- fails
explain (costs off) insert into insertconflicttest values(0, 'Crowberry') on conflict (key) do nothing;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
explain (costs off) insert into insertconflicttest values(0, 'Crowberry') on conflict (fruit) do nothing;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
-- succeeds
explain (costs off) insert into insertconflicttest values(0, 'Crowberry') on conflict (key, fruit) do nothing;
                                  QUERY PLAN                                   
-------------------------------------------------------------------------------
 Insert on insertconflicttest
   Conflict Resolution: NOTHING
   Conflict Arbiter Indexes: op_index_key, collation_index_key, both_index_key
   ->  Result
(4 rows)
 
explain (costs off) insert into insertconflicttest values(0, 'Crowberry') on conflict (fruit, key, fruit, key) do nothing;
                                  QUERY PLAN                                   
-------------------------------------------------------------------------------
 Insert on insertconflicttest
   Conflict Resolution: NOTHING
   Conflict Arbiter Indexes: op_index_key, collation_index_key, both_index_key
   ->  Result
(4 rows)
 
explain (costs off) insert into insertconflicttest values(0, 'Crowberry') on conflict (lower(fruit), key, lower(fruit), key) do nothing;
                   QUERY PLAN                    
-------------------------------------------------
 Insert on insertconflicttest
   Conflict Resolution: NOTHING
   Conflict Arbiter Indexes: both_index_expr_key
   ->  Result
(4 rows)
 
explain (costs off) insert into insertconflictview values(0, 'Crowberry') on conflict (lower(fruit), key, lower(fruit), key) do nothing;
                   QUERY PLAN                    
-------------------------------------------------
 Insert on insertconflicttest
   Conflict Resolution: NOTHING
   Conflict Arbiter Indexes: both_index_expr_key
   ->  Result
(4 rows)
 
explain (costs off) insert into insertconflicttest values(0, 'Crowberry') on conflict (key, fruit) do update set fruit = excluded.fruit
  where exists (select 1 from insertconflicttest ii where ii.key = excluded.key);
                                  QUERY PLAN                                   
-------------------------------------------------------------------------------
 Insert on insertconflicttest
   Conflict Resolution: UPDATE
   Conflict Arbiter Indexes: op_index_key, collation_index_key, both_index_key
   Conflict Filter: EXISTS(SubPlan exists_1)
   ->  Result
   SubPlan exists_1
     ->  Index Only Scan using both_index_expr_key on insertconflicttest ii
           Index Cond: (key = excluded.key)
(8 rows)
 
-- Neither collation nor operator class specifications are required --
-- supplying them merely *limits* matches to indexes with matching opclasses
-- used for relevant indexes
explain (costs off) insert into insertconflicttest values(0, 'Crowberry') on conflict (key, fruit text_pattern_ops) do nothing;
                        QUERY PLAN                        
----------------------------------------------------------
 Insert on insertconflicttest
   Conflict Resolution: NOTHING
   Conflict Arbiter Indexes: op_index_key, both_index_key
   ->  Result
(4 rows)
 
-- Okay, arbitrates using both index where text_pattern_ops opclass does and
-- does not appear.
explain (costs off) insert into insertconflicttest values(0, 'Crowberry') on conflict (key, fruit collate "C") do nothing;
                           QUERY PLAN                            
-----------------------------------------------------------------
 Insert on insertconflicttest
   Conflict Resolution: NOTHING
   Conflict Arbiter Indexes: collation_index_key, both_index_key
   ->  Result
(4 rows)
 
-- Okay, but only accepts the single index where both opclass and collation are
-- specified
explain (costs off) insert into insertconflicttest values(0, 'Crowberry') on conflict (fruit collate "C" text_pattern_ops, key) do nothing;
                 QUERY PLAN                 
--------------------------------------------
 Insert on insertconflicttest
   Conflict Resolution: NOTHING
   Conflict Arbiter Indexes: both_index_key
   ->  Result
(4 rows)
 
-- Okay, but only accepts the single index where both opclass and collation are
-- specified (plus expression variant)
explain (costs off) insert into insertconflicttest values(0, 'Crowberry') on conflict (lower(fruit) collate "C", key, key) do nothing;
                   QUERY PLAN                    
-------------------------------------------------
 Insert on insertconflicttest
   Conflict Resolution: NOTHING
   Conflict Arbiter Indexes: both_index_expr_key
   ->  Result
(4 rows)
 
-- Attribute appears twice, while not all attributes/expressions on attributes
-- appearing within index definition match in terms of both opclass and
-- collation.
--
-- Works because every attribute in inference specification needs to be
-- satisfied once or more by cataloged index attribute, and as always when an
-- attribute in the cataloged definition has a non-default opclass/collation,
-- it still satisfied some inference attribute lacking any particular
-- opclass/collation specification.
--
-- The implementation is liberal in accepting inference specifications on the
-- assumption that multiple inferred unique indexes will prevent problematic
-- cases.  It rolls with unique indexes where attributes redundantly appear
-- multiple times, too (which is not tested here).
explain (costs off) insert into insertconflicttest values(0, 'Crowberry') on conflict (fruit, key, fruit text_pattern_ops, key) do nothing;
                        QUERY PLAN                        
----------------------------------------------------------
 Insert on insertconflicttest
   Conflict Resolution: NOTHING
   Conflict Arbiter Indexes: op_index_key, both_index_key
   ->  Result
(4 rows)
 
explain (costs off) insert into insertconflicttest values(0, 'Crowberry') on conflict (lower(fruit) collate "C" text_pattern_ops, key, key) do nothing;
                   QUERY PLAN                    
-------------------------------------------------
 Insert on insertconflicttest
   Conflict Resolution: NOTHING
   Conflict Arbiter Indexes: both_index_expr_key
   ->  Result
(4 rows)
 
drop index op_index_key;
drop index collation_index_key;
drop index both_index_key;
drop index both_index_expr_key;
--
-- Make sure that cross matching of attribute opclass/collation does not occur
--
create unique index cross_match on insertconflicttest(lower(fruit) collate "C", upper(fruit) text_pattern_ops);
-- fails:
explain (costs off) insert into insertconflicttest values(0, 'Crowberry') on conflict (lower(fruit) text_pattern_ops, upper(fruit) collate "C") do nothing;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
-- works:
explain (costs off) insert into insertconflicttest values(0, 'Crowberry') on conflict (lower(fruit) collate "C", upper(fruit) text_pattern_ops) do nothing;
               QUERY PLAN                
-----------------------------------------
 Insert on insertconflicttest
   Conflict Resolution: NOTHING
   Conflict Arbiter Indexes: cross_match
   ->  Result
(4 rows)
 
drop index cross_match;
--
-- Index expressions that are simplified to a plain column can be inferred
--
create table insertconflict_nn (a int not null, b text);
create unique index nn_expr_key on insertconflict_nn(coalesce(a, 0), lower(b));
explain (costs off) insert into insertconflict_nn values(1, 'x') on conflict (coalesce(a, 0), lower(b)) do nothing;
               QUERY PLAN                
-----------------------------------------
 Insert on insertconflict_nn
   Conflict Resolution: NOTHING
   Conflict Arbiter Indexes: nn_expr_key
   ->  Result
(4 rows)
 
explain (costs off) insert into insertconflict_nn values(1, 'x') on conflict (a int4_ops, lower(b)) do nothing;
               QUERY PLAN                
-----------------------------------------
 Insert on insertconflict_nn
   Conflict Resolution: NOTHING
   Conflict Arbiter Indexes: nn_expr_key
   ->  Result
(4 rows)
 
-- fails:
explain (costs off) insert into insertconflict_nn values(1, 'x') on conflict (a) do nothing;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
drop table insertconflict_nn;
--
-- Single key tests
--
create unique index key_index on insertconflicttest(key);
--
-- Explain tests
--
explain (costs off) insert into insertconflicttest values (0, 'Bilberry') on conflict (key) do update set fruit = excluded.fruit;
              QUERY PLAN               
---------------------------------------
 Insert on insertconflicttest
   Conflict Resolution: UPDATE
   Conflict Arbiter Indexes: key_index
   ->  Result
(4 rows)
 
-- Should display qual actually attributable to internal sequential scan:
explain (costs off) insert into insertconflicttest values (0, 'Bilberry') on conflict (key) do update set fruit = excluded.fruit where insertconflicttest.fruit != 'Cawesh';
                           QUERY PLAN                            
-----------------------------------------------------------------
 Insert on insertconflicttest
   Conflict Resolution: UPDATE
   Conflict Arbiter Indexes: key_index
   Conflict Filter: (insertconflicttest.fruit <> 'Cawesh'::text)
   ->  Result
(5 rows)
 
-- With EXCLUDED.* expression in scan node:
explain (costs off) insert into insertconflicttest values(0, 'Crowberry') on conflict (key) do update set fruit = excluded.fruit where excluded.fruit != 'Elderberry';
                        QUERY PLAN                         
-----------------------------------------------------------
 Insert on insertconflicttest
   Conflict Resolution: UPDATE
   Conflict Arbiter Indexes: key_index
   Conflict Filter: (excluded.fruit <> 'Elderberry'::text)
   ->  Result
(5 rows)
 
-- Does the same, but JSON format shows "Conflict Arbiter Index" as JSON array:
explain (costs off, format json) insert into insertconflicttest values (0, 'Bilberry') on conflict (key) do update set fruit = excluded.fruit where insertconflicttest.fruit != 'Lime' returning *;
                               QUERY PLAN                               
------------------------------------------------------------------------
 [                                                                     +
   {                                                                   +
     "Plan": {                                                         +
       "Node Type": "ModifyTable",                                     +
       "Operation": "Insert",                                          +
       "Parallel Aware": false,                                        +
       "Async Capable": false,                                         +
       "Relation Name": "insertconflicttest",                          +
       "Alias": "insertconflicttest",                                  +
       "Disabled": false,                                              +
       "Conflict Resolution": "UPDATE",                                +
       "Conflict Arbiter Indexes": ["key_index"],                      +
       "Conflict Filter": "(insertconflicttest.fruit <> 'Lime'::text)",+
       "Plans": [                                                      +
         {                                                             +
           "Node Type": "Result",                                      +
           "Parent Relationship": "Outer",                             +
           "Parallel Aware": false,                                    +
           "Async Capable": false,                                     +
           "Disabled": false                                           +
         }                                                             +
       ]                                                               +
     }                                                                 +
   }                                                                   +
 ]
(1 row)
 
-- Should display lock strength, if specified
explain (costs off) insert into insertconflicttest values (1, 'Apple') on conflict (key) do select returning *;
              QUERY PLAN               
---------------------------------------
 Insert on insertconflicttest
   Conflict Resolution: SELECT
   Conflict Arbiter Indexes: key_index
   ->  Result
(4 rows)
 
explain (costs off) insert into insertconflicttest values (1, 'Apple') on conflict (key) do select for key share returning *;
                 QUERY PLAN                  
---------------------------------------------
 Insert on insertconflicttest
   Conflict Resolution: SELECT FOR KEY SHARE
   Conflict Arbiter Indexes: key_index
   ->  Result
(4 rows)
 
-- Fails (no unique index inference specification, required for do update variant):
insert into insertconflicttest values (1, 'Apple') on conflict do update set fruit = excluded.fruit;
ERROR:  ON CONFLICT DO UPDATE requires inference specification or constraint name
LINE 1: ...nsert into insertconflicttest values (1, 'Apple') on conflic...
                                                             ^
HINT:  For example, ON CONFLICT (column_name).
-- inference succeeds:
insert into insertconflicttest values (1, 'Apple') on conflict (key) do update set fruit = excluded.fruit;
insert into insertconflicttest values (2, 'Orange') on conflict (key, key, key) do update set fruit = excluded.fruit;
-- Succeed, since multi-assignment does not involve subquery:
insert into insertconflicttest
values (1, 'Apple'), (2, 'Orange')
on conflict (key) do update set (fruit, key) = (excluded.fruit, excluded.key);
-- Give good diagnostic message when EXCLUDED.* spuriously referenced from
-- RETURNING:
insert into insertconflicttest values (1, 'Apple') on conflict (key) do update set fruit = excluded.fruit RETURNING excluded.fruit;
ERROR:  invalid reference to FROM-clause entry for table "excluded"
LINE 1: ...y) do update set fruit = excluded.fruit RETURNING excluded.f...
                                                             ^
DETAIL:  There is an entry for table "excluded", but it cannot be referenced from this part of the query.
-- Only suggest <table>.* column when inference element misspelled:
insert into insertconflicttest values (1, 'Apple') on conflict (keyy) do update set fruit = excluded.fruit;
ERROR:  column "keyy" does not exist
LINE 1: ...nsertconflicttest values (1, 'Apple') on conflict (keyy) do ...
                                                             ^
HINT:  Perhaps you meant to reference the column "insertconflicttest.key" or the column "excluded.key".
-- Have useful HINT for EXCLUDED.* RTE within UPDATE:
insert into insertconflicttest values (1, 'Apple') on conflict (key) do update set fruit = excluded.fruitt;
ERROR:  column excluded.fruitt does not exist
LINE 1: ... 'Apple') on conflict (key) do update set fruit = excluded.f...
                                                             ^
HINT:  Perhaps you meant to reference the column "excluded.fruit".
-- inference fails:
insert into insertconflicttest values (3, 'Kiwi') on conflict (key, fruit) do update set fruit = excluded.fruit;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
insert into insertconflicttest values (4, 'Mango') on conflict (fruit, key) do update set fruit = excluded.fruit;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
insert into insertconflicttest values (5, 'Lemon') on conflict (fruit) do update set fruit = excluded.fruit;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
insert into insertconflicttest values (6, 'Passionfruit') on conflict (lower(fruit)) do update set fruit = excluded.fruit;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
-- Check the target relation can be aliased
insert into insertconflicttest AS ict values (6, 'Passionfruit') on conflict (key) do update set fruit = excluded.fruit; -- ok, no reference to target table
insert into insertconflicttest AS ict values (6, 'Passionfruit') on conflict (key) do update set fruit = ict.fruit; -- ok, alias
insert into insertconflicttest AS ict values (6, 'Passionfruit') on conflict (key) do update set fruit = insertconflicttest.fruit; -- error, references aliased away name
ERROR:  invalid reference to FROM-clause entry for table "insertconflicttest"
LINE 1: ...onfruit') on conflict (key) do update set fruit = insertconf...
                                                             ^
HINT:  Perhaps you meant to reference the table alias "ict".
-- Check helpful hint when qualifying set column with target table
insert into insertconflicttest values (3, 'Kiwi') on conflict (key, fruit) do update set insertconflicttest.fruit = 'Mango';
ERROR:  column "insertconflicttest" of relation "insertconflicttest" does not exist
LINE 1: ...3, 'Kiwi') on conflict (key, fruit) do update set insertconf...
                                                             ^
HINT:  SET target columns cannot be qualified with the relation name.
--
-- DO SELECT tests
--
delete from insertconflicttest where fruit = 'Apple';
insert into insertconflicttest values (1, 'Apple') on conflict (key) do select; -- fails
ERROR:  ON CONFLICT DO SELECT requires a RETURNING clause
LINE 1: ...nsert into insertconflicttest values (1, 'Apple') on conflic...
                                                             ^
insert into insertconflicttest as i values (1, 'Apple') on conflict (key) do select returning old, new, i;
 old |    new    |     i     
-----+-----------+-----------
     | (1,Apple) | (1,Apple)
(1 row)
 
insert into insertconflicttest as i values (1, 'Orange') on conflict (key) do select returning old, new, i;
    old    |    new    |     i     
-----------+-----------+-----------
 (1,Apple) | (1,Apple) | (1,Apple)
(1 row)
 
insert into insertconflicttest as i values (1, 'Apple') on conflict (key) do select where i.fruit = 'Apple' returning *;
 key | fruit 
-----+-------
   1 | Apple
(1 row)
 
insert into insertconflicttest as i values (1, 'Apple') on conflict (key) do select where i.fruit = 'Orange' returning *;
 key | fruit 
-----+-------
(0 rows)
 
insert into insertconflicttest as i values (1, 'Orange') on conflict (key) do select where excluded.fruit = 'Apple' returning *;
 key | fruit 
-----+-------
(0 rows)
 
insert into insertconflicttest as i values (1, 'Orange') on conflict (key) do select where excluded.fruit = 'Orange' returning *;
 key | fruit 
-----+-------
   1 | Apple
(1 row)
 
drop index key_index;
--
-- Composite key tests
--
create unique index comp_key_index on insertconflicttest(key, fruit);
-- inference succeeds:
insert into insertconflicttest values (7, 'Raspberry') on conflict (key, fruit) do update set fruit = excluded.fruit;
insert into insertconflicttest values (8, 'Lime') on conflict (fruit, key) do update set fruit = excluded.fruit;
-- inference fails:
insert into insertconflicttest values (9, 'Banana') on conflict (key) do update set fruit = excluded.fruit;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
insert into insertconflicttest values (10, 'Blueberry') on conflict (key, key, key) do update set fruit = excluded.fruit;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
insert into insertconflicttest values (11, 'Cherry') on conflict (key, lower(fruit)) do update set fruit = excluded.fruit;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
insert into insertconflicttest values (12, 'Date') on conflict (lower(fruit), key) do update set fruit = excluded.fruit;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
drop index comp_key_index;
--
-- Partial index tests, no inference predicate specified
--
create unique index part_comp_key_index on insertconflicttest(key, fruit) where key < 5;
create unique index expr_part_comp_key_index on insertconflicttest(key, lower(fruit)) where key < 5;
-- inference fails:
insert into insertconflicttest values (13, 'Grape') on conflict (key, fruit) do update set fruit = excluded.fruit;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
insert into insertconflicttest values (14, 'Raisin') on conflict (fruit, key) do update set fruit = excluded.fruit;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
insert into insertconflicttest values (15, 'Cranberry') on conflict (key) do update set fruit = excluded.fruit;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
insert into insertconflicttest values (16, 'Melon') on conflict (key, key, key) do update set fruit = excluded.fruit;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
insert into insertconflicttest values (17, 'Mulberry') on conflict (key, lower(fruit)) do update set fruit = excluded.fruit;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
insert into insertconflicttest values (18, 'Pineapple') on conflict (lower(fruit), key) do update set fruit = excluded.fruit;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
drop index part_comp_key_index;
drop index expr_part_comp_key_index;
--
-- Expression index tests
--
create unique index expr_key_index on insertconflicttest(lower(fruit));
-- inference succeeds:
insert into insertconflicttest values (20, 'Quince') on conflict (lower(fruit)) do update set fruit = excluded.fruit;
insert into insertconflicttest values (21, 'Pomegranate') on conflict (lower(fruit), lower(fruit)) do update set fruit = excluded.fruit;
-- inference fails:
insert into insertconflicttest values (22, 'Apricot') on conflict (upper(fruit)) do update set fruit = excluded.fruit;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
insert into insertconflicttest values (23, 'Blackberry') on conflict (fruit) do update set fruit = excluded.fruit;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
drop index expr_key_index;
--
-- Expression index tests (with regular column)
--
create unique index expr_comp_key_index on insertconflicttest(key, lower(fruit));
create unique index tricky_expr_comp_key_index on insertconflicttest(key, lower(fruit), upper(fruit));
-- inference succeeds:
insert into insertconflicttest values (24, 'Plum') on conflict (key, lower(fruit)) do update set fruit = excluded.fruit;
insert into insertconflicttest values (25, 'Peach') on conflict (lower(fruit), key) do update set fruit = excluded.fruit;
-- Should not infer "tricky_expr_comp_key_index" index:
explain (costs off) insert into insertconflicttest values (26, 'Fig') on conflict (lower(fruit), key, lower(fruit), key) do update set fruit = excluded.fruit;
                   QUERY PLAN                    
-------------------------------------------------
 Insert on insertconflicttest
   Conflict Resolution: UPDATE
   Conflict Arbiter Indexes: expr_comp_key_index
   ->  Result
(4 rows)
 
-- inference fails:
insert into insertconflicttest values (27, 'Prune') on conflict (key, upper(fruit)) do update set fruit = excluded.fruit;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
insert into insertconflicttest values (28, 'Redcurrant') on conflict (fruit, key) do update set fruit = excluded.fruit;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
insert into insertconflicttest values (29, 'Nectarine') on conflict (key) do update set fruit = excluded.fruit;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
drop index expr_comp_key_index;
drop index tricky_expr_comp_key_index;
--
-- Non-spurious duplicate violation tests
--
create unique index key_index on insertconflicttest(key);
create unique index fruit_index on insertconflicttest(fruit);
-- succeeds, since UPDATE happens to update "fruit" to existing value:
insert into insertconflicttest values (26, 'Fig') on conflict (key) do update set fruit = excluded.fruit;
-- fails, since UPDATE is to row with key value 26, and we're updating "fruit"
-- to a value that happens to exist in another row ('peach'):
insert into insertconflicttest values (26, 'Peach') on conflict (key) do update set fruit = excluded.fruit;
ERROR:  duplicate key value violates unique constraint "fruit_index"
DETAIL:  Key (fruit)=(Peach) already exists.
-- succeeds, since "key" isn't repeated/referenced in UPDATE, and "fruit"
-- arbitrates that statement updates existing "Fig" row:
insert into insertconflicttest values (25, 'Fig') on conflict (fruit) do update set fruit = excluded.fruit;
drop index key_index;
drop index fruit_index;
--
-- Test partial unique index inference
--
create unique index partial_key_index on insertconflicttest(key) where fruit like '%berry';
-- Succeeds
insert into insertconflicttest values (23, 'Blackberry') on conflict (key) where fruit like '%berry' do update set fruit = excluded.fruit;
insert into insertconflicttest as t values (23, 'Blackberry') on conflict (key) where fruit like '%berry' and t.fruit = 'inconsequential' do nothing;
insert into insertconflictview as t values (23, 'Blackberry') on conflict (key) where fruit like '%berry' and t.fruit = 'inconsequential' do nothing;
-- fails
insert into insertconflicttest values (23, 'Blackberry') on conflict (key) do update set fruit = excluded.fruit;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
insert into insertconflicttest values (23, 'Blackberry') on conflict (key) where fruit like '%berry' or fruit = 'consequential' do nothing;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
insert into insertconflicttest values (23, 'Blackberry') on conflict (fruit) where fruit like '%berry' do update set fruit = excluded.fruit;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
drop index partial_key_index;
--
-- Test that wholerow references to ON CONFLICT's EXCLUDED work
--
create unique index plain on insertconflicttest(key);
-- Succeeds, updates existing row:
insert into insertconflicttest as i values (23, 'Jackfruit') on conflict (key) do update set fruit = excluded.fruit
  where i.* != excluded.* returning *;
 key |   fruit   
-----+-----------
  23 | Jackfruit
(1 row)
 
-- No update this time, though:
insert into insertconflicttest as i values (23, 'Jackfruit') on conflict (key) do update set fruit = excluded.fruit
  where i.* != excluded.* returning *;
 key | fruit 
-----+-------
(0 rows)
 
-- Predicate changed to require match rather than non-match, so updates once more:
insert into insertconflicttest as i values (23, 'Jackfruit') on conflict (key) do update set fruit = excluded.fruit
  where i.* = excluded.* returning *;
 key |   fruit   
-----+-----------
  23 | Jackfruit
(1 row)
 
-- Assign:
insert into insertconflicttest as i values (23, 'Avocado') on conflict (key) do update set fruit = excluded.*::text
  returning *;
 key |    fruit     
-----+--------------
  23 | (23,Avocado)
(1 row)
 
-- deparse whole row var in WHERE and SET clauses:
explain (costs off) insert into insertconflicttest as i values (23, 'Avocado') on conflict (key) do update set fruit = excluded.fruit where excluded.* is null;
               QUERY PLAN                
-----------------------------------------
 Insert on insertconflicttest i
   Conflict Resolution: UPDATE
   Conflict Arbiter Indexes: plain
   Conflict Filter: (excluded.* IS NULL)
   ->  Result
(5 rows)
 
explain (costs off) insert into insertconflicttest as i values (23, 'Avocado') on conflict (key) do update set fruit = excluded.*::text;
            QUERY PLAN             
-----------------------------------
 Insert on insertconflicttest i
   Conflict Resolution: UPDATE
   Conflict Arbiter Indexes: plain
   ->  Result
(4 rows)
 
drop index plain;
-- Cleanup
drop view insertconflictview;
drop table insertconflicttest;
--
-- Verify that EXCLUDED does not allow system column references. These
-- do not make sense because EXCLUDED isn't an already stored tuple
-- (and thus doesn't have a ctid etc).
--
create table syscolconflicttest(key int4, data text);
insert into syscolconflicttest values (1);
insert into syscolconflicttest values (1) on conflict (key) do update set data = excluded.ctid::text;
ERROR:  column excluded.ctid does not exist
LINE 1: ...values (1) on conflict (key) do update set data = excluded.c...
                                                             ^
drop table syscolconflicttest;
--
-- Previous tests all managed to not test any expressions requiring
-- planner preprocessing ...
--
create table insertconflict (a bigint, b bigint);
create unique index insertconflicti1 on insertconflict(coalesce(a, 0));
create unique index insertconflicti2 on insertconflict(b)
  where coalesce(a, 1) > 0;
insert into insertconflict values (1, 2)
on conflict (coalesce(a, 0)) do nothing;
insert into insertconflict values (1, 2)
on conflict (b) where coalesce(a, 1) > 0 do nothing;
insert into insertconflict values (1, 2)
on conflict (b) where coalesce(a, 1) > 1 do nothing;
drop table insertconflict;
--
-- test insertion through view
--
create table insertconflict (f1 int primary key, f2 text);
create view insertconflictv as
  select * from insertconflict with cascaded check option;
insert into insertconflictv values (1,'foo')
  on conflict (f1) do update set f2 = excluded.f2;
select * from insertconflict;
 f1 | f2  
----+-----
  1 | foo
(1 row)
 
insert into insertconflictv values (1,'bar')
  on conflict (f1) do update set f2 = excluded.f2;
select * from insertconflict;
 f1 | f2  
----+-----
  1 | bar
(1 row)
 
drop view insertconflictv;
drop table insertconflict;
-- ******************************************************************
-- *                                                                *
-- * Test inheritance (example taken from tutorial)                 *
-- *                                                                *
-- ******************************************************************
create table cities (
    name        text,
    population  float8,
    altitude    int     -- (in ft)
);
create table capitals (
    state       char(2)
) inherits (cities);
-- Create unique indexes.  Due to a general limitation of inheritance,
-- uniqueness is only enforced per-relation.  Unique index inference
-- specification will do the right thing, though.
create unique index cities_names_unique on cities (name);
create unique index capitals_names_unique on capitals (name);
-- prepopulate the tables.
insert into cities values ('San Francisco', 7.24E+5, 63);
insert into cities values ('Las Vegas', 2.583E+5, 2174);
insert into cities values ('Mariposa', 1200, 1953);
insert into capitals values ('Sacramento', 3.694E+5, 30, 'CA');
insert into capitals values ('Madison', 1.913E+5, 845, 'WI');
-- Tests proper for inheritance:
select * from capitals;
    name    | population | altitude | state 
------------+------------+----------+-------
 Sacramento |     369400 |       30 | CA
 Madison    |     191300 |      845 | WI
(2 rows)
 
-- Succeeds:
insert into cities values ('Las Vegas', 2.583E+5, 2174) on conflict do nothing;
insert into capitals values ('Sacramento', 4664.E+5, 30, 'CA') on conflict (name) do update set population = excluded.population;
-- Wrong "Sacramento", so do nothing:
insert into capitals values ('Sacramento', 50, 2267, 'NE') on conflict (name) do nothing;
select * from capitals;
    name    | population | altitude | state 
------------+------------+----------+-------
 Madison    |     191300 |      845 | WI
 Sacramento |  466400000 |       30 | CA
(2 rows)
 
insert into cities values ('Las Vegas', 5.83E+5, 2001) on conflict (name) do update set population = excluded.population, altitude = excluded.altitude;
select tableoid::regclass, * from cities;
 tableoid |     name      | population | altitude 
----------+---------------+------------+----------
 cities   | San Francisco |     724000 |       63
 cities   | Mariposa      |       1200 |     1953
 cities   | Las Vegas     |     583000 |     2001
 capitals | Madison       |     191300 |      845
 capitals | Sacramento    |  466400000 |       30
(5 rows)
 
insert into capitals values ('Las Vegas', 5.83E+5, 2222, 'NV') on conflict (name) do update set population = excluded.population;
-- Capitals will contain new capital, Las Vegas:
select * from capitals;
    name    | population | altitude | state 
------------+------------+----------+-------
 Madison    |     191300 |      845 | WI
 Sacramento |  466400000 |       30 | CA
 Las Vegas  |     583000 |     2222 | NV
(3 rows)
 
-- Cities contains two instances of "Las Vegas", since unique constraints don't
-- work across inheritance:
select tableoid::regclass, * from cities;
 tableoid |     name      | population | altitude 
----------+---------------+------------+----------
 cities   | San Francisco |     724000 |       63
 cities   | Mariposa      |       1200 |     1953
 cities   | Las Vegas     |     583000 |     2001
 capitals | Madison       |     191300 |      845
 capitals | Sacramento    |  466400000 |       30
 capitals | Las Vegas     |     583000 |     2222
(6 rows)
 
-- This only affects "cities" version of "Las Vegas":
insert into cities values ('Las Vegas', 5.86E+5, 2223) on conflict (name) do update set population = excluded.population, altitude = excluded.altitude;
select tableoid::regclass, * from cities;
 tableoid |     name      | population | altitude 
----------+---------------+------------+----------
 cities   | San Francisco |     724000 |       63
 cities   | Mariposa      |       1200 |     1953
 cities   | Las Vegas     |     586000 |     2223
 capitals | Madison       |     191300 |      845
 capitals | Sacramento    |  466400000 |       30
 capitals | Las Vegas     |     583000 |     2222
(6 rows)
 
-- clean up
drop table capitals;
drop table cities;
-- Make sure a table named excluded is handled properly
create table excluded(key int primary key, data text);
insert into excluded values(1, '1');
-- error, ambiguous
insert into excluded values(1, '2') on conflict (key) do update set data = excluded.data RETURNING *;
ERROR:  table reference "excluded" is ambiguous
LINE 1: ...es(1, '2') on conflict (key) do update set data = excluded.d...
                                                             ^
-- ok, aliased
insert into excluded AS target values(1, '2') on conflict (key) do update set data = excluded.data RETURNING *;
 key | data 
-----+------
   1 | 2
(1 row)
 
-- ok, aliased
insert into excluded AS target values(1, '2') on conflict (key) do update set data = target.data RETURNING *;
 key | data 
-----+------
   1 | 2
(1 row)
 
-- make sure excluded isn't a problem in returning clause
insert into excluded values(1, '2') on conflict (key) do update set data = 3 RETURNING excluded.*;
 key | data 
-----+------
   1 | 3
(1 row)
 
-- clean up
drop table excluded;
-- check that references to columns after dropped columns are handled correctly
create table dropcol(key int primary key, drop1 int, keep1 text, drop2 numeric, keep2 float);
insert into dropcol(key, drop1, keep1, drop2, keep2) values(1, 1, '1', '1', 1);
-- set using excluded
insert into dropcol(key, drop1, keep1, drop2, keep2) values(1, 2, '2', '2', 2) on conflict(key)
    do update set drop1 = excluded.drop1, keep1 = excluded.keep1, drop2 = excluded.drop2, keep2 = excluded.keep2
    where excluded.drop1 is not null and excluded.keep1 is not null and excluded.drop2 is not null and excluded.keep2 is not null
          and dropcol.drop1 is not null and dropcol.keep1 is not null and dropcol.drop2 is not null and dropcol.keep2 is not null
    returning *;
 key | drop1 | keep1 | drop2 | keep2 
-----+-------+-------+-------+-------
   1 |     2 | 2     |     2 |     2
(1 row)
 
;
-- set using existing table
insert into dropcol(key, drop1, keep1, drop2, keep2) values(1, 3, '3', '3', 3) on conflict(key)
    do update set drop1 = dropcol.drop1, keep1 = dropcol.keep1, drop2 = dropcol.drop2, keep2 = dropcol.keep2
    returning *;
 key | drop1 | keep1 | drop2 | keep2 
-----+-------+-------+-------+-------
   1 |     2 | 2     |     2 |     2
(1 row)
 
;
alter table dropcol drop column drop1, drop column drop2;
-- set using excluded
insert into dropcol(key, keep1, keep2) values(1, '4', 4) on conflict(key)
    do update set keep1 = excluded.keep1, keep2 = excluded.keep2
    where excluded.keep1 is not null and excluded.keep2 is not null
          and dropcol.keep1 is not null and dropcol.keep2 is not null
    returning *;
 key | keep1 | keep2 
-----+-------+-------
   1 | 4     |     4
(1 row)
 
;
-- set using existing table
insert into dropcol(key, keep1, keep2) values(1, '5', 5) on conflict(key)
    do update set keep1 = dropcol.keep1, keep2 = dropcol.keep2
    returning *;
 key | keep1 | keep2 
-----+-------+-------
   1 | 4     |     4
(1 row)
 
;
DROP TABLE dropcol;
-- check handling of regular btree constraint along with gist constraint
create table twoconstraints (f1 int unique, f2 box,
                             exclude using gist(f2 with &&));
insert into twoconstraints values(1, '((0,0),(1,1))');
insert into twoconstraints values(1, '((2,2),(3,3))');  -- fail on f1
ERROR:  duplicate key value violates unique constraint "twoconstraints_f1_key"
DETAIL:  Key (f1)=(1) already exists.
insert into twoconstraints values(2, '((0,0),(1,2))');  -- fail on f2
ERROR:  conflicting key value violates exclusion constraint "twoconstraints_f2_excl"
DETAIL:  Key (f2)=((1,2),(0,0)) conflicts with existing key (f2)=((1,1),(0,0)).
insert into twoconstraints values(2, '((0,0),(1,2))')
  on conflict on constraint twoconstraints_f1_key do nothing;  -- fail on f2
ERROR:  conflicting key value violates exclusion constraint "twoconstraints_f2_excl"
DETAIL:  Key (f2)=((1,2),(0,0)) conflicts with existing key (f2)=((1,1),(0,0)).
insert into twoconstraints values(2, '((0,0),(1,2))')
  on conflict on constraint twoconstraints_f2_excl do nothing;  -- do nothing
select * from twoconstraints;
 f1 |     f2      
----+-------------
  1 | (1,1),(0,0)
(1 row)
 
drop table twoconstraints;
-- check handling of self-conflicts at various isolation levels
create table selfconflict (f1 int primary key, f2 int);
begin transaction isolation level read committed;
insert into selfconflict values (1,1), (1,2) on conflict do nothing;
commit;
begin transaction isolation level repeatable read;
insert into selfconflict values (2,1), (2,2) on conflict do nothing;
commit;
begin transaction isolation level serializable;
insert into selfconflict values (3,1), (3,2) on conflict do nothing;
commit;
begin transaction isolation level read committed;
insert into selfconflict values (4,1), (4,2) on conflict(f1) do update set f2 = 0;
ERROR:  ON CONFLICT DO UPDATE command cannot affect row a second time
HINT:  Ensure that no rows proposed for insertion within the same command have duplicate constrained values.
commit;
begin transaction isolation level repeatable read;
insert into selfconflict values (5,1), (5,2) on conflict(f1) do update set f2 = 0;
ERROR:  ON CONFLICT DO UPDATE command cannot affect row a second time
HINT:  Ensure that no rows proposed for insertion within the same command have duplicate constrained values.
commit;
begin transaction isolation level serializable;
insert into selfconflict values (6,1), (6,2) on conflict(f1) do update set f2 = 0;
ERROR:  ON CONFLICT DO UPDATE command cannot affect row a second time
HINT:  Ensure that no rows proposed for insertion within the same command have duplicate constrained values.
commit;
begin transaction isolation level read committed;
insert into selfconflict values (7,1), (7,2) on conflict(f1) do select returning *;
 f1 | f2 
----+----
  7 |  1
  7 |  1
(2 rows)
 
commit;
begin transaction isolation level repeatable read;
insert into selfconflict values (8,1), (8,2) on conflict(f1) do select returning *;
 f1 | f2 
----+----
  8 |  1
  8 |  1
(2 rows)
 
commit;
begin transaction isolation level serializable;
insert into selfconflict values (9,1), (9,2) on conflict(f1) do select returning *;
 f1 | f2 
----+----
  9 |  1
  9 |  1
(2 rows)
 
commit;
begin transaction isolation level read committed;
insert into selfconflict values (10,1), (10,2) on conflict(f1) do select for update returning *;
ERROR:  ON CONFLICT DO SELECT command cannot affect row a second time
HINT:  Ensure that no rows proposed for insertion within the same command have duplicate constrained values.
commit;
begin transaction isolation level repeatable read;
insert into selfconflict values (11,1), (11,2) on conflict(f1) do select for update returning *;
ERROR:  ON CONFLICT DO SELECT command cannot affect row a second time
HINT:  Ensure that no rows proposed for insertion within the same command have duplicate constrained values.
commit;
begin transaction isolation level serializable;
insert into selfconflict values (12,1), (12,2) on conflict(f1) do select for update returning *;
ERROR:  ON CONFLICT DO SELECT command cannot affect row a second time
HINT:  Ensure that no rows proposed for insertion within the same command have duplicate constrained values.
commit;
select * from selfconflict;
 f1 | f2 
----+----
  1 |  1
  2 |  1
  3 |  1
  7 |  1
  8 |  1
  9 |  1
(6 rows)
 
drop table selfconflict;
-- check ON CONFLICT handling with partitioned tables
create table parted_conflict_test (a int unique, b char) partition by list (a);
create table parted_conflict_test_1 partition of parted_conflict_test (b unique) for values in (1, 2);
-- no indexes required here
insert into parted_conflict_test values (1, 'a') on conflict do nothing;
-- index on a required, which does exist in parent
insert into parted_conflict_test values (1, 'a') on conflict (a) do nothing;
insert into parted_conflict_test values (1, 'a') on conflict (a) do update set b = excluded.b;
insert into parted_conflict_test values (1, 'a') on conflict (a) do select returning *;
 a | b 
---+---
 1 | a
(1 row)
 
insert into parted_conflict_test values (1, 'a') on conflict (a) do select for update returning *;
 a | b 
---+---
 1 | a
(1 row)
 
-- targeting partition directly will work
insert into parted_conflict_test_1 values (1, 'a') on conflict (a) do nothing;
insert into parted_conflict_test_1 values (1, 'b') on conflict (a) do update set b = excluded.b;
insert into parted_conflict_test_1 values (1, 'b') on conflict (a) do select returning b;
 b 
---
 b
(1 row)
 
-- index on b required, which doesn't exist in parent
insert into parted_conflict_test values (2, 'b') on conflict (b) do update set a = excluded.a; -- fail
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
insert into parted_conflict_test values (2, 'b') on conflict (b) do select returning b; -- fail
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
-- targeting partition directly will work
insert into parted_conflict_test_1 values (2, 'b') on conflict (b) do update set a = excluded.a;
-- should see (2, 'b')
select * from parted_conflict_test order by a;
 a | b 
---+---
 2 | b
(1 row)
 
-- now check that DO UPDATE and DO SELECT work correctly for target partition
-- with different attribute numbers
create table parted_conflict_test_2 (b char, a int unique);
alter table parted_conflict_test attach partition parted_conflict_test_2 for values in (3);
truncate parted_conflict_test;
insert into parted_conflict_test values (3, 'a') on conflict (a) do update set b = excluded.b;
insert into parted_conflict_test values (3, 'b') on conflict (a) do update set b = excluded.b;
insert into parted_conflict_test values (3, 'a') on conflict (a) do select returning b;
 b 
---
 b
(1 row)
 
insert into parted_conflict_test values (3, 'a') on conflict (a) do select where excluded.b = 'a' returning parted_conflict_test;
 parted_conflict_test 
----------------------
 (3,b)
(1 row)
 
insert into parted_conflict_test values (3, 'a') on conflict (a) do select where parted_conflict_test.b = 'b' returning b;
 b 
---
 b
(1 row)
 
-- should see (3, 'b')
select * from parted_conflict_test order by a;
 a | b 
---+---
 3 | b
(1 row)
 
-- case where parent will have a dropped column, but the partition won't
alter table parted_conflict_test drop b, add b char;
create table parted_conflict_test_3 partition of parted_conflict_test for values in (4);
truncate parted_conflict_test;
insert into parted_conflict_test (a, b) values (4, 'a') on conflict (a) do update set b = excluded.b;
insert into parted_conflict_test (a, b) values (4, 'b') on conflict (a) do update set b = excluded.b where parted_conflict_test.b = 'a';
insert into parted_conflict_test (a, b) values (4, 'b') on conflict (a) do select returning b;
 b 
---
 b
(1 row)
 
-- should see (4, 'b')
select * from parted_conflict_test order by a;
 a | b 
---+---
 4 | b
(1 row)
 
-- case with multi-level partitioning
create table parted_conflict_test_4 partition of parted_conflict_test for values in (5) partition by list (a);
create table parted_conflict_test_4_1 partition of parted_conflict_test_4 for values in (5);
truncate parted_conflict_test;
insert into parted_conflict_test (a, b) values (5, 'a') on conflict (a) do update set b = excluded.b;
insert into parted_conflict_test (a, b) values (5, 'b') on conflict (a) do update set b = excluded.b where parted_conflict_test.b = 'a';
insert into parted_conflict_test (a, b) values (5, 'b') on conflict (a) do select where parted_conflict_test.b = 'a' returning b;
 b 
---
(0 rows)
 
-- should see (5, 'b')
select * from parted_conflict_test order by a;
 a | b 
---+---
 5 | b
(1 row)
 
-- test with multiple rows
truncate parted_conflict_test;
insert into parted_conflict_test (a, b) values (1, 'a'), (2, 'a'), (4, 'a') on conflict (a) do update set b = excluded.b where excluded.b = 'b';
insert into parted_conflict_test (a, b) values (1, 'b'), (2, 'c'), (4, 'b') on conflict (a) do update set b = excluded.b where excluded.b = 'b';
-- should see (1, 'b'), (2, 'a'), (4, 'b')
select * from parted_conflict_test order by a;
 a | b 
---+---
 1 | b
 2 | a
 4 | b
(3 rows)
 
-- test DO SELECT with multiple rows hitting different partitions
truncate parted_conflict_test;
insert into parted_conflict_test (a, b) values (1, 'a'), (2, 'b'), (4, 'c');
insert into parted_conflict_test (a, b) values (1, 'x'), (2, 'y'), (4, 'z')
  on conflict (a) do select returning *, tableoid::regclass;
 a | b |        tableoid        
---+---+------------------------
 1 | a | parted_conflict_test_1
 2 | b | parted_conflict_test_1
 4 | c | parted_conflict_test_3
(3 rows)
 
-- should see original values (1, 'a'), (2, 'b'), (4, 'c')
select * from parted_conflict_test order by a;
 a | b 
---+---
 1 | a
 2 | b
 4 | c
(3 rows)
 
-- test DO SELECT with WHERE filtering across partitions
insert into parted_conflict_test (a, b) values (1, 'n') on conflict (a) do select where parted_conflict_test.b = 'a' returning *;
 a | b 
---+---
 1 | a
(1 row)
 
insert into parted_conflict_test (a, b) values (2, 'n') on conflict (a) do select where parted_conflict_test.b = 'x' returning *;
 a | b 
---+---
(0 rows)
 
-- test DO SELECT with EXCLUDED in WHERE across partitions with different layouts
insert into parted_conflict_test (a, b) values (3, 't') on conflict (a) do select where excluded.b = 't' returning *;
 a | b 
---+---
 3 | t
(1 row)
 
-- test DO SELECT FOR UPDATE across different partition layouts
insert into parted_conflict_test (a, b) values (1, 'l') on conflict (a) do select for update returning *;
 a | b 
---+---
 1 | a
(1 row)
 
insert into parted_conflict_test (a, b) values (3, 'l') on conflict (a) do select for update returning *;
 a | b 
---+---
 3 | t
(1 row)
 
drop table parted_conflict_test;
-- test behavior of inserting a conflicting tuple into an intermediate
-- partitioning level
create table parted_conflict (a int primary key, b text) partition by range (a);
create table parted_conflict_1 partition of parted_conflict for values from (0) to (1000) partition by range (a);
create table parted_conflict_1_1 partition of parted_conflict_1 for values from (0) to (500);
insert into parted_conflict values (40, 'forty');
insert into parted_conflict_1 values (40, 'cuarenta')
  on conflict (a) do update set b = excluded.b;
drop table parted_conflict;
-- same thing, but this time try to use an index that's created not in the
-- partition
create table parted_conflict (a int, b text) partition by range (a);
create table parted_conflict_1 partition of parted_conflict for values from (0) to (1000) partition by range (a);
create table parted_conflict_1_1 partition of parted_conflict_1 for values from (0) to (500);
create unique index on only parted_conflict_1 (a);
create unique index on only parted_conflict (a);
alter index parted_conflict_a_idx attach partition parted_conflict_1_a_idx;
insert into parted_conflict values (40, 'forty');
insert into parted_conflict_1 values (40, 'cuarenta')
  on conflict (a) do update set b = excluded.b;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
drop table parted_conflict;
-- a partition-local deferrable unique constraint on the arbiter columns
-- must not be picked up as an additional arbiter for routed inserts
create table parted_conflict (a int, b text, primary key (a)) partition by range (a);
create table parted_conflict_1 partition of parted_conflict for values from (0) to (1000);
alter table parted_conflict_1 add constraint parted_conflict_1_a_def unique (a) deferrable;
insert into parted_conflict values (40, 'forty');
insert into parted_conflict values (40, 'cuarenta')
  on conflict (a) do update set b = excluded.b;
insert into parted_conflict values (41, 'forty-one')
  on conflict (a) do update set b = excluded.b;
insert into parted_conflict values (42, 'forty-two') on conflict (a) do nothing;
select * from parted_conflict order by a;
 a  |     b     
----+-----------
 40 | cuarenta
 41 | forty-one
 42 | forty-two
(3 rows)
 
drop table parted_conflict;
-- ON CONFLICT ON CONSTRAINT has the same hazard: an index that merely covers
-- the same columns as the named constraint's index, but is deferrable or
-- disagrees with it about duplicates, must not join the arbiter set
create table namedconstraint (a int, b text, c text);
alter table namedconstraint add constraint namedconstraint_a_key unique (a);
alter table namedconstraint add constraint namedconstraint_a_def unique (a) deferrable;
create unique index namedconstraint_b_nnd on namedconstraint (b) nulls not distinct;
alter table namedconstraint add constraint namedconstraint_b_key unique (b);
insert into namedconstraint values (1, 'x', 'first');
insert into namedconstraint values (2, null, 'first');
insert into namedconstraint values (1, 'y', 'second')
  on conflict on constraint namedconstraint_a_key do update set c = excluded.c;
insert into namedconstraint values (3, null, 'second')
  on conflict on constraint namedconstraint_b_key do update set c = excluded.c;  -- error
ERROR:  duplicate key value violates unique constraint "namedconstraint_b_nnd"
DETAIL:  Key (b)=(null) already exists.
insert into namedconstraint values (4, 'z', 'third')
  on conflict on constraint namedconstraint_a_key do nothing;
insert into namedconstraint values (5, null, 'third')
  on conflict on constraint namedconstraint_b_key do nothing;  -- error
ERROR:  duplicate key value violates unique constraint "namedconstraint_b_nnd"
DETAIL:  Key (b)=(null) already exists.
select * from namedconstraint order by a;
 a | b |   c    
---+---+--------
 1 | x | second
 2 |   | first
 4 | z | third
(3 rows)
 
drop table namedconstraint;
-- same, with the indexes created in the opposite order
create table namedconstraint2 (a int, b text, c text);
alter table namedconstraint2 add constraint namedconstraint2_a_def unique (a) deferrable;
alter table namedconstraint2 add constraint namedconstraint2_a_key unique (a);
alter table namedconstraint2 add constraint namedconstraint2_b_key unique (b);
create unique index namedconstraint2_b_nnd on namedconstraint2 (b) nulls not distinct;
insert into namedconstraint2 values (1, 'x', 'first');
insert into namedconstraint2 values (2, null, 'first');
insert into namedconstraint2 values (1, 'y', 'second')
  on conflict on constraint namedconstraint2_a_key do update set c = excluded.c;
insert into namedconstraint2 values (3, 'z', 'second')
  on conflict on constraint namedconstraint2_a_key do nothing;
insert into namedconstraint2 values (4, null, 'second')
  on conflict on constraint namedconstraint2_b_key do nothing;  -- error
ERROR:  duplicate key value violates unique constraint "namedconstraint2_b_nnd"
DETAIL:  Key (b)=(null) already exists.
-- ON CONFLICT DO SELECT reaches the same code
insert into namedconstraint2 values (1, 'w', 'third')
  on conflict on constraint namedconstraint2_a_key do select returning *;
 a | b |   c    
---+---+--------
 1 | x | second
(1 row)
 
insert into namedconstraint2 values (5, 'v', 'third')
  on conflict on constraint namedconstraint2_a_key do select returning *;
 a | b |   c   
---+---+-------
 5 | v | third
(1 row)
 
insert into namedconstraint2 values (6, null, 'fourth')
  on conflict on constraint namedconstraint2_b_key do select returning *;  -- error
ERROR:  duplicate key value violates unique constraint "namedconstraint2_b_nnd"
DETAIL:  Key (b)=(null) already exists.
select * from namedconstraint2 order by a;
 a | b |   c    
---+---+--------
 1 | x | second
 2 |   | first
 3 | z | second
 5 | v | third
(4 rows)
 
drop table namedconstraint2;
-- test whole-row Vars in ON CONFLICT expressions
create table parted_conflict (a int, b text, c int) partition by range (a);
create table parted_conflict_1 (drp text, c int, a int, b text);
alter table parted_conflict_1 drop column drp;
create unique index on parted_conflict (a, b);
alter table parted_conflict attach partition parted_conflict_1 for values from (0) to (1000);
truncate parted_conflict;
insert into parted_conflict values (50, 'cincuenta', 1);
insert into parted_conflict values (50, 'cincuenta', 2)
  on conflict (a, b) do update set (a, b, c) = row(excluded.*)
  where parted_conflict = (50, text 'cincuenta', 1) and
        excluded = (50, text 'cincuenta', 2);
-- should see (50, 'cincuenta', 2)
select * from parted_conflict order by a;
 a  |     b     | c 
----+-----------+---
 50 | cincuenta | 2
(1 row)
 
-- test with statement level triggers
create or replace function parted_conflict_update_func() returns trigger as $$
declare
    r record;
begin
 for r in select * from inserted loop
    raise notice 'a = %, b = %, c = %', r.a, r.b, r.c;
 end loop;
 return new;
end;
$$ language plpgsql;
create trigger parted_conflict_update
    after update on parted_conflict
    referencing new table as inserted
    for each statement
    execute procedure parted_conflict_update_func();
truncate parted_conflict;
insert into parted_conflict values (0, 'cero', 1);
insert into parted_conflict values(0, 'cero', 1)
  on conflict (a,b) do update set c = parted_conflict.c + 1;
NOTICE:  a = 0, b = cero, c = 2
drop table parted_conflict;
drop function parted_conflict_update_func();