summaryrefslogtreecommitdiff
path: root/src/pl/plpython/expected/plpython_subtransaction.out
blob: 50d97faa78644e84941d60d0eb6950b296351097 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
--
-- Test explicit subtransactions
--
-- Test table to see if transactions get properly rolled back
CREATE TABLE subtransaction_tbl (
    i integer
);
-- Explicit case for Python <2.6
CREATE FUNCTION subtransaction_test(what_error text = NULL) RETURNS text
AS $$
import sys
subxact = plpy.subtransaction()
subxact.__enter__()
exc = True
try:
    try:
        plpy.execute("INSERT INTO subtransaction_tbl VALUES (1)")
        plpy.execute("INSERT INTO subtransaction_tbl VALUES (2)")
        if what_error == "SPI":
            plpy.execute("INSERT INTO subtransaction_tbl VALUES ('oops')")
        elif what_error == "Python":
            plpy.attribute_error
    except:
        exc = False
        subxact.__exit__(*sys.exc_info())
        raise
finally:
    if exc:
        subxact.__exit__(None, None, None)
$$ LANGUAGE plpythonu;
SELECT subtransaction_test();
 subtransaction_test 
---------------------
 
(1 row)

SELECT * FROM subtransaction_tbl;
 i 
---
 1
 2
(2 rows)

TRUNCATE subtransaction_tbl;
SELECT subtransaction_test('SPI');
ERROR:  spiexceptions.InvalidTextRepresentation: invalid input syntax for integer: "oops"
LINE 1: INSERT INTO subtransaction_tbl VALUES ('oops')
                                               ^
QUERY:  INSERT INTO subtransaction_tbl VALUES ('oops')
CONTEXT:  PL/Python function "subtransaction_test"
SELECT * FROM subtransaction_tbl;
 i 
---
(0 rows)

TRUNCATE subtransaction_tbl;
SELECT subtransaction_test('Python');
ERROR:  AttributeError: 'module' object has no attribute 'attribute_error'
CONTEXT:  PL/Python function "subtransaction_test"
SELECT * FROM subtransaction_tbl;
 i 
---
(0 rows)

TRUNCATE subtransaction_tbl;
-- Context manager case for Python >=2.6
CREATE FUNCTION subtransaction_ctx_test(what_error text = NULL) RETURNS text
AS $$
with plpy.subtransaction():
    plpy.execute("INSERT INTO subtransaction_tbl VALUES (1)")
    plpy.execute("INSERT INTO subtransaction_tbl VALUES (2)")
    if what_error == "SPI":
        plpy.execute("INSERT INTO subtransaction_tbl VALUES ('oops')")
    elif what_error == "Python":
        plpy.attribute_error
$$ LANGUAGE plpythonu;
SELECT subtransaction_ctx_test();
 subtransaction_ctx_test 
-------------------------
 
(1 row)

SELECT * FROM subtransaction_tbl;
 i 
---
 1
 2
(2 rows)

TRUNCATE subtransaction_tbl;
SELECT subtransaction_ctx_test('SPI');
ERROR:  spiexceptions.InvalidTextRepresentation: invalid input syntax for integer: "oops"
LINE 1: INSERT INTO subtransaction_tbl VALUES ('oops')
                                               ^
QUERY:  INSERT INTO subtransaction_tbl VALUES ('oops')
CONTEXT:  PL/Python function "subtransaction_ctx_test"
SELECT * FROM subtransaction_tbl;
 i 
---
(0 rows)

TRUNCATE subtransaction_tbl;
SELECT subtransaction_ctx_test('Python');
ERROR:  AttributeError: 'module' object has no attribute 'attribute_error'
CONTEXT:  PL/Python function "subtransaction_ctx_test"
SELECT * FROM subtransaction_tbl;
 i 
---
(0 rows)

TRUNCATE subtransaction_tbl;
-- Nested subtransactions
CREATE FUNCTION subtransaction_nested_test(swallow boolean = 'f') RETURNS text
AS $$
plpy.execute("INSERT INTO subtransaction_tbl VALUES (1)")
with plpy.subtransaction():
    plpy.execute("INSERT INTO subtransaction_tbl VALUES (2)")
    try:
        with plpy.subtransaction():
            plpy.execute("INSERT INTO subtransaction_tbl VALUES (3)")
            plpy.execute("error")
    except plpy.SPIError, e:
        if not swallow:
            raise
        plpy.notice("Swallowed %r" % e)
return "ok"
$$ LANGUAGE plpythonu;
SELECT subtransaction_nested_test();
ERROR:  spiexceptions.SyntaxError: syntax error at or near "error"
LINE 1: error
        ^
