-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
plpgsql_txn
641 lines (561 loc) · 11.9 KB
/
plpgsql_txn
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
# LogicTest: !local-mixed-23.1
statement ok
CREATE TABLE t (x INT);
# Variables are preserved across both COMMIT and ROLLBACK.
statement ok
CREATE PROCEDURE p(a INT, b INT) LANGUAGE PLpgSQL AS $$
DECLARE
x INT := 0;
y INT := 1;
BEGIN
RAISE NOTICE 'a: % b: % x: % y: %', a, b, x, y;
a := a + 1;
COMMIT;
RAISE NOTICE 'a: % b: % x: % y: %', a, b, x, y;
b := b + 1;
COMMIT;
RAISE NOTICE 'a: % b: % x: % y: %', a, b, x, y;
x := x + 1;
ROLLBACK;
RAISE NOTICE 'a: % b: % x: % y: %', a, b, x, y;
y := y + 1;
ROLLBACK;
RAISE NOTICE 'a: % b: % x: % y: %', a, b, x, y;
END;
$$;
query T noticetrace
CALL p(100, 101);
----
NOTICE: a: 100 b: 101 x: 0 y: 1
NOTICE: a: 101 b: 101 x: 0 y: 1
NOTICE: a: 101 b: 102 x: 0 y: 1
NOTICE: a: 101 b: 102 x: 1 y: 1
NOTICE: a: 101 b: 102 x: 1 y: 2
# Verify that mutations are correctly committed or reverted.
statement ok
DROP PROCEDURE p;
CREATE PROCEDURE p() LANGUAGE PLpgSQL AS $$
BEGIN
RAISE NOTICE 'max: %', (SELECT max(x) FROM t);
RAISE NOTICE 'INSERT (1)';
INSERT INTO t VALUES (1);
RAISE NOTICE 'max: %', (SELECT max(x) FROM t);
RAISE NOTICE 'COMMIT;';
COMMIT;
RAISE NOTICE 'max: %', (SELECT max(x) FROM t);
RAISE NOTICE 'INSERT (2)';
INSERT INTO t VALUES (2);
RAISE NOTICE 'max: %', (SELECT max(x) FROM t);
RAISE NOTICE 'ROLLBACK;';
ROLLBACK;
RAISE NOTICE 'max: %', (SELECT max(x) FROM t);
RAISE NOTICE 'DELETE (1)';
DELETE FROM t WHERE x = 1;
RAISE NOTICE 'max: %', (SELECT max(x) FROM t);
RAISE NOTICE 'ROLLBACK;';
ROLLBACK;
RAISE NOTICE 'max: %', (SELECT max(x) FROM t);
END;
$$;
query T noticetrace
CALL p();
----
NOTICE: max: <NULL>
NOTICE: INSERT (1)
NOTICE: max: 1
NOTICE: COMMIT;
NOTICE: max: 1
NOTICE: INSERT (2)
NOTICE: max: 2
NOTICE: ROLLBACK;
NOTICE: max: 1
NOTICE: DELETE (1)
NOTICE: max: <NULL>
NOTICE: ROLLBACK;
NOTICE: max: 1
query I rowsort
SELECT * FROM t;
----
1
# Verify that the transaction timestamp is advanced.
statement ok
CREATE TABLE txn_timestamps (ts TIMESTAMP);
statement ok
DROP PROCEDURE p;
CREATE PROCEDURE p() LANGUAGE PLpgSQL AS $$
BEGIN
INSERT INTO txn_timestamps VALUES (now());
COMMIT;
INSERT INTO txn_timestamps VALUES (now());
ROLLBACK;
INSERT INTO txn_timestamps VALUES (now());
END;
$$;
statement ok
CALL p();
query I
SELECT count(DISTINCT ts) FROM txn_timestamps;
----
2
# There should be no blocking when updating and locking the same row in
# different transactions, since they never overlap.
statement ok
DROP PROCEDURE p;
CREATE PROCEDURE p() LANGUAGE PLpgSQL AS $$
BEGIN
SELECT * FROM t FOR UPDATE;
COMMIT;
UPDATE t SET x = x + 1;
ROLLBACK;
UPDATE t SET x = x + 1;
END;
$$;
CALL p();
query I
SELECT * FROM t;
----
2
subtest branches_loops
statement ok
DROP PROCEDURE p;
CREATE PROCEDURE p(a INT) LANGUAGE PLpgSQL AS $$
DECLARE
i INT;
BEGIN
IF i IS NULL THEN
i := 0;
ROLLBACK;
END IF;
WHILE i < 5 LOOP
IF i = 2 THEN
ROLLBACK;
END IF;
i := i + 1;
COMMIT;
IF i = a THEN
EXIT;
END IF;
END LOOP;
RAISE NOTICE '%', i;
END;
$$;
query T noticetrace
CALL p(2);
----
NOTICE: 2
query T noticetrace
CALL p(10);
----
NOTICE: 5
subtest cursors
statement ok
DROP PROCEDURE p;
CREATE PROCEDURE p(a INT) LANGUAGE PLpgSQL AS $$
DECLARE
foo CURSOR FOR SELECT 1;
foundException BOOL := false;
BEGIN
IF a = 0 THEN
OPEN foo;
COMMIT;
BEGIN
CLOSE foo;
EXCEPTION WHEN SQLSTATE '34000' THEN
foundException := true;
END;
ELSIF a = 1 THEN
OPEN foo;
ROLLBACK;
BEGIN
CLOSE foo;
EXCEPTION WHEN SQLSTATE '34000' THEN
foundException := true;
END;
ELSIF a = 3 THEN
COMMIT;
OPEN foo;
BEGIN
CLOSE foo;
EXCEPTION WHEN SQLSTATE '34000' THEN
foundException := true;
END;
ELSE
ROLLBACK;
OPEN foo;
BEGIN
CLOSE foo;
EXCEPTION WHEN SQLSTATE '34000' THEN
foundException := true;
END;
END IF;
RAISE NOTICE 'found exception: %', foundException;
END;
$$;
# Closing the transaction causes the cursor to be closed as well.
query T noticetrace
CALL p(0);
----
NOTICE: found exception: true
# Closing the transaction causes the cursor to be closed as well.
query T noticetrace
CALL p(1);
----
NOTICE: found exception: true
# The cursor is opened after the new transaction starts, so no exception.
query T noticetrace
CALL p(2);
----
NOTICE: found exception: false
# The cursor is opened after the new transaction starts, so no exception.
query T noticetrace
CALL p(3);
----
NOTICE: found exception: false
statement ok
SET close_cursors_at_commit = false;
# The cursor stays open after commit due to the setting.
query T noticetrace
CALL p(0);
----
NOTICE: found exception: false
# The cursor is still closed, because the transaction was aborted.
query T noticetrace
CALL p(1);
----
NOTICE: found exception: true
statement ok
RESET close_cursors_at_commit;
CLOSE ALL;
subtest blocks
statement ok
DROP PROCEDURE p;
CREATE PROCEDURE p(a INT) LANGUAGE PLpgSQL AS $$
BEGIN
RAISE NOTICE '%', (SELECT max(x) FROM t);
INSERT INTO t VALUES (3);
ROLLBACK;
RAISE NOTICE '%', (SELECT max(x) FROM t);
IF a > 0 THEN
BEGIN
RAISE NOTICE '%', (SELECT max(x) FROM t);
INSERT INTO t VALUES (4);
COMMIT;
RAISE NOTICE '%', (SELECT max(x) FROM t);
END;
END IF;
RAISE NOTICE '%', (SELECT max(x) FROM t);
INSERT INTO t VALUES (5);
COMMIT;
RAISE NOTICE '%', (SELECT max(x) FROM t);
IF a > 0 THEN
BEGIN
RAISE NOTICE '%', (SELECT max(x) FROM t);
INSERT INTO t VALUES (6);
ROLLBACK;
RAISE NOTICE '%', (SELECT max(x) FROM t);
END;
END IF;
RAISE NOTICE '%', (SELECT max(x) FROM t);
END;
$$;
query T noticetrace
CALL p(0);
----
NOTICE: 2
NOTICE: 2
NOTICE: 2
NOTICE: 5
NOTICE: 5
statement ok
DELETE FROM t WHERE x > 2;
query T noticetrace
CALL p(1);
----
NOTICE: 2
NOTICE: 2
NOTICE: 2
NOTICE: 4
NOTICE: 4
NOTICE: 5
NOTICE: 5
NOTICE: 5
NOTICE: 5
query I rowsort
SELECT * FROM t;
----
2
4
5
subtest exceptions
statement ok
DELETE FROM t WHERE True;
INSERT INTO t VALUES (1);
statement ok
DROP PROCEDURE p;
CREATE PROCEDURE p(a INT) LANGUAGE PLpgSQL AS $$
BEGIN
RAISE NOTICE '%', (SELECT max(x) FROM t);
INSERT INTO t VALUES (2);
COMMIT;
RAISE NOTICE '%', (SELECT max(x) FROM t);
BEGIN
IF (a // 0) > 0 THEN
RAISE NOTICE 'foo';
END IF;
EXCEPTION WHEN division_by_zero THEN
RAISE NOTICE 'bar';
END;
RAISE NOTICE '%', (SELECT max(x) FROM t);
INSERT INTO t VALUES (3);
ROLLBACK;
RAISE NOTICE '%', (SELECT max(x) FROM t);
END;
$$;
query T noticetrace
CALL p(1);
----
NOTICE: 1
NOTICE: 2
NOTICE: bar
NOTICE: 2
NOTICE: 2
query I rowsort
SELECT * FROM t;
----
1
2
statement ok
DROP PROCEDURE p;
# COMMIT is not valid in a block with an exception handler.
statement error pgcode 2D000 pq: invalid transaction termination
CREATE PROCEDURE p(a INT) LANGUAGE PLpgSQL AS $$
BEGIN
IF a > 0 THEN
COMMIT;
END IF;
EXCEPTION WHEN division_by_zero THEN
RAISE NOTICE 'foo';
END;
$$;
statement error pgcode 2D000 pq: invalid transaction termination
CREATE PROCEDURE p(a INT) LANGUAGE PLpgSQL AS $$
BEGIN
IF a > 0 THEN
ROLLBACK;
END IF;
EXCEPTION WHEN division_by_zero THEN
RAISE NOTICE 'foo';
END;
$$;
# The cases above, with a nested block.
statement error pgcode 2D000 pq: invalid transaction termination
CREATE PROCEDURE p(a INT) LANGUAGE PLpgSQL AS $$
BEGIN
BEGIN
IF a > 0 THEN
COMMIT;
END IF;
EXCEPTION WHEN division_by_zero THEN
RAISE NOTICE 'foo';
END;
END;
$$;
statement error pgcode 2D000 pq: invalid transaction termination
CREATE PROCEDURE p(a INT) LANGUAGE PLpgSQL AS $$
BEGIN
BEGIN
IF a > 0 THEN
ROLLBACK;
END IF;
EXCEPTION WHEN division_by_zero THEN
RAISE NOTICE 'foo';
END;
END;
$$;
subtest postgres_example
# This case is adopted from the postgres "Transaction Management" docs page.
statement ok
CREATE TABLE test1 (a INT);
statement ok
CREATE PROCEDURE transaction_test1() LANGUAGE plpgsql AS $$
DECLARE
i INT := 0;
BEGIN
WHILE i <= 9 LOOP
RAISE NOTICE 'i = %', i;
INSERT INTO test1 (a) VALUES (i);
IF i % 2 = 0 THEN
RAISE NOTICE 'COMMIT;';
COMMIT;
ELSE
RAISE NOTICE 'ROLLBACK;';
ROLLBACK;
END IF;
i := i + 1;
END LOOP;
END;
$$;
query T noticetrace
CALL transaction_test1();
----
NOTICE: i = 0
NOTICE: COMMIT;
NOTICE: i = 1
NOTICE: ROLLBACK;
NOTICE: i = 2
NOTICE: COMMIT;
NOTICE: i = 3
NOTICE: ROLLBACK;
NOTICE: i = 4
NOTICE: COMMIT;
NOTICE: i = 5
NOTICE: ROLLBACK;
NOTICE: i = 6
NOTICE: COMMIT;
NOTICE: i = 7
NOTICE: ROLLBACK;
NOTICE: i = 8
NOTICE: COMMIT;
NOTICE: i = 9
NOTICE: ROLLBACK;
query I rowsort
SELECT * FROM test1;
----
0
2
4
6
8
subtest function
statement error pgcode 2D000 pq: invalid transaction termination
CREATE FUNCTION f() RETURNS INT LANGUAGE PLpgSQL AS $$
BEGIN
COMMIT;
RETURN 1;
END;
$$;
statement error pgcode 2D000 pq: invalid transaction termination
CREATE FUNCTION f() RETURNS INT LANGUAGE PLpgSQL AS $$
BEGIN
ROLLBACK;
RETURN 1;
END;
$$;
subtest explicit_txn
statement ok
CREATE PROCEDURE p() LANGUAGE PLpgSQL AS $$
BEGIN
COMMIT;
END;
$$;
statement ok
BEGIN;
statement error pgcode 2D000 pq: invalid transaction termination
CALL p();
statement ok
ABORT;
statement ok
DROP PROCEDURE p;
CREATE PROCEDURE p() LANGUAGE PLpgSQL AS $$
BEGIN
ROLLBACK;
END;
$$;
statement ok
BEGIN;
statement error pgcode 2D000 pq: invalid transaction termination
CALL p();
statement ok
ABORT;
subtest serializable_retry
statement ok
DELETE FROM t WHERE x <> 1;
statement ok
CREATE SEQUENCE s1;
CREATE SEQUENCE s2;
# TODO(#119632): we can't use RAISE statements, since they cause results to be
# flushed to the client, which prevents retries.
statement ok
DROP PROCEDURE p;
CREATE PROCEDURE p() LANGUAGE PLpgSQL AS $$
BEGIN
INSERT INTO t VALUES (2);
COMMIT;
INSERT INTO t VALUES (3);
SELECT IF(nextval('s1')<3, crdb_internal.force_retry('1h':::INTERVAL), 0);
ROLLBACK;
INSERT INTO t VALUES (4);
SELECT IF(nextval('s2')<3, crdb_internal.force_retry('1h':::INTERVAL), 0);
COMMIT;
END
$$;
statement ok
CALL p();
query I rowsort
SELECT * FROM t;
----
1
2
4
# Verify that the automatic retries happened.
query II
SELECT currval('s1'), currval('s2');
----
3 3
subtest read_committed_retry
statement ok
DELETE FROM t WHERE x <> 1;
statement ok
DROP PROCEDURE p;
DROP SEQUENCE s1;
DROP SEQUENCE s2;
CREATE SEQUENCE s1;
CREATE SEQUENCE s2;
# TODO(#119632): we can't use RAISE statements, since they cause results to be
# flushed to the client, which prevents retries.
statement ok
CREATE PROCEDURE p() LANGUAGE PLpgSQL AS $$
BEGIN
INSERT INTO t VALUES (2);
COMMIT;
INSERT INTO t VALUES (3);
SELECT IF(nextval('s1')<3, crdb_internal.force_retry('1h':::INTERVAL), 0);
ROLLBACK;
INSERT INTO t VALUES (4);
SELECT IF(nextval('s2')<3, crdb_internal.force_retry('1h':::INTERVAL), 0);
COMMIT;
END
$$;
statement ok
SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ COMMITTED;
statement ok
CALL p();
query I rowsort
SELECT * FROM t;
----
1
2
4
# Verify that the automatic retries happened.
query II
SELECT currval('s1'), currval('s2');
----
3 3
statement ok
SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL SERIALIZABLE;
subtest err
statement ok
DROP PROCEDURE p;
statement error pgcode 0A000 pq: unimplemented: COMMIT or ROLLBACK with AND CHAIN syntax is not yet implemented
CREATE PROCEDURE p() LANGUAGE PLpgSQL AS $$
BEGIN
COMMIT AND CHAIN;
END
$$;
statement error pgcode 0A000 pq: unimplemented: COMMIT or ROLLBACK with AND CHAIN syntax is not yet implemented
CREATE PROCEDURE p() LANGUAGE PLpgSQL AS $$
BEGIN
ROLLBACK AND CHAIN;
END
$$;
subtest end