summaryrefslogtreecommitdiffstats
path: root/src/test/isolation/specs/eval-plan-qual-trigger.spec
blob: b512edd28798a6f3a49a34f4ad3633fd74a7c22c (plain)
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
setup
{
    CREATE TABLE trigtest(key text primary key, data text);

    CREATE FUNCTION noisy_oper(p_comment text, p_a anynonarray, p_op text, p_b anynonarray)
    RETURNS bool LANGUAGE plpgsql AS $body$
        DECLARE
            r bool;
        BEGIN
            EXECUTE format('SELECT $1 %s $2', p_op) INTO r USING p_a, p_b;
            RAISE NOTICE '%: % % % % %: %', p_comment, pg_typeof(p_a), p_a, p_op, pg_typeof(p_b), p_b, r;
        RETURN r;
    END;$body$;

    CREATE FUNCTION trig_report() RETURNS TRIGGER LANGUAGE plpgsql AS $body$
    DECLARE
	r_new text;
	r_old text;
	r_ret record;
    BEGIN
	-- In older releases it wasn't allowed to reference OLD/NEW
        -- when not applicable for TG_WHEN
	IF TG_OP = 'INSERT' THEN
	    r_old = NULL;
	    r_new = NEW;
	    r_ret = NEW;
	ELSIF TG_OP = 'DELETE' THEN
	    r_old = OLD;
	    r_new = NULL;
	    r_ret = OLD;
	ELSIF TG_OP = 'UPDATE' THEN
	    r_old = OLD;
	    r_new = NEW;
	    r_ret = NEW;
	END IF;

	IF TG_WHEN = 'AFTER' THEN
	   r_ret = NULL;
	END IF;

        RAISE NOTICE 'trigger: name %; when: %; lev: %s; op: %; old: % new: %',
            TG_NAME, TG_WHEN, TG_LEVEL, TG_OP, r_old, r_new;

	RETURN r_ret;
    END;
    $body$;
}

teardown
{
     DROP TABLE trigtest;
     DROP FUNCTION noisy_oper(text, anynonarray, text, anynonarray);
     DROP FUNCTION trig_report();
}


session s0
step s0_rep { SELECT * FROM trigtest ORDER BY key, data }

session s1
#setup          {  }
step s1_b_rc     { BEGIN ISOLATION LEVEL READ COMMITTED; SELECT 1; }
step s1_b_rr     { BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT 1; }
step s1_c     { COMMIT; }
step s1_r     { ROLLBACK; }
step s1_trig_rep_b_i { CREATE TRIGGER rep_b_i BEFORE INSERT ON trigtest FOR EACH ROW EXECUTE PROCEDURE trig_report(); }
step s1_trig_rep_a_i { CREATE TRIGGER rep_a_i AFTER INSERT ON trigtest FOR EACH ROW EXECUTE PROCEDURE trig_report(); }
step s1_trig_rep_b_u { CREATE TRIGGER rep_b_u BEFORE UPDATE ON trigtest FOR EACH ROW EXECUTE PROCEDURE trig_report(); }
step s1_trig_rep_a_u { CREATE TRIGGER rep_a_u AFTER UPDATE ON trigtest FOR EACH ROW EXECUTE PROCEDURE trig_report(); }
step s1_trig_rep_b_d { CREATE TRIGGER rep_b_d BEFORE DELETE ON trigtest FOR EACH ROW EXECUTE PROCEDURE trig_report(); }
step s1_trig_rep_a_d { CREATE TRIGGER rep_a_d AFTER DELETE ON trigtest FOR EACH ROW EXECUTE PROCEDURE trig_report(); }
step s1_ins_a { INSERT INTO trigtest VALUES ('key-a', 'val-a-s1') RETURNING *; }
step s1_ins_b { INSERT INTO trigtest VALUES ('key-b', 'val-b-s1') RETURNING *; }
step s1_ins_c { INSERT INTO trigtest VALUES ('key-c', 'val-c-s1') RETURNING *; }
step s1_del_a {
    DELETE FROM trigtest
    WHERE
        noisy_oper('upd', key, '=', 'key-a') AND
        noisy_oper('upk', data, '<>', 'mismatch')
    RETURNING *
}
step s1_del_b {
    DELETE FROM trigtest
    WHERE
        noisy_oper('upd', key, '=', 'key-b') AND
        noisy_oper('upk', data, '<>', 'mismatch')
    RETURNING *
}
step s1_upd_a_data {
    UPDATE trigtest SET data = data || '-ups1'
    WHERE
        noisy_oper('upd', key, '=', 'key-a') AND
        noisy_oper('upk', data, '<>', 'mismatch')
    RETURNING *;
}
step s1_upd_b_data {
    UPDATE trigtest SET data = data || '-ups1'
    WHERE
        noisy_oper('upd', key, '=', 'key-b') AND
        noisy_oper('upk', data, '<>', 'mismatch')
    RETURNING *;
}
step s1_upd_a_tob {
    UPDATE trigtest SET key = 'key-b', data = data || '-tobs1'
    WHERE
        noisy_oper('upk', key, '=', 'key-a') AND
        noisy_oper('upk', data, '<>', 'mismatch')
    RETURNING *;
}

