PostgreSQL executes transactions in
by default. Also called
, 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:
CREATE TABLE test (id integer, name text);