1 CREATE TABLE test1 (a int, b text);
4 CREATE PROCEDURE transaction_test1()
8 spi_exec_query("INSERT INTO test1 (a) VALUES ($i)");
17 CALL transaction_test1();
27 foreach my $i (0..9) {
28 spi_exec_query("INSERT INTO test1 (a) VALUES ($i)");
42 -- not allowed in a function
43 CREATE FUNCTION transaction_test2() RETURNS int
46 foreach my $i (0..9) {
47 spi_exec_query("INSERT INTO test1 (a) VALUES ($i)");
57 SELECT transaction_test2();
62 -- also not allowed if procedure is called from a function
63 CREATE FUNCTION transaction_test3() RETURNS int
66 spi_exec_query("CALL transaction_test1()");
70 SELECT transaction_test3();
75 -- DO block inside function
76 CREATE FUNCTION transaction_test4() RETURNS int
79 spi_exec_query('DO LANGUAGE plperl $x$ spi_commit(); $x$');
83 SELECT transaction_test4();
86 -- commit inside cursor loop
87 CREATE TABLE test2 (x int);
88 INSERT INTO test2 VALUES (0), (1), (2), (3), (4);
93 my $sth = spi_query("SELECT * FROM test2 ORDER BY x");
95 while (defined($row = spi_fetchrow($sth))) {
96 spi_exec_query("INSERT INTO test1 (a) VALUES (" . $row->{x} . ")");
103 -- check that this doesn't leak a holdable portal
104 SELECT * FROM pg_cursors;
107 -- error in cursor loop with commit
110 DO LANGUAGE plperl $$
111 my $sth = spi_query("SELECT * FROM test2 ORDER BY x");
113 while (defined($row = spi_fetchrow($sth))) {
114 spi_exec_query("INSERT INTO test1 (a) VALUES (12/(" . $row->{x} . "-2))");
121 SELECT * FROM pg_cursors;
124 -- rollback inside cursor loop
127 DO LANGUAGE plperl $$
128 my $sth = spi_query("SELECT * FROM test2 ORDER BY x");
130 while (defined($row = spi_fetchrow($sth))) {
131 spi_exec_query("INSERT INTO test1 (a) VALUES (" . $row->{x} . ")");
138 SELECT * FROM pg_cursors;
141 -- first commit then rollback inside cursor loop
144 DO LANGUAGE plperl $$
145 my $sth = spi_query("SELECT * FROM test2 ORDER BY x");
147 while (defined($row = spi_fetchrow($sth))) {
148 spi_exec_query("INSERT INTO test1 (a) VALUES (" . $row->{x} . ")");
149 if ($row->{x} % 2 == 0) {
159 SELECT * FROM pg_cursors;
162 -- check handling of an error during COMMIT
163 CREATE TABLE testpk (id int PRIMARY KEY);
164 CREATE TABLE testfk(f1 int REFERENCES testpk DEFERRABLE INITIALLY DEFERRED);
166 DO LANGUAGE plperl $$
167 # this insert will fail during commit:
168 spi_exec_query("INSERT INTO testfk VALUES (0)");
170 elog(WARNING, 'should not get here');
173 SELECT * FROM testpk;
174 SELECT * FROM testfk;
176 DO LANGUAGE plperl $$
177 # this insert will fail during commit:
178 spi_exec_query("INSERT INTO testfk VALUES (0)");
185 # these inserts should work:
186 spi_exec_query("INSERT INTO testpk VALUES (1)");
187 spi_exec_query("INSERT INTO testfk VALUES (1)");
190 SELECT * FROM testpk;
191 SELECT * FROM testfk;