20
Subtransactions
Separate ID and status in CLOG
final status depends on the status of the main transaction
Nesting information is stored on disk
PGDATA/pg_subtrans directory
cached in the shared memory buffers (similar to CLOG)
Use cases
SAVEPOINT
PL/pgSQL exception handling (EXCEPTION)
psql mode: ON_ERROR_ROLLBACK = on/interactive
Subtransactions have their own IDs (which are higher than the ID of the
main transaction). Their status is written to CLOG in the usual manner, but
the final status depends on the status of the main transaction: if it is aborted,
all subtransactions are aborted as well.
Information about transaction nesting is stored in the PGDATA/pg_subtrans
directory. Its files are accessed through buffers in the server’s shared
memory, which are structured the same way as CLOG buffers.
Note that SQL does not permit explicit use of subtransactions, that is, you
cannot start a new transaction before you complete the current one. This
mechanism gets implicitly involved when savepoints are used and also when
handling PL/pgSQL exceptions.
It is worth noting that ON_ERROR_ROLLBACK mode in psql allows
continuing the transaction after failure. Why is this mode not used by
default? The reason is that an error might occur in the middle of a
statement’s execution, so the atomicity would be broken. The only way to
abort changes already made by that specific statement, without affecting
other changes, is to use subtransactions. So, ON_ERROR_ROLLBACK
mode places an implicit savepoint before each command, resulting in
significant overhead.