Postgresql 中文操作指南

BEGIN

BEGIN — 启动事务块

BEGIN — start a transaction block

Synopsis

BEGIN [ WORK | TRANSACTION ] [ transaction_mode [, ...] ]

where transaction_mode is one of:

    ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED }
    READ WRITE | READ ONLY
    [ NOT ] DEFERRABLE

Description

BEGIN 启动事务块,即 BEGIN 命令之后的全部语句将在单个事务中执行,直至给出显示的 COMMITROLLBACK 。默认情况下(没有 BEGIN ),PostgreSQL 以“自动提交”模式执行事务,即每条语句都在其事务中执行,并在语句结束时隐含执行提交(如果执行成功;否则执行回滚)。

BEGIN initiates a transaction block, that is, all statements after a BEGIN command will be executed in a single transaction until an explicit COMMIT or ROLLBACK is given. By default (without BEGIN), PostgreSQL executes transactions in “autocommit” mode, that is, each statement is executed in its own transaction and a commit is implicitly performed at the end of the statement (if execution was successful, otherwise a rollback is done).

在事务块中执行语句速度更快,因为事务开始/提交需要大量 CPU 和磁盘活动。在事务中执行多条语句还有助于在执行多个相关更改时确保一致性:其他会话将无法看到那些尚未完成所有相关更新的中间状态。

Statements are executed more quickly in a transaction block, because transaction start/commit requires significant CPU and disk activity. Execution of multiple statements inside a transaction is also useful to ensure consistency when making several related changes: other sessions will be unable to see the intermediate states wherein not all the related updates have been done.

如果指定了隔离级别、读/写模式或可延迟模式,新事务将具有那些特性,就像执行了 SET TRANSACTION

If the isolation level, read/write mode, or deferrable mode is specified, the new transaction has those characteristics, as if SET TRANSACTION was executed.

Parameters

  • WORK__TRANSACTION

    • Optional key words. They have no effect.

有关此语句中其他参数含义的信息,请参阅 SET TRANSACTION

Refer to SET TRANSACTION for information on the meaning of the other parameters to this statement.

Notes

START TRANSACTION 具有与 BEGIN 相同的功能。

START TRANSACTION has the same functionality as BEGIN.

使用 COMMITROLLBACK 终止事务块。

Use COMMIT or ROLLBACK to terminate a transaction block.

当已在事务块内时发出 BEGIN ,将触发警告消息。不会影响事务的状态。要在事务块内嵌套事务,请使用保存点(请参阅 SAVEPOINT )。

Issuing BEGIN when already inside a transaction block will provoke a warning message. The state of the transaction is not affected. To nest transactions within a transaction block, use savepoints (see SAVEPOINT).

为了向后兼容,可以省略连续的 transaction_modes 之间的逗号。

For reasons of backwards compatibility, the commas between successive transaction_modes can be omitted.

Examples

开始事务块:

To begin a transaction block:

BEGIN;

Compatibility

BEGIN 是 PostgreSQL 语言扩展。它等同于 SQL 标准命令 START TRANSACTION ,其参考页面包含其他兼容性信息。

BEGIN is a PostgreSQL language extension. It is equivalent to the SQL-standard command START TRANSACTION, whose reference page contains additional compatibility information.

DEFERRABLE transaction_mode 是 PostgreSQL 语言扩展。

The DEFERRABLE transaction_mode is a PostgreSQL language extension.

顺便说一句, BEGIN 关键字在嵌入式 SQL 中具有不同的用途。在移植数据库应用程序时,建议您谨慎对待事务语义。

Incidentally, the BEGIN key word is used for a different purpose in embedded SQL. You are advised to be careful about the transaction semantics when porting database applications.