]> begriffs open source - ai-pg/blob - full-docs/txt/plpython-transactions.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / plpython-transactions.txt
1
2 44.8. Transaction Management #
3
4    In a procedure called from the top level or an anonymous code block (DO
5    command) called from the top level it is possible to control
6    transactions. To commit the current transaction, call plpy.commit(). To
7    roll back the current transaction, call plpy.rollback(). (Note that it
8    is not possible to run the SQL commands COMMIT or ROLLBACK via
9    plpy.execute or similar. It has to be done using these functions.)
10    After a transaction is ended, a new transaction is automatically
11    started, so there is no separate function for that.
12
13    Here is an example:
14 CREATE PROCEDURE transaction_test1()
15 LANGUAGE plpython3u
16 AS $$
17 for i in range(0, 10):
18     plpy.execute("INSERT INTO test1 (a) VALUES (%d)" % i)
19     if i % 2 == 0:
20         plpy.commit()
21     else:
22         plpy.rollback()
23 $$;
24
25 CALL transaction_test1();
26
27    Transactions cannot be ended when an explicit subtransaction is active.