4.1 What Is a Transaction

A transaction groups several data operations (delete, insert, select, create) into one unit of work. An application can create a transaction and apply it to the database (commit) as follows.

BEGIN;
UPDATE account SET balance = balance - 100000 WHERE id = 'hong';
UPDATE account SET balance = balance + 100000 WHERE id = 'kim';
COMMIT;

The BEGIN statement declares the start of the transaction. After that, you define the work the transaction represents (in the example, increasing and decreasing the balances of two user records). When the definition is finished, the COMMIT statement applies it to the database for good. If a unique constraint is violated or the balance falls below 0, the ROLLBACK statement can cancel the transaction. Even if it is rolled back while changing the second user record, the change already made to the first user record is cancelled too, and both records remain unchanged.

The important fact here is that the database treats the state between the two UPDATE statements as if it does not exist. In reality the first user record changes first and the second changes later, but from outside the transaction this intermediate state is never visible under any circumstances. Even if the server terminates abnormally or another user queries the balance midway, the intermediate state (only user A's record changed) is not visible. This property is called isolation. A transaction's intermediate state cannot be referenced by anything other than the transaction itself.

Another point to know is that a transaction does not have to consist of multiple statements. If one UPDATE statement modifies 1,000 rows, that too is a transaction that handles 1,000 changes in a single query. If an error occurs at the 500th row, the preceding 499 are cancelled as well. The key point is that the unit of a transaction is neither a statement, a page, nor a record, but a piece of work.

A transaction has a unique number and a state. Each time a transaction starts, the database assigns it a unique number (transaction ID, XID). Transaction numbers increase monotonically, so comparing two numbers tells you which transaction started first. Each transaction is in one of three states: active, committed, or aborted. The database records "which transaction number is in which state" in a separate data structure.

The three states of a transaction

stateDiagram-v2
  [*] --> Active: Transaction starts, XID issued
  Active --> Committed: COMMIT
  Active --> Aborted: ROLLBACK or error
  Committed --> [*]
  Aborted --> [*]

A transaction receives a transaction ID the moment it starts and becomes active. Changes made in this state are not yet final. When COMMIT succeeds, it moves to the committed state; when ROLLBACK runs or an error prevents it from continuing, it moves to the aborted state. The two end states never change into each other, and a transaction that has been committed or aborted never returns to the active state. What the database records is, for each transaction ID, which of these three states it is in.