SET_TRANSACTION(7)

๐Ÿ’… SET_TRANSACTION โ€” setting the vibe of ur current transaction โœจ

okay bestie, so this is the SET TRANSACTION page from the PostgreSQL 18.6 docs. it's giving "how do i tell my transaction how to behave" and honestly? iconic. let's get into it ๐Ÿซถ


๐ŸŒธ NAME ๐ŸŒธ

SET_TRANSACTION โ€” literally just: set the characteristics of the current transaction. that's the whole job. no cap. ๐Ÿ’–


โœจ SYNOPSIS โœจ

SET TRANSACTION transaction_mode [, ...]
SET TRANSACTION SNAPSHOT snapshot_id
SET SESSION CHARACTERISTICS AS TRANSACTION transaction_mode [, ...]

and transaction_mode is one of these lil guys:

ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED }
READ WRITE | READ ONLY
[ NOT ] DEFERRABLE

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

so SET TRANSACTION sets the characteristics of the current transaction. key word: current. it does not touch any subsequent transactions, bestie. it's a one-transaction wonder. โœจ

meanwhile SET SESSION CHARACTERISTICS sets the default transaction characteristics for subsequent transactions in a session. so it's the "set the vibe for the rest of the night" option. and those defaults can still be overridden by SET TRANSACTION for one individual transaction. ๐Ÿ’…

the available transaction characteristics are:

  • the transaction isolation level
  • the transaction access mode (read/write or read-only)
  • the deferrable mode

plus you can select a snapshot โ€” but only for the current transaction, not as a session default. that's a this-transaction-only kinda deal, fr fr.

๐Ÿงš isolation level: what can u even see

the isolation level of a transaction determines what data the transaction can see when other transactions are running concurrently. so basically: how much chaos from other people's queries are you exposed to.

  • READ COMMITTED โ€” a statement can only see rows committed before it began. this is the default. yes, the default is the chill one. ๐Ÿ“
  • REPEATABLE READ โ€” all statements of the current transaction can only see rows committed before the first query or data-modification statement was executed in this transaction.
  • SERIALIZABLE โ€” all statements of the current transaction can only see rows committed before the first query or data-modification statement was executed in this transaction. AND, if a pattern of reads and writes among concurrent serializable transactions would create a situation which could not have occurred for any serial (one-at-a-time) execution of those transactions, one of them will be rolled back with a serialization_failure error. so like, somebody's getting cancelled and it might be you ๐Ÿ˜ญ

the SQL standard defines one additional level, READ UNCOMMITTED. in PostgreSQL, READ UNCOMMITTED is treated as READ COMMITTED. so it's the fake one. it's giving costume jewelry. ๐Ÿ’€

the transaction isolation level cannot be changed after the first query or data-modification statement (SELECT, INSERT, DELETE, UPDATE, MERGE, FETCH, or COPY) of a transaction has been executed. so decide early or don't decide at all, bestie.

see Chapter 13 for more information about transaction isolation and concurrency control. ๐Ÿ“–

๐ŸŽ€ access mode: read/write or read-only

the transaction access mode determines whether the transaction is read/write or read-only. read/write is the default. periodt.

when a transaction is read-only, these SQL commands are disallowed:

  • INSERT, UPDATE, DELETE, MERGE, and COPY FROM โ€” if the table they would write to is not a temporary table
  • all CREATE, ALTER, and DROP commands
  • COMMENT, GRANT, REVOKE, TRUNCATE
  • EXPLAIN ANALYZE and EXECUTE โ€” if the command they would execute is among those listed
heads up: this is a high-level notion of read-only that does not prevent all writes to disk. so it's not a security boundary, it's a vibe boundary. ๐Ÿ™ˆ

๐ŸŒˆ the DEFERRABLE thing

the DEFERRABLE transaction property has no effect unless the transaction is also SERIALIZABLE and READ ONLY. all three or it's nothing, bestie.

when all three of these properties are selected for a transaction, the transaction may block when first acquiring its snapshot, after which it is able to run without the normal overhead of a SERIALIZABLE transaction and without any risk of contributing to or being canceled by a serialization failure. slay. โœจ

