|
| 1 | +-- |
| 2 | +-- Test explicit subtransactions |
| 3 | +-- |
| 4 | +-- Test table to see if transactions get properly rolled back |
| 5 | +CREATE TABLE subtransaction_tbl ( |
| 6 | + i integer |
| 7 | +); |
| 8 | +-- Explicit case for Python <2.6 |
| 9 | +CREATE FUNCTION subtransaction_test(what_error text = NULL) RETURNS text |
| 10 | +AS $$ |
| 11 | +import sys |
| 12 | +subxact = plpy.subtransaction() |
| 13 | +subxact.__enter__() |
| 14 | +exc = True |
| 15 | +try: |
| 16 | + try: |
| 17 | + plpy.execute("INSERT INTO subtransaction_tbl VALUES (1)") |
| 18 | + plpy.execute("INSERT INTO subtransaction_tbl VALUES (2)") |
| 19 | + if what_error == "SPI": |
| 20 | + plpy.execute("INSERT INTO subtransaction_tbl VALUES ('oops')") |
| 21 | + elif what_error == "Python": |
| 22 | + plpy.attribute_error |
| 23 | + except: |
| 24 | + exc = False |
| 25 | + subxact.__exit__(*sys.exc_info()) |
| 26 | + raise |
| 27 | +finally: |
| 28 | + if exc: |
| 29 | + subxact.__exit__(None, None, None) |
| 30 | +$$ LANGUAGE plpythonu; |
| 31 | +SELECT subtransaction_test(); |
| 32 | + subtransaction_test |
| 33 | +--------------------- |
| 34 | + |
| 35 | +(1 row) |
| 36 | + |
| 37 | +SELECT * FROM subtransaction_tbl; |
| 38 | + i |
| 39 | +--- |
| 40 | + 1 |
| 41 | + 2 |
| 42 | +(2 rows) |
| 43 | + |
| 44 | +TRUNCATE subtransaction_tbl; |
| 45 | +SELECT subtransaction_test('SPI'); |
| 46 | +ERROR: spiexceptions.InvalidTextRepresentation: invalid input syntax for integer: "oops" |
| 47 | +LINE 1: INSERT INTO subtransaction_tbl VALUES ('oops') |
| 48 | + ^ |
| 49 | +QUERY: INSERT INTO subtransaction_tbl VALUES ('oops') |
| 50 | +CONTEXT: PL/Python function "subtransaction_test" |
| 51 | +SELECT * FROM subtransaction_tbl; |
| 52 | + i |
| 53 | +--- |
| 54 | +(0 rows) |
| 55 | + |
| 56 | +TRUNCATE subtransaction_tbl; |
| 57 | +SELECT subtransaction_test('Python'); |
| 58 | +ERROR: AttributeError: 'module' object has no attribute 'attribute_error' |
| 59 | +CONTEXT: PL/Python function "subtransaction_test" |
| 60 | +SELECT * FROM subtransaction_tbl; |
| 61 | + i |
| 62 | +--- |
| 63 | +(0 rows) |
| 64 | + |
| 65 | +TRUNCATE subtransaction_tbl; |
| 66 | +-- Context manager case for Python >=2.6 |
| 67 | +CREATE FUNCTION subtransaction_ctx_test(what_error text = NULL) RETURNS text |
| 68 | +AS $$ |
| 69 | +with plpy.subtransaction(): |
| 70 | + plpy.execute("INSERT INTO subtransaction_tbl VALUES (1)") |
| 71 | + plpy.execute("INSERT INTO subtransaction_tbl VALUES (2)") |
| 72 | + if what_error == "SPI": |
| 73 | + plpy.execute("INSERT INTO subtransaction_tbl VALUES ('oops')") |
| 74 | + elif what_error == "Python": |
| 75 | + plpy.attribute_error |
| 76 | +$$ LANGUAGE plpythonu; |
| 77 | +ERROR: could not compile PL/Python function "subtransaction_ctx_test" |
| 78 | +DETAIL: SyntaxError: invalid syntax (<string>, line 3) |
| 79 | +SELECT subtransaction_ctx_test(); |
| 80 | +ERROR: function subtransaction_ctx_test() does not exist |
| 81 | +LINE 1: SELECT subtransaction_ctx_test(); |
| 82 | + ^ |
| 83 | +HINT: No function matches the given name and argument types. You might need to add explicit type casts. |
| 84 | +SELECT * FROM subtransaction_tbl; |
| 85 | + i |
| 86 | +--- |
| 87 | +(0 rows) |
| 88 | + |
| 89 | +TRUNCATE subtransaction_tbl; |
| 90 | +SELECT subtransaction_ctx_test('SPI'); |
| 91 | +ERROR: function subtransaction_ctx_test(unknown) does not exist |
| 92 | +LINE 1: SELECT subtransaction_ctx_test('SPI'); |
| 93 | + ^ |
| 94 | +HINT: No function matches the given name and argument types. You might need to add explicit type casts. |
| 95 | +SELECT * FROM subtransaction_tbl; |
| 96 | + i |
| 97 | +--- |
| 98 | +(0 rows) |
| 99 | + |
| 100 | +TRUNCATE subtransaction_tbl; |
| 101 | +SELECT subtransaction_ctx_test('Python'); |
| 102 | +ERROR: function subtransaction_ctx_test(unknown) does not exist |
| 103 | +LINE 1: SELECT subtransaction_ctx_test('Python'); |
| 104 | + ^ |
| 105 | +HINT: No function matches the given name and argument types. You might need to add explicit type casts. |
| 106 | +SELECT * FROM subtransaction_tbl; |
| 107 | + i |
| 108 | +--- |
| 109 | +(0 rows) |
| 110 | + |
| 111 | +TRUNCATE subtransaction_tbl; |
| 112 | +-- Nested subtransactions |
| 113 | +CREATE FUNCTION subtransaction_nested_test(swallow boolean = 'f') RETURNS text |
| 114 | +AS $$ |
| 115 | +plpy.execute("INSERT INTO subtransaction_tbl VALUES (1)") |
| 116 | +with plpy.subtransaction(): |
| 117 | + plpy.execute("INSERT INTO subtransaction_tbl VALUES (2)") |
| 118 | + try: |
| 119 | + with plpy.subtransaction(): |
| 120 | + plpy.execute("INSERT INTO subtransaction_tbl VALUES (3)") |
| 121 | + plpy.execute("error") |
| 122 | + except plpy.SPIError, e: |
| 123 | + if not swallow: |
| 124 | + raise |
| 125 | + plpy.notice("Swallowed %r" % e) |
| 126 | +return "ok" |
| 127 | +$$ LANGUAGE plpythonu; |
| 128 | +ERROR: could not compile PL/Python function "subtransaction_nested_test" |
| 129 | +DETAIL: SyntaxError: invalid syntax (<string>, line 4) |
| 130 | +SELECT subtransaction_nested_test(); |
| 131 | +ERROR: function subtransaction_nested_test() does not exist |
| 132 | +LINE 1: SELECT subtransaction_nested_test(); |
| 133 | + ^ |
| 134 | +HINT: No function matches the given name and argument types. You might need to add explicit type casts. |
| 135 | +SELECT * FROM subtransaction_tbl; |
| 136 | + i |
| 137 | +--- |
| 138 | +(0 rows) |
| 139 | + |
| 140 | +TRUNCATE subtransaction_tbl; |
| 141 | +SELECT subtransaction_nested_test('t'); |
| 142 | +ERROR: function subtransaction_nested_test(unknown) does not exist |
| 143 | +LINE 1: SELECT subtransaction_nested_test('t'); |
| 144 | + ^ |
| 145 | +HINT: No function matches the given name and argument types. You might need to add explicit type casts. |
| 146 | +SELECT * FROM subtransaction_tbl; |
| 147 | + i |
| 148 | +--- |
| 149 | +(0 rows) |
| 150 | + |
| 151 | +TRUNCATE subtransaction_tbl; |
| 152 | +-- Nested subtransactions that recursively call code dealing with |
| 153 | +-- subtransactions |
| 154 | +CREATE FUNCTION subtransaction_deeply_nested_test() RETURNS text |
| 155 | +AS $$ |
| 156 | +plpy.execute("INSERT INTO subtransaction_tbl VALUES (1)") |
| 157 | +with plpy.subtransaction(): |
| 158 | + plpy.execute("INSERT INTO subtransaction_tbl VALUES (2)") |
| 159 | + plpy.execute("SELECT subtransaction_nested_test('t')") |
| 160 | +return "ok" |
| 161 | +$$ LANGUAGE plpythonu; |
| 162 | +ERROR: could not compile PL/Python function "subtransaction_deeply_nested_test" |
| 163 | +DETAIL: SyntaxError: invalid syntax (<string>, line 4) |
| 164 | +SELECT subtransaction_deeply_nested_test(); |
| 165 | +ERROR: function subtransaction_deeply_nested_test() does not exist |
| 166 | +LINE 1: SELECT subtransaction_deeply_nested_test(); |
| 167 | + ^ |
| 168 | +HINT: No function matches the given name and argument types. You might need to add explicit type casts. |
| 169 | +SELECT * FROM subtransaction_tbl; |
| 170 | + i |
| 171 | +--- |
| 172 | +(0 rows) |
| 173 | + |
| 174 | +TRUNCATE subtransaction_tbl; |
| 175 | +-- Error conditions from not opening/closing subtransactions |
| 176 | +CREATE FUNCTION subtransaction_exit_without_enter() RETURNS void |
| 177 | +AS $$ |
| 178 | +plpy.subtransaction().__exit__(None, None, None) |
| 179 | +$$ LANGUAGE plpythonu; |
| 180 | +CREATE FUNCTION subtransaction_enter_without_exit() RETURNS void |
| 181 | +AS $$ |
| 182 | +plpy.subtransaction().__enter__() |
| 183 | +$$ LANGUAGE plpythonu; |
| 184 | +CREATE FUNCTION subtransaction_exit_twice() RETURNS void |
| 185 | +AS $$ |
| 186 | +plpy.subtransaction().__enter__() |
| 187 | +plpy.subtransaction().__exit__(None, None, None) |
| 188 | +plpy.subtransaction().__exit__(None, None, None) |
| 189 | +$$ LANGUAGE plpythonu; |
| 190 | +CREATE FUNCTION subtransaction_enter_twice() RETURNS void |
| 191 | +AS $$ |
| 192 | +plpy.subtransaction().__enter__() |
| 193 | +plpy.subtransaction().__enter__() |
| 194 | +$$ LANGUAGE plpythonu; |
| 195 | +CREATE FUNCTION subtransaction_exit_same_subtransaction_twice() RETURNS void |
| 196 | +AS $$ |
| 197 | +s = plpy.subtransaction() |
| 198 | +s.__enter__() |
| 199 | +s.__exit__(None, None, None) |
| 200 | +s.__exit__(None, None, None) |
| 201 | +$$ LANGUAGE plpythonu; |
| 202 | +CREATE FUNCTION subtransaction_enter_same_subtransaction_twice() RETURNS void |
| 203 | +AS $$ |
| 204 | +s = plpy.subtransaction() |
| 205 | +s.__enter__() |
| 206 | +s.__enter__() |
| 207 | +s.__exit__(None, None, None) |
| 208 | +$$ LANGUAGE plpythonu; |
| 209 | +-- No warnings here, as the subtransaction gets indeed closed |
| 210 | +CREATE FUNCTION subtransaction_enter_subtransaction_in_with() RETURNS void |
| 211 | +AS $$ |
| 212 | +with plpy.subtransaction() as s: |
| 213 | + s.__enter__() |
| 214 | +$$ LANGUAGE plpythonu; |
| 215 | +ERROR: could not compile PL/Python function "subtransaction_enter_subtransaction_in_with" |
| 216 | +DETAIL: SyntaxError: invalid syntax (<string>, line 3) |
| 217 | +CREATE FUNCTION subtransaction_exit_subtransaction_in_with() RETURNS void |
| 218 | +AS $$ |
| 219 | +with plpy.subtransaction() as s: |
| 220 | + s.__exit__(None, None, None) |
| 221 | +$$ LANGUAGE plpythonu; |
| 222 | +ERROR: could not compile PL/Python function "subtransaction_exit_subtransaction_in_with" |
| 223 | +DETAIL: SyntaxError: invalid syntax (<string>, line 3) |
| 224 | +SELECT subtransaction_exit_without_enter(); |
| 225 | +ERROR: ValueError: this subtransaction has not been entered |
| 226 | +CONTEXT: PL/Python function "subtransaction_exit_without_enter" |
| 227 | +SELECT subtransaction_enter_without_exit(); |
| 228 | +WARNING: forcibly aborting a subtransaction that has not been exited |
| 229 | +CONTEXT: PL/Python function "subtransaction_enter_without_exit" |
| 230 | + subtransaction_enter_without_exit |
| 231 | +----------------------------------- |
| 232 | + |
| 233 | +(1 row) |
| 234 | + |
| 235 | +SELECT subtransaction_exit_twice(); |
| 236 | +WARNING: forcibly aborting a subtransaction that has not been exited |
| 237 | +CONTEXT: PL/Python function "subtransaction_exit_twice" |
| 238 | +ERROR: ValueError: this subtransaction has not been entered |
| 239 | +CONTEXT: PL/Python function "subtransaction_exit_twice" |
| 240 | +SELECT subtransaction_enter_twice(); |
| 241 | +WARNING: forcibly aborting a subtransaction that has not been exited |
| 242 | +CONTEXT: PL/Python function "subtransaction_enter_twice" |
| 243 | +WARNING: forcibly aborting a subtransaction that has not been exited |
| 244 | +CONTEXT: PL/Python function "subtransaction_enter_twice" |
| 245 | + subtransaction_enter_twice |
| 246 | +---------------------------- |
| 247 | + |
| 248 | +(1 row) |
| 249 | + |
| 250 | +SELECT subtransaction_exit_same_subtransaction_twice(); |
| 251 | +ERROR: ValueError: this subtransaction has already been exited |
| 252 | +CONTEXT: PL/Python function "subtransaction_exit_same_subtransaction_twice" |
| 253 | +SELECT subtransaction_enter_same_subtransaction_twice(); |
| 254 | +WARNING: forcibly aborting a subtransaction that has not been exited |
| 255 | +CONTEXT: PL/Python function "subtransaction_enter_same_subtransaction_twice" |
| 256 | +ERROR: ValueError: this subtransaction has already been entered |
| 257 | +CONTEXT: PL/Python function "subtransaction_enter_same_subtransaction_twice" |
| 258 | +SELECT subtransaction_enter_subtransaction_in_with(); |
| 259 | +ERROR: function subtransaction_enter_subtransaction_in_with() does not exist |
| 260 | +LINE 1: SELECT subtransaction_enter_subtransaction_in_with(); |
| 261 | + ^ |
| 262 | +HINT: No function matches the given name and argument types. You might need to add explicit type casts. |
| 263 | +SELECT subtransaction_exit_subtransaction_in_with(); |
| 264 | +ERROR: function subtransaction_exit_subtransaction_in_with() does not exist |
| 265 | +LINE 1: SELECT subtransaction_exit_subtransaction_in_with(); |
| 266 | + ^ |
| 267 | +HINT: No function matches the given name and argument types. You might need to add explicit type casts. |
| 268 | +-- Make sure we don't get a "current transaction is aborted" error |
| 269 | +SELECT 1 as test; |
| 270 | + test |
| 271 | +------ |
| 272 | + 1 |
| 273 | +(1 row) |
| 274 | + |
| 275 | +-- Mix explicit subtransactions and normal SPI calls |
| 276 | +CREATE FUNCTION subtransaction_mix_explicit_and_implicit() RETURNS void |
| 277 | +AS $$ |
| 278 | +p = plpy.prepare("INSERT INTO subtransaction_tbl VALUES ($1)", ["integer"]) |
| 279 | +try: |
| 280 | + with plpy.subtransaction(): |
| 281 | + plpy.execute("INSERT INTO subtransaction_tbl VALUES (1)") |
| 282 | + plpy.execute(p, [2]) |
| 283 | + plpy.execute(p, ["wrong"]) |
| 284 | +except plpy.SPIError: |
| 285 | + plpy.warning("Caught a SPI error from an explicit subtransaction") |
| 286 | + |
| 287 | +try: |
| 288 | + plpy.execute("INSERT INTO subtransaction_tbl VALUES (1)") |
| 289 | + plpy.execute(p, [2]) |
| 290 | + plpy.execute(p, ["wrong"]) |
| 291 | +except plpy.SPIError: |
| 292 | + plpy.warning("Caught a SPI error") |
| 293 | +$$ LANGUAGE plpythonu; |
| 294 | +ERROR: could not compile PL/Python function "subtransaction_mix_explicit_and_implicit" |
| 295 | +DETAIL: SyntaxError: invalid syntax (<string>, line 5) |
| 296 | +SELECT subtransaction_mix_explicit_and_implicit(); |
| 297 | +ERROR: function subtransaction_mix_explicit_and_implicit() does not exist |
| 298 | +LINE 1: SELECT subtransaction_mix_explicit_and_implicit(); |
| 299 | + ^ |
| 300 | +HINT: No function matches the given name and argument types. You might need to add explicit type casts. |
| 301 | +SELECT * FROM subtransaction_tbl; |
| 302 | + i |
| 303 | +--- |
| 304 | +(0 rows) |
| 305 | + |
| 306 | +TRUNCATE subtransaction_tbl; |
| 307 | +-- Alternative method names for Python <2.6 |
| 308 | +CREATE FUNCTION subtransaction_alternative_names() RETURNS void |
| 309 | +AS $$ |
| 310 | +s = plpy.subtransaction() |
| 311 | +s.enter() |
| 312 | +s.exit(None, None, None) |
| 313 | +$$ LANGUAGE plpythonu; |
| 314 | +SELECT subtransaction_alternative_names(); |
| 315 | + subtransaction_alternative_names |
| 316 | +---------------------------------- |
| 317 | + |
| 318 | +(1 row) |
| 319 | + |
| 320 | +-- try/catch inside a subtransaction block |
| 321 | +CREATE FUNCTION try_catch_inside_subtransaction() RETURNS void |
| 322 | +AS $$ |
| 323 | +with plpy.subtransaction(): |
| 324 | + plpy.execute("INSERT INTO subtransaction_tbl VALUES (1)") |
| 325 | + try: |
| 326 | + plpy.execute("INSERT INTO subtransaction_tbl VALUES ('a')") |
| 327 | + except plpy.SPIError: |
| 328 | + plpy.notice("caught") |
| 329 | +$$ LANGUAGE plpythonu; |
| 330 | +ERROR: could not compile PL/Python function "try_catch_inside_subtransaction" |
| 331 | +DETAIL: SyntaxError: invalid syntax (<string>, line 3) |
| 332 | +SELECT try_catch_inside_subtransaction(); |
| 333 | +ERROR: function try_catch_inside_subtransaction() does not exist |
| 334 | +LINE 1: SELECT try_catch_inside_subtransaction(); |
| 335 | + ^ |
| 336 | +HINT: No function matches the given name and argument types. You might need to add explicit type casts. |
| 337 | +SELECT * FROM subtransaction_tbl; |
| 338 | + i |
| 339 | +--- |
| 340 | +(0 rows) |
| 341 | + |
| 342 | +TRUNCATE subtransaction_tbl; |
| 343 | +ALTER TABLE subtransaction_tbl ADD PRIMARY KEY (i); |
| 344 | +NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index "subtransaction_tbl_pkey" for table "subtransaction_tbl" |
| 345 | +CREATE FUNCTION pk_violation_inside_subtransaction() RETURNS void |
| 346 | +AS $$ |
| 347 | +with plpy.subtransaction(): |
| 348 | + plpy.execute("INSERT INTO subtransaction_tbl VALUES (1)") |
| 349 | + try: |
| 350 | + plpy.execute("INSERT INTO subtransaction_tbl VALUES (1)") |
| 351 | + except plpy.SPIError: |
| 352 | + plpy.notice("caught") |
| 353 | +$$ LANGUAGE plpythonu; |
| 354 | +ERROR: could not compile PL/Python function "pk_violation_inside_subtransaction" |
| 355 | +DETAIL: SyntaxError: invalid syntax (<string>, line 3) |
| 356 | +SELECT pk_violation_inside_subtransaction(); |
| 357 | +ERROR: function pk_violation_inside_subtransaction() does not exist |
| 358 | +LINE 1: SELECT pk_violation_inside_subtransaction(); |
| 359 | + ^ |
| 360 | +HINT: No function matches the given name and argument types. You might need to add explicit type casts. |
| 361 | +SELECT * FROM subtransaction_tbl; |
| 362 | + i |
| 363 | +--- |
| 364 | +(0 rows) |
| 365 | + |
| 366 | +DROP TABLE subtransaction_tbl; |
0 commit comments