Debian Codegraph @

src/test/isolation/specs/merge-match-recheck.spec

raw
1 # MERGE MATCHED recheck
2 #
3 # This test looks at what happens when we have complex
4 # WHEN MATCHED AND conditions and a concurrent UPDATE causes a
5 # recheck of the AND condition on the new row
6
7 setup
8 {
9 CREATE TABLE target (key int primary key, balance integer, status text, val text);
10 INSERT INTO target VALUES (1, 160, 's1', 'setup');
11
12 CREATE TABLE target_pa (key int, balance integer, status text, val text) PARTITION BY RANGE (balance);
13 CREATE TABLE target_pa1 PARTITION OF target_pa FOR VALUES FROM (-100) TO (200);
14 CREATE TABLE target_pa2 PARTITION OF target_pa FOR VALUES FROM (200) TO (1000);
15 INSERT INTO target_pa VALUES (1, 160, 's1', 'setup');
16
17 CREATE TABLE target_tg (key int primary key, balance integer, status text, val text);
18 CREATE FUNCTION target_tg_trig_fn() RETURNS trigger LANGUAGE plpgsql AS
19 $$
20 BEGIN
21 IF tg_op = 'INSERT' THEN
22 RAISE NOTICE 'Insert: %', NEW;
23 RETURN NEW;
24 ELSIF tg_op = 'UPDATE' THEN
25 RAISE NOTICE 'Update: % -> %', OLD, NEW;
26 RETURN NEW;
27 ELSE
28 RAISE NOTICE 'Delete: %', OLD;
29 RETURN OLD;
30 END IF;
31 END
32 $$;
33 CREATE TRIGGER target_tg_trig BEFORE INSERT OR UPDATE OR DELETE ON target_tg
34 FOR EACH ROW EXECUTE FUNCTION target_tg_trig_fn();
35 INSERT INTO target_tg VALUES (1, 160, 's1', 'setup');
36 }
37
38 teardown
39 {
40 DROP TABLE target;
41 DROP TABLE target_pa;
42 DROP TABLE target_tg;
43 DROP FUNCTION target_tg_trig_fn;
44 }
45
46 session "s1"
47 setup
48 {
49 BEGIN ISOLATION LEVEL READ COMMITTED;
50 }
51 step "merge_status"
52 {
53 MERGE INTO target t
54 USING (SELECT 1 as key) s
55 ON s.key = t.key
56 WHEN MATCHED AND status = 's1' THEN
57 UPDATE SET status = 's2', val = t.val || ' when1'
58 WHEN MATCHED AND status = 's2' THEN
59 UPDATE SET status = 's3', val = t.val || ' when2'
60 WHEN MATCHED AND status = 's3' THEN
61 UPDATE SET status = 's4', val = t.val || ' when3';
62 }
63 step "merge_status_tg"
64 {
65 MERGE INTO target_tg t
66 USING (SELECT 1 as key) s
67 ON s.key = t.key
68 WHEN MATCHED AND status = 's1' THEN
69 UPDATE SET status = 's2', val = t.val || ' when1'
70 WHEN MATCHED AND status = 's2' THEN
71 UPDATE SET status = 's3', val = t.val || ' when2'
72 WHEN MATCHED AND status = 's3' THEN
73 UPDATE SET status = 's4', val = t.val || ' when3';
74 }
75
76 step "merge_bal"
77 {
78 MERGE INTO target t
79 USING (SELECT 1 as key) s
80 ON s.key = t.key
81 WHEN MATCHED AND balance < 0 THEN
82 DELETE
83 WHEN MATCHED AND balance < 100 THEN
84 UPDATE SET balance = balance * 2, val = t.val || ' when1'
85 WHEN MATCHED AND balance < 200 THEN
86 UPDATE SET balance = balance * 4, val = t.val || ' when2'
87 WHEN MATCHED AND balance < 300 THEN
88 UPDATE SET balance = balance * 8, val = t.val || ' when3'
89 RETURNING t.key, old.balance AS old_balance, new.balance AS new_balance, t.status, t.val;
90 }
91 step "merge_bal_pa"
92 {
93 MERGE INTO target_pa t
94 USING (SELECT 1 as key) s
95 ON s.key = t.key
96 WHEN MATCHED AND balance < 0 THEN
97 DELETE
98 WHEN MATCHED AND balance < 100 THEN
99 UPDATE SET balance = balance * 2, val = t.val || ' when1'
100 WHEN MATCHED AND balance < 200 THEN
101 UPDATE SET balance = balance * 4, val = t.val || ' when2'
102 WHEN MATCHED AND balance < 300 THEN
103 UPDATE SET balance = balance * 8, val = t.val || ' when3'
104 RETURNING t.key, old.balance AS old_balance, new.balance AS new_balance, t.status, t.val;
105 }
106 step "merge_bal_tg"
107 {
108 WITH t AS (
109 MERGE INTO target_tg t
110 USING (SELECT 1 as key) s
111 ON s.key = t.key
112 WHEN MATCHED AND balance < 0 THEN
113 DELETE
114 WHEN MATCHED AND balance < 100 THEN
115 UPDATE SET balance = balance * 2, val = t.val || ' when1'
116 WHEN MATCHED AND balance < 200 THEN
117 UPDATE SET balance = balance * 4, val = t.val || ' when2'
118 WHEN MATCHED AND balance < 300 THEN
119 UPDATE SET balance = balance * 8, val = t.val || ' when3'
120 RETURNING t.key, old.balance AS old_balance, new.balance AS new_balance, t.status, t.val
121 )
122 SELECT * FROM t;
123 }
124
125 step "merge_delete"
126 {
127 MERGE INTO target t
128 USING (SELECT 1 as key) s
129 ON s.key = t.key
130 WHEN MATCHED AND balance < 100 THEN
131 UPDATE SET balance = balance * 2, val = t.val || ' when1'
132 WHEN MATCHED AND balance < 200 THEN
133 DELETE;
134 }
135 step "merge_delete_tg"
136 {
137 MERGE INTO target_tg t
138 USING (SELECT 1 as key) s
139 ON s.key = t.key
140 WHEN MATCHED AND balance < 100 THEN
141 UPDATE SET balance = balance * 2, val = t.val || ' when1'
142 WHEN MATCHED AND balance < 200 THEN
143 DELETE;
144 }
145
146 step "select1" { SELECT * FROM target; }
147 step "select1_pa" { SELECT * FROM target_pa; }
148 step "select1_tg" { SELECT * FROM target_tg; }
149 step "c1" { COMMIT; }
150
151 session "s2"
152 setup
153 {
154 BEGIN ISOLATION LEVEL READ COMMITTED;
155 }
156 step "update1" { UPDATE target t SET balance = balance + 10, val = t.val || ' updated by update1' WHERE t.key = 1; }
157 step "update1_pa" { UPDATE target_pa t SET balance = balance + 10, val = t.val || ' updated by update1_pa' WHERE t.key = 1; }
158 step "update1_pa_move" { UPDATE target_pa t SET balance = 210, val = t.val || ' updated by update1_pa_move' WHERE t.key = 1; }
159 step "update1_tg" { UPDATE target_tg t SET balance = balance + 10, val = t.val || ' updated by update1_tg' WHERE t.key = 1; }
160 step "update2" { UPDATE target t SET status = 's2', val = t.val || ' updated by update2' WHERE t.key = 1; }
161 step "update2_tg" { UPDATE target_tg t SET status = 's2', val = t.val || ' updated by update2_tg' WHERE t.key = 1; }
162 step "update3" { UPDATE target t SET status = 's3', val = t.val || ' updated by update3' WHERE t.key = 1; }
163 step "update3_tg" { UPDATE target_tg t SET status = 's3', val = t.val || ' updated by update3_tg' WHERE t.key = 1; }
164 step "update5" { UPDATE target t SET status = 's5', val = t.val || ' updated by update5' WHERE t.key = 1; }
165 step "update5_tg" { UPDATE target_tg t SET status = 's5', val = t.val || ' updated by update5_tg' WHERE t.key = 1; }
166 step "update6" { UPDATE target t SET balance = balance - 100, val = t.val || ' updated by update6' WHERE t.key = 1; }
167 step "update6_pa" { UPDATE target_pa t SET balance = balance - 100, val = t.val || ' updated by update6_pa' WHERE t.key = 1; }
168 step "update6_tg" { UPDATE target_tg t SET balance = balance - 100, val = t.val || ' updated by update6_tg' WHERE t.key = 1; }
169 step "update7" { UPDATE target t SET balance = 350, val = t.val || ' updated by update7' WHERE t.key = 1; }
170 step "update_bal1" { UPDATE target t SET balance = 50, val = t.val || ' updated by update_bal1' WHERE t.key = 1; }
171 step "update_bal1_pa" { UPDATE target_pa t SET balance = 50, val = t.val || ' updated by update_bal1_pa' WHERE t.key = 1; }
172 step "update_bal1_tg" { UPDATE target_tg t SET balance = 50, val = t.val || ' updated by update_bal1_tg' WHERE t.key = 1; }
173 step "c2" { COMMIT; }
174
175 # merge_status sees concurrently updated row and rechecks WHEN conditions, but recheck passes and final status = 's2'
176 permutation "update1" "merge_status" "c2" "select1" "c1"
177 permutation "update1_tg" "merge_status_tg" "c2" "select1_tg" "c1"
178
179 # merge_status sees concurrently updated row and rechecks WHEN conditions, recheck fails, so final status = 's3' not 's2'
180 permutation "update2" "merge_status" "c2" "select1" "c1"
181 permutation "update2_tg" "merge_status_tg" "c2" "select1_tg" "c1"
182
183 # merge_status sees concurrently updated row and rechecks WHEN conditions, recheck fails, so final status = 's4' not 's2'
184 permutation "update3" "merge_status" "c2" "select1" "c1"
185 permutation "update3_tg" "merge_status_tg" "c2" "select1_tg" "c1"
186
187 # merge_status sees concurrently updated row and rechecks WHEN conditions, recheck fails, but we skip update and MERGE does nothing
188 permutation "update5" "merge_status" "c2" "select1" "c1"
189 permutation "update5_tg" "merge_status_tg" "c2" "select1_tg" "c1"
190
191 # merge_bal sees concurrently updated row and rechecks WHEN conditions, recheck fails, so final balance = 100 not 640
192 permutation "update_bal1" "merge_bal" "c2" "select1" "c1"
193 permutation "update_bal1_pa" "merge_bal_pa" "c2" "select1_pa" "c1"
194 permutation "update_bal1_tg" "merge_bal_tg" "c2" "select1_tg" "c1"
195
196 # merge_bal sees row concurrently updated twice and rechecks WHEN conditions, different check passes, so final balance = 140
197 permutation "update1" "update6" "merge_bal" "c2" "select1" "c1"
198 permutation "update1_pa" "update6_pa" "merge_bal_pa" "c2" "select1_pa" "c1"
199 permutation "update1_tg" "update6_tg" "merge_bal_tg" "c2" "select1_tg" "c1"
200
201 # merge_bal sees row concurrently updated twice and rechecks WHEN conditions, different check passes, and row is deleted
202 permutation "update6" "update6" "merge_bal" "c2" "select1" "c1"
203 permutation "update6_pa" "update6_pa" "merge_bal_pa" "c2" "select1_pa" "c1"
204 permutation "update6_tg" "update6_tg" "merge_bal_tg" "c2" "select1_tg" "c1"
205
206 # merge_bal sees row concurrently updated twice, first update would cause all checks to fail, second update causes different check to pass, so final balance = 2000
207 permutation "update7" "update6" "merge_bal" "c2" "select1" "c1"
208
209 # merge_bal sees concurrently updated row moved to new partition, so fails
210 permutation "update1_pa_move" "merge_bal_pa" "c2" "c1"
211 permutation "update1_pa" "update1_pa_move" "merge_bal_pa" "c2" "c1"
212
213 # merge_delete sees concurrently updated row and rechecks WHEN conditions, but recheck passes and row is deleted
214 permutation "update1" "merge_delete" "c2" "select1" "c1"
215 permutation "update1_tg" "merge_delete_tg" "c2" "select1_tg" "c1"
216
217 # merge_delete sees concurrently updated row and rechecks WHEN conditions, recheck fails, so final balance is 100
218 permutation "update_bal1" "merge_delete" "c2" "select1" "c1"
219 permutation "update_bal1_tg" "merge_delete_tg" "c2" "select1_tg" "c1"