session s2
#setup          {  }
step s2_b_rc     { BEGIN ISOLATION LEVEL READ COMMITTED; SELECT 1; }
step s2_b_rr     { BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT 1; }
step s2_c     { COMMIT; }
step s2_r     { ROLLBACK; }
step s2_ins_a { INSERT INTO trigtest VALUES ('key-a', 'val-a-s2') RETURNING *; }
step s2_del_a {
    DELETE FROM trigtest
    WHERE
        noisy_oper('upd', key, '=', 'key-a') AND
        noisy_oper('upk', data, '<>', 'mismatch')
    RETURNING *
}
step s2_upd_a_data {
    UPDATE trigtest SET data = data || '-ups2'
    WHERE
        noisy_oper('upd', key, '=', 'key-a') AND
        noisy_oper('upk', data, '<>', 'mismatch')
    RETURNING *;
}
step s2_upd_b_data {
    UPDATE trigtest SET data = data || '-ups2'
    WHERE
        noisy_oper('upd', key, '=', 'key-b') AND
        noisy_oper('upk', data, '<>', 'mismatch')
    RETURNING *;
}
step s2_upd_all_data {
    UPDATE trigtest SET data = data || '-ups2'
    WHERE
        noisy_oper('upd', key, '<>', 'mismatch') AND
        noisy_oper('upk', data, '<>', 'mismatch')
    RETURNING *;
}
step s2_upsert_a_data {
    INSERT INTO trigtest VALUES ('key-a', 'val-a-upss2')
    ON CONFLICT (key)
        DO UPDATE SET data = trigtest.data || '-upserts2'
        WHERE
            noisy_oper('upd', trigtest.key, '=', 'key-a') AND
            noisy_oper('upk', trigtest.data, '<>', 'mismatch')
    RETURNING *;
}

session s3
#setup          {  }
step s3_b_rc     { BEGIN ISOLATION LEVEL READ COMMITTED; SELECT 1; }
step s3_c     { COMMIT; }
step s3_r     { ROLLBACK; }
step s3_del_a {
    DELETE FROM trigtest
    WHERE
        noisy_oper('upd', key, '=', 'key-a') AND
        noisy_oper('upk', data, '<>', 'mismatch')
    RETURNING *
}
step s3_upd_a_data {
    UPDATE trigtest SET data = data || '-ups3'
    WHERE
        noisy_oper('upd', key, '=', 'key-a') AND
        noisy_oper('upk', data, '<>', 'mismatch')
    RETURNING *;
}