QUERY:  error
CONTEXT:  PL/Python function "subtransaction_nested_test"
SELECT * FROM subtransaction_tbl;
 i 
---
(0 rows)

TRUNCATE subtransaction_tbl;
SELECT subtransaction_nested_test('t');
NOTICE:  Swallowed SyntaxError('syntax error at or near "error"',)
CONTEXT:  PL/Python function "subtransaction_nested_test"
 subtransaction_nested_test 
----------------------------
 ok
(1 row)

SELECT * FROM subtransaction_tbl;
 i 
---
 1
 2
(2 rows)

TRUNCATE subtransaction_tbl;
-- Nested subtransactions that recursively call code dealing with
-- subtransactions
CREATE FUNCTION subtransaction_deeply_nested_test() RETURNS text
AS $$
plpy.execute("INSERT INTO subtransaction_tbl VALUES (1)")
with plpy.subtransaction():
    plpy.execute("INSERT INTO subtransaction_tbl VALUES (2)")
    plpy.execute("SELECT subtransaction_nested_test('t')")
return "ok"
$$ LANGUAGE plpythonu;
SELECT subtransaction_deeply_nested_test();
NOTICE:  Swallowed SyntaxError('syntax error at or near "error"',)
CONTEXT:  PL/Python function "subtransaction_nested_test"
SQL statement "SELECT subtransaction_nested_test('t')"
PL/Python function "subtransaction_nested_test"
 subtransaction_deeply_nested_test 
-----------------------------------
 ok
(1 row)

SELECT * FROM subtransaction_tbl;
 i 
---
 1
 2
 1
 2
(4 rows)

TRUNCATE subtransaction_tbl;
-- Error conditions from not opening/closing subtransactions
CREATE FUNCTION subtransaction_exit_without_enter() RETURNS void
AS $$
plpy.subtransaction().__exit__(None, None, None)
$$ LANGUAGE plpythonu;
CREATE FUNCTION subtransaction_enter_without_exit() RETURNS void
AS $$
plpy.subtransaction().__enter__()
$$ LANGUAGE plpythonu;
CREATE FUNCTION subtransaction_exit_twice() RETURNS void
AS $$
plpy.subtransaction().__enter__()
plpy.subtransaction().__exit__(None, None, None)
plpy.subtransaction().__exit__(None, None, None)
$$ LANGUAGE plpythonu;
CREATE FUNCTION subtransaction_enter_twice() RETURNS void
AS $$
plpy.subtransaction().__enter__()
plpy.subtransaction().__enter__()
$$ LANGUAGE plpythonu;
CREATE FUNCTION subtransaction_exit_same_subtransaction_twice() RETURNS void
AS $$
s = plpy.subtransaction()
s.__enter__()
s.__exit__(None, None, None)
s.__exit__(None, None, None)
$$ LANGUAGE plpythonu;
CREATE FUNCTION subtransaction_enter_same_subtransaction_twice() RETURNS void
AS $$
s = plpy.subtransaction()
s.__enter__()
s.__enter__()
s.__exit__(None, None, None)
$$ LANGUAGE plpythonu;
-- No warnings here, as the subtransaction gets indeed closed
CREATE FUNCTION subtransaction_enter_subtransaction_in_with() RETURNS void
AS $$
with plpy.subtransaction() as s:
    s.__enter__()
$$ LANGUAGE plpythonu;
CREATE FUNCTION subtransaction_exit_subtransaction_in_with() RETURNS void
AS $$
with plpy.subtransaction() as s:
    s.__exit__(None, None, None)
$$ LANGUAGE plpythonu;
SELECT subtransaction_exit_without_enter();
ERROR:  ValueError: this subtransaction has not been entered
CONTEXT:  PL/Python function "subtransaction_exit_without_enter"
SELECT subtransaction_enter_without_exit();
WARNING:  forcibly aborting a subtransaction that has not been exited
CONTEXT:  PL/Python function "subtransaction_enter_without_exit"
 subtransaction_enter_without_exit 
-----------------------------------
 
(1 row)

SELECT subtransaction_exit_twice();
WARNING:  forcibly aborting a subtransaction that has not been exited
CONTEXT:  PL/Python function "subtransaction_exit_twice"
ERROR:  ValueError: this subtransaction has not been entered
CONTEXT:  PL/Python function "subtransaction_exit_twice"
SELECT subtransaction_enter_twice();
WARNING:  forcibly aborting a subtransaction that has not been exited
CONTEXT:  PL/Python function "subtransaction_enter_twice"
WARNING:  forcibly aborting a subtransaction that has not been exited
CONTEXT:  PL/Python function "subtransaction_enter_twice"
 subtransaction_enter_twice 
