| 1 |
CREATE TABLE test1 (a int, b text); |
| 2 |
CREATE PROCEDURE transaction_test1() |
| 3 |
LANGUAGE pltcl |
| 4 |
AS $$ |
| 5 |
for {set i 0} {$i < 10} {incr i} { |
| 6 |
spi_exec "INSERT INTO test1 (a) VALUES ($i)" |
| 7 |
if {$i % 2 == 0} { |
| 8 |
commit |
| 9 |
} else { |
| 10 |
rollback |
| 11 |
} |
| 12 |
} |
| 13 |
$$; |
| 14 |
CALL transaction_test1(); |
| 15 |
SELECT * FROM test1; |
| 16 |
a | b |
| 17 |
---+--- |
| 18 |
0 | |
| 19 |
2 | |
| 20 |
4 | |
| 21 |
6 | |
| 22 |
8 | |
| 23 |
(5 rows) |
| 24 |
|
| 25 |
TRUNCATE test1; |
| 26 |
-- not allowed in a function |
| 27 |
CREATE FUNCTION transaction_test2() RETURNS int |
| 28 |
LANGUAGE pltcl |
| 29 |
AS $$ |
| 30 |
for {set i 0} {$i < 10} {incr i} { |
| 31 |
spi_exec "INSERT INTO test1 (a) VALUES ($i)" |
| 32 |
if {$i % 2 == 0} { |
| 33 |
commit |
| 34 |
} else { |
| 35 |
rollback |
| 36 |
} |
| 37 |
} |
| 38 |
return 1 |
| 39 |
$$; |
| 40 |
SELECT transaction_test2(); |
| 41 |
ERROR: invalid transaction termination |
| 42 |
CONTEXT: while executing |
| 43 |
"commit" |
| 44 |
(procedure "__PLTcl_proc_transaction_test2" line 6) |
| 45 |
invoked from within |
| 46 |
"__PLTcl_proc_transaction_test2" |
| 47 |
in PL/Tcl function transaction_test2() |
| 48 |
SELECT * FROM test1; |
| 49 |
a | b |
| 50 |
---+--- |
| 51 |
(0 rows) |
| 52 |
|
| 53 |
-- also not allowed if procedure is called from a function |
| 54 |
CREATE FUNCTION transaction_test3() RETURNS int |
| 55 |
LANGUAGE pltcl |
| 56 |
AS $$ |
| 57 |
spi_exec "CALL transaction_test1()" |
| 58 |
return 1 |
| 59 |
$$; |
| 60 |
SELECT transaction_test3(); |
| 61 |
ERROR: invalid transaction termination |
| 62 |
CONTEXT: while executing |
| 63 |
"commit" |
| 64 |
(procedure "__PLTcl_proc_transaction_test1" line 6) |
| 65 |
invoked from within |
| 66 |
"__PLTcl_proc_transaction_test1" |
| 67 |
invoked from within |
| 68 |
"spi_exec "CALL transaction_test1()"" |
| 69 |
(procedure "__PLTcl_proc_transaction_test3" line 3) |
| 70 |
invoked from within |
| 71 |
"__PLTcl_proc_transaction_test3" |
| 72 |
in PL/Tcl function transaction_test3() |
| 73 |
SELECT * FROM test1; |
| 74 |
a | b |
| 75 |
---+--- |
| 76 |
(0 rows) |
| 77 |
|
| 78 |
-- commit inside cursor loop |
| 79 |
CREATE TABLE test2 (x int); |
| 80 |
INSERT INTO test2 VALUES (0), (1), (2), (3), (4); |
| 81 |
TRUNCATE test1; |
| 82 |
CREATE PROCEDURE transaction_test4a() |
| 83 |
LANGUAGE pltcl |
| 84 |
AS $$ |
| 85 |
spi_exec -array row "SELECT * FROM test2 ORDER BY x" { |
| 86 |
spi_exec "INSERT INTO test1 (a) VALUES ($row(x))" |
| 87 |
commit |
| 88 |
} |
| 89 |
$$; |
| 90 |
CALL transaction_test4a(); |
| 91 |
ERROR: cannot commit while a subtransaction is active |
| 92 |
CONTEXT: while executing |
| 93 |
"commit" |
| 94 |
invoked from within |
| 95 |
"spi_exec -array row "SELECT * FROM test2 ORDER BY x" { |
| 96 |
spi_exec "INSERT INTO test1 (a) VALUES ($row(x))" |
| 97 |
commit |
| 98 |
}" |
| 99 |
(procedure "__PLTcl_proc_transaction_test4a" line 3) |
| 100 |
invoked from within |
| 101 |
"__PLTcl_proc_transaction_test4a" |
| 102 |
in PL/Tcl function transaction_test4a() |
| 103 |
SELECT * FROM test1; |
| 104 |
a | b |
| 105 |
---+--- |
| 106 |
(0 rows) |
| 107 |
|
| 108 |
-- rollback inside cursor loop |
| 109 |
TRUNCATE test1; |
| 110 |
CREATE PROCEDURE transaction_test4b() |
| 111 |
LANGUAGE pltcl |
| 112 |
AS $$ |
| 113 |
spi_exec -array row "SELECT * FROM test2 ORDER BY x" { |
| 114 |
spi_exec "INSERT INTO test1 (a) VALUES ($row(x))" |
| 115 |
rollback |
| 116 |
} |
| 117 |
$$; |
| 118 |
CALL transaction_test4b(); |
| 119 |
ERROR: cannot roll back while a subtransaction is active |
| 120 |
CONTEXT: while executing |
| 121 |
"rollback" |
| 122 |
invoked from within |
| 123 |
"spi_exec -array row "SELECT * FROM test2 ORDER BY x" { |
| 124 |
spi_exec "INSERT INTO test1 (a) VALUES ($row(x))" |
| 125 |
rollback |
| 126 |
}" |
| 127 |
(procedure "__PLTcl_proc_transaction_test4b" line 3) |
| 128 |
invoked from within |
| 129 |
"__PLTcl_proc_transaction_test4b" |
| 130 |
in PL/Tcl function transaction_test4b() |
| 131 |
SELECT * FROM test1; |
| 132 |
a | b |
| 133 |
---+--- |
| 134 |
(0 rows) |
| 135 |
|
| 136 |
-- check handling of an error during COMMIT |
| 137 |
CREATE TABLE testpk (id int PRIMARY KEY); |
| 138 |
CREATE TABLE testfk(f1 int REFERENCES testpk DEFERRABLE INITIALLY DEFERRED); |
| 139 |
CREATE PROCEDURE transaction_testfk() |
| 140 |
LANGUAGE pltcl |
| 141 |
AS $$ |
| 142 |
# this insert will fail during commit: |
| 143 |
spi_exec "INSERT INTO testfk VALUES (0)" |
| 144 |
commit |
| 145 |
elog WARNING "should not get here" |
| 146 |
$$; |
| 147 |
CALL transaction_testfk(); |
| 148 |
ERROR: insert or update on table "testfk" violates foreign key constraint "testfk_f1_fkey" |
| 149 |
CONTEXT: while executing |
| 150 |
"commit" |
| 151 |
(procedure "__PLTcl_proc_transaction_testfk" line 5) |
| 152 |
invoked from within |
| 153 |
"__PLTcl_proc_transaction_testfk" |
| 154 |
in PL/Tcl function transaction_testfk() |
| 155 |
SELECT * FROM testpk; |
| 156 |
id |
| 157 |
---- |
| 158 |
(0 rows) |
| 159 |
|
| 160 |
SELECT * FROM testfk; |
| 161 |
f1 |
| 162 |
---- |
| 163 |
(0 rows) |
| 164 |
|
| 165 |
CREATE OR REPLACE PROCEDURE transaction_testfk() |
| 166 |
LANGUAGE pltcl |
| 167 |
AS $$ |
| 168 |
# this insert will fail during commit: |
| 169 |
spi_exec "INSERT INTO testfk VALUES (0)" |
| 170 |
if [catch {commit} msg] { |
| 171 |
elog INFO $msg |
| 172 |
} |
| 173 |
# these inserts should work: |
| 174 |
spi_exec "INSERT INTO testpk VALUES (1)" |
| 175 |
spi_exec "INSERT INTO testfk VALUES (1)" |
| 176 |
$$; |
| 177 |
CALL transaction_testfk(); |
| 178 |
INFO: insert or update on table "testfk" violates foreign key constraint "testfk_f1_fkey" |
| 179 |
SELECT * FROM testpk; |
| 180 |
id |
| 181 |
---- |
| 182 |
1 |
| 183 |
(1 row) |
| 184 |
|
| 185 |
SELECT * FROM testfk; |
| 186 |
f1 |
| 187 |
---- |
| 188 |
1 |
| 189 |
(1 row) |
| 190 |
|
| 191 |
DROP TABLE test1; |
| 192 |
DROP TABLE test2; |