16.3. Example

The following figure describes an example where a transaction executes a subtransaction. A continuous line denotes an active transaction, while a dotted line denotes a transaction which has been paused and pushed in the AST stack. Time flows downwards.

BEGIN; -- start ordinary tx T0
|
INSERT INTO t VALUES (1);
:\
: BEGIN AUTONOMOUS SUBTRANSACTION; -- start AST tx T1, pushes T0 into stack
: |
: INSERT INTO t VALUES (2);
: |
: COMMIT AUTONOMOUS TRANSACTION / ROLLBACK AUTONOMOUS TRANSACTION; -- ends tx T1, pops tx T0 from stack
:/
COMMIT / ROLLBACK; -- ends tx T0

Depending on the two choices between COMMIT and ROLLBACK we can get 4 different outputs from:

SELECT sum(x) from t;