バージョンごとのドキュメント一覧

44.10. トランザクション制御 #

<title>Transaction Management</title>

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

Here is an example: 以下に例を示します。

CREATE PROCEDURE transaction_test1()
LANGUAGE pltcl
AS $$
for {set i 0} {$i < 10} {incr i} {
    spi_exec "INSERT INTO test1 (a) VALUES ($i)"
    if {$i % 2 == 0} {
        commit
    } else {
        rollback
    }
}
$$;

CALL transaction_test1();

Transactions cannot be ended when an explicit subtransaction is active. 明示的なサブトランザクションの中ではトランザクションを終了することはできません。