BEGIN [ WORK | TRANSACTION ]
A pair of optional noise keywords. They can be ignored, or used to make your SQL more readable.
The message returned when a transaction begins.
The notice returned if a transaction is already in progress within your PostgreSQL session. You cannot nest multiple transactions. The transaction you have in progress is not altered when this happens.
PostgreSQL executes transactions in unchained mode by default. Also called autocommit, this mode encapsulates each user statement in an implicit transaction, and automatically finalizes the transaction by either committing the modification, or performing a rollback, depending on whether or not the execution was successful.
Using BEGIN specifies that you want to enter a transaction block using chained mode, in which statements will be queued by the database, and then sent in a single transaction when the database receives a COMMIT command. Alternatively, the queued statements can be discarded by a ROLLBACK, or by an unexpected disconnection.
Chained mode can be useful when you are working with multiple related tables, and also to increase database performance in general. Executing statements in chained mode uses less CPU and disk resources, as there is only one commit needed per block of statements executed.
When a transaction is committed, the database will attempt to run all updates that have been specified within it. If there are were no errors, the updates will be performed; otherwise the transaction block will be aborted.
The following example begins a transaction block, creates a table, and commits the action:
booktown=# BEGIN WORK; BEGIN booktown=# CREATE TABLE test (id integer, name text); CREATE booktown=# COMMIT WORK; COMMIT