----------------------------
 
(1 row)

SELECT subtransaction_exit_same_subtransaction_twice();
ERROR:  ValueError: this subtransaction has already been exited
CONTEXT:  PL/Python function "subtransaction_exit_same_subtransaction_twice"
SELECT subtransaction_enter_same_subtransaction_twice();
WARNING:  forcibly aborting a subtransaction that has not been exited
CONTEXT:  PL/Python function "subtransaction_enter_same_subtransaction_twice"
ERROR:  ValueError: this subtransaction has already been entered
CONTEXT:  PL/Python function "subtransaction_enter_same_subtransaction_twice"
SELECT subtransaction_enter_subtransaction_in_with();
ERROR:  ValueError: this subtransaction has already been entered
CONTEXT:  PL/Python function "subtransaction_enter_subtransaction_in_with"
SELECT subtransaction_exit_subtransaction_in_with();
ERROR:  ValueError: this subtransaction has already been exited
CONTEXT:  PL/Python function "subtransaction_exit_subtransaction_in_with"
-- Make sure we don't get a "current transaction is aborted" error
SELECT 1 as test;
 test 
------
    1
(1 row)

-- Mix explicit subtransactions and normal SPI calls
CREATE FUNCTION subtransaction_mix_explicit_and_implicit() RETURNS void
AS $$
p = plpy.prepare("INSERT INTO subtransaction_tbl VALUES ($1)", ["integer"])
try:
    with plpy.subtransaction():
        plpy.execute("INSERT INTO subtransaction_tbl VALUES (1)")
        plpy.execute(p, [2])
        plpy.execute(p, ["wrong"])
except plpy.SPIError:
    plpy.warning("Caught a SPI error from an explicit subtransaction")

try:
    plpy.execute("INSERT INTO subtransaction_tbl VALUES (1)")
    plpy.execute(p, [2])
    plpy.execute(p, ["wrong"])
except plpy.SPIError:
    plpy.warning("Caught a SPI error")
$$ LANGUAGE plpythonu;
SELECT subtransaction_mix_explicit_and_implicit();
WARNING:  Caught a SPI error from an explicit subtransaction
CONTEXT:  PL/Python function "subtransaction_mix_explicit_and_implicit"
WARNING:  Caught a SPI error
CONTEXT:  PL/Python function "subtransaction_mix_explicit_and_implicit"
 subtransaction_mix_explicit_and_implicit 
------------------------------------------
 
(1 row)

SELECT * FROM subtransaction_tbl;
 i 
---
 1
 2
(2 rows)

TRUNCATE subtransaction_tbl;
-- Alternative method names for Python <2.6
CREATE FUNCTION subtransaction_alternative_names() RETURNS void
AS $$
s = plpy.subtransaction()
s.enter()
s.exit(None, None, None)
$$ LANGUAGE plpythonu;
SELECT subtransaction_alternative_names();
 subtransaction_alternative_names 
----------------------------------
 
(1 row)

-- try/catch inside a subtransaction block
CREATE FUNCTION try_catch_inside_subtransaction() RETURNS void
AS $$
with plpy.subtransaction():
     plpy.execute("INSERT INTO subtransaction_tbl VALUES (1)")
     try:
         plpy.execute("INSERT INTO subtransaction_tbl VALUES ('a')")
     except plpy.SPIError:
         plpy.notice("caught")
$$ LANGUAGE plpythonu;
SELECT try_catch_inside_subtransaction();
NOTICE:  caught
CONTEXT:  PL/Python function "try_catch_inside_subtransaction"
 try_catch_inside_subtransaction 
---------------------------------
 
(1 row)

SELECT * FROM subtransaction_tbl;
 i 
---
 1
(1 row)

TRUNCATE subtransaction_tbl;
ALTER TABLE subtransaction_tbl ADD PRIMARY KEY (i);
NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "subtransaction_tbl_pkey" for table "subtransaction_tbl"
CREATE FUNCTION pk_violation_inside_subtransaction() RETURNS void
AS $$
with plpy.subtransaction():
     plpy.execute("INSERT INTO subtransaction_tbl VALUES (1)")
     try:
         plpy.execute("INSERT INTO subtransaction_tbl VALUES (1)")
     except plpy.SPIError:
         plpy.notice("caught")
$$ LANGUAGE plpythonu;
SELECT pk_violation_inside_subtransaction();
NOTICE:  caught
CONTEXT:  PL/Python function "pk_violation_inside_subtransaction"
 pk_violation_inside_subtransaction 
------------------------------------
 
(1 row)

SELECT * FROM subtransaction_tbl;
 i 
---
 1
(1 row)

DROP TABLE subtransaction_tbl;