Computer Science
BEGIN(l) BEGIN(l)
NAME
BEGIN - Begins a transaction in chained mode
SYNOPSIS
BEGIN [ WORK | TRANSACTION ]
INPUTS
WORK
TRANSACTION
Optional keywords. They have no effect.
OUTPUTS
BEGIN This signifies that a new transaction has been
started.
NOTICE: BeginTransactionBlock and not in default state
This indicates that a transaction was already in
progress. The current transaction is not affected.
DESCRIPTION
By default, Postgres executes transactions in unchained
mode (also known as ``autocommit'' in other database sys-
tems). In other words, each user 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). BEGIN initiates a user
transaction in chained mode, i.e. all user statements
after BEGIN command will be executed in a single transac-
tion until an explicit commit(l), rollback(l), or execu-
tion abort. Statements in chained mode are executed much
faster, because transaction start/commit requires signifi-
cant CPU and disk activity. Execution of multiple state-
ments inside a transaction is also required for consis-
tency when changing several related tables.
The default transaction isolation level in Postgres is
READ COMMITTED, where queries inside the transaction see
only changes committed before query execution. So, you
have to use SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
just after BEGIN if you need more rigorous transaction
isolation. In SERIALIZABLE mode queries will see only
changes committed before the entire transaction began
(actually, before execution of the first DML statement in
a serializable transaction).
If the transaction is committed, Postgres will ensure
either that all updates are done or else that none of them
are done. Transactions have the standard ACID (atomic,
consistent, isolatable, and durable) property.
NOTES
The keyword TRANSACTION is just a cosmetic alternative to
WORK. Neither keyword need be specified.
Refer to lock(l) for further information about locking
tables inside a transaction.
Use commit(l) or rollback(l) to terminate a transaction.
USAGE
To begin a user transaction:
BEGIN WORK;
COMPATIBILITY
SQL92
BEGIN is a Postgres language extension. There is no
explicit BEGIN command in SQL92; transaction initiation is
always implicit and it terminates either with a COMMIT or
ROLLBACK statement.
Note: Many relational database systems offer an
autocommit feature as a convenience.
SQL92 also requires SERIALIZABLE to be the default trans-
action isolation level.
SQL - Language Statements 15 August 1999 1
Back to the index