--- /dev/null
+++ b/src/pl/plpython/expected/plpython_subtransaction_1.out
@@ -0,0 +1,411 @@
+--
+-- 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:  Traceback (most recent call last):
+  PL/Python function "subtransaction_test", line 11, in <module>
+    plpy.execute("INSERT INTO subtransaction_tbl VALUES ('oops')")
+PL/Python function "subtransaction_test"
+SELECT * FROM subtransaction_tbl;
+ i 
+---
+(0 rows)
+
+TRUNCATE subtransaction_tbl;
+SELECT subtransaction_test('Python');
+ERROR:  AttributeError: module 'plpy' has no attribute 'attribute_error'
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "subtransaction_test", line 13, in <module>
+    plpy.attribute_error
+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:  Traceback (most recent call last):
+  PL/Python function "subtransaction_ctx_test", line 6, in <module>
+    plpy.execute("INSERT INTO subtransaction_tbl VALUES ('oops')")
+PL/Python function "subtransaction_ctx_test"
+SELECT * FROM subtransaction_tbl;
+ i 
+---
+(0 rows)
+
+TRUNCATE subtransaction_tbl;
+SELECT subtransaction_ctx_test('Python');
+ERROR:  AttributeError: module 'plpy' has no attribute 'attribute_error'
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "subtransaction_ctx_test", line 8, in <module>
+    plpy.attribute_error
+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:  Traceback (most recent call last):
+  PL/Python function "subtransaction_nested_test", line 8, in <module>
+    plpy.execute("error")
+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:  Traceback (most recent call last):
+  PL/Python function "subtransaction_exit_without_enter", line 2, in <module>
+    plpy.subtransaction().__exit__(None, None, None)
+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:  Traceback (most recent call last):
+  PL/Python function "subtransaction_exit_twice", line 3, in <module>
+    plpy.subtransaction().__exit__(None, None, None)
+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:  Traceback (most recent call last):
+  PL/Python function "subtransaction_exit_same_subtransaction_twice", line 5, in <module>
+    s.__exit__(None, None, None)
+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:  Traceback (most recent call last):
+  PL/Python function "subtransaction_enter_same_subtransaction_twice", line 4, in <module>
+    s.__enter__()
+PL/Python function "subtransaction_enter_same_subtransaction_twice"
+SELECT subtransaction_enter_subtransaction_in_with();
+ERROR:  ValueError: this subtransaction has already been entered
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "subtransaction_enter_subtransaction_in_with", line 3, in <module>
+    s.__enter__()
+PL/Python function "subtransaction_enter_subtransaction_in_with"
+SELECT subtransaction_exit_subtransaction_in_with();
+ERROR:  ValueError: this subtransaction has already been exited
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "subtransaction_exit_subtransaction_in_with", line 3, in <module>
+    s.__exit__(None, None, None)
+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;
--- /dev/null
+++ b/src/pl/plpython/expected/plpython_do_1.out
@@ -0,0 +1,9 @@
+DO $$ plpy.notice("This is plpythonu.") $$ LANGUAGE plpythonu;
+NOTICE:  This is plpythonu.
+CONTEXT:  PL/Python anonymous code block
+DO $$ nonsense $$ LANGUAGE plpythonu;
+ERROR:  NameError: name 'nonsense' is not defined
+CONTEXT:  Traceback (most recent call last):
+  PL/Python anonymous code block, line 1, in <module>
+    nonsense 
+PL/Python anonymous code block
--- /dev/null
+++ b/src/pl/plpython/expected/plpython_error_1.out
@@ -0,0 +1,403 @@
+-- test error handling, i forgot to restore Warn_restart in
+-- the trigger handler once. the errors and subsequent core dump were
+-- interesting.
+/* Flat out Python syntax error
+ */
+CREATE FUNCTION python_syntax_error() RETURNS text
+        AS
+'.syntaxerror'
+        LANGUAGE plpythonu;
+ERROR:  could not compile PL/Python function "python_syntax_error"
+DETAIL:  SyntaxError: invalid syntax (<string>, line 2)
+/* With check_function_bodies = false the function should get defined
+ * and the error reported when called
+ */
+SET check_function_bodies = false;
+CREATE FUNCTION python_syntax_error() RETURNS text
+        AS
+'.syntaxerror'
+        LANGUAGE plpythonu;
+SELECT python_syntax_error();
+ERROR:  could not compile PL/Python function "python_syntax_error"
+DETAIL:  SyntaxError: invalid syntax (<string>, line 2)
+/* Run the function twice to check if the hashtable entry gets cleaned up */
+SELECT python_syntax_error();
+ERROR:  could not compile PL/Python function "python_syntax_error"
+DETAIL:  SyntaxError: invalid syntax (<string>, line 2)
+RESET check_function_bodies;
+/* Flat out syntax error
+ */
+CREATE FUNCTION sql_syntax_error() RETURNS text
+        AS
+'plpy.execute("syntax error")'
+        LANGUAGE plpythonu;
+SELECT sql_syntax_error();
+ERROR:  spiexceptions.SyntaxError: syntax error at or near "syntax"
+LINE 1: syntax error
+        ^
+QUERY:  syntax error
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "sql_syntax_error", line 1, in <module>
+    plpy.execute("syntax error")
+PL/Python function "sql_syntax_error"
+/* check the handling of uncaught python exceptions
+ */
+CREATE FUNCTION exception_index_invalid(text) RETURNS text
+	AS
+'return args[1]'
+	LANGUAGE plpythonu;
+SELECT exception_index_invalid('test');
+ERROR:  IndexError: list index out of range
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "exception_index_invalid", line 1, in <module>
+    return args[1]
+PL/Python function "exception_index_invalid"
+/* check handling of nested exceptions
+ */
+CREATE FUNCTION exception_index_invalid_nested() RETURNS text
+	AS
+'rv = plpy.execute("SELECT test5(''foo'')")
+return rv[0]'
+	LANGUAGE plpythonu;
+SELECT exception_index_invalid_nested();
+ERROR:  spiexceptions.UndefinedFunction: function test5(unknown) does not exist
+LINE 1: SELECT test5('foo')
+               ^
+HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
+QUERY:  SELECT test5('foo')
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "exception_index_invalid_nested", line 1, in <module>
+    rv = plpy.execute("SELECT test5('foo')")
+PL/Python function "exception_index_invalid_nested"
+/* a typo
+ */
+CREATE FUNCTION invalid_type_uncaught(a text) RETURNS text
+	AS
+'if "plan" not in SD:
+	q = "SELECT fname FROM users WHERE lname = $1"
+	SD["plan"] = plpy.prepare(q, [ "test" ])
+rv = plpy.execute(SD["plan"], [ a ])
+if len(rv):
+	return rv[0]["fname"]
+return None
+'
+	LANGUAGE plpythonu;
+SELECT invalid_type_uncaught('rick');
+ERROR:  spiexceptions.UndefinedObject: type "test" does not exist
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "invalid_type_uncaught", line 3, in <module>
+    SD["plan"] = plpy.prepare(q, [ "test" ])
+PL/Python function "invalid_type_uncaught"
+/* for what it's worth catch the exception generated by
+ * the typo, and return None
+ */
+CREATE FUNCTION invalid_type_caught(a text) RETURNS text
+	AS
+'if "plan" not in SD:
+	q = "SELECT fname FROM users WHERE lname = $1"
+	try:
+		SD["plan"] = plpy.prepare(q, [ "test" ])
+	except plpy.SPIError, ex:
+		plpy.notice(str(ex))
+		return None
+rv = plpy.execute(SD["plan"], [ a ])
+if len(rv):
+	return rv[0]["fname"]
+return None
+'
+	LANGUAGE plpythonu;
+SELECT invalid_type_caught('rick');
+NOTICE:  type "test" does not exist
+CONTEXT:  PL/Python function "invalid_type_caught"
+ invalid_type_caught 
+---------------------
+ 
+(1 row)
+
+/* for what it's worth catch the exception generated by
+ * the typo, and reraise it as a plain error
+ */
+CREATE FUNCTION invalid_type_reraised(a text) RETURNS text
+	AS
+'if "plan" not in SD:
+	q = "SELECT fname FROM users WHERE lname = $1"
+	try:
+		SD["plan"] = plpy.prepare(q, [ "test" ])
+	except plpy.SPIError, ex:
+		plpy.error(str(ex))
+rv = plpy.execute(SD["plan"], [ a ])
+if len(rv):
+	return rv[0]["fname"]
+return None
+'
+	LANGUAGE plpythonu;
+SELECT invalid_type_reraised('rick');
+ERROR:  plpy.Error: type "test" does not exist
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "invalid_type_reraised", line 6, in <module>
+    plpy.error(str(ex))
+PL/Python function "invalid_type_reraised"
+/* no typo no messing about
+ */
+CREATE FUNCTION valid_type(a text) RETURNS text
+	AS
+'if "plan" not in SD:
+	SD["plan"] = plpy.prepare("SELECT fname FROM users WHERE lname = $1", [ "text" ])
+rv = plpy.execute(SD["plan"], [ a ])
+if len(rv):
+	return rv[0]["fname"]
+return None
+'
+	LANGUAGE plpythonu;
+SELECT valid_type('rick');
+ valid_type 
+------------
+ 
+(1 row)
+
+/* error in nested functions to get a traceback
+*/
+CREATE FUNCTION nested_error() RETURNS text
+	AS
+'def fun1():
+	plpy.error("boom")
+
+def fun2():
+	fun1()
+
+def fun3():
+	fun2()
+
+fun3()
+return "not reached"
+'
+	LANGUAGE plpythonu;
+SELECT nested_error();
+ERROR:  plpy.Error: boom
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "nested_error", line 10, in <module>
+    fun3()
+  PL/Python function "nested_error", line 8, in fun3
+    fun2()
+  PL/Python function "nested_error", line 5, in fun2
+    fun1()
+  PL/Python function "nested_error", line 2, in fun1
+    plpy.error("boom")
+PL/Python function "nested_error"
+/* raising plpy.Error is just like calling plpy.error
+*/
+CREATE FUNCTION nested_error_raise() RETURNS text
+	AS
+'def fun1():
+	raise plpy.Error("boom")
+
+def fun2():
+	fun1()
+
+def fun3():
+	fun2()
+
+fun3()
+return "not reached"
+'
+	LANGUAGE plpythonu;
+SELECT nested_error_raise();
+ERROR:  plpy.Error: boom
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "nested_error_raise", line 10, in <module>
+    fun3()
+  PL/Python function "nested_error_raise", line 8, in fun3
+    fun2()
+  PL/Python function "nested_error_raise", line 5, in fun2
+    fun1()
+  PL/Python function "nested_error_raise", line 2, in fun1
+    raise plpy.Error("boom")
+PL/Python function "nested_error_raise"
+/* using plpy.warning should not produce a traceback
+*/
+CREATE FUNCTION nested_warning() RETURNS text
+	AS
+'def fun1():
+	plpy.warning("boom")
+
+def fun2():
+	fun1()
+
+def fun3():
+	fun2()
+
+fun3()
+return "you''ve been warned"
+'
+	LANGUAGE plpythonu;
+SELECT nested_warning();
+WARNING:  boom
+CONTEXT:  PL/Python function "nested_warning"
+   nested_warning   
+--------------------
+ you've been warned
+(1 row)
+
+/* AttributeError at toplevel used to give segfaults with the traceback
+*/
+CREATE FUNCTION toplevel_attribute_error() RETURNS void AS
+$$
+plpy.nonexistent
+$$ LANGUAGE plpythonu;
+SELECT toplevel_attribute_error();
+ERROR:  AttributeError: module 'plpy' has no attribute 'nonexistent'
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "toplevel_attribute_error", line 2, in <module>
+    plpy.nonexistent
+PL/Python function "toplevel_attribute_error"
+/* Calling PL/Python functions from SQL and vice versa should not lose context.
+ */
+CREATE OR REPLACE FUNCTION python_traceback() RETURNS void AS $$
+def first():
+  second()
+
+def second():
+  third()
+
+def third():
+  plpy.execute("select sql_error()")
+
+first()
+$$ LANGUAGE plpythonu;
+CREATE OR REPLACE FUNCTION sql_error() RETURNS void AS $$
+begin
+  select 1/0;
+end
+$$ LANGUAGE plpgsql;
+CREATE OR REPLACE FUNCTION python_from_sql_error() RETURNS void AS $$
+begin
+  select python_traceback();
+end
+$$ LANGUAGE plpgsql;
+CREATE OR REPLACE FUNCTION sql_from_python_error() RETURNS void AS $$
+plpy.execute("select sql_error()")
+$$ LANGUAGE plpythonu;
+SELECT python_traceback();
+ERROR:  spiexceptions.DivisionByZero: division by zero
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "python_traceback", line 11, in <module>
+    first()
+  PL/Python function "python_traceback", line 3, in first
+    second()
+  PL/Python function "python_traceback", line 6, in second
+    third()
+  PL/Python function "python_traceback", line 9, in third
+    plpy.execute("select sql_error()")
+PL/Python function "python_traceback"
+SELECT sql_error();
+ERROR:  division by zero
+CONTEXT:  SQL statement "select 1/0"
+PL/pgSQL function "sql_error" line 3 at SQL statement
+SELECT python_from_sql_error();
+ERROR:  spiexceptions.DivisionByZero: division by zero
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "python_traceback", line 11, in <module>
+    first()
+  PL/Python function "python_traceback", line 3, in first
+    second()
+  PL/Python function "python_traceback", line 6, in second
+    third()
+  PL/Python function "python_traceback", line 9, in third
+    plpy.execute("select sql_error()")
+PL/Python function "python_traceback"
+SQL statement "select python_traceback()"
+PL/pgSQL function "python_from_sql_error" line 3 at SQL statement
+SELECT sql_from_python_error();
+ERROR:  spiexceptions.DivisionByZero: division by zero
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "sql_from_python_error", line 2, in <module>
+    plpy.execute("select sql_error()")
+PL/Python function "sql_from_python_error"
+/* check catching specific types of exceptions
+ */
+CREATE TABLE specific (
+    i integer PRIMARY KEY
+);
+NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "specific_pkey" for table "specific"
+CREATE FUNCTION specific_exception(i integer) RETURNS void AS
+$$
+from plpy import spiexceptions
+try:
+    plpy.execute("insert into specific values (%s)" % (i or "NULL"));
+except spiexceptions.NotNullViolation, e:
+    plpy.notice("Violated the NOT NULL constraint, sqlstate %s" % e.sqlstate)
+except spiexceptions.UniqueViolation, e:
+    plpy.notice("Violated the UNIQUE constraint, sqlstate %s" % e.sqlstate)
+$$ LANGUAGE plpythonu;
+SELECT specific_exception(2);
+ specific_exception 
+--------------------
+ 
+(1 row)
+
+SELECT specific_exception(NULL);
+NOTICE:  Violated the NOT NULL constraint, sqlstate 23502
+CONTEXT:  PL/Python function "specific_exception"
+ specific_exception 
+--------------------
+ 
+(1 row)
+
+SELECT specific_exception(2);
+NOTICE:  Violated the UNIQUE constraint, sqlstate 23505
+CONTEXT:  PL/Python function "specific_exception"
+ specific_exception 
+--------------------
+ 
+(1 row)
+
+/* SPI errors in PL/Python functions should preserve the SQLSTATE value
+ */
+CREATE FUNCTION python_unique_violation() RETURNS void AS $$
+plpy.execute("insert into specific values (1)")
+plpy.execute("insert into specific values (1)")
+$$ LANGUAGE plpythonu;
+CREATE FUNCTION catch_python_unique_violation() RETURNS text AS $$
+begin
+    begin
+        perform python_unique_violation();
+    exception when unique_violation then
+        return 'ok';
+    end;
+    return 'not reached';
+end;
+$$ language plpgsql;
+SELECT catch_python_unique_violation();
+ catch_python_unique_violation 
+-------------------------------
+ ok
+(1 row)
+
+/* manually starting subtransactions - a bad idea
+ */
+CREATE FUNCTION manual_subxact() RETURNS void AS $$
+plpy.execute("savepoint save")
+plpy.execute("create table foo(x integer)")
+plpy.execute("rollback to save")
+$$ LANGUAGE plpythonu;
+SELECT manual_subxact();
+ERROR:  plpy.SPIError: SPI_execute failed: SPI_ERROR_TRANSACTION
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "manual_subxact", line 2, in <module>
+    plpy.execute("savepoint save")
+PL/Python function "manual_subxact"
+/* same for prepared plans
+ */
+CREATE FUNCTION manual_subxact_prepared() RETURNS void AS $$
+save = plpy.prepare("savepoint save")
+rollback = plpy.prepare("rollback to save")
+plpy.execute(save)
+plpy.execute("create table foo(x integer)")
+plpy.execute(rollback)
+$$ LANGUAGE plpythonu;
+SELECT manual_subxact_prepared();
+ERROR:  plpy.SPIError: SPI_execute_plan failed: SPI_ERROR_TRANSACTION
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "manual_subxact_prepared", line 4, in <module>
+    plpy.execute(save)
+PL/Python function "manual_subxact_prepared"
--- /dev/null
+++ b/src/pl/plpython/expected/plpython_import_1.out
@@ -0,0 +1,82 @@
+-- import python modules
+CREATE FUNCTION import_fail() returns text
+    AS
+'try:
+	import foosocket
+except Exception, ex:
+	plpy.notice("import socket failed -- %s" % str(ex))
+	return "failed as expected"
+return "succeeded, that wasn''t supposed to happen"'
+    LANGUAGE plpythonu;
+CREATE FUNCTION import_succeed() returns text
+	AS
+'try:
+  import array
+  import bisect
+  import calendar
+  import cmath
+  import errno
+  import math
+  import operator
+  import random
+  import re
+  import string
+  import time
+except Exception, ex:
+	plpy.notice("import failed -- %s" % str(ex))
+	return "failed, that wasn''t supposed to happen"
+return "succeeded, as expected"'
+    LANGUAGE plpythonu;
+CREATE FUNCTION import_test_one(p text) RETURNS text
+	AS
+'try:
+    import hashlib
+    digest = hashlib.sha1(p.encode("ascii"))
+except ImportError:
+    import sha
+    digest = sha.new(p)
+return digest.hexdigest()'
+	LANGUAGE plpythonu;
+CREATE FUNCTION import_test_two(u users) RETURNS text
+	AS
+'plain = u["fname"] + u["lname"]
+try:
+    import hashlib
+    digest = hashlib.sha1(plain.encode("ascii"))
+except ImportError:
+    import sha
+    digest = sha.new(plain);
+return "sha hash of " + plain + " is " + digest.hexdigest()'
+	LANGUAGE plpythonu;
+-- import python modules
+--
+SELECT import_fail();
+NOTICE:  import socket failed -- No module named 'foosocket'
+CONTEXT:  PL/Python function "import_fail"
+    import_fail     
+--------------------
+ failed as expected
+(1 row)
+
+SELECT import_succeed();
+     import_succeed     
+------------------------
+ succeeded, as expected
+(1 row)
+
+-- test import and simple argument handling
+--
+SELECT import_test_one('sha hash of this string');
+             import_test_one              
+------------------------------------------
+ a04e23cb9b1a09cd1051a04a7c571aae0f90346c
+(1 row)
+
+-- test import and tuple argument handling
+--
+select import_test_two(users) from users where fname = 'willem';
+                          import_test_two                          
+-------------------------------------------------------------------
+ sha hash of willemdoe is 3cde6b574953b0ca937b4d76ebc40d534d910759
+(1 row)
+
--- /dev/null
+++ b/src/pl/plpython/expected/plpython_subtransaction_2.out
@@ -0,0 +1,411 @@
+--
+-- 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 plpython3u;
+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:  Traceback (most recent call last):
+  PL/Python function "subtransaction_test", line 11, in <module>
+    plpy.execute("INSERT INTO subtransaction_tbl VALUES ('oops')")
+PL/Python function "subtransaction_test"
+SELECT * FROM subtransaction_tbl;
+ i 
+---
+(0 rows)
+
+TRUNCATE subtransaction_tbl;
+SELECT subtransaction_test('Python');
+ERROR:  AttributeError: module 'plpy' has no attribute 'attribute_error'
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "subtransaction_test", line 13, in <module>
+    plpy.attribute_error
+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 plpython3u;
+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:  Traceback (most recent call last):
+  PL/Python function "subtransaction_ctx_test", line 6, in <module>
+    plpy.execute("INSERT INTO subtransaction_tbl VALUES ('oops')")
+PL/Python function "subtransaction_ctx_test"
+SELECT * FROM subtransaction_tbl;
+ i 
+---
+(0 rows)
+
+TRUNCATE subtransaction_tbl;
+SELECT subtransaction_ctx_test('Python');
+ERROR:  AttributeError: module 'plpy' has no attribute 'attribute_error'
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "subtransaction_ctx_test", line 8, in <module>
+    plpy.attribute_error
+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 as e:
+        if not swallow:
+            raise
+        plpy.notice("Swallowed %r" % e)
+return "ok"
+$$ LANGUAGE plpython3u;
+SELECT subtransaction_nested_test();
+ERROR:  spiexceptions.SyntaxError: syntax error at or near "error"
+LINE 1: error
+        ^
+QUERY:  error
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "subtransaction_nested_test", line 8, in <module>
+    plpy.execute("error")
+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 plpython3u;
+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 plpython3u;
+CREATE FUNCTION subtransaction_enter_without_exit() RETURNS void
+AS $$
+plpy.subtransaction().__enter__()
+$$ LANGUAGE plpython3u;
+CREATE FUNCTION subtransaction_exit_twice() RETURNS void
+AS $$
+plpy.subtransaction().__enter__()
+plpy.subtransaction().__exit__(None, None, None)
+plpy.subtransaction().__exit__(None, None, None)
+$$ LANGUAGE plpython3u;
+CREATE FUNCTION subtransaction_enter_twice() RETURNS void
+AS $$
+plpy.subtransaction().__enter__()
+plpy.subtransaction().__enter__()
+$$ LANGUAGE plpython3u;
+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 plpython3u;
+CREATE FUNCTION subtransaction_enter_same_subtransaction_twice() RETURNS void
+AS $$
+s = plpy.subtransaction()
+s.__enter__()
+s.__enter__()
+s.__exit__(None, None, None)
+$$ LANGUAGE plpython3u;
+-- 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 plpython3u;
+CREATE FUNCTION subtransaction_exit_subtransaction_in_with() RETURNS void
+AS $$
+with plpy.subtransaction() as s:
+    s.__exit__(None, None, None)
+$$ LANGUAGE plpython3u;
+SELECT subtransaction_exit_without_enter();
+ERROR:  ValueError: this subtransaction has not been entered
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "subtransaction_exit_without_enter", line 2, in <module>
+    plpy.subtransaction().__exit__(None, None, None)
+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:  Traceback (most recent call last):
+  PL/Python function "subtransaction_exit_twice", line 3, in <module>
+    plpy.subtransaction().__exit__(None, None, None)
+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:  Traceback (most recent call last):
+  PL/Python function "subtransaction_exit_same_subtransaction_twice", line 5, in <module>
+    s.__exit__(None, None, None)
+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:  Traceback (most recent call last):
+  PL/Python function "subtransaction_enter_same_subtransaction_twice", line 4, in <module>
+    s.__enter__()
+PL/Python function "subtransaction_enter_same_subtransaction_twice"
+SELECT subtransaction_enter_subtransaction_in_with();
+ERROR:  ValueError: this subtransaction has already been entered
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "subtransaction_enter_subtransaction_in_with", line 3, in <module>
+    s.__enter__()
+PL/Python function "subtransaction_enter_subtransaction_in_with"
+SELECT subtransaction_exit_subtransaction_in_with();
+ERROR:  ValueError: this subtransaction has already been exited
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "subtransaction_exit_subtransaction_in_with", line 3, in <module>
+    s.__exit__(None, None, None)
+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 plpython3u;
+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 plpython3u;
+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 plpython3u;
+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 plpython3u;
+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;
--- /dev/null
+++ b/src/pl/plpython/expected/plpython_trigger_1.out
@@ -0,0 +1,630 @@
+-- these triggers are dedicated to HPHC of RI who
+-- decided that my kid's name was william not willem, and
+-- vigorously resisted all efforts at correction.  they have
+-- since gone bankrupt...
+CREATE FUNCTION users_insert() returns trigger
+	AS
+'if TD["new"]["fname"] == None or TD["new"]["lname"] == None:
+	return "SKIP"
+if TD["new"]["username"] == None:
+	TD["new"]["username"] = TD["new"]["fname"][:1] + "_" + TD["new"]["lname"]
+	rv = "MODIFY"
+else:
+	rv = None
+if TD["new"]["fname"] == "william":
+	TD["new"]["fname"] = TD["args"][0]
+	rv = "MODIFY"
+return rv'
+	LANGUAGE plpython3u;
+CREATE FUNCTION users_update() returns trigger
+	AS
+'if TD["event"] == "UPDATE":
+	if TD["old"]["fname"] != TD["new"]["fname"] and TD["old"]["fname"] == TD["args"][0]:
+		return "SKIP"
+return None'
+	LANGUAGE plpython3u;
+CREATE FUNCTION users_delete() RETURNS trigger
+	AS
+'if TD["old"]["fname"] == TD["args"][0]:
+	return "SKIP"
+return None'
+	LANGUAGE plpython3u;
+CREATE TRIGGER users_insert_trig BEFORE INSERT ON users FOR EACH ROW
+	EXECUTE PROCEDURE users_insert ('willem');
+CREATE TRIGGER users_update_trig BEFORE UPDATE ON users FOR EACH ROW
+	EXECUTE PROCEDURE users_update ('willem');
+CREATE TRIGGER users_delete_trig BEFORE DELETE ON users FOR EACH ROW
+	EXECUTE PROCEDURE users_delete ('willem');
+-- quick peek at the table
+--
+SELECT * FROM users;
+ fname  | lname | username | userid 
+--------+-------+----------+--------
+ jane   | doe   | j_doe    |      1
+ john   | doe   | johnd    |      2
+ willem | doe   | w_doe    |      3
+ rick   | smith | slash    |      4
+(4 rows)
+
+-- should fail
+--
+UPDATE users SET fname = 'william' WHERE fname = 'willem';
+-- should modify william to willem and create username
+--
+INSERT INTO users (fname, lname) VALUES ('william', 'smith');
+INSERT INTO users (fname, lname, username) VALUES ('charles', 'darwin', 'beagle');
+SELECT * FROM users;
+  fname  | lname  | username | userid 
+---------+--------+----------+--------
+ jane    | doe    | j_doe    |      1
+ john    | doe    | johnd    |      2
+ willem  | doe    | w_doe    |      3
+ rick    | smith  | slash    |      4
+ willem  | smith  | w_smith  |      5
+ charles | darwin | beagle   |      6
+(6 rows)
+
+-- dump trigger data
+CREATE TABLE trigger_test
+	(i int, v text );
+CREATE FUNCTION trigger_data() RETURNS trigger LANGUAGE plpython3u AS $$
+
+if 'relid' in TD:
+	TD['relid'] = "bogus:12345"
+
+skeys = list(TD.keys())
+skeys.sort()
+for key in skeys:
+	val = TD[key]
+	plpy.notice("TD[" + key + "] => " + str(val))
+
+return None
+
+$$;
+CREATE TRIGGER show_trigger_data_trig_before
+BEFORE INSERT OR UPDATE OR DELETE ON trigger_test
+FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
+CREATE TRIGGER show_trigger_data_trig_after
+AFTER INSERT OR UPDATE OR DELETE ON trigger_test
+FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
+CREATE TRIGGER show_trigger_data_trig_stmt
+BEFORE INSERT OR UPDATE OR DELETE OR TRUNCATE ON trigger_test
+FOR EACH STATEMENT EXECUTE PROCEDURE trigger_data(23,'skidoo');
+insert into trigger_test values(1,'insert');
+NOTICE:  TD[args] => ['23', 'skidoo']
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[event] => INSERT
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[level] => STATEMENT
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[name] => show_trigger_data_trig_stmt
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[new] => None
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[old] => None
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[relid] => bogus:12345
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_name] => trigger_test
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_schema] => public
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[when] => BEFORE
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[args] => ['23', 'skidoo']
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[event] => INSERT
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[level] => ROW
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[name] => show_trigger_data_trig_before
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[new] => {'i': 1, 'v': 'insert'}
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[old] => None
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[relid] => bogus:12345
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_name] => trigger_test
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_schema] => public
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[when] => BEFORE
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[args] => ['23', 'skidoo']
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[event] => INSERT
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[level] => ROW
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[name] => show_trigger_data_trig_after
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[new] => {'i': 1, 'v': 'insert'}
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[old] => None
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[relid] => bogus:12345
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_name] => trigger_test
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_schema] => public
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[when] => AFTER
+CONTEXT:  PL/Python function "trigger_data"
+update trigger_test set v = 'update' where i = 1;
+NOTICE:  TD[args] => ['23', 'skidoo']
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[event] => UPDATE
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[level] => STATEMENT
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[name] => show_trigger_data_trig_stmt
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[new] => None
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[old] => None
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[relid] => bogus:12345
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_name] => trigger_test
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_schema] => public
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[when] => BEFORE
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[args] => ['23', 'skidoo']
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[event] => UPDATE
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[level] => ROW
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[name] => show_trigger_data_trig_before
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[new] => {'i': 1, 'v': 'update'}
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[old] => {'i': 1, 'v': 'insert'}
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[relid] => bogus:12345
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_name] => trigger_test
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_schema] => public
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[when] => BEFORE
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[args] => ['23', 'skidoo']
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[event] => UPDATE
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[level] => ROW
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[name] => show_trigger_data_trig_after
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[new] => {'i': 1, 'v': 'update'}
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[old] => {'i': 1, 'v': 'insert'}
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[relid] => bogus:12345
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_name] => trigger_test
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_schema] => public
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[when] => AFTER
+CONTEXT:  PL/Python function "trigger_data"
+delete from trigger_test;
+NOTICE:  TD[args] => ['23', 'skidoo']
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[event] => DELETE
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[level] => STATEMENT
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[name] => show_trigger_data_trig_stmt
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[new] => None
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[old] => None
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[relid] => bogus:12345
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_name] => trigger_test
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_schema] => public
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[when] => BEFORE
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[args] => ['23', 'skidoo']
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[event] => DELETE
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[level] => ROW
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[name] => show_trigger_data_trig_before
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[new] => None
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[old] => {'i': 1, 'v': 'update'}
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[relid] => bogus:12345
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_name] => trigger_test
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_schema] => public
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[when] => BEFORE
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[args] => ['23', 'skidoo']
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[event] => DELETE
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[level] => ROW
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[name] => show_trigger_data_trig_after
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[new] => None
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[old] => {'i': 1, 'v': 'update'}
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[relid] => bogus:12345
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_name] => trigger_test
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_schema] => public
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[when] => AFTER
+CONTEXT:  PL/Python function "trigger_data"
+truncate table trigger_test;
+NOTICE:  TD[args] => ['23', 'skidoo']
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[event] => TRUNCATE
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[level] => STATEMENT
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[name] => show_trigger_data_trig_stmt
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[new] => None
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[old] => None
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[relid] => bogus:12345
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_name] => trigger_test
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_schema] => public
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[when] => BEFORE
+CONTEXT:  PL/Python function "trigger_data"
+DROP TRIGGER show_trigger_data_trig_stmt on trigger_test;
+DROP TRIGGER show_trigger_data_trig_before on trigger_test;
+DROP TRIGGER show_trigger_data_trig_after on trigger_test;
+insert into trigger_test values(1,'insert');
+CREATE VIEW trigger_test_view AS SELECT * FROM trigger_test;
+CREATE TRIGGER show_trigger_data_trig
+INSTEAD OF INSERT OR UPDATE OR DELETE ON trigger_test_view
+FOR EACH ROW EXECUTE PROCEDURE trigger_data(24,'skidoo view');
+insert into trigger_test_view values(2,'insert');
+NOTICE:  TD[args] => ['24', 'skidoo view']
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[event] => INSERT
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[level] => ROW
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[name] => show_trigger_data_trig
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[new] => {'i': 2, 'v': 'insert'}
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[old] => None
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[relid] => bogus:12345
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_name] => trigger_test_view
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_schema] => public
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[when] => INSTEAD OF
+CONTEXT:  PL/Python function "trigger_data"
+update trigger_test_view set v = 'update' where i = 1;
+NOTICE:  TD[args] => ['24', 'skidoo view']
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[event] => UPDATE
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[level] => ROW
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[name] => show_trigger_data_trig
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[new] => {'i': 1, 'v': 'update'}
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[old] => {'i': 1, 'v': 'insert'}
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[relid] => bogus:12345
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_name] => trigger_test_view
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_schema] => public
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[when] => INSTEAD OF
+CONTEXT:  PL/Python function "trigger_data"
+delete from trigger_test_view;
+NOTICE:  TD[args] => ['24', 'skidoo view']
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[event] => DELETE
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[level] => ROW
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[name] => show_trigger_data_trig
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[new] => None
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[old] => {'i': 1, 'v': 'insert'}
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[relid] => bogus:12345
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_name] => trigger_test_view
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[table_schema] => public
+CONTEXT:  PL/Python function "trigger_data"
+NOTICE:  TD[when] => INSTEAD OF
+CONTEXT:  PL/Python function "trigger_data"
+DROP FUNCTION trigger_data() CASCADE;
+NOTICE:  drop cascades to trigger show_trigger_data_trig on view trigger_test_view
+DROP VIEW trigger_test_view;
+delete from trigger_test;
+--
+-- trigger error handling
+--
+INSERT INTO trigger_test VALUES (0, 'zero');
+-- returning non-string from trigger function
+CREATE FUNCTION stupid1() RETURNS trigger
+AS $$
+    return 37
+$$ LANGUAGE plpython3u;
+CREATE TRIGGER stupid_trigger1
+BEFORE INSERT ON trigger_test
+FOR EACH ROW EXECUTE PROCEDURE stupid1();
+INSERT INTO trigger_test VALUES (1, 'one');
+ERROR:  unexpected return value from trigger procedure
+DETAIL:  Expected None or a string.
+CONTEXT:  PL/Python function "stupid1"
+DROP TRIGGER stupid_trigger1 ON trigger_test;
+-- returning MODIFY from DELETE trigger
+CREATE FUNCTION stupid2() RETURNS trigger
+AS $$
+    return "MODIFY"
+$$ LANGUAGE plpython3u;
+CREATE TRIGGER stupid_trigger2
+BEFORE DELETE ON trigger_test
+FOR EACH ROW EXECUTE PROCEDURE stupid2();
+DELETE FROM trigger_test WHERE i = 0;
+WARNING:  PL/Python trigger function returned "MODIFY" in a DELETE trigger -- ignored
+CONTEXT:  PL/Python function "stupid2"
+DROP TRIGGER stupid_trigger2 ON trigger_test;
+INSERT INTO trigger_test VALUES (0, 'zero');
+-- returning unrecognized string from trigger function
+CREATE FUNCTION stupid3() RETURNS trigger
+AS $$
+    return "foo"
+$$ LANGUAGE plpython3u;
+CREATE TRIGGER stupid_trigger3
+BEFORE UPDATE ON trigger_test
+FOR EACH ROW EXECUTE PROCEDURE stupid3();
+UPDATE trigger_test SET v = 'null' WHERE i = 0;
+ERROR:  unexpected return value from trigger procedure
+DETAIL:  Expected None, "OK", "SKIP", or "MODIFY".
+CONTEXT:  PL/Python function "stupid3"
+DROP TRIGGER stupid_trigger3 ON trigger_test;
+-- Unicode variant
+CREATE FUNCTION stupid3u() RETURNS trigger
+AS $$
+    return "foo"
+$$ LANGUAGE plpython3u;
+CREATE TRIGGER stupid_trigger3
+BEFORE UPDATE ON trigger_test
+FOR EACH ROW EXECUTE PROCEDURE stupid3u();
+UPDATE trigger_test SET v = 'null' WHERE i = 0;
+ERROR:  unexpected return value from trigger procedure
+DETAIL:  Expected None, "OK", "SKIP", or "MODIFY".
+CONTEXT:  PL/Python function "stupid3u"
+DROP TRIGGER stupid_trigger3 ON trigger_test;
+-- deleting the TD dictionary
+CREATE FUNCTION stupid4() RETURNS trigger
+AS $$
+    del TD["new"]
+    return "MODIFY";
+$$ LANGUAGE plpython3u;
+CREATE TRIGGER stupid_trigger4
+BEFORE UPDATE ON trigger_test
+FOR EACH ROW EXECUTE PROCEDURE stupid4();
+UPDATE trigger_test SET v = 'null' WHERE i = 0;
+ERROR:  TD["new"] deleted, cannot modify row
+CONTEXT:  while modifying trigger row
+PL/Python function "stupid4"
+DROP TRIGGER stupid_trigger4 ON trigger_test;
+-- TD not a dictionary
+CREATE FUNCTION stupid5() RETURNS trigger
+AS $$
+    TD["new"] = ['foo', 'bar']
+    return "MODIFY";
+$$ LANGUAGE plpython3u;
+CREATE TRIGGER stupid_trigger5
+BEFORE UPDATE ON trigger_test
+FOR EACH ROW EXECUTE PROCEDURE stupid5();
+UPDATE trigger_test SET v = 'null' WHERE i = 0;
+ERROR:  TD["new"] is not a dictionary
+CONTEXT:  while modifying trigger row
+PL/Python function "stupid5"
+DROP TRIGGER stupid_trigger5 ON trigger_test;
+-- TD not having string keys
+CREATE FUNCTION stupid6() RETURNS trigger
+AS $$
+    TD["new"] = {1: 'foo', 2: 'bar'}
+    return "MODIFY";
+$$ LANGUAGE plpython3u;
+CREATE TRIGGER stupid_trigger6
+BEFORE UPDATE ON trigger_test
+FOR EACH ROW EXECUTE PROCEDURE stupid6();
+UPDATE trigger_test SET v = 'null' WHERE i = 0;
+ERROR:  TD["new"] dictionary key at ordinal position 0 is not a string
+CONTEXT:  while modifying trigger row
+PL/Python function "stupid6"
+DROP TRIGGER stupid_trigger6 ON trigger_test;
+-- TD keys not corresponding to row columns
+CREATE FUNCTION stupid7() RETURNS trigger
+AS $$
+    TD["new"] = {'a': 'foo', 'b': 'bar'}
+    return "MODIFY";
+$$ LANGUAGE plpython3u;
+CREATE TRIGGER stupid_trigger7
+BEFORE UPDATE ON trigger_test
+FOR EACH ROW EXECUTE PROCEDURE stupid7();
+UPDATE trigger_test SET v = 'null' WHERE i = 0;
+ERROR:  key "b" found in TD["new"] does not exist as a column in the triggering row
+CONTEXT:  while modifying trigger row
+PL/Python function "stupid7"
+DROP TRIGGER stupid_trigger7 ON trigger_test;
+-- Unicode variant
+CREATE FUNCTION stupid7u() RETURNS trigger
+AS $$
+    TD["new"] = {'a': 'foo', 'b': 'bar'}
+    return "MODIFY"
+$$ LANGUAGE plpython3u;
+CREATE TRIGGER stupid_trigger7
+BEFORE UPDATE ON trigger_test
+FOR EACH ROW EXECUTE PROCEDURE stupid7u();
+UPDATE trigger_test SET v = 'null' WHERE i = 0;
+ERROR:  key "b" found in TD["new"] does not exist as a column in the triggering row
+CONTEXT:  while modifying trigger row
+PL/Python function "stupid7u"
+DROP TRIGGER stupid_trigger7 ON trigger_test;
+-- calling a trigger function directly
+SELECT stupid7();
+ERROR:  trigger functions can only be called as triggers
+--
+-- Null values
+--
+SELECT * FROM trigger_test;
+ i |  v   
+---+------
+ 0 | zero
+(1 row)
+
+CREATE FUNCTION test_null() RETURNS trigger
+AS $$
+    TD["new"]['v'] = None
+    return "MODIFY"
+$$ LANGUAGE plpython3u;
+CREATE TRIGGER test_null_trigger
+BEFORE UPDATE ON trigger_test
+FOR EACH ROW EXECUTE PROCEDURE test_null();
+UPDATE trigger_test SET v = 'null' WHERE i = 0;
+DROP TRIGGER test_null_trigger ON trigger_test;
+SELECT * FROM trigger_test;
+ i | v 
+---+---
+ 0 | 
+(1 row)
+
+--
+-- Test that triggers honor typmod when assigning to tuple fields,
+-- as per an early 9.0 bug report
+--
+SET DateStyle = 'ISO';
+CREATE FUNCTION set_modif_time() RETURNS trigger AS $$
+    TD['new']['modif_time'] = '2010-10-13 21:57:28.930486'
+    return 'MODIFY'
+$$ LANGUAGE plpython3u;
+CREATE TABLE pb (a TEXT, modif_time TIMESTAMP(0) WITHOUT TIME ZONE);
+CREATE TRIGGER set_modif_time BEFORE UPDATE ON pb
+  FOR EACH ROW EXECUTE PROCEDURE set_modif_time();
+INSERT INTO pb VALUES ('a', '2010-10-09 21:57:33.930486');
+SELECT * FROM pb;
+ a |     modif_time      
+---+---------------------
+ a | 2010-10-09 21:57:34
+(1 row)
+
+UPDATE pb SET a = 'b';
+SELECT * FROM pb;
+ a |     modif_time      
+---+---------------------
+ b | 2010-10-13 21:57:29
+(1 row)
+
+-- triggers for tables with composite types
+CREATE TABLE comp1 (i integer, j boolean);
+CREATE TYPE comp2 AS (k integer, l boolean);
+CREATE TABLE composite_trigger_test (f1 comp1, f2 comp2);
+CREATE FUNCTION composite_trigger_f() RETURNS trigger AS $$
+    TD['new']['f1'] = (3, False)
+    TD['new']['f2'] = {'k': 7, 'l': 'yes', 'ignored': 10}
+    return 'MODIFY'
+$$ LANGUAGE plpython3u;
+CREATE TRIGGER composite_trigger BEFORE INSERT ON composite_trigger_test
+  FOR EACH ROW EXECUTE PROCEDURE composite_trigger_f();
+INSERT INTO composite_trigger_test VALUES (NULL, NULL);
+SELECT * FROM composite_trigger_test;
+  f1   |  f2   
+-------+-------
+ (3,f) | (7,t)
+(1 row)
+
+-- triggers with composite type columns (bug #6559)
+CREATE TABLE composite_trigger_noop_test (f1 comp1, f2 comp2);
+CREATE FUNCTION composite_trigger_noop_f() RETURNS trigger AS $$
+    return 'MODIFY'
+$$ LANGUAGE plpython3u;
+CREATE TRIGGER composite_trigger_noop BEFORE INSERT ON composite_trigger_noop_test
+  FOR EACH ROW EXECUTE PROCEDURE composite_trigger_noop_f();
+INSERT INTO composite_trigger_noop_test VALUES (NULL, NULL);
+INSERT INTO composite_trigger_noop_test VALUES (ROW(1, 'f'), NULL);
+INSERT INTO composite_trigger_noop_test VALUES (ROW(NULL, 't'), ROW(1, 'f'));
+SELECT * FROM composite_trigger_noop_test;
+  f1   |  f2   
+-------+-------
+       | 
+ (1,f) | 
+ (,t)  | (1,f)
+(3 rows)
+
+-- nested composite types
+CREATE TYPE comp3 AS (c1 comp1, c2 comp2, m integer);
+CREATE TABLE composite_trigger_nested_test(c comp3);
+CREATE FUNCTION composite_trigger_nested_f() RETURNS trigger AS $$
+    return 'MODIFY'
+$$ LANGUAGE plpython3u;
+CREATE TRIGGER composite_trigger_nested BEFORE INSERT ON composite_trigger_nested_test
+  FOR EACH ROW EXECUTE PROCEDURE composite_trigger_nested_f();
+INSERT INTO composite_trigger_nested_test VALUES (NULL);
+INSERT INTO composite_trigger_nested_test VALUES (ROW(ROW(1, 'f'), NULL, 3));
+INSERT INTO composite_trigger_nested_test VALUES (ROW(ROW(NULL, 't'), ROW(1, 'f'), NULL));
+SELECT * FROM composite_trigger_nested_test;
+         c         
+-------------------
+ 
+ ("(1,f)",,3)
+ ("(,t)","(1,f)",)
+(3 rows)
+
+-- check that using a function as a trigger over two tables works correctly
+CREATE FUNCTION trig1234() RETURNS trigger LANGUAGE plpython3u AS $$
+    TD["new"]["data"] = '1234'
+    return 'MODIFY'
+$$;
+CREATE TABLE a(data text);
+CREATE TABLE b(data int); -- different type conversion
+CREATE TRIGGER a_t BEFORE INSERT ON a FOR EACH ROW EXECUTE PROCEDURE trig1234();
+CREATE TRIGGER b_t BEFORE INSERT ON b FOR EACH ROW EXECUTE PROCEDURE trig1234();
+INSERT INTO a DEFAULT VALUES;
+SELECT * FROM a;
+ data 
+------
+ 1234
+(1 row)
+
+DROP TABLE a;
+INSERT INTO b DEFAULT VALUES;
+SELECT * FROM b;
+ data 
+------
+ 1234
+(1 row)
+
--- a/src/pl/plpython/expected/plpython_params.out
+++ b/src/pl/plpython/expected/plpython_params.out
@@ -34,13 +34,13 @@ SELECT test_param_names1(1,'text');
  t
 (1 row)
 
-SELECT test_param_names2(users) from users;
-                           test_param_names2                           
------------------------------------------------------------------------
- {'lname': 'doe', 'username': 'j_doe', 'userid': 1, 'fname': 'jane'}
- {'lname': 'doe', 'username': 'johnd', 'userid': 2, 'fname': 'john'}
- {'lname': 'doe', 'username': 'w_doe', 'userid': 3, 'fname': 'willem'}
- {'lname': 'smith', 'username': 'slash', 'userid': 4, 'fname': 'rick'}
+SELECT length(test_param_names2(users)) from users;
+ length 
+--------
+     67
+     67
+     69
+     69
 (4 rows)
 
 SELECT test_param_names2(NULL);
--- a/src/pl/plpython/sql/plpython_params.sql
+++ b/src/pl/plpython/sql/plpython_params.sql
@@ -30,6 +30,6 @@ $$ LANGUAGE plpythonu;
 
 SELECT test_param_names0(2,7);
 SELECT test_param_names1(1,'text');
-SELECT test_param_names2(users) from users;
+SELECT length(test_param_names2(users)) from users;
 SELECT test_param_names2(NULL);
 SELECT test_param_names3(1);
--- a/src/pl/plpython/Makefile
+++ b/src/pl/plpython/Makefile
@@ -80,7 +80,6 @@ REGRESS = \
 	plpython_params \
 	plpython_setof \
 	plpython_record \
-	plpython_trigger \
 	plpython_types \
 	plpython_error \
 	plpython_unicode \
--- /dev/null
+++ b/src/pl/plpython/expected/plpython_subtransaction_3.out
@@ -0,0 +1,411 @@
+--
+-- 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:  Traceback (most recent call last):
+  PL/Python function "subtransaction_test", line 11, in <module>
+    plpy.execute("INSERT INTO subtransaction_tbl VALUES ('oops')")
+PL/Python function "subtransaction_test"
+SELECT * FROM subtransaction_tbl;
+ i 
+---
+(0 rows)
+
+TRUNCATE subtransaction_tbl;
+SELECT subtransaction_test('Python');
+ERROR:  AttributeError: module 'plpy' has no attribute 'attribute_error'
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "subtransaction_test", line 13, in <module>
+    plpy.attribute_error
+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:  Traceback (most recent call last):
+  PL/Python function "subtransaction_ctx_test", line 6, in <module>
+    plpy.execute("INSERT INTO subtransaction_tbl VALUES ('oops')")
+PL/Python function "subtransaction_ctx_test"
+SELECT * FROM subtransaction_tbl;
+ i 
+---
+(0 rows)
+
+TRUNCATE subtransaction_tbl;
+SELECT subtransaction_ctx_test('Python');
+ERROR:  AttributeError: module 'plpy' has no attribute 'attribute_error'
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "subtransaction_ctx_test", line 8, in <module>
+    plpy.attribute_error
+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:  Traceback (most recent call last):
+  PL/Python function "subtransaction_nested_test", line 8, in <module>
+    plpy.execute("error")
+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:  Traceback (most recent call last):
+  PL/Python function "subtransaction_exit_without_enter", line 2, in <module>
+    plpy.subtransaction().__exit__(None, None, None)
+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:  Traceback (most recent call last):
+  PL/Python function "subtransaction_exit_twice", line 3, in <module>
+    plpy.subtransaction().__exit__(None, None, None)
+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:  Traceback (most recent call last):
+  PL/Python function "subtransaction_exit_same_subtransaction_twice", line 5, in <module>
+    s.__exit__(None, None, None)
+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:  Traceback (most recent call last):
+  PL/Python function "subtransaction_enter_same_subtransaction_twice", line 4, in <module>
+    s.__enter__()
+PL/Python function "subtransaction_enter_same_subtransaction_twice"
+SELECT subtransaction_enter_subtransaction_in_with();
+ERROR:  ValueError: this subtransaction has already been entered
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "subtransaction_enter_subtransaction_in_with", line 3, in <module>
+    s.__enter__()
+PL/Python function "subtransaction_enter_subtransaction_in_with"
+SELECT subtransaction_exit_subtransaction_in_with();
+ERROR:  ValueError: this subtransaction has already been exited
+CONTEXT:  Traceback (most recent call last):
+  PL/Python function "subtransaction_exit_subtransaction_in_with", line 2, in <module>
+    with plpy.subtransaction() as s:
+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;