this mode is well suited for long-running reports or backups. the "i'm just gonna sit here and read everything for an hour" mode. ๐Ÿ‘‘

๐Ÿซถ SET TRANSACTION SNAPSHOT

the SET TRANSACTION SNAPSHOT command allows a new transaction to run with the same snapshot as an existing transaction. matching snapshots, matching vibes. ๐Ÿฆ‹

  • the pre-existing transaction must have exported its snapshot with the pg_export_snapshot function (see Section 9.28.5)
  • that function returns a snapshot identifier, which must be given to SET TRANSACTION SNAPSHOT to specify which snapshot is to be imported
  • the identifier must be written as a string literal in this command, for example '00000003-0000001B-1'
  • SET TRANSACTION SNAPSHOT can only be executed at the start of a transaction, before the first query or data-modification statement (SELECT, INSERT, DELETE, UPDATE, MERGE, FETCH, or COPY) of the transaction
  • furthermore, the transaction must already be set to SERIALIZABLE or REPEATABLE READ isolation level โ€” otherwise, the snapshot would be discarded immediately, since READ COMMITTED mode takes a new snapshot for each command
  • if the importing transaction uses SERIALIZABLE isolation level, then the transaction that exported the snapshot must also use that isolation level
  • also, a non-read-only serializable transaction cannot import a snapshot from a read-only transaction

๐Ÿ’œ NOTES ๐Ÿ’œ

  • if SET TRANSACTION is executed without a prior START TRANSACTION or BEGIN, it emits a warning and otherwise has no effect. so it's just gonna yell into the void. ๐Ÿ˜ญ
  • it is possible to dispense with SET TRANSACTION by instead specifying the desired transaction_modes in BEGIN or START TRANSACTION. but that option is not available for SET TRANSACTION SNAPSHOT.
  • the session default transaction modes can also be set or examined via the configuration parameters default_transaction_isolation, default_transaction_read_only, and default_transaction_deferrable. (in fact SET SESSION CHARACTERISTICS is just a verbose equivalent for setting these variables with SET.) this means the defaults can be set in the configuration file, via ALTER DATABASE, etc. consult Chapter 19 for more information.
  • the current transaction's modes can similarly be set or examined via the configuration parameters transaction_isolation, transaction_read_only, and transaction_deferrable. setting one of these parameters acts the same as the corresponding SET TRANSACTION option, with the same restrictions on when it can be done. however, these parameters cannot be set in the configuration file, or from any source other than live SQL. live only, bestie. ๐Ÿ’…

โญ EXAMPLES โญ

to begin a new transaction with the same snapshot as an already existing transaction, first export the snapshot from the existing transaction. that will return the snapshot identifier, for example:

BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
SELECT pg_export_snapshot();
 pg_export_snapshot
---------------------
 00000003-0000001B-1
(1 row)

then give the snapshot identifier in a SET TRANSACTION SNAPSHOT command at the beginning of the newly opened transaction:

BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
SET TRANSACTION SNAPSHOT '00000003-0000001B-1';

look at that. two transactions, one shared snapshot. main character energy. ๐Ÿ‘‘


๐Ÿ”ฅ COMPATIBILITY ๐Ÿ”ฅ

  • these commands are defined in the SQL standard, except for the DEFERRABLE transaction mode and the SET TRANSACTION SNAPSHOT form, which are PostgreSQL extensions. homegrown. ๐ŸŒฑ
  • SERIALIZABLE is the default transaction isolation level in the standard. in PostgreSQL the default is ordinarily READ COMMITTED, but you can change it as mentioned above.
  • in the SQL standard, there is one other transaction characteristic that can be set with these commands: the size of the diagnostics area. this concept is specific to embedded SQL, and therefore is not implemented in the PostgreSQL server. so it's just not here. ๐Ÿ’€
  • the SQL standard requires commas between successive transaction_modes, but for historical reasons PostgreSQL allows the commas to be omitted. the commas are optional, the drama is not. โœจ

and that's the whole page, bestie!! this was PostgreSQL 18.6, documented back in 2026, and you just read SET TRANSACTION(7). go forth and set ur transaction characteristics responsibly. mwah ๐Ÿ’‹๐ŸŽ€๐Ÿฆ‹