1 CREATE TABLE test1 (a int, b text);
2 CREATE PROCEDURE transaction_test1()
6 spi_exec_query("INSERT INTO test1 (a) VALUES ($i)");
14 CALL transaction_test1();
29 foreach my $i (0..9) {
30 spi_exec_query("INSERT INTO test1 (a) VALUES ($i)");
49 -- not allowed in a function
50 CREATE FUNCTION transaction_test2() RETURNS int
53 foreach my $i (0..9) {
54 spi_exec_query("INSERT INTO test1 (a) VALUES ($i)");
63 SELECT transaction_test2();
64 ERROR: invalid transaction termination at line 5.
65 CONTEXT: PL/Perl function "transaction_test2"
71 -- also not allowed if procedure is called from a function
72 CREATE FUNCTION transaction_test3() RETURNS int
75 spi_exec_query("CALL transaction_test1()");
78 SELECT transaction_test3();
79 ERROR: invalid transaction termination at line 5. at line 2.
80 CONTEXT: PL/Perl function "transaction_test3"
86 -- DO block inside function
87 CREATE FUNCTION transaction_test4() RETURNS int
90 spi_exec_query('DO LANGUAGE plperl $x$ spi_commit(); $x$');
93 SELECT transaction_test4();
94 ERROR: invalid transaction termination at line 1. at line 2.
95 CONTEXT: PL/Perl function "transaction_test4"
96 -- commit inside cursor loop
97 CREATE TABLE test2 (x int);
98 INSERT INTO test2 VALUES (0), (1), (2), (3), (4);
100 DO LANGUAGE plperl $$
101 my $sth = spi_query("SELECT * FROM test2 ORDER BY x");
103 while (defined($row = spi_fetchrow($sth))) {
104 spi_exec_query("INSERT INTO test1 (a) VALUES (" . $row->{x} . ")");
118 -- check that this doesn't leak a holdable portal
119 SELECT * FROM pg_cursors;
120 name | statement | is_holdable | is_binary | is_scrollable | creation_time
121 ------+-----------+-------------+-----------+---------------+---------------
124 -- error in cursor loop with commit
126 DO LANGUAGE plperl $$
127 my $sth = spi_query("SELECT * FROM test2 ORDER BY x");
129 while (defined($row = spi_fetchrow($sth))) {
130 spi_exec_query("INSERT INTO test1 (a) VALUES (12/(" . $row->{x} . "-2))");
134 ERROR: division by zero at line 5.
135 CONTEXT: PL/Perl anonymous code block
143 SELECT * FROM pg_cursors;
144 name | statement | is_holdable | is_binary | is_scrollable | creation_time
145 ------+-----------+-------------+-----------+---------------+---------------
148 -- rollback inside cursor loop
150 DO LANGUAGE plperl $$
151 my $sth = spi_query("SELECT * FROM test2 ORDER BY x");
153 while (defined($row = spi_fetchrow($sth))) {
154 spi_exec_query("INSERT INTO test1 (a) VALUES (" . $row->{x} . ")");
163 SELECT * FROM pg_cursors;
164 name | statement | is_holdable | is_binary | is_scrollable | creation_time
165 ------+-----------+-------------+-----------+---------------+---------------
168 -- first commit then rollback inside cursor loop
170 DO LANGUAGE plperl $$
171 my $sth = spi_query("SELECT * FROM test2 ORDER BY x");
173 while (defined($row = spi_fetchrow($sth))) {
174 spi_exec_query("INSERT INTO test1 (a) VALUES (" . $row->{x} . ")");
175 if ($row->{x} % 2 == 0) {
190 SELECT * FROM pg_cursors;
191 name | statement | is_holdable | is_binary | is_scrollable | creation_time
192 ------+-----------+-------------+-----------+---------------+---------------