In a procedure called from the top level or an anonymous code block
(<command>DO</command> command) called from the top level it is possible to
control transactions. To commit the current transaction, call
<literal>plpy.commit()</literal>. To roll back the current transaction,
call <literal>plpy.rollback()</literal>. (Note that it is not possible to
run the SQL commands <command>COMMIT</command> or
<command>ROLLBACK</command> via <function>plpy.execute</function> 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.
トップレベル、またはトップレベルから呼ばれた無名コードブロック(DOコマンド)から呼ばれたプロシージャでは、トランザクションの制御が可能です。
現在のトランザクションをコミットするには、plpy.commit()を呼びます。
現在のロールバックするには、plpy.rollback()を呼びます。
(SQLコマンドのCOMMITやROLLBACKをplpy.executeなどを通して実行することはできない点に注意してください。前述の関数を使って行う必要があります。)
トランザクションが終了した後は新たなトランザクションが自動的に開始されますので、開始のための別の関数はありません。
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. トランザクションは明示的なサブトランザクションの中では終了できません。