### base case verifying that triggers see performed modifications
# s1 updates, s1 commits, s2 updates
permutation s1_trig_rep_b_u s1_trig_rep_a_u
    s1_ins_a s1_ins_b s1_b_rc s2_b_rc
    s1_upd_a_data s1_c s2_upd_a_data s2_c
    s0_rep
# s1 updates, s1 rolls back, s2 updates
permutation s1_trig_rep_b_u s1_trig_rep_a_u
    s1_ins_a s1_ins_b s1_b_rc s2_b_rc
    s1_upd_a_data s1_r s2_upd_a_data s2_c
    s0_rep
# s1 updates, s1 commits back, s2 deletes
permutation s1_trig_rep_b_d s1_trig_rep_b_u s1_trig_rep_a_d s1_trig_rep_a_u
    s1_ins_a s1_ins_b s1_b_rc s2_b_rc
    s1_upd_a_data s1_c s2_del_a s2_c
    s0_rep
# s1 updates, s1 rolls back back, s2 deletes
permutation s1_trig_rep_b_d s1_trig_rep_b_u s1_trig_rep_a_d s1_trig_rep_a_u
    s1_ins_a s1_ins_b s1_b_rc s2_b_rc
    s1_upd_a_data s1_r s2_del_a s2_c
    s0_rep

### Verify EPQ is performed if necessary, and skipped if transaction rolled back
# s1 updates, s2 updates, s1 commits, EPQ
permutation s1_trig_rep_b_u s1_trig_rep_a_u
    s1_ins_a s1_ins_b s1_b_rc s2_b_rc
    s1_upd_a_data s2_upd_a_data s1_c s2_c
    s0_rep
# s1 updates, s2 updates, s1 rolls back, no EPQ
permutation s1_trig_rep_b_u s1_trig_rep_a_u
    s1_ins_a s1_ins_b s1_b_rc s2_b_rc
    s1_upd_a_data s2_upd_a_data s1_r s2_c
    s0_rep
# s1 updates, s2 deletes, s1 commits, EPQ
permutation s1_trig_rep_b_d s1_trig_rep_b_u s1_trig_rep_a_d s1_trig_rep_a_u
    s1_ins_a s1_ins_b s1_b_rc s2_b_rc
    s1_upd_a_data s2_upd_a_data s1_c s2_c
    s0_rep
# s1 updates, s2 deletes, s1 rolls back, no EPQ
permutation s1_trig_rep_b_d s1_trig_rep_b_u s1_trig_rep_a_d s1_trig_rep_a_u
    s1_ins_a s1_ins_b s1_b_rc s2_b_rc
    s1_upd_a_data s2_upd_a_data s1_r s2_c
    s0_rep
# s1 deletes, s2 updates, s1 commits, EPQ
permutation s1_trig_rep_b_d s1_trig_rep_b_u s1_trig_rep_a_d s1_trig_rep_a_u
    s1_ins_a s1_ins_b s1_b_rc s2_b_rc
    s1_del_a s2_upd_a_data s1_c s2_c
    s0_rep
# s1 deletes, s2 updates, s1 rolls back, no EPQ
permutation s1_trig_rep_b_d s1_trig_rep_b_u s1_trig_rep_a_d s1_trig_rep_a_u
    s1_ins_a s1_ins_b s1_b_rc s2_b_rc
    s1_del_a s2_upd_a_data s1_r s2_c
    s0_rep
# s1 inserts, s2 inserts, s1 commits, s2 inserts, unique conflict
permutation s1_trig_rep_b_i s1_trig_rep_b_d s1_trig_rep_a_i s1_trig_rep_a_d
    s1_b_rc s2_b_rc
    s1_ins_a s2_ins_a s1_c s2_c
    s0_rep
# s1 inserts, s2 inserts, s1 rolls back, s2 inserts, no unique conflict
permutation s1_trig_rep_b_i s1_trig_rep_b_d s1_trig_rep_a_i s1_trig_rep_a_d
    s1_b_rc s2_b_rc
    s1_ins_a s2_ins_a s1_r s2_c
    s0_rep
