| 1 |
CREATE TABLE test1 (a int, b text); |
| 2 |
|
| 3 |
|
| 4 |
CREATE PROCEDURE transaction_test1() |
| 5 |
LANGUAGE pltcl |
| 6 |
AS $$ |
| 7 |
for {set i 0} {$i < 10} {incr i} { |
| 8 |
spi_exec "INSERT INTO test1 (a) VALUES ($i)" |
| 9 |
if {$i % 2 == 0} { |
| 10 |
commit |
| 11 |
} else { |
| 12 |
rollback |
| 13 |
} |
| 14 |
} |
| 15 |
$$; |
| 16 |
|
| 17 |
CALL transaction_test1(); |
| 18 |
|
| 19 |
SELECT * FROM test1; |
| 20 |
|
| 21 |
|
| 22 |
TRUNCATE test1; |
| 23 |
|
| 24 |
-- not allowed in a function |
| 25 |
CREATE FUNCTION transaction_test2() RETURNS int |
| 26 |
LANGUAGE pltcl |
| 27 |
AS $$ |
| 28 |
for {set i 0} {$i < 10} {incr i} { |
| 29 |
spi_exec "INSERT INTO test1 (a) VALUES ($i)" |
| 30 |
if {$i % 2 == 0} { |
| 31 |
commit |
| 32 |
} else { |
| 33 |
rollback |
| 34 |
} |
| 35 |
} |
| 36 |
return 1 |
| 37 |
$$; |
| 38 |
|
| 39 |
SELECT transaction_test2(); |
| 40 |
|
| 41 |
SELECT * FROM test1; |
| 42 |
|
| 43 |
|
| 44 |
-- also not allowed if procedure is called from a function |
| 45 |
CREATE FUNCTION transaction_test3() RETURNS int |
| 46 |
LANGUAGE pltcl |
| 47 |
AS $$ |
| 48 |
spi_exec "CALL transaction_test1()" |
| 49 |
return 1 |
| 50 |
$$; |
| 51 |
|
| 52 |
SELECT transaction_test3(); |
| 53 |
|
| 54 |
SELECT * FROM test1; |
| 55 |
|
| 56 |
|
| 57 |
-- commit inside cursor loop |
| 58 |
CREATE TABLE test2 (x int); |
| 59 |
INSERT INTO test2 VALUES (0), (1), (2), (3), (4); |
| 60 |
|
| 61 |
TRUNCATE test1; |
| 62 |
|
| 63 |
CREATE PROCEDURE transaction_test4a() |
| 64 |
LANGUAGE pltcl |
| 65 |
AS $$ |
| 66 |
spi_exec -array row "SELECT * FROM test2 ORDER BY x" { |
| 67 |
spi_exec "INSERT INTO test1 (a) VALUES ($row(x))" |
| 68 |
commit |
| 69 |
} |
| 70 |
$$; |
| 71 |
|
| 72 |
CALL transaction_test4a(); |
| 73 |
|
| 74 |
SELECT * FROM test1; |
| 75 |
|
| 76 |
|
| 77 |
-- rollback inside cursor loop |
| 78 |
TRUNCATE test1; |
| 79 |
|
| 80 |
CREATE PROCEDURE transaction_test4b() |
| 81 |
LANGUAGE pltcl |
| 82 |
AS $$ |
| 83 |
spi_exec -array row "SELECT * FROM test2 ORDER BY x" { |
| 84 |
spi_exec "INSERT INTO test1 (a) VALUES ($row(x))" |
| 85 |
rollback |
| 86 |
} |
| 87 |
$$; |
| 88 |
|
| 89 |
CALL transaction_test4b(); |
| 90 |
|
| 91 |
SELECT * FROM test1; |
| 92 |
|
| 93 |
|
| 94 |
-- check handling of an error during COMMIT |
| 95 |
CREATE TABLE testpk (id int PRIMARY KEY); |
| 96 |
CREATE TABLE testfk(f1 int REFERENCES testpk DEFERRABLE INITIALLY DEFERRED); |
| 97 |
|
| 98 |
CREATE PROCEDURE transaction_testfk() |
| 99 |
LANGUAGE pltcl |
| 100 |
AS $$ |
| 101 |
# this insert will fail during commit: |
| 102 |
spi_exec "INSERT INTO testfk VALUES (0)" |
| 103 |
commit |
| 104 |
elog WARNING "should not get here" |
| 105 |
$$; |
| 106 |
|
| 107 |
CALL transaction_testfk(); |
| 108 |
|
| 109 |
SELECT * FROM testpk; |
| 110 |
SELECT * FROM testfk; |
| 111 |
|
| 112 |
CREATE OR REPLACE PROCEDURE transaction_testfk() |
| 113 |
LANGUAGE pltcl |
| 114 |
AS $$ |
| 115 |
# this insert will fail during commit: |
| 116 |
spi_exec "INSERT INTO testfk VALUES (0)" |
| 117 |
if [catch {commit} msg] { |
| 118 |
elog INFO $msg |
| 119 |
} |
| 120 |
# these inserts should work: |
| 121 |
spi_exec "INSERT INTO testpk VALUES (1)" |
| 122 |
spi_exec "INSERT INTO testfk VALUES (1)" |
| 123 |
$$; |
| 124 |
|
| 125 |
CALL transaction_testfk(); |
| 126 |
|
| 127 |
SELECT * FROM testpk; |
| 128 |
SELECT * FROM testfk; |
| 129 |
|
| 130 |
|
| 131 |
DROP TABLE test1; |
| 132 |
DROP TABLE test2; |