1 CREATE TABLE test1 (a int, b text);
2 CREATE PROCEDURE transaction_test1()
6 plpy.execute("INSERT INTO test1 (a) VALUES (%d)" % i)
12 CALL transaction_test1();
27 for i in range(0, 10):
28 plpy.execute("INSERT INTO test1 (a) VALUES (%d)" % i)
45 -- not allowed in a function
46 CREATE FUNCTION transaction_test2() RETURNS int
49 for i in range(0, 10):
50 plpy.execute("INSERT INTO test1 (a) VALUES (%d)" % i)
57 SELECT transaction_test2();
58 ERROR: invalid transaction termination
59 CONTEXT: PL/Python function "transaction_test2"
65 -- also not allowed if procedure is called from a function
66 CREATE FUNCTION transaction_test3() RETURNS int
69 plpy.execute("CALL transaction_test1()")
72 SELECT transaction_test3();
73 ERROR: spiexceptions.InvalidTransactionTermination: invalid transaction termination
74 CONTEXT: Traceback (most recent call last):
75 PL/Python function "transaction_test3", line 2, in <module>
76 plpy.execute("CALL transaction_test1()")
77 PL/Python function "transaction_test3"
83 -- DO block inside function
84 CREATE FUNCTION transaction_test4() RETURNS int
87 plpy.execute("DO LANGUAGE plpythonu $x$ plpy.commit() $x$")
90 SELECT transaction_test4();
91 ERROR: spiexceptions.InvalidTransactionTermination: invalid transaction termination
92 CONTEXT: Traceback (most recent call last):
93 PL/Python function "transaction_test4", line 2, in <module>
94 plpy.execute("DO LANGUAGE plpythonu $x$ plpy.commit() $x$")
95 PL/Python function "transaction_test4"
96 -- commit inside subtransaction (prohibited)
97 DO LANGUAGE plpythonu $$
98 s = plpy.subtransaction()
102 WARNING: forcibly aborting a subtransaction that has not been exited
103 ERROR: cannot commit while a subtransaction is active
104 CONTEXT: PL/Python anonymous code block
105 -- commit inside cursor loop
106 CREATE TABLE test2 (x int);
107 INSERT INTO test2 VALUES (0), (1), (2), (3), (4);
109 DO LANGUAGE plpythonu $$
110 for row in plpy.cursor("SELECT * FROM test2 ORDER BY x"):
111 plpy.execute("INSERT INTO test1 (a) VALUES (%s)" % row['x'])
124 -- check that this doesn't leak a holdable portal
125 SELECT * FROM pg_cursors;
126 name | statement | is_holdable | is_binary | is_scrollable | creation_time
127 ------+-----------+-------------+-----------+---------------+---------------
130 -- error in cursor loop with commit
132 DO LANGUAGE plpythonu $$
133 for row in plpy.cursor("SELECT * FROM test2 ORDER BY x"):
134 plpy.execute("INSERT INTO test1 (a) VALUES (12/(%s-2))" % row['x'])
137 ERROR: spiexceptions.DivisionByZero: division by zero
138 CONTEXT: Traceback (most recent call last):
139 PL/Python anonymous code block, line 3, in <module>
140 plpy.execute("INSERT INTO test1 (a) VALUES (12/(%s-2))" % row['x'])
141 PL/Python anonymous code block
149 SELECT * FROM pg_cursors;
150 name | statement | is_holdable | is_binary | is_scrollable | creation_time
151 ------+-----------+-------------+-----------+---------------+---------------
154 -- rollback inside cursor loop
156 DO LANGUAGE plpythonu $$
157 for row in plpy.cursor("SELECT * FROM test2 ORDER BY x"):
158 plpy.execute("INSERT INTO test1 (a) VALUES (%s)" % row['x'])
166 SELECT * FROM pg_cursors;
167 name | statement | is_holdable | is_binary | is_scrollable | creation_time
168 ------+-----------+-------------+-----------+---------------+---------------
171 -- first commit then rollback inside cursor loop
173 DO LANGUAGE plpythonu $$
174 for row in plpy.cursor("SELECT * FROM test2 ORDER BY x"):
175 plpy.execute("INSERT INTO test1 (a) VALUES (%s)" % row['x'])
176 if row['x'] % 2 == 0:
189 SELECT * FROM pg_cursors;
190 name | statement | is_holdable | is_binary | is_scrollable | creation_time
191 ------+-----------+-------------+-----------+---------------+---------------