# s1 updates, s2 upserts, s1 commits, EPQ
permutation s1_trig_rep_b_i s1_trig_rep_b_d s1_trig_rep_b_u s1_trig_rep_a_i s1_trig_rep_a_d s1_trig_rep_a_u
    s1_ins_a s1_ins_b s1_b_rc s2_b_rc
    s1_upd_a_data s2_upsert_a_data s1_c s2_c
    s0_rep
# s1 updates, s2 upserts, s1 rolls back, no EPQ
permutation s1_trig_rep_b_i s1_trig_rep_b_d s1_trig_rep_b_u s1_trig_rep_a_i s1_trig_rep_a_d s1_trig_rep_a_u
    s1_ins_a s1_ins_b s1_b_rc s2_b_rc
    s1_upd_a_data s2_upsert_a_data s1_c s2_c
    s0_rep
# s1 inserts, s2 upserts, s1 commits
permutation s1_trig_rep_b_i s1_trig_rep_b_d s1_trig_rep_b_u s1_trig_rep_a_i s1_trig_rep_a_d s1_trig_rep_a_u
    s1_b_rc s2_b_rc
    s1_ins_a s2_upsert_a_data s1_c s2_c
    s0_rep
# s1 inserts, s2 upserts, s1 rolls back
permutation s1_trig_rep_b_i s1_trig_rep_b_d s1_trig_rep_b_u s1_trig_rep_a_i s1_trig_rep_a_d s1_trig_rep_a_u
    s1_b_rc s2_b_rc
    s1_ins_a s2_upsert_a_data s1_r s2_c
    s0_rep
# s1 inserts, s2 upserts, s1 updates, s1 commits, EPQ
permutation s1_trig_rep_b_i s1_trig_rep_b_d s1_trig_rep_b_u s1_trig_rep_a_i s1_trig_rep_a_d s1_trig_rep_a_u
    s1_b_rc s2_b_rc
    s1_ins_a s1_upd_a_data s2_upsert_a_data s1_c s2_c
    s0_rep
# s1 inserts, s2 upserts, s1 updates, s1 rolls back, no EPQ
permutation s1_trig_rep_b_i s1_trig_rep_b_d s1_trig_rep_b_u s1_trig_rep_a_i s1_trig_rep_a_d s1_trig_rep_a_u
    s1_b_rc s2_b_rc
    s1_ins_a s1_upd_a_data s2_upsert_a_data s1_r s2_c
    s0_rep

### Verify EPQ is performed if necessary, and skipped if transaction rolled back,
### just without before triggers (for comparison, no additional row locks)
# s1 updates, s2 updates, s1 commits, EPQ
permutation s1_trig_rep_a_u
    s1_ins_a s1_ins_b s1_b_rc s2_b_rc
    s1_upd_a_data s2_upd_a_data s1_c s2_c
    s0_rep
# s1 updates, s2 updates, s1 rolls back, no EPQ
permutation s1_trig_rep_a_u
    s1_ins_a s1_ins_b s1_b_rc s2_b_rc
    s1_upd_a_data s2_upd_a_data s1_r s2_c
    s0_rep
# s1 updates, s2 deletes, s1 commits, EPQ
permutation s1_trig_rep_a_d s1_trig_rep_a_u
    s1_ins_a s1_ins_b s1_b_rc s2_b_rc
    s1_upd_a_data s2_del_a s1_c s2_c
    s0_rep
# s1 updates, s2 deletes, s1 rolls back, no EPQ
permutation s1_trig_rep_a_d s1_trig_rep_a_u
    s1_ins_a s1_ins_b s1_b_rc s2_b_rc
    s1_upd_a_data s2_del_a s1_r s2_c
    s0_rep
# s1 deletes, s2 updates, s1 commits, EPQ
permutation s1_trig_rep_a_d s1_trig_rep_a_u
    s1_ins_a s1_ins_b s1_b_rc s2_b_rc
    s1_del_a s2_upd_a_data s1_c s2_c
    s0_rep
