Debian Codegraph @

src/pl/plperl/expected/plperl_transaction.out

raw
1 CREATE TABLE test1 (a int, b text);
2 CREATE PROCEDURE transaction_test1()
3 LANGUAGE plperl
4 AS $$
5 foreach my $i (0..9) {
6 spi_exec_query("INSERT INTO test1 (a) VALUES ($i)");
7 if ($i % 2 == 0) {
8 spi_commit();
9 } else {
10 spi_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 DO
27 LANGUAGE plperl
28 $$
29 foreach my $i (0..9) {
30 spi_exec_query("INSERT INTO test1 (a) VALUES ($i)");
31 if ($i % 2 == 0) {
32 spi_commit();
33 } else {
34 spi_rollback();
35 }
36 }
37 $$;
38 SELECT * FROM test1;
39 a | b
40 ---+---
41 0 |
42 2 |
43 4 |
44 6 |
45 8 |
46 (5 rows)
47
48 TRUNCATE test1;
49 -- not allowed in a function
50 CREATE FUNCTION transaction_test2() RETURNS int
51 LANGUAGE plperl
52 AS $$
53 foreach my $i (0..9) {
54 spi_exec_query("INSERT INTO test1 (a) VALUES ($i)");
55 if ($i % 2 == 0) {
56 spi_commit();
57 } else {
58 spi_rollback();
59 }
60 }
61 return 1;
62 $$;
63 SELECT transaction_test2();
64 ERROR: invalid transaction termination at line 5.
65 CONTEXT: PL/Perl function "transaction_test2"
66 SELECT * FROM test1;
67 a | b
68 ---+---
69 (0 rows)
70
71 -- also not allowed if procedure is called from a function
72 CREATE FUNCTION transaction_test3() RETURNS int
73 LANGUAGE plperl
74 AS $$
75 spi_exec_query("CALL transaction_test1()");
76 return 1;
77 $$;
78 SELECT transaction_test3();
79 ERROR: invalid transaction termination at line 5. at line 2.
80 CONTEXT: PL/Perl function "transaction_test3"
81 SELECT * FROM test1;
82 a | b
83 ---+---
84 (0 rows)
85
86 -- DO block inside function
87 CREATE FUNCTION transaction_test4() RETURNS int
88 LANGUAGE plperl
89 AS $$
90 spi_exec_query('DO LANGUAGE plperl $x$ spi_commit(); $x$');
91 return 1;
92 $$;
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);
99 TRUNCATE test1;
100 DO LANGUAGE plperl $$
101 my $sth = spi_query("SELECT * FROM test2 ORDER BY x");
102 my $row;
103 while (defined($row = spi_fetchrow($sth))) {
104 spi_exec_query("INSERT INTO test1 (a) VALUES (" . $row->{x} . ")");
105 spi_commit();
106 }
107 $$;
108 SELECT * FROM test1;
109 a | b
110 ---+---
111 0 |
112 1 |
113 2 |
114 3 |
115 4 |
116 (5 rows)
117
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 ------+-----------+-------------+-----------+---------------+---------------
122 (0 rows)
123
124 -- error in cursor loop with commit
125 TRUNCATE test1;
126 DO LANGUAGE plperl $$
127 my $sth = spi_query("SELECT * FROM test2 ORDER BY x");
128 my $row;
129 while (defined($row = spi_fetchrow($sth))) {
130 spi_exec_query("INSERT INTO test1 (a) VALUES (12/(" . $row->{x} . "-2))");
131 spi_commit();
132 }
133 $$;
134 ERROR: division by zero at line 5.
135 CONTEXT: PL/Perl anonymous code block
136 SELECT * FROM test1;
137 a | b
138 -----+---
139 -6 |
140 -12 |
141 (2 rows)
142
143 SELECT * FROM pg_cursors;
144 name | statement | is_holdable | is_binary | is_scrollable | creation_time
145 ------+-----------+-------------+-----------+---------------+---------------
146 (0 rows)
147
148 -- rollback inside cursor loop
149 TRUNCATE test1;
150 DO LANGUAGE plperl $$
151 my $sth = spi_query("SELECT * FROM test2 ORDER BY x");
152 my $row;
153 while (defined($row = spi_fetchrow($sth))) {
154 spi_exec_query("INSERT INTO test1 (a) VALUES (" . $row->{x} . ")");
155 spi_rollback();
156 }
157 $$;
158 SELECT * FROM test1;
159 a | b
160 ---+---
161 (0 rows)
162
163 SELECT * FROM pg_cursors;
164 name | statement | is_holdable | is_binary | is_scrollable | creation_time
165 ------+-----------+-------------+-----------+---------------+---------------
166 (0 rows)
167
168 -- first commit then rollback inside cursor loop
169 TRUNCATE test1;
170 DO LANGUAGE plperl $$
171 my $sth = spi_query("SELECT * FROM test2 ORDER BY x");
172 my $row;
173 while (defined($row = spi_fetchrow($sth))) {
174 spi_exec_query("INSERT INTO test1 (a) VALUES (" . $row->{x} . ")");
175 if ($row->{x} % 2 == 0) {
176 spi_commit();
177 } else {
178 spi_rollback();
179 }
180 }
181 $$;
182 SELECT * FROM test1;
183 a | b
184 ---+---
185 0 |
186 2 |
187 4 |
188 (3 rows)
189
190 SELECT * FROM pg_cursors;
191 name | statement | is_holdable | is_binary | is_scrollable | creation_time
192 ------+-----------+-------------+-----------+---------------+---------------
193 (0 rows)
194
195 -- check handling of an error during COMMIT
196 CREATE TABLE testpk (id int PRIMARY KEY);
197 CREATE TABLE testfk(f1 int REFERENCES testpk DEFERRABLE INITIALLY DEFERRED);
198 DO LANGUAGE plperl $$
199 # this insert will fail during commit:
200 spi_exec_query("INSERT INTO testfk VALUES (0)");
201 spi_commit();
202 elog(WARNING, 'should not get here');
203 $$;
204 ERROR: insert or update on table "testfk" violates foreign key constraint "testfk_f1_fkey" at line 4.
205 CONTEXT: PL/Perl anonymous code block
206 SELECT * FROM testpk;
207 id
208 ----
209 (0 rows)
210
211 SELECT * FROM testfk;
212 f1
213 ----
214 (0 rows)
215
216 DO LANGUAGE plperl $$
217 # this insert will fail during commit:
218 spi_exec_query("INSERT INTO testfk VALUES (0)");
219 eval {
220 spi_commit();
221 };
222 if ($@) {
223 elog(INFO, $@);
224 }
225 # these inserts should work:
226 spi_exec_query("INSERT INTO testpk VALUES (1)");
227 spi_exec_query("INSERT INTO testfk VALUES (1)");
228 $$;
229 INFO: insert or update on table "testfk" violates foreign key constraint "testfk_f1_fkey" at line 5.
230
231 SELECT * FROM testpk;
232 id
233 ----
234 1
235 (1 row)
236
237 SELECT * FROM testfk;
238 f1
239 ----
240 1
241 (1 row)
242
243 DROP TABLE test1;
244 DROP TABLE test2;