September 25, 2025: PostgreSQL 18 Released!
DocumentationPostgreSQL devel (2025年09月29日 20:56:03 - git commit ef38a4d9756)
Supported Versions: Current (18) / 17 / 16 / 15 / 14 / 13
Development Versions: devel
Unsupported versions: 12 / 11
This documentation is for an unsupported version of PostgreSQL.
You may want to view the same page for the current version, or one of the other supported versions listed above instead.
44.9. Transaction Management
Prev Up Chapter 44. PL/Python — Python Procedural Language Home Next

44.9. Transaction Management #

In a procedure called from the top level or an anonymous code block (DO command) called from the top level it is possible to control transactions. To commit the current transaction, call plpy.commit(). To roll back the current transaction, call plpy.rollback(). (Note that it is not possible to run the SQL commands COMMIT or ROLLBACK via plpy.execute or similar. It has to be done using these functions.) After a transaction is ended, a new transaction is automatically started, so there is no separate function for that.

Here is an example:

CREATE PROCEDURE transaction_test1()
LANGUAGE plpython3u
AS $$
for i in range(0, 10):
 plpy.execute("INSERT INTO test1 (a) VALUES (%d)" % i)
 if i % 2 == 0:
 plpy.commit()
 else:
 plpy.rollback()
$$;
CALL transaction_test1();

Transactions cannot be ended when an explicit subtransaction is active.


Prev Up Next
44.8. Explicit Subtransactions Home 44.10. Utility Functions

AltStyle によって変換されたページ (->オリジナル) /