# s1 deletes, s2 updates, s1 rolls back, no EPQ
permutation s1_trig_rep_a_d s1_trig_rep_a_u
    s1_ins_a s1_ins_b s1_b_rc s2_b_rc
    s1_del_a s2_upd_a_data s1_r s2_c
    s0_rep
# s1 deletes, s2 deletes, s1 commits, EPQ
permutation s1_trig_rep_a_d
    s1_ins_a s1_ins_b s1_b_rc s2_b_rc
    s1_del_a s2_del_a s1_c s2_c
    s0_rep
# s1 deletes, s2 deletes, s1 rolls back, no EPQ
permutation s1_trig_rep_a_d
    s1_ins_a s1_ins_b s1_b_rc s2_b_rc
    s1_del_a s2_del_a s1_r s2_c
    s0_rep

### Verify that an update affecting a row that has been
### updated/deleted to not match the where clause anymore works
### correctly
# s1 updates to different key, s2 updates old key, s1 commits, EPQ failure should lead to no update
permutation s1_trig_rep_b_u s1_trig_rep_a_u
    s1_ins_a s1_ins_c s1_b_rc s2_b_rc
    s1_upd_a_tob s2_upd_a_data s1_c s2_c
    s0_rep
# s1 updates to different key, s2 updates old key, s1 rolls back, no EPQ failure
permutation s1_trig_rep_b_u s1_trig_rep_a_u
    s1_ins_a s1_ins_c s1_b_rc s2_b_rc
    s1_upd_a_tob s2_upd_a_data s1_r s2_c
    s0_rep
# s1 updates to different key, s2 updates new key, s1 commits, s2 will
# not see tuple with new key and not block
permutation s1_trig_rep_b_u s1_trig_rep_a_u
    s1_ins_a s1_ins_c s1_b_rc s2_b_rc
    s1_upd_a_tob s2_upd_b_data s1_c s2_c
    s0_rep
# s1 updates to different key, s2 updates all keys, s1 commits, s2,
# will not see tuple with old key, but block on old, and then follow
# the chain
permutation s1_trig_rep_b_u s1_trig_rep_a_u
    s1_ins_a s1_ins_c s1_b_rc s2_b_rc
    s1_upd_a_tob s2_upd_all_data s1_c s2_c
    s0_rep
# s1 deletes, s2 updates, s1 committs, EPQ failure should lead to no update
permutation s1_trig_rep_b_d s1_trig_rep_b_u s1_trig_rep_a_d s1_trig_rep_a_u
    s1_ins_a s1_ins_c s1_b_rc s2_b_rc
    s1_del_a s2_upd_a_data s1_c s2_c
    s0_rep
# s1 deletes, s2 updates, s1 rolls back, no EPQ failure
permutation s1_trig_rep_b_d s1_trig_rep_b_u s1_trig_rep_a_d s1_trig_rep_a_u
    s1_ins_a s1_ins_c s1_b_rc s2_b_rc
    s1_del_a s2_upd_a_data s1_r s2_c
    s0_rep
# s1 deletes, s2 deletes, s1 committs, EPQ failure should lead to no delete
permutation s1_trig_rep_b_d s1_trig_rep_a_d
    s1_ins_a s1_ins_c s1_b_rc s2_b_rc
    s1_del_a s2_del_a s1_c s2_c
    s0_rep
# s1 deletes, s2 deletes, s1 rolls back, no EPQ failure
permutation s1_trig_rep_b_d s1_trig_rep_a_d
    s1_ins_a s1_ins_c s1_b_rc s2_b_rc
    s1_del_a s2_del_a s1_r s2_c
    s0_rep

