Appearance
BEGIN
Start a transaction block.
BEGIN [ WORK | TRANSACTION ]
SQL statements that follow a BEGIN statement are executed in a single transaction until an explicit COMMIT or ROLLBACK command is executed. WORK
and TRANSACTION
are both optional keywords; they have no effect.
START TRANSACTION
is accepted alternative syntax for this command.
Explicit transactions are useful because they offer a consistent view of the database to other users and sessions when you are making a series of related changes. The complete set of changes is visible when the transaction commits, but no intermediate states are visible.
By default, Yellowbrick automatically commits transactions one SQL statement at a time. Client tools should never turn off automatic commit; turning it off may create downstream blocking of transactions for other users in the system.
The only supported transaction isolation level is read committed
. Any user can set the transaction isolation level to read committed
.
For example, drop five tables in one transaction:
premdb=# begin;
BEGIN
premdb=# drop table season;
DROP TABLE
premdb=# drop table team;
DROP TABLE
premdb=# drop table hometeam;
DROP TABLE
premdb=# drop table awayteam;
DROP TABLE
premdb=# drop table match;
DROP TABLE
premdb=# commit;
COMMIT
Setting Transactions to Read-Only
You can control read-only access for users at the session level by setting the default_transaction_read_only
command. When this parameter is set to ON
in a session, the user is restricted to running queries and cannot create or modify objects. For example:
premdb=# create table t1(c1 int);
CREATE TABLE
premdb=# drop table t1;
DROP TABLE
premdb=# set default_transaction_read_only to on;
SET
premdb=# create table t1(c1 int);
ERROR: cannot execute CREATE TABLE in a read-only transaction