### Verify EPQ with more than two participants works
## XXX: Disable tests, there is some potential for instability here that's not yet fully understood
## s1 updates, s2 updates, s3 updates, s1 commits, s2 EPQ, s2 commits, s3 EPQ
#permutation s1_trig_rep_b_u s1_trig_rep_a_u
#    s1_ins_a s1_ins_b s1_b_rc s2_b_rc s3_b_rc
#    s1_upd_a_data s2_upd_a_data s3_upd_a_data s1_c s2_c s3_c
#    s0_rep
## s1 updates, s2 updates, s3 updates, s1 commits, s2 EPQ, s2 rolls back, s3 EPQ
#permutation s1_trig_rep_b_u s1_trig_rep_a_u
#    s1_ins_a s1_ins_b s1_b_rc s2_b_rc s3_b_rc
#    s1_upd_a_data s2_upd_a_data s3_upd_a_data s1_c s2_r s3_c
#    s0_rep
## s1 updates, s3 updates, s2 upserts, s1 updates, s1 commits, s3 EPQ, s3 deletes, s3 commits, s2 inserts without EPQ recheck
#permutation s1_trig_rep_b_i s1_trig_rep_b_d s1_trig_rep_b_u s1_trig_rep_a_i s1_trig_rep_a_d s1_trig_rep_a_u
#    s1_ins_a s1_b_rc s2_b_rc s3_b_rc
#    s1_upd_a_data s3_upd_a_data s2_upsert_a_data s1_upd_a_data s1_c s3_del_a s3_c s2_c
#    s0_rep
## s1 updates, s3 updates, s2 upserts, s1 updates, s1 commits, s3 EPQ, s3 deletes, s3 rolls back, s2 EPQ
#permutation s1_trig_rep_b_i s1_trig_rep_b_d s1_trig_rep_b_u s1_trig_rep_a_i s1_trig_rep_a_d s1_trig_rep_a_u
#    s1_ins_a s1_b_rc s2_b_rc s3_b_rc
#    s1_upd_a_data s3_upd_a_data s2_upsert_a_data s1_upd_a_data s1_c s3_del_a s3_r s2_c
#    s0_rep

### Document that EPQ doesn't "leap" onto a tuple that would match after blocking
# s1 inserts a, s1 updates b, s2 updates b, s1 deletes b, s1 updates a to b, s1 commits, s2 EPQ finds tuple deleted
permutation s1_trig_rep_b_i s1_trig_rep_b_d s1_trig_rep_b_u s1_trig_rep_a_i s1_trig_rep_a_d s1_trig_rep_a_u
    s1_ins_b s1_b_rc s2_b_rc
    s1_ins_a s1_upd_b_data s2_upd_b_data s1_del_b s1_upd_a_tob s1_c s2_c
    s0_rep

### Triggers for EPQ detect serialization failures
# s1 updates, s2 updates, s1 commits, serialization failure
permutation s1_trig_rep_b_u s1_trig_rep_a_u
    s1_ins_a s1_ins_b s1_b_rr s2_b_rr
    s1_upd_a_data s2_upd_a_data s1_c s2_c
    s0_rep
# s1 updates, s2 updates, s1 rolls back, s2 succeeds
permutation s1_trig_rep_b_u s1_trig_rep_a_u
    s1_ins_a s1_ins_b s1_b_rr s2_b_rr
    s1_upd_a_data s2_upd_a_data s1_r s2_c
    s0_rep
# s1 deletes, s2 updates, s1 commits, serialization failure
permutation s1_trig_rep_b_d s1_trig_rep_b_u s1_trig_rep_a_d s1_trig_rep_a_u
    s1_ins_a s1_ins_b s1_b_rr s2_b_rr
    s1_del_a s2_upd_a_data s1_c s2_c
    s0_rep
# s1 deletes, s2 updates, s1 rolls back, s2 succeeds
permutation s1_trig_rep_b_d s1_trig_rep_b_u s1_trig_rep_a_d s1_trig_rep_a_u
    s1_ins_a s1_ins_b s1_b_rr s2_b_rr
    s1_del_a s2_upd_a_data s1_r s2_c
